From 0fbbf068eb7f2e4571e2444a868697e9a2aaa454 Mon Sep 17 00:00:00 2001 From: Highlander <3656749+jerry78424@users.noreply.github.com> Date: Mon, 26 Oct 2026 18:56:54 +0100 Subject: [PATCH] nvfp4 local pack: read the checkpoint with cached sequential reads, not a process-lifetime np.memmap (#3377, #3378) Checkpoint.raw mapped every shard for the life of the process. On Windows each faulted page of the ~64 GB of expert bytes is charged to the process working set, so the pack step looked like a 64 GB RAM demand and was killed by free-RAM watchers on a PC with a model loaded (nothing leaked: it all came back at once). raw() now reads the tensor's byte range into a fresh buffer through one cached file handle per shard; the pages go to the system file cache, which counts as available. The author measured the same bytes, 1.2x faster, flat working set, and a byte-identical pack. raw() is no longer thread-safe (this file uses no threads). Ported from PR #3378 by jerry78424 (the fix for his issue #3377) with its two tests; I ran the tests (13 pass); the full 102 GB pack was not run. --- tools/nvfp4_local_pack.py | 27 +++++++++++++++++----- tools/test_nvfp4_local_pack.py | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/tools/nvfp4_local_pack.py b/tools/nvfp4_local_pack.py index 03f5c884..a9752e4a 100644 --- a/tools/nvfp4_local_pack.py +++ b/tools/nvfp4_local_pack.py @@ -63,7 +63,19 @@ PLE_ROW_BYTES = 90 # 80 B of E2M1 codes + 10 E4M3 block scales, per 160 # --------------------------------------------------------------------------------------------- safetensors access class Checkpoint: - """Header-indexed, memory-mapped access to a checkpoint's safetensors shards (raw bytes, any dtype).""" + """Header-indexed, buffered access to a checkpoint's safetensors shards (raw bytes, any dtype). + + Reads go through a cached file handle into a fresh buffer instead of a process-lifetime np.memmap: + a memmap sweep of the ~64 GB of expert bytes charges every faulted page to the PROCESS WORKING + SET, which Windows (and every free-RAM observer reading GlobalMemoryStatusEx) counts as used + until the OS trims it - the pack step then looks like a ~64 GB RAM demand while its real + committed need is a few GB, and under memory pressure it is the pack run itself that pays + (issue: the silent exit-1 mid-pack failure). + A cached sequential read puts the same pages in the system file cache (standby: counted as + available). The returned array is a fresh WRITABLE buffer - no consumer writes (a mutator would + previously have hit the read-only memmap) - and reads share one handle per shard, so raw() is + not thread-safe, unlike the memmap it replaces. Each shard is read as three ascending streams + (exceptions, weights, CSF-fixed) walked expert-ascending.""" def __init__(self, folder, partial_ok=False): self.dir = pathlib.Path(folder) @@ -76,7 +88,7 @@ class Checkpoint: if not files: raise FileNotFoundError("no .safetensors files in " + str(self.dir)) self.meta: dict[str, tuple[str, str, list[int], int, int]] = {} - self._mm: dict[str, np.memmap] = {} + self._fh: dict[str, "object"] = {} for fn in files: path = self.dir / fn if not path.exists() and partial_ok: @@ -100,9 +112,14 @@ class Checkpoint: def raw(self, name) -> np.ndarray: fn, _, _, a, b = self.meta[name] - if fn not in self._mm: - self._mm[fn] = np.memmap(self.dir / fn, dtype=np.uint8, mode="r") - return self._mm[fn][a:b] + f = self._fh.get(fn) + if f is None: + f = self._fh[fn] = open(self.dir / fn, "rb") + buf = np.empty(b - a, dtype=np.uint8) + f.seek(a) + if f.readinto(buf) != b - a: + raise EOFError("%s: shard %s ended early at %d" % (name, fn, a)) + return buf def array(self, name, np_dtype) -> np.ndarray: return self.raw(name).view(np_dtype).reshape(self.shape(name)) diff --git a/tools/test_nvfp4_local_pack.py b/tools/test_nvfp4_local_pack.py index dd6fbe32..b32c50e1 100644 --- a/tools/test_nvfp4_local_pack.py +++ b/tools/test_nvfp4_local_pack.py @@ -1,5 +1,7 @@ """Tests for tools/nvfp4_local_pack.py: the CSF scale decode, the MXFP8 dequantization, the PLE rows.""" +import json import pathlib +import struct import sys import numpy as np @@ -114,3 +116,42 @@ def test_nvfp4_blocks_layout_by_hand(): # within a 16-block: element j in the low nibble, element j+8 in the high one blk0 = out[0, 4:12] assert [int(b & 15) for b in blk0] == list(range(8)) and [int(b >> 4) for b in blk0] == list(range(8, 16)) + + +def _write_shard(path, tensors): + """tensors: name -> (dtype, shape, uint8 bytes), written safetensors-style (data after the header).""" + hdr, data, off = {}, bytearray(), 0 + for name, (dt, shape, buf) in tensors.items(): + hdr[name] = {"dtype": dt, "shape": shape, "data_offsets": [off, off + len(buf)]} + data += buf + off += len(buf) + hj = json.dumps(hdr).encode("utf-8") + hj += b" " * (-len(hj) % 8) + with open(path, "wb") as f: + f.write(struct.pack("