Skip to content

[HIP] [CK] [JIT] torch-free a4w4 GEMM + C++ library build - #4124

Merged
amd-ruitang3 merged 12 commits into
mainfrom
zain/torch-free/a4w4-poc
Oct 13, 2026
Merged

amd-ruitang3 merged 12 commits into
mainfrom
zain/torch-free/a4w4-poc

Conversation

@Micky774

@Micky774 Micky774 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Motivation

This PR aims to update the a4w4 GEMM implementation to be torch-free, as well as exposing a C++ API for separate compilation (similar to what is currently done for libmha_{f,b}wd) for use in TE as a common operation exposed through both PyTorch and JAX integrations.

Technical Details

This PR swaps torch-specific aspects for neutral AITER-provided components (e.g. aiter_tensor_t), as well as providing a new C++ interface and compilation script for exporting a C++ library build for downstream consumption.

Test Plan

Verify against a branch build of TE with a C++ level integration

Test Result

The validation passes locally and the GEMMs are succesfully exported.

Submission Checklist

@Micky774 Micky774 changed the title Initial in-place torch-free modification + compile script torch-free a4w4 GEMM + C++ library build Jul 7, 2026
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 4124 --add-label <label>

@zufayu
zufayu removed the request for review from amd-ruitang3 July 13, 2026 02:25
wangye805
wangye805 previously approved these changes Jul 14, 2026

@wangye805 wangye805 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

Comment thread op_tests/cpp/gemm_a4w4/compile.py Resolved
Comment thread csrc/include/pybind_aiter_compat.h Resolved
@Micky774
Micky774 marked this pull request as ready for review July 22, 2026 17:17
@Micky774
Micky774 requested review from a team and a lite review from Copilot July 22, 2026 17:17
@Micky774

Copy link
Copy Markdown
Contributor Author

@zufayu this PR is ready for review now

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the a4w4 blockscale GEMM implementation to move torch dependencies out of the kernel layer (switching to aiter_tensor_t + explicit hipStream_t) and adds build targets/scripts for exporting standalone C++ libraries suitable for downstream (e.g., TE) integration.

Changes:

  • Introduces torch-aware pybind wrappers that convert torch::Tensor → aiter_tensor_t and pass the current HIP stream into torch-free kernel APIs.
  • Updates the CK a4w4 blockscale GEMM kernel layer + instance generation to use aiter_tensor_t and hipStream_t, exposing exported C++ symbols.
  • Adds standalone (non-Python-module) JIT build configs and a small compile driver for producing libgemm_a4w4_* artifacts.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
op_tests/cpp/gemm_a4w4/compile.py Adds a small driver script to build/export standalone a4w4 GEMM libraries via compile_ops.
csrc/pybind/gemm_a4w4_blockscale_pybind.cu Makes the pybind entrypoint a thin torch-aware wrapper over the new torch-free aiter::gemm_a4w4_blockscale.
csrc/pybind/gemm_a4w4_blockscale_tune_pybind.cu Same as above, but for the tuning entrypoint.
csrc/include/pybind_aiter_compat.h Adds a shared helper to shallow-convert torch::Tensor to aiter_tensor_t for pybind wrappers.
csrc/ck_gemm_a4w4_blockscale/include/gemm_a4w4_blockscale.h Changes the public interface to torch-free C++ APIs (aiter_tensor_t + hipStream_t) and exports symbols for library builds.
csrc/ck_gemm_a4w4_blockscale/include/gemm_a4w4_blockscale_common.cuh Updates the core implementation to use aiter_tensor_t, HipDeviceGuard, AITER_CHECK, and explicit stream plumbing.
csrc/ck_gemm_a4w4_blockscale/gen_instances.py Updates generated kernel signatures/instantiations to accept aiter_tensor_t and hipStream_t.
csrc/ck_gemm_a4w4_blockscale/gemm_a4w4_blockscale.cu Refactors dispatch + top-level implementation into the torch-free aiter:: API and threads through hipStream_t.
csrc/ck_gemm_a4w4_blockscale/gemm_a4w4_blockscale_tune.cu Refactors tuner dispatch and API to be torch-free (aiter_tensor_t + hipStream_t) and uses AITER_CHECK.
aiter/jit/utils/torch_guard.py Adds the new compile entrypoints to the non-wrapped op list (torch.compile guard integration).
aiter/jit/optCompilerConfig.json Adds new standalone build targets (libgemm_a4w4_blockscale, libgemm_a4w4_asm) with torch_exclude=True and is_python_module=False.

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

Comment thread op_tests/cpp/gemm_a4w4/compile.py Resolved
Comment thread csrc/include/pybind_aiter_compat.h Resolved
@zufayu
zufayu requested a review from amd-ruitang3 July 23, 2026 01:40
@amd-ruitang3

amd-ruitang3 commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

@Micky774 ,
You only removed torch from kernel.cu. Pybind.cu and py_itfs_common.h‎ still include torch.
Are you only requiring kernel.cu to be free of torch? Doesn't your JAX use pybind to launch kernel?

Now we try to remove torch from C/C++/CU. You can refer to:
#4569
#4393

@Micky774

Copy link
Copy Markdown
Contributor Author

@Micky774 , You only removed torch from kernel.cu. Pybind.cu and py_itfs_common.h‎ still include torch. Are you only requiring kernel.cu to be free of torch? Doesn't your JAX use pybind to launch kernel?

Now we try to remove torch from C/C++/CU. You can refer to: #4569 #4393

We don't require the pybind to be torch-free since we use the generated .so directly. Would you consider it a requirement for this PR to make the pybind torch free? It'll still be built with torch libs when is_python_module=True is set right?

@amd-ruitang3

Copy link
Copy Markdown
Contributor

@Micky774 , You only removed torch from kernel.cu. Pybind.cu and py_itfs_common.h‎ still include torch. Are you only requiring kernel.cu to be free of torch? Doesn't your JAX use pybind to launch kernel?
Now we try to remove torch from C/C++/CU. You can refer to: #4569 #4393

We don't require the pybind to be torch-free since we use the generated .so directly. Would you consider it a requirement for this PR to make the pybind torch free? It'll still be built with torch libs when is_python_module=True is set right?

Yes. We want Torch to exist only on the Python side.

@Micky774

Copy link
Copy Markdown
Contributor Author

@amd-ruitang3 I've updated the PR -- let me know if you need to see any changes, thanks!

Copilot AI review requested due to automatic review settings September 3, 2026 02:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review is ineligible. To be eligible to request a review, you need a paid Copilot license, or your organization must enable Copilot code review.

@github-actions github-actions Bot changed the title torch-free a4w4 GEMM + C++ library build [HIP] [CK] torch-free a4w4 GEMM + C++ library build Sep 3, 2026
@github-actions github-actions Bot added the CK label Sep 3, 2026
@github-actions github-actions Bot added the HIP label Sep 3, 2026
Copilot AI review requested due to automatic review settings September 15, 2026 02:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved critical ABI and exception-handling issues remain, along with moderate validation and standalone-build gaps.

Get a fresh assessment by requesting another Copilot review.

Review details

Suppressed comments (2)

aiter/ops/gemm_op_a4w4.py:573

  • The tuning binding has the same ABI mismatch: develop=True uses module_aiter_core.aiter_tensor_t, while module_gemm_a4w4_blockscale_tune defaults to torch_exclude=False and is compiled with the PyTorch pybind11 ABI. Calls will fail to convert the marshalled tensor arguments; mark this module torch_exclude=True in optCompilerConfig.json as well.
    develop=True,

csrc/ck_gemm_a4w4_blockscale/gemm_a4w4_blockscale_tune.cu:48

  • The new cast changes the validation for negative IDs: id < (int)lookup.size() accepts -1, after which lookup.find(-1) misses and silently falls back to kernel 0. The previous unsigned comparison rejected negative IDs. Please validate the lower bound as well so an invalid tuning request cannot run a different kernel.
  AITER_CHECK(id < (int)lookup.size(),
              "Kernel id ", id, " is out of range!");
  • Files reviewed: 8/8 changed files
  • Comments generated: 4
  • Review effort level: Lite

Comment thread aiter/ops/gemm_op_a4w4.py Resolved
amd-ruitang3
amd-ruitang3 previously approved these changes Sep 15, 2026
@zufayu
zufayu requested a review from yifehuan September 17, 2026 05:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Critical ABI, fake-argument, exception-handling, and device-validation issues remain unresolved.

Get a fresh assessment by requesting another Copilot review.

Review details

Suppressed comments (4)

aiter/ops/gemm_op_a4w4.py:547

  • [verified] develop=True converts Python tensors to the aiter_tensor_t registered by module_aiter_core, but module_gemm_a4w4_blockscale is missing torch_exclude in optCompilerConfig.json, so the build defaults it to false. That compiles this extension with PyTorch's pybind/ABI settings while the core module is torch-free, and core.py can reject tensor calls as an ABI split. Author must set torch_exclude=True for this module and its tune companion.
    develop=True,

csrc/ck_gemm_a4w4_blockscale/include/gemm_a4w4_blockscale.h:12

  • [verified] The visibility annotations export these symbols only from whichever shared object is built, but the repository's module_gemm_a4w4_blockscale entries still use the default Python-extension build (is_python_module=True, is_standalone=False), and this PR adds no standalone compile target or script. The advertised separately consumable C++ library therefore has no reproducible build artifact for TE/JAX. Author must add or wire a standalone library build and verify its exported entry points.
// Library entry points: the caller owns the stream.
__attribute__((visibility("default"))) aiter_tensor_t&
gemm_a4w4_blockscale(aiter_tensor_t& XQ,

csrc/ck_gemm_a4w4_blockscale/include/gemm_a4w4_blockscale_common.cuh:12

  • [verified] After removing the transitive torch headers, this common header still uses std::pow without a direct <cmath> include, and the torch-free tune TU uses std::unordered_map and CHAR_BIT without direct <unordered_map>/<climits> includes. The current include graph may mask this through transitive headers, but a standalone torch-free build is not self-contained and can fail with undeclared names. Author must add each required standard header at its point of use.
#include "aiter_hip_common.h"
#include "aiter_tensor.h"

csrc/ck_gemm_a4w4_blockscale/include/gemm_a4w4_blockscale_common.cuh:152

  • [verified] This changes TORCH_CHECK to AITER_CHECK, but g_aiter_can_throw defaults to false and neither the normal nor tuning pybind wrapper enables scoped throwing. An unsupported CK argument now calls abort() instead of raising the RuntimeError the Python API previously exposed, terminating the host process on a normal invalid-shape/dtype case. Author must make these pybind entry points use a throwing check (or enable scoped throwing) while keeping the raw C++ API's error policy explicit.
    AITER_CHECK(device_gemm.IsSupportedArgument(argument), "This GEMM is not supported!");
  • Files reviewed: 9/9 changed files
  • Comments generated: 6
  • Review effort level: Lite

Comment thread aiter/ops/gemm_op_a4w4.py Resolved
Copilot AI review requested due to automatic review settings September 18, 2026 20:08
@github-actions github-actions Bot changed the title [HIP] [CK] torch-free a4w4 GEMM + C++ library build [HIP] [CK] [JIT] torch-free a4w4 GEMM + C++ library build Sep 18, 2026
@github-actions github-actions Bot added the JIT label Sep 18, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved build/export, stream/device, validation, and standalone-compilation issues block approval.

Get a fresh assessment by requesting another Copilot review.

Review details

Suppressed comments (3)

Previously missed (1) — in code that hasn't changed since the last review.

aiter/ops/gemm_op_a4w4.py:563

  • [verified] Enabling develop=True routes this op through the pybind hook, which currently pushes raw_stream(current_device()) rather than a stream for the converted tensors' device. If all operands are on cuda:1 while the current device is cuda:0, the C++ device check accepts them, then HipDeviceGuard(1) launches with a device-0 stream, causing a stream/device launch failure or incorrect ordering. Author must select the stream from the first tensor's device or require the current device to match before enabling this path.

csrc/ck_gemm_a4w4_blockscale/gemm_a4w4_blockscale_tune.cu:49

  • [verified] Casting lookup.size() to int removes the unsigned comparison that rejected negative IDs; kernelId=-1 now passes this check, misses the map, and silently falls back to kernel 0 at line 57 instead of reporting out of range. Invalid tuner/API input therefore executes a different kernel. Author must require id >= 0 as well as the upper bound.
  AITER_CHECK(id < (int)lookup.size(),
              "Kernel id ", id, " is out of range!");

csrc/ck_gemm_a4w4_blockscale/include/gemm_a4w4_blockscale_common.cuh:117

  • [verified] This validation only checks placement, but the generated kernels immediately call size(1) and stride(-2) on every operand. aiter_tensor_t::stride() does unchecked negative indexing, so a 1-D tensor reaches strides[-1] (unlike the old torch::Tensor API, which rejected the rank) and can launch with invalid metadata or crash. Author must reject non-2D operands here before entering the kernel.
        AITER_CHECK(operands[i]->is_gpu(), names[i], " must be a GPU tensor!");
  • Files reviewed: 11/11 changed files
  • Comments generated: 5
  • Review effort level: Lite

Comment thread aiter/jit/optCompilerConfig.json Resolved
Comment thread aiter/ops/gemm_op_a4w4.py Resolved
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

No activity for 15 days, so this is now labelled stale. A push or a comment clears it; keep-open exempts it.

@github-actions github-actions Bot added the stale The PR hasn't been updated in 2 weeks label Oct 7, 2026
@Micky774

Micky774 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

@amd-ruitang3 can this be merged now, or are we still waiting on an additional review?

@github-actions github-actions Bot removed the stale The PR hasn't been updated in 2 weeks label Oct 11, 2026
@amd-ruitang3
amd-ruitang3 merged commit af8d826 into main Oct 13, 2026
56 checks passed
@amd-ruitang3
amd-ruitang3 deleted the zain/torch-free/a4w4-poc branch October 13, 2026 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants