-
Notifications
You must be signed in to change notification settings - Fork 86
feat(musa): add native deterministic gemm kernel #395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Arlo-mt
wants to merge
1
commit into
RL-Align:main
Choose a base branch
from
Arlo-mt:musa-support-native-gemm_kernel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright (c) 2026 RL-Kernel Contributors | ||
|
|
||
| #include <musa_runtime.h> | ||
| #include <torch/extension.h> | ||
| #include <torch_musa/csrc/aten/musa/Exceptions.h> | ||
| #include <torch_musa/csrc/aten/musa/MUSAContext.h> | ||
|
|
||
| namespace { | ||
|
|
||
| constexpr int kBlockSize = 256; | ||
|
|
||
| template < | ||
| typename input_t, | ||
| typename output_t, | ||
| bool TransposeA, | ||
| bool TransposeB, | ||
| bool TransposeOutput> | ||
| __global__ void det_gemm_kernel( | ||
| const input_t* a, | ||
| const input_t* b, | ||
| output_t* c, | ||
| int m, | ||
| int n, | ||
| int k) { | ||
| const int index = blockIdx.x * blockDim.x + threadIdx.x; | ||
| if (index >= m * n) { | ||
| return; | ||
| } | ||
| const int row = index / n; | ||
| const int column = index % n; | ||
| float accumulator = 0.0f; | ||
| for (int inner = 0; inner < k; ++inner) { | ||
| const int a_index = TransposeA ? inner * m + row : row * k + inner; | ||
| const int b_index = TransposeB ? column * k + inner : inner * n + column; | ||
| accumulator += | ||
| static_cast<float>(a[a_index]) * static_cast<float>(b[b_index]); | ||
| } | ||
| const int output_index = TransposeOutput ? column * m + row : index; | ||
| c[output_index] = static_cast<output_t>(accumulator); | ||
| } | ||
|
|
||
| template <typename input_t, typename output_t> | ||
| void launch( | ||
| torch::Tensor a, | ||
| torch::Tensor b, | ||
| torch::Tensor output, | ||
| int m, | ||
| int n, | ||
| int k, | ||
| bool transpose_a, | ||
| bool transpose_b, | ||
| bool transpose_output) { | ||
| const int blocks = (m * n + kBlockSize - 1) / kBlockSize; | ||
| auto stream = at::musa::getCurrentMUSAStream(); | ||
| if (transpose_a) { | ||
| if (transpose_output) { | ||
| det_gemm_kernel<input_t, output_t, true, false, true> | ||
| <<<blocks, kBlockSize, 0, stream>>>( | ||
| a.data_ptr<input_t>(), | ||
| b.data_ptr<input_t>(), | ||
| output.data_ptr<output_t>(), | ||
| m, | ||
| n, | ||
| k); | ||
| } else { | ||
| det_gemm_kernel<input_t, output_t, true, false, false> | ||
| <<<blocks, kBlockSize, 0, stream>>>( | ||
| a.data_ptr<input_t>(), | ||
| b.data_ptr<input_t>(), | ||
| output.data_ptr<output_t>(), | ||
| m, | ||
| n, | ||
| k); | ||
| } | ||
| } else if (transpose_b) { | ||
| det_gemm_kernel<input_t, output_t, false, true, false> | ||
| <<<blocks, kBlockSize, 0, stream>>>( | ||
| a.data_ptr<input_t>(), | ||
| b.data_ptr<input_t>(), | ||
| output.data_ptr<output_t>(), | ||
| m, | ||
| n, | ||
| k); | ||
| } else { | ||
| det_gemm_kernel<input_t, output_t, false, false, false> | ||
| <<<blocks, kBlockSize, 0, stream>>>( | ||
| a.data_ptr<input_t>(), | ||
| b.data_ptr<input_t>(), | ||
| output.data_ptr<output_t>(), | ||
| m, | ||
| n, | ||
| k); | ||
| } | ||
| } | ||
|
|
||
| void check_inputs(torch::Tensor a, torch::Tensor b) { | ||
| TORCH_CHECK( | ||
| a.device().type() == c10::kPrivateUse1 && | ||
| b.device().type() == c10::kPrivateUse1, | ||
| "det_gemm requires MUSA tensors"); | ||
| TORCH_CHECK(a.device() == b.device(), "det_gemm tensors must share a device"); | ||
| TORCH_CHECK(a.dim() == 2 && b.dim() == 2, "det_gemm expects 2-D tensors"); | ||
| TORCH_CHECK(a.scalar_type() == b.scalar_type(), "det_gemm dtypes must match"); | ||
| TORCH_CHECK( | ||
| a.scalar_type() == at::ScalarType::BFloat16, | ||
| "MUSA det_gemm currently supports bfloat16 inputs"); | ||
| } | ||
|
|
||
| torch::Tensor dispatch( | ||
| torch::Tensor a, | ||
| torch::Tensor b, | ||
| int m, | ||
| int n, | ||
| int k, | ||
| bool transpose_a, | ||
| bool transpose_b, | ||
| bool transpose_output, | ||
| bool output_fp32) { | ||
| a = a.contiguous(); | ||
| b = b.contiguous(); | ||
| auto options = output_fp32 ? a.options().dtype(torch::kFloat) : a.options(); | ||
| auto output = torch::empty( | ||
| transpose_output ? std::vector<int64_t>{n, m} | ||
| : std::vector<int64_t>{m, n}, | ||
| options); | ||
| if (m == 0 || n == 0) { | ||
| return output; | ||
| } | ||
| if (output_fp32) { | ||
| launch<c10::BFloat16, float>( | ||
| a, b, output, m, n, k, transpose_a, transpose_b, transpose_output); | ||
| } else { | ||
| launch<c10::BFloat16, c10::BFloat16>( | ||
| a, b, output, m, n, k, transpose_a, transpose_b, transpose_output); | ||
| } | ||
| C10_MUSA_KERNEL_LAUNCH_CHECK(); | ||
| return output; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| torch::Tensor det_gemm_fwd(torch::Tensor a, torch::Tensor b) { | ||
| check_inputs(a, b); | ||
| TORCH_CHECK(b.size(0) == a.size(1), "det_gemm_fwd: K mismatch"); | ||
| return dispatch(a, b, a.size(0), b.size(1), a.size(1), false, false, false, false); | ||
| } | ||
|
|
||
| torch::Tensor det_gemm_fwd_fp32(torch::Tensor a, torch::Tensor b) { | ||
| check_inputs(a, b); | ||
| TORCH_CHECK(b.size(0) == a.size(1), "det_gemm_fwd_fp32: K mismatch"); | ||
| return dispatch(a, b, a.size(0), b.size(1), a.size(1), false, false, false, true); | ||
| } | ||
|
|
||
| torch::Tensor det_gemm_fwd_rhs_transposed(torch::Tensor a, torch::Tensor bt) { | ||
| check_inputs(a, bt); | ||
| TORCH_CHECK(bt.size(1) == a.size(1), "det_gemm_fwd_rhs_transposed: K mismatch"); | ||
| return dispatch(a, bt, a.size(0), bt.size(0), a.size(1), false, true, false, false); | ||
| } | ||
|
|
||
| torch::Tensor det_gemm_da(torch::Tensor dc, torch::Tensor b) { | ||
| check_inputs(dc, b); | ||
| TORCH_CHECK(b.size(1) == dc.size(1), "det_gemm_da: N mismatch"); | ||
| return dispatch(dc, b, dc.size(0), b.size(0), dc.size(1), false, true, false, false); | ||
| } | ||
|
|
||
| torch::Tensor det_gemm_db(torch::Tensor a, torch::Tensor dc) { | ||
| check_inputs(a, dc); | ||
| TORCH_CHECK(a.size(0) == dc.size(0), "det_gemm_db: M mismatch"); | ||
| return dispatch(a, dc, a.size(1), dc.size(1), a.size(0), true, false, false, false); | ||
| } | ||
|
|
||
| torch::Tensor det_gemm_db_transposed(torch::Tensor a, torch::Tensor dc) { | ||
| check_inputs(a, dc); | ||
| TORCH_CHECK(a.size(0) == dc.size(0), "det_gemm_db_transposed: M mismatch"); | ||
| return dispatch(a, dc, a.size(1), dc.size(1), a.size(0), true, false, true, false); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright (c) 2026 RL-Kernel Contributors | ||
|
|
||
| #include <torch/extension.h> | ||
|
|
||
| torch::Tensor det_gemm_fwd(torch::Tensor a, torch::Tensor b); | ||
| torch::Tensor det_gemm_fwd_fp32(torch::Tensor a, torch::Tensor b); | ||
| torch::Tensor det_gemm_fwd_rhs_transposed(torch::Tensor a, torch::Tensor bt); | ||
| torch::Tensor det_gemm_da(torch::Tensor dc, torch::Tensor b); | ||
| torch::Tensor det_gemm_db(torch::Tensor a, torch::Tensor dc); | ||
| torch::Tensor det_gemm_db_transposed(torch::Tensor a, torch::Tensor dc); | ||
|
|
||
| PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { | ||
| m.def("det_gemm_fwd", &det_gemm_fwd); | ||
| m.def("det_gemm_fwd_fp32", &det_gemm_fwd_fp32); | ||
| m.def("det_gemm_fwd_rhs_transposed", &det_gemm_fwd_rhs_transposed); | ||
| m.def("det_gemm_da", &det_gemm_da); | ||
| m.def("det_gemm_db", &det_gemm_db); | ||
| m.def("det_gemm_db_transposed", &det_gemm_db_transposed); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from .det_gemm import MusaDetGemmOp | ||
|
|
||
| __all__ = ["MusaDetGemmOp"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # Copyright (c) 2026 RL-Kernel Contributors | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import torch | ||
|
|
||
| from rl_engine.kernels.ops.base import _C, _EXT_AVAILABLE | ||
|
|
||
|
|
||
| class _MusaDetGemmFunction(torch.autograd.Function): | ||
| @staticmethod | ||
| def forward(ctx, a: torch.Tensor, b: torch.Tensor, output_fp32: bool): | ||
| ctx.save_for_backward(a, b) | ||
| ctx.output_fp32 = bool(output_fp32) | ||
| if ctx.output_fp32: | ||
| return _C.det_gemm_fwd_fp32(a, b) | ||
| return _C.det_gemm_fwd(a, b) | ||
|
|
||
| @staticmethod | ||
| def backward(ctx, grad_output: torch.Tensor): | ||
| a, b = ctx.saved_tensors | ||
| grad_a = _C.det_gemm_da(grad_output, b) if ctx.needs_input_grad[0] else None | ||
| grad_b = _C.det_gemm_db(a, grad_output) if ctx.needs_input_grad[1] else None | ||
| return grad_a, grad_b, None | ||
|
|
||
|
|
||
| class _MusaDetLinearFunction(torch.autograd.Function): | ||
| @staticmethod | ||
| def forward(ctx, a: torch.Tensor, weight: torch.Tensor): | ||
| ctx.save_for_backward(a, weight) | ||
| return _C.det_gemm_fwd_rhs_transposed(a, weight) | ||
|
|
||
| @staticmethod | ||
| def backward(ctx, grad_output: torch.Tensor): | ||
| a, weight = ctx.saved_tensors | ||
| grad_a = _C.det_gemm_fwd(grad_output, weight) if ctx.needs_input_grad[0] else None | ||
| grad_weight = _C.det_gemm_db_transposed(a, grad_output) if ctx.needs_input_grad[1] else None | ||
| return grad_a, grad_weight | ||
|
|
||
|
|
||
| class MusaDetGemmOp: | ||
| """Deterministic fixed-order GEMM for MUSA BF16 tensors.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| if not _EXT_AVAILABLE or _C is None: | ||
| raise RuntimeError("MUSA det_gemm requires the compiled extension") | ||
| required = ( | ||
| "det_gemm_fwd", | ||
| "det_gemm_fwd_fp32", | ||
| "det_gemm_fwd_rhs_transposed", | ||
| "det_gemm_da", | ||
| "det_gemm_db", | ||
| "det_gemm_db_transposed", | ||
| ) | ||
| missing = [name for name in required if not hasattr(_C, name)] | ||
| if missing: | ||
| raise RuntimeError(f"MUSA det_gemm extension is missing: {', '.join(missing)}") | ||
|
|
||
| @staticmethod | ||
| def _check(a: torch.Tensor, b: torch.Tensor) -> None: | ||
| if a.device.type != "musa" or b.device.type != "musa": | ||
| raise ValueError("MUSA det_gemm requires MUSA tensors") | ||
| if a.dtype != torch.bfloat16 or b.dtype != torch.bfloat16: | ||
| raise TypeError("MUSA det_gemm currently supports BF16 tensors") | ||
|
|
||
| def __call__(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor: | ||
| self._check(a, b) | ||
| if a.ndim != 2 or b.ndim != 2 or a.size(1) != b.size(0): | ||
| raise ValueError("det_gemm expects A[M,K] and B[K,N]") | ||
| return _MusaDetGemmFunction.apply(a.contiguous(), b.contiguous(), False) | ||
|
|
||
| def forward_fp32(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor: | ||
| self._check(a, b) | ||
| if a.ndim != 2 or b.ndim != 2 or a.size(1) != b.size(0): | ||
| raise ValueError("det_gemm expects A[M,K] and B[K,N]") | ||
| return _MusaDetGemmFunction.apply(a.contiguous(), b.contiguous(), True) | ||
|
|
||
| forward_accum_fp32 = forward_fp32 | ||
|
|
||
| def linear(self, a: torch.Tensor, weight: torch.Tensor) -> torch.Tensor: | ||
| self._check(a, weight) | ||
| if a.ndim != 2 or weight.ndim != 2 or a.size(1) != weight.size(1): | ||
| raise ValueError("linear expects A[M,K] and weight[N,K]") | ||
| return _MusaDetLinearFunction.apply(a.contiguous(), weight.contiguous()) | ||
|
|
||
|
|
||
| def deterministic_gemm(a: torch.Tensor, b: torch.Tensor) -> torch.Tensor: | ||
| return MusaDetGemmOp()(a, b) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: RL-Align/RL-Kernel
Length of output: 246
🤖 get_repo_knowledge executed:
get_repo_knowledge RL-Align/RL-Kernel /tmp/coderabbit-repo-knowledge/rl-align-rl-kernel-ae20f626/architectureLength of output: 44958
🏁 Script executed:
Repository: RL-Align/RL-Kernel
Length of output: 27538
🏁 Script executed:
Repository: RL-Align/RL-Kernel
Length of output: 27717
🏁 Script executed:
Repository: RL-Align/RL-Kernel
Length of output: 33806
🏁 Script executed:
Repository: RL-Align/RL-Kernel
Length of output: 25361
🏁 Script executed:
Repository: RL-Align/RL-Kernel
Length of output: 9278
Cast
grad_outputto BF16 for FP32-output backward.det_gemm_fwd_fp32returns FP32, whiledet_gemm_daanddet_gemm_dbrequire BF16 inputs. The current backward path therefore raises a dtype error. Usectx.output_fp32to castgrad_outputbefore both calls, and add coverage for both gradients.🤖 Prompt for AI Agents