Open-source State-of-the-art TTS model which runs on a CPU 😻
Python
"""vLLM plugin: selected KittenTTS weights, without an intermediate export."""
import logging
from vllm.model_executor.model_loader.base_loader import BaseModelLoader
from ..weights import iter_lm_weights
class KittenModelLoader(BaseModelLoader):
def download_model(self, model_config):
# KittenTTS resolves the selected files before constructing the engine.
pass
def load_model(self, vllm_config, model_config, prefix=""):
model = super().load_model(vllm_config, model_config, prefix=prefix)
# AMD GPUs: check the 4-bit kernel on the weights as it repacked them (rocm.py).
references = getattr(self, "_probe_references", None)
self._probe_references = None
if references:
from .rocm import probe_w4a16
report = probe_w4a16(model, references, model_config.dtype)
logging.getLogger("vllm.kittenml").info("W4A16 kernel check passed: %s", report)
return model
def load_weights(self, model, model_config):
options = self.load_config.model_loader_extra_config
quantization = options.get("quantization")
# Rescaled float16: finite rounding is measured; overflow remains an error.
float16 = options.get("float16_rescale", False)
if float16:
_check_float16_model(model, model_config)
probe = False
if quantization == "w4a16":
# vLLM chose each layer's kernel when it built the model; check before loading.
probe = _check_w4a16_kernels(model, model_config, float16)
# Exact 4-bit weights straight from the trits, one tensor at a time.
from .w4a16 import iter_w4a16
weights = iter_w4a16(options["repo_dir"], options["config"], options["weights"])
elif quantization is None:
state = iter_lm_weights(options["repo_dir"], options["config"], options["weights"])
weights = ((name, tensor) for name, tensor in state
if not name.startswith("spk_proj."))
else:
raise ValueError(f"unknown KittenTTS quantization {quantization!r}")
if float16:
from .fp16 import rescale_weights
weights = rescale_weights(weights)
if probe:
from .rocm import keep_references
self._probe_references = {}
weights = keep_references(weights, self._probe_references)
loaded = model.load_weights(weights)
missing = set(dict(model.named_parameters())) - loaded - _kv_cache_scale_names(model)
if missing:
raise ValueError(f"weights missing from the checkpoint: {sorted(missing)[:5]}")
if float16:
from .fp16 import scale_norm_epsilons
config = model_config.hf_config
scale_norm_epsilons(model, config.num_hidden_layers, config.rms_norm_eps)
def _check_float16_model(model, model_config):
"""The float16 rescale needs a float16 engine with its own, unscaled output head."""
import torch
refuse = "float16 rescale: refusing to load: "
if model_config.dtype != torch.float16:
raise RuntimeError(refuse + f"the engine dtype is {model_config.dtype}, not float16")
head = getattr(getattr(model, "lm_head", None), "weight", None)
embedding = getattr(getattr(getattr(model, "model", None), "embed_tokens", None),
"weight", None)
if getattr(model_config.hf_config, "tie_word_embeddings", True) or head is None \
or head is embedding:
raise RuntimeError(refuse + "the output head is tied to the scaled embedding")
def _quantized_layers(model):
"""(name, module, kernel) for every linear layer vLLM built with a mixed-precision kernel."""
for name, module in model.named_modules():
kernel = getattr(getattr(module, "scheme", None), "kernel", None)
if kernel is not None:
yield name, module, kernel
def _check_w4a16_kernels(model, model_config, float16=False):
"""Every quantized linear layer must run exact W4A16 on Marlin (NVIDIA).
vLLM picks the mixed-precision kernel per GPU (Machete on Hopper, Marlin
elsewhere, others when a kernel is disabled). Exact W4A16 was verified with
Marlin only, so refuse to load if vLLM chose anything else. This runs in the
engine process: VLLM_MARLIN_INPUT_DTYPE keeps the Marlin kernel but quantizes
its activations to 8 bits (W4A8), which shows in the kernel's activation type
(in vLLM 0.31.0 that is the only way the setting acts).
On AMD GPUs (ROCm), where vLLM has no Marlin, the one allowed kernel is
rocm.W4A16_KERNEL; True is returned so the loader checks its results after
loading (rocm.probe_w4a16).
"""
import torch
from vllm.model_executor.kernels.linear import MarlinLinearKernel
from .rocm import W4A16_KERNEL as ROCM_KERNEL, is_rocm
refuse = "W4A16: refusing to load: "
# bfloat16, or float16 in the opt-in float16 mode (its group scales are rescaled).
expected = torch.float16 if float16 else torch.bfloat16
if model_config.dtype != expected:
raise RuntimeError(refuse + f"the engine dtype is {model_config.dtype}, "
f"not {str(expected).removeprefix('torch.')}")
chosen, activations = {}, {}
for name, _, kernel in _quantized_layers(model):
chosen.setdefault(type(kernel).__name__, []).append(name)
act_type = getattr(getattr(kernel, "config", None), "act_type", None)
activations.setdefault(str(act_type), []).append(name)
if not chosen:
raise RuntimeError("W4A16: vLLM created no quantized linear layers")
rocm = is_rocm()
allowed = ROCM_KERNEL if rocm else MarlinLinearKernel.__name__
other = {kind: names for kind, names in chosen.items() if kind != allowed}
if other:
layers = sum(map(len, other.values()))
where = (f"on AMD GPUs only with {ROCM_KERNEL} (Radeon RX 7000 and RX 9000 series)"
if rocm else "only with MarlinLinearKernel")
raise RuntimeError(f"W4A16: vLLM chose {sorted(other)} for {layers} linear layers, but "
f"exact W4A16 is verified {where} (check "
"VLLM_DISABLED_KERNELS; with VLLM_ENABLE_V1_MULTIPROCESSING=0 vLLM "
"may still use the value from the first engine in this process)")
wrong = {act: names for act, names in activations.items() if act != str(model_config.dtype)}
if wrong:
layers = sum(map(len, wrong.values()))
raise RuntimeError(refuse + f"{layers} linear layers would quantize their activations to "
f"{sorted(wrong)} instead of computing in {model_config.dtype} (check "
"VLLM_MARLIN_INPUT_DTYPE)")
return rocm
def _kv_cache_scale_names(model):
"""Attention KV-cache scales a quantization method adds, which the checkpoint omits.
With quantization="compressed-tensors", every attention layer gets q/k/v scale
and zero-point parameters for a quantized KV cache. KittenTTS keeps the KV cache
in the model dtype, so they keep vLLM's defaults (as with vLLM's own loader);
every other parameter must still come from the checkpoint.
"""
from vllm.model_executor.layers.quantization.kv_cache import BaseKVCacheMethod
names = set()
for prefix, module in model.named_modules():
if isinstance(getattr(module, "quant_method", None), BaseKVCacheMethod):
names.update(f"{prefix}.{name}" if prefix else name
for name, _ in module.named_parameters(recurse=False))
return names
def register():
from vllm.model_executor.model_loader import register_model_loader
register_model_loader("kittenml")(KittenModelLoader)