Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions tests/utils/test_mxfp_qat_vllm_ascend.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ def test_mxfp_qat_config_maps_vllm_ascend_env(mode, expected_mode):
"ignore_patterns": ["lm_head"],
"experts": {"enable": True},
"fallback_layers": [[0, 2], [37, 40]],
"mxfp4_scale_alg": "cx",
}
)

assert env["VLLM_ASCEND_QAT_FAKE_QUANT"] == "1"
assert env["VLLM_ASCEND_QAT_FAKE_QUANT_MODE"] == expected_mode
assert env["VLLM_ASCEND_QAT_MXFP8_QUANT_BACKEND"] == "torch"
assert env["VLLM_ASCEND_QAT_MXFP8_ROUNDING_MODE"] == "hash"
assert env["VLLM_ASCEND_QAT_MXFP4_SCALE_ALG"] == "cx"
assert json.loads(env["VLLM_ASCEND_QAT_MXFP8_FAKE_QUANT_TARGETS"]) == ["fprop", "dgrad", "wgrad"]
assert env["VLLM_ASCEND_QAT_MXFP8_ROTATION_ENABLE"] == "true"
assert json.loads(env["VLLM_ASCEND_QAT_MXFP8_ROTATION_TARGETS"]) == ["fprop", "wgrad"]
Expand Down
37 changes: 36 additions & 1 deletion tests/utils/test_mxfp_quant_cpu.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,59 @@
import pytest
import torch

from verl.utils.qat.mxfp_linear import MXFPQATConfig, MXFPQATLinear
from verl.utils.qat.mxfp_quant import (
MXFPMode,
dequantize_mxfp4,
dequantize_mxfp8,
fake_quantize,
normalize_mxfp4_scale_alg,
normalize_mxfp_mode,
quantize_mxfp4,
quantize_mxfp8,
)
from verl.utils.qat.mxfp_rotation import RotationConfig, _matrix, rotate_blocks
from verl.utils.qat.mxfp_linear import MXFPQATConfig, MXFPQATLinear


@pytest.mark.parametrize("mode", ["mxfp_w8a8", "w8a8_mxfp8", "mxfp_w4a8", "mxfp_w4a4"])
def test_mode_names_normalize(mode):
assert normalize_mxfp_mode(mode) in {MXFPMode.W8A8, MXFPMode.W4A8, MXFPMode.W4A4}


@pytest.mark.parametrize(
("scale_alg", "expected"),
[
("standard", "standard"),
("ocp", "standard"),
(0, "standard"),
("tfs", "tfs"),
(1, "tfs"),
("cx", "cx"),
(2, "cx"),
],
)
def test_mxfp4_scale_algorithm_names_normalize(scale_alg, expected):
assert normalize_mxfp4_scale_alg(scale_alg) == expected


@pytest.mark.parametrize(
("scale_alg", "value", "expected_scale", "expected_decoded"),
[
("standard", 7.0, 127.0, 6.0),
("tfs", 7.0, 128.0, 8.0),
("cx", 7.0, 127.0, 6.0),
("cx", 8.0, 128.0, 8.0),
("cx", 15.0, 129.0, 16.0),
],
)
def test_mxfp4_scale_algorithms_use_expected_block_scale(scale_alg, value, expected_scale, expected_decoded):
x = torch.full((1, 32), value)
payload, scale = quantize_mxfp4(x, scale_alg=scale_alg)
assert scale.item() == expected_scale
decoded = dequantize_mxfp4(payload, scale, x.dtype, scale_alg=scale_alg)
assert torch.equal(decoded, torch.full_like(x, expected_decoded))


@pytest.mark.parametrize(
"quantize,dequantize", [(quantize_mxfp8, dequantize_mxfp8), (quantize_mxfp4, dequantize_mxfp4)]
)
Expand Down
3 changes: 3 additions & 0 deletions verl/trainer/config/_generated_ppo_trainer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ actor_rollout_ref:
quantization_config_path: null
quant_backend: npu
rounding_mode: rint
mxfp4_scale_alg: standard
fake_quant_targets:
- Fprop
backward_reduction_axis_fake_quant: false
Expand Down Expand Up @@ -249,6 +250,7 @@ actor_rollout_ref:
quantization_config_path: null
quant_backend: npu
rounding_mode: rint
mxfp4_scale_alg: standard
fake_quant_targets:
- Fprop
backward_reduction_axis_fake_quant: false
Expand Down Expand Up @@ -559,6 +561,7 @@ critic:
quantization_config_path: null
quant_backend: npu
rounding_mode: rint
mxfp4_scale_alg: standard
fake_quant_targets:
- Fprop
backward_reduction_axis_fake_quant: false
Expand Down
3 changes: 3 additions & 0 deletions verl/trainer/config/engine/fsdp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ qat:
# MXFP rounding mode: rint, round, random, or hash (torch backend only for stochastic modes).
rounding_mode: "rint"

# MXFP4 shared-scale algorithm: standard (current OCP behavior), tfs, or cx.
mxfp4_scale_alg: "standard"

# MXFP pseudo-quantization locations. Valid values are Fprop, Dgrad, and Wgrad.
fake_quant_targets: [Fprop]

Expand Down
25 changes: 20 additions & 5 deletions verl/trainer/ppo/ray_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1312,12 +1312,27 @@ def _update_actor(self, batch: DataProto) -> DataProto:
ppo_epochs = self.config.actor_rollout_ref.actor.ppo_epochs
seed = self.config.actor_rollout_ref.actor.data_loader_seed
shuffle = self.config.actor_rollout_ref.actor.shuffle
qat_config = self.config.actor_rollout_ref.actor.fsdp_config.qat
probe_quant_error = (
qat_config.get("probe_quant_error", False)
if hasattr(qat_config, "get")
else getattr(qat_config, "probe_quant_error", False)
actor_config = self.config.actor_rollout_ref.actor
actor_strategy = (
actor_config.get("strategy") if hasattr(actor_config, "get") else getattr(actor_config, "strategy", None)
)
probe_quant_error = False
if actor_strategy in {"fsdp", "fsdp2"}:
fsdp_config = (
actor_config.get("fsdp_config")
if hasattr(actor_config, "get")
else getattr(actor_config, "fsdp_config", None)
)
qat_config = (
fsdp_config.get("qat")
if hasattr(fsdp_config, "get")
else getattr(fsdp_config, "qat", None)
)
probe_quant_error = (
qat_config.get("probe_quant_error", False)
if hasattr(qat_config, "get")
else getattr(qat_config, "probe_quant_error", False)
)
tu.assign_non_tensor(
batch_td,
calculate_entropy=calculate_entropy,
Expand Down
2 changes: 2 additions & 0 deletions verl/utils/qat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
dequantize_mxfp4,
dequantize_mxfp8,
fake_quantize,
normalize_mxfp4_scale_alg,
normalize_mxfp_mode,
quantize_mxfp4,
quantize_mxfp8,
Expand Down Expand Up @@ -76,6 +77,7 @@
"quantize_mxfp4",
"dequantize_mxfp4",
"fake_quantize",
"normalize_mxfp4_scale_alg",
"normalize_mxfp_mode",
"RotationConfig",
"rotate_blocks",
Expand Down
7 changes: 6 additions & 1 deletion verl/utils/qat/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class QATConfig(BaseConfig):
quantization_config_path: Optional[str] = None
quant_backend: str = "npu"
rounding_mode: str = "rint"
mxfp4_scale_alg: str = "standard"
fake_quant_targets: list[str] = field(default_factory=lambda: ["Fprop"])
backward_reduction_axis_fake_quant: bool = False
rotation_enable: bool = False
Expand Down Expand Up @@ -138,6 +139,7 @@ def apply_qat(
group_size=config.group_size,
quant_backend=config.quant_backend,
rounding_mode=config.rounding_mode,
mxfp4_scale_alg=config.mxfp4_scale_alg,
fake_quant_targets=config.fake_quant_targets,
backward_reduction_axis_fake_quant=config.backward_reduction_axis_fake_quant,
rotation_enable=config.rotation_enable,
Expand Down Expand Up @@ -185,10 +187,12 @@ def apply_qat(
tuple(config.fake_quant_targets),
tuple(config.rotation_targets),
config.rotation_enable,
config.mxfp4_scale_alg,
experts_enabled,
),
"FSDP2 MXFP QAT configured: mode=%s dense_linear=%d moe_blocks=%d experts=%s "
"fprop=%s dgrad=%s wgrad=%s rotation=%s rotation_targets=%s backward_axis_cache=%s",
"fprop=%s dgrad=%s wgrad=%s rotation=%s rotation_targets=%s mxfp4_scale_alg=%s "
"backward_axis_cache=%s",
mxfp_mode.value,
len(modules_to_replace),
moe_count,
Expand All @@ -198,6 +202,7 @@ def apply_qat(
"wgrad" in mxfp_config.fake_quant_targets,
mxfp_config.rotation.enable,
mxfp_config.rotation.targets,
mxfp_config.mxfp4_scale_alg,
mxfp_config.backward_reduction_axis_fake_quant,
)
return model
Expand Down
4 changes: 3 additions & 1 deletion verl/utils/qat/mxfp_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from verl.utils.qat.mxfp_debug import warn_once
from verl.utils.qat.mxfp_probe import quantize_with_probe
from verl.utils.qat.mxfp_quant import MXFPMode, normalize_mxfp_mode
from verl.utils.qat.mxfp_quant import MXFPMode, normalize_mxfp4_scale_alg, normalize_mxfp_mode
from verl.utils.qat.mxfp_rotation import RotationConfig, rotate_blocks

_TARGETS = ("fprop", "wgrad", "dgrad")
Expand Down Expand Up @@ -54,6 +54,7 @@ class MXFPQATConfig:
group_size: int = 32
quant_backend: str = "npu"
rounding_mode: str = "rint"
mxfp4_scale_alg: str = "standard"
fake_quant_targets: tuple[str, ...] = ("fprop",)
backward_reduction_axis_fake_quant: bool = True
rotation_enable: bool = False
Expand All @@ -69,6 +70,7 @@ def __post_init__(self):
mode = normalize_mxfp_mode(self.mode)
object.__setattr__(self, "mode", mode.value)
object.__setattr__(self, "quant_backend", str(self.quant_backend).lower())
object.__setattr__(self, "mxfp4_scale_alg", normalize_mxfp4_scale_alg(self.mxfp4_scale_alg))
if self.group_size != 32:
raise ValueError(f"MXFP QAT requires group_size=32, got {self.group_size}")
if mode in (MXFPMode.W4A8, MXFPMode.W4A4) and self.quant_backend != "torch":
Expand Down
1 change: 1 addition & 0 deletions verl/utils/qat/mxfp_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ def quantize_with_probe(
fmt,
quant_backend=config.quant_backend,
rounding_mode=config.rounding_mode,
mxfp4_scale_alg=getattr(config, "mxfp4_scale_alg", "standard"),
)
if record_names:
_PROBE.record_many(
Expand Down
79 changes: 67 additions & 12 deletions verl/utils/qat/mxfp_quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
MXFP4_MIN_PRIVATE_EXP = 0
MXFP4_MANTISSA_SCALE = 2.0
MXFP4_MAX_NORM = 6.0
MXFP4_CX_MAX_NORM = 7.25


class MXFPMode(str, Enum):
Expand Down Expand Up @@ -70,6 +71,48 @@ def normalize_rounding_mode(mode: str) -> str:
return mode


_MXFP4_SCALE_ALIASES = {
"standard": "standard",
"ocp": "standard",
"0": "standard",
"tfs": "tfs",
"1": "tfs",
"cx": "cx",
"2": "cx",
}


def normalize_mxfp4_scale_alg(scale_alg: str) -> str:
"""Normalize the MXFP4 shared-scale algorithm name.

``standard`` preserves the existing OCP-style scale. ``tfs`` and ``cx``
are the two optimized scale choices used by the MXFP4 experiments.
Numeric aliases match the corresponding hardware ``scale_alg`` values.
"""
key = str(scale_alg).strip().lower()
try:
return _MXFP4_SCALE_ALIASES[key]
except KeyError as exc:
raise ValueError(f"Unsupported MXFP4 scale algorithm {scale_alg!r}; expected standard, tfs, or cx") from exc


def _mxfp4_shared_scale(amax: torch.Tensor, scale_alg: str) -> tuple[torch.Tensor, torch.Tensor]:
"""Return the shared exponent and real scale factor for each 32-value block."""
scale_alg = normalize_mxfp4_scale_alg(scale_alg)
safe_amax = torch.where(amax == 0, torch.ones_like(amax), amax)
if scale_alg == "standard":
shared_exp = torch.floor(torch.log2(safe_amax)) - MXFP4_EMAX
elif scale_alg == "tfs":
shared_exp = torch.ceil(torch.log2(safe_amax / MXFP4_MAX_NORM))
else: # CX uses 7.25 as the effective maximum when selecting a power-of-two scale.
shared_exp = torch.ceil(torch.log2(safe_amax / MXFP4_CX_MAX_NORM))
shared_exp = shared_exp.clamp(min=-MXFP8_SCALE_EMAX)
shared_exp = torch.where(amax == 0, torch.full_like(shared_exp, -MXFP8_SCALE_EMAX), shared_exp)
shared_exp = torch.where(shared_exp > MXFP8_SCALE_EMAX, torch.full_like(shared_exp, float("nan")), shared_exp)
scale_factor = torch.pow(2.0, shared_exp)
return shared_exp, scale_factor


def _check_tensor(tensor: torch.Tensor) -> None:
if tensor.ndim != 2:
raise ValueError(f"MXFP quantization expects a 2D tensor, got shape={tuple(tensor.shape)}")
Expand Down Expand Up @@ -178,18 +221,19 @@ def quantize_mxfp8(


def quantize_mxfp4(
tensor: torch.Tensor, *, quant_backend: str = "torch", rounding_mode: str = "rint"
tensor: torch.Tensor,
*,
quant_backend: str = "torch",
rounding_mode: str = "rint",
scale_alg: str = "standard",
) -> tuple[torch.Tensor, torch.Tensor]:
_check_tensor(tensor)
if normalize_quant_backend(quant_backend) != "torch":
raise ValueError("MXFP4 QAT requires quant_backend='torch'")
blocks = tensor.to(torch.float32).reshape(tensor.shape[0], -1, MXFP_GROUP_SIZE)
amax = blocks.abs().amax(dim=-1)
safe_amax = torch.where(amax == 0, torch.ones_like(amax), amax)
shared_exp = (torch.floor(torch.log2(safe_amax)) - MXFP4_EMAX).clamp(min=-MXFP8_SCALE_EMAX)
shared_exp = torch.where(amax == 0, torch.full_like(shared_exp, -MXFP8_SCALE_EMAX), shared_exp)
shared_exp = torch.where(shared_exp > MXFP8_SCALE_EMAX, torch.full_like(shared_exp, float("nan")), shared_exp)
normalized = blocks * torch.pow(2.0, -shared_exp.unsqueeze(-1))
shared_exp, scale_factor = _mxfp4_shared_scale(amax, scale_alg)
normalized = blocks / scale_factor.unsqueeze(-1)
private_exp = torch.floor(torch.log2(normalized.abs() + (normalized == 0).float())).clamp(
min=MXFP4_MIN_PRIVATE_EXP
)
Expand All @@ -209,10 +253,14 @@ def dequantize_mxfp8(payload: torch.Tensor, scale: torch.Tensor, dtype: torch.dt
return (blocks * descale.unsqueeze(-1)).reshape_as(payload).to(dtype)


def dequantize_mxfp4(payload: torch.Tensor, scale: torch.Tensor, dtype: torch.dtype) -> torch.Tensor:
def dequantize_mxfp4(
payload: torch.Tensor, scale: torch.Tensor, dtype: torch.dtype, *, scale_alg: str = "standard"
) -> torch.Tensor:
scale_alg = normalize_mxfp4_scale_alg(scale_alg)
scale = scale.to(torch.float32)
scale = torch.where(scale == 255, torch.full_like(scale, float("nan")), scale)
return dequantize_mxfp8(payload, scale, dtype)
result = dequantize_mxfp8(payload, scale, torch.float32)
return result.to(dtype)


def fake_quantize(
Expand All @@ -221,23 +269,30 @@ def fake_quantize(
*,
quant_backend: str = "torch",
rounding_mode: str = "rint",
mxfp4_scale_alg: str = "standard",
) -> torch.Tensor:
"""Return the QDQ value; callers provide the STE boundary."""
warn_once(
("qdq", fmt, quant_backend, rounding_mode),
"MXFP QDQ executed: format=%s backend=%s rounding=%s",
("qdq", fmt, quant_backend, rounding_mode, mxfp4_scale_alg),
"MXFP QDQ executed: format=%s backend=%s rounding=%s mxfp4_scale_alg=%s",
fmt,
quant_backend,
rounding_mode,
mxfp4_scale_alg,
)
original_shape = tensor.shape
padded = (-tensor.shape[-1]) % MXFP_GROUP_SIZE
if padded:
tensor = torch.nn.functional.pad(tensor, (0, padded))
matrix = tensor.reshape(-1, tensor.shape[-1])
if fmt == "mxfp4":
payload, scale = quantize_mxfp4(matrix, quant_backend=quant_backend, rounding_mode=rounding_mode)
result = dequantize_mxfp4(payload, scale, tensor.dtype)
payload, scale = quantize_mxfp4(
matrix,
quant_backend=quant_backend,
rounding_mode=rounding_mode,
scale_alg=mxfp4_scale_alg,
)
result = dequantize_mxfp4(payload, scale, tensor.dtype, scale_alg=mxfp4_scale_alg)
elif fmt == "mxfp8":
payload, scale = quantize_mxfp8(matrix, quant_backend=quant_backend, rounding_mode=rounding_mode)
result = dequantize_mxfp8(payload, scale, tensor.dtype)
Expand Down
3 changes: 3 additions & 0 deletions verl/utils/qat/vllm_ascend.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def mxfp_qat_config_to_vllm_ascend_env(config: Mapping[str, Any]) -> dict[str, s
group_size = int(config.get("group_size", 32))
quant_backend = str(config.get("quant_backend", "npu")).lower()
rounding_mode = normalize_rounding_mode(config.get("rounding_mode", "rint"))
mxfp4_scale_alg = str(config.get("mxfp4_scale_alg", "standard"))
if quant_backend == "npu" and rounding_mode in {"random", "hash"}:
raise ValueError("MXFP8 stochastic rounding requires quant_backend='torch'")
fake_quant_targets = _as_list(config.get("fake_quant_targets"), ["Fprop"])
Expand All @@ -68,6 +69,7 @@ def mxfp_qat_config_to_vllm_ascend_env(config: Mapping[str, Any]) -> dict[str, s
group_size=group_size,
quant_backend=quant_backend,
rounding_mode=rounding_mode,
mxfp4_scale_alg=mxfp4_scale_alg,
fake_quant_targets=tuple(fake_quant_targets),
rotation_enable=rotation_enable,
rotation_targets=tuple(rotation_targets),
Expand Down Expand Up @@ -98,6 +100,7 @@ def mxfp_qat_config_to_vllm_ascend_env(config: Mapping[str, Any]) -> dict[str, s
"VLLM_ASCEND_QAT_FAKE_QUANT_MODE": _VLLM_MODE[mode],
"VLLM_ASCEND_QAT_MXFP8_QUANT_BACKEND": qat_config.quant_backend,
"VLLM_ASCEND_QAT_MXFP8_ROUNDING_MODE": qat_config.rounding_mode,
"VLLM_ASCEND_QAT_MXFP4_SCALE_ALG": qat_config.mxfp4_scale_alg,
"VLLM_ASCEND_QAT_MXFP8_GROUP_SIZE": str(qat_config.group_size),
"VLLM_ASCEND_QAT_MXFP8_FAKE_QUANT_TARGETS": json.dumps(
[target for target in ("fprop", "dgrad", "wgrad") if target in qat_config.fake_quant_targets]
Expand Down
1 change: 1 addition & 0 deletions verl/workers/config/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class QATEngineConfig(BaseConfig):
quantization_config_path: Optional[str] = None
quant_backend: str = "npu"
rounding_mode: str = "rint"
mxfp4_scale_alg: str = "standard"
fake_quant_targets: list[str] = field(default_factory=lambda: ["Fprop"])
backward_reduction_axis_fake_quant: bool = False
rotation_enable: bool = False
Expand Down
Loading