Skip to content
Open
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
3 changes: 3 additions & 0 deletions .buildkite/test-amd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,8 @@ steps:
- tests/models/minimax_m3/amd_specialized_decode/
- tests/models/minimax_m3/test_index_cache_overrides.py
- tests/models/minimax_m3/test_dspark_drafter.py
- tests/models/minimax_m3/test_xlayer_allreduce_fusion.py
- vllm/model_executor/layers/fused_allreduce_gemma_rms_norm.py
- vllm/model_executor/models/config.py
- vllm/platforms/rocm.py
commands:
Expand All @@ -1457,6 +1459,7 @@ steps:
models/minimax_m3/amd_specialized_decode/test_runtime.py
- pytest -v -s models/minimax_m3/test_index_cache_overrides.py
- pytest -v -s models/minimax_m3/test_dspark_drafter.py
- pytest -v -s models/minimax_m3/test_xlayer_allreduce_fusion.py
# the long TP4 hand-off stress, the one pytest.mark.optional in the directory
- pytest -v -s --optional -m optional models/minimax_m3/amd_specialized_decode/test_ops.py

Expand Down
3 changes: 3 additions & 0 deletions .buildkite/test_areas/monokernels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ steps:
- vllm/models/minimax_m3/amd/
- tests/models/minimax_m3/amd_specialized_decode/
- tests/models/minimax_m3/test_dspark_drafter.py
- tests/models/minimax_m3/test_xlayer_allreduce_fusion.py
- vllm/model_executor/layers/fused_allreduce_gemma_rms_norm.py
commands:
# Named one by one, not as a directory: a directory selection silently adopts
# every new file into this blocking 4-GPU step, and check_test_tethering.py
Expand All @@ -159,6 +161,7 @@ steps:
models/minimax_m3/amd_specialized_decode/test_model.py
models/minimax_m3/amd_specialized_decode/test_runtime.py
- pytest -v -s models/minimax_m3/test_dspark_drafter.py
- pytest -v -s models/minimax_m3/test_xlayer_allreduce_fusion.py

- label: ':amd: (MI355) Kimi-K3 Specialized Decode'
key: kimi-k3-specialized-decode-amd
Expand Down
45 changes: 45 additions & 0 deletions tests/kernels/test_minimax_m3_amd_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* Gemma RMSNorm (plain + fused-add-residual) -> fp32 PyTorch normalize
* Fused-add Gemma RMSNorm + MXFP8 epilogue -> norm, then mxfp8_e4m3_quantize
(bitwise)
* Fused-add Gemma RMSNorm + per-token FP8 -> norm, then per-token FP8 quant
epilogue
* SwiGLU-OAI (split layout) -> fp32 PyTorch elementwise
* Fused MXFP8 activation quant (Triton) -> _mxfp8_e4m3_quantize_torch
* Native MXFP8 linear (dot_scaled) -> dequant-to-bf16 @ matmul
Expand Down Expand Up @@ -56,6 +58,7 @@
)
from vllm.models.minimax_m3.amd.ops import ( # noqa: E402
gemma_fused_add_rmsnorm,
gemma_fused_add_rmsnorm_fp8_token,
gemma_fused_add_rmsnorm_mxfp8,
gemma_fused_add_rmsnorm_mxfp8_and_bf16,
gemma_rmsnorm,
Expand Down Expand Up @@ -215,6 +218,48 @@ def test_gemma_fused_add_rmsnorm_mxfp8_bitwise_eq_unfused(n, dtype):
assert torch.equal(got_res, ref_res)


def _ref_per_token_fp8(x: torch.Tensor, fp8_dtype: torch.dtype):
"""Per-token symmetric FP8 quant as AITER's dynamic_per_token_scaled_quant does
it: scale = amax * (1 / FP8_MAX), values multiplied by the reciprocal."""
fp8_max = torch.finfo(fp8_dtype).max
xf = x.float()
scale = xf.abs().amax(dim=-1, keepdim=True) * (1.0 / fp8_max)
q = xf * (1.0 / scale.clamp(min=torch.finfo(torch.float32).tiny))
return q.clamp(-fp8_max, fp8_max).to(fp8_dtype), scale


@pytest.mark.parametrize("m", [1, 7, 64, 2069])
@pytest.mark.parametrize("n", [3072, 6144])
@pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float16])
@torch.inference_mode()
def test_gemma_fused_add_rmsnorm_fp8_token_matches_unfused(m, n, dtype):
"""The per-token FP8 epilogue matches `gemma_fused_add_rmsnorm` followed by a
per-token FP8 quant: same residual and scales, FP8 values different in at most
1 in 10,000 elements (rounding of the reciprocal)."""
fp8 = current_platform.fp8_dtype()
torch.manual_seed(0)
w = torch.randn(n, device=DEVICE, dtype=dtype) * 0.5
x = torch.randn(m, n, device=DEVICE, dtype=dtype) * 2.0
res = torch.randn(m, n, device=DEVICE, dtype=dtype) * 8.0
x[:, :16] *= 40.0 # outlier channels
if m > 1:
x[m // 2] = 0
res[m // 2] = 0 # an all-zero row (padding): scale 0, values 0

got_q, got_scale, got_res = gemma_fused_add_rmsnorm_fp8_token(x, res, w, EPS, fp8)
normed, ref_res = gemma_fused_add_rmsnorm(x, res, w, EPS)
ref_q, ref_scale = _ref_per_token_fp8(normed, fp8)

assert got_q.shape == (m, n) and got_q.dtype == fp8
assert got_scale.shape == (m, 1) and got_scale.dtype == torch.float32
assert torch.equal(got_res, ref_res)
assert (got_scale == ref_scale).float().mean().item() >= 0.999
torch.testing.assert_close(got_scale, ref_scale, rtol=1e-2, atol=0)
assert not got_q.float().isnan().any()
differ = (got_q.view(torch.uint8) != ref_q.view(torch.uint8)).sum().item()
assert differ <= max(1, m * n // 10000)


@pytest.mark.parametrize("n", [2048, 6144])
@pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float16])
@torch.inference_mode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ def _stash(worker, s):

def _replay(worker):
"""Each captured sparse layer's FFN half, native and fused, on every rank."""
from vllm.distributed import tensor_model_parallel_all_reduce
from vllm.forward_context import set_forward_context
from vllm.model_executor.layers.fused_allreduce_gemma_rms_norm import (
fused_allreduce_gemma_rms_norm,
Expand All @@ -307,11 +308,16 @@ def _replay(worker):
for s, caps in worker._ffn_caps.items():
for i, (hidden, residual, part) in caps.items():
layer = layers[i]
_, residual = layer.input_layernorm(hidden, residual)
# The native model defers each FFN all-reduce into the next
# input norm: finish it there, and reduce this FFN's output
# the way the fused launch returns it.
_, residual = layer.forward_input_norm(hidden, residual)
x, want_res = fused_allreduce_gemma_rms_norm(
part, residual, layer.post_attention_layernorm
)
want = layer.block_sparse_moe(x)
if layer.ffn_all_reduce_deferred:
want = tensor_model_parallel_all_reduce(want)
slots = torch.arange(s, dtype=torch.int64, device=part.device)
got, got_res = executor.runner(part, residual, weights[i], slots)
results[(s, i)] = (*_metrics(got, want), torch.equal(got_res, want_res))
Expand Down
Loading