Skip to content

Add Torch export for HSTU model - #327

Merged
shijieliu merged 20 commits into
NVIDIA:mainfrom
jensenhwa:jhwa/snap_export
Apr 10, 2026
Merged

shijieliu merged 20 commits into
NVIDIA:mainfrom
jensenhwa:jhwa/snap_export

Conversation

@jensenhwa

Copy link
Copy Markdown
Contributor

No description provided.

Comment thread examples/hstu/inference/README.md Resolved
Comment thread docker/Dockerfile.torch211 Resolved
Comment thread third_party/FBGEMM Resolved
@geoffreyQiu geoffreyQiu changed the title torch.export for HSTU dense model Add Torch export for HSTU model Apr 9, 2026
@geoffreyQiu
geoffreyQiu requested a review from Copilot April 9, 2026 21:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Torch export / AOTInductor export support for the HSTU inference stack (including dynamic embedding custom ops), plus Python/C++ demos and supporting operator registrations.

Changes:

  • Wrap Triton kernels and register fake/meta kernels so torch.export can trace custom/Triton ops.
  • Introduce an exportable dynamic embedding path (InferenceEmbeddingTable + ExportableEmbedding) and an export pipeline script for ranking GR.
  • Add build/run docs and a C++ AOTI inference demo to validate exported artifacts.

Reviewed changes

Copilot reviewed 48 out of 49 changed files in this pull request and generated 14 comments.

Show a summary per file
File Description
HOW_TO_BUILD_AND_RUN_DEMO.md New build/run instructions for inference_emb_ops.so and export demo.
examples/hstu/ops/triton_ops/triton_position.py Wrap Triton kernel with torch.library.wrap_triton for export compatibility.
examples/hstu/ops/triton_ops/triton_norm_mul_dropout.py Wrap Triton kernel with torch.library.wrap_triton for export compatibility.
examples/hstu/ops/triton_ops/triton_layer_norm.py Wrap Triton kernels with torch.library.wrap_triton for export compatibility.
examples/hstu/ops/triton_ops/triton_addmm.py Wrap Triton kernel with torch.library.wrap_triton for export compatibility.
examples/hstu/ops/fused_hstu_op.py Import GPU ops module to register fake impls needed by torch.export.
examples/hstu/modules/inference_embedding.py Make feature splitting conditional; add dynamic-embedding checkpoint loading path.
examples/hstu/modules/inference_dense_module.py Add exportable dense path and new forward API for export vs KVCache inference.
examples/hstu/modules/hstu_processor.py Adjust preprocessing to be more compile/export friendly (reduce .item() usage, reshape logic).
examples/hstu/modules/hstu_block.py Respect config.is_inference when wiring pre/post processors.
examples/hstu/modules/exportable_embedding.py New export-friendly embedding module loading inference_emb_ops.so + fake registrations.
examples/hstu/model/inference_ranking_gr.py Split KVCache forward into forward_with_kvcache and add export-friendly forward.
examples/hstu/inference/test_export_demo.py New end-to-end export/AOTI smoke test for inference embedding table custom ops.
examples/hstu/inference/register_hstubatch_pytree_example.py Register HSTUBatch/Jagged tensor pytrees for export package serialization.
examples/hstu/inference/inference_gr_ranking.py Update call sites to forward_with_kvcache.
examples/hstu/inference/export_inference_gr_ranking.py New Python export pipeline for the GR ranking model + test-case dumping.
examples/hstu/inference/cpp_inference/inferece_hstu_gr_ranking_exported_model.cpp New C++ AOTI runner to load package and compare outputs vs dumped refs.
examples/hstu/inference/cpp_inference/CMakeLists.txt New CMake build for the C++ AOTI runner and a CUDA ops runtime library.
examples/hstu/inference/benchmark/inference_benchmark.py Update benchmark call sites to forward_with_kvcache.
examples/hstu/GUIDE_TO_RUN_CPP_INFERENCE_DEMO.md New end-to-end guide for Python export + C++ inference demo.
examples/hstu/Dockerfile New Dockerfile to run HSTU example workflows.
examples/hstu/configs/hstu_config.py Force TP size to 1 in inference mode.
examples/commons/setup.py Add kjt_aux_op.cpp and compile definition for pybind loading behavior.
examples/commons/sequence_batch/batch.py Accept export-time integer wrapper types for batch_size validation.
examples/commons/ops/triton_ops/triton_jagged.py Change split helper signature and wrap Triton kernels for export.
examples/commons/ops/cuda_ops/JaggedTensorOpFunction.py Route through torch.ops.hstu_cuda_ops.* for registered ops usage.
examples/commons/ops/cuda_ops/fake_hstu_cuda_ops.py New fake kernels for export compatibility of hstu_cuda_ops ops.
examples/commons/ops/cuda_ops/csrc/kjt_aux_op.cpp New C++ ops implementing KJT helper ops via Torch dispatcher.
examples/commons/ops/cuda_ops/csrc/jagged_tensor_op_kernel.cu Remove pybind include to support non-pybind build paths.
examples/commons/ops/cuda_ops/csrc/jagged_tensor_op_cuda.cpp Register ops via TORCH_LIBRARY_* and keep minimal pybind loader.
examples/commons/datasets/hstu_batch.py Accept export-time integer wrapper types for max_num_candidates.
docker/Dockerfile.nve Simplify NVEmbedding installation command.
docker/Dockerfile Build dynamicemb inference ops + C++ inference demo during image build (non-tritonserver).
corelib/dynamicemb/src/unique_op.h Gate pybind declarations behind DEMB_USE_PYBIND11.
corelib/dynamicemb/src/unique_op.cu Gate pybind bindings behind DEMB_USE_PYBIND11.
corelib/dynamicemb/src/torch_utils.h Gate pybind includes/bindings behind DEMB_USE_PYBIND11.
corelib/dynamicemb/src/torch_utils.cu Gate pybind bindings behind DEMB_USE_PYBIND11.
corelib/dynamicemb/src/table_operation/table.cuh Gate pybind/extension includes behind DEMB_USE_PYBIND11.
corelib/dynamicemb/src/table_operation/lookup_torch_binding.cu New dispatcher registration + CUDA-only impl for INFERENCE_EMB::table_lookup.
corelib/dynamicemb/src/table_operation/get_table_range_torch_binding.cu New dispatcher registration + CUDA-only impl for INFERENCE_EMB::get_table_range.
corelib/dynamicemb/src/table_operation/expand_table_ids_torch_binding.cu New dispatcher registration + CUDA-only impl for INFERENCE_EMB::expand_table_ids.
corelib/dynamicemb/src/index_calculation.h Gate pybind binding declaration behind DEMB_USE_PYBIND11.
corelib/dynamicemb/src/index_calculation.cu Gate pybind includes/bindings behind DEMB_USE_PYBIND11.
corelib/dynamicemb/setup.py Add DEMB_USE_PYBIND11 defines and exclude new dispatcher-only binding sources from extension build.
corelib/dynamicemb/dynamicemb/lookup_meta.py New fake/meta kernel registration for INFERENCE_EMB::table_lookup.
corelib/dynamicemb/dynamicemb/index_range_meta.py New fake/meta kernel registration for INFERENCE_EMB::get_table_range / expand_table_ids.
corelib/dynamicemb/dynamicemb/exportable_tables.py New exportable inference embedding table implementation using dispatcher ops + NVE.
corelib/dynamicemb/CMakeLists.txt New standalone CMake build for inference_emb_ops.so.
.gitignore Ignore build artifacts and export/demo dumps.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@geoffreyQiu
geoffreyQiu marked this pull request as ready for review April 9, 2026 22:37
@greptile-apps

greptile-apps Bot commented Apr 9, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a torch.export / AOTInductor path for the HSTU ranking model, enabling C++ inference without Python. It introduces ExportableEmbedding (backed by a new InferenceEmbeddingTable hash table), registers HSTUBatch/JaggedTensor/KJT as serializable pytree nodes, wraps Triton kernels with torch.library.wrap_triton, and adds export_inference_gr_ranking.py as the end-to-end export script. The dense pipeline is unified by adding a use_exportable flag to InferenceDenseModule that selects HSTUBlock instead of HSTUBlockInference.

Confidence Score: 4/5

Safe to merge for the single-user (batch_size=1) export use case; the fake op shape issue should be resolved before enabling dynamic multi-user batches.

One P1 finding: _lengths_reduce_dim1_fake returns a static (num_splits,) shape that is incorrect for batch_size > 1, which would cause incorrect symbolic shape propagation during torch.export with dynamic batch. The remaining findings are P2 (leftover NVTX code, training-only contextual seqlen edge case). Previous review concerns are all addressed.

examples/commons/ops/cuda_ops/fake_hstu_cuda_ops.py — the _lengths_reduce_dim1_fake output shape needs verification against the real kernel for dynamic batch support.

Important Files Changed

Filename Overview
examples/commons/ops/cuda_ops/fake_hstu_cuda_ops.py New abstract (fake) op implementations for torch.export; _lengths_reduce_dim1_fake returns the static size (num_splits,) instead of the dynamic (batch_size,), which is inconsistent with _lengths_splits_fake and incorrect for batch_size > 1.
examples/hstu/inference/export_inference_gr_ranking.py New end-to-end export script; contains a leftover empty NVTX range (lines 430-432) in the test loop that should be removed.
examples/hstu/modules/hstu_processor.py Contextual-seqlen zero-guard removed to avoid .item() during compilation; changes training behaviour when all contextual seqlens are zero.
corelib/dynamicemb/dynamicemb/exportable_tables.py New export-compatible embedding table implementation; includes the previously-requested non-decreasing feature_table_map validation and careful capacity/offset bookkeeping.
examples/hstu/modules/inference_dense_module.py Refactored to support use_exportable mode using HSTUBlock instead of HSTUBlockInference; kvcache/cudagraph setup correctly skipped for exportable path.
examples/hstu/model/inference_ranking_gr.py Old forward renamed to forward_with_kvcache; new forward added for export path calling sparse and dense modules sequentially.
examples/hstu/modules/exportable_embedding.py New export-compatible embedding module wrapping InferenceEmbeddingTable; cleanly handles KJT→JT conversion via custom ops with appropriate fake registrations.
examples/hstu/inference/register_hstubatch_pytree_example.py New pytree registration for HSTUBatch, KJT, and JT for torch.export serialization; guards against double-registration and excludes labels from exported inputs.

Sequence Diagram

sequenceDiagram
    participant Script as export_inference_gr_ranking.py
    participant Pytree as register_hstu_export_pytrees()
    participant Model as InferenceRankingGR
    participant Sparse as ExportableEmbedding
    participant EmbTable as InferenceEmbeddingTable
    participant Dense as InferenceDenseModule
    participant HSTUBlock as HSTUBlock
    participant MLP as MLP
    participant NVE as export_aot (pynve)

    Script->>Pytree: register HSTUBatch / KJT / JT pytree nodes
    Script->>Model: load_checkpoint()
    Script->>NVE: export_aot(model, (batch,), dynamic_shapes)
    NVE->>Model: trace forward(batch)
    Model->>Sparse: forward(batch.features)
    Sparse->>EmbTable: forward(keys, offsets)
    EmbTable->>EmbTable: expand_table_ids (custom op)
    EmbTable->>EmbTable: hash table lookup
    EmbTable->>EmbTable: NVEmbedding lookup
    Sparse-->>Model: Dict[str, JaggedTensor]
    Model->>Dense: forward(batch, embeddings)
    Dense->>HSTUBlock: forward(embeddings, batch)
    HSTUBlock-->>Dense: JaggedData
    Dense->>MLP: forward(jd.values)
    MLP-->>Dense: logits
    Dense-->>Model: logits
    Model-->>NVE: logits
    NVE-->>Script: ExportedProgram + model.pt2 package
Loading

Reviews (4): Last reviewed commit: "Fix" | Re-trigger Greptile

Comment thread examples/hstu/modules/hstu_processor.py Resolved
Comment thread corelib/dynamicemb/CMakeLists.txt Resolved
@greptile-apps

greptile-apps Bot commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

Tip:

Greploop — Automatically fix all review issues by running /greploops in Claude Code. It iterates: fix, push, re-review, repeat until 5/5 confidence.

Use the Greptile plugin for Claude Code to query reviews, search comments, and manage custom context directly from your terminal.

@shijieliu
shijieliu merged commit efb9bde into NVIDIA:main Apr 10, 2026
JacoCheung added a commit to JacoCheung/recsys-examples that referenced this pull request Apr 13, 2026
Caching currently adds overhead without benefit after PR NVIDIA#327 rebase.
Remove exp3_caching from all experiment files until caching performance
is improved.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
JacoCheung added a commit to JacoCheung/recsys-examples that referenced this pull request Apr 13, 2026
…gle file

Caching currently adds overhead without benefit after PR NVIDIA#327 rebase.
Remove all caching/recompute/TP experiments and extra experiment files,
keep only experiments.txt with baseline/shuffler/cutlass.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
JacoCheung added a commit to JacoCheung/recsys-examples that referenced this pull request Apr 16, 2026
The module hstu.hstu_ops_gpu does not exist as a Python module.
The C++ source hstu_ops_gpu.cpp compiles into hstu/fbgemm_gpu_experimental_hstu.so,
not a separate hstu_ops_gpu submodule. This import was incorrectly added in PR NVIDIA#327
and causes ModuleNotFoundError in CI.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
shijieliu pushed a commit that referenced this pull request Apr 17, 2026
…363)

* fix: reduce Docker image layers to avoid overlay2 max depth limit

Aggressively merge RUN instructions in the Dockerfile to reduce total
layer count from ~126 to ~119. The inference image was hitting the
overlay2 128-layer limit ("failed to register layer: max depth
exceeded") on CI nodes.

devel stage: 8 RUN + 1 COPY -> 4 RUN + 1 COPY (-4 layers)
build stage: 4 RUN + 1 COPY -> 1 RUN + 1 COPY (-3 layers)
FBGEMM and TorchRec kept as separate layers for build cache efficiency.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* ci: add pull_request_target trigger for auto CI on PR open/sync

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* Fix imports for fake ops wrapper used in expor

* fix: remove invalid import of hstu.hstu_ops_gpu

The module hstu.hstu_ops_gpu does not exist as a Python module.
The C++ source hstu_ops_gpu.cpp compiles into hstu/fbgemm_gpu_experimental_hstu.so,
not a separate hstu_ops_gpu submodule. This import was incorrectly added in PR #327
and causes ModuleNotFoundError in CI.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* fix: update FBGEMM submodule to include hstu_ops_gpu.py fake impl

Update from 04df536 to 65bad42 which adds fake tensor implementations
for torch.export (hstu_ops_gpu.py). This was missing since PR #340
accidentally reverted the submodule pointer.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* ci: allow /build with flags by matching prefix instead of exact string

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* ci: remove pull_request_target trigger, keep only /build comment

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
Co-authored-by: Junyi Qiu <[email protected]>
geoffreyQiu pushed a commit that referenced this pull request May 14, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants