# Copyright (c) 2026 Peasant Smith and the Project Maya contributors (MIT; https://github.com/mw00/project-maya, v1.1.17). # Ported into Strata (third_party/project-maya/NOTICE.md, LICENSE); changes are in the git history of this file. """tools/ds4/st.py - every tensor of a DeepSeek-V4.1 model directory, read in place. st = SafeTensors(Path("~/models/DSV4").expanduser()) st.t["layers.0.attn.wq_a.trellis"] -> (file, dtype, shape, absolute offset, bytes) st.raw(name), st.f32(name) -> numpy views / float32 copies The index's own files come first; any other .safetensors in the directory (the Engram shards 47 / 48 of the base repo, model-0004[78]-of-00048.safetensors) only adds names the index does not have. """ from __future__ import annotations import json import pathlib import struct import numpy as np DTYPE_BYTES = {"F32": 4, "F16": 2, "BF16": 2, "I32": 4, "I16": 2, "F8_E4M3": 1, "F8_E8M0": 1, "U8": 1, "I8": 1, "I64": 8} class SafeTensors: def __init__(self, d: pathlib.Path): self.dir = pathlib.Path(d) ix = json.loads((self.dir / "model.safetensors.index.json").read_text())["weight_map"] primary = sorted(set(ix.values())) others = sorted(p.name for p in self.dir.glob("*.safetensors") if p.name not in primary) self.files: list[str] = [] self.t: dict[str, tuple] = {} self.mm: dict[str, np.memmap] = {} for fn in primary + others: p = self.dir / fn with p.open("rb") as f: n = struct.unpack(" bool: return name in self.t def raw(self, name: str) -> np.ndarray: fn, _, _, off, nb = self.t[name] return self.mm[fn][off:off + nb] def f32(self, name: str) -> np.ndarray: """The tensor as float32 (exact for F32/F16/BF16), shaped as stored.""" _, dt, shape, _, _ = self.t[name] r = self.raw(name) if dt == "F32": a = r.view("