Skip to content

Studio: stop LTX-2 loads crashing Windows with an access violation on 32 GB machines - #13432

Merged
danielhanchen merged 6 commits into
mainfrom
studio-windows-fp32-shard-pread
Oct 13, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
studio-windows-fp32-shard-pread

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

Fixes #9977

Problem

On Windows, loading unsloth/LTX-2-GGUF (any quant) on a machine with around 32 GB of RAM kills the Studio backend with Windows fatal exception: access violation / exit code 0xC0000005. The crash happens in transformers/core_model_loading.py::_materialize_copy while the Gemma3 text encoder from Lightricks/LTX-2 is loaded.

Why:

  • That text encoder is stored as fp32 (11 shards, 48.7 GB) and is loaded at bf16 (24.4 GB).
  • safetensors' default backend maps each shard through torch.UntypedStorage.from_file(shared=False). On Windows, torch does this with a copy-on-write view (PAGE_WRITECOPY + FILE_MAP_COPY), and Windows charges the whole view to the commit limit.
  • transformers 5.5 opens every shard of the model before converting any tensor, so the load needs about 48.7 GB of mapped views plus 24.4 GB of bf16 output at the same time.
  • When the commit limit runs out, MapViewOfFile returns NULL. torch does not check that return value on this path, so the first read of a tensor (the .to(dtype) in _materialize_copy) is an access violation instead of a Python error.
  • Machines with a lot of RAM have enough commit, which is why a 256 GB machine could not reproduce it.

Fix

During a video load on Windows, transformers.modeling_utils.safe_open opens shards stored mostly as fp32 with safetensors' pread backend. pread reads each tensor into its own buffer instead of mapping the file.

  • The weights are the same bytes: the loaded text encoder is bit-identical.
  • bf16 / fp16 shards keep the default mmap path, because their mapping is the loaded weight itself and costs no extra commit.
  • Linux and macOS are unchanged.
  • The patch is scoped to VideoBackend._run_load and nests safely.
  • transformers 5.13 to 5.16 pass backend="mmap" themselves, so that value is replaced for fp32 shards too. 5.17 already picks pread on Windows upstream (Fix safe_open mmap memory exhaustion on Windows by using pread backend huggingface/transformers#48341); Studio pins 5.5.0.
  • pread needs safetensors 0.8.0. studio.txt and the pyproject studio extra now require safetensors>=0.8.0 on Windows only, so unsloth studio update / the Desktop updater replaces an older install. Fresh installs already resolve 0.8.0, and other platforms see no resolver change. With an older safetensors the scope does nothing.
  • UNSLOTH_SAFETENSORS_PREAD=0 turns it off and =1 forces it on any OS.

Evidence

All Windows results are from windows-latest runners (16 GB RAM, 18.9 GB commit limit, system-managed pagefile) with torch 2.10.0, transformers 5.5.0 and safetensors 0.8.0, the reporter's torch with Studio's pins. Each arm ran in its own fresh VM.

Load at bf16 Before (mmap) After (this PR)
Real Lightricks/LTX-2 Gemma3 text encoder, 48.7 GB fp32 exit 0xC0000005 access violation (twice) loads, 12.19B params, peak commit 29.2 GB
Synthetic Llama, 24 GiB fp32 exit 0xC0000005 (twice) loads, peak commit 20.3 GB
Synthetic Llama, 6 GiB fp32 (fits either way), 3 interleaved repeats 2.0 s (1.9 to 2.1), peak commit 13.6 GB 3.8 s (3.8 to 4.1), peak commit 7.6 GB

Cost: about 0.3 s per GiB of fp32 shards on a warm cache. This only applies to fp32-stored checkpoints on Windows (the LTX-2 family's text encoder in the video picker). In exchange, those loads no longer need commit for the whole mapped file.

Full Studio VideoBackend load of unsloth/LTX-2-GGUF UD-Q2_K_XL on Linux, with the fix forced on via UNSLOTH_SAFETENSORS_PREAD=1. This only shows the Studio path is wired; Linux itself is unaffected.

Before After
Bytes mapped through torch.UntypedStorage.from_file 54.3 GB (15 files) 5.5 GB (4 bf16 diffusers files)
Peak process RSS 81.9 GB 38.6 GB
Text encoder weights (sha256 fingerprint) 86c4bcbb... 86c4bcbb... (identical)

Tests:

  • tests/test_safetensors_pread.py covers:
    • the platform gate;
    • the old-safetensors no-op;
    • a Windows fp32 checkpoint loading through pread with weights equal to the mmap load;
    • bf16 shards staying on mmap;
    • the backend chosen for fp32 vs bf16 shards with no, mmap or pread passed in;
    • nested and concurrent scopes restoring once;
    • the video load running inside the scope (this one fails with the wiring reverted).
  • The new tests pass on transformers 5.5.0 and 5.17.0. A load check (fp32 shards switch to pread, bf16 shards keep their backend, weights equal, patch restored) also passes on 4.57.6, 5.5.0, 5.10.0 and 5.17.0.
  • test_video_backend.py, test_video_prequant.py, test_video_offline_load.py and test_diffusion_small_host.py pass: 583 passed.
  • tests/studio/install/ fails the same tests with and without the floor (installer step counts and a managed Node path, untouched here).

Other video / image families

Header scan of every supported video base repo, showing which transformers-loaded parts are fp32 and so switch to pread on Windows:

  • LTX-2 / LTX-2.3: Gemma3 text encoder.
  • Wan2.1 1.3B / 14B: UMT5 text encoder, 22.7 GB fp32.
  • HunyuanVideo-1.5: the 0.9 GB T5 text_encoder_2.

Nothing switches for Wan2.2 or MiniMax-H3 (bf16 encoders), and denoisers / VAEs never switch: diffusers loads them through its own code. Image loads never enter the scope.

Forced on, through Studio's real begin_load path:

  • Tiny Wan pipes as Wan2.2-TI2V and Wan2.1: 18 loads, every component bit-identical with and without pread.
  • Five tiny image pipelines (FLUX.1, FLUX Kontext, Lumina2, Qwen-Image, SDXL): identical weights and default backend in all three arms.

Not covered

  • A full Desktop load on a Windows NVIDIA machine (no such runner here). The repro above runs the exact transformers call that crashes, on real Windows, with the real checkpoint.
  • A host whose commit cannot hold even the bf16 weights still fails. On the 16 GB runner a 48 GiB fp32 synthetic model (24 GiB at bf16) aborted at 93% with memory allocation of 180355072 bytes failed (exit 0xC0000409) instead of an access violation. That is a real out-of-memory, not mapping overhead.

torch maps a safetensors shard copy-on-write, and Windows charges the whole
view against the commit limit while transformers holds every shard open.
LTX-2's 48.7 GB fp32 Gemma3 text encoder loaded at bf16 then needs ~73 GB
of commit, and on a 32 GB host torch's unchecked MapViewOfFile returns NULL
and the first read is an access violation (0xC0000005).

During a video load on Windows, transformers now opens fp32-stored shards
with safetensors' pread backend (same bytes, no mapping). bf16 / fp16
shards and every other OS keep the default mmap path.
@danielhanchen danielhanchen self-assigned this Oct 12, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T06:54:36.649654Z 4b86d68 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 12, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: bc9ed55257

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…ust tests

transformers 5.6 to 5.16 pass backend="mmap" to safe_open themselves, so the
Windows fp32 shard would have kept the commit-charging mapping on those
releases. 5.17 already picks pread on Windows upstream.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 402e9d035f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

current = getattr(modeling_utils, "safe_open", None)
if _depth:
installed = True
elif current is not None and _supports_backend(current):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Require a safetensors release that provides pread

On upgraded Studio environments that still have any valid pre-0.8 safetensors release, this condition leaves safe_open untouched, so the affected Windows LTX-2 load continues down the mmap path and can still terminate with the access violation this change is intended to prevent. Neither this commit nor the Studio requirements raises the existing safetensors>=0.4.3 floor (Transformers 5.5 also accepts that floor), so the updater has no reason to replace an already-satisfied 0.7 installation; add an installation migration/platform-compatible 0.8 floor or a fallback for older versions rather than silently disabling the fix.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 156f7c7: studio.txt and the pyproject studio extra now require safetensors>=0.8.0 on Windows, so an update replaces an older install instead of leaving the fix inert.

…ad backend

An install that predates safetensors 0.8 keeps it on update (>=0.4.3 is
already satisfied), and the Windows fix silently does nothing there. The
floor is Windows-only; fresh installs already resolve 0.8.0.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 156f7c7d43

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread pyproject.toml Outdated
"fastmcp>=3.0.2",
"gguf==0.19.0 ; python_version >= '3.10'",
"gguf==0.18.0 ; python_version < '3.10'",
"safetensors>=0.8.0 ; sys_platform == 'win32'",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve the supported Windows Python 3.9 install

On Windows with Python 3.9, this requirement makes pip install "unsloth[studio]" unresolvable: the project declares support for Python 3.9, while safetensors 0.8.0 declares Requires-Python >=3.10, so pip currently has no candidate satisfying >=0.8.0. The mirrored requirement in studio/backend/requirements/studio.txt breaks that installation path as well, despite that file explicitly retaining Python 3.9-specific pins; gate this floor on python_version >= '3.10' with a compatible fallback, or formally raise Studio's Python floor.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c640e50: both lines now also carry python_version >= '3.10'; uv resolves 0.7.0 on Windows 3.9 (previously no solution) and 0.8.0 on 3.10+.

safetensors 0.8.0 requires Python >= 3.10, so the unmarked floor made the
studio extra unresolvable on Windows Python 3.9.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: c640e5027c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

pre-commit.ci run

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 4b86d68fd8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit to LeoBorcherding/unsloth that referenced this pull request Oct 13, 2026
@danielhanchen
danielhanchen merged commit 65e03e7 into main Oct 13, 2026
@danielhanchen
danielhanchen deleted the studio-windows-fp32-shard-pread branch October 13, 2026 09:41
danielhanchen added a commit that referenced this pull request Oct 23, 2026
…ds (#14514)

## Summary

GGUF image loads can exhaust Windows system commit while the small-host
guard considers dense loading safe. The guard now also checks the
Windows commit headroom, so affected loads take the existing low-memory
route; the existing RAM refusal is unchanged.

Fixes #14513

## Changes

- On Windows, read the commit headroom (`CommitLimit - CommitTotal`)
with `K32GetPerformanceInfo`. A converting load keeps each copy-on-write
safetensors view charged to commit next to the converted copy, so its
commit need is the stored bytes plus the dense estimate (2x dense for an
fp16 load; measured 2.06x on the Qwen-Image encoder). If that plus the
reserve does not fit the headroom, the low-memory route engages even
when physical RAM says the dense load fits.
- The physical check is the same as before (dense estimate vs available
RAM, no extra margin). Linux and macOS have no commit reading, so their
decisions do not change.
- On Windows, image pipeline loads now read every bf16 / fp16
safetensors shard that converts to another dtype with safetensors'
`pread` backend (the scope #13432 added for fp32 video encoders,
generalized). `pread` reads each tensor into its own buffer, so the
stored bytes are never charged to commit; this is what transformers 5.17
does on Windows (huggingface/transformers#48341), and what ComfyUI and
llama.cpp get by mapping read-only. Shards loaded at their stored dtype
keep mmap, since that mapping is the weight and the low-memory route
relies on it. The commit check then counts one copy for components read
this way, so the reporter's load can stay dense instead of rerouting.
- If unsloth's own Windows pread patch (#14856) is loaded in the same
process, this scope still decides each shard inside a media load and
tells it which shards to keep on mmap.
- The pre-built GGUF transformer is not added to the estimate: diffusers
copies every GGUF tensor into private memory when it builds the
transformer, so both readings already exclude it.
- The commit check only routes. The refusal still uses physical RAM,
since a system-managed pagefile can grow past the current commit limit.
- Log measured dense decisions as well, with the commit headroom when it
is read, so a crash after a dense decision shows what the guard saw.
- Add `UNSLOTH_DIFFUSION_HOST_RAM_LIMIT_MIB` to cap the total, available
and commit values the guard sees, for testing. It does not enforce a
process memory limit.

## Known limitations

- A system-managed pagefile can grow, so the current commit limit can
understate what Windows would eventually allow. Those hosts may take the
low-memory route when a dense load would have just fit.
- The commit estimate assumes Studio's pinned transformers 5.5, which
reads shards through copy-on-write mmap on Windows. transformers 5.17
and later read them with `pread` on Windows
(huggingface/transformers#48341), so there the source views cost no
commit and the estimate is conservative: such installs may take the
low-memory route sooner than needed.
- The replacement/reload crash described in the issue is outside this
change. Loading peaks and the timing of host-memory release need
separate investigation.
- The video loading path is unchanged.

## Testing

`studio/backend/tests/test_diffusion_small_host.py` covers the #14513
case (a 16.7 GB bf16 encoder with the GGUF DiT already built: dense on
physical RAM alone, routed with 30 GB of commit headroom, dense again
with 50 GB), commit never refusing, LoRA keeping the dense denoiser,
unreadable commit giving the same decisions as the physical check, the
env cap, the Windows API reader (runs on Windows), and the pipeline call
site passing the commit reading and logging dense decisions.

Decision grid against `main` (3 families x fp16/fp32/bf16 x 5 host sizes
x 22 free-RAM levels x LoRA on/off): with no commit reading, 864 of 864
cells give the same decision. With commit headroom from 0 to 192 GB,
every cell that moves goes from dense to the low-memory route because
the commit need does not fit; no new refusals and no LoRA cell moves.

Windows commit, measured on `windows-latest` (transformers 5.5.0,
safetensors 0.8.0) through this scope: a 1426 MiB bf16 checkpoint loaded
at fp16 peaks at 3478 MiB of process commit with mmap and 2085 MiB with
`pread`; the 1.4 GB difference is the copy-on-write charge for the
stored bytes. Staging: 51 passed on Windows, 50 passed + 1 Windows-only
skip on Ubuntu and macOS.

GPU check on a Colab Tesla T4 (fp16 compute, like the reporter's V100),
through Studio's real load and generate, with a Qwen-Image 2.1 pipe
whose text encoder is 667 MiB bf16. With the commit reading faked to 1
GB, the guard logs `engaged=True (... 1024 MiB commit headroom (dense
needs ~1536 MiB))`, the encoder loads memory-mapped with layerwise
casting, and peak RSS drops from 2.92 to 2.31 GiB. With no commit
reading (Linux) or a large one, the load stays dense. The image is
bit-identical to `main` in all four arms.

The same check with `UNSLOTH_SAFETENSORS_PREAD=1` forced on: the dense
load reads the converted encoder through `pread`, and with the commit
reading faked tight the route engages and keeps the encoder
memory-mapped. Every arm's image is bit-identical to `main`.

On the original 48 GB Windows host, the earlier version of this PR sent
the previously failing Turbo load through the low-memory route, and it
completed and generated images. With this version the same load needs
about 43.4 GB of commit headroom to stay dense (38.4 GB for the source
views and the fp16 copy, plus the 4.9 GB reserve). The failing run
committed 44.7 GB before it ran out, so its headroom was below that and
it routes. This version has not been rerun on that host yet.

---------

Co-authored-by: Daniel Han <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
danielhanchen added a commit to Etherll/unsloth that referenced this pull request Nov 10, 2026
…unslothai#17378)

Fixes unslothai#17137

## Problem

On Windows, loading Qwen-Image-2.1-Turbo or FLUX.3 Klein (GGUF denoiser + base repo) onto an Intel Arc 140V (Lunar Lake iGPU, 32 GB shared RAM) fails with

```
OSError: The paging file is too small for this operation to complete. (os error 1455)
  transformers/modeling_utils.py, in _load_pretrained_model: safe_open(file, framework="pt", device="cpu")
```

and the retry kills the backend with `0xc0000005`, the native form of the same commit exhaustion (unslothai#13432, unslothai#14513).

Why:
- On Windows, safetensors' default mmap backend maps a shard copy-on-write, and Windows charges the whole view to the commit limit while any tensor from that shard is alive.
- Studio's media-load scope (unslothai#14514) moves only *converting* loads to `pread`; a same-dtype bf16 load keeps the mapping, which is fine on a discrete card.
- On an iGPU the device copy is host commit too. Qwen-Image-2.1's text encoder is one 16.3 GB bf16 file: its mapped view stays charged until the last tensor has moved to the XPU, so the load needs about twice the encoder in commit.

## Change

- `pread_safetensors(..., every_shard=True)` reads same-dtype shards through `pread` as well (component dirs the small-host route keeps mapped still stay mapped).
- The image-load scope sets it only on Windows when the current XPU is a Core Ultra Arc iGPU (`xpu_is_integrated()`, by PCI id: the iGPU subset of the existing XPU allowlist, split into `_INTEL_XPU_IGPU_PCI_IDS` with no change to `_INTEL_XPU_PCI_IDS`).

Every other platform and device keeps the byte-identical path: off Windows the scope installs nothing, and on Windows a CUDA, ROCm, discrete Arc or unclassifiable XPU load passes `every_shard=False`.

## Tests

`studio/backend/tests/test_safetensors_pread_igpu.py`: the iGPU classifier over Lunar Lake / Meteor Lake / Panther Lake vs discrete Arc / Data Center ids, the backend choice with and without `every_shard`, the image-load scope engaging only on Windows + iGPU, and a Windows-only commit measurement (bf16 load at bf16, then each weight copied the way `.to("xpu")` does on shared memory, mmap vs pread arms).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Unsloth Desktop crashes when I try to load the LTX-2-GGUF video model on Windows. exit code: 0xc0000005

1 participant