"""tools/image/gate/test_safetensors_lite.py - TEST-ONLY: src/image/vae/safetensors_lite.hpp (the VAE decoder's file reader) on files written here: names, dtypes, shapes, offsets, the bytes of a tensor, __metadata__ skipped, escaped strings, and the refusals (bad length, broken JSON). Builds a small C++ program with the host compiler; skipped when there is none. python -m pytest tools/image/gate/test_safetensors_lite.py""" import json import shutil import struct import subprocess import tempfile import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[3] CXX = shutil.which("g++") or shutil.which("clang++") PROGRAM = r''' #include "safetensors_lite.hpp" #include #include int main(int argc, char** argv) { strata::image::StFile f; std::string err = strata::image::st_read_header(argv[1], f); if (!err.empty()) { std::printf("ERR %s\n", err.c_str()); return 1; } for (auto& kv : f.tensors) { std::printf("T %s %s", kv.first.c_str(), kv.second.dtype.c_str()); for (auto s : kv.second.shape) std::printf(" %lld", (long long) s); std::vector b; err = strata::image::st_read_tensor(argv[1], f, kv.second, b); if (!err.empty()) { std::printf(" READERR %s\n", err.c_str()); return 1; } std::printf(" |"); for (auto c : b) std::printf(" %02x", c); std::printf("\n"); } for (int th : {1, 3, 16}) { // the whole data section in parallel parts: the same bytes as the tensor reads std::unique_ptr all; size_t size = 0; err = strata::image::st_read_all_parallel(argv[1], f, all, size, th); if (!err.empty()) { std::printf("PARERR %s\n", err.c_str()); return 1; } for (auto& kv : f.tensors) { std::vector b; strata::image::st_read_tensor(argv[1], f, kv.second, b); if (b.size() && std::memcmp(b.data(), all.get() + kv.second.begin, b.size()) != 0) { std::printf("PARDIFF\n"); return 1; } } } return 0; } ''' def write_st(path: Path, header: dict, data: bytes, raw_header: bytes = None): h = raw_header if raw_header is not None else json.dumps(header).encode() path.write_bytes(struct.pack("