Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
metal: wrap_split_test - a sparse file longer than maxBufferLength, wrapped in chunks, read at every chunk boundary (#2338)
Niko122138978a6dfd50
diff --git a/metal/kernels/wrap_probe.metal b/metal/kernels/wrap_probe.metalnew file mode 100644index 000000000..1cc2a7a1d--- /dev/null+++ b/metal/kernels/wrap_probe.metal@@ -0,0 +1,12 @@+// wrap_probe.metal - the tests/wrap_split_test.cpp kernel: reads a window at the start of a bound range and another at+// its far end (`far` floats on), to prove a chunked no-copy buffer (Context::wrap with a span) binds the whole range.+#include <metal_stdlib>+using namespace metal;++kernel void probe_copy(device const float* src [[buffer(0)]], device float* dst [[buffer(1)]],+ device const uint* par [[buffer(2)]], uint i [[thread_position_in_grid]]) {+ const uint n = par[0], far = par[1];+ if (i >= n) return;+ dst[i] = src[i];+ dst[n + i] = src[far + i];+}diff --git a/metal/tests/wrap_split_test.cpp b/metal/tests/wrap_split_test.cppnew file mode 100644index 000000000..f7c1e04f1--- /dev/null+++ b/metal/tests/wrap_split_test.cpp@@ -0,0 +1,90 @@+// metal/tests/wrap_split_test.cpp - Context::wrap(ptr, bytes, max_span) (#2338): a file longer than maxBufferLength is+// wrapped as overlapping no-copy buffers, and a dispatch that binds the buffer at ANY offset sees the bytes of that+// range, whole, through the chunk the offset selects (encode() and run_icb()). The file is sparse (no disk used+// beyond the few pages written). Exit 0 = every check passed.+// wrap_split_test [--factor 1.6] the file is factor x maxBufferLength+#include "mtl_rt.h"++#include <fcntl.h>+#include <sys/mman.h>+#include <unistd.h>++#include <cstdio>+#include <cstdlib>+#include <cstring>+#include <string>+#include <vector>++static int failures = 0;+static void check(bool ok, const std::string& what) {+ std::printf("%s %s\n", ok ? "ok " : "FAIL", what.c_str());+ failures += !ok;+}++int main(int argc, char** argv) {+ double factor = 1.6;+ for (int i = 1; i + 1 < argc; ++i)+ if (!std::strcmp(argv[i], "--factor")) factor = std::atof(argv[i + 1]);+ mtl::Context cx;+ const size_t pg = 16384, maxlen = cx.max_buffer_length() / pg * pg;+ const size_t bytes = ((size_t) (factor * (double) maxlen) + pg - 1) / pg * pg;+ const size_t span = 256ull << 20; // the largest range bound at one offset: 256 MiB+ std::printf("maxBufferLength %.2f GB, file %.2f GB, span %.0f MiB\n", maxlen / 1e9, bytes / 1e9, span / 1048576.0);++ char path[] = "/tmp/wrap_split_XXXXXX";+ const int fd = mkstemp(path);+ if (fd < 0 || ftruncate(fd, (off_t) bytes) != 0) { std::printf("FAIL cannot make the sparse file\n"); return 1; }+ unlink(path);+ void* map = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);+ if (map == MAP_FAILED) { std::printf("FAIL mmap\n"); return 1; }++ const int lib = cx.compile({"wrap_probe.metal"});+ const mtl::Pipeline pipe = cx.pipeline(lib, "probe_copy");+ mtl::Buffer W = cx.wrap(map, bytes, span);+ check(W.chunks && W.chunks->size() >= 2, "the file is wrapped as " + std::to_string(W.chunks ? W.chunks->size() : 0) + " chunks");+ if (!W.chunks) return 1;+ const size_t stride = W.stride;+ check(stride == maxlen - span, "stride = maxBufferLength - span");++ // starts of ranges bound at one offset: the file's start, either side of every chunk boundary, the last range+ std::vector<size_t> starts = {0, 4096, pg};+ for (size_t k = 1; k * stride < bytes; ++k)+ for (long d : {-(long) pg, 0L, (long) pg, 3L * (long) pg}) {+ const long s = (long) (k * stride) + d;+ if (s > 0 && (size_t) s + span <= bytes) starts.push_back((size_t) s);+ }+ starts.push_back(bytes - span);+ const uint32_t n = 4096, far = (uint32_t) (span / 4) - n;+ mtl::Buffer dst = cx.alloc(2 * n * 4), par = cx.alloc(8);+ auto* P = par.as<uint32_t>();+ P[0] = n; P[1] = far;+ int bad_enc = 0, bad_icb = 0;+ for (size_t s : starts) {+ s = s / 4 * 4;+ auto* f = reinterpret_cast<float*>(static_cast<uint8_t*>(map) + s);+ for (uint32_t i = 0; i < n; ++i) { f[i] = (float) (s / pg) * 10.0f + (float) i; f[far + i] = -(float) (s / pg) - 0.25f * (float) i; }+ for (int mode = 0; mode < 2; ++mode) {+ std::memset(dst.ptr, 0, dst.bytes);+ mtl::Dispatch d;+ d.p = &pipe;+ d.args = {mtl::Arg::B(W, s), mtl::Arg::B(dst), mtl::Arg::B(par)};+ d.grid[0] = n / 64; d.tg[0] = 64;+ if (mode == 0) cx.run({d}); else cx.run_icb({d}, 1, false);+ const float* o = dst.as<float>();+ int bad = 0;+ for (uint32_t i = 0; i < n; ++i)+ bad += o[i] != (float) (s / pg) * 10.0f + (float) i || o[n + i] != -(float) (s / pg) - 0.25f * (float) i;+ (mode == 0 ? bad_enc : bad_icb) += bad != 0;+ if (bad) std::printf(" offset %zu (%s): %d wrong values\n", s, mode == 0 ? "encode" : "icb", bad);+ }+ }+ check(bad_enc == 0, "encode(): " + std::to_string(starts.size()) + " offsets, both ends of each range read right");+ check(bad_icb == 0, "run_icb(): the same offsets");+ cx.free(W);+ cx.free(dst);+ cx.free(par);+ munmap(map, bytes);+ close(fd);+ std::printf(failures ? "FAILED\n" : "PASS\n");+ return failures ? 1 : 0;+}