diff --git a/metal/src/engine/window.cpp b/metal/src/engine/window.cpp index b4f7e06111..2358f4858f 100644 --- a/metal/src/engine/window.cpp +++ b/metal/src/engine/window.cpp @@ -737,34 +737,47 @@ bool Window::init(const strata::core::ModelGeometry& g, strata::core::SessionSta st_ = std::make_unique(); if (!st_->open(f_, err)) return false; } else { - { // read ahead the weights, not the n-gram table: in a Gyro file it is 28.8 GB in the middle of the one file, read by the host from - // the SSD a few rows at a time (src/ngram/ple_reader.cpp), and must not fill the page cache - const gguf::Tensor* ple = f_.find("per_layer_token_embd.weight"); - if (ple == nullptr) { - madvise(f_.map, f_.map_bytes, MADV_WILLNEED); - } else { - const size_t page = 16384; - const size_t lo = (size_t) ple->offset / page * page, hi = ((size_t) (ple->offset + ple->bytes) + page - 1) / page * page; - if (lo > 0) madvise(f_.map, lo, MADV_WILLNEED); - if (hi < f_.map_bytes) madvise((uint8_t*) f_.map + hi, f_.map_bytes - hi, MADV_WILLNEED); + // a Gyro file holds the PLE table (54 GB of the 92) in the same GGUF: the host code reads it with direct I/O, + // no kernel touches it, so it is neither wrapped nor read ahead (it would take its 54 GB of file pages and + // wired GPU residency); the rest is wrapped as the ranges around it + const gguf::Tensor* ple = f_.find("per_layer_token_embd.weight"); + const size_t pg = 16384, maxlen = (size_t) cx_->max_buffer_length() / pg * pg; + std::vector> ranges; + size_t head = 0, tail = 0; + if (ple && ple->bytes > 0) { + head = std::min(ple->offset / pg * pg, f_.map_bytes); + tail = std::min((ple->offset + ple->bytes + pg - 1) / pg * pg, f_.map_bytes); + if (head > 0) ranges.push_back({0, head}); + if (tail < f_.map_bytes) ranges.push_back({tail, f_.map_bytes}); + } + bool split = !ranges.empty() && !(std::getenv("STRATA_METAL_WRAP_PLE") && std::string(std::getenv("STRATA_METAL_WRAP_PLE")) == "1"); + for (const auto& rg : ranges) + if (rg.second - rg.first > maxlen) split = false; + if (split) { + for (const auto& rg : ranges) madvise((uint8_t*) f_.map + rg.first, rg.second - rg.first, MADV_WILLNEED); + // pages of the table the header parse or an earlier run left in the mapping: not wanted + madvise((uint8_t*) f_.map + head, tail - head, MADV_RANDOM); + W_ = cx_->wrap_ranges(f_.map, ranges); + } else { + madvise(f_.map, f_.map_bytes, MADV_WILLNEED); + // the chunks of a model bigger than one Metal buffer overlap by the largest range bound at one offset: a tensor, + // or a layer's routed experts (gate, up and down, bound together at the first of them: moe_rebase) + size_t span = 0; + // (the PLE table is read from the file by the shared host code, never by a kernel: a Gyro file holds it in the same + // GGUF - 54 GB, more than a chunk can overlap by - so it does not set the span) + for (const auto& kv : f_.tensors) + if (kv.first != "per_layer_token_embd.weight") span = std::max(span, kv.second.bytes); + for (int l = 0; l < G_NL; ++l) { + const std::string b = "blk." + std::to_string(l) + "."; + const gguf::Tensor *g = f_.find(b + "ffn_gate_exps.weight"), *u = f_.find(b + "ffn_up_exps.weight"), + *d = f_.find(b + "ffn_down_exps.weight"); + if (!g || !u || !d) continue; + const uint64_t lo = std::min({g->offset, u->offset, d->offset}); + const uint64_t hi = std::max({g->offset + g->bytes, u->offset + u->bytes, d->offset + d->bytes}); + span = std::max(span, hi - lo); } + W_ = cx_->wrap(f_.map, f_.map_bytes, span); } - // the chunks of a model bigger than one Metal buffer overlap by the largest range bound at one offset: a tensor, - // or a layer's routed experts (gate, up and down, bound together at the first of them: moe_rebase) - size_t span = 0; - // (not the n-gram table: the host reads it from the SSD, the GPU never binds it - in a Gyro file it is 28.8 GB of the one file) - for (const auto& kv : f_.tensors) - if (kv.first != "per_layer_token_embd.weight") span = std::max(span, kv.second.bytes); - for (int l = 0; l < G_NL; ++l) { - const std::string b = "blk." + std::to_string(l) + "."; - const gguf::Tensor *g = f_.find(b + "ffn_gate_exps.weight"), *u = f_.find(b + "ffn_up_exps.weight"), - *d = f_.find(b + "ffn_down_exps.weight"); - if (!g || !u || !d) continue; - const uint64_t lo = std::min({g->offset, u->offset, d->offset}); - const uint64_t hi = std::max({g->offset + g->bytes, u->offset + u->bytes, d->offset + d->bytes}); - span = std::max(span, hi - lo); - } - W_ = cx_->wrap(f_.map, f_.map_bytes, span); if (W_.handle == nullptr) { err = "metal window: cannot wrap " + shard1 + " as a Metal buffer"; return false; } } n_vocab_ = (int64_t) T("output.weight").shape[1]; diff --git a/metal/src/mtl_rt.h b/metal/src/mtl_rt.h index 408767ec7d..6d26f855af 100644 --- a/metal/src/mtl_rt.h +++ b/metal/src/mtl_rt.h @@ -23,6 +23,8 @@ struct Buffer { std::shared_ptr> chunks; size_t stride = 0; size_t bias = 0; // bytes from the MTLBuffer's start to ptr (a view onto an allocation the runtime owns) + // or disjoint regions (Context::wrap_ranges): chunk k holds the bytes from starts[k]; the gaps are not wrapped + std::shared_ptr> starts; template T* as() const { return static_cast(ptr); } }; @@ -105,6 +107,9 @@ public: // the same, also when bytes > maxBufferLength: then overlapping chunks, each holding any range of up to // max_span bytes that starts in it (max_span = the largest tensor bound at one offset) Buffer wrap(void* page_aligned, size_t bytes, size_t max_span); + // only the given [begin, end) byte ranges (page-aligned, ascending, each up to maxBufferLength), bound by their + // offsets from the start of p: what lies between them is never wrapped, so it stays out of the GPU's working set + Buffer wrap_ranges(void* page_aligned, const std::vector>& ranges); void free(Buffer& b); // Encodes `ds` into ONE compute encoder (serial or concurrent dispatch type) and ONE command buffer, `reps` diff --git a/metal/src/mtl_rt.mm b/metal/src/mtl_rt.mm index 7a2bbbb8b4..b3bf41f6fd 100644 --- a/metal/src/mtl_rt.mm +++ b/metal/src/mtl_rt.mm @@ -123,6 +123,12 @@ static inline id B(const Buffer& b) { return (__bridge id) // the MTLBuffer and offset that bind byte `off` of b (a chunked buffer: the chunk the offset selects) static inline id BO(const Buffer& b, size_t off, size_t& o) { if (!b.chunks) { o = off + b.bias; return B(b); } // (bias: a view onto part of a bigger allocation; 0 otherwise) + if (b.starts) { + size_t k = b.starts->size() - 1; + while (k > 0 && (*b.starts)[k] > off) --k; + o = off - (*b.starts)[k]; + return (__bridge id)(*b.chunks)[k]; + } const size_t k = std::min(off / b.stride, b.chunks->size() - 1); o = off - k * b.stride; return (__bridge id)(*b.chunks)[k]; @@ -339,6 +345,31 @@ Buffer Context::wrap(void* p, size_t bytes, size_t max_span) { return r; } +Buffer Context::wrap_ranges(void* p, const std::vector>& ranges) { + Buffer r; + r.chunks = std::make_shared>(); + r.starts = std::make_shared>(); + size_t total = 0; + for (const auto& rg : ranges) { + id b = [impl_->dev newBufferWithBytesNoCopy:(uint8_t*) p + rg.first length:rg.second - rg.first + options:MTLResourceStorageModeShared deallocator:nil]; + if (!b) { + std::fprintf(stderr, "mtl: wrap range %zu+%zu failed\n", rg.first, rg.second - rg.first); + std::exit(4); + } + impl_->track(b); // (the #6078 keep-alive names it like every other buffer of the context) + r.chunks->push_back((__bridge_retained void*)b); + r.starts->push_back(rg.first); + total += rg.second - rg.first; + } + allocated_ += total; + r.handle = (*r.chunks)[0]; + r.ptr = p; + r.bytes = ranges.back().second; + std::fprintf(stderr, "mtl: wrapped %.2f GB as %zu separate ranges (of %.2f GB)\n", total / 1e9, ranges.size(), r.bytes / 1e9); + return r; +} + Buffer Context::wrap(void* p, size_t bytes) { id b = [impl_->dev newBufferWithBytesNoCopy:p length:bytes options:MTLResourceStorageModeShared deallocator:nil]; if (!b) {