NVIDIA-NeMo/RL #4776, commit 4a8997dba · all links pinned to head 96ec8a36
reuse_logits_storage, writes the grad into the logits' own memory, one chunk at a time. Only the fused packed loss turns it on.Three pages on this PR, one question each. This one: how the memory change works.
The policy loss needs the log-probability of each chosen token. ChunkedDistributedLogprob computes it in chunks of tokens to save memory, and saves the logits for backward. In backward, each chunk's grad is one_hot(target) − softmax(logits), scaled by the incoming grad. Before, every chunk was copied into a new zeros_like(logits). Now, when the flag is on, it is copied back into the logits. Each chunk is read before it is written, so the math does not change.
Tiny example: [1, 6, 4] = [sequences, tokens, vocab on this GPU], fp32 logits, chunk size 2, so 3 chunks. The picture is backward, halfway through chunk 1.
The change itself, at the pinned head:
@@ model_utils.py L307-L319 · ChunkedDistributedLogprob.backward - grad_input: torch.Tensor = torch.zeros_like(vocab_parallel_logits) + # Opt-in: write the grad into the fp32 logits' storage instead of a second + # [B, S, V/TP] tensor. Only safe when this op is the logits' sole consumer; + # each chunk is read before it is overwritten, and leaf tensors are left intact. + reuse_logits_storage = ( + ctx.reuse_logits_storage + and vocab_parallel_logits.dtype == torch.float32 + and not vocab_parallel_logits.is_leaf + ) + grad_input: torch.Tensor = ( + vocab_parallel_logits + if reuse_logits_storage + else torch.zeros_like(vocab_parallel_logits) + ) @@ loss_input.py L538-L539 · prepare_packed_loss_input + # Sole consumer of the packed logits, so the grad may overwrite them. + reuse_logits_storage=True,
Only fp32 logits qualify, because the grad is fp32. With bf16 logits the old path runs. No shipped config sets fp32_lm_head: true, so no recipe or nightly test reaches this code today.
After backward, the logits tensor holds the grad. Anything that reads it as logits later gets wrong numbers. If that reader saved the logits for its own backward, PyTorch raises "modified by an inplace operation". If it kept them some other way (a ctx attribute, .data), it reads the grad silently. The review checked every reader on the one path that turns the flag on:
| Reader of the packed logits | Affected? |
|---|---|
| This logprob op | Reads each chunk before overwriting it. |
| Second logprob call for the unfiltered KL term | No. It runs only with top-k/top-p, which turns chunking off, so the reuse never runs. But the "sole consumer" comment is wrong. |
| Packed draft loss (speculative decoding head) | No. It copies the logits with torch.roll during forward. |
| MTP loss, context-parallel gather | No. They use hidden states and logprobs, not these logits. |
A CPU check gave the same weight and input grads with the flag on and off, including the pre-packed view path and temperature scaling.
Where these land: RL only. loss_input.py, test_sequence_packing_fusion.py, and the PR description. Nothing below is implemented.
@@ loss_input.py L538-L539 · prepare_packed_loss_input (proposed) - # Sole consumer of the packed logits, so the grad may overwrite them. - reuse_logits_storage=True, + # The grad may overwrite these logits only on the chunked path. The + # unfiltered KL call below reads them again, but only with chunking off. + reuse_logits_storage=use_chunking,
use_chunking instead of True (above). Same behavior today, and the safety condition is now in the code.chunk_size (None, 3) to test_sequence_packing_fusion_vs_baseline. Today it passes no chunk size, so no test runs the reuse with real tensor- and context-parallel all-reduces.↪ C1 · review comment — model_utils.py:1110