# Data Preparation for CausalLM

**URL:** <https://discuss.huggingface.co/t/data-preparation-for-causallm/33867>\
**Category:** 🤗Transformers\
**Created:** [March 15, 2023, 1:05pm UTC](https://discuss.huggingface.co/t/data-preparation-for-causallm/33867 "2023-03-15T13:05:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![blueeagle](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@blueeagle](https://discuss.huggingface.co/u/blueeagle)\
**Post date:** [March 15, 2023, 1:05pm UTC](https://discuss.huggingface.co/t/data-preparation-for-causallm/33867/1 "2023-03-15T13:05:41Z")

</div>

Hi together,

I want to train a CausalLM (gpt2) according to this [course](https://huggingface.co/course/chapter7/6).  
Hereby, I am using the [DataCollatorforLM](https://huggingface.co/docs/transformers/main_classes/data_collator#transformers.DataCollatorForLanguageModeling) with the flag `mlm` set to `False`.  
However, I am still unsure about how exactly the batches are generated from one sample.  
Given a tokenized sample

```auto
[10, 14, 36, 28, 30, 31, 77, 100, 101]

```

the data collator is returning the input and label for training

```auto
input = [10, 14, 36, 28, 30, 31, 77, 100, 101]
label = [10, 14, 36, 28, 30, 31, 77, 100, 101]

```

In the documentation of the datacollator I already found, that the labels will be shifted right automatically during training by the model. Still, for causal language modeling I would want to create multiple inputs and labels of the given sample, so that the model will have to predict the correct token at each position, hence:

```auto
input = [
	[10, 0, 0, 0, 0, 0, 0, 0, 0]
	[10, 14, 0, 0, 0, 0, 0, 0, 0]
	[10, 14, 36, 0, 0, 0, 0, 0, 0]
	[10, 14, 36, 28, 0, 0, 0, 0, 0]
	[10, 14, 36, 28, 30, 0, 0, 0, 0]
	[10, 14, 36, 28, 30, 31, 0, 0, 0]
	[10, 14, 36, 28, 30, 31, 77, 0, 0]
	[10, 14, 36, 28, 30, 31, 77, 100, 0]
]
label = [
	[10, 14, 0, 0, 0, 0, 0, 0, 0]
	[10, 14, 36, 0, 0, 0, 0, 0, 0]
	[10, 14, 36, 28, 0, 0, 0, 0, 0]
	[10, 14, 36, 28, 30, 0, 0, 0, 0]
	[10, 14, 36, 28, 30, 31, 0, 0, 0]
	[10, 14, 36, 28, 30, 31, 77, 0, 0]
	[10, 14, 36, 28, 30, 31, 77, 100, 0]
	[10, 14, 36, 28, 30, 31, 77, 100, 101]
]

```

My question is now:  
**Is this done automatically by the „CausalLM“ model or do I have to implement this by myself in a custom dataloader/dataset?**

---

<div class="post-metadata">

**Author:** ![blueeagle](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@blueeagle](https://discuss.huggingface.co/u/blueeagle)\
**Post date:** [March 16, 2023, 7:46am UTC](https://discuss.huggingface.co/t/data-preparation-for-causallm/33867/2 "2023-03-16T07:46:54Z")

</div>

I just figured it out by myself.  
As explained [in this video](https://www.youtube.com/watch?v=kCc8FmEb1nY), assuring that the model predicts to correct next token at each position is usually done by using a triangular mask in the self-attention layer and not by passing all possibilities as a separate sample.  
By looking at the GPT2 implementation from huggingface, I found that the `GPT2Attention` module implements a triangular `causal_mask` for this, thus **there should be no need for preprocessing the data manually as asked above** 🙂
