main
py 73 lines 2.09 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 from abc import ABC, abstractmethod
5 from dataclasses import dataclass
6
7 import torch
8
9 from vllm.model_executor.layers.quantization.utils.quant_utils import QuantKey
10
11
12 @dataclass
13 class Mxfp8LinearLayerConfig:
14 """Configuration for an MXFP8 linear layer.
15
16 All MXFP8 layers share the same structure: FP8-E4M3 weights with
17 uint8 (E8M0) per-block scales at block size 32.
18
19 Attributes:
20 weight_shape: The layer's `(out_features, in_features)`, i.e. `(N, K)`.
21
22 """
23
24 weight_shape: tuple[int, int]
25 bmm_batch_size: int | None = None
26
27
28 class Mxfp8LinearKernel(ABC):
29 """Base class for MXFP8 quantized linear kernels.
30
31 Each subclass implements a specific GEMM backend (FlashInfer CUTLASS,
32 Marlin, emulation).
33 """
34
35 supports_pre_processed_weights: bool = False
36 """True if ``process_weights_after_loading`` only rewrites parameters, so
37 weights exported by the weight cache daemon can be used as-is."""
38
39 def __init__(self, c: Mxfp8LinearLayerConfig) -> None:
40 assert self.can_implement(c)[0]
41 assert self.is_supported()[0]
42 self.config = c
43
44 def input_quant_key(self) -> QuantKey | None:
45 """Return the input quantization key supported by this kernel. If the kernel
46 does not support input quantization outside of the kernel, return None.
47 """
48 return None
49
50 @classmethod
51 @abstractmethod
52 def is_supported(
53 cls, compute_capability: int | None = None
54 ) -> tuple[bool, str | None]:
55 raise NotImplementedError
56
57 @classmethod
58 @abstractmethod
59 def can_implement(cls, c: Mxfp8LinearLayerConfig) -> tuple[bool, str | None]:
60 raise NotImplementedError
61
62 @abstractmethod
63 def process_weights_after_loading(self, layer: torch.nn.Module) -> None:
64 raise NotImplementedError
65
66 @abstractmethod
67 def apply_weights(
68 self,
69 layer: torch.nn.Module,
70 x: torch.Tensor,
71 bias: torch.Tensor | None = None,
72 ) -> torch.Tensor:
73 raise NotImplementedError