moss4gb / tests /test_cache.py
AEUPH's picture
moss4gb v0.2.0: NF4 4-bit MOSS-Image-SFT packaging (code, bank, tests, docs, skill)
1533f24 verified
Raw History Blame Contribute Delete
1.5 kB
from __future__ import annotations
import pytest
import torch
from moss4gb import quant
MODEL_DIR = "models/MOSS-Image-SFT"
def test_cache_path_is_stable_and_namespaced():
first = quant.default_cache_path(MODEL_DIR)
assert first == quant.default_cache_path(MODEL_DIR)
# Next to the checkpoint, so it survives a CWD change and dies with a
# re-downloaded model rather than being silently reused.
assert first.parent.name == ".moss4gb-cache"
assert first.parent.parent.name == "MOSS-Image-SFT"
assert "nf4" in first.name and first.suffix == ".pt"
def test_cache_path_separates_compute_dtypes():
# The packed weights are identical but compute_dtype changes the linear's
# matmul dtype, so the two builds must not share a file.
assert quant.default_cache_path(MODEL_DIR, torch.float16) != quant.default_cache_path(
MODEL_DIR, torch.bfloat16
)
def test_cache_key_tracks_the_checkpoint():
# A different config or shard set has to produce a different name, otherwise
# a stale cache would be loaded against new weights.
key = quant.default_cache_path(MODEL_DIR).stem
assert key.startswith("dit_nf4_float16-") and len(key.split("-")[-1]) == 12
def test_file_without_a_module_is_rejected(tmp_path):
path = tmp_path / "bad.pt"
torch.save({"weight": torch.zeros(2, 2)}, path)
with pytest.raises(RuntimeError, match="does not contain a transformer"):
quant._load_cached_transformer(path, device="cpu", verbose=False)