Files
foxhunt/crates
jgrusewski 3f9b34030c feat(sp20): Phase 2 Task 2.2 — SP12 v3 reward block → 4-quadrant + alpha plumbing (atomic)
Lands the SP20 4-quadrant reward at the experience_env_step::
segment_complete site, atomically with per-env alpha plumbing through
the SP20 aggregation kernel per `feedback_no_partial_refactor`:

* `experience_env_step.cu` — replace SP12 v3 inlined block (vol-norm
  calc + base_reward + SP18 D-leg trade-close call + asymmetric cap +
  min-hold penalty + r_popart/r_trail cascade, ~260 LoC) with the SP20
  4-quadrant reward (~80 LoC) computing:
    R_event = sp20_compute_event_reward(segment_return,
                                         label_at_open_per_env[i],
                                         ISV[LOSS_CAP_INDEX])
    alpha   = R_event - 0.0  (Phase 3.2 hold_baseline placeholder)
    r_used  = alpha - ISV[ALPHA_EMA_INDEX]    (advantage centering)
    alpha_per_env[i] = alpha   (consumed by sp20_aggregate)
    r_popart / r_trail = r_used (SP11 component split preserved)

  Adds 3 new kernel args at the end (preserves Task 2.0 args):
    float* alpha_per_env, int loss_cap_idx, int alpha_ema_idx
  Deletes 3 SP12 v3 args (min_hold_target, min_hold_penalty_max,
  min_hold_temperature) per `feedback_no_hiding` "wire up or delete".

* `sp20_aggregate_inputs_kernel.cu` — add `alpha_per_env` input arg
  (NULL-tolerant), 5th shmem stripe (sh_alpha_sum, was 4 stripes),
  per-thread close-gated accumulation, 5-way tree-reduce loop body,
  output write `alpha = mean / closed_count`. Replaces the Phase 1.4
  hardcoded `out_inputs->alpha = 0.0f` placeholder atomically.

* `sp20_aggregate_inputs.rs` launcher — +1 arg `alpha_per_env_dev`
  (passes 0/NULL for tests). Shmem byte-count: 4 stripes → 5 stripes.

* `gpu_experience_collector.rs` — new `alpha_per_env: CudaSlice<f32>`
  field [alloc_episodes], allocated next to `label_at_open_per_env`,
  threaded into env_step launch + sp20_aggregate launch. Removes the
  3 obsolete config.min_hold_* `.arg(...)` calls from env_step launch.

* `state_reset_registry.rs` — `alpha_per_env` FoldReset entry +
  invariant test `sp20_alpha_per_env_registered_fold_reset`.

* `training_loop.rs::reset_named_state` — `alpha_per_env` dispatch arm
  via `stream.memset_zeros` (mirrors `label_at_open_per_env` pattern).

* Pin-test updates per spec (atomic with the contract change):
  - `sp20_aggregate_inputs_test::alpha_and_per_bar_hold_reward_are_
    phase_2_and_3_2_placeholders` →
    `null_alpha_producer_keeps_phase_1_4_placeholder_contract` (asserts
    NULL alpha producer ⇒ alpha=0.0 backward-compat).
  - `sp20_phase1_4_wireup_test::alpha_and_hold_reward_emas_stay_at_
    zero_phase_2_3_2_placeholders` →
    `..._with_null_producers` (same NULL-tolerance pin).
  - +2 NEW tests in `sp20_aggregate_inputs_test`:
    * `alpha_mean_over_closed_envs_phase_2_contract` — 4 envs (3
      close, 1 doesn't), asserts close-gated mean (env 3's alpha=99
      ignored).
    * `alpha_zero_when_no_close_phase_2_contract` — no-close steps
      emit alpha=0.0 regardless of stale alpha_per_env content.

* `dqn-wire-up-audit.md` — Task 2.2 entry documents the deleted SP12 v3
  block, the per-env alpha plumbing, the retained device functions
  per `feedback_no_stubs`, and the deferred Task 2.4 cleanup of the
  orphaned min_hold_temperature_update_kernel producer chain.

Per `pearl_event_driven_reward_density_alignment`: per-bar SP18 D-leg
sites at experience_kernels.cu:3722 / :3833 are RETAINED pending
Phase 3.2 Component 2 replacement. Per `feedback_no_stubs`:
compute_sp18_hold_opportunity_cost / compute_asymmetric_capped_pnl /
compute_min_hold_penalty device functions are RETAINED (still called
by per-bar SP18 sites + sp12_reward_math_test_kernel test wrapper).

Per spec §4.1: alpha_ema centering (`R_used = alpha - alpha_ema`) is
load-bearing for cold-start learning at WR=46% with raw EV ≈ -0.06,
NOT just Q-target stability. Documented in the kernel comment block.

Verification:
  SQLX_OFFLINE=true cargo build -p ml                          # cubin compile
  SQLX_OFFLINE=true cargo check -p ml --tests --features cuda  # tests compile
  SQLX_OFFLINE=true cargo test -p ml --lib sp20                # 20/20 lib tests

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 00:28:52 +02:00
..