feat(qwen_image): optimize VAE memory usage via dynamic offloading#668
Open
avan06 wants to merge 1 commit intoostris:mainfrom
Open
feat(qwen_image): optimize VAE memory usage via dynamic offloading#668avan06 wants to merge 1 commit intoostris:mainfrom
avan06 wants to merge 1 commit intoostris:mainfrom
Conversation
85732be to
60e5fa9
Compare
This commit introduces memory optimization to the encode_images method in qwen_image.py. Changes: - Dynamic Device Management: The VAE is now temporarily moved to the target device for encoding and automatically offloaded back to the CPU if low_vram is enabled or if the model was originally on the CPU. - Memory Optimization: Added torch.cuda.empty_cache() after offloading to ensure GPU memory is released immediately. - Resource Efficiency: Wrapped the encoding process in torch.no_grad() to prevent unnecessary gradient computation and reduce memory footprint.
60e5fa9 to
c32220f
Compare
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.
This commit introduces memory optimization to the encode_images method in qwen_image.py.
Changes:
The VAE offloading mechanism is currently enabled only when low_vram is active or the VAE is located on the CPU. This restriction minimizes the impact of the changes. These modifications have been tested and verified to effectively reduce VRAM usage during training.