File size: 1,504 Bytes
1533f24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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)