cuda: sm_120/121/89/86 Wave-2+3 decode/prefill multipliers, partial-cache fix, and defaults

This commit is contained in:
Niko1221 committed 2026-11-22 02:27:43 +01:00
1 parent ce2a51b2eb
commit a2ca43e53b
17 files changed
+1934 -237

No files matched your search

+5
View File
@@ -279,6 +279,11 @@ private:
int launch_n_ = 0;
int64_t launch_pos0_ = 0;
std::chrono::steady_clock::time_point launch_t0_{};
// Lazy batched context scratch (kCtxBatch = 64 rows, ~2.2 MiB) for fast zero-sync prompt context updates
struct CtxBatch;
CtxBatch* ctx_batch_ = nullptr;
bool ensure_ctx_batch(std::string& err);
void free_ctx_batch();
};
} // namespace strata::core
@@ -54,6 +54,8 @@ void df2_attention(const float* q, const float* kring, const float* vring, const
/// df2_rope_table with the inverse frequencies computed once (df2_rope_inv: the same double-precision pow per i), so a round pays no pow: the same table, bitwise.
void df2_rope_inv(float theta, double* inv, void* stream);
void df2_rope_table_inv(const int32_t* pos, int rows, const double* inv, float* table, void* stream);
/// Fill pos[i] = pos0 + i for i in 0..rows-1 on the device (async batched context updates without host sync).
void df2_iota_pos(int32_t* pos, int32_t pos0, int rows, void* stream);
/// The same table computed on the host (double precision, bitwise the CUDA kernel up to sincosf): table [rows][2][64]. The SYCL port uses this one: a kernel with FP64 operations on an Arc Alchemist (IGC DP emulation) slows every later kernel of the process by 2x or more, so the drafter has no device FP64.
void df2_rope_table_cpu(const int32_t* pos, int rows, float theta, float* table);
void df2_gemm_variant(int v);
@@ -39,6 +39,7 @@ struct NativeSharedWeights {
const void* down_data = nullptr;
void* q8_1 = nullptr;
const void* x_q8_1 = nullptr; // optional pre-quantized Q8_1 input for gate/up in shared_expert_multi
void* q8_1_il = nullptr; // optional interleaved Q8_1 scratch for native_mmvq_il (bit-exact!)
};
/// Bytes of caller-owned scratch `shared_expert` needs. **THE KERNEL USED TO `cudaMalloc` FOUR BUFFERS ON
+39 -27
View File
@@ -69,75 +69,87 @@ int main() {
CHECK(!adapt_fetch_default_card(10, 0, 16 * GiB));
CHECK(!adapt_fetch_default_card(6, 0, 16 * GiB)); // P100 16 GB
// the table: the sm_120 entries are unchanged, the fifth is the switch, and only for a card the rule takes
auto unset_sm120 = [] {
for (const char* k : {
"STRATA_ADAPT_FETCH", "STRATA_VERIFY_DEVICE_HITS", "STRATA_L2_PF", "STRATA_HC_UPMIX",
"STRATA_Q80_ROWS1", "STRATA_EXP_MODE", "STRATA_SIDE_MIXER", "STRATA_DEQUANT_SIDE",
"STRATA_QFUSE", "STRATA_LFUSE", "STRATA_PLE_COMPACT", "STRATA_PLE_COPY_STREAM",
"STRATA_PF_FUSED_KQ", "STRATA_HC_Q8", "STRATA_PREFILL_CPU_SHARE_NT", "STRATA_PREFILL_NVFP4"}) {
unset(k);
}
};
// the table: the sm_120 entries (14 base), + ADAPT_FETCH for <= 16 GiB or + VERIFY_DEVICE_HITS for > 16 GiB
{
const auto t = nv_default_env(12, 0, false, 12 * GiB);
CHECK(t.size() == 5 && has(t, "STRATA_ADAPT_FETCH", "2") && has(t, "STRATA_HC_UPMIX", "1") && has(t, "STRATA_EXP_MODE", "8"));
CHECK(nv_default_env(12, 0, false, 16 * GiB).size() == 5);
CHECK(nv_default_env(12, 0, false, 32 * GiB).size() == 4 && lacks(nv_default_env(12, 0, false, 32 * GiB), "STRATA_ADAPT_FETCH"));
CHECK(nv_default_env(12, 0, false, 0).size() == 4); // no memory given: the table of 0.2.16 + BW-A / BW-G
CHECK(nv_default_env(12, 0, false).size() == 4);
CHECK(t.size() == 15 && has(t, "STRATA_ADAPT_FETCH", "2") && has(t, "STRATA_HC_UPMIX", "1") && has(t, "STRATA_EXP_MODE", "8"));
CHECK(nv_default_env(12, 0, false, 16 * GiB).size() == 15);
CHECK(nv_default_env(12, 0, false, 32 * GiB).size() == 15 &&
has(nv_default_env(12, 0, false, 32 * GiB), "STRATA_VERIFY_DEVICE_HITS", "1") &&
lacks(nv_default_env(12, 0, false, 32 * GiB), "STRATA_ADAPT_FETCH"));
CHECK(nv_default_env(12, 0, false, 0).size() == 14); // no memory given: the base sm_120 table
CHECK(nv_default_env(12, 0, false).size() == 14);
CHECK(nv_default_env(12, 0, true, 12 * GiB).empty()); // STRATA_DEFAULTS=026
put("STRATA_SM120_PERF", "0");
CHECK(nv_default_env(12, 0, false, 12 * GiB).empty()); // STRATA_SM120_PERF=0 leaves the whole table out, the switch with it
unset("STRATA_SM120_PERF");
CHECK(nv_default_env(12, 1, false, 12 * GiB).size() == 7); // a 12.1 part: only if it reported 16 GiB or less (the GB10 reports far more)
CHECK(nv_default_env(12, 1, false, 121 * GiB).size() == 6);
CHECK(nv_default_env(12, 1, false, 12 * GiB).size() == 13); // a 12.1 part: 12 base + ADAPT_FETCH when <= 16 GiB
CHECK(nv_default_env(12, 1, false, 121 * GiB).size() == 13 &&
has(nv_default_env(12, 1, false, 121 * GiB), "STRATA_VERIFY_DEVICE_HITS", "1") &&
lacks(nv_default_env(12, 1, false, 121 * GiB), "STRATA_ADAPT_FETCH"));
}
// no other architecture gets it, whatever its memory
// no other architecture gets ADAPT_FETCH, whatever its memory
CHECK(lacks(nv_default_env(8, 6, false, 12 * GiB), "STRATA_ADAPT_FETCH"));
CHECK(lacks(nv_default_env(8, 9, false, 16 * GiB), "STRATA_ADAPT_FETCH"));
CHECK(lacks(nv_default_env(6, 0, false, 16 * GiB), "STRATA_ADAPT_FETCH"));
CHECK(nv_default_env(8, 9, false, 16 * GiB).empty() && nv_default_env(10, 0, false, 16 * GiB).empty());
CHECK(nv_default_env(10, 0, false, 16 * GiB).empty());
// apply_nv_defaults: sets the environment where the user has not
{
unset("STRATA_ADAPT_FETCH");
unset("STRATA_L2_PF"); unset("STRATA_HC_UPMIX"); unset("STRATA_Q80_ROWS1"); unset("STRATA_EXP_MODE");
unset_sm120();
const auto set = apply_nv_defaults(12, 0, 12 * GiB);
CHECK(in(set, "STRATA_ADAPT_FETCH") && set.size() == 5);
CHECK(in(set, "STRATA_ADAPT_FETCH") && set.size() == 15);
const char* v = val("STRATA_ADAPT_FETCH");
CHECK(v != nullptr && std::string(v) == "2");
CHECK(adapt_fetch_is_default());
}
{ // a card over 16 GiB: the 4 earlier entries only, the variable stays unset, the flag is false
unset("STRATA_ADAPT_FETCH");
unset("STRATA_L2_PF"); unset("STRATA_HC_UPMIX"); unset("STRATA_Q80_ROWS1"); unset("STRATA_EXP_MODE");
{ // a card over 16 GiB: the 14 base entries + VERIFY_DEVICE_HITS, ADAPT_FETCH stays unset, the flag is false
unset_sm120();
const auto set = apply_nv_defaults(12, 0, 32 * GiB);
CHECK(!in(set, "STRATA_ADAPT_FETCH") && set.size() == 4);
CHECK(!in(set, "STRATA_ADAPT_FETCH") && in(set, "STRATA_VERIFY_DEVICE_HITS") && set.size() == 15);
CHECK(val("STRATA_ADAPT_FETCH") == nullptr);
CHECK(!adapt_fetch_is_default());
}
{ // the user's own value wins, 0 included (the way back), and then the flag is false: the value is not the default's
for (const char* mine : {"0", "1", "2"}) {
unset("STRATA_L2_PF"); unset("STRATA_HC_UPMIX"); unset("STRATA_Q80_ROWS1"); unset("STRATA_EXP_MODE");
unset_sm120();
put("STRATA_ADAPT_FETCH", mine);
const auto set = apply_nv_defaults(12, 0, 12 * GiB);
CHECK(!in(set, "STRATA_ADAPT_FETCH") && set.size() == 4);
CHECK(!in(set, "STRATA_ADAPT_FETCH") && set.size() == 14);
const char* v = val("STRATA_ADAPT_FETCH");
CHECK(v != nullptr && std::string(v) == mine);
CHECK(!adapt_fetch_is_default());
}
unset("STRATA_ADAPT_FETCH");
unset_sm120();
}
{ // STRATA_SM120_PERF=0: nothing of the table is set, so no switch either
unset("STRATA_ADAPT_FETCH");
unset_sm120();
put("STRATA_SM120_PERF", "0");
const auto set = apply_nv_defaults(12, 0, 12 * GiB);
CHECK(set.empty() && val("STRATA_ADAPT_FETCH") == nullptr && !adapt_fetch_is_default());
unset("STRATA_SM120_PERF");
}
{ // an older card with 12 GiB: the architecture decides, not the memory
unset("STRATA_ADAPT_FETCH");
unset_sm120();
const auto set = apply_nv_defaults(8, 6, 12 * GiB);
CHECK(!in(set, "STRATA_ADAPT_FETCH") && val("STRATA_ADAPT_FETCH") == nullptr && !adapt_fetch_is_default());
}
{ // no memory given (the two-argument call of the other callers): as before
unset("STRATA_ADAPT_FETCH");
unset("STRATA_L2_PF"); unset("STRATA_HC_UPMIX"); unset("STRATA_Q80_ROWS1"); unset("STRATA_EXP_MODE");
{ // no memory given (the two-argument call of the other callers): as before (14 base entries)
unset_sm120();
const auto set = apply_nv_defaults(12, 0);
CHECK(set.size() == 4 && val("STRATA_ADAPT_FETCH") == nullptr && !adapt_fetch_is_default());
CHECK(set.size() == 14 && val("STRATA_ADAPT_FETCH") == nullptr && !adapt_fetch_is_default());
}
unset("STRATA_ADAPT_FETCH");
unset_sm120();
if (fails == 0) std::printf("adapt_fetch_default_test: ok\n");
return fails == 0 ? 0 : 1;
}
+141
View File
@@ -64,6 +64,66 @@ Dflash2Drafter::~Dflash2Drafter() {
for (cudaEvent_t e : ev_ctx_) if (e) cudaEventDestroy(e);
if (ev_a_) cudaEventDestroy(ev_a_);
if (ev_b_) cudaEventDestroy(ev_b_);
free_ctx_batch();
}
struct Dflash2Drafter::CtxBatch {
static constexpr int kB = 64;
int32_t* pos = nullptr;
float* rt = nullptr;
uint8_t* xqc = nullptr;
float* xdc = nullptr;
float* fcy = nullptr;
float* kc = nullptr;
float* vc = nullptr;
int8_t* h_logq[2] = {};
float* h_logd[2] = {};
cudaEvent_t ev[2] = {};
};
void Dflash2Drafter::free_ctx_batch() {
if (!ctx_batch_) return;
auto* cb = ctx_batch_;
ctx_batch_ = nullptr;
auto fr = [](void* p) { if (p) cudaFree(p); };
fr(cb->pos); fr(cb->rt); fr(cb->xqc); fr(cb->xdc); fr(cb->fcy); fr(cb->kc); fr(cb->vc);
for (int i = 0; i < 2; ++i) {
if (cb->h_logq[i]) cudaFreeHost(cb->h_logq[i]);
if (cb->h_logd[i]) cudaFreeHost(cb->h_logd[i]);
if (cb->ev[i]) cudaEventDestroy(cb->ev[i]);
}
delete cb;
}
bool Dflash2Drafter::ensure_ctx_batch(std::string& err) {
if (ctx_batch_) return true;
auto* cb = new CtxBatch();
constexpr int B = CtxBatch::kB;
auto dev = [&](auto** p, size_t n) {
using T = std::remove_pointer_t<std::remove_reference_t<decltype(*p)>>;
return cudaMalloc((void**) p, n * sizeof(T)) == cudaSuccess;
};
const size_t tap_n = std::max<size_t>(taps_.size(), 1) * (size_t) N_;
bool ok = dev(&cb->pos, B) && dev(&cb->rt, (size_t) B * 128) &&
dev(&cb->xqc, (size_t) B * tap_n + 4096) && dev(&cb->xdc, (size_t) B * (tap_n / 32) + 64) &&
dev(&cb->fcy, (size_t) B * N_) && dev(&cb->kc, (size_t) B * NKV_ * HD_) &&
dev(&cb->vc, (size_t) B * NKV_ * HD_);
for (int i = 0; ok && i < 2; ++i) {
ok = cudaEventCreateWithFlags(&cb->ev[i], cudaEventDisableTiming) == cudaSuccess;
if (ok && log_on_) {
ok = cudaHostAlloc((void**) &cb->h_logq[i], (size_t) B * N_, cudaHostAllocDefault) == cudaSuccess &&
cudaHostAlloc((void**) &cb->h_logd[i], (size_t) B * (N_ / 32) * sizeof(float), cudaHostAllocDefault) == cudaSuccess;
}
}
if (!ok) {
(void) cudaGetLastError();
ctx_batch_ = cb;
free_ctx_batch();
(void) err;
return false;
}
ctx_batch_ = cb;
return true;
}
// STRATA_DF2_PROFILE=1 (measurement only): the round runs without its graph and an event follows every stage; the per-stage averages print at exit
@@ -971,6 +1031,87 @@ bool Dflash2Drafter::context(const float* taps, int64_t n, int64_t pos0, std::st
if (!context_wait(err)) return false;
const OnDevice on_device(device_);
const int tap_n = (int) taps_.size() * N_;
static const bool batch_on = [] {
const char* e = std::getenv("STRATA_DF2_CTX_BATCH");
return !(e && e[0] == '0');
}();
if (batch_on && n > kRows && prof_ == nullptr && ensure_ctx_batch(err)) {
auto* cb = ctx_batch_;
constexpr int B = CtxBatch::kB;
h_meta_[18] = epoch_;
std::atomic_thread_fence(std::memory_order_seq_cst);
kernels::copy_i32_from_mapped(d_meta_, m_meta_, 32, cs_);
bool slot_pend[2] = {false, false};
int64_t slot_p0[2] = {-1, -1};
int slot_cnt[2] = {0, 0};
auto flush_slot = [&](int s) -> bool {
if (!slot_pend[s]) return true;
slot_pend[s] = false;
if (cudaEventSynchronize(cb->ev[s]) != cudaSuccess) {
err = std::string("dflash2: ") + cudaGetErrorString(cudaGetLastError());
return false;
}
if (log_on_) {
for (int i = 0; i < slot_cnt[s]; ++i) {
const int64_t pos = slot_p0[s] + i;
LogChunk& ch = cur_.writable((size_t) (pos / kLogChunk));
const size_t r = (size_t) (pos % kLogChunk);
std::memcpy(&ch.q[r * 2560], cb->h_logq[s] + (size_t) i * 2560, 2560);
std::memcpy(&ch.d[r * 80], cb->h_logd[s] + (size_t) i * 80, 80 * sizeof(float));
ch.ok[r] = 1;
}
}
return true;
};
try {
int b_idx = 0;
for (int64_t r0 = 0; r0 < n; r0 += B, ++b_idx) {
const int m = (int) std::min<int64_t>(B, n - r0);
const bool need_kv = (r0 + m) > (n - kRing);
if (!log_on_ && !need_kv) continue;
const int s = b_idx & 1;
if (log_on_ && !flush_slot(s)) return false;
kernels::df2_iota_pos(cb->pos, (int32_t) (pos0 + r0), m, cs_);
kernels::df2_quant_rows(taps + (size_t) r0 * (size_t) tap_n, tap_n, m, cb->xqc, cb->xdc, cs_);
gemm({"fc.weight"}, {cb->fcy}, m, cs_, cb->xqc, cb->xdc);
kernels::df2_rms_quant(cb->fcy, f32("hidden_norm.weight"), nullptr, m, N_, eps_, cb->xqc, cb->xdc, cs_);
if (log_on_) {
cudaMemcpyAsync(cb->h_logq[s], cb->xqc, (size_t) m * N_, cudaMemcpyDeviceToHost, cs_);
cudaMemcpyAsync(cb->h_logd[s], cb->xdc, (size_t) m * (N_ / 32) * sizeof(float), cudaMemcpyDeviceToHost, cs_);
cudaEventRecord(cb->ev[s], cs_);
slot_pend[s] = true;
slot_p0[s] = pos0 + r0;
slot_cnt[s] = m;
}
if (need_kv) {
kernels::df2_rope_table_inv(cb->pos, m, rope_inv_, cb->rt, cs_);
for (int l = 0; l < L_; ++l) {
const std::string p = "layers." + std::to_string(l) + ".";
const std::string kp = p + "self_attn.k_proj.weight", vp = p + "self_attn.v_proj.weight";
gemm({kp.c_str(), vp.c_str()}, {cb->kc, cb->vc}, m, cs_, cb->xqc, cb->xdc);
kernels::df2_kv_ring(cb->kc, cb->vc, f32(p + "self_attn.k_norm.weight"), cb->pos, cb->rt,
kring_[l], vring_[l], kRing, m, NKV_, eps_, cs_, tag_, d_meta_ + 18);
}
for (int i = 0; i < m; ++i)
slot_pos_[(size_t) ((pos0 + r0 + i) % kRing)] = ((int64_t) epoch_ << 32) | (pos0 + r0 + i);
}
}
} catch (const std::exception& e) {
err = e.what();
return false;
}
if (cudaStreamSynchronize(cs_) != cudaSuccess) {
err = std::string("dflash2: ") + cudaGetErrorString(cudaGetLastError());
return false;
}
if (log_on_ && (!flush_slot(0) || !flush_slot(1))) return false;
have_ctx_ = std::max<int64_t>(have_ctx_, pos0 + n);
ring_hi_ = std::max<int64_t>(ring_hi_, pos0 + n);
return true;
}
for (int64_t r0 = 0; r0 < n; r0 += kRows) {
const int m = (int) std::min<int64_t>(kRows, n - r0);
for (int i = 0; i < m; ++i) h_meta_[i] = (int32_t) (pos0 + r0 + i);
+11 -5
View File
@@ -5563,9 +5563,13 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32
}
}
static const bool dec_batch = [] { const char* v = std::getenv("STRATA_DEC_BATCH"); return v == nullptr || std::atoi(v) != 0; }();
// the GPU's copy_rows_from_mapped zeroes the rows it computed itself (dec_batch); STRATA_VERIFY_DEVICE_PLAN's
// copy_or_zero_from_mapped copies every row, so there the host still zeroes them
static const bool device_plan = [] { const char* v = std::getenv("STRATA_VERIFY_DEVICE_PLAN"); return v != nullptr && std::atoi(v) != 0; }();
// the GPU's copy_rows_from_mapped zeroes the rows it computed itself (dec_batch); STRATA_VERIFY_DEVICE_PLAN /
// STRATA_VERIFY_DEVICE_HITS's copy_or_zero_from_mapped copies every row, so there the host still zeroes them
static const bool device_plan = [] {
const char* p = std::getenv("STRATA_VERIFY_DEVICE_PLAN");
const char* h = std::getenv("STRATA_VERIFY_DEVICE_HITS");
return (p != nullptr && std::atoi(p) != 0) || (h != nullptr && std::atoi(h) != 0);
}();
const bool gpu_zeroes_hits = (d.plan != nullptr && n <= kMaxWindowEntries && n <= d.plan->cap && dec_batch && !device_plan);
bool any_cpu = false;
for (int64_t i = 0; i < n; ++i)
@@ -7130,8 +7134,10 @@ ForesightSwap* foresight_swap_from_env(int64_t layers, int64_t experts, const st
const char* v = std::getenv("STRATA_FS_SLOTS");
const int slots = v ? std::atoi(v) : 0;
if (slots <= 0) return nullptr;
if (const char* dp = std::getenv("STRATA_VERIFY_DEVICE_PLAN"); dp && std::atoi(dp) != 0) {
std::fprintf(stderr, "strata serve: foresight swap: off beside STRATA_VERIFY_DEVICE_PLAN (the device would plan "
const char* dp = std::getenv("STRATA_VERIFY_DEVICE_PLAN");
const char* dh = std::getenv("STRATA_VERIFY_DEVICE_HITS");
if ((dp && std::atoi(dp) != 0) || (dh && std::atoi(dh) != 0)) {
std::fprintf(stderr, "strata serve: foresight swap: off beside STRATA_VERIFY_DEVICE_PLAN/HITS (the device would plan "
"resident layers without the host)\n");
return nullptr;
}
+42 -13
View File
@@ -54,7 +54,7 @@ std::vector<std::pair<std::string, std::string>> nv_default_env(int major, int m
{"STRATA_QSA_ROWS", "1"}, // -0.5, -1.2
{"STRATA_B6_MMVQ_ROWS", "1"}, // A4000 0, 3060 -1.7
// 0.2.19 sm_86 (RTX 3090 24 GB / A4000 16 GB): bit-exact pipeline/PLE/quant fusions + Q8_0 HC (with sm_80+
// hybrid INT8 TC gr_up_q8_tc saving ~600 MiB VRAM) + fused K-quant prompt kernels.
// 320-CTA INT8 TC gr_up_q8_tc saving ~600 MiB VRAM & -0.62 ms/round) + fused K-quant prompt kernels.
{"STRATA_DEQUANT_SIDE", "1"},
{"STRATA_QFUSE", "1"},
{"STRATA_LFUSE", "1"},
@@ -64,6 +64,26 @@ std::vector<std::pair<std::string, std::string>> nv_default_env(int major, int m
{"STRATA_HC_Q8", "1"},
};
if (main_vram_bytes > kAdaptFetchMaxVramBytes) t.push_back({"STRATA_VERIFY_DEVICE_HITS", "1"});
} else if (mj == 8 && mn == 9) {
// RTX 40 / Ada Lovelace (compute capability 8.9: RTX 4090 / 4080 / 4070 / RTX 6000 Ada).
// STRATA_SM89_PERF=0 and STRATA_DEFAULTS=026 leave it out.
if (off("STRATA_SM89_PERF") || defaults026) return t;
t = {
{"STRATA_HC_UPMIX", "1"},
{"STRATA_L2_PF", "1"},
{"STRATA_Q80_ROWS1", "1"},
{"STRATA_EXP_MODE", "8"},
{"STRATA_SIDE_MIXER", "2"},
{"STRATA_DEQUANT_SIDE", "1"},
{"STRATA_QFUSE", "1"},
{"STRATA_LFUSE", "1"},
{"STRATA_PLE_COMPACT", "1"},
{"STRATA_PLE_COPY_STREAM", "1"},
{"STRATA_PF_FUSED_KQ", "1"},
{"STRATA_HC_Q8", "1"},
{"STRATA_PREFILL_CPU_SHARE_NT", "32"},
};
if (main_vram_bytes > kAdaptFetchMaxVramBytes) t.push_back({"STRATA_VERIFY_DEVICE_HITS", "1"});
} else if (mj == 12 && mn == 0) {
// RTX 50 / PRO Blackwell (compute capability 12.0; a gfx12 HIP card reports 12.0 too, but this table is applied by the CUDA build only). STRATA_SM120_PERF=0 and STRATA_DEFAULTS=026 leave it out.
if (off("STRATA_SM120_PERF") || defaults026) return t;
@@ -80,9 +100,9 @@ std::vector<std::pair<std::string, std::string>> nv_default_env(int major, int m
{"STRATA_Q80_ROWS1", "1"},
// the grouped native experts' mode-8 layout (the GB10 / P100 / P40 default; exact against the CUDA layout on UD-Q4_K_XL)
{"STRATA_EXP_MODE", "8"},
// 0.2.19 sm_120 (RTX 5090 / RTX PRO 5000 Blackwell): exact overlap/PLE/quant fusions + Q8_0 HC (hybrid
// gr_down_q8_fast + INT8 TC gr_up_q8_tc, -0.58 ms/round & ~600 MiB VRAM saved) + fused K-quant prompt kernels
// + wide CPU prefill share backed by pinned cpu_x_stream & parallel native_quant_act.
// 0.2.19 sm_120 (RTX 5090 / RTX PRO 5000 Blackwell): exact overlap/PLE/quant fusions + Q8_0 HC (320-CTA
// INT8 TC gr_up_q8_tc, -0.62 ms/round & ~600 MiB VRAM saved) + fused K-quant prompt kernels + wide CPU
// prefill share + native sm_120a mxf4nvf4 NVFP4 prefill (STRATA_HC_Q8=0 turns Q8_0 HC off).
{"STRATA_SIDE_MIXER", "2"},
{"STRATA_DEQUANT_SIDE", "1"},
{"STRATA_QFUSE", "1"},
@@ -92,6 +112,7 @@ std::vector<std::pair<std::string, std::string>> nv_default_env(int major, int m
{"STRATA_PF_FUSED_KQ", "1"},
{"STRATA_HC_Q8", "1"},
{"STRATA_PREFILL_CPU_SHARE_NT", "32"},
{"STRATA_PREFILL_NVFP4", "w4a4"},
};
// 0.2.17 (owner yes 11-19), a card of 16 GiB or less: the PCIe share also fills the expert cache (not bit-exact: CPU and GPU round differently; the tier is not deterministic).
// BW-9 (2x RTX 5060 Ti 16 GB, UD-Q4_K_XL, DFlash2 spec 6, tail skip OFF, product flags, 3 paired rounds; phaseA-tests/bw/RESULTS-BW-F.md): decode +4 to +43% by kind (medians 1.087 to 1.325, 3 of 3 rounds up),
@@ -128,9 +149,6 @@ std::vector<std::pair<std::string, std::string>> nv_default_env(int major, int m
// text twice byte for byte on both sides (repeat floor 0); prompt speed kq / MMQ, 10 interleaved pairs, median of the per-round ratios 1.10 / 1.26 / 1.18 / 1.53 at 2K / 4K / 8K / 20K (faster in 7 / 8 / 8 / 9 of 10; the rounds scatter 0.5 to 2.0)
// (phaseA-tests/par/RESULTS-SPARK-KQ.md). STRATA_PF_FUSED_KQ=0 is the way back (a switch you set is kept); it is off with the rest of the table by STRATA_SM121_PERF=0.
{"STRATA_PF_FUSED_KQ", "1"},
// (STRATA_PREFILL_NVFP4=w4a4 was an MMQ switch before 0.2.16 removed llama.cpp's MMQ from the tree; leaving it
// here made moe_fused_iq.cu decline NVFP4 layers onto FP16 cuBLAS. Strata's own sm_120a/sm_121a mxf4nvf4 path
// stays opt-in via STRATA_PREFILL_NVFP4=w4a4 until paired GB10 measurements land.)
// the hyper-connection read's fused up projection + mix (gr_upmix). Measured on IQ3_S against the unfused read:
// decode 1.054x [1.007, 1.089], prompt unchanged, and 24 of 24 greedy answers byte-identical (the parity test
// src/prefill/gr_upmix_parity.cu covers it upstream). The HC read is ~13% of a decode round here, which is
@@ -144,9 +162,19 @@ std::vector<std::pair<std::string, std::string>> nv_default_env(int major, int m
// Measured on IQ3_S: decode 1.047x [1.019, 1.079], prompt unchanged, 24 of 24 greedy answers byte-identical.
// It only changes which drafts are attempted, and every draft is verified, so the answers cannot drift.
{"STRATA_SPEC_COST", "1"},
// 0.2.19 sm_121 (DGX Spark GB10 Blackwell): pipeline/PLE/quant fusions + Q8_0 HC (INT8 TC gr_up_q8_tc) +
// native sm_121a mxf4nvf4 NVFP4 prefill (STRATA_SM121_PERF=0 or per-switch =0 turns off).
{"STRATA_DEQUANT_SIDE", "1"},
{"STRATA_QFUSE", "1"},
{"STRATA_LFUSE", "1"},
{"STRATA_PLE_COMPACT", "1"},
{"STRATA_PLE_COPY_STREAM", "1"},
{"STRATA_HC_Q8", "1"},
{"STRATA_PREFILL_NVFP4", "w4a4"},
};
// a 12.1 part reports its unified memory, far above 16 GiB, so this stays off there; the rule is the same as for 12.0
// a 12.1 part reports its unified memory, far above 16 GiB, so ADAPT_FETCH stays off and on-device verify hit planning turns on
if (adapt_fetch_default_card(major, minor, main_vram_bytes)) t.push_back({"STRATA_ADAPT_FETCH", "2"});
else if (main_vram_bytes > kAdaptFetchMaxVramBytes) t.push_back({"STRATA_VERIFY_DEVICE_HITS", "1"});
}
return t;
}
@@ -167,16 +195,17 @@ std::vector<std::string> apply_nv_defaults(int major, int minor, uint64_t main_v
}
adapt_fetch_default_flag() = fetch_set;
if (!set.empty()) {
const bool sm86 = mj == 8;
const bool sm86 = mj == 8 && mn == 6;
const bool sm89 = mj == 8 && mn == 9;
const bool sm121 = mj == 12 && mn == 1;
// sm_86 / sm_60: bit-exact switches. sm_121: the fused prompt path and the FP4-activation NVFP4 prompt round differently from the
// sm_86 / sm_89 / sm_60: bit-exact switches. sm_121: the fused prompt path and the FP4-activation NVFP4 prompt round differently from the
// paths they replace (24 of 24 greedy answers were identical on the Spark; no KL run), so the line does not say "exact".
const bool sm120 = mj == 12 && mn == 0;
// ADAPT_FETCH (a card of 16 GiB or less) is not bit-exact either: the line then says so and names its own way back
std::fprintf(stderr, "strata: %s: %zu %sspeed switches on by default (%s=0 turns them off%s; a switch you set is kept%s): ",
sm86 ? "sm_86 (Ampere)" : sm121 ? "sm_121 (GB10)" : sm120 ? "sm_120 (Blackwell)" : "sm_60 (Pascal)", set.size(), sm121 || fetch_set ? "" : "exact ",
sm86 ? "STRATA_SM86_PERF" : sm121 ? "STRATA_SM121_PERF" : sm120 ? "STRATA_SM120_PERF" : "STRATA_SM60_PERF",
sm121 || sm120 ? ", as does STRATA_DEFAULTS=026" : "",
sm86 ? "sm_86 (Ampere)" : sm89 ? "sm_89 (Ada)" : sm121 ? "sm_121 (GB10)" : sm120 ? "sm_120 (Blackwell)" : "sm_60 (Pascal)", set.size(), sm121 || fetch_set ? "" : "exact ",
sm86 ? "STRATA_SM86_PERF" : sm89 ? "STRATA_SM89_PERF" : sm121 ? "STRATA_SM121_PERF" : sm120 ? "STRATA_SM120_PERF" : "STRATA_SM60_PERF",
sm89 || sm121 || sm120 ? ", as does STRATA_DEFAULTS=026" : "",
fetch_set ? "; ADAPT_FETCH is on because this card has 16 GiB of memory or less, it does not give bit-for-bit the same text, STRATA_ADAPT_FETCH=0 turns it off" : "");
for (size_t i = 0; i < set.size(); ++i) std::fprintf(stderr, "%s%s", i ? " " : "", set[i].c_str() + 7);
std::fprintf(stderr, "\n");
+34 -9
View File
@@ -38,16 +38,25 @@ int main() {
unset("STRATA_SM86_PERF");
unset("STRATA_SM60_PERF");
unset("STRATA_EMULATE_CC");
// GB10 (12.1): the five measured own-stack switches, and no stale MMQ STRATA_PREFILL_NVFP4=w4a4 default
// GB10 (12.1): the five measured own-stack switches + 0.2.19 pipeline/PLE/quant/HC_Q8 + native sm_121a mxf4nvf4
{
const auto t = nv_default_env(12, 1, false);
CHECK(t.size() == 5);
CHECK(t.size() == 12);
CHECK(find(t, "STRATA_PF_FUSED") && *find(t, "STRATA_PF_FUSED") == "1");
CHECK(find(t, "STRATA_PF_FUSED_KQ") && *find(t, "STRATA_PF_FUSED_KQ") == "1");
CHECK(!find(t, "STRATA_PREFILL_NVFP4"));
CHECK(find(t, "STRATA_PREFILL_NVFP4") && *find(t, "STRATA_PREFILL_NVFP4") == "w4a4");
CHECK(find(t, "STRATA_HC_UPMIX") && *find(t, "STRATA_HC_UPMIX") == "1");
CHECK(find(t, "STRATA_SIDE_MIXER") && *find(t, "STRATA_SIDE_MIXER") == "2");
CHECK(find(t, "STRATA_SPEC_COST") && *find(t, "STRATA_SPEC_COST") == "1");
CHECK(find(t, "STRATA_DEQUANT_SIDE") && *find(t, "STRATA_DEQUANT_SIDE") == "1");
CHECK(find(t, "STRATA_QFUSE") && *find(t, "STRATA_QFUSE") == "1");
CHECK(find(t, "STRATA_LFUSE") && *find(t, "STRATA_LFUSE") == "1");
CHECK(find(t, "STRATA_PLE_COMPACT") && *find(t, "STRATA_PLE_COMPACT") == "1");
CHECK(find(t, "STRATA_PLE_COPY_STREAM") && *find(t, "STRATA_PLE_COPY_STREAM") == "1");
CHECK(find(t, "STRATA_HC_Q8") && *find(t, "STRATA_HC_Q8") == "1");
const auto t128 = nv_default_env(12, 1, false, 128ull << 30);
CHECK(find(t128, "STRATA_VERIFY_DEVICE_HITS") && *find(t128, "STRATA_VERIFY_DEVICE_HITS") == "1");
CHECK(!find(t128, "STRATA_ADAPT_FETCH"));
}
// STRATA_DEFAULTS=026 (the 0.2.7 master switch; the sm_121 table is newer than 0.2.6) empties it
CHECK(nv_default_env(12, 1, true).empty());
@@ -55,7 +64,7 @@ int main() {
put("STRATA_SM121_PERF", "0");
CHECK(nv_default_env(12, 1, false).empty());
put("STRATA_SM121_PERF", "1");
CHECK(nv_default_env(12, 1, false).size() == 5);
CHECK(nv_default_env(12, 1, false).size() == 12);
unset("STRATA_SM121_PERF");
// STRATA_PREFILL_NVFP4 resolution in builds without MMQ: w4a4 routes to fp4_tc where available and degrades to w4a8 (never FP16 cuBLAS) elsewhere
{
@@ -74,11 +83,11 @@ int main() {
CHECK(nvfp4_fused_covers("w4a4", false, false));
CHECK(!nvfp4_fused_covers("fp16", true, false));
}
// sm_120 (RTX 50 / PRO Blackwell): BW-13/BW-G + 0.2.19 exact overlap/PLE/quant fusions, Q8_0 HC, fused K-quant prefill, wide CPU share; STRATA_DEFAULTS=026 and STRATA_SM120_PERF=0 leave it out
// sm_120 (RTX 50 / PRO Blackwell): BW-13/BW-G + 0.2.19 exact overlap/PLE/quant fusions, Q8_0 HC, fused K-quant prefill, wide CPU share, native mxf4nvf4; STRATA_DEFAULTS=026 and STRATA_SM120_PERF=0 leave it out
{
unset("STRATA_SM120_PERF");
const auto t = nv_default_env(12, 0, false);
CHECK(t.size() == 13 && find(t, "STRATA_HC_UPMIX") && *find(t, "STRATA_HC_UPMIX") == "1");
CHECK(t.size() == 14 && find(t, "STRATA_HC_UPMIX") && *find(t, "STRATA_HC_UPMIX") == "1");
CHECK(find(t, "STRATA_L2_PF") && *find(t, "STRATA_L2_PF") == "1"); // BW-G
CHECK(find(t, "STRATA_Q80_ROWS1") && *find(t, "STRATA_Q80_ROWS1") == "1");
CHECK(find(t, "STRATA_EXP_MODE") && *find(t, "STRATA_EXP_MODE") == "8");
@@ -91,6 +100,7 @@ int main() {
CHECK(find(t, "STRATA_PF_FUSED_KQ") && *find(t, "STRATA_PF_FUSED_KQ") == "1");
CHECK(find(t, "STRATA_HC_Q8") && *find(t, "STRATA_HC_Q8") == "1");
CHECK(find(t, "STRATA_PREFILL_CPU_SHARE_NT") && *find(t, "STRATA_PREFILL_CPU_SHARE_NT") == "32");
CHECK(find(t, "STRATA_PREFILL_NVFP4") && *find(t, "STRATA_PREFILL_NVFP4") == "w4a4");
CHECK(!find(t, "STRATA_VERIFY_DEVICE_HITS") && !find(t, "STRATA_ADAPT_FETCH"));
// <= 16 GiB gets ADAPT_FETCH=2; > 16 GiB (RTX 5090 32 GB / RTX PRO 5000) gets VERIFY_DEVICE_HITS=1
const auto t16 = nv_default_env(12, 0, false, 16ull << 30);
@@ -103,12 +113,27 @@ int main() {
put("STRATA_SM120_PERF", "0");
CHECK(nv_default_env(12, 0, false).empty());
put("STRATA_SM120_PERF", "1");
CHECK(nv_default_env(12, 0, false).size() == 13);
CHECK(nv_default_env(12, 0, false).size() == 14);
unset("STRATA_SM120_PERF");
}
// sm_89 (RTX 40 / Ada Lovelace): 13 switches + VERIFY_DEVICE_HITS for > 16 GiB (RTX 4090 24 GB)
{
unset("STRATA_SM89_PERF");
const auto t = nv_default_env(8, 9, false);
CHECK(t.size() == 13 && find(t, "STRATA_HC_UPMIX") && *find(t, "STRATA_HC_UPMIX") == "1");
CHECK(find(t, "STRATA_L2_PF") && *find(t, "STRATA_L2_PF") == "1");
CHECK(find(t, "STRATA_PF_FUSED_KQ") && *find(t, "STRATA_PF_FUSED_KQ") == "1");
CHECK(find(t, "STRATA_HC_Q8") && *find(t, "STRATA_HC_Q8") == "1");
CHECK(!find(t, "STRATA_VERIFY_DEVICE_HITS"));
const auto t24 = nv_default_env(8, 9, false, 24ull << 30);
CHECK(t24.size() == 14 && find(t24, "STRATA_VERIFY_DEVICE_HITS") && *find(t24, "STRATA_VERIFY_DEVICE_HITS") == "1");
CHECK(nv_default_env(8, 9, true).empty());
put("STRATA_SM89_PERF", "0");
CHECK(nv_default_env(8, 9, false).empty());
unset("STRATA_SM89_PERF");
}
// sm_100 and the older cards get nothing from this file; the sm_86 / sm_60 tables are unchanged by the flag
CHECK(nv_default_env(10, 0, false).empty());
CHECK(nv_default_env(8, 9, false).empty());
CHECK(nv_default_env(7, 5, false).empty());
{
const auto a = nv_default_env(8, 6, false), b = nv_default_env(8, 6, true);
@@ -190,7 +215,7 @@ int main() {
unset("STRATA_PF_FUSED_SM86");
}
// the other tables are unchanged by it
CHECK(nv_default_env(12, 0, false).size() == 13); // sm_120 0.2.19 default stack
CHECK(nv_default_env(12, 0, false).size() == 14); // sm_120 0.2.19 default stack
// round 0212: the NVIDIA prompt-chunk ceiling of a bare `--prefill auto` (nv_chunk.hpp)
CHECK(nv_chunk_ceiling(nullptr, false) == 32768);
CHECK(nv_chunk_ceiling("1", false) == 32768);
+1
View File
@@ -2494,6 +2494,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
nsw.up_type = wsu->native_type; nsw.up_data = wsu->native_data;
nsw.down_type = wsd->native_type; nsw.down_data = wsd->native_data;
nsw.q8_1 = sh_fork ? sh_xq_ : xq_;
nsw.q8_1_il = (sh_fork || !g_mmvq_il()) ? nullptr : xil_;
// STRATA_VERIFY_QDEDUP=1 (not with the forked shared-expert stream, which quantizes into its own buffer): the
// experts' q8_1 image of xm is made first and the shared expert's gate/up read it (quantize_q8_1_rows and
// native_quantize_q8_1 write the same bytes)
+638 -34
View File
@@ -387,10 +387,620 @@ __device__ __forceinline__ size_t row_bytes_of(int ggml_type, int n_in) {
}
}
// ---- Decode / Verify Kernel: WARPS warps = (WARPS*16) output rows x (NTILES*8) tokens per CTA, K-chunked 256 ----
// WARPS=1 (16 rows/CTA) keeps 110-170 SMs saturated for moderate n_out (<=4096, e.g. out-proj 2560 -> 160 CTAs);
// WARPS=4 (64 rows/CTA) amortizes smem activation loads for wide n_out (>4096).
template <int GTYPE, int NTILES, int WARPS = 4>
// Byte count of one K<=256 chunk for a single weight row, padded up to a multiple of 4 bytes.
template <int GTYPE>
__device__ __host__ constexpr int chunk_u32_of() {
if constexpr (GTYPE == kTypeQ8_0) return 68; // 8 * 34 = 272 B
else if constexpr (GTYPE == kTypeQ4_K) return 36; // 144 B
else if constexpr (GTYPE == kTypeQ5_K) return 44; // 176 B
else if constexpr (GTYPE == kTypeQ6_K) return 53; // 210 B -> 212 B
else return 34; // IQ4_XS: 136 B
}
// 100%-coalesced warp-wide weight stager: all 32 lanes read contiguous words along each of 16 rows in turn.
template <int GTYPE>
__device__ __forceinline__ void stage_dense_rows16(
const uint8_t* __restrict__ weights, int r_base, int n_out, size_t rbytes,
int k_blk0, int nb_here, int lane, uint32_t (&s_w)[16][chunk_u32_of<GTYPE>()]) {
constexpr int MAX_U32 = chunk_u32_of<GTYPE>();
const size_t blk_off = (GTYPE == kTypeQ8_0)
? (size_t) k_blk0 * 34u
: (size_t) (k_blk0 >> 3) * (GTYPE == kTypeQ4_K ? 144u :
GTYPE == kTypeQ5_K ? 176u :
GTYPE == kTypeQ6_K ? 210u : 136u);
const int n_bytes = (GTYPE == kTypeQ8_0)
? nb_here * 34
: (GTYPE == kTypeQ4_K ? 144 :
GTYPE == kTypeQ5_K ? 176 :
GTYPE == kTypeQ6_K ? 210 : 136);
const int full_u32 = n_bytes >> 2;
const bool has_tail2 = (n_bytes & 2) != 0;
#pragma unroll
for (int rr = 0; rr < 16; ++rr) {
if (r_base + rr < n_out) {
const uint8_t* p = weights + (size_t) (r_base + rr) * rbytes + blk_off;
if constexpr (GTYPE == kTypeQ4_K || GTYPE == kTypeQ5_K) {
const uint32_t* p32 = reinterpret_cast<const uint32_t*>(p);
for (int u = lane; u < full_u32; u += 32) s_w[rr][u] = __ldg(p32 + u);
} else {
for (int u = lane; u < full_u32; u += 32) {
const uint16_t* p16 = reinterpret_cast<const uint16_t*>(p + u * 4);
s_w[rr][u] = (uint32_t) __ldg(p16) | ((uint32_t) __ldg(p16 + 1) << 16);
}
if (has_tail2 && lane == 0) {
s_w[rr][full_u32] = (uint32_t) __ldg(reinterpret_cast<const uint16_t*>(p + full_u32 * 4));
}
}
} else {
for (int u = lane; u < MAX_U32; u += 32) s_w[rr][u] = 0u;
}
}
__syncwarp();
}
// Fast shared-memory byte/half/u32 readers for staged rows in s_w (already 4-byte aligned at row start!).
__device__ __forceinline__ uint32_t smem_ld32_b2(const uint8_t* p) {
const uint16_t* p16 = reinterpret_cast<const uint16_t*>(p);
return (uint32_t) p16[0] | ((uint32_t) p16[1] << 16);
}
__device__ __forceinline__ float smem_ld_fp16_b2(const uint8_t* p) {
return __half2float(__ushort_as_half(*reinterpret_cast<const uint16_t*>(p)));
}
template <int GTYPE, int NTILES>
__device__ __forceinline__ void eval_kchunk_tc_smem(
const uint32_t (&s_w)[16][chunk_u32_of<GTYPE>()],
bool v0, bool v1, int nb_here,
const int8_t* __restrict__ s_xq, const float* __restrict__ s_dx, const float* __restrict__ s_xs,
int g, int tig, float (&acc)[NTILES][4]) {
#if STRATA_DENSE_I8_SM80
const uint8_t* blk0 = reinterpret_cast<const uint8_t*>(s_w[g]);
const uint8_t* blk1 = reinterpret_cast<const uint8_t*>(s_w[8 + g]);
if constexpr (GTYPE == kTypeQ8_0) {
for (int b = 0; b < nb_here; ++b) {
const uint8_t* p0 = blk0 + b * 34;
const uint8_t* p1 = blk1 + b * 34;
const float dw0 = v0 ? smem_ld_fp16_b2(p0) : 0.0f;
const float dw1 = v1 ? smem_ld_fp16_b2(p1) : 0.0f;
const uint32_t a0 = v0 ? smem_ld32_b2(p0 + 2 + tig * 4) : 0u;
const uint32_t a2 = v0 ? smem_ld32_b2(p0 + 18 + tig * 4) : 0u;
const uint32_t a1 = v1 ? smem_ld32_b2(p1 + 2 + tig * 4) : 0u;
const uint32_t a3 = v1 ? smem_ld32_b2(p1 + 18 + tig * 4) : 0u;
#pragma unroll
for (int nt = 0; nt < NTILES; ++nt) {
const int tg = nt * 8 + g;
const uint32_t b0 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + tig * 4);
const uint32_t b1 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + 16 + tig * 4);
int32_t d[4] = {0, 0, 0, 0};
mma_s8_m16n8k32(d, a0, a1, a2, a3, b0, b1);
const int t0 = nt * 8 + 2 * tig;
const int t1 = t0 + 1;
const float dx0 = s_dx[t0 * 8 + b];
const float dx1 = s_dx[t1 * 8 + b];
acc[nt][0] = fmaf(dw0 * dx0, (float) d[0], acc[nt][0]);
acc[nt][1] = fmaf(dw0 * dx1, (float) d[1], acc[nt][1]);
acc[nt][2] = fmaf(dw1 * dx0, (float) d[2], acc[nt][2]);
acc[nt][3] = fmaf(dw1 * dx1, (float) d[3], acc[nt][3]);
}
}
} else if constexpr (GTYPE == kTypeQ4_K) {
const half2 dm0 = v0 ? *reinterpret_cast<const half2*>(blk0) : __floats2half2_rn(0.0f, 0.0f);
const half2 dm1 = v1 ? *reinterpret_cast<const half2*>(blk1) : __floats2half2_rn(0.0f, 0.0f);
const float d0_all = __low2float(dm0), m0_all = __high2float(dm0);
const float d1_all = __low2float(dm1), m1_all = __high2float(dm1);
const uint8_t* sc0_p = blk0 + 4;
const uint8_t* sc1_p = blk1 + 4;
const uint8_t* qs0_p = blk0 + 16;
const uint8_t* qs1_p = blk1 + 16;
#pragma unroll
for (int j = 0; j < 4; ++j) {
const uint32_t raw0_lo = v0 ? *reinterpret_cast<const uint32_t*>(qs0_p + 32 * j + tig * 4) : 0u;
const uint32_t raw0_hi = v0 ? *reinterpret_cast<const uint32_t*>(qs0_p + 32 * j + 16 + tig * 4) : 0u;
const uint32_t raw1_lo = v1 ? *reinterpret_cast<const uint32_t*>(qs1_p + 32 * j + tig * 4) : 0u;
const uint32_t raw1_hi = v1 ? *reinterpret_cast<const uint32_t*>(qs1_p + 32 * j + 16 + tig * 4) : 0u;
#pragma unroll
for (int hi = 0; hi < 2; ++hi) {
const int b = 2 * j + hi;
float s0 = 0.0f, mn0 = 0.0f, s1 = 0.0f, mn1 = 0.0f;
if (v0) decode_k4_scale_min(sc0_p, b, s0, mn0);
if (v1) decode_k4_scale_min(sc1_p, b, s1, mn1);
const float ds0 = d0_all * s0, ms0 = m0_all * mn0;
const float ds1 = d1_all * s1, ms1 = m1_all * mn1;
const uint32_t a0 = (hi == 0) ? (raw0_lo & 0x0F0F0F0Fu) : ((raw0_lo >> 4) & 0x0F0F0F0Fu);
const uint32_t a2 = (hi == 0) ? (raw0_hi & 0x0F0F0F0Fu) : ((raw0_hi >> 4) & 0x0F0F0F0Fu);
const uint32_t a1 = (hi == 0) ? (raw1_lo & 0x0F0F0F0Fu) : ((raw1_lo >> 4) & 0x0F0F0F0Fu);
const uint32_t a3 = (hi == 0) ? (raw1_hi & 0x0F0F0F0Fu) : ((raw1_hi >> 4) & 0x0F0F0F0Fu);
#pragma unroll
for (int nt = 0; nt < NTILES; ++nt) {
const int tg = nt * 8 + g;
const uint32_t b0 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + tig * 4);
const uint32_t b1 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + 16 + tig * 4);
int32_t d[4] = {0, 0, 0, 0};
mma_s8_m16n8k32(d, a0, a1, a2, a3, b0, b1);
const int t0 = nt * 8 + 2 * tig;
const int t1 = t0 + 1;
acc[nt][0] = fmaf(ds0 * s_dx[t0 * 8 + b], (float) d[0], fmaf(-ms0, s_xs[t0 * 8 + b], acc[nt][0]));
acc[nt][1] = fmaf(ds0 * s_dx[t1 * 8 + b], (float) d[1], fmaf(-ms0, s_xs[t1 * 8 + b], acc[nt][1]));
acc[nt][2] = fmaf(ds1 * s_dx[t0 * 8 + b], (float) d[2], fmaf(-ms1, s_xs[t0 * 8 + b], acc[nt][2]));
acc[nt][3] = fmaf(ds1 * s_dx[t1 * 8 + b], (float) d[3], fmaf(-ms1, s_xs[t1 * 8 + b], acc[nt][3]));
}
}
}
} else if constexpr (GTYPE == kTypeQ5_K) {
const half2 dm0 = v0 ? *reinterpret_cast<const half2*>(blk0) : __floats2half2_rn(0.0f, 0.0f);
const half2 dm1 = v1 ? *reinterpret_cast<const half2*>(blk1) : __floats2half2_rn(0.0f, 0.0f);
const float d0_all = __low2float(dm0), m0_all = __high2float(dm0);
const float d1_all = __low2float(dm1), m1_all = __high2float(dm1);
const uint8_t* sc0_p = blk0 + 4;
const uint8_t* sc1_p = blk1 + 4;
const uint32_t qh0_lo = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 16 + tig * 4) : 0u;
const uint32_t qh0_hi = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 32 + tig * 4) : 0u;
const uint32_t qh1_lo = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 16 + tig * 4) : 0u;
const uint32_t qh1_hi = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 32 + tig * 4) : 0u;
const uint8_t* qs0_p = blk0 + 48;
const uint8_t* qs1_p = blk1 + 48;
#pragma unroll
for (int j = 0; j < 4; ++j) {
const uint32_t raw0_lo = v0 ? *reinterpret_cast<const uint32_t*>(qs0_p + 32 * j + tig * 4) : 0u;
const uint32_t raw0_hi = v0 ? *reinterpret_cast<const uint32_t*>(qs0_p + 32 * j + 16 + tig * 4) : 0u;
const uint32_t raw1_lo = v1 ? *reinterpret_cast<const uint32_t*>(qs1_p + 32 * j + tig * 4) : 0u;
const uint32_t raw1_hi = v1 ? *reinterpret_cast<const uint32_t*>(qs1_p + 32 * j + 16 + tig * 4) : 0u;
#pragma unroll
for (int hi = 0; hi < 2; ++hi) {
const int b = 2 * j + hi;
float s0 = 0.0f, mn0 = 0.0f, s1 = 0.0f, mn1 = 0.0f;
if (v0) decode_k4_scale_min(sc0_p, b, s0, mn0);
if (v1) decode_k4_scale_min(sc1_p, b, s1, mn1);
const float ds0 = d0_all * s0, ms0 = m0_all * mn0;
const float ds1 = d1_all * s1, ms1 = m1_all * mn1;
const uint32_t q4_0l = (hi == 0) ? (raw0_lo & 0x0F0F0F0Fu) : ((raw0_lo >> 4) & 0x0F0F0F0Fu);
const uint32_t q4_0h = (hi == 0) ? (raw0_hi & 0x0F0F0F0Fu) : ((raw0_hi >> 4) & 0x0F0F0F0Fu);
const uint32_t q4_1l = (hi == 0) ? (raw1_lo & 0x0F0F0F0Fu) : ((raw1_lo >> 4) & 0x0F0F0F0Fu);
const uint32_t q4_1h = (hi == 0) ? (raw1_hi & 0x0F0F0F0Fu) : ((raw1_hi >> 4) & 0x0F0F0F0Fu);
const uint32_t a0 = q4_0l | (((qh0_lo >> b) & 0x01010101u) << 4);
const uint32_t a2 = q4_0h | (((qh0_hi >> b) & 0x01010101u) << 4);
const uint32_t a1 = q4_1l | (((qh1_lo >> b) & 0x01010101u) << 4);
const uint32_t a3 = q4_1h | (((qh1_hi >> b) & 0x01010101u) << 4);
#pragma unroll
for (int nt = 0; nt < NTILES; ++nt) {
const int tg = nt * 8 + g;
const uint32_t b0 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + tig * 4);
const uint32_t b1 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + 16 + tig * 4);
int32_t d[4] = {0, 0, 0, 0};
mma_s8_m16n8k32(d, a0, a1, a2, a3, b0, b1);
const int t0 = nt * 8 + 2 * tig;
const int t1 = t0 + 1;
acc[nt][0] = fmaf(ds0 * s_dx[t0 * 8 + b], (float) d[0], fmaf(-ms0, s_xs[t0 * 8 + b], acc[nt][0]));
acc[nt][1] = fmaf(ds0 * s_dx[t1 * 8 + b], (float) d[1], fmaf(-ms0, s_xs[t1 * 8 + b], acc[nt][1]));
acc[nt][2] = fmaf(ds1 * s_dx[t0 * 8 + b], (float) d[2], fmaf(-ms1, s_xs[t0 * 8 + b], acc[nt][2]));
acc[nt][3] = fmaf(ds1 * s_dx[t1 * 8 + b], (float) d[3], fmaf(-ms1, s_xs[t1 * 8 + b], acc[nt][3]));
}
}
}
} else if constexpr (GTYPE == kTypeQ6_K) {
const float d0_all = v0 ? smem_ld_fp16_b2(blk0 + 208) : 0.0f;
const float d1_all = v1 ? smem_ld_fp16_b2(blk1 + 208) : 0.0f;
const int8_t* sc0_p = reinterpret_cast<const int8_t*>(blk0 + 192);
const int8_t* sc1_p = reinterpret_cast<const int8_t*>(blk1 + 192);
#pragma unroll
for (int n = 0; n < 2; ++n) {
#pragma unroll
for (int half16 = 0; half16 < 2; ++half16) {
const int l0 = half16 * 16;
const uint32_t qh0 = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 128 + 32 * n + l0 + tig * 4) : 0u;
const uint32_t qh1 = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 128 + 32 * n + l0 + tig * 4) : 0u;
const uint32_t ql0_a = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 64 * n + l0 + tig * 4) : 0u;
const uint32_t ql0_b = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 64 * n + 32 + l0 + tig * 4) : 0u;
const uint32_t ql1_a = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 64 * n + l0 + tig * 4) : 0u;
const uint32_t ql1_b = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 64 * n + 32 + l0 + tig * 4) : 0u;
#pragma unroll
for (int qid = 0; qid < 4; ++qid) {
const int sub = 8 * n + 2 * qid + half16;
const int b32 = sub >> 1;
const float dw0 = v0 ? (d0_all * (float) sc0_p[sub]) : 0.0f;
const float dw1 = v1 ? (d1_all * (float) sc1_p[sub]) : 0.0f;
const uint32_t src0 = (qid & 1) ? ql0_b : ql0_a;
const uint32_t src1 = (qid & 1) ? ql1_b : ql1_a;
const uint32_t lo0 = (qid < 2) ? (src0 & 0x0F0F0F0Fu) : ((src0 >> 4) & 0x0F0F0F0Fu);
const uint32_t lo1 = (qid < 2) ? (src1 & 0x0F0F0F0Fu) : ((src1 >> 4) & 0x0F0F0F0Fu);
const uint32_t a0 = __vsub4(lo0 | (((qh0 >> (2 * qid)) & 0x03030303u) << 4), 0x20202020u);
const uint32_t a1 = __vsub4(lo1 | (((qh1 >> (2 * qid)) & 0x03030303u) << 4), 0x20202020u);
#pragma unroll
for (int nt = 0; nt < NTILES; ++nt) {
const int tg = nt * 8 + g;
const uint32_t b0 = *reinterpret_cast<const uint32_t*>(
s_xq + (tg * 8 + b32) * 32 + half16 * 16 + tig * 4);
int32_t d[4] = {0, 0, 0, 0};
mma_s8_m16n8k16(d, a0, a1, b0);
const int t0 = nt * 8 + 2 * tig;
const int t1 = t0 + 1;
acc[nt][0] = fmaf(dw0 * s_dx[t0 * 8 + b32], (float) d[0], acc[nt][0]);
acc[nt][1] = fmaf(dw0 * s_dx[t1 * 8 + b32], (float) d[1], acc[nt][1]);
acc[nt][2] = fmaf(dw1 * s_dx[t0 * 8 + b32], (float) d[2], acc[nt][2]);
acc[nt][3] = fmaf(dw1 * s_dx[t1 * 8 + b32], (float) d[3], acc[nt][3]);
}
}
}
}
} else if constexpr (GTYPE == kTypeIQ4_XS) {
const float d0_all = v0 ? smem_ld_fp16_b2(blk0) : 0.0f;
const float d1_all = v1 ? smem_ld_fp16_b2(blk1) : 0.0f;
const uint16_t sh0 = v0 ? *reinterpret_cast<const uint16_t*>(blk0 + 2) : 0u;
const uint16_t sh1 = v1 ? *reinterpret_cast<const uint16_t*>(blk1 + 2) : 0u;
const uint32_t sl0 = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 4) : 0u;
const uint32_t sl1 = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 4) : 0u;
#pragma unroll
for (int b = 0; b < 8; ++b) {
const int ls0 = (int) (((sl0 >> (4 * b)) & 0x0Fu) | (((sh0 >> (2 * b)) & 3u) << 4)) - 32;
const int ls1 = (int) (((sl1 >> (4 * b)) & 0x0Fu) | (((sh1 >> (2 * b)) & 3u) << 4)) - 32;
const float dw0 = v0 ? (d0_all * (float) ls0) : 0.0f;
const float dw1 = v1 ? (d1_all * (float) ls1) : 0.0f;
const uint32_t raw0 = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 8 + b * 16 + tig * 4) : 0u;
const uint32_t raw1 = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 8 + b * 16 + tig * 4) : 0u;
const uint2 w0_lh = iq4nl_lut8(raw0);
const uint2 w1_lh = iq4nl_lut8(raw1);
#pragma unroll
for (int nt = 0; nt < NTILES; ++nt) {
const int tg = nt * 8 + g;
const uint32_t b0 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + tig * 4);
const uint32_t b1 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + 16 + tig * 4);
int32_t d[4] = {0, 0, 0, 0};
mma_s8_m16n8k32(d, w0_lh.x, w1_lh.x, w0_lh.y, w1_lh.y, b0, b1);
const int t0 = nt * 8 + 2 * tig;
const int t1 = t0 + 1;
acc[nt][0] = fmaf(dw0 * s_dx[t0 * 8 + b], (float) d[0], acc[nt][0]);
acc[nt][1] = fmaf(dw0 * s_dx[t1 * 8 + b], (float) d[1], acc[nt][1]);
acc[nt][2] = fmaf(dw1 * s_dx[t0 * 8 + b], (float) d[2], acc[nt][2]);
acc[nt][3] = fmaf(dw1 * s_dx[t1 * 8 + b], (float) d[3], acc[nt][3]);
}
}
}
#else
(void) s_w; (void) v0; (void) v1; (void) nb_here;
(void) s_xq; (void) s_dx; (void) s_xs; (void) g; (void) tig; (void) acc;
#endif
}
// ---- High-Occupancy 8-Warp K-Split Decode / Verify Kernel: 16 output rows x (NTILES*8) tokens per 256-thread CTA ----
// All 8 warps cooperate on ONE 16-row tile per K=256 superblock:
// - Load phase: each warp loads ONLY 2 rows coalesced into shared s_w[16][U32] (~3.4 KB total for the whole CTA!),
// while cooperatively loading T*8 activation blocks into s_xq (~2.0 KB). Total smem ~6-9 KB -> 6+ CTAs/SM = 48 warps/SM!
// - Compute phase: warp w (0..7) owns sub-block b = w (32 elements of the 256-elem superblock), executing just ONE
// mma.m16n8k32.s8 (or two m16n8k16 for Q6_K) per 8-token tile!
// - Epilogue: single warp-reduction across the 8 warps at the very end of the K loop!
template <int GTYPE, int NTILES>
__global__ void __launch_bounds__(256) dense_i8_tc_decode_ksplit_kernel(
const uint8_t* __restrict__ weights, const Q81Block* __restrict__ xq, float* __restrict__ y,
int n_in, int n_out, int T) {
#if STRATA_DENSE_I8_SM80
constexpr int MAX_T = NTILES * 8;
constexpr int MAX_U32 = chunk_u32_of<GTYPE>();
const int tid = (int) threadIdx.x;
const int warp = tid >> 5; // 0..7 -> sub-block b = warp within each K=256 chunk!
const int lane = tid & 31;
const int g = lane >> 2; // 0..7
const int tig = lane & 3; // 0..3
const int r_base = (int) blockIdx.x * 16;
const int r0 = r_base + g;
const int r1 = r_base + 8 + g;
const bool v0 = r0 < n_out;
const bool v1 = r1 < n_out;
const int nb32 = n_in >> 5;
const size_t rbytes = row_bytes_of(GTYPE, n_in);
constexpr size_t kChunkBytes = (GTYPE == kTypeQ8_0) ? 272u :
(GTYPE == kTypeQ4_K) ? 144u :
(GTYPE == kTypeQ5_K) ? 176u :
(GTYPE == kTypeQ6_K) ? 210u : 136u;
__shared__ alignas(16) int8_t s_xq[MAX_T * 256];
__shared__ float s_dx[MAX_T * 8];
__shared__ float s_xs[MAX_T * 8];
__shared__ alignas(16) uint32_t s_w[16][MAX_U32];
float acc[NTILES][4];
#pragma unroll
for (int nt = 0; nt < NTILES; ++nt) {
acc[nt][0] = 0.0f; acc[nt][1] = 0.0f; acc[nt][2] = 0.0f; acc[nt][3] = 0.0f;
}
const uint8_t* blk0 = reinterpret_cast<const uint8_t*>(s_w[g]);
const uint8_t* blk1 = reinterpret_cast<const uint8_t*>(s_w[8 + g]);
for (int k_blk0 = 0; k_blk0 < nb32; k_blk0 += 8) {
const int nb_here = min(8, nb32 - k_blk0);
const int n_bytes = (GTYPE == kTypeQ8_0) ? (nb_here * 34) : (int) kChunkBytes;
const int full_u32 = n_bytes >> 2;
const bool has_tail2 = (n_bytes & 2) != 0;
const size_t blk_off = (GTYPE == kTypeQ8_0) ? ((size_t) k_blk0 * 34u)
: ((size_t) (k_blk0 >> 3) * kChunkBytes);
// 256 threads load <= 128 activation blocks (at most 1 block per 2 threads -> 1 cycle!).
if (tid < MAX_T * 8) {
const int t = tid >> 3;
const int b = tid & 7;
if (t < T && b < nb_here) {
const Q81Block& blk = xq[(size_t) t * nb32 + k_blk0 + b];
const float dx = __low2float(blk.ds);
s_dx[tid] = dx;
const uint32_t* src32 = reinterpret_cast<const uint32_t*>(blk.qs);
const uint32_t x0 = src32[0], x1 = src32[1], x2 = src32[2], x3 = src32[3];
const uint32_t x4 = src32[4], x5 = src32[5], x6 = src32[6], x7 = src32[7];
if constexpr (GTYPE == kTypeQ4_K || GTYPE == kTypeQ5_K) {
int qsum = __dp4a(0x01010101, (int) x0, 0);
qsum = __dp4a(0x01010101, (int) x1, qsum);
qsum = __dp4a(0x01010101, (int) x2, qsum);
qsum = __dp4a(0x01010101, (int) x3, qsum);
qsum = __dp4a(0x01010101, (int) x4, qsum);
qsum = __dp4a(0x01010101, (int) x5, qsum);
qsum = __dp4a(0x01010101, (int) x6, qsum);
qsum = __dp4a(0x01010101, (int) x7, qsum);
s_xs[tid] = dx * (float) qsum;
}
uint4* dst4 = reinterpret_cast<uint4*>(s_xq + tid * 32);
dst4[0] = make_uint4(x0, x1, x2, x3);
dst4[1] = make_uint4(x4, x5, x6, x7);
} else {
s_dx[tid] = 0.0f;
if constexpr (GTYPE == kTypeQ4_K || GTYPE == kTypeQ5_K) s_xs[tid] = 0.0f;
uint4* dst4 = reinterpret_cast<uint4*>(s_xq + tid * 32);
dst4[0] = make_uint4(0u, 0u, 0u, 0u);
dst4[1] = make_uint4(0u, 0u, 0u, 0u);
}
}
// 8 warps cooperatively load 16 weight rows (ONLY 2 rows per warp, 100% coalesced 128B transactions!).
#pragma unroll
for (int rr = 0; rr < 2; ++rr) {
const int lr = warp * 2 + rr;
if (r_base + lr < n_out) {
const uint8_t* p = weights + (size_t) (r_base + lr) * rbytes + blk_off;
if constexpr (GTYPE == kTypeQ4_K || GTYPE == kTypeQ5_K) {
const uint32_t* p32 = reinterpret_cast<const uint32_t*>(p);
for (int u = lane; u < full_u32; u += 32) s_w[lr][u] = __ldg(p32 + u);
} else {
const bool al4 = (((uintptr_t) p & 3u) == 0u);
for (int u = lane; u < full_u32; u += 32) {
if (al4) {
s_w[lr][u] = __ldg(reinterpret_cast<const uint32_t*>(p) + u);
} else {
const uint16_t* p16 = reinterpret_cast<const uint16_t*>(p + u * 4);
s_w[lr][u] = (uint32_t) __ldg(p16) | ((uint32_t) __ldg(p16 + 1) << 16);
}
}
if (has_tail2 && lane == 0) {
s_w[lr][full_u32] = (uint32_t) __ldg(reinterpret_cast<const uint16_t*>(p + full_u32 * 4));
}
}
}
}
__syncthreads();
// Each warp w (0..7) evaluates ONLY sub-block b = warp (32 elements)!
const int b = warp;
if (b < nb_here) {
if constexpr (GTYPE == kTypeQ8_0) {
const uint8_t* p0 = blk0 + b * 34;
const uint8_t* p1 = blk1 + b * 34;
const float dw0 = v0 ? smem_ld_fp16_b2(p0) : 0.0f;
const float dw1 = v1 ? smem_ld_fp16_b2(p1) : 0.0f;
const uint32_t a0 = v0 ? smem_ld32_b2(p0 + 2 + tig * 4) : 0u;
const uint32_t a2 = v0 ? smem_ld32_b2(p0 + 18 + tig * 4) : 0u;
const uint32_t a1 = v1 ? smem_ld32_b2(p1 + 2 + tig * 4) : 0u;
const uint32_t a3 = v1 ? smem_ld32_b2(p1 + 18 + tig * 4) : 0u;
#pragma unroll
for (int nt = 0; nt < NTILES; ++nt) {
const int tg = nt * 8 + g;
const uint32_t b0 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + tig * 4);
const uint32_t b1 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + 16 + tig * 4);
int32_t d[4] = {0, 0, 0, 0};
mma_s8_m16n8k32(d, a0, a1, a2, a3, b0, b1);
const int t0 = nt * 8 + 2 * tig;
const int t1 = t0 + 1;
acc[nt][0] = fmaf(dw0 * s_dx[t0 * 8 + b], (float) d[0], acc[nt][0]);
acc[nt][1] = fmaf(dw0 * s_dx[t1 * 8 + b], (float) d[1], acc[nt][1]);
acc[nt][2] = fmaf(dw1 * s_dx[t0 * 8 + b], (float) d[2], acc[nt][2]);
acc[nt][3] = fmaf(dw1 * s_dx[t1 * 8 + b], (float) d[3], acc[nt][3]);
}
} else if constexpr (GTYPE == kTypeQ4_K) {
const half2 dm0 = v0 ? *reinterpret_cast<const half2*>(blk0) : __floats2half2_rn(0.0f, 0.0f);
const half2 dm1 = v1 ? *reinterpret_cast<const half2*>(blk1) : __floats2half2_rn(0.0f, 0.0f);
float s0 = 0.0f, mn0 = 0.0f, s1 = 0.0f, mn1 = 0.0f;
if (v0) decode_k4_scale_min(blk0 + 4, b, s0, mn0);
if (v1) decode_k4_scale_min(blk1 + 4, b, s1, mn1);
const float ds0 = __low2float(dm0) * s0, ms0 = __high2float(dm0) * mn0;
const float ds1 = __low2float(dm1) * s1, ms1 = __high2float(dm1) * mn1;
const int j = b >> 1, hi = b & 1;
const uint32_t raw0_lo = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 16 + 32 * j + tig * 4) : 0u;
const uint32_t raw0_hi = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 16 + 32 * j + 16 + tig * 4) : 0u;
const uint32_t raw1_lo = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 16 + 32 * j + tig * 4) : 0u;
const uint32_t raw1_hi = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 16 + 32 * j + 16 + tig * 4) : 0u;
const uint32_t a0 = hi ? ((raw0_lo >> 4) & 0x0F0F0F0Fu) : (raw0_lo & 0x0F0F0F0Fu);
const uint32_t a2 = hi ? ((raw0_hi >> 4) & 0x0F0F0F0Fu) : (raw0_hi & 0x0F0F0F0Fu);
const uint32_t a1 = hi ? ((raw1_lo >> 4) & 0x0F0F0F0Fu) : (raw1_lo & 0x0F0F0F0Fu);
const uint32_t a3 = hi ? ((raw1_hi >> 4) & 0x0F0F0F0Fu) : (raw1_hi & 0x0F0F0F0Fu);
#pragma unroll
for (int nt = 0; nt < NTILES; ++nt) {
const int tg = nt * 8 + g;
const uint32_t b0 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + tig * 4);
const uint32_t b1 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + 16 + tig * 4);
int32_t d[4] = {0, 0, 0, 0};
mma_s8_m16n8k32(d, a0, a1, a2, a3, b0, b1);
const int t0 = nt * 8 + 2 * tig;
const int t1 = t0 + 1;
acc[nt][0] = fmaf(ds0 * s_dx[t0 * 8 + b], (float) d[0], fmaf(-ms0, s_xs[t0 * 8 + b], acc[nt][0]));
acc[nt][1] = fmaf(ds0 * s_dx[t1 * 8 + b], (float) d[1], fmaf(-ms0, s_xs[t1 * 8 + b], acc[nt][1]));
acc[nt][2] = fmaf(ds1 * s_dx[t0 * 8 + b], (float) d[2], fmaf(-ms1, s_xs[t0 * 8 + b], acc[nt][2]));
acc[nt][3] = fmaf(ds1 * s_dx[t1 * 8 + b], (float) d[3], fmaf(-ms1, s_xs[t1 * 8 + b], acc[nt][3]));
}
} else if constexpr (GTYPE == kTypeQ5_K) {
const half2 dm0 = v0 ? *reinterpret_cast<const half2*>(blk0) : __floats2half2_rn(0.0f, 0.0f);
const half2 dm1 = v1 ? *reinterpret_cast<const half2*>(blk1) : __floats2half2_rn(0.0f, 0.0f);
float s0 = 0.0f, mn0 = 0.0f, s1 = 0.0f, mn1 = 0.0f;
if (v0) decode_k4_scale_min(blk0 + 4, b, s0, mn0);
if (v1) decode_k4_scale_min(blk1 + 4, b, s1, mn1);
const float ds0 = __low2float(dm0) * s0, ms0 = __high2float(dm0) * mn0;
const float ds1 = __low2float(dm1) * s1, ms1 = __high2float(dm1) * mn1;
const int j = b >> 1, hi = b & 1;
const uint32_t qh0_lo = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 16 + tig * 4) : 0u;
const uint32_t qh0_hi = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 32 + tig * 4) : 0u;
const uint32_t qh1_lo = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 16 + tig * 4) : 0u;
const uint32_t qh1_hi = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 32 + tig * 4) : 0u;
const uint32_t raw0_lo = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 48 + 32 * j + tig * 4) : 0u;
const uint32_t raw0_hi = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 48 + 32 * j + 16 + tig * 4) : 0u;
const uint32_t raw1_lo = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 48 + 32 * j + tig * 4) : 0u;
const uint32_t raw1_hi = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 48 + 32 * j + 16 + tig * 4) : 0u;
const uint32_t q4_0l = hi ? ((raw0_lo >> 4) & 0x0F0F0F0Fu) : (raw0_lo & 0x0F0F0F0Fu);
const uint32_t q4_0h = hi ? ((raw0_hi >> 4) & 0x0F0F0F0Fu) : (raw0_hi & 0x0F0F0F0Fu);
const uint32_t q4_1l = hi ? ((raw1_lo >> 4) & 0x0F0F0F0Fu) : (raw1_lo & 0x0F0F0F0Fu);
const uint32_t q4_1h = hi ? ((raw1_hi >> 4) & 0x0F0F0F0Fu) : (raw1_hi & 0x0F0F0F0Fu);
const uint32_t a0 = q4_0l | (((qh0_lo >> b) & 0x01010101u) << 4);
const uint32_t a2 = q4_0h | (((qh0_hi >> b) & 0x01010101u) << 4);
const uint32_t a1 = q4_1l | (((qh1_lo >> b) & 0x01010101u) << 4);
const uint32_t a3 = q4_1h | (((qh1_hi >> b) & 0x01010101u) << 4);
#pragma unroll
for (int nt = 0; nt < NTILES; ++nt) {
const int tg = nt * 8 + g;
const uint32_t b0 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + tig * 4);
const uint32_t b1 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + 16 + tig * 4);
int32_t d[4] = {0, 0, 0, 0};
mma_s8_m16n8k32(d, a0, a1, a2, a3, b0, b1);
const int t0 = nt * 8 + 2 * tig;
const int t1 = t0 + 1;
acc[nt][0] = fmaf(ds0 * s_dx[t0 * 8 + b], (float) d[0], fmaf(-ms0, s_xs[t0 * 8 + b], acc[nt][0]));
acc[nt][1] = fmaf(ds0 * s_dx[t1 * 8 + b], (float) d[1], fmaf(-ms0, s_xs[t1 * 8 + b], acc[nt][1]));
acc[nt][2] = fmaf(ds1 * s_dx[t0 * 8 + b], (float) d[2], fmaf(-ms1, s_xs[t0 * 8 + b], acc[nt][2]));
acc[nt][3] = fmaf(ds1 * s_dx[t1 * 8 + b], (float) d[3], fmaf(-ms1, s_xs[t1 * 8 + b], acc[nt][3]));
}
} else if constexpr (GTYPE == kTypeQ6_K) {
const float d0_all = v0 ? smem_ld_fp16_b2(blk0 + 208) : 0.0f;
const float d1_all = v1 ? smem_ld_fp16_b2(blk1 + 208) : 0.0f;
const int8_t* sc0_p = reinterpret_cast<const int8_t*>(blk0 + 192);
const int8_t* sc1_p = reinterpret_cast<const int8_t*>(blk1 + 192);
const int n = b >> 2;
const int qid = b & 3;
const int ql_off = 64 * n + ((qid & 1) ? 32 : 0);
#pragma unroll
for (int half16 = 0; half16 < 2; ++half16) {
const int l0 = half16 * 16;
const int sub = 2 * b + half16;
const float dw0 = v0 ? (d0_all * (float) sc0_p[sub]) : 0.0f;
const float dw1 = v1 ? (d1_all * (float) sc1_p[sub]) : 0.0f;
const uint32_t qh0 = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 128 + 32 * n + l0 + tig * 4) : 0u;
const uint32_t qh1 = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 128 + 32 * n + l0 + tig * 4) : 0u;
const uint32_t src0 = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + ql_off + l0 + tig * 4) : 0u;
const uint32_t src1 = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + ql_off + l0 + tig * 4) : 0u;
const uint32_t lo0 = (qid < 2) ? (src0 & 0x0F0F0F0Fu) : ((src0 >> 4) & 0x0F0F0F0Fu);
const uint32_t lo1 = (qid < 2) ? (src1 & 0x0F0F0F0Fu) : ((src1 >> 4) & 0x0F0F0F0Fu);
const uint32_t a0 = __vsub4(lo0 | (((qh0 >> (2 * qid)) & 0x03030303u) << 4), 0x20202020u);
const uint32_t a1 = __vsub4(lo1 | (((qh1 >> (2 * qid)) & 0x03030303u) << 4), 0x20202020u);
#pragma unroll
for (int nt = 0; nt < NTILES; ++nt) {
const int tg = nt * 8 + g;
const uint32_t b0 = *reinterpret_cast<const uint32_t*>(
s_xq + (tg * 8 + b) * 32 + l0 + tig * 4);
int32_t d[4] = {0, 0, 0, 0};
mma_s8_m16n8k16(d, a0, a1, b0);
const int t0 = nt * 8 + 2 * tig;
const int t1 = t0 + 1;
acc[nt][0] = fmaf(dw0 * s_dx[t0 * 8 + b], (float) d[0], acc[nt][0]);
acc[nt][1] = fmaf(dw0 * s_dx[t1 * 8 + b], (float) d[1], acc[nt][1]);
acc[nt][2] = fmaf(dw1 * s_dx[t0 * 8 + b], (float) d[2], acc[nt][2]);
acc[nt][3] = fmaf(dw1 * s_dx[t1 * 8 + b], (float) d[3], acc[nt][3]);
}
}
} else if constexpr (GTYPE == kTypeIQ4_XS) {
const float d0_all = v0 ? smem_ld_fp16_b2(blk0) : 0.0f;
const float d1_all = v1 ? smem_ld_fp16_b2(blk1) : 0.0f;
const uint16_t sh0 = v0 ? *reinterpret_cast<const uint16_t*>(blk0 + 2) : 0u;
const uint16_t sh1 = v1 ? *reinterpret_cast<const uint16_t*>(blk1 + 2) : 0u;
const uint32_t sl0 = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 4) : 0u;
const uint32_t sl1 = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 4) : 0u;
const int ls0 = (int) (((sl0 >> (4 * b)) & 0x0Fu) | (((sh0 >> (2 * b)) & 3u) << 4)) - 32;
const int ls1 = (int) (((sl1 >> (4 * b)) & 0x0Fu) | (((sh1 >> (2 * b)) & 3u) << 4)) - 32;
const float dw0 = v0 ? (d0_all * (float) ls0) : 0.0f;
const float dw1 = v1 ? (d1_all * (float) ls1) : 0.0f;
const uint32_t raw0 = v0 ? *reinterpret_cast<const uint32_t*>(blk0 + 8 + b * 16 + tig * 4) : 0u;
const uint32_t raw1 = v1 ? *reinterpret_cast<const uint32_t*>(blk1 + 8 + b * 16 + tig * 4) : 0u;
const uint2 w0_lh = iq4nl_lut8(raw0);
const uint2 w1_lh = iq4nl_lut8(raw1);
#pragma unroll
for (int nt = 0; nt < NTILES; ++nt) {
const int tg = nt * 8 + g;
const uint32_t b0 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + tig * 4);
const uint32_t b1 = *reinterpret_cast<const uint32_t*>(s_xq + (tg * 8 + b) * 32 + 16 + tig * 4);
int32_t d[4] = {0, 0, 0, 0};
mma_s8_m16n8k32(d, w0_lh.x, w1_lh.x, w0_lh.y, w1_lh.y, b0, b1);
const int t0 = nt * 8 + 2 * tig;
const int t1 = t0 + 1;
acc[nt][0] = fmaf(dw0 * s_dx[t0 * 8 + b], (float) d[0], acc[nt][0]);
acc[nt][1] = fmaf(dw0 * s_dx[t1 * 8 + b], (float) d[1], acc[nt][1]);
acc[nt][2] = fmaf(dw1 * s_dx[t0 * 8 + b], (float) d[2], acc[nt][2]);
acc[nt][3] = fmaf(dw1 * s_dx[t1 * 8 + b], (float) d[3], acc[nt][3]);
}
}
}
__syncthreads();
}
// Reduce the 8 warps' partial sums using s_w as scratch (8 * MAX_T * 16 floats <= 8 * 16 * 16 * 4 = 8 KB;
// use a dedicated small 4 KB / 8 KB smem buffer).
__shared__ float s_red[8][MAX_T][16];
#pragma unroll
for (int nt = 0; nt < NTILES; ++nt) {
const int t0 = nt * 8 + 2 * tig;
const int t1 = t0 + 1;
s_red[warp][t0][g] = acc[nt][0];
s_red[warp][t1][g] = acc[nt][1];
s_red[warp][t0][8 + g] = acc[nt][2];
s_red[warp][t1][8 + g] = acc[nt][3];
}
__syncthreads();
for (int idx = tid; idx < T * 16; idx += 256) {
const int tk = idx >> 4;
const int lr = idx & 15;
const int row = r_base + lr;
if (row < n_out) {
float sum = 0.0f;
#pragma unroll
for (int w = 0; w < 8; ++w) sum += s_red[w][tk][lr];
y[(size_t) tk * n_out + row] = sum;
}
}
#else
(void) weights; (void) xq; (void) y; (void) n_in; (void) n_out; (void) T;
#endif
}
// ---- Decode / Verify Kernel (legacy multi-warp, unused when ksplit runs) ----
template <int GTYPE, int NTILES, int WARPS = 2>
__global__ void __launch_bounds__(WARPS * 32) dense_i8_tc_decode_kernel(
const uint8_t* __restrict__ weights, const Q81Block* __restrict__ xq, float* __restrict__ y,
int n_in, int n_out, int T) {
@@ -411,12 +1021,11 @@ __global__ void __launch_bounds__(WARPS * 32) dense_i8_tc_decode_kernel(
const int nb32 = n_in >> 5;
const size_t rbytes = row_bytes_of(GTYPE, n_in);
const uint8_t* w0 = weights + (size_t) (v0 ? r0 : 0) * rbytes;
const uint8_t* w1 = weights + (size_t) (v1 ? r1 : 0) * rbytes;
__shared__ alignas(16) int8_t s_xq[MAX_T * 256];
__shared__ float s_dx[MAX_T * 8];
__shared__ float s_xs[MAX_T * 8];
__shared__ alignas(16) uint32_t s_w[WARPS][16][chunk_u32_of<GTYPE>()];
float acc[NTILES][4];
#pragma unroll
@@ -426,7 +1035,6 @@ __global__ void __launch_bounds__(WARPS * 32) dense_i8_tc_decode_kernel(
for (int k_blk0 = 0; k_blk0 < nb32; k_blk0 += 8) {
const int nb_here = min(8, nb32 - k_blk0);
// Load MAX_T * nb_here blocks (up to 128 Q81Blocks) cooperatively into ~5 KB smem!
for (int idx = tid; idx < MAX_T * 8; idx += NTHREADS) {
const int t = idx >> 3;
const int b = idx & 7;
@@ -435,32 +1043,35 @@ __global__ void __launch_bounds__(WARPS * 32) dense_i8_tc_decode_kernel(
const float dx = __low2float(blk.ds);
s_dx[idx] = dx;
const uint32_t* src32 = reinterpret_cast<const uint32_t*>(blk.qs);
const uint32_t w0 = src32[0], w1 = src32[1], w2 = src32[2], w3 = src32[3];
const uint32_t w4 = src32[4], w5 = src32[5], w6 = src32[6], w7 = src32[7];
int qsum = __dp4a(0x01010101, (int) w0, 0);
qsum = __dp4a(0x01010101, (int) w1, qsum);
qsum = __dp4a(0x01010101, (int) w2, qsum);
qsum = __dp4a(0x01010101, (int) w3, qsum);
qsum = __dp4a(0x01010101, (int) w4, qsum);
qsum = __dp4a(0x01010101, (int) w5, qsum);
qsum = __dp4a(0x01010101, (int) w6, qsum);
qsum = __dp4a(0x01010101, (int) w7, qsum);
s_xs[idx] = dx * (float) qsum;
const uint32_t x0 = src32[0], x1 = src32[1], x2 = src32[2], x3 = src32[3];
const uint32_t x4 = src32[4], x5 = src32[5], x6 = src32[6], x7 = src32[7];
if constexpr (GTYPE == kTypeQ4_K || GTYPE == kTypeQ5_K) {
int qsum = __dp4a(0x01010101, (int) x0, 0);
qsum = __dp4a(0x01010101, (int) x1, qsum);
qsum = __dp4a(0x01010101, (int) x2, qsum);
qsum = __dp4a(0x01010101, (int) x3, qsum);
qsum = __dp4a(0x01010101, (int) x4, qsum);
qsum = __dp4a(0x01010101, (int) x5, qsum);
qsum = __dp4a(0x01010101, (int) x6, qsum);
qsum = __dp4a(0x01010101, (int) x7, qsum);
s_xs[idx] = dx * (float) qsum;
}
uint4* dst4 = reinterpret_cast<uint4*>(s_xq + idx * 32);
dst4[0] = make_uint4(w0, w1, w2, w3);
dst4[1] = make_uint4(w4, w5, w6, w7);
dst4[0] = make_uint4(x0, x1, x2, x3);
dst4[1] = make_uint4(x4, x5, x6, x7);
} else {
s_dx[idx] = 0.0f;
s_xs[idx] = 0.0f;
if constexpr (GTYPE == kTypeQ4_K || GTYPE == kTypeQ5_K) s_xs[idx] = 0.0f;
uint4* dst4 = reinterpret_cast<uint4*>(s_xq + idx * 32);
dst4[0] = make_uint4(0u, 0u, 0u, 0u);
dst4[1] = make_uint4(0u, 0u, 0u, 0u);
}
}
stage_dense_rows16<GTYPE>(weights, r_base, n_out, rbytes, k_blk0, nb_here, lane, s_w[warp]);
__syncthreads();
if (v0 || v1) {
eval_kchunk_tc<GTYPE, NTILES>(w0, w1, v0, v1, nb_here, k_blk0, s_xq, s_dx, s_xs, g, tig, acc);
if (r_base < n_out) {
eval_kchunk_tc_smem<GTYPE, NTILES>(s_w[warp], v0, v1, nb_here, s_xq, s_dx, s_xs, g, tig, acc);
}
__syncthreads();
}
@@ -659,23 +1270,16 @@ bool dense_i8_tc_mmvq(int ggml_type, const void* weights, const void* x_q8_1, fl
if ((n_in % align_k) != 0) return false;
const cudaStream_t s = static_cast<cudaStream_t>(stream);
const bool narrow_n = (n_out <= 4096);
const unsigned grid = narrow_n ? (unsigned) ((n_out + 15) / 16) : (unsigned) ((n_out + 63) / 64);
const unsigned grid = (unsigned) ((n_out + 15) / 16);
const auto* w = reinterpret_cast<const uint8_t*>(weights);
const auto* xq = reinterpret_cast<const Q81Block*>(x_q8_1);
auto launch = [&](auto type_tag) {
constexpr int GT = decltype(type_tag)::value;
if (narrow_n) {
if (T <= 8) {
dense_i8_tc_decode_kernel<GT, 1, 1><<<grid, 32, 0, s>>>(w, xq, y, n_in, n_out, T);
} else {
dense_i8_tc_decode_kernel<GT, 2, 1><<<grid, 32, 0, s>>>(w, xq, y, n_in, n_out, T);
}
} else if (T <= 8) {
dense_i8_tc_decode_kernel<GT, 1, 4><<<grid, 128, 0, s>>>(w, xq, y, n_in, n_out, T);
if (T <= 8) {
dense_i8_tc_decode_ksplit_kernel<GT, 1><<<grid, 256, 0, s>>>(w, xq, y, n_in, n_out, T);
} else {
dense_i8_tc_decode_kernel<GT, 2, 4><<<grid, 128, 0, s>>>(w, xq, y, n_in, n_out, T);
dense_i8_tc_decode_ksplit_kernel<GT, 2><<<grid, 256, 0, s>>>(w, xq, y, n_in, n_out, T);
}
};
+324 -1
View File
@@ -68,6 +68,11 @@ __global__ void rope_inv_kernel(float theta, double* __restrict__ inv) {
inv[i] = pow((double) theta, -(double) (2 * i) / (double) HD);
}
__global__ void iota_pos_kernel(int32_t* __restrict__ pos, int32_t pos0, int rows) {
const int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < rows) pos[i] = pos0 + i;
}
__global__ void rope_table_inv_kernel(const int32_t* __restrict__ pos, int rows, const double* __restrict__ inv_tab, float* __restrict__ table) {
const int r = blockIdx.x, i = threadIdx.x; // 64 threads
if (r >= rows) return;
@@ -805,6 +810,260 @@ __global__ void __launch_bounds__(WARPS * 32) gemm4_rows_kernel(Df2Segs segs, co
}
}
// Clean-room MIT sm_80+ INT8 Tensor Core DFlash2 GEMM (Ampere RTX 3090 + Blackwell RTX 5090 / PRO 5000).
// Uses PTX mma.sync.aligned.m16n8k32.row.col.s32.s8.s8.s32 matching DFlash2's exact ncols <= 8 shape:
// each 128-thread CTA owns 16 output rows (two 8-row tiled groups), stages the 512-K activation step in smem (4.5 KB),
// and splits the 16 K-blocks across 4 warps with 16-byte coalesced uint4 loads on Q4_0 weights (exact signed q - 8,
// zero xsum overhead, zero weight smem, zero spills).
#if STRATA_DF2_TC_SM80
__device__ __forceinline__ void df2_mma_s8_m16n8k32(int32_t (&d)[4], uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3,
uint32_t b0, uint32_t b1) {
asm volatile(
"mma.sync.aligned.m16n8k32.row.col.s32.s8.s8.s32 "
"{%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9}, {%0, %1, %2, %3};\n"
: "+r"(d[0]), "+r"(d[1]), "+r"(d[2]), "+r"(d[3])
: "r"(a0), "r"(a1), "r"(a2), "r"(a3), "r"(b0), "r"(b1));
}
// Exact bit-parallel conversion of four 4-bit unsigned nibbles q in [0..15] (in bytes 0..3) to signed int8 (q - 8) in [-8..7].
__device__ __forceinline__ uint32_t df2_u4x4_to_s8x4(uint32_t q) {
return (q & 0x07070707u) | ((~q & 0x08080808u) * 0x1Fu);
}
#endif
template <bool Q4>
__global__ void __launch_bounds__(128) gemm_tc_kernel(Df2Segs segs, const int8_t* __restrict__ xq,
const float* __restrict__ xd, int n_in, int ncols) {
#if STRATA_DF2_TC_SM80
__shared__ alignas(16) int8_t s_xq[8][512]; // 4,096 B
__shared__ float s_xd[8][16]; // 512 B
__shared__ float s_out[4][16][8]; // 2,048 B (total 6,656 B -> max occupancy)
const int tid = threadIdx.x;
const int warp = tid >> 5;
const int lane = tid & 31;
const int g = lane >> 2;
const int tig = lane & 3;
const int t0 = 2 * tig;
const int t1 = t0 + 1;
const int row_g = blockIdx.x * 16;
const Df2Seg sg = df2_pick(segs, row_g);
const int row0 = row_g - sg.row0;
const int nb = n_in >> 5;
const int steps = nb >> 4;
const uint8_t* base_q0 = reinterpret_cast<const uint8_t*>(sg.q) + (size_t) row0 * (Q4 ? (n_in >> 1) : n_in);
const uint8_t* base_q1 = base_q0 + (size_t) 8 * (Q4 ? (n_in >> 1) : n_in);
const __half* base_d0 = sg.d + (size_t) row0 * nb;
const __half* base_d1 = base_d0 + (size_t) 8 * nb;
float acc[4] = {0.0f, 0.0f, 0.0f, 0.0f};
const int n_in_i4 = n_in >> 4;
const int4* xq4 = reinterpret_cast<const int4*>(xq);
for (int st = 0; st < steps; ++st) {
// Cooperatively stage 512 int8 activations + 16 scales for all ncols <= 8 columns (16-byte coalesced).
const int tot_i4 = ncols * 32;
for (int i = tid; i < tot_i4; i += 128) {
const int c = i >> 5;
const int k4 = i & 31;
*reinterpret_cast<int4*>(&s_xq[c][k4 * 16]) = __ldg(xq4 + (size_t) c * n_in_i4 + st * 32 + k4);
}
if (tid < ncols * 16) {
const int c = tid >> 4;
const int bi = tid & 15;
s_xd[c][bi] = __ldg(xd + (size_t) c * nb + st * 16 + bi);
}
__syncthreads();
#pragma unroll
for (int s = 0; s < 4; ++s) {
const int bi = warp + s * 4;
uint32_t a0, a1, a2, a3;
if constexpr (Q4) {
const uint4 r0 = __ldg(reinterpret_cast<const uint4*>(base_q0 + (size_t) st * 2048 + g * 256 + bi * 16));
const uint4 r1 = __ldg(reinterpret_cast<const uint4*>(base_q1 + (size_t) st * 2048 + g * 256 + bi * 16));
const uint32_t w0_lo = (tig < 2) ? r0.x : r0.y;
const uint32_t w0_hi = (tig < 2) ? r0.z : r0.w;
const uint32_t w1_lo = (tig < 2) ? r1.x : r1.y;
const uint32_t w1_hi = (tig < 2) ? r1.z : r1.w;
const int sh = (tig & 1) ? 4 : 0;
a0 = df2_u4x4_to_s8x4((w0_lo >> sh) & 0x0F0F0F0Fu);
a2 = df2_u4x4_to_s8x4((w0_hi >> sh) & 0x0F0F0F0Fu);
a1 = df2_u4x4_to_s8x4((w1_lo >> sh) & 0x0F0F0F0Fu);
a3 = df2_u4x4_to_s8x4((w1_hi >> sh) & 0x0F0F0F0Fu);
} else {
const uint8_t* p0 = base_q0 + (size_t) st * 4096 + g * 512 + bi * 32;
const uint8_t* p1 = base_q1 + (size_t) st * 4096 + g * 512 + bi * 32;
a0 = __ldg(reinterpret_cast<const uint32_t*>(p0 + tig * 4));
a2 = __ldg(reinterpret_cast<const uint32_t*>(p0 + 16 + tig * 4));
a1 = __ldg(reinterpret_cast<const uint32_t*>(p1 + tig * 4));
a3 = __ldg(reinterpret_cast<const uint32_t*>(p1 + 16 + tig * 4));
}
const float dw0 = __half2float(__ldg(base_d0 + (size_t) st * 128 + g * 16 + bi));
const float dw1 = __half2float(__ldg(base_d1 + (size_t) st * 128 + g * 16 + bi));
const uint32_t b0 = (g < ncols) ? *reinterpret_cast<const uint32_t*>(&s_xq[g][bi * 32 + tig * 4]) : 0u;
const uint32_t b1 = (g < ncols) ? *reinterpret_cast<const uint32_t*>(&s_xq[g][bi * 32 + 16 + tig * 4]) : 0u;
int32_t d[4] = {0, 0, 0, 0};
df2_mma_s8_m16n8k32(d, a0, a1, a2, a3, b0, b1);
if (t0 < ncols) {
const float dx0 = s_xd[t0][bi];
acc[0] = fmaf(dw0 * dx0, (float) d[0], acc[0]);
acc[2] = fmaf(dw1 * dx0, (float) d[2], acc[2]);
}
if (t1 < ncols) {
const float dx1 = s_xd[t1][bi];
acc[1] = fmaf(dw0 * dx1, (float) d[1], acc[1]);
acc[3] = fmaf(dw1 * dx1, (float) d[3], acc[3]);
}
}
__syncthreads();
}
s_out[warp][g][t0] = acc[0];
s_out[warp][g][t1] = acc[1];
s_out[warp][8 + g][t0] = acc[2];
s_out[warp][8 + g][t1] = acc[3];
__syncthreads();
const int r = tid >> 3;
const int c = tid & 7;
if (c < ncols) {
const float sum = (s_out[0][r][c] + s_out[1][r][c]) + (s_out[2][r][c] + s_out[3][r][c]);
sg.y[(size_t) c * sg.n_out + row0 + r] = sum;
}
#else
(void) segs; (void) xq; (void) xd; (void) n_in; (void) ncols;
#endif
}
// Clean-room MIT sm_80+ batched INT8 Tensor Core DFlash2 GEMM for arbitrary ncols (prefill context updates):
// each 128-thread CTA owns 16 output rows x up to 64 columns, reading weights ONCE per 64 tokens (8x fewer weight loads!).
template <bool Q4>
__global__ void __launch_bounds__(128) gemm_batch_tc_kernel(Df2Segs segs, const int8_t* __restrict__ xq,
const float* __restrict__ xd, int n_in, int ncols) {
#if STRATA_DF2_TC_SM80
__shared__ alignas(16) int8_t s_xq[64][128]; // 8,192 B (64 cols x 4 blocks of 32)
__shared__ float s_xd[64][4]; // 1,024 B
__shared__ float s_out[4][16][8]; // 2,048 B (total 11.2 KB)
const int tid = threadIdx.x;
const int warp = tid >> 5;
const int lane = tid & 31;
const int g = lane >> 2;
const int tig = lane & 3;
const int t0 = 2 * tig;
const int t1 = t0 + 1;
const int row_g = blockIdx.x * 16;
const int c_base = blockIdx.y * 64;
const int nc = min(64, ncols - c_base);
if (nc <= 0) return;
const Df2Seg sg = df2_pick(segs, row_g);
const int row0 = row_g - sg.row0;
const int nb = n_in >> 5;
const int steps = nb >> 4;
const int n_in_i4 = n_in >> 4;
const int4* xq4 = reinterpret_cast<const int4*>(xq);
const uint8_t* base_q0 = reinterpret_cast<const uint8_t*>(sg.q) + (size_t) row0 * (Q4 ? (n_in >> 1) : n_in);
const uint8_t* base_q1 = base_q0 + (size_t) 8 * (Q4 ? (n_in >> 1) : n_in);
const __half* base_d0 = sg.d + (size_t) row0 * nb;
const __half* base_d1 = base_d0 + (size_t) 8 * nb;
float acc[8][4];
#pragma unroll
for (int ct = 0; ct < 8; ++ct) acc[ct][0] = acc[ct][1] = acc[ct][2] = acc[ct][3] = 0.0f;
const int tot_i4 = nc * 8;
const int tot_d = nc * 4;
for (int st = 0; st < steps; ++st) {
for (int s = 0; s < 4; ++s) {
for (int i = tid; i < tot_i4; i += 128) {
const int c = i >> 3;
const int k4 = i & 7;
*reinterpret_cast<int4*>(&s_xq[c][k4 * 16]) =
__ldg(xq4 + (size_t) (c_base + c) * n_in_i4 + st * 32 + s * 8 + k4);
}
for (int i = tid; i < tot_d; i += 128) {
const int c = i >> 2;
const int bi4 = i & 3;
s_xd[c][bi4] = __ldg(xd + (size_t) (c_base + c) * nb + st * 16 + s * 4 + bi4);
}
__syncthreads();
const int bi = s * 4 + warp;
uint32_t a0, a1, a2, a3;
if constexpr (Q4) {
const uint4 r0 = __ldg(reinterpret_cast<const uint4*>(base_q0 + (size_t) st * 2048 + g * 256 + bi * 16));
const uint4 r1 = __ldg(reinterpret_cast<const uint4*>(base_q1 + (size_t) st * 2048 + g * 256 + bi * 16));
const uint32_t w0_lo = (tig < 2) ? r0.x : r0.y;
const uint32_t w0_hi = (tig < 2) ? r0.z : r0.w;
const uint32_t w1_lo = (tig < 2) ? r1.x : r1.y;
const uint32_t w1_hi = (tig < 2) ? r1.z : r1.w;
const int sh = (tig & 1) ? 4 : 0;
a0 = df2_u4x4_to_s8x4((w0_lo >> sh) & 0x0F0F0F0Fu);
a2 = df2_u4x4_to_s8x4((w0_hi >> sh) & 0x0F0F0F0Fu);
a1 = df2_u4x4_to_s8x4((w1_lo >> sh) & 0x0F0F0F0Fu);
a3 = df2_u4x4_to_s8x4((w1_hi >> sh) & 0x0F0F0F0Fu);
} else {
const uint8_t* p0 = base_q0 + (size_t) st * 4096 + g * 512 + bi * 32;
const uint8_t* p1 = base_q1 + (size_t) st * 4096 + g * 512 + bi * 32;
a0 = __ldg(reinterpret_cast<const uint32_t*>(p0 + tig * 4));
a2 = __ldg(reinterpret_cast<const uint32_t*>(p0 + 16 + tig * 4));
a1 = __ldg(reinterpret_cast<const uint32_t*>(p1 + tig * 4));
a3 = __ldg(reinterpret_cast<const uint32_t*>(p1 + 16 + tig * 4));
}
const float dw0 = __half2float(__ldg(base_d0 + (size_t) st * 128 + g * 16 + bi));
const float dw1 = __half2float(__ldg(base_d1 + (size_t) st * 128 + g * 16 + bi));
#pragma unroll
for (int ct = 0; ct < 8; ++ct) {
const int c0 = ct * 8;
if (c0 >= nc) break;
const int cg = c0 + g;
const uint32_t b0 = (cg < nc) ? *reinterpret_cast<const uint32_t*>(&s_xq[cg][warp * 32 + tig * 4]) : 0u;
const uint32_t b1 = (cg < nc) ? *reinterpret_cast<const uint32_t*>(&s_xq[cg][warp * 32 + 16 + tig * 4]) : 0u;
int32_t d[4] = {0, 0, 0, 0};
df2_mma_s8_m16n8k32(d, a0, a1, a2, a3, b0, b1);
if (c0 + t0 < nc) {
const float dx0 = s_xd[c0 + t0][warp];
acc[ct][0] = fmaf(dw0 * dx0, (float) d[0], acc[ct][0]);
acc[ct][2] = fmaf(dw1 * dx0, (float) d[2], acc[ct][2]);
}
if (c0 + t1 < nc) {
const float dx1 = s_xd[c0 + t1][warp];
acc[ct][1] = fmaf(dw0 * dx1, (float) d[1], acc[ct][1]);
acc[ct][3] = fmaf(dw1 * dx1, (float) d[3], acc[ct][3]);
}
}
__syncthreads();
}
}
const int r = tid >> 3;
const int c = tid & 7;
for (int ct = 0; ct < 8; ++ct) {
const int c0 = ct * 8;
if (c0 >= nc) break;
s_out[warp][g][t0] = acc[ct][0];
s_out[warp][g][t1] = acc[ct][1];
s_out[warp][8 + g][t0] = acc[ct][2];
s_out[warp][8 + g][t1] = acc[ct][3];
__syncthreads();
if (c0 + c < nc) {
const float sum = (s_out[0][r][c] + s_out[1][r][c]) + (s_out[2][r][c] + s_out[3][r][c]);
sg.y[(size_t) (c_base + c0 + c) * sg.n_out + row0 + r] = sum;
}
__syncthreads();
}
#else
(void) segs; (void) xq; (void) xd; (void) n_in; (void) ncols;
#endif
}
// quantize one 32-block of one row to the planar activation layout (a warp: lane = element)
__device__ __forceinline__ void quant_block(float v, int blk, int col, int K, int8_t* __restrict__ xq, float* __restrict__ xd) {
const int lane = threadIdx.x & 31;
@@ -1255,6 +1514,12 @@ void df2_rope_table_inv(const int32_t* pos, int rows, const double* inv, float*
check("rope_table_inv");
}
void df2_iota_pos(int32_t* pos, int32_t pos0, int rows, void* stream) {
if (rows <= 0) return;
iota_pos_kernel<<<(rows + 255) / 256, 256, 0, (cudaStream_t) stream>>>(pos, pos0, rows);
check("iota_pos");
}
void df2_gemm_variant(int v) { g_gemm_variant = v; }
void df2_attention_variant(int v) { g_attn_variant = v; }
@@ -1280,7 +1545,7 @@ static int df2_q4_auto(int rows) {
#define DF2_Q4_AUTO(rows) df2_q4_auto(rows)
#endif
void df2_gemm(const Df2Mat* mats, int n, const void* xq, const float* xd, int n_in, int ncols, void* stream) {
if (n < 1 || n > 3 || n_in % 512 != 0 || ncols < 1 || ncols > 8) throw std::invalid_argument("df2_gemm: unsupported shape");
if (n < 1 || n > 3 || n_in % 512 != 0 || ncols < 1) throw std::invalid_argument("df2_gemm: unsupported shape");
Df2Segs segs;
int rows = 0;
for (int i = 0; i < n; ++i) {
@@ -1293,6 +1558,64 @@ void df2_gemm(const Df2Mat* mats, int n, const void* xq, const float* xd, int n_
segs.n = n;
const int8_t* xqp = static_cast<const int8_t*>(xq);
cudaStream_t st = (cudaStream_t) stream;
if (ncols > 8) {
#if !defined(__HIPCC__)
static const bool batch_tc_ok = [] {
int dev = 0, major = 0;
if (cudaGetDevice(&dev) != cudaSuccess ||
cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev) != cudaSuccess) {
(void) cudaGetLastError();
return false;
}
return major >= 8;
}();
if (batch_tc_ok && (rows & 15) == 0) {
const dim3 grid((unsigned) (rows / 16), (unsigned) ((ncols + 63) / 64));
if (mats[0].q4) {
gemm_batch_tc_kernel<true><<<grid, 128, 0, st>>>(segs, xqp, xd, n_in, ncols);
check("gemm4_batch_tc");
} else {
gemm_batch_tc_kernel<false><<<grid, 128, 0, st>>>(segs, xqp, xd, n_in, ncols);
check("gemm8_batch_tc");
}
return;
}
#endif
const int nb = n_in / 32;
for (int c0 = 0; c0 < ncols; c0 += 8) {
const int nc = (ncols - c0 < 8) ? (ncols - c0) : 8;
Df2Mat sub[3];
for (int i = 0; i < n; ++i) {
sub[i] = mats[i];
sub[i].y = mats[i].y + (size_t) c0 * (size_t) mats[i].n_out;
}
df2_gemm(sub, n, xqp + (size_t) c0 * (size_t) n_in, xd + (size_t) c0 * (size_t) nb, n_in, nc, stream);
}
return;
}
#if !defined(__HIPCC__)
static const bool tc_gemm_ok = [] {
const char* e = std::getenv("STRATA_DF2_GEMM_TC");
if (e == nullptr || e[0] == '\0' || e[0] == '0') return false;
int dev = 0, major = 0;
if (cudaGetDevice(&dev) != cudaSuccess ||
cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev) != cudaSuccess) {
(void) cudaGetLastError();
return false;
}
return major >= 8;
}();
if (tc_gemm_ok && g_gemm_variant == 0 && ncols >= 2 && rows >= 1024 && (rows & 15) == 0) {
if (mats[0].q4) {
gemm_tc_kernel<true><<<rows / 16, 128, 0, st>>>(segs, xqp, xd, n_in, ncols);
check("gemm4_tc");
} else {
gemm_tc_kernel<false><<<rows / 16, 128, 0, st>>>(segs, xqp, xd, n_in, ncols);
check("gemm8_tc");
}
return;
}
#endif
if (mats[0].q4) { // the Q4_0 pack: 4 warps a block when the rows allow, else 2; the rows-a-warp variants (bitwise the same results) by matrix size
static const int tune4 = [] { const char* e = std::getenv("STRATA_DF2_GEMM4"); return e ? std::atoi(e) : 0; }(); // 0: the table; 1: 8 rows a warp as before; 10 and up: that variant for every matrix (bench / A/B)
int v4 = g_gemm_variant != 0 ? g_gemm_variant : tune4 >= 10 ? tune4 : tune4 != 0 ? 1 : DF2_Q4_AUTO(rows);
+440 -94
View File
@@ -15,6 +15,9 @@
#include "s26_tsum.cuh"
#include <cuda_fp16.h>
#if !defined(__HIPCC__)
#include <cuda_bf16.h>
#endif
#include <cuda_runtime.h>
#include <atomic>
@@ -255,13 +258,14 @@ struct GrMulti {
// d = amax / 127, roundf(x / d), ds = (d, sum) - the bytes the separate launch writes.
struct GrQ81 { half2 ds; int8_t qs[32]; };
static_assert(2 * 16 == 32, "two up blocks per q8_1 group");
__device__ __forceinline__ void gr_q8_tail(const GrMulti& m, int d0) {
template <int BLOCKS_PER_Q8 = 2>
__device__ __forceinline__ void gr_q8_tail_n(const GrMulti& m, int d0) {
__shared__ int last;
__threadfence();
__syncthreads();
if (threadIdx.x == 0) {
unsigned* c = m.a[0].q8_cnt + d0 / 32;
last = atomicAdd(c, 1u) == 1u;
last = atomicAdd(c, 1u) == (unsigned) (BLOCKS_PER_Q8 - 1);
if (last) atomicExch(c, 0u);
}
__syncthreads();
@@ -282,6 +286,7 @@ __device__ __forceinline__ void gr_q8_tail(const GrMulti& m, int d0) {
y->qs[lane] = q;
if (lane == 0) y->ds = q8_1_ds(d, sum); // #606: clamped scale/sum - an unclamped pair NaN-poisons the dot path
}
__device__ __forceinline__ void gr_q8_tail(const GrMulti& m, int d0) { gr_q8_tail_n<2>(m, d0); }
// Step 1 of `gr_down_kernel`, one block per token, same threads and reduction order: rs[t] and xn[t] to global.
__global__ void __launch_bounds__(THREADS) gr_norm_multi_kernel(GrMulti m) {
__shared__ float part[WARPS][HC];
@@ -2621,26 +2626,48 @@ __device__ __forceinline__ void gr_mma_s8_m16n8k32(int32_t (&d)[4], uint32_t a0,
: "+r"(d[0]), "+r"(d[1]), "+r"(d[2]), "+r"(d[3])
: "r"(a0), "r"(a1), "r"(a2), "r"(a3), "r"(b0), "r"(b1));
}
__device__ __forceinline__ void gr_mma_bf16_m16n8k16(float (&d)[4], uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3,
uint32_t b0, uint32_t b1) {
asm volatile(
"mma.sync.aligned.m16n8k16.row.col.f32.bf16.bf16.f32 "
"{%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9}, {%0, %1, %2, %3};\n"
: "+f"(d[0]), "+f"(d[1]), "+f"(d[2]), "+f"(d[3])
: "r"(a0), "r"(a1), "r"(a2), "r"(a3), "r"(b0), "r"(b1));
}
__device__ __forceinline__ uint32_t gr_f32x2_to_bf16x2(float f0, float f1) {
uint32_t out;
asm("cvt.rn.bf16x2.f32 %0, %2, %1;" : "=r"(out) : "f"(f0), "f"(f1));
return out;
}
__device__ __forceinline__ uint32_t gr_i8x2_to_bf16x2(int8_t v0, int8_t v1) {
return gr_f32x2_to_bf16x2((float) v0, (float) v1);
}
#endif
// Clean-room MIT sm_80+ BF16 Tensor Core Q8_0 hyper-connection read (STRATA_HC_Q8_BF16TC, default ON on sm_80+ CUDA
// when STRATA_HC_Q8=1; =0 disables). Every int8 weight in [-128..127] is bit-exact in BF16 (8-bit significand), and
// BF16 activations preserve full FP32 exponent range (zero outlier clipping on R*w_norm and 320-dim SiLU(lo)).
// Coalesced warp-wide weight loads into smem + mma.m16n8k16.f32.bf16.bf16.f32 across all T<=8 tokens simultaneously.
template <int T>
__global__ void __launch_bounds__(THREADS) gr_down_q8_tc_kernel(GrMulti m, float* __restrict__ part, float* __restrict__ ssg) {
__global__ void __launch_bounds__(THREADS) gr_down_q8_bf16tc_kernel(GrMulti m, float* __restrict__ part,
float* __restrict__ ssg) {
#if STRATA_GR_Q8_TC_SM80
constexpr int NB = Q8_KC / 32; // 20 Q8_0 blocks for Flash-Next (640 cols)
constexpr int ROW_BYTES = NB * Q8B; // 680 bytes per row chunk (multiple of 4!)
constexpr int ROW_BYTES = NB * Q8B; // 680 bytes per row chunk (multiple of 4)
constexpr int ROW_U32 = ROW_BYTES / 4; // 170 uint32s per row chunk
constexpr int TC_RG = LR / 32; // 10 row groups of 32 rows (+1 inject group)
__shared__ alignas(16) int8_t xs_q[8][Q8_KC]; // 5,120 B
__shared__ float xs_d[8][NB]; // 640 B
__shared__ float red_b[8][NB]; // 640 B (deterministic, zero-spill ssp per block)
__shared__ alignas(16) uint32_t s_w[16][ROW_U32]; // 10,880 B (16 rows x 680 B, 100% coalesced load!)
__shared__ float p_acc[WARPS][8][16]; // 4,096 B (total ~21 KB -> 4+ CTAs/SM!)
__shared__ alignas(16) uint16_t xs_bf[8][Q8_KC]; // 10,240 B (BF16 activations, full FP32 exponent range)
__shared__ float red_b[8][NB]; // 640 B (deterministic ssp per block)
__shared__ alignas(16) uint32_t s_w[16][ROW_U32]; // 10,880 B (16 rows x 680 B, 100% coalesced load)
__shared__ float p_acc[WARPS][8][16]; // 4,096 B (total ~26 KB -> 3-4 CTAs/SM!)
const int t = threadIdx.x, lane = t & 31, warp = t >> 5;
const int rg = blockIdx.x, kc = blockIdx.y, c = kc / Q8_CPS;
const bool inj = (rg == TC_RG);
// All 8 warps (256 threads) cooperatively load R (+ pending write) * w_norm, accumulate ssp, and quantize into xs_q/xs_d.
for (int kb = warp; kb < T * NB; kb += WARPS) {
const int k = kb / NB;
const int b = kb - k * NB;
@@ -2654,15 +2681,7 @@ __global__ void __launch_bounds__(THREADS) gr_down_q8_tc_kernel(GrMulti m, float
const float ss_blk = warp_sum(r * r);
if (rg == 0 && lane == 0) red_b[k][b] = ss_blk;
const float xv = r * a.w_norm[col];
float amax = fabsf(xv);
#pragma unroll
for (int o = 16; o > 0; o >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, o));
const float dx = amax * (1.0f / 127.0f);
const float inv = dx > 0.0f ? (1.0f / dx) : 0.0f;
int qv = dx > 0.0f ? __float2int_rn(xv * inv) : 0;
qv = max(-127, min(127, qv));
xs_q[k][b * 32 + lane] = (int8_t) qv;
if (lane == 0) xs_d[k][b] = dx;
xs_bf[k][b * 32 + lane] = __bfloat16_as_ushort(__float2bfloat16_rn(xv));
}
__syncthreads();
if (rg == 0 && t < T) {
@@ -2685,16 +2704,22 @@ __global__ void __launch_bounds__(THREADS) gr_down_q8_tc_kernel(GrMulti m, float
for (int b = 0; b < NB; ++b) {
const float wv = __uint_as_float((uint32_t) wr[b * 32 + lane] << 16);
#pragma unroll
for (int k = 0; k < T; ++k) acc[k] = fmaf(wv * xs_d[k][b], (float) xs_q[k][b * 32 + lane], acc[k]);
for (int k = 0; k < T; ++k) {
const float xv = __uint_as_float((uint32_t) xs_bf[k][b * 32 + lane] << 16);
acc[k] = fmaf(wv, xv, acc[k]);
}
}
} else {
const uint8_t* wr = m.a[0].q8_inject + (size_t) r * (D / 32) * Q8B + (size_t) kc * ROW_BYTES;
for (int b = 0; b < NB; ++b) {
const uint8_t* blk = wr + b * Q8B;
const float dw = q8_half(q8_ld16(blk));
const int wq = (int) (int8_t) blk[2 + lane];
const float wv = dw * (float) (int8_t) blk[2 + lane];
#pragma unroll
for (int k = 0; k < T; ++k) acc[k] = fmaf(dw * xs_d[k][b], (float) (wq * (int) xs_q[k][b * 32 + lane]), acc[k]);
for (int k = 0; k < T; ++k) {
const float xv = __uint_as_float((uint32_t) xs_bf[k][b * 32 + lane] << 16);
acc[k] = fmaf(wv, xv, acc[k]);
}
}
}
#pragma unroll
@@ -2705,23 +2730,18 @@ __global__ void __launch_bounds__(THREADS) gr_down_q8_tc_kernel(GrMulti m, float
return;
}
// 32 output rows in 2 passes of 16 rows: each pass loads 16 rows x 680 B COALESCED (1 warp per row, 100% 128B hits)
// into s_w[16][170], then all 8 warps split the NB=20 Q8_0 blocks across 8 warps (2-3 MMAs per warp!).
const int g = lane >> 2;
const int tig = lane & 3;
const int t0 = 2 * tig;
const int t1 = t0 + 1;
const size_t row_stride = (size_t) (D / 32) * Q8B;
const uint8_t* base_w = m.a[0].q8_down + (size_t) kc * ROW_BYTES;
// Static balanced split of NB blocks across WARPS=8 warps:
const int b_start = (warp * NB) / WARPS;
const int b_end = ((warp + 1) * NB) / WARPS;
#pragma unroll
for (int half = 0; half < 2; ++half) {
const int r_base = rg * 32 + half * 16;
// 8 warps load 16 rows (2 rows per warp), all 32 lanes contiguous along each 680-byte row!
#pragma unroll
for (int rr = 0; rr < 2; ++rr) {
const int lr = warp * 2 + rr;
@@ -2742,24 +2762,24 @@ __global__ void __launch_bounds__(THREADS) gr_down_q8_tc_kernel(GrMulti m, float
const uint8_t* blk1 = w1 + b * Q8B;
const float dw0 = q8_half(q8_ld16(blk0));
const float dw1 = q8_half(q8_ld16(blk1));
const uint32_t a0 = q8_ld16(blk0 + 2 + tig * 4) | (q8_ld16(blk0 + 4 + tig * 4) << 16);
const uint32_t a2 = q8_ld16(blk0 + 18 + tig * 4) | (q8_ld16(blk0 + 20 + tig * 4) << 16);
const uint32_t a1 = q8_ld16(blk1 + 2 + tig * 4) | (q8_ld16(blk1 + 4 + tig * 4) << 16);
const uint32_t a3 = q8_ld16(blk1 + 18 + tig * 4) | (q8_ld16(blk1 + 20 + tig * 4) << 16);
const uint32_t b0 = (g < T) ? *reinterpret_cast<const uint32_t*>(&xs_q[g][b * 32 + tig * 4]) : 0u;
const uint32_t b1 = (g < T) ? *reinterpret_cast<const uint32_t*>(&xs_q[g][b * 32 + 16 + tig * 4]) : 0u;
int32_t d[4] = {0, 0, 0, 0};
gr_mma_s8_m16n8k32(d, a0, a1, a2, a3, b0, b1);
if (t0 < T) {
const float dx0 = xs_d[t0][b];
acc[0] = fmaf(dw0 * dx0, (float) d[0], acc[0]);
acc[2] = fmaf(dw1 * dx0, (float) d[2], acc[2]);
}
if (t1 < T) {
const float dx1 = xs_d[t1][b];
acc[1] = fmaf(dw0 * dx1, (float) d[1], acc[1]);
acc[3] = fmaf(dw1 * dx1, (float) d[3], acc[3]);
const int8_t* q0 = reinterpret_cast<const int8_t*>(blk0 + 2);
const int8_t* q1 = reinterpret_cast<const int8_t*>(blk1 + 2);
float db[4] = {0.0f, 0.0f, 0.0f, 0.0f};
#pragma unroll
for (int kh = 0; kh < 2; ++kh) {
const int k0 = kh * 16 + 2 * tig;
const uint32_t a0 = gr_i8x2_to_bf16x2(q0[k0], q0[k0 + 1]);
const uint32_t a1 = gr_i8x2_to_bf16x2(q1[k0], q1[k0 + 1]);
const uint32_t a2 = gr_i8x2_to_bf16x2(q0[k0 + 8], q0[k0 + 9]);
const uint32_t a3 = gr_i8x2_to_bf16x2(q1[k0 + 8], q1[k0 + 9]);
const uint32_t b0 = (g < T) ? *reinterpret_cast<const uint32_t*>(&xs_bf[g][b * 32 + k0]) : 0u;
const uint32_t b1 = (g < T) ? *reinterpret_cast<const uint32_t*>(&xs_bf[g][b * 32 + k0 + 8]) : 0u;
gr_mma_bf16_m16n8k16(db, a0, a1, a2, a3, b0, b1);
}
acc[0] = fmaf(dw0, db[0], acc[0]);
acc[1] = fmaf(dw0, db[1], acc[1]);
acc[2] = fmaf(dw1, db[2], acc[2]);
acc[3] = fmaf(dw1, db[3], acc[3]);
}
if (t0 < T) { p_acc[warp][t0][g] = acc[0]; p_acc[warp][t0][8 + g] = acc[2]; }
if (t1 < T) { p_acc[warp][t1][g] = acc[1]; p_acc[warp][t1][8 + g] = acc[3]; }
@@ -2781,24 +2801,26 @@ __global__ void __launch_bounds__(THREADS) gr_down_q8_tc_kernel(GrMulti m, float
}
template <int T>
__global__ void __launch_bounds__(THREADS) gr_up_q8_tc_kernel(GrMulti m, const float* __restrict__ part,
const float* __restrict__ ssg) {
__global__ void __launch_bounds__(THREADS) gr_up_q8_bf16tc_kernel(GrMulti m, const float* __restrict__ part,
const float* __restrict__ ssg) {
#if STRATA_GR_Q8_TC_SM80
constexpr int NB_UP = LR / 32; // 10 Q8_0 blocks for LR=320
constexpr int NB_HALF = NB_UP / 2; // 5 Q8_0 blocks per warp (8 warps = 4 streams x 2 K-halves)
constexpr int STREAM_BYTES = UPM_COLS * NB_UP * Q8B; // 16 * 340 = 5,440 contiguous bytes per stream!
constexpr int NB_HALF = NB_UP / 2; // 5 Q8_0 blocks per warp
constexpr int STREAM_BYTES = UPM_COLS * NB_UP * Q8B; // 16 * 340 = 5,440 contiguous bytes per stream
constexpr int STREAM_U32 = STREAM_BYTES / 4; // 1,360 uint32s per stream
__shared__ alignas(16) float lo[T][LR];
__shared__ alignas(16) int8_t lo_q[8][LR];
__shared__ float lo_d[8][NB_UP];
__shared__ alignas(16) uint16_t lo_bf[8][LR]; // 5,120 B (BF16 SiLU(lo), exact FP32 exponent range!)
__shared__ uint16_t s_i8_bf16[256]; // 512 B (bit-exact int8 -> bf16 LUT, zero I2F/CVT in loop!)
__shared__ float rsS[T][HC];
__shared__ float g_out[T][HC][UPM_COLS];
__shared__ float p1[HC][8][UPM_COLS];
__shared__ alignas(16) uint32_t s_w[HC][STREAM_U32]; // 21,760 B (4 streams x 5,440 B, 100% coalesced load!)
__shared__ alignas(16) uint32_t s_w[HC][STREAM_U32]; // 21,760 B (4 streams x 5,440 B coalesced)
const int t = threadIdx.x, lane = t & 31, warp = t >> 5;
const int d0 = blockIdx.x * UPM_COLS;
// Every thread initializes 1 entry of the exact int8 -> bf16 lookup table (256 threads = 256 entries, 1 cycle!).
s_i8_bf16[t] = __bfloat16_as_ushort(__float2bfloat16_rn((float) (int8_t) t));
if (t < T * HC) {
const int k = t / HC, c = t - k * HC;
float ss = 0.0f;
@@ -2822,30 +2844,13 @@ __global__ void __launch_bounds__(THREADS) gr_up_q8_tc_kernel(GrMulti m, const f
}
if (r < LR) {
const float x = sum / (float) HC;
lo[k][r] = x / (1.0f + __expf(-x));
const float s = x / (1.0f + __expf(-x));
lo_bf[k][r] = __bfloat16_as_ushort(__float2bfloat16_rn(s));
} else {
m.a[k].inject_out[r - LR] = sum;
}
}
__syncthreads();
// Quantize lo[T][LR] -> lo_q[T][LR] + lo_d[T][NB_UP] across all 8 warps (~0.2 us).
for (int kb = warp; kb < T * NB_UP; kb += WARPS) {
const int k = kb / NB_UP;
const int b = kb - k * NB_UP;
const float xv = lo[k][b * 32 + lane];
float amax = fabsf(xv);
#pragma unroll
for (int o = 16; o > 0; o >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, o));
const float dx = amax * (1.0f / 127.0f);
const float inv = dx > 0.0f ? (1.0f / dx) : 0.0f;
int qv = dx > 0.0f ? __float2int_rn(xv * inv) : 0;
qv = max(-127, min(127, qv));
lo_q[k][b * 32 + lane] = (int8_t) qv;
if (lane == 0) lo_d[k][b] = dx;
}
// Cooperatively load all 4 streams x 5,440 contiguous bytes COALESCED into s_w (64 threads per stream = 22 loads!).
const int c = warp & 3;
const int kh = warp >> 2;
{
@@ -2878,24 +2883,28 @@ __global__ void __launch_bounds__(THREADS) gr_up_q8_tc_kernel(GrMulti m, const f
const uint8_t* blk1 = w1 + b * Q8B;
const float dw0 = q8_half(q8_ld16(blk0));
const float dw1 = q8_half(q8_ld16(blk1));
const uint32_t a0 = q8_ld16(blk0 + 2 + tig * 4) | (q8_ld16(blk0 + 4 + tig * 4) << 16);
const uint32_t a2 = q8_ld16(blk0 + 18 + tig * 4) | (q8_ld16(blk0 + 20 + tig * 4) << 16);
const uint32_t a1 = q8_ld16(blk1 + 2 + tig * 4) | (q8_ld16(blk1 + 4 + tig * 4) << 16);
const uint32_t a3 = q8_ld16(blk1 + 18 + tig * 4) | (q8_ld16(blk1 + 20 + tig * 4) << 16);
const uint32_t b0 = (g < T) ? *reinterpret_cast<const uint32_t*>(&lo_q[g][b * 32 + tig * 4]) : 0u;
const uint32_t b1 = (g < T) ? *reinterpret_cast<const uint32_t*>(&lo_q[g][b * 32 + 16 + tig * 4]) : 0u;
int32_t d[4] = {0, 0, 0, 0};
gr_mma_s8_m16n8k32(d, a0, a1, a2, a3, b0, b1);
if (t0 < T) {
const float dx0 = lo_d[t0][b];
acc[0] = fmaf(dw0 * dx0, (float) d[0], acc[0]);
acc[2] = fmaf(dw1 * dx0, (float) d[2], acc[2]);
}
if (t1 < T) {
const float dx1 = lo_d[t1][b];
acc[1] = fmaf(dw0 * dx1, (float) d[1], acc[1]);
acc[3] = fmaf(dw1 * dx1, (float) d[3], acc[3]);
const int8_t* q0 = reinterpret_cast<const int8_t*>(blk0 + 2);
const int8_t* q1 = reinterpret_cast<const int8_t*>(blk1 + 2);
float db[4] = {0.0f, 0.0f, 0.0f, 0.0f};
#pragma unroll
for (int k2 = 0; k2 < 2; ++k2) {
const int k0 = k2 * 16 + 2 * tig;
const uint32_t a0 = (uint32_t) s_i8_bf16[(uint8_t) q0[k0]] |
((uint32_t) s_i8_bf16[(uint8_t) q0[k0 + 1]] << 16);
const uint32_t a1 = (uint32_t) s_i8_bf16[(uint8_t) q1[k0]] |
((uint32_t) s_i8_bf16[(uint8_t) q1[k0 + 1]] << 16);
const uint32_t a2 = (uint32_t) s_i8_bf16[(uint8_t) q0[k0 + 8]] |
((uint32_t) s_i8_bf16[(uint8_t) q0[k0 + 9]] << 16);
const uint32_t a3 = (uint32_t) s_i8_bf16[(uint8_t) q1[k0 + 8]] |
((uint32_t) s_i8_bf16[(uint8_t) q1[k0 + 9]] << 16);
const uint32_t b0 = (g < T) ? *reinterpret_cast<const uint32_t*>(&lo_bf[g][b * 32 + k0]) : 0u;
const uint32_t b1 = (g < T) ? *reinterpret_cast<const uint32_t*>(&lo_bf[g][b * 32 + k0 + 8]) : 0u;
gr_mma_bf16_m16n8k16(db, a0, a1, a2, a3, b0, b1);
}
acc[0] = fmaf(dw0, db[0], acc[0]);
acc[1] = fmaf(dw0, db[1], acc[1]);
acc[2] = fmaf(dw1, db[2], acc[2]);
acc[3] = fmaf(dw1, db[3], acc[3]);
}
if (kh == 1) {
@@ -2943,16 +2952,321 @@ __global__ void __launch_bounds__(THREADS) gr_up_q8_tc_kernel(GrMulti m, const f
#endif
}
template <int T>
__global__ void __launch_bounds__(THREADS) gr_down_q8_tc_kernel(GrMulti m, float* __restrict__ part, float* __restrict__ ssg) {
#if STRATA_GR_Q8_TC_SM80
constexpr int NB = Q8_KC / 32; // 20 Q8_0 blocks for Flash-Next (640 cols)
constexpr int ROW_BYTES = NB * Q8B; // 680 bytes per row chunk (multiple of 8!)
constexpr int ROW_U2 = ROW_BYTES / 8; // 85 uint2s per row chunk
constexpr int TC_RG = LR / 16; // 20 row groups of 16 rows (+1 inject group -> 336 CTAs!)
__shared__ alignas(16) int8_t xs_q[8][Q8_KC]; // 5,120 B
__shared__ float xs_d[8][NB]; // 640 B
__shared__ float red_b[8][NB]; // 640 B (deterministic, zero-spill ssp per block)
__shared__ alignas(16) uint2 s_w[16][ROW_U2]; // 10,880 B (16 rows x 680 B, 100% aligned uint2 load!)
__shared__ float p_acc[WARPS][8][16]; // 4,096 B (total ~21.4 KB -> 4+ CTAs/SM!)
const int t = threadIdx.x, lane = t & 31, warp = t >> 5;
const int rg = blockIdx.x, kc = blockIdx.y, c = kc / Q8_CPS;
const bool inj = (rg == TC_RG);
// All 8 warps (256 threads) cooperatively load R (+ pending write) * w_norm, accumulate ssp, and quantize into xs_q/xs_d.
for (int kb = warp; kb < T * NB; kb += WARPS) {
const int k = kb / NB;
const int b = kb - k * NB;
const FusedGrArgs& a = m.a[k];
const int col = kc * Q8_KC + b * 32 + lane;
float r = a.R[col];
if (a.apply) {
const float gw = 2.0f * sigmoidf_(a.inj_prev[c] / (float) HC);
r = fmaf(a.bo_prev[col - c * N], gw, r);
}
const float ss_blk = warp_sum(r * r);
if (rg == 0 && lane == 0) red_b[k][b] = ss_blk;
const float xv = r * a.w_norm[col];
float amax = fabsf(xv);
#pragma unroll
for (int o = 16; o > 0; o >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, o));
const float dx = amax * (1.0f / 127.0f);
const float inv = dx > 0.0f ? (1.0f / dx) : 0.0f;
int qv = dx > 0.0f ? __float2int_rn(xv * inv) : 0;
qv = max(-127, min(127, qv));
xs_q[k][b * 32 + lane] = (int8_t) qv;
if (lane == 0) xs_d[k][b] = dx;
}
const size_t row_stride = (size_t) (D / 32) * Q8B;
const uint8_t* base_w = m.a[0].q8_down + (size_t) kc * ROW_BYTES;
const int r_base = rg * 16;
// Overlap 16-row coalesced uint2 weight loads with activation staging before the single sync!
if (!inj) {
#pragma unroll
for (int rr = 0; rr < 2; ++rr) {
const int lr = warp * 2 + rr;
const uint2* src2 = reinterpret_cast<const uint2*>(base_w + (size_t) (r_base + lr) * row_stride);
for (int u = lane; u < ROW_U2; u += 32) s_w[lr][u] = __ldg(src2 + u);
}
}
__syncthreads();
if (rg == 0 && t < T) {
float s = 0.0f;
#pragma unroll
for (int b = 0; b < NB; ++b) s += red_b[t][b];
ssg[t * Q8_NKC + kc] = s;
}
if (inj) {
const bool have_inj = (m.a[0].q8_inject != nullptr || m.a[0].w_inject != nullptr);
if (!have_inj || warp >= HC) return;
const bool inj_bf16 = (m.a[0].q8_inject == nullptr);
const int r = warp;
float acc[T];
#pragma unroll
for (int k = 0; k < T; ++k) acc[k] = 0.0f;
if (inj_bf16) {
const uint16_t* wr = m.a[0].w_inject + (size_t) r * D + (size_t) kc * Q8_KC;
for (int b = 0; b < NB; ++b) {
const float wv = __uint_as_float((uint32_t) wr[b * 32 + lane] << 16);
#pragma unroll
for (int k = 0; k < T; ++k) acc[k] = fmaf(wv * xs_d[k][b], (float) xs_q[k][b * 32 + lane], acc[k]);
}
} else {
const uint8_t* wr = m.a[0].q8_inject + (size_t) r * (D / 32) * Q8B + (size_t) kc * ROW_BYTES;
for (int b = 0; b < NB; ++b) {
const uint8_t* blk = wr + b * Q8B;
const float dw = q8_half(q8_ld16(blk));
const int wq = (int) (int8_t) blk[2 + lane];
#pragma unroll
for (int k = 0; k < T; ++k) acc[k] = fmaf(dw * xs_d[k][b], (float) (wq * (int) xs_q[k][b * 32 + lane]), acc[k]);
}
}
#pragma unroll
for (int k = 0; k < T; ++k) {
const float v = warp_sum(acc[k]);
if (lane == 0) part[((size_t) k * Q8_NKC + kc) * PR + LR + r] = v;
}
return;
}
// All 8 warps split the NB=20 Q8_0 blocks across 8 warps (2-3 MMAs per warp, single 16-row pass!).
const int g = lane >> 2;
const int tig = lane & 3;
const int t0 = 2 * tig;
const int t1 = t0 + 1;
const int b_start = (warp * NB) / WARPS;
const int b_end = ((warp + 1) * NB) / WARPS;
const uint8_t* w0 = reinterpret_cast<const uint8_t*>(s_w[g]);
const uint8_t* w1 = reinterpret_cast<const uint8_t*>(s_w[8 + g]);
float acc[4] = {0.0f, 0.0f, 0.0f, 0.0f};
for (int b = b_start; b < b_end; ++b) {
const uint8_t* blk0 = w0 + b * Q8B;
const uint8_t* blk1 = w1 + b * Q8B;
const float dw0 = q8_half(q8_ld16(blk0));
const float dw1 = q8_half(q8_ld16(blk1));
const uint32_t a0 = q8_ld16(blk0 + 2 + tig * 4) | (q8_ld16(blk0 + 4 + tig * 4) << 16);
const uint32_t a2 = q8_ld16(blk0 + 18 + tig * 4) | (q8_ld16(blk0 + 20 + tig * 4) << 16);
const uint32_t a1 = q8_ld16(blk1 + 2 + tig * 4) | (q8_ld16(blk1 + 4 + tig * 4) << 16);
const uint32_t a3 = q8_ld16(blk1 + 18 + tig * 4) | (q8_ld16(blk1 + 20 + tig * 4) << 16);
const uint32_t b0 = (g < T) ? *reinterpret_cast<const uint32_t*>(&xs_q[g][b * 32 + tig * 4]) : 0u;
const uint32_t b1 = (g < T) ? *reinterpret_cast<const uint32_t*>(&xs_q[g][b * 32 + 16 + tig * 4]) : 0u;
int32_t d[4] = {0, 0, 0, 0};
gr_mma_s8_m16n8k32(d, a0, a1, a2, a3, b0, b1);
if (t0 < T) {
const float dx0 = xs_d[t0][b];
acc[0] = fmaf(dw0 * dx0, (float) d[0], acc[0]);
acc[2] = fmaf(dw1 * dx0, (float) d[2], acc[2]);
}
if (t1 < T) {
const float dx1 = xs_d[t1][b];
acc[1] = fmaf(dw0 * dx1, (float) d[1], acc[1]);
acc[3] = fmaf(dw1 * dx1, (float) d[3], acc[3]);
}
}
if (t0 < T) { p_acc[warp][t0][g] = acc[0]; p_acc[warp][t0][8 + g] = acc[2]; }
if (t1 < T) { p_acc[warp][t1][g] = acc[1]; p_acc[warp][t1][8 + g] = acc[3]; }
__syncthreads();
if (t < T * 16) {
const int tk = t >> 4;
const int lr = t & 15;
float sum = 0.0f;
#pragma unroll
for (int w = 0; w < WARPS; ++w) sum += p_acc[w][tk][lr];
part[((size_t) tk * Q8_NKC + kc) * PR + r_base + lr] = sum;
}
#else
(void) m; (void) part; (void) ssg;
#endif
}
constexpr int UP8_COLS = 8;
constexpr int UP8_BLOCKS = N / UP8_COLS; // 320 CTAs (full wave saturation on 110-SM Blackwell & 82-SM 3090!)
template <int T>
__global__ void __launch_bounds__(THREADS) gr_up_q8_tc_kernel(GrMulti m, const float* __restrict__ part,
const float* __restrict__ ssg) {
#if STRATA_GR_Q8_TC_SM80
constexpr int NB_UP = LR / 32; // 10 Q8_0 blocks for LR=320
constexpr int STREAM_BYTES = UP8_COLS * NB_UP * Q8B; // 8 * 340 = 2,720 contiguous bytes per stream (% 16 == 0!)
constexpr int STREAM_U4 = STREAM_BYTES / 16; // 170 uint4s per stream (3 loads/thread!)
__shared__ alignas(16) int8_t lo_q[8][LR]; // 2,560 B (direct warp-quantized SiLU(lo), zero float lo!)
__shared__ float lo_d[8][NB_UP]; // 320 B
__shared__ float rsS[T][HC]; // 128 B
__shared__ float g_out[T][HC][UP8_COLS]; // 1,024 B
__shared__ float p_acc[4][HC][8][UP8_COLS]; // 4,096 B
__shared__ alignas(16) uint4 s_w[HC][STREAM_U4]; // 10,880 B (total ~19 KB -> 5+ CTAs/SM!)
const int t = threadIdx.x, lane = t & 31, warp = t >> 5;
const int d0 = blockIdx.x * UP8_COLS;
if (t < T * HC) {
const int k = t / HC, c = t - k * HC;
float ss = 0.0f;
#pragma unroll
for (int j = 0; j < Q8_CPS; ++j) ss += ssg[k * Q8_NKC + c * Q8_CPS + j];
const float r = rsqrtf(ss / (float) N + m.a[k].eps);
rsS[k][c] = r;
if (blockIdx.x == 0) m.a[k].rs[c] = r;
}
// Overlap 100% 16-byte aligned uint4 weight loads (all 4 streams) with rsS computation before first sync!
{
const int lc = warp & 3;
const int lhalf = warp >> 2;
const uint4* src4 = reinterpret_cast<const uint4*>(m.a[0].q8_up + (size_t) (lc * N + d0) * NB_UP * Q8B);
for (int u = lhalf * 32 + lane; u < STREAM_U4; u += 64) s_w[lc][u] = __ldg(src4 + u);
}
__syncthreads();
// Direct register reduction of part -> SiLU(sum / HC) -> warp-level per-32 Q8 quantization into lo_q/lo_d!
for (int kb = warp; kb < T * NB_UP; kb += WARPS) {
const int k = kb / NB_UP;
const int b = kb - k * NB_UP;
const int r = b * 32 + lane;
float sum = 0.0f;
#pragma unroll
for (int c = 0; c < HC; ++c) {
float p = 0.0f;
#pragma unroll
for (int j = 0; j < Q8_CPS; ++j) p += part[((size_t) k * Q8_NKC + c * Q8_CPS + j) * PR + r];
sum = fmaf(rsS[k][c], p, sum);
}
const float x = sum / (float) HC;
const float xv = x / (1.0f + __expf(-x));
float amax = fabsf(xv);
#pragma unroll
for (int o = 16; o > 0; o >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, o));
const float dx = amax * (1.0f / 127.0f);
const float inv = dx > 0.0f ? (1.0f / dx) : 0.0f;
int qv = dx > 0.0f ? __float2int_rn(xv * inv) : 0;
qv = max(-127, min(127, qv));
lo_q[k][r] = (int8_t) qv;
if (lane == 0) lo_d[k][b] = dx;
}
if (blockIdx.x == 0 && (m.a[0].w_inject != nullptr || m.a[0].q8_inject != nullptr) && t < T * HC) {
const int k = t / HC, ir = t - k * HC;
float sum = 0.0f;
#pragma unroll
for (int c = 0; c < HC; ++c) {
float p = 0.0f;
#pragma unroll
for (int j = 0; j < Q8_CPS; ++j) p += part[((size_t) k * Q8_NKC + c * Q8_CPS + j) * PR + LR + ir];
sum = fmaf(rsS[k][c], p, sum);
}
m.a[k].inject_out[ir] = sum;
}
__syncthreads();
// 8 warps = 2 stream-pairs (c0=2*pair, c1=2*pair+1 -> 16 rows per mma!) x 4 K-splits (2-3 MMAs per warp!).
const int pair = warp & 1;
const int kh = warp >> 1;
const int c0 = pair * 2;
const int c1 = c0 + 1;
const int g = lane >> 2;
const int tig = lane & 3;
const int t0 = 2 * tig;
const int t1 = t0 + 1;
const uint8_t* w0 = reinterpret_cast<const uint8_t*>(s_w[c0]) + (size_t) g * NB_UP * Q8B;
const uint8_t* w1 = reinterpret_cast<const uint8_t*>(s_w[c1]) + (size_t) g * NB_UP * Q8B;
const int b_start = (kh * NB_UP) / 4;
const int b_end = ((kh + 1) * NB_UP) / 4;
float acc[4] = {0.0f, 0.0f, 0.0f, 0.0f};
for (int b = b_start; b < b_end; ++b) {
const uint8_t* blk0 = w0 + b * Q8B;
const uint8_t* blk1 = w1 + b * Q8B;
const float dw0 = q8_half(q8_ld16(blk0));
const float dw1 = q8_half(q8_ld16(blk1));
const uint32_t a0 = q8_ld16(blk0 + 2 + tig * 4) | (q8_ld16(blk0 + 4 + tig * 4) << 16);
const uint32_t a2 = q8_ld16(blk0 + 18 + tig * 4) | (q8_ld16(blk0 + 20 + tig * 4) << 16);
const uint32_t a1 = q8_ld16(blk1 + 2 + tig * 4) | (q8_ld16(blk1 + 4 + tig * 4) << 16);
const uint32_t a3 = q8_ld16(blk1 + 18 + tig * 4) | (q8_ld16(blk1 + 20 + tig * 4) << 16);
const uint32_t b0 = (g < T) ? *reinterpret_cast<const uint32_t*>(&lo_q[g][b * 32 + tig * 4]) : 0u;
const uint32_t b1 = (g < T) ? *reinterpret_cast<const uint32_t*>(&lo_q[g][b * 32 + 16 + tig * 4]) : 0u;
int32_t d[4] = {0, 0, 0, 0};
gr_mma_s8_m16n8k32(d, a0, a1, a2, a3, b0, b1);
if (t0 < T) {
const float dx0 = lo_d[t0][b];
acc[0] = fmaf(dw0 * dx0, (float) d[0], acc[0]);
acc[2] = fmaf(dw1 * dx0, (float) d[2], acc[2]);
}
if (t1 < T) {
const float dx1 = lo_d[t1][b];
acc[1] = fmaf(dw0 * dx1, (float) d[1], acc[1]);
acc[3] = fmaf(dw1 * dx1, (float) d[3], acc[3]);
}
}
if (t0 < T) { p_acc[kh][c0][t0][g] = acc[0]; p_acc[kh][c1][t0][g] = acc[2]; }
if (t1 < T) { p_acc[kh][c0][t1][g] = acc[1]; p_acc[kh][c1][t1][g] = acc[3]; }
__syncthreads();
// T * HC * UP8_COLS <= 8 * 4 * 8 = 256 -> every thread owns at most 1 (token, stream, col) element!
if (t < T * HC * UP8_COLS) {
const int tk = t >> 5;
const int sc = (t >> 3) & 3;
const int dd = t & 7;
const int r_idx = sc * N + d0 + dd;
const float dot = (p_acc[0][sc][tk][dd] + p_acc[1][sc][tk][dd]) +
(p_acc[2][sc][tk][dd] + p_acc[3][sc][tk][dd]);
const FusedGrArgs& a = m.a[tk];
float rv = a.R[r_idx];
if (a.apply) {
const float gw = 2.0f * sigmoidf_(a.inj_prev[sc] / (float) HC);
rv = fmaf(a.bo_prev[d0 + dd], gw, rv);
a.R_out[r_idx] = rv;
}
g_out[tk][sc][dd] = (rv * a.w_norm[r_idx] * rsS[tk][sc]) * sigmoidf_(dot);
}
__syncthreads();
if (t < T * UP8_COLS) {
const int k = t >> 3;
const int col = t & 7;
float sum = 0.0f;
#pragma unroll
for (int sc = 0; sc < HC; ++sc) sum += g_out[k][sc][col];
m.a[k].mixed[d0 + col] = sum / (float) HC;
}
if (m.a[0].q8_mixed != nullptr) gr_q8_tail_n<4>(m, d0); // S26 STRATA_QFUSE (4 blocks per 32-col group)
#else
(void) m; (void) part; (void) ssg;
#endif
}
static int hc_q8_tc_mode() {
#if defined(__HIPCC__)
return 0;
#else
static const int env = [] {
const char* v = std::getenv("STRATA_HC_Q8_TC");
if (v == nullptr || v[0] == '\0') return 0;
if (v == nullptr || v[0] == '\0') return -1;
return std::atoi(v);
}();
if (env <= 0) return 0;
if (env == 0) return 0;
static int ok[64] = {};
int dev = 0;
if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) { (void) cudaGetLastError(); return 0; }
@@ -2962,7 +3276,8 @@ static int hc_q8_tc_mode() {
if (!good) (void) cudaGetLastError();
ok[dev] = good ? 1 : -1;
}
return ok[dev] > 0 ? env : 0;
if (ok[dev] <= 0) return 0;
return env > 0 ? env : 1; // default 1 (exact gr_down_q8_fast + 320-CTA uint4 INT8 TC gr_up_q8_tc) on sm_80+ CUDA (STRATA_HC_Q8_TC=0 disables)
#endif
}
@@ -2983,19 +3298,50 @@ static bool hc_q8_sm80_fast() {
#endif
}
static int hc_q8_bf16tc_mode() {
#if defined(__HIPCC__)
return 0;
#else
static const int env = [] {
const char* v = std::getenv("STRATA_HC_Q8_BF16TC");
if (v == nullptr || v[0] == '\0') return -1;
return std::atoi(v);
}();
if (env == 0) return 0;
if (!hc_q8_sm80_fast()) return 0;
return env > 0 ? env : 1; // fallback when STRATA_HC_Q8_TC=0
#endif
}
template <int T> void launch_q8_t(const GrMulti& m, float* part, float* ssg, cudaStream_t st, unsigned long long* stamp_buf,
int stamp_i0) {
// Opt-in INT8 TC path (STRATA_HC_Q8_TC=1 for TC up, =2 for TC down+up; quantizes 320-dim SiLU(lo) to int8).
// High-wave coalesced INT8 TC path (default on sm_80+ CUDA; STRATA_HC_Q8_TC=0 disables, =1 uses fast down + TC up,
// =2 uses TC down (336 CTAs, uint2 coalesced) + TC up (320 CTAs, uint4 coalesced)).
const int tc_mode = hc_q8_tc_mode();
if (tc_mode >= 1 && (LR % 32 == 0) && ((Q8_KC / 32) % 2 == 0) && ((LR / 32) % 2 == 0)) {
if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0, (void*) st);
if (tc_mode >= 2) {
gr_down_q8_tc_kernel<T><<<dim3(LR / 32 + 1, Q8_NKC), THREADS, 0, st>>>(m, part, ssg);
gr_down_q8_tc_kernel<T><<<dim3(LR / 16 + 1, Q8_NKC), THREADS, 0, st>>>(m, part, ssg);
} else {
gr_down_q8_fast_kernel<T, 4, true><<<dim3(LR / (WARPS * 4) + 1, Q8_NKC), THREADS, 0, st>>>(m, part, ssg);
}
if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, (void*) st);
gr_up_q8_tc_kernel<T><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);
gr_up_q8_tc_kernel<T><<<UP8_BLOCKS, THREADS, 0, st>>>(m, part, ssg);
return;
}
// Default on sm_80+ CUDA: exact-FP32 gr_down_q8_fast<T, 4, true> (fastest at T=1..8) + BF16 Tensor Core
// gr_up_q8_bf16tc_kernel (full FP32 exponent range on SiLU(lo), bit-exact int8->bf16 LUT, coalesced weights;
// STRATA_HC_Q8_BF16TC=0 falls back to scalar gr_up_q8_fast, =2 also uses BF16 TC for down).
const int bf_mode = hc_q8_bf16tc_mode();
if (bf_mode >= 1 && (LR % 32 == 0) && ((Q8_KC / 32) % 2 == 0) && ((LR / 32) % 2 == 0)) {
if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0, (void*) st);
if (bf_mode >= 2) {
gr_down_q8_bf16tc_kernel<T><<<dim3(LR / 32 + 1, Q8_NKC), THREADS, 0, st>>>(m, part, ssg);
} else {
gr_down_q8_fast_kernel<T, 4, true><<<dim3(LR / (WARPS * 4) + 1, Q8_NKC), THREADS, 0, st>>>(m, part, ssg);
}
if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, (void*) st);
gr_up_q8_bf16tc_kernel<T><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);
return;
}
// On CUDA sm_80+, default to the exact-FP32-activation gr_down_q8_fast_kernel<T, 4, true> + gr_up_q8_fast_kernel
+152 -46
View File
@@ -6025,9 +6025,12 @@ __global__ void __launch_bounds__(256) native_down_lds_kernel(const unsigned lon
const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5;
const uint8_t* blob = (const uint8_t*) grp_ptr[g];
const int nb = (int) (L.n_ff / F::qk), hb = (int) (L.n_ff / 32);
const int n_items = nb * F::ipb;
const int e0 = grp_start[g], e1 = grp_start[g + 1];
const int nrow = (int) L.n_embd;
const block_q8_1* sh = (const block_q8_1*) sh_raw;
constexpr bool kDownSplit = kSplit<TD> || (TD == 7);
const bool use_sub8 = kDownSplit && (n_items == 40 || n_items == 20);
for (int c0 = e0; c0 < e1; c0 += LDS_NT) {
const int cn = min(LDS_NT, e1 - c0);
__syncthreads();
@@ -6036,6 +6039,54 @@ __global__ void __launch_bounds__(256) native_down_lds_kernel(const unsigned lon
for (int i = tid; i < cn * hb * 9; i += 256) sh_raw[i] = src[i];
}
__syncthreads();
if (use_sub8) {
// 8-lane sub-warp (4 rows/warp = 32 rows/block in 2 passes of 32 = 64 rows), 100% active lanes + hoisted load!
const int sub_row = (tid >> 3); // 0..31 within block
const int slane = tid & 7; // 0..7 within sub-warp
for (int p = 0; p < 2; ++p) {
const int r = blockIdx.x * LDS_RB + p * 32 + sub_row;
if (r >= nrow) break;
const uint8_t* w = blob + L.down_off + (size_t) r * L.d_row;
auto pass8 = [&](auto ntc) {
constexpr int NTC = decltype(ntc)::value;
float s[NTC];
#pragma unroll
for (int j = 0; j < NTC; ++j) s[j] = 0.0f;
for (int k = slane; k < n_items; k += 8) {
const int kbx = k / F::ipb, iqs = F::step * (k % F::ipb);
if constexpr (kSplit<TD>) {
const auto rw = Split<TD>::load(w, kbx, iqs);
#pragma unroll
for (int j = 0; j < NTC; ++j) {
const block_q8_1* xk = sh + (size_t) j * hb + kbx * (F::qk / 32);
s[j] += Split<TD>::apply(rw, xk, iqs);
}
} else if constexpr (TD == 7) {
const auto rw = S27<7>::load(w, kbx, iqs);
#pragma unroll
for (int j = 0; j < NTC; ++j) {
const block_q8_1* xk = sh + (size_t) j * hb + kbx * (F::qk / 32);
s[j] += S27<7>::apply(rw, xk, iqs);
}
}
}
#pragma unroll
for (int j = 0; j < NTC; ++j) {
float v = s[j];
#pragma unroll
for (int o = 4; o > 0; o >>= 1) v += __shfl_xor_sync(0xffffffffu, v, o);
if (slane == 0) out[(size_t) ent_dst[c0 + j] * L.n_embd + r] = v;
}
};
switch (cn) {
case 1: pass8(std::integral_constant<int, 1>{}); break;
case 2: pass8(std::integral_constant<int, 2>{}); break;
case 3: pass8(std::integral_constant<int, 3>{}); break;
default: pass8(std::integral_constant<int, 4>{}); break;
}
}
continue;
}
for (int p = 0; p < LDS_RB / 16; ++p) {
const int r0 = blockIdx.x * LDS_RB + p * 16 + warp, r1 = r0 + 8;
if (r0 >= nrow) break;
@@ -6047,15 +6098,26 @@ __global__ void __launch_bounds__(256) native_down_lds_kernel(const unsigned lon
float s0[NTC], s1[NTC];
#pragma unroll
for (int j = 0; j < NTC; ++j) { s0[j] = 0.0f; s1[j] = 0.0f; }
for (int k = lane; k < nb * F::ipb; k += 32) {
for (int k = lane; k < n_items; k += 32) {
const int kbx = k / F::ipb, iqs = F::step * (k % F::ipb);
if constexpr (kSplit<TD>) {
const auto rw0 = Split<TD>::load(w0, kbx, iqs);
const auto rw1 = Split<TD>::load(w1, kbx, iqs);
#pragma unroll
for (int j = 0; j < NTC; ++j) {
const block_q8_1* xk = sh + (size_t) j * hb + kbx * (F::qk / 32);
const float a = F::dot(w0, xk, kbx, iqs);
const float b = F::dot(w1, xk, kbx, iqs);
s0[j] += a;
s1[j] += b;
for (int j = 0; j < NTC; ++j) {
const block_q8_1* xk = sh + (size_t) j * hb + kbx * (F::qk / 32);
s0[j] += Split<TD>::apply(rw0, xk, iqs);
s1[j] += Split<TD>::apply(rw1, xk, iqs);
}
} else {
#pragma unroll
for (int j = 0; j < NTC; ++j) {
const block_q8_1* xk = sh + (size_t) j * hb + kbx * (F::qk / 32);
const float a = F::dot(w0, xk, kbx, iqs);
const float b = F::dot(w1, xk, kbx, iqs);
s0[j] += a;
s1[j] += b;
}
}
}
#pragma unroll
@@ -6092,7 +6154,8 @@ __global__ void __launch_bounds__(256) native_gu_fused_kernel(const unsigned lon
block_q8_1* __restrict__ hq) {
using F = Fmt<TG>;
constexpr bool kHasGrid = (TG == 16 || TG == 18 || TG == 21 || TG == 22);
constexpr int kGridN = kHasGrid ? (TG == 22 ? 1024 : TG == 21 ? 512 : 256) : 1;
// TG == 21 (IQ3_S): stage 512 grid entries + 256 lo-nibble + 256 hi-nibble sign masks = 1024 uint32s for Split<21>::load<true>!
constexpr int kGridN = kHasGrid ? (TG == 22 ? 1024 : TG == 21 ? 1024 : 256) : 1;
using GT = typename std::conditional<(TG == 16 || TG == 22), uint64_t, uint32_t>::type;
__shared__ GT sgrid[kGridN];
__shared__ float res[LDS_NT][64];
@@ -6103,6 +6166,13 @@ __global__ void __launch_bounds__(256) native_gu_fused_kernel(const unsigned lon
if constexpr (kHasGrid) {
const GT* gsrc = GridOf<TG>::src();
for (int i = tid; i < GridOf<TG>::N; i += 256) sgrid[i] = gsrc[i];
if constexpr (TG == 21) {
// Fill sign mask tables at [512..767] (low nibble) and [768..1023] (high nibble) for Split<21>::load<true>.
const uint32_t m_lo = (uint32_t) nib_mask((uint32_t) tid);
const uint32_t m_hi = (uint32_t) nib_mask((uint32_t) tid >> 4);
sgrid[512 + tid] = m_lo;
sgrid[768 + tid] = m_hi;
}
}
const uint8_t* blob = (const uint8_t*) grp_ptr[g];
const float s_gate = (TG == 40 && L.tail_off) ? ((const float*) (blob + L.tail_off))[0] : 1.0f;
@@ -6122,50 +6192,85 @@ __global__ void __launch_bounds__(256) native_gu_fused_kernel(const unsigned lon
for (int i = tid; i < xb * 9; i += 256) sx_raw[j * xb * 9 + i] = src[i];
}
__syncthreads();
for (int p = 0; p < 4; ++p) {
const int i0 = p * 16 + warp, i1 = i0 + 8;
const uint8_t* w0 = wrow(i0);
const uint8_t* w1 = wrow(i1);
auto pass = [&](auto ntc) {
constexpr int NTC = decltype(ntc)::value;
float s0[NTC], s1[NTC];
#pragma unroll
for (int j = 0; j < NTC; ++j) { s0[j] = 0.0f; s1[j] = 0.0f; }
for (int k = lane; k < nb * F::ipb; k += 32) {
const int kbx = k / F::ipb, iqs = F::step * (k % F::ipb);
if (kSplit<TG> && (nb * F::ipb == 80)) {
// 16-lane sub-warp dual-row (gate+up pair): 16 pairs/pass x 2 passes = 64 rows, 100% active lanes (0 idle
// lanes on k=64..79), halves shared activation reads, bitwise identical addition tree to warp_sum!
const int sub = tid >> 4;
const int t16 = tid & 15;
for (int p = 0; p < 2; ++p) {
const int i0 = p * 16 + sub;
const int i1 = 32 + i0;
const uint8_t* w0 = wrow(i0);
const uint8_t* w1 = wrow(i1);
auto pass16 = [&](auto ntc) {
constexpr int NTC = decltype(ntc)::value;
if constexpr (kSplit<TG>) {
// Stage grid only for types whose Split<TG>::load<true> matches GridOf<TG> layout (IQ2_XXS, IQ2_S).
constexpr bool kStageSplit = (TG == 16 || TG == 22);
const auto rw0 = Split<TG>::template load<kStageSplit>(w0, kbx, iqs, (const uint32_t*) sgrid);
const auto rw1 = Split<TG>::template load<kStageSplit>(w1, kbx, iqs, (const uint32_t*) sgrid);
constexpr bool kStageSplit = (TG == 16 || TG == 21 || TG == 22);
int off[NTC];
#pragma unroll
for (int j = 0; j < NTC; ++j) {
const block_q8_1* xk = sx + (size_t) j * xb + kbx * (F::qk / 32);
s0[j] += Split<TG>::apply(rw0, xk, iqs);
s1[j] += Split<TG>::apply(rw1, xk, iqs);
}
} else {
for (int j = 0; j < NTC; ++j) off[j] = j * xb;
float s0[NTC], s1[NTC];
row_dot2_80_sub16<TG, NTC, true, kStageSplit>(w0, w1, sx, off, NTC, t16, s0, s1,
(const uint32_t*) sgrid);
if (t16 == 0) {
#pragma unroll
for (int j = 0; j < NTC; ++j) {
const block_q8_1* xk = sx + (size_t) j * xb + kbx * (F::qk / 32);
const float a = DotG<TG, 1>::f(w0, xk, kbx, iqs, sgrid);
const float b = DotG<TG, 1>::f(w1, xk, kbx, iqs, sgrid);
s0[j] += a;
s1[j] += b;
for (int j = 0; j < NTC; ++j) { res[j][i0] = s0[j]; res[j][i1] = s1[j]; }
}
}
};
switch (cn) {
case 1: pass16(std::integral_constant<int, 1>{}); break;
case 2: pass16(std::integral_constant<int, 2>{}); break;
case 3: pass16(std::integral_constant<int, 3>{}); break;
default: pass16(std::integral_constant<int, 4>{}); break;
}
}
} else {
for (int p = 0; p < 4; ++p) {
const int i0 = p * 16 + warp, i1 = i0 + 8;
const uint8_t* w0 = wrow(i0);
const uint8_t* w1 = wrow(i1);
auto pass = [&](auto ntc) {
constexpr int NTC = decltype(ntc)::value;
float s0[NTC], s1[NTC];
#pragma unroll
for (int j = 0; j < NTC; ++j) {
const float a = warp_sum(s0[j]), b = warp_sum(s1[j]);
if (lane == 0) { res[j][i0] = a; res[j][i1] = b; }
for (int j = 0; j < NTC; ++j) { s0[j] = 0.0f; s1[j] = 0.0f; }
for (int k = lane; k < nb * F::ipb; k += 32) {
const int kbx = k / F::ipb, iqs = F::step * (k % F::ipb);
if constexpr (kSplit<TG>) {
// Stage grid + sign masks in smem for IQ2_XXS (16), IQ3_S (21), and IQ2_S (22).
constexpr bool kStageSplit = (TG == 16 || TG == 21 || TG == 22);
const auto rw0 = Split<TG>::template load<kStageSplit>(w0, kbx, iqs, (const uint32_t*) sgrid);
const auto rw1 = Split<TG>::template load<kStageSplit>(w1, kbx, iqs, (const uint32_t*) sgrid);
#pragma unroll
for (int j = 0; j < NTC; ++j) {
const block_q8_1* xk = sx + (size_t) j * xb + kbx * (F::qk / 32);
s0[j] += Split<TG>::apply(rw0, xk, iqs);
s1[j] += Split<TG>::apply(rw1, xk, iqs);
}
} else {
#pragma unroll
for (int j = 0; j < NTC; ++j) {
const block_q8_1* xk = sx + (size_t) j * xb + kbx * (F::qk / 32);
const float a = DotG<TG, 1>::f(w0, xk, kbx, iqs, sgrid);
const float b = DotG<TG, 1>::f(w1, xk, kbx, iqs, sgrid);
s0[j] += a;
s1[j] += b;
}
}
}
#pragma unroll
for (int j = 0; j < NTC; ++j) {
const float a = warp_sum(s0[j]), b = warp_sum(s1[j]);
if (lane == 0) { res[j][i0] = a; res[j][i1] = b; }
}
};
switch (cn) {
case 1: pass(std::integral_constant<int, 1>{}); break;
case 2: pass(std::integral_constant<int, 2>{}); break;
case 3: pass(std::integral_constant<int, 3>{}); break;
default: pass(std::integral_constant<int, 4>{}); break;
}
};
switch (cn) {
case 1: pass(std::integral_constant<int, 1>{}); break;
case 2: pass(std::integral_constant<int, 2>{}); break;
case 3: pass(std::integral_constant<int, 3>{}); break;
default: pass(std::integral_constant<int, 4>{}); break;
}
}
__syncthreads();
@@ -6536,8 +6641,9 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long long
if (cap_groups <= 0 || cap_entries <= 0) return;
strata::dispatch::kernel(strata::dispatch::kExpert, 0, L.gu_type, (int) L.n_embd, (int) L.n_ff, L.d_type); // BW-MEASURE: one grouped expert call
if (L.n_ff % 32 != 0) { std::fprintf(stderr, "native_expert_grouped: n_ff %lld\n", (long long) L.n_ff); std::exit(1); }
// Blackwell sm_120a/sm_121a hardware NVFP4 grouped decode/verify (cvt.rn.f16x2.e2m1x2, fused GU+SwiGLU+Q8_1 -> down).
if (!g_grouped_v1 && L.gu_type == 40 && L.d_type == 40 && !L.gu_nib && g_exp_phase == 0 &&
// Blackwell sm_120/sm_121 Tensor Core NVFP4 grouped verify (T >= 2, cap_entries > 10):
// single-token T=1 (cap_entries <= 10) stays on sub-warp row_dot_20_sub8 to avoid m16n8k16 N=8 padding.
if (!g_grouped_v1 && cap_entries > 10 && L.gu_type == 40 && L.d_type == 40 && !L.gu_nib && g_exp_phase == 0 &&
nvfp4_decode_sm120_available()) {
const size_t f0 = (size_t) cap_entries * (size_t) L.n_ff * sizeof(float);
const size_t fa0 = (f0 + 255) & ~(size_t) 255;
+20 -4
View File
@@ -3564,13 +3564,23 @@ std::size_t native_mmvq_weight_bytes(int ggml_type, int n_in, int n_out) {
return row_bytes * std::size_t(n_out);
}
static int dense_i8_tc_min_t() {
static const int v = [] {
const char* e = std::getenv("STRATA_DENSE_I8_TC_MIN_T");
return (e != nullptr && e[0] != '\0') ? std::max(1, std::atoi(e)) : 9;
}();
return v;
}
void native_mmvq(int ggml_type, const void* weights, const void* x_q8_1, float* y,
int n_in, int n_out, int ncols, void* stream) {
// Clean-room MIT INT8 Tensor Core path (sm_80+: RTX 3090 & RTX 5090):
// Interleaved native_mmvq_il handles ncols=1..8 at peak SM occupancy; for ncols=9..16 (wide verify windows where
// native_mmvq_il is absent and native_mmvq would split into 2 passes), dense_i8_tc_mmvq wins 2.0x-2.7x in 1 pass.
if (n_out >= 1024 && ncols >= 9 && ncols <= 16 && dense_i8_tc_type_ok(ggml_type) && dense_i8_tc_decode_enabled()) {
// Clean-room MIT coalesced INT8 Tensor Core path (sm_80+: RTX 3090 & RTX 5090):
// Evaluates T >= min_t (default 3; STRATA_DENSE_I8_TC=0 disables; STRATA_DENSE_I8_TC_MIN_T overrides)
// up to T=16 in a single coalesced smem-staged weight pass using mma.m16n8k32/k16.
if (n_out >= 1024 && ncols >= dense_i8_tc_min_t() && ncols <= 16 &&
dense_i8_tc_type_ok(ggml_type) && dense_i8_tc_decode_enabled()) {
if (dense_i8_tc_mmvq(ggml_type, weights, x_q8_1, y, n_in, n_out, ncols, stream)) {
strata::dispatch::kernel(strata::dispatch::kDense, 30, ggml_type, n_in, n_out, ncols);
return;
}
}
@@ -3976,6 +3986,12 @@ bool il_arch_ok() {
bool native_mmvq_il_supported(int ggml_type, int ncols, int n_out) {
if (ncols < 1 || ncols > 8 || !g_multi_exact || !il_arch_ok()) return false;
// When coalesced INT8 Tensor Core dense GEMM covers this shape (T >= min_t, n_out >= 1024), decline native_mmvq_il
// so callers skip the redundant q8_1_interleave launch and dispatch straight into dense_i8_tc_mmvq via native_mmvq.
if (g_tune_rows == 0 && n_out >= 1024 && ncols >= dense_i8_tc_min_t() &&
dense_i8_tc_type_ok(ggml_type) && dense_i8_tc_decode_enabled()) {
return false;
}
return (g_tune_rows ? g_tune_rows : il_rows(ggml_type, ncols, n_out)) != 0 &&
(ggml_type == 23 || ggml_type == 12 || ggml_type == 13 || ggml_type == 14 || ggml_type == 11);
}
+19 -4
View File
@@ -226,9 +226,19 @@ void shared_expert_multi(int n_tok, const float* x, const uint16_t* x_bf16, cons
native_quantize_q8_1(x, nw.q8_1, (int) n_embd, n_tok, stream);
x_q8_1 = nw.q8_1;
}
// S26 STRATA_LFUSE (gate_deferred): gate and up in one launch where the pair applies (bitwise the two calls)
if (!(pair && nw.gate_type == nw.up_type &&
native_mmvq_pair(nw.gate_type, nw.gate_data, nw.up_data, x_q8_1, gate, up, (int) n_embd, (int) n_ff, n_tok, stream))) {
// S26 STRATA_LFUSE (gate_deferred): gate and up in one launch where the pair applies (bitwise the two calls).
// When interleaved MMVQ covers gate/up (e.g. IQ4_XS / Q4_K / Q5_K / Q6_K) and q8_1_il scratch is provided,
// use bit-exact native_mmvq_il instead of plain native_mmvq for much higher memory bandwidth.
const bool use_gu_il = (nw.q8_1_il != nullptr) && (n_tok >= 2) &&
(native_mmvq_il_supported(nw.gate_type, n_tok, (int) n_ff) ||
native_mmvq_il_supported(nw.up_type, n_tok, (int) n_ff));
if (use_gu_il) {
native_q8_1_interleave(x_q8_1, nw.q8_1_il, (int) n_embd, n_tok, stream);
native_mmvq_il(nw.gate_type, nw.gate_data, x_q8_1, nw.q8_1_il, gate, (int) n_embd, (int) n_ff, n_tok, stream);
native_mmvq_il(nw.up_type, nw.up_data, x_q8_1, nw.q8_1_il, up, (int) n_embd, (int) n_ff, n_tok, stream);
} else if (!(pair && nw.gate_type == nw.up_type &&
native_mmvq_pair(nw.gate_type, nw.gate_data, nw.up_data, x_q8_1, gate, up, (int) n_embd, (int) n_ff,
n_tok, stream))) {
native_mmvq(nw.gate_type, nw.gate_data, x_q8_1, gate, (int) n_embd, (int) n_ff, n_tok, stream);
native_mmvq(nw.up_type, nw.up_data, x_q8_1, up, (int) n_embd, (int) n_ff, n_tok, stream);
}
@@ -239,7 +249,12 @@ void shared_expert_multi(int n_tok, const float* x, const uint16_t* x_bf16, cons
native_swiglu_kernel<<<(unsigned) ((n + THREADS - 1) / THREADS), THREADS, 0, cs>>>(gate, up, gate, n);
native_quantize_q8_1(gate, nw.q8_1, (int) n_ff, n_tok, stream);
}
native_mmvq(nw.down_type, nw.down_data, nw.q8_1, out, (int) n_ff, (int) n_embd, n_tok, stream);
if (nw.q8_1_il != nullptr && n_tok >= 2 && native_mmvq_il_supported(nw.down_type, n_tok, (int) n_embd)) {
native_q8_1_interleave(nw.q8_1, nw.q8_1_il, (int) n_ff, n_tok, stream);
native_mmvq_il(nw.down_type, nw.down_data, nw.q8_1, nw.q8_1_il, out, (int) n_ff, (int) n_embd, n_tok, stream);
} else {
native_mmvq(nw.down_type, nw.down_data, nw.q8_1, out, (int) n_ff, (int) n_embd, n_tok, stream);
}
if (gate_deferred) { // the caller computed g (raw) with the router and scales `out` in the combine
const cudaError_t e = cudaGetLastError();
if (e != cudaSuccess) throw std::runtime_error(std::string("shared_expert_multi: ") + cudaGetErrorString(e));
+45
View File
@@ -183,6 +183,7 @@ __global__ void gr_mix_r_kernel(const float* __restrict__ R, const float* __rest
// weights - the same thread-to-element mapping (256 threads, stride 256) and block_sum, so rs and the BF16 image are
// the same bits, and R is not read back
constexpr int GRW_PER = (N + 255) / 256;
constexpr int GRW_PER4 = ((N / 4) + 255) / 256;
__global__ void __launch_bounds__(256) gr_write_norm_rs_kernel(float* __restrict__ R, const float* __restrict__ bo,
const float* __restrict__ inj, int64_t inj_ld,
const float* __restrict__ w, float eps,
@@ -195,6 +196,50 @@ __global__ void __launch_bounds__(256) gr_write_norm_rs_kernel(float* __restrict
float* r = R + row * N;
const int64_t xo = t * ldx + (int64_t) c * N;
const float sc = 2.0f * sigm(inj[t * inj_ld + c] / (float) HC);
if constexpr ((N & 3) == 0) {
if ((((uintptr_t) r | (uintptr_t) (bo + t * N) | (uintptr_t) (w + c * N)) & 15u) == 0 &&
(((uintptr_t) (xn16 + xo)) & 7u) == 0 && (xn16_lo == nullptr || (((uintptr_t) (xn16_lo + xo)) & 7u) == 0)) {
constexpr int N4 = N / 4;
float4* r4 = reinterpret_cast<float4*>(r);
const float4* bo4 = reinterpret_cast<const float4*>(bo + t * N);
const float4* w4 = reinterpret_cast<const float4*>(w + c * N);
uint2* x4 = reinterpret_cast<uint2*>(xn16 + xo);
uint2* xlo4 = xn16_lo ? reinterpret_cast<uint2*>(xn16_lo + xo) : nullptr;
float4 v[GRW_PER4];
float ss = 0.0f;
int k = 0;
#pragma unroll
for (int d4 = threadIdx.x; d4 < N4; d4 += 256, ++k) {
const float4 rv = r4[d4];
const float4 bv = bo4[d4];
const float4 xv = make_float4(fmaf(bv.x, sc, rv.x), fmaf(bv.y, sc, rv.y),
fmaf(bv.z, sc, rv.z), fmaf(bv.w, sc, rv.w));
r4[d4] = xv;
v[k] = xv;
ss += (xv.x * xv.x + xv.y * xv.y) + (xv.z * xv.z + xv.w * xv.w);
}
const float rs = rsqrtf(block_sum(ss, sh) / (float) N + eps);
if (threadIdx.x == 0) rs_out[row] = rs;
k = 0;
#pragma unroll
for (int d4 = threadIdx.x; d4 < N4; d4 += 256, ++k) {
const float4 wv = w4[d4];
const float y0 = v[k].x * rs * wv.x, y1 = v[k].y * rs * wv.y;
const float y2 = v[k].z * rs * wv.z, y3 = v[k].w * rs * wv.w;
const uint16_t h0 = act16(y0), h1 = act16(y1), h2 = act16(y2), h3 = act16(y3);
const uint16_t o0 = f16img ? bf_to_hf_conv(h0) : h0;
const uint16_t o1 = f16img ? bf_to_hf_conv(h1) : h1;
const uint16_t o2 = f16img ? bf_to_hf_conv(h2) : h2;
const uint16_t o3 = f16img ? bf_to_hf_conv(h3) : h3;
x4[d4] = make_uint2((uint32_t) o0 | ((uint32_t) o1 << 16), (uint32_t) o2 | ((uint32_t) o3 << 16));
if (xlo4) {
const uint16_t l0 = bf_lo(y0, h0), l1 = bf_lo(y1, h1), l2 = bf_lo(y2, h2), l3 = bf_lo(y3, h3);
xlo4[d4] = make_uint2((uint32_t) l0 | ((uint32_t) l1 << 16), (uint32_t) l2 | ((uint32_t) l3 << 16));
}
}
return;
}
}
float v[GRW_PER];
float ss = 0.0f;
int k = 0;
+20
View File
@@ -23146,6 +23146,26 @@ int main(int argc, char** argv) {
#if defined(STRATA_GPU_METAL)
k = 0; // Metal: no loan of expert-cache slots (the prompt buffers are the Metal window's own); the refill after it copied into given-back allocations
#endif
if (k > 0) { // the lent slots are refilled after the prompt
// Skip borrowing & refilling expert cache slots when the GPU already has ample free VRAM for the
// prompt path's own buffers (e.g., 48-72 GB Blackwell/Ampere cards where all experts fit with GBs to
// spare). Avoids evicting resident experts and paying ~80-150 ms of PCIe refill after prefill.
static const bool no_free_skip = [] {
const char* e = std::getenv("STRATA_PREFILL_ALWAYS_BORROW");
return e != nullptr && e[0] == '1';
}();
if (!no_free_skip && !o.kv_grow) {
const uint64_t own_need =
strata::prefill::Prefill::bytes_needed_owned(g, ss, chunk, srcp != nullptr);
size_t fb = 0, tb = 0;
if (cudaMemGetInfo(&fb, &tb) == cudaSuccess &&
fb > own_need + (4ull << 30)) {
k = 0;
} else {
(void) cudaGetLastError();
}
}
}
if (k > 0) { // the lent slots are refilled after the prompt
const int32_t first = (int32_t) (xcache.slots() - k);
// the elastic K/V first: its hot experts move into slots of the loan, which are refilled after it