vllm.model_executor.kernels.linear.base
¶
Classes:
-
FP8Params–FP8 layer parameters with typed fields.
-
LinearKernel–Base class of every quantized linear kernel.
-
LinearKernelOracle–Shared behaviour of linear kernel selection (dispatch).
-
MMLinearKernel–Abstract base class for quantized matrix multiplication kernels.
-
Params–Base class for quantized layer parameters.
FP8Params
dataclass
¶
Bases: Params
FP8 layer parameters with typed fields.
Methods:
-
from_layer–Extract parameters from layer.
Source code in vllm/model_executor/kernels/linear/base.py
from_layer(layer)
classmethod
¶
Extract parameters from layer.
Source code in vllm/model_executor/kernels/linear/base.py
LinearKernel
¶
Bases: ABC, Generic[_LinearConfigT]
Base class of every quantized linear kernel.
Methods:
-
apply_weights–Run the quantized GEMM.
-
can_implement–Whether the kernel handles
config, and if not, why. -
executed_activation_key–The activation quantization the kernel runs for a layer requesting
-
input_quant_key–The activation quant key the kernel accepts pre-quantized (via
-
is_supported–Whether the kernel runs on this device (the current one if
-
process_weights_after_loading–Convert the loaded weights on
layerin place to the kernel's format. -
supports_batch_invariance–Whether a row's output is independent of the other rows in the batch.
Source code in vllm/model_executor/kernels/linear/base.py
apply_weights(layer, x, bias=None)
abstractmethod
¶
Run the quantized GEMM.
can_implement(config)
abstractmethod
classmethod
¶
Whether the kernel handles config, and if not, why.
executed_activation_key(activation_quant_key)
classmethod
¶
The activation quantization the kernel runs for a layer requesting
activation_quant_key. Weight-only kernels return None; kernel
selection then warns about, or under VLLM_STRICT_QUANT_SCHEME forbids,
the fallback.
Source code in vllm/model_executor/kernels/linear/base.py
input_quant_key()
¶
The activation quant key the kernel accepts pre-quantized (via
as_quantized_activation), or None if it quantizes its own input.
is_supported(compute_capability=None)
abstractmethod
classmethod
¶
Whether the kernel runs on this device (the current one if
compute_capability is None), and if not, why.
Source code in vllm/model_executor/kernels/linear/base.py
process_weights_after_loading(layer)
abstractmethod
¶
Convert the loaded weights on layer in place to the kernel's format.
supports_batch_invariance()
classmethod
¶
LinearKernelOracle
¶
Shared behaviour of linear kernel selection (dispatch).
Linear kernels have no oracle abstraction yet; this only hosts the logic
shared by the init_*_linear_kernel selectors, to be extended as they are
migrated.
Methods:
-
maybe_skip_for_requantization_source–Whether to return a placeholder instead of selecting a compute kernel.
Source code in vllm/model_executor/kernels/linear/base.py
maybe_skip_for_requantization_source()
staticmethod
¶
Whether to return a placeholder instead of selecting a compute kernel.
A checkpoint method created as an online requantization source only dequantizes its weights, so it needs no linear kernel.
Source code in vllm/model_executor/kernels/linear/base.py
MMLinearKernel
¶
Bases: LinearKernel[_ConfigT], Generic[_ConfigT, _ParamsT]
Abstract base class for quantized matrix multiplication kernels.
This class provides the interface for implementing custom quantized linear layer kernels in vLLM. Subclasses should implement specific quantization strategies (e.g., FP8, INT8) and their corresponding compute kernels.
Generic Type Parameters
_ConfigT: Configuration type for the kernel (subclass of MMLinearLayerConfig). Contains kernel-specific settings like quantization keys, dtypes, etc. _ParamsT: Parameter type for the kernel (subclass of Params). Defines the quantized weights and scales needed by the kernel.
Typical Usage
- Define a config dataclass inheriting from MMLinearLayerConfig
- Define a params dataclass inheriting from Params
- Subclass MMLinearKernel with your config and params types
- Implement all abstract methods
- Register the kernel with the quantization method
Example
@dataclass
class MyKernelConfig(MMLinearLayerConfig):
static: bool
output_dtype: torch.dtype
@dataclass
class MyKernelParams(FP8Params):
custom_scale: torch.Tensor
CUSTOM_SCALE: ClassVar[str] = "custom_scale"
class MyKernel(MMLinearKernel[MyKernelConfig, MyKernelParams]):
@classmethod
def is_supported(cls, compute_capability=None):
if compute_capability and compute_capability < 90:
return False, "Requires compute capability >= 9.0"
return True, None
@classmethod
def can_implement(cls, config):
if not config.static:
return False, "Only static quantization supported"
return True, None
def process_weights_after_loading(self, layer):
# Preprocess weights for the kernel
params = self._get_layer_params(layer)
processed = preprocess_weights(params.weight)
replace_parameter(layer, params.WEIGHT, processed)
def _get_layer_params(self, layer, **kwargs):
return MyKernelParams.from_layer(layer)
def apply_weights(self, layer, x, bias=None, **kwargs):
params = self._get_layer_params(layer)
# Call your custom kernel
output = my_custom_kernel(x, params.weight, params.weight_scale)
if bias is not None:
output += bias
return output
Methods:
-
__init__–Initialize the kernel with the given configuration.
-
get_output_padding–Get the number of output tokens to pad for this kernel.
Source code in vllm/model_executor/kernels/linear/base.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
__init__(config)
¶
Initialize the kernel with the given configuration.
Parameters:
-
(config¶_ConfigT) –Kernel-specific configuration containing settings like quantization keys, output dtypes, etc.
Source code in vllm/model_executor/kernels/linear/base.py
_get_layer_params(layer, **kwargs)
abstractmethod
¶
Extract typed parameters from the layer module.
This internal method retrieves the quantized weights and scales from the layer as a typed parameter object. Subclasses should typically delegate to ParamsClass.from_layer().
Parameters:
-
(layer¶Module) –The layer module containing the parameters
-
(**kwargs¶Any, default:{}) –Additional arguments
Returns:
-
_ParamsT–A typed parameter object containing weights, scales, and other
-
_ParamsT–quantization parameters
Source code in vllm/model_executor/kernels/linear/base.py
get_output_padding()
¶
Get the number of output tokens to pad for this kernel.
Some kernels require input padding for optimal performance. Override this method to specify padding requirements.
Returns:
-
int | None–Number of tokens to pad, or None for no padding (default)
Source code in vllm/model_executor/kernels/linear/base.py
Params
dataclass
¶
Base class for quantized layer parameters.
This class provides a typed interface for accessing quantized weights and scales from layer modules. It serves as a parameter container that can be extracted from layers and passed to kernel implementations.
Attributes:
-
weight(Tensor) –The quantized weight tensor
-
weight_scale(Tensor) –weight scaling factors
-
input_scale(Tensor | None) –Optional input scaling factors
Class Variables
WEIGHT: Attribute name for weight tensor on the layer module WEIGHT_SCALE: Attribute name for weight scale tensor on the layer module INPUT_SCALE: Attribute name for input scale tensor on the layer module
Important
The string values of WEIGHT, WEIGHT_SCALE, and INPUT_SCALE class variables MUST match the attribute names used in the corresponding quantization method's create_weights() implementation. For example, if FP8LinearMethod.create_weights() sets layer.weight and layer.weight_scale, then WEIGHT="weight" and WEIGHT_SCALE="weight_scale" must be used here.