Files
foxhunt/docs
jgrusewski 64bbbe4181 fix(sp20): WR_EMA pinning bug — wire segment-level is_win_per_env producer + kernel-body read
Closes the fix gap left by the failing-test commit. The aggregation
kernel body now reads `is_win_per_env[env]` (when non-NULL) in the
`tc != 0` branch in place of the broken per-bar `step_ret > 0`
predicate; the producer site at `experience_kernels.cu::segment_
complete` writes the segment-level `(segment_return > 0.0f) ? 1 : 0`
flag at the same location as `alpha_per_env[i] = alpha`.

Atomic changes:
- sp20_aggregate_inputs_kernel.cu: when is_win_per_env != NULL, read
  the per-env i32 flag for wins_count; legacy `sr > 0` predicate
  preserved as NULL-tolerant fallback for oracle-test scaffolds.
- experience_kernels.cu: new `int* is_win_per_env` kernel arg;
  segment_complete branch writes `(segment_return > 0.0f) ? 1 : 0`
  at the same site as `alpha_per_env[i] = alpha`. Race-free per-i
  write (mirrors the alpha producer pattern). NULL-tolerant.
- gpu_experience_collector.rs: `is_win_per_env: CudaSlice<i32>`
  `[alloc_episodes]` field, alloc_zeros at construction, kernel-arg
  pass at experience_env_step launch site, raw_ptr() pass at
  sp20_aggregate_inputs launch site.
- state_reset_registry.rs: FoldReset entry + assertion test
  (sp20_is_win_per_env_registered_fold_reset). The
  every_fold_and_soft_reset_entry_has_dispatch_arm test passing
  confirms the dispatch arm is present.
- training_loop.rs: `"is_win_per_env" => memset_zeros` arm.
- docs/dqn-wire-up-audit.md: 2026-05-10 fix-commit entry.

Bug pattern for the memory pearl: per-bar mark-to-market step_return
is NOT the trade's segment-level win/loss outcome at close bars
because tx_cost (deducted via `*cash -= cost` in
trade_physics.cuh::execute_trade) plus the per-bar Δprice tick
swamp out segment-accumulated unrealized gains. The segment-level
realized P&L (`(realized_pnl + unrealized_at_exit -
trade_start_pnl) / prev_equity`) is the correct outcome scalar;
the producer in segment_complete already had it (used by
sp20_compute_event_reward's sign check for alpha), the aggregation
kernel just wasn't reading the right thing. Bug latent since SP20
Phase 1.4 / 2.2 landed on 2026-05-09; caught 2026-05-10 by
HEALTH_DIAG observation in a multi-seed L40S smoke (alpha_ema
populated correctly while wr_ema pinned at 0.0000 across 17 epochs).

Verification:
- SQLX_OFFLINE=true cargo check -p ml --tests passes
- 21 SP20 lib tests pass; 9 state_reset_registry tests pass
- The new GPU oracle test
  wr_ema_uses_segment_pnl_via_is_win_per_env_predicate passes
  after this commit (would FAIL on prior commit's kernel-body)
- The NULL-tolerance pin
  null_is_win_per_env_falls_back_to_legacy_step_ret_predicate
  preserves the pre-fix oracle-test contract

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