huggingface / transformers

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

EncoderDecoderModel with XLM-R #30211

Open Bachstelze opened 3 months ago

Bachstelze commented 3 months ago

System Info

private setup:

Google Colab setup:

Who can help?

@ArthurZucker @patrickvonplaten

Information

Tasks

Reproduction

Those are the training steps for an instruction-tuned shared EncoderDecoder xlm-r model:

Expected behavior

The XLM-R model is supported as encoderDecoderModel, so it should train just like other smaller mBERT or monolingual Roberta models.

This "IndexError: index out of range in self" is thrown by training it with matching context size of the filtered dataset and model:

python3 test_train_multilingual.py "/media/data/models/xlm-roberta-base" 512 False "adamw_hf" 1 1 20000 0.00001 1 1
load the saved and prefiltered dataset from the csv
1110446
['Heestan waxaa qada Khalid Haref Ahmed \nOO ku Jiray Kooxdii Dur Dur!', "Habeen ma hurdoo\nAday horjoogoo\nDharaar ma hargalo\nAduun baabay helayee\nRuntii ku helayoo\nCaawaan iman iman\nOonkaan u liitay\nIga ba'ay harraadkiisa\n\nHannaan wanaageey\nHadal macaaneey\nWadnahaad haleeshayoo\nWaad hirgalaysaa\nRuntii ku helayoo\nMaantaan iman iman\nOonkaan u liitay\nIga ba'ay harraadkiisa\n\nOlolaha jacaylkeenna\nYididdiiladeeniyo\nuur midoo fiyowbaan\nku abaabulaynaa\nUbixii aan beernaan\nku intifaacsanaynaa\nCaawaan iman iman\nOonkaan u liitay\nIga ba'ay harraadkiisa\n\nAfar gu' iyo dheeraad\nAxdigaynu taagnay\nAyaan dantiyo guur\nu adkaynay gaarnoo\nMarwadayda noqotoo\nUbad daadahaysee\nCaawaan iman iman\nOonkaan u liitay\nIga ba'ay harraadkiisa..."]
The following encoder weights were not tied to the decoder ['roberta/pooler']
accumulated batch size: 1
data size without validation set: 1110346
max train steps: 1110346
Detected kernel version 5.4.0, which is below the recommended minimum of 5.5.0; this can cause the process to hang. It is recommended to upgrade the kernel to the minimum version or higher.
/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/optimization.py:411: FutureWarning: This implementation of AdamW is deprecated and will be removed in a future version. Use the PyTorch implementation torch.optim.AdamW instead, or set `no_deprecation_warning=True` to disable this warning
  warnings.warn(
  0%|                                                           | 0/1110346 [00:00<?, ?it/s]/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/models/encoder_decoder/modeling_encoder_decoder.py:639: FutureWarning: Version v4.12.0 introduces a better way to train encoder-decoder models by computing the loss inside the encoder-decoder framework rather than in the decoder itself. You may observe training discrepancies if fine-tuning a model trained with versions anterior to 4.12.0. The decoder_input_ids are now created based on the labels, no need to pass them yourself anymore.
  warnings.warn(DEPRECATION_WARNING, FutureWarning)
{'loss': 12.575, 'learning_rate': 5.0000000000000004e-08, 'epoch': 0.0}
{'eval_loss': 12.56555461883545, 'eval_runtime': 100.4938, 'eval_samples_per_second': 0.995, 'eval_steps_per_second': 0.995, 'learning_rate': 5.0000000000000004e-08, 'epoch': 0.0}
{'loss': 12.5526, 'learning_rate': 1.0000000000000001e-07, 'epoch': 0.0}
{'eval_loss': 12.492094993591309, 'eval_runtime': 99.7907, 'eval_samples_per_second': 1.002, 'eval_steps_per_second': 1.002, 'learning_rate': 1.0000000000000001e-07, 'epoch': 0.0}
{'loss': 12.4601, 'learning_rate': 1.5000000000000002e-07, 'epoch': 0.0}
{'eval_loss': 12.378839492797852, 'eval_runtime': 103.8677, 'eval_samples_per_second': 0.963, 'eval_steps_per_second': 0.963, 'learning_rate': 1.5000000000000002e-07, 'epoch': 0.0}
{'loss': 12.3297, 'learning_rate': 2.0000000000000002e-07, 'epoch': 0.0}
{'eval_loss': 12.233966827392578, 'eval_runtime': 105.7982, 'eval_samples_per_second': 0.945, 'eval_steps_per_second': 0.945, 'learning_rate': 2.0000000000000002e-07, 'epoch': 0.0}
  0%|                                            | 497/1110346 [41:38<1299:57:07,  4.22s/it]Traceback (most recent call last):
  File "test_train_multilingual.py", line 69, in <module>
    trainer.train()
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/trainer.py", line 1555, in train
    return inner_training_loop(
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/trainer.py", line 1860, in _inner_training_loop
    tr_loss_step = self.training_step(model, inputs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/trainer.py", line 2725, in training_step
    loss = self.compute_loss(model, inputs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/trainer.py", line 2748, in compute_loss
    outputs = model(**inputs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/models/encoder_decoder/modeling_encoder_decoder.py", line 622, in forward
    decoder_outputs = self.decoder(
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/models/xlm_roberta/modeling_xlm_roberta.py", line 957, in forward
    outputs = self.roberta(
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/models/xlm_roberta/modeling_xlm_roberta.py", line 830, in forward
    embedding_output = self.embeddings(
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/models/xlm_roberta/modeling_xlm_roberta.py", line 131, in forward
    position_embeddings = self.position_embeddings(position_ids)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/sparse.py", line 162, in forward
    return F.embedding(
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/functional.py", line 2233, in embedding
    return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
IndexError: index out of range in self
  0%|          | 497/1110346 [41:40<1550:51:23,  5.03s/it]

In google colab this CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling cublasLtMatmul is thrown.

The training completes by reducing the filtered dataset to e.g. 510 tokens. Inferencing such a model throws this RuntimeError:

Write a tweet for a new transformer model with hashtags #instruction #nlp #generation #encoder #decoder
Traceback (most recent call last):
  File "test_train_multilingual.py", line 110, in <module>
    test_run(1)
  File "test_train_multilingual.py", line 104, in test_run
    output_ids = multilingualInstructionBERT.sharedModel.generate(input_ids.cuda(), do_sample=True, max_new_tokens=int(maximal_length))
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/generation/utils.py", line 1719, in generate
    return self.sample(
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/generation/utils.py", line 2801, in sample
    outputs = self(
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/accelerate/utils/operations.py", line 680, in forward
    return model_forward(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/accelerate/utils/operations.py", line 668, in __call__
    return convert_to_fp32(self.model_forward(*args, **kwargs))
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/amp/autocast_mode.py", line 16, in decorate_autocast
    return func(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/models/encoder_decoder/modeling_encoder_decoder.py", line 622, in forward
    decoder_outputs = self.decoder(
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/models/xlm_roberta/modeling_xlm_roberta.py", line 957, in forward
    outputs = self.roberta(
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/models/xlm_roberta/modeling_xlm_roberta.py", line 837, in forward
    encoder_outputs = self.encoder(
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/models/xlm_roberta/modeling_xlm_roberta.py", line 525, in forward
    layer_outputs = layer_module(
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/models/xlm_roberta/modeling_xlm_roberta.py", line 440, in forward
    cross_attention_outputs = self.crossattention(
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/models/xlm_roberta/modeling_xlm_roberta.py", line 341, in forward
    self_outputs = self.self(
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/hilsenbek/workspace/monolingualInstructionBERT/lib/python3.8/site-packages/transformers/models/xlm_roberta/modeling_xlm_roberta.py", line 277, in forward
    context_layer = torch.matmul(attention_probs, value_layer)
RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasGemmStridedBatchedExFix( handle, opa, opb, m, n, k, (void*)(&falpha), a, CUDA_R_16F, lda, stridea, b, CUDA_R_16F, ldb, strideb, (void*)(&fbeta), c, CUDA_R_16F, ldc, stridec, num_batches, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP)`
ArthurZucker commented 3 months ago

Hey IndexError: index out of range in self this is usually when the tokenizer's max encoded token id does not match the size of the embedding matrix. The cuda error might be asyncronus. Print the input ids

Bachstelze commented 2 months ago

Yes, the encoded tokens don't match the size of the embedding matrix. The training was completed by reducing the filtered dataset to e.g. 510 tokens. Inferencing such a trained model throws the stated RuntimeError. Which input ids should I print?

ArthurZucker commented 2 months ago

Just what ever gets passed to the model. You might not have resized correctly. model.resize_embedding() should work

github-actions[bot] commented 1 month 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.

Bachstelze commented 1 month ago

Why do I have to resize the model if the original size is used? The smaller input size is just for the dataset filtering. Is there somewhere a documentation about model.resize_embedding()?

ArthurZucker commented 1 month ago

Here is the documentation! https://huggingface.co/docs/transformers/en/main_classes/model#transformers.PreTrainedModel.resize_token_embeddings

github-actions[bot] commented 2 weeks 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.

Bachstelze commented 1 week ago

Thanks for the hint @ArthurZucker ! I will try it in the next development iteration.

The stale-bot is annoying. Couldn't it provide useful information and suggestions? And only close the issue if those aren't addressed?