# Buggy code
def _add_padding_to_batch(
    self, batch_token_ids: List[List[int]]
) -> Tuple[List[int], List[List[int]]]:
    """Add padding so that all examples in the batch are of the same length.

    Args:
        batch_token_ids: Batch of examples where each example is a non-padded list
        of token ids.

    Returns:
        Padded batch with all examples of the same length.
    """
    padded_token_ids = []
    # Compute max length across examples
    max_seq_len = 0
    actual_sequence_lengths = []

    for example_token_ids in batch_token_ids:
        actual_sequence_lengths.append(len(example_token_ids))
        max_seq_len = max(max_seq_len, len(example_token_ids))

    # Add padding according to max_seq_len
    # Some models don't contain pad token, we use unknown token as padding token.
    # This doesn't affect the computation since we compute an attention mask
    # anyways.
    for example_token_ids in batch_token_ids:
        padded_token_ids.append(
            example_token_ids
            + [self.pad_token_id] * (max_seq_len - len(example_token_ids))
        )
    return actual_sequence_lengths, padded_token_ids


# Traceback (tensorflow.python.framework.errors_impl.InvalidArgumentError)
2020-07-16 13:05:16 INFO     rasa.nlu.model  - Starting to train component HFTransformersNLP
Traceback (most recent call last):
File "/home/ubuntu/anaconda3/bin/rasa", line 8, in <module>
sys.exit(main())
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/rasa/__main__.py", line 92, in main
cmdline_arguments.func(cmdline_arguments)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/rasa/cli/train.py", line 140, in train_nlu
persist_nlu_training_data=args.persist_nlu_data,
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/rasa/train.py", line 414, in train_nlu
persist_nlu_training_data,
File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/rasa/train.py", line 453, in _train_nlu_async
persist_nlu_training_data=persist_nlu_training_data,
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/rasa/train.py", line 482, in _train_nlu_with_validated_data
persist_nlu_training_data=persist_nlu_training_data,
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/rasa/nlu/train.py", line 90, in train
interpreter = trainer.train(training_data, **kwargs)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/rasa/nlu/model.py", line 191, in train
updates = component.train(working_data, self.config, **context)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/rasa/nlu/utils/hugging_face/hf_transformers.py", line 472, in train
batch_docs = self._get_docs_for_batch(batch_messages, attribute)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/rasa/nlu/utils/hugging_face/hf_transformers.py", line 423, in _get_docs_for_batch
) = self._get_model_features_for_batch(batch_token_ids)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/rasa/nlu/utils/hugging_face/hf_transformers.py", line 387, in _get_model_features_for_batch
batch_attention_mask, padded_token_ids
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/rasa/nlu/utils/hugging_face/hf_transformers.py", line 347, in _compute_batch_sequence_features
np.array(padded_token_ids), attention_mask=np.array(batch_attention_mask)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 822, in __call__
outputs = self.call(cast_inputs, *args, **kwargs)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/transformers/modeling_tf_bert.py", line 708, in call
outputs = self.bert(inputs, **kwargs)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 822, in __call__
outputs = self.call(cast_inputs, *args, **kwargs)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/transformers/modeling_tf_bert.py", line 570, in call
embedding_output = self.embeddings([input_ids, position_ids, token_type_ids, inputs_embeds], training=training)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 822, in __call__
outputs = self.call(cast_inputs, *args, **kwargs)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/transformers/modeling_tf_bert.py", line 150, in call
return self._embedding(inputs, training=training)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/transformers/modeling_tf_bert.py", line 173, in _embedding
position_embeddings = self.position_embeddings(position_ids)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 822, in __call__
outputs = self.call(cast_inputs, *args, **kwargs)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/embeddings.py", line 184, in call
out = embedding_ops.embedding_lookup(self.embeddings, inputs)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/embedding_ops.py", line 323, in embedding_lookup
transform_fn=None)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/embedding_ops.py", line 137, in _embedding_lookup_and_transform
array_ops.gather(params[0], ids, name=name), ids, max_norm)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/util/dispatch.py", line 180, in wrapper
return target(*args, **kwargs)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/array_ops.py", line 4106, in gather
return params.sparse_read(indices, name=name)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py", line 645, in sparse_read
self._handle, indices, dtype=self._dtype, name=name)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_resource_variable_ops.py", line 556, in resource_gather
_ops.raise_from_not_ok_status(e, name)
File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 6606, in raise_from_not_ok_status
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: indices[0,512] = 512 is not in [0, 512) [Op:ResourceGather] name: tf_bert_model/bert/embeddings/position_embeddings/embedding_lookup/

# Fixed code
def _add_padding_to_batch(
    self, batch_token_ids: List[List[int]], max_sequence_length_model: int
) -> List[List[int]]:
    """Add padding so that all examples in the batch are of the same length.

    Args:
        batch_token_ids: Batch of examples where each example is a non-padded list
        of token ids.
        max_sequence_length_model: Maximum length of any input sequence in the batch
        to be fed to the model.

    Returns:
        Padded batch with all examples of the same length.
    """
    padded_token_ids = []

    # Add padding according to max_sequence_length
    # Some models don't contain pad token, we use unknown token as padding token.
    # This doesn't affect the computation since we compute an attention mask
    # anyways.
    for example_token_ids in batch_token_ids:
        # Truncate any longer sequences so that they can be fed to the model
        if len(example_token_ids) > max_sequence_length_model:
            example_token_ids = example_token_ids[:max_sequence_length_model]

        padded_token_ids.append(
            example_token_ids
            + [self.pad_token_id] * (max_sequence_length_model - len(example_token_ids))
        )
    return padded_token_ids