# Error in DataCollator section of Hugging Face Tutorial LM fine tuning

**URL:** <https://discuss.huggingface.co/t/error-in-datacollator-section-of-hugging-face-tutorial-lm-fine-tuning/68521>\
**Category:** Beginners\
**Created:** [January 9, 2024, 2:32pm UTC](https://discuss.huggingface.co/t/error-in-datacollator-section-of-hugging-face-tutorial-lm-fine-tuning/68521 "2024-01-09T14:32:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![metamath](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/metamath/32/22205_2.png) [@metamath](https://discuss.huggingface.co/u/metamath)\
**Post date:** [January 9, 2024, 2:32pm UTC](https://discuss.huggingface.co/t/error-in-datacollator-section-of-hugging-face-tutorial-lm-fine-tuning/68521/1 "2024-01-09T14:32:26Z")

</div>

Hello,

I believe I have found a minor error in the documentation on the following page: [Causal language modeling](https://huggingface.co/docs/transformers/v4.36.1/en/tasks/language_modeling).

In the tutorial section about the collator, it is written as follows:

```auto
"Use the end-of-sequence token as the padding token and set mlm=False. This will use the inputs as labels shifted to the right by one element:

from transformers import DataCollatorForLanguageModeling

tokenizer.pad_token = tokenizer.eos_token
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)"

```

However, it seems to me that DataCollatorForLanguageModeling does not shift the `labels` to create `input_ids`. Instead, it keeps both values the same.

Could there be something I am misunderstanding?

Thank you for reading my message and for considering this potential correction.

---

<div class="post-metadata">

**Author:** ![lkurlandski](https://avatars.discourse-cdn.com/v4/letter/l/b782af/32.png) [@lkurlandski](https://discuss.huggingface.co/u/lkurlandski)\
**Post date:** [January 11, 2024, 11:48pm UTC](https://discuss.huggingface.co/t/error-in-datacollator-section-of-hugging-face-tutorial-lm-fine-tuning/68521/2 "2024-01-11T23:48:36Z")

</div>

I believe this is handled by the forward method of the XXXFormerLMHeadModel class. For example, the transformers.models.gpt2.modeling\_gpt2 from the GPT2LMHeadModel.forward method contains the following snippet:

```auto
        loss = None
        if labels is not None:
            # move labels to correct device to enable model parallelism
            labels = labels.to(lm_logits.device)
            # Shift so that tokens < n predict n
            shift_logits = lm_logits[..., :-1, :].contiguous()
            shift_labels = labels[..., 1:].contiguous()
            # Flatten the tokens
            loss_fct = CrossEntropyLoss()
            loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))

```

---

<div class="post-metadata">

**Author:** ![metamath](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/metamath/32/22205_2.png) [@metamath](https://discuss.huggingface.co/u/metamath)\
**Post date:** [January 12, 2024, 3:16am UTC](https://discuss.huggingface.co/t/error-in-datacollator-section-of-hugging-face-tutorial-lm-fine-tuning/68521/3 "2024-01-12T03:16:58Z")

</div>

You are correct in saying that the label shifting handled within the model.  
Therefore, the part pointed out in the tutorial is indeed an error.

Thank you for providing an example with the code.
