Files
foxhunt/crates/ml-alpha/examples
jgrusewski 779c03b9d7 feat(ml-alpha): Phase 1B-B — rollout collection loop + GAE behind FOXHUNT_USE_ROLLOUT flag
Spec: docs/superpowers/specs/2026-06-02-trainer-rollout-buffer-gae.md
Plan: docs/superpowers/plans/2026-06-02-trainer-rollout-buffer-gae-implementation.md §Phase 1B-B

Builds on commit 5cd2f8703 (1B-A foundation). Wires the RolloutBuffer
into the per-step training loop via a new RolloutCollection helper,
gated behind FOXHUNT_USE_ROLLOUT=1 env flag. Mode 0 (default unset) is
bit-equal to HEAD; Mode 1 (flag=1) snapshots per-step data into the
rollout buffer and invokes GAE every T_rollout steps.

This phase is a STRUCTURAL validation, not an empirical pnl test.
Phase 1B-C will replace the per-step training in Mode 1 with multi-
epoch PPO over the rollout buffer.

New components:

* crates/ml-alpha/cuda/rollout_pack.cu (42 LOC):
    Single-thread-per-batch deterministic f32 -> u8 packer for dones.
    Used because RolloutBuffer.dones_d is u8 (per spec for memory) but
    the trainer's dones_d is f32 (per existing pipeline).

* crates/ml-alpha/src/trainer/rollout_collection.rs (375 LOC):
    RolloutCollection helper with three methods:
      - new(ctx): loads rollout_pack cubin
      - snapshot_step(trainer, buf, t, b_size, t_max): DtoD scatter via
        cuMemcpy2DAsync_v2 for rewards/v_t/actions/log_pi/h_t at offset
        b*t_max+t in the [B × T] buffer; rollout_pack kernel for dones
      - copy_bootstrap_v(trainer, buf, b_size): DtoD V(s_T) from
        trainer.v_pred_tp1_d into buf.v_t_bootstrap_d at rollout end
    All transfers use mapped-pinned / DtoD only — no raw memcpy_dtoh
    on regular slices (`feedback_no_htod_htoh_only_mapped_pinned`).

* crates/ml-alpha/examples/alpha_rl_train.rs (+137 LOC at lines 59-66,
  410-466, 591-666):
    - FOXHUNT_USE_ROLLOUT env-flag parse at startup
    - Conditional RolloutBuffer + RolloutCollection allocation
    - Per-step snapshot_step() after step_with_lobsim_gpu
    - Every T_rollout steps: copy_bootstrap_v + compute_gae + stderr log
      `[rollout] step=N GAE complete (T=…, γ=…, λ=0.95): adv_mean=…
       adv_abs_mean=… returns_mean=…`

Adaptations from the dispatch (documented inline):

1. T_rollout fallback to 32 when slot 758 is unbootstrapped. Slot 758
   (RL_PPO_ROLLOUT_HORIZON_INDEX) was allocated in 1B-A but no kernel
   bootstraps it — `read_isv_host(758)` returns 0.0. Binary uses
   `.clamp(32, 1024)`, flooring to T=32 for the smoke. Phase 1B-C will
   add the trainer bootstrap entry (or a controller kernel) to make
   T_rollout=256 the production default.

2. Mode 1 still runs per-step training as a side effect; the rollout
   buffer is populated as a parallel snapshot. The "stop training in
   Mode 1" separation would require a ~1500 LOC refactor of
   step_with_lobsim_gpu (single-step/training-coupled per
   pearl_foxhunt_trainer_is_genuinely_single_step). The dispatch
   explicitly authorized this "skeleton with correct semantics"
   adaptation — the load-bearing 1B-B gate is "collection + GAE
   pipeline works structurally", which is verified. Phase 1B-C will
   replace the per-step training with multi-epoch PPO over the rollout
   buffer.

Validation (all gates PASS):
* cargo build --release --example alpha_rl_train -p ml-alpha: exit 0 (~60s)
* cargo test rollout_buffer_invariants --release: 5/5 PASS (no regression)
* Mode 0 (flag unset, default): ./scripts/determinism-check.sh --quick exit 0
  - DETERMINISTIC: all checksums.* leaves match across all 200 rows
  - pipeline output bit-equal to HEAD 5cd2f8703 baseline
* Mode 1 (FOXHUNT_USE_ROLLOUT=1): smoke completes in 2m 53s
  - 500 train + 100 eval steps, exit 0, completed_clean=true
  - 16 GAE windows logged (T=32 each)
  - adv_abs_mean range [4.4e-3, 1.92e-1] — always > 1e-3 sanity gate
  - eval_summary written: total_pnl_usd=-$101,487.70, n_trades=301, wr=0.346
* Cross-source consistency BOTH modes within $50 (Mode 0: $0.00 diff;
  Mode 1: $0.01 diff). Phase 0 contract preserved.
* Pre-commit hook PASS (0 memcpy_dtoh raw violations, 0 atomicAdd).

Next: Phase 1B-C will (a) bootstrap slot 758 to production T_rollout=256,
(b) replace Mode 1's per-step training with multi-epoch PPO over the
rollout buffer using the existing PPO surrogate + V regression kernels
operating on minibatches of [B × T] flattened to [B*T].

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-02 22:42:39 +02:00
..