"""Strict, CPU-only inspection and non-merging application of MOSS LoRAs. The edit adapter has its own loader. This module handles additional MOSS style/detail adapters without changing that required editing adapter. """ from __future__ import annotations import json import math from pathlib import Path import re import struct def linear_shapes(config=None): """Return (input, output) widths of the bundled MOSS transformer.""" config = config or {} d, f = int(config.get("dim", 3072)), int(config.get("ffn_dim", 8192)) t, c = int(config.get("text_dim", 4096)), int(config.get("latent_channels", 128)) shapes = { "video_embedder": (c, d), "text_embedder.proj": (t, d), "time_embedding.0": (int(config.get("freq_dim", 256)), d), "time_embedding.2": (d, d), "shared_block_modulation": (d, 6 * d), "final_linear_video": (d, c), "video_head_modulation": (d, 2 * d), } for n in range(int(config.get("num_layers", 42))): for part in ("q", "k", "v", "o"): shapes[f"layers.{n}.proj.{part}"] = (d, d) shapes[f"layers.{n}.proj.qkv"] = (d, 3 * d) shapes[f"layers.{n}.proj.ffn_up_gate"] = (d, 2 * f) shapes[f"layers.{n}.proj.ffn_down"] = (f, d) for n in range(2): for name, shape in {"qkv": (d, 3*d), "proj": (d, d), "ffn_gate": (d, f), "ffn_up": (d, f), "ffn_down": (f, d)}.items(): shapes[f"text_embedder.blocks.{n}.{name}"] = shape return shapes def read_header(path): """Inspect safetensors without importing torch or allocating model tensors.""" path = Path(path) with path.open("rb") as stream: raw = stream.read(8) if len(raw) != 8: raise ValueError("The LoRA file is incomplete.") size = struct.unpack("