Skip to content

Centralize setup optimizations#1623

Open
dxqb wants to merge 3 commits into
Nerogar:masterfrom
dxqb:centralize-setup-optimizations
Open

Centralize setup optimizations#1623
dxqb wants to merge 3 commits into
Nerogar:masterfrom
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.

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
Comment thread modules/modelSetup/BaseStableDiffusion3Setup.py
Comment thread modules/modelSetup/BaseStableDiffusionSetup.py
Comment thread modules/modelSetup/BaseWuerstchenSetup.py
Comment thread modules/modelSetup/BaseStableDiffusionXLSetup.py
Comment thread modules/modelSetup/BaseZImageSetup.py

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