Lottolabs's picture
tt-model push qwen3.8-flash-next-coder-tt-p150 (container) (part 4)
c1880be verified
Raw History Blame Contribute Delete
2.81 kB
"""Dequantized Flash-Next weights (float32 numpy, math orientation), cached on disk.
`Weights(model).get(name)` returns the tensor GGUF calls `name` (e.g. "blk.3.attn_q.weight") as float32 in numpy
order (GGUF ne reversed: a projection is (n_out, n_in)). Quantized GGUF blocks are dequantized once (gguf-py is
slow) and kept as .npy under fnx.paths.DEQUANT/<shard1 stem>/ (FNX_CACHE/dequant, default FNX_DATA/cache/dequant);
float tensors are read straight from the mapping. Routed experts (ffn_{gate,up,down}_exps) are NOT served here:
they are 23 GiB; use `model.get_expert` or fnx.cpu_experts.
Every value is exactly the GGUF's: no transforms (Strata applies none for the Coder: gammas are final multipliers,
ssm_a is already negative - spec §A.4).
"""
from __future__ import annotations
import os
from pathlib import Path
import numpy as np
from . import paths
from .config import check_metadata
from .gguf_load import Model
DEFAULT_SHARD1 = str(paths.SHARD1)
FLOAT = {"F32", "F16", "BF16"}
class Weights:
def __init__(self, model: Model | str | None = None, cache_dir: str | None = None, ple: str | None = None):
"""model: a Model or shard 1's path (default fnx.paths.SHARD1); ple: the shard with the PLE table (Strata's
--ple-gguf; default: the shard name's other shards). The GGUF must match the FNX_MODEL variant's geometry
(config.check_metadata)."""
self.model = model if isinstance(model, Model) else Model(model or DEFAULT_SHARD1, [ple] if ple else None)
check_metadata(self.model.metadata)
stem = Path(self.model.files[0].path).name.removesuffix(".gguf")
self.cache = (Path(cache_dir) if cache_dir else paths.CACHE) / "dequant" / stem
self.cache.mkdir(parents=True, exist_ok=True)
def __contains__(self, name: str) -> bool:
return name in self.model
def type_of(self, name: str) -> str:
return self.model.type_of(name)
def get(self, name: str) -> np.ndarray:
if "_exps." in name:
raise ValueError(f"{name}: routed experts are not served by Weights (use model.get_expert / cpu_experts)")
if not self.model.available(name):
# a file still downloading has holes that read as zeros: never cache (or return) them
raise ValueError(f"{name}: its bytes are not downloaded yet")
if self.model.type_of(name) in FLOAT:
return self.model.get(name)
p = self.cache / (name + ".npy")
if p.exists():
return np.load(p, mmap_mode="r")
a = self.model.get(name)
tmp = p.with_suffix(".tmp.npy")
np.save(tmp, a)
os.replace(tmp, p)
return np.load(p, mmap_mode="r")
def layer(self, L: int, suffix: str) -> np.ndarray:
return self.get(f"blk.{L}.{suffix}")