prefill: quantize each token's MoE input once and scatter it to its k rows (the same bytes)
mmq::quantize(m.mixed, m.src_dev, ...) read and quantized every token's row of `mixed` once per routed expert (K =
10). mmq::quantize_scatter uses llama.cpp's quantize_scatter_mmq_q8_1_cuda (the pinned quantize.cu strata_mmq
already compiles): each token quantized once, its block written to all of its rows through slot_dev, the inverse of
src_dev. The same q8_1 bytes, so the default; STRATA_QUANT_GATHER=1 keeps the gather. No new device code (CUDA and
HIP compile the same kernels); SYCL's prefill.cpp untouched.
prefill_mmq_scatter_test (new ctest): every MMQ q8_1 layout (D4, DS4, D2S6), T = 1..2099: 0 differing bytes. IQ2_XS
600 / 2K / 32K and Q2_0 2K against e8ca9afd: identical first-token logits and 32 greedy tokens. RTX 5090 (96 MB L2):
the quantizer 2x at 2K, even at 8K (the chunk's input fits in L2), 1.4-1.9x at 32K; a 32K prompt with
--prefill auto:32768: 77.5 -> 58.1 ms of quantization.
Co-authored-by: sergiywith <sergiywith@gmail.com>
This commit is contained in:
1 parent
b6ccf873b8
commit
c675f5b787
5 files changed
+183
-2
No files matched your search
@@ -1542,6 +1542,12 @@ if(STRATA_NATIVE_EXPERTS)
|
||||
target_link_libraries(prefill_fused_iq_test PRIVATE strata_mmq ggml-base ${_strata_gpu_runtime_target})
|
||||
add_test(NAME prefill_fused_iq_test COMMAND prefill_fused_iq_test)
|
||||
set_tests_properties(prefill_fused_iq_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600)
|
||||
# the MoE input quantized per token and scattered to its rows (mmq::quantize_scatter) against the per-row
|
||||
# gather: the same q8_1 bytes for every MMQ layout (--bench: both at 2K, 8K and 32K tokens)
|
||||
add_executable(prefill_mmq_scatter_test tests/cuda/prefill_mmq_scatter_test.cpp)
|
||||
target_link_libraries(prefill_mmq_scatter_test PRIVATE strata_mmq ggml-base ${_strata_gpu_runtime_target})
|
||||
add_test(NAME prefill_mmq_scatter_test COMMAND prefill_mmq_scatter_test)
|
||||
set_tests_properties(prefill_mmq_scatter_test PROPERTIES TIMEOUT 120)
|
||||
endif()
|
||||
if(STRATA_BUILD_TESTS AND STRATA_MMQ_KQUANTS)
|
||||
# the UD-Q4_K_XL expert formats through MMQ against ggml's dequantized weights (synthetic, a few seconds)
|
||||
|
||||
@@ -46,6 +46,12 @@ void scale_gu_rows(float* gu, int64_t ld, int64_t n_ff, const int32_t* bounds, i
|
||||
/// The down product's rows [0, nrows) (`ld` floats each, relative `bounds`) times their expert's s_down - on the FP32
|
||||
/// output, not folded into up, where the hidden would sit near 1e-5 (FP16-subnormal block scales in q8 formats).
|
||||
void scale_down_rows(float* d, int64_t ld, const int32_t* bounds, int n, const float* tails, int64_t nrows, void* stream);
|
||||
/// The rows quantize(x, src, ...) writes for tokens x k_used rows in expert order, written token by token: token t's
|
||||
/// row of x is quantized once and stored at rows slot[t * k_used + k] (`slot` the inverse of `src`) - the same bytes,
|
||||
/// a k_used-th of the quantizations (llama.cpp's quantize_scatter_mmq_q8_1_cuda). STRATA_QUANT_GATHER=1: quantize()
|
||||
/// over `src`, as before.
|
||||
void quantize_scatter(const float* x, const int32_t* slot, const int32_t* src, void* xq, int ggml_type, int64_t cols,
|
||||
int64_t ld, int64_t tokens, int k_used, void* stream);
|
||||
|
||||
/// One launch over n experts whose weights lie `expert_bytes` apart from `w`: for expert e, the activation rows
|
||||
/// [bounds[e], bounds[e+1]) of `xq` (bounds on the device, n+1 entries) times its [w_rows, w_cols] matrix into
|
||||
|
||||
@@ -324,6 +324,19 @@ void quantize(const float* x, const int32_t* ids, void* xq, int t, int64_t cols,
|
||||
ck(cudaGetLastError(), "quantize");
|
||||
}
|
||||
|
||||
void quantize_scatter(const float* x, const int32_t* slot, const int32_t* src, void* xq, int t, int64_t cols, int64_t ld,
|
||||
int64_t tokens, int k_used, void* stream) {
|
||||
if (tokens <= 0 || k_used <= 0) return;
|
||||
static const bool gather = [] { const char* e = std::getenv("STRATA_QUANT_GATHER"); return e != nullptr && std::atoi(e) != 0; }();
|
||||
if (gather) {
|
||||
quantize(x, src, xq, t, cols, ld, tokens * k_used, stream);
|
||||
return;
|
||||
}
|
||||
quantize_scatter_mmq_q8_1_cuda(x, slot, xq, (ggml_type) t, cols, ld, pad512(cols), tokens, tokens * k_used, k_used,
|
||||
(cudaStream_t) stream);
|
||||
ck(cudaGetLastError(), "quantize_scatter");
|
||||
}
|
||||
|
||||
Context::Context() {
|
||||
int dev = 0;
|
||||
cudaGetDevice(&dev);
|
||||
|
||||
@@ -68,6 +68,7 @@ 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*) {}
|
||||
@@ -3453,8 +3454,13 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
const size_t mmq_db = use_mmq ? mmq::matrix_bytes(mmq_dt, N, 640) : 0;
|
||||
pt.mark(kPfGather, cs);
|
||||
if (use_mmq) {
|
||||
// step 2b: the layer's activations as q8_1 rows in expert order, straight from `mixed`
|
||||
mmq::quantize(m.mixed, m.src_dev, m.Xq, mmq_gt, N, N, T * K, m.cs, m.Xscale);
|
||||
// step 2b: the layer's activations as q8_1 rows in expert order, straight from `mixed`:
|
||||
// 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 (mmq::fp4_activations(mmq_gt))
|
||||
mmq::quantize(m.mixed, m.src_dev, m.Xq, mmq_gt, N, N, T * K, m.cs, m.Xscale);
|
||||
else
|
||||
mmq::quantize_scatter(m.mixed, 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;
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
// prefill_mmq_scatter_test - the prompt path's MoE input quantized per token and scattered to its k rows
|
||||
// (mmq::quantize_scatter) against the per-row gather (mmq::quantize over the expert-order source rows): the same
|
||||
// q8_1 bytes, the whole buffer, for every q8_1 layout MMQ uses (D4: Q2_0 Q8_0 IQ2_XS, DS4: Q4_1, D2S6: Q2_K), on a
|
||||
// routing built the way Prefill builds it (10 of 512 experts a token, rows grouped by expert), with an all-zero
|
||||
// token and token counts that are not multiples of anything. --bench: both at 2K, 8K and 32K tokens.
|
||||
#include "strata/prefill/moe_mmq.hpp"
|
||||
|
||||
#include "ggml.h"
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <random>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
namespace mmq = strata::prefill::mmq;
|
||||
|
||||
void ck(cudaError_t e, const char* what) {
|
||||
if (e != cudaSuccess) throw std::runtime_error(std::string(what) + ": " + cudaGetErrorString(e));
|
||||
}
|
||||
|
||||
struct Dev {
|
||||
void* p = nullptr;
|
||||
explicit Dev(size_t n) { ck(cudaMalloc(&p, n), "cudaMalloc"); }
|
||||
~Dev() { cudaFree(p); }
|
||||
Dev(const Dev&) = delete;
|
||||
Dev& operator=(const Dev&) = delete;
|
||||
};
|
||||
|
||||
constexpr int kExperts = 512, kUsed = 10;
|
||||
constexpr int64_t kCols = 2560;
|
||||
|
||||
// Prefill's maps: slot[t * k + j] = the token's row in expert order, src[row] = the token
|
||||
void routing(int64_t T, std::mt19937& rng, std::vector<int32_t>& slot, std::vector<int32_t>& src) {
|
||||
std::vector<int32_t> ids((size_t) (T * kUsed));
|
||||
std::vector<int32_t> pick(kExperts);
|
||||
for (int e = 0; e < kExperts; ++e) pick[(size_t) e] = e;
|
||||
for (int64_t t = 0; t < T; ++t) {
|
||||
for (int j = 0; j < kUsed; ++j) std::swap(pick[(size_t) j], pick[(size_t) (j + rng() % (kExperts - j))]);
|
||||
for (int j = 0; j < kUsed; ++j) ids[(size_t) (t * kUsed + j)] = pick[(size_t) j];
|
||||
}
|
||||
std::vector<int32_t> cnt(kExperts, 0), fill(kExperts, 0);
|
||||
for (int32_t e : ids) ++cnt[(size_t) e];
|
||||
for (int e = 1; e < kExperts; ++e) fill[(size_t) e] = fill[(size_t) e - 1] + cnt[(size_t) e - 1];
|
||||
slot.assign(ids.size(), 0);
|
||||
src.assign(ids.size(), 0);
|
||||
for (size_t i = 0; i < ids.size(); ++i) {
|
||||
const int32_t p = fill[(size_t) ids[i]]++;
|
||||
slot[i] = p;
|
||||
src[(size_t) p] = (int32_t) (i / kUsed);
|
||||
}
|
||||
}
|
||||
|
||||
int check(ggml_type t, int64_t T, uint32_t seed) {
|
||||
std::mt19937 rng(seed);
|
||||
std::vector<int32_t> slot, src;
|
||||
routing(T, rng, slot, src);
|
||||
std::normal_distribution<float> nd(0.0f, 1.0f);
|
||||
std::vector<float> x((size_t) (T * kCols));
|
||||
for (float& v : x) v = nd(rng) * 0.7f;
|
||||
if (T > 3) std::fill(x.begin() + 2 * kCols, x.begin() + 3 * kCols, 0.0f); // an all-zero token
|
||||
const int64_t rows = T * kUsed;
|
||||
const size_t qb = mmq::q8_bytes(rows, kCols);
|
||||
Dev dx(x.size() * 4), dslot(slot.size() * 4), dsrc(src.size() * 4), qa(qb), qs(qb);
|
||||
ck(cudaMemcpy(dx.p, x.data(), x.size() * 4, cudaMemcpyHostToDevice), "x");
|
||||
ck(cudaMemcpy(dslot.p, slot.data(), slot.size() * 4, cudaMemcpyHostToDevice), "slot");
|
||||
ck(cudaMemcpy(dsrc.p, src.data(), src.size() * 4, cudaMemcpyHostToDevice), "src");
|
||||
ck(cudaMemset(qa.p, 0xA5, qb), "memset");
|
||||
ck(cudaMemset(qs.p, 0xA5, qb), "memset");
|
||||
mmq::quantize((const float*) dx.p, (const int32_t*) dsrc.p, qa.p, (int) t, kCols, kCols, rows, nullptr);
|
||||
mmq::quantize_scatter((const float*) dx.p, (const int32_t*) dslot.p, (const int32_t*) dsrc.p, qs.p, (int) t, kCols,
|
||||
kCols, T, kUsed, nullptr);
|
||||
ck(cudaDeviceSynchronize(), "quantize");
|
||||
std::vector<uint8_t> a(qb), b(qb);
|
||||
ck(cudaMemcpy(a.data(), qa.p, qb, cudaMemcpyDeviceToHost), "back");
|
||||
ck(cudaMemcpy(b.data(), qs.p, qb, cudaMemcpyDeviceToHost), "back");
|
||||
size_t diff = 0;
|
||||
for (size_t i = 0; i < qb; ++i) diff += a[i] != b[i];
|
||||
std::printf("%s %-7s T=%-5lld %zu of %zu bytes differ\n", diff ? "FAIL" : "ok ", ggml_type_name(t), (long long) T,
|
||||
diff, qb);
|
||||
return diff ? 1 : 0;
|
||||
}
|
||||
|
||||
void bench(ggml_type t, int64_t T) {
|
||||
std::mt19937 rng(7);
|
||||
std::vector<int32_t> slot, src;
|
||||
routing(T, rng, slot, src);
|
||||
std::vector<float> x((size_t) (T * kCols), 0.25f);
|
||||
const int64_t rows = T * kUsed;
|
||||
const size_t qb = mmq::q8_bytes(rows, kCols);
|
||||
Dev dx(x.size() * 4), dslot(slot.size() * 4), dsrc(src.size() * 4), q(qb);
|
||||
ck(cudaMemcpy(dx.p, x.data(), x.size() * 4, cudaMemcpyHostToDevice), "x");
|
||||
ck(cudaMemcpy(dslot.p, slot.data(), slot.size() * 4, cudaMemcpyHostToDevice), "slot");
|
||||
ck(cudaMemcpy(dsrc.p, src.data(), src.size() * 4, cudaMemcpyHostToDevice), "src");
|
||||
cudaEvent_t e0, e1;
|
||||
ck(cudaEventCreate(&e0), "event");
|
||||
ck(cudaEventCreate(&e1), "event");
|
||||
std::vector<float> tg, ts;
|
||||
for (int i = 0; i < 23; ++i)
|
||||
for (int v = 0; v < 2; ++v) {
|
||||
cudaEventRecord(e0);
|
||||
if (v == 0) mmq::quantize((const float*) dx.p, (const int32_t*) dsrc.p, q.p, (int) t, kCols, kCols, rows, nullptr);
|
||||
else mmq::quantize_scatter((const float*) dx.p, (const int32_t*) dslot.p, (const int32_t*) dsrc.p, q.p, (int) t,
|
||||
kCols, kCols, T, kUsed, nullptr);
|
||||
cudaEventRecord(e1);
|
||||
ck(cudaEventSynchronize(e1), "bench");
|
||||
float ms = 0.0f;
|
||||
cudaEventElapsedTime(&ms, e0, e1);
|
||||
if (i >= 3) (v == 0 ? tg : ts).push_back(ms);
|
||||
}
|
||||
std::sort(tg.begin(), tg.end());
|
||||
std::sort(ts.begin(), ts.end());
|
||||
const float g = tg[tg.size() / 2], s = ts[ts.size() / 2];
|
||||
std::printf(" %-7s T=%lld x %d rows: gather %.3f ms, scatter %.3f ms (%.2fx)\n", ggml_type_name(t), (long long) T,
|
||||
kUsed, g, s, g / s);
|
||||
cudaEventDestroy(e0);
|
||||
cudaEventDestroy(e1);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
const bool do_bench = argc > 1 && std::strcmp(argv[1], "--bench") == 0;
|
||||
int fails = 0;
|
||||
try {
|
||||
const ggml_type types[] = {GGML_TYPE_Q2_0, GGML_TYPE_Q8_0, GGML_TYPE_IQ2_XS, GGML_TYPE_Q4_1, GGML_TYPE_Q2_K};
|
||||
uint32_t seed = 11;
|
||||
for (ggml_type t : types)
|
||||
for (int64_t T : {1, 2, 7, 600, 2099}) fails += check(t, T, seed++);
|
||||
if (do_bench) {
|
||||
std::printf("--bench (median of 20):\n");
|
||||
for (ggml_type t : {GGML_TYPE_IQ2_XS, GGML_TYPE_Q2_0}) {
|
||||
bench(t, 2048);
|
||||
bench(t, 8192);
|
||||
bench(t, 32768);
|
||||
}
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::printf("FAIL %s\n", e.what());
|
||||
return 1;
|
||||
}
|
||||
std::printf("prefill_mmq_scatter_test: %d failures\n", fails);
|
||||
return fails ? 1 : 0;
|
||||
}
|
||||
Reference in new issue
Block a user