-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Llamas 3.1 405B fp4 changes upstreaming from 355_wip #25135
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
mgoin
merged 17 commits into
vllm-project:main
from
ROCm:llamas_changes_upstr_from_355_wip
Sep 26, 2025
+301
−38
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2ceeff7
405B fp4 llama 3.1 support
06d8515
405B fp4 llama 3.1 support
82aa4b2
405B fp4 llama 3.1 support
3433861
refactor ROPE base
dd7697f
refactor linear to exclude Fp8LinearMethod called with x_scale
f17dc60
refactor ROPE base
f9626ee
Ensure right fp8 is called for given arch
43bd10e
merge with main
ddcc59c
Merge branch 'main' of github.com:vllm-project/vllm into llamas_chang…
af993eb
fix for MoE kernel tests
ba529e5
merge with main
b2120ce
corrected FP4 ASM GEMM flag name
fa9950c
removed batched rope, inmproved env handling
8d3aca2
ROPE refactoring
1020f95
ROPE hip kernel remove and clean up
5a54910
Merge branch 'upstream/main' into llamas_changes_upstr_from_355_wip
4dff937
removed offsets and x scale
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
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 |
---|---|---|
@@ -1,20 +1,104 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
||
from functools import cache | ||
from typing import Any, Callable, Optional | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
|
||
from vllm.logger import init_logger | ||
from vllm import envs | ||
from vllm.model_executor.layers.quantization.quark.schemes import QuarkScheme | ||
from vllm.model_executor.layers.quantization.utils.mxfp4_utils import ( | ||
OCP_MX_BLOCK_SIZE, dequant_mxfp4, quant_dequant_mxfp4) | ||
from vllm.model_executor.parameter import (GroupQuantScaleParameter, | ||
PackedvLLMParameter) | ||
from vllm.platforms import current_platform | ||
|
||
logger = init_logger(__name__) | ||
|
||
@cache | ||
def is_rocm_aiter_fp4_asm_gemm_enabled() -> bool: | ||
return current_platform.is_rocm() \ | ||
and envs.VLLM_ROCM_USE_AITER_FP4_ASM_GEMM \ | ||
and envs.VLLM_ROCM_USE_AITER | ||
|
||
|
||
try: | ||
from aiter.ops.shuffle import shuffle_weight | ||
from aiter.ops.triton.gemm_afp4wfp4 import gemm_afp4wfp4 | ||
from aiter.ops.triton.quant import dynamic_mxfp4_quant | ||
|
||
from vllm.utils import direct_register_custom_op | ||
if is_rocm_aiter_fp4_asm_gemm_enabled(): | ||
from aiter import gemm_a4w4, per_1x32_f4_quant_hip | ||
|
||
def gemm_with_dynamic_quant( | ||
x: torch.Tensor, | ||
weight: torch.Tensor, | ||
weight_scale: torch.Tensor, | ||
rocm_use_aiter_fp4_asm_gemm: bool = False, | ||
out_dtype: Optional[torch.dtype] = torch.bfloat16, | ||
x_scales: Optional[torch.Tensor] = None, | ||
) -> torch.Tensor: | ||
M = x.shape[0] | ||
if rocm_use_aiter_fp4_asm_gemm: | ||
if x_scales is None: | ||
# use hip quant kernel for performance | ||
x_q, x_s = per_1x32_f4_quant_hip(x, shuffle=True) | ||
else: | ||
x_q = x | ||
x_s = x_scales | ||
|
||
# 32 alignment is enough for dim0 padding of output for | ||
# gemm_a4w4 kernel | ||
y = torch.empty((M + 31) // 32 * 32, | ||
weight.shape[0], | ||
device=x_q.device, | ||
dtype=out_dtype) | ||
|
||
gemm_a4w4(x_q, | ||
weight, | ||
x_s, | ||
weight_scale.view(x_s.dtype), | ||
y, | ||
bpreshuffle=True) | ||
return y[:M] | ||
else: | ||
if x_scales is None: | ||
x_q, x_s = dynamic_mxfp4_quant(x) | ||
else: | ||
x_q = x | ||
x_s = x_scales | ||
y = torch.empty(x_q.shape[0], | ||
weight.shape[0], | ||
device=x_q.device, | ||
dtype=out_dtype) | ||
|
||
gemm_afp4wfp4(x_q, weight, x_s, weight_scale.T, out_dtype, y) | ||
return y | ||
|
||
def gemm_with_dynamic_quant_fake( | ||
x: torch.Tensor, | ||
weight: torch.Tensor, | ||
weight_scale: torch.Tensor, | ||
x_scales: torch.Tensor = None, | ||
rocm_use_aiter_fp4_asm_gemm: bool = False, | ||
out_dtype: Optional[torch.dtype] = torch.bfloat16, | ||
) -> torch.Tensor: | ||
return torch.empty((*x.shape[:-1], weight.shape[0]), | ||
dtype=out_dtype, | ||
device=x.device) | ||
|
||
direct_register_custom_op( | ||
op_name="gemm_with_dynamic_quant", | ||
op_func=gemm_with_dynamic_quant, | ||
mutates_args=[], | ||
fake_impl=gemm_with_dynamic_quant_fake, | ||
dispatch_key=current_platform.dispatch_key, | ||
) | ||
|
||
except ImportError: | ||
dynamic_mxfp4_quant = gemm_afp4wfp4 = None | ||
|
||
__all__ = ["QuarkW4A4MXFP4"] | ||
|
||
|
@@ -27,29 +111,15 @@ def __init__(self, weight_quant_spec: dict[str, Any], | |
self.qscheme = "per_group" | ||
self.weight_quant_spec = weight_quant_spec | ||
self.input_quant_spec = input_quant_spec | ||
|
||
self.static_input_scales = not input_quant_spec.get("is_dynamic") | ||
|
||
if self.static_input_scales: | ||
self.emulate = not current_platform.supports_mx() | ||
self.rocm_use_aiter_fp4_asm_gemm = is_rocm_aiter_fp4_asm_gemm_enabled() | ||
if not self.emulate and (dynamic_mxfp4_quant is None | ||
or gemm_afp4wfp4 is None): | ||
# Currently need these kernels if not emulating | ||
raise NotImplementedError( | ||
"QuarkW4A4MXFP4 with static input scales is currently not " | ||
"implemented. Please open an issue.") | ||
|
||
if not current_platform.supports_mx(): | ||
self.emulate = True | ||
logger.warning_once( | ||
"The current platform does not support native MXFP4 " | ||
"computation. Simulated weight dequantization and activation " | ||
"QDQ (quantize and dequantize) will be used, with the linear " | ||
"layers computed in high precision.") | ||
else: | ||
self.emulate = True | ||
logger.warning_once( | ||
"The current platform supports native MXFP4 " | ||
"computation, but kernels are not yet integrated in vLLM. " | ||
"Simulated weight dequantization and activation " | ||
"QDQ (quantize and dequantize) will be used, with the linear " | ||
"layers computed in high precision.") | ||
f"{self.__class__.__name__} requires AITER to be installed " | ||
"for non-emulation mode! Please refer to " | ||
"https://github.com/ROCm/aiter for installation details.") | ||
|
||
@classmethod | ||
def get_min_capability(cls) -> int: | ||
|
@@ -58,8 +128,65 @@ def get_min_capability(cls) -> int: | |
def process_weights_after_loading(self, layer: torch.nn.Module) -> None: | ||
layer.weight = torch.nn.Parameter(layer.weight.data, | ||
requires_grad=False) | ||
layer.weight_scale = torch.nn.Parameter(layer.weight_scale.data, | ||
requires_grad=False) | ||
|
||
if self.emulate: | ||
layer.weight_scale = torch.nn.Parameter(layer.weight_scale.data, | ||
requires_grad=False) | ||
try: | ||
from quark.torch.export.nn.modules import realquantizer | ||
from quark.torch.quantization.config.config import ( | ||
QuantizationSpec) | ||
except ImportError as err: | ||
raise ImportError( | ||
"The package `amd-quark` is required to use AMD Quark " | ||
"MX-FP4 models. Please install it with `pip install " | ||
"amd-quark`.") from err | ||
|
||
weight_quant_spec = QuantizationSpec.from_dict( | ||
self.weight_quant_spec) | ||
|
||
weight_quantizer = realquantizer.get_real_quantizer( | ||
qspec=weight_quant_spec, | ||
quantizer=None, | ||
real_quantized=True, | ||
reorder=False, | ||
float_dtype=self.out_dtype, | ||
scale_shape=layer.weight_scale.shape, | ||
zero_point_shape=None, | ||
) | ||
weight_quantizer.scale.data = layer.weight_scale.data | ||
|
||
layer.weight = torch.nn.Parameter( | ||
weight_quantizer(layer.weight.data).to(self.out_dtype), | ||
requires_grad=False, | ||
) | ||
layer.weight_scale = None | ||
|
||
# This call is necessary to release the scales memory. | ||
torch.cuda.empty_cache() | ||
maleksan85 marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+132
to
+166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I insist that this is unnecessary https://github.com/vllm-project/vllm/pull/25135/files#r2378191214 - was not able to reopen the thread that was closed unfortunately. |
||
else: | ||
if self.rocm_use_aiter_fp4_asm_gemm: | ||
# shuffle weight scale | ||
weight_scale_shuffle = layer.weight_scale.data | ||
sm, sn = weight_scale_shuffle.shape | ||
weight_scale_shuffle = weight_scale_shuffle.view( | ||
sm // 32, 2, 16, sn // 8, 2, 4, 1) | ||
weight_scale_shuffle = weight_scale_shuffle.permute( | ||
0, 3, 5, 2, 4, 1, 6).contiguous() | ||
weight_scale_shuffle = weight_scale_shuffle.view(sm, sn) | ||
layer.weight_scale = torch.nn.Parameter(weight_scale_shuffle, | ||
requires_grad=False) | ||
|
||
# shuffle weight | ||
weight_shuffle = layer.weight.data | ||
weight_shuffle = shuffle_weight(weight_shuffle, | ||
layout=(16, 16)) | ||
layer.weight = torch.nn.Parameter(weight_shuffle, | ||
requires_grad=False) | ||
else: | ||
layer.weight_scale = torch.nn.Parameter( | ||
layer.weight_scale.data.T.contiguous(), | ||
requires_grad=False) | ||
|
||
def create_weights(self, layer: torch.nn.Module, | ||
output_partition_sizes: list[int], | ||
|
@@ -104,9 +231,9 @@ def apply_weights(self, | |
|
||
if self.emulate: | ||
dq_w = dequant_mxfp4(layer.weight, layer.weight_scale, x.dtype) | ||
|
||
x = quant_dequant_mxfp4(x) | ||
|
||
return F.linear(x, dq_w, bias) | ||
else: | ||
raise NotImplementedError() | ||
return torch.ops.vllm.gemm_with_dynamic_quant( | ||
x, layer.weight, layer.weight_scale, | ||
self.rocm_use_aiter_fp4_asm_gemm, self.out_dtype) |
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.