Skip to content

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++

fix(metal): the prompt's buffers get their own allocation when no arena chunk is big enough for them

Kingdon Barrettf037612bad91
diff --git a/docs/MAC.md b/docs/MAC.md
index b7db8e95e..668ee1c3e 100644
--- a/docs/MAC.md
+++ b/docs/MAC.md
@@ -138,7 +138,7 @@ The arena was 11.5-14.7 GB. Tokens per second, one token per step (no MTP), medi
- The SSD alone measures the same: 2.97 GB/s for one 1.75 MB read per expert against 2.30 GB/s for three 0.58 MB reads (6 threads, uncached).
- The arena's size matters most. With the model file, a 7.5 GB arena gave 5.5 tok/s, 10.5 GB gave 6.7 and 12 GB gave 7.5. The read-ahead depth, the I/O threads and the eviction order each moved decode by less than 3%.
- The Q2_0 copy is not measured yet. The numbers in the table above are with the model file.
-- **Measured again on the release build, with less memory free:** the M4 had 13-17 GB available when the engine started (another user's programs held the rest), so the arena was 5.7 GB (Q2_0) and 9.3 GB (IQ3_XXS, with the experts' copy). One token per step, medians of 3: Q2_0 story 8.3 tok/s, 4K prompt 80; IQ3_XXS story 8.3, 4K prompt 89. That is the arena's size effect above, not a regression: with a 6.6 GB arena the engine used to stop at the first prompt ("no free slot"); a small arena now gives the prompt's 1.6 GB of buffers their own allocation, and a 26-minute server run with a 5.4 GB arena (29 requests, 6K and 31K needles found) was clean.
+- **Measured again on the release build, with less memory free:** the M4 had 13-17 GB available when the engine started (another user's programs held the rest), so the arena was 5.7 GB (Q2_0) and 9.3 GB (IQ3_XXS, with the experts' copy). One token per step, medians of 3: Q2_0 story 8.3 tok/s, 4K prompt 80; IQ3_XXS story 8.3, 4K prompt 89. That is the arena's size effect above, not a regression: with a 6.6 GB arena the engine used to stop at the first prompt ("no free slot"); a small arena now gives the prompt's 1.6 GB of buffers their own allocation, also when no chunk of the arena is big enough for them (on a 24 GB M2 whose arena shrank below the prompt's needs, every prompt stopped with "no arena region for the prompt buffers"), and a 26-minute server run with a 5.4 GB arena (29 requests, 6K and 31K needles found) was clean.
- With the default arena (12.1-12.2 GB here), the experts' copy gives 8.35-8.48 tok/s decode and 82.5-82.8 tok/s on the prompt. Nothing was swapped out. macOS's memory pressure stayed "normal", except for one 2-second "warn" sample while the arena fills; keeping 7 GB for macOS shows the same sample.
- Those runs and the server run below kept 5 GB for macOS. The default is now 7 GB with the stepped fill: after the 6K document, 7.3 and 6.7 tok/s decode (arenas 9.3 and 7.6 GB, as much as was free), the same tokens, the memory pressure "normal" throughout and nothing swapped out.
diff --git a/metal/src/engine/stream.cpp b/metal/src/engine/stream.cpp
index 9db74e251..b06aac61c 100644
--- a/metal/src/engine/stream.cpp
+++ b/metal/src/engine/stream.cpp
@@ -731,17 +731,7 @@ uint8_t* ExpertStream::region(uint64_t bytes) {
for (int s : region_slots_) ++stolen[(size_t) cls_of(s)];
bool tight = false;
for (size_t i = 0; i < cls_.size(); ++i) tight = tight || (cls_[i].n > 0 && cls_[i].n - stolen[i] < NE);
- if (tight) {
- const uint64_t sz = (bytes + PAGE - 1) / PAGE * PAGE;
- void* m = mmap(nullptr, (size_t) sz, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
- if (m == MAP_FAILED) return nullptr;
- region_slots_.clear();
- region_ = (uint8_t*) m;
- region_bytes_ = 0; // fresh zero pages; nothing in the arena to empty
- std::fprintf(stderr, "strata metal stream: the arena is small: the prompt's %.2f GB of buffers are their own "
- "allocation, not a region of its slots\n", bytes / 1e9);
- return region_;
- }
+ if (tight) return standalone_region(bytes);
}
region_ = lo;
region_bytes_ = (uint64_t) (end - lo);
@@ -749,7 +739,23 @@ uint8_t* ExpertStream::region(uint64_t bytes) {
bytes / 1e9, region_slots_.size());
return lo;
}
- return nullptr;
+ // no chunk is big enough for the prompt's buffers (an arena that shrank at the start, a 24 GB Mac): the fallback
+ // inside the loop above is only reached when a chunk fits, so the buffers get their own allocation here too
+ return standalone_region(bytes);
+}
+
+// The prompt's buffers as their own anonymous allocation (fresh zero pages), when no region of the arena's slots
+// takes them: no slots are held while a prompt runs, and the arena keeps decoding with all of its slots.
+uint8_t* ExpertStream::standalone_region(uint64_t bytes) {
+ const uint64_t sz = (bytes + PAGE - 1) / PAGE * PAGE;
+ void* m = mmap(nullptr, (size_t) sz, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
+ if (m == MAP_FAILED) return nullptr;
+ region_slots_.clear();
+ region_ = (uint8_t*) m;
+ region_bytes_ = 0; // fresh zero pages; nothing in the arena to empty
+ std::fprintf(stderr, "strata metal stream: the arena is small: the prompt's %.2f GB of buffers are their own "
+ "allocation, not a region of its slots\n", bytes / 1e9);
+ return region_;
}
void ExpertStream::hold_region(bool hold) {
diff --git a/metal/src/engine/stream.h b/metal/src/engine/stream.h
index 2806938a4..1a72af5f3 100644
--- a/metal/src/engine/stream.h
+++ b/metal/src/engine/stream.h
@@ -89,6 +89,7 @@ private:
void ahead(int ln, int T, const float* pred, int K);
void submit(int slot, int l, int e, bool urgent);
void wait_slot(int s);
+ uint8_t* standalone_region(uint64_t bytes); // the prompt's buffers as their own allocation, when no region of the arena's slots takes them
int fd_ = -1;
// <gguf>.experts (metal/tools/stream_experts.py): each expert's gate, up and down rows together, read with one