Repository navigation
fix(offload): stop tracking mmap file descriptors in the weight loader - #6659
Merged
Merged
Conversation
`file_back_module` keeps a zero-copy mapping of each offloaded module for the
rest of the run, and the checkpoint reader keeps one mapping per module group
for CPU-placed models. Python's `mmap` duplicates the mapped file descriptor
unless the caller passes `trackfd=False` (3.13+), and that duplicate stays open
until the mapping is closed, so a 64-expert MoE opens thousands of descriptors
and hits the default soft `RLIMIT_NOFILE` of 1024:
OSError: [Errno 24] Too many open files
.../weight_loader/reader.py:331
Bisect: the pre-#6473 parent (aff56aa) completes OLMoE GPTQ at 1024; #6473
(0043349) introduced the per-module file-backed offload.
Add `gptqmodel/utils/fd_limit.py`:
- `map_file_window(fd, length, offset=0, access=ACCESS_COPY)` asks `mmap` for
an untracked mapping when the interpreter supports it. The mapping still
keeps the file data alive after the caller closes the fd; only the duplicate
descriptor is gone.
- `ensure_nofile_limit()` is the Python 3.11/3.12 fallback where `trackfd`
does not exist: it raises the soft `RLIMIT_NOFILE` to the hard limit (only
ever raises, logs once at debug) and warns once, naming `ulimit -n`, when the
hard limit is still below 4096.
Use the helper at every `mmap.mmap` site in `weight_loader/reader.py` (3) and
`weight_loader/filebacked.py` (1), and call `ensure_nofile_limit()` once at
the start of `GPTQModel.load`, `GPTQModel.from_quantized` and
`BaseQModel.quantize` (a no-op on 3.13+).
The offload format, mapping lifetimes, read order and tensor values are
unchanged; `close()`, `flush()` and `madvise()` do not need the tracked fd and
there are no `size()`/`resize()` calls on these mappings.
This replaces the per-layer offload bundle from the first revision of the PR:
bundling forced a synchronous finalize drain, raised host RSS up to ~2x on
Qwen2.5-0.5B, and still failed for a layer with >~1000 modules.
Tests: `tests/test_offload_fd_bound.py` now exercises the offload write path
(GPTQ, RTN) and a quantized-checkpoint load at a deterministic soft limit of
256; all three fail with EMFILE on main and pass here. A fourth test disables
the trackfd capability and asserts the rlimit fallback raises only the soft
limit. GPU A/B on OLMoE-1B-7B (GPTQ, RTN) and Qwen2.5-0.5B (GPTQ, AWQ) is
bit-identical to main at 65536 with the branch at 1024, and host RSS peak is
unchanged.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Qubitium
force-pushed
the
fix/olmo-offload-fd-leak
branch
from
December 1, 2026 15:21
ffcc7fd to
98a6a3b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes
OSError: [Errno 24] Too many open fileswhen a large MoE is quantized with the defaultoffload_to_disk=Trueand the default softRLIMIT_NOFILEof 1024. Onallenai/OLMoE-1B-7B-0924GPTQ (64 experts, 64x512 calibration) current main opens one file descriptor per offloaded expert projection and dies after a few layers; the same model also cannot be loaded back on CPU at 1024.Root cause
file_back_modulewrites one safetensors file per offloaded module and keeps a zero-copy tensor view of it for the rest of the run (weight_loader/filebacked.py).read_tensorslikewise keeps one mapping per module group when the tensors stay on CPU (weight_loader/reader.py). CPython'smmap.mmapduplicates the mapped file descriptor (dup(2)) unless the caller passestrackfd=False(Python 3.13+), and the duplicate lives until the mapping is closed. A 64-expert MoE with 16 layers has 3072 such projections, so the process needs thousands of descriptors and hitsEMFILEat the default soft limit of 1024.Bisect: pre-#6473 parent
aff56aa1acompletes OLMoE GPTQ at 1024 (max fd 355, no file-backed fds); #6473 (0043349bf) introduced the per-module file-backed offload and current main fails.What Changed
gptqmodel/utils/fd_limit.py:map_file_window(fd, length, offset=0, access=ACCESS_COPY)asksmmap.mmapfor an untracked mapping (trackfd=False) when the interpreter supports it. The mapping still keeps the file data alive after the caller closes the fd; only the duplicate descriptor is gone.ensure_nofile_limit()is the Python 3.11/3.12 fallback wheretrackfddoes not exist: it raises the softRLIMIT_NOFILEto the hard limit (only ever raises, logs once at debug) and emits one warning namingulimit -nwhen the hard limit is still below 4096.mmap.mmapsite inweight_loader/reader.py(3) andweight_loader/filebacked.py(1) now goes throughmap_file_window.ensure_nofile_limit()is called once at the start ofGPTQModel.load,GPTQModel.from_quantizedandBaseQModel.quantize. On Python 3.13+ it is a no-op.close(),flush()andmadvise()do not need the tracked fd; there are nosize()/resize()calls on these mappings.Why not bundle finalized modules per layer
The first revision of this PR bundled a finalized decoder layer into one file-backed safetensors file. That changes the offload design and was rejected for concrete costs:
mmapbookkeeping, not by the offload granularity, so the correct fix is one call-site helper rather than a new offload unit.Tests
Red/green (
tests/test_offload_fd_bound.py, Python 3.14, CPU, deterministicRLIMIT_NOFILEsoft limit of 256 set inside the test):The two offload cases fail on main while writing the per-module file; the load case fails on main while mapping the quantized checkpoint back in (384 module groups). On the branch the same 64-expert model completes and the file-backed fd count stays at 0 after the run; the load case holds 0 checkpoint fds.
The real OLMoE GPTQ output from the A/B below (3.7 GB, 3,136 modules) also cannot be loaded back on CPU at
ulimit -n 1024on main:The fourth test (
test_nofile_limit_fallback_raises_soft_limit) monkeypatches thetrackfdcapability off and asserts the fallback raises only the soft limit to the hard limit. It cannot exist on main because the helper module is new.Other tests (CPU):
Measured Impact
GPU 4,
BACKEND.TORCH, seed 0. "main-high" is current main0127a0313withulimit -n 65536; "branch-low" is this branch atulimit -n 1024. OLMoE-1B-7B-0924 64x512 batch 8; Qwen2.5-0.5B-Instruct 128x512 batch 8.Host RSS peak is unchanged. Wall time stays inside the host's run-to-run variance: the same main revision measured 136 s and 296 s for OLMoE GPTQ in different runs, and the interleaved Qwen pair is slightly faster on the branch. Interleaved pairs on the same GPU:
The offload file set, tensor bytes and config jsons are identical, so there is no format or accuracy change.
Model Support / Module Plan
Not applicable: no model, alias, definition, or module tree changes.
AWQ onboarding safety
Not applicable: no AWQ grouping, search, replay or fold change. AWQ shares the checkpoint reader and offload path; the Qwen AWQ control above is bit-identical (626 tensors).
Logic Flow Documentation
file:lineevidence.flowchart TD A["GPTQModel.load / from_quantized / quantize"] --> B{"ensure_nofile_limit<br/>trackfd supported?"} B -- "yes (3.13+)" --> C["no-op"] B -- "no (3.11/3.12)" --> D["raise soft RLIMIT_NOFILE to hard<br/>warn once if hard < 4096"] C --> E["file_back_module / read_tensors"] D --> E E --> F["map_file_window(fd, length, offset, access)"] F --> G{"TRACKFD_SUPPORTED?"} G -- "yes" --> H["mmap.mmap(..., trackfd=False)<br/>no descriptor retained"] G -- "no" --> I["mmap.mmap(...)<br/>one descriptor per mapping"] H --> J["zero-copy tensor view<br/>mapping lives with the view"] I --> J J --> K{"more modules?"} K -- "yes" --> E K -- "no" --> L["done"] I -. "fd limit reached before the fallback ran" .-> X["OSError EMFILE"] style X fill:#ffe8e8,stroke:#cc3333mmap.mmapatreader.py:226,308,331andfilebacked.py:167map_file_window(utils/fd_limit.py)dup)RLIMIT_NOFILEraised to hard at load/quantizeReview Requirements
AI-assisted code is welcome. Open the PR as a draft. Mark it ready only after
review and after GitHub confirms the pushed head has no conflicts with the
current remote base.
I or an AI agent (such as Codex or Claude) have:
Kernel Accuracy Requirements
Not applicable: no kernel, packer or dispatch change.
Notes
trackfdis a Python 3.13+ keyword; the helper probes it once at import and falls back to the plain call on 3.11/3.12.resourceis optional in the helper and the test module; the mapping helper itself is portable. Not run on Windows./procpaths are untested.file_back_module; it is a few dozen files per model, not per expert, and is covered by the same helper.🤖 Generated with Claude Code