Skip to content

Conversation

Cui-yshoho
Copy link
Contributor

@Cui-yshoho Cui-yshoho commented Aug 26, 2025

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(?).
  • Monkey patch speeds up setting the default dtype of the network layers.
  • 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

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline?
  • Did you make sure to update the documentation with your changes? E.g. record bug fixes or new features in What's New. Here are the
    documentation guidelines
  • Did you build and run the code without any errors?
  • Did you report the running environment (NPU type/MS version) and performance in the doc? (better record it for data loading, model inference, or training tasks)
  • Did you write any new necessary tests?

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

@Cui-yshoho Cui-yshoho requested a review from vigo999 as a code owner August 26, 2025 02:22
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 in mindone/diffusers/models/model_loading_utils.py now directly uses ms.load_checkpoint with format="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

  1. 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.

@Cui-yshoho Cui-yshoho changed the title fix(transformers/diffusers): modify the loading weight method to accelerate fix(transformers/diffusers): accelerate model loading by optimizing the checkpoint-loading strategy. Aug 26, 2025
@Cui-yshoho Cui-yshoho changed the title fix(transformers/diffusers): accelerate model loading by optimizing the checkpoint-loading strategy. fix(transformers/diffusers): accelerate model loading by optimizing the checkpoint-loading strategy Aug 26, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

@Cui-yshoho Cui-yshoho force-pushed the load_checkpoint_faster branch from 700dc82 to 49d9a2f Compare August 26, 2025 02:34
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)
Copy link
Collaborator

@zhtmike zhtmike Aug 26, 2025

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.

@Cui-yshoho Cui-yshoho force-pushed the load_checkpoint_faster branch 4 times, most recently from 8f9c082 to bb46efa Compare August 27, 2025 06:22
@Cui-yshoho Cui-yshoho force-pushed the load_checkpoint_faster branch from bb46efa to 18264a5 Compare August 27, 2025 06:42
@vigo999 vigo999 added the feature request Add new features label Sep 29, 2025
@vigo999 vigo999 added this to mindone Sep 29, 2025
@vigo999 vigo999 moved this to In Progress in mindone Sep 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Add new features
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

4 participants