mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
fix(metal): wrap a Gyro file without its PLE table: the head and tail ranges only, no read-ahead of the table
This commit is contained in:
1 parent
028b96373c
commit
8baf901707
3 files changed
+75
-26
No files matched your search
+39
-26
@@ -737,34 +737,47 @@ bool Window::init(const strata::core::ModelGeometry& g, strata::core::SessionSta
|
||||
st_ = std::make_unique<ExpertStream>();
|
||||
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<std::pair<size_t, size_t>> ranges;
|
||||
size_t head = 0, tail = 0;
|
||||
if (ple && ple->bytes > 0) {
|
||||
head = std::min<size_t>(ple->offset / pg * pg, f_.map_bytes);
|
||||
tail = std::min<size_t>((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<size_t>(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<size_t>(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<size_t>(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<size_t>(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];
|
||||
|
||||
@@ -23,6 +23,8 @@ struct Buffer {
|
||||
std::shared_ptr<std::vector<void*>> 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<std::vector<size_t>> starts;
|
||||
template <class T> T* as() const { return static_cast<T*>(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<std::pair<size_t, size_t>>& ranges);
|
||||
void free(Buffer& b);
|
||||
|
||||
// Encodes `ds` into ONE compute encoder (serial or concurrent dispatch type) and ONE command buffer, `reps`
|
||||
|
||||
@@ -123,6 +123,12 @@ static inline id<MTLBuffer> B(const Buffer& b) { return (__bridge id<MTLBuffer>)
|
||||
// the MTLBuffer and offset that bind byte `off` of b (a chunked buffer: the chunk the offset selects)
|
||||
static inline id<MTLBuffer> 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<MTLBuffer>)(*b.chunks)[k];
|
||||
}
|
||||
const size_t k = std::min(off / b.stride, b.chunks->size() - 1);
|
||||
o = off - k * b.stride;
|
||||
return (__bridge id<MTLBuffer>)(*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<std::pair<size_t, size_t>>& ranges) {
|
||||
Buffer r;
|
||||
r.chunks = std::make_shared<std::vector<void*>>();
|
||||
r.starts = std::make_shared<std::vector<size_t>>();
|
||||
size_t total = 0;
|
||||
for (const auto& rg : ranges) {
|
||||
id<MTLBuffer> 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<MTLBuffer> b = [impl_->dev newBufferWithBytesNoCopy:p length:bytes options:MTLResourceStorageModeShared deallocator:nil];
|
||||
if (!b) {
|
||||
|
||||
Reference in new issue
Block a user