From 98e21c1713a6d10769998c3271dd35245d32a581 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Thu, 12 Nov 2026 03:16:37 +0100 Subject: [PATCH] prefill: a failed weight-staging check takes the unstaged kernel for that launch instead of ending the process check_staged_blobs ended the process with std::exit(1) when an expert blob was not device / managed memory, or a row size or offset was odd. No default configuration reaches it (the prompt path passes expert-cache slots and stage-ring slots), but the engine must not exit there. The check is now staged_blobs_ok (bool) and launch_ww combines it with pick_ww: a launch that fails it gets ww = 4 (fused_tile::kUnstagedWw), the 64-row instances, which no weight-staging instance shares, also when STRATA_PF_FUSED_TILE=128 forces the 128-row tile. The output bits are the same (the identity tables of RESULTS-P4-SM.md: tile 64, 128 and the default hash equal). The first failing launch of a process logs one line to stderr. The check still runs on the first 256 launches only (a pointer query is a driver call); the decision is sticky: once one launch failed it, every later launch of the process takes the unstaged kernel without asking, because after the first launches nothing is checked any more and a call site that gave one bad blob will give more. The decision is a set of pure functions and a small state struct in fused_tile_rule.hpp (staged_geometry_ok, staged_blob_ok, StagedGuard), tested on the host in fused_tile_rule_test (blob memory kinds x addresses, the 16 odd / even geometries, the 256-launch budget, the sticky fallback, one log line). moe_fused_iq.hpp declares staged_blob_fallbacks(), the number of launches that took the fallback (0 in a HIP build). prefill_fused_iq_test takes --host-blobs=N (the first N experts' blobs in pinned host memory, timing part) with --expect-fallback / --expect-no-fallback; ctest entry prefill_fused_iq_test_hostblobs. moe_fused_iq.cu is also compiled a second time for the Qwen3.6 shape (moe_fused_iq_q36.cu includes it under a renamed namespace): the new names are inside the included source and carry no `fused` token outside namespaces, the state is per copy. A dispatch of that copy that launches staging instances itself (experts_native_ext) takes its ww from launch_ww(b, g, n, n_expert, staged, wide) instead of pick_ww. --- cmake/backends/cuda.cmake | 3 + include/strata/prefill/fused_tile_rule.hpp | 36 +++++++++ include/strata/prefill/moe_fused_iq.hpp | 3 + src/prefill/moe_fused_iq.cu | 86 +++++++++++++++------- tests/cuda/prefill_fused_iq_test.cpp | 34 ++++++++- tests/prefill/fused_tile_rule_test.cpp | 50 +++++++++++++ 6 files changed, 183 insertions(+), 29 deletions(-) diff --git a/cmake/backends/cuda.cmake b/cmake/backends/cuda.cmake index e24b222f8e..7d518fce88 100644 --- a/cmake/backends/cuda.cmake +++ b/cmake/backends/cuda.cmake @@ -288,6 +288,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) diff --git a/include/strata/prefill/fused_tile_rule.hpp b/include/strata/prefill/fused_tile_rule.hpp index 6a8e0d0e15..afe637714d 100644 --- a/include/strata/prefill/fused_tile_rule.hpp +++ b/include/strata/prefill/fused_tile_rule.hpp @@ -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 #include 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 diff --git a/include/strata/prefill/moe_fused_iq.hpp b/include/strata/prefill/moe_fused_iq.hpp index 91e7c81b14..70d7426971 100644 --- a/include/strata/prefill/moe_fused_iq.hpp +++ b/include/strata/prefill/moe_fused_iq.hpp @@ -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); diff --git a/src/prefill/moe_fused_iq.cu b/src/prefill/moe_fused_iq.cu index c3820bcf63..c87ee0664b 100644 --- a/src/prefill/moe_fused_iq.cu +++ b/src/prefill/moe_fused_iq.cu @@ -2507,32 +2507,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 lk(mu); - if (left <= 0) return; - --left; + std::lock_guard 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(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(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 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. @@ -2560,8 +2574,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 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"); @@ -2811,10 +2848,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); diff --git a/tests/cuda/prefill_fused_iq_test.cpp b/tests/cuda/prefill_fused_iq_test.cpp index 32007c8da5..eba825c9ef 100644 --- a/tests/cuda/prefill_fused_iq_test.cpp +++ b/tests/cuda/prefill_fused_iq_test.cpp @@ -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 blob((size_t) E); for (int e = 0; e < E; ++e) blob[(size_t) e] = blobs.as() + (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 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 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(); diff --git a/tests/prefill/fused_tile_rule_test.cpp b/tests/prefill/fused_tile_rule_test.cpp index 5174d5c0bd..76b27c6ba9 100644 --- a/tests/prefill/fused_tile_rule_test.cpp +++ b/tests/prefill/fused_tile_rule_test.cpp @@ -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; }