Repository navigation
fix(loss): keep the logits intact in the chunked CE backward - #334
Merged
tastelikefeet merged 1 commit intoOct 26, 2026
Merged
Conversation
ChunkedCrossEntropyLoss's backward wrote the gradient into `logits.data` and returned it, so after backward the model output kept in train_status.outputs held d(loss)/d(logits) instead of the logits. The Accuracy train metric (enabled by default) takes argmax over those values at the start of the next step; at the label the gradient is (p_y - 1) * scale < 0, so the reported token accuracy collapses to ~0. The logits returned from forward_backward were corrupted the same way. Write the gradient to a fresh buffer, as the class docstring already says. Only one chunk's fp32 log_softmax is live at a time, so the memory saving of chunking is kept. Co-Authored-By: Claude Opus 5.5 <[email protected]>
tastelikefeet
approved these changes
Oct 26, 2026
tastelikefeet
added a commit
that referenced
this pull request
Nov 28, 2026
main carried 17 commits (#311..#291) over merge-base 96dab68. v5 had already ported everything up to #312 (a4ddc3b) and moved server/client to twinkle_cs, so: - server/client (src/twinkle/server, twinkle_cs/*, tests/server, tests/twinkle_client): kept the v5 tree; the legacy src/twinkle/server path and the old ci_container_test.sh stay removed on v5. - loss: #291 detached DFT weights, #335 Liger CE contract (delegates micro_batch_scale / batch_normalizer to CrossEntropyLoss), #334 chunked CE comments, #336 InfoNCE tests. - model: #343 Torch.empty_cache (auto-merged), #385 fused_lm_rl ported onto the v5 task registry (processor/tasks.py + model/tasks.py FusedRL + transformers postprocess), #386 NPU fused CE fast path in selective_log_softmax. - twinkle_agentic: #345 check kinds (hidden_python / sql_query / answer_one_of / answer_number / web_receipt / lean) adapted to the episode-scoped Env interface; AgenticChallenger gains the _verify_check / _proposal_reward hooks. Known: cookbook/rsi and tests/cookbook/rsi (#345) are written against main's stateless Env / AgenticChallenger API and do not run on v5 yet.
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.
PR type
PR information
Problem. The backward of
ChunkedCrossEntropyLosswrites the gradient into the input logits and returns that tensor:logitsis a view ofoutputs['logits'], the model output thatTransformersModel.forwardstores intrain_status.outputsand returns to the caller. Afterbackward(), that tensor holdsd(loss)/d(logits)instead of the logits. The write goes through.data, so autograd's version counter cannot catch it.Visible effect.
Accuracyis one of the default train metrics. It is accumulated byaccumulate_metrics(True)at the start of the next forward, after the previous backward, and it takesargmaxoveroutputs['logits']. At the label the gradient is(p_y - 1) * scale < 0, while every other token getsp_j * scale > 0. The argmax therefore never hits the label, and the reported token accuracy collapses to about 0 for any run usingchunked_cross_entropy. The logits returned byforward_backwardare corrupted the same way.Reproduction (CPU), with logits that predict every label:
Fix. Write the gradient into a fresh buffer (
torch.empty_like(logits)), which is what the class docstring already describes ("writes per-token gradients into a freshly allocatedgrad_logitstensor (the inputlogitsis never mutated)").Memory: chunking still only keeps one chunk's fp32
log_softmaxalive, which is where its saving overCrossEntropyLosscomes from. The extra[N, V]buffer is the same size as the gradient thatCrossEntropyLossitself produces.Experiment results
New test
test_chunked_backward_keeps_logits, parametrized overmean,sumanddft, checks two things:CrossEntropyLoss;All 3 cases fail on
main.tests/loss/test_ce_mse.pypasses with the fix (21 tests, CPU).🤖 Generated with Claude Code