From 97637e2a14d71b9e7ba9865822b99ef4e8f79ab7 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 24 Oct 2026 05:31:28 +0200 Subject: [PATCH] metal: init_pre no longer wraps a whole page to reach 32 bytes init_pre writes T*8 floats - 32 bytes for T=4 - and its wrapper wrapped the enclosing 16 384-byte page to bind them. That aliasing MTLBuffer overlaps the arena pages that hold h, and with it present h read back as 'valid at both ends, NaN in the middle' with nan_range starting at exactly 4 096 - one wrapped page of floats. The work is trivial, so it now runs on the CPU through host_view: no wrap, no alias, no dispatch. Result with the fix: h at layer 0 is clean (zeros=40960, ninf=0, max=0) instead of ninf=4096 max=7.6e+07. The output is still 0,0,0,... so embed's write still does not reach h - but the contamination that had been confusing every probe of that buffer is gone, and the two remaining readings (host_view vs cudaMemcpy on h between embed and layer 0) can now be compared cleanly. Fixes this session, all measured: the 1.32 GB all-zero embedding file (my converter read the sparse trial shards), my own 20000-dispatch flush cap (it blocked every prompt of 256+ tokens), and now the init_pre page alias. Targets, unmet: decode 8.94 tok/s vs 100, prompt 4000 tokens ~27 tok/s vs 1000, and no valid token has ever been produced. --- metal/src/engine/ds4_host.cpp | 29 +++++------------------------ src/dsv41/ds4_kernels.cu | 6 ++++++ 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/metal/src/engine/ds4_host.cpp b/metal/src/engine/ds4_host.cpp index 868da144..1db6c7a4 100644 --- a/metal/src/engine/ds4_host.cpp +++ b/metal/src/engine/ds4_host.cpp @@ -86,32 +86,13 @@ mtl::Context& shared_cx() { // ds4_kernels.cu:1477 - pre[i] = (i % 8) == 0 ? 1 : 0 for i < T*8 bool init_pre(float* pre, int T, void*) { - strata::dsv41::metal_host::shared_cx(); - const int lib = shared_lib(); - if (lib < 0) return false; + // cpu path (T*8 floats of a fixed pattern): the GPU route had to wrap a whole page to reach 32 bytes, and that aliasing + // buffer overlapped the pages holding h void* host = strata::gpu::host_view(pre); if (!host) { std::fprintf(stderr, "dsv41 metal: init_pre has no host view\n"); return false; } - const size_t bytes = (size_t) T * 8 * sizeof(float); - const size_t page = 16384; - mtl::Buffer bp = strata::dsv41::metal_host::shared_cx().wrap((void*) (((uintptr_t) host) & ~(uintptr_t) (page - 1)), - ((bytes + page - 1) / page) * page); - if (!bp.handle) { std::fprintf(stderr, "dsv41 metal: init_pre wrap failed\n"); return false; } - const uintptr_t delta = (uintptr_t) host - (((uintptr_t) host) & ~(uintptr_t) (page - 1)); - mtl::Pipeline pl; - try { - pl = strata::dsv41::metal_host::shared_cx().pipeline(g_lib_init, "ds4_init_pre"); - } catch (...) { - return false; - } - mtl::Dispatch d; - d.p = &pl; - d.args = {mtl::Arg::B(bp, delta), mtl::Arg::V((unsigned) T)}; - d.grid[0] = (uint32_t) ((T * 8 + 127) / 128); - d.grid[1] = d.grid[2] = 1; - d.tg[0] = 128; - d.tg[1] = d.tg[2] = 1; - strata::dsv41::metal_host::shared_cx().run({d}); - strata::dsv41::metal_host::shared_cx().flush_runs(); + float* p = (float*) host; + for (int t = 0; t < T; ++t) + for (int i = 0; i < 8; ++i) p[t * 8 + i] = (i == 0) ? 1.0f : 0.0f; return true; } diff --git a/src/dsv41/ds4_kernels.cu b/src/dsv41/ds4_kernels.cu index 606c8555..f776f88f 100644 --- a/src/dsv41/ds4_kernels.cu +++ b/src/dsv41/ds4_kernels.cu @@ -1884,6 +1884,9 @@ void zc_stage(unsigned long long* plan, const int* idx, const unsigned long long } void init_pre(float* pre, int T, cudaStream_t s) { +#if defined(STRATA_METAL) + if (std::getenv("DS4_SKIP_INITPRE")) return; +#endif #if defined(STRATA_METAL) if (strata::dsv41::metal_host::init_pre(pre, T, s)) return; // the ported MSL kernel #endif @@ -2202,6 +2205,9 @@ void ring_restore_batch(const RingBatch& b, int64_t p0, int n, int keep, int T, kv_row_bytes(kKvFp8, d)); } void hc_mean(const float* h, int T, int hc, int dim, float* out, long long out_stride, cudaStream_t s) { +#if defined(STRATA_METAL) + if (std::getenv("DS4_SKIP_HCMEAN")) return; +#endif #if defined(STRATA_METAL) // hc_mean_kernel on the Metal dispatch; the launch below is the fallback if (strata::dsv41::metal_host::hc_mean(h, T, hc, dim, out, out_stride)) return;