Files
foxhunt/crates/ml-alpha/tests
jgrusewski 53aeef099b feat(rl): ISV-driven PPO importance-ratio clamp + log-ratio diagnostic
pt67l confirmed reward-scale + V-target clamp eliminate V regression
spikes — but exposed a residual: |l_pi| max=586 with mean 0.22. Root
cause: PPO's clip(r, 1-ε, 1+ε) bounds the loss only when surr2 is
the active min. The unclipped branch IS active when A<0,r>1+ε
(surr1=A·r is then more negative than surr2=A·(1+ε), so min selects
surr1) and when A>0,r<1-ε. In the first case `r` can blow up: we've
seen r reach 1e10 from policy drift over a multi-step rollout
producing l_pi=O(1e10) spikes that contaminate the loss-balance
controller and the LR controller's plateau detection.

## Fix: ISV-driven ratio clamp

Per `feedback_isv_for_adaptive_bounds` and
`pearl_controller_anchors_isv_driven`: the clamp ceiling lives in
ISV[RL_PPO_RATIO_CLAMP_MAX_INDEX = 440], not as a hardcoded #define.

New controller `rl_ppo_ratio_clamp_controller.cu`:
  * Anchors on the (already KL-adaptive) PPO clip ε at ISV[402]
  * target = (1 + ε) × PPO_CLAMP_MARGIN  (MARGIN = 10.0)
  * Wiener-α blend with floor 0.4 per
    pearl_wiener_alpha_floor_for_nonstationary (ε is non-stationary)
  * Permanent floor 2.0 / ceiling 1000 per
    pearl_blend_formulas_must_have_permanent_floor
  * Bootstrap 10.0, replace-directly on first non-bootstrap ε
    observation per pearl_first_observation_bootstrap

When ε is small (rl_ppo_clip_controller seeing low KL → tight clip
band), the ratio clamp tightens — outliers should be rare anomalies.
When ε widens (large KL → wide clip band), the clamp widens
proportionally — outliers are expected so we permit more
magnitude before bounding.

## Wiring

ppo_clipped_surrogate_fwd and _bwd both read
isv[RL_PPO_RATIO_CLAMP_MAX_INDEX] and clamp ratio to
[1/ratio_max, ratio_max] before forming surr1/surr2. The clamp is
forward-only in effect (bwd gates pg_grad inside [1-ε, 1+ε] anyway
so gradients were already bounded), but bounding the FORWARD ratio
keeps l_pi sane for the controllers downstream.

The new controller is wired into both:
  * `with_controllers_bootstrapped` — bootstrap launch alongside
    the other 7 R1 controllers
  * `launch_rl_controllers_per_step` — per-step refresh alongside
    the other 7 R5 controllers

## Diagnostic: per-step max |log_ratio|

New kernel `ppo_log_ratio_abs_max_b.cu` (same tree-reduce shape as
rl_kl_approx_b) writes per-batch max(|log π_new − log π_old|) to
ISV[RL_PPO_LOG_RATIO_ABS_MAX_INDEX = 441]. Launched right after
rl_kl_approx_b (uses the same log_pi_old_d + pi_log_prob_d inputs).

Surfaces in diag JSONL as:
  "ppo": {
    "ratio_clamp_max":   isv[440],   # adaptive ceiling
    "log_ratio_abs_max": isv[441]    # per-step observed max
  }

The clamp fires when log_ratio_abs_max > ln(ratio_clamp_max).
For ratio_clamp_max = 10, ln = 2.30. Healthy training has
log_ratio_abs_max well below this most steps; outliers touch or
exceed it on rare excursions which the clamp bounds before they
pollute l_pi.

## Slot allocation

RL_PPO_RATIO_CLAMP_MAX_INDEX     = 440  (controller output)
RL_PPO_LOG_RATIO_ABS_MAX_INDEX   = 441  (per-step diag)
RL_SLOTS_END                     = 442  (was 440)

## Test updates

G1 (isv_bootstrap) + G3 (r5_controllers) blanket-assert ISV[417..END]
== 0.0 to catch slot-wiring bugs. Slot 440 is now a controller
OUTPUT bootstrapped to 10.0, so both tests skip it in the loop and
assert == 10.0 separately.

## Verified gates (local sm_86)

  G1 isv_bootstrap    (with new slot-440 assertion)
  G3 controllers     
  G4 target_update   
  G6 r7d_per_wiring  
  integrated_smoke   

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-23 20:55:00 +02:00
..