Skip to content

Fix DDP "marked ready twice" for VLMs with CPU offload + TiledMLP#4077

Open
nepfaff wants to merge 4 commits intounslothai:mainfrom
nepfaff:fix/ddp-vlm-tiled-mlp-checkpoint
Open

Fix DDP "marked ready twice" for VLMs with CPU offload + TiledMLP#4077
nepfaff wants to merge 4 commits intounslothai:mainfrom
nepfaff:fix/ddp-vlm-tiled-mlp-checkpoint

Conversation

@nepfaff
Copy link

@nepfaff nepfaff commented Feb 17, 2026

Improves the existing DDP compatibility block (from PR #3751) with two targeted fixes that preserve Unsloth's memory optimizations:

  1. Non-reentrant checkpointing with CPU activation offloading via saved_tensors_hooks. PR Fix VLM + DDP checkpointing #3751 switched to non-reentrant but dropped CPU offloading entirely.

  2. DDP-safe TiledMLP backward: uses functional torch.autograd.grad() for all-but-last sequence chunk (no DDP hooks fired), then .backward() for the final chunk (fires hooks exactly once).

Both fixes are gated behind is_distributed(), so single-GPU training is completely unaffected.

Tested on Qwen3-VL-4B + LoRA with 8x L40S GPUs. These changes successfully enabled multi-GPU training.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @nepfaff, 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 resolves critical DDP compatibility issues for Vision Language Models (VLMs) that utilize CPU offloading and TiledMLP. The changes ensure that Unsloth's memory optimizations are maintained while enabling stable multi-GPU training by preventing the 'marked ready twice' error. These fixes are specifically applied when is_distributed() is true, ensuring no impact on single-GPU training setups.

Highlights

  • Enhanced DDP Checkpointing with CPU Offload: Implemented non-reentrant checkpointing that leverages saved_tensors_hooks to offload activations to CPU, preserving memory optimizations while ensuring compatibility with Distributed Data Parallel (DDP) training.
  • DDP-Safe TiledMLP Backward Pass: Patched the TiledMLP backward pass to correctly handle gradient accumulation in DDP. It now uses torch.autograd.grad() for all but the final sequence chunk and torch.autograd.backward() only for the last chunk, preventing multiple DDP hook firings.

🧠 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
  • unsloth/models/vision.py
    • Modified the DDP compatibility block to incorporate CPU activation offloading for non-reentrant gradient checkpointing.
    • Introduced a new _ddp_safe_backward method for TiledMLP to manage gradient computation in a DDP-compatible manner, using torch.autograd.grad() and torch.autograd.backward() strategically.
Activity
  • No specific activity (comments, reviews, or progress updates) was provided in the context for this pull request.
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.

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

The pull request introduces two critical fixes for Distributed Data Parallel (DDP) compatibility in Vision-Language Models (VLMs). First, it implements non-reentrant gradient checkpointing with CPU activation offloading using saved_tensors_hooks. This restores memory efficiency that was previously lost when switching to non-reentrant checkpointing for DDP safety. Second, it patches the TiledMLP backward pass to be DDP-safe by using functional torch.autograd.grad for all but the last sequence chunk, ensuring that DDP hooks (which trigger gradient reduction) are only fired once during the final .backward() call. These changes are correctly gated behind is_distributed() to avoid impacting single-GPU performance.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 93c5aca876

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

nepfaff and others added 2 commits February 17, 2026 15:52
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@mmathew23
Copy link
Collaborator

Hi @nepfaff thanks for the PR. We've been working on a non reentrant checkpointer which should perform similarly to the reentrant version. I haven't yet started the TiledMLP portion yet. It would be cool to collab on that piece if you're interested. That would be a change to unsloth-zoo/tiled_mlp.py and ultimately there would need to be some coordination to match checkpointing and tiled mlp reentrant approaches.

@nepfaff
Copy link
Author

nepfaff commented Feb 17, 2026

That sounds exciting! I'm unsure about how much time I will have, but I'd be happy to help out.

@mmathew23
Copy link
Collaborator

@nepfaff Are you on our discord? If you could ping me there I'm doublemathew and we can discuss further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants