Skip to content

core: release each tensor's staged mmap pages after the backend copy - #113

Open
erichchampion wants to merge 1 commit into
ServeurpersoCom:masterfrom
erichchampion:offer/patch-5-release-mmap-pages
Open

core: release each tensor's staged mmap pages after the backend copy#113
erichchampion wants to merge 1 commit into
ServeurpersoCom:masterfrom
erichchampion:offer/patch-5-release-mmap-pages

Conversation

@erichchampion

Copy link
Copy Markdown

A fifth patch from Cadenza, offered separately from the add_subdirectory / ace_fatal / AceProgress / backend-config series: this one is an embedder-concern with narrower appeal, so we would entirely understand if it is not wanted -- it is the one we expect to keep carrying if so.

The problem

An embedder loading several GGUFs (or reloading one) pays for every staging mmap twice: the pages stay resident for the life of the process even after the backend copy has made them redundant. On iOS the kill is on footprint long before malloc fails, so the redundant residency is the difference between a model set fitting and not.

What it does

  • gguf/safetensors staging records each tensor's file range, and once the backend copy of that tensor is done, the pages are returned: copy-before-unmap, with a Debug-mode overlap sweep asserting no tensor's range is read after release, and silent release failures surfaced rather than swallowed.
  • Loads that fail mid-way release what they staged instead of relying on process teardown.
  • The GGUF/safetensors close paths and the weight-ctx copy factory share one release body; repeated warnings key on the format, so a multi-hundred-tensor model prints one report line, not hundreds.
  • Non-Apple builds keep compiling: the release call degrades to a no-op where the platform has no usable madvise.

Numerically a no-op: nothing reads a released range -- that is what the overlap sweep verifies under Debug on every loader. The memory behaviour is the point of the change.

Testing

Full engine build with the overlap sweep on, all loader paths exercised against real GGUFs on macOS/arm64 (Metal); philox/LM-prompt test binaries green.

An embedder loading several GGUFs (or reloading one) pays for every staging
mmap twice: the pages stay resident for the life of the process even after
the backend copy has made them redundant, and on macOS the address space is
the scarce resource -- iOS kills on footprint long before malloc fails.

gguf/safetensors staging now records each tensor's file range and, once the
backend copy of that tensor is done, returns the pages: the mapping is
dropped copy-before-unmap, with a debug-mode overlap sweep asserting no
tensor's range is read after release, and silent release failures surfaced
rather than swallowed. Loads that fail mid-way release what they staged
instead of relying on process teardown; the GGUF/safetensors close paths and
the weight-ctx copy factory share one release body, and repeated warnings
key on the format so a multi-hundred-tensor model prints one report line,
not hundreds.

Behaviour-preserving numerically: nothing reads a released range, verified
by the overlap sweep under Debug on every loader. Memory behaviour is the
point of the change. Non-Apple builds keep compiling (the release call
degrades to a no-op where the platform has no usable madvise).

This is an embedder-concern patch from [Cadenza](https://github.com/erichchampion/cadenza-audio), offered
separately from the add_subdirectory / ace_fatal / AceProgress / backend-config
series because it is the one we would keep carrying if upstream is not
interested.
@coderabbitai

coderabbitai Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 51 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: 3b51b22d-eb1e-470e-9b5c-3fe4f580bb8d

📥 Commits

Reviewing files that changed from the base of the PR and between c9045e2 and 1601a3e.

📒 Files selected for processing (6)
  • src/adapter-merge.h
  • src/dit.h
  • src/gguf-weights.h
  • src/qwen3-lm.h
  • src/safetensors.h
  • src/weight-ctx.h

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant