Repository navigation
perf: avoid D2H sync in _Split2DJaggedFunction by precomputing split lengths - #318
Conversation
Greptile SummaryThis PR successfully eliminates device-to-host synchronizations from the HSTU block's forward critical path by precomputing split lengths in the preprocessor and threading them through Core changes:
Remaining issue: The Confidence Score: 4/5
|
e352b24 to
27ec14c
Compare
…lengths _Split2DJaggedFunction.forward() calls offsets[-1].item() which triggers a device-to-host sync, blocking subsequent kernel launches on the critical path. Fix by: 1. Adding optional seq_len_a/seq_len_b params to triton_split_2D_jagged and _Split2DJaggedFunction to accept precomputed lengths. 2. Adding total_candidates_seq_len field to HSTUBatch (computed in dataset/collate on CPU) and JaggedData. 3. Preprocess doubles it when action interleave is applied (training). 4. Postprocess derives seq_len_a = values.shape[0] - total_candidates_seq_len and passes both to triton_split_2D_jagged, avoiding .item() calls. Made-with: Cursor
27ec14c to
efb701a
Compare
…ribution The balanced shuffler redistributes samples across ranks, but total_candidates_seq_len (a plain int) was copied verbatim by _apply_to_tensors_or_kjt, becoming stale and causing negative tensor dimensions in triton_split_2D_jagged. Add a _on_samples_redistributed hook to BaseBatch (no-op by default) and override it in HSTUBatch to recompute the aggregate from the correctly redistributed num_candidates tensor. Made-with: Cursor
dmp_batch_to_tp AllGathers num_candidates across TP ranks but total_candidates_seq_len was copied verbatim, causing the jagged split to produce half-sized logits while labels were fully gathered. Multiply by tp_size to match (zero-cost, no D2H sync). Made-with: Cursor
Made-with: Cursor
Made-with: Cursor
a3a59f0 to
0219b1e
Compare
- Enforce that total_candidates_seq_len and num_candidates are both None or both non-None (ranking only). Added validation in is_batch_valid. - Removed contextual-only branch from compute_split_lengths_for_jagged; retrieval/contextual paths fall back to D2H-sync in _Split2DJaggedFunction. - Simplified dmp_to_tp.py guard based on the new invariant. - Added comment in batch_shuffler.py documenting the acceptable D2H sync in _on_samples_redistributed. Made-with: Cursor
…cess_embeddings Compute total_candidates_seq_len on-the-fly in hstu_preprocess_embeddings from num_candidates (ranking) or sequence_embeddings_lengths minus contextual (retrieval), instead of precomputing in HSTUBatch. This eliminates stale values after distributed operations (TP gather, balanced shuffler) and removes compute_split_lengths_for_jagged, _on_samples_redistributed hook, and dmp_to_tp fixups. Made-with: Cursor
Made-with: Cursor
Made-with: Cursor
Made-with: Cursor
Made-with: Cursor
…e triton_jagged.py (#389) * perf(hstu): restore .item() in eager preprocessor to avoid D2H sync in postprocessor PR #318 removed the D2H sync from HSTUPostprocessing by materializing total_candidates_seq_len as a Python int in hstu_preprocess_embeddings. PR #327 (torch.export support) and #340 reverted that to a 0-d tensor so the dynamic shape would survive tracing. In eager mode the tensor was then forwarded to torch.empty((seq_len_a, D), ...) inside _Split2DJaggedFunction.forward, where PyTorch implicitly calls .item() to convert the shape arg — relocating the same sync to the end of the forward pass, where it now blocks loss/backward. Fix: branch on torch.compiler.is_compiling() in the preprocessor: - eager: int(total_candidates_seq_len.item()) — sync happens early, hidden by subsequent HSTU layer compute (PR #318 behaviour). - tracing: keep the 0-d tensor so torch.export / torch.compile can capture the dynamic shape (PR #327 behaviour). Verified on ipp1-1621 (computelab-job-2139432): - pytest examples/hstu/test/test_hstu_preprocess.py: 8/8 passed - sanity check: jd.total_candidates_seq_len is `int` in eager mode Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * refactor(hstu): drop duplicate triton_jagged.py, route to commons copy examples/hstu/ops/triton_ops/triton_jagged.py was a stale duplicate of examples/commons/ops/triton_ops/triton_jagged.py. The commons copy is the post-torch.export version (PR #327) that wraps kernel launches in torch.library.wrap_triton — required for tracing — while the hstu copy was the pre-export snapshot. - delete examples/hstu/ops/triton_ops/triton_jagged.py - update test_jagged_tensor.py (3 sites) and test_position_encoder.py (1 site) to import from commons.ops.triton_ops.triton_jagged Production code (hstu_processor.py, retrieval_gr.py) was already on the commons import; only tests still hit the local copy. Verified on ipp1-1621 (computelab-job-2139432): - pytest examples/hstu/test/test_jagged_tensor.py: 97/97 passed - pytest examples/hstu/test/test_position_encoder.py: 3/3 passed Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Summary
_Split2DJaggedFunction.forward()callsoffsets[-1].item()which triggers a device-to-host sync, blocking subsequent kernel launches on the forward critical path.total_candidates_seq_lenin dataset/collate (CPU side) and storing it inHSTUBatch→JaggedData, so the forward path can derive split lengths without any D2H sync.seq_len_a/seq_len_bparameters totriton_split_2D_jaggedand_Split2DJaggedFunction; when not provided, falls back to original.item()behavior.Changes
HSTUBatch(commons/datasets/hstu_batch.py): Newtotal_candidates_seq_lenfield +compute_split_lengths_for_jagged()helper.hstu_sequence_dataset.py,inference_dataset.py,random_inference_dataset.py): Call helper when building batch.JaggedData(hstu/modules/jagged_data.py): Newtotal_candidates_seq_lenfield.hstu_processor.py): Reads from batch, doubles when action interleave applies.hstu_processor.py): Derivesseq_len_a = values.shape[0] - total_candidates_seq_lenand passes totriton_split_2D_jagged.triton_jagged.py(bothhstu/opsandcommons/ops):_Split2DJaggedFunctionandtriton_split_2D_jaggedaccept optional precomputed lengths.Test plan
examples/tests)_Split2DJaggedFunctionCI