Files
foxhunt/crates
jgrusewski 27f8e332da plan(sp18 v2): Phase 0 Task 0.2 — B-leg V_SHARE trajectory + TD-error magnitude diagnostic
Adds the SP18 v2 Phase 0 B-leg observability scaffold per the plan's
Task 0.2 spec:

- New kernel `td_error_mag_ema_kernel.cu` (single-block × 256 threads):
  reads `td_errors_buf [B]` (already populated by C51/MSE loss for PER
  priority recomputation, post-train-step), block tree-reduces
  `mean(|td_errors[b]|)` (no atomicAdd), and EMA-blends into
  `ISV[TD_ERROR_MAG_EMA_INDEX=493]` via Pearl-A first-observation
  bootstrap (sentinel 0.0 → REPLACE) + fixed α=`WELFORD_ALPHA_MIN=0.4`
  per `pearl_wiener_alpha_floor_for_nonstationary`. The TDB_* Welford
  accumulators in slots [498..504) are RESERVED for the Phase 4
  q_next_target Wiener-α chain — not used in Phase 0.

- Cubin manifest entry in `crates/ml/build.rs` + `TD_ERROR_MAG_EMA_
  CUBIN` re-export in `gpu_dqn_trainer.rs`.

- `sp18_td_error_mag_ema_kernel` field on `GpuDqnTrainer` + cubin load
  on the trainer's stream + `launch_sp18_td_error_mag_ema_update()`
  cold-path launcher + `read_sp18_td_error_mag_ema()` convenience
  wrapper.

- New `sp18_v_share_history: [[f32; 4]; 5]` field on `DQNTrainer` —
  fixed-size ring buffer of the last 5 epochs of per-branch V_SHARE
  EMA readings (slots [478..482) per SP17 Phase 3.2). Initialised to
  `[[NaN; 4]; 5]`; epochs 0–3 emit `nan` as the slope and skip the
  ISV write; epoch 4 onward computes `(EMA[now] - EMA[now-4]) / 4`
  per branch and writes the dir-branch slope to
  `ISV[V_SHARE_TREND_DIAG_INDEX=496]`.

- Two new HEALTH_DIAG lines in `training_loop.rs` at the per-epoch
  boundary (right after the SP18 reward_decomp line):

    HEALTH_DIAG[N]: v_share_traj [dir_slope=X mag_slope=Y ord_slope=Z urg_slope=W]
    HEALTH_DIAG[N]: td_error_pre [magnitude_ema=X]

  V_SHARE slope is host-side computation against the ring buffer
  (`(now - now_m4) / 4` per branch). TD-error magnitude is post-blend
  ISV slot 493 read (producer fires inside `read_sp18_td_error_mag_
  ema()`). Pre-fix baseline for the B-DD9 ratio gate
  (`avg(|TD-error|) ratio post-fix / pre-fix ∈ [0.5, 5.0]`).

- New GPU oracle tests in `crates/ml/tests/sp18_hold_reward_oracle_
  tests.rs`:
  * `td_error_mag_ema_pearl_a_bootstrap` — synthetic td_errors with
    closed-form mean(|td|)=1.125; pre-populate slot at sentinel;
    assert post-launch slot equals the mean (Pearl-A direct-replace).
  * `td_error_mag_ema_blend_post_bootstrap` — synthetic td_errors
    with mean(|td|)=0.5; pre-populate slot at non-sentinel 1.0;
    assert blend equals `(1 - 0.4) × 1.0 + 0.4 × 0.5 = 0.8`.

Pure observability — no production-path consumer in this commit. No
reward changes, no Bellman target changes, no kernel modifications to
the action-selection or training paths. Per `feedback_no_partial_
refactor` the kernel + cubin manifest + buffer + launcher + ring
buffer + HEALTH_DIAG emit + GPU oracle tests all land atomically.

Verification:
  SQLX_OFFLINE=true CUDA_COMPUTE_CAP=86 cargo check --workspace clean.
  All 4 GPU oracle tests pass on RTX 3050 Ti (2.09s):
    reward_decomp_per_action_gpu_oracle, reward_decomp_empty_bin,
    td_error_mag_ema_pearl_a_bootstrap, td_error_mag_ema_blend_post_bootstrap.
  Existing slot lock + state_reset_registry tests still pass.

Plan: docs/superpowers/plans/2026-05-08-sp18-reward-shape-hold-attractor.md
      § Phase 0 Task 0.2.
Audit: docs/dqn-wire-up-audit.md § "SP18 v2 Phase 0 Task 0.2".

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