Merge f-p4-sm 98e21c171 (a failed weight-staging check takes the unstaged kernel instead of ending the process); the staged K-quant downs go through launch_ww too

The merge is textually clean. On top of it, in the merge commit:

- experts_native_ext (moe_fused_iq_q36.cu) takes its ww from launch_ww(b, g, n, n_expert, staged, wide) instead of pick_ww plus the old check_staged_blobs
  call (which the merge renamed to staged_blobs_ok). staged and wide are what the K-quant down commit passed: staged = sw_on(gu_type, true) ||
  sw_on(d_type, false) (the Q5_K / Q6_K downs are staging instances, K = 512), wide = the gate/up is Q6_K or Q8_0 (false for the Q4_K / Q5_K gate/up of
  this path). A staged K-quant launch whose expert blobs are not device memory at 2-byte aligned addresses now takes the unstaged 64-row kernel, sticky for the
  process, one log line, no exit, like every other launch of the copy.
- prefill_fused_q36_test takes --host-blobs=N [--expect-fallback | --expect-no-fallback] (the first N experts' blobs in pinned, mapped host memory; timing
  part; MMQ is not run for those pairs), as prefill_fused_iq_test does; moe_fused_q36.hpp declares the copy's staged_blob_fallbacks(); ctest entry
  prefill_fused_q36_test_hostblobs (Q4_K / Q5_K, 2,048 tokens).

Checked on RTX A6000 (sm_86, CUDA 13.1): the five pairs' output bits hashes at tile default / 64 / 128 and 2048 / 4096 / 8192 / 20000 tokens are the
ones of the K-quant down commit (20 of 20, three tiles); the three K-quant pairs with --host-blobs=1 take the unstaged kernel (one log line, 346 to
1,198 launches counted over the pairs, no exit), at the default tile and with STRATA_PF_FUSED_TILE=128 forced, and their hashes are the all-device
run's (6 of 6, both).
This commit is contained in:
Niko1221 committed 2026-11-12 04:59:03 +01:00
commit 6d10d61e74
9 files changed
+225 -36

No files matched your search

+6
View File
@@ -277,6 +277,9 @@ macro(strata_cuda_mmq)
target_link_libraries(prefill_fused_q36_test PRIVATE strata_mmq ggml-base ${_strata_gpu_runtime_target})
add_test(NAME prefill_fused_q36_test COMMAND prefill_fused_q36_test)
set_tests_properties(prefill_fused_q36_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 900)
# a launch whose expert blobs are not in device memory takes the unstaged kernel instead of ending the process (a staged K-quant down: timing part only, one layer of 2,048 tokens)
add_test(NAME prefill_fused_q36_test_hostblobs COMMAND prefill_fused_q36_test "--only=Q4_K / Q5_K" --no-ref --chunks=2048 --host-blobs=1 --expect-fallback)
set_tests_properties(prefill_fused_q36_test_hostblobs PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600)
# the opt-in STRATA_PF_FUSED_Q2N=1 on its own (the test leaves STRATA_PF_FUSED unset): the native Q2_0 pack's Q2_0 / Q2_0
# layers on sm_80+ (77 below sm_80 and without a card)
add_test(NAME prefill_fused_iq_test_q2n COMMAND prefill_fused_iq_test "--only=Q2_0 / Q2_0" --unset-pf-fused)
@@ -296,6 +299,9 @@ macro(strata_cuda_mmq)
# (this build has no Q5_0 MMQ), with the tails and tile boundaries the test adds for every Q5_0 pair (77 below sm_80)
add_test(NAME prefill_fused_iq_test_q50 COMMAND prefill_fused_iq_test "--only=Q5_0" --no-timing)
set_tests_properties(prefill_fused_iq_test_q50 PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_Q50=1" SKIP_RETURN_CODE 77 TIMEOUT 600)
# a launch whose expert blobs are not in device memory takes the unstaged kernel instead of ending the process (timing part only: one layer of 2,048 tokens, S20_NOMMQ because MMQ does not read host blobs)
add_test(NAME prefill_fused_iq_test_hostblobs COMMAND prefill_fused_iq_test "--only=IQ3_S / IQ4_NL" --no-ref --chunks=2048 --host-blobs=1 --expect-fallback)
set_tests_properties(prefill_fused_iq_test_hostblobs PROPERTIES ENVIRONMENT "S20_NOMMQ=1" SKIP_RETURN_CODE 77 TIMEOUT 600)
# the opt-in STRATA_PF_FUSED_KQ=1 (phase 4): UD-Q4_K_XL's Q4_K / Q5_K gate/up with Q5_1 / Q8_0 down on the native kernels, against the
# reference and the int8 model at every shape (--edge), MMQ's K-quant library alongside (STRATA_MMQ_KQ=1; 77 below sm_80)
add_test(NAME prefill_fused_iq_test_kq COMMAND prefill_fused_iq_test "--only=K / Q" --edge --no-timing)
@@ -14,6 +14,7 @@
// end-to-end row on a 5070-class card yet, and the fused kernels are opt-in on that class today (MMQ is its default).
#pragma once
#include <cstddef>
#include <cstdint>
namespace strata::prefill::fused_tile {
@@ -86,4 +87,39 @@ constexpr bool takes_by_default(const Card& c, bool sm40, bool gate_staged, bool
return sm40 && gate_staged && down_ok && c.sms >= 40;
}
// ---- the weight-staging guard (check_staged_blobs's decision in src/prefill/moe_fused_iq.cu), as pure functions so the decision can be tested without a GPU.
// The staging kernels (sw_on instances) copy the expert blobs into shared memory with cp.async and read 16-bit words at the rows' offsets: they need blobs in device (or managed)
// memory at 2-byte aligned addresses and 2-byte aligned rows and offsets. A launch that fails the check does NOT end the process: it takes the unstaged 64-row kernel of the same
// formats (same output bits, the identity tables of RESULTS-P4-SM.md), whatever STRATA_PF_FUSED_TILE says. The memory kinds are cudaMemoryType's, as the kernel's host code reads them.
enum class BlobMem { kDevice, kManaged, kHost, kUnregistered, kUnknown }; // kUnknown: cudaPointerGetAttributes failed (the launch's own error says so)
constexpr bool staged_geometry_ok(std::size_t gu_row, std::size_t d_row, std::size_t up_off, std::size_t down_off) { return ((gu_row | d_row | up_off | down_off) & 1) == 0; }
constexpr bool staged_blob_ok(BlobMem m, std::uintptr_t addr) {
if (m == BlobMem::kUnknown) return true; // as before: not a pointer the driver knows, the launch reports it
return (m == BlobMem::kDevice || m == BlobMem::kManaged) && (addr & 1) == 0;
}
// The per-process state of the check. The check costs a driver call per blob, so it runs on the first `left` launches only; once one launch has failed it, every later launch of
// the process takes the unstaged kernel without asking again (sticky): after the first launches nothing is checked any more, and a call site that gave one bad blob will give more.
// The log line is printed by the caller that gets `true` from report() (the first failure only).
struct StagedGuard {
enum class Step { kUnstaged, kCheck, kTrust };
int left = 256;
bool bad = false;
constexpr Step next() {
if (bad) return Step::kUnstaged;
if (left <= 0) return Step::kTrust;
--left;
return Step::kCheck;
}
constexpr bool report(bool ok) { // true: this failure is the first, log it
if (ok) return false;
const bool first = !bad;
bad = true;
return first;
}
};
constexpr int kUnstagedWw = 4; // the fallback's work item: the 64-row instances, which no weight-staging instance shares
} // namespace strata::prefill::fused_tile
+3
View File
@@ -57,6 +57,9 @@ void hadamard128_rows(float* x, int64_t rows, int64_t cols, const float* signs,
/// The NT = 256 kernels (STRATA_PF_FUSED_NT=256 or _F16=1) or the warp-specialized kernel (STRATA_PF_FUSED_WS=1) are
/// set up on the current device: experts_native takes a WideBatch (STRATA_PF_BATCH up to kBatchRoom experts a launch).
bool native_wide();
/// Launches of experts_native that took the unstaged 64-row kernel because the weight-staging check failed (an expert blob not in device memory, or an odd address / row / offset):
/// 0 in every default configuration; 0 in a HIP build (no staging kernels). The first such launch logs one line to stderr; the process does not exit. For the GPU test.
int staged_blob_fallbacks();
/// experts_native() of a WideBatch, on the NT = 256 kernels or the warp-specialized kernel; only when native_wide().
void experts_native(const WideBatch& b, const NativeGeom& g, int n_expert, int64_t n, const void* scratch, const void* xa,
const int32_t* src, void* ha, float* dm, void* stream);
+3
View File
@@ -32,6 +32,9 @@ void experts_native(const Batch& b, const NativeGeom& g, int n_expert, int64_t n
/// native_supported / experts_native plus the K-quant downs of UD-Q4_K_XL (a Q4_K or Q5_K gate/up with a Q5_K or Q6_K down: K = 512 is two 256-value
/// super-blocks), which the copy of moe_fused_iq.cu has instances for but whose dispatch is not part of it. Every other pair goes to the two above.
bool native_supported_ext(int gu_type, int d_type);
/// Launches (of experts_native / experts_native_ext) that took the unstaged 64-row kernel because the weight-staging check failed (an expert blob not in device memory, or an odd
/// address / row / offset): 0 in every default configuration. This copy's own count (moe_fused_iq.hpp's is the original copy's). For the GPU test.
int staged_blob_fallbacks();
void experts_native_ext(const Batch& b, const NativeGeom& g, int n_expert, int64_t n, const void* scratch, const void* xa,
const int32_t* src, void* ha, float* dm, void* stream);
#endif
+60 -26
View File
@@ -2546,32 +2546,46 @@ void launch(bool ws, unsigned grid, const WideBatch& b, const NativeGeom& g, con
// 17.5 (256 rows was slower still). STRATA_PF_FUSED_TILE=64|128 forces one.
#if !defined(__HIPCC__)
// (staging) what the weight copies assume of a launch: the expert blobs are device memory (the prompt path's expert cache slots and its stage ring), not a host-mapped
// pointer, and every row and offset is 2-byte aligned (the decode reads 16-bit words at the same offsets inside the shared-memory window). A call site that breaks it
// stops here with a message instead of reading something else. Checked on the first launches of a process only (a pointer query is a driver call).
void check_staged_blobs(const Batch& b, const NativeGeom& g) {
static std::mutex mu;
static int left = 256;
// pointer, and every row and offset is 2-byte aligned (the decode reads 16-bit words at the same offsets inside the shared-memory window). A launch that breaks it
// does not end the process: it takes the unstaged 64-row kernel of the same formats (same output bits), whatever STRATA_PF_FUSED_TILE says, and the first such launch of a
// process logs one line. Checked on the first 256 launches of a process only (a pointer query is a driver call); once one launch failed, every later launch of the process
// takes the unstaged kernel without asking (fused_tile::StagedGuard: the decision is a pure function, tests/prefill/fused_tile_rule_test.cpp).
std::mutex g_staged_mu;
strata::prefill::fused_tile::StagedGuard g_staged_guard;
int g_staged_fallbacks = 0; // launches that took the unstaged kernel for this reason (the GPU test reads it)
bool staged_blobs_ok(const Batch& b, const NativeGeom& g) {
namespace ft = strata::prefill::fused_tile;
{
std::lock_guard<std::mutex> lk(mu);
if (left <= 0) return;
--left;
std::lock_guard<std::mutex> lk(g_staged_mu);
const ft::StagedGuard::Step st = g_staged_guard.next();
if (st == ft::StagedGuard::Step::kTrust) return true;
if (st == ft::StagedGuard::Step::kUnstaged) { ++g_staged_fallbacks; return false; }
}
if (((g.gu_row | g.d_row | g.up_off | g.down_off) & 1) != 0) {
std::fprintf(stderr, "prefill fused experts (native): the staged kernels need 2-byte aligned rows (gu_row %zu d_row %zu up_off %zu down_off %zu)\n", g.gu_row, g.d_row,
g.up_off, g.down_off);
std::exit(1);
}
for (int e = b.e0; e < b.e1; ++e) {
const uint8_t* p = b.blob[e - b.e0];
cudaPointerAttributes pa{};
const cudaError_t err = cudaPointerGetAttributes(&pa, p);
if (err != cudaSuccess) { cudaGetLastError(); continue; } // (not a pointer the driver knows: the launch's own error says so)
if ((pa.type != cudaMemoryTypeDevice && pa.type != cudaMemoryTypeManaged) || (reinterpret_cast<uintptr_t>(p) & 1) != 0) {
std::fprintf(stderr, "prefill fused experts (native): the staged kernels need expert blobs in device memory, 2-byte aligned (expert %d: %p, memory type %d)\n", e, (const void*) p,
(int) pa.type);
std::exit(1);
char why[256];
why[0] = 0;
bool ok = ft::staged_geometry_ok(g.gu_row, g.d_row, g.up_off, g.down_off);
if (!ok) {
std::snprintf(why, sizeof why, "rows and offsets must be 2-byte aligned (gu_row %zu d_row %zu up_off %zu down_off %zu)", g.gu_row, g.d_row, g.up_off, g.down_off);
} else {
for (int e = b.e0; e < b.e1; ++e) {
const uint8_t* p = b.blob[e - b.e0];
cudaPointerAttributes pa{};
ft::BlobMem m = ft::BlobMem::kUnknown;
if (cudaPointerGetAttributes(&pa, p) != cudaSuccess) cudaGetLastError(); // (not a pointer the driver knows: the launch's own error says so)
else m = pa.type == cudaMemoryTypeDevice ? ft::BlobMem::kDevice : pa.type == cudaMemoryTypeManaged ? ft::BlobMem::kManaged : pa.type == cudaMemoryTypeHost ? ft::BlobMem::kHost : ft::BlobMem::kUnregistered;
if (!ft::staged_blob_ok(m, reinterpret_cast<uintptr_t>(p))) {
std::snprintf(why, sizeof why, "expert blobs must be in device memory at 2-byte aligned addresses (expert %d: %p, memory type %d)", e, (const void*) p, (int) pa.type);
ok = false;
break;
}
}
}
std::lock_guard<std::mutex> lk(g_staged_mu);
if (g_staged_guard.report(ok))
std::fprintf(stderr, "prefill fused experts (native): %s; this launch and every later one in this process take the unstaged kernel (same results, slower)\n", why);
if (!ok) ++g_staged_fallbacks;
return ok;
}
#endif
// `staged`: the launch's gate/up kernel is a weight-staging instance (sw_on); the rule then takes 128 rows from every size on the sm_86 / sm_120 cards under 80 SMs.
@@ -2599,8 +2613,31 @@ int pick_ww(int64_t n, int n_expert, bool staged, bool wide) {
return pk.ww;
}
// The work item of one launch of the native kernels: pick_ww's, unless the launch has a weight-staging kernel (`staged`: its gate/up kernel is one; the down's is looked at here) and
// the staging check fails (staged_blobs_ok), in which case the unstaged 64-row instances run (fused_tile::kUnstagedWw), also when STRATA_PF_FUSED_TILE=128 forces the 128-row tile.
// Every call site that launches `launch<...>` with a ww from pick_ww on a batch of blobs takes its ww from here.
int launch_ww(const Batch& b, const NativeGeom& g, int64_t n, int n_expert, bool staged, bool wide) {
#if !defined(__HIPCC__)
if ((staged || sw_on(g.d_type, false)) && !staged_blobs_ok(b, g)) return strata::prefill::fused_tile::kUnstagedWw;
#else
(void) b;
(void) g;
#endif
return pick_ww(n, n_expert, staged, wide);
}
} // namespace
// Launches that took the unstaged kernel because the staging check failed (see staged_blobs_ok); 0 in a HIP build, which has no staging kernels.
int staged_blob_fallbacks() {
#if !defined(__HIPCC__)
std::lock_guard<std::mutex> lk(g_staged_mu);
return g_staged_fallbacks;
#else
return 0;
#endif
}
bool gfx12_iq2s_default() {
static const bool on = [] {
const char* v = std::getenv("STRATA_PF_FUSED_IQ2S");
@@ -2850,10 +2887,7 @@ void experts_native(const Batch& b, const NativeGeom& g, int n_expert, int64_t n
// the launch's gate/up kernel is a staging instance: the tile rule then takes 128 rows (the gate/up is about two thirds of the layer; a down that is not staged, Q2_0 or Q8_0 or Q5_0,
// still ran faster in the 128-row tile with it: IQ3_S / Q2_0 on the RTX A4000, RESULTS-P4-SM.md S1). Either kernel staged: the blob check.
const bool staged = sw_on(g.gu_type, true), wide = g.gu_type == T_Q6_K || g.gu_type == T_Q8_0; // wide: the Q6_K / Q8_0 gate/up class of the sm_86 tile line (40+ SMs)
#if !defined(__HIPCC__)
if (staged || sw_on(g.d_type, false)) check_staged_blobs(b, g);
#endif
const int ww = pick_ww(n, n_expert, staged, wide);
const int ww = launch_ww(b, g, n, n_expert, staged, wide); // (the staging check inside: a launch that fails it takes the unstaged 64-row kernel)
// the most 64-row tiles the batch can have (every row in it, plus a partial tile per expert) - the items of the
// ones inside a larger item end at once
const int64_t tiles = (n + kTileRows - 1) / kTileRows + (b.e1 - b.e0);
+4 -4
View File
@@ -114,11 +114,11 @@ void experts_native_ext(const Batch& b, const NativeGeom& g, int n_expert, int64
}
const cudaStream_t s = (cudaStream_t) stream;
const Tables tb = tables(const_cast<void*>(scratch), n_expert);
// staged: the launch's gate/up or down kernel is a weight-staging instance (the K-quant downs are since the staging commit of this file's lane); the staged instances are the
// 128-row tile's, and the blob check applies to them as in experts_native
// staged: the launch's gate/up or down kernel is a weight-staging instance (the K-quant downs are, K = 512); the staged instances are the 128-row tile's. launch_ww (as in
// experts_native) runs the staging check for such a launch: one whose expert blobs are not device memory at 2-byte aligned addresses takes the unstaged 64-row kernel of the
// same formats (same output bits), sticky for the process, one log line, no exit. The wide argument (the Q6_K / Q8_0 gate/up class of the sm_86 tile line) is false for this path's gate/up.
const bool staged = sw_on(g.gu_type, true) || sw_on(g.d_type, false);
if (staged) check_staged_blobs(b, g);
const int ww = pick_ww(n, n_expert, staged, g.gu_type == T_Q6_K || g.gu_type == T_Q8_0);
const int ww = launch_ww(b, g, n, n_expert, staged, g.gu_type == T_Q6_K || g.gu_type == T_Q8_0);
const int64_t tiles = (n + kTileRows - 1) / kTileRows + (b.e1 - b.e0);
const unsigned k_gu = (unsigned) std::min<int64_t>(tiles * (GU_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_kq);
const unsigned k_d = (unsigned) std::min<int64_t>(tiles * (D_ROWS_N / weight_rows(ww)), (int64_t) d.sms * (g.d_type == T_Q5_K ? kd.occ5 : kd.occ6));
+31 -3
View File
@@ -13,7 +13,8 @@
// different expert ranges. Part 2 (timing): one layer at the engine's chunks (2048, 3584, 8192 tokens; 512 experts,
// top 10) on both paths and on MMQ's products alone (no gathers), for the IQ2_XS, IQ3_XXS and IQ3_S packs' most
// common layers, with the grouping checked and a sample of rows against the reference. --no-ref, --no-timing,
// --only=NAME, --edge, --chunks=A,B, --blob-shift=N, --unset-pf-fused (STRATA_PF_FUSED left unset instead of set to 1: the engine's default, for the
// --only=NAME, --edge, --chunks=A,B, --blob-shift=N, --host-blobs=N [--expect-fallback | --expect-no-fallback] (the first N experts' blobs in host memory: the weight-staging
// check fails for the launches that hold one and they take the unstaged kernel, same output bits), --unset-pf-fused (STRATA_PF_FUSED left unset instead of set to 1: the engine's default, for the
// STRATA_PF_FUSED_Q2N=1 ctest entry). Exit 77 without a CUDA device of sm_80 or newer.
#include "strata/core/emulate.hpp"
#include "strata/prefill/moe_fused_iq.hpp"
@@ -117,6 +118,8 @@ void tq_block(int t, const uint8_t* b, float* out, bool int8cb) {
// hadamard128_rows (the same device code: the reference is the independent side); the gate/up input is rotated on the host for every path.
int g_blob_shift = 0; // --blob-shift=N (even, 0-14; the timing part): every expert blob starts N bytes past its 16-byte aligned place, as in the engine's cache slots (stage-ring and slot offsets are only 2-byte aligned); use it with S20_NOMMQ=1 (MMQ wants aligned blobs)
bool g_folded = false;
int g_host_blobs = 0; // --host-blobs=N (the timing part): the first N experts' blobs in pinned host memory, see timing_part; use with --no-ref (the reference part is not affected)
int g_expect_fallback = 0; // --expect-fallback (+1) / --expect-no-fallback (-1): with --host-blobs, require that some / no launch took the unstaged kernel
void rot128(float* x, int n, const float* signs) {
for (int b = 0; b < n; b += 128) {
double v[128];
@@ -578,6 +581,22 @@ void timing_part(const Pair& p, int T, cudaStream_t s) {
}
std::vector<const uint8_t*> blob((size_t) E);
for (int e = 0; e < E; ++e) blob[(size_t) e] = blobs.as<uint8_t>() + (size_t) e * stride + g_blob_shift;
// --host-blobs=N: the first N experts' blobs live in pinned, mapped HOST memory (the kernels can still read them through the host pointer), not device memory: the weight-staging
// check of experts_native fails for the launches that hold one, which then take the unstaged kernel (no process exit); the output bits must equal the all-device run's
struct HostBlobs {
void* p = nullptr;
~HostBlobs() { if (p) cudaFreeHost(p); }
} host_blobs;
if (g_host_blobs > 0) {
const int nh = std::min(g_host_blobs, E);
ck(cudaHostAlloc(&host_blobs.p, (size_t) nh * stride + 4096, cudaHostAllocMapped), "cudaHostAlloc");
std::memset(host_blobs.p, 0, (size_t) nh * stride + 4096);
for (int e = 0; e < nh; ++e) {
uint8_t* dst = (uint8_t*) host_blobs.p + (size_t) e * stride + g_blob_shift;
ck(cudaMemcpy(dst, blob[(size_t) e], geo.bytes, cudaMemcpyDeviceToHost), "blob to host");
blob[(size_t) e] = dst;
}
}
std::vector<float> x = make_x(T, rng, -1);
const Folded fo(tq_type(p.gu), rows); // (--folded trellis pairs: rotated input, the H rotation on both paths, inside their times)
fo.rotate_x(x);
@@ -633,7 +652,7 @@ void timing_part(const Pair& p, int T, cudaStream_t s) {
}
}
// ~1 s of both first (the clocks ramp up), then alternating rounds; the medians
const bool no_mmq = !mq || std::getenv("S20_NOMMQ") != nullptr; // (debug: the fused runs alone)
const bool no_mmq = !mq || std::getenv("S20_NOMMQ") != nullptr || g_host_blobs > 0; // (debug: the fused runs alone; MMQ does not read host blobs)
for (float spent = 0; spent < 1000.0f;) spent += (no_mmq ? 0.0f : once(mmq_run)) + once(fused_run);
std::vector<float> tm, tf, td;
for (int rep = 0; rep < 8; ++rep) {
@@ -739,7 +758,7 @@ void timing_part(const Pair& p, int T, cudaStream_t s) {
for (size_t j = 0; j < pick.size(); ++j) {
const size_t i = pick[j];
const int e = ids[i];
ck(cudaMemcpy(hb.data(), blob[(size_t) e], geo.bytes, cudaMemcpyDeviceToHost), "blob back");
ck(cudaMemcpy(hb.data(), blob[(size_t) e], geo.bytes, cudaMemcpyDefault), "blob back"); // (default kind: a --host-blobs blob is host memory)
dequant(p, geo, hb, gu, dn);
const float* xr = x.data() + (i / K) * N;
for (int f = 0; f < FF; ++f) {
@@ -792,6 +811,12 @@ void timing_part(const Pair& p, int T, cudaStream_t s) {
cudaEventDestroy(a);
cudaEventDestroy(b);
delete mbp;
if (g_host_blobs > 0) {
const int fbk = fused::staged_blob_fallbacks();
std::printf(" host blobs (first %d experts): %d launches took the unstaged kernel (the staging check failed; no process exit)\n", g_host_blobs, fbk);
if (g_expect_fallback > 0 && fbk == 0) throw std::runtime_error(std::string(p.name) + ": --expect-fallback: no launch took the unstaged kernel");
if (g_expect_fallback < 0 && fbk != 0) throw std::runtime_error(std::string(p.name) + ": --expect-no-fallback: a launch took the unstaged kernel");
}
// the two int8 paths at the real shape, every tile of the layer: R9700 rel RMS 1.3-1.9e-4, worst row 2.9e-3-8.3e-3
// (STRATA_PF_F16=1: F16 products against MMQ's int8, gfx1151 rel RMS 2.5-2.7e-3, worst row 6.4e-3-8.8e-3)
// (K-quant gate/up pairs: fused against MMQ is information only, see kq_gu)
@@ -875,6 +900,9 @@ int main(int argc, char** argv) {
if (std::strcmp(argv[i], "--no-ref") == 0) ref = false;
if (std::strcmp(argv[i], "--edge") == 0) edge = true;
if (std::strncmp(argv[i], "--blob-shift=", 13) == 0) g_blob_shift = std::atoi(argv[i] + 13);
if (std::strncmp(argv[i], "--host-blobs=", 13) == 0) g_host_blobs = std::atoi(argv[i] + 13);
if (std::strcmp(argv[i], "--expect-fallback") == 0) g_expect_fallback = 1;
if (std::strcmp(argv[i], "--expect-no-fallback") == 0) g_expect_fallback = -1;
if (std::strcmp(argv[i], "--folded") == 0) g_folded = true; // (the trellis pairs as Hadamard-folded experts: see g_folded)
if (std::strncmp(argv[i], "--chunks=", 9) == 0) {
chunks.clear();
+32 -3
View File
@@ -10,7 +10,9 @@
// Part 1 (reference), per pair: 256 tokens over 64 experts, skewed routing (experts with 0 rows, with one, with several 64-row tiles), an
// all-zero token, per-expert blobs at unrelated (2-byte aligned) addresses, three launches of different expert ranges; then the tails and
// tile edges (33, 1, 130, 517 tokens). Part 2 (timing): one layer at the engine's chunks (2048, 8192 tokens; 256 experts, top 8) on both
// paths, the grouping checked and a sample of rows against the reference. --no-ref, --no-timing, --only=NAME, --chunks=A,B.
// paths, the grouping checked and a sample of rows against the reference. --no-ref, --no-timing, --only=NAME, --chunks=A,B,
// --host-blobs=N [--expect-fallback | --expect-no-fallback] (the first N experts' blobs in host memory: the weight-staging check fails for the launches that hold one and they take the
// unstaged kernel, same output bits; timing part, use with --no-ref).
// Exit 77 without a CUDA device of sm_80 or newer.
#include "strata/prefill/moe_fused.hpp"
#include "strata/prefill/moe_fused_q36.hpp"
@@ -60,6 +62,8 @@ struct Pair {
bool optional = false; // a pair this build may not cover yet: skipped with a printed line instead of failing (UD-Q4_K_XL's K-quant downs)
};
bool mmq_ok(const Pair& p) { return mmq::supported((int) p.gu) && mmq::supported((int) p.d); }
int g_host_blobs = 0; // --host-blobs=N (the timing part): the first N experts' blobs in pinned host memory, see timing_part; use with --no-ref
int g_expect_fallback = 0; // --expect-fallback (+1) / --expect-no-fallback (-1): with --host-blobs, require that some / no launch took the unstaged kernel
size_t row_size(ggml_type t, int64_t n) { return (size_t) (n / ggml_blck_size(t)) * ggml_type_size(t); }
struct Geo {
@@ -475,13 +479,29 @@ void timing_part(const Pair& p, int T, cudaStream_t s) {
}
std::vector<const uint8_t*> blob((size_t) E);
for (int e = 0; e < E; ++e) blob[(size_t) e] = blobs.as<uint8_t>() + (size_t) e * stride;
// --host-blobs=N: the first N experts' blobs live in pinned, mapped HOST memory (the kernels can still read them through the host pointer), not device memory: the weight-staging
// check of the launch fails for the launches that hold one, which then take the unstaged 64-row kernel (no process exit); the output bits must equal the all-device run's
struct HostBlobs {
void* p = nullptr;
~HostBlobs() { if (p) cudaFreeHost(p); }
} host_blobs;
if (g_host_blobs > 0) {
const int nh = std::min(g_host_blobs, E);
ck(cudaHostAlloc(&host_blobs.p, (size_t) nh * stride + 4096, cudaHostAllocMapped), "cudaHostAlloc");
std::memset(host_blobs.p, 0, (size_t) nh * stride + 4096);
for (int e = 0; e < nh; ++e) {
uint8_t* dst = (uint8_t*) host_blobs.p + (size_t) e * stride;
ck(cudaMemcpy(dst, blob[(size_t) e], geo.bytes, cudaMemcpyDeviceToHost), "blob to host");
blob[(size_t) e] = dst;
}
}
std::vector<float> x = make_x(T, rng, -1);
const std::vector<int32_t> ids = make_ids(T, E, rng, 0, 0);
const Routing r = sort_rows(ids, E);
Dev x_dev(x.size() * 4), src_dev((size_t) rows * 4);
ck(cudaMemcpy(x_dev.p, x.data(), x.size() * 4, cudaMemcpyHostToDevice), "x");
ck(cudaMemcpy(src_dev.p, r.src.data(), r.src.size() * 4, cudaMemcpyHostToDevice), "src");
const bool mq = mmq_ok(p);
const bool mq = mmq_ok(p) && g_host_blobs == 0; // (MMQ does not read host blobs)
mmq::Context ctx;
MmqBufs* mbp = mq ? new MmqBufs(p, rows, E) : nullptr;
FusedBufs fb(T, rows, E);
@@ -546,7 +566,7 @@ void timing_part(const Pair& p, int T, cudaStream_t s) {
RefWork rw;
for (size_t j = 0; j < pick.size(); ++j) {
const size_t i = pick[j];
ck(cudaMemcpy(hb.data(), blob[(size_t) ids[i]], geo.bytes, cudaMemcpyDeviceToHost), "blob back");
ck(cudaMemcpy(hb.data(), blob[(size_t) ids[i]], geo.bytes, cudaMemcpyDefault), "blob back"); // (default kind: a --host-blobs blob is host memory)
dequant(p, geo, hb, gu, dn);
rw.run(gu, dn, x.data() + (i / K) * N, ref.data() + j * N, ref2.data() + j * N);
if (mq) std::copy_n(y_mmq.begin() + (ptrdiff_t) ((size_t) r.row_of[i] * N), N, ym.begin() + (ptrdiff_t) (j * N));
@@ -562,6 +582,12 @@ void timing_part(const Pair& p, int T, cudaStream_t s) {
cudaEventDestroy(a);
cudaEventDestroy(b);
delete mbp;
if (g_host_blobs > 0) {
const int fbk = fq::staged_blob_fallbacks();
std::printf(" host blobs (first %d experts): %d launches took the unstaged kernel (the staging check failed; no process exit)\n", g_host_blobs, fbk);
if (g_expect_fallback > 0 && fbk == 0) throw std::runtime_error(std::string(p.name) + ": --expect-fallback: no launch took the unstaged kernel");
if (g_expect_fallback < 0 && fbk != 0) throw std::runtime_error(std::string(p.name) + ": --expect-no-fallback: a launch took the unstaged kernel");
}
// the two int8 paths at the real shape, every tile of the layer (K-quant gate/up pairs: fused against MMQ is information only, see kq_gu)
if (mq && !kq_gu(p) && (efm.rms > 1e-3 || efm.worst > 2e-2))
throw std::runtime_error(std::string(p.name) + ": the paths disagree at the real shape");
@@ -601,6 +627,9 @@ int main(int argc, char** argv) {
if (std::strncmp(argv[i], "--only=", 7) == 0) only = argv[i] + 7;
if (std::strcmp(argv[i], "--no-timing") == 0) timing = false;
if (std::strcmp(argv[i], "--no-ref") == 0) ref = false;
if (std::strncmp(argv[i], "--host-blobs=", 13) == 0) g_host_blobs = std::atoi(argv[i] + 13);
if (std::strcmp(argv[i], "--expect-fallback") == 0) g_expect_fallback = 1;
if (std::strcmp(argv[i], "--expect-no-fallback") == 0) g_expect_fallback = -1;
if (std::strncmp(argv[i], "--chunks=", 9) == 0) {
chunks.clear();
for (const char* c = argv[i] + 9; *c;) {
+50
View File
@@ -210,5 +210,55 @@ int main() {
CHECK(wrong == 0);
std::printf("fused_tile_rule_test: %lld launches compared against the previous rule (%lld differing), %lld launches of the 40-79 SM class checked, %d failed\n", compared,
differing, new_class, failed);
// ---- the weight-staging guard (check_staged_blobs's decision): a failed check makes the launch take the unstaged kernel, it never ends the process
{
using namespace strata::prefill::fused_tile;
// the blob: only device and managed memory at an even address pass; a pointer the driver does not know (kUnknown) is left to the launch's own error, as before
long long cases = 0;
for (BlobMem m : {BlobMem::kDevice, BlobMem::kManaged, BlobMem::kHost, BlobMem::kUnregistered, BlobMem::kUnknown})
for (std::uintptr_t a : {(std::uintptr_t) 0x7f0000001000, (std::uintptr_t) 0x7f0000001002, (std::uintptr_t) 0x7f0000001001, (std::uintptr_t) 0x7f0000001fff, (std::uintptr_t) 0}) {
++cases;
const bool want = m == BlobMem::kUnknown || ((m == BlobMem::kDevice || m == BlobMem::kManaged) && (a & 1) == 0);
CHECK(staged_blob_ok(m, a) == want);
}
CHECK(cases == 25);
static_assert(staged_blob_ok(BlobMem::kDevice, 0x1000) && !staged_blob_ok(BlobMem::kHost, 0x1000) && !staged_blob_ok(BlobMem::kDevice, 0x1001));
// the geometry: every combination of odd / even for the four values; ok only when all four are even
for (int bits = 0; bits < 16; ++bits) {
const std::size_t v[4] = {(std::size_t) (1000 + (bits & 1)), (std::size_t) (2000 + ((bits >> 1) & 1)), (std::size_t) (3000 + ((bits >> 2) & 1)), (std::size_t) (4000 + ((bits >> 3) & 1))};
CHECK(staged_geometry_ok(v[0], v[1], v[2], v[3]) == (bits == 0));
}
CHECK(staged_geometry_ok(0, 0, 0, 0) && staged_geometry_ok(136 * 1280, 72 * 640, 136 * 640 * 2, 136 * 1280 * 2));
// the per-process state: the first 256 launches are checked, then trusted; one failure makes every later launch unstaged, and only the first failure is logged
{
StagedGuard g;
int checked = 0, trusted = 0;
for (int i = 0; i < 1000; ++i) {
const auto s = g.next();
CHECK(s != StagedGuard::Step::kUnstaged);
if (s == StagedGuard::Step::kCheck) { ++checked; CHECK(!g.report(true)); } else ++trusted;
}
CHECK(checked == 256 && trusted == 744 && !g.bad);
}
{
StagedGuard g;
for (int i = 0; i < 2; ++i) CHECK(g.next() == StagedGuard::Step::kCheck && !g.report(true));
CHECK(g.next() == StagedGuard::Step::kCheck);
CHECK(g.report(false)); // the first failure: logged
for (int i = 0; i < 2000; ++i) CHECK(g.next() == StagedGuard::Step::kUnstaged); // sticky, also past the 256 checked launches
CHECK(!g.report(false) && !g.report(true) && g.bad); // no second log line, and a passing report does not clear it
CHECK(g.next() == StagedGuard::Step::kUnstaged);
}
{ // a failure on the last checked launch, and one with the budget used up (a failure cannot come then: the launch is trusted, nothing to report)
StagedGuard g;
for (int i = 0; i < 255; ++i) CHECK(g.next() == StagedGuard::Step::kCheck);
CHECK(g.next() == StagedGuard::Step::kCheck && g.report(false));
CHECK(g.next() == StagedGuard::Step::kUnstaged);
}
// the fallback's item is the 64-row one (WW 4), which no staging instance shares, and the rule never answers it with a staged item for a launch it does not stage
CHECK(kUnstagedWw == 4);
CHECK(pick(Card{false, false, 48, true}, 81910, E, false, false).ww == 4 && pick(Card{false, false, 48, true}, 81910, E, true, true).ww == 2); // the staged 128-row choice the fallback overrides
std::printf("fused_tile_rule_test: staging guard: %lld blob cases, 16 geometries, the check budget and the sticky fallback checked, %d failed so far\n", cases, failed);
}
return failed == 0 ? 0 : 1;
}