| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Weight-layout normalization for the FlashInfer CuTeDSL NVFP4 MoE backend.""" |
| 4 | |
| 5 | import pytest |
| 6 | import torch |
| 7 | |
| 8 | from vllm.model_executor.layers.fused_moe.activation import MoEActivation |
| 9 | from vllm.model_executor.layers.quantization.utils.flashinfer_fp4_moe import ( |
| 10 | reorder_w13_to_w31_for_flashinfer_cutedsl, |
| 11 | ) |
| 12 | |
| 13 | _GATE = torch.tensor([[[1], [2], [3], [4]]]) |
| 14 | _UP = torch.tensor([[[10], [20], [30], [40]]]) |
| 15 | _EXPECTED = torch.cat([_UP, _GATE], dim=1) |
| 16 | |
| 17 | |
| 18 | def test_reorder_w13_swigluoai_interleaved(): |
| 19 | """gpt-oss w13 is [gate0, up0, gate1, ...] rather than packed [gate; up].""" |
| 20 | w13 = torch.empty(1, 8, 1, dtype=_GATE.dtype) |
| 21 | w13[:, 0::2] = _GATE |
| 22 | w13[:, 1::2] = _UP |
| 23 | |
| 24 | out, out_scale = reorder_w13_to_w31_for_flashinfer_cutedsl( |
| 25 | MoEActivation.SWIGLUOAI, w13, w13 + 100 |
| 26 | ) |
| 27 | |
| 28 | torch.testing.assert_close(out, _EXPECTED) |
| 29 | torch.testing.assert_close(out_scale, _EXPECTED + 100) |
| 30 | |
| 31 | |
| 32 | @pytest.mark.parametrize( |
| 33 | "activation", [MoEActivation.SILU, MoEActivation.SWIGLUOAI_UNINTERLEAVE] |
| 34 | ) |
| 35 | def test_reorder_w13_packed_layouts(activation: MoEActivation): |
| 36 | w13 = torch.cat([_GATE, _UP], dim=1) |
| 37 | |
| 38 | out, out_scale = reorder_w13_to_w31_for_flashinfer_cutedsl( |
| 39 | activation, w13, w13 + 100 |
| 40 | ) |
| 41 | |
| 42 | torch.testing.assert_close(out, _EXPECTED) |
| 43 | torch.testing.assert_close(out_scale, _EXPECTED + 100) |