Centralize setup optimizations - #1623
Merged
Merged
Conversation
…()/evict() API
Replaces the per-model `{part}_to(device)` methods across all model classes,
plus scattered call sites in dataLoader/modelSetup/modelSampler/GenericTrainer,
with generic BaseModel methods driven by the existing ModelType.model_parts()
registry: materialize(*parts), evict(*parts), and materialize_only(*parts)
(evict everything else, then materialize the given parts - the swap-in/swap-out
pattern used throughout the Samplers and text-caching setup). eval() and
adapters() are likewise made concrete on BaseModel instead of hand-written per
model. Models whose component names diverge (Wuerstchen) or that have
components outside model_parts() (SD's depth_estimator, Anima's
text_conditioner) override the relevant methods directly.
Also fixes multi-TE samplers (Flux/SD3/SDXL/HiDream/HunyuanVideo) that
previously evicted all but the first text encoder and ran encode_text with
the rest still on temp_device.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…elper into BaseModelSetup
hameerabbasi
suggested changes
Jul 16, 2026
hameerabbasi
left a comment
Contributor
There was a problem hiding this comment.
Bunch of torch_gcs removed. They should be in evict and/or materialize.
| model.text_encoder_to(self.train_device) | ||
| model.materialize_only("text_encoder") | ||
| model.eval() | ||
| torch_gc() |
Contributor
There was a problem hiding this comment.
Careful here, do the materialise functions have GC built-in?
Collaborator
Author
There was a problem hiding this comment.
the meaning of materialize_only() is evict everything except this
evict() contains torch_gc()
note that this is introduced by #1617 not here
unfortunately github doesn't have an easy feature to only show the changes of PR B when it builds on top of PR A
Collaborator
Author
There was a problem hiding this comment.
here is the commit that is this PR 5f2caab
hameerabbasi
approved these changes
Jul 16, 2026
hameerabbasi
left a comment
Contributor
There was a problem hiding this comment.
Thanks; the once concern I had is resolved.
3 tasks
…eanup evictions `LayerOffloadConductor.to(device)` inferred the direction from the device argument. Splitting it into `materialize()` and `evict()` makes the direction explicit at the call site, and lets the offload config be passed in per call instead of being stored on the conductor. Cleanup calls that evicted a part only for the next step to materialize it again are removed; eviction now happens at save and teardown boundaries. `SampleWindowController` keeps an explicit eviction because nothing follows it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The dataloader debug hooks used materialize_only("vae"), which evicts every
other part. They run in the middle of caching, so the parts the caching loop
had just materialized were pulled back out from under it. They only need the
VAE present, so materialize() is enough.
_move_part dispatched the conductor on train_device and treated everything
else as an eviction, so an unexpected device silently evicted instead of
failing. It now dispatches on temp_device and asserts the remaining case is
train_device.
SampleWindowController evicts in a finally block, so a failed or cancelled
sample no longer leaves the model resident.
scripts/sample.py passed the train device as both train_device and
temp_device, so nothing could ever be evicted; it now sets a real temp device
and fills in train_device/temp_device on the TrainConfig it builds. It also
passes the quantization config to the loader, which it previously dropped.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
_setup_model_part took an attention_mask parameter whose None value meant "do not set an attention backend for this part", which conflated two unrelated things: mask is only a hint about whether the architecture uses masked attention, and its absence is not a sensible way to skip backend selection. Drop the parameter and call _set_attention_backend directly from each leaf setup_optimizations again, once per part that has a backend. The resulting set of calls is unchanged. Also condense the comments added when setup_optimizations was centralized. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
BaseStableDiffusionSetup, BaseStableDiffusionXLSetup and BaseWuerstchenSetup kept the super() call in the middle of the function, where the create_autocast_context assignment it replaced used to sit. Every setup that goes through _setup_model_part calls it first, as the comment in BaseModelSetup.setup_optimizations describes. Nothing above the call reads model.train_dtype or model.autocast_context, so moving it up is behaviour neutral. Also drop the comment claiming the SD/SDXL UNet can't go through _setup_model_part because supports_offloading=False is unpassable: the slot takes it fine when hardcoded in a wrapper, as enable_checkpointing_for_clip_encoder_layers does. What actually doesn't fit is that the UNet needs a second, diffusers-side enable_gradient_checkpointing() call, and the slot holds only one function. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
dxqb
added a commit
to dxqb/OneTrainer
that referenced
this pull request
Aug 9, 2026
materialize()/evict() (Nerogar#1617) and the setup_optimizations centralization (Nerogar#1623) landed upstream in a later form than this branch was published with. Resolved to the shape already reconciled on the source branch.
dxqb
added a commit
to dxqb/OneTrainer
that referenced
this pull request
Aug 9, 2026
materialize()/evict() (Nerogar#1617) and the setup_optimizations centralization (Nerogar#1623) landed upstream in a later form than this branch carries. Resolved to the shape already reconciled on the source branch.
dxqb
added a commit
to dxqb/OneTrainer
that referenced
this pull request
Aug 13, 2026
Resolves the collisions with the materialize()/evict() API (Nerogar#1617), the centralized setup_optimizations (Nerogar#1623) and the weight-compression work (Nerogar#1630) that landed upstream while this branch was open. Conflicting files are resolved to the versions this feature has been developed and tested against.
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
This is another refactor PR that does not change any behaviour, but is necessary to implement future PRs without having to copy their wiring code across all ~ 15 models
Contains #1617
Test plan
pre-commit run --all-filespassesAI assistance