[FIXED] RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback): initialization failed

Issue

Goal: Run a GPT-2 model instance.

I am using the latest Tensorflow and Hugging Face 🤗 Transformers.

  • Tensorflow – 2.9.1
  • Transformers – 4.21.1

Notebook:

pip install tensorflow
pip install transformers
from transformers import pipeline, set_seed

generator = pipeline('text-generation', model='gpt2')
set_seed(42)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
ImportError: numpy.core.multiarray failed to import

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

SystemError                               Traceback (most recent call last)
SystemError: <built-in method __contains__ of dict object at 0x7f5b58a64d00> returned a result with an error set

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

ImportError                               Traceback (most recent call last)
~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/utils/import_utils.py in _get_module(self, module_name)
   1001         try:
-> 1002             return importlib.import_module("." + module_name, self.__name__)
   1003         except Exception as e:

~/anaconda3/envs/python3/lib/python3.8/importlib/__init__.py in import_module(name, package)
    126             level += 1
--> 127     return _bootstrap._gcd_import(name[level:], package, level)
    128 

~/anaconda3/envs/python3/lib/python3.8/importlib/_bootstrap.py in _gcd_import(name, package, level)

~/anaconda3/envs/python3/lib/python3.8/importlib/_bootstrap.py in _find_and_load(name, import_)

~/anaconda3/envs/python3/lib/python3.8/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

~/anaconda3/envs/python3/lib/python3.8/importlib/_bootstrap.py in _load_unlocked(spec)

~/anaconda3/envs/python3/lib/python3.8/importlib/_bootstrap_external.py in exec_module(self, module)

~/anaconda3/envs/python3/lib/python3.8/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/pipelines/__init__.py in <module>
     36 from ..utils import HUGGINGFACE_CO_RESOLVE_ENDPOINT, http_get, is_tf_available, is_torch_available, logging
---> 37 from .audio_classification import AudioClassificationPipeline
     38 from .automatic_speech_recognition import AutomaticSpeechRecognitionPipeline

~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/pipelines/audio_classification.py in <module>
     19 from ..utils import add_end_docstrings, is_torch_available, logging
---> 20 from .base import PIPELINE_INIT_ARGS, Pipeline
     21 

~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/pipelines/base.py in <module>
     33 from ..feature_extraction_utils import PreTrainedFeatureExtractor
---> 34 from ..modelcard import ModelCard
     35 from ..models.auto.configuration_auto import AutoConfig

~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/modelcard.py in <module>
     43 )
---> 44 from .training_args import ParallelMode
     45 from .utils import (

~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/training_args.py in <module>
     25 from .debug_utils import DebugOption
---> 26 from .trainer_utils import (
     27     EvaluationStrategy,

~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/trainer_utils.py in <module>
     46 if is_tf_available():
---> 47     import tensorflow as tf
     48 

~/anaconda3/envs/python3/lib/python3.8/site-packages/tensorflow/__init__.py in <module>
     36 
---> 37 from tensorflow.python.tools import module_util as _module_util
     38 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader

~/anaconda3/envs/python3/lib/python3.8/site-packages/tensorflow/python/__init__.py in <module>
     36 from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
---> 37 from tensorflow.python.eager import context
     38 

~/anaconda3/envs/python3/lib/python3.8/site-packages/tensorflow/python/eager/context.py in <module>
     34 from tensorflow.python import tf2
---> 35 from tensorflow.python.client import pywrap_tf_session
     36 from tensorflow.python.eager import executor

~/anaconda3/envs/python3/lib/python3.8/site-packages/tensorflow/python/client/pywrap_tf_session.py in <module>
     18 from tensorflow.python import pywrap_tensorflow
---> 19 from tensorflow.python.client._pywrap_tf_session import *
     20 from tensorflow.python.client._pywrap_tf_session import _TF_SetTarget

ImportError: initialization failed

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

RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_4924/2487422996.py in <cell line: 1>()
----> 1 from transformers import pipeline, set_seed
      2 
      3 generator = pipeline('text-generation', model='gpt2')
      4 set_seed(42)

~/anaconda3/envs/python3/lib/python3.8/importlib/_bootstrap.py in _handle_fromlist(module, fromlist, import_, recursive)

~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/utils/import_utils.py in __getattr__(self, name)
    990             value = self._get_module(name)
    991         elif name in self._class_to_module.keys():
--> 992             module = self._get_module(self._class_to_module[name])
    993             value = getattr(module, name)
    994         else:

~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/utils/import_utils.py in _get_module(self, module_name)
   1002             return importlib.import_module("." + module_name, self.__name__)
   1003         except Exception as e:
-> 1004             raise RuntimeError(
   1005                 f"Failed to import {self.__name__}.{module_name} because of the following error (look up to see its"
   1006                 f" traceback):\n{e}"

RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback):
initialization failed
def query(payload, multiple, min_tokens, max_tokens):
    nlp_setup()
    list_dict = generator(payload, min_length=min_tokens, max_new_tokens=max_tokens, num_return_sequences=multiple)
    return [d['generated_text'].split(payload)[1].strip() for d in list_dict
output = query("Banking customer's needs:", 3000, 50, 50)

RunTime, SystemError and ImportError all occur during import of transformers:

RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback): initialization failed

Solution

Changed Kernel: conda_tensorflow2_p38

Answered By – DanielBell99

Answer Checked By – Laura B. (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published