// src/prefill/prefill.cpp - see include/strata/prefill/prefill.hpp. #include "strata/kernels/fused_gr.hpp" #include "strata/prefill/prefill.hpp" #include "strata/prefill/copy_events.hpp" #include "strata/prefill/timer_marks.hpp" #include "strata/prefill/mmq_ids_margin.hpp" #include "strata/core/run_manifest.hpp" #include "strata/artifact/gguf_reader.hpp" #include "strata/prefill/pf_fused_override.hpp" #include "strata/prefill/staged_group_rule.hpp" #include "strata/prefill/staged_fo_rule.hpp" #include "strata/prefill/plain_floor_rule.hpp" #include "strata/prefill/share_own_rule.hpp" #include "strata/prefill/fp16_rows_rule.hpp" #include "strata/prefill/own_only_line.hpp" #include "strata/prefill/own_only_switches.hpp" #include "strata/prefill/stream_floor_rule.hpp" #include "strata/prefill/switch_point_rule.hpp" #include "strata/kernels/gfx_arch.hpp" #include "strata/quant/types.hpp" #include "strata/prefill/fewrows.hpp" #include "strata/prefill/gp100_prompt_default.hpp" #if defined(STRATA_HAVE_DFLASH2) #include "strata/kernels/dflash2.hpp" #endif #include "strata/prefill/warm_policy.hpp" #include "strata/prefill/share_rules.hpp" #include "strata/core/arch_defaults.hpp" #include "strata/core/kfd_quiet.hpp" // LOAD: the first prompt's buffers at init (integrated gfx11, Linux HIP) #include #if defined(__cpp_lib_atomic_wait) #define STRATA_ATOMIC_WAIT 1 // #1488: std::atomic::wait / notify_all are a GCC 11 library feature; GCC 10 spins instead #endif #include "mmq_resident_sort.hpp" #include "wmma_gemm.h" #include "strata/core/kv_host_dma.hpp" #include "strata/core/mtp.hpp" #include "strata/core/pinned_ring.hpp" #include "strata/core/progress.hpp" #include "strata/core/on_device.hpp" #include "strata/core/stream_watch.hpp" #include "strata/platform/aux_cpus.hpp" #include "strata/core/layout.hpp" #include "strata/kernels/cpu/expert.hpp" #include "strata/kernels/native_qsa.hpp" #include "strata/kernels/native_mmvq.hpp" #include "strata/kernels/cpu/native_expert.hpp" #include "strata/kernels/cpu/pool.hpp" #include "strata/kernels/native_qsa_indexer.hpp" #include "strata/kernels/ngram.hpp" #include "strata/kernels/ple.hpp" #include "strata/kernels/native_ple_postops.hpp" #include "strata/kernels/iq_kernels.hpp" #include "strata/kernels/cpu/expert_layout.hpp" #include "strata/core/latent_dense.hpp" // latent MoE: the per-layer D/U Q8_0 tables, host-side #include "strata/core/latent_share.hpp" // latent MoE: the session-level device D/U pair, pulled in init #include "strata/kernels/latent.hpp" // latent_plan_rows / latent_u_grouped (the verify soul) #include "strata/kernels/cpu/hadamard.hpp" #include "strata/kernels/qsa.hpp" #include "strata/kernels/kv_stream.hpp" #include "strata/kernels/cvec.hpp" #include "strata/kernels/kv_q4.hpp" #include "strata/core/layer.hpp" #include "strata/core/native_dense.hpp" #include "strata/core/native_head.hpp" #include "strata/kernels/verify_kernels.hpp" #include "strata/kernels/qsa_decode_attn.hpp" #include "strata/kernels/qsa_pattn.hpp" #include "strata/kernels/qsa_prompt_attn.hpp" #include "strata/kernels/qsa_select.hpp" #include "strata/kernels/qsa_two_pass.hpp" #include "strata/kernels/qsa_two_pass_status.hpp" #include "strata/prefill/gemm.hpp" #include "strata/prefill/moe_fused.hpp" #include "strata/prefill/moe_ws.hpp" #include "strata/prefill/moe_fused_iq.hpp" #include "strata/prefill/moe_mmq.hpp" #include "strata/kernels/i8_expert_rule.hpp" #include "strata/prefill/i8_group.hpp" #include "strata/prefill/routed_stream.hpp" #include "strata/prefill/lat_group_tables.hpp" #include "strata/prefill/lat_q3k_fused.hpp" // M4: STRATA_LAT_FUSED_Q3K #include "strata/prefill/moe_fused_q3k.hpp" // the q3k pack's variant of v5's latent kernel #include "strata/prefill/staged_batch.hpp" #include "strata/core/peer_experts.hpp" #include "strata/core/expert_nibble.hpp" #include "strata/core/sm120_series.hpp" namespace strata::kernels { // the P100 class rule's entry point (iq_kernels.cu; Metal: stubs_i8.cpp); declared here so that no kernel object's header changes bool native_expert_i8_floor_gated() noexcept; // true: the unset int8 default gives way to the FP16 path from stream_all_min() tokens up (Tesla P100, build without MMQ) } // namespace strata::kernels namespace { // STRATA_PLE_BATCH for the prompt path: the user's variable wins; unset, it is on except for an engine built with CUDA 12.8 on a compute capability 12.0 card // (sm120_series.hpp ple_batch_default), which says so once. bool ple_batch_enabled() { #if defined(CUDART_VERSION) && !defined(STRATA_USE_HIP) && !defined(__HIPCC__) constexpr int kRt = CUDART_VERSION; constexpr bool kHip = false; #else constexpr int kRt = 0; constexpr bool kHip = true; #endif static const char* env = std::getenv("STRATA_PLE_BATCH"); int dev = 0, major = 0, minor = 0; if (env == nullptr && !kHip) { if (cudaGetDevice(&dev) == cudaSuccess) { cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev); cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, dev); } (void) cudaGetLastError(); } const bool on = strata::ple_batch_resolve(env, kRt, kHip, major, minor); static std::atomic said{false}; if (!on && env == nullptr && !said.exchange(true)) std::fprintf(stderr, "strata prefill: the batched PLE post-ops are off by default here: this engine was built with CUDA 12.8, whose build faults in the prompt path on a " "compute capability 12.0 card (issues #220, #224). Use an engine built with CUDA 13.0 or newer; STRATA_PLE_BATCH=1 turns it on anyway.\n"); return on; } } // namespace #include "strata/core/defaults_026.hpp" #include "strata/prefill/kernels.hpp" #include "strata/prefill/kva.hpp" // STRATA_KVA: the optional approximate-prefill plan (research, off by default) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if !defined(STRATA_PREFILL_MMQ) && !defined(STRATA_PREFILL_FUSED) // A build without the llama.cpp sources (no STRATA_NATIVE_EXPERTS): no MMQ, the FP16 expert path everywhere. (P4-SPLIT: a build with // Strata's own prompt kernels but no MMQ, -DSTRATA_MMQ=OFF, has the real helpers and src/prefill/mmq_absent.cpp instead.) #include "strata/core/geometry.hpp" namespace strata::prefill::mmq { bool built() { return false; } bool supported(int) { return false; } bool fits(int, int64_t) { return false; } size_t matrix_bytes(int, int64_t, int64_t) { return 0; } size_t q8_bytes(int64_t, int64_t) { return 0; } void quantize(const float*, const int32_t*, void*, int, int64_t, int64_t, int64_t, void*, float*) {} bool fp4_activations(int) { return false; } void quantize_scatter(const float*, const int32_t*, const int32_t*, void*, int, int64_t, int64_t, int64_t, int, void*) {} Context::Context() {} Context::~Context() {} void Context::run(const Product&, void*) {} void gather_native(const void*, const void*, size_t, const void*, size_t, void*, void*, void*, const void*, void*, size_t) {} bool gather_native_group(const GatherGroup&, size_t, size_t, size_t, size_t, void*, size_t, void*, size_t, void*, size_t, void*, size_t) { return false; } void gather_strata_q2(const uint8_t*, void*, void*, void*) {} void swiglu(const float*, float*, int64_t, int64_t, bool, void*) {} bool swiglu_quant_ok(int) { return false; } void swiglu_quant(const float*, void*, int64_t, bool, void*) {} void swiglu_scaled(const float*, float*, int64_t, int64_t, const int32_t*, int, const float*, int64_t, void*) {} void down_row_scales(float*, const int32_t*, int, const float*, int64_t, void*) {} void iota(int32_t*, int64_t, void*) {} bool tab_built() { return false; } bool tab_fits(int, int64_t) { return false; } size_t overread_bytes(int, int64_t) { return 0; } bool whole_tiles(int, int64_t, int) { return false; } int direct_mode() { return 0; } size_t direct_slot_guard() { return 0; } void prep_group(const GuardTab&, size_t, void*, void*, size_t, void*) {} } // namespace strata::prefill::mmq #endif #ifndef STRATA_PREFILL_FUSED // #136: the fused int8 experts are CUDA-only (HIP and builds without MMQ keep the MMQ / FP16 paths) namespace strata::prefill::fused { bool built() { return false; } bool gfx12_iq2s_default() { return false; } // AMD-Q: a HIP build without the fused kernels (STRATA_PREFILL_MMQ off) still links prefill.cpp's share-limit call bool available() { return false; } bool enabled() { return false; } bool requested() { return false; } size_t act_bytes(int64_t, int64_t) { return 0; } size_t group_bytes(int64_t, int) { return 0; } void quantize_act(const float*, int64_t, int64_t, void*, void*) {} void group(const int32_t*, int64_t, int, int, void*, int32_t*, int32_t*, void*) {} void group_bounds(const int32_t*, int, void*, void*) {} void experts(const Batch&, int, int64_t, const void*, const void*, const int32_t*, void*, float*, void*) {} bool native_supported(int, int) { return false; } void quantize_act_native(const float*, int64_t, int64_t, void*, void*) {} void experts_native(const Batch&, const NativeGeom&, int, int64_t, const void*, const void*, const int32_t*, void*, float*, void*) {} bool native_wide() { return false; } void experts_native(const WideBatch&, const NativeGeom&, int, int64_t, const void*, const void*, const int32_t*, void*, float*, void*) {} void set_down_rotation(bool, const float*) {} bool p1a_wanted() { return false; } void p1a_input(const void*, size_t) {} bool p1a_used() { return false; } bool p1a_takes_pair(int, int) { return false; } void hadamard128_rows(float*, int64_t, int64_t, const float*, void*) {} } // namespace strata::prefill::fused #endif #if defined(STRATA_PREFILL_FUSED) && !defined(STRATA_PREFILL_MMQ) // P4-SPLIT: built with Strata's own prompt kernels and without llama.cpp's MMQ (-DSTRATA_MMQ=OFF, or HIP without STRATA_PREFILL_MMQ) #define STRATA_PF_OWN_ONLY 1 namespace { // Such an engine takes Strata's fused kernels wherever they exist for the card and the layer's formats, so the opt-in switches of those // kernels are on by default here (a switch the user set, either value, is kept); every other layer takes the FP16 path. Set before main(), // so the first reader of each (the engine's start-up rules, the kernels' own static reads) sees it. Listed once in the prompt path's line. // (the list lives in own_only_switches.hpp: the start-up rules of generate.cpp ask it which names the build set and which the user did) std::vector& own_only_switches() { return strata::prefill::own_only_recorded(); } const bool g_own_only_defaults = [] { strata::prefill::own_only_build_flag() = true; // (the engine's start-up rules ask own_only_build(): a build without MMQ has no MMQ kernels to go back to) strata::kernels::native_expert_i8_set_mmq_free(); // the int8 expert kernels are the default prompt-expert kernels here (i8_expert_rule.hpp, i8_switch_effective) #if defined(STRATA_USE_HIP) || defined(__HIPCC__) // (gfx12 only: every one of these is read behind the device's own gfx12 check, moe_fused_iq.cu q8d_hip_requested .. q6k_hip_requested, so a gfx11 // card keeps the rules it has: its layers of those formats take the FP16 path here as before) constexpr bool kHipList = true; // (include/strata/prefill/own_only_switches.hpp) #else // (STRATA_PF_FUSED_Q2N, the native Q2_0 pack's Q2_0 / Q2_0 layers, is in the list: the kernel reads it behind its own sm_80 check, so a card below sm_80 is unchanged, // and =0 takes it back; the reasons and the numbers are in own_only_switches.hpp) constexpr bool kHipList = false; #endif // a name that is already in the environment is the user's and is not recorded (the engine's rules read the record: own_only_user_named) strata::prefill::own_only_apply_defaults( kHipList, [](const char* k) { return std::getenv(k); }, [](const char* k) { #if defined(_WIN32) _putenv_s(k, "1"); #else setenv(k, "1", 0); #endif }, own_only_switches()); return true; }(); } // namespace #endif namespace { // own_only_line.hpp peer_keeps_mmq: this engine has no MMQ, so --peer-device's prompt share is always declined (set_peer) and the walk keeps its own kernels. // Here and not in the header: STRATA_PF_OWN_ONLY is defined above, after the header was included. #if defined(STRATA_PF_OWN_ONLY) constexpr bool kPfOwnOnlyBuild = true; #else constexpr bool kPfOwnOnlyBuild = false; #endif } // namespace #ifndef STRATA_PREFILL_WS // the weight-stationary experts (moe_ws.hpp) are in the CUDA STRATA_EXPERIMENTAL_SM60 build only namespace strata::prefill::ws { int mode() { return 0; } bool supported(int, int) { return false; } bool faster(int, double) { return false; } size_t chunks(const int32_t*, size_t, const int32_t*, const int32_t*, Chunk*) { return 0; } bool gate_up(const Layer&, void*) { return false; } void down(const Layer&, void*) {} } // namespace strata::prefill::ws #endif #if defined(STRATA_GPU_METAL) // The Metal backend reads a prompt through its decode window (metal/src/engine/window.cpp): init keeps the chunk, run // hands over, draft_kv declines (the MTP hook reads the window's rows itself). namespace strata::metal_engine { bool window_ensure(const strata::core::ModelGeometry& g, strata::core::SessionState& ss, int64_t max_cells, std::string& err); bool prompt_taps(const std::vector& layers, int64_t first_pos, const std::function& cb, std::string& err); // dflash2.inc bool prompt_run(const int64_t* tokens, int64_t n, int64_t pos0, const float* const* embd_rows, const std::function& on_chunk, std::string& err); } // namespace strata::metal_engine #endif namespace { // STRATA_PLE_COMPACT=1 (opt-in): the prompt chunk's PLE rows go up raw and are dequantized on the device (IQ4_NL tables // held in memory); the same floats as the host dequant, 7x fewer bytes over PCIe bool ple_compact_on() { static const bool on = [] { const char* v = std::getenv("STRATA_PLE_COMPACT"); return v != nullptr && v[0] == '1'; }(); return on; } // STRATA_PLE_COPY_STREAM=1 (opt-in, with PLE_COMPACT): the raw rows go up on a stream of their own instead of the compute // stream, which otherwise idles for the DMA (4.7 MB a 3K chunk = 2.9 ms on a Gen2 x4 link); the same bytes and kernels bool ple_copy_stream_on() { static const bool on = [] { const char* v = std::getenv("STRATA_PLE_COPY_STREAM"); return v != nullptr && v[0] == '1'; }(); return on; } } // namespace namespace strata::prefill { namespace { using Clock = std::chrono::steady_clock; constexpr float EPS = 1e-6f; // the shape the engine is compiled for (include/strata/core/geometry.hpp; -DSTRATA_GEOMETRY picks it) constexpr int64_t N = strata::core::kCompiledGeometry.n_embd, HC = strata::core::kCompiledGeometry.hc, D = N * HC, LR = strata::core::kCompiledGeometry.hc_lr, K = strata::core::kCompiledGeometry.n_expert_used, NE = strata::core::kCompiledGeometry.n_expert; constexpr int64_t C = strata::core::kCompiledGeometry.ssm_conv_channels, ZV = strata::core::kCompiledGeometry.ssm_value_dim, HV = strata::core::kCompiledGeometry.ssm_v_heads; // widths the buffers and kernel arguments below are written in (int, as the literals they replace were) constexpr int kEmbd = (int) strata::core::kCompiledGeometry.n_embd, kFF = (int) strata::core::kCompiledGeometry.n_ff, kFF2 = 2 * kFF; constexpr int kNHead = (int) strata::core::kCompiledGeometry.n_head, kNKv = (int) strata::core::kCompiledGeometry.n_head_kv, kHeadDim = (int) strata::core::kCompiledGeometry.head_dim; constexpr int kQW = kNHead * kHeadDim, kQFull = 2 * kQW, kKvW = kNKv * kHeadDim; static_assert(kQW == strata::core::kCompiledGeometry.ssm_value_dim, "prefill: the attention width (heads x head_dim) and the GDN value width share the m.q / m.attn / m.attn_h buffers, so they must be equal"); constexpr int kIdxQHeads = (int) strata::core::kCompiledGeometry.idx_q_heads, kIdxK = (int) strata::core::kCompiledGeometry.idx_key_dim, kIdxQW = kIdxQHeads * kIdxK; // plan v0.3 P6: staging holds the largest blob of the pack (a native pack's blobs differ per layer) inline int64_t MAXBLOB() { return (int64_t) strata::kernels::cpu::expert_layout().max_blob; } constexpr int STAGE = 8; // host->device expert staging ring (chunks below stream_all_min()) // The staged walk's ring when its MMQ groups take the staged experts too: two groups, so a slot is refilled only after // the group that read it was gathered (the walk stages at most STAGE_GRP - MMQ_GROUP - 1 entries ahead) constexpr int STAGE_GRP = 32; constexpr int kSplitHelpRing = 48; // layer split help: the helper stage's ring slots it streams through (at most) // The staged walk's batched group entry (fz_staged below): on by default in a CUDA build, opt-in (STRATA_PF_FUSED=1) in a HIP build (owner 2026-11-12, "8 yes for // nvidia"; staged_group_rule.hpp). The card's own fused rule (fused::native_supported) still has to take the layer's formats. inline bool staged_group_entry_on() { static const bool on = [] { #if defined(STRATA_USE_HIP) || defined(__HIPCC__) || defined(STRATA_HIP_GFX906) constexpr bool hip = true; #else constexpr bool hip = false; #endif return strata::prefill::staged_group_entry_default(hip, std::getenv("STRATA_PF_FUSED")); }(); return on; } // STRATA_PREFILL_GROUP_GATHER (unset or not 0): the staged walk's resident groups (read again here for P4-SMALL's fused groups, which // need that staging: the same variable, the same answer) inline bool staged_group_gather() { static const bool on = [] { const char* v = std::getenv("STRATA_PREFILL_GROUP_GATHER"); return v == nullptr || std::atoi(v) != 0; }(); return on; } // STRATA_PREFILL_STREAM_AHEAD=0 keeps the previous routed-only upload schedule (A/B). inline bool stream_ahead_enabled() { static const bool on = [] { const char* v = std::getenv("STRATA_PREFILL_STREAM_AHEAD"); return v == nullptr || std::atoi(v) != 0; }(); return on; } // Step 3: from this chunk size on, every non-resident expert of every layer streams in a fixed order through a // ring_slots()-slot ring (nearly all 512 are routed at such a chunk), so the copy engine keeps working through the // attention halves instead of waiting for each layer's routing. // #828: STRATA_TEST_PAGEABLE=1 makes every pinned host allocation with a pageable fallback in Prefill::init fail, so the // fallbacks (what an LXC container's memlock limit forces) run on a PC that can pin - for ASan / MALLOC_CHECK_=3 runs. inline bool force_pageable() { static const bool v = [] { const char* e = std::getenv("STRATA_TEST_PAGEABLE"); return e != nullptr && e[0] == '1'; }(); return v; } // #1057: the Stager threads wait by sleeping (atomic wait, blocking-sync events) instead of a yield spin. On Linux the // spinners starved a pinned host thread (DGX Spark IQ3_S 8K prompt 91 -> 1,222 tok/s with sleeping waits); on Windows // the 5070 read an 8K Q2_0 prompt 1.2% slower with them (1,469.5 -> 1,452 tok/s, 10 pairs), so they stay spinning // there. Same output either way. STRATA_STAGER_SLEEP=1/0 forces either. inline bool stager_sleep() { static const bool v = [] { const char* e = std::getenv("STRATA_STAGER_SLEEP"); if (e != nullptr && e[0] != '\0') return e[0] != '0'; #if defined(_WIN32) return false; #else return true; #endif }(); return v; } constexpr int RING_MAX = 1024; // the arrays; the ring itself is ring_slots(), at most ring_cap() // The chunk size from which every expert streams: 1024 since 0.1.30 (was 2048). Measured on the 5070, Q2_0 / IQ2_XS, // fixed cache: 1,500-token prompts 621 -> 785 / 612 -> 735 tok/s, 2,000 727 -> 934 / 712 -> 892, 4,000 (its last // chunk) 779 -> 912 / 766 -> 844, the same output. Below ~1,000 tokens the output changed on Q2_0 (a smaller chunk // takes other kernels), so 1024 is the floor. STRATA_PREFILL_STREAM_MIN overrides (A/B). With the CPU share armed // (Prefill::arm_cpu_share) it is the share's own limit instead: the share applies to staged chunks only. constexpr int64_t kStreamAllMinDefault = 1024; // the default above (and the CPU share's default limit) int64_t g_stream_min_share = 0; // A layer set in which EVERY layer has no MMQ tile for its formats and the fused kernels take it (UD-Q4_K_XL's Q4_K / Q5_K gate/up with a Q5_1 / Q8_0 down: STRATA_MMQ_KQ is // opt-in, so mmq::fits is false; the same test mmq_plan() makes, p.fo). Its chunks below the floor take the staged walk, which has no fused entry for such a layer // (fz_staged needs use_mmq, or the staged entry for fused-only layers: STRATA_PF_STAGED_FO, on by default on the class below) and without it runs every routed expert on the dequantize-to-FP16 path: about 1.6 s a prompt whatever its length (stream_floor_rule.hpp). inline bool layers_all_fused_only() { const auto& lay = strata::kernels::cpu::expert_layout(); if (!lay.native || lay.fmt.empty()) return false; if (const char* e = std::getenv("STRATA_PREFILL_MMQ"); e != nullptr && std::atoi(e) == 0) return false; // mmq_plan(): no plan, no fused-only layers #if defined(STRATA_PF_OWN_ONLY) if (!fused::built()) return false; #else if (!mmq::built()) return false; #endif for (const auto& f : lay.fmt) { if (mmq::fits(f.gu_type, kFF2) && mmq::fits(f.d_type, N)) return false; // a layer on MMQ if (!fused::native_supported(f.gu_type, f.d_type)) return false; // a layer on the FP16 path at every size } return true; } // The device class the lower floor was measured on: Windows, one visible card, gfx1151 (Strix Halo). Not cached before a card answers. inline bool stream_floor_class_measured() { #if defined(STRATA_USE_HIP) && defined(_WIN32) static std::atomic state{-1}; int s = state.load(std::memory_order_acquire); if (s < 0) { int dev = 0, n = 0; cudaDeviceProp prop; if (cudaGetDeviceCount(&n) != cudaSuccess || cudaGetDevice(&dev) != cudaSuccess || cudaGetDeviceProperties(&prop, dev) != cudaSuccess) { (void) cudaGetLastError(); return false; } s = n == 1 && strata::kernels::gfx_arch_is_gfx1151(prop.gcnArchName) ? 1 : 0; state.store(s, std::memory_order_release); } return s == 1; #else return false; #endif } // The staged walk's group entry (fz_staged) also takes a layer with no MMQ tile that the fused kernels cover (mmq_plan().fo), routed experts only, so a prompt chunk below the long-prompt // floor does not run each routed expert on the dequantize + FP16 GEMM path (staged_fo_rule.hpp). STRATA_PF_STAGED_FO=1 asks for it on any card (research), 0 turns it off; unset: in a build without MMQ on every class for the layers the fused kernels cover; in a build with MMQ on where it was // measured (Windows gfx1151 with a layer set that is fused-only on every layer, and the entry's own prerequisites: the staged walk's resident-group gather and its group entry). Decided once when // the expert layout is known (a call before that answers from the variable alone and is not cached). inline bool staged_fo_on() { static std::atomic cached{-1}; const int c = cached.load(std::memory_order_acquire); if (c >= 0) return c == 1; const char* env = std::getenv("STRATA_PF_STAGED_FO"); const auto& lay = strata::kernels::cpu::expert_layout(); if (!lay.native || lay.fmt.empty()) return strata::prefill::staged_fo_switch(env); const bool cls = stream_floor_class_measured(); #if defined(STRATA_PF_OWN_ONLY) constexpr bool own_only = true; // a build without MMQ: the fused staged entry on every class, for every layer the fused kernels cover (staged_fo_rule.hpp) #else constexpr bool own_only = false; #endif const bool v = strata::prefill::staged_fo_decide(env, strata::prefill::staged_fo_class_default(own_only, cls && layers_all_fused_only(), staged_group_gather() && staged_group_entry_on())); cached.store(v ? 1 : 0, std::memory_order_release); return v; } // A layer on which the CPU share took experts (rows_cpu > 0) in a build without MMQ keeps the staged fused entry and the int8 kernels for the GPU's own experts: their rows, bounds and scratch come // from `order` / `off`, the CPU's rows are the last block of Dm and the GPU never writes them (share_own_rule.hpp). A build with MMQ keeps the old rule (MMQ's groups take the layer). // STRATA_PF_SHARE_OWN=0 is the old rule everywhere, =1 the new one in a build with MMQ too (research). inline bool share_keeps_own_kernels(int64_t rows_cpu) { static const char* const env = std::getenv("STRATA_PF_SHARE_OWN"); #if defined(STRATA_PF_OWN_ONLY) constexpr bool own_only = true; #else constexpr bool own_only = false; #endif return strata::prefill::share_layer_keeps_own_kernels(own_only, env, rows_cpu); } // STRATA_SWITCH_POINT (CUDA builds only): the floor of a card that streams part of the experts comes from switch_point_rule.hpp's cost estimate. ON BY DEFAULT for the class // in_default_class() names (NVIDIA discrete card on PCIe, one GPU, link probe ran, compute capability 8.6); STRATA_SWITCH_POINT=0 is the way back, =1 asks for it on any other CUDA card. // The card's counts arrive from Prefill::set_card_expert_share (the cache's slots and layers x experts, known before the first lend); until then the default answers. std::atomic g_sp_resident{-1}, g_sp_total{0}; double g_sp_blob_mb = 0.0, g_sp_link_gbps = 0.0; // the cache's mean blob (MB) and the start-up probe (GB/s), from Prefill::set_card_expert_share int g_sp_sm = 0; // the card's multiprocessor count int g_sp_cc_major = 0, g_sp_cc_minor = 0; // the card's compute capability bool g_sp_integrated = true; // not known as discrete until the card answered // STRATA_SWITCH_POINT as set: -1 unset, 1 for "1", 0 for any other value (the way back) inline int switch_point_env() { static const int e = [] { const char* v = std::getenv("STRATA_SWITCH_POINT"); return v != nullptr && v[0] != '\0' ? strata::prefill::switch_point::env_state(v) : -1; }(); return e; } // the card's counts have arrived (set_card_expert_share ran: a single card, no batch slots) inline bool switch_point_known() { return g_sp_resident.load(std::memory_order_acquire) >= 0; } inline bool switch_point_on() { #if defined(STRATA_USE_HIP) || defined(__HIPCC__) || defined(STRATA_HIP_GFX906) return false; // measured on CUDA only #else if (!switch_point_known()) return false; strata::prefill::switch_point::DeviceClass d; d.cuda = true; d.discrete = !g_sp_integrated; d.single_gpu = true; // set_card_expert_share is called from the single-card, no-batch paths only d.link_probed = g_sp_link_gbps > 0.0; d.cc_major = g_sp_cc_major; d.cc_minor = g_sp_cc_minor; return strata::prefill::switch_point::enabled(d, switch_point_env()); #endif } // the card as the rule scales it; a field not set gives card_known() == false and the default floor inline strata::prefill::switch_point::Card sp_card() { strata::prefill::switch_point::Card c; c.blob_mb = g_sp_blob_mb; c.link_gbps = g_sp_link_gbps; c.sm = g_sp_sm; return c; } // A build without MMQ (STRATA_PF_OWN_ONLY) loading a pack that is not a native one (the AVX-512 Q2_0 pack) whose layers the plan marks fused (mmq_plan(): `!lay.native && fused::enabled()`). // The staged fused entry (fz_staged) and the int8 kernels (i8_l) read the native layout only, so below the floor each routed expert of such a layer runs on the dequantize + FP16 path. // False without a loaded layout (the caller does not cache then) and in a build with MMQ. inline bool plain_pack_all_fused() { #if defined(STRATA_PF_OWN_ONLY) const auto& lay = strata::kernels::cpu::expert_layout(); if (lay.native || lay.n_layers <= 0) return false; if (const char* e = std::getenv("STRATA_PREFILL_MMQ"); e != nullptr && std::atoi(e) == 0) return false; // mmq_plan(): no plan, no fused layers return fused::built() && fused::enabled(); #else return false; #endif } // A build without MMQ (STRATA_PF_OWN_ONLY) on a gfx12 card (R9700, RX 9070 / 9060) loading a native pack whose every layer is a trellis pair (Gyro-S TQK6 / TQK7 and the other trellis types) that the fused kernels // take (layers_all_fused_only(): no layer on MMQ or on the FP16 path). Below the floor the staged fused entry reads these layers about twice as slowly as MMQ did (R9700, Gyro-S, 128 to 1,000 tokens: 542 to 1,241 ms // against 252 to 659 ms), the streamed walk is faster than MMQ at every size (167 to 415 ms). False in a build with MMQ, on every other card, and without a loaded layout. inline bool trellis_gfx12_all_fused() { #if defined(STRATA_PF_OWN_ONLY) && defined(STRATA_USE_HIP) const auto& lay = strata::kernels::cpu::expert_layout(); if (!lay.native || lay.fmt.empty()) return false; for (const auto& f : lay.fmt) if (f.gu_type < (int) strata::quant::Type::TQ2_T || f.gu_type > (int) strata::quant::Type::TQP7) return false; // not a trellis gate/up int dev = 0; cudaDeviceProp prop; if (cudaGetDevice(&dev) != cudaSuccess || cudaGetDeviceProperties(&prop, dev) != cudaSuccess) { (void) cudaGetLastError(); return false; } return strata::kernels::gfx_arch_is_gfx12_wmma(prop.gcnArchName) && layers_all_fused_only(); #else return false; #endif } // The same layer set on a CUDA card (a build without MMQ, STRATA_PF_OWN_ONLY, not HIP): a native pack whose every layer is a trellis pair (Gyro-S TQK6 / TQK7) that the fused kernels take (layers_all_fused_only()). // No card query here: on CUDA fused::native_supported takes the trellis pair only where the kernels are set up (the sm_86 class, compute capability 8.0 / 8.6 / 8.9, with 80 or more SMs by default; any // sm_80+ card with STRATA_PF_FUSED_TQ=1; none with STRATA_PF_FUSED=0 or _TQ=0), so the floor is lowered exactly where those kernels run. False in a build with MMQ, on HIP, and without a loaded layout. inline bool trellis_cuda_all_fused() { #if defined(STRATA_PF_OWN_ONLY) && !defined(STRATA_USE_HIP) const auto& lay = strata::kernels::cpu::expert_layout(); if (!lay.native || lay.fmt.empty()) return false; for (const auto& f : lay.fmt) if (f.gu_type < (int) strata::quant::Type::TQ2_T || f.gu_type > (int) strata::quant::Type::TQP7) return false; // not a trellis gate/up return layers_all_fused_only(); #else return false; #endif } // The gfx12 set on Windows gfx1151 (one card, stream_floor_class_measured()): a build without MMQ (STRATA_PF_OWN_ONLY) loading a native pack whose every layer is a trellis pair that the fused kernels // take (layers_all_fused_only()). On gfx1151 the fused kernels take the trellis pair only where fused_tq_hip.hpp device_on says so (STRATA_PF_TQ_G1151), so with the switch off this is false. Below the // 1,024 floor the staged fused entry does not take Gyro-S's Hadamard-folded layers and every chunk ran on the FP16 path (256 tokens 1,914 ms against 726 ms in 0.2.13): the floor goes to 64 // (stream_floor_rule.hpp) and --short-read keeps 64 (staged_fo_rule.hpp). False in a build with MMQ, on Linux, on every other card, and without a loaded layout. inline bool trellis_g1151_all_fused() { #if defined(STRATA_PF_OWN_ONLY) && defined(STRATA_USE_HIP) && defined(_WIN32) const auto& lay = strata::kernels::cpu::expert_layout(); if (!lay.native || lay.fmt.empty()) return false; for (const auto& f : lay.fmt) if (f.gu_type < (int) strata::quant::Type::TQ2_T || f.gu_type > (int) strata::quant::Type::TQP7) return false; // not a trellis gate/up return stream_floor_class_measured() && layers_all_fused_only(); #else return false; #endif } // a layer of the loaded native pack is Hadamard-folded (its gate/up or its down): the staged fused entry (fz_staged) does not take such a layer inline bool layers_any_folded() { const auto& lay = strata::kernels::cpu::expert_layout(); if (!lay.native) return false; for (const auto& f : lay.fmt) if (f.had_gu || f.had_d) return true; return false; } #if defined(STRATA_PF_OWN_ONLY) // The two start-up lines of a build without MMQ that name the long-prompt floor ("prompt pieces of N tokens or more read on the fused kernels ..." when the pack's own floor is // cached, and "prompt kernels: chunks of N tokens or more ..." when the plan is made) are printed once arm_cpu_share has run (own_only_line.hpp OwnOnlyLineDeferred), with // the floor stream_all_min() returns then: both moments come before the CPU share is armed and before the card has answered, and printed there they named 64 while the engine // ran at the share's 3,072 or the plain pack's 1,536. own_only_line_flush() is defined after stream_all_min(). std::mutex g_own_line_mu; strata::prefill::OwnOnlyLineDeferred g_own_line; inline void own_only_line_flush(); #endif // The floor without STRATA_PREFILL_STREAM_MIN and the CPU share: stream_floor_rule.hpp's answer, decided once when the expert layout is known (a call before that gets the default and is not cached). inline int64_t default_stream_floor() { static std::atomic cached{-1}; const int64_t c = cached.load(std::memory_order_acquire); if (c >= 0) return c; const auto& lay = strata::kernels::cpu::expert_layout(); const bool plain = plain_pack_all_fused(); // a build without MMQ and a non-native pack on the fused kernels: no staged entry or int8 path exists for its layout (stream_floor_rule.hpp) if ((!lay.native || lay.fmt.empty()) && !plain) return strata::prefill::kStreamFloorDefault; const bool cls = !plain && stream_floor_class_measured(); int64_t v = strata::prefill::stream_floor_default(cls, cls && layers_all_fused_only(), staged_fo_on(), plain, trellis_gfx12_all_fused(), trellis_cuda_all_fused(), !plain && trellis_g1151_all_fused()); #if !defined(STRATA_USE_HIP) && !defined(__HIPCC__) && !defined(STRATA_HIP_GFX906) // the card's counts are not known yet (set_card_expert_share has not run) and the rule is not switched off: the default, not cached if (v == strata::prefill::kStreamFloorDefault && switch_point_env() != 0 && !switch_point_known()) return v; #endif if (v == strata::prefill::kStreamFloorDefault && switch_point_on()) { const int64_t res = g_sp_resident.load(std::memory_order_acquire), tot = g_sp_total.load(std::memory_order_acquire); const auto card = sp_card(); if (!strata::prefill::switch_point::card_known(card)) return v; // the link or the card is not known: the default, not cached v = strata::prefill::switch_point::floor_for_card(res, tot, card); if (v != strata::prefill::kStreamFloorDefault) std::fprintf(stderr, "prefill: prompt chunks of %lld tokens or more stream every expert the card does not hold (%lld of %lld experts resident; %.2f MB experts, %.1f GB/s link, %d SMs); a shorter chunk stages only the routed ones. STRATA_SWITCH_POINT=0 keeps the fixed 1,024.\n", (long long) v, (long long) res, (long long) tot, card.blob_mb, card.link_gbps, card.sm); cached.store(v, std::memory_order_release); return v; } cached.store(v, std::memory_order_release); #if defined(STRATA_PF_OWN_ONLY) if (v != strata::prefill::kStreamFloorDefault) { // printed with the floor in force once the share is armed (own_only_line_flush) { std::lock_guard lk(g_own_line_mu); g_own_line.want_pieces(plain); } own_only_line_flush(); } #else if (v != strata::prefill::kStreamFloorDefault) std::fprintf(stderr, "prefill: prompt pieces of %lld tokens or more read on the fused kernels, as long prompts do. STRATA_PREFILL_STREAM_MIN=1024 keeps the old rule.\n", (long long) v); #endif return v; } // The plain pack in a build without MMQ on a DISCRETE CUDA card takes strata::prefill::kPlainDiscreteFloor, with or without the CPU share (plain_floor_rule.hpp: the share's limit would replace the // floor although it takes nothing of that pack, and the plain pack's own 64 was measured on an integrated GPU). The card must have answered (set_card_expert_share ran: one card, no batch slots) and // the expert layout be loaded; before that the old rule answers and nothing is cached. Integrated cards, HIP builds and a build with MMQ never take it. STRATA_PF_PLAIN_FLOOR=0 is the way back. inline bool plain_discrete_floor_now() { #if defined(STRATA_PF_OWN_ONLY) && !defined(STRATA_USE_HIP) && !defined(__HIPCC__) && !defined(STRATA_HIP_GFX906) static std::atomic cached{-1}; const int c = cached.load(std::memory_order_acquire); if (c >= 0) return c == 1; if (strata::kernels::cpu::expert_layout().n_layers <= 0 || !switch_point_known()) return false; const bool v = strata::prefill::plain_takes_discrete_floor(plain_pack_all_fused(), !g_sp_integrated, std::getenv("STRATA_PF_PLAIN_FLOOR")); cached.store(v ? 1 : 0, std::memory_order_release); return v; #else return false; #endif } inline int64_t stream_all_min() { static const int64_t env = [] { const char* e = std::getenv("STRATA_PREFILL_STREAM_MIN"); return e ? (int64_t) std::atoll(e) : (int64_t) -1; }(); if (env >= 0) return env; #if defined(STRATA_PF_OWN_ONLY) const bool discrete = plain_discrete_floor_now(); // (default_stream_floor() prints its line once and caches: only asked when its answer can be the result, as before) return strata::prefill::floor_with_share(discrete, g_stream_min_share, discrete || g_stream_min_share <= 0 ? default_stream_floor() : 0); #else // a build with MMQ keeps the old text of this function (its compiled code stays the same) return g_stream_min_share > 0 ? g_stream_min_share : default_stream_floor(); #endif } #if defined(STRATA_PF_OWN_ONLY) inline void own_only_line_flush() { { std::lock_guard lk(g_own_line_mu); if (!g_own_line.due() && !g_own_line.pieces_due()) return; } const int64_t floor_tokens = stream_all_min(); // outside the lock: a first call caches default_stream_floor(), which flushes itself (nothing is printed twice) std::lock_guard lk(g_own_line_mu); if (g_own_line.pieces_due()) std::fprintf(stderr, "%s\n", g_own_line.take_pieces(floor_tokens).c_str()); if (g_own_line.due()) std::fprintf(stderr, "%s\n", g_own_line.take(floor_tokens).c_str()); } #endif // STRATA_PREFILL_CPU_SHARE (opt-in; set_cpu_pool): a chunk below stream_all_min() hands the decode CPU pool - idle // while a prompt is read - the non-resident experts few of its tokens route to, instead of streaming them over PCIe. // `auto`: the share measured, each layer's CPU time per expert and the GPU's per streamed expert (CUDA events around // its expert work, read after the next layer's routing sync - the host reaches the combine long before the GPU does) // as running means, and the next layers hand the CPU g / (c + g) of them, where both sides end together - so a small // CPU takes a small share - and only while the layers that share are measured cheaper than the ones that do not (the // gate at cpu_maybe). x: a fixed x. Unset or 0: every expert on the GPU (the default). RTX 5090 + 9950X3D, // 600-token prompts: UD-Q4_K_XL 1,528-1,548 -> 1,296-1,369 ms (share 0.65), Q2_0 472-491 -> 445-448, IQ2_XS 507 -> 490 // (250 tokens 402 -> 368); IQ2_XS on 2 AVX2 workers 512 -> 487 (share 0.49). // 0.1.41: ON BY DEFAULT where it was measured (CUDA builds, one GPU, no batch slots): STRATA_PREFILL_CPU_SHARE unset // behaves as `auto` with STRATA_PREFILL_CPU_SHARE_MAX=1024 (chunks below 1024 tokens: -20..-35% at 512 and 1000 tokens // on an RTX 3060, a Tesla P100 and an RTX 5070, 8/8 pairs each; mean KL against the share off about 0.004). // STRATA_PREFILL_CPU_SHARE=0 turns it off: the output is then the 0.1.40.3 bytes. g_share_default is set by // Prefill::arm_cpu_share(.., true) from the paths that qualify; the layer split and batch slots stay off, and HIP except a single gfx12 card (AMD-Q, arm_cpu_share). bool g_share_default = false; // Prefill::fix_cpu_share (--draft-costs, repeatable output): a share learned once on this machine, used like a fixed // STRATA_PREFILL_CPU_SHARE while the variable is unset and the default share is armed: no measured time then changes // which experts the CPU takes (the balance's EMA and the per-layer gate are both skipped, as with the variable). double g_share_fixed = -1.0; inline double cpu_share_explicit() { // -2: the variable is not set; -1: measured static const double v = [] { const char* e = std::getenv("STRATA_PREFILL_CPU_SHARE"); if (e == nullptr) return -2.0; if (std::strcmp(e, "auto") == 0) return -1.0; return std::clamp(std::atof(e), 0.0, 1.0); }(); return v; } inline double cpu_share_env() { // -1: measured const double v = cpu_share_explicit(); return v == -2.0 ? (g_share_default ? (g_share_fixed >= 0.0 ? g_share_fixed : -1.0) : 0.0) : v; } inline bool cpu_share_on() { return cpu_share_env() != 0.0; } // STRATA_PF_PEER_CPU=1 (opt-in): the share also runs beside a peer GPU (--peer-device), which it otherwise leaves off. It // takes only experts that neither card computes, and its rows go before the peer's block, so the peer's stays the tail // its combine reads from (back_at); the staged chunks then go up to cpu_share_max()'s 3072 tokens, not the default // share's 1024. Measured on an RTX 5060 Ti (PCIe 3.0 x8) + RTX 3060, IQ3_XXS, a 161-173K-token conversation, tool results // read one after another: 117-132 tokens 1.80-1.92 -> 1.16 s, 325-335 2.12-2.89 -> 1.36-1.63 s, 524 3.33 -> 1.99 s, // 1067 3.74 -> 2.64 s, 2012 4.33 -> 4.22 s, 3.2-3.5K unchanged (the whole chain 36.0 -> 29.7 s). inline bool pf_peer_cpu() { static const bool on = [] { const char* e = std::getenv("STRATA_PF_PEER_CPU"); return e != nullptr && std::atoi(e) != 0; }(); return on; } // STRATA_PREFILL_CPU_SHARE_MAX (default 3072, at least 1024): with the share on, chunks below it are staged after their // routing - so they can hand the CPU its share - instead of streaming every expert the cards do not hold. The share pays // on chunks up to ~3K tokens, where few tokens route to many of the streamed experts; 1024 keeps the old limit. int64_t g_share_limit_arch = 0; // AMD-Q: the default limit of a gfx12 card (share_rules::gfx12_share_limit), set by arm_cpu_share inline int64_t cpu_share_max() { static const int64_t v = [] { const char* e = std::getenv("STRATA_PREFILL_CPU_SHARE_MAX"); return e ? std::max(kStreamAllMinDefault, (int64_t) std::atoll(e)) : (int64_t) 3072; }(); static const bool explicit_max = std::getenv("STRATA_PREFILL_CPU_SHARE_MAX") != nullptr; // its measured range; STRATA_PF_PEER_CPU's is 3072 (see pf_peer_cpu) if (g_share_default && cpu_share_explicit() == -2.0 && !explicit_max && !pf_peer_cpu()) // the default-armed share: share_rules::default_share_limit (STRATA_PREFILL_CPU_SHARE_MAX=1024 restores the old 1,024) return g_share_limit_arch > 0 ? g_share_limit_arch : strata::prefill::share_rules::default_share_limit(); return v; } // On a layer split every stage may have the pool (set_cpu_pool), and the stages read different chunks at the same time: // one stage at a time takes it for a chunk, the other reads its chunk with the GPU alone. std::atomic g_cpu_pool_busy{false}; // STRATA_PREFILL_CPU_SHARE_BATCH: a slot is decoding beside this read (Prefill::set_share_blocked): the pool is its windows' std::atomic g_share_blocked{false}; double g_pinned_share = 1.0; // The streamed ring: 384 slots when (nearly) every streamed expert is DMA'd from pinned RAM - measured on Q2_0, // 8192-token chunks: 96 slots 1153 tok/s, 384 1294 (the next layer's experts arrive during its attention half) - // and 96 when a large share goes through host copies (IQ3_S on 64 GB, a third unpinned: 96 slots 1216, 256 1070 - // the host copies are the limit and the bigger ring only takes cache slots). STRATA_PREFILL_RING overrides. // // Those are slots on the pack they were measured on, and a slot is one WHOLE expert blob - a Q2_0 blob is // 1,382,400 B, so 384 slots is 506 MiB. On a pack with bigger blobs the same slot count is a different amount of // memory: 1,912 MiB on Q8_0 (5,222,400 B), which is more than half of an 8 GB card's expert cache and what held // that card at a 1,024-token chunk when its own buffers would have fitted 6,144. Measured on the 4-way rig, one // env var and nothing else: chunk 1,024 -> 6,144, prefill 87 -> 402 tok/s. So the budget is kept in BYTES and the // slot count is derived from the pack (`ring_budget_slots`); Q2_0 still resolves to exactly 1024 (fused) and 384, // so the pack all of this was tuned on does not move. int g_ring_override = 0; // #340: set by a layer split (Prefill::set_ring_override); 0 = the rule below // #583 (0.1.39b): the auto chunk scan's byte-budget ring (Prefill::set_ring_budget), for requests whose chunk is above // g_ring_small_max - the chunk 0.1.39's rule would have picked. A prompt that fits that chunk keeps 0.1.39's ring: // on one chunk the smaller ring only slowed it (RTX 5070, 4K prompts: Coder -15%, IQ3_S -5%, IQ3_XXS -2%). int g_ring_budget = 0; int64_t g_ring_small_max = 0; // #136: the fused experts (STRATA_PF_FUSED=1) launch on a batch of a layer's streamed experts at once, so the ring // should hold a whole layer's (~460 of 512 on Q2_0): with 384 slots a layer's last batch waits for slots its own // first batch frees. Measured on the 5070, Q2_0, the 4K / 32K code-agent prompts (one run each): fused at 384 slots // +5% / +3% over MMQ, at 512 +19-22% / +11-12% (MMQ itself at 512: -2% / -1%). P3: with the fused path's smaller // buffers (moe_bufs) 1024 slots - two layers' experts - fit too; 2 pairs each, prompt tok/s at 512 / 1024 slots: 4K // 1,512 / 1,605, 32K 2,485 / 2,660 (both chunk 8192), 128K with KV streaming 2,346 / 2,392 (the chunk falls from // 8192 to 6144, but more experts stay resident). A native pack likewise when the native kernels (moe_fused_iq.hpp) // take any of its layers: IQ2_XS, 4K / 32K, their first version at 384 slots -3% / -6% against MMQ, at 512 +8% / 0%. inline bool fused_ring() { if (const int ov = strata::prefill::pf_fused_override().load(std::memory_order_relaxed); ov == 0 || ov == strata::prefill::kPfFusedKernelsOnly) return false; // the small-card prompt path decided on MMQ's layout if (core::peer_portable()) return false; // multi-GPU: --peer-device keeps the MMQ path and its buffer sizes const strata::kernels::cpu::ExpertLayout& lay = strata::kernels::cpu::expert_layout(); // the Q2_0 pack: fused::enabled(). A native pack: native_supported() alone - it implies enabled() wherever the Q2_0 // kernels exist, and on gfx12 (the native kernels only) enabled() is false while the native layers do run fused. if (!lay.native) return fused::enabled(); // EVERY layer: fused_layout() shrinks the MoE buffers to the fused path's needs, so a layer the native kernels do // not cover (Unsloth UD-IQ4_XS's Q8_0 down projections) would run MMQ in them at the full chunk and overflow them // (garbage, an illegal memory access or a hung prompt on gfx1151). A pack with such a layer keeps MMQ's buffers; // its covered layers still take the fused kernels. // PER DEVICE: native_supported() asks the CURRENT device (the SM count and compute capability of moe_fused_iq.cu's // dev_info()), so on a layer split over unlike cards the answer differs by stage: an 84-SM 5080 takes the fused // kernels at 12.0, a 70-SM 5070 Ti beside it does not. Cached as one global bool, the answer came from whichever // card happened to be current at the first call: on a split whose first stage was the 5080 the MoE buffers were // shrunk for every stage, and each 5070 Ti stage then failed every chunk >= stream_all_min() with "Prefill: the // fused layout's MoE buffers are too small for layer 25's expert path at a chunk of 8192 tokens" (measured // 2026-10-26, 2x 5080 + 2x 5070 Ti, --layer-split 12,25,37 - layer 25 opens the first 5070 Ti stage). Atomic // because the batch-slot threads read this too; a lost race recomputes the same per-device answer. static std::atomic per_dev[32]; // 0 unknown, 1 every layer covered on this device, 2 not int dev = 0; if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 32) { (void) cudaGetLastError(); dev = 0; } const int8_t known = per_dev[dev].load(std::memory_order_relaxed); if (known != 0) return known == 1; bool all = !lay.fmt.empty(); for (const auto& f : lay.fmt) if (!fused::native_supported(f.gu_type, f.d_type)) { all = false; break; } per_dev[dev].store(all ? 1 : 2, std::memory_order_relaxed); return all; } // the largest ring: 512 slots; 1024 with the Q2_0 pack's fused experts (P3's smaller buffers, measured there) - the // native packs' fused layers were measured at 512 inline int ring_cap() { return fused_ring() && !strata::kernels::cpu::expert_layout().native ? RING_MAX : 512; } // The ring's budget in bytes: the measured slot counts above, at the blob size they were measured with. It is // bytes and not slots because a slot is one whole blob and the blob is the pack's - see the note above. inline constexpr uint64_t Q2_0_BLOB = 1382400ull; // the blob of the pack the ring was tuned on inline uint64_t ring_bytes() { const uint64_t slots = fused_ring() ? 1024ull : 384ull; // #136: a fused ring holds two layers' experts return (g_pinned_share >= 0.9 ? slots : slots / 4) * Q2_0_BLOB; } // ...and what that buys on THIS pack, never past ring_cap(): the slot count `init` lays out, and what the auto // chunk scan treats as a full ring. A pack whose blobs are larger than Q2_0's gets fewer slots for the same // bytes, which is the point - the ring competes with the expert cache for the same VRAM. // 0.1.39b: on by default (#583); STRATA_RING_BYTES=0 restores 0.1.39's ring, loan and auto chunk list. It moves the // prompt path's loan on a native pack (fewer ring slots, the rest kept as cache slots, a larger auto chunk), so a long // prompt's experts are read through a different mix of resident and streamed groups and its bits differ from 0.1.39's // (RTX 5070, IQ3_XXS, 32K prompt: +14% to +26%; teacher-forced against the FP16 prompt path in the same band). // #1454: `bo` shares `emb`'s storage (carve), which frees T * N floats per chunk. The planner keeps counting them by // default, so the auto chunk and the borrowed cache slots (and with them the prompt path's bits) are exactly 0.1.40.3's; // STRATA_EMB_REUSE_ACCOUNT=1 lets it use the saved bytes: a larger chunk or more borrowed slots where VRAM is the limit // (RTX 3060, IQ3_XXS: chunk 6400 -> 6656, prompt +3.9%), with other rounding in the prompt path. inline bool emb_reuse_account() { static const bool on = [] { const char* v = std::getenv("STRATA_EMB_REUSE_ACCOUNT"); return v != nullptr && v[0] == '1'; }(); return on; } inline bool ring_bytes_on() { static const bool on = [] { const char* v = std::getenv("STRATA_RING_BYTES"); return v == nullptr || v[0] != '0'; }(); return on; } inline int ring_budget_slots() { const int64_t per = MAXBLOB(); const int64_t n = per > 0 ? (int64_t) (ring_bytes() / (uint64_t) per) : 0; const int cap = ring_cap(); return (int) (n <= 0 ? 0 : (n > cap ? cap : n)); } inline int ring_slots(size_t T) { const char* v = std::getenv("STRATA_PREFILL_RING"); #if defined(STRATA_USE_HIP) // S6: with the matrix-core attention (the gfx12 default since 148-amd; STRATA_HIP_WMMA=1 or auto elsewhere / forced, on a card that // has it) a 96-slot ring: measured with it, 9070 XT 4K prompts 718 -> 1,211 tok/s (16K 1,949 -> 2,032), R9700 4K 2,426 -> 2,483 (16K the same) static const bool wmma = strata::kernels::qsa_prompt_attn_matrix_cores_requested(); if (!v && g_ring_override <= 0 && wmma) { if ((int64_t) T < stream_all_min()) return STAGE; // R9700 (gfx1201) 11-03, IQ3_S fused kernels and UD-Q4_K_XL MMQ: a layer's streamed experts want a deeper ring than 96 at chunks // up to 8K tokens (ring 256 / 192 / 96, 3 rounds x 2 reps interleaved, prompt tok/s: 4K +12.5% / +11.8% / base, 8K +2.4% / +4.4% / // base; UD-Q4 ring 192: 4K +6.9%, 8K +9.5%, 20K +4.0%), while at 16K chunks the extra loan only costs (32K prompt: 256 -0.4%, // 192 -1.3%). The ring is staging depth only; capped at ~800 MiB of blobs (UD-Q4's 3.99 MB blobs: 200 slots). // STRATA_PREFILL_RING=96 restores the old value; so does STRATA_GFX12_RING=0. // gfx12 only: the ring was measured on an R9700. The Strix Halo (gfx1151) also runs the matrix-core attention by default on Windows and keeps 96, as in 0.2.10 // (STRATA_HIP_WMMA=1 on it made the branch above true, and this depth with it). static const bool deeper = [] { const char* e = std::getenv("STRATA_GFX12_RING"); return !(e != nullptr && e[0] == '0') && strata::kernels::qsa_prompt_attn_matrix_cores_gfx12(); }(); if (!deeper || (int64_t) T >= 12288) return 96; const int64_t per = MAXBLOB(); const int cap = per > 0 ? (int) std::max(96, (int64_t) (800u << 20) / per) : 96; return std::min(cap, (int64_t) T <= 6144 ? 256 : 192); } #endif // the byte budget as this pack's slots: 1024 fused / 384 not on Q2_0, fewer on a pack with bigger blobs. The // unpinned arm stays a slot count (96): it was measured where the host copies are the limit, and there the ring // is not what is competing for VRAM. // 0.1.39's ring (1024 fused / 384 pinned, 96 when a large share goes through host copies), and the #583 byte // budget the auto scan chose for chunks past the size 0.1.39's rule would have picked (set_ring_budget) const int pinned_ring = g_pinned_share >= 0.9 ? (fused_ring() ? 1024 : 384) : 96; const bool budget = ring_bytes_on() && g_ring_budget > 0 && (int64_t) T > g_ring_small_max; const int r = v ? std::atoi(v) : g_ring_override > 0 ? g_ring_override : budget ? g_ring_budget : pinned_ring; if (v && r == STAGE) return STAGE; // Explicit opt-in to routed-only staging, including large chunks. const int big = r < 16 ? 16 : r > ring_cap() ? ring_cap() : r; return (int64_t) T >= stream_all_min() ? big : STAGE; } // The staging buffers a layout holds: the ring, and at least STAGE_GRP for the staged walk's grouped gather. Counted // and taken through this one place, so the bytes the prompt path borrows cover every buffer it writes. inline int stage_slots(size_t T) { return std::max(ring_slots(T), STAGE_GRP); } constexpr int DQ = 2; // dequantized-expert ring (FP16 gate/up + down) // The BF16-weight projections (hyper-connection, SSM alpha/beta, indexer, router, shared gate, PLE key/value) take // BF16 activations here and FP32 ones in decode. STRATA_PREFILL_BF16X2=1 adds each activation's BF16 remainder as a // second GEMM (Y = W.hi + W.lo, ~16 mantissa bits): a router that picks its top 10 from the same x decode would. // 2 = all but the hyper-connection's; 1 = the hyper-connection's too (its activations are 10240 wide and its up // projection writes as much: slower); 0 (the default: opt-in, it changes the prompt path's numbers) = off. // `f16_io`: the stage's FP16 prompt path (Prefill::init decides it once per stage, on the stage's own device): FP16 // activations carry 11 mantissa bits, so the BF16 low part does not apply there. inline int bf16x2_mode(bool f16_io) { static const int v = [] { const char* e = std::getenv("STRATA_PREFILL_BF16X2"); return e != nullptr ? std::atoi(e) : 0; }(); return f16_io ? 0 : v; } inline bool bf16x2(bool f16_io) { return bf16x2_mode(f16_io) != 0; } // STRATA_HANDOFF_PIPE=P (opt-in, 2..16; unset/0/1 = the one copy): a layer split hands a chunk's rows on in P row pieces. // The stage records an event when its chunk's work is done and one after each piece's copy into the pinned hand-off // buffer, waits on the host only for the first (what on_stage_chunk's checkpoint parts and the error check need), and // starts the next stage at once: that stage's upload of piece i waits on piece i's event on the GPU, so its uploads // overlap the remaining downloads (separate PCIe links; the 3090s here have no P2P) and the thread start is hidden // behind them. The same bytes land in the same rows before the next stage's first kernel: exact. No device memory; // the events and the existing two pinned buffers per stage only. static constexpr int kHandPiecesMax = 16; static int handoff_pipe_pieces() { static const int v = [] { const char* e = std::getenv("STRATA_HANDOFF_PIPE"); const int p = e ? std::atoi(e) : 0; return p >= 2 ? std::min(p, kHandPiecesMax) : 0; }(); return v; } // rows [r0, r0 + nr) of piece i of a T-row chunk in P pieces (the same split on both sides of a hand-off) static void handoff_piece(int64_t T, int P, int i, int64_t& r0, int64_t& nr) { const int64_t per = (T + P - 1) / P; r0 = std::min(T, (int64_t) i * per); nr = std::min(T, r0 + per) - r0; } // STRATA_HANDOFF_PIPE: the next stage's chunks run on one thread of the stage's own, started once and pinned once // (std::async started a thread per chunk: ~1 ms from the hand-off to the next stage's first call, measured with nsys). // The futures are the same (run_impl's result, or what it threw), so the chain's waits do not change. // (STRATA_HANDOFF_PIPE_WORKER=0: a thread per chunk again, for an A/B of the two halves.) static bool handoff_pipe_worker() { static const bool v = [] { const char* e = std::getenv("STRATA_HANDOFF_PIPE_WORKER"); return !e || std::atoi(e) != 0; }(); return handoff_pipe_pieces() > 0 && v; } struct HandWorker { std::mutex mu; std::condition_variable cv; std::deque> jobs; bool stop = false; std::thread th; HandWorker() { th = std::thread([this] { strata::platform::name_this_thread("strata-hand"); strata::aux_cpus::pin_current_thread(); for (;;) { std::packaged_task job; { std::unique_lock lk(mu); cv.wait(lk, [this] { return stop || !jobs.empty(); }); if (jobs.empty()) return; // stopped, and every job taken has run (no broken promise) job = std::move(jobs.front()); jobs.pop_front(); } job(); } }); } std::future submit(std::packaged_task job) { std::future f = job.get_future(); { std::lock_guard lk(mu); jobs.push_back(std::move(job)); } cv.notify_one(); return f; } ~HandWorker() { { std::lock_guard lk(mu); stop = true; } cv.notify_one(); if (th.joinable()) th.join(); } }; // S23 (opt-in STRATA_HC_UPMIX=1): the hyper-connection read's up projection and gr_mix_r as one kernel (gr_upmix: gfx11, // and CUDA sm_80+ on chunks of 33+ tokens - there, with CUDA 13.3 on an RTX 5090, cuBLAS's up GEMM sums K in the fused // kernel's order and the read is the default pair's bits: gr_upmix_parity, STRATA_HC_UPMIX_CHECK); // STRATA_HC_UPMIX_CHECK=N also runs the default pair on the first N reads and reports the difference of `mixed` static int64_t pf_switch_min_t() { // S23: STRATA_PF_SWITCH_MIN_T=N - the rounding-level prompt switches only on chunks of // N or more tokens (shorter prompts keep the default numerics, and their outputs; the switches pay on long ones) static const int64_t v = [] { const char* e = std::getenv("STRATA_PF_SWITCH_MIN_T"); return e ? (int64_t) std::atoll(e) : (int64_t) 0; }(); return v; } // STRATA_PF_DM16 (gfx11, the F16 fused expert kernels): the down rows go to the combine as FP16 instead of FP32 (half the bytes written and read) inline bool pf_dm16() { static const bool on = [] { const char* e = std::getenv("STRATA_PF_DM16"); return e != nullptr && e[0] == '1'; }(); return on; } inline bool hc_upmix() { static const bool v = [] { const char* e = std::getenv("STRATA_HC_UPMIX"); return e != nullptr && e[0] == '1'; }(); return v; } // CUDA: the fused read only on chunks of 33+ tokens - below that cuBLAS's up GEMM takes another kernel, rounding-level // apart, so short chunks keep the default pair inline int64_t hc_upmix_min_t() { #if defined(__HIPCC__) return 0; #else return 33; #endif } inline bool bf16x2_hc(bool f16_io) { return bf16x2_mode(f16_io) == 1; } // S23 (opt-in STRATA_CVEC_FUSE=1): a steered layer's FFN write + control vector + the next half's norm in one pass // over R (gr_write_cvec_norm_rs; bitwise the gr_write + cvec_apply + gr_norm_rs it replaces) // S23 (opt-in STRATA_PF_HCDOWN=1, on chunks of STRATA_PF_SWITCH_MIN_T+ tokens): the hyper-connection read's down and // inject projections as one WMMA GEMM over xn16 (strata_pf_hcdown_bf16), xn16 written with token stride // 10240 + 64. Rounding-level (another k order than hipBLASLt): quality-gated. inline bool pf_hcdown() { static const bool v = [] { const char* e = std::getenv("STRATA_PF_HCDOWN"); return e && e[0] == '1'; }(); return v; } constexpr int64_t XN_PAD = 64; // S (opt-in STRATA_HCD_EXACT=1, on chunks of STRATA_PF_SWITCH_MIN_T+ tokens): the HC down projection by a WMMA kernel with // hipBLASLt's own k order (bitwise: strata_pf_hcdown_exact_bf16), xn16 written with token stride 10240 + 64 (the inject // projection stays on hipBLASLt, reading that stride) inline bool hcd_exact() { static const bool v = [] { const char* e = std::getenv("STRATA_HCD_EXACT"); return e && e[0] == '1'; }(); return v; } inline bool hc_pad() { return pf_hcdown() || hcd_exact(); } // STRATA_QSA_FUSE_NR (default on, =0 runs rms_rows + rope [+ split_q] as separate kernels): the QSA layer's K, query and // indexer-query norm + rotary in one pass each (strata::prefill::rms_rope_rows; the same bits as the chain it replaces). // STRATA_QSA_FUSE_GATE (default on, =0 keeps the separate gate_attn pass): the attention output gate sigmoid(gate) * O in the // epilogue of the matrix-core prompt attention (qsa_prompt_attn_batch_gated; the same bits as gate_attn). Only where that // kernel runs and the output is not rotated back (kv_rot / hybrid K8V4 rotate it before the gate). inline bool qsa_fuse_gate() { static const bool v = [] { const char* e = std::getenv("STRATA_QSA_FUSE_GATE"); return !(e != nullptr && e[0] == '0'); }(); return v; } inline bool qsa_fuse_nr() { static const bool v = [] { const char* e = std::getenv("STRATA_QSA_FUSE_NR"); return !(e != nullptr && e[0] == '0'); }(); return v; } inline bool cvec_fuse() { static const bool v = [] { const char* e = std::getenv("STRATA_CVEC_FUSE"); return e && e[0] == '1'; }(); return v; } // S23 (opt-in STRATA_PF_PAD=1 with STRATA_PF_GEMM=1): the FP16 activations of the two K 6144 projections (ssm_out's // y_h, attn_output's attn_h) with row stride 6144 + 64, so the GEMM's rows do not camp on the memory channels (the // 4 KB-multiple stride; Gemm::native pads the weight the same way). Same bits. inline bool pf_pad() { static const bool v = [] { const char* p = std::getenv("STRATA_PF_PAD"); const char* g = std::getenv("STRATA_PF_GEMM"); if (!(p && p[0] == '1' && g && g[0] == '1')) return false; // The padded stride pays only for strata_pf_gemm_f16_ld, which is built for the gfx11 matrix cores. On another // card nothing takes a padded X (it used to end the engine: "a padded X ... needs STRATA_PF_PAD's path", #1237): // do not pad, and say so once. if (!strata_pf_padded_gemm_available()) { std::fprintf(stderr, "prefill gemm: STRATA_PF_PAD needs the padded gfx11 product, which this card does not run; " "not padding\n"); return false; } return true; }(); return v; } constexpr int64_t ZV_PAD = 64; // F-1: STRATA_GR_UNFUSED=1 keeps the FP32 copy of the normalized rows (gr_norm + gr_mix), the A/B arm inline bool gr_unfused() { static const bool v = [] { const char* e = std::getenv("STRATA_GR_UNFUSED"); return e && e[0] == '1'; }(); return v; } double ms_since(Clock::time_point t) { return std::chrono::duration(Clock::now() - t).count(); } // STRATA_PF_ALLOC_TRACE=1 (diagnostic, no behaviour change; round 0216): the free-memory figure (cudaMemGetInfo) around the prompt path's // allocations, against what the code asked for, to see what `bytes_needed` does not count (RESULTS-STRIX2.md). inline bool pf_alloc_trace() { static const bool on = [] { const char* e = std::getenv("STRATA_PF_ALLOC_TRACE"); return e != nullptr && e[0] == '1'; }(); return on; } inline int64_t pf_free_mib() { size_t f = 0, t = 0; cudaMemGetInfo(&f, &t); return (int64_t) (f >> 20); } // one line: the free figure now, its drop since the last mark, and what the code itself asked for in between (MiB) inline void pf_mark(const char* what, int64_t& last, uint64_t asked_bytes = 0) { if (!pf_alloc_trace()) return; const int64_t now = pf_free_mib(); std::fprintf(stderr, "prefill alloc trace: %-44s free %7lld MiB, dropped %7lld MiB, asked %7lld MiB\n", what, (long long) now, (long long) (last - now), (long long) (asked_bytes >> 20)); last = now; } // Either cudaMalloc (owned, freed with the object) or a bump allocation from a borrowed region; with no base and // no region it only counts, which is how `bytes_needed` sizes the region. struct Alloc { uint64_t tr_asked = 0, tr_n = 0; ///< STRATA_PF_ALLOC_TRACE: the cudaMalloc takes' sizes, and how far the free figure fell over them int64_t tr_drop = 0; uint8_t* base = nullptr; uint64_t cap = 0, used = 0; uint64_t failed_bytes = 0; ///< the take that could not be allocated; the failure message names it uint64_t granule = 0; ///< count_only: each take rounds up to this (cudaMalloc's 2 MiB pages: owned buffers) bool count_only = false; bool spill = false, spilled = false; std::vector* owned = nullptr; template T* take(size_t n, bool& ok) { uint64_t bytes = ((uint64_t) n * sizeof(T) + 256 + 255) & ~255ull; if (count_only) { if (granule > 0) bytes = (bytes + granule - 1) / granule * granule; used += bytes; return nullptr; } if (base != nullptr && !spilled && used + bytes <= cap) { T* p = (T*) (base + used); used += bytes; return p; } if (base != nullptr && !spill) { ok = false; failed_bytes = bytes; return nullptr; } void* p = nullptr; const int64_t tr0 = pf_alloc_trace() ? pf_free_mib() : 0; if (cudaMalloc(&p, bytes) != cudaSuccess) { ok = false; failed_bytes = bytes; return nullptr; } if (pf_alloc_trace()) { tr_drop += tr0 - pf_free_mib(); tr_asked += bytes; ++tr_n; } spilled = true; owned->push_back(p); used += bytes; return (T*) p; } }; } // namespace // Step 4 of the prompt-speed plan: the experts the arena could not pin (a third of the streamed ones on IQ3_S) are // copied into pinned buffers by these threads, ahead of the launches. Copied in line by the launching thread they // left the GPU without queued work while each ~2 MB memcpy ran (~15 s of a 32K prompt on IQ3_S). Job j - a layer's // j-th unpinned expert, in launch order - lands in host buffer j % kRing, which is free again once the DMA of job // j - kRing (recorded by the launching thread, `issued`) is done. struct Stager { // D-5: the pinned ring's depth (STRATA_STAGER_RING, default 16) - how far the host copies can run ahead of the // DMAs of the unpinned experts' blobs int kRing = 16; bool sleep = stager_sleep(); // blocking waits (the layer-major pair always: they share the CPU with the host thread) // pp-opt: the jobs a thread claims at once (STRATA_STAGER_BATCH; set by init's caller, at most kMaxBatch and kRing) static constexpr int kMaxBatch = 32; int batch = 1; // `from` set: the blob is copied by the source itself (CS-T: a GGUF read in place assembles it from its three // role slices; a pointer to it would not live as long as the queue) struct Job { const uint8_t* src; size_t bytes; core::ExpertSource* from = nullptr; int32_t l = 0, e = 0; }; std::vector buf; std::vector pinned; std::vector> pageable; // the fallback when no more RAM can be pinned 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) std::atomic head{0}; std::atomic issued{0}, active{0}; uint32_t gen = 0; bool quit = false; std::mutex mu; std::condition_variable cv; std::vector threads; int device = 0; bool init(size_t blob_bytes, int nthreads) { if (const char* v = std::getenv("STRATA_STAGER_RING")) kRing = std::clamp(std::atoi(v), 2, 256); buf.assign((size_t) kRing, nullptr); pinned.assign((size_t) kRing, 0); dma_done.assign((size_t) kRing, nullptr); pageable.resize(kRing); for (int i = 0; i < kRing; ++i) { pinned[i] = !force_pageable() && cudaHostAlloc((void**) &buf[i], blob_bytes, cudaHostAllocDefault) == cudaSuccess; if (!pinned[i]) { cudaGetLastError(); pageable[(size_t) i].resize(blob_bytes); buf[i] = pageable[(size_t) i].data(); } if (cudaEventCreateWithFlags(&dma_done[i], sleep ? (cudaEventDisableTiming | cudaEventBlockingSync) : cudaEventDisableTiming) != cudaSuccess) return false; } cudaGetDevice(&device); for (int t = 0; t < nthreads; ++t) threads.emplace_back([this] { // --aux-cpus: the copy threads go to the spare CPUs with the other helpers; STRATA_AUX_STAGER=0 leaves them where // they were created (a small spare set would then hold all of them: it limits the copy rate of a long read) strata::platform::name_this_thread("strata-stage"); static const bool keep = [] { const char* v = std::getenv("STRATA_AUX_STAGER"); return v != nullptr && std::atoi(v) == 0; }(); if (keep) strata::aux_cpus::note_owned_thread(); else strata::aux_cpus::pin_current_thread(); work(); }); return true; } ~Stager() { finish(); { std::lock_guard lk(mu); quit = true; } cv.notify_all(); for (auto& t : threads) t.join(); for (int i = 0; i < kRing; ++i) { if (dma_done[i]) cudaEventDestroy(dma_done[i]); if (buf[i] && pinned[i]) cudaFreeHost(buf[i]); } } void work() { cudaSetDevice(device); uint32_t seen = 0; for (;;) { { std::unique_lock lk(mu); cv.wait(lk, [&] { return quit || gen != seen; }); if (quit) return; seen = gen; } for (;;) { active.fetch_add(1, std::memory_order_acq_rel); // pp-opt: a run of up to `batch` consecutive jobs (batch <= kRing, so the earliest unfinished run // never waits for a buffer of its own) int k = 1; const int j = claim(seen, batch, k); if (j < 0) { active.fetch_sub(1, std::memory_order_acq_rel); break; } for (int i = j; i < j + k; ++i) { // The copies run ahead of the DMAs, so most of these threads spend most of a prompt waiting here: sleep // (atomic wait, and a blocking-sync event below), not a yield spin - 32 spinners took every core // (Linux; see stager_sleep for Windows). if (i >= kRing) { // job i - kRing's DMA from this buffer is queued #if defined(STRATA_ATOMIC_WAIT) if (sleep) for (int x; (x = issued.load(std::memory_order_acquire)) <= i - kRing;) issued.wait(x); 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 // was a ring entry the routing skipped (an event never recorded returns at once) cudaEventSynchronize(dma_done[i % kRing]); } int32_t rl[kMaxBatch], re[kMaxBatch]; uint8_t* rd[kMaxBatch]; 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; rl[nr] = jb.l; re[nr] = jb.e; rd[nr] = buf[i % kRing]; ++nr; } if (nr > 0 && !from->copy_blobs(rl, re, rd, nr)) { std::fprintf(stderr, "prefill: the expert source could not copy experts %d.. of layer %d\n", re[0], rl[0]); std::abort(); } for (int i = j; i < j + k; ++i) ready[(size_t) i].store(1, std::memory_order_release); active.fetch_sub(1, std::memory_order_acq_rel); } } } int claim(uint32_t g, int want, int& k) { uint64_t cur = head.load(std::memory_order_acquire); for (;;) { if ((uint32_t) (cur >> 32) != g) return -1; const int n = (int) ((cur >> 16) & 0xffff), j = (int) (cur & 0xffff); if (j >= n) return -1; k = std::min(want, n - j); if (head.compare_exchange_weak(cur, cur + (uint64_t) k, std::memory_order_acq_rel, std::memory_order_acquire)) return j; } } /// A layer's jobs; the previous layer's are finished (finish()). void start(std::vector&& js) { if (js.empty()) return; std::lock_guard lk(mu); jobs = std::move(js); 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); } 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(); return buf[j % kRing]; } /// The launching thread queued job j's DMA on `copy`: its buffer is free once that is done. void issued_one(int j, cudaStream_t copy) { cudaEventRecord(dma_done[j % kRing], copy); issued.store(j + 1, std::memory_order_release); #if defined(STRATA_ATOMIC_WAIT) issued.notify_all(); #endif } /// No job is running after this (the end of a layer, or an early return in the middle of one). void finish() { head.store((uint64_t) gen << 32, std::memory_order_release); // n = 0: nothing more to claim issued.store(1 << 30, std::memory_order_release); #if defined(STRATA_ATOMIC_WAIT) issued.notify_all(); #endif while (active.load(std::memory_order_acquire) != 0) std::this_thread::yield(); } }; // multi-GPU: the peer GPU's share of a prompt chunk's experts. Per MoE layer the primary copies its normed // activations over P2P, the peer quantizes the rows routed to the experts it holds, runs the same MMQ products the // primary would (gathered from its own slots), and copies the result rows back into the primary's Dm rows - the rows // are laid out local-first, so the peer's are one contiguous block at the end. struct PeerPrefill { core::PeerExperts* peer = nullptr; int dev = -1; int64_t cap_rows = 0, T_max = 0; cudaStream_t s = nullptr; cudaEvent_t ev_in = nullptr; // on the primary: the activations and the row tables are ready cudaEvent_t ev_done = nullptr; // on the peer: its rows have landed in the primary's Dm // multi-GPU: the result rows go back group by group on a second stream while the next group computes cudaStream_t s_out = nullptr; static constexpr int kGrpEv = NE / 16 + 2; cudaEvent_t ev_grp[kGrpEv] = {}; bool out_pending = false; // s_out may still read Dm (the next layer's products wait for it) bool out_pipe = true; // multi-GPU COMPACT: the peer's per-row buffers hold one GROUP's rows instead of the layer's (a group // = up to 16 experts and at most G rows), the result rows double-buffered: ~0.9 GB -> ~0.26 GB on the helper // card at 8192-token chunks, and no row cap (every peer-held expert's rows go to the peer). // STRATA_PF_PEER_COMPACT=0: the layer-sized buffers (the A/B). bool compact = true; int64_t G = 0; void *Xq_g = nullptr, *Hq_g = nullptr; float *GU_g = nullptr, *H_g = nullptr, *Dm_b[2] = {}; cudaEvent_t ev_dm[2] = {}; bool dm_live[2] = {}; std::vector> groups; // multi-GPU PEER STREAMING: a share of the experts the primary would stream over ITS PCIe link in a // big chunk is streamed by the peer over its own link instead (into its own ring) and computed there - it halves // the primary's copy load and moves expert work to the card that idles through most of the MoE half. // STRATA_PF_PEER_STREAM = the share (0 = off), STRATA_PF_PEER_RING = its ring slots. double ps_frac = 0.0; int RP = 0; std::vector pstage; std::vector pcopied, pused; std::vector plive; cudaStream_t s_cp = nullptr; struct PsEntry { int32_t l, e; const uint8_t* blob; }; std::vector pseq; // this chunk's peer-streamed experts, layer by layer in id order std::vector pseq_start; std::vector ps_flag; // [layer * NE + e]: streamed by the peer in this chunk size_t p_issued = 0, pk = 0; int64_t ps_experts = 0; float *mixed = nullptr, *GU = nullptr, *H = nullptr, *Dm = nullptr; void *Xq = nullptr, *Hq = nullptr; int32_t *src = nullptr, *bounds = nullptr, *ident = nullptr; uint8_t *grp_gu = nullptr, *grp_d = nullptr; std::unique_ptr ctx; mmq::Context* run_ctx = nullptr; // the context its products run in: ctx, or a layer split helper stage's // Without P2P (a layer split's helper) the activations and the result rows go through MAPPED pinned host buffers: the device that needs them reads them with a copy kernel. A copy-engine // upload would queue behind the expert blobs that device's streams already hold (up to a ring of them, ~70 us // each - the grouping tables' reason for mapped memory too), and cudaMemcpyPeerAsync would be staged by the // driver. `back_at` / `back_rows`: the rows this layer sends back. bool p2p = true; // The row table and the group bounds go the same way: the primary's are pinned for its own device only (or // pageable), and a copy from them on the peer's stream would hold up the host until that stream is idle. float* host_x = nullptr; // T x N activations (mapped, portable) float* host_rows = nullptr; // the peer's result rows (mapped, portable) int32_t* host_src = nullptr; // the peer's rows' tokens (mapped, portable) int32_t* host_bounds = nullptr; // its group bounds (mapped, portable) size_t host_x_cap = 0, host_rows_cap = 0, host_src_cap = 0; // elements static constexpr size_t kHostBounds = 2 * (NE + NE / 16 + 2) + 64; int64_t back_at = 0, back_rows = 0; // SUMS (the host route's default, STRATA_PF_PEER_SUMS=0: rows): the peer adds its rows into one weighted sum per // token (`sum`, from the routing weights `wk` and each row's routed pair `pair`) and sends that back (`host_sum`, // T x N) instead of its rows (rows_peer x N, up to K times more over the primary's link); the primary's combine // adds it to its own experts' rows (eddoursul/Strata's second-GPU prompt path, f8de703). bool sums = false; cudaStream_t x_stream = nullptr; // primary: the host route's copies beside its compute stream cudaEvent_t ev_x = nullptr; bool f16 = false; // the sums mode's transfers in FP16 (STRATA_PF_PEER_F16=0: FP32) // the sums in one launch per group (STRATA_PF_PEER_GATHER=0: per expert): per group [tokens | starts | rows] at // adds_at[g], built on the host from the rows' pairs, uploaded once per layer bool gather = false; int32_t *adds = nullptr, *host_adds = nullptr; size_t adds_cap = 0; std::vector adds_at; std::vector ntok, tcur, touched; uint16_t *sum16 = nullptr, *host_x16 = nullptr, *host_sum16 = nullptr; float *sum = nullptr, *wk = nullptr; int32_t* pair = nullptr; float *host_w = nullptr, *host_sum = nullptr; int32_t* host_pair = nullptr; std::vector bounds_host; core::PinnedRing up_ring; // #1996: this device's pageable uploads (bounds, routed rows) go up from pinned slots // APR (prism.hadamard): per layer, this device's copy of the down projection's sign vector (nullptr: identity or // not folded). The gate/up input arrives already rotated (the primary sends its rotated copy, m.Xr). std::vector had_sh; std::vector owned; int64_t layers = 0, experts = 0, rows = 0, over_cap = 0; // stats ~PeerPrefill() { if (dev < 0) return; int prev = 0; cudaGetDevice(&prev); cudaSetDevice(dev); if (s) cudaStreamSynchronize(s); ctx.reset(); up_ring.release(); for (void* p : owned) cudaFree(p); if (ev_done) cudaEventDestroy(ev_done); if (s_out) cudaStreamSynchronize(s_out); for (cudaEvent_t e : ev_grp) if (e) cudaEventDestroy(e); for (cudaEvent_t e : ev_dm) if (e) cudaEventDestroy(e); if (s_cp) cudaStreamSynchronize(s_cp); for (cudaEvent_t e : pcopied) if (e) cudaEventDestroy(e); for (cudaEvent_t e : pused) if (e) cudaEventDestroy(e); if (s_cp) cudaStreamDestroy(s_cp); if (s_out) cudaStreamDestroy(s_out); if (s) cudaStreamDestroy(s); cudaSetDevice(prev); if (ev_in) cudaEventDestroy(ev_in); if (x_stream) { cudaStreamSynchronize(x_stream); cudaStreamDestroy(x_stream); } if (ev_x) cudaEventDestroy(ev_x); if (host_x) cudaFreeHost(host_x); if (host_rows) cudaFreeHost(host_rows); if (host_src) cudaFreeHost(host_src); if (host_bounds) cudaFreeHost(host_bounds); if (host_w) cudaFreeHost(host_w); if (host_sum) cudaFreeHost(host_sum); if (host_pair) cudaFreeHost(host_pair); if (host_x16) cudaFreeHost(host_x16); if (host_sum16) cudaFreeHost(host_sum16); if (host_adds) cudaFreeHost(host_adds); } }; // #1712: a chunk's small int32 tables (the QSA step records, the token ids) go up from a pinned buffer. A cudaMemcpyAsync // from pageable memory is staged by the runtime; on gfx1100 the first text prompt after a picture one was seen to read stale // step records that way. The event holds the next upload back until the last one has left the buffer. No pinned RAM: // the copy is made from the caller's memory, as before. struct PinnedUpload { int32_t* p = nullptr; size_t n = 0; cudaEvent_t ev = nullptr; void init(size_t count) { if (p || force_pageable()) return; if (cudaHostAlloc((void**) &p, count * sizeof(int32_t), cudaHostAllocDefault) != cudaSuccess) { cudaGetLastError(); p = nullptr; return; } n = count; if (cudaEventCreateWithFlags(&ev, cudaEventDisableTiming) != cudaSuccess) { cudaGetLastError(); cudaFreeHost(p); p = nullptr; n = 0; ev = nullptr; } } bool upload(void* dst, const int32_t* src, size_t count, cudaStream_t s) { const size_t bytes = count * sizeof(int32_t); if (!p || count > n) return cudaMemcpyAsync(dst, src, bytes, cudaMemcpyHostToDevice, s) == cudaSuccess; if (cudaEventSynchronize(ev) != cudaSuccess) return false; // the previous upload is out of the buffer std::memcpy(p, src, bytes); return cudaMemcpyAsync(dst, p, bytes, cudaMemcpyHostToDevice, s) == cudaSuccess && cudaEventRecord(ev, s) == cudaSuccess; } void release() { if (ev) cudaEventDestroy(ev); if (p) cudaFreeHost(p); p = nullptr; ev = nullptr; n = 0; } }; // 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; core::SessionState* ss = nullptr; core::ExpertSource* src = nullptr; const core::ExpertCache* cache = nullptr; const int32_t* host_res = nullptr; int64_t T = 0, T_max = 0; bool borrowed = false, borrow_spill = false; cudaStream_t cs = nullptr, copy = nullptr; Gemm gemm; std::vector owned; // chunk buffers float *emb = nullptr, *R = nullptr, *xn = nullptr, *lo = nullptr, *gated = nullptr, *inj = nullptr; float* grs = nullptr; // F-1: the hyper-connection read's row scales (T x 4) uint16_t *xn16 = nullptr, *lo16 = nullptr; float* mixed = nullptr; uint16_t *mixed_bf = nullptr, *mixed_h = nullptr; bool f16_io = false; // HIP, STRATA_HIP_PROMPT_F16=1: this stage's 16-bit GEMMs run FP16 in and out (set in init) bool xn_f16 = false; // Turing, BF16 on FP16 tensor cores: xn16 written as the FP16 values the GEMM would convert uint16_t *xn16_lo = nullptr, *lo16_lo = nullptr, *mixed_bf_lo = nullptr; // bf16x2(): the BF16 GEMMs' low parts float* bo = nullptr; // GDN float *qkv = nullptr, *z = nullptr, *ab = nullptr, *gate = nullptr, *beta = nullptr, *hbuf = nullptr, *y = nullptr; uint16_t* y_h = nullptr; // QSA float *Kc = nullptr, *Vc = nullptr, *Qf = nullptr, *q = nullptr, *idx_raw = nullptr, *q_idx = nullptr, *attn = nullptr; uint16_t* attn_h = nullptr; int32_t* steps_dev = nullptr; std::vector steps_host; PinnedUpload steps_up; int32_t* sel_ids = nullptr; float* sel_scores = nullptr; // [sel_batch, max_blocks] int64_t sel_batch = 256, max_blocks = 0; float* attn_scratch = nullptr; int64_t attn_batch = 32, cap = 0; // MoE float *logits = nullptr, *w = nullptr, *GU = nullptr, *Dm = nullptr, *sgate = nullptr, *sup = nullptr, *shared = nullptr, *sg = nullptr; int32_t *ids = nullptr, *slot_dev = nullptr, *src_dev = nullptr; uint16_t *Xs = nullptr, *Hh = nullptr, *sh_h = nullptr; float* Xr = nullptr; // APR: Hadamard-rotated copy of `mixed` for the MMQ path // latent MoE: D/U stay pinned host-side, brought up once per session (m.owned); xl/xlq are the per-token // group-wise D products (xl row = src*G + labels[ids[slot]]), yl/ylq the down latents, tok the routing table bool latent = false; int64_t lat_first_layer = 0; int64_t lat_H = 0, lat_ELL = 0, lat_I = 0, lat_G = 8, lat_E = 0; strata::core::LatentDense lat_d; int lat_fmt = 8; // Q4DU: the dense pack's format (8 Q8_0 / 4 Q4_0) std::vector lat_labels; // [layers * E] group labels, host int32_t* lat_labels_dev = nullptr; // their device copy uint8_t *lat_d_dev = nullptr, *lat_u_dev = nullptr; // per-layer D then U, layer-major on the device float* xl = nullptr; // T * G * ELL floats, the D products uint8_t* xlq = nullptr; // q8_1 rows of xl (T*G rows, ELL/32*36 bytes each) float* yl = nullptr; // T * K * ELL floats, the down latents (A_dn products) uint8_t* ylq = nullptr; // q8_1 rows of yl int32_t* tok = nullptr; // T * K: the xl row of each routed entry int32_t* tok_ix = nullptr; // token-major (s*K+k) -> expert (the plan kernel's ids table) int32_t* ids_p = nullptr; // position p -> expert (U's ids) int32_t* lat_n = nullptr; // device: [0] grouped kernels' group count, [1] latent_plan_rows' entries unsigned long long* lat_gp = nullptr; // the grouped kernel's per-group blob pointers (device) int32_t* lat_gs = nullptr; // its per-group entry starts (device) int32_t* lat_gcd = nullptr; // STRATA_LAT_FUSED_Q3K: the per-group row COUNTS (device) uint8_t* lat_hbuf = nullptr; // ... and the fused A stage's staged H rows, 800 B an entry int32_t* lat_gxl = nullptr; // STRATA_LATENT_MMQ: the xl row of each row of a pending group's buffers int32_t* lat_gdst = nullptr; // ... and the absolute entry each of those rows belongs to (down's dst) uint8_t* lat_xq = nullptr; // T q8_1 rows of `mixed` (D's input activations) uint8_t* lat_scratch = nullptr; // the grouped kernel's scratch (T*K entries) std::vector lat_tokx_host, lat_idsx_host, lat_idsp_host; // the plan tables, T*K each, host // step 2b (MMQ): the activations quantized per layer, H in FP32 and its group's quantized rows, the identity // row map, the group bounds, the group buffers of gathered experts void *Xq = nullptr, *Hq = nullptr; float* H = nullptr; size_t H_bytes = 0; // H's size (STRATA_PF_P1A: FP16 H rows must fit) int32_t *ids_identity = nullptr, *bounds_dev = nullptr; uint8_t *grp_gu = nullptr, *grp_d = nullptr; float *Xscale = nullptr, *Hscale = nullptr, *grp_tail = nullptr; // NVFP4: activation scales, expert tails float* row_sd = nullptr; // NVFP4 in MMQ: each Dm row's s_down, applied by the combine as it reads the row std::vector bounds_host; // STRATA_MMQ_WS=1 (moe_ws.hpp): a layer whose experts are all resident - their byte offsets from the expert cache's // base in MMQ order and the CTAs' chunks, built on the host per layer and uploaded with the bounds (the device // buffers exist only where ws::mode() != 0; the host copies are the unmapped path's) int64_t* xoff_dev = nullptr; std::vector xoff_host; ws::Chunk* ws_chunks = nullptr; std::vector ws_chunks_host; // STRATA_PF_GROUP_SCRATCH: per group, its first entry in sub (-1: the group fits the scratch); a sub-range is // experts [a, b) of `order` and the offset of its relative bounds in bounds_host / bounds_dev struct SubRange { int32_t a, b, rel; }; std::vector sub_first; std::vector sub; int64_t group_cap = 0; std::unique_ptr mmq_ctx; std::vector ids_host, slot_host, src_host, cnt, off; // set_cpu_pool: a small chunk's CPU experts. The layer's MoE input (T x N, from `mixed`), the rows the pool writes // (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, 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) std::vector cpu_jobs; // the measured share: running means of the CPU's ms per expert and the GPU's per streamed expert, and the share // they balance at (cpu_share_env) double cpu_c_ms = 0, cpu_g_ms = 0, cpu_share_now = 0.5; cudaEvent_t cpu_ev[2] = {}; // the GPU's expert work of the last CPU-sharing layer, read once the stream is done bool cpu_pend = false; double pend_cpu_ms = 0; int64_t pend_n_cpu = 0, pend_n_gpu = 0; // ... and whether sharing pays at all (auto): an eligible layer's wall time per non-resident expert, from before // its routing sync to its combine (CUDA events, two sets: a layer's start is recorded before the last one's is // read); two adjacent layers of the two arms give a ratio (with / without), and the median of the last // CPU_RATIOS decides. Layers differ up to 8x in that cost, the same in every run, which is why adjacent layers // are compared rather than a running mean per arm. static constexpr int CPU_RATIOS = 5; int64_t cpu_layers = 0; // the eligible layers so far (the arms' schedule) int64_t cpu_first_l = -1; // the model's first eligible layer: 2x the others' cost in both arms, never read cudaEvent_t cpu_wall[4] = {}; int pend_wall = 0, pend_set = 0, pend_l = 0; // pend_wall 1: the last eligible layer ran without the share, 2: with int64_t pend_wall_n = 0; int last_arm = 0, last_l = 0; double last_w = 0; double cpu_ratio[CPU_RATIOS] = {}; int cpu_nratio = 0; bool cpu_gate = false; // the median ratio is below 1: the layers that share are the cheaper ones // The grouping tables in mapped pinned memory, [ids | slot | src] of T_max * K each, then the MMQ bounds: kernels // read and write them in place. A cudaMemcpyAsync of them queues behind the expert blobs the copy stream already // holds (up to `ring` of them, ~70 us each), and the GPU idles meanwhile - measured 4.2 s of a 128K prompt's // 70 s at the default ring, 0.7 s with a 16-slot one. STRATA_GROUP_COPY=1: the copies (the A/B arm). int32_t* grp_host = nullptr; int32_t* grp_dev = nullptr; // its device alias size_t grp_n = 0, grp_tk = 0; // int32s allocated; T_max * K (the offset of slot, and of src past it) uint16_t* dq_gu[DQ] = {}; uint16_t* dq_d[DQ] = {}; // Few-row experts on the FP16 path (fewrows.hpp, STRATA_PF_FEWROWS): fr_xq: the layer's T rows as q8_1 (the grouped // kernels' activations); fr_rec: one plan record per expert position of the layer; fr_scr: the grouped kernels' scratch. void* fr_xq = nullptr; uint8_t* fr_rec = nullptr; void* fr_scr = nullptr; int64_t fr_xq_rows = 0; // OWN-I8 (STRATA_EXPERT_I8=1, native_expert_i8): kI8RecBytes of device memory - the group tables of one launch (blob pointers, row bounds, the // group count); the layer's tokens as q8_1 rows live in fr_xq (the same rows the few-row path quantizes), its scratch is the group's GU rows uint8_t* i8_rec = nullptr; uint8_t* stage_dev[RING_MAX] = {}; int ring = STAGE; // the slots of this layout's ring (ring_slots) // THE PROMPT ROOM's resolved counts for this layout (carve): the rule's ring, the room's first slot (past the // staging buffers this layout holds), and the lender's slots on top of them. `ring` is `room_at` + `room` while // a roomed prompt runs and `rule` alone otherwise (set_ring_room_active). int ring_rule = 0, ring_room = 0, ring_room_at = 0; // this layout's GU, H and Xq are the fused path's (moe_bufs' `fused`), so a layer that runs MMQ or the FP16 path may // only write stream_all_min() - 1 of its rows into them; fused_layout() keeps the two in step (#583, #954) bool fused_bufs = false; int64_t tail_req = -1; // the MMQ tail (tokens) the fused layout was carved for: -1 = stream_all_min() - 1 (start-up, the way back); relayout's `tail` std::unique_ptr stager; // the unpinned experts' host copies (step 4) std::unique_ptr lm_stager[2]; // STRATA_PREFILL_LM_NEXTFILES: layer-major's two stagers, used in turn 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 // `used` of the last slot gathered with it, recorded once for all of them (a later record only waits longer) int used_of[RING_MAX] = {}; // PLE float* ple_emb = nullptr; std::vector ple_pageable[2]; // the fallback when no more RAM can be pinned float* ple_emb_host[2] = {}; // pinned, double-buffered: the next chunk's rows are read while this cudaEvent_t ple_copied[2] = {}; // one runs; the event marks that buffer's upload done std::vector ple_rows[2]; bool ple_compact[2] = {false, false}; // STRATA_PLE_COMPACT: the pinned buffer holds raw IQ4_NL rows uint8_t* ple_raw_dev = nullptr; // their device copy (cudaMalloc'd, the chunk's 16 rows a token) cudaStream_t ple_copy = nullptr; // STRATA_PLE_COPY_STREAM: the raw rows' upload stream (null: on cs) cudaEvent_t ple_raw_free = nullptr; // cs has dequantized ple_raw_dev: the next chunk's upload may overwrite it float* ple_norm = nullptr; uint8_t* region = nullptr; // the attention/MoE scratch region (idle while the PLE block runs) uint64_t region_bytes = 0; PrefillStats* stats = nullptr; // KV streaming: one layer's whole K/V, staged from the host copy per layer and chunk (identity layout) strata::kernels::KvHostPools stage; int32_t* ident_table = nullptr; cudaStream_t kv_copy = nullptr; cudaEvent_t kv_released = nullptr, kv_ready = nullptr; // layer split: the device, and the hand-off to the next stage (two pinned chunk buffers, used in turn) int device = -1; float* hand[2] = {}; // STRATA_HANDOFF_PIPE: per hand-off buffer, "the chunk's work is done" and one "landed" event per piece int hand_pieces = 0; cudaEvent_t hand_done[2] = {}; cudaEvent_t hand_ev[2][kHandPiecesMax] = {}; std::unique_ptr hand_worker; // STRATA_HANDOFF_PIPE: the next stage's chunks' thread (lazily) // C-4: the chunk's token ids on the device, for one batched embedding gather int32_t* tok_dev = nullptr; std::vector tok_host; PinnedUpload tok_up; core::PinnedRing up_ring; // #1996: every other upload of the prompt path whose host side is pageable // APR (prism.hadamard): per layer, this device's copies of the gate/up (n_embd) and down (n_ff) sign vectors // (nullptr: identity signs or not folded), resolved once in init so a prompt chunk reads a pointer std::vector had_sx, had_sh; std::unique_ptr pp; // multi-GPU: the peer GPU's expert share (set_peer) // 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) uint8_t* xa = nullptr; // the FFN half's int8 rows per token (fused::quantize_act_native), when a layer's groups run fused int32_t *ids = nullptr, *tab = nullptr; uint8_t *lat_xlq = nullptr, *lat_ylq = nullptr; void* lat_host = nullptr; // mapped compressed rows in host-residual mode int32_t *lat_tok = nullptr, *lat_slot = nullptr; float* lat_sg = nullptr; uint8_t* lat_tabs = nullptr; // every A batch's grouping tables of one layer (one upload a layer) size_t lat_tabs_n = 0; // STRATA_PREFILL_LM_NEXT: the next layer's RAM-tier experts, copied while this layer finishes uint8_t* pre = nullptr; int64_t pre_n = 0, pre_layer = -1; std::vector pre_of; // expert -> its slot in `pre` for pre_layer (-1: not prefetched) cudaEvent_t pre_done = nullptr, pre_free = 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 { // the staging pool of a streamed session: every page of one layer (same sequence in init and bytes_needed) // STRATA_KV_STAGE_OWN (A/B only): the staging pool gets its own allocation instead of borrowed expert slots, so a // streamed run lends the prompt path exactly the slots a resident one does (a lent expert runs on the CPU, which // rounds differently: without this an A/B compares two expert placements as well as two KV placements) bool stage_own() { static const bool v = std::getenv("STRATA_KV_STAGE_OWN") != nullptr; return v; } void take_stage(Alloc& o_borrowed, const core::SessionState& ss, const strata::kernels::QsaShapes& s, strata::kernels::KvHostPools& st, bool& ok) { const core::QsaState& q0 = ss.qsa_states[ss.qsa_primary()]; if (q0.kv_mode != 1) return; if (stage_own() && o_borrowed.count_only) return; Alloc own; own.owned = o_borrowed.owned; Alloc& o = stage_own() ? own : o_borrowed; const size_t rows = (size_t) q0.n_pages * s.n_head_kv * s.page_size; if (q0.kv_hybrid) { // K8V4: the three runs of kKvHybrid; pools_of() then reads as the hybrid pools (mode 3) st.k_q = o.take(rows * s.head_dim, ok); st.k_scale = o.take(rows * (s.head_dim / 64), ok); st.v_q4 = o.take(rows * strata::kernels::kv_q4_bytes_per_head((int) s.head_dim), ok); } else if (q0.kv_q4) { st.k_q4 = o.take(rows * strata::kernels::kv_q4_bytes_per_head((int) s.head_dim), ok); st.v_q4 = o.take(rows * strata::kernels::kv_q4_bytes_per_head((int) s.head_dim), ok); } else if (q0.kv_int8) { st.k_q = o.take(rows * s.head_dim, ok); st.v_q = o.take(rows * s.head_dim, ok); st.k_scale = o.take(rows * (s.head_dim / 64), ok); st.v_scale = o.take(rows * (s.head_dim / 64), ok); } else { st.k_pool = o.take(rows * s.head_dim, ok); st.v_pool = o.take(rows * s.head_dim, ok); } } strata::kernels::QsaAttnPools pools_of(const strata::kernels::KvHostPools& h, const int32_t* table) { strata::kernels::QsaAttnPools p; p.k_pool = h.k_pool; p.v_pool = h.v_pool; p.k_q = h.k_q; p.v_q = h.v_q; p.k_scale = h.k_scale; p.v_scale = h.v_scale; p.k_q4 = h.k_q4; p.v_q4 = h.v_q4; p.page_table = table; return p; } } // namespace Prefill::Prefill() : impl_(new Impl) {} Prefill::~Prefill() { clear_taps(); release(); } bool Prefill::reserve_taps(const std::vector& layers, int64_t cap_rows, int64_t n_embd, std::string& err) { #if defined(STRATA_HAVE_DFLASH2) tap_layers_ = layers; tap_cap_ = cap_rows; if (tap_dev_ != nullptr) return true; size_t mine = 0; // a layer split's stage keeps the taps of the layers it runs (the others arrive from the other stages) for (int l : layers) if (l >= stage_lb_ && (stage_le_ < 0 || l < stage_le_)) ++mine; if (mine == 0) return true; if (cudaMalloc((void**) &tap_dev_, (size_t) cap_rows * mine * (size_t) n_embd * 4) != cudaSuccess) { (void) cudaGetLastError(); tap_dev_ = nullptr; err = "the prompt staging of the DFlash2 taps does not fit in VRAM"; return false; } return true; #else (void) layers; (void) cap_rows; (void) n_embd; err = "no DFlash2 in this build"; return false; #endif } void Prefill::clear_taps() { if (tap_dev_ != nullptr) { cudaFree(tap_dev_); tap_dev_ = nullptr; } tap_layers_.clear(); on_taps = nullptr; } void Prefill::reset() { release(); impl_.reset(new Impl); stats_ = PrefillStats{}; } void Prefill::release() { if (!impl_) return; impl_->hand_worker.reset(); // idle here (run drains the chain before it returns); joins its thread if (impl_->cs) cudaStreamSynchronize(impl_->cs); if (impl_->copy) cudaStreamSynchronize(impl_->copy); if (impl_->kv_copy) cudaStreamSynchronize(impl_->kv_copy); if (impl_->ple_copy) (void) cudaStreamSynchronize(impl_->ple_copy); for (int i = 0; i < RING_MAX; ++i) { if (impl_->copied[i]) cudaEventDestroy(impl_->copied[i]); if (impl_->used[i]) cudaEventDestroy(impl_->used[i]); } for (int b = 0; b < 2; ++b) { if (impl_->hand[b]) cudaFreeHost(impl_->hand[b]); if (impl_->hand_done[b]) cudaEventDestroy(impl_->hand_done[b]); for (cudaEvent_t& e : impl_->hand_ev[b]) if (e) cudaEventDestroy(e); if (impl_->ple_copied[b]) cudaEventDestroy(impl_->ple_copied[b]); if (impl_->ple_emb_host[b] && impl_->ple_pageable[b].empty()) cudaFreeHost(impl_->ple_emb_host[b]); if (b == 0 && impl_->ple_raw_dev) { (void) cudaFree(impl_->ple_raw_dev); impl_->ple_raw_dev = nullptr; } } if (impl_->copy) cudaStreamDestroy(impl_->copy); if (impl_->fr_xq) cudaFree(impl_->fr_xq); if (impl_->fr_rec) cudaFree(impl_->fr_rec); if (impl_->fr_scr) cudaFree(impl_->fr_scr); if (impl_->i8_rec) cudaFree(impl_->i8_rec); if (impl_->kv_copy) cudaStreamDestroy(impl_->kv_copy); if (impl_->ple_copy) (void) cudaStreamDestroy(impl_->ple_copy); if (impl_->ple_raw_free) (void) cudaEventDestroy(impl_->ple_raw_free); 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.lat_host) cudaFreeHost(impl_->lm.lat_host); 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(); for (float* p : {impl_->cpu_x, impl_->cpu_rows, impl_->cpu_ones}) if (p) cudaFreeHost(p); for (cudaEvent_t e : impl_->cpu_ev) if (e) cudaEventDestroy(e); for (cudaEvent_t e : impl_->cpu_wall) if (e) cudaEventDestroy(e); for (void* p : impl_->owned) cudaFree(p); } namespace { constexpr int64_t GEMM_SCRATCH = 32ll << 20; // FP16 elements for the largest dequantized dense weight constexpr size_t GEMM_WS = 32u << 20; // cuBLAS workspace // THE ATTENTION HALF AND THE MoE HALF SHARE THEIR BUFFERS. A layer runs its attention (GDN or QSA), writes it back // into the residual, and only then its MoE, so the three sets of scratch are never live at once: one region the size // of the largest holds them all. That is ~260 KB of the ~680 KB a prompt token cost - which is what lets a chunk // grow (every expert is streamed once per chunk, so a bigger chunk streams fewer bytes per token). The sizes are // counted with the same `take` sequence `init` uses; a mismatch makes `init` fail with "do not fit", never overlap. uint64_t gdn_set_bytes(size_t T) { Alloc a; a.count_only = true; bool ok = true; a.take(T * C, ok); a.take(T * ZV, ok); a.take(T * 2 * HV, ok); a.take(T * HV, ok); a.take(T * HV, ok); a.take(T * C, ok); a.take(T * ZV, ok); a.take(T * (ZV + (pf_pad() ? ZV_PAD : 0)), ok); return a.used; } uint64_t qsa_set_bytes(size_t T, int64_t cap, int64_t max_blocks, int64_t sel_batch, int64_t attn_batch, const strata::kernels::QsaShapes& s) { Alloc a; a.count_only = true; bool ok = true; a.take(T * kKvW, ok); a.take(T * kKvW, ok); a.take(T * kQFull, ok); a.take(T * kQW, ok); a.take(T * kIdxK, ok); a.take(T * kIdxQW, ok); a.take(T * kQW, ok); a.take(T * (ZV + (pf_pad() ? ZV_PAD : 0)), ok); a.take(T * (size_t) cap, ok); a.take((size_t) sel_batch * (size_t) max_blocks, ok); a.take((size_t) attn_batch * strata::kernels::qsa_decode_attn_scratch_floats(cap, s), ok); return a.used; } // Step 2b: which layers' experts go through MMQ (both weight types covered; the Strata Q2_0 pack always - its blob // is converted to GGUF Q2_0 blocks on the gather), whether any layer keeps the FP16 path (IQ1_M), and the largest // gate/up and down matrices a group buffer slot holds. STRATA_PREFILL_MMQ=0: the FP16 path everywhere (the A/B). constexpr int MMQ_GROUP = 16; // experts per MMQ launch (the gather is per expert, as blobs arrive) // Latent A batches (Edge0): up to this many experts a launch; the latent tables (lat_gp/lat_gs/lat_gcd) and, for a latent pack, // the group buffers are sized for it. constexpr int LAT_GROUP = 64; // MMQ reads up to one 256-value tile past a matrix's last row when the row length is not a multiple of it (the down // product: 640 values). Those bytes meet zero activations, which is harmless only if they decode to finite numbers - // llama.cpp zero-pads after every tensor, and so does a group buffer: this many zeroed bytes follow its last expert. constexpr size_t MMQ_TAIL = 4096; // OWN-I8: the int8 kernels' groups are collected by i8_group.hpp (its own group size, no MMQ); both sizes are the same number here static_assert(MMQ_GROUP == strata::prefill::i8grp::kGroupMax, "the int8 expert group and the MMQ group have the same size"); #if defined(STRATA_PREFILL_MMQ) static_assert(strata::prefill::i8grp::kGroupMax == strata::prefill::mmq::kGatherGroupMax, "the int8 expert group and MMQ's gather group have the same size"); #endif // STRATA_I8_COUNT=1 (diagnostic, off by default, no effect on any result): one stderr line per prompt chunk saying how many of the routed (token, expert) // entries of the layers that reached the MMQ group walk ran on native_expert_i8 and how many stayed on the walk's own kernels (MMQ, or the FP16 path). bool i8_count_on() { static const bool on = [] { const char* v = std::getenv("STRATA_I8_COUNT"); return v != nullptr && std::atoi(v) != 0; }(); return on; } struct I8Count { int64_t layers = 0, layers_i8 = 0, ent = 0, ent_i8 = 0, groups_i8 = 0, min_avg = INT64_MAX, layers_few = 0, ent_few = 0; }; I8Count& i8_count() { static I8Count c; return c; } // The MMQ row table (`ids_identity`, T*K rows) is read past its end by llama.cpp's stream-k fixup, which loads a whole // J-column tile of ids before it compares the tile with the expert's rows (see mmq_ids_margin.hpp: the margin follows the // backend's tile bound). An expert takes at most one row per token, so max_rows <= T. Without the slack the table is an // exact-sized cudaMalloc that can end a 2 MiB page, and the over-read faults at the next one (Xid 31 FAULT_PDE, "prefill // copy_i32: an illegal memory access" on a long prompt). The one size used by carve, bytes_needed_impl and the three // places that write the identity (bind_stage_helper, run_impl, lm_run). 0: the size overflows (carve fails). size_t mmq_ids_rows(size_t T) { if (T > (size_t) INT64_MAX / (size_t) K) return 0; return (size_t) mmq::mmq_ids_table_rows((uint64_t) (T * (size_t) K), (uint64_t) T); } struct MmqPlan { bool any = false, fallback = true; std::vector layer; // per layer: MMQ std::vector wmma_only; // per layer: in the MMQ grouping for the WMMA tiles only (pf_wmma: IQ1_M) std::vector fo; // per layer: no MMQ here but the native fused kernels cover its formats // (gfx11: UD-Q4_K_XL's Q4_K / Q5_K and Q5_1 / Q8_0 experts, STRATA_PF_FUSED=1) size_t gu_max = 0, d_max = 0; // STRATA_MMQ_DIRECT=1: per layer, whether resident experts are read in their cache slots (pointer-table MMQ), // and the bytes the down product reads past its matrix there (zeroed in the slot's guard before each group) std::vector direct; std::vector d_over; // STRATA_MMQ_GROUP_SORT=1 (without STRATA_MMQ_RESIDENT_SORT_NE): per MMQ layer, whether both products' 16-expert // launches run whole tiles on every visible GPU (mmq::whole_tiles), so its full groups may be taken in row-count order std::vector group_sort; }; // Volta (sm_70): MMQ runs on dp4a there (no int8 tensor cores). pf_wmma(): the layers MMQ would take, and the IQ1_M // ones the FP16 path would, run as one launch per expert group on FP16 tensor-core tiles instead // (strata::kernels::gemm_iq_f16_grouped: each weight superblock dequantized once into shared memory by the FP16 path's // own formulas, FP16 activations, FP32 sums). OPT-IN: STRATA_PF_WMMA=1 (the activations are FP16 rather than MMQ's // q8_1, so the answers differ from the default). Only when EVERY visible CUDA card is compute capability 7.0 or // newer: the tiles need tensor cores, and a Volta + Pascal rig (one process, one decision) keeps MMQ. bool pf_wmma() { #if defined(__HIPCC__) || defined(STRATA_PF_OWN_ONLY) // (P4-SPLIT: its layers ride MMQ's grouping) return false; #else static const bool on = [] { const char* e = std::getenv("STRATA_PF_WMMA"); if (e == nullptr || e[0] != '1') return false; int n = 0; if (cudaGetDeviceCount(&n) != cudaSuccess || n <= 0) return false; for (int d = 0; d < n; ++d) { int major = 0; if (cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, d) != cudaSuccess || major < 7) return false; } return true; }(); return on; #endif } // Tesla P100 (GP100, compute capability 6.0; the STRATA_EXPERIMENTAL_SM60 build): the prompt experts take the FP16 path // by default instead of MMQ, with the few-row experts (fewrows.hpp) on the quantized grouped kernels. GP100 has no // __dp4a (MMQ runs on the bit-exact emulation in dp4a.hpp), and with the few-row route the FP16 path is at 0.95 / 0.91 / // 0.93 / 1.00 of MMQ's prompt speed at 300 / 1,000 / 2K / 4K and above it from 8K (lane P4-FALLBACK, 11-11: // phaseA-tests/par/RESULTS-P4-FALLBACK.md). Only when EVERY visible CUDA card is cc 6.0 (one process, one decision), // and not with what needs MMQ: STRATA_MMQ_WS=1 (its weight-stationary kernels are MMQ's products), STRATA_EXPERT_NIBBLE=1 // (the FP16 path reads GGUF blocks) and --peer-device (the prompt peer computes on MMQ only). An explicit // STRATA_PREFILL_MMQ keeps its old meaning. The rule: gp100_prompt_default.hpp (gp100_prompt_default_test); // kGp100Default there is the one line that turns it on. bool gp100_fp16_default(const char* env) { #if defined(__HIPCC__) || !defined(STRATA_EXPERIMENTAL_SM60) (void) env; return false; #else if (!kGp100Default || env != nullptr) return false; static const bool on = [] { Gp100PromptInputs in; in.mmq_ws = ws::mode() != 0; in.nibble = strata::core::nibble::config().on; in.peer_device = peer_device_requested().load(); in.all_cc60 = true; int n = 0; if (cudaGetDeviceCount(&n) != cudaSuccess || n <= 0) { cudaGetLastError(); in.all_cc60 = false; } for (int d = 0; in.all_cc60 && d < n; ++d) { int major = 0, minor = 0; if (cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, d) != cudaSuccess || cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, d) != cudaSuccess) { cudaGetLastError(); in.all_cc60 = false; } else if (major != 6 || minor != 0) { in.all_cc60 = false; } } return gp100_fp16_rule(in); }(); return on; #endif } const MmqPlan& mmq_plan() { static const MmqPlan plan = [] { MmqPlan p; const auto& lay = strata::kernels::cpu::expert_layout(); const char* env = std::getenv("STRATA_PREFILL_MMQ"); #if defined(STRATA_PF_OWN_ONLY) const bool gp100 = false; // no MMQ in this build: the FP16 path is not a default of the P100 rule here const bool on = fused::built() && (env == nullptr || std::atoi(env) != 0); // P4-SPLIT: no MMQ tile anywhere, the fused-only layers below #else const bool gp100 = mmq::built() && gp100_fp16_default(env); const bool on = mmq::built() && !gp100 && (env == nullptr || std::atoi(env) != 0); #endif const int64_t layers = lay.native ? (int64_t) lay.fmt.size() : lay.n_layers; p.layer.assign((size_t) std::max(layers, 0), 0); p.fo.assign(p.layer.size(), 0); p.wmma_only.assign(p.layer.size(), 0); p.fallback = !on || layers <= 0; std::map mf_layers; // STRATA_MANIFEST: "path|reason|formats" -> layers (filled only when it is on) const bool mf = strata::core::manifest::enabled(); auto mf_note = [&](const char* path, const std::string& why, int gt, int dt) { if (mf) ++mf_layers[std::string(path) + "|" + why + "|gate_up " + strata::ggml_type_name((uint32_t) gt) + ", down " + strata::ggml_type_name((uint32_t) dt)]; }; for (int64_t l = 0; on && l < layers; ++l) { const int gt = lay.native ? lay.fmt[(size_t) l].gu_type : 42, dt = lay.native ? lay.fmt[(size_t) l].d_type : 42; // #420: a tile on every GPU for these shapes (gate+up: 1280 rows, down: N rows), else the FP16 path if (!mmq::fits(gt, kFF2) || !mmq::fits(dt, N)) { // pf_wmma: a format dq_dispatch takes (IQ1_M, an IQ4_NL down) rides the MMQ grouping, computed by the WMMA tiles only if (pf_wmma() && lay.native && (dt == 42 || dt == 20) && strata::kernels::iq_row_bytes(gt, N) > 0) { p.layer[(size_t) l] = 1; p.wmma_only[(size_t) l] = 1; p.any = true; p.gu_max = std::max(p.gu_max, mmq::matrix_bytes(gt, kFF2, N)); p.d_max = std::max(p.d_max, mmq::matrix_bytes(dt, N, kFF)); mf_note("mmq-grouping, FP16 WMMA tiles", "MMQ has no tile for these formats; STRATA_PF_WMMA=1 computes them on FP16 tensor cores", gt, dt); continue; } p.fallback = true; // the fused path's buffers (Xq, H) exist for it; a chunk it does not take (a small one) keeps the FP16 path if (on && lay.native && fused::native_supported(gt, dt)) { p.fo[(size_t) l] = 1; p.any = true; } #if defined(STRATA_PF_OWN_ONLY) if (on && !lay.native && fused::enabled()) { p.fo[(size_t) l] = 1; p.any = true; } // the Strata Q2_0 pack (moe_fused.cu) #endif if (mf) { const bool fo = p.fo[(size_t) l] != 0; const std::string why = std::string(!mmq::fits(gt, kFF2) ? "no MMQ tile for the gate/up format on every visible GPU" : "no MMQ tile for the down format on every visible GPU") + (fo ? "; the native fused kernels take it" : "; the dequantize + FP16 GEMM path takes it"); mf_note(fo ? "fused-native" : "fp16-gemm", why, gt, dt); } continue; } p.layer[(size_t) l] = 1; p.any = true; p.gu_max = std::max(p.gu_max, mmq::matrix_bytes(gt, kFF2, N)); p.d_max = std::max(p.d_max, mmq::matrix_bytes(dt, N, kFF)); mf_note("mmq", "an MMQ tile exists for both products on every visible GPU", gt, dt); } if (mf) { if (!on) { strata::core::manifest::instance().path("prefill", "experts", false, !mmq::built() ? "this build has no MMQ (ggml sources were not available)" : gp100 ? "Tesla P100 (compute capability 6.0): the FP16 path is the default there" : "STRATA_PREFILL_MMQ=0", "the expert products take the FP16 path"); } else if (layers <= 0) { strata::core::manifest::instance().path("prefill", "experts", false, "no expert layout loaded", ""); } for (const auto& [k, n] : mf_layers) { const size_t a = k.find('|'), b = k.find('|', a + 1); const std::string kind = k.substr(0, a); strata::core::manifest::instance().path("prefill", "experts: " + k.substr(b + 1), kind == "mmq" || kind[0] == 'm', k.substr(a + 1, b - a - 1), kind + ", " + std::to_string(n) + " layers"); } } #if defined(STRATA_PF_OWN_ONLY) if (layers > 0) { // The owner approved this wording (11-13). One line per process (the plan is made once): the layer counts per path, from the rule functions that pick the path. const bool peer_off = strata::prefill::peer_keeps_mmq(strata::core::peer_portable(), kPfOwnOnlyBuild); // the walk's own condition (no_peer): false in this build const bool entry_on = staged_fo_on(); // the staged group entry for a fused-only layer below the floor (staged_fo_rule.hpp) strata::prefill::OwnOnlySplit big, small; // at or above the long-prompt floor, below it const bool i8_gated = strata::kernels::native_expert_i8_floor_gated(); // P100 class: the int8 default is for chunks under stream_all_min() only, the FP16 path from it up for (int64_t l = 0; l < layers; ++l) { const bool fo = p.fo[(size_t) l] != 0 && !peer_off; bool i8 = false; // the int8 expert kernels take this layer's formats (the class rule or STRATA_EXPERT_I8, i8_wants_identity's test) bool unfolded = true; // no Hadamard fold and no tail: the int8 kernels read neither (i8_l: !had_gu && !had_d && !tail_off) bool staged_ok = true; // the staged entry: no fold; a tail only where the fused kernels take the pair (fz_staged: they apply the tail's scales inside) if (lay.native) { const auto& f = lay.fmt[(size_t) l]; unfolded = !f.tail_off && !f.had_gu && !f.had_d; staged_ok = !f.had_gu && !f.had_d && (!f.tail_off || fo); if (unfolded) i8 = strata::kernels::native_expert_i8_prompt(strata::kernels::native_expert_layout(f.gu_type, f.d_type, f.n_embd, f.n_ff), (int64_t) 1 << 20); } strata::prefill::own_only_count_layer(big, small, fo, i8, i8_gated, entry_on, staged_ok); } { // printed once the share is armed, with the floor in force then (own_only_line_flush) std::lock_guard lk(g_own_line_mu); g_own_line.plan(layers, big, small, peer_off); } own_only_line_flush(); std::string sw; for (const std::string& k : own_only_switches()) sw += (sw.empty() ? "" : " ") + k; if (mf) strata::core::manifest::instance().path("prefill", "own-only-build", true, "this engine was built without llama.cpp's MMQ prompt kernels", "switches on by default here (a switch you set is kept): " + (sw.empty() ? std::string("none") : sw)); } #endif // STRATA_MMQ_DIRECT=1: a native layer's resident experts are read in place when the pointer-table MMQ covers // both products, gate/up reads nothing past its matrix, the down over-read fits the slot guard, and the blob // is exactly [gate | up | down] in MMQ's matrix sizes (the bytes a gather would copy) p.direct.assign(p.layer.size(), 0); p.d_over.assign(p.layer.size(), 0); if (on && lay.native && mmq::direct_mode() == 1) { int64_t nd = 0; for (int64_t l = 0; l < layers; ++l) { if (!p.layer[(size_t) l]) continue; const auto& f = lay.fmt[(size_t) l]; const int gt = f.gu_type, dt = f.d_type; if (!mmq::tab_fits(gt, kFF2) || !mmq::tab_fits(dt, N)) continue; if (mmq::overread_bytes(gt, N) != 0 || mmq::overread_bytes(dt, kFF) > mmq::kSlotGuard) continue; if (f.up_off != mmq::matrix_bytes(gt, kFF2, N) / 2 || f.down_off != 2 * f.up_off) continue; if (mmq::matrix_bytes(dt, N, kFF) != f.bytes - f.down_off) continue; p.direct[(size_t) l] = 1; p.d_over[(size_t) l] = (uint32_t) mmq::overread_bytes(dt, kFF); ++nd; } std::fprintf(stderr, "strata: prompt MMQ reads resident experts in place (STRATA_MMQ_DIRECT=1): %lld of %lld " "layers\n", (long long) nd, (long long) layers); strata::core::manifest::instance().path("prefill", "mmq-direct", nd > 0, nd > 0 ? "STRATA_MMQ_DIRECT=1" : "STRATA_MMQ_DIRECT=1, but no layer is eligible", std::to_string(nd) + " of " + std::to_string(layers) + " layers read resident experts in place"); } // the verdict depends only on the layer's formats and the visible GPUs: taken once here, not per chunk p.group_sort.assign(p.layer.size(), 0); if (on && detail::mmq_group_sort_requested() && !detail::mmq_resident_sort_requested()) { int64_t ns = 0; for (int64_t l = 0; l < layers; ++l) { if (!p.layer[(size_t) l]) continue; const int gt = lay.native ? lay.fmt[(size_t) l].gu_type : 42, dt = lay.native ? lay.fmt[(size_t) l].d_type : 42; p.group_sort[(size_t) l] = mmq::whole_tiles(gt, kFF2, MMQ_GROUP) && mmq::whole_tiles(dt, N, MMQ_GROUP); ns += p.group_sort[(size_t) l]; } std::fprintf(stderr, "strata: prompt MMQ sorts the full expert groups (STRATA_MMQ_GROUP_SORT=1): %lld of %lld " "layers\n", (long long) ns, (long long) layers); strata::core::manifest::instance().path("prefill", "mmq-group-sort", ns > 0, ns > 0 ? "STRATA_MMQ_GROUP_SORT=1" : "STRATA_MMQ_GROUP_SORT=1, but whole tiles are not guaranteed on every GPU", std::to_string(ns) + " of " + std::to_string(layers) + " layers"); } return p; }(); return plan; } // the FP16 buffers (Xs, Hh): the FP16 path's, or pf_wmma's activations and down input bool fp16_bufs(const MmqPlan& mp) { return mp.fallback || pf_wmma(); } // OWN-I8: does a layer of this layout take the int8 expert groups on this device (the class rule or STRATA_EXPERT_I8=1, and the formats)? A build without MMQ // has no MMQ buffers when no layer takes the fused kernels (mmq_plan().any is false); the int8 groups need one row table of theirs (the identity ids_identity), // which carve and bytes_needed then take on their own. bool i8_wants_identity() { static const bool on = [] { const auto& lay = strata::kernels::cpu::expert_layout(); if (!lay.native) return false; for (const auto& f : lay.fmt) { if (f.tail_off || f.had_gu || f.had_d) continue; const strata::kernels::NativeExpertLayout L = strata::kernels::native_expert_layout(f.gu_type, f.d_type, f.n_embd, f.n_ff); if (strata::kernels::native_expert_i8_prompt(L, (int64_t) 1 << 20)) return true; } return false; }(); return on; } // STRATA_PF_GROUP_SCRATCH=1 (opt-in): on a layout whose every layer takes MMQ (no fused experts, no FP16 fallback or pf_wmma), the // gate/up output, its SwiGLU and its q8_1 copy (GU, H, Hq) hold one expert group's rows instead of the whole chunk's: // a group's products read them and are done with them before the next group starts, so the products and the bits are // the ones of the chunk-sized buffers. ~84 KB a token less, which lets a bigger chunk fit in the slots the prompt // path borrows (fewer chunks: every streamed expert crosses PCIe fewer times per token). A group whose rows pass the // cap runs as sub-ranges of its experts (one expert holds at most T rows: a token routes to an expert once). bool pf_group_scratch() { static const bool on = [] { const char* v = std::getenv("STRATA_PF_GROUP_SCRATCH"); return v != nullptr && std::atoi(v) != 0; }(); if (!on) return false; // decided once, with a line saying why when it stays off (recommend, never force: the switch is ignored, the prompt path is as before) static const bool ok = [] { const MmqPlan& mp = mmq_plan(); const char* why = nullptr; if (!mp.any || fp16_bufs(mp)) why = "the prompt path runs the FP16 path or pf_wmma here, not MMQ"; // the FP16 path's or pf_wmma's buffers else for (char c : mp.layer) if (!c) { why = "a layer does not take MMQ"; break; } if (why == nullptr) for (char c : mp.direct) if (c) { why = "STRATA_MMQ_DIRECT is on for a layer (its guard tables are per whole group)"; break; } // per whole group if (why != nullptr) std::fprintf(stderr, "strata prefill: STRATA_PF_GROUP_SCRATCH=1 is ignored: %s%s", why, "\n"); return why == nullptr; }(); return ok; } // one MMQ product range: experts [a, b) of the layer's order, and its relative bounds' offset in the bounds table struct MoeRange { int32_t a, b, rel; }; // the rows one group's scratch holds: T (a single expert's most) plus three times a group's average int64_t group_cap_rows(size_t T, int64_t n_expert) { // STRATA_PF_GROUP_CAP= (a test knob, at least T): a smaller cap, so that most groups run as sub-ranges static const int64_t forced = [] { const char* v = std::getenv("STRATA_PF_GROUP_CAP"); return v ? (int64_t) std::atoll(v) : 0; }(); if (forced > 0) return std::max(forced, (int64_t) T); const int64_t avg = (int64_t) T * K * MMQ_GROUP / std::max(n_expert, 1); return ((int64_t) T + 3 * avg + 63) / 64 * 64; } // the bounds table's int32s: the absolute bounds, one relative table per group and, with STRATA_PF_GROUP_SCRATCH, the // relative tables of the sub-ranges of the groups that pass the cap (at most one per expert, each with an end entry) size_t bounds_ints(int64_t n_expert) { return (size_t) (2 * (n_expert + n_expert / MMQ_GROUP + 2)) + (pf_group_scratch() ? (size_t) (3 * n_expert + 64) : 0); } // STRATA_PF_HC_REGION=1 (opt-in): the hyper-connection read's scratch (xn16, lo, lo16, gated and the BF16x2 low halves) in the // region the attention half and the MoE half share, as a fourth set: a half's read is done with it before that half's // set is written, and the fused write at a half's end (gr_write_norm_rs: the next half's xn16) runs after the half's last // region reader on the same stream. The same kernels on the same values: the same bits. ~63 KB a token outside the // region less (the region already holds more than this set). bool pf_hc_region() { static const bool on = [] { const char* v = std::getenv("STRATA_PF_HC_REGION"); return v != nullptr && std::atoi(v) != 0; }(); return on && !gr_unfused(); } uint64_t hc_set_bytes(size_t T, bool f16_io) { Alloc a; a.count_only = true; bool ok = true; a.take(T * (D + (hc_pad() ? XN_PAD : 0)), ok); a.take(T * LR, ok); a.take(T * LR, ok); a.take(T * D, ok); if (bf16x2_hc(f16_io)) { a.take(T * D, ok); a.take(T * LR, ok); } return a.used; } // #136 P3: a layout whose chunks run the fused experts (STRATA_PF_FUSED=1, the Q2_0 pack, a streamed chunk of // stream_all_min() tokens or more). Its GU, H and Xq hold only the fused path's grouping tables, int8 H and per-token // int8 activations, and Hq nothing: ~100 KB a token less than MMQ's FP32 GU / H and per-slot q8_1 rows, which is what // lets a bigger chunk or ring fit in the slots the prompt path borrows. A last chunk below stream_all_min() still runs // MMQ in the same buffers, so each keeps MMQ's size for stream_all_min() - 1 tokens. `src`: the layout streams experts // (Prefill::init got an ExpertSource; without one no chunk takes the streamed walk, so no chunk is fused). bool fused_layout(size_t T, bool src) { // STRATA_LATENT_MMQ: the latent A stage's MMQ reads every entry row of the full chunk, so the fused path's // small-chunk buffers (stream_all_min()-1 tokens) must not be chosen - the backstop at the layer loop would // otherwise be the only thing standing between this and an out-of-bounds Xq/Hq write. static const bool lat_mmq = [] { const char* v = std::getenv("STRATA_LATENT_MMQ"); return v != nullptr && v[0] == '1'; }(); if (lat_mmq) return false; return src && fused_ring() && mmq_plan().any && ring_slots(T) > STAGE && (int64_t) T >= stream_all_min(); } // M4: STRATA_LAT_FUSED_Q3K=1 - the latent A stage on the q3k pack's own fused kernels (lat_q3k_fused: gate/up + // SwiGLU + the q8_1 of H + down in two launches, each expert read where it already is, so no gather_native and no // per-expert launch). Read once: moe_set_bytes, the carve and the layer loop must agree on it. Off by default. bool lat_fused_q3k_on() { static const bool on = [] { const char* v = std::getenv("STRATA_LAT_FUSED_Q3K"); return v != nullptr && v[0] == '1'; }(); return on; } // The MoE buffers MMQ and the fused path share: GU and H in floats, Xq and Hq in bytes. Without `fused` (the // default): MMQ's, for T tokens. // STRATA_PF_P1A=1: once, at the last layer of the first fused prompt chunk, which layers' experts took the P1A kernels (by type pair) template void p1a_report(const std::vector& fmt, size_t l, bool took) { static std::vector seen; // per layer: -1 not on the fused path yet, 0 today's kernels, 1 P1A static bool said = false; if (said || fmt.empty()) return; if (seen.empty()) seen.assign(fmt.size(), -1); if (l < seen.size() && seen[l] < 0) seen[l] = took ? 1 : 0; if (l + 1 != fmt.size()) return; said = true; const auto name = [](int t) -> std::string { switch (t) { case 7: return "Q5_1"; case 8: return "Q8_0"; case 12: return "Q4_K"; case 13: return "Q5_K"; default: return "type " + std::to_string(t); } }; std::map on, off; int n_on = 0, n_off = 0, n_unseen = 0; for (size_t i = 0; i < fmt.size(); ++i) { const std::string pair = name(fmt[i].gu_type) + "/" + name(fmt[i].d_type); if (seen[i] == 1) { ++on[pair]; ++n_on; } else if (seen[i] == 0) { ++off[pair]; ++n_off; } else ++n_unseen; } const auto list = [](const std::map& m) { std::string s; for (const auto& kv : m) s += (s.empty() ? "" : ", ") + kv.first + " " + std::to_string(kv.second); return s.empty() ? std::string("none") : s; }; if (n_on == 0) return; // a pack or chunk with no P1A layer: no line std::fprintf(stderr, "strata: prompt experts P1A (STRATA_PF_P1A=1): gate/up + down on the P1A kernels in %d of %zu layers (%s); " "today's kernels in %d (%s); %d not on the fused path in this chunk\n", n_on, fmt.size(), list(on).c_str(), n_off, list(off).c_str(), n_unseen); } // STRATA_PF_P1A (gfx1151): some layer of the loaded native pack takes the P1A kernels; only then does H grow to FP16 rows, so a // pack with no such layer sizes exactly what it did before. The expert layout is loaded before the first plan (generate.cpp's // main: expert_layout_load ahead of every bytes_needed and Prefill::init); debug builds check that every sizing sees one answer. // A smaller H than the P1A kernels need is never written past: experts_native takes them only when the FP16 rows fit in the // H that Prefill::init carved (H_bytes), and runs today's kernels for that call otherwise. bool p1a_pack() { bool any = false; if (fused::p1a_wanted()) { const strata::kernels::cpu::ExpertLayout& lay = strata::kernels::cpu::expert_layout(); for (size_t l = 0; lay.native && l < lay.fmt.size() && !any; ++l) any = fused::p1a_takes_pair(lay.fmt[l].gu_type, lay.fmt[l].d_type); } #ifndef NDEBUG static std::atomic first{-1}; // the first sizing's answer; a later different one = a plan made before the layout was loaded int want = -1; if (!first.compare_exchange_strong(want, (int) any)) assert(want == (int) any); #endif return any; } struct MoeBufs { size_t gu, h, xq, hq; }; // `tail` (>= 0, fused only): the MMQ rows a chunk below the floor may bring, in tokens (a lent layout sized for its // segment's own last chunk, loan_rules.hpp); -1: stream_all_min() - 1, as the start-up layout. MoeBufs moe_bufs(size_t T, int64_t n_expert, bool fused, int64_t tail = -1) { if (!fused && pf_group_scratch()) { // GU, H, Hq: one group's rows (Xq keeps every row: gate/up reads the layer's) const size_t gc = (size_t) group_cap_rows(T, n_expert); return {gc * kFF2, gc * kFF, mmq::q8_bytes((int64_t) (T * K), N), mmq::q8_bytes((int64_t) gc, kFF)}; } if (!fused) return {T * K * kFF2, T * K * kFF, mmq::q8_bytes((int64_t) (T * K), N), mmq::q8_bytes((int64_t) (T * K), kFF)}; const int64_t tail_tokens = tail >= 0 ? std::min(tail, std::max(stream_all_min() - 1, 0)) : stream_all_min() - 1; const size_t ts = (size_t) std::min((int64_t) T, tail_tokens); // MMQ's last small chunk // STRATA_PF_P1A=1 (gfx1151 only; on by default on Windows gfx1151) and a pack with P1A layers: H holds FP16 rows (n_ff halves a routed row) const size_t h_p1a = p1a_pack() ? (T * K * kFF * 2 + 3) / 4 : 0; return {std::max(ts * K * kFF2, (fused::group_bytes((int64_t) (T * K), (int) n_expert) + 3) / 4), std::max(std::max(ts * K * kFF, (fused::act_bytes((int64_t) (T * K), kFF) + 3) / 4), h_p1a), std::max(mmq::q8_bytes((int64_t) (ts * K), N), fused::act_bytes((int64_t) T, N)), mmq::q8_bytes((int64_t) (ts * K), kFF)}; } uint64_t moe_set_bytes(size_t T, int64_t n_expert, bool fused, int64_t tail = -1) { const MmqPlan& mp = mmq_plan(); const MoeBufs mb = moe_bufs(T, n_expert, fused, tail); Alloc a; a.count_only = true; bool ok = true; a.take(T * n_expert, ok); a.take(T * K, ok); a.take(T * K, ok); a.take(T * K, ok); a.take(T * K, ok); // the FP16 path's Xs / Hh: in a fused layout the tail below the stream floor only (fp16_rows_rule.hpp), like GU / H / Hq above const size_t Tf = (size_t) strata::prefill::fp16_buf_tokens((int64_t) T, fused, stream_all_min()); if (fp16_bufs(mp)) a.take(Tf * K * N, ok); a.take(mb.gu, ok); if (fp16_bufs(mp)) a.take(Tf * K * kFF, ok); a.take(T * K * N, ok); a.take(T * kFF, ok); a.take(T * kFF, ok); a.take(T * kFF, ok); a.take(T * N, ok); a.take(T, ok); if (mp.any) { a.take(mb.xq, ok); a.take(mb.h, ok); a.take(mb.hq, ok); a.take(T * K, ok); a.take(T * K, ok); a.take(MMQ_GROUP * 4, ok); a.take(T * K, ok); // APR: the rotated copy of `mixed` for the MMQ path (carve's m.Xr) - it must be counted here too, or the MoE // set outgrows the region `carve` sizes from this, and `init` fails with "do not fit" at any chunk if (strata::kernels::cpu::hadamard_spec().any()) a.take(T * N, ok); } { // latent MoE: xl/xlq/yl/ylq/tok (carve's takes, same order; region-sized for both arm layouts) const auto& lay0 = strata::kernels::cpu::expert_layout(); if (lay0.native && !lay0.fmt.empty() && lay0.fmt[0].n_embd != (int64_t) N) { const int64_t ELL = lay0.fmt[0].n_embd; const int64_t G = 8; a.take(T * G * ELL, ok); a.take(T * G * (ELL / 32 * 36), ok); a.take(T * K * ELL, ok); a.take(T * K * (ELL / 32 * 36), ok); a.take(T * K, ok); a.take(T * K, ok); a.take(T * K, ok); a.take(T * (N / 32) * 36, ok); a.take(2, ok); a.take(strata::kernels::native_expert_scratch_bytes((int64_t) (T * K), lay0.fmt[0].n_ff), ok); a.take(LAT_GROUP, ok); a.take(LAT_GROUP + 1, ok); a.take(LAT_GROUP, ok); // M4: lat_gcd // M4: the fused A stage's staged H rows (n_ff / 32 = 20 q8_1 blocks = 800 B an entry), only when on if (lat_fused_q3k_on()) a.take(T * K * 800, ok); a.take(T * K, ok); // STRATA_LATENT_MMQ: lat_gxl / lat_gdst (always taken: the region must a.take(T * K, ok); // not depend on an env var the carve might see differently) } } return a.used; } } // The first BF16 and the first FP16 GEMM of a process each pay a one-time library / code-object load in hipBLAS (1.2 s and // 1.0 s on an R9700, hipBLASLt 1.2.0: STRATA_COLD_TRACE), which the first prompt then carries (a 700-token prompt: 5.9 s, // 3.5 s of it these). One tiny product of each type, on a handle and stream of its own, moves that cost out of the first // request; generate.cpp starts it on a thread before the expert arena loads, so it costs no start-up time, and // Prefill::init waits for it. The outputs go to scratch buffers of their own, so no later result changes. // STRATA_PREFILL_WARM=0 turns it off. void warm_gemm_libraries(int device) { static const bool off = [] { const char* e = std::getenv("STRATA_PREFILL_WARM"); return e != nullptr && e[0] == '0'; }(); if (off) return; #if defined(STRATA_USE_HIP) // #3204: not when the process sees cards of more than one architecture (gfx1151 + gfx1201: prompts -25%); see // warm_policy.hpp. STRATA_PREFILL_WARM=1 asks for it anyway. static const bool arch_ok = [] { std::vector archs; int n = 0; if (cudaGetDeviceCount(&n) != cudaSuccess) { (void) cudaGetLastError(); return true; } for (int i = 0; i < n && i < 32; ++i) { cudaDeviceProp p{}; if (cudaGetDeviceProperties(&p, i) == cudaSuccess) archs.emplace_back(p.gcnArchName); else (void) cudaGetLastError(); } const bool wanted = warm_gemm_wanted(archs, std::getenv("STRATA_PREFILL_WARM")); if (!wanted) std::fprintf(stderr, "strata prefill: no hipBLAS warm-up: the cards are of more than one architecture " "(it slows prompts there, #3204); the first prompt loads the libraries (STRATA_PREFILL_WARM=1 asks for it)\n"); return wanted; }(); if (!arch_ok) return; static std::mutex mu; static unsigned done_mask = 0; // per device: each card loads its own code objects if (device < 0 || device >= 32) return; std::lock_guard lk(mu); if (done_mask & (1u << device)) return; done_mask |= 1u << device; int prev = 0; if (cudaGetDevice(&prev) != cudaSuccess) { (void) cudaGetLastError(); return; } if (prev != device && cudaSetDevice(device) != cudaSuccess) { (void) cudaGetLastError(); return; } const auto t0 = std::chrono::steady_clock::now(); constexpr int64_t T = 64, Nn = 64, K = 320; uint16_t *x = nullptr, *w = nullptr; float* y = nullptr; cudaStream_t st = nullptr; bool ok = cudaStreamCreate(&st) == cudaSuccess && cudaMalloc((void**) &x, (size_t) T * K * 2) == cudaSuccess && cudaMalloc((void**) &w, (size_t) Nn * K * 2) == cudaSuccess && cudaMalloc((void**) &y, (size_t) T * Nn * 4) == cudaSuccess; if (ok) { Gemm gm; std::string err; ok = gm.init((void*) st, T * K, err); if (ok) { cudaMemsetAsync(x, 0, (size_t) T * K * 2, st); cudaMemsetAsync(w, 0, (size_t) Nn * K * 2, st); gm.bf16(x, w, y, T, Nn, K); gm.f16(x, w, y, T, Nn, K); cudaStreamSynchronize(st); } } (void) cudaGetLastError(); if (x) cudaFree(x); if (w) cudaFree(w); if (y) cudaFree(y); if (st) cudaStreamDestroy(st); if (prev != device) cudaSetDevice(prev); if (ok) std::fprintf(stderr, "strata prefill: warmed the BF16 and FP16 GEMM libraries on GPU %d in %.0f ms (STRATA_PREFILL_WARM=0 skips)\n", device, std::chrono::duration(std::chrono::steady_clock::now() - t0).count()); #else (void) device; #endif } bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, core::SessionState& ss, core::ExpertSource* src, const core::ExpertCache* cache, const int32_t* host_res, int64_t chunk, void* stream, std::string& err, void* borrow, uint64_t borrow_bytes, bool borrow_spill) { Impl& m = *impl_; int64_t tr_last = pf_alloc_trace() ? pf_free_mib() : 0; pf_mark("init: start (chunk, then the sign tables)", tr_last); #if defined(STRATA_GPU_METAL) (void) wt; (void) src; (void) cache; (void) host_res; (void) stream; (void) borrow; (void) borrow_bytes; m.T = chunk > 0 ? chunk : 1; return strata::metal_engine::window_ensure(g, ss, ss.qsa_states[ss.qsa_primary()].max_cells, err); #endif m.wt = &wt; m.g = &g; m.ss = &ss; m.src = src; m.cache = cache; m.host_res = host_res; m.T = chunk; m.cs = (cudaStream_t) stream; m.stats = &stats_; if (g.n_embd != N || g.hc != HC || g.hc_lr != LR || g.n_expert < 1 || ss.k != K) { err = "prefill: geometry differs from the artifact's"; return false; } { // a native pack's experts: this path's expert buffers and products (and so their Hadamard rotations) are // N wide in and 640 wide in the middle; the sign vectors are looked up at the layout's own widths const strata::kernels::cpu::ExpertLayout& lay = strata::kernels::cpu::expert_layout(); m.latent = lay.native && !lay.fmt.empty() && lay.fmt[0].n_embd != (int64_t) N; if (m.latent) { // latent MoE: stride-N attention, ELL-wide expert rows (the A part of W1 = A @ D.T) m.lat_H = (int64_t) N; m.lat_ELL = lay.fmt[0].n_embd; m.lat_I = lay.fmt[0].n_ff; m.lat_G = 8; m.lat_E = lay.n_expert; for (size_t l = 1; l < lay.fmt.size(); ++l) if (lay.fmt[l].n_embd != m.lat_ELL || lay.fmt[l].n_ff != m.lat_I) { err = "prefill latent: layer " + std::to_string(l) + "'s experts differ from layer 0's"; return false; } const char* dense = std::getenv("STRATA_LATENT_DENSE"); if (dense == nullptr || dense[0] == 0) { err = "prefill latent: the latent layout needs STRATA_LATENT_DENSE="; return false; } if (!m.lat_d.open(dense, g.n_layers, m.lat_G, m.lat_H, m.lat_ELL, err)) { err = "prefill latent dense: " + err; return false; } m.lat_fmt = m.lat_d.fmt(); // Q4DU: every D/U kernel call below derives from it const char* apack = std::getenv("STRATA_LATENT_A"); if (apack == nullptr || apack[0] == 0) { err = "prefill latent: the latent layout needs STRATA_LATENT_A="; return false; } { // labels.bin beside the A pack: one int32 group label per (layer, expert), rows of E std::string lp = apack; const size_t slash = lp.find_last_of('/'); lp = slash == std::string::npos ? std::string("labels.bin") : lp.substr(0, slash + 1) + "labels.bin"; std::FILE* lf = std::fopen(lp.c_str(), "rb"); if (lf == nullptr) { err = "prefill latent: " + lp + " missing"; return false; } m.lat_labels.resize((size_t) g.n_layers * (size_t) m.lat_E); const size_t got = std::fread(m.lat_labels.data(), sizeof(int32_t), m.lat_labels.size(), lf); std::fclose(lf); if (got != m.lat_labels.size()) { err = "prefill latent: " + lp + " is too small"; return false; } for (const int32_t v : m.lat_labels) if (v < 0 || v >= m.lat_G) { err = "prefill latent: bad group label " + std::to_string(v); return false; } } } for (size_t l = 0; lay.native && !m.latent && l < lay.fmt.size(); ++l) if (lay.fmt[l].n_embd != N || lay.fmt[l].n_ff != kFF) { err = "prefill: layer " + std::to_string(l) + "'s experts are " + std::to_string(lay.fmt[l].n_embd) + " x " + std::to_string(lay.fmt[l].n_ff) + ", not the artifact's 2560 x 640"; return false; } // APR: the sign vectors on this device, once (hadamard_device_signs takes a mutex and, the first time per // device, cudaMallocs its copy): run_impl reads m.had_sx / m.had_sh m.had_sx.assign(lay.native ? lay.fmt.size() : 0, nullptr); m.had_sh.assign(m.had_sx.size(), nullptr); for (size_t l = 0; l < m.had_sx.size(); ++l) { if (lay.fmt[l].had_gu) m.had_sx[l] = strata::kernels::hadamard_device_signs(lay.fmt[l].gu_signs, (int) lay.fmt[l].n_embd); if (lay.fmt[l].had_d) m.had_sh[l] = strata::kernels::hadamard_device_signs(lay.fmt[l].d_signs, (int) lay.fmt[l].n_ff); } } cudaGetDevice(&m.device); if (stage_le_ < 0) stage_le_ = g.n_layers; if (stage_lb_ < 0 || stage_lb_ >= stage_le_ || stage_le_ > g.n_layers || (stage_le_ < g.n_layers) != (next_ != nullptr)) { err = "prefill: the stage's layer range is wrong"; return false; } // the CPU share's staged-chunk limit, before the ring is sized below (a caller that sizes loans first armed it) arm_cpu_share(cpu_pool_ != nullptr); for (int b = 0; next_ != nullptr && b < 2; ++b) if (!m.hand[b] && cudaHostAlloc((void**) &m.hand[b], (size_t) chunk * D * 4, cudaHostAllocPortable) != cudaSuccess) { err = "prefill: the layer split's hand-off buffers"; return false; } if (next_ != nullptr && handoff_pipe_pieces() > 0 && m.hand_pieces == 0) { // events only: no device memory bool ok = true; auto make = [](cudaEvent_t& e) { return e != nullptr || cudaEventCreateWithFlags(&e, cudaEventDisableTiming) == cudaSuccess; }; for (int b = 0; b < 2; ++b) { // (a retry after a failed init keeps the events it made) ok = ok && make(m.hand_done[b]); for (int i = 0; ok && i < handoff_pipe_pieces(); ++i) ok = make(m.hand_ev[b][i]); } if (!ok) { err = "prefill: the layer split's hand-off events (STRATA_HANDOFF_PIPE)"; return false; } m.hand_pieces = handoff_pipe_pieces(); } if (m.tok_dev == nullptr) { if (const cudaError_t e = cudaMalloc((void**) &m.tok_dev, (size_t) chunk * sizeof(int32_t)); e != cudaSuccess) { err = std::string("prefill: the token id buffer (") + cudaGetErrorString(e) + ")"; return false; } m.owned.push_back(m.tok_dev); m.tok_host.resize((size_t) chunk); m.tok_up.init((size_t) chunk); } if (m.latent && m.lat_labels_dev == nullptr) { // the group labels, once per session const size_t lb = m.lat_labels.size() * sizeof(int32_t); if (cudaMalloc((void**) &m.lat_labels_dev, lb) != cudaSuccess || cudaMemcpy(m.lat_labels_dev, m.lat_labels.data(), lb, cudaMemcpyHostToDevice) != cudaSuccess) { (void) cudaGetLastError(); err = "prefill latent: the labels device buffer could not be had"; return false; } m.owned.push_back(m.lat_labels_dev); } if (m.latent && m.lat_d_dev == nullptr) { // a session-shared pair when present (latent_share.hpp), else owned below m.lat_d_dev = strata::core::latent_shared_d(); m.lat_u_dev = strata::core::latent_shared_u(); if (m.lat_d_dev != nullptr) { const auto& shared = strata::core::latent_shared(); m.lat_first_layer = shared.first_layer; const int64_t end_layer = shared.end_layer < 0 ? g.n_layers : shared.end_layer; if (m.lat_first_layer > stage_lb_ || end_layer < stage_le_) { err = "prefill latent: shared D/U range does not cover this stage"; return false; } } } if (m.latent && m.lat_d_dev == nullptr) { // no shared pair: one owned copy, uploaded once per session const uint64_t db = (uint64_t) g.n_layers * m.lat_d.d_bytes(); const uint64_t ub = (uint64_t) g.n_layers * m.lat_d.u_bytes(); if (cudaMalloc((void**) &m.lat_d_dev, db) != cudaSuccess || cudaMalloc((void**) &m.lat_u_dev, ub) != cudaSuccess) { const cudaError_t ce = cudaGetLastError(); if (m.lat_d_dev) { cudaFree(m.lat_d_dev); m.lat_d_dev = nullptr; } if (m.lat_u_dev) { cudaFree(m.lat_u_dev); m.lat_u_dev = nullptr; } err = "prefill latent: the D/U device buffers could not be had (" + std::string(ce != cudaSuccess ? cudaGetErrorString(ce) : "ok") + ", bytes=" + std::to_string(db + ub) + ")"; return false; } bool up = true; for (int64_t l = 0; up && l < g.n_layers; ++l) { if (cudaMemcpy(m.lat_d_dev + (size_t) l * m.lat_d.d_bytes(), m.lat_d.d(l), m.lat_d.d_bytes(), cudaMemcpyHostToDevice) != cudaSuccess || cudaMemcpy(m.lat_u_dev + (size_t) l * m.lat_d.u_bytes(), m.lat_d.u(l), m.lat_d.u_bytes(), cudaMemcpyHostToDevice) != cudaSuccess) up = false; } m.owned.push_back(m.lat_d_dev); // pushed only after both mallocs held; the failure path freed them above m.owned.push_back(m.lat_u_dev); if (!up) { err = "prefill latent: the D/U upload failed"; return false; } } if (cudaStreamCreateWithFlags(&m.copy, cudaStreamNonBlocking) != cudaSuccess) { err = "prefill: copy stream"; return false; } const size_t T = (size_t) chunk; m.T_max = chunk; m.borrowed = borrow != nullptr; m.borrow_spill = m.borrowed && borrow_spill; bool ok = true; // one-time: events, the stager, the host buffers (for the largest chunk), the identity page table for (int i = 0; i < ring_cap(); ++i) { if (cudaEventCreateWithFlags(&m.copied[i], cudaEventDisableTiming) != cudaSuccess) ok = false; if (cudaEventCreateWithFlags(&m.used[i], cudaEventDisableTiming) != cudaSuccess) ok = false; } if (!m.stager) { m.stager = std::make_unique(); const int hw = (int) std::thread::hardware_concurrency(); const char* stv = std::getenv("STRATA_STAGER_THREADS"); // D-5: the host copy threads of unpinned blobs // A GGUF read in place (UD-Q4_K_XL beyond its RAM budget): most of a chunk's blobs are page faults on the // SSD, so the copies need many reads in flight - 32 threads and a 128-deep ring read a 4K chunk in 29 s // instead of 71 s on an RTX 5070 / NVMe PC (4 threads, 16 deep: the defaults, kept for every other source) bool files = false; for (int64_t l = 0; src != nullptr && !files && l < g.n_layers; ++l) for (int64_t e = 0; !files && e < g.n_expert; ++e) files = src->transient(l, e); // #1353: ... but a source answers transient() for every blob it copies out of the GGUF, including the ones whose // pages are already in the page cache (a Q8 pack on a big-RAM box with a warm cache). Those copies are memcpy // from RAM, where 32 threads and a 128-deep ring ran 4-7x slower than the defaults. So ask the OS: the SSD // profile only when fewer than 90% of a sample of the blobs' pages are resident (-1: it cannot tell, keep it). if (files && !stv) { const char* ev = std::getenv("STRATA_STAGER_SSD"); // 1 / 0: force the profile (A/B, support) if (ev != nullptr && ev[0] != '\0') files = ev[0] != '0'; else if (const double warm = src->cached_share(256); warm >= 0.9) { files = false; std::fprintf(stderr, "prefill: the GGUF experts are %.0f%% in the page cache; the stager takes the RAM profile\n", warm * 100.0); } } const int threads = stv ? std::clamp(std::atoi(stv), 1, 32) : files ? 32 : std::max(2, std::min(4, hw / 4)); if (files && std::getenv("STRATA_STAGER_RING") == nullptr) m.stager->kRing = 4 * threads; // pp-opt: with files, each thread claims runs of neighbouring blobs and reads them as one request (copy_blobs); // STRATA_STAGER_BATCH=1 is the A/B arm (one blob per request, as before) { const char* bv = std::getenv("STRATA_STAGER_BATCH"); const int want = bv ? std::atoi(bv) : files ? 8 : 1; m.stager->batch = std::clamp(want, 1, Stager::kMaxBatch); } if (!m.stager->init((size_t) MAXBLOB(), threads)) ok = false; m.stager->batch = std::min(m.stager->batch, m.stager->kRing); } pf_mark("init: stager ring (pinned host buffers)", tr_last, (uint64_t) m.stager->kRing * (uint64_t) MAXBLOB()); // STRATA_PREFILL_LM_NEXTFILES=: layer-major's two stagers on the last stage (the one that reads a short prompt // whole), created here so no prompt pays for their pinned rings; // STRATA_PREFILL_LM_NEXTFILES_THREADS (default 8) each, sleeping waits if (const char* nf = std::getenv("STRATA_PREFILL_LM_NEXTFILES"); m.latent && next_ == nullptr && nf != nullptr && std::atoll(nf) > 0) { const char* tv = std::getenv("STRATA_PREFILL_LM_NEXTFILES_THREADS"); const int nthreads = tv ? std::clamp(std::atoi(tv), 1, 32) : 8; for (int k = 0; k < 2; ++k) { if (m.lm_stager[k]) continue; m.lm_stager[k] = std::make_unique(); m.lm_stager[k]->kRing = (int) std::clamp(std::atoll(nf), 2, 1024); m.lm_stager[k]->batch = m.stager->batch; m.lm_stager[k]->sleep = true; if (!m.lm_stager[k]->init((size_t) MAXBLOB(), nthreads)) { m.lm_stager[k].reset(); continue; } m.lm_stager[k]->batch = std::min(m.lm_stager[k]->batch, m.lm_stager[k]->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); if (m.latent) { m.lat_tokx_host.resize(T * K); m.lat_idsx_host.resize(T * K); m.lat_idsp_host.resize(T * K); } { // the routing tables, the MMQ group bounds (bounds_ints: with STRATA_PF_GROUP_SCRATCH, the sub-ranges' tables too); // STRATA_MMQ_WS: the in-place offsets (n_expert int64) and the weight-stationary layers' chunks (4 int32 each, // 16-byte aligned) const size_t need = 3 * T * K + bounds_ints(m.g->n_expert) + (ws::mode() != 0 ? 2 * (size_t) m.g->n_expert + 4 * ws::max_chunks((int64_t) T * K, m.g->n_expert) + 3 : 0); const char* gc = std::getenv("STRATA_GROUP_COPY"); if (m.grp_n < need && !(gc && gc[0] == '1')) { if (m.grp_host) cudaFreeHost(m.grp_host); m.grp_host = m.grp_dev = nullptr; m.grp_n = m.grp_tk = 0; void *h = nullptr, *d = nullptr; if (!force_pageable() && cudaHostAlloc(&h, need * 4, cudaHostAllocMapped | (core::peer_portable() ? cudaHostAllocPortable : 0)) == cudaSuccess && cudaHostGetDevicePointer(&d, h, 0) == cudaSuccess) { m.grp_host = (int32_t*) h; m.grp_dev = (int32_t*) d; m.grp_n = need; m.grp_tk = T * K; } else { // the copies, as before if (h) cudaFreeHost(h); cudaGetLastError(); } } } pf_mark("init: routing tables (pinned, mapped)", tr_last); for (int b = 0; b < 2; ++b) { if (!m.ple_emb_host[b] && (force_pageable() || cudaHostAlloc((void**) &m.ple_emb_host[b], (size_t) T * N * 4, cudaHostAllocDefault) != cudaSuccess)) { cudaGetLastError(); m.ple_pageable[b].resize(T * N); // pageable: the upload is staged before it returns m.ple_emb_host[b] = m.ple_pageable[b].data(); } if (!m.ple_copied[b] && cudaEventCreateWithFlags(&m.ple_copied[b], cudaEventDisableTiming) != cudaSuccess) ok = false; m.ple_rows[b].resize(T * strata::kernels::PLE_N_HEADS); if (ple_compact_on() && m.ple_raw_dev == nullptr && cudaMalloc((void**) &m.ple_raw_dev, (size_t) T * strata::kernels::PLE_N_HEADS * strata::kernels::PLE_ROW_BYTES) != cudaSuccess) { m.ple_raw_dev = nullptr; (void) cudaGetLastError(); } } // STRATA_PLE_COPY_STREAM: a stream of its own, not the expert copy stream - that one may hold a ring of blobs and // waits on their slots' release, so a wait on it could idle the compute stream for longer than the DMA itself if (ple_copy_stream_on() && m.ple_raw_dev != nullptr && ((!m.ple_copy && cudaStreamCreateWithFlags(&m.ple_copy, cudaStreamNonBlocking) != cudaSuccess) || (!m.ple_raw_free && cudaEventCreateWithFlags(&m.ple_raw_free, cudaEventDisableTiming) != cudaSuccess))) { // an optional optimization: the raw rows go up on the compute stream as with STRATA_PLE_COMPACT alone if (m.ple_copy) { (void) cudaStreamDestroy(m.ple_copy); m.ple_copy = nullptr; } (void) cudaGetLastError(); std::fprintf(stderr, "prefill: STRATA_PLE_COPY_STREAM=1: the upload stream could not be created, the PLE rows go up on the compute stream\n"); } if (ss.qsa_states[ss.qsa_primary()].kv_mode == 1) { // KV streaming: the staging pool's identity page table const int64_t pages = ss.qsa_states[ss.qsa_primary()].n_pages; std::vector ident((size_t) pages); for (int64_t i = 0; i < pages; ++i) ident[(size_t) i] = (int32_t) i; if (cudaMalloc((void**) &m.ident_table, ident.size() * 4) != cudaSuccess || cudaMemcpy(m.ident_table, ident.data(), ident.size() * 4, cudaMemcpyHostToDevice) != cudaSuccess) ok = false; else m.owned.push_back(m.ident_table); } if (!ok) { err = "prefill: host buffers or events for a chunk of " + std::to_string(chunk) + " tokens"; return false; } #if defined(STRATA_USE_HIP) && defined(__linux__) // integrated gfx11 (kfd_quiet.hpp, LOAD): the device buffers the first prompt made at its first layer (the int8 experts' q8_1 // activations for a whole chunk, their group record; the K/V copy stream with STRATA_KV_PREFETCH=1) are made here, during // the load, so the first request allocates nothing while its kernels run. The same buffers and sizes the prompt path makes // (it keeps them while a chunk fits: fr_xq_rows >= T); a failure here is not an error, the prompt path then tries as before. if (strata::core::kfd_quiet::load_moves().first_prompt_buffers && T > 0) { if (m.fr_xq == nullptr && cudaMalloc(&m.fr_xq, T * (size_t) (N / 32) * 36) == cudaSuccess) m.fr_xq_rows = (int64_t) T; else if (m.fr_xq == nullptr) { m.fr_xq = nullptr; (void) cudaGetLastError(); } if (m.i8_rec == nullptr && cudaMalloc((void**) &m.i8_rec, strata::prefill::i8grp::kRecBytes) != cudaSuccess) { m.i8_rec = nullptr; (void) cudaGetLastError(); } if (const char* kp = std::getenv("STRATA_KV_PREFETCH"); kp != nullptr && std::atoi(kp) != 0) { if ((!m.kv_copy && cudaStreamCreateWithFlags(&m.kv_copy, cudaStreamNonBlocking) != cudaSuccess) || (!m.kv_released && cudaEventCreateWithFlags(&m.kv_released, cudaEventDisableTiming) != cudaSuccess) || (!m.kv_ready && cudaEventCreateWithFlags(&m.kv_ready, cudaEventDisableTiming) != cudaSuccess)) (void) cudaGetLastError(); // the request makes what is missing, and says so if it cannot } } // and (11-15) the CPU prompt share's pinned buffers, only where the share is armed (STRATA_PREFILL_CPU_SHARE: off by default on // this class): the activations, the rows and their scales at the sizes the first CPU-sharing layer asks for, for the largest chunk // that shares (below cpu_share_max(), the floor of the full stream on an integrated GPU). The prompt path keeps a buffer that is // large enough and regrows one that is not, as before; a failure here leaves it to the prompt path. if (strata::core::kfd_quiet::load_moves().cpu_share_buffers && T > 0 && cpu_pool_ != nullptr && cpu_share_on()) { const size_t ts = std::min(T, (size_t) std::max(cpu_share_max(), 1)); const size_t rows = (size_t) std::min((int64_t) ts * K, (int64_t) strata::kernels::cpu::MAXT * g.n_expert); auto pin = [](float*& p, size_t& n, size_t want) { if (p != nullptr) return; if (cudaHostAlloc((void**) &p, want * sizeof(float), cudaHostAllocDefault) == cudaSuccess) { n = want; } else { p = nullptr; n = 0; (void) cudaGetLastError(); } }; pin(m.cpu_x, m.cpu_x_n, ts * N); pin(m.cpu_rows, m.cpu_rows_n, rows * N); const bool ones_new = m.cpu_ones == nullptr; pin(m.cpu_ones, m.cpu_ones_n, rows); if (ones_new && m.cpu_ones != nullptr) std::fill(m.cpu_ones, m.cpu_ones + rows, 1.0f); } #endif pf_mark("init: host buffers, PLE rows, identity table", tr_last); Alloc o; o.base = (uint8_t*) borrow; o.cap = borrow_bytes; o.spill = m.borrow_spill; o.owned = &m.owned; { uint16_t* gs = o.take((size_t) GEMM_SCRATCH, ok); void* ws = o.take(GEMM_WS, ok); // STRATA_DEQUANT_SIDE: the second dequant scratch (counted by bytes_needed_impl, retaken by relayout) uint16_t* gs2 = gemm_dequant_side() ? o.take((size_t) GEMM_SCRATCH, ok) : nullptr; if (!ok) { err = "prefill: GEMM scratch does not fit"; return false; } if (!m.gemm.init_external(stream, gs, GEMM_SCRATCH, ws, GEMM_WS, err)) return false; { std::string side_err; if (!m.gemm.set_dequant_side(gs2, side_err)) std::fprintf(stderr, "prefill: STRATA_DEQUANT_SIDE=1 not taken (%s): the dense weights are dequantized in front of their GEMMs\n", side_err.c_str()); } // this stage's device decides, once, here (init runs on the stage's own device): its gr_* kernels write the // image its GEMMs read, and set_act_f16 writes that device's symbol const core::OnDevice on_stage(m.device); m.f16_io = prompt_f16(); m.gemm.set_f16_io(m.f16_io); set_act_f16(m.f16_io); static const bool f16_range = std::getenv("STRATA_F16_RANGE") != nullptr; // what reaches FP16's range if (f16_range) { set_f16_range(true); gemm_set_f16_range(true); } // the hyper-connection's xn16 straight as the FP16 the tensor-core GEMM reads (its two readers then skip the // conversion pass; the same values): STRATA_XN_F16=0 keeps the BF16 image static const bool xn_env = [] { const char* e = std::getenv("STRATA_XN_F16"); return e == nullptr || e[0] != '0'; }(); m.xn_f16 = xn_env && !m.f16_io && m.gemm.bf16_converts_x(); } pf_mark("init: GEMM scratch + workspace (Alloc takes so far)", tr_last, o.tr_asked); if (!carve(T, &o)) { size_t fb = 0, tb = 0; cudaMemGetInfo(&fb, &tb); (void) cudaGetLastError(); err = "prefill: device buffers for a chunk of " + std::to_string(chunk) + " tokens do not fit (" + std::to_string(fb >> 20) + " of " + std::to_string(tb >> 20) + " MiB free at the failure; " + std::to_string(o.used >> 20) + " MiB taken, the failing buffer wanted " + std::to_string(o.failed_bytes >> 20) + " MiB)"; return false; } pf_mark("init: carve (all buffers of the chunk)", tr_last, o.tr_asked); if (pf_alloc_trace()) std::fprintf(stderr, "prefill alloc trace: Alloc takes: %llu cudaMallocs, asked %lld MiB, free fell %lld MiB over them (bytes_needed counts %lld MiB for this chunk's own buffers; o.used %lld MiB)\n", (unsigned long long) o.tr_n, (long long) (o.tr_asked >> 20), (long long) o.tr_drop, (long long) (Prefill::bytes_needed(g, ss, chunk, src != nullptr) >> 20), (long long) (o.used >> 20)); { int wd = 0; if (cudaGetDevice(&wd) == cudaSuccess) warm_gemm_libraries(wd); else (void) cudaGetLastError(); } pf_mark("init: warm_gemm_libraries (BLAS handles, workspaces)", tr_last); return true; } // Every device buffer of a chunk of T tokens, from the Alloc `alloc` (after the GEMM scratch and workspace): `init` // once, and `relayout` for a request's own chunk. The order is `bytes_needed`'s. bool Prefill::carve(size_t T, void* alloc) { Impl& m = *impl_; Alloc& o = *static_cast(alloc); const core::ModelGeometry& g = *m.g; core::SessionState& ss = *m.ss; bool ok = true; m.emb = o.take(T * N, ok); m.R = o.take(T * D, ok); m.xn = gr_unfused() ? o.take(T * D, ok) : nullptr; // F-1: not needed (gr_mix_r reads R) m.grs = o.take(T * HC, ok); const bool hcr = pf_hc_region(); // STRATA_PF_HC_REGION: xn16 / lo / lo16 / gated (and their _lo) are taken in the region if (!hcr) { m.xn16 = o.take(T * (D + (hc_pad() ? XN_PAD : 0)), ok); m.lo = o.take(T * LR, ok); m.lo16 = o.take(T * LR, ok); m.gated = o.take(T * D, ok); } m.inj = o.take(T * HC, ok); m.mixed = o.take(T * N, ok); m.mixed_bf = o.take(T * N, ok); m.mixed_h = o.take(T * N, ok); // Embedding rows are dead after gr_broadcast on m.cs; subsequent half outputs use the same stream. m.bo = m.emb; if (!hcr && bf16x2_hc(m.f16_io)) { m.xn16_lo = o.take(T * D, ok); m.lo16_lo = o.take(T * LR, ok); } if (bf16x2(m.f16_io)) m.mixed_bf_lo = o.take(T * N, ok); m.steps_dev = o.take(T * strata::kernels::kStepCount, ok); strata::kernels::QsaShapes s = core::qsa_shapes(g); m.cap = strata::kernels::qsa_selection_width(strata::kernels::kTopkMaxCells, s); m.max_blocks = ss.qsa_states[ss.qsa_primary()].max_cells / s.idx_block + 2; { // one region for the attention half's and the MoE half's scratch (see gdn_set_bytes) const bool fz = fused_layout(T, m.src != nullptr); m.fused_bufs = fz; const MoeBufs mb = moe_bufs(T, m.g->n_expert, fz, m.tail_req); const uint64_t region = std::max({gdn_set_bytes(T), qsa_set_bytes(T, m.cap, m.max_blocks, m.sel_batch, m.attn_batch, s), moe_set_bytes(T, m.g->n_expert, fz, m.tail_req), hcr ? hc_set_bytes(T, m.f16_io) : (uint64_t) 0}); uint8_t* base = o.take((size_t) region, ok); // No region: the sub-allocators below would each cudaMalloc their share (hundreds of MiB) on a card that has just // shown it has no room, and leave those buffers in `owned`. The failure is reported by the caller. if (base == nullptr) return false; m.region = base; m.region_bytes = region; Alloc a; a.base = base; a.cap = region; a.owned = &m.owned; m.qkv = a.take(T * C, ok); m.z = a.take(T * ZV, ok); m.ab = a.take(T * 2 * HV, ok); m.gate = a.take(T * HV, ok); m.beta = a.take(T * HV, ok); m.hbuf = a.take(T * C, ok); m.y = a.take(T * ZV, ok); m.y_h = a.take(T * (ZV + (pf_pad() ? ZV_PAD : 0)), ok); Alloc b; b.base = base; b.cap = region; b.owned = &m.owned; m.Kc = b.take(T * kKvW, ok); m.Vc = b.take(T * kKvW, ok); m.Qf = b.take(T * kQFull, ok); m.q = b.take(T * kQW, ok); m.idx_raw = b.take(T * kIdxK, ok); m.q_idx = b.take(T * kIdxQW, ok); m.attn = b.take(T * ZV, ok); m.attn_h = b.take(T * (ZV + (pf_pad() ? ZV_PAD : 0)), ok); m.sel_ids = b.take(T * (size_t) m.cap, ok); m.sel_scores = b.take((size_t) m.sel_batch * (size_t) m.max_blocks, ok); m.attn_scratch = b.take((size_t) m.attn_batch * strata::kernels::qsa_decode_attn_scratch_floats(m.cap, s), ok); Alloc c; c.base = base; c.cap = region; c.owned = &m.owned; m.logits = c.take(T * m.g->n_expert, ok); m.w = c.take(T * K, ok); m.ids = c.take(T * K, ok); m.slot_dev = c.take(T * K, ok); m.src_dev = c.take(T * K, ok); const MmqPlan& mp = mmq_plan(); const size_t Tf = (size_t) strata::prefill::fp16_buf_tokens((int64_t) T, fz, stream_all_min()); // (the tail below the floor in a fused layout: fp16_rows_rule.hpp) m.Xs = fp16_bufs(mp) ? c.take(Tf * K * N, ok) : nullptr; m.GU = c.take(mb.gu, ok); m.Hh = fp16_bufs(mp) ? c.take(Tf * K * kFF, ok) : nullptr; m.Dm = c.take(T * K * N, ok); m.sgate = c.take(T * kFF, ok); m.sup = c.take(T * kFF, ok); m.sh_h = c.take(T * kFF, ok); m.shared = c.take(T * N, ok); m.sg = c.take(T, ok); if (mp.any) { m.Xq = c.take(mb.xq, ok); m.H = c.take(mb.h, ok); m.H_bytes = mb.h * sizeof(float); m.Hq = c.take(mb.hq, ok); m.Xscale = c.take(T * K, ok); m.Hscale = c.take(T * K, ok); m.grp_tail = c.take(MMQ_GROUP * 4, ok); m.row_sd = c.take(T * K, ok); // APR: the rotated expert input (only for Hadamard-folded packs) m.Xr = strata::kernels::cpu::hadamard_spec().any() ? c.take(T * N, ok) : nullptr; } if (m.latent) { // latent MoE buffers (counted in moe_set_bytes and so in the region) m.xl = c.take(T * (size_t) m.lat_G * (size_t) m.lat_ELL, ok); m.xlq = c.take(T * (size_t) m.lat_G * ((size_t) m.lat_ELL / 32 * 36), ok); m.yl = c.take(T * K * (size_t) m.lat_ELL, ok); m.ylq = c.take(T * K * ((size_t) m.lat_ELL / 32 * 36), ok); m.tok = c.take(T * K, ok); m.tok_ix = c.take(T * K, ok); m.ids_p = c.take(T * K, ok); m.lat_xq = c.take(T * (N / 32) * 36, ok); m.lat_n = c.take(2, ok); // [0] the grouped kernels' group count, [1] latent_plan_rows' all entries m.lat_scratch = c.take( strata::kernels::native_expert_scratch_bytes((int64_t) (T * K), m.lat_I), ok); m.lat_gp = c.take(LAT_GROUP, ok); m.lat_gs = c.take(LAT_GROUP + 1, ok); m.lat_gcd = c.take(LAT_GROUP, ok); m.lat_hbuf = lat_fused_q3k_on() ? c.take((size_t) T * K * 800, ok) : nullptr; m.lat_gxl = c.take(T * K, ok); m.lat_gdst = c.take(T * K, ok); } if (hcr) { // STRATA_PF_HC_REGION: the hyper-connection read's set, from the region's start like the others Alloc h; h.base = base; h.cap = region; h.owned = &m.owned; m.xn16 = h.take(T * (D + (hc_pad() ? XN_PAD : 0)), ok); m.lo = h.take(T * LR, ok); m.lo16 = h.take(T * LR, ok); m.gated = h.take(T * D, ok); if (bf16x2_hc(m.f16_io)) { m.xn16_lo = h.take(T * D, ok); m.lo16_lo = h.take(T * LR, ok); } } if (base == nullptr) ok = false; } for (int i = 0; i < DQ; ++i) { m.dq_gu[i] = o.take(kFF2 * kEmbd, ok); m.dq_d[i] = o.take(kEmbd * kFF, ok); } if (mmq_plan().any) { const MmqPlan& mp = mmq_plan(); const size_t ids_rows = mmq_ids_rows(T); if (ids_rows == 0) ok = false; m.ids_identity = o.take(ids_rows, ok); // (its over-read slack: mmq_ids_rows) m.bounds_dev = o.take(bounds_ints(m.g->n_expert), ok); const size_t grp_n = m.latent ? LAT_GROUP : MMQ_GROUP; // a latent pack batches LAT_GROUP experts here (STRATA_LATENT_MMQ) m.grp_gu = o.take(grp_n * mp.gu_max + MMQ_TAIL, ok); m.grp_d = o.take(grp_n * mp.d_max + MMQ_TAIL, ok); if (ws::mode() != 0) { // STRATA_MMQ_WS: the offsets and chunks (bytes_needed_impl takes the same) m.xoff_dev = o.take((size_t) m.g->n_expert, ok); m.ws_chunks = (ws::Chunk*) o.take(4 * ws::max_chunks((int64_t) T * K, m.g->n_expert), ok); } // (written at every run's start, not here: when serving, these are live expert-cache slots until a request // lends them - a write now would corrupt a resident expert) if (!m.mmq_ctx) m.mmq_ctx = std::make_unique(); } else if (i8_wants_identity()) { const size_t ids_rows = mmq_ids_rows(T); if (ids_rows == 0) ok = false; m.ids_identity = o.take(ids_rows, ok); // OWN-I8: the identity row table of the int8 groups (no MMQ buffers in this layout) } if (m.latent && !mmq_plan().any) // the latent walk's ent_dst needs the identity map even without MMQ m.ids_identity = o.take(T * K, ok); m.ring = ring_slots(T); const int nstage = stage_slots(T); // the staged walk may use STAGE_GRP of them m.ring_rule = m.ring; // the rule's ring: inside the region, because the loan was sized for it m.ring_room = 0; // THE PROMPT ROOM's slots on top of it (set_ring_room), the lender's own bytes m.ring_room_at = nstage; // the room starts past the layout's staging buffers (stage_slots(T) of them) // THE PROMPT ROOM (set_ring_room + set_ring_room_base): another device user's bytes - the MTP drafter's 512 // routed experts, which only the decode reads - pointed at as so many more ring slots for one prompt. The slots // ARE those bytes: nothing is allocated, nothing is owned, nothing spills, and the last layout of this path // keeps them, so a prompt takes them by moving `m.ring` alone (set_ring_room_active) and the lender's own bytes // are never left out of VRAM. Never past ring_cap() (`copied`/`used` exist for i < ring_cap(), and the arrays // are RING_MAX) and never past what the lender said it has (one slot is one max_blob). if (ring_room_ > 0 && m.ring > STAGE && ring_room_base_ != nullptr) { const int64_t cap = std::min((int64_t) ring_cap() - m.ring_room_at, (int64_t) (ring_room_bytes_ / (uint64_t) MAXBLOB())); const int64_t room = std::min((int64_t) ring_room_, cap); if (room > 0) { m.ring_room = (int) room; if (ring_room_active_) m.ring = m.ring_room_at + (int) room; // the room is this prompt's: the ring counts it } } if (o.base == nullptr && nstage > 0) { // OWNED buffers: the ring in ONE allocation. 384 separate 2.7 MiB cudaMallocs each round up to a 2 MiB page // (~1.3 MiB a slot, ~0.5 GiB in all) that no count ever saw. A borrowed region keeps its per-slot layout (and // so its price, `bytes_needed`: the loans' slot counts do not move). uint8_t* ring_base = o.take((size_t) nstage * (size_t) MAXBLOB(), ok); for (int i = 0; ok && i < nstage; ++i) { m.stage_dev[i] = ring_base + (size_t) i * (size_t) MAXBLOB(); m.stage_live[i] = false; m.used_of[i] = i; } } else { for (int i = 0; i < nstage; ++i) { m.stage_dev[i] = o.take((size_t) MAXBLOB(), ok); m.stage_live[i] = false; // a new buffer: nothing of an earlier layout to wait for m.used_of[i] = i; } } if (m.ring_room > 0) { // THE PROMPT ROOM's slots: the lender's bytes, at the lender's address, taken from nobody for (int i = m.ring_room_at; i < m.ring_room_at + m.ring_room; ++i) { m.stage_dev[i] = (uint8_t*) ring_room_base_ + (size_t) (i - m.ring_room_at) * (size_t) MAXBLOB(); m.stage_live[i] = false; // the lender's bytes are live experts, not a staged blob m.used_of[i] = i; } } m.ple_emb = o.take(T * N, ok); m.ple_norm = o.take((size_t) strata::kernels::NG_HC_DIM, ok); take_stage(o, ss, s, m.stage, ok); m.T = (int64_t) T; m.group_cap = pf_group_scratch() && !m.fused_bufs ? group_cap_rows(T, m.g->n_expert) : 0; if (m.group_cap > 0 || hcr) { // activation evidence: once per layout (a chunk size), what the switches took std::fprintf(stderr, "strata prefill: chunk %zu tokens:%s%s\n", T, m.group_cap > 0 ? (" STRATA_PF_GROUP_SCRATCH on (GU/H/Hq hold " + std::to_string(m.group_cap) + " rows, not " + std::to_string((long long) (T * K)) + ");").c_str() : "", hcr ? " STRATA_PF_HC_REGION on (the hyper-connection read's scratch is in the shared region);" : ""); } return ok; } bool Prefill::relayout(int64_t chunk, void* borrow, uint64_t borrow_bytes, std::string& err, int64_t tail) { Impl& m = *impl_; m.tail_req = tail; // read by carve (a failure leaves the layout unusable as before; the caller ends the request) if (m.borrow_spill) { err = "prefill: a hybrid borrowed/owned layout cannot be changed between requests"; return false; } if (!m.borrowed || borrow == nullptr || chunk <= 0 || chunk > m.T_max) { err = "prefill: relayout needs borrowed buffers and a chunk of at most " + std::to_string(m.T_max); return false; } if (cudaStreamSynchronize(m.cs) != cudaSuccess || cudaStreamSynchronize(m.copy) != cudaSuccess || (m.kv_copy && cudaStreamSynchronize(m.kv_copy) != cudaSuccess)) { err = "prefill: relayout: the stream failed"; return false; } bool ok = true; Alloc o; o.base = (uint8_t*) borrow; o.cap = borrow_bytes; o.owned = &m.owned; uint16_t* gs = o.take((size_t) GEMM_SCRATCH, ok); void* ws = o.take(GEMM_WS, ok); uint16_t* gs2 = gemm_dequant_side() ? o.take((size_t) GEMM_SCRATCH, ok) : nullptr; // STRATA_DEQUANT_SIDE if (ok) m.gemm.rebind(gs, GEMM_SCRATCH, ws, GEMM_WS); if (ok) { std::string side_err; if (!m.gemm.set_dequant_side(gs2, side_err)) std::fprintf(stderr, "prefill: STRATA_DEQUANT_SIDE=1 not taken (%s): the dense weights are dequantized in front of their GEMMs\n", side_err.c_str()); } if (!ok || !carve((size_t) chunk, &o)) { size_t fb = 0, tb = 0; cudaMemGetInfo(&fb, &tb); (void) cudaGetLastError(); err = "prefill: device buffers for a chunk of " + std::to_string(chunk) + " tokens do not fit (" + std::to_string(fb >> 20) + " of " + std::to_string(tb >> 20) + " MiB free at the failure; " + std::to_string(o.used >> 20) + " MiB taken, the failing buffer wanted " + std::to_string(o.failed_bytes >> 20) + " MiB)"; return false; } return true; } int64_t Prefill::chunk() const { return impl_->T; } int64_t Prefill::layout_tail() const { return impl_->tail_req; } int64_t Prefill::tail_room() const { const Impl& m = *impl_; if (!m.fused_bufs) return std::numeric_limits::max(); // MMQ-sized buffers: any chunk the layout has const int64_t d = std::max(stream_all_min() - 1, 0); return m.tail_req >= 0 ? std::min(m.tail_req, d) : d; } bool Prefill::tail_matters(int64_t chunk, bool src) { return chunk > 0 && fused_layout((size_t) chunk, src); } void Prefill::set_ring_room_active(bool on) { ring_room_active_ = on; Impl& m = *impl_; // the slots' own pointers do not move: the room is taken and given back by the ring's count alone if (m.ring_room > 0) m.ring = on ? m.ring_room_at + m.ring_room : m.ring_rule; } bool Prefill::idle(std::string& err) { Impl& m = *impl_; if ((m.cs && cudaStreamSynchronize(m.cs) != cudaSuccess) || (m.copy && cudaStreamSynchronize(m.copy) != cudaSuccess) || (m.kv_copy && cudaStreamSynchronize(m.kv_copy) != cudaSuccess)) { (void) cudaGetLastError(); err = "prefill: the prompt path's streams failed"; return false; } return true; } bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t* next_tokens, int64_t n, int64_t cell0, std::string& err, const char** why) { Impl& m = *impl_; #if defined(STRATA_GPU_METAL) (void) m; (void) mtp; (void) R_rows; (void) next_tokens; (void) n; (void) cell0; (void) err; if (why) *why = "the Metal window feeds the draft layer itself"; return false; #endif const core::OnDevice on_device(m.device); // the stage's own GPU current (a layer split's drafter: the last stage) auto decline = [&](const char* reason) { if (why != nullptr) *why = reason; return false; }; static const bool off = [] { const char* v = std::getenv("STRATA_MTP_BATCH"); return v != nullptr && v[0] == '0'; }(); // A ring (KV streaming: the drafter's window, page p in slot p % n_slots over a host copy) takes the same appends // with its own page table and host copy, as a streamed main layer does; the cells written are those the window can // still reach (r0 below), which the ring holds, so no two of them share a slot. STRATA_MTP_BATCH_RING=0: the // drafter's own pass for a ring (the A/B). static const bool ring_ok = [] { const char* v = std::getenv("STRATA_MTP_BATCH_RING"); return v == nullptr || v[0] != '0'; }(); core::QsaState& st = mtp.kv_state_rw(); if (off) return decline("STRATA_MTP_BATCH=0"); if (n <= 0 || m.g == nullptr || m.region == nullptr) return decline("no prompt scratch region on this path"); if (st.kv_mode != 0 && !(st.kv_mode == 2 && ring_ok)) return decline(st.kv_mode == 2 ? "the drafter's K/V is a ring and STRATA_MTP_BATCH_RING=0" : "the drafter's K/V mode is not paged/ring"); if (st.kv_hybrid) return decline("the drafter's K/V is hybrid (kv_hybrid)"); if (mtp.device() != m.device) return decline("the drafter is on another device than this prefill path"); const auto t0 = Clock::now(); const core::ModelGeometry& g = *m.g; const int64_t Nn = g.n_embd, HCN = g.hc * g.n_embd, KV = g.n_head_kv * g.head_dim; constexpr int kQ8_0 = 8; // GGML_TYPE_Q8_0 const float* w_ne = mtp.tensor_f32("pre_fc_norm_embedding.weight"); const void* w_fe = mtp.tensor_q8("fc_embedding.weight"); const float* w_nh = mtp.tensor_f32("pre_fc_norm_hidden.weight"); const void* w_fh = mtp.tensor_q8("fc_hidden.weight"); const float* w_hn = mtp.tensor_f32("attn_hyper_connection.hc_norm.weight"); const uint16_t* w_dn = mtp.tensor_bf16("attn_hyper_connection.input_mix_weight_down.weight"); const uint16_t* w_up = mtp.tensor_bf16("attn_hyper_connection.input_mix_weight_up.weight"); const void* w_k = mtp.tensor_q8("self_attn.k_proj.weight"); const void* w_v = mtp.tensor_q8("self_attn.v_proj.weight"); const float* w_kn = mtp.tensor_f32("self_attn.k_norm.weight"); if (!w_ne || !w_fe || !w_nh || !w_fh || !w_hn || !w_dn || !w_up || !w_k || !w_v || !w_kn) return decline("a drafter tensor is missing (Q8_0 projections / norms)"); const core::NativeEmbed* nemb = core::native_embed(); const core::WeightRef* wemb = nemb ? nullptr : m.wt->find("token_embd.weight"); if (!nemb && (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))) return decline("the token embedding is not gatherable on this path"); // the cells the drafter's window can still reach const int64_t r0 = std::max(0, mtp.first_needed() - cell0); if (r0 >= n) return true; // per row: emb/e2 (N), en16 (N half), hn/h2/Rm/gated (HCN), hn16/xn16 (HCN half), lo (LR) + lo16, grs, mixed (N) + // mixed_h, K and V (KV each), the token id // E-9: the drafter's Q8_0 matrices through FP16 GEMMs (Gemm::native: dequantize + cuBLAS / hipBLAS), our own code // instead of llama.cpp's MMQ. Measured on an RTX PRO 5000 (IQ3_S, 5 rounds, 20 decode prompts of five classes, lane // P4-FALLBACK 11-11): the same tokens in 100 of 100 requests, prompt time 1.001 of MMQ's form, decode 0.993-1.000 by // class, acceptance 0.7447 against 0.7470. STRATA_MTP_BATCH_F16=0: the Q8_1 x Q8_0 MMQ form (as the drafter's own // integer dot products, mmvq), kept until MMQ leaves the build. static const bool f16_only = [] { const char* v = std::getenv("STRATA_MTP_BATCH_F16"); return v == nullptr || v[0] != '0'; }(); const bool q8 = !f16_only && mmq::built() && mmq::fits(kQ8_0, Nn) && mmq::fits(kQ8_0, KV); // #420 const uint64_t per_row = 4 * (2 * Nn + 4 * HCN + LR + HC + Nn + 2 * KV + 1) + 2 * (Nn + 2 * HCN + LR + Nn) + 64 + (q8 ? (uint64_t) mmq::q8_bytes(g.hc, Nn) + 8 * g.hc : 0); // (8 * hc: the row table twice, see its margin below) int64_t B = std::min(n - r0, (int64_t) (m.region_bytes / per_row) & ~(int64_t) 63); if (st.kv_mode == 2) // a ring: one batch's cells must not share a slot (a batch can straddle one page more) B = std::min(B, ((st.n_slots - 1) * strata::kernels::qsa_real_shapes().page_size) & ~(int64_t) 63); if (B < 64) return decline("too little scratch for a 64-row batch"); uint8_t* q = m.region; auto carve = [&](size_t bytes) { void* p = q; q += (bytes + 255) & ~(size_t) 255; return p; }; float* emb = (float*) carve((size_t) B * Nn * 4); float* e2 = (float*) carve((size_t) B * Nn * 4); uint16_t* en16 = (uint16_t*) carve((size_t) B * Nn * 2); float* hn = (float*) carve((size_t) B * HCN * 4); float* h2 = (float*) carve((size_t) B * HCN * 4); float* Rm = (float*) carve((size_t) B * HCN * 4); float* gated = (float*) carve((size_t) B * HCN * 4); uint16_t* hn16 = (uint16_t*) carve((size_t) B * HCN * 2); uint16_t* xn16 = (uint16_t*) carve((size_t) B * HCN * 2); float* lo = (float*) carve((size_t) B * LR * 4); uint16_t* lo16 = (uint16_t*) carve((size_t) B * LR * 2); float* grs = (float*) carve((size_t) B * HC * 4); float* mixed = (float*) carve((size_t) B * Nn * 4); uint16_t* mixed_h = (uint16_t*) carve((size_t) B * Nn * 2); float* Kc = (float*) carve((size_t) B * KV * 4); float* Vc = (float*) carve((size_t) B * KV * 4); int32_t* tok = (int32_t*) carve((size_t) B * 4); void* xq = q8 ? carve(mmq::q8_bytes(B * g.hc, Nn)) : nullptr; // The row table is read past its end by llama.cpp's MMQ (a whole J-column tile from the start of each tile; the stream-k fixup up to roundup(max_rows, J) // from the expert's first row; J up to 128): rows + the margin of mmq_ids_margin.hpp, max_rows <= B * hc (one expert, p.n = 1), the identity over all of it. const uint64_t ids_rows = q8 ? mmq::mmq_ids_table_rows((uint64_t) (B * g.hc), (uint64_t) (B * g.hc)) : 0; int32_t* ident = q8 ? (int32_t*) carve((size_t) ids_rows * 4) : nullptr; int32_t* bnd = q8 ? (int32_t*) carve(16) : nullptr; if ((uint64_t) (q - m.region) > m.region_bytes) return decline("the batch buffers do not fit the scratch region"); static const bool timing = std::getenv("STRATA_DRAFT_TIMING") != nullptr; // debug: where this pass's time goes if (timing) cudaStreamSynchronize(m.cs); const auto ti0 = Clock::now(); if (!mtp.idle(err)) return false; // the drafter's own stream (its graph uploads) before this writes its K/V const double ms_idle = ms_since(ti0); const auto tl0 = Clock::now(); strata::kernels::QsaShapes s = core::qsa_shapes(g); std::vector tk((size_t) B); if (q8) { if (!m.mmq_ctx) m.mmq_ctx = std::make_unique(); mmq::iota(ident, (int64_t) ids_rows, m.cs); } // y[rows, n_out] = x[rows, k] . w^T for a Q8_0 matrix: MMQ from the FP32 rows (bounds slot 0: rows, 1: rows*hc) auto proj = [&](const float* x, const uint16_t* x16, const void* w, float* y, int64_t rows, int64_t n_out, int64_t k, int slot) { if (!q8) { m.gemm.native(x16, kQ8_0, w, y, rows, n_out, k); return; } mmq::quantize(x, nullptr, xq, kQ8_0, k, k, rows, m.cs); mmq::Product p; p.w = w; p.type = kQ8_0; p.w_rows = n_out; p.w_cols = k; p.expert_bytes = mmq::matrix_bytes(kQ8_0, n_out, k); p.n = 1; p.xq = xq; p.bounds = bnd + 2 * slot; p.ids = ident; p.total_rows = rows; p.max_rows = rows; p.dst = y; p.ld_dst = n_out; m.mmq_ctx->run(p, m.cs); }; for (int64_t b0 = r0; b0 < n; b0 += B) { const int64_t nb = std::min(B, n - b0), c0 = cell0 + b0; for (int64_t i = 0; i < nb; ++i) tk[(size_t) i] = next_tokens[b0 + i]; const int32_t bh[4] = {0, (int32_t) nb, 0, (int32_t) (nb * g.hc)}; // #1996: bh is on this stack and tk is refilled every pass: both go up through pinned slots if (q8 && m.up_ring.h2d(bnd, bh, sizeof bh, m.cs) != cudaSuccess) { err = "prefill: the draft bounds' upload failed"; return false; } if (m.up_ring.h2d(tok, tk.data(), (size_t) nb * 4, m.cs) != cudaSuccess) { err = "prefill: the draft tokens' upload failed"; return false; } // the input branches: the next token's embedding, and this cell's final residual rows if (nemb) { nemb->gather_dev(tok, nb, emb, m.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, tok, (int) nb, 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), emb, m.cs); } rms_rows(emb, w_ne, nb, Nn, Nn, EPS, m.cs); if (!q8) to_f16(emb, en16, nb * Nn, m.cs); proj(emb, en16, w_fe, e2, nb, Nn, Nn, 0); cudaMemcpyAsync(hn, R_rows + (size_t) b0 * HCN, (size_t) nb * HCN * 4, cudaMemcpyDeviceToDevice, m.cs); if (mtp.hnorm_per_stream()) // --mtp-hnorm stream: as the drafter's own pass (mtp.cpp) strata::kernels::native_qsa_rms_norm_grouped(hn, w_nh, hn, (int) Nn, (int) g.hc, (int) (nb * g.hc), EPS, m.cs); else rms_rows(hn, w_nh, nb, HCN, HCN, EPS, m.cs); if (!q8) to_f16(hn, hn16, nb * HCN, m.cs); proj(hn, hn16, w_fh, h2, nb * g.hc, Nn, Nn, 1); // every stream through fc_hidden strata::kernels::add_streams_broadcast(h2, e2, Rm, Nn, (int) g.hc, (int) nb, m.cs); // the attention hyper-connection's read (its mixed input only: this pass writes nothing back) gr_norm_rs(Rm, w_hn, EPS, grs, xn16, nb, m.cs); m.gemm.bf16(xn16, w_dn, lo, nb, LR, HCN); gr_silu(lo, lo16, nb, m.cs); m.gemm.bf16(lo16, w_up, gated, nb, HCN, LR); gr_mix_r(Rm, grs, w_hn, gated, mixed, nullptr, nb, m.cs, mixed_h); // K and V into the drafter's cache, as the prompt path's QSA layers append theirs proj(mixed, mixed_h, w_k, Kc, nb, KV, Nn, 0); proj(mixed, mixed_h, w_v, Vc, nb, KV, Nn, 0); rms_rows(Kc, w_kn, nb * g.n_head_kv, g.head_dim, g.head_dim, EPS, m.cs); rope(Kc, nb, g.n_head_kv, g.head_dim, KV, c0, strata::kernels::rope_scaling(), m.cs); if (st.kv_rot) { // rotated as the drafter's own decode stores them (mtp.cpp) strata::kernels::fwht256_inplace_cuda(Kc, nb * g.n_head_kv, m.cs); strata::kernels::fwht256_inplace_cuda(Vc, nb * g.n_head_kv, m.cs); } if (st.kv_q4) { strata::kernels::kv_append_q4(st.k_q4, st.v_q4, st.page_table, c0, nb, Kc, Vc, s, m.cs, &st.host); } else { kv_append(Kc, Vc, nb, c0, 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, m.cs, &st.host); } } if (cudaStreamSynchronize(m.cs) != cudaSuccess) { err = std::string("prefill: the draft layer's K/V: ") + cudaGetErrorString(cudaGetLastError()); return false; } mtp.ms_prefill += ms_since(t0); if (timing) std::fprintf(stderr, "strata draft kv: %lld cells from %lld (first needed %lld), batch %lld: drafter idle %.1f ms, " "the batches %.1f ms, all %.1f ms\n", (long long) n, (long long) cell0, (long long) (cell0 + r0), (long long) B, ms_idle, ms_since(tl0), ms_since(t0)); return true; } namespace { // Layer split: STRATA_PREFILL_HELP=1 lends the idle stage's GPU (opt-in: the helped rows are computed in another MMQ // grouping and round differently from the default, like STRATA_SPLIT_OWN). Unset or 0: every stage's prompt experts on // its own GPU, as before. bool split_help_env() { static const bool v = [] { const char* e = std::getenv("STRATA_PREFILL_HELP"); return e != nullptr && std::atoi(e) != 0; }(); return v; } // The share of a stage's streamed experts the idle stage's GPU takes for a one-chunk prompt of T tokens (0: none). // The streamed bytes are the same at any T; the hand-over (the activations there, the rows back) grows with T and is // paid per layer whatever the share. Measured on 2x RTX 3090 (UD-Q4_K_XL, PCIe 4.0 x16 each, no P2P), fixed shares // 0.2 / 0.3 / 0.4 against none: 1.5K tokens 749 / 820 / 908 (none 675), 3K 1335 / 1437 / 1328 (1262), 4K 1622 / 1620 / // 1440 (1609), 6K 1811 / 1660 / 1471 (2038), 8K 1895 / 1804 / 1663 (2230) - the best share falls with T and from ~4K // none pays, which this rule follows (0.41 at 1.5K, 0.32 at 3K, off from ~3.3K). // STRATA_PREFILL_HELP_FRAC sets the share for every T. double split_help_frac(int64_t T) { static const double fixed = [] { const char* e = std::getenv("STRATA_PREFILL_HELP_FRAC"); return e ? std::clamp(std::atof(e), 0.0, 1.0) : -1.0; }(); if (fixed >= 0.0) return fixed; const double f = std::min(0.45, 0.5 - (double) T / 16384.0); return f >= 0.3 ? f : 0.0; } } // namespace namespace { // APR: the down projections' sign vectors on the current device (the peer's or the helper's), one per layer, // resolved once at setup (hadamard_device_signs: a mutex and, the first time on that device, a cudaMalloc) into // P.had_sh, which a prompt chunk reads directly. The gate/up signs are not needed there: the primary sends its // already rotated input (m.Xr). void peer_had_signs(PeerPrefill& P) { const auto& lay = strata::kernels::cpu::expert_layout(); P.had_sh.assign(lay.native ? lay.fmt.size() : 0, nullptr); for (size_t l = 0; l < P.had_sh.size(); ++l) if (lay.fmt[l].had_d) P.had_sh[l] = strata::kernels::hadamard_device_signs(lay.fmt[l].d_signs, (int) lay.fmt[l].n_ff); } } // namespace bool Prefill::set_stage_helper(Prefill* helper, std::string& err) { Impl& m = *impl_; m.help_pp.reset(); if (helper == nullptr || helper == this || !split_help_env()) return true; const Impl& h = *helper->impl_; if (m.device < 0 || h.device < 0 || h.device == m.device || !mmq_plan().any) return true; #if defined(STRATA_PF_OWN_ONLY) return true; // (P4-SPLIT: the helper stage computes on MMQ only) #endif auto pp = std::make_unique(); pp->peer = nullptr; // stream-only: it holds no experts, it streams a share of this stage's pp->compact = true; pp->p2p = false; // the mapped host route, whether or not the cards could reach each other (measured there) pp->out_pipe = true; bool ok; { const core::OnDevice on(m.device); ok = cudaEventCreateWithFlags(&pp->ev_in, cudaEventDisableTiming) == cudaSuccess; } int prev = 0; cudaGetDevice(&prev); pp->dev = h.device; cudaSetDevice(pp->dev); ok = ok && cudaStreamCreateWithFlags(&pp->s, cudaStreamNonBlocking) == cudaSuccess && cudaStreamCreateWithFlags(&pp->s_out, cudaStreamNonBlocking) == cudaSuccess && cudaStreamCreateWithFlags(&pp->s_cp, cudaStreamNonBlocking) == cudaSuccess && cudaEventCreateWithFlags(&pp->ev_done, cudaEventDisableTiming) == cudaSuccess; for (int i = 0; ok && i < PeerPrefill::kGrpEv; ++i) ok = cudaEventCreateWithFlags(&pp->ev_grp[i], cudaEventDisableTiming) == cudaSuccess; for (int b = 0; ok && b < 2; ++b) ok = cudaEventCreateWithFlags(&pp->ev_dm[b], cudaEventDisableTiming) == cudaSuccess; // the ring's events; its slots are the helper stage's own ring, bound per prompt (bind_stage_helper) pp->pcopied.assign((size_t) kSplitHelpRing, nullptr); pp->pused.assign((size_t) kSplitHelpRing, nullptr); for (int i = 0; ok && i < kSplitHelpRing; ++i) ok = cudaEventCreateWithFlags(&pp->pcopied[(size_t) i], cudaEventDisableTiming) == cudaSuccess && cudaEventCreateWithFlags(&pp->pused[(size_t) i], cudaEventDisableTiming) == cudaSuccess; if (ok) peer_had_signs(*pp); // APR: on the helper's device cudaSetDevice(prev); if (!ok) { err = "prefill: the layer split's prompt help (streams and events)"; return false; } helper_ = helper; m.help_pp = std::move(pp); return true; } // A one-chunk prompt of T tokens on a layer split: binds the stream-only peer to the helper stage's prompt buffers // (that stage idles until this one hands its rows over). False when it cannot help this prompt. bool Prefill::bind_stage_helper(int64_t T) { Impl& m = *impl_; if (!m.help_pp || helper_ == nullptr || m.pp) return false; const double frac = split_help_frac(T); if (frac <= 0.0 || T < stream_all_min() || m.src == nullptr || m.ring <= STAGE || fused_ring()) return false; const strata::kernels::cpu::ExpertLayout& lay = strata::kernels::cpu::expert_layout(); for (int64_t l = stage_lb_; l < stage_le_; ++l) { if (!(mmq_plan().any && mmq_plan().layer[(size_t) l])) return false; // the peer computes on MMQ only if (lay.native && fused::native_supported(lay.fmt[(size_t) l].gu_type, lay.fmt[(size_t) l].d_type)) return false; } Impl& h = *helper_->impl_; // the helper's buffers must be laid out for this chunk (the serve path lends every stage's at once) if (h.T < T || h.mixed == nullptr || h.Xq == nullptr || h.Hq == nullptr || h.GU == nullptr || h.H == nullptr || h.Dm == nullptr || h.src_dev == nullptr || h.ids_identity == nullptr || h.bounds_dev == nullptr || h.grp_gu == nullptr || h.grp_d == nullptr || !h.mmq_ctx || h.ring <= STAGE) return false; PeerPrefill& P = *m.help_pp; P.T_max = T; P.cap_rows = T * K; P.G = T; // one expert never has more rows than the chunk has tokens P.mixed = h.mixed; P.Xq_g = h.Xq; // the helper's per-layer buffers hold T * K rows: a group's T fit P.Hq_g = h.Hq; P.GU_g = h.GU; P.H_g = h.H; P.Dm_b[0] = h.Dm; P.Dm_b[1] = h.Dm + (size_t) T * N; P.src = h.src_dev; P.ident = h.ids_identity; P.bounds = h.bounds_dev; P.grp_gu = h.grp_gu; P.grp_d = h.grp_d; P.run_ctx = h.mmq_ctx.get(); P.RP = std::min(h.ring, kSplitHelpRing); P.pstage.assign(h.stage_dev, h.stage_dev + P.RP); P.plive.assign((size_t) P.RP, 0); P.dm_live[0] = P.dm_live[1] = false; P.out_pending = false; P.ps_frac = frac; if (!P.p2p) { // grown once to the largest chunk it helped auto grow = [](auto*& p, size_t& cap, size_t n) { if (cap >= n) return true; if (p) cudaFreeHost(p); p = nullptr; cap = 0; if (cudaHostAlloc((void**) &p, n * sizeof(*p), cudaHostAllocPortable | cudaHostAllocMapped) != cudaSuccess) { cudaGetLastError(); p = nullptr; return false; } cap = n; return true; }; size_t bounds_cap = P.host_bounds ? PeerPrefill::kHostBounds : 0; if (!grow(P.host_x, P.host_x_cap, (size_t) T * N) || !grow(P.host_rows, P.host_rows_cap, (size_t) T * K * N) || !grow(P.host_src, P.host_src_cap, (size_t) T * K) || !grow(P.host_bounds, bounds_cap, PeerPrefill::kHostBounds)) return false; } const core::OnDevice on(P.dev); mmq::iota(P.ident, (int64_t) mmq_ids_rows((size_t) T), P.s); // the helper's row table (h.T >= T): a refill may have overwritten its slots return true; } bool Prefill::set_peer(core::PeerExperts* peer, int64_t cap_rows, std::string& err) { Impl& m = *impl_; if (peer == nullptr || !peer->valid()) { m.pp.reset(); return true; } // Without P2P the activations and the result rows take the mapped host route of the layer split's helper // (STRATA_PF_PEER_HOST=0: refuse as before, the prompt path then stays on the primary) const bool host_route_ok = [] { const char* v = std::getenv("STRATA_PF_PEER_HOST"); return v == nullptr || std::atoi(v) != 0; }(); if (!peer->p2p() && !host_route_ok) { err = "prefill peer: the two GPUs cannot access each other (no P2P)"; return false; } #if defined(STRATA_PF_OWN_ONLY) err = "prefill peer: needs the MMQ prompt path (this engine was built without it)"; return false; #endif if (!mmq_plan().any) { err = "prefill peer: needs the MMQ prompt path"; return false; } { // NVFP4: the peer's MMQ products have no per-expert scales (grp_tail, row_sd); its decode rows do const auto& lay = strata::kernels::cpu::expert_layout(); for (const auto& f : lay.fmt) if (lay.native && f.tail_off) { err = "prefill peer: not for NVFP4 experts yet"; return false; } } if (strata::kernels::cpu::hadamard_spec().any()) { // APR: the peer's MMQ share does not rotate; declined std::fprintf(stderr, "strata: prefill peer: not used for a Hadamard-folded (APR) pack\n"); m.pp.reset(); return true; } auto pp = std::make_unique(); pp->peer = peer; pp->p2p = peer->p2p(); pp->T_max = m.T_max; pp->cap_rows = std::max(1, std::min(cap_rows, m.T_max * K)); const int64_t R = pp->cap_rows; const MmqPlan& mp = mmq_plan(); if (cudaEventCreateWithFlags(&pp->ev_in, cudaEventDisableTiming) != cudaSuccess) { err = "prefill peer: event"; return false; } if (!pp->p2p) { // the host route's copies on a stream of their own (STRATA_PF_PEER_XSTREAM=0: the compute stream) const char* xv = std::getenv("STRATA_PF_PEER_XSTREAM"); if ((xv == nullptr || std::atoi(xv) != 0) && (cudaStreamCreateWithFlags(&pp->x_stream, cudaStreamNonBlocking) != cudaSuccess || cudaEventCreateWithFlags(&pp->ev_x, cudaEventDisableTiming) != cudaSuccess)) { err = "prefill peer: copy stream"; return false; } } int prev = 0; cudaGetDevice(&prev); pp->dev = peer->device(); cudaSetDevice(pp->dev); bool ok = cudaStreamCreateWithFlags(&pp->s, cudaStreamNonBlocking) == cudaSuccess && cudaEventCreateWithFlags(&pp->ev_done, cudaEventDisableTiming) == cudaSuccess; { const char* v = std::getenv("STRATA_PF_PEER_OUT_PIPE"); // =0: one copy after the last group (the A/B) pp->out_pipe = (v == nullptr || std::atoi(v) != 0) || pp->compact; // compact always pipes } if (ok && pp->out_pipe) { ok = cudaStreamCreateWithFlags(&pp->s_out, cudaStreamNonBlocking) == cudaSuccess; for (int i = 0; ok && i < PeerPrefill::kGrpEv; ++i) ok = cudaEventCreateWithFlags(&pp->ev_grp[i], cudaEventDisableTiming) == cudaSuccess; } auto take = [&](size_t bytes) -> void* { void* p = nullptr; if (!ok || cudaMalloc(&p, bytes) != cudaSuccess) { ok = false; return nullptr; } pp->owned.push_back(p); return p; }; pp->mixed = (float*) take((size_t) m.T_max * N * 4); { const char* v = std::getenv("STRATA_PF_PEER_COMPACT"); pp->compact = (v == nullptr || std::atoi(v) != 0) || !pp->p2p; // the layer-sized path copies over P2P only } if (pp->compact) { pp->cap_rows = m.T_max * K; // no row cap: only the row tables grow with it pp->G = m.T_max; // one expert never has more rows than the chunk has tokens const int64_t Gr = pp->G; pp->Xq_g = take(mmq::q8_bytes(Gr, N)); pp->Hq_g = take(mmq::q8_bytes(Gr, kFF)); pp->GU_g = (float*) take((size_t) Gr * kFF2 * 4); pp->H_g = (float*) take((size_t) Gr * kFF * 4); pp->Dm_b[0] = (float*) take((size_t) Gr * N * 4); pp->Dm_b[1] = (float*) take((size_t) Gr * N * 4); for (int b = 0; b < 2 && ok; ++b) ok = cudaEventCreateWithFlags(&pp->ev_dm[b], cudaEventDisableTiming) == cudaSuccess; const char* fs = std::getenv("STRATA_PF_PEER_STREAM"); pp->ps_frac = fs ? std::atof(fs) : 0.35; // measured: 0.25-0.5 all ~1950-1970 at 32K, 0.35 best if (pp->ps_frac > 0.0 && !mp.fallback && m.T_max >= stream_all_min()) { pp->RP = 48; if (const char* pr = std::getenv("STRATA_PF_PEER_RING"); pr != nullptr) pp->RP = std::atoi(pr); pp->pstage.assign((size_t) pp->RP, nullptr); pp->pcopied.assign((size_t) pp->RP, nullptr); pp->pused.assign((size_t) pp->RP, nullptr); pp->plive.assign((size_t) pp->RP, 0); ok = ok && cudaStreamCreateWithFlags(&pp->s_cp, cudaStreamNonBlocking) == cudaSuccess; for (int i = 0; ok && i < pp->RP; ++i) { pp->pstage[(size_t) i] = (uint8_t*) take((size_t) MAXBLOB()); ok = ok && cudaEventCreateWithFlags(&pp->pcopied[(size_t) i], cudaEventDisableTiming) == cudaSuccess && cudaEventCreateWithFlags(&pp->pused[(size_t) i], cudaEventDisableTiming) == cudaSuccess; } } else { pp->ps_frac = 0.0; } } else { pp->Xq = take(mmq::q8_bytes(R, N)); pp->Hq = take(mmq::q8_bytes(R, kFF)); pp->GU = (float*) take((size_t) R * kFF2 * 4); pp->H = (float*) take((size_t) R * kFF * 4); pp->Dm = (float*) take((size_t) R * N * 4); } const int64_t Rt = pp->cap_rows; if (!pp->p2p && pp->compact) { const char* v = std::getenv("STRATA_PF_PEER_SUMS"); pp->sums = v == nullptr || std::atoi(v) != 0; } if (pp->sums) { const char* v = std::getenv("STRATA_PF_PEER_F16"); pp->f16 = (v == nullptr || std::atoi(v) != 0) && N % 8 == 0; if (pp->f16) pp->sum16 = (uint16_t*) take((size_t) m.T_max * N * 2); const char* gv = std::getenv("STRATA_PF_PEER_GATHER"); pp->gather = gv == nullptr || std::atoi(gv) != 0; if (pp->gather) { // tokens and starts (<= 2 rows + 1 per group) and rows: <= 3 x the rows + 2 per expert pp->adds_cap = 3 * (size_t) Rt + 2 * NE + 64; pp->adds = (int32_t*) take(pp->adds_cap * 4); pp->tcur.assign((size_t) m.T_max, 0); } pp->sum = (float*) take((size_t) m.T_max * N * 4); pp->wk = (float*) take((size_t) m.T_max * K * 4); pp->pair = (int32_t*) take((size_t) Rt * 4); } pp->src = (int32_t*) take((size_t) Rt * 4); const uint64_t ident_rows = mmq::mmq_ids_table_rows((uint64_t) Rt, (uint64_t) m.T_max); // + the stream-k fixup's over-read if (ident_rows == 0) ok = false; pp->ident = (int32_t*) take((size_t) ident_rows * 4); pp->bounds = (int32_t*) take((size_t) (2 * (NE + NE / MMQ_GROUP + 2)) * 4); pp->grp_gu = (uint8_t*) take(MMQ_GROUP * mp.gu_max + MMQ_TAIL); pp->grp_d = (uint8_t*) take(MMQ_GROUP * mp.d_max + MMQ_TAIL); if (ok) { pp->ctx = std::make_unique(); pp->run_ctx = pp->ctx.get(); mmq::iota(pp->ident, (int64_t) ident_rows, pp->s); peer_had_signs(*pp); // APR: on the peer's device ok = cudaStreamSynchronize(pp->s) == cudaSuccess; } if (ok && !pp->p2p) { // the host route's mapped buffers, sized for the largest chunk auto host = [&](auto*& ptr, size_t& cap, size_t n) { if (cudaHostAlloc((void**) &ptr, n * sizeof(*ptr), cudaHostAllocPortable | cudaHostAllocMapped) != cudaSuccess) { cudaGetLastError(); ptr = nullptr; ok = false; return; } cap = n; }; size_t bounds_cap = 0, w_cap = 0, sum_cap = 0, pair_cap = 0; size_t x16_cap = 0, sum16_cap = 0; if (pp->f16) host(pp->host_x16, x16_cap, (size_t) m.T_max * N); else host(pp->host_x, pp->host_x_cap, (size_t) m.T_max * N); if (ok && pp->sums) { host(pp->host_w, w_cap, (size_t) m.T_max * K); if (ok && pp->f16) host(pp->host_sum16, sum16_cap, (size_t) m.T_max * N); else if (ok) host(pp->host_sum, sum_cap, (size_t) m.T_max * N); if (ok) host(pp->host_pair, pair_cap, (size_t) pp->cap_rows); size_t adds_cap = 0; if (ok && pp->gather) host(pp->host_adds, adds_cap, pp->adds_cap); } else if (ok) { host(pp->host_rows, pp->host_rows_cap, (size_t) pp->cap_rows * N); } if (ok) host(pp->host_src, pp->host_src_cap, (size_t) pp->cap_rows); if (ok) host(pp->host_bounds, bounds_cap, PeerPrefill::kHostBounds); if (!ok) { cudaSetDevice(prev); err = "prefill peer: the host route's pinned buffers (" + std::to_string(((size_t) pp->cap_rows * N * 4 + (size_t) m.T_max * N * 4) >> 20) + " MiB) do not fit in RAM"; return false; } } size_t fb = 0, tb = 0; cudaMemGetInfo(&fb, &tb); cudaSetDevice(prev); if (!ok) { err = "prefill peer: the peer's buffers do not fit (raise --peer-reserve-mib or lower --peer-prefill-rows)"; return false; } std::fprintf(stderr, "strata prefill: peer GPU %d computes its experts' rows of each prompt chunk (up to %lld rows per " "layer%s%s); %zu MiB left free on it\n", pp->dev, (long long) pp->cap_rows, pp->p2p ? "" : (pp->sums ? (pp->f16 ? ", no P2P: through mapped host memory in FP16, one weighted sum per token back" : ", no P2P: through mapped host memory, one weighted sum per token back") : ", no P2P: through mapped host memory"), pp->compact ? (pp->ps_frac > 0.0 ? (", compact group buffers, streams " + std::to_string((int) (pp->ps_frac * 100 + 0.5)) + "% of the primary's streamed experts through a " + std::to_string(pp->RP) + "-slot ring").c_str() : ", compact group buffers") : "", fb >> 20); // APR: this path rotates on the peer too (peer_had_signs), but no P2P pair has run it end to end with a // Hadamard-folded pack yet (the validated two-GPU runs had no P2P, so their prompts stayed on the first GPU) if (strata::kernels::cpu::hadamard_spec().any()) std::fprintf(stderr, "strata prefill: warning: prompt processing on a P2P peer GPU is not validated end to end " "with Hadamard-folded experts yet\n"); m.pp = std::move(pp); return true; } void Prefill::set_pinned_share(double share) { g_pinned_share = share; } void Prefill::set_ring_override(int slots) { g_ring_override = slots > 0 ? slots : 0; } void Prefill::set_ring_budget(int slots, int64_t small_max) { g_ring_budget = slots > 0 ? slots : 0; g_ring_small_max = small_max > 0 ? small_max : 0; } double Prefill::pinned_share() { return g_pinned_share; } int64_t Prefill::stream_all_min_tokens() { return stream_all_min(); } int64_t Prefill::short_read_default(int64_t base) { const auto& lay = strata::kernels::cpu::expert_layout(); if (!lay.native || lay.fmt.empty()) return base; const bool cls = stream_floor_class_measured(); return strata::prefill::short_read_default(base, cls, cls && layers_all_fused_only(), staged_fo_on(), trellis_g1151_all_fused() && layers_any_folded()); } void Prefill::set_card_expert_share(int64_t resident_slots, int64_t total_pairs, double blob_mb, double link_gbps) { #if !defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906) int dev = 0; cudaDeviceProp prop; const bool got = cudaGetDevice(&dev) == cudaSuccess && cudaGetDeviceProperties(&prop, dev) == cudaSuccess; g_sp_sm = got ? prop.multiProcessorCount : 0; g_sp_cc_major = got ? prop.major : 0; g_sp_cc_minor = got ? prop.minor : 0; g_sp_integrated = got ? prop.integrated != 0 : true; #endif g_sp_blob_mb = blob_mb; g_sp_link_gbps = link_gbps; g_sp_total.store(total_pairs, std::memory_order_release); g_sp_resident.store(resident_slots, std::memory_order_release); } // --prefill-pipe (opt-in; STRATA_PREFILL_PIPE): a layer split reads a prompt as a pipeline - stage s reads chunk c while // stage s+1 reads chunk c-1. A stage-chunk costs its layers x (b + a C): a fixed part per chunk (b: the pass over the // layer's experts - dequantized, or streamed over PCIe where they are not in VRAM) and a part per token (a). The prompt // then takes about sum_i (b + a C_i) + (S - 1) max_i (b + a C_i) for chunks C_i over S stages: few large or uneven // chunks keep the later stages waiting (a 10.7K prompt in a 7680 and a 3011 chunk over four GP100 dies: the first // stage carried 24 of 36 s), many small ones pay b again and again. In units of a that is k r + n + (S - 1)(r + C) for // k chunks of C tokens (the last one the rest) and r = b/a, so the rule tries every chunk count from the fewest the // buffers allow, each chunk evened out on the 256-token grid and at least 512, and keeps the cheapest - when it beats // today's split (chunks of `cap`, the last the rest) by 3%. r differs by an order of magnitude between rigs: ~700 tokens // on four GP100 dies (every expert in VRAM, slow arithmetic), ~5000 on two RTX 3090 Ti (fast arithmetic, part of the // experts streamed per chunk), and a wrong r costs (1024 on two RTX 4090: 22-36% slower). The engine neither guesses r // nor learns it from the prompts it serves - that made a prompt's chunks, and so its output bits, depend on how busy // the PC happened to be: setup's --calibrate measures it once (tools/calibrate.py) and writes `--prefill-pipe `. // `--prefill-pipe 1` needs no r: STRATA_PREFILL_EQUAL's rule - today's chunk count evened out, only when today's last // chunk has stream_all_min() tokens or more. A chunk that size streams every expert its stage does not hold, a shorter // one only the experts it routes to: a tail of a few tokens is nearly free, and evening it out would add a full pass // (two RTX 4090, a prompt 25 tokens past 8192: 25% slower). --calibrate tries it like the other values. namespace { double g_pipe = -1.0; // --prefill-pipe; < 0: not given (STRATA_PREFILL_PIPE, else 0) double g_pipe_req = -1.0; // the `pipe=` request key; < 0: none double pipe_value() { if (g_pipe_req >= 0.0) return g_pipe_req; if (g_pipe >= 0.0) return g_pipe; static const double env = [] { const char* v = std::getenv("STRATA_PREFILL_PIPE"); return v != nullptr && v[0] != '\0' ? std::max(0.0, std::atof(v)) : 0.0; }(); return env; } } // namespace void Prefill::set_pipe(double value) { g_pipe = value; } void Prefill::set_pipe_request(double value) { g_pipe_req = value; } double Prefill::pipe_setting() { return pipe_value(); } namespace { // b/a in whole tokens: 0 for none (or NaN), at most 2^20 (llround of an infinity is undefined - `pipe=inf` parses) int64_t ratio_tokens(double ratio) { return ratio > 0.0 ? (int64_t) std::llround(std::min(ratio, 1048576.0)) : 0; } } // namespace int64_t Prefill::pipe_key(double value) { return !(value > 0.0) ? -1 : value <= 1.0 ? 0 : ratio_tokens(value); } // In whole tokens (r rounded): integer sums and a 97/100 test, so a near-tie cannot fall one way in one build and the // other way in another (the same chunks as the double version for every whole r: an exact 97% is not better). int64_t Prefill::pipeline_chunk(int64_t n, int64_t cap, int stages, double ratio) { if (stages < 2 || n <= 0 || cap <= 0) return cap; const int64_t r = ratio_tokens(ratio), S1 = stages - 1; if (r == 0) { // `--prefill-pipe 1`: STRATA_PREFILL_EQUAL's rule (#693, generate.cpp equal_chunk) - today's chunk count, evened // out on the 256-token grid, only when today's last chunk streams every expert anyway (stream_all_min tokens or // more): evening a short tail out would add a full pass over the experts (two RTX 4090: 25% slower) const int64_t k = (n + cap - 1) / cap; const int64_t last = n - (k - 1) * cap; if (k <= 1 || last < stream_all_min()) return cap; const int64_t per = (n + k - 1) / k; return std::min(cap, ((per + 255) / 256) * 256); } auto cost = [&](int64_t c) { // chunks of c tokens, the last the rest const int64_t k = (n + c - 1) / c; return k * r + n + S1 * (r + std::min(c, n)); }; const int64_t today = std::min(n, cap); const int64_t today_cost = cost(today); const int64_t k_min = (n + cap - 1) / cap; const int64_t k_max = ratio > 0.0 ? std::max(k_min, std::min(64, (n + 511) / 512)) : k_min; int64_t best = today; int64_t best_cost = today_cost; for (int64_t k = k_min; k <= k_max; ++k) { const int64_t c = std::min(cap, std::max(512, (((n + k - 1) / k + 255) / 256) * 256)); const int64_t v = cost(c); if (v < best_cost) { best_cost = v; best = c; } } return (best < today && 100 * best_cost < 97 * today_cost) ? best : cap; } int Prefill::stage_count() const { int stages = 1; for (const Prefill* p = next_; p != nullptr; p = p->next_) ++stages; return stages; } int64_t Prefill::plan_chunk(int64_t n) const { const double pipe = pipe_value(); const int stages = stage_count(); if (pipe <= 0.0 || hand_in_ != nullptr || stages < 2) return 0; return pipeline_chunk(n, impl_->T, stages, pipe > 1.0 ? pipe : 0.0); } void Prefill::force_chunk(int64_t chunk, bool single, bool planned) { force_chunk_ = chunk; force_single_ = single; force_planned_ = planned; } void Prefill::log_plan(int64_t n, int64_t chunk, int stages) { const double pipe = pipe_value(); std::fprintf(stderr, "strata prefill: %lld tokens in %lld-token chunks over %d stages (--prefill-pipe %s)\n", (long long) n, (long long) chunk, stages, pipe > 1.0 ? (std::to_string((long long) ratio_tokens(pipe)) + ": b/a").c_str() : "1: today's chunk count, evened out"); } int64_t Prefill::chain_experts_streamed() const { int64_t sum = 0; for (const Prefill* p = this; p != nullptr; p = p->next_) sum += p->stats_.experts_streamed; return sum; } uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk, bool src, int64_t tail) { return bytes_needed_impl(g, ss, chunk, false, src, tail); } // What `init` really allocates when the prompt path owns its buffers (no loan): every cudaMalloc rounds up to a 2 MiB // page, and the ring is one allocation (carve). `bytes_needed` stays the borrowed region's sum. uint64_t Prefill::bytes_needed_owned(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk, bool src) { return bytes_needed_impl(g, ss, chunk, true, src); } uint64_t Prefill::bytes_needed_impl(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk, bool owned_pages, bool src, int64_t tail) { // the same allocation sequence as `init` (`carve`), counted - with the MoE layout `carve` picks for the same source const size_t T = (size_t) chunk; bool ok = true; Alloc o; o.count_only = true; if (owned_pages) o.granule = 2ull << 20; o.take((size_t) GEMM_SCRATCH, ok); o.take(GEMM_WS, ok); if (gemm_dequant_side()) o.take((size_t) GEMM_SCRATCH, ok); // STRATA_DEQUANT_SIDE: the second scratch auto f = [&](size_t n) { o.take(n, ok); }; // `carve`'s order, buffer for buffer: emb, R, xn, grs, xn16, lo, lo16, gated, inj, mixed, mixed_bf, mixed_h, // bo aliases emb. This counted `xn` unconditionally (carve takes it only under STRATA_GR_UNFUSED) and never counted // `grs`. Net over-count T*(D-HC)*4 bytes: 42 MB at a 1024-token chunk, 252 MB (48 Q8_0 slots) at 6144 - the // prompt path was told it had less room than it did. Safe - the direction is over-estimating, and `take` // still bounds-checks - but it under-sizes every loan, so every chunk the scan picks is one step smaller. if (ring_bytes_on()) { f(T * N); f(T * D); if (gr_unfused()) f(T * D); f(T * HC); } else { // STRATA_RING_BYTES=0: 0.1.39's count f(T * N); f(T * D); f(T * D); } const bool hcr = pf_hc_region(); if (!hcr) { o.take(T * (D + (hc_pad() ? XN_PAD : 0)), ok); f(T * LR); o.take(T * LR, ok); f(T * D); } f(T * HC); f(T * N); o.take(T * N, ok); o.take(T * N, ok); if (!emb_reuse_account()) f(T * N); // bo: aliases emb in carve; still counted unless STRATA_EMB_REUSE_ACCOUNT=1 const bool f16_io = prompt_f16(); // the current device's mode (the stage's), as Prefill::init will decide it if (!hcr && bf16x2_hc(f16_io)) { o.take(T * D, ok); o.take(T * LR, ok); } if (bf16x2(f16_io)) o.take(T * N, ok); o.take(T * strata::kernels::kStepCount, ok); strata::kernels::QsaShapes s = core::qsa_shapes(g); const int64_t cap = strata::kernels::qsa_selection_width(strata::kernels::kTopkMaxCells, s); const int64_t max_blocks = ss.qsa_states[ss.qsa_primary()].max_cells / s.idx_block + 2; o.take((size_t) std::max({gdn_set_bytes(T), qsa_set_bytes(T, cap, max_blocks, 256, 32, s), moe_set_bytes(T, g.n_expert, fused_layout(T, src), tail), hcr ? hc_set_bytes(T, f16_io) : (uint64_t) 0}), ok); for (int i = 0; i < DQ; ++i) { o.take(kFF2 * kEmbd, ok); o.take(kEmbd * kFF, ok); } if (mmq_plan().any) { const MmqPlan& mp = mmq_plan(); const size_t ids_rows = mmq_ids_rows(T); if (ids_rows == 0) ok = false; o.take(ids_rows, ok); // ids_identity o.take(bounds_ints(g.n_expert), ok); const auto& lay_g = strata::kernels::cpu::expert_layout(); const size_t grp_n = lay_g.native && !lay_g.fmt.empty() && lay_g.fmt[0].n_embd != (int64_t) N ? LAT_GROUP : MMQ_GROUP; // as carve: latent packs batch LAT_GROUP o.take(grp_n * mp.gu_max + MMQ_TAIL, ok); o.take(grp_n * mp.d_max + MMQ_TAIL, ok); if (ws::mode() != 0) { // carve's STRATA_MMQ_WS offsets and chunks o.take((size_t) g.n_expert, ok); o.take(4 * ws::max_chunks((int64_t) T * K, g.n_expert), ok); } } else if (i8_wants_identity()) { const size_t ids_rows = mmq_ids_rows(T); if (ids_rows == 0) ok = false; o.take(ids_rows, ok); // ids_identity (OWN-I8, a build without MMQ buffers) } else { const auto& lay0 = strata::kernels::cpu::expert_layout(); if (lay0.native && !lay0.fmt.empty() && lay0.fmt[0].n_embd != (int64_t) N) o.take(T * K, ok); } if (owned_pages) { if (stage_slots(T) > 0) o.take((size_t) stage_slots(T) * (size_t) MAXBLOB(), ok); // one allocation } else { for (int i = 0; i < stage_slots(T); ++i) o.take((size_t) MAXBLOB(), ok); } f(T * N); f((size_t) strata::kernels::NG_HC_DIM); strata::kernels::KvHostPools stage; take_stage(o, ss, s, stage, ok); return o.used + (8u << 20); // alignment slack } uint64_t Prefill::bytes_needed_no_ring(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk, bool src) { return bytes_needed(g, ss, chunk, src) - (uint64_t) stage_slots((size_t) chunk) * (uint64_t) MAXBLOB(); } int64_t Prefill::ring_default_slots() { const int r = g_pinned_share >= 0.9 ? (fused_ring() ? 1024 : 384) : 96; return (int64_t) std::min(r, ring_cap()); } int64_t Prefill::ring_cap_for(int64_t old_chunk) { // 0.1.39b (#583, measured on the RTX 5070): giving up ring slots for a bigger chunk paid where 0.1.39's ring held // the chunk at 4096 or less (IQ3_XXS 32K prompts +14% to +26%: 4096/384 -> 6656/56) and lost where 0.1.39 already // read 6144-token chunks (the Coder: 6144/384 -> 7936/199, 32K -12%). From kKeepRingChunk on the ring keeps its // 0.1.39 size and the scan only looks for a bigger chunk next to it (IQ3_XXS unpinned 6144/96 -> 6912/96: +9%). constexpr int64_t kKeepRingChunk = 6144; return old_chunk >= kKeepRingChunk ? ring_default_slots() : ring_max_slots(); } // the unpinned arm keeps its measured 96 (the PR's rule; the byte budget would have been ~49 slots on IQ3_S) int64_t Prefill::ring_max_slots() { return g_pinned_share >= 0.9 ? (int64_t) ring_budget_slots() : (int64_t) std::min(96, ring_cap()); } int64_t Prefill::ring_slots_for(int64_t chunk) { return ring_slots((size_t) chunk); } void Prefill::set_cpu_pool(kernels::cpu::ExpertPool* pool) { cpu_pool_ = pool; } void Prefill::arm_cpu_share(bool applies, bool by_default) { #if !defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906) if (applies && by_default && cpu_share_explicit() == -2.0 && !g_share_default) { g_share_default = true; if (switch_point_on() && g_sp_resident.load(std::memory_order_acquire) >= 0 && strata::prefill::switch_point::card_known(sp_card())) { // STRATA_SWITCH_POINT (on by default for its class, 0 = off): the share's limit scales with the card's own floor (floor x kShareRatio), not the fixed 3,072 const int64_t lim = strata::prefill::switch_point::share_limit_for(strata::prefill::switch_point::floor_for_card( g_sp_resident.load(std::memory_order_acquire), g_sp_total.load(std::memory_order_acquire), sp_card())); if (lim > 0) g_share_limit_arch = lim; } std::fprintf(stderr, "prefill: the CPU share is ON by default for prompt chunks up to %lld tokens (the idle CPU " "takes some of the experts the GPU would stream; answers can differ slightly from 0.1.40.3, " "mean KL ~0.004 below 1024 tokens, ~0.011 at 1-3K). STRATA_PREFILL_CPU_SHARE=0 turns it off, " "STRATA_PREFILL_CPU_SHARE_MAX=1024 keeps the old limit.\n", (long long) cpu_share_max()); } #elif defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906) // AMD-Q (owner decision 10-31): the same default on a single gfx12 card (arch_defaults.hpp: gfx12_prefill_share_default); every other AMD family stays off if (applies && by_default && cpu_share_explicit() == -2.0 && !g_share_default) { int dev = 0; cudaDeviceProp prop; if (cudaGetDevice(&dev) == cudaSuccess && cudaGetDeviceProperties(&prop, dev) == cudaSuccess && strata::core::gfx12_prefill_share_default(prop.gcnArchName)) { g_share_default = true; g_share_limit_arch = strata::prefill::share_rules::gfx12_share_limit(fused::gfx12_iq2s_default()); std::fprintf(stderr, "prefill: the CPU share is ON by default for prompt chunks up to %lld tokens on this gfx12 card (the idle CPU " "takes some of the experts the GPU would stream; answers can differ slightly: first-token KL ~0.02 at 1K tokens, " "prompts of 500-1,000 tokens +18..+31%%, 2K and up unchanged). STRATA_PREFILL_CPU_SHARE=0 or STRATA_GFX12_DEFAULTS=0 turns it off, " "STRATA_PREFILL_CPU_SHARE_MAX=1024 keeps a 1,024 limit.\n", (long long) cpu_share_max()); } } #else (void) by_default; #endif if (applies && cpu_share_on()) g_stream_min_share = cpu_share_max(); #if defined(STRATA_PF_OWN_ONLY) { // the floor is final now (the card answered before this call): the start-up line of a build without MMQ std::lock_guard lk(g_own_line_mu); g_own_line.mark_ready(); } own_only_line_flush(); #endif } void Prefill::set_share_blocked(bool blocked) { g_share_blocked.store(blocked, std::memory_order_release); } void Prefill::fix_cpu_share(double share) { g_share_fixed = share >= 0.0 ? std::clamp(share, 0.0, 1.0) : -1.0; } bool Prefill::cpu_share_balancing() { return cpu_share_env() < 0.0; } bool Prefill::ring_bytes_enabled() { return ring_bytes_on(); } size_t Prefill::slot_guard_bytes() { return mmq::direct_slot_guard(); } bool Prefill::nibble_prompt_check(std::string& why) { const auto& lay = strata::kernels::cpu::expert_layout(); if (mmq::direct_mode() != 0) { why = "STRATA_MMQ_DIRECT (the prompt MMQ would read the 4-bit slots in place)"; return false; } for (int64_t l = 0; lay.native && l < (int64_t) lay.fmt.size(); ++l) { if (!core::nibble::layer(l).on) continue; const auto& f = lay.fmt[(size_t) l]; const char* w = !mmq_plan().any || (size_t) l >= mmq_plan().layer.size() || !mmq_plan().layer[(size_t) l] ? "no prompt MMQ for it (the FP16 path dequantizes GGUF blocks)" : mmq_plan().wmma_only[(size_t) l] ? "the WMMA tiles read GGUF blocks" : fused::native_supported(f.gu_type, f.d_type) ? "the fused prompt kernels read GGUF blocks" : mmq::matrix_bytes(f.gu_type, kFF2, N) != (size_t) core::nibble::kRows * f.gu_row || mmq::matrix_bytes(f.d_type, N, kFF) != f.bytes - f.down_off ? "MMQ's matrices are not the blob's [gate | up] and down" : nullptr; if (w != nullptr) { why = "layer " + std::to_string(l) + ": " + w; return false; } } return true; } namespace { const core::WeightRef* need(const core::LayerView& v, const char* suffix, std::string& err) { const core::WeightRef* r = v.get(suffix); if (!r) err = v.name(suffix) + " is missing"; return r; } // STRATA_COLD_TRACE=1 (diagnostic): the wall time of the first 80 dense projections of the process, each synchronized, to show // which first calls pay a one-time cost (library / code object loads). struct ColdTrace { bool on; int n = 0; std::chrono::steady_clock::time_point t0; ColdTrace() { const char* e = std::getenv("STRATA_COLD_TRACE"); on = e != nullptr && e[0] == '1'; } bool begin() { if (!on || n >= 80) return false; cudaDeviceSynchronize(); t0 = std::chrono::steady_clock::now(); return true; } void end(const char* what, const std::string& name, int64_t T, int64_t N, int64_t K) { cudaDeviceSynchronize(); const double ms = std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); ++n; std::fprintf(stderr, "strata cold-trace: #%d %s %s T=%lld N=%lld K=%lld %.2f ms\n", n, what, name.c_str(), (long long) T, (long long) N, (long long) K, ms); } }; ColdTrace g_cold; bool native_proj(Gemm& gm, const core::WeightRef* w, const uint16_t* X, float* Y, int64_t T, const std::string& name, std::string& err, int64_t ldy = 0, int64_t ldx = 0) { if (!w->native_data) { err = "prefill: " + name + " has no native GGUF blocks (run with --native)"; return false; } const bool ct = g_cold.begin(); gm.native(X, w->native_type, w->native_data, Y, T, w->ne1, w->ne0, ldy, 0.0f, ldx); if (ct) g_cold.end("native", name, T, w->ne1, w->ne0); return true; } bool bf16_proj(Gemm& gm, const core::WeightRef* w, const uint16_t* X, float* Y, int64_t T, const std::string& name, std::string& err, int64_t ldy = 0, const uint16_t* X_lo = nullptr, int64_t ldx = 0, void* stream = nullptr, bool x_f16 = false) { if (w->kind != core::WeightKind::Bf16InF32 || !w->data) { err = "prefill: " + name + " is not a resident BF16 tensor"; return false; } const uint16_t* wb = (const uint16_t*) w->data; if (w->q8_scale != nullptr) { // STRATA_HC_REQ8: the GEMM takes bf16, so the int8 codes are expanded into one reused scratch on this stream static uint16_t* scratch = nullptr; static int64_t cap = 0; if (w->elements > cap) { if (scratch) cudaFree(scratch); if (cudaMalloc((void**) &scratch, (size_t) w->elements * 2) != cudaSuccess) { scratch = nullptr; cap = 0; err = "prefill: the STRATA_HC_REQ8 scratch"; return false; } cap = w->elements; } strata::kernels::hc_q8_to_bf16(w->data, w->q8_scale, scratch, (long long) w->elements, stream); wb = scratch; } const bool ct = g_cold.begin(); gm.bf16(X, wb, Y, T, w->ne1 > 0 ? w->ne1 : 1, w->ne0, ldy, 0.0f, ldx, x_f16); if (ct) g_cold.end("bf16", name, T, w->ne1, w->ne0); if (X_lo) gm.bf16(X_lo, wb, Y, T, w->ne1 > 0 ? w->ne1 : 1, w->ne0, ldy, 1.0f); return true; } } // namespace namespace { // STRATA_PREFILL_TIMING=1: the prompt path's GPU time by phase. Events are recorded on the compute stream in order; // the time between two consecutive marks is charged to the phase of the first, so a gap where the GPU waits (for the // host's expert grouping, or for an expert's copy) lands on the phase that was waiting. Events are reused: the marks // are folded at every MoE layer's host sync, after which all of them have completed. enum PfPhase { kPfStart, kPfHc, kPfGdn, kPfQsa, kPfQsaIdx, kPfQsaSel, kPfQsaAttn, kPfRouter, kPfHostGroup, kPfGather, kPfWaitCopy, kPfDequant, kPfGemmGU, kPfGemmD, kPfCombine, kPfPle, kPfKvStage, kPfGdnConv, kPfGdnRec, kPfGdnOut, kPfHcWrite, kPfHcDown, kPfHcUp, kPfHcInj, kPfHcMix, kPfQsaIdxG, kPfQsaPrep, kPfCount }; const char* const kPfNames[kPfCount] = {"embed+steps", "hc read", "gdn", "qsa proj", "qsa indexer", "qsa select", "qsa attn", "router+shared", "host grouping", "gather", "wait copy", "dequant", "gemm gate/up", "gemm down", "combine", "ple", "kv stage", "gdn conv+gates", "gdn recurrence", "gdn out proj", "hc write", "hc read: down+silu", "hc read: up", "hc read: inject", "hc read: mix", "qsa: indexer gemms", "qsa: norm/rope/kv append/q prep"}; struct PfTimer { bool on = std::getenv("STRATA_PREFILL_TIMING") != nullptr; std::vector ev; std::vector ph; size_t used = 0; double ms[kPfCount] = {}; void mark(int phase, cudaStream_t s) { if (!on) return; // a mark in the phase already running adds nothing to the totals (the gaps are charged to the earlier mark's // phase either way), but every event record is a submission of its own under WDDM (~80 us on a 6900 XT, // Windows 11): compute() marks "dequant" once per expert, which put ~16 records per MMQ group on the stream // and charged the launch gaps they made to "dequant". The same totals, a fraction of the records. if (strata::prefill::timer_mark_repeats(ph.data(), used, phase)) return; if (used == ev.size()) { cudaEvent_t e = nullptr; cudaEventCreate(&e); ev.push_back(e); ph.push_back(0); } ph[used] = phase; cudaEventRecord(ev[used], s); ++used; } // every recorded mark has completed (the stream was synchronized): charge the gaps, keep the last mark void fold() { if (!on || used < 2) return; for (size_t i = 0; i + 1 < used; ++i) { float t = 0.0f; if (cudaEventElapsedTime(&t, ev[i], ev[i + 1]) == cudaSuccess) ms[ph[i]] += t; } std::swap(ev[0], ev[used - 1]); std::swap(ph[0], ph[used - 1]); used = 1; } ~PfTimer() { for (cudaEvent_t e : ev) cudaEventDestroy(e); } }; // multi-GPU: the peer's own timeline (STRATA_PREFILL_TIMING): marks on the peer stream, folded with the primary's enum PePhase { kPeIdle, kPeMoeIn, kPeMoeGemm, kPeMoeOut, kPeCount }; const char* const kPeNames[kPeCount] = {"idle", "moe in", "moe gemm", "moe out"}; struct PeTimer { bool on = std::getenv("STRATA_PREFILL_TIMING") != nullptr; int dev = -1; std::vector ev; std::vector ph; size_t used = 0; double ms[kPeCount] = {}; void mark(int phase, cudaStream_t s) { // the peer device is current if (!on) return; if (used == ev.size()) { cudaEvent_t e = nullptr; cudaEventCreate(&e); ev.push_back(e); ph.push_back(0); } ph[used] = phase; cudaEventRecord(ev[used], s); ++used; } void fold() { // every mark has completed if (!on || used < 2) return; for (size_t i = 0; i + 1 < used; ++i) { float t = 0.0f; if (cudaEventElapsedTime(&t, ev[i], ev[i + 1]) == cudaSuccess) ms[ph[i]] += t; } std::swap(ev[0], ev[used - 1]); std::swap(ph[0], ph[used - 1]); used = 1; } ~PeTimer() { if (dev < 0) return; int prev = 0; cudaGetDevice(&prev); cudaSetDevice(dev); for (cudaEvent_t e : ev) cudaEventDestroy(e); cudaSetDevice(prev); } }; // ---- KVA (STRATA_KVA, research): the capture and oracle taps of the late layers' block inputs --------------- // // A capture run writes, for every layer at or after the split and every chunk, the chunk's multi-stream residual // R [T, hc*n_embd] as it ENTERS the layer - exactly the vector a projector would have to produce. One file per // layer (`L.bin`), rows in position order, bf16 (fp32 with STRATA_KVA_CAPTURE_FP32, for parity runs): a // reader seeks straight to a position. The oracle mode replays such a file in place of a projector, which is how // the state-only path is checked against the full one before any weights are fitted. struct KvaTap { std::string dir; bool fp32 = false; std::vector f; // one per layer, opened on first use std::vector next_pos; // where the next record must start (0: nothing written yet) std::vector host; // one chunk's rows, fp32 std::vector bf; // their bf16 image std::string err; bool open(const std::string& d, bool is_fp32) { dir = d; fp32 = is_fp32; f.assign(128, nullptr); next_pos.assign(128, 0); return true; } void close() { for (std::FILE*& p : f) { if (p != nullptr) std::fclose(p); p = nullptr; } } /// A new cold read starts: a capture file holds one prompt's rows, so every layer starts over. void begin_prompt() { close(); if (next_pos.empty()) next_pos.assign(f.size(), 0); for (int64_t& p : next_pos) p = 0; } std::string path(int64_t layer) const { return dir + "/L" + std::to_string((long long) layer) + ".bin"; } /// The chunk's rows of `layer` from the device residual `R` - a capture. bool write(int64_t layer, const float* R, int64_t T, int64_t p0, int64_t D, cudaStream_t cs) { if (layer < 0 || layer >= (int64_t) f.size()) { err = "kva capture: layer out of range"; return false; } if (next_pos[(size_t) layer] != p0) { err = "kva capture: layer " + std::to_string((long long) layer) + " expects position " + std::to_string((long long) next_pos[(size_t) layer]) + ", got " + std::to_string((long long) p0) + " (a capture needs one cold, in-order read)"; return false; } host.resize((size_t) T * D); if (cudaMemcpyAsync(host.data(), R, (size_t) T * D * 4, cudaMemcpyDeviceToHost, cs) != cudaSuccess || cudaStreamSynchronize(cs) != cudaSuccess) { err = "kva capture: the residual copy failed"; return false; } std::FILE*& p = f[(size_t) layer]; if (p == nullptr) { p = std::fopen(path(layer).c_str(), "wb"); if (p == nullptr) { err = "kva capture: cannot write " + path(layer); return false; } } if (fp32) { std::fwrite(host.data(), 4, (size_t) T * D, p); } else { bf.resize((size_t) T * D); for (size_t i = 0; i < bf.size(); ++i) { // round to nearest even, as STRATA_PREFILL_DUMP_R_ALL does uint32_t u; std::memcpy(&u, &host[i], 4); u += 0x7fffu + ((u >> 16) & 1u); bf[i] = (uint16_t) (u >> 16); } std::fwrite(bf.data(), 2, bf.size(), p); } std::fflush(p); next_pos[(size_t) layer] = p0 + T; return true; } /// The chunk's rows of `layer` into the device residual `R` - an oracle replay. bool read(int64_t layer, float* R, int64_t T, int64_t p0, int64_t D, cudaStream_t cs) { if (layer < 0 || layer >= (int64_t) f.size()) { err = "kva oracle: layer out of range"; return false; } std::FILE*& p = f[(size_t) layer]; if (p == nullptr) { p = std::fopen(path(layer).c_str(), "rb"); if (p == nullptr) { err = "kva oracle: cannot read " + path(layer); return false; } } const int64_t n = T * D; const int64_t esz = fp32 ? 4 : 2; const long long off = (long long) ((int64_t) p0 * D * esz); #if defined(_WIN32) const bool seek_ok = _fseeki64(p, off, SEEK_SET) == 0; #else const bool seek_ok = fseeko(p, (off_t) off, SEEK_SET) == 0; #endif if (!seek_ok) { err = "kva oracle: cannot seek " + path(layer); return false; } host.resize((size_t) n); if (fp32) { const size_t got = std::fread(host.data(), 4, (size_t) n, p); if (got != (size_t) n) { err = "kva oracle: short read of " + path(layer) + " at position " + std::to_string((long long) p0) + ": wanted " + std::to_string((long long) n) + " values, got " + std::to_string((long long) got); return false; } } else { bf.resize((size_t) n); const size_t got = std::fread(bf.data(), 2, (size_t) n, p); if (got != (size_t) n) { err = "kva oracle: short read of " + path(layer) + " at position " + std::to_string((long long) p0) + ": wanted " + std::to_string((long long) n) + " values, got " + std::to_string((long long) got); return false; } for (size_t i = 0; i < bf.size(); ++i) { // bf16 -> fp32: the top half, exactly const uint32_t u = (uint32_t) bf[i] << 16; std::memcpy(&host[i], &u, 4); } } if (cudaMemcpyAsync(R, host.data(), (size_t) n * 4, cudaMemcpyHostToDevice, cs) != cudaSuccess) { err = "kva oracle: the upload failed"; return false; } return true; } }; } // namespace bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::string& err) { err.clear(); // force_chunk is for this run only: taken before any early return can leave it to the next one const int64_t forced = hand_in_ == nullptr ? std::exchange(force_chunk_, 0) : 0; const bool forced_single = force_single_, forced_planned = force_planned_; Impl& m = *impl_; const core::OnDevice on_device(m.device); // the stall report asks this stream (the one the waits below block on) whether its work is done (#5718 follow-up) const core::WatchedStream watched("the prompt's GPU work", (void*) m.cs, m.device); const core::ModelGeometry& g = *m.g; core::SessionState& ss = *m.ss; const auto t_start = Clock::now(); const int64_t LB = stage_lb_, LE = stage_le_; // STRATA_KVA: the approximate-prefix plan of this run, if it qualifies (a cold read of one long prompt, one // stage, no capture). Positions [0, kva_frontier) are approximated in the layers at and after the split. const KvaConfig& kva = kva_config(); const KvaEngine& kva_eng = kva_engine(); const int64_t kva_suffix = kva.effective_suffix(); bool kva_routine = false; const char* kva_why_not = kva.decline(kva_eng, pos0, n, LB, LE < 0 ? -1 : LE, (int64_t) g.n_layers, embd_rows != nullptr, !tap_layers_.empty(), &kva_routine, g.n_layers != 48 || g.n_expert != 512); if (kva_why_not == nullptr && (g.n_layers != 48 || g.n_expert != 512)) { // STRATA_KVA_ANY_MODEL=1 (research): allowed on a shape KVA was not measured on, with a warning once static std::atomic warned{false}; if (!warned.exchange(true)) std::fprintf(stderr, "strata prefill: WARNING: STRATA_KVA_ANY_MODEL=1: KVA was only measured on Qwen3.8-Flash-Next " "(48 layers, 512 experts); this model has %lld layers and %lld experts\n", (long long) g.n_layers, (long long) g.n_expert); } const bool kva_run = kva_why_not == nullptr; const int64_t kva_split = std::min(std::max(kva.split, 1), g.n_layers); const int64_t kva_frontier = std::max(0, n - kva_suffix); static KvaTap kva_capture; // process-wide: one cold read at a time, so the per-layer position is enough static KvaTap kva_oracle; int64_t kva_positions = 0, kva_chunks = 0; // what actually took the approximate path (diagnostics) kva_floor_ = 0; // this run's: raised below as chunks are approximated if (kva.capture && kva_capture.f.empty()) kva_capture.open(kva.capture_dir, kva.capture_fp32); if (kva.capture && pos0 == 0 && hand_in_ == nullptr) kva_capture.begin_prompt(); // one prompt per capture if (kva_run && kva.mode == KvaConfig::Mode::Oracle && kva_oracle.f.empty()) kva_oracle.open(kva.oracle_dir, kva.oracle_fp32); // The direct successor's future lives on the Prefill object. Intermediate // stages therefore do not drain the complete remaining GPU chain here. double host_sync_ms = 0, host_chunk_ms = 0, host_setup_ms = 0; // STRATA_PREFILL_TIMING: the host's share strata::kernels::QsaShapes s = core::qsa_shapes(g); // read once (STRATA_KV_PREFETCH=1/0: the next streamed layer's KV prefix is staged on its own stream) static const bool kv_prefetch_on = [] { const char* e = std::getenv("STRATA_KV_PREFETCH"); return e ? std::atoi(e) != 0 : false; }(); const bool kv_prefetch = kv_prefetch_on; if (kv_prefetch) { if ((!m.kv_copy && cudaStreamCreateWithFlags(&m.kv_copy, cudaStreamNonBlocking) != cudaSuccess) || (!m.kv_released && cudaEventCreateWithFlags(&m.kv_released, cudaEventDisableTiming) != cudaSuccess) || (!m.kv_ready && cudaEventCreateWithFlags(&m.kv_ready, cudaEventDisableTiming) != cudaSuccess)) { err = "prefill: KV prefetch stream/events"; return false; } } // An early return must drain DMA before the caller refills the borrowed expert slots. struct KvDrain { cudaStream_t stream; ~KvDrain() { if (stream) cudaStreamSynchronize(stream); } } kv_drain{kv_prefetch ? m.kv_copy : nullptr}; KvDrain ple_drain{m.ple_copy}; // STRATA_PLE_COPY_STREAM: no upload left in flight from a buffer the next run refills int64_t kv_prefetches = 0; 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]}; // layer split: a prompt of one chunk runs the stages one after the other, so the next stage's GPU idles while // this one reads; it streams and computes a share of this stage's experts then (set_stage_helper), in the // place of a --peer-device peer, and gives the stage back its own buffers when the run ends // the chunk this prompt is read in: the buffers' size, or with --prefill-pipe on a layer split the one that keeps // every stage busy (pipeline_chunk: 1 evens today's chunk count out, a number > 1 is the rig's b/a). Only the first // stage chooses: a later one is handed one chunk at a time. A run the engine forced (force_chunk: one part of a // segment it planned on its whole length) reads in that chunk and keeps the segment's single/planned flags. if (forced > m.T) { err = "prefill: a forced chunk of " + std::to_string((long long) forced) + " tokens is larger than the buffers' " + std::to_string((long long) m.T); return false; } const int64_t planned_C = forced > 0 ? 0 : plan_chunk(n); const int64_t C = forced > 0 ? forced : planned_C > 0 ? planned_C : m.T; if (planned_C > 0 && C < std::min(n, m.T)) log_plan(n, C, stage_count()); const bool single_chunk = hand_in_ == nullptr ? (forced > 0 ? forced_single : n <= C) : single_chunk_; // a read the pipe chunked takes no CPU share: its chunks overlap on the stages by design, and which stage won the // pool would decide which experts the CPU computed (other bits) - timing, where the chunks are a function of n const bool pipe_chunked = hand_in_ == nullptr ? (forced > 0 ? forced_planned : planned_C > 0 && C < std::min(n, m.T)) : pipe_chunked_; const bool helped = single_chunk && bind_stage_helper(n); if (helped) std::swap(m.pp, m.help_pp); struct HelpScope { Impl& m; bool on; ~HelpScope() { if (!on) return; const core::OnDevice d(m.pp->dev); // its work is done before the stage hands over; settle its streams cudaStreamSynchronize(m.pp->s_cp); cudaStreamSynchronize(m.pp->s_out); cudaStreamSynchronize(m.pp->s); std::swap(m.pp, m.help_pp); } } help_scope{m, helped}; PfTimer pt; PeTimer pe; if (m.pp) pe.dev = m.pp->dev; else pe.on = false; const cudaStream_t cs = (cudaStream_t) m.cs; // the MMQ row table lives in the borrowed cache slots, which the refill after a prompt overwrites with experts: // write it again for every prompt (a layout is reused as long as the chunk and the slots are the same) if (m.ids_identity != nullptr) mmq::iota(m.ids_identity, (int64_t) mmq_ids_rows((size_t) m.T), m.cs); // The PLE rows of a chunk are read from the model file on the host (an SSD read per missed row): the chunk // after this one is read on a thread while the GPU runs this one, into the other of two buffers. The rows // depend only on the tokens (the two before a position name its n-grams), so this is the same data. const bool ple_on = ss.ple.ready() && LB <= 1 && 1 < LE; // the PLE block batched over the chunk: the pinned postops and a BF16 or GGUF-native key (else token by token); // STRATA_PLE_BATCH=0 keeps the per-token block (the A/B) const bool ple_batch_env = ple_batch_enabled(); 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, C](int64_t c0, int buf, std::string& e) -> bool { const int64_t T = std::min(C, n - c0); auto at = [&](int64_t i) { return i < 2 ? prev0[i] : (int32_t) tokens[i - 2]; }; // prev0, then the tokens 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; } // STRATA_PLE_COMPACT=1: the raw rows (1,440 bytes a token instead of 10,240 floats' bytes) into the same pinned // buffer; ple_land uploads them and dequantizes on the device if (ple_compact_on() && m.ple_pageable[buf].empty() && m.ple_raw_dev != nullptr && ss.ple.table->gather_raw_iq4nl(m.ple_rows[buf].data(), (size_t) T, (uint8_t*) m.ple_emb_host[buf])) { m.ple_compact[buf] = true; return true; } m.ple_compact[buf] = false; 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; // declared after everything it reads: an early return waits for it int ple_buf = 0; if (hand_in_ == nullptr) stopped_at_ = -1; #if defined(STRATA_HAVE_DFLASH2) std::vector tap_slot; // the taps of the layers THIS stage runs (the whole model without a split), at [row][its taps][n_embd]; on a split the callback of each stage // gets its own share and the engine lands them on the drafter's card tap_local_.clear(); for (int l : tap_layers_) if (l >= stage_lb_ && (stage_le_ < 0 || l < stage_le_) && l < g.n_layers - 1) tap_local_.push_back(l); if (!tap_local_.empty()) { if (tap_dev_ == nullptr && cudaMalloc((void**) &tap_dev_, (size_t) tap_cap_ * tap_local_.size() * (size_t) g.n_embd * 4) != cudaSuccess) { (void) cudaGetLastError(); tap_dev_ = nullptr; err = "prefill: the DFlash2 tap staging does not fit in VRAM"; return false; } tap_slot.assign((size_t) g.n_layers, -1); for (size_t i = 0; i < tap_local_.size(); ++i) tap_slot[(size_t) tap_local_[i]] = (int) i; } #endif for (int64_t c0 = 0; c0 < n; c0 += C) { if (should_stop && should_stop()) { err = "cancelled"; return false; } // the first stage: end the run before this chunk (every chunk before it is handed on; the caller's drain // waits for the later stages to read them) if (c0 > 0 && hand_in_ == nullptr && stop_at_chunk && stop_at_chunk()) { stopped_at_ = c0; break; } if (std::getenv("STRATA_TRACE")) { static Clock::time_point t0; // process-wide: the stages run on different threads static bool set = false; if (!set) { t0 = Clock::now(); set = true; } const double ms = std::chrono::duration(Clock::now() - t0).count(); std::fprintf(stderr, "strata trace: prompt chunk %lld of %lld stage [%lld,%lld) %.1f ms\n", (long long) c0, (long long) n, (long long) stage_lb_, (long long) stage_le_, ms); std::fflush(stderr); } const int64_t T = std::min(C, n - c0), p0 = pos0 + c0; // A fused layout carved with a tail (relayout's `tail`, loan_rules.hpp) holds MMQ's rows for that many tokens of a // chunk below the stream floor: a longer one would overrun GU / H / Xq / Hq (an illegal access, or a hang). The // lend sizes the tail from the segment, so this is the clean error for anything that drifts from it. if (m.fused_bufs && T < stream_all_min() && T > tail_room()) { err = "prefill: a chunk of " + std::to_string((long long) T) + " tokens below the stream floor does not fit the fused layout's tail of " + std::to_string((long long) tail_room()) + " tokens"; return false; } // STRATA_KVA: this chunk is approximated in the layers at and after the split when its last position is // before the request's full-computation suffix; a chunk in the suffix runs the ordinary path everywhere. const bool kva_approx = kva_run && (p0 + T <= kva_frontier); if (kva_approx) { kva_positions += T; ++kva_chunks; kva_floor_ = p0 + T; // the chunks are a prefix of the run: this is the first position with real rows } core::progress_at("reading the prompt (batched): preparing the chunk from token", p0); // #251 #if defined(STRATA_HAVE_DFLASH2) // DFlash2 prompt taps: the rows of this chunk at positions >= tap_first_ (the drafter looks back one window) const int64_t tap_t0 = tap_dev_ != nullptr && !tap_local_.empty() ? std::min(T, std::max(0, tap_first_ - p0)) : T; const int64_t tap_rows = T - tap_t0; if (tap_rows > tap_cap_) { err = "prefill: the DFlash2 tap staging is smaller than the chunk's tapped rows"; return false; } #else const int64_t tap_t0 = 0, tap_rows = 0; #endif ++stats_.chunks; pt.mark(kPfStart, cs); const auto tsetup = Clock::now(); // ---- embeddings, broadcast to the four streams - or, in a later stage of a layer split, the rows the // previous stage handed on if (hand_in_ != nullptr && hand_ev_ != nullptr) { // STRATA_HANDOFF_PIPE: each piece's upload waits on the GPU for its download on the stage before. The // stage before split the chunk it handed on (n rows); one chunk here is all of it, otherwise (a smaller // chunk here) every piece is waited for before the one copy. const auto* ev = static_cast(hand_ev_); const bool whole = c0 == 0 && T == n; bool ok = true; for (int i = 0; ok && i < hand_pieces_; ++i) { int64_t r0 = 0, nr = 0; handoff_piece(n, hand_pieces_, i, r0, nr); ok = cudaStreamWaitEvent(m.cs, ev[i], 0) == cudaSuccess; if (ok && whole && nr > 0) ok = cudaMemcpyAsync(m.R + (size_t) r0 * D, hand_in_ + (size_t) r0 * D, (size_t) nr * D * 4, cudaMemcpyHostToDevice, m.cs) == cudaSuccess; } if (ok && !whole) ok = cudaMemcpyAsync(m.R, hand_in_ + (size_t) c0 * D, (size_t) T * D * 4, cudaMemcpyHostToDevice, m.cs) == cudaSuccess; if (!ok) { err = std::string("prefill: the layer split's hand-off upload failed (STRATA_HANDOFF_PIPE): ") + cudaGetErrorString(cudaGetLastError()); return false; } } else if (hand_in_ != nullptr) { if (cudaMemcpyAsync(m.R, hand_in_ + (size_t) c0 * D, (size_t) T * D * 4, cudaMemcpyHostToDevice, m.cs) != cudaSuccess) { err = "prefill: the layer split's hand-off upload failed"; return false; } } // C-4: the whole chunk's rows in one gather (the same per-element arithmetic as the per-token path, so the // same bits); a chunk with picture rows, or a token outside the table, takes the per-token path bool batched = hand_in_ == nullptr && 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 ((embd_rows && embd_rows[p0 + t]) || 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, m.cs)) { err = "prefill: the token id upload failed"; return false; } if (nemb) { nemb->gather_dev(m.tok_dev, T, m.emb, m.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, m.cs); } } for (int64_t t = 0; hand_in_ == nullptr && !batched && t < T;) { const float* row = embd_rows ? embd_rows[p0 + t] : nullptr; if (row) { // #1996: the picture's rows live in the caller's (pageable) memory. A run of consecutive rows is // gathered into one pinned slot and goes up as one copy; the slot is guarded by its event. const int64_t row_bytes = N * 4, run_max = std::max(1, (4 << 20) / row_bytes); int64_t t1 = t + 1; while (t1 < T && t1 - t < run_max && embd_rows[p0 + t1]) ++t1; void* stage = m.up_ring.acquire((size_t) ((t1 - t) * row_bytes)); cudaError_t e = cudaSuccess; if (stage != nullptr) { for (int64_t r = t; r < t1; ++r) std::memcpy((char*) stage + (size_t) ((r - t) * row_bytes), embd_rows[p0 + r], (size_t) row_bytes); e = m.up_ring.submit(m.emb + t * N, stage, (size_t) ((t1 - t) * row_bytes), m.cs); } else { // no pinned RAM: row by row from the caller's memory, each waited for for (int64_t r = t; r < t1 && e == cudaSuccess; ++r) e = core::PinnedRing::direct(m.emb + r * N, embd_rows[p0 + r], (size_t) row_bytes, m.cs); } if (e != cudaSuccess) { err = "prefill: the image embedding upload failed"; return false; } t = t1; } else { if (!core::embed_row(*m.wt, g, tokens[c0 + t], m.emb + t * N, m.cs, err)) return false; ++t; } } if (hand_in_ == nullptr) gr_broadcast(m.emb, m.R, T, m.cs); // ---- the PLE rows of the whole chunk, one batched SSD request on a thread (see ple_gather). A later chunk's // were read ahead during the previous chunk; the first chunk's are read beside layer 0 - they are needed from // layer 1 on, and gathering them here first left the GPU idle for the whole read (~0.4 s of a 32K prompt) 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); }); bool ple_pending = ple_on; // the chunk's rows onto the device just before layer 1 reads them, and the next chunk's gather started auto ple_land = [&]() -> bool { if (!ple_pending) return true; ple_pending = false; 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 const size_t raw_bytes = (size_t) T * strata::kernels::PLE_N_HEADS * strata::kernels::PLE_ROW_BYTES; // STRATA_PLE_COPY_STREAM: the raw rows go up on their own stream once the previous chunk's dequant has read // the buffer (ple_raw_free), and the compute stream waits for them (ple_copied) only at the dequant - the // DMA overlaps layer 0 instead of idling the compute queue. ple_copied still marks that buffer's upload // done, on whichever stream ran it, for the host-side reuse guard below. const bool ple_side = m.ple_compact[ple_buf] && m.ple_copy != nullptr; const cudaStream_t ple_up = ple_side ? m.ple_copy : m.cs; if ((ple_side && cudaStreamWaitEvent(m.ple_copy, m.ple_raw_free, 0) != cudaSuccess) || (m.ple_compact[ple_buf] ? cudaMemcpyAsync(m.ple_raw_dev, m.ple_emb_host[ple_buf], raw_bytes, cudaMemcpyHostToDevice, ple_up) : m.ple_pageable[ple_buf].empty() ? cudaMemcpyAsync(m.ple_emb, m.ple_emb_host[ple_buf], (size_t) T * N * 4, cudaMemcpyHostToDevice, m.cs) : m.up_ring.h2d(m.ple_emb, m.ple_emb_host[ple_buf], (size_t) T * N * 4, m.cs)) != cudaSuccess || cudaEventRecord(m.ple_copied[ple_buf], ple_up) != cudaSuccess || (ple_side && cudaStreamWaitEvent(m.cs, m.ple_copied[ple_buf], 0) != cudaSuccess)) { err = std::string("prefill: the PLE rows' upload failed: ") + cudaGetErrorString(cudaGetLastError()); return false; } if (m.ple_compact[ple_buf]) { strata::kernels::ple_iq4nl_dequant(m.ple_raw_dev, T * strata::kernels::PLE_N_HEADS, m.ple_emb, m.cs); if (ple_side && cudaEventRecord(m.ple_raw_free, m.cs) != cudaSuccess) { err = std::string("prefill: the PLE rows' upload failed: ") + cudaGetErrorString(cudaGetLastError()); return false; } } if (c0 + C < n) { // the other buffer's upload (a chunk ago) is done before the SSD thread refills it 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 + C, 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; }; for (int64_t t = 0; t < T; ++t) { prev[0] = prev[1]; prev[1] = (int32_t) tokens[c0 + t]; } // ---- the QSA step records of every position in the chunk for (int64_t t = 0; t < T; ++t) strata::kernels::qsa_step_fill(m.steps_host.data() + t * strata::kernels::kStepCount, p0 + t, s); if (!m.steps_up.upload(m.steps_dev, m.steps_host.data(), (size_t) T * strata::kernels::kStepCount, m.cs)) { err = "prefill: the QSA step records' upload failed"; return false; } int64_t qsa_index = 0, gdn_index = 0; for (int64_t l = 0; l < LB; ++l) (core::is_qsa_layer(g, l) ? qsa_index : gdn_index) += 1; int64_t kv_pending = -1; auto kv_prefetch_after = [&](int64_t layer, int64_t ordinal) -> bool { if (!kv_prefetch || p0 <= 0) return true; if (kv_pending >= 0) return true; // one staging pool: the pending prefetch has not been consumed yet for (int64_t l = layer; l < LE; ++l) { if (!core::is_qsa_layer(g, l)) continue; const core::QsaState& state = ss.qsa_states[ordinal]; if (state.kv_mode != 1) { ++ordinal; continue; } // Reuse one KV pool only after its last attention reader completes. The next layer's // [0, p0) host prefix is immutable throughout this chunk; its new rows are appended later. if (cudaEventRecord(m.kv_released, cs) != cudaSuccess || cudaStreamWaitEvent(m.kv_copy, m.kv_released, 0) != cudaSuccess) { err = "prefill: releasing KV staging pool"; return false; } strata::kernels::kv_stage_from_host(pools_of(m.stage, m.ident_table), state.host, core::qsa_kv_format(state), (p0 + s.page_size - 1) / s.page_size, s, m.kv_copy); if (cudaPeekAtLastError() != cudaSuccess || cudaEventRecord(m.kv_ready, m.kv_copy) != cudaSuccess) { err = "prefill: enqueueing KV prefetch"; return false; } kv_pending = ordinal; ++kv_prefetches; break; } return true; }; if (!kv_prefetch_after(LB, qsa_index)) return false; // step 3: this chunk's stream - every non-resident expert of every layer, layer by layer in id order (entry // k lands in ring slot k % ring); a copy is issued once the entry `ring` before it is consumed (its slot's // `used` event recorded), so the copy stream never waits on an event that is not queued yet const strata::kernels::cpu::ExpertLayout& lay0 = strata::kernels::cpu::expert_layout(); const bool stream_all = m.ring > STAGE && T >= stream_all_min() && m.src != nullptr; // the CPU share: this chunk takes the pool if no other stage of a layer split has it now (released at the end // of the chunk, after the last layer's CPU thread is joined) struct PoolHold { bool held = false; ~PoolHold() { if (held) g_cpu_pool_busy.store(false, std::memory_order_release); } } pool_hold; if (cpu_pool_ != nullptr && cpu_share_on() && !stream_all && !pipe_chunked && !g_share_blocked.load(std::memory_order_acquire)) { bool expect = false; pool_hold.held = g_cpu_pool_busy.compare_exchange_strong(expect, true, std::memory_order_acq_rel); } const bool ps_on = stream_all && m.pp && m.pp->ps_frac > 0.0; if (m.pp && !ps_on) m.pp->ps_flag.clear(); // multi-GPU: the peer's ring - issue its copies up to `limit` / give entries back (the peer device is current) auto p_issue_until = [&](size_t limit) { PeerPrefill& P = *m.pp; limit = std::min(limit, P.pseq.size()); while (P.p_issued < limit) { const PeerPrefill::PsEntry& en = P.pseq[P.p_issued]; const size_t sl = P.p_issued % (size_t) P.RP; if (P.plive[sl]) cudaStreamWaitEvent(P.s_cp, P.pused[sl], 0); cudaMemcpyAsync(P.pstage[sl], en.blob, (size_t) strata::kernels::cpu::expert_layout().blob_bytes(en.l), cudaMemcpyHostToDevice, P.s_cp); cudaEventRecord(P.pcopied[sl], P.s_cp); P.plive[sl] = 1; ++P.p_issued; } }; auto p_release_to = [&](int64_t l, int32_t e_stop, cudaStream_t ps) { PeerPrefill& P = *m.pp; while (P.pk < P.pseq_start[(size_t) l + 1] && P.pseq[P.pk].e < e_stop) { cudaEventRecord(P.pused[P.pk % (size_t) P.RP], ps); ++P.pk; p_issue_until(P.pk + (size_t) P.RP); } }; struct StreamEntry { int32_t l, e; const uint8_t* blob; int job; }; std::vector seq; std::vector seq_start; size_t issued = 0, consumed = 0; if (stream_all) { seq_start.assign((size_t) g.n_layers + 1, 0); std::vector js; if (ps_on) { m.pp->pseq.clear(); m.pp->pseq_start.assign((size_t) g.n_layers + 1, 0); m.pp->ps_flag.assign((size_t) g.n_layers * m.g->n_expert, 0); m.pp->p_issued = m.pp->pk = 0; } double ps_acc = 0.0; // STRATA_KVA: a layer that will not run its MoE half must not queue expert transfers either - the // transfer plan is where the avoidable cost lives (the research handoff's "expert transfers must be // skipped too"). `kva_skips_layer` is the one place the two conditions meet. auto kva_skips_layer = [&](int64_t l) { return kva_approx && l >= kva_split && (kva.only == KvaConfig::Only::All || (kva.only == KvaConfig::Only::Gdn) == !core::is_qsa_layer(g, l)); }; for (int64_t l = LB; l < LE; ++l) { seq_start[(size_t) l] = seq.size(); if (ps_on) m.pp->pseq_start[(size_t) l] = m.pp->pseq.size(); if (kva_skips_layer(l)) continue; // no transfer for a layer whose MoE half is skipped for (int32_t e = 0; e < m.g->n_expert; ++e) { if (m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0) continue; if (m.pp && m.pp->peer && m.pp->peer->has(l, e)) continue; // multi-GPU: computed on (or read from) the peer int job = -1; const uint8_t* b = nullptr; if (m.src->transient(l, e)) { // CS-T: copied by the source into the stager's buffer job = (int) js.size(); js.push_back({nullptr, (size_t) lay0.blob_bytes(l), m.src, (int32_t) l, e}); } else { b = m.src->blob_stable(l, e); if (!b) { err = "prefill: expert source has no blob"; return false; } if (ps_on && m.src->pinned(l, e)) { // multi-GPU: every ps_frac-th one goes to the peer's ring ps_acc += m.pp->ps_frac; if (ps_acc >= 1.0) { ps_acc -= 1.0; m.pp->pseq.push_back({(int32_t) l, e, b}); m.pp->ps_flag[(size_t) l * m.g->n_expert + e] = 1; continue; } } if (!m.src->pinned(l, e)) { job = (int) js.size(); js.push_back({b, (size_t) lay0.blob_bytes(l)}); } } seq.push_back({(int32_t) l, e, b, job}); } } for (int64_t l = LE; l <= g.n_layers; ++l) { seq_start[(size_t) l] = seq.size(); if (ps_on) m.pp->pseq_start[(size_t) l] = m.pp->pseq.size(); } m.stager->start(std::move(js)); } struct StagerDone { Stager* st; ~StagerDone() { if (st) st->finish(); } } chunk_stager_done{stream_all ? m.stager.get() : nullptr}; auto issue_until = [&](size_t limit) { limit = std::min(limit, seq.size()); while (issued < limit) { const StreamEntry& en = seq[issued]; const int sl = (int) (issued % (size_t) m.ring); const auto th = Clock::now(); const size_t bytes = (size_t) lay0.blob_bytes(en.l); if (m.stage_live[sl]) cudaStreamWaitEvent(m.copy, m.used[m.used_of[sl]], 0); if (en.job < 0) { cudaMemcpyAsync(m.stage_dev[sl], en.blob, bytes, cudaMemcpyHostToDevice, m.copy); ++stats_.experts_dma; } else { const uint8_t* hb = m.stager->wait(en.job); cudaMemcpyAsync(m.stage_dev[sl], hb, bytes, cudaMemcpyHostToDevice, m.copy); m.stager->issued_one(en.job, m.copy); } cudaEventRecord(m.copied[sl], m.copy); m.stage_live[sl] = true; stats_.ms_experts_host += ms_since(th); ++stats_.experts_streamed; ++issued; } }; // D-5: the stream is issued by its own host thread, so the thread launching the layers' kernels never waits // behind a host copy of an unpinned blob (that wait left the GPU idle: the 'wait copy' / 'dequant' time of the // i-quant prompts). The same copies in the same order into the same slots, and a slot is refilled only once // the compute stream has recorded that it is done with it: the same results. STRATA_PREFILL_ISSUER=0: inline. static const bool issuer_on = [] { const char* v = std::getenv("STRATA_PREFILL_ISSUER"); return v == nullptr || std::atoi(v) != 0; }(); static const int copy_event_every = [] { const char* v = std::getenv("STRATA_PREFILL_COPY_EVENT_EVERY"); return strata::prefill::copy_event_every_from(v ? std::atoi(v) : 1); }(); static const bool copy_wait_dedupe = [] { const char* v = std::getenv("STRATA_PREFILL_COPY_WAIT_DEDUPE"); return v != nullptr && std::atoi(v) != 0; }(); // STRATA_PREFILL_ROUTED_STREAM=1 (opt-in, routed_stream.hpp): the issuer keeps at most a few copies queued // ahead of the link (STRATA_PREFILL_ROUTED_STREAM_DEPTH, default 16), each layer's routing is published once // its router has run (host grouping below), and an entry not yet issued that no token of the chunk routes to // is dropped. Routed entries are copied as below, into the same slots: the same bits. The threaded issuer // on one GPU only (no peer prompt ring), and not with STRATA_PREFILL_COPY_EVENT_EVERY > 1 or // STRATA_PREFILL_COPY_WAIT_DEDUPE=1, whose copy records the routed walk does not write (routed_stream::eligible); // unset, nothing here runs and the loop below is the walk. Declared // before the issuer thread, so they outlive it on every return (IssuerJoin joins it first). static const bool rs_env = routed_stream::enabled_from(std::getenv("STRATA_PREFILL_ROUTED_STREAM")); static const int rs_depth = routed_stream::depth_from(std::getenv("STRATA_PREFILL_ROUTED_STREAM_DEPTH")); const bool rs_on = rs_env && routed_stream::eligible( stream_all, issuer_on, m.pp != nullptr, copy_event_every, copy_wait_dedupe); routed_stream::RouteGate rs_gate; size_t rs_dropped = 0; if (rs_on) { rs_gate.reset(g.n_layers, (int32_t) m.g->n_expert); if (static bool said = false; !said) { said = true; std::fprintf(stderr, "strata: STRATA_PREFILL_ROUTED_STREAM=1: a streamed prompt chunk drops the experts no token " "routes to before they are copied (at most %d copies queued ahead of the link)\n", rs_depth); } } else if (rs_env && stream_all) { if (static bool said_off = false; !said_off) { said_off = true; std::fprintf(stderr, "strata: STRATA_PREFILL_ROUTED_STREAM=1 is not used: %s\n", routed_stream::held_off(issuer_on, m.pp != nullptr, copy_event_every, copy_wait_dedupe)); } } std::atomic a_issued{0}, a_consumed{0}; std::atomic a_stop{false}; double iss_ms = 0; int64_t iss_streamed = 0, iss_dma = 0; std::thread issuer; struct IssuerJoin { std::atomic* stop; std::thread* t; ~IssuerJoin() { if (t->joinable()) { stop->store(true); t->join(); } } } issuer_join{&a_stop, &issuer}; const bool threaded_issue = stream_all && issuer_on; // Opt-in, the threaded issuer only: under WDDM every cudaEventRecord and cross-stream wait is a // submission of its own (~80 / ~45 us each on a 6900 XT under Windows 11, ~3 / ~0 us on Linux), and the // streamed walk paid one record and one wait per expert although a group gathers after ONE wait on its last // copy and is released by ONE event. STRATA_PREFILL_COPY_EVENT_EVERY=N records the copied event every N-th // entry (and before the issuer waits, and on the last), and the consumer waits on the first recorded entry at or // after the one it needs (the copy stream is in order). STRATA_PREFILL_COPY_WAIT_DEDUPE=1 skips the copy // stream's wait on a slot's release event when the previous slot was released by the same event (one flush). // The same copies into the same slots: the same bytes. const bool sparse_copied = threaded_issue && copy_event_every > 1; if (threaded_issue && (sparse_copied || copy_wait_dedupe)) { static bool said = false; if (!said) { said = true; std::fprintf(stderr, "strata: prompt copies: a copied event every %d streamed experts " "(STRATA_PREFILL_COPY_EVENT_EVERY), %s (STRATA_PREFILL_COPY_WAIT_DEDUPE)\n", copy_event_every, copy_wait_dedupe ? "one wait per release event" : "a wait per slot"); } } std::vector> rec_of(sparse_copied ? (size_t) m.ring : 0); // the entry a slot's event marks for (auto& r : rec_of) r.store(-1, std::memory_order_relaxed); std::atomic a_rec{0}; // 1 + the last entry whose copied event is recorded int64_t iss_waits = 0, iss_dedup = 0; if (threaded_issue) { issuer = std::thread([&] { strata::platform::name_this_thread("strata-issue"); strata::aux_cpus::pin_current_thread(); const core::OnDevice od(m.device); if (rs_on) { auto ops = routed_stream::make_ops( [&] { return a_consumed.load(std::memory_order_acquire); }, [&] { return a_stop.load(std::memory_order_acquire); }, [&](int sl) { return cudaEventQuery(m.copied[sl]) != cudaErrorNotReady; }, [](const StreamEntry& en) { return en.job < 0; }, [&](size_t idx, int sl) { // the copy of the loop below, unchanged const StreamEntry& en = seq[idx]; const auto th = Clock::now(); const size_t bytes = (size_t) lay0.blob_bytes(en.l); if (m.stage_live[sl]) cudaStreamWaitEvent(m.copy, m.used[m.used_of[sl]], 0); if (en.job < 0) { cudaMemcpyAsync(m.stage_dev[sl], en.blob, bytes, cudaMemcpyHostToDevice, m.copy); ++iss_dma; } else { const uint8_t* hb = m.stager->wait(en.job); cudaMemcpyAsync(m.stage_dev[sl], hb, bytes, cudaMemcpyHostToDevice, m.copy); m.stager->issued_one(en.job, m.copy); } cudaEventRecord(m.copied[sl], m.copy); m.stage_live[sl] = true; iss_ms += ms_since(th); ++iss_streamed; }, [] { std::this_thread::yield(); }); rs_dropped = routed_stream::issue_walk(seq, (size_t) m.ring, rs_depth, rs_gate, a_issued, ops).dropped; return; } int64_t last_rec = -1; // sparse_copied: the last entry whose copied event is recorded int prev_rel = -1; // dedupe: the release event the copy stream waited on for the previous entry auto record = [&](size_t i) { cudaEventRecord(m.copied[i % (size_t) m.ring], m.copy); rec_of[i % (size_t) m.ring].store((int64_t) i, std::memory_order_release); a_rec.store(i + 1, std::memory_order_release); last_rec = (int64_t) i; }; // before the issuer waits: what it issued is covered, so no consumer waits on an entry with no event auto cover = [&](size_t idx) { if (sparse_copied && idx > 0 && last_rec < (int64_t) idx - 1) record(idx - 1); }; for (size_t idx = 0; idx < seq.size(); ++idx) { while (idx >= a_consumed.load(std::memory_order_acquire) + (size_t) m.ring) { cover(idx); if (a_stop.load(std::memory_order_acquire)) return; std::this_thread::yield(); } const StreamEntry& en = seq[idx]; const int sl = (int) (idx % (size_t) m.ring); const auto th = Clock::now(); const size_t bytes = (size_t) lay0.blob_bytes(en.l); if (m.stage_live[sl]) { // Both this slot and the previous one must have been released in THIS walk (idx - 1 >= ring: the // previous slot's event of an earlier walk could be re-recorded by this walk's first flush), and // the ring must be longer than one MMQ group (a flush of a whole ring would re-record the event // after the previous slot's wait). Then the same event means the same flush, and the wait for // the previous slot already covers this one. const int rel = m.used_of[sl]; if (copy_wait_dedupe && strata::prefill::copy_wait_dedupe_ok(idx, m.ring, MMQ_GROUP, rel, prev_rel)) { ++iss_dedup; } else { cudaStreamWaitEvent(m.copy, m.used[rel], 0); ++iss_waits; } prev_rel = rel; } else { prev_rel = -1; } if (en.job < 0) { cudaMemcpyAsync(m.stage_dev[sl], en.blob, bytes, cudaMemcpyHostToDevice, m.copy); ++iss_dma; } else { cover(idx); // the stager may make the issuer wait const uint8_t* hb = m.stager->wait(en.job); cudaMemcpyAsync(m.stage_dev[sl], hb, bytes, cudaMemcpyHostToDevice, m.copy); m.stager->issued_one(en.job, m.copy); } if (!sparse_copied) cudaEventRecord(m.copied[sl], m.copy); else if (strata::prefill::copy_event_due(idx, seq.size(), copy_event_every)) record(idx); m.stage_live[sl] = true; iss_ms += ms_since(th); ++iss_streamed; a_issued.store(idx + 1, std::memory_order_release); } }); } else if (stream_all) { issue_until((size_t) m.ring); // layer 0's first experts, behind the embedding and the PLE } // the consumer's side: entry k's copy is on the copy stream (the thread issued it), then k is given back auto wait_issued = [&](size_t k) { if (!threaded_issue) return; while (a_issued.load(std::memory_order_acquire) <= k) std::this_thread::yield(); }; // the compute stream waits for entry k's copy: on its slot's event (`slot`, as before this switch existed), or // (sparse_copied) on the first recorded entry at or after it. That event's slot cannot be re-recorded before k // is given back (the issuer stays a ring ahead). auto wait_copied = [&](size_t k, size_t slot) { if (!sparse_copied) { cudaStreamWaitEvent(m.cs, m.copied[slot], 0); return; } while (a_rec.load(std::memory_order_acquire) <= k) std::this_thread::yield(); const size_t hi = a_rec.load(std::memory_order_acquire); // (hi - 1 is always recorded, so the last entry is only the fallback that covers k all the same) const size_t r = strata::prefill::copy_event_entry(k, hi, [&](size_t e) { return rec_of[e % (size_t) m.ring].load(std::memory_order_acquire) == (int64_t) e; }); cudaStreamWaitEvent(m.cs, m.copied[r % (size_t) m.ring], 0); }; auto give_back = [&](size_t upto) { if (threaded_issue) a_consumed.store(upto, std::memory_order_release); else issue_until(upto + (size_t) m.ring); }; if (ps_on) { int pd = 0; cudaGetDevice(&pd); cudaSetDevice(m.pp->dev); p_issue_until((size_t) m.pp->RP); cudaSetDevice(pd); } host_setup_ms += ms_since(tsetup); bool normed = false; // F-2: the previous half's write already normed R for this half (grs, xn16) // #579 #613 (opt-in diagnosis, STRATA_PF_STEP_SYNC=1): the compute and copy streams are waited for after each // step named below, a step that took over 250 ms is logged, and a stall's report names the step it is in. // Slower (a sync per step); the bytes are the same. static const bool step_sync = [] { const char* e = std::getenv("STRATA_PF_STEP_SYNC"); return e && e[0] == '1'; }(); auto pf_step = [&](const char* what, int64_t layer) { if (!step_sync) return; core::progress_at(what, layer, p0); const auto ts = Clock::now(); const cudaError_t a = cudaStreamSynchronize(m.cs), b = cudaStreamSynchronize(m.copy); const double ms = ms_since(ts); if (ms > 250.0 || a != cudaSuccess || b != cudaSuccess) std::fprintf(stderr, "strata pf-step: chunk from token %lld, layer %lld: %s took %.0f ms (%s / %s)\n", (long long) p0, (long long) layer, what, ms, cudaGetErrorString(a), cudaGetErrorString(b)); }; for (int64_t l = LB; l < LE; ++l) { core::progress_beat(); // the serve watchdog: a prompt chunk of 8192 tokens is still moving if (l > LB) pf_step("reading the prompt (batched, step sync): the experts and the rest of layer", l - 1); core::progress_at("reading the prompt (batched): layer", l, p0); // #251: a stall names layer and chunk const core::LayerView v(*m.wt, l); // STRATA_KVA: `kva_layer` means this layer's output has no consumer left for this chunk, so only its // state advances; `kva_subst` means the block input itself is replayed from a capture (oracle mode). const bool kva_layer = kva_approx && l >= kva_split && (kva.only == KvaConfig::Only::All || (kva.only == KvaConfig::Only::Gdn) == !core::is_qsa_layer(g, l)); // STRATA_KVA_SKIP=moe: the layer's attention half (GDN or QSA, outputs included) runs in full and only the MoE // half is skipped; `kva_state_only` is the default form, where only the state advances const bool kva_moe_only = kva_layer && kva.skip == KvaConfig::Skip::Moe; const bool kva_state_only = kva_layer && !kva_moe_only; const bool kva_subst = kva_layer && kva.mode == KvaConfig::Mode::Oracle; const bool kva_cap_layer = kva.capture && LB == 0 && (LE < 0 || LE == g.n_layers) && l >= kva_split; if (l == std::max(LB, 1)) { // the PLE rows, read from layer 1 on (a wait for the SSD read of the first chunk lands on "ple") pt.mark(kPfPle, cs); if (!ple_land()) return false; } // ---- the PLE block at layer 1, token by token (its conv reads the previous tokens' rows) if (l == 1 && ple_on && ple_batch) { // the whole chunk at once, in sub-batches carved from the idle scratch region: the key and value // projections as GEMMs (a token at a time they re-read ~52 MB of BF16 key per token on the IQ // files), the rest with the per-token kernels' arithmetic (native_ple_postops_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 n) { float* p = (float*) q; q += (n * 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, m.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, m.cs); m.gemm.native(e16, pw.key_native_type, pw.key_native_data, key, nb, HD, N); to_bf16(emb, e16, nb * N, m.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, m.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, m.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, m.cs); } stats_.ms_ple += ms_since(tp); } // STRATA_KVA: the capture and the oracle hook the residual ENTERING the layer - the vector a projector // has to produce for it. A capture run is an ordinary full read; the oracle replaces the input. if (kva_cap_layer && !kva_capture.write(l, m.R, T, p0, D, cs)) { err = kva_capture.err; return false; } // STRATA_LAT_R_DUMP= (latent debug): the residual entering the layer, fp32, rows in // position order, appended to /L.bin per chunk - offline layer-by-layer diff. static const char* lat_r_dump = std::getenv("STRATA_LAT_R_DUMP"); if (lat_r_dump) { static std::vector lat_r_host; lat_r_host.resize((size_t) T * D); if (cudaMemcpyAsync(lat_r_host.data(), m.R, (size_t) T * D * 4, cudaMemcpyDeviceToHost, m.cs) != cudaSuccess || cudaStreamSynchronize(m.cs) != cudaSuccess) { err = "lat R dump: the residual copy failed"; return false; } char lat_r_path[512]; std::snprintf(lat_r_path, sizeof lat_r_path, "%s/L%lld.bin", lat_r_dump, (long long) l); std::FILE* lat_r_f = std::fopen(lat_r_path, "wb"); if (lat_r_f == nullptr) { err = std::string("lat R dump: cannot write ") + lat_r_path; return false; } std::fwrite(lat_r_host.data(), 4, (size_t) T * D, lat_r_f); std::fclose(lat_r_f); } if (kva_subst) { if (!kva_oracle.read(l, m.R, T, p0, D, cs)) { err = kva_oracle.err; return false; } normed = false; // the previous half's norm of R is stale once the input is replaced } for (int half = 0; half < 2; ++half) { if (kva_moe_only && half == 1) { // KVA moe-skip: the attention half wrote its output into R; this layer's MoE half has no consumer // left for an approximated chunk, so it (and its expert transfers) is skipped. R is not normed for // it (the write after the attention half normed it with this layer's FFN weights). normed = false; consumed = std::max(consumed, seq_start[(size_t) l + 1]); give_back(consumed); break; } // ---- the hyper-connection read of this half 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: 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, m.cs, 0, 0); wu_q8.data = core::NativeDense::hc_bf16_view(*wu, m.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); // the xn16 token stride of this chunk (the writers of both halves and of the fused write-backs use it) const int64_t ldx = hc_pad() && T >= std::max(pf_switch_min_t(), 64) && !gr_unfused() && !m.xn16_lo ? D + XN_PAD : D; const bool xf16 = m.xn_f16 && !m.xn16_lo && ldx == D; // xn16 holds FP16 (its writers and readers below) if (gr_unfused()) gr_norm(m.R, (const float*) wn->data, EPS, m.xn, m.xn16, T, m.cs, m.xn16_lo, xf16); else if (!normed) gr_norm_rs(m.R, (const float*) wn->data, EPS, m.grs, m.xn16, T, m.cs, m.xn16_lo, ldx, xf16); normed = false; pt.mark(kPfHcDown, cs); // STRATA_PREFILL_TIMING: the hyper-connection read's parts on their own lines (norm stays on "hc read") bool hcd = false; bool hdown = false; if (wd->q8_scale == nullptr && ldx != D && !pf_hcdown()) { // not with STRATA_HC_REQ8's int8 rows // STRATA_HCD_EXACT: the exact-order down kernel when hipBLASLt would take 1176 / 1177 hdown = wd->kind == core::WeightKind::Bf16InF32 && wd->data && wd->ne0 == D && wd->ne1 == LR && m.gemm.bf16_hcd_exact(m.xn16, ldx, (const uint16_t*) wd->data, m.lo, T, LR, D); } else if (wd->q8_scale == nullptr && ldx != D) { if (wd->kind != core::WeightKind::Bf16InF32 || !wd->data || wd->ne0 != D || wd->ne1 != LR || wi->kind != core::WeightKind::Bf16InF32 || !wi->data || wi->ne0 != D || wi->ne1 != HC || !strata_pf_hcdown_bf16(m.xn16, ldx, (const uint16_t*) wd->data, (const uint16_t*) wi->data, LR, HC, m.lo, m.inj, T, D, m.cs)) { err = "prefill: STRATA_PF_HCDOWN could not run the hyper-connection projections"; return false; } hcd = true; } if (!hcd && !hdown && !bf16_proj(m.gemm, wd, m.xn16, m.lo, T, sd, err, 0, m.xn16_lo, ldx != D ? ldx : 0, m.cs, xf16)) return false; gr_silu(m.lo, m.lo16, T, m.cs, m.lo16_lo); pt.mark(kPfHcUp, cs); bool upmixed = false; // on the FP16 prompt path (m.f16_io) lo16 holds FP16 bits (act16) and gr_upmix reads it on the FP16 matrix-core builtin (g_act_f16) if (wu->q8_scale == nullptr && hc_upmix() && T >= std::max(pf_switch_min_t(), hc_upmix_min_t()) && !gr_unfused() && !m.lo16_lo && !m.mixed_bf_lo && wu->kind == core::WeightKind::Bf16InF32 && wu->data && wu->ne0 == LR && wu->ne1 == D) { static int checks = [] { const char* e = std::getenv("STRATA_HC_UPMIX_CHECK"); return e ? std::atoi(e) : 0; }(); if (checks > 0) { // the default pair first, kept for the comparison --checks; if (!bf16_proj(m.gemm, wu, m.lo16, m.gated, T, su, err)) return false; gr_mix_r(m.R, m.grs, (const float*) wn->data, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h); std::vector ref((size_t) T * N), got((size_t) T * N); cudaMemcpyAsync(ref.data(), m.mixed, ref.size() * 4, cudaMemcpyDeviceToHost, m.cs); upmixed = gr_upmix(m.lo16, (const uint16_t*) wu->data, m.R, m.grs, (const float*) wn->data, m.mixed, m.mixed_bf, m.mixed_h, T, m.cs); cudaMemcpyAsync(got.data(), m.mixed, got.size() * 4, cudaMemcpyDeviceToHost, m.cs); cudaStreamSynchronize(m.cs); double e2 = 0, r2 = 0, emax = 0; for (size_t i = 0; i < ref.size(); ++i) { const double dd = (double) got[i] - ref[i]; e2 += dd * dd; r2 += (double) ref[i] * ref[i]; emax = std::max(emax, std::fabs(dd)); } std::fprintf(stderr, "strata: STRATA_HC_UPMIX_CHECK layer %lld half %d T %lld: mixed rel RMS %.3e, " "max |diff| %.3e (%s)\n", (long long) l, half, (long long) T, std::sqrt(e2 / std::max(r2, 1e-30)), emax, upmixed ? "upmix" : "upmix refused"); } else { upmixed = gr_upmix(m.lo16, (const uint16_t*) wu->data, m.R, m.grs, (const float*) wn->data, m.mixed, m.mixed_bf, m.mixed_h, T, m.cs); } } if (!upmixed && !bf16_proj(m.gemm, wu, m.lo16, m.gated, T, su, err, 0, m.lo16_lo, 0, m.cs)) return false; pt.mark(kPfHcInj, cs); if (!hcd && !bf16_proj(m.gemm, wi, m.xn16, m.inj, T, si, err, 0, m.xn16_lo, ldx != D ? ldx : 0, m.cs, xf16)) return false; pt.mark(kPfHcMix, cs); if (upmixed) { } else if (gr_unfused()) { gr_mix(m.xn, m.gated, m.mixed, m.mixed_bf, T, m.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, m.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(m.cs, 0, 0); core::NativeDense::hc_bf16_release(m.cs, 1, 0); } if (half == 0 && !core::is_qsa_layer(g, l)) { // ======================= GDN ======================= 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; // KVA: the gate projection feeds only the output normalization, which a state-only layer skips // (STRATA_KVA_GDN_NORM=1 keeps the full recurrence as the A/B control for the state-only one) static const bool kva_gdn_norm = [] { const char* v = std::getenv("STRATA_KVA_GDN_NORM"); return v != nullptr && std::atoi(v) != 0; }(); if ((!kva_state_only || kva_gdn_norm) && !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" is the projections in; the rest on their own lines gdn_gates(m.ab, (const float*) wdt->data, (const float*) wsa->data, m.gate, m.beta, T, m.cs); const int64_t ld_y = pf_pad() && T >= std::max(pf_switch_min_t(), 64) ? ZV + ZV_PAD : 0; bool fused_gdn = false; #if defined(STRATA_USE_HIP) // gfx12, STRATA_GDN_CHUNKED=1: conv and recurrence together, the conv output as FP16 (their time then shows in "gdn conv+gates") if (!kva_state_only) fused_gdn = gdn_conv_recurrence_fused(conv, m.qkv, (const float*) wc->data, m.hbuf, state, m.gate, m.beta, m.z, (const float*) wnm->data, EPS, EPS, m.y, m.y_h, T, m.cs, ld_y); #endif if (!fused_gdn) gdn_conv(conv, m.qkv, (const float*) wc->data, m.hbuf, T, EPS, m.cs); pt.mark(kPfGdnRec, cs); if (fused_gdn) { } else if (kva_state_only && !kva_gdn_norm) // KVA: the recurrence without the output normalization gdn_recurrence_state_only(state, m.hbuf, m.gate, m.beta, m.y, T, m.cs); else gdn_recurrence(state, m.hbuf, m.gate, m.beta, m.z, (const float*) wnm->data, EPS, m.y, m.y_h, T, m.cs, ld_y); pt.mark(kPfGdnOut, cs); if (!kva_state_only) // KVA: the out projection feeds only the residual write and the MoE half if (!native_proj(m.gemm, wo, m.y_h, m.bo, T, v.name("ssm_out.weight"), err, 0, ld_y)) return false; ++gdn_index; if (kva_state_only) { // the layer consumed no expert: its stream entries must still be released, or the issuer // thread fills the ring and the next fully computed layer waits for a copy that never comes consumed = std::max(consumed, seq_start[(size_t) l + 1]); give_back(consumed); break; // state advanced: no gated residual write, no MoE half of this layer } } else if (half == 0) { // ======================= QSA ======================= 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; // KVA: the query projections have no consumer when only the KV/indexer state advances if (!kva_state_only && !native_proj(m.gemm, wq, m.mixed_h, m.Qf, T, v.name("attn_q.weight"), err)) return false; pt.mark(kPfQsaIdxG, cs); // STRATA_PREFILL_TIMING: "qsa proj" is the k, v and q projections 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 (!kva_state_only && !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; pt.mark(kPfQsaPrep, cs); if (qsa_fuse_nr()) { rms_rope_rows(m.Kc, kKvW, kHeadDim, m.Kc, kKvW, (const float*) wkn->data, T, kNKv, kHeadDim, EPS, p0, strata::kernels::rope_scaling(), m.cs); } else { rms_rows(m.Kc, (const float*) wkn->data, T * 2, 256, 256, EPS, m.cs); rope(m.Kc, T, kNKv, kHeadDim, kKvW, p0, strata::kernels::rope_scaling(), m.cs); } // KV streaming: this layer's cells [0, p0) come in from the host copy to the staging pool, and the // chunk's cells go to the host copy, the staging pool, and the VRAM slots of resident blocks const bool staged = st.kv_mode == 1; if (staged) { pt.mark(kPfKvStage, cs); if (kv_pending == qsa_index) { if (cudaStreamWaitEvent(cs, m.kv_ready, 0) != cudaSuccess) { err = "prefill: waiting for KV prefetch"; return false; } kv_pending = -1; } else 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, m.cs); pt.mark(kPfQsa, cs); pf_step("reading the prompt (batched, step sync): the K/V staged from RAM at layer", l); } // #579 #613 (HIP, opt-in A/B, STRATA_KV_HOST_DMA=1): the append writes the staging pool and the // resident slots only, and one DMA copies the chunk's blocks from the staging pool to the host copy // - no kernel writes host memory over PCIe. The bytes every reader sees are the same (the staged // first block is complete; past the chunk's last cell nothing is read until a later append writes // it). CUDA: the default (core/kv_host_dma.hpp: the kernel's own stores to the host copy ran at // about 0.24 GB/s, 50 us a prompt token); STRATA_KV_HOST_DMA=0 gives the kernel's stores back. #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 static const bool kv_host_dma = core::kv_host_dma_on(std::getenv("STRATA_KV_HOST_DMA"), true); #endif const bool host_by_dma = core::kv_host_dma_use(kv_host_dma, staged, m.stage.present()); const strata::kernels::KvHostPools* host_w = host_by_dma ? nullptr : &st.host; if (st.kv_hybrid) { // K8V4: K INT8 unrotated, V rotated Q4_0 (only V and the output rotate) // streamed: each half writes its part of the host copy and of the staging pool strata::kernels::fwht256_inplace_cuda(m.Vc, T * 2, m.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, m.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, m.cs, mirror ? &hv : nullptr, staged ? &sv : nullptr); } else { if (st.kv_rot) { // rotated K and V (kv_q4.hpp), the queries below too, the output back strata::kernels::fwht256_inplace_cuda(m.Kc, T * 2, m.cs); strata::kernels::fwht256_inplace_cuda(m.Vc, T * 2, m.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, m.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, m.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, m.cs); } if (staged) pf_step("reading the prompt (batched, step sync): the K/V append at layer", l); if (qsa_fuse_nr()) { rms_rope_rows(m.Qf, kNHead * 2 * kHeadDim, 2 * kHeadDim, m.q, kQW, (const float*) wqn->data, T, kNHead, kHeadDim, EPS, p0, strata::kernels::rope_scaling(), m.cs); } else { split_q(m.Qf, m.q, T, m.cs); rms_rows(m.q, (const float*) wqn->data, T * kNHead, kHeadDim, kHeadDim, EPS, m.cs); rope(m.q, T, kNHead, kHeadDim, kQW, p0, strata::kernels::rope_scaling(), m.cs); } if (st.kv_rot) strata::kernels::fwht256_inplace_cuda(m.q, T * kNHead, m.cs); if (qsa_fuse_nr()) { rms_rope_rows(m.q_idx, kIdxQW, kIdxK, m.q_idx, kIdxQW, (const float*) wiqn->data, T, kIdxQHeads, kIdxK, EPS, p0, strata::kernels::rope_scaling(), m.cs); } else { rms_rows(m.q_idx, (const float*) wiqn->data, T * 4, 128, 128, EPS, m.cs); rope(m.q_idx, T, kIdxQHeads, kIdxK, kIdxQW, p0, strata::kernels::rope_scaling(), m.cs); } // the indexer appends, token by token; then scores + selection for many queries at once: // a query reads completed blocks (final once completed) and `dead` for its own tail block const strata::kernels::QsaIndexerBuffers ib{st.idx_tail, st.idx_dead, st.idx_pooled, st.idx_block_pos}; pt.mark(kPfQsaIdx, cs); // C-2: the chunk's appends in three launches instead of one per token (the same end state: // the queries below read it only after the whole chunk is appended). STRATA_INDEXER_PER_TOKEN=1: the old 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(), m.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(), m.cs); } } catch (const std::exception& e) { err = std::string("prefill indexer: ") + e.what(); return false; } // KVA: the KV pages and the indexer's keys/pool are complete for this chunk. Nothing that // reads them (query side, selection, attention, output projection, the MoE half) has a // consumer left, so the layer ends here. if (kva_state_only) { ++qsa_index; if (!kv_prefetch_after(l + 1, qsa_index)) return false; // no expert is consumed here: release the stream entries so the issuer cannot stall consumed = std::max(consumed, seq_start[(size_t) l + 1]); give_back(consumed); break; } pt.mark(kPfQsaSel, cs); #if !defined(STRATA_GPU_METAL) // STRATA_SELECT_TWO_PASS=1 (qsa_two_pass.hpp): the tensor-core scorer's calls whose last query has // STRATA_SELECT_TWO_PASS_MIN blocks or more score every block in single-pass TF32 and again in 3xTF32 // only where the error bound reaches the cut-off; qsa_block_topk below returns the same cells. Its // scratch is m.attn, which the attention below writes and nothing reads before; the key norms are // this layer's and chunk's (the indexer appends above are complete) static const bool tp_on = strata::kernels::qsa_two_pass_on(); static const bool tp_check = tp_on && strata::kernels::qsa_two_pass_check_on(); static const int64_t tp_min = strata::kernels::qsa_two_pass_min_blocks(); const uint64_t tp_bytes = (uint64_t) m.T * ZV * sizeof(float); const int64_t tp_nbid = T > 0 ? (int64_t) m.steps_host[(size_t) ((T - 1) * strata::kernels::kStepCount + strata::kernels::kStepNBid)] : 0; const bool tp_ok = tp_on && tp_nbid + 1 >= tp_min && strata::kernels::qsa_two_pass_keys(st.idx_pooled, tp_nbid, m.sel_batch, m.max_blocks, m.attn, tp_bytes, m.cs); #endif for (int64_t t0 = 0; t0 < T; t0 += m.sel_batch) { const int64_t nb = std::min(m.sel_batch, T - t0); const int32_t* steps0 = m.steps_dev + t0 * strata::kernels::kStepCount; // C-1: the grid reaches the batch's last query's n_bid (they rise with the position) const int64_t active = (int64_t) m.steps_host[(size_t) ((t0 + nb - 1) * strata::kernels::kStepCount + strata::kernels::kStepNBid)] + 1; // the scores on tensor cores (3xTF32: FP32-level, not bitwise), below sm_80 the FP32 tiled kernel with STRATA_SELECT_SIMT=1; // STRATA_SELECT_OLD=1: the warp kernel static const bool old_sel = std::getenv("STRATA_SELECT_OLD") != nullptr; bool tp_call = false; #if !defined(STRATA_GPU_METAL) tp_call = tp_ok && !old_sel && active >= tp_min && strata::kernels::qsa_block_scores_two_pass(st.idx_pooled, st.idx_dead, m.q_idx + t0 * kIdxQW, steps0, nb, m.sel_batch, m.max_blocks, s, m.sel_scores, m.attn, tp_bytes, m.cs, active); #endif if (!tp_call && (old_sel || !strata::kernels::qsa_block_scores_tc(st.idx_pooled, st.idx_dead, m.q_idx + t0 * kIdxQW, steps0, nb, m.max_blocks, s, m.sel_scores, m.cs, active))) strata::kernels::qsa_block_scores(st.idx_pooled, st.idx_dead, m.q_idx + t0 * kIdxQW, steps0, nb, m.max_blocks, s, m.sel_scores, m.cs, active); strata::kernels::qsa_block_topk(m.sel_scores, steps0, nb, m.max_blocks, m.cap, s, m.sel_ids + t0 * m.cap, m.cs, active); #if !defined(STRATA_GPU_METAL) // STRATA_SELECT_TWO_PASS_CHECK=1 (diagnostic, one GPU): today's scores and ids for the same // queries, compared with the two-pass ones (counts at the prompt's end). Their buffers are the // rest of m.attn past the two-pass scratch when it is large enough (the attention below writes // m.attn after the comparison), else buffers of their own if (tp_call && tp_check) { static float* own_sc = nullptr; static int32_t* own_ids = nullptr; static size_t own_sc_n = 0, own_ids_n = 0; const size_t want_sc = (size_t) m.sel_batch * (size_t) m.max_blocks, want_ids = (size_t) m.sel_batch * (size_t) m.cap; const uint64_t at = (strata::kernels::qsa_two_pass_scratch_bytes(m.sel_batch, m.max_blocks) + 255u) & ~uint64_t(255u); const uint64_t at_ids = at + (((uint64_t) want_sc * 4u + 255u) & ~uint64_t(255u)); float* ref_sc = nullptr; int32_t* ref_ids = nullptr; if (at_ids + (uint64_t) want_ids * 4u <= tp_bytes) { ref_sc = reinterpret_cast(reinterpret_cast(m.attn) + at); ref_ids = reinterpret_cast(reinterpret_cast(m.attn) + at_ids); } else { if (want_sc > own_sc_n || want_ids > own_ids_n) { cudaStreamSynchronize(m.cs); cudaFree(own_sc); cudaFree(own_ids); own_sc = nullptr; own_ids = nullptr; own_sc_n = own_ids_n = 0; if (cudaMalloc((void**) &own_sc, want_sc * 4) != cudaSuccess || cudaMalloc((void**) &own_ids, want_ids * 4) != cudaSuccess) { err = "STRATA_SELECT_TWO_PASS_CHECK: no room for its buffers"; return false; } own_sc_n = want_sc; own_ids_n = want_ids; } ref_sc = own_sc; ref_ids = own_ids; } if (!strata::kernels::qsa_block_scores_tc(st.idx_pooled, st.idx_dead, m.q_idx + t0 * kIdxQW, steps0, nb, m.max_blocks, s, ref_sc, m.cs, active)) { err = "STRATA_SELECT_TWO_PASS_CHECK: the tensor-core scorer did not run"; return false; } strata::kernels::qsa_block_topk(ref_sc, steps0, nb, m.max_blocks, m.cap, s, ref_ids, m.cs, active); strata::kernels::qsa_two_pass_check(m.sel_scores, m.sel_ids + t0 * m.cap, ref_sc, ref_ids, steps0, nb, m.sel_batch, m.max_blocks, m.cap, m.attn, m.cs); } #endif } // STRATA_SEL_OVERLAP (debug, D-1's question): how much do neighbouring queries' selections share? // Per tile of 16 queries: the union of their selected cells against the sum of their widths. if (static const bool ovl = std::getenv("STRATA_SEL_OVERLAP") != nullptr; ovl && qsa_index == 0) { std::vector ids((size_t) (T * m.cap)); cudaMemcpyAsync(ids.data(), m.sel_ids, ids.size() * 4, cudaMemcpyDeviceToHost, m.cs); cudaStreamSynchronize(m.cs); double sum_w = 0, sum_u = 0; for (int64_t t0 = 0; t0 + 16 <= T; t0 += 16) { std::vector u; for (int64_t t = t0; t < t0 + 16; ++t) { const int64_t w = m.steps_host[(size_t) (t * strata::kernels::kStepCount + strata::kernels::kStepWidth)]; sum_w += (double) w; u.insert(u.end(), ids.begin() + t * m.cap, ids.begin() + t * m.cap + w); } std::sort(u.begin(), u.end()); sum_u += (double) (std::unique(u.begin(), u.end()) - u.begin()); } std::fprintf(stderr, "strata prefill: selection overlap at %lld: 16-query tiles read %.1f%% of the " "cells one query at a time does\n", (long long) p0, sum_w > 0 ? 100.0 * sum_u / sum_w : 0.0); } // STRATA_IDX_FP16_CHECK: would FP16 pooled indexer keys select the same cells? (the KV-streaming // design's last question). Every query is selected again from the pooled keys and `dead` rounded // to fp16 (exactly what an fp16 store reads back); the agreement with the fp32 selection is // printed cumulatively after each chunk's last QSA layer. Debug: syncs per layer. if (static const bool f16chk = std::getenv("STRATA_IDX_FP16_CHECK") != nullptr; f16chk) { static float *pooled16 = nullptr, *dead16 = nullptr; static int32_t* ids16 = nullptr; static double shared = 0, cells = 0; static long long queries = 0, same = 0, sel_queries = 0; const int64_t rows = st.idx_pooled_rows; if (pooled16 == nullptr && (cudaMalloc((void**) &pooled16, (size_t) rows * s.idx_dim * 4) != cudaSuccess || cudaMalloc((void**) &dead16, (size_t) s.idx_dim * 4) != cudaSuccess || cudaMalloc((void**) &ids16, (size_t) (m.T * m.cap) * 4) != cudaSuccess)) { err = "STRATA_IDX_FP16_CHECK: no room for its buffers"; return false; } round_f16(st.idx_pooled, pooled16, rows * s.idx_dim, m.cs); round_f16(st.idx_dead, dead16, s.idx_dim, m.cs); for (int64_t t0 = 0; t0 < T; t0 += m.sel_batch) { const int64_t nb = std::min(m.sel_batch, T - t0); const int32_t* steps0 = m.steps_dev + t0 * strata::kernels::kStepCount; strata::kernels::qsa_block_scores(pooled16, dead16, m.q_idx + t0 * kIdxQW, steps0, nb, m.max_blocks, s, m.sel_scores, m.cs); strata::kernels::qsa_block_topk(m.sel_scores, steps0, nb, m.max_blocks, m.cap, s, ids16 + t0 * m.cap, m.cs); } std::vector a((size_t) (T * m.cap)), b((size_t) (T * m.cap)); cudaMemcpyAsync(a.data(), m.sel_ids, a.size() * 4, cudaMemcpyDeviceToHost, m.cs); cudaMemcpyAsync(b.data(), ids16, b.size() * 4, cudaMemcpyDeviceToHost, m.cs); cudaStreamSynchronize(m.cs); for (int64_t t = 0; t < T; ++t) { const int64_t w = m.steps_host[(size_t) (t * strata::kernels::kStepCount + strata::kernels::kStepWidth)]; const int32_t *x = a.data() + t * m.cap, *y = b.data() + t * m.cap; int64_t i = 0, j = 0, c = 0; while (i < w && j < w) { if (x[i] == y[j]) { ++c; ++i; ++j; } else if (x[i] < y[j]) ++i; else ++j; } ++queries; same += c == w; if (p0 + t + 1 > m.cap) { ++sel_queries; shared += (double) c; cells += (double) w; } } if (qsa_index + 1 == g.n_qsa_layers()) std::fprintf(stderr, "strata prefill: FP16 indexer keys: %lld of %lld selections identical; " "where the selection is sparse, %.4f%% of cells shared (%lld queries)\n", same, queries, cells > 0 ? 100.0 * shared / cells : 100.0, sel_queries); } // STRATA_QSA_DUMP=: append every QSA layer's selected cells for the prompt's last // STRATA_QSA_DUMP_LAST (4096) positions - records of int32 {qsa layer, pos0, T, cap} + T*cap cells, // for tools/qsa_locality.py (how local the sparse attention's reads are: the KV-streaming question). // STRATA_QSA_DUMP_LAYER=i: QSA layer i only; STRATA_QSA_DUMP_STEPS=1: each record then ends with the // T * kStepCount steps too (tests/cuda/pa_bench.cpp replays such a record) if (static const char* dump = std::getenv("STRATA_QSA_DUMP"); dump != nullptr) { static const long long last = std::getenv("STRATA_QSA_DUMP_LAST") ? std::atoll(std::getenv("STRATA_QSA_DUMP_LAST")) : 4096; static const int only = std::getenv("STRATA_QSA_DUMP_LAYER") ? std::atoi(std::getenv("STRATA_QSA_DUMP_LAYER")) : -1; if (p0 + T > pos0 + n - last && (only < 0 || only == (int) qsa_index)) { std::vector h((size_t) (T * m.cap)); cudaMemcpyAsync(h.data(), m.sel_ids, h.size() * 4, cudaMemcpyDeviceToHost, m.cs); cudaStreamSynchronize(m.cs); if (std::FILE* f = std::fopen(dump, "ab")) { const int32_t hdr[4] = {(int32_t) qsa_index, (int32_t) p0, (int32_t) T, (int32_t) m.cap}; std::fwrite(hdr, 4, 4, f); std::fwrite(h.data(), 4, h.size(), f); if (std::getenv("STRATA_QSA_DUMP_STEPS")) { std::vector sv((size_t) (T * strata::kernels::kStepCount)); cudaMemcpy(sv.data(), m.steps_dev, sv.size() * 4, cudaMemcpyDeviceToHost); std::fwrite(sv.data(), 4, sv.size(), f); } std::fclose(f); } } } const strata::kernels::QsaAttnPools pools = staged ? pools_of(m.stage, m.ident_table) : core::qsa_attn_pools(st); pt.mark(kPfQsaAttn, cs); // perf-review D-1: the whole chunk on tensor cores, one block per (query, KV head), FP32-level // accuracy but not bitwise (qsa_prompt_attn.hpp). Q4_0 KV, or STRATA_PROMPT_ATTN_OLD=1: the // decode kernel, 32 queries at a time (K8V4 runs the tensor kernel's mode 3: INT8 K, // V dequantized from its q4_0 blocks to fp16 at gather) static const bool old_attn = std::getenv("STRATA_PROMPT_ATTN_OLD") != nullptr; const int64_t ld_a = pf_pad() && T >= std::max(pf_switch_min_t(), 64) ? ZV + ZV_PAD : 0; const bool gated = !old_attn && !st.kv_rot && !st.kv_hybrid && qsa_fuse_gate() && strata::kernels::qsa_prompt_attn_batch_gated(m.q, pools, m.sel_ids, m.steps_dev, m.cap, s, m.Qf, m.attn_h, ld_a > 0 ? ld_a : ZV, T, m.cs); { static bool told = false; // once: which of the two fusions this run took if (!told) { told = true; std::fprintf(stderr, "strata prefill: QSA layers: norm + rope in one pass %s, attention gate in the attention epilogue %s\n", qsa_fuse_nr() ? "on" : "off", gated ? "on" : "off"); } } // STRATA_PATTN_SM60=1 (qsa_pattn.hpp, sm_6x, int8 K/V): the decode kernels' output bit for bit from a // score kernel and a value kernel that merges as it folds (no partials, no merge kernel), ~100 // queries a batch in the same scratch; any refusal or launch failure runs the loop below if (!gated && (old_attn || !strata::kernels::qsa_prompt_attn_batch(m.q, pools, m.sel_ids, m.steps_dev, m.cap, s, m.attn, T, m.cs))) #if !defined(STRATA_GPU_METAL) // qsa_pattn.cu is in the CUDA / HIP kernel library only if (!(strata::kernels::pattn_mode() != 0 && strata::kernels::qsa_pattn_batch(m.q, pools, m.sel_ids, m.steps_dev, m.cap, s, m.attn_scratch, (uint64_t) m.attn_batch * strata::kernels::qsa_decode_attn_scratch_floats(m.cap, s), m.attn, T, m.cs))) #endif for (int64_t t0 = 0; t0 < T; t0 += m.attn_batch) { const int64_t nb = std::min(m.attn_batch, T - t0); strata::kernels::qsa_decode_attn_batch(m.q + t0 * kQW, pools, m.sel_ids + t0 * m.cap, m.steps_dev + t0 * strata::kernels::kStepCount, m.cap, s, m.attn_scratch, m.attn + t0 * kQW, nb, m.cs); } if (st.kv_rot || st.kv_hybrid) strata::kernels::fwht256_inplace_cuda(m.attn, T * kNHead, m.cs); pt.mark(kPfQsa, cs); if (!gated) gate_attn(m.attn, m.Qf, m.attn_h, T, m.cs, ld_a); if (!native_proj(m.gemm, wo, m.attn_h, m.bo, T, v.name("attn_output.weight"), err, 0, ld_a)) return false; ++qsa_index; if (!kv_prefetch_after(l + 1, qsa_index)) return false; } else { // ======================= MoE ======================= 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, m.ids, m.w, T, m.g->n_expert, m.cs); // RESEARCH HOOK (141-research, opt-in, never on by default): STRATA_DBG_FEAT= dumps, per MoE layer and chunk, // the routed ids + weights (and for the layers in STRATA_DBG_FEAT_LAYERS the router-input hidden state, bf16); // STRATA_DBG_ROUTE_OVERRIDE= replaces the routing of the layers listed in that file with the file's ids + weights. { static const char* feat_path = std::getenv("STRATA_DBG_FEAT"); static const char* ovr_path = std::getenv("STRATA_DBG_ROUTE_OVERRIDE"); if (feat_path != nullptr || ovr_path != nullptr) { static std::FILE* ff = feat_path ? std::fopen(feat_path, "wb") : nullptr; static std::vector featl = [] { std::vector r(64, false); const char* e = std::getenv("STRATA_DBG_FEAT_LAYERS"); std::string s = e ? e : "3,4,15,16,27,28,39,40"; size_t i = 0; while (i < s.size()) { size_t j = s.find(',', i); if (j == std::string::npos) j = s.size(); int v = std::atoi(s.substr(i, j - i).c_str()); if (v >= 0 && v < 64) r[(size_t) v] = true; i = j + 1; } return r; }(); static int64_t posl[64] = {}; struct Ovr { std::vector ids; std::vector w; }; static std::map, Ovr> ovr = [] { std::map, Ovr> mp; const char* p = std::getenv("STRATA_DBG_ROUTE_OVERRIDE"); std::FILE* f = p ? std::fopen(p, "rb") : nullptr; if (f == nullptr) return mp; int32_t h[3]; while (std::fread(h, 4, 3, f) == 3) { Ovr o; o.ids.resize((size_t) h[2] * K); o.w.resize((size_t) h[2] * K); if (std::fread(o.ids.data(), 4, o.ids.size(), f) != o.ids.size()) break; if (std::fread(o.w.data(), 4, o.w.size(), f) != o.w.size()) break; mp[{h[0], h[1]}] = std::move(o); } std::fclose(f); std::fprintf(stderr, "route override: %zu records\n", mp.size()); return mp; }(); const int32_t pos = (int32_t) posl[l & 63]; posl[l & 63] += T; cudaStreamSynchronize((cudaStream_t) m.cs); if (ff != nullptr) { std::vector hid((size_t) T * K); std::vector hw((size_t) T * K); cudaMemcpy(hid.data(), m.ids, hid.size() * 4, cudaMemcpyDeviceToHost); cudaMemcpy(hw.data(), m.w, hw.size() * 4, cudaMemcpyDeviceToHost); std::vector ids16(hid.size()), w16(hw.size()); for (size_t i = 0; i < hid.size(); ++i) { ids16[i] = (uint16_t) hid[i]; w16[i] = (uint16_t) (((*(const uint32_t*) &hw[i]) + 0x8000u) >> 16); } const int32_t hd[4] = {(int32_t) l, pos, (int32_t) T, featl[(size_t) (l & 63)] ? 1 : 0}; std::fwrite(hd, 4, 4, ff); std::fwrite(ids16.data(), 2, ids16.size(), ff); std::fwrite(w16.data(), 2, w16.size(), ff); // bf16 weights if (hd[3]) { std::vector hx((size_t) T * N); cudaMemcpy(hx.data(), m.mixed_bf, hx.size() * 2, cudaMemcpyDeviceToHost); std::fwrite(hx.data(), 2, hx.size(), ff); } } auto it = ovr.find({(int32_t) l, pos}); if (it != ovr.end() && (int64_t) it->second.ids.size() == T * K) { cudaMemcpy(m.ids, it->second.ids.data(), (size_t) T * K * 4, cudaMemcpyHostToDevice); cudaMemcpy(m.w, it->second.w.data(), (size_t) T * K * 4, cudaMemcpyHostToDevice); } } } // the shared expert and its scalar gate auto shared_expert = [&]() -> bool { 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, m.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); return true; }; // Only the router needs to finish before readback. Queue the shared expert afterwards // so it can overlap CPU grouping and the routed-only path's initial expert uploads. const bool defer_shared = !stream_all && stream_ahead_enabled(); if (!defer_shared && !shared_expert()) return false; // #136: STRATA_PF_FUSED=1 - the Q2_0 pack's experts on the fused int8 kernels (moe_fused.hpp), // grouped on the GPU: no host sync. Only where every expert's place is known before the routing - // the streamed walk, in which every non-resident expert of the layer comes through the ring in id // order - and where the MMQ buffers exist: they hold the fused path's own (the per-token int8 // activations in Xq, the int8 H in H, the grouping tables in GU). Chunks below stream_all_min() // keep MMQ; without the variable nothing here runs. A native pack's layer takes the native kernels // (moe_fused_iq.hpp) where they cover its two formats, else MMQ (or the FP16 path: IQ1_M). const strata::kernels::cpu::ExpertLayout& lay = strata::kernels::cpu::expert_layout(); // APR (prism.hadamard): folded gate/up take H (s * x), folded down H (s * h), on the FP16 and MMQ // paths and on the multi-GPU prompt paths (the peer, the layer split's helper: MMQ); the fused // kernels do not rotate (and have no trellis types), so a folded layer takes MMQ instead const bool had_gu = lay.native && lay.fmt[(size_t) l].had_gu; const bool had_d = lay.native && lay.fmt[(size_t) l].had_d; const bool use_mmq = mmq_plan().any && mmq_plan().layer[(size_t) l]; // STRATA_EXPERT_NIBBLE=1: a layer whose gate/up experts are 4-bit codes in their slots is gathered // for MMQ as the GGUF blocks rebuilt from them (core::nibble::gather_gguf); every other expert path // reads GGUF blocks in the slot, so it stops there instead (never a wrong answer) #if STRATA_EXPERT_NIBBLE_BUILT // every guard of this switch is compiled out of the other builds const core::nibble::LayerVram& nib_l = core::nibble::layer(l); #endif const int had_block = lay.native ? lay.fmt[(size_t) l].had_block : 0; const float* had_sx = had_gu ? m.had_sx[(size_t) l] : nullptr; // resolved in init const float* had_sh = had_d ? m.had_sh[(size_t) l] : nullptr; 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; // --peer-device with MMQ: MMQ only, whether or not the peer took the prompt path (set_peer can decline), as // fused_ring() sized the ring and the buffers for it. Without MMQ the share is always declined: the // kernels the walk picks without the flag, in MMQ's buffer layout (own_only_line.hpp peer_keeps_mmq) const bool no_peer = !strata::prefill::peer_keeps_mmq(core::peer_portable(), kPfOwnOnlyBuild); const bool fused_only = mmq_plan().any && mmq_plan().fo[(size_t) l]; // a Hadamard-folded layer (APR / Gyro) on the fused kernels: only the trellis pairs (CUDA, STRATA_PF_FUSED_TQ=1: native_supported // answers for them only with it), block 128: the gate/up input rotated into m.Xr as the MMQ path does, the down input // rotated inside experts_native (fused::set_down_rotation); every other folded layer keeps MMQ const bool fold_ok = had_block == 128 && ((mmq_gt == 145 && mmq_dt == 146) || (mmq_gt == 144 && mmq_dt == 144)) && (!had_gu || m.Xr != nullptr); const bool fused_nat = (use_mmq || fused_only) && stream_all && no_peer && lay.native && ((!had_gu && !had_d) || fold_ok) && fused::native_supported(mmq_gt, mmq_dt); #if defined(STRATA_PF_OWN_ONLY) const bool fused_l = ((use_mmq || fused_only) && stream_all && no_peer && !lay.native && fused::enabled()) || fused_nat; #else const bool fused_l = (use_mmq && stream_all && no_peer && !lay.native && fused::enabled()) || fused_nat; #endif bool dm16_used = false; // STRATA_PF_DM16: this layer's down rows are FP16 (fused::dm_f16_used() after the experts) bool fz_staged_l = false; // P4-SMALL: this staged chunk's groups ran on the fused kernels (GU / H hold their tables and int8 rows) if (m.group_cap > 0 && (fused_l || !use_mmq)) { // GU / H / Hq are one group's size err = "prefill: STRATA_PF_GROUP_SCRATCH needs MMQ on every layer (a layer took the fused or FP16 path)"; return false; } // #583 / #954: the fused layout's GU/H/Xq hold the grouping tables and the int8 rows, sized for // stream_all_min() - 1 tokens of MMQ's rows, so a layer that takes MMQ or the FP16 path at the FULL // chunk would write T*K rows into a (stream_all_min() - 1)*K-row buffer: an illegal access, or a kernel // that never returns (the first ck() to see it is `prefill mmq: iota`, one prompt later). fused_layout() // and fused_ring() keep the shrink and this decision in step; this is the backstop for anything that // drifts: a clear error, never a hang. // STRATA_DBG_FORCE_SLOW_LAYER=1 (a test of the backstop): layer 0 counts as one the fused path does not take static const bool force_slow = [] { const char* e = std::getenv("STRATA_DBG_FORCE_SLOW_LAYER"); return e && e[0] == '1'; }(); if ((!fused_l || (force_slow && l == 0)) && m.fused_bufs && T >= stream_all_min()) { err = "prefill: the fused layout's MoE buffers are too small for layer " + std::to_string(l) + "'s expert path at a chunk of " + std::to_string(T) + " tokens"; return false; } size_t n_order = 0; // the routed experts (the debug report; unknown when fused) bool peer_now = false; // multi-GPU: the peer computed rows of this layer (MMQ path only) // 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, cpu_at = 0; // cpu_at: the CPU's first row of Dm bool cpu_arm = true; // auto: this layer may share (false: the arm without) int cpu_set = -1; // auto: the wall events' set, -1 when not measured bool cpu_cold = false; // auto: one-time costs in the window (no reading) #if STRATA_EXPERT_NIBBLE_BUILT if (fused_l && nib_l.on) core::nibble::fatal(l, "the prompt path's fused experts", "they read GGUF blocks"); #endif if (m.latent && fused_l) { // the int8 fused path (STRATA_PF_FUSED) reads GGUF blocks err = "prefill latent: the fused expert path (STRATA_PF_FUSED) is not supported"; return false; } if (fused_l) { if (static bool said = false; !said) { said = true; std::fprintf(stderr, "strata: prompt experts on the fused int8 kernels (STRATA_PF_FUSED=1, " "#136)\n"); } pt.mark(kPfGather, cs); // the layer's input to int8 once per token; the rows of each expert from the router's ids if (fused_nat && had_gu) { // (a folded trellis gate/up: its input rotated, as the MMQ path's xsrc = m.Xr) strata::kernels::hadamard_rows(m.mixed, m.Xr, T, N, had_block, had_sx, m.cs); fused::quantize_act_native(m.Xr, T, N, m.Xq, m.cs); } else if (fused_nat) fused::quantize_act_native(m.mixed, T, N, m.Xq, m.cs); else fused::quantize_act(m.mixed, T, N, m.Xq, m.cs); // (a folded trellis down: H rotated inside experts_native; off again after the layer's launches) if (fused_nat && had_d) fused::set_down_rotation(true, had_sh); // STRATA_PF_P1A=1 (gfx1151): the layer input's FP16 rows for the P1A kernels (p1a_input keeps nullptr otherwise) if (fused_nat) fused::p1a_input(had_gu ? nullptr : m.mixed_h, m.H_bytes); bool p1a_layer = false; fused::group(m.ids, T * K, (int) K, (int) m.g->n_expert, m.GU, m.slot_dev, m.src_dev, m.cs); // launches over the experts in id order, each at most kMaxBatch experts (STRATA_PF_BATCH: below) // of which at most a third of the ring streamed: the next batch's blobs arrive while one computes. // An expert the routing did not pick has no tiles; its ring slot is given back with its batch. const size_t per = (size_t) std::max(1, m.ring / 3); size_t k = seq_start[(size_t) l]; const size_t kend = seq_start[(size_t) l + 1]; // STRATA_PF_BATCH=N (opt-in): experts per launch (the same rows, so the same bits; a launch is one // persistent grid, so fewer launches leave fewer grid tails) - at most kMaxBatch, or up to // kBatchRoom in a WideBatch on the native NT = 256 kernels (STRATA_PF_FUSED_NT=256 or _F16=1) or // the warp-specialized kernel (STRATA_PF_FUSED_WS=1) static const int pf_batch = [] { const char* v = std::getenv("STRATA_PF_BATCH"); const int n = v ? std::atoi(v) : 0; return n > 0 ? std::min(n, fused::kBatchRoom) : fused::kMaxBatch; }(); const bool wide = fused_nat && pf_batch > fused::kMaxBatch && fused::native_wide(); const int per_launch = wide ? pf_batch : std::min(pf_batch, fused::kMaxBatch); for (int32_t e = 0; e < m.g->n_expert;) { fused::Batch b; std::optional wb; // wide: the launch's pointers (b keeps e0 / e1) if (wide) wb.emplace(); const uint8_t** blob = wb ? wb->blob : b.blob; b.e0 = e; const size_t k0 = k; for (; e < m.g->n_expert && e - b.e0 < per_launch; ++e) { if (k < kend && seq[k].e == e) { if (k - k0 == per) break; blob[e - b.e0] = m.stage_dev[k % (size_t) m.ring]; ++k; } else { // not streamed: resident (the walk streams all others) blob[e - b.e0] = m.cache->device_slot(m.host_res[(size_t) l * m.g->n_expert + e]); // counted whether routed or not (the routing stays on the GPU): at a streamed // chunk's size (>= 1024 tokens x 10 of 512) nearly every expert is routed ++stats_.experts_resident; } } b.e1 = e; if (k > k0) { // one copy stream, in order: the batch's last blob covers the others pt.mark(kPfWaitCopy, cs); wait_issued(k - 1); wait_copied(k - 1, (k - 1) % (size_t) m.ring); } pt.mark(kPfGemmGU, cs); if (fused_nat) { fused::want_dm_f16(pf_dm16() && rows_cpu == 0 && !peer_now && ((reinterpret_cast(m.shared) | reinterpret_cast(m.bo)) & 15) == 0); const auto& f = lay.fmt[(size_t) l]; const fused::NativeGeom ng{f.gu_type, f.d_type, f.gu_row, f.d_row, f.up_off, f.down_off}; if (wb) { wb->e0 = b.e0; wb->e1 = b.e1; fused::experts_native(*wb, ng, (int) m.g->n_expert, T * K, m.GU, m.Xq, m.src_dev, m.H, m.Dm, m.cs); } else { fused::experts_native(b, ng, (int) m.g->n_expert, T * K, m.GU, m.Xq, m.src_dev, m.H, m.Dm, m.cs); } dm16_used = fused::dm_f16_used(); p1a_layer = p1a_layer || fused::p1a_used(); } else { fused::experts(b, (int) m.g->n_expert, T * K, m.GU, m.Xq, m.src_dev, m.H, m.Dm, m.cs); } for (size_t kk = k0; kk < k; ++kk) { cudaEventRecord(m.used[kk % (size_t) m.ring], m.cs); m.used_of[kk % (size_t) m.ring] = (int) (kk % (size_t) m.ring); } if (k > k0) { consumed = k; give_back(consumed); } } if (fused_nat && had_d) fused::set_down_rotation(false, nullptr); if (fused_nat && fused::p1a_wanted()) { fused::p1a_input(nullptr, 0); p1a_report(lay.fmt, (size_t) l, p1a_layer); } } else { // group the (token, k) pairs by expert on the host pt.mark(kPfHostGroup, cs); // (the sync below also orders this layer's writes of slot/src/bounds after the previous // layer's kernels that read them) const bool grp_mapped = m.grp_host != nullptr; int32_t* ids_h = grp_mapped ? m.grp_host : m.ids_host.data(); int32_t* slot_h = grp_mapped ? m.grp_host + m.grp_tk : m.slot_host.data(); int32_t* src_h = grp_mapped ? m.grp_host + 2 * m.grp_tk : m.src_host.data(); if (grp_mapped) copy_i32(m.grp_dev, m.ids, T * K, m.cs); else if (m.up_ring.d2h_sync(m.ids_host.data(), m.ids, (size_t) T * K * 4, m.cs) != cudaSuccess) { err = "prefill: the routed ids' download failed"; // #1996 return false; } bool cpu_maybe = pool_hold.held && !stream_all && m.src != nullptr && !m.cpu_dead && lay.native && !m.latent && (!m.pp || pf_peer_cpu()) && !lay.fmt.empty(); // The share's two pinned buffers are small (~50 MB) but are had mid-request, when the host may have // nothing left (0.1.41 report: tight RAM, chats failing). A buffer that cannot be had ends the share // for this engine - the GPU takes every expert, as with STRATA_PREFILL_CPU_SHARE=0 - instead of // failing the request. auto cpu_share_off = [&](const char* what) { m.cpu_dead = true; cpu_maybe = false; std::fprintf(stderr, "prefill: the CPU share is off for this run: the host could not give %s " "(RAM is short); the GPU streams every expert\n", what); }; // auto shares only while the layers that share cost less per non-resident expert than their // neighbours that do not. #1282 (RX 7900 GRE + 5700X3D): every share lost, auto's 0.47 by // 4.5%, the host's issue time per streamed expert rising 107 -> 200 us with the CPU busy - // a cost g / (c + g) cannot see, as both sides are measured with the share on. The first // eligible layers alternate, without first, until CPU_MIN_RATIOS ratios are in; then the // cheaper arm, and every CPU_PROBE-th layer the other one (a prime: the probes move across // the layers from one chunk to the next), so both stay measured. if (cpu_maybe && cpu_share_env() < 0.0) { constexpr int64_t CPU_PROBE = 29; constexpr int CPU_MIN_RATIOS = 3; const int64_t i = m.cpu_layers++; cpu_arm = m.cpu_nratio < CPU_MIN_RATIOS ? (i & 1) != 0 : (i % CPU_PROBE == 0) != m.cpu_gate; if (m.cpu_first_l < 0) m.cpu_first_l = l; cpu_cold = l == m.cpu_first_l; if (m.cpu_wall[0] == nullptr) for (cudaEvent_t& e : m.cpu_wall) cudaEventCreate(&e); cpu_set = (int) (i & 1); cudaEventRecord(m.cpu_wall[2 * cpu_set], m.cs); } if (cpu_maybe && cpu_arm) { const size_t want = (size_t) T * N; if (m.cpu_x_n < want) { cpu_cold = true; if (m.cpu_x) cudaFreeHost(m.cpu_x); m.cpu_x = nullptr; m.cpu_x_n = 0; if (cudaHostAlloc((void**) &m.cpu_x, want * sizeof(float), cudaHostAllocDefault) != cudaSuccess) { (void) cudaGetLastError(); m.cpu_x = nullptr; cpu_share_off("the activation buffer"); } else { m.cpu_x_n = want; } } if (cpu_maybe) cudaMemcpyAsync(m.cpu_x, m.mixed, want * sizeof(float), cudaMemcpyDeviceToHost, m.cs); } // #579: a stall here is the GPU (this layer's attention and router, or the previous layer's // work), not the host: the watchdog's report says so (only its text changes) core::progress_at("reading the prompt (batched): waiting for the GPU (attention, router) at layer", l, p0); cudaStreamSynchronize(m.cs); core::progress_at("reading the prompt (batched): layer", l, p0); if (on_routes) on_routes(l, ids_h, T * K); // 149-prefetch: the RAM policy's prompt seed if (cpu_maybe) { // the rows buffer, once, at its largest (an expert takes at most MAXT tokens' rows) const size_t cap = (size_t) std::min(T * K, (int64_t) strata::kernels::cpu::MAXT * m.g->n_expert) * N; if (m.cpu_rows_n < cap) { if (m.cpu_rows) cudaFreeHost(m.cpu_rows); m.cpu_rows = nullptr; m.cpu_rows_n = 0; if (cudaHostAlloc((void**) &m.cpu_rows, cap * sizeof(float), cudaHostAllocDefault) != cudaSuccess) { (void) cudaGetLastError(); m.cpu_rows = nullptr; cpu_share_off("the rows buffer"); } else { m.cpu_rows_n = cap; } } } if (m.cpu_pend) { // the measured share: the last CPU-sharing layer's GPU time is final now m.cpu_pend = false; float g_ms = 0; if (cudaEventElapsedTime(&g_ms, m.cpu_ev[0], m.cpu_ev[1]) == cudaSuccess) { constexpr double a = 0.25; const double c = m.pend_cpu_ms / (double) m.pend_n_cpu, g = g_ms / (double) m.pend_n_gpu; m.cpu_c_ms = m.cpu_c_ms > 0 ? m.cpu_c_ms + a * (c - m.cpu_c_ms) : c; m.cpu_g_ms = m.cpu_g_ms > 0 ? m.cpu_g_ms + a * (g - m.cpu_g_ms) : g; m.cpu_share_now = std::clamp(m.cpu_g_ms / (m.cpu_c_ms + m.cpu_g_ms), 0.05, 0.9); stats_.cpu_share_balanced = m.cpu_share_now; ++stats_.cpu_share_readings; } } if (m.pend_wall) { // the last eligible layer's wall, and its ratio to the one before it float w_ms = 0; if (cudaEventElapsedTime(&w_ms, m.cpu_wall[2 * m.pend_set], m.cpu_wall[2 * m.pend_set + 1]) == cudaSuccess) { const double w = w_ms / (double) m.pend_wall_n; if (m.last_arm != 0 && m.last_arm != m.pend_wall && m.pend_l > m.last_l) { // same chunk std::rotate(m.cpu_ratio, m.cpu_ratio + 1, m.cpu_ratio + Impl::CPU_RATIOS); m.cpu_ratio[Impl::CPU_RATIOS - 1] = m.pend_wall == 2 ? w / m.last_w : m.last_w / w; m.cpu_nratio = std::min(m.cpu_nratio + 1, Impl::CPU_RATIOS); double v[Impl::CPU_RATIOS]; std::copy(m.cpu_ratio + Impl::CPU_RATIOS - m.cpu_nratio, m.cpu_ratio + Impl::CPU_RATIOS, v); std::nth_element(v, v + m.cpu_nratio / 2, v + m.cpu_nratio); // even: the upper one m.cpu_gate = v[m.cpu_nratio / 2] < 1.0; } // debug: STRATA_DBG_CPU_GATE=1 prints each reading and what the next layers do if (static const bool dbg = std::getenv("STRATA_DBG_CPU_GATE") != nullptr; dbg) std::fprintf(stderr, "cpu gate: layer %d %s, %lld non-resident, %.2f ms (%.4f a expert) -> %s\n", m.pend_l, m.pend_wall == 2 ? "shared" : "not shared", (long long) m.pend_wall_n, w_ms, w, m.cpu_gate ? "share" : "do not share"); m.last_arm = m.pend_wall; m.last_l = m.pend_l; m.last_w = w; } m.pend_wall = 0; } pt.fold(); if (pe.on) { // the peer's marks so far are done: the primary waited for its last rows int pd = 0; cudaGetDevice(&pd); cudaSetDevice(pe.dev); cudaStreamSynchronize(m.pp->s); pe.fold(); cudaSetDevice(pd); } if (defer_shared) { pt.mark(kPfRouter, cs); // keep the existing router+shared timing attribution if (!shared_expert()) return false; pt.mark(kPfHostGroup, cs); } std::fill(m.cnt.begin(), m.cnt.end(), 0); for (int64_t i = 0; i < T * K; ++i) { const int32_t e = ids_h[(size_t) i]; if (e < 0 || e >= m.g->n_expert) { err = "prefill: routed id out of range"; return false; } ++m.cnt[(size_t) e]; } // STRATA_PREFILL_ROUTED_STREAM: this layer's routing to the issuer (it drops the unrouted entries // it has not issued yet; the walk below gives every unrouted entry back without reading it) if (rs_on) rs_gate.publish(l, m.cnt.data()); const bool pre_mmq = mmq_plan().any && mmq_plan().layer[(size_t) l]; // STRATA_PF_PEER_CPU: an expert the peer computes below (streamed to it, or held by it) is not // the CPU's to take, nor one the primary would stream auto peer_takes = [&](int32_t e) -> bool { if (!m.pp || !pre_mmq) return false; const size_t i = (size_t) l * m.g->n_expert + e; if (!m.pp->ps_flag.empty() && m.pp->ps_flag[i]) return true; return m.pp->peer != nullptr && m.pp->peer->has(l, e); }; if (cpu_maybe) { std::vector> cand; // (tokens, expert) int64_t nstream = 0; for (int32_t e = 0; e < m.g->n_expert; ++e) { const int32_t c = m.cnt[(size_t) e]; if (c == 0 || (m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0)) continue; if (peer_takes(e)) continue; ++nstream; // a blob the CPU reads from RAM: page-locked, or any other that is not assembled into a // short-lived buffer (the arena's unpinned part, the mapped experts.bin's pages) if (c <= strata::kernels::cpu::MAXT && (m.src->pinned(l, e) || !m.src->transient(l, e))) cand.emplace_back(c, e); } std::sort(cand.begin(), cand.end()); const double share = cpu_share_env() >= 0.0 ? cpu_share_env() : cpu_arm ? m.cpu_share_now : 0.0; const size_t take = std::min(cand.size(), (size_t) std::llround(share * (double) nstream)); n_stream = nstream; // The CPU 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, and an expert without a stable blob stays on the GPU. for (size_t i = 0; i < cand.size() && n_cpu < (int64_t) take; ++i) { const int32_t e = cand[i].second; const uint8_t* b = m.src->blob_stable(l, e); if (b == nullptr) continue; if (on_cpu.empty()) { on_cpu.assign((size_t) m.g->n_expert, 0); cpu_blob.assign((size_t) m.g->n_expert, nullptr); } on_cpu[(size_t) e] = 1; cpu_blob[(size_t) e] = b; rows_cpu += cand[i].first; ++n_cpu; } } // multi-GPU: the rows of the experts the peer computes go last, as one block [peer_at, T*K) std::vector on_peer; int64_t rows_local = T * K, rows_peer = 0; if (m.pp && pre_mmq) { on_peer.assign((size_t) m.g->n_expert, 0); for (int32_t e = 0; e < m.g->n_expert; ++e) { const int32_t c = m.cnt[(size_t) e]; if (c > 0 && !m.pp->ps_flag.empty() && m.pp->ps_flag[(size_t) l * m.g->n_expert + e]) { // peer-streamed on_peer[(size_t) e] = 2; rows_peer += c; continue; } if (c == 0 || (m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0) || !m.pp->peer || !m.pp->peer->has(l, e)) continue; if (rows_peer + c > m.pp->cap_rows) { ++m.pp->over_cap; continue; } on_peer[(size_t) e] = 1; rows_peer += c; } rows_local = T * K - rows_peer; } // STRATA_PF_PEER_CPU: beside the peer the CPU's block goes before the peer's, which stays the tail // (its combine takes every row from back_at on); alone, last as before const bool cpu_mid = !on_peer.empty() && !on_cpu.empty(); { int32_t r = 0; // latent MoE: the resident experts' entries come first, so the A stage takes // them as one contiguous run of grouped launches (the staged blobs' per-expert // launches follow). Every table below is built from off[], so the permutation // flows through coherently (tok/ids/yl/U/Dm and the combine all stay entry-major). for (int pass = m.latent ? 0 : 1; pass < 2; ++pass) for (int32_t e = 0; e < m.g->n_expert; ++e) { if (!(on_peer.empty() || !on_peer[(size_t) e]) || !(on_cpu.empty() || !on_cpu[(size_t) e])) continue; const bool res = m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0; if (m.latent && pass == 0 && !res) continue; if (m.latent && pass == 1 && res) continue; m.off[(size_t) e] = r; r += m.cnt[(size_t) e]; } for (int32_t e = 0; e < m.g->n_expert && cpu_mid; ++e) // the CPU's block, before the peer's if (on_cpu[(size_t) e]) { m.off[(size_t) e] = r; r += m.cnt[(size_t) e]; } // the peer's rows in order_peer's order (peer-held, then peer-streamed): its groups need them contiguous for (int kind = 1; kind <= 2 && !on_peer.empty(); ++kind) for (int32_t e = 0; e < m.g->n_expert; ++e) if (on_peer[(size_t) e] == kind) { m.off[(size_t) e] = r; r += m.cnt[(size_t) e]; } for (int32_t e = 0; e < m.g->n_expert && !on_cpu.empty() && !cpu_mid; ++e) // the CPU's block, last if (on_cpu[(size_t) e]) { m.off[(size_t) e] = r; r += m.cnt[(size_t) e]; } m.off[(size_t) m.g->n_expert] = r; rows_local -= rows_cpu; } cpu_at = cpu_mid ? rows_local : T * K - rows_cpu; // the CPU's first row const int64_t peer_at = cpu_mid ? rows_local + rows_cpu : rows_local; // the peer's first row std::vector fill(m.off.begin(), m.off.end() - 1); for (int64_t i = 0; i < T * K; ++i) { const int32_t e = ids_h[(size_t) i]; const int32_t p = fill[(size_t) e]++; slot_h[(size_t) i] = p; src_h[(size_t) p] = (int32_t) (i / K); if (m.latent) { // latent plan tables in position order (tokx: token-major index; idsx at that cell) const int32_t s = (int32_t) (i / K), kk = (int32_t) (i % K); m.lat_tokx_host[(size_t) p] = s * (int32_t) K + kk; m.lat_idsx_host[(size_t) (s * (int32_t) K + kk)] = e; m.lat_idsp_host[(size_t) p] = e; } } // the experts, in id order: resident ones from VRAM, the others through the staging ring std::vector order, order_peer; for (int32_t e = 0; e < m.g->n_expert; ++e) if (m.cnt[(size_t) e] > 0 && (on_peer.empty() || on_peer[(size_t) e] != 2) && (on_cpu.empty() || !on_cpu[(size_t) e])) (!on_peer.empty() && on_peer[(size_t) e] ? order_peer : order).push_back(e); for (int32_t e = 0; e < m.g->n_expert && !on_peer.empty(); ++e) // the peer-streamed ones last: if (on_peer[(size_t) e] == 2) order_peer.push_back(e); // their copies get the most time // Aurora (STRATA_MMQ_RESIDENT_SORT_NE=1, opt-in): a layer whose experts are ALL resident and // run through MMQ groups takes them in row-count order, so each 16-expert group's max_rows // (its padded tile rows) is close to its experts' own; slot/src/off are rebuilt coherently // before the upload, the routed ids, weights and the within-expert row order stay as they are. // STRATA_MMQ_GROUP_SORT=1 (opt-in, exact): the same order for the full groups only, where each // tile is one block's whole K (MmqPlan::group_sort); the partial group keeps its ID order and bits. // STRATA_MMQ_RESIDENT_SORT_NE=1 takes precedence (it sorts the partial group too). const bool sort_all = m.latent ? false : detail::mmq_resident_sort_requested(); const bool sort_full = !sort_all && !m.latent && use_mmq && mmq_plan().group_sort[(size_t) l] && order.size() >= (size_t) MMQ_GROUP; if (detail::mmq_resident_sort_eligible((sort_all || sort_full) && on_peer.empty(), use_mmq, lay.native, m.cache != nullptr, m.host_res ? m.host_res + (size_t) l * m.g->n_expert : nullptr, m.g->n_expert, stream_all, !stream_all || seq_start[(size_t) l] == seq_start[(size_t) l + 1])) detail::mmq_resident_sort_rows(ids_h, T * K, (int32_t) K, m.cnt, m.off, order, slot_h, src_h, detail::mmq_resident_sort_span(order.size(), MMQ_GROUP, sort_full)); if (grp_mapped) { copy_i32(m.slot_dev, m.grp_dev + m.grp_tk, T * K, m.cs); copy_i32(m.src_dev, m.grp_dev + 2 * m.grp_tk, T * K, m.cs); } else { if (m.up_ring.h2d(m.slot_dev, m.slot_host.data(), (size_t) T * K * 4, m.cs) != cudaSuccess || // #1996 m.up_ring.h2d(m.src_dev, m.src_host.data(), (size_t) T * K * 4, m.cs) != cudaSuccess) { err = "prefill: the routing tables' upload failed"; return false; } } // Diagnostics: STRATA_DUMP_MOE_INPUT=path, STRATA_DUMP_MOE_LAYER=l - the first chunk's MoE input // of layer l ({T, N, K} int64, T x N floats, T x K expert ids) for testing products on real rows static const char* dump_moe = std::getenv("STRATA_DUMP_MOE_INPUT"); static bool dumped_moe = false; // STRATA_DUMP_MOE_LAYER=all (research): every layer of the process's first chunk, layer l into .L (the layers come in order; the second chunk and later // requests are not dumped) static const bool dump_all = [] { const char* e = std::getenv("STRATA_DUMP_MOE_LAYER"); return e != nullptr && std::strcmp(e, "all") == 0; }(); static int dump_next = 0; const bool dump_this = dump_moe != nullptr && (dump_all ? l == dump_next : (!dumped_moe && l == std::atoi(std::getenv("STRATA_DUMP_MOE_LAYER") ? std::getenv("STRATA_DUMP_MOE_LAYER") : "0"))); if (dump_this) { dumped_moe = true; if (dump_all) ++dump_next; std::vector x((size_t) (T * N)); cudaMemcpyAsync(x.data(), m.mixed, x.size() * sizeof(float), cudaMemcpyDeviceToHost, m.cs); cudaStreamSynchronize(m.cs); const std::string dump_path = dump_all ? std::string(dump_moe) + ".L" + std::to_string(l) : std::string(dump_moe); if (std::FILE* f = std::fopen(dump_path.c_str(), "wb")) { const int64_t hdr[3] = {(int64_t) T, (int64_t) N, (int64_t) K}; std::fwrite(hdr, sizeof hdr, 1, f); std::fwrite(x.data(), sizeof(float), x.size(), f); std::fwrite(ids_h, sizeof(int32_t), (size_t) (T * K), f); std::fclose(f); } } n_order = order.size(); // set_cpu_pool: the CPU's experts on a thread while this one streams and runs the GPU's; their // unweighted rows land in Dm's row order, so the combine weights them as any other row if (rows_cpu > 0) { const size_t want = (size_t) rows_cpu * N; if (m.cpu_rows_n < want) { cpu_cold = true; 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) { err = "prefill: cannot allocate the CPU experts' rows"; return false; } 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 = cpu_at; const strata::kernels::cpu::NativeFmt& cf = lay.fmt[(size_t) l]; if (cpu_share_env() < 0.0) { 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, &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 const bool q2 = strata::kernels::cpu::q2_native_kernels(cf.gu_type); if (q2 && m.cpu_actq.size() < (size_t) T) m.cpu_actq.resize((size_t) T); if (!q2 && m.cpu_nact.size() < (size_t) T * AB) m.cpu_nact.resize((size_t) T * AB); std::vector need((size_t) T, 0); for (int32_t e = 0; e < m.g->n_expert; ++e) // the tokens the CPU's experts read if (on_cpu[(size_t) e]) for (int32_t r = 0; r < m.cnt[(size_t) e]; ++r) need[(size_t) src_h[(size_t) m.off[(size_t) e] + (size_t) r]] = 1; for (int64_t t = 0; t < T; ++t) if (need[(size_t) t] && q2) strata::kernels::cpu::act_quant_any(m.cpu_x + (size_t) t * N, (int) N, m.cpu_actq[(size_t) t]); else if (need[(size_t) t]) strata::kernels::cpu::native_quant_act(cf, m.cpu_x + (size_t) t * N, m.cpu_nact.data() + (size_t) t * AB); m.cpu_jobs.clear(); 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 = 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; if (q2) j.act[r] = &m.cpu_actq[(size_t) src_h[(size_t) p]]; else j.nact[r] = m.cpu_nact.data() + (size_t) src_h[(size_t) p] * AB; j.out[r] = m.cpu_rows + (size_t) (p - r0c) * N; } m.cpu_jobs.push_back(j); } cpu_pool_->run_split_multi_native(cf, m.cpu_jobs.data(), (int) m.cpu_jobs.size()); cpu_ms = std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); return true; }); } if (m.latent) { // latent MoE: D once per layer, A/U in expert-major batches (position-ordered Dm) // STRATA_PREFILL_GROUP_GATHER=0: one gather, one wait and one record per expert. static const bool group_env = [] { const char* v = std::getenv("STRATA_PREFILL_GROUP_GATHER"); return v == nullptr || std::atoi(v) != 0; }(); // (LAT_GROUP > kGatherGroupMax: the latent walk gathers per expert, as on the fork) const bool group_gather = group_env && stream_all && lay.native && (use_mmq && LAT_GROUP <= mmq::kGatherGroupMax); // The staged walk (a short prompt) groups too, resident and staged experts alike: its ring is // then STAGE_GRP slots, two groups, and it stages no further ahead than the open group allows, // so a group's staged slots stay unread-over until its flush (one wait on the last copy, one // event releasing them all, as in the streamed walk). const bool group_resident = group_env && !stream_all && lay.native && (use_mmq && LAT_GROUP <= mmq::kGatherGroupMax); const bool gg_now = group_gather || group_resident; const int stage_n = group_resident ? STAGE_GRP : STAGE; // Stage ahead: the copy stream moves blobs host -> device while the compute stream works. int stage_next = 0; std::vector stage_of(order.size(), -1); // the unpinned ones are copied to pinned buffers by the stager's threads, in this order std::vector job_of(order.size(), -1); if (!stream_all) { std::vector js; for (size_t j = 0; j < order.size(); ++j) { const int32_t e = order[j]; if (m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0) continue; if (m.src->pinned(l, e)) continue; job_of[j] = (int) js.size(); if (m.src->transient(l, e)) { // CS-T: copied by the source 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: 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()}; auto stage_one = [&](size_t j) -> bool { const int32_t e = order[j]; const bool resident = m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0; if (resident) return true; const int sl = stage_next; stage_next = (stage_next + 1) % stage_n; const auto th = Clock::now(); const bool pinned = m.src->pinned(l, e); // pinned: never transient const uint8_t* b = pinned ? m.src->blob_stable(l, e) : nullptr; if (pinned && !b) { err = "prefill: expert source has no blob"; return false; } if (pinned) { // DMA straight from the page-locked arena: the copy stream only waits for the slot if (m.stage_live[sl]) cudaStreamWaitEvent(m.copy, m.used[m.used_of[sl]], 0); cudaMemcpyAsync(m.stage_dev[sl], b, (size_t) lay.blob_bytes(l), cudaMemcpyHostToDevice, m.copy); ++stats_.experts_dma; } else { // copied to a pinned buffer by the stager (waits only if it is behind), then DMA const uint8_t* hb = m.stager->wait(job_of[j]); if (m.stage_live[sl]) cudaStreamWaitEvent(m.copy, m.used[m.used_of[sl]], 0); cudaMemcpyAsync(m.stage_dev[sl], hb, (size_t) lay.blob_bytes(l), cudaMemcpyHostToDevice, m.copy); m.stager->issued_one(job_of[j], m.copy); } cudaEventRecord(m.copied[sl], m.copy); m.stage_live[sl] = true; stage_of[j] = sl; stats_.ms_experts_host += ms_since(th); ++stats_.experts_streamed; return true; }; const auto& f = lay.fmt[(size_t) l]; const int64_t ELL = m.lat_ELL, G = m.lat_G; if (!m.cache || !m.host_res) { err = "prefill latent: the expert cache is required (--expert-cache)"; return false; } if (m.up_ring.h2d(m.tok, m.lat_tokx_host.data(), (size_t) T * K * 4, m.cs) != cudaSuccess || m.up_ring.h2d(m.tok_ix, m.lat_idsx_host.data(), (size_t) T * K * 4, m.cs) != cudaSuccess || m.up_ring.h2d(m.ids_p, m.lat_idsp_host.data(), (size_t) T * K * 4, m.cs) != cudaSuccess) { err = "prefill latent: the plan tables' upload failed"; return false; } { int32_t duo[2] = {0, (int32_t) (T * K)}; m.up_ring.h2d(m.lat_n, duo, 8, m.cs); } // STRATA_LATENT_FUSED_DU (on by default, =0 the iq_mmvq/latent_u_grouped path): the D // and U dense projections on moe_fused.cu's fused dense kernels - the Q8_0 weights on // the int8 tensor cores, the D stage's products quantized straight to the xl rows' // own q8_1 blocks (the FP32 xl round trip never runs). static const bool lat_du_env = [] { const char* v = std::getenv("STRATA_LATENT_FUSED_DU"); return v == nullptr || v[0] != '0'; }(); const bool lat_du = lat_du_env && fused::latent_fused_available(); // D: each token's 8 group products [T, G*ELL] -> q8_1 rows (xlq) pt.mark(kPfGather, m.cs); strata::kernels::quantize_q8_1_rows(m.mixed, T, (int) N, m.lat_xq, m.cs); if (lat_du) { // a colscaled pack (the Q3_K one: scales/l{l}.f32 beside its manifest) needs the // DU kernel that folds 1/s into the xl columns; v5's pack has no scales and keeps // the original kernel (its signature and its bits are untouched) const float* lat_inv = strata::kernels::lat_gu_colscale_dev((int) l, (int) ELL); if (lat_inv) fused_q3k::latent_dense_d(m.lat_d_dev + (size_t) (l - m.lat_first_layer) * m.lat_d.d_bytes(), m.lat_xq, T, (int) N, (int) G, ELL, m.xlq, m.cs, m.lat_fmt, lat_inv); else fused::latent_dense_d(m.lat_d_dev + (size_t) (l - m.lat_first_layer) * m.lat_d.d_bytes(), m.lat_xq, T, (int) N, (int) G, ELL, m.xlq, m.cs, m.lat_fmt); } else { strata::kernels::iq_mmvq(strata::kernels::lat_dense_fmt_to_type(m.lat_fmt), m.lat_d_dev + (size_t) (l - m.lat_first_layer) * m.lat_d.d_bytes(), m.lat_xq, m.xl, (int) N, (int) (G * ELL), T, m.cs); // Q4DU: fmt->ggml type (8 Q8_0 / 2 Q4_0) // Q3_K shared column scales: as in verify.cpp, scale the D stage's x by 1/s once, // before the quantize below (the prefill's only consumer of the f32 m.xl rows). if (const float* inv = strata::kernels::lat_gu_colscale_dev((int) l, (int) ELL)) strata::kernels::lat_colscale_rows(m.xl, (int64_t) T * G, (int) ELL, inv, m.cs); strata::kernels::quantize_q8_1_rows(m.xl, (int64_t) T * G, (int) ELL, m.xlq, m.cs); } { // STRATA_LAT_MOE_DUMP (debug): the latent arm's intermediates, layer 0 static const char* lat_moe_dump = std::getenv("STRATA_LAT_MOE_DUMP"); if (lat_moe_dump && l == 0) { std::vector hb; auto lat_wr = [&](const char* name, const void* dev, size_t bytes) { hb.resize(bytes); if (cudaMemcpyAsync(hb.data(), dev, bytes, cudaMemcpyDeviceToHost, m.cs) != cudaSuccess || cudaStreamSynchronize(m.cs) != cudaSuccess) { err = std::string("lat moe dump: the copy of ") + name + " failed"; return false; } char p[512]; std::snprintf(p, sizeof p, "%s/%s", lat_moe_dump, name); std::FILE* fp = std::fopen(p, "wb"); if (fp == nullptr) { err = std::string("lat moe dump: cannot write ") + p; return false; } std::fwrite(hb.data(), 1, bytes, fp); std::fclose(fp); return true; }; const size_t xrowb = (size_t) (ELL / 32) * 36; if (!lat_wr("d-xlq.bin", m.xlq, (size_t) T * G * xrowb)) return false; } } { // STRATA_LAT_MOE_DUMP (debug): the fused kernels' exact inputs, layer 0 static const char* lat_moe_dump4 = std::getenv("STRATA_LAT_MOE_DUMP"); if (lat_moe_dump4 && l == 0) { const int32_t E = (int32_t) m.g->n_expert; { // m.cnt / m.off (host) char p[512]; std::snprintf(p, sizeof p, "%s/g-cnt.bin", lat_moe_dump4); std::FILE* fp = std::fopen(p, "wb"); if (fp) { std::fwrite(m.cnt.data(), 4, (size_t) E, fp); std::fclose(fp); } std::snprintf(p, sizeof p, "%s/g-off.bin", lat_moe_dump4); fp = std::fopen(p, "wb"); if (fp) { std::fwrite(m.off.data(), 4, (size_t) E + 1, fp); std::fclose(fp); } } auto lat_dwr = [&](const char* name, const void* dev, size_t bytes) { std::vector hb(bytes); if (cudaMemcpyAsync(hb.data(), dev, bytes, cudaMemcpyDeviceToHost, m.cs) != cudaSuccess || cudaStreamSynchronize(m.cs) != cudaSuccess) return false; char p[512]; std::snprintf(p, sizeof p, "%s/%s", lat_moe_dump4, name); std::FILE* fp = std::fopen(p, "wb"); if (fp == nullptr) return false; std::fwrite(hb.data(), 1, bytes, fp); std::fclose(fp); return true; }; const size_t npitch = (size_t) (N / 32) * 36; if (!lat_dwr("i-xq.bin", m.lat_xq, (size_t) T * npitch)) return false; } } strata::kernels::latent_plan_rows(m.tok, m.tok, m.lat_n, m.tok_ix, m.lat_labels_dev + (size_t) l * m.lat_E, m.lat_E, 0, (int) G, (int) K, m.cs); { // STRATA_LAT_MOE_DUMP (debug): the plan tables after rewrite, layer 0 static const char* lat_moe_dump5 = std::getenv("STRATA_LAT_MOE_DUMP"); if (lat_moe_dump5 && l == 0) { auto lat_dwr = [&](const char* name, const void* dev, size_t bytes) { std::vector hb(bytes); if (cudaMemcpyAsync(hb.data(), dev, bytes, cudaMemcpyDeviceToHost, m.cs) != cudaSuccess || cudaStreamSynchronize(m.cs) != cudaSuccess) return false; char p[512]; std::snprintf(p, sizeof p, "%s/%s", lat_moe_dump5, name); std::FILE* fp = std::fopen(p, "wb"); if (fp == nullptr) return false; std::fwrite(hb.data(), 1, bytes, fp); std::fclose(fp); return true; }; if (!lat_dwr("g-tok.bin", m.tok, (size_t) T * K * 4)) return false; if (!lat_dwr("g-ids.bin", m.ids_p, (size_t) T * K * 4)) return false; } } // A: batches of busy experts in id order; a streamed blob waits on its ring slot // STRATA_EXPERT_NIBBLE: a nibble layer's cache slots hold 4-bit gate/up codes, which only // the nib layout reads (verify: L = nibble::layer(l).on ? layout_nib : layout, for the VRAM // share); a staged / PCIe blob is always the plain GGUF bytes. const bool nib_l = #if STRATA_EXPERT_NIBBLE_BUILT strata::core::nibble::layer(l).on; #else false; #endif auto la_for = [&](bool nib) { #if STRATA_EXPERT_NIBBLE_BUILT return nib ? strata::kernels::native_expert_layout_nib(f.gu_type, f.d_type, (int) ELL, (int) m.lat_I) : strata::kernels::native_expert_layout(f.gu_type, f.d_type, (int) ELL, (int) m.lat_I); #else (void) nib; return strata::kernels::native_expert_layout(f.gu_type, f.d_type, (int) ELL, (int) m.lat_I); #endif }; // STRATA_LATENT_FUSED (on by default, =0 the native grouped kernel): the A stage on // moe_fused.cu's fused batch kernels - the plain-GGUF NVFP4 blobs where they are // (resident slot or staged ring slot), the layer's whole grouping as its tables // (fused::LatentGeom / group_latent / experts_latent in moe_fused.hpp) static const bool lat_fused_env = [] { const char* v = std::getenv("STRATA_LATENT_FUSED"); return v == nullptr || v[0] != '0'; }(); static const bool lat_log = [] { const char* v = std::getenv("STRATA_LATENT_FUSED_LOG"); return v != nullptr && v[0] == '1'; }(); const bool lat_fused = lat_fused_env && !nib_l && fused::latent_fused_available() && ((f.gu_type == 40 && f.d_type == 40) || (lat_fused_q3k_on() && f.gu_type == 11 && f.d_type == 20 && m.lat_I == 640)); fused::LatentGeom lat_lg{}; size_t lat_h = 0; if (lat_fused) { const strata::kernels::NativeExpertLayout& lp = la_for(false); const int64_t nent = (int64_t) (T * K); const int lne = (int) m.g->n_expert; lat_lg.ell = (int64_t) ELL; lat_lg.iff = (int64_t) m.lat_I; lat_lg.gu_row = lp.gu_row; lat_lg.d_row = lp.d_row; lat_lg.up_off = lp.up_off; lat_lg.down_off = lp.down_off; lat_lg.tail_off = lp.tail_off; lat_h = fused::latent_h_off(nent, lne); if (strata::kernels::native_expert_scratch_bytes(nent, (int64_t) m.lat_I) < fused::latent_scratch_bytes(nent, lne, (int64_t) m.lat_I)) { err = "prefill latent: the fused A stage does not fit the grouped kernel's scratch"; return false; } std::vector lt(fused::latent_group_bytes(nent, lne)); fused::group_latent(m.cnt.data(), m.off.data(), lne, lt.data(), lt.size()); if (m.up_ring.h2d(m.lat_scratch, lt.data(), lt.size(), m.cs) != cudaSuccess) { err = "prefill latent: the fused tables' upload failed"; return false; } } // STRATA_LATENT_MMQ=1: the A stage on llama.cpp's int8 MMQ. xlq is (token,group)- // ordered and MMQ reads entry-ordered rows, so this layer builds the entry -> xl row // table once (m.tok, T*K entries); each launch then quantizes just its own rows from // it. Off by default: the native_expert_grouped path below stays the default. static const bool lat_mmq_env = [] { const char* v = std::getenv("STRATA_LATENT_MMQ"); return v != nullptr && v[0] == '1'; }(); const bool lat_mmq = lat_mmq_env && m.mmq_ctx != nullptr && m.Xq != nullptr && m.H != nullptr && m.Hq != nullptr && m.grp_gu != nullptr && m.grp_d != nullptr && m.bounds_dev != nullptr && !nib_l; // M4: STRATA_LAT_FUSED_Q3K=1 - the q3k pack only (Q3_K gate/up, IQ4_NL down, the 1280 / // 640 latent geometry), and it takes the launches in place of both other paths. const bool lat_fused_q3k = lat_fused_q3k_on() && f.gu_type == 11 /* GGML_TYPE_Q3_K */ && f.d_type == 20 /* GGML_TYPE_IQ4_NL */ && ELL == 1280 && m.lat_I == 640 && m.lat_hbuf != nullptr && m.lat_gcd != nullptr && !nib_l; if (lat_fused_q3k && !nib_l) { static bool noted = false; if (!noted) { noted = true; std::fprintf(stderr, "latfusedq3k: the A stage runs lat_q3k_fused (experts read in " "place, gather_native skipped), hbuf %lld B, T*K %lld entries\n", (long long) ((size_t) T * K * 800), (long long) (T * K)); } } std::vector lat_rows; // STRATA_LATENT_MMQ: entry -> xl row, this layer strata::prefill::lat::Tables lat_tab; // its per-layer row tables (built below) // One WAIT covers a whole copy batch, which can be many MMQ groups deep: the fused walk waits // once per ring/3 experts, and 16 is far too shallow to keep the copy engine fed. The MMQ // products still take 16 experts a launch (kGatherGroupMax), sliced out of the batch. constexpr size_t kLatBatch = 8 * LAT_GROUP; // Measured: a copy batch deeper than one product group (ring/3 = 128) was 6 % SLOWER at 4K // (712.3 vs 757.5) and 1.4 % at 16K - the flushes are host-cheap (0.43 ms each, profiled), // so there was nothing for a deeper batch to hide. One group per batch: the fused walk's // depth is for its one-launch-per-128 kernels, which the MMQ products cannot use. const size_t lat_batch = (size_t) LAT_GROUP; if (lat_mmq) { if (l == 0 && std::getenv("STRATA_LATENT_MMQ_DEBUG") != nullptr) { int32_t mx = 0, mn = (int32_t) 2147483647; for (int32_t v : m.lat_tokx_host) { mx = std::max(mx, v); mn = std::min(mn, v); } std::fprintf(stderr, "latmmq-dbg: T=%lld K=%d G=%lld ELL=%lld TG=%lld TK=%lld " "tok[min,max]=[%d,%d] nb_experts=%lld\n", (long long) T, (int) K, (long long) G, (long long) ELL, (long long) (T * G), (long long) (T * K), (int) mn, (int) mx, (long long) m.g->n_expert); } // xlq's rows are (token, group): xl row = src*G + labels[l*E + expert], NOT the // default path's m.tok (src*K + kk, which reads past T*G rows whenever K > G). // m.tok is rebuilt for this branch only; the default path below is not taken. lat_rows.assign((size_t) (T * K), 0); for (size_t p2 = 0; p2 < lat_rows.size(); ++p2) { const int32_t s2 = m.lat_tokx_host[p2] / (int32_t) K; const int32_t e2 = m.lat_idsp_host[p2]; const size_t li = (size_t) l * (size_t) m.lat_E + (size_t) e2; lat_rows[p2] = s2 * (int32_t) G + (li < m.lat_labels.size() ? m.lat_labels[li] : 0); } // The layer's two row tables, in the walk's order: a group is one contiguous run of them, // so a flush uploads only its bounds and shifts the two pointers by its base - instead of // rebuilding both tables on the host and uploading them at every group. lat_tab = strata::prefill::lat::build(order, m.off.data(), m.cnt.data(), lat_rows); if (lat_tab.rows > 0 && (m.up_ring.h2d(m.lat_gxl, lat_tab.xl.data(), (size_t) lat_tab.rows * 4, m.cs) != cudaSuccess || m.up_ring.h2d(m.lat_gdst, lat_tab.entry.data(), (size_t) lat_tab.rows * 4, m.cs) != cudaSuccess)) { err = "prefill latent: the layer's row tables' upload failed"; return false; } } // STRATA_LATENT_MMQ: the A stage's gathers are collected up to LAT_GROUP experts and run as // one gate/up + SwiGLU + down set. A 4K prompt's experts average ~80 rows, so a per-expert // Product was ~100 us of launch for ~0.1 GFLOP: 24.6k launches, `gemm down` alone 40 % of // the chunk's GPU time. The rows also have to be one contiguous entry range (the row table // slice and the relative bounds both assume it), so a group flushes early when it is not. const size_t lat_gub = mmq::matrix_bytes(f.gu_type, 1280, (int64_t) ELL); const size_t lat_db = mmq::matrix_bytes(f.d_type, (int64_t) ELL, 640); size_t lat_q = 0, lat_nflush = 0, lat_nexp = 0, lat_maxq = 0; static const size_t lat_gmax = std::getenv("STRATA_LATENT_MMQ_G1") != nullptr ? 1 : (std::getenv("STRATA_LATENT_MMQ_G") != nullptr ? (size_t) std::atoi(std::getenv("STRATA_LATENT_MMQ_G")) : (size_t) LAT_GROUP); // A/B: the group's expert count // The MMQ buffers hold stream_all_min() - 1 tokens' rows (moe_bufs), so a pending group is capped // by ROWS, not by expert count: sixteen experts can carry more rows than that (a 4K chunk's top // sixteen held far more than the 10.2k-row cap), and a group over the cap ran the gate/up product // past m.GU into the next buffer - the illegal memory access the trace named at the gu launch. const int64_t lat_rowcap = std::min(T, stream_all_min() - 1) * (int64_t) K; int64_t lat_nr = 0; int64_t lat_base = 0; // the layer tables' first row of the group being filled int32_t lat_gq[LAT_GROUP], lat_ge[LAT_GROUP]; int32_t lat_gc[kLatBatch]; // per batch entry: its row COUNT (-1: take gs's difference) int32_t lat_cnt[LAT_GROUP]; // the GROUP's counts, in z - the walk's batch position is a // different index once a cap flush resets z mid-batch for (int32_t& v : lat_gc) v = -1; auto arm_flush = [&]() -> bool { if (!lat_mmq || lat_q == 0) return true; const size_t nb2 = lat_q; lat_q = 0; // lat flush breakdown: host time per step of one flush, summed over the chunk auto now2 = [] { return std::chrono::steady_clock::now(); }; auto ms2 = [](auto x, auto y) { return std::chrono::duration(y - x).count(); }; static double P[6] = {0, 0, 0, 0, 0, 0}; const auto T0 = now2(); auto tr = [&](const char* tag) -> bool { static const bool on = std::getenv("STRATA_LATENT_MMQ_TRACE") != nullptr; if (!on) return true; const cudaError_t e = cudaStreamSynchronize(m.cs); if (e != cudaSuccess || cudaGetLastError() != cudaSuccess) { std::fprintf(stderr, "latmmq-trace FAIL %s l=%d nb2=%zu: %s\n", tag, (int) l, nb2, cudaGetErrorString(cudaGetLastError())); return false; } std::fprintf(stderr, "latmmq-trace ok %s l=%d nb2=%zu\n", tag, (int) l, nb2); return true; }; ++lat_nflush; lat_nexp += nb2; lat_maxq = std::max(lat_maxq, nb2); // The buffers hold this group's rows as an expert-major concatenation, 0..nr-1, so a // group never needs a contiguous entry range: off[] is NOT monotonic in expert id (the // resident block is laid out first), and requiring it let a group average 2.6 experts // instead of LAT_GROUP - which kept the products in stream-k + fixup mode. // This group's rows are the layer tables' run [lat_base, lat_base + nr), so only its // bounds are per group here. lat_gc carries the count where the walk knows it; the // per-expert and resident walks leave it -1, and there gs's difference is the count. std::vector bh((size_t) (nb2 + 1)); int32_t at = 0; bh[0] = 0; for (size_t q = 0; q < nb2; ++q) { at += lat_cnt[q]; bh[q + 1] = at; } const int64_t nr = (int64_t) at; if (lat_base + nr > lat_tab.rows) { std::fprintf(stderr, "prefill latent: the group's rows [%lld, %lld) pass the layer's %lld " "(lat_q=%zu nb2=%zu)\n", (long long) lat_base, (long long) (lat_base + nr), (long long) lat_tab.rows, lat_q, nb2); for (size_t q = 0; q < nb2; ++q) std::fprintf(stderr, " g[%zu] base=%d end=%d cnt=%d\n", q, lat_gq[q], lat_ge[q], lat_gc[q]); err = "prefill latent: the group's rows pass the layer tables"; return false; } lat_nr = 0; // lat_gc is NOT cleared here: a flush can fall in the middle of a batch (the row cap), and // the experts after it still need their own counts. Each layer initialises it to -1. if (nr <= 0) return true; // the gate/up and down rows of this launch only: rows 0..nr-1 of Xq/GU/H/Hq. Under // stream_all those buffers hold stream_all_min() - 1 tokens' rows, not the chunk's // T*K, so an absolute row index writes past them (T >= 1024 went non-finite). cudaMemsetAsync(m.grp_gu + nb2 * lat_gub, 0, MMQ_TAIL, m.cs); cudaMemsetAsync(m.grp_d + nb2 * lat_db, 0, MMQ_TAIL, m.cs); if (!tr("memset")) return false; if (!tr("pre-upload")) return false; if (m.up_ring.h2d(m.bounds_dev, bh.data(), bh.size() * 4, m.cs) != cudaSuccess) { if (!tr("upload")) return false; err = "prefill latent: the MMQ group's bounds' upload failed"; return false; } int64_t maxr = 0; for (size_t q = 0; q < nb2; ++q) maxr = std::max(maxr, (int64_t) (bh[q + 1] - bh[q])); const auto T1 = now2(); P[0] += ms2(T0, T1); // Xq is MMQ's q8_1 (block_q8_1_mmq: 128-value blocks, transposed, row stride // pad512(ELL) values), filled from the f32 latent rows by MMQ's own quantizer, which // gathers row i from ids[i]. xlq holds the engine's kv q8_1 rows (36-byte blocks, // no padding) - a different layout, only the native/decode kernels can read it. pt.mark(kPfGemmGU, m.cs); mmq::quantize(m.xl, m.lat_gxl + lat_base, m.Xq, f.gu_type, (int64_t) ELL, (int64_t) ELL, nr, m.cs); const auto T2 = now2(); P[1] += ms2(T1, T2); if (!tr("quant_xq")) return false; if (std::getenv("STRATA_LATENT_MMQ_TRACE") != nullptr) { std::vector hi((size_t) nr), hb2(nb2 + 1); cudaMemcpy(hi.data(), m.ids_identity, (size_t) nr * 4, cudaMemcpyDeviceToHost); cudaMemcpy(hb2.data(), m.bounds_dev, (nb2 + 1) * 4, cudaMemcpyDeviceToHost); int32_t imn = hi[0], imx = hi[0]; for (int32_t v : hi) { imn = std::min(imn, v); imx = std::max(imx, v); } std::fprintf(stderr, "latmmq-ids: nr=%lld id[min,max]=[%d,%d] bounds[0]=%d bounds[nb2]=%d nb2=%zu\n", (long long) nr, imn, imx, hb2[0], hb2[nb2], nb2); } static const bool lat_tr = std::getenv("STRATA_LATENT_MMQ_TRACE") != nullptr; static bool lat_said = false; if (lat_tr && !lat_said) { lat_said = true; const MmqPlan& mpp = mmq_plan(); const MoeBufs mbx = moe_bufs((size_t) T, m.g->n_expert, false); std::fprintf(stderr, "latmmq-cap: any=%d gu_max=%zu d_max=%zu gub=%zu db=%zu mb_gu=%zu mb_h=%zu " "mb_hq=%zu mb_xq=%zu " "ids=%p bounds=%p grp_gu=%p grp_d=%p xl=%p Xq=%p GU=%p H=%p Hq=%p yl=%p nr=%lld maxr=%lld\n", (int) mpp.any, mpp.gu_max, mpp.d_max, lat_gub, lat_db, mbx.gu, mbx.h, mbx.hq, mbx.xq, (void*) m.ids_identity, (void*) m.bounds_dev, (void*) m.grp_gu, (void*) m.grp_d, (void*) m.xl, (void*) m.Xq, (void*) m.GU, (void*) m.H, (void*) m.Hq, (void*) m.yl, (long long) nr, (long long) maxr); for (size_t q = 0; q <= nb2 && q < 17; ++q) std::fprintf(stderr, "latmmq-bh[%zu]=%d\n", q, bh[q]); } { // latmmq-dev: which device each buffer lives on, and the current one static bool devsaid = false; if (!devsaid && std::getenv("STRATA_LATENT_MMQ_TRACE") != nullptr) { devsaid = true; int cur = -1; cudaGetDevice(&cur); auto devof = [](const void* p2) -> int { cudaPointerAttributes a{}; if (cudaPointerGetAttributes(&a, p2) != cudaSuccess) { cudaGetLastError(); return -1; } return (int) a.device; }; auto typeof = [](const void* p2) -> int { cudaPointerAttributes a2{}; if (cudaPointerGetAttributes(&a2, p2) != cudaSuccess) { cudaGetLastError(); return -1; } return (int) a2.type; // 1 host, 2 device, 3 managed }; std::fprintf(stderr, "latmmq-mem: xl=%d yl=%d H=%d Hq=%d GU=%d Xq=%d Dm=%d grp_gu=%d grp_d=%d " "ids=%d (1=host,2=device,3=managed)\n", typeof(m.xl), typeof(m.yl), typeof(m.H), typeof(m.Hq), typeof(m.GU), typeof(m.Xq), typeof(m.Dm), typeof(m.grp_gu), typeof(m.grp_d), typeof(m.ids_identity)); std::fprintf(stderr, "latmmq-dev: cur=%d ids=%d bounds=%d grp_gu=%d grp_d=%d Xq=%d " "GU=%d H=%d Hq=%d yl=%d xl=%d gxl=%d gdst=%d\n", cur, devof(m.ids_identity), devof(m.bounds_dev), devof(m.grp_gu), devof(m.grp_d), devof(m.Xq), devof(m.GU), devof(m.H), devof(m.Hq), devof(m.yl), devof(m.xl), devof(m.lat_gxl), devof(m.lat_gdst)); } } auto run_p = [&](const mmq::Product& pr) { static const bool alt = std::getenv("STRATA_LATENT_MMQ_ALTCTX") != nullptr; static std::unique_ptr altctx; if (alt) { if (!altctx) altctx = std::make_unique(); altctx->run(pr, m.cs); return; } m.mmq_ctx->run(pr, m.cs); }; mmq::Product gu; gu.w = m.grp_gu; gu.type = f.gu_type; gu.w_rows = 1280; gu.w_cols = (int64_t) ELL; gu.expert_bytes = lat_gub; gu.n = (int) nb2; gu.xq = m.Xq; gu.bounds = m.bounds_dev; gu.ids = m.ids_identity; gu.total_rows = nr; gu.max_rows = maxr; gu.dst = m.GU; gu.ld_dst = 1280; run_p(gu); const auto T3 = now2(); P[2] += ms2(T2, T3); if (!tr("gu")) return false; pt.mark(kPfDequant, m.cs); // gate/up = quantize(xl) + the gu kernel mmq::swiglu(m.GU, m.H, nr, 640, false, m.cs); if (!tr("swiglu")) return false; pt.mark(kPfGemmD, m.cs); mmq::quantize(m.H, nullptr, m.Hq, f.d_type, 640, 640, nr, m.cs); const auto T4 = now2(); P[3] += ms2(T3, T4); if (!tr("quant_h")) return false; mmq::Product dn; dn.w = m.grp_d; dn.type = f.d_type; dn.w_rows = (int64_t) ELL; dn.w_cols = 640; dn.expert_bytes = lat_db; dn.n = (int) nb2; dn.xq = m.Hq; // A/B only (wrong answers): is the scattered dst row - yl[entry], with entry not // monotonic in buffer row - what makes the down product slow? static const bool lat_dn_ident = std::getenv("STRATA_LATENT_MMQ_IDENT") != nullptr; dn.bounds = m.bounds_dev; dn.ids = lat_dn_ident ? m.ids_identity : (m.lat_gdst + lat_base); dn.total_rows = nr; dn.max_rows = maxr; dn.dst = m.yl; dn.ld_dst = (int64_t) ELL; pt.mark(kPfGemmD, m.cs); // dequant phase = swiglu + quantize(H) run_p(dn); const auto T5 = now2(); P[4] += ms2(T4, T5); if (!tr("dn")) return false; pt.mark(kPfCombine, m.cs); // gemm down = the dn kernel alone lat_base += nr; P[5] += ms2(T0, now2()); if (l % 8 == 7 && std::getenv("STRATA_LATENT_MMQ_PROF") != nullptr) std::fprintf(stderr, "latmmq-prof: l=%d pre=%.0f quant=%.0f gu=%.0f h=%.0f dn=%.0f " "all=%.0f ms flushes=%zu\n", (int) l, P[0], P[1], P[2], P[3], P[4], P[5], lat_nflush); return true; }; const size_t n_order = order.size(); unsigned long long gp[kLatBatch]; // host tables, uploaded per launch: the grouped int32_t gs[kLatBatch + 1]; // kernel reads grp_ptr/grp_start on the device auto arm_launch = [&](const strata::kernels::NativeExpertLayout& La, size_t nb, size_t q0 = 0) { if (m.up_ring.h2d(m.lat_gp, gp, nb * 8, m.cs) != cudaSuccess || m.up_ring.h2d(m.lat_gs, gs, (nb + 1) * 4, m.cs) != cudaSuccess) { err = "prefill latent: the group tables' upload failed"; return false; } { int32_t ngi = (int32_t) nb; m.up_ring.h2d(m.lat_n, &ngi, 4, m.cs); } pt.mark(kPfGemmGU, m.cs); // STRATA_LATENT_MMQ=1: the A stage on llama.cpp's int8 MMQ, the path the non-latent // native packs already take (gather_native + Product). xlq is (token,group)-ordered // and MMQ reads entry-ordered rows, so its rows are gathered by m.tok first; the // experts' blobs are gathered into MMQ's group matrices; gate/up, SwiGLU and down // are then three launches over the whole batch instead of one grouped kernel that // re-reads each expert per entry. Off by default (native_expert_grouped below). if (lat_fused_q3k) { // M4: the group's row count - lat_gc where the walk knows it, else the gs // difference (the same resolution the MMQ path does), so starts[] may be in any // order and need not tile the entry space int32_t lc[LAT_GROUP]; for (size_t q = 0; q < nb; ++q) { const int32_t c = lat_gc[q0 + q]; lc[q] = c >= 0 ? c : (int32_t) (gs[q0 + q + 1] - gs[q0 + q]); } if (m.up_ring.h2d(m.lat_gcd, lc, nb * 4, m.cs) != cudaSuccess) { err = "prefill latent: the per-group row counts' upload failed"; return false; } static const bool fq_nprof = std::getenv("STRATA_LATENT_NPROF") != nullptr; cudaEvent_t ev0 = nullptr, ev1 = nullptr; if (fq_nprof) { cudaEventCreate(&ev0); cudaEventCreate(&ev1); cudaEventRecord(ev0, m.cs); } strata::prefill::lat_q3k_fused((const uint8_t* const*) m.lat_gp, m.lat_gs, (int) nb, m.xlq, m.lat_hbuf, m.yl, ELL, m.cs, m.lat_gcd, m.tok, m.ids_identity); if (fq_nprof) { cudaEventRecord(ev1, m.cs); cudaEventSynchronize(ev1); float cms = 0.f; cudaEventElapsedTime(&cms, ev0, ev1); const long long ent = (long long) gs[q0 + nb] - (long long) gs[q0]; static double acc = 0; static long long ncall = 0, nent = 0; acc += cms; ++ncall; nent += ent; if (ncall % 24 == 0) std::fprintf(stderr, "latfq3kprof: calls=%lld ms=%.0f entries=%lld us/entry=%.2f " "last: nb=%zu ent=%lld cap=%lld T=%lld K=%lld\n", ncall, acc, nent, nent > 0 ? acc * 1000.0 / (double) nent : 0.0, nb, ent, (long long) (T * K), (long long) T, (long long) K); cudaEventDestroy(ev0); cudaEventDestroy(ev1); } return true; } if (lat_mmq) { if (m.xl == nullptr) { err = "prefill latent: the MMQ A stage needs the f32 latent rows"; return false; } pt.mark(kPfGather, m.cs); for (size_t q = 0; q < nb; ++q) { const int64_t c = lat_gc[q0 + q] >= 0 ? (int64_t) lat_gc[q0 + q] : (int64_t) gs[q0 + q + 1] - (int64_t) gs[q0 + q]; // a group is one contiguous run of rows in the buffers: close it before it would pass the cap if (lat_q > 0 && lat_nr + c > lat_rowcap && !arm_flush()) return false; const size_t z = lat_q; const uint8_t* bd = (const uint8_t*) gp[q0 + q]; static const bool lat_nogather = std::getenv("STRATA_LATENT_MMQ_NOGATHER") != nullptr; if (!lat_nogather) // A/B only: the answers are wrong with this on mmq::gather_native(bd, bd + f.up_off, lat_gub / 2, bd + f.down_off, lat_db, m.grp_gu + z * lat_gub, m.grp_d + z * lat_db, m.cs); lat_gq[z] = gs[q0 + q]; lat_ge[z] = (int32_t) ((int64_t) gs[q0 + q] + c); lat_cnt[z] = (int32_t) c; lat_nr += c; ++lat_q; if (lat_q == lat_gmax && !arm_flush()) return false; } pt.mark(kPfDequant, m.cs); // the gather kernels' own time return true; } { static const bool nprof = std::getenv("STRATA_LATENT_NPROF") != nullptr; cudaEvent_t ev0 = nullptr, ev1 = nullptr; if (nprof) { cudaEventCreate(&ev0); cudaEventCreate(&ev1); cudaEventRecord(ev0, m.cs); } strata::kernels::native_expert_grouped(La, m.lat_gp, m.lat_gs, m.lat_n, m.ids_identity, m.tok, (int64_t) nb, (int64_t) (T * K), m.xlq, m.lat_scratch, m.yl, m.cs, 0); if (nprof) { cudaEventRecord(ev1, m.cs); cudaEventSynchronize(ev1); float cms = 0.f; cudaEventElapsedTime(&cms, ev0, ev1); const long long ent = (long long) gs[q0 + nb] - (long long) gs[q0]; static double acc = 0; static long long ncall = 0, nent = 0; acc += cms; ++ncall; nent += ent; if (ncall % 24 == 0) std::fprintf(stderr, "latnprof: calls=%lld ms=%.0f entries=%lld us/entry=%.2f " "last: nb=%zu ent=%lld cap=%lld T=%lld K=%lld\n", ncall, acc, nent, nent > 0 ? acc * 1000.0 / (double) nent : 0.0, nb, ent, (long long) (T * K), (long long) T, (long long) K); cudaEventDestroy(ev0); cudaEventDestroy(ev1); } } return true; }; // A: per-expert grouped launches (the kernel's grp_start ranges tile contiguous entries, so a // layout-mixed batch cannot share a launch): resident slots take the nib layout on nib // layers, staged blobs (plain GGUF bytes) the plain one. size_t staged = 0; const size_t lookahead = (size_t) stage_n - 1; if (lat_fused) { // A on the fused batch kernels, #136's walk: the experts in id order, one WideBatch // launch per kMaxBatch of them - at most a third of the ring streamed, so the next // batch's blobs arrive while one computes. An expert the routing did not pick has no // tiles (its blob pointer is read only if it has); its ring slot is given back with // its batch. The blobs are wherever they are (a resident cache slot, a staged ring // slot, the peer's VRAM), all the same plain native NVFP4 layout. auto lat_fire = [&](const fused::WideBatch& fb) { if (lat_log) { int nrouted = 0, nnull = 0; for (int ee = fb.e0; ee < fb.e1; ++ee) { if (m.cnt[(size_t) ee] > 0) ++nrouted; if (fb.blob[ee - fb.e0] == nullptr) ++nnull; } std::fprintf(stderr, "latfused: l=%d e0=%d e1=%d span=%d routed=%d nullblob=%d n=%lld " "xlq=%p tok=%p scratch=%p lat_h=%zu ring=%d\n", (int) l, fb.e0, fb.e1, fb.e1 - fb.e0, nrouted, nnull, (long long) (T * K), (void*) m.xlq, (void*) m.tok, (void*) m.lat_scratch, lat_h, (int) m.ring); } // off[] is a permutation for the latent grouping (the residents' block first), // so the grid bound is the whole chunk, not off[e1] - off[e0] // the q3k pack takes the same walk with Q3_K gate/up and IQ4_NL down in place of // the NVFP4 blocks; every other pack takes the original. `fb` already carries the // batch's blobs by expert id and `m.tok` the entry -> xl row map, so the two calls // differ only in the decode. // the geometry is the pack's own: lat_lg carries gu_row / d_row / up_off / down_off / // tail_off read from its native_experts.txt, so ell 1280 and 1792 both work if (f.gu_type == 11 /* GGML_TYPE_Q3_K */ && f.d_type == 20 /* GGML_TYPE_IQ4_NL */ && m.lat_I == 640) { fused_q3k::experts_latent_q3k(fb, lat_lg, (int) m.g->n_expert, (int64_t) (T * K), m.lat_scratch, m.xlq, m.tok, m.lat_scratch + lat_h, m.yl, m.cs); } else { fused::experts_latent(fb, lat_lg, (int) m.g->n_expert, (int64_t) (T * K), m.lat_scratch, m.xlq, m.tok, m.lat_scratch + lat_h, m.yl, m.cs); } }; auto lat_blob = [&](int32_t e) -> const uint8_t* { const bool r0 = m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0; const uint8_t* bp = r0 ? m.cache->device_slot(m.host_res[(size_t) l * m.g->n_expert + e]) : (m.pp && m.pp->peer ? m.pp->peer->slot_ptr(l, e) : nullptr); if (bp == nullptr && m.cnt[(size_t) e] > 0) { err = "prefill latent: an expert is neither resident, streamed nor on the peer"; return nullptr; } if (r0) ++stats_.experts_resident; return bp; // null: unrouted, the kernels skip it (no tiles) }; if (stream_all) { const size_t per = (size_t) std::max(1, (int64_t) m.ring / 3); size_t k = seq_start[(size_t) l]; const size_t kend = seq_start[(size_t) l + 1]; for (int32_t e = 0; e < m.g->n_expert;) { fused::WideBatch fb; fb.e0 = e; const size_t k0 = k; for (; e < m.g->n_expert && e - fb.e0 < fused::kMaxBatch; ++e) { if (k < kend && seq[k].e == e) { if (k - k0 == per) break; fb.blob[e - fb.e0] = m.stage_dev[k % (size_t) m.ring]; ++k; } else { fb.blob[e - fb.e0] = lat_blob(e); if (fb.blob[e - fb.e0] == nullptr && !err.empty()) return false; } } fb.e1 = e; if (k > k0) { // one copy stream, in order: the batch's last blob covers the others pt.mark(kPfWaitCopy, m.cs); wait_issued(k - 1); wait_copied(k - 1, (k - 1) % (size_t) m.ring); } pt.mark(kPfGemmGU, m.cs); lat_fire(fb); for (size_t kk = k0; kk < k; ++kk) { cudaEventRecord(m.used[kk % (size_t) m.ring], m.cs); m.used_of[kk % (size_t) m.ring] = (int) (kk % (size_t) m.ring); } if (k > k0) { consumed = k; give_back(consumed); } } } else { // the staged walk: the same lookahead keeps the copies coming; a batch waits // only for its last staged blob (one copy stream, in order) size_t nb = 0, nst = 0; // an open batch keeps its staged slots until it launches (staged_batch.hpp) const strata::prefill::StagedBatchPlan plan = strata::prefill::staged_batch_plan(stage_n, LAT_GROUP); int fb_stage[LAT_GROUP]; fused::WideBatch fb; int32_t bfirst = 0, blast = 0; auto lat_flush = [&]() -> bool { if (nb == 0) return true; fb.e0 = bfirst; fb.e1 = blast + 1; if (nst > 0) { pt.mark(kPfWaitCopy, m.cs); cudaStreamWaitEvent(m.cs, m.copied[fb_stage[nst - 1]], 0); } pt.mark(kPfGemmGU, m.cs); lat_fire(fb); for (size_t i = 0; i < nst; ++i) { // the staged slots free once the launch is done cudaEventRecord(m.used[fb_stage[i]], m.cs); m.used_of[fb_stage[i]] = fb_stage[i]; } nb = nst = 0; return true; }; for (size_t j = 0; j < n_order; ++j) { const int32_t e = order[j]; while (staged < n_order && staged <= j + plan.ahead) { if (!stage_one(staged)) return false; ++staged; } if (nb == 0) bfirst = e; if (stage_of[j] < 0) { // resident (or on the peer) fb.blob[e - bfirst] = lat_blob(e); if (fb.blob[e - bfirst] == nullptr && !err.empty()) return false; } else { fb.blob[e - bfirst] = m.stage_dev[stage_of[j]]; fb_stage[nst++] = stage_of[j]; } blast = e; ++nb; if (nb == plan.group_limit || (size_t) (e + 1 - bfirst) >= fused::kBatchRoom) { if (!lat_flush()) return false; } } if (!lat_flush()) return false; } } else if (stream_all) { // STRATA_LATENT_MMQ: the same walk, but ONE wait per batch of LAT_GROUP experts, as // the fused path waits once per batch: the batch's copies are ordered on m.copy, so // the last one covers the others, and one gate/up + down set runs over all of them. // Waiting per expert made the A stage copy -> compute -> copy with no overlap (the // GPU sat at ~45 %): that, not the kernels, was the whole gap to the fused path. // STRATA_LATENT_BATCH=1 (opt-in until verified end to end): the batched walk for the // native path too. STRATA_LATENT_NOBATCH=1 keeps it on for the MMQ path only. static const bool lat_batched = lat_mmq ? std::getenv("STRATA_LATENT_NOBATCH") == nullptr : std::getenv("STRATA_LATENT_BATCH") != nullptr; if (lat_batched) { size_t k = seq_start[(size_t) l]; const size_t kend = seq_start[(size_t) l + 1]; auto release_to = [&](int32_t e_stop) { while (k < kend && seq[k].e < e_stop) { const int sl = (int) (k % (size_t) m.ring); cudaEventRecord(m.used[sl], m.cs); m.used_of[sl] = sl; consumed = ++k; give_back(consumed); } }; static double lat_wait_ms[2] = {0, 0}; // [0] issued, [1] copied size_t nb = 0, k0 = k; auto fire = [&]() -> bool { if (nb == 0) return true; if (k > k0) { auto now = [] { return std::chrono::steady_clock::now(); }; auto ms = [](auto a, auto b) { return std::chrono::duration(b - a).count(); }; const auto t0 = now(); wait_issued(k - 1); const auto t1 = now(); pt.mark(kPfWaitCopy, m.cs); wait_copied(k - 1, (k - 1) % (size_t) m.ring); const auto t2 = now(); lat_wait_ms[0] += ms(t0, t1); lat_wait_ms[1] += ms(t1, t2); } { // STRATA_LATENT_MMQ_TRACE: the batch's shape and blob pointers static int dbgn = 0; if (dbgn < 2 && std::getenv("STRATA_LATENT_MMQ_TRACE") != nullptr) { ++dbgn; std::fprintf(stderr, "latmmq-fire l=%d nb=%zu ring=%d\n", (int) l, nb, (int) m.ring); for (size_t q = 0; q < nb; ++q) std::fprintf(stderr, "latmmq-fire[%zu] gp=%llx gs=%d..%d\n", q, gp[q], gs[q], gs[q + 1]); } } for (size_t q0 = 0; q0 < nb; q0 += (size_t) LAT_GROUP) // the batch's groups if (!arm_launch(la_for(false), std::min((size_t) LAT_GROUP, nb - q0), q0)) return false; for (size_t kk = k0; kk < k; ++kk) { // a slot is refilled only once the kernels cudaEventRecord(m.used[kk % (size_t) m.ring], m.cs); // that read it are done m.used_of[kk % (size_t) m.ring] = (int) (kk % (size_t) m.ring); } if (k > k0) { consumed = k; give_back(consumed); } nb = 0; k0 = k; return true; }; for (size_t j = 0; j < n_order; ++j) { const int32_t e = order[j]; release_to(e); if (k < kend && seq[k].e == e) { gp[nb] = (unsigned long long) m.stage_dev[k % (size_t) m.ring]; ++k; ++stats_.experts_streamed; } else { const bool r0 = m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0; const uint8_t* bp = r0 ? m.cache->device_slot(m.host_res[(size_t) l * m.g->n_expert + e]) : (m.pp && m.pp->peer ? m.pp->peer->slot_ptr(l, e) : nullptr); if (bp == nullptr) { err = "prefill latent: an expert is neither resident, streamed nor on the peer"; return false; } ++stats_.experts_resident; gp[nb] = (unsigned long long) bp; } // An entry carries BOTH things: gs is the expert's absolute entry base (what the row // table slice and the dst rows are keyed by), lat_gc its row count (what the group's // buffer rows and the launch grid are keyed by). Using gs's consecutive differences // as the counts was wrong: off[] is NOT monotonic in expert id under stream_all (the // resident block is laid out first), so a group's bounds ran backwards, max_rows came // out ~10x total_rows, and the gate/up launch read ids past nr and wrote dst rows past // m.GU - the illegal memory access the trace named at the gu launch. Only a group whose // experts happened to sit in off[] order (the layer's first) came out right. // native_expert_grouped takes no counts: group g's rows are // [grp_start[g], grp_start[g+1]). So a batch may only hold experts whose row // ranges are contiguous - flush when this one does not start where the last // ended (under stream_all the rows are slot-ordered, the walk id-ordered). if (!lat_mmq && !lat_fused_q3k && nb > 0 && (int32_t) m.off[(size_t) e] != gs[nb]) { if (!fire()) return false; } gs[nb] = (int32_t) m.off[(size_t) e]; gs[nb + 1] = (int32_t) (m.off[(size_t) e] + m.cnt[(size_t) e]); lat_gc[nb] = (int32_t) m.cnt[(size_t) e]; ++nb; if (nb == lat_batch && !fire()) return false; } if (!fire()) return false; release_to(m.g->n_expert); } else { // the streamed walk, with the original walk's bookkeeping below: this layer's // entries [k, kend) in id order; an entry the routing did not pick only gives // its slot back (the group walk never runs on the latent arm, so the MMQ // group-gather sides of the original conditions are provably inert) size_t k = seq_start[(size_t) l]; const size_t kend = seq_start[(size_t) l + 1]; auto release_to = [&](int32_t e_stop) { while (k < kend && seq[k].e < e_stop) { const int sl = (int) (k % (size_t) m.ring); cudaEventRecord(m.used[sl], m.cs); m.used_of[sl] = sl; consumed = ++k; give_back(consumed); } }; for (size_t j = 0; j < n_order; ++j) { const int32_t e = order[j]; release_to(e); if (k < kend && seq[k].e == e) { const int sl = (int) (k % (size_t) m.ring); wait_issued(k); pt.mark(kPfWaitCopy, m.cs); wait_copied(k, (size_t) sl); gp[0] = (unsigned long long) m.stage_dev[sl]; gs[0] = (int32_t) m.off[(size_t) e]; gs[1] = (int32_t) (m.off[(size_t) e] + m.cnt[(size_t) e]); // staged bytes: always the plain GGUF layout if (!arm_launch(la_for(false), 1)) return false; consumed = ++k; give_back(consumed); // the slot frees once this launch is done } else { const bool r0 = m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0; const uint8_t* bp = r0 ? m.cache->device_slot(m.host_res[(size_t) l * m.g->n_expert + e]) : (m.pp && m.pp->peer ? m.pp->peer->slot_ptr(l, e) : nullptr); if (bp == nullptr) { err = "prefill latent: an expert is neither resident, streamed nor on the peer"; return false; } gp[0] = (unsigned long long) bp; gs[0] = (int32_t) m.off[(size_t) e]; gs[1] = (int32_t) (m.off[(size_t) e] + m.cnt[(size_t) e]); ++stats_.experts_resident; // the cache slot's nib layout on nib layers if (!arm_launch(la_for(nib_l), 1)) return false; } } release_to(m.g->n_expert); } } else { // A: the resident experts share grouped launches (off[] put their entries // first, so up to LAT_GROUP of them tile one launch's group table - the // ranges stay contiguous because the residents were assigned offsets in id // order as one block). A staged blob still launches alone behind its copy's // event: the staging ring's lookahead overlap is what it is for. // STRATA_EXPERT_NIBBLE: the cache slots' nib layout on nib layers; a staged // blob is always the plain GGUF bytes. size_t nb = 0; for (size_t j = 0; j < n_order; ++j) { const int32_t e = order[j]; while (staged < n_order && staged <= j + lookahead) { if (!stage_one(staged)) return false; ++staged; } if (stage_of[j] < 0) { // resident: accumulate into the shared launch gp[nb] = (unsigned long long) m.cache->device_slot( m.host_res[(size_t) l * m.g->n_expert + e]); gs[nb] = (int32_t) m.off[(size_t) e]; gs[nb + 1] = (int32_t) (m.off[(size_t) e] + m.cnt[(size_t) e]); ++nb; ++stats_.experts_resident; if (nb == (size_t) LAT_GROUP) { if (!arm_launch(la_for(nib_l), nb)) return false; nb = 0; } continue; } if (nb > 0) { // launch the residents collected so far before the staged one if (!arm_launch(la_for(nib_l), nb)) return false; nb = 0; } pt.mark(kPfWaitCopy, m.cs); cudaStreamWaitEvent(m.cs, m.copied[stage_of[j]], 0); gp[0] = (unsigned long long) m.stage_dev[stage_of[j]]; gs[0] = (int32_t) m.off[(size_t) e]; gs[1] = (int32_t) (m.off[(size_t) e] + m.cnt[(size_t) e]); if (!arm_launch(la_for(false), 1)) return false; cudaEventRecord(m.used[stage_of[j]], m.cs); // the slot frees once this launch is done m.used_of[stage_of[j]] = stage_of[j]; } if (nb > 0) { if (!arm_launch(la_for(nib_l), nb)) return false; } } if (!arm_flush()) return false; // STRATA_LATENT_MMQ: the layer's pending group // U: each down latent hits U[labels[expert]]; the H-wide rows are this layer's Dm { // STRATA_LAT_MOE_DUMP (debug): the A stage's product yl, layer 0 static const char* lat_moe_dump3 = std::getenv("STRATA_LAT_MOE_DUMP"); if (lat_moe_dump3 && l == 0) { std::vector hb((size_t) T * K * (size_t) ELL); if (cudaMemcpyAsync(hb.data(), m.yl, hb.size() * 4, cudaMemcpyDeviceToHost, m.cs) != cudaSuccess || cudaStreamSynchronize(m.cs) != cudaSuccess) { err = "lat moe dump: the yl copy failed"; return false; } char p[512]; std::snprintf(p, sizeof p, "%s/a-yl.bin", lat_moe_dump3); std::FILE* fp = std::fopen(p, "wb"); if (fp == nullptr) { err = std::string("lat moe dump: cannot write ") + p; return false; } std::fwrite(hb.data(), 4, hb.size(), fp); std::fclose(fp); } } pt.mark(kPfGemmD, m.cs); strata::kernels::quantize_q8_1_rows(m.yl, (int64_t) (T * K), (int) ELL, m.ylq, m.cs); if (lat_du) { // the entries bucketed by their expert's label: the fused U kernel's tiles are one // label's rows. The A stage's tables and H inside lat_scratch are dead by now // (stream-ordered), so the bucket list lives there. // the tables ON THE DEVICE: a mid-layer pinned h2d waits behind the blob prefetch's // DMA queue (20-30 ms a layer once stream_all's prefetch runs deep); these tiny // launches cost ~50 us. goff at +0, cursors at +48 (9 ints), rows at +128 of the // scratch (the A stage's tables and H are dead here, stream-ordered). fused::latent_dense_u_tables(m.ids_p, m.lat_labels_dev + (size_t) l * m.lat_E, (int64_t) (T * K), (int) G, (int32_t*) m.lat_scratch, (int32_t*) m.lat_scratch + 12, (int32_t*) m.lat_scratch + 32, m.cs); // the grid bound without the counts: every split fits in ceil(n / 64) + G tiles const int64_t unw = (((int64_t) (T * K) + 63) / 64 + G) * (N >> 8); fused::latent_dense_u(m.lat_u_dev + (size_t) (l - m.lat_first_layer) * m.lat_d.u_bytes(), m.ylq, (const int32_t*) m.lat_scratch + 32, (const int32_t*) m.lat_scratch, (int) G, unw, (int64_t) (T * K), (int) ELL, (int) N, m.Dm, m.cs, m.lat_fmt); } else { strata::kernels::latent_u_grouped(m.lat_u_dev + (size_t) (l - m.lat_first_layer) * m.lat_d.u_bytes(), m.ylq, m.ids_p, m.lat_labels_dev + (size_t) l * m.lat_E, m.lat_E, (int64_t) (T * K), (int) ELL, (int) N, m.Dm, m.cs, m.lat_fmt); } } else // pf_wmma: this layer's groups on the FP16 tensor-core tiles (single GPU, native pack, Q2_0 / IQ4_NL down) { const bool wmma_l = use_mmq && pf_wmma() && !fused_l && on_peer.empty() && lay.native && (mmq_dt == 42 || mmq_dt == 20) && !lay.fmt[(size_t) l].tail_off && strata::kernels::iq_row_bytes(mmq_gt, N) > 0; if (use_mmq && mmq_plan().wmma_only[(size_t) l] && !wmma_l) { err = "prefill: an IQ1_M layer in the MMQ grouping needs the WMMA tiles, which this chunk cannot take"; return false; } const size_t mmq_gub = use_mmq ? mmq::matrix_bytes(mmq_gt, kFF2, N) : 0; const size_t mmq_db = use_mmq ? mmq::matrix_bytes(mmq_dt, N, kFF) : 0; bool ws_layer = false; // STRATA_MMQ_WS: this layer's products weight-stationary (below) ws::Layer wl; pt.mark(kPfGather, cs); // P4-SMALL: a staged chunk's (below stream_all_min()) MMQ groups of a native layer the fused kernels cover on this device // run as the gathered-group entry (fused::group_bounds + experts_native on each expert where it is: its cache slot or its // staging slot), in place of MMQ's per-row quantize, gather, gate/up, SwiGLU, quantize and down. The activations go to int8 // ONCE PER TOKEN (quantize_act_native into Xq, where MMQ wrote a q8_1 copy for each of a token's K rows); the rows' tokens are // src_dev, the groups' relative bounds MMQ's own, H into H, the down rows into Dm at the group's rows (the combine is MMQ's). // The staging is the resident groups' (STAGE_GRP slots held until the group's launch). Not (in a build with MMQ) with a layer the CPU share took experts of (share_own_rule.hpp), nor with a peer, // STRATA_MMQ_DIRECT / _WS, the WMMA tiles, STRATA_PF_GROUP_SCRATCH, NVFP4 tails, Hadamard-folded or nibble layers. const bool fz_staged = !stream_all && strata::prefill::staged_entry_layer(use_mmq, fused_only, staged_fo_on()) && lay.native && !fused_l && !wmma_l && !had_gu && !had_d && share_keeps_own_kernels(rows_cpu) && on_peer.empty() && !m.pp && m.group_cap == 0 && (!lay.fmt[(size_t) l].tail_off || fused::native_supported(mmq_gt, mmq_dt)) && ws::mode() == 0 && // (an NVFP4 tail: only where the fused kernels take the pair - they apply s_gate / s_up / s_down inside, so the entry needs no row_sd) !(m.cache && mmq_plan().direct[(size_t) l]) && staged_group_gather() && staged_group_entry_on() && MMQ_GROUP <= mmq::kGatherGroupMax && MMQ_GROUP <= fused::kMaxBatch && #if STRATA_EXPERT_NIBBLE_BUILT !nib_l.on && #endif fused::native_supported(mmq_gt, mmq_dt); // OWN-I8 (STRATA_EXPERT_I8=1 on any card; unset: the default of the HIP cards gfx1030 / 1031 / 1032, i8_expert_rule.hpp; =0 off): the MMQ groups of a native layer on our own // int8 dot-product kernels (native_expert_i8, src/kernels/cuda/i8_expert.cuh), where MMQ's gather, per-row quantize, // gate/up, SwiGLU, quantize and down run. The group's experts are read where they are (the cache slot or the ring slot: // the gather's wait and slot release are kept, the copy is not made); the layer's tokens go to q8_1 ONCE per token (fr_xq, // the few-row path's rows) in place of MMQ's per-row copy; SwiGLU and the q8_1 of H are inside the gate/up launch, down // writes the rows of Dm the combine reads. Not with a layer the fused kernels take (they are the faster path on the // cards that have them), (in a build with MMQ) a layer the CPU share took experts of, a peer, STRATA_MMQ_DIRECT / _WS, the WMMA tiles, STRATA_PF_GROUP_SCRATCH, NVFP4 // tails, Hadamard-folded or nibble layers; and only for groups of 256 entries or more (native_expert_i8_prompt). static const bool i8_group_env = [] { // the group walk's own switch: the I8 launches ride on it const char* v = std::getenv("STRATA_PREFILL_GROUP_GATHER"); return v == nullptr || std::atoi(v) != 0; }(); strata::kernels::NativeExpertLayout i8_L{}; bool i8_l = false; bool i8_few = false; // the few-rows route: the layer's groups run on native_expert_grouped, the activations as quantize_q8_1_rows makes them // (a build without MMQ has no use_mmq layer: the layers the fused kernels do not take come here too, as long as the buffers below exist) if (lay.native && strata::kernels::i8_order_allows(fused_l, fz_staged) && !wmma_l && !had_gu && !had_d && share_keeps_own_kernels(rows_cpu) && on_peer.empty() && !m.pp && m.group_cap == 0 && (!m.fused_bufs || !stream_all) && !lay.fmt[(size_t) l].tail_off && ws::mode() == 0 && !(m.cache && mmq_plan().direct[(size_t) l]) && i8_group_env && !order.empty() && (!strata::kernels::native_expert_i8_floor_gated() || strata::kernels::i8_chunk_default_allows(-1, true, true, (int64_t) T, stream_all_min())) && // the P100 class rule: from the stream floor up the unset default is the FP16 path (i8_expert_rule.hpp) m.GU != nullptr && m.Dm != nullptr && m.src_dev != nullptr && m.ids_identity != nullptr && #if STRATA_EXPERT_NIBBLE_BUILT !nib_l.on && #endif true) { const auto& f = lay.fmt[(size_t) l]; i8_L = strata::kernels::native_expert_layout(f.gu_type, f.d_type, f.n_embd, f.n_ff); const int64_t i8_groups = ((int64_t) order.size() + MMQ_GROUP - 1) / MMQ_GROUP; const bool i8_same = i8_L.up_off == f.up_off && i8_L.down_off == f.down_off && i8_L.bytes == f.bytes && f.n_embd == N && f.n_ff == kFF; // The few-rows route (i8_expert_rule.hpp): with it, the size floor of the int8 kernel (256 entries a group) gives way to the crossing; a layer // under it (average rows per expert, over the experts that have rows) runs on the grouped kernels in the same groups, from it up on the int8 // kernel. A pair the grouped kernels do not have, or a group whose scratch would not fit the layer's GU rows, keeps the floor. const int i8_fr = i8_same ? strata::kernels::native_expert_i8_few_rows_limit() : 0; i8_l = i8_same && strata::kernels::native_expert_i8_prompt(i8_L, i8_fr > 0 ? strata::kernels::kI8MinGroupEntries : rows_local / i8_groups); if (i8_l && i8_fr > 0 && strata::kernels::i8_fewrows_takes(i8_fr, rows_local, (int64_t) order.size())) { int64_t max_rows = 0; // the most rows one group of the walk launches with for (size_t j = 0; j < order.size(); j += (size_t) MMQ_GROUP) { const size_t je = std::min(order.size(), j + (size_t) MMQ_GROUP) - 1; max_rows = std::max(max_rows, (int64_t) m.off[(size_t) order[je]] + m.cnt[(size_t) order[je]] - m.off[(size_t) order[j]]); } if (strata::kernels::native_expert_supported(f.gu_type, f.d_type, f.n_embd, f.n_ff) && strata::kernels::native_expert_scratch_bytes(max_rows, kFF) <= (size_t) rows_local * (size_t) kFF2 * sizeof(float)) i8_few = true; else i8_l = strata::kernels::native_expert_i8_prompt(i8_L, rows_local / i8_groups); } if (i8_count_on() && i8_groups > 0) { I8Count& c = i8_count(); c.min_avg = std::min(c.min_avg, rows_local / i8_groups); if (i8_l) c.groups_i8 += i8_groups; } } if (i8_count_on()) { // every layer that reaches here: its routed entries and where they ran I8Count& c = i8_count(); ++c.layers; c.ent += rows_local; if (i8_l) { ++c.layers_i8; c.ent_i8 += rows_local; } if (i8_few) { ++c.layers_few; c.ent_few += rows_local; } // (the few-rows route's layers are counted in the int8 ones too) } // the routed experts' input: `mixed`, or (APR) its rotated copy, which the peer / the layer // split's helper also take (one rotation for both cards; `mixed` itself feeds the shared expert) const float* xsrc = m.mixed; if (fz_staged) { fz_staged_l = true; if (!use_mmq) { // a fused-only layer (the class default, or STRATA_PF_STAGED_FO=1): said once #if !defined(STRATA_PF_OWN_ONLY) // a build without MMQ says it in its start-up line (own_only_line.hpp) static bool said_fo = false; if (!said_fo) { said_fo = true; std::fprintf(stderr, "strata prefill: a layer with no MMQ tile reads prompt pieces below %lld tokens on the fused kernels' staged group " "entry, routed experts only. STRATA_PF_STAGED_FO=0 keeps the old rule.\n", (long long) stream_all_min()); } #endif } fused::quantize_act_native(m.mixed, T, N, m.Xq, m.cs); } if (i8_l) { // the layer's T tokens as q8_1 rows, once (the grouped kernels' activations) if (static bool said = false; !said) { said = true; if (strata::kernels::native_expert_i8_by_default()) { #if !defined(STRATA_PF_OWN_ONLY) // a build without MMQ says what runs in its start-up line (own_only_line.hpp), per chunk size: this one would be wrong above the P100's stream floor std::fprintf(stderr, "strata: prompt experts on our own int8 kernels (the default of this build or graphics card; STRATA_EXPERT_I8=0 goes back to the old kernels)\n"); #endif } else std::fprintf(stderr, "strata: prompt experts on our own int8 dot-product kernels (STRATA_EXPERT_I8=1)\n"); } if (m.fr_xq == nullptr || m.fr_xq_rows < T) { if (m.fr_xq) cudaFree(m.fr_xq); m.fr_xq = nullptr; m.fr_xq_rows = 0; if (cudaMalloc(&m.fr_xq, (size_t) T * (size_t) (N / 32) * 36) != cudaSuccess) { m.fr_xq = nullptr; err = "prefill: int8 experts: no memory for the activations"; return false; } m.fr_xq_rows = T; } if (m.i8_rec == nullptr && cudaMalloc((void**) &m.i8_rec, strata::prefill::i8grp::kRecBytes) != cudaSuccess) { m.i8_rec = nullptr; err = "prefill: int8 experts: no memory for the group record"; return false; } if (i8_few) // the few-rows route: the grouped kernels read q8_1 with FP16 scales (the same 36 bytes a block) strata::kernels::quantize_q8_1_rows(m.mixed, T, N, m.fr_xq, m.cs); else strata::kernels::native_expert_i8_quantize(m.mixed, T, N, m.fr_xq, m.cs); // the kernels' own image (FP32 scales), 36 bytes per block as q8_1 } if (use_mmq || fz_staged) { // (fz_staged without use_mmq: a fused-only layer with the entry on (staged_fo_on(), staged_fo_rule.hpp); the block builds the group bounds) // step 2b: the layer's activations as q8_1 rows in expert order, straight from `mixed` (APR: // from its rotated copy `Xr`): each token quantized once and written to its K rows // (slot_dev, the inverse of src_dev). // NVFP4 w4a4 activations carry a per-row scale (Xscale): that mode keeps the gather. if (wmma_l) { // FP16 rows (pf_wmma); APR: Hadamard-folded gate/up take H, rotated in fp16 storage as the FP16 path does gather_rows16(m.mixed_h, m.src_dev, m.Xs, T * K, N, m.cs); if (had_gu) strata::kernels::hadamard_rows_f16(m.Xs, T * K, N, had_block, had_sx, m.cs); } else { if (had_gu) { strata::kernels::hadamard_rows(m.mixed, m.Xr, T, N, had_block, had_sx, m.cs); xsrc = m.Xr; } if (fz_staged) { // (P4-SMALL: Xq already holds the per-token int8 rows, above) } else if (i8_l) { // (OWN-I8: the tokens' q8_1 rows are in fr_xq, above; MMQ's per-row copy is not made) } else if (mmq::fp4_activations(mmq_gt)) mmq::quantize(xsrc, m.src_dev, m.Xq, mmq_gt, N, N, T * K, m.cs, m.Xscale); else mmq::quantize_scatter(xsrc, m.slot_dev, m.src_dev, m.Xq, mmq_gt, N, N, T, (int) K, m.cs); } // each group's rows: absolute bounds (gate/up reads the layer's rows), relative ones (down // reads the group's own quantized H) const size_t n = order.size(), ng = (n + MMQ_GROUP - 1) / MMQ_GROUP; m.bounds_host.resize(n + 1 + ng * (MMQ_GROUP + 1)); for (size_t j = 0; j < n; ++j) m.bounds_host[j] = m.off[(size_t) order[j]]; m.bounds_host[n] = (int32_t) rows_local; for (size_t g = 0; g < ng; ++g) for (size_t i = 0; i <= MMQ_GROUP; ++i) m.bounds_host[n + 1 + g * (MMQ_GROUP + 1) + i] = m.bounds_host[std::min(n, g * MMQ_GROUP + i)] - m.bounds_host[g * MMQ_GROUP]; // STRATA_PF_GROUP_SCRATCH: a group whose rows pass the scratch's cap runs as sub-ranges of // its experts, each with its own relative bounds (appended); every expert fits alone m.sub_first.assign(ng, -1); m.sub.clear(); if (m.group_cap > 0) { for (size_t g = 0; g < ng; ++g) { const size_t j0 = g * MMQ_GROUP, j1 = std::min(n, j0 + MMQ_GROUP); if (m.bounds_host[j1] - m.bounds_host[j0] <= m.group_cap) continue; m.sub_first[g] = (int32_t) m.sub.size(); for (size_t a = j0; a < j1;) { size_t b = a + 1; while (b < j1 && m.bounds_host[b + 1] - m.bounds_host[a] <= m.group_cap) ++b; const int32_t rel = (int32_t) m.bounds_host.size(); for (size_t i = a; i <= b; ++i) m.bounds_host.push_back(m.bounds_host[i] - m.bounds_host[a]); m.sub.push_back({(int32_t) a, (int32_t) b, rel}); a = b; } } if (m.bounds_host.size() > bounds_ints(m.g->n_expert)) { err = "prefill: STRATA_PF_GROUP_SCRATCH's bounds table overflowed"; return false; } } if (grp_mapped) { int32_t* bh = m.grp_host + 3 * m.grp_tk; std::memcpy(bh, m.bounds_host.data(), m.bounds_host.size() * 4); copy_i32(m.bounds_dev, m.grp_dev + 3 * m.grp_tk, (int64_t) m.bounds_host.size(), m.cs); } else { if (m.up_ring.h2d(m.bounds_dev, m.bounds_host.data(), m.bounds_host.size() * 4, m.cs) != cudaSuccess) { // #1996 err = "prefill: the group bounds' upload failed"; return false; } } // STRATA_MMQ_WS=1 (moe_ws.hpp, GP100): a layer whose routed experts are ALL resident, on this // GPU alone (no peer, CPU or streamed share), runs gate/up and down as one weight-stationary // launch each, with ONE swiglu_quant over all its rows in between, instead of a gate/up, // swiglu and down MMQ launch per 16-expert group - the same GU, H and Dm bits. The experts // are read where they lie in the expert cache (offsets from its base). Only where both // products beat the per-group MMQ at this layer's rows per expert, H can be q8_1 in one pass // (no Hadamard, NVFP4 or FP4 rows; not STRATA_DBG_NAN, which reads H in floats), and not the // fused layout (its Hq holds stream_all_min() - 1 tokens' rows). static const bool ws_dbg = std::getenv("STRATA_DBG_NAN") != nullptr; const double ws_u = n > 0 ? (double) rows_local / (double) n : 0.0; if (ws::mode() != 0 && m.xoff_dev != nullptr && m.ws_chunks != nullptr && n > 0 && !ws_dbg && lay.native && m.cache != nullptr && m.host_res != nullptr && !wmma_l && !mmq_plan().direct[(size_t) l] && lay.fmt[(size_t) l].up_off == mmq_gub / 2 && order_peer.empty() && rows_cpu == 0 && rows_local == T * K && !m.fused_bufs && m.group_cap == 0 && (!stream_all || seq_start[(size_t) l] == seq_start[(size_t) l + 1]) && !had_gu && !had_d && !lay.fmt[(size_t) l].tail_off && !mmq::fp4_activations(mmq_gt) && !mmq::fp4_activations(mmq_dt) && mmq::swiglu_quant_ok(mmq_dt) && ws::supported(mmq_gt, mmq_dt) && ws::faster(mmq_gt, ws_u) && ws::faster(mmq_dt, ws_u)) { const uint8_t* ip_base = m.cache->device_slot(0); m.xoff_host.assign(n, -1); bool all = true; // every expert resident uint64_t at = 0; // their offsets' low bits for (size_t j = 0; j < n && all; ++j) { const int32_t s = m.host_res[(size_t) l * m.g->n_expert + order[j]]; if (s < 0) { all = false; break; } m.xoff_host[j] = (int64_t) (m.cache->device_slot(s) - ip_base); at |= (uint64_t) m.xoff_host[j]; } ws_layer = all && ((uint64_t) (uintptr_t) ip_base | at | lay.fmt[(size_t) l].down_off) % ws::kAlign == 0; if (ws_layer) { if (static bool said_ws = false; !said_ws) { said_ws = true; std::fprintf(stderr, "strata prefill: a resident layer's experts weight-stationary, one " "launch per product (STRATA_MMQ_WS=1)\n"); } // the offsets after the bounds in the mapped table, the chunks after them (16-byte // aligned); or the host copies through pinned slots const size_t xo = 3 * m.grp_tk + bounds_ints(m.g->n_expert); ws::Chunk* ch = nullptr; if (grp_mapped) { int32_t* xh = m.grp_host + xo; std::memcpy(xh, m.xoff_host.data(), n * sizeof(int64_t)); copy_i32((int32_t*) m.xoff_dev, m.grp_dev + xo, (int64_t) (2 * n), m.cs); ch = (ws::Chunk*) (m.grp_host + (xo + 2 * m.g->n_expert + 3) / 4 * 4); } else { m.ws_chunks_host.resize(ws::max_chunks(T * K, m.g->n_expert)); ch = m.ws_chunks_host.data(); } wl.nchunks = (int64_t) ws::chunks(order.data(), n, m.cnt.data(), m.bounds_host.data(), ch); if (grp_mapped) { copy_i32((int32_t*) m.ws_chunks, m.grp_dev + ((int32_t*) ch - m.grp_host), 4 * wl.nchunks, m.cs); } else if (m.up_ring.h2d(m.xoff_dev, m.xoff_host.data(), n * sizeof(int64_t), m.cs) != cudaSuccess || m.up_ring.h2d(m.ws_chunks, ch, (size_t) wl.nchunks * sizeof(ws::Chunk), m.cs) != cudaSuccess) { err = "prefill: the weight-stationary tables' upload failed"; return false; } wl.gu_type = mmq_gt; wl.d_type = mmq_dt; wl.w = ip_base; wl.xoff = m.xoff_dev; wl.down_off = (int64_t) lay.fmt[(size_t) l].down_off; wl.chunks = m.ws_chunks; wl.rows = T * K; wl.n = (int) n; wl.xq = m.Xq; wl.gu = m.GU; wl.hq = m.Hq; wl.dm = m.Dm; } } } else if (i8_l) { // (the int8 kernels read the tokens' q8_1 rows through src_dev: no per-row copy at all) } else { gather_rows16(m.mixed_h, m.src_dev, m.Xs, T * K, N, m.cs); // APR: Hadamard-folded gate/up take H (s * x); Xs is this layer's private gathered copy // (rotated in fp16 storage, fp32 arithmetic: see the down projection's note below) if (had_gu) strata::kernels::hadamard_rows_f16(m.Xs, T * K, N, had_block, had_sx, m.cs); } // multi-GPU: the peer's share, enqueued before the primary's own experts so both cards work at once peer_now = use_mmq && !order_peer.empty(); if (peer_now) { PeerPrefill& P = *m.pp; // the host route's copies beside the compute stream: the primary's own experts start at once // (mixed_h and w are rewritten only after this layer's combine, which waits for the peer) cudaStream_t xs = m.cs; if (P.x_stream != nullptr) { cudaEventRecord(P.ev_x, m.cs); cudaStreamWaitEvent(P.x_stream, P.ev_x, 0); xs = P.x_stream; } if (!P.p2p && P.f16) cudaMemcpyAsync(P.host_x16, m.mixed_h, (size_t) T * N * 2, cudaMemcpyDeviceToHost, xs); else if (!P.p2p) cudaMemcpyAsync(P.host_x, m.mixed, (size_t) T * N * 4, cudaMemcpyDeviceToHost, xs); if (P.sums) { // the routing weights, and each peer row's routed pair (t * K + k) cudaMemcpyAsync(P.host_w, m.w, (size_t) T * K * 4, cudaMemcpyDeviceToHost, xs); for (int64_t i = 0; i < T * K; ++i) if (const int32_t r = slot_h[(size_t) i]; r >= peer_at) P.host_pair[r - peer_at] = (int32_t) i; } cudaEventRecord(P.ev_in, xs); int prevd = 0; cudaGetDevice(&prevd); cudaSetDevice(P.dev); const cudaStream_t ps = P.s; cudaStreamWaitEvent(ps, P.ev_in, 0); if (P.out_pending) { cudaStreamWaitEvent(ps, P.ev_done, 0); P.out_pending = false; } pe.mark(kPeMoeIn, ps); if (P.p2p) cudaMemcpyPeerAsync(P.mixed, P.dev, m.mixed, prevd, (size_t) T * N * 4, ps); else if (P.f16) f16_to_f32_wide(P.mixed, P.host_x16, T * N, ps); else copy_f32_wide(P.mixed, P.host_x, T * N, ps); P.back_at = peer_at; P.back_rows = rows_peer; if (P.p2p) { // multi-GPU: the mapped table when on; src_host (a pageable vector) goes up from a pinned slot (#1996) if ((grp_mapped ? cudaMemcpyAsync(P.src, src_h + peer_at, (size_t) rows_peer * 4, cudaMemcpyHostToDevice, ps) : P.up_ring.h2d(P.src, src_h + peer_at, (size_t) rows_peer * 4, ps)) != cudaSuccess) { cudaSetDevice(prevd); err = "prefill: the peer's routing table upload failed"; return false; } } else { std::memcpy(P.host_src, src_h + peer_at, (size_t) rows_peer * 4); copy_i32(P.src, P.host_src, rows_peer, ps); } const size_t n = order_peer.size(); if (P.sums) { copy_f32_wide(P.wk, P.host_w, T * K, ps); copy_i32(P.pair, P.host_pair, rows_peer, ps); cudaMemsetAsync(P.sum, 0, (size_t) T * N * 4, ps); } if (P.compact) { // groups of up to MMQ_GROUP experts and at most G rows, each computed from its own rows P.groups.clear(); { size_t g0 = 0; int64_t gr = 0; for (size_t j = 0; j < n; ++j) { const int64_t c = m.cnt[(size_t) order_peer[j]]; if (j > g0 && (j - g0 == (size_t) MMQ_GROUP || gr + c > P.G)) { P.groups.push_back({g0, j}); g0 = j; gr = 0; } gr += c; } if (n > g0) P.groups.push_back({g0, n}); } const size_t ng = P.groups.size(); P.bounds_host.resize(n + 1 + ng * (MMQ_GROUP + 1)); for (size_t j = 0; j < n; ++j) P.bounds_host[j] = m.off[(size_t) order_peer[j]] - (int32_t) peer_at; P.bounds_host[n] = (int32_t) rows_peer; for (size_t g2 = 0; g2 < ng; ++g2) for (size_t i = 0; i <= MMQ_GROUP; ++i) P.bounds_host[n + 1 + g2 * (MMQ_GROUP + 1) + i] = P.bounds_host[std::min(P.groups[g2].second, P.groups[g2].first + i)] - P.bounds_host[P.groups[g2].first]; if (P.p2p || P.bounds_host.size() > PeerPrefill::kHostBounds) { if (P.up_ring.h2d(P.bounds, P.bounds_host.data(), P.bounds_host.size() * 4, ps) != cudaSuccess) { // #1996 cudaSetDevice(prevd); err = "prefill: the peer's group bounds upload failed"; return false; } } else { std::memcpy(P.host_bounds, P.bounds_host.data(), P.bounds_host.size() * 4); copy_i32(P.bounds, P.host_bounds, (int64_t) P.bounds_host.size(), ps); } if (P.sums && P.gather) { // each group's tokens, their row lists in the experts' order P.adds_at.resize(ng); P.ntok.resize(ng); size_t at = 0; for (size_t g2 = 0; g2 < ng; ++g2) { const int32_t r0 = P.bounds_host[P.groups[g2].first], r1 = P.bounds_host[P.groups[g2].second]; P.touched.clear(); for (int32_t r = r0; r < r1; ++r) { const int32_t t = P.host_pair[r] / (int32_t) K; if (P.tcur[(size_t) t]++ == 0) P.touched.push_back(t); } const size_t nt = P.touched.size(); int32_t* a = P.host_adds + at; // [tokens nt | starts nt + 1 | rows] a[nt] = 0; for (size_t i = 0; i < nt; ++i) { const int32_t t = P.touched[i]; a[i] = t; a[nt + 1 + i] = a[nt + i] + P.tcur[(size_t) t]; P.tcur[(size_t) t] = a[nt + i]; // the fill cursor } int32_t* list = a + 2 * nt + 1; for (int32_t r = r0; r < r1; ++r) list[P.tcur[(size_t) (P.host_pair[r] / (int32_t) K)]++] = r; for (int32_t t : P.touched) P.tcur[(size_t) t] = 0; P.adds_at[g2] = at; P.ntok[g2] = (int32_t) nt; at += 2 * nt + 1 + (size_t) (r1 - r0); } if (at > P.adds_cap) { err = "prefill: the peer's group tables overflow"; cudaSetDevice(prevd); return false; } copy_i32(P.adds, P.host_adds, (int64_t) at, ps); } pe.mark(kPeMoeGemm, ps); const auto& f = lay.fmt[(size_t) l]; for (size_t g2 = 0; g2 < ng; ++g2) { const size_t j0 = P.groups[g2].first, j1 = P.groups[g2].second; const int ngx = (int) (j1 - j0); int64_t maxr = 0; for (size_t j = j0; j < j1; ++j) { const int32_t e = order_peer[j]; maxr = std::max(maxr, m.cnt[(size_t) e]); const uint8_t* bd = nullptr; int psl = -1; if (on_peer[(size_t) e] == 2) { // from the peer's ring p_release_to(l, e, ps); if (P.pk >= P.pseq_start[(size_t) l + 1] || P.pseq[P.pk].e != e) { err = "prefill: a peer-streamed expert is not next in the peer's ring"; cudaSetDevice(prevd); return false; } psl = (int) (P.pk % (size_t) P.RP); cudaStreamWaitEvent(ps, P.pcopied[(size_t) psl], 0); bd = P.pstage[(size_t) psl]; ++P.ps_experts; } else { bd = P.peer->slot_ptr(l, e); } const size_t q = j - j0; if (lay.native) mmq::gather_native(bd, bd + f.up_off, mmq_gub / 2, bd + f.down_off, mmq_db, P.grp_gu + q * mmq_gub, P.grp_d + q * mmq_db, ps); else mmq::gather_strata_q2(bd, P.grp_gu + q * mmq_gub, P.grp_d + q * mmq_db, ps); if (psl >= 0) { // the slot is free once gathered cudaEventRecord(P.pused[(size_t) psl], ps); ++P.pk; p_issue_until(P.pk + (size_t) P.RP); } } const int64_t r0 = P.bounds_host[j0], nr = P.bounds_host[j1] - r0; if (nr <= 0) continue; const int32_t* rel = P.bounds + n + 1 + g2 * (MMQ_GROUP + 1); cudaMemsetAsync(P.grp_gu + (size_t) ngx * mmq_gub, 0, MMQ_TAIL, ps); cudaMemsetAsync(P.grp_d + (size_t) ngx * mmq_db, 0, MMQ_TAIL, ps); mmq::quantize(P.mixed, P.src + r0, P.Xq_g, mmq_gt, N, N, nr, ps); mmq::Product gu; gu.w = P.grp_gu; gu.type = mmq_gt; gu.w_rows = kFF2; gu.w_cols = N; gu.expert_bytes = mmq_gub; gu.n = ngx; gu.xq = P.Xq_g; gu.bounds = rel; gu.ids = P.ident; gu.total_rows = nr; gu.max_rows = maxr; gu.dst = P.GU_g; gu.ld_dst = kFF2; P.run_ctx->run(gu, ps); mmq::swiglu(P.GU_g, P.H_g, nr, kFF, !lay.native, ps); if (had_d) // APR: folded down weights take H (s * h), on this card as on the primary strata::kernels::hadamard_rows(P.H_g, P.H_g, nr, kFF, had_block, P.had_sh[(size_t) l], ps); mmq::quantize(P.H_g, nullptr, P.Hq_g, mmq_dt, kFF, kFF, nr, ps); const int b = (int) (g2 & 1); if (P.dm_live[b]) cudaStreamWaitEvent(ps, P.ev_dm[b], 0); // its last rows have left mmq::Product dn; dn.w = P.grp_d; dn.type = mmq_dt; dn.w_rows = N; dn.w_cols = kFF; dn.expert_bytes = mmq_db; dn.n = ngx; dn.xq = P.Hq_g; dn.bounds = rel; dn.ids = P.ident; dn.total_rows = nr; dn.max_rows = maxr; dn.dst = P.Dm_b[b]; dn.ld_dst = N; P.run_ctx->run(dn, ps); if (P.sums && P.gather) { // into the per-token sums, one launch for the group const int32_t* a = P.adds + P.adds_at[g2]; const int64_t nt = P.ntok[g2]; peer_gather_add(P.sum, P.Dm_b[b], r0, P.wk, P.pair, a, a + nt, a + 2 * nt + 1, nt, ps); continue; } if (P.sums) { // into the per-token sums on this stream: Dm_b[b] is free after it for (size_t j = j0; j < j1; ++j) peer_scatter_add(P.sum, P.Dm_b[b] + (size_t) (P.bounds_host[j] - r0) * N, P.wk, P.pair + P.bounds_host[j], P.bounds_host[j + 1] - P.bounds_host[j], ps); continue; } cudaEventRecord(P.ev_grp[g2 % PeerPrefill::kGrpEv], ps); cudaStreamWaitEvent(P.s_out, P.ev_grp[g2 % PeerPrefill::kGrpEv], 0); if (P.p2p) cudaMemcpyPeerAsync(m.Dm + (size_t) (peer_at + r0) * N, prevd, P.Dm_b[b], P.dev, (size_t) nr * N * 4, P.s_out); else // the primary reads them when it combines (below) cudaMemcpyAsync(P.host_rows + (size_t) r0 * N, P.Dm_b[b], (size_t) nr * N * 4, cudaMemcpyDeviceToHost, P.s_out); cudaEventRecord(P.ev_dm[b], P.s_out); P.dm_live[b] = true; } } else { const size_t ng = (n + MMQ_GROUP - 1) / MMQ_GROUP; P.bounds_host.resize(n + 1 + ng * (MMQ_GROUP + 1)); for (size_t j = 0; j < n; ++j) P.bounds_host[j] = m.off[(size_t) order_peer[j]] - (int32_t) peer_at; P.bounds_host[n] = (int32_t) rows_peer; for (size_t g2 = 0; g2 < ng; ++g2) for (size_t i = 0; i <= MMQ_GROUP; ++i) P.bounds_host[n + 1 + g2 * (MMQ_GROUP + 1) + i] = P.bounds_host[std::min(n, g2 * MMQ_GROUP + i)] - P.bounds_host[g2 * MMQ_GROUP]; if (P.p2p || P.bounds_host.size() > PeerPrefill::kHostBounds) { if (P.up_ring.h2d(P.bounds, P.bounds_host.data(), P.bounds_host.size() * 4, ps) != cudaSuccess) { // #1996 cudaSetDevice(prevd); err = "prefill: the peer's group bounds upload failed"; return false; } } else { std::memcpy(P.host_bounds, P.bounds_host.data(), P.bounds_host.size() * 4); copy_i32(P.bounds, P.host_bounds, (int64_t) P.bounds_host.size(), ps); } mmq::quantize(P.mixed, P.src, P.Xq, mmq_gt, N, N, rows_peer, ps); pe.mark(kPeMoeGemm, ps); const auto& f = lay.fmt[(size_t) l]; for (size_t j = 0; j < n; ++j) { const int32_t e = order_peer[j]; const uint8_t* bd = P.peer->slot_ptr(l, e); const size_t q = j % MMQ_GROUP; if (lay.native) mmq::gather_native(bd, bd + f.up_off, mmq_gub / 2, bd + f.down_off, mmq_db, P.grp_gu + q * mmq_gub, P.grp_d + q * mmq_db, ps); else mmq::gather_strata_q2(bd, P.grp_gu + q * mmq_gub, P.grp_d + q * mmq_db, ps); if (q + 1 < MMQ_GROUP && j + 1 < n) continue; const size_t j0 = j - q, g2 = j0 / MMQ_GROUP; const int ngx = (int) (q + 1); const int64_t r0 = P.bounds_host[j0], nr = P.bounds_host[j + 1] - r0; int64_t maxr = 0; for (size_t i = j0; i <= j; ++i) maxr = std::max(maxr, m.cnt[(size_t) order_peer[i]]); cudaMemsetAsync(P.grp_gu + (size_t) ngx * mmq_gub, 0, MMQ_TAIL, ps); cudaMemsetAsync(P.grp_d + (size_t) ngx * mmq_db, 0, MMQ_TAIL, ps); mmq::Product gu; gu.w = P.grp_gu; gu.type = mmq_gt; gu.w_rows = kFF2; gu.w_cols = N; gu.expert_bytes = mmq_gub; gu.n = ngx; gu.xq = P.Xq; gu.bounds = P.bounds + j0; gu.ids = P.ident; gu.total_rows = rows_peer; gu.max_rows = maxr; gu.dst = P.GU; gu.ld_dst = kFF2; P.run_ctx->run(gu, ps); mmq::swiglu(P.GU + r0 * kFF2, P.H + r0 * kFF, nr, kFF, !lay.native, ps); if (had_d) // APR: as above strata::kernels::hadamard_rows(P.H + r0 * kFF, P.H + r0 * kFF, nr, kFF, had_block, P.had_sh[(size_t) l], ps); mmq::quantize(P.H + r0 * kFF, nullptr, P.Hq, mmq_dt, kFF, kFF, nr, ps); mmq::Product dn; dn.w = P.grp_d; dn.type = mmq_dt; dn.w_rows = N; dn.w_cols = kFF; dn.expert_bytes = mmq_db; dn.n = ngx; dn.xq = P.Hq; dn.bounds = P.bounds + n + 1 + g2 * (MMQ_GROUP + 1); dn.ids = P.ident; dn.total_rows = nr; dn.max_rows = maxr; dn.dst = P.Dm + r0 * N; dn.ld_dst = N; P.run_ctx->run(dn, ps); if (P.out_pipe && nr > 0) { // this group's rows go back while the next group computes cudaEventRecord(P.ev_grp[g2], ps); cudaStreamWaitEvent(P.s_out, P.ev_grp[g2], 0); cudaMemcpyPeerAsync(m.Dm + (size_t) (peer_at + r0) * N, prevd, P.Dm + (size_t) r0 * N, P.dev, (size_t) nr * N * 4, P.s_out); } } } pe.mark(kPeMoeOut, ps); if (P.sums) { // the sums go back: T x N if (P.f16) { sums_to_f16(P.sum, P.sum16, T * N, ps); cudaMemcpyAsync(P.host_sum16, P.sum16, (size_t) T * N * 2, cudaMemcpyDeviceToHost, ps); } else { cudaMemcpyAsync(P.host_sum, P.sum, (size_t) T * N * 4, cudaMemcpyDeviceToHost, ps); } cudaEventRecord(P.ev_done, ps); P.out_pending = true; } else if (P.out_pipe) { cudaEventRecord(P.ev_done, P.s_out); P.out_pending = true; } else { cudaMemcpyPeerAsync(m.Dm + (size_t) peer_at * N, prevd, P.Dm, P.dev, (size_t) rows_peer * N * 4, ps); cudaEventRecord(P.ev_done, ps); } pe.mark(kPeIdle, ps); cudaSetDevice(prevd); ++P.layers; P.experts += (int64_t) n; P.rows += rows_peer; } if (ps_on) { // multi-GPU: this layer's peer-ring entries the routing did not pick give their slots back int pd = 0; cudaGetDevice(&pd); cudaSetDevice(m.pp->dev); p_release_to(l, (int32_t) m.g->n_expert, m.pp->s); cudaSetDevice(pd); } // STRATA_PREFILL_GROUP_GATHER=0: one gather, one wait and one record per expert. static const bool group_env = [] { const char* v = std::getenv("STRATA_PREFILL_GROUP_GATHER"); return v == nullptr || std::atoi(v) != 0; }(); const bool group_gather = group_env && stream_all && lay.native && ((use_mmq && MMQ_GROUP <= mmq::kGatherGroupMax) || i8_l); // The staged walk (a short prompt) groups too, resident and staged experts alike: its ring is // then STAGE_GRP slots, two groups, and it stages no further ahead than the open group allows, // so a group's staged slots stay unread-over until its flush (one wait on the last copy, one // event releasing them all, as in the streamed walk). const bool group_resident = group_env && !stream_all && lay.native && (((use_mmq || fz_staged) && MMQ_GROUP <= mmq::kGatherGroupMax) || i8_l); const bool gg_now = group_gather || group_resident; const int stage_n = group_resident ? STAGE_GRP : STAGE; // Stage ahead: the copy stream moves blobs host -> device while the compute stream works. int stage_next = 0; std::vector stage_of(order.size(), -1); // the unpinned ones are copied to pinned buffers by the stager's threads, in this order std::vector job_of(order.size(), -1); if (!stream_all) { std::vector js; for (size_t j = 0; j < order.size(); ++j) { const int32_t e = order[j]; if (m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0) continue; if (m.src->pinned(l, e)) continue; job_of[j] = (int) js.size(); if (m.src->transient(l, e)) { // CS-T: copied by the source 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: 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()}; auto stage_one = [&](size_t j) -> bool { const int32_t e = order[j]; const bool resident = m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0; if (resident) return true; const int sl = stage_next; stage_next = (stage_next + 1) % stage_n; const auto th = Clock::now(); const bool pinned = m.src->pinned(l, e); // pinned: never transient const uint8_t* b = pinned ? m.src->blob_stable(l, e) : nullptr; if (pinned && !b) { err = "prefill: expert source has no blob"; return false; } if (pinned) { // DMA straight from the page-locked arena: the copy stream only waits for the slot if (m.stage_live[sl]) cudaStreamWaitEvent(m.copy, m.used[m.used_of[sl]], 0); cudaMemcpyAsync(m.stage_dev[sl], b, (size_t) lay.blob_bytes(l), cudaMemcpyHostToDevice, m.copy); ++stats_.experts_dma; } else { // copied to a pinned buffer by the stager (waits only if it is behind), then DMA const uint8_t* hb = m.stager->wait(job_of[j]); if (m.stage_live[sl]) cudaStreamWaitEvent(m.copy, m.used[m.used_of[sl]], 0); cudaMemcpyAsync(m.stage_dev[sl], hb, (size_t) lay.blob_bytes(l), cudaMemcpyHostToDevice, m.copy); m.stager->issued_one(job_of[j], m.copy); } cudaEventRecord(m.copied[sl], m.copy); m.stage_live[sl] = true; stage_of[j] = sl; stats_.ms_experts_host += ms_since(th); ++stats_.experts_streamed; return true; }; // In the streamed walk an MMQ group is gathered in ONE launch, after ONE wait on its last // streamed copy (the copy stream is in order), and its ring slots are released by ONE event. A // wait and a record per expert put ~10 us of GPU bubble each on the compute stream under WDDM // (226 ms of a 32K prompt on an NVFP4 pack). The same bytes into the same group slots. A ring // entry the routing skipped inside an open group first gathers what the group holds so far // (`flush`), so no more than a group's entries are ever held back from the issuer. // STRATA_PREFILL_GROUP_GATHER=0: one gather, one wait and one record per expert. mmq::GatherGroup gg; int gg_slots[MMQ_GROUP]; size_t gg_ents[MMQ_GROUP]; // the streamed walk: their entries (wait_copied) int gg_nslots = 0; // ring slots gathered by the next flush size_t cur_entry = 0; // the streamed walk: the entry compute() is given strata::prefill::i8grp::Group g8; // OWN-I8: the open group of the int8 kernels (i8_group.hpp), for a layer with i8_l // P4-SMALL (fz_staged): the open group's expert pointers and the layer's native geometry // The layer's tables are built once (fused::group_bounds over every routed expert's rows, below); the experts then // launch in batches: the open batch (fz_b, local experts [fz_j0, ..)) closes at kMaxBatch experts, at MMQ_GROUP experts // from the staging ring (their slots stay held until the launch; the ring has STAGE_GRP), or at the layer's last expert. // Resident experts take no slot, so a layer whose experts all sit in the cache launches ceil(n / kMaxBatch) pairs. fused::Batch fz_b; size_t fz_j0 = 0; fused::NativeGeom fz_geom{}; if (fz_staged) { const auto& f = lay.fmt[(size_t) l]; fz_geom = fused::NativeGeom{f.gu_type, f.d_type, f.gu_row, f.d_row, f.up_off, f.down_off}; } // STRATA_MMQ_DIRECT=1: a resident expert with guard room in its slot is not gathered - the // group's pointer tables give MMQ each expert's own address (group position q), the others are // gathered at compact positions c = 0..ns-1 of the group buffers, as before (same bytes) const bool direct = use_mmq && lay.native && m.cache && mmq_plan().direct[(size_t) l] && !wmma_l; mmq::WeightTab tgu, td; mmq::GuardTab gd; int ns = 0; // `zero`: the MMQ tail zeroed after the gathered experts (the group's last flush); a direct // group's tail is zeroed by prep_group auto flush = [&](size_t zero) { if (i8_l ? !g8.open() : gg.n <= gg.first) return; const auto& f = lay.fmt[(size_t) l]; if (gg_nslots > 0) { // the copies land in order: the last one covers the others pt.mark(kPfWaitCopy, cs); wait_copied(gg_ents[gg_nslots - 1], (size_t) gg_slots[gg_nslots - 1]); pt.mark(kPfDequant, cs); } if (i8_l) { // OWN-I8 (i8_group.hpp): the open group's experts [g8.first, g8.n) run here as one launch pair on native_expert_i8, each read // where it is (a flush in the middle of a group, for a skipped ring entry, runs the experts it holds so far as a group of // their own). Their rows are contiguous: bounds relative to the first row go up with the blob pointers; the rows' tokens are // src_dev from there, the rows' places in Dm are the identity from there; H as q8_1 lives in the group's own rows of GU // (the rows of an MMQ gate/up output, unused here: 5,120 bytes a row against the 720 + a few bytes of H that this needs) int64_t i8_nr = 0; int i8_ne = 0; pt.mark(kPfGemmGU, cs); if (!strata::prefill::i8grp::run(i8_L, g8, order.data(), m.off.data(), m.cnt.data(), [&](const void* h, size_t bytes) { return m.up_ring.h2d(m.i8_rec, h, bytes, m.cs) == cudaSuccess; }, m.i8_rec, m.ids_identity, m.src_dev, m.fr_xq, m.GU, kFF2, m.Dm, N, m.cs, &i8_nr, &i8_ne, i8_few, (size_t) rows_local * (size_t) kFF2 * sizeof(float))) { std::fprintf(stderr, "prefill: int8 experts: the group's record could not be made (%lld rows, %d experts)\n", (long long) i8_nr, i8_ne); std::exit(1); // (flush cannot return an error: as the gather's own failure path, the engine stops) } if (gg_nslots > 0) { // the group's staged blobs are read: one event releases their slots const int rel = gg_slots[gg_nslots - 1]; cudaEventRecord(m.used[rel], m.cs); for (int i = 0; i < gg_nslots; ++i) m.used_of[gg_slots[i]] = rel; } gg_nslots = 0; g8.launched(); return; } #if STRATA_EXPERT_NIBBLE_BUILT if (nib_l.on) { // the GGUF blocks rebuilt from the 4-bit slots: the bytes the copy gives if (const int rc = core::nibble::gather_gguf(nib_l, gg.blob, gg.first, gg.n, m.grp_gu, mmq_gub, m.grp_d, mmq_db, zero, m.cs); rc != 0) core::nibble::fatal(l, "the prompt path's group gather", cudaGetErrorString((cudaError_t) rc)); } else #endif // NVFP4: each expert's scales go next to its group slot in the same launch if (!mmq::gather_native_group(gg, f.up_off, mmq_gub / 2, f.down_off, mmq_db, m.grp_gu, mmq_gub, m.grp_d, mmq_db, m.cs, f.tail_off, f.tail_off ? m.grp_tail : nullptr, zero)) { for (int i = gg.first; i < gg.n; ++i) { // not 16-byte aligned: one at a time const uint8_t* b = gg.blob[i]; mmq::gather_native(b, b + f.up_off, mmq_gub / 2, b + f.down_off, mmq_db, m.grp_gu + i * mmq_gub, m.grp_d + i * mmq_db, m.cs, f.tail_off ? b + f.tail_off : nullptr, f.tail_off ? m.grp_tail + i * 4 : nullptr, i + 1 == gg.n ? zero : 0); } } if (gg_nslots > 0) { const int rel = gg_slots[gg_nslots - 1]; cudaEventRecord(m.used[rel], m.cs); for (int i = 0; i < gg_nslots; ++i) m.used_of[gg_slots[i]] = rel; } gg_nslots = 0; gg.first = gg.n; }; // one expert's products from its blob on the device; `slot` (a ring slot, or -1 for a resident // expert) is released once the blob is read; `cslot`: the expert-cache slot the blob is in (-1: // a ring slot or the peer's), which STRATA_MMQ_DIRECT reads in place // Few-row experts (fewrows.hpp): research switch STRATA_PF_FEWROWS, off unless set static const bool fr_env = fewrows_switch(std::getenv("STRATA_PF_FEWROWS"), gp100_fp16_default(std::getenv("STRATA_PREFILL_MMQ"))); bool fr_q_done = false; // the layer's rows quantized to q8_1 (once per layer) auto compute = [&](size_t j, const uint8_t* blob_dev, int slot, int32_t cslot) -> bool { const int32_t e = order[j]; #if STRATA_EXPERT_NIBBLE_BUILT if (nib_l.on && (!use_mmq || direct || wmma_l || cslot < 0 || !lay.native || mmq_gub != (size_t) core::nibble::kRows * nib_l.raw_gu_row || mmq_db != nib_l.down_bytes)) core::nibble::fatal(l, "the prompt path", !use_mmq ? "no MMQ for the layer (the FP16 path dequantizes GGUF blocks)" : direct ? "STRATA_MMQ_DIRECT reads the slots in place" : wmma_l ? "the WMMA tiles read GGUF blocks" : cslot < 0 ? "an expert not in this GPU's slots (streamed or the peer's)" : "the MMQ group buffers are not the blob's [gate | up] and down"); #endif pt.mark(kPfDequant, cs); if (fz_staged) { // P4-SMALL: the batch's experts where they are; its two products and SwiGLU at its last expert fz_b.blob[j - fz_j0] = blob_dev; if (slot >= 0) { gg_ents[gg_nslots] = cur_entry; gg_slots[gg_nslots++] = slot; } if (j + 1 - fz_j0 < (size_t) fused::kMaxBatch && gg_nslots < MMQ_GROUP && j + 1 < order.size()) return true; if (gg_nslots > 0) { // the copies land in order: the last one covers the others pt.mark(kPfWaitCopy, cs); wait_copied(gg_ents[gg_nslots - 1], (size_t) gg_slots[gg_nslots - 1]); } fz_b.e0 = (int) fz_j0; fz_b.e1 = (int) (j + 1); pt.mark(kPfGemmGU, cs); fused::want_dm_f16(false); // the layer's tables (group_bounds, before the walk) and rows: the batch's experts are local indices of them fused::experts_native(fz_b, fz_geom, (int) order.size(), T * K, m.GU, m.Xq, m.src_dev, m.H, m.Dm, m.cs); if (gg_nslots > 0) { // the batch's staged blobs are read: one event releases their slots const int rel = gg_slots[gg_nslots - 1]; cudaEventRecord(m.used[rel], m.cs); for (int i = 0; i < gg_nslots; ++i) m.used_of[gg_slots[i]] = rel; } gg_nslots = 0; fz_j0 = j + 1; return true; } if (i8_l) { // OWN-I8: the expert joins the int8 kernels' open group; the group's products run in the flush g8.add(j, blob_dev); if (slot >= 0) { gg_ents[gg_nslots] = cur_entry; gg_slots[gg_nslots++] = slot; } if (!strata::prefill::i8grp::Group::last(j, order.size())) return true; flush(0); g8.reset(); return true; } if (use_mmq) { // gather the expert into its group slot (GGUF blocks, unchanged or converted) const size_t q = j % MMQ_GROUP; // the group's last expert also zeroes the MMQ tail after its slot (see MMQ_TAIL) const bool last_of_group = q + 1 == MMQ_GROUP || j + 1 == order.size(); if (direct) { const auto& f = lay.fmt[(size_t) l]; uint8_t* gp = cslot >= 0 && ((uintptr_t) blob_dev | (uintptr_t) f.down_off) % 16 == 0 ? m.cache->slot_tail(cslot, f.bytes, mmq_plan().d_over[(size_t) l]) : nullptr; if (gp) { // read in its slot: the guard after the blob is zeroed at the group end tgu.p[q] = blob_dev; td.p[q] = blob_dev + f.down_off; gd.p[gd.n++] = gp; ++stats_.experts_direct; } else { const int c = ns++; tgu.p[q] = m.grp_gu + (size_t) c * mmq_gub; td.p[q] = m.grp_d + (size_t) c * mmq_db; if (gg_now) { gg.blob[c] = blob_dev; gg.n = c + 1; if (slot >= 0) { gg_ents[gg_nslots] = cur_entry; gg_slots[gg_nslots++] = slot; } } else { mmq::gather_native(blob_dev, blob_dev + f.up_off, mmq_gub / 2, blob_dev + f.down_off, mmq_db, m.grp_gu + (size_t) c * mmq_gub, m.grp_d + (size_t) c * mmq_db, m.cs); } } if (slot >= 0 && !gg_now) { cudaEventRecord(m.used[slot], m.cs); m.used_of[slot] = slot; } if (q + 1 < MMQ_GROUP && j + 1 < order.size()) return true; if (gg_now) { // also when the last entry was direct (a no-op without gathers) flush(0); gg = mmq::GatherGroup{}; } } else { if (gg_now) { gg.blob[q] = blob_dev; gg.n = (int) q + 1; if (slot >= 0) { gg_ents[gg_nslots] = cur_entry; gg_slots[gg_nslots++] = slot; } if (!last_of_group) return true; flush(MMQ_TAIL); gg = mmq::GatherGroup{}; } else if (lay.native) { const auto& f = lay.fmt[(size_t) l]; #if STRATA_EXPERT_NIBBLE_BUILT if (nib_l.on) { // as the group gather (flush), one expert at its group position q const uint8_t* one[mmq::kGatherGroupMax] = {}; one[q] = blob_dev; if (const int rc = core::nibble::gather_gguf(nib_l, one, (int) q, (int) q + 1, m.grp_gu, mmq_gub, m.grp_d, mmq_db, last_of_group ? MMQ_TAIL : 0, m.cs); rc != 0) core::nibble::fatal(l, "the prompt path's gather", cudaGetErrorString((cudaError_t) rc)); } else #endif // NVFP4: this expert's scales go next to its group slot in the same launch 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, m.cs, f.tail_off ? blob_dev + f.tail_off : nullptr, f.tail_off ? m.grp_tail + q * 4 : nullptr, last_of_group ? MMQ_TAIL : 0); } else { mmq::gather_strata_q2(blob_dev, m.grp_gu + q * mmq_gub, m.grp_d + q * mmq_db, m.cs); if (last_of_group) { cudaMemsetAsync(m.grp_gu + (size_t) (q + 1) * mmq_gub, 0, MMQ_TAIL, m.cs); cudaMemsetAsync(m.grp_d + (size_t) (q + 1) * mmq_db, 0, MMQ_TAIL, m.cs); } } if (slot >= 0 && !gg_now) { cudaEventRecord(m.used[slot], m.cs); m.used_of[slot] = slot; } if (q + 1 < MMQ_GROUP && j + 1 < order.size()) return true; } // the group's products: gate/up, swiglu, the group's H to q8_1, down const size_t j0 = j - q, g = j0 / MMQ_GROUP, n = order.size(); const int ngx = (int) (q + 1); if (m.group_cap > 0) { // STRATA_PF_GROUP_SCRATCH: GU / H / Hq hold this range's rows only (the same // products: gate/up writes its absolute rows at GU - ra; SwiGLU, the q8_1 copy and // down read them from 0) const MoeRange whole{(int32_t) j0, (int32_t) (j + 1), (int32_t) (n + 1 + g * (MMQ_GROUP + 1))}; const int32_t s0 = m.sub_first[g]; const size_t ns = s0 < 0 ? 1 : (size_t) [&] { size_t c = 0; while (s0 + c < m.sub.size() && (size_t) m.sub[s0 + c].a < j + 1) ++c; return c; }(); for (size_t si = 0; si < ns; ++si) { const MoeRange rg = s0 < 0 ? whole : MoeRange{m.sub[s0 + si].a, m.sub[s0 + si].b, m.sub[s0 + si].rel}; const int64_t ra = m.bounds_host[rg.a], nra = m.bounds_host[rg.b] - ra; if (nra <= 0) continue; if (nra > m.group_cap) { err = "prefill: STRATA_PF_GROUP_SCRATCH: a range of " + std::to_string(nra) + " rows passes the scratch's " + std::to_string(m.group_cap); return false; } const int nx = rg.b - rg.a; const size_t eo = (size_t) (rg.a - (int32_t) j0); // the range's first slot in the group int64_t maxr = 0; for (int32_t i = rg.a; i < rg.b; ++i) maxr = std::max(maxr, m.cnt[(size_t) order[(size_t) i]]); pt.mark(kPfGemmGU, cs); mmq::Product gu; gu.w = m.grp_gu + eo * mmq_gub; gu.type = mmq_gt; gu.w_rows = kFF2; gu.w_cols = N; gu.expert_bytes = mmq_gub; gu.n = nx; gu.xq = m.Xq; gu.bounds = m.bounds_dev + rg.a; gu.ids = m.ids_identity; gu.total_rows = T * K; gu.max_rows = maxr; gu.dst = m.GU - ra * kFF2; gu.ld_dst = kFF2; gu.y_scale = mmq::fp4_activations(mmq_gt) ? m.Xscale : nullptr; m.mmq_ctx->run(gu, m.cs); if (lay.native && lay.fmt[(size_t) l].tail_off) mmq::swiglu_scaled(m.GU, m.H, nra, kFF, m.bounds_dev + rg.a, nx, m.grp_tail + eo * 4, ra, m.cs); else mmq::swiglu(m.GU, m.H, nra, kFF, !lay.native, m.cs); if (had_d) // per row, as the whole group's path strata::kernels::hadamard_rows(m.H, m.H, nra, kFF, had_block, had_sh, m.cs); pt.mark(kPfGemmD, cs); mmq::quantize(m.H, nullptr, m.Hq, mmq_dt, kFF, kFF, nra, m.cs, m.Hscale); mmq::Product dn; dn.w = m.grp_d + eo * mmq_db; dn.type = mmq_dt; dn.w_rows = N; dn.w_cols = kFF; dn.expert_bytes = mmq_db; dn.n = nx; dn.xq = m.Hq; dn.bounds = m.bounds_dev + rg.rel; dn.ids = m.ids_identity; dn.total_rows = nra; dn.max_rows = maxr; dn.dst = m.Dm + ra * N; dn.ld_dst = N; dn.y_scale = mmq::fp4_activations(mmq_dt) ? m.Hscale : nullptr; m.mmq_ctx->run(dn, m.cs); if (lay.native && lay.fmt[(size_t) l].tail_off) mmq::down_row_scales(m.row_sd + ra, dn.bounds, nx, m.grp_tail + eo * 4, nra, m.cs); } return true; } const int64_t r0 = m.bounds_host[j0], nr = m.bounds_host[j + 1] - r0; int64_t maxr = 0; for (size_t i = j0; i <= j; ++i) maxr = std::max(maxr, m.cnt[(size_t) order[i]]); pt.mark(kPfGemmGU, cs); if (wmma_l) { // pf_wmma: gate/up, SwiGLU to FP16, down - the group in 2 tile launches const int32_t* dnb = m.bounds_dev + n + 1 + g * (MMQ_GROUP + 1); const bool ok1 = strata::kernels::gemm_iq_f16_grouped( mmq_gt, m.grp_gu, mmq_gub, kFF2, (int) N, m.Xs, (int) N, m.bounds_dev + j0, ngx, (int) maxr, m.GU, kFF2, m.cs); swiglu_split_f16(m.GU + r0 * kFF2, m.Hh + r0 * kFF, nr, kFF, !lay.native, m.cs); if (had_d) // APR: folded down weights take H (s * h), rotated in fp16 storage as the FP16 path does strata::kernels::hadamard_rows_f16(m.Hh + r0 * kFF, nr, kFF, had_block, had_sh, m.cs); pt.mark(kPfGemmD, cs); const bool ok2 = strata::kernels::gemm_iq_f16_grouped( mmq_dt, m.grp_d, mmq_db, (int) N, kFF, m.Hh + r0 * kFF, kFF, dnb, ngx, (int) maxr, m.Dm + r0 * N, (int) N, m.cs); if (!ok1 || !ok2) { err = "prefill: the WMMA expert tiles could not launch"; return false; } return true; } if (direct) { // one launch: the direct experts' guards (their down over-read) and the zeroed tail // after the last gathered expert (see MMQ_TAIL) mmq::prep_group(gd, mmq_plan().d_over[(size_t) l], ns ? m.grp_gu + (size_t) ns * mmq_gub : nullptr, ns ? m.grp_d + (size_t) ns * mmq_db : nullptr, MMQ_TAIL, m.cs); } mmq::Product gu; gu.w = m.grp_gu; gu.type = mmq_gt; gu.w_rows = kFF2; gu.w_cols = N; gu.expert_bytes = mmq_gub; gu.n = ngx; gu.xq = m.Xq; gu.bounds = m.bounds_dev + j0; gu.ids = m.ids_identity; gu.total_rows = T * K; gu.max_rows = maxr; gu.dst = m.GU; gu.ld_dst = kFF2; gu.y_scale = mmq::fp4_activations(mmq_gt) ? m.Xscale : nullptr; if (direct) gu.tab = &tgu; m.mmq_ctx->run(gu, m.cs); if (lay.native && lay.fmt[(size_t) l].tail_off) // gate/up scales applied as read mmq::swiglu_scaled(m.GU + r0 * kFF2, m.H + r0 * kFF, nr, kFF, m.bounds_dev + j0, ngx, m.grp_tail, r0, m.cs); else mmq::swiglu(m.GU + r0 * kFF2, m.H + r0 * kFF, nr, kFF, !lay.native, m.cs); if (had_d) strata::kernels::hadamard_rows(m.H + r0 * kFF, m.H + r0 * kFF, nr, kFF, had_block, had_sh, m.cs); pt.mark(kPfGemmD, cs); mmq::quantize(m.H + r0 * kFF, nullptr, m.Hq, mmq_dt, kFF, kFF, nr, m.cs, m.Hscale); mmq::Product dn; dn.w = m.grp_d; dn.type = mmq_dt; dn.w_rows = N; dn.w_cols = kFF; dn.expert_bytes = mmq_db; dn.n = ngx; dn.xq = m.Hq; dn.bounds = m.bounds_dev + n + 1 + g * (MMQ_GROUP + 1); dn.ids = m.ids_identity; dn.total_rows = nr; dn.max_rows = maxr; dn.dst = m.Dm + r0 * N; dn.ld_dst = N; dn.y_scale = mmq::fp4_activations(mmq_dt) ? m.Hscale : nullptr; if (direct) dn.tab = &td; m.mmq_ctx->run(dn, m.cs); if (lay.native && lay.fmt[(size_t) l].tail_off) // s_down: applied by the combine mmq::down_row_scales(m.row_sd + r0, dn.bounds, ngx, m.grp_tail, nr, m.cs); if (direct) { tgu = mmq::WeightTab{}; td = mmq::WeightTab{}; gd = mmq::GuardTab{}; ns = 0; } return true; } // few rows: the quantized grouped kernels on the GGUF blob, no dequantize (fewrows.hpp) if (fr_env) { const int64_t fo0 = m.off[(size_t) e], fne = m.cnt[(size_t) e]; if (fewrows_takes(fr_env, !use_mmq, lay.native, had_gu || had_d, fne)) { const auto& f = lay.fmt[(size_t) l]; if (m.fr_rec == nullptr || m.fr_xq_rows < T) { if (m.fr_xq) cudaFree(m.fr_xq); m.fr_xq = nullptr; const size_t xb = (size_t) T * (size_t) (N / 32) * 36; if (cudaMalloc(&m.fr_xq, xb) != cudaSuccess || (m.fr_rec == nullptr && cudaMalloc((void**) &m.fr_rec, (size_t) m.g->n_expert * kFewRowsRecBytes) != cudaSuccess) || (m.fr_scr == nullptr && cudaMalloc(&m.fr_scr, strata::kernels::native_expert_scratch_bytes(kFewRows, f.n_ff)) != cudaSuccess)) { err = "prefill: few-row experts: no memory"; return false; } m.fr_xq_rows = T; fr_q_done = false; } if (!fr_q_done) { // the layer's T rows as q8_1, once strata::kernels::quantize_q8_1_rows(m.mixed, T, N, m.fr_xq, m.cs); fr_q_done = true; } uint8_t* rec = m.fr_rec + j * kFewRowsRecBytes; fewrows_record(rec, blob_dev, (int) fne, (int) fo0, m.cs); const strata::kernels::NativeExpertLayout FL = strata::kernels::native_expert_layout(f.gu_type, f.d_type, f.n_embd, f.n_ff); pt.mark(kPfGemmGU, cs); strata::kernels::native_expert_grouped(FL, (const unsigned long long*) rec, (const int32_t*) (rec + 8), (const int32_t*) (rec + 16), (const int32_t*) (rec + 24), m.src_dev + fo0, 1, fne, m.fr_xq, m.fr_scr, m.Dm, m.cs, 1); if (slot >= 0) { cudaEventRecord(m.used[slot], m.cs); m.used_of[slot] = slot; } return true; } } const int q = (int) (j % DQ); if (lay.native) { // plan v0.3 P6: a native pack's layer, dequantized by llama.cpp's own formulas 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[q], m.cs, f.tail_off ? (const float*) (blob_dev + f.tail_off) : nullptr); strata::kernels::iq_dequant_f16(f.d_type, blob_dev + f.down_off, f.n_embd * f.n_ff, m.dq_d[q], m.cs, f.tail_off ? (const float*) (blob_dev + f.tail_off) + 2 : nullptr); } else { blob_dequant_f16(blob_dev, m.dq_gu[q], m.dq_d[q], m.cs); } if (slot >= 0) { cudaEventRecord(m.used[slot], m.cs); m.used_of[slot] = slot; } const int64_t o0 = m.off[(size_t) e], ne = m.cnt[(size_t) e]; pt.mark(kPfGemmGU, cs); m.gemm.f16(m.Xs + o0 * N, m.dq_gu[q], m.GU + o0 * kFF2, ne, kFF2, N); swiglu_interleaved(m.GU + o0 * kFF2, m.Hh + o0 * kFF, ne, m.cs); // APR, deliberately in place on the fp16 H: hadamard_rows_f16 reads fp16, runs the // butterflies in fp32 and rounds once on the store, so against an fp32 rotation the only // extra error is the one fp16 rounding of the SwiGLU output before it (~2^-11 relative per // value; the GEMM reads fp16 either way). An fp32 rotation would need a T*K*640 float // buffer (~200 MiB at an 8k chunk) or a fused SwiGLU + rotation kernel, for the fallback // of types and builds without MMQ (the MMQ path rotates fp32 H). native_expert_parity // bounds the fp16 rotation against the float one at relative L1 error < 2e-3. The // gate/up input (Xs above) is handled the same way. if (had_d) strata::kernels::hadamard_rows_f16(m.Hh + o0 * kFF, ne, kFF, had_block, had_sh, m.cs); pt.mark(kPfGemmD, cs); m.gemm.f16(m.Hh + o0 * kFF, m.dq_d[q], m.Dm + o0 * N, ne, N, kFF); return true; }; if (ws_layer) { // STRATA_MMQ_WS: the layer's gate/up in one launch, H of all its rows in one pass, its down // in one launch (no expert of the layer is staged or streamed: nothing to wait for or give // back beyond what the walk would) stats_.experts_resident += (int64_t) order.size(); pt.mark(kPfGemmGU, cs); if (!ws::gate_up(wl, m.cs)) { // the layer was taken because ws::faster said so err = "prefill: the weight-stationary gate/up declined a layer it was picked for"; return false; } mmq::swiglu_quant(m.GU, m.Hq, T * K, !lay.native, m.cs); pt.mark(kPfGemmD, cs); ws::down(wl, m.cs); if (stream_all && group_gather) give_back(consumed); } else if (!stream_all) { size_t staged = 0; size_t pending = 0; // P4-SMALL: the layer's tables from the routed experts' row bounds (absolute, uploaded above), once; every // batch of experts below reads its own experts' tiles out of them (the streamed path's fused::group, from bounds) if (fz_staged && !order.empty()) fused::group_bounds(m.bounds_dev, (int) order.size(), m.GU, m.cs); const bool stream_ahead = stream_ahead_enabled(); const size_t lookahead = group_resident ? (size_t) (STAGE_GRP - MMQ_GROUP - 1) : STAGE - 1; for (size_t j = 0; j < order.size(); ++j) { // Resident experts occupy no staging slot. Keep STAGE actual transfers ahead, // rather than STAGE positions in the mixed resident/streamed order. while (staged < order.size() && (stream_ahead ? pending < STAGE : staged <= j + lookahead)) { if (!stage_one(staged)) return false; if (stage_of[staged] >= 0) ++pending; ++staged; } const int32_t e = order[j]; if (stage_of[j] < 0) { ++stats_.experts_resident; if (!compute(j, m.cache->device_slot(m.host_res[(size_t) l * m.g->n_expert + e]), -1, m.host_res[(size_t) l * m.g->n_expert + e])) return false; } else { if (!group_resident) { // grouped: the flush waits once, on the group's last copy pt.mark(kPfWaitCopy, cs); cudaStreamWaitEvent(m.cs, m.copied[stage_of[j]], 0); } if (!compute(j, m.stage_dev[stage_of[j]], stage_of[j], -1)) return false; --pending; // compute recorded the slot's release event before any reuse } } } else { // the streamed walk: this layer's entries [k, kend) in id order; an entry the routing did not // pick only gives its slot back size_t k = seq_start[(size_t) l]; const size_t kend = seq_start[(size_t) l + 1]; auto release_to = [&](int32_t e_stop) { while (k < kend && seq[k].e < e_stop) { if (gg_nslots > 0) flush(0); // the open group's slots get their event first const int sl = (int) (k % (size_t) m.ring); cudaEventRecord(m.used[sl], m.cs); m.used_of[sl] = sl; consumed = ++k; give_back(consumed); } }; for (size_t j = 0; j < order.size(); ++j) { const int32_t e = order[j]; release_to(e); if (k < kend && seq[k].e == e) { const int sl = (int) (k % (size_t) m.ring); wait_issued(k); if (!group_gather) { pt.mark(kPfWaitCopy, cs); wait_copied(k, (size_t) sl); } cur_entry = k; if (!compute(j, m.stage_dev[sl], sl, -1)) return false; consumed = ++k; if (!group_gather || gg_nslots == 0) give_back(consumed); // its group was gathered } else { const bool r0 = m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0; const uint8_t* bp = r0 ? m.cache->device_slot(m.host_res[(size_t) l * m.g->n_expert + e]) : (m.pp && m.pp->peer ? m.pp->peer->slot_ptr(l, e) : nullptr); // over the cap: P2P if (bp == nullptr) { err = "prefill: an expert is neither resident, streamed nor on the peer"; return false; } ++stats_.experts_resident; if (!compute(j, bp, -1, r0 ? m.host_res[(size_t) l * m.g->n_expert + e] : -1)) return false; if (group_gather && gg_nslots == 0) give_back(consumed); } } release_to(m.g->n_expert); } } } pt.mark(kPfCombine, cs); if (cpu_fut.valid()) { // set_cpu_pool: the CPU's rows into Dm's tail, row_sd 1 (s_down is in them) if (cpu_share_env() < 0.0) cudaEventRecord(m.cpu_ev[1], m.cs); // after the GPU's expert work if (!cpu_fut.get()) { err = "prefill: a CPU expert has no blob"; return false; } if (cpu_share_env() < 0.0 && n_stream > n_cpu) { m.cpu_pend = true; m.pend_cpu_ms = cpu_ms; m.pend_n_cpu = n_cpu; m.pend_n_gpu = n_stream - n_cpu; } stats_.cpu_share = cpu_share_env() >= 0.0 ? cpu_share_env() : m.cpu_share_now; const int64_t r0c = cpu_at; cudaMemcpyAsync(m.Dm + (size_t) r0c * N, m.cpu_rows, (size_t) rows_cpu * N * sizeof(float), cudaMemcpyHostToDevice, m.cs); if (use_mmq && !fused_l && !fz_staged_l && lay.native && lay.fmt[(size_t) l].tail_off) cudaMemcpyAsync(m.row_sd + r0c, m.cpu_ones, (size_t) rows_cpu * sizeof(float), cudaMemcpyHostToDevice, m.cs); stats_.experts_cpu += n_cpu; } // no reading from a layer the share could not change (nothing taken) or with one-time costs if (cpu_set >= 0 && n_stream > 0 && (!cpu_arm || n_cpu > 0) && !cpu_cold) { cudaEventRecord(m.cpu_wall[2 * cpu_set + 1], m.cs); m.pend_wall = cpu_arm ? 2 : 1; m.pend_set = cpu_set; m.pend_l = (int) l; m.pend_wall_n = n_stream; } if (peer_now) { // multi-GPU: the peer's rows are in Dm (or, without P2P, in host memory) cudaStreamWaitEvent(m.cs, m.pp->ev_done, 0); if (!m.pp->p2p && !m.pp->sums) copy_f32_wide(m.Dm + (size_t) m.pp->back_at * N, m.pp->host_rows, m.pp->back_rows * N, m.cs); } if (peer_now && m.pp->sums && m.pp->f16) moe_combine_peer16(m.Dm, m.slot_dev, m.w, m.shared, m.sg, m.pp->host_sum16, m.pp->back_at, m.bo, T, m.cs); else if (peer_now && m.pp->sums) moe_combine_peer(m.Dm, m.slot_dev, m.w, m.shared, m.sg, m.pp->host_sum, m.pp->back_at, m.bo, T, m.cs); else if (dm16_used && rows_cpu == 0) moe_combine16(reinterpret_cast(m.Dm), m.slot_dev, m.w, m.shared, m.sg, m.bo, T, m.cs); else { // STRATA_LAT_MOE_DUMP (debug): Dm entering the combine, layer 0 static const char* lat_moe_dump2 = std::getenv("STRATA_LAT_MOE_DUMP"); if (lat_moe_dump2 && l == 0) { const int64_t n_dm = std::min(8, T * K); std::vector hdm((size_t) n_dm * (size_t) m.g->n_embd); if (cudaMemcpyAsync(hdm.data(), m.Dm, hdm.size() * 4, cudaMemcpyDeviceToHost, m.cs) != cudaSuccess || cudaStreamSynchronize(m.cs) != cudaSuccess) { err = "lat moe dump: the Dm copy failed"; return false; } char p[512]; std::snprintf(p, sizeof p, "%s/u-dm.bin", lat_moe_dump2); std::FILE* fp = std::fopen(p, "wb"); if (fp == nullptr) { err = std::string("lat moe dump: cannot write ") + p; return false; } std::fwrite(hdm.data(), 4, hdm.size(), fp); std::fclose(fp); } } moe_combine(m.Dm, m.slot_dev, m.w, m.shared, m.sg, m.bo, T, m.cs, use_mmq && !fused_l && !fz_staged_l && !m.latent && lay.native && lay.fmt[(size_t) l].tail_off ? m.row_sd : nullptr); // (fz_staged_l: the fused kernels applied s_down already) // debug: STRATA_DBG_NAN=1 reports the first layer of a chunk whose MoE produced non-finite values if (static const bool dbg = std::getenv("STRATA_DBG_NAN") != nullptr; dbg) { cudaStreamSynchronize(m.cs); auto bad = [&](const float* d, int64_t n) { std::vector h((size_t) n); cudaMemcpy(h.data(), d, (size_t) n * 4, cudaMemcpyDeviceToHost); int64_t c = 0; for (float v : h) c += !std::isfinite(v); return c; }; // (fused: GU and H hold the grouping tables and int8 rows, not floats) // (the CPU's rows, the last rows_cpu of GU and H, are never written by the GPU: not checked) const int64_t gpu_rows = T * K - rows_cpu; const int64_t bgu = fused_l || fz_staged_l || m.group_cap > 0 ? 0 : bad(m.GU, gpu_rows * kFF2), bdm = bad(m.Dm, T * K * N), bbo = bad(m.bo, T * N), bsh = bad(m.shared, T * N), bsg = bad(m.sg, T), bw = bad(m.w, T * K); const int64_t bh = m.H && !fused_l && !fz_staged_l && m.group_cap == 0 ? bad(m.H, gpu_rows * kFF) : -1; // (group scratch: the last range's rows) static int64_t reported = -1; if ((bgu || bdm || bbo || bsh || bsg || bw || bh > 0) && reported != stats_.chunks) { reported = stats_.chunks; std::fprintf(stderr, "strata dbg: layer %lld (mmq %d, types %d/%d, %zu experts): non-finite GU %lld " "H %lld Dm %lld bo %lld of T %lld\n", (long long) l, (int) use_mmq, mmq_gt, mmq_dt, n_order, (long long) bgu, (long long) bh, (long long) bdm, (long long) bbo, (long long) T); std::fprintf(stderr, "strata dbg: layer %lld: non-finite shared %lld sg %lld w %lld\n", (long long) l, (long long) bsh, (long long) bsg, (long long) bw); } } } pt.mark(kPfHcWrite, cs); // the write-back (and the next half's norm) on its own line; it used to be charged to the phase before // ---- the hyper-connection write of this half; F-2: fused with the next half's norm when nothing else // touches R in between (not the stage's last half, not before the PLE block of layer 1, not under a // control vector) const int64_t nl = half == 0 ? l : l + 1; const bool steered = half == 1 && strata::kernels::cvec().covers(l); const float *cv_dir = nullptr, *cv_s = nullptr; const int* cv_on = nullptr; const bool cv_fused = steered && cvec_fuse() && !gr_unfused() && nl < LE && !(nl == 1 && ple_on) && strata::kernels::cvec().n_embd == N && strata::kernels::cvec().hc == HC && strata::kernels::cvec_tables(&cv_dir, &cv_s, &cv_on); const bool fuse = !gr_unfused() && nl < LE && !(half == 1 && nl == 1 && ple_on) && (!steered || cv_fused); const core::WeightRef* wnn = nullptr; if (fuse) { const core::LayerView vn(*m.wt, nl); wnn = need(vn, half == 0 ? "hc_ffn_norm.weight" : "hc_attn_norm.weight", err); if (!wnn) return false; } if (wnn && cv_fused) { // STRATA_CVEC_FUSE_CHECK=N: on the first N fused writes, also run the three-kernel path on a copy // of R and report whether R, the row scales and the BF16 image are bit-identical static int checks = [] { const char* e = std::getenv("STRATA_CVEC_FUSE_CHECK"); return e ? std::atoi(e) : 0; }(); if (checks > 0) { --checks; float *Rc = nullptr, *rc = nullptr; uint16_t* xc = nullptr; cudaMalloc(&Rc, (size_t) T * D * 4); cudaMalloc(&rc, (size_t) T * HC * 4); cudaMalloc(&xc, (size_t) T * D * 2); cudaMemcpyAsync(Rc, m.R, (size_t) T * D * 4, cudaMemcpyDeviceToDevice, m.cs); gr_write(Rc, m.bo, m.inj, HC, T, m.cs); strata::kernels::cvec_apply(Rc, l, T, D, nullptr, 0, nullptr, 0, false, m.cs); gr_norm_rs(Rc, (const float*) wnn->data, EPS, rc, xc, T, m.cs, nullptr, D, xf16); gr_write_cvec_norm_rs(m.R, m.bo, m.inj, HC, cv_dir + l * N, cv_s + l, cv_on, strata::kernels::cvec().mode, (const float*) wnn->data, EPS, m.grs, m.xn16, T, m.cs, m.xn16_lo, ldx, xf16); std::vector a((size_t) T * D), b((size_t) T * D), ra((size_t) T * HC), rb((size_t) T * HC); std::vector xa((size_t) T * D), xb((size_t) T * D); cudaMemcpyAsync(a.data(), m.R, a.size() * 4, cudaMemcpyDeviceToHost, m.cs); cudaMemcpyAsync(b.data(), Rc, b.size() * 4, cudaMemcpyDeviceToHost, m.cs); cudaMemcpyAsync(ra.data(), m.grs, ra.size() * 4, cudaMemcpyDeviceToHost, m.cs); cudaMemcpyAsync(rb.data(), rc, rb.size() * 4, cudaMemcpyDeviceToHost, m.cs); cudaMemcpyAsync(xa.data(), m.xn16, xa.size() * 2, cudaMemcpyDeviceToHost, m.cs); cudaMemcpyAsync(xb.data(), xc, xb.size() * 2, cudaMemcpyDeviceToHost, m.cs); cudaStreamSynchronize(m.cs); cudaFree(Rc); cudaFree(rc); cudaFree(xc); std::fprintf(stderr, "strata: STRATA_CVEC_FUSE_CHECK layer %lld T %lld: R %s, rs %s, xn16 %s\n", (long long) l, (long long) T, std::memcmp(a.data(), b.data(), a.size() * 4) ? "DIFFERS" : "identical", std::memcmp(ra.data(), rb.data(), ra.size() * 4) ? "DIFFERS" : "identical", std::memcmp(xa.data(), xb.data(), xa.size() * 2) ? "DIFFERS" : "identical"); } else gr_write_cvec_norm_rs(m.R, m.bo, m.inj, HC, cv_dir + l * N, cv_s + l, cv_on, strata::kernels::cvec().mode, (const float*) wnn->data, EPS, m.grs, m.xn16, T, m.cs, m.xn16_lo, ldx, xf16); normed = true; } else if (wnn) { gr_write_norm_rs(m.R, m.bo, m.inj, HC, (const float*) wnn->data, EPS, m.grs, m.xn16, T, m.cs, m.xn16_lo, ldx, xf16); normed = true; } else { gr_write(m.R, m.bo, m.inj, HC, T, m.cs); } if (steered && !cv_fused) // --control-vector-scaled strata::kernels::cvec_apply(m.R, l, T, D, nullptr, 0, nullptr, 0, false, m.cs); #if defined(STRATA_HAVE_DFLASH2) if (half == 1 && tap_rows > 0 && tap_slot[(size_t) l] >= 0) // DFlash2: this layer's completed output, mean of the streams strata::kernels::df2_tap_rows(m.R + (size_t) tap_t0 * D, tap_dev_ + (size_t) tap_slot[(size_t) l] * N, (int64_t) tap_local_.size() * N, N, (int) HC, (int) tap_rows, m.cs); #endif } } if (!ple_land()) return false; // a stage that ends before layer 1: the rows land anyway, the next gather starts if (issuer.joinable()) { issuer.join(); stats_.ms_experts_host += iss_ms; stats_.experts_streamed += iss_streamed; stats_.experts_dma += iss_dma; if (rs_on && std::getenv("STRATA_TRACE")) { std::fprintf(stderr, "strata trace: routed stream: chunk from token %lld (%lld tokens): %lld of %zu ring entries " "copied, %zu dropped (no token routes to them), depth %d\n", (long long) p0, (long long) T, (long long) iss_streamed, seq.size(), rs_dropped, rs_depth); std::fflush(stderr); } if (copy_wait_dedupe && pt.on) std::fprintf(stderr, "strata prefill copies: %lld slot waits, %lld skipped as the same release event\n", (long long) iss_waits, (long long) iss_dedup); } stats_.tokens += T; core::progress_at("reading the prompt (batched): finishing the chunk from token", p0); pt.mark(kPfStart, cs); if (next_ != nullptr) { // The current hand-off slot was used two chunks ago. float* h = m.hand[hand_buf_]; const int hp = m.hand_pieces; if (hp > 0) { // STRATA_HANDOFF_PIPE: the pieces' downloads stay queued; the host waits for the chunk's work only (the // state on_stage_chunk saves, the error check), the next stage's uploads wait on the pieces' events bool ok = cudaEventRecord(m.hand_done[hand_buf_], m.cs) == cudaSuccess; for (int i = 0; ok && i < hp; ++i) { int64_t r0 = 0, nr = 0; handoff_piece(T, hp, i, r0, nr); if (nr > 0) ok = cudaMemcpyAsync(h + (size_t) r0 * D, m.R + (size_t) r0 * D, (size_t) nr * D * 4, cudaMemcpyDeviceToHost, m.cs) == cudaSuccess; ok = ok && cudaEventRecord(m.hand_ev[hand_buf_][i], m.cs) == cudaSuccess; } if (!ok || cudaEventSynchronize(m.hand_done[hand_buf_]) != cudaSuccess) { err = std::string("prefill: the layer split's hand-off: ") + cudaGetErrorString(cudaGetLastError()); return false; } } else if (cudaMemcpyAsync(h, m.R, (size_t) T * D * 4, cudaMemcpyDeviceToHost, m.cs) != cudaSuccess || cudaStreamSynchronize(m.cs) != cudaSuccess) { err = std::string("prefill: the layer split's hand-off: ") + cudaGetErrorString(cudaGetLastError()); return false; } // Wait only for the DIRECT successor's previous chunk. That successor // may already have forwarded its older chunk to later GPUs. if (on_stage_chunk && !on_stage_chunk(p0 + T, err)) return false; if (next_run_.valid() && !next_run_.get()) { err = next_err_; return false; } next_err_.clear(); next_->hand_in_ = h; next_->hand_ev_ = hp > 0 ? static_cast(m.hand_ev[hand_buf_]) : nullptr; next_->hand_pieces_ = hp; next_->single_chunk_ = single_chunk; next_->pipe_chunked_ = pipe_chunked; if (handoff_pipe_worker()) { if (!m.hand_worker) m.hand_worker = std::make_unique(); next_run_ = m.hand_worker->submit(std::packaged_task([this, tokens, c0, T, p0] { const bool r = next_->run_impl(tokens + c0, T, p0, next_err_); (void) cudaGetLastError(); // clear what a thread's end cleared (std::async) return r; })); } else next_run_ = std::async(std::launch::async, [this, tokens, c0, T, p0] { strata::aux_cpus::pin_current_thread(); return next_->run_impl(tokens + c0, T, p0, next_err_); }); hand_buf_ ^= 1; continue; // the last stage reports the chunk (on_chunk) } if (i8_count_on()) { // STRATA_I8_COUNT=1: this chunk's entries by path (the counters restart for the next chunk) I8Count& c = i8_count(); std::fprintf(stderr, "strata i8-count: chunk of %lld tokens: %lld layers reached the group walk, %lld on native_expert_i8; routed entries %lld, on native_expert_i8 %lld (%.2f %%), " "elsewhere in the walk %lld; i8 groups %lld; smallest group average seen %lld entries; few-rows route (grouped kernels, counted in the int8 ones): %lld layers, %lld entries\n", (long long) T, (long long) c.layers, (long long) c.layers_i8, (long long) c.ent, (long long) c.ent_i8, c.ent > 0 ? 100.0 * (double) c.ent_i8 / (double) c.ent : 0.0, (long long) (c.ent - c.ent_i8), (long long) c.groups_i8, (long long) (c.min_avg == INT64_MAX ? -1 : c.min_avg), (long long) c.layers_few, (long long) c.ent_few); c = I8Count{}; } if (const char* dump = std::getenv("STRATA_PREFILL_DUMP_R")) { // debug: the final residuals, every 64th cudaStreamSynchronize(m.cs); // position (A/B quality of this path) if (std::FILE* f = std::fopen(dump, p0 == 0 ? "wb" : "ab")) { // one prompt per file, all its chunks 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 (const char* dump = std::getenv("STRATA_PREFILL_DUMP_R_ALL")) { // S25 (draft-layer distillation data, opt-in): every position's final multi-stream residual as BF16 // (round-to-nearest-even), rows in position order, appended across chunks and requests: [n][hc*n_embd] cudaStreamSynchronize(m.cs); if (std::FILE* f = std::fopen(dump, "ab")) { constexpr int64_t kRows = 512; std::vector rows((size_t) (kRows * D)); std::vector out((size_t) (kRows * D)); for (int64_t t0 = 0; t0 < T; t0 += kRows) { const int64_t nr = std::min(kRows, T - t0); cudaMemcpy(rows.data(), m.R + t0 * D, (size_t) (nr * D) * 4, cudaMemcpyDeviceToHost); for (int64_t i = 0; i < nr * D; ++i) { uint32_t u; std::memcpy(&u, &rows[(size_t) i], 4); u += 0x7fffu + ((u >> 16) & 1u); out[(size_t) i] = (uint16_t) (u >> 16); } std::fwrite(out.data(), 2, (size_t) (nr * D), f); } std::fclose(f); } } #if defined(STRATA_HAVE_DFLASH2) if (tap_rows > 0 && on_taps) { if (cudaStreamSynchronize(m.cs) != cudaSuccess) { err = std::string("prefill: ") + cudaGetErrorString(cudaGetLastError()); return false; } if (!on_taps(tap_dev_, tap_rows, p0 + tap_t0, err)) return false; } #endif if (on_chunk || on_stage_chunk) { const auto toc = Clock::now(); if (cudaStreamSynchronize(m.cs) != cudaSuccess) { err = std::string("prefill: ") + cudaGetErrorString(cudaGetLastError()); return false; } const auto toc2 = Clock::now(); if (on_stage_chunk && !on_stage_chunk(p0 + T, err)) return false; if (on_chunk && !on_chunk(m.R, T, p0, err)) return false; host_sync_ms += std::chrono::duration(toc2 - toc).count(); host_chunk_ms += ms_since(toc2); } } // Do not drain the successor here. This is the overlap: an intermediate // stage can return while later GPUs are still processing the previous chunk. ss.ple_prev[0] = prev[0]; ss.ple_prev[1] = prev[1]; if (std::getenv("STRATA_DBG_NAN") != nullptr) { // debug: the state the prompt leaves for the token path cudaStreamSynchronize(m.cs); auto bad = [&](const float* d, int64_t n) { std::vector h((size_t) n); cudaMemcpy(h.data(), d, (size_t) n * 4, cudaMemcpyDeviceToHost); int64_t c = 0; double mx = 0; for (float v : h) { c += !std::isfinite(v); if (std::isfinite(v)) mx = std::max(mx, (double) std::fabs(v)); } std::fprintf(stderr, " %lld non-finite (max |x| %.3g)", (long long) c, mx); }; const int64_t last = (n - 1) % C; std::fprintf(stderr, "strata dbg: prompt end: last residual row"); bad(m.R + last * D, D); if (ss.ple.ready()) { std::fprintf(stderr, "; PLE history"); bad(ss.ple.hist, (int64_t) strata::kernels::NG_HIST * strata::kernels::NG_HC_DIM); } std::fprintf(stderr, "; GDN state 0"); bad(ss.gdn_state, 64 * 1024); std::fprintf(stderr, "\n"); } if (cudaStreamSynchronize(m.cs) != cudaSuccess) { err = std::string("prefill: ") + cudaGetErrorString(cudaGetLastError()); return false; } // (PR #121) an expert copy that failed on the copy stream surfaces here, not in the next request if (const cudaError_t cst = cudaStreamSynchronize(m.copy); cst != cudaSuccess) { err = std::string("prefill: expert copy stream: ") + cudaGetErrorString(cst); return false; } if (kv_prefetch) { if (const cudaError_t cst = cudaStreamSynchronize(m.kv_copy); cst != cudaSuccess) { err = std::string("prefill: KV prefetch stream: ") + cudaGetErrorString(cst); return false; } std::fprintf(stderr, "strata KV prefetch: %lld layer prefixes, single existing staging pool\n", (long long) kv_prefetches); } stats_.ms_total += ms_since(t_start); #if !defined(STRATA_GPU_METAL) if (strata::kernels::qsa_two_pass_on()) { // STRATA_SELECT_TWO_PASS: what the two-pass scorer did in this prompt namespace tp = strata::kernels::two_pass; unsigned long long c[tp::kStatCount] = {}; if (strata::kernels::qsa_two_pass_stats(c, true, m.cs) && c[tp::kStCalls] > 0) { const unsigned long long sparse = c[tp::kStQueries], kept = sparse - c[tp::kStFull]; std::fprintf(stderr, "strata prefill: two-pass QSA select: %llu calls, %llu queries (%llu blocks) in single-pass TF32, " "%llu blocks again in 3xTF32 (%.1f per query, max %llu), %llu queries flagged (%llu " "16-query tiles in full), %llu selecting every cell; candidates %.0f per query (max %llu)\n", c[tp::kStCalls], sparse, c[tp::kStBlocks], c[tp::kStBand], kept ? (double) c[tp::kStBand] / kept : 0.0, c[tp::kStBandMax], c[tp::kStFull], c[tp::kStFullTiles], c[tp::kStIdentity], kept ? (double) c[tp::kStCand] / kept : 0.0, c[tp::kStCandMax]); if (c[tp::kChkQueries] > 0) { const uint32_t rb = (uint32_t) c[tp::kChkRatioMax]; float ratio = 0.0f; std::memcpy(&ratio, &rb, 4); std::fprintf(stderr, "strata prefill: two-pass check: %llu queries, %llu with ids not the 3xTF32 scorer's (%llu ids); " "scored again: %llu of %llu blocks not the 3xTF32 score bit for bit; kept: %llu of %llu " "single-pass scores outside the bound, the largest |S1 - S3| / bound %.4g\n", c[tp::kChkQueries], c[tp::kChkIdQueries], c[tp::kChkIdEntries], c[tp::kChkRescoredDiff], c[tp::kChkRescored], c[tp::kChkKeptOut], c[tp::kChkKept], (double) ratio); } } } #endif { static const bool f16_range = std::getenv("STRATA_F16_RANGE") != nullptr; float xm = 0.0f, wy[2] = {0.0f, 0.0f}; unsigned long long xo = 0, wyo[2] = {0, 0}; if (f16_range && f16_range_read(xm, xo, true) && gemm_f16_range_read(wy, wyo, true)) std::fprintf(stderr, "strata f16 range: %lld tokens: activation images max |x| %.1f (%llu beyond 65504 or NaN), " "BF16 weights max |w| %.3f (%llu), FP16 GEMM outputs max |y| %.1f (%llu not finite)\n", (long long) n, xm, xo, wy[0], wyo[0], wy[1], wyo[1]); } if (pt.on) { pt.fold(); double total = 0.0; for (double v : pt.ms) total += v; 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: %lld tokens, GPU timeline %.0f ms, wall %.0f ms, host staging %.0f ms:%s\n", (long long) n, total, ms_since(t_start), stats_.ms_experts_host, line.c_str()); std::fprintf(stderr, "strata prefill timing: host: chunk setup (PLE rows, the expert stream plan) %.0f ms, " "waiting for each chunk %.0f ms, after each chunk (the draft layer, progress) %.0f ms, " "PLE %.0f ms\n", host_setup_ms, host_sync_ms, host_chunk_ms, stats_.ms_ple); if (pe.on) { int pd = 0; cudaGetDevice(&pd); cudaSetDevice(pe.dev); cudaStreamSynchronize(m.pp->s); pe.fold(); cudaSetDevice(pd); std::string pl; for (int i = 0; i < kPeCount; ++i) { std::snprintf(b, sizeof b, " %s %.0f", kPeNames[i], pe.ms[i]); pl += b; } std::fprintf(stderr, "strata prefill timing (peer GPU, ms):%s; MoE layers %lld, rows/layer %.0f of %lld cap, experts/layer %.0f, " "over the cap %lld\n", pl.c_str(), (long long) m.pp->layers, m.pp->layers ? (double) m.pp->rows / m.pp->layers : 0.0, (long long) m.pp->cap_rows, m.pp->layers ? (double) m.pp->experts / m.pp->layers : 0.0, (long long) m.pp->over_cap); if (m.pp->ps_frac > 0.0) std::fprintf(stderr, "strata prefill timing: peer-streamed experts %lld (%.0f per MoE layer)\n", (long long) m.pp->ps_experts, m.pp->layers ? (double) m.pp->ps_experts / m.pp->layers : 0.0); } } if (kva_run || kva.capture) { const char* mode = kva.skip == KvaConfig::Skip::Moe ? "moe-skip" : kva.mode == KvaConfig::Mode::Oracle ? "oracle" : kva.mode == KvaConfig::Mode::Linear ? "linear" : "identity"; const char* only = kva.only == KvaConfig::Only::Gdn ? ", gdn layers only" : kva.only == KvaConfig::Only::Qsa ? ", qsa layers only" : ""; std::fprintf(stderr, "strata prefill: KVA %s: split %lld, suffix %lld, approximated %lld of %lld positions " "in %lld of %lld chunks, mode %s%s%s\n", kva_run ? "on" : "off (capture)", (long long) kva_split, (long long) kva_suffix, (long long) kva_positions, (long long) n, (long long) kva_chunks, (long long) (n > 0 && m.T > 0 ? (n + m.T - 1) / m.T : 0), mode, only, kva.capture ? ", capturing the late layers' inputs" : ""); if (kva_positions > 0) { // What a later turn does with this state (serve): it continues from the live session or a checkpoint of // it and reads only the new tokens, exactly, over the cells this read left; nothing is approximated twice. // The answer quality of those turns is that of this read. STRATA_KVA=0 reads exactly. static std::atomic said{false}; if (!said.exchange(true)) std::fprintf(stderr, "strata prefill: KVA: the late layers (%lld and up) hold approximated cells for " "the first %lld positions of this prompt; a follow-up turn that continues from " "this state reads its new tokens exactly on top of them (nothing is approximated " "twice); STRATA_KVA=0 reads every prompt exactly\n", (long long) kva_split, (long long) kva_positions); } } else if (kva.on && (kva.explicit_on || (n >= kva.min_prompt && !kva_routine && kva_eng.blocked.empty()))) { // KVA-8 (observability): a run that was configured for KVA but fell back completely must say so and why, // or an operator cannot tell it apart from a run where the feature was never asked for. (KVA only runs // when STRATA_KVA=1 asked for it, so every reason is worth a line.) if (kva_why_not == kKvaShapeReason) std::fprintf(stderr, "strata prefill: KVA configured but NOT applied: this model's shape (%lld layers, %lld experts) " "is not one KVA was measured on (STRATA_KVA_ANY_MODEL=1 allows it)\n", (long long) g.n_layers, (long long) g.n_expert); else std::fprintf(stderr, "strata prefill: KVA configured but NOT applied: %s (prompt %lld positions, " "min_prompt %lld, suffix %lld, split %lld)\n", kva_why_not != nullptr ? kva_why_not : "not applicable", (long long) n, (long long) kva.min_prompt, (long long) kva_suffix, (long long) kva_split); } if (std::getenv("STRATA_STATE_HASH_GDN") != nullptr) { // debug: the GDN states as the prompt path leaves them cudaStreamSynchronize(m.cs); std::vector b((size_t) gdn_floats * 4); std::string line; char h[8]; for (int64_t i = 0; i < ss.gdn_alloc; ++i) { cudaMemcpy(b.data(), ss.gdn_state + (size_t) i * gdn_floats, b.size(), cudaMemcpyDeviceToHost); uint64_t x = 1469598103934665603ull; for (uint8_t c : b) x = (x ^ c) * 1099511628211ull; std::snprintf(h, sizeof(h), "%04llx ", (unsigned long long) (x & 0xffff)); line += h; } std::fprintf(stderr, "strata prefill: GDN_HASH %s\n", line.c_str()); } 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 { const char* lm_env_text() { // STRATA_PREFILL_LAYER_MAJOR as set (nullptr: unset); the rule for unset is strata::prefill_layer_major_default static const char* const e = std::getenv("STRATA_PREFILL_LAYER_MAJOR"); return e; } bool lm_env_explicit() { const char* e = lm_env_text(); return e != nullptr && e[0] != '\0'; } bool lm_force_env() { // STRATA_PREFILL_LM_FORCE=1 (research): the layer-major walk even for a prompt under two chunks (the identity reference of a multi-prompt read) static const bool v = [] { const char* e = std::getenv("STRATA_PREFILL_LM_FORCE"); return e != nullptr && e[0] == '1'; }(); 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; } int64_t lm_next_env() { // STRATA_PREFILL_LM_NEXT: slots for the next layer's RAM-tier experts (0: off) static const int64_t v = [] { const char* e = std::getenv("STRATA_PREFILL_LM_NEXT"); 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; } // VRAM left free while a prompt runs (the verify windows, the driver): 96 MiB when STRATA_PREFILL_LAYER_MAJOR=1 asked for the walk, 512 MiB when // it is on by default (a user who set nothing gets the wider margin) int64_t lm_margin_bytes(bool by_default) { static const int64_t explicit_v = [] { const char* e = std::getenv("STRATA_PREFILL_LM_MARGIN_MIB"); return e ? (int64_t) std::atoll(e) << 20 : (int64_t) -1; }(); return explicit_v >= 0 ? explicit_v : (int64_t) (by_default ? 512 : 96) << 20; } constexpr int64_t kLmDefaultTokens = 8192; // the super-chunk target when the walk is on by default and STRATA_PREFILL_LM_TOKENS is not set: the size measured (2x RTX A4000, 32K prompts) constexpr int64_t kLmDefaultOnTokens = 32768; // 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); } // The walk's groups of layer l on the fused int8 kernels (P4-SMALL): one launch pair per gathered group (fused::group_bounds from the group's // bounds, experts_native on the experts where they are, the rows' tokens from row_tok) in place of MMQ's quantize, gate/up, SwiGLU, // quantize and down. A native layer MMQ takes in the walk, whose formats the kernels cover on this device (native_supported: the card // rule and the formats), not a Hadamard-folded or NVFP4 one (lm_prepare declines those anyway). The walk's MMQ buffers hold what it // needs: the tables in GU, the int8 H in H, the down rows in Dm; the per-token int8 rows are the super-chunk's own (LmBufs::xa). CUDA // (the HIP walk is opt-in and not measured with it); not with STRATA_PF_GROUP_SCRATCH's one-group GU / H. bool lm_fused_layer(int64_t l, bool group_scratch) { #if defined(STRATA_USE_HIP) || defined(STRATA_HIP_GFX906) (void) l; (void) group_scratch; return false; #else const strata::kernels::cpu::ExpertLayout& lay = strata::kernels::cpu::expert_layout(); const MmqPlan& mp = mmq_plan(); // (a build without MMQ has no layer on MMQ: the layers the fused kernels cover are the plan's `fo` ones, which this walk takes the same way; a build with MMQ keeps // its `fo` layers on the FP16 path in the walk as before) if (group_scratch || !lay.native || l < 0 || l >= (int64_t) mp.layer.size() || l >= (int64_t) lay.fmt.size()) return false; if (!mp.layer[(size_t) l] && !(!mmq::built() && mp.any && mp.fo[(size_t) l])) return false; const auto& f = lay.fmt[(size_t) l]; if (f.had_gu || f.had_d || f.tail_off) return false; return fused::native_supported(f.gu_type, f.d_type); #endif } // the most A batches a latent layer cuts its rows into: one per routed expert, plus the pieces of an expert with more // rows than the tile buffers int64_t lm_lat_batches(int64_t rows, int64_t G) { return NE + rows / std::max(G, 1) + 2; } } // 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); } if (m.lm.lat_host) { cudaStreamSynchronize(m.cs); cudaFreeHost(m.lm.lat_host); } m.lm.lat_host = nullptr; m.lm.lat_xlq = m.lm.lat_ylq = nullptr; m.lm.lat_tok = m.lm.lat_slot = nullptr; m.lm.lat_sg = nullptr; m.lm.lat_tabs = nullptr; m.lm.lat_tabs_n = 0; m.lm.pre = nullptr; m.lm.pre_n = 0; m.lm.pre_layer = -1; 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.xa = 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 by default; on an AMD build the prompt stays on the chunk-major path unless // STRATA_LM_HIP=1 (opt-in, PAR-A 2026-10-30: gfx1201 is being qualified) { static const bool lm_hip = [] { const char* v = std::getenv("STRATA_LM_HIP"); return v != nullptr && v[0] == '1'; }(); if (!lm_hip) { 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: KVA's approximate late layers (a prompt that wants them takes the chunk-major path, which has it). // The DFlash2 drafter's prompt taps it carries for one stage and one prompt (DF-08: lm_run taps a layer's completed output when the next layer starts) if (!tap_layers_.empty() && (split || !on_taps || tap_cap_ <= 0)) { why = "the DFlash2 drafter's prompt taps on a layer split"; 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; } if (m.latent) { const char* mmq = std::getenv("STRATA_LATENT_MMQ"); if (mmq && mmq[0] == '1') { why = "latent layer-major MMQ is not supported"; return 0; } if (m.lat_ELL != 1792 || m.lat_G != 8 || m.lat_I != 640 || m.lat_E != NE || g.n_expert != NE || !m.cache || !m.host_res || !m.lat_d_dev || !m.lat_u_dev || !m.ids_identity || !m.yl || !m.ylq || !m.lat_scratch) { why = "latent layer-major buffers or geometry are unsupported"; return 0; } } if (!m.latent) { 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_force_ || lm_force_env()) ? 1 : (lm_tokens_env() > 0 ? m.T : 2 * m.T); // (lm_force_: run_multi - several prompts, however short) if (n < min_s) { why = "a short prompt"; return 0; } const int64_t want = lm_tokens_env() > 0 ? lm_tokens_env() : (lm_by_default_ ? kLmDefaultOnTokens : 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 ((!lm_force_ && cap % m.T != 0) || cap < min_s) { why = "the layer split's stages read different chunks"; return 0; } S = std::min(S, cap); } if (m.latent && S > INT32_MAX / std::max(m.lat_G, K)) { why = "latent super-chunk route indices exceed int32"; return 0; } const int64_t G = m.T * K; bool fz = false; // a layer's groups run on the fused kernels: the super-chunk's int8 rows (LmBufs::xa) for (int64_t l = 0; l < g.n_layers && !fz; ++l) fz = lm_fused_layer(l, m.group_cap > 0); // ylq (A's output) stays in VRAM whenever that does not cost super-chunk size; mapped RAM otherwise bool y_dev = true; 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); // xlq stays in VRAM in both modes: A re-reads it once per gate/up row block, too often for mapped RAM const uint64_t latent = m.latent ? (uint64_t) s * (2 * K * 4 + 4) + (uint64_t) s * (m.lat_G + (r_dev || y_dev ? K : 0)) * (m.lat_ELL / 32 * 36) + (uint64_t) lm_lat_batches(s * K, G) * fused::latent_group_bytes(G, (int) NE) + 256 : 0; return (uint64_t) s * per + latent + (uint64_t) lm_table_ints(s * K, G) * 4 + (m.latent ? 8192 : 4096) + (r_dev ? 0 : (uint64_t) m.T * D * 4 + 256) + (fz ? (uint64_t) fused::act_bytes(s, N) + 256 : 0); }; size_t free_b = 0, total_b = 0; cudaMemGetInfo(&free_b, &total_b); const int64_t room = (int64_t) free_b - lm_margin_bytes(lm_by_default_); 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); in VRAM mode it copies // them up once a super-chunk instead of once a layer and sub-chunk const int mode = lm_r_env(); bool r_dev = false; int64_t s_use = 0; auto fit_host = [&] { // host residual rows: ylq in VRAM unless mapped RAM allows a bigger super-chunk y_dev = true; const int64_t with_y = fit(false); y_dev = false; const int64_t without_y = fit(false); // ylq crosses PCIe twice per layer when mapped: a smaller super-chunk with ylq in VRAM is faster // (STRATA_PREFILL_LM_YLQ=map, A/B: the bigger super-chunk wins, as before) static const bool y_map = [] { const char* e = std::getenv("STRATA_PREFILL_LM_YLQ"); return e && std::string(e) == "map"; }(); y_dev = m.latent && (y_map ? with_y >= without_y : with_y > 0); return y_dev ? with_y : without_y; }; if (mode == 1) { s_use = fit(true); r_dev = true; } else if (mode == 2) { s_use = fit_host(); } 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_host(); } 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 || stage_lb_ > 0) { 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; } int64_t pre_n = 0; if (m.latent && lm_next_env() > 0) { // as many as fit beside the super-chunk's buffers const int64_t left = room - (int64_t) dev_bytes(s_use, r_dev); pre_n = std::clamp(left / std::max(MAXBLOB(), 1), 0, std::min(lm_next_env(), NE)); } const uint64_t bytes = dev_bytes(s_use, r_dev) + (uint64_t) pre_n * (uint64_t) MAXBLOB() + 256; 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; if (m.latent) { lb.lat_tok = (int32_t*) carve_b((size_t) s_use * K * 4); lb.lat_slot = (int32_t*) carve_b((size_t) s_use * K * 4); lb.lat_sg = (float*) carve_b((size_t) s_use * 4); lb.lat_tabs_n = (size_t) lm_lat_batches(s_use * K, G) * fused::latent_group_bytes(G, (int) NE); lb.lat_tabs = carve_b(lb.lat_tabs_n); lb.pre_n = pre_n; lb.pre = pre_n > 0 ? carve_b((size_t) pre_n * (size_t) MAXBLOB()) : nullptr; lb.pre_layer = -1; lb.pre_of.assign((size_t) NE, -1); for (cudaEvent_t* e : {&lb.pre_done, &lb.pre_free}) if (pre_n > 0 && *e == nullptr && cudaEventCreateWithFlags(e, cudaEventDisableTiming) != cudaSuccess) { cudaGetLastError(); *e = nullptr; lb.pre_n = 0; } const size_t xb = (size_t) s_use * m.lat_G * (m.lat_ELL / 32 * 36); const size_t yb = (size_t) s_use * K * (m.lat_ELL / 32 * 36); lb.lat_xlq = carve_b(xb); if (r_dev || y_dev) { lb.lat_ylq = carve_b(yb); } else { // ylq is written once by A and read once by U void* mapped = nullptr; if (cudaHostAlloc(&lb.lat_host, yb, cudaHostAllocMapped) != cudaSuccess || cudaHostGetDevicePointer(&mapped, lb.lat_host, 0) != cudaSuccess) { cudaGetLastError(); why = "no mapped RAM for the latent super-chunk"; lm_release(); return 0; } lb.lat_ylq = (uint8_t*) mapped; } } lb.R2 = r_dev ? nullptr : (float*) carve_b((size_t) m.T * D * 4); lb.xa = fz ? carve_b(fused::act_bytes(s_use, N)) : nullptr; 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) { Seg one; one.tokens = tokens; one.n = n; one.pos0 = pos0; return lm_run(std::vector{one}, 0, nullptr, err); } // Several prompts in ONE layer-major pass (PAR-H, 2026-10-30): the super-chunk is a list of sub-chunks, each from one // prompt ("segment") with its own sessions; a layer's attention halves run per sub-chunk against that segment's // session (GDN / QSA / PLE state), and the layer's experts are staged ONCE for every sub-chunk of the super-chunk - // the streamed experts cost is shared by the prompts. A single segment with no sessions given is exactly the usual run. bool Prefill::run_multi(const std::vector& segs, std::string& err) { err.clear(); if (segs.empty()) { err = "prefill: no prompt"; return false; } int64_t total = 0; for (const Seg& sg : segs) total += sg.n; std::string why; if (hand_in_ != nullptr || !impl_) { err = "prefill: multi-prompt read declined (chunk hand-off in use)"; return false; } if (!tap_layers_.empty()) { err = "prefill: multi-prompt read declined (the DFlash2 prompt taps are for one prompt)"; return false; } for (Prefill* q = this; q != nullptr; q = q->next_) q->lm_force_ = true; struct ForceOff { Prefill* h; ~ForceOff() { for (Prefill* q = h; q != nullptr; q = q->next_) q->lm_force_ = false; } } force_off{this}; if (lm_prepare_chain(total, why) <= 0) { err = "prefill: multi-prompt read declined: " + why; return false; } bool ok = lm_run(segs, 0, nullptr, err); std::string drain_err; if (!drain_pipeline(drain_err) && ok) { ok = false; err = drain_err; } for (Prefill* q = this; q != nullptr; q = q->next_) q->lm_mid_ = false; lm_release_chain(); return ok; } // read_short's weights of the first stage's layers: a copy of this GPU's table whose rows of those layers point into // one of two layer slots, filled from the first stage's arena (and its latent D/U tables) a layer ahead struct Prefill::ShortWeights { struct Item { core::WeightRef* dst; core::WeightRef src; uint64_t off, bytes, noff, nbytes; }; // n*: native blocks core::WeightTable shadow; std::vector> items; // per layer uint8_t* slot[2] = {}; // (set_short_slots's: not freed here) uint64_t d_off = 0, u_off = 0, d_bytes = 0, u_bytes = 0; const uint8_t *src_d = nullptr, *src_u = nullptr; // the first stage's D/U of layer 0 int src_dev = 0, dev = 0; cudaStream_t st = nullptr; cudaEvent_t ready[2] = {}, freed[2] = {}; bool freed_set[2] = {}; ~ShortWeights() { if (st) cudaStreamSynchronize(st); } // layer l's copies into slot l % 2, once the layer that used the slot before has finished there bool issue(int64_t l) { const int s = (int) (l & 1); if (freed_set[s] && cudaStreamWaitEvent(st, freed[s], 0) != cudaSuccess) return false; for (const Item& it : items[(size_t) l]) if ((it.bytes && cudaMemcpyPeerAsync(slot[s] + it.off, dev, it.src.data, src_dev, (size_t) it.bytes, st) != cudaSuccess) || (it.nbytes && cudaMemcpyPeerAsync(slot[s] + it.noff, dev, it.src.native_data, src_dev, (size_t) it.nbytes, st) != cudaSuccess)) return false; if (d_bytes && (cudaMemcpyPeerAsync(slot[s] + d_off, dev, src_d + (size_t) l * d_bytes, src_dev, (size_t) d_bytes, st) != cudaSuccess || cudaMemcpyPeerAsync(slot[s] + u_off, dev, src_u + (size_t) l * u_bytes, src_dev, (size_t) u_bytes, st) != cudaSuccess)) return false; return cudaEventRecord(ready[s], st) == cudaSuccess; } // before layer l's work on `cs`: its slot has landed, and its rows point there bool begin(int64_t l, cudaStream_t cs) { const int s = (int) (l & 1); for (const Item& it : items[(size_t) l]) { void* const q8_1 = it.dst->native_q8_1; // (this GPU's scratch, set below) *it.dst = it.src; it.dst->data = it.bytes ? slot[s] + it.off : nullptr; it.dst->native_data = it.nbytes ? slot[s] + it.noff : nullptr; it.dst->native_q8_1 = it.nbytes ? q8_1 : nullptr; } return cudaStreamWaitEvent(cs, ready[s], 0) == cudaSuccess; } // after layer l's work on `cs`: its slot takes layer l + 2 bool end(int64_t l, int64_t lo_end, cudaStream_t cs) { const int s = (int) (l & 1); if (cudaEventRecord(freed[s], cs) != cudaSuccess) return false; freed_set[s] = true; return l + 2 >= lo_end || issue(l + 2); } const uint8_t* d(int64_t l) const { return slot[l & 1] + d_off; } const uint8_t* u(int64_t l) const { return slot[l & 1] + u_off; } }; bool Prefill::lm_run(const std::vector& segs, int depth, const std::vector* given_super, std::string& err) { err.clear(); Impl& m = *impl_; const core::OnDevice on_device(m.device); const core::ModelGeometry& g = *m.g; // the session of segment i on THIS stage: the segment's own (stage depth `depth`), else this stage's main session auto ss_of = [&](size_t i) -> core::SessionState* { const Seg& sg = segs[i]; return (size_t) depth < sg.stage_ss.size() && sg.stage_ss[(size_t) depth] != nullptr ? sg.stage_ss[(size_t) depth] : m.ss; }; core::SessionState* ssp = ss_of(0); const bool multi = segs.size() > 1; int64_t n = 0; // all the prompts' tokens for (const Seg& sg : segs) n += sg.n; const int64_t pos0 = segs[0].pos0; // (only the progress line's) const auto t_start = Clock::now(); const cudaStream_t cs = (cudaStream_t) m.cs; Impl::LmBufs& lb = m.lm; const int64_t 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); // read_short: layers [0, short_end_) run against the scratch session `short_lo_` (it also holds the PLE run) core::SessionState* const pss = short_lo_ != nullptr ? short_lo_ : m.ss; std::vector> prevs(segs.size()); for (size_t i = 0; i < segs.size(); ++i) prevs[i] = {ss_of(i)->ple_prev[0], ss_of(i)->ple_prev[1]}; if (short_lo_ != nullptr) prevs[0] = {short_lo_->ple_prev[0], short_lo_->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, (int64_t) mmq_ids_rows((size_t) m.T), 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 = pss->ple.ready(); const bool ple_batch_env = ple_batch_enabled(); const bool ple_batch = ple_on && ple_batch_env && strata::kernels::ple_native_postops_enabled() && (pss->ple.w.key_bf16 != nullptr || pss->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; // ---- the plan. A segment is cut into pieces of at most S tokens (the usual super-chunks), each piece into // sub-chunks of m.T tokens from the piece's start; pieces of several segments are packed into one super-chunk while // their rows fit S. One segment: exactly the usual super-chunks. const int64_t S = m.lm.S; // the sub-chunk size of a multi-prompt read: what every stage of the chain can take (the stages' chunks may differ) int64_t tsub = m.T; if (lm_force_) for (const Prefill* q = this; q != nullptr; q = q->next_) tsub = std::min(tsub, q->impl_->T); std::vector> supers_own; const std::vector>* supers_p = &supers_own; std::vector> one_given; if (given_super != nullptr) { one_given.push_back(*given_super); supers_p = &one_given; } else { std::vector cur; int64_t cur_rows = 0; for (size_t si = 0; si < segs.size(); ++si) for (int64_t p0s = 0; p0s < segs[si].n; p0s += S) { const int64_t piece = std::min(S, segs[si].n - p0s); if (!cur.empty() && cur_rows + piece > S) { supers_own.push_back(std::move(cur)); cur.clear(); cur_rows = 0; } for (int64_t c0s = 0; c0s < piece; c0s += tsub) { LmSub sb; sb.seg = (int) si; sb.c0 = p0s + c0s; sb.T = std::min(tsub, piece - c0s); sb.t0 = cur_rows + c0s; cur.push_back(sb); } cur_rows += piece; } if (!cur.empty()) supers_own.push_back(std::move(cur)); } const std::vector>& supers = *supers_p; std::vector> flat; // every sub-chunk in order: (super-chunk, sub-chunk) for (size_t si = 0; si < supers.size(); ++si) for (size_t c = 0; c < supers[si].size(); ++c) flat.push_back({(int) si, (int) c}); std::vector flat_base(supers.size(), 0); for (size_t si = 1; si < supers.size(); ++si) flat_base[si] = flat_base[si - 1] + (int64_t) supers[si - 1].size(); auto sub_at = [&](int64_t gi) -> const LmSub& { return supers[(size_t) flat[(size_t) gi].first][(size_t) flat[(size_t) gi].second]; }; const std::array prev_none{-1, -1}; (void) prev_none; std::vector> prev0s = prevs; auto ple_gather = [&m, pss, &segs, &ss_of, &prev0s, &sub_at](int64_t gi, int buf, std::string& e) -> bool { const LmSub& sb = sub_at(gi); const int64_t* tokens = segs[(size_t) sb.seg].tokens; core::SessionState* ssq = ss_of((size_t) sb.seg); const int64_t c0 = sb.c0, T = sb.T; const std::array& prev0 = prev0s[(size_t) sb.seg]; auto at = [&](int64_t i) { return i < 2 ? prev0[(size_t) 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, pss->ple.consts, m.ple_rows[buf].data() + t * strata::kernels::PLE_N_HEADS); pv[0] = pv[1]; pv[1] = tok; } return pss->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 gi, 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 (gi + 1 < (int64_t) flat.size()) { 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 = gi + 1, 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, ms_wait_files = 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'; }(); // STRATA_PREFILL_LM_NEXTFILES=: two stagers taking the layers in turn, so the next layer's file reads start // as soon as this layer's walk has issued its own (never competing with them), into a ring that holds a layer static const int64_t nextfiles = [] { const char* e = std::getenv("STRATA_PREFILL_LM_NEXTFILES"); return e ? (int64_t) std::atoll(e) : (int64_t) 0; }(); const bool pf_two = m.latent && pf_on && nextfiles > 0 && m.src != nullptr && m.lm_stager[0] && m.lm_stager[1]; auto st_of = [&](int64_t l) -> Stager* { return pf_two ? m.lm_stager[(l - LB) & 1].get() : m.stager.get(); }; 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) { // #4695: no contiguous mapped blob (Windows, the mapping dropped for unbuffered reads): the source copies it itself js.push_back({nullptr, (size_t) lay.blob_bytes(l), m.src, (int32_t) l, e}); continue; } js.push_back({b, (size_t) lay.blob_bytes(l)}); } st_of(l)->start(std::move(js)); return true; }; struct StagerStop { Stager* st[3]; ~StagerStop() { for (Stager* s : st) if (s) s->finish(); } // an early return in a layer's attention halves } stager_stop{{m.stager.get(), m.lm_stager[0].get(), m.lm_stager[1].get()}}; int64_t lm_streamed = 0, lm_resident = 0, lm_groups = 0, lm_supers = 0, lm_groups_f16 = 0, lm_groups_fz = 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 // STRATA_PREFILL_LM_NEXT: the thread that enqueues the next layer's prefetch copies (see the latent walk) std::atomic pre_ready{-1}, pre_bytes{0}, pre_blobs{0}; // (outlive the worker below) struct DmaWorker { std::thread t; std::mutex mu; std::condition_variable cv; std::deque> q; bool quit = false; void start(int dev) { t = std::thread([this, dev] { cudaSetDevice(dev); for (;;) { std::function f; { std::unique_lock lk(mu); cv.wait(lk, [&] { return quit || !q.empty(); }); if (q.empty()) return; f = std::move(q.front()); q.pop_front(); } f(); } }); } void push(std::function f) { { std::lock_guard lk(mu); q.push_back(std::move(f)); } cv.notify_one(); } ~DmaWorker() { { std::lock_guard lk(mu); quit = true; } cv.notify_one(); if (t.joinable()) t.join(); } } dma; int64_t pre_pending = -1; if (lb.pre_n > 0) { int dev = 0; cudaGetDevice(&dev); dma.start(dev); } const bool latent_du = m.latent && fused::latent_fused_available() && [] { const char* v = std::getenv("STRATA_LATENT_FUSED_DU"); return v == nullptr || v[0] != '0'; }(); auto latent_dense_route = [&](int64_t l, int64_t token_base, int64_t token_count) -> bool { if ((l < m.lat_first_layer && !(short_w_ && l < short_end_)) || l < LB || l >= LE || token_base < 0 || token_count <= 0 || token_count > m.T || token_base + token_count > lb.S) { err = "prefill latent layer-major: D tile outside the stage or arena"; return false; } pt.mark(kPfGather, cs); // the timeline's "gather" is the latent D projection on this path const int64_t ell = m.lat_ELL, groups = m.lat_G; uint8_t* dst = lb.lat_xlq + (size_t) token_base * groups * (ell / 32 * 36); const uint8_t* d = short_w_ && l < short_end_ ? short_w_->d(l) : m.lat_d_dev + (size_t) (l - m.lat_first_layer) * m.lat_d.d_bytes(); const float* inv = strata::kernels::lat_gu_colscale_dev((int) l, (int) ell); strata::kernels::quantize_q8_1_rows(m.mixed, token_count, (int) N, m.lat_xq, cs); if (latent_du) { if (inv) fused_q3k::latent_dense_d(d, m.lat_xq, token_count, (int) N, (int) groups, ell, dst, cs, m.lat_fmt, inv); else fused::latent_dense_d(d, m.lat_xq, token_count, (int) N, (int) groups, ell, dst, cs, m.lat_fmt); } else { strata::kernels::iq_mmvq(strata::kernels::lat_dense_fmt_to_type(m.lat_fmt), d, m.lat_xq, m.xl, (int) N, (int) (groups * ell), token_count, cs); if (inv) strata::kernels::lat_colscale_rows(m.xl, token_count * groups, (int) ell, inv, cs); strata::kernels::quantize_q8_1_rows(m.xl, token_count * groups, (int) ell, dst, cs); } return true; }; auto latent_u_reduce = [&](int64_t l, int64_t token_base, int64_t token_count) -> bool { if ((l < m.lat_first_layer && !(short_w_ && l < short_end_)) || l < LB || l >= LE || token_base < 0 || token_count <= 0 || token_count > m.T || token_base + token_count > lb.S) { err = "prefill latent layer-major: U tile outside the stage or arena"; return false; } pt.mark(kPfGemmD, cs); // U, then the ordered combine below const int64_t nent = token_count * K, ell = m.lat_ELL, groups = m.lat_G; const int32_t* ids = lb.ids + token_base * K; const int32_t* labels = m.lat_labels_dev + (size_t) l * m.lat_E; // This gather copies uint4s, not BF16 values: q8_1 blocks survive unchanged. const int64_t row_bytes = ell / 32 * 36; gather_rows16((const uint16_t*) lb.lat_ylq, lb.lat_slot + token_base * K, (uint16_t*) m.ylq, nent, row_bytes / 2, cs); const uint8_t* u = short_w_ && l < short_end_ ? short_w_->u(l) : m.lat_u_dev + (size_t) (l - m.lat_first_layer) * m.lat_d.u_bytes(); if (latent_du) { fused::latent_dense_u_tables(ids, labels, nent, (int) groups, (int32_t*) m.lat_scratch, (int32_t*) m.lat_scratch + 12, (int32_t*) m.lat_scratch + 32, cs); const int64_t work = ((nent + 63) / 64 + groups) * (N >> 8); fused::latent_dense_u(u, m.ylq, (int32_t*) m.lat_scratch + 32, (int32_t*) m.lat_scratch, (int) groups, work, nent, (int) ell, (int) N, m.Dm, cs, m.lat_fmt); } else { strata::kernels::latent_u_grouped(u, m.ylq, ids, labels, m.lat_E, nent, (int) ell, (int) N, m.Dm, cs, m.lat_fmt); } pt.mark(kPfCombine, cs); // The reference starts its k-ordered FMA sum at zero, then adds shared. moe_combine(m.Dm, m.ids_identity, lb.w + token_base * K, lb.mixed + token_base * N, lb.lat_sg + token_base, lb.acc + token_base * N, token_count, cs); return true; }; // ---- one layer's experts over the super-chunk's Sn tokens auto lm_experts = [&](int64_t l, int64_t Sn) -> bool { Stager* const stg = st_of(l); const auto th0 = Clock::now(); const int64_t rows = Sn * K; pt.mark(kPfHostGroup, cs); // the GPU idles from here while the host groups the routes 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; }; auto prefetched = [&](int32_t e) { return lb.pre_layer == l && lb.pre_of[(size_t) 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) stg->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) { // #4695: as in lm_prefetch js.push_back({nullptr, (size_t) lay.blob_bytes(l), m.src, (int32_t) l, e}); continue; } js.push_back({b, (size_t) lay.blob_bytes(l)}); } stg->start(std::move(js)); } struct StagerDone { Stager* st; ~StagerDone() { if (st) st->finish(); } } stager_done{stg}; // 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; } // P4-SMALL: a fused layer's group reads its staged experts where they landed (no gather), so their slots stay held until the group's // launch: STAGE_GRP slots, staged no further ahead than the open group allows (as the staged walk's resident groups, STAGE_GRP's note) const bool fz_l = lb.xa != nullptr && lm_fused_layer(l, m.group_cap > 0); const int stage_n = fz_l ? STAGE_GRP : STAGE; const size_t ahead = fz_l ? (size_t) (STAGE_GRP - MMQ_GROUP - 1) : (size_t) STAGE - 1; int stage_next = 0; // the latent walk keeps a batch's staged blobs until it launches, so it takes the layout's whole staging ring const int lm_slots = m.latent ? stage_slots((size_t) m.T) : STAGE; auto stage_one = [&](size_t u) -> bool { const int32_t e = uniq[u]; if (resident(e) || prefetched(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) % (m.latent ? lm_slots : stage_n); 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 { stg->pass(job_of[u]); // the prefetched jobs below it the routing skipped get no DMA const auto tw = Clock::now(); const uint8_t* hb = stg->wait(job_of[u]); ms_wait_files += ms_since(tw); cudaMemcpyAsync(m.stage_dev[sl], hb, (size_t) lay.blob_bytes(l), cudaMemcpyHostToDevice, m.copy); stg->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; }; if (m.latent) { const auto& f = lay.fmt[(size_t) l]; const int64_t ell = m.lat_ELL; const size_t row_bytes = (size_t) (ell / 32 * 36); std::vector xl_rows((size_t) rows), slots((size_t) rows, -1); std::vector fill(off.begin(), off.end() - 1); for (int64_t i = 0; i < rows; ++i) { const int32_t e = ids_h[i], p = fill[(size_t) e]++; const int32_t label = m.lat_labels[(size_t) l * m.lat_E + e]; if (label < 0 || label >= m.lat_G || p < 0 || p >= rows) { err = "prefill latent layer-major: invalid route mapping"; return false; } xl_rows[(size_t) p] = (int32_t) (i / K * m.lat_G + label); slots[(size_t) i] = p; } if (m.up_ring.h2d(lb.lat_tok, xl_rows.data(), (size_t) rows * 4, cs) != cudaSuccess || m.up_ring.h2d(lb.lat_slot, slots.data(), (size_t) rows * 4, cs) != cudaSuccess) { err = "prefill latent layer-major: route upload failed"; return false; } const auto layout = strata::kernels::native_expert_layout(f.gu_type, f.d_type, (int) ell, (int) m.lat_I); const bool use_fused = fused::latent_fused_available() && [] { const char* v = std::getenv("STRATA_LATENT_FUSED"); return v == nullptr || v[0] != '0'; }() && ((f.gu_type == 40 && f.d_type == 40) || (lat_fused_q3k_on() && f.gu_type == 11 && f.d_type == 20 && m.lat_I == 640)); fused::LatentGeom geom{}; geom.ell = ell; geom.iff = m.lat_I; geom.gu_row = layout.gu_row; geom.d_row = layout.d_row; geom.up_off = layout.up_off; geom.down_off = layout.down_off; geom.tail_off = layout.tail_off; // the layer's A batches, cut before the walk so all their tables go up in one copy: a small upload per // batch waits behind the staging copies on the copy engine, and the kernel behind it struct Cut { int32_t e0, e1; int64_t base, count; }; std::vector cuts; size_t next_cut = 0; const size_t tab_stride = fused::latent_group_bytes(G, (int) NE); // A over the entries [row_base, row_base + entry_count) of experts [e0, e1): the rows of an expert // keep their 64-row tiles from the expert's first row, and every FP32 output and scratch row is // relative to row_base, so a batch writes the same bytes per entry as one launch per expert. auto latent_a_tile = [&](int32_t e0, int32_t e1, int64_t row_base, int64_t entry_count, const fused::WideBatch& blobs) -> bool { if (entry_count <= 0 || entry_count > G || row_base < 0 || row_base + entry_count > rows || e0 < 0 || e1 > NE || e1 <= e0 || e1 - e0 > fused::kBatchRoom) { err = "prefill latent layer-major: A tile outside the arena"; return false; } pt.mark(kPfGemmGU, cs); // A: gate/up, SwiGLU and down in one launch pair if (use_fused) { if (fused::latent_scratch_bytes(entry_count, (int) NE, m.lat_I) > strata::kernels::native_expert_scratch_bytes(G, m.lat_I)) { err = "prefill latent layer-major: fused A scratch does not fit"; return false; } // this batch's tables, uploaded with the layer's others (latent_a_superchunk) if (next_cut >= cuts.size() || cuts[next_cut].e0 != e0 || cuts[next_cut].e1 != e1 || cuts[next_cut].base != row_base || cuts[next_cut].count != entry_count) { err = "prefill latent layer-major: the A walk left its planned batches"; return false; } const uint8_t* tables = lb.lat_tabs + next_cut++ * tab_stride; fused::WideBatch batch = blobs; batch.e0 = e0; batch.e1 = e1; void* h = m.lat_scratch; // H rows; the tables live in lat_tabs if (f.gu_type == 11 && f.d_type == 20 && m.lat_I == 640) fused_q3k::experts_latent_q3k(batch, geom, (int) NE, entry_count, tables, lb.lat_xlq, lb.lat_tok + row_base, h, m.yl, cs); else fused::experts_latent(batch, geom, (int) NE, entry_count, tables, lb.lat_xlq, lb.lat_tok + row_base, h, m.yl, cs); } else { if (e1 != e0 + 1) { err = "prefill latent layer-major: the native A stage takes one expert a launch"; return false; } const unsigned long long blob = (unsigned long long) blobs.blob[0]; const int32_t starts[2] = {0, (int32_t) entry_count}, one = 1; if (m.up_ring.h2d(m.lat_gp, &blob, sizeof blob, cs) != cudaSuccess || m.up_ring.h2d(m.lat_gs, starts, sizeof starts, cs) != cudaSuccess || m.up_ring.h2d(m.lat_n, &one, sizeof one, cs) != cudaSuccess) { err = "prefill latent layer-major: native A tables upload failed"; return false; } strata::kernels::native_expert_grouped(layout, m.lat_gp, m.lat_gs, m.lat_n, m.ids_identity, lb.lat_tok + row_base, 1, entry_count, lb.lat_xlq, m.lat_scratch, m.yl, cs, 0); } strata::kernels::quantize_q8_1_rows(m.yl, entry_count, (int) ell, lb.lat_ylq + (size_t) row_base * row_bytes, cs); pt.mark(kPfHostGroup, cs); // the GPU idles from here until the host issues the next batch ++lm_groups; return true; }; // the routed experts in id order, consecutive ones batched while their rows fit the tile buffers; a // batch's staged slots are released only after its launch (staged_batch.hpp sizes batch and lookahead) auto latent_a_superchunk = [&]() -> bool { const StagedBatchPlan plan = staged_batch_plan(lm_slots, LAT_GROUP); { // the walk below flushes on exactly these conditions size_t pn = 0; for (size_t u = 0; u < uniq.size(); ++u) { const int32_t e = uniq[u]; const int64_t c = off[(size_t) e + 1] - off[(size_t) e]; if (pn > 0 && (!use_fused || cuts.back().count + c > G || c > G || pn == plan.group_limit || e + 1 - cuts.back().e0 > fused::kBatchRoom)) pn = 0; if (c > G) { for (int64_t p = off[(size_t) e]; p < off[(size_t) e + 1]; p += G) cuts.push_back({e, e + 1, p, std::min(G, off[(size_t) e + 1] - p)}); continue; } if (pn == 0) cuts.push_back({e, e + 1, off[(size_t) e], 0}); cuts.back().e1 = e + 1; cuts.back().count += c; ++pn; } if (use_fused) { if (cuts.size() * tab_stride > lb.lat_tabs_n) { err = "prefill latent layer-major: more A batches than the table buffer holds"; return false; } std::vector tabs(cuts.size() * tab_stride, 0); std::vector tile_cnt((size_t) NE), tile_off((size_t) NE + 1); for (size_t k = 0; k < cuts.size(); ++k) { const Cut& ct = cuts[k]; auto rel = [&](int64_t r) { return (int32_t) (std::clamp(r, ct.base, ct.base + ct.count) - ct.base); }; std::fill(tile_cnt.begin(), tile_cnt.end(), 0); for (int32_t e = 0; e <= NE; ++e) { tile_off[(size_t) e] = e < ct.e0 ? 0 : e > ct.e1 ? (int32_t) ct.count : rel(off[(size_t) e]); if (e >= ct.e0 && e < ct.e1) tile_cnt[(size_t) e] = rel(off[(size_t) e + 1]) - rel(off[(size_t) e]); } fused::group_latent(tile_cnt.data(), tile_off.data(), (int) NE, tabs.data() + k * tab_stride, fused::latent_group_bytes(ct.count, (int) NE)); } if (m.up_ring.h2d(lb.lat_tabs, tabs.data(), tabs.size(), cs) != cudaSuccess) { err = "prefill latent layer-major: the A tables' upload failed"; return false; } } } size_t staged = 0, nb = 0; int32_t e0 = 0, e_last = 0; int64_t base = 0, count = 0; fused::WideBatch blobs; std::vector open_slots; auto flush = [&]() -> bool { if (nb == 0) return true; if (!open_slots.empty()) { // one copy stream, in order: the last copy covers the others pt.mark(kPfWaitCopy, cs); cudaStreamWaitEvent(cs, m.copied[open_slots.back()], 0); } if (!latent_a_tile(e0, e_last + 1, base, count, blobs)) return false; for (int sl : open_slots) { cudaEventRecord(m.used[sl], cs); m.used_of[sl] = sl; } open_slots.clear(); blobs = fused::WideBatch{}; nb = 0; return true; }; for (size_t u = 0; u < uniq.size(); ++u) { const int32_t e = uniq[u]; const int64_t c = off[(size_t) e + 1] - off[(size_t) e]; // a full batch launches before more is staged: its slots plus the lookahead fill the ring if (nb > 0 && (!use_fused || count + c > G || c > G || nb == plan.group_limit || e + 1 - e0 > fused::kBatchRoom)) { if (!flush()) return false; } while (staged < uniq.size() && staged <= u + plan.ahead) { if (!stage_one(staged++)) return false; } const uint8_t* blob = nullptr; if (stage_of[u] < 0 && prefetched(e)) { blob = lb.pre + (size_t) lb.pre_of[(size_t) e] * (size_t) MAXBLOB(); } else if (stage_of[u] < 0) { blob = m.cache->device_slot(m.host_res[(size_t) l * NE + e]); ++stats_.experts_resident; ++lm_resident; } else { blob = m.stage_dev[stage_of[u]]; } if (c > G) { // more rows than the tile buffers: this expert alone, in pieces fused::WideBatch one; one.blob[0] = blob; if (stage_of[u] >= 0) cudaStreamWaitEvent(cs, m.copied[stage_of[u]], 0); for (int64_t p = off[(size_t) e]; p < off[(size_t) e + 1]; p += G) if (!latent_a_tile(e, e + 1, p, std::min(G, off[(size_t) e + 1] - p), one)) return false; if (stage_of[u] >= 0) { cudaEventRecord(m.used[stage_of[u]], cs); m.used_of[stage_of[u]] = stage_of[u]; } continue; } if (nb == 0) { e0 = e; base = off[(size_t) e]; count = 0; } blobs.blob[e - e0] = blob; if (stage_of[u] >= 0) open_slots.push_back(stage_of[u]); e_last = e; count += c; ++nb; core::progress_beat(); } return flush(); }; if (lb.pre_n > 0 && pre_pending == l) { // this layer's prefetched experts: enqueued, then waited on while (pre_ready.load(std::memory_order_acquire) != l) std::this_thread::yield(); lb.pre_layer = l; cudaStreamWaitEvent(cs, lb.pre_done, 0); } if (!latent_a_superchunk()) return false; // the next layer's RAM-tier experts, routed or not (nearly all are at this length), copied while this // layer's U and the next layer's attention run. a DMA thread enqueues them: a cudaMemcpyAsync blocks while // the copy engine's queue is full, which held back the next launches on this thread. they wait for this // layer's A to be launched, which may still read the same slots. lb.pre_layer = -1; if (lb.pre_n > 0 && l + 1 < LE && m.src != nullptr) { cudaEventRecord(lb.pre_free, cs); pre_pending = l + 1; const int64_t l1 = l + 1; dma.push([&, l1] { cudaStreamWaitEvent(m.copy, lb.pre_free, 0); std::fill(lb.pre_of.begin(), lb.pre_of.end(), -1); int64_t k = 0; for (int32_t e = 0; e < NE && k < lb.pre_n; ++e) { if ((m.host_res && m.cache && m.host_res[(size_t) l1 * NE + e] >= 0) || !m.src->pinned(l1, e)) continue; const uint8_t* b = m.src->blob_stable(l1, e); if (b == nullptr) continue; cudaMemcpyAsync(lb.pre + (size_t) k * (size_t) MAXBLOB(), b, (size_t) lay.blob_bytes(l1), cudaMemcpyHostToDevice, m.copy); lb.pre_of[(size_t) e] = (int32_t) k++; pre_bytes.fetch_add((int64_t) lay.blob_bytes(l1), std::memory_order_relaxed); pre_blobs.fetch_add(1, std::memory_order_relaxed); } cudaEventRecord(lb.pre_done, m.copy); pre_ready.store(l1, std::memory_order_release); }); } for (int64_t t = 0; t < Sn; t += m.T) if (!latent_u_reduce(l, t, std::min(m.T, Sn - t))) return false; // this layer's reads are all issued: the next layer's start now, while U and its attention run if (pf_two && l + 1 < LE && !lm_prefetch(l + 1)) return false; ms_experts += ms_since(th0); 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] && !fz_l; // (fz_l without an MMQ layer: a build without MMQ, a layer of the plan's `fo` set) const size_t mmq_gub = fp16_l ? 0 : mmq::matrix_bytes(mmq_gt, kFF2, N), mmq_db = fp16_l ? 0 : mmq::matrix_bytes(mmq_dt, N, kFF); 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 // fused: the open group's expert pointers and the staged slots its launch gives back; the super-chunk's rows to int8 once per layer const uint8_t* fz_blob[MMQ_GROUP] = {}; int fz_rel[MMQ_GROUP] = {}; int fz_nrel = 0; fused::NativeGeom fz_geom{}; if (fz_l) { const auto& f = lay.fmt[(size_t) l]; fz_geom = fused::NativeGeom{f.gu_type, f.d_type, f.gu_row, f.d_row, f.up_off, f.down_off}; fused::quantize_act_native(lb.mixed, Sn, N, lb.xa, cs); } 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 + ahead) { 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 * kFF2, nr, kFF2, N); swiglu_interleaved(m.GU + o * kFF2, m.Hh + o * kFF, nr, cs); pt.mark(kPfGemmD, cs); m.gemm.f16(m.Hh + o * kFF, m.dq_d[dq], m.Dm + o * N, nr, N, kFF); 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 (fz_l) { // P4-SMALL: the group's experts where they are; its two products and SwiGLU in one launch pair at its last entry fz_blob[q] = blob_dev; if (j == last_ent[u] && stage_of[u] >= 0) fz_rel[fz_nrel++] = stage_of[u]; if (j + 1 < gp.j1) continue; const int ngx = (int) (gp.j1 - gp.j0); pt.mark(kPfGemmGU, cs); fused::group_bounds(d_bounds + gp.b_at, ngx, m.GU, cs); fused::Batch fb; fb.e0 = 0; fb.e1 = ngx; for (int i = 0; i < ngx; ++i) fb.blob[i] = fz_blob[i]; fused::want_dm_f16(false); fused::experts_native(fb, fz_geom, ngx, gp.nr, m.GU, lb.xa, d_row_tok + gp.r0, m.H, m.Dm, cs); for (int i = 0; i < fz_nrel; ++i) { // the group's staged blobs are read: their slots are free cudaEventRecord(m.used[fz_rel[i]], cs); m.used_of[fz_rel[i]] = fz_rel[i]; } fz_nrel = 0; 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_fz; ++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 = kFF2; 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 = kFF2; m.mmq_ctx->run(gu, cs); mmq::swiglu(m.GU, m.H, gp.nr, kFF, !lay.native, cs); pt.mark(kPfGemmD, cs); mmq::quantize(m.H, nullptr, m.Hq, mmq_dt, kFF, kFF, gp.nr, cs); mmq::Product dn; dn.w = m.grp_d; dn.type = mmq_dt; dn.w_rows = N; dn.w_cols = kFF; 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; }; #if defined(STRATA_HAVE_DFLASH2) // DFlash2 prompt taps (DF-08): one prompt, one stage (lm_prepare declined the rest). A layer's completed output is the rows after the // hyper-connection write of its FFN half, which this walk does when the NEXT layer starts (or in the final pass for the last one); // each sub-chunk's tapped rows (positions >= tap_first_) go to their place in the staging, and the super-chunk's rows are handed to // on_taps, in position order, once all its layers are done std::vector tap_slot; const bool tap_on = !tap_layers_.empty() && on_taps && segs.size() == 1 && LB == 0 && LE == L && tap_cap_ > 0; if (tap_on) { if (tap_dev_ == nullptr && cudaMalloc((void**) &tap_dev_, (size_t) tap_cap_ * tap_layers_.size() * (size_t) g.n_embd * 4) != cudaSuccess) { (void) cudaGetLastError(); tap_dev_ = nullptr; err = "prefill: the DFlash2 tap staging does not fit in VRAM"; return false; } tap_slot.assign((size_t) g.n_layers, -1); for (size_t i = 0; i < tap_layers_.size(); ++i) tap_slot[(size_t) tap_layers_[i]] = (int) i; } else if (!tap_layers_.empty()) { err = "prefill (layer-major): the DFlash2 taps are not supported for this read"; return false; } #endif for (size_t sci = 0; sci < supers.size(); ++sci) { const std::vector& subs = supers[sci]; int64_t Sn = 0; for (const LmSub& sb : subs) Sn = std::max(Sn, sb.t0 + sb.T); #if defined(STRATA_HAVE_DFLASH2) // the super-chunk's tapped rows: those at positions >= tap_first_ (the super-chunk's row r is position segs[0].pos0 + subs[0].c0 + r) const int64_t super_pos = segs[0].pos0 + subs[0].c0; const int64_t tap_s0 = tap_on ? std::min(Sn, std::max(0, tap_first_ - super_pos)) : Sn; // first tapped row const int64_t tap_super_rows = Sn - tap_s0; if (tap_on && tap_super_rows > tap_cap_) { err = "prefill: the DFlash2 tap staging is smaller than the super-chunk's tapped rows"; return false; } #endif const int64_t nc = (int64_t) subs.size(); int64_t last_seg0 = -1; for (int64_t c = 0; c < nc; ++c) if (subs[(size_t) c].seg == 0) last_seg0 = c; ++lm_supers; lm_mid_ = true; if (stage_lb_ > 0 && lb.r_dev && cudaMemcpyAsync(lb.R, lb.R_host, (size_t) Sn * D * 4, cudaMemcpyHostToDevice, cs) != cudaSuccess) { err = "prefill (layer-major): the super-chunk's rows did not go up"; return false; } for (int64_t l = LB; l <= LE; ++l) { const auto ta = Clock::now(); if (short_w_ != nullptr) { // read_short: the first stage's layers come through two weight slots if (l > 0 && l - 1 < short_end_ && !short_w_->end(l - 1, short_end_, cs)) { err = "prefill: the streamed weights"; return false; } if (l == 0 && (!short_w_->issue(0) || (short_end_ > 1 && !short_w_->issue(1)))) { err = "prefill: the streamed weights"; return false; } if (l < short_end_ && !short_w_->begin(l, cs)) { err = "prefill: the streamed weights"; return false; } } if (l < LE && (!pf_two || l == LB) && !lm_prefetch(l)) return false; // (two: started by l - 1's walk) // 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 TT = subs[(size_t) c].T; cudaMemcpyAsync(r_buf(c), lb.R_host + (size_t) subs[(size_t) c].t0 * 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(); const LmSub& sub = subs[(size_t) c]; const Seg& sgm = segs[(size_t) sub.seg]; const int64_t* const tokens = sgm.tokens; ssp = ss_of((size_t) sub.seg); if (short_lo_ != nullptr && l < short_end_) ssp = short_lo_; core::progress_at("reading the prompt (layer-major): layer", l, sgm.pos0 + sub.c0); qsa_index = qsa_index0; gdn_index = gdn_index0; const int64_t c0 = sub.c0; // in this prompt's tokens const int64_t T = sub.T, p0 = sgm.pos0 + c0; const int64_t t0 = sub.t0; // in the super-chunk const int64_t gi = flat_base[sci] + c; // in the whole run 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, gi, b = ple_buf] { strata::aux_cpus::pin_current_thread(); return ple_gather(gi, b, ple_next_err); }); { std::array& pvv = prevs[(size_t) sub.seg]; for (int64_t t = 0; t < T; ++t) { pvv[0] = pvv[1]; pvv[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 defined(STRATA_HAVE_DFLASH2) if (tap_on && tap_slot[(size_t) (l - 1)] >= 0) { // layer l - 1 is complete: its tapped rows of this sub-chunk, mean of the streams const int64_t lo = std::min(T, std::max(0, tap_s0 - t0)); if (lo < T) strata::kernels::df2_tap_rows(m.R + lo * D, tap_dev_ + (size_t) (t0 + lo - tap_s0) * tap_layers_.size() * (size_t) N + (size_t) tap_slot[(size_t) (l - 1)] * (size_t) N, (int64_t) tap_layers_.size() * N, N, (int) HC, (int) (T - lo), cs); } #endif } 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, p0 == 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 != last_seg0 && last_seg0 >= 0; if (sub.seg == 0) { // (the reports are the first prompt's; the others only fill their sessions) 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(gi, 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 = pss->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, ssp->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, ssp->ple_hist, pss->ple.w, po, pss->ple.scratch, cs); } catch (const std::exception& e) { err = std::string("prefill PLE: ") + e.what(); return false; } strata::kernels::ple_history_advance(ssp->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; } // the stream is not optional: under STRATA_HC_REQ8 bf16_proj expands the int8 codes into one static // scratch per call site, on the stream it is given. Omitted, the GEMMs below read it out of order. 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, 0, cs)) 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, 0, cs)) return false; if (!bf16_proj(m.gemm, wi, m.xn16, m.inj, T, si, err, 0, m.xn16_lo, 0, cs)) 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 = ssp->gdn_state + (size_t) (gdn_index - ssp->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); bool fused_gdn = false; #if defined(STRATA_USE_HIP) fused_gdn = gdn_conv_recurrence_fused(conv, m.qkv, (const float*) wc->data, m.hbuf, state, m.gate, m.beta, m.z, (const float*) wnm->data, EPS, EPS, m.y, m.y_h, T, cs, 0); #endif if (!fused_gdn) gdn_conv(conv, m.qkv, (const float*) wc->data, m.hbuf, T, EPS, cs); pt.mark(kPfGdnRec, cs); if (!fused_gdn) 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 = ssp->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; if (qsa_fuse_nr()) { rms_rope_rows(m.Kc, 512, 256, m.Kc, 512, (const float*) wkn->data, T, 2, 256, EPS, p0, strata::kernels::rope_scaling(), cs); } else { 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 static const bool kv_host_dma = core::kv_host_dma_on(std::getenv("STRATA_KV_HOST_DMA"), true); #endif const bool host_by_dma = core::kv_host_dma_use(kv_host_dma, staged, m.stage.present()); 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); } if (qsa_fuse_nr()) { rms_rope_rows(m.Qf, kQFull, 2 * kHeadDim, m.q, kQW, (const float*) wqn->data, T, kNHead, kHeadDim, EPS, p0, strata::kernels::rope_scaling(), cs); } else { split_q(m.Qf, m.q, T, cs); rms_rows(m.q, (const float*) wqn->data, T * kNHead, kHeadDim, kHeadDim, EPS, cs); rope(m.q, T, kNHead, kHeadDim, kQW, p0, strata::kernels::rope_scaling(), cs); } if (st.kv_rot) strata::kernels::fwht256_inplace_cuda(m.q, T * kNHead, cs); if (qsa_fuse_nr()) { rms_rope_rows(m.q_idx, 512, 128, m.q_idx, 512, (const float*) wiqn->data, T, 4, 128, EPS, p0, strata::kernels::rope_scaling(), cs); } else { 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; const bool gated = !old_attn && !st.kv_rot && !st.kv_hybrid && qsa_fuse_gate() && strata::kernels::qsa_prompt_attn_batch_gated(m.q, pools, m.sel_ids, m.steps_dev, m.cap, s, m.Qf, m.attn_h, ZV, T, cs); if (!gated && (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 * kQW, pools, m.sel_ids + q0 * m.cap, m.steps_dev + q0 * strata::kernels::kStepCount, m.cap, s, m.attn_scratch, m.attn + q0 * kQW, nb, cs); } if (st.kv_rot || st.kv_hybrid) strata::kernels::fwht256_inplace_cuda(m.attn, T * kNHead, cs); pt.mark(kPfQsa, cs); if (!gated) 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); if (m.latent) { if (!latent_dense_route(l, t0, T)) return false; cudaMemcpyAsync(lb.mixed + t0 * N, m.shared, (size_t) T * N * 4, cudaMemcpyDeviceToDevice, cs); cudaMemcpyAsync(lb.lat_sg + t0, m.sg, (size_t) T * 4, cudaMemcpyDeviceToDevice, cs); } else { 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 defined(STRATA_HAVE_DFLASH2) if (tap_on && tap_super_rows > 0) { // every layer of the super-chunk is done: its tapped rows, in position order (DF-08) if (cudaStreamSynchronize(cs) != cudaSuccess) { err = std::string("prefill: ") + cudaGetErrorString(cudaGetLastError()); return false; } if (!on_taps(tap_dev_, tap_super_rows, super_pos + tap_s0, err)) return false; } #endif 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 && last_seg0 >= 0 && !on_stage_chunk(segs[0].pos0 + subs[(size_t) last_seg0].c0 + subs[(size_t) last_seg0].T, err)) return false; next_err_.clear(); next_run_ = std::async(std::launch::async, [this, segs_c = std::make_shared>(segs), depth, sp_super = std::make_shared>(supers[sci])] { strata::aux_cpus::pin_current_thread(); return next_->lm_run(*segs_c, depth + 1, sp_super.get(), next_err_); }); } } lm_mid_ = false; for (size_t i = 0; i < segs.size(); ++i) { ss_of(i)->ple_prev[0] = prevs[i][0]; ss_of(i)->ple_prev[1] = prevs[i][1]; } if (short_lo_ != nullptr) { short_lo_->ple_prev[0] = prevs[0][0]; short_lo_->ple_prev[1] = prevs[0][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); lm_streamed += pre_blobs.load(); lm_bytes += (uint64_t) pre_bytes.load(); 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, %lld fused); %.0f ms (host: " "enqueueing the attention halves %.0f, the experts %.0f: waiting for the attention halves " "%.0f, the routing tables %.0f, the expert files %.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, (long long) lm_groups_fz, ms_since(t_start), ms_attn, ms_experts, ms_wait_a, ms_host_tables, ms_wait_files); 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; } // The first stage's rows of layers [0, lo_end) that `mine` lacks, laid out in one slot (256-byte aligned: the canonical // bytes, then the native GGUF blocks). `each` sees every row with its offsets and may refuse it. Returns one slot's row // bytes; `why` is set when a row cannot be streamed. template static uint64_t short_rows(const core::WeightTable& mine, const core::WeightTable& first, int64_t lo_end, std::string& why, Each&& each) { uint64_t need = 0; for (int64_t l = 0; l < lo_end && why.empty(); ++l) { const std::string prefix = "blk." + std::to_string(l) + "."; uint64_t off = 0; for (auto it = mine.all().lower_bound(prefix); it != mine.all().end() && it->first.rfind(prefix, 0) == 0; ++it) { if (it->second.data != nullptr || it->second.native_data != nullptr) continue; // (held here: the PLE tensors) const core::WeightRef* s = first.find(it->first); if (s == nullptr || (s->data == nullptr && s->native_data == nullptr)) continue; if (s->hc_q8 != nullptr || s->q8_scale != nullptr) { why = "a first-stage weight in HC Q8 form"; break; } uint64_t o = 0, b = 0, no = 0, nb = 0; if (s->data != nullptr) { o = off; b = s->quantized() ? s->codes_bytes + s->scales_bytes + s->offset_bytes : s->bytes; off = (off + b + 255) & ~(uint64_t) 255; } if (s->native_data != nullptr) { no = off; nb = strata::kernels::native_mmvq_weight_bytes(s->native_type, (int) s->ne0, (int) s->ne1); off = (off + nb + 255) & ~(uint64_t) 255; } if (!each(l, it->first, *s, o, b, no, nb)) { why = "no native-projection scratch on this GPU"; break; } } const core::WeightRef* router = mine.find(prefix + "ffn_gate_inp.weight"); if (why.empty() && (router == nullptr || router->data == nullptr) && off == 0) why = "the first stage does not hold its layers' weights"; need = std::max(need, off); } return need; } uint64_t Prefill::short_slot_bytes(const core::WeightTable& mine, const core::WeightTable& first, int64_t lo_end, uint64_t d_bytes, uint64_t u_bytes) { std::string why; const uint64_t rows = short_rows(mine, first, lo_end, why, [](auto&&...) { return true; }); if (!why.empty()) return 0; const uint64_t d_off = rows, u_off = (d_off + d_bytes + 255) & ~(uint64_t) 255; return u_off + u_bytes; } bool Prefill::set_short_slots(void* slot0, void* slot1, uint64_t bytes, std::string& err) { cudaStream_t st = nullptr; cudaEvent_t ev[4] = {}; bool ok = cudaStreamCreateWithFlags(&st, cudaStreamNonBlocking) == cudaSuccess; for (int i = 0; ok && i < 4; ++i) ok = cudaEventCreateWithFlags(&ev[i], cudaEventDisableTiming) == cudaSuccess; if (!ok) { (void) cudaGetLastError(); err = "prefill: the short read's weight stream"; return false; } short_slot_[0] = slot0; short_slot_[1] = slot1; short_slot_n_ = bytes; short_st_ = st; for (int i = 0; i < 4; ++i) short_ev_[i] = ev[i]; return true; } bool Prefill::read_short(const int64_t* tokens, int64_t n, int64_t pos0, core::SessionState* lo, int64_t lo_end, const Prefill* first, std::string& err) { err.clear(); const auto decline = [&](const char* why) { err = std::string("prefill: short read declined: ") + why; return false; }; if (!impl_ || impl_->g == nullptr) return decline("no prefill"); Impl& m = *impl_; const core::ModelGeometry& g = *m.g; if (pos0 != 0 || n <= 0) return decline("a continued prompt"); if (next_ != nullptr || stage_lb_ != lo_end || stage_le_ != g.n_layers) return decline("not the last stage of a two-way split"); if (lo == nullptr || lo->layer_lo != 0 || lo->layer_hi != lo_end || n > lo->max_cells) return decline("the scratch session"); if (first == nullptr || !first->impl_ || first->next_ != this || first->stage_lb_ != 0 || first->stage_le_ != lo_end) return decline("no first stage to take the weights from"); if (first->next_run_.valid()) return decline("a layer split's chunk hand-off is in flight"); hand_in_ = nullptr; // (stale: a chunk-major prompt's last hand-off, as lm_prepare_chain clears it) hand_ev_ = nullptr; hand_pieces_ = 0; const Impl& f = *first->impl_; if (!m.latent || !f.latent || f.lat_first_layer > 0 || !f.lat_d_dev || !f.lat_u_dev || f.lat_d.d_bytes() != m.lat_d.d_bytes() || f.lat_d.u_bytes() != m.lat_d.u_bytes()) return decline("the first stage's latent D/U tables"); if (g.n_qsa_layers() > 0 && lo->qsa_states[lo->qsa_primary()].kv_mode != 0) return decline("streamed K/V"); const core::OnDevice on_device(m.device); // the first stage's layers' rows this table lacks, laid out in one slot (256-byte aligned), then that layer's D and U ShortWeights w; w.shadow = *m.wt; w.items.resize((size_t) lo_end); void* q8_1 = nullptr; // this GPU's native-projection activation scratch (one per table) for (const auto& [name, ref] : m.wt->all()) if (ref.native_q8_1 != nullptr) { q8_1 = ref.native_q8_1; break; } std::string rows_why; const uint64_t need = short_rows(*m.wt, *f.wt, lo_end, rows_why, [&](int64_t l, const std::string& name, const core::WeightRef& s, uint64_t off, uint64_t bytes, uint64_t noff, uint64_t nbytes) { ShortWeights::Item item{w.shadow.find_mut(name), s, off, bytes, noff, nbytes}; item.dst->native_q8_1 = q8_1; w.items[(size_t) l].push_back(item); return nbytes == 0 || q8_1 != nullptr; }); if (!rows_why.empty()) return decline(rows_why.c_str()); w.d_bytes = m.lat_d.d_bytes(); w.u_bytes = m.lat_d.u_bytes(); w.d_off = need; w.u_off = (w.d_off + w.d_bytes + 255) & ~(uint64_t) 255; const uint64_t slot_bytes = w.u_off + w.u_bytes; w.src_d = f.lat_d_dev; w.src_u = f.lat_u_dev; w.src_dev = f.device; w.dev = m.device; if (short_slot_n_ < slot_bytes || short_st_ == nullptr) return decline("no weight slots (set_short_slots)"); for (int s = 0; s < 2; ++s) { w.slot[s] = (uint8_t*) short_slot_[s]; w.ready[s] = (cudaEvent_t) short_ev_[s]; w.freed[s] = (cudaEvent_t) short_ev_[2 + s]; } w.st = (cudaStream_t) short_st_; // the residency table holds the slots of whichever GPU's cache owns a layer: the first stage's layers have none here std::vector res; if (m.host_res != nullptr) { const size_t NE = (size_t) g.n_expert; res.assign(m.host_res, m.host_res + (size_t) g.n_layers * NE); std::fill(res.begin(), res.begin() + (size_t) lo_end * NE, (int32_t) -1); } struct Restore { Prefill* p; int64_t lb; const int32_t* res; const core::WeightTable* wt; ~Restore() { p->stage_lb_ = lb; p->short_lo_ = nullptr; p->short_end_ = 0; p->short_w_ = nullptr; p->lm_force_ = false; p->lm_mid_ = false; p->impl_->host_res = res; p->impl_->wt = wt; } } restore{this, stage_lb_, m.host_res, m.wt}; if (!res.empty()) m.host_res = res.data(); m.wt = &w.shadow; short_w_ = &w; stage_lb_ = 0; short_lo_ = lo; short_end_ = lo_end; lm_force_ = true; std::string why; if (lm_prepare_chain(n, why) <= 0) return decline(why.c_str()); Seg one; one.tokens = tokens; one.n = n; one.pos0 = 0; const bool ok = lm_run(std::vector{one}, 0, nullptr, err); lm_release_chain(); return ok; } bool Prefill::drain_pipeline(std::string& err) { bool ok = true; if (next_run_.valid()) { if (!next_run_.get()) { err = next_err_; ok = false; } } if (next_ != nullptr) { std::string tail_err; if (!next_->drain_pipeline(tail_err)) { if (ok) err = tail_err; ok = false; } } return ok; } bool Prefill::reserve_uploads(int64_t T) { bool ok = true; if (impl_ != nullptr && T > 0) { Impl& m = *impl_; const core::OnDevice on_device(m.device); // (the ring is the device's that is current at its first use) // a text chunk's largest pageable uploads through the ring: the routing tables (and their ids down, T x K // each); the PLE rows when they could not be pinned (T x N). A picture's rows are not covered. size_t bytes = (size_t) (T * K) * sizeof(int32_t); if (!m.ple_pageable[0].empty() || !m.ple_pageable[1].empty()) bytes = std::max(bytes, (size_t) (T * N) * sizeof(float)); ok = m.up_ring.reserve(bytes); } if (next_ != nullptr) ok = next_->reserve_uploads(T) && ok; return ok; } bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& err) { #if defined(STRATA_USE_HIP) || defined(STRATA_HIP_GFX906) (void) strata::kernels::qsa_two_pass_on(); // HIP: a set STRATA_SELECT_TWO_PASS says once it is not used #endif #if defined(STRATA_GPU_METAL) static strata::kernels::two_pass::RequestStatus tp_status(std::getenv("STRATA_SELECT_TWO_PASS")); (void) tp_status.on("Metal"); { const auto t0 = std::chrono::steady_clock::now(); // DFlash2: the window writes the taps of the prompt rows at positions >= tap_first_ and hands them to on_taps after each chunk const bool taps = !tap_layers_.empty() && on_taps; if (taps && !strata::metal_engine::prompt_taps(tap_layers_, tap_first_, on_taps, err)) return false; const bool ok = strata::metal_engine::prompt_run(tokens, n, pos0, embd_rows, on_chunk, err); if (taps) { std::string off_err; strata::metal_engine::prompt_taps(tap_layers_, 0, {}, off_err); // no callback after the prompt } stats_.tokens += n; stats_.chunks += 1; stats_.ms_total += std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); return ok; } #endif #if defined(STRATA_USE_HIP) || defined(STRATA_HIP_GFX906) constexpr bool lm_hip = true; #else constexpr bool lm_hip = false; #endif // layer-major prefill: STRATA_PREFILL_LAYER_MAJOR=1 / 0 win; unset, it is off everywhere (sm120_series.hpp prefill_layer_major_default: a layer split // reads 32K prompts 37-55% sooner chunk-major, owner's yes 2026-11-02; the single-card small-cache rule sets the variable itself) if (hand_in_ == nullptr && impl_ && strata::prefill_layer_major_resolve(lm_env_text(), lm_hip, stage_count(), strata::qwen_defaults_026())) { const bool by_default = !lm_env_explicit(); for (Prefill* p = this; p != nullptr; p = p->next_) p->lm_by_default_ = by_default; std::string why; if (by_default) { static bool noted = false; if (!noted) { noted = true; std::fprintf(stderr, "strata prefill: layer-major prefill is on for this layer split (each layer's experts are read once per " "super-chunk of up to %lld tokens, not once per chunk; STRATA_PREFILL_LAYER_MAJOR=0 or STRATA_DEFAULTS=026 " "turns it off)\n", (long long) kLmDefaultOnTokens); } } if (lm_prepare_chain(n, why) > 0) { #if !defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906) // STRATA_SELECT_TWO_PASS is in run_impl's selection only, not in this walk: say so once static strata::kernels::two_pass::RequestStatus tp_status(std::getenv("STRATA_SELECT_TWO_PASS")); (void) tp_status.on("the layer-major prompt path"); #endif 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; not worth a line when nobody asked for the walk) if (why != said && !(by_default && why == "a short prompt")) { 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; const bool drain_ok = drain_pipeline(drain_err); if (!body_ok) return false; if (!drain_ok) { err = drain_err; return false; } // STRATA_MMQ_DIRECT=1 with STRATA_MMQ_DIRECT_LOG=1 (a check, not a default): each stage's totals so far - how many // resident experts its prompt MMQ read in their slots, of all its resident and streamed ones if (mmq::direct_mode() == 1) { static const bool log = [] { const char* v = std::getenv("STRATA_MMQ_DIRECT_LOG"); return v && std::atoi(v) != 0; }(); for (const Prefill* p = this; log && p != nullptr; p = p->next_) std::fprintf(stderr, "strata prefill: layers %lld..%lld: %lld of %lld resident experts read in place, %lld " "streamed (STRATA_MMQ_DIRECT, totals)\n", (long long) p->stage_lb_, (long long) p->stage_le_, (long long) p->stats_.experts_direct, (long long) p->stats_.experts_resident, (long long) p->stats_.experts_streamed); } return true; } } // namespace strata::prefill