okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-split-int' into f-split-vision7c5e84612

main
10.3 KiB206 linesraw

Resident expert exchange buffer rotation

When the resident RAM complement exchanges an expert with the GPU cache, the evicted expert lands in a temporary host buffer. The existing path then copies those bytes into the promoted expert's old RAM slot. Rotation removes that last host copy: the temporary buffer becomes resident, and the old RAM slot becomes the next temporary buffer.

Enable

Rotation is off by default. Set STRATA_EXCHANGE_ROTATE=1 before starting the engine with your existing arguments:

export STRATA_EXCHANGE_ROTATE=1
$env:STRATA_EXCHANGE_ROTATE = '1'

Only the exact value 1 opts in. Set it to 0 or remove it to restore the copy path. This setting does not select resident RAM mode or change any preset. It applies only when the resident complement is fully pinned and GPU-mapped and the exchange buffers can also be pinned and mapped. Other layouts keep the copy path and log why rotation could not be enabled. Configurations without a resident complement do not use it. Layers may differ in expert block size (a GGUF with mixed quantizations per layer): see Layers of different block sizes.

The startup diagnostic confirms activation:

FileExpertSource: exchange buffer rotation enabled: ...; no host commit memcpy

Serving diagnostics report the cumulative rotated block count and avoided memcpy payload bytes. This counts the copied payload once, not read plus write traffic, and does not include the GPU transfers, which still occur.

Ownership and synchronization

The original allocations own their memory until FileExpertSource::close(). Rotation updates slot ownership and keeps each host pointer paired with its GPU alias. Atomic slot IDs let background residency queries observe those immutable pairs. Compute readers and GPU transfers must still finish before the existing serialized commit; rotation adds no overlapping compute or new synchronization policy. Reserve the maximum exchange capacity before rotation starts: growing a live exchange arena is rejected because it can now hold resident experts.

Layers of different block sizes

A swap exchanges two experts of the same layer, so the RAM slot the promoted expert leaves has exactly the size of the block the evicted one needs. Rotation therefore keeps its spare buffers per block size: an exchange of a layer takes a spare of that layer's size and hands back one of the same size, and every size keeps its number of spares for good. The first version required one size for all layers and fell back to the copy path on any other pack; Swift 1.5 IQ3_S has seven sizes (1,510,400 to 2,662,400 bytes) and IQ3_XXS has nine too, so neither could rotate.

The exchange allocation holds one spare of every size per swap of a round: --adapt-swaps (at most 96) times the sum of the distinct sizes. With --adapt-async 1 the second half of the buffers only stages copies and is kept in the largest size alone. On IQ3_S with --adapt-swaps 80 that is 1.06 GiB of page-locked RAM (1.26 GiB with --adapt-async 1), against 0.20 and 0.40 GiB for the copy path's buffers. The startup line says how many sizes and how much:

FileExpertSource: exchange buffer rotation enabled: 80 buffers in 7 block sizes (largest 2662400 bytes, 1.06 GiB page-locked); no host commit memcpy

With --adapt-async 1 a rotated round also ends one verify window sooner: there are no bytes for the background job to move, so the ownership flip runs in the tick that sees the copies in.

tests/core/exchange_storage_test.cpp covers it without CUDA (three sizes, 6,144 exchanges: bytes, aliases, a spare of another size or an expert of another layer refused, overlapping resident blocks refused).

Measured: Swift 1.5 IQ3_S on an RTX 5090 with 64 GB (Windows 11)

Core i7-13700K, RTX 5090 32 GB, 64 GB DDR5, Windows 11, driver 617.42, CUDA 13.0.88, engine 0.1.42 built from source with this change. Swift 1.5 IQ3_S, 128K context, --resident-experts (31.4 GiB of experts page-locked in RAM, about 10,800 of 24,576 in VRAM), --adapt-every 1 --adapt-swaps 80 --adapt-decay 0.97, --spec 4, --pool-affinity p-cores. In the first and third rows one binary ran both arms and only STRATA_EXCHANGE_ROTATE differs; in the second the off arm is the build before this change (the third row's binary also carried extra timers).

RunRotation offRotation on
One 8K prompt, the same 1,500 forced tokens (--spec-follow), blocking tier, decode tok/s163.4, 165.1, 163.1, 161.6, 162.6187.4, 188.7, 185.8, 184.9
--serve, 23 coding-agent turns (tools/agent_turns_bench.py's turns, greedy, up to 400 new tokens), blocking tier, tok/s over all turns, three interleaved rounds147.8, 159.2, 158.4175.2, 184.9, 172.8
The same turns with --adapt-async 1, one binary, two interleaved rounds166.0, 170.1185.6, 182.5

In the first row, two runs of each arm that sized the expert cache alike (10,681 slots) wrote the same --window-hashes file for all 434 windows: the same arithmetic, window by window. (--expert-cache auto sizes the cache from the free VRAM at start, and a run with another slot count places experts differently, so its hashes differ with or without rotation.) The tier swapped about 14,000 experts in such a run, 10,100 to 10,600 of them exchanges. In the serve rows the rotated arm was faster on 23 of 23 turns (blocking tier) and 22 of 23 (--adapt-async 1). On this box the tier swaps about 30 experts per verify window all through a run (the cache holds 44% of the experts), so the copy path moved about 50 MiB of RAM per window on the decode thread; that is the time rotation removes. With --adapt-async 1 the same copy ran on the background job, and the decode thread still lost about 1 ms per window (0.4 to 2.5 by request) while it ran; rotated, the tier's part of a window is 0.06 ms.

Not measured: Linux, AMD cards, a layer split, a pack with one block size on this machine (the code path for it is the one above with a single size).

Tests

The ownership test can run without CUDA:

g++ -std=c++20 -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer \
  -pthread -Iinclude tests/core/exchange_storage_test.cpp -o /tmp/exchange_storage_test
/tmp/exchange_storage_test

With an existing CUDA build configured:

cmake --build build --target strata exchange_storage_test file_expert_source_test
ctest --test-dir build -R '^(exchange_storage_test|file_expert_source_test)$' --output-on-failure
./build/file_expert_source_test --rotation-gpu
compute-sanitizer --tool memcheck --error-exitcode 71 ./build/file_expert_source_test --rotation-gpu

The CPU test checks byte preservation, pointer/alias ownership, guards, invalid operations and concurrent residency queries across 12,304 exchanges. The CUDA fixture performs 64 exchanges in each of three modes: copy with pinned RAM, rotation with pinned RAM, and requested rotation with pageable RAM (fallback). It checks exact bytes, GPU alias reads, file fallback, capacity, and close/reopen. The GPU fixture is explicit; the ordinary CTest invocation does not run it.

On 2026-10-04, the clean patch on upstream 6f32ec070f23ced9f50e704d854d775da52591ab built on Linux with GCC 15.2 and CUDA 13.3 for an RTX 4090. Both CTest tests, the ASan/UBSan ownership test, and the CUDA fixture passed. Compute Sanitizer reported 0 errors. AMD and Windows GPU execution were not tested for this patch.

One native token-identical A/B

Measured on 2026-10-04 using the clean rotation build at 18a30ad775bce86e99a54e6dc552ccb72c66d16e, on upstream 6f32ec070f23ced9f50e704d854d775da52591ab. The model is the standard GSQ-RCO Q2_0, with its matching IQ4_NL PLE shard. Both shards were verified against the publisher's SHA-256 hashes at the pinned revision in the receipt. The native pack uses the existing tools/iq_pack.py; no compatibility conversion or Q8 PLE reader extension is needed.

Both arms used the same Release binary (GCC 13.3, CUDA 13.2), RTX PRO 6000 Blackwell 96GB, Ryzen 9 7950X and 128GB installed RAM. A fresh engine per arm generated exactly 1,024 tokens after the same 1,024-token counting prompt. The context allocation was 40,960, with 12,000 GPU expert slots, a 16.19 GiB pinned/mapped resident complement, FP16 KV, greedy target-only decoding and zero offered drafts, reused prompt tokens or file-tier blob reads during generation. Only STRATA_EXCHANGE_ROTATE changed. Transfer completion is awaited before admission.

The GPU cache was deliberately capped at 12,000 slots (15.45 GiB) to exercise resident RAM exchanges. Q2 can fit entirely on this 96GB GPU; this is a storage path comparison, not the fastest full-GPU configuration. It does not change the project's presets or enable resident mode for existing users.

RotationDecode tokens/sResident exchangesAvoided host-copy payloadOutput IDs
0104.903,8020 bytes1,024, identical
1113.443,8025,255,884,800 bytes1,024, identical

Decode rates exclude startup and prefill. The observed throughput change was +8.1% in this one pair; repeated timing or a general speedup is not established. The out-of-vocabulary EOS sentinel 2147483647 forces the exact output length, so this is not a model-quality evaluation. Speculative-path token parity, AMD and Windows GPU execution remain untested.

The same Blackwell binary passed both CTest checks and the three-mode CUDA fixture; Compute Sanitizer reported 0 errors. The receipt contains source, binary, model, pack and profile hashes, arguments, timings and diagnostics. The raw IDs retain the input and both outputs. To check the token comparison without a model:

python - <<'PY'
import hashlib, json
from pathlib import Path
p = Path('bench/results')
r = json.loads((p / 'exchange-rotation-ab.json').read_text())
raw = (p / r['token_ids_file']).read_bytes()
assert hashlib.sha256(raw).hexdigest() == r['token_ids_file_sha256']
d = json.loads(raw)
assert len(d['input']) == len(d['off']) == len(d['on']) == 1024
assert d['off'] == d['on']
for name, arm in zip(('off', 'on'), r['arms']):
    sha = hashlib.sha256(json.dumps(d[name], separators=(',', ':')).encode()).hexdigest()
    assert sha == arm['output_token_ids_sha256']
print('1,024 identical output token IDs')
PY
1# Resident expert exchange buffer rotation
2
3When the resident RAM complement exchanges an expert with the GPU cache, the
4evicted expert lands in a temporary host buffer. The existing path then copies
5those bytes into the promoted expert's old RAM slot. Rotation removes that last
6host copy: the temporary buffer becomes resident, and the old RAM slot becomes
7the next temporary buffer.
8
9## Enable
10
11Rotation is **off by default**. Set `STRATA_EXCHANGE_ROTATE=1` before starting the
12engine with your existing arguments:
13
14```bash
15export STRATA_EXCHANGE_ROTATE=1
16```
17
18```powershell
19$env:STRATA_EXCHANGE_ROTATE = '1'
20```
21
22Only the exact value `1` opts in. Set it to `0` or remove it to restore the copy
23path. This setting does not select resident RAM mode or change any preset.
24It applies only when the resident complement is fully pinned and GPU-mapped
25and the exchange buffers can also be pinned and mapped. Other layouts keep the
26copy path and log why rotation could not be enabled. Configurations without a
27resident complement do not use it. Layers may differ in expert block size (a
28GGUF with mixed quantizations per layer): see
29[Layers of different block sizes](#layers-of-different-block-sizes).
30
31The startup diagnostic confirms activation:
32
33```text
34FileExpertSource: exchange buffer rotation enabled: ...; no host commit memcpy
35```
36
37Serving diagnostics report the cumulative rotated block count and avoided
38`memcpy` payload bytes. This counts the copied payload once, not read plus write
39traffic, and does not include the GPU transfers, which still occur.
40
41## Ownership and synchronization
42
43The original allocations own their memory until `FileExpertSource::close()`.
44Rotation updates slot ownership and keeps each host pointer paired with its GPU
45alias. Atomic slot IDs let background residency queries observe those immutable
46pairs. Compute readers and GPU transfers must still finish before the existing
47serialized commit; rotation adds no overlapping compute or new synchronization
48policy. Reserve the maximum exchange capacity before rotation starts: growing a
49live exchange arena is rejected because it can now hold resident experts.
50
51## Layers of different block sizes
52
53A swap exchanges two experts of the same layer, so the RAM slot the promoted
54expert leaves has exactly the size of the block the evicted one needs. Rotation
55therefore keeps its spare buffers per block size: an exchange of a layer takes
56a spare of that layer's size and hands back one of the same size, and every
57size keeps its number of spares for good. The first version required one size
58for all layers and fell back to the copy path on any other pack; Swift 1.5
59IQ3_S has seven sizes (1,510,400 to 2,662,400 bytes) and IQ3_XXS has nine
60too, so neither could rotate.
61
62The exchange allocation holds one spare of every size per swap of a round:
63`--adapt-swaps` (at most 96) times the sum of the distinct sizes. With
64`--adapt-async 1` the second half of the buffers only stages copies and is
65kept in the largest size alone. On IQ3_S with `--adapt-swaps 80` that is
661.06 GiB of page-locked RAM (1.26 GiB with `--adapt-async 1`), against 0.20
67and 0.40 GiB for the copy path's buffers. The startup line says how many sizes
68and how much:
69
70```text
71FileExpertSource: exchange buffer rotation enabled: 80 buffers in 7 block sizes (largest 2662400 bytes, 1.06 GiB page-locked); no host commit memcpy
72```
73
74With `--adapt-async 1` a rotated round also ends one verify window sooner:
75there are no bytes for the background job to move, so the ownership flip runs
76in the tick that sees the copies in.
77
78`tests/core/exchange_storage_test.cpp` covers it without CUDA (three sizes,
796,144 exchanges: bytes, aliases, a spare of another size or an expert of
80another layer refused, overlapping resident blocks refused).
81
82### Measured: Swift 1.5 IQ3_S on an RTX 5090 with 64 GB (Windows 11)
83
84Core i7-13700K, RTX 5090 32 GB, 64 GB DDR5, Windows 11, driver 617.42, CUDA
8513.0.88, engine 0.1.42 built from source with this change. Swift 1.5 IQ3_S,
86128K context, `--resident-experts` (31.4 GiB of experts page-locked in RAM,
87about 10,800 of 24,576 in VRAM), `--adapt-every 1 --adapt-swaps 80
88--adapt-decay 0.97`, `--spec 4`, `--pool-affinity p-cores`. In the first and third
89rows one binary ran both arms and only `STRATA_EXCHANGE_ROTATE` differs; in the
90second the off arm is the build before this change (the third row's binary also
91carried extra timers).
92
93| Run | Rotation off | Rotation on |
94|---|---|---|
95| One 8K prompt, the same 1,500 forced tokens (`--spec-follow`), blocking tier, decode tok/s | 163.4, 165.1, 163.1, 161.6, 162.6 | 187.4, 188.7, 185.8, 184.9 |
96| `--serve`, 23 coding-agent turns (`tools/agent_turns_bench.py`'s turns, greedy, up to 400 new tokens), blocking tier, tok/s over all turns, three interleaved rounds | 147.8, 159.2, 158.4 | 175.2, 184.9, 172.8 |
97| The same turns with `--adapt-async 1`, one binary, two interleaved rounds | 166.0, 170.1 | 185.6, 182.5 |
98
99In the first row, two runs of each arm that sized the expert cache alike (10,681
100slots) wrote the same `--window-hashes` file for all 434 windows: the same
101arithmetic, window by window. (`--expert-cache auto` sizes the cache from the
102free VRAM at start, and a run with another slot count places experts
103differently, so its hashes differ with or without rotation.) The tier swapped
104about 14,000 experts in such a run, 10,100 to 10,600 of them exchanges. In the serve rows the
105rotated arm was faster on 23 of 23 turns (blocking tier) and 22 of 23
106(`--adapt-async 1`). On this box the tier swaps about 30 experts per verify
107window all through a run (the cache holds 44% of the experts), so the copy
108path moved about 50 MiB of RAM per window on the decode thread; that is the
109time rotation removes. With `--adapt-async 1` the same copy ran on the
110background job, and the decode thread still lost about 1 ms per window (0.4 to 2.5 by request)
111while it ran; rotated, the tier's part of a window is 0.06 ms.
112
113Not measured: Linux, AMD cards, a layer split, a pack with one block size on
114this machine (the code path for it is the one above with a single size).
115
116## Tests
117
118The ownership test can run without CUDA:
119
120```bash
121g++ -std=c++20 -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer \
122  -pthread -Iinclude tests/core/exchange_storage_test.cpp -o /tmp/exchange_storage_test
123/tmp/exchange_storage_test
124```
125
126With an existing CUDA build configured:
127
128```bash
129cmake --build build --target strata exchange_storage_test file_expert_source_test
130ctest --test-dir build -R '^(exchange_storage_test|file_expert_source_test)$' --output-on-failure
131./build/file_expert_source_test --rotation-gpu
132compute-sanitizer --tool memcheck --error-exitcode 71 ./build/file_expert_source_test --rotation-gpu
133```
134
135The CPU test checks byte preservation, pointer/alias ownership, guards, invalid
136operations and concurrent residency queries across 12,304 exchanges. The CUDA
137fixture performs 64 exchanges in each of three modes: copy with pinned RAM,
138rotation with pinned RAM, and requested rotation with pageable RAM (fallback).
139It checks exact bytes, GPU alias reads, file fallback, capacity, and close/reopen.
140The GPU fixture is explicit; the ordinary CTest invocation does not run it.
141
142On 2026-10-04, the clean patch on upstream
143`6f32ec070f23ced9f50e704d854d775da52591ab` built on Linux with GCC 15.2 and CUDA
14413.3 for an RTX 4090. Both CTest tests, the ASan/UBSan ownership test, and the
145CUDA fixture passed. Compute Sanitizer reported **0 errors**. AMD and Windows
146GPU execution were not tested for this patch.
147
148## One native token-identical A/B
149
150Measured on 2026-10-04 using the clean rotation build at
151`18a30ad775bce86e99a54e6dc552ccb72c66d16e`, on upstream
152`6f32ec070f23ced9f50e704d854d775da52591ab`. The model is the standard
153GSQ-RCO **Q2_0**, with its matching IQ4_NL PLE shard. Both shards were verified
154against the publisher's SHA-256 hashes at the pinned revision in the receipt.
155The native pack uses the existing `tools/iq_pack.py`; no compatibility conversion
156or Q8 PLE reader extension is needed.
157
158Both arms used the same Release binary (GCC 13.3, CUDA 13.2), RTX PRO 6000
159Blackwell 96GB, Ryzen 9 7950X and 128GB installed RAM. A fresh engine per arm
160generated exactly 1,024 tokens after the same 1,024-token counting prompt.
161The context allocation was 40,960, with 12,000 GPU expert slots, a 16.19 GiB
162pinned/mapped resident complement, FP16 KV, greedy target-only decoding and
163zero offered drafts, reused prompt tokens or file-tier blob reads during
164generation. Only `STRATA_EXCHANGE_ROTATE` changed. Transfer completion is
165awaited before admission.
166
167The GPU cache was deliberately capped at 12,000 slots (15.45 GiB) to exercise
168resident RAM exchanges. Q2 can fit entirely on this 96GB GPU; this is a storage
169path comparison, not the fastest full-GPU configuration. It does not change
170the project's presets or enable resident mode for existing users.
171
172| Rotation | Decode tokens/s | Resident exchanges | Avoided host-copy payload | Output IDs |
173|---|---:|---:|---:|---|
174| `0` | 104.90 | 3,802 | 0 bytes | 1,024, identical |
175| `1` | 113.44 | 3,802 | 5,255,884,800 bytes | 1,024, identical |
176
177Decode rates exclude startup and prefill. The observed throughput change was
178+8.1% in this one pair; repeated timing or a general speedup is not established.
179The out-of-vocabulary EOS sentinel `2147483647` forces the exact output length,
180so this is not a model-quality evaluation. Speculative-path token parity,
181AMD and Windows GPU execution remain untested.
182
183The same Blackwell binary passed both CTest checks and the three-mode CUDA
184fixture; Compute Sanitizer reported 0 errors. The
185[receipt](../bench/results/exchange-rotation-ab.json) contains source, binary,
186model, pack and profile hashes, arguments, timings and diagnostics. The
187[raw IDs](../bench/results/exchange-rotation-token-ids.json) retain the input
188and both outputs. To check the token comparison without a model:
189
190```bash
191python - <<'PY'
192import hashlib, json
193from pathlib import Path
194p = Path('bench/results')
195r = json.loads((p / 'exchange-rotation-ab.json').read_text())
196raw = (p / r['token_ids_file']).read_bytes()
197assert hashlib.sha256(raw).hexdigest() == r['token_ids_file_sha256']
198d = json.loads(raw)
199assert len(d['input']) == len(d['off']) == len(d['on']) == 1024
200assert d['off'] == d['on']
201for name, arm in zip(('off', 'on'), r['arms']):
202    sha = hashlib.sha256(json.dumps(d[name], separators=(',', ':')).encode()).hexdigest()
203    assert sha == arm['output_token_ids_sha256']
204print('1,024 identical output token IDs')
205PY
206```