Files
Xiaoze Fan 477c8601b2 refactor(quant): config, scheme and method layers for quantization (#418)
* refactor(quant): config, scheme and method layers for checkpoint quantization

QuantConfig parses the checkpoint's quantization_config into a QuantScheme per
projection; every Linear*, ParallelLMHead and MoE layer takes quant_config and a
prefix, declares its tensors through a QuantMethod and selects its kernel at
construction. MoE kernels own the expert bank layout, pack and apply, so the
offload cache, the FTW converter and the CPU executor follow one layout.

- --quant-backend layer[.kind]=name selects kernels (--nvfp4-backend forwards to it)
- --moe-strategy replaces --moe-backend; the old spelling stays as an alias
- --moe-cpu-layers auto for Windows/WSL pin budgets; --moe-cache-auto respects the kernel's slot limit
- removes the per-family quantized layer classes, bank providers, format env switches and GLM runtime fp8
2026-09-08 23:37:23 -07:00

34 lines
997 B
Python

"""bf16 Linear: one kernel (torch), no scheme."""
from __future__ import annotations
from typing import Any
import torch
import torch.nn.functional as F
from ..registry import LayerKind, register_method
from ..scheme import QuantKind
from .base import LinearKernel, LinearMethod
class TorchLinearKernel(LinearKernel):
name = "torch"
def apply(self, layer: Any, x: torch.Tensor) -> torch.Tensor:
# an fp32 activation stream (DeepSeek-V4's compressors) upcasts the bf16 weight on the fly, as the reference does
w, b = layer.weight, layer.bias
if w.dtype != x.dtype:
w = w.to(x.dtype)
b = b.to(x.dtype) if b is not None else None
return F.linear(x, w, b)
@register_method(QuantKind.NONE, LayerKind.LINEAR)
class UnquantizedLinearMethod(LinearMethod):
candidates = (TorchLinearKernel,)
def create_weights(self, layer: Any) -> None:
g = self.cfg
layer.weight = torch.empty(g.out_features, g.in_features)