[PR #4367] [Engine] DeepSeek-V4.1: stream the model load, 3x faster start, up to 7x faster restart #5022

Open
opened 2026-11-07 16:32:06 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4367
Author: @Licenser
Created: 10/27/2026
Status: 🔄 Open

Base: main ← Head: dsv41-copy-stream


📝 Commits (10+)

  • be01fda dsv41: drop each copied expert's file pages after the copy
  • 994f720 dsv41: DS4_COPY_STREAM=1 streams the expert copy in sequential reads
  • 1c366aa dsv41: the VRAM experts in the same stream as the copy
  • 6670b69 dsv41: a stream reader a range of a file, sized by the smallest file
  • d01603d dsv41: the dense weights in the experts' stream
  • a089178 dsv41: the dense weights' staging buffer on huge pages
  • b88db8e dsv41: one stream reader for all files by default
  • 18ba360 dsv41: the stream reads 128 MB at a time
  • be00571 dsv41: the stream's chunk plan and reader ranges in functions of their own
  • c0871e3 dsv41: --stream-probe times the stream's reads and prints the settings to use

📊 Changes

3 files changed (+675 additions, -165 deletions)

View changed files

📝 src/dsv41/ds4_main.cpp (+10 -1)
📝 src/dsv41/ds4_model.cu (+660 -164)
📝 src/dsv41/ds4_model.hpp (+5 -0)

📄 Description

Problem and result

Loading DeepSeek-V4.1-Flash copies the CPU lane's experts into RAM piece by piece through the page cache, and loads
the VRAM experts and the dense weights in passes of their own. The copy and the file pages it leaves behind compete for
RAM. When part of the model is still in the page cache from the last run, free memory runs out mid-copy and the rest
of the copy runs in direct reclaim and compaction: a restart is slower than a cold start.

This PR makes the default copy drop each piece's file pages once it is copied, and adds DS4_COPY_STREAM=1, which
reads the whole model in one sequential pass.

load, s (ds4: loaded in) cold, best cold, worst restart, best restart, worst
main 5e8a4cca 78.0 81.1 117.9 not logged
this PR, default 53.1 54.1 52.7 53.1
this PR, DS4_COPY_STREAM=1 25.8 25.9 25.6 25.8

A restart is the same binary started straight after its own load. Main's restart time depends on how much of the
model the page cache still holds: its two restarts on this machine, both with about 86 GB cached and 94 GB free at the
start, took 95.1 s and 156.7 s to get the experts into memory (the slower run's whole load was not logged). Two
runs of each case:

experts into memory, s cold, best cold, worst restart, best restart, worst
main 5e8a4cca (copy) 54.4 57.2 95.1 156.7
this PR, default (copy) 29.5 30.3 29.3 29.8
this PR, DS4_COPY_STREAM=1 21.5 21.5 21.4 21.5
speed against main cold restart
this PR, default 1.4-1.5x 2.2x (load), up to 5.3x (experts)
this PR, DS4_COPY_STREAM=1 3.0-3.1x 4.6x (load), up to 7.3x (experts)

With the stream the load takes the same 26 s from cold and on restart, and its 21.5 s carry the
VRAM experts and the dense weights as well (138.2 GB against the copy's 94.5 GB). Main's restarts ran into direct
reclaim and compaction (15,605 and 54,200 stalls in the faster one). Decode is unchanged (36.2-36.6 tok/s in every
run), and the scored output is byte-identical on both paths.

None. The page drop changes the default copy; I would like to agree its scope here. The stream is opt-in.

Approach

  1. Default: the page drop. copy_experts drops each copied expert's file pages right after the copy, as the VRAM
    upload already does, so the copy and the page cache never hold the same bytes. DS4_COPY_DROP=0 keeps them.

  2. DS4_COPY_STREAM=1: one pass. One reader reads the model's files front to back in unbuffered reads of 128 MB
    into one of two pinned arenas, while a mover copies the other arena's pieces to their places: the CPU lane's
    experts to RAM, a one-card layer's VRAM experts to their slots (cudaMemcpyAsync), and the dense weights to a
    staging buffer on huge pages. The dense weights' VRAM is allocated first, as before, so the experts' placement sees
    it; their conversion and upload run after the stream, from the staging buffer. Gaps of up to 16 MB between pieces
    are read through. On the machine below the stream reads 6.4 GB/s, 95% of what its two drives read in one
    sequential stream.

  3. Settings for other drives. DS4_STREAM_RANGES=N splits each file among up to N readers, for drive arrays that
    one reader cannot keep busy; DS4_STREAM_MB sets the read size. strata-dsv41 --pack <pack> --stream-probe
    times the reader layouts and read sizes on the pack's own files in about 30 s, without loading the model, and
    prints the settings to use:

    ds4: stream probe: 4 files, 138.2 GB, unbuffered reads; 5 trials of 27.6 GB, each on bytes of its own
      readers                            read MB    GB/s
      1, the files in order (default)        128    6.54
      4: 1,1,1,1 a file                      128    4.28
      7: 2,2,2,1 a file                      128    3.98
      1, the files in order                   64    6.43
      1, the files in order                  256    6.32
    ds4: the reads alone, not the copy into memory a load adds
    ds4: the settings for these drives:
    DS4_COPY_STREAM=1 DS4_STREAM_MB=128 DS4_STREAM_RANGES=0
    

Validation

Machine: an A40 48 GB used alone (CUDA_VISIBLE_DEVICES), Threadripper 3960X, 184 GiB RAM, two Samsung 970 EVO 1 TB
in btrfs RAID0 (fio: 6.74 GB/s in one sequential stream), Linux 7.0, transparent huge pages madvise. Model:
ISTA-DASLab DeepSeek-V4.1-Flash-1017-GSQ-RCO IQ2_XXS 2.00 bpw, the shipped expert profile. Lines that do not name main
c62d5e3a or 46c152a1 were measured with this PR on main 5e8a4cca.

Passed:

  • CUDA build (sm_75 and sm_86) of bf3f4616, on main 46c152a1 of 34d2395f, and on main c62d5e3a of c0871e3a.
  • HIP build (gfx1200 + gfx1201) of bf3f4616: tools/dsv41/build_hip_linux.sh, targets strata-dsv41 and
    ds4_iq_test, ROCm 7.10.0a20251120 from AMD's TheRock wheels, setup's pin. Compiled and linked. The warnings main's
    HIP build lacks are 7 of a kind it has many of, a runtime call's hipError_t left unchecked (-Wunused-value), all
    at calls this branch adds.
  • Loads, the table above: CUDA_VISIBLE_DEVICES=<A40> DS4_NO_EOS=1 strata-dsv41 --pack <pack> --max-context 8192 --eos 1 --dspark auto --dspark-conf 0.65 --experts 5 -v --n 32 --ids <1K prompt> with each setting. Cold: the
    shards' cached pages dropped first (posix_fadvise DONTNEED); restart: straight after a load of the same binary,
    nothing dropped.
  • The scored output is byte-identical to main 5e8a4cca: --score on the multilingual set (999 positions, windows of
    16, fixed placement), 4995 rows, ppl 3.1167, top-1 0.7638, on the default path at bf3f4616 and with
    DS4_COPY_STREAM=1 at 2f7a9afb (bf3f4616 adds only the probe).
  • On main c62d5e3a, the scored output of c0871e3a is byte-identical to main's, on the default path and with
    DS4_COPY_STREAM=1: the same --score and settings as on 46c152a1 below, 4995 rows, ppl 3.0856, top-1 0.7618.
  • On main 46c152a1, the scored output of 34d2395f is byte-identical to main's, on the default path and with
    DS4_COPY_STREAM=1: the same --score, 4995 rows, ppl 3.0856, top-1 0.7618, with the CPU lane's experts read in
    place (--mmap-experts on). The stream then carries the VRAM experts (36.8 GB) and the dense weights (6.2 GB), not
    the RAM copy.
  • The reader layouts in loads (one reader 25.8-26.8 s, a reader a file 34.7-35.7 s, 2,2,2,1 38.9-39.9 s) and the read
    sizes (16 / 32 / 64 / 128 / 256 MB: 30.7 / 27.5 / 26.1 / 25.8 / 25.7 s).
  • --stream-probe, the output above, 27 s.

Failed: none observed.

Not run:

  • HIP on an AMD card (built only; there is no AMD card here); SYCL builds; a Windows build (the unbuffered reads and
    huge-page allocations have Windows paths that were not compiled).
  • Layers split across several cards (they keep the staging they had); DSpark loaded; EXL3 packs (they have no
    unbuffered file handles, so the stream stays off for them).
  • Other drives than the two above.
  • On main c62d5e3a and 46c152a1: the loads, the probe and a HIP build.

Limits

  • The stream is opt-in. It always reads from the disk: on a machine whose RAM holds the whole model and the copy,
    main's restart can copy from the page cache faster (copies of 8-9 s on this machine when the cache held the right
    part of the model).
  • During the load the stream holds 6.2 GB of staging and 256 MB of pinned arenas, freed afterwards. The RAM copy
    decides how much it copies while the staging is held, so a machine close to its RAM limit may copy fewer experts
    (from reading the code, not measured).
  • With the page drop, the model's files are no longer cached after a load, so a restart reads them from the disk.
  • With 81 GB in the page cache at the start (straight after a load on the default path), the stream's load took
    30.5 s: the stream itself its usual 21.6 s, the rest of the load 4.7 s longer, with 290 compaction stalls.

Used Claude.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4367 **Author:** [@Licenser](https://github.com/Licenser) **Created:** 10/27/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `dsv41-copy-stream` --- ### 📝 Commits (10+) - [`be01fda`](https://github.com/Niko1221/Strata/commit/be01fda11ecd8f9c28988f88f1dba20ccaeebb52) dsv41: drop each copied expert's file pages after the copy - [`994f720`](https://github.com/Niko1221/Strata/commit/994f720d8861901ebc8ed939689cd3908783e536) dsv41: DS4_COPY_STREAM=1 streams the expert copy in sequential reads - [`1c366aa`](https://github.com/Niko1221/Strata/commit/1c366aa3c4bc2bdc6d20b7d34b393e63efac55fc) dsv41: the VRAM experts in the same stream as the copy - [`6670b69`](https://github.com/Niko1221/Strata/commit/6670b695ad91daf4502673673a7666811418faa6) dsv41: a stream reader a range of a file, sized by the smallest file - [`d01603d`](https://github.com/Niko1221/Strata/commit/d01603d9fc4803cb5cbb16431ffae9ba5e36f145) dsv41: the dense weights in the experts' stream - [`a089178`](https://github.com/Niko1221/Strata/commit/a08917850adab91006d3048210b1df1c6bde34ae) dsv41: the dense weights' staging buffer on huge pages - [`b88db8e`](https://github.com/Niko1221/Strata/commit/b88db8eafe6c3c655496bdcfb2b7d39c2f23f8ea) dsv41: one stream reader for all files by default - [`18ba360`](https://github.com/Niko1221/Strata/commit/18ba360f72822ce22988021968b45cf8b8ac36bf) dsv41: the stream reads 128 MB at a time - [`be00571`](https://github.com/Niko1221/Strata/commit/be005716551d8621f521bd0acac0eb006549f18a) dsv41: the stream's chunk plan and reader ranges in functions of their own - [`c0871e3`](https://github.com/Niko1221/Strata/commit/c0871e3a775979a99c9e096e707099ea895416c7) dsv41: --stream-probe times the stream's reads and prints the settings to use ### 📊 Changes **3 files changed** (+675 additions, -165 deletions) <details> <summary>View changed files</summary> 📝 `src/dsv41/ds4_main.cpp` (+10 -1) 📝 `src/dsv41/ds4_model.cu` (+660 -164) 📝 `src/dsv41/ds4_model.hpp` (+5 -0) </details> ### 📄 Description ## Problem and result Loading DeepSeek-V4.1-Flash copies the CPU lane's experts into RAM piece by piece through the page cache, and loads the VRAM experts and the dense weights in passes of their own. The copy and the file pages it leaves behind compete for RAM. When part of the model is still in the page cache from the last run, free memory runs out mid-copy and the rest of the copy runs in direct reclaim and compaction: a restart is slower than a cold start. This PR makes the default copy drop each piece's file pages once it is copied, and adds `DS4_COPY_STREAM=1`, which reads the whole model in one sequential pass. | load, s (`ds4: loaded in`) | cold, best | cold, worst | restart, best | restart, worst | |------------------------------|-----------:|------------:|--------------:|---------------:| | main 5e8a4cca | 78.0 | 81.1 | 117.9 | not logged | | this PR, default | 53.1 | 54.1 | 52.7 | 53.1 | | this PR, `DS4_COPY_STREAM=1` | 25.8 | 25.9 | 25.6 | 25.8 | A restart is the same binary started straight after its own load. Main's restart time depends on how much of the model the page cache still holds: its two restarts on this machine, both with about 86 GB cached and 94 GB free at the start, took 95.1 s and 156.7 s to get the experts into memory (the slower run's whole load was not logged). Two runs of each case: | experts into memory, s | cold, best | cold, worst | restart, best | restart, worst | |------------------------------|-----------:|------------:|--------------:|---------------:| | main 5e8a4cca (copy) | 54.4 | 57.2 | 95.1 | 156.7 | | this PR, default (copy) | 29.5 | 30.3 | 29.3 | 29.8 | | this PR, `DS4_COPY_STREAM=1` | 21.5 | 21.5 | 21.4 | 21.5 | | speed against main | cold | restart | |------------------------------|----------|-------------------------------------------| | this PR, default | 1.4-1.5x | 2.2x (load), up to 5.3x (experts) | | this PR, `DS4_COPY_STREAM=1` | 3.0-3.1x | **4.6x** (load), **up to 7.3x** (experts) | With the stream the load takes the same 26 s from cold and on restart, and its 21.5 s carry the VRAM experts and the dense weights as well (138.2 GB against the copy's 94.5 GB). Main's restarts ran into direct reclaim and compaction (15,605 and 54,200 stalls in the faster one). Decode is unchanged (36.2-36.6 tok/s in every run), and the scored output is byte-identical on both paths. ## Related discussion None. The page drop changes the default copy; I would like to agree its scope here. The stream is opt-in. ## Approach 1. **Default: the page drop.** `copy_experts` drops each copied expert's file pages right after the copy, as the VRAM upload already does, so the copy and the page cache never hold the same bytes. `DS4_COPY_DROP=0` keeps them. 2. **`DS4_COPY_STREAM=1`: one pass.** One reader reads the model's files front to back in unbuffered reads of 128 MB into one of two pinned arenas, while a mover copies the other arena's pieces to their places: the CPU lane's experts to RAM, a one-card layer's VRAM experts to their slots (`cudaMemcpyAsync`), and the dense weights to a staging buffer on huge pages. The dense weights' VRAM is allocated first, as before, so the experts' placement sees it; their conversion and upload run after the stream, from the staging buffer. Gaps of up to 16 MB between pieces are read through. On the machine below the stream reads 6.4 GB/s, 95% of what its two drives read in one sequential stream. 3. **Settings for other drives.** `DS4_STREAM_RANGES=N` splits each file among up to N readers, for drive arrays that one reader cannot keep busy; `DS4_STREAM_MB` sets the read size. `strata-dsv41 --pack <pack> --stream-probe` times the reader layouts and read sizes on the pack's own files in about 30 s, without loading the model, and prints the settings to use: ``` ds4: stream probe: 4 files, 138.2 GB, unbuffered reads; 5 trials of 27.6 GB, each on bytes of its own readers read MB GB/s 1, the files in order (default) 128 6.54 4: 1,1,1,1 a file 128 4.28 7: 2,2,2,1 a file 128 3.98 1, the files in order 64 6.43 1, the files in order 256 6.32 ds4: the reads alone, not the copy into memory a load adds ds4: the settings for these drives: DS4_COPY_STREAM=1 DS4_STREAM_MB=128 DS4_STREAM_RANGES=0 ``` ## Validation Machine: an A40 48 GB used alone (`CUDA_VISIBLE_DEVICES`), Threadripper 3960X, 184 GiB RAM, two Samsung 970 EVO 1 TB in btrfs RAID0 (fio: 6.74 GB/s in one sequential stream), Linux 7.0, transparent huge pages `madvise`. Model: ISTA-DASLab DeepSeek-V4.1-Flash-1017-GSQ-RCO IQ2_XXS 2.00 bpw, the shipped expert profile. Lines that do not name main c62d5e3a or 46c152a1 were measured with this PR on main 5e8a4cca. **Passed:** - CUDA build (sm_75 and sm_86) of bf3f4616, on main 46c152a1 of 34d2395f, and on main c62d5e3a of c0871e3a. - HIP build (gfx1200 + gfx1201) of bf3f4616: `tools/dsv41/build_hip_linux.sh`, targets `strata-dsv41` and `ds4_iq_test`, ROCm 7.10.0a20251120 from AMD's TheRock wheels, setup's pin. Compiled and linked. The warnings main's HIP build lacks are 7 of a kind it has many of, a runtime call's `hipError_t` left unchecked (`-Wunused-value`), all at calls this branch adds. - Loads, the table above: `CUDA_VISIBLE_DEVICES=<A40> DS4_NO_EOS=1 strata-dsv41 --pack <pack> --max-context 8192 --eos 1 --dspark auto --dspark-conf 0.65 --experts 5 -v --n 32 --ids <1K prompt>` with each setting. Cold: the shards' cached pages dropped first (`posix_fadvise` DONTNEED); restart: straight after a load of the same binary, nothing dropped. - The scored output is byte-identical to main 5e8a4cca: `--score` on the multilingual set (999 positions, windows of 16, fixed placement), 4995 rows, ppl 3.1167, top-1 0.7638, on the default path at bf3f4616 and with `DS4_COPY_STREAM=1` at 2f7a9afb (bf3f4616 adds only the probe). - On main c62d5e3a, the scored output of c0871e3a is byte-identical to main's, on the default path and with `DS4_COPY_STREAM=1`: the same `--score` and settings as on 46c152a1 below, 4995 rows, ppl 3.0856, top-1 0.7618. - On main 46c152a1, the scored output of 34d2395f is byte-identical to main's, on the default path and with `DS4_COPY_STREAM=1`: the same `--score`, 4995 rows, ppl 3.0856, top-1 0.7618, with the CPU lane's experts read in place (`--mmap-experts on`). The stream then carries the VRAM experts (36.8 GB) and the dense weights (6.2 GB), not the RAM copy. - The reader layouts in loads (one reader 25.8-26.8 s, a reader a file 34.7-35.7 s, 2,2,2,1 38.9-39.9 s) and the read sizes (16 / 32 / 64 / 128 / 256 MB: 30.7 / 27.5 / 26.1 / 25.8 / 25.7 s). - `--stream-probe`, the output above, 27 s. **Failed:** none observed. **Not run:** - HIP on an AMD card (built only; there is no AMD card here); SYCL builds; a Windows build (the unbuffered reads and huge-page allocations have Windows paths that were not compiled). - Layers split across several cards (they keep the staging they had); DSpark loaded; EXL3 packs (they have no unbuffered file handles, so the stream stays off for them). - Other drives than the two above. - On main c62d5e3a and 46c152a1: the loads, the probe and a HIP build. ## Limits - The stream is opt-in. It always reads from the disk: on a machine whose RAM holds the whole model and the copy, main's restart can copy from the page cache faster (copies of 8-9 s on this machine when the cache held the right part of the model). - During the load the stream holds 6.2 GB of staging and 256 MB of pinned arenas, freed afterwards. The RAM copy decides how much it copies while the staging is held, so a machine close to its RAM limit may copy fewer experts (from reading the code, not measured). - With the page drop, the model's files are no longer cached after a load, so a restart reads them from the disk. - With 81 GB in the page cache at the start (straight after a load on the default path), the stream's load took 30.5 s: the stream itself its usual 21.6 s, the rest of the load 4.7 s longer, with 290 compaction stalls. Used Claude. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5022
No description provided.