|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +"""Contract tests for the QuantizedActivation linear-kernel integration.""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import torch |
| 7 | + |
| 8 | +from vllm.model_executor.kernels.linear import ( |
| 9 | + _POSSIBLE_FP8_BLOCK_KERNELS, |
| 10 | + _POSSIBLE_FP8_KERNELS, |
| 11 | + _POSSIBLE_INT8_KERNELS, |
| 12 | + _POSSIBLE_NVFP4_KERNELS, |
| 13 | +) |
| 14 | +from vllm.model_executor.kernels.linear.nvfp4.base import ( |
| 15 | + NvFp4LinearKernel, |
| 16 | + NvFp4LinearLayerConfig, |
| 17 | +) |
| 18 | +from vllm.model_executor.kernels.linear.nvfp4.flashinfer import ( |
| 19 | + FlashInferCutlassNvFp4LinearKernel, |
| 20 | + FlashInferTrtllmNvFp4LinearKernel, |
| 21 | +) |
| 22 | +from vllm.model_executor.kernels.linear.scaled_mm.cutlass import ( |
| 23 | + CutlassFP8ScaledMMLinearKernel, |
| 24 | +) |
| 25 | +from vllm.model_executor.kernels.linear.scaled_mm.flashinfer import ( |
| 26 | + FlashInferFP8ScaledMMLinearKernel, |
| 27 | +) |
| 28 | +from vllm.model_executor.kernels.linear.scaled_mm.ScaledMMLinearKernel import ( |
| 29 | + FP8ScaledMMLinearLayerConfig, |
| 30 | + Int8ScaledMMLinearKernel, |
| 31 | + Int8ScaledMMLinearLayerConfig, |
| 32 | +) |
| 33 | +from vllm.model_executor.layers.fusion.quant_activation import ( |
| 34 | + QuantizedActivation, |
| 35 | + as_quantized_activation, |
| 36 | + expose_input_quant_key, |
| 37 | +) |
| 38 | +from vllm.model_executor.layers.quantization.utils.quant_utils import ( |
| 39 | + kFp8StaticTensorSym, |
| 40 | + kNvfp4Dynamic, |
| 41 | +) |
| 42 | +from vllm.platforms import current_platform |
| 43 | + |
| 44 | +# The only backends that consume a pre-quantized activation. |
| 45 | +SUPPORTING = { |
| 46 | + CutlassFP8ScaledMMLinearKernel, |
| 47 | + FlashInferFP8ScaledMMLinearKernel, |
| 48 | + FlashInferCutlassNvFp4LinearKernel, |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +def _all_kernel_classes() -> list[type]: |
| 53 | + seen: dict[type, None] = {} |
| 54 | + for registry in ( |
| 55 | + _POSSIBLE_FP8_KERNELS, |
| 56 | + _POSSIBLE_FP8_BLOCK_KERNELS, |
| 57 | + _POSSIBLE_INT8_KERNELS, |
| 58 | + _POSSIBLE_NVFP4_KERNELS, |
| 59 | + ): |
| 60 | + for kernels in registry.values(): |
| 61 | + for cls in kernels: |
| 62 | + seen.setdefault(cls, None) |
| 63 | + return list(seen) |
| 64 | + |
| 65 | + |
| 66 | +def _probe(cls: type): |
| 67 | + """A bare kernel instance with a plausible config, so input_quant_key() |
| 68 | + can be queried without the hardware-gated constructor.""" |
| 69 | + obj = cls.__new__(cls) # type: ignore[call-overload] |
| 70 | + if issubclass(cls, NvFp4LinearKernel): |
| 71 | + obj.config = NvFp4LinearLayerConfig() |
| 72 | + elif issubclass(cls, Int8ScaledMMLinearKernel): |
| 73 | + obj.config = Int8ScaledMMLinearLayerConfig( |
| 74 | + is_static_input_scheme=True, is_channelwise=False, input_symmetric=True |
| 75 | + ) |
| 76 | + else: |
| 77 | + obj.config = FP8ScaledMMLinearLayerConfig( |
| 78 | + weight_quant_key=kFp8StaticTensorSym, |
| 79 | + activation_quant_key=kFp8StaticTensorSym, |
| 80 | + weight_shape=(16, 16), |
| 81 | + input_dtype=torch.bfloat16, |
| 82 | + out_dtype=torch.bfloat16, |
| 83 | + ) |
| 84 | + return obj |
| 85 | + |
| 86 | + |
| 87 | +def _resolved_apply_weights(cls: type): |
| 88 | + for base in cls.__mro__: |
| 89 | + if "apply_weights" in base.__dict__: |
| 90 | + return base.__dict__["apply_weights"] |
| 91 | + raise AssertionError(f"{cls.__name__} has no apply_weights in its MRO") |
| 92 | + |
| 93 | + |
| 94 | +def test_only_known_backends_support_prequantized_input(): |
| 95 | + declarers = {c for c in _all_kernel_classes() if _probe(c).input_quant_key()} |
| 96 | + assert declarers == SUPPORTING |
| 97 | + |
| 98 | + |
| 99 | +def test_supporting_backend_declares_consume_via_helper(): |
| 100 | + for cls in SUPPORTING: |
| 101 | + fn = _resolved_apply_weights(cls) |
| 102 | + assert "as_quantized_activation" in fn.__code__.co_names, cls.__name__ |
| 103 | + |
| 104 | + |
| 105 | +def test_bridge_marks_supporting_and_skips_others(): |
| 106 | + supported = _probe(FlashInferCutlassNvFp4LinearKernel) |
| 107 | + layer = torch.nn.Module() |
| 108 | + expose_input_quant_key(layer, supported) |
| 109 | + assert layer.input_quant_key == kNvfp4Dynamic |
| 110 | + |
| 111 | + unsupported = _probe(FlashInferTrtllmNvFp4LinearKernel) |
| 112 | + assert unsupported.input_quant_key() is None |
| 113 | + layer = torch.nn.Module() |
| 114 | + expose_input_quant_key(layer, unsupported) |
| 115 | + assert not hasattr(layer, "input_quant_key") |
| 116 | + |
| 117 | + |
| 118 | +def test_as_quantized_activation_validates_key(): |
| 119 | + qa = QuantizedActivation( |
| 120 | + data=torch.zeros(2, 4, dtype=current_platform.fp8_dtype()), |
| 121 | + scale=torch.tensor(1.0), |
| 122 | + orig_dtype=torch.bfloat16, |
| 123 | + orig_shape=torch.Size([2, 4]), |
| 124 | + quant_key=kFp8StaticTensorSym, |
| 125 | + ) |
| 126 | + with pytest.raises(AssertionError): |
| 127 | + as_quantized_activation(qa, kNvfp4Dynamic) |
| 128 | + with pytest.raises(AssertionError): |
| 129 | + as_quantized_activation(qa, None) |
| 130 | + assert as_quantized_activation(torch.zeros(2, 4), kFp8StaticTensorSym) is None |
| 131 | + assert as_quantized_activation(qa, kFp8StaticTensorSym) is qa |
0 commit comments