vllm.model_executor.kernels.linear.base ¶
FP8Params dataclass ¶
Bases: Params
FP8 layer parameters with typed fields
Source code in vllm/model_executor/kernels/linear/base.py
from_layer classmethod ¶
Extract parameters from layer
Source code in vllm/model_executor/kernels/linear/base.py
Int8Params dataclass ¶
Bases: Params
Int8 layer parameters with typed fields
Source code in vllm/model_executor/kernels/linear/base.py
from_layer classmethod ¶
from_layer(layer: Module) -> Int8Params
Extract parameters from layer
Source code in vllm/model_executor/kernels/linear/base.py
MMLinearKernel ¶
Bases: ABC, 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 (or FP8Params/Int8Params)
- 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
Lifecycle
- Kernel selection: is_supported() and can_implement() check compatibility
- Initialization: init() creates kernel instance with config
- Weight loading: process_weights_after_loading() preprocesses weights
- Inference: apply_weights() executes the quantized matmul
Source code in vllm/model_executor/kernels/linear/base.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
__init__ ¶
Initialize the kernel with the given configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config | _ConfigT | Kernel-specific configuration containing settings like quantization keys, output dtypes, etc. | required |
Source code in vllm/model_executor/kernels/linear/base.py
_get_layer_params 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:
| Name | Type | Description | Default |
|---|---|---|---|
layer | Module | The layer module containing the parameters | required |
**kwargs | Any | Additional arguments | {} |
Returns:
| Type | Description |
|---|---|
_ParamsT | A typed parameter object containing weights, scales, and other |
_ParamsT | quantization parameters |
Source code in vllm/model_executor/kernels/linear/base.py
apply_weights abstractmethod ¶
Apply the quantized weights to the input tensor.
This is the main inference method that performs the quantized matrix multiplication. It should handle input quantization (if needed), call the underlying kernel, and apply bias.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer | Module | The layer module containing the quantized weights | required |
x | Tensor | Input tensor of shape [..., in_features] | required |
bias | Tensor | None | Optional bias tensor of shape [out_features] | None |
**kwargs | Any | Additional kernel-specific arguments | {} |
Returns:
| Type | Description |
|---|---|
Tensor | Output tensor of shape [..., out_features] |
Source code in vllm/model_executor/kernels/linear/base.py
can_implement abstractmethod classmethod ¶
Check if this kernel can implement the given configuration.
This method checks configuration-level compatibility (e.g., quantization scheme, group sizes, static vs dynamic quantization). It's called after is_supported() to determine if this kernel can handle the specific quantization configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config | _ConfigT | The kernel configuration to check | required |
Returns:
| Type | Description |
|---|---|
bool | A tuple of (can_implement, reason): - can_implement: True if this kernel supports the config - reason: If not supported, a string explaining why; otherwise None |
str | None | ``` |
Source code in vllm/model_executor/kernels/linear/base.py
get_output_padding ¶
get_output_padding() -> int | None
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:
| Type | Description |
|---|---|
int | None | Number of tokens to pad, or None for no padding (default) |
Source code in vllm/model_executor/kernels/linear/base.py
is_supported abstractmethod classmethod ¶
Check if this kernel is supported on the current hardware.
This method checks hardware-level compatibility (e.g., GPU architecture, compute capability, available instructions). It's called during kernel selection to filter out kernels that cannot run on the current device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compute_capability | int | None | GPU compute capability (e.g., 80 for A100, 90 for H100). If None, should check the current device. | None |
Returns:
| Type | Description |
|---|---|
tuple[bool, str | None] | A tuple of (is_supported, reason): - is_supported: True if the kernel can run on this hardware - reason: If not supported, a string explaining why; otherwise None |
Source code in vllm/model_executor/kernels/linear/base.py
process_weights_after_loading abstractmethod ¶
process_weights_after_loading(layer: Module) -> None
Process and transform weights after loading from checkpoint.
This method is called once after weights are loaded but before inference. Use it to preprocess weights into the format required by your kernel (e.g., reordering, padding, format conversion).
Modifications should be done in-place using replace_parameter() to ensure the layer's parameters are properly updated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer | Module | The layer module containing the weights to process | required |
Example
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:
| Name | Type | Description |
|---|---|---|
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.