-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Mistral Large 3 NVFP4 support #14485
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
dcampora
wants to merge
14
commits into
sgl-project:main
Choose a base branch
from
dcampora:dcampora/nvfp4_support
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.
+1,139
−31
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d6322e0
Support eagle
elvischenv 0c9e202
Fixes for running eagle
Linda-Stadter 0575231
Merge branch 'main' into elvis/eagle
elvischenv 1402ae4
Added w4a16 loading support.
dcampora b8b4cc6
Adding w4a4 support for compressed tensors.
dcampora c54fc52
Do not change sgl kernel.
dcampora ab9fe7a
add compressed tensors w4a4 nvfp4 moe support
elvischenv 09d4eba
Merge branch 'main' into dcampora/nvfp4_support
JustinTong0323 fdf7a4e
lint
JustinTong0323 6b67c6c
fix marlin undefined name
JustinTong0323 1c5b478
Merge branch 'main' into dcampora/nvfp4_support
JustinTong0323 c4b3399
Merge branch 'main' into dcampora/nvfp4_support
JustinTong0323 da20243
Merge branch 'main' into dcampora/nvfp4_support
JustinTong0323 854bfa5
Merge branch 'main' into dcampora/nvfp4_support
Fridge003 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,68 @@ | ||
| import torch | ||
|
|
||
| from sglang.srt.utils import is_flashinfer_available | ||
|
|
||
| if is_flashinfer_available(): | ||
|
|
||
| @torch.library.custom_op( | ||
| "sglang::flashinfer_mm_fp4", | ||
| mutates_args=[], | ||
| device_types="cuda", | ||
| ) | ||
| def flashinfer_mm_fp4( | ||
| A: torch.Tensor, | ||
| B: torch.Tensor, | ||
| A_scale: torch.Tensor, | ||
| B_scale: torch.Tensor, | ||
| g_scale: torch.Tensor, | ||
| dtype: torch.dtype, | ||
| backend: str, | ||
| ) -> torch.Tensor: | ||
| from flashinfer.gemm import mm_fp4 as flashinfer_mm_fp4_ | ||
|
|
||
| return flashinfer_mm_fp4_( | ||
| A, B, A_scale, B_scale, g_scale, dtype, block_size=16, backend=backend | ||
| ) | ||
|
|
||
| @torch.library.register_fake( | ||
| "sglang::flashinfer_mm_fp4", | ||
| ) | ||
| def flashinfer_mm_fp4_fake( | ||
| A: torch.Tensor, | ||
| B: torch.Tensor, | ||
| A_scale: torch.Tensor, | ||
| B_scale: torch.Tensor, | ||
| g_scale: torch.Tensor, | ||
| dtype: torch.dtype, | ||
| backend: str, | ||
| ) -> torch.Tensor: | ||
| return torch.empty(A.shape[0], B.shape[1], dtype=dtype, device=A.device) | ||
|
|
||
|
|
||
| def flashinfer_scaled_fp4_mm( | ||
| a: torch.Tensor, | ||
| b: torch.Tensor, | ||
| block_scale_a: torch.Tensor, | ||
| block_scale_b: torch.Tensor, | ||
| alpha: torch.Tensor, | ||
| out_dtype: torch.dtype, | ||
| backend: str, | ||
| ) -> torch.Tensor: | ||
| assert a.ndim == 2 and b.ndim == 2 | ||
| assert block_scale_a.ndim == 2 and block_scale_b.ndim == 2 | ||
| assert a.stride(-1) == 1 and b.stride(-1) == 1 | ||
| assert a.shape[1] == b.shape[1] | ||
|
|
||
| if backend == "cutlass": | ||
| block_scale_a = block_scale_a.view(torch.uint8) | ||
| block_scale_b = block_scale_b.view(torch.uint8) | ||
|
|
||
| return flashinfer_mm_fp4( | ||
| a, | ||
| b.t(), | ||
| block_scale_a, | ||
| block_scale_b.t(), | ||
| alpha, | ||
| out_dtype, | ||
| backend=backend, | ||
| ) |
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 |
|---|---|---|
|
|
@@ -30,6 +30,8 @@ | |
| from sglang.srt.layers.quantization.compressed_tensors.schemes import ( | ||
| WNA16_SUPPORTED_BITS, | ||
| CompressedTensorsScheme, | ||
| CompressedTensorsW4A4Fp4, | ||
| CompressedTensorsW4A16Fp4, | ||
| CompressedTensorsW8A8Fp8, | ||
| CompressedTensorsW8A8Int8, | ||
| CompressedTensorsW8A16Fp8, | ||
|
|
@@ -42,6 +44,7 @@ | |
| ) | ||
| from sglang.srt.layers.quantization.fp8 import Fp8LinearMethod | ||
| from sglang.srt.layers.quantization.unquant import UnquantizedLinearMethod | ||
| from sglang.srt.utils import cutlass_fp4_supported | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -376,6 +379,35 @@ def _is_fp8_w8a16(self, weight_quant: BaseModel, input_quant: BaseModel) -> bool | |
| # All conditions satisfied. | ||
| return True | ||
|
|
||
| def _is_fp4a4_nvfp4( | ||
| self, weight_quant: QuantizationArgs, input_quant: QuantizationArgs | ||
| ): | ||
| if weight_quant is None or input_quant is None: | ||
| return False | ||
|
|
||
| is_tensor_group_quant = ( | ||
| weight_quant.strategy == QuantizationStrategy.TENSOR_GROUP.value | ||
| and input_quant.strategy == QuantizationStrategy.TENSOR_GROUP.value | ||
| ) | ||
| is_symmetric = weight_quant.symmetric and input_quant.symmetric | ||
|
|
||
| is_group_size_16 = ( | ||
| weight_quant.group_size == 16 and input_quant.group_size == 16 | ||
| ) | ||
| is_float_type = ( | ||
| weight_quant.type == QuantizationType.FLOAT | ||
| and input_quant.type == QuantizationType.FLOAT | ||
| ) | ||
| is_4_bits = weight_quant.num_bits == 4 and input_quant.num_bits == 4 | ||
|
|
||
| return ( | ||
| is_tensor_group_quant | ||
| and is_float_type | ||
| and is_4_bits | ||
| and is_group_size_16 | ||
| and is_symmetric | ||
| ) | ||
|
|
||
| def _is_wNa16_group_channel( | ||
| self, weight_quant: BaseModel, input_quant: BaseModel | ||
| ) -> bool: | ||
|
|
@@ -389,10 +421,35 @@ def _is_wNa16_group_channel( | |
|
|
||
| return is_channel_group and input_quant_none and is_symmetric and is_static | ||
|
|
||
| def _is_fp4a16_nvfp4( | ||
| self, weight_quant: QuantizationArgs, input_quant: QuantizationArgs | ||
| ): | ||
| is_weight_only = weight_quant is not None and input_quant is None | ||
| is_tensor_group_quant = ( | ||
| weight_quant.strategy == QuantizationStrategy.TENSOR_GROUP.value | ||
| ) | ||
| is_symmetric = weight_quant.symmetric | ||
|
|
||
| is_group_size_16 = weight_quant.group_size == 16 | ||
| is_float_type = weight_quant.type == QuantizationType.FLOAT | ||
| is_4_bits = weight_quant.num_bits == 4 | ||
|
|
||
| return ( | ||
| is_weight_only | ||
| and is_tensor_group_quant | ||
| and is_float_type | ||
| and is_4_bits | ||
| and is_group_size_16 | ||
| and is_symmetric | ||
| ) | ||
|
|
||
| def _get_scheme_from_parts( | ||
| self, weight_quant: BaseModel, input_quant: BaseModel | ||
| ) -> CompressedTensorsScheme: | ||
|
|
||
| if self._is_fp4a16_nvfp4(weight_quant, input_quant): | ||
| return CompressedTensorsW4A16Fp4() | ||
|
|
||
| # Detect If Mixed Precision | ||
| if self._is_wNa16_group_channel(weight_quant, input_quant): | ||
| if ( | ||
|
|
@@ -411,6 +468,16 @@ def _get_scheme_from_parts( | |
| ) | ||
|
|
||
| if is_activation_quantization_format(self.quant_format): | ||
| if self._is_fp4a4_nvfp4(weight_quant, input_quant): | ||
| if cutlass_fp4_supported(): | ||
|
Contributor
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. w4a4 supports both flashinfer and cutlass, right? I think we should do something similar to the below method, check the capability. |
||
| return CompressedTensorsW4A4Fp4() | ||
| else: | ||
| logger.warning_once( | ||
| "Current platform does not support cutlass NVFP4." | ||
| " Running CompressedTensorsW4A16Fp4." | ||
| ) | ||
| return CompressedTensorsW4A16Fp4(has_input_global_scale=True) | ||
|
|
||
| if self._is_fp8_w8a8(weight_quant, input_quant): | ||
| is_fp8_w8a8_supported = self._check_scheme_supported( | ||
| CompressedTensorsW8A8Fp8.get_min_capability(), error=False | ||
|
|
||
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.
This is a mm op, why put under attention layer?