Skip to content

Studio: price the int8 ConvRot text encoder on Apple Silicon when the dense one is still cached - #13306

Merged
danielhanchen merged 2 commits into
mainfrom
studio-mps-int8-te-cached-dense
Oct 11, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-mps-int8-te-cached-dense

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

Fixes #11792

Follow-up to #13026. That PR loads the int8 ConvRot Qwen-Image-2.1 text encoder (9.35 GB) on Apple Silicon by default instead of the bf16 one (17.5 GB), but the memory check before the load still refused the reporter, because they had already downloaded the bf16 encoder.

Problem

On a 48 GB Mac with 27 GB free, unsloth/Qwen-Image-2.1-GGUF Q4_K_M with default settings is still refused on main with exactly the message from the issue:

'qwen-image-2.1' needs about 24 GB of memory for its weights, but only about 17 GB is usable on this device (of the 27 GB currently free, after reserving room for the operating system).

Two things in the planner kept the bf16 encoder's size in the budget:

  • _plan_memory floors the pre-cast encoder at the size of any cached dense text encoder shards ("a bad checkpoint falls back to opening them"). Everyone who ran Qwen-Image-2.1 on a Mac before Studio: int8 ConvRot Qwen-Image-2.1 text encoder on Apple Silicon, and release text encoders during denoise on unified memory #13026 has those 17.5 GB of shards cached, so the encoder was priced at 16,722 MiB while the load opens the 8,916 MiB int8 file.
  • The int8 file is downloaded inside the load, after this check, so on the first load after updating it is not cached yet. _precast_text_encoder_mib then prices it from the family table at the fp8 scale (0.65) instead of the int8 ConvRot scale (0.56) that te_prequant_budget_scale already uses on MPS.

Change

  • diffusion.py: on MPS, the dense-shard floor is skipped, and an uncached pre-cast encoder is priced at TE_INT8_CONVROT_BUDGET_SCALE. Every other device keeps the floor and the 0.65 scale (both branches are gated on int8_convrot_te_runs_on(target), i.e. device == "mps").
  • diffusion_te_prequant.py: since the MPS plan no longer budgets the dense encoder, a pre-cast encoder that fails to load on MPS now raises a clear error instead of silently loading the dense 17.5 GB encoder in its place (which is what swapped and froze the machine in the issue). The message points at Text encoder precision > Dense (bf16), which goes through the normal memory check sized for the dense encoder. CUDA, ROCm and CPU keep the existing fallback.

Results

The real DiffusionBackend._plan_memory and unified_memory_shortfall_message for a GGUF Q4_K_M load, on CPU with an MPS target and spoofed unified memory (48 GiB total). The HF cache is a sparse fake sized from the real Hub files:

Cache state free GiB main this PR
bf16 encoder cached, int8 not yet (first load after updating) 27 refused, needs ~24 GB admitted (14,839 MiB)
bf16 encoder cached, int8 not yet 30 / 33 refused admitted
bf16 encoder cached, int8 cached 27 / 30 / 33 refused, needs ~24 GB admitted (14,409 MiB)
fresh cache, int8 cached 27 / 30 / 33 admitted admitted, unchanged
any 20 / 24 refused refused (needs ~16 GB, the int8 load does not fit there)

Testing

  • New tests, all of which fail on main and pass here:
    • test_mps_prices_the_int8_encoder_over_cached_dense_shards[True/False]: the report's numbers with the dense shards cached, the int8 file cached or not.
    • test_precast_text_encoder_mib_prices_an_uncached_int8_encoder_on_mps.
    • test_a_failed_int8_encoder_on_mps_never_falls_back_to_dense[mps], with [cuda] as the control that keeps the fallback.
  • The existing CUDA floor tests (test_plan_memory_prices_cached_dense_shards_over_a_cached_precast, test_cached_dense_shards_keep_the_24g_denoiser_resident) are unchanged and pass.
  • I ran 21 related backend suites (diffusion backend, memory, unified memory, precision, te prequant, te release, prompt cache, small host, fast load, video H3 TE) on main and on this branch. Main: 1372 passed, 6 failed. This branch: 1377 passed, 6 failed. The 6 failures are the same tests on both sides (test_health_reports_unified_memory.py x5, test_mirrored_base_still_trips_the_flux2_shape_guard).
  • test_diffusion_te_release.py from Studio: int8 ConvRot Qwen-Image-2.1 text encoder on Apple Silicon, and release text encoders during denoise on unified memory #13026 passes on the macos-15 and macos-26 runners (20 passed) with torch 2.14.1 and diffusers 0.41.0.
  • Staging CI ran this PR's two test files on macos-15 and ubuntu-latest: 678 and 679 passed, including the new tests.
    • 5 failures there also fail on main under the same conditions, reproduced locally.
    • 4 of them (test_download_plan_* / nvfp4) fail when torchao is missing, and the staging legs do not install it.
    • test_gguf_offload_swap_on_mps_keeps_the_gguf reads the host's real free memory, which was 3 GB on the macOS runner.
  • Studio regression switchboard, comparing the merge base and this branch:
    • Functional verdict: NO_REGRESSION (63 SAME, 2 DOM-only diffs at 0 px).
    • The image and MiniMax-H3 planner matrices are identical (720 cells, 0 moved). They use a CUDA target, which confirms the non-MPS path is unchanged.
    • Tiny diffusion_bench suites on this branch's head (46c08f9), HTTP and in-process: 31 PASS, 0 FAIL, 1 SKIP each.

Not covered

  • I have not run this on a real 48 GB Mac. The admission numbers come from the real planner with spoofed memory.
  • The unified-memory check still reserves 20% of total RAM on top of the free memory that macOS reports. At 20 to 24 GB free, this model is still refused.

@danielhanchen danielhanchen self-assigned this Oct 11, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 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-11T10:34:14.743167Z 46c08f9 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.

@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. Already looking forward to the next diff.

Reviewed commit: 46c08f9b51

ℹ️ 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".

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.

[Bug]? Unable to run Qwen Image 2.1 Q4_K_M on an M5 Max with 48 gb?

1 participant