diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index ee3a1ae9..39cb544a 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -2620,6 +2620,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri // set_cpu_pool: this layer's CPU experts (rows [T * K - rows_cpu, T * K) of Dm), computed on a thread // that reads on_cpu: declared first, so the future (which waits for the thread) goes first std::vector on_cpu; + std::vector cpu_blob; // the CPU experts' blobs, taken on this thread (the source is not thread-safe) double cpu_ms = 0; // the thread's time (the measured share) std::future cpu_fut; int64_t rows_cpu = 0, n_cpu = 0, n_stream = 0; @@ -2853,7 +2854,15 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri if (m.cpu_ev[0] == nullptr) { cudaEventCreate(&m.cpu_ev[0]); cudaEventCreate(&m.cpu_ev[1]); } cudaEventRecord(m.cpu_ev[0], m.cs); } - cpu_fut = std::async(std::launch::async, [&m, &on_cpu, &cf, &cpu_ms, src_h, l, T, r0c, this]() -> bool { + // The thread touches no ExpertSource (its blob() counts reads without a lock, and its calls + // race with this thread's own blob_stable()/pinned() ones): the pointers are taken here. + cpu_blob.assign((size_t) m.g->n_expert, nullptr); + for (int32_t e = 0; e < m.g->n_expert; ++e) + if (on_cpu[(size_t) e] && (cpu_blob[(size_t) e] = m.src->blob_stable(l, e)) == nullptr) { + err = "prefill: a CPU expert has no blob"; + return false; + } + cpu_fut = std::async(std::launch::async, [&m, &on_cpu, &cpu_blob, &cf, &cpu_ms, src_h, l, T, r0c, this]() -> bool { const auto t0 = std::chrono::steady_clock::now(); constexpr size_t AB = strata::kernels::cpu::kNativeActBytes; // a Q2_0 layer (gate/up on the pool's Q2_0 kernels): ActQ activations, as decode's @@ -2875,8 +2884,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri for (int32_t e = 0; e < m.g->n_expert; ++e) { if (!on_cpu[(size_t) e]) continue; strata::kernels::cpu::ExpertJobMulti j; - j.blob = m.src->blob(l, e); - if (j.blob == nullptr) return false; + j.blob = cpu_blob[(size_t) e]; j.nt = m.cnt[(size_t) e]; for (int r = 0; r < j.nt; ++r) { const int64_t p = (int64_t) m.off[(size_t) e] + r;