diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index 391f6e3..397196f 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -2029,6 +2029,23 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32 const ExpertLayout& lay = expert_layout(); const bool native = lay.native; if (native && d.nact_multi.size() < (size_t) MAXT * kNativeActBytes) d.nact_multi.resize((size_t) MAXT * kNativeActBytes); + // The activation and output row width. A native pack's is the layer's own (NativeFmt n_embd: 2048 for + // Qwen3.6-35B-A3B); the canonical Q2_0 pack's is H. The window publishes its rows at that width, so reading + // token t at t * H or writing its row at i * H addressed the wrong token for every t > 0 and ran past the + // window's buffers (the single-token dispatch above refuses n_embd != H; this one took the width for granted). + if (native && (d.layers < 0 || (size_t) d.layers >= lay.fmt.size())) { + d.failed = true; + d.fail = "the expert pool was asked for a layer the pack does not have"; + d.fail_layer = d.layers; + return; + } + const int64_t W = native ? lay.fmt[(size_t) d.layers].n_embd : (int64_t) H; + if (W <= 0 || W > (int64_t) H) { + d.failed = true; + d.fail = "the expert pool's activation buffers are compiled for at most a 2560-wide activation"; + d.fail_layer = d.layers; + return; + } if (d.job_of.size() != (size_t) d.n_expert) d.job_of.assign((size_t) d.n_expert, (int16_t) -1); if (d.jobs_multi.size() < (size_t) (n_tok * k)) d.jobs_multi.resize((size_t) (MAXT * k)); static const bool ptrace = std::getenv("STRATA_POOL_TRACE") != nullptr; @@ -2171,11 +2188,11 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32 for (int64_t t = 0; t < n_tok; ++t) { if (ep && std::all_of(kind + t * k, kind + (t + 1) * k, [](int32_t v) { return v >= 0; })) continue; if (native && strata::kernels::cpu::q2_native_kernels(lay.fmt[(size_t) d.layers].gu_type)) // a native Q2_0 pack: the Q2_0 kernels' activations - act_quant_any(x_f + (size_t) t * H, H, d.act_multi[(size_t) t]); + act_quant_any(x_f + (size_t) t * W, (int) W, d.act_multi[(size_t) t]); else if (native) - native_quant_act(lay.fmt[(size_t) d.layers], x_f + (size_t) t * H, d.nact_multi.data() + (size_t) t * kNativeActBytes); + native_quant_act(lay.fmt[(size_t) d.layers], x_f + (size_t) t * W, d.nact_multi.data() + (size_t) t * kNativeActBytes); else - act_quant_q8_1(x_f + (size_t) t * H, H, d.act_multi[(size_t) t]); + act_quant_q8_1(x_f + (size_t) t * W, (int) W, d.act_multi[(size_t) t]); } } const auto c2 = std::chrono::steady_clock::now(); @@ -2193,7 +2210,7 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32 for (int64_t j = 0; j < k; ++j) { const int64_t i = t * k + j; const int64_t e = ids[i]; - float* row = out + (size_t) i * H; + float* row = out + (size_t) i * W; if (e < 0 || e >= d.n_expert) { d.failed = true; d.fail = "a routed expert id is out of range"; @@ -2209,7 +2226,7 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32 // dec_batch: copy_rows_from_mapped_kernel already zeroes kind 0/1 rows on the GPU if (!((kind[i] == 2 && d.peer != nullptr && d.peer->launched_direct()) || (gpu_zeroes_hits && (kind[i] == 0 || kind[i] == 1)))) - std::memset(row, 0, (size_t) H * sizeof(float)); + std::memset(row, 0, (size_t) W * sizeof(float)); continue; } ++d.cache_refused; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 46f6d59..5ec6884 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -2216,8 +2216,11 @@ int main(int argc, char** argv) { const int64_t n_vocab_file = n_dvocab; std::error_code ec; const auto dv = std::filesystem::file_size(o.mtp + "/draft_vocab.bin", ec); - if (!ec && dv >= 4) n_dvocab = (int64_t) (dv / 4); - in.drafter_head_bytes = (uint64_t) n_dvocab * head_row + (uint64_t) 8 * (uint64_t) n_vocab_file * 4; + // With a vocabulary subset the drafter gathers its own head (n_dvocab rows); without one it scores + // with the target's head and allocates no copy (mtp.cpp: dhead_ stays null), so only the logits count + const bool subset = !ec && dv >= 4; + if (subset) n_dvocab = (int64_t) (dv / 4); + in.drafter_head_bytes = (subset ? (uint64_t) n_dvocab * head_row : 0) + (uint64_t) 8 * (uint64_t) n_vocab_file * 4; in.drafter_ids_bytes_per_token = (uint64_t) std::max(o.spec, 1) * 4; } in.n_experts = g.n_layers * g.n_expert; @@ -2232,7 +2235,9 @@ int main(int argc, char** argv) { const double kv_elem = o.kv == "q4_0" ? 18.0 / 32.0 : o.kv == "int8" ? 1.0 + 2.0 / 64.0 : o.kv == "k8v4" ? 816.0 / 1024.0 : 2.0; in.kv_bytes_per_token = (uint64_t) (g.n_qsa_layers() * 2 * g.n_head_kv * g.head_dim * kv_elem); // the drafter's one attention layer, the same format, the whole context (no ring without --kv-resident) - if (!o.mtp.empty()) in.drafter_kv_bytes_per_token = (uint64_t) (2 * g.n_head_kv * g.head_dim * kv_elem); + // (under --kv k8v4 the drafter keeps plain INT8 K/V, mtp.cpp: charged as such, not as the hybrid's bytes) + const double drafter_kv_elem = o.kv == "k8v4" ? 1.0 + 2.0 / 64.0 : kv_elem; + if (!o.mtp.empty()) in.drafter_kv_bytes_per_token = (uint64_t) (2 * g.n_head_kv * g.head_dim * drafter_kv_elem); // the session: one RoPE table (max_cells x n_rot/2 x 2 floats), the GDN state, ~36 MiB of scratch in.session_bytes = (uint64_t) o.max_context * (uint64_t) g.n_rot * 4 + (uint64_t) g.n_gdn_layers() * ((uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size + @@ -2267,6 +2272,42 @@ int main(int argc, char** argv) { std::fprintf(stderr, "strata generate: %s\n", plan.refusal.c_str()); return 2; } + if (plan.lowered) { // the plan's context is the one everything below allocates for, not the request's + std::fprintf(stderr, "strata generate: the context is lowered from %lld to %lld tokens by the VRAM plan above\n", + (long long) o.max_context, (long long) plan.context); + o.max_context = plan.context; + } + // The RAM tier. The resident arena pins every expert in RAM; when the host cannot hold that (the plan's + // 12 GiB rung: --ram-cap, a cgroup limit, or simply less RAM than the file), the experts stay mapped from the + // file and the hottest ones by profile rank are copied into RAM (the --resident-experts mode), which is what + // the plan's "expert copies in RAM" line already assumed. Chosen here, before anything keys on the mode; + // an explicit --mmap-experts / --resident-experts is left alone. + if (!o.mmap_experts) { + const auto& lay2 = strata::kernels::cpu::expert_layout(); + strata::core::detail::HostMemory hm; + if (lay2.native && lay2.total > 0 && strata::core::detail::host_available_memory(hm)) { + const uint64_t need = lay2.total + (uint64_t) lay2.max_blob; + if (hm.cgroup_limit < need || hm.available < need) { + const double gib = 1073741824.0; + std::fprintf(stderr, "strata generate: RAM tier: the resident expert arena needs %.2f GiB but the host " + "allows %.2f GiB (%s): the experts stay mapped from the file and the hottest are copied " + "into RAM by profile rank (as --resident-experts)\n", + (double) need / gib, (double) std::min(hm.cgroup_limit, hm.available) / gib, + hm.cgroup_limit < need ? "the memory limit" : "available RAM"); + o.mmap_experts = true; + if (!o.expert_profile.empty()) { + o.resident_cpu_experts = o.resident_pin = o.resident_soft = true; + o.resident_headroom = 4ull << 30; + } + } + } + } + if ((o.peer_device >= 1 || o.remote_expert_opt) && g.n_embd != (int64_t) strata::kernels::cpu::H) { + std::fprintf(stderr, "strata generate: the multi-GPU expert helpers (--peer-device, --remote-expert-opt) are " + "compiled for the canonical %d-wide expert and are not available for this model (n_embd %lld)\n", + (int) strata::kernels::cpu::H, (long long) g.n_embd); + return 2; + } // a native pack decodes in verify windows only (plan v0.3 P6), and this model has no MTP drafter wired yet // (docs/qwen36-port/step4-runner.md: the drafter is `--mtp DIR`, qwen36-step4-drafter): without one the // windows hold one token each and `--spec` is only the verifier's capacity, so a run that asked for none