Files
foxhunt/crates
jgrusewski e72885e8b6 gem(G12): finish predictive_coding — write backward + wire into training loop
Found by V7-gem audit: predictive_coding_loss kernel existed (forward only,
per-sample MSE on consecutive trunk activations), and compute_predictive_coding_loss
Rust wrapper existed, but no backward and no caller. Pure scaffolding.

Self-supervised temporal smoothness on the enriched trunk h_s2 IS in the
"better form" by the V7 methodology — it operates at the gradient level on
the network representation, not as a reward shaping term. Worth wiring up.

Three changes:

1) New CUDA kernel `predictive_coding_backward` (experience_kernels.cu)

   Loss:    L = sum_{i=0..N-2} (lambda/SH2) * sum_j (h[i,j] - h[i+1,j])^2
   Grad:    dL/dh[i,j] = (2*lambda/SH2) * sum-of-affected-loss-terms

   Each h[i,j] appears in TWO loss terms (interior i): "current" of term i
   and "next" of term i-1. Boundaries (i=0, i=N-1) have one. Closed-form
   gradient written directly with no atomicAdd — one thread per (sample,
   feature) cell, each writes to a unique slot, plain += accumulates into
   bw_d_h_s2. Bit-deterministic.

2) Rust glue (gpu_dqn_trainer.rs)

   - Load `predictive_coding_backward` kernel via existing exp_module_for_mag
   - New field on DQNTrainer (predictive_coding_backward_kernel)
   - Extend `compute_predictive_coding_loss` to also launch backward as
     step 3 (after forward + reduce). Now the function name accurately
     describes what it does — both compute and accumulate gradient.

3) Integration (fused_training.rs::submit_aux_ops)

   Inserted the call right after `launch_recursive_confidence_backward`,
   before regime_scale_td_errors. Both spots accumulate into bw_d_h_s2 via
   plain +=, so ordering is irrelevant for correctness — what matters is
   that this runs INSIDE the aux_child CUDA-graph capture window AND
   BEFORE the trunk W_s2 → W_s1 backward GEMMs read bw_d_h_s2.

Why not also G6 (branch_independence) and G10 (temporal_consistency)?
Per V7-gem methodology — check for redundancy first:
  - G10 wants Lipschitz on Q for similar states. Spectral normalization
    (already wired on all 12 weight tensors) achieves *global* Lipschitz.
    G10 adds *local-pair* Lipschitz on top. Possibly redundant — needs
    a measurement before wiring blindly.
  - G6 wants the 4 advantage branches to stay diverse. NoisyNets already
    adds different parameter noise per layer → naturally diverse heads.
    Ensemble KL gradient pushes ensemble heads apart (different mechanism
    but similar intent). Possibly redundant for the 4-branch case.

G12 is unambiguously useful — trunk smoothness is a genuine gem with no
existing equivalent in the codebase. G6/G10 land separately if measurement
shows they add real value beyond spectral_norm + NoisyNets + ensemble KL.

Files touched:
  crates/ml/src/cuda_pipeline/experience_kernels.cu      (+45)
  crates/ml/src/cuda_pipeline/gpu_dqn_trainer.rs         (+33 / -3)
  crates/ml/src/trainers/dqn/fused_training.rs           (+9)

Verified: cargo check passes. Smoke test running to verify training is
stable with G12 active (lambda_pred=0.1 — small enough that any regression
is from a real bug, not dominance over the C51/IQN gradient).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 09:23:28 +02:00
..