# Why use batched=True in map function?

**URL:** <https://discuss.huggingface.co/t/why-use-batched-true-in-map-function/18042>\
**Category:** 🤗Datasets\
**Created:** [May 17, 2022, 11:20am UTC](https://discuss.huggingface.co/t/why-use-batched-true-in-map-function/18042 "2022-05-17T11:20:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![petarulev](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@petarulev](https://discuss.huggingface.co/u/petarulev)\
**Post date:** [May 17, 2022, 11:20am UTC](https://discuss.huggingface.co/t/why-use-batched-true-in-map-function/18042/1 "2022-05-17T11:20:08Z")

</div>

I have a dataset and a tokenizer:

```auto
dataset = load_dataset(path='/Users/petar/Documents/data', split='train')

def encode(examples):
    return tokenizer(examples['text'], truncation=True, padding='max_length', max_length=128)

# dataset = dataset.shuffle()
dataset = dataset.map(encode, batched=True) # Use num_proc=N. Investigate why use batched=True?

```

I see that when I used batched=True, the tokenization happens significantly faster. What is the reason and is there any difference if I train the model on the batched data vs unbatched data. If yes, what should be the size of batch\_size? Should it match `per_device_train_batch_size` in `TrainingArguments` ?

I also have a question about DataCollatorForLanguageModelling:

```auto
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=True, mlm_probability=0.15)

```

I pass it to my trainer:

```auto
trainer = Trainer(
    model=model,
    args=training_args,
    data_collator=data_collator,
    train_dataset=dataset,
)

```

But why do I need to specify the tokenizer in the data collator when I already have tokenized my data with the map function?

Regards

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](https://discuss.huggingface.co/u/sgugger)\
**Post date:** [May 17, 2022, 11:44am UTC](https://discuss.huggingface.co/t/why-use-batched-true-in-map-function/18042/2 "2022-05-17T11:44:31Z")

</div>

No, the batch size should not be the same as for the training. The default in the `Dataset.map` method is 1,000 which is more than enough for the use case. As for why it’s faster, it’s all explained in [the course](https://huggingface.co/course/chapter6/3?fw=pt). Fast tokenizers need a lot of texts to be able to leverage parallelism in Rust (a bit like a GPU needs a batch of examples to be more efficient).

---

<div class="post-metadata">

**Author:** ![petarulev](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@petarulev](https://discuss.huggingface.co/u/petarulev)\
**Post date:** [May 17, 2022, 11:47am UTC](https://discuss.huggingface.co/t/why-use-batched-true-in-map-function/18042/3 "2022-05-17T11:47:27Z")

</div>

> [@sgugger](#):
>
> need a lot of texts to be able to leverage parallelism in Rust

I will have to watch the course these days. Thanks!
