Skip to content

Fix wandb import crash in trl callbacks (accelerate path)#4148

Merged
danielhanchen merged 1 commit into
mainfrom
fix-wandb-import-accelerate-path
Mar 3, 2026
Merged

Fix wandb import crash in trl callbacks (accelerate path)#4148
danielhanchen merged 1 commit into
mainfrom
fix-wandb-import-accelerate-path

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Problem

After #4147, from unsloth import FastLanguageModel works with a broken wandb, but from trl import GRPOTrainer still crashes:

trl/trainer/grpo_trainer.py -> trl/trainer/callbacks.py line 51-52:
  if is_wandb_available():
      import wandb  # crashes here

This is because callbacks.py imports is_wandb_available from accelerate.utils, which is a separate function from the transformers version that #4147 patched. The accelerate version does not check WANDB_DISABLED and was not covered by the original fix.

Additionally, Python's from accelerate.utils import X resolves through accelerate/utils/__init__.py which holds its own cached reference. Patching accelerate.utils.imports.is_wandb_available alone does not affect the re-exported name in accelerate.utils, so both namespaces must be patched.

Test plan

  • Broken wandb: from unsloth import FastLanguageModel succeeds
  • Broken wandb: from trl import GRPOConfig, GRPOTrainer succeeds
  • Healthy wandb: both imports work, is_wandb_available() returns True, import wandb works

trl/trainer/callbacks.py imports is_wandb_available from
accelerate.utils, not from transformers. The original fix in #4147
only patched the transformers version, so `from trl import GRPOTrainer`
still crashed via the callbacks.py -> accelerate -> wandb path.

Must patch both the source module (accelerate.utils.imports) AND the
re-export namespace (accelerate.utils) since Python's
`from accelerate.utils import X` reads from the latter, which holds
its own cached reference.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 an issue where trl.GRPOTrainer would crash due to a broken wandb installation, even after a previous fix for transformers' is_wandb_available. The solution extends the disable_broken_wandb utility to correctly patch accelerate's is_wandb_available function across both its direct module and re-exported namespace, thereby preventing wandb import failures from disrupting unsloth's import chain.

Highlights

  • Extended disable_broken_wandb function: The disable_broken_wandb function was extended to also patch accelerate.utils.is_wandb_available.
  • Fixed trl.GRPOTrainer crash: Addressed crashes in trl.GRPOTrainer that occurred due to broken wandb imports, specifically when accelerate's is_wandb_available was not properly disabled.
  • Comprehensive accelerate patching: Implemented patching at both accelerate.utils.imports and accelerate.utils namespace levels to ensure comprehensive disabling of wandb checks, accounting for Python's import mechanisms.
Changelog
  • unsloth/import_fixes.py
    • Updated the docstring for disable_broken_wandb to reflect the need to patch accelerate's is_wandb_available in addition to transformers.
    • Added logic to patch accelerate.utils.imports.is_wandb_available and accelerate.utils.is_wandb_available to return False when wandb is installed but broken.
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.

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

Copy link
Copy Markdown

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: 6680599ecb

ℹ️ 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".

Comment thread unsloth/import_fixes.py
Comment on lines +1332 to +1336
try:
import accelerate.utils.imports as acc_imports

acc_imports.is_wandb_available = _wandb_false
except (ImportError, AttributeError):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Catch non-import failures when patching accelerate

In disable_broken_wandb(), the new accelerate patch path only handles ImportError/AttributeError, so any other exception raised while importing accelerate.utils.imports (for example runtime/library mismatch errors during module initialization) will propagate and crash import unsloth in the broken-wandb recovery path. This is a regression from the goal of making imports resilient to broken optional dependencies; broaden this handler (and the similar block below) to tolerate non-import exceptions as well.

Useful? React with 👍 / 👎.

@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

This pull request correctly extends the fix for broken wandb installations by patching accelerate's is_wandb_available function, in addition to the one in transformers, which is necessary to prevent import crashes in trl. The implementation is sound. I've provided one suggestion to refactor the repeated patching logic into a loop, which would improve code clarity and maintainability.

Comment thread unsloth/import_fixes.py
Comment on lines +1321 to 1343
# Patch transformers' is_wandb_available (used by most trl trainers)
try:
import transformers.integrations.integration_utils as tf_integration

tf_integration.is_wandb_available = lambda: False
tf_integration.is_wandb_available = _wandb_false
except (ImportError, AttributeError):
pass
# Patch accelerate's is_wandb_available (used by trl/trainer/callbacks.py).
# Must patch both the source module AND the re-export namespace since
# `from accelerate.utils import is_wandb_available` reads from
# accelerate.utils, not accelerate.utils.imports.
try:
import accelerate.utils.imports as acc_imports

acc_imports.is_wandb_available = _wandb_false
except (ImportError, AttributeError):
pass
try:
import accelerate.utils as acc_utils

acc_utils.is_wandb_available = _wandb_false
except (ImportError, AttributeError):
pass

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.

medium

While the current implementation is correct, there's an opportunity to refactor the repeated try...except blocks to improve code clarity and maintainability. By using a loop to iterate over the modules that need patching, you can reduce duplication and make it easier to add more modules in the future.

        # Patch all known is_wandb_available functions to prevent import errors.
        # We patch both the source module and any re-export namespaces.
        modules_to_patch = [
            "transformers.integrations.integration_utils",  # Used by most trl trainers
            "accelerate.utils.imports",  # Source for accelerate's check
            "accelerate.utils",  # Re-export used by trl/trainer/callbacks.py
        ]
        for module_name in modules_to_patch:
            try:
                module = importlib.import_module(module_name)
                module.is_wandb_available = _wandb_false
            except (ImportError, AttributeError):
                pass

@danielhanchen danielhanchen merged commit a914948 into main Mar 3, 2026
4 checks passed
@danielhanchen danielhanchen deleted the fix-wandb-import-accelerate-path branch March 3, 2026 16:28
abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
…slothai#4148)

trl/trainer/callbacks.py imports is_wandb_available from
accelerate.utils, not from transformers. The original fix in unslothai#4147
only patched the transformers version, so `from trl import GRPOTrainer`
still crashed via the callbacks.py -> accelerate -> wandb path.

Must patch both the source module (accelerate.utils.imports) AND the
re-export namespace (accelerate.utils) since Python's
`from accelerate.utils import X` reads from the latter, which holds
its own cached reference.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant