Add entropy-gradient probing script for RL checkpoints#4
Open
Upcccccc wants to merge 1 commit into
Open
Conversation
Offline probe for answering: at which token positions do gradients live,
and do high-entropy ("forking") positions have special gradient stats?
For each HF checkpoint:
1. Sample trajectories from a math RL prompt subset.
2. Compute per-token response entropy; bucket positions by entropy quantile
(default 5 bins = 20-percentile buckets).
3. For selected positions per (bin x correctness) group, compute the gradient
of that token's log-prob w.r.t. targeted parameter rows on
Qwen2 k_proj / down_proj layers.
4. Summarize grad_norm, row_cosine (alignment with current weight row),
proj_energy_frac (fraction of grad energy in current-row direction),
and effective_rank of stacked gradient rows.
Caveat (called out in the module docstring): this is the per-token log-prob
gradient under teacher forcing, not the exact token-level contribution to
the current trainer's sample-aggregated DAPO objective.
Entry: python -m nanorl.scripts.probe_entropy_gradients \
--checkpoint-root <dir> [--steps 20,80,160,240] [--include-final] \
--output-dir <out>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
nanorl/scripts/probe_entropy_gradients.py— offline probe for which token positions drive gradient updates in RL checkpoints.What it does
For each HF checkpoint:
build_rl_dataset, soRL_DATASET_PATHenv var applies).--max-positions-per-grouppositions per (entropy_bin × correctness) group.∂(token_logp)/∂Ww.r.t. targeted rows ofself_attn.k_proj/mlp.down_projacross configurable layers.grad_norm,row_cosine,proj_energy_frac,effective_rank.Caveat (module docstring)
This probes per-token log-prob gradients under teacher forcing; it does not reconstruct the exact token-level contribution to the trainer's sample-aggregated DAPO/GRPO objective (trainer reduces response tokens to a sample-level masked-mean log-prob before loss).
Usage
Outputs:
summary.json,token_probe_records.jsonl,manifest.json.Fixes vs earlier local drafts
sample_trajectories: HFgenerate(num_return_sequences>1)pads shorter sequences withpad_token_id(=eos_token_idhere); those pad positions were polluting entropy/gradient stats. Now trim at first EOS in each response (keeping the EOS itself — its entropy is a real policy decision).--max-new-tokensdefault 512 → 2048 (training uses 8192; 512 cut off most math responses).Test plan
Does not modify
Trainer / losses / rollout / data loader untouched.