memory use optimisations; disable grad calc for text encoder - #2110
Merged
Conversation
added 10 commits
December 8, 2025 10:27
…oad even if not in StateTracker
samzhang996-hue
pushed a commit
to samzhang996-hue/SimpleTuner
that referenced
this pull request
Apr 27, 2026
…-multigpu memory use optimisations; disable grad calc for text encoder
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.
tl;dr fixes memory use for text encoders not evicting fully.
This pull request introduces improvements to memory management and pipeline handling for text embedding caches and model components, with a focus on reducing VRAM usage and improving resource cleanup. The changes add mechanisms to track and clear cached pipelines and text encoders, enhance reporting of CUDA memory usage, and ensure that all text embedding caches are properly cleared, including those not directly tracked by the main data backend registry. Additionally, new tests are provided to verify the correct unloading of text encoders and cache components.
Memory and resource management improvements:
WeakSetregistry to theTextEmbeddingCacheclass to track all active cache instances and a newactive_cachesclass method for retrieving them. This enables comprehensive cleanup of all caches, including those not referenced by the main backend registry. [1] [2]_report_cuda_usagemethod inTrainerto log which model and cache components still hold CUDA tensors, aiding in pinpointing VRAM leaks. Also added_clear_pipeline_cachesto drop cached pipelines before training.init_unload_text_encodermethod inTrainerto clear text encoders and pipelines from allTextEmbeddingCacheinstances, not just those in the main registry, and to report memory usage before and after unloading.Pipeline initialization and placeholder logic:
ModelFoundationto use placeholder modules when the base model is not loaded, preventing errors from unavailable components. Also improved scheduler handling by reconstructing or deep-copying the scheduler if needed. [1] [2] [3] [4] [5]encode_wan_promptmethod inTextEmbeddingCacheto obtain pipelines via the model using the new registry and placeholder logic, improving robustness.Testing and reliability:
tests/test_text_encoder_unload.py) to verify that untracked text embed caches are properly cleared when unloading text encoders, ensuring reliability of the new cleanup logic.These changes collectively improve the robustness of memory management, resource cleanup, and pipeline initialization in the training workflow, helping to prevent VRAM leaks and ensuring that all caches are properly handled.