Fix broken wandb import crashing unsloth startup (#4147)

* Fix broken wandb import crashing unsloth startup

When wandb is installed but broken (e.g., wandb < 0.19.11 with
protobuf >= 6.0), the import chain unsloth -> trl -> transformers ->
is_wandb_available() -> import wandb crashes with:

  ImportError: cannot import name 'Imports' from
  'wandb.proto.wandb_telemetry_pb2'

This happens because transformers' is_wandb_available() has no
try/except around `import wandb`. The error propagates up and kills
`from unsloth import FastLanguageModel` even though wandb is optional.

Add disable_broken_wandb() following the same pattern as
disable_torchcodec_if_broken(). It proactively tries importing wandb
during early init, and if the import fails, patches
is_wandb_available() to return False and sets WANDB_DISABLED=true.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Daniel Han 2026-03-03 07:08:12 -08:00 committed by GitHub
commit 52e0358569
2 changed files with 35 additions and 0 deletions

View file

@ -151,6 +151,7 @@ from .import_fixes import (
patch_vllm_for_notebooks,
patch_torchcodec_audio_decoder,
disable_torchcodec_if_broken,
disable_broken_wandb,
)
fix_xformers_performance_issue()
@ -172,6 +173,7 @@ fix_executorch()
patch_vllm_for_notebooks()
patch_torchcodec_audio_decoder()
disable_torchcodec_if_broken()
disable_broken_wandb()
del fix_xformers_performance_issue
del fix_vllm_aimv2_issue
@ -191,6 +193,7 @@ del fix_executorch
del patch_vllm_for_notebooks
del patch_torchcodec_audio_decoder
del disable_torchcodec_if_broken
del disable_broken_wandb
# Torch 2.4 has including_emulation
if DEVICE_TYPE == "cuda":

View file

@ -1290,6 +1290,38 @@ def disable_torchcodec_if_broken():
pass
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.
This function tests if wandb can actually import and if not, patches
transformers' is_wandb_available() to return False.
"""
if importlib.util.find_spec("wandb") is None:
return # wandb not installed, nothing to do
try:
import wandb
except Exception:
# wandb is installed but broken - patch transformers 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"
)
try:
import transformers.integrations.integration_utils as tf_integration
tf_integration.is_wandb_available = lambda: False
except (ImportError, AttributeError):
pass
# Also set env var as fallback for any other code path
os.environ["WANDB_DISABLED"] = "true"
CAUSAL_CONV1D_BROKEN = False
_CAUSAL_CONV1D_PREFIX = "causal_conv1d"
_CAUSAL_CONV1D_BLOCKER_SENTINEL = "_unsloth_causal_conv1d_blocker"