Skip to content

Removal of is_torch_fx_available in v5.0 breaks trust_remote_code models #44561

@janbernloehr

Description

@janbernloehr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions