[GH-ISSUE #570] GLM: start copying an expert from the SSD to the GPU before its read has finished (+12% decode, same output) #237

Open
opened 2026-10-29 21:23:04 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @pschaub on GitHub (Oct 27, 2026).
Original GitHub issue: https://github.com/mw00/project-maya/issues/570

Note

Claude Opus 5.5 found this change in Abyss, ported it to Maya and fully tested it on my PC. It also wrote the text below.
Abyss is under the MIT license, so the code can be reused here with attribution.

The problem: When a GLM model does not fit into VRAM and RAM, Maya keeps part of the experts on the SSD. When a token needs one of them, the GPU waits until the whole expert has been read from the SSD, and only then copies it over. Reading and copying run one after the other.

The idea: Copy each piece of the expert to the GPU as soon as that piece has been read, while the rest is still being read. Most of the copy then hides behind the read. zephel01/Abyss, a Maya fork, already does this (d626d96, on by default there). I ported it to Maya as an opt-in setting, MAYA_GLM_STREAM_FETCH=1.

today      read  [1][2][3][4]
           copy              [1][2][3][4]

streamed   read  [1][2][3][4]
           copy     [1][2][3][4]

What it brings: On my PC decode is about 12% faster, prefill stays the same, and the output is the same. It is off by default, so nothing changes unless you turn it on.

Who it helps: Everyone whose model does not fit into VRAM and RAM, so that experts are read from the SSD while answering. Without SSD reads the setting changes nothing.

Where it waits today: The host reads all pieces first and answers the GPU only after that:

github.com/mw00/project-maya@88a746c9f9/src/core/glm_fast_path.cu (L4487-L4511)

Then the GPU copies each expert in one go:

github.com/mw00/project-maya@88a746c9f9/src/kernels/cuda/glm_fast.cu (L4342-L4352)

With the setting on, the GPU waits per piece instead and copies it as soon as it is in RAM, as in Abyss's kernel (glm_fast.cu#L2783-L2804). This is a different overlap from #118, which overlaps compute with the copy; this one overlaps the copy with the SSD read.

Measurements: Maya-S-v2 IQ2_XXS at 32K, --bench against unchanged v1.1.29, 3 sessions of 5 rounds, the order changing from round to round:

decode, tok/s v1.1.29 patch, setting off MAYA_GLM_STREAM_FETCH=1 ratio
session 1 17.92 18.08 20.10 1.122
session 2 15.98 15.94 17.84 1.117
session 3 17.34 17.56 19.38 1.118

Decode was ahead in 15 of 15 rounds. Prefill is unchanged, and with the setting off the build is as fast as before. The output is identical (KL 0, same top token at 100 % over 1,471 positions). The code it touches is unchanged in v1.1.33.

I only measured Windows with one GPU and GGUF, so it would be good to see it on Linux and other GPUs before turning it on by default. A PR with the change as an opt-in setting follows.

My setup: Maya v1.1.29 built from source, MSVC 19.44, CUDA 13.4, RTX 5090 32 GB, 64 GB DDR5, Ryzen 9 9950X3D2, 8 TB NVMe, Windows 11, driver 621.01, Maya-S-v2 IQ2_XXS

Originally created by @pschaub on GitHub (Oct 27, 2026). Original GitHub issue: https://github.com/mw00/project-maya/issues/570 > [!NOTE] > Claude Opus 5.5 found this change in [Abyss](https://github.com/zephel01/Abyss), ported it to Maya and fully tested it on my PC. It also wrote the text below. > Abyss is under the [MIT license](https://github.com/zephel01/Abyss/blob/main/LICENSE), so the code can be reused here with attribution. **The problem:** When a GLM model does not fit into VRAM and RAM, Maya keeps part of the experts on the SSD. When a token needs one of them, the GPU waits until the whole expert has been read from the SSD, and only then copies it over. Reading and copying run one after the other. **The idea:** Copy each piece of the expert to the GPU as soon as that piece has been read, while the rest is still being read. Most of the copy then hides behind the read. zephel01/Abyss, a Maya fork, already does this ([d626d96](https://github.com/zephel01/Abyss/commit/d626d96d5b7aad8af90153c36ff520d464658cea), on by default there). I ported it to Maya as an opt-in setting, `MAYA_GLM_STREAM_FETCH=1`. ``` today read [1][2][3][4] copy [1][2][3][4] streamed read [1][2][3][4] copy [1][2][3][4] ``` **What it brings:** On my PC decode is about 12% faster, prefill stays the same, and the output is the same. It is off by default, so nothing changes unless you turn it on. **Who it helps:** Everyone whose model does not fit into VRAM and RAM, so that experts are read from the SSD while answering. Without SSD reads the setting changes nothing. **Where it waits today:** The host reads all pieces first and answers the GPU only after that: https://github.com/mw00/project-maya/blob/88a746c9f94d3c34a9ed31711ecfc64a2f03aa39/src/core/glm_fast_path.cu#L4487-L4511 Then the GPU copies each expert in one go: https://github.com/mw00/project-maya/blob/88a746c9f94d3c34a9ed31711ecfc64a2f03aa39/src/kernels/cuda/glm_fast.cu#L4342-L4352 With the setting on, the GPU waits per piece instead and copies it as soon as it is in RAM, as in Abyss's kernel ([glm_fast.cu#L2783-L2804](https://github.com/zephel01/Abyss/blob/d626d96d5b7aad8af90153c36ff520d464658cea/src/kernels/cuda/glm_fast.cu#L2783-L2804)). This is a different overlap from #118, which overlaps compute with the copy; this one overlaps the copy with the SSD read. **Measurements:** Maya-S-v2 IQ2_XXS at 32K, `--bench` against unchanged v1.1.29, 3 sessions of 5 rounds, the order changing from round to round: | decode, tok/s | v1.1.29 | patch, setting off | `MAYA_GLM_STREAM_FETCH=1` | ratio | |---|---|---|---|---| | session 1 | 17.92 | 18.08 | 20.10 | 1.122 | | session 2 | 15.98 | 15.94 | 17.84 | 1.117 | | session 3 | 17.34 | 17.56 | 19.38 | 1.118 | Decode was ahead in 15 of 15 rounds. Prefill is unchanged, and with the setting off the build is as fast as before. The output is identical (KL 0, same top token at 100 % over 1,471 positions). The code it touches is unchanged in v1.1.33. I only measured Windows with one GPU and GGUF, so it would be good to see it on Linux and other GPUs before turning it on by default. A PR with the change as an opt-in setting follows. My setup: Maya v1.1.29 built from source, MSVC 19.44, CUDA 13.4, RTX 5090 32 GB, 64 GB DDR5, Ryzen 9 9950X3D2, 8 TB NVMe, Windows 11, driver 621.01, Maya-S-v2 IQ2_XXS
Author
Owner

@klvnblst commented on GitHub (Oct 27, 2026):

Note

Measured and written up with Claude Code on my PC.

An independent data point on a different machine: a laptop where the gain is smaller, +2.7%, but it showed up in every pair. I ported the idea myself onto v1.1.33 (your PR wasn't up yet), opt-in as MAYA_GLM_STREAM_FETCH=1. The patch is below in case it helps the PR.

Setup: ASUS Strix SCAR 18, RTX 5090 Laptop 24 GB (sm_120), Core Ultra 9 275HX (no SMT), 64 GB RAM, Intel RST RAID 0 NVMe, Windows 11, CUDA 12.8, MSVC 19.44, driver 617.42. Model: Maya-S24 at 131072 context, MTP drafting.

Env:

  • In both runs: KV_INT8=1 DISK_READERS=8 DISK_SKIP_W=0.12 MISS_SKIP_W=0.12 MISS_SKIP_FROM=23 VRAM_EVICT=lru PCIE_SHARE=0.10 READ_CHUNKS=4.
  • The RAM tier was 42.2-42.8 GB in every run.

maya.py --bench, decode tok/s, in run order with the pairs interleaved:

pair off (v1.1.33) MAYA_GLM_STREAM_FETCH=1
1 25.6 26.1
2 25.5 26.0
3 24.3 26.0
4 25.3 25.7
5 25.1 25.4
mean 25.2 25.8 (+2.7%)

Results:

  • Streaming was ahead in 5 of 5 pairs.
  • Prefill is unchanged: 2K ~510 tok/s and 8K ~1120 tok/s on both.
  • With streaming on, READ_CHUNKS=8 was slower here (24.3). This RST RAID still prefers 4, as in #67.
  • tools/calibrate_glm.py on the streaming build again kept PCIE_SHARE=0.10 and READ_CHUNKS=4.

Why the gain is smaller here than your 12%:

  • About 2/3 of each token's experts already hit VRAM.
  • The disk does only ~7 reads/tok, ~1.9 ms each, so there is less read time left to hide the copy behind.

Quality (17 small tasks: code with hidden tests, exact answers, long-context needles; greedy, thinking off):

  • 16/17 with the setting off and 16/17 with it on.
  • A second run with it off scored 14/17, and its long answers diverged from the first off-run as much as from the streaming run. The divergence comes from the miss/disk skips (they depend on cache state), not from the streaming.
  • I did not run an exact KL comparison with the skips off.

How the port differs from Abyss d626d96:

  • Default off. The patch carries only the streamed fetch, without SEED_PRIOR or DEMOTE_TIES.
  • The response describes the stream itself. MoeResponse gains stream_mask, ready[8] and the chunk layout (s_gu, s_dn, s_chunks). Then moe_fetch_kernel and moe_wait_fetch_kernel both handle streamed entries without signature changes. That covers the token path, the MTP verify window (glm_window.cu) and the batched rows (glm_rows.cu): a call site still using the old whole-blob copy could otherwise copy chunks that have not landed.
  • The device reads stream_mask only for this route's own answer: when wait_seq != 0 and resp->seq == wait_seq.
  • The chunks are split across all SMs. With READ_CHUNKS=4 there are only 12 chunk bits, so the extra blocks share each chunk instead of idling. The ready-bit spin uses __nanosleep(128) to poll PCIe less while the copies run.
  • Guards:
    • The engine streams only GGUF packs (x_cb < 0), because EXL3's pieces are laid out differently.
    • It streams only with 3 * chunks <= 32, the bits one ready word holds. Abyss allows up to 16 chunks, i.e. 48 bits.
    • The lookahead's in-flight slots (nw) are waited for before the early answer.
  • The .cu files build as C++17, so the host sets the bits through std::atomic<unsigned> over the word rather than std::atomic_ref.
patch against v1.1.33 (88a746c)
diff --git a/include/strata/kernels/glm_fast.hpp b/include/strata/kernels/glm_fast.hpp
index c83d4c2..5d29e69 100644
--- a/include/strata/kernels/glm_fast.hpp
+++ b/include/strata/kernels/glm_fast.hpp
@@ -345,6 +345,13 @@ struct MoeResponse {
     volatile unsigned int seq;
     unsigned int ptr_mask;            // plan entries the host made resident: ptr[i] valid
     unsigned long long ptr[8];
+    // STRATA_GLM_STREAM_FETCH=1: entries in stream_mask were posted before their disk reads finished; ready[i] gets
+    // bit (part * s_chunks + chunk) as each chunk lands in ptr[i] (fast_read_part's layout: gate 0, up s_gu, down
+    // 2 s_gu), and the fetch kernels copy each chunk once its bit is set (zephel01/Abyss d626d96, mw00 #570)
+    unsigned int stream_mask;
+    unsigned int ready[8];
+    unsigned long long s_gu, s_dn;
+    int s_chunks;
     int n_upd;                        // table updates: tab[key] = val
     int upd_key[64];
     unsigned long long upd_val[64];
diff --git a/src/core/glm_fast_path.cu b/src/core/glm_fast_path.cu
index ddabcac..5de687a 100644
--- a/src/core/glm_fast_path.cu
+++ b/src/core/glm_fast_path.cu
@@ -4484,12 +4484,7 @@ void Glm5Model::fast_service() {
                     return std::max(1, std::min(16, v ? std::atoi(v) : 8));
                 }();
                 const int rch = F->read_chunks > 0 ? F->read_chunks : rch_env;
-                F->workers->run(nr * 3 * rch, [&](int job) {
-                    const int m = rd[job / (3 * rch)], part = job % (3 * rch);
-                    fast_read_part(il, ids[mi[m]], part / rch, dst[m], part % rch, rch);
-                });
-                for (int i = 0; i < ns; ++i) F->rload[(size_t) cls][(size_t) sl[i]].store(stv[i], std::memory_order_release);
-                if (nw > 0) {
+                const auto wait_ahead = [&] {   // the lookahead's reads in flight for this route land outside its jobs
                     const auto tw = std::chrono::steady_clock::now();
                     for (int i = 0; i < nw; ++i)
                         while ((F->rload[(size_t) cls][(size_t) wt[i]].load(std::memory_order_acquire) & 3u) != 2u)
@@ -4498,17 +4493,72 @@ void Glm5Model::fast_service() {
                     F->ah_wait_us.fetch_add((uint64_t) std::chrono::duration_cast<std::chrono::microseconds>(
                                                 std::chrono::steady_clock::now() - tw).count(),
                                             std::memory_order_relaxed);
-                }
+                };
+                // STRATA_GLM_STREAM_FETCH=1 (zephel01/Abyss d626d96, mw00 #570): the answer goes out BEFORE the reads,
+                // each read job sets its chunk's bit of ready[] once the chunk is in the slot, and the device's fetch
+                // copies the chunks as they land - the PCIe pull runs behind the SSD read instead of after it.  The
+                // same bytes reach the same slots (the output is unchanged).  GGUF packs only (EXL3's chunks are laid
+                // out differently), and at most 10 chunks a part (32 bits a ready word).
+                static const bool stream_env = [] {
+                    const char* v = getenv("STRATA_GLM_STREAM_FETCH");
+                    return v != nullptr && std::atoi(v) != 0;
+                }();
+                const bool stream = stream_env && nr > 0 && 3 * rch <= 32 && pack_layers_[(size_t) il].x_cb < 0;
                 gf::MoeResponse* Rp = F->resp_h;
-                Rp->ptr_mask = 0;
-                Rp->n_upd = 0;
-                Rp->cpu = 0;
-                for (int m = 0; m < nm; ++m) {
-                    Rp->ptr[mi[m]] = (unsigned long long) dst[m];
-                    Rp->ptr_mask |= 1u << mi[m];
+                if (stream) {
+                    static bool said = false;
+                    if (!said) {
+                        said = true;
+                        std::fprintf(stderr, "glm fast: CUDA%d disk misses stream to the GPU behind their reads "
+                                     "(STRATA_GLM_STREAM_FETCH, %d chunks a part)\n", dev_, rch);
+                    }
+                    if (nw > 0) wait_ahead();
+                    const auto& Lys = F->L[(size_t) il];
+                    Rp->ptr_mask = 0;
+                    Rp->n_upd = 0;
+                    Rp->cpu = 0;
+                    Rp->stream_mask = 0;
+                    Rp->s_gu = (unsigned long long) Lys.gu_bytes;
+                    Rp->s_dn = (unsigned long long) Lys.dn_bytes;
+                    Rp->s_chunks = rch;
+                    for (int m = 0; m < nm; ++m) {
+                        Rp->ptr[mi[m]] = (unsigned long long) dst[m];
+                        Rp->ptr_mask |= 1u << mi[m];
+                        Rp->ready[mi[m]] = 0xffffffffu;   // (landed already)
+                    }
+                    for (int r2 = 0; r2 < nr; ++r2) {
+                        Rp->ready[mi[rd[r2]]] = 0u;
+                        Rp->stream_mask |= 1u << mi[rd[r2]];
+                    }
+                    std::atomic_thread_fence(std::memory_order_release);
+                    Rp->seq = next;
+                }
+                F->workers->run(nr * 3 * rch, [&](int job) {
+                    const int m = rd[job / (3 * rch)], part = job % (3 * rch);
+                    fast_read_part(il, ids[mi[m]], part / rch, dst[m], part % rch, rch);
+                    if (stream) {   // the chunk is in the slot: its bit tells the fetch kernel
+                        // (the .cu files build as C++17: no std::atomic_ref - a lock-free atomic over the same word)
+                        static_assert(sizeof(std::atomic<unsigned int>) == sizeof(unsigned int) &&
+                                      std::atomic<unsigned int>::is_always_lock_free, "ready word");
+                        std::atomic_thread_fence(std::memory_order_release);
+                        reinterpret_cast<std::atomic<unsigned int>*>(&Rp->ready[mi[m]])
+                            ->fetch_or(1u << part, std::memory_order_release);
+                    }
+                });
+                for (int i = 0; i < ns; ++i) F->rload[(size_t) cls][(size_t) sl[i]].store(stv[i], std::memory_order_release);
+                if (!stream) {
+                    if (nw > 0) wait_ahead();
+                    Rp->ptr_mask = 0;
+                    Rp->n_upd = 0;
+                    Rp->cpu = 0;
+                    Rp->stream_mask = 0;
+                    for (int m = 0; m < nm; ++m) {
+                        Rp->ptr[mi[m]] = (unsigned long long) dst[m];
+                        Rp->ptr_mask |= 1u << mi[m];
+                    }
+                    std::atomic_thread_fence(std::memory_order_release);
+                    Rp->seq = next;
                 }
-                std::atomic_thread_fence(std::memory_order_release);
-                Rp->seq = next;
                 const uint64_t us = (uint64_t) std::chrono::duration_cast<std::chrono::microseconds>(
                                         std::chrono::steady_clock::now() - t0).count();
                 F->disk_reads.fetch_add((uint64_t) nr, std::memory_order_relaxed);   // read here (not ahead)
diff --git a/src/kernels/cuda/glm_fast.cu b/src/kernels/cuda/glm_fast.cu
index 268563c..f7de196 100644
--- a/src/kernels/cuda/glm_fast.cu
+++ b/src/kernels/cuda/glm_fast.cu
@@ -4339,11 +4339,76 @@ __global__ void __launch_bounds__(256) moe_prefetch_kernel(MoeDev d, size_t n16)
     }
 }
 
+// STRATA_GLM_STREAM_FETCH: the response's streamed entries (posted before their disk reads finished).  0 when the
+// route had no disk misses, or the answer is not this route's (a wait that gave up).
+__device__ unsigned int moe_stream_mask(const MoeDev& d) {
+    if (d.wait_seq == nullptr || d.resp == nullptr) return 0u;
+    const unsigned int need = *(volatile const unsigned int*) d.wait_seq;
+    if (need == 0u) return 0u;
+    const volatile MoeResponse* r = (const volatile MoeResponse*) d.resp;
+    return r->seq == need ? r->stream_mask : 0u;
+}
+
+// entry i of a streamed response: each chunk copied from its RAM landing slot once the host's read of it set its
+// ready bit (part * n + chunk, fast_read_part's layout), so the PCIe pull runs behind the SSD read instead of after
+// it (zephel01/Abyss d626d96).  Block b takes the chunks b modulo their count; with more blocks than chunks each chunk
+// is split between them.  A bit that never comes gives up after ~60 s like moe_wait.  (Every value deciding this
+// block's path is the same for all its threads: the __syncthreads below are reached by all or none.)
+__device__ void moe_stream_copy(const MoeDev& d, int i, const uint8_t* s, uint8_t* t) {
+    const volatile MoeResponse* r = (const volatile MoeResponse*) d.resp;
+    const int rch = r->s_chunks;
+    const unsigned long long gu = r->s_gu, dn = r->s_dn;
+    const int nb = 3 * rch, G = (int) gridDim.x, b = (int) blockIdx.x;
+    const bool split = G >= nb;
+    const int nsub = split ? G / nb : 1, sub = split ? b / nb : 0;
+    if (nb <= 0 || sub >= nsub) return;
+    for (int bit = split ? b % nb : b; bit < nb; bit += split ? nb : G) {
+        const int part = bit / rch, c = bit % rch;
+        const unsigned long long len = part == 2 ? dn : gu;
+        const unsigned long long piece = ((len + (unsigned long long) rch - 1) / rch + 4095ull) & ~4095ull;
+        const unsigned long long c0 = min(len, (unsigned long long) c * piece), c1 = min(len, c0 + piece);
+        if (c1 <= c0) continue;
+        const unsigned long long n = c1 - c0, per = ((n + nsub - 1) / nsub + 15ull) & ~15ull;
+        const unsigned long long a0 = min(n, (unsigned long long) sub * per), a1 = min(n, a0 + per);
+        if (a1 <= a0) continue;
+        if (threadIdx.x == 0) {
+            const long long t0 = clock64();
+            while (((r->ready[i] >> bit) & 1u) == 0u) {
+                if (clock64() - t0 > (1ll << 36)) break;
+#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 700
+                __nanosleep(128);   // (fewer polls over PCIe beside the copies)
+#endif
+            }
+            __threadfence_system();
+        }
+        __syncthreads();
+        const unsigned long long off = (part == 0 ? 0ull : part == 1 ? gu : 2ull * gu) + c0 + a0;
+        const uint8_t* sp = s + off;
+        uint8_t* tp = t + off;
+        const size_t nbytes = (size_t) (a1 - a0);
+        if ((((unsigned long long) sp | (unsigned long long) tp) & 15ull) == 0ull) {
+            const size_t m16 = nbytes / 16;
+            for (size_t j = threadIdx.x; j < m16; j += blockDim.x) ((uint4*) tp)[j] = ((const uint4*) sp)[j];
+            for (size_t j = m16 * 16 + threadIdx.x; j < nbytes; j += blockDim.x) tp[j] = sp[j];
+        } else {
+            for (size_t j = threadIdx.x; j < nbytes; j += blockDim.x) tp[j] = sp[j];
+        }
+        __syncthreads();
+    }
+}
+
 // one block per SM, 16-byte loads: the PCIe pull measured 11.5 GB/s (a little over cudaMemcpy's 10.7)
 __global__ void __launch_bounds__(256) moe_fetch_kernel(MoeDev d, int k, size_t n16) {
+    __shared__ unsigned int s_sm;
+    if (threadIdx.x == 0) s_sm = moe_stream_mask(d);
+    __syncthreads();
     for (int i = 0; i < k; ++i) {
         const unsigned long long src = d.fetch_src[i];
         if (src == 0ull) continue;
+        if ((s_sm >> i) & 1u) {
+            moe_stream_copy(d, i, (const uint8_t*) src, (uint8_t*) d.plan_ptr[i]);
+            continue;
+        }
         const uint4* s = (const uint4*) src;
         uint4* t = (uint4*) d.plan_ptr[i];
         for (size_t j = blockIdx.x * (size_t) blockDim.x + threadIdx.x; j < n16; j += (size_t) gridDim.x * blockDim.x)
@@ -4400,10 +4465,18 @@ __global__ void __launch_bounds__(256) moe_wait_fetch_kernel(MoeDev d, int n_emb
         __syncthreads();
     }
     if (tid < 8) s_src[tid] = tid < k ? ((volatile const unsigned long long*) d.fetch_src)[tid] : 0ull;
+    __shared__ unsigned int s_sm;   // STRATA_GLM_STREAM_FETCH's streamed entries (block 0 reset wait_seq: s_need)
+    if (tid == 0)
+        s_sm = s_need != 0u && d.resp != nullptr && ((volatile const MoeResponse*) d.resp)->seq == s_need
+                   ? ((volatile const MoeResponse*) d.resp)->stream_mask : 0u;
     __syncthreads();
     for (int i = 0; i < k; ++i) {
         const unsigned long long src = s_src[i];
         if (src == 0ull) continue;
+        if ((s_sm >> i) & 1u) {
+            moe_stream_copy(d, i, (const uint8_t*) src, (uint8_t*) d.plan_ptr[i]);
+            continue;
+        }
         const uint4* s = (const uint4*) src;
         uint4* t = (uint4*) d.plan_ptr[i];
         for (size_t j = blockIdx.x * (size_t) blockDim.x + threadIdx.x; j < n16; j += (size_t) gridDim.x * blockDim.x)
<!-- gh-comment-id:6455963476 --> @klvnblst commented on GitHub (Oct 27, 2026): > [!NOTE] > Measured and written up with Claude Code on my PC. An independent data point on a different machine: a **laptop** where the gain is smaller, **+2.7%**, but it showed up in every pair. I ported the idea myself onto **v1.1.33** (your PR wasn't up yet), opt-in as `MAYA_GLM_STREAM_FETCH=1`. The patch is below in case it helps the PR. **Setup:** ASUS Strix SCAR 18, RTX 5090 Laptop 24 GB (sm_120), Core Ultra 9 275HX (no SMT), 64 GB RAM, Intel RST RAID 0 NVMe, Windows 11, CUDA 12.8, MSVC 19.44, driver 617.42. Model: Maya-S24 at 131072 context, MTP drafting. **Env:** - In both runs: `KV_INT8=1 DISK_READERS=8 DISK_SKIP_W=0.12 MISS_SKIP_W=0.12 MISS_SKIP_FROM=23 VRAM_EVICT=lru PCIE_SHARE=0.10 READ_CHUNKS=4`. - The RAM tier was 42.2-42.8 GB in every run. **`maya.py --bench`, decode tok/s,** in run order with the pairs interleaved: | pair | off (v1.1.33) | `MAYA_GLM_STREAM_FETCH=1` | |---|---|---| | 1 | 25.6 | 26.1 | | 2 | 25.5 | 26.0 | | 3 | 24.3 | 26.0 | | 4 | 25.3 | 25.7 | | 5 | 25.1 | 25.4 | | **mean** | **25.2** | **25.8 (+2.7%)** | **Results:** - Streaming was ahead in 5 of 5 pairs. - Prefill is unchanged: 2K ~510 tok/s and 8K ~1120 tok/s on both. - With streaming on, `READ_CHUNKS=8` was slower here (24.3). This RST RAID still prefers 4, as in #67. - `tools/calibrate_glm.py` on the streaming build again kept `PCIE_SHARE=0.10` and `READ_CHUNKS=4`. **Why the gain is smaller here than your 12%:** - About 2/3 of each token's experts already hit VRAM. - The disk does only ~7 reads/tok, ~1.9 ms each, so there is less read time left to hide the copy behind. **Quality** (17 small tasks: code with hidden tests, exact answers, long-context needles; greedy, thinking off): - 16/17 with the setting off and 16/17 with it on. - A second run with it off scored 14/17, and its long answers diverged from the first off-run as much as from the streaming run. The divergence comes from the miss/disk skips (they depend on cache state), not from the streaming. - I did not run an exact KL comparison with the skips off. **How the port differs from Abyss d626d96:** - **Default off.** The patch carries only the streamed fetch, without SEED_PRIOR or DEMOTE_TIES. - **The response describes the stream itself.** `MoeResponse` gains `stream_mask`, `ready[8]` and the chunk layout (`s_gu`, `s_dn`, `s_chunks`). Then `moe_fetch_kernel` **and** `moe_wait_fetch_kernel` both handle streamed entries without signature changes. That covers the token path, the MTP verify window (`glm_window.cu`) and the batched rows (`glm_rows.cu`): a call site still using the old whole-blob copy could otherwise copy chunks that have not landed. - **The device reads `stream_mask` only for this route's own answer:** when `wait_seq != 0` and `resp->seq == wait_seq`. - **The chunks are split across all SMs.** With READ_CHUNKS=4 there are only 12 chunk bits, so the extra blocks share each chunk instead of idling. The ready-bit spin uses `__nanosleep(128)` to poll PCIe less while the copies run. - **Guards:** - The engine streams only GGUF packs (`x_cb < 0`), because EXL3's pieces are laid out differently. - It streams only with `3 * chunks <= 32`, the bits one `ready` word holds. Abyss allows up to 16 chunks, i.e. 48 bits. - The lookahead's in-flight slots (`nw`) are waited for before the early answer. - **The `.cu` files build as C++17**, so the host sets the bits through `std::atomic<unsigned>` over the word rather than `std::atomic_ref`. <details><summary>patch against v1.1.33 (88a746c)</summary> ```diff diff --git a/include/strata/kernels/glm_fast.hpp b/include/strata/kernels/glm_fast.hpp index c83d4c2..5d29e69 100644 --- a/include/strata/kernels/glm_fast.hpp +++ b/include/strata/kernels/glm_fast.hpp @@ -345,6 +345,13 @@ struct MoeResponse { volatile unsigned int seq; unsigned int ptr_mask; // plan entries the host made resident: ptr[i] valid unsigned long long ptr[8]; + // STRATA_GLM_STREAM_FETCH=1: entries in stream_mask were posted before their disk reads finished; ready[i] gets + // bit (part * s_chunks + chunk) as each chunk lands in ptr[i] (fast_read_part's layout: gate 0, up s_gu, down + // 2 s_gu), and the fetch kernels copy each chunk once its bit is set (zephel01/Abyss d626d96, mw00 #570) + unsigned int stream_mask; + unsigned int ready[8]; + unsigned long long s_gu, s_dn; + int s_chunks; int n_upd; // table updates: tab[key] = val int upd_key[64]; unsigned long long upd_val[64]; diff --git a/src/core/glm_fast_path.cu b/src/core/glm_fast_path.cu index ddabcac..5de687a 100644 --- a/src/core/glm_fast_path.cu +++ b/src/core/glm_fast_path.cu @@ -4484,12 +4484,7 @@ void Glm5Model::fast_service() { return std::max(1, std::min(16, v ? std::atoi(v) : 8)); }(); const int rch = F->read_chunks > 0 ? F->read_chunks : rch_env; - F->workers->run(nr * 3 * rch, [&](int job) { - const int m = rd[job / (3 * rch)], part = job % (3 * rch); - fast_read_part(il, ids[mi[m]], part / rch, dst[m], part % rch, rch); - }); - for (int i = 0; i < ns; ++i) F->rload[(size_t) cls][(size_t) sl[i]].store(stv[i], std::memory_order_release); - if (nw > 0) { + const auto wait_ahead = [&] { // the lookahead's reads in flight for this route land outside its jobs const auto tw = std::chrono::steady_clock::now(); for (int i = 0; i < nw; ++i) while ((F->rload[(size_t) cls][(size_t) wt[i]].load(std::memory_order_acquire) & 3u) != 2u) @@ -4498,17 +4493,72 @@ void Glm5Model::fast_service() { F->ah_wait_us.fetch_add((uint64_t) std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::steady_clock::now() - tw).count(), std::memory_order_relaxed); - } + }; + // STRATA_GLM_STREAM_FETCH=1 (zephel01/Abyss d626d96, mw00 #570): the answer goes out BEFORE the reads, + // each read job sets its chunk's bit of ready[] once the chunk is in the slot, and the device's fetch + // copies the chunks as they land - the PCIe pull runs behind the SSD read instead of after it. The + // same bytes reach the same slots (the output is unchanged). GGUF packs only (EXL3's chunks are laid + // out differently), and at most 10 chunks a part (32 bits a ready word). + static const bool stream_env = [] { + const char* v = getenv("STRATA_GLM_STREAM_FETCH"); + return v != nullptr && std::atoi(v) != 0; + }(); + const bool stream = stream_env && nr > 0 && 3 * rch <= 32 && pack_layers_[(size_t) il].x_cb < 0; gf::MoeResponse* Rp = F->resp_h; - Rp->ptr_mask = 0; - Rp->n_upd = 0; - Rp->cpu = 0; - for (int m = 0; m < nm; ++m) { - Rp->ptr[mi[m]] = (unsigned long long) dst[m]; - Rp->ptr_mask |= 1u << mi[m]; + if (stream) { + static bool said = false; + if (!said) { + said = true; + std::fprintf(stderr, "glm fast: CUDA%d disk misses stream to the GPU behind their reads " + "(STRATA_GLM_STREAM_FETCH, %d chunks a part)\n", dev_, rch); + } + if (nw > 0) wait_ahead(); + const auto& Lys = F->L[(size_t) il]; + Rp->ptr_mask = 0; + Rp->n_upd = 0; + Rp->cpu = 0; + Rp->stream_mask = 0; + Rp->s_gu = (unsigned long long) Lys.gu_bytes; + Rp->s_dn = (unsigned long long) Lys.dn_bytes; + Rp->s_chunks = rch; + for (int m = 0; m < nm; ++m) { + Rp->ptr[mi[m]] = (unsigned long long) dst[m]; + Rp->ptr_mask |= 1u << mi[m]; + Rp->ready[mi[m]] = 0xffffffffu; // (landed already) + } + for (int r2 = 0; r2 < nr; ++r2) { + Rp->ready[mi[rd[r2]]] = 0u; + Rp->stream_mask |= 1u << mi[rd[r2]]; + } + std::atomic_thread_fence(std::memory_order_release); + Rp->seq = next; + } + F->workers->run(nr * 3 * rch, [&](int job) { + const int m = rd[job / (3 * rch)], part = job % (3 * rch); + fast_read_part(il, ids[mi[m]], part / rch, dst[m], part % rch, rch); + if (stream) { // the chunk is in the slot: its bit tells the fetch kernel + // (the .cu files build as C++17: no std::atomic_ref - a lock-free atomic over the same word) + static_assert(sizeof(std::atomic<unsigned int>) == sizeof(unsigned int) && + std::atomic<unsigned int>::is_always_lock_free, "ready word"); + std::atomic_thread_fence(std::memory_order_release); + reinterpret_cast<std::atomic<unsigned int>*>(&Rp->ready[mi[m]]) + ->fetch_or(1u << part, std::memory_order_release); + } + }); + for (int i = 0; i < ns; ++i) F->rload[(size_t) cls][(size_t) sl[i]].store(stv[i], std::memory_order_release); + if (!stream) { + if (nw > 0) wait_ahead(); + Rp->ptr_mask = 0; + Rp->n_upd = 0; + Rp->cpu = 0; + Rp->stream_mask = 0; + for (int m = 0; m < nm; ++m) { + Rp->ptr[mi[m]] = (unsigned long long) dst[m]; + Rp->ptr_mask |= 1u << mi[m]; + } + std::atomic_thread_fence(std::memory_order_release); + Rp->seq = next; } - std::atomic_thread_fence(std::memory_order_release); - Rp->seq = next; const uint64_t us = (uint64_t) std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::steady_clock::now() - t0).count(); F->disk_reads.fetch_add((uint64_t) nr, std::memory_order_relaxed); // read here (not ahead) diff --git a/src/kernels/cuda/glm_fast.cu b/src/kernels/cuda/glm_fast.cu index 268563c..f7de196 100644 --- a/src/kernels/cuda/glm_fast.cu +++ b/src/kernels/cuda/glm_fast.cu @@ -4339,11 +4339,76 @@ __global__ void __launch_bounds__(256) moe_prefetch_kernel(MoeDev d, size_t n16) } } +// STRATA_GLM_STREAM_FETCH: the response's streamed entries (posted before their disk reads finished). 0 when the +// route had no disk misses, or the answer is not this route's (a wait that gave up). +__device__ unsigned int moe_stream_mask(const MoeDev& d) { + if (d.wait_seq == nullptr || d.resp == nullptr) return 0u; + const unsigned int need = *(volatile const unsigned int*) d.wait_seq; + if (need == 0u) return 0u; + const volatile MoeResponse* r = (const volatile MoeResponse*) d.resp; + return r->seq == need ? r->stream_mask : 0u; +} + +// entry i of a streamed response: each chunk copied from its RAM landing slot once the host's read of it set its +// ready bit (part * n + chunk, fast_read_part's layout), so the PCIe pull runs behind the SSD read instead of after +// it (zephel01/Abyss d626d96). Block b takes the chunks b modulo their count; with more blocks than chunks each chunk +// is split between them. A bit that never comes gives up after ~60 s like moe_wait. (Every value deciding this +// block's path is the same for all its threads: the __syncthreads below are reached by all or none.) +__device__ void moe_stream_copy(const MoeDev& d, int i, const uint8_t* s, uint8_t* t) { + const volatile MoeResponse* r = (const volatile MoeResponse*) d.resp; + const int rch = r->s_chunks; + const unsigned long long gu = r->s_gu, dn = r->s_dn; + const int nb = 3 * rch, G = (int) gridDim.x, b = (int) blockIdx.x; + const bool split = G >= nb; + const int nsub = split ? G / nb : 1, sub = split ? b / nb : 0; + if (nb <= 0 || sub >= nsub) return; + for (int bit = split ? b % nb : b; bit < nb; bit += split ? nb : G) { + const int part = bit / rch, c = bit % rch; + const unsigned long long len = part == 2 ? dn : gu; + const unsigned long long piece = ((len + (unsigned long long) rch - 1) / rch + 4095ull) & ~4095ull; + const unsigned long long c0 = min(len, (unsigned long long) c * piece), c1 = min(len, c0 + piece); + if (c1 <= c0) continue; + const unsigned long long n = c1 - c0, per = ((n + nsub - 1) / nsub + 15ull) & ~15ull; + const unsigned long long a0 = min(n, (unsigned long long) sub * per), a1 = min(n, a0 + per); + if (a1 <= a0) continue; + if (threadIdx.x == 0) { + const long long t0 = clock64(); + while (((r->ready[i] >> bit) & 1u) == 0u) { + if (clock64() - t0 > (1ll << 36)) break; +#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 700 + __nanosleep(128); // (fewer polls over PCIe beside the copies) +#endif + } + __threadfence_system(); + } + __syncthreads(); + const unsigned long long off = (part == 0 ? 0ull : part == 1 ? gu : 2ull * gu) + c0 + a0; + const uint8_t* sp = s + off; + uint8_t* tp = t + off; + const size_t nbytes = (size_t) (a1 - a0); + if ((((unsigned long long) sp | (unsigned long long) tp) & 15ull) == 0ull) { + const size_t m16 = nbytes / 16; + for (size_t j = threadIdx.x; j < m16; j += blockDim.x) ((uint4*) tp)[j] = ((const uint4*) sp)[j]; + for (size_t j = m16 * 16 + threadIdx.x; j < nbytes; j += blockDim.x) tp[j] = sp[j]; + } else { + for (size_t j = threadIdx.x; j < nbytes; j += blockDim.x) tp[j] = sp[j]; + } + __syncthreads(); + } +} + // one block per SM, 16-byte loads: the PCIe pull measured 11.5 GB/s (a little over cudaMemcpy's 10.7) __global__ void __launch_bounds__(256) moe_fetch_kernel(MoeDev d, int k, size_t n16) { + __shared__ unsigned int s_sm; + if (threadIdx.x == 0) s_sm = moe_stream_mask(d); + __syncthreads(); for (int i = 0; i < k; ++i) { const unsigned long long src = d.fetch_src[i]; if (src == 0ull) continue; + if ((s_sm >> i) & 1u) { + moe_stream_copy(d, i, (const uint8_t*) src, (uint8_t*) d.plan_ptr[i]); + continue; + } const uint4* s = (const uint4*) src; uint4* t = (uint4*) d.plan_ptr[i]; for (size_t j = blockIdx.x * (size_t) blockDim.x + threadIdx.x; j < n16; j += (size_t) gridDim.x * blockDim.x) @@ -4400,10 +4465,18 @@ __global__ void __launch_bounds__(256) moe_wait_fetch_kernel(MoeDev d, int n_emb __syncthreads(); } if (tid < 8) s_src[tid] = tid < k ? ((volatile const unsigned long long*) d.fetch_src)[tid] : 0ull; + __shared__ unsigned int s_sm; // STRATA_GLM_STREAM_FETCH's streamed entries (block 0 reset wait_seq: s_need) + if (tid == 0) + s_sm = s_need != 0u && d.resp != nullptr && ((volatile const MoeResponse*) d.resp)->seq == s_need + ? ((volatile const MoeResponse*) d.resp)->stream_mask : 0u; __syncthreads(); for (int i = 0; i < k; ++i) { const unsigned long long src = s_src[i]; if (src == 0ull) continue; + if ((s_sm >> i) & 1u) { + moe_stream_copy(d, i, (const uint8_t*) src, (uint8_t*) d.plan_ptr[i]); + continue; + } const uint4* s = (const uint4*) src; uint4* t = (uint4*) d.plan_ptr[i]; for (size_t j = blockIdx.x * (size_t) blockDim.x + threadIdx.x; j < n16; j += (size_t) gridDim.x * blockDim.x) ``` </details>
Sign in to join this conversation.
No labels
pull-request
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
mw00/project-maya#237
No description provided.