diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e5c142e..6515afb9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1271,6 +1271,12 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) target_link_libraries(gr_upmix_parity PRIVATE strata_prefill ) add_test(NAME gr_upmix_parity COMMAND gr_upmix_parity) set_tests_properties(gr_upmix_parity PROPERTIES SKIP_RETURN_CODE 77) + # the two kernels of the layer-major prompt path (STRATA_PREFILL_LAYER_MAJOR=1, #3976): moe_shared_init and + # moe_scatter_add against a host reference, bitwise for the scatter (GPU, synthetic, no model) + add_executable(lm_kernels_parity src/prefill/lm_kernels_parity.cu) + target_link_libraries(lm_kernels_parity PRIVATE strata_prefill ) + add_test(NAME lm_kernels_parity COMMAND lm_kernels_parity) + set_tests_properties(lm_kernels_parity PROPERTIES SKIP_RETURN_CODE 77) endif() # native_expert_grouped's group stride and fused SwiGLU + q8_1, bitwise vs the launches before them (GPU, synthetic, # no model; --bench adds v1/new timings of a window's 48 calls in a graph) diff --git a/include/strata/prefill/kernels.hpp b/include/strata/prefill/kernels.hpp index 5f1bf355..9be33b71 100644 --- a/include/strata/prefill/kernels.hpp +++ b/include/strata/prefill/kernels.hpp @@ -104,6 +104,14 @@ void gather_rows16(const uint16_t* x16, const int32_t* src, uint16_t* dst16, int /// bo[t, :] = shared[t, :] * sigmoid(sg[t]) + sum_k w[t, k] * D[slot[t, k], :] void moe_combine(const float* D, const int32_t* slot, const float* w, const float* shared, const float* sg, float* bo, int64_t T, void* stream, const float* row_sd = nullptr); +/// Layer-major prefill (STRATA_PREFILL_LAYER_MAJOR=1): acc[t, :] = shared[t, :] * sigmoid(sg[t]) - the shared expert's +/// part of moe_combine, written before the routed experts are known. +void moe_shared_init(const float* shared, const float* sg, float* acc, int64_t T, void* stream); +/// Layer-major prefill: one expert group's rows added into the super-chunk's accumulator, per token in a fixed order +/// (deterministic, no atomics): for each u < n_u, acc[u_tok[u], :] += sum_{j in [u_off[u], u_off[u+1])} u_w[j] * +/// Dm[u_rows[j], :]. The sum runs in the group's row order, not moe_combine's k order (FP32 rounding only). +void moe_scatter_add(const float* Dm, const int32_t* u_tok, const int32_t* u_off, const int32_t* u_rows, + const float* u_w, int64_t n_u, float* acc, void* stream); /// --peer-device's prompt share as sums: bo[t, :] = (sum over the k with slot[t, k] < rows_local of w[t, k] * /// D[slot[t, k], :], in k order) + peer[t, :] + shared[t, :] * sigmoid(sg[t]). `peer` (the peer's per-token sums) /// may be mapped host memory. diff --git a/include/strata/prefill/prefill.hpp b/include/strata/prefill/prefill.hpp index fa75c31d..da26dcc4 100644 --- a/include/strata/prefill/prefill.hpp +++ b/include/strata/prefill/prefill.hpp @@ -262,10 +262,28 @@ public: /// the share is on for chunks below 1024 tokens (STRATA_PREFILL_CPU_SHARE=0 turns it off). static void arm_cpu_share(bool applies, bool by_default = false); + /// Layer-major prefill (STRATA_PREFILL_LAYER_MAJOR=1): true while `on_chunk` reports a sub-chunk before the end of + /// its super-chunk. The session state is already at the super-chunk's end then, so no checkpoint may be taken. + /// On a layer split it is the last stage's (the one that reports the rows), asked through the first. + bool layer_major_mid() const { + const Prefill* p = this; + while (p->next_ != nullptr) p = p->next_; + return p->lm_mid_; + } + private: static uint64_t bytes_needed_impl(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk, bool owned_pages, bool src); kernels::cpu::ExpertPool* cpu_pool_ = nullptr; ///< set_cpu_pool + // Layer-major prefill: the super-chunk's buffers for an n-token prompt (its size, 0 = not this prompt: `why`), + // the run, and the buffers given back after it. `cap` (> 0): at most that many tokens (a layer split's stages + // share one size); lm_prepare_chain sizes every stage of a split. + int64_t lm_prepare(int64_t n, std::string& why, int64_t cap = 0); + int64_t lm_prepare_chain(int64_t n, std::string& why); + void lm_release_chain(); + bool run_layer_major(const int64_t* tokens, int64_t n, int64_t pos0, std::string& err); + void lm_release(); + bool lm_mid_ = false; // Stage-1 pipeline: intermediate stages return after handing their chunk to // the direct successor. The public run() drains the chain once at prompt end. bool run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::string& err); diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cu index 6fb5f714..365d5a90 100644 --- a/src/prefill/kernels.cu +++ b/src/prefill/kernels.cu @@ -2160,6 +2160,24 @@ __global__ void moe_combine4_kernel(const float* __restrict__ Dm, const int32_t* bo[i] = make_float4(s.x + sh.x * g, s.y + sh.y * g, s.z + sh.z * g, s.w + sh.w * g); } +__global__ void moe_shared_init_kernel(const float* __restrict__ shared, const float* __restrict__ sg, + float* __restrict__ acc, int64_t T) { + const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; + if (i >= T * N) return; + acc[i] = shared[i] * sigm(sg[i / N]); +} +// grid (n_u, ceil(N / 256)): one token of the group per block row, its rows summed in order +__global__ void moe_scatter_add_kernel(const float* __restrict__ Dm, const int32_t* __restrict__ u_tok, + const int32_t* __restrict__ u_off, const int32_t* __restrict__ u_rows, + const float* __restrict__ u_w, float* __restrict__ acc) { + const int64_t u = blockIdx.x; + const int64_t d = (int64_t) blockIdx.y * blockDim.x + threadIdx.x; + if (d >= N) return; + float s = 0.0f; + for (int32_t j = u_off[u]; j < u_off[u + 1]; ++j) s = fmaf(u_w[j], Dm[(int64_t) u_rows[j] * N + d], s); + acc[(int64_t) u_tok[u] * N + d] += s; +} + // ---------------------------------------------------------------- QSA helpers __global__ void rms_rows_kernel(float* __restrict__ x, const float* __restrict__ w, int64_t cols, int64_t ld, float eps) { __shared__ float sh[32]; @@ -2976,6 +2994,18 @@ void moe_combine(const float* Dm, const int32_t* slot, const float* w, const flo moe_combine_kernel<<>>(Dm, slot, w, shared, sg, bo, T, row_sd); check("moe_combine"); } +void moe_shared_init(const float* shared, const float* sg, float* acc, int64_t T, void* stream) { + if (T <= 0) return; + moe_shared_init_kernel<<>>(shared, sg, acc, T); + check("moe_shared_init"); +} +void moe_scatter_add(const float* Dm, const int32_t* u_tok, const int32_t* u_off, const int32_t* u_rows, + const float* u_w, int64_t n_u, float* acc, void* stream) { + if (n_u <= 0) return; + const dim3 grid((unsigned) n_u, (unsigned) ((N + 255) / 256)); + moe_scatter_add_kernel<<>>(Dm, u_tok, u_off, u_rows, u_w, acc); + check("moe_scatter_add"); +} void rms_rows(float* x, const float* w, int64_t rows, int64_t cols, int64_t ld, float eps, void* stream) { if (rows <= 0) return; rms_rows_kernel<<<(unsigned) rows, 256, 0, (cudaStream_t) stream>>>(x, w, cols, ld, eps); diff --git a/src/prefill/lm_kernels_parity.cu b/src/prefill/lm_kernels_parity.cu new file mode 100644 index 00000000..f7bc167c --- /dev/null +++ b/src/prefill/lm_kernels_parity.cu @@ -0,0 +1,165 @@ +// src/prefill/lm_kernels_parity.cu - the two kernels of the layer-major prompt path (STRATA_PREFILL_LAYER_MAJOR=1, +// #3976 / #1670) against a host reference, on synthetic data of the model's width (N = 2560): no model, a few MiB. +// moe_shared_init acc[t, :] = shared[t, :] * sigmoid(sg[t]) (the device sigmoid is __expf: within 1e-5 relative) +// moe_scatter_add acc[u_tok[u], :] += sum_{j in [u_off[u], u_off[u+1])} u_w[j] * Dm[u_rows[j], :], in that j order, +// one block per token and no atomics: the same bits as the host's fmaf chain, and the same bits on a +// second launch (deterministic) +// Exit 0 pass, 1 fail, 77 no CUDA device (ctest SKIP_RETURN_CODE). +#include "strata/prefill/kernels.hpp" + +#include + +#include +#include +#include +#include +#include +#include + +namespace { + +constexpr int N = 2560; + +bool ck(cudaError_t e, const char* what) { + if (e != cudaSuccess) std::printf("FAIL: %s: %s\n", what, cudaGetErrorString(e)); + return e == cudaSuccess; +} +template T* dalloc(size_t n) { + T* p = nullptr; + if (!ck(cudaMalloc(&p, n * sizeof(T)), "cudaMalloc")) std::exit(1); + return p; +} +template void up(T* d, const std::vector& h) { + if (!h.empty() && !ck(cudaMemcpy(d, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice), "upload")) std::exit(1); +} + +int fails = 0; + +void check_shared_init(int64_t T, std::mt19937& g) { + std::normal_distribution nd(0.0f, 1.0f); + std::vector shared((size_t) T * N), sg((size_t) T), ref((size_t) T * N), got((size_t) T * N); + for (auto& x : shared) x = nd(g); + for (auto& x : sg) x = nd(g) * 3.0f; + for (int64_t t = 0; t < T; ++t) + for (int d = 0; d < N; ++d) + ref[(size_t) t * N + d] = shared[(size_t) t * N + d] * (1.0f / (1.0f + std::exp(-sg[(size_t) t]))); + float *d_sh = dalloc(shared.size()), *d_sg = dalloc(sg.size()), *d_acc = dalloc(shared.size()); + up(d_sh, shared); + up(d_sg, sg); + cudaMemset(d_acc, 0xff, shared.size() * 4); + strata::prefill::moe_shared_init(d_sh, d_sg, d_acc, T, nullptr); + ck(cudaDeviceSynchronize(), "sync"); + ck(cudaMemcpy(got.data(), d_acc, got.size() * 4, cudaMemcpyDeviceToHost), "download"); + double worst = 0; + for (size_t i = 0; i < got.size(); ++i) { + const double scale = std::fabs(ref[i]) + 1e-6; + worst = std::fmax(worst, std::fabs((double) got[i] - ref[i]) / scale); + } + const bool ok = worst < 1e-5; + std::printf("moe_shared_init T=%lld: worst relative error %.2e %s\n", (long long) T, worst, ok ? "ok" : "FAIL"); + fails += ok ? 0 : 1; + cudaFree(d_sh); + cudaFree(d_sg); + cudaFree(d_acc); +} + +// rows: how many expert rows the group has; tokens: how many super-chunk tokens exist; each row belongs to a token, a token +// may own several rows of the group (an entry's rows are in token order, a token has up to 8 routed experts) +void check_scatter(int64_t tokens, int64_t rows, int max_per_tok, uint32_t seed) { + std::mt19937 g(seed); + std::normal_distribution nd(0.0f, 1.0f); + std::uniform_int_distribution pick(0, (int) tokens - 1); + // assign rows to tokens, at most max_per_tok per token + std::vector per((size_t) tokens, 0); + std::vector row_tok; + while ((int64_t) row_tok.size() < rows) { + const int t = pick(g); + if (per[(size_t) t] >= max_per_tok) continue; + ++per[(size_t) t]; + row_tok.push_back(t); + } + // the CSR the engine builds: unique tokens in order of first appearance, each token's rows in row order + std::vector u_tok, u_off, u_rows(rows); + std::vector slot((size_t) tokens, -1); + for (int32_t r = 0; r < (int32_t) rows; ++r) { + const int t = row_tok[(size_t) r]; + if (slot[(size_t) t] < 0) { slot[(size_t) t] = (int) u_tok.size(); u_tok.push_back(t); u_off.push_back(0); } + ++u_off[(size_t) slot[(size_t) t]]; + } + const int64_t nu = (int64_t) u_tok.size(); + std::vector fillp((size_t) nu); + int32_t acc_n = 0; + for (int64_t i = 0; i < nu; ++i) { const int32_t c = u_off[(size_t) i]; u_off[(size_t) i] = acc_n; fillp[(size_t) i] = acc_n; acc_n += c; } + u_off.push_back((int32_t) rows); + std::vector u_w(rows); + std::vector w_row((size_t) rows); + for (auto& x : w_row) x = std::fabs(nd(g)) * 0.2f; + for (int32_t r = 0; r < (int32_t) rows; ++r) { + const int32_t p = fillp[(size_t) slot[(size_t) row_tok[(size_t) r]]]++; + u_rows[(size_t) p] = r; + u_w[(size_t) p] = w_row[(size_t) r]; + } + std::vector Dm((size_t) rows * N), acc0((size_t) tokens * N); + for (auto& x : Dm) x = nd(g); + for (auto& x : acc0) x = nd(g); + // host reference, the kernel's order: s = fma chain over the token's rows, then acc += s + std::vector ref = acc0; + for (int64_t u = 0; u < nu; ++u) + for (int d = 0; d < N; ++d) { + float s = 0.0f; + for (int32_t j = u_off[(size_t) u]; j < u_off[(size_t) u + 1]; ++j) + s = std::fma(u_w[(size_t) j], Dm[(size_t) u_rows[(size_t) j] * N + d], s); + ref[(size_t) u_tok[(size_t) u] * N + d] += s; + } + float *d_Dm = dalloc(Dm.size()), *d_acc = dalloc(acc0.size()), *d_w = dalloc(u_w.size()); + int32_t *d_ut = dalloc(u_tok.size()), *d_uo = dalloc(u_off.size()), *d_ur = dalloc(u_rows.size()); + up(d_Dm, Dm); + up(d_w, u_w); + up(d_ut, u_tok); + up(d_uo, u_off); + up(d_ur, u_rows); + std::vector got1(acc0.size()), got2(acc0.size()); + for (int run = 0; run < 2; ++run) { + up(d_acc, acc0); + strata::prefill::moe_scatter_add(d_Dm, d_ut, d_uo, d_ur, d_w, nu, d_acc, nullptr); + ck(cudaDeviceSynchronize(), "sync"); + ck(cudaMemcpy((run ? got2 : got1).data(), d_acc, acc0.size() * 4, cudaMemcpyDeviceToHost), "download"); + } + size_t diff = 0, diff_runs = 0; + for (size_t i = 0; i < ref.size(); ++i) { + diff += std::memcmp(&ref[i], &got1[i], 4) != 0; + diff_runs += std::memcmp(&got1[i], &got2[i], 4) != 0; + } + const bool ok = diff == 0 && diff_runs == 0; + std::printf("moe_scatter_add tokens=%lld rows=%lld (<= %d per token, %lld tokens in the group): %zu values differ from the host " + "chain, %zu differ between two launches %s\n", (long long) tokens, (long long) rows, max_per_tok, (long long) nu, + diff, diff_runs, ok ? "ok" : "FAIL"); + fails += ok ? 0 : 1; + cudaFree(d_Dm); + cudaFree(d_acc); + cudaFree(d_w); + cudaFree(d_ut); + cudaFree(d_uo); + cudaFree(d_ur); +} + +} // namespace + +int main() { + int n = 0; + if (cudaGetDeviceCount(&n) != cudaSuccess || n == 0) { + std::printf("SKIP: no CUDA device\n"); + return 77; + } + std::mt19937 g(7); + check_shared_init(1, g); + check_shared_init(512, g); + check_shared_init(2049, g); + check_scatter(512, 1, 8, 1); // one row + check_scatter(512, 700, 8, 2); // a group inside one sub-chunk + check_scatter(4096, 5000, 8, 3); // several sub-chunks of a super-chunk + check_scatter(8192, 8192, 1, 4); // every token once + check_scatter(300, 2400, 8, 5); // every token at its limit + std::printf(fails == 0 ? "PASS\n" : "FAIL (%d)\n", fails); + return fails == 0 ? 0 : 1; +} diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 3a5485fb..2e424ff0 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -585,6 +585,8 @@ struct Stager { std::vector dma_done; std::vector jobs; std::unique_ptr[]> ready; + // layer-major prefill: a job the routing turned out not to need (`skip`) is not read once nobody has claimed it + std::unique_ptr[]> skipped; size_t ready_cap = 0; // gen << 32 | n << 16 | next index: a claim is a CAS on the generation it woke for (a thread late from the // previous layer can never take a job of this one - the expert pool's issue #29 lesson) @@ -664,6 +666,10 @@ struct Stager { else #endif while (issued.load(std::memory_order_acquire) <= i - kRing) std::this_thread::yield(); + // ...and its own copy into the buffer is done: `issued` is a watermark, so a job nobody waits for + // (the layer-major prefetch reads experts the routing may skip) can be passed while its thread still + // writes the buffer this job is about to fill + while (!ready[(size_t) (i - kRing)].load(std::memory_order_acquire)) std::this_thread::yield(); } // and done - for a generation's first kRing jobs that is the previous generation's last DMA from // the buffer, which nothing else waits for when a chunk ends without a sync (no MTP) or the DMA @@ -675,6 +681,7 @@ struct Stager { size_t nr = 0; core::ExpertSource* from = nullptr; for (int i = j; i < j + k; ++i) { + if (skipped[(size_t) i].load(std::memory_order_acquire)) continue; // nothing will read it: no copy const Job& jb = jobs[(size_t) i]; if (jb.from == nullptr) { std::memcpy(buf[i % kRing], jb.src, jb.bytes); continue; } from = jb.from; @@ -708,13 +715,31 @@ struct Stager { if (ready_cap < jobs.size()) { ready_cap = jobs.size() * 2; ready.reset(new std::atomic[ready_cap]); + skipped.reset(new std::atomic[ready_cap]); + } + for (size_t i = 0; i < jobs.size(); ++i) { + ready[i].store(0, std::memory_order_relaxed); + skipped[i].store(0, std::memory_order_relaxed); } - for (size_t i = 0; i < jobs.size(); ++i) ready[i].store(0, std::memory_order_relaxed); issued.store(0); ++gen; head.store((uint64_t) gen << 32 | (uint64_t) jobs.size() << 16, std::memory_order_release); cv.notify_all(); } + /// Layer-major prefill: job j's bytes are not needed after all (the routing skipped the expert) - a thread that has not + /// started it yet marks it ready without reading. Never waited for, so its buffer's content does not matter. + void skip(int j) { skipped[(size_t) j].store(1, std::memory_order_release); } + /// Layer-major prefill, before wait(j): the launching thread stages its jobs in increasing order, so a job below j + /// it has not issued was skipped and never gets a DMA - the watermark moves to j. A skipped job is never issued, + /// so without this a routed job after about a ring's depth of skipped ones waits for `issued` forever. + void pass(int j) { + if (issued.load(std::memory_order_relaxed) < j) { + issued.store(j, std::memory_order_release); +#if defined(STRATA_ATOMIC_WAIT) + issued.notify_all(); +#endif + } + } /// Job j's bytes, in a pinned buffer (waits for the copy). const uint8_t* wait(int j) { while (!ready[(size_t) j].load(std::memory_order_acquire)) std::this_thread::yield(); @@ -906,6 +931,59 @@ struct PinnedUpload { } }; +// STRATA_PREFILL_WARM=1: chunks below stream_all_min() take the per-layer walk, where each layer's file-tier +// experts are page faults of the stager's few threads (128 KiB of read-ahead each). This thread hands the source a +// layer's whole list at once (madvise(WILLNEED) - the drive sees one deep queue), and the next layer's list from +// the previous chunk while this layer computes. Only warms pages: what the stager copies is unchanged. +struct PrefillWarmer { + core::ExpertSource* src = nullptr; + struct Job { int64_t layer; std::vector experts; }; + std::vector queue; + bool quit = false; + std::mutex mu; + std::condition_variable cv; + std::thread thread; + std::atomic warmed{0}, dropped{0}; + + explicit PrefillWarmer(core::ExpertSource* s) : src(s) { + thread = std::thread([this] { + strata::aux_cpus::pin_current_thread(); + for (;;) { + Job j; + { + std::unique_lock lk(mu); + cv.wait(lk, [&] { return quit || !queue.empty(); }); + if (quit) return; + j = std::move(queue.front()); + queue.erase(queue.begin()); + } + src->warm(j.layer, j.experts.data(), (int64_t) j.experts.size()); + warmed.fetch_add((int64_t) j.experts.size(), std::memory_order_relaxed); + } + }); + } + ~PrefillWarmer() { + { + std::lock_guard lk(mu); + quit = true; + } + cv.notify_all(); + if (thread.joinable()) thread.join(); + } + void submit(int64_t layer, std::vector experts) { + if (experts.empty()) return; + { + std::lock_guard lk(mu); + while (queue.size() >= 4) { // behind: the oldest layer is done or nearly so + dropped.fetch_add((int64_t) queue.front().experts.size(), std::memory_order_relaxed); + queue.erase(queue.begin()); + } + queue.push_back({layer, std::move(experts)}); + } + cv.notify_one(); + } +}; + struct Prefill::Impl { const core::WeightTable* wt = nullptr; const core::ModelGeometry* g = nullptr; @@ -1013,6 +1091,8 @@ struct Prefill::Impl { // only write stream_all_min() - 1 of its rows into them; fused_layout() keeps the two in step (#583, #954) bool fused_bufs = false; std::unique_ptr stager; // the unpinned experts' host copies (step 4) + std::unique_ptr warmer; // STRATA_PREFILL_WARM=1: the per-layer walk's page warming + std::vector> warm_prev; // per layer: the file-tier experts of the previous chunk cudaEvent_t copied[RING_MAX] = {}, used[RING_MAX] = {}; bool stage_live[RING_MAX] = {}; // the event that releases each ring slot: its own `used`, or - when an MMQ group is gathered in one launch - the @@ -1057,6 +1137,28 @@ struct Prefill::Impl { // layer split: the next stage's GPU as a stream-only peer (set_stage_helper); its buffers are that stage's own // prompt buffers, bound per prompt, and it takes the place of `pp` for a run it helps std::unique_ptr help_pp; + // layer-major prefill (STRATA_PREFILL_LAYER_MAJOR=1): a super-chunk's state between a layer's attention halves + // and its experts - device buffers per prompt (lm_prepare / lm_release), pinned host buffers kept + struct LmBufs { + int64_t S = 0; // tokens of a super-chunk (0: not allocated) + bool r_dev = false; // its residual rows in VRAM (else R_host, copied per sub-chunk) + void* dev = nullptr; // the one device allocation the pointers below are carved from + float *mixed = nullptr, *acc = nullptr, *inj = nullptr, *R = nullptr, *w = nullptr; + uint16_t* mixed_h = nullptr; // the FFN half's FP16 rows (the FP16 path's inputs, port 2026-10-24) + int32_t *ids = nullptr, *tab = nullptr; + size_t tab_n = 0; + float* R_host = nullptr; + size_t R_host_n = 0; + int32_t* tab_host = nullptr; + size_t tab_host_n = 0; + int32_t* idsw_host = nullptr; + size_t idsw_host_n = 0; + int64_t free_mib = 0; // VRAM free when the buffers were sized (the log line) + // host mode: the residual rows of two sub-chunks on the device in turn (the chunk's R and R2), so the copy + // stream uploads the next one and downloads the last one while the compute stream works on this one + float* R2 = nullptr; + cudaEvent_t ev_in[2] = {}, ev_done[2] = {}, ev_out[2] = {}; + } lm; }; namespace { @@ -1148,6 +1250,13 @@ void Prefill::release() { if (impl_->kv_released) cudaEventDestroy(impl_->kv_released); if (impl_->kv_ready) cudaEventDestroy(impl_->kv_ready); if (impl_->grp_host) cudaFreeHost(impl_->grp_host); + if (impl_->lm.dev) cudaFree(impl_->lm.dev); + if (impl_->lm.R_host) cudaFreeHost(impl_->lm.R_host); + if (impl_->lm.tab_host) cudaFreeHost(impl_->lm.tab_host); + if (impl_->lm.idsw_host) cudaFreeHost(impl_->lm.idsw_host); + for (int b = 0; b < 2; ++b) + for (cudaEvent_t e : {impl_->lm.ev_in[b], impl_->lm.ev_done[b], impl_->lm.ev_out[b]}) + if (e) cudaEventDestroy(e); impl_->steps_up.release(); impl_->tok_up.release(); impl_->up_ring.release(); @@ -1536,6 +1645,15 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, co if (!m.stager->init((size_t) MAXBLOB(), threads)) ok = false; m.stager->batch = std::min(m.stager->batch, m.stager->kRing); } + if (!m.warmer && src != nullptr && src->warms()) { + const char* wv = std::getenv("STRATA_PREFILL_WARM"); + if (wv != nullptr && std::atoi(wv) != 0) { + m.warmer = std::make_unique(src); + m.warm_prev.assign((size_t) g.n_layers, {}); + std::fprintf(stderr, "prefill: STRATA_PREFILL_WARM=1: the per-layer walk warms this layer's and the next " + "layer's file-tier experts\n"); + } + } m.steps_host.resize(T * strata::kernels::kStepCount); m.steps_up.init(T * strata::kernels::kStepCount); m.ids_host.resize(T * K); m.slot_host.resize(T * K); m.src_host.resize(T * K); m.cnt.resize(m.g->n_expert); m.off.resize(m.g->n_expert + 1); @@ -4981,6 +5099,23 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri if (!b) { err = "prefill: expert source has no blob"; return false; } js.push_back({b, (size_t) lay.blob_bytes(l)}); } + if (m.warmer) { + std::vector cur; + for (size_t j = 0; j < order.size(); ++j) + if (job_of[j] >= 0) cur.push_back(order[j]); + m.warm_prev[(size_t) l] = cur; + m.warmer->submit(l, std::move(cur)); + if (l + 1 < m.g->n_layers) { + std::vector nxt; + for (int64_t e : m.warm_prev[(size_t) (l + 1)]) { + if (m.host_res && m.cache && m.host_res[(size_t) (l + 1) * m.g->n_expert + e] >= 0) + continue; + if (m.src->pinned(l + 1, (int32_t) e)) continue; + nxt.push_back(e); + } + m.warmer->submit(l + 1, std::move(nxt)); + } + } m.stager->start(std::move(js)); } StagerDone stager_done{stream_all ? nullptr : m.stager.get()}; @@ -5720,6 +5855,1176 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri return true; } +// ============================================================================================================ +// Layer-major prefill (STRATA_PREFILL_LAYER_MAJOR=1). +// +// The default prompt path reads a chunk (here 512 tokens) through all 48 layers, so every chunk streams every routed +// expert of every layer to the GPU again - ~25-34 GB of expert blobs per chunk on Q2_0, which is what bounds a prompt +// on a small card (PCIe / NVMe). This path reads a SUPER-CHUNK of S tokens layer by layer instead: for layer l it +// runs each sub-chunk's attention half, the FFN half's hyper-connection read, the router and the shared expert +// (in order, so the GDN / QSA / PLE state advance exactly as in the default path), parks what the experts need +// (the FFN input rows, the routing, the shared expert's part of the output) and then runs the layer's experts ONCE +// for all S tokens: each routed expert is staged once, its rows of every sub-chunk computed in one MMQ group, and +// the results added per token into the super-chunk's accumulator. The FFN half's hyper-connection write of layer l +// runs at the start of the sub-chunk's layer l + 1 (or in the final pass). Expert traffic drops by ~S / 512. +// +// The residual rows of the super-chunk live in VRAM when they fit (STRATA_PREFILL_LM_R=vram) or in pinned host +// memory, copied in and out per sub-chunk and layer (host). Only the experts' sum differs from the default path: +// it is added per expert group in id order, not in the router's k order (FP32 rounding), and the MMQ products run on +// other row groupings. on_chunk reports every sub-chunk in order from the final pass; while it reports one before +// the super-chunk's end, layer_major_mid() is true (the session state is the super-chunk's end: no checkpoint). +namespace { +bool lm_env() { + static const bool v = [] { const char* e = std::getenv("STRATA_PREFILL_LAYER_MAJOR"); return e && std::atoi(e) != 0; }(); + return v; +} + +bool lm_even_env() { // even super-chunks (STRATA_PREFILL_LM_EVEN=0: the largest that fits, then the rest) + static const bool v = [] { const char* e = std::getenv("STRATA_PREFILL_LM_EVEN"); return !(e && e[0] == '0'); }(); + return v; +} + +int64_t lm_tokens_env() { // the super-chunk's target size (0: the default) + static const int64_t v = [] { const char* e = std::getenv("STRATA_PREFILL_LM_TOKENS"); return e ? (int64_t) std::atoll(e) : (int64_t) 0; }(); + return v; +} +int lm_r_env() { // 0 auto, 1 vram, 2 host + static const int v = [] { + const char* e = std::getenv("STRATA_PREFILL_LM_R"); + if (!e) return 0; + const std::string s(e); + return s == "vram" ? 1 : s == "host" ? 2 : 0; + }(); + return v; +} +int64_t lm_margin_bytes() { // VRAM left free while a prompt runs (the verify windows, the driver) + static const int64_t v = [] { + const char* e = std::getenv("STRATA_PREFILL_LM_MARGIN_MIB"); + return (int64_t) (e ? std::atoll(e) : 96) << 20; + }(); + return v; +} +constexpr int64_t kLmDefaultTokens = 8192; +// the device tables of one layer, in int32s: row_tok (rows) | bounds (2 E) | u_tok (rows) | u_off (rows + E) | +// u_rows (rows) | u_w (rows), E = the most entries a layer can split into +int64_t lm_entries_max(int64_t rows, int64_t G) { return NE + rows / std::max(G, 1) + 2; } +int64_t lm_table_ints(int64_t rows, int64_t G) { return 5 * rows + 3 * lm_entries_max(rows, G); } +} // namespace + +void Prefill::lm_release() { + Impl& m = *impl_; + const core::OnDevice on_device(m.device); + if (m.lm.dev) { + cudaStreamSynchronize(m.cs); + cudaFree(m.lm.dev); + } + m.lm.dev = nullptr; + m.lm.mixed = m.lm.acc = m.lm.inj = m.lm.R = m.lm.w = m.lm.R2 = nullptr; + m.lm.mixed_h = nullptr; + m.lm.ids = m.lm.tab = nullptr; + m.lm.S = 0; +} + +int64_t Prefill::lm_prepare(int64_t n, std::string& why, int64_t cap) { + Impl& m = *impl_; + if (m.g == nullptr) { why = "not initialized"; return 0; } +#if defined(STRATA_USE_HIP) || defined(STRATA_HIP_GFX906) + // only NVIDIA CUDA has run this path; on an AMD build the prompt stays on the chunk-major path + why = "layer-major prefill has only been run on NVIDIA CUDA"; + return 0; +#endif + const core::ModelGeometry& g = *m.g; + const core::OnDevice on_device(m.device); // a layer split's later stage is sized on its own GPU + // layer split (2026-10-24): each stage runs its own layers layer-major over the same super-chunk and hands the + // super-chunk's rows to the next one (lm_prepare_chain); the peer GPU's expert share and the stage helper have + // no layer-major form + const bool split = stage_lb_ != 0 || stage_le_ != g.n_layers || next_ != nullptr; + if (hand_in_ != nullptr) { why = "a layer split's chunk hand-off is in use"; return 0; } + if (m.pp || helper_ != nullptr || core::peer_portable()) { why = "a peer GPU"; return 0; } + // the PLE block (layer 1) reads the token history: on a split it runs in a first stage that holds layers 0 and 1 + // (a later stage never reaches it) + if (split && m.ss->ple.ready() && stage_lb_ <= 1 && !(stage_lb_ == 0 && stage_le_ >= 2)) { + why = "a layer split whose PLE layer is not in the first stage"; + return 0; + } + if (embd_rows != nullptr) { why = "an image prompt"; return 0; } + // features of run_impl this walk does not carry: the DFlash2 drafter's prompt taps, and KVA's approximate late layers + // (a prompt that wants them takes the chunk-major path, which has both) + if (!tap_layers_.empty()) { why = "the DFlash2 drafter's prompt taps"; return 0; } + if (kva_config().on) { why = "STRATA_KVA is on"; return 0; } + if (stop_at_chunk) { why = "a batch read that yields at chunk boundaries"; return 0; } + if (force_chunk_ > 0) { why = "a --prefill-pipe plan sets the chunks of this read"; return 0; } // (left set for run_impl) +#if STRATA_EXPERT_NIBBLE_BUILT // the 4-bit expert slots (STRATA_EXPERT_NIBBLE) are gathered for MMQ only by run_impl + for (int64_t l = 0; l < g.n_layers; ++l) + if (core::nibble::layer(l).on) { why = "STRATA_EXPERT_NIBBLE (the experts are 4-bit slots)"; return 0; } +#endif + // run_impl's debug hooks on the routing and the rows have no layer-major form + if (std::getenv("STRATA_DBG_FEAT") || std::getenv("STRATA_DBG_ROUTE_OVERRIDE") || std::getenv("STRATA_PREFILL_DUMP_R_ALL")) { + why = "a routing or row debug dump"; + return 0; + } + const MmqPlan& mp = mmq_plan(); + if ((int64_t) mp.layer.size() < g.n_layers) { why = "the expert plan has fewer layers than the model"; return 0; } + // each layer's experts on MMQ, or (port, 2026-10-24) on run_impl's FP16 path; the buffers each one needs + int64_t n_mmq = 0; + for (int64_t l = 0; l < g.n_layers; ++l) n_mmq += mp.layer[(size_t) l] ? 1 : 0; + if (n_mmq > 0 && (!m.grp_gu || !m.grp_d || !m.Xq || !m.H || !m.Hq)) { why = "the MMQ buffers are missing"; return 0; } + if (n_mmq < g.n_layers) { + bool dq_ok = true; + for (int i = 0; i < DQ; ++i) dq_ok = dq_ok && m.dq_gu[i] && m.dq_d[i]; + if (!m.Xs || !m.Hh || !dq_ok) { why = "the FP16 expert path's buffers are missing"; return 0; } + } + // 0.2.2's expert formats this path's MMQ pass does not carry (port, 2026-10-24): APR's Hadamard-folded layers, + // NVFP4's tails and w4a4 activations, and IQ1_M layers that only the WMMA tiles run + { + const strata::kernels::cpu::ExpertLayout& lay = strata::kernels::cpu::expert_layout(); + for (int64_t l = 0; l < g.n_layers; ++l) { + if (l < (int64_t) mp.wmma_only.size() && mp.wmma_only[(size_t) l]) { why = "a layer needs the WMMA tiles"; return 0; } + if (!lay.native) continue; + const auto& f = lay.fmt[(size_t) l]; + if (f.had_gu || f.had_d) { why = "an APR (Hadamard-folded) layer"; return 0; } + if (f.tail_off || mmq::fp4_activations(f.gu_type)) { why = "an NVFP4 layer"; return 0; } + } + } + if (m.fused_bufs) { why = "the fused expert layout"; return 0; } + if (!m.GU || !m.Dm || (n_mmq > 0 && !m.ids_identity)) { why = "the MoE buffers are missing"; return 0; } + // (an explicit STRATA_PREFILL_LM_TOKENS may be one chunk: the A/B against the default path, experts aside) + const int64_t min_s = lm_tokens_env() > 0 ? m.T : 2 * m.T; + if (n < min_s) { why = "a short prompt"; return 0; } + const int64_t want = lm_tokens_env() > 0 ? lm_tokens_env() : kLmDefaultTokens; + int64_t S = std::max(min_s, want / m.T * m.T); + S = std::min(S, (n + m.T - 1) / m.T * m.T); + if (cap > 0) { + if (cap % m.T != 0 || cap < min_s) { why = "the layer split's stages read different chunks"; return 0; } + S = std::min(S, cap); + } + const int64_t G = m.T * K; + const auto dev_bytes = [&](int64_t s, bool r_dev) { + const uint64_t per = (uint64_t) (2 * N + HC + 2 * K) * 4 + (uint64_t) N * 2 + (r_dev ? (uint64_t) D * 4 : 0); + return (uint64_t) s * per + (uint64_t) lm_table_ints(s * K, G) * 4 + 4096 + + (r_dev ? 0 : (uint64_t) m.T * D * 4 + 256); + }; + size_t free_b = 0, total_b = 0; + cudaMemGetInfo(&free_b, &total_b); + const int64_t room = (int64_t) free_b - lm_margin_bytes(); + m.lm.free_mib = (int64_t) (free_b >> 20); + auto fit = [&](bool r_dev) { // the largest super-chunk (a multiple of the chunk) that fits `room` + int64_t s = S; + while (s >= min_s && (int64_t) dev_bytes(s, r_dev) > room) s -= m.T; + return s >= min_s ? s : (int64_t) 0; + }; + // a stage after the first: its rows arrive in R_host (the previous stage writes them there), so they stay there + const int mode = stage_lb_ > 0 ? 2 : lm_r_env(); + bool r_dev = false; + int64_t s_use = 0; + if (mode == 1) { s_use = fit(true); r_dev = true; } + else if (mode == 2) { s_use = fit(false); } + else { // auto: VRAM when the whole target fits there, else pinned host (the bigger super-chunk wins) + const int64_t sv = fit(true); + if (sv == S) { s_use = sv; r_dev = true; } else s_use = fit(false); + } + if (s_use <= 0) { + why = "no VRAM for a super-chunk (" + std::to_string(free_b >> 20) + " MiB free)"; + return 0; + } + Impl::LmBufs& lb = m.lm; + // pinned host: the routing's copy, the tables, and (host mode) the residual rows + const size_t idsw = (size_t) (2 * s_use * K), tab = (size_t) lm_table_ints(s_use * K, G); + auto host_grow = [](int32_t*& p, size_t& have, size_t want_n) -> bool { + if (have >= want_n) return true; + if (p) cudaFreeHost(p); + p = nullptr; + have = 0; + if (cudaHostAlloc((void**) &p, want_n * 4, cudaHostAllocDefault) != cudaSuccess) { cudaGetLastError(); p = nullptr; return false; } + have = want_n; + return true; + }; + if (!host_grow(lb.idsw_host, lb.idsw_host_n, idsw) || !host_grow(lb.tab_host, lb.tab_host_n, tab)) { + why = "no pinned RAM for the routing tables"; return 0; + } + if (!r_dev) { + int32_t* rh = (int32_t*) lb.R_host; + if (!host_grow(rh, lb.R_host_n, (size_t) s_use * D)) { why = "no pinned RAM for the residual rows"; lb.R_host = nullptr; return 0; } + lb.R_host = (float*) rh; + } + const uint64_t bytes = dev_bytes(s_use, r_dev); + void* d = nullptr; + if (cudaMalloc(&d, bytes) != cudaSuccess) { + cudaGetLastError(); + why = "the super-chunk's buffers (" + std::to_string(bytes >> 20) + " MiB) do not fit"; + return 0; + } + lb.dev = d; + uint8_t* q = (uint8_t*) d; + auto carve_b = [&](size_t b) { uint8_t* p = q; q += (b + 255) & ~(size_t) 255; return p; }; + lb.mixed = (float*) carve_b((size_t) s_use * N * 4); + lb.acc = (float*) carve_b((size_t) s_use * N * 4); + lb.mixed_h = (uint16_t*) carve_b((size_t) s_use * N * 2); + lb.inj = (float*) carve_b((size_t) s_use * HC * 4); + lb.ids = (int32_t*) carve_b((size_t) s_use * K * 4); + lb.w = (float*) carve_b((size_t) s_use * K * 4); + lb.R = r_dev ? (float*) carve_b((size_t) s_use * D * 4) : nullptr; + lb.tab = (int32_t*) carve_b(tab * 4); + lb.tab_n = tab; + lb.R2 = r_dev ? nullptr : (float*) carve_b((size_t) m.T * D * 4); + for (int b = 0; b < 2; ++b) + for (cudaEvent_t* e : {&lb.ev_in[b], &lb.ev_done[b], &lb.ev_out[b]}) + if (*e == nullptr && cudaEventCreateWithFlags(e, cudaEventDisableTiming) != cudaSuccess) { + cudaGetLastError(); + *e = nullptr; + why = "the copy events"; + lm_release(); + return 0; + } + lb.r_dev = r_dev; + lb.S = s_use; + return s_use; +} + +// A layer split: every stage gets the same super-chunk (the smallest any of them fits), re-sized until they agree. +int64_t Prefill::lm_prepare_chain(int64_t n, std::string& why) { + // a later stage's hand_in_ still points at the last chunk a chunk-major prompt handed it (run_impl never clears it): + // with no hand-off in flight that is stale, not in use (a short prompt first would otherwise turn layer-major off + // for every later prompt) + for (Prefill* p = this; p->next_ != nullptr; p = p->next_) + if (p->next_run_.valid()) { why = "a layer split's chunk hand-off is in flight"; return 0; } + for (Prefill* p = next_; p != nullptr; p = p->next_) { + p->hand_in_ = nullptr; + p->hand_ev_ = nullptr; + p->hand_pieces_ = 0; + } + int64_t cap = 0; + for (int round = 0; round < 4; ++round) { + bool same = true; + for (Prefill* p = this; p != nullptr; p = p->next_) { + Impl::LmBufs& lb = p->impl_->lm; + if (lb.S > 0 && lb.S == cap) continue; + if (lb.S > 0) p->lm_release(); + const int64_t s = p->lm_prepare(n, why, cap); + if (s <= 0) { + if (p != this) + why = "layers " + std::to_string(p->stage_lb_) + ".." + std::to_string(p->stage_le_) + ": " + why; + lm_release_chain(); + return 0; + } + if (cap > 0 && s != cap) same = false; + cap = s; + } + if (same) { + // even super-chunks (STRATA_PREFILL_LM_EVEN, default on): n split into the same number of super-chunks + // of near-equal size (32K at a 28K fit: 2 x 16K, not 28K + 4K), so a stage's residual rows fit VRAM + // and a layer split's stages overlap on every super-chunk + const int64_t T = impl_->T; + const int64_t k = (n + cap - 1) / cap; + const int64_t even = ((n + k - 1) / k + T - 1) / T * T; + if (k < 2 || even >= cap || !lm_even_env()) return cap; + for (Prefill* p = this; p != nullptr; p = p->next_) { + p->lm_release(); + if (p->lm_prepare(n, why, even) != even) { + if (p != this) + why = "layers " + std::to_string(p->stage_lb_) + ".." + std::to_string(p->stage_le_) + ": " + why; + lm_release_chain(); + return 0; + } + } + return even; + } + } + why = "the layer split's stages found no common super-chunk"; + lm_release_chain(); + return 0; +} + +void Prefill::lm_release_chain() { + for (Prefill* p = this; p != nullptr; p = p->next_) + if (p->impl_) p->lm_release(); +} + +bool Prefill::run_layer_major(const int64_t* tokens, int64_t n, int64_t pos0, std::string& err) { + err.clear(); + Impl& m = *impl_; + const core::OnDevice on_device(m.device); + const core::ModelGeometry& g = *m.g; + core::SessionState& ss = *m.ss; + const auto t_start = Clock::now(); + const cudaStream_t cs = (cudaStream_t) m.cs; + Impl::LmBufs& lb = m.lm; + const int64_t S = lb.S, L = g.n_layers; + // a layer split's stage (2026-10-24): its layers [LB, LE); a later stage's rows arrive in R_host, written by the + // stage before it, and a stage before the last writes its final rows into the next stage's R_host, then runs + // the next stage on that super-chunk on a thread while it reads its own next super-chunk (one hand-off buffer: + // its final pass waits until the next stage has finished the previous one) + const int64_t LB = stage_lb_, LE = stage_le_; + if (hand_in_ == nullptr) stopped_at_ = -1; // (stop_at_chunk is declined: this run reads every token) + strata::kernels::QsaShapes s = core::qsa_shapes(g); // as 0.2.2's run_impl + const uint64_t gdn_floats = (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size + + (uint64_t) g.ssm_conv_channels * (g.ssm_d_conv - 1); + int32_t prev[2] = {ss.ple_prev[0], ss.ple_prev[1]}; + PfTimer pt; + // the MMQ row table lives in the borrowed cache slots when the prompt path borrows: written for every prompt + // (none without an MMQ layer: the FP16 path has no such table) + if (m.ids_identity) mmq::iota(m.ids_identity, m.T * K, m.cs); + // ---- the PLE rows, read per chunk on a thread as run_impl does (the same state machine, in chunk order) + const bool ple_on = ss.ple.ready(); + static const bool ple_batch_env = [] { + const char* v = std::getenv("STRATA_PLE_BATCH"); + return v == nullptr || std::atoi(v) != 0; + }(); + const bool ple_batch = ple_on && ple_batch_env && strata::kernels::ple_native_postops_enabled() && + (ss.ple.w.key_bf16 != nullptr || ss.ple.w.key_native_data != nullptr) && + m.region_bytes / ((uint64_t) (3 * strata::kernels::NG_HC_DIM + N + 4) * 4 + (uint64_t) N * 2 + 4096) >= 64; + const int32_t prev0[2] = {prev[0], prev[1]}; + auto ple_gather = [&m, &ss, tokens, n, prev0](int64_t c0, int buf, std::string& e) -> bool { + const int64_t T = std::min(m.T, n - c0); + auto at = [&](int64_t i) { return i < 2 ? prev0[i] : (int32_t) tokens[i - 2]; }; + int32_t pv[2] = {at(c0), at(c0 + 1)}; + for (int64_t t = 0; t < T; ++t) { + const int32_t tok = (int32_t) tokens[c0 + t]; + strata::kernels::ngram_rows(&tok, pv, 1, ss.ple.consts, + m.ple_rows[buf].data() + t * strata::kernels::PLE_N_HEADS); + pv[0] = pv[1]; + pv[1] = tok; + } + return ss.ple.table->gather_batch(m.ple_rows[buf].data(), (size_t) T, m.ple_emb_host[buf], e); + }; + std::string ple_next_err; + std::future ple_next; + int ple_buf = 0; + auto ple_land = [&](int64_t c0, int64_t T) -> bool { + const auto tp = Clock::now(); + if (!ple_next.get()) { err = ple_next_err; return false; } + // #1996: the pageable fallback (no RAM to pin) goes up through a pinned slot; the pinned buffers as before + if ((m.ple_pageable[ple_buf].empty() + ? cudaMemcpyAsync(m.ple_emb, m.ple_emb_host[ple_buf], (size_t) T * N * 4, cudaMemcpyHostToDevice, cs) + : m.up_ring.h2d(m.ple_emb, m.ple_emb_host[ple_buf], (size_t) T * N * 4, cs)) != cudaSuccess || + cudaEventRecord(m.ple_copied[ple_buf], cs) != cudaSuccess) { + err = std::string("prefill: the PLE rows' upload failed: ") + cudaGetErrorString(cudaGetLastError()); + return false; + } + if (c0 + m.T < n) { + if (cudaEventSynchronize(m.ple_copied[ple_buf ^ 1]) != cudaSuccess) { + err = std::string("prefill: the PLE rows' upload failed: ") + cudaGetErrorString(cudaGetLastError()); + return false; + } + ple_next = std::async(std::launch::async, [&ple_gather, &ple_next_err, c1 = c0 + m.T, b = ple_buf ^ 1] { + strata::aux_cpus::pin_current_thread(); + return ple_gather(c1, b, ple_next_err); + }); + } + ple_buf ^= 1; + stats_.ms_ple += ms_since(tp); + return true; + }; + // the chunk buffers this path points into the super-chunk (restored on every return) + struct Restore { + Impl& m; + float *R, *mixed, *inj; + uint16_t* mixed_h; + ~Restore() { m.R = R; m.mixed = mixed; m.inj = inj; m.mixed_h = mixed_h; } + } restore{m, m.R, m.mixed, m.inj, m.mixed_h}; + float* const R_chunk = m.R; + const strata::kernels::cpu::ExpertLayout& lay = strata::kernels::cpu::expert_layout(); + double ms_attn = 0, ms_experts = 0, ms_host_tables = 0, ms_wait_a = 0; + std::vector pf_job((size_t) NE, -1); // the layer's prefetch: expert -> stager job (-1: not streamed) + // every streamed expert of layer l, in id order, to the stager - started with the layer's attention halves, so + // the NVMe / page-cache reads overlap them (at a super-chunk's size nearly every expert is routed) + static const bool pf_on = [] { const char* e = std::getenv("STRATA_LM_PREFETCH"); return e == nullptr || e[0] != '0'; }(); + auto lm_prefetch = [&](int64_t l) -> bool { + std::fill(pf_job.begin(), pf_job.end(), -1); + if (m.src == nullptr || !pf_on) return true; + std::vector js; + const bool pdirect = false; // (STRATA_PREFILL_DIRECT, #1670's other commit, is not ported) + for (int32_t e = 0; e < NE; ++e) { + if ((m.host_res && m.cache && m.host_res[(size_t) l * NE + e] >= 0) || m.src->pinned(l, e)) continue; + pf_job[(size_t) e] = (int) js.size(); + if (pdirect || m.src->transient(l, e)) { + js.push_back({nullptr, (size_t) lay.blob_bytes(l), m.src, (int32_t) l, e}); + continue; + } + const uint8_t* b = m.src->blob_stable(l, e); + if (!b) { err = "prefill (layer-major): expert source has no blob"; return false; } + js.push_back({b, (size_t) lay.blob_bytes(l)}); + } + m.stager->start(std::move(js)); + return true; + }; + struct StagerStop { + Stager* st; + ~StagerStop() { if (st) st->finish(); } // an early return in a layer's attention halves + } stager_stop{m.stager.get()}; + int64_t lm_streamed = 0, lm_resident = 0, lm_groups = 0, lm_supers = 0, lm_groups_f16 = 0; + uint64_t lm_bytes = 0; + const int64_t G = m.T * K; // the MoE buffers' rows (Xq, GU, H, Hq, Dm): a group's most + + // ---- one layer's experts over the super-chunk's Sn tokens + auto lm_experts = [&](int64_t l, int64_t Sn) -> bool { + const auto th0 = Clock::now(); + const int64_t rows = Sn * K; + int32_t* idsw = lb.idsw_host; + cudaMemcpyAsync(idsw, lb.ids, (size_t) rows * 4, cudaMemcpyDeviceToHost, cs); + cudaMemcpyAsync(idsw + rows, lb.w, (size_t) rows * 4, cudaMemcpyDeviceToHost, cs); + core::progress_at("reading the prompt (layer-major): waiting for the GPU (attention, router) at layer", l, pos0); + if (cudaStreamSynchronize(cs) != cudaSuccess) { + err = std::string("prefill (layer-major): ") + cudaGetErrorString(cudaGetLastError()); + return false; + } + core::progress_at("reading the prompt (layer-major): the experts of layer", l, pos0); + pt.fold(); + ms_wait_a += ms_since(th0); + const auto th1 = Clock::now(); + const int32_t* ids_h = idsw; + const float* w_h = (const float*) (idsw + rows); + std::vector cnt((size_t) NE, 0), off((size_t) NE + 1, 0); + for (int64_t i = 0; i < rows; ++i) { + const int32_t e = ids_h[(size_t) i]; + if (e < 0 || e >= NE) { err = "prefill (layer-major): routed id out of range"; return false; } + ++cnt[(size_t) e]; + } + for (int32_t e = 0; e < NE; ++e) off[(size_t) e + 1] = off[(size_t) e] + cnt[(size_t) e]; + // ---- the routed experts in id order: resident ones from the cache, the others staged once (run_impl's walk) + // (started before the tables are built: the stager's threads read while the host builds them) + std::vector uniq; + for (int32_t e = 0; e < NE; ++e) + if (cnt[(size_t) e] > 0) uniq.push_back(e); + auto resident = [&](int32_t e) { return m.host_res && m.cache && m.host_res[(size_t) l * NE + e] >= 0; }; + // the stager was started with every streamed expert of the layer when its attention halves began (the + // prefetch); an expert the routing skipped is never waited for (a later job's issue covers its buffer) + std::vector job_of(uniq.size(), -1), stage_of(uniq.size(), -1); + for (size_t u = 0; u < uniq.size(); ++u) job_of[u] = pf_job[(size_t) uniq[u]]; + for (int32_t e = 0; e < NE; ++e) // prefetched but not routed: not read, if not started yet + if (cnt[(size_t) e] == 0 && pf_job[(size_t) e] >= 0) m.stager->skip(pf_job[(size_t) e]); + if (!pf_on && m.src != nullptr) { // STRATA_LM_PREFETCH=0: the routed experts only, started now + std::vector js; + const bool pdirect = false; // (STRATA_PREFILL_DIRECT, #1670's other commit, is not ported) + for (size_t u = 0; u < uniq.size(); ++u) { + const int32_t e = uniq[u]; + if (resident(e) || m.src->pinned(l, e)) continue; + job_of[u] = (int) js.size(); + if (pdirect || m.src->transient(l, e)) { + js.push_back({nullptr, (size_t) lay.blob_bytes(l), m.src, (int32_t) l, e}); + continue; + } + const uint8_t* b = m.src->blob_stable(l, e); + if (!b) { err = "prefill (layer-major): expert source has no blob"; return false; } + js.push_back({b, (size_t) lay.blob_bytes(l)}); + } + m.stager->start(std::move(js)); + } + struct StagerDone { + Stager* st; + ~StagerDone() { if (st) st->finish(); } + } stager_done{m.stager.get()}; + // the tables: rows sorted by expert (token order within one), then per group its bounds and per-token CSR + int32_t* th = lb.tab_host; + int32_t* row_tok = th; + std::vector row_w((size_t) rows); + { + std::vector fill(off.begin(), off.end() - 1); + for (int64_t i = 0; i < rows; ++i) { + const int32_t e = ids_h[(size_t) i]; + const int32_t p = fill[(size_t) e]++; + row_tok[p] = (int32_t) (i / K); + row_w[(size_t) p] = w_h[(size_t) i]; + } + } + struct Entry { int32_t e, r0, nr; }; + std::vector ents; + for (int32_t e = 0; e < NE; ++e) + for (int32_t r = off[(size_t) e]; r < off[(size_t) e + 1]; r += (int32_t) G) + ents.push_back({e, r, (int32_t) std::min(G, off[(size_t) e + 1] - r)}); + struct Grp { size_t j0, j1; int32_t r0, nr, maxr; int64_t b_at, u_at, uo_at, nu; }; + std::vector grps; + for (size_t j = 0; j < ents.size();) { + Grp gp{}; + gp.j0 = j; + gp.r0 = ents[j].r0; + int32_t nr = 0, maxr = 0; + while (j < ents.size() && j - gp.j0 < (size_t) MMQ_GROUP && nr + ents[j].nr <= G) { + nr += ents[j].nr; + maxr = std::max(maxr, ents[j].nr); + ++j; + } + gp.j1 = j; + gp.nr = nr; + gp.maxr = maxr; + grps.push_back(gp); + } + const int64_t E = (int64_t) ents.size(); + if (E > lm_entries_max(rows, G)) { err = "prefill (layer-major): more expert entries than the tables hold"; return false; } + int64_t at = rows; + int32_t* bounds = th + at; + int64_t b_used = 0; + at += 2 * lm_entries_max(rows, G); + int32_t* u_tok = th + at; + at += rows; + int32_t* u_off = th + at; + at += rows + lm_entries_max(rows, G); + int32_t* u_rows = th + at; + at += rows; + float* u_w = (float*) (th + at); + at += rows; + if (at > (int64_t) lb.tab_n || at > (int64_t) lb.tab_host_n) { err = "prefill (layer-major): the tables overflow"; return false; } + int64_t u_used = 0, uo_used = 0; + // per group, the tokens in order of first appearance and each token's rows in row order (counting, O(rows)); + // `slot` maps a token to its place in the group (-1: not in it), reset through the group's own token list + std::vector slot((size_t) Sn, -1), fillp; + for (Grp& gp : grps) { + gp.b_at = b_used; + for (size_t j = gp.j0; j < gp.j1; ++j) bounds[b_used++] = ents[j].r0 - gp.r0; + bounds[b_used++] = gp.nr; + gp.u_at = u_used; + gp.uo_at = uo_used; + int32_t* ut = u_tok + u_used; + int32_t* uo = u_off + uo_used; + int64_t nu = 0; + for (int32_t r = 0; r < gp.nr; ++r) { + const int32_t t = row_tok[gp.r0 + r]; + if (slot[(size_t) t] < 0) { slot[(size_t) t] = (int32_t) nu; ut[nu] = t; uo[nu] = 0; ++nu; } + ++uo[slot[(size_t) t]]; + } + fillp.assign((size_t) nu, 0); + for (int64_t i = 0, acc_n = 0; i < nu; ++i) { const int32_t c = uo[i]; uo[i] = (int32_t) acc_n; fillp[(size_t) i] = (int32_t) acc_n; acc_n += c; } + uo[nu] = gp.nr; + for (int32_t r = 0; r < gp.nr; ++r) { + const int32_t p = fillp[(size_t) slot[(size_t) row_tok[gp.r0 + r]]]++; + u_rows[gp.r0 + p] = r; + u_w[gp.r0 + p] = row_w[(size_t) (gp.r0 + r)]; + } + for (int64_t i = 0; i < nu; ++i) slot[(size_t) ut[i]] = -1; + gp.nu = nu; + u_used += nu; + uo_used += nu + 1; + } + // (u_rows / u_w are indexed by the group's own row range [r0, r0 + nr) of the layer) + cudaMemcpyAsync(lb.tab, th, (size_t) at * 4, cudaMemcpyHostToDevice, cs); + const int32_t* d_row_tok = lb.tab; + const int32_t* d_bounds = lb.tab + rows; + const int32_t* d_u_tok = lb.tab + rows + 2 * lm_entries_max(rows, G); + const int32_t* d_u_off = d_u_tok + rows; + const int32_t* d_u_rows = d_u_off + rows + lm_entries_max(rows, G); + const float* d_u_w = (const float*) (d_u_rows + rows); + ms_host_tables += ms_since(th1); + // (the entries' last index per expert, for releasing a staged blob's ring slot) + std::vector last_ent; + for (size_t j = 0; j < ents.size(); ++j) { + if (j == 0 || ents[j - 1].e != ents[j].e) last_ent.push_back(j); + else last_ent.back() = j; + } + int stage_next = 0; + auto stage_one = [&](size_t u) -> bool { + const int32_t e = uniq[u]; + if (resident(e)) return true; + if (m.src == nullptr) { err = "prefill (layer-major): an expert is neither resident nor in a source"; return false; } + const int sl = stage_next; + stage_next = (stage_next + 1) % STAGE; + const auto tq = Clock::now(); + const bool pinned = m.src->pinned(l, e); + const uint8_t* b = pinned ? m.src->blob_stable(l, e) : nullptr; + if (pinned && !b) { err = "prefill (layer-major): expert source has no blob"; return false; } + if (m.stage_live[sl]) cudaStreamWaitEvent(m.copy, m.used[m.used_of[sl]], 0); + if (pinned) { + cudaMemcpyAsync(m.stage_dev[sl], b, (size_t) lay.blob_bytes(l), cudaMemcpyHostToDevice, m.copy); + ++stats_.experts_dma; + } else { + m.stager->pass(job_of[u]); // the prefetched jobs below it the routing skipped get no DMA + const uint8_t* hb = m.stager->wait(job_of[u]); + cudaMemcpyAsync(m.stage_dev[sl], hb, (size_t) lay.blob_bytes(l), cudaMemcpyHostToDevice, m.copy); + m.stager->issued_one(job_of[u], m.copy); + } + cudaEventRecord(m.copied[sl], m.copy); + m.stage_live[sl] = true; + stage_of[u] = sl; + stats_.ms_experts_host += ms_since(tq); + ++stats_.experts_streamed; + ++lm_streamed; + lm_bytes += (uint64_t) lay.blob_bytes(l); + return true; + }; + const int mmq_gt = lay.native ? lay.fmt[(size_t) l].gu_type : 42; + const int mmq_dt = lay.native ? lay.fmt[(size_t) l].d_type : 42; + // a layer MMQ does not take runs run_impl's FP16 path (port, 2026-10-24): each expert dequantized to FP16 once + // per super-chunk, then per entry its rows' FP16 inputs (lb.mixed_h, the bits run_impl's gather reads) through + // two FP16 GEMMs into the group's Dm rows; the group's rows are added per token as on the MMQ path + const bool fp16_l = !mmq_plan().layer[(size_t) l]; + const size_t mmq_gub = fp16_l ? 0 : mmq::matrix_bytes(mmq_gt, 1280, N), mmq_db = fp16_l ? 0 : mmq::matrix_bytes(mmq_dt, N, 640); + size_t staged = 0, u = 0; + const uint8_t* blob_dev = nullptr; + size_t gi = 0; + int dq = -1; // FP16 path: the dequant slot of the current expert + for (size_t j = 0; j < ents.size(); ++j) { + const int32_t e = ents[j].e; + if (j == 0 || ents[j - 1].e != e) { // the next expert: staged ahead, waited for, its blob located + if (j > 0) ++u; + while (staged < uniq.size() && staged <= u + (size_t) STAGE - 1) { + if (!stage_one(staged)) return false; + ++staged; + } + if (stage_of[u] < 0) { + blob_dev = m.cache->device_slot(m.host_res[(size_t) l * NE + e]); + ++stats_.experts_resident; + ++lm_resident; + } else { + pt.mark(kPfWaitCopy, cs); + cudaStreamWaitEvent(cs, m.copied[stage_of[u]], 0); + blob_dev = m.stage_dev[stage_of[u]]; + } + } + const Grp& gp = grps[gi]; + const size_t q = j - gp.j0; + pt.mark(kPfDequant, cs); + if (fp16_l) { + if (j == 0 || ents[j - 1].e != e) { // the expert's FP16 matrices, once (run_impl's dequant) + dq = (dq + 1) % DQ; + if (lay.native) { + const auto& f = lay.fmt[(size_t) l]; + strata::kernels::iq_dequant_gu_f16(f.gu_type, blob_dev, blob_dev + f.up_off, f.n_ff, f.n_embd, + m.dq_gu[dq], cs, nullptr); + strata::kernels::iq_dequant_f16(f.d_type, blob_dev + f.down_off, f.n_embd * f.n_ff, m.dq_d[dq], cs, + nullptr); + } else { + blob_dequant_f16(blob_dev, m.dq_gu[dq], m.dq_d[dq], cs); + } + if (stage_of[u] >= 0) { // the blob is read by the dequant: its ring slot is free + cudaEventRecord(m.used[stage_of[u]], cs); + m.used_of[stage_of[u]] = stage_of[u]; + } + } + // this entry's rows (group-relative offset o): FP16 inputs, gate/up, SwiGLU, down + const int64_t o = ents[j].r0 - gp.r0, nr = ents[j].nr; + gather_rows16(lb.mixed_h, d_row_tok + ents[j].r0, m.Xs + o * N, nr, N, cs); + pt.mark(kPfGemmGU, cs); + m.gemm.f16(m.Xs + o * N, m.dq_gu[dq], m.GU + o * 1280, nr, 1280, N); + swiglu_interleaved(m.GU + o * 1280, m.Hh + o * 640, nr, cs); + pt.mark(kPfGemmD, cs); + m.gemm.f16(m.Hh + o * 640, m.dq_d[dq], m.Dm + o * N, nr, N, 640); + if (j + 1 < gp.j1) continue; + pt.mark(kPfCombine, cs); + moe_scatter_add(m.Dm, d_u_tok + gp.u_at, d_u_off + gp.uo_at, d_u_rows + gp.r0, + d_u_w + gp.r0, gp.nu, lb.acc, cs); + ++lm_groups; + ++lm_groups_f16; + ++gi; + core::progress_beat(); + continue; + } + if (lay.native) { + const auto& f = lay.fmt[(size_t) l]; + mmq::gather_native(blob_dev, blob_dev + f.up_off, mmq_gub / 2, blob_dev + f.down_off, mmq_db, + m.grp_gu + q * mmq_gub, m.grp_d + q * mmq_db, cs); + } else { + mmq::gather_strata_q2(blob_dev, m.grp_gu + q * mmq_gub, m.grp_d + q * mmq_db, cs); + } + if (j == last_ent[u] && stage_of[u] >= 0) { // the blob is read: its ring slot is free + cudaEventRecord(m.used[stage_of[u]], cs); + m.used_of[stage_of[u]] = stage_of[u]; + } + if (j + 1 < gp.j1) continue; + // ---- the group's products over all of its rows of the super-chunk, then the rows added per token + const int ngx = (int) (gp.j1 - gp.j0); + pt.mark(kPfGemmGU, cs); + cudaMemsetAsync(m.grp_gu + (size_t) ngx * mmq_gub, 0, MMQ_TAIL, cs); + cudaMemsetAsync(m.grp_d + (size_t) ngx * mmq_db, 0, MMQ_TAIL, cs); + mmq::quantize(lb.mixed, d_row_tok + gp.r0, m.Xq, mmq_gt, N, N, gp.nr, cs); + mmq::Product gu; + gu.w = m.grp_gu; gu.type = mmq_gt; gu.w_rows = 1280; gu.w_cols = N; gu.expert_bytes = mmq_gub; + gu.n = ngx; gu.xq = m.Xq; gu.bounds = d_bounds + gp.b_at; gu.ids = m.ids_identity; + gu.total_rows = gp.nr; gu.max_rows = gp.maxr; gu.dst = m.GU; gu.ld_dst = 1280; + m.mmq_ctx->run(gu, cs); + mmq::swiglu(m.GU, m.H, gp.nr, 640, !lay.native, cs); + pt.mark(kPfGemmD, cs); + mmq::quantize(m.H, nullptr, m.Hq, mmq_dt, 640, 640, gp.nr, cs); + mmq::Product dn; + dn.w = m.grp_d; dn.type = mmq_dt; dn.w_rows = N; dn.w_cols = 640; dn.expert_bytes = mmq_db; + dn.n = ngx; dn.xq = m.Hq; dn.bounds = d_bounds + gp.b_at; dn.ids = m.ids_identity; + dn.total_rows = gp.nr; dn.max_rows = gp.maxr; dn.dst = m.Dm; dn.ld_dst = N; + m.mmq_ctx->run(dn, cs); + pt.mark(kPfCombine, cs); + moe_scatter_add(m.Dm, d_u_tok + gp.u_at, d_u_off + gp.uo_at, d_u_rows + gp.r0, + d_u_w + gp.r0, gp.nu, lb.acc, cs); + ++lm_groups; + ++gi; + core::progress_beat(); + } + ms_experts += ms_since(th0); + return true; + }; + + for (int64_t sc0 = 0; sc0 < n; sc0 += S) { + const int64_t Sn = std::min(S, n - sc0); + const int64_t nc = (Sn + m.T - 1) / m.T; + ++lm_supers; + lm_mid_ = true; + for (int64_t l = LB; l <= LE; ++l) { + const auto ta = Clock::now(); + if (l < LE && !lm_prefetch(l)) return false; + // host mode: sub-chunk c's rows go up on the copy stream into buffer c % 2 (issued one sub-chunk ahead) + auto r_buf = [&](int64_t c) { return (c & 1) ? lb.R2 : R_chunk; }; + auto issue_up = [&](int64_t c) { + const int64_t cc0 = sc0 + c * m.T, TT = std::min(m.T, n - cc0); + cudaMemcpyAsync(r_buf(c), lb.R_host + (size_t) (c * m.T) * D, (size_t) TT * D * 4, cudaMemcpyHostToDevice, + m.copy); + cudaEventRecord(lb.ev_in[c & 1], m.copy); + }; + if (!lb.r_dev && l > 0) issue_up(0); + int64_t qsa_index = 0, gdn_index = 0; + for (int64_t i = 0; i < std::min(l, L); ++i) (core::is_qsa_layer(g, i) ? qsa_index : gdn_index) += 1; + const int64_t qsa_index0 = qsa_index, gdn_index0 = gdn_index; + for (int64_t c = 0; c < nc; ++c) { + if (should_stop && should_stop()) { err = "cancelled"; return false; } + core::progress_beat(); + core::progress_at("reading the prompt (layer-major): layer", l, pos0 + sc0 + c * m.T); + qsa_index = qsa_index0; + gdn_index = gdn_index0; + const int64_t c0 = sc0 + c * m.T; // in this run's tokens + const int64_t T = std::min(m.T, n - c0), p0 = pos0 + c0; + const int64_t t0 = c * m.T; // in the super-chunk + m.R = lb.r_dev ? lb.R + t0 * D : r_buf(c); + if (!lb.r_dev) { + if (l > 0) { + if (c + 1 < nc) issue_up(c + 1); + cudaStreamWaitEvent(cs, lb.ev_in[c & 1], 0); + } else { + cudaStreamWaitEvent(cs, lb.ev_out[c & 1], 0); // its last download (sub-chunk c - 2) is done + } + } + m.mixed = lb.mixed + t0 * N; + m.mixed_h = lb.mixed_h + t0 * N; // the FFN half's FP16 rows stay for the layer's FP16 expert pass + m.inj = lb.inj + t0 * HC; + float* const acc = lb.acc + t0 * N; + if (l == 0) { + // ---- embeddings, broadcast to the four streams (run_impl's C-4 batched gather, else per token) + pt.mark(kPfStart, cs); + bool batched = m.tok_dev != nullptr; + const core::NativeEmbed* nemb = core::native_embed(); + const core::WeightRef* wemb = nemb ? nullptr : m.wt->find("token_embd.weight"); + if (batched && nemb == nullptr && + (wemb == nullptr || wemb->codebook_iq4nl || wemb->ne0 != g.n_embd || wemb->group_elems <= 0 || + (wemb->code_bits != 2 && wemb->code_bits != 4 && wemb->code_bits != 8))) + batched = false; + for (int64_t t = 0; batched && t < T; ++t) { + const int64_t tok = tokens[c0 + t]; + if (tok < 0 || (wemb && tok >= wemb->ne1)) batched = false; + else m.tok_host[(size_t) t] = (int32_t) tok; + } + if (batched) { + if (!m.tok_up.upload(m.tok_dev, m.tok_host.data(), (size_t) T, cs)) { + err = "prefill: the token id upload failed"; + return false; + } + if (nemb) { + nemb->gather_dev(m.tok_dev, T, m.emb, cs); + } else { + const auto* codes = (const uint8_t*) wemb->data; + const auto* scales = (const float*) (codes + wemb->codes_bytes); + const auto* offsets = wemb->has_offset ? (const float*) (codes + wemb->codes_bytes + wemb->scales_bytes) + : nullptr; + strata::kernels::embedding_gather_dev(codes, scales, offsets, m.tok_dev, (int) T, wemb->ne0, + wemb->code_bits, wemb->code_bias, wemb->group_elems, + (uint64_t) (wemb->ne0 / (8 / wemb->code_bits)), + (uint64_t) (wemb->ne0 / wemb->group_elems), m.emb, cs); + } + } + for (int64_t t = 0; !batched && t < T; ++t) + if (!core::embed_row(*m.wt, g, tokens[c0 + t], m.emb + t * N, cs, err)) return false; + gr_broadcast(m.emb, m.R, T, cs); + if (ple_on && !ple_next.valid()) + ple_next = std::async(std::launch::async, [&ple_gather, &ple_next_err, c0, b = ple_buf] { + strata::aux_cpus::pin_current_thread(); + return ple_gather(c0, b, ple_next_err); + }); + for (int64_t t = 0; t < T; ++t) { prev[0] = prev[1]; prev[1] = (int32_t) tokens[c0 + t]; } + } else if (l > LB) { // (a later stage's first layer: the stage before wrote layer LB - 1) + // the previous layer's experts: the hyper-connection write of its FFN half + gr_write(m.R, acc, m.inj, HC, T, cs); + if (strata::kernels::cvec().covers(l - 1)) + strata::kernels::cvec_apply(m.R, l - 1, T, D, nullptr, 0, nullptr, 0, false, cs); + } + if (l == LE && next_ != nullptr) { + // ---- a layer split: this sub-chunk's rows into the next stage's R_host (on the copy stream, as + // the per-layer download) once the next stage has finished the previous super-chunk + stats_.tokens += T; + ++stats_.chunks; + if (c == 0 && next_run_.valid() && !next_run_.get()) { err = next_err_; return false; } + cudaEventRecord(lb.ev_done[c & 1], cs); + cudaStreamWaitEvent(m.copy, lb.ev_done[c & 1], 0); + if (cudaMemcpyAsync(next_->impl_->lm.R_host + (size_t) t0 * D, m.R, (size_t) T * D * 4, + cudaMemcpyDeviceToHost, m.copy) != cudaSuccess) { + err = "prefill (layer-major): the layer split's hand-off failed"; + return false; + } + cudaEventRecord(lb.ev_out[c & 1], m.copy); + continue; + } + if (l == LE) { + // ---- the final rows of this sub-chunk: reported in order, as run_impl's chunks are + stats_.tokens += T; + ++stats_.chunks; + if (const char* dump = std::getenv("STRATA_PREFILL_DUMP_R")) { // debug: run_impl's dump format + cudaStreamSynchronize(cs); + if (std::FILE* f = std::fopen(dump, c0 == 0 ? "wb" : "ab")) { + std::vector row((size_t) D); + for (int64_t t = (64 - p0 % 64) % 64; t < T; t += 64) { + cudaMemcpy(row.data(), m.R + t * D, (size_t) D * 4, cudaMemcpyDeviceToHost); + const int64_t pos = p0 + t; + std::fwrite(&pos, sizeof pos, 1, f); + std::fwrite(row.data(), 4, row.size(), f); + } + std::fclose(f); + } + } + if (on_chunk || on_stage_chunk) { + if (cudaStreamSynchronize(cs) != cudaSuccess) { + err = std::string("prefill: ") + cudaGetErrorString(cudaGetLastError()); + return false; + } + lm_mid_ = c + 1 < nc; + if (on_stage_chunk && !lm_mid_ && !on_stage_chunk(p0 + T, err)) return false; + if (on_chunk && !on_chunk(m.R, T, p0, err)) return false; + } + continue; + } + const core::LayerView v(*m.wt, l); + if (l == 1 && ple_on && !ple_land(c0, T)) return false; + // ---- the PLE block at layer 1 (run_impl's, batched or token by token) + if (l == 1 && ple_on && ple_batch) { + pt.mark(kPfPle, cs); + const auto tp = Clock::now(); + const strata::kernels::PleWeights& pw = ss.ple.w; + constexpr int64_t HD = strata::kernels::NG_HC_DIM; + const uint64_t per_token = (uint64_t) (3 * HD + N + 4) * 4 + (uint64_t) N * (bf16x2(m.f16_io) ? 4 : 2) + 4096; + const int64_t SB = std::min(T, (int64_t) (m.region_bytes / per_token)); + for (int64_t s0 = 0; s0 < T; s0 += SB) { + const int64_t nb = std::min(SB, T - s0); + uint8_t* q = m.region; + auto carve_f = [&](size_t nn) { float* p = (float*) q; q += (nn * 4 + 255) & ~(size_t) 255; return p; }; + float* key = carve_f((size_t) nb * HD); + float* qn = carve_f((size_t) nb * HD); + float* gated = carve_f((size_t) nb * HD); + float* val = carve_f((size_t) nb * N); + float* gate = carve_f((size_t) nb * 4); + uint16_t* e16 = (uint16_t*) carve_f((size_t) nb * N / 2); + uint16_t* e16_lo = bf16x2(m.f16_io) ? (uint16_t*) carve_f((size_t) nb * N / 2) : nullptr; + const float* emb = m.ple_emb + s0 * N; + if (pw.key_bf16 != nullptr) { + to_bf16(emb, e16, nb * N, cs, e16_lo); + m.gemm.bf16(e16, pw.key_bf16, key, nb, HD, N); + if (e16_lo) m.gemm.bf16(e16_lo, pw.key_bf16, key, nb, HD, N, 0, 1.0f); + } else { + to_f16(emb, e16, nb * N, cs); + m.gemm.native(e16, pw.key_native_type, pw.key_native_data, key, nb, HD, N); + to_bf16(emb, e16, nb * N, cs, e16_lo); + } + m.gemm.bf16(e16, pw.value_bf16, val, nb, N, N); + if (e16_lo) m.gemm.bf16(e16_lo, pw.value_bf16, val, nb, N, N, 0, 1.0f); + try { + strata::kernels::native_ple_postops_batch(key, m.R + s0 * D, val, ss.ple.hist, pw, qn, gated, + gate, (int) nb, cs); + } catch (const std::exception& e) { err = std::string("prefill PLE: ") + e.what(); return false; } + } + stats_.ms_ple += ms_since(tp); + } else if (l == 1 && ple_on) { + pt.mark(kPfPle, cs); + const auto tp = Clock::now(); + for (int64_t t = 0; t < T; ++t) { + strata::kernels::PleOut po; + po.normalized = m.ple_norm; + po.result = m.R + t * D; + try { + strata::kernels::ple_block(m.ple_emb + t * N, m.R + t * D, ss.ple.hist, ss.ple.w, po, + ss.ple.scratch, cs); + } catch (const std::exception& e) { err = std::string("prefill PLE: ") + e.what(); return false; } + strata::kernels::ple_history_advance(ss.ple.hist, m.ple_norm, cs); + } + stats_.ms_ple += ms_since(tp); + } + if (core::is_qsa_layer(g, l)) { // the QSA step records of this sub-chunk's positions + for (int64_t t = 0; t < T; ++t) + strata::kernels::qsa_step_fill(m.steps_host.data() + t * strata::kernels::kStepCount, p0 + t, s); + // the host table is rewritten by the next QSA sub-chunk: its upload has read it by then (#1996: from a + // pinned slot) + if (m.up_ring.h2d(m.steps_dev, m.steps_host.data(), (size_t) T * strata::kernels::kStepCount * 4, cs) != + cudaSuccess) { + err = "prefill: the QSA step records' upload failed"; + return false; + } + } + bool normed = false; + for (int half = 0; half < 2; ++half) { + // ---- the hyper-connection read of this half (run_impl's) + const char* pre = half == 0 ? "hc_attn_" : "hc_ffn_"; + const std::string sn = std::string(pre) + "norm.weight", sd = std::string(pre) + "down.weight", + su = std::string(pre) + "up.weight", si = std::string(pre) + "inject.weight"; + const core::WeightRef *wn = need(v, sn.c_str(), err), *wd = need(v, sd.c_str(), err), + *wu = need(v, su.c_str(), err), *wi = need(v, si.c_str(), err); + if (!wn || !wd || !wu || !wi) return false; + // STRATA_HC_Q8 without a bf16 copy (0.2.2's run_impl): this path's GEMMs read bf16, so the matrices + // are dequantized per half + core::WeightRef wd_q8, wu_q8; + if (!wd->data && wd->hc_q8 && !wu->data && wu->hc_q8) { + wd_q8 = *wd; wu_q8 = *wu; + wd_q8.data = core::NativeDense::hc_bf16_view(*wd, cs, 0, 0); + wu_q8.data = core::NativeDense::hc_bf16_view(*wu, cs, 1, 0); + if (!wd_q8.data || !wu_q8.data) { err = "prefill: the Q8_0 hyper-connection projections could not be dequantized"; return false; } + wd = &wd_q8; wu = &wu_q8; + } + pt.mark(kPfHc, cs); + if (gr_unfused()) gr_norm(m.R, (const float*) wn->data, EPS, m.xn, m.xn16, T, cs, m.xn16_lo); + else if (!normed) gr_norm_rs(m.R, (const float*) wn->data, EPS, m.grs, m.xn16, T, cs, m.xn16_lo); + normed = false; + if (!bf16_proj(m.gemm, wd, m.xn16, m.lo, T, sd, err, 0, m.xn16_lo)) return false; + gr_silu(m.lo, m.lo16, T, cs, m.lo16_lo); + if (!bf16_proj(m.gemm, wu, m.lo16, m.gated, T, su, err, 0, m.lo16_lo)) return false; + if (!bf16_proj(m.gemm, wi, m.xn16, m.inj, T, si, err, 0, m.xn16_lo)) return false; + if (gr_unfused()) gr_mix(m.xn, m.gated, m.mixed, m.mixed_bf, T, cs, m.mixed_h, m.mixed_bf_lo); + else gr_mix_r(m.R, m.grs, (const float*) wn->data, m.gated, m.mixed, m.mixed_bf, T, cs, m.mixed_h, + m.mixed_bf_lo); + if (wd_q8.data) { // the HC GEMMs that read the scratch are enqueued: the next user waits for them + core::NativeDense::hc_bf16_release(cs, 0, 0); + core::NativeDense::hc_bf16_release(cs, 1, 0); + } + if (half == 0 && !core::is_qsa_layer(g, l)) { + // ======================= GDN (run_impl's) ======================= + const core::WeightRef *wqkv = need(v, "attn_qkv.weight", err), *wg = need(v, "attn_gate.weight", err), + *wo = need(v, "ssm_out.weight", err), *wa = need(v, "ssm_alpha.weight", err), + *wb = need(v, "ssm_beta.weight", err), *wc = need(v, "ssm_conv1d.weight", err), + *wnm = need(v, "ssm_norm.weight", err), *wdt = need(v, "ssm_dt.bias", err), + *wsa = need(v, "ssm_a", err); + if (!wqkv || !wg || !wo || !wa || !wb || !wc || !wnm || !wdt || !wsa) return false; + pt.mark(kPfGdn, cs); + float* state = ss.gdn_state + (size_t) (gdn_index - ss.gdn_ord0) * gdn_floats; + float* conv = state + (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size; + if (!native_proj(m.gemm, wqkv, m.mixed_h, m.qkv, T, v.name("attn_qkv.weight"), err)) return false; + if (!native_proj(m.gemm, wg, m.mixed_h, m.z, T, v.name("attn_gate.weight"), err)) return false; + if (!bf16_proj(m.gemm, wa, m.mixed_bf, m.ab, T, v.name("ssm_alpha.weight"), err, 2 * HV, m.mixed_bf_lo)) return false; + if (!bf16_proj(m.gemm, wb, m.mixed_bf, m.ab + HV, T, v.name("ssm_beta.weight"), err, 2 * HV, m.mixed_bf_lo)) return false; + pt.mark(kPfGdnConv, cs); + gdn_gates(m.ab, (const float*) wdt->data, (const float*) wsa->data, m.gate, m.beta, T, cs); + gdn_conv(conv, m.qkv, (const float*) wc->data, m.hbuf, T, EPS, cs); + pt.mark(kPfGdnRec, cs); + gdn_recurrence(state, m.hbuf, m.gate, m.beta, m.z, (const float*) wnm->data, EPS, m.y, m.y_h, T, cs); + pt.mark(kPfGdnOut, cs); + if (!native_proj(m.gemm, wo, m.y_h, m.bo, T, v.name("ssm_out.weight"), err)) return false; + ++gdn_index; + } else if (half == 0) { + // ======================= QSA (run_impl's, without its debug dumps) ======================= + const core::QsaState& st = ss.qsa_states[qsa_index]; + const core::WeightRef *wq = need(v, "attn_q.weight", err), *wk = need(v, "attn_k.weight", err), + *wv = need(v, "attn_v.weight", err), *wo = need(v, "attn_output.weight", err), + *wik = need(v, "indexer.k_proj.weight", err), + *wiq = need(v, "indexer.q_proj.weight", err), + *wqn = need(v, "attn_q_norm.weight", err), *wkn = need(v, "attn_k_norm.weight", err), + *wiqn = need(v, "indexer.q_norm.weight", err), + *wikn = need(v, "indexer.k_norm.weight", err); + if (!wq || !wk || !wv || !wo || !wik || !wiq || !wqn || !wkn || !wiqn || !wikn) return false; + pt.mark(kPfQsa, cs); + if (!native_proj(m.gemm, wk, m.mixed_h, m.Kc, T, v.name("attn_k.weight"), err)) return false; + if (!native_proj(m.gemm, wv, m.mixed_h, m.Vc, T, v.name("attn_v.weight"), err)) return false; + if (!native_proj(m.gemm, wq, m.mixed_h, m.Qf, T, v.name("attn_q.weight"), err)) return false; + if (!bf16_proj(m.gemm, wik, m.mixed_bf, m.idx_raw, T, v.name("indexer.k_proj.weight"), err, 0, m.mixed_bf_lo)) return false; + if (!bf16_proj(m.gemm, wiq, m.mixed_bf, m.q_idx, T, v.name("indexer.q_proj.weight"), err, 0, m.mixed_bf_lo)) return false; + rms_rows(m.Kc, (const float*) wkn->data, T * 2, 256, 256, EPS, cs); + rope(m.Kc, T, 2, 256, 512, p0, strata::kernels::rope_scaling(), cs); + const bool staged = st.kv_mode == 1; + if (staged) { + pt.mark(kPfKvStage, cs); + strata::kernels::kv_stage_from_host(pools_of(m.stage, m.ident_table), st.host, + core::qsa_kv_format(st), + (p0 + s.page_size - 1) / s.page_size, s, cs); + pt.mark(kPfQsa, cs); + } +#if defined(STRATA_USE_HIP) + static const bool kv_host_dma = [] { const char* e = std::getenv("STRATA_KV_HOST_DMA"); return e && e[0] == '1'; }(); +#else + constexpr bool kv_host_dma = false; +#endif + const bool host_by_dma = staged && kv_host_dma; + const strata::kernels::KvHostPools* host_w = host_by_dma ? nullptr : &st.host; + if (st.kv_hybrid) { + // streamed: each half writes its part of the host copy and of the staging pool + strata::kernels::fwht256_inplace_cuda(m.Vc, T * 2, cs); + const strata::kernels::KvHostPools hk = strata::kernels::kv_hybrid_k_half(st.host), + hv = strata::kernels::kv_hybrid_v_half(st.host), + sk = strata::kernels::kv_hybrid_k_half(m.stage), + sv = strata::kernels::kv_hybrid_v_half(m.stage); + const bool mirror = st.host.present(); + kv_append(m.Kc, m.Kc, T, p0, st.page_table, s.page_size, nullptr, nullptr, + st.k_q, st.k_q, st.k_scale, st.k_scale, cs, mirror ? &hk : nullptr, + staged ? &sk : nullptr); + strata::kernels::kv_append_q4(st.v_q4, st.v_q4, st.page_table, p0, T, m.Vc, m.Vc, s, cs, + mirror ? &hv : nullptr, staged ? &sv : nullptr); + } else { + if (st.kv_rot) { + strata::kernels::fwht256_inplace_cuda(m.Kc, T * 2, cs); + strata::kernels::fwht256_inplace_cuda(m.Vc, T * 2, cs); + } + if (st.kv_q4) + strata::kernels::kv_append_q4(st.k_q4, st.v_q4, st.page_table, p0, T, m.Kc, m.Vc, s, cs, + host_w, staged ? &m.stage : nullptr); + else + kv_append(m.Kc, m.Vc, T, p0, st.page_table, s.page_size, st.kv_int8 ? nullptr : st.k_pool, + st.kv_int8 ? nullptr : st.v_pool, st.k_q, st.v_q, st.k_scale, st.v_scale, cs, + host_w, staged ? &m.stage : nullptr); + if (host_by_dma) + strata::kernels::kv_unstage_to_host(pools_of(m.stage, m.ident_table), st.host, + core::qsa_kv_format(st), p0 / s.page_size, + (p0 + T + s.page_size - 1) / s.page_size, s, cs); + } + split_q(m.Qf, m.q, T, cs); + rms_rows(m.q, (const float*) wqn->data, T * 24, 256, 256, EPS, cs); + rope(m.q, T, 24, 256, 6144, p0, strata::kernels::rope_scaling(), cs); + if (st.kv_rot) strata::kernels::fwht256_inplace_cuda(m.q, T * 24, cs); + rms_rows(m.q_idx, (const float*) wiqn->data, T * 4, 128, 128, EPS, cs); + rope(m.q_idx, T, 4, 128, 512, p0, strata::kernels::rope_scaling(), cs); + const strata::kernels::QsaIndexerBuffers ib{st.idx_tail, st.idx_dead, st.idx_pooled, st.idx_block_pos}; + pt.mark(kPfQsaIdx, cs); + try { + static const bool per_token = std::getenv("STRATA_INDEXER_PER_TOKEN") != nullptr; + if (!per_token) { + strata::kernels::native_qsa_indexer_append_batch(m.idx_raw, T, p0, 0, (const float*) wikn->data, + EPS, ib, s, st.max_cells, + strata::kernels::rope_scaling(), cs); + } + for (int64_t t = 0; per_token && t < T; ++t) { + const int32_t* step_t = m.steps_dev + t * strata::kernels::kStepCount; + strata::kernels::native_qsa_indexer_append(m.idx_raw + t * 128, step_t + strata::kernels::kStepPos, 0, + (const float*) wikn->data, EPS, ib, s, st.max_cells, + strata::kernels::rope_scaling(), cs); + } + } catch (const std::exception& e) { err = std::string("prefill indexer: ") + e.what(); return false; } + pt.mark(kPfQsaSel, cs); + for (int64_t q0 = 0; q0 < T; q0 += m.sel_batch) { + const int64_t nb = std::min(m.sel_batch, T - q0); + const int32_t* steps0 = m.steps_dev + q0 * strata::kernels::kStepCount; + const int64_t active = (int64_t) m.steps_host[(size_t) ((q0 + nb - 1) * strata::kernels::kStepCount + + strata::kernels::kStepNBid)] + 1; + static const bool old_sel = std::getenv("STRATA_SELECT_OLD") != nullptr; + if (old_sel || !strata::kernels::qsa_block_scores_tc(st.idx_pooled, st.idx_dead, m.q_idx + q0 * 512, + steps0, nb, m.max_blocks, s, m.sel_scores, + cs, active)) + strata::kernels::qsa_block_scores(st.idx_pooled, st.idx_dead, m.q_idx + q0 * 512, steps0, nb, + m.max_blocks, s, m.sel_scores, cs, active); + strata::kernels::qsa_block_topk(m.sel_scores, steps0, nb, m.max_blocks, m.cap, s, + m.sel_ids + q0 * m.cap, cs, active); + } + const strata::kernels::QsaAttnPools pools = staged ? pools_of(m.stage, m.ident_table) + : core::qsa_attn_pools(st); + pt.mark(kPfQsaAttn, cs); + static const bool old_attn = std::getenv("STRATA_PROMPT_ATTN_OLD") != nullptr; + if (old_attn || !strata::kernels::qsa_prompt_attn_batch(m.q, pools, m.sel_ids, m.steps_dev, m.cap, s, + m.attn, T, cs)) + for (int64_t q0 = 0; q0 < T; q0 += m.attn_batch) { + const int64_t nb = std::min(m.attn_batch, T - q0); + strata::kernels::qsa_decode_attn_batch(m.q + q0 * ZV, pools, m.sel_ids + q0 * m.cap, + m.steps_dev + q0 * strata::kernels::kStepCount, m.cap, + s, m.attn_scratch, m.attn + q0 * ZV, nb, cs); + } + if (st.kv_rot || st.kv_hybrid) strata::kernels::fwht256_inplace_cuda(m.attn, T * 24, cs); + pt.mark(kPfQsa, cs); + gate_attn(m.attn, m.Qf, m.attn_h, T, cs); + if (!native_proj(m.gemm, wo, m.attn_h, m.bo, T, v.name("attn_output.weight"), err)) return false; + ++qsa_index; + } else { + // ======================= the FFN half: router + shared expert now, the experts later ========= + const core::WeightRef *wr = need(v, "ffn_gate_inp.weight", err), + *wgi = need(v, "ffn_gate_inp_shexp.weight", err), + *wsg = need(v, "ffn_gate_shexp.weight", err), + *wsu = need(v, "ffn_up_shexp.weight", err), + *wsd = need(v, "ffn_down_shexp.weight", err); + if (!wr || !wgi || !wsg || !wsu || !wsd) return false; + pt.mark(kPfRouter, cs); + if (!bf16_proj(m.gemm, wr, m.mixed_bf, m.logits, T, v.name("ffn_gate_inp.weight"), err, 0, m.mixed_bf_lo)) return false; + route(m.logits, lb.ids + t0 * K, lb.w + t0 * K, T, m.g->n_expert, cs); + if (!native_proj(m.gemm, wsg, m.mixed_h, m.sgate, T, v.name("ffn_gate_shexp.weight"), err)) return false; + if (!native_proj(m.gemm, wsu, m.mixed_h, m.sup, T, v.name("ffn_up_shexp.weight"), err)) return false; + swiglu_pair(m.sgate, m.sup, m.sh_h, T, cs); + if (!native_proj(m.gemm, wsd, m.sh_h, m.shared, T, v.name("ffn_down_shexp.weight"), err)) return false; + if (wgi->kind != core::WeightKind::Bf16InF32) { err = "prefill: shared gate is not BF16"; return false; } + m.gemm.bf16(m.mixed_bf, (const uint16_t*) wgi->data, m.sg, T, 1, N); + if (m.mixed_bf_lo) m.gemm.bf16(m.mixed_bf_lo, (const uint16_t*) wgi->data, m.sg, T, 1, N, 0, 1.0f); + moe_shared_init(m.shared, m.sg, acc, T, cs); + break; // the FFN half's write waits for the experts (the next layer, or the final pass) + } + // ---- the attention half's write, fused with the FFN half's norm (run_impl's F-2) + const core::WeightRef* wnn = nullptr; + if (!gr_unfused()) { + wnn = need(v, "hc_ffn_norm.weight", err); + if (!wnn) return false; + } + if (wnn) { + gr_write_norm_rs(m.R, m.bo, m.inj, HC, (const float*) wnn->data, EPS, m.grs, m.xn16, T, cs, + m.xn16_lo); + normed = true; + } else { + gr_write(m.R, m.bo, m.inj, HC, T, cs); + } + } + if (!lb.r_dev) { // down on the copy stream once this sub-chunk's work is done + cudaEventRecord(lb.ev_done[c & 1], cs); + cudaStreamWaitEvent(m.copy, lb.ev_done[c & 1], 0); + if (cudaMemcpyAsync(lb.R_host + (size_t) t0 * D, m.R, (size_t) T * D * 4, cudaMemcpyDeviceToHost, + m.copy) != cudaSuccess) { + err = "prefill (layer-major): the residual rows' download failed"; + return false; + } + cudaEventRecord(lb.ev_out[c & 1], m.copy); + } + } + ms_attn += ms_since(ta); + if (l == LE) break; + if (!lm_experts(l, Sn)) return false; + } + if (next_ != nullptr) { // the super-chunk's rows are in the next stage's R_host: it reads them on a thread + if (cudaStreamSynchronize(cs) != cudaSuccess || cudaStreamSynchronize(m.copy) != cudaSuccess) { + err = std::string("prefill (layer-major): the layer split's hand-off: ") + + cudaGetErrorString(cudaGetLastError()); + return false; + } + if (on_stage_chunk && !on_stage_chunk(pos0 + sc0 + Sn, err)) return false; + next_err_.clear(); + next_run_ = std::async(std::launch::async, [this, tokens, sc0, Sn, pos0] { + strata::aux_cpus::pin_current_thread(); + return next_->run_layer_major(tokens + sc0, Sn, pos0 + sc0, next_err_); + }); + } + } + lm_mid_ = false; + ss.ple_prev[0] = prev[0]; + ss.ple_prev[1] = prev[1]; + if (ple_next.valid()) ple_next.wait(); + if (cudaStreamSynchronize(cs) != cudaSuccess) { + err = std::string("prefill: ") + cudaGetErrorString(cudaGetLastError()); + return false; + } + if (const cudaError_t cst = cudaStreamSynchronize(m.copy); cst != cudaSuccess) { + err = std::string("prefill: expert copy stream: ") + cudaGetErrorString(cst); + return false; + } + stats_.ms_total += ms_since(t_start); + std::fprintf(stderr, "strata prefill: layer-major (layers %lld..%lld): %lld tokens in %lld super-chunk(s) of up to %lld (R in %s, %lld MiB VRAM free before), " + "%lld expert blobs streamed (%.1f MB), %lld resident, %lld expert groups (%lld on the FP16 path); %.0f ms (host: " + "enqueueing the attention halves %.0f, the experts %.0f: waiting for the attention halves " + "%.0f, the routing tables %.0f)\n", + (long long) LB, (long long) LE, (long long) n, (long long) lm_supers, (long long) S, + lb.r_dev ? "VRAM" : "pinned RAM", (long long) lb.free_mib, + (long long) lm_streamed, (double) lm_bytes / 1e6, (long long) lm_resident, (long long) lm_groups, + (long long) lm_groups_f16, ms_since(t_start), ms_attn, ms_experts, ms_wait_a, ms_host_tables); + if (pt.on) { + pt.fold(); + double total = 0.0; + for (double x : pt.ms) total += x; + std::string line; + char b[96]; + for (int i = 0; i < kPfCount; ++i) { + if (pt.ms[i] <= 0.0) continue; + std::snprintf(b, sizeof b, " %s %.0f (%.1f%%)", kPfNames[i], pt.ms[i], total > 0 ? 100.0 * pt.ms[i] / total : 0.0); + line += b; + } + std::fprintf(stderr, "strata prefill timing (layer-major): %lld tokens, GPU timeline %.0f ms:%s\n", + (long long) n, total, line.c_str()); + } + return true; +} + bool Prefill::drain_pipeline(std::string& err) { bool ok = true; @@ -5768,6 +7073,25 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e return ok; } #endif + if (lm_env() && hand_in_ == nullptr && impl_) { + std::string why; + if (lm_prepare_chain(n, why) > 0) { + bool ok = run_layer_major(tokens, n, pos0, err); + std::string drain_err; // a layer split: the later stages' last super-chunk + if (!drain_pipeline(drain_err) && ok) { + ok = false; + err = drain_err; + } + for (Prefill* p = this; p != nullptr; p = p->next_) p->lm_mid_ = false; + lm_release_chain(); + return ok; + } + static std::string said; // once per reason (a short prompt is the common one) + if (why != said) { + said = why; + std::fprintf(stderr, "strata prefill: layer-major off for this prompt: %s\n", why.c_str()); + } + } const bool body_ok = run_impl(tokens, n, pos0, err); std::string drain_err; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 054b4de0..5c381ffd 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -10860,12 +10860,15 @@ int main(int argc, char** argv) { strata::core::progress_at("reading the prompt (batched), done up to token", done); strata::core::progress_beat(); std::fflush(stdout); - const bool periodic_checkpoint = o.prompt_cache_every > 0 && done >= pp_next_check; + // layer-major prefill: a sub-chunk before its super-chunk's end has the super-chunk's state - the + // checkpoint is taken at that end instead + const bool periodic_checkpoint = o.prompt_cache_every > 0 && done >= pp_next_check && !sp.layer_major_mid(); // Keep the prefill's existing chunk geometry. One extra near-tail checkpoint // can serve a branch whose shared prefix ends before the final cached state. const bool tail_checkpoint = o.prompt_cache_tail && o.prompt_cache > 0 && o.prompt_cache_every > 0 && !multi_gpu && !pp_tail_saved && pp_total - done > std::max(1, o.short_read) && - pp_total - done <= T; // this chunk's real size (--prefill auto chunks differ from o.prefill_chunk) + pp_total - done <= T && // this chunk's real size (--prefill auto chunks differ from o.prefill_chunk) + !sp.layer_major_mid(); if (periodic_checkpoint || tail_checkpoint) { bool saved = false; if (multi_gpu) { // the stages' parts, saved when each of them read this chunk