[feat] fsdp2 memory_efficient_init#117
Conversation
…uffers, _restore_non_persistent_buffers helpers
…ss memory_efficient through _lazy_wrap_model
…istent buffer detection
… into optimize_fsdp_init
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 focuses on optimizing the memory footprint during the initialization of FSDP models, particularly for large Transformer architectures. By leveraging meta-device initialization and distributed state dict handling, it allows models to be loaded and sharded more efficiently across multiple GPUs, reducing the peak memory consumption on both CPU and GPU. The changes ensure that both native FSDP and Accelerate-based FSDP strategies can benefit from these memory improvements, making it feasible to train larger models or use more aggressive sharding configurations. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. 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. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a significant optimization for FSDP model initialization by implementing a memory-efficient path. The changes are well-architected, with clear logic for both the accelerate and native_fsdp strategies. The use of a meta-device initialization flow, along with robust handling of environment variables for integration with the transformers library, is commendable. The addition of a comprehensive test suite, including unit, integration, and regression tests, greatly enhances the reliability of this new feature. I have one suggestion to further improve the test coverage for the environment variable manipulation logic.
…st_sharded_state_dict
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a memory_efficient_init parameter to enable memory-efficient FSDP model initialization, where only rank 0 loads full weights and broadcasts sharded parameters. This feature is integrated into AccelerateStrategy and NativeFSDPStrategy, and new utility functions are added to manage state dicts and buffers during this process. The documentation has been updated to reflect this new parameter. The reviewer suggested improving the clarity of the transformers version specifier in the documentation from 4.57.x to <4.58> for both English and Chinese versions.
|
/gemini summary |
Summary of ChangesThis pull request introduces a memory-efficient initialization mechanism for FSDP (Fully Sharded Data Parallel) models, applicable to both native PyTorch FSDP and Accelerate strategies. The core idea is to reduce peak memory and VRAM usage during model loading and initialization by having only rank 0 load full model weights, while other ranks use a meta-device and then receive sharded parameters via broadcast. This optimization allows for more efficient handling of large models, particularly with Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
|
PR type
PR information
Add
memory_efficient_initsupport for FSDP (including Accelerate Strategy and Native FSDP Strategy) to reduce peak memory and VRAM usage during the model initialization phase.Core idea: Before FSDP wrapping, only rank 0 holds the full parameters, while other ranks move the model to the meta device. After wrapping is completed, parameters are broadcast and sharded across ranks via
broadcast+distribute_tensor, avoiding each rank loading the full model weights.Main changes:
NativeFSDPStrategy.wrap_model: Added a meta-device initialization process — save state_dict →to('meta')→fully_shard→ broadcast sharded parameters → restore non-persistent buffersAccelerateStrategy: Achieves the same effect via thecpu_ram_efficient_loadingconfiguration option and environment variable context managerload_context.py: Providesfsdp_pretrained_load_context, which temporarily sets theACCELERATE_USE_FSDP/FSDP_CPU_RAM_EFFICIENT_LOADINGenvironment variables duringfrom_pretrainedExperiment results
ENV:
4xH800,transformers==4.57.6,Qwen3-8BSampling CPU memory and GPU memory during the
transformers.from_pretrained+ optimizer creation +wrap_modelprocessAccelerate: memory_efficient=True vs. memory_efficient=False
Accelerate vs. Native FSDP