Files
leleka/engine/strata/tools/dsv41/st.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

67 lines
2.7 KiB
Python

# 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("<Q", f.read(8))[0]
h = json.loads(f.read(n))
base = 8 + n
self.files.append(fn)
for k, v in h.items():
if k == "__metadata__" or k in self.t:
continue
a, b = v["data_offsets"]
self.t[k] = (fn, v["dtype"], tuple(v["shape"]), base + a, b - a)
self.mm[fn] = np.memmap(p, dtype=np.uint8, mode="r")
def has(self, name: str) -> 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("<f4")
elif dt == "F16":
a = r.view("<f2").astype(np.float32)
elif dt == "BF16":
a = (r.view("<u2").astype(np.uint32) << 16).view(np.float32)
else:
raise ValueError("%s is %s, not a float tensor" % (name, dt))
return a.reshape(shape)