mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
[Engine] Prompt FP16 path: few-row experts on the quantized grouped kernels (research switch, off)
On the FP16 prompt path every routed expert is dequantized to FP16 before its two GEMMs, at about 104 us an expert on a Tesla P100 whatever its row count; a short piece routes only a few rows to most experts and pays it for nothing. With STRATA_PF_FEWROWS set (research switch, off unless set, "0" off) an expert with 1..12 rows of a native pack (no APR) runs on native_expert_grouped, the quantized kernels the verify window uses, on its GGUF blob with q8_1 activations and no dequantize. A one-block kernel writes the expert's plan record on the device. The rule and the switch are in include/strata/prefill/fewrows.hpp (fewrows_rule_test); the streamed and staged walk takes it, the layer-major walk is unchanged. With the switch unset nothing changes except one added kernel (fewrows_record_kernel) that nothing launches. No default and no user-visible text; settings register entry STRATA_PF_FEWROWS, status research. Tesla P100 (sm_60), IQ3_XXS, STRATA_PREFILL_MMQ=0 with the switch on, from the screen build (3 rotated rounds; prompt tok/s, ratio to MMQ): prompt MMQ FP16 path few-rows ratio few-rows / MMQ plain FP16 / MMQ 300 171.3 144.9 162.8 0.954 0.846 1,000 248.4 215.6 226.6 0.911 0.868 2K 291.1 264.5 270.7 0.931 0.909 4K 304.5 290.5 303.5 0.997 0.954 8K 343.6 352.1 358.3 1.044 1.025 20K 332.7 335.8 344.4 1.035 1.009 First-token KL 2K / 8K (mean): few-rows vs plain FP16 0.0168 / 0.0048, vs MMQ 0.0065 / 0.0085 (plain FP16 vs MMQ 0.0155 / 0.0078, MMQ again 0.0012 / 0.0006). Gate on the committed tree follows (lane P4-FALLBACK, phaseA-tests/par).
This commit is contained in:
1 parent
482c6beeab
commit
af5803e4be
7 files changed
+188
No files matched your search
@@ -435,6 +435,11 @@ if(STRATA_BUILD_TESTS)
|
||||
target_include_directories(mmq_ids_margin_test PRIVATE include)
|
||||
add_test(NAME mmq_ids_margin_test COMMAND mmq_ids_margin_test)
|
||||
|
||||
# few-row experts on the FP16 prompt path: the research switch and the rule (header-only, no GPU)
|
||||
add_executable(fewrows_rule_test tests/prefill/fewrows_rule_test.cpp)
|
||||
target_include_directories(fewrows_rule_test PRIVATE include)
|
||||
add_test(NAME fewrows_rule_test COMMAND fewrows_rule_test)
|
||||
|
||||
# PR #4844: the adaptive tier's copy-in range across serial, pipelined and drained rounds (header-only, no GPU)
|
||||
add_executable(adaptive_range_test tests/program/adaptive_range_test.cpp)
|
||||
target_include_directories(adaptive_range_test PRIVATE include)
|
||||
|
||||
@@ -23829,6 +23829,34 @@
|
||||
"retire_after": "",
|
||||
"retired_in": ""
|
||||
},
|
||||
{
|
||||
"name": "STRATA_PF_FEWROWS",
|
||||
"backends": [
|
||||
"cuda-hip"
|
||||
],
|
||||
"reads": [
|
||||
{
|
||||
"file": "src/prefill/prefill.cpp",
|
||||
"line": 5830,
|
||||
"backend": "cuda-hip",
|
||||
"fn": "getenv"
|
||||
}
|
||||
],
|
||||
"parse": "string",
|
||||
"default_state": null,
|
||||
"code_default": null,
|
||||
"engine_sets": [],
|
||||
"engine_defaults": [],
|
||||
"launch_files": [],
|
||||
"launch_sets": [],
|
||||
"launch_reads": [],
|
||||
"doc_mentions": [],
|
||||
"status": "research",
|
||||
"owner": "prefill",
|
||||
"note": "[reviewed] Research switch, off unless set (\"0\" off, anything else on; src/prefill/fewrows.hpp): on the FP16 prompt path an expert with 1..12 rows runs on the quantized grouped kernels (no dequantize) instead of dequantize + FP16 GEMMs; native packs without APR only. Tesla P100 numbers in phaseA-tests/par/RESULTS-P4-FALLBACK.md. Not documented for users",
|
||||
"retire_after": "",
|
||||
"retired_in": ""
|
||||
},
|
||||
{
|
||||
"name": "STRATA_PF_FUSED",
|
||||
"backends": [
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
// Few-row experts on the FP16 prompt path (lane P4-FALLBACK, 11-11).
|
||||
//
|
||||
// The FP16 path (no MMQ) dequantizes every routed expert of a layer to FP16 before its two GEMMs. The dequantize costs
|
||||
// about the same for an expert with 6 rows as for one with 600, so a short piece (a few hundred tokens routes only a handful
|
||||
// of rows to most experts) pays it for nothing. An expert with 1..kFewRows rows instead runs on the quantized grouped
|
||||
// kernels the verify window already uses (strata::kernels::native_expert_grouped, reading the GGUF blocks and q8_1
|
||||
// activations), with no dequantize. Measured on a Tesla P100 (IQ3_XXS, plan phaseA-tests/par/RESULTS-P4-FALLBACK.md):
|
||||
// 0.846 -> 0.954 of MMQ's prompt speed at 300 tokens, 0.868 -> 0.911 at 1,000, 0.909 -> 0.931 at 2K, 0.954 -> 0.997 at 4K,
|
||||
// and above plain FP16 at 8K and 20K (1.044 / 1.035 of MMQ).
|
||||
//
|
||||
// The switch STRATA_PF_FEWROWS is a research switch: unset = `dflt` (off in this build's code), "0" off, anything else on.
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace strata::prefill {
|
||||
|
||||
/// An expert with 1..kFewRows rows takes the quantized kernels. Screened at 6, 12, 16, 24 and 32 on the P100: 12 was best.
|
||||
constexpr int kFewRows = 12;
|
||||
/// One-expert plan record (the grouped kernels' arguments, written by a one-block kernel so no host buffer is involved):
|
||||
/// {u64 blob, i32 start[2] = {0, ne}, i32 n_groups = 1, i32 pad, i32 dst[ne] = o0 + i}.
|
||||
constexpr size_t kFewRowsRecBytes = 24 + 4 * (size_t) kFewRows;
|
||||
|
||||
/// The switch's value: `v` is the variable's text (nullptr when unset); `dflt` is the answer when it is unset or empty.
|
||||
inline bool fewrows_switch(const char* v, bool dflt) {
|
||||
if (v == nullptr || v[0] == '\0') return dflt;
|
||||
return v[0] != '0';
|
||||
}
|
||||
|
||||
/// Whether the expert with `rows` rows (its rows of this layer's chunk) runs on the quantized kernels instead of the
|
||||
/// dequantize + FP16 GEMMs. `on`: the switch; `fp16_path`: the layer takes the FP16 path (not MMQ, not the fused kernels);
|
||||
/// `native_pack`: the layer's experts are GGUF blocks in a native pack (a Strata pack's blob has another layout and no
|
||||
/// grouped-kernel layout here); `apr`: the pack carries APR (Hadamard-folded) gate/up or down weights, which the FP16 path
|
||||
/// rotates itself (hadamard_rows_f16) and this route does not.
|
||||
inline bool fewrows_takes(bool on, bool fp16_path, bool native_pack, bool apr, int64_t rows) {
|
||||
return on && fp16_path && native_pack && !apr && rows >= 1 && rows <= (int64_t) kFewRows;
|
||||
}
|
||||
|
||||
} // namespace strata::prefill
|
||||
@@ -105,6 +105,9 @@ void copy_i32(int32_t* dst, const int32_t* src, int64_t n, void* stream);
|
||||
/// dst[0, n) = src[0, n) in 16-byte loads (both 16-byte aligned; src may be mapped host memory: read over PCIe by
|
||||
/// the kernel, not queued on a copy engine).
|
||||
void copy_f32_wide(float* dst, const float* src, int64_t n, void* stream);
|
||||
/// Few-row experts (fewrows.hpp): writes one expert's native_expert_grouped plan on the device, so no host buffer is
|
||||
/// involved: rec = {u64 blob, i32 start[2] = {0, ne}, i32 n_groups = 1, i32 pad, i32 dst[ne] = o0 + i}. 1 <= ne <= 32.
|
||||
void fewrows_record(void* rec, const void* blob, int ne, int o0, void* stream);
|
||||
/// Gather rows: dst16[i, :] = x16[src[i], :] (n rows of `width` BF16).
|
||||
void gather_rows16(const uint16_t* x16, const int32_t* src, uint16_t* dst16, int64_t n, int64_t width, void* stream);
|
||||
/// bo[t, :] = shared[t, :] * sigmoid(sg[t]) + sum_k w[t, k] * D[slot[t, k], :]
|
||||
|
||||
@@ -3222,6 +3222,22 @@ void blob_dequant_f16(const uint8_t* blob, uint16_t* gu16, uint16_t* down16, voi
|
||||
blob_dequant_kernel<true><<<blocks_for((int64_t) BLOB_GU_ROWS * BLOB_ROW_GU + (int64_t) N * BLOB_ROW_D), 256, 0, (cudaStream_t) stream>>>(blob, gu16, down16);
|
||||
check("blob_dequant_f16");
|
||||
}
|
||||
__global__ void fewrows_record_kernel(uint8_t* rec, unsigned long long blob, int ne, int o0) {
|
||||
const int i = threadIdx.x;
|
||||
int32_t* r = reinterpret_cast<int32_t*>(rec + 8);
|
||||
if (i == 0) {
|
||||
*reinterpret_cast<unsigned long long*>(rec) = blob;
|
||||
r[0] = 0;
|
||||
r[1] = ne;
|
||||
r[2] = 1;
|
||||
r[3] = 0;
|
||||
}
|
||||
if (i < ne) r[4 + i] = o0 + i;
|
||||
}
|
||||
void fewrows_record(void* rec, const void* blob, int ne, int o0, void* stream) {
|
||||
fewrows_record_kernel<<<1, 32, 0, (cudaStream_t) stream>>>((uint8_t*) rec, (unsigned long long) (uintptr_t) blob, ne, o0);
|
||||
check("fewrows_record");
|
||||
}
|
||||
void swiglu_interleaved(const float* gu, uint16_t* h16, int64_t n, void* stream) {
|
||||
if (n <= 0) return;
|
||||
swiglu_il_kernel<<<blocks_for(n * FFC), 256, 0, (cudaStream_t) stream>>>(gu, h16, n);
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "strata/artifact/gguf_reader.hpp"
|
||||
#include "strata/prefill/pf_fused_override.hpp"
|
||||
#include "strata/prefill/staged_group_rule.hpp"
|
||||
#include "strata/prefill/fewrows.hpp"
|
||||
#if defined(STRATA_HAVE_DFLASH2)
|
||||
#include "strata/kernels/dflash2.hpp"
|
||||
#endif
|
||||
@@ -1296,6 +1297,12 @@ struct Prefill::Impl {
|
||||
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;
|
||||
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
|
||||
@@ -1482,6 +1489,9 @@ void Prefill::release() {
|
||||
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_->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);
|
||||
@@ -5953,6 +5963,9 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
// 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"), false);
|
||||
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
|
||||
@@ -6170,6 +6183,40 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
}
|
||||
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
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
// Few-row experts on the FP16 prompt path (fewrows.hpp): the research switch STRATA_PF_FEWROWS and the rule that picks the
|
||||
// experts. Header-only, no GPU.
|
||||
#include <cstdio>
|
||||
|
||||
#include "strata/prefill/fewrows.hpp"
|
||||
|
||||
using strata::prefill::fewrows_switch;
|
||||
using strata::prefill::fewrows_takes;
|
||||
using strata::prefill::kFewRows;
|
||||
using strata::prefill::kFewRowsRecBytes;
|
||||
|
||||
static int failed = 0;
|
||||
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
|
||||
|
||||
int main() {
|
||||
// the switch: unset or empty = the default the caller passes; "0" off; anything else on
|
||||
CHECK(!fewrows_switch(nullptr, false));
|
||||
CHECK(fewrows_switch(nullptr, true));
|
||||
CHECK(!fewrows_switch("", false));
|
||||
CHECK(fewrows_switch("", true));
|
||||
CHECK(!fewrows_switch("0", true));
|
||||
CHECK(!fewrows_switch("0", false));
|
||||
CHECK(fewrows_switch("1", false));
|
||||
CHECK(fewrows_switch("12", false)); // a number is not a row count any more: any non-zero value is "on"
|
||||
CHECK(fewrows_switch("on", false));
|
||||
|
||||
// the constant and the plan record: 12 rows, 24 bytes of header + one int32 destination per row
|
||||
CHECK(kFewRows == 12);
|
||||
CHECK(kFewRowsRecBytes == 24 + 4 * 12);
|
||||
CHECK(kFewRowsRecBytes % 8 == 0); // the u64 blob pointer at the start of the next record stays aligned
|
||||
|
||||
// the rule: switch on, FP16 path, native pack, no APR, 1..kFewRows rows
|
||||
CHECK(fewrows_takes(true, true, true, false, 1));
|
||||
CHECK(fewrows_takes(true, true, true, false, kFewRows));
|
||||
CHECK(!fewrows_takes(true, true, true, false, 0)); // an expert with no rows is skipped elsewhere
|
||||
CHECK(!fewrows_takes(true, true, true, false, kFewRows + 1)); // 13 rows and up: dequantize + GEMMs
|
||||
CHECK(!fewrows_takes(true, true, true, false, 600));
|
||||
CHECK(!fewrows_takes(false, true, true, false, 6)); // switch off
|
||||
CHECK(!fewrows_takes(true, false, true, false, 6)); // MMQ or the fused kernels take the layer
|
||||
CHECK(!fewrows_takes(true, true, false, false, 6)); // a Strata pack's blob
|
||||
CHECK(!fewrows_takes(true, true, true, true, 6)); // APR: the FP16 path rotates itself
|
||||
CHECK(!fewrows_takes(true, true, true, false, -1));
|
||||
|
||||
if (failed) {
|
||||
std::fprintf(stderr, "fewrows_rule_test: %d failed\n", failed);
|
||||
return 1;
|
||||
}
|
||||
std::printf("fewrows_rule_test: ok\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user