You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

导入transformers的trainer_seq2seq报错,BertTokenizer正常,求排查

问题:导入transformers的trainer_seq2seq模块失败

执行代码from transformers import trainer_seq2seq时出现报错,但from transformers import BertTokenizer可正常加载。

报错日志

2024-09-12 16:47:25.651645: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-09-12 16:47:25.672144: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-09-12 16:47:25.678423: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-09-12 16:47:25.693224: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-09-12 16:47:26.503496: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
Traceback (most recent call last):
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 1603, in _get_module
    return importlib.import_module("." + module_name, self.__name__)
  File "/opt/tljh/user/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/transformers/modeling_tf_utils.py", line 38, in <module>
    from .activations_tf import get_tf_activation
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/transformers/activations_tf.py", line 22, in <module>
    import tf_keras as keras
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/tf_keras/__init__.py", line 3, in <module>
    from tf_keras import __internal__
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/tf_keras/__internal__/__init__.py", line 3, in <module>
    from tf_keras.__internal__ import backend
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/tf_keras/__internal__/backend/__init__.py", line 3, in <module>
    from tf_keras.src.backend import _initialize_variables as initialize_variables
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/tf_keras/src/__init__.py", line 21, in <module>
    from tf_keras.src import applications
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/tf_keras/src/applications/__init__.py", line 18, in <module>
    from tf_keras.src.applications.convnext import ConvNeXtBase
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/tf_keras/src/applications/convnext.py", line 33, in <module>
    from tf_keras.src.engine import sequential
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/tf_keras/src/engine/sequential.py", line 24, in <module>
    from tf_keras.src.engine import functional
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/tf_keras/src/engine/functional.py", line 33, in <module>
    from tf_keras.src.engine import training as training_lib
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/tf_keras/src/engine/training.py", line 48, in <module>
    from tf_keras.src.saving import saving_api
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/tf_keras/src/saving/saving_api.py", line 25, in <module>
    from tf_keras.src.saving.legacy import save as legacy_sm_saving_lib
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/tf_keras/src/saving/legacy/save.py", line 27, in <module>
    from tf_keras.src.saving.legacy.saved_model import load_context
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/tf_keras/src/saving/legacy/saved_model/load_context.py", line 68, in <module>
    tf.__internal__.register_load_context_function(in_load_context)
AttributeError: module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function'. Did you mean: 'register_call_context_function'?

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 1603, in _get_module
    return importlib.import_module("." + module_name, self.__name__)
  File "/opt/tljh/user/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/transformers/integrations/integration_utils.py", line 36, in <module>
    from .. import PreTrainedModel, TFPreTrainedModel
  File "<frozen importlib._bootstrap>", line 1075, in _handle_fromlist
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 1593, in __getattr__
    module = self._get_module(self._class_to_module[name])
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 1605, in _get_module
    raise RuntimeError(
RuntimeError: Failed to import transformers.modeling_tf_utils because of the following error (look up to see its traceback):
module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 1603, in _get_module
    return importlib.import_module("." + module_name, self.__name__)
  File "/opt/tljh/user/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/transformers/trainer_seq2seq.py", line 26, in <module>
    from .trainer import Trainer
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/transformers/trainer.py", line 42, in <module>
    from .integrations import (
  File "<frozen importlib._bootstrap>", line 1075, in _handle_fromlist
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 1593, in __getattr__
    module = self._get_module(self._class_to_module[name])
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 1605, in _get_module
    raise RuntimeError(
RuntimeError: Failed to import transformers.integrations.integration_utils because of the following error (look up to see its traceback):
Failed to import transformers.modeling_tf_utils because of the following error (look up to see its traceback):
module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1075, in _handle_fromlist
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 1591, in __getattr__
    value = self._get_module(name)
  File "/home/jupyter-raptor/.local/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 1605, in _get_module
    raise RuntimeError(
RuntimeError: Failed to import transformers.trainer_seq2seq because of the following error (look up to see its traceback):
Failed to import transformers.integrations.integration_utils because of the following error (look up to see its traceback):
Failed to import transformers.modeling_tf_utils because of the following error (look up to see its traceback):
module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function'

环境版本

  • transformers 4.44.2
  • keras 3.5.0
  • tensorflow 2.17.0
  • Python 3.10.10

解决方案

核心问题是独立安装的tf_keras与TensorFlow 2.17.0版本不兼容,tf_keras调用了TensorFlow内部不存在的register_load_context_function接口,可通过以下方式修复:

方案1:匹配tf_keras与TensorFlow版本

卸载当前的tf_keras,安装与TensorFlow主版本号一致的tf_keras:

pip uninstall -y tf_keras
pip install tf_keras==2.17.0

方案2:使用TensorFlow内置的Keras

如果不需要独立的tf_keras包,直接卸载它,transformers会自动切换到TensorFlow自带的Keras:

pip uninstall -y tf_keras

验证修复

执行以下代码确认问题解决:

from transformers import trainer_seq2seq

内容的提问来源于stack exchange,提问作者Raptor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 12:34:49