Repository navigation
perf(utils): reduce selective_log_softmax allocations and reuse Ascend CE for FP32 logp - #386
Merged
kevssim merged 4 commits intoNov 17, 2026
Conversation
- Verify the missing torch_npu cross-entropy API returns None so the generic autograd path remains available - Verify an incompatible vendor signature falls back instead of failing at the end of the NPU path - Keep the regressions CPU-only and independent of an installed torch_npu runtime
kevssim
reviewed
Nov 16, 2026
- Exercise the real torch_npu FP32 cross-entropy fast path on A2 with V=151936 - Compare both selected log-probabilities and logits gradients against log_softmax + gather - Keep the test skipped on CPU-only environments
- Fail the A2 contract test if the vendor helper falls back to None - Preserve output and gradient checks against log_softmax + gather
kevssim
approved these changes
Nov 17, 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.
perf(utils): reduce selective_log_softmax allocations and reuse Ascend CE for FP32 logp
Background and motivation
RL, DPO, and GRPO training need per-token log-probabilities. GRPO with an entropy bonus also needs per-token entropy. Twinkle's generic
selective_log_softmaxcurrently creates one autograd node per vocabulary chunk:[tokens, vocab]gradient throughSliceBackward.The FP32 logp-only path also uses per-chunk
logsumexp + gatherwith a custom backward. On A2, this path takes2.91 msfor256x151936, which is slower than the2.20 msdirect materialized PyTorch path. This PR addresses both issues:torch_npu.npu_cross_entropy_lossfor FP32 logp-only instead of adding an equivalent kernel to Twinkle.Relationship to upstream capabilities
torch_npu.npu_cross_entropy_lossfused_linear_ppoThis PR adds no public parameters, dependencies, or opt-in switches. All existing
selective_log_softmaxcall sites benefit automatically:NPU + FP32 + logp-onlyuses the vendor fused cross-entropy op.torch_npuis unavailable, the API is missing, or the installed signature is incompatible, the implementation falls back to Twinkle's custom autograd path.Relationship to PR #385
PR #385's
_fused_rl_logps()callsfused_linear_selective_log_softmaxdirectly and bypasses the genericselective_log_softmaxchanged here. The two changes can coexist, but their performance gains do not compose. Combining them would enlarge the review surface and obscure performance attribution, so they remain independent.Correctness
The required tolerances are not relaxed:
rtol=1e-4, atol=1e-5rtol=1.6e-2, atol=1e-2Coverage includes:
gradcheck;log_softmax + gather;torch_npu.npu_cross_entropy_losscontract withV=151936; the test fails if the vendor helper falls back toNone;torch_npuAPI is missing or its signature is incompatible.Verification results:
Performance
Primary baseline: pre-change Twinkle
A2 910B4, 32 GB, torch_npu 2.10.0,
V=151936; single-op forward plus input gradient:These numbers are the primary merge-benefit evidence for this PR.
Regression boundary: direct materialized native PyTorch
Native
log_softmax + gatheris a single-operator regression boundary, not an end-to-end model baseline. It materializes the complete log-softmax once, which is faster but uses more peak memory.Key result: FP32 logp-only previously measured
0.75xversus direct PyTorch. It now measures1.51x-1.96xwhile retaining lower peak memory.End-to-end training
Qwen3-0.6B, BF16,
B=2, S=512, entropy coefficient 0.01, fixed inputs, SGD, 50 steps:6.94 GBto5.39 GB(-22.3%).ZerosLikeoperations attributed to the selective path in msprof:Honest limitations
torch_npu.npu_cross_entropy_lossshould be covered by NPU CI as CANN and torch_npu evolve. Capability detection and fallback are included in this PR.Evidence boundaries
torch.testing.assert_closechecks run before timing. A numerical failure prevents performance results from being produced.citestandcitest-npustill require maintainer approval.