Skip to content

Studio diffusion: download a partially cached snapshot before sizing it for small hosts - #16080

Merged
danielhanchen merged 8 commits into
mainfrom
dh/diffusion-partial-snapshot-download
Oct 30, 2026
Merged

danielhanchen merged 8 commits into
mainfrom
dh/diffusion-partial-snapshot-download

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Problem

On a small host, Studio's diffusion loader downloads the full snapshot before deciding whether to spill components to disk, so the decision sizes the real weights. That download-first step only runs when the snapshot is missing or holds no stored components. A partially cached snapshot (an interrupted download) skips it: the decision sizes only the files present, spilling stays off, and the load then pulls the missing components into a dense load that can exceed host RAM.

Reproduced with header-only safetensors at Klein-9B sizes on a 12977 / 11161 MiB host: with one DiT shard missing, main makes no spill decision (assert [] == [1]).

Change

  • diffusion_small_host.snapshot_weights_complete(snapshot, skip): False when a model_index.json component folder is missing, a model folder (one with config.json) has no weights, or a shard listed in a *.index.json is absent. Components the caller supplies are skipped; a snapshot without model_index.json makes no claim.
  • _small_host_decision also downloads first when the cached snapshot is incomplete, so the sizing sees every component.
  • The check and the early fetch follow the layout the load will read: with fp16 twins selected by local_fp16_variant(), only the .fp16. files and indexes are checked and the fetch passes variant="fp16", so an orphan default index never triggers a download of unused default weights. The variant is picked from the final load arguments (after the hosted VAE, extra text encoders and native-quant transformer are added), the same way the load picks it.

Tests

studio/backend/tests/test_diffusion_small_host.py: 4 new cases (missing DiT shard, weightless model folder, missing component folder, complete fp16 twins with an orphan default index). main: 3 failed, 31 passed on the first three. This branch: 36 passed; with tests/test_diffusion_fp16_variant.py, 67 passed.

Not covered

  • No real full-model load on a small host; the decision path was tested with header-only weight files.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 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-30T09:48:57.852366Z 75cebaf 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

@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: 7a79737002

ℹ️ 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 on lines +294 to +295
if any(not (sub / shard).is_file() for shard in shards):
return False

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Check only the weight layout selected for loading

When a cached snapshot contains complete .fp16.safetensors twins but also a default shard index whose shards are absent, this loop marks the snapshot incomplete even though local_fp16_variant() selects the complete fp16 layout. This newly triggers _small_host_fetch(), which calls pipeline_cls.download() without variant="fp16": the caller only adds that argument after _small_host_decision() returns. Consequently, an otherwise fully cached fp16 load now downloads the unused default weights, potentially consuming tens of gigabytes of disk and bandwidth or failing on insufficient disk space. Make the completeness check and early download respect the selected variant rather than requiring every cached index's shards.

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 4d4186f. snapshot_weights_complete now takes the variant the load will use and checks only that layout's files and shard indexes. _small_host_decision gets the local_fp16_variant() pick, and the early fetch passes the same variant="fp16". A cached load with complete fp16 twins no longer counts as incomplete because of an orphan default index, and nothing downloads the unused default weights. Added a test for complete fp16 twins plus an orphan default index: the snapshot is complete and nothing is fetched.

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 f73deec: the completeness check now only looks at the selected variant's files and index, and the early download passes the same variant. Covered by test_complete_fp16_twins_with_an_orphan_default_index_skip_the_download.

A cached fp16 load with complete .fp16 twins and an orphan default shard
index was judged incomplete, so the early fetch downloaded the unused
default weights. snapshot_weights_complete now takes the variant the load
reads, and the early fetch passes it too.
[skip ci]
@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: 5bc47bc42d

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

…uments

The fp16 variant was chosen before the hosted VAE, extra text encoders and native-quant transformer were added, so a component without an fp16 twin could trigger a default-weights download the load never reads.
@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: 75cebafa05

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

[skip ci]
@danielhanchen
danielhanchen merged commit a00356e into main Oct 30, 2026
@danielhanchen
danielhanchen deleted the dh/diffusion-partial-snapshot-download branch October 30, 2026 11:49
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 30, 2026
… installed (unslothai#16126)

## What was wrong

`test_twins_completed_by_a_supplied_component_skip_the_early_download`
(added in unslothai#16080) fails in Backend CI's `(Python 3.13, a-f)` shard on
main and on every PR: that shard installs transformers but not
diffusers, so `_class_pins_fp32("diffusers", "SD3Transformer2DModel")`
cannot import the class and conservatively reports an fp32 pin,
`plan_fp16_twins` returns None, and `local_fp16_variant` answers None
instead of `"fp16"`.

The fp32 pin is not what the test checks (it checks that a supplied
component completes the twins and skips the early download), so the test
now stubs `_class_pins_fp32` to False, the way a diffusers class without
`_keep_in_fp32_modules` answers.

## Testing

- Shard environment (transformers 5.17.0, no diffusers): main `1 failed,
34 passed, 1 skipped` (this test); this branch `35 passed, 1 skipped`.
- With diffusers 0.42.1 and transformers 5.22.1: 36 passed on both.
- Negative control with the stub in place: the same snapshot answers
`fp16` with the VAE supplied and `None` without it, so the test still
fails if the supplied-component handling regresses.
- Main's own Backend CI `(Python 3.13, a-f)` failed on this test (job
124087281866), as did `perf/r10-gemma3-decode` (124086773561) and unslothai#16021
(124087032249).
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