Repository navigation
[HIP] [CK] [JIT] torch-free a4w4 GEMM + C++ library build - #4124
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
|
@zufayu this PR is ready for review now |
There was a problem hiding this comment.
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_tand pass the current HIP stream into torch-free kernel APIs. - Updates the CK a4w4 blockscale GEMM kernel layer + instance generation to use
aiter_tensor_tandhipStream_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.
|
@Micky774 , Now we try to remove torch from C/C++/CU. You can refer to: |
We don't require the pybind to be torch-free since we use the generated |
Yes. We want Torch to exist only on the Python side. |
|
@amd-ruitang3 I've updated the PR -- let me know if you need to see any changes, thanks! |
There was a problem hiding this comment.
🟡 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=Trueusesmodule_aiter_core.aiter_tensor_t, whilemodule_gemm_a4w4_blockscale_tunedefaults totorch_exclude=Falseand is compiled with the PyTorch pybind11 ABI. Calls will fail to convert the marshalled tensor arguments; mark this moduletorch_exclude=TrueinoptCompilerConfig.jsonas 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 whichlookup.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
There was a problem hiding this comment.
🟡 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=Trueconverts Python tensors to theaiter_tensor_tregistered bymodule_aiter_core, butmodule_gemm_a4w4_blockscaleis missingtorch_excludeinoptCompilerConfig.json, so the build defaults it tofalse. That compiles this extension with PyTorch's pybind/ABI settings while the core module is torch-free, andcore.pycan reject tensor calls as an ABI split. Author must settorch_exclude=Truefor 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_blockscaleentries 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::powwithout a direct<cmath>include, and the torch-free tune TU usesstd::unordered_mapandCHAR_BITwithout 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_CHECKtoAITER_CHECK, butg_aiter_can_throwdefaults to false and neither the normal nor tuning pybind wrapper enables scoped throwing. An unsupported CK argument now callsabort()instead of raising theRuntimeErrorthe 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
There was a problem hiding this comment.
🟡 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=Trueroutes this op through the pybind hook, which currently pushesraw_stream(current_device())rather than a stream for the converted tensors' device. If all operands are oncuda:1while the current device iscuda:0, the C++ device check accepts them, thenHipDeviceGuard(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()tointremoves the unsigned comparison that rejected negative IDs;kernelId=-1now 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 requireid >= 0as 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)andstride(-2)on every operand.aiter_tensor_t::stride()does unchecked negative indexing, so a 1-D tensor reachesstrides[-1](unlike the oldtorch::TensorAPI, 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
|
No activity for 15 days, so this is now labelled |
|
@amd-ruitang3 can this be merged now, or are we still waiting on an additional review? |
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