Skip to content

Centralize setup optimizations - #1623

Merged
dxqb merged 10 commits into
Nerogar:mergefrom
dxqb:centralize-setup-optimizations
Aug 7, 2026
Merged

Centralize setup optimizations#1623
dxqb merged 10 commits into
Nerogar:mergefrom
dxqb:centralize-setup-optimizations

Conversation

@dxqb

@dxqb dxqb commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

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-files passes
  • Launched the affected UI or script and exercised the change

AI assistance

  • AI-assisted — I have read every line in this diff and can defend each change

dxqb and others added 3 commits July 15, 2026 00:07
…()/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>

@hameerabbasi hameerabbasi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bunch of torch_gcs removed. They should be in evict and/or materialize.

Comment thread modules/modelSetup/BaseErnieSetup.py Outdated
model.text_encoder_to(self.train_device)
model.materialize_only("text_encoder")
model.eval()
torch_gc()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Careful here, do the materialise functions have GC built-in?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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

@dxqb dxqb Jul 16, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

here is the commit that is this PR 5f2caab

Comment thread modules/modelSetup/BaseSanaSetup.py Outdated
Comment thread modules/modelSetup/BaseStableDiffusion3Setup.py Outdated
Comment thread modules/modelSetup/BaseStableDiffusionSetup.py Outdated
Comment thread modules/modelSetup/BaseWuerstchenSetup.py Outdated
Comment thread modules/modelSetup/BaseStableDiffusionXLSetup.py Outdated
Comment thread modules/modelSetup/BaseZImageSetup.py Outdated

@hameerabbasi hameerabbasi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks; the once concern I had is resolved.

@dxqb dxqb added the preview merged in the preview branch label Jul 22, 2026
@dxqb dxqb mentioned this pull request Jul 23, 2026
3 tasks
dxqb and others added 5 commits July 31, 2026 22:57
…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>
@dxqb
dxqb changed the base branch from master to merge August 7, 2026 19:19
dxqb and others added 2 commits August 7, 2026 21:25
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
dxqb merged commit 22c11c9 into Nerogar:merge Aug 7, 2026
1 check passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview merged in the preview branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants