Skip to content

ValueError: Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' 'truncation=True' to have batched tensors with the same length. #15505

Description

@anum94

Maybe @SaulLu can help?

Information

I am following the text summarization tutorial on hugging face website which uses the mt5-small model. It explains step by step on how to perform a text summarization task.

To reproduce

Steps to reproduce the behavior:

  1. Run the following notebook
  2. cell # 32 should reproduce the following error. (it did for me)
ValueError                                Traceback (most recent call last)
File ~/PycharmProjects/nlp-env/lib/python3.9/site-packages/transformers/tokenization_utils_base.py:707, in BatchEncoding.convert_to_tensors(self, tensor_type, prepend_batch_axis)
    706 if not is_tensor(value):
--> 707     tensor = as_tensor(value)
    709     # Removing this for now in favor of controlling the shape with `prepend_batch_axis`
    710     # # at-least2d
    711     # if tensor.ndim > 2:
    712     #     tensor = tensor.squeeze(0)
    713     # elif tensor.ndim < 2:
    714     #     tensor = tensor[None, :]

ValueError: too many dimensions 'str'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
Input In [72], in <module>
----> 1 data_collator(features)

File ~/PycharmProjects/nlp-env/lib/python3.9/site-packages/transformers/data/data_collator.py:586, in DataCollatorForSeq2Seq.__call__(self, features, return_tensors)
    583         else:
    584             feature["labels"] = np.concatenate([remainder, feature["labels"]]).astype(np.int64)
--> 586 features = self.tokenizer.pad(
    587     features,
    588     padding=self.padding,
    589     max_length=self.max_length,
    590     pad_to_multiple_of=self.pad_to_multiple_of,
    591     return_tensors=return_tensors,
    592 )
    594 # prepare decoder_input_ids
    595 if (
    596     labels is not None
    597     and self.model is not None
    598     and hasattr(self.model, "prepare_decoder_input_ids_from_labels")
    599 ):

File ~/PycharmProjects/nlp-env/lib/python3.9/site-packages/transformers/tokenization_utils_base.py:2842, in PreTrainedTokenizerBase.pad(self, encoded_inputs, padding, max_length, pad_to_multiple_of, return_attention_mask, return_tensors, verbose)
   2839             batch_outputs[key] = []
   2840         batch_outputs[key].append(value)
-> 2842 return BatchEncoding(batch_outputs, tensor_type=return_tensors)

File ~/PycharmProjects/nlp-env/lib/python3.9/site-packages/transformers/tokenization_utils_base.py:212, in BatchEncoding.__init__(self, data, encoding, tensor_type, prepend_batch_axis, n_sequences)
    208     n_sequences = encoding[0].n_sequences
    210 self._n_sequences = n_sequences
--> 212 self.convert_to_tensors(tensor_type=tensor_type, prepend_batch_axis=prepend_batch_axis)

File ~/PycharmProjects/nlp-env/lib/python3.9/site-packages/transformers/tokenization_utils_base.py:723, in BatchEncoding.convert_to_tensors(self, tensor_type, prepend_batch_axis)
    718         if key == "overflowing_tokens":
    719             raise ValueError(
    720                 "Unable to create tensor returning overflowing tokens of different lengths. "
    721                 "Please see if a fast version of this tokenizer is available to have this feature available."
    722             )
--> 723         raise ValueError(
    724             "Unable to create tensor, you should probably activate truncation and/or padding "
    725             "with 'padding=True' 'truncation=True' to have batched tensors with the same length."
    726         )
    728 return self

ValueError: Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' 'truncation=True' to have batched tensors with the same length.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions