diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 90dfbe1a..9183ee96 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -797,7 +797,7 @@ struct Prefill::Impl { // (Dm's tail, in Dm's row order), ones for row_sd (the CPU's rows carry s_down already), all pinned; and the // pool's per-token activations and jobs. float *cpu_x = nullptr, *cpu_rows = nullptr, *cpu_ones = nullptr; - size_t cpu_x_n = 0, cpu_rows_n = 0; + size_t cpu_x_n = 0, cpu_rows_n = 0, cpu_ones_n = 0; bool cpu_dead = false; // a pinned buffer of the share could not be had (host memory is short): the GPU does it all std::vector cpu_nact; std::vector cpu_actq; // a Q2_0 layer's activations (the pool's Q2_0 kernels read ActQ) @@ -3361,19 +3361,30 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri const size_t want = (size_t) rows_cpu * N; if (m.cpu_rows_n < want) { cpu_cold = true; - for (float* q : {m.cpu_rows, m.cpu_ones}) - if (q) cudaFreeHost(q); - m.cpu_rows = m.cpu_ones = nullptr; + if (m.cpu_rows) cudaFreeHost(m.cpu_rows); + m.cpu_rows = nullptr; m.cpu_rows_n = 0; const size_t cap_rows = (size_t) rows_cpu * 2; // headroom: few reallocations - if (cudaHostAlloc((void**) &m.cpu_rows, cap_rows * N * sizeof(float), cudaHostAllocDefault) != cudaSuccess || - cudaHostAlloc((void**) &m.cpu_ones, cap_rows * sizeof(float), cudaHostAllocDefault) != cudaSuccess) { + if (cudaHostAlloc((void**) &m.cpu_rows, cap_rows * N * sizeof(float), cudaHostAllocDefault) != cudaSuccess) { err = "prefill: cannot allocate the CPU experts' rows"; return false; } - std::fill(m.cpu_ones, m.cpu_ones + cap_rows, 1.0f); m.cpu_rows_n = cap_rows * N; } + // this fork (NVFP4): row_sd 1 for the CPU's rows, which carry s_down already - sized on its + // own, as the rows buffer is now taken once at its largest above (0.1.42) + if (m.cpu_ones_n < (size_t) rows_cpu) { + if (m.cpu_ones) cudaFreeHost(m.cpu_ones); + m.cpu_ones = nullptr; + m.cpu_ones_n = 0; + const size_t n1 = std::max((size_t) rows_cpu * 2, m.cpu_rows_n / N); + if (cudaHostAlloc((void**) &m.cpu_ones, n1 * sizeof(float), cudaHostAllocDefault) != cudaSuccess) { + err = "prefill: cannot allocate the CPU experts' row scales"; + return false; + } + std::fill(m.cpu_ones, m.cpu_ones + n1, 1.0f); + m.cpu_ones_n = n1; + } const int64_t r0c = T * K - rows_cpu; const strata::kernels::cpu::NativeFmt& cf = lay.fmt[(size_t) l]; if (cpu_share_env() < 0.0) {