[V1] Support meta loading for full and free#10236
[V1] Support meta loading for full and free#10236jiaqiw09 wants to merge 2 commits intohiyouga:mainfrom
Conversation
Summary of ChangesHello, 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 significantly improves the model loading process for FSDP2 by enabling proper support for 'meta' device initialization. It addresses critical aspects like managing non-persistent buffers and ensuring tied weights are correctly handled, which is crucial for large model training efficiency. Concurrently, it temporarily restricts LoRA from using meta-device loading, indicating a focused approach to FSDP2's capabilities. Highlights
Changelog
Activity
Using Gemini Code AssistThe 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 by creating a comment using either
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces support for meta device loading in FSDP2. The changes involve saving and restoring non-persistent buffers, and correctly handling tied weights during model materialization. A new test file has been added to verify this behavior. My review includes a suggestion to improve code clarity in src/llamafactory/v1/plugins/trainer_plugins/distributed/fsdp2.py by reducing code duplication.
| if getattr(model.config, "tie_word_embeddings", None): | ||
| model.tie_weights() | ||
|
|
||
| model = self.prepare_model(model) | ||
| model = self.materialize_and_load(model, hf_model_path=model.config.name_or_path, dcp_path=self.dcp_path) | ||
|
|
||
| # fix tied broken for no-fsdp-wrap case | ||
| if getattr(model.config, "tie_word_embeddings", None): | ||
| model.tie_weights() |
There was a problem hiding this comment.
To improve code clarity and avoid redundant calls to getattr, you can store the result of getattr(model.config, "tie_word_embeddings", None) in a variable and reuse it.
| if getattr(model.config, "tie_word_embeddings", None): | |
| model.tie_weights() | |
| model = self.prepare_model(model) | |
| model = self.materialize_and_load(model, hf_model_path=model.config.name_or_path, dcp_path=self.dcp_path) | |
| # fix tied broken for no-fsdp-wrap case | |
| if getattr(model.config, "tie_word_embeddings", None): | |
| model.tie_weights() | |
| should_tie_weights = getattr(model.config, "tie_word_embeddings", None) | |
| if should_tie_weights: | |
| model.tie_weights() | |
| model = self.prepare_model(model) | |
| model = self.materialize_and_load(model, hf_model_path=model.config.name_or_path, dcp_path=self.dcp_path) | |
| # fix tied broken for no-fsdp-wrap case | |
| if should_tie_weights: | |
| model.tie_weights() |
What does this PR do?
Fixes # (issue)
Before submitting
@hiyouga would you mind having a check? I will make pr for lora next.
here is the test for full after I set all deterministic
