|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +""" |
| 4 | +NVFP4 quantization emulation for MoE. |
| 5 | +
|
| 6 | +This file implements NVFP4 emulation for NVFP4 MOE in case the hardware used does not |
| 7 | +natively support NVFP4 MOE. |
| 8 | +
|
| 9 | +Weights are dequantized on the fly during each forward, we fall back to calling |
| 10 | +`TritonExperts` using BF16, and fake NVFP4 quantize-dequantize |
| 11 | +is applied on `a13`, `a2`. |
| 12 | +""" |
| 13 | + |
| 14 | +import torch |
| 15 | + |
| 16 | +import vllm.model_executor.layers.fused_moe.modular_kernel as mk |
| 17 | +from vllm.logger import init_logger |
| 18 | +from vllm.model_executor.layers.fused_moe.activation import MoEActivation |
| 19 | +from vllm.model_executor.layers.fused_moe.config import ( |
| 20 | + FusedMoEConfig, |
| 21 | + FusedMoEQuantConfig, |
| 22 | +) |
| 23 | +from vllm.model_executor.layers.fused_moe.fused_moe import TritonExperts |
| 24 | +from vllm.model_executor.layers.fused_moe.utils import moe_kernel_quantize_input |
| 25 | +from vllm.model_executor.layers.quantization.utils.nvfp4_emulation_utils import ( |
| 26 | + dequantize_to_dtype, |
| 27 | +) |
| 28 | +from vllm.model_executor.layers.quantization.utils.quant_utils import ( |
| 29 | + QuantKey, |
| 30 | + kNvfp4Dynamic, |
| 31 | + kNvfp4Static, |
| 32 | +) |
| 33 | + |
| 34 | +logger = init_logger(__name__) |
| 35 | + |
| 36 | + |
| 37 | +class Nvfp4QuantizationEmulationTritonExperts(TritonExperts): |
| 38 | + """ |
| 39 | + Extension of TritonExperts to support emulated NVFP4 MoE experts. |
| 40 | +
|
| 41 | + It may be used for NVFP4 models when the device does not have |
| 42 | + native support for this dtype. |
| 43 | + """ |
| 44 | + |
| 45 | + def __init__( |
| 46 | + self, |
| 47 | + moe_config: FusedMoEConfig, |
| 48 | + quant_config: FusedMoEQuantConfig, |
| 49 | + ): |
| 50 | + super().__init__(moe_config, quant_config) |
| 51 | + logger.warning_once( |
| 52 | + "Using Nvfp4QuantizationEmulationTritonExperts MOE backend. This will" |
| 53 | + " dequantize weights on the fly and may be slower than native" |
| 54 | + " quantized MOE. Consider using a device with native quantization" |
| 55 | + " support (e.g. Nvidia Blackwell) for better performance." |
| 56 | + ) |
| 57 | + |
| 58 | + # `TritonExperts.apply` expects pre-dequantized weights, |
| 59 | + # which we handle in `apply` below. |
| 60 | + self.w1_scale_val = self.quant_config.w1_scale |
| 61 | + self.w2_scale_val = self.quant_config.w2_scale |
| 62 | + |
| 63 | + self.quant_config._w1.scale = None |
| 64 | + self.quant_config._w2.scale = None |
| 65 | + |
| 66 | + self.quantization_emulation = True |
| 67 | + |
| 68 | + @property |
| 69 | + def quant_dtype(self) -> torch.dtype | str | None: |
| 70 | + return "nvfp4" |
| 71 | + |
| 72 | + @property |
| 73 | + def expects_unquantized_inputs(self) -> bool: |
| 74 | + return True |
| 75 | + |
| 76 | + @staticmethod |
| 77 | + def _supports_quant_scheme( |
| 78 | + weight_key: QuantKey | None, |
| 79 | + activation_key: QuantKey | None, |
| 80 | + ) -> bool: |
| 81 | + return (weight_key, activation_key) == (kNvfp4Static, kNvfp4Dynamic) |
| 82 | + |
| 83 | + def apply( |
| 84 | + self, |
| 85 | + output: torch.Tensor, |
| 86 | + hidden_states: torch.Tensor, |
| 87 | + w1: torch.Tensor, |
| 88 | + w2: torch.Tensor, |
| 89 | + topk_weights: torch.Tensor, |
| 90 | + topk_ids: torch.Tensor, |
| 91 | + activation: MoEActivation, |
| 92 | + global_num_experts: int, |
| 93 | + expert_map: torch.Tensor | None, |
| 94 | + a1q_scale: torch.Tensor | None, |
| 95 | + a2_scale: torch.Tensor | None, |
| 96 | + workspace13: torch.Tensor, |
| 97 | + workspace2: torch.Tensor, |
| 98 | + expert_tokens_meta: mk.ExpertTokensMetadata | None, |
| 99 | + apply_router_weight_on_input: bool, |
| 100 | + ): |
| 101 | + """ |
| 102 | + Apply emulated quantized MoE computation. |
| 103 | +
|
| 104 | + This dequantizes the weights on the fly and calls fused_experts_impl |
| 105 | + with activation quantization support. |
| 106 | + """ |
| 107 | + # Dequantize weights if they are quantized |
| 108 | + # For NVFP4, weights are packed in uint8 format |
| 109 | + # w1 shape: [num_experts, 2*intermediate_size, hidden_size//2] |
| 110 | + # w2 shape: [num_experts, hidden_size, intermediate_size//2] |
| 111 | + assert w1.dtype == torch.uint8 |
| 112 | + assert w2.dtype == torch.uint8 |
| 113 | + |
| 114 | + # Dequantize w1 from packed NVFP4 to fp16/bf16 |
| 115 | + w13_global_scale = self.quant_config.g1_alphas |
| 116 | + |
| 117 | + w1_dequant = dequantize_to_dtype( |
| 118 | + tensor_fp4=w1, |
| 119 | + tensor_sf=self.w1_scale_val, |
| 120 | + global_scale=w13_global_scale, |
| 121 | + dtype=hidden_states.dtype, |
| 122 | + block_size=16, |
| 123 | + swizzle=False, |
| 124 | + ) |
| 125 | + |
| 126 | + # Dequantize w2 from packed NVFP4 to fp16/bf16 |
| 127 | + w2_global_scale = self.quant_config.g2_alphas |
| 128 | + |
| 129 | + w2_dequant = dequantize_to_dtype( |
| 130 | + tensor_fp4=w2, |
| 131 | + tensor_sf=self.w2_scale_val, |
| 132 | + global_scale=w2_global_scale, |
| 133 | + dtype=hidden_states.dtype, |
| 134 | + block_size=16, |
| 135 | + swizzle=False, |
| 136 | + ) |
| 137 | + |
| 138 | + hidden_states, _ = moe_kernel_quantize_input( |
| 139 | + A=hidden_states, |
| 140 | + A_scale=self.quant_config.a1_gscale, |
| 141 | + quant_dtype="nvfp4", |
| 142 | + per_act_token_quant=False, |
| 143 | + quantization_emulation=True, |
| 144 | + ) |
| 145 | + |
| 146 | + # Activation quantization/dequantization is deferred to |
| 147 | + # `moe_kernel_quantize_input` in TritonExperts.apply. |
| 148 | + super().apply( |
| 149 | + output=output, |
| 150 | + hidden_states=hidden_states, |
| 151 | + w1=w1_dequant, |
| 152 | + w2=w2_dequant, |
| 153 | + topk_weights=topk_weights, |
| 154 | + topk_ids=topk_ids, |
| 155 | + activation=activation, |
| 156 | + global_num_experts=global_num_experts, |
| 157 | + expert_map=expert_map, |
| 158 | + a1q_scale=None, |
| 159 | + a2_scale=self.quant_config.a2_gscale, |
| 160 | + workspace13=workspace13, |
| 161 | + workspace2=workspace2, |
| 162 | + expert_tokens_meta=expert_tokens_meta, |
| 163 | + apply_router_weight_on_input=apply_router_weight_on_input, |
| 164 | + ) |
0 commit comments