from __future__ import annotations import struct import torch from wal_tat.runtime.cache import ( HARDWARE_BASE_HEADER, HARDWARE_BASE_MAGIC, build_hardware_cache, load_hardware_base_cache, ) def test_load_minimal_hardware_cache(tmp_path): rows, columns, groups = 1, 128, 1 payloads = ( bytes(columns // 4), bytes(groups * 8), bytes(groups), torch.ones((rows, groups), dtype=torch.float16).numpy().tobytes(), torch.zeros((rows, groups), dtype=torch.float16).numpy().tobytes(), ) header = HARDWARE_BASE_HEADER.pack( HARDWARE_BASE_MAGIC, 1, rows, columns, groups, *(len(value) for value in payloads), ) path = tmp_path / "matrix.walhw" path.write_bytes(header + b"".join(payloads)) loaded = load_hardware_base_cache(path, torch.device("cpu")) assert loaded[-2:] == (rows, columns) assert loaded[0].shape == (rows, columns // 4) assert loaded[3].item() == 1.0 def test_rejects_truncated_hardware_cache(tmp_path): path = tmp_path / "bad.walhw" path.write_bytes(struct.pack("