Files
foxhunt/crates/ml/tests
jgrusewski 5ded1cb4b9 feat(sp20): Phase 1.3 sp20_controllers_compute kernel
Component 5 / Kernel 2 of the SP20 fused-producer chain — the
deterministic derivation step. Reads the EMAs Phase 1.2's
sp20_emas_compute wrote (4 ISV + 4 internal scratch slots) and
derives 6 controller outputs into ISV slots:

  - LOSS_CAP (510)            = -1 - clamp((wr-0.50)/0.05, 0, 1)
  - HOLD_COST_SCALE (513)     two-sided ramp around TARGET_HOLD_PCT ±0.05
  - TARGET_HOLD_PCT (514)     = clamp(0.8 - aux_p50_ema*1.5, 0.1, 0.8)
  - N_STEP (517)              = clamp(round(trade_dur_ema), 1, 30) (f32)
  - AUX_CONF_THRESHOLD (518)  = clamp(aux_dir_acc_ema-0.50, 0.01, 0.20)
  - AUX_GATE_TEMP (519)       = max(aux_conf_std_ema, 0.01)

TARGET_HOLD_PCT computed before HOLD_COST_SCALE because the
HOLD_COST_SCALE controller reads tgt; implemented as a local f32
written to ISV then re-used for the controller (no redundant ISV
read-back). HOLD_COST_SCALE is the only output that READS its own
previous ISV value (in-place update); deadband path writes the
unchanged previous value verbatim per feedback_no_stubs.

Single-block, single-thread kernel — captureable in the per-step
CUDA Graph per pearl_no_host_branches_in_captured_graph. ISV +
internal pointers are mapped-pinned device pointers (existing
buffers from Phase 1.2); kernel emits __threadfence_system() for
PCIe-visible coherence.

Pearls + invariants honoured:
  - pearl_controller_anchors_isv_driven: every output's anchor /
    target / cap derives from EMAs; only spec-frozen ramp / clamp
    parameters are compile-time constants.
  - feedback_isv_for_adaptive_bounds: these 6 outputs ARE the
    adaptive bounds.
  - feedback_no_atomicadd, feedback_no_cpu_compute_strict,
    feedback_no_htod_htoh_only_mapped_pinned, feedback_no_stubs.
  - feedback_no_partial_refactor: kernel + launcher + tests + build
    entry land atomically; production wire-up lands in Phase 1.4.
  - pearl_tests_must_prove_not_lock_observations: 7 GPU oracle
    tests assert clamp boundaries, formula correctness, and
    bidirectional ramp behavior — NOT specific observed values.

Tests (all #[ignore = "requires GPU"], pass on RTX 3050 Ti sm_86):
  1. loss_cap_ramp_boundaries — 4 wr_ema sweeps incl. clamps.
  2. n_step_bounds — round + clamp [1, 30].
  3. aux_conf_threshold_bounds — clamp [0.01, 0.20].
  4. aux_gate_temp_floor — max(std, 0.01).
  5. target_hold_pct_inverse_relation — 0.8 − p50*1.5 with clamps.
  6. hold_cost_scale_two_sided_ramp — up/down/deadband + clamps.
  7. all_six_outputs_written_in_one_launch — guards missed writes.

Plus 3 launcher unit tests (slot range, slot uniqueness, ISV input
slot range).

Verification:
  SQLX_OFFLINE=true CUDA_COMPUTE_CAP=86 cargo check -p ml --features cuda
  SQLX_OFFLINE=true CUDA_COMPUTE_CAP=86 cargo test -p ml \
    --test sp20_controllers_compute_test --features cuda \
    -- --ignored --nocapture
  → 7/7 GPU oracle tests pass; 3/3 launcher unit tests pass.

Phase 1.4 will land the production caller atomically alongside
Kernel 1 + Kernel 3 launches on the same stream in order
Stats → EMAs → Controllers per the SP20 design.
2026-05-09 19:09:06 +02:00
..