Skip to content

refactor: migrate to fbgemm_gpu_hstu, remove legacy HSTU compat layer - #321

Merged
shijieliu merged 11 commits into
NVIDIA:mainfrom
JacoCheung:junzhang/update_hstu_attention_api
Mar 12, 2026
Merged

shijieliu merged 11 commits into
NVIDIA:mainfrom
JacoCheung:junzhang/update_hstu_attention_api

Conversation

@JacoCheung

@JacoCheung JacoCheung commented Mar 10, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Migrate the HSTU attention kernel from the in-tree corelib/hstu package (per-architecture C-extensions hstu_attn_2_cuda / hstu_hopper_cuda) to the standalone fbgemm_gpu_hstu package (import hstu), which provides a unified API and auto-dispatches to the appropriate kernel by GPU architecture (SM 8.0 / 9.0).

What changed (vs main)

Dockerfile

  • Removed legacy corelib/hstu installation steps.
  • Added COPY third_party/FBGEMM from git submodule and pip install of fbgemm_gpu_hstu with HSTU_ARCH_LIST="8.0 9.0 10.0".

HSTU attention modules

  • hstu_attention.py: replaced from hstu_attn import hstu_attn_varlen_func with from hstu import hstu_attn_varlen_func and updated all call sites to the new positional API (added seqused_q, seqused_k, scaling_seqlen as positional args).
  • paged_hstu_infer_layer.py: same import migration and API update, including paged KV-cache paths.
  • fused_hstu_op.py: replaced calls to legacy hstu_attn_2_cuda / hstu_hopper_cuda C-extensions with direct calls to torch.ops.fbgemm.hstu_varlen_{fwd,bwd}_{80,90}, dispatching by sm_major_version. Added FP8 descale None placeholders for SM 9.0 backward.

Tests

  • test_hstu_attn_smoke.py, test_paged_hstu_attn_kernel.py: updated imports, use pytest.importorskip for graceful skips.
  • test_fbgemm_hstu_smoke.py (new): forward + backward smoke test comparing new HSTU kernel against PyTorch reference.

Documentation

  • corelib/hstu/README.md: added deprecation notice pointing to fbgemm_gpu_hstu.
  • examples/hstu/training/README.md: added migration note, updated clone commands to --recursive.

Submodule

  • .gitmodules: added third_party/FBGEMM → jiayus-nvidia/FBGEMM (pinned commit 04df5361; will switch to upstream once standalone-building fix is merged).

Test plan

  • Forward + backward smoke test: new HSTU kernel vs PyTorch reference (atol=1e-3 / 1e-2)
  • Triton HSTU attention smoke test (unaffected, passing)
  • CI: Docker build, pre-commit, 1GPU H100 unit test, 4GPU unit test
  • E2E training convergence test
  • Inference test with paged KV-cache path

CI

@greptile-apps

greptile-apps Bot commented Mar 10, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR completes the migration of the HSTU attention kernels from the legacy in-tree corelib/hstu package (per-architecture hstu_attn_2_cuda / hstu_hopper_cuda C-extensions) to the standalone fbgemm_gpu_hstu package (import hstu), which provides a unified hstu_attn_varlen_func API that auto-dispatches to the correct SM-specific CUDA kernel. The change also removes the now-redundant FusedHSTUAttentionHopper class, simplifying the attention module hierarchy.

Key changes:

  • API migration: All call sites updated to the new positional argument order (seqused_q, seqused_k, scaling_seqlen added as explicit positional args; legacy rab, has_drab, cu_seqlens_t kwargs removed).
  • fused_hstu_op.py: Direct torch.ops.fbgemm.hstu_varlen_{fwd,bwd}_{80,90} calls replace the legacy C-extension dispatch; dtype assertions guard the Hopper paths.
  • Tests: All test files updated with pytest.importorskip guards; a new test_fbgemm_hstu_smoke.py verifies forward + backward against PyTorch bf16 and fp32 references.
  • Infrastructure: third_party/FBGEMM added as a git submodule (personal fork, pinned to commit 04df5361, acknowledged as transitional); Dockerfile updated to build from the submodule.
  • One minor inconsistency remains: the FusedHSTULayer class docstring pseudo-code was updated to hstu.hstu_attn_varlen_func rather than the bare hstu_attn_varlen_func used in every other updated file.

Confidence Score: 4/5

  • PR is safe to merge; the migration is clean, CI is passing across 7 pipelines, and all previously flagged issues have been addressed or acknowledged.
  • Score reflects a well-executed, thoroughly tested migration with multiple CI runs. The only remaining rough edges are intentional and acknowledged: the submodule points to a personal fork (transitional), Dockerfile contains TODO markers (transitional), and the unconditional import hstu is accepted per project policy. The single new finding is a trivial docstring inconsistency.
  • No files require special attention; docker/Dockerfile and .gitmodules contain acknowledged transitional references to the personal fork that should be updated once the upstream FBGEMM merge completes.

Important Files Changed

Filename Overview
examples/hstu/modules/hstu_attention.py Unified Ampere/Hopper into a single FusedHSTUAttention using the new hstu_attn_varlen_func; removed FusedHSTUAttentionHopper and simplified create_hstu_attention factory. Clean and correct migration.
examples/hstu/ops/fused_hstu_op.py Replaced legacy hstu_attn_2_cuda/hstu_hopper_cuda C-extensions with direct torch.ops.fbgemm.hstu_varlen_{fwd,bwd}_{80,90} calls; added dtype assertions for Hopper paths. Correctness verified by CI.
examples/hstu/modules/paged_hstu_infer_layer.py Migrated from hstu_attn.hstu_attn_varlen_func to direct hstu_attn_varlen_func import; updated all call sites with new positional API. Paged KV-cache kwargs are still forwarded and confirmed working per PR checklist.
examples/hstu/test/hstu_attn/test_fbgemm_hstu_smoke.py New forward+backward smoke test comparing fbgemm_gpu_hstu against PyTorch bf16 and fp32 references; uses pytest.importorskip for graceful skipping and arch-based skip for non-SM8/9 GPUs.
examples/hstu/test/test_paged_hstu_attn_kernel.py Migrated to new hstu import with pytest.importorskip; added module-level signature inspection assert to verify kv_cache support. Assert (vs pytest.skip) causes a collection ERROR rather than a SKIP if the check fails, but paged KV-cache path is confirmed working in CI.
docker/Dockerfile Replaced legacy corelib/hstu install with fbgemm_gpu_hstu from third_party/FBGEMM submodule; TODO comments and placement outside devel stage acknowledged as intentional transitional state.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[FusedHSTUAttention.forward] -->|hstu_attn_varlen_func| B[hstu package\nauto-dispatch]
    B -->|SM 8.x| C[torch.ops.fbgemm.hstu_varlen_fwd_80]
    B -->|SM 9.0| D[torch.ops.fbgemm.hstu_varlen_fwd_90]

    E[FusedHSTULayerFunction.forward\nCUTLASS path] -->|SM 8| C
    E -->|SM 9| D

    F[PagedHSTUInferLayer.forward] -->|hstu_attn_varlen_func\n+ paged KV kwargs| B

    G[create_hstu_attention] -->|SM in 8,9| A
    G -->|other SM| H[TorchHSTUAttention\nfallback]
    G -->|TRITON backend| I[TritonHSTUAttention]

    C --> J[CUTLASS Ampere kernel]
    D --> K[CUTLASS Hopper kernel]
Loading

Comments Outside Diff (1)

  1. examples/hstu/modules/fused_hstu_layer.py, line 28 (link)

    Stale pseudo-code in docstring

    The class docstring was updated from hstu_attn.hstu_attn_varlen_func to hstu.hstu_attn_varlen_func, but the equivalent docstring in paged_hstu_infer_layer.py was correctly updated to just hstu_attn_varlen_func (matching the from hstu import hstu_attn_varlen_func import style used across the codebase). The hstu. prefix here is inconsistent with how the function is actually imported and called in every other module.

Last reviewed commit: 43618e4

Comment thread examples/hstu/ops/fused_hstu_op.py Resolved
Comment thread docker/Dockerfile Resolved
Comment thread examples/hstu/ops/fused_hstu_op.py Resolved
Comment thread examples/hstu/ops/fused_hstu_op.py Resolved
Comment thread .gitignore Resolved
Comment thread docker/Dockerfile Resolved
Comment thread examples/hstu/ops/fused_hstu_op.py Resolved
Comment thread .gitmodules Resolved
@JacoCheung JacoCheung changed the title feat: integrate fbgemm_gpu_hstu with priority-based fallback to legacy HSTU kernels refactor: migrate to fbgemm_gpu_hstu, remove legacy HSTU compat layer Mar 11, 2026
@JacoCheung
JacoCheung force-pushed the junzhang/update_hstu_attention_api branch from 3c95fd9 to 6ec37e9 Compare March 11, 2026 05:43
Comment thread examples/hstu/ops/fused_hstu_op.py Resolved
Comment thread corelib/hstu/README.md Resolved
Comment thread examples/hstu/modules/hstu_attention.py Resolved
Comment thread examples/hstu/ops/fused_hstu_op.py Resolved
@JacoCheung
JacoCheung force-pushed the junzhang/update_hstu_attention_api branch from 72fd215 to 213ddbd Compare March 11, 2026 07:50
…y HSTU kernels

Add support for the new unified FBGEMM HSTU attention package (fbgemm_gpu_hstu,
import name: hstu) alongside the existing corelib/hstu packages (hstu_attn,
hopper). The new package is prioritized at import time; if unavailable, the
legacy interfaces are used as fallback.

- Dockerfile: install fbgemm_gpu_hstu from jiayus-nvidia/FBGEMM fork
- hstu_attention.py: add _make_new_hstu_compat wrapper to bridge API
  differences (seqused_q/k, positional arg reordering, dynamic defaults)
  and _import_hstu_attn_func for priority-based import
- paged_hstu_infer_layer.py: try-except import with SimpleNamespace shim
- fused_hstu_op.py: adapter classes mapping legacy C-extension signatures
  to torch.ops.fbgemm.hstu_varlen_{fwd,bwd}_{80,90}
- test_hstu_attn_smoke.py, test_paged_hstu_attn_kernel.py: updated imports
  to use priority-based fallback

Made-with: Cursor
Mark corelib/hstu as deprecated in its README with migration instructions
and link to the new unified package at jiayus-nvidia/FBGEMM. Add a note
in the training README where corelib/hstu is referenced.

Made-with: Cursor
- Guard Hopper varlen_bwd against empty *args to prevent IndexError
- Use from-import style in paged_hstu_infer_layer to catch AttributeError
- Add runtime guard for unsupported paged KV-cache kwargs in compat wrapper
- Add pytest.skip() when kernel function is None in test_hstu_attn_smoke
- Add blank line after except ImportError block in fused_hstu_op

Made-with: Cursor
Docker build is non-interactive so pip uninstall prompts for Y/n
confirmation and fails with EOFError.

Made-with: Cursor
…ule directly

- Remove _NewHSTUAmpereCompat, _NewHSTUHopperCompat classes and
  _make_new_hstu_compat / _import_hstu_attn_func wrappers
- Update all call sites to use hstu.hstu_attn_varlen_func directly
- Replace git clone with FBGEMM submodule for fbgemm_gpu_hstu in Dockerfile
- Update README docs with --recursive clone and submodule install instructions

Made-with: Cursor
…e newline

- Replace hard top-level imports with pytest.importorskip for fbgemm_gpu
  and hstu in test files to avoid collection ERRORs on environments
  without those packages
- Add trailing newline to .gitignore (POSIX compliance)

Made-with: Cursor
hstu_varlen_bwd_90 expects 11 Optional[Tensor] fp8 descale parameters
(q/qt/k/kt/v/do/dot/cu_qt/cu_kt/cu_q_block/cu_kv_block) between
func and output_dtype. Without them, output_dtype (int) was passed
to q_descale (Optional[Tensor]), causing RuntimeError on H100.

Made-with: Cursor
…tention class

The new hstu_attn_varlen_func auto-dispatches by GPU arch internally,
so FusedHSTUAttention and FusedHSTUAttentionHopper had identical forward()
implementations. Collapse into a single FusedHSTUAttention for both SM 8/9.

Made-with: Cursor
- Add dtype assert for Hopper fwd/bwd output_dtype mapping
- Add kv_cache signature check in paged attn test
- Fix stale docstring in paged_hstu_infer_layer

Made-with: Cursor
@JacoCheung
JacoCheung force-pushed the junzhang/update_hstu_attention_api branch from 6439b89 to 43618e4 Compare March 12, 2026 07:23

@geoffreyQiu geoffreyQiu left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approve for the code and tests.

@shijieliu
shijieliu merged commit 218f664 into NVIDIA:main Mar 12, 2026
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.

3 participants