The maximum value of input_ids must be smaller than the embedding layer's input dimension. (TFBartEncoder)
Author: dave-shore
Pull request: no
Labels: name: bug

### System Info

transformers 4.46.0 (tried all >= 4.39)
tokenizers 0.20.1 (tried all >= 0.19)
tensorflow 2.18.0
tf-keras 2.18.0


### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [X] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [X] My own task or dataset (give details below)

### Reproduction

Code:
```
from transformers import AutoTokenizer, TFAutoModel
import tensorflow as tf
from typing import Callable, List

def map_embeddings_to_words(encoding, vectors, reduction_function: Callable = tf.stack):
    """
    Maps subword token embeddings from a Huggingface transformer model onto words (or predefined tokens).

    :param encoding: Encoding from tokenizer's output.
    :param vectors: Embedding from model's output.
    :param reduction_function: Optional, TensorFlow function to reduce subword token embeddings to a single word embedding.
    :return: List[
    """
    reduced_vectors = []
    length = max(i for i in encoding.word_ids if i is not None) + 1

    for i in range(length):
        start, end = encoding.word_to_tokens(i)
        w = reduction_function(vectors[start:end, :])
        reduced_vectors.append(w)

    reduced_vectors = list(map(tf.constant, reduced_vectors))

    return reduced_vectors


def embedding(corpus: List[List[str]], tokenizer, transformer, batch_size: int = 128, reduction: Callable = tf.stack, input_transform: Callable = None, tensors_: str = "tf"):
    """
    Function to embed tokenized corpus into TensorFlow tensors.

    :param corpus: List of tokenized pseudo-sentences.
    :param tokenizer: HuggingFace tokenizer.
    :param transformer: HuggingFace transformer model with encoder module.
    :param batch_size: Batch size.
    :param reduction: Optional, function to reduce subword embeddings to a single word embedding vector.
    :return: List[List[tf.Tensor]]
    """
    final_embeddings = []

    for i in range(0, len(corpus), batch_size):
        batch = corpus[i:i+batch_size]

        encoded_input = tokenizer(batch, is_split_into_words=True, padding=True, return_tensors=tensors_, add_special_tokens = False)

        if hasattr(transformer, "encoder"):
            output = transformer.encoder(**encoded_input)
        else:
            output = transformer(**encoded_input)
        word_embeddings = [map_embeddings_to_words(encoding, vectors, reduction) for encoding, vectors in
                           zip(encoded_input.encodings, output.last_hidden_state)]
        final_embeddings.extend(word_embeddings)
        gc.collect()

    return final_embeddings

if __name__ == "__main__":
    model = "facebook/bart-base"
    tokenizer = AutoTokenizer.from_pretrained(model)
    transformer = TFAutoModel.from_pretrained(model)
    TENSORS_ = "tf"
    REDUCTION = tf.stack
    triples_train = [("a", "p", "c"), ("x", "p", "y"), ("s", "p", "o")]
    X_train = embedding(triples_train, tokenizer, transformer, reduction=REDUCTION, tensors_=TENSORS_)

```

Error:
```
Traceback (most recent call last):
  File "/home/serusr01/lognets/scripts/entity_learning.py", line 85, in <module>
    X_train = embedding(triples_train, tokenizer, transformer, reduction=REDUCTION, tensors_=TENSORS_)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serusr01/lognets/scripts/utils.py", line 159, in embedding
    output = transformer(**encoded_input)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serusr01/lognets/lognets/lib/python3.12/site-packages/tf_keras/src/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/serusr01/lognets/lognets/lib/python3.12/site-packages/transformers/modeling_tf_utils.py", line 437, in run_call_with_unpacked_inputs
    return func(self, **unpacked_inputs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serusr01/lognets/lognets/lib/python3.12/site-packages/transformers/models/bart/modeling_tf_bart.py", line 1311, in call
    outputs = self.model(
              ^^^^^^^^^^^
  File "/home/serusr01/lognets/lognets/lib/python3.12/site-packages/transformers/modeling_tf_utils.py", line 437, in run_call_with_unpacked_inputs
    return func(self, **unpacked_inputs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serusr01/lognets/lognets/lib/python3.12/site-packages/transformers/models/bart/modeling_tf_bart.py", line 1196, in call
    encoder_outputs = self.encoder(
                      ^^^^^^^^^^^^^
  File "/home/serusr01/lognets/lognets/lib/python3.12/site-packages/transformers/modeling_tf_utils.py", line 437, in run_call_with_unpacked_inputs
    return func(self, **unpacked_inputs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serusr01/lognets/lognets/lib/python3.12/site-packages/transformers/models/bart/modeling_tf_bart.py", line 830, in call
    check_embeddings_within_bounds(input_ids, self.embed_tokens.input_dim)
  File "/home/serusr01/lognets/lognets/lib/python3.12/site-packages/transformers/tf_utils.py", line 190, in check_embeddings_within_bounds
    tf.debugging.assert_less(
tensorflow.python.framework.errors_impl.InvalidArgumentError: Exception encountered when calling layer 'encoder' (type TFBartEncoder).

The maximum value of input_ids (50265) must be smaller than the embedding layer's input dimension (50265). The likely cause is some problem at tokenization time.
Condition x < y did not hold.
First 3 elements of x:
[ 3268   102 37314]
First 1 elements of y:
[50265]

Call arguments received by layer 'encoder' (type TFBartEncoder):
  • input_ids=tf.Tensor(shape=(128, 16), dtype=int32)
  • inputs_embeds=None
  • attention_mask=tf.Tensor(shape=(128, 16), dtype=int32)
  • head_mask=None
  • output_attentions=False
  • output_hidden_states=False
  • return_dict=True
  • training=False
```

### Expected behavior

The code is expected to return a list of lists of 3 tf tensors.

Comments:
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](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.