-
Notifications
You must be signed in to change notification settings - Fork 88
fix(transformers/diffusers): accelerate model loading by optimizing the checkpoint-loading strategy #1231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @Cui-yshoho, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces significant optimizations to the model weight loading process within the transformers
and diffusers
components. The primary goal is to accelerate model loading times and mitigate Out-Of-Memory (OOM) issues during dtype conversions. This is achieved by leveraging MindSpore's no_init_parameters
context manager to skip redundant parameter initialization and by adopting the to
method for safer and more efficient dtype changes of state dictionary parameters. The changes have been tested on Ascend Atlas 800T A2 machines with MindSpore 2.7.0, demonstrating a substantial 65% reduction in loading time for models like THUDM/CogVideoX-5b
.
Highlights
- Accelerated Weight Loading: The pull request significantly reduces the time required to load model weights, specifically demonstrating a 65% speedup for
THUDM/CogVideoX-5b
by optimizing the loading method. - Memory Optimization: Changes were made to prevent Out-Of-Memory (OOM) errors during state dictionary dtype changes by utilizing the
to
method for type conversion. - Parameter Initialization Control: The
no_init_parameters
context manager is now employed during model instantiation to skip unnecessary parameter initialization, contributing to faster model loading. - Safetensors Loading Improvement: The
load_state_dict
function inmindone/diffusers/models/model_loading_utils.py
now directly usesms.load_checkpoint
withformat="safetensors"
for more efficient loading of safetensors files.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces significant optimizations to accelerate model weight loading. The use of no_init_parameters
to prevent unnecessary weight initialization and the switch to v.to()
for data type casting to avoid out-of-memory errors are excellent improvements. The reported performance gain is substantial. The changes are well-implemented and address a crucial performance bottleneck. I have one suggestion to refactor a small portion of the code for better readability and maintainability.
700dc82
to
49d9a2f
Compare
for k, v in state_dict.items(): | ||
if k in local_state: | ||
v.set_dtype(local_state[k].dtype) | ||
state_dict[k] = ms.Parameter(v.to(local_state[k].dtype), name=k) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this change will load tensor v
from host to device (NPU), cause the NPU memory pre-occupied. So it may be not compatible with current Mindone ZeRO3 implementation, i.e., load parameter in host -> shard in host -> shard weight loaded from host to device.
8f9c082
to
bb46efa
Compare
bb46efa
to
18264a5
Compare
What does this PR do?
cpu_cast
is used instead of.to()
to avoid changing the device and MindSpore 2.7 can automatically release Parameters(?).no_init_parameters
is used to skip parameter initialization.!!! Experiments are tested on Ascend Atlas 800T A2 machines with mindspore 2.7.0.
Recommend to use mindspore 2.7.0 to avoid many known issues.
Reduces the loading time of
THUDM/CogVideoX-5b
from 408 s → 143 s (≈ 65 % faster) with this change.Before submitting
What's New
. Here are thedocumentation guidelines
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
@xxx