fastnlp / CPT

CPT: A Pre-Trained Unbalanced Transformer for Both Chinese Language Understanding and Generation
481 stars 70 forks source link

run_gen.py shows error: module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function' #83

Closed shivanraptor closed 3 weeks ago

shivanraptor commented 3 weeks ago

I tried to execute finetune/generation/run_gen.py, even without parameters, it shows a 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'

Here is the output:

$python run_gen.py 
2024-09-12 16:23:32.959343: 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:23:32.979813: 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:23:32.986078: 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:23:33.000725: 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:23:33.841453: 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 "/home/jupyter-raptor/finetune/generation/run_gen.py", line 13, in <module>
    from transformers import (AutoConfig, AutoModel, BertTokenizer,BertForTokenClassification,
  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.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'

It seems that it is a version problem.

Key versions:

shivanraptor commented 3 weeks ago

The problem can be narrowed down to:

from transformers import trainer_seq2seq

which will lead to the same error.

shivanraptor commented 3 weeks ago

This issue is resolved by downgrading transformers to 4.28.1 and tokenizers to 0.13.3 on Python 3.10 However, in Python 3.12, the import error does not exist with the same versions listed in the first thread.