Skip to content

Extend libtorch-stable ABI compat for torch_musa 2.9 (vLLM v0.24.0 kernels) - #86

Merged
yeahdongcn merged 14 commits into
mainfrom
xd/stable-abi-v0240-accessors
Jul 1, 2026
Merged

yeahdongcn merged 14 commits into
mainfrom
xd/stable-abi-v0240-accessors

Conversation

@yeahdongcn

Copy link
Copy Markdown
Collaborator

Summary

torch_musa 2.9.0's torch::stable runtime predates the torch 2.10 stable-ABI
surface that vLLM v0.24.0's csrc/libtorch_stable/ kernels (and the equivalent
SGLang kernels) are written against. This extends torchada's libtorch-stable
compat layer so those kernels build and register on MUSA without patching the
kernels themselves.

What's added

  • torch::stable::Device / DeviceType shim (device.h) with the ctors and
    == / is_cpu / is_cuda / is_privateuseone predicates the kernels use.
  • torch::stable::Tensor accessors backported into tensor_struct.h at
    build time — sizes / strides / device / element_size /
    storage_offset, and the templated mutable_data_ptr<T> / const_data_ptr<T>
    — gated on TORCHADA_STABLE_ACCESSORS so a TU that does not force-include the
    box header is untouched.
  • TORCH_BOX boxer (torchada_stable_box.h) synthesized over torch_musa's
    from / to conversions, including spreading std::tuple / std::pair
    returns across stack slots.
  • torch::stable free functions torch_musa 2.9 lacks: contiguous,
    flatten, empty, from_blob.
  • THO_DISPATCH_* shim (torch/headeronly/core/Dispatch.h) and a
    torch/csrc/stable/macros.h forward.
  • CUDA→MUSA header shims for stable kernels that include them directly:
    cuda_runtime.h, cuda_bf16.h (+ __nv_bfloat16{,2} alias), cuda_fp16.h,
    cuda_fp8.h, and cublas_v2.h → mublas.h.
  • Build wiring: include_paths() auto-appends the compat include dir on MUSA,
    and the header backport runs lazily at extension-build time (never at import,
    so a bare import torchada / inference run never mutates the torch headers).

Testing

Added TestStableCompatHeaders + TestStableHeaderBackport to
tests/test_cuda_patching.py, covering the compat headers, the TORCH_BOX
shim, and the accessor / inline-rewrite backport transforms (path helpers,
header content, cuda_* → musa_* redirects, injection injected / already
/ anchor-missing statuses + idempotency, inline-rewrite skip rules).

Full suite run in a MUSA container (MTT S5000, torch_musa 2.9.0 / musa 40305):

PYTHONPATH=src python -m pytest tests/test_cuda_patching.py
=> 201 passed, 4 skipped, 0 failed

Notes

  • Targets torch_musa 2.9's ABI specifically. The torch 2.11 from_blob
    deleter overload and the fully-defaulted / layout-enum empty forms are
    intentionally not shimmed — torch_musa 2.9's AOTI C-shim has no
    deleter-capable create_tensor_from_blob, so a dropped deleter would leak the
    backing storage; kernels needing those forms stay out of the MUSA kernel set.
  • The header backport surfaces a warning if the tensor_struct.h anchor is
    absent, instead of silently skipping and failing later in the compiler.

yeahdongcn added 12 commits July 1, 2026 14:03
…rface)

torch_musa 2.9's torch::stable predates several torch-2.10 stable APIs that newer libtorch-stable kernels call. Add torch::stable::Device, Tensor::{sizes,strides,device}() (patched into tensor_struct.h, gated on TORCHADA_STABLE_ACCESSORS), a non-template void* mutable_data_ptr(), and torch::stable::{empty,from_blob} (box header), all built on torch_musa's AOTI C-shim.
…os.h shims

Three fixes for newer libtorch-stable kernels on torch_musa 2.9: move the headeronly Dispatch.h scalar_type into torchada_detail (torch_musa's ATen/Dispatch.h already defines ::detail::scalar_type for the same type -> ODR); add a cuda_runtime.h shim -> musa_runtime.h; add a torch/csrc/stable/macros.h shim -> library.h (the macros pre-date the 2.10 header split).
More torch-2.10 stable surface newer libtorch-stable kernels use: move Device into torch/csrc/stable/device.h with operator==/!= and is_cpu(); add torch::stable::flatten (aten::flatten.using_ints via the dispatcher); add a cublas_v2.h -> mublas.h shim.
v0.24 libtorch-stable kernels call const_data_ptr() without a template arg (like mutable_data_ptr()).
v0.24 libtorch-stable kernels: q_gemm compares device().type() against torch::stable::DeviceType::Meta (type() now returns the enum); minimax uses STD_CUDA_CHECK on musa runtime calls.
…ce ctors, kernel-launch check, cuda_bf16.h shim
Factor the tensor_struct.h accessor injection and the tensor_inl.h inline
rewrite into module-level helpers, and warn when the numel() anchor is absent
so a skipped backport surfaces immediately instead of as a later compiler
error. Correct the empty() shim comment (the third positional slot is layout,
not pin_memory) and give from_blob an explicit storage_offset. Add unit tests
for the compat headers, the TORCH_BOX shim, and the backport transforms.
@yeahdongcn
yeahdongcn force-pushed the xd/stable-abi-v0240-accessors branch from 1d71468 to 292496d Compare July 1, 2026 06:06
@augmentcode

augmentcode Bot commented Jul 1, 2026

Copy link
Copy Markdown
🤖 Augment PR Summary

Summary: This PR extends torchada’s libtorch-stable compatibility layer so vLLM v0.24.0 / SGLang stable-ABI kernels (written against newer torch::stable surfaces) can build and register on torch_musa 2.9 without patching kernel sources.

Changes:

  • Added stable-compat header shims that redirect CUDA-only includes to MUSA equivalents (e.g. cuda_runtime.h, cuda_fp16.h, cuda_bf16.h, cublas_v2.h).
  • Introduced torch::stable::Device/DeviceType shim and a torch/csrc/stable/macros.h forwarder for older torch_musa layouts.
  • Shipped a THO_DISPATCH_* shim (torch/headeronly/core/Dispatch.h) and avoided namespace collisions with ATen’s ::detail.
  • Expanded the force-include stable box header to backport missing stable free functions (contiguous, flatten, empty, from_blob) and add MUSA runtime/kernel-launch check macros used by upstream kernels.
  • Enhanced the TORCH_BOX shim to support tuple/pair multi-value returns by spreading elements across StableIValue stack slots.
  • Updated extension build wiring to append the stable_compat include dir on MUSA and to lazily backport missing torch::stable::Tensor accessors / inline definitions at extension-build time.
  • Added unit tests validating header presence, path helpers, and idempotent/string-transform backport behavior.

Technical Notes: Header backporting is best-effort and gated so normal import torchada / inference runs do not mutate torch headers; the compat headers are appended last in include paths so future real torch_musa headers can take precedence.

🤖 Was this summary useful? React with 👍 or 👎

@augmentcode augmentcode Bot 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.

Review completed. 2 suggestions posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

Comment thread src/torchada/csrc/stable_compat/torch/csrc/stable/device.h
Comment thread src/torchada/utils/cpp_extension.py Outdated
…guard

Give torch::stable::Device default member initializers so a default-constructed
Device is well-defined (an unset device: is_cuda/is_cpu/is_privateuseone all
false) rather than indeterminate. Rely on the regex's leading-inline lookahead
alone for the tensor_inl.h inline rewrite's idempotency, dropping the "inline"
substring guard that could skip a Tensor:: definition whose trailing comment
contains "inline". Add regression tests for both.
Signed-off-by: Xiaodong Ye <xiaodong.ye@mthreads.com>
@yeahdongcn
yeahdongcn merged commit 92f964f into main Jul 1, 2026
@yeahdongcn
yeahdongcn deleted the xd/stable-abi-v0240-accessors branch August 13, 2026 12:17
yeahdongcn added a commit that referenced this pull request Oct 10, 2026
The English and Chinese READMEs have not kept up with the May-October work.
This documents what merged, moves the version-gated shims into one table, and
refreshes the measured numbers that had gone stale.

Feature table
- CUDA memory-pool APIs, `torch.cuda.streams`, CUDA-graph executable rotation,
  `torch.cuda._get_device_index`, `get_memory_info()`, and the FlashAttention
  provider shims (#61, #98, #103, #106, #108, #115)
- the "What Works" table goes back to one line per feature; the paragraph-sized
  `log_` / `isfinite` / `out_dtype` cells move into the new section below

New "torch_musa Compatibility" section
- one table of every version-gated shim with the release it is installed on:
  the four `< 2.11.0.post2` patches (#106, #113, #124), the `< 2.13.0`
  `mm`/`bmm` `out_dtype=` backport (#116), the stable-ABI header backport (#86,
  #96), asynchronous `isfinite` (#120), and `torch.cuda.streams` (#98)

New "Environment Variables" section
- the graph-rotation knobs (#72), `TORCHADA_PLATFORM`, the C++ operator-override
  switches (#61, #128), and the two variables that were already documented

Corrected and extended details
- torch.compile: FX `device` builtin (#124), Dynamo's device-index helper (#108),
  `MUSA_VISIBLE_DEVICES` mirroring (#106)
- C++ extensions: nested `<torch/cuda.h>` porting (#95), stable-ABI
  `STABLE_TORCH_LIBRARY_IMPL` rekeying and stream helpers (#100), torch 2.6+
  `include_paths`/`library_paths` signatures (#121), stale JIT build locks (#128)
- MoE tables are generated from checked-in recipes (#115)
- unsupported CUDA runtime APIs as no-ops (#65)
- Performance: replace the 0.1.94 / torch_musa 2.7.1 numbers with the checked-in
  0.1.95 / 2.11.0.post2 entry, and stop claiming every fast path is under 200ns
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.

1 participant