# Buggy code
def _compute_attention_mask(actual_sequence_lengths: List[int]) -> np.ndarray:
    """Compute a mask for padding tokens.

    This mask will be used by the language model so that it does not attend to
    padding tokens.

    Args:
        actual_sequence_lengths: List of length of each example without any padding

    Returns:
        Computed attention mask, 0 for padding and 1 for non-padding tokens.
    """

    attention_mask = []
    max_seq_length = max(actual_sequence_lengths)
    for actual_sequence_length in actual_sequence_lengths:
        # add 1s for present tokens, fill up the remaining space up to max
        # sequence length with 0s (non-existing tokens)
        padded_sequence = [1] * actual_sequence_length + [0] * (
            max_seq_length - actual_sequence_length
        )
        attention_mask.append(padded_sequence)

    attention_mask = np.array(attention_mask).astype(np.float32)

    return attention_mask


# 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 _compute_attention_mask(
    actual_sequence_lengths: List[int], max_input_sequence_length: int
) -> np.ndarray:
    """Compute a mask for padding tokens.

    This mask will be used by the language model so that it does not attend to
    padding tokens.

    Args:
        actual_sequence_lengths: List of length of each example without any padding.
        max_input_sequence_length: Maximum length of a sequence that will be present in the input batch. This is
        after taking into consideration the maximum input sequence the model can handle. Hence it can never be
        greater than self.max_model_sequence_length in case the model applies length restriction.

    Returns:
        Computed attention mask, 0 for padding and 1 for non-padding tokens.
    """

    attention_mask = []

    for actual_sequence_length in actual_sequence_lengths:
        # add 1s for present tokens, fill up the remaining space up to max
        # sequence length with 0s (non-existing tokens)
        padded_sequence = [1] * min(
            actual_sequence_length, max_input_sequence_length
        ) + [0] * (
            max_input_sequence_length
            - min(actual_sequence_length, max_input_sequence_length)
        )
        attention_mask.append(padded_sequence)

    attention_mask = np.array(attention_mask).astype(np.float32)
    return attention_mask