From 6782d9937a8e391ad15773b206a958914ca4237d Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Tue, 3 Mar 2026 08:28:55 -0800 Subject: [PATCH] Also patch accelerate's is_wandb_available for trl callbacks path (#4148) 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. --- unsloth/import_fixes.py | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/unsloth/import_fixes.py b/unsloth/import_fixes.py index 06322a9396..ca44a0ce7e 100644 --- a/unsloth/import_fixes.py +++ b/unsloth/import_fixes.py @@ -1294,12 +1294,17 @@ def disable_broken_wandb(): """Disable wandb if it's installed but cannot actually import. wandb can fail to import when there's a protobuf version mismatch - (e.g., wandb < 0.19.11 with protobuf >= 6.0). This causes a cascading - import failure through trl -> transformers -> wandb that crashes - unsloth's import chain. + (e.g., wandb < 0.19.11 with protobuf >= 6.0). This causes cascading + import failures through trl -> transformers/accelerate -> wandb that + crash unsloth's import chain. - This function tests if wandb can actually import and if not, patches - transformers' is_wandb_available() to return False. + There are two separate is_wandb_available() functions used by trl: + - transformers.integrations.integration_utils.is_wandb_available + (used by most trl trainers) + - accelerate.utils.imports.is_wandb_available + (used by trl/trainer/callbacks.py) + + Both must be patched to fully prevent broken wandb imports. """ if importlib.util.find_spec("wandb") is None: return # wandb not installed, nothing to do @@ -1307,18 +1312,36 @@ def disable_broken_wandb(): try: import wandb except Exception: - # wandb is installed but broken - patch transformers to skip it + # wandb is installed but broken - patch all checkers to skip it logger.info( "Unsloth: wandb is installed but broken (likely a protobuf version mismatch). " "Disabling wandb to prevent import errors. To fix, run: pip install --upgrade wandb" ) + _wandb_false = lambda: False + # 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 - # Also set env var as fallback for any other code path + # 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 + # Set env var as additional fallback os.environ["WANDB_DISABLED"] = "true"