Skip to content

feat(fsdp): add memory_efficient_load option for large model initialization#862

Merged
garrett4wade merged 1 commit into
mainfrom
rchardx/fsdp_oom
Jan 28, 2026
Merged

feat(fsdp): add memory_efficient_load option for large model initialization#862
garrett4wade merged 1 commit into
mainfrom
rchardx/fsdp_oom

Conversation

@rchardx

@rchardx rchardx commented Jan 27, 2026

Copy link
Copy Markdown
Collaborator

Description

When training large models, loading weights directly onto each GPU can cause OOM during initialization. This adds fsdp.memory_efficient_load config option that initializes model structure on CPU, then rank 0 loads pretrained weights and broadcasts to all ranks after FSDP sharding.

This reduces peak GPU memory during model loading at the cost of slightly longer initialization time.

Related Issue

Fixes #845

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not
    work as expected)
  • Documentation update
  • Code refactoring (no functional changes)
  • Performance improvement
  • Test coverage improvement

Checklist

  • I have read the Contributing Guide
  • I have run formatting tools (pre-commit or manual)
  • I have run relevant unit tests and they pass
  • I have added tests for new functionality
  • I have updated documentation if needed
  • My branch is up to date with main
  • This PR introduces breaking changes (if yes, fill out details below)
  • If this PR changes documentation, I have built and previewed it locally with
    jb build docs
  • No critical issues raised by AI reviewers (/gemini review)

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @rchardx, 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 addresses Out-Of-Memory (OOM) issues that can occur during the initialization of very large models in a Fully Sharded Data Parallel (FSDP) setup. It introduces a new configuration option, memory_efficient_load, which alters the model loading strategy. Instead of each GPU loading the full model weights, the model structure is initialized on the CPU, and only rank 0 loads the pretrained weights, which are then broadcast to all other ranks after FSDP sharding. This approach prioritizes memory efficiency over a slight increase in initialization time, making it easier to train larger models.

Highlights

  • New FSDP Configuration Option: Introduced fsdp.memory_efficient_load as a new boolean configuration option to control the memory-efficient loading strategy for large models.
  • Memory-Efficient Model Loading Implementation: Implemented logic where, if memory_efficient_load is enabled, model weights are initialized on the CPU by rank 0 and then broadcast to other ranks after FSDP sharding, significantly reducing peak GPU memory usage during initialization.
  • Documentation Updates: Added comprehensive documentation in docs/best_practices/handling_oom.md and docs/cli_reference.md to explain the new memory_efficient_load feature, its benefits, and usage.

🧠 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 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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces a memory_efficient_load option for FSDP, which allows initializing large models on the CPU and then broadcasting weights after FSDP sharding. This is a valuable performance improvement for reducing peak GPU memory during initialization. The changes are well-implemented across the configuration, engine logic, and documentation, ensuring consistency and clarity. The new feature is clearly explained in the best practices and CLI reference documentation.

Comment thread areal/engine/fsdp_engine.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a memory_efficient_load configuration option for FSDP training to address out-of-memory (OOM) issues during large model initialization, specifically fixing issue #845 regarding Qwen3 32B OOM with FSDP2.

Changes:

  • Added fsdp.memory_efficient_load boolean config option that enables memory-efficient model loading
  • Modified model initialization to create model structure on CPU and load pretrained weights only on rank 0, then broadcast to all ranks after FSDP sharding
  • Updated documentation to explain the new feature and its benefits for handling OOM during initialization

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
areal/api/cli_args.py Added memory_efficient_load field to FSDPEngineConfig with appropriate metadata
areal/engine/fsdp_engine.py Implemented memory-efficient loading logic: model creation on CPU, conditional weight loading on rank 0, and broadcasting after FSDP sharding
docs/cli_reference.md Added documentation for the new memory_efficient_load parameter in the FSDP configuration table
docs/best_practices/handling_oom.md Added section explaining how to use memory-efficient loading to resolve initialization OOM errors

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread docs/best_practices/handling_oom.md
Comment thread docs/cli_reference.md Outdated
Comment thread areal/engine/fsdp_engine.py
@rchardx rchardx added the safe-to-test Ready to run unit-tests in a PR. label Jan 27, 2026
@rchardx rchardx temporarily deployed to AReaL-unittests January 27, 2026 10:26 — with GitHub Actions Inactive
Comment thread areal/api/cli_args.py
Comment thread areal/engine/fsdp_engine.py
…zation

When training large models, loading weights directly onto each GPU can cause
OOM during initialization. This adds `fsdp.memory_efficient_load` config option
that initializes model structure on CPU, then rank 0 loads pretrained weights
and broadcasts to all ranks after FSDP sharding.

This reduces peak GPU memory during model loading at the cost of slightly
longer initialization time.
@rchardx rchardx added safe-to-test Ready to run unit-tests in a PR. and removed safe-to-test Ready to run unit-tests in a PR. labels Jan 27, 2026
@rchardx rchardx temporarily deployed to AReaL-unittests January 27, 2026 15:59 — with GitHub Actions Inactive

@garrett4wade garrett4wade left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@garrett4wade garrett4wade merged commit 803dccb into main Jan 28, 2026
7 checks passed
@garrett4wade garrett4wade deleted the rchardx/fsdp_oom branch January 28, 2026 02:27
leandermaben pushed a commit to leandermaben/AReaL that referenced this pull request Mar 24, 2026
…zation (areal-project#862)

When training large models, loading weights directly onto each GPU can cause
OOM during initialization. This adds `fsdp.memory_efficient_load` config option
that initializes model structure on CPU, then rank 0 loads pretrained weights
and broadcasts to all ranks after FSDP sharding.

This reduces peak GPU memory during model loading at the cost of slightly
longer initialization time.
SathyaGnanakumar pushed a commit to danielkiely/AReaL that referenced this pull request Apr 29, 2026
…zation (areal-project#862)

When training large models, loading weights directly onto each GPU can cause
OOM during initialization. This adds `fsdp.memory_efficient_load` config option
that initializes model structure on CPU, then rank 0 loads pretrained weights
and broadcasts to all ranks after FSDP sharding.

This reduces peak GPU memory during model loading at the cost of slightly
longer initialization time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

safe-to-test Ready to run unit-tests in a PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]qwen3 32B oom when using fsdp2,loading model should use cpu or meta but gpu/npu

4 participants