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
121 changes: 116 additions & 5 deletions tests/utils/test_mxfp_moe_cpu.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import torch
from torch import nn

Expand All @@ -7,7 +8,8 @@
_transform_weight,
attach_mxfp_expert_qat,
get_mxfp_expert_qat_config,
prepare_mxfp_fprop_operands,
mxfp_grouped_matmul,
prepare_mxfp_fprop_input,
)


Expand All @@ -31,6 +33,24 @@ def _config():
)


def _fake_grouped_matmul(x, weight, group_list, group_list_type, *, split_item=2, group_type=0):
counts = group_list.tolist()
if group_list_type == 0:
counts = [count - (counts[index - 1] if index else 0) for index, count in enumerate(counts)]
offset = 0
if split_item == 2:
outputs = []
for expert, count in enumerate(counts):
outputs.append(x[offset : offset + count] @ weight[expert])
offset += count
return torch.cat(outputs, dim=0)
outputs = []
for count in counts:
outputs.append(x[:, offset : offset + count] @ weight[offset : offset + count])
offset += count
return torch.stack(outputs)


def test_attach_only_marks_moe_blocks():
model = nn.Sequential(_FakeMoeBlock(), nn.Linear(32, 32))
config = _config()
Expand Down Expand Up @@ -61,9 +81,100 @@ def test_wgrad_transform_quantizes_token_reduction_axis():
def test_gate_up_can_share_detached_fprop_input():
config = _config()
x = torch.randn(65, 32, requires_grad=True)
weight = torch.randn(4, 32, 64, requires_grad=True)
x_fprop, weight_fprop = prepare_mxfp_fprop_operands(x, weight, config)
x_fprop = prepare_mxfp_fprop_input(x, config)
assert x_fprop.shape == x.shape
assert weight_fprop.shape == weight.shape
assert not x_fprop.requires_grad
assert not weight_fprop.requires_grad


@pytest.mark.parametrize(
("need_input_grad", "need_weight_grad"),
[(True, True), (True, False), (False, True)],
)
def test_grouped_matmul_recomputes_backward_operands_and_saves_only_inputs(
monkeypatch, need_input_grad, need_weight_grad
):
wgrad_input_strides = []

def fake_grouped_matmul(x, weight, group_list, group_list_type, *, split_item=2, group_type=0):
if split_item == 3:
wgrad_input_strides.append(x.stride())
return _fake_grouped_matmul(
x, weight, group_list, group_list_type, split_item=split_item, group_type=group_type
)

monkeypatch.setattr("verl.utils.qat.mxfp_moe._call_grouped_matmul", fake_grouped_matmul)
config = _config()
x = torch.randn(64, 32, requires_grad=need_input_grad)
weight = torch.randn(1, 32, 32, requires_grad=need_weight_grad)
group_list = torch.tensor([64])
saved_shapes = []

def pack(tensor):
saved_shapes.append(tuple(tensor.shape))
return tensor

with torch.autograd.graph.saved_tensors_hooks(pack, lambda tensor: tensor):
output = mxfp_grouped_matmul(x, weight, group_list, config, name="test").sum()

expected_shapes = []
if need_weight_grad:
expected_shapes.append(tuple(x.shape))
if need_input_grad:
expected_shapes.append(tuple(weight.shape))
assert saved_shapes == expected_shapes
output.backward()
assert (x.grad is not None) == need_input_grad
assert (weight.grad is not None) == need_weight_grad
if x.grad is not None:
assert torch.isfinite(x.grad).all()
if weight.grad is not None:
assert torch.isfinite(weight.grad).all()
if need_weight_grad:
assert wgrad_input_strides == [(1, 32)]


@pytest.mark.parametrize(
"rotation_targets",
[("fprop",), ("dgrad",), ("wgrad",), ("fprop", "dgrad", "wgrad")],
)
def test_grouped_matmul_rotation_gradients_match_unrotated_matmul(monkeypatch, rotation_targets):
monkeypatch.setattr("verl.utils.qat.mxfp_moe._call_grouped_matmul", _fake_grouped_matmul)
config = MXFPQATConfig(
mode="mxfp_w8a8",
quant_backend="torch",
fake_quant_targets=(),
rotation_enable=True,
rotation_targets=rotation_targets,
rotation_block_size=32,
rotation_seed=29,
)
torch.manual_seed(31)
x_reference = torch.randn(65, 32, requires_grad=True)
x_qat = x_reference.detach().clone().requires_grad_()
weight_reference = torch.randn(3, 32, 64, requires_grad=True)
weight_qat = weight_reference.detach().clone().requires_grad_()
grad_output = torch.randn(65, 64)
group_list = torch.tensor([7, 30, 28])

reference_output = torch.cat(
[
x_reference[:7] @ weight_reference[0],
x_reference[7:37] @ weight_reference[1],
x_reference[37:] @ weight_reference[2],
],
dim=0,
)
qat_output = mxfp_grouped_matmul(
x_qat,
weight_qat,
group_list,
config,
name="rotation-test",
fprop_x=prepare_mxfp_fprop_input(x_qat, config),
)
(reference_output * grad_output).sum().backward()
(qat_output * grad_output).sum().backward()

assert torch.allclose(qat_output, reference_output, atol=1e-5, rtol=1e-5)
assert torch.allclose(x_qat.grad, x_reference.grad, atol=1e-5, rtol=1e-5)
assert torch.allclose(weight_qat.grad, weight_reference.grad, atol=1e-5, rtol=1e-5)
85 changes: 85 additions & 0 deletions tests/utils/test_mxfp_qat_vllm_ascend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import json

import pytest
from omegaconf import OmegaConf

from verl.utils.qat.vllm_ascend import mxfp_qat_config_to_vllm_ascend_env


@pytest.mark.parametrize(
("mode", "expected_mode"),
[
("mxfp_w8a8", "w8a8_mxfp8"),
("mxfp_w4a8", "w4a8_mxfp4"),
("mxfp_w4a4", "w4a4_mxfp4"),
],
)
def test_mxfp_qat_config_maps_vllm_ascend_env(mode, expected_mode):
env = mxfp_qat_config_to_vllm_ascend_env(
{
"mode": mode,
"group_size": 32,
"quant_backend": "torch",
"rounding_mode": "hash",
"fake_quant_targets": ["Fprop", "Dgrad", "Wgrad"],
"rotation_enable": True,
"rotation_targets": ["Fprop", "Wgrad"],
"rotation_block_size": 32,
"rotation_seed": 19,
"ignore_patterns": ["lm_head"],
"experts": {"enable": True},
"fallback_layers": [[0, 2], [37, 40]],
}
)

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 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"]
assert json.loads(env["VLLM_ASCEND_QAT_MXFP8_IGNORE_PATTERNS"]) == ["lm_head"]
assert json.loads(env["VLLM_ASCEND_QAT_MXFP8_FALLBACK_LAYERS"]) == [[0, 2], [37, 40]]


def test_mxfp_qat_config_keeps_disabled_experts_in_high_precision():
env = mxfp_qat_config_to_vllm_ascend_env(
{"mode": "mxfp_w8a8", "group_size": 32, "experts": {"enable": False}}
)

ignore_patterns = json.loads(env["VLLM_ASCEND_QAT_MXFP8_IGNORE_PATTERNS"])
assert any("mlp\\.experts" in pattern for pattern in ignore_patterns)
assert any("shared_expert" in pattern for pattern in ignore_patterns)


def test_mxfp_qat_config_accepts_hydra_config_objects():
config = OmegaConf.create(
{
"mode": "mxfp_w8a8",
"group_size": 32,
"quant_backend": "npu",
"fake_quant_targets": ["Fprop", "Dgrad"],
"rotation_targets": ["Wgrad"],
"fallback_layers": [[0, 2]],
"experts": {"enable": True},
}
)

env = mxfp_qat_config_to_vllm_ascend_env(config)

assert json.loads(env["VLLM_ASCEND_QAT_MXFP8_FAKE_QUANT_TARGETS"]) == ["fprop", "dgrad"]
assert json.loads(env["VLLM_ASCEND_QAT_MXFP8_ROTATION_TARGETS"]) == ["wgrad"]
assert json.loads(env["VLLM_ASCEND_QAT_MXFP8_FALLBACK_LAYERS"]) == [[0, 2]]


def test_mxfp_qat_config_rejects_unsupported_group_size():
with pytest.raises(ValueError, match="group_size=32"):
mxfp_qat_config_to_vllm_ascend_env({"mode": "mxfp_w8a8", "group_size": 16})


def test_mxfp_qat_config_rejects_npu_stochastic_rounding():
with pytest.raises(ValueError, match="requires quant_backend='torch'"):
mxfp_qat_config_to_vllm_ascend_env(
{"mode": "mxfp_w8a8", "group_size": 32, "quant_backend": "npu", "rounding_mode": "hash"}
)
41 changes: 37 additions & 4 deletions tests/utils/test_mxfp_quant_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def test_mode_names_normalize(mode):
assert normalize_mxfp_mode(mode) in {MXFPMode.W8A8, MXFPMode.W4A8, MXFPMode.W4A4}


@pytest.mark.parametrize("quantize,dequantize", [(quantize_mxfp8, dequantize_mxfp8), (quantize_mxfp4, dequantize_mxfp4)])
@pytest.mark.parametrize(
"quantize,dequantize", [(quantize_mxfp8, dequantize_mxfp8), (quantize_mxfp4, dequantize_mxfp4)]
)
def test_mxfp_qdq_preserves_shape_and_is_finite(quantize, dequantize):
x = torch.linspace(-2, 2, 96, dtype=torch.float32).reshape(3, 32)
payload, scale = quantize(x)
Expand Down Expand Up @@ -55,9 +57,7 @@ def test_rotation_is_seeded_and_orthogonal():

def test_linear_replacement_reuses_parameters_and_has_identity_without_transforms():
linear = torch.nn.Linear(32, 64)
config = MXFPQATConfig(
mode="mxfp_w8a8", quant_backend="torch", fake_quant_targets=(), rotation_enable=False
)
config = MXFPQATConfig(mode="mxfp_w8a8", quant_backend="torch", fake_quant_targets=(), rotation_enable=False)
qat = MXFPQATLinear.from_linear(linear, config)
assert qat.weight is linear.weight
assert qat.bias is linear.bias
Expand Down Expand Up @@ -98,3 +98,36 @@ def test_linear_fprop_rotation_returns_gradients_in_original_coordinates():
qat(x).sum().backward()
assert x.grad.shape == x.shape
assert qat.weight.grad.shape == linear.weight.shape


@pytest.mark.parametrize(
"rotation_targets",
[("fprop",), ("dgrad",), ("wgrad",), ("fprop", "dgrad", "wgrad")],
)
def test_linear_rotation_gradients_match_unrotated_linear(rotation_targets):
torch.manual_seed(23)
reference = torch.nn.Linear(32, 64)
qat_source = torch.nn.Linear(32, 64)
qat_source.load_state_dict(reference.state_dict())
config = MXFPQATConfig(
mode="mxfp_w8a8",
quant_backend="torch",
fake_quant_targets=(),
rotation_enable=True,
rotation_targets=rotation_targets,
rotation_seed=29,
)
qat = MXFPQATLinear.from_linear(qat_source, config)
x_reference = torch.randn((4, 32), requires_grad=True)
x_qat = x_reference.detach().clone().requires_grad_()
grad_output = torch.randn((4, 64))

reference_output = reference(x_reference)
qat_output = qat(x_qat)
(reference_output * grad_output).sum().backward()
(qat_output * grad_output).sum().backward()

assert torch.allclose(qat_output, reference_output, atol=1e-5, rtol=1e-5)
assert torch.allclose(x_qat.grad, x_reference.grad, atol=1e-5, rtol=1e-5)
assert torch.allclose(qat.weight.grad, reference.weight.grad, atol=1e-5, rtol=1e-5)
assert torch.allclose(qat.bias.grad, reference.bias.grad, atol=1e-5, rtol=1e-5)
34 changes: 34 additions & 0 deletions tests/workers/test_fsdp_mxfp_qat_weight_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from types import SimpleNamespace

import pytest
import torch

pytest.importorskip("peft")

from verl.workers.engine.fsdp import transformer_impl
from verl.workers.engine.fsdp.transformer_impl import FSDPEngineWithLMHead


@pytest.mark.parametrize("mode", ["mxfp_w8a8", "mxfp_w4a8", "mxfp_w4a4"])
def test_mxfp_weight_sync_keeps_high_precision_weights(mode, monkeypatch):
engine = object.__new__(FSDPEngineWithLMHead)
engine.module = torch.nn.Linear(32, 32)
engine.model_config = SimpleNamespace(lora={})
engine.engine_config = SimpleNamespace(mixed_precision=None)
engine._qat_enabled = True
engine._qat_config = SimpleNamespace(mode=mode, group_size=32, ignore_patterns=[])
engine._is_offload_param = False
engine._uses_fsdp2_cpu_offload_policy = True
engine.rank = 0

monkeypatch.setattr(transformer_impl, "log_gpu_memory_usage", lambda *args, **kwargs: None)
monkeypatch.setattr(transformer_impl, "get_device_id", lambda: torch.device("cpu"))

original_params = engine.module.state_dict()
params, peft_config = engine.get_per_tensor_param()
synced_params = dict(params)

assert peft_config is None
assert synced_params.keys() == original_params.keys()
for name, tensor in original_params.items():
torch.testing.assert_close(synced_params[name], tensor)
13 changes: 3 additions & 10 deletions verl/models/transformers/npu_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
from verl.utils.qat.mxfp_moe import (
get_mxfp_expert_qat_config,
mxfp_grouped_matmul,
prepare_mxfp_fprop_operands,
prepare_mxfp_weight_fprop,
prepare_mxfp_fprop_input,
)

logger = logging.get_logger(__name__)
Expand Down Expand Up @@ -202,18 +201,14 @@ def _qwen3_sparse_moe_routed_forward_npu(self, hidden_states: torch.Tensor):

mxfp_qat_config = get_mxfp_expert_qat_config(self)
if mxfp_qat_config is not None:
permuted_tokens_fprop, w1_fprop = prepare_mxfp_fprop_operands(
permuted_tokens, w1, mxfp_qat_config
)
w2_fprop = prepare_mxfp_weight_fprop(w2, mxfp_qat_config)
permuted_tokens_fprop = prepare_mxfp_fprop_input(permuted_tokens, mxfp_qat_config)
up_res = mxfp_grouped_matmul(
permuted_tokens,
w1,
tokens_per_expert,
mxfp_qat_config,
name="gate_up.up_proj",
fprop_x=permuted_tokens_fprop,
fprop_weight=w1_fprop,
)
gate_res = mxfp_grouped_matmul(
permuted_tokens,
Expand All @@ -222,22 +217,20 @@ def _qwen3_sparse_moe_routed_forward_npu(self, hidden_states: torch.Tensor):
mxfp_qat_config,
name="gate_up.gate_proj",
fprop_x=permuted_tokens_fprop,
fprop_weight=w2_fprop,
)
else:
up_res = NPUGmmFunction.apply(permuted_tokens, w1, tokens_per_expert)
gate_res = NPUGmmFunction.apply(permuted_tokens, w2, tokens_per_expert)
act_res = torch_npu.npu_swiglu(torch.cat([gate_res, up_res], dim=-1))
if mxfp_qat_config is not None:
act_res_fprop, w3_fprop = prepare_mxfp_fprop_operands(act_res, w3, mxfp_qat_config)
act_res_fprop = prepare_mxfp_fprop_input(act_res, mxfp_qat_config)
down_res = mxfp_grouped_matmul(
act_res,
w3,
tokens_per_expert,
mxfp_qat_config,
name="down_proj",
fprop_x=act_res_fprop,
fprop_weight=w3_fprop,
)
else:
down_res = NPUGmmFunction.apply(act_res, w3, tokens_per_expert)
Expand Down
3 changes: 2 additions & 1 deletion verl/trainer/config/_generated_ppo_megatron_trainer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,9 @@ actor_rollout_ref:
served_model_name: ${oc.select:actor_rollout_ref.model.path,null}
quantization: null
quantization_config_file: null
qat_fake_quant: false
mtp: ${oc.select:actor_rollout_ref.model.mtp, null}
qat: ${oc.select:actor_rollout_ref.actor.fsdp_config.qat,${oc.select:actor_rollout_ref.actor.megatron.qat,null}}
qat: ${oc.select:actor_rollout_ref.actor.fsdp_config.qat,null}
disaggregation:
enabled: false
prefill_replicas: 1
Expand Down
Loading