huggingface / transformers

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
https://huggingface.co/transformers
Apache License 2.0
134.22k stars 26.84k forks source link

'tuple' object doesn't have attribute `as_list` #14346

Closed ndgnuh closed 2 years ago

ndgnuh commented 2 years ago

Environment info

- `transformers` version: 4.12.3
- Platform: Linux-5.10.0-9-amd64-x86_64-with-glibc2.2.5
- Python version: 3.8.12
- PyTorch version (GPU?): not installed (NA)
- Tensorflow version (GPU?): 2.6.2 (False)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: no
- Using distributed or parallel set-up in script?: no

Who can help

Information

I'm sorry for not being able to give more information about this, since I don't directly works with the model. I believe the model is bert-base-multilingual-cased. I'm trying to create a chat bot with Rasa.

To reproduce

Steps to reproduce the behavior: Run the chat bot, either use rasa shell or rasa run --enable-api and curl to chat with the bot.

Error log:

2021-11-09 08:08:34 ERROR    rasa.core.channels.rest  - An exception occured while handling user message 'hello'.
Traceback (most recent call last):
  File "/home/grooo/.local/lib/python3.8/site-packages/rasa/core/channels/rest.py", line 120, in receive
    await on_new_message(
  File "/home/grooo/.local/lib/python3.8/site-packages/rasa/core/channels/channel.py", line 89, in handler
    await app.agent.handle_message(message)
  File "/home/grooo/.local/lib/python3.8/site-packages/rasa/core/agent.py", line 577, in handle_message
    return await processor.handle_message(message)
  File "/home/grooo/.local/lib/python3.8/site-packages/rasa/core/processor.py", line 96, in handle_message
    tracker = await self.log_message(message, should_save_tracker=False)
  File "/home/grooo/.local/lib/python3.8/site-packages/rasa/core/processor.py", line 327, in log_message
    await self._handle_message_with_tracker(message, tracker)
  File "/home/grooo/.local/lib/python3.8/site-packages/rasa/core/processor.py", line 594, in _handle_message_with_tracker
    parse_data = await self.parse_message(message, tracker)
  File "/home/grooo/.local/lib/python3.8/site-packages/rasa/core/processor.py", line 572, in parse_message
    parse_data = await self.interpreter.parse(
  File "/home/grooo/.local/lib/python3.8/site-packages/rasa/core/interpreter.py", line 145, in parse
    result = self.interpreter.parse(text)
  File "/home/grooo/.local/lib/python3.8/site-packages/rasa/nlu/model.py", line 470, in parse
    component.process(message, **self.context)
  File "/home/grooo/.local/lib/python3.8/site-packages/rasa/nlu/utils/hugging_face/hf_transformers.py", line 749, in process
    self._get_docs_for_batch(
  File "/home/grooo/.local/lib/python3.8/site-packages/rasa/nlu/utils/hugging_face/hf_transformers.py", line 678, in _get_docs_for_batch
    ) = self._get_model_features_for_batch(
  File "/home/grooo/.local/lib/python3.8/site-packages/rasa/nlu/utils/hugging_face/hf_transformers.py", line 609, in _get_model_features_for_batch
    sequence_hidden_states = self._compute_batch_sequence_features(
  File "/home/grooo/.local/lib/python3.8/site-packages/rasa/nlu/utils/hugging_face/hf_transformers.py", line 460, in _compute_batch_sequence_features
    model_outputs = self.model(
  File "/home/grooo/.local/lib/python3.8/site-packages/keras/engine/base_layer.py", line 1037, in __call__
    outputs = call_fn(inputs, *args, **kwargs)
  File "/home/grooo/.local/lib/python3.8/site-packages/transformers/models/bert/modeling_tf_bert.py", line 1129, in call
    outputs = self.bert(
  File "/home/grooo/.local/lib/python3.8/site-packages/keras/engine/base_layer.py", line 1037, in __call__
    outputs = call_fn(inputs, *args, **kwargs)
  File "/home/grooo/.local/lib/python3.8/site-packages/transformers/models/bert/modeling_tf_bert.py", line 803, in call
    attention_mask_shape = shape_list(inputs["attention_mask"])
  File "/home/grooo/.local/lib/python3.8/site-packages/transformers/modeling_tf_utils.py", line 1831, in shape_list
    static = tensor.shape.as_list()
AttributeError: 'tuple' object has no attribute 'as_list'

Line 1831 of transformers/modeling_tf_utils.py:

static = tensor.shape.as_list()

After printing out stuff in transformers/modeling_tf_utils.py, I found out that sometime tensor is a numpy array, therefore tensor.shape is a tuple and indeed doesn't have as_list.

Proposed fix:

    static = tensor.shape
    if type(static) == tuple:
        static = list(static)
    else:
        static = static.as_list()

Expected behavior

No error.

juan-park commented 2 years ago

I also encountered this error when upgrading Transformers from version 3.5.1 --> 4.12.2. Can confirm @ndgnuh's proposed fix works!

Can this fix be incorporated into the bug fixes?

LysandreJik commented 2 years ago

Nice catch, do you want to open a PR with the fix?

ndgnuh commented 2 years ago

Sorry but I have a potato computer and I'm too lazy for the full PR procedure :smile:

LysandreJik commented 2 years ago

cc @Rocketknight1

Rocketknight1 commented 2 years ago

This looks like the same issue as #14404, it definitely needs a fix

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.