Skip to content
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
7ce6386
Add online NVFP4 -> MXFP4 requantization (quark_mxfp4)
ColinZ22 Jun 9, 2026
4fbf291
experts fusion and config loading fixes
ColinZ22 Jun 10, 2026
5b6bcf1
Add doc changes
ColinZ22 Jun 10, 2026
67d3943
merge main
ColinZ22 Jun 15, 2026
fd61676
Fixed comments, variable naming, and updated quark NVFP4 detection lo…
ColinZ22 Jun 17, 2026
5eeb8bf
removed meta device usage for weight creation
ColinZ22 Jun 17, 2026
73d3839
refactor to use can_fuse_shared_expert
ColinZ22 Jun 17, 2026
4a8a276
add deepseek R1 test
ColinZ22 Jun 18, 2026
f909735
cleanup
ColinZ22 Jun 25, 2026
779f554
fixed lint
ColinZ22 Jun 25, 2026
19edee6
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jun 26, 2026
ca9f41a
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jun 29, 2026
8f412a2
add extra_buffer fix for rocm
ColinZ22 Jun 30, 2026
30fa773
fixed ci test memory check
ColinZ22 Jul 1, 2026
6614cfb
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jul 6, 2026
8fe55b9
moved deepseek_r1 test to local, skip in ci
ColinZ22 Jul 8, 2026
fdb2314
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jul 8, 2026
d29b08e
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jul 14, 2026
29af4e8
Mixed precision support
ColinZ22 Jul 14, 2026
5677d5d
Remove ROCm mamba extra_buffer fallback gate
ColinZ22 Jul 15, 2026
d587d64
Merge remote-tracking branch 'origin/main' into online-nvfp4-to-mxfp4…
ColinZ22 Jul 15, 2026
648e68f
fixed load time server arg override
ColinZ22 Jul 15, 2026
e76a519
fixed TP=8 padding issue
ColinZ22 Jul 16, 2026
da064f7
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jul 16, 2026
cc4134d
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jul 16, 2026
9f60770
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jul 17, 2026
80b4a7b
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jul 17, 2026
27984d0
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jul 17, 2026
fcb81be
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jul 22, 2026
cefbbfc
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jul 22, 2026
8fef995
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jul 22, 2026
cba45fb
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jul 23, 2026
8e00c35
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Jul 31, 2026
b0140cc
fix merge
ColinZ22 Aug 3, 2026
0ae5396
dynamic vs static FP8 act scale fix
ColinZ22 Aug 3, 2026
7d44c77
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Aug 4, 2026
3725894
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Aug 5, 2026
e9b918f
update moe load
ColinZ22 Aug 6, 2026
bd17aaa
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Aug 6, 2026
13db513
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Aug 6, 2026
856315a
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Aug 6, 2026
d08f163
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Aug 7, 2026
70c21a0
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Aug 7, 2026
af08845
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Aug 7, 2026
87b556d
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Aug 7, 2026
e7904a0
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Aug 7, 2026
f396bce
remove dup def
ColinZ22 Aug 10, 2026
069bd38
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Aug 12, 2026
a55a9c7
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Aug 13, 2026
d40285a
Merge branch 'main' into online-nvfp4-to-mxfp4-convert
ColinZ22 Aug 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions docs/docs/advanced_features/quantization.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ Other layers (e.g. projections in the attention layers) have their weights quant

### `quark_mxfp4` online quantization method

SGLang running on AMD GPUs with hardware FP4 support (CDNA4 architecture, e.g. MI355x) supports the quantization method `--quantization quark_mxfp4`, that will quantize BF16 model weights to MXFP4 at load time, use dynamic MXFP4 quantization for activations and MXFP4 GEMMs instead of BF16 GEMMs.
SGLang running on AMD GPUs with hardware FP4 support (CDNA4 architecture, e.g. MI355x) supports the quantization method `--quantization quark_mxfp4`, that will quantize BF16 or NVFP4 model weights to MXFP4 at load time, use dynamic MXFP4 quantization for activations and MXFP4 GEMMs instead of BF16 GEMMs.

Example (BF16 to MXFP4 requantization):

Expand All @@ -891,7 +891,24 @@ sglang serve --model-path Qwen/Qwen3-30B-A3B \
--quantization quark_mxfp4
```

The option `--quantization quark_mxfp4` also supports converting FP8 dense and MOE models to MXFP4, following this logic:
#### Online NVFP4 to MXFP4 Requantization

The option `--quantization quark_mxfp4` supports converting NVFP4 checkpoints (e.g. `nvidia/Kimi-K2.6-NVFP4`) to MXFP4 at load time to allow efficient inference using supported AMD hardware (gfx95x+):

- The quantization metadata of the source NVFP4 checkpoint is read from either `config.json` (`quantization_config`) or a standalone `hf_quant_config.json`;
- Producer-declared excluded modules will remain in higher precision;
- NVFP4 checkpoints with mixed precision (`"quant_algo": "MIXED_PRECISION"`, e.g. `nvidia/Qwen3.5-397B-A17B-NVFP4-V2`) are also supported.

Example (NVFP4 to MXFP4 requantization):

```bash
sglang serve --model-path nvidia/Kimi-K2.6-NVFP4 \
--tensor-parallel-size 4 \
--quantization quark_mxfp4 \
```
#### Online FP8 to MXFP4 Requantization

The option `--quantization quark_mxfp4` supports converting FP8 dense and MOE models to MXFP4, following this logic:

1. Load an FP8 weight tensor,
2. Dequantize it to BF16,
Expand Down
28 changes: 18 additions & 10 deletions python/sglang/srt/configs/model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ def get_num_indexer_layers(config) -> int:
return getattr(config, "num_indexer_layers", 0)


REQUANTIZATION_METHODS = ["quark_mxfp4"]


Comment thread
ColinZ22 marked this conversation as resolved.
Resolved
class ModelConfig:
def __init__(
self,
Expand Down Expand Up @@ -1475,16 +1478,21 @@ def _verify_quantization(self) -> None:
and self.quantization == "nvfp4_online"
and quant_method == "modelopt_fp4"
)
# Detect which checkpoint is it
if not preserve_online_draft_quantization:
for _, method in QUANTIZATION_METHODS.items():
quantization_override = method.override_quantization_method(
quant_cfg, self.quantization
)
if quantization_override:
quant_method = quantization_override
self.quantization = quantization_override
break
# An explicit online-requantization request (e.g. quark_mxfp4 on top
# of an NVFP4/mixed checkpoint) must not be overridden back to the
# source format
if self.quantization not in REQUANTIZATION_METHODS:

# Detect which checkpoint is it
if not preserve_online_draft_quantization:
for _, method in QUANTIZATION_METHODS.items():
quantization_override = method.override_quantization_method(
quant_cfg, self.quantization
)
if quantization_override:
quant_method = quantization_override
self.quantization = quantization_override
break

# Verify quantization configurations.
if self.quantization is None:
Expand Down
20 changes: 18 additions & 2 deletions python/sglang/srt/layers/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ class LinearBase(torch.nn.Module):
quant_config: Quantization configure.
"""

# Set by quant methods that attach a per-layer scheme (e.g. Quark) inside
# get_quant_method(), which runs before create_weights() picks the loader.
scheme = None

def __init__(
self,
input_size: int,
Expand Down Expand Up @@ -366,7 +370,13 @@ def __init__(
skip_block_quant_check=skip_block_quant_check,
weight_loader=(
self.weight_loader_v2
if self.quant_method.__class__.__name__ in WEIGHT_LOADER_V2_SUPPORTED
if (
self.quant_method.__class__.__name__ in WEIGHT_LOADER_V2_SUPPORTED
or (
self.scheme is not None
and self.scheme.requires_weight_loader_v2
)
)
else self.weight_loader
),
)
Expand Down Expand Up @@ -1450,7 +1460,13 @@ def __init__(
params_dtype=self.params_dtype,
weight_loader=(
self.weight_loader_v2
if self.quant_method.__class__.__name__ in WEIGHT_LOADER_V2_SUPPORTED
if (
self.quant_method.__class__.__name__ in WEIGHT_LOADER_V2_SUPPORTED
or (
self.scheme is not None
and self.scheme.requires_weight_loader_v2
)
)
else self.weight_loader
),
)
Expand Down
8 changes: 8 additions & 0 deletions python/sglang/srt/layers/quantization/base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ def _modelopt_override_quantization_method(
if hf_quant_config is None:
return None

# If the user explicitly requested an online requantization (e.g.
# quark_mxfp4 on top of an NVFP4 checkpoint), do not override it back
# to the source format.
from sglang.srt.configs.model_config import REQUANTIZATION_METHODS

if user_quant in REQUANTIZATION_METHODS:
return None

# Check if this is a ModelOpt config
quant_algo = hf_quant_config.get("quant_algo", "").upper()

Expand Down
6 changes: 6 additions & 0 deletions python/sglang/srt/layers/quantization/base_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class BaseLinearScheme(ABC):
of different quantization schemes.
"""

# Schemes whose parameters only implement the v2 loader API
# (load_{column,row,merged_column,qkv}_weight) set this so LinearBase
# routes them through weight_loader_v2 without flipping the loader for
# every scheme that shares the same LinearMethod class.
requires_weight_loader_v2: bool = False

@abstractmethod
def create_weights(self, *args, **kwargs):
"""
Expand Down
53 changes: 53 additions & 0 deletions python/sglang/srt/layers/quantization/dequantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Utilities to manage the dequantization of weights.
"""

from typing import Optional

import torch

from sglang.srt.layers.quantization.fp8_utils import (
Expand All @@ -10,6 +12,29 @@
)
from sglang.srt.utils import set_weight_attrs

NVFP4_BLOCK_SIZE = 16
_FP4_E2M1_LUT = torch.tensor(
[
0.0,
0.5,
1.0,
1.5,
2.0,
3.0,
4.0,
6.0,
-0.0,
-0.5,
-1.0,
-1.5,
-2.0,
-3.0,
-4.0,
-6.0,
],
dtype=torch.float32,
)


def copy_missing_attrs(old: torch.Tensor, new: torch.Tensor) -> None:
"""Copies any attrs present in `old` but not in `new` to `new`"""
Expand Down Expand Up @@ -42,3 +67,31 @@ def dequantize_fp8(
)

return w_dequant


def dequantize_nvfp4(
w_q: torch.Tensor,
w_s: torch.Tensor,
w_s2: Optional[torch.Tensor],
out_dtype: torch.dtype = torch.bfloat16,
) -> torch.Tensor:
"""NVFP4 -> ``out_dtype``. ``w_q``: uint8 [..., out, in/2] packed e2m1
(low nibble = even idx). ``w_s``: fp8 e4m3 [..., out, in/16] per-block.
``w_s2``: optional fp32 per-tensor scalar that multiplies the per-block
scale (ModelOpt / AMD Quark NVFP4)."""
device = w_q.device
*batch, out_dim, half_in = w_q.shape
in_dim = half_in * 2

low = (w_q & 0xF).to(torch.int64)
high = (w_q >> 4).to(torch.int64)
lut = _FP4_E2M1_LUT.to(device=device, dtype=torch.float32)
deq = torch.empty(*batch, out_dim, in_dim, dtype=torch.float32, device=device)
deq[..., 0::2] = lut[low]
deq[..., 1::2] = lut[high]

scale = w_s.to(torch.float32)
if w_s2 is not None:
scale = scale * w_s2.to(torch.float32)
scale = scale.repeat_interleave(NVFP4_BLOCK_SIZE, dim=-1)
return (deq * scale).to(out_dtype)
Loading
Loading