Files
songe/tests/test_quantized.py
BasilMan517andCodex 4ad05de882 Warn when a Qwen Deep Dream lacks fast linear-attention kernels (#340)
* Warn before a Dream uses slow linear-attention kernels

Written by Codex. Based on the dd handover kernel diagnostic.

* Keep the kernel warning regression independent of model reads

Written by Codex.

* Keep the existing review assertion with its regression

Written by Codex.

---------

Co-authored-by: Codex <[email protected]>
2026-11-29 18:37:20 -05:00

64 lines
3.4 KiB
Python

import json
from types import SimpleNamespace
from songe.backends import quantized
from songe.backends.quantized import missing_packages
def checkpoint(tmp_path, quant=None):
config = {"model_type": "qwen3"} | ({"quantization_config": quant} if quant else {})
(tmp_path / "config.json").write_text(json.dumps(config), encoding="utf-8")
return tmp_path
def test_a_quantized_checkpoint_names_the_package_it_needs(tmp_path, monkeypatch):
"""Live (#257): FP8 and NVFP4 builds failed at calibration, after 11 to
23 minutes of practice, for want of kernels / compressed-tensors."""
monkeypatch.setattr(quantized.importlib.util, "find_spec", lambda name: None)
assert missing_packages(checkpoint(tmp_path, {"quant_method": "fp8"})) == ["kernels>=0.18.2,<0.19"]
assert missing_packages(checkpoint(tmp_path, {"quant_method": "compressed-tensors"})) == [
"compressed-tensors>=0.15.0"]
# Unquantized, other methods (Inkling's packed experts) and unreadable
# configs need nothing from here.
assert missing_packages(checkpoint(tmp_path)) == []
assert missing_packages(checkpoint(tmp_path, {"quant_method": "modelopt"})) == []
assert missing_packages(tmp_path / "missing") == []
def test_an_installed_package_is_not_missing(tmp_path, monkeypatch):
monkeypatch.setattr(quantized.importlib.util, "find_spec", lambda name: SimpleNamespace(name=name))
assert missing_packages(checkpoint(tmp_path, {"quant_method": "fp8"})) == []
def linear(tmp_path, nested=False):
layers = {"layer_types": ["linear_attention", "linear_attention", "linear_attention", "full_attention"]}
config = {"model_type": "qwen3_5", "text_config": layers} if nested else {"model_type": "qwen3_next"} | layers
(tmp_path / "config.json").write_text(json.dumps(config), encoding="utf-8")
return tmp_path
def test_a_linear_attention_model_names_the_fast_kernels_it_lacks(tmp_path, monkeypatch):
"""#337: Qwen3.8-27B without flash-linear-attention and causal-conv1d
generated at 0.75 to 4 tokens a second, and a Deep Dream's check ran over
an hour with no progress. `songe doctor` and a Dream press say what to install."""
from songe.backends.quantized import slow_kernels
from songe.wording import slow_kernels_line
monkeypatch.setattr(quantized.sys, "platform", "linux")
monkeypatch.setattr(quantized.importlib.util, "find_spec", lambda name: None)
assert slow_kernels(linear(tmp_path)) == ["flash-linear-attention", "causal-conv1d"]
assert slow_kernels(linear(tmp_path, nested=True)) == ["flash-linear-attention", "causal-conv1d"]
line = slow_kernels_line(slow_kernels(linear(tmp_path)))
assert "pip install flash-linear-attention" in line
assert "pip install --no-build-isolation causal-conv1d" in line
# Only what is missing; nothing for a model without such layers, on a
# Mac (MLX has its own), or for an unreadable config.
monkeypatch.setattr(quantized.importlib.util, "find_spec", lambda name: name if name == "fla" else None)
assert slow_kernels(linear(tmp_path)) == ["causal-conv1d"]
assert slow_kernels(checkpoint(tmp_path)) == []
assert slow_kernels(tmp_path / "missing") == []
(tmp_path / "config.json").write_text('{"layer_types": 42}', encoding="utf-8")
assert slow_kernels(tmp_path) == []
monkeypatch.setattr(quantized.sys, "platform", "darwin")
assert slow_kernels(linear(tmp_path)) == []