MiniMax-H3-Image-mlx / tests /test_preconverted.py
devin-lai's picture
Prepare inference-only v0.5.0 runtime cleanup
63e9703 verified
Raw History Blame
1.87 kB
import json
import mlx.core as mx
import numpy as np
import pytest
from h3_mlx.config import DiTConfig
from h3_mlx.dit import DiTBlock
from h3_mlx.weights import Linear, WeightStore
@pytest.mark.parametrize("bits,word", [(4, 0x11111111), (8, 0x01010101)])
def test_preconverted_quantized_linear_loads_without_online_quantization(
tmp_path, monkeypatch, bits, word
):
# Fixed synthetic quantized matrix: each decoded weight is 0.5.
tensors = {
"proj.weight": mx.full((4, 64 // (32 // bits)), word, mx.uint32),
"proj.scales": mx.full((4, 1), 0.5, mx.bfloat16),
"proj.biases": mx.zeros((4, 1), mx.bfloat16),
"proj.bias": mx.array([0, 0.25, 0.5, 1], mx.bfloat16),
}
mx.save_safetensors(str(tmp_path / "core.safetensors"), tensors)
(tmp_path / "quantization.json").write_text(
json.dumps({"bits": bits, "group_size": 64})
)
def forbidden(*args, **kwargs):
pytest.fail("Inference must never quantize weights")
monkeypatch.setattr(mx, "quantize", forbidden)
layer = Linear.load(WeightStore(tmp_path), "proj")
x = mx.ones((3, 64), mx.bfloat16)
expected = mx.array([[32, 32.25, 32.5, 33]] * 3, mx.bfloat16)
np.testing.assert_array_equal(
np.asarray(layer(x).view(mx.uint16)), np.asarray(expected.view(mx.uint16))
)
def test_missing_packed_block_cannot_trigger_conversion(tmp_path):
class NormStore:
def get(self, key):
assert (
key.endswith("norm1.weight")
or key.endswith("norm2.weight")
or key.endswith("_norm.weight")
)
return mx.ones(32, mx.bfloat16)
missing = tmp_path / "missing"
with pytest.raises(FileNotFoundError, match="preconverted"):
DiTBlock(NormStore(), "blocks.0", DiTConfig(), packed_cache=missing)
assert not missing.exists()