OSError: Exception encountered when calling layer "encoder" (type TFBertEncoder)

Viewed 12
from transformers import AutoTokenizer, TFBertModel
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
model = TFBertModel.from_pretrained("bert-base-uncased")

This is the code i've used to import the bert model for tensorflow in jupyter notebook. I've installed all the required packages and libraries such as pytorch, tensorflow, etc in my virtual conda environment, which i've used to run this code. At first it threw an error and asked to turn on developer mode in windows settings, and after doing it again it is throwing a different error. Since i'm a beginner and not very well-versed with bert, can anyone please help me?

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Input In [6], in <cell line: 3>()
  1 from transformers import AutoTokenizer, TFBertModel
  2 tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
----> 3 model = TFBertModel.from_pretrained("bert-base-uncased")

File ~\.conda\envs\mlenv\lib\site-packages\transformers\modeling_tf_utils.py:2464, in 

TFPreTrainedModel.from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
   2462         model(model.dummy_inputs)  # build the network with dummy inputs
   2463 else:
-> 2464     model(model.dummy_inputs)  # build the network with dummy inputs
   2466 # 'by_name' allow us to do transfer learning by skipping/adding layers
   2467 # see https://github.com/tensorflow/tensorflow/blob/00fad90125b18b80fe054de1055770cfb8fe4ba3/tensorflow/python/keras/engine/network.py#L1339-L1357
   2468 try:

File ~\.conda\envs\mlenv\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     65 except Exception as e:  # pylint: disable=broad-except
     66   filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67   raise e.with_traceback(filtered_tb) from None
     68 finally:
     69   del filtered_tb

File ~\.conda\envs\mlenv\lib\site-packages\transformers\modeling_tf_utils.py:409, in unpack_inputs.<locals>.run_call_with_unpacked_inputs(self, *args, **kwargs)
    406     config = self.config
    408 unpacked_inputs = input_processing(func, config, **fn_args_and_kwargs)
--> 409 return func(self, **unpacked_inputs)

File ~\.conda\envs\mlenv\lib\site-packages\transformers\models\bert\modeling_tf_bert.py:1108, in TFBertModel.call(self, input_ids, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, encoder_hidden_states, encoder_attention_mask, past_key_values, use_cache, output_attentions, output_hidden_states, return_dict, training)
   1063 @unpack_inputs
   1064 @add_start_docstrings_to_model_forward(BERT_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
   1065 @add_code_sample_docstrings(
   (...)
   1086     training: Optional[bool] = False,
   1087 ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, Tuple[tf.Tensor]]:
   1088     r"""
   1089     encoder_hidden_states  (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
   1090         Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if
   (...)
   1106         `past_key_values`). Set to `False` during training, `True` during generation
   1107     """
-> 1108     outputs = self.bert(
   1109         input_ids=input_ids,
   1110         attention_mask=attention_mask,
   1111         token_type_ids=token_type_ids,
   1112         position_ids=position_ids,
   1113         head_mask=head_mask,
   1114         inputs_embeds=inputs_embeds,
   1115         encoder_hidden_states=encoder_hidden_states,
   1116         encoder_attention_mask=encoder_attention_mask,
   1117         past_key_values=past_key_values,
   1118         use_cache=use_cache,
   1119         output_attentions=output_attentions,
   1120         output_hidden_states=output_hidden_states,
   1121         return_dict=return_dict,
   1122         training=training,
   1123     )
   1124     return outputs

File ~\.conda\envs\mlenv\lib\site-packages\transformers\modeling_tf_utils.py:409, in unpack_inputs.<locals>.run_call_with_unpacked_inputs(self, *args, **kwargs)
    406     config = self.config
    408 unpacked_inputs = input_processing(func, config, **fn_args_and_kwargs)
--> 409 return func(self, **unpacked_inputs)

File ~\.conda\envs\mlenv\lib\site-packages\transformers\models\bert\modeling_tf_bert.py:863, in TFBertMainLayer.call(self, input_ids, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, encoder_hidden_states, encoder_attention_mask, past_key_values, use_cache, output_attentions, output_hidden_states, return_dict, training)
    860 else:
    861     head_mask = [None] * self.config.num_hidden_layers
--> 863 encoder_outputs = self.encoder(
    864     hidden_states=embedding_output,
    865     attention_mask=extended_attention_mask,
    866     head_mask=head_mask,
    867     encoder_hidden_states=encoder_hidden_states,
    868     encoder_attention_mask=encoder_extended_attention_mask,
    869     past_key_values=past_key_values,
    870     use_cache=use_cache,
    871     output_attentions=output_attentions,
    872     output_hidden_states=output_hidden_states,
    873     return_dict=return_dict,
    874     training=training,
    875 )
    877 sequence_output = encoder_outputs[0]
    878 pooled_output = self.pooler(hidden_states=sequence_output) if self.pooler is not None else None

File ~\.conda\envs\mlenv\lib\site-packages\transformers\models\bert\modeling_tf_bert.py:583, in TFBertEncoder.call(self, hidden_states, attention_mask, head_mask, encoder_hidden_states, encoder_attention_mask, past_key_values, use_cache, output_attentions, output_hidden_states, return_dict, training)
    578 if not return_dict:
    579     return tuple(
    580         v for v in [hidden_states, all_hidden_states, all_attentions, all_cross_attentions] if v is not None
    581     )
--> 583 return TFBaseModelOutputWithPastAndCrossAttentions(
    584     last_hidden_state=hidden_states,
    585     past_key_values=next_decoder_cache,
    586     hidden_states=all_hidden_states,
    587     attentions=all_attentions,
    588     cross_attentions=all_cross_attentions,
    589 )

File <string>:8, in __init__(self, last_hidden_state, past_key_values, hidden_states, attentions, cross_attentions)

File ~\.conda\envs\mlenv\lib\site-packages\transformers\utils\generic.py:174, in ModelOutput.__post_init__(self)
    171 first_field = getattr(self, class_fields[0].name)
    172 other_fields_are_none = all(getattr(self, field.name) is None for field in class_fields[1:])
--> 174 if other_fields_are_none and not is_tensor(first_field):
    175     if isinstance(first_field, dict):
    176         iterator = first_field.items()

File ~\.conda\envs\mlenv\lib\site-packages\transformers\utils\generic.py:59, in is_tensor(x)
     55 def is_tensor(x):
     56     """
     57     Tests if `x` is a `torch.Tensor`, `tf.Tensor`, `jaxlib.xla_extension.DeviceArray` or `np.ndarray`.
     58     """
---> 59     if is_torch_fx_proxy(x):
     60         return True
     61     if is_torch_available():

File ~\.conda\envs\mlenv\lib\site-packages\transformers\utils\import_utils.py:976, in is_torch_fx_proxy(x)
    974 def is_torch_fx_proxy(x):
    975     if is_torch_fx_available():
--> 976         import torch.fx
    978         return isinstance(x, torch.fx.Proxy)
    979     return False

File ~\.conda\envs\mlenv\lib\site-packages\torch\__init__.py:124, in <module>
    122     err = ctypes.WinError(last_error)
    123     err.strerror += f' Error loading "{dll}" or one of its dependencies.'
--> 124     raise err
    125 elif res is not None:
    126     is_loaded = True

OSError: Exception encountered when calling layer "encoder" (type TFBertEncoder).

22

Call arguments received by layer "encoder" (type TFBertEncoder):
  • hidden_states=tf.Tensor(shape=(3, 5, 768), dtype=float32)
  • attention_mask=tf.Tensor(shape=(3, 1, 1, 5), dtype=float32)
  • head_mask=['None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None']
  • encoder_hidden_states=None
  • encoder_attention_mask=None
  • past_key_values=['None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None']
  • use_cache=False
  • output_attentions=False
  • output_hidden_states=False
  • return_dict=True
  • training=False

This is the error.

0 Answers
Related