Repository navigation
Opt-in speedups for shared components: dense_full 1.99x on 8 GPUs - #26
Conversation
The training loop copied the token count from the host in every micro batch. A copy from pageable memory synchronizes, so the GPU went idle until the host had enqueued the next forward. Filling the value on the device keeps it identical and saves 4 ms per micro batch for every method. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fused kernels for the shell around the mixer, the loss and the data path, applied at run time so shared model files, proofs and fingerprints stay as they are. `exact` is bit-identical to the default path; `all` adds cuDNN SDPA and `merge`, which change rounding only. install() refuses a torch version or a patched function it was not reviewed against. dense_full on the real trainer, 8 GPUs: 1375 ms/update by default, 851 ms with `exact` (1.62x), 690 ms with `all` (1.99x). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
python -m nexttransformer.checks.speedups runs the GPU tests, alternating timing, bit identity across the architecture families, resume and the numerics of `all` on the real trainer, so a maintainer can check them after changing a patched function. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ction --speedups stays off by default. With --formal the trainer accepts only `exact` or a selection of its parts, which reproduce the default path bit for bit, and refuses `all`, `cudnn` and `merge`. A resumed segment must use the checkpoint's selection in both directions, the audit rejects formal segments outside `exact`, and the exported result records the selection as proof.speedups; records of runs without speedups are unchanged. The protocol overview states the rule. Co-authored-by: Cursor <cursoragent@cursor.com>
The fused readout leaves logits uncapped for the patched shared loss. A model class that overrides _token_loss, as a plugin may, received those uncapped logits and computed a different loss without an error. Such a class now keeps the original readout; a GPU test checks that its loss is unchanged. Co-authored-by: Cursor <cursoragent@cursor.com>
…wed sources Two checkouts at different commits record different software/git_commit, which `checks.speedups bits` reported as a difference. The fast tests now warn, without failing, when a patched function no longer has its reviewed source. Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
5943c54
into
Interactive-Active-AI:main
|
Merged in 5943c54. Thank you, @henry-y, and thanks to the ResearchBoost-Agent team: this speeds up every method that runs on the shared trainer. Before merging we added four maintainer commits on top of yours; #27 has the full review and our measurements. In short:
On our node of eight 80 GB Ampere-generation GPUs, We also added ResearchBoost-Agent to "Built on NextTransformer" in our README and documentation (#28). We would be glad to see NextTransformer among RBA's results too, for example a Faster-NextTransformer entry next to Faster-NanoChat on the project page. If you publish one, a line in its README saying it builds on NextTransformer keeps the listing in line with our community rule, and we will point the card to it. The kernels pin torch 2.9.1 and 13 shared functions; when either changes, |
ResearchBoost-Agent by AI4AI for Anything contributed the opt-in --speedups (Interactive-Active-AI#26). Its card sits next to NextTransformer-V in the README, its Chinese translation and the community page; the invitation for further projects now spans the row below. Co-authored-by: Cursor <cursoragent@cursor.com>
Optimized with ResearchBoost-Agent (RBA).
What this changes
This adds
--speedups, an opt-in flag that runs the components every architecture shares through fused kernels, and removes one host synchronization from the training loop. Ondense_fullwith the real trainer on our device:dense_full, ms/updatemain)--speedups exact--speedups allReference and optimized runs alternate; the three 8-GPU reference runs measured 1373.4, 1375.8 and 1374.9 ms. Each number is a median after warmup, over 30 updates on 8 GPUs and 14 on 4 GPUs, at micro batch 16.
exactis bit-identical to the default path on the deterministic path: parameters, optimizer state and the data cursor match.alladdscudnnandmerge. They compute the same function with other rounding, so the run is not comparable bit for bit. Over 300 updates its loss stays within what two default-path runs differ by.Without the flag, nothing changes except the host synchronization below, which keeps the same value.
nexttransformer/models/andnanochat/are untouched: the patches apply at run time, so published proofs and fingerprints stay valid.Why this is a draft: it adds code under
nexttransformer/runtime/and editsnexttransformer/train.py, both contract paths, and CONTRIBUTING asks for an issue first. We have not opened one yet and are happy to move the discussion there. We would like your view on whether this belongs in the repository, and on whetherexactcould be allowed in formal runs. Until then we suggest treating it as a research-mode option.Starting point
The work began by asking which optimizations of Faster-NanoChat transfer here. Its retained changes hook FP8 and Inductor-generated code, while NextTransformer is BF16 and eager, so its patches do not apply; its row-packing idea does, and that was the largest single gain. The rest came from profiling
dense_full, where GEMM took only 28% of GPU time, elementwise work and RMSNorm 45%, and attention 18%. Most of that overhead sits in the shell shared by all methods.What each part does
normF.rms_normattentionnorm(apply_rotary_emb(z))*1.2,v+gate*e, the input gradient of the projectionsmlpF.relu(h).square()residuala*x+b*x0readout15*tanh(x/15)and cross entropyembeddingnn.Embeddinggradientsdataweightsweight.to(bf16)addnormx+attn(...)with the next norm, the residual mix with the first normx0in placecudnn--deterministic-attentionmergeMeasured effect
First round, 8 GPUs, cumulative ms/update, 14 updates per step:
dataattentionnormmlpresidualreadoutembeddingcudnnSecond round, 1 GPU running 4 micro batches with
cudnnon, starting from the first round plus the one-pass cross entropy:weights,addnormx0gradient, gate gradient sum in the kernel, cross entropy tilingmerge, on one GPU, alternatingThe change on the default path
The training loop copied a constant from the host once per micro batch, and that copy drained the GPU queue:
The value is the same. It saves 4 ms per micro batch for every method, with or without
--speedups.How bit identity is kept
Fused kernels keep every rounding point of the eager sequence and reproduce ATen's reduction order in Triton: RMSNorm (128 threads, vectors of 4, shuffle-down tree,
(w0+w2)+(w1+w3)),log_softmaxat width 32768 (1024 threads, 8 rounds of 4 elements, two shuffle-down levels), the BF16 sum over 128 (32 lanes of 4 strided elements, then adjacent pairs), embedding gradients (stable sort, groups of 10 rows in FP32), and path gradients in the order autograd receives them.Traps worth knowing when maintaining this: Triton folds
.to(bf16).to(f32)away, so rounding goes through an inlinecvt.rn.bf16.f32; ATen does not contractx*rstd*statinto an FMA, hencelibdevice.mul_rn; libdevicetanhflushes subnormals, hence a guard for tiny inputs; and NSA'sbranch_gatealso readsx, so NSA stays off the fused projection path.installrefuses to start on another torch version, or if any of the 13 patched functions no longer has the reviewed source hash, so an upstream change cannot silently diverge. Every fast path falls back to the original code outside CUDA BF16 with the expected layout.Results per method
All 33 methods run with
all. Single GPU, micro batch 16 (2 forglaanddeltanet), which excludes the gains fromdataand the host synchronization, sodense_fullis below its trainer number.All methods
all, msourofixed_recursivedenseut_acthuginndeeploopdense_fullmha_no_veresidual_standardattnresmla_nopemoba_hybridgdn_hybrid_swamobaseergdn_hybriddeltanet_hybridqsaretnethcqsa_hybridkda_hybriddsamhcgdnkdansakda_no_veelu_normelu_qknormelu_lineargladeltanetThe gain follows the share of the shared shell and softmax attention.
glaanddeltanetuse thereferencebackend, a per-token Python loop these changes do not reach; repeated runs show about 1.00×, with roughly 10% noise between processes.Verification
Bit comparisons use
--deterministic-attention, because SDPA flash differs between two runs of the same code.exact, all 33 methods, 6 updates, reference run twicedense_full, 30 updatesgdn,moba_hybrid,deeploop,mhc,ouro,mla_nope,residual_standard,attnres, 6 updatesexactandalltests/test_speedups.pytanhon every BF16 value and whole-layer gradientspytest -n 16,checks.golden,protocol.fingerprint,checks.public,checks.changesNumerics of
all,dense_full, 4 GPUs, 300 updates, each configuration run twice:exact, first runexact, second runall, first runall, second runMean absolute loss difference over updates 200 to 300: 0.0054 between the two
exactruns, and 0.0053 and 0.0060 betweenexactandall.Reproduce
nexttransformer/checks/speedups.pyis the entry point for maintenance. Arguments it does not know go to the trainer.After changing a patched function, review its patch, update its hash in
REVIEWED, and runtestsandbits.We ran all five commands at reduced update counts,
resumeon 2 GPUs and the others on 4. The numbers in this description come from equivalent runs at full length.Limits
cudnnhas no effect.allwere compared ondense_fullonly.Tried and not kept
addmmfor the residual: cuBLAS copies C first, which cancels the gain.tanhandexpin the loss: no measurable change, so the exact version stays.Checklist
python -m nexttransformer.checks.golden --base origin/mainreports no difference, or this pull request explains why one is expected. Golden reports no difference; the issue is still open, see above.python -m pytest -q -n autopasses locally.AI assistance
🤖 Generated with Claude Code