"""Shared helpers for the MOSS-Image OpenVINO port (CT 140, /opt/mossimage). Layout: /opt/mossimage/hf/{sft,base} original HF files (safetensors, configs, tokenizer) /opt/mossimage/models/... OpenVINO IRs /opt/mossimage/cache OpenVINO GPU/CPU model caches /opt/mossimage/work reference tensors, test images, logs """ import json import os import struct import time import numpy as np ROOT = "/opt/mossimage" HF = {"sft": f"{ROOT}/hf/sft", "base": f"{ROOT}/hf/base"} MODELS = f"{ROOT}/models" CACHE = f"{ROOT}/cache" WORK = f"{ROOT}/work" TE_DIR = f"{HF['sft']}/text_encoder" # byte-identical in Base and SFT VAE_DIR = f"{HF['sft']}/vae" # byte-identical in Base and SFT TE_LAYERS = 32 TE_PARTS = 4 # 8 decoder layers per part (pattern L L L F x2 per part) class Timer: def __init__(self, label): self.label = label def __enter__(self): self.t = time.time() return self def __exit__(self, *a): self.dt = time.time() - self.t print(f"[time] {self.label}: {self.dt:.2f}s", flush=True) def rss_gb(): with open("/proc/self/status") as f: for line in f: if line.startswith("VmRSS"): return int(line.split()[1]) / 1048576 if line.startswith("VmHWM"): pass return 0.0 def hwm_gb(): with open("/proc/self/status") as f: for line in f: if line.startswith("VmHWM"): return int(line.split()[1]) / 1048576 return 0.0 def host_memfree_gb(): d = {} with open("/proc/meminfo") as f: for line in f: k, v = line.split(":") d[k] = int(v.split()[0]) / 1048576 return d # ---------------------------------------------------------------- safetensors class ST: """Minimal lazy safetensors reader over a sharded checkpoint (numpy, bf16 via uint16).""" def __init__(self, folder, index_name): self.folder = folder idx = json.load(open(os.path.join(folder, index_name))) self.map = idx["weight_map"] self._hdr = {} def header(self, shard): if shard not in self._hdr: with open(os.path.join(self.folder, shard), "rb") as f: n = struct.unpack("