Skip to content

materialize() / evict() API - #1617

Merged
dxqb merged 4 commits into
Nerogar:mergefrom
dxqb:materialize-evict-api
Aug 7, 2026
Merged

dxqb merged 4 commits into
Nerogar:mergefrom
dxqb:materialize-evict-api

Conversation

@dxqb

@dxqb dxqb commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

Summary

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

Doesn't do anything except refactor, but it's the basis to implement future functionality without duplicating it for each model

Test plan

  • pre-commit run --all-files passes
  • Launched the affected UI or script and exercised the change
  • Tested with at least one real preset / config when relevant (note which: ____)

AI assistance

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

…()/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 <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
@dxqb dxqb mentioned this pull request Jul 15, 2026
1 of 2 tasks
@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 done
dxqb and others added 2 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) <[email protected]>
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) <[email protected]>
@dxqb
dxqb changed the base branch from master to merge August 7, 2026 19:18
@dxqb
dxqb merged commit 563db34 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.
joshuafcole pushed a commit to joshuafcole/OneTrainer that referenced this pull request Aug 28, 2026
…ain_config

`capture_activations` built an `AnimaModel`, loaded it, and handed it to
`AnimaSampler` without ever setting `model.train_config`. `BaseModel.__init__`
leaves that at `None`, and both `train_device` and `temp_device` are properties
that read it -- so upstream's materialize/evict API (Nerogar#1617), which is how the
sampler places each part, died on the first phase switch:

    File "modules/model/BaseModel.py", line 106, in temp_device
        return torch.device(self.train_config.temp_device)
    AttributeError: 'NoneType' object has no attribute 'temp_device'

The timing is the worst part: `materialize_only("text_encoder")` runs after the
base model has finished loading, so the whole expensive half of a capture is
paid before the failure appears. `scripts/sample.py:44` sets the same field for
the same reason and is the shape copied here.

`temp_device` is `cpu`, matching `sample.py` -- the only Anima sampling
configuration ever proven to work -- rather than the GPU-resident value that was
being passed to the sampler. That value was inert either way: `BaseModelSampler`
stores `temp_device` and `AnimaSampler` never reads it, so nothing had ever
exercised the everything-stays-resident intent it implied. The sampler now gets
the device the model actually evicts to.

Guarded by AST rather than by running it: as the test module says, the capture
half needs Anima and a box. What is provable without one is that the assignment
exists and precedes the sampler's construction, which is exactly what was wrong.

Co-Authored-By: Claude Opus 5 <[email protected]>
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.

1 participant