Repository navigation
perf(hstu): restore eager-mode .item() in preprocessor; drop duplicate triton_jagged.py - #389
Conversation
…n postprocessor PR NVIDIA#318 removed the D2H sync from HSTUPostprocessing by materializing total_candidates_seq_len as a Python int in hstu_preprocess_embeddings. PR NVIDIA#327 (torch.export support) and NVIDIA#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 NVIDIA#318 behaviour). - tracing: keep the 0-d tensor so torch.export / torch.compile can capture the dynamic shape (PR NVIDIA#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]>
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 NVIDIA#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]>
Greptile SummaryThis PR restores an early D2H sync for
Confidence Score: 4/5Safe to merge; the eager-mode change is well-scoped and the dedup is mechanical. The perf fix correctly branches on The Important Files Changed
Sequence DiagramsequenceDiagram
participant P as hstu_preprocess_embeddings
participant JD as JaggedData
participant SP as _Split2DJaggedFunction.forward
Note over P: Compute total_candidates_seq_len (tensor)
alt Eager mode (not compiling)
P->>P: "int(total_candidates_seq_len.item())<br/>D2H sync happens HERE (early, maskable)"
P->>JD: "total_candidates_seq_len = Python int"
JD->>SP: "seq_len_a = int passed directly"
Note over SP: torch.empty((seq_len_a, D)) — no D2H sync
else Compile / export mode
P->>JD: "total_candidates_seq_len = 0-d Tensor"
JD->>SP: "seq_len_a = Tensor (symbolic shape preserved)"
Note over SP: Graph captures dynamic shape correctly
end
|
nsys runtime verification (computelab-sc-2147773, a4u8g-0043)Minimal repro: Baseline (origin/main, commit 1782d6d —
→ 60 sync events / 20 iters = 3 syncs per iter, all inside postprocessor. After fix (this branch, commit ac3d5d6):
→ 20 sync events / 20 iters = 1 sync per iter, all inside preprocessor. Postprocessor is sync-free, matching PR #318 design intent. Classification done by |
|
/build |
|
❌ Pipeline #51169052 -- failed
Result: 9/14 jobs passed |
Summary
int(total_candidates_seq_len.item())inhstu_preprocess_embeddingsfor the eager path. PR perf: avoid D2H sync in _Split2DJaggedFunction by precomputing split lengths #318 originally moved this D2H sync into the preprocessor so that subsequent HSTU layer compute could mask the latency; PR Add Torch export for HSTU model #327/feat(benchmark): HSTU E2E training benchmark suite with progressive optimizations #340 removed the.item()to keep the dynamic shape symbolic fortorch.export, but in eager mode the resulting 0-d tensor was then forwarded totorch.empty((seq_len_a, D), ...)inside_Split2DJaggedFunction.forward, which calls.item()internally — relocating the same sync to the end of the forward pass where it blocks loss/backward kickoff. Fix branches ontorch.compiler.is_compiling(): scalar in eager, 0-d tensor under tracing.examples/hstu/ops/triton_ops/triton_jagged.pywas a stale pre-export snapshot ofexamples/commons/ops/triton_ops/triton_jagged.py. Production code (hstu_processor.py,retrieval_gr.py) already imported fromcommons; onlytest_jagged_tensor.pyandtest_position_encoder.pystill hit the local copy. Delete the duplicate, update those imports.Test plan
Run on ipp1-1621 (
computelab-job-2139432):pytest examples/hstu/test/test_hstu_preprocess.py— 8/8 passed (covers preprocessor + postprocessor, interleave/contextual/max_num_candidates=0,10 combos)pytest examples/hstu/test/test_jagged_tensor.py— 97/97 passedpytest examples/hstu/test/test_position_encoder.py— 3/3 passedjd.total_candidates_seq_lenisint(not tensor) in eager mode/buildto run the full GitLab pipeline🤖 Generated with Claude Code