Repository navigation
[ROCm][MiniMax-M3.1] MXFP8 linear on AITER's tuned FlyDSL GEMM #65479
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
micah-wil
wants to merge
1
commit into
vllm-project:main
Choose a base branch
from
ROCm:micah/m31-mxfp8-flydsl
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
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 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| import os | ||
|
|
||
| import torch | ||
|
|
||
| import vllm.envs as envs | ||
| from vllm._aiter_ops import rocm_aiter_ops | ||
| from vllm.model_executor.layers.quantization.utils.mxfp8_utils import ( | ||
| MXFP8_BLOCK_SIZE, | ||
| mxfp8_e4m3_quantize, | ||
| ) | ||
| from vllm.model_executor.utils import replace_parameter | ||
| from vllm.platforms import current_platform | ||
|
|
||
| from .Mxfp8LinearKernel import Mxfp8LinearKernel, Mxfp8LinearLayerConfig | ||
|
|
||
|
|
||
| class AiterMxfp8LinearKernel(Mxfp8LinearKernel): | ||
| """MXFP8 linear on AITER's tuned FlyDSL GEMM (gfx950). | ||
|
|
||
| Weights are (16, 16)-preshuffled once at load; activation and weight | ||
| scales stay row-major E8M0, [M, K / 32] and [N, K / 32]. | ||
| """ | ||
|
|
||
| @classmethod | ||
| def is_supported( | ||
| cls, compute_capability: int | None = None | ||
| ) -> tuple[bool, str | None]: | ||
| if not current_platform.is_rocm(): | ||
| return False, "AITER is only supported on ROCm" | ||
| if not current_platform.supports_mx(): | ||
| return False, "native MX requires CDNA4 (gfx95x)" | ||
| if envs.VLLM_BATCH_INVARIANT: | ||
| return False, "tuned buckets use split K; not batch invariant" | ||
| if not rocm_aiter_ops.is_linear_enabled(): | ||
| return False, "AITER linear is disabled or not supported" | ||
| # MiniMax-M3 mono decode re-tiles the MXFP8 QKV, output and separate | ||
| # shared-expert weights from the row-major layout; only the shared | ||
| # experts check for preshuffled weights. | ||
| if os.getenv("VLLM_ROCM_MINIMAX_M3_MONO", "0") in ("1", "true", "True"): | ||
| return False, "VLLM_ROCM_MINIMAX_M3_MONO needs row-major MXFP8 weights" | ||
| return True, None | ||
|
|
||
| @classmethod | ||
| def can_implement(cls, config: Mxfp8LinearLayerConfig) -> tuple[bool, str | None]: | ||
| N, K = config.weight_shape | ||
| if config.bmm_batch_size is not None: | ||
| return False, "batched MXFP8 GEMM is not supported" | ||
| # The FlyDSL GEMM writes bf16 only. | ||
| if torch.get_default_dtype() != torch.bfloat16: | ||
| return False, "requires a bf16 model" | ||
| if not rocm_aiter_ops.is_mxfp8_bpreshuffle_gemm_tuned(N, K): | ||
| return False, f"AITER has no tuned MXFP8 config for N={N}, K={K}" | ||
| return True, None | ||
|
|
||
| def process_weights_after_loading(self, layer: torch.nn.Module) -> None: | ||
| weight = layer.weight.data | ||
| N, K = weight.shape | ||
| weight_scale = layer.weight_scale.data[:N, : K // MXFP8_BLOCK_SIZE] | ||
| replace_parameter( | ||
| layer, "weight", rocm_aiter_ops.shuffle_weight(weight.contiguous()) | ||
| ) | ||
| replace_parameter( | ||
| layer, | ||
| "weight_scale", | ||
| weight_scale.contiguous().view(torch.float8_e8m0fnu), | ||
| ) | ||
|
|
||
| def apply_weights( | ||
| self, | ||
| layer: torch.nn.Module, | ||
| x: torch.Tensor, | ||
| bias: torch.Tensor | None = None, | ||
| ) -> torch.Tensor: | ||
| out_shape = (*x.shape[:-1], layer.weight.shape[0]) | ||
| x_q, x_scale = mxfp8_e4m3_quantize(x.reshape(-1, x.shape[-1])) | ||
| out = rocm_aiter_ops.gemm_a8w8_blockscale_bpreshuffle( | ||
| x_q, | ||
| layer.weight, | ||
| x_scale.view(torch.float8_e8m0fnu), | ||
| layer.weight_scale, | ||
| x.dtype, | ||
| ) | ||
| out = out.reshape(out_shape) | ||
| if bias is not None: | ||
| out = out + bias | ||
| return out | ||
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.
I know we shouldn't have
os.getenvhere, it's because MiniMax-M3.1 mono added its own envs file https://github.com/vllm-project/vllm/blob/main/vllm/models/minimax_m3/amd/mono/envs.py, I am going to make a separate PR to clean that up