Repository navigation
[None][perf] Use FP8 MiniMax-M3 MSA indexer QK #17318
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
adb9ad7
[None][perf] Use FP8 MiniMax-M3 MSA indexer QK
peihu-nv 459bd9d
fix: align MiniMax M3 FP8 indexer validation on main
peihu-nv 6904b7b
[None][fix] Guard MiniMax-M3 FP8 cache writes
peihu-nv cf6ae3b
Merge branch 'main' into peihengh/m3-fp8-indexer-main
peihu-nv ac6ba83
Merge upstream main into peihengh/m3-fp8-indexer-main\n\nSigned-off-b…
peihu-nv fe02d46
[None][fix] Address MiniMax-M3 FP8 indexer review feedback
peihu-nv f00dfdc
[None][doc] Document MiniMax-M3 FP8 Torch operator contract
peihu-nv 2e4e063
[None][fix] Close remaining MiniMax-M3 review gaps
peihu-nv 7b35f38
[None][fix] Address follow-up MiniMax-M3 review comments
peihu-nv cd349b1
[None][fix] Preserve MiniMax-M3 index view semantics
peihu-nv 9a9f48a
[None][fix] Enforce MiniMax-M3 indexer dtype contracts
peihu-nv 08e17cb
[None][fix] Validate MiniMax-M3 sparse index dimensions
peihu-nv c26d6e4
Merge upstream/main into peihengh/m3-fp8-indexer-main
peihu-nv c5f2216
[None][test] Mark MiniMax-M3 epsilon validation as CPU-only
peihu-nv 72a5c2e
Merge branch 'main' into peihengh/m3-fp8-indexer-main
peihu-nv e87b679
Merge upstream/main into peihengh/m3-fp8-indexer-main
peihu-nv 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
Some comments aren't visible on the classic Files Changed page.
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,181 @@ | ||
| /* | ||
| * Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "minimaxM3Fp8IndexerKernel.h" | ||
|
|
||
| #include "tensorrt_llm/common/cudaUtils.h" | ||
| #include "tensorrt_llm/common/mathUtils.h" | ||
| #include "tensorrt_llm/common/reduceKernelUtils.cuh" | ||
|
|
||
| #include <cuda_bf16.h> | ||
| #include <cuda_fp8.h> | ||
| #include <cuda_runtime.h> | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| TRTLLM_NAMESPACE_BEGIN | ||
|
|
||
| namespace kernels | ||
| { | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| constexpr int kHeadDim = 128; | ||
| constexpr int kRotaryDim = 64; | ||
| constexpr int kElemsPerThread = kHeadDim / 32; | ||
|
|
||
| // Match the established vLLM contract exactly: the normalized/RoPE result is | ||
| // first materialized as BF16 and then cast, without an external FP8 scale. | ||
| __device__ __forceinline__ __nv_fp8_e4m3 bf16RoundedToFp8(float value) | ||
| { | ||
| return __nv_fp8_e4m3(__bfloat162float(__float2bfloat16_rn(value))); | ||
| } | ||
|
|
||
| __global__ void minimaxM3Fp8IndexerQKNormRopeKernel(__nv_bfloat16 const* qk, __nv_fp8_e4m3* q_out, | ||
| __nv_fp8_e4m3* k_cache, int const* out_cache_loc, int64_t page_stride, int64_t token_stride, int page_size, | ||
| int64_t num_pages, int num_tokens, int num_heads_q, float eps, __nv_bfloat16 const* q_weight, | ||
| __nv_bfloat16 const* k_weight, float base, int const* position_ids) | ||
| { | ||
| int const warps_per_block = blockDim.x / 32; | ||
| int const warp_id = threadIdx.x / 32; | ||
| int const lane_id = threadIdx.x % 32; | ||
| int const global_warp = blockIdx.x * warps_per_block + warp_id; | ||
| int const total_heads = num_heads_q + 1; | ||
| int const token_idx = global_warp / total_heads; | ||
| int const local_head = global_warp % total_heads; | ||
| if (token_idx >= num_tokens) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| bool const is_q = local_head < num_heads_q; | ||
| int64_t const input_offset | ||
| = (static_cast<int64_t>(token_idx) * total_heads + local_head) * kHeadDim + lane_id * kElemsPerThread; | ||
|
|
||
| uint2 const packed_input = *reinterpret_cast<uint2 const*>(qk + input_offset); | ||
| float elements[kElemsPerThread]; | ||
| float sum_squares = 0.0F; | ||
| #pragma unroll | ||
| for (int pair = 0; pair < 2; ++pair) | ||
| { | ||
| auto const values = __bfloat1622float2(reinterpret_cast<__nv_bfloat162 const*>(&packed_input)[pair]); | ||
| elements[pair * 2] = values.x; | ||
| elements[pair * 2 + 1] = values.y; | ||
| // Preserve the accumulation order used by fusedQKNormRopeKernel. A | ||
| // reassociated pair sum can move a final BF16 value across an FP8 bin. | ||
| sum_squares += values.x * values.x; | ||
| sum_squares += values.y * values.y; | ||
| } | ||
|
|
||
| sum_squares = tensorrt_llm::common::warpReduceSum(sum_squares); | ||
| float const rms_rcp = rsqrtf(sum_squares / static_cast<float>(kHeadDim) + eps); | ||
| auto const* weight = is_q ? q_weight : k_weight; | ||
| #pragma unroll | ||
| for (int i = 0; i < kElemsPerThread; ++i) | ||
| { | ||
| int const dim = lane_id * kElemsPerThread + i; | ||
| elements[i] *= rms_rcp * (1.0F + __bfloat162float(weight[dim])); | ||
| } | ||
|
|
||
| // MiniMax-M3 uses NeoX partial RoPE: rotate the first 64 of 128 channels. | ||
| // Four elements per lane means the matching half is eight lanes away. | ||
| __syncwarp(); | ||
| constexpr int kPairOffset = (kRotaryDim / 2) / kElemsPerThread; | ||
| // Keep the frequency calculation bitwise aligned with the shared fused | ||
| // QK-norm/RoPE kernel. That kernel uses the fast base-2 intrinsics rather | ||
| // than powf, and the BF16-to-FP8 contract depends on the resulting rounding. | ||
| float const neg2_log2base_over_rd = -2.0F * __log2f(base) / static_cast<float>(kRotaryDim); | ||
| #pragma unroll | ||
| for (int i = 0; i < kElemsPerThread; ++i) | ||
| { | ||
| int const dim = lane_id * kElemsPerThread + i; | ||
| float paired = __shfl_xor_sync(0xffffffff, elements[i], kPairOffset); | ||
| if (dim < kRotaryDim) | ||
| { | ||
| if (lane_id < kPairOffset) | ||
| { | ||
| paired = -paired; | ||
| } | ||
| int const dim_idx = (dim * 2) % kRotaryDim; | ||
| int const half_dim = dim_idx / 2; | ||
| float const frequency = exp2f(static_cast<float>(half_dim) * neg2_log2base_over_rd); | ||
| float sine; | ||
| float cosine; | ||
| __sincosf(static_cast<float>(position_ids[token_idx]) * frequency, &sine, &cosine); | ||
| elements[i] = elements[i] * cosine + paired * sine; | ||
| } | ||
| } | ||
| __syncwarp(); | ||
|
|
||
| uint32_t packed_output = 0; | ||
| auto* fp8_values = reinterpret_cast<__nv_fp8_e4m3*>(&packed_output); | ||
| #pragma unroll | ||
| for (int i = 0; i < kElemsPerThread; ++i) | ||
| { | ||
| fp8_values[i] = bf16RoundedToFp8(elements[i]); | ||
| } | ||
|
|
||
| __nv_fp8_e4m3* output; | ||
| if (is_q) | ||
| { | ||
| int64_t const output_offset | ||
| = (static_cast<int64_t>(token_idx) * num_heads_q + local_head) * kHeadDim + lane_id * kElemsPerThread; | ||
| output = q_out + output_offset; | ||
| } | ||
| else | ||
| { | ||
| int const slot = out_cache_loc[token_idx]; | ||
| if (slot < 0) | ||
| { | ||
| return; | ||
| } | ||
| int const page = slot / page_size; | ||
| if (page >= num_pages) | ||
| { | ||
| return; | ||
| } | ||
| int const within_page = slot % page_size; | ||
| output = k_cache + static_cast<int64_t>(page) * page_stride + static_cast<int64_t>(within_page) * token_stride | ||
| + lane_id * kElemsPerThread; | ||
| } | ||
| *reinterpret_cast<uint32_t*>(output) = packed_output; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| void launchMinimaxM3Fp8IndexerQKNormRope(void const* qk, void* q_out, void* k_cache, int const* out_cache_loc, | ||
| int64_t page_stride, int64_t token_stride, int page_size, int64_t num_pages, int num_tokens, int num_heads_q, | ||
| int head_dim, int rotary_dim, float eps, void const* q_weight, void const* k_weight, float base, | ||
| int const* position_ids, cudaStream_t stream) | ||
| { | ||
| TLLM_CHECK_WITH_INFO(head_dim == kHeadDim, "MiniMax-M3 FP8 indexer requires head_dim=128"); | ||
| TLLM_CHECK_WITH_INFO(rotary_dim == kRotaryDim, "MiniMax-M3 FP8 indexer requires rotary_dim=64"); | ||
| TLLM_CHECK_WITH_INFO(num_heads_q > 0, "MiniMax-M3 FP8 indexer requires at least one query head"); | ||
|
|
||
| constexpr int kBlockSize = 256; | ||
| constexpr int kWarpsPerBlock = kBlockSize / 32; | ||
| int const total_warps = num_tokens * (num_heads_q + 1); | ||
| int const grid_size = common::divUp(total_warps, kWarpsPerBlock); | ||
| minimaxM3Fp8IndexerQKNormRopeKernel<<<grid_size, kBlockSize, 0, stream>>>(static_cast<__nv_bfloat16 const*>(qk), | ||
| static_cast<__nv_fp8_e4m3*>(q_out), static_cast<__nv_fp8_e4m3*>(k_cache), out_cache_loc, page_stride, | ||
| token_stride, page_size, num_pages, num_tokens, num_heads_q, eps, static_cast<__nv_bfloat16 const*>(q_weight), | ||
| static_cast<__nv_bfloat16 const*>(k_weight), base, position_ids); | ||
| } | ||
|
|
||
| } // namespace kernels | ||
|
|
||
| TRTLLM_NAMESPACE_END | ||
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,41 @@ | ||
| /* | ||
| * Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "tensorrt_llm/common/config.h" | ||
|
|
||
| #include <cstdint> | ||
| #include <cuda_runtime.h> | ||
|
|
||
| TRTLLM_NAMESPACE_BEGIN | ||
|
|
||
| namespace kernels | ||
| { | ||
|
|
||
| // MiniMax-M3-specific index-branch producer. It applies Gemma RMSNorm and | ||
| // NeoX partial RoPE to a packed BF16 [index-Q | index-K] projection, writes | ||
| // index-Q as unscaled E4M3, and inserts index-K directly into the paged E4M3 | ||
| // HND cache. The direct cache store removes the standalone cast/scatter launch | ||
| // from the decode graph. | ||
| void launchMinimaxM3Fp8IndexerQKNormRope(void const* qk, void* q_out, void* k_cache, int const* out_cache_loc, | ||
| int64_t page_stride, int64_t token_stride, int page_size, int64_t num_pages, int num_tokens, int num_heads_q, | ||
| int head_dim, int rotary_dim, float eps, void const* q_weight, void const* k_weight, float base, | ||
| int const* position_ids, cudaStream_t stream); | ||
|
|
||
| } // namespace kernels | ||
|
|
||
| TRTLLM_NAMESPACE_END |
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
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 @@ | ||
| /* | ||
| * Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "tensorrt_llm/kernels/minimaxM3Fp8IndexerKernel.h" | ||
| #include "tensorrt_llm/thop/thUtils.h" | ||
|
|
||
| #include <ATen/cuda/CUDAContext.h> | ||
| #include <torch/extension.h> | ||
|
|
||
| TRTLLM_NAMESPACE_BEGIN | ||
|
|
||
| namespace torch_ext | ||
| { | ||
|
|
||
| torch::Tensor minimaxM3Fp8IndexerQKNormRope(torch::Tensor const& qk, torch::Tensor& indexKCache, | ||
| torch::Tensor const& outCacheLoc, int64_t numHeadsQ, int64_t headDim, int64_t rotaryDim, double eps, | ||
| torch::Tensor const& qWeight, torch::Tensor const& kWeight, double base, torch::Tensor const& positionIds) | ||
| { | ||
| TORCH_CHECK(qk.dim() == 2, "Index QK must be [num_tokens, (num_heads_q + 1) * head_dim]"); | ||
| TORCH_CHECK(indexKCache.dim() == 4 && indexKCache.size(1) == 1, | ||
| "Index-K cache must be HND [num_pages, 1, page_size, head_dim]"); | ||
| TORCH_CHECK(outCacheLoc.dim() == 1, "out_cache_loc must be one-dimensional"); | ||
| TORCH_CHECK(positionIds.dim() == 1, "position_ids must be one-dimensional"); | ||
| TORCH_CHECK(qWeight.dim() == 1 && kWeight.dim() == 1, "Q/K norm weights must be one-dimensional"); | ||
|
|
||
| CHECK_INPUT(qk, torch::kBFloat16); | ||
| CHECK_INPUT(outCacheLoc, torch::kInt32); | ||
| CHECK_INPUT(positionIds, torch::kInt32); | ||
| CHECK_INPUT(qWeight, torch::kBFloat16); | ||
| CHECK_INPUT(kWeight, torch::kBFloat16); | ||
| TORCH_CHECK(indexKCache.is_cuda(), "Index-K cache must be on CUDA"); | ||
| TORCH_CHECK( | ||
| indexKCache.scalar_type() == at::ScalarType::Float8_e4m3fn, "Index-K cache must use torch.float8_e4m3fn"); | ||
|
|
||
| int64_t const numTokens = qk.size(0); | ||
| TORCH_CHECK(qk.size(1) == (numHeadsQ + 1) * headDim, "Index QK width must equal (num_heads_q + 1) * head_dim"); | ||
| TORCH_CHECK(indexKCache.size(3) == headDim, "Index-K cache head dimension mismatch"); | ||
| TORCH_CHECK(indexKCache.stride(3) == 1 && indexKCache.stride(2) == headDim, | ||
| "Index-K cache must have contiguous token rows in HND layout"); | ||
| TORCH_CHECK(outCacheLoc.numel() >= numTokens, "out_cache_loc is shorter than num_tokens"); | ||
| TORCH_CHECK(positionIds.numel() == numTokens, "position_ids length must equal num_tokens"); | ||
| TORCH_CHECK(qWeight.numel() == headDim && kWeight.numel() == headDim, "Q/K norm weight width must equal head_dim"); | ||
| TORCH_CHECK(qk.get_device() == indexKCache.get_device() && qk.get_device() == outCacheLoc.get_device() | ||
| && qk.get_device() == positionIds.get_device() && qk.get_device() == qWeight.get_device() | ||
| && qk.get_device() == kWeight.get_device(), | ||
| "All MiniMax-M3 FP8 indexer tensors must be on the same CUDA device"); | ||
|
|
||
| auto const qOut = torch::empty({numTokens, numHeadsQ, headDim}, qk.options().dtype(at::ScalarType::Float8_e4m3fn)); | ||
| if (numTokens == 0) | ||
| { | ||
| return qOut; | ||
| } | ||
| auto const stream = at::cuda::getCurrentCUDAStream(qk.get_device()); | ||
| tensorrt_llm::kernels::launchMinimaxM3Fp8IndexerQKNormRope(qk.data_ptr(), qOut.data_ptr(), indexKCache.data_ptr(), | ||
| outCacheLoc.data_ptr<int>(), indexKCache.stride(0), indexKCache.stride(2), indexKCache.size(2), | ||
| indexKCache.size(0), numTokens, numHeadsQ, headDim, rotaryDim, static_cast<float>(eps), qWeight.data_ptr(), | ||
| kWeight.data_ptr(), static_cast<float>(base), positionIds.data_ptr<int>(), stream); | ||
| return qOut; | ||
| } | ||
|
|
||
| TORCH_LIBRARY_FRAGMENT(trtllm, m) | ||
| { | ||
| m.def( | ||
| "minimax_m3_fp8_indexer_qk_norm_rope(Tensor qk, Tensor(a!) index_k_cache, Tensor out_cache_loc, int " | ||
| "num_heads_q, int head_dim, int rotary_dim, float eps, Tensor q_weight, Tensor k_weight, float base, Tensor " | ||
| "position_ids) -> Tensor"); | ||
| } | ||
|
|
||
| TORCH_LIBRARY_IMPL(trtllm, CUDA, m) | ||
| { | ||
| m.impl("minimax_m3_fp8_indexer_qk_norm_rope", &minimaxM3Fp8IndexerQKNormRope); | ||
| } | ||
|
|
||
| } // namespace torch_ext | ||
|
|
||
| TRTLLM_NAMESPACE_END |
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
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.
Uh oh!
There was an error while loading. Please reload this page.