transformers - ✅(Solved) Fix Removal of `is_torch_fx_available` in v5.0 breaks `trust_remote_code` models [1 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#44561Fetched 2026-04-08 00:27:38
View on GitHub
Comments
1
Participants
2
Timeline
8
Reactions
0
Timeline (top)
mentioned ×2subscribed ×2closed ×1commented ×1

Error Message

File ".../transformers/dynamic_module_utils.py", line 309, in get_class_in_module ... File ".../modules/transformers_modules/deepseek.../modeling_deepseek.py", line 50 from transformers.utils.import_utils import is_torch_fx_available ImportError: cannot import name 'is_torch_fx_available' from 'transformers.utils.import_utils'

Root Cause

Since the function was removed because PyTorch >= 2.1 is now the minimum (where torch.fx is always available), returning True is correct. This would unbreak all trust_remote_code models that reference this symbol without any behavioral change.

Fix Action

Fix / Workaround

Since these models ship their own modeling_*.py on the Hub and are loaded dynamically, they break at import time with no way for users to fix it (short of monkey-patching or forking the model code).

PR fix notes

PR #37234: Remove old code for PyTorch, Accelerator and tokenizers

Description (problem / solution / changelog)

What does this PR do?

Remove outdated conditions and comments for PyTorch and Accelerator.

Specifically, some code for PyTorch < 2.1 has been found and removed. As a result, the functions is_torch_bf16_cpu_available,is_torch_fx_available, is_torchdynamo_available and is_torch_compile_available are now equivalent to is_torch_available. Some tests are further simplified using this fact.

There is one change regarding to old Accelerator code. And old tokenizers version check is removed.

Changed files

  • src/transformers/activations.py (modified, +1/-13)
  • src/transformers/modeling_utils.py (modified, +0/-1)
  • src/transformers/models/blip/modeling_blip_text.py (modified, +0/-1)
  • src/transformers/models/code_llama/tokenization_code_llama_fast.py (modified, +0/-3)
  • src/transformers/models/cohere/tokenization_cohere_fast.py (modified, +0/-3)
  • src/transformers/models/gemma/tokenization_gemma_fast.py (modified, +0/-3)
  • src/transformers/models/gpt_neo/modeling_gpt_neo.py (modified, +1/-3)
  • src/transformers/models/llama/tokenization_llama_fast.py (modified, +0/-3)
  • src/transformers/models/phimoe/modeling_phimoe.py (modified, +1/-3)
  • src/transformers/models/vilt/modeling_vilt.py (modified, +1/-1)
  • src/transformers/optimization.py (modified, +0/-2)
  • src/transformers/testing_utils.py (modified, +0/-9)
  • src/transformers/trainer.py (modified, +3/-8)
  • src/transformers/training_args.py (modified, +1/-3)
  • src/transformers/utils/fx.py (modified, +0/-9)
  • src/transformers/utils/import_utils.py (modified, +11/-42)
  • tests/fsdp/test_fsdp.py (modified, +0/-1)
  • tests/models/bert/test_modeling_bert.py (modified, +0/-1)
  • tests/models/bert_generation/test_modeling_bert_generation.py (modified, +0/-1)
  • tests/models/big_bird/test_modeling_big_bird.py (modified, +0/-1)
  • tests/models/chinese_clip/test_modeling_chinese_clip.py (modified, +0/-1)
  • tests/models/data2vec/test_modeling_data2vec_text.py (modified, +0/-1)
  • tests/models/ernie/test_modeling_ernie.py (modified, +0/-1)
  • tests/models/gpt_neox/test_modeling_gpt_neox.py (modified, +0/-1)
  • tests/models/gpt_neox_japanese/test_modeling_gpt_neox_japanese.py (modified, +0/-1)
  • tests/models/hubert/test_modeling_hubert.py (modified, +3/-5)
  • tests/models/mt5/test_modeling_mt5.py (modified, +3/-6)
  • tests/models/rembert/test_modeling_rembert.py (modified, +0/-1)
  • tests/models/roberta/test_modeling_roberta.py (modified, +0/-1)
  • tests/models/roberta_prelayernorm/test_modeling_roberta_prelayernorm.py (modified, +0/-1)
  • tests/models/roc_bert/test_modeling_roc_bert.py (modified, +0/-1)
  • tests/models/roformer/test_modeling_roformer.py (modified, +0/-1)
  • tests/models/t5/test_modeling_t5.py (modified, +4/-7)
  • tests/models/umt5/test_modeling_umt5.py (modified, +3/-7)
  • tests/models/wav2vec2/test_modeling_wav2vec2.py (modified, +3/-5)
  • tests/models/xlm_roberta_xl/test_modeling_xlm_roberta_xl.py (modified, +0/-1)
  • tests/models/xmod/test_modeling_xmod.py (modified, +0/-1)
  • tests/test_modeling_common.py (modified, +4/-7)
  • tests/trainer/test_trainer.py (modified, +13/-10)
  • tests/trainer/test_trainer_fsdp.py (modified, +0/-4)
  • tests/utils/test_modeling_utils.py (modified, +0/-3)
  • tests/utils/test_versions_utils.py (modified, +1/-1)

Code Example

from transformers import AutoModelForCausalLM

model = AutoModelForCausalLM.from_pretrained(
    "deepseek-ai/deepseek-moe-16b-base",
    trust_remote_code=True,
)

---

File ".../transformers/dynamic_module_utils.py", line 309, in get_class_in_module
  ...
File ".../modules/transformers_modules/deepseek.../modeling_deepseek.py", line 50
    from transformers.utils.import_utils import is_torch_fx_available
ImportError: cannot import name 'is_torch_fx_available' from 'transformers.utils.import_utils'

---

from transformers.utils.import_utils import is_torch_fx_available

---

def is_torch_fx_available():
    """Deprecated: kept for backwards compatibility with trust_remote_code models."""
    return True
RAW_BUFFERClick to expand / collapse

System Info

  • transformers version: 5.0.0
  • PyTorch version: 2.10.0
  • Python version: 3.12

Who can help?

@ArthurZucker @Rocketknight1

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder
  • My own task or dataset (give details below)

Reproduction

Several HuggingFace Hub models that use trust_remote_code=True import is_torch_fx_available from transformers.utils.import_utils. This function was removed in #37234 ("Remove old code for PyTorch, Accelerator and tokenizers"), which landed in transformers 5.0.0.

Since these models ship their own modeling_*.py on the Hub and are loaded dynamically, they break at import time with no way for users to fix it (short of monkey-patching or forking the model code).

Example — deepseek-ai/deepseek-moe-16b-base:

from transformers import AutoModelForCausalLM

model = AutoModelForCausalLM.from_pretrained(
    "deepseek-ai/deepseek-moe-16b-base",
    trust_remote_code=True,
)

Traceback:

File ".../transformers/dynamic_module_utils.py", line 309, in get_class_in_module
  ...
File ".../modules/transformers_modules/deepseek.../modeling_deepseek.py", line 50
    from transformers.utils.import_utils import is_torch_fx_available
ImportError: cannot import name 'is_torch_fx_available' from 'transformers.utils.import_utils'

The offending line in the Hub model code (modeling_deepseek.py line 50):

from transformers.utils.import_utils import is_torch_fx_available

This pattern was common in transformers v4.x and was used in many community models that copied from the official codebase at the time. The function was used to conditionally torch.fx.wrap() attention mask helpers.

Affected models

At minimum:

  • deepseek-ai/deepseek-moe-16b-base
  • deepseek-ai/deepseek-moe-16b-chat
  • Likely many other older community models that followed the same pattern

Suggested fix

Add a backwards-compatibility shim in transformers/utils/import_utils.py:

def is_torch_fx_available():
    """Deprecated: kept for backwards compatibility with trust_remote_code models."""
    return True

Since the function was removed because PyTorch >= 2.1 is now the minimum (where torch.fx is always available), returning True is correct. This would unbreak all trust_remote_code models that reference this symbol without any behavioral change.

Alternatively, a DeprecationWarning could be emitted to nudge model authors to update their code.

Expected behavior

Loading models with trust_remote_code=True should not break due to internal API removals in transformers, especially for widely-used symbols. A compatibility shim (returning True) would maintain backwards compatibility at zero cost.

extent analysis

Fix Plan

Add Backwards-Compatibility Shim

To fix the issue, we need to add a backwards-compatibility shim in transformers/utils/import_utils.py. Here are the steps:

  1. Create a new file backwards_compat.py in the transformers/utils directory:
# transformers/utils/backwards_compat.py
def is_torch_fx_available():
    """Deprecated: kept for backwards compatibility with trust_remote_code models."""
    return True
  1. Import the shim in transformers/utils/import_utils.py:
# transformers/utils/import_utils.py
import backwards_compat

# ... (rest of the file remains the same)
  1. Update the import in the affected models to use the shim:
# deepseek-ai/deepseek-moe-16b-base/modeling_deepseek.py
from transformers.utils.backwards_compat import is_torch_fx_available

Alternatively, you can also emit a DeprecationWarning to nudge model authors to update their code:

# transformers/utils/backwards_compat.py
import warnings

def is_torch_fx_available():
    """Deprecated: kept for backwards compatibility with trust_remote_code models."""
    warnings.warn("is_torch_fx_available is deprecated and will be removed in future versions.", DeprecationWarning)
    return True

Verify the Fix

To verify that the fix worked, you can try loading the affected models with trust_remote_code=True and check that they load without errors:

from transformers import AutoModelForCausalLM

model = AutoModelForCausalLM.from_pretrained(
    "deepseek-ai/deepseek-moe-16b-base",
    trust_remote_code=True,
)

If the model loads without errors, the fix is successful.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

Loading models with trust_remote_code=True should not break due to internal API removals in transformers, especially for widely-used symbols. A compatibility shim (returning True) would maintain backwards compatibility at zero cost.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING