repo
stringlengths
7
90
file_url
stringlengths
81
315
file_path
stringlengths
4
228
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 14:38:15
2026-01-05 02:33:18
truncated
bool
2 classes
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/gptq_marlin.py
vllm/model_executor/layers/quantization/gptq_marlin.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from copy import deepcopy from typing import Any, Optional import torch from safetensors.torch import _TYPES as _SAFETENSORS_TO_TORCH_DTYPE import vllm.model_executor.layers.fused_moe # noqa from vllm import _custom_ops as ops from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe.config import ( FusedMoEConfig, FusedMoEQuantConfig, ) from vllm.model_executor.layers.fused_moe.fused_marlin_moe import fused_marlin_moe from vllm.model_executor.layers.fused_moe.layer import ( FusedMoE, FusedMoEMethodBase, FusedMoeWeightScaleSupported, UnquantizedFusedMoEMethod, ) from vllm.model_executor.layers.linear import LinearMethodBase, set_weight_attrs from vllm.model_executor.layers.quantization import QuantizationMethods from vllm.model_executor.layers.quantization.base_config import ( QuantizationConfig, QuantizeMethodBase, ) from vllm.model_executor.layers.quantization.kernels.mixed_precision import ( MPLinearLayerConfig, choose_mp_linear_kernel, ) from vllm.model_executor.layers.quantization.utils import replace_parameter from vllm.model_executor.layers.quantization.utils.gptq_utils import ( get_dynamic_override, get_linear_quant_method, override_config, ) from vllm.model_executor.layers.quantization.utils.marlin_utils import ( check_marlin_supported, check_moe_marlin_supports_layer, get_marlin_input_dtype, marlin_act_int8_process_scales, marlin_make_workspace_new, marlin_moe_permute_scales, marlin_permute_bias, marlin_repeat_scales_on_all_ranks, verify_marlin_supported, ) from vllm.model_executor.parameter import ( ChannelQuantScaleParameter, GroupQuantScaleParameter, PackedColumnParameter, PackedvLLMParameter, RowvLLMParameter, ) from vllm.platforms import current_platform from vllm.scalar_type import scalar_types from vllm.transformers_utils.config import get_safetensors_params_metadata from vllm.utils.collection_utils import is_list_of logger = init_logger(__name__) def get_moe_quant_method( config: "GPTQMarlinConfig", layer: torch.nn.Module, prefix: str, moe_method_cls: type, ): cloned_config = deepcopy(config) if isinstance(layer, FusedMoE): # False = skip module, None = no override, else = Positive match if ( get_dynamic_override( # noqa: E712 cloned_config, # noqa: E712 layer_name=prefix, ) == False ): # noqa: E712 return UnquantizedFusedMoEMethod(layer.moe_config) if prefix: # Dynamic per module/layer rules may override base config override_config(cloned_config, prefix=prefix) return moe_method_cls(cloned_config, layer.moe_config) return None class GPTQMarlinConfig(QuantizationConfig): """Config class for GPTQ Marlin""" # (num_bits, is_sym) -> quant_type TYPE_MAP = { (4, True): scalar_types.uint4b8, (8, True): scalar_types.uint8b128, } def __init__( self, weight_bits: int, group_size: int, desc_act: bool, is_sym: bool, lm_head_quantized: bool, dynamic: dict[str, dict[str, int | bool]], full_config: dict[str, Any], modules_in_block_to_quantize: list[str] | None = None, ) -> None: super().__init__() if desc_act and group_size == -1: # In this case, act_order == True is the same as act_order == False # (since we have only one group per output channel) desc_act = False # GPTQModel use `dynamic` config property to allow per module # quantization config so each module can be individually optimized. # Format is dict[str, dict] where key is a regex string that can # perform both positive ("+:" prefixed) or negative ("-:" prefixed) # matching of a module. # Default to positive match, override base quant config mode, if no # prefix is used. Value is in dict format of field key and override # value. # Negative matching will skip quantization init for this module # entirely: # non-quantized inference. More details and quantization examples can be # found at: https://github.com/ModelCloud/GPTQModel # Example: # # last 1/2 of the layers 10-21 has 8bit vs 4bit for 0-9 # # last 1/4 of the layers 16-21 has 8bit and group_size 64 # dynamic = { # #`.*\.` matches the layers_node prefix # # positive match layer 10-15 # r"+:.*\.(?:1[0-5])\..*": {"bits": 8,}, # # positive match layer 16-21 # r"+:.*\.(?:1[6-9]|20|21)\..*": {"bits": 8, "group_size": 64,}, # r"-:.*\.moe\..*": {}, # negative match (skip) all `moe` layers # } self.dynamic = dynamic self.weight_bits = weight_bits self.is_sym = is_sym self.pack_factor = 32 // weight_bits # packed into int32 self.group_size = group_size self.desc_act = desc_act self.lm_head_quantized = lm_head_quantized self.full_config = full_config if (weight_bits, is_sym) not in self.TYPE_MAP: raise ValueError( f"Unsupported quantization config: bits={weight_bits}, sym={is_sym}" ) self.quant_type = self.TYPE_MAP[(weight_bits, is_sym)] self.modules_in_block_to_quantize = modules_in_block_to_quantize or [] # used to identify GPTQ model quantized by autoround self.autoround_version = full_config.get("autoround_version", "") def __repr__(self) -> str: return ( f"GPTQMarlinConfig(quant_type={self.quant_type}, " f"group_size={self.group_size}, " f"desc_act={self.desc_act}, " f"lm_head_quantized={self.lm_head_quantized}, " f"dynamic={self.dynamic}, " f"modules_in_block_to_quantize={self.modules_in_block_to_quantize})" ) @classmethod def get_name(cls) -> QuantizationMethods: return "gptq_marlin" @classmethod def get_supported_act_dtypes(cls) -> list[torch.dtype]: return [torch.half, torch.bfloat16] @classmethod def get_min_capability(cls) -> int: return 75 @classmethod def get_config_filenames(cls) -> list[str]: return ["quantize_config.json"] @classmethod def from_config(cls, config: dict[str, Any]) -> "GPTQMarlinConfig": dynamic = cls.get_from_keys_or(config, ["dynamic"], default={}) dynamic = {} if dynamic is None else dynamic weight_bits = cls.get_from_keys(config, ["bits"]) group_size = cls.get_from_keys(config, ["group_size"]) desc_act = cls.get_from_keys(config, ["desc_act"]) is_sym = cls.get_from_keys(config, ["sym"]) lm_head_quantized = cls.get_from_keys_or(config, ["lm_head"], default=False) modules_in_block_to_quantize = cls.get_from_keys_or( config, ["modules_in_block_to_quantize"], default=None ) return cls( weight_bits, group_size, desc_act, is_sym, lm_head_quantized, dynamic, config, modules_in_block_to_quantize, ) @classmethod def override_quantization_method( cls, hf_quant_cfg, user_quant ) -> QuantizationMethods | None: can_convert = cls.is_gptq_marlin_compatible(hf_quant_cfg) is_valid_user_quant = ( user_quant is None or user_quant == "marlin" or user_quant == "gptq_marlin" ) if can_convert and is_valid_user_quant: msg = ( "The model is convertible to {} during runtime." " Using {} kernel.".format(cls.get_name(), cls.get_name()) ) logger.info(msg) return cls.get_name() if can_convert and user_quant == "gptq": logger.info( "Detected that the model can run with gptq_marlin" ", however you specified quantization=gptq explicitly," " so forcing gptq. Use quantization=gptq_marlin for" " faster inference" ) return None def get_quant_method( self, layer: torch.nn.Module, prefix: str ) -> Optional["QuantizeMethodBase"]: if isinstance(layer, FusedMoE): from vllm.model_executor.layers.quantization.moe_wna16 import MoeWNA16Config if not check_moe_marlin_supports_layer(layer, self.group_size): logger.warning_once( f"Layer '{prefix}' is not supported by GPTQMoeMarlin. " "Falling back to Moe WNA16 kernels." ) return MoeWNA16Config.from_config(self.full_config).get_quant_method( layer, prefix ) moe_quant_method = get_moe_quant_method( self, layer, prefix, GPTQMarlinMoEMethod ) if moe_quant_method is None: return None moe_quant_method.input_dtype = get_marlin_input_dtype(prefix) return moe_quant_method quant_method = get_linear_quant_method( self, layer, prefix, GPTQMarlinLinearMethod ) if quant_method is None: return None quant_method.input_dtype = get_marlin_input_dtype(prefix) return quant_method @classmethod def is_gptq_marlin_compatible(cls, quant_config: dict[str, Any]): quant_method = quant_config.get("quant_method", "").lower() num_bits = quant_config.get("bits") group_size = quant_config.get("group_size") sym = quant_config.get("sym") desc_act = quant_config.get("desc_act") if not current_platform.is_cuda(): return False if quant_method != "gptq": return False # Marlin conversion is only valid if required properties are found if num_bits is None or group_size is None or sym is None or desc_act is None: return False if (num_bits, sym) not in cls.TYPE_MAP: return False return check_marlin_supported( quant_type=cls.TYPE_MAP[(num_bits, sym)], group_size=group_size ) def apply_vllm_mapper(self, hf_to_vllm_mapper): if self.modules_in_block_to_quantize is not None: self.modules_in_block_to_quantize = hf_to_vllm_mapper.apply_list( self.modules_in_block_to_quantize ) def maybe_update_config(self, model_name: str, revision: str | None = None): if self.modules_in_block_to_quantize: if is_list_of(self.modules_in_block_to_quantize, list): # original modules_in_block_to_quantize: list[list[str]] # flatten original modules_in_block_to_quantize self.modules_in_block_to_quantize = [ item for sublist in self.modules_in_block_to_quantize for item in sublist ] return unquant_dtypes = [torch.float16, torch.bfloat16, torch.float32] metadata = get_safetensors_params_metadata(model_name, revision=revision) quant_layers: set[str] = { param_name.rsplit(".", 1)[0] for param_name, info in metadata.items() if (dtype := info.get("dtype", None)) and _SAFETENSORS_TO_TORCH_DTYPE[dtype] not in unquant_dtypes } self.modules_in_block_to_quantize = list(quant_layers) class GPTQMarlinLinearMethod(LinearMethodBase): """Linear method for GPTQ Marlin. Args: quant_config: The GPTQ Marlin quantization config. """ _kernel_backends_being_used: set[str] = set() def __init__(self, quant_config: GPTQMarlinConfig) -> None: self.quant_config = quant_config self.input_dtype = None self.quant_type = self.quant_config.quant_type # Verify supported on platform. verify_marlin_supported( quant_type=self.quant_config.quant_type, group_size=self.quant_config.group_size, ) def create_weights( self, layer: torch.nn.Module, input_size_per_partition: int, output_partition_sizes: list[int], input_size: int, output_size: int, params_dtype: torch.dtype, **extra_weight_attrs, ) -> None: output_size_per_partition = sum(output_partition_sizes) is_row_parallel = input_size != input_size_per_partition weight_loader = extra_weight_attrs.get("weight_loader") input_dtype = self.input_dtype mp_linear_kernel_config = MPLinearLayerConfig( full_weight_shape=(input_size, output_size), partition_weight_shape=( input_size_per_partition, output_size_per_partition, ), weight_type=self.quant_config.quant_type, act_type=params_dtype if input_dtype is None else input_dtype, group_size=self.quant_config.group_size, zero_points=False, has_g_idx=self.quant_config.desc_act, ) kernel_type = choose_mp_linear_kernel(mp_linear_kernel_config) if kernel_type.__name__ not in self._kernel_backends_being_used: logger.info("Using %s for GPTQMarlinLinearMethod", kernel_type.__name__) self._kernel_backends_being_used.add(kernel_type.__name__) # Normalize group_size if self.quant_config.group_size != -1: group_size = self.quant_config.group_size else: group_size = input_size # Determine sharding if marlin_repeat_scales_on_all_ranks( self.quant_config.desc_act, self.quant_config.group_size, is_row_parallel ): # By setting scale_dim == None, weight_loader will # repeat the scales on each GPU in TP>1 case. scales_and_zp_input_dim = None scales_and_zp_size = input_size // group_size else: # By setting scale_dim == 0, weight_loader will # shard the scales in TP>1 case. scales_and_zp_input_dim = 0 scales_and_zp_size = input_size_per_partition // group_size # Quantized weights qweight = PackedvLLMParameter( data=torch.empty( input_size_per_partition // self.quant_config.pack_factor, output_size_per_partition, dtype=torch.int32, ), input_dim=0, output_dim=1, packed_dim=0, packed_factor=self.quant_config.pack_factor, weight_loader=weight_loader, ) # Activation order g_idx = RowvLLMParameter( data=torch.empty( input_size_per_partition, dtype=torch.int32, ), input_dim=0, weight_loader=weight_loader, ) qzeros_args = { "data": torch.empty( scales_and_zp_size, output_size_per_partition // self.quant_config.pack_factor, dtype=torch.int32, ), "weight_loader": weight_loader, } weight_scale_args = { "data": torch.empty( scales_and_zp_size, output_size_per_partition, dtype=params_dtype, ), "weight_loader": weight_loader, } if scales_and_zp_input_dim is None: scales = ChannelQuantScaleParameter(output_dim=1, **weight_scale_args) qzeros = PackedColumnParameter( output_dim=1, packed_dim=1, packed_factor=self.quant_config.pack_factor, **qzeros_args, ) else: scales = GroupQuantScaleParameter( output_dim=1, input_dim=0, **weight_scale_args ) qzeros = PackedvLLMParameter( input_dim=0, output_dim=1, packed_dim=1, packed_factor=self.quant_config.pack_factor, **qzeros_args, ) layer.register_parameter("qweight", qweight) layer.register_parameter("g_idx", g_idx) layer.register_parameter("scales", scales) layer.register_parameter("qzeros", qzeros) self.kernel = kernel_type( mp_linear_kernel_config, w_q_param_name="qweight", w_s_param_name="scales", w_zp_param_name="qzeros", w_gidx_param_name="g_idx", ) def process_weights_after_loading(self, layer: torch.nn.Module) -> None: self.kernel.process_weights_after_loading(layer) def apply( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: return self.kernel.apply_weights(layer, x, bias) class GPTQMarlinMoEMethod(FusedMoEMethodBase): """MoE Marlin method with quantization.""" def __init__( self, quant_config: GPTQMarlinConfig, moe: FusedMoEConfig, ) -> None: super().__init__(moe) self.quant_config = quant_config if self.quant_config.quant_type.size_bits == 4: self.quant_type = scalar_types.uint4b8 elif self.quant_config.quant_type.size_bits == 8: self.quant_type = scalar_types.uint8b128 else: raise ValueError("GPTQMarlinMoEMethod only supports int4 and int8 now.") self.input_dtype = None self.use_marlin = True def create_weights( self, layer: torch.nn.Module, num_experts: int, hidden_size: int, intermediate_size_per_partition: int, params_dtype: torch.dtype, **extra_weight_attrs, ): layer.input_dtype = self.input_dtype is_a_8bit = self.input_dtype is not None and self.input_dtype.itemsize == 1 if is_a_8bit: assert self.quant_type == scalar_types.uint4b8, ( "W8A8-INT8 is not supported by marlin kernel." ) intermediate_size_full = extra_weight_attrs.pop("intermediate_size_full") self.is_k_full = (not self.quant_config.desc_act) or ( intermediate_size_per_partition == intermediate_size_full ) if self.quant_config.group_size != -1: scales_size13 = hidden_size // self.quant_config.group_size w2_scales_size = ( intermediate_size_full if self.quant_config.desc_act else intermediate_size_per_partition ) scales_size2 = w2_scales_size // self.quant_config.group_size strategy = FusedMoeWeightScaleSupported.GROUP.value else: scales_size13 = 1 scales_size2 = 1 strategy = FusedMoeWeightScaleSupported.CHANNEL.value layer.num_groups_w13 = scales_size13 layer.num_groups_w2 = scales_size2 extra_weight_attrs.update({"quant_method": strategy, "is_transposed": True}) # Fused gate_up_proj (column parallel) w13_qweight = torch.nn.Parameter( torch.empty( num_experts, hidden_size // self.quant_config.pack_factor, 2 * intermediate_size_per_partition, dtype=torch.int32, ), requires_grad=False, ) layer.register_parameter("w13_qweight", w13_qweight) set_weight_attrs(w13_qweight, extra_weight_attrs) # down_proj (row parallel) w2_qweight = torch.nn.Parameter( torch.empty( num_experts, intermediate_size_per_partition // self.quant_config.pack_factor, hidden_size, dtype=torch.int32, ), requires_grad=False, ) layer.register_parameter("w2_qweight", w2_qweight) set_weight_attrs(w2_qweight, extra_weight_attrs) # up_proj scales w13_scales = torch.nn.Parameter( torch.empty( num_experts, scales_size13, 2 * intermediate_size_per_partition, dtype=params_dtype, ), requires_grad=False, ) layer.register_parameter("w13_scales", w13_scales) set_weight_attrs(w13_scales, extra_weight_attrs) # down_proj scales w2_scales = torch.nn.Parameter( torch.empty(num_experts, scales_size2, hidden_size, dtype=params_dtype), requires_grad=False, ) layer.register_parameter("w2_scales", w2_scales) set_weight_attrs(w2_scales, extra_weight_attrs) # don't shard the w2 scales when running act order set_weight_attrs(w2_scales, {"load_full_w2": self.quant_config.desc_act}) # up_proj scales w13_qzeros = torch.nn.Parameter( torch.empty( num_experts, scales_size13, 2 * intermediate_size_per_partition // self.quant_config.pack_factor, dtype=params_dtype, ), requires_grad=False, ) layer.register_parameter("w13_qzeros", w13_qzeros) set_weight_attrs(w13_qzeros, extra_weight_attrs) # down_proj scales w2_qzeros = torch.nn.Parameter( torch.empty( num_experts, scales_size2, hidden_size // self.quant_config.pack_factor, dtype=params_dtype, ), requires_grad=False, ) layer.register_parameter("w2_qzeros", w2_qzeros) set_weight_attrs(w2_qzeros, extra_weight_attrs) # don't shard the w2 scales when running act order set_weight_attrs(w2_qzeros, {"load_full_w2": self.quant_config.desc_act}) w13_g_idx = torch.nn.Parameter( torch.empty( num_experts, hidden_size, dtype=torch.int32, ), requires_grad=False, ) layer.register_parameter("w13_g_idx", w13_g_idx) set_weight_attrs(w13_g_idx, extra_weight_attrs) w2_g_idx = torch.nn.Parameter( torch.empty( num_experts, intermediate_size_per_partition, dtype=torch.int32, ), requires_grad=False, ) layer.register_parameter("w2_g_idx", w2_g_idx) set_weight_attrs(w2_g_idx, extra_weight_attrs) w13_g_idx_sort_indices = torch.nn.Parameter( torch.empty( num_experts, hidden_size, dtype=torch.int32, ), requires_grad=False, ) layer.register_parameter("w13_g_idx_sort_indices", w13_g_idx_sort_indices) set_weight_attrs(w13_g_idx_sort_indices, extra_weight_attrs) w2_g_idx_sort_indices = torch.nn.Parameter( torch.empty( num_experts, intermediate_size_per_partition, dtype=torch.int32, ), requires_grad=False, ) layer.register_parameter("w2_g_idx_sort_indices", w2_g_idx_sort_indices) set_weight_attrs(w2_g_idx_sort_indices, extra_weight_attrs) device = layer.w13_qweight.device layer.workspace = marlin_make_workspace_new(device, 4) def process_weights_after_loading(self, layer: torch.nn.Module) -> None: is_a_8bit = self.input_dtype is not None and self.input_dtype.itemsize == 1 if is_a_8bit: assert self.quant_type == scalar_types.uint4b8, ( "W8A8-INT8 is not supported by marlin kernel." ) if self.input_dtype == torch.float8_e4m3fn: ops.marlin_int4_fp8_preprocess(layer.w13_qweight, inplace=True) ops.marlin_int4_fp8_preprocess(layer.w2_qweight, inplace=True) layer.w13_scales.data = layer.w13_scales.data * 512 layer.w2_scales.data = layer.w2_scales.data * 512 # Process act_order if self.quant_config.desc_act: # Get sorting based on g_idx num_experts = layer.w13_g_idx.shape[0] w13_g_idx_sort_indices = torch.empty_like(layer.w13_g_idx) w2_g_idx_sort_indices = torch.empty_like(layer.w2_g_idx) w13_sorted_g_idx = torch.empty_like(layer.w13_g_idx) w2_sorted_g_idx = torch.empty_like(layer.w2_g_idx) for e in range(num_experts): w13_g_idx_sort_indices[e] = torch.argsort(layer.w13_g_idx[e]).to( torch.int32 ) w2_g_idx_sort_indices[e] = torch.argsort(layer.w2_g_idx[e]).to( torch.int32 ) w13_sorted_g_idx[e] = layer.w13_g_idx[e][w13_g_idx_sort_indices[e]] w2_sorted_g_idx[e] = layer.w2_g_idx[e][w2_g_idx_sort_indices[e]] replace_parameter(layer, "w13_g_idx", w13_sorted_g_idx) replace_parameter(layer, "w2_g_idx", w2_sorted_g_idx) replace_parameter(layer, "w13_g_idx_sort_indices", w13_g_idx_sort_indices) replace_parameter(layer, "w2_g_idx_sort_indices", w2_g_idx_sort_indices) else: # Reset g_idx related tensors num_experts = layer.w13_g_idx.shape[0] device = layer.w13_g_idx.device layer.w13_g_idx = torch.nn.Parameter( torch.empty((num_experts, 0), dtype=torch.int32, device=device), requires_grad=False, ) layer.w2_g_idx = torch.nn.Parameter( torch.empty((num_experts, 0), dtype=torch.int32, device=device), requires_grad=False, ) layer.w13_g_idx_sort_indices = torch.nn.Parameter( torch.empty((num_experts, 0), dtype=torch.int32, device=device), requires_grad=False, ) layer.w2_g_idx_sort_indices = torch.nn.Parameter( torch.empty((num_experts, 0), dtype=torch.int32, device=device), requires_grad=False, ) # Repack weights marlin_w13_qweight = ops.gptq_marlin_moe_repack( layer.w13_qweight, layer.w13_g_idx_sort_indices, layer.w13_qweight.shape[1] * self.quant_config.pack_factor, layer.w13_qweight.shape[2], self.quant_config.quant_type.size_bits, is_a_8bit=is_a_8bit, ) replace_parameter(layer, "w13_qweight", marlin_w13_qweight) marlin_w2_qweight = ops.gptq_marlin_moe_repack( layer.w2_qweight, layer.w2_g_idx_sort_indices, layer.w2_qweight.shape[1] * self.quant_config.pack_factor, layer.w2_qweight.shape[2], self.quant_config.quant_type.size_bits, is_a_8bit=is_a_8bit, ) replace_parameter(layer, "w2_qweight", marlin_w2_qweight) # The modular kernel expects w13_weight and w2_weight, # but GPTQ uses w13_qweight and w2_qweight # Alias for modular kernel layer.w13_weight = layer.w13_qweight # Alias for modular kernel layer.w2_weight = layer.w2_qweight # Repack scales marlin_w13_scales = marlin_moe_permute_scales( s=layer.w13_scales, size_k=layer.intermediate_size_per_partition, size_n=layer.w13_scales.shape[2], group_size=self.quant_config.group_size, is_a_8bit=is_a_8bit, ) if self.input_dtype == torch.int8 and layer.num_groups_w13 > 1: marlin_w13_scales, w13_input_global_scale = marlin_act_int8_process_scales( marlin_w13_scales ) layer.register_parameter( "w13_input_global_scale", torch.nn.Parameter(w13_input_global_scale, requires_grad=False), ) replace_parameter(layer, "w13_scales", marlin_w13_scales) marlin_w2_scales = marlin_moe_permute_scales( s=layer.w2_scales, size_k=layer.w2_scales.shape[1] * ( self.quant_config.group_size if self.quant_config.group_size != -1 else self.quant_config.pack_factor ), size_n=layer.w2_scales.shape[2], group_size=self.quant_config.group_size, is_a_8bit=is_a_8bit, ) if self.input_dtype == torch.int8 and layer.num_groups_w2 > 1: marlin_w2_scales, w2_input_global_scale = marlin_act_int8_process_scales( marlin_w2_scales ) layer.register_parameter( "w2_input_global_scale", torch.nn.Parameter(w2_input_global_scale, requires_grad=False), ) replace_parameter(layer, "w2_scales", marlin_w2_scales) if hasattr(layer, "w13_bias") and layer.w13_bias is not None: layer.w13_bias.data = marlin_permute_bias(layer.w13_bias) if hasattr(layer, "w2_bias") and layer.w2_bias is not None: layer.w2_bias.data = marlin_permute_bias(layer.w2_bias) def get_fused_moe_quant_config( self, layer: torch.nn.Module ) -> FusedMoEQuantConfig | None: from vllm.model_executor.layers.fused_moe.config import ( gptq_marlin_moe_quant_config, ) return gptq_marlin_moe_quant_config( w1_scale=layer.w13_scales, w2_scale=layer.w2_scales, weight_bits=self.quant_config.weight_bits, group_size=self.quant_config.group_size, w1_zp=getattr(layer, "w13_qzeros", None) if not self.quant_config.is_sym else None, w2_zp=getattr(layer, "w2_qzeros", None) if not self.quant_config.is_sym else None, w1_bias=getattr(layer, "w13_bias", None), w2_bias=getattr(layer, "w2_bias", None), ) def select_gemm_impl( self, prepare_finalize, layer: torch.nn.Module, ): """ Select the GEMM implementation for GPTQ-Marlin MoE. Returns MarlinExperts configured for GPTQ quantization. This is ONLY used when LoRA is enabled. Without LoRA, GPTQ uses its own apply() method. """ # Only use modular kernels when LoRA is enabled # Without LoRA, GPTQ's own apply() method works fine and is more efficient if not self.moe.is_lora_enabled: raise NotImplementedError( "GPTQ-Marlin uses its own apply() method when LoRA is not enabled. " "Modular kernels are only used for LoRA support." ) # The modular marlin kernels do not support 8-bit weights. if self.quant_config.weight_bits == 8: raise NotImplementedError( "GPTQ-Marlin kernel does not support 8-bit weights." ) from vllm.model_executor.layers.fused_moe import modular_kernel as mk from vllm.model_executor.layers.fused_moe.fused_marlin_moe import ( BatchedMarlinExperts, MarlinExperts, ) # Ensure quant config is initialized assert self.moe_quant_config is not None, ( "moe_quant_config must be initialized before select_gemm_impl" ) w13_g_idx = ( getattr(layer, "w13_g_idx", None) if self.quant_config.desc_act else None ) w2_g_idx = ( getattr(layer, "w2_g_idx", None) if self.quant_config.desc_act else None ) w13_g_idx_sort_indices = ( getattr(layer, "w13_g_idx_sort_indices", None) if self.quant_config.desc_act else None ) w2_g_idx_sort_indices = ( getattr(layer, "w2_g_idx_sort_indices", None) if self.quant_config.desc_act else None ) # Check if using batched expert format (for Expert Parallelism) if ( prepare_finalize.activation_format == mk.FusedMoEActivationFormat.BatchedExperts ): # For batched format, use BatchedMarlinExperts max_num_tokens_per_rank = prepare_finalize.max_num_tokens_per_rank() assert max_num_tokens_per_rank is not None
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
true
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/bitblas.py
vllm/model_executor/layers/quantization/bitblas.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from typing import Any, Optional import torch from packaging import version from vllm.logger import init_logger from vllm.model_executor.layers.linear import LinearBase, LinearMethodBase from vllm.model_executor.layers.quantization import ( QuantizationConfig, QuantizationMethods, ) from vllm.model_executor.layers.quantization.utils.bitblas_utils import ( BITBLAS_OPTIMIZE_FEATURES, BITBLAS_SUPPORTED_NUM_BITS, BITBLAS_SUPPORTED_SYM, MINIMUM_BITBLAS_VERSION, ) from vllm.model_executor.layers.vocab_parallel_embedding import ParallelLMHead from vllm.model_executor.parameter import ( BasevLLMParameter, ChannelQuantScaleParameter, GroupQuantScaleParameter, PackedvLLMParameter, ) from vllm.model_executor.utils import set_weight_attrs logger = init_logger(__name__) class BitBLASConfig(QuantizationConfig): """Config class for BitBLAS. Reference: https://github.com/Microsoft/BitBLAS """ TORCH_DTYPE = torch.float16 STORAGE_DTYPE = "int8" # assume int8 storage TORCH_STORAGE_DTYPE = getattr(torch, STORAGE_DTYPE) # "original" or "rescale" or "quantized", # gptq_with_bitblas prefer "quantized implementation" ZEROS_MODE = "quantized" def __init__( self, weight_bits: int, group_size: int | None, desc_act: bool | None, is_sym: bool | None, quant_method: str | None, lm_head_quantized: bool, ) -> None: try: import bitblas if version.parse(bitblas.__version__) < version.parse( MINIMUM_BITBLAS_VERSION ): raise ImportError( "bitblas version is wrong. Please " f"install bitblas>={MINIMUM_BITBLAS_VERSION}" ) except ImportError as e: bitblas_import_exception = e raise ValueError( "Trying to use the bitblas backend, but could not import" f"with the following error: {bitblas_import_exception}. " "Please install bitblas through the following command: " f"`pip install bitblas>={MINIMUM_BITBLAS_VERSION}`" ) from bitblas_import_exception if desc_act and group_size == -1: # In this case, act_order == True is the same as act_order == False # (since we have only one group per output channel) desc_act = False super().__init__() self.weight_bits = weight_bits self.group_size = group_size self.desc_act = desc_act self.is_sym = is_sym self.quant_method = quant_method self.lm_head_quantized = lm_head_quantized # Verify if self.weight_bits not in BITBLAS_SUPPORTED_NUM_BITS: raise ValueError( f"BitBLAS does not support weight_bits = {self.weight_bits}. " f"Only weight_bits = {BITBLAS_SUPPORTED_NUM_BITS} " "are supported." ) if self.is_sym not in BITBLAS_SUPPORTED_SYM: raise ValueError( f"BitBLAS does not support is_sym = {self.is_sym}. " f"Only sym = {BITBLAS_SUPPORTED_SYM} are supported." ) storage_dtype = self.STORAGE_DTYPE storage_nbit = int("".join(c for c in storage_dtype if c.isdigit())) self.storage_dtype = storage_dtype self.storage_torch_dtype = self.TORCH_STORAGE_DTYPE # 4 Bits packed into 32 bit datatype. self.pack_factor = storage_nbit // weight_bits self.nbits = weight_bits # Zeros type for the quantized weights. self.zeros_mode = self.ZEROS_MODE def __repr__(self) -> str: return ( f"BitBLASConfig(weight_bits={self.weight_bits}, " f"group_size={self.group_size}, " f"desc_act={self.desc_act}, " f"is_sym={self.is_sym}, " f"quant_method={self.quant_method})" ) @classmethod def get_name(cls) -> QuantizationMethods: return "bitblas" @classmethod def get_supported_act_dtypes(cls) -> list[torch.dtype]: return [torch.half, torch.bfloat16] @classmethod # Need to figure it out def get_min_capability(cls) -> int: return 70 @classmethod def get_config_filenames(cls) -> list[str]: return ["quantize_config.json"] @staticmethod def get_from_keys( config: dict[str, Any], keys: list[str], default: Any = None ) -> Any: """Get a value from the model's quantization config.""" for key in keys: if key in config: return config[key] return default @classmethod def from_config(cls, config: dict[str, Any]) -> "BitBLASConfig": weight_bits = cls.get_from_keys(config, ["bits"]) group_size = cls.get_from_keys(config, ["group_size"], -1) desc_act = cls.get_from_keys(config, ["desc_act"], False) is_sym = cls.get_from_keys(config, ["sym"], False) quant_method = cls.get_from_keys(config, ["quant_method"]) lm_head_quantized = cls.get_from_keys_or(config, ["lm_head"], default=False) return cls( weight_bits, group_size, desc_act, is_sym, quant_method, lm_head_quantized ) @classmethod def override_quantization_method( cls, hf_quant_cfg, user_quant ) -> QuantizationMethods | None: # compat: autogptq >=0.8.0 use checkpoint_format: str # compat: autogptq <=0.7.1 is_bitblas_format: bool is_bitblas_format = hf_quant_cfg.get( "checkpoint_format" ) == "bitblas" or hf_quant_cfg.get("is_bitblas_format", False) is_valid_user_quant = ( user_quant is None or user_quant == "gptq" or user_quant == "bitblas" ) if is_bitblas_format and is_valid_user_quant: msg = "The model is serialized in {} format. Using {} kernel.".format( cls.get_name(), cls.get_name() ) logger.info(msg) return cls.get_name() return None def get_quant_method( self, layer: torch.nn.Module, prefix: str ) -> Optional["BitBLASLinearMethod"]: if isinstance(layer, LinearBase) or ( isinstance(layer, ParallelLMHead) and self.lm_head_quantized ): return BitBLASLinearMethod(self) return None class BitBLASLinearMethod(LinearMethodBase): """Linear method for BitBLAS. Args: quant_config: The BitBLAS quantization config. """ # USE BITBLAS_OPTIMIZE_FEATURES_CONTIGUOUS # Instead of BITBLAS_OPTIMIZE_FEATURES # If you want to high contiguous batching # performance OPT_FEATURES = BITBLAS_OPTIMIZE_FEATURES ENABLE_TUNING = True BITBLAS_DTYPES = { torch.float32: "float32", torch.float16: "float16", torch.bfloat16: "bfloat16", torch.half: "float16", torch.int8: "int8", } def __init__(self, quant_config: BitBLASConfig): self.quant_config = quant_config def create_weights_gptq( self, layer: torch.nn.Module, input_size_per_partition: int, output_partition_sizes: list[int], input_size: int, output_size: int, params_dtype: torch.dtype, **extra_weight_attrs, ) -> None: """Creates quantized weights for use in linear operations. The function initializes and returns a dictionary containing quantized weights, scales, and zeros for performing quantized matrix multiplication operations. Args: input_size_per_partition: The size of the input partition. output_partition_sizes: List of output partition sizes. input_size: The total size of the input (unused). output_size: The total size of the output (unused). params_dtype: The data type of the parameters (expected to be torch.float16). Returns: A dictionary containing the quantized weights ('qweight'), scales ('scales'), and zeros ('zeros'). Raises: ValueError: If `params_dtype` is not `torch.float16` or if the input size per partition is not divisible by the group size in `quant_config`. """ del input_size, output_size # Unused arguments. weight_loader = extra_weight_attrs["weight_loader"] if params_dtype not in self.quant_config.get_supported_act_dtypes(): raise ValueError( f"Parameter data type must be torch.float16, but got {params_dtype}" ) group_size = self.quant_config.group_size if group_size is None: group_size = -1 # Validate output_size_per_partition output_size_per_partition = sum(output_partition_sizes) if group_size != -1 and input_size_per_partition % group_size != 0: raise ValueError( f"Input size per partition ({input_size_per_partition}) must " f"be divisible by group size ({group_size})." ) # Initialize or retrieve the BitBLAS matrix multiplication operator. self._configure_bitblas_matmul( input_size_per_partition, output_size_per_partition, params_dtype=params_dtype, enable_tuning=self.ENABLE_TUNING, bias=False, layout="nt", bits=self.quant_config.weight_bits, ) # Initialize quantized weights with dimensions # Quantized 4Bit weights packed. qweight = PackedvLLMParameter( data=torch.empty( self.bitblas_matmul.retrieve_weight_shape(), device="cuda", dtype=self.quant_config.storage_torch_dtype, requires_grad=False, ), input_dim=1, output_dim=0, packed_dim=1, packed_factor=self.quant_config.pack_factor, bitblas_tile_size=( self.bitblas_matmul.retrieve_weight_shape()[-2] if self.bitblas_matmul.propagate_b else None ), weight_loader=weight_loader, ) # Compute the number of input groups for channel-wise quantization. input_groups = 1 if group_size == -1 else input_size_per_partition // group_size # Initialize scales and zeros for the quantized weights. weight_scale_args = { "data": torch.empty( output_size_per_partition, input_groups, device="cuda", dtype=params_dtype, ), "weight_loader": weight_loader, } if input_groups == 1: scales = ChannelQuantScaleParameter(output_dim=0, **weight_scale_args) else: scales = GroupQuantScaleParameter( output_dim=0, input_dim=1, **weight_scale_args ) if self.quant_config.zeros_mode == "quantized": zeros = PackedvLLMParameter( data=torch.empty( input_groups, output_size_per_partition // self.quant_config.pack_factor, device="cuda", dtype=self.quant_config.storage_torch_dtype, requires_grad=False, ), input_dim=0, output_dim=1, packed_dim=1, packed_factor=self.quant_config.pack_factor, weight_loader=weight_loader, ) else: zeros = BasevLLMParameter( torch.empty( output_size_per_partition, input_groups, device="cuda", dtype=params_dtype, ), weight_loader=weight_loader, ) # Set attributes to indicate how scales and zeros are applied. set_weight_attrs( zeros, { "input_dim": None if input_groups == 1 else 1, "output_dim": 0, }, ) layer.register_parameter("qweight", qweight) layer.register_parameter("scales", scales) layer.register_parameter("zeros", zeros) def create_weights( self, layer: torch.nn.Module, input_size_per_partition: int, output_partition_sizes: list[int], input_size: int, output_size: int, params_dtype: torch.dtype, **extra_weight_attrs, ): if self.quant_config.quant_method == "gptq": return self.create_weights_gptq( layer, input_size_per_partition, output_partition_sizes, input_size, output_size, params_dtype, **extra_weight_attrs, ) else: raise ValueError( f"Unsupported quant_method {self.quant_config.quant_method}" ) def _configure_bitblas_matmul( self, infeatures, outfeatures, params_dtype, enable_tuning, bias, layout, bits, out_dtype="float16", ): from bitblas import MatmulConfig bitblas_dtype = self.BITBLAS_DTYPES[params_dtype] with_scaling = False with_zeros = False group_size = self.quant_config.group_size zeros_mode = self.quant_config.zeros_mode if self.quant_config.quant_method == "gptq": with_scaling = True with_zeros = True W_dtype = f"uint{bits}" if self.quant_config.is_sym: with_zeros = False W_dtype = f"int{bits}" else: raise ValueError( f"Unsupported quant_method {self.quant_config.quant_method}" ) matmul_config = MatmulConfig( N=outfeatures, K=infeatures, A_dtype=bitblas_dtype, W_dtype=W_dtype, out_dtype=out_dtype, accum_dtype="int32" if bitblas_dtype == "int8" else bitblas_dtype, storage_dtype=self.quant_config.STORAGE_DTYPE, with_scaling=with_scaling, with_zeros=with_zeros, group_size=group_size, with_bias=bias, layout=layout, zeros_mode=zeros_mode, ) self.bitblas_matmul = self._get_or_create_bitblas_operator( matmul_config, enable_tuning ) def _get_or_create_bitblas_operator(self, config, enable_tuning): from bitblas import Matmul, auto_detect_nvidia_target from bitblas.cache import get_database_path, global_operator_cache BITBLAS_DATABASE_PATH = get_database_path() BITBLAS_TARGET = auto_detect_nvidia_target() if global_operator_cache.size() == 0: global_operator_cache.load_from_database( BITBLAS_DATABASE_PATH, BITBLAS_TARGET ) bitblas_matmul = global_operator_cache.get(config) if bitblas_matmul is None: bitblas_matmul = Matmul(config, target=BITBLAS_TARGET, enable_tuning=False) if enable_tuning: TUNING_MESSAGE = f"BitBLAS Operator {config} is tuning ..." logger.info(TUNING_MESSAGE) bitblas_matmul.hardware_aware_finetune(topk=20) global_operator_cache.add(config, bitblas_matmul) global_operator_cache.save_into_database( BITBLAS_DATABASE_PATH, BITBLAS_TARGET ) TUNED_MESSAGE = ( f"BitBLAS Operator {config} tuned and saved to database." ) logger.info(TUNED_MESSAGE) else: _message = f"BitBLAS Operator {config} created." logger.info(_message) else: _message = f"BitBLAS Operator {config} found in global_operator_cache." logger.info(_message) return bitblas_matmul def apply_gptq( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: qweight = layer.qweight scales = layer.scales qzeros = layer.zeros x_2d = x.view(-1, x.shape[-1]) if self.quant_config.is_sym: output_2d = self.bitblas_matmul(x_2d, qweight, scales) else: output_2d = self.bitblas_matmul(x_2d, qweight, scales, qzeros) output = output_2d.view(x.shape[:-1] + (output_2d.shape[1],)) if bias is not None: output.add_(bias) # In-place add return output def apply( self, *args: Any, **kwargs: Any, ) -> torch.Tensor: if self.quant_config.quant_method == "gptq": return self.apply_gptq(*args, **kwargs) else: raise ValueError( f"Unsupported quant_method {self.quant_config.quant_method}" )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/fbgemm_fp8.py
vllm/model_executor/layers/quantization/fbgemm_fp8.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from typing import Any, Optional import torch from torch.nn import Module from torch.nn.parameter import Parameter from vllm.logger import init_logger from vllm.model_executor.layers.linear import ( LinearBase, LinearMethodBase, UnquantizedLinearMethod, ) from vllm.model_executor.layers.quantization import QuantizationMethods from vllm.model_executor.layers.quantization.base_config import ( QuantizationConfig, QuantizeMethodBase, ) from vllm.model_executor.layers.quantization.utils.marlin_utils_fp8 import ( apply_fp8_marlin_linear, prepare_fp8_layer_for_marlin, ) from vllm.model_executor.layers.quantization.utils.quant_utils import ( GroupShape, is_layer_skipped, ) from vllm.model_executor.layers.quantization.utils.w8a8_utils import ( Fp8LinearOp, maybe_create_device_identity, normalize_e4m3fn_to_e4m3fnuz, ) from vllm.model_executor.parameter import ( ChannelQuantScaleParameter, ModelWeightParameter, ) from vllm.platforms import current_platform logger = init_logger(__name__) class FBGEMMFp8Config(QuantizationConfig): """Config class for FBGEMM Fp8.""" def __init__(self, ignore_list: list[str], input_scale_ub: float): super().__init__() self.ignore_list = ignore_list if ignore_list else [] self.input_scale_ub = input_scale_ub # For GPUs that lack FP8 hardware support, we can leverage the Marlin # kernel for fast weight-only FP8 quantization self.use_marlin = not current_platform.has_device_capability(89) @classmethod def get_name(cls) -> QuantizationMethods: return "fbgemm_fp8" @classmethod def get_supported_act_dtypes(cls) -> list[torch.dtype]: return [torch.bfloat16, torch.float16] @classmethod def get_min_capability(cls) -> int: return 80 @classmethod def get_config_filenames(cls) -> list[str]: return [] @classmethod def from_config(cls, config: dict[str, Any]) -> "FBGEMMFp8Config": ignore_list = cls.get_from_keys(config, ["modules_to_not_convert"]) input_scale_ub = cls.get_from_keys(config, ["activation_scale_ub"]) return cls(ignore_list=ignore_list, input_scale_ub=input_scale_ub) def get_quant_method( self, layer: torch.nn.Module, prefix: str ) -> Optional["QuantizeMethodBase"]: if isinstance(layer, LinearBase): if is_layer_skipped( prefix=prefix, ignored_layers=self.ignore_list, fused_mapping=self.packed_modules_mapping, ): return UnquantizedLinearMethod() return FBGEMMFp8LinearMethod(self) return None class FBGEMMFp8LinearMethod(LinearMethodBase): def __init__(self, quant_config: FBGEMMFp8Config): self.quant_config = quant_config self.fp8_linear = Fp8LinearOp( act_quant_static=False, act_quant_group_shape=GroupShape.PER_TOKEN ) self.out_dtype = torch.get_default_dtype() def create_weights( self, layer: torch.nn.Module, input_size_per_partition: int, output_partition_sizes: list[int], input_size: int, output_size: int, params_dtype: torch.dtype, **extra_weight_attrs, ): maybe_create_device_identity() weight_loader = extra_weight_attrs.get("weight_loader") del input_size, output_size output_size_per_partition = sum(output_partition_sizes) layer.logical_widths = output_partition_sizes layer.input_size_per_partition = input_size_per_partition layer.output_size_per_partition = output_size_per_partition layer.orig_dtype = params_dtype # WEIGHT weight = ModelWeightParameter( data=torch.empty( output_size_per_partition, input_size_per_partition, dtype=torch.float8_e4m3fn, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight", weight) # WEIGHT SCALE weight_scale = ChannelQuantScaleParameter( data=torch.empty((sum(output_partition_sizes), 1), dtype=torch.float32), output_dim=0, weight_loader=weight_loader, ) weight_scale[:] = torch.finfo(torch.float32).min layer.register_parameter("weight_scale", weight_scale) # INPUT SCALE UPPER BOUND input_scale_ub = torch.nn.Parameter( torch.tensor((self.quant_config.input_scale_ub), dtype=torch.float32), requires_grad=False, ) layer.input_scale_ub = input_scale_ub def process_weights_after_loading(self, layer: Module) -> None: # required by torch.compile layer.weight_scale = Parameter(layer.weight_scale.data, requires_grad=False) layer.weight = Parameter(layer.weight.data, requires_grad=False) weight = layer.weight if current_platform.is_fp8_fnuz(): weight, weight_scale, input_scale = normalize_e4m3fn_to_e4m3fnuz( weight=weight, weight_scale=layer.weight_scale, input_scale=None ) if input_scale is not None: layer.input_scale = Parameter(input_scale, requires_grad=False) layer.weight_scale = Parameter(weight_scale, requires_grad=False) layer.weight = Parameter(weight.t(), requires_grad=False) if self.quant_config.use_marlin: prepare_fp8_layer_for_marlin(layer) # Activations not quantized for marlin. del layer.input_scale_ub def apply( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: if self.quant_config.use_marlin: return apply_fp8_marlin_linear( input=x, weight=layer.weight, weight_scale=layer.weight_scale, workspace=layer.workspace, size_n=layer.output_size_per_partition, size_k=layer.input_size_per_partition, bias=bias, ) return self.fp8_linear.apply( input=x, weight=layer.weight, weight_scale=layer.weight_scale, out_dtype=self.out_dtype, input_scale=None, input_scale_ub=layer.input_scale_ub, bias=bias, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/tpu_int8.py
vllm/model_executor/layers/quantization/tpu_int8.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from typing import Any, Optional import torch from torch.nn import Module from torch.nn.parameter import Parameter from vllm.model_executor.layers.linear import LinearBase, LinearMethodBase from vllm.model_executor.layers.quantization import ( QuantizationConfig, QuantizationMethods, ) from vllm.model_executor.parameter import ModelWeightParameter ACTIVATION_SCHEMES = ["none", "dynamic"] class Int8TpuConfig(QuantizationConfig): """Int8 Quantization Config class for TPU Backend.""" def __init__( self, activation_scheme: str = "none", ) -> None: super().__init__() if activation_scheme not in ACTIVATION_SCHEMES: raise ValueError(f"Unsupported activation scheme {activation_scheme}") self.activation_scheme = activation_scheme def get_name(self) -> QuantizationMethods: return "tpu_int8" def get_supported_act_dtypes(self) -> list[torch.dtype]: return [torch.float16, torch.bfloat16] @classmethod def get_min_capability(cls) -> int: raise NotImplementedError("This function should not be called with TPU Backend") @staticmethod def get_config_filenames() -> list[str]: return [] @classmethod def from_config(cls, config: dict[str, Any]) -> "Int8TpuConfig": activation_scheme = cls.get_from_keys(config, ["activation_scheme"]) return cls(activation_scheme=activation_scheme) def get_quant_method( self, layer: Module, prefix: str ) -> Optional["TPUInt8LinearMethod"]: if isinstance(layer, LinearBase): return TPUInt8LinearMethod(self) return None class TPUInt8LinearMethod(LinearMethodBase): """Int8 Linear method for TPU Quant.""" def __init__(self, quant_config: Int8TpuConfig): self.quant_config = quant_config self.quantize_activation = False if self.quant_config.activation_scheme == "dynamic": self.quantize_activation = True def create_weights( self, layer: Module, input_size_per_partition: int, output_partition_sizes: list[int], input_size: int, output_size: int, params_dtype: torch.dtype, **extra_weight_attrs, ): weight_loader = extra_weight_attrs.get("weight_loader") weight = ModelWeightParameter( data=torch.empty( sum(output_partition_sizes), input_size_per_partition, dtype=params_dtype, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight", weight) def _quantize_weight( self, weight: torch.Tensor ) -> tuple[torch.Tensor, torch.Tensor]: weight_dtype = weight.dtype weight = weight.cpu().to(torch.float32) n_bit = 8 eps = 1e-5 max_int = 2 ** (n_bit - 1) - 1 min_int = -(2 ** (n_bit - 1)) max_val = weight.abs().amax(dim=-1, keepdim=True) max_val = max_val.clamp(min=eps) qscale = max_val / max_int qweight = torch.clamp( torch.round(weight * (1.0 / qscale)), min_int, max_int ).to(torch.int8) qscale = qscale.squeeze().to(weight_dtype) return qweight, qscale def process_weights_after_loading(self, layer: Module) -> None: layer.weight = Parameter(layer.weight.data, requires_grad=False) device = layer.weight.device qweight, qscale = self._quantize_weight(layer.weight) qweight = qweight.to(device) qscale = qscale.to(device) layer.weight = Parameter(qweight, requires_grad=False) layer.scale = Parameter(qscale, requires_grad=False) def apply( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: try: import torch_xla.experimental.custom_kernel # noqa: F401 except ImportError as err: raise ImportError( "Please install torch_xla by following the instructions at " "https://docs.vllm.ai/en/latest/getting_started/tpu-installation.html " # noqa: E501 "to run vLLM on TPU." ) from err weight = layer.weight scale = layer.scale out = torch.ops.xla.quantized_matmul_int8( x, weight, scale, quantize_activation=self.quantize_activation ) if bias is not None: out = out + bias return out
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/ptpc_fp8.py
vllm/model_executor/layers/quantization/ptpc_fp8.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from typing import Any, Optional import torch from torch.nn.parameter import Parameter from vllm import _custom_ops as ops from vllm.attention.layer import Attention from vllm.logger import init_logger from vllm.model_executor.layers.linear import LinearBase, UnquantizedLinearMethod from vllm.model_executor.layers.quantization import QuantizationMethods from vllm.model_executor.layers.quantization.base_config import QuantizeMethodBase from vllm.model_executor.layers.quantization.fp8 import ( Fp8Config, Fp8KVCacheMethod, Fp8LinearMethod, ) from vllm.model_executor.layers.quantization.utils.quant_utils import ( GroupShape, is_layer_skipped, ) from vllm.model_executor.layers.quantization.utils.w8a8_utils import Fp8LinearOp from vllm.platforms import current_platform ACTIVATION_SCHEMES = ["static", "dynamic"] logger = init_logger(__name__) class PTPCFp8Config(Fp8Config): """Config class for Per-Token-Per-Channel Dynamic Quantization Fp8.""" def __init__( self, activation_scheme: str = "dynamic", ignored_layers: list[str] | None = None, ) -> None: if not current_platform.is_rocm(): raise ValueError("ptpc_fp8 quantization is supported only on ROCm.") if not current_platform.has_device_capability(94): raise ValueError( "ptpc_fp8 quantization is supported only on AMD Instinct MI300 GPUs and newer." # noqa: E501 ) if activation_scheme == "static": raise ValueError("ptpc_fp8 as of now only support dynamic quantization.") super().__init__( is_checkpoint_fp8_serialized=False, activation_scheme=activation_scheme, ignored_layers=ignored_layers, ) @classmethod def get_name(cls) -> QuantizationMethods: return "ptpc_fp8" @classmethod def from_config(cls, config: dict[str, Any]) -> "PTPCFp8Config": activation_scheme = cls.get_from_keys(config, ["activation_scheme"]) ignored_layers = cls.get_from_keys_or(config, ["ignored_layers"], None) return cls(activation_scheme=activation_scheme, ignored_layers=ignored_layers) def get_quant_method( self, layer: torch.nn.Module, prefix: str ) -> Optional["QuantizeMethodBase"]: if isinstance(layer, LinearBase): if is_layer_skipped(prefix, self.ignored_layers): return UnquantizedLinearMethod() return PTPCFp8LinearMethod(self) elif isinstance(layer, Attention): return Fp8KVCacheMethod(self) return None class PTPCFp8LinearMethod(Fp8LinearMethod): """Linear method for Per-Token and Per-Channel FP8 Quantization. Only supports loading quantized BF16 model checkpoints with dynamic activation scaling. To load FP16 model checkpoints, user must specify to convert the FP16 model weight loading into BF16. The weight scaling factor will be initialized after the model weights are loaded. Limitations: 1. Only support float8_e4m3fnuz data type due to the limitation of torch._scaled_mm (https://github.com/ROCm/pytorch/blob/8c0504d7f3fb0ee4c278c096a5c3caedb01129fa/aten/src/ATen/native/cuda/Blas.cpp#L1041) Args: quant_config: The quantization config. """ def __init__(self, quant_config: PTPCFp8Config): assert current_platform.is_rocm(), ( "PTPCFp8LinearMethod is only supported on ROCm." ) super().__init__(quant_config=quant_config) # Force weight quantization self.quant_config.is_checkpoint_fp8_serialized = False self.fp8_linear = Fp8LinearOp( act_quant_static=False, act_quant_group_shape=GroupShape.PER_TOKEN ) def process_weights_after_loading(self, layer: torch.nn.Module) -> None: layer.weight = torch.nn.Parameter(layer.weight.data, requires_grad=False) assert layer.weight.data.dtype == torch.bfloat16, ( f"Currently torch._scaled_mm (hipBLASLt) rowwise gemm only support output dtype of bfloat16. {str(layer.weight.data.dtype)} is specified." # noqa: E501 ) # Quantize the weights. qweight, weight_scale = ops.scaled_fp8_quant( layer.weight, scale=None, use_per_token_if_dynamic=True ) # Update the layer with the new values. layer.weight = Parameter( qweight.t(), requires_grad=False ) # Pretranspose the weight layer.weight_scale = Parameter(weight_scale, requires_grad=False) layer.input_scale = None def apply( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: return self.fp8_linear.apply( input=x, weight=layer.weight, weight_scale=layer.weight_scale, input_scale=None, input_scale_ub=None, bias=bias, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/petit.py
vllm/model_executor/layers/quantization/petit.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/model_executor/layers/quantization/modelopt.py from typing import Any, Optional import regex as re import torch from torch.nn.parameter import Parameter from vllm.attention.layer import Attention from vllm.logger import init_logger from vllm.model_executor.layers.linear import ( LinearBase, LinearMethodBase, UnquantizedLinearMethod, ) from vllm.model_executor.layers.quantization import QuantizationMethods from vllm.model_executor.layers.quantization.base_config import ( QuantizationConfig, QuantizeMethodBase, ) from vllm.model_executor.layers.quantization.kv_cache import BaseKVCacheMethod from vllm.model_executor.layers.quantization.utils.petit_utils import ( apply_petit_nvfp4_linear, prepare_nvfp4_layer_for_petit, verify_petit_nvfp4_supported, ) from vllm.model_executor.layers.quantization.utils.quant_utils import is_layer_skipped from vllm.model_executor.parameter import ModelWeightParameter, PerTensorScaleParameter from vllm.platforms import current_platform # Initialize logger for the module logger = init_logger(__name__) # Configuration class to support the NVFP4 quantized model # generated by the ModelOpt quantization tool class PetitNvFp4Config(QuantizationConfig): """Config class for Petit FP4.""" def __init__( self, is_checkpoint_nvfp4_serialized: bool = False, kv_cache_quant_algo: str | None = None, group_size: int | None = None, exclude_modules: list[str] | None = None, ) -> None: self._check_hardware_support() self.is_checkpoint_nvfp4_serialized = is_checkpoint_nvfp4_serialized if is_checkpoint_nvfp4_serialized: logger.warning( "Detected nvfp4 checkpoint. Please note that the " "format is experimental and subject to change." ) self.group_size = group_size self.kv_cache_quant_algo = kv_cache_quant_algo self.exclude_modules = exclude_modules def _check_hardware_support(self) -> None: """ Verifies that the current hardware is supported by the Petit backend. This backend is specifically designed for AMD GPUs and is not supported on the CUDA platform. """ # This check ensures the code is NOT running on an NVIDIA GPU. if current_platform.is_cuda(): raise ValueError( "The 'petit' quantization backend is designed for AMD GPUs " "and is not supported on the CUDA platform. For NVIDIA GPUs, " "please use a different quantization method such as FP8, AWQ, " "or GPTQ." ) @classmethod def get_name(cls) -> QuantizationMethods: return "petit_nvfp4" @classmethod def get_supported_act_dtypes(cls) -> list[torch.dtype]: return [torch.bfloat16, torch.half] @classmethod def get_min_capability(cls) -> int: # Petit supports the gfx90a and gfx942 GPUs return 90 @classmethod def get_config_filenames(cls) -> list[str]: return ["hf_quant_config.json"] @classmethod def from_config(cls, config: dict[str, Any]) -> "PetitNvFp4Config": qc = cls.get_from_keys(config, ["quantization"]) quant_method_raw = qc.get("quant_algo") if not isinstance(quant_method_raw, str) or not quant_method_raw: raise ValueError("Missing or invalid 'quant_algo' in quantization config.") quant_method = quant_method_raw.upper() group_size_raw = qc.get("group_size") if not isinstance(group_size_raw, int): raise ValueError( "Missing or invalid 'group_size' (int) in hf_quant_config.json." ) group_size = group_size_raw verify_petit_nvfp4_supported(quant_method, group_size) kv_cache_quant_algo_raw = qc.get("kv_cache_quant_algo") or "auto" if not isinstance(kv_cache_quant_algo_raw, str): raise ValueError("'kv_cache_quant_algo' must be a string if provided.") kv_cache_quant_algo = kv_cache_quant_algo_raw exclude_raw = qc.get("exclude_modules", []) if exclude_raw is None: exclude_modules: list[str] = [] elif isinstance(exclude_raw, list) and all( isinstance(x, str) for x in exclude_raw ): exclude_modules = exclude_raw else: raise ValueError("'exclude_modules' must be a list[str] (or omitted).") is_checkpoint_nvfp4_serialized = "NVFP4" in quant_method return cls( is_checkpoint_nvfp4_serialized=is_checkpoint_nvfp4_serialized, kv_cache_quant_algo=kv_cache_quant_algo, group_size=group_size, exclude_modules=exclude_modules, ) @classmethod def override_quantization_method( cls, hf_quant_cfg, user_quant ) -> QuantizationMethods | None: if not current_platform.is_rocm(): return None qc = hf_quant_cfg.get("quantization", hf_quant_cfg) algo = (qc.get("quant_algo") or qc.get("quant_method") or "").upper() if algo in ("NVFP4", "MODELOPT_FP4", "MODELOPT"): return cls.get_name() # "petit_nvfp4" return None @classmethod def is_petit_nvfp4_compatible(cls, quant_config: dict[str, Any]) -> bool: qc = quant_config.get("quantization", quant_config) algo = (qc.get("quant_algo") or qc.get("quant_method") or "").upper() return algo == "NVFP4" def is_layer_excluded(self, prefix: str, exclude_modules: list[str]) -> bool: for pattern in exclude_modules: regex_str = pattern.replace(".", r"\.").replace("*", r".*") if re.fullmatch(regex_str, prefix): return True return False def get_quant_method( self, layer: torch.nn.Module, prefix: str ) -> Optional["QuantizeMethodBase"]: exclude = self.require_exclude_modules() if isinstance(layer, LinearBase): if is_layer_skipped(prefix, exclude) or self.is_layer_excluded( prefix, exclude ): return UnquantizedLinearMethod() return PetitNvFp4LinearMethod(self) elif isinstance(layer, Attention): return PetitFp8KVCacheMethod(self) return None def get_scaled_act_names(self) -> list[str]: return [] def require_group_size(self) -> int: if self.group_size is None: logger.warning("group_size not set; defaulting to 16 for NVFP4.") return 16 return self.group_size def require_kv_cache_quant_algo(self) -> str: return self.kv_cache_quant_algo or "auto" def require_exclude_modules(self) -> list[str]: return list(self.exclude_modules or []) class PetitFp8KVCacheMethod(BaseKVCacheMethod): """ Supports loading kv-cache scaling factors from FP8 checkpoints. """ def __init__(self, quant_config: PetitNvFp4Config): super().__init__(quant_config) class PetitNvFp4LinearMethod(LinearMethodBase): """Linear method for NVFP4. Supports loading NVFP4 checkpoints with the following structure: |Tensor Name | datatype | shape | |----------------------------------------------------| |input_scale | torch.float32 | scalar | |weight | NVFP4(SE2M1) | [1, X, y/2] | |weight_scale | FP8-E4M3 | [X, Y] | |weight_scale_2 | torch.float32 | scalar | The weights are quantized per block of 16 elements. Args: quant_config: The ModelOpt quantization config. """ def __init__(self, quant_config: PetitNvFp4Config): self.quant_config = quant_config def create_weights( self, layer: torch.nn.Module, input_size_per_partition: int, output_partition_sizes: list[int], input_size: int, output_size: int, params_dtype: torch.dtype, **extra_weight_attrs, ): del input_size, output_size if not self.quant_config.is_checkpoint_nvfp4_serialized: raise ValueError( "NVFP4 quantization was selected, " " dynamic quantization is not supported." ) output_size_per_partition = sum(output_partition_sizes) weight_loader = extra_weight_attrs.get("weight_loader") layer.logical_widths = output_partition_sizes layer.input_size_per_partition = input_size_per_partition layer.output_size_per_partition = output_size_per_partition if input_size_per_partition % 16 != 0: raise ValueError( "Unsupported model when in features size is not multiple of 16" ) weight_dtype = ( torch.float8_e4m3fn if self.quant_config.is_checkpoint_nvfp4_serialized else params_dtype ) weight = ModelWeightParameter( data=torch.empty( # 2 fp4 data is packed in one uint8 in the input dimension output_size_per_partition, input_size_per_partition // 2, dtype=torch.uint8, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight", weight) input_scale = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) layer.register_parameter("input_scale", input_scale) weight_scale_2 = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) layer.register_parameter("weight_scale_2", weight_scale_2) group_size = self.quant_config.require_group_size() weight_scale = ModelWeightParameter( data=torch.empty( output_size_per_partition, input_size_per_partition // group_size, dtype=weight_dtype, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight_scale", weight_scale) def process_weights_after_loading(self, layer: torch.nn.Module) -> None: input_scale_2 = layer.input_scale.max().to(torch.float32) weight_scale_2 = layer.weight_scale_2.max().to(torch.float32) layer.input_scale = Parameter(input_scale_2, requires_grad=False) layer.weight_scale_2 = Parameter(weight_scale_2, requires_grad=False) layer.alpha = Parameter( layer.input_scale * layer.weight_scale_2, requires_grad=False ) prepare_nvfp4_layer_for_petit(layer) del layer.input_scale def apply( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: return apply_petit_nvfp4_linear( input=x, weight=layer.weight, weight_scale=layer.weight_scale, weight_scale_2=layer.weight_scale_2, size_n=layer.output_size_per_partition, size_k=layer.input_size_per_partition, bias=bias, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/inc.py
vllm/model_executor/layers/quantization/inc.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # # Intel Gaudi supports quantization of various modules and functions, # including, but not limited to `Linear`, `KVCache`, `Matmul` and `Softmax`. # During model loading, # INC will patch layers with quantization/dequantization operators. # Meanwhile, INC will convert original weight to target datatype # and loading to target device. # static scaling should be provided through Quant_CONFIG: # `QUANT_CONFIG` is an environment variable, # that points to the measurement or quantization JSON config file. # The measurement configuration file is used during the calibration procedure, # to collect measurements for a given model. # The quantization configuration is used during inference. # For more information, please refer to: # https://docs.habana.ai/en/v1.21.1/PyTorch/vLLM_Inference/vLLM_FP8_Inference.html from typing import Any, Optional import torch from vllm.model_executor.layers.fused_moe.layer import ( FusedMoE, UnquantizedFusedMoEMethod, ) from vllm.model_executor.layers.linear import LinearBase, UnquantizedLinearMethod from vllm.model_executor.layers.quantization import QuantizationMethods from vllm.model_executor.layers.quantization.base_config import ( QuantizationConfig, QuantizeMethodBase, ) class INCConfig(QuantizationConfig): """Config class for FP8 using Intel Neural Compressor.""" @classmethod def get_name(cls) -> QuantizationMethods: return "inc" @classmethod def get_supported_act_dtypes(cls) -> list[torch.dtype]: return [torch.bfloat16] @classmethod def from_config(cls, config: dict[str, Any]) -> "INCConfig": raise AssertionError def get_quant_method( self, layer: torch.nn.Module, prefix: str ) -> Optional["QuantizeMethodBase"]: if isinstance(layer, LinearBase): return UnquantizedLinearMethod() elif isinstance(layer, FusedMoE): return UnquantizedFusedMoEMethod(layer.moe_config) return None @classmethod def get_min_capability(cls) -> int: raise AssertionError @staticmethod def get_config_filenames() -> list[str]: return []
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/auto_round.py
vllm/model_executor/layers/quantization/auto_round.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from fractions import Fraction from typing import TYPE_CHECKING, Any import regex as re import torch from vllm.logger import init_logger from vllm.model_executor.layers.linear import LinearBase, UnquantizedLinearMethod from vllm.model_executor.layers.quantization import ( QuantizationConfig, QuantizationMethods, ) from vllm.model_executor.layers.vocab_parallel_embedding import ParallelLMHead from vllm.platforms import current_platform from vllm.scalar_type import scalar_types if TYPE_CHECKING: from vllm.model_executor.models.utils import WeightsMapper logger = init_logger(__name__) class AutoRoundConfig(QuantizationConfig): """Config class for AutoRound. Reference: https://arxiv.org/pdf/2309.05516 """ SUPPORTED_BITS = {2, 3, 4, 8} SUPPORTED_DTYPES = {"int"} SUPPORTED_FORMATS = {"auto_round:auto_gptq", "auto_round:auto_awq"} SUPPORTED_BACKENDS = { "auto", "gptq", "gptq:marlin", "awq", "awq:marlin", "marlin", "ipex", } def __init__( self, weight_bits: int, group_size: int, sym: bool = True, packing_format: str = "auto_round:auto_gptq", block_name_to_quantize: str | list[str] | None = None, extra_config: dict[str, Any] | None = None, data_type: str = "int", backend: str = "auto", ) -> None: super().__init__() if weight_bits not in self.SUPPORTED_BITS: raise ValueError( f"Unsupported weight_bits: {weight_bits}, " f"currently only support {self.SUPPORTED_BITS}" ) if data_type not in self.SUPPORTED_DTYPES: raise ValueError( f"Unsupported data_type: {data_type}," f" currently only support {self.SUPPORTED_DTYPES}" ) if packing_format not in self.SUPPORTED_FORMATS: raise ValueError( f"Unsupported packing_format: {packing_format}, " f"currently only support {self.SUPPORTED_FORMATS}" ) if backend not in self.SUPPORTED_BACKENDS: raise ValueError( f"Unsupported backend: {backend}, " f"currently only support {self.SUPPORTED_BACKENDS}" ) self.weight_bits = weight_bits self.group_size = group_size self.sym = sym self.packing_format = packing_format self.block_name_to_quantize = ( block_name_to_quantize.split(",") if isinstance(block_name_to_quantize, str) else block_name_to_quantize ) self.extra_config = extra_config self.data_type = data_type self.backend = backend self.pack_factor = Fraction(32, weight_bits) def __repr__(self) -> str: return ( f"AutoRoundConfig(weight_bits={self.weight_bits}, " f"group_size={self.group_size}, sym={self.sym})" ) @classmethod def get_name(cls) -> QuantizationMethods: return "auto-round" @classmethod def get_supported_act_dtypes(cls) -> list[torch.dtype]: return [torch.half, torch.bfloat16] @classmethod def get_min_capability(cls) -> int: return 60 @classmethod def get_config_filenames(cls) -> list[str]: return ["quantization_config.json"] @classmethod def from_config(cls, config: dict[str, Any]) -> "AutoRoundConfig": return cls( weight_bits=cls.get_from_keys(config, ["bits"]), group_size=cls.get_from_keys(config, ["group_size"]), sym=cls.get_from_keys(config, ["sym"]), packing_format=cls.get_from_keys_or( config, ["packing_format"], "auto_round:auto_gptq" ), block_name_to_quantize=cls.get_from_keys_or( config, ["block_name_to_quantize", "to_quant_block_names"], None ), extra_config=cls.get_from_keys_or(config, ["extra_config"], None), data_type=cls.get_from_keys_or(config, ["data_type"], "int"), backend=cls.get_from_keys_or(config, ["backend", "vllm_backend"], "auto"), ) def get_layer_config(self, layer, layer_name: str): def get_config(name: str, quantized: bool = True): if not self.extra_config: return ( self.weight_bits if quantized else 16, self.group_size if quantized else -1, self.sym if quantized else True, ) # exact match first if name in self.extra_config: cfg = self.extra_config[name] return ( cfg.get("bits", self.weight_bits if quantized else 16), cfg.get("group_size", self.group_size if quantized else -1), cfg.get("sym", self.sym if quantized else True), ) REGEX_SPECIAL_CHARS = set(r"*+?^$()[]{}|\\") for pattern, cfg in self.extra_config.items(): if not isinstance(pattern, str) or not any( c in REGEX_SPECIAL_CHARS for c in pattern ): continue try: if re.search(re.compile(pattern), name) is not None: return ( cfg.get("bits", self.weight_bits if quantized else 16), cfg.get("group_size", self.group_size if quantized else -1), cfg.get("sym", self.sym if quantized else True), ) except re.error: # Invalid regex, ignore. continue return ( self.weight_bits if quantized else 16, self.group_size if quantized else -1, self.sym if quantized else True, ) # 1. Exact match from config if self.extra_config and layer_name in self.extra_config: return get_config(layer_name) # 2. Determine whether layer should be quantized quantized = not isinstance(layer, ParallelLMHead) if self.block_name_to_quantize: quantized = any( layer_name.startswith(name) for name in self.block_name_to_quantize ) # 3. Handle fused MoE if self.extra_config and "fusedmoe" in layer.__class__.__name__.lower(): moe_configs = [ get_config(name, quantized) for name in self.extra_config if name.startswith(layer_name) ] if moe_configs: if len(set(moe_configs)) == 1: return moe_configs[0] raise ValueError( f"Fused MoE layer '{layer_name}' requires " f"consistent quant config for all sub-layers" ) # 4. Handle fused QKV or other patterns if self.extra_config: for fusion_key, sub_keys in self.packed_modules_mapping.items(): if fusion_key in layer_name and layer_name.count(fusion_key) == 1: sub_names = [ layer_name.replace(fusion_key, sub_key) for sub_key in sub_keys ] sub_configs = [get_config(name, quantized) for name in sub_names] if len(set(sub_configs)) == 1: return sub_configs[0] raise ValueError( f"Fused module '{layer_name}' requires " f"consistent quant config for {sub_names}" ) # 5. Fallback or try a regular expression match return get_config(layer_name, quantized) def check_quantized(self, weight_bits: int) -> bool: return weight_bits < 16 def apply_vllm_mapper(self, hf_to_vllm_mapper: "WeightsMapper"): if self.block_name_to_quantize is not None: self.block_name_to_quantize = hf_to_vllm_mapper.apply_list( self.block_name_to_quantize ) if self.extra_config is not None: self.extra_config = hf_to_vllm_mapper.apply_dict(self.extra_config) def apply_awq_quant_layer(self, layer, prefix: str, backend: str = "auto"): from vllm.model_executor.layers.fused_moe import FusedMoE from vllm.model_executor.layers.quantization.utils.marlin_utils import ( check_marlin_supported, check_moe_marlin_supports_layer, ) weight_bits, group_size, sym = self.get_layer_config(layer, prefix) if not self.check_quantized(weight_bits): if isinstance(layer, (LinearBase, ParallelLMHead)): return UnquantizedLinearMethod() else: return None logger.debug( "[%s] Type: %s, Bits: %s, Group Size: %s, Sym: %s", prefix, layer.__class__.__name__, weight_bits, group_size, sym, ) if backend == "auto" or "marlin" in backend: AWQ_TYPE_MAP = { 4: scalar_types.uint4, 8: scalar_types.uint8, } use_marlin = (weight_bits in AWQ_TYPE_MAP) and check_marlin_supported( AWQ_TYPE_MAP[weight_bits], group_size, not sym ) if isinstance(layer, FusedMoE): use_marlin = use_marlin and check_moe_marlin_supports_layer( layer, group_size ) else: use_marlin = False if use_marlin: from vllm.model_executor.layers.quantization.awq_marlin import ( AWQMarlinConfig, AWQMarlinLinearMethod, AWQMarlinMoEMethod, ) quant_args_marlin = AWQMarlinConfig( weight_bits=weight_bits, group_size=group_size, zero_point=not sym, lm_head_quantized=False, full_config={}, modules_to_not_convert=[], ) else: from vllm.model_executor.layers.quantization.awq import ( AWQConfig, AWQLinearMethod, ) quant_args = AWQConfig( weight_bits=weight_bits, group_size=group_size, zero_point=not sym, ) if isinstance(layer, FusedMoE): if use_marlin: return AWQMarlinMoEMethod(quant_args_marlin, layer.moe) from vllm.model_executor.layers.quantization.moe_wna16 import MoeWNA16Config config = { "quant_method": "awq", "bits": weight_bits, "group_size": group_size, "zero_point": not sym, "lm_head": False, } return MoeWNA16Config.from_config(config).get_quant_method(layer, prefix) if isinstance(layer, (LinearBase, ParallelLMHead)): if use_marlin: return AWQMarlinLinearMethod(quant_args_marlin) else: return AWQLinearMethod(quant_args) return None def apply_gptq_quant_layer(self, layer, prefix: str, backend: str = "auto"): from vllm.model_executor.layers.fused_moe import FusedMoE from vllm.model_executor.layers.quantization.utils.marlin_utils import ( check_marlin_supported, check_moe_marlin_supports_layer, ) weight_bits, group_size, sym = self.get_layer_config(layer, prefix) if not self.check_quantized(weight_bits): if isinstance(layer, (LinearBase, ParallelLMHead)): return UnquantizedLinearMethod() else: return None logger.debug( "[%s] Type: %s, Bits: %s, Group Size: %s, Sym: %s", prefix, layer.__class__.__name__, weight_bits, group_size, sym, ) if backend == "auto" or "marlin" in backend: GPTQ_TYPE_MAP = { (4, True): scalar_types.uint4b8, (8, True): scalar_types.uint8b128, } use_marlin = (weight_bits, sym) in GPTQ_TYPE_MAP and check_marlin_supported( GPTQ_TYPE_MAP[(weight_bits, sym)], group_size, has_zp=not sym ) if isinstance(layer, FusedMoE): use_marlin = use_marlin and check_moe_marlin_supports_layer( layer, group_size ) else: use_marlin = False if use_marlin: from vllm.model_executor.layers.quantization.gptq_marlin import ( GPTQMarlinConfig, GPTQMarlinLinearMethod, GPTQMarlinMoEMethod, ) quant_args_marlin = GPTQMarlinConfig( weight_bits=weight_bits, group_size=group_size, is_sym=sym, lm_head_quantized=False, desc_act=False, dynamic={}, full_config={}, ) else: from vllm.model_executor.layers.quantization.gptq import ( GPTQConfig, GPTQLinearMethod, ) quant_args = GPTQConfig( weight_bits=weight_bits, group_size=group_size, lm_head_quantized=False, desc_act=False, dynamic={}, ) if isinstance(layer, FusedMoE): if use_marlin: return GPTQMarlinMoEMethod(quant_args_marlin, layer.moe_config) else: from vllm.model_executor.layers.quantization.moe_wna16 import ( MoeWNA16Config, ) config = { "quant_method": "gptq", "bits": weight_bits, "group_size": group_size, "sym": sym, "lm_head": False, } return MoeWNA16Config.from_config(config).get_quant_method( layer, prefix ) if isinstance(layer, (LinearBase, ParallelLMHead)): if use_marlin: return GPTQMarlinLinearMethod(quant_args_marlin) else: return GPTQLinearMethod(quant_args) return None def apply_ipex_quant_layer(self, layer, prefix: str): weight_bits, group_size, sym = self.get_layer_config(layer, prefix) if not self.check_quantized(weight_bits): if isinstance(layer, (LinearBase, ParallelLMHead)): return UnquantizedLinearMethod() else: return None from vllm.model_executor.layers.quantization.ipex_quant import ( IPEXAWQLinearMethod, IPEXConfig, IPEXGPTQLinearMethod, ) if isinstance(layer, (LinearBase, ParallelLMHead)): if "awq" in self.packing_format: config = IPEXConfig( method="awq", weight_bits=weight_bits, group_size=group_size ) return IPEXAWQLinearMethod(config) elif "gptq" in self.packing_format: config = IPEXConfig( method="gptq", weight_bits=weight_bits, group_size=group_size ) return IPEXGPTQLinearMethod(config) else: raise ValueError( f"ipex backend only supports awq " f"and gtpq format,but got {self.packing_format}" ) else: return None def get_quant_method(self, layer: torch.nn.Module, prefix: str): if prefix and self.extra_config: for layer_name in self.extra_config: if ( layer_name == prefix or layer_name == f"model.{prefix}" ) and self.extra_config[layer_name].get("bits", 16) >= 16: return UnquantizedLinearMethod() if ( current_platform.is_cpu() or current_platform.is_xpu() or self.backend == "ipex" ): return self.apply_ipex_quant_layer(layer, prefix) if "gptq" in self.packing_format or "gptq" in self.backend: return self.apply_gptq_quant_layer(layer, prefix) if "awq" in self.packing_format or "awq" in self.backend: return self.apply_awq_quant_layer(layer, prefix)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/mxfp4.py
vllm/model_executor/layers/quantization/mxfp4.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from enum import Enum from typing import Optional import torch from torch.nn.parameter import Parameter from vllm import envs from vllm.attention.layer import Attention from vllm.config import get_current_vllm_config from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe import ( FusedMoE, FusedMoEConfig, FusedMoEMethodBase, ) from vllm.model_executor.layers.fused_moe import modular_kernel as mk from vllm.model_executor.layers.fused_moe.config import ( FusedMoEQuantConfig, mxfp4_mxfp8_moe_quant_config, mxfp4_w4a16_moe_quant_config, ocp_mx_moe_quant_config, ) from vllm.model_executor.layers.fused_moe.fused_marlin_moe import ( BatchedMarlinExperts, MarlinExperts, fused_marlin_moe, ) from vllm.model_executor.layers.fused_moe.gpt_oss_triton_kernels_moe import ( OAITritonExperts, UnfusedOAITritonExperts, ) from vllm.model_executor.layers.fused_moe.trtllm_moe import TrtLlmGenExperts from vllm.model_executor.layers.linear import LinearBase, UnquantizedLinearMethod from vllm.model_executor.layers.quantization import QuantizationMethods from vllm.model_executor.layers.quantization.base_config import ( QuantizationConfig, QuantizeMethodBase, ) from vllm.model_executor.layers.quantization.utils.marlin_utils import ( get_marlin_input_dtype, ) from vllm.model_executor.layers.quantization.utils.marlin_utils_fp4 import ( prepare_moe_fp4_layer_for_marlin, ) from vllm.model_executor.layers.quantization.utils.mxfp4_utils import ( _can_support_mxfp4, _swizzle_mxfp4, get_padding_alignment, ) from vllm.model_executor.layers.quantization.utils.quant_utils import is_layer_skipped from vllm.model_executor.utils import set_weight_attrs from vllm.platforms import current_platform from vllm.scalar_type import scalar_types from vllm.utils.flashinfer import has_flashinfer from vllm.utils.import_utils import has_triton_kernels from vllm.utils.math_utils import round_up from vllm.utils.torch_utils import is_torch_equal_or_newer logger = init_logger(__name__) # enum for mxfp4 backend class Mxfp4Backend(Enum): NONE = 0 # FlashInfer Backend SM100_FI_MXFP4_MXFP8_TRTLLM = 1 SM100_FI_MXFP4_MXFP8_CUTLASS = 2 SM100_FI_MXFP4_BF16 = 3 SM90_FI_MXFP4_BF16 = 4 # Marlin Backend MARLIN = 5 # Triton Backend TRITON = 6 def get_mxfp4_backend_with_lora() -> Mxfp4Backend: """ Not all MXFP4 backends support LoRA. Select backends that are known to have LoRA support. """ if not current_platform.is_cuda(): return Mxfp4Backend.NONE # If FlashInfer is not available, try either Marlin or Triton triton_kernels_supported = ( has_triton_kernels() and is_torch_equal_or_newer("2.8.0") # NOTE: triton_kernels are only confirmed to work on SM90 and SM100 # SM110 fails with this error: https://github.com/vllm-project/vllm/issues/29317 # SM120 needs this fix: https://github.com/triton-lang/triton/pull/8498 and (9, 0) <= current_platform.get_device_capability() < (11, 0) ) if envs.VLLM_MXFP4_USE_MARLIN is False and triton_kernels_supported: logger.info_once("[get_mxfp4_backend_with_lora] Using Triton backend") return Mxfp4Backend.TRITON logger.info_once("[get_mxfp4_backend_with_lora] Using Marlin backend") return Mxfp4Backend.MARLIN def get_mxfp4_backend(with_lora_support: bool) -> Mxfp4Backend: # Backend Selection if with_lora_support: return get_mxfp4_backend_with_lora() if current_platform.is_cuda(): if ( current_platform.is_device_capability(90) and has_flashinfer() and envs.VLLM_USE_FLASHINFER_MOE_MXFP4_BF16 ): logger.info_once("Using FlashInfer MXFP4 BF16 backend for SM90") return Mxfp4Backend.SM90_FI_MXFP4_BF16 elif ( current_platform.is_device_capability_family(100) and has_flashinfer() and envs.VLLM_USE_FLASHINFER_MOE_MXFP4_MXFP8_CUTLASS ): logger.info_once("Using FlashInfer MXFP4 MXFP8 CUTLASS backend for SM100") return Mxfp4Backend.SM100_FI_MXFP4_MXFP8_CUTLASS elif ( current_platform.is_device_capability_family(100) and has_flashinfer() and envs.VLLM_USE_FLASHINFER_MOE_MXFP4_MXFP8 ): return Mxfp4Backend.SM100_FI_MXFP4_MXFP8_TRTLLM elif current_platform.is_device_capability_family(100) and has_flashinfer(): logger.info_once( "Using FlashInfer MXFP4 BF16 backend for SM100, " "For faster performance on SM100, consider setting " "VLLM_USE_FLASHINFER_MOE_MXFP4_MXFP8=1, though this may impact " "accuracy." ) return Mxfp4Backend.SM100_FI_MXFP4_BF16 elif ( current_platform.is_device_capability_family(100) or current_platform.is_device_capability(90) ) and not has_flashinfer(): logger.warning_once( "MXFP4 MoE is enabled on Hopper/Blackwell but FlashInfer " "is not available. This may result in degraded performance. " "Please `pip install vllm[flashinfer]` for best results." ) # If FlashInfer is not available, try either Marlin or Triton triton_kernels_supported = ( has_triton_kernels() and is_torch_equal_or_newer("2.8.0") # NOTE: triton_kernels are only confirmed to work on SM90 and SM100 # SM110 fails with this error: https://github.com/vllm-project/vllm/issues/29317 # SM120 needs this fix: https://github.com/triton-lang/triton/pull/8498 and (9, 0) <= current_platform.get_device_capability() < (11, 0) ) if envs.VLLM_MXFP4_USE_MARLIN or not triton_kernels_supported: logger.info_once("Using Marlin backend") return Mxfp4Backend.MARLIN else: logger.info_once("Using Triton backend") return Mxfp4Backend.TRITON elif current_platform.is_xpu(): logger.info_once("Using ipex marlin backend on XPU") return Mxfp4Backend.MARLIN elif current_platform.is_rocm() and has_triton_kernels(): logger.info_once("Using Triton backend") return Mxfp4Backend.TRITON return Mxfp4Backend.NONE class Mxfp4Config(QuantizationConfig): def __init__(self, ignored_layers: list[str] | None = None): super().__init__() self.ignored_layers = ignored_layers @classmethod def from_config(cls, config): return cls() @classmethod def get_min_capability(cls) -> int: return 80 @classmethod def get_name(cls) -> QuantizationMethods: return "mxfp4" @classmethod def get_supported_act_dtypes(cls) -> list[torch.dtype]: return [torch.bfloat16] @classmethod def get_config_filenames(cls) -> list[str]: return [] def get_quant_method( self, layer: torch.nn.Module, prefix: str ) -> Optional["QuantizeMethodBase"]: if isinstance(layer, LinearBase): if self.ignored_layers and is_layer_skipped( prefix=prefix, ignored_layers=self.ignored_layers, fused_mapping=self.packed_modules_mapping, ): return UnquantizedLinearMethod() # TODO: Add support for MXFP4 Linear Method. # MXFP4 LinearMethod is available in AMD-Quark, refer to that implementation # if you are interested in enabling MXFP4 here. logger.debug_once( "MXFP4 linear layer is not implemented - falling back to " "UnquantizedLinearMethod.", scope="local", ) return UnquantizedLinearMethod() elif isinstance(layer, FusedMoE): if current_platform.is_xpu(): return IpexMxfp4MoEMethod(layer.moe_config) else: quant_method = Mxfp4MoEMethod(layer.moe_config) quant_method.marlin_input_dtype = get_marlin_input_dtype(prefix) return quant_method elif isinstance(layer, Attention): # TODO: Add support for MXFP4 Attention. logger.debug_once( "MXFP4 attention layer is not implemented. " "Skipping quantization for this layer.", scope="local", ) return None class Mxfp4MoEMethod(FusedMoEMethodBase): def __init__(self, moe: FusedMoEConfig): super().__init__(moe) self.mxfp4_backend = get_mxfp4_backend(moe.is_lora_enabled) self.marlin_input_dtype = None self.max_capture_size = ( get_current_vllm_config().compilation_config.max_cudagraph_capture_size ) assert self.mxfp4_backend != Mxfp4Backend.NONE, ( f"get_mxfp4_backend(with_lora_support={moe.is_lora_enabled}) found" "no compatible MXFP4 MoE backend (FlashInfer/Marlin/Triton)." "Please check your environment and try again." ) self._cache_permute_indices: dict[torch.Size, torch.Tensor] = {} def create_weights( self, layer: torch.nn.Module, num_experts: int, hidden_size: int, intermediate_size_per_partition: int, params_dtype: torch.dtype, **extra_weight_attrs, ): self.num_experts = num_experts weight_dtype = torch.uint8 scale_dtype = torch.uint8 # FIXME (zyongye): ship after torch and safetensors support mxfp4 # is_torch_mxfp4_available = ( # hasattr(torch, "float4_e2m1fn_x2") and # hasattr(torch, "float8_e8m0fnu")) # if is_torch_mxfp4_available: # weight_dtype = torch.float4_e2m1fn_x2 # scale_dtype = torch.float8_e8m0fnu mxfp4_block = 32 intermediate_size_per_partition_after_pad = intermediate_size_per_partition if self.mxfp4_backend == Mxfp4Backend.MARLIN: # The moe marlin kernel requires that for each linear # n % 256 == 0 and k % 128 == 0. # In gate_up_proj: # n = 2 * intermediate_size_per_partition_after_pad # k = hidden_size # In down_proj # n = hidden_size # k = intermediate_size_per_partition_after_pad intermediate_size_per_partition_after_pad = round_up( intermediate_size_per_partition, 128 ) if current_platform.is_xpu(): hidden_size = round_up(hidden_size, 128) else: hidden_size = round_up(hidden_size, 256) layer.params_dtype = params_dtype layer.num_experts = num_experts layer.hidden_size = hidden_size layer.intermediate_size_per_partition = ( intermediate_size_per_partition_after_pad ) elif ( self.mxfp4_backend == Mxfp4Backend.SM100_FI_MXFP4_MXFP8_TRTLLM or self.mxfp4_backend == Mxfp4Backend.SM100_FI_MXFP4_BF16 ): # pad the intermediate size to be a multiple of 2 * mxfp4_block # for to hold non-uniform sharded tensor as well as swizzling # other padding to increase performance intermediate_size_per_partition_after_pad = round_up( intermediate_size_per_partition, 256 ) hidden_size = round_up(hidden_size, 256) elif ( self.mxfp4_backend == Mxfp4Backend.SM100_FI_MXFP4_MXFP8_CUTLASS or self.mxfp4_backend == Mxfp4Backend.SM90_FI_MXFP4_BF16 ): intermediate_size_per_partition_after_pad = round_up( intermediate_size_per_partition, 128 ) hidden_size = round_up(hidden_size, 128) elif current_platform.is_rocm(): pad_align = get_padding_alignment() intermediate_size_per_partition_after_pad = round_up( intermediate_size_per_partition, pad_align ) hidden_size = round_up(hidden_size, pad_align) else: intermediate_size_per_partition_after_pad = round_up( intermediate_size_per_partition, 64 ) self.intermediate_size = intermediate_size_per_partition_after_pad self.hidden_size = hidden_size # Fused gate_up_proj (column parallel) w13_weight = torch.nn.Parameter( torch.zeros( num_experts, 2 * intermediate_size_per_partition_after_pad, hidden_size // 2, dtype=weight_dtype, ), requires_grad=False, ) layer.register_parameter("w13_weight", w13_weight) set_weight_attrs(w13_weight, extra_weight_attrs) w13_weight_scale = torch.nn.Parameter( torch.zeros( num_experts, 2 * intermediate_size_per_partition_after_pad, hidden_size // mxfp4_block, dtype=scale_dtype, ), requires_grad=False, ) layer.register_parameter("w13_weight_scale", w13_weight_scale) set_weight_attrs(w13_weight_scale, extra_weight_attrs) w13_bias = torch.nn.Parameter( torch.zeros( num_experts, 2 * intermediate_size_per_partition_after_pad, dtype=torch.bfloat16, ), requires_grad=False, ) layer.register_parameter("w13_bias", w13_bias) set_weight_attrs(w13_bias, extra_weight_attrs) # down_proj (row parallel) w2_weight = torch.nn.Parameter( torch.zeros( num_experts, hidden_size, intermediate_size_per_partition_after_pad // 2, dtype=weight_dtype, ), requires_grad=False, ) layer.register_parameter("w2_weight", w2_weight) set_weight_attrs(w2_weight, extra_weight_attrs) w2_weight_scale = torch.nn.Parameter( torch.zeros( num_experts, hidden_size, intermediate_size_per_partition_after_pad // mxfp4_block, dtype=scale_dtype, ), requires_grad=False, ) layer.register_parameter("w2_weight_scale", w2_weight_scale) set_weight_attrs(w2_weight_scale, extra_weight_attrs) w2_bias = torch.nn.Parameter( torch.zeros( num_experts, hidden_size, dtype=torch.bfloat16, ), requires_grad=False, ) layer.register_parameter("w2_bias", w2_bias) set_weight_attrs(w2_bias, extra_weight_attrs) def process_weights_after_loading(self, layer): if self.mxfp4_backend == Mxfp4Backend.MARLIN: prepare_moe_fp4_layer_for_marlin(layer, input_dtype=self.marlin_input_dtype) elif ( self.mxfp4_backend == Mxfp4Backend.SM100_FI_MXFP4_MXFP8_TRTLLM or self.mxfp4_backend == Mxfp4Backend.SM100_FI_MXFP4_BF16 ): from flashinfer.fp4_quantization import nvfp4_block_scale_interleave from flashinfer.fused_moe.core import get_w2_permute_indices_with_cache layer.gemm1_alpha = Parameter( torch.tensor([1.702] * self.num_experts, dtype=torch.float32).cuda(), requires_grad=False, ) layer.gemm1_beta = Parameter( torch.tensor([1.0] * self.num_experts, dtype=torch.float32).cuda(), requires_grad=False, ) layer.gemm1_clamp_limit = Parameter( torch.tensor([7.0] * self.num_experts, dtype=torch.float32).cuda(), requires_grad=False, ) sf_block_size = 32 # mxfp4 block size assert ( layer.w13_weight.dim() == 3 and layer.w13_weight.shape[0] == self.num_experts and layer.w13_weight.shape[1] == self.intermediate_size * 2 and layer.w13_weight.shape[2] == self.hidden_size // 2 ) assert ( layer.w13_weight_scale.dim() == 3 and layer.w13_weight_scale.shape[0] == self.num_experts and layer.w13_weight_scale.shape[1] == self.intermediate_size * 2 and layer.w13_weight_scale.shape[2] == self.hidden_size // sf_block_size ) assert ( layer.w2_weight.dim() == 3 and layer.w2_weight.shape[0] == self.num_experts and layer.w2_weight.shape[1] == self.hidden_size and layer.w2_weight.shape[2] == self.intermediate_size // 2 ) assert ( layer.w2_weight_scale.dim() == 3 and layer.w2_weight_scale.shape[1] == self.hidden_size and layer.w2_weight_scale.shape[2] == self.intermediate_size // sf_block_size ) assert ( layer.w13_bias.dim() == 2 and layer.w13_bias.shape[0] == self.num_experts and layer.w13_bias.shape[1] == self.intermediate_size * 2 ) assert ( layer.w2_bias.dim() == 2 and layer.w2_bias.shape[0] == self.num_experts and layer.w2_bias.shape[1] == self.hidden_size ) w13_weight_scale = layer.w13_weight_scale.data w2_weight_scale = layer.w2_weight_scale.data w13_weight = layer.w13_weight.data w2_weight = layer.w2_weight.data w13_bias = layer.w13_bias.data.to(torch.float32) w2_bias = layer.w2_bias.data.to(torch.float32) # Swap w1 and w3 as the definition of # swiglu is different in the trtllm-gen def swap_every_two_rows(x, axis=-1): shape = x.shape if axis < 0: axis = len(shape) + axis # Create a new shape with pairs swapped along specified axis new_shape = list(shape) new_shape[axis] = shape[axis] // 2 new_shape.insert(axis + 1, 2) # Reshape to expose pairs, swap them, and reshape back x = x.reshape(*new_shape) x = x.flip(axis + 1) new_shape = list(shape) return x.reshape(*new_shape) w13_weight_scale = swap_every_two_rows(w13_weight_scale, -2) w13_weight = swap_every_two_rows(w13_weight, -2) w13_bias = swap_every_two_rows(w13_bias, -1) # Do not interleave as the checkpoint is already interleaved # Shuffle weights and scaling factors for transposed mma output gemm1_weights_mxfp4_shuffled = [] gemm1_scales_mxfp4_shuffled = [] gemm2_weights_mxfp4_shuffled = [] gemm2_scales_mxfp4_shuffled = [] gemm1_bias_shuffled = [] gemm2_bias_shuffled = [] epilogue_tile_m = 128 # FIXME: this depends on the kernel internals for i in range(self.num_experts): # w13 weight shuffling permute_indices = get_w2_permute_indices_with_cache( self._cache_permute_indices, w13_weight[i].view(torch.uint8), epilogue_tile_m, ) gemm1_weights_mxfp4_shuffled.append( w13_weight[i] .view(torch.uint8)[permute_indices.to(w13_weight.device)] .contiguous() ) # w13 scale shuffling permute_sf_indices = get_w2_permute_indices_with_cache( self._cache_permute_indices, w13_weight_scale[i].view(torch.uint8), epilogue_tile_m, num_elts_per_sf=16, ) gemm1_scales_mxfp4_shuffled.append( nvfp4_block_scale_interleave( w13_weight_scale[i] .view(torch.uint8)[ permute_sf_indices.to(w13_weight_scale.device) ] .contiguous() ) ) # w13 bias shuffling permute_bias_indices = get_w2_permute_indices_with_cache( self._cache_permute_indices, w13_bias[i].clone().reshape(-1, 1), epilogue_tile_m, ) gemm1_bias_shuffled.append( w13_bias[i] .clone() .reshape(-1, 1)[permute_bias_indices.to(w13_bias.device)] .contiguous() ) # w2 weight shuffling permute_indices = get_w2_permute_indices_with_cache( self._cache_permute_indices, w2_weight[i].view(torch.uint8), epilogue_tile_m, ) gemm2_weights_mxfp4_shuffled.append( w2_weight[i] .view(torch.uint8)[permute_indices.to(w2_weight.device)] .contiguous() ) # w2 scale shuffling permute_sf_indices = get_w2_permute_indices_with_cache( self._cache_permute_indices, w2_weight_scale[i].view(torch.uint8), epilogue_tile_m, num_elts_per_sf=16, ) gemm2_scales_mxfp4_shuffled.append( nvfp4_block_scale_interleave( w2_weight_scale[i] .view(torch.uint8)[ permute_sf_indices.to(w2_weight_scale.device) ] .contiguous() ) ) # w2 bias shuffling permute_indices = get_w2_permute_indices_with_cache( self._cache_permute_indices, w2_bias[i].clone().reshape(-1, 1), epilogue_tile_m, ) gemm2_bias_shuffled.append( w2_bias[i] .clone() .reshape(-1, 1)[permute_indices.to(w2_bias.device)] .contiguous() ) w13_weight = torch.stack(gemm1_weights_mxfp4_shuffled) w13_weight_scale = ( torch.stack(gemm1_scales_mxfp4_shuffled) .reshape( self.num_experts, 2 * self.intermediate_size, self.hidden_size // sf_block_size, ) .view(torch.float8_e4m3fn) ) w2_weight = torch.stack(gemm2_weights_mxfp4_shuffled) w2_weight_scale = ( torch.stack(gemm2_scales_mxfp4_shuffled) .reshape( self.num_experts, self.hidden_size, self.intermediate_size // sf_block_size, ) .view(torch.float8_e4m3fn) ) layer.w13_weight = Parameter(w13_weight, requires_grad=False) layer.w13_weight_scale = Parameter(w13_weight_scale, requires_grad=False) layer.w2_weight = Parameter(w2_weight, requires_grad=False) layer.w2_weight_scale = Parameter(w2_weight_scale, requires_grad=False) layer.w13_bias = Parameter( torch.stack(gemm1_bias_shuffled).reshape(self.num_experts, -1), requires_grad=False, ) layer.w2_bias = Parameter( torch.stack(gemm2_bias_shuffled).reshape(self.num_experts, -1), requires_grad=False, ) elif ( self.mxfp4_backend == Mxfp4Backend.SM100_FI_MXFP4_MXFP8_CUTLASS or self.mxfp4_backend == Mxfp4Backend.SM90_FI_MXFP4_BF16 ): layer.gemm1_alpha = Parameter( torch.tensor([1.702] * self.num_experts, dtype=torch.float32).cuda(), requires_grad=False, ) layer.gemm1_beta = Parameter( torch.tensor([1.0] * self.num_experts, dtype=torch.float32).cuda(), requires_grad=False, ) layer.gemm1_clamp_limit = Parameter( torch.tensor([7.0] * self.num_experts, dtype=torch.float32).cuda(), requires_grad=False, ) sf_block_size = 32 # mxfp4 block size # Common shape assertions assert ( layer.w13_weight.dim() == 3 and layer.w13_weight.shape[0] == self.num_experts and layer.w13_weight.shape[1] == self.intermediate_size * 2 and layer.w13_weight.shape[2] == self.hidden_size // 2 ) assert ( layer.w13_weight_scale.dim() == 3 and layer.w13_weight_scale.shape[0] == self.num_experts and layer.w13_weight_scale.shape[1] == self.intermediate_size * 2 and layer.w13_weight_scale.shape[2] == self.hidden_size // sf_block_size ) assert ( layer.w2_weight.dim() == 3 and layer.w2_weight.shape[0] == self.num_experts and layer.w2_weight.shape[1] == self.hidden_size and layer.w2_weight.shape[2] == self.intermediate_size // 2 ) assert ( layer.w2_weight_scale.dim() == 3 and layer.w2_weight_scale.shape[1] == self.hidden_size and layer.w2_weight_scale.shape[2] == self.intermediate_size // sf_block_size ) assert ( layer.w13_bias.dim() == 2 and layer.w13_bias.shape[0] == self.num_experts and layer.w13_bias.shape[1] == self.intermediate_size * 2 ) assert ( layer.w2_bias.dim() == 2 and layer.w2_bias.shape[0] == self.num_experts and layer.w2_bias.shape[1] == self.hidden_size ) # De-interleave and swap for w13 weight, bias, and scales w13_w = layer.w13_weight.data gate_w, up_w = w13_w[:, ::2, :], w13_w[:, 1::2, :] deinterleaved_w13_w = torch.cat([gate_w, up_w], dim=1) w1_w, w3_w = torch.chunk(deinterleaved_w13_w, 2, dim=1) w13_weight_swapped = torch.cat([w3_w, w1_w], dim=1) w13_b = layer.w13_bias.data.to(torch.float32) gate_b, up_b = w13_b[:, ::2], w13_b[:, 1::2] deinterleaved_w13_b = torch.cat([gate_b, up_b], dim=1) b1, b3 = torch.chunk(deinterleaved_w13_b, 2, dim=-1) w13_bias_swapped = torch.cat([b3, b1], dim=-1).to(torch.bfloat16) w13_s = layer.w13_weight_scale.data gate_s, up_s = w13_s[:, ::2, :], w13_s[:, 1::2, :] deinterleaved_w13_s = torch.cat([gate_s, up_s], dim=1) s1, s3 = torch.chunk(deinterleaved_w13_s, 2, dim=1) w13_scale_swapped = torch.cat([s3, s1], dim=1) if self.mxfp4_backend == Mxfp4Backend.SM100_FI_MXFP4_MXFP8_CUTLASS: from flashinfer import block_scale_interleave orig_shape = w13_scale_swapped.shape w13_scale_interleaved = block_scale_interleave( w13_scale_swapped.view(torch.uint8) ).reshape(orig_shape) w2_s = layer.w2_weight_scale.data orig_shape = w2_s.shape w2_scale_interleaved = block_scale_interleave( w2_s.view(torch.uint8) ).reshape(orig_shape) layer.w13_weight = Parameter(w13_weight_swapped, requires_grad=False) layer.w13_weight_scale = Parameter( w13_scale_interleaved, requires_grad=False ) layer.w13_bias = Parameter(w13_bias_swapped, requires_grad=False) layer.w2_weight_scale = Parameter( w2_scale_interleaved, requires_grad=False ) elif self.mxfp4_backend == Mxfp4Backend.SM90_FI_MXFP4_BF16: def _interleave_mxfp4_cutlass_sm90(w): w_shape = w.shape w_interleaved = w.reshape( w_shape[0], w_shape[1], (w_shape[2] // 4), 4 ) w_interleaved = w_interleaved.permute(0, 2, 1, 3) w_interleaved = w_interleaved.reshape( w_shape[0], w_shape[2] // 4, w_shape[1] * 4 ) return w_interleaved w31_scales = w13_scale_swapped.to(torch.uint8).view(torch.uint8) w31_scales_interleaved = _interleave_mxfp4_cutlass_sm90(w31_scales) w2_weight_scale = layer.w2_weight_scale.data w2_scales = w2_weight_scale.to(torch.uint8).view(torch.uint8) w2_scales_interleaved = _interleave_mxfp4_cutlass_sm90(w2_scales) layer.w13_weight = torch.nn.Parameter( torch.cat([w3_w, w1_w], dim=1), requires_grad=False ) layer.w13_bias = torch.nn.Parameter( w13_bias_swapped, requires_grad=False ) layer.w13_weight_scale = torch.nn.Parameter( w31_scales_interleaved, requires_grad=False ) layer.w2_weight_scale = torch.nn.Parameter( w2_scales_interleaved, requires_grad=False ) elif self.mxfp4_backend == Mxfp4Backend.TRITON: from triton_kernels.matmul_ogs import FlexCtx, PrecisionConfig w13_bias = layer.w13_bias.to(torch.float32) w2_bias = layer.w2_bias.to(torch.float32) layer.w13_bias = Parameter(w13_bias, requires_grad=False) layer.w2_bias = Parameter(w2_bias, requires_grad=False) # Ideally we'd use FusedMoEModularKernel.prepare_finalize object # (stored in self.fused_experts) to determine if the MoE has a # batched activation format. As self.fused_experts is not # initialized at this point, we resort to checking the MoE config # directly. is_batched_moe = self.moe.use_pplx_kernels or self.moe.use_deepep_ll_kernels if is_batched_moe: num_warps = 4 if envs.VLLM_MOE_DP_CHUNK_SIZE <= 512 else 8 else: num_warps = 8 w13_weight, w13_flex, w13_scale = _swizzle_mxfp4( layer.w13_weight, layer.w13_weight_scale, num_warps ) w2_weight, w2_flex, w2_scale = _swizzle_mxfp4( layer.w2_weight, layer.w2_weight_scale, num_warps ) self.w13_precision_config = PrecisionConfig( weight_scale=w13_scale, flex_ctx=FlexCtx(rhs_data=w13_flex) ) self.w2_precision_config = PrecisionConfig( weight_scale=w2_scale, flex_ctx=FlexCtx(rhs_data=w2_flex) ) self.w13_weight = w13_weight self.w2_weight = w2_weight del layer.w13_weight del layer.w2_weight layer.w13_weight = w13_weight layer.w2_weight = w2_weight else: raise ValueError(f"Unsupported backend: {self.mxfp4_backend}") def get_fused_moe_quant_config( self, layer: torch.nn.Module ) -> FusedMoEQuantConfig | None: if self.mxfp4_backend == Mxfp4Backend.MARLIN: return mxfp4_w4a16_moe_quant_config( w1_bias=layer.w13_bias, w2_bias=layer.w2_bias, w1_scale=layer.w13_weight_scale, w2_scale=layer.w2_weight_scale, ) elif self.mxfp4_backend == Mxfp4Backend.TRITON: w1_scale = self.w13_precision_config w2_scale = self.w2_precision_config return mxfp4_w4a16_moe_quant_config( w1_bias=layer.w13_bias,
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
true
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/awq_marlin.py
vllm/model_executor/layers/quantization/awq_marlin.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from typing import TYPE_CHECKING, Any, Optional import torch from safetensors.torch import _TYPES as _SAFETENSORS_TO_TORCH_DTYPE from torch.nn import Parameter import vllm.model_executor.layers.fused_moe # noqa from vllm import _custom_ops as ops from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe.config import ( FusedMoEConfig, FusedMoEQuantConfig, ) from vllm.model_executor.layers.fused_moe.fused_marlin_moe import fused_marlin_moe from vllm.model_executor.layers.fused_moe.layer import ( FusedMoE, FusedMoEMethodBase, FusedMoeWeightScaleSupported, UnquantizedFusedMoEMethod, ) from vllm.model_executor.layers.linear import ( LinearBase, LinearMethodBase, UnquantizedLinearMethod, set_weight_attrs, ) from vllm.model_executor.layers.quantization.awq import AWQConfig from vllm.model_executor.layers.quantization.base_config import ( QuantizationConfig, QuantizeMethodBase, ) from vllm.model_executor.layers.quantization.utils import replace_parameter from vllm.model_executor.layers.quantization.utils.marlin_utils import ( apply_awq_marlin_linear, awq_to_marlin_zero_points, check_marlin_supported, check_marlin_supports_layer, check_moe_marlin_supports_layer, get_marlin_input_dtype, marlin_act_int8_process_scales, marlin_make_empty_g_idx, marlin_make_workspace_new, marlin_moe_permute_scales, marlin_permute_bias, marlin_permute_scales, moe_awq_to_marlin_zero_points, verify_marlin_supported, verify_marlin_supports_shape, ) from vllm.model_executor.layers.quantization.utils.quant_utils import is_layer_skipped from vllm.model_executor.layers.vocab_parallel_embedding import ParallelLMHead from vllm.model_executor.parameter import GroupQuantScaleParameter, PackedvLLMParameter from vllm.platforms import current_platform from vllm.scalar_type import scalar_types from vllm.transformers_utils.config import get_safetensors_params_metadata if TYPE_CHECKING: from vllm.model_executor.layers.quantization import QuantizationMethods from vllm.model_executor.models.utils import WeightsMapper logger = init_logger(__name__) class AWQMarlinConfig(QuantizationConfig): """Config class for AWQ Marlin""" # num_bits -> type TYPE_MAP = { 4: scalar_types.uint4, } def __init__( self, weight_bits: int, group_size: int, zero_point: bool, lm_head_quantized: bool, modules_to_not_convert: list[str] | None, full_config: dict[str, Any], ) -> None: super().__init__() self.pack_factor = 32 // weight_bits # packed into int32 self.group_size = group_size self.zero_point = zero_point self.lm_head_quantized = lm_head_quantized self.weight_bits = weight_bits self.modules_to_not_convert = modules_to_not_convert or [] self.full_config = full_config if self.weight_bits not in self.TYPE_MAP: raise ValueError( f"Unsupported num_bits = {self.weight_bits}. " f"Supported num_bits = {self.TYPE_MAP.keys()}" ) self.quant_type = self.TYPE_MAP[self.weight_bits] verify_marlin_supported( self.quant_type, group_size=self.group_size, has_zp=self.zero_point ) def __repr__(self) -> str: return ( f"AWQMarlinConfig(quant_type={self.quant_type}, " f"group_size={self.group_size}, " f"zero_point={self.zero_point}, " f"lm_head_quantized={self.lm_head_quantized}, " f"modules_to_not_convert={self.modules_to_not_convert})" ) @classmethod def get_name(cls) -> "QuantizationMethods": return "awq_marlin" @classmethod def get_supported_act_dtypes(cls) -> list[torch.dtype]: return [torch.half, torch.bfloat16] @classmethod def get_min_capability(cls) -> int: return 75 @classmethod def get_config_filenames(cls) -> list[str]: return ["quantize_config.json"] @classmethod def from_config(cls, config: dict[str, Any]) -> "AWQMarlinConfig": weight_bits = cls.get_from_keys(config, ["bits"]) group_size = cls.get_from_keys(config, ["group_size"]) zero_point = cls.get_from_keys(config, ["zero_point"]) lm_head_quantized = cls.get_from_keys_or(config, ["lm_head"], default=False) modules_to_not_convert = cls.get_from_keys_or( config, ["modules_to_not_convert"], None ) return cls( weight_bits, group_size, zero_point, lm_head_quantized, modules_to_not_convert, config, ) @classmethod def override_quantization_method( cls, hf_quant_cfg, user_quant ) -> Optional["QuantizationMethods"]: can_convert = cls.is_awq_marlin_compatible(hf_quant_cfg) is_valid_user_quant = ( user_quant is None or user_quant == "marlin" or user_quant == "awq_marlin" ) if can_convert and is_valid_user_quant: msg = ( "The model is convertible to {} during runtime." " Using {} kernel.".format(cls.get_name(), cls.get_name()) ) logger.info(msg) return cls.get_name() if can_convert and user_quant == "awq": logger.info( "Detected that the model can run with awq_marlin" ", however you specified quantization=awq explicitly," " so forcing awq. Use quantization=awq_marlin for" " faster inference" ) return None def get_quant_method( self, layer: torch.nn.Module, prefix: str ) -> Optional["QuantizeMethodBase"]: if isinstance(layer, LinearBase) or ( isinstance(layer, ParallelLMHead) and self.lm_head_quantized ): if is_layer_skipped( prefix, self.modules_to_not_convert, self.packed_modules_mapping, skip_with_substr=True, ): return UnquantizedLinearMethod() # Check if the layer is supported by AWQMarlin. if not check_marlin_supports_layer(layer, self.group_size): logger.warning_once( "Layer '%s' is not supported by AWQMarlin. Falling back to unoptimized AWQ kernels.", # noqa: E501 prefix, ) return AWQConfig.from_config(self.full_config).get_quant_method( layer, prefix ) quant_method = AWQMarlinLinearMethod(self) quant_method.input_dtype = get_marlin_input_dtype(prefix) return quant_method elif isinstance(layer, FusedMoE): from vllm.model_executor.layers.quantization.moe_wna16 import MoeWNA16Config if is_layer_skipped( prefix, getattr(self, "modules_to_not_convert", []), skip_with_substr=True, ): return UnquantizedFusedMoEMethod(layer.moe_config) if not check_moe_marlin_supports_layer(layer, self.group_size): logger.warning_once( f"Layer '{prefix}' is not supported by AWQMoeMarlin. " "Falling back to Moe WNA16 kernels." ) return MoeWNA16Config.from_config(self.full_config).get_quant_method( layer, prefix ) moe_quant_method = AWQMarlinMoEMethod(self, layer.moe_config) moe_quant_method.input_dtype = get_marlin_input_dtype(prefix) return moe_quant_method return None @classmethod def is_awq_marlin_compatible(cls, quant_config: dict[str, Any]): # Extract data from quant config. quant_method = quant_config.get("quant_method", "").lower() num_bits = quant_config.get("bits") group_size = quant_config.get("group_size") zero_point = quant_config.get("zero_point") if not current_platform.is_cuda(): return False if quant_method != "awq": return False # If we cannot find the info needed in the config, cannot convert. if num_bits is None or group_size is None or zero_point is None: return False if num_bits not in cls.TYPE_MAP: return False return check_marlin_supported( quant_type=cls.TYPE_MAP[num_bits], group_size=group_size, has_zp=zero_point ) def apply_vllm_mapper(self, hf_to_vllm_mapper: "WeightsMapper"): if self.modules_to_not_convert: self.modules_to_not_convert = hf_to_vllm_mapper.apply_list( self.modules_to_not_convert ) def maybe_update_config(self, model_name: str, revision: str | None = None): if self.modules_to_not_convert: return unquant_dtypes = [torch.float16, torch.bfloat16, torch.float32] metadata = get_safetensors_params_metadata(model_name, revision=revision) layers = {param_name.rsplit(".", 1)[0] for param_name in metadata} quant_layers: set[str] = { param_name.rsplit(".", 1)[0] for param_name, info in metadata.items() if (dtype := info.get("dtype", None)) and _SAFETENSORS_TO_TORCH_DTYPE[dtype] not in unquant_dtypes } self.modules_to_not_convert = list(layers - quant_layers) class AWQMarlinLinearMethod(LinearMethodBase): """Linear method for AWQ Marlin. Args: quant_config: The AWQ Marlin quantization config. """ def __init__(self, quant_config: AWQMarlinConfig) -> None: self.quant_config = quant_config self.quant_type = scalar_types.uint4 self.input_dtype = None def create_weights( self, layer: torch.nn.Module, input_size_per_partition: int, output_partition_sizes: list[int], input_size: int, output_size: int, params_dtype: torch.dtype, **extra_weight_attrs, ) -> None: del output_size output_size_per_partition = sum(output_partition_sizes) weight_loader = extra_weight_attrs.get("weight_loader") # Normalize group_size if self.quant_config.group_size != -1: group_size = self.quant_config.group_size else: group_size = input_size verify_marlin_supports_shape( output_size_per_partition=output_size_per_partition, input_size_per_partition=input_size_per_partition, input_size=input_size, group_size=group_size, ) qweight = PackedvLLMParameter( data=torch.empty( input_size_per_partition, output_size_per_partition // self.quant_config.pack_factor, dtype=torch.int32, ), input_dim=0, output_dim=1, packed_dim=1, packed_factor=self.quant_config.pack_factor, weight_loader=weight_loader, ) num_groups = input_size_per_partition // group_size layer.num_groups = num_groups qzeros = PackedvLLMParameter( data=torch.empty( num_groups, output_size_per_partition // self.quant_config.pack_factor, dtype=torch.int32, ), input_dim=0, output_dim=1, packed_dim=1, packed_factor=self.quant_config.pack_factor, weight_loader=weight_loader, ) scales = GroupQuantScaleParameter( data=torch.empty( num_groups, output_size_per_partition, dtype=params_dtype, ), input_dim=0, output_dim=1, weight_loader=weight_loader, ) layer.register_parameter("qweight", qweight) layer.register_parameter("qzeros", qzeros) layer.register_parameter("scales", scales) layer.input_size_per_partition = input_size_per_partition layer.output_size_per_partition = output_size_per_partition layer.num_groups = num_groups # TODO: Update this docs # Checkpoints are serialized in AutoAWQ format, which is different from the # marlin format. This function is called after the weights are loaded. # Here, we handle the repacking def process_weights_after_loading(self, layer: torch.nn.Module) -> None: device = layer.qweight.device layer.qweight = torch.nn.Parameter(layer.qweight.data, requires_grad=False) layer.qzeros = torch.nn.Parameter(layer.qzeros.data, requires_grad=False) layer.scales = torch.nn.Parameter(layer.scales.data, requires_grad=False) # Allocate marlin workspace layer.workspace = marlin_make_workspace_new(device) is_a_8bit = self.input_dtype is not None and self.input_dtype.itemsize == 1 if self.input_dtype == torch.float8_e4m3fn: ops.marlin_int4_fp8_preprocess(layer.qweight, layer.qzeros, inplace=True) layer.scales.data = layer.scales.data * 512 # Repack weights from AWQ format to marlin format. marlin_qweight = ops.awq_marlin_repack( layer.qweight, size_k=layer.input_size_per_partition, size_n=layer.output_size_per_partition, num_bits=self.quant_config.quant_type.size_bits, is_a_8bit=is_a_8bit, ) replace_parameter(layer, "qweight", marlin_qweight) # Permute scales from AWQ format to marlin format. marlin_scales = marlin_permute_scales( layer.scales, size_k=layer.input_size_per_partition, size_n=layer.output_size_per_partition, group_size=self.quant_config.group_size, is_a_8bit=is_a_8bit, ) if self.input_dtype == torch.int8 and layer.num_groups > 1: marlin_scales, input_global_scale = marlin_act_int8_process_scales( marlin_scales ) layer.register_parameter( "input_global_scale", Parameter(input_global_scale, requires_grad=False) ) replace_parameter(layer, "scales", marlin_scales) # Permute zero-points from AWQ format to marlin format. marlin_zp = awq_to_marlin_zero_points( layer.qzeros, size_k=layer.num_groups, size_n=layer.output_size_per_partition, num_bits=self.quant_config.quant_type.size_bits, is_a_8bit=is_a_8bit, ) replace_parameter(layer, "qzeros", marlin_zp) # Not-used layer.g_idx = marlin_make_empty_g_idx(device) layer.g_idx_sort_indices = marlin_make_empty_g_idx(device) if hasattr(layer, "bias") and layer.bias is not None: layer.bias.data = marlin_permute_bias(layer.bias) def apply( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: return apply_awq_marlin_linear( input=x, weight=layer.qweight, weight_scale=layer.scales, weight_zp=layer.qzeros, g_idx=layer.g_idx, g_idx_sort_indices=layer.g_idx_sort_indices, workspace=layer.workspace, quant_type=self.quant_config.quant_type, output_size_per_partition=layer.output_size_per_partition, input_size_per_partition=layer.input_size_per_partition, input_global_scale=getattr(layer, "input_global_scale", None), bias=bias, input_dtype=self.input_dtype, ) class AWQMarlinMoEMethod(FusedMoEMethodBase): def __init__( self, quant_config: AWQMarlinConfig, moe: FusedMoEConfig, ): super().__init__(moe) self.quant_config = quant_config if self.quant_config.weight_bits != 4: raise ValueError("AWQMarlinMoEMethod only supports 4bit now.") self.quant_type = scalar_types.uint4 self.input_dtype = None self.use_marlin = True def create_weights( self, layer: torch.nn.Module, num_experts: int, hidden_size: int, intermediate_size_per_partition: int, params_dtype: torch.dtype, **extra_weight_attrs, ): layer.input_dtype = self.input_dtype extra_weight_attrs.update( { "is_transposed": True, "quant_method": FusedMoeWeightScaleSupported.GROUP.value, } ) intermediate_size_full = extra_weight_attrs.pop( "intermediate_size_full", intermediate_size_per_partition ) self.is_k_full = intermediate_size_per_partition == intermediate_size_full w13_qweight = Parameter( torch.empty( num_experts, hidden_size, 2 * intermediate_size_per_partition // self.quant_config.pack_factor, dtype=torch.int32, ), requires_grad=False, ) layer.register_parameter("w13_qweight", w13_qweight) set_weight_attrs(w13_qweight, extra_weight_attrs) w2_qweight = Parameter( torch.empty( num_experts, intermediate_size_per_partition, hidden_size // self.quant_config.pack_factor, dtype=torch.int32, ), requires_grad=False, ) layer.register_parameter("w2_qweight", w2_qweight) set_weight_attrs(w2_qweight, extra_weight_attrs) num_groups_w13 = hidden_size // self.quant_config.group_size num_groups_w2 = intermediate_size_per_partition // self.quant_config.group_size layer.num_groups_w13 = num_groups_w13 layer.num_groups_w2 = num_groups_w2 # WEIGHT_SCALES # Allocate 2 scales for w1 and w3 respectively. w13_scales = Parameter( torch.empty( num_experts, num_groups_w13, intermediate_size_per_partition * 2, dtype=params_dtype, ), requires_grad=False, ) layer.register_parameter("w13_scales", w13_scales) set_weight_attrs(w13_scales, extra_weight_attrs) w2_scales = Parameter( torch.empty(num_experts, num_groups_w2, hidden_size, dtype=params_dtype), requires_grad=False, ) layer.register_parameter("w2_scales", w2_scales) set_weight_attrs(w2_scales, extra_weight_attrs) # WEIGHT_ZERO_POINT # Allocate 2 zero points for w1 and w3 respectively. w13_qzeros = Parameter( torch.empty( num_experts, num_groups_w13, 2 * intermediate_size_per_partition // self.quant_config.pack_factor, dtype=torch.int32, ), requires_grad=False, ) layer.register_parameter("w13_qzeros", w13_qzeros) set_weight_attrs(w13_qzeros, extra_weight_attrs) w2_qzeros = Parameter( torch.empty( num_experts, num_groups_w2, hidden_size // self.quant_config.pack_factor, dtype=torch.int32, ), requires_grad=False, ) layer.register_parameter("w2_qzeros", w2_qzeros) set_weight_attrs(w2_qzeros, extra_weight_attrs) device = layer.w13_qweight.device layer.workspace = marlin_make_workspace_new(device, 4) def process_weights_after_loading(self, layer: torch.nn.Module) -> None: num_experts = layer.w13_qweight.shape[0] device = layer.w13_qweight.device is_a_8bit = self.input_dtype is not None and self.input_dtype.itemsize == 1 if self.input_dtype == torch.float8_e4m3fn: ops.marlin_int4_fp8_preprocess( layer.w13_qweight.view(-1, layer.w13_qweight.size(2)), layer.w13_qzeros.view(-1, layer.w13_qzeros.size(2)), inplace=True, ) ops.marlin_int4_fp8_preprocess( layer.w2_qweight.view(-1, layer.w2_qweight.size(2)), layer.w2_qzeros.view(-1, layer.w2_qzeros.size(2)), inplace=True, ) layer.w13_scales.data = layer.w13_scales.data * 512 layer.w2_scales.data = layer.w2_scales.data * 512 layer.w13_g_idx_sort_indices = torch.nn.Parameter( torch.empty((num_experts, 0), dtype=torch.int32, device=device), requires_grad=False, ) layer.w2_g_idx_sort_indices = torch.nn.Parameter( torch.empty((num_experts, 0), dtype=torch.int32, device=device), requires_grad=False, ) marlin_w13_qweight = ops.awq_marlin_moe_repack( layer.w13_qweight, layer.w13_g_idx_sort_indices, size_k=layer.w13_qweight.shape[1], size_n=layer.w13_qweight.shape[2] * self.quant_config.pack_factor, num_bits=self.quant_config.weight_bits, is_a_8bit=is_a_8bit, ) replace_parameter(layer, "w13_qweight", marlin_w13_qweight) marlin_w2_qweight = ops.awq_marlin_moe_repack( layer.w2_qweight, layer.w2_g_idx_sort_indices, size_k=layer.w2_qweight.shape[1], size_n=layer.w2_qweight.shape[2] * self.quant_config.pack_factor, num_bits=self.quant_config.weight_bits, is_a_8bit=is_a_8bit, ) replace_parameter(layer, "w2_qweight", marlin_w2_qweight) # The modular kernel expects w13_weight and w2_weight, # but AWQ uses w13_qweight and w2_qweight # Alias for modular kernel layer.w13_weight = layer.w13_qweight # Alias for modular kernel layer.w2_weight = layer.w2_qweight # Why does this take the intermediate size for size_k? marlin_w13_scales = marlin_moe_permute_scales( s=layer.w13_scales, size_k=layer.intermediate_size_per_partition, size_n=layer.w13_scales.shape[2], group_size=self.quant_config.group_size, is_a_8bit=is_a_8bit, ) if self.input_dtype == torch.int8 and layer.num_groups_w13 > 1: marlin_w13_scales, w13_input_global_scale = marlin_act_int8_process_scales( marlin_w13_scales ) layer.register_parameter( "w13_input_global_scale", Parameter(w13_input_global_scale, requires_grad=False), ) replace_parameter(layer, "w13_scales", marlin_w13_scales) marlin_w2_scales = marlin_moe_permute_scales( s=layer.w2_scales, size_k=layer.intermediate_size_per_partition, size_n=layer.w2_scales.shape[2], group_size=self.quant_config.group_size, is_a_8bit=is_a_8bit, ) if self.input_dtype == torch.int8 and layer.num_groups_w2 > 1: marlin_w2_scales, w2_input_global_scale = marlin_act_int8_process_scales( marlin_w2_scales ) layer.register_parameter( "w2_input_global_scale", Parameter(w2_input_global_scale, requires_grad=False), ) replace_parameter(layer, "w2_scales", marlin_w2_scales) marlin_w13_zp = moe_awq_to_marlin_zero_points( layer.w13_qzeros, size_k=layer.w13_qzeros.shape[1], size_n=layer.w13_qzeros.shape[2] * self.quant_config.pack_factor, num_bits=self.quant_config.weight_bits, is_a_8bit=is_a_8bit, ) replace_parameter(layer, "w13_qzeros", marlin_w13_zp) marlin_w2_zp = moe_awq_to_marlin_zero_points( layer.w2_qzeros, size_k=layer.w2_qzeros.shape[1], size_n=layer.w2_qzeros.shape[2] * self.quant_config.pack_factor, num_bits=self.quant_config.weight_bits, is_a_8bit=is_a_8bit, ) replace_parameter(layer, "w2_qzeros", marlin_w2_zp) if hasattr(layer, "w13_bias") and layer.w13_bias is not None: layer.w13_bias.data = marlin_permute_bias(layer.w13_bias) if hasattr(layer, "w2_bias") and layer.w2_bias is not None: layer.w2_bias.data = marlin_permute_bias(layer.w2_bias) def get_fused_moe_quant_config( self, layer: torch.nn.Module ) -> FusedMoEQuantConfig | None: from vllm.model_executor.layers.fused_moe.config import ( awq_marlin_moe_quant_config, ) return awq_marlin_moe_quant_config( w1_scale=layer.w13_scales, w2_scale=layer.w2_scales, weight_bits=self.quant_config.weight_bits, group_size=self.quant_config.group_size, w1_zp=getattr(layer, "w13_qzeros", None) if self.quant_config.zero_point else None, w2_zp=getattr(layer, "w2_qzeros", None) if self.quant_config.zero_point else None, w1_bias=getattr(layer, "w13_bias", None), w2_bias=getattr(layer, "w2_bias", None), ) def select_gemm_impl( self, prepare_finalize, layer: torch.nn.Module, ): """ Select the GEMM implementation for AWQ-Marlin MoE. Returns MarlinExperts configured for AWQ quantization. This is ONLY used when LoRA is enabled. Without LoRA, AWQ uses its own apply() method. """ # Only use modular kernels when LoRA is enabled # Without LoRA, AWQ's own apply() method works fine and is more efficient if not self.moe.is_lora_enabled: raise NotImplementedError( "AWQ-Marlin uses its own apply() method when LoRA is not enabled. " "Modular kernels are only used for LoRA support." ) from vllm.model_executor.layers.fused_moe import modular_kernel as mk from vllm.model_executor.layers.fused_moe.fused_marlin_moe import ( BatchedMarlinExperts, MarlinExperts, ) # Ensure quant config is initialized assert self.moe_quant_config is not None, ( "moe_quant_config must be initialized before select_gemm_impl" ) w13_g_idx = getattr(layer, "w13_g_idx", None) w2_g_idx = getattr(layer, "w2_g_idx", None) w13_g_idx_sort_indices = getattr(layer, "w13_g_idx_sort_indices", None) w2_g_idx_sort_indices = getattr(layer, "w2_g_idx_sort_indices", None) # Check if using batched expert format (for Expert Parallelism) if ( prepare_finalize.activation_format == mk.FusedMoEActivationFormat.BatchedExperts ): # For batched format, use BatchedMarlinExperts max_num_tokens_per_rank = prepare_finalize.max_num_tokens_per_rank() assert max_num_tokens_per_rank is not None return BatchedMarlinExperts( max_num_tokens=max_num_tokens_per_rank, num_dispatchers=prepare_finalize.num_dispatchers(), quant_config=self.moe_quant_config, w13_g_idx=w13_g_idx, w2_g_idx=w2_g_idx, w13_g_idx_sort_indices=w13_g_idx_sort_indices, w2_g_idx_sort_indices=w2_g_idx_sort_indices, is_k_full=self.is_k_full, ) else: # Standard Marlin experts for AWQ return MarlinExperts( quant_config=self.moe_quant_config, w13_g_idx=w13_g_idx, w2_g_idx=w2_g_idx, w13_g_idx_sort_indices=w13_g_idx_sort_indices, w2_g_idx_sort_indices=w2_g_idx_sort_indices, is_k_full=self.is_k_full, ) def apply( self, layer: FusedMoE, x: torch.Tensor, router_logits: torch.Tensor, ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]: assert layer.activation == "silu", "Only SiLU activation is supported." topk_weights, topk_ids = layer.select_experts( hidden_states=x, router_logits=router_logits, ) return fused_marlin_moe( x, layer.w13_qweight, layer.w2_qweight, getattr(layer, "w13_bias", None), getattr(layer, "w2_bias", None), layer.w13_scales, layer.w2_scales, router_logits, topk_weights, topk_ids, input_global_scale1=getattr(layer, "w13_input_global_scale", None), input_global_scale2=getattr(layer, "w2_input_global_scale", None), quant_type_id=self.quant_type.id, apply_router_weight_on_input=layer.apply_router_weight_on_input, global_num_experts=layer.global_num_experts, expert_map=layer.expert_map, w1_zeros=layer.w13_qzeros, w2_zeros=layer.w2_qzeros, workspace=layer.workspace, input_dtype=self.input_dtype, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py
vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import enum from enum import Enum import torch from compressed_tensors import CompressionFormat from compressed_tensors.quantization import ( ActivationOrdering, QuantizationArgs, QuantizationStrategy, ) from torch.nn.parameter import Parameter import vllm.envs as envs import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm import _custom_ops as ops from vllm._aiter_ops import rocm_aiter_ops from vllm.distributed import get_tensor_model_parallel_world_size from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe import ( FusedMoE, FusedMoEActivationFormat, FusedMoEConfig, FusedMoEMethodBase, FusedMoEPermuteExpertsUnpermute, FusedMoeWeightScaleSupported, UnquantizedFusedMoEMethod, ) from vllm.model_executor.layers.fused_moe.config import ( FusedMoEQuantConfig, fp8_w8a8_moe_quant_config, int4_w4a16_moe_quant_config, int4_w4afp8_moe_quant_config, int8_w8a8_moe_quant_config, int8_w8a16_moe_quant_config, nvfp4_moe_quant_config, ) from vllm.model_executor.layers.fused_moe.cpu_fused_moe import select_experts from vllm.model_executor.layers.fused_moe.flashinfer_cutlass_moe import ( is_valid_flashinfer_cutlass_fused_moe, ) from vllm.model_executor.layers.fused_moe.fused_marlin_moe import ( BatchedMarlinExperts, MarlinExperts, fused_marlin_moe, ) from vllm.model_executor.layers.quantization.compressed_tensors.schemes.compressed_tensors_wNa16 import ( # noqa WNA16_SUPPORTED_BITS, WNA16_SUPPORTED_TYPES_MAP, ) from vllm.model_executor.layers.quantization.utils import replace_parameter from vllm.model_executor.layers.quantization.utils.flashinfer_fp4_moe import ( build_flashinfer_fp4_cutlass_moe_prepare_finalize, flashinfer_trtllm_fp4_moe, prepare_static_weights_for_trtllm_fp4_moe, reorder_w1w3_to_w3w1, select_nvfp4_gemm_impl, ) from vllm.model_executor.layers.quantization.utils.flashinfer_utils import ( FlashinferMoeBackend, get_flashinfer_moe_backend, ) from vllm.model_executor.layers.quantization.utils.fp8_utils import ( expert_weight_is_col_major, requant_weight_ue8m0_inplace, ) from vllm.model_executor.layers.quantization.utils.marlin_utils import ( check_moe_marlin_supports_layer, get_marlin_input_dtype, marlin_act_int8_process_scales, marlin_make_workspace_new, marlin_moe_permute_scales, ) from vllm.model_executor.layers.quantization.utils.marlin_utils_fp4 import ( prepare_moe_fp4_layer_for_marlin, ) from vllm.model_executor.layers.quantization.utils.marlin_utils_fp8 import ( prepare_moe_fp8_layer_for_marlin, ) from vllm.model_executor.layers.quantization.utils.quant_utils import ( convert_bf16_scales_to_fp8, convert_packed_uint4b8_to_signed_int4_inplace, swizzle_blockscale, ) from vllm.model_executor.layers.quantization.utils.w8a8_utils import ( all_close_1d, normalize_e4m3fn_to_e4m3fnuz, per_tensor_dequantize, ) from vllm.model_executor.utils import set_weight_attrs from vllm.platforms import CpuArchEnum, current_platform from vllm.scalar_type import scalar_types from vllm.utils.deep_gemm import ( get_col_major_tma_aligned_tensor, get_mk_alignment_for_contiguous_layout, is_deep_gemm_e8m0_used, is_deep_gemm_supported, ) from vllm.utils.import_utils import has_deep_gemm logger = init_logger(__name__) class GPTQMarlinState(Enum): REPACK = enum.auto() READY = enum.auto() __all__ = [ "CompressedTensorsMoEMethod", "CompressedTensorsW8A8Fp8MoEMethod", "CompressedTensorsW8A8Int8MoEMethod", "CompressedTensorsWNA16MarlinMoEMethod", "CompressedTensorsWNA16MoEMethod", "CompressedTensorsW4A4Nvfp4MoEMethod", "CompressedTensorsW4A8Int8MoEMethod", ] class CompressedTensorsMoEMethod(FusedMoEMethodBase): @staticmethod def get_moe_method( quant_config: "CompressedTensorsConfig", # type: ignore # noqa E501 layer: torch.nn.Module, layer_name: str, ) -> "CompressedTensorsMoEMethod": # FusedMoE was made by combining multiple Linears so need to # make sure quantization config for Linear can target it quant_config._add_fused_moe_to_target_scheme_map() unfused_names = [ layer_name + proj_name for proj_name in [".0.gate_proj", ".0.up_proj", ".0.down_proj"] ] # TODO: refactor this to use expert_mapping and check all layer numbers all_scheme_dicts = [ quant_config.get_scheme_dict(layer, name) for name in unfused_names ] scheme_dict = all_scheme_dicts.pop() # multiple schemes found if not all([cur_dict == scheme_dict for cur_dict in all_scheme_dicts]): raise ValueError( "All MoE projections need to have same " "quantization scheme but found multiple" ) if scheme_dict is None: # ignored layer return UnquantizedFusedMoEMethod(layer.moe_config) # TODO: @dsikka: refactor this to use schemes as other kernels # are supported + check if the layer is being ignored. weight_quant = scheme_dict.get("weights") input_quant = scheme_dict.get("input_activations") format = scheme_dict.get("format") if quant_config._is_wNa16_group_channel(weight_quant, input_quant): # group_size=None means channelwise group_size = weight_quant.group_size or -1 valid_format_and_bits = ( weight_quant.num_bits in WNA16_SUPPORTED_BITS and format == CompressionFormat.pack_quantized.value ) if not valid_format_and_bits: raise ValueError( "For Fused MoE layers, only format: ", f"{CompressionFormat.pack_quantized.value} ", f" and bits: {WNA16_SUPPORTED_BITS} is supported ", f"but got format: {CompressionFormat.pack_quantized.value} " f" and bits: {weight_quant.num_bits}", ) # Prefer to use the MarlinMoE kernel when it is supported. if ( not check_moe_marlin_supports_layer(layer, group_size) or current_platform.is_rocm() ): if ( weight_quant.strategy == QuantizationStrategy.GROUP and weight_quant.actorder in (ActivationOrdering.GROUP, ActivationOrdering.DYNAMIC) ): raise ValueError( "WNA16MoE is not supported with actorder=group/dynamic." ) logger.info_once("Using CompressedTensorsWNA16MoEMethod") return CompressedTensorsWNA16MoEMethod( weight_quant, input_quant, layer.moe_config ) else: logger.info_once("Using CompressedTensorsWNA16MarlinMoEMethod") return CompressedTensorsWNA16MarlinMoEMethod( weight_quant, input_quant, layer.moe_config ) elif quant_config._is_fp4a4_nvfp4(weight_quant, input_quant): return CompressedTensorsW4A4Nvfp4MoEMethod(layer.moe_config, layer_name) elif ( quant_config._is_fp8_w8a8_sm90(weight_quant, input_quant) or quant_config._is_fp8_w8a8_sm100(weight_quant, input_quant) or quant_config._is_fp8_w8a8(weight_quant, input_quant) ): return CompressedTensorsW8A8Fp8MoEMethod( weight_quant, input_quant, layer.moe_config ) elif quant_config._is_dynamic_token_w8a8(weight_quant, input_quant): return CompressedTensorsW8A8Int8MoEMethod( weight_quant, input_quant, layer.moe_config ) elif quant_config._is_fp8_w4a8_sm90(weight_quant, input_quant): logger.info_once("Using CompressedTensorsW4A8Fp8MoEMethod") return CompressedTensorsW4A8Fp8MoEMethod( weight_quant, input_quant, layer.moe_config ) elif quant_config._is_dynamic_token_w4a8_int(weight_quant, input_quant): return CompressedTensorsW4A8Int8MoEMethod( weight_quant, input_quant, layer.moe_config ) else: raise RuntimeError( f"Unsupported FusedMoe scheme: {weight_quant}, {input_quant}" ) class CompressedTensorsW4A4Nvfp4MoEMethod(CompressedTensorsMoEMethod): def __init__(self, moe: FusedMoEConfig, layer_name: str | None = None): from vllm.model_executor.layers.quantization.utils.nvfp4_moe_support import ( # noqa: E501 detect_nvfp4_moe_support, ) super().__init__(moe) _nvfp4 = detect_nvfp4_moe_support(self.__class__.__name__) self.cutlass_nvfp4_supported = _nvfp4.cutlass_supported self.allow_flashinfer = _nvfp4.allow_flashinfer self.use_marlin = _nvfp4.use_marlin self.group_size = 16 self.layer_name = layer_name self.marlin_input_dtype = ( get_marlin_input_dtype(layer_name) if self.use_marlin else None ) self.flashinfer_moe_backend = None if self.allow_flashinfer: self.flashinfer_moe_backend = get_flashinfer_moe_backend() logger.info_once( f"Using FlashInfer {self.flashinfer_moe_backend.value} kernels" " for CompressedTensorsW4A4Nvfp4MoEMethod." ) elif self.use_marlin: logger.info_once("Using Marlin for CompressedTensorsW4A4Nvfp4MoEMethod.") else: logger.info_once("Using Cutlass for CompressedTensorsW4A4Nvfp4MoEMethod.") def create_weights( self, layer: torch.nn.Module, num_experts: int, hidden_size: int, intermediate_size_per_partition: int, params_dtype: torch.dtype, **extra_weight_attrs, ): layer.num_experts = num_experts layer.params_dtype = params_dtype w13_weight = torch.nn.Parameter( torch.empty( num_experts, 2 * intermediate_size_per_partition, # 2 fp4 items are packed in the input dimension hidden_size // 2, requires_grad=False, dtype=torch.uint8, ), requires_grad=False, ) layer.register_parameter("w13_weight_packed", w13_weight) set_weight_attrs(w13_weight, extra_weight_attrs) w2_weight = torch.nn.Parameter( torch.empty( num_experts, hidden_size, # 2 fp4 items are packed in the input dimension intermediate_size_per_partition // 2, dtype=torch.uint8, ), requires_grad=False, ) layer.register_parameter("w2_weight_packed", w2_weight) set_weight_attrs(w2_weight, extra_weight_attrs) # Weight Scales w13_weight_scale = torch.nn.Parameter( torch.empty( num_experts, 2 * intermediate_size_per_partition, # 2 fp4 items are packed in the input dimension hidden_size // self.group_size, dtype=torch.float8_e4m3fn, ), requires_grad=False, ) layer.register_parameter("w13_weight_scale", w13_weight_scale) extra_weight_attrs.update( {"quant_method": FusedMoeWeightScaleSupported.GROUP.value} ) set_weight_attrs(w13_weight_scale, extra_weight_attrs) w2_weight_scale = torch.nn.Parameter( torch.empty( num_experts, hidden_size, # 2 fp4 items are packed in the input dimension intermediate_size_per_partition // self.group_size, dtype=torch.float8_e4m3fn, ), requires_grad=False, ) layer.register_parameter("w2_weight_scale", w2_weight_scale) extra_weight_attrs.update( {"quant_method": FusedMoeWeightScaleSupported.GROUP.value} ) set_weight_attrs(w2_weight_scale, extra_weight_attrs) # Weight Global Scales w13_weight_scale_2 = torch.nn.Parameter( torch.empty(num_experts, 2, dtype=torch.float32), requires_grad=False ) layer.register_parameter("w13_weight_global_scale", w13_weight_scale_2) extra_weight_attrs.update( {"quant_method": FusedMoeWeightScaleSupported.TENSOR.value} ) set_weight_attrs(w13_weight_scale_2, extra_weight_attrs) w2_weight_scale_2 = torch.nn.Parameter( torch.empty(num_experts, dtype=torch.float32), requires_grad=False ) layer.register_parameter("w2_weight_global_scale", w2_weight_scale_2) extra_weight_attrs.update( {"quant_method": FusedMoeWeightScaleSupported.TENSOR.value} ) set_weight_attrs(w2_weight_scale_2, extra_weight_attrs) # Input Global Scales w13_input_scale = torch.nn.Parameter( torch.empty(num_experts, 2, dtype=torch.float32), requires_grad=False ) layer.register_parameter("w13_input_global_scale", w13_input_scale) extra_weight_attrs.update( {"quant_method": FusedMoeWeightScaleSupported.TENSOR.value} ) set_weight_attrs(w13_input_scale, extra_weight_attrs) w2_input_scale = torch.nn.Parameter( torch.empty(num_experts, dtype=torch.float32), requires_grad=False ) layer.register_parameter("w2_input_global_scale", w2_input_scale) extra_weight_attrs.update( {"quant_method": FusedMoeWeightScaleSupported.TENSOR.value} ) set_weight_attrs(w2_input_scale, extra_weight_attrs) def process_weights_after_loading(self, layer: torch.nn.Module) -> None: # From packed to weight layer.w13_weight = torch.nn.Parameter( layer.w13_weight_packed.data, requires_grad=False ) delattr(layer, "w13_weight_packed") layer.w2_weight = torch.nn.Parameter( layer.w2_weight_packed.data, requires_grad=False ) delattr(layer, "w2_weight_packed") # reorder GEMM1 weights and block scales for FlashInfer CUTLASS kernel. if self.allow_flashinfer: w, s = reorder_w1w3_to_w3w1( layer.w13_weight.data, layer.w13_weight_scale.data, dim=-2 ) layer.w13_weight = torch.nn.Parameter(w, requires_grad=False) layer.w13_weight_scale = torch.nn.Parameter(s, requires_grad=False) if not torch.allclose( layer.w13_weight_global_scale[:, 0], layer.w13_weight_global_scale[:, 1] ): logger.warning_once( "w1_weight_global_scale must match w3_weight_global_scale. " "Accuracy may be affected." ) # Take inverse of global scale saved to disk layer.w13_weight_scale_2 = torch.nn.Parameter( 1 / layer.w13_weight_global_scale[:, 0], requires_grad=False ) layer.w2_weight_scale_2 = torch.nn.Parameter( 1 / layer.w2_weight_global_scale.data, requires_grad=False ) if self.use_marlin: prepare_moe_fp4_layer_for_marlin(layer, input_dtype=self.marlin_input_dtype) return # w13 if ( self.allow_flashinfer and self.flashinfer_moe_backend == FlashinferMoeBackend.TENSORRT_LLM ): w13_input_global_scale = ( layer.w13_input_global_scale.min() .to(torch.float32) .expand(layer.num_experts) ) else: w13_input_global_scale = layer.w13_input_global_scale.min(dim=1).values.to( torch.float32 ) layer.g1_alphas = torch.nn.Parameter( ((1 / w13_input_global_scale) * layer.w13_weight_scale_2), requires_grad=False, ) layer.w13_input_scale_quant = torch.nn.Parameter( (w13_input_global_scale), requires_grad=False ) # w2 if ( self.allow_flashinfer and self.flashinfer_moe_backend == FlashinferMoeBackend.TENSORRT_LLM ): w2_input_global_scale = ( layer.w2_input_global_scale.min() .to(torch.float32) .expand(layer.num_experts) ) else: w2_input_global_scale = layer.w2_input_global_scale layer.g2_alphas = torch.nn.Parameter( ((1 / w2_input_global_scale) * layer.w2_weight_scale_2).to(torch.float32), requires_grad=False, ) layer.w2_input_scale_quant = torch.nn.Parameter( (w2_input_global_scale), requires_grad=False ) # TensorRT-LLM specific processing if ( self.allow_flashinfer and self.flashinfer_moe_backend == FlashinferMoeBackend.TENSORRT_LLM ): # Prepare static weights for TRT-LLM kernel # alternate: prepare_static_weight_layouts_for_trtllm_moe ( gemm1_weights_fp4_shuffled, gemm1_scales_fp4_shuffled, gemm2_weights_fp4_shuffled, gemm2_scales_fp4_shuffled, ) = prepare_static_weights_for_trtllm_fp4_moe( layer.w13_weight, layer.w2_weight, layer.w13_weight_scale, layer.w2_weight_scale, layer.w2_weight.size(-2), # hidden_size layer.w13_weight.size(-2) // 2, # intermediate_size layer.w13_weight.size(0), # num_experts ) logger.debug_once("Finished shuffling weights for TRT-LLM MOE") layer.w13_weight = Parameter( gemm1_weights_fp4_shuffled, requires_grad=False ) layer.w2_weight = Parameter(gemm2_weights_fp4_shuffled, requires_grad=False) layer.w13_weight_scale = Parameter( gemm1_scales_fp4_shuffled, requires_grad=False ) layer.w2_weight_scale = Parameter( gemm2_scales_fp4_shuffled, requires_grad=False ) # Additional parameter needed for TRT-LLM layer.g1_scale_c = Parameter( (layer.w2_input_scale_quant * layer.g1_alphas).to(torch.float32), requires_grad=False, ) else: # swizzle weight scales layer.w13_weight_scale = torch.nn.Parameter( swizzle_blockscale(layer.w13_weight_scale), requires_grad=False ) layer.w2_weight_scale = torch.nn.Parameter( swizzle_blockscale(layer.w2_weight_scale), requires_grad=False ) def maybe_make_prepare_finalize( self, routing_tables: tuple[torch.Tensor, torch.Tensor, torch.Tensor] | None = None, ) -> mk.FusedMoEPrepareAndFinalize | None: if self.use_marlin or ( self.allow_flashinfer and self.flashinfer_moe_backend == FlashinferMoeBackend.TENSORRT_LLM ): return None elif not self.allow_flashinfer: return super().maybe_make_prepare_finalize(routing_tables) prepare_finalize = build_flashinfer_fp4_cutlass_moe_prepare_finalize(self.moe) logger.debug_once("%s", prepare_finalize.__class__.__name__) return prepare_finalize def select_gemm_impl( self, prepare_finalize: mk.FusedMoEPrepareAndFinalize, layer: torch.nn.Module, ) -> mk.FusedMoEPermuteExpertsUnpermute: assert self.moe_quant_config is not None """Return the appropriate GEMM experts implementation.""" experts = select_nvfp4_gemm_impl( self.moe, self.moe_quant_config, allow_flashinfer=self.allow_flashinfer, ) logger.debug_once("Using %s", experts.__class__.__name__) return experts def get_fused_moe_quant_config( self, layer: torch.nn.Module ) -> FusedMoEQuantConfig | None: if ( self.use_marlin or self.flashinfer_moe_backend == FlashinferMoeBackend.TENSORRT_LLM ): return None return nvfp4_moe_quant_config( g1_alphas=layer.g1_alphas, g2_alphas=layer.g2_alphas, a1_gscale=layer.w13_input_scale_quant, a2_gscale=layer.w2_input_scale_quant, w1_scale=layer.w13_weight_scale, w2_scale=layer.w2_weight_scale, ) def apply( self, layer: FusedMoE, x: torch.Tensor, router_logits: torch.Tensor, ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]: assert layer.activation == "silu", "Only SiLU activation is supported." if ( self.allow_flashinfer and self.flashinfer_moe_backend == FlashinferMoeBackend.TENSORRT_LLM ): if layer.enable_eplb: raise NotImplementedError( "EPLB not supported for `CompressedTensorsW4A4MoEMethod` yet." ) return flashinfer_trtllm_fp4_moe( layer=layer, x=x, router_logits=router_logits, top_k=layer.top_k, global_num_experts=layer.global_num_experts, num_expert_group=layer.num_expert_group, topk_group=layer.topk_group, custom_routing_function=layer.custom_routing_function, e_score_correction_bias=layer.e_score_correction_bias, ) topk_weights, topk_ids = layer.select_experts( hidden_states=x, router_logits=router_logits, ) if self.use_marlin: return fused_marlin_moe( x, layer.w13_weight, layer.w2_weight, None, None, layer.w13_weight_scale, layer.w2_weight_scale, router_logits, topk_weights, topk_ids, global_scale1=layer.w13_weight_scale_2, global_scale2=layer.w2_weight_scale_2, quant_type_id=scalar_types.float4_e2m1f.id, apply_router_weight_on_input=layer.apply_router_weight_on_input, global_num_experts=layer.global_num_experts, expert_map=layer.expert_map, input_dtype=self.marlin_input_dtype, workspace=layer.workspace, ) # FlashInfer fused experts path elif self.allow_flashinfer: from vllm.model_executor.layers.fused_moe.flashinfer_cutlass_moe import ( # noqa: E501 flashinfer_cutlass_moe_fp4, ) assert is_valid_flashinfer_cutlass_fused_moe( x, layer.w13_weight, layer.w2_weight ), "Flashinfer CUTLASS Fused MoE not applicable!" assert self.moe_quant_config is not None return flashinfer_cutlass_moe_fp4( hidden_states=x, w1=layer.w13_weight, w2=layer.w2_weight, topk_weights=topk_weights, topk_ids=topk_ids, quant_config=self.moe_quant_config, inplace=False, # TODO(shuw): fix later, now output is high prec activation=layer.activation, global_num_experts=layer.global_num_experts, expert_map=layer.expert_map, apply_router_weight_on_input=layer.apply_router_weight_on_input, ) else: # If no modular kernel is provided, use cutlass_moe_fp4 for TP case # only (no EP). from vllm.model_executor.layers.fused_moe.cutlass_moe import cutlass_moe_fp4 assert self.moe_quant_config is not None return cutlass_moe_fp4( a=x, w1_fp4=layer.w13_weight, w2_fp4=layer.w2_weight, topk_weights=topk_weights, topk_ids=topk_ids, quant_config=self.moe_quant_config, expert_map=layer.expert_map, apply_router_weight_on_input=layer.apply_router_weight_on_input, # TODO(bnell): derive these from arguments m=x.shape[0], n=layer.w2_weight.shape[2] * 2, k=x.shape[1], e=layer.w13_weight.shape[0], ).to(x.dtype) class CompressedTensorsW8A8Fp8MoEMethod(CompressedTensorsMoEMethod): def __init__( self, weight_quant: QuantizationArgs, input_quant: QuantizationArgs, moe: FusedMoEConfig, layer_name: str | None = None, ): from vllm.model_executor.layers.quantization.compressed_tensors.compressed_tensors import ( # noqa: E501 CompressedTensorsConfig, ) super().__init__(moe) self.weight_quant = weight_quant self.input_quant = input_quant per_tensor = ( self.weight_quant.strategy == QuantizationStrategy.TENSOR and self.input_quant.strategy == QuantizationStrategy.TENSOR ) per_channel = ( self.weight_quant.strategy == QuantizationStrategy.CHANNEL and self.input_quant.strategy == QuantizationStrategy.TOKEN ) if not (per_tensor or per_channel): assert self.weight_quant.strategy == QuantizationStrategy.BLOCK self.weight_block_size = self.weight_quant.block_structure assert self.weight_quant.dynamic is not None else: self.weight_block_size = None self.block_quant = self.weight_block_size is not None self.static_input_scales = not self.input_quant.dynamic if self.static_input_scales and per_channel: raise ValueError( "For FP8 Fused MoE layer, we require either per tensor or " "channelwise, dynamic per token quantization." ) # For GPUs that lack FP8 hardware support, we can leverage the Marlin # kernel for fast weight-only FP8 quantization self.use_marlin = ( not current_platform.has_device_capability(89) or envs.VLLM_TEST_FORCE_FP8_MARLIN and not self.block_quant ) # Disable marlin for rocm if current_platform.is_rocm(): self.use_marlin = False self.rocm_aiter_moe_enabled = rocm_aiter_ops.is_fused_moe_enabled() # cutlass path self.is_fp8_w8a8_sm100 = CompressedTensorsConfig._is_fp8_w8a8_sm100( self.weight_quant, self.input_quant ) self.use_cutlass = not self.block_quant and ( CompressedTensorsConfig._is_fp8_w8a8_sm90( self.weight_quant, self.input_quant ) or self.is_fp8_w8a8_sm100 ) self.disable_expert_map = False self.layer_name = layer_name self.marlin_input_dtype = ( get_marlin_input_dtype(layer_name) if self.use_marlin else None ) self.allow_deep_gemm = ( self.block_quant and envs.VLLM_MOE_USE_DEEP_GEMM and is_deep_gemm_supported() and list(self.weight_block_size) == get_mk_alignment_for_contiguous_layout() ) def create_weights( self, layer: torch.nn.Module, num_experts: int, hidden_size: int, intermediate_size_per_partition: int, params_dtype: torch.dtype, **extra_weight_attrs, ): layer.intermediate_size_per_partition = intermediate_size_per_partition layer.hidden_size = hidden_size layer.num_experts = num_experts layer.orig_dtype = params_dtype layer.weight_block_size = None params_dtype = torch.float8_e4m3fn if self.block_quant: assert self.weight_block_size is not None layer.weight_block_size = self.weight_block_size tp_size = get_tensor_model_parallel_world_size() block_n, block_k = ( self.weight_block_size[0], self.weight_block_size[1], ) # NOTE: To ensure proper alignment of the block-wise quantization # scales, the output_size of the weights for both the gate and up # layers must be divisible by block_n. # Required by column parallel or enabling merged weights if intermediate_size_per_partition % block_n != 0: raise ValueError( f"The output_size of gate's and up's weight = " f"{intermediate_size_per_partition} is not divisible by " f"weight quantization block_n = {block_n}." ) if tp_size > 1 and intermediate_size_per_partition % block_k != 0: # Required by row parallel raise ValueError( f"The input_size of down's weight = " f"{intermediate_size_per_partition} is not divisible by " f"weight quantization block_k = {block_k}." ) # WEIGHTS w13_weight = torch.nn.Parameter( torch.empty( num_experts, 2 * intermediate_size_per_partition, hidden_size, dtype=params_dtype, ), requires_grad=False, ) layer.register_parameter("w13_weight", w13_weight) set_weight_attrs(w13_weight, extra_weight_attrs) w2_weight = torch.nn.Parameter( torch.empty( num_experts, hidden_size, intermediate_size_per_partition, dtype=params_dtype, ), requires_grad=False, ) layer.register_parameter("w2_weight", w2_weight) set_weight_attrs(w2_weight, extra_weight_attrs) # WEIGHT_SCALES if self.weight_quant.strategy == QuantizationStrategy.TENSOR: # Allocate 2 scales for w1 and w3 respectively. # They are combined to a single scale after weight loading. w13_weight_scale = torch.nn.Parameter( torch.ones(num_experts, 2, dtype=torch.float32), requires_grad=False ) layer.register_parameter("w13_weight_scale", w13_weight_scale) w2_weight_scale = torch.nn.Parameter( torch.ones(num_experts, dtype=torch.float32), requires_grad=False ) layer.register_parameter("w2_weight_scale", w2_weight_scale) # Add PER-TENSOR quantization for FusedMoE.weight_loader. extra_weight_attrs.update( {"quant_method": FusedMoeWeightScaleSupported.TENSOR.value} ) set_weight_attrs(w13_weight_scale, extra_weight_attrs) set_weight_attrs(w2_weight_scale, extra_weight_attrs) elif self.weight_quant.strategy == QuantizationStrategy.CHANNEL: w13_weight_scale = torch.nn.Parameter( torch.ones( num_experts, 2 * intermediate_size_per_partition, 1, dtype=torch.float32, ), requires_grad=False, ) layer.register_parameter("w13_weight_scale", w13_weight_scale) w2_weight_scale = torch.nn.Parameter( torch.ones(num_experts, hidden_size, 1, dtype=torch.float32), requires_grad=False, ) layer.register_parameter("w2_weight_scale", w2_weight_scale) # Add PER-CHANNEL quantization for FusedMoE.weight_loader. extra_weight_attrs.update( {"quant_method": FusedMoeWeightScaleSupported.CHANNEL.value} ) set_weight_attrs(w13_weight_scale, extra_weight_attrs) set_weight_attrs(w2_weight_scale, extra_weight_attrs) elif self.weight_quant.strategy == QuantizationStrategy.BLOCK: w13_weight_scale = torch.nn.Parameter( torch.ones( num_experts,
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
true
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/utils.py
vllm/model_executor/layers/quantization/compressed_tensors/utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Iterable, Mapping from types import MappingProxyType import regex as re from compressed_tensors import CompressionFormat from torch.nn import Module def is_activation_quantization_format(format: str) -> bool: _ACTIVATION_QUANTIZATION_FORMATS = [ CompressionFormat.naive_quantized.value, CompressionFormat.int_quantized.value, CompressionFormat.float_quantized.value, CompressionFormat.nvfp4_pack_quantized.value, ] return format in _ACTIVATION_QUANTIZATION_FORMATS def should_ignore_layer( layer_name: str | None, ignore: Iterable[str] = tuple(), fused_mapping: Mapping[str, list[str]] = MappingProxyType({}), ) -> bool: if layer_name is None: return False # layer_name = model.layers.0.self_attn.qkv_proj # proj_name = qkv_proj proj_name = layer_name.split(".")[-1] # Fused layers like gate_up_proj or qkv_proj will not be fused # in the safetensors checkpoint. So, we convert the name # from the fused version to unfused + check to make sure that # each shard of the fused layer has the same scheme. if proj_name in fused_mapping and layer_name not in ignore: shard_proj_names = fused_mapping[proj_name] # Convert fused_name --> [shard_names] shard_names = [ layer_name.replace(proj_name, shard_proj_name) for shard_proj_name in shard_proj_names ] # Layer should be ignored if shards are ignored. should_ignore_layer = None for shard_name in shard_names: should_ignore_shard = check_equal_or_regex_match( layer_name=shard_name, targets=ignore ) # If shard_idx=0, set layer ignore to match shard. if should_ignore_layer is None: should_ignore_layer = should_ignore_shard # If shard_idx=1+ confirm scheme matches prior shards. elif should_ignore_shard != should_ignore_layer: raise ValueError( f"Found a different quantization schemes for " f"{shard_proj_names} in {layer_name}. vLLM " "requires all to use the same scheme." ) # Unfused layers like down_proj and o_proj will match # the safetensors checkpoint already. else: should_ignore_layer = check_equal_or_regex_match( layer_name=layer_name, targets=ignore ) assert should_ignore_layer is not None return should_ignore_layer def check_equal_or_regex_match(layer_name: str, targets: Iterable[str]) -> bool: """ Checks whether a layer_name is exactly equal or a regex match for if target starts with 're:' to any target in list. """ return any(_is_equal_or_regex_match(layer_name, target) for target in targets) def find_matched_target( layer_name: str | None, module: Module, targets: Iterable[str], fused_mapping: Mapping[str, list[str]] = MappingProxyType({}), ) -> str: """ Helper function to look up which "target" in the compressed-tensors config that a layer corresponds to. Recall that a compressed-tensors configs has a concept of config_groups, where each layer can be quantized with a different scheme. targets in each config_group will be a list of either layer names (or regexes corresponding to layer names) or names of torch Modules. First, we try to match the layer_name with a target Second, we try to match the module's name with a target Third, we try to map the layer_name to a list of fused module names. *All* component module names must match in order for a match to be successful. A successful match returns the first component target :param layer_name: layer name :param module: torch.nn.Module :param targets: list of targets to match the layer against :param fused_mapping: map from fused layer names to its components :param fused_strategy: either "all" or "any". If using "all", fused layers match if "all" of its components match """ if layer_name is None: layer_name = "" matched_target = ( _find_first_match(layer_name, targets) or _find_first_match(module.__class__.__name__, targets, True) or _match_fused_layer(layer_name, targets, fused_mapping) ) if matched_target is None: raise ValueError( f"Unable to find matching target for {layer_name} in the " "compressed-tensors config." ) return matched_target def _find_first_match( value: str, targets: Iterable[str], check_contains: bool = False ) -> str | None: """ Returns first element of target that matches value either exactly or as a regex after 're:'. If check_contains is set to True, additionally checks if the target string is contained within the value. :param value: string to compare the list of targets against :param targets: list of targets to match the layer against :param check_contains: whether or not to do a substring match """ for target in targets: if _is_equal_or_regex_match(value, target, check_contains=check_contains): return target return None def _is_equal_or_regex_match( value: str, target: str, check_contains: bool = False ) -> bool: """ Checks whether a value is exactly equal or a regex match for target if target starts with 're:'. If check_contains is set to True, additionally checks if the target string is contained within the value. """ if target.startswith("re:"): pattern = target[3:] if re.match(pattern, value): return True elif check_contains: if target.lower() in value.lower(): return True elif target == value: return True return False def _match_fused_layer( layer_name: str, target_layers: Iterable[str], fused_mapping: Mapping[str, list[str]], ) -> str | None: """ Match a fused layer name to its corresponding individual layer in target_layers. Returns first value in fused_mapping which matches targets Implements an "all" matching strategy where a fused layer matches iff "all" of its components match :param layer_name: layer name :param target_layers: list of targets to match the layer against :param fused_mapping: map from fused layer names to its components Examples: layer_name = "model.layers.0.self_attn.qkv_proj" target_layers = ["model.layers.0.self_attn.q_proj", "model.layers.0.self_attn.k_proj", "model.layers.0.self_attn.v_proj"] """ # find layer_name in mapping fused = next((key for key in fused_mapping if layer_name.endswith(key)), None) if fused is None: return None # expand path of unfused components unfused_paths = [ layer_name.replace(fused, unfused) for unfused in fused_mapping[fused] ] # for each unfused component, find a match in targets unfused_matches: list[str | None] = [] for unfused in unfused_paths: for target in target_layers: if _is_equal_or_regex_match(unfused, target): unfused_matches.append(target) break else: unfused_matches.append(None) return unfused_matches[0] if all(unfused_matches) else None
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors.py
vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from contextlib import suppress from typing import TYPE_CHECKING, Any, Literal, Optional, cast import torch from compressed_tensors.config import ( CompressionFormat, SparsityCompressionConfig, SparsityStructure, ) from compressed_tensors.quantization import ( QuantizationArgs, QuantizationStrategy, QuantizationType, ) from compressed_tensors.transform import TransformConfig import vllm.envs as envs from vllm.attention.layer import Attention from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe import FusedMoE from vllm.model_executor.layers.linear import ( LinearBase, LinearMethodBase, UnquantizedLinearMethod, ) from vllm.model_executor.layers.quantization import QuantizationMethods from vllm.model_executor.layers.quantization.base_config import ( QuantizationConfig, QuantizeMethodBase, ) from vllm.model_executor.layers.quantization.compressed_tensors.compressed_tensors_moe import ( # noqa: E501 CompressedTensorsMoEMethod, ) from vllm.model_executor.layers.quantization.compressed_tensors.schemes import ( W4A16SPARSE24_SUPPORTED_BITS, WNA16_SUPPORTED_BITS, CompressedTensors24, CompressedTensorsScheme, CompressedTensorsW4A4Fp4, CompressedTensorsW4A8Fp8, CompressedTensorsW4A8Int, CompressedTensorsW4A16Fp4, CompressedTensorsW4A16Sparse24, CompressedTensorsW8A8Fp8, CompressedTensorsW8A8Int8, CompressedTensorsW8A16Fp8, CompressedTensorsWNA16, ) from vllm.model_executor.layers.quantization.compressed_tensors.transform.linear import ( # noqa: E501 CompressedTensorsLinearTransformMethod, get_linear_transform_schemes, ) from vllm.model_executor.layers.quantization.compressed_tensors.utils import ( find_matched_target, is_activation_quantization_format, should_ignore_layer, ) from vllm.model_executor.layers.quantization.kv_cache import BaseKVCacheMethod from vllm.model_executor.layers.quantization.utils.quant_utils import ( cutlass_fp4_supported, ) from vllm.platforms import current_platform if TYPE_CHECKING: from vllm.model_executor.models.utils import WeightsMapper logger = init_logger(__name__) __all__ = ["CompressedTensorsLinearMethod"] SPARSITY_CONFIG_NAME: Literal["sparsity_config"] = "sparsity_config" QUANTIZATION_SCHEME_MAP_TYPE = dict[str, dict[str, QuantizationArgs] | None] class CompressedTensorsConfig(QuantizationConfig): def __init__( self, target_scheme_map: dict[str, Any], ignore: list[str], quant_format: str, sparsity_scheme_map: dict[str, SparsityCompressionConfig], sparsity_ignore_list: list[str], kv_cache_scheme: dict[str, Any] | None = None, config: dict[str, Any] | None = None, transform_config: dict[str, Any] | None = None, ): super().__init__() self.ignore = ignore self.quant_format = quant_format # Map from [target -> scheme] self.target_scheme_map = target_scheme_map self.kv_cache_scheme = kv_cache_scheme self.sparsity_scheme_map = sparsity_scheme_map self.sparsity_ignore_list = sparsity_ignore_list self.config = config if transform_config: self.transform_config = TransformConfig.model_validate(transform_config) else: self.transform_config = None def get_linear_method(self) -> "CompressedTensorsLinearMethod": return CompressedTensorsLinearMethod(self) def get_supported_act_dtypes(cls) -> list[torch.dtype]: return [torch.float32, torch.float16, torch.bfloat16] @classmethod def get_min_capability(cls) -> int: return 70 def get_name(self) -> QuantizationMethods: return "compressed-tensors" def apply_vllm_mapper(self, hf_to_vllm_mapper: "WeightsMapper"): """ Transform layer paths in config targets to match vLLM's naming. The WeightsMapper is designed for weight paths, but some backends (e.g. transformers) use broad prefix mappings like "" -> "model." which would incorrectly transform non-path targets. compressed-tensors targets can be: - Layer paths: "layers.0.self_attn.q_proj" -> transformed - Module class names: "Linear" -> preserved (no ".") - Regex patterns: "re:.*proj" -> preserved (starts with "re:") """ def _map_target(target: str) -> str | None: is_layer_path = "." in target and not target.startswith("re:") if is_layer_path: return hf_to_vllm_mapper._map_name(target) return target def _apply_dict(d: dict) -> dict: return {k: v for t, v in d.items() if (k := _map_target(t)) is not None} def _apply_list(lst: list) -> list: return [t for x in lst if (t := _map_target(x)) is not None] self.target_scheme_map = _apply_dict(self.target_scheme_map) self.ignore = _apply_list(self.ignore) self.sparsity_scheme_map = _apply_dict(self.sparsity_scheme_map) self.sparsity_ignore_list = _apply_list(self.sparsity_ignore_list) if self.kv_cache_scheme is not None: self.kv_cache_scheme = _apply_dict(self.kv_cache_scheme) def get_quant_method( self, layer: torch.nn.Module, prefix: str, ) -> Optional["QuantizeMethodBase"]: if isinstance(layer, LinearBase): # collect schemes quant_scheme = self.get_scheme(layer=layer, layer_name=prefix) input_tfms, output_tfms = get_linear_transform_schemes( layer, prefix, self.transform_config, self.packed_modules_mapping ) # choose quantization method quant_method: LinearMethodBase = UnquantizedLinearMethod() if quant_scheme is not None: layer.scheme = quant_scheme quant_method = CompressedTensorsLinearMethod(self) # choose transform method if any((input_tfms, output_tfms)): return CompressedTensorsLinearTransformMethod.from_schemes( quant_method, quant_scheme, input_tfms, output_tfms ) else: return quant_method if isinstance(layer, Attention): return CompressedTensorsKVCacheMethod(self) if isinstance(layer, FusedMoE): return CompressedTensorsMoEMethod.get_moe_method( self, layer, layer_name=prefix ) return None def _add_fused_moe_to_target_scheme_map(self): """ Helper function to update target_scheme_map since linear layers get fused into FusedMoE targetting 'Linear' needs to also match FusedMoE modules. """ if ( "Linear" not in self.target_scheme_map or "FusedMoE" in self.target_scheme_map ): return self.target_scheme_map["FusedMoE"] = self.target_scheme_map["Linear"] @classmethod def from_config(cls, config: dict[str, Any]) -> "CompressedTensorsConfig": ignore: list[str] = cast(list[str], config.get("ignore", [])) quant_format = cast(str, config.get("format")) target_scheme_map = cls._quantization_scheme_map_from_config(config=config) sparsity_scheme_map, sparsity_ignore_list = cls._parse_sparsity_config( config=config ) transform_config = config.get("transform_config") return cls( target_scheme_map=target_scheme_map, ignore=ignore, quant_format=quant_format, sparsity_scheme_map=sparsity_scheme_map, sparsity_ignore_list=sparsity_ignore_list, config=config, transform_config=transform_config, ) @classmethod def _parse_sparsity_config( cls, config: dict[str, Any] ) -> tuple[dict[str, SparsityCompressionConfig], list[str]]: """ :param config: The `quantization_config` dictionary from config.json :return: A tuple with two elements 1. A dictionary mapping target layer names to their corresponding sparsity_config 2. A list of layer names to ignore for sparsity """ if not (sparsity_config := config.get(SPARSITY_CONFIG_NAME)): return dict(), [] sparsity_config = SparsityCompressionConfig.model_validate(sparsity_config) sparse_scheme_map: dict[str, SparsityCompressionConfig] = { target: sparsity_config for target in sparsity_config.targets or list() } sparsity_ignore_list = sparsity_config.ignore or list() return sparse_scheme_map, sparsity_ignore_list @classmethod def _quantization_scheme_map_from_config( cls, config: dict[str, Any] ) -> QUANTIZATION_SCHEME_MAP_TYPE: """ :param config: The `quantization_config` dictionary from config.json :return: A dictionary mapping target layer names to their corresponding quantization_args for weights and input activations """ target_scheme_map: dict[str, Any] = dict() quant_format = cast(str, config.get("format")) # The quant_config has multiple config_groups, each containing # an input_activations key with details about how the activations are # quantized, a weights key indicating how the weights are quantized, # and a list of targets under the `targets` key, dictating which # layers are impacted by the quantization details. The quantization # details follow the structure defined by the QuantizationArgs # pydantic model, which is used to verify the structure of the # quant_config and also store the details for later use. config_groups = config.get("config_groups", dict()) for _, quant_config in config_groups.items(): targets = quant_config.get("targets") for target in targets: target_scheme_map[target] = {} target_scheme_map[target]["weights"] = QuantizationArgs.model_validate( quant_config.get("weights") ) target_scheme_map[target]["input_activations"] = None target_scheme_map[target]["format"] = quant_config.get("format") format = target_scheme_map[target].get("format") # If no per-config format defined, use global format in config act_quant_format = ( is_activation_quantization_format(format) if format is not None else is_activation_quantization_format(quant_format) ) # w4a8fp8 is in packed-quantized format # but needs input activation quantization input_activations = quant_config.get("input_activations") if act_quant_format or input_activations: # The only case where we have activation quant supported # but no input_activations provided in the config # should be w8a16fp8 w8a16fp8 can also run for cases where # there is an input_quant but it is ignored if not input_activations: assert ( target_scheme_map[target]["weights"].type == QuantizationType.FLOAT ) else: target_scheme_map[target]["input_activations"] = ( QuantizationArgs.model_validate( quant_config.get("input_activations") ) ) return target_scheme_map @classmethod def get_config_filenames(cls) -> list[str]: return [] @staticmethod def _check_scheme_supported( min_capability: int, error: bool = True, match_exact: bool = False ) -> bool: capability_tuple = current_platform.get_device_capability() if capability_tuple is not None: capability = capability_tuple.to_int() if match_exact: supported = capability == min_capability if error and not supported: raise RuntimeError( "Quantization scheme is not supported for ", "the current GPU. Required capability: ", f"{min_capability}. Current capability: {capability}.", ) else: supported = capability >= min_capability if error and not supported: raise RuntimeError( "Quantization scheme is not supported for ", f"the current GPU. Min capability: {min_capability}. ", f"Current capability: {capability}.", ) return supported else: return False @staticmethod def _is_fp4a4_nvfp4(weight_quant: QuantizationArgs, input_quant: QuantizationArgs): if weight_quant is None or input_quant is None: return False is_tensor_group_quant = ( weight_quant.strategy == QuantizationStrategy.TENSOR_GROUP.value and input_quant.strategy == QuantizationStrategy.TENSOR_GROUP.value ) is_symmetric = weight_quant.symmetric and input_quant.symmetric is_group_size_16 = ( weight_quant.group_size == 16 and input_quant.group_size == 16 ) is_float_type = ( weight_quant.type == QuantizationType.FLOAT and input_quant.type == QuantizationType.FLOAT ) is_4_bits = weight_quant.num_bits == 4 and input_quant.num_bits == 4 return ( is_tensor_group_quant and is_float_type and is_4_bits and is_group_size_16 and is_symmetric ) @staticmethod def _is_fp4a16_nvfp4(weight_quant: QuantizationArgs, input_quant: QuantizationArgs): is_weight_only = weight_quant is not None and input_quant is None is_tensor_group_quant = ( weight_quant.strategy == QuantizationStrategy.TENSOR_GROUP.value ) is_symmetric = weight_quant.symmetric is_group_size_16 = weight_quant.group_size == 16 is_float_type = weight_quant.type == QuantizationType.FLOAT is_4_bits = weight_quant.num_bits == 4 return ( is_weight_only and is_tensor_group_quant and is_float_type and is_4_bits and is_group_size_16 and is_symmetric ) @staticmethod def _is_static_tensor_w8a8( weight_quant: QuantizationArgs, input_quant: QuantizationArgs ) -> bool: is_8_bits = weight_quant.num_bits == input_quant.num_bits == 8 weight_strategy = ( weight_quant.strategy == QuantizationStrategy.TENSOR.value or weight_quant.strategy == QuantizationStrategy.CHANNEL.value ) is_tensor = ( weight_strategy and input_quant.strategy == QuantizationStrategy.TENSOR.value ) is_static = not weight_quant.dynamic and not input_quant.dynamic # Both symmetric and asymmetric input quantization supported. # Only symmetric weight quantization supported. return is_8_bits and is_tensor and weight_quant.symmetric and is_static @staticmethod def _is_dynamic_token_w8a8( weight_quant: QuantizationArgs, input_quant: QuantizationArgs ) -> bool: is_8_bits = weight_quant.num_bits == input_quant.num_bits == 8 weight_strategy = ( weight_quant.strategy == QuantizationStrategy.TENSOR.value or weight_quant.strategy == QuantizationStrategy.CHANNEL.value ) is_token = ( weight_strategy and input_quant.strategy == QuantizationStrategy.TOKEN.value ) is_dynamic = not weight_quant.dynamic and input_quant.dynamic # Both symmetric and asymmetric input quantization supported. # Only symmetric weight quantization supported. return is_8_bits and is_token and weight_quant.symmetric and is_dynamic @staticmethod def _is_dynamic_token_w4a8_int( weight_quant: QuantizationArgs, input_quant: QuantizationArgs ) -> bool: is_weight_4_bits = weight_quant.num_bits == 4 is_activation_8_bits = input_quant.num_bits == 8 weight_strategy = ( weight_quant.strategy == QuantizationStrategy.GROUP.value or weight_quant.strategy == QuantizationStrategy.CHANNEL.value ) is_token = ( weight_strategy and input_quant.strategy == QuantizationStrategy.TOKEN.value ) is_dynamic = not weight_quant.dynamic and input_quant.dynamic # Both symmetric and asymmetric input quantization supported. # Only symmetric weight quantization supported. return ( is_weight_4_bits and is_activation_8_bits and is_token and weight_quant.symmetric and is_dynamic ) @staticmethod def _is_fp8_w8a8( weight_quant: QuantizationArgs, input_quant: QuantizationArgs ) -> bool: # Confirm weights and activations quantized. if weight_quant is None or input_quant is None: return False # Confirm weight scheme is supported. is_floating_point = ( weight_quant.type == QuantizationType.FLOAT and input_quant.type == QuantizationType.FLOAT ) is_symmetric_weight = weight_quant.symmetric is_static_weight = not weight_quant.dynamic is_tensor_or_channel_or_block_weight = weight_quant.strategy in [ QuantizationStrategy.TENSOR, QuantizationStrategy.CHANNEL, QuantizationStrategy.BLOCK, ] if not ( is_floating_point and is_symmetric_weight and is_static_weight and is_tensor_or_channel_or_block_weight ): return False # Dynamic quantization is always supported if weights supported. if input_quant.dynamic: return True # Confirm activation scheme is supported. is_symmetric_activation = input_quant.symmetric is_per_tensor_activation = input_quant.strategy == QuantizationStrategy.TENSOR return is_symmetric_activation and is_per_tensor_activation @staticmethod def _is_fp8_w4a8( weight_quant: QuantizationArgs, input_quant: QuantizationArgs ) -> bool: if not weight_quant or not input_quant: return False is_weight_4_bits = weight_quant.num_bits == 4 is_activation_8_bits = input_quant.num_bits == 8 weight_strategy = weight_quant.strategy == QuantizationStrategy.GROUP.value is_token = ( weight_strategy and input_quant.strategy == QuantizationStrategy.TOKEN.value ) is_dynamic = not weight_quant.dynamic and input_quant.dynamic is_symmetric = weight_quant.symmetric and input_quant.symmetric # Only per-group symmetric weight (4bit) # + per-tok symmetric activation (8bit) quantization supported. return ( is_weight_4_bits and is_activation_8_bits and is_token and is_symmetric and is_dynamic ) @classmethod def _is_fp8_w4a8_sm90( cls, weight_quant: QuantizationArgs, input_quant: QuantizationArgs ) -> bool: return cls._check_scheme_supported( 90, error=False, match_exact=True ) and cls._is_fp8_w4a8(weight_quant, input_quant) @classmethod def _is_fp8_w8a8_sm90( cls, weight_quant: QuantizationArgs, input_quant: QuantizationArgs ) -> bool: return cls._check_scheme_supported( 90, error=False, match_exact=True ) and cls._is_fp8_w8a8(weight_quant, input_quant) @classmethod def _is_fp8_w8a8_sm100( cls, weight_quant: QuantizationArgs, input_quant: QuantizationArgs ) -> bool: return cls._check_scheme_supported( 100, error=False, match_exact=True ) and cls._is_fp8_w8a8(weight_quant, input_quant) @staticmethod def _is_fp8_w8a16( weight_quant: QuantizationArgs, input_quant: QuantizationArgs ) -> bool: # Confirm weights quantized. if weight_quant is None: return False # Confirm we have floating points. if weight_quant.type != QuantizationType.FLOAT: return False # Confirm weight scheme is supported. is_symmetric_weight = weight_quant.symmetric is_static_weight = not weight_quant.dynamic is_tensor_or_channel_or_block_weight = weight_quant.strategy in [ QuantizationStrategy.TENSOR, QuantizationStrategy.CHANNEL, QuantizationStrategy.BLOCK, ] return ( is_symmetric_weight and is_static_weight and is_tensor_or_channel_or_block_weight ) @staticmethod def _is_wNa16_group_channel( weight_quant: QuantizationArgs, input_quant: QuantizationArgs ) -> bool: input_quant_none = input_quant is None is_channel_group = ( weight_quant.strategy == QuantizationStrategy.CHANNEL.value or weight_quant.strategy == QuantizationStrategy.GROUP.value ) is_static = not weight_quant.dynamic return is_channel_group and input_quant_none and is_static def _get_scheme_from_parts( self, weight_quant: QuantizationArgs, input_quant: QuantizationArgs, format: str | None = None, layer_name: str | None = None, ) -> "CompressedTensorsScheme": # use the per-layer format if defined, otherwise, use global format format = format if format is not None else self.quant_format # Detect If Mixed Precision if self._is_fp4a16_nvfp4(weight_quant, input_quant): return CompressedTensorsW4A16Fp4() if self._is_fp8_w4a8_sm90(weight_quant, input_quant): return CompressedTensorsW4A8Fp8( num_bits=weight_quant.num_bits, strategy=weight_quant.strategy, symmetric=weight_quant.symmetric, group_size=weight_quant.group_size, actorder=weight_quant.actorder, ) if self._is_wNa16_group_channel(weight_quant, input_quant): if ( format == CompressionFormat.marlin_24.value and weight_quant.num_bits in W4A16SPARSE24_SUPPORTED_BITS ): assert weight_quant.symmetric return CompressedTensorsW4A16Sparse24( strategy=weight_quant.strategy, num_bits=weight_quant.num_bits, group_size=weight_quant.group_size, ) if ( format == CompressionFormat.pack_quantized.value and weight_quant.num_bits in WNA16_SUPPORTED_BITS ): return CompressedTensorsWNA16( num_bits=weight_quant.num_bits, strategy=weight_quant.strategy, symmetric=weight_quant.symmetric, group_size=weight_quant.group_size, actorder=weight_quant.actorder, layer_name=layer_name, ) act_quant_format = is_activation_quantization_format(format) if act_quant_format: if self._is_fp4a4_nvfp4(weight_quant, input_quant): if cutlass_fp4_supported() or envs.VLLM_USE_NVFP4_CT_EMULATIONS: return CompressedTensorsW4A4Fp4() else: logger.warning_once( "Current platform does not support cutlass NVFP4." " Running CompressedTensorsW4A16Fp4." ) return CompressedTensorsW4A16Fp4(has_input_global_scale=True) if self._is_fp8_w8a8(weight_quant, input_quant): is_fp8_w8a8_supported = self._check_scheme_supported( CompressedTensorsW8A8Fp8.get_min_capability(), error=False ) if is_fp8_w8a8_supported: return CompressedTensorsW8A8Fp8( weight_quant=weight_quant, is_static_input_scheme=( input_quant and not input_quant.dynamic ), ) else: # note: input_quant will be present for converted models; # will be ignored during inference post loading return CompressedTensorsW8A16Fp8( strategy=weight_quant.strategy, is_static_input_scheme=not input_quant.dynamic, ) # note: input_quant can be None if self._is_fp8_w8a16(weight_quant, input_quant): is_static_input_scheme = input_quant and not input_quant.dynamic return CompressedTensorsW8A16Fp8( strategy=weight_quant.strategy, is_static_input_scheme=is_static_input_scheme, ) if self._is_static_tensor_w8a8(weight_quant, input_quant): return CompressedTensorsW8A8Int8( strategy=weight_quant.strategy, is_static_input_scheme=True, input_symmetric=input_quant.symmetric, ) if self._is_dynamic_token_w8a8(weight_quant, input_quant): return CompressedTensorsW8A8Int8( strategy=weight_quant.strategy, is_static_input_scheme=False, input_symmetric=input_quant.symmetric, ) if self._is_dynamic_token_w4a8_int(weight_quant, input_quant): is_static_input_scheme = input_quant and not input_quant.dynamic return CompressedTensorsW4A8Int( num_bits=weight_quant.num_bits, strategy=weight_quant.strategy, group_size=weight_quant.group_size, is_static_input_scheme=is_static_input_scheme, input_symmetric=input_quant.symmetric, ) raise NotImplementedError("No compressed-tensors compatible scheme was found.") def get_scheme( self, layer: torch.nn.Module, layer_name: str | None = None ) -> Optional["CompressedTensorsScheme"]: """ compressed-tensors supports non uniform in the following way: targets of config_groups: There can be N config_groups which each have a quantization scheme. Each config_group has a list of targets which can be a full layer_name, a regex for a layer_name, or an nn.Module name. Detect whether a layer_name is found in any target and use the quantization scheme corresponding to the matched target to select the CompressedTensorsScheme used for inference. """ # Use the new get_quant_args method to extract QuantizationArgs scheme_dict = self.get_scheme_dict(layer, layer_name) weight_quant = None input_quant = None format = None if scheme_dict: weight_quant = scheme_dict.get("weights") input_quant = scheme_dict.get("input_activations") format = scheme_dict.get("format") # Find the sparsity scheme of the layer # assume that fused layers inherit first component's sparsity scheme sparsity_targets = self.sparsity_scheme_map.keys() - set( self.sparsity_ignore_list ) sparsity_scheme: SparsityCompressionConfig | None = None with suppress(ValueError): matched_target = find_matched_target( layer_name=layer_name, module=layer, targets=sparsity_targets, fused_mapping=self.packed_modules_mapping, ) sparsity_scheme = self.sparsity_scheme_map[matched_target] if self.supports_cutlass_24( weight_quant=weight_quant, input_quant=input_quant, sparsity_scheme=sparsity_scheme, ): # Have a valid sparsity scheme # Validate layer is supported by Cutlass 2:4 Kernel model_compression_config = ( None if sparsity_scheme is None or sparsity_scheme.format == "dense" else self.config ) scheme = CompressedTensors24( quantized=weight_quant is not None or input_quant is not None, weight_quant=weight_quant, input_quant=input_quant, model_compression_config=model_compression_config, ) elif weight_quant is None: logger.warning_once( "Acceleration for non-quantized schemes is " "not supported by Compressed Tensors. " "Falling back to UnquantizedLinearMethod" ) return None else: # Find the quant_scheme scheme = self._get_scheme_from_parts( # type: ignore weight_quant=weight_quant, input_quant=input_quant, format=format, layer_name=layer_name, ) # Raise error if device does not support the scheme # (e.g. fp8 needs ada lovelace) self._check_scheme_supported(scheme.get_min_capability()) logger.debug("Using scheme: %s for %s", scheme.__class__.__name__, layer_name) return scheme def get_scheme_dict( self, layer: torch.nn.Module, layer_name: str | None = None ) -> dict[str, QuantizationArgs | str | None] | None: """ Extract the QuantizationArgs for a given layer. Returns: dict with { "weights": QuantizationArgs, "input_activations": QuantizationArgs | None, "format": str | None } | None """ # TODO (@kylesayrs): support ignore module names with ct matching utils if should_ignore_layer( layer_name, ignore=self.ignore, fused_mapping=self.packed_modules_mapping ): return None # Will be empty for models with only sparsity if self.target_scheme_map: matched_target = find_matched_target( layer_name=layer_name, module=layer, targets=self.target_scheme_map.keys(), fused_mapping=self.packed_modules_mapping, ) scheme_dict = self.target_scheme_map[matched_target] if scheme_dict.get("format") is None: scheme_dict["format"] = self.quant_format return scheme_dict return None def get_cache_scale(self, name: str) -> str | None: """ Check whether the param name matches the format for k/v cache scales in compressed-tensors. If this is the case, return its equivalent param name expected by vLLM :param name: param name :return: matching param name for KV cache scale in vLLM """ if name.endswith(".output_scale") and ".k_proj" in name: return name.replace(".k_proj.output_scale", ".attn.k_scale") if name.endswith(".output_scale") and ".v_proj" in name: return name.replace(".v_proj.output_scale", ".attn.v_scale") # If no matches, return None return None def has_blocked_weights(self) -> bool: for scheme in self.target_scheme_map.values(): weight_quant = scheme.get("weights") if ( weight_quant is not None and weight_quant.strategy == QuantizationStrategy.BLOCK ): return True return False @staticmethod def supports_cutlass_24( weight_quant: QuantizationArgs | None, input_quant: QuantizationArgs | None,
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
true
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/triton_scaled_mm.py
vllm/model_executor/layers/quantization/compressed_tensors/triton_scaled_mm.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm.triton_utils import tl, triton def is_weak_contiguous(x: torch.Tensor): strides = x.stride() sizes = x.shape is_not_transpose = strides[0] == 1 and (strides[1] >= max(1, sizes[0])) is_transpose = strides[1] == 1 and (strides[0] >= max(1, sizes[1])) return is_transpose or is_not_transpose @triton.jit def scaled_mm_kernel( a_ptr, b_ptr, scale_a_ptr, scale_b_ptr, c_ptr, bias_ptr, M, N, K, stride_am, stride_ak, stride_bk, stride_bn, stride_cm, stride_cn, ACCUMULATOR_DTYPE: tl.constexpr, BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr, BLOCK_SIZE_SCALE_A: tl.constexpr, BLOCK_SIZE_SCALE_B: tl.constexpr, ): pid = tl.program_id(axis=0) num_pid_n = tl.cdiv(N, BLOCK_SIZE_N) pid_m = pid // num_pid_n pid_n = pid % num_pid_n accumulator_dtype = ACCUMULATOR_DTYPE accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=accumulator_dtype) # NOTE: Some tensor inputs are so large, they will cause int32 overflow # so it is necessary to use tl.int64 for all the offsets, else SEGV will # eventually occur. # Offsets and masks. offsets_am = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M).to(tl.int64) masks_am = offsets_am < M offsets_bn = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N).to(tl.int64) masks_bn = offsets_bn < N offsets_k = tl.arange(0, BLOCK_SIZE_K).to(tl.int64) offsets_a = stride_am * offsets_am[:, None] + stride_ak * offsets_k[None, :] offsets_b = stride_bk * offsets_k[:, None] + stride_bn * offsets_bn[None, :] # NOTE: BLOCK_SIZE_SCALE_A could be 1 or BLOCK_SIZE_M, so need to create # appropriate offsets and masks for each case. Same goes for # BLOCK_SIZE_SCALE_B. offsets_scale_am = ( tl.arange(0, BLOCK_SIZE_SCALE_A) + (BLOCK_SIZE_SCALE_A > 1) * pid_m * BLOCK_SIZE_M ) masks_scale_am = offsets_scale_am < M offsets_scale_bn = ( tl.arange(0, BLOCK_SIZE_SCALE_B) + (BLOCK_SIZE_SCALE_B > 1) * pid_n * BLOCK_SIZE_N ) masks_scale_bn = offsets_scale_bn < N a_ptrs = a_ptr + offsets_a b_ptrs = b_ptr + offsets_b scale_a_ptrs = scale_a_ptr + offsets_scale_am scale_b_ptrs = scale_b_ptr + offsets_scale_bn for k in range(0, tl.cdiv(K, BLOCK_SIZE_K)): masks_k = offsets_k < K masks_a = masks_am[:, None] & masks_k[None, :] a = tl.load(a_ptrs, mask=masks_a) masks_b = masks_k[:, None] & masks_bn[None, :] b = tl.load(b_ptrs, mask=masks_b) # Accumulate results. accumulator = tl.dot(a, b, accumulator, out_dtype=accumulator_dtype) offsets_k += BLOCK_SIZE_K a_ptrs += BLOCK_SIZE_K * stride_ak b_ptrs += BLOCK_SIZE_K * stride_bk # Apply scale at end. masks_scale_a = masks_scale_am[:, None] & (tl.arange(0, 1) < 1)[:, None] scale_a = tl.load(scale_a_ptrs[:, None], masks_scale_a) # Need to broadcast to the appropriate size, if scale_a is already # (BLOCK_SIZE_M, 1) then it will broadcast to its own shape. Same goes # for scale_b below. scale_a = scale_a.broadcast_to((BLOCK_SIZE_M, 1)) accumulator = scale_a * accumulator.to(tl.float32) masks_scale_b = masks_scale_bn[:, None] & (tl.arange(0, 1) < 1)[None, :] scale_b = tl.load(scale_b_ptrs[:, None], masks_scale_b) scale_b = scale_b.broadcast_to((BLOCK_SIZE_N, 1)) accumulator = scale_b.T * accumulator.to(tl.float32) # Convert to output format. c = accumulator.to(c_ptr.type.element_ty) # Add bias, it's already in output format, so add it after conversion. if bias_ptr: offsets_bias = offsets_bn bias_ptrs = bias_ptr + offsets_bias bias_mask = offsets_bias < N bias = tl.load(bias_ptrs, bias_mask) c += bias # Save output offs_cm = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M).to(tl.int64) offs_cn = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N).to(tl.int64) offs_cm = offs_cm.to(tl.int64) offs_cn = offs_cn.to(tl.int64) c_ptrs = c_ptr + stride_cm * offs_cm[:, None] + stride_cn * offs_cn[None, :] c_mask = (offs_cm[:, None] < M) & (offs_cn[None, :] < N) tl.store(c_ptrs, c, mask=c_mask) # input - [M, K] # weight - [K, N] def triton_scaled_mm( input: torch.Tensor, weight: torch.Tensor, scale_a: torch.Tensor, scale_b: torch.Tensor, out_dtype: type[torch.dtype], bias: torch.Tensor | None = None, block_size_m: int = 32, block_size_n: int = 32, block_size_k: int = 32, use_heuristic=True, ) -> torch.Tensor: M, K = input.shape N = weight.shape[1] assert N > 0 and K > 0 and M > 0 assert weight.shape[0] == K assert input.dtype == weight.dtype scale_a = scale_a.reshape(-1, 1) if scale_a.dim() <= 1 else scale_a scale_b = scale_b.reshape(-1, 1) if scale_b.dim() <= 1 else scale_b assert scale_a.dtype == scale_b.dtype and scale_a.is_floating_point() assert scale_a.shape[1] == 1 and (scale_a.shape[0] == 1 or scale_a.shape[0] == M) assert scale_b.shape[1] == 1 and (scale_b.shape[0] == 1 or scale_b.shape[0] == N) assert out_dtype.is_floating_point assert bias is None or bias.is_floating_point() assert is_weak_contiguous(input) assert is_weak_contiguous(weight) grid = lambda META: ( triton.cdiv(M, META["BLOCK_SIZE_M"]) * triton.cdiv(N, META["BLOCK_SIZE_N"]), ) result = torch.empty((M, N), dtype=out_dtype, device=input.device) has_scalar = lambda x: x.shape[0] == 1 and x.shape[1] == 1 if use_heuristic: is_small_N = N < 8192 next_power_of_2_M = max(32, triton.next_power_of_2(M)) if next_power_of_2_M <= 32: tile_shape = (64, 64, 256) if is_small_N else (64, 128, 256) elif next_power_of_2_M <= 64: tile_shape = (64, 64, 256) elif next_power_of_2_M <= 128: tile_shape = (64, 128, 128) else: tile_shape = (128, 128, 128) block_size_m, block_size_n, block_size_k = tile_shape block_size_sa = 1 if has_scalar(scale_a) else block_size_m block_size_sb = 1 if has_scalar(scale_b) else block_size_n accumulator_dtype = tl.float32 if input.is_floating_point() else tl.int32 # A = input, B = weight, C = result # A = M x K, B = K x N, C = M x N scaled_mm_kernel[grid]( input, weight, scale_a, scale_b, result, bias, M, N, K, input.stride(0), input.stride(1), weight.stride(0), weight.stride(1), result.stride(0), result.stride(1), accumulator_dtype, BLOCK_SIZE_M=block_size_m, BLOCK_SIZE_N=block_size_n, BLOCK_SIZE_K=block_size_k, BLOCK_SIZE_SCALE_A=block_size_sa, BLOCK_SIZE_SCALE_B=block_size_sb, ) return result.to(out_dtype)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/__init__.py
vllm/model_executor/layers/quantization/compressed_tensors/__init__.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/transform/module.py
vllm/model_executor/layers/quantization/compressed_tensors/transform/module.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import math from collections.abc import Callable, Hashable import torch from compressed_tensors.transform import ( TransformArgs, TransformLocation, TransformScheme, ) from torch import Tensor import vllm._custom_ops as ops from vllm.distributed.parallel_state import get_tensor_model_parallel_world_size from vllm.model_executor.layers.linear import LinearBase from vllm.model_executor.layers.quantization.compressed_tensors.transform.utils import ( # noqa: E501 TransformTuple, ) from vllm.model_executor.layers.utils import dispatch_unquantized_gemm from vllm.model_executor.layers.vocab_parallel_embedding import VocabParallelEmbedding from vllm.model_executor.parameter import SharedWeightParameter class HadamardTransform(torch.nn.Module): """ Class which handles weight loading, postprocessing, and application of transforms. Meant to be used with `CompressedTensorsLinearTransformMethod` and attention transforms method (not implemented yet) """ transforms: dict[int, TransformTuple] # info parsed from transforms config weight: SharedWeightParameter # container for shared tensors scales: dict[int, float] # hadamard scale, usually sqrt(matrix.size(0)) def __init__( self, transforms: dict[int, TransformTuple], layer: torch.nn.Module, weight_loader: Callable, input_size_per_partition: int, output_partition_sizes: list[int], ): super().__init__() self.transforms = transforms self.scales = {} if get_tensor_model_parallel_world_size() > 1: raise NotImplementedError( "Online transforms with tensor parallelism is not supported" ) # Similar to row/col parallel params, but tensors are separate # to allow for loading with shared memory self.weight = SharedWeightParameter(weight_loader=weight_loader) # create shared partition data for each partition of the original weight input_size = input_size_per_partition for part_index, (_scheme_name, scheme, args) in self.transforms.items(): output_size = output_partition_sizes[part_index] weight_size = self._get_weight_size( layer, scheme, args, input_size, output_size ) data_key = self._get_data_key(scheme, weight_size) self.weight.add_partition( part_index, data_key, size=(weight_size, weight_size), dtype=scheme.precision, ) # validate that shared tensors and schemes are correct self._validate_input_transforms() def process_weights_after_loading(self): for part_id in self.weight.partitions: data = self.weight.partitions[part_id].data # required by torch.compile self.weight.process_weights_after_loading() # precompute scale as a runtime multiply, not division # do not fold into weight in order to utilize FWHT self.scales[part_id] = 1 / math.sqrt(data.size(0)) # FUTURE: avoid runtime transpose by processing weights # prior to apply def forward(self, value: Tensor, part_id: int = 0) -> Tensor: if part_id not in self.weight.partitions: return value # use hadacore if possible if self.transforms[part_id].scheme.type == "hadamard": if self.transforms[part_id].scheme.head_dim is not None: weight_size = self.transforms[part_id].scheme.head_dim value = value.unflatten(-1, (-1, weight_size)) value = ops.hadacore_transform(value) value = value.flatten(-2, -1) return value # sylvester transforms are symmetric, inv => transpose => original return ops.hadacore_transform(value) # fall back to dense else: weight = self.weight.partitions[part_id] weight = ( weight if self.transforms[part_id].args.inverse else weight.T ) # linear := x(W.T) scale = self.scales[part_id] if self.transforms[part_id].scheme.head_dim is not None: value = value.unflatten(-1, (-1, weight.size(0))) value = ( dispatch_unquantized_gemm()( self, value.to(weight.dtype), weight, None ).to(value.dtype) * scale ) value = value.flatten(-2, -1) return value return ( dispatch_unquantized_gemm()( self, value.to(weight.dtype), weight, None ).to(value.dtype) * scale ) def _get_data_key(self, scheme: TransformScheme, weight_size: int) -> Hashable: return (id(scheme), weight_size) def _get_weight_size( self, layer: torch.nn.Module, scheme: TransformScheme, args: TransformArgs, input_size: int, output_size: int, ) -> int: if scheme.head_dim is not None: return scheme.head_dim if isinstance(layer, LinearBase): if args.location == TransformLocation.INPUT: return input_size elif args.location == TransformLocation.OUTPUT: return output_size elif isinstance(layer, VocabParallelEmbedding): if args.location == TransformLocation.INPUT: return output_size elif args.location == TransformLocation.OUTPUT: return input_size raise ValueError() def _validate_input_transforms(self): assert len(self.transforms) > 0 location = list(self.transforms.values())[0].args.location if location == TransformLocation.INPUT: first_data = self.weight.partitions[0].data for partition in self.weight.partitions.values(): if partition.data.data_ptr() != first_data.data_ptr(): raise ValueError("")
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/transform/utils.py
vllm/model_executor/layers/quantization/compressed_tensors/transform/utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from typing import NamedTuple from compressed_tensors.transform import TransformArgs, TransformScheme __all__ = ["TransformTuple"] class TransformTuple(NamedTuple): scheme_name: str scheme: TransformScheme args: TransformArgs
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/transform/__init__.py
vllm/model_executor/layers/quantization/compressed_tensors/transform/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/transform/linear.py
vllm/model_executor/layers/quantization/compressed_tensors/transform/linear.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable, Generator from itertools import accumulate import torch from compressed_tensors.transform import ( TransformArgs, TransformConfig, TransformLocation, TransformScheme, ) from compressed_tensors.utils import is_match from vllm.model_executor.layers.linear import ( WEIGHT_LOADER_V2_SUPPORTED, LinearMethodBase, ) from vllm.model_executor.layers.quantization.compressed_tensors.compressed_tensors import ( # noqa: E501 CompressedTensorsScheme, ) from vllm.model_executor.layers.quantization.compressed_tensors.transform.module import ( # noqa: E501 HadamardTransform, ) from vllm.model_executor.layers.quantization.compressed_tensors.transform.utils import ( # noqa: E501 TransformTuple, ) class CompressedTensorsLinearTransformMethod(LinearMethodBase): """ Wraps `CompressedTensorsLinearMethod` or `UnquantizedLinearMethod` and adds input and output transforms to either side of the original apply method """ @classmethod def from_schemes( cls, quant_method: LinearMethodBase, quant_scheme: CompressedTensorsScheme | None, input_tfms: dict[int, TransformTuple], output_tfms: dict[int, TransformTuple], ) -> "CompressedTensorsLinearTransformMethod": from vllm.model_executor.layers.quantization.compressed_tensors.transform.schemes.linear_qutlass_nvfp4 import ( # noqa: E501 QutlassNvFP4LinearMethod, is_qutlass_fp4_scheme, ) assert input_tfms or output_tfms if is_qutlass_fp4_scheme(quant_scheme, input_tfms): return QutlassNvFP4LinearMethod(quant_method, input_tfms, output_tfms) # hadacore or dense gemm is selected by Transform module return cls(quant_method, input_tfms, output_tfms) def __init__( self, quant_method: LinearMethodBase, input_tfms: dict[int, TransformTuple], output_tfms: dict[int, TransformTuple], ): self.quant_method = quant_method self.input_tfms = input_tfms self.output_tfms = output_tfms self.input_transform: HadamardTransform | None = None self.output_transform: HadamardTransform | None = None def create_weights( self, layer: torch.nn.Module, input_size_per_partition: int, output_partition_sizes: list[int], input_size: int, output_size: int, params_dtype: torch.dtype, **extra_weight_attrs, ): # get weight loader for transforms weight_loader: Callable = extra_weight_attrs.get("weight_loader") # type: ignore[assignment] # HACK: UnquantizedLinearMethod does not support weight loader v2, but # transforms (specifically SharedWeightParameter) requires # weight loader v2. Until UnquantizedLinearMethod supports v2, we must # hack around this by getting weight loader v1 so ULM can load correctly quant_method_name = self.quant_method.__class__.__name__ if quant_method_name not in WEIGHT_LOADER_V2_SUPPORTED: weight_loader_v1 = layer.weight_loader extra_weight_attrs["weight_loader"] = weight_loader_v1 self.quant_method.create_weights( layer=layer, input_size_per_partition=input_size_per_partition, output_partition_sizes=output_partition_sizes, input_size=input_size, output_size=output_size, params_dtype=params_dtype, **extra_weight_attrs, ) # validate schemes num_partitions = len(output_partition_sizes) self._validate_tfm_schemes(num_partitions) # create submodules for weight loading if len(self.input_tfms) > 0: scheme_name = list(self.input_tfms.values())[0].scheme_name location = list(self.input_tfms.values())[0].args.location transform_name = f"{scheme_name}_{location}" transform = HadamardTransform( self.input_tfms, layer, weight_loader, input_size_per_partition, output_partition_sizes, ) layer.register_module(transform_name, transform) self.input_transform = transform if len(self.output_tfms) > 0: scheme_name = list(self.output_tfms.values())[0].scheme_name location = list(self.output_tfms.values())[0].args.location transform_name = f"{scheme_name}_{location}" transform = HadamardTransform( self.output_tfms, layer, weight_loader, input_size_per_partition, output_partition_sizes, ) layer.register_module(transform_name, transform) self.output_transform = transform # compute partition ranges for slicing activations starts = [0] + list(accumulate(output_partition_sizes))[:-1] self.partition_ranges = list(zip(starts, output_partition_sizes)) def process_weights_after_loading(self, layer): self.quant_method.process_weights_after_loading(layer) for submodule in layer.children(): if isinstance(submodule, HadamardTransform): submodule.process_weights_after_loading() def apply( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: if self.input_transform is not None: x = self.input_transform(x) assert bias is None x = self.quant_method.apply(layer, x, bias) # In most cases, input transforms are preferred over output transforms # (@ksayers): confirm that this is done concurrently if self.output_transform is not None: for part_id, (start, length) in enumerate(self.partition_ranges): x[:, start : start + length] = self.output_transform( x[:, start : start + length].clone(), part_id=part_id ) return x def _validate_tfm_schemes(self, num_partitions: int): if len(self.input_tfms) > 0: if 0 not in self.input_tfms: raise ValueError("Must have same input") for part_index in range(num_partitions): if self.input_tfms[part_index] != self.input_tfms[0]: raise ValueError("Must have same input") if len(self.output_tfms) > 0: scheme_name = list(self.output_tfms.values())[0].scheme_name location = list(self.output_tfms.values())[0].args.location for tfm in self.output_tfms.values(): if tfm.scheme_name != scheme_name: raise ValueError("Must have same scheme name") if tfm.args.location != location: raise ValueError("Must have same location") return self.input_tfms, self.output_tfms def get_linear_transform_schemes( layer: torch.nn.Module, layer_name: str, transform_config: TransformConfig | None, packed_modules_mapping: dict[str, list[str]], ) -> tuple[ dict[int, TransformTuple], dict[int, TransformTuple] ]: # [input_transform, [output_transform, ...]] # there can only be one transform input scheme per (fused) module input_tfms = {} output_tfms = {} partition_names = get_layer_partition_names(layer_name, packed_modules_mapping) for scheme_name, scheme, args in get_schemes_args(transform_config): for part_index, part_name in enumerate(partition_names): if ( is_match(part_name, layer, args.targets, args.ignore) and args.is_online() ): if args.location == TransformLocation.INPUT: input_tfms[part_index] = TransformTuple(scheme_name, scheme, args) elif args.location == TransformLocation.OUTPUT: output_tfms[part_index] = TransformTuple(scheme_name, scheme, args) else: raise ValueError( f"Cannot apply `{args.location}` transform to `{layer_name}`" ) return (input_tfms, output_tfms) def get_schemes_args( transform_config: TransformConfig | None, ) -> Generator[tuple[str, TransformScheme, TransformArgs]]: if transform_config is None: return for scheme_name, scheme in transform_config.config_groups.items(): for args in scheme.apply: yield (scheme_name, scheme, args) def get_layer_partition_names( layer_name: str, packed_modules_mapping: dict[str, list[str]] ) -> list[str]: """ Get all partition names associated with this layer. Names are returned in order of their partition indices. ```python mapping = {"gate_up_proj", "gate_proj", "up_proj"} assert get_layer_partition_names("mlp.gate_up_proj", mapping) == [ "gate_proj", "up_proj", ] assert get_layer_partition_names("mlp.down_proj", mapping) == ["down_proj"]""" for fused_suffix, part_suffixes in packed_modules_mapping.items(): if layer_name.endswith(fused_suffix): return [ layer_name.removesuffix(fused_suffix) + part_suffix for part_suffix in part_suffixes ] return [layer_name]
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/transform/schemes/__init__.py
vllm/model_executor/layers/quantization/compressed_tensors/transform/schemes/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/transform/schemes/linear_qutlass_nvfp4.py
vllm/model_executor/layers/quantization/compressed_tensors/transform/schemes/linear_qutlass_nvfp4.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm.model_executor.layers.quantization.compressed_tensors.compressed_tensors import ( # noqa: E501 CompressedTensorsScheme, CompressedTensorsW4A4Fp4, ) from vllm.model_executor.layers.quantization.compressed_tensors.transform.linear import ( # noqa: E501 CompressedTensorsLinearTransformMethod, TransformTuple, ) __all__ = ["is_qutlass_fp4_scheme", "QutlassNvFP4LinearMethod"] def is_qutlass_fp4_scheme( quant_scheme: CompressedTensorsScheme | None, input_tfms: dict[int, TransformTuple], ) -> bool: return ( isinstance(quant_scheme, (CompressedTensorsW4A4Fp4,)) and len(input_tfms) == 1 and input_tfms[0].scheme.head_dim == quant_scheme.group_size ) class QutlassNvFP4LinearMethod(CompressedTensorsLinearTransformMethod): def create_weights( self, layer, input_size_per_partition, output_partition_sizes, input_size, output_size, params_dtype, **extra_weight_attrs, ): # initializes fp4 qparams assert isinstance(layer.scheme, (CompressedTensorsW4A4Fp4,)) ret = super().create_weights( layer, input_size_per_partition, output_partition_sizes, input_size, output_size, params_dtype, **extra_weight_attrs, ) assert self.input_transform is not None assert len(self.input_transform.weight) == 1 assert self.input_transform.weight[0].size(0) == layer.scheme.group_size return ret def apply( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: raise NotImplementedError()
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w4a16_nvfp4.py
vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w4a16_nvfp4.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import torch from torch.nn.parameter import Parameter from vllm.model_executor.layers.quantization.compressed_tensors.schemes import ( CompressedTensorsScheme, ) from vllm.model_executor.layers.quantization.utils.marlin_utils_fp4 import ( apply_fp4_marlin_linear, prepare_fp4_layer_for_marlin, ) from vllm.model_executor.parameter import ( GroupQuantScaleParameter, ModelWeightParameter, PerTensorScaleParameter, ) __all__ = ["CompressedTensorsW4A16Fp4"] class CompressedTensorsW4A16Fp4(CompressedTensorsScheme): def __init__(self, has_input_global_scale: bool = False): self.has_input_global_scale = has_input_global_scale self.group_size = 16 @classmethod def get_min_capability(cls) -> int: # don't restrict as emulations return 80 def create_weights( self, layer: torch.nn.Module, output_partition_sizes: list[int], input_size_per_partition: int, params_dtype: torch.dtype, weight_loader: Callable, **kwargs, ): output_size_per_partition = sum(output_partition_sizes) layer.logical_widths = output_partition_sizes layer.input_size_per_partition = input_size_per_partition layer.output_size_per_partition = output_size_per_partition # Weight weight = ModelWeightParameter( data=torch.empty( sum(output_partition_sizes), input_size_per_partition // 2, dtype=torch.uint8, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight_packed", weight) # Global Weight Scale weight_global_scale = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) layer.register_parameter("weight_global_scale", weight_global_scale) # Per Group Weight Scale weight_scale = GroupQuantScaleParameter( data=torch.empty( sum(output_partition_sizes), input_size_per_partition // self.group_size, dtype=torch.float8_e4m3fn, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight_scale", weight_scale) if self.has_input_global_scale: input_global_scale = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) layer.register_parameter("input_global_scale", input_global_scale) def process_weights_after_loading(self, layer) -> None: # Process parameters for marlin repacking # Rename weight_packed to weight that marlin expects layer.weight = Parameter(layer.weight_packed.data, requires_grad=False) del layer.weight_packed # Rename weight_global_scale to weight_scale_2 that marlin expects # Note: ct stores the inverse of what is expected by the marlin kernel layer.weight_scale_2 = Parameter( 1 / layer.weight_global_scale.max().to(torch.float32), requires_grad=False ) del layer.weight_global_scale if self.has_input_global_scale: layer.input_global_scale = torch.nn.Parameter( layer.input_global_scale.data, requires_grad=False ) prepare_fp4_layer_for_marlin(layer) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: return apply_fp4_marlin_linear( input=x, weight=layer.weight, weight_scale=layer.weight_scale, weight_scale_2=layer.weight_scale_2, workspace=layer.workspace, size_n=layer.output_size_per_partition, size_k=layer.input_size_per_partition, bias=bias, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_24.py
vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_24.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable from typing import Any import torch from compressed_tensors import CompressionFormat, ModelCompressor from compressed_tensors.quantization import ( QuantizationArgs, QuantizationStrategy, QuantizationType, ) from compressed_tensors.utils import combine_shards from vllm import _custom_ops as ops from vllm.model_executor.layers.linear import ( MergedColumnParallelLinear, QKVParallelLinear, ) from vllm.model_executor.layers.quantization.compressed_tensors.schemes import ( CompressedTensorsScheme, ) from vllm.model_executor.layers.quantization.input_quant_fp8 import QuantFP8 from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape from vllm.model_executor.layers.quantization.utils.w8a8_utils import ( convert_to_channelwise, sparse_cutlass_supported, ) from vllm.model_executor.parameter import ( BasevLLMParameter, ChannelQuantScaleParameter, ModelWeightParameter, PerTensorScaleParameter, ) __all__ = ["CompressedTensors24"] from vllm.platforms import current_platform class CompressedTensors24(CompressedTensorsScheme): def __init__( self, quantized: bool = False, weight_quant: QuantizationArgs | None = None, input_quant: QuantizationArgs | None = None, model_compression_config: dict[str, Any] | None = None, ): self.quantized = quantized self.weight_quant = weight_quant self.input_quant = input_quant model_compressor = ModelCompressor.from_compression_config( model_compression_config ) self.do_sparse_decompress = ( model_compressor is not None and model_compressor.sparsity_config.format == CompressionFormat.sparse_24_bitmask.value ) if self.do_sparse_decompress: self.model_compressor = model_compressor if ( quantized and input_quant is not None and self._get_quant_dtype() == current_platform.fp8_dtype() ): static = not input_quant.dynamic g_shape = GroupShape.PER_TENSOR if static else GroupShape.PER_TOKEN self.quant_fp8 = QuantFP8(static, g_shape) @classmethod def get_min_capability(cls) -> int: # Only cutlass 3.x kernels are implemented so far return 90 def create_weights( self, layer: torch.nn.Module, input_size: int, output_partition_sizes: list[int], input_size_per_partition: int, params_dtype: torch.dtype, weight_loader: Callable, **kwargs, ): if not sparse_cutlass_supported(): raise ValueError( "Sparse CUTLASS not supported. vLLM must be built with " "CUDA 12.2 or later to use this feature" ) layer.logical_widths = output_partition_sizes layer.input_size = input_size layer.input_size_per_partition = input_size_per_partition self.weights_dtype: torch.dtype = self._get_params_dtype(params_dtype) # parameter to store uncompressed weight weight = ModelWeightParameter( data=torch.empty( sum(output_partition_sizes), input_size_per_partition, dtype=self.weights_dtype, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) if self.do_sparse_decompress: assert all( partition_size % 8 == 0 for partition_size in output_partition_sizes ), "All partitions must be divisible by 8 for " "2:4 sparse compressed models" shape = BasevLLMParameter( data=torch.empty(2, 1, dtype=torch.int64), weight_loader=weight_loader, ) compressed_weight = ModelWeightParameter( data=torch.empty( sum(output_partition_sizes), input_size_per_partition // 2, dtype=self.weights_dtype, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) bitmask = ModelWeightParameter( data=torch.empty( sum(output_partition_sizes), input_size_per_partition // 8, dtype=torch.uint8, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("shape", shape) layer.register_parameter("compressed", compressed_weight) layer.register_parameter("bitmask", bitmask) # Check if quantized, not just 2:4 Sparse if self.quantized: if ( self.weight_quant and self.weight_quant.strategy == QuantizationStrategy.CHANNEL.value ): weight_scale = ChannelQuantScaleParameter( data=torch.empty( (sum(output_partition_sizes), 1), dtype=torch.float32 ), output_dim=0, weight_loader=weight_loader, ) else: assert ( self.weight_quant and self.weight_quant.strategy == QuantizationStrategy.TENSOR.value ) weight_scale = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) layer.register_parameter("weight_scale", weight_scale) # input quant will be non-none if self.input_quant and not self.input_quant.dynamic: # register input quant scale assert self.input_quant.strategy == QuantizationStrategy.TENSOR.value input_scale = BasevLLMParameter( data=torch.empty(1, dtype=torch.float32), weight_loader=weight_loader, ) layer.register_parameter("input_scale", input_scale) else: # for sparse-only, pass in 1 for weight/input scales weight_scale = torch.nn.Parameter( data=torch.ones(1, dtype=torch.float32), requires_grad=False ) input_scale = torch.nn.Parameter( data=torch.ones(1, dtype=torch.float32), requires_grad=False ) layer.register_parameter("input_scale", input_scale) layer.register_parameter("weight_scale", weight_scale) layer.register_parameter("weight", weight) def process_weights_after_loading(self, layer: torch.nn.Module) -> None: """ Compress weights after loading. Store compressed weight and meta tensor :post-condition: layer.w_compressed and layer.meta are set to the compressed weight and meta tensor in the format expected by the Cutlass kernels :param layer: The layer with the weights to be processed """ if self.do_sparse_decompress: layer.weight.data = self._decompress_bitmask_compressed_weight( compressed=layer.compressed, bitmask=layer.bitmask, layer=layer, ) # compressed and bitmask tensors # are no longer needed after decompression del layer.compressed del layer.bitmask # torch.compile workaround if hasattr(layer, "input_scale"): layer.input_scale = torch.nn.Parameter( layer.input_scale.data, requires_grad=False ) if self.weight_quant: if self.weight_quant.strategy == QuantizationStrategy.TENSOR.value: layer.weight_scale = torch.nn.Parameter( convert_to_channelwise( weight_scale=layer.weight_scale, logical_widths=layer.logical_widths, ), requires_grad=False, ) else: # torch.compile workaround layer.weight_scale = torch.nn.Parameter( layer.weight_scale.data, requires_grad=False ) # Set all negative zero values to 0 prior to compression if layer.weight.dtype.is_floating_point and layer.weight.dtype.itemsize >= 2: layer.weight.data[layer.weight.data == -0.0] = 0.0 w_compressed, meta = ops.cutlass_sparse_compress(layer.weight.data) layer.weight = torch.nn.Parameter(w_compressed, requires_grad=False) layer.meta = torch.nn.Parameter(meta, requires_grad=False) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: """ Returns the output tensor for the layer with 2:4 sparse compressed weights, given the input tensor and bias :param layer: The layer with 2:4 sparse compressed weights to be used for the computation :param x: The input tensor to the layer :param bias: The bias to be added to the output tensor :return: The output tensor of the layer """ if self.quantized: scale = getattr(layer, "input_scale", None) if self.weights_dtype == torch.int8: ops_output = ops.scaled_int8_quant(x, scale=scale) q_input = ops_output[0] input_scale = ops_output[1] else: assert self.weights_dtype == torch.float8_e4m3fn q_input, input_scale = self.quant_fp8(x, scale=scale) else: # Not quantized, nothing to do with the input_scales, use as is input_scale = layer.input_scale q_input = x out = ops.cutlass_scaled_sparse_mm( a=q_input, bt_nzs=layer.weight, bt_meta=layer.meta, scale_a=input_scale, scale_b=layer.weight_scale, out_dtype=x.dtype, bias=bias, ) assert out.is_contiguous() return out def _get_params_dtype(self, params_dtype: torch.dtype) -> torch.dtype: if not self.quantized: return params_dtype return self._get_quant_dtype() def _get_quant_dtype(self) -> torch.dtype: assert self.quantized assert self.weight_quant is not None assert self.input_quant is not None is_8_bits = self.weight_quant.num_bits == self.input_quant.num_bits == 8 if not is_8_bits: raise ValueError("Cutlass only supports 8-bit quantization") if ( self.weight_quant.type == QuantizationType.FLOAT and self.input_quant.type == QuantizationType.FLOAT ): return torch.float8_e4m3fn if ( self.weight_quant.type == QuantizationType.INT and self.input_quant.type == QuantizationType.INT ): return torch.int8 raise ValueError("Quantization type not supported by Cutlass") def _decompress_bitmask_compressed_weight( self, compressed: torch.Tensor, bitmask: torch.Tensor, layer: torch.nn.Module, ) -> torch.Tensor: """ Decompress a compressed 2:4 sparse weight tensor using the bitmask and return the result. This function also supports sharded decompression. :param compressed: The 2:4 sparse weight tensor compressed using the sparse-24-bitmask compressor. This is different from `cutlass_sparse_compress` which uses a different scheme (2 bits for every nonzero element that represent the coordinate within the block of 4). The bitmask compression here uses a bitmask to indicate the positions of non-zero elements. :param bitmask: The 2:4 bitmask associated with the compressed weights, representing the positions of non-zero elements in the compressed tensor. :param layer: The layer whose weights need to be processed after loading. :return: The decompressed 2:4 sparse weight tensor. """ sparsity_compressor = self.model_compressor.sparsity_compressor def _process_split( bitmask_compressed_weight: torch.Tensor, shape, bitmask: torch.Tensor, ) -> torch.Tensor: weight_data = dict( compressed=bitmask_compressed_weight, shape=shape, bitmask=bitmask, ) return sparsity_compressor.decompress_weight(weight_data) split_weights: list[torch.Tensor] = [] split_bitmask: list[torch.Tensor] = [] split_shape: list[tuple[int, int]] = [] if isinstance(layer, (QKVParallelLinear, MergedColumnParallelLinear)): split_weights = torch.split(compressed, layer.logical_widths) split_bitmask = torch.split(bitmask, layer.logical_widths) split_shape = [ (out, layer.input_size_per_partition) for out in layer.logical_widths ] if split_weights: decompressed_shards = [ _process_split(compressed_weight, shape, bitmask) for compressed_weight, shape, bitmask in zip( split_weights, split_shape, split_bitmask ) ] decompressed = combine_shards(decompressed_shards) else: decompressed = sparsity_compressor.decompress_weight( dict( compressed=compressed, shape=( layer.logical_widths[0], layer.input_size_per_partition, ), bitmask=bitmask, ) ) return decompressed
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w4a8_fp8.py
vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w4a8_fp8.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import torch from compressed_tensors.quantization import ActivationOrdering from vllm.logger import init_logger from vllm.model_executor.layers.quantization.compressed_tensors.schemes import ( CompressedTensorsScheme, ) from vllm.model_executor.layers.quantization.kernels.mixed_precision import ( MPLinearLayerConfig, choose_mp_linear_kernel, ) from vllm.model_executor.layers.quantization.utils.marlin_utils import ( marlin_repeat_scales_on_all_ranks, ) from vllm.model_executor.parameter import ( BasevLLMParameter, ChannelQuantScaleParameter, GroupQuantScaleParameter, PackedvLLMParameter, ) from vllm.scalar_type import scalar_types logger = init_logger(__name__) __all__ = ["CompressedTensorsW4A8Fp8"] W4A8_SUPPORTED_TYPES_MAP = { 4: scalar_types.int4, } W4A8_SUPPORTED_BITS = list(W4A8_SUPPORTED_TYPES_MAP.keys()) class CompressedTensorsW4A8Fp8(CompressedTensorsScheme): _kernel_backends_being_used: set[str] = set() def __init__( self, strategy: str, num_bits: int, group_size: int | None = None, symmetric: bool | None = True, actorder: ActivationOrdering | None = None, ): self.pack_factor = 32 // num_bits self.strategy = strategy self.symmetric = symmetric self.group_size = -1 if group_size is None else group_size self.has_g_idx = actorder == ActivationOrdering.GROUP if self.group_size != 128 or self.strategy != "group": raise ValueError( "W4A8 kernels require group quantization with group size 128" ) if num_bits not in W4A8_SUPPORTED_TYPES_MAP: raise ValueError( f"Unsupported num_bits = {num_bits}. " f"Supported num_bits = {W4A8_SUPPORTED_TYPES_MAP.keys()}" ) self.quant_type = W4A8_SUPPORTED_TYPES_MAP[num_bits] @classmethod def get_min_capability(cls) -> int: # hopper return 90 def create_weights( self, layer: torch.nn.Module, output_size: int, input_size: int, output_partition_sizes: list[int], input_size_per_partition: int, params_dtype: torch.dtype, weight_loader: Callable, **kwargs, ): output_size_per_partition = sum(output_partition_sizes) mp_linear_kernel_config = MPLinearLayerConfig( full_weight_shape=(input_size, output_size), partition_weight_shape=( input_size_per_partition, output_size_per_partition, ), weight_type=self.quant_type, act_type=torch.float8_e4m3fn, # always use fp8(e4m3) group_size=self.group_size, zero_points=not self.symmetric, has_g_idx=self.has_g_idx, out_type=params_dtype, ) kernel_type = choose_mp_linear_kernel(mp_linear_kernel_config) if kernel_type.__name__ not in self._kernel_backends_being_used: logger.info("Using %s for CompressedTensorsW4A8Fp8", kernel_type.__name__) self._kernel_backends_being_used.add(kernel_type.__name__) # If group_size is -1, we are in channelwise case. group_size = self.group_size if self.group_size != -1 else input_size row_parallel = input_size != input_size_per_partition partition_scales = not marlin_repeat_scales_on_all_ranks( self.has_g_idx, self.group_size, row_parallel ) scales_and_zp_size = input_size // group_size if partition_scales: assert input_size_per_partition % group_size == 0 scales_and_zp_size = input_size_per_partition // group_size weight = PackedvLLMParameter( input_dim=1, output_dim=0, weight_loader=weight_loader, packed_factor=self.pack_factor, packed_dim=1, data=torch.empty( output_size_per_partition, input_size_per_partition // self.pack_factor, dtype=torch.int32, ), ) # After loading, we will transform bf16 -> fp8 -> # expand by 8x via `cutlass_pack_scale_fp8` # and construct per-channel fp32 scales. weight_scale_args = { "weight_loader": weight_loader, "data": torch.empty( output_size_per_partition, scales_and_zp_size, dtype=params_dtype, ), } if not partition_scales: weight_scale = ChannelQuantScaleParameter(output_dim=0, **weight_scale_args) else: weight_scale = GroupQuantScaleParameter( output_dim=0, input_dim=1, **weight_scale_args ) # A 2D array defining the original shape of the weights # before packing weight_shape = BasevLLMParameter( data=torch.empty(2, dtype=torch.int64), weight_loader=weight_loader ) layer.register_parameter("weight_packed", weight) layer.register_parameter("weight_scale", weight_scale) layer.register_parameter("weight_shape", weight_shape) self.kernel = kernel_type( mp_linear_kernel_config, w_q_param_name="weight_packed", w_s_param_name="weight_scale", w_zp_param_name="weight_zero_point", w_gidx_param_name="weight_g_idx", ) # Checkpoints are serialized in compressed-tensors format, which is # different from the format the kernel may want. Handle repacking here. def process_weights_after_loading(self, layer: torch.nn.Module) -> None: self.kernel.process_weights_after_loading(layer) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None ) -> torch.Tensor: return self.kernel.apply_weights(layer, x, bias)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w4a16_24.py
vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w4a16_24.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import torch from torch.nn import Parameter from vllm import _custom_ops as ops from vllm.model_executor.layers.quantization.compressed_tensors.schemes import ( CompressedTensorsScheme, ) from vllm.model_executor.layers.quantization.gptq_marlin_24 import ( GPTQ_MARLIN_24_MAX_PARALLEL, GPTQ_MARLIN_24_MIN_THREAD_N, ) from vllm.model_executor.parameter import ( BasevLLMParameter, ChannelQuantScaleParameter, GroupQuantScaleParameter, PackedvLLMParameter, ) from vllm.scalar_type import scalar_types __all__ = ["CompressedTensorsW4A16Sparse24"] W4A16SPARSE24_SUPPORTED_TYPES_MAP = { 4: scalar_types.uint4b8, } W4A16SPARSE24_SUPPORTED_BITS = list(W4A16SPARSE24_SUPPORTED_TYPES_MAP.keys()) class CompressedTensorsW4A16Sparse24(CompressedTensorsScheme): def __init__(self, strategy: str, num_bits: int, group_size: int | None = None): self.strategy = strategy self.group_size = group_size self.tile_size = 16 if num_bits not in W4A16SPARSE24_SUPPORTED_TYPES_MAP: raise ValueError( f"Unsupported num_bits = {num_bits}. " f"Supported num_bits = {W4A16SPARSE24_SUPPORTED_BITS}" ) self.quant_type = W4A16SPARSE24_SUPPORTED_TYPES_MAP[num_bits] if self.strategy == "group" and self.group_size is None: raise ValueError("group_size must be given when using strategy group") @classmethod def get_min_capability(cls) -> int: # ampere + up return 80 def process_weights_after_loading(self, layer: torch.nn.Module) -> None: # required by torch.compile to be torch.nn.Parameter layer.weight_packed = Parameter(layer.weight_packed.data, requires_grad=False) layer.scale_packed = Parameter(layer.scale_packed.data, requires_grad=False) layer.meta = Parameter(layer.meta.data, requires_grad=False) def create_weights( self, layer: torch.nn.Module, input_size: int, output_partition_sizes: list[int], input_size_per_partition: int, params_dtype: torch.dtype, weight_loader: Callable, **kwargs, ): assert params_dtype == torch.float16, ( "float16 is required for marlin24 compressed models. Set dtype=torch.float16" # noqa: E501 ) pack_factor = 32 // self.quant_type.size_bits output_size_per_partition = sum(output_partition_sizes) qweight = PackedvLLMParameter( data=torch.empty( input_size_per_partition // self.tile_size // 2, output_size_per_partition * self.tile_size // pack_factor, dtype=torch.int32, ), input_dim=0, output_dim=1, packed_dim=1, packed_factor=pack_factor, marlin_tile_size=self.tile_size, weight_loader=weight_loader, ) input_groups = ( 1 if self.group_size is None else input_size_per_partition // self.group_size ) weight_scale_args = { "data": torch.empty( input_groups, output_size_per_partition, dtype=params_dtype, ), "weight_loader": weight_loader, } if self.group_size is not None: scales = GroupQuantScaleParameter( output_dim=1, input_dim=0, **weight_scale_args ) else: scales = ChannelQuantScaleParameter(output_dim=1, **weight_scale_args) weight_shape = BasevLLMParameter( data=torch.empty(2, dtype=torch.int64), weight_loader=weight_loader ) meta = PackedvLLMParameter( data=torch.empty( input_size_per_partition // 8 // 2 // 2, output_size_per_partition * 2, dtype=torch.int16, ), input_dim=0, output_dim=1, packed_dim=1, packed_factor=1, marlin_tile_size=2, weight_loader=weight_loader, ) layer.register_parameter("weight_packed", qweight) layer.register_parameter("weight_shape", weight_shape) layer.register_parameter("scale_packed", scales) layer.register_parameter("meta", meta) max_workspace_size = ( output_size_per_partition // GPTQ_MARLIN_24_MIN_THREAD_N ) * GPTQ_MARLIN_24_MAX_PARALLEL workspace = Parameter( torch.zeros(max_workspace_size, dtype=torch.int), requires_grad=False ) layer.workspace = workspace def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None ) -> torch.Tensor: qweight = layer.weight_packed meta = layer.meta scales = layer.scale_packed workspace = layer.workspace x_2d = x.view(-1, x.shape[-1]) size_m = x_2d.shape[0] size_k = x_2d.shape[1] size_n = scales.shape[1] output_2d = ops.gptq_marlin_24_gemm( x_2d, qweight, meta, scales, workspace, self.quant_type, size_m, size_n, size_k, ) output = output_2d.view(x.shape[:-1] + (output_2d.shape[1],)) if bias is not None: output.add_(bias) # In-place add return output
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w8a8_int8.py
vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w8a8_int8.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import torch from compressed_tensors.quantization import QuantizationStrategy from vllm.logger import init_logger from vllm.model_executor.layers.quantization.compressed_tensors.schemes import ( CompressedTensorsScheme, ) from vllm.model_executor.layers.quantization.kernels.scaled_mm import ( ScaledMMLinearLayerConfig, choose_scaled_mm_linear_kernel, ) from vllm.model_executor.parameter import ( BasevLLMParameter, ChannelQuantScaleParameter, ModelWeightParameter, PerTensorScaleParameter, ) logger = init_logger(__name__) class CompressedTensorsW8A8Int8(CompressedTensorsScheme): _kernel_backends_being_used: set[str] = set() def __init__( self, strategy: str, is_static_input_scheme: bool, input_symmetric: bool ): self.strategy = strategy self.is_static_input_scheme = is_static_input_scheme self.input_symmetric = input_symmetric @classmethod def get_min_capability(cls) -> int: # turing and up return 75 def create_weights( self, layer: torch.nn.Module, output_partition_sizes: list[int], input_size_per_partition: int, params_dtype: torch.dtype, weight_loader: Callable, **kwargs, ): layer.logical_widths = output_partition_sizes scaled_mm_linear_kernel_config = ScaledMMLinearLayerConfig( is_channelwise=(self.strategy == QuantizationStrategy.CHANNEL), is_static_input_scheme=self.is_static_input_scheme, input_symmetric=self.input_symmetric, ) kernel_type = choose_scaled_mm_linear_kernel(scaled_mm_linear_kernel_config) if kernel_type.__name__ not in self._kernel_backends_being_used: logger.info("Using %s for CompressedTensorsW8A8Int8", kernel_type.__name__) self._kernel_backends_being_used.add(kernel_type.__name__) # WEIGHT weight = ModelWeightParameter( data=torch.empty( sum(output_partition_sizes), input_size_per_partition, dtype=torch.int8 ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight", weight) # WEIGHT SCALE if self.strategy == QuantizationStrategy.CHANNEL: weight_scale = ChannelQuantScaleParameter( data=torch.empty((sum(output_partition_sizes), 1), dtype=torch.float32), output_dim=0, weight_loader=weight_loader, ) else: assert self.strategy == QuantizationStrategy.TENSOR weight_scale = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) layer.register_parameter("weight_scale", weight_scale) # INPUT SCALE if self.is_static_input_scheme: input_scale = BasevLLMParameter( data=torch.empty(1, dtype=torch.float32), weight_loader=weight_loader ) layer.register_parameter("input_scale", input_scale) if not self.input_symmetric: # Note: compressed-tensors stores the zp using the same dtype # as the weights # AZP loaded as int8 but used as int32 input_zero_point = BasevLLMParameter( data=torch.empty(1, dtype=torch.int8), weight_loader=weight_loader ) layer.register_parameter("input_zero_point", input_zero_point) self.kernel = kernel_type( c=scaled_mm_linear_kernel_config, w_q_param_name="weight", w_s_param_name="weight_scale", i_s_param_name="input_scale", i_zp_param_name="input_zero_point", azp_adj_param_name="azp_adj", ) # Checkpoints are serialized in compressed-tensors format, which is # different from the format the kernel may want. Handle repacking here. def process_weights_after_loading(self, layer: torch.nn.Module) -> None: self.kernel.process_weights_after_loading(layer) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None ) -> torch.Tensor: return self.kernel.apply_weights(layer, x, bias)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w4a8_int.py
vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w4a8_int.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import torch from vllm.logger import init_logger from vllm.model_executor.layers.quantization.compressed_tensors.schemes import ( CompressedTensorsScheme, ) from vllm.model_executor.layers.quantization.kernels.mixed_precision import ( MPLinearLayerConfig, choose_mp_linear_kernel, ) from vllm.model_executor.parameter import ( ChannelQuantScaleParameter, GroupQuantScaleParameter, ModelWeightParameter, ) from vllm.scalar_type import scalar_types logger = init_logger(__name__) __all__ = ["CompressedTensorsW4A8Int"] W4A8_SUPPORTED_TYPES_MAP = { 4: scalar_types.int4, } W4A8_SUPPORTED_BITS = list(W4A8_SUPPORTED_TYPES_MAP.keys()) class CompressedTensorsW4A8Int(CompressedTensorsScheme): _kernel_backends_being_used: set[str] = set() def __init__( self, strategy: str, num_bits: int, group_size: int | None = None, is_static_input_scheme: bool = False, input_symmetric: bool = True, ): self.strategy = strategy self.group_size = -1 if group_size is None else group_size self.is_static_input_scheme = is_static_input_scheme self.input_symmetric = input_symmetric if num_bits not in W4A8_SUPPORTED_TYPES_MAP: raise ValueError( f"Unsupported num_bits = {num_bits}." f"Supported num_bits = {W4A8_SUPPORTED_TYPES_MAP.keys()}" ) self.quant_type = W4A8_SUPPORTED_TYPES_MAP[num_bits] @classmethod def get_min_capability(cls) -> int: return 1 def create_weights( self, layer: torch.nn.Module, output_size: int, input_size: int, output_partition_sizes: list[int], input_size_per_partition: int, params_dtype: torch.dtype, weight_loader: Callable, **kwargs, ): output_size_per_partition = sum(output_partition_sizes) row_parallel = input_size != input_size_per_partition # Compute effective group_size if self.group_size == -1: effective_group_size = ( input_size_per_partition if row_parallel else input_size ) else: effective_group_size = self.group_size # Ensure group_size divides input_size_per_partition assert input_size_per_partition % effective_group_size == 0, ( f"input_size_per_partition {input_size_per_partition}" f" not divisible by group_size {effective_group_size}" ) # Determine scale partitioning is_channelwise = self.group_size == -1 repeat_scales = is_channelwise and row_parallel partition_scales = not repeat_scales mp_linear_kernel_config = MPLinearLayerConfig( full_weight_shape=(input_size, output_size), partition_weight_shape=( input_size_per_partition, output_size_per_partition, ), weight_type=self.quant_type, act_type=params_dtype, group_size=effective_group_size, zero_points=False, has_g_idx=False, ) kernel_type = choose_mp_linear_kernel(mp_linear_kernel_config) if kernel_type.__name__ not in self._kernel_backends_being_used: logger.info("Using %s for CompressedTensorsW4A8Int", kernel_type.__name__) self._kernel_backends_being_used.add(kernel_type.__name__) scales_and_zp_size = input_size_per_partition // effective_group_size weight = ModelWeightParameter( data=torch.empty( output_size_per_partition, input_size_per_partition, dtype=torch.int8 ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight", weight) weight_scale_args = { "weight_loader": weight_loader, "data": torch.empty( output_size_per_partition, scales_and_zp_size, dtype=params_dtype ), } if partition_scales: weight_scale = GroupQuantScaleParameter( output_dim=0, input_dim=1, **weight_scale_args ) else: weight_scale = ChannelQuantScaleParameter(output_dim=0, **weight_scale_args) layer.register_parameter("weight_packed", weight) layer.register_parameter("weight_scale", weight_scale) self.kernel = kernel_type( mp_linear_kernel_config, w_q_param_name="weight_packed", w_s_param_name="weight_scale", w_zp_param_name=None, w_gidx_param_name=None, ) def process_weights_after_loading(self, layer: torch.nn.Module) -> None: self.kernel.process_weights_after_loading(layer) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None ) -> torch.Tensor: return self.kernel.apply_weights(layer, x, bias)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w4a4_nvfp4.py
vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w4a4_nvfp4.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import torch from torch.nn.parameter import Parameter import vllm.envs as envs from vllm._custom_ops import cutlass_scaled_fp4_mm, scaled_fp4_quant from vllm.logger import init_logger from vllm.model_executor.layers.quantization.compressed_tensors.schemes import ( CompressedTensorsScheme, ) from vllm.model_executor.layers.quantization.utils.nvfp4_emulation_utils import ( # noqa: E501 run_nvfp4_emulations, ) from vllm.model_executor.layers.quantization.utils.quant_utils import ( cutlass_fp4_supported, swizzle_blockscale, ) from vllm.model_executor.parameter import ( GroupQuantScaleParameter, ModelWeightParameter, PerTensorScaleParameter, ) from vllm.utils.flashinfer import flashinfer_scaled_fp4_mm, has_flashinfer logger = init_logger(__name__) __all__ = ["CompressedTensorsW4A4Fp4"] class CompressedTensorsW4A4Fp4(CompressedTensorsScheme): def __init__(self): self.backend = "none" if envs.VLLM_NVFP4_GEMM_BACKEND is None: if has_flashinfer(): self.backend = "flashinfer-cutlass" elif cutlass_fp4_supported(): self.backend = "cutlass" elif envs.VLLM_USE_FBGEMM: self.backend = "fbgemm" try: import fbgemm_gpu # noqa: F401 except ImportError as exc: raise ImportError( "Backend fbgemm requires fbgemm.f4f4bf16 operator, " "Please install with: pip install fbgemm-gpu-genai" ) from exc elif envs.VLLM_NVFP4_GEMM_BACKEND.startswith("flashinfer-"): self.backend = envs.VLLM_NVFP4_GEMM_BACKEND assert has_flashinfer(), f"FlashInfer is required for {self.backend}" elif envs.VLLM_NVFP4_GEMM_BACKEND == "cutlass": self.backend = "cutlass" assert cutlass_fp4_supported(), f"Cutlass is required for {self.backend}" if self.backend == "none": raise ValueError( "No valid NVFP4 GEMM backend found. " "Please check your platform capability." ) logger.info_once(f"Using {self.backend} for NVFP4 GEMM") self.group_size = 16 @classmethod def get_min_capability(cls) -> int: if envs.VLLM_USE_NVFP4_CT_EMULATIONS: return 80 return 100 def create_weights( self, layer: torch.nn.Module, output_partition_sizes: list[int], input_size_per_partition: int, params_dtype: torch.dtype, weight_loader: Callable, **kwargs, ): output_size_per_partition = sum(output_partition_sizes) layer.logical_widths = output_partition_sizes layer.input_size_per_partition = input_size_per_partition layer.output_size_per_partition = output_size_per_partition # Weight weight = ModelWeightParameter( data=torch.empty( sum(output_partition_sizes), input_size_per_partition // 2, dtype=torch.uint8, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight_packed", weight) # Global Weight Scale weight_global_scale = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) layer.register_parameter("weight_global_scale", weight_global_scale) # Per Group Weight Scale weight_scale = GroupQuantScaleParameter( data=torch.empty( sum(output_partition_sizes), input_size_per_partition // self.group_size, dtype=torch.float8_e4m3fn, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight_scale", weight_scale) input_global_scale = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) layer.register_parameter("input_global_scale", input_global_scale) def process_weights_after_loading(self, layer) -> None: global_input_scale = layer.input_global_scale.max().to(torch.float32) layer.input_global_scale = Parameter(global_input_scale, requires_grad=False) layer.weight_global_scale = Parameter( layer.weight_global_scale.max().to(torch.float32), requires_grad=False ) if self.backend == "flashinfer-trtllm": # FlashInfer TRTLLM FP4 GEMM requires a different weight layout. # FlashInfer provides nvfp4_quantize to quantize + shuffle the # layout but we use our own quantization so we have to call # shuffles ourselves. from flashinfer import shuffle_matrix_a, shuffle_matrix_sf_a weight = layer.weight_packed.data weight_scale = layer.weight_scale.data epilogue_tile_m = 128 weight = shuffle_matrix_a(weight.view(torch.uint8), epilogue_tile_m) weight_scale = ( shuffle_matrix_sf_a(weight_scale.view(torch.uint8), epilogue_tile_m) .reshape(weight_scale.shape) .view(torch.float8_e4m3fn) ) layer.weight_scale = Parameter(weight_scale, requires_grad=False) layer.weight_packed = Parameter(weight, requires_grad=False) else: swizzled_weight_scale = swizzle_blockscale(layer.weight_scale) if self.backend == "fbgemm": swizzled_weight_scale = swizzled_weight_scale.view(-1).view(torch.uint8) layer.weight_scale = Parameter(swizzled_weight_scale, requires_grad=False) layer.weight_packed = Parameter( layer.weight_packed.data, requires_grad=False ) layer.alpha = Parameter( 1 / (layer.input_global_scale * layer.weight_global_scale), requires_grad=False, ) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: if envs.VLLM_USE_NVFP4_CT_EMULATIONS: out = run_nvfp4_emulations( x=x, input_global_scale=layer.input_global_scale, weight=layer.weight_packed, weight_scale_swizzled=layer.weight_scale, weight_global_scale=layer.weight_global_scale, ) if bias is not None: out = out + bias return out output_dtype = x.dtype output_shape = [*x.shape[:-1], layer.weight_packed.shape[0]] # quantize BF16 or FP16 to (FP4 and interleaved block scale) x_fp4, x_blockscale = scaled_fp4_quant(x, layer.input_global_scale) mm_args = ( x_fp4, layer.weight_packed, x_blockscale, layer.weight_scale, layer.alpha, output_dtype, ) if self.backend.startswith("flashinfer-"): backend_name = self.backend[len("flashinfer-") :] out = flashinfer_scaled_fp4_mm(*mm_args, backend=backend_name) elif self.backend == "fbgemm": out = torch.ops.fbgemm.f4f4bf16( x_fp4, layer.weight_packed, x_blockscale.view(-1).view(torch.uint8), layer.weight_scale, layer.alpha, use_mx=False, ).to(output_dtype) else: assert self.backend == "cutlass" out = cutlass_scaled_fp4_mm(*mm_args) if bias is not None: out = out + bias return out.view(*output_shape)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/schemes/__init__.py
vllm/model_executor/layers/quantization/compressed_tensors/schemes/__init__.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from .compressed_tensors_scheme import CompressedTensorsScheme from .compressed_tensors_w4a4_nvfp4 import CompressedTensorsW4A4Fp4 from .compressed_tensors_w4a8_fp8 import CompressedTensorsW4A8Fp8 from .compressed_tensors_w4a8_int import CompressedTensorsW4A8Int from .compressed_tensors_w4a16_24 import ( W4A16SPARSE24_SUPPORTED_BITS, CompressedTensorsW4A16Sparse24, ) from .compressed_tensors_w4a16_nvfp4 import CompressedTensorsW4A16Fp4 from .compressed_tensors_w8a8_fp8 import CompressedTensorsW8A8Fp8 from .compressed_tensors_w8a8_int8 import CompressedTensorsW8A8Int8 from .compressed_tensors_w8a16_fp8 import CompressedTensorsW8A16Fp8 from .compressed_tensors_wNa16 import WNA16_SUPPORTED_BITS, CompressedTensorsWNA16 # This avoids circular import error from .compressed_tensors_24 import CompressedTensors24 # isort: skip __all__ = [ "CompressedTensorsScheme", "CompressedTensorsWNA16", "CompressedTensorsW8A16Fp8", "CompressedTensorsW4A16Sparse24", "CompressedTensorsW8A8Int8", "CompressedTensorsW8A8Fp8", "WNA16_SUPPORTED_BITS", "W4A16SPARSE24_SUPPORTED_BITS", "CompressedTensors24", "CompressedTensorsW4A16Fp4", "CompressedTensorsW4A4Fp4", "CompressedTensorsW4A8Int", "CompressedTensorsW4A8Fp8", ]
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w8a8_fp8.py
vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w8a8_fp8.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import torch from compressed_tensors.quantization import QuantizationArgs, QuantizationStrategy from torch.nn import Parameter from vllm._aiter_ops import rocm_aiter_ops from vllm.model_executor.layers.quantization.compressed_tensors.schemes import ( CompressedTensorsScheme, ) from vllm.model_executor.layers.quantization.utils.fp8_utils import ( W8A8BlockFp8LinearOp, create_fp8_input_scale, create_fp8_scale_parameter, create_fp8_weight_parameter, maybe_post_process_fp8_weight_block, process_fp8_weight_block_strategy, process_fp8_weight_channel_strategy, process_fp8_weight_tensor_strategy, validate_fp8_block_shape, ) from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape from vllm.model_executor.layers.quantization.utils.w8a8_utils import ( Fp8LinearOp, cutlass_block_fp8_supported, maybe_create_device_identity, ) from vllm.model_executor.parameter import ( BlockQuantScaleParameter, ChannelQuantScaleParameter, PerTensorScaleParameter, ) __all__ = ["CompressedTensorsW8A8Fp8"] strategy_to_parameter_type = { QuantizationStrategy.BLOCK: BlockQuantScaleParameter, QuantizationStrategy.CHANNEL: ChannelQuantScaleParameter, QuantizationStrategy.TENSOR: PerTensorScaleParameter, } class CompressedTensorsW8A8Fp8(CompressedTensorsScheme): def __init__(self, weight_quant: QuantizationArgs, is_static_input_scheme: bool): self.weight_quant = weight_quant self.strategy = weight_quant.strategy self.out_dtype = torch.get_default_dtype() self.is_static_input_scheme = is_static_input_scheme self.weight_block_size = self.weight_quant.block_structure if self.weight_block_size is not None: self.act_q_group_shape = GroupShape(1, self.weight_block_size[0]) else: self.act_q_group_shape = ( GroupShape.PER_TENSOR if is_static_input_scheme else GroupShape.PER_TOKEN ) self.cutlass_block_fp8_supported = cutlass_block_fp8_supported() self.use_aiter_and_is_supported = rocm_aiter_ops.is_linear_fp8_enabled() if self.weight_block_size is not None: assert not self.is_static_input_scheme self.w8a8_block_fp8_linear = W8A8BlockFp8LinearOp( weight_group_shape=GroupShape(*self.weight_block_size), act_quant_group_shape=self.act_q_group_shape, cutlass_block_fp8_supported=self.cutlass_block_fp8_supported, use_aiter_and_is_supported=self.use_aiter_and_is_supported, ) else: self.fp8_linear = Fp8LinearOp( act_quant_static=self.is_static_input_scheme, act_quant_group_shape=self.act_q_group_shape, ) @classmethod def get_min_capability(cls) -> int: # lovelace and up return 89 def create_weights( self, layer: torch.nn.Module, input_size_per_partition: int, output_partition_sizes: list[int], input_size: int, output_size: int, params_dtype: torch.dtype, weight_loader: Callable, **kwargs, ): maybe_create_device_identity() output_size_per_partition = sum(output_partition_sizes) layer.logical_widths = output_partition_sizes layer.weight_block_size = None layer.orig_dtype = params_dtype if self.strategy == QuantizationStrategy.BLOCK: assert self.weight_block_size is not None layer.weight_block_size = self.weight_block_size # Validate block quantization shapes validate_fp8_block_shape( layer, input_size, output_size, input_size_per_partition, output_partition_sizes, self.weight_block_size, ) # WEIGHT weight = create_fp8_weight_parameter( output_size_per_partition, input_size_per_partition, weight_loader ) layer.register_parameter("weight", weight) # WEIGHT SCALE weight_scale = create_fp8_scale_parameter( strategy_to_parameter_type[self.strategy], output_partition_sizes, input_size_per_partition, layer.weight_block_size, weight_loader, ) layer.register_parameter("weight_scale", weight_scale) # INPUT SCALE if self.is_static_input_scheme: input_scale = create_fp8_input_scale(output_partition_sizes, weight_loader) layer.register_parameter("input_scale", input_scale) def process_weights_after_loading(self, layer) -> None: if self.strategy == QuantizationStrategy.TENSOR: weight, weight_scale, input_scale = process_fp8_weight_tensor_strategy( layer.weight, layer.weight_scale, layer.logical_widths, getattr(layer, "input_scale", None), ) weight = weight.t() elif self.strategy == QuantizationStrategy.CHANNEL: weight, weight_scale, input_scale = process_fp8_weight_channel_strategy( layer.weight, layer.weight_scale, getattr(layer, "input_scale", None) ) weight = weight.t() elif self.strategy == QuantizationStrategy.BLOCK: assert self.is_static_input_scheme is False weight, weight_scale = process_fp8_weight_block_strategy( layer.weight, layer.weight_scale ) input_scale = None else: raise ValueError(f"Unknown quantization strategy {self.strategy}") # required by torch.compile to be torch.nn.Parameter layer.weight = Parameter(weight.data, requires_grad=False) layer.weight_scale = Parameter(weight_scale.data, requires_grad=False) if input_scale is not None: layer.input_scale = Parameter(input_scale.data, requires_grad=False) # INPUT SCALE if self.is_static_input_scheme and hasattr(layer, "input_scale"): layer.input_scale = Parameter(layer.input_scale.max(), requires_grad=False) else: layer.input_scale = None if self.strategy == QuantizationStrategy.BLOCK: maybe_post_process_fp8_weight_block(layer) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: if self.weight_block_size is not None: return self.w8a8_block_fp8_linear.apply( input=x, weight=layer.weight, weight_scale=layer.weight_scale, input_scale=layer.input_scale, bias=bias, ) return self.fp8_linear.apply( input=x, weight=layer.weight, weight_scale=layer.weight_scale, out_dtype=self.out_dtype, input_scale=layer.input_scale, bias=bias, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w8a16_fp8.py
vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w8a16_fp8.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import torch from compressed_tensors.quantization import QuantizationStrategy from vllm.model_executor.layers.quantization.compressed_tensors.schemes import ( CompressedTensorsScheme, ) from vllm.model_executor.layers.quantization.utils.marlin_utils_fp8 import ( apply_fp8_marlin_linear, prepare_fp8_layer_for_marlin, ) from vllm.model_executor.layers.quantization.utils.w8a8_utils import ( convert_to_channelwise, ) from vllm.model_executor.parameter import ( ChannelQuantScaleParameter, ModelWeightParameter, PerTensorScaleParameter, ) __all__ = ["CompressedTensorsW8A16Fp8"] SUPPORTED_STRATEGIES = [QuantizationStrategy.CHANNEL, QuantizationStrategy.TENSOR] class CompressedTensorsW8A16Fp8(CompressedTensorsScheme): def __init__(self, strategy: str, is_static_input_scheme: bool): self.strategy = strategy self.is_static_input_scheme = is_static_input_scheme @classmethod def get_min_capability(cls) -> int: # ampere and up return 80 # W8A8-Fp8 kernels support only per-tensor and per-channel cases. # So if we have a fused module (QKV, MLP) with per tensor scales, # we expand each scale to its shard's channels. def process_weights_after_loading(self, layer) -> None: if self.strategy == QuantizationStrategy.TENSOR: ws_channelwise = convert_to_channelwise( layer.weight_scale, layer.logical_widths ) layer.weight_scale = torch.nn.Parameter(ws_channelwise, requires_grad=False) else: # required by torch.compile to be torch.nn.Parameter layer.weight_scale = torch.nn.Parameter( layer.weight_scale.data, requires_grad=False ) # Weights must be transposed for marlin layer.weight = torch.nn.Parameter(layer.weight.t(), requires_grad=False) if self.is_static_input_scheme: # required by torch.compile to be torch.nn.Parameter layer.input_scale = torch.nn.Parameter( layer.input_scale.data, requires_grad=False ) prepare_fp8_layer_for_marlin(layer) def create_weights( self, layer: torch.nn.Module, input_size: int, output_partition_sizes: list[int], input_size_per_partition: int, params_dtype: torch.dtype, weight_loader: Callable, **kwargs, ): output_size_per_partition = sum(output_partition_sizes) layer.logical_widths = output_partition_sizes layer.input_size_per_partition = input_size_per_partition layer.output_size_per_partition = output_size_per_partition layer.orig_dtype = params_dtype layer.weight_block_size = None # WEIGHT weight = ModelWeightParameter( data=torch.empty( output_size_per_partition, input_size_per_partition, dtype=torch.float8_e4m3fn, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight", weight) # WEIGHT SCALE if self.strategy == QuantizationStrategy.CHANNEL: weight_scale = ChannelQuantScaleParameter( data=torch.empty((sum(output_partition_sizes), 1), dtype=torch.float32), output_dim=0, weight_loader=weight_loader, ) elif self.strategy == QuantizationStrategy.TENSOR: weight_scale = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) else: raise ValueError( f"Unsupported weight strategy={self.strategy}, " f"supported strategies are {SUPPORTED_STRATEGIES}" ) weight_scale[:] = torch.finfo(torch.float32).min layer.register_parameter("weight_scale", weight_scale) # INPUT SCALE (to deal with converted checkpoints) if self.is_static_input_scheme: input_scale = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) layer.register_parameter("input_scale", input_scale) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: return apply_fp8_marlin_linear( input=x, weight=layer.weight, weight_scale=layer.weight_scale, workspace=layer.workspace, size_n=layer.output_size_per_partition, size_k=layer.input_size_per_partition, bias=bias, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_wNa16.py
vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_wNa16.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import torch from compressed_tensors.quantization import ActivationOrdering from vllm.logger import init_logger from vllm.model_executor.layers.quantization.compressed_tensors.schemes import ( CompressedTensorsScheme, ) from vllm.model_executor.layers.quantization.kernels.mixed_precision import ( MPLinearLayerConfig, choose_mp_linear_kernel, ) from vllm.model_executor.layers.quantization.kernels.mixed_precision.marlin import ( MarlinLinearKernel, ) from vllm.model_executor.layers.quantization.utils.marlin_utils import ( get_marlin_input_dtype, marlin_repeat_scales_on_all_ranks, ) from vllm.model_executor.parameter import ( BasevLLMParameter, ChannelQuantScaleParameter, GroupQuantScaleParameter, PackedColumnParameter, PackedvLLMParameter, RowvLLMParameter, ) from vllm.scalar_type import scalar_types logger = init_logger(__name__) __all__ = ["CompressedTensorsWNA16"] WNA16_SUPPORTED_TYPES_MAP = {4: scalar_types.uint4b8, 8: scalar_types.uint8b128} WNA16_ZP_SUPPORTED_TYPES_MAP = {4: scalar_types.uint4, 8: scalar_types.uint8} WNA16_SUPPORTED_BITS = list(WNA16_SUPPORTED_TYPES_MAP.keys()) class CompressedTensorsWNA16(CompressedTensorsScheme): _kernel_backends_being_used: set[str] = set() def __init__( self, strategy: str, num_bits: int, group_size: int | None = None, symmetric: bool | None = True, actorder: ActivationOrdering | None = None, layer_name: str | None = None, ): self.pack_factor = 32 // num_bits self.strategy = strategy self.symmetric = symmetric self.group_size = -1 if group_size is None else group_size self.has_g_idx = actorder == ActivationOrdering.GROUP self.layer_name = layer_name if self.group_size == -1 and self.strategy != "channel": raise ValueError( "Marlin kernels require group quantization or " "channelwise quantization, but found no group " "size and strategy is not channelwise." ) if num_bits not in WNA16_SUPPORTED_TYPES_MAP: raise ValueError( f"Unsupported num_bits = {num_bits}. " f"Supported num_bits = {WNA16_SUPPORTED_TYPES_MAP.keys()}" ) self.quant_type = ( WNA16_ZP_SUPPORTED_TYPES_MAP[num_bits] if not self.symmetric else WNA16_SUPPORTED_TYPES_MAP[num_bits] ) @classmethod def get_min_capability(cls) -> int: # Turing and up return 75 def create_weights( self, layer: torch.nn.Module, output_size: int, input_size: int, output_partition_sizes: list[int], input_size_per_partition: int, params_dtype: torch.dtype, weight_loader: Callable, **kwargs, ): output_size_per_partition = sum(output_partition_sizes) mp_linear_kernel_config = MPLinearLayerConfig( full_weight_shape=(input_size, output_size), partition_weight_shape=( input_size_per_partition, output_size_per_partition, ), weight_type=self.quant_type, act_type=params_dtype, group_size=self.group_size, zero_points=not self.symmetric, has_g_idx=self.has_g_idx, ) kernel_type = choose_mp_linear_kernel(mp_linear_kernel_config) if kernel_type.__name__ not in self._kernel_backends_being_used: logger.info("Using %s for CompressedTensorsWNA16", kernel_type.__name__) self._kernel_backends_being_used.add(kernel_type.__name__) if kernel_type is MarlinLinearKernel: input_dtype = get_marlin_input_dtype(self.layer_name) if input_dtype is not None: mp_linear_kernel_config.act_type = input_dtype # If group_size is -1, we are in channelwise case. group_size = self.group_size if self.group_size != -1 else input_size row_parallel = input_size != input_size_per_partition partition_scales = not marlin_repeat_scales_on_all_ranks( self.has_g_idx, self.group_size, row_parallel ) scales_and_zp_size = input_size // group_size if partition_scales: assert input_size_per_partition % group_size == 0 scales_and_zp_size = input_size_per_partition // group_size weight = PackedvLLMParameter( input_dim=1, output_dim=0, weight_loader=weight_loader, packed_factor=self.pack_factor, packed_dim=1, data=torch.empty( output_size_per_partition, input_size_per_partition // self.pack_factor, dtype=torch.int32, ), ) weight_scale_args = { "weight_loader": weight_loader, "data": torch.empty( output_size_per_partition, scales_and_zp_size, dtype=params_dtype, ), } zeros_args = { "weight_loader": weight_loader, "data": torch.zeros( output_size_per_partition // self.pack_factor, scales_and_zp_size, dtype=torch.int32, ), } if not partition_scales: weight_scale = ChannelQuantScaleParameter(output_dim=0, **weight_scale_args) if not self.symmetric: qzeros = PackedColumnParameter( output_dim=0, packed_dim=0, packed_factor=self.pack_factor, **zeros_args, ) else: weight_scale = GroupQuantScaleParameter( output_dim=0, input_dim=1, **weight_scale_args ) if not self.symmetric: qzeros = PackedvLLMParameter( input_dim=1, output_dim=0, packed_dim=0, packed_factor=self.pack_factor, **zeros_args, ) # A 2D array defining the original shape of the weights # before packing weight_shape = BasevLLMParameter( data=torch.empty(2, dtype=torch.int64), weight_loader=weight_loader ) layer.register_parameter("weight_packed", weight) layer.register_parameter("weight_scale", weight_scale) layer.register_parameter("weight_shape", weight_shape) if not self.symmetric: layer.register_parameter("weight_zero_point", qzeros) # group index (for activation reordering) if self.has_g_idx: weight_g_idx = RowvLLMParameter( data=torch.empty( input_size_per_partition, dtype=torch.int32, ), input_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight_g_idx", weight_g_idx) self.kernel = kernel_type( mp_linear_kernel_config, w_q_param_name="weight_packed", w_s_param_name="weight_scale", w_zp_param_name="weight_zero_point", w_gidx_param_name="weight_g_idx", ) # Checkpoints are serialized in compressed-tensors format, which is # different from the format the kernel may want. Handle repacking here. def process_weights_after_loading(self, layer: torch.nn.Module) -> None: self.kernel.process_weights_after_loading(layer) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None ) -> torch.Tensor: return self.kernel.apply_weights(layer, x, bias)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_scheme.py
vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_scheme.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from abc import ABC, abstractmethod import torch __all__ = ["CompressedTensorsScheme"] class CompressedTensorsScheme(ABC): """ Abstract class used to describe the weight creation and forward pass of different quantization schemes supported by CompressedTensors. """ @classmethod @abstractmethod def get_min_capability(cls) -> int: """ Get minimum device capability. """ raise NotImplementedError @abstractmethod def create_weights(self, *args, **kwargs): """ Weight creation for the particular scheme. Inputs to this function """ raise NotImplementedError @abstractmethod def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None ): """ Run the forward pass for the particular scheme. This is where scheme-specific dequant/quant steps/kernels should be applied. :param layer: torch.nn.Module with the registered weights and other parameters relevant to the particular scheme. :param x: input to the layer :param bias: bias parameter """ raise NotImplementedError @abstractmethod def process_weights_after_loading(self, layer: torch.nn.Module): """ Called after weight loading is complete for any cleanup that needs to occur. """ raise NotImplementedError
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/flashinfer_fp4_moe.py
vllm/model_executor/layers/quantization/utils/flashinfer_fp4_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Utility helpers for NVFP4 + FlashInfer fused-MoE path""" import torch import vllm.envs as envs import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm.model_executor.layers.fused_moe.config import ( FusedMoEConfig, FusedMoEQuantConfig, RoutingMethodType, ) from vllm.model_executor.layers.fused_moe.flashinfer_cutedsl_moe import ( FlashInferCuteDSLExperts, ) from vllm.model_executor.layers.fused_moe.flashinfer_cutlass_moe import ( FlashInferExperts, ) from vllm.model_executor.layers.fused_moe.flashinfer_cutlass_prepare_finalize import ( # noqa: E501 create_flashinfer_prepare_finalize, ) from vllm.platforms import current_platform from vllm.utils.flashinfer import ( has_flashinfer_cutedsl_grouped_gemm_nt_masked, has_flashinfer_cutlass_fused_moe, ) __all__ = [ "is_flashinfer_fp4_cutlass_moe_available", "is_flashinfer_fp4_cutedsl_moe_available", "reorder_w1w3_to_w3w1", "build_flashinfer_fp4_cutlass_moe_prepare_finalize", ] def is_flashinfer_fp4_cutlass_moe_available() -> bool: """Return `True` when FlashInfer CUTLASS NV-FP4 kernels can be used.""" return ( envs.VLLM_USE_FLASHINFER_MOE_FP4 and has_flashinfer_cutlass_fused_moe() and current_platform.is_cuda() and current_platform.has_device_capability(100) ) def is_flashinfer_fp4_cutedsl_moe_available() -> bool: """Return ``True`` when FlashInfer CUTEDSL NV-FP4 kernels can be used.""" return ( envs.VLLM_USE_FLASHINFER_MOE_FP4 and has_flashinfer_cutedsl_grouped_gemm_nt_masked() and current_platform.is_cuda() and current_platform.is_device_capability_family(100) ) def reorder_w1w3_to_w3w1( weight: torch.Tensor, scale: torch.Tensor, dim: int = -2 ) -> tuple[torch.Tensor, torch.Tensor]: """Re-order the concatenated `[w1, w3]` tensors to `[w3, w1]`""" size = weight.size(dim) assert size % 2 == 0, f"Expected even size in dim {dim}, got {size}" half = size // 2 w1, w3 = weight.split(half, dim=dim) s1, s3 = scale.split(half, dim=dim) return ( torch.cat([w3, w1], dim=dim).contiguous(), torch.cat([s3, s1], dim=dim).contiguous(), ) def build_flashinfer_fp4_cutlass_moe_prepare_finalize( moe: FusedMoEConfig, ) -> mk.FusedMoEPrepareAndFinalize: """Create a FlashInfer CUTLASS fused-MoE prepare finalize kernel""" use_dp = moe.moe_parallel_config.dp_size > 1 enable_alltoallv = moe.moe_parallel_config.all2all_backend == "flashinfer_all2allv" return create_flashinfer_prepare_finalize( use_dp=use_dp, use_nvfp4=True, enable_alltoallv=enable_alltoallv ) def select_nvfp4_gemm_impl( moe: FusedMoEConfig, moe_quant_config: FusedMoEQuantConfig, allow_flashinfer: bool, ) -> mk.FusedMoEPermuteExpertsUnpermute: """Return a GEMM *experts* implementation for NV-FP4 fused-MoE layers""" if allow_flashinfer: if envs.VLLM_FLASHINFER_MOE_BACKEND == "masked_gemm": return FlashInferCuteDSLExperts( out_dtype=moe.in_dtype, quant_config=moe_quant_config, ) elif envs.VLLM_FLASHINFER_MOE_BACKEND == "throughput": return FlashInferExperts( out_dtype=moe.in_dtype, quant_config=moe_quant_config, ep_rank=moe.moe_parallel_config.ep_rank, ep_size=moe.moe_parallel_config.ep_size, tp_rank=moe.moe_parallel_config.tp_rank, tp_size=moe.moe_parallel_config.tp_size, use_dp=moe.moe_parallel_config.dp_size > 1, ) # native cutlass experts currently don't support DP; TP case won't call this raise ValueError( "CutlassExpertsFp4 doesn't support DP. Use flashinfer CUTLASS " "Fused MoE backend instead (set VLLM_USE_FLASHINFER_MOE_FP4=1)" ) def prepare_static_weights_for_trtllm_fp4_moe( # args_dequant, # args, gemm1_weights, gemm2_weights, gemm1_scales_linear_fp4_bytes, gemm2_scales_linear_fp4_bytes, hidden_size, intermediate_size, num_experts, ): from flashinfer import nvfp4_block_scale_interleave from flashinfer.fused_moe.core import ( _maybe_get_cached_w3_w1_permute_indices, get_w2_permute_indices_with_cache, ) _cache_permute_indices: dict[torch.Size, torch.Tensor] = {} """Prepare quantized weights for kernel (done offline with weights).""" epilogue_tile_m = 128 # FIXME: this depends on the kernel internals # Convert quantized weights to proper formats gemm1_weights_fp4 = gemm1_weights.view(torch.float8_e4m3fn).reshape( num_experts, 2 * intermediate_size, hidden_size // 2 ) # packed fp4 gemm1_scales_linear_fp4 = gemm1_scales_linear_fp4_bytes.view( torch.float8_e4m3fn ).reshape( num_experts, 2 * intermediate_size, hidden_size // 16 ) # fp8 scaling factors gemm2_weights_fp4 = gemm2_weights.view(torch.float8_e4m3fn).reshape( num_experts, hidden_size, intermediate_size // 2 ) # packed fp4 gemm2_scales_linear_fp4 = gemm2_scales_linear_fp4_bytes.view( torch.float8_e4m3fn ).reshape(num_experts, hidden_size, intermediate_size // 16) # fp8 scaling factors gemm1_weights_fp4_shuffled = [] gemm1_scales_fp4_shuffled = [] gemm2_weights_fp4_shuffled = [] gemm2_scales_fp4_shuffled = [] for i in range(num_experts): # Calculate the permute indices for the following: # 1. Reorder rows of W1 and scales for fused gated activation # 2. Shuffle weights and scaling factors for transposed mma output # for both w3_w1 and w2 weights and scale factors permute_indices = _maybe_get_cached_w3_w1_permute_indices( _cache_permute_indices, gemm1_weights_fp4[i].view(torch.uint8), epilogue_tile_m, ) gemm1_weights_fp4_shuffled.append( gemm1_weights_fp4[i] .view(torch.uint8)[permute_indices.to(gemm1_weights_fp4.device)] .contiguous() ) permute_sf_indices = _maybe_get_cached_w3_w1_permute_indices( _cache_permute_indices, gemm1_scales_linear_fp4[i].view(torch.uint8), epilogue_tile_m, num_elts_per_sf=16, ) gemm1_scales_fp4_shuffled.append( nvfp4_block_scale_interleave( gemm1_scales_linear_fp4[i] .view(torch.uint8)[ permute_sf_indices.to(gemm1_scales_linear_fp4.device) ] .contiguous() ) ) permute_indices = get_w2_permute_indices_with_cache( _cache_permute_indices, gemm2_weights_fp4[i].view(torch.uint8), epilogue_tile_m, ) gemm2_weights_fp4_shuffled.append( gemm2_weights_fp4[i] .view(torch.uint8)[permute_indices.to(gemm2_weights_fp4.device)] .contiguous() ) permute_sf_indices = get_w2_permute_indices_with_cache( _cache_permute_indices, gemm2_scales_linear_fp4[i].view(torch.uint8), epilogue_tile_m, num_elts_per_sf=16, ) gemm2_scales_fp4_shuffled.append( nvfp4_block_scale_interleave( gemm2_scales_linear_fp4[i] .view(torch.uint8)[ permute_sf_indices.to(gemm2_scales_linear_fp4.device) ] .contiguous() ) ) # Stack weights for all experts gemm1_weights_fp4_shuffled = torch.stack(gemm1_weights_fp4_shuffled) gemm1_scales_fp4_shuffled = ( torch.stack(gemm1_scales_fp4_shuffled) .view(torch.float8_e4m3fn) .reshape(num_experts, 2 * intermediate_size, hidden_size // 16) ) gemm2_weights_fp4_shuffled = torch.stack(gemm2_weights_fp4_shuffled) gemm2_scales_fp4_shuffled = ( torch.stack(gemm2_scales_fp4_shuffled) .view(torch.float8_e4m3fn) .reshape(num_experts, hidden_size, intermediate_size // 16) ) return ( gemm1_weights_fp4_shuffled, gemm1_scales_fp4_shuffled, gemm2_weights_fp4_shuffled, gemm2_scales_fp4_shuffled, ) def flashinfer_trtllm_fp4_moe( layer: torch.nn.Module, x: torch.Tensor | tuple[torch.Tensor, torch.Tensor], router_logits: torch.Tensor, top_k: int, global_num_experts: int, num_expert_group: int | None, topk_group: int | None, custom_routing_function: object | None, e_score_correction_bias: torch.Tensor | None, ) -> torch.Tensor: """ Apply FlashInfer TensorRT-LLM FP4 MoE kernel. Args: layer: The MoE layer with weights and scales x: Input tensor router_logits: Router logits for expert selection top_k: Number of experts to select per token global_num_experts: Total number of experts across all ranks num_expert_group: Number of expert groups (for grouped routing) topk_group: Top-k within each group custom_routing_function: Custom routing function (e.g., Llama4) e_score_correction_bias: Optional routing bias correction Returns: Output tensor from the MoE layer """ import flashinfer from vllm.model_executor.models.llama4 import Llama4MoE # Quantize input to FP4 if isinstance(x, tuple): hidden_states_fp4, hidden_states_scale_linear_fp4 = x else: # hidden_states is the already quantized a1_gscale = layer.w13_input_scale_quant (hidden_states_fp4, hidden_states_scale_linear_fp4) = flashinfer.fp4_quantize( x, a1_gscale, is_sf_swizzled_layout=False, ) # Determine routing method type use_llama4_routing = custom_routing_function is Llama4MoE.custom_routing_function routing_method_type = layer.routing_method_type if use_llama4_routing: routing_method_type = flashinfer.RoutingMethodType.Llama4 # Prepare routing bias routing_bias = e_score_correction_bias if routing_bias is not None: routing_bias = routing_bias.to(torch.bfloat16) router_logits = ( router_logits.to(torch.float32) if routing_method_type == RoutingMethodType.DeepSeekV3 else router_logits ) # Call TRT-LLM FP4 block-scale MoE kernel out = flashinfer.fused_moe.trtllm_fp4_block_scale_moe( routing_logits=router_logits, routing_bias=routing_bias, hidden_states=hidden_states_fp4, hidden_states_scale=hidden_states_scale_linear_fp4.view( torch.float8_e4m3fn ).flatten(), gemm1_weights=layer.w13_weight.data, gemm1_weights_scale=layer.w13_weight_scale.data.view(torch.float8_e4m3fn), gemm1_bias=None, gemm1_alpha=None, gemm1_beta=None, gemm1_clamp_limit=None, gemm2_weights=layer.w2_weight.data, gemm2_weights_scale=layer.w2_weight_scale.data.view(torch.float8_e4m3fn), gemm2_bias=None, output1_scale_scalar=layer.g1_scale_c.data, output1_scale_gate_scalar=layer.g1_alphas.data, output2_scale_scalar=layer.g2_alphas.data, num_experts=global_num_experts, top_k=top_k, n_group=num_expert_group if num_expert_group is not None else 0, topk_group=topk_group if topk_group is not None else 0, intermediate_size=layer.intermediate_size_per_partition, local_expert_offset=layer.ep_rank * layer.local_num_experts, local_num_experts=layer.local_num_experts, routed_scaling_factor=None, tile_tokens_dim=None, routing_method_type=routing_method_type, do_finalize=True, )[0] return out def flashinfer_trtllm_fp4_routed_moe( layer: torch.nn.Module, x: torch.Tensor, topk_ids: torch.Tensor, topk_weights: torch.Tensor, top_k: int, global_num_experts: int, ) -> torch.Tensor: """ Apply FlashInfer TensorRT-LLM FP4 MoE kernel. Uses packed input top k expert indices and scores rather than computing top k expert indices from scores. Args: layer: The MoE layer with weights and scales x: Input tensor topk_ids: Ids of selected experts top_k: Number of experts to select per token global_num_experts: Total number of experts across all ranks Returns: Output tensor from the MoE layer """ import flashinfer # Pack top k ids and expert weights into a single int32 tensor, as # required by TRT-LLM packed_tensor = (topk_ids.to(torch.int32) << 16) | topk_weights.to( torch.bfloat16 ).view(torch.int16) if isinstance(x, tuple): # Hidden_states is the already quantized hidden_states_fp4, hidden_states_scale_linear_fp4 = x else: # Quantize input to FP4 a1_gscale = layer.w13_input_scale_quant (hidden_states_fp4, hidden_states_scale_linear_fp4) = flashinfer.fp4_quantize( x, a1_gscale, is_sf_swizzled_layout=False, ) # Call TRT-LLM FP4 block-scale MoE kernel out = flashinfer.fused_moe.trtllm_fp4_block_scale_routed_moe( topk_ids=packed_tensor, routing_bias=None, hidden_states=hidden_states_fp4, hidden_states_scale=hidden_states_scale_linear_fp4.view( torch.float8_e4m3fn ).flatten(), gemm1_weights=layer.w13_weight.data, gemm1_weights_scale=layer.w13_weight_scale.data.view(torch.float8_e4m3fn), gemm1_bias=None, gemm1_alpha=None, gemm1_beta=None, gemm1_clamp_limit=None, gemm2_weights=layer.w2_weight.data, gemm2_weights_scale=layer.w2_weight_scale.data.view(torch.float8_e4m3fn), gemm2_bias=None, output1_scale_scalar=layer.g1_scale_c.data, output1_scale_gate_scalar=layer.g1_alphas.data, output2_scale_scalar=layer.g2_alphas.data, num_experts=global_num_experts, top_k=top_k, n_group=0, topk_group=0, intermediate_size=layer.intermediate_size_per_partition, local_expert_offset=layer.ep_rank * layer.local_num_experts, local_num_experts=layer.local_num_experts, routed_scaling_factor=None, tile_tokens_dim=None, routing_method_type=1, do_finalize=True, )[0] return out
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/petit_utils.py
vllm/model_executor/layers/quantization/utils/petit_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from typing import TYPE_CHECKING, Optional import torch # TYPE_CHECKING is used for static type analysis to prevent circular imports. if TYPE_CHECKING: from types import ModuleType # 1. Create a global variable as a placeholder for the module _petit_kernel: Optional["ModuleType"] = None _PETIT_INSTALL_MSG = ( "Petit is not installed. Please install it with `pip install petit-kernel`." ) def _import_petit_kernel() -> "ModuleType": """ A helper function to handle the lazy import. The first time this function is called, it will import the petit_kernel library and store it in the global _petit_kernel variable. Subsequent calls will return the already-loaded module directly. """ global _petit_kernel if _petit_kernel is not None: return _petit_kernel try: import petit_kernel _petit_kernel = petit_kernel return _petit_kernel except ImportError: # The 'from None' syntax prevents chaining the original ImportError, # making the traceback cleaner. raise ImportError(_PETIT_INSTALL_MSG) from None # The _require_petit function can now be a simple alias for consistency. _require_petit = _import_petit_kernel def _check_petit_nvfp4_supported( quant_method: str, group_size: int | None ) -> tuple[bool, str | None]: if quant_method != "NVFP4": return ( False, ( "Petit currently only supports: NVFP4 quantizations in sglang. " "Please check the `hf_quant_config.json` file for your model's " "quant configuration." ), ) if group_size is not None and group_size != 16: return ( False, "Petit currently only supports: group_size=16 quantizations.", ) return (True, None) def verify_petit_nvfp4_supported(quant_method: str, group_size: int | None) -> None: supported, error_msg = _check_petit_nvfp4_supported(quant_method, group_size) if not supported: assert error_msg is not None raise ValueError(error_msg) def prepare_nvfp4_layer_for_petit(layer: torch.nn.Module) -> None: # 2. Call _import_petit_kernel() to trigger (or get) the import. petit_kernel = _import_petit_kernel() # Repack weights to petit format part_size_n = layer.output_size_per_partition part_size_k = layer.input_size_per_partition qweight = layer.weight.view(torch.int32).contiguous() # 3. Call functions through the imported module variable. petit_qweight = petit_kernel.repack_nvfp4( qweight, size_n=part_size_n, size_k=part_size_k ) layer.weight = torch.nn.Parameter(petit_qweight, requires_grad=False) # Permute scales weight_scale = petit_kernel.process_nvfp4_scales( scales=layer.weight_scale, size_k=part_size_k, size_n=part_size_n ) layer.weight_scale = torch.nn.Parameter(weight_scale, requires_grad=False) def apply_petit_nvfp4_linear( input: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, weight_scale_2: torch.Tensor, size_n: int, size_k: int, bias: torch.Tensor | None = None, ) -> torch.Tensor: # Trigger (or get) the import here as well. petit_kernel = _import_petit_kernel() reshaped_x = input.reshape(-1, input.shape[-1]) out_shape = input.shape[:-1] + (size_n,) # TODO: Use auto-tuning to find the performant solution_id # Call the function via the module variable. output = petit_kernel.mul_nvfp4_a16( a=reshaped_x, b=weight, s=weight_scale, global_scale=weight_scale_2, size_m=reshaped_x.size(0), size_n=size_n, size_k=size_k, solution_id=-1, ) if bias is not None: output.add_(bias) # In-place add return output.reshape(out_shape)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/layer_utils.py
vllm/model_executor/layers/quantization/utils/layer_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch def update_tensor_inplace(dst: torch.Tensor, src: torch.Tensor): assert dst.dtype == src.dtype, "Tensors must have the same dtype" # update tensor shape and stride dst.as_strided_(src.shape, src.stride()) # If not the same underlying storage move tensor data if dst.data_ptr() != src.data_ptr(): dst.copy_(src) del src # Newly generated tensors need to replace existing tensors that are # already registered as parameters by vLLM (and won't be freed) def replace_parameter( mod: torch.nn.Module, name: str, new: torch.Tensor | torch.nn.Parameter ) -> None: old = getattr(mod, name) if ( type(old) is type(new) and old.dtype == new.dtype and old.untyped_storage().nbytes() == new.untyped_storage().nbytes() ): # If we can just update in-place to avoid re-registering # can be faster if the underlying storage is the same update_tensor_inplace(old, new) else: # Fallback re-register parameter, convert to Parameter if necessary # this not only ensures we don't register a tensor as a parameter, but # also ensures that all parameter subclasses get re-registered as # parameters for `torch.compile` compatibility if not isinstance(new, torch.nn.Parameter): new = torch.nn.Parameter(new, requires_grad=False) mod.register_parameter(name, torch.nn.Parameter(new, requires_grad=False))
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/nvfp4_emulation_utils.py
vllm/model_executor/layers/quantization/utils/nvfp4_emulation_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm.scalar_type import scalar_types __all__ = [ "break_fp4_bytes", "dequantize_to_dtype", "ref_nvfp4_quant", ] FLOAT4_E2M1_MAX = scalar_types.float4_e2m1f.max() kE2M1ToFloat = torch.tensor( [0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0], dtype=torch.float32 ) def break_fp4_bytes(a, dtype): assert a.dtype == torch.uint8 m, n = a.shape # Vectorized nibble processing a_flat = a.flatten() high = (a_flat & 0xF0) >> 4 # Upper nibbles low = a_flat & 0x0F # Lower nibbles # Combine nibbles for batch processing combined = torch.stack((low, high), dim=1).flatten() # Vectorized sign and magnitude extraction signs = (combined & 0x08).to(torch.bool) # Sign bits abs_vals = (combined & 0x07).to(torch.long) # Device-aware lookup and sign application kE2M1 = kE2M1ToFloat.to(device=a.device) values = kE2M1[abs_vals] * torch.where(signs, -1.0, 1.0) # Reshape to final form return values.reshape(m, n * 2).to(dtype=dtype) def convert_swizzled_to_linear(a_sf_swizzled: torch.Tensor, m, k, block_size): m_tiles = (m + 128 - 1) // 128 f = block_size * 4 k_tiles = (k + f - 1) // f tmp = torch.reshape(a_sf_swizzled, (1, m_tiles, k_tiles, 32, 4, 4)) tmp = torch.permute(tmp, (0, 1, 4, 3, 2, 5)) out = tmp.reshape(m_tiles * 128, k_tiles * f // block_size) return out[0:m, 0:k] def dequantize_to_dtype( tensor_fp4, tensor_sf, global_scale, dtype, device, block_size=16 ): """Dequantize the fp4 tensor back to high precision.""" # Two fp4 values are packed into one uint8. assert tensor_fp4.dtype == torch.uint8 m, packed_k = tensor_fp4.shape k = packed_k * 2 tensor_f32 = break_fp4_bytes(tensor_fp4, torch.float32) tensor_f32 = tensor_f32.reshape(m, k // block_size, block_size) tensor_sf = tensor_sf.view(torch.float8_e4m3fn) tensor_sf = convert_swizzled_to_linear(tensor_sf, m, k, block_size) tensor_sf_dtype = tensor_sf.to(torch.float32) / global_scale # scale the tensor out = (tensor_f32 * tensor_sf_dtype.unsqueeze(-1)).reshape(m, k) return out.to(dtype) def get_reciprocal(x): if isinstance(x, torch.Tensor): return torch.where(x == 0, torch.tensor(0.0, dtype=x.dtype), 1.0 / x) elif isinstance(x, (float, int)): return 0.0 if x == 0 else 1.0 / x else: raise TypeError("Input must be a float, int, or a torch.Tensor.") def cast_to_fp4(x): sign = torch.sign(x) x = torch.abs(x) x[(x >= 0.0) & (x <= 0.25)] = 0.0 x[(x > 0.25) & (x < 0.75)] = 0.5 x[(x >= 0.75) & (x <= 1.25)] = 1.0 x[(x > 1.25) & (x < 1.75)] = 1.5 x[(x >= 1.75) & (x <= 2.5)] = 2.0 x[(x > 2.5) & (x < 3.5)] = 3.0 x[(x >= 3.5) & (x <= 5.0)] = 4.0 x[x > 5.0] = 6.0 return x * sign def ref_nvfp4_quant(x, global_scale, block_size): assert global_scale.dtype == torch.float32 assert x.ndim == 2 m, n = x.shape x = torch.reshape(x, (m, n // block_size, block_size)) vec_max = torch.max(torch.abs(x), dim=-1, keepdim=True)[0].to(torch.float32) scale = global_scale * (vec_max * get_reciprocal(FLOAT4_E2M1_MAX)) scale = torch.clamp(scale, max=448, min=-448) scale = scale.to(torch.float8_e4m3fn).to(torch.float32) output_scale = get_reciprocal(scale * get_reciprocal(global_scale)) scaled_x = x.to(torch.float32) * output_scale clipped_x = torch.clamp(scaled_x, -6.0, 6.0).reshape(m, n) # both outputs are float32 return cast_to_fp4(clipped_x), scale.squeeze(-1) def run_nvfp4_emulations( x: torch.Tensor, input_global_scale: torch.Tensor, weight: torch.Tensor, weight_scale_swizzled: torch.Tensor, weight_global_scale: torch.Tensor, ): group_size = 16 x_m, x_k = x.shape output_dtype = x.dtype # quantize input to (FP4 and interleaved block scale) x_fp4, x_blockscale = ref_nvfp4_quant(x, input_global_scale, group_size) # dequantize input x_fp4 = x_fp4.reshape(x_m, x_k // group_size, group_size) x_blockscale = x_blockscale.unsqueeze(-1) / input_global_scale x_dq = (x_fp4 * x_blockscale).reshape(x_m, x_k).to(output_dtype) del x_fp4, x_blockscale # dequantize weight w_fp4 = weight.data.view(torch.uint8) w_dq = dequantize_to_dtype( w_fp4, weight_scale_swizzled.data, weight_global_scale, output_dtype, x.device, group_size, ) # matmul out = torch.matmul(x_dq, w_dq.t()) del w_dq, x_dq return out
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/nvfp4_moe_support.py
vllm/model_executor/layers/quantization/utils/nvfp4_moe_support.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from dataclasses import dataclass import vllm.envs as envs from vllm.logger import init_logger from vllm.model_executor.layers.quantization.utils.flashinfer_fp4_moe import ( is_flashinfer_fp4_cutedsl_moe_available, is_flashinfer_fp4_cutlass_moe_available, ) from vllm.model_executor.layers.quantization.utils.marlin_utils_fp4 import ( is_fp4_marlin_supported, ) from vllm.model_executor.layers.quantization.utils.quant_utils import ( cutlass_fp4_supported, ) __all__ = ["detect_nvfp4_moe_support", "NvFp4Support"] _logger = init_logger(__name__) @dataclass(frozen=True) class NvFp4Support: """Result container for NV-FP4 capability probing.""" cutlass_supported: bool allow_flashinfer: bool use_marlin: bool def detect_nvfp4_moe_support(class_name: str = "") -> NvFp4Support: """Detect platform support for NV-FP4 fused-MoE path""" cutlass_supported = cutlass_fp4_supported() allow_flashinfer = cutlass_supported and ( is_flashinfer_fp4_cutlass_moe_available() or is_flashinfer_fp4_cutedsl_moe_available() ) if allow_flashinfer: _logger.info_once( "Using FlashInfer kernels for %s.", class_name or "NVFP4 path" ) else: if envs.VLLM_USE_FLASHINFER_MOE_FP4: _logger.warning_once( "FlashInfer kernels unavailable for %s on current platform.", class_name or "NVFP4 path", ) use_marlin = False if not cutlass_supported: if is_fp4_marlin_supported(): use_marlin = True _logger.info_once("Falling back to Marlin FP4 MoE kernel.") else: raise ValueError( "Current platform does not support NVFP4 quantization. " "Please use Blackwell GPUs or enable FlashInfer." ) return NvFp4Support( cutlass_supported=cutlass_supported, allow_flashinfer=allow_flashinfer, use_marlin=use_marlin, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/marlin_utils_fp4.py
vllm/model_executor/layers/quantization/utils/marlin_utils_fp4.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch import vllm._custom_ops as ops from vllm.logger import init_logger from vllm.model_executor.layers.quantization.utils.marlin_utils import ( USE_FP32_REDUCE_DEFAULT, marlin_make_workspace_new, marlin_permute_bias, marlin_permute_scales, marlin_quant_input, should_use_atomic_add_reduce, ) from vllm.platforms import current_platform from vllm.scalar_type import scalar_types FP4_MARLIN_SUPPORTED_GROUP_SIZES = [16] logger = init_logger(__name__) def is_fp4_marlin_supported(): return current_platform.has_device_capability(75) def nvfp4_marlin_process_scales(marlin_scales): if not (marlin_scales >= 0).all(): logger.warning_once( "NVFP4 Marlin assumes the scales to be >=0, but has encountered " "negative scales. Accuracy will likely be degraded. This is " "because it changes the scales from FP8-S1E4M3 to a special " "FP8-S0E5M3 format to speedup the dequantization." ) # convert to half first, we would convert to fp8 later marlin_scales = marlin_scales.to(torch.half) # fit the layout of fp8 dequantization marlin_scales = marlin_scales.view(-1, 4)[:, [0, 2, 1, 3]].view( marlin_scales.size(0), -1 ) # We assume that weight_scale (FP8-S1E4M3) is always greater # than or equal to 0. So we can convert # (weight_scale * (2 ** 7) to a special FP8-S0E5M3 format. # After multiplying by 2 ** 7, the top bit of FP8-S0E5M3 would always be 1 # when weight_scale > 0. This allows us to have an exponent bias # closer to zero after dequantization. marlin_scales = (marlin_scales * (2**7)).view(torch.int16) << 1 marlin_scales = marlin_scales.view(torch.float8_e4m3fn) marlin_scales = marlin_scales[:, 1::2].contiguous() return marlin_scales def mxfp4_marlin_process_scales(marlin_scales, input_dtype=None): # fit the layout of fp8 dequantization if input_dtype is None or input_dtype.itemsize == 2: marlin_scales = marlin_scales.view(-1, 4)[:, [0, 2, 1, 3]].view( marlin_scales.size(0), -1 ) marlin_scales = marlin_scales.to(torch.float8_e8m0fnu) if input_dtype == torch.float8_e4m3fn: marlin_scales = marlin_scales.view(torch.uint8) assert marlin_scales.max() <= 249 # exponent_bias (fp4->fp8) = 2 ** 3 - 2 ** 1 = 6 marlin_scales = marlin_scales + 6 marlin_scales = marlin_scales.view(torch.float8_e8m0fnu) return marlin_scales def nvfp4_marlin_process_global_scale(global_scale): assert global_scale.dtype in [torch.half, torch.bfloat16] fp4_exponent = 2 if global_scale.dtype == torch.half: target_exponent = 5 elif global_scale.dtype == torch.bfloat16: target_exponent = 8 # exponent_bias_fp16 = 2 ** 4 - 2 ** 1 = 14 # exponent_bias_bf16 = 2 ** 7 - 2 ** 1 = 126 exponent_bias = 2 ** (target_exponent - 1) - 2 ** (fp4_exponent - 1) return global_scale * (2.0 ** (exponent_bias - 7)) def apply_fp4_marlin_linear( input: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, weight_scale_2: torch.Tensor | None, workspace: torch.Tensor, size_n: int, size_k: int, bias: torch.Tensor | None = None, input_dtype: torch.dtype | None = None, use_fp32_reduce: bool = USE_FP32_REDUCE_DEFAULT, ) -> torch.Tensor: # For GPUs that lack FP4 hardware support, we can leverage the # Marlin kernel for fast weight-only FP4 quantization reshaped_x = input.reshape(-1, input.shape[-1]) out_shape = input.shape[:-1] + (size_n,) use_atomic_add = should_use_atomic_add_reduce( m=reshaped_x.size(0), n=size_n, k=size_k, device=input.device, dtype=input.dtype ) inputs = reshaped_x a_scales = None is_nvfp4 = weight_scale_2 is not None if input_dtype is not None and input_dtype.itemsize == 1: if is_nvfp4: raise RuntimeError("NVFP4 weight + INT8/FP8 activation is not supported.") elif input_dtype != torch.float8_e4m3fn: raise RuntimeError("MXFP4 weight + INT8 activation is not supported.") inputs, a_scales = marlin_quant_input(inputs, torch.float8_e4m3fn) output = ops.gptq_marlin_gemm( a=inputs, c=None, b_q_weight=weight, b_bias=bias, b_scales=weight_scale, a_scales=a_scales, global_scale=weight_scale_2, b_zeros=None, g_idx=None, perm=None, workspace=workspace, b_q_type=scalar_types.float4_e2m1f, size_m=reshaped_x.size(0), size_n=size_n, size_k=size_k, use_atomic_add=use_atomic_add, use_fp32_reduce=use_fp32_reduce, ) return output.reshape(out_shape) def prepare_fp4_layer_for_marlin( layer: torch.nn.Module, input_dtype: torch.dtype | None = None ) -> None: logger.warning_once( "Your GPU does not have native support for FP4 computation but " "FP4 quantization is being used. Weight-only FP4 compression will " "be used leveraging the Marlin kernel. This may degrade " "performance for compute-heavy workloads." ) is_nvfp4 = hasattr(layer, "weight_scale_2") if input_dtype is not None and input_dtype.itemsize == 1: if is_nvfp4: raise RuntimeError("NVFP4 weight + INT8/FP8 activation is not supported.") elif input_dtype != torch.float8_e4m3fn: raise RuntimeError("MXFP4 weight + INT8 activation is not supported.") group_size = 16 if is_nvfp4 else 32 part_size_n = layer.output_size_per_partition part_size_k = layer.input_size_per_partition param_dtype = layer.params_dtype assert layer.weight.shape == (part_size_n, part_size_k // 2) device = layer.weight.device # WORKSPACE layer.workspace = marlin_make_workspace_new(device) # WEIGHT # Repack weights to marlin format perm = torch.empty(0, dtype=torch.int, device=device) qweight = layer.weight.view(torch.int32).T.contiguous() is_a_8bit = input_dtype is not None and input_dtype.itemsize == 1 marlin_qweight = ops.gptq_marlin_repack( b_q_weight=qweight, perm=perm, size_k=part_size_k, size_n=part_size_n, num_bits=4, is_a_8bit=is_a_8bit, ) layer.weight = torch.nn.Parameter(marlin_qweight, requires_grad=False) # WEIGHT SCALES # Permute scales weight_scale = layer.weight_scale.T.contiguous() if not is_nvfp4: weight_scale = weight_scale.view(torch.float8_e8m0fnu) weight_scale = weight_scale.to(param_dtype) weight_scale = marlin_permute_scales( s=weight_scale, size_k=part_size_k, size_n=part_size_n, group_size=group_size, is_a_8bit=is_a_8bit, ) if is_nvfp4: weight_scale = nvfp4_marlin_process_scales(weight_scale) layer.weight_scale = torch.nn.Parameter(weight_scale, requires_grad=False) weight_scale_2 = layer.weight_scale_2.to(param_dtype) weight_scale_2 = nvfp4_marlin_process_global_scale(weight_scale_2) layer.weight_scale_2 = torch.nn.Parameter(weight_scale_2, requires_grad=False) else: weight_scale = mxfp4_marlin_process_scales( weight_scale, input_dtype=input_dtype ) layer.weight_scale = torch.nn.Parameter(weight_scale, requires_grad=False) if hasattr(layer, "bias") and layer.bias is not None: assert layer.bias.shape == (part_size_n,) bias = marlin_permute_bias(layer.bias) layer.bias = torch.nn.Parameter(bias, requires_grad=False) return def prepare_moe_fp4_layer_for_marlin( layer: torch.nn.Module, input_dtype: torch.dtype | None = None ) -> None: logger.warning_once( "Your GPU does not have native support for FP4 computation but " "FP4 quantization is being used. Weight-only FP4 compression will " "be used leveraging the Marlin kernel. This may degrade " "performance for compute-heavy workloads." ) is_nvfp4 = hasattr(layer, "w13_weight_scale_2") if input_dtype is not None and input_dtype.itemsize == 1: if is_nvfp4: raise RuntimeError("NVFP4 weight + INT8/FP8 activation is not supported.") elif input_dtype != torch.float8_e4m3fn: raise RuntimeError("MXFP4 weight + INT8 activation is not supported.") group_size = 16 if is_nvfp4 else 32 e = layer.num_experts k = layer.hidden_size n = layer.intermediate_size_per_partition # WORKSPACE device = layer.w13_weight.device param_dtype = layer.params_dtype layer.workspace = marlin_make_workspace_new(device, 4) perm = torch.empty(0, dtype=torch.int, device=device) is_a_8bit = input_dtype is not None and input_dtype.itemsize == 1 # WEIGHT # Repack weights to marlin format for name in ["w13_weight", "w2_weight"]: weight = getattr(layer, name) tensor_list = [] if "w13" in name: size_n, size_k = n * 2, k else: size_n, size_k = k, n assert weight.shape == (e, size_n, size_k // 2) for i in range(e): qweight = weight[i].view(torch.int32).T.contiguous() marlin_qweight = ops.gptq_marlin_repack( b_q_weight=qweight, perm=perm, size_k=size_k, size_n=size_n, num_bits=4, is_a_8bit=is_a_8bit, ) tensor_list.append(marlin_qweight) weight = torch.cat([x.unsqueeze(0) for x in tensor_list], 0) weight = torch.nn.Parameter(weight, requires_grad=False) setattr(layer, name, weight) # WEIGHT SCALES # Permute scales for name in ["w13", "w2"]: scales = getattr(layer, name + "_weight_scale") if not is_nvfp4: scales = scales.view(torch.float8_e8m0fnu) scales = scales.to(param_dtype) if is_nvfp4: global_scale = getattr(layer, name + "_weight_scale_2").to(param_dtype) tensor_list = [] if "w13" in name: size_n, size_k = n * 2, k else: size_n, size_k = k, n for i in range(e): scale = scales[i].T marlin_scales = marlin_permute_scales( s=scale, size_k=size_k, size_n=size_n, group_size=group_size, is_a_8bit=is_a_8bit, ) if is_nvfp4: marlin_scales = nvfp4_marlin_process_scales(marlin_scales) else: marlin_scales = mxfp4_marlin_process_scales( marlin_scales, input_dtype=input_dtype ) tensor_list.append(marlin_scales) scales = torch.cat([x.unsqueeze(0) for x in tensor_list], 0) scales = torch.nn.Parameter(scales, requires_grad=False) setattr(layer, name + "_weight_scale", scales) if is_nvfp4: global_scale = nvfp4_marlin_process_global_scale(global_scale) global_scale = torch.nn.Parameter(global_scale, requires_grad=False) setattr(layer, name + "_weight_scale_2", global_scale) # BIAS # Permute bias for name in ["w13_bias", "w2_bias"]: if not hasattr(layer, name): continue bias = getattr(layer, name).to(param_dtype) tensor_list = [] for i in range(e): expert_bias = bias[i] tensor_list.append(marlin_permute_bias(expert_bias)) bias = torch.cat([x.unsqueeze(0) for x in tensor_list], 0) bias = torch.nn.Parameter(bias, requires_grad=False) setattr(layer, name, bias) def rand_marlin_weight_nvfp4_like(weight, group_size, input_dtype=None): is_a_8bit = input_dtype is not None and input_dtype.itemsize == 1 assert not is_a_8bit, "NVFP4 weight + INT8/FP8 activation is not supported." assert group_size > 0 size_n, size_k = weight.shape device = weight.device scales = weight.view(size_n, -1, group_size).abs().max(-1)[0] / 6 global_scale = scales.max() / 448 scales = (scales / global_scale).to(torch.float8_e4m3fn) fp4_weight = torch.randint( 0, 256, (size_n, size_k // 2), dtype=torch.uint8, device=weight.device ) fp4_weight_part_1 = (fp4_weight & 0b10000000) | ((fp4_weight & 0b01110000) >> 2) fp4_weight_part_1 = fp4_weight_part_1.view(torch.float8_e4m3fn) fp4_weight_part_1 = fp4_weight_part_1.to(weight.dtype) * (2**6) fp4_weight2 = fp4_weight << 4 fp4_weight_part_2 = (fp4_weight2 & 0b10000000) | ((fp4_weight2 & 0b01110000) >> 2) fp4_weight_part_2 = fp4_weight_part_2.view(torch.float8_e4m3fn) fp4_weight_part_2 = fp4_weight_part_2.to(weight.dtype) * (2**6) weight_ref = torch.cat( [fp4_weight_part_2.unsqueeze(2), fp4_weight_part_1.unsqueeze(2)], 2 ).view(size_n, size_k) weight_ref = ( weight_ref * global_scale.to(weight.dtype) * scales.repeat_interleave(group_size, 1).to(weight.dtype) ) marlin_qweight = ops.gptq_marlin_repack( b_q_weight=fp4_weight.view(torch.int32).T.contiguous(), perm=torch.empty(0, dtype=torch.int, device=device), size_k=size_k, size_n=size_n, num_bits=4, is_a_8bit=is_a_8bit, ) marlin_scales = marlin_permute_scales( s=scales.T.to(weight.dtype), size_k=size_k, size_n=size_n, group_size=group_size, is_a_8bit=is_a_8bit, ) marlin_scales = nvfp4_marlin_process_scales(marlin_scales) global_scale = nvfp4_marlin_process_global_scale(global_scale) return weight_ref.T, marlin_qweight, marlin_scales, global_scale def rand_marlin_weight_mxfp4_like(weight, group_size, input_dtype=None): is_a_8bit = input_dtype is not None and input_dtype.itemsize == 1 if is_a_8bit: assert input_dtype == torch.float8_e4m3fn, ( "MXFP4 weight + INT8 activation is not supported." ) assert group_size > 0 size_n, size_k = weight.shape device = weight.device scales = torch.randint( 110, 120, (size_n, size_k // group_size), dtype=torch.uint8, device=weight.device, ) scales = scales.view(torch.float8_e8m0fnu) fp4_weight = torch.randint( 0, 256, (size_n, size_k // 2), dtype=torch.uint8, device=weight.device ) fp4_weight_part_1 = (fp4_weight & 0b10000000) | ((fp4_weight & 0b01110000) >> 2) fp4_weight_part_1 = fp4_weight_part_1.view(torch.float8_e4m3fn) fp4_weight_part_1 = fp4_weight_part_1.to(weight.dtype) * (2**6) fp4_weight2 = fp4_weight << 4 fp4_weight_part_2 = (fp4_weight2 & 0b10000000) | ((fp4_weight2 & 0b01110000) >> 2) fp4_weight_part_2 = fp4_weight_part_2.view(torch.float8_e4m3fn) fp4_weight_part_2 = fp4_weight_part_2.to(weight.dtype) * (2**6) weight_ref = torch.cat( [fp4_weight_part_2.unsqueeze(2), fp4_weight_part_1.unsqueeze(2)], 2 ).view(size_n, size_k) weight_ref = weight_ref * scales.repeat_interleave(group_size, 1).to(weight.dtype) perm = torch.empty(0, dtype=torch.int, device=device) fp4_weight = fp4_weight.view(torch.int32).T.contiguous() marlin_qweight = ops.gptq_marlin_repack( b_q_weight=fp4_weight, perm=perm, size_k=size_k, size_n=size_n, num_bits=4, is_a_8bit=is_a_8bit, ) marlin_scales = marlin_permute_scales( s=scales.T.to(weight.dtype), size_k=size_k, size_n=size_n, group_size=group_size, is_a_8bit=is_a_8bit, ) marlin_scales = mxfp4_marlin_process_scales(marlin_scales, input_dtype=input_dtype) return weight_ref.T, marlin_qweight, marlin_scales.to(torch.float8_e8m0fnu)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/ocp_mx_utils.py
vllm/model_executor/layers/quantization/utils/ocp_mx_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from enum import Enum from vllm.logger import init_logger logger = init_logger(__name__) OCP_MX_BLOCK_SIZE = 32 OCP_MX_DTYPES = { "mxfp4", "mxfp6_e3m2", "mxfp6_e2m3", "mxfp8_e4m3", "mxfp8_e5m2", "mxint8", } SUPPORTED_OCP_MX_DTYPES = {"mxfp4", "mxfp6_e3m2", "mxfp6_e2m3"} class OCP_MX_Scheme(str, Enum): w_mxfp4_a_mxfp4 = "w_mxfp4_a_mxfp4" w_mxfp4_a_mxfp6_e3m2 = "w_mxfp4_a_mxfp6_e3m2" w_mxfp4_a_mxfp6_e2m3 = "w_mxfp4_a_mxfp6_e2m3" w_mxfp6_e3m2_a_mxfp6_e3m2 = "w_mxfp6_e3m2_a_mxfp6_e3m2" w_mxfp6_e2m3_a_mxfp6_e2m3 = "w_mxfp6_e2m3_a_mxfp6_e2m3" @classmethod def from_quant_dtype(cls, input_dtype: str | None, weight_dtype: str | None): if input_dtype not in OCP_MX_DTYPES or weight_dtype not in OCP_MX_DTYPES: return None elif input_dtype == "mxfp4" and weight_dtype == "mxfp4": return cls.w_mxfp4_a_mxfp4 elif input_dtype == "mxfp6_e3m2" and weight_dtype == "mxfp4": return cls.w_mxfp4_a_mxfp6_e3m2 elif input_dtype == "mxfp6_e2m3" and weight_dtype == "mxfp4": return cls.w_mxfp4_a_mxfp6_e2m3 elif input_dtype == "mxfp6_e3m2" and weight_dtype == "mxfp6_e3m2": return cls.w_mxfp6_e3m2_a_mxfp6_e3m2 elif input_dtype == "mxfp6_e2m3" and weight_dtype == "mxfp6_e2m3": return cls.w_mxfp6_e2m3_a_mxfp6_e2m3 else: logger.warning( "input_dtype='%s' and" " weight_dtype='%s' is not supported " "in OCP_MX_Scheme at the moment.", input_dtype, weight_dtype, ) return None
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/marlin_utils_fp8.py
vllm/model_executor/layers/quantization/utils/marlin_utils_fp8.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch import vllm._custom_ops as ops from vllm.logger import init_logger from vllm.model_executor.layers.quantization.utils.marlin_utils import ( USE_FP32_REDUCE_DEFAULT, marlin_make_workspace_new, marlin_permute_bias, marlin_permute_scales, should_use_atomic_add_reduce, ) from vllm.model_executor.utils import replace_parameter from vllm.platforms import current_platform from vllm.scalar_type import scalar_types logger = init_logger(__name__) def is_fp8_marlin_supported(): return current_platform.has_device_capability(75) def fp8_fused_exponent_bias_into_scales(scales): fp8_exponent = 4 if scales.dtype == torch.half: target_exponent = 5 elif scales.dtype == torch.bfloat16: target_exponent = 8 # exponent_bias_fp16 = 2 ** 4 - 2 ** 3 = 8 # exponent_bias_bf16 = 2 ** 7 - 2 ** 3 = 120 exponent_bias = 2 ** (target_exponent - 1) - 2 ** (fp8_exponent - 1) s = torch.ones_like(scales) * 2 s = s**exponent_bias return scales * s def apply_fp8_marlin_linear( input: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, workspace: torch.Tensor, size_n: int, size_k: int, bias: torch.Tensor | None, input_dtype: torch.dtype | None = None, use_fp32_reduce: bool = USE_FP32_REDUCE_DEFAULT, ) -> torch.Tensor: # For GPUs that lack FP8 hardware support, we can leverage the # Marlin kernel for fast weight-only FP8 quantization reshaped_x = input.reshape(-1, input.shape[-1]) out_shape = input.shape[:-1] + (size_n,) use_atomic_add = should_use_atomic_add_reduce( m=reshaped_x.size(0), n=size_n, k=size_k, device=input.device, dtype=input.dtype ) inputs = reshaped_x a_scales = None if input_dtype is not None and input_dtype.itemsize == 1: # inputs, a_scales = marlin_quant_input(inputs, torch.float8_e4m3fn) raise RuntimeError("Marlin W8A8 is not supported.") output = ops.gptq_marlin_gemm( a=inputs, c=None, b_q_weight=weight, b_bias=bias, b_scales=weight_scale, a_scales=a_scales, global_scale=None, b_zeros=None, g_idx=None, perm=None, workspace=workspace, b_q_type=scalar_types.float8_e4m3fn, size_m=reshaped_x.size(0), size_n=size_n, size_k=size_k, use_atomic_add=use_atomic_add, use_fp32_reduce=use_fp32_reduce, ) return output.reshape(out_shape) def prepare_fp8_layer_for_marlin( layer: torch.nn.Module, size_k_first: bool = True, input_dtype: torch.dtype | None = None, ) -> None: logger.warning_once( "Your GPU does not have native support for FP8 computation but " "FP8 quantization is being used. Weight-only FP8 compression will " "be used leveraging the Marlin kernel. This may degrade " "performance for compute-heavy workloads." ) if input_dtype is not None and input_dtype.itemsize == 1: raise RuntimeError("Marlin W8A8 is not supported.") part_size_n = layer.output_size_per_partition part_size_k = layer.input_size_per_partition weight_block_size = getattr(layer, "weight_block_size", None) if size_k_first: assert layer.weight.shape == (part_size_k, part_size_n) else: assert layer.weight.shape == (part_size_n, part_size_k) device = layer.weight.device # WORKSPACE layer.workspace = marlin_make_workspace_new(device) # WEIGHT # Repack weights to marlin format perm = torch.empty(0, dtype=torch.int, device=device) qweight = pack_fp8_to_int32(layer.weight, size_k_first) if not size_k_first: qweight = qweight.T.contiguous() marlin_qweight = ops.gptq_marlin_repack( b_q_weight=qweight, perm=perm, size_k=part_size_k, size_n=part_size_n, num_bits=8, ) replace_parameter(layer, "weight", marlin_qweight) # WEIGHT SCALES # Permute scales if "weight_scale" in dir(layer): scales = layer.weight_scale.to(layer.orig_dtype) elif "weight_scale_inv" in dir(layer): scales = layer.weight_scale_inv.to(layer.orig_dtype) group_size = -1 if weight_block_size is None else weight_block_size[1] # marlin kernel only support channel-wise and group-wise quantization # we need to convert the scales if weight_block_size is None: logical_widths = getattr(layer, "logical_widths", []) if scales.nelement() == 1: # tensor-wise quantization -> channel-wise quantization # (1, 1) =>(repeat)=> (1, size_n) scales = scales.view(1, 1).repeat_interleave(part_size_n, 1) elif scales.nelement() == len(logical_widths): # tensor-wise quantization with logical_widths -> # channel-wise quantization assert sum(logical_widths) == part_size_n, ( f"Sum of logical_widths ({sum(logical_widths)}) must be equal " f"to part_size_n ({part_size_n})" ) lw_tensor = scales.new_tensor(logical_widths, dtype=torch.int64) scales = scales.view(1, -1).repeat_interleave(lw_tensor, dim=1) elif scales.nelement() > 1 and scales.nelement() != part_size_n: assert part_size_n % scales.nelement() == 0 s_size = scales.nelement() # tensor-wise quantization (for gate-up proj) # -> channel-wise quantization # (1, s_size) =>(repeat)=> (1, size_n) scales = scales.view(1, s_size) scales = scales.repeat_interleave(part_size_n // s_size, 1) else: # channel-wise quantization # (1, size_n) scales = scales.view(1, part_size_n) else: # block-wise quantization -> group-wise quantization # (size_k // block_size[1], ceil(size_n / block_size[0])) # =>(repeat)=> (size_k // block_size[1], size_n) if not size_k_first: scales = scales.T.contiguous() block_n = weight_block_size[0] scales = scales.repeat_interleave(block_n, 1) # size_n may not divisible by block_size[0] scales = scales[:, :part_size_n] marlin_scales = marlin_permute_scales( s=scales, size_k=part_size_k, size_n=part_size_n, group_size=group_size ) if input_dtype != torch.float8_e4m3fn: marlin_scales = fp8_fused_exponent_bias_into_scales(marlin_scales) if hasattr(layer, "weight_scale"): replace_parameter(layer, "weight_scale", marlin_scales) elif hasattr(layer, "weight_scale_inv"): replace_parameter(layer, "weight_scale_inv", marlin_scales) if hasattr(layer, "bias") and layer.bias is not None: assert layer.bias.shape == (part_size_n,) bias = marlin_permute_bias(layer.bias) replace_parameter(layer, "bias", bias) def prepare_moe_fp8_layer_for_marlin( layer: torch.nn.Module, w13_weight: torch.Tensor, w2_weight: torch.Tensor, w13_weight_scale: torch.Tensor, w2_weight_scale: torch.Tensor, input_dtype: torch.dtype | None = None, ) -> tuple[ torch.Tensor, # workspace torch.Tensor, # w13_weight torch.Tensor, # w2_weight torch.Tensor, # w13_weight_scale torch.Tensor, # w2_weight_scale ]: logger.warning_once( "Your GPU does not have native support for FP8 computation but " "FP8 quantization is being used. Weight-only FP8 compression will " "be used leveraging the Marlin kernel. This may degrade " "performance for compute-heavy workloads." ) if input_dtype is not None and input_dtype.itemsize == 1: raise NotImplementedError("Marlin W8A8 is not supported.") e = layer.num_experts k = layer.hidden_size n = layer.intermediate_size_per_partition weight_block_size = getattr(layer, "weight_block_size", None) # WORKSPACE device = layer.w13_weight.device workspace = marlin_make_workspace_new(device, 4) perm = torch.empty(0, dtype=torch.int, device=device) # WEIGHT # Repack weights to marlin format def repack_weight(name: str, weight: torch.Tensor) -> torch.Tensor: tensor_list = [] if "w13" in name: size_n, size_k = n * 2, k else: size_n, size_k = k, n assert weight.shape == (e, size_n, size_k) for i in range(e): qweight = pack_fp8_to_int32(weight[i], size_k_first=False) qweight = qweight.T.contiguous() marlin_qweight = ops.gptq_marlin_repack( b_q_weight=qweight, perm=perm, size_k=size_k, size_n=size_n, num_bits=8 ) tensor_list.append(marlin_qweight) return torch.cat([x.unsqueeze(0) for x in tensor_list], 0) w13_weight = repack_weight("w13", w13_weight) w2_weight = repack_weight("w2", w2_weight) # WEIGHT SCALES # Permute scales group_size = -1 if weight_block_size is None else weight_block_size[1] def permute_scales(scales: torch.Tensor, name: str) -> torch.Tensor: scales = scales.to(layer.orig_dtype) tensor_list = [] if "w13" in name: size_n, size_k = n * 2, k else: size_n, size_k = k, n # marlin kernel only support channel-wise and group-wise quantization # we need to convert the scales if weight_block_size is None: if scales.nelement() == e: # tensor-wise quantization -> channel-wise quantization # (e, 1, 1) =>(repeat)=> (e, 1, size_n) scales = scales.view(e, 1, 1).repeat_interleave(size_n, 2) elif scales.nelement() > e and scales.nelement() != e * size_n: assert (e * size_n) % scales.nelement() == 0 s_size = scales.nelement() // e # tensor-wise quantization (for gate-up proj) # -> channel-wise quantization # (e, 1, s_size) =>(repeat)=> (e, 1, size_n) scales = scales.view(e, 1, s_size) scales = scales.repeat_interleave(size_n // s_size, 2) else: # channel-wise quantization # (e, 1, size_n) scales = scales.view(e, 1, size_n) else: # block-wise quantization -> group-wise quantization # (e, size_k // block_size[1], ceil(size_n / block_size[0])) # =>(repeat)=> (e, size_k // block_size[1], size_n) scales = scales.permute(0, 2, 1) block_n = weight_block_size[0] scales = scales.repeat_interleave(block_n, 2) # size_n may not divisible by block_size[0] scales = scales[..., :size_n].contiguous() for i in range(e): marlin_scales = marlin_permute_scales( s=scales[i], size_k=size_k, size_n=size_n, group_size=group_size ) tensor_list.append(marlin_scales) scales = torch.cat([x.unsqueeze(0) for x in tensor_list], 0) if input_dtype != torch.float8_e4m3fn: scales = fp8_fused_exponent_bias_into_scales(scales) return scales w13_weight_scale = permute_scales(w13_weight_scale, "w13") w2_weight_scale = permute_scales(w2_weight_scale, "w2") return ( workspace, w13_weight, w2_weight, w13_weight_scale, w2_weight_scale, ) def pack_fp8_to_int32( fp8_tensor: torch.Tensor, size_k_first: bool = True ) -> torch.Tensor: """ Repack FP8 weights to gptq format (packed int32 elements) """ assert fp8_tensor.dtype == torch.float8_e4m3fn assert fp8_tensor.ndim == 2 fp8_tensor = fp8_tensor.T if size_k_first else fp8_tensor fp8_tensor = fp8_tensor.contiguous() # fp8_tensor is contiguous and have shape (N, K) now # with `.view(torch.int32)`, it become (N, K // 4) int32_tensor = fp8_tensor.view(torch.int32) return int32_tensor.T.contiguous() if size_k_first else int32_tensor def marlin_quant_fp8_torch(weight, group_size, input_dtype=None): is_a_8bit = input_dtype is not None and input_dtype.itemsize == 1 if is_a_8bit: assert input_dtype == torch.float8_e4m3fn size_n, size_k = weight.shape device = weight.device if group_size != -1: scales = weight.view(size_n, -1, group_size).abs().max(-1)[0] / 448 repeated_scales = scales.repeat_interleave(group_size, 1) fp8_weight = (weight / repeated_scales).to(torch.float8_e4m3fn) weight_ref = fp8_weight.to(weight.dtype) * repeated_scales else: scales = weight.view(size_n, 1, group_size).abs().max(-1)[0] / 448 repeated_scales = scales.repeat_interleave(size_k, 1) fp8_weight = (weight / repeated_scales).to(torch.float8_e4m3fn) weight_ref = fp8_weight.to(weight.dtype) * repeated_scales packed_weight = pack_fp8_to_int32(fp8_weight, False).T.contiguous() perm = torch.empty(0, dtype=torch.int, device=device) marlin_qweight = ops.gptq_marlin_repack( b_q_weight=packed_weight, perm=perm, size_k=size_k, size_n=size_n, num_bits=8, is_a_8bit=is_a_8bit, ) marlin_scales = marlin_permute_scales( s=scales.T, size_k=size_k, size_n=size_n, group_size=group_size, is_a_8bit=is_a_8bit, ) marlin_scales = fp8_fused_exponent_bias_into_scales(marlin_scales) return weight_ref.T, marlin_qweight, marlin_scales
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/marlin_utils.py
vllm/model_executor/layers/quantization/utils/marlin_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import numpy import torch import vllm.envs as envs from vllm import _custom_ops as ops from vllm.logger import init_logger from vllm.model_executor.layers.linear import LinearBase from vllm.model_executor.layers.quantization.input_quant_fp8 import QuantFP8 from vllm.model_executor.layers.quantization.utils.int8_utils import ( per_token_quant_int8, ) from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape from vllm.platforms import current_platform from vllm.scalar_type import ScalarType, scalar_types from .quant_utils import pack_cols, unpack_cols logger = init_logger(__name__) GPTQ_MARLIN_TILE = 16 GPTQ_MARLIN_MIN_THREAD_N = 64 GPTQ_MARLIN_MIN_THREAD_K = 128 GPTQ_MARLIN_MAX_PARALLEL = 16 MARLIN_SUPPORTED_GROUP_SIZES = [-1, 32, 64, 128] # In case there is a performance issue with Marlin, the variable below can be # changed to False, which allows Marlin to perform global reductions in fp16 # precision (instead of fp32), and therefore, save on some memory movements. USE_FP32_REDUCE_DEFAULT = True # For binary size and compile time, we don't support the same types for with and # without runtime zero-point. We support common cases, i.e. AWQ and GPTQ. # TODO: we may want to move this into the C++ so its closer to the actual impl def query_marlin_supported_quant_types( has_zp: bool | None = None, include_fp_type: bool = True, device_capability: int | None = None, ): if device_capability is None: capability_tuple = current_platform.get_device_capability() device_capability = ( -1 if capability_tuple is None else capability_tuple.to_int() ) if device_capability < 75: return [] # - has_zp is True: return quant_types that has zero points # - has_zp is False: return quant_types that has not zero points # - has_zp is None: both if has_zp is None: types0 = query_marlin_supported_quant_types( False, include_fp_type, device_capability ) types1 = query_marlin_supported_quant_types( True, include_fp_type, device_capability ) return types0 + types1 if has_zp: # AWQ style, unsigned + runtime zero-point return [scalar_types.uint4] else: # GPTQ style, unsigned + symmetric bias res = [scalar_types.uint4b8, scalar_types.uint8b128] if include_fp_type: res += [scalar_types.float8_e4m3fn, scalar_types.float4_e2m1f] return res def _check_marlin_supported( quant_type: ScalarType, group_size: int | None, has_zp: bool, device_capability: int | None = None, ) -> tuple[bool, str | None]: if device_capability is None: capability_tuple = current_platform.get_device_capability() device_capability = ( -1 if capability_tuple is None else capability_tuple.to_int() ) supported_types = query_marlin_supported_quant_types( has_zp, True, device_capability ) if quant_type not in supported_types: return ( False, f"Marlin does not support weight_bits = {quant_type}. " f"Only types = {supported_types} " f"are supported (for group_size = {group_size}, " f"device_capability = {device_capability}, zp = {has_zp}).", ) if group_size is None or group_size not in MARLIN_SUPPORTED_GROUP_SIZES: return ( False, f"Marlin does not support group_size = {group_size}. " f"Only group_sizes = {MARLIN_SUPPORTED_GROUP_SIZES} " "are supported.", ) return True, None def check_marlin_supported( quant_type: ScalarType, group_size: int, has_zp: bool = False, device_capability: int | None = None, ) -> bool: cond, _ = _check_marlin_supported(quant_type, group_size, has_zp, device_capability) return cond def verify_marlin_supported( quant_type: ScalarType, group_size: int, has_zp: bool = False ) -> None: cond, err_msg = _check_marlin_supported(quant_type, group_size, has_zp) if not cond: assert err_msg is not None raise ValueError(err_msg) def verify_marlin_supports_shape( output_size_per_partition: int, input_size_per_partition: int, input_size: int, group_size: int, ) -> None: # Validate output_size_per_partition if output_size_per_partition % GPTQ_MARLIN_MIN_THREAD_N != 0: raise ValueError( f"Weight output_size_per_partition = " f"{output_size_per_partition} is not divisible by " f" min_thread_n = {GPTQ_MARLIN_MIN_THREAD_N}. " "Consider reducing tensor_parallel_size or running " "with --quantization gptq." ) # Validate input_size_per_partition if input_size_per_partition % GPTQ_MARLIN_MIN_THREAD_K != 0: raise ValueError( f"Weight input_size_per_partition = " f"{input_size_per_partition} is not divisible " f"by min_thread_k = {GPTQ_MARLIN_MIN_THREAD_K}. " "Consider reducing tensor_parallel_size or running " "with --quantization gptq." ) if group_size < input_size and input_size_per_partition % group_size != 0: raise ValueError( f"Weight input_size_per_partition = {input_size_per_partition}" f" is not divisible by group_size = {group_size}. " "Consider reducing tensor_parallel_size or running " "with --quantization gptq." ) def check_marlin_supports_shape( output_size_per_partition: int, input_size_per_partition: int, input_size: int, group_size: int, ) -> tuple[bool, str | None]: try: verify_marlin_supports_shape( output_size_per_partition, input_size_per_partition, input_size, group_size ) except ValueError as e: return False, e.__str__() return True, None def check_marlin_supports_layer(layer: LinearBase, group_size: int) -> bool: if current_platform.is_rocm(): return False output_size_per_partition = ( getattr(layer, "output_size_per_partition", None) or layer.output_size ) input_size_per_partition = ( getattr(layer, "input_size_per_partition", None) or layer.input_size ) return check_marlin_supports_shape( output_size_per_partition=output_size_per_partition, input_size_per_partition=input_size_per_partition, input_size=layer.input_size, group_size=group_size, )[0] def check_moe_marlin_supports_layer(layer: LinearBase, group_size: int) -> bool: if current_platform.is_rocm(): return False hidden_size = layer.hidden_size intermediate_size_per_partition = layer.intermediate_size_per_partition # apply_router_weight_on_input is not supported for moe marlin supports_router_weight = not layer.apply_router_weight_on_input # moe marlin requires the activation to be silu supports_activation = layer.activation == "silu" # gate-up: (n, k) = (intermediate_size_per_partition * 2, hidden_size) # down: (n, k) = (hidden_size, intermediate_size_per_partition) # moe marlin requires n % 128 == 0 and k % 64 == 0 supports_shape = ( hidden_size % 128 == 0 and intermediate_size_per_partition % max(64, group_size) == 0 ) supports_group_size = group_size in [-1, 32, 64, 128] return ( supports_shape and supports_group_size and supports_router_weight and supports_activation ) def marlin_moe_intermediate_size(w1_packed: torch.Tensor, w2_packed: torch.Tensor): """ Given Marlin packed weight matrices w1_packed, and w2_packed, return the MoE intermediate size N """ marlin_tile_size = 16 return w2_packed.size(1) * marlin_tile_size def marlin_make_workspace( output_size_per_partition: int, device: torch.device ) -> torch.Tensor: max_workspace_size = ( output_size_per_partition // GPTQ_MARLIN_MIN_THREAD_N ) * GPTQ_MARLIN_MAX_PARALLEL return torch.zeros( max_workspace_size, dtype=torch.int, device=device, requires_grad=False ) def marlin_make_workspace_new( device: torch.device, max_blocks_per_sm: int = 1 ) -> torch.Tensor: # In the new marlin kernel, we use the num of threadblocks as workspace # size. The num of threadblocks is sms_count * max_blocks_per_sm. sms = torch.cuda.get_device_properties(device).multi_processor_count return torch.zeros( sms * max_blocks_per_sm, dtype=torch.int, device=device, requires_grad=False ) def marlin_is_k_full(act_order: bool, is_row_parallel: bool) -> bool: return (not act_order) or (act_order and not is_row_parallel) def marlin_repeat_scales_on_all_ranks( act_order: bool, group_size: int, is_row_parallel: bool ) -> bool: # Need to repeat scales on every rank if act_ordering or # channelwise and RowParallelLinear is_channelwise = group_size == -1 return act_order or (is_channelwise and is_row_parallel) def marlin_make_empty_g_idx(device: torch.device) -> torch.Tensor: return torch.nn.Parameter( torch.empty(0, dtype=torch.int, device=device), requires_grad=False ) def marlin_make_empty_zp(device: torch.device) -> torch.Tensor: return torch.nn.Parameter( torch.empty(0, dtype=torch.int, device=device), requires_grad=False ) def marlin_sort_g_idx(g_idx: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: g_idx_sort_indices = torch.argsort(g_idx).to(torch.int) return g_idx[g_idx_sort_indices], g_idx_sort_indices def get_scale_perms(): scale_perm: list[int] = [] for i in range(8): scale_perm.extend([i + 8 * j for j in range(8)]) scale_perm_single: list[int] = [] for i in range(4): scale_perm_single.extend([2 * i + j for j in [0, 1, 8, 9, 16, 17, 24, 25]]) return scale_perm, scale_perm_single def marlin_permute_scales( s: torch.Tensor, size_k: int, size_n: int, group_size: int, is_a_8bit: bool = False ) -> torch.Tensor: scale_perm, scale_perm_single = get_scale_perms() if group_size < size_k and group_size != -1 and not is_a_8bit: s = s.reshape((-1, len(scale_perm)))[:, scale_perm] else: s = s.reshape((-1, len(scale_perm_single)))[:, scale_perm_single] s = s.reshape((-1, size_n)).contiguous() return s def marlin_permute_bias(s: torch.Tensor) -> torch.Tensor: origin_shape = s.shape _, scale_perm_single = get_scale_perms() s = s.reshape((-1, len(scale_perm_single)))[:, scale_perm_single] return s.reshape(*origin_shape).contiguous() def marlin_act_int8_process_scales(s: torch.Tensor): a_scales_scale_factor = 1 / 4096 * s.max().float() s = s / s.max() * 4096 s = s.round().to(torch.int16).view(s.dtype) return s, a_scales_scale_factor def marlin_moe_permute_scales( s: torch.Tensor, size_k: int, size_n: int, group_size: int, is_a_8bit: bool = False ): num_experts = s.shape[0] output = torch.empty( (num_experts, s.shape[1], s.shape[2]), device=s.device, dtype=s.dtype, ) for e in range(num_experts): output[e] = marlin_permute_scales(s[e], size_k, size_n, group_size, is_a_8bit) return output def marlin_zero_points( zp: torch.Tensor, size_k: int, size_n: int, num_bits: int, is_a_8bit: bool = False ) -> torch.Tensor: # Permute zero-points in a similar way to scales, but do not use the # "single" permutation, since zero-points are applied on every MMA scale_perm, _ = get_scale_perms() zp = zp.reshape((-1, len(scale_perm)))[:, scale_perm] # Interleave column dim (for the dequantize code) and pack it to int32 if num_bits == 4: interleave = numpy.array([0, 2, 4, 6, 1, 3, 5, 7]) elif num_bits == 8: interleave = numpy.array([0, 2, 1, 3]) else: raise Exception("num_bits must be 4 or 8, got {}".format(num_bits)) if not is_a_8bit: zp = zp.reshape((-1, len(interleave)))[:, interleave].ravel() zp = zp.reshape((-1, size_n)).contiguous() zp = pack_cols(zp, num_bits, size_k, size_n) return zp def awq_to_marlin_zero_points( q_zp_packed: torch.Tensor, size_k: int, size_n: int, num_bits: int, is_a_8bit: bool = False, ) -> torch.Tensor: # AWQ zero-points are quantized and packed on the column dim. # In addition, the values are permuted based on dequantizer. # Here we undo both of these, and then apply marlin permutation # and pack it back. q_zp = unpack_cols(q_zp_packed, num_bits, size_k, size_n) # Undo interleaving (use argsort(..) to get inverse perm) if num_bits == 4: undo_interleave = numpy.argsort(numpy.array([0, 2, 4, 6, 1, 3, 5, 7])) elif num_bits == 8: undo_interleave = numpy.argsort(numpy.array([0, 2, 1, 3])) else: raise Exception("num_bits must be 4 or 8, got {}".format(num_bits)) q_zp = q_zp.reshape((-1, len(undo_interleave)))[:, undo_interleave].ravel() q_zp = q_zp.reshape((-1, size_n)).contiguous() marlin_zp = marlin_zero_points(q_zp, size_k, size_n, num_bits, is_a_8bit) return marlin_zp def moe_awq_to_marlin_zero_points( q_zp_packed: torch.Tensor, size_k: int, size_n: int, num_bits: int, is_a_8bit: bool = False, ): num_experts = q_zp_packed.shape[0] output = torch.empty( (num_experts, q_zp_packed.shape[1], q_zp_packed.shape[2]), device=q_zp_packed.device, dtype=q_zp_packed.dtype, ) for e in range(num_experts): output[e] = awq_to_marlin_zero_points( q_zp_packed[e], size_k, size_n, num_bits, is_a_8bit ) return output def maybe_warn_marlin_atomic_add(device, dtype): if torch.compiler.is_dynamo_compiling(): return device_capability = torch.cuda.get_device_capability(device) if device_capability[0] < 9 and dtype == torch.bfloat16: logger.info_once( "You are running Marlin kernel with bf16 on GPUs before SM90. " "You can consider change to fp16 to achieve better performance " "if possible." ) def maybe_warn_marlin_atomic_add_env(): if torch.compiler.is_dynamo_compiling(): return if envs.VLLM_MARLIN_USE_ATOMIC_ADD: return logger.info_once( "Marlin kernel can achieve better performance for small size_n " "with experimental use_atomic_add feature. " "You can consider set environment variable " "VLLM_MARLIN_USE_ATOMIC_ADD to 1 if possible." ) def should_use_atomic_add_reduce( m: int, n: int, k: int, device: torch.device, dtype: torch.dtype ) -> bool: # the performance of atomicAdd is better than global reduce # only when m*n is small and k is large if n >= 2048 or k < 2048 or device.type != "cuda": return False # disable atomicAdd reduce by default, # one can enable it with VLLM_MARLIN_USE_ATOMIC_ADD=1 if not envs.VLLM_MARLIN_USE_ATOMIC_ADD: maybe_warn_marlin_atomic_add_env() return False # sm8x doesn't support atomicAdd + bfloat16 natively device_capability = torch.cuda.get_device_capability(device) if device_capability[0] < 9 and dtype == torch.bfloat16: maybe_warn_marlin_atomic_add(device, dtype) return False return True _quant_fp8_method: QuantFP8 | None = None def get__quant_fp8_method() -> QuantFP8: global _quant_fp8_method if _quant_fp8_method is None: _quant_fp8_method = QuantFP8(False, GroupShape.PER_TOKEN) return _quant_fp8_method def get_marlin_input_dtype(prefix): if envs.VLLM_MARLIN_INPUT_DTYPE is None: return elif envs.VLLM_MARLIN_INPUT_DTYPE.lower() == "int8": return torch.int8 elif envs.VLLM_MARLIN_INPUT_DTYPE.lower() == "fp8": if not current_platform.is_device_capability( 89 ) and not current_platform.is_device_capability(120): raise ValueError( "Marlin W4A8-FP8 only support SM89 or SM120 device " "(It is slower than Marlin W4A16 on other devices). " "You can consider using W4A8-INT8 instead" "(set VLLM_MARLIN_INPUT_DTYPE=int8)." ) _ = get__quant_fp8_method() return torch.float8_e4m3fn else: return def marlin_quant_input(x: torch.Tensor, quant_dtype: torch.dtype): x = x.reshape(-1, x.shape[-1]) if quant_dtype == torch.int8: return per_token_quant_int8(x) elif quant_dtype == torch.float8_e4m3fn: return get__quant_fp8_method()(x) else: raise ValueError(f"unsupported quant_dtype {quant_dtype}") def apply_gptq_marlin_linear( input: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, weight_zp: torch.Tensor, g_idx: torch.Tensor, g_idx_sort_indices: torch.Tensor, workspace: torch.Tensor, wtype: ScalarType, output_size_per_partition: int, input_size_per_partition: int, is_k_full: bool, input_global_scale: torch.Tensor | None = None, bias: torch.Tensor | None = None, use_fp32_reduce: bool = USE_FP32_REDUCE_DEFAULT, input_dtype: torch.dtype | None = None, ) -> torch.Tensor: reshaped_x = input.reshape(-1, input.shape[-1]) out_shape = input.shape[:-1] + (output_size_per_partition,) use_atomic_add = should_use_atomic_add_reduce( m=reshaped_x.size(0), n=output_size_per_partition, k=reshaped_x.size(1), device=input.device, dtype=input.dtype, ) a_scales = None if input_dtype == torch.int8: assert wtype == scalar_types.uint4b8, ( "W8A8-INT8 is not supported by marlin kernel." ) reshaped_x, a_scales = marlin_quant_input(reshaped_x, input_dtype) a_scales = a_scales * input_global_scale elif input_dtype == torch.float8_e4m3fn: assert wtype == scalar_types.uint4b8, ( "INT8 weight + FP8 activation is not supported." ) reshaped_x, a_scales = marlin_quant_input(reshaped_x, input_dtype) output = ops.gptq_marlin_gemm( reshaped_x, None, weight, bias, weight_scale, a_scales, None, weight_zp, g_idx, g_idx_sort_indices, workspace, wtype, size_m=reshaped_x.shape[0], size_n=output_size_per_partition, size_k=input_size_per_partition, is_k_full=is_k_full, use_atomic_add=use_atomic_add, use_fp32_reduce=use_fp32_reduce, is_zp_float=False, ) return output.reshape(out_shape) def apply_awq_marlin_linear( input: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, weight_zp: torch.Tensor, g_idx: torch.Tensor, g_idx_sort_indices: torch.Tensor, workspace: torch.Tensor, quant_type: ScalarType, output_size_per_partition: int, input_size_per_partition: int, input_global_scale: torch.Tensor | None = None, bias: torch.Tensor | None = None, use_fp32_reduce: bool = USE_FP32_REDUCE_DEFAULT, input_dtype: torch.dtype | None = None, ) -> torch.Tensor: reshaped_x = input.reshape(-1, input.shape[-1]) out_shape = input.shape[:-1] + (output_size_per_partition,) use_atomic_add = should_use_atomic_add_reduce( m=reshaped_x.size(0), n=output_size_per_partition, k=reshaped_x.size(1), device=input.device, dtype=input.dtype, ) a_scales = None if input_dtype == torch.int8: assert quant_type == scalar_types.uint4, ( "W8A8-INT8 is not supported by marlin kernel." ) reshaped_x, a_scales = marlin_quant_input(reshaped_x, input_dtype) a_scales = a_scales * input_global_scale elif input_dtype == torch.float8_e4m3fn: assert quant_type == scalar_types.uint4, ( "INT8 weight + FP8 activation is not supported." ) reshaped_x, a_scales = marlin_quant_input(reshaped_x, input_dtype) output = ops.gptq_marlin_gemm( reshaped_x, None, weight, bias, weight_scale, a_scales, None, weight_zp, g_idx, g_idx_sort_indices, workspace, quant_type, size_m=reshaped_x.shape[0], size_n=output_size_per_partition, size_k=input_size_per_partition, use_atomic_add=use_atomic_add, use_fp32_reduce=use_fp32_reduce, is_zp_float=False, ) return output.reshape(out_shape) def apply_rtn_marlin_linear( input: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, workspace: torch.Tensor, quant_type: ScalarType, output_size_per_partition: int, input_size_per_partition: int, input_global_scale: torch.Tensor | None = None, bias: torch.Tensor | None = None, use_fp32_reduce: bool = USE_FP32_REDUCE_DEFAULT, input_dtype: torch.dtype | None = None, ) -> torch.Tensor: reshaped_x = input.reshape(-1, input.shape[-1]) out_shape = input.shape[:-1] + (output_size_per_partition,) use_atomic_add = should_use_atomic_add_reduce( m=reshaped_x.size(0), n=output_size_per_partition, k=reshaped_x.size(1), device=input.device, dtype=input.dtype, ) a_scales = None if input_dtype == torch.int8: assert quant_type == scalar_types.uint4b8, ( "W8A8-INT8 is not supported by marlin kernel." ) reshaped_x, a_scales = marlin_quant_input(reshaped_x, input_dtype) a_scales = a_scales * input_global_scale elif input_dtype == torch.float8_e4m3fn: assert quant_type == scalar_types.uint4b8, ( "INT8 weight + FP8 activation is not supported." ) reshaped_x, a_scales = marlin_quant_input(reshaped_x, input_dtype) output = ops.gptq_marlin_gemm( reshaped_x, None, weight, bias, weight_scale, a_scales, None, None, None, None, workspace, quant_type, size_m=reshaped_x.shape[0], size_n=output_size_per_partition, size_k=input_size_per_partition, use_atomic_add=use_atomic_add, use_fp32_reduce=use_fp32_reduce, is_zp_float=False, ) return output.reshape(out_shape)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/int8_utils.py
vllm/model_executor/layers/quantization/utils/int8_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # Adapted from https://github.com/sgl-project/sglang/blob/4cb53ecd0cffceb6dee5c011a58f65997a86f151/python/sglang/srt/layers/quantization/int8_kernel.py import functools import json import logging import os from typing import Any import torch from vllm.platforms import current_platform from vllm.triton_utils import tl, triton logger = logging.getLogger(__name__) def apply_w8a8_block_int8_linear( input: torch.Tensor, weight: torch.Tensor, block_size: list[int], weight_scale: torch.Tensor, input_scale: torch.Tensor | None = None, bias: torch.Tensor | None = None, ) -> torch.Tensor: assert input_scale is None # View input as 2D matrix for fp8 methods input_2d = input.view(-1, input.shape[-1]) output_shape = [*input.shape[:-1], weight.shape[0]] q_input, x_scale = per_token_group_quant_int8(input_2d, block_size[1]) output = w8a8_block_int8_matmul( q_input, weight, x_scale, weight_scale, block_size, output_dtype=input.dtype ) if bias is not None: output = output + bias return output.to(dtype=input.dtype).view(*output_shape) def input_to_int8( x: torch.Tensor, dtype: torch.dtype = torch.int8 ) -> tuple[torch.Tensor, torch.Tensor]: """This function quantizes input values to int8 values with tensor-wise quantization.""" iinfo = torch.iinfo(dtype) min_val, max_val = x.aminmax() amax = torch.maximum(min_val.abs(), max_val.abs()).clamp(min=1e-12) int8_min, int8_max = iinfo.min, iinfo.max scale = int8_max / amax x_scl_sat = (x * scale).clamp(min=int8_min, max=int8_max) return x_scl_sat.to(dtype).contiguous(), scale.float().reciprocal() def block_dequant( x_q_block: torch.Tensor, x_s: torch.Tensor, block_size: list[int], ) -> torch.Tensor: """This function conducts block-wise dequantization. The inputs are block-wise quantization tensor `x_q_block`, block-wise quantization scale and the block size. The outputs are dequantized tensor. """ block_n, block_k = block_size[0], block_size[1] n, k = x_q_block.shape n_tiles = (n + block_n - 1) // block_n k_tiles = (k + block_k - 1) // block_k assert n_tiles == x_s.shape[0] assert k_tiles == x_s.shape[1] x_dq_block = x_q_block.to(torch.float32) for i in range(k_tiles): for j in range(n_tiles): x_dq_block[ j * block_n : min((j + 1) * block_n, n), i * block_k : min((i + 1) * block_k, k), ] *= x_s[j][i] return x_dq_block if current_platform.is_rocm(): @triton.jit def round_int8(x): return tl.extra.hip.libdevice.round(x).to(tl.int8) else: @triton.jit def round_int8(x): return tl.extra.cuda.libdevice.round(x).to(tl.int8) @triton.jit def _per_token_quant_int8( x_ptr, xq_ptr, scale_ptr, stride_x, stride_xq, N, BLOCK: tl.constexpr, ): # Adapted from https://github.com/InternLM/lmdeploy/blob/086481ed84b59bee3b8e4274e5fc69620040c048/lmdeploy/pytorch/kernels/cuda/w8a8_triton_kernels.py#L282 row_id = tl.program_id(0) cols = tl.arange(0, BLOCK) mask = cols < N x = tl.load(x_ptr + row_id * stride_x + cols, mask=mask, other=0.0).to(tl.float32) absmax = tl.maximum(tl.max(tl.abs(x)), 1e-10) scale_x = absmax / 127 x_q = x * (127 / absmax) x_q = round_int8(x_q) tl.store(xq_ptr + row_id * stride_xq + cols, x_q, mask=mask) tl.store(scale_ptr + row_id, scale_x) def per_token_quant_int8(x): M = x.numel() // x.shape[-1] N = x.shape[-1] x_q = torch.empty_like(x, device=x.device, dtype=torch.int8) scales = torch.empty(x.shape[:-1] + (1,), device=x.device, dtype=torch.float32) BLOCK = triton.next_power_of_2(N) # heuristics for number of warps num_warps = min(max(BLOCK // 256, 1), 8) assert x.is_contiguous() _per_token_quant_int8[(M,)]( x, x_q, scales, stride_x=x.stride(-2), stride_xq=x_q.stride(-2), N=N, BLOCK=BLOCK, num_warps=num_warps, num_stages=1, ) return x_q, scales @triton.jit def _per_token_group_quant_int8( # Pointers to inputs and output y_ptr, y_q_ptr, y_s_ptr, # Stride of input y_stride, # Columns of input N, # Avoid to divide zero eps, # Information for int8 int8_min, int8_max, # Meta-parameters BLOCK: tl.constexpr, ): """A Triton-accelerated function to perform per-token-group quantization on a tensor. This function converts the tensor values into int8 values. """ # Map the program id to the row of X and Y it should compute. g_id = tl.program_id(0) y_ptr += g_id * y_stride y_q_ptr += g_id * y_stride y_s_ptr += g_id cols = tl.arange(0, BLOCK) # N <= BLOCK mask = cols < N y = tl.load(y_ptr + cols, mask=mask, other=0.0).to(tl.float32) # Quant _absmax = tl.maximum(tl.max(tl.abs(y)), eps) y_s = _absmax / int8_max y_q = tl.clamp(y / y_s, int8_min, int8_max).to(y_q_ptr.dtype.element_ty) tl.store(y_q_ptr + cols, y_q, mask=mask) tl.store(y_s_ptr, y_s) def per_token_group_quant_int8( x: torch.Tensor, group_size: int, eps: float = 1e-10, dtype: torch.dtype = torch.int8, ) -> tuple[torch.Tensor, torch.Tensor]: """Function to perform per-token-group quantization on an input tensor `x`. It converts the tensor values into signed int8 values and returns the quantized tensor along with the scaling factor used for quantization. Args: x: The input tensor with ndim >= 2. group_size: The group size used for quantization. eps: The minimum to avoid dividing zero. dtype: The dype of output tensor. Note that only `torch.int8` is supported for now. Returns: tuple[torch.Tensor, torch.Tensor]: The quantized tensor and the scaling factor for quantization. """ assert x.shape[-1] % group_size == 0, ( "the last dimension of `x` cannot be divisible by `group_size`" ) assert x.is_contiguous(), "`x` is not contiguous" iinfo = torch.iinfo(dtype) int8_max = iinfo.max int8_min = iinfo.min x_q = torch.empty_like(x, device=x.device, dtype=dtype) x_s = torch.empty( x.shape[:-1] + (x.shape[-1] // group_size,), device=x.device, dtype=torch.float32, ) # prefer CUDA kernel if available if current_platform.is_cuda(): torch.ops._C.per_token_group_quant_int8( x, x_q, x_s, group_size, eps, float(int8_min), float(int8_max) ) return x_q, x_s M = x.numel() // group_size N = group_size BLOCK = triton.next_power_of_2(N) # heuristics for number of warps num_warps = min(max(BLOCK // 256, 1), 8) num_stages = 1 _per_token_group_quant_int8[(M,)]( x, x_q, x_s, group_size, N, eps, int8_min=int8_min, int8_max=int8_max, BLOCK=BLOCK, num_warps=num_warps, num_stages=num_stages, ) return x_q, x_s @triton.jit def _w8a8_block_int8_matmul( # Pointers to inputs and output A, B, C, As, Bs, # Shape for matmul M, N, K, # Block size for block-wise quantization group_n, group_k, # Stride for inputs and output stride_am, stride_ak, stride_bk, stride_bn, stride_cm, stride_cn, stride_As_m, stride_As_k, stride_Bs_k, stride_Bs_n, # Meta-parameters BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr, GROUP_SIZE_M: tl.constexpr, ): """Triton-accelerated function used to perform linear operations (dot product) on input tensors `A` and `B` with block-wise quantization, and store the result in output tensor `C`. """ pid = tl.program_id(axis=0) num_pid_m = tl.cdiv(M, BLOCK_SIZE_M) num_pid_n = tl.cdiv(N, BLOCK_SIZE_N) num_pid_in_group = GROUP_SIZE_M * num_pid_n group_id = pid // num_pid_in_group first_pid_m = group_id * GROUP_SIZE_M group_size_m = min(num_pid_m - first_pid_m, GROUP_SIZE_M) pid_m = first_pid_m + (pid % group_size_m) pid_n = (pid % num_pid_in_group) // group_size_m offs_am = (pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)) % M offs_bn = (pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)) % N offs_k = tl.arange(0, BLOCK_SIZE_K) a_ptrs = A + (offs_am[:, None] * stride_am + offs_k[None, :] * stride_ak) b_ptrs = B + (offs_k[:, None] * stride_bk + offs_bn[None, :] * stride_bn) As_ptrs = As + offs_am * stride_As_m offs_bsn = offs_bn // group_n Bs_ptrs = Bs + offs_bsn * stride_Bs_n accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32) for k in range(0, tl.cdiv(K, BLOCK_SIZE_K)): a = tl.load(a_ptrs, mask=offs_k[None, :] < K - k * BLOCK_SIZE_K, other=0.0) b = tl.load(b_ptrs, mask=offs_k[:, None] < K - k * BLOCK_SIZE_K, other=0.0) k_start = k * BLOCK_SIZE_K offs_ks = k_start // group_k a_s = tl.load(As_ptrs + offs_ks * stride_As_k) b_s = tl.load(Bs_ptrs + offs_ks * stride_Bs_k) accumulator += tl.dot(a, b).to(tl.float32) * a_s[:, None] * b_s[None, :] a_ptrs += BLOCK_SIZE_K * stride_ak b_ptrs += BLOCK_SIZE_K * stride_bk if C.dtype.element_ty == tl.bfloat16: c = accumulator.to(tl.bfloat16) elif C.dtype.element_ty == tl.float16: c = accumulator.to(tl.float16) else: c = accumulator.to(tl.float32) offs_cm = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M) offs_cn = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N) c_ptrs = C + stride_cm * offs_cm[:, None] + stride_cn * offs_cn[None, :] c_mask = (offs_cm[:, None] < M) & (offs_cn[None, :] < N) tl.store(c_ptrs, c, mask=c_mask) @functools.lru_cache def get_w8a8_block_int8_configs( N: int, K: int, block_n: int, block_k: int ) -> dict[int, Any] | None: """ Return optimized configurations for the w8a8 block fp8 kernel. The return value will be a dictionary that maps an irregular grid of batch sizes to configurations of the w8a8 block fp8 kernel. To evaluate the kernel on a given batch size bs, the closest batch size in the grid should be picked and the associated configuration chosen to invoke the kernel. """ # First look up if an optimized configuration is available in the configs # directory device_name = current_platform.get_device_name().replace(" ", "_") json_file_name = f"N={N},K={K},device_name={device_name},dtype=int8_w8a8,block_shape=[{block_n}, {block_k}].json" # noqa: E501 config_file_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), "configs", json_file_name ) if os.path.exists(config_file_path): with open(config_file_path) as f: logger.info( "Using configuration from %s for W8A8 Block INT8 kernel.", config_file_path, ) # If a configuration has been found, return it return {int(key): val for key, val in json.load(f).items()} # If no optimized configuration is available, we will use the default # configuration logger.warning( ( "Using default W8A8 Block INT8 kernel config. Performance might " "be sub-optimal! Config file not found at %s" ), config_file_path, ) return None def w8a8_block_int8_matmul( A: torch.Tensor, B: torch.Tensor, As: torch.Tensor, Bs: torch.Tensor, block_size: list[int], output_dtype: torch.dtype = torch.float16, ) -> torch.Tensor: """This function performs matrix multiplication with block-wise quantization. It takes two input tensors `A` and `B` with scales `As` and `Bs`. The output is returned in the specified `output_dtype`. Args: A: The input tensor, e.g., activation. B: The input tensor, e.g., weight. As: The per-token-group quantization scale for `A`. Bs: The per-block quantization scale for `B`. block_size: The block size for per-block quantization. It should be 2-dim, e.g., [128, 128]. output_dtype: The dtype of the returned tensor. Returns: torch.Tensor: The result of matmul. """ assert len(block_size) == 2 block_n, block_k = block_size[0], block_size[1] assert A.shape[-1] == B.shape[-1] assert A.shape[:-1] == As.shape[:-1] and A.is_contiguous() assert triton.cdiv(A.shape[-1], block_k) == As.shape[-1] M = A.numel() // A.shape[-1] assert B.ndim == 2 and B.is_contiguous() and Bs.ndim == 2 N, K = B.shape assert triton.cdiv(N, block_n) == Bs.shape[0] assert triton.cdiv(K, block_k) == Bs.shape[1] C_shape = A.shape[:-1] + (N,) C = A.new_empty(C_shape, dtype=output_dtype) configs = get_w8a8_block_int8_configs(N, K, block_size[0], block_size[1]) if configs: # If an optimal configuration map has been found, look up the # optimal config config = configs[min(configs.keys(), key=lambda x: abs(x - M))] else: # Default config # Block-wise quant: BLOCK_SIZE_K must be divisible by block_size[1] config = { "BLOCK_SIZE_M": 64, "BLOCK_SIZE_N": block_size[0], "BLOCK_SIZE_K": block_size[1], "GROUP_SIZE_M": 32, "num_warps": 4, "num_stages": 3, } def grid(META): return ( triton.cdiv(M, META["BLOCK_SIZE_M"]) * triton.cdiv(N, META["BLOCK_SIZE_N"]), ) _w8a8_block_int8_matmul[grid]( A, B, C, As, Bs, M, N, K, block_n, block_k, A.stride(-2), A.stride(-1), B.stride(1), B.stride(0), C.stride(-2), C.stride(-1), As.stride(-2), As.stride(-1), Bs.stride(1), Bs.stride(0), **config, ) return C
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/marlin_utils_test.py
vllm/model_executor/layers/quantization/utils/marlin_utils_test.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Utility functions used for tests and benchmarks""" import numpy as np import torch from vllm import _custom_ops as ops from vllm.scalar_type import ScalarType, scalar_types from .marlin_utils import GPTQ_MARLIN_TILE, marlin_permute_scales, marlin_zero_points from .quant_utils import ( get_pack_factor, gptq_quantize_weights, quantize_weights, sort_weights, ) class MarlinWorkspace: def __init__(self, out_features, min_thread_n, max_parallel): assert out_features % min_thread_n == 0, ( "out_features = {} is indivisible by min_thread_n = {}".format( out_features, min_thread_n ) ) max_workspace_size = (out_features // min_thread_n) * max_parallel self.scratch = torch.zeros(max_workspace_size, dtype=torch.int, device="cuda") def marlin_permute_weights( q_w, size_k, size_n, perm, tile=GPTQ_MARLIN_TILE, is_a_8bit=False ): assert q_w.shape == (size_k, size_n) assert size_k % tile == 0, f"size_k = {size_k}, tile = {tile}" assert size_n % tile == 0, f"size_k = {size_n}, tile = {tile}" if is_a_8bit: # Permute weights to 32x32 marlin tiles q_w = q_w.reshape((size_k // (tile * 2), tile * 2, size_n // tile, tile)) else: # Permute weights to 16x64 marlin tiles q_w = q_w.reshape((size_k // tile, tile, size_n // tile, tile)) q_w = q_w.permute((0, 2, 1, 3)) q_w = q_w.reshape((size_k // tile, size_n * tile)) q_w = q_w.reshape((-1, perm.numel()))[:, perm].reshape(q_w.shape) return q_w def marlin_weights(q_w, size_k, size_n, num_bits, perm, is_a_8bit=False): # Permute q_w = marlin_permute_weights(q_w, size_k, size_n, perm, is_a_8bit=is_a_8bit) # Pack pack_factor = get_pack_factor(num_bits) orig_device = q_w.device q_w = q_w.cpu().numpy().astype(np.uint32) q_packed = np.zeros((q_w.shape[0], q_w.shape[1] // pack_factor), dtype=np.uint32) for i in range(pack_factor): q_packed |= q_w[:, i::pack_factor] << num_bits * i q_packed = torch.from_numpy(q_packed.astype(np.int32)).to(orig_device) return q_packed def get_weight_perm(num_bits: int, is_a_8bit: bool = False): perm_list: list[int] = [] if is_a_8bit: for i in range(32): perm1 = [] col = i // 4 for block in [0, 1]: for row in [ 4 * (i % 4), 4 * (i % 4) + 1, 4 * (i % 4) + 2, 4 * (i % 4) + 3, 4 * (i % 4 + 4), 4 * (i % 4 + 4) + 1, 4 * (i % 4 + 4) + 2, 4 * (i % 4 + 4) + 3, ]: perm1.append(16 * row + col + 8 * block) for j in range(2): perm_list.extend([p + 512 * j for p in perm1]) else: for i in range(32): perm1 = [] col = i // 4 for block in [0, 1]: for row in [ 2 * (i % 4), 2 * (i % 4) + 1, 2 * (i % 4 + 4), 2 * (i % 4 + 4) + 1, ]: perm1.append(16 * row + col + 8 * block) for j in range(4): perm_list.extend([p + 256 * j for p in perm1]) perm = np.array(perm_list) if num_bits == 4: if is_a_8bit: # noqa: SIM108 interleave = np.array([0, 4, 1, 5, 2, 6, 3, 7]) else: interleave = np.array([0, 2, 4, 6, 1, 3, 5, 7]) elif num_bits == 8: if is_a_8bit: # noqa: SIM108 interleave = np.array([0, 1, 2, 3]) else: interleave = np.array([0, 2, 1, 3]) else: raise Exception("num_bits must be 4 or 8, got {}".format(num_bits)) perm = perm.reshape((-1, len(interleave)))[:, interleave].ravel() perm = torch.from_numpy(perm) return perm def marlin_quantize( w: torch.Tensor, quant_type: ScalarType, group_size: int, act_order: bool, test_perm: torch.Tensor | None = None, input_dtype: torch.dtype | None = None, ): is_a_8bit = input_dtype is not None and input_dtype.itemsize == 1 size_k, size_n = w.shape num_bits = quant_type.size_bits # Normalize group_size if group_size == -1: group_size = size_k assert group_size <= size_k # Quantize (and apply act_order if provided) w_ref, q_w, s, g_idx, rand_perm = gptq_quantize_weights( w, quant_type, group_size, act_order, test_perm ) # For act_order, sort the "weights" and "g_idx" so that group ids are # increasing sort_indices = torch.empty(0, dtype=torch.int, device=w.device) if act_order: q_w, g_idx, sort_indices = sort_weights(q_w, g_idx) # Reformat to marlin weight_perm = get_weight_perm(num_bits, is_a_8bit) marlin_q_w = marlin_weights( q_w, size_k, size_n, num_bits, weight_perm, is_a_8bit=is_a_8bit ) marlin_s = marlin_permute_scales(s, size_k, size_n, group_size, is_a_8bit=is_a_8bit) if input_dtype == torch.float8_e4m3fn and quant_type == scalar_types.uint4b8: ops.marlin_int4_fp8_preprocess(marlin_q_w, inplace=True) marlin_s = marlin_s * 512 # Create result res_list = [w_ref, marlin_q_w, marlin_s, g_idx, sort_indices, rand_perm] for i in range(len(res_list)): res_list[i] = res_list[i].to(w.device) return res_list def awq_marlin_quantize( w: torch.Tensor, quant_type: ScalarType, group_size: int, input_dtype: torch.dtype | None = None, ): is_a_8bit = input_dtype is not None and input_dtype.itemsize == 1 size_k, size_n = w.shape # Normalize group_size if group_size == -1: group_size = size_k assert group_size <= size_k # Detect num groups assert size_k % group_size == 0 num_groups = size_k // group_size # Quantize with zp w_ref, q_w, s, zp = quantize_weights(w, quant_type, group_size, zero_points=True) if input_dtype == torch.float8_e4m3fn and quant_type == scalar_types.uint4: repeated_zp = zp.repeat_interleave(group_size, 0) q_w_old = q_w q_w = q_w_old - repeated_zp q_w[q_w < 0] = 15 - q_w_old[q_w < 0] s = s * 512 # Reformat to marlin weight_perm = get_weight_perm(quant_type.size_bits, is_a_8bit) marlin_q_w = marlin_weights( q_w, size_k, size_n, quant_type.size_bits, weight_perm, is_a_8bit=is_a_8bit ) marlin_s = marlin_permute_scales(s, size_k, size_n, group_size, is_a_8bit=is_a_8bit) marlin_zp = marlin_zero_points( zp, num_groups, size_n, quant_type.size_bits, is_a_8bit=is_a_8bit ) # Create result res_list = [w_ref, marlin_q_w, marlin_s, marlin_zp] for i in range(len(res_list)): res_list[i] = res_list[i].to(w.device) return res_list
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/mxfp4_utils.py
vllm/model_executor/layers/quantization/utils/mxfp4_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable from typing import Any import torch from vllm.logger import init_logger from vllm.platforms import current_platform from vllm.triton_utils import triton from vllm.utils.import_utils import has_triton_kernels from vllm.utils.torch_utils import direct_register_custom_op, is_torch_equal_or_newer logger = init_logger(__name__) def _swizzle_mxfp4(quant_tensor, scale, num_warps): """weight swizzle for mxfp4 moe, used for OAI mxfp4 kernel""" assert has_triton_kernels() import triton_kernels.matmul_ogs_details.opt_flags as opt_flags from triton_kernels.numerics import InFlexData from triton_kernels.tensor import FP4, convert_layout, wrap_torch_tensor from triton_kernels.tensor_details import layout from triton_kernels.tensor_details.layout import StridedLayout value_layout_opts: dict[str, Any] = {} scale_layout_opts: dict[str, Any] = {} if ( current_platform.is_cuda() and current_platform.is_device_capability(90) and not is_torch_equal_or_newer("2.8.1") ): logger.warning_once( "Mxfp4 on hopper is running on torch < 2.8.1, " "this cause swizling to be disabled, which may " "cause performance degradation. Please upgrade to torch nightly" ) value_layout = StridedLayout scale_layout = StridedLayout elif current_platform.is_rocm(): from vllm.platforms.rocm import on_gfx950 value_layout = StridedLayout if on_gfx950(): from triton_kernels.tensor_details.layout import GFX950MXScaleLayout scale_layout = GFX950MXScaleLayout else: scale_layout = StridedLayout else: value_layout, value_layout_opts = layout.make_default_matmul_mxfp4_w_layout( mx_axis=1 ) scale_layout, scale_layout_opts = ( layout.make_default_matmul_mxfp4_w_scale_layout( mx_axis=1, num_warps=num_warps ) ) if current_platform.is_cuda(): if current_platform.is_device_capability(90): constraints = { "split_k": 1, } opt_flags.update_opt_flags_constraints(constraints) elif current_platform.is_device_capability_family(100): constraints = { "is_persistent": True, "epilogue_subtile": 1, } opt_flags.update_opt_flags_constraints(constraints) # transpose the tensor so that the quantization axis is on dim1 quant_tensor = quant_tensor.transpose(-2, -1) scale = scale.transpose(-2, -1) quant_tensor = convert_layout( wrap_torch_tensor(quant_tensor, dtype=FP4), value_layout, **value_layout_opts ) scale = convert_layout(wrap_torch_tensor(scale), scale_layout, **scale_layout_opts) return quant_tensor, InFlexData(), scale def _can_support_mxfp4( use_grouped_topk: bool = False, topk_group: int | None = None, num_expert_group: int | None = None, expert_map: torch.Tensor | None = None, custom_routing_function: Callable | None = None, e_score_correction_bias: torch.Tensor | None = None, apply_router_weight_on_input: bool = False, scoring_func: str = "softmax", activation: str = "swigluoai", expert_load_view: torch.Tensor | None = None, logical_to_physical_map: torch.Tensor | None = None, logical_replica_count: torch.Tensor | None = None, ): return not ( use_grouped_topk or topk_group or num_expert_group or custom_routing_function or e_score_correction_bias or apply_router_weight_on_input or scoring_func != "softmax" or activation != "swigluoai" or expert_load_view or logical_to_physical_map or logical_replica_count ) def get_padding_alignment(): return ( 256 if triton.runtime.driver.active.get_current_target().arch in ("gfx950",) else 128 ) def _dequant_mxfp4( x: torch.Tensor, scale: torch.Tensor, float_dtype: torch.dtype ) -> torch.Tensor: try: from quark.torch.kernel import mx except ImportError as err: raise ImportError( "The package `amd-quark` is required to use " "MX-FP4 models. Please install it with `pip install " "amd-quark`." ) from err return mx.dq_mxfp4(x, scale, float_dtype) def _dequant_mxfp4_fake( x: torch.Tensor, scale: torch.Tensor, float_dtype: torch.dtype ) -> torch.Tensor: return torch.empty( (*x.shape[:-1], x.shape[-1] * 2), dtype=float_dtype, device=x.device ) def _quant_dequant_mxfp4( x: torch.Tensor, scale_calculation_mode: str = "even" ) -> torch.Tensor: try: from quark.torch.kernel import mx except ImportError as err: raise ImportError( "The package `amd-quark` is required to use " "MX-FP4 models. Please install it with `pip install " "amd-quark`." ) from err return mx.qdq_mxfp4(x, scale_calculation_mode) def _quant_dequant_mxfp4_fake( x: torch.Tensor, scale_calculation_mode: str = "even" ) -> torch.Tensor: return torch.empty_like(x) # Protect these operations into a torch custom op to avoid errors as # torch._dynamo.exc.Unsupported: Attempted to call function marked as skipped # Explanation: Dynamo does not know how to trace the builtin # `kernel_ext.PyCapsule.dq_uint8_mxfp4_to_half.` This function is either a # Python builtin (e.g. _warnings.warn) or a third-party C/C++ Python # extension (perhaps created with pybind). # TODO: Make sure there is no way to avoid having these functions # marked as skipped by dynamo. try: direct_register_custom_op( op_name="dequant_mxfp4", op_func=_dequant_mxfp4, fake_impl=_dequant_mxfp4_fake, ) dequant_mxfp4 = torch.ops.vllm.dequant_mxfp4 except AttributeError as error: raise error try: direct_register_custom_op( op_name="quant_dequant_mxfp4", op_func=_quant_dequant_mxfp4, fake_impl=_quant_dequant_mxfp4_fake, ) quant_dequant_mxfp4 = torch.ops.vllm.quant_dequant_mxfp4 except AttributeError as error: raise error
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/mxfp8_utils.py
vllm/model_executor/layers/quantization/utils/mxfp8_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm.logger import init_logger logger = init_logger(__name__) def mxfp8_e4m3_quantize(x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: try: from flashinfer import mxfp8_quantize as mxfp8_e4m3_quantize except ImportError as err: raise ImportError( "The package `flashinfer` is required to do " "MX-FP8 quantization. Please install it with" "`pip install flashinfer`" ) from err x_q, x_scales = mxfp8_e4m3_quantize(x, is_sf_swizzled_layout=False) if x_scales.ndim == 1: x_scales = x_scales.view(x.size(0), -1) return x_q, x_scales
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/mxfp6_utils.py
vllm/model_executor/layers/quantization/utils/mxfp6_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm.model_executor.layers.quantization.utils.ocp_mx_utils import OCP_MX_BLOCK_SIZE from vllm.utils.torch_utils import direct_register_custom_op def _quant_dequant_mxfp6( x: torch.Tensor, quant_dtype: str, scale_calculation_mode: str = "even", ) -> torch.Tensor: try: from quark.torch.kernel.hw_emulation.hw_emulation_interface import ( fake_quantize_fp4_fp6_per_group_with_scale, ) from quark.torch.quantization.utils import even_round, reshape_to_blocks except ImportError as err: raise ImportError( "The package `amd-quark` is required to use " "MX-FP6 models. Please install it with `pip install " "amd-quark`." ) from err axis = -1 block_x = reshape_to_blocks(x, OCP_MX_BLOCK_SIZE, axis) amax, _ = torch.max(torch.abs(block_x), dim=-1, keepdim=True) amax = amax.squeeze(-1) # TODO: there are other rounding strategies supported in quark and in the # config.json that we do not check for here! if scale_calculation_mode != "even": raise NotImplementedError( f"Scale calculation mode {scale_calculation_mode} is not yet " "supported in MX-FP6 quantization" ) scale = even_round(amax, quant_dtype) # Apply dequantize(quantize(x)). x = fake_quantize_fp4_fp6_per_group_with_scale( x, scale.to(x.device), axis=axis, group_size=OCP_MX_BLOCK_SIZE, quant_dtype=quant_dtype, ) return x def _quant_dequant_mxfp6_fake( x: torch.Tensor, quant_dtype: str, scale_calculation_mode: str = "even", ) -> torch.Tensor: return torch.empty_like(x) def _dequant_mxfp6( x: torch.Tensor, scale: torch.Tensor, float_dtype: torch.dtype, quant_dtype: str ) -> torch.Tensor: try: from quark.torch.kernel.hw_emulation.hw_emulation_interface import ( dequantize_fp4_fp6_per_group, ) from quark.torch.utils.pack import create_pack_method except ImportError as e: raise ImportError( "The package `amd-quark` is required to use " "MX-FP6 models. Please install it with `pip install " "amd-quark`." ) from e pack_method = create_pack_method(None, dtype=quant_dtype) unpacked_x = pack_method.unpack(x, reorder=False) scale = 2 ** (scale.view(torch.uint8).to(torch.int16) - 127).to(float_dtype) # TODO: `dequantize_fp4_fp6_per_group` and `prepare_inputs_per_group` # always return fp32. return dequantize_fp4_fp6_per_group( unpacked_x, scale, axis=-1, group_size=OCP_MX_BLOCK_SIZE, quant_dtype=quant_dtype, ).to(float_dtype) def _dequant_mxfp6_fake( x: torch.Tensor, scale: torch.Tensor, float_dtype: torch.dtype, quant_dtype: str ) -> torch.Tensor: assert (x.shape[-1] * 4) % 3 == 0 return torch.empty( (*x.shape[:-1], (x.shape[-1] * 4) // 3), dtype=float_dtype, device=x.device ) # Protect these operations into a torch custom op to avoid errors as # torch._dynamo.exc.Unsupported: Attempted to call function marked as skipped # Explanation: Dynamo does not know how to trace the builtin # `kernel_ext.PyCapsule.dq_uint8_mxfp4_to_half.` This function is either a # Python builtin (e.g. _warnings.warn) or a third-party C/C++ Python # extension (perhaps created with pybind). # TODO: Make sure there is no way to avoid having these functions # marked as skipped by dynamo. try: direct_register_custom_op( op_name="quant_dequant_mxfp6", op_func=_quant_dequant_mxfp6, mutates_args=[], fake_impl=_quant_dequant_mxfp6_fake, ) except AttributeError as error: raise error # Expose keyword arguments. def quant_dequant_mxfp6( x: torch.Tensor, quant_dtype: str, scale_calculation_mode: str = "even", ) -> torch.Tensor: return torch.ops.vllm.quant_dequant_mxfp6(x, quant_dtype, scale_calculation_mode) try: direct_register_custom_op( op_name="dequant_mxfp6", op_func=_dequant_mxfp6, mutates_args=[], fake_impl=_dequant_mxfp6_fake, ) except AttributeError as error: raise error def dequant_mxfp6( x: torch.Tensor, scale: torch.Tensor, float_dtype: torch.dtype, quant_dtype: str ) -> torch.Tensor: return torch.ops.vllm.dequant_mxfp6(x, scale, float_dtype, quant_dtype)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/machete_utils.py
vllm/model_executor/layers/quantization/utils/machete_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm.scalar_type import ScalarType, scalar_types MACHETE_PREPACKED_BLOCK_SHAPE = [64, 128] def query_machete_supported_quant_types(zero_points: bool) -> list[ScalarType]: if zero_points: return [scalar_types.uint4, scalar_types.uint8] else: return [scalar_types.uint4b8, scalar_types.uint8b128] def query_machete_supported_act_types(zero_points: bool) -> list[ScalarType]: return [torch.float16, torch.bfloat16] def query_machete_supported_group_sizes(act_type: torch.dtype) -> list[int]: """ Queries the supported group sizes for Machete based on the activation type. Args: act_type: The activation data type (torch.float16, torch.bfloat16). Returns: A list of supported group sizes. The group size must be divisible by `TileShapeK = 128 * 8 // num_bits(act_type)`. -1 indicates per-channel quantization. """ if act_type in [torch.float16, torch.bfloat16]: return [-1, 64, 128] else: return [-1, 128] def check_machete_supports_shape( in_features: int, out_featrues: int ) -> tuple[bool, str | None]: if in_features % MACHETE_PREPACKED_BLOCK_SHAPE[0] != 0: return ( False, "Input features size must be divisible by " f"{MACHETE_PREPACKED_BLOCK_SHAPE[0]}", ) if out_featrues % MACHETE_PREPACKED_BLOCK_SHAPE[1] != 0: return ( False, "Output features size must be divisible by " f"{MACHETE_PREPACKED_BLOCK_SHAPE[1]}", ) return True, None
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/flashinfer_utils.py
vllm/model_executor/layers/quantization/utils/flashinfer_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from enum import Enum import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm import envs from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe.config import ( FusedMoEConfig, FusedMoEQuantConfig, ) from vllm.model_executor.layers.fused_moe.flashinfer_cutlass_moe import ( FlashInferExperts, ) from vllm.model_executor.layers.fused_moe.flashinfer_cutlass_prepare_finalize import ( # noqa: E501 create_flashinfer_prepare_finalize, ) from vllm.platforms import current_platform logger = init_logger(__name__) class FlashinferMoeBackend(Enum): TENSORRT_LLM = "TensorRT-LLM" CUTLASS = "CUTLASS" CUTEDSL = "CUTEDSL" def calculate_tile_tokens_dim(num_tokens, top_k, num_experts): from flashinfer import next_positive_power_of_2 # FlashInfer 0.2.10 has issues with larger tile sizes. Set to 8 for now. # TODO: Revert this to dynamic calculation once a new version of FlashInfer # with the necessary kernels is released. tile_tokens_dim = 8 # A factor considering tokens are not perfectly balanced among experts. imbalance_factor = 1.3 # Calculate the number of tokens per expert # assuming perfect distribution. num_tokens_per_expert = (num_tokens * top_k) // num_experts # Apply the imbalance factor. num_tokens_per_expert = int(num_tokens_per_expert * imbalance_factor) # And pad the number to the next power of 2. tile_tokens_dim = next_positive_power_of_2(num_tokens_per_expert) # Cap to 8-max_tile_tokens_dim tokens per CTA tile # as it's the range supported by the kernel. tile_tokens_dim = min(max(tile_tokens_dim, 8), 64) return tile_tokens_dim def swap_w13_to_w31(x: torch.Tensor) -> torch.Tensor: return ( x.reshape(-1, 2, x.shape[-2] // 2, x.shape[-1]).flip(dims=[1]).reshape(x.shape) ) def rotate_flashinfer_fp8_moe_weights( gemm1_weights: torch.Tensor, gemm2_weights: torch.Tensor ): from flashinfer import reorder_rows_for_gated_act_gemm, shuffle_matrix_a epilogue_tile_m = 128 num_experts = gemm1_weights.shape[0] hidden_size = gemm1_weights.shape[-1] intermediate_size = gemm1_weights.shape[1] // 2 # Reorder rows of W1 for fused gated activation gemm1_weights_fp8_interleaved = [] for i in range(num_experts): gemm1_weights_fp8_interleaved.append( reorder_rows_for_gated_act_gemm(gemm1_weights[i]) ) # Stack weights and scales for all experts gemm1_weights_fp8_interleaved = torch.stack(gemm1_weights_fp8_interleaved).reshape( num_experts, 2 * intermediate_size, hidden_size ) # Shuffle weights and scaling factors for transposed mma output gemm1_weights_fp8_shuffled = [] gemm2_weights_fp8_shuffled = [] for i in range(num_experts): gemm1_weights_fp8_shuffled.append( shuffle_matrix_a( gemm1_weights_fp8_interleaved[i].view(torch.uint8), epilogue_tile_m ) ) gemm2_weights_fp8_shuffled.append( shuffle_matrix_a(gemm2_weights[i].view(torch.uint8), epilogue_tile_m) ) # Stack weights for all experts gemm1_weights.data = torch.stack(gemm1_weights_fp8_shuffled).view( torch.float8_e4m3fn ) gemm2_weights.data = torch.stack(gemm2_weights_fp8_shuffled).view( torch.float8_e4m3fn ) def apply_flashinfer_per_tensor_scale_fp8( layer: torch.nn.Module, hidden_states: torch.Tensor, router_logits: torch.Tensor, routing_bias: torch.Tensor | None, top_k: int, num_expert_group: int | None, topk_group: int | None, global_num_experts: int, apply_router_weight_on_input: bool, ) -> torch.Tensor: from flashinfer.fused_moe import RoutingMethodType import vllm.model_executor.layers.fused_moe.flashinfer_trtllm_moe # noqa: E501, F401 assert layer.output1_scales_scalar is not None, ( "Expected output1_scales_scalar to be initialized" ) assert layer.output1_scales_scalar is not None, ( "Expected output1_scales_gate_scalar to be initialized" ) assert layer.output1_scales_scalar is not None, ( "Expected output2_scales_scalar to be initialized" ) from vllm.model_executor.models.llama4 import Llama4MoE assert layer.custom_routing_function == Llama4MoE.custom_routing_function, ( "FusedMoE flashinfer kernels are only supported for Llama4" ) return torch.ops.vllm.flashinfer_fused_moe_per_tensor_scale_fp8( routing_logits=router_logits, routing_bias=routing_bias, hidden_states=hidden_states, input_scale=layer.w13_input_scale, gemm1_weights=layer.w13_weight, gemm2_weights=layer.w2_weight, output1_scales_scalar=layer.output1_scales_scalar, output1_scales_gate_scalar=layer.output1_scales_gate_scalar, output2_scales_scalar=layer.output2_scales_scalar, num_experts=global_num_experts, top_k=top_k, num_expert_group=num_expert_group, topk_group=topk_group, intermediate_size=layer.intermediate_size_per_partition, local_expert_offset=layer.ep_rank * layer.local_num_experts, local_num_experts=layer.local_num_experts, use_routing_scales_on_input=apply_router_weight_on_input, routing_method_type=RoutingMethodType.Llama4, ) def get_moe_scaling_factors( input_scale: torch.Tensor, gemm1_weights_scale: torch.Tensor, activation_scale: torch.Tensor, gemm2_weights_scale: torch.Tensor, ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: output1_scales_scalar = gemm1_weights_scale * input_scale * (1.0 / activation_scale) output1_scales_gate_scalar = gemm1_weights_scale * input_scale output2_scales_scalar = activation_scale * gemm2_weights_scale return output1_scales_scalar, output1_scales_gate_scalar, output2_scales_scalar def register_moe_scaling_factors(layer: torch.nn.Module) -> None: output1_scales, output1_gate_scales, output2_scales = get_moe_scaling_factors( layer.w13_input_scale, layer.w13_weight_scale, layer.w2_input_scale, layer.w2_weight_scale, ) layer.register_parameter( "output1_scales_scalar", torch.nn.Parameter(output1_scales, requires_grad=False) ) layer.register_parameter( "output1_scales_gate_scalar", torch.nn.Parameter(output1_gate_scales, requires_grad=False), ) layer.register_parameter( "output2_scales_scalar", torch.nn.Parameter(output2_scales, requires_grad=False) ) layer.register_parameter( "w2_input_scale_inv", torch.nn.Parameter(1.0 / layer.w2_input_scale, requires_grad=False), ) def build_flashinfer_fp8_cutlass_moe_prepare_finalize( moe: FusedMoEConfig | None, use_deepseek_fp8_block_scale: bool = False ) -> mk.FusedMoEPrepareAndFinalize: """Create a FlashInfer CUTLASS fused-MoE prepare finalize kernel""" use_dp = moe.moe_parallel_config.dp_size > 1 if moe is not None else False # Propagate block-scale flag so prepare/finalize can skip act quantization # and inform the kernel to consume per-block weight scales. return create_flashinfer_prepare_finalize( use_dp, use_deepseek_fp8_block_scale=use_deepseek_fp8_block_scale ) def select_cutlass_fp8_gemm_impl( moe: FusedMoEConfig | None, quant_config: FusedMoEQuantConfig, out_dtype: torch.dtype | None = None, use_deepseek_fp8_block_scale: bool = False, ) -> mk.FusedMoEPermuteExpertsUnpermute: """Return a GEMM *experts* implementation for fused-MoE layers""" if moe is not None: return FlashInferExperts( out_dtype=moe.in_dtype, quant_config=quant_config, ep_rank=moe.moe_parallel_config.ep_rank, ep_size=moe.moe_parallel_config.ep_size, tp_rank=moe.moe_parallel_config.tp_rank, tp_size=moe.moe_parallel_config.tp_size, use_deepseek_fp8_block_scale=use_deepseek_fp8_block_scale, ) assert out_dtype is not None, "If moe config is None, out_dtype must be passed" return FlashInferExperts( out_dtype=out_dtype, quant_config=quant_config, use_deepseek_fp8_block_scale=use_deepseek_fp8_block_scale, ) def flashinfer_cutlass_moe_fp8( hidden_states: torch.Tensor, layer: torch.nn.Module, topk_weights: torch.Tensor, topk_ids: torch.Tensor, inplace: bool = False, activation: str = "silu", global_num_experts: int = -1, expert_map: torch.Tensor | None = None, apply_router_weight_on_input: bool = False, use_deepseek_fp8_block_scale: bool = False, moe: FusedMoEConfig | None = None, ) -> torch.Tensor: quant_config = layer.quant_method.get_fused_moe_quant_config(layer) assert quant_config is not None # Construct modular kernel with block-scale support when requested. fused_experts = mk.FusedMoEModularKernel( build_flashinfer_fp8_cutlass_moe_prepare_finalize( moe=moe, use_deepseek_fp8_block_scale=use_deepseek_fp8_block_scale ), select_cutlass_fp8_gemm_impl( moe=moe, quant_config=quant_config, out_dtype=hidden_states.dtype, use_deepseek_fp8_block_scale=use_deepseek_fp8_block_scale, ), moe_parallel_config=layer.moe_parallel_config, ) return fused_experts( hidden_states, layer.w13_weight, layer.w2_weight, topk_weights, topk_ids, inplace=inplace, activation=activation, global_num_experts=global_num_experts, expert_map=expert_map, apply_router_weight_on_input=apply_router_weight_on_input, ) def get_flashinfer_moe_backend() -> FlashinferMoeBackend: backend_map = { "throughput": FlashinferMoeBackend.CUTLASS, "latency": FlashinferMoeBackend.TENSORRT_LLM, "masked_gemm": FlashinferMoeBackend.CUTEDSL, } flashinfer_moe_backend = envs.VLLM_FLASHINFER_MOE_BACKEND if flashinfer_moe_backend in backend_map: if ( flashinfer_moe_backend == "latency" and not current_platform.is_device_capability_family(100) ): logger.info_once( "Flashinfer TRTLLM MOE backend is only supported on " "SM100 and later, using CUTLASS backend instead", scope="local", ) return FlashinferMoeBackend.CUTLASS return backend_map[flashinfer_moe_backend] elif current_platform.is_device_capability(90): return FlashinferMoeBackend.CUTLASS raise ValueError( f"Unknown flashinfer moe backend: {flashinfer_moe_backend!r}. " f"Expected one of {list(backend_map.keys())}." ) def is_flashinfer_supporting_global_sf(backend: FlashinferMoeBackend | None) -> bool: # TODO(shuw@nvidia): Update when new backends are added. backends_supporting_global_sf = ( FlashinferMoeBackend.CUTLASS, FlashinferMoeBackend.TENSORRT_LLM, ) return backend in backends_supporting_global_sf
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/__init__.py
vllm/model_executor/layers/quantization/utils/__init__.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from .layer_utils import replace_parameter, update_tensor_inplace __all__ = ["update_tensor_inplace", "replace_parameter"]
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/gptq_utils.py
vllm/model_executor/layers/quantization/utils/gptq_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Mapping from copy import deepcopy from fractions import Fraction from types import MappingProxyType from typing import TYPE_CHECKING import regex as re import torch from vllm.model_executor.layers.linear import LinearBase, UnquantizedLinearMethod from vllm.model_executor.layers.vocab_parallel_embedding import ( ParallelLMHead, UnquantizedEmbeddingMethod, ) if TYPE_CHECKING: from ..gptq import GPTQConfig from ..gptq_marlin import GPTQMarlinConfig else: GPTQConfig = object GPTQMarlinConfig = object # Match dynamic rules with module name (prefix) and override quantize # config if module (prefix) matches a rule def override_config(config: GPTQConfig | GPTQMarlinConfig, prefix: str): weight_bits = get_dynamic_override(config, prefix, "bits", config.weight_bits) if isinstance(weight_bits, int): config.weight_bits = weight_bits group_size = get_dynamic_override(config, prefix, "group_size", config.group_size) if isinstance(group_size, int): config.group_size = group_size desc_act = get_dynamic_override(config, prefix, "desc_act", config.desc_act) if isinstance(desc_act, bool): config.desc_act = desc_act config.pack_factor = Fraction(32, config.weight_bits) # packed into int32 if config.get_name() == "gptq_marlin": assert isinstance(config, GPTQMarlinConfig) is_sym = get_dynamic_override(config, prefix, "sym", config.is_sym) if isinstance(is_sym, bool): config.is_sym = is_sym if (config.weight_bits, config.is_sym) not in config.TYPE_MAP: raise ValueError( "Unsupported quantization config: " f"bits={config.weight_bits}, sym={config.is_sym}" ) config.quant_type = config.TYPE_MAP[(config.weight_bits, config.is_sym)] elif config.get_name() == "gptq": assert isinstance(config, GPTQConfig) if config.weight_bits not in [2, 3, 4, 8]: raise ValueError( "Currently, only 2/3/4/8-bit weight quantization is " f"supported for GPTQ, but got {config.weight_bits} bits." ) def get_dynamic_override( config: GPTQConfig | GPTQMarlinConfig, layer_name: str, key: str | None = None, default_value: int | bool | None = None, ) -> dict | int | bool | None: for pattern, pattern_dict in config.dynamic.items(): # Negative match: matched modules are excluded from quantized init if pattern.startswith("-:"): if re.match(pattern.removeprefix("-:"), layer_name): return False # Positive match: matched modules have quant properties overrides # base quant config elif re.match(pattern.removeprefix("+:"), layer_name): if key is None: return pattern_dict else: return pattern_dict.get(key, default_value) return default_value def is_layer_gptq_quantized( prefix: str, quantized_layers: list[str], fused_mapping: Mapping[str, list[str]] = MappingProxyType({}), ) -> bool: # prefix: model.layers.0.self_attn.q_proj # proj_name: q_proj # GPTQ's `modules_in_block_to_quantize`: # Substr: ["self_attn.k_proj", "self_attn.v_proj", "self_attn.q_proj"] # Full prefix ["model.layers.0.self_attn.q_proj"] proj_name = prefix.split(".")[-1] # Fused layers like gate_up_proj or qkv_proj will not be fused # in the safetensors checkpoint. So, we convert the name # from the fused version to unfused + check to make sure that # each shard of the fused layer has the same scheme. if proj_name in fused_mapping: shard_prefixes = [ prefix.replace(proj_name, shard_proj_name) for shard_proj_name in fused_mapping[proj_name] ] is_quantized = None for shard_prefix in shard_prefixes: is_shard_quantized = any( layer in shard_prefix for layer in quantized_layers ) if is_quantized is None: is_quantized = is_shard_quantized elif is_shard_quantized != is_quantized: raise ValueError( f"Detected some but not all shards of {prefix} " "are quantized. All shards of fused layers " "to have the same precision." ) else: is_quantized = any(layer in prefix for layer in quantized_layers) assert is_quantized is not None return is_quantized def get_linear_quant_method( config: GPTQConfig | GPTQMarlinConfig, layer: torch.nn.Module, prefix: str, linear_method_cls: type, ): cloned_config = deepcopy(config) parallel_lm_head_quantized = ( isinstance(layer, ParallelLMHead) and cloned_config.lm_head_quantized ) if isinstance(layer, LinearBase) or parallel_lm_head_quantized: is_layer_quantized = is_layer_gptq_quantized( prefix=prefix, quantized_layers=cloned_config.modules_in_block_to_quantize, fused_mapping=cloned_config.packed_modules_mapping, ) # False = skip module, None = no override, else = Positive match if get_dynamic_override( # noqa: E712 cloned_config, # noqa: E712 layer_name=prefix, ) == False or (not is_layer_quantized): # noqa: E712 if parallel_lm_head_quantized: return UnquantizedEmbeddingMethod() return UnquantizedLinearMethod() if prefix: # Dynamic per module/layer rules may override base config override_config(cloned_config, prefix=prefix) return linear_method_cls(cloned_config) return None
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/quant_utils.py
vllm/model_executor/layers/quantization/utils/quant_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """This file is used for /tests and /benchmarks""" from collections.abc import Callable, Mapping from dataclasses import dataclass from types import MappingProxyType from typing import ClassVar, NamedTuple import numpy import torch from torch import fx from vllm._custom_ops import cutlass_scaled_mm_supports_fp4 from vllm.platforms import current_platform from vllm.scalar_type import ScalarType, scalar_types FP8_DTYPE = current_platform.fp8_dtype() FP4_DTYPE = torch.uint8 # Use proxy as NamedTuple direct subclasses cannot have static members class _GroupShape(NamedTuple): row: int col: int class GroupShape(_GroupShape): """ This class describes the quantization group shape. It includes static members for common shapes (per-tensor, per-token). """ # Aliases for common quantization group shapes PER_TENSOR: ClassVar["GroupShape"] PER_TOKEN: ClassVar["GroupShape"] def is_per_tensor(self) -> bool: return self.row == -1 and self.col == -1 def is_per_token(self) -> bool: return self.row == 1 and self.col == -1 def is_per_group(self) -> bool: return self.row == 1 and self.col >= 1 GroupShape.PER_TENSOR = GroupShape(-1, -1) GroupShape.PER_TOKEN = GroupShape(1, -1) @dataclass(frozen=True) class ScaleDesc: """ Class for describing a single quantization scaling factor. dtype: data type of the scale static: static scale if True, dynamic if False group_shape: group shape of the scale """ dtype: torch.dtype static: bool group_shape: GroupShape def __str__(self): group_shape = ( "per_tensor" if self.group_shape == GroupShape.PER_TENSOR else ( "per_token" if self.group_shape == GroupShape.PER_TOKEN else str(self.group_shape) ) ) return ( f"{fx.graph.dtype_abbrs[self.dtype]}," f"{'static' if self.static else 'dynamic'},{group_shape}" ) @dataclass(frozen=True) class QuantKey: """ Class for identifying the type of quantization. dtype: quantized data type scale: scale descriptor scale2: second-level scale descriptor symmetric: symmetric if True, asymmetric if False """ dtype: torch.dtype scale: ScaleDesc scale2: ScaleDesc | None = None symmetric: bool = True def __str__(self): scale2_str = f"scale2({self.scale2})," if self.scale2 else "" return ( f"QuantKey({fx.graph.dtype_abbrs[self.dtype]}," f"scale({self.scale}),{scale2_str}" f"{'a' if not self.symmetric else ''}symmetric)" ) kStaticTensorScale = ScaleDesc(torch.float32, True, GroupShape.PER_TENSOR) kFp8StaticTensorSym = QuantKey(FP8_DTYPE, kStaticTensorScale, symmetric=True) kDynamicTensorScale = ScaleDesc(torch.float32, False, GroupShape.PER_TENSOR) kFp8DynamicTensorSym = QuantKey(FP8_DTYPE, kDynamicTensorScale, symmetric=True) kDynamicTokenScale = ScaleDesc(torch.float32, False, GroupShape.PER_TOKEN) kFp8DynamicTokenSym = QuantKey(FP8_DTYPE, kDynamicTokenScale, symmetric=True) kNvfp4GroupScale = ScaleDesc(FP8_DTYPE, False, GroupShape(1, 16)) kNvfp4Quant = QuantKey(FP4_DTYPE, scale=kNvfp4GroupScale, scale2=kStaticTensorScale) kDynamic128Scale = ScaleDesc(torch.float32, False, GroupShape(1, 128)) kFp8Dynamic128Sym = QuantKey(FP8_DTYPE, kDynamic128Scale, symmetric=True) kDynamic64Scale = ScaleDesc(torch.float32, False, GroupShape(1, 64)) kFp8Dynamic64Sym = QuantKey(FP8_DTYPE, kDynamic64Scale, symmetric=True) # Normalize the group_shape to the full extent for any dims that are -1 def _normalize_quant_group_shape(x: torch.Tensor, group_shape: GroupShape): # -1 means full extent return ( group_shape[0] if group_shape[0] > 0 else x.shape[-2], group_shape[1] if group_shape[1] > 0 else x.shape[-1], ) # Useful when treating N-dimensional group scaling as extended numpy-style # broadcasting in numpy simply stretches dimensions with an extent of 1 to match # the target shape by repeating the data along that dimension (broadcasting) # , we extend these semantics to say if the extent of a dimension in the # source shape is not 1 and does not match the target shape we repeat each # element along that dimension src_shape[dim] // target_shape[dim] times # example if we have: # a = [[1, 2], and target_shape = (2, 4) # [3, 4]] # then we would expand a to: # a = [[1, 1, 2, 2], # [3, 3, 4, 4]] # NOTE this function does not explicitly broadcast dimensions # with an extent of 1, since this can be done implicitly by pytorch def group_broadcast(t, shape): for i, s in enumerate(shape): if t.shape[i] != s and t.shape[i] != 1: assert s % t.shape[i] == 0 t = ( t.unsqueeze(i + 1) .expand(*t.shape[: i + 1], s // t.shape[i], *t.shape[i + 1 :]) .flatten(i, i + 1) ) return t # Quantize assuming once scale per group of elements with shape group_shape, # example group shapes: # * (-1, -1) for per-tensor quantization # * (1, -1) for per-row quantization # * (-1, 1) for per-column quantization # * (128, 128) for 128x128 deepseek style block quantization # * (1, 128) for deepseek style activation quantization # (i.e. per-token-per-group) def scaled_quantize( x: torch.Tensor, group_shape: GroupShape, quant_dtype: torch.dtype, ) -> tuple[torch.Tensor, torch.Tensor]: group_shape = _normalize_quant_group_shape(x, group_shape) assert quant_dtype.is_floating_point, ( "currently `scaled_quantize` only supports floating point dtypes " "but could be extended to support other dtypes" ) finfo = torch.finfo(quant_dtype) # Reshape (M, N) into (BLK_M, BLOCK_SIZE_M, BLK_N, BLOCK_SIZE_N) assert x.ndim == 2 assert x.shape[0] % group_shape[0] == 0 and x.shape[1] % group_shape[1] == 0 blk_m, blk_n = x.shape[0] // group_shape[0], x.shape[1] // group_shape[1] x_blkd = x.reshape(blk_m, group_shape[0], blk_n, group_shape[1]) # Permute to (BLK_M, BLK_N, BLOCK_SIZE_M, BLOCK_SIZE_N) x_blkd_permd = x_blkd.permute(0, 2, 1, 3) # Flatten to (BLK_M, BLK_N, BLOCK_SIZE_M * BLOCK_SIZE_N) x_blkd_permd = x_blkd_permd.flatten(start_dim=2) # Compute scales min_val, max_val = x_blkd_permd.aminmax(dim=-1) amax = torch.maximum(min_val.abs(), max_val.abs()).clamp(min=1e-12) scale = finfo.max / amax # Apply scale and convert form: # (BLK_M, BLK_N, BLOCK_SIZE_M * BLOCK_SIZE_N) to (M, N) x_scl_sat = ( (x_blkd_permd * scale.unsqueeze(-1)) .clamp(min=finfo.min, max=finfo.max) .reshape(blk_m, blk_n, group_shape[0], group_shape[1]) .permute(0, 2, 1, 3) .reshape(x.shape) ) return x_scl_sat.to(quant_dtype).contiguous(), scale.float().reciprocal() # inverses `scaled_quantize` def scaled_dequantize( x_q: torch.Tensor, x_s: torch.Tensor, group_shape: GroupShape | None = None, out_dtype: torch.dtype = torch.float32, ) -> tuple[torch.Tensor, torch.Tensor]: if group_shape is not None: group_shape = _normalize_quant_group_shape(x_q, group_shape) if x_s.ndim == 0: # scalar x_s = x_s.unsqueeze(-1).unsqueeze(-1) # convert to (1, 1) tensor if x_s.ndim == 1: if group_shape is None: raise AssertionError( "if x_s is 1D tensor, group_shape must be provided otherwise " "its ambiguous which dimension to broadcast x_s to" ) # unsqueeze the scales for the dimension where we want to broadcast # across the full extent if group_shape[0] == x_q.shape[-2]: x_s = x_s.unsqueeze(-2) elif group_shape[1] == x_q.shape[-1]: x_s = x_s.unsqueeze(-1) else: raise AssertionError( "if x_s is a vector we should be broadcasting it to the full " "extent of one of the dimensions" ) if group_shape is not None: assert x_s.shape[-1] == x_q.shape[-1] // group_shape[1] assert x_s.shape[-2] == x_q.shape[-2] // group_shape[0] x_s = group_broadcast(x_s.to(torch.float32), x_q.shape) return (x_q.to(torch.float32) * x_s).to(out_dtype) def pack_quantized_values_into_int32( w_q: torch.Tensor, wtype: ScalarType, packed_dim: int = 0 ): # move dim to pack to the end perm = (*[i for i in range(len(w_q.shape)) if i != packed_dim], packed_dim) inv_perm = tuple(perm.index(i) for i in range(len(perm))) w_q_perm = w_q.permute(perm) pack_factor = 32 // wtype.size_bits mask = (1 << wtype.size_bits) - 1 new_shape_perm = list(w_q_perm.shape) assert w_q_perm.shape[-1] % pack_factor == 0 new_shape_perm[-1] //= pack_factor res = torch.zeros(new_shape_perm, dtype=torch.int32, device=w_q.device) for i in range(pack_factor): res |= (w_q_perm[..., i::pack_factor] & mask) << wtype.size_bits * i return res.permute(inv_perm) def unpack_quantized_values_into_int32( w_q: torch.Tensor, wtype: ScalarType, packed_dim: int = 0 ): # move dim to pack to the end perm = (*[i for i in range(len(w_q.shape)) if i != packed_dim], packed_dim) inv_perm = tuple(perm.index(i) for i in range(len(perm))) w_q_perm = w_q.permute(perm) pack_factor = 32 // wtype.size_bits mask = (1 << wtype.size_bits) - 1 new_shape_perm = list(w_q_perm.shape) new_shape_perm[-1] *= pack_factor res = torch.zeros(new_shape_perm, dtype=torch.int32, device=w_q.device) for i in range(pack_factor): res[..., i::pack_factor] = (w_q_perm >> wtype.size_bits * i) & mask return res.permute(inv_perm) def is_layer_skipped( prefix: str, ignored_layers: list[str], fused_mapping: Mapping[str, list[str]] = MappingProxyType({}), *, skip_with_substr: bool = False, ) -> bool: def prefix_full_match(prefix: str, ignored_layers: list[str]) -> bool: return prefix in ignored_layers # For case like: ignored_layers = ["self_attn"] def substr_match(prefix: str, ignored_layers: list[str]) -> bool: return any(layer in prefix for layer in ignored_layers) match_func = substr_match if skip_with_substr else prefix_full_match # prefix: model.layers.0.self_attn.q_proj # proj_name: q_proj proj_name = prefix.split(".")[-1] # Fused layers like gate_up_proj or qkv_proj will not be fused # in the safetensors checkpoint. So, we convert the name # from the fused version to unfused + check to make sure that # each shard of the fused layer has the same scheme. if proj_name in fused_mapping: shard_prefixes = [ prefix.replace(proj_name, shard_proj_name) for shard_proj_name in fused_mapping[proj_name] ] is_skipped = None for shard_prefix in shard_prefixes: is_shard_skipped = match_func(shard_prefix, ignored_layers) if is_skipped is None: is_skipped = is_shard_skipped elif is_shard_skipped != is_skipped: raise ValueError( f"Detected some but not all shards of {prefix} " "are quantized. All shards of fused layers " "to have the same precision." ) elif "experts" in prefix and not skip_with_substr: expert_ignore_layers = filter( lambda layer_name: "experts" in layer_name, ignored_layers ) return any( prefix in layer_name if not skip_with_substr else layer_name in prefix for layer_name in expert_ignore_layers ) else: is_skipped = match_func(prefix, ignored_layers) assert is_skipped is not None return is_skipped def get_pack_factor(num_bits): assert 32 % num_bits == 0, f"Unsupported num_bits = {num_bits}" return 32 // num_bits def permute_rows( q_w: torch.Tensor, w_ref: torch.Tensor, group_size: int, test_perm: torch.Tensor | None = None, ): assert q_w.shape == w_ref.shape orig_device = q_w.device k_size, _ = q_w.shape g_idx = torch.zeros((k_size,), dtype=torch.int32) for i in range(k_size): g_idx[i] = i // group_size # Simulate act_order by doing a random permutation on K rand_perm = test_perm if test_perm is not None else torch.randperm(k_size) g_idx = g_idx[rand_perm].contiguous() q_w = q_w[rand_perm, :].contiguous() w_ref = w_ref[rand_perm, :].contiguous() return ( w_ref.to(device=orig_device), q_w.to(device=orig_device), g_idx.to(device=orig_device), rand_perm.to(device=orig_device), ) def quantize_weights( w: torch.Tensor, quant_type: ScalarType, group_size: int | None, zero_points: bool = False, ref_zero_points_after_scales: bool = False, ): assert quant_type.is_integer(), ( "Floating point quantization may work but has not been tested" ) assert not zero_points or group_size is not None, ( "to have group zero points, group_size must be provided " "(-1 group_size is channelwise)" ) orig_device = w.device orig_type = w.dtype size_k, size_n = w.shape assert w.is_floating_point(), "w must be float" if group_size == -1: group_size = size_k # Reshape to [groupsize, -1] if group_size is not None and group_size < size_k: w = w.reshape((-1, group_size, size_n)) w = w.permute(1, 0, 2) w = w.reshape((group_size, -1)) # Compute scale for each group max_val = torch.max(w, 0, keepdim=True).values min_val = torch.min(w, 0, keepdim=True).values max_q_val = quant_type.max() min_q_val = quant_type.min() w_s = torch.Tensor([1.0]).to(w.device) # unscaled case maybe_w_zp = None if group_size is not None: if zero_points: assert not quant_type.is_signed() and quant_type.max() > 0 w_s = (max_val - min_val).clamp(min=1e-5) / quant_type.max() maybe_w_zp = ( torch.round(torch.abs(min_val / w_s)).clamp(min_q_val, max_q_val).int() ) else: # If the bias is such that there are no possible negative/positive # values, set the max value to inf to avoid divide by 0 w_s = torch.max( abs(max_val / (max_q_val if max_q_val != 0 else torch.inf)), abs(min_val / (min_q_val if min_q_val != 0 else torch.inf)), ) # Quantize w_q = torch.round(w / w_s).int() + (maybe_w_zp if zero_points else 0) w_q = torch.clamp(w_q, min_q_val, max_q_val) # Compute ref (dequantized) # For some kernels (namely Machete) the zero-points are applied after the # scales are applied, for this case computing the reference in similar way # allows us to use tighter error tolerances in our unit tests. if ref_zero_points_after_scales and maybe_w_zp is not None: w_ref = w_q.to(orig_type) * w_s - maybe_w_zp.to(orig_type) * w_s else: w_ref = (w_q - (maybe_w_zp if zero_points else 0)).to(orig_type) * w_s if quant_type.has_bias(): w_q += quant_type.bias # Restore original shapes if group_size is not None and group_size < size_k: def reshape_w(w): w = w.reshape((group_size, -1, size_n)) w = w.permute(1, 0, 2) w = w.reshape((size_k, size_n)).contiguous() return w w_q = reshape_w(w_q) w_ref = reshape_w(w_ref) w_s = w_s.reshape((-1, size_n)).contiguous() if maybe_w_zp is not None: maybe_w_zp = maybe_w_zp.reshape((-1, size_n)).contiguous() maybe_w_zp = maybe_w_zp.to(device=orig_device) return ( w_ref.to(device=orig_device), w_q.to(device=orig_device), w_s if group_size is not None else None, maybe_w_zp, ) SUPPORTED_GPTQ_QUANT_TYPES = [scalar_types.uint4b8, scalar_types.uint8b128] SUPPORTED_GROUP_SIZES = [-1, 32, 64, 128] def gptq_quantize_weights( w: torch.Tensor, quant_type: ScalarType, group_size: int, act_order: bool, test_perm: torch.Tensor | None = None, ): size_k, _ = w.shape assert w.is_floating_point(), "w must be float" assert quant_type in SUPPORTED_GPTQ_QUANT_TYPES, ( f"Unsupported gptq type = {quant_type}" ) assert group_size in SUPPORTED_GROUP_SIZES + [size_k], ( f"Unsupported groupsize = {group_size}" ) w_ref, w_q, w_s, _ = quantize_weights(w, quant_type, group_size) # Apply act_order g_idx = torch.empty(0, dtype=torch.int, device=w.device) rand_perm = torch.empty(0, dtype=torch.int, device=w.device) if act_order: assert group_size < size_k, ( "For act_order, groupsize = {} must be less than size_k = {}".format( group_size, size_k ) ) w_ref, w_q, g_idx, rand_perm = permute_rows(w_q, w_ref, group_size, test_perm) return w_ref, w_q, w_s, g_idx, rand_perm def sort_weights(q_w: torch.Tensor, g_idx: torch.Tensor): orig_device = q_w.device sort_indices = torch.argsort(g_idx).to(dtype=torch.int32) # Sort based on g_idx g_idx = g_idx[sort_indices].contiguous() q_w = q_w[sort_indices, :].contiguous() return ( q_w.to(device=orig_device), g_idx.to(device=orig_device), sort_indices.to(device=orig_device), ) def pack_rows( q_w: torch.Tensor, num_bits: int, size_k: int, size_n: int, ): assert q_w.shape == (size_k, size_n) pack_factor = get_pack_factor(num_bits) assert size_k % pack_factor == 0 orig_device = q_w.device q_w = q_w.cpu().numpy().astype(numpy.uint32) q_res = numpy.zeros((size_k // pack_factor, size_n), dtype=numpy.uint32) for i in range(pack_factor): q_res |= q_w[i::pack_factor, :] << num_bits * i q_res = torch.from_numpy(q_res.astype(numpy.int32)).to(orig_device) return q_res def pack_cols( q_w: torch.Tensor, num_bits: int, size_k: int, size_n: int, ): assert q_w.shape == (size_k, size_n) pack_factor = get_pack_factor(num_bits) assert size_n % pack_factor == 0 orig_device = q_w.device q_w = q_w.cpu().numpy().astype(numpy.uint32) q_res = numpy.zeros((size_k, size_n // pack_factor), dtype=numpy.uint32) for i in range(pack_factor): q_res |= q_w[:, i::pack_factor] << num_bits * i q_res = torch.from_numpy(q_res.astype(numpy.int32)).to(orig_device) q_res = q_res.contiguous() return q_res def unpack_cols( packed_q_w: torch.Tensor, num_bits: int, size_k: int, size_n: int, ): pack_factor = get_pack_factor(num_bits) assert size_n % pack_factor == 0 assert packed_q_w.shape == (size_k, size_n // pack_factor), ( "packed_q_w.shape = {} size_k = {}, size_n = {} pack_Factor = {}".format( packed_q_w.shape, size_k, size_n, pack_factor ) ) orig_device = packed_q_w.device packed_q_w_cpu = packed_q_w.cpu().numpy().astype(numpy.uint32) q_res = numpy.zeros((size_k, size_n), dtype=numpy.uint32) mask = (1 << num_bits) - 1 for i in range(pack_factor): vals = packed_q_w_cpu & mask packed_q_w_cpu >>= num_bits q_res[:, i::pack_factor] = vals q_res = torch.from_numpy(q_res.astype(numpy.int32)).to(orig_device) q_res = q_res.contiguous() return q_res def gptq_pack( q_w: torch.Tensor, num_bits: int, size_k: int, size_n: int, ): return pack_rows(q_w, num_bits, size_k, size_n) def awq_pack( q_w: torch.Tensor, num_bits: int, size_k: int, size_n: int, ): assert q_w.shape == (size_k, size_n) # Interleave column dim (for the dequantize code) and pack it to int32 if num_bits == 4: interleave = numpy.array([0, 2, 4, 6, 1, 3, 5, 7]) elif num_bits == 8: interleave = numpy.array([0, 2, 1, 3]) else: raise Exception("num_bits must be 4 or 8, got {}".format(num_bits)) q_w = q_w.reshape((-1, len(interleave)))[:, interleave].ravel() q_w = q_w.reshape((-1, size_n)).contiguous() return pack_cols(q_w, num_bits, size_k, size_n) def swizzle_blockscale(scale: torch.Tensor) -> torch.Tensor: """ Pad and block-interleave the FP4 block-scales so that they match the data layout expected by the CUTLASS / FlashInfer kernels. Parameters ---------- scale: torch.Tensor Returns ------- torch.Tensor The swizzled tensor with the same logical shape as *scale*. """ assert scale.dtype == torch.float8_e4m3fn, ( "swizzle_blockscale expects the input tensor to be in " "torch.float8_e4m3fn format." ) scale_ndim = scale.ndim if scale_ndim == 2: scale = scale.unsqueeze(0) # (1, M, K) assert scale.ndim == 3, "Expected a 2-D or 3-D tensor for block scales." B, M, K = scale.shape def _round_up(x: int, m: int) -> int: return (x + m - 1) // m * m M_padded = _round_up(M, 128) K_padded = _round_up(K, 4) padded = torch.zeros( (B, M_padded, K_padded), dtype=scale.dtype, device=scale.device ) padded[:B, :M, :K] = scale # Reshape / permute to the layout required by the kernel. padded = padded.reshape(B, M_padded // 128, 4, 32, K_padded // 4, 4) swizzled = padded.permute(0, 1, 4, 3, 2, 5).contiguous().cuda() if scale_ndim == 2: return swizzled.reshape(M_padded, K_padded) return swizzled.reshape(B, M_padded, K_padded) def cutlass_fp4_supported() -> bool: if not current_platform.is_cuda(): return False capability_tuple = current_platform.get_device_capability() capability = -1 if capability_tuple is None else capability_tuple.to_int() return cutlass_scaled_mm_supports_fp4(capability) def convert_bf16_scales_to_fp8( quant_fp8: Callable, scales: torch.Tensor ) -> tuple[torch.Tensor, torch.Tensor]: """ Convert a BF16 scale tensor into the pair of (fp8_scales, channel_scales) expected by W4A8 GEMM kernels. """ assert scales.is_contiguous(), ( f"scale tensor must be contiguous, got {scales.stride()=}" ) assert scales.is_cuda, "scales must be on gpu" orig_shape = scales.shape k_groups = orig_shape[-1] flat_scales = scales.view(-1, k_groups) fp8_scales, chan_scales = quant_fp8(flat_scales) fp8_scales = (fp8_scales.float() / 8.0).to(torch.float8_e4m3fn) chan_scales *= 8.0 # restore original shape fp8_scales = fp8_scales.view(orig_shape) chan_scales = chan_scales.view(orig_shape[:-1], -1) return fp8_scales, chan_scales def convert_packed_uint4b8_to_signed_int4_inplace(t: torch.Tensor) -> torch.Tensor: """ Convert int4b8 (packed to int32) to signed int4 """ assert t.is_cuda, "tensor must be on gpu" assert t.dtype == torch.int32, f"expected int32 packed weights but got {t.dtype}" # loop through the 8 4-bit nibbles in each int32 entry for i in range(8): shift = 4 * i # extract the i-th 4-bit nibble nib = (t >> shift) & 0xF # clear the original nibble by masking out t &= ~(0xF << shift) # convert int4b8 [0..15] to signed int4 [-8..7] by subtracting 8 # and update in-place t |= ((nib - 8) & 0xF) << shift return t
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/fp8_utils.py
vllm/model_executor/layers/quantization/utils/fp8_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # Adapted from https://github.com/sgl-project/sglang/pull/2575 import functools import json import os from collections.abc import Callable, Sequence from typing import Any import torch import vllm.envs as envs from vllm import _custom_ops as ops from vllm._aiter_ops import rocm_aiter_ops from vllm.logger import init_logger from vllm.model_executor.layers.quantization.input_quant_fp8 import QuantFP8 from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape from vllm.model_executor.layers.quantization.utils.w8a8_utils import ( CUTLASS_BLOCK_FP8_SUPPORTED, ) from vllm.model_executor.parameter import ( BlockQuantScaleParameter, ChannelQuantScaleParameter, PerTensorScaleParameter, ) from vllm.model_executor.utils import replace_parameter from vllm.platforms import current_platform from vllm.triton_utils import tl, triton from vllm.utils.deep_gemm import ( DeepGemmQuantScaleFMT, fp8_gemm_nt, is_deep_gemm_e8m0_used, is_deep_gemm_supported, should_use_deepgemm_for_fp8_linear, transform_sf_into_required_layout, ) from vllm.utils.torch_utils import direct_register_custom_op logger = init_logger(__name__) def is_fp8(x: torch.dtype | torch.Tensor) -> bool: if isinstance(x, torch.Tensor): x = x.dtype return x == torch.float8_e4m3fn or x == torch.float8_e4m3fnuz # We need to pass in the is_hopper flag as argument because the function # current_platform.is_device_capability() is not supported by Torch compiler. def cutlass_scaled_mm( A: torch.Tensor, B: torch.Tensor, As: torch.Tensor, Bs: torch.Tensor, block_size: list[int], output_dtype: torch.dtype = torch.float16, ) -> torch.Tensor: return ops.cutlass_scaled_mm( A, B.T, out_dtype=output_dtype, scale_a=As, scale_b=Bs.T, ) # TODO we should be able to change the type of block_size to GroupShape # after we resolve GroupShape compilation issue # https://github.com/vllm-project/vllm/issues/25270 def _w8a8_triton_block_scaled_mm_func( qx: torch.Tensor, weight: torch.Tensor, x_scale: torch.Tensor, weight_scale: torch.Tensor, block_size: list[int], output_dtype: torch.dtype, ) -> torch.Tensor: return w8a8_triton_block_scaled_mm( qx, weight, x_scale, weight_scale, block_size, output_dtype ) def _w8a8_triton_block_scaled_mm_fake( qx: torch.Tensor, weight: torch.Tensor, x_scale: torch.Tensor, weight_scale: torch.Tensor, block_size: list[int], output_dtype: torch.dtype, ) -> torch.Tensor: return torch.empty( (qx.size(0), weight.size(0)), dtype=output_dtype, device=qx.device ) direct_register_custom_op( "w8a8_triton_block_scaled_mm_func", _w8a8_triton_block_scaled_mm_func, fake_impl=_w8a8_triton_block_scaled_mm_fake, ) def _padded_cutlass( qx: torch.Tensor, weight: torch.Tensor, x_scale: torch.Tensor, weight_scale: torch.Tensor, block_size: list[int], output_dtype: torch.dtype, ) -> torch.Tensor: pad_multiple = 4 dim = qx.shape[0] padded = ( dim if dim % pad_multiple == 0 else dim + pad_multiple - (dim % pad_multiple) ) has_pad = padded > dim if has_pad: padded_shape = [padded, *qx.shape[1:]] padded_qx = torch.zeros(padded_shape, device=qx.device, dtype=qx.dtype) padded_qx[0 : qx.shape[0], ...].copy_(qx) padded_x_scale_shape = [*x_scale.shape[1:], padded] padded_x_scale = torch.ones( padded_x_scale_shape, device=x_scale.device, dtype=x_scale.dtype ).permute(-1, -2) padded_x_scale[0 : x_scale.shape[0], ...].copy_(x_scale) output = cutlass_scaled_mm( padded_qx, weight, padded_x_scale, weight_scale, block_size, output_dtype ) return output[0 : qx.shape[0], ...] else: return cutlass_scaled_mm( qx, weight, x_scale, weight_scale, block_size, output_dtype ) def _padded_cutlass_fake( qx: torch.Tensor, weight: torch.Tensor, x_scale: torch.Tensor, weight_scale: torch.Tensor, block_size: list[int], output_dtype: torch.dtype, ) -> torch.Tensor: return torch.empty( (qx.size(0), weight.size(0)), dtype=output_dtype, device=qx.device ) direct_register_custom_op( "padded_cutlass", _padded_cutlass, fake_impl=_padded_cutlass_fake, ) def _fp8_gemm_nt_op( q_input: torch.Tensor, input_scale: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, output: torch.Tensor, use_deep_gemm_e8m0: bool, ) -> None: fp8_gemm_nt( (q_input, input_scale), (weight, weight_scale), output, is_deep_gemm_e8m0_used=use_deep_gemm_e8m0, ) def _fp8_gemm_nt_op_fake( q_input: torch.Tensor, input_scale: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, output: torch.Tensor, use_deep_gemm_e8m0: bool, ) -> None: return None direct_register_custom_op( "fp8_gemm_nt_op", _fp8_gemm_nt_op, mutates_args=["output"], fake_impl=_fp8_gemm_nt_op_fake, ) def _triton_per_token_group_quant_fp8_impl( x: torch.Tensor, group_size: int, ) -> tuple[torch.Tensor, torch.Tensor]: return per_token_group_quant_fp8( x, group_size, column_major_scales=False, use_ue8m0=False ) def _triton_per_token_group_quant_fp8_fake( x: torch.Tensor, group_size: int, ) -> tuple[torch.Tensor, torch.Tensor]: M, N = x.shape x_fp8 = torch.empty((M, N), dtype=current_platform.fp8_dtype(), device=x.device) out_bs = torch.empty( ( M, (N + group_size - 1) // group_size, ), dtype=torch.float32, device=x.device, ) return x_fp8, out_bs direct_register_custom_op( "triton_per_token_group_quant_fp8", _triton_per_token_group_quant_fp8_impl, fake_impl=_triton_per_token_group_quant_fp8_fake, ) # TODO fix ROCm->Triton custom path: # https://github.com/vllm-project/vllm/issues/14397 class W8A8BlockFp8LinearOp: """ This class executes a Blocked FP8 linear layer using cutlass if supported and torch.scaled_mm otherwise. """ def __init__( self, weight_group_shape: GroupShape, act_quant_group_shape: GroupShape, cutlass_block_fp8_supported: bool = CUTLASS_BLOCK_FP8_SUPPORTED, use_aiter_and_is_supported: bool = False, ): self.weight_group_shape = weight_group_shape self.act_quant_group_shape = act_quant_group_shape self.is_deep_gemm_supported = is_deep_gemm_supported() self.is_hopper = current_platform.is_device_capability(90) self.use_deep_gemm_e8m0 = is_deep_gemm_e8m0_used() # Get the correct blockscale mul and input quant operations. # We can't use _dispatch_w8a8_blockscale_op to figure out if we want # to use deepgemm because we don't know the shape of weights (and # whether deepgemm supports it) at the init time. self.w8a8_blockscale_op, self.input_quant_op = ( self._dispatch_w8a8_blockscale_op( cutlass_block_fp8_supported, use_aiter_and_is_supported ) ) self.deepgemm_input_quant_op = ( QuantFP8( False, self.act_quant_group_shape, column_major_scales=True, use_ue8m0=self.use_deep_gemm_e8m0, ) if self.is_deep_gemm_supported else None ) def apply( self, input: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, input_scale: torch.Tensor | None = None, bias: torch.Tensor | None = None, ) -> torch.Tensor: assert input_scale is None # View input as 2D matrix for fp8 methods input_2d = input.view(-1, input.shape[-1]) output_shape = [*input.shape[:-1], weight.shape[0]] output_dtype = input.dtype if should_use_deepgemm_for_fp8_linear( output_dtype, weight, self.is_deep_gemm_supported ): output = self._run_deepgemm(input_2d, weight, weight_scale) else: output = self.w8a8_blockscale_op( input_2d, weight, weight_scale, input_scale ) if bias is not None: output = output + bias return output.to(dtype=input.dtype).view(*output_shape) def _run_deepgemm( self, input_2d: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, ) -> torch.Tensor: if DeepGemmQuantScaleFMT.from_oracle() == DeepGemmQuantScaleFMT.UE8M0: q_input, input_scale = per_token_group_quant_fp8_packed_for_deepgemm( input_2d, group_size=self.act_quant_group_shape.col, use_ue8m0=True, ) else: assert self.deepgemm_input_quant_op is not None q_input, input_scale = self.deepgemm_input_quant_op(input_2d) output = torch.empty( (q_input.shape[0], weight.shape[0]), dtype=torch.bfloat16, device=q_input.device, ) torch.ops.vllm.fp8_gemm_nt_op( q_input, input_scale, weight, weight_scale, output, self.use_deep_gemm_e8m0 ) return output def _run_cutlass( self, input_2d: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, input_scale: torch.Tensor | None = None, ) -> torch.Tensor: assert input_scale is None assert self.input_quant_op is not None q_input, input_scale = self.input_quant_op(input_2d) if self.is_hopper: return torch.ops.vllm.padded_cutlass( q_input, weight, input_scale, weight_scale, list(self.weight_group_shape), input_2d.dtype, ) else: return cutlass_scaled_mm( q_input, weight, input_scale, weight_scale, list(self.weight_group_shape), input_2d.dtype, ) def _run_aiter( self, input_2d: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, input_scale: torch.Tensor | None = None, ) -> torch.Tensor: assert self.act_quant_group_shape == GroupShape(1, 128) n, k = weight.shape use_triton = ( not current_platform.is_fp8_fnuz() and rocm_aiter_ops.is_triton_gemm_w8a8_tuned(n, k) ) if use_triton: gemm_a8w8_blockscale_op = rocm_aiter_ops.triton_gemm_a8w8_blockscale else: gemm_a8w8_blockscale_op = rocm_aiter_ops.gemm_a8w8_blockscale if input_scale is not None: q_input = input_2d elif use_triton: q_input, input_scale = torch.ops.vllm.triton_per_token_group_quant_fp8( input_2d, self.act_quant_group_shape.col, ) else: q_input, input_scale = rocm_aiter_ops.group_fp8_quant( input_2d, self.act_quant_group_shape.col ) return gemm_a8w8_blockscale_op( q_input, weight, input_scale, weight_scale, list(self.weight_group_shape), output_dtype=input_2d.dtype, ) def _run_triton( self, input_2d: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, input_scale: torch.Tensor | None = None, ) -> torch.Tensor: assert input_scale is None assert self.input_quant_op is not None q_input, input_scale = self.input_quant_op(input_2d) return torch.ops.vllm.w8a8_triton_block_scaled_mm_func( q_input, weight, input_scale, weight_scale, list(self.weight_group_shape), input_2d.dtype, ) def _dispatch_w8a8_blockscale_op( self, use_cutlass: bool, use_aiter_and_is_supported: bool, ) -> tuple[ Callable[ [ torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor | None, ], torch.Tensor, ], QuantFP8 | None, ]: if use_cutlass: return self._run_cutlass, ( QuantFP8( False, self.act_quant_group_shape, column_major_scales=True, use_ue8m0=False, ) ) if use_aiter_and_is_supported: return self._run_aiter, None return self._run_triton, ( QuantFP8( False, self.act_quant_group_shape, column_major_scales=False, use_ue8m0=False, ) ) def input_to_float8( x: torch.Tensor, dtype: torch.dtype | None = None ) -> tuple[torch.Tensor, torch.Tensor]: """This function quantizes input values to float8 values " "with tensor-wise quantization.""" dtype = current_platform.fp8_dtype() if dtype is None else dtype finfo = torch.finfo(dtype) min_val, max_val = x.aminmax() amax = torch.maximum(min_val.abs(), max_val.abs()).clamp(min=1e-12) scale = finfo.max / amax x_scl_sat = (x * scale).clamp(min=finfo.min, max=finfo.max) return x_scl_sat.to(dtype).contiguous(), scale.float().reciprocal() @triton.jit def _per_token_group_quant_fp8( # Pointers to inputs and output y_ptr, y_q_ptr, y_s_ptr, group_size, # Num columns of y y_num_columns, y_row_stride, # Avoid to divide zero eps, # Information for float8 fp8_min, fp8_max, use_ue8m0: tl.constexpr, # Meta-parameters BLOCK: tl.constexpr, ): """A Triton-accelerated function to perform per-token-group quantization on a tensor. This function converts the tensor values into float8 values. """ groups_per_row = y_num_columns // group_size # Map the program id to the row of X and Y it should compute. g_id = tl.program_id(0) row = g_id // groups_per_row row_g_id = g_id % groups_per_row # Ensure offset calculations use int64 to prevent overflow y_ptr_offset = (row.to(tl.int64) * y_row_stride) + ( row_g_id.to(tl.int64) * group_size ) y_ptr += y_ptr_offset y_q_ptr_offset = g_id.to(tl.int64) * group_size y_q_ptr += y_q_ptr_offset y_s_ptr += g_id cols = tl.arange(0, BLOCK) # N <= BLOCK mask = cols < group_size y = tl.load(y_ptr + cols, mask=mask, other=0.0).to(tl.float32) # Quant _absmax = tl.maximum(tl.max(tl.abs(y)), eps) scale_raw = _absmax / fp8_max y_s = tl.math.exp2(tl.ceil(tl.log2(scale_raw))) if use_ue8m0 else scale_raw y_q = tl.clamp(y / y_s, fp8_min, fp8_max).to(y_q_ptr.dtype.element_ty) tl.store(y_q_ptr + cols, y_q, mask=mask) tl.store(y_s_ptr, y_s) @triton.jit def _silu_mul_per_token_group_quant_fp8_colmajor( y_ptr, # [M, N] y_q_ptr, # [M, N // 2] y_s_ptr, # [M, (N // 2) // GROUP_SIZE] M, # num tokens N, # intermediate size # Stride y_s_col_stride: tl.int64, # Information for float8 eps, fp8_min, fp8_max, use_ue8m0: tl.constexpr, # Meta-parameters GROUP_SIZE: tl.constexpr, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, ): # TODO(varun) : Add expert_ids so we may early-exit no-op thread blocks. """ Each thread block (BLOCK_N) computes [BLOCK_M, GROUP_SIZE] act-mul outputs. Then the thread block quantizes the [BLOCK_M, GROUP_SIZE] block of values and fills the outputs tensors at the right positions. """ pid_m = tl.program_id(0) pid_n = tl.program_id(1) N_2 = N // 2 m_offset = pid_m * BLOCK_M n_offset = pid_n * BLOCK_N if m_offset >= M: return offs_n = tl.arange(0, BLOCK_N).to(tl.int64) offs_m = tl.arange(0, BLOCK_M).to(tl.int64) base_y_ptr = y_ptr + m_offset * N + n_offset act_in_ptrs = base_y_ptr + offs_m[:, None] * N + offs_n[None, :] act_in = tl.load(act_in_ptrs) mul_in = tl.load(act_in_ptrs + N_2) # silu & mul act_in = act_in.to(tl.float32) one_f32 = tl.cast(1, tl.float32) silu_out = (act_in / (one_f32 + tl.exp(-act_in))).to(y_ptr.dtype.element_ty) y = (silu_out * mul_in).to(tl.float32) # quant _absmax = tl.maximum(tl.max(tl.abs(y), axis=1), eps) scale_raw = _absmax / fp8_max y_s = tl.math.exp2(tl.ceil(tl.log2(scale_raw))) if use_ue8m0 else scale_raw y_s = tl.reshape(y_s, (BLOCK_M, 1)) y_q = tl.clamp(y / y_s, fp8_min, fp8_max).to(y_q_ptr.dtype.element_ty) # store y_q base_y_q_ptr = y_q_ptr + m_offset * N_2 + n_offset y_q_ptrs = base_y_q_ptr + offs_m[:, None] * N_2 + offs_n[None, :] tl.store(y_q_ptrs, y_q) # store y_s group_id = n_offset // GROUP_SIZE base_y_s_ptr = y_s_ptr + group_id * y_s_col_stride + m_offset y_s_ptrs = base_y_s_ptr + offs_m y_s = tl.reshape(y_s, (BLOCK_M,)) tl.store(y_s_ptrs, y_s) def silu_mul_per_token_group_quant_fp8_colmajor( input: torch.Tensor, # [M, N] output: torch.Tensor | None = None, # [M, N // 2] use_ue8m0: bool | None = None, eps: float = 1e-10, ): """ silu+mul + block-fp8 quant with group size 128. """ GROUP_SIZE = 128 assert input.ndim == 2 if output is not None: assert output.ndim == 2 assert input.size(0) % GROUP_SIZE == 0 assert input.size(1) % (GROUP_SIZE * 2) == 0 if use_ue8m0 is None: use_ue8m0 = is_deep_gemm_e8m0_used() M, N = input.size() N_2 = N // 2 fp8_dtype = current_platform.fp8_dtype() if output is None: output = torch.empty((M, N_2), dtype=fp8_dtype, device=input.device) output_scales = torch.empty( ((N_2 // GROUP_SIZE), M), dtype=torch.float32, device=input.device ).transpose(0, 1) BLOCK_M = 8 BLOCK_N = GROUP_SIZE assert M % BLOCK_M == 0 assert N_2 % BLOCK_N == 0 # Using the default value (240.0) from pytorch will cause accuracy # issue on dynamic quantization models. Here use 224.0 for fnuz on ROCm # platforms that use the torch.float8_e4m3fnuz dtype. finfo = torch.finfo(fp8_dtype) fp8_min = -224.0 if current_platform.is_fp8_fnuz() else finfo.min fp8_max = 224.0 if current_platform.is_fp8_fnuz() else finfo.max # Force even division so we can avoid edgecases within the kernel. assert M % BLOCK_M == 0 assert N_2 % BLOCK_N == 0 grid = (M // BLOCK_M, N_2 // BLOCK_N) _silu_mul_per_token_group_quant_fp8_colmajor[grid]( input, output, output_scales, M, N, output_scales.stride(-1), eps, fp8_min, fp8_max, use_ue8m0, GROUP_SIZE, BLOCK_M, BLOCK_N, ) return output, output_scales @triton.jit def _per_token_group_quant_fp8_colmajor( # Pointers to inputs and output y_ptr, y_q_ptr, y_s_ptr, group_size, # Num columns of y y_num_columns, y_row_stride, # Stride from one column to the next of y_s y_s_col_stride, # Avoid to divide zero eps, # Information for float8 fp8_min, fp8_max, use_ue8m0: tl.constexpr, # Meta-parameters BLOCK: tl.constexpr, ): """A Triton-accelerated function to perform per-token-group quantization on a tensor. This function converts the tensor values into float8 values. """ groups_per_row = y_num_columns // group_size # Map the program id to the row of X and Y it should compute. g_id = tl.program_id(0) row = g_id // groups_per_row row_g_id = g_id % groups_per_row # Ensure offset calculations use int64 to prevent overflow y_ptr_offset = (row.to(tl.int64) * y_row_stride) + ( row_g_id.to(tl.int64) * group_size ) y_ptr += y_ptr_offset y_q_ptr_offset = g_id.to(tl.int64) * group_size y_q_ptr += y_q_ptr_offset # Convert g_id the flattened block coordinate to 2D so we can index # into the output y_scales matrix blocks_per_row = y_num_columns // group_size scale_col = g_id % blocks_per_row scale_row = g_id // blocks_per_row # Ensure offset calculation uses int64 for y_s_ptr y_s_ptr_offset = (scale_col.to(tl.int64) * y_s_col_stride) + scale_row.to(tl.int64) y_s_ptr += y_s_ptr_offset cols = tl.arange(0, BLOCK) # group_size <= BLOCK mask = cols < group_size y = tl.load(y_ptr + cols, mask=mask, other=0.0).to(tl.float32) # Quant _absmax = tl.maximum(tl.max(tl.abs(y)), eps) scale_raw = _absmax / fp8_max y_s = tl.math.exp2(tl.ceil(tl.log2(scale_raw))) if use_ue8m0 else scale_raw y_q = tl.clamp(y / y_s, fp8_min, fp8_max).to(y_q_ptr.dtype.element_ty) tl.store(y_q_ptr + cols, y_q, mask=mask) tl.store(y_s_ptr, y_s) def per_token_group_quant_fp8( x: torch.Tensor, group_size: int, eps: float = 1e-10, dtype: torch.dtype | None = None, column_major_scales: bool = False, out_q: torch.Tensor | None = None, use_ue8m0: bool | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: """Function to perform per-token-group quantization on an input tensor `x`. It converts the tensor values into signed float8 values and returns the quantized tensor along with the scaling factor used for quantization. Args: x: The input tensor with ndim >= 2. group_size: The group size used for quantization. eps: The minimum to avoid dividing zero. dtype: The dype of output tensor. Note that only `torch.float8_e4m3fn` is supported for now. column_major_scales: Outputs scales in column major. out_q: Optional output tensor. If not provided, function will create. Returns: tuple[torch.Tensor, torch.Tensor]: The quantized tensor and the scaling factor. """ if use_ue8m0 is None: use_ue8m0 = is_deep_gemm_e8m0_used() dtype = current_platform.fp8_dtype() if dtype is None else dtype assert x.shape[-1] % group_size == 0, ( f"the last dimension of `x` {x.shape[-1]} must be divisible " f"by `group_size` {group_size}" ) assert x.stride(-1) == 1, "`x` groups must be contiguous" # Using the default value (240.0) from pytorch will cause accuracy # issue on dynamic quantization models. Here use 224.0 for fnuz on ROCm # platforms that use the torch.float8_e4mefnuz dtype. finfo = torch.finfo(dtype) fp8_min = -224.0 if current_platform.is_fp8_fnuz() else finfo.min fp8_max = 224.0 if current_platform.is_fp8_fnuz() else finfo.max assert out_q is None or out_q.shape == x.shape x_q = out_q if x_q is None: x_q = torch.empty(x.shape, device=x.device, dtype=dtype) # Allocate the scale tensor in either row- or column-major format. if column_major_scales: shape = (x.shape[-1] // group_size,) + x.shape[:-1] x_s = torch.empty(shape, device=x.device, dtype=torch.float32).permute(-1, -2) else: shape = x.shape[:-1] + (x.shape[-1] // group_size,) x_s = torch.empty(shape, device=x.device, dtype=torch.float32) # prefer CUDA kernel if available # TODO(bnell): this causes some fp8 moe test to fail. if current_platform.is_cuda() and x.is_contiguous(): torch.ops._C.per_token_group_fp8_quant( x, x_q, x_s, group_size, eps, fp8_min, fp8_max, use_ue8m0 ) return x_q, x_s # TRITON FALLBACK M = x.numel() // group_size N = group_size BLOCK = triton.next_power_of_2(N) # heuristics for number of warps num_warps = min(max(BLOCK // 256, 1), 8) num_stages = 1 if column_major_scales: _per_token_group_quant_fp8_colmajor[(M,)]( x, x_q, x_s, group_size, x.shape[1], x.stride(0), x_s.stride(1), eps, fp8_min=fp8_min, fp8_max=fp8_max, use_ue8m0=use_ue8m0, BLOCK=BLOCK, num_warps=num_warps, num_stages=num_stages, ) else: _per_token_group_quant_fp8[(M,)]( x, x_q, x_s, group_size, x.shape[1], x.stride(0), eps, fp8_min=fp8_min, fp8_max=fp8_max, use_ue8m0=use_ue8m0, BLOCK=BLOCK, num_warps=num_warps, num_stages=num_stages, ) return x_q, x_s def per_token_group_quant_fp8_packed_for_deepgemm( x: torch.Tensor, group_size: int, eps: float = 1e-10, use_ue8m0: bool | None = None, out_q: torch.Tensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: """FP8 per-token-group quantization for DeepGEMM. Returns: (x_q, x_s_packed) x_q: FP8 activations, same shape as `x`. x_s_packed: Int32 tensor with logical shape [mn, ceil(num_groups_per_row / 4)], laid out with TMA-aligned stride along the packed-K dimension """ if use_ue8m0 is None: use_ue8m0 = is_deep_gemm_e8m0_used() # for DeepGEMM UE8M0-packed layout we *require* UE8M0 scales. assert use_ue8m0, ( "per_token_group_quant_fp8_packed_for_deepgemm requires UE8M0 scales." ) dtype = current_platform.fp8_dtype() assert x.shape[-1] % group_size == 0, ( f"the last dimension of `x` {x.shape[-1]} must be divisible " f"by `group_size` {group_size}" ) assert x.stride(-1) == 1, "`x` groups must be contiguous" finfo = torch.finfo(dtype) fp8_min, fp8_max = finfo.min, finfo.max # compute DeepGEMM-style packed scale tensor shape. hidden_dim = x.shape[-1] mn = x.numel() // hidden_dim num_groups_per_row = hidden_dim // group_size k_num_packed_sf_k = (num_groups_per_row + 3) // 4 tma_aligned_mn = ((mn + 3) // 4) * 4 x_s_packed = torch.empty_strided( (mn, k_num_packed_sf_k), (1, tma_aligned_mn), device=x.device, dtype=torch.int32, ) # CUDA kernel path only (DeepGEMM + E8M0 is CUDA-specific). assert current_platform.is_cuda(), ( "per_token_group_quant_fp8_packed_for_deepgemm is only valid on CUDA " "platforms using DeepGEMM." ) x_contiguous = x.contiguous() if out_q is not None: x_q_local = out_q else: x_q_local = torch.empty_like(x_contiguous, device=x.device, dtype=dtype) torch.ops._C.per_token_group_fp8_quant_packed( x_contiguous, x_q_local, x_s_packed, group_size, eps, fp8_min, fp8_max, ) # return a tensor with the original logical shape. x_q = x_q_local.view_as(x) return x_q, x_s_packed @triton.jit def _w8a8_triton_block_scaled_mm( # Pointers to inputs and output A, B, C, As, Bs, # Shape for matmul M, N, K, # Block size for block-wise quantization group_n, group_k, # Stride for inputs and output stride_am, stride_ak, stride_bk, stride_bn, stride_cm, stride_cn, stride_As_m, stride_As_k, stride_Bs_k, stride_Bs_n, # Meta-parameters BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr, GROUP_SIZE_M: tl.constexpr, ): """Triton-accelerated function used to perform linear operations (dot product) on input tensors `A` and `B` with block-wise quantization, and store the result in output tensor `C`. """ pid = tl.program_id(axis=0) num_pid_m = tl.cdiv(M, BLOCK_SIZE_M) num_pid_n = tl.cdiv(N, BLOCK_SIZE_N) num_pid_in_group = GROUP_SIZE_M * num_pid_n group_id = pid // num_pid_in_group first_pid_m = group_id * GROUP_SIZE_M group_size_m = min(num_pid_m - first_pid_m, GROUP_SIZE_M) pid_m = first_pid_m + (pid % group_size_m) pid_n = (pid % num_pid_in_group) // group_size_m offs_am = (pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)) % M offs_bn = (pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)) % N offs_k = tl.arange(0, BLOCK_SIZE_K) a_ptrs = A + (offs_am[:, None] * stride_am + offs_k[None, :] * stride_ak) b_ptrs = B + (offs_k[:, None] * stride_bk + offs_bn[None, :] * stride_bn) As_ptrs = As + offs_am * stride_As_m offs_bsn = offs_bn // group_n Bs_ptrs = Bs + offs_bsn * stride_Bs_n accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32) for k in range(0, tl.cdiv(K, BLOCK_SIZE_K)): a = tl.load(a_ptrs, mask=offs_k[None, :] < K - k * BLOCK_SIZE_K, other=0.0) b = tl.load(b_ptrs, mask=offs_k[:, None] < K - k * BLOCK_SIZE_K, other=0.0) k_start = k * BLOCK_SIZE_K offs_ks = k_start // group_k a_s = tl.load(As_ptrs + offs_ks * stride_As_k) b_s = tl.load(Bs_ptrs + offs_ks * stride_Bs_k) accumulator += tl.dot(a, b) * a_s[:, None] * b_s[None, :] a_ptrs += BLOCK_SIZE_K * stride_ak b_ptrs += BLOCK_SIZE_K * stride_bk if C.dtype.element_ty == tl.bfloat16: c = accumulator.to(tl.bfloat16) elif C.dtype.element_ty == tl.float16: c = accumulator.to(tl.float16) else: c = accumulator.to(tl.float32) offs_cm = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M) offs_cn = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N) c_ptrs = C + stride_cm * offs_cm[:, None] + stride_cn * offs_cn[None, :] c_mask = (offs_cm[:, None] < M) & (offs_cn[None, :] < N) tl.store(c_ptrs, c, mask=c_mask) @functools.lru_cache def get_w8a8_block_fp8_configs( N: int, K: int, block_n: int, block_k: int ) -> dict[int, Any] | None: """ Return optimized configurations for the w8a8 block fp8 kernel. The return value will be a dictionary that maps an irregular grid of batch sizes to configurations of the w8a8 block fp8 kernel. To evaluate the kernel on a given batch size bs, the closest batch size in the grid should be picked and the associated configuration chosen to invoke the kernel. """ # First look up if an optimized configuration is available in the configs # directory device_name = current_platform.get_device_name().replace(" ", "_") json_file_name = f"N={N},K={K},device_name={device_name},dtype=fp8_w8a8,block_shape=[{block_n},{block_k}].json" # noqa: E501 config_file_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), "configs", json_file_name ) if os.path.exists(config_file_path): with open(config_file_path) as f: logger.info( "Using configuration from %s for W8A8 Block FP8 kernel.", config_file_path, ) # If a configuration has been found, return it return {int(key): val for key, val in json.load(f).items()} # If no optimized configuration is available, we will use the default # configuration logger.warning( "Using default W8A8 Block FP8 kernel config. Performance might " "be sub-optimal! Config file not found at %s", config_file_path, ) return None def w8a8_triton_block_scaled_mm( A: torch.Tensor, B: torch.Tensor, As: torch.Tensor, Bs: torch.Tensor, block_size: list[int], output_dtype: torch.dtype = torch.float16, ) -> torch.Tensor: """This function performs matrix multiplication with block-wise quantization. It takes two input tensors `A` and `B` with scales `As` and `Bs`. The output is returned in the specified `output_dtype`. Args: A: The input tensor, e.g., activation. B: The input tensor, e.g., weight. As: The per-token-group quantization scale for `A`. Bs: The per-block quantization scale for `B`. block_size: The block size for per-block quantization. It should be 2-dim, e.g., [128, 128]. output_dytpe: The dtype of the returned tensor. Returns: torch.Tensor: The result of matmul. """ assert len(block_size) == 2 block_n, block_k = block_size[0], block_size[1] assert A.shape[-1] == B.shape[-1] assert A.shape[:-1] == As.shape[:-1] and A.is_contiguous()
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
true
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/bitblas_utils.py
vllm/model_executor/layers/quantization/utils/bitblas_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from packaging import version from vllm.platforms import current_platform from vllm.scalar_type import ScalarType, scalar_types MINIMUM_BITBLAS_VERSION = "0.1.0" BITBLAS_MIN_WEIGHT_SIZE_N = 16 BITBLAS_MIN_WEIGHT_SIZE_K = 16 GPTQ_BITBLAS_MAX_PARALLEL = 16 BITBLAS_SUPPORTED_GROUP_SIZES = [-1, 32, 64, 128] # For dynamic shape code generation BITBLAS_OPTIMIZE_FEATURES = [1, 16, 32, 64, 128, 256, 512, 1024] # If want to enable high performance for contiguous batching # Please use the following values BITBLAS_OPTIMIZE_FEATURES_CONTIGUOUS = [16, 32, 64, 128, 256, 512, 1024] BITBLAS_SUPPORTED_NUM_BITS = [1, 2, 4, 8] BITBLAS_SUPPORTED_SYM = [False, True] # Determines the supported quantization types for BitBLAS based on the # device's capability and whether zero-point (zp) is used. def query_bitblas_supported_quant_types( has_zp: bool, device_capability: int | None = None ): if device_capability is None: capability_tuple = current_platform.get_device_capability() device_capability = ( -1 if capability_tuple is None else capability_tuple.to_int() ) if device_capability < 70: return [] if has_zp: # AWQ style, unsigned + runtime zero-point return [scalar_types.uint4, scalar_types.uint8] else: # GPTQ style, unsigned + symmetric bias # TODO: once fp8_bitblas is merged into "gptq_bitblas" we should be able # to add `scalar_types.float8_e4m3fn` here return [scalar_types.uint4b8, scalar_types.uint8b128] def _check_bitblas_supported( quant_type: ScalarType, group_size: int | None, has_zp: bool, device_capability: int | None = None, ) -> tuple[bool, str | None]: if device_capability is None: capability_tuple = current_platform.get_device_capability() device_capability = ( -1 if capability_tuple is None else capability_tuple.to_int() ) supported_types = query_bitblas_supported_quant_types(has_zp, device_capability) if quant_type not in supported_types: return ( False, f"BitBLAS does not support weight_bits = {quant_type}. " f"Only types = {supported_types} " f"are supported (for group_size = {group_size}, " f"device_capability = {device_capability}, zp = {has_zp}).", ) if group_size is None or group_size not in BITBLAS_SUPPORTED_GROUP_SIZES: return ( False, f"BitBLAS does not support group_size = {group_size}. " f"Only group_sizes = {BITBLAS_SUPPORTED_GROUP_SIZES} " "are supported.", ) # Finally, check if bitblas is installed try: import bitblas if version.parse(bitblas.__version__) < version.parse(MINIMUM_BITBLAS_VERSION): raise ImportError( "bitblas version is wrong. Please " f"install bitblas>={MINIMUM_BITBLAS_VERSION}" ) except ImportError: return False, "BitBLAS is not installed." return True, None def check_bitblas_supported( quant_type: ScalarType, group_size: int, has_zp: bool = False, device_capability: int | None = None, ) -> bool: cond, _ = _check_bitblas_supported( quant_type, group_size, has_zp, device_capability ) return cond def verify_bitblas_supported( quant_type: ScalarType, group_size: int, has_zp: bool = False ) -> None: cond, err_msg = _check_bitblas_supported(quant_type, group_size, has_zp) if not cond: assert err_msg is not None raise ValueError(err_msg) def verify_bitblas_supports_shape( output_size_per_partition: int, input_size_per_partition: int, input_size: int, group_size: int, ) -> None: # Validate output_size_per_partition if output_size_per_partition % BITBLAS_MIN_WEIGHT_SIZE_N != 0: raise ValueError( f"Weight output_size_per_partition = " f"{output_size_per_partition} is not divisible by " f" min_thread_n = {BITBLAS_MIN_WEIGHT_SIZE_N}. " "Consider reducing tensor_parallel_size or running " "with --quantization gptq." ) # Validate input_size_per_partition if input_size_per_partition % BITBLAS_MIN_WEIGHT_SIZE_K != 0: raise ValueError( f"Weight input_size_per_partition = " f"{input_size_per_partition} is not divisible " f"by min_thread_k = {BITBLAS_MIN_WEIGHT_SIZE_K}. " "Consider reducing tensor_parallel_size or running " "with --quantization gptq." ) if group_size < input_size and input_size_per_partition % group_size != 0: raise ValueError( f"Weight input_size_per_partition = {input_size_per_partition}" f" is not divisible by group_size = {group_size}." "Consider reducing tensor_parallel_size or running " "with --quantization gptq." ) def check_bitblas_supports_shape( output_size_per_partition: int, input_size_per_partition: int, input_size: int, group_size: int, ) -> tuple[bool, str | None]: try: verify_bitblas_supports_shape( output_size_per_partition, input_size_per_partition, input_size, group_size ) except ValueError as e: return False, e.__str__() return True, None def bitblas_is_k_full(act_order: bool, is_row_parallel: bool) -> bool: return (not act_order) or (act_order and not is_row_parallel) def bitblas_repeat_scales_on_all_ranks( act_order: bool, group_size: int, is_row_parallel: bool ) -> bool: # Need to repeat scales on every rank if act_ordering or # channelwise and RowParallelLinear is_channelwise = group_size == -1 return act_order or (is_channelwise and is_row_parallel) def bitblas_make_empty_g_idx(device: torch.device) -> torch.Tensor: return torch.nn.Parameter( torch.empty(0, dtype=torch.int, device=device), requires_grad=False ) def bitblas_make_empty_zp(device: torch.device) -> torch.Tensor: return torch.nn.Parameter( torch.empty(0, dtype=torch.int, device=device), requires_grad=False ) def bitblas_sort_g_idx(g_idx: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: g_idx_sort_indices = torch.argsort(g_idx).to(torch.int) return g_idx[g_idx_sort_indices], g_idx_sort_indices def unpack_gptq_qzeros(qzeros, bits, is_gptq_v2=False) -> torch.Tensor: qzeros = qzeros.view(torch.int32) elems_per_int32 = 32 // bits unpacked_zeros = torch.zeros( (qzeros.shape[0], qzeros.shape[1] * elems_per_int32), dtype=torch.int8, device=qzeros.device, requires_grad=False, ) for col in range(unpacked_zeros.shape[1]): i = col % elems_per_int32 unpacked_zeros[:, col] = (qzeros[:, col // elems_per_int32] >> (bits * i)) & 0xF if not is_gptq_v2: return unpacked_zeros + 1 return unpacked_zeros def unpack_gptq_qweight(qweight, bits): qweight = qweight.view(torch.int8) elems_per_int8 = 8 // bits unpacked_weight = torch.zeros( (qweight.shape[0], qweight.shape[1] * elems_per_int8), dtype=torch.int8, device=qweight.device, requires_grad=False, ) for col in range(unpacked_weight.shape[1]): i = col % elems_per_int8 unpacked_weight[:, col] = qweight[:, col // elems_per_int8] >> (bits * i) return torch.bitwise_and(unpacked_weight, 2**bits - 1)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/marlin_utils_test_24.py
vllm/model_executor/layers/quantization/utils/marlin_utils_test_24.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Utility functions used for tests and benchmarks""" import random import numpy import torch from vllm.scalar_type import ScalarType from .marlin_utils_test import marlin_weights from .quant_utils import gptq_quantize_weights # This is PyTorch implementation of main part of reorder_meta() # function, from tools/util/include/cutlass/util/host_reorder.h file # of CUTLASS source tree. Furthermore, CUTLASS template for sparse # GEMM decides upon layout of this matrix, and at the moment for the # sparse GEMM executed on tensor cores, this is layout described by # ColumnMajorInterleaved<2> data structure, in # include/cutlass/layout/matrix.h of CUTLASS source tree. The # reordering of meta matrix into meta_reordered matrix calculated # according to these segments of CUTLASS code is re-implemented here. # Note that this calculation produces offsets for scattering metadata # matrix elements into reordered metadata matrix elements (or, # equivalently, for gathering reordered metadata matrix element back # into metadata matrix elements). def _calculate_meta_reordering_scatter_offsets(m, meta_ncols, meta_dtype, device): dst_rows = torch.arange(0, m, device=device)[:, None].repeat(1, meta_ncols) dst_cols = torch.arange(0, meta_ncols, device=device).repeat(m, 1) # Reorder the rows, then swizzle the 2x2 blocks. group_x = 64 group_y = 32 if meta_dtype.itemsize == 2 else 16 dst_rows = ( dst_rows // group_x * group_x + (dst_rows % 2) * 2 + (dst_rows % 8) // 4 + ((dst_rows % group_y) % 4) // 2 * 32 + ((dst_rows % group_x) // 8) * 4 ) topright = ((dst_rows % 2 == 0) & (dst_cols % 2 == 1)).to(torch.int8) bottomleft = ((dst_rows % 2 == 1) & (dst_cols % 2 == 0)).to(torch.int8) dst_rows += topright - bottomleft dst_cols -= topright - bottomleft # Assumed that meta tensor is to be stored in CUTLASS # InterleavedColumnMajor layout, and reverse engineered # corresponding code to store values into this tensor. interleave = 2 cols_maj = dst_cols // interleave cols_min = dst_cols % interleave return (cols_maj * m * interleave + dst_rows * interleave + cols_min).view(-1) # This function converts dense matrix into sparse semi-structured # representation, producing "compressed" matrix, in the layout used by # CUTLASS backend, and corresponding metadata matrix. def sparse_semi_structured_from_dense_cutlass(dense): if dense.dim() != 2: raise RuntimeError( f"Expected 2-dimensional dense tensor, got {dense.dim()}-dimensional tensor" # noqa: E501 ) m, k = dense.shape device = dense.device meta_dtype = torch.int8 if dense.dtype == torch.int8: meta_dtype = torch.int32 elif dense.dtype in [torch.half, torch.bfloat16, torch.float, torch.int32]: meta_dtype = torch.int16 else: raise RuntimeError(f"Invalid datatype {dense.dtype} of dense matrix") quadbits_per_meta_elem = meta_dtype.itemsize * 8 // 4 if quadbits_per_meta_elem not in (4, 8): raise RuntimeError("Invalid number of elements per meta element calculated") if meta_dtype == torch.int32: if m % 16 != 0: raise RuntimeError( f"Number of rows of dense matrix {m} must be divisible by 16" ) else: if m % 32 != 0: raise RuntimeError( f"Number of rows of dense matrix {m} must be divisible by 32" ) if k % (4 * quadbits_per_meta_elem) != 0: raise RuntimeError( f"Number of columns of dense matrix {k} must be divisible by {4 * quadbits_per_meta_elem}" # noqa: E501 ) if dense.dtype != torch.float: ksparse = 4 dense_4 = dense.view(-1, k // ksparse, ksparse) m0, m1, m2, m3 = (dense_4 != 0).unbind(-1) else: ksparse = 2 dense_2 = dense.view(-1, k // ksparse, ksparse) m0, m2 = m1, m3 = (dense_2 != 0).unbind(-1) meta_ncols = k // (ksparse * quadbits_per_meta_elem) # Encoding quadruples of True/False values as follows: # [True, True, False, False] -> 0b0100 # [True, False, True, False] -> 0b1000 # [False, True, True, False] -> 0b1001 # [True, False, False, True ] -> 0b1100 # [False, True, False, True ] -> 0b1101 # [False, False, True, True ] -> 0b1110 # Thus, lower two bits in the encoding are index of the True value # at the lowest index in the quadruple, and the higher two bits in # the encoding are index of the other True value in the quadruple. # In case there are less than two True values, than False value or # values at some index or indices are considered True for the # encoding. In case there are more than two True values, then the # excess True value(s) at some indices are considered False for # the encoding. The exact encodings used for these cases are as # follows: # [False, False, False, False] -> 0b1110 # [False, False, False, True ] -> 0b1110 # [False, False, True, False] -> 0b1110 # [False, True, False, False] -> 0b1001 # [False, True, True, True ] -> 0b1101 # [True, False, False, False] -> 0b1000 # [True, False, True, True ] -> 0b1100 # [True, True, False, True ] -> 0b0100 # [True, True, True, False] -> 0b0100 # [True, True, True, True ] -> 0b0100 # These particular encodings are chosen, with the help of Espresso # logic minimizer software, for the purpose of minimization of # corresponding Boolean functions, that translate non-zero flags # into encoding bits. Note also possible choices for the first # and last of these encodings were limited only to (0b0100, # 0b1110), in order to produce valid encodings for 1:2 sparsity # case. expr0 = m0 & m1 expr1 = ~m0 & m1 expr2 = ~m0 & ~m1 bit0 = expr1 bit1 = expr2 bit2 = expr0 | expr2 | m3 bit3 = expr1 | ~m1 idxs0 = bit0 | (bit1.to(torch.int64) << 1) idxs1 = bit2 | (bit3.to(torch.int64) << 1) if dense.dtype != torch.float: sparse0 = dense_4.gather(-1, idxs0.unsqueeze(-1)) # type: ignore[possibly-undefined] sparse1 = dense_4.gather(-1, idxs1.unsqueeze(-1)) sparse = torch.stack((sparse0, sparse1), dim=-1).view(m, k // 2) else: sparse = dense_2.gather(-1, idxs0.unsqueeze(-1) // 2).view(m, k // 2) # type: ignore[possibly-undefined] meta_4 = idxs0 | (idxs1 << 2) meta_n = meta_4.view((-1, meta_ncols, quadbits_per_meta_elem)).to(meta_dtype) if quadbits_per_meta_elem == 4: meta = ( meta_n[:, :, 0] | (meta_n[:, :, 1] << 4) | (meta_n[:, :, 2] << 8) | (meta_n[:, :, 3] << 12) ) elif quadbits_per_meta_elem == 8: meta = ( meta_n[:, :, 0] | (meta_n[:, :, 1] << 4) | (meta_n[:, :, 2] << 8) | (meta_n[:, :, 3] << 12) | (meta_n[:, :, 4] << 16) | (meta_n[:, :, 5] << 20) | (meta_n[:, :, 6] << 24) | (meta_n[:, :, 7] << 28) ) # Reorder meta tensor elements. meta_reordered = meta.new_empty((m * meta_ncols,)) # type: ignore[possibly-undefined] meta_offsets = _calculate_meta_reordering_scatter_offsets( m, meta_ncols, meta_dtype, device ) meta_reordered.scatter_(0, meta_offsets, meta.view(-1)) return (sparse, meta_reordered.view(m, meta_ncols)) # This function performs reverse of the function above - it # reconstructs dense matrix from a pair of "compressed" matrix, given # in the layout used by CUTLASS backend, and accompanying metadata # matrix. def sparse_semi_structured_to_dense_cutlass(sparse, meta_reordered): if sparse.dim() != 2: raise RuntimeError( f"Expected 2-dimensional sparse tensor, got {sparse.dim()}-dimensional tensor" # noqa: E501 ) m, k = sparse.shape device = sparse.device if meta_reordered.dim() != 2: raise RuntimeError( f"Expected 2-dimensional meta tensor, got {meta_reordered.dim()}-dimensional tensor" # noqa: E501 ) if meta_reordered.device != device: raise RuntimeError( f"Expected meta matrix to be on {device} device, got matrix on {meta_reordered.device} device" # noqa: E501 ) meta_dtype = meta_reordered.dtype if meta_dtype not in (torch.int16, torch.int32): raise RuntimeError(f"Invalid datatype {meta_dtype} of meta matrix") quadbits_per_meta_elem = meta_dtype.itemsize * 8 // 4 ksparse = 4 if sparse.dtype != torch.float else 2 meta_nrows, meta_ncols = meta_reordered.shape if meta_nrows != m: raise RuntimeError( f"Number of rows of meta matrix {meta_nrows} must be equal to number of columns of spase matrix {m}" # noqa: E501 ) if meta_ncols * ksparse * quadbits_per_meta_elem != 2 * k: raise RuntimeError( f"Number of columns of sparse matrix {k} different from the {meta_ncols * ksparse * quadbits_per_meta_elem // 2}, " # noqa: E501 "expected according to the number of columns of meta matrix" ) # Undo meta tensor elements reordering. meta_offsets = _calculate_meta_reordering_scatter_offsets( m, meta_ncols, meta_dtype, device ) meta = torch.gather(meta_reordered.view(-1), 0, meta_offsets).view(m, meta_ncols) # Unpack sparse tensor back to original dense tensor, using # information provided by meta tensor. Note that torch.float # datatype is handled pretty much the same as # torch.half/torch.bfloat16, as metadata for a pair of torch.float # value is encoded as if underlying 8 bytes contain four # torch.half/torch.bfloat16 values, where either first two or last # two are zeros. meta_2 = torch.empty( (m, meta_ncols, 2 * quadbits_per_meta_elem), dtype=meta_dtype, device=device, ) if quadbits_per_meta_elem == 4: meta_2[:, :, 0] = meta & 0b11 meta_2[:, :, 1] = (meta >> 2) & 0b11 meta_2[:, :, 2] = (meta >> 4) & 0b11 meta_2[:, :, 3] = (meta >> 6) & 0b11 meta_2[:, :, 4] = (meta >> 8) & 0b11 meta_2[:, :, 5] = (meta >> 10) & 0b11 meta_2[:, :, 6] = (meta >> 12) & 0b11 meta_2[:, :, 7] = (meta >> 14) & 0b11 elif quadbits_per_meta_elem == 8: meta_2[:, :, 0] = meta & 0b11 meta_2[:, :, 1] = (meta >> 2) & 0b11 meta_2[:, :, 2] = (meta >> 4) & 0b11 meta_2[:, :, 3] = (meta >> 6) & 0b11 meta_2[:, :, 4] = (meta >> 8) & 0b11 meta_2[:, :, 5] = (meta >> 10) & 0b11 meta_2[:, :, 6] = (meta >> 12) & 0b11 meta_2[:, :, 7] = (meta >> 14) & 0b11 meta_2[:, :, 8] = (meta >> 16) & 0b11 meta_2[:, :, 9] = (meta >> 18) & 0b11 meta_2[:, :, 10] = (meta >> 20) & 0b11 meta_2[:, :, 11] = (meta >> 22) & 0b11 meta_2[:, :, 12] = (meta >> 24) & 0b11 meta_2[:, :, 13] = (meta >> 26) & 0b11 meta_2[:, :, 14] = (meta >> 28) & 0b11 meta_2[:, :, 15] = (meta >> 30) & 0b11 dense_offsets = meta_2.view(-1) + ( torch.arange(0, 2 * m * k // ksparse, device=device) * 4 ).view(-1, 1).repeat(1, 2).view(-1) dense = torch.zeros((m * 2 * k,), dtype=sparse.dtype, device=device) if sparse.dtype != torch.float: # dense.scatter_(0, dense_offsets, sparse.view(-1)) dense.scatter_(0, dense_offsets, sparse.reshape(-1)) else: dense.view(torch.half).scatter_( 0, dense_offsets, sparse.view(torch.half).view(-1) ) return dense.view(m, 2 * k) def mask_creator(tensor): """ Class for creating N:M sparsity masks. Masks will be created using the N:M ratio, where for every block of M weights, N will be pruned based on ranked weight value. Each mask will correspond to the given tensor. :param N: The number of weights in a group to keep :param M: The size of a weight group """ N = 2 M = 4 mask = None # for i, tensor in enumerate(tensors): if tensor.numel() % M != 0: raise ValueError( f"Tensor of size {tensor.shape} can't be evenly divided into {M} groups" ) num_groups = tensor.numel() // M # N:M sparsity for linear layers tensor_temp = tensor.detach().abs().reshape(num_groups, M) index = torch.argsort(tensor_temp, dim=1)[:, : int(M - N)] w_b = torch.ones(tensor_temp.shape, device=tensor_temp.device) mask = w_b.scatter_(dim=1, index=index, value=0).reshape(tensor.shape) return mask def inject_24(w, size_k, size_n): assert w.shape == (size_k, size_n) mask = mask_creator(w.t()).t().cuda().bool() return (mask * w).contiguous(), mask.contiguous() def check_24(w, num_rows_to_sample=50, _verbose=False): BLOCK_SIZE = 4 MAX_NON_ZEROS = 2 w = w.t().contiguous() print("check_24: w.shape = {}".format(w.shape)) num_rows, num_cols = w.shape sampled_row_idxs = random.choices(range(num_rows), k=num_rows_to_sample) if _verbose: print(f"Sampled row idxs = {sampled_row_idxs}") total_segments = 0 non_24_segments = 0 for i in sampled_row_idxs: for j in range(0, num_cols - BLOCK_SIZE, BLOCK_SIZE): total_segments += 1 block = w[i, j : j + BLOCK_SIZE] num_nonzero = torch.count_nonzero(block) if num_nonzero > MAX_NON_ZEROS: print("i = {} j = {} block = {}".format(i, j, block)) non_24_segments += 1 print(f"{non_24_segments} / {total_segments} do not have 2:4 structure.") def compress_quantized_24_weight(q_24, size_k, size_n, wtype: ScalarType): assert q_24.shape == (size_k, size_n) # Remove bias to normalize over 0 q_24_no_zp = q_24 - wtype.bias # Compress q_24_no_zp = q_24_no_zp.t().contiguous() q_24_no_zp_comp, meta = sparse_semi_structured_from_dense_cutlass(q_24_no_zp) q_24_no_zp_comp = q_24_no_zp_comp.t().contiguous() # Restore bias q_24_comp = q_24_no_zp_comp + wtype.bias # Resize meta to its actual shape (without moving any data) meta = meta.resize_(meta.shape[1] // 2, meta.shape[0] * 2) return q_24_comp, meta def get_scale_perms_24(): scale_perm: list[int] = [] for i in range(8): scale_perm.extend([i * 8 + j for j in [0, 4, 1, 5, 2, 6, 3, 7]]) scale_perm_single: list[int] = [] for i in range(8): scale_perm_single.extend([8 * i + j for j in [0, 1, 2, 3, 4, 5, 6, 7]]) return scale_perm, scale_perm_single def get_weight_perm_24(num_bits: int): perm_list: list[int] = [] for i in range(32): perm1: list[int] = [] col = i // 4 col_o = col // 2 for block in [0, 1]: for row in [ 2 * (i % 4), 2 * (i % 4) + 1, 2 * (i % 4 + 4), 2 * (i % 4 + 4) + 1, ]: perm1.append(16 * row + col_o * 256 + 8 * (col % 2) + 4 * block) for j in range(4): perm_list.extend([p + 1 * j for p in perm1]) perm = numpy.array(perm_list) if num_bits == 4: interleave = numpy.array([0, 2, 4, 6, 1, 3, 5, 7]) elif num_bits == 8: interleave = numpy.array([0, 2, 1, 3]) else: raise ValueError("num_bits must be 4 or 8, got {}".format(num_bits)) perm = perm.reshape((-1, len(interleave)))[:, interleave].ravel() perm = torch.from_numpy(perm) return perm def marlin_permute_scales_24( s: torch.Tensor, size_k: int, size_n: int, group_size: int ) -> torch.Tensor: scale_perm, scale_perm_single = get_scale_perms_24() if group_size < size_k and group_size != -1: s = s.reshape((-1, len(scale_perm)))[:, scale_perm] else: s = s.reshape((-1, len(scale_perm_single)))[:, scale_perm_single] s = s.reshape((-1, size_n)).contiguous() return s def marlin_24_quantize( w: torch.Tensor, quant_type: ScalarType, group_size: int, ): size_k, size_n = w.shape # Normalize group_size if group_size == -1: group_size = size_k assert group_size <= size_k # Inject 2:4 sparsity w_24, mask_24 = inject_24(w, size_k, size_n) # Quantize w_24_ref, q_w_24, s, g_idx, rand_perm = gptq_quantize_weights( w_24, quant_type, group_size, act_order=False ) # Compress quantized weight q_w_24_comp, meta = compress_quantized_24_weight(q_w_24, size_k, size_n, quant_type) size_k_comp = size_k // 2 # Reformat to marlin weight_perm = get_weight_perm_24(quant_type.size_bits) marlin_24_q_w_comp = marlin_weights( q_w_24_comp, size_k_comp, size_n, quant_type.size_bits, weight_perm ) marlin_24_s = marlin_permute_scales_24(s, size_k, size_n, group_size) # Create result res_list = [w_24_ref, marlin_24_q_w_comp, meta, marlin_24_s] for i in range(len(res_list)): res_list[i] = res_list[i].to(w.device) return res_list
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/w8a8_utils.py
vllm/model_executor/layers/quantization/utils/w8a8_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import torch from packaging import version from vllm import _custom_ops as ops from vllm import envs from vllm.config import CompilationMode, get_current_vllm_config from vllm.model_executor.layers.quantization.input_quant_fp8 import QuantFP8 from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape from vllm.platforms import current_platform from vllm.utils.flashinfer import flashinfer_scaled_fp8_mm, has_flashinfer from vllm.utils.platform_utils import get_cu_count from vllm.utils.torch_utils import direct_register_custom_op # Input scaling factors are no longer optional in _scaled_mm starting # from pytorch 2.5. Allocating a dummy tensor to pass as input_scale TORCH_DEVICE_IDENTITY = None # The condition to determine if it is on a platform that supports # torch._scaled_mm rowwise feature. # The condition is determined once as the operations # are time-consuming. USE_ROWWISE_TORCH_SCALED_MM = ( current_platform.is_rocm() and version.parse(torch.__version__) >= version.parse("2.7") and current_platform.has_device_capability(94) ) def sparse_cutlass_supported() -> bool: if not current_platform.is_cuda(): return False capability_tuple = current_platform.get_device_capability() capability = -1 if capability_tuple is None else capability_tuple.to_int() return ops.cutlass_sparse_scaled_mm_supported(capability) def cutlass_fp8_supported() -> bool: if not current_platform.is_cuda(): return False capability_tuple = current_platform.get_device_capability() capability = -1 if capability_tuple is None else capability_tuple.to_int() return ops.cutlass_scaled_mm_supports_fp8(capability) def cutlass_block_fp8_supported() -> bool: if not current_platform.is_cuda(): return False capability_tuple = current_platform.get_device_capability() capability = -1 if capability_tuple is None else capability_tuple.to_int() return ops.cutlass_scaled_mm_supports_block_fp8(capability) def cutlass_group_gemm_supported() -> bool: if not current_platform.is_cuda(): return False capability_tuple = current_platform.get_device_capability() capability = -1 if capability_tuple is None else capability_tuple.to_int() return ops.cutlass_group_gemm_supported(capability) CUTLASS_FP8_SUPPORTED = cutlass_fp8_supported() CUTLASS_BLOCK_FP8_SUPPORTED = cutlass_block_fp8_supported() def per_tensor_dequantize( tensor: torch.Tensor, inv_scale: float | torch.Tensor ) -> torch.Tensor: fake_qweight = tensor.to(torch.float16) dq_weight = fake_qweight * inv_scale return dq_weight def all_close_1d(x: torch.Tensor) -> bool: assert len(x.shape) == 1 return all(torch.allclose(x[0], x[i]) for i in range(x.shape[0])) def convert_to_channelwise( weight_scale: torch.Tensor, logical_widths: list[int] ) -> tuple[torch.Tensor, torch.Tensor]: # Create channelwise buffer weight_scale_channel = torch.empty( (sum(logical_widths), 1), dtype=torch.float32, device=weight_scale.device ) # Expand each scale to match the size of each logical matrix. start = 0 for idx, logical_width in enumerate(logical_widths): end = start + logical_width weight_scale_channel[start:end, :] = weight_scale[idx] start = end return weight_scale_channel def requantize_with_max_scale( weight: torch.Tensor, weight_scale: torch.Tensor, logical_widths: list[int] ) -> tuple[torch.Tensor, torch.Tensor]: # Max scale to be used for requanitzation. max_w_scale = weight_scale.max() # QKV / MLP is fused in the on disk checkpoint if any of the # weight scales are still set to the default since we initialize # N weight scales for N shards but we only load 1 weight scale # from disk in this case. Skip requantization in this case (since) # we already are quantized with the single scale. # * Sample Model: nm-testing/Phi-3-mini-128k-instruct-FP8 # # Extra note: upon weight reloading weight_scale.ndim == 0 unfused_module_in_checkpoint = ( weight_scale.ndim != 0 and weight_scale[-1] > torch.finfo(torch.float8_e4m3fn).min ) # If unfused checkpoint, need requanize with the single scale. if unfused_module_in_checkpoint: start = 0 for idx, logical_width in enumerate(logical_widths): # Skip any component with zero width. if logical_width == 0: continue end = start + logical_width weight_dq = per_tensor_dequantize(weight[start:end, :], weight_scale[idx]) weight[start:end, :], _ = ops.scaled_fp8_quant(weight_dq, max_w_scale) start = end return max_w_scale, weight def maybe_create_device_identity(): # Allocate dummy ones tensor for torch._scaled_mm global TORCH_DEVICE_IDENTITY if TORCH_DEVICE_IDENTITY is None: TORCH_DEVICE_IDENTITY = torch.ones(1, dtype=torch.float32) def cutlass_w8a8_scaled_mm( *, qinput: torch.Tensor, weight: torch.Tensor, out_dtype: torch.dtype, scale_a: torch.Tensor, scale_b: torch.Tensor, bias: torch.Tensor, output_shape: list, **kwargs, ) -> torch.Tensor: # Fused GEMM_DQ output = ops.cutlass_scaled_mm( qinput, weight, out_dtype=out_dtype, scale_a=scale_a, scale_b=scale_b, bias=bias ) return output.view(*output_shape) def flashinfer_w8a8_scaled_mm( *, qinput: torch.Tensor, weight: torch.Tensor, out_dtype: torch.dtype, scale_a: torch.Tensor, scale_b: torch.Tensor, bias: torch.Tensor, output_shape: list, **kwargs, ) -> torch.Tensor: return flashinfer_scaled_fp8_mm( qinput, weight, out_dtype=out_dtype, scale_a=scale_a, scale_b=scale_b, bias=bias ) def rocm_per_tensor_w8a8_scaled_mm_impl( qinput: torch.Tensor, weight: torch.Tensor, out_dtype: torch.dtype, scale_a: torch.Tensor, scale_b: torch.Tensor, bias: torch.Tensor, ) -> torch.Tensor: from vllm.platforms.rocm import on_mi3xx if ( envs.VLLM_ROCM_USE_SKINNY_GEMM and on_mi3xx() and qinput.shape[0] == 1 and qinput.shape[1] % 16 == 0 and ((bias is None) or (bias.dtype == out_dtype)) ): output = ops.wvSplitKQ( weight.t(), qinput, out_dtype, scale_a, scale_b, get_cu_count(), bias, ) else: output = torch._scaled_mm( qinput, weight, out_dtype=out_dtype, scale_a=scale_a, scale_b=scale_b, bias=bias, ) return output def rocm_per_tensor_w8a8_scaled_mm_fake( qinput: torch.Tensor, weight: torch.Tensor, out_dtype: torch.dtype, scale_a: torch.Tensor, scale_b: torch.Tensor, bias: torch.Tensor, ) -> torch.Tensor: return qinput.new_empty((*qinput.shape[:-1], weight.shape[1]), dtype=out_dtype) def rocm_per_tensor_w8a8_scaled_mm( *, qinput: torch.Tensor, weight: torch.Tensor, out_dtype: torch.dtype, scale_a: torch.Tensor, scale_b: torch.Tensor, bias: torch.Tensor, output_shape: list, ) -> torch.Tensor: output = torch.ops.vllm.rocm_per_tensor_w8a8_scaled_mm_impl( qinput, weight, out_dtype, scale_a, scale_b, bias ) return torch.narrow(output, 0, 0, qinput.shape[0]).view(*output_shape) direct_register_custom_op( op_name="rocm_per_tensor_w8a8_scaled_mm_impl", op_func=rocm_per_tensor_w8a8_scaled_mm_impl, fake_impl=rocm_per_tensor_w8a8_scaled_mm_fake, ) def torch_per_tensor_w8a8_scaled_mm( *, qinput: torch.Tensor, weight: torch.Tensor, out_dtype: torch.dtype, scale_a: torch.Tensor, scale_b: torch.Tensor, bias: torch.Tensor, output_shape: list, ) -> torch.Tensor: output = torch._scaled_mm( qinput, weight, out_dtype=out_dtype, scale_a=scale_a, scale_b=scale_b, bias=bias ) # A fix for discrepancy in scaled_mm which returns tuple # for torch < 2.5 and a single value in torch >= 2.5 if type(output) is tuple and len(output) == 2: output = output[0] return torch.narrow(output, 0, 0, qinput.shape[0]).view(*output_shape) def torch_per_token_w8a8_scaled_mm( *, qinput: torch.Tensor, weight: torch.Tensor, out_dtype: torch.dtype, scale_a: torch.Tensor, scale_b: torch.Tensor, bias: torch.Tensor, output_shape: list, **kwargs, ) -> torch.Tensor: # Note: Callers of this function should check USE_ROWWISE_TORCH_SCALED_MM # when using it. # For now it has only been validated on ROCm platform. # fp8 rowwise scaling in torch._scaled_mm is introduced in # https://github.com/pytorch/pytorch/pull/144432 using # hipBLASLt and ROCm 6.3, which only exists in torch 2.7 and above. # # For CUDA platform please validate if the torch._scaled_mm supports # rowwise scaled GEMM before using it # Fused GEMM_DQ Rowwise GEMM output = torch._scaled_mm( qinput, weight, out_dtype=out_dtype, scale_a=scale_a, scale_b=scale_b.t(), bias=bias, ) output = torch.narrow(output, 0, 0, qinput.shape[0]) output = output.view(*output_shape) return output def torch_channelwise_w8a8_scaled_mm( *, qinput: torch.Tensor, weight: torch.Tensor, out_dtype: torch.dtype, scale_a: torch.Tensor, scale_b: torch.Tensor, bias: torch.Tensor, output_shape: list, **kwargs, ) -> torch.Tensor: # Use unfused DQ due to limitations with scaled_mm # Symmetric quantized GEMM by definition computes the following: # C = (s_x * X) (s_w * W) + bias # This is equivalent to dequantizing the weights and activations # before applying a GEMM. # # In order to compute quantized operands, a quantized kernel # will rewrite the above like so: # C = s_w * s_x * (X * W) + bias # # For the scaled_mm fallback case, we break this down, since it # does not support s_w being a vector. # GEMM # This computes C = (X * W). # Output in fp32 to allow subsequent ops to happen in-place output = torch._scaled_mm( qinput, weight, scale_a=TORCH_DEVICE_IDENTITY, scale_b=TORCH_DEVICE_IDENTITY, out_dtype=torch.float32, ) # A fix for discrepancy in scaled_mm which returns tuple # for torch < 2.5 and a single value in torch >= 2.5 if type(output) is tuple and len(output) == 2: output = output[0] # Unpad (undo num_token_padding) output = torch.narrow(output, 0, 0, qinput.shape[0]) x_scale = torch.narrow(scale_a, 0, 0, qinput.shape[0]) # DQ # C = sw * sx * (X * W) + bias output = output * x_scale * scale_b.t() if bias is not None: output = output + bias return output.to(out_dtype).view(*output_shape) def dispatch_w8a8_scaled_mm( preferred_backend: str, per_tensor_weights: bool, per_tensor_activations: bool ) -> Callable[..., torch.Tensor]: if per_tensor_weights and per_tensor_activations: if preferred_backend == "rocm": return rocm_per_tensor_w8a8_scaled_mm if preferred_backend == "flashinfer": return flashinfer_w8a8_scaled_mm if preferred_backend == "cutlass": return cutlass_w8a8_scaled_mm return torch_per_tensor_w8a8_scaled_mm # cutlass_scaled_mm supports per tensor/channel W and per tensor/token A if preferred_backend == "cutlass" or preferred_backend == "flashinfer": return cutlass_w8a8_scaled_mm # If torch.scaled_mm supports per-channel (weights) per-token (inputs) if ( not per_tensor_weights and not per_tensor_activations and USE_ROWWISE_TORCH_SCALED_MM ): return torch_per_token_w8a8_scaled_mm # Normally, torch.scaled_mm supports per tensor weights + activations only # so fallback to naive if per channel or per token return torch_channelwise_w8a8_scaled_mm # TODO(luka): follow similar pattern for marlin and block-fp8-linear # https://github.com/vllm-project/vllm/issues/14397 class Fp8LinearOp: """ This class executes a FP8 linear layer using cutlass if supported and torch.scaled_mm otherwise. It needs to be a class instead of a method so that config can be read in the __init__ method, as reading config is not allowed inside forward. """ def __init__( self, act_quant_static: bool, act_quant_group_shape: GroupShape = GroupShape.PER_TENSOR, pad_output: bool | None = None, ): if current_platform.is_rocm(): self.preferred_backend = "rocm" elif current_platform.is_cuda() and cutlass_fp8_supported(): if has_flashinfer() and current_platform.has_device_capability(100): self.preferred_backend = "flashinfer" else: self.preferred_backend = "cutlass" else: self.preferred_backend = "torch" # Note: we pad the input because torch._scaled_mm is more performant # for matrices with batch dimension > 16. # This could change in the future. # We also don't pad when using torch.compile, # as it breaks with dynamic shapes. if pad_output is None: config = get_current_vllm_config().compilation_config pad_output = ( config.mode < CompilationMode.VLLM_COMPILE and self.preferred_backend == "torch" ) self.output_padding = 17 if pad_output else None self.act_quant_static = act_quant_static self.act_quant_group_shape = act_quant_group_shape self.quant_fp8 = QuantFP8( static=act_quant_static, group_shape=act_quant_group_shape, num_token_padding=self.output_padding, ) def apply( self, input: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, out_dtype: torch.dtype | None = None, input_scale: torch.Tensor | None = None, input_scale_ub: torch.Tensor | None = None, bias: torch.Tensor | None = None, ) -> torch.Tensor: # ops.scaled_fp8_quant supports both dynamic and static quant. # If dynamic, layer.input_scale is None and x_scale computed from x. # If static, layer.input_scale is scalar and x_scale is input_scale. # View input as 2D matrix for fp8 methods input_2d = input.view(-1, input.shape[-1]) output_shape = [*input.shape[:-1], weight.shape[1]] if out_dtype is None: out_dtype = input.dtype # If input not quantized # TODO(luka) remove this path if not used anymore if input.dtype != current_platform.fp8_dtype(): qinput, x_scale = self.quant_fp8( input_2d, input_scale, input_scale_ub, ) else: qinput, x_scale = input_2d, input_scale # Must have dim() conditions # In per-token quant scenario, when the number of token is 1, # the scale will only have 1 elements. # Without checking the dim(), # we cannot distingushes between per-tensor and per-token quant. # Example: # When the number of token is 1, per-token scale is [[1]] # When per-tensor scale is [1] or (). per_tensor_weights = weight_scale.numel() == 1 per_tensor_activations = (x_scale.numel() == 1) and x_scale.dim() < 2 # TODO(luka) do this dispatch during init (after ScaledMM refactor) w8a8_scaled_mm_func = dispatch_w8a8_scaled_mm( self.preferred_backend, per_tensor_weights, per_tensor_activations ) return w8a8_scaled_mm_func( qinput=qinput, weight=weight, out_dtype=out_dtype, scale_a=x_scale, scale_b=weight_scale, bias=bias, output_shape=output_shape, ) def normalize_e4m3fn_to_e4m3fnuz( weight: torch.Tensor, weight_scale: torch.Tensor, input_scale: torch.Tensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None]: assert weight.dtype == torch.float8_e4m3fn # The bits pattern 10000000(-128) represents zero in e4m3fn # but NaN in e4m3fnuz. So here we set it to 0. # https://onnx.ai/onnx/technical/float8.html weight_as_int8 = weight.view(torch.int8) ROCM_FP8_NAN_AS_INT = -128 weight_as_int8[weight_as_int8 == ROCM_FP8_NAN_AS_INT] = 0 weight = weight_as_int8.view(torch.float8_e4m3fnuz) # For the same bits representation, e4m3fnuz value is half of # the e4m3fn value, so we should double the scaling factor to # get the same dequantized value. # https://onnx.ai/onnx/technical/float8.html weight_scale = weight_scale * 2.0 if input_scale is not None: input_scale = input_scale * 2.0 return weight, weight_scale, input_scale
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/utils/allspark_utils.py
vllm/model_executor/layers/quantization/utils/allspark_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm.platforms import current_platform from vllm.scalar_type import ScalarType, scalar_types ALLSPARK_AMPERE_M_CUBLAS_THRESHOLD = 1024 ALLSPARK_SUPPORTED_QUANT_TYPES = [scalar_types.uint8b128] ALLSPARK_AMPERE_N_ALIGN = 16 ALLSPARK_AMPERE_K_ALIGN = 16 def check_allspark_supported_dtype_shape( input_size_per_partition: int, output_size_per_partition: int, group_size: int, weight_dtype: ScalarType, act_dtype: torch.dtype, ): capability_tuple = current_platform.get_device_capability() device_capability = -1 if capability_tuple is None else capability_tuple.to_int() # For Ampere GPU if device_capability >= 80 and device_capability < 90: if group_size != -1: return ( False, "For Ampere GPU, AllSpark does not support group_size " f"= {group_size}. Only group_size = -1 are supported.", ) if weight_dtype not in ALLSPARK_SUPPORTED_QUANT_TYPES: return ( False, "For Ampere GPU, AllSpark does not support " f"quant type ({weight_dtype}). Only quant type " f"({ALLSPARK_SUPPORTED_QUANT_TYPES}) are supported.", ) if ( input_size_per_partition % ALLSPARK_AMPERE_K_ALIGN != 0 or output_size_per_partition % ALLSPARK_AMPERE_N_ALIGN != 0 ): return ( False, "AllSpark needs input_size_per_partition % " f"{ALLSPARK_AMPERE_K_ALIGN} = 0 and " f"output_size_per_partition % {ALLSPARK_AMPERE_N_ALIGN} = 0 " "for Ampere GPU optimized kernels.", ) if act_dtype != torch.float16 and act_dtype != torch.bfloat16: return ( False, "AllSpark only supports act_dtype = float16 or bfloat16," f"for Ampere GPU, but got act_dtype = {act_dtype}.", ) else: return ( False, "AllSpark currently does not support " f"device_capability = {device_capability}.", ) return True, None
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/quark/quark.py
vllm/model_executor/layers/quantization/quark/quark.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import fnmatch from typing import TYPE_CHECKING, Any, Optional, cast import torch from vllm.attention.layer import Attention from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe import FusedMoE from vllm.model_executor.layers.linear import ( LinearBase, LinearMethodBase, UnquantizedLinearMethod, ) from vllm.model_executor.layers.quantization import QuantizationMethods from vllm.model_executor.layers.quantization.base_config import ( # noqa: E501 QuantizationConfig, QuantizeMethodBase, ) from vllm.model_executor.layers.quantization.kv_cache import BaseKVCacheMethod from vllm.model_executor.layers.quantization.quark.quark_moe import ( # noqa: E501 QuarkMoEMethod, ) from vllm.model_executor.layers.quantization.quark.schemes import ( QuarkOCP_MX, QuarkScheme, QuarkW8A8Fp8, QuarkW8A8Int8, ) from vllm.model_executor.layers.quantization.quark.utils import ( deep_compare, should_ignore_layer, ) from vllm.model_executor.models.utils import WeightsMapper from vllm.platforms import current_platform if TYPE_CHECKING: from vllm.model_executor.models.utils import WeightsMapper __all__ = ["QuarkLinearMethod"] logger = init_logger(__name__) class QuarkConfig(QuantizationConfig): def __init__( self, quant_config: dict[str, Any], kv_cache_group: list[str] | None = None, kv_cache_config: dict[str, Any] | None = None, pack_method: str = "reorder", ): super().__init__() if kv_cache_group is None: kv_cache_group = [] self.quant_config = quant_config self.kv_cache_group = kv_cache_group self.kv_cache_config = kv_cache_config self.pack_method = pack_method def get_linear_method(self) -> "QuarkLinearMethod": return QuarkLinearMethod(self) def get_supported_act_dtypes(cls) -> list[torch.dtype]: return [torch.float16, torch.bfloat16] @classmethod def get_min_capability(cls) -> int: return 70 def get_name(self) -> QuantizationMethods: return "quark" def apply_vllm_mapper( # noqa: B027 self, hf_to_vllm_mapper: "WeightsMapper" ): """ Interface for models to update module names referenced in quantization configs in order to reflect the vllm model structure :param hf_to_vllm_mapper: maps from hf model structure (the assumed structure of the qconfig) to vllm model structure """ quant_config_with_hf_to_vllm_mapper = {} for k, v in self.quant_config.items(): if isinstance(v, list): quant_config_with_hf_to_vllm_mapper[k] = hf_to_vllm_mapper.apply_list(v) elif isinstance(v, dict): quant_config_with_hf_to_vllm_mapper[k] = hf_to_vllm_mapper.apply_dict(v) else: if isinstance(v, str): mapped_v_list = hf_to_vllm_mapper.apply_list([v]) if mapped_v_list: quant_config_with_hf_to_vllm_mapper[k] = mapped_v_list[0] else: quant_config_with_hf_to_vllm_mapper[k] = v self.quant_config = quant_config_with_hf_to_vllm_mapper def get_quant_method( self, layer: torch.nn.Module, prefix: str ) -> Optional["QuantizeMethodBase"]: # Check if the layer is skipped for quantization. exclude_layers = cast(list[str], self.quant_config.get("exclude")) if should_ignore_layer( prefix, ignore=exclude_layers, fused_mapping=self.packed_modules_mapping ): return UnquantizedLinearMethod() if isinstance(layer, LinearBase): scheme = self.get_scheme(layer=layer, layer_name=prefix) layer.scheme = scheme return QuarkLinearMethod(self) if isinstance(layer, Attention): return QuarkKVCacheMethod(self) if isinstance(layer, FusedMoE): return QuarkMoEMethod.get_moe_method(self, module=layer, layer_name=prefix) return None @classmethod def from_config(cls, config: dict[str, Any]) -> "QuarkConfig": export_config = config.get("export") if export_config is None: raise ValueError( "The export key should be included in " "the configurations of Quark quantized model" ) kv_cache_group = cast(list[str], export_config.get("kv_cache_group")) pack_method = cast(str, export_config.get("pack_method")) # In the export model of quark, the quantization configuration # of kv_cache is stored in layer_quant_config. First, it is # judged whether kv_cache_group exists, and then it is judged # whether layer_quant_config has a quantization configuration # that matches kv_cache. if len(kv_cache_group) == 0: kv_cache_config = None else: kv_cache_set = set(kv_cache_group) layer_quant_config = cast(dict[str, Any], config.get("layer_quant_config")) layer_quant_names = list(layer_quant_config.keys()) layer_quant_set = set(layer_quant_names) if not ( kv_cache_set.issubset(layer_quant_set) or any( fnmatch.fnmatchcase(layer_quant, pat) for layer_quant in list(layer_quant_set) for pat in list(kv_cache_set) ) ): raise ValueError( "The Quark quantized model has the " "kv_cache_group parameter setting, " "but no kv_cache quantization settings " "were found in the quantization " "configuration." ) q_configs = [ quant_cfg for name, quant_cfg in layer_quant_config.items() if any(fnmatch.fnmatchcase(name, pattern) for pattern in kv_cache_group) ] if not all( deep_compare(q_config["output_tensors"], q_configs[0]["output_tensors"]) for q_config in q_configs ): raise ValueError( "The quantization method used for kv_cache should " "be the same, but the quantization method for the " "kv_cache layer in the config is different." ) kv_cache_config = q_configs[0].get("output_tensors") if kv_cache_config is None: raise ValueError("The kv_cache quantization configuration is empty.") # Since we have already set kv_cache quantization configurations, # we will remove the quantization configuration for the # output_tensors corresponding to the kv_cache layer. for q_config in q_configs: q_config["output_tensors"] = None # In case q_proj output is also quantized, remove the configuration # to keep qkv consistency. q_proj_q_config = cast(dict[str, Any], layer_quant_config.get("*q_proj")) if q_proj_q_config is not None: q_proj_q_config["output_tensors"] = None return cls( quant_config=config, kv_cache_group=kv_cache_group, kv_cache_config=kv_cache_config, pack_method=pack_method, ) @classmethod def get_config_filenames(cls) -> list[str]: return [] def _check_scheme_supported(self, min_capability: int, error: bool = True) -> bool: capability_tuple = current_platform.get_device_capability() if capability_tuple is not None: capability = capability_tuple.to_int() supported = capability >= min_capability if error and not supported: raise RuntimeError( "Quantization scheme is not supported for ", f"the current GPU. Min capability: {min_capability}. ", f"Current capability: {capability}.", ) return supported else: return False def _is_fp8_w4a8( self, weight_quant: list[dict[str, Any]] | None, input_quant: dict[str, Any] | None, ) -> bool: # Confirm weights and input quantized. if weight_quant is None or input_quant is None: return False if not isinstance(weight_quant, list) or len(weight_quant) != 2: return False # Confirm weight scheme is supported is_w4a8_dtype = ( weight_quant[0].get("dtype") == "fp8_e4m3" and weight_quant[1].get("dtype") == "int4" and input_quant.get("dtype") == "fp8_e4m3" ) is_static_weight = not weight_quant[0].get("is_dynamic") and not weight_quant[ 1 ].get("is_dynamic") is_per_tensor_fp8_and_per_channel_int4_weight = ( weight_quant[0].get("qscheme") == "per_tensor" and weight_quant[1].get("qscheme") == "per_channel" and weight_quant[1].get("symmetric") is True and weight_quant[1].get("ch_axis") == 0 ) if not ( is_w4a8_dtype and is_static_weight and is_per_tensor_fp8_and_per_channel_int4_weight ): return False # Dynamic quantization is always supported if weights supported. if input_quant.get("is_dynamic"): return True # Confirm activation scheme is supported. is_per_tensor_activation = input_quant.get("qscheme") == "per_tensor" return is_per_tensor_activation def _is_fp8_w8a8( self, weight_quant: dict[str, Any] | None, input_quant: dict[str, Any] | None, ) -> bool: # Confirm weights and input quantized. if weight_quant is None or input_quant is None: return False # Confirm weight scheme is supported is_fp8_dtype = ( weight_quant.get("dtype") == "fp8_e4m3" and input_quant.get("dtype") == "fp8_e4m3" ) is_static_weight = not weight_quant.get("is_dynamic") is_per_tensor_or_channel_weight = weight_quant.get("qscheme") in [ "per_tensor", "per_channel", ] if not (is_fp8_dtype and is_static_weight and is_per_tensor_or_channel_weight): return False # Dynamic quantization is always supported if weights supported. if input_quant.get("is_dynamic"): return True # Confirm activation scheme is supported. is_per_tensor_activation = input_quant.get("qscheme") == "per_tensor" return is_per_tensor_activation def _is_static_tensor_w8a8( self, weight_quant: dict[str, Any] | None, input_quant: dict[str, Any] | None, ) -> bool: # Confirm weights and input quantized. if weight_quant is None or input_quant is None: return False is_int8_dtype = ( weight_quant.get("dtype") == "int8" and input_quant.get("dtype") == "int8" ) is_tensor = ( weight_quant.get("qscheme") in ["per_tensor", "per_channel"] and input_quant.get("qscheme") == "per_tensor" ) is_static = not weight_quant.get("is_dynamic") and not input_quant.get( "is_dynamic" ) is_weight_symmetric = weight_quant.get("symmetric") is True # Both symmetric and asymmetric input quantization supported. # Only symmetric weight quantization supported. return is_int8_dtype and is_tensor and is_weight_symmetric and is_static def _is_ocp_mx( self, weight_quant: dict[str, Any] | None, input_quant: dict[str, Any] | None, ) -> bool: # Confirm weights and input quantized. if weight_quant is None or input_quant is None: logger.debug( "Quark model is not in OCP MX format: " "weight_quant or input_quant not set" ) return False # Input and weight qscheme needs to be per group. if ( weight_quant.get("qscheme") != "per_group" or input_quant.get("qscheme") != "per_group" ): logger.debug("Quark model is not in OCP MX format: not per_group") return False # Input and weight group size needs to be 32. if weight_quant.get("group_size") != 32 or input_quant.get("group_size") != 32: logger.debug("Quark model is not in OCP MX format: not group_size=32") return False # Activations and weight scales need to be in e8m0 format. if ( weight_quant.get("scale_format") != "e8m0" or input_quant.get("scale_format") != "e8m0" ): logger.debug("Quark model is not in OCP MX format: not scale_format e8m0") return False # Input and weight dtypes need to be any of fp4, # fp6_e3m2 or fp6_e3m2, possibly mixed. if weight_quant.get("dtype") not in { "fp4", "fp6_e3m2", "fp6_e2m3", } or input_quant.get("dtype") not in {"fp4", "fp6_e3m2", "fp6_e2m3"}: logger.debug( "Quark model is not in OCP MX format: dtype not fp4, fp6_e3m2, fp6_e2m3" ) return False return True def _find_matched_config( self, layer_name: str, module: torch.nn.Module ) -> dict[str, Any]: proj_name = layer_name.split(".")[-1] if proj_name in self.packed_modules_mapping: shard_proj_names = self.packed_modules_mapping[proj_name] # Convert fused_name --> [shard_names] shard_names = [ layer_name.replace(proj_name, shard_proj_name) for shard_proj_name in shard_proj_names ] shard_configs = [ self._find_matched_config(shard_name, module) for shard_name in shard_names ] if not all( deep_compare(q_config, shard_configs[0]) for q_config in shard_configs ): raise ValueError( f"Found a different quantization configuration for " f"{shard_proj_names} in {layer_name}. vLLM " "requires all to use the same scheme." ) return shard_configs[0] else: layer_quant_config = cast( dict[str, Any], self.quant_config.get("layer_quant_config") ) def _matches_pattern(layer_name, pattern): if "*" not in pattern: return layer_name in pattern return fnmatch.fnmatch(layer_name, pattern) for name_pattern, config in layer_quant_config.items(): if _matches_pattern(layer_name, name_pattern): return config layer_type = cast(str, type(module)) layer_type_quant_config = cast( dict[str, Any], self.quant_config.get("layer_type_quant_config") ) if layer_type in layer_type_quant_config: return layer_type_quant_config[layer_type] global_quant_config = cast( dict[str, Any], self.quant_config.get("global_quant_config") ) return global_quant_config def _get_scheme_from_config(self, config: dict[str, Any]) -> "QuarkScheme": if config.get("output_tensors") or config.get("bias"): raise NotImplementedError( "Currently, Quark models with output_tensors " "and bias quantized are not supported" ) weight_config = cast(dict[str, Any], config.get("weight")) input_config = cast(dict[str, Any], config.get("input_tensors")) if self._is_fp8_w8a8(weight_config, input_config): is_fp8_w8a8_supported = self._check_scheme_supported( QuarkW8A8Fp8.get_min_capability(), error=False ) if is_fp8_w8a8_supported: return QuarkW8A8Fp8(weight_config, input_config) elif self._is_static_tensor_w8a8(weight_config, input_config): weight_qscheme = cast(str, weight_config.get("qscheme")) return QuarkW8A8Int8( qscheme=weight_qscheme, is_static_input_scheme=True, input_symmetric=input_config.get("symmetric"), ) elif self._is_ocp_mx(weight_config, input_config): return QuarkOCP_MX(weight_config, input_config) raise NotImplementedError( "No quark compatible scheme was found. " f"Weight config: {weight_config}, " f"Input config: {input_config}" ) def get_scheme(self, layer: torch.nn.Module, layer_name: str) -> "QuarkScheme": layer_quant_config = self._find_matched_config(layer_name, layer) # Find the quant_scheme scheme = self._get_scheme_from_config(layer_quant_config) # Raise error if device does not support the scheme # (e.g. fp8 needs ada lovelace) self._check_scheme_supported(scheme.get_min_capability()) return scheme def get_cache_scale(self, name: str) -> str | None: """ Check whether the param name matches the format for k/v cache scales in quark. If this is the case, return its equivalent param name expected by vLLM :param name: param name :return: matching param name for KV cache scale in vLLM """ if name.endswith(".output_scale") and ".k_proj" in name: return name.replace(".k_proj.output_scale", ".attn.k_scale") if name.endswith(".output_scale") and ".v_proj" in name: return name.replace(".v_proj.output_scale", ".attn.v_scale") if name.endswith(".output_scale") and ".q_proj" in name: return name.replace(".q_proj.output_scale", ".attn.q_scale") if name.endswith("self_attn.prob_output_scale"): return name.replace(".prob_output_scale", ".attn.prob_scale") # If no matches, return None return None class QuarkLinearMethod(LinearMethodBase): def __init__(self, quantization_config: QuarkConfig): self.quantization_config = quantization_config def process_weights_after_loading(self, layer: torch.nn.Module) -> None: layer.scheme.process_weights_after_loading(layer) def create_weights( self, layer: torch.nn.Module, input_size_per_partition: int, output_partition_sizes: list[int], input_size: int, output_size: int, params_dtype: torch.dtype, **extra_weight_attrs, ): """ Use the CompressedTensorsScheme associated with each layer to create the necessary parameters for the layer. See LinearMethodBase for param details """ weight_loader = extra_weight_attrs.get("weight_loader") layer.scheme.create_weights( layer=layer, input_size=input_size, input_size_per_partition=input_size_per_partition, output_partition_sizes=output_partition_sizes, output_size=output_size, params_dtype=params_dtype, weight_loader=weight_loader, ) def apply( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ): """ Use the output of create_weights and the CompressedTensorsScheme associated with the layer to apply the forward pass with the layer input. See LinearMethodBase for param details """ scheme = layer.scheme if scheme is None: raise ValueError("A scheme must be defined for each layer") return scheme.apply_weights(layer, x, bias=bias) class QuarkKVCacheMethod(BaseKVCacheMethod): """ Supports loading kv-cache scaling factors from quark checkpoints. """ def __init__(self, quant_config: QuarkConfig): self.validate_kv_cache_config(quant_config.kv_cache_config) super().__init__(quant_config) @staticmethod def validate_kv_cache_config(kv_cache_config: dict[str, Any] | None): """ Validator for the kv cache configuration. Useful for controlling the kv cache quantization schemes, that are being supported in vLLM :param kv_cache_config: the quark kv cache scheme """ if kv_cache_config is None: return dtype = kv_cache_config.get("dtype") if dtype != "fp8_e4m3": raise NotImplementedError( "Currently supported kv cache quantization is " f"dtype=fp8_e4m3, however received {dtype}" ) qscheme = kv_cache_config.get("qscheme") if qscheme != "per_tensor": raise NotImplementedError( "Only support per-tensor scaling factor " "for quark KV cache. " f"Expected qscheme: per_tensor, found qscheme: {qscheme}" )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/quark/utils.py
vllm/model_executor/layers/quantization/quark/utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Iterable, Mapping from types import MappingProxyType from typing import Any import regex as re def deep_compare(dict1: Any, dict2: Any) -> bool: if type(dict1) is not type(dict2): return False if isinstance(dict1, dict): if dict1.keys() != dict2.keys(): return False return all(deep_compare(dict1[k], dict2[k]) for k in dict1) elif isinstance(dict1, list): return set(dict1) == set(dict2) else: return dict1 == dict2 def should_ignore_layer( layer_name: str | None, ignore: Iterable[str], fused_mapping: Mapping[str, list[str]] = MappingProxyType({}), ) -> bool: if layer_name is None: return False # layer_name = model.layers.0.self_attn.qkv_proj # proj_name = qkv_proj proj_name = layer_name.split(".")[-1] # Fused layers like gate_up_proj or qkv_proj will not be fused # in the safetensors checkpoint. So, we convert the name # from the fused version to unfused + check to make sure that # each shard of the fused layer has the same scheme. if proj_name in fused_mapping: shard_proj_names = fused_mapping[proj_name] # Convert fused_name --> [shard_names] shard_names = [ layer_name.replace(proj_name, shard_proj_name) for shard_proj_name in shard_proj_names ] # Layer should be ignored if shards are ignored. should_ignore_layer = None for shard_name in shard_names: should_ignore_shard = check_equal_or_regex_match( layer_name=shard_name, targets=ignore ) # If shard_idx=0, set layer ignore to match shard. if should_ignore_layer is None: should_ignore_layer = should_ignore_shard # If shard_idx=1+ confirm scheme matches prior shards. elif should_ignore_shard != should_ignore_layer: raise ValueError( f"Found a different quantization schemes for " f"{shard_proj_names} in {layer_name}. vLLM " "requires all to use the same scheme." ) # Unfused layers like down_proj and o_proj will match # the safetensors checkpoint already. else: should_ignore_layer = check_equal_or_regex_match( layer_name=layer_name, targets=ignore ) assert should_ignore_layer is not None return should_ignore_layer def check_equal_or_regex_match(layer_name: str, targets: Iterable[str]) -> bool: """ Checks whether a layer_name is exactly equal or a regex match for if target starts with 're:' to any target in list. """ return any(_is_equal_or_regex_match(layer_name, target) for target in targets) def _is_equal_or_regex_match( value: str, target: str, check_contains: bool = False ) -> bool: """ Checks whether a value is exactly equal or a regex match for target if target starts with 're:'. If check_contains is set to True, additionally checks if the target string is contained within the value. """ if target.startswith("re:"): pattern = target[3:] if re.match(pattern, value): return True elif check_contains: if target.lower() in value.lower(): return True elif target == value: return True return False
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/quark/__init__.py
vllm/model_executor/layers/quantization/quark/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/quark/quark_moe.py
vllm/model_executor/layers/quantization/quark/quark_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from typing import Any import torch import vllm.envs as envs from vllm import _custom_ops as ops from vllm._aiter_ops import rocm_aiter_ops from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe import ( FusedMoE, FusedMoEConfig, FusedMoEMethodBase, FusedMoeWeightScaleSupported, ) from vllm.model_executor.layers.fused_moe.config import ( FusedMoEQuantConfig, fp8_w8a8_moe_quant_config, ocp_mx_moe_quant_config, ) from vllm.model_executor.layers.fused_moe.fused_marlin_moe import fused_marlin_moe from vllm.model_executor.layers.quantization.utils.marlin_utils_fp8 import ( prepare_moe_fp8_layer_for_marlin, ) from vllm.model_executor.layers.quantization.utils.ocp_mx_utils import ( OCP_MX_BLOCK_SIZE, OCP_MX_Scheme, ) from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape from vllm.model_executor.layers.quantization.utils.w8a8_utils import ( all_close_1d, normalize_e4m3fn_to_e4m3fnuz, per_tensor_dequantize, ) from vllm.model_executor.utils import set_weight_attrs from vllm.platforms import current_platform from vllm.scalar_type import scalar_types logger = init_logger(__name__) __all__ = ["QuarkMoEMethod", "QuarkW8A8Fp8MoEMethod", "QuarkOCP_MX_MoEMethod"] class QuarkMoEMethod(FusedMoEMethodBase): def __init__(self, moe: FusedMoEConfig): super().__init__(moe) @staticmethod def get_moe_method( quant_config: "QuarkConfig", # type: ignore # noqa E501 # noqa F821 module: torch.nn.Module, layer_name: str, ) -> "QuarkMoEMethod": layer_quant_config = quant_config._find_matched_config(layer_name, module) if layer_quant_config.get("output_tensors") or layer_quant_config.get("bias"): raise NotImplementedError( "Currently, Quark models with " "output_tensors and bias " "quantized are not supported" ) weight_config = layer_quant_config.get("weight") input_config = layer_quant_config.get("input_tensors") if quant_config._is_fp8_w4a8(weight_config, input_config): return QuarkW4A8Fp8MoEMethod(weight_config, input_config, module.moe_config) elif quant_config._is_fp8_w8a8(weight_config, input_config): return QuarkW8A8Fp8MoEMethod(weight_config, input_config, module.moe_config) elif quant_config._is_ocp_mx(weight_config, input_config): return QuarkOCP_MX_MoEMethod(weight_config, input_config, module.moe_config) else: raise RuntimeError("Unsupported FusedMoe scheme") class QuarkW8A8Fp8MoEMethod(QuarkMoEMethod): def __init__( self, weight_config: dict[str, Any], input_config: dict[str, Any], moe: FusedMoEConfig, ): super().__init__(moe) self.weight_quant = weight_config self.input_quant = input_config self.weight_qscheme = self.weight_quant.get("qscheme") self.input_qscheme = self.input_quant.get("qscheme") per_tensor = ( self.weight_qscheme == "per_tensor" and self.input_qscheme == "per_tensor" ) per_channel = ( self.weight_qscheme == "per_channel" and self.input_qscheme == "per_channel" ) self.act_quant_group_shape = ( GroupShape.PER_TOKEN if per_channel else GroupShape.PER_TENSOR ) if not (per_tensor or per_channel): raise ValueError( "For FP8 Fused MoE layers, only per-tensor and per-channel " "scales for weights and activations are supported. Found " f"{self.weight_qscheme}, {self.input_qscheme}" ) # noqa E501 self.static_input_scales = not self.input_quant.get("is_dynamic") if self.static_input_scales and per_channel: raise ValueError( "For FP8 Fused MoE layer, we require either per tensor or " "channelwise, dynamic per token quantization." ) # For GPUs that lack FP8 hardware support, we can leverage the Marlin # kernel for fast weight-only FP8 quantization self.use_marlin = ( not current_platform.has_device_capability(89) or envs.VLLM_TEST_FORCE_FP8_MARLIN ) # Disable marlin for rocm if current_platform.is_rocm(): self.use_marlin = False self.rocm_aiter_moe_enabled = rocm_aiter_ops.is_fused_moe_enabled() def create_weights( self, layer: torch.nn.Module, num_experts: int, hidden_size: int, intermediate_size_per_partition: int, params_dtype: torch.dtype, **extra_weight_attrs, ): layer.intermediate_size_per_partition = intermediate_size_per_partition layer.hidden_size = hidden_size layer.num_experts = num_experts layer.orig_dtype = params_dtype layer.weight_block_size = None params_dtype = torch.float8_e4m3fn # WEIGHTS w13_weight = torch.nn.Parameter( torch.empty( num_experts, 2 * intermediate_size_per_partition, hidden_size, dtype=params_dtype, ), requires_grad=False, ) layer.register_parameter("w13_weight", w13_weight) set_weight_attrs(w13_weight, extra_weight_attrs) w2_weight = torch.nn.Parameter( torch.empty( num_experts, hidden_size, intermediate_size_per_partition, dtype=params_dtype, ), requires_grad=False, ) layer.register_parameter("w2_weight", w2_weight) set_weight_attrs(w2_weight, extra_weight_attrs) # WEIGHT_SCALES if self.weight_qscheme == "per_tensor": # Allocate 2 scales for w1 and w3 respectively. # They are combined to a single scale after weight loading. w13_weight_scale = torch.nn.Parameter( torch.ones(num_experts, 2, dtype=torch.float32), requires_grad=False ) layer.register_parameter("w13_weight_scale", w13_weight_scale) w2_weight_scale = torch.nn.Parameter( torch.ones(num_experts, dtype=torch.float32), requires_grad=False ) layer.register_parameter("w2_weight_scale", w2_weight_scale) # Add PER-TENSOR quantization for FusedMoE.weight_loader. extra_weight_attrs.update( {"quant_method": FusedMoeWeightScaleSupported.TENSOR.value} ) set_weight_attrs(w13_weight_scale, extra_weight_attrs) set_weight_attrs(w2_weight_scale, extra_weight_attrs) elif self.weight_qscheme == "per_channel": # quark's scale is 1 dim. w13_weight_scale = torch.nn.Parameter( torch.ones( num_experts, 2 * intermediate_size_per_partition, dtype=torch.float32, ), requires_grad=False, ) layer.register_parameter("w13_weight_scale", w13_weight_scale) w2_weight_scale = torch.nn.Parameter( torch.ones(num_experts, hidden_size, dtype=torch.float32), requires_grad=False, ) layer.register_parameter("w2_weight_scale", w2_weight_scale) # Add PER-CHANNEL quantization for FusedMoE.weight_loader. extra_weight_attrs.update( {"quant_method": FusedMoeWeightScaleSupported.CHANNEL.value} ) set_weight_attrs(w13_weight_scale, extra_weight_attrs) set_weight_attrs(w2_weight_scale, extra_weight_attrs) # INPUT_SCALES if self.static_input_scales: w13_input_scale = torch.nn.Parameter( torch.ones(num_experts, dtype=torch.float32), requires_grad=False ) layer.register_parameter("w13_input_scale", w13_input_scale) set_weight_attrs(w13_input_scale, extra_weight_attrs) w2_input_scale = torch.nn.Parameter( torch.ones(num_experts, dtype=torch.float32), requires_grad=False ) layer.register_parameter("w2_input_scale", w2_input_scale) set_weight_attrs(w2_input_scale, extra_weight_attrs) else: layer.w13_input_scale = None layer.w2_input_scale = None def process_weights_after_loading(self, layer: torch.nn.Module) -> None: # Fp8 moe kernels require a single activation scale. # We take the max of all the scales in case they differ. if self.static_input_scales: if layer.w13_input_scale is None or layer.w2_input_scale is None: raise ValueError( "QuantConfig has static quantization, but found " "activation scales are None." ) if not all_close_1d(layer.w13_input_scale) or not all_close_1d( layer.w2_input_scale ): logger.warning_once( "Found input_scales that are not equal for " "fp8 MoE layer. Using the maximum across experts " "for each layer. " ) layer.w13_input_scale = torch.nn.Parameter( layer.w13_input_scale.max(), requires_grad=False ) layer.w2_input_scale = torch.nn.Parameter( layer.w2_input_scale.max(), requires_grad=False ) if current_platform.is_fp8_fnuz(): # Normalize the weights and scales w13_weight, w13_weight_scale, w13_input_scale = ( normalize_e4m3fn_to_e4m3fnuz( layer.w13_weight, layer.w13_weight_scale, layer.w13_input_scale ) ) w2_weight, w2_weight_scale, w2_input_scale = normalize_e4m3fn_to_e4m3fnuz( layer.w2_weight, layer.w2_weight_scale, layer.w2_input_scale ) # Reset the parameter layer.w13_weight = torch.nn.Parameter(w13_weight, requires_grad=False) layer.w13_weight_scale = torch.nn.Parameter( w13_weight_scale, requires_grad=False ) if w13_input_scale is not None: layer.w13_input_scale = torch.nn.Parameter( w13_input_scale, requires_grad=False ) layer.w2_weight = torch.nn.Parameter(w2_weight, requires_grad=False) layer.w2_weight_scale = torch.nn.Parameter( w2_weight_scale, requires_grad=False ) if w2_input_scale is not None: layer.w2_input_scale = torch.nn.Parameter( w2_input_scale, requires_grad=False ) # For per-tensor case, Fp8 moe kernel needs single weight scale # for w13 per expert. Use max then dequant and requant each expert. if self.weight_qscheme == "per_tensor": assert layer.w13_weight_scale is not None shard_size = layer.intermediate_size_per_partition max_w13_scales = layer.w13_weight_scale.max(dim=1).values for expert_id in range(layer.local_num_experts): start = 0 for shard_id in range(2): dq_weight = per_tensor_dequantize( layer.w13_weight[expert_id][start : start + shard_size, :], layer.w13_weight_scale[expert_id][shard_id], ) layer.w13_weight[expert_id][start : start + shard_size, :], _ = ( ops.scaled_fp8_quant(dq_weight, max_w13_scales[expert_id]) ) start += shard_size layer.w13_weight_scale = torch.nn.Parameter( max_w13_scales, requires_grad=False ) # quark's scale is 1 dim. elif self.weight_qscheme == "per_channel": if self.act_quant_group_shape == GroupShape.PER_TOKEN: w13_weight_scale = layer.w13_weight_scale.unsqueeze(-1) layer.w13_weight_scale = torch.nn.Parameter( w13_weight_scale, requires_grad=False ) w2_weight_scale = layer.w2_weight_scale.unsqueeze(-1) layer.w2_weight_scale = torch.nn.Parameter( w2_weight_scale, requires_grad=False ) # Property to determine if AITER is used if self.rocm_aiter_moe_enabled: # reshaping weights is required for aiter moe kernel. shuffled_w13, shuffled_w2 = rocm_aiter_ops.shuffle_weights( layer.w13_weight.data, layer.w2_weight.data ) layer.w13_weight = torch.nn.Parameter(shuffled_w13, requires_grad=False) layer.w2_weight = torch.nn.Parameter(shuffled_w2, requires_grad=False) elif self.use_marlin: (workspace, w13_weight, w2_weight, w13_weight_scale, w2_weight_scale) = ( prepare_moe_fp8_layer_for_marlin( layer, layer.w13_weight, layer.w2_weight, layer.w13_weight_scale, layer.w2_weight_scale, ) ) layer.workspace = workspace # TODO(rob): once we apply refactor to Quark, switch to using # replace_parameter for compatibility with reloading in RL. layer.w13_weight = torch.nn.Parameter(w13_weight, requires_grad=False) layer.w2_weight = torch.nn.Parameter(w2_weight, requires_grad=False) layer.w13_weight_scale = torch.nn.Parameter( w13_weight_scale, requires_grad=False ) layer.w2_weight_scale = torch.nn.Parameter( w2_weight_scale, requires_grad=False ) def get_fused_moe_quant_config( self, layer: torch.nn.Module ) -> FusedMoEQuantConfig | None: return fp8_w8a8_moe_quant_config( w1_scale=layer.w13_weight_scale, w2_scale=layer.w2_weight_scale, a1_scale=layer.w13_input_scale, a2_scale=layer.w2_input_scale, per_act_token_quant=self.input_qscheme == "per_channel", per_out_ch_quant=self.weight_qscheme == "per_channel", ) def apply( self, layer: FusedMoE, x: torch.Tensor, router_logits: torch.Tensor, ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]: topk_weights, topk_ids = layer.select_experts( hidden_states=x, router_logits=router_logits, ) if self.rocm_aiter_moe_enabled: from vllm.model_executor.layers.fused_moe.rocm_aiter_fused_moe import ( rocm_aiter_fused_experts, ) return rocm_aiter_fused_experts( hidden_states=x, w1=layer.w13_weight, w2=layer.w2_weight, topk_weights=topk_weights, topk_ids=topk_ids, activation=layer.activation, apply_router_weight_on_input=layer.apply_router_weight_on_input, quant_config=self.moe_quant_config, expert_map=layer.expert_map, ) elif self.use_marlin: assert layer.activation == "silu", ( f"{layer.activation} not supported for Marlin MoE." ) return fused_marlin_moe( x, layer.w13_weight, layer.w2_weight, None, None, layer.w13_weight_scale, layer.w2_weight_scale, router_logits, topk_weights, topk_ids, quant_type_id=scalar_types.float8_e4m3fn.id, apply_router_weight_on_input=layer.apply_router_weight_on_input, global_num_experts=layer.global_num_experts, expert_map=layer.expert_map, ) else: from vllm.model_executor.layers.fused_moe import fused_experts return fused_experts( hidden_states=x, w1=layer.w13_weight, w2=layer.w2_weight, topk_weights=topk_weights, topk_ids=topk_ids, inplace=True, activation=layer.activation, apply_router_weight_on_input=layer.apply_router_weight_on_input, global_num_experts=layer.global_num_experts, expert_map=layer.expert_map, quant_config=self.moe_quant_config, ) class QuarkW4A8Fp8MoEMethod(QuarkMoEMethod): def __init__( self, weight_config: dict[str, Any], input_config: dict[str, Any], moe: FusedMoEConfig, ): super().__init__(moe) self.weight_quant = weight_config self.input_quant = input_config assert rocm_aiter_ops.is_fused_moe_enabled(), ( "W4A8 FP8 MoE requires ROCm AITER fused MoE support." ) def create_weights( self, layer: torch.nn.Module, num_experts: int, hidden_size: int, intermediate_size_per_partition: int, params_dtype: torch.dtype, **extra_weight_attrs, ): params_dtype = torch.uint32 w13_weight = torch.nn.Parameter( torch.empty( num_experts, 2 * intermediate_size_per_partition, hidden_size // 8, # INT32 packing for W4 dtype=params_dtype, ), requires_grad=False, ) w2_weight = torch.nn.Parameter( torch.empty( num_experts, hidden_size, intermediate_size_per_partition // 8, # INT32 packing for W4 dtype=params_dtype, ), requires_grad=False, ) layer.register_parameter("w13_weight", w13_weight) layer.register_parameter("w2_weight", w2_weight) set_weight_attrs(w13_weight, extra_weight_attrs) set_weight_attrs(w2_weight, extra_weight_attrs) # Per-tensor fp8 weight scales w13_weight_scale = torch.nn.Parameter( torch.ones(num_experts, 2, dtype=torch.float32), requires_grad=False ) w2_weight_scale = torch.nn.Parameter( torch.ones(num_experts, dtype=torch.float32), requires_grad=False ) layer.register_parameter("w13_weight_scale", w13_weight_scale) layer.register_parameter("w2_weight_scale", w2_weight_scale) extra_weight_attrs.update( {"quant_method": FusedMoeWeightScaleSupported.TENSOR.value} ) set_weight_attrs(w13_weight_scale, extra_weight_attrs) set_weight_attrs(w2_weight_scale, extra_weight_attrs) # Per-channel int4 weight scales w13_weight_scale_2 = torch.nn.Parameter( torch.ones( num_experts, 2 * intermediate_size_per_partition, dtype=torch.float32, ), requires_grad=False, ) w2_weight_scale_2 = torch.nn.Parameter( torch.ones(num_experts, hidden_size, dtype=torch.float32), requires_grad=False, ) layer.register_parameter("w13_weight_scale_2", w13_weight_scale_2) layer.register_parameter("w2_weight_scale_2", w2_weight_scale_2) extra_weight_attrs.update( {"quant_method": FusedMoeWeightScaleSupported.CHANNEL.value} ) set_weight_attrs(w13_weight_scale_2, extra_weight_attrs) set_weight_attrs(w2_weight_scale_2, extra_weight_attrs) def process_weights_after_loading(self, layer: torch.nn.Module) -> None: shuffled_w13, shuffled_w2 = rocm_aiter_ops.shuffle_weights( layer.w13_weight.data, layer.w2_weight.data ) layer.w13_weight = torch.nn.Parameter(shuffled_w13, requires_grad=False) layer.w2_weight = torch.nn.Parameter(shuffled_w2, requires_grad=False) # INT4-FP8 : offset INT4 w13_weight_scale1 to single w13_weight_scale # Fp8 moe kernel needs single fp8 w13_weight_scale for w13 per expert. # We won't do requant each expert's fp8 weight (not direct available), # instead we adjust half of INT4 w13_weight_scale1 numbers shard_size = layer.intermediate_size_per_partition max_w13_scales = layer.w13_weight_scale.max(dim=1).values assert torch.all(max_w13_scales != 0), "fp8 weight scale cannot be zero." for expert_id in range(layer.local_num_experts): start = 0 max_w13_scale_fp8 = max_w13_scales[expert_id] for shard_id in range(2): if layer.w13_weight_scale[expert_id][shard_id] != max_w13_scale_fp8: int4_rescale = ( layer.w13_weight_scale[expert_id][shard_id] / max_w13_scale_fp8 ) layer.w13_weight_scale_2[expert_id][start : start + shard_size] *= ( int4_rescale ) start += shard_size layer.w13_weight_scale = torch.nn.Parameter(max_w13_scales, requires_grad=False) # special hack to asm_moe, which takes (weight_scale1 * weight_scale) as post # GEMM scaling optimal design - shall apply per-column weight_scale1 before # GEMM, and weight_scale post for expert_id in range(layer.local_num_experts): layer.w13_weight_scale_2[expert_id] *= max_w13_scales[expert_id] layer.w2_weight_scale_2[expert_id] *= layer.w2_weight_scale[expert_id] def get_fused_moe_quant_config(self, layer): return fp8_w8a8_moe_quant_config( w1_scale=layer.w13_weight_scale_2, w2_scale=layer.w2_weight_scale_2, per_out_ch_quant=True, ) def apply( self, layer: FusedMoE, x: torch.Tensor, router_logits: torch.Tensor, ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]: topk_weights, topk_ids = layer.select_experts( hidden_states=x, router_logits=router_logits, ) from vllm.model_executor.layers.fused_moe.rocm_aiter_fused_moe import ( rocm_aiter_fused_experts, ) return rocm_aiter_fused_experts( hidden_states=x, w1=layer.w13_weight, w2=layer.w2_weight, topk_weights=topk_weights, topk_ids=topk_ids, activation=layer.activation, apply_router_weight_on_input=layer.apply_router_weight_on_input, quant_config=self.moe_quant_config, expert_map=layer.expert_map, ) class QuarkOCP_MX_MoEMethod(QuarkMoEMethod): def __init__( self, weight_config: dict[str, Any], input_config: dict[str, Any], moe: FusedMoEConfig, ): super().__init__(moe) self.weight_quant = weight_config self.input_quant = input_config weight_qscheme = self.weight_quant.get("qscheme") input_qscheme = self.input_quant.get("qscheme") if not (weight_qscheme == "per_group" and input_qscheme == "per_group"): raise ValueError( "For MX(FP4) Fused MoE layers, only per-group scales " "for weights and activations are supported. Found " f"{weight_qscheme}, {input_qscheme}" ) # noqa E501 self.static_input_scales = not self.input_quant.get("is_dynamic") self.weight_dtype = self.weight_quant["dtype"].replace("fp", "mxfp") self.input_dtype = self.input_quant["dtype"].replace("fp", "mxfp") self.fp4_dtype = getattr(torch, "float4_e2m1fn_x2", None) self.ocp_mx_scheme = OCP_MX_Scheme.from_quant_dtype( self.input_dtype, self.weight_dtype ) if self.static_input_scales: raise NotImplementedError( "QuarkOCP_MX_MoEMethod with static input scales is currently " "not implemented. Please open an issue." ) self.use_rocm_aiter_moe = rocm_aiter_ops.is_fused_moe_enabled() self.emulate = not current_platform.supports_mx() or not ( self.use_rocm_aiter_moe and self.ocp_mx_scheme == "w_mxfp4_a_mxfp4" ) if self.emulate: logger.warning_once( f"The current mode (supports_mx={current_platform.supports_mx()}, " f"use_mxfp4_aiter_moe={self.use_rocm_aiter_moe}, " f"ocp_mx_scheme={self.ocp_mx_scheme}) " "does not support native MXFP4/MXFP6 " "computation. Simulated weight dequantization and activation " "QDQ (quantize and dequantize) will be used, with the linear " "layers computed in high precision." ) else: logger.warning_once( "The current mode supports native MoE MXFP4 computation" ) def get_packed_dim(self, dim: int, quant_dtype: str): if quant_dtype == "mxfp4": assert dim % 2 == 0 return dim // 2 else: # FP6 packs 4 * 6 = 24 bits on 3 bytes. assert (dim * 3) % 4 == 0 return (dim * 3) // 4 def create_weights( self, layer: torch.nn.Module, num_experts: int, hidden_size: int, intermediate_size_per_partition: int, params_dtype: torch.dtype, **extra_weight_attrs, ): # Add the quantization method used (per tensor/grouped/channel) # to ensure the weight scales are loaded in properly extra_weight_attrs.update( {"quant_method": FusedMoeWeightScaleSupported.BLOCK.value} ) params_dtype = torch.uint8 # WEIGHTS w13_weight = torch.nn.Parameter( torch.empty( num_experts, 2 * intermediate_size_per_partition, self.get_packed_dim(hidden_size, self.weight_dtype), dtype=params_dtype, ), requires_grad=False, ) layer.register_parameter("w13_weight", w13_weight) set_weight_attrs(w13_weight, extra_weight_attrs) w2_weight = torch.nn.Parameter( torch.empty( num_experts, hidden_size, self.get_packed_dim(intermediate_size_per_partition, self.weight_dtype), dtype=params_dtype, ), requires_grad=False, ) layer.register_parameter("w2_weight", w2_weight) set_weight_attrs(w2_weight, extra_weight_attrs) # WEIGHT_SCALES w13_weight_scale = torch.nn.Parameter( torch.ones( num_experts, 2 * intermediate_size_per_partition, hidden_size // OCP_MX_BLOCK_SIZE, dtype=params_dtype, ), requires_grad=False, ) w2_weight_scale = torch.nn.Parameter( torch.ones( num_experts, hidden_size, intermediate_size_per_partition // OCP_MX_BLOCK_SIZE, dtype=params_dtype, ), requires_grad=False, ) set_weight_attrs(w2_weight_scale, extra_weight_attrs) set_weight_attrs(w13_weight_scale, extra_weight_attrs) layer.register_parameter("w13_weight_scale", w13_weight_scale) layer.register_parameter("w2_weight_scale", w2_weight_scale) def process_weights_after_loading(self, layer): if self.emulate: return from aiter.utility.fp4_utils import e8m0_shuffle # Pre-shuffle weight scales s0, s1, _ = layer.w13_weight_scale.shape w13_weight_scale = layer.w13_weight_scale.view(s0 * s1, -1) w13_weight_scale = e8m0_shuffle(w13_weight_scale) layer.w13_weight_scale.data = w13_weight_scale.view(s0, s1, -1) s0, s1, _ = layer.w2_weight_scale.shape w2_weight_scale = layer.w2_weight_scale.view(s0 * s1, -1) w2_weight_scale = e8m0_shuffle(w2_weight_scale) layer.w2_weight_scale.data = w2_weight_scale.view(s0, s1, -1) if self.fp4_dtype is not None: layer.w13_weight = torch.nn.Parameter( layer.w13_weight.view(self.fp4_dtype), requires_grad=layer.w13_weight.requires_grad, ) layer.w2_weight = torch.nn.Parameter( layer.w2_weight.view(self.fp4_dtype), requires_grad=layer.w2_weight.requires_grad, ) torch.cuda.empty_cache() def get_fused_moe_quant_config( self, layer: torch.nn.Module ) -> FusedMoEQuantConfig | None: return ocp_mx_moe_quant_config( quant_dtype=self.input_dtype, weight_dtype=self.weight_dtype, w1_scale=layer.w13_weight_scale, w2_scale=layer.w2_weight_scale, a1_scale=None, a2_scale=None, block_shape=None, ) @property def allow_inplace(self) -> bool: return True def apply( self, layer: FusedMoE, x: torch.Tensor, router_logits: torch.Tensor, ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]: topk_weights, topk_ids = layer.select_experts( hidden_states=x, router_logits=router_logits, ) if not self.emulate: from vllm.model_executor.layers.fused_moe.rocm_aiter_fused_moe import ( rocm_aiter_fused_experts, ) out = rocm_aiter_fused_experts( x, layer.w13_weight, layer.w2_weight, topk_weights=topk_weights, topk_ids=topk_ids, activation=layer.activation, quant_config=self.moe_quant_config, expert_map=layer.expert_map, ) else: from vllm.model_executor.layers.fused_moe import fused_experts out = fused_experts( x, layer.w13_weight, layer.w2_weight, topk_weights=topk_weights, topk_ids=topk_ids, inplace=True, activation=layer.activation, global_num_experts=layer.global_num_experts, apply_router_weight_on_input=layer.apply_router_weight_on_input, expert_map=layer.expert_map, quant_config=self.moe_quant_config, ) return out
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/quark/schemes/quark_w8a8_fp8.py
vllm/model_executor/layers/quantization/quark/schemes/quark_w8a8_fp8.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable from typing import Any, cast import torch from torch.nn import Parameter from vllm.model_executor.layers.quantization.quark.schemes import QuarkScheme from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape from vllm.model_executor.layers.quantization.utils.w8a8_utils import ( Fp8LinearOp, normalize_e4m3fn_to_e4m3fnuz, requantize_with_max_scale, ) from vllm.model_executor.parameter import ( ChannelQuantScaleParameter, ModelWeightParameter, PerTensorScaleParameter, ) from vllm.platforms import current_platform __all__ = ["QuarkW8A8Fp8"] class QuarkW8A8Fp8(QuarkScheme): def __init__( self, weight_config: dict[str, Any], input_config: dict[str, Any] | None ): self.weight_qscheme = cast(str, weight_config.get("qscheme")) self.is_static_input_scheme: bool = False self.input_qscheme: str | None = None if input_config is not None: self.is_static_input_scheme = not cast(bool, input_config.get("is_dynamic")) self.input_qscheme = cast(str, input_config.get("qscheme")) per_token = ( not self.is_static_input_scheme and self.input_qscheme == "per_channel" ) self.act_quant_group_shape = ( GroupShape.PER_TOKEN if per_token else GroupShape.PER_TENSOR ) self.fp8_linear = Fp8LinearOp( act_quant_static=self.is_static_input_scheme, act_quant_group_shape=self.act_quant_group_shape, ) self.out_dtype = torch.get_default_dtype() @classmethod def get_min_capability(cls) -> int: # lovelace and up return 89 def process_weights_after_loading(self, layer) -> None: # If per tensor, when we have a fused module (e.g. QKV) with per # tensor scales (thus N scales being passed to the kernel), # requantize so we can always run per tensor if self.weight_qscheme == "per_tensor": if current_platform.is_fp8_fnuz(): input_scale = getattr(layer, "input_scale", None) weight, max_w_scale, input_scale = normalize_e4m3fn_to_e4m3fnuz( weight=layer.weight, weight_scale=layer.weight_scale, input_scale=input_scale, ) if input_scale is not None: layer.input_scale = Parameter(input_scale, requires_grad=False) else: max_w_scale = layer.weight_scale weight = layer.weight max_w_scale, weight = requantize_with_max_scale( weight=weight, weight_scale=max_w_scale, logical_widths=layer.logical_widths, ) layer.weight = Parameter(weight.t(), requires_grad=False) layer.weight_scale = Parameter(max_w_scale, requires_grad=False) # If channelwise, scales are already lined up, so just transpose. elif self.weight_qscheme == "per_channel": weight = layer.weight if current_platform.is_fp8_fnuz(): input_scale = getattr(layer, "input_scale", None) weight, weight_scale, input_scale = normalize_e4m3fn_to_e4m3fnuz( weight=weight, weight_scale=layer.weight_scale, input_scale=input_scale, ) if input_scale is not None: layer.input_scale = Parameter(input_scale, requires_grad=False) else: weight_scale = layer.weight_scale.data if self.act_quant_group_shape == GroupShape.PER_TOKEN: weight_scale = weight_scale.view(-1, 1) layer.weight = Parameter(weight.t(), requires_grad=False) # required by torch.compile to be torch.nn.Parameter layer.weight_scale = Parameter(weight_scale, requires_grad=False) else: raise ValueError(f"Unknown quantization scheme {self.weight_qscheme}") # INPUT SCALE if self.is_static_input_scheme: layer.input_scale = Parameter(layer.input_scale.max(), requires_grad=False) else: layer.input_scale = None def create_weights( self, layer: torch.nn.Module, output_partition_sizes: list[int], input_size_per_partition: int, params_dtype: torch.dtype, weight_loader: Callable, **kwargs, ): output_size_per_partition = sum(output_partition_sizes) layer.logical_widths = output_partition_sizes # WEIGHT weight = ModelWeightParameter( data=torch.empty( output_size_per_partition, input_size_per_partition, dtype=torch.float8_e4m3fn, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight", weight) # WEIGHT SCALE # TODO: update create_xxx_parameter functions to return # the newly added parameters if self.weight_qscheme == "per_channel": weight_scale = ChannelQuantScaleParameter( data=torch.empty((sum(output_partition_sizes)), dtype=torch.float32), output_dim=0, weight_loader=weight_loader, ) else: assert self.weight_qscheme == "per_tensor" weight_scale = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) # min requirement for fp8 kernels weight_scale[:] = torch.finfo(torch.float32).min layer.register_parameter("weight_scale", weight_scale) # INPUT SCALE if self.is_static_input_scheme: input_scale = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) input_scale[:] = torch.finfo(torch.float32).min layer.register_parameter("input_scale", input_scale) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: return self.fp8_linear.apply( input=x, weight=layer.weight, weight_scale=layer.weight_scale, out_dtype=self.out_dtype, input_scale=layer.input_scale, bias=bias, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/quark/schemes/quark_scheme.py
vllm/model_executor/layers/quantization/quark/schemes/quark_scheme.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from abc import ABC, abstractmethod import torch __all__ = ["QuarkScheme"] class QuarkScheme(ABC): """ Abstract class used to describe the weight creation and forward pass of different quantization schemes supported by Quark. """ @classmethod @abstractmethod def get_min_capability(cls) -> int: """ Get minimum device capability. """ raise NotImplementedError @abstractmethod def create_weights(self, *args, **kwargs): """ Weight creation for the particular scheme. Inputs to this function """ raise NotImplementedError @abstractmethod def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None ): """ Run the forward pass for the particular scheme. This is where scheme-specific dequant/quant steps/kernels should be applied. :param layer: torch.nn.Module with the registered weights and other parameters relevant to the particular scheme. :param x: input to the layer :param bias: bias parameter """ raise NotImplementedError @abstractmethod def process_weights_after_loading(self, layer: torch.nn.Module): """ Called after weight loading is complete for any cleanup that needs to occur. """ raise NotImplementedError
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/quark/schemes/quark_ocp_mx.py
vllm/model_executor/layers/quantization/quark/schemes/quark_ocp_mx.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable from fractions import Fraction from functools import cache, partial from typing import Any import torch import torch.nn.functional as F from vllm import envs from vllm._aiter_ops import rocm_aiter_ops from vllm.logger import init_logger from vllm.model_executor.layers.quantization.utils.mxfp4_utils import ( dequant_mxfp4, quant_dequant_mxfp4, ) from vllm.model_executor.layers.quantization.utils.mxfp6_utils import ( dequant_mxfp6, quant_dequant_mxfp6, ) from vllm.model_executor.layers.quantization.utils.ocp_mx_utils import ( OCP_MX_BLOCK_SIZE, OCP_MX_Scheme, ) from vllm.model_executor.parameter import GroupQuantScaleParameter, PackedvLLMParameter from vllm.platforms import current_platform from .quark_scheme import QuarkScheme logger = init_logger(__name__) # TODO: move registration of custom op to aiter_ops.py # `from vllm._aiter_ops import rocm_aiter_ops` # use `rocm_aiter_ops.is_asm_fp4_gemm_dynamic_quant_enabled()` # for envs checks which does not require @cache anymore. # triton kernel is torch compile compatible. # does not require direct registration. # use `rocm_aiter_ops.triton_fp4_gemm_dynamic_qaunt`. @cache def is_rocm_aiter_fp4_asm_gemm_enabled() -> bool: return ( current_platform.is_rocm() and envs.VLLM_ROCM_USE_AITER_FP4_ASM_GEMM and envs.VLLM_ROCM_USE_AITER ) try: from aiter.ops.shuffle import shuffle_weight from aiter.ops.triton.gemm_afp4wfp4 import ( gemm_afp4wfp4, gemm_afp4wfp4_preshuffled_weight_scales, ) from aiter.ops.triton.quant import dynamic_mxfp4_quant from vllm.utils.torch_utils import direct_register_custom_op if is_rocm_aiter_fp4_asm_gemm_enabled(): from aiter import gemm_a4w4, per_1x32_f4_quant_hip def gemm_with_dynamic_quant( x: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, rocm_use_aiter_fp4_asm_gemm: bool = False, out_dtype: torch.dtype | None = torch.bfloat16, x_scales: torch.Tensor | None = None, ) -> torch.Tensor: M = x.shape[0] N = weight.shape[0] K = weight.shape[1] if rocm_use_aiter_fp4_asm_gemm: if M <= 64 and rocm_aiter_ops.is_triton_gemm_afp4wfp4_presh_ws_tuned(N, K): if x_scales is None: # use hip quant kernel for performance if M >= 32: x_q, x_s = per_1x32_f4_quant_hip(x, shuffle=True) else: x_q, x_s = per_1x32_f4_quant_hip(x, shuffle=False) else: x_q = x x_s = x_scales if M >= 32: x_s = x_s.view(torch.uint8).view(x_s.shape[0] // 32, -1) else: x_s = x_s[:M, ...].view(torch.uint8) y = torch.empty(M, N, device=x_q.device, dtype=out_dtype) gemm_afp4wfp4_preshuffled_weight_scales( x_q.view(torch.uint8), weight.view(torch.uint8).view(weight.shape[0] // 16, -1), x_s, weight_scale.view(torch.uint8).view( weight_scale.shape[0] // 32, -1 ), out_dtype, y, ) else: if x_scales is None: # use hip quant kernel for performance x_q, x_s = per_1x32_f4_quant_hip(x, shuffle=True) else: x_q = x x_s = x_scales # 32 alignment is enough for dim0 padding of output for # gemm_a4w4 kernel y = torch.empty( (M + 31) // 32 * 32, weight.shape[0], device=x_q.device, dtype=out_dtype, ) gemm_a4w4( x_q, weight, x_s, weight_scale.view(x_s.dtype), y, bpreshuffle=True ) return y[:M] else: if x_scales is None: x_q, x_s = dynamic_mxfp4_quant(x) else: x_q = x x_s = x_scales y = torch.empty( x_q.shape[0], weight.shape[0], device=x_q.device, dtype=out_dtype ) gemm_afp4wfp4(x_q, weight, x_s, weight_scale.T, out_dtype, y) return y def gemm_with_dynamic_quant_fake( x: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, x_scales: torch.Tensor = None, rocm_use_aiter_fp4_asm_gemm: bool = False, out_dtype: torch.dtype | None = torch.bfloat16, ) -> torch.Tensor: return torch.empty( (*x.shape[:-1], weight.shape[0]), dtype=out_dtype, device=x.device ) direct_register_custom_op( op_name="gemm_with_dynamic_quant", op_func=gemm_with_dynamic_quant, mutates_args=[], fake_impl=gemm_with_dynamic_quant_fake, dispatch_key=current_platform.dispatch_key, ) except (ImportError, AttributeError): dynamic_mxfp4_quant = gemm_afp4wfp4 = None class QuarkOCP_MX(QuarkScheme): def __init__( self, weight_quant_spec: dict[str, Any], input_quant_spec: dict[str, Any] ): self.out_dtype = torch.get_default_dtype() self.qscheme = "per_group" self.weight_quant_spec = weight_quant_spec self.input_quant_spec = input_quant_spec self.weight_dtype = weight_quant_spec["dtype"].replace("fp", "mxfp") self.input_dtype = input_quant_spec["dtype"].replace("fp", "mxfp") self.ocp_mx_scheme = OCP_MX_Scheme.from_quant_dtype( self.input_dtype, self.weight_dtype ) if self.weight_dtype == "mxfp4": self.packed_factor: int | Fraction = 2 self.dequant_func = dequant_mxfp4 else: self.packed_factor = Fraction(numerator=8, denominator=6) self.dequant_func = partial( dequant_mxfp6, quant_dtype=self.weight_dtype.replace("mx", "") ) if self.input_dtype == "mxfp4": self.quant_dequant_func = quant_dequant_mxfp4 else: self.quant_dequant_func = partial( quant_dequant_mxfp6, quant_dtype=self.input_dtype.replace("mx", "") ) self.static_input_scales = not input_quant_spec.get("is_dynamic") if self.static_input_scales: raise NotImplementedError( "QuarkOCP_MX with static input scales is currently not " "implemented. Please open an issue." ) # TODO: integrate (or test) mixed-precision kernel. self.emulate = not current_platform.supports_mx() or ( self.input_dtype != "mxfp4" or self.weight_dtype != "mxfp4" ) self.rocm_use_aiter_fp4_asm_gemm = is_rocm_aiter_fp4_asm_gemm_enabled() if not self.emulate and (dynamic_mxfp4_quant is None or gemm_afp4wfp4 is None): # Currently need these kernels if not emulating raise NotImplementedError( f"{self.__class__.__name__} requires AITER to be installed " "for non-emulation mode! Please refer to " "https://github.com/ROCm/aiter for installation details." ) if not current_platform.supports_mx(): logger.warning_once( "The current platform does not support native MXFP4/MXFP6 " "computation. Simulated weight dequantization and activation " "QDQ (quantize and dequantize) will be used, with the linear " "layers computed in high precision." ) if current_platform.supports_mx() and ( self.input_dtype != "mxfp4" or self.weight_dtype != "mxfp4" ): logger.warning_once( "The current platform supports native MXFP4/MXFP6 " f"computation, but kernels for input_dtype={self.input_dtype} " f"and weight_dtype={self.weight_dtype} are not yet integrated " "in vLLM. Simulated weight dequantization and activation " "QDQ (quantize and dequantize) will be used, with the linear " "layers computed in high precision." ) def get_packed_dim(self, dim: int, quant_dtype: str): if quant_dtype == "mxfp4": assert dim % 2 == 0 return dim // 2 elif quant_dtype in {"mxfp6_e3m2", "mxfp6_e2m3"}: # FP6 packs 4 * 6 = 24 bits on 3 bytes. assert (dim * 3) % 4 == 0 return (dim * 3) // 4 else: raise NotImplementedError( "Unsupported quant_dtype in QuarkOCP_MX.get_packed_dim, " f"got quant_dtype={quant_dtype}. Something is wrong, please " "open an issue." ) @classmethod def get_min_capability(cls) -> int: return 70 def process_weights_after_loading(self, layer: torch.nn.Module) -> None: layer.weight = torch.nn.Parameter(layer.weight.data, requires_grad=False) if self.emulate: layer.weight_scale = torch.nn.Parameter( layer.weight_scale.data, requires_grad=False ) else: if self.rocm_use_aiter_fp4_asm_gemm: # shuffle weight scale weight_scale_shuffle = layer.weight_scale.data sm, sn = weight_scale_shuffle.shape weight_scale_shuffle = weight_scale_shuffle.view( sm // 32, 2, 16, sn // 8, 2, 4, 1 ) weight_scale_shuffle = weight_scale_shuffle.permute( 0, 3, 5, 2, 4, 1, 6 ).contiguous() weight_scale_shuffle = weight_scale_shuffle.view(sm, sn) layer.weight_scale = torch.nn.Parameter( weight_scale_shuffle, requires_grad=False ) # shuffle weight weight_shuffle = layer.weight.data weight_shuffle = shuffle_weight(weight_shuffle, layout=(16, 16)) layer.weight = torch.nn.Parameter(weight_shuffle, requires_grad=False) else: layer.weight_scale = torch.nn.Parameter( layer.weight_scale.data.T.contiguous(), requires_grad=False ) def create_weights( self, layer: torch.nn.Module, output_partition_sizes: list[int], input_size_per_partition: int, params_dtype: torch.dtype, weight_loader: Callable, **kwargs, ): output_size_per_partition = sum(output_partition_sizes) layer.logical_widths = output_partition_sizes # WEIGHT weight = PackedvLLMParameter( data=torch.empty( output_size_per_partition, self.get_packed_dim(input_size_per_partition, self.weight_dtype), dtype=torch.uint8, ), input_dim=1, output_dim=0, packed_dim=1, packed_factor=self.packed_factor, weight_loader=weight_loader, ) layer.register_parameter("weight", weight) # WEIGHT SCALE weight_scale = GroupQuantScaleParameter( data=torch.empty( output_size_per_partition, input_size_per_partition // OCP_MX_BLOCK_SIZE, dtype=torch.uint8, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight_scale", weight_scale) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: if self.emulate: dq_w = self.dequant_func(layer.weight, layer.weight_scale, x.dtype) qdq_x = self.quant_dequant_func(x) return F.linear(qdq_x, dq_w, bias) else: return torch.ops.vllm.gemm_with_dynamic_quant( x, layer.weight, layer.weight_scale, self.rocm_use_aiter_fp4_asm_gemm, self.out_dtype, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/quark/schemes/quark_w8a8_int8.py
vllm/model_executor/layers/quantization/quark/schemes/quark_w8a8_int8.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import torch from vllm.logger import init_logger from vllm.model_executor.layers.quantization.kernels.scaled_mm import ( ScaledMMLinearLayerConfig, choose_scaled_mm_linear_kernel, ) from vllm.model_executor.layers.quantization.quark.schemes import QuarkScheme from vllm.model_executor.parameter import ( BasevLLMParameter, ChannelQuantScaleParameter, ModelWeightParameter, PerTensorScaleParameter, ) logger = init_logger(__name__) class QuarkW8A8Int8(QuarkScheme): _kernel_backends_being_used: set[str] = set() def __init__( self, qscheme: str, is_static_input_scheme: bool | None, input_symmetric: bool | None, ): self.qscheme = qscheme self.is_static_input_scheme = is_static_input_scheme self.input_symmetric = input_symmetric @classmethod def get_min_capability(cls) -> int: # turing and up return 75 def create_weights( self, layer: torch.nn.Module, output_partition_sizes: list[int], input_size_per_partition: int, params_dtype: torch.dtype, weight_loader: Callable, **kwargs, ): layer.logical_widths = output_partition_sizes scaled_mm_linear_kernel_config = ScaledMMLinearLayerConfig( is_channelwise=(self.qscheme == "per_channel"), is_static_input_scheme=(self.is_static_input_scheme is True), input_symmetric=(self.input_symmetric is True), ) kernel_type = choose_scaled_mm_linear_kernel(scaled_mm_linear_kernel_config) if kernel_type.__name__ not in self._kernel_backends_being_used: logger.info("Using %s for QuarkW8A8Int8", kernel_type.__name__) self._kernel_backends_being_used.add(kernel_type.__name__) # WEIGHT weight = ModelWeightParameter( data=torch.empty( sum(output_partition_sizes), input_size_per_partition, dtype=torch.int8 ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight", weight) # WEIGHT SCALE if self.qscheme == "per_channel": weight_scale = ChannelQuantScaleParameter( data=torch.empty((sum(output_partition_sizes)), dtype=torch.float32), output_dim=0, weight_loader=weight_loader, ) ChannelQuantZPParameter = ChannelQuantScaleParameter weight_zero_point = ChannelQuantZPParameter( data=torch.empty((sum(output_partition_sizes)), dtype=torch.int8), output_dim=0, weight_loader=weight_loader, ) else: assert self.qscheme == "per_tensor" weight_scale = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) PerTensorZPParameter = PerTensorScaleParameter weight_zero_point = PerTensorZPParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.int8), weight_loader=weight_loader, ) layer.register_parameter("weight_scale", weight_scale) layer.register_parameter("weight_zero_point", weight_zero_point) # INPUT SCALE if self.is_static_input_scheme: input_scale = BasevLLMParameter( data=torch.empty(1, dtype=torch.float32), weight_loader=weight_loader ) layer.register_parameter("input_scale", input_scale) input_zero_point = BasevLLMParameter( data=torch.empty(1, dtype=torch.int8), weight_loader=weight_loader ) layer.register_parameter("input_zero_point", input_zero_point) self.kernel = kernel_type( c=scaled_mm_linear_kernel_config, w_q_param_name="weight", w_s_param_name="weight_scale", i_s_param_name="input_scale", i_zp_param_name="input_zero_point", azp_adj_param_name="azp_adj", ) # Checkpoints are serialized in quark format, which is # different from the format the kernel may want. Handle repacking here. def process_weights_after_loading(self, layer: torch.nn.Module) -> None: layer.register_parameter("weight_zero_point", None) delattr(layer, "weight_zero_point") if self.input_symmetric: layer.register_parameter("input_zero_point", None) delattr(layer, "input_zero_point") self.kernel.process_weights_after_loading(layer) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None ) -> torch.Tensor: return self.kernel.apply_weights(layer, x, bias)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/quark/schemes/__init__.py
vllm/model_executor/layers/quantization/quark/schemes/__init__.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from .quark_ocp_mx import QuarkOCP_MX from .quark_scheme import QuarkScheme from .quark_w8a8_fp8 import QuarkW8A8Fp8 from .quark_w8a8_int8 import QuarkW8A8Int8 __all__ = ["QuarkScheme", "QuarkW8A8Fp8", "QuarkW8A8Int8", "QuarkOCP_MX"]
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/__init__.py
vllm/model_executor/layers/quantization/kernels/__init__.py
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/scaled_mm/ScaledMMLinearKernel.py
vllm/model_executor/layers/quantization/kernels/scaled_mm/ScaledMMLinearKernel.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from abc import ABC, abstractmethod from dataclasses import dataclass import torch @dataclass class ScaledMMLinearLayerConfig: is_channelwise: bool is_static_input_scheme: bool input_symmetric: bool class ScaledMMLinearKernel(ABC): @classmethod @abstractmethod def is_supported( cls, compute_capability: int | None = None ) -> tuple[bool, str | None]: raise NotImplementedError @classmethod @abstractmethod def can_implement(cls, c: ScaledMMLinearLayerConfig) -> tuple[bool, str | None]: raise NotImplementedError def __init__( self, c: ScaledMMLinearLayerConfig, w_q_param_name: str, w_s_param_name: str, i_s_param_name: str, i_zp_param_name: str, azp_adj_param_name: str, ) -> None: assert self.can_implement(c) assert self.is_supported() self.config = c self.w_q_name = w_q_param_name self.w_s_name = w_s_param_name self.i_s_name = i_s_param_name self.i_zp_name = i_zp_param_name self.azp_adj_name = azp_adj_param_name @abstractmethod def process_weights_after_loading(self, layer: torch.nn.Module) -> None: raise NotImplementedError @abstractmethod def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: raise NotImplementedError def _get_weight_params( self, layer: torch.nn.Module ) -> tuple[ torch.Tensor, # weight torch.Tensor, # weight_scale torch.Tensor | None, # input_scale, torch.Tensor | None, # input_zp torch.Tensor | None, # azp_adj ]: return ( getattr(layer, self.w_q_name), getattr(layer, self.w_s_name), getattr(layer, self.i_s_name), getattr(layer, self.i_zp_name), getattr(layer, self.azp_adj_name), )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/scaled_mm/cpu.py
vllm/model_executor/layers/quantization/kernels/scaled_mm/cpu.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm import _custom_ops as ops from vllm import envs from vllm.model_executor.layers.quantization.utils import replace_parameter from vllm.model_executor.layers.quantization.utils.w8a8_utils import ( convert_to_channelwise, ) from vllm.model_executor.layers.utils import check_cpu_sgl_kernel from vllm.platforms import current_platform from vllm.platforms.interface import CpuArchEnum from .ScaledMMLinearKernel import ScaledMMLinearKernel, ScaledMMLinearLayerConfig class CPUScaledMMLinearKernel(ScaledMMLinearKernel): @classmethod def is_supported( cls, compute_capability: int | None = None ) -> tuple[bool, str | None]: if not current_platform.is_cpu(): return False, "Requires CPU." return True, None @classmethod def can_implement(cls, c: ScaledMMLinearLayerConfig) -> tuple[bool, str | None]: return True, None def process_weights_after_loading(self, layer: torch.nn.Module) -> None: weight = getattr(layer, self.w_q_name) dtype = weight.dtype N, K = weight.size() if ( current_platform.get_cpu_architecture() == CpuArchEnum.X86 and envs.VLLM_CPU_SGL_KERNEL and self.config.input_symmetric and check_cpu_sgl_kernel(N, K, dtype) ): self.linear_method = self._apply_weights_sgl self.process_weights_for_sgl(layer) else: self.linear_method = self._apply_weights_onednn self.process_weights_for_onednn(layer) def process_weights_for_onednn(self, layer: torch.nn.Module) -> None: # WEIGHT # Transpose to [K, N] for convenience weight = getattr(layer, self.w_q_name) replace_parameter( layer, self.w_q_name, torch.nn.Parameter(weight.t().data, requires_grad=False), ) # WEIGHT SCALE # oneDNN kernels support only per-tensor and per-channel. # If we have a fused module (QKV, MLP) with per tensor scales (thus N # scales being passed to the kernel), convert to the per-channel case. is_fused_module = len(layer.logical_widths) > 1 weight_scale = getattr(layer, self.w_s_name) if is_fused_module and not self.config.is_channelwise: weight_scale = convert_to_channelwise(weight_scale, layer.logical_widths) replace_parameter( layer, self.w_s_name, torch.nn.Parameter(weight_scale.data, requires_grad=False), ) # INPUT SCALE if self.config.is_static_input_scheme: input_scale = getattr(layer, self.i_s_name) if self.config.input_symmetric: replace_parameter( layer, self.i_s_name, torch.nn.Parameter(input_scale.max(), requires_grad=False), ) setattr(layer, self.i_zp_name, None) else: input_zero_point = getattr(layer, self.i_zp_name) # reconstruct the ranges int8_traits = torch.iinfo(torch.int8) azps = input_zero_point.to(dtype=torch.int32) range_max = (input_scale * (int8_traits.max - azps)).max() range_min = (input_scale * (int8_traits.min - azps)).min() scale = (range_max - range_min) / (int8_traits.max - int8_traits.min) replace_parameter( layer, self.i_s_name, torch.nn.Parameter(scale, requires_grad=False) ) azp = ( (int8_traits.min - range_min / scale).round().to(dtype=torch.int32) ) replace_parameter( layer, self.i_zp_name, torch.nn.Parameter(azp, requires_grad=False) ) else: setattr(layer, self.i_s_name, None) setattr(layer, self.i_zp_name, None) # Different from cutlass, oneDNN kernels only need the AZP adjustment # term for dynamic quantization. And s_b should be folded into the # term. Such as: # s_a * s_b * [(A - zp_a)B] + bias = # s_a * (s_b * AB) - s_a * s_b * zp_a * B + bias = # s_a * GEMM_output - s_a * zp_a * adj + bias if not (self.config.input_symmetric and self.config.is_static_input_scheme): weight = getattr(layer, self.w_q_name) weight_scale = getattr(layer, self.w_s_name) azp_adj = weight.sum(dim=0, keepdim=True, dtype=torch.float32) azp_adj = azp_adj * weight_scale.squeeze() setattr( layer, self.azp_adj_name, torch.nn.Parameter(azp_adj, requires_grad=False), ) else: setattr(layer, self.azp_adj_name, None) weight = getattr(layer, self.w_q_name) self.dnnl_handler = ops.create_onednn_scaled_mm( weight, getattr(layer, self.w_s_name), torch.get_default_dtype(), getattr(layer, self.i_s_name) is None, not self.config.input_symmetric, 32, ) # weight is prepacked and maintained by the dnnl_handler, # release the original weight setattr(layer, self.w_q_name, None) del weight def process_weights_for_sgl(self, layer: torch.nn.Module) -> None: # WEIGHT weight = getattr(layer, self.w_q_name) packed_weight = torch.ops._C.convert_weight_packed(weight) replace_parameter( layer, self.w_q_name, torch.nn.Parameter(packed_weight, requires_grad=False) ) if layer.bias is not None: bias = layer.bias layer.register_parameter( "bias_fp32", torch.nn.Parameter(bias.float().data, requires_grad=False) ) # WEIGHT SCALE # CPU SGL kernels only support per-channel. # For per-tensor quant, convert to the per-channel case. weight_scale = getattr(layer, self.w_s_name) if not self.config.is_channelwise: weight_scale = convert_to_channelwise(weight_scale, layer.logical_widths) replace_parameter( layer, self.w_s_name, torch.nn.Parameter(weight_scale.data, requires_grad=False), ) setattr(layer, self.i_s_name, None) setattr(layer, self.i_zp_name, None) setattr(layer, self.azp_adj_name, None) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: return self.linear_method( layer, x, bias, ) def _apply_weights_onednn( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: w_q, w_s, i_s, i_zp, azp_adj = self._get_weight_params(layer) # ops.scaled_int8_quant supports both dynamic and static quant: # * dynamic, i_s is None and x_s computed from x. # * static, i_s is scalar and x_s is i_s. x_q, x_s, x_zp = ops.onednn_scaled_int8_quant( x, i_s, i_zp, self.config.input_symmetric ) m = x.size(0) n = self.dnnl_handler.n out = torch.empty((m, n), dtype=x.dtype) ops.onednn_scaled_mm(self.dnnl_handler, x_q, out, x_s, x_zp, azp_adj, bias) return out def _apply_weights_sgl( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: w_q, w_s, _, _, _ = self._get_weight_params(layer) return torch.ops._C.int8_scaled_mm_with_quant( x, w_q, w_s, layer.bias_fp32 if bias is not None else None, x.dtype, True, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/scaled_mm/aiter.py
vllm/model_executor/layers/quantization/kernels/scaled_mm/aiter.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm import _custom_ops as ops from vllm._aiter_ops import rocm_aiter_ops from vllm.platforms import current_platform from .cutlass import CutlassScaledMMLinearKernel from .ScaledMMLinearKernel import ScaledMMLinearLayerConfig class AiterScaledMMLinearKernel(CutlassScaledMMLinearKernel): @classmethod def is_supported( cls, compute_capability: int | None = None ) -> tuple[bool, str | None]: if not current_platform.is_rocm(): return ( False, "AiterScaledMMLinearKernel requires `aiter` which is not " + "currently supported on non-ROCm platform.", ) if compute_capability is None: _cc = current_platform.get_device_capability() if _cc is not None: compute_capability = _cc.major * 10 + _cc.minor if compute_capability is not None and compute_capability < 90: return False, f"requires capability 90, got {compute_capability}" try: import aiter # noqa: F401 # deliberately attempt to import aiter except Exception: return ( False, "AiterScaledMMLinearKernel requires `aiter` which is not " + "installed on ROCm.", ) if not rocm_aiter_ops.is_linear_enabled(): return ( False, "AiterScaledMMLinearKernel is disabled. " + "Enable by setting `VLLM_ROCM_USE_AITER=1` " + "and `VLLM_ROCM_USE_AITER_LINEAR=1`. " + "`VLLM_ROCM_USE_AITER_LINEAR` default is True.", ) return True, None @classmethod def can_implement(cls, c: ScaledMMLinearLayerConfig) -> tuple[bool, str | None]: if not c.input_symmetric: return ( False, "AiterScaledMMLinearKernel only supports symmetric " + "quantization.", ) return True, None def process_weights_after_loading(self, layer: torch.nn.Module) -> None: super().process_weights_after_loading(layer) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: """ `AiterScaledMMLinearKernel` implements a fused version of `output = torch.mm((scale_a * a), (scale_b * b)).to(out_dtype)` where scale_a * a and scale_b * b are implemented using numpy-style broadcasting. Currently only support per-tensor-per-tensor GEMM and per-token-per-channel GEMM through AITER w8a8 scaled gemm. `AiterScaledMMLinearKernel` also does not support ATIER block scaled GEMM and mix-precision GEMM. """ w_q, w_s, i_s, i_zp, azp_adj = self._get_weight_params(layer) # ops.scaled_int8_quant supports both dynamic and static quant: # * dynamic, i_s is None and x_s computed from x. # * static, i_s is scalar and x_s is i_s. symmetric = azp_adj is None assert symmetric, ( "AiterScaledMMLinearKernel only supports symmetric quantization." ) x_q, x_s, x_zp = ops.scaled_int8_quant(x, i_s, i_zp, symmetric=symmetric) assert x_zp is None, ( "AiterScaledMMLinearKernel only supports symmetric quantization." ) out_dtype = x.dtype assert w_q.shape[0] % 16 == 0 and w_q.shape[1] % 16 == 0 assert out_dtype is torch.bfloat16 or out_dtype is torch.float16 assert bias is None or bias.shape[0] == w_q.shape[1] and bias.dtype == out_dtype m = x_q.shape[0] # a n = w_q.shape[1] # b per_tensor_scale_a = x_s.numel() == 1 per_tensor_scale_b = w_s.numel() == 1 per_token_scale_a = x_s.numel() == m per_channel_scale_b = w_s.numel() == n # @TODO: # Maybe broadcast the per-tensor-scale into per-channel-scale # if one of the scale is a per-channel-scale. # For now, it only supports: # - per-tensor-per-tensor a8w8 scaled GEMM, and # - per-token-per-channel a8w8 scaled GEMM assert (per_tensor_scale_a and per_tensor_scale_b) or ( per_token_scale_a and per_channel_scale_b ), ( "Currently only support per-tensor-per-tensor GEMM " + " and per-token-per-channel GEMM through AITER" " w8a8 scaled gemm. `AiterScaledMMLinearKernel` " + "does not support AITER block scaled GEMM." ) # gemm_a8w8_CK(a, b, scale_a, scale_b, bias) expects # a to be [M, K] # b to be [N, K] # CutlassScaledMMLinearKernel prepare weight `w_q` in [K, N] format return rocm_aiter_ops.gemm_a8w8(x_q, w_q.t(), x_s, w_s, bias, out_dtype)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/scaled_mm/__init__.py
vllm/model_executor/layers/quantization/kernels/scaled_mm/__init__.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import os from vllm.model_executor.layers.quantization.kernels.scaled_mm.aiter import ( AiterScaledMMLinearKernel, ) from vllm.model_executor.layers.quantization.kernels.scaled_mm.cpu import ( CPUScaledMMLinearKernel, ) from vllm.model_executor.layers.quantization.kernels.scaled_mm.cutlass import ( CutlassScaledMMLinearKernel, ) from vllm.model_executor.layers.quantization.kernels.scaled_mm.ScaledMMLinearKernel import ( # noqa: E501 ScaledMMLinearKernel, ScaledMMLinearLayerConfig, ) from vllm.model_executor.layers.quantization.kernels.scaled_mm.triton import ( TritonScaledMMLinearKernel, ) from vllm.model_executor.layers.quantization.kernels.scaled_mm.xla import ( XLAScaledMMLinearKernel, ) from vllm.platforms import PlatformEnum, current_platform # in priority/performance order (when available) _POSSIBLE_KERNELS: dict[PlatformEnum, list[type[ScaledMMLinearKernel]]] = { PlatformEnum.CPU: [CPUScaledMMLinearKernel], PlatformEnum.CUDA: [CutlassScaledMMLinearKernel, TritonScaledMMLinearKernel], PlatformEnum.ROCM: [AiterScaledMMLinearKernel, TritonScaledMMLinearKernel], PlatformEnum.TPU: [XLAScaledMMLinearKernel], } def choose_scaled_mm_linear_kernel( config: ScaledMMLinearLayerConfig, compute_capability: int | None = None ) -> type[ScaledMMLinearKernel]: """ Choose an ScaledMMLinearKernel that can implement the given config for the given compute capability. Attempts to choose the best kernel in terms of performance. Args: config (ScaledMMLinearLayerConfig): Description of the linear layer to be implemented. compute_capability (Optional[int], optional): The compute capability of the target device, if None uses `current_platform` to get the compute capability. Defaults to None. Raises: ValueError: If no kernel can implement the given config. Returns: type[ScaledMMLinearKernel]: Chosen kernel. """ failure_reasons = [] for kernel in _POSSIBLE_KERNELS[current_platform._enum]: if kernel.__name__ in os.environ.get("VLLM_DISABLED_KERNELS", "").split(","): failure_reasons.append(f"{kernel.__name__}: disabled by env var") continue # If the current platform uses compute_capability, # make sure the kernel supports the compute capability. is_supported, reason = kernel.is_supported(compute_capability) if not is_supported: failure_reasons.append(f"{kernel.__name__}: {reason}") continue can_implement, reason = kernel.can_implement(config) if not can_implement: failure_reasons.append(f"{kernel.__name__}: {reason}") continue return kernel raise ValueError( "Failed to find a kernel that can implement the " "ScaledMM linear layer. Reasons: \n" + "\n".join(failure_reasons) )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/scaled_mm/cutlass.py
vllm/model_executor/layers/quantization/kernels/scaled_mm/cutlass.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm import _custom_ops as ops from vllm.model_executor.layers.quantization.utils import replace_parameter from vllm.model_executor.layers.quantization.utils.w8a8_utils import ( convert_to_channelwise, ) from vllm.platforms import current_platform from .ScaledMMLinearKernel import ScaledMMLinearKernel, ScaledMMLinearLayerConfig class CutlassScaledMMLinearKernel(ScaledMMLinearKernel): @classmethod def is_supported( cls, compute_capability: int | None = None ) -> tuple[bool, str | None]: if not current_platform.is_cuda(): return False, "Requires CUDA." if compute_capability is None: _cc = current_platform.get_device_capability() if _cc is not None: compute_capability = _cc.major * 10 + _cc.minor if compute_capability is not None and compute_capability < 75: return False, f"requires capability 75, got {compute_capability}" return True, None @classmethod def can_implement(cls, c: ScaledMMLinearLayerConfig) -> tuple[bool, str | None]: return True, None def process_weights_after_loading(self, layer: torch.nn.Module) -> None: # WEIGHT # Cutlass kernels need transposed weight. weight = getattr(layer, self.w_q_name) replace_parameter( layer, self.w_q_name, torch.nn.Parameter(weight.t().data, requires_grad=False), ) # WEIGHT SCALE # Cutlass kernels support only per-tensor and per-channel. # If we have a fused module (QKV, MLP) with per tensor scales (thus N # scales being passed to the kernel), convert to the per-channel case. is_fused_module = len(layer.logical_widths) > 1 weight_scale = getattr(layer, self.w_s_name) if is_fused_module and not self.config.is_channelwise: weight_scale = convert_to_channelwise(weight_scale, layer.logical_widths) replace_parameter( layer, self.w_s_name, torch.nn.Parameter(weight_scale.data, requires_grad=False), ) # INPUT SCALE if self.config.is_static_input_scheme: input_scale = getattr(layer, self.i_s_name) if self.config.input_symmetric: replace_parameter( layer, self.i_s_name, torch.nn.Parameter(input_scale.max(), requires_grad=False), ) setattr(layer, self.i_zp_name, None) else: input_zero_point = getattr(layer, self.i_zp_name) # reconstruct the ranges int8_traits = torch.iinfo(torch.int8) azps = input_zero_point.to(dtype=torch.int32) range_max = (input_scale * (int8_traits.max - azps)).max() range_min = (input_scale * (int8_traits.min - azps)).min() scale = (range_max - range_min) / (int8_traits.max - int8_traits.min) replace_parameter( layer, self.i_s_name, torch.nn.Parameter(scale, requires_grad=False) ) # AZP loaded as int8 but used as int32 azp = (int8_traits.min - range_min / scale).to(dtype=torch.int32) replace_parameter( layer, self.i_zp_name, torch.nn.Parameter(azp, requires_grad=False) ) else: setattr(layer, self.i_s_name, None) setattr(layer, self.i_zp_name, None) # azp_adj is the AZP adjustment term, used to account for weights. # It does not depend on scales or azp, so it is the same for # static and dynamic quantization. # For more details, see csrc/quantization/w8a8/cutlass/Epilogues.md # https://github.com/vllm-project/vllm/blob/main/csrc/quantization/w8a8/cutlass/Epilogues.md if not self.config.input_symmetric: weight = getattr(layer, self.w_q_name) azp_adj = weight.sum(dim=0, keepdim=True, dtype=torch.int32) if self.config.is_static_input_scheme: # cutlass_w8a8 requires azp to be folded into azp_adj # in the per-tensor case azp_adj = getattr(layer, self.i_zp_name) * azp_adj setattr( layer, self.azp_adj_name, torch.nn.Parameter(azp_adj, requires_grad=False), ) else: setattr(layer, self.azp_adj_name, None) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: w_q, w_s, i_s, i_zp, azp_adj = self._get_weight_params(layer) # ops.scaled_int8_quant supports both dynamic and static quant: # * dynamic, i_s is None and x_s computed from x. # * static, i_s is scalar and x_s is i_s. symmetric = azp_adj is None x_q, x_s, x_zp = ops.scaled_int8_quant( x.contiguous(), i_s, i_zp, symmetric=symmetric ) if x_zp is not None: # Currently, static is always per-tensor and dynamic is per-token static = i_zp is not None azp = None if static else x_zp return ops.cutlass_scaled_mm_azp( x_q, w_q, scale_a=x_s, scale_b=w_s, out_dtype=x.dtype, azp_adj=azp_adj, azp=azp, bias=bias, ) return ops.cutlass_scaled_mm( x_q, w_q, scale_a=x_s, scale_b=w_s, out_dtype=x.dtype, bias=bias )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/scaled_mm/triton.py
vllm/model_executor/layers/quantization/kernels/scaled_mm/triton.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm import _custom_ops as ops from vllm.model_executor.layers.quantization.compressed_tensors.triton_scaled_mm import ( # noqa: E501 triton_scaled_mm, ) from vllm.model_executor.layers.quantization.utils import replace_parameter from vllm.platforms import current_platform from .ScaledMMLinearKernel import ScaledMMLinearKernel, ScaledMMLinearLayerConfig class TritonScaledMMLinearKernel(ScaledMMLinearKernel): @classmethod def is_supported( cls, compute_capability: int | None = None ) -> tuple[bool, str | None]: if current_platform.is_cuda_alike(): return True, None return False, "Requires ROCm or CUDA." @classmethod def can_implement(cls, c: ScaledMMLinearLayerConfig) -> tuple[bool, str | None]: if not c.input_symmetric: return False, "Only symmetric input is supported." return True, None def process_weights_after_loading(self, layer: torch.nn.Module) -> None: weight = getattr(layer, self.w_q_name) replace_parameter( layer, self.w_q_name, torch.nn.Parameter(weight.t().data, requires_grad=False), ) # INPUT SCALE if self.config.is_static_input_scheme: input_scale = getattr(layer, self.i_s_name) replace_parameter( layer, self.i_s_name, torch.nn.Parameter(input_scale.max(), requires_grad=False), ) setattr(layer, self.i_zp_name, None) else: setattr(layer, self.i_s_name, None) setattr(layer, self.i_zp_name, None) setattr(layer, self.azp_adj_name, None) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: w_q, w_s, i_s, i_zp, azp_adj = self._get_weight_params(layer) x_q, x_s, x_zp = ops.scaled_int8_quant( x.contiguous(), i_s, i_zp, symmetric=True ) assert x_zp is None, "Triton kernel only supports symmetric quantization" return triton_scaled_mm( x_q, w_q, scale_a=x_s, scale_b=w_s, out_dtype=x.dtype, bias=bias )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/scaled_mm/xla.py
vllm/model_executor/layers/quantization/kernels/scaled_mm/xla.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import warnings import torch from functorch.experimental.control_flow import cond # noqa: F401 from vllm.model_executor.layers.quantization.utils import replace_parameter from vllm.model_executor.layers.quantization.utils.w8a8_utils import ( convert_to_channelwise, ) from vllm.platforms import current_platform from .ScaledMMLinearKernel import ScaledMMLinearKernel, ScaledMMLinearLayerConfig class XLAScaledMMLinearKernel(ScaledMMLinearKernel): @classmethod def is_supported( cls, compute_capability: int | None = None ) -> tuple[bool, str | None]: if not current_platform.is_tpu(): return False, "Requires TPU." return True, None @classmethod def can_implement(cls, c: ScaledMMLinearLayerConfig) -> tuple[bool, str | None]: if not current_platform.is_tpu(): return False, "ScaledMMXLA requires running on TPU." if c.is_static_input_scheme: return False, "ScaledMMXLA requires dynamic activation scales." if not c.input_symmetric: return False, "ScaledMMXLA requires symmetric activation scales." if not c.is_channelwise: return False, "ScaledMMXLA requires channelwise weight scales" return True, None def process_weights_after_loading(self, layer: torch.nn.Module) -> None: # WEIGHT # [out, in] (different than cutlass_scaled_mm) weight = getattr(layer, self.w_q_name) replace_parameter( layer, self.w_q_name, torch.nn.Parameter(weight.data, requires_grad=False) ) # WEIGHT SCALE # XLA kernels support only per-tensor and per-channel. # If we have a fused module (QKV, MLP) with per tensor scales (thus N # scales being passed to the kernel), convert to the per-channel case. is_fused_module = len(layer.logical_widths) > 1 weight_scale = getattr(layer, self.w_s_name) if is_fused_module and not self.config.is_channelwise: weight_scale = convert_to_channelwise(weight_scale, layer.logical_widths) # [out_channel,] (different than cutlass_scaled_mm) weight_scale = weight_scale.squeeze(-1) replace_parameter( layer, self.w_s_name, torch.nn.Parameter(weight_scale.data, requires_grad=False), ) # Only support symmetric dynamic activation quantization. setattr(layer, self.i_s_name, None) setattr(layer, self.i_zp_name, None) setattr(layer, self.azp_adj_name, None) # Filter warning for cond usage in apply_weights. It is okay # to specialize the graph since bias is not dynamic. warnings.filterwarnings( "ignore", message="Pred is a Python constant. When used with torch.cond, it specializes on one of the branches.", # noqa: E501 ) def no_add_bias(self, x: torch.Tensor, bias: torch.Tensor | None): return x def add_bias(self, x: torch.Tensor, bias: torch.Tensor | None): return x + bias def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: w_q, w_s, _, _, _ = self._get_weight_params(layer) # Required to register custom ops. import torch_xla.experimental.custom_kernel # noqa: F401 out = torch.ops.xla.quantized_matmul_int8( x, w_q, w_s, quantize_activation=True, ) # Explicitly capture control flow to make dynamo happy. # https://pytorch.org/docs/main/generated/exportdb/index.html#cond-branch-class-method # noqa: E501 return cond(bias is None, self.no_add_bias, self.add_bias, [out, bias])
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/mixed_precision/xpu.py
vllm/model_executor/layers/quantization/kernels/mixed_precision/xpu.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm.platforms import current_platform from .MPLinearKernel import MPLinearKernel, MPLinearLayerConfig class XPUwNa16LinearKernel(MPLinearKernel): @classmethod def get_min_capability(cls) -> int: return 0 @classmethod def can_implement(cls, c: MPLinearLayerConfig) -> tuple[bool, str | None]: if not current_platform.is_xpu(): return False, "IPEX wNa16 only supported on XPU/CPU devices" # TODO: (yiliu30) relax these restrictions in later PRs if c.zero_points: return False, "Zero points not supported for Now" return True, None def process_weights_after_loading(self, layer: torch.nn.Module) -> None: from packaging import version MIN_IPEX_VERSION = "2.6.0" bias = layer.bias if not layer.skip_bias_add else None try: import intel_extension_for_pytorch as ipex if version.parse(ipex.__version__) < version.parse(MIN_IPEX_VERSION): raise ImportError( "intel_extension_for_pytorch version is " "wrong. Please install " f"intel_extension_for_pytorch>={MIN_IPEX_VERSION}." ) except ImportError as err: raise ImportError( "Please install " f"intel_extension_for_pytorch>={MIN_IPEX_VERSION} via " f"`pip install intel_extension_for_pytorch>={MIN_IPEX_VERSION}`" " to use IPEX-AWQ linear method." ) from err # Using the compute dtype (lowp_mode) as INT8 to leverage instructions # with better performance. lowp_mode = ipex.quantization.WoqLowpMode.INT8 # The weight will be de-packed from INT4 to INT8. weight_dtype = ipex.quantization.WoqWeightDtype.INT4 # The float activation will be quantized (dynamic, per-token) to INT8. act_quant_mode = ipex.quantization.WoqActQuantMode.PER_BATCH qconfig = ipex.quantization.get_weight_only_quant_qconfig_mapping( weight_dtype=weight_dtype, lowp_mode=lowp_mode, act_quant_mode=act_quant_mode, group_size=self.config.group_size, weight_qscheme=ipex.quantization.WoqWeightQScheme.SYMMETRIC, ) qweight = layer.weight_packed g_idx = layer.weight_g_idx if self.config.has_g_idx else None scales = layer.weight_scale qzeros = None if self.config.zero_points: qzeros = layer.weight_zero_point.contiguous() qweight = qweight.t().contiguous() scales = scales.t().contiguous() layer.ipex_output_size = self.config.partition_weight_shape[1] layer.ipex_qlinear = ( ipex.llm.quantization.woq_linear.IPEXWeightOnlyQuantizedLinear.from_weight( qweight, scales, qzeros, in_features=self.config.partition_weight_shape[0], out_features=self.config.partition_weight_shape[1], qconfig=qconfig, g_idx=g_idx, bias=bias, group_size=self.config.group_size, quant_method=0, # `0` stands for the IPEX GPTQ ) ) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: reshaped_x = x.reshape(-1, x.shape[-1]) out = layer.ipex_qlinear(reshaped_x) return out.reshape(x.shape[:-1] + (layer.ipex_output_size,))
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/mixed_precision/allspark.py
vllm/model_executor/layers/quantization/kernels/mixed_precision/allspark.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm import _custom_ops as ops from vllm.model_executor.layers.quantization.utils import replace_parameter from vllm.model_executor.layers.quantization.utils.allspark_utils import ( ALLSPARK_AMPERE_M_CUBLAS_THRESHOLD, check_allspark_supported_dtype_shape, ) from vllm.model_executor.parameter import BasevLLMParameter, permute_param_layout_ from .MPLinearKernel import MPLinearKernel, MPLinearLayerConfig class AllSparkLinearKernel(MPLinearKernel): @classmethod def get_min_capability(cls) -> int: return 80 @classmethod def can_implement(cls, c: MPLinearLayerConfig) -> tuple[bool, str | None]: if c.has_g_idx: return False, "Act reordering currently not supported by AllSpark" if c.zero_points: return False, "Zero points currently not supported by AllSpark" return check_allspark_supported_dtype_shape( c.partition_weight_shape[0], # in_features c.partition_weight_shape[1], # out_features c.group_size, c.weight_type, c.act_type, ) # note assumes that # `weight_packed` is: {input_dim = 0, output_dim = 1, packed_dim = 0} # `weight_scale` is: {input_dim = 0, output_dim = 1} def process_weights_after_loading(self, layer: torch.nn.Module) -> None: device = getattr(layer, self.w_q_name).device c = self.config # prepare the parameters required for the kernel properties = torch.cuda.get_device_properties(device.index) sm_count = properties.multi_processor_count sm_version = properties.major * 10 + properties.minor gemm_args = {} gemm_args["sm_count"] = sm_count gemm_args["sm_version"] = sm_version self.gemm_args = gemm_args # transform param weight, scale old_weight_param = getattr(layer, self.w_q_name) old_scale_param = getattr(layer, self.w_s_name) assert isinstance(old_weight_param, BasevLLMParameter) permute_param_layout_(old_weight_param, input_dim=0, output_dim=1, packed_dim=0) assert isinstance(old_scale_param, BasevLLMParameter) permute_param_layout_(old_scale_param, input_dim=0, output_dim=1) # unpack weight from K / 4 x N int32 to K x N uint8 new_weight_param = torch.nn.Parameter( old_weight_param.data, requires_grad=False ) new_weight_param.data = ( new_weight_param.data.t().contiguous().view(dtype=torch.uint8) ) new_weight_param.data = new_weight_param.data.t().contiguous() new_scale_param = torch.nn.Parameter(old_scale_param.data, requires_grad=False) # reorder K x N weight as N32K16 format for Ampere W8A16 new_weight_param.data, new_scale_param.data, _ = ops.allspark_repack_weight( new_weight_param.data, new_scale_param.data, None, c.zero_points ) replace_parameter(layer, self.w_q_name, new_weight_param.data) replace_parameter(layer, self.w_s_name, new_scale_param.data) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: c = self.config gemm_args = self.gemm_args w_q, w_s, _, _ = self._get_weight_params(layer) reshaped_x = x.reshape(-1, x.shape[-1]) out_shape = x.shape[:-1] + (c.partition_weight_shape[1],) output = ops.allspark_w8a16_gemm( a=reshaped_x, b_qweight=w_q, b_scales=w_s, b_qzeros=None, n=c.partition_weight_shape[1], group_size=c.group_size, sm_count=gemm_args["sm_count"], sm_version=gemm_args["sm_version"], CUBLAS_M_THRESHOLD=ALLSPARK_AMPERE_M_CUBLAS_THRESHOLD, has_zp=c.zero_points, n32k16_reorder=True, ) if bias is not None: output.add_(bias) # In-place add return output.reshape(out_shape)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/mixed_precision/conch.py
vllm/model_executor/layers/quantization/kernels/mixed_precision/conch.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from importlib.util import find_spec from typing import Final import torch from vllm.model_executor.parameter import BasevLLMParameter, permute_param_layout_ from vllm.scalar_type import scalar_types from .MPLinearKernel import MPLinearKernel, MPLinearLayerConfig _CONCH_SUPPORTED_WEIGHT_TYPES: Final = [ scalar_types.uint4, scalar_types.uint8, scalar_types.uint4b8, scalar_types.uint8b128, ] _CONCH_SUPPORTED_GROUP_SIZES: Final = [-1, 128] class ConchLinearKernel(MPLinearKernel): @classmethod def get_min_capability(cls) -> int: return 80 @classmethod def can_implement(cls, c: MPLinearLayerConfig) -> tuple[bool, str | None]: if c.weight_type not in _CONCH_SUPPORTED_WEIGHT_TYPES: error_msg = ( f"Weight type ({c.weight_type}) not supported by " "ConchLinearKernel, supported types are: " f"{_CONCH_SUPPORTED_WEIGHT_TYPES}" ) return False, error_msg if c.group_size not in _CONCH_SUPPORTED_GROUP_SIZES: error_msg = ( f"Group size ({c.group_size}) not supported by " "ConchLinearKernel, supported group sizes are: " f"{_CONCH_SUPPORTED_GROUP_SIZES}" ) return False, error_msg if find_spec("conch") is None: error_msg = ( "conch-triton-kernels is not installed, please " "install it via `pip install conch-triton-kernels` " "and try again!" ) return False, error_msg return True, None # note assumes that # `weight_packed` is: {input_dim = 0, output_dim = 1, packed_dim = 0} # `weight_scale` is: {input_dim = 0, output_dim = 1} def process_weights_after_loading(self, layer: torch.nn.Module) -> None: def transform_w_q(x): assert isinstance(x, BasevLLMParameter) permute_param_layout_(x, input_dim=0, output_dim=1, packed_dim=0) x.data = x.data.contiguous() return x def transform_w_s(x): assert isinstance(x, BasevLLMParameter) permute_param_layout_(x, input_dim=0, output_dim=1) x.data = x.data.contiguous() return x self._transform_param(layer, self.w_q_name, transform_w_q) self._transform_param(layer, self.w_s_name, transform_w_s) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: from conch.ops.quantization.gemm import mixed_precision_gemm w_q, w_s, w_zp, _ = self._get_weight_params(layer) output = mixed_precision_gemm( x=x, w_q_packed=w_q.data, w_s=w_s.data, w_zp=w_zp.data if w_zp is not None else None, weight_size_bits=self.config.weight_type.size_bits, weight_bias=self.config.weight_type.bias, group_size=self.config.group_size, ) if bias is not None: output.add_(bias) # In-place add return output
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/mixed_precision/marlin.py
vllm/model_executor/layers/quantization/kernels/mixed_precision/marlin.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm import _custom_ops as ops from vllm.model_executor.layers.quantization.utils.marlin_utils import ( MARLIN_SUPPORTED_GROUP_SIZES, apply_gptq_marlin_linear, check_marlin_supports_shape, marlin_act_int8_process_scales, marlin_is_k_full, marlin_make_empty_g_idx, marlin_make_workspace_new, marlin_permute_bias, marlin_permute_scales, marlin_sort_g_idx, marlin_zero_points, query_marlin_supported_quant_types, unpack_cols, ) from vllm.model_executor.parameter import BasevLLMParameter, permute_param_layout_ from vllm.platforms import current_platform from vllm.scalar_type import scalar_types from .MPLinearKernel import MPLinearKernel, MPLinearLayerConfig class MarlinLinearKernel(MPLinearKernel): @classmethod def get_min_capability(cls) -> int: return 75 @classmethod def can_implement(cls, c: MPLinearLayerConfig) -> tuple[bool, str | None]: # Marlin uses inline PTX, so it can only be compatible with Nvidia if not current_platform.is_cuda(): return False, "Marlin only supported on CUDA" quant_types = query_marlin_supported_quant_types(c.zero_points) if c.weight_type not in quant_types: return ( False, f"Quant type ({c.weight_type}) not supported by" f" Marlin, supported types are: {quant_types}", ) if c.group_size not in MARLIN_SUPPORTED_GROUP_SIZES: return ( False, f"Group size ({c.group_size}) not supported by " "Marlin, supported group sizes are: " f"{MARLIN_SUPPORTED_GROUP_SIZES}", ) return check_marlin_supports_shape( c.partition_weight_shape[1], # out_features c.partition_weight_shape[0], # in_features c.full_weight_shape[0], # in_features c.group_size, ) # note assumes that # `weight_packed` is: {input_dim = 0, output_dim = 1, packed_dim = 0} # `weight_scale` is: {input_dim = 0, output_dim = 1} def process_weights_after_loading(self, layer: torch.nn.Module) -> None: device = getattr(layer, self.w_q_name).device c = self.config is_a_8bit = c.act_type is not None and c.act_type.itemsize == 1 if is_a_8bit: assert c.weight_type == scalar_types.uint4b8, ( "W8A8 is not supported by marlin kernel." ) if c.act_type == torch.float8_e4m3fn: ops.marlin_int4_fp8_preprocess(getattr(layer, self.w_q_name), inplace=True) getattr(layer, self.w_s_name).data = ( getattr(layer, self.w_s_name).data * 512 ) row_parallel = c.partition_weight_shape[0] != c.full_weight_shape[0] self.is_k_full = marlin_is_k_full(c.has_g_idx, row_parallel) # Allocate marlin workspace. self.workspace = marlin_make_workspace_new(device) # Default names since marlin requires empty parameters for these, # TODO: remove this requirement from marlin (allow optional tensors) if self.w_gidx_name is None: self.w_gidx_name = "g_idx" if self.w_zp_name is None: self.w_zp_name = "w_zp" def transform_w_q(x): assert isinstance(x, BasevLLMParameter) permute_param_layout_(x, input_dim=0, output_dim=1, packed_dim=0) x.data = ops.gptq_marlin_repack( x.data.contiguous(), perm=layer.g_idx_sort_indices, size_k=c.partition_weight_shape[0], size_n=c.partition_weight_shape[1], num_bits=c.weight_type.size_bits, is_a_8bit=is_a_8bit, ) return x def transform_w_s(x): assert isinstance(x, BasevLLMParameter) permute_param_layout_(x, input_dim=0, output_dim=1) x.data = marlin_permute_scales( x.data.contiguous(), size_k=c.partition_weight_shape[0], size_n=c.partition_weight_shape[1], group_size=c.group_size, is_a_8bit=is_a_8bit, ) if c.group_size == -1: num_groups = 1 else: num_groups = c.partition_weight_shape[0] // c.group_size if c.act_type == torch.int8 and num_groups > 1: x.data, input_global_scale = marlin_act_int8_process_scales(x.data) layer.register_parameter( "input_global_scale", torch.nn.Parameter(input_global_scale, requires_grad=False), ) else: layer.input_global_scale = None return x if c.has_g_idx: g_idx, g_idx_sort_indices = marlin_sort_g_idx( getattr(layer, self.w_gidx_name) ) self._transform_param(layer, self.w_gidx_name, lambda _: g_idx) layer.g_idx_sort_indices = g_idx_sort_indices else: setattr(layer, self.w_gidx_name, marlin_make_empty_g_idx(device)) layer.g_idx_sort_indices = marlin_make_empty_g_idx(device) if c.zero_points: grouped_k = ( c.partition_weight_shape[0] // c.group_size if c.group_size != -1 else 1 ) self._transform_param( layer, self.w_zp_name, lambda x: marlin_zero_points( unpack_cols( x.t(), c.weight_type.size_bits, grouped_k, c.partition_weight_shape[1], ), size_k=grouped_k, size_n=c.partition_weight_shape[1], num_bits=c.weight_type.size_bits, is_a_8bit=is_a_8bit, ), ) else: setattr(layer, self.w_zp_name, marlin_make_empty_g_idx(device)) self._transform_param(layer, self.w_q_name, transform_w_q) self._transform_param(layer, self.w_s_name, transform_w_s) if hasattr(layer, "bias") and layer.bias is not None: layer.bias.data = marlin_permute_bias(layer.bias) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: c = self.config w_q, w_s, w_zp, w_gidx = self._get_weight_params(layer) # `process_weights_after_loading` will ensure w_zp and w_gidx are not # None for marlin return apply_gptq_marlin_linear( input=x, weight=w_q, weight_scale=w_s, weight_zp=w_zp, # type: ignore g_idx=w_gidx, # type: ignore g_idx_sort_indices=layer.g_idx_sort_indices, workspace=self.workspace, wtype=c.weight_type, input_size_per_partition=c.partition_weight_shape[0], output_size_per_partition=c.partition_weight_shape[1], is_k_full=self.is_k_full, input_global_scale=getattr(layer, "input_global_scale", None), bias=bias, input_dtype=c.act_type, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/mixed_precision/__init__.py
vllm/model_executor/layers/quantization/kernels/mixed_precision/__init__.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import vllm.envs as envs from vllm.model_executor.layers.quantization.kernels.mixed_precision.allspark import ( # noqa: E501 AllSparkLinearKernel, ) from vllm.model_executor.layers.quantization.kernels.mixed_precision.bitblas import ( # noqa: E501 BitBLASLinearKernel, ) from vllm.model_executor.layers.quantization.kernels.mixed_precision.conch import ( # noqa: E501 ConchLinearKernel, ) from vllm.model_executor.layers.quantization.kernels.mixed_precision.cutlass import ( # noqa: E501 CutlassW4A8LinearKernel, ) from vllm.model_executor.layers.quantization.kernels.mixed_precision.dynamic_4bit import ( # noqa: E501 Dynamic4bitLinearKernel, ) from vllm.model_executor.layers.quantization.kernels.mixed_precision.exllama import ( # noqa: E501 ExllamaLinearKernel, ) from vllm.model_executor.layers.quantization.kernels.mixed_precision.machete import ( # noqa: E501 MacheteLinearKernel, ) from vllm.model_executor.layers.quantization.kernels.mixed_precision.marlin import ( # noqa: E501 MarlinLinearKernel, ) from vllm.model_executor.layers.quantization.kernels.mixed_precision.MPLinearKernel import ( # noqa: E501 MPLinearKernel, MPLinearLayerConfig, ) from vllm.model_executor.layers.quantization.kernels.mixed_precision.xpu import ( # noqa: E501 XPUwNa16LinearKernel, ) from vllm.platforms import current_platform # in priority/performance order (when available) _POSSIBLE_KERNELS: list[type[MPLinearKernel]] = [ CutlassW4A8LinearKernel, MacheteLinearKernel, AllSparkLinearKernel, MarlinLinearKernel, Dynamic4bitLinearKernel, BitBLASLinearKernel, ConchLinearKernel, ExllamaLinearKernel, XPUwNa16LinearKernel, ] def choose_mp_linear_kernel( config: MPLinearLayerConfig, compute_capability: int | None = None ) -> type[MPLinearKernel]: """ Choose an MPLinearKernel that can implement the given config for the given compute capability. Attempts to choose the best kernel in terms of performance. Args: config (MPLinearLayerConfig): Description of the linear layer to be implemented. compute_capability (Optional[int], optional): The compute capability of the target device, if None uses `current_platform` to get the compute capability. Defaults to None. Raises: ValueError: If no kernel can implement the given config. Returns: type[MPLinearKernel]: Chosen kernel. """ if compute_capability is None: if current_platform is None: raise ValueError("Cannot determine compute capability") _cc = current_platform.get_device_capability() if _cc is not None: compute_capability = _cc[0] * 10 + _cc[1] failure_reasons = [] for kernel in _POSSIBLE_KERNELS: if kernel.__name__ in envs.VLLM_DISABLED_KERNELS: failure_reasons.append( f" {kernel.__name__} disabled by environment variable" ) continue if ( compute_capability is not None and kernel.get_min_capability() > compute_capability ): failure_reasons.append( f"{kernel.__name__} requires capability " f"{kernel.get_min_capability()}, current compute " f" capability is {compute_capability}" ) continue can_implement, failure_reason = kernel.can_implement(config) if can_implement: return kernel else: failure_reasons.append( f" {kernel.__name__} cannot implement due to: {failure_reason}" ) raise ValueError( "Failed to find a kernel that can implement the " "WNA16 linear layer. Reasons: \n" + "\n".join(failure_reasons) )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/mixed_precision/cutlass.py
vllm/model_executor/layers/quantization/kernels/mixed_precision/cutlass.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm import _custom_ops as ops from vllm.model_executor.layers.quantization.input_quant_fp8 import QuantFP8 from vllm.model_executor.layers.quantization.utils.quant_utils import ( GroupShape, convert_bf16_scales_to_fp8, convert_packed_uint4b8_to_signed_int4_inplace, ) from vllm.model_executor.parameter import BasevLLMParameter, permute_param_layout_ from vllm.platforms import current_platform from vllm.scalar_type import scalar_types from .MPLinearKernel import MPLinearKernel, MPLinearLayerConfig class CutlassW4A8LinearKernel(MPLinearKernel): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # dynamic per-tok fp8 activation quantization self.quant_fp8 = QuantFP8(static=False, group_shape=GroupShape.PER_TOKEN) @classmethod def get_min_capability(cls) -> int: return 90 @classmethod def can_implement(cls, c: MPLinearLayerConfig) -> tuple[bool, str | None]: if not current_platform.is_cuda(): return False, "CUTLASS only supported on CUDA" if not current_platform.is_device_capability(90): return False, "CUTLASS W4A8 requires compute capability of 90 (Hopper)" if c.act_type != torch.float8_e4m3fn: return False, "CUTLASS W4A8 only supports FP8 (e4m3) activations" if c.has_g_idx: return False, "Act reordering not supported by CUTLASS W4A8" if c.zero_points: return False, "Zero points not supported by CUTLASS W4A8" if c.weight_type != scalar_types.int4: return ( False, f"Quant type ({c.weight_type}) not supported by " "CUTLASS W4A8, only supported int4", ) if c.group_size != 128: return False, "Only group_size 128 is supported" in_features, out_features = c.partition_weight_shape if in_features % 128 or out_features % 128: return ( False, f"K and N must be divisible by 128, got {c.partition_weight_shape}", ) if c.out_type != torch.bfloat16: return ( False, f"Only bfloat16 output type currently supportedgot {c.out_type=}", ) return True, None # note assumes that # `weight_packed` is: {input_dim = 0, output_dim = 1, packed_dim = 0} # `weight_scale` is: {input_dim = 0, output_dim = 1} def process_weights_after_loading(self, layer: torch.nn.Module): def transform_w_q(x): assert isinstance(x, BasevLLMParameter) convert_packed_uint4b8_to_signed_int4_inplace(x.data) permute_param_layout_(x, input_dim=0, output_dim=1, packed_dim=0) x.data = ops.cutlass_encode_and_reorder_int4b(x.data.t().contiguous().t()) return x def transform_w_s(x): assert isinstance(x, BasevLLMParameter) permute_param_layout_(x, input_dim=0, output_dim=1) x.data = x.data.contiguous().to(torch.float8_e4m3fn) x.data = ops.cutlass_pack_scale_fp8(x.data) return x w_s = getattr(layer, self.w_s_name) fp8_scales, chan_scales = convert_bf16_scales_to_fp8(self.quant_fp8, w_s.data) w_s.data = fp8_scales # register per-channel scales layer.register_parameter( "weight_chan_scale", torch.nn.Parameter(chan_scales, requires_grad=False) ) # Encode/reorder weights and pack scales self._transform_param(layer, self.w_q_name, transform_w_q) self._transform_param(layer, self.w_s_name, transform_w_s) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: c = self.config w_q, w_s, _, _ = self._get_weight_params(layer) w_ch_s = layer.weight_chan_scale x_2d = x.reshape(-1, x.shape[-1]) out_shape = x.shape[:-1] + (c.partition_weight_shape[1],) x_2d, act_scales = self.quant_fp8(x_2d) output = ops.cutlass_w4a8_mm( a=x_2d, b_q=w_q, b_group_scales=w_s, b_group_size=c.group_size, a_token_scales=act_scales, b_channel_scales=w_ch_s, ) if bias is not None: output.add_(bias) # In-place add return output.reshape(out_shape)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/mixed_precision/dynamic_4bit.py
vllm/model_executor/layers/quantization/kernels/mixed_precision/dynamic_4bit.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm.model_executor.layers.quantization.utils import replace_parameter from vllm.platforms import CpuArchEnum, current_platform from vllm.scalar_type import scalar_types from .MPLinearKernel import MPLinearKernel, MPLinearLayerConfig class Dynamic4bitLinearKernel(MPLinearKernel): SUPPORTED_QUANT_TYPES = [scalar_types.int4] @classmethod def get_min_capability(cls) -> int: return 1 @classmethod def can_implement(cls, c: MPLinearLayerConfig) -> tuple[bool, str | None]: if not current_platform.is_cpu(): return False, "Only CPU is supported" if c.weight_type not in cls.SUPPORTED_QUANT_TYPES: return False, f"Unsupported quant type {c.weight_type}" if ( current_platform.get_cpu_architecture() == CpuArchEnum.ARM and c.act_type not in [ torch.float32, ] ): return False, "Dynamic4bitLinearKernel on Arm requires Float32 activations" if c.full_weight_shape[0] % c.group_size != 0: return ( False, f"Group size ({c.group_size}) does not evenly divide" " the number of input features " f"({c.full_weight_shape[0]})", ) if current_platform.get_cpu_architecture() == CpuArchEnum.ARM: try: # Attempt to retrieve the operation _ = torch.ops.aten._dyn_quant_matmul_4bit except AttributeError: return ( False, f"PyTorch {torch.__version__} does not support" " _dyn_quant_matmul_4bit. Install a newer version", ) return True, None def process_weights_after_loading(self, layer: torch.nn.Module): c = self.config packed_weight = getattr(layer, self.w_q_name) packed_weight = packed_weight.add(8) uint8_packed = (packed_weight[::, 1::2] << 4 | packed_weight[::, ::2]).to( torch.uint8 ) scales = getattr(layer, self.w_s_name) block_size = c.group_size # Handle scaling factors for partitioned weights if block_size == c.partition_weight_shape[0]: scales = scales.to( torch.float32 ) # Float32 & Bfloat16 variants requires float32 scales scales = scales.view(-1, 1) # Channel-wise scales if layer.bias is not None: layer.bias = layer.bias.to( torch.float32 ) # Float32 & Bfloat16 variants requires float32 bias else: # KleidiAI kernel requires bfloat16 scales with groupwise scheme scales = scales.to(torch.bfloat16) # Repack weights as per kernel requirement w = torch.ops.aten._dyn_quant_pack_4bit_weight( uint8_packed, scales, layer.bias, block_size, c.partition_weight_shape[0], c.partition_weight_shape[1], ) replace_parameter( layer, self.w_q_name, torch.nn.Parameter(w, requires_grad=False) ) setattr(layer, self.w_s_name, None) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: c = self.config x_2d = x.reshape(-1, x.shape[-1]) out_shape = x.shape[:-1] + (c.partition_weight_shape[1],) w_q = getattr(layer, self.w_q_name) output = torch.ops.aten._dyn_quant_matmul_4bit( x_2d, w_q, c.group_size, c.partition_weight_shape[0], c.partition_weight_shape[1], ) return output.reshape(out_shape)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/mixed_precision/exllama.py
vllm/model_executor/layers/quantization/kernels/mixed_precision/exllama.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm import _custom_ops as ops from vllm.model_executor.layers.quantization.utils.quant_utils import ( pack_quantized_values_into_int32, ) from vllm.model_executor.parameter import BasevLLMParameter, permute_param_layout_ from vllm.scalar_type import scalar_types from .MPLinearKernel import MPLinearKernel, MPLinearLayerConfig class ExllamaLinearKernel(MPLinearKernel): SUPPORTED_QUANT_TYPES = [scalar_types.uint4b8, scalar_types.uint8b128] # In theory supports `scalar_types.uint2b2, scalar_types.uint3b4` too but # currently untested so not added to the list @classmethod def get_min_capability(cls) -> int: return 60 @classmethod def can_implement(cls, c: MPLinearLayerConfig) -> tuple[bool, str | None]: if c.has_g_idx and c.partition_weight_shape[0] != c.full_weight_shape[0]: return ( False, "Act reordering currently not supported by Exllama, " "when the input features are partitioned across " "devices", ) if c.partition_weight_shape[1] % (32 // c.weight_type.size_bits) != 0: return ( False, "Output features must be a multiple of the pack " "factor (32 / num_bits) so that we can correctly " "pack the zero points", ) if c.act_type != torch.float16: return False, "Exllama only supports float16 activations" if c.weight_type not in cls.SUPPORTED_QUANT_TYPES: return ( False, f"Quant type ({c.weight_type}) not supported by " "Exllama, supported types are: " f"{cls.SUPPORTED_QUANT_TYPES}", ) if c.full_weight_shape[0] % c.group_size != 0: return ( False, f"Group size ({c.group_size}) does not evenly divide" " the number of input features " f"({c.full_weight_shape[0]})", ) return True, None def process_weights_after_loading(self, layer: torch.nn.Module): c = self.config # For Exllama, we need to set a zero-point tensor if there is not one if not c.zero_points: self.w_zp_name = "qzeros" device = getattr(layer, self.w_q_name).device groups = c.partition_weight_shape[0] // c.group_size out_features = c.partition_weight_shape[1] if c.weight_type.has_bias(): # if the type has a bias we have to create a zeros tensor that # contains the bias values repeated for each group (-1 due to # a bug in the original GPTQ checkpoint format leading to # exllama kernel adding 1 to the zero points during inference) # Documentation of the bug can be found here: # https://garden.danieldk.eu/GPTQ-Checkpoint-Format zeros = torch.full( (groups, out_features), c.weight_type.bias - 1, dtype=torch.int32, device=device, ) else: raise NotImplementedError( "A 0 zero-point is not supported by Exllama due to " "a bug in the original GPTQ checkpoint format leading to " "exllama kernel adding 1 to the zero points during " "inference" ) zeros = pack_quantized_values_into_int32(zeros, c.weight_type, packed_dim=1) setattr( layer, self.w_zp_name, torch.nn.Parameter(zeros, requires_grad=False) ) if c.has_g_idx: def transform_w_g_idx(x): # Exllama wants the permutation array instead of the group # indices return torch.argsort(x).to(torch.int) self._transform_param(layer, self.w_gidx_name, transform_w_g_idx) else: self.w_gidx_name = "g_idx" empty_g_idx = torch.nn.Parameter( torch.empty((0,), dtype=torch.int, device=device), requires_grad=False ) setattr(layer, self.w_gidx_name, empty_g_idx) def transform_w_q(x): assert isinstance(x, BasevLLMParameter) assert self.w_gidx_name is not None g_idx = getattr(layer, self.w_gidx_name) permute_param_layout_(x, input_dim=0, output_dim=1, packed_dim=0) x_cont = x.data.contiguous() ops.gptq_shuffle(x_cont, g_idx, c.weight_type.size_bits) return x_cont def transform_w_s(x): assert isinstance(x, BasevLLMParameter) permute_param_layout_(x, input_dim=0, output_dim=1) x.data = x.data.contiguous() return x.to(dtype=c.act_type) # Repack weights and scales for Machete self._transform_param(layer, self.w_q_name, transform_w_q) self._transform_param(layer, self.w_s_name, transform_w_s) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: c = self.config x_2d = x.reshape(-1, x.shape[-1]) out_shape = x.shape[:-1] + (c.partition_weight_shape[1],) w_q, w_s, w_zp, w_g_idx = self._get_weight_params(layer) # gptq_gemm supports GPTQv2 format by passing use_v2_format=True. # However, the MPLinearLayerConfig doesn't contain format info. # So hardcode GPTQv1 format here, to keep its behavior unchanged. use_v2_format = False assert w_zp is not None, "Zero points are required by Exllama" assert w_g_idx is not None, "Group index is required by Exllama" output = ops.gptq_gemm( x_2d, w_q, w_zp, w_s, w_g_idx, True, use_v2_format, c.weight_type.size_bits ) if bias is not None: output.add_(bias) return output.reshape(out_shape)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/mixed_precision/bitblas.py
vllm/model_executor/layers/quantization/kernels/mixed_precision/bitblas.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from packaging import version from vllm.logger import init_logger from vllm.model_executor.layers.quantization import QuantizationConfig from vllm.model_executor.layers.quantization.utils import replace_parameter from vllm.model_executor.layers.quantization.utils.bitblas_utils import ( BITBLAS_OPTIMIZE_FEATURES, BITBLAS_SUPPORTED_GROUP_SIZES, MINIMUM_BITBLAS_VERSION, bitblas_make_empty_g_idx, bitblas_sort_g_idx, check_bitblas_supports_shape, query_bitblas_supported_quant_types, unpack_gptq_qweight, unpack_gptq_qzeros, ) from .MPLinearKernel import MPLinearKernel, MPLinearLayerConfig logger = init_logger(__name__) class BitBLASLinearKernel(MPLinearKernel): OPT_FEATURES: list[int] = BITBLAS_OPTIMIZE_FEATURES ENABLE_TUNING: bool = True MATMUL_LAYOUT: str = "nt" BITBLAS_DTYPES: dict[torch.dtype, str] = { torch.float32: "float32", torch.float16: "float16", torch.bfloat16: "bfloat16", torch.half: "float16", torch.int8: "int8", } bitblas_matmul: object = None def __init__( self, c: MPLinearLayerConfig, w_q_param_name: str, w_s_param_name: str, w_zp_param_name: str | None = None, w_gidx_param_name: str | None = None, bitblas_quant_config: QuantizationConfig | None = None, ): self.quant_config = bitblas_quant_config super().__init__( c, w_q_param_name, w_s_param_name, w_zp_param_name, w_gidx_param_name ) def repack_bitblas_from_gptq( self, b_q_weight: torch.Tensor, scales: torch.Tensor, qzeros: torch.Tensor | None = None, ): from bitblas.quantization.utils import general_compress assert self.bitblas_matmul is not None, "bitblas_matmul is None" quant_config = self.quant_config # qweight in gptq old quant linear stored with # (outfeatures, infeatures), should be transposed. qweight = b_q_weight.T.contiguous().view(quant_config.torch_storage_dtype) # type: ignore[union-attr] intweight = unpack_gptq_qweight(qweight, quant_config.weight_bits).contiguous() # type: ignore[union-attr] if self.bitblas_matmul.weight_transform is not None: # type: ignore[attr-defined] qweight = self.bitblas_matmul.weight_transform( # type: ignore[attr-defined] intweight.cpu() ).cuda() # scales in gptq old quant linear stored with # (infeatures // group_size, outfeatures), should be transposed. scales = scales.T.contiguous() if qzeros is None: return qweight, scales, None # qzeros should be de-quantized to int zeros. weight_bits = quant_config.weight_bits # type: ignore[union-attr] intzeros = unpack_gptq_qzeros(qzeros, weight_bits).T.contiguous() zeros: torch.Tensor | None = None zeros_mode = self.bitblas_matmul.config.zeros_mode # type: ignore[attr-defined] if zeros_mode == "original": zeros = intzeros.to(torch.float16).contiguous() elif zeros_mode == "rescale": assert zeros is not None, "zeros should not be None" zeros[:, :] = intzeros.to(torch.float16)[:, :] * scales[:, :] elif zeros_mode == "quantized": zeros = ( torch.Tensor( general_compress( intzeros.T.contiguous().cpu().numpy(), weight_bits, ) ) .to(qweight.device) .to( quant_config.torch_storage_dtype # type: ignore[union-attr] ) .contiguous() ) else: raise ValueError("Unsupported zeros type: {}".format(zeros_mode)) return qweight, scales, zeros @classmethod def get_min_capability(cls) -> int: return 70 @classmethod def can_implement(cls, c: MPLinearLayerConfig) -> tuple[bool, str | None]: is_bitblas_installed = True try: import bitblas if version.parse(bitblas.__version__) < version.parse( MINIMUM_BITBLAS_VERSION ): raise ImportError( "bitblas version is wrong. Please " f"install bitblas>={MINIMUM_BITBLAS_VERSION}" ) except ImportError: is_bitblas_installed = False if not is_bitblas_installed: return ( False, "bitblas is not installed. Please install bitblas " "by running `pip install bitblas>=" f"{MINIMUM_BITBLAS_VERSION}`", ) quant_types = query_bitblas_supported_quant_types(c.zero_points) if c.weight_type not in quant_types: return False, ( f"Quant type ({c.weight_type}) not supported by" f" BitBLAS, supported types are: {quant_types}" ) if c.group_size not in BITBLAS_SUPPORTED_GROUP_SIZES: return False, ( f"Group size ({c.group_size}) not supported by " "BitBLAS, supported group sizes are: " f"{BITBLAS_SUPPORTED_GROUP_SIZES}" ) return check_bitblas_supports_shape( c.partition_weight_shape[1], # out_features c.partition_weight_shape[0], # in_features c.full_weight_shape[0], # in_features c.group_size, ) # note assumes that # `weight_packed` is: {input_dim = 0, output_dim = 1, packed_dim = 0} # `weight_scale` is: {input_dim = 0, output_dim = 1} def process_weights_after_loading(self, layer: torch.nn.Module) -> None: device = getattr(layer, self.w_q_name).device c = self.config quant_config = self.quant_config # Default names since bitblas requires empty parameters for these, # TODO: remove this requirement from bitblas (allow optional tensors) if getattr(self, "w_gidx_name", None) is None: self.w_gidx_name: str = "g_idx" if getattr(self, "w_zp_name", None) is None: self.w_zp_name: str = "qzeros" if c.has_g_idx: g_idx, g_idx_sort_indices = bitblas_sort_g_idx( getattr(layer, self.w_gidx_name) ) self._transform_param(layer, self.w_gidx_name, lambda _: g_idx) layer.g_idx_sort_indices = g_idx_sort_indices else: setattr(layer, self.w_gidx_name, bitblas_make_empty_g_idx(device)) layer.g_idx_sort_indices = bitblas_make_empty_g_idx(device) if c.zero_points: raise NotImplementedError("Zero points not supported by BitBLAS") else: setattr(layer, self.w_zp_name, bitblas_make_empty_g_idx(device)) # Repack weights bitblas_qweight, bitblas_scales, bitblas_qzeros = self.repack_bitblas_from_gptq( layer.qweight, layer.scales, None if quant_config.is_sym else layer.qzeros, # type: ignore[union-attr] ) replace_parameter(layer, self.w_q_name, bitblas_qweight) replace_parameter(layer, self.w_s_name, bitblas_scales) if bitblas_qzeros is not None: replace_parameter(layer, self.w_zp_name, bitblas_qzeros) def configure_bitblas_matmul( self, infeatures: int, outfeatures: int, params_dtype: torch.dtype, bias: bool, ) -> None: enable_tuning = self.ENABLE_TUNING layout = self.MATMUL_LAYOUT bits = self.quant_config.weight_bits # type: ignore[union-attr] self._configure_bitblas_matmul( infeatures, outfeatures, params_dtype, enable_tuning, bias, layout, bits, ) def _configure_bitblas_matmul( self, infeatures, outfeatures, params_dtype, enable_tuning, bias, layout, bits, ): from bitblas import MatmulConfig bitblas_dtype = self.BITBLAS_DTYPES[params_dtype] quant_config = self.quant_config with_scaling = False with_zeros = False group_size = quant_config.group_size # type: ignore[union-attr] zeros_mode = quant_config.zeros_mode # type: ignore[union-attr] if quant_config.quant_method == "gptq": # type: ignore[union-attr] with_scaling = True with_zeros = True W_dtype = f"uint{bits}" if quant_config.is_sym: # type: ignore[union-attr] with_zeros = False W_dtype = f"int{bits}" else: raise ValueError( f"Unsupported quant_method {quant_config.quant_method}" # type: ignore[union-attr] ) # type: ignore[union-attr] matmul_config = MatmulConfig( M=self.OPT_FEATURES, N=outfeatures, K=infeatures, A_dtype=bitblas_dtype, W_dtype=W_dtype, out_dtype=bitblas_dtype, accum_dtype="int32" if bitblas_dtype == "int8" else bitblas_dtype, storage_dtype=quant_config. # type: ignore[union-attr] storage_dtype, # type: ignore[union-attr] with_scaling=with_scaling, with_zeros=with_zeros, group_size=group_size, with_bias=bias, layout=layout, zeros_mode=zeros_mode, ) self.bitblas_matmul = self._get_or_create_bitblas_operator( matmul_config, enable_tuning ) def _get_or_create_bitblas_operator(self, config, enable_tuning): from bitblas import Matmul, auto_detect_nvidia_target from bitblas.cache import get_database_path, global_operator_cache BITBLAS_DATABASE_PATH = get_database_path() BITBLAS_TARGET = auto_detect_nvidia_target() if global_operator_cache.size() == 0: global_operator_cache.load_from_database( BITBLAS_DATABASE_PATH, BITBLAS_TARGET ) bitblas_matmul = global_operator_cache.get(config) if bitblas_matmul is None: bitblas_matmul = Matmul(config, target=BITBLAS_TARGET, enable_tuning=False) if enable_tuning: bitblas_matmul.hardware_aware_finetune(topk=20) global_operator_cache.add(config, bitblas_matmul) global_operator_cache.save_into_database( BITBLAS_DATABASE_PATH, BITBLAS_TARGET ) TUNING_MESSAGE = ( f"BitBLAS Operator {config} tuned and saved to database." ) logger.info(TUNING_MESSAGE) else: _message = f"BitBLAS Operator {config} created without tuning. " logger.info(_message) else: _message = f"BitBLAS Operator {config} retrieved from cache." logger.info(_message) return bitblas_matmul def apply_gptq_bitblas_linear( self, layer: torch.nn.Module, x: torch.Tensor, ) -> torch.Tensor: output_size_per_partition = self.config.partition_weight_shape[1] out_shape = x.shape[:-1] + (output_size_per_partition,) args = [x, layer.qweight, layer.scales] if self.bitblas_matmul.config.with_zeros: # type: ignore[attr-defined] args.append(layer.qzeros) output = self.bitblas_matmul(*args) # type: ignore[operator] return output.view(out_shape) def apply_weights(self, layer, x, bias=None): NOT_IMPLEMENT_MESSAGE = ( f"{self.__class__.__name__}.apply_weights is not implemented. " "Please use BitBLASLinearKernel.apply_gptq_bitblas_linear instead" ) raise NotImplementedError(NOT_IMPLEMENT_MESSAGE)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/mixed_precision/machete.py
vllm/model_executor/layers/quantization/kernels/mixed_precision/machete.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from functools import partial import torch from vllm import _custom_ops as ops from vllm.model_executor.layers.quantization.utils.machete_utils import ( check_machete_supports_shape, query_machete_supported_group_sizes, query_machete_supported_quant_types, ) from vllm.model_executor.layers.quantization.utils.quant_utils import ( pack_quantized_values_into_int32, unpack_quantized_values_into_int32, ) from vllm.model_executor.parameter import BasevLLMParameter, permute_param_layout_ from vllm.platforms import current_platform from .MPLinearKernel import MPLinearKernel, MPLinearLayerConfig class MacheteLinearKernel(MPLinearKernel): @classmethod def get_min_capability(cls) -> int: return 90 @classmethod def can_implement(cls, c: MPLinearLayerConfig) -> tuple[bool, str | None]: # Machete uses CUTLASS, so it can only be compatible with Nvidia if not current_platform.is_cuda(): return False, "Machete only supported on CUDA" if not current_platform.is_device_capability(90): return False, "Machete requires compute capability of 90 (Hopper)" if c.has_g_idx and c.partition_weight_shape[0] != c.full_weight_shape[0]: return ( False, "Act reordering currently not supported by Machete, " "when the input features are partitioned across " "devices", ) if c.weight_type not in query_machete_supported_quant_types(c.zero_points): return ( False, f"Quant type ({c.weight_type}) not supported by " "Machete, supported types are: " f"{query_machete_supported_quant_types(c.zero_points)}", ) if c.group_size not in query_machete_supported_group_sizes(c.act_type): return ( False, f"Group size ({c.group_size}) not supported by " "Machete, supported group sizes are: " f"{query_machete_supported_group_sizes(c.act_type)}", ) return check_machete_supports_shape( c.partition_weight_shape[0], c.partition_weight_shape[1] ) # note assumes that # `weight_packed` is: {input_dim = 0, output_dim = 1, packed_dim = 0} # `weight_scale` is: {input_dim = 0, output_dim = 1} # `weight_zp` is: {input_dim = 0, output_dim = 1, packed_dim = 1} def process_weights_after_loading(self, layer: torch.nn.Module): c = self.config if c.has_g_idx: assert self.w_gidx_name is not None perm = torch.argsort(getattr(layer, self.w_gidx_name)).to(torch.int) self.act_perm = lambda x: x[:, perm] # use `ops.permute_cols` if possible if ( c.act_type in [torch.float16, torch.bfloat16] and c.partition_weight_shape[0] % 8 == 0 ): self.act_perm = partial(ops.permute_cols, perm=perm) def transform_w_q(x): assert isinstance(x, BasevLLMParameter) permute_param_layout_(x, input_dim=0, output_dim=1, packed_dim=0) if c.has_g_idx: x_unpacked = unpack_quantized_values_into_int32( x.data, c.weight_type, packed_dim=0 ) x_perm = x_unpacked[perm, :] x.data = pack_quantized_values_into_int32( x_perm, c.weight_type, packed_dim=0 ) x.data = ops.machete_prepack_B( x.data.t().contiguous().t(), a_type=c.act_type, b_type=c.weight_type, group_scales_type=c.act_type, ) return x def transform_w_s(x): assert isinstance(x, BasevLLMParameter) permute_param_layout_(x, input_dim=0, output_dim=1) x.data = x.data.contiguous() return x def transform_w_zp(x): assert isinstance(x, BasevLLMParameter) permute_param_layout_(x, input_dim=0, output_dim=1, packed_dim=1) x_unpacked = unpack_quantized_values_into_int32( x.data, c.weight_type, packed_dim=1 ) w_s = getattr(layer, self.w_s_name).data # pre-apply scales to zero-points x.data = (-1.0 * w_s * (x_unpacked.to(w_s.dtype))).contiguous() return x # Repack weights and scales for Machete self._transform_param(layer, self.w_q_name, transform_w_q) self._transform_param(layer, self.w_s_name, transform_w_s) if c.zero_points: self._transform_param(layer, self.w_zp_name, transform_w_zp) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: c = self.config w_q, w_s, w_zp, _ = self._get_weight_params(layer) x_2d = x.reshape(-1, x.shape[-1]) out_shape = x.shape[:-1] + (c.partition_weight_shape[1],) if c.has_g_idx: x_2d = self.act_perm(x_2d) if c.zero_points: assert w_zp is not None else: w_zp = None output = ops.machete_mm( a=x_2d, b_q=w_q, b_type=c.weight_type, b_group_zeros=w_zp, b_group_scales=w_s, b_group_size=c.group_size, ) if bias is not None: output.add_(bias) # In-place add return output.reshape(out_shape)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/quantization/kernels/mixed_precision/MPLinearKernel.py
vllm/model_executor/layers/quantization/kernels/mixed_precision/MPLinearKernel.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from abc import ABC, abstractmethod from collections.abc import Callable from dataclasses import dataclass import torch from vllm.model_executor.layers.quantization.utils import replace_parameter from vllm.scalar_type import ScalarType @dataclass class MPLinearLayerConfig: full_weight_shape: tuple[int, int] # [in, out] partition_weight_shape: tuple[int, int] weight_type: ScalarType act_type: torch.dtype group_size: int zero_points: bool has_g_idx: bool out_type: torch.dtype | None = None class MPLinearKernel(ABC): @classmethod @abstractmethod def get_min_capability(cls) -> int: raise NotImplementedError @classmethod @abstractmethod def can_implement(cls, c: MPLinearLayerConfig) -> tuple[bool, str | None]: raise NotImplementedError def __init__( self, c: MPLinearLayerConfig, w_q_param_name: str, w_s_param_name: str, w_zp_param_name: str | None = None, w_gidx_param_name: str | None = None, ) -> None: assert self.can_implement(c) self.config = c self.w_q_name = w_q_param_name self.w_s_name = w_s_param_name if c.zero_points: assert w_zp_param_name is not None if c.has_g_idx: assert w_gidx_param_name is not None self.w_zp_name = w_zp_param_name self.w_gidx_name = w_gidx_param_name @abstractmethod def process_weights_after_loading(self, layer: torch.nn.Module) -> None: raise NotImplementedError @abstractmethod def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: raise NotImplementedError def _transform_param( self, layer: torch.nn.Module, name: str | None, fn: Callable ) -> None: if name is not None and getattr(layer, name, None) is not None: old_param = getattr(layer, name) new_param = fn(old_param) # replace the parameter with torch.nn.Parameter for TorchDynamo # compatibility replace_parameter( layer, name, torch.nn.Parameter(new_param.data, requires_grad=False) ) def _get_weight_params( self, layer: torch.nn.Module ) -> tuple[ torch.Tensor, # w_q torch.Tensor, # w_s torch.Tensor | None, # w_zp, torch.Tensor | None, # w_gidx ]: return ( getattr(layer, self.w_q_name), getattr(layer, self.w_s_name), getattr(layer, self.w_zp_name or "", None), getattr(layer, self.w_gidx_name or "", None), )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/zero_expert_fused_moe.py
vllm/model_executor/layers/fused_moe/zero_expert_fused_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from contextlib import contextmanager import torch from torch import nn from vllm.model_executor.layers.fused_moe.fused_moe import zero_experts_compute_triton from vllm.model_executor.layers.fused_moe.layer import FusedMoE class ZeroExpertFusedMoE(FusedMoE): """ A FusedMoE operation that also computes the results of zero experts. Zero experts perform identity operations (scaled pass-through) instead of full MLP computations. This class uses memoization to avoid redundant routing computation: routing is computed once and reused for both zero expert computation and the main FusedMoE forward pass. """ def __init__( self, zero_expert_num: int, zero_expert_type: str, router: nn.Module, **kwargs, ): # ZeroExpertFusedMoE manages its own custom_routing_function for memoization assert ( "custom_routing_function" not in kwargs or kwargs.get("custom_routing_function") is None ), ( "ZeroExpertFusedMoE does not support external custom_routing_function. " "It manages its own for routing memoization." ) # Automatically slice router's e_score_correction_bias to only include # real experts (not zero_experts) for the base FusedMoE. # The full bias will be used temporarily in forward() for routing. if hasattr(router, "e_score_correction_bias") and "num_experts" in kwargs: num_real_experts = kwargs["num_experts"] router_bias = router.e_score_correction_bias user_bias = kwargs.get("e_score_correction_bias") # Use router's bias if: # 1. User didn't provide bias, or # 2. User provided full bias (same size as router) if user_bias is None or user_bias.shape[0] == router_bias.shape[0]: kwargs["e_score_correction_bias"] = router_bias[:num_real_experts] # FusedMoE no longer accepts zero_expert_num/zero_expert_type. # We handle zero experts ourselves in forward(). super().__init__(**kwargs) # Store the actual zero_expert_num and zero_expert_type for our own use self._actual_zero_expert_num = zero_expert_num self._actual_zero_expert_type = zero_expert_type self._router = router # Full router (includes zero experts) # Expose zero_expert_num and zero_expert_type as attributes for # compatibility with quantization methods that check these attributes self.zero_expert_num = 0 self.zero_expert_type = None # Memoization state for routing results self._memoized_topk_weights: torch.Tensor | None = None self._memoized_topk_ids: torch.Tensor | None = None # Create custom_routing_function to reuse memoized routing results def custom_routing_function(hidden_states, gating_output, topk, renormalize): """Return memoized `topk_weights` and `topk_ids`.""" if self._memoized_topk_weights is None or self._memoized_topk_ids is None: raise RuntimeError( "ZeroExpertFusedMoE: routing results not memoized. " "Call select_experts first to compute routing." ) return self._memoized_topk_weights, self._memoized_topk_ids self.custom_routing_function = custom_routing_function @contextmanager def _temporarily_set_attrs(self, **attrs): """ Temporarily set attributes using object.__setattr__ and restore them. This bypasses nn.Module.__setattr__ to avoid Dynamo tracing issues. When PyTorch Dynamo traces the forward pass, it cannot handle nn.Module.__setattr__ calls (which include parameter registration logic), resulting in "Unsupported" errors. Using object.__setattr__ directly sets the attribute without triggering nn.Module's custom __setattr__, allowing Dynamo to trace the code successfully. """ originals = {key: getattr(self, key) for key in attrs} try: for key, value in attrs.items(): object.__setattr__(self, key, value) yield finally: for key, value in originals.items(): object.__setattr__(self, key, value) def _compute_zero_expert_result( self, hidden_states: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, ) -> torch.Tensor | None: """Compute zero expert results using pre-computed routing.""" if ( self._actual_zero_expert_num is None or self._actual_zero_expert_num <= 0 or self._actual_zero_expert_type is None ): return None return zero_experts_compute_triton( expert_indices=topk_ids.clone(), expert_scales=topk_weights.clone(), num_experts=self.logical_num_experts, zero_expert_type=self._actual_zero_expert_type, hidden_states=hidden_states, ) def forward( self, hidden_states: torch.Tensor, router_logits: torch.Tensor, # Full logits including zero experts ) -> torch.Tensor: """ Forward pass with zero expert support and routing memoization. Args: hidden_states: Input hidden states router_logits: Full router logits (including zero experts) Returns: Combined output from real experts and zero experts """ # Prepare temporary attribute overrides for routing computation temp_attrs = { "custom_routing_function": None, # Disable for first routing } if self._router is not None: temp_attrs["e_score_correction_bias"] = self._router.e_score_correction_bias # Compute routing with temporary attributes # Pass full router_logits (including zero experts) so that zero experts # can be properly identified in topk_ids with self._temporarily_set_attrs(**temp_attrs): topk_weights, topk_ids = self.select_experts( hidden_states=hidden_states, router_logits=router_logits, # Full logits (includes zero experts) ) # Compute zero expert result if needed zero_expert_result = self._compute_zero_expert_result( hidden_states=hidden_states, topk_weights=topk_weights, topk_ids=topk_ids, ) # Memoize routing results for reuse in super().forward() self._memoized_topk_weights = topk_weights self._memoized_topk_ids = topk_ids # Slice router_logits for real experts only router_logits_sliced = router_logits[..., : self.logical_num_experts] # Compute real expert results (will reuse memoized routing via # custom_routing_function) # zero_expert_num is already 0, so FusedMoE won't handle zero experts fused_out = super().forward( hidden_states=hidden_states, router_logits=router_logits_sliced, ) # Combine results # Both zero_expert_result and fused_out are computed from the same # hidden_states, so they should be on the same device. if zero_expert_result is not None: fused_out = fused_out + zero_expert_result # Clear memoization after use self._memoized_topk_weights = None self._memoized_topk_ids = None return fused_out
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/pplx_prepare_finalize.py
vllm/model_executor/layers/fused_moe/pplx_prepare_finalize.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import pplx_kernels as pplx import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe.config import FusedMoEQuantConfig from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import ( TopKWeightAndReduceDelegate, ) from vllm.model_executor.layers.fused_moe.utils import ( _validate_scale_shape, moe_kernel_quantize_input, ) from vllm.utils.math_utils import cdiv, round_up logger = init_logger(__name__) def pplx_hidden_dim_scale_bytes( max_num_tokens: int, hidden_dim: int, in_dtype: torch.dtype, quant_dtype: torch.dtype | str | None, per_act_token_quant: bool, block_shape: list[int] | None, ): # All pplx byte sizes must be 16-byte aligned. align = 16 # For blocked per token: set to # ceil_div(hidden_dim, block_size) * sizeof(float32) # For per-token: set to 4 * sizeof(float32) (x4 for alignment) if quant_dtype is not None: assert isinstance(quant_dtype, torch.dtype) assert quant_dtype.itemsize == 1 hidden_dim_bytes = hidden_dim * quant_dtype.itemsize elem_size = torch.float32.itemsize if per_act_token_quant: # per-token (M x 1) assert block_shape is None hidden_scale_bytes = elem_size elif block_shape is not None: # per-group (M x K_tiles) block_size = block_shape[1] num_blocks = cdiv(hidden_dim, block_size) hidden_scale_bytes = num_blocks * elem_size else: # per-tensor (1 x 1) hidden_scale_bytes = elem_size else: hidden_dim_bytes = hidden_dim * in_dtype.itemsize hidden_scale_bytes = 0 return ( round_up(hidden_dim_bytes, align), round_up(hidden_scale_bytes, align), ) class PplxPrepareAndFinalize(mk.FusedMoEPrepareAndFinalize): def __init__( self, a2a: pplx.AllToAll, max_num_tokens: int, num_local_experts: int, num_dispatchers: int, ): super().__init__() assert max_num_tokens > 0 assert num_local_experts > 0 self.a2a = a2a self.max_num_tokens = max_num_tokens self.num_local_experts = num_local_experts self.num_dispatchers_ = num_dispatchers @property def activation_format(self) -> mk.FusedMoEActivationFormat: return mk.FusedMoEActivationFormat.BatchedExperts def max_num_tokens_per_rank(self) -> int | None: return self.max_num_tokens def topk_indices_dtype(self) -> torch.dtype | None: return torch.uint32 def num_dispatchers(self) -> int: return self.num_dispatchers_ def output_is_reduced(self) -> bool: return True def supports_async(self) -> bool: return True def prepare_async( self, a1: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, num_experts: int, expert_map: torch.Tensor | None, apply_router_weight_on_input: bool, quant_config: FusedMoEQuantConfig, ) -> tuple[Callable, mk.ReceiverType]: num_tokens = a1.size(0) # M hidden_dim = a1.size(-1) # K assert topk_ids.size(0) == num_tokens # expert_map should be None because with expert map, -1 id is used for # non-local token; this causes error when casting ids to the # topk_indices_dtype() int32 # if expert_map is not None: logger.warning_once( "The PPLX backend does not support expert mapping. " "The provided `expert_map` will be ignored." ) expert_map = None # noqa: F841 # Is this always going to be a1.device? device = a1.device if apply_router_weight_on_input: topk = topk_ids.size(1) # TODO: this only works for topK=1, will need to update for topK>1 assert topk == 1, ( "apply_router_weight_on_input is only implemented for topk=1" ) a1 = a1 * topk_weights.to(a1.dtype) repeat_cols = 4 repeat_rows = 1 if quant_config.per_act_token_quant else a1.size(0) # TODO(bnell): always pass quant_config.a1_scale? a1q, a1q_scale = moe_kernel_quantize_input( a1, (None if quant_config.per_act_token_quant else quant_config.a1_scale), quant_dtype=quant_config.quant_dtype, per_act_token_quant=quant_config.per_act_token_quant, block_shape=quant_config.block_shape, ) _validate_scale_shape( a1q, a1q_scale, quant_config.per_act_token_quant, quant_config.block_shape ) orig_a_scale_block_shape: int | None = None if a1q_scale is not None: scalar_scales = a1q_scale.numel() == 1 # pplx requires 2-d scales even for scalar scales if a1q_scale.dim() <= 1: assert scalar_scales a1q_scale = a1q_scale.view(1, 1) orig_a_scale_block_shape = a1q_scale.shape[-1] if not quant_config.is_block_quantized: # TODO (bnell): use group_broadcast instead? a1q_scale = a1q_scale.repeat(repeat_rows, repeat_cols) assert a1q_scale is None or a1q_scale.ndim == 2, ( f"{0 if a1q_scale is None else (a1q_scale.ndim, a1q_scale.shape)}" ) expert_num_tokens = torch.empty( self.num_local_experts, dtype=torch.int32, device=device, ) expert_x = torch.empty( ( self.num_local_experts, self.max_num_tokens * self.num_dispatchers(), hidden_dim, ), dtype=a1q.dtype, device=device, ) expert_x_scale: torch.Tensor | None = None if a1q.dtype.itemsize == 1: if quant_config.is_per_act_token: # (M x 1) -> (E x M x K) final_dim = expert_x.size(2) elif quant_config.is_per_tensor: # (1 x 1) -> (E x 1 x 1) final_dim = 1 else: # (M x K_tiles) -> (E x M x K_tiles) assert quant_config.block_shape is not None num_blocks = cdiv(expert_x.size(2), quant_config.block_shape[1]) final_dim = num_blocks expert_x_scale_shape = ( self.num_local_experts, expert_x.size(1), round_up(final_dim, 4), # round up for alignment ) expert_x_scale = torch.empty( expert_x_scale_shape, dtype=torch.float32, device=expert_x.device, ) # This argument is optional, defaults to indices.size(0) # There's not much point setting this unless it is != indices.size(0) bound_m: torch.Tensor | None = None self.a2a.dispatch( out_expert_num_tokens=expert_num_tokens, out_expert_x=expert_x, out_expert_x_scale=expert_x_scale, dp_x=a1q, dp_x_scale=a1q_scale, indices=topk_ids, bound_m=bound_m, do_send=True, do_recv=False, ) hook = lambda: self.a2a.dispatch( out_expert_num_tokens=expert_num_tokens, out_expert_x=expert_x, out_expert_x_scale=expert_x_scale, dp_x=a1q, dp_x_scale=a1q_scale, indices=topk_ids, bound_m=bound_m, do_send=False, do_recv=True, ) return ( hook, lambda: self._receiver( expert_num_tokens, expert_x, expert_x_scale, orig_a_scale_block_shape, ), ) def _receiver( self, expert_num_tokens: torch.Tensor, expert_x: torch.Tensor, expert_x_scale: torch.Tensor | None, orig_a_scale_block_shape: int | None, ) -> mk.PrepareResultType: if expert_x_scale is not None: expert_x_scale = expert_x_scale[:, :, :orig_a_scale_block_shape] assert expert_x_scale.ndim == 3 expert_tokens_meta = mk.ExpertTokensMetadata( expert_num_tokens=expert_num_tokens, expert_num_tokens_cpu=None ) return expert_x, expert_x_scale, expert_tokens_meta, None, None def prepare( self, a1: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, num_experts: int, expert_map: torch.Tensor | None, apply_router_weight_on_input: bool, quant_config: FusedMoEQuantConfig, ) -> mk.PrepareResultType: hook, receiver = self.prepare_async( a1, topk_weights, topk_ids, num_experts, expert_map, apply_router_weight_on_input, quant_config, ) hook() return receiver() def finalize_async( self, output: torch.Tensor, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, weight_and_reduce_impl: mk.TopKWeightAndReduce, ) -> Callable: assert isinstance(weight_and_reduce_impl, TopKWeightAndReduceDelegate), ( "Weight application and reduction happens in the combine kernel." ) # This argument is optional # There's not much point setting this unless it is != topk_ids.size(0) bound_m: torch.Tensor | None = None # TODO (bnell): fails in test_pplx_moe.py, figure out what's going on # num_tokens = output.size(0) # M # assert topk_ids.size(0) == num_tokens, ( # f"{topk_ids.size(0)} == {num_tokens}") assert topk_ids.size() == topk_weights.size(), ( f"{topk_ids.size()} == {topk_weights.size()}" ) assert output.size(0) <= self.max_num_tokens, ( f"{output.size(0)} <= {self.max_num_tokens}" ) assert output.size(1) == fused_expert_output.size(-1) # Set weights to 1 if we did them in dispatch. This is hacky. if apply_router_weight_on_input: topk_weights = torch.ones_like(topk_weights) topk_ids_u32 = topk_ids.view(dtype=torch.uint32) self.a2a.combine( out_tokens=output, indices=topk_ids_u32, weights=topk_weights, expert_y=fused_expert_output, bound_m=bound_m, do_send=True, do_recv=False, ) return lambda: self.a2a.combine( out_tokens=output, indices=topk_ids_u32, weights=topk_weights, expert_y=fused_expert_output, bound_m=bound_m, do_send=False, do_recv=True, ) def finalize( self, output: torch.Tensor, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, weight_and_reduce_impl: mk.TopKWeightAndReduce, ) -> None: receiver = self.finalize_async( output, fused_expert_output, topk_weights, topk_ids, apply_router_weight_on_input, weight_and_reduce_impl, ) receiver()
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/deep_gemm_moe.py
vllm/model_executor/layers/fused_moe/deep_gemm_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe.config import ( FusedMoEQuantConfig, fp8_w8a8_moe_quant_config, ) from vllm.model_executor.layers.fused_moe.deep_gemm_utils import ( compute_aligned_M, deepgemm_moe_permute, deepgemm_unpermute_and_reduce, ) from vllm.model_executor.layers.fused_moe.prepare_finalize import ( MoEPrepareAndFinalizeNoEP, ) from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import ( TopKWeightAndReduceNoOP, ) from vllm.model_executor.layers.fused_moe.utils import _resize_cache from vllm.model_executor.layers.quantization.utils.fp8_utils import ( per_token_group_quant_fp8, per_token_group_quant_fp8_packed_for_deepgemm, silu_mul_per_token_group_quant_fp8_colmajor, ) from vllm.utils.deep_gemm import ( DeepGemmQuantScaleFMT, get_mk_alignment_for_contiguous_layout, m_grouped_fp8_gemm_nt_contiguous, ) from vllm.utils.import_utils import has_deep_gemm logger = init_logger(__name__) def _valid_deep_gemm_shape(M: int, N: int, K: int) -> bool: align = get_mk_alignment_for_contiguous_layout()[0] return align <= M and N % align == 0 and K % align == 0 def _valid_deep_gemm( hidden_states: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor ) -> bool: """ Check if the given problem size is supported by the DeepGemm grouped gemm kernel. All of M, N, K and the quantization block_shape must be aligned by `dg.get_m_alignment_for_contiguous_layout()`. """ if not has_deep_gemm(): logger.debug_once("DeepGemm disabled: deep_gemm not available.") return False M = hidden_states.size(0) _, K, N = w2.size() align = get_mk_alignment_for_contiguous_layout()[0] if not _valid_deep_gemm_shape(M, N, K): logger.debug_once( "DeepGemm disabled due to unaligned problem size. " "M: %s, N: %s, K: %s. M should >= %s " "and N and K must be multiples of %s. " "This is not an error and we will fall back to triton.", M, N, K, align, align, ) return False elif N <= 512: logger.debug_once( "DeepGemm disabled for N <= 512. M: %s, N: %s, K: %s. " "This means we will fallback to triton " "for this specific shape for further speed up.", M, N, K, ) return False if w1.dtype != torch.float8_e4m3fn or w2.dtype != torch.float8_e4m3fn: logger.debug_once( "DeepGemm disabled: invalid weight dtype(s). w1.dtype: %s, w2.dtype: %s", w1.dtype, w2.dtype, ) return False if ( not hidden_states.is_contiguous() or not w1.is_contiguous() or not w2.is_contiguous() ): logger.debug_once( "DeepGemm disabled: weights or activations not contiguous. " "hidden_states.is_contiguous(): %s, w1.is_contiguous(): %s, " "w2.is_contiguous(): %s", hidden_states.is_contiguous(), w1.is_contiguous(), w2.is_contiguous(), ) return False return True class DeepGemmExperts(mk.FusedMoEPermuteExpertsUnpermute): def __init__(self, quant_config: FusedMoEQuantConfig): super().__init__(quant_config) assert quant_config.block_shape == get_mk_alignment_for_contiguous_layout() assert quant_config.quant_dtype == torch.float8_e4m3fn assert not quant_config.per_act_token_quant assert not quant_config.per_out_ch_quant @property def activation_formats( self, ) -> tuple[mk.FusedMoEActivationFormat, mk.FusedMoEActivationFormat]: return ( mk.FusedMoEActivationFormat.Standard, mk.FusedMoEActivationFormat.Standard, ) def supports_chunking(self) -> bool: return True def supports_expert_map(self) -> bool: return True def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce: return TopKWeightAndReduceNoOP() def workspace_shapes( self, M: int, N: int, K: int, topk: int, global_num_experts: int, local_num_experts: int, expert_tokens_meta: mk.ExpertTokensMetadata | None, ) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...]]: assert self.block_shape is not None block_m = self.block_shape[0] M_sum = compute_aligned_M( M, topk, local_num_experts, block_m, expert_tokens_meta ) assert M_sum % block_m == 0 workspace1 = (M_sum, max(N // 2, K)) workspace2 = (M_sum, max(N, K)) output = (M, K) return (workspace1, workspace2, output) def _act_mul_quant( self, input: torch.Tensor, output: torch.Tensor, activation: str ) -> tuple[torch.Tensor, torch.Tensor]: assert self.block_shape is not None block_k = self.block_shape[1] scale_fmt = DeepGemmQuantScaleFMT.from_oracle() # 1. DeepGemm UE8M0: use packed per-token-group quant if scale_fmt == DeepGemmQuantScaleFMT.UE8M0: M_sum, N = input.size() act_out = torch.empty( (M_sum, N // 2), dtype=input.dtype, device=input.device ) self.activation(activation, act_out, input) a2q, a2q_scale = per_token_group_quant_fp8_packed_for_deepgemm( act_out, block_k, out_q=output, ) return a2q, a2q_scale # 2. Hopper / non‑E8M0: prefer the fused SiLU+mul+quant kernel if activation == "silu": use_ue8m0 = scale_fmt == DeepGemmQuantScaleFMT.FLOAT32_CEIL_UE8M0 return silu_mul_per_token_group_quant_fp8_colmajor( input=input, output=output, use_ue8m0=use_ue8m0, ) # 3. fallback path for non-SiLU activations in non‑UE8M0 cases. M_sum, N = input.size() act_out = torch.empty((M_sum, N // 2), dtype=input.dtype, device=input.device) self.activation(activation, act_out, input) return per_token_group_quant_fp8( act_out, block_k, column_major_scales=True, out_q=output ) def apply( self, output: torch.Tensor, hidden_states: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: str, global_num_experts: int, expert_map: torch.Tensor | None, a1q_scale: torch.Tensor | None, a2_scale: torch.Tensor | None, workspace13: torch.Tensor, workspace2: torch.Tensor, expert_tokens_meta: mk.ExpertTokensMetadata | None, apply_router_weight_on_input: bool, ): assert a1q_scale is not None assert a2_scale is None assert self.block_shape is not None assert self.w1_scale is not None assert self.w2_scale is not None a1q = hidden_states _, N, K = w1.size() local_num_experts = w1.size(0) if global_num_experts == -1: global_num_experts = local_num_experts assert w2.size(1) == K M_sum = compute_aligned_M( M=topk_ids.size(0), num_topk=topk_ids.size(1), local_num_experts=local_num_experts, alignment=get_mk_alignment_for_contiguous_layout()[0], expert_tokens_meta=expert_tokens_meta, ) a1q_perm = _resize_cache( workspace13.view(dtype=torch.float8_e4m3fn), (M_sum, K) ) a1q, a1q_scale, expert_ids, inv_perm = deepgemm_moe_permute( aq=a1q, aq_scale=a1q_scale, topk_ids=topk_ids, local_num_experts=local_num_experts, expert_map=expert_map, expert_tokens_meta=expert_tokens_meta, aq_out=a1q_perm, ) assert a1q.size(0) == M_sum mm1_out = _resize_cache(workspace2, (M_sum, N)) m_grouped_fp8_gemm_nt_contiguous( (a1q, a1q_scale), (w1, self.w1_scale), mm1_out, expert_ids ) quant_out = _resize_cache( workspace13.view(dtype=torch.float8_e4m3fn), (M_sum, N // 2) ) a2q, a2q_scale = self._act_mul_quant( input=mm1_out.view(-1, N), output=quant_out, activation=activation ) mm2_out = _resize_cache(workspace2, (M_sum, K)) m_grouped_fp8_gemm_nt_contiguous( (a2q, a2q_scale), (w2, self.w2_scale), mm2_out, expert_ids ) if apply_router_weight_on_input: topk_weights = torch.ones_like(topk_weights) deepgemm_unpermute_and_reduce( a=mm2_out, topk_ids=topk_ids, topk_weights=topk_weights, inv_perm=inv_perm, expert_map=expert_map, output=output, ) def deep_gemm_moe_fp8( hidden_states: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor, w1_scale: torch.Tensor, w2_scale: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, inplace: bool = False, activation: str = "silu", global_num_experts: int = -1, expert_map: torch.Tensor | None = None, a1_scale: torch.Tensor | None = None, a2_scale: torch.Tensor | None = None, apply_router_weight_on_input: bool = False, ) -> torch.Tensor: """ This function computes a a8w8-quantized Mixture of Experts (MoE) layer using two sets of quantized weights, w1_q and w2_q, and top-k gating mechanism. The matrix multiplications are implemented with DeepGemm grouped gemm. Parameters: - hidden_states (torch.Tensor): The input tensor to the MoE layer. Shape: [M, K] - w1 (torch.Tensor): The first set of fp8 quantized expert weights. Shape: [num_experts, K, 2N] (the weights are passed transposed) - w2 (torch.Tensor): The second set of fp8 quantized expert weights. Shape: [num_experts, N, K] (the weights are passed transposed) - w1_scale (torch.Tensor): The fp32 scale to dequantize w1_q. Shape: [num_experts] or [num_experts, 2N] - w2_scale (torch.Tensor): The fp32 scale to dequantize w2_q. Shape: [num_experts] or [num_experts, K] - topk_weights (torch.Tensor): The weights of each token->expert mapping. - topk_ids (torch.Tensor): The token->expert mapping for topk_weights. - inplace (bool): If True, perform the operation in-place. Defaults to False. - activation (str): The activation function to apply after the first MoE layer. - global_num_experts (int): The total number of experts in the global expert space. - expert_map (Optional[torch.Tensor]): A tensor mapping expert indices from the global expert space to the local expert space of the expert parallel shard. - a1_scale (Optional[torch.Tensor]): The optional fp32 scale to quantize a. Shape: scalar or [M] - a2_scale (Optional[torch.Tensor]): The optional fp32 scale to quantize the intermediate result between the gemms. Shape: scalar or [M] Returns: - torch.Tensor: The bfloat16 output tensor after applying the MoE layer. """ quant_config = fp8_w8a8_moe_quant_config( w1_scale=w1_scale, w2_scale=w2_scale, a1_scale=a1_scale, a2_scale=a2_scale, block_shape=get_mk_alignment_for_contiguous_layout(), ) fn = mk.FusedMoEModularKernel( MoEPrepareAndFinalizeNoEP(), DeepGemmExperts(quant_config), ) return fn( hidden_states, w1, w2, topk_weights, topk_ids, inplace=inplace, activation=activation, global_num_experts=global_num_experts, expert_map=expert_map, apply_router_weight_on_input=apply_router_weight_on_input, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/batched_deep_gemm_moe.py
vllm/model_executor/layers/fused_moe/batched_deep_gemm_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm.forward_context import get_forward_context, is_forward_context_available from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe.config import FusedMoEQuantConfig from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import ( TopKWeightAndReduceDelegate, ) from vllm.model_executor.layers.fused_moe.utils import _resize_cache from vllm.platforms import current_platform from vllm.triton_utils import tl, triton from vllm.utils.deep_gemm import ( DeepGemmQuantScaleFMT, fp8_m_grouped_gemm_nt_masked, get_mk_alignment_for_contiguous_layout, is_deep_gemm_e8m0_used, ) from vllm.utils.math_utils import cdiv, round_up logger = init_logger(__name__) def scales_shape_stride_dtype( E: int, T: int, G: int, quant_scale_fmt: DeepGemmQuantScaleFMT ) -> tuple[tuple[int, ...], tuple[int, ...], torch.dtype]: shape = (E, T, G) strides = (T * G, 1, T) if quant_scale_fmt in [ DeepGemmQuantScaleFMT.FLOAT32, DeepGemmQuantScaleFMT.FLOAT32_CEIL_UE8M0, ]: return shape, strides, torch.float32 assert quant_scale_fmt == DeepGemmQuantScaleFMT.UE8M0 shape = (E, T, cdiv(G, 4)) strides = (T * cdiv(G, 4), 1, T) return shape, strides, torch.int32 @triton.jit def _silu_mul_fp8_quant_deep_gemm( # Pointers ------------------------------------------------------------ input_ptr, # 16-bit activations (E, T, 2*H) y_q_ptr, # fp8 quantized activations (E, T, H) y_s_ptr, # 16-bit scales (E, T, G) counts_ptr, # int32 num tokens per expert (E) # Sizes --------------------------------------------------------------- H: tl.constexpr, # hidden dimension (per output) GROUP_SIZE: tl.constexpr, # elements per group (usually 128) # Strides for input (elements) --------------------------------------- stride_i_e, stride_i_t, stride_i_h, # Strides for y_q (elements) ----------------------------------------- stride_yq_e, stride_yq_t, stride_yq_h, # Strides for y_s (elements) ----------------------------------------- stride_ys_e, stride_ys_t, stride_ys_g, # Stride for counts (elements) stride_counts_e, # Numeric params ------------------------------------------------------ eps: tl.constexpr, fp8_min: tl.constexpr, fp8_max: tl.constexpr, ceil_ue8m0: tl.constexpr, # Meta --------------------------------------------------------------- BLOCK: tl.constexpr, NUM_STAGES: tl.constexpr, ): G = H // GROUP_SIZE # map program id -> (e, g) pid = tl.program_id(0) e = pid // G g = pid % G e = e.to(tl.int64) g = g.to(tl.int64) # number of valid tokens for this expert n_tokens = tl.load(counts_ptr + e * stride_counts_e).to(tl.int64) cols = tl.arange(0, BLOCK).to(tl.int64) mask = cols < BLOCK base_input_offset = e * stride_i_e + g * GROUP_SIZE * stride_i_h base_gate_offset = base_input_offset + cols * stride_i_h base_up_offset = base_input_offset + H * stride_i_h + cols * stride_i_h base_yq_offset = e * stride_yq_e + g * GROUP_SIZE * stride_yq_h + cols * stride_yq_h base_ys_offset = e * stride_ys_e + g * stride_ys_g for t in tl.range(0, n_tokens, num_stages=NUM_STAGES): gate = tl.load( input_ptr + base_gate_offset + t * stride_i_t, mask=mask, other=0.0 ).to(tl.float32) up = tl.load(input_ptr + base_up_offset + t * stride_i_t, mask=mask, other=0.0) gate = gate * (1.0 / (1.0 + tl.exp(-gate))) y = gate * up y_s = tl.maximum(tl.max(tl.abs(y)), eps) / fp8_max if ceil_ue8m0: y_s = tl.exp2(tl.ceil(tl.log2(y_s))) y_q = tl.clamp(y / y_s, fp8_min, fp8_max).to(y_q_ptr.dtype.element_ty) tl.store(y_q_ptr + base_yq_offset + t * stride_yq_t, y_q, mask=mask) tl.store(y_s_ptr + base_ys_offset + t * stride_ys_t, y_s) def persistent_masked_m_silu_mul_quant( y: torch.Tensor, # (E, T, 2*H) tokens_per_expert: torch.Tensor, # (E,) number of valid tokens per expert num_parallel_tokens=16, group_size: int = 128, quant_scale_fmt: DeepGemmQuantScaleFMT = DeepGemmQuantScaleFMT.FLOAT32, ) -> tuple[torch.Tensor, torch.Tensor]: """Quantize silu(y[..., :H]) * y[..., H:] to FP8 with group per-token scales y has shape (E, T, 2*H). The first half of the last dimension is silu-activated, multiplied by the second half, then quantized into FP8. We launch a fixed grid of threads to accommodate CUDA graphs. Let `P2` be a parallelization factor for persistent_masked_m_silu_mul_quant over the hidden dimension. Let `expert_offsets = [0] + [num_tokens.cumsum()]` and `total_tokens = expert_offsets[-1]`. persistent_masked_m_silu_mul_quant launches `total_tokens x P2` number of thread blocks. Each thread block contains `NUM_WARPS` warps. Every thread block needs to find it's corresponding expert by warp-parallel scanning over the `expert_offsets` array. The i-th warp in the first thread block processes `[i * warp_chunk_size, (i + 1) * warp_chunk_size]` groups sequentially, where `warp_chunk_size = ((H / GROUP_SIZE) / P2) / NUM_WARPS`, pipelining loads and computes. The shared memory layout for 4 warps with a 2-stage pipeline for SiLU V2 can is visualized like so: stage0 stage1 ┌─────┬───┬─────┬───┬─────┬───┬─────┬───┬─────┬───┬─────┬───┬─────┬───┬─────┬───┐ │gate0│up0│gate1│up1│gate2│up2│gate3│up3│gate0│up0│gate1│up1│gate2│up2│gate3│up3│ └─────┴───┴─────┴───┴─────┴───┴─────┴───┴─────┴───┴─────┴───┴─────┴───┴─────┴───┘ with the main difference between V1 and V2 being the global load stride between warps, and between half-warps. Regarding the latter stride, we assign the first half warp of every warp for `gate` loads and the second half-warp to `up` loads. Returns `(y_q, y_s)` where * `y_q`: FP8 tensor, shape (E, T, H), same layout as y[..., :H] * `y_s` depends on quant_scale_fmt, - quant_scale_fmt == FLOAT32, `y_s`: FP32 tensor, shape (E, T, H // group_size), strides (T*G, 1, T) - quant_scale_fmt == E8M0, `y_s`: Int32 tensor, shape (E, T, H // group_size // 4), strides (T*G, 1, T) - quant_scale_fmt == E8M0_FLOAT32_SPARSE `y_s`: FP32 tensor, shape (E, T, H // group_size), strides (T*G, 1, T) Let NUM_WARPS be the number of warps in a single thread block and `GROUP_SIZE = 128` be the size of the quantization group. """ assert y.ndim == 3, "y must be (E, T, 2*H)" E, T, H2 = y.shape assert H2 % 2 == 0, "last dim of y must be even (2*H)" H = H2 // 2 G = (H + group_size - 1) // group_size assert H % 8 == 0, "H must be divisible by 8" assert group_size == 128, "H must be divisible by 8" assert tokens_per_expert.ndim == 1 and tokens_per_expert.shape[0] == E tokens_per_expert = tokens_per_expert.to(device=y.device, dtype=torch.int32) fp8_dtype = torch.float8_e4m3fn y_q = torch.empty((E, T, H), dtype=fp8_dtype, device=y.device) ys_shape, ys_strides, ys_dtype = scales_shape_stride_dtype(E, T, G, quant_scale_fmt) y_s = torch.empty_strided( ys_shape, ys_strides, dtype=ys_dtype, device=y.device, ) ceil_ue8m0 = quant_scale_fmt in [ DeepGemmQuantScaleFMT.FLOAT32_CEIL_UE8M0, DeepGemmQuantScaleFMT.UE8M0, ] cuda_arch = current_platform.get_device_capability( device_id=y.device.index ).to_int() if cuda_arch >= 80: torch.ops._C.persistent_masked_m_silu_mul_quant( y, tokens_per_expert, y_q, y_s, ceil_ue8m0 ) else: stride_cnt_e = tokens_per_expert.stride()[0] # Static grid over experts and H-groups. # A loop inside the kernel handles the token dim grid = (E * G,) # strides (elements) stride_i_e, stride_i_t, stride_i_h = y.stride() stride_yq_e, stride_yq_t, stride_yq_h = y_q.stride() f_info = torch.finfo(fp8_dtype) fp8_max = f_info.max fp8_min = f_info.min eps: float = 1e-10 assert y_s.dtype == torch.float32, ( "_silu_mul_fp8_quant_deep_gemm does" "not support {y_s.dtype} scales. Only torch.float32 supported." ) _silu_mul_fp8_quant_deep_gemm[grid]( y, y_q, y_s, tokens_per_expert, H, group_size, stride_i_e, stride_i_t, stride_i_h, stride_yq_e, stride_yq_t, stride_yq_h, ys_strides[0], ys_strides[1], ys_strides[2], stride_cnt_e, eps, fp8_min, fp8_max, ceil_ue8m0, BLOCK=group_size, NUM_STAGES=4, num_warps=1, ) return y_q, y_s class BatchedDeepGemmExperts(mk.FusedMoEPermuteExpertsUnpermute): def __init__( self, max_num_tokens: int, num_dispatchers: int, quant_config: FusedMoEQuantConfig, ): """ max_num_tokens: Maximum number of tokens from a DP Rank num_dispatchers: The number of DP dispatchers. quant_config: Quantization configuration """ super().__init__(quant_config) assert self.block_shape == get_mk_alignment_for_contiguous_layout() assert self.quant_config.use_fp8_w8a8 self.max_num_tokens = max_num_tokens self.num_dispatchers = num_dispatchers @property def activation_formats( self, ) -> tuple[mk.FusedMoEActivationFormat, mk.FusedMoEActivationFormat]: return ( mk.FusedMoEActivationFormat.BatchedExperts, mk.FusedMoEActivationFormat.BatchedExperts, ) def supports_chunking(self) -> bool: return False def supports_expert_map(self) -> bool: return False def supports_packed_ue8m0_act_scales(self) -> bool: """ DeepGemm supports packed ue8m0 activation scales format in devices == sm100 """ return ( is_deep_gemm_e8m0_used() and current_platform.is_device_capability_family(100) ) def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce: # Let PrepareAndFinalize::finalize() decide the impl. return TopKWeightAndReduceDelegate() def workspace_shapes( self, M: int, N: int, K: int, topk: int, global_num_experts: int, local_num_experts: int, expert_tokens_meta: mk.ExpertTokensMetadata | None, ) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...]]: # FIXME (varun): We should be able to dispatch only from the leader # DP ranks in the case of TP > 1. At the moment, all the Ranks # end up sending their tokens. This needs to be fixed. num_dispatchers = self.num_dispatchers num_experts = local_num_experts max_num_tokens = M if self.max_num_tokens is None else self.max_num_tokens workspace13 = (num_experts, max_num_tokens * num_dispatchers, max(K, N)) workspace2 = (num_experts, max_num_tokens * num_dispatchers, (N // 2)) output = (num_experts, max_num_tokens * num_dispatchers, K) return (workspace13, workspace2, output) def estimate_expected_m( self, global_num_experts: int, max_tokens_per_expert: int, topk: int ) -> int: dp_meta = ( get_forward_context().dp_metadata if is_forward_context_available() else None ) if dp_meta is None: logger.warning_once( "DPMetadata unavailable. Defaulting expected_m to " f"{max_tokens_per_expert}.", scope="local", ) return max_tokens_per_expert total_num_tokens = dp_meta.num_tokens_across_dp_cpu.sum().item() total_num_tokens_replicated = total_num_tokens * topk # Assume even load balancing assert global_num_experts != 0 estimate = round_up(int(total_num_tokens_replicated // global_num_experts), 16) # clamp estimate estimate = max(estimate, 16) estimate = min(max_tokens_per_expert, estimate) return estimate def apply( self, output: torch.Tensor, hidden_states: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: str, global_num_experts: int, expert_map: torch.Tensor | None, a1q_scale: torch.Tensor | None, a2_scale: torch.Tensor | None, workspace13: torch.Tensor, workspace2: torch.Tensor, expert_tokens_meta: mk.ExpertTokensMetadata | None, apply_router_weight_on_input: bool, ): assert expert_tokens_meta is not None expert_num_tokens = expert_tokens_meta.expert_num_tokens assert hidden_states.ndim == 3 assert self.block_shape is not None a1q = hidden_states _, N, K = w1.size() assert w2.size(1) == K E, max_num_tokens, N, K, _ = self.moe_problem_size( hidden_states, w1, w2, topk_ids ) workspace1 = _resize_cache(workspace13, (E, max_num_tokens, N)) expected_m = self.estimate_expected_m( global_num_experts=global_num_experts, max_tokens_per_expert=max_num_tokens, topk=topk_ids.size(-1), ) fp8_m_grouped_gemm_nt_masked( (a1q, a1q_scale), (w1, self.w1_scale), workspace1, expert_num_tokens, expected_m, ) quant_scale_fmt = DeepGemmQuantScaleFMT.from_oracle() a2q, a2q_scale = persistent_masked_m_silu_mul_quant( workspace1, expert_num_tokens, quant_scale_fmt=quant_scale_fmt, ) fp8_m_grouped_gemm_nt_masked( (a2q, a2q_scale), (w2, self.w2_scale), output, expert_num_tokens, expected_m, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/rocm_aiter_fused_moe.py
vllm/model_executor/layers/fused_moe/rocm_aiter_fused_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from enum import IntEnum from functools import lru_cache import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm._aiter_ops import rocm_aiter_ops from vllm.model_executor.layers.fused_moe.config import ( FUSED_MOE_UNQUANTIZED_CONFIG, FusedMoEQuantConfig, ) from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import ( TopKWeightAndReduceNoOP, ) class QuantMethod(IntEnum): # This allows interfacing with AITER QuantType Enum # without importing the QuantType from AITER globally. # Note that these quantization methods are # supported in AITER package. However, # not all are used in this module. NO = 0 # a16w16 PER_TENSOR = 1 # w8a8 (pre_Tensor) PER_TOKEN = 2 # w8a8/w8a4 (per_Token) BLOCK_1X32 = 3 # fp4x2 BLOCK_1X128 = 4 # block quantized w8a8 (per_1x128) BLOCK_128x128 = 5 # block quantized w8a8 (per_128x128) class ActivationMethod(IntEnum): # This allows interfacing with AITER ActivationType enum # without importing the ActivationType enum from AITER globally. SILU = 0 GELU = 1 aiter_topK_meta_data = None @lru_cache(maxsize=1) def init_aiter_topK_meta_data( n_routed_experts: int, n_shared_experts: int, top_k: int, tp_rank: int, tp_size: int, shared_experts_score: float = 1.0, max_num_tokens: int = 32768, is_EP: bool = False, ): global aiter_topK_meta_data fake_expertid = n_routed_experts + n_shared_experts # all layers reuse same buffer # This extra element when EP is enabled is used as a sentinel # to mask out shared expert processing for tokens not owned by # the current EP rank. This is necessary to avoid double-processing # of shared experts. total_topk_ids = torch.empty( (max_num_tokens, top_k + n_shared_experts + is_EP), dtype=torch.int32, device="cuda", ) ns_topk_ids, s_topk_ids = total_topk_ids.split( [top_k, n_shared_experts + is_EP], dim=1 ) shared_expert_ids = [n_routed_experts + i for i in range(n_shared_experts + is_EP)] if is_EP: s_topk_ids_list = [ [fake_expertid] * (n_shared_experts + is_EP) ] * max_num_tokens for i in range(tp_rank, max_num_tokens, tp_size): s_topk_ids_list[i] = shared_expert_ids else: s_topk_ids_list = [ list(range(n_routed_experts, fake_expertid)) ] * max_num_tokens s_topk_ids[:] = torch.tensor(s_topk_ids_list, dtype=torch.int32, device="cuda") total_topk_weights = torch.empty( (max_num_tokens, top_k + n_shared_experts + is_EP), dtype=torch.float32, device="cuda", ) ns_topk_weights, s_topk_weights = total_topk_weights.split( [top_k, n_shared_experts + is_EP], dim=1 ) s_topk_weights.fill_(shared_experts_score) assert aiter_topK_meta_data is None, "AITER topK meta data is already initialized" aiter_topK_meta_data = (total_topk_weights, total_topk_ids) def rocm_aiter_grouped_topk( hidden_states: torch.Tensor, gating_output: torch.Tensor, topk: int, renormalize: bool, num_expert_group: int = 0, topk_group: int = 0, scoring_func: str = "softmax", routed_scaling_factor: float = 1.0, e_score_correction_bias: torch.Tensor | None = None, num_fused_shared_experts: int = 0, ) -> tuple[torch.Tensor, torch.Tensor]: token = hidden_states.shape[0] device = hidden_states.device if ( rocm_aiter_ops.is_fusion_moe_shared_experts_enabled() and num_fused_shared_experts > 0 ): assert aiter_topK_meta_data is not None, ( "AITER topK meta data is not initialized. " "Please ensure that init_aiter_topK_meta_data " "is called before this function." ) total_topk_weights, total_topk_ids = aiter_topK_meta_data assert total_topk_weights.shape[0] >= token, ( f"AITER topK meta data support {total_topk_weights.shape[0]} " f"tokens which is determined by max_num_batched_tokens, " f"but got {token} tokens now." ) total_topk_weights = total_topk_weights[:token] total_topk_ids = total_topk_ids[:token] topk_weights, _ = total_topk_weights.split( [topk, total_topk_weights.shape[1] - topk], dim=1 ) topk_ids, _ = total_topk_ids.split( [topk, total_topk_ids.shape[1] - topk], dim=1 ) else: topk_ids = torch.empty((token, topk), dtype=torch.int32, device=device) topk_weights = torch.empty((token, topk), dtype=torch.float32, device=device) if e_score_correction_bias is not None: rocm_aiter_ops.biased_grouped_topk( gating_output, e_score_correction_bias.to(gating_output.dtype), topk_weights, topk_ids, num_expert_group, topk_group, renormalize, routed_scaling_factor=routed_scaling_factor, ) else: assert scoring_func == "softmax" or scoring_func == "sigmoid" rocm_aiter_ops.grouped_topk( gating_output, topk_weights, topk_ids, num_expert_group, topk_group, renormalize, scoring_func, routed_scaling_factor=routed_scaling_factor, ) if ( rocm_aiter_ops.is_fusion_moe_shared_experts_enabled() and num_fused_shared_experts > 0 ): return total_topk_weights, total_topk_ids return topk_weights, topk_ids def rocm_aiter_fused_experts( hidden_states: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: str = "silu", apply_router_weight_on_input: bool = False, expert_map: torch.Tensor | None = None, quant_config: FusedMoEQuantConfig | None = None, ) -> torch.Tensor: if quant_config is None: quant_config = FUSED_MOE_UNQUANTIZED_CONFIG activation_method = ( ActivationMethod.SILU if activation == "silu" else ActivationMethod.GELU ) # All AITER Fused MoE kernels are expecting the following datatypes topk_weights = topk_weights.to(torch.float32) topk_ids = topk_ids.to(torch.int32) expert_mask = expert_map if expert_map is not None else None # w8a8 per-channel quantization if ( quant_config.per_act_token_quant and apply_router_weight_on_input and quant_config.use_fp8_w8a8 ): # AITER tkw1 kernel for FP8 models with `apply_router_weight_on_input` # This applies topk_weights on the GEMM output of the first FC layer # rather than the second FC. assert topk_weights.dim() == 2, ( "`topk_weights` should be in shape (num_tokens, topk)" ) assert topk_weights.shape[-1] == 1, ( "Only support topk=1 when `apply_router_weight_on_input` is True" ) return rocm_aiter_ops.asm_moe_tkw1( hidden_states, w1, w2, topk_weights, topk_ids, fc1_scale=quant_config.w1_scale, fc2_scale=quant_config.w2_scale, fc1_smooth_scale=None, fc2_smooth_scale=None, a16=False, per_tensor_quant_scale=None, expert_mask=expert_mask, activation_method=activation_method, ) else: quant_method = QuantMethod.NO.value # quark moe for mxfp4 w_dtype mxfp4 a_dtype if quant_config.use_mxfp4_w4a4: quant_method = QuantMethod.BLOCK_1X32.value # w8a8 block-scaled if quant_config.block_shape is not None and quant_config.use_fp8_w8a8: assert not apply_router_weight_on_input, ( "apply_router_weight_on_input is\ not supported for block scaled moe" ) assert quant_config.w1_scale is not None assert quant_config.w2_scale is not None quant_method = QuantMethod.BLOCK_128x128.value elif quant_config.use_fp8_w8a8 and quant_config.per_out_ch_quant: quant_method = QuantMethod.PER_TOKEN.value elif quant_config.use_fp8_w8a8: # Currently only per tensor quantization method is enabled. quant_method = QuantMethod.PER_TENSOR.value if apply_router_weight_on_input: assert topk_weights.dim() == 2, ( "`topk_weights` should be in shape (num_tokens, topk)" ) _, topk = topk_weights.shape assert topk == 1, ( "Only support topk=1 when `apply_router_weight_on_input` is True" ) return rocm_aiter_ops.fused_moe( hidden_states, w1, w2, topk_weights, topk_ids, expert_mask=expert_mask, quant_method=quant_method, activation_method=activation_method, w1_scale=quant_config.w1_scale, w2_scale=quant_config.w2_scale, a1_scale=quant_config.a1_scale, a2_scale=quant_config.a2_scale, doweight_stage1=apply_router_weight_on_input, ) class AiterExperts(mk.FusedMoEPermuteExpertsUnpermute): def __init__(self, quant_config): super().__init__(quant_config) @property def activation_formats( self, ) -> tuple[mk.FusedMoEActivationFormat, mk.FusedMoEActivationFormat]: return ( mk.FusedMoEActivationFormat.Standard, mk.FusedMoEActivationFormat.Standard, ) def supports_expert_map(self): return True def supports_chunking(self): return False def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce: return TopKWeightAndReduceNoOP() def workspace_shapes( self, M: int, N: int, K: int, topk: int, global_num_experts: int, local_num_experts: int, expert_tokens_meta: mk.ExpertTokensMetadata | None, ) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...]]: # Workspaces are managed internally by AITER. workspace1 = (0,) workspace2 = (0,) output = (M, K) return (workspace1, workspace2, output) def apply( self, output: torch.Tensor, hidden_states: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: str, global_num_experts: int, expert_map: torch.Tensor | None, a1q_scale: torch.Tensor | None, a2_scale: torch.Tensor | None, workspace13: torch.Tensor, workspace2: torch.Tensor, expert_tokens_meta: mk.ExpertTokensMetadata | None, apply_router_weight_on_input: bool, ): # TODO(rob): rocm_aiter_fused_experts uses self.quant_config's # a_scales for static quantization. Update this to fit better # with the interface once all quant integrations are complete. assert a1q_scale is None assert a2_scale == self.quant_config.a2_scale assert expert_tokens_meta is None result = rocm_aiter_fused_experts( hidden_states=hidden_states, w1=w1, w2=w2, topk_weights=topk_weights, topk_ids=topk_ids, activation=activation, apply_router_weight_on_input=apply_router_weight_on_input, expert_map=expert_map, quant_config=self.quant_config, ) assert result.shape == output.shape output.copy_(result)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/cpu_fused_moe.py
vllm/model_executor/layers/fused_moe/cpu_fused_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import weakref from collections.abc import Callable import torch from torch.nn import functional as F from vllm import _custom_ops as ops from vllm._custom_ops import cpu_fused_moe, cpu_prepack_moe_weight from vllm.model_executor.layers.activation import SiluAndMul, SwigluOAIAndMul from vllm.model_executor.layers.quantization.utils.layer_utils import replace_parameter from vllm.utils.torch_utils import direct_register_custom_op _CPU_MOE_LAYER_CACHE = {} _CPU_MOE_ACT = { "silu": SiluAndMul(), "swigluoai": SwigluOAIAndMul(), } def grouped_topk( hidden_states: torch.Tensor, gating_output: torch.Tensor, topk: int, renormalize: bool, num_expert_group: int = 0, topk_group: int = 0, scoring_func: str = "softmax", routed_scaling_factor: float = 1.0, e_score_correction_bias: torch.Tensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: assert hidden_states.shape[0] == gating_output.shape[0], "Number of tokens mismatch" gating_output = gating_output.float() if scoring_func == "softmax": scores = torch.softmax(gating_output, dim=-1) elif scoring_func == "sigmoid": scores = gating_output.sigmoid() else: raise ValueError(f"Unsupported scoring function: {scoring_func}") num_token = scores.shape[0] if e_score_correction_bias is not None: original_scores = scores scores = scores + e_score_correction_bias.unsqueeze(0) group_scores = ( scores.view(num_token, num_expert_group, -1).topk(2, dim=-1)[0].sum(dim=-1) ) else: group_scores = ( scores.view(num_token, num_expert_group, -1).max(dim=-1).values ) # [n, n_group] group_idx = torch.topk(group_scores, k=topk_group, dim=-1, sorted=False)[ 1 ] # [n, top_k_group] group_mask = torch.zeros_like(group_scores) # [n, n_group] group_mask.scatter_(1, group_idx, 1) # [n, n_group] score_mask = ( group_mask.unsqueeze(-1) .expand(num_token, num_expert_group, scores.shape[-1] // num_expert_group) .reshape(num_token, -1) ) # [n, e] tmp_scores = scores.masked_fill(~score_mask.bool(), float("-inf")) # [n, e] if e_score_correction_bias is not None: topk_ids = torch.topk(tmp_scores, k=topk, dim=-1, sorted=False)[1] topk_weights = original_scores.gather(1, topk_ids) else: topk_weights, topk_ids = torch.topk(tmp_scores, k=topk, dim=-1, sorted=False) if renormalize: topk_weights = topk_weights / topk_weights.sum(dim=-1, keepdim=True) if routed_scaling_factor != 1.0: topk_weights = topk_weights * routed_scaling_factor return topk_weights, topk_ids.to(torch.int32) def select_experts( hidden_states: torch.Tensor, router_logits: torch.Tensor, top_k: int, use_grouped_topk: bool, renormalize: bool, topk_group: int | None = None, num_expert_group: int | None = None, custom_routing_function: Callable | None = None, scoring_func: str = "softmax", routed_scaling_factor: float = 1.0, e_score_correction_bias: torch.Tensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: if use_grouped_topk: assert topk_group is not None assert num_expert_group is not None return grouped_topk( hidden_states=hidden_states, gating_output=router_logits, topk=top_k, renormalize=renormalize, num_expert_group=num_expert_group, topk_group=topk_group, scoring_func=scoring_func, routed_scaling_factor=routed_scaling_factor, e_score_correction_bias=e_score_correction_bias, ) elif custom_routing_function is None: assert scoring_func == "softmax" topk_logit_vals, topk_idx = torch.topk( router_logits, k=top_k, dim=-1, sorted=False ) if renormalize: topk_vals = torch.softmax(topk_logit_vals, dim=-1) else: logZ = torch.logsumexp(router_logits, dim=-1, keepdim=True) topk_vals = (topk_logit_vals - logZ).exp() return topk_vals.to(torch.float32), topk_idx.to(torch.int32) else: return custom_routing_function( hidden_states=hidden_states, gating_output=router_logits, topk=top_k, renormalize=renormalize, ) class SGLFusedMOE: def __init__(self, layer: torch.nn.Module) -> None: pass def __call__( self, layer: torch.nn.Module, x: torch.Tensor, use_grouped_topk: bool, top_k: int, router_logits: torch.Tensor, renormalize: bool, topk_group: int | None = None, num_expert_group: int | None = None, global_num_experts: int = -1, expert_map: torch.Tensor | None = None, custom_routing_function: Callable | None = None, scoring_func: str = "softmax", routed_scaling_factor: float = 1.0, e_score_correction_bias: torch.Tensor | None = None, apply_router_weight_on_input: bool = False, activation: str = "silu", ) -> torch.Tensor: assert activation == "silu", f"{activation} is not supported." assert not apply_router_weight_on_input topk_weights, topk_ids = select_experts( hidden_states=x, router_logits=router_logits, use_grouped_topk=use_grouped_topk, top_k=top_k, renormalize=renormalize, topk_group=topk_group, num_expert_group=num_expert_group, custom_routing_function=custom_routing_function, scoring_func=scoring_func, routed_scaling_factor=routed_scaling_factor, e_score_correction_bias=e_score_correction_bias, ) torch.ops._C.fused_experts_cpu( x, layer.w13_weight, layer.w2_weight, topk_weights, topk_ids, True, False, False, None, None, None, None, None, True, ) return x class CPUFusedMOE: def __init__(self, layer: torch.nn.Module) -> None: use_grouped_gemm, isa = self.check_grouped_gemm(layer) self.isa = isa if use_grouped_gemm: self.forward_method = self.forward_grouped_gemm self.init_moe_grouped_gemm(layer=layer) else: self.forward_method = self.forward_torch self.init_moe_torch(layer=layer) def __call__( self, layer: torch.nn.Module, x: torch.Tensor, use_grouped_topk: bool, top_k: int, router_logits: torch.Tensor, renormalize: bool, topk_group: int | None = None, num_expert_group: int | None = None, global_num_experts: int = -1, expert_map: torch.Tensor | None = None, custom_routing_function: Callable | None = None, scoring_func: str = "softmax", routed_scaling_factor: float = 1.0, e_score_correction_bias: torch.Tensor | None = None, apply_router_weight_on_input: bool = False, activation: str = "silu", ) -> torch.Tensor: assert activation in _CPU_MOE_ACT, f"{activation} is not supported." assert not apply_router_weight_on_input topk_weights, topk_ids = select_experts( hidden_states=x, router_logits=router_logits, use_grouped_topk=use_grouped_topk, top_k=top_k, renormalize=renormalize, topk_group=topk_group, num_expert_group=num_expert_group, custom_routing_function=custom_routing_function, scoring_func=scoring_func, routed_scaling_factor=routed_scaling_factor, e_score_correction_bias=e_score_correction_bias, ) return self.forward_method( layer, x, topk_weights, topk_ids, activation, global_num_experts, ) def check_grouped_gemm( self, layer: torch.nn.Module, ) -> tuple[bool, str]: if not hasattr(torch.ops._C, "prepack_moe_weight"): return False, "none" dtype = layer.w13_weight.dtype w13_input_size = layer.w13_weight.size(2) w13_output_size = layer.w13_weight.size(1) w2_input_size = layer.w2_weight.size(2) w2_output_size = layer.w2_weight.size(1) if not (w13_output_size % 32 == 0 and w2_output_size % 32 == 0): return False, "none" supports_amx = torch._C._cpu._is_amx_tile_supported() if ( supports_amx and dtype == torch.bfloat16 and w13_input_size % 32 == 0 and w2_input_size % 32 == 0 ): return True, "amx" if supports_amx: return False, "none" return True, "vec" def init_moe_grouped_gemm( self, layer: torch.nn.Module, ) -> None: new_w13 = cpu_prepack_moe_weight(layer.w13_weight, self.isa) replace_parameter(layer, "w13_weight", new_w13) new_w2 = cpu_prepack_moe_weight(layer.w2_weight, self.isa) replace_parameter(layer, "w2_weight", new_w2) def init_moe_torch( self, layer: torch.nn.Module, ) -> None: use_onednn_mm = ops._supports_onednn and ops.is_onednn_acl_supported() num_experts = layer.w13_weight.size(0) has_w13_bias = hasattr(layer, "w13_bias") has_w2_bias = hasattr(layer, "w2_bias") layer.gate_up_linear = [] layer.down_linear = [] for i in range(num_experts): layer_w13_weight = layer.w13_weight[i] layer_w13_bias = layer.w13_bias[i] if has_w13_bias else None layer_w2_weight = layer.w2_weight[i] layer_w2_bias = layer.w2_bias[i] if has_w2_bias else None if use_onednn_mm: gate_up_handle = ops.create_onednn_mm(layer_w13_weight.t(), 32) layer.gate_up_linear.append( lambda x, handle=gate_up_handle, bias=layer_w13_bias: ops.onednn_mm( handle, x, bias ) ) down_handle = ops.create_onednn_mm(layer_w2_weight.t(), 32) layer.down_linear.append( lambda x, handle=down_handle, bias=layer_w2_bias: ops.onednn_mm( handle, x, bias ) ) else: layer.gate_up_linear.append( lambda x, w=layer_w13_weight, b=layer_w13_bias: F.linear(x, w, b) ) layer.down_linear.append( lambda x, w=layer_w2_weight, b=layer_w2_bias: F.linear(x, w, b) ) if use_onednn_mm: # remove weight layer.w13_weight = torch.nn.Parameter(torch.empty(0), requires_grad=False) layer.w2_weight = torch.nn.Parameter(torch.empty(0), requires_grad=False) _CPU_MOE_LAYER_CACHE[id(layer)] = weakref.ref(layer) def forward_grouped_gemm( self, layer: torch.nn.Module, input: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: str, global_num_experts: int = -1, ) -> torch.Tensor: output = cpu_fused_moe( input, layer.w13_weight, layer.w2_weight, getattr(layer, "w13_bias", None), getattr(layer, "w2_bias", None), topk_weights, topk_ids, activation, self.isa, ) return output def forward_torch( self, layer: torch.nn.Module, input: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: str, global_num_experts: int = -1, ) -> torch.Tensor: output = torch.empty_like(input) layer_id = id(layer) torch.ops.vllm.cpu_fused_moe_torch( layer_id, output, input, topk_weights, topk_ids, activation, global_num_experts, ) return output def cpu_fused_moe_torch( layer_id: int, output: torch.Tensor, input: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: str, global_num_experts: int = -1, ) -> None: layer = _CPU_MOE_LAYER_CACHE[layer_id]() # Ref code from https://github.com/sgl-project/sglang/blob/716e682721397df103f347d22da8bd46c6016dab/python/sglang/srt/layers/moe/fused_moe_native.py#L53 len_experts = global_num_experts cnts = topk_ids.new_zeros((topk_ids.shape[0], len_experts)) cnts.scatter_(1, topk_ids.to(torch.int64), 1) tokens_per_expert = cnts.sum(dim=0) idxs = topk_ids.view(-1).argsort() sorted_tokens = input[idxs // topk_ids.shape[1]] tokens_per_expert = tokens_per_expert.cpu().numpy() outputs = [] start_idx = 0 for i, num_tokens in enumerate(tokens_per_expert): end_idx = start_idx + num_tokens if num_tokens == 0: continue tokens_for_this_expert = sorted_tokens[start_idx:end_idx] gate_up = layer.gate_up_linear[i](tokens_for_this_expert) # type: ignore gate_up = _CPU_MOE_ACT[activation].forward_native(gate_up) expert_out = layer.down_linear[i](gate_up) # type: ignore outputs.append(expert_out) start_idx = end_idx outs = torch.cat(outputs, dim=0) if len(outputs) else sorted_tokens.new_empty(0) new_x = torch.empty_like(outs) new_x[idxs] = outs final_out = ( new_x.view(*topk_ids.shape, -1) .type(topk_weights.dtype) .mul_(topk_weights.unsqueeze(dim=-1)) .sum(dim=1) .type(new_x.dtype) ) output.copy_(final_out) direct_register_custom_op( op_name="cpu_fused_moe_torch", op_func=cpu_fused_moe_torch, mutates_args=["output"], )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/topk_weight_and_reduce.py
vllm/model_executor/layers/fused_moe/topk_weight_and_reduce.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch import vllm._custom_ops as ops import vllm.model_executor.layers.fused_moe.modular_kernel as mk class TopKWeightAndReduceDelegate(mk.TopKWeightAndReduce): """ Useful in the case when some FusedMoEPermuteExpertsUnpermute implementation does not perform weight application and reduction but cannot address the needs of all the compatible PrepareAndFinalize implementations. For example, BatchedTritonExperts is compatible with both PplxPrepareAndFinalize and BatchedPrepareAndFinalize. PplxPrepareAndFinalize does the weight-application + reduction as part of the pplx combine kernel. But the BatchedPrepareAndFinalize needs an implementation. To facilitate this case, the BatchedTritonExperts could use TopKWeightAndReduceDelegate so the PrepareAndFinalize implementations could choose how to weight + reduce. """ def __eq__(self, other): return isinstance(other, TopKWeightAndReduceDelegate) def apply( self, output: torch.Tensor | None, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, ) -> torch.Tensor: raise RuntimeError( "The caller is expected to choose an appropriate " "TopKWeightAndReduce implementation." ) class TopKWeightAndReduceNoOP(mk.TopKWeightAndReduce): """ The fused_experts outputs have already been weight applied and reduced. This implementation is a no-op. """ def __eq__(self, other): return isinstance(other, TopKWeightAndReduceNoOP) def apply( self, output: torch.Tensor | None, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, ) -> torch.Tensor: # Weight application and reduction operations are already done. if output is None: return fused_expert_output # MoEPrepareAndFinalizeNoEP needs the output to be in the `output` # tensor. assert output.size() == fused_expert_output.size(), ( "output shape is expected to match the fused_expert_output shape. " f"But got output={output.size()}, " f"used_expert_output={fused_expert_output.size()}" ) output.copy_(fused_expert_output, non_blocking=True) return output class TopKWeightAndReduceContiguous(mk.TopKWeightAndReduce): """ TopKWeightAndReduce implementation for a fused_experts output of shape (m, topk, K) """ def __eq__(self, other): return isinstance(other, TopKWeightAndReduceContiguous) def apply( self, output: torch.Tensor | None, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, ) -> torch.Tensor: m, num_topk = topk_ids.size() k = fused_expert_output.size(-1) if fused_expert_output.ndim == 2: fused_expert_output = fused_expert_output.view(m, num_topk, k) assert fused_expert_output.size() == (m, num_topk, k), ( f"Expected fused_expert_output size {(m, num_topk, k)}. But got " f"{fused_expert_output.size()}" ) if not apply_router_weight_on_input: fused_expert_output.mul_(topk_weights.view(m, -1, 1)) if output is None: output = torch.empty( (m, k), device=fused_expert_output.device, dtype=fused_expert_output.dtype, ) assert output.size() == (m, k), ( f"Expected output size {(m, k)}. But got {output.size()}" ) ops.moe_sum(fused_expert_output, output) return output class TopKWeightAndReduceNaiveBatched(mk.TopKWeightAndReduce): """ TopKWeightAndReduce implementation for a fused_experts output of shape (num_experts, batch_size, K) """ def __init__(self, rank: int): self.rank = rank def __eq__(self, other): return isinstance(other, TopKWeightAndReduceNaiveBatched) and ( other.rank == self.rank ) def apply( self, output: torch.Tensor | None, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, ) -> torch.Tensor: assert fused_expert_output.ndim == 3 num_tokens = topk_ids.size(0) num_local_experts = fused_expert_output.size(0) K = fused_expert_output.size(-1) if output is None: output = torch.zeros( (num_tokens, K), device=fused_expert_output.device, dtype=fused_expert_output.dtype, ) else: output.fill_(0) assert output.size() == (num_tokens, K), ( f"Expected output size {(num_tokens, K)}, but got {output.size()}" ) first_expert = num_local_experts * self.rank last_expert = first_expert + num_local_experts for expert_id in range(first_expert, last_expert): matching_tokens = topk_ids == expert_id topks = torch.any(matching_tokens, dim=1).flatten() rows = torch.count_nonzero(topks) rhs = fused_expert_output[expert_id - first_expert, :rows, :] if not apply_router_weight_on_input: rhs.mul_(topk_weights[matching_tokens].view(rhs.size(0), 1)) output[topks] = output[topks] + rhs return output
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/layer.py
vllm/model_executor/layers/fused_moe/layer.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable, Iterable from contextlib import nullcontext from enum import Enum from typing import Literal, cast, get_args, overload import torch import torch.nn.functional as F from torch.nn.parameter import UninitializedParameter import vllm.envs as envs from vllm._aiter_ops import rocm_aiter_ops from vllm.config import VllmConfig, get_current_vllm_config from vllm.config.parallel import ExpertPlacementStrategy from vllm.distributed import ( get_dp_group, get_ep_group, get_pcp_group, get_tensor_model_parallel_world_size, tensor_model_parallel_all_reduce, ) from vllm.distributed.eplb.eplb_state import EplbState from vllm.forward_context import ForwardContext, get_forward_context from vllm.logger import init_logger from vllm.model_executor.custom_op import CustomOp from vllm.model_executor.layers.fused_moe.config import ( FusedMoEConfig, FusedMoEParallelConfig, FusedMoEQuantConfig, RoutingMethodType, ) from vllm.model_executor.layers.fused_moe.rocm_aiter_fused_moe import ( init_aiter_topK_meta_data, ) from vllm.model_executor.layers.fused_moe.routing_simulator import RoutingSimulator from vllm.model_executor.layers.quantization.base_config import ( QuantizationConfig, ) from vllm.model_executor.layers.quantization.utils.flashinfer_utils import ( is_flashinfer_supporting_global_sf, ) from vllm.platforms import current_platform from vllm.utils.flashinfer import has_flashinfer_trtllm_fused_moe from vllm.utils.math_utils import cdiv, round_up from vllm.utils.torch_utils import ( aux_stream, current_stream, direct_register_custom_op, ) from vllm.v1.worker.ubatching import dbo_current_ubatch_id if current_platform.is_cuda_alike(): from .fused_moe import eplb_map_to_physical_and_record else: def _eplb_map_to_physical_and_record( topk_ids: torch.Tensor, expert_load_view: torch.Tensor, logical_to_physical_map: torch.Tensor, logical_replica_count: torch.Tensor, ) -> torch.Tensor: # CPU fallback: no EPLB so just return as is return topk_ids eplb_map_to_physical_and_record = _eplb_map_to_physical_and_record from vllm.model_executor.layers.fused_moe.fused_moe import GroupedTopk if current_platform.is_tpu(): from .moe_pallas import fused_moe as fused_moe_pallas else: fused_moe_pallas = None # type: ignore from vllm.model_executor.layers.fused_moe.fused_moe_method_base import ( FusedMoEMethodBase, ) from vllm.model_executor.layers.fused_moe.fused_moe_modular_method import ( FusedMoEModularMethod, ) from vllm.model_executor.layers.fused_moe.unquantized_fused_moe_method import ( UnquantizedFusedMoEMethod, ) logger = init_logger(__name__) class FusedMoeWeightScaleSupported(Enum): TENSOR = "tensor" CHANNEL = "channel" GROUP = "group" BLOCK = "block" def determine_expert_map( ep_size: int, ep_rank: int, global_num_experts: int, expert_placement_strategy: ExpertPlacementStrategy = "linear", num_fused_shared_experts: int = 0, return_expert_mask: bool = False, ) -> tuple[int, torch.Tensor | None, torch.Tensor | None]: """ Calculates how many experts should be assigned to each rank for EP and creates a mapping from global to local expert index. Experts are distributed evenly across ranks. Any remaining are assigned to the last rank. Args: ep_size: The size of the expert parallel group ep_rank: The rank of the current process in the expert parallel group global_num_experts: The total number of experts in the model. expert_placement_strategy: The expert placement strategy. Returns: tuple[int, Optional[torch.Tensor]]: A tuple containing: - local_num_experts (int): The number of experts assigned to the current rank. - expert_map (Optional[torch.Tensor]): A tensor of shape (global_num_experts,) mapping from global to local index. Contains -1 for experts not assigned to the current rank. Returns None if ep_size is 1. - expert_mask (Optional[torch.Tensor]): A tensor of shape (global_num_experts + num_fused_shared_experts + 1,) containing 1 for experts assigned to the current rank and 0 for sentinel. Returns None if ep_size is 1. Used only when AITER MOE is enabled. """ assert ep_size > 0 if ep_size == 1: return (global_num_experts, None, None) # Distribute experts as evenly as possible to each rank. base_experts = global_num_experts // ep_size remainder = global_num_experts % ep_size local_num_experts = base_experts + 1 if ep_rank < remainder else base_experts # Create a tensor of size num_experts filled with -1 expert_map = torch.full((global_num_experts,), -1, dtype=torch.int32) # Create an expert map for the local experts if expert_placement_strategy == "linear": start_idx = ep_rank * base_experts + min(ep_rank, remainder) expert_map[start_idx : start_idx + local_num_experts] = torch.arange( 0, local_num_experts, dtype=torch.int32 ) elif expert_placement_strategy == "round_robin": local_log_experts = torch.arange( ep_rank, global_num_experts, ep_size, dtype=torch.int32 ) expert_map[local_log_experts] = torch.arange( 0, local_num_experts, dtype=torch.int32 ) else: raise ValueError( "Unsupported expert placement strategy " f"'{expert_placement_strategy}', expected one of " f"{get_args(ExpertPlacementStrategy)}" ) expert_mask = None if return_expert_mask: expert_mask = torch.ones( (global_num_experts + num_fused_shared_experts + 1,), dtype=torch.int32 ) expert_mask[-1] = 0 expert_mask[:global_num_experts] = expert_map > -1 expert_map = torch.cat( ( expert_map, torch.tensor( [local_num_experts + i for i in range(num_fused_shared_experts)], dtype=torch.int32, ), ), dim=0, ) return (local_num_experts, expert_map, expert_mask) def determine_expert_placement_strategy( expert_placement_strategy: ExpertPlacementStrategy, moe_parallel_config: FusedMoEParallelConfig, num_expert_group: int | None, num_redundant_experts: int, enable_eplb: bool, ) -> ExpertPlacementStrategy: if expert_placement_strategy == "round_robin": round_robin_supported = ( (num_expert_group is not None and num_expert_group > 1) and num_redundant_experts == 0 and not enable_eplb ) if not round_robin_supported: logger.warning( "Round-robin expert placement is only supported for " "models with multiple expert groups and no redundant " "experts. Falling back to linear expert placement." ) return "linear" if ( moe_parallel_config.use_all2all_kernels and not moe_parallel_config.use_deepep_ll_kernels ): logger.warning( "Round-robin expert placement currently only supports " "the DeepEP low-latency backend, but '%s' was configured. " "Falling back to linear expert placement.", moe_parallel_config.all2all_backend, ) return "linear" return expert_placement_strategy def get_compressed_expert_map(expert_map: torch.Tensor) -> str: """ Compresses the expert map by removing any -1 entries. Args: expert_map (torch.Tensor): A tensor of shape (global_num_experts,) mapping from global to local index. Contains -1 for experts not assigned to the current rank. Returns: str: A string mapping from local to global index. Using str to support hashing for logging once only. """ global_indices = torch.where(expert_map != -1)[0] local_indices = expert_map[global_indices] return ", ".join( f"{local_index.item()}->{global_index.item()}" for local_index, global_index in zip(local_indices, global_indices) ) def maybe_roundup_hidden_size( hidden_size: int, act_dtype: torch.dtype, quant_config: QuantizationConfig | None, moe_parallel_config: FusedMoEParallelConfig, is_lora_enabled: bool, ) -> int: """ Given layer hidden size and MoE configurations, round up hidden_size if necessary. Args: hidden_size: Layer hidden-size act_dtype: Data type of the layer activations. quant_config: Fused MoE quantization configuration. moe_parallel_config: Fused MoE parallelization strategy configuration. is_lora_enabled: True if the engine is enabled with LoRA. This is used in the case of mxfp4 quantization in selecting the MxFP4Backend. Return: Rounded up hidden_size if rounding up is required based on the configs. Original hidden size otherwise. """ from vllm.model_executor.layers.fused_moe.all2all_utils import ( maybe_roundup_layer_hidden_size, ) hidden_size = maybe_roundup_layer_hidden_size( hidden_size, act_dtype, moe_parallel_config ) # we are padding globally so EP buffer allocation works if quant_config and quant_config.get_name() == "mxfp4": from vllm.model_executor.layers.quantization.mxfp4 import ( Mxfp4Backend, get_mxfp4_backend, ) current_mxfp4_backend = get_mxfp4_backend(is_lora_enabled) if ( current_mxfp4_backend == Mxfp4Backend.SM90_FI_MXFP4_BF16 or current_mxfp4_backend == Mxfp4Backend.SM100_FI_MXFP4_MXFP8_CUTLASS ): hidden_size = round_up(hidden_size, 128) elif ( current_platform.is_rocm() or current_mxfp4_backend == Mxfp4Backend.SM100_FI_MXFP4_MXFP8_TRTLLM or current_mxfp4_backend == Mxfp4Backend.SM100_FI_MXFP4_BF16 ): hidden_size = round_up(hidden_size, 256) return hidden_size @CustomOp.register("fused_moe") class FusedMoE(CustomOp): """FusedMoE layer for MoE models. This layer contains both MergedColumnParallel weights (gate_up_proj / w13) and RowParallelLinear weights (down_proj/ w2). Note: Mixtral uses w1, w2, and w3 for gate, up, and down_proj. We copy that naming convention here and handle any remapping in the load_weights function in each model implementation. Args: num_experts: Number of experts in the model top_k: Number of experts selected for each token hidden_size: Input hidden state size of the transformer intermediate_size: Intermediate size of the experts params_dtype: Data type for the parameters. reduce_results: Whether to all_reduce on the output of the layer renormalize: Whether to renormalize the logits in the fused_moe kernel quant_config: Quantization configure. enable_eplb: Whether to enable expert parallelism load balancer. """ def __init__( self, num_experts: int, # Global number of experts top_k: int, hidden_size: int, intermediate_size: int, params_dtype: torch.dtype | None = None, reduce_results: bool = False, renormalize: bool = True, use_grouped_topk: bool = False, num_expert_group: int | None = None, topk_group: int | None = None, quant_config: QuantizationConfig | None = None, tp_size: int | None = None, ep_size: int | None = None, dp_size: int | None = None, pcp_size: int | None = None, prefix: str = "", custom_routing_function: Callable | None = None, scoring_func: str = "softmax", routed_scaling_factor: float = 1.0, e_score_correction_bias: torch.Tensor | None = None, apply_router_weight_on_input: bool = False, activation: str = "silu", is_act_and_mul: bool = True, enable_eplb: bool = False, num_redundant_experts: int = 0, has_bias: bool = False, is_sequence_parallel=False, expert_mapping: list[tuple[str, str, int, str]] | None = None, n_shared_experts: int | None = None, routing_method_type: int | None = None, ): super().__init__() # Allow disabling of the separate shared experts stream for # debug purposes. # TODO: Remove this after more extensive testings with TP/DP # and other execution modes if envs.VLLM_DISABLE_SHARED_EXPERTS_STREAM: logger.debug_once("Disabling MoE shared_experts cuda stream", scope="local") self.shared_experts_stream = None else: # TODO(rob): enable shared expert overlap with non-cuda-alike. # aux_stream() returns None on non-cuda-alike platforms. self.shared_experts_stream = aux_stream() if self.shared_experts_stream is not None: logger.debug_once( "Enabled separate cuda stream for MoE shared_experts", scope="local" ) if params_dtype is None: params_dtype = torch.get_default_dtype() self.params_dtype = params_dtype vllm_config = get_current_vllm_config() self.vllm_config = vllm_config # FIXME (varun): We should have a better way of inferring the activation # datatype. This works for now as the tensor datatype entering the MoE # operation is typically unquantized (i.e. float16/bfloat16). if vllm_config.model_config is not None: moe_in_dtype = vllm_config.model_config.dtype else: # TODO (bnell): This is a hack to get test_mixtral_moe to work # since model_config is not set in the pytest test. moe_in_dtype = params_dtype tp_size_ = ( tp_size if tp_size is not None else get_tensor_model_parallel_world_size() ) dp_size_ = dp_size if dp_size is not None else get_dp_group().world_size pcp_size_ = pcp_size if pcp_size is not None else get_pcp_group().world_size self.is_sequence_parallel = is_sequence_parallel self.sp_size = tp_size_ if is_sequence_parallel else 1 self.moe_parallel_config: FusedMoEParallelConfig = FusedMoEParallelConfig.make( tp_size_=tp_size_, pcp_size_=pcp_size_, dp_size_=dp_size_, vllm_parallel_config=vllm_config.parallel_config, ) self.global_num_experts = num_experts + num_redundant_experts self.logical_num_experts = num_experts # Expert mapping used in self.load_weights self.expert_mapping = expert_mapping # Round up hidden size if needed. hidden_size = maybe_roundup_hidden_size( hidden_size, moe_in_dtype, quant_config, self.moe_parallel_config, is_lora_enabled=self.vllm_config.lora_config is not None, ) # For smuggling this layer into the fused moe custom op compilation_config = vllm_config.compilation_config if prefix in compilation_config.static_forward_context: raise ValueError("Duplicate layer name: {}".format(prefix)) compilation_config.static_forward_context[prefix] = self self.layer_name = prefix self.enable_eplb = enable_eplb self.expert_load_view: torch.Tensor | None = None self.logical_to_physical_map: torch.Tensor | None = None self.logical_replica_count: torch.Tensor | None = None self.expert_placement_strategy: ExpertPlacementStrategy = ( vllm_config.parallel_config.expert_placement_strategy ) # ROCm aiter shared experts fusion self.rocm_aiter_fmoe_enabled = rocm_aiter_ops.is_fused_moe_enabled() self.aiter_fmoe_shared_expert_enabled = ( rocm_aiter_ops.is_fusion_moe_shared_experts_enabled() ) self.num_fused_shared_experts = ( n_shared_experts if n_shared_experts is not None and self.aiter_fmoe_shared_expert_enabled else 0 ) if ( not self.aiter_fmoe_shared_expert_enabled and self.num_fused_shared_experts != 0 ): raise ValueError( "n_shared_experts is only supported on ROCm aiter when " "VLLM_ROCM_USE_AITER_FUSION_SHARED_EXPERTS is enabled" ) # Determine expert maps if self.use_ep: if self.enable_eplb: assert self.global_num_experts % self.ep_size == 0, ( "EPLB currently only supports even distribution of " "experts across ranks." ) else: assert num_redundant_experts == 0, ( "Redundant experts are only supported with EPLB." ) self.expert_placement_strategy = determine_expert_placement_strategy( expert_placement_strategy=self.expert_placement_strategy, moe_parallel_config=self.moe_parallel_config, num_expert_group=num_expert_group, num_redundant_experts=num_redundant_experts, enable_eplb=self.enable_eplb, ) self._expert_map: torch.Tensor | None local_num_experts, expert_map, expert_mask = determine_expert_map( ep_size=self.ep_size, ep_rank=self.ep_rank, global_num_experts=self.global_num_experts, expert_placement_strategy=self.expert_placement_strategy, num_fused_shared_experts=self.num_fused_shared_experts, return_expert_mask=self.rocm_aiter_fmoe_enabled, ) self.local_num_experts = local_num_experts self.register_buffer("_expert_map", expert_map) self.register_buffer("expert_mask", expert_mask) self._maybe_init_expert_routing_tables() logger.info_once( "[EP Rank %s/%s] Expert parallelism is enabled. Expert " "placement strategy: %s. Local/global" " number of experts: %s/%s. Experts local to global index map:" " %s.", self.ep_rank, self.ep_size, self.expert_placement_strategy, self.local_num_experts, self.global_num_experts, get_compressed_expert_map(self._expert_map), ) else: self.local_num_experts, self._expert_map, self.expert_mask = ( self.global_num_experts, None, None, ) self.top_k = top_k self._init_aiter_shared_experts_topK_buffer( vllm_config=vllm_config, dp_size=dp_size_ ) if self.use_ep and self.rocm_aiter_fmoe_enabled: assert self.expert_mask is None or torch.all( (expert_mask == 0) | (expert_mask == 1) ), "Aiter Fused MoE kernel only supports expert_map with 0 and 1s." assert intermediate_size % self.tp_size == 0 self.hidden_size = hidden_size self.intermediate_size_per_partition = intermediate_size // self.tp_size self.reduce_results = reduce_results self.renormalize = renormalize self.use_grouped_topk = use_grouped_topk if self.use_grouped_topk: assert num_expert_group is not None and topk_group is not None self.num_expert_group = num_expert_group self.topk_group = topk_group self.custom_routing_function = custom_routing_function self.scoring_func = scoring_func self.routed_scaling_factor = routed_scaling_factor self.e_score_correction_bias = e_score_correction_bias self.apply_router_weight_on_input = apply_router_weight_on_input self.activation = activation if self.scoring_func != "softmax" and not self.use_grouped_topk: raise ValueError( "Only softmax scoring function is supported for non-grouped topk." ) # ToDo: Better logic to determine the routing method type if routing_method_type is not None: self.routing_method_type = routing_method_type else: if scoring_func == "sigmoid": if self.use_grouped_topk: self.routing_method_type = RoutingMethodType.DeepSeekV3 elif self.top_k == 1: self.routing_method_type = RoutingMethodType.Llama4 elif self.scoring_func == "softmax": self.routing_method_type = ( RoutingMethodType.Renormalize if not self.renormalize else RoutingMethodType.RenormalizeNaive ) else: self.routing_method_type = RoutingMethodType.TopK self.moe_config: FusedMoEConfig = FusedMoEConfig( num_experts=self.global_num_experts, experts_per_token=top_k, hidden_dim=hidden_size, num_local_experts=self.local_num_experts, moe_parallel_config=self.moe_parallel_config, in_dtype=moe_in_dtype, max_num_tokens=envs.VLLM_MOE_DP_CHUNK_SIZE, has_bias=has_bias, is_act_and_mul=is_act_and_mul, is_lora_enabled=vllm_config.lora_config is not None, ) self.moe_config_use_flashinfer_cutlass_kernels = ( self.moe_config.use_flashinfer_cutlass_kernels ) self.quant_config = quant_config def _get_quant_method() -> FusedMoEMethodBase: """ Helper method to ensure self.quant_method is never None and of the proper type. """ quant_method = None if self.quant_config is not None: quant_method = self.quant_config.get_quant_method(self, prefix) if quant_method is None: quant_method = UnquantizedFusedMoEMethod(self.moe_config) assert isinstance(quant_method, FusedMoEMethodBase) return quant_method # Note: get_quant_method will look at the layer's local_num_experts # for heuristic purposes, so it must be initialized first. self.quant_method: FusedMoEMethodBase = _get_quant_method() if not self.moe_config.is_act_and_mul: # Avoid circular import from vllm.model_executor.layers.quantization.modelopt import ( ModelOptFp8MoEMethod, ModelOptNvFp4FusedMoE, ) if not isinstance( self.quant_method, ( UnquantizedFusedMoEMethod, ModelOptFp8MoEMethod, ModelOptNvFp4FusedMoE, ), ): raise NotImplementedError( "is_act_and_mul=False is supported only for unquantized " ", ModelOpt FP8, and ModelOpt NvFp4 checkpoints" ) if not current_platform.is_cuda(): raise NotImplementedError( "is_act_and_mul=False is supported only for CUDA for now" ) if self.enable_eplb and not self.quant_method.supports_eplb: # TODO: Add support for additional quantization methods. # The implementation for other quantization methods does not # contain essential differences, but the current quant API # design causes duplicated work when extending to new # quantization methods, so I'm leaving it for now. # If you plan to add support for more quantization methods, # please refer to the implementation in `Fp8MoEMethod`. raise NotImplementedError( f"EPLB is not supported {self.quant_method.__class__.__name__}. " "EPLB is only supported for FP8 quantization for now." ) moe_quant_params = { "num_experts": self.local_num_experts, "hidden_size": hidden_size, "intermediate_size_per_partition": self.intermediate_size_per_partition, "params_dtype": params_dtype, "weight_loader": self.weight_loader, "global_num_experts": self.global_num_experts, } # need full intermediate size pre-sharding for WNA16 act order if self.quant_method.__class__.__name__ in ( "GPTQMarlinMoEMethod", "CompressedTensorsWNA16MarlinMoEMethod", "CompressedTensorsWNA16MoEMethod", ): moe_quant_params["intermediate_size_full"] = intermediate_size self.quant_method.create_weights(layer=self, **moe_quant_params) # Chunked all2all staging tensor self.batched_hidden_states: torch.Tensor | None = None self.batched_router_logits: torch.Tensor | None = None # Note: maybe_init_modular_kernel should only be called by # prepare_communication_buffer_for_model. # This is called after all weight loading and post-processing, so it # should be safe to swap out the quant_method. def maybe_init_modular_kernel(self) -> None: self.ensure_moe_quant_config_init() # routing_tables only needed for round-robin expert placement with # DeepEP all2all backend. routing_tables = self._maybe_init_expert_routing_tables() prepare_finalize = self.quant_method.maybe_make_prepare_finalize( routing_tables=routing_tables ) if prepare_finalize is not None: logger.debug( "%s for %s(%s)", prepare_finalize.__class__.__name__, self, id(self) ) self.quant_method = FusedMoEModularMethod.make( self, self.quant_method, prepare_finalize, self.shared_experts ) @property def shared_experts(self) -> torch.nn.Module | None: return None @property def gate(self) -> torch.nn.Module | None: return None @property def tp_size(self): return self.moe_parallel_config.tp_size @property def dp_size(self): return self.moe_parallel_config.dp_size @property def pcp_size(self): return self.moe_parallel_config.pcp_size @property def ep_size(self): return self.moe_parallel_config.ep_size @property def tp_rank(self): return self.moe_parallel_config.tp_rank @property def dp_rank(self): return self.moe_parallel_config.dp_rank @property def pcp_rank(self): return self.moe_parallel_config.pcp_rank @property def ep_rank(self): return self.moe_parallel_config.ep_rank @property def use_ep(self): return self.moe_parallel_config.use_ep @property def use_pplx_kernels(self): return self.moe_parallel_config.use_pplx_kernels @property def use_deepep_ht_kernels(self): return self.moe_parallel_config.use_deepep_ht_kernels @property def use_deepep_ll_kernels(self): return self.moe_parallel_config.use_deepep_ll_kernels @property def use_flashinfer_cutlass_kernels(self): return ( self.moe_quant_config is not None and self.moe_quant_config.quant_dtype == "nvfp4" and self.moe_config_use_flashinfer_cutlass_kernels ) @property def use_marlin_kernels(self): return getattr(self.quant_method, "use_marlin", False) @property def use_dp_chunking(self) -> bool: return ( self.moe_parallel_config.use_pplx_kernels or self.moe_parallel_config.use_deepep_ll_kernels or (self.dp_size > 1 and self.use_flashinfer_cutlass_kernels) ) and envs.VLLM_ENABLE_MOE_DP_CHUNK @property def is_internal_router(self) -> bool: # By default, router/gate is called before FusedMoE forward pass return False def _maybe_init_expert_routing_tables( self, ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor] | None: # Currently routing_tables only needed for round-robin expert placement # with DeepEP-ll all2all backend. if ( self.expert_placement_strategy != "round_robin" or not self.use_deepep_ll_kernels ): return None if hasattr(self, "expert_global_to_physical"): return cast( tuple[torch.Tensor, torch.Tensor, torch.Tensor], ( self.expert_global_to_physical, self.expert_physical_to_global, self.expert_local_to_global, ), ) if self._expert_map is None: return None routing_tables = self.ensure_round_robin_expert_routing_tables( global_num_experts=self.global_num_experts, ep_size=self.ep_size, ep_rank=self.ep_rank, local_num_experts=self.local_num_experts, device=self._expert_map.device, ) global_to_physical, physical_to_global, local_global = routing_tables self.register_buffer("expert_global_to_physical", global_to_physical) self.register_buffer("expert_physical_to_global", physical_to_global) self.register_buffer("expert_local_to_global", local_global) return routing_tables @staticmethod def ensure_round_robin_expert_routing_tables( global_num_experts: int, ep_size: int, ep_rank: int, local_num_experts: int, device: torch.device | None = None, ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: device_kwargs = {"device": device} if device is not None else {} global_indices = torch.arange( global_num_experts, dtype=torch.long, **device_kwargs ) owner = torch.remainder(global_indices, ep_size) local_index = torch.div(global_indices, ep_size, rounding_mode="floor") base = global_num_experts // ep_size remainder = global_num_experts % ep_size physical_offset = owner * base if remainder > 0: remainder_tensor = torch.tensor( remainder, dtype=torch.long, **device_kwargs ) physical_offset = physical_offset + torch.minimum(owner, remainder_tensor) global_to_physical = physical_offset + local_index physical_to_global = torch.empty_like(global_to_physical) physical_to_global[global_to_physical] = global_indices local_global = torch.arange( ep_rank, global_num_experts, ep_size, dtype=torch.long, **device_kwargs, ) if local_global.numel() != local_num_experts: local_global = local_global[:local_num_experts] return (global_to_physical, physical_to_global, local_global) def update_expert_map(self): # ep_size and ep_rank should already be updated assert self._expert_map is not None with self._expert_map.device: local_num_experts, expert_map, expert_mask = determine_expert_map( ep_size=self.ep_size, ep_rank=self.ep_rank, global_num_experts=self.global_num_experts, expert_placement_strategy=self.expert_placement_strategy, num_fused_shared_experts=self.num_fused_shared_experts, return_expert_mask=self.rocm_aiter_fmoe_enabled, ) self.local_num_experts = local_num_experts self.register_buffer("_expert_map", expert_map) self.register_buffer("expert_mask", expert_mask) self._maybe_init_expert_routing_tables() if self.aiter_fmoe_shared_expert_enabled: self._init_aiter_shared_experts_topK_buffer( vllm_config=get_current_vllm_config(), dp_size=get_dp_group().world_size, ) def _maybe_setup_shared_experts_stream( self,
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
true
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/trtllm_moe.py
vllm/model_executor/layers/fused_moe/trtllm_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm.model_executor.layers.fused_moe.config import ( FusedMoEConfig, FusedMoEQuantConfig, ) from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import ( TopKWeightAndReduceNoOP, ) class TrtLlmGenExperts(mk.FusedMoEPermuteExpertsUnpermute): def __init__( self, moe: FusedMoEConfig, quant_config: FusedMoEQuantConfig, gemm1_alpha, gemm1_beta, gemm1_clamp_limit, max_capture_size, ): super().__init__(quant_config) self.moe = moe self.gemm1_alpha = gemm1_alpha self.gemm1_beta = gemm1_beta self.gemm1_clamp_limit = gemm1_clamp_limit self.max_capture_size = max_capture_size @property def activation_formats( self, ) -> tuple[mk.FusedMoEActivationFormat, mk.FusedMoEActivationFormat]: return ( mk.FusedMoEActivationFormat.Standard, mk.FusedMoEActivationFormat.Standard, ) def supports_chunking(self) -> bool: return True def supports_expert_map(self) -> bool: return True def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce: return TopKWeightAndReduceNoOP() def workspace_shapes( self, M: int, N: int, K: int, topk: int, global_num_experts: int, local_num_experts: int, expert_tokens_meta: mk.ExpertTokensMetadata | None, ) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...]]: # The workspaces for this implementation are managed by flashinfer. workspace1 = (0,) workspace2 = (0,) output = (M, K) return (workspace1, workspace2, output) def apply( self, output: torch.Tensor, hidden_states: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: str, global_num_experts: int, expert_map: torch.Tensor | None, a1q_scale: torch.Tensor | None, a2_scale: torch.Tensor | None, workspace13: torch.Tensor, workspace2: torch.Tensor, expert_tokens_meta: mk.ExpertTokensMetadata | None, apply_router_weight_on_input: bool, ): topk = topk_ids.size(-1) local_num_experts = w1.size(0) intermediate_size = w2.size(1) local_expert_offset = self.moe.ep_rank * local_num_experts x_quant = hidden_states x_scale = a1q_scale if x_scale is not None: x_scale = x_scale.view(torch.float8_e4m3fn).reshape(*x_quant.shape[:-1], -1) packed_tensor = (topk_ids.to(torch.int32) << 16) | topk_weights.to( torch.bfloat16 ).view(torch.int16) assert self.w1_scale is not None assert self.w2_scale is not None kwargs = { "topk_ids": packed_tensor, "routing_bias": None, "hidden_states": x_quant, "hidden_states_scale": x_scale, "gemm1_weights": w1, "gemm1_weights_scale": self.w1_scale, "gemm1_bias": self.w1_bias, "gemm1_alpha": self.gemm1_alpha, "gemm1_beta": self.gemm1_beta, "gemm1_clamp_limit": self.gemm1_clamp_limit, "gemm2_weights": w2, "gemm2_weights_scale": self.w2_scale, "gemm2_bias": self.w2_bias, "output1_scale_scalar": None, "output1_scale_gate_scalar": None, "output2_scale_scalar": None, "num_experts": global_num_experts, "top_k": topk, "n_group": None, "topk_group": None, "intermediate_size": intermediate_size, "local_expert_offset": local_expert_offset, "local_num_experts": local_num_experts, "routed_scaling_factor": None, "tile_tokens_dim": None, "routing_method_type": 1, "do_finalize": True, "output": output, "tune_max_num_tokens": max(self.max_capture_size, 1), } from flashinfer import trtllm_fp4_block_scale_routed_moe from vllm.utils.flashinfer import autotune with autotune(False): # Enable autotune when, # https://github.com/flashinfer-ai/flashinfer/issues/2023 is # resolved. trtllm_fp4_block_scale_routed_moe(**kwargs) return output
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/fused_batched_moe.py
vllm/model_executor/layers/fused_moe/fused_batched_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Fused batched MoE kernel.""" import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm.model_executor.layers.fused_moe.config import FusedMoEQuantConfig from vllm.model_executor.layers.fused_moe.fused_moe import try_get_optimal_moe_config from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import ( TopKWeightAndReduceDelegate, TopKWeightAndReduceNaiveBatched, ) from vllm.model_executor.layers.fused_moe.utils import ( _resize_cache, moe_kernel_quantize_input, normalize_batched_scales_shape, normalize_scales_shape, ) from vllm.model_executor.layers.quantization.utils.quant_utils import group_broadcast from vllm.triton_utils import tl, triton @triton.jit def moe_mmk( a_ptrs, b_ptrs, K, expert_id, a_scale_ptr, b_scale_ptr, # The stride variables represent how much to increase the ptr by when # moving by 1 element in a particular dimension. E.g. `stride_am` is # how much to increase `a_ptr` by to get the element one row down # (A has M rows). stride_ak: tl.int64, stride_bk: tl.int64, stride_ase: tl.int64, stride_asm: tl.int64, stride_ask: tl.int64, stride_bse: tl.int64, stride_bsk: tl.int64, stride_bsn: tl.int64, # Offsets and masks offs_m, offs_n, offs_bn, mask_m, # Block size for block-wise quantization group_n: tl.constexpr, group_k: tl.constexpr, # Meta-parameters BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, BLOCK_K: tl.constexpr, compute_type: tl.constexpr, use_w8a8: tl.constexpr, use_w8a16: tl.constexpr, per_act_token_quant: tl.constexpr, ): offs_k = tl.arange(0, BLOCK_K) if use_w8a16: b_scale_ptrs = ( b_scale_ptr + expert_id * stride_bse + offs_n[None, :] * stride_bsn ) b_scale = tl.load(b_scale_ptrs) if use_w8a8: # block-wise if group_k > 0 and group_n > 0: a_scale_ptrs = a_scale_ptr + offs_m * stride_asm offs_bsn = offs_bn // group_n b_scale_ptrs = b_scale_ptr + offs_bsn * stride_bsn # per act token elif per_act_token_quant: # Load per-token scale for activations a_scale_ptrs = a_scale_ptr + offs_m * stride_asm a_scale = tl.load(a_scale_ptrs, mask=mask_m, other=0.0)[:, None] b_scale_ptrs = b_scale_ptr + offs_bn[None, :] * stride_bsn b_scale = tl.load(b_scale_ptrs) # tensor-wise else: a_scale = tl.load(a_scale_ptr) b_scale = tl.load(b_scale_ptr) # ----------------------------------------------------------- # Iterate to compute a block of the C matrix. # We accumulate into a `[BLOCK_SIZE_M, BLOCK_SIZE_N]` block # of fp32 values for higher accuracy. # `accumulator` will be converted back to fp16 after the loop. accumulator = tl.zeros((BLOCK_M, BLOCK_N), dtype=tl.float32) for k in range(0, tl.cdiv(K, BLOCK_K)): # Load the next block of A and B, generate a mask by checking the # K dimension. a = tl.load( a_ptrs, mask=mask_m[:, None] & (offs_k[None, :] < K - k * BLOCK_K), other=0.0, ) b = tl.load(b_ptrs, mask=offs_k[:, None] < K - k * BLOCK_K, other=0.0) # We accumulate along the K dimension. if use_w8a16: accumulator = tl.dot(a, b.to(compute_type), acc=accumulator) elif use_w8a8: if group_k > 0 and group_n > 0: k_start = k * BLOCK_K offs_ks = k_start // group_k a_scale = tl.load( a_scale_ptrs + offs_ks * stride_ask, mask=mask_m, other=0.0 ) b_scale = tl.load(b_scale_ptrs + offs_ks * stride_bsk) accumulator += tl.dot(a, b) * a_scale[:, None] * b_scale[None, :] else: # acc used to enable fp8_fast_accum accumulator = tl.dot(a, b, acc=accumulator) else: accumulator += tl.dot(a, b) # Advance the ptrs to the next K block. a_ptrs += BLOCK_K * stride_ak b_ptrs += BLOCK_K * stride_bk if use_w8a16: accumulator = (accumulator * b_scale).to(compute_type) elif use_w8a8: if group_k > 0 and group_n > 0: accumulator = accumulator.to(compute_type) else: accumulator = (accumulator * a_scale * b_scale).to(compute_type) else: accumulator = accumulator.to(compute_type) return accumulator @triton.jit def expert_triton_kernel( a_ptr, # [max_tokens, K] b_ptr, # [K, N] c_ptr, # [max_tokens, N] expert_id, compute_type: tl.constexpr, # Dimensions M, N, K, # Quantization data a_scale_ptr, b_scale_ptr, b_zp_ptr, # strides stride_am: tl.int64, stride_ak: tl.int64, stride_bk: tl.int64, stride_bn: tl.int64, stride_cm: tl.int64, stride_cn: tl.int64, stride_ase: tl.int64, stride_asm: tl.int64, stride_ask: tl.int64, stride_bse: tl.int64, stride_bsk: tl.int64, stride_bsn: tl.int64, # offsets offs_bn, # Blockwise quantization data group_n, group_k, # Quantization schemes use_fp8_w8a8: tl.constexpr, use_int8_w8a16: tl.constexpr, per_act_token_quant: tl.constexpr, # Kernel config BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, BLOCK_K: tl.constexpr, ): offs_m = tl.arange(0, BLOCK_M) offs_n = tl.arange(0, BLOCK_N) % N offs_k = tl.arange(0, BLOCK_K) mask_m = offs_m < M # Make grids of a + b pointers a_ptrs = a_ptr + offs_m[:, None] * stride_am + offs_k[None, :] * stride_ak b_ptrs = b_ptr + offs_k[:, None] * stride_bk + offs_n[None, :] * stride_bn accumulator = moe_mmk( a_ptrs, b_ptrs, K, expert_id, a_scale_ptr, b_scale_ptr, # The stride variables represent how much to increase the ptr by when # moving by 1 element in a particular dimension. E.g. `stride_am` is # how much to increase `a_ptr` by to get the element one row down # (A has M rows). stride_ak, stride_bk, stride_ase, stride_asm, stride_ask, stride_bse, stride_bsk, stride_bsn, # Offsets and masks offs_m, offs_n, offs_bn, mask_m, # Block size for block-wise quantization group_n, group_k, # Meta-parameters BLOCK_M, BLOCK_N, BLOCK_K, compute_type, use_fp8_w8a8, use_int8_w8a16, per_act_token_quant, ) # store in C offs_cn = tl.arange(0, BLOCK_N) c_ptrs = c_ptr + offs_m[:, None] * stride_cm + offs_cn[None, :] * stride_cn c_mask = mask_m[:, None] & (offs_cn[None, :] < N) tl.store(c_ptrs, accumulator, mask=c_mask) @triton.jit def batched_triton_kernel( a_ptr, # [E, max_num_tokens, K] b_ptr, # [E, K, N] c_ptr, # [E, max_num_tokens, N] expert_num_tokens, # [E] compute_type: tl.constexpr, # Dimensions max_num_tokens, K, N, # Quantization data a_scale_ptr, b_scale_ptr, b_zp_ptr, # The stride variables represent how much to increase the ptr by when # moving by 1 element in a particular dimension. E.g. `stride_am` is # how much to increase `a_ptr` by to get the element one row down # (A has M rows). stride_ae: tl.int64, stride_am: tl.int64, stride_ak: tl.int64, stride_be: tl.int64, stride_bk: tl.int64, stride_bn: tl.int64, stride_ce: tl.int64, stride_cm: tl.int64, stride_cn: tl.int64, stride_ase: tl.int64, stride_asm: tl.int64, stride_ask: tl.int64, stride_bse: tl.int64, stride_bsk: tl.int64, stride_bsn: tl.int64, # Blockwise quantization data group_n: tl.constexpr, group_k: tl.constexpr, # Quantization schemes use_fp8_w8a8: tl.constexpr, use_int8_w8a16: tl.constexpr, per_act_token_quant: tl.constexpr, # Kernel config BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, BLOCK_K: tl.constexpr, ): expert_id = tl.program_id(axis=0) e_num_tokens = tl.load(expert_num_tokens + expert_id) if e_num_tokens == 0: # Early exit return # axis 1 is M_blocks * N_blocks pid_mn = tl.program_id(axis=1) # num_pid_m = tl.cdiv(max_num_tokens, BLOCK_M) num_pid_n = tl.cdiv(N, BLOCK_N) pid_m = pid_mn // num_pid_n pid_n = pid_mn % num_pid_n cta_m_start = pid_m * BLOCK_M cta_n_start = pid_n * BLOCK_N if cta_m_start >= e_num_tokens: # Early exit return cta_m_size = min(BLOCK_M, e_num_tokens - cta_m_start) cta_n_size = min(BLOCK_N, N - cta_n_start) a_ptr = a_ptr + expert_id * stride_ae + cta_m_start * stride_am b_ptr = b_ptr + expert_id * stride_be + cta_n_start * stride_bn c_ptr = ( c_ptr + expert_id * stride_ce + cta_m_start * stride_cm + cta_n_start * stride_cn ) offs_bn = (pid_n * BLOCK_N + tl.arange(0, BLOCK_N).to(tl.int64)) % N if use_fp8_w8a8: a_scale_ptr = a_scale_ptr + expert_id * stride_ase b_scale_ptr = b_scale_ptr + expert_id * stride_bse # block-wise if group_k > 0 and group_n > 0 or per_act_token_quant: a_scale_ptr = a_scale_ptr + cta_m_start * stride_asm expert_triton_kernel( a_ptr, b_ptr, c_ptr, expert_id, compute_type, cta_m_size, # M cta_n_size, # N K, # K a_scale_ptr, b_scale_ptr, b_zp_ptr, # Strides stride_am, stride_ak, stride_bk, stride_bn, stride_cm, stride_cn, stride_ase, stride_asm, stride_ask, stride_bse, stride_bsk, stride_bsn, # offsets offs_bn, # Blockwise quantization data group_n, group_k, # Quantization schemes use_fp8_w8a8, use_int8_w8a16, per_act_token_quant, # Kernel config BLOCK_M, BLOCK_N, BLOCK_K, ) def invoke_moe_batched_triton_kernel( A: torch.Tensor, # [E, max_tokens, K] B: torch.Tensor, # [E, N, K] C: torch.Tensor, # [E, max_tokens, N] expert_num_tokens: torch.Tensor, # [E] compute_type: tl.dtype, # Quantization data A_scale: torch.Tensor | None, B_scale: torch.Tensor | None, B_zp: torch.Tensor, # Quantization schemes use_fp8_w8a8: bool, use_int8_w8a16: bool, use_int4_w4a16: bool, config: dict[str, int], per_act_token_quant: bool, block_shape: list[int] | None = None, ): assert not use_int4_w4a16 max_num_tokens = A.size(1) K = A.size(2) N = C.size(2) BLOCK_M = config["BLOCK_SIZE_M"] BLOCK_N = config["BLOCK_SIZE_N"] BLOCK_K = config["BLOCK_SIZE_K"] grid = ( expert_num_tokens.size(0), triton.cdiv(max_num_tokens, BLOCK_M) * triton.cdiv(B.size(1), BLOCK_N), ) A_scale = normalize_batched_scales_shape(A_scale, expert_num_tokens.shape[0]) if B_scale is not None and B_scale.ndim == 1: assert B_scale.numel() == expert_num_tokens.shape[0] B_scale = B_scale.view(-1, 1, 1) assert A_scale is None or A_scale.ndim == 3, ( f"{0 if A_scale is None else A_scale.shape}" ) assert B_scale is None or B_scale.ndim == 1 or B_scale.ndim == 3, ( f"{0 if B_scale is None else B_scale.shape}" ) if B_scale is not None: if B_scale.ndim == 1: stride_bse = 1 stride_bsk = 0 stride_bsn = 0 else: stride_bse = B_scale.stride(0) stride_bsk = B_scale.stride(2) stride_bsn = B_scale.stride(1) else: stride_bse = 0 stride_bsk = 0 stride_bsn = 0 if A_scale is not None: stride_ase = A_scale.stride(0) stride_asm = A_scale.stride(1) stride_ask = A_scale.stride(2) else: stride_ase = 0 stride_asm = 0 stride_ask = 0 batched_triton_kernel[grid]( A, B, C, expert_num_tokens, compute_type, # Dimensions max_num_tokens, K, N, # Quantization data A_scale, B_scale, B_zp, # Strides A.stride(0), A.stride(1), A.stride(2), B.stride(0), B.stride(2), B.stride(1), C.stride(0), C.stride(1), C.stride(2), stride_ase, stride_asm, stride_ask, stride_bse, stride_bsk, stride_bsn, # Blockwise quantization data 0 if block_shape is None else block_shape[0], 0 if block_shape is None else block_shape[1], # Quantization schemes use_fp8_w8a8, use_int8_w8a16, per_act_token_quant, # Kernel config BLOCK_M=BLOCK_M, BLOCK_N=BLOCK_N, BLOCK_K=BLOCK_K, ) class BatchedPrepareAndFinalize(mk.FusedMoEPrepareAndFinalize): """ A reference prepare/finalize class that reorganizes the tokens into expert batched format, i.e. E x max_num_tokens x K. This is the format that the PPLX dispatch/combine kernels use. """ def __init__( self, max_num_tokens: int, num_local_experts: int, num_dispatchers: int, rank: int, ): super().__init__() self.max_num_tokens = max_num_tokens self.num_local_experts = num_local_experts self.rank = rank self.num_dispatchers_ = num_dispatchers @property def activation_format(self) -> mk.FusedMoEActivationFormat: return mk.FusedMoEActivationFormat.BatchedExperts def max_num_tokens_per_rank(self) -> int | None: return self.max_num_tokens def topk_indices_dtype(self) -> torch.dtype | None: return None def num_dispatchers(self) -> int: return self.num_dispatchers_ def output_is_reduced(self) -> bool: return False def prepare( self, a1: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, num_experts: int, expert_map: torch.Tensor | None, apply_router_weight_on_input: bool, quant_config: FusedMoEQuantConfig, ) -> mk.PrepareResultType: assert a1.dim() == 2 assert topk_ids.dim() == 2 assert topk_ids.size(0) == a1.size(0) if apply_router_weight_on_input: topk = topk_ids.size(1) # TODO: this only works for topK=1, will need to update for topK>1 assert topk == 1, ( "apply_router_weight_on_input is only implemented for topk=1" ) a1.mul_(topk_weights.to(a1.dtype)) num_tokens, hidden_dim = a1.size() topk = topk_ids.size(1) tokens_per_expert = torch.zeros(num_experts, dtype=torch.int, device=a1.device) num_local_experts = self.num_local_experts if quant_config.quant_dtype is None: b_type = a1.dtype else: b_type = quant_config.quant_dtype b_a1 = torch.zeros( (num_local_experts, self.max_num_tokens, hidden_dim), dtype=b_type, device=a1.device, ) if quant_config.is_quantized: scale_shape = quant_config.batched_scale_shape( num_local_experts, self.max_num_tokens, hidden_dim ) b_a1_scale = torch.empty(scale_shape, dtype=torch.float32, device=a1.device) else: assert quant_config.a1_scale is None b_a1_scale = None first_expert = num_local_experts * self.rank last_expert = first_expert + num_local_experts a1_scale = normalize_scales_shape(quant_config.a1_scale) for expert_id in range(first_expert, last_expert): topks = torch.any(topk_ids == expert_id, dim=1).flatten() rows = torch.count_nonzero(topks.flatten()) if rows == 0: continue idx = expert_id - first_expert tokens_per_expert[idx] = rows rhs = a1[: topks.numel()][topks] if quant_config.quant_dtype is not None: if a1_scale is not None: if quant_config.is_per_act_token: rhs_a1_scale = a1_scale[: topks.numel()][topks] else: rhs_a1_scale = a1_scale else: rhs_a1_scale = None b_a1[idx, :rows, :], b_s = moe_kernel_quantize_input( rhs, rhs_a1_scale, quant_config.quant_dtype, quant_config.per_act_token_quant, quant_config.block_shape, ) assert b_s is not None if quant_config.is_per_act_token: b_a1_scale[idx, :rows] = b_s[:rows] else: b_a1_scale[idx, : b_s.shape[0]] = b_s else: b_a1[idx, :rows, :] = rhs assert b_a1_scale is None or b_a1_scale.ndim == 3 expert_tokens_meta = mk.ExpertTokensMetadata( expert_num_tokens=tokens_per_expert, expert_num_tokens_cpu=None ) return b_a1, b_a1_scale, expert_tokens_meta, None, None def finalize( self, output: torch.Tensor, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, weight_and_reduce_impl: mk.TopKWeightAndReduce, ) -> None: if isinstance(weight_and_reduce_impl, TopKWeightAndReduceDelegate): weight_and_reduce_impl = TopKWeightAndReduceNaiveBatched(self.rank) weight_and_reduce_impl.apply( output=output, fused_expert_output=fused_expert_output, topk_weights=topk_weights, topk_ids=topk_ids, apply_router_weight_on_input=apply_router_weight_on_input, ) class NaiveBatchedExperts(mk.FusedMoEPermuteExpertsUnpermute): """ A reference MoE expert class that operates on expert batched format, i.e. E x max_num_tokens x K. This is the format that the pplx dispatch/combine kernels use. """ def __init__( self, max_num_tokens: int, num_dispatchers: int, quant_config: FusedMoEQuantConfig, ): super().__init__(quant_config) assert not self.quant_config.use_int8_w8a8, "NYI" assert not self.quant_config.use_int8_w8a16, "NYI" assert not self.quant_config.use_int4_w4a16, "NYI" assert self.quant_config.ocp_mx_scheme is None, "NYI" self.max_num_tokens = max_num_tokens self.num_dispatchers = num_dispatchers @property def activation_formats( self, ) -> tuple[mk.FusedMoEActivationFormat, mk.FusedMoEActivationFormat]: return ( mk.FusedMoEActivationFormat.BatchedExperts, mk.FusedMoEActivationFormat.BatchedExperts, ) def supports_chunking(self) -> bool: return False def supports_expert_map(self) -> bool: return False def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce: # Let PrepareAndFinalize::finalize() decide the impl. return TopKWeightAndReduceDelegate() def workspace_shapes( self, M: int, N: int, K: int, topk: int, global_num_experts: int, local_num_experts: int, expert_tokens_meta: mk.ExpertTokensMetadata | None, ) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...]]: num_dp = self.num_dispatchers num_experts = local_num_experts workspace13 = (num_experts, self.max_num_tokens * num_dp, K) workspace2 = (self.max_num_tokens * num_dp, N) output = workspace13 return (workspace13, workspace2, output) def dequant(self, t: torch.Tensor, scale: torch.Tensor) -> torch.Tensor: assert self.quant_config.is_quantized f32 = torch.float32 if self.quant_config.is_per_act_token or self.quant_config.is_per_tensor: return t.to(f32) * scale else: return t.to(f32) * group_broadcast(scale, t.shape) def apply( self, output: torch.Tensor, hidden_states: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: str, global_num_experts: int, expert_map: torch.Tensor | None, a1q_scale: torch.Tensor | None, a2_scale: torch.Tensor | None, workspace13: torch.Tensor, workspace2: torch.Tensor, expert_tokens_meta: mk.ExpertTokensMetadata | None, apply_router_weight_on_input: bool, ): assert hidden_states.dim() == 3 assert expert_tokens_meta is not None expert_num_tokens = expert_tokens_meta.expert_num_tokens num_local_experts = w1.size(0) assert num_local_experts == w1.size(0), f"{num_local_experts} == {w1.size(0)}" N = w1.size(1) // 2 for expert in range(num_local_experts): # Indexing expert_num_tokens doesn't work w/cudagraphs or inductor if ( torch.compiler.is_compiling() or torch.cuda.is_current_stream_capturing() ): num = hidden_states.shape[1] else: num = int(expert_num_tokens[expert].item()) if num == 0: continue tmp = _resize_cache(workspace2, (num, N)) if self.quant_config.is_quantized: assert a1q_scale is not None and self.w1_scale is not None input = self.dequant(hidden_states[expert, :, :], a1q_scale[expert]) w1_dq = self.dequant(w1[expert], self.w1_scale[expert]) input = input[:num] @ w1_dq.transpose(0, 1) else: input = hidden_states[expert, :num, :] @ w1[expert].transpose(0, 1) self.activation(activation, tmp, input.to(tmp.dtype)) if self.quant_config.is_quantized: assert self.w2_scale is not None w2_dq = self.dequant(w2[expert], self.w2_scale[expert]) else: w2_dq = w2[expert] output[expert, :num, :] = tmp @ w2_dq.transpose(0, 1).to(tmp.dtype) def batched_moe_kernel_quantize_input( A: torch.Tensor, A_scale: torch.Tensor | None, num_tokens: int, E: int, N: int, expert_num_tokens: torch.Tensor, qtype: torch.dtype | None, per_act_token_quant: bool, block_shape: list[int] | None = None, ) -> tuple[torch.Tensor, torch.Tensor | None]: if torch.compiler.is_compiling() or torch.cuda.is_current_stream_capturing(): # Note: this does a bunch of extra work because expert_num_tokens is # ignored but it does support torch.compile + cudagraphs. hidden_dim = A.size(-1) assert A_scale is None or A_scale.ndim <= 2, ( f"{A_scale.shape if A_scale is not None else None}" ) A_q, A_q_scale = moe_kernel_quantize_input( A.view(-1, hidden_dim), A_scale, qtype, per_act_token_quant, block_shape ) A_q = A_q.view(E, -1, hidden_dim) A_q_scale = normalize_batched_scales_shape(A_q_scale, E) return A_q, A_q_scale elif qtype is None: return A, normalize_batched_scales_shape(A_scale, E) else: A_q = torch.empty_like(A, dtype=qtype) if per_act_token_quant: assert block_shape is None scale_shape = (E, num_tokens, 1) elif block_shape is not None: _, block_k = block_shape k_tiles = (A.shape[-1] + block_k - 1) // block_k scale_shape = (E, num_tokens, k_tiles) else: scale_shape = (E, 1, 1) A_q_scale = torch.zeros(scale_shape, dtype=torch.float32, device=A.device) num_experts = expert_num_tokens.numel() A_scale = normalize_batched_scales_shape(A_scale, num_experts) for e in range(E): num_tokens = int(expert_num_tokens[e].item()) if num_tokens > 0: if A_scale is not None: scales = A_scale[e, : min(num_tokens, A_scale.shape[1])] else: scales = None A_q[e, :num_tokens], tmp_scale = moe_kernel_quantize_input( A[e, :num_tokens], scales, qtype, per_act_token_quant, block_shape, ) assert tmp_scale is not None A_q_scale[e, : tmp_scale.shape[0]] = tmp_scale return A_q, A_q_scale class BatchedTritonExperts(mk.FusedMoEPermuteExpertsUnpermute): """ A Triton based MoE expert class that operates on expert batched format, i.e. E x max_num_tokens x K. This is the format that the pplx dispatch/combine kernels use. """ def __init__( self, max_num_tokens: int, num_dispatchers: int, quant_config: FusedMoEQuantConfig, ): super().__init__(quant_config) assert not self.quant_config.use_int8_w8a8, "NYI" assert not self.quant_config.use_int8_w8a16, "NYI" assert not self.quant_config.use_int4_w4a16, "NYI" assert self.quant_config.ocp_mx_scheme is None, "NYI" assert max_num_tokens > 0 assert num_dispatchers > 0 self.max_num_tokens = max_num_tokens self.num_dispatchers = num_dispatchers @property def activation_formats( self, ) -> tuple[mk.FusedMoEActivationFormat, mk.FusedMoEActivationFormat]: return ( mk.FusedMoEActivationFormat.BatchedExperts, mk.FusedMoEActivationFormat.BatchedExperts, ) def supports_chunking(self) -> bool: return False def supports_expert_map(self) -> bool: return False def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce: # Let PrepareAndFinalize::finalize() decide the impl. return TopKWeightAndReduceDelegate() def workspace_shapes( self, M: int, N: int, K: int, topk: int, global_num_experts: int, local_num_experts: int, expert_tokens_meta: mk.ExpertTokensMetadata | None, ) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...]]: num_dp = self.num_dispatchers num_experts = local_num_experts max_num_tokens = self.max_num_tokens workspace13 = (num_experts, max_num_tokens * num_dp, max(K, N)) workspace2 = (num_experts, max_num_tokens * num_dp, (N // 2)) output = (num_experts, max_num_tokens * num_dp, K) return (workspace13, workspace2, output) def apply( self, output: torch.Tensor, hidden_states: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: str, global_num_experts: int, expert_map: torch.Tensor | None, a1q_scale: torch.Tensor | None, a2_scale: torch.Tensor | None, workspace13: torch.Tensor, workspace2: torch.Tensor, expert_tokens_meta: mk.ExpertTokensMetadata | None, apply_router_weight_on_input: bool, ): # Check constraints. if self.quant_config.use_int4_w4a16: assert hidden_states.size(-1) // 2 == w1.size(2), "Hidden size mismatch" else: assert hidden_states.size(-1) == w1.size(2), ( f"Hidden size mismatch {hidden_states.size(-1)} != {w1.size(2)}" ) assert hidden_states.is_contiguous(), "Hidden_states must be contiguous" assert w1.stride(-1) == 1, "Stride of last dimension must be 1" assert w2.stride(-1) == 1, "Stride of last dimension must be 1" assert hidden_states.dtype in [ torch.float32, torch.float16, torch.bfloat16, torch.float8_e4m3fn, ] assert expert_tokens_meta is not None expert_num_tokens = expert_tokens_meta.expert_num_tokens E, max_num_tokens, N, K, top_k_num = self.moe_problem_size( hidden_states, w1, w2, topk_ids ) assert w1.size(0) == E assert w2.size(0) == E config_dtype = self.quant_config.config_name(hidden_states.dtype) config = try_get_optimal_moe_config( w1.size(), w2.size(), top_k_num, config_dtype, max_num_tokens, block_shape=self.block_shape, ) if hidden_states.dtype == torch.bfloat16: compute_type = tl.bfloat16 elif hidden_states.dtype == torch.float16: compute_type = tl.float16 elif hidden_states.dtype == torch.float32: compute_type = tl.float32 elif hidden_states.dtype == torch.float8_e4m3fn: compute_type = tl.bfloat16 else: raise ValueError(f"Unsupported compute_type: {hidden_states.dtype}") # We can reuse the memory between these because by the time we need # cache3, we're done with cache1 intermediate_cache1 = _resize_cache(workspace13, (E, max_num_tokens, N)) intermediate_cache2 = _resize_cache(workspace2, (E, max_num_tokens, N // 2)) # TODO(bnell): should this be done for any quantized type? if self.quant_config.use_fp8_w8a8: intermediate_cache1.fill_(0) a1q_scale = normalize_batched_scales_shape(a1q_scale, E) # MM1 invoke_moe_batched_triton_kernel( A=hidden_states, B=w1, C=intermediate_cache1, expert_num_tokens=expert_num_tokens, compute_type=compute_type, A_scale=a1q_scale, B_scale=self.w1_scale, B_zp=self.w1_zp, use_fp8_w8a8=self.quant_config.use_fp8_w8a8, use_int8_w8a16=self.quant_config.use_int8_w8a16, use_int4_w4a16=self.quant_config.use_int4_w4a16, config=config, per_act_token_quant=self.per_act_token_quant, block_shape=self.block_shape, ) intermediate_cache2.fill_(0) # TODO (bnell): use triton utility from batched deep gemm. self.activation( activation, intermediate_cache2.view(-1, N // 2), intermediate_cache1.view(-1, N), ) qintermediate_cache2, a2q_scale = batched_moe_kernel_quantize_input( intermediate_cache2, a2_scale, max_num_tokens, E, N, expert_num_tokens, self.quant_dtype, self.per_act_token_quant, self.block_shape, ) invoke_moe_batched_triton_kernel( A=qintermediate_cache2, B=w2, C=output, expert_num_tokens=expert_num_tokens, compute_type=compute_type, A_scale=a2q_scale, B_scale=self.w2_scale, B_zp=self.w2_zp, use_fp8_w8a8=self.quant_config.use_fp8_w8a8, use_int8_w8a16=self.quant_config.use_int8_w8a16, use_int4_w4a16=self.quant_config.use_int4_w4a16, config=config, per_act_token_quant=self.per_act_token_quant, block_shape=self.block_shape, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/flashinfer_cutedsl_moe.py
vllm/model_executor/layers/fused_moe/flashinfer_cutedsl_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm import envs from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe.config import FusedMoEQuantConfig from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import ( TopKWeightAndReduceDelegate, ) from vllm.utils.flashinfer import ( flashinfer_cutedsl_grouped_gemm_nt_masked, has_flashinfer_cutedsl_grouped_gemm_nt_masked, scaled_fp4_grouped_quantize, silu_and_mul_scaled_nvfp4_experts_quantize, ) logger = init_logger(__name__) def is_valid_flashinfer_cutedsl_fused_moe( hidden_states: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor ) -> bool: """ Check if the given problem size is supported by the FlashInfer CuteDSL MoE kernel. """ if not has_flashinfer_cutedsl_grouped_gemm_nt_masked(): logger.debug_once( "FlashInferCuteDSLExperts disabled: " "flashinfer_cutedsl_fused_moe not available." ) return False # Data type checks if ( w1.dtype != torch.uint8 or w2.dtype != torch.uint8 or hidden_states.dtype not in [torch.float32, torch.float16, torch.bfloat16] ): logger.debug_once( "FlashInferCuteDSLExperts disabled: w1/w2 must be torch.uint8 " f"(got w1={w1.dtype}, w2={w2.dtype}), hidden_states must be " f"float32, float16, or bfloat16 (got {hidden_states.dtype})." ) return False return True class FlashInferCuteDSLExperts(mk.FusedMoEPermuteExpertsUnpermute): def __init__( self, out_dtype: torch.dtype, quant_config: FusedMoEQuantConfig, ): super().__init__(quant_config) assert quant_config.quant_dtype == "nvfp4", ( "Only nvfp4 quantization are currently supported." ) self.out_dtype = out_dtype @property def activation_formats( self, ) -> tuple[mk.FusedMoEActivationFormat, mk.FusedMoEActivationFormat]: return ( mk.FusedMoEActivationFormat.BatchedExperts, mk.FusedMoEActivationFormat.BatchedExperts, ) def supports_expert_map(self) -> bool: return False def supports_chunking(self) -> bool: # This refers to TP chunking; DP chunking is handled separately. # TODO(shuw@nvidia.com): Set to False to be consistent with # batched_deep_gemm_moe return False def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce: # Let PrepareAndFinalize::finalize() decide the impl. return TopKWeightAndReduceDelegate() def workspace_shapes( self, M: int, N: int, K: int, topk: int, global_num_experts: int, local_num_experts: int, expert_tokens_meta: mk.ExpertTokensMetadata | None, ) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...]]: # We use global_num_experts due to how moe_align_block_size handles # expert_maps. """ Compute the shapes for the temporary and final outputs of the two gemms and activation in the fused expert function. Since the gemms are independent, the workspace for the first gemm can be shared with the workspace for the last gemm. Returns a tuple of: - workspace13 shape tuple: must be large enough to hold the result of either expert gemm. - workspace2 shape tuple: must be large enough to hold the result of the activation function. - output shape tuple: must be exact size of the final gemm output. - Workspace type: The dtype to use for the workspace tensors. - Note: in order for activation chunking to work, the first dimension of each tuple must be the number of tokens. """ K_dim = K * 2 if envs.VLLM_DEEPEPLL_NVFP4_DISPATCH else K output_shape = (local_num_experts, M, K_dim) workspace2 = (local_num_experts, M, N) workspace1 = output_shape return (workspace1, workspace2, output_shape) def apply( self, output: torch.Tensor, hidden_states: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: str, global_num_experts: int, expert_map: torch.Tensor | None, a1q_scale: torch.Tensor | None, a2_scale: torch.Tensor | None, # Not used workspace13: torch.Tensor | None, workspace2: torch.Tensor | None, expert_tokens_meta: mk.ExpertTokensMetadata | None, apply_router_weight_on_input: bool | None, ): assert self.quant_dtype == "nvfp4", ( "Only nvfp4 quantization are currently supported." ) # Ensure w1_scale and w2_scale are not None before calling view assert self.w1_scale is not None and self.w2_scale is not None, ( "w1_scale and w2_scale must not be None for FlashInferExperts" ) assert expert_tokens_meta is not None expert_num_tokens = expert_tokens_meta.expert_num_tokens assert hidden_states.ndim == 3 assert self.w1_scale.ndim == 3 assert self.w2_scale.ndim == 3 input_global_scale = ( None if envs.VLLM_DEEPEPLL_NVFP4_DISPATCH else self.a1_gscale ) flashinfer_hidden_states = ( (hidden_states, a1q_scale) if envs.VLLM_DEEPEPLL_NVFP4_DISPATCH else hidden_states ) flashinfer_cutedsl_moe_masked( hidden_states=flashinfer_hidden_states, input_global_scale=input_global_scale, w1=w1, w1_blockscale=self.w1_scale, w1_alpha=self.g1_alphas, w2=w2, a2_global_scale=self.a2_gscale, w2_blockscale=self.w2_scale, w2_alpha=self.g2_alphas, masked_m=expert_num_tokens, workspace=workspace2, out=output, ) def get_cute_dtype(input: torch.Tensor) -> str: if input.dtype == torch.bfloat16: return "bfloat16" elif input.dtype == torch.float16: return "float16" elif input.dtype == torch.float32: return "float32" else: raise ValueError(f"Unsupported cute dtype {input.dtype}") def flashinfer_cutedsl_moe_masked( hidden_states: torch.Tensor | tuple[torch.Tensor, torch.Tensor], input_global_scale: torch.Tensor, w1: torch.Tensor, w1_blockscale: torch.Tensor, w1_alpha, w2: torch.Tensor, a2_global_scale: torch.Tensor, w2_blockscale: torch.Tensor, w2_alpha, masked_m: torch.Tensor, workspace: torch.Tensor, out: torch.Tensor, ): """ Perform masked Mixture-of-Experts computation with FlashInfer's CuteDSL kernels. Args: hidden_states: Either of the following case * torch.Tensor: [num_experts, m, k], bf16 * tuple[torch.Tensor, torch.Tensor]: [num_experts, m, k // 2], uint8, [num_experts, m, k // 16], float8_e4m3fn input_global_scale (torch.Tensor): (l,) w1 (torch.Tensor): fp4 weights, [l, 2 * n, k // 2], uint8 w1_blockscale (torch.Tensor): blockscale factors, e4m3, w1_alpha (torch.Tensor): (l,) w2 (torch.Tensor): fp4 weights, [l, k, n // 2], uint8 a2_global_scale (torch.Tensor): (l,) w2_blockscale (torch.Tensor): blockscale factors, e4m3, w2_alpha (torch.Tensor): (l,) masked_m (torch.Tensor): Masked dimension indices workspace (torch.Tensor): For gateup_output Notes: - Assumes max(masked_m) <= m. """ # === Assertions on dtypes === assert w1.dtype == torch.uint8, f"w1 must be uint8, got {w1.dtype}" assert w1_blockscale.dtype == torch.float8_e4m3fn, ( f"w1_blockscale must be float8_e4m3fn, got {w1_blockscale.dtype}" ) assert w1_alpha.dtype == torch.float32, ( f"w1_alpha must be float32, got {w1_alpha.dtype}" ) assert w2.dtype == torch.uint8, f"w2 must be uint8, got {w2.dtype}" assert a2_global_scale.dtype == torch.float32, ( f"a2_global_scale must be float32, got {a2_global_scale.dtype}" ) assert w2_blockscale.dtype == torch.float8_e4m3fn, ( f"w2_blockscale must be float8_e4m3fn, got {w2_blockscale.dtype}" ) assert w2_alpha.dtype == torch.float32, ( f"w2_alpha must be float32, got {w2_alpha.dtype}" ) # === Assertions on shapes === n = w2.shape[-1] * 2 # intermediate dimension if isinstance(hidden_states, tuple): assert input_global_scale is None, ( "input_global_scale is needed when input needs quant" ) aq = hidden_states[0].view(torch.uint8) aq_sf = hidden_states[1].view(torch.float8_e4m3fn) # m, k_by_2, num_experts = aq.shape num_experts, m, k_by_2 = aq.shape k = k_by_2 * 2 aq = aq.permute(1, 2, 0) else: num_experts, m, k = hidden_states.shape assert input_global_scale.dtype == torch.float32, ( f"input_global_scale must be float32, got {input_global_scale.dtype}" ) assert input_global_scale.shape == (num_experts,), ( f"input_global_scale must be (l,), got {input_global_scale.shape}" ) aq, aq_sf = scaled_fp4_grouped_quantize( hidden_states, masked_m, input_global_scale, ) assert w1.shape[-2] == 2 * n, f"w1 last-2 dim must be 2*n, got {w1.shape}" assert w1.shape[-1] * 2 == k, ( f"w1 last dim * 2 must equal k, got {w1.shape[-1]} vs k={k}" ) assert w2.shape[-2:] == ( k, n // 2, ), f"w2 shape mismatch, got {w2.shape[-2:]}, expected {(k, n // 2)}" assert w1_alpha.shape == (num_experts,), ( f"w1_alpha must be (l,), got {w1_alpha.shape}" ) assert a2_global_scale.shape == (num_experts,), ( f"a2_global_scale must be (l,), got {a2_global_scale.shape}" ) assert w2_alpha.shape == (num_experts,), ( f"w2_alpha must be (l,), got {w2_alpha.shape}" ) workspace = workspace.permute(1, 2, 0) # requirement of kernel sf_vec_size = 16 assert aq_sf.dtype == torch.float8_e4m3fn assert aq.dtype == torch.uint8 ab_dtype = "float4_e2m1fn" sf_dtype = "float8_e4m3fn" if isinstance(hidden_states, tuple): c_dtype = "bfloat16" else: c_dtype = get_cute_dtype(hidden_states) # Gemm1 flashinfer_cutedsl_grouped_gemm_nt_masked( (aq, aq_sf), (w1.permute(1, 2, 0), w1_blockscale), workspace, masked_m, ab_dtype=ab_dtype, sf_dtype=sf_dtype, c_dtype=c_dtype, sf_vec_size=sf_vec_size, alpha=w1_alpha.view(1, 1, num_experts), alpha_dtype=get_cute_dtype(w1_alpha), ) # in logical [m, n, l] # SILU and quantization diq, diq_sf = silu_and_mul_scaled_nvfp4_experts_quantize( workspace.permute(2, 0, 1), masked_m, a2_global_scale, ) # Gemm2 out = out.permute(1, 2, 0) # requirement of kernel flashinfer_cutedsl_grouped_gemm_nt_masked( (diq, diq_sf), (w2.permute(1, 2, 0), w2_blockscale), out, masked_m, ab_dtype=ab_dtype, sf_dtype=sf_dtype, c_dtype=c_dtype, sf_vec_size=sf_vec_size, alpha=w2_alpha.view(1, 1, num_experts), alpha_dtype=get_cute_dtype(w2_alpha), ) # in logical [m, k, l] out = out.permute(2, 0, 1) def flashinfer_cutedsl_moe_fp4( hidden_states: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, quant_config: FusedMoEQuantConfig, inplace: bool = False, activation: str = "silu", global_num_experts: int = -1, expert_map: torch.Tensor | None = None, apply_router_weight_on_input: bool = False, ) -> torch.Tensor: from vllm.model_executor.layers.fused_moe.flashinfer_cutlass_prepare_finalize import ( # noqa: E501 create_flashinfer_prepare_finalize, ) fused_experts = mk.FusedMoEModularKernel( create_flashinfer_prepare_finalize(use_dp=False), # could be swapped later FlashInferCuteDSLExperts( out_dtype=hidden_states.dtype, quant_config=quant_config, ), ) return fused_experts( hidden_states=hidden_states, w1=w1, w2=w2, topk_weights=topk_weights, topk_ids=topk_ids, inplace=inplace, activation=activation, global_num_experts=global_num_experts, expert_map=expert_map, apply_router_weight_on_input=apply_router_weight_on_input, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/deep_gemm_utils.py
vllm/model_executor/layers/fused_moe/deep_gemm_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ Taken from https://github.com/ModelTC/LightLLM/blob/8ed97c74c18f11505b048b1ba00ba5c0cef8bff6/lightllm/common/fused_moe/deepep_scatter_gather.py and updated to fit vllm needs and terminology. """ import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm.model_executor.layers.fused_moe.utils import count_expert_num_tokens from vllm.triton_utils import tl, triton from vllm.utils.deep_gemm import get_mk_alignment_for_contiguous_layout from vllm.utils.math_utils import round_up def expert_num_tokens_round_up_and_sum( expert_num_tokens: torch.Tensor, alignment: int ) -> int: # Round up each element in expert_num_tokens to the nearest multiple of # alignment. ent = (expert_num_tokens.to(torch.int64) + (alignment - 1)) // alignment * alignment return torch.sum(ent).item() def compute_aligned_M( M: int, num_topk: int, local_num_experts: int, alignment: int, expert_tokens_meta: mk.ExpertTokensMetadata | None, ): if (expert_tokens_meta is not None) and ( expert_tokens_meta.expert_num_tokens_cpu is not None ): return expert_num_tokens_round_up_and_sum( expert_tokens_meta.expert_num_tokens_cpu, alignment=alignment ) # expert_num_tokens information is not available on the cpu. # compute the max required size. M_sum = (M * num_topk) + local_num_experts * (alignment - 1) M_sum = round_up(M_sum, alignment) return M_sum @triton.jit def apply_expert_map(expert_id, expert_map): if expert_id != -1: expert_id = tl.load(expert_map + expert_id).to(expert_id.dtype) return expert_id @triton.jit def round_up_128(x: int) -> int: y = 128 return ((x + y - 1) // y) * y @triton.jit def _fwd_kernel_ep_scatter_1( num_recv_tokens_per_expert, expert_start_loc, m_indices, num_experts: tl.constexpr, BLOCK_E: tl.constexpr, BLOCK_EXPERT_NUM: tl.constexpr, ): cur_expert = tl.program_id(0) offset_cumsum = tl.arange(0, BLOCK_EXPERT_NUM) tokens_per_expert = tl.load( num_recv_tokens_per_expert + offset_cumsum, mask=offset_cumsum < num_experts, other=0, ) tokens_per_expert = round_up_128(tokens_per_expert) cumsum = tl.cumsum(tokens_per_expert) - tokens_per_expert tl.store(expert_start_loc + offset_cumsum, cumsum, mask=offset_cumsum < num_experts) cur_expert_start = tl.load(expert_start_loc + cur_expert) cur_expert_token_num = tl.load(num_recv_tokens_per_expert + cur_expert) m_indices_start_ptr = m_indices + cur_expert_start off_expert = tl.arange(0, BLOCK_E) # any rows in the per-expert aligned region that do not correspond to # real tokens are left untouched here and should remain initialized to # -1 so DeepGEMM can skip them for start_m in tl.range(0, cur_expert_token_num, BLOCK_E, num_stages=4): offs = start_m + off_expert mask = offs < cur_expert_token_num tl.store( m_indices_start_ptr + offs, cur_expert, mask=mask, ) @triton.jit def _fwd_kernel_ep_scatter_2( total_token_num, expert_start_loc, recv_x, recv_x_stride0, recv_x_stride1, recv_x_scale, recv_x_scale_stride0, recv_x_scale_stride1, recv_topk, recv_topk_stride0, recv_topk_stride1, output_tensor, output_tensor_stride0, output_tensor_stride1, output_tensor_scale, output_tensor_scale_stride0, output_tensor_scale_stride1, output_index, output_index_stride0, output_index_stride1, topk_num: tl.constexpr, expert_map, HAS_EXPERT_MAP: tl.constexpr, HIDDEN_SIZE: tl.constexpr, HIDDEN_SIZE_PAD: tl.constexpr, SCALE_HIDDEN_SIZE: tl.constexpr, SCALE_HIDDEN_SIZE_PAD: tl.constexpr, ): start_token_id = tl.program_id(0) grid_num = tl.num_programs(0) offset_in = tl.arange(0, HIDDEN_SIZE_PAD) mask = offset_in < HIDDEN_SIZE offset_in_s = tl.arange(0, SCALE_HIDDEN_SIZE_PAD) mask_s = offset_in_s < SCALE_HIDDEN_SIZE for token_id in range(start_token_id, total_token_num, grid_num): to_copy = tl.load(recv_x + token_id * recv_x_stride0 + offset_in, mask=mask) to_copy_s = tl.load( recv_x_scale + token_id * recv_x_scale_stride0 + offset_in_s, mask=mask_s ) for topk_index in tl.range(0, topk_num, 1, num_stages=4): expert_id = tl.load(recv_topk + token_id * recv_topk_stride0 + topk_index) if HAS_EXPERT_MAP: expert_id = apply_expert_map(expert_id, expert_map) if expert_id >= 0: dest_token_index = tl.atomic_add(expert_start_loc + expert_id, 1) tl.store( output_index + token_id * output_index_stride0 + topk_index, dest_token_index, ) output_tensor_ptr = ( output_tensor + dest_token_index * output_tensor_stride0 ) output_tensor_scale_ptr = ( output_tensor_scale + dest_token_index * output_tensor_scale_stride0 ) tl.store(output_tensor_ptr + offset_in, to_copy, mask=mask) tl.store(output_tensor_scale_ptr + offset_in_s, to_copy_s, mask=mask_s) @torch.no_grad() def ep_scatter( recv_x: torch.Tensor, recv_x_scale: torch.Tensor, recv_topk: torch.Tensor, num_recv_tokens_per_expert: torch.Tensor, expert_map: torch.Tensor | None, expert_start_loc: torch.Tensor, output_tensor: torch.Tensor, output_tensor_scale: torch.Tensor, m_indices: torch.Tensor, output_index: torch.Tensor, ): BLOCK_E = 128 # token num of per expert is aligned to 128 BLOCK_D = 128 # block size of quantization num_warps = 8 num_experts = num_recv_tokens_per_expert.shape[0] hidden_size = recv_x.shape[1] # grid = (triton.cdiv(hidden_size, BLOCK_D), num_experts) grid = num_experts assert m_indices.shape[0] % BLOCK_E == 0 _fwd_kernel_ep_scatter_1[(grid,)]( num_recv_tokens_per_expert, expert_start_loc, m_indices, num_experts=num_experts, num_warps=num_warps, BLOCK_E=BLOCK_E, BLOCK_EXPERT_NUM=triton.next_power_of_2(num_experts), ) grid = min(recv_topk.shape[0], 1024 * 8) _fwd_kernel_ep_scatter_2[(grid,)]( recv_topk.shape[0], expert_start_loc, recv_x, recv_x.stride(0), recv_x.stride(1), recv_x_scale, recv_x_scale.stride(0), recv_x_scale.stride(1), recv_topk, recv_topk.stride(0), recv_topk.stride(1), output_tensor, output_tensor.stride(0), output_tensor.stride(1), output_tensor_scale, output_tensor_scale.stride(0), output_tensor_scale.stride(1), output_index, output_index.stride(0), output_index.stride(1), topk_num=recv_topk.shape[1], expert_map=expert_map, HAS_EXPERT_MAP=expert_map is not None, num_warps=num_warps, HIDDEN_SIZE=hidden_size, HIDDEN_SIZE_PAD=triton.next_power_of_2(hidden_size), SCALE_HIDDEN_SIZE=hidden_size // BLOCK_D, SCALE_HIDDEN_SIZE_PAD=triton.next_power_of_2(hidden_size // BLOCK_D), ) return @triton.jit def _fwd_kernel_ep_gather( total_token_num, input_tensor, input_tensor_stride0, input_tensor_stride1, recv_topk_ids, recv_topk_ids_stride0, recv_topk_ids_stride1, recv_topk_weight, recv_topk_weight_stride0, recv_topk_weight_stride1, input_index, input_index_stride0, input_index_stride1, output_tensor, output_tensor_stride0, output_tensor_stride1, topk_num: tl.constexpr, expert_map, HAS_EXPERT_MAP: tl.constexpr, BLOCK_D: tl.constexpr, ): cur_block = tl.program_id(0) start_cur_token = tl.program_id(1) grid_num = tl.num_programs(1) for cur_token in range(start_cur_token, total_token_num, grid_num): off_d = tl.arange(0, BLOCK_D) accumulator = tl.zeros([BLOCK_D], dtype=tl.float32) for topk_index in range(0, topk_num): expert_id = tl.load( recv_topk_ids + cur_token * recv_topk_ids_stride0 + topk_index ) if HAS_EXPERT_MAP: expert_id = apply_expert_map(expert_id, expert_map) if expert_id >= 0: source_token_index = tl.load( input_index + cur_token * input_index_stride0 + topk_index ) acc_weight = tl.load( recv_topk_weight + cur_token * recv_topk_weight_stride0 + topk_index ) tmp = tl.load( input_tensor + source_token_index * input_tensor_stride0 + cur_block * BLOCK_D + off_d ) accumulator += tmp.to(tl.float32) * acc_weight tl.store( output_tensor + cur_token * output_tensor_stride0 + cur_block * BLOCK_D + off_d, accumulator.to(output_tensor.dtype.element_ty), ) @torch.no_grad() def ep_gather( input_tensor: torch.Tensor, recv_topk_ids: torch.Tensor, recv_topk_weight: torch.Tensor, input_index: torch.Tensor, expert_map: torch.Tensor | None, output_tensor: torch.Tensor, ): num_warps = 2 num_tokens = output_tensor.shape[0] hidden_size = input_tensor.shape[1] BLOCK_D = min(hidden_size, 1024) assert hidden_size % BLOCK_D == 0 grid = (triton.cdiv(hidden_size, BLOCK_D), min(num_tokens, 1024)) _fwd_kernel_ep_gather[grid]( num_tokens, input_tensor, input_tensor.stride(0), input_tensor.stride(1), recv_topk_ids, recv_topk_ids.stride(0), recv_topk_ids.stride(1), recv_topk_weight, recv_topk_weight.stride(0), recv_topk_weight.stride(1), input_index, input_index.stride(0), input_index.stride(1), output_tensor, output_tensor.stride(0), output_tensor.stride(1), topk_num=recv_topk_ids.shape[1], expert_map=expert_map, HAS_EXPERT_MAP=expert_map is not None, num_warps=num_warps, BLOCK_D=BLOCK_D, ) return def deepgemm_moe_permute( aq: torch.Tensor, aq_scale: torch.Tensor, topk_ids: torch.Tensor, local_num_experts: int, expert_map: torch.Tensor | None, expert_tokens_meta: mk.ExpertTokensMetadata | None, aq_out: torch.Tensor | None = None, ): assert aq.ndim == 2 assert topk_ids.dtype.is_signed, "The kernel uses -1 to represent invalid topk_ids" H = aq.size(1) device = aq.device block_m, block_k = get_mk_alignment_for_contiguous_layout() M_sum = compute_aligned_M( M=topk_ids.size(0), num_topk=topk_ids.size(1), local_num_experts=local_num_experts, alignment=block_m, expert_tokens_meta=expert_tokens_meta, ) expert_start_loc = torch.empty( (local_num_experts), device=device, dtype=torch.int32 ) assert aq_out is None or aq_out.shape == (M_sum, H) if aq_out is None: aq_out = torch.empty((M_sum, H), device=device, dtype=aq.dtype) aq_scale_out = torch.empty( (M_sum, H // block_k), device=device, dtype=torch.float32 ) # DeepGEMM uses negative values in m_indices (here expert_ids) to mark # completely invalid / padded blocks that should be skipped. We always # initialize expert_ids to -1 so any row that is not explicitly written # by the scatter kernel will be treated as invalid and skipped by # DeepGEMM's scheduler. expert_ids = torch.full( (M_sum,), fill_value=-1, device=device, dtype=torch.int32, ) inv_perm = torch.empty(topk_ids.shape, device=device, dtype=torch.int32) expert_num_tokens = None if expert_tokens_meta is not None: expert_num_tokens = expert_tokens_meta.expert_num_tokens else: expert_num_tokens = count_expert_num_tokens( topk_ids, local_num_experts, expert_map ) ep_scatter( recv_x=aq, recv_x_scale=aq_scale, recv_topk=topk_ids, num_recv_tokens_per_expert=expert_num_tokens, expert_start_loc=expert_start_loc, expert_map=expert_map, output_tensor=aq_out, output_tensor_scale=aq_scale_out, m_indices=expert_ids, output_index=inv_perm, ) return aq_out, aq_scale_out, expert_ids, inv_perm def deepgemm_unpermute_and_reduce( a: torch.Tensor, # Grouped gemm output topk_ids: torch.Tensor, topk_weights: torch.Tensor, inv_perm: torch.Tensor, expert_map: torch.Tensor | None, output: torch.Tensor, ): return ep_gather( input_tensor=a, recv_topk_ids=topk_ids, recv_topk_weight=topk_weights, input_index=inv_perm, expert_map=expert_map, output_tensor=output, )
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/flashinfer_cutlass_prepare_finalize.py
vllm/model_executor/layers/fused_moe/flashinfer_cutlass_prepare_finalize.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm.distributed import get_dp_group, get_ep_group from vllm.distributed.device_communicators.base_device_communicator import ( All2AllManagerBase, ) from vllm.forward_context import get_forward_context from vllm.model_executor.layers.fused_moe.config import FusedMoEQuantConfig from vllm.model_executor.layers.fused_moe.prepare_finalize import ( MoEPrepareAndFinalizeNoEP, ) from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import ( TopKWeightAndReduceNoOP, ) from vllm.model_executor.layers.fused_moe.utils import moe_kernel_quantize_input from vllm.utils.flashinfer import nvfp4_block_scale_interleave def get_local_sizes(): return get_forward_context().dp_metadata.get_chunk_sizes_across_dp_rank() class FlashInferCutlassMoEPrepareAndFinalize(mk.FusedMoEPrepareAndFinalize): """Base class for FlashInfer MoE prepare and finalize operations.""" def __init__( self, use_dp: bool, num_dispatchers: int = 1, use_deepseek_fp8_block_scale: bool = False, ): super().__init__() self.num_dispatchers_ = num_dispatchers self.use_dp = use_dp self.local_tokens = None # Toggle for DeepSeek-style FP8 block-scale path where activations are # not quantized here and weight block scales are consumed by the kernel. self.use_deepseek_fp8_block_scale = use_deepseek_fp8_block_scale @property def activation_format(self) -> mk.FusedMoEActivationFormat: return mk.FusedMoEActivationFormat.Standard def max_num_tokens_per_rank(self) -> int | None: return None def topk_indices_dtype(self) -> torch.dtype | None: return None def num_dispatchers(self) -> int: return self.num_dispatchers_ def output_is_reduced(self) -> bool: return False def _apply_router_weight_on_input( self, a1: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, ) -> None: """Apply router weight on input if needed.""" if apply_router_weight_on_input: topk = topk_ids.size(1) assert topk == 1, ( "apply_router_weight_on_input is only implemented for topk=1" ) a1.mul_(topk_weights.to(a1.dtype)) class FlashInferAllToAllMoEPrepareAndFinalize(FlashInferCutlassMoEPrepareAndFinalize): """FlashInfer implementation using AllToAll communication.""" def __init__( self, use_dp: bool, num_dispatchers: int = 1, use_deepseek_fp8_block_scale: bool = False, ): super().__init__(use_dp, num_dispatchers, use_deepseek_fp8_block_scale) self.alltoall_info = None # Initialize all2all_manager only for DP case self.all2all_manager = None if self.use_dp: self.all2all_manager = get_ep_group().device_communicator.all2all_manager def prepare( self, a1: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, num_experts: int, expert_map: torch.Tensor | None, apply_router_weight_on_input: bool, quant_config: FusedMoEQuantConfig, ) -> mk.PrepareResultType: self._apply_router_weight_on_input( a1, topk_weights, topk_ids, apply_router_weight_on_input ) if not self.use_dp: # Non-DP case: quantize activations unless using block-scale path if not self.use_deepseek_fp8_block_scale: a1q, a1q_scale = moe_kernel_quantize_input( a1, quant_config.a1_gscale, quant_config.quant_dtype, quant_config.per_act_token_quant, quant_config.block_shape, is_fp4_scale_swizzled=not self.use_dp, ) else: a1q = a1 a1q_scale = None else: # DP case: use FlashInfer AllToAll global_num_tokens_cpu = get_local_sizes() top_k = topk_ids.size(1) (self.alltoall_info, topk_ids, topk_weights, a1q, a1q_scale) = ( flashinfer_alltoall_dispatch( self.all2all_manager, global_num_tokens_cpu, a1, quant_config.a1_gscale, topk_ids, topk_weights, top_k, num_experts, quant_config, use_deepseek_fp8_block_scale=self.use_deepseek_fp8_block_scale, ) ) return a1q, a1q_scale, None, topk_ids, topk_weights def finalize( self, output: torch.Tensor, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, weight_and_reduce_impl: mk.TopKWeightAndReduce, ) -> None: if self.use_dp: top_k = topk_ids.size(1) token_count = output.shape[0] fused_expert_output = flashinfer_alltoall_combine( self.all2all_manager, fused_expert_output, top_k=top_k, token_count=token_count, alltoall_info=self.alltoall_info, ) output.copy_(fused_expert_output) class FlashInferAllGatherMoEPrepareAndFinalize(FlashInferCutlassMoEPrepareAndFinalize): def __init__( self, use_dp: bool, num_dispatchers: int = 1, use_deepseek_fp8_block_scale: bool = False, ): super().__init__(use_dp, num_dispatchers, use_deepseek_fp8_block_scale) def prepare( self, a1: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, num_experts: int, expert_map: torch.Tensor | None, apply_router_weight_on_input: bool, quant_config: FusedMoEQuantConfig, ) -> mk.PrepareResultType: self._apply_router_weight_on_input( a1, topk_weights, topk_ids, apply_router_weight_on_input ) if not self.use_dp and quant_config.quant_dtype == "nvfp4": return a1, None, None, topk_ids, topk_weights if not self.use_deepseek_fp8_block_scale: a1q, a1q_scale = moe_kernel_quantize_input( a1, quant_config.a1_gscale, quant_config.quant_dtype, quant_config.per_act_token_quant, quant_config.block_shape, is_fp4_scale_swizzled=not self.use_dp, ) else: # Block-scale path: pass activations through, omit per-token scales a1q = a1 a1q_scale = None if self.use_dp: # Build gather list conditionally - omit a1q_scale if None # (block-scale path) gather_list = [topk_weights, topk_ids, a1q] if a1q_scale is not None: gather_list.append(a1q_scale) gathered = get_dp_group().all_gatherv( gather_list, dim=0, sizes=get_local_sizes(), ) topk_weights, topk_ids, a1q, a1q_scale = gathered else: gathered = get_dp_group().all_gatherv( gather_list, dim=0, sizes=get_local_sizes(), ) topk_weights, topk_ids, a1q = gathered a1q_scale = None if quant_config.quant_dtype == "nvfp4" and a1q_scale is not None: a1q_scale = nvfp4_block_scale_interleave(a1q_scale) return a1q, a1q_scale, None, topk_ids, topk_weights def finalize( self, output: torch.Tensor, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, weight_and_reduce_impl: mk.TopKWeightAndReduce, ) -> None: assert isinstance(weight_and_reduce_impl, TopKWeightAndReduceNoOP) if self.use_dp: fused_expert_output = get_dp_group().reduce_scatterv( fused_expert_output, dim=0, sizes=get_local_sizes() ) output.copy_(fused_expert_output) def flashinfer_alltoall_dispatch( all2all_manager: All2AllManagerBase, global_num_tokens_cpu: list[int], x: torch.Tensor, gs: torch.Tensor, topk_ids: torch.Tensor, topk_weights: torch.Tensor, top_k: int, num_experts: int, quant_config: FusedMoEQuantConfig, use_deepseek_fp8_block_scale: bool = False, ): from flashinfer.comm.trtllm_alltoall import MnnvlMoe assert all2all_manager.ensure_alltoall_workspace_initialized(), ( "FlashInfer AllToAll workspace not available" ) ep_rank = all2all_manager.rank ep_size = all2all_manager.world_size max_num_token = ( max(global_num_tokens_cpu) if global_num_tokens_cpu is not None else x.shape[0] ) orig_topk_weights_dtype = topk_weights.dtype alltoall_info, topk_ids, topk_weights, _ = ( MnnvlMoe.mnnvl_moe_alltoallv_prepare_without_allgather( topk_ids, topk_weights, None, all2all_manager.prepare_workspace_tensor, max_num_token, ep_rank, ep_size, num_experts, num_experts, top_k, ) ) topk_weights = topk_weights.view(dtype=orig_topk_weights_dtype) if not use_deepseek_fp8_block_scale: x, x_sf = moe_kernel_quantize_input( x, gs, quant_config.quant_dtype, quant_config.per_act_token_quant, quant_config.block_shape, is_fp4_scale_swizzled=False, # delay swizzle to after comm ) x = MnnvlMoe.mnnvl_moe_alltoallv( x, alltoall_info, all2all_manager.workspace_tensor, ep_rank, ep_size, ) x_sf = MnnvlMoe.mnnvl_moe_alltoallv( x_sf, alltoall_info, all2all_manager.workspace_tensor, ep_rank, ep_size, ) if quant_config.quant_dtype == "nvfp4": x_sf = nvfp4_block_scale_interleave(x_sf) else: # Block-scale path: pass activations through without quantization x_sf = None x = MnnvlMoe.mnnvl_moe_alltoallv( x, alltoall_info, all2all_manager.workspace_tensor, ep_rank, ep_size, ) return alltoall_info, topk_ids, topk_weights, x, x_sf def flashinfer_alltoall_combine( all2all_manager: All2AllManagerBase, output: torch.Tensor, top_k: int, token_count: int, alltoall_info, ): from flashinfer.comm.trtllm_alltoall import MnnvlMoe assert all2all_manager.ensure_alltoall_workspace_initialized(), ( "FlashInfer AllToAll workspace not available" ) return MnnvlMoe.mnnvl_moe_alltoallv_combine( output, alltoall_info, all2all_manager.workspace_tensor, ep_rank=all2all_manager.rank, ep_size=all2all_manager.world_size, top_k=top_k, token_count=token_count, ) def create_flashinfer_prepare_finalize( use_dp: bool, use_nvfp4: bool = False, enable_alltoallv: bool = False, use_deepseek_fp8_block_scale: bool = False, ) -> FlashInferCutlassMoEPrepareAndFinalize | MoEPrepareAndFinalizeNoEP: """Factory function to create the appropriate FlashInfer implementation.""" # TODO(rob): migrate non-DP cases to MoEPrepareAndFinalizeNoEP # once we complete the FP8 refactor. if use_nvfp4: if enable_alltoallv: return FlashInferAllToAllMoEPrepareAndFinalize(use_dp) else: return FlashInferAllGatherMoEPrepareAndFinalize(use_dp) # FP8 DP path currently supported via AllGather. if use_dp: return FlashInferAllGatherMoEPrepareAndFinalize( use_dp=True, use_deepseek_fp8_block_scale=use_deepseek_fp8_block_scale, ) else: # NOTE(rob): CUTLASS FP8 block quant executes the input # quantzation and grouped gemm in a single kernel. return MoEPrepareAndFinalizeNoEP(defer_input_quant=use_deepseek_fp8_block_scale)
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/fused_moe_modular_method.py
vllm/model_executor/layers/fused_moe/fused_moe_modular_method.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm.logger import init_logger from vllm.model_executor.custom_op import CustomOp from vllm.model_executor.layers.fused_moe.config import ( FusedMoEQuantConfig, ) from vllm.model_executor.layers.fused_moe.fused_moe_method_base import ( FusedMoEMethodBase, ) from vllm.model_executor.layers.fused_moe.modular_kernel import ( FusedMoEModularKernel, FusedMoEPrepareAndFinalize, ) logger = init_logger(__name__) @CustomOp.register("modular_fused_moe") class FusedMoEModularMethod(FusedMoEMethodBase, CustomOp): def __init__( self, old_quant_method: FusedMoEMethodBase, experts: FusedMoEModularKernel ): super().__init__(old_quant_method.moe) self.moe_quant_config = old_quant_method.moe_quant_config self.fused_experts = experts self.disable_expert_map = getattr( old_quant_method, "disable_expert_map", not self.fused_experts.supports_expert_map(), ) self.old_quant_method = old_quant_method logger.debug("Swapping out %s", self.old_quant_method.__class__.__name__) @staticmethod def make( moe_layer: torch.nn.Module, old_quant_method: FusedMoEMethodBase, prepare_finalize: FusedMoEPrepareAndFinalize, shared_experts: torch.nn.Module | None, ) -> "FusedMoEModularMethod": return FusedMoEModularMethod( old_quant_method, FusedMoEModularKernel( prepare_finalize, old_quant_method.select_gemm_impl(prepare_finalize, moe_layer), shared_experts, moe_parallel_config=moe_layer.moe_parallel_config, ), ) @property def topk_indices_dtype(self) -> torch.dtype | None: return self.fused_experts.prepare_finalize.topk_indices_dtype() @property def supports_eplb(self) -> bool: return self.old_quant_method.supports_eplb @property def allow_inplace(self) -> bool: return self.old_quant_method.allow_inplace @property def method_name(self) -> str: return self.old_quant_method.method_name def create_weights( self, layer: torch.nn.Module, num_experts: int, hidden_size: int, intermediate_size_per_partition: int, params_dtype: torch.dtype, **extra_weight_attrs, ): raise NotImplementedError def get_fused_moe_quant_config( self, layer: torch.nn.Module ) -> FusedMoEQuantConfig | None: return self.moe_quant_config def apply( self, layer: "FusedMoE", # type: ignore[name-defined] # noqa: F821 x: torch.Tensor, router_logits: torch.Tensor, ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]: topk_weights, topk_ids = layer.select_experts( hidden_states=x, router_logits=router_logits, ) result = self.fused_experts( hidden_states=x, w1=layer.w13_weight, w2=layer.w2_weight, topk_weights=topk_weights, topk_ids=topk_ids, inplace=self.allow_inplace, activation=layer.activation, global_num_experts=layer.global_num_experts, apply_router_weight_on_input=layer.apply_router_weight_on_input, expert_map=None if self.disable_expert_map else layer.expert_map, ) return result
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false
vllm-project/vllm
https://github.com/vllm-project/vllm/blob/0d4044edd85de30d7d4558aeea4d1e95c7c556d6/vllm/model_executor/layers/fused_moe/moe_torch_iterative.py
vllm/model_executor/layers/fused_moe/moe_torch_iterative.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch import torch.nn.functional as F def fused_moe( hidden_states: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor, gating_output: torch.Tensor, topk: int, global_num_experts: int, expert_map: torch.Tensor = None, renormalize: bool = False, ) -> torch.Tensor: """ Args: hidden_states: [*, hidden_size] w1: [num_experts, intermediate_size * 2, hidden_size] w2: [num_experts, hidden_size, intermediate_size] gating_output: [*, num_experts] expert_map: [num_experts] """ orig_shape = hidden_states.shape hidden_size = hidden_states.shape[-1] num_tokens = hidden_states.shape[:-1].numel() num_experts = w1.shape[0] intermediate_size = w2.shape[-1] dtype = hidden_states.dtype hidden_states = hidden_states.view(num_tokens, hidden_size) gating_output = gating_output.view(num_tokens, global_num_experts) topk_weights = gating_output.softmax(dim=-1, dtype=torch.float) topk_weights, selected_experts = topk_weights.topk(topk, dim=-1) if renormalize: topk_weights = topk_weights / topk_weights.sum(dim=-1, keepdim=True) topk_weights = topk_weights.to(dtype) if expert_map is not None: selected_experts = expert_map[selected_experts] final_hidden_states = None for expert_idx in range(num_experts): expert_w1 = w1[expert_idx] expert_w2 = w2[expert_idx] expert_mask = selected_experts == expert_idx expert_weights = (topk_weights * expert_mask).sum(dim=-1, keepdim=True) x = F.linear(hidden_states, expert_w1) gate = F.silu(x[:, :intermediate_size]) x = x[:, intermediate_size:] * gate x = F.linear(x, expert_w2) current_hidden_states = x * expert_weights if final_hidden_states is None: final_hidden_states = current_hidden_states else: final_hidden_states = final_hidden_states + current_hidden_states return final_hidden_states.view(orig_shape) # type: ignore
python
Apache-2.0
0d4044edd85de30d7d4558aeea4d1e95c7c556d6
2026-01-04T14:38:19.902011Z
false