Skip to content

fix: honor Flash Next host offload and explicit MLX memory budgets - #49

Closed
Chedrian07 wants to merge 4 commits into
ashhart:mainfrom
Chedrian07:fix/flash-next-host-memory
Closed

Chedrian07 wants to merge 4 commits into
ashhart:mainfrom
Chedrian07:fix/flash-next-host-memory

Conversation

@Chedrian07

@Chedrian07 Chedrian07 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

On a 128 GiB M4 Max, tensorfold serve Vontra/Qwen3.8-Flash-Next-MLX-4bit-MTP refused the 105.4 GiB checkpoint before the loader could map its 29.8 GiB of n-gram tables on the host. Count those tables separately so the remaining 75.6 GiB of weights can enter memory admission, and allow an explicit memory budget to raise or lower the default for larger contexts.

The default parallel startup also retained Flash Next's shared-forward probe buffers. After correcting the weight check, those unused rollback states made prompt admission report 88.0 GiB needed against the 86.6 GiB MLX limit. Implement Flash Next's existing release_rounds() family hook so startup releases the backbone and MTP probe states before profiling prompt memory.

  • Add an optional weight_bytes(model_dir) family hook, retaining the file-size estimate for other families. Flash Next reads only safetensors headers and subtracts the mapped n-gram weight, scale and bias ranges; unrelated tensors and files stay counted.
  • Share the loader's automatic host-selection rule with preflight. Honor both TF_NGRAM_HOST=1 and TF_NGRAM_HOST=0, and report resident/file-backed sizes at startup.
  • Let TENSORFOLD_MEMORY_LIMIT_GB replace the default 70% RAM budget, within physical RAM and the GPU's recommended working set. On this Mac, 110 now means a 110 GiB process budget and 107 GiB for MLX. Concurrent admission honors the larger allowance while retaining the checks for memory held elsewhere and the existing behavior for lower budgets.
  • Keep the 70% default when the variable is absent, the 3 GiB reserve, prefill schedule, context fitting and oversized-weight refusals. Share the default fraction between the CLI and allocator, and update help and memory guidance.

Validation on Apple M4 Max, 128 GiB, macOS 26.6.2, Python 3.14.0, MLX 0.32.2 and mlx-lm 0.31.3:

  • Reproduced the original CLI refusal before the patch. The new automatic/forced-host admission cases fail on the original implementation and pass with the fix.
  • Affected memory, admission and CLI suites: 124 passed. Coverage includes exact header accounting, forced GPU mode, raised/lowered limits, physical/GPU caps, both device-info APIs, large finite values, concurrent admission under memory pressure and probe-state reclamation. The GPU regression also checks a subsequent shared forward with identical output bits.
  • Full suite (python -m pytest -q --tb=no): 1,251 passed, 285 skipped, 4 failed. The same four test_bounded_prompt_matches_stock_causal_bits cases (257/273 rows, bf16/fp16) fail on a separate unmodified checkout of base a3274f1 with this runtime. CUDA GPU tests are not collected on this Mac.
  • Real checkpoint revision dadefa8066e3be900a0d148d0f5a2f4eb1cf6534: the default-budget check at e7547c1 reached serving with parallelism 8, MTP enabled and context automatically fitted to 63,488 tokens.
  • At 3d9530b, TENSORFOLD_MEMORY_LIMIT_GB=110 tensorfold serve Vontra/Qwen3.8-Flash-Next-MLX-4bit-MTP --context 262144 --max-tokens 131072 --host 127.0.0.1 --port 1313 --no-update-check reached serving with context 262,144, default parallelism 8 and MTP enabled. The allocator and concurrent admission both reported 107 GiB for MLX; /health reported a 110 GiB process budget. Startup took 539.3 seconds on this host with paging.
  • /health, /v1/models and short /v1/chat/completions requests returned HTTP 200. With thinking enabled by default, the greedy arithmetic smoke emitted 26 completion tokens and final answer 4 with drafting enabled and disabled; both token hashes were 894ffec4f91c. The requests used max_tokens: 512 to bound the smoke, so they do not validate a 131,072-token completion.

Starting with a 262,144-token context is not a full-window inference qualification; only short requests were run. Host mappings still use reclaimable RAM pages, and a raised budget can increase memory pressure. This change does not promise swap-free execution or qualify SSD throughput. The test host already had swap in use and paging increased during startup.

The MLX preflight charged the entire checkpoint before the loader could memory-map its n-gram tables, refusing Flash Next on a 128 GiB Mac even in host mode.

Let families estimate startup weight bytes. Flash Next uses the loader's shared host-selection rule and subtracts only mapped weight, scale and bias ranges from safetensors headers. Keep the 70% budget and reject oversized resident weights or forced GPU tables.

Tests cover automatic and explicit host mode, forced GPU mode, reduced budgets, mixed checkpoint files and both MLX device-info APIs.
Document automatic n-gram placement, the family weight estimate hook and the distinction between resident buffers and reclaimable file pages. Keep context fitting and the existing memory ceiling explicit.
Default parallel startup retains the shared-forward probe's rollback buffers in Flash Next. The following prompt-memory probe then charges those unused rows as resident memory, leaving no prompt room on a 128 GiB Mac even after host n-grams are excluded.

Implement the existing release_rounds family hook for backbone and MTP states, cache references and hidden rows. Preserve the prefill schedule and memory limits. Test prompt admission after release and real GPU buffer reclamation with bit-identical subsequent shared forwards.
TENSORFOLD_MEMORY_LIMIT_GB now replaces the 70% default, capped by physical RAM and the GPU recommended working set. Keep the 3 GiB process reserve and honor the resolved budget in concurrent admission without changing lower-budget behavior.

Cover raised and lowered limits, hardware caps, both device-info APIs, large finite values, concurrent admission under memory pressure and the CLI's allocator/server handoff. Update the memory guidance and startup help.
@ashhart

ashhart commented Sep 28, 2026

Copy link
Copy Markdown
Owner

Thanks @Chedrian07. This lands in 0.3.6.2 with your four commits under your name: 222fe5fd, 923c1f0a, 70a61130, 153f488b.

Our 0.3.6 line had moved on since 0.3.5 (--ple-on-ssd, a narrower release_rounds, per-model memory shares,
--ssd-experts), so the commits were rebased onto it. ngrams_on_host moved to host_table.py as you had it,
keeping the SSD argument. Your release_rounds replaced ours because it frees more: the MTP head's rows, _specs and
the model's last streams. TENSORFOLD_MEMORY_LIMIT_GB also overrides a model's own share.

On top of yours: the concurrency admission moved to its own module (we keep server/app.py under 600 lines);
ple_bytes and your weight_bytes now count the n-gram bytes one way for both host modes; and the startup line and
refusals name the variable and the value that fits. Your change also answered #50, where a 32 GB MacBook Air needed
exactly this. I hope you'll send the next one too.

@ashhart ashhart added the landed Landed on main by the maintainers; the commits keep the contributor's authorship label Oct 28, 2026
plotarmordev referenced this pull request in plotarmordev/TensorFold Nov 12, 2026
plotarmordev referenced this pull request in plotarmordev/TensorFold Nov 12, 2026
…e more after the output; the loader sets give_way and tf-h3-dit gives way too (#49)
plotarmordev referenced this pull request in plotarmordev/TensorFold Nov 12, 2026
plotarmordev referenced this pull request in plotarmordev/TensorFold Nov 12, 2026
…ay to speech too (#49)

Card 50 moved binding into Folder.bind and h3.Model.bind; give_way now rides on the folder's io, so
tf-h3-image and tf-h3-image-server both yield between blocks and report timing_valid.
plotarmordev referenced this pull request in plotarmordev/TensorFold Nov 12, 2026
…before the free run (#49)

buildCase unmapped pic while case.noise still pointed into it; once rebinding remapped those pages,
fresh and resident runs read different garbage noise.
plotarmordev referenced this pull request in plotarmordev/TensorFold Nov 12, 2026
plotarmordev referenced this pull request in plotarmordev/TensorFold Nov 12, 2026
plotarmordev referenced this pull request in plotarmordev/TensorFold Nov 12, 2026
plotarmordev referenced this pull request in plotarmordev/TensorFold Nov 12, 2026
plotarmordev referenced this pull request in plotarmordev/TensorFold Nov 12, 2026
…closing, so late wake-ups cannot outlast the grace (#49)
plotarmordev referenced this pull request in plotarmordev/TensorFold Nov 12, 2026
plotarmordev referenced this pull request in plotarmordev/TensorFold Nov 12, 2026
plotarmordev referenced this pull request in plotarmordev/TensorFold Nov 12, 2026
plotarmordev referenced this pull request in plotarmordev/TensorFold Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

landed Landed on main by the maintainers; the commits keep the contributor's authorship

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants