# Example of how to pretrain T5?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%3E%5C
**Category:** 🤗Transformers\
**Created:** [March 3, 2021, 8:27pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129 "2021-03-03T20:27:51Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![mralexis](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@mralexis](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmralexis)%5C
**Post date:** [March 3, 2021, 8:27pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F1 "2021-03-03T20:27:51Z")

</div>

Is there any codebase in huggingface that could be used to pretrain T5 model? Looking into the examples dir in the repo there is nothing mentioned about T5. Thanks!

---

<div class="post-metadata">

**Author:** ![mralexis](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@mralexis](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmralexis)%5C
**Post date:** [March 4, 2021, 6:11pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F2 "2021-03-04T18:11:01Z")

</div>

Still need help on this…

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flewtun)%5C
**Post date:** [March 4, 2021, 6:26pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F3 "2021-03-04T18:26:22Z")

</div>

Hi @mralexis, there’s a GitHub issue that might help you: [How do I pre-train the T5 model in HuggingFace library using my own text corpus? · Issue #5079 · huggingface/transformers · GitHub](https://github.com/huggingface/transformers/issues/5079)

In particular `T5ForConditionalGeneration` is probably what you are looking for doing pretraining: [T5 — transformers 4.3.0 documentation](https://huggingface.co/transformers/model_doc/t5.html#t5forconditionalgeneration)

---

<div class="post-metadata">

**Author:** ![mralexis](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@mralexis](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmralexis)%5C
**Post date:** [March 4, 2021, 6:39pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F4 "2021-03-04T18:39:16Z")

</div>

@lewtun Thanks for the quick reply! I did check it out but there is only a code block on how to calculate the loss for pretraining but no other implementation details which are also critical. Do you know whether there is code on that?

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flewtun)%5C
**Post date:** [March 4, 2021, 6:52pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F5 "2021-03-04T18:52:39Z")

</div>

Unfortunately I do not know where one can find a detailed example of T5 pretraining, so pinging @valhalla in case he does

---

<div class="post-metadata">

**Author:** ![valhalla](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/valhalla/32/4179_2.png) [@valhalla](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fvalhalla)%5C
**Post date:** [March 15, 2021, 7:41am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F6 "2021-03-15T07:41:11Z")

</div>

Hey guys, sorry about the super late response.

T5 pre-training is not implemented with `Transformers`, AFAIK it’s only available in the original T5 repo.  
What we need to implement this with `Transformers` is the T5 style denoising dataset. It’s in my todo-list to implement this hopefully early next month.

---

<div class="post-metadata">

**Author:** ![stykat](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@stykat](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fstykat)%5C
**Post date:** [April 2, 2021, 1:15pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F7 "2021-04-02T13:15:14Z")

</div>

Hey! 🙂 just checking in on that to see if anyone has any updates.  
Thank you 🌻

---

<div class="post-metadata">

**Author:** ![ncoop57](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ncoop57/32/3107_2.png) [@ncoop57](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fncoop57)%5C
**Post date:** [May 7, 2021, 8:30pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F8 "2021-05-07T20:30:59Z")

</div>

I am also interested in this and actually have a semi working version (needs more testing) based on the original T5 repo. I’d be happy to work together on this to bring to the transformers library if it is still on the roadmap. Here is the colab with the current implementation: [Google Colaboratory](https://colab.research.google.com/drive/1OFiiaBA40EdReaeKB6MHnC7Dvl8u5a2T?usp=sharing) (scroll down/CTRL-F for DataCollatorForSeq2SeqMaskLanguageModeling

I can also open a PR to start this process if interested.

---

<div class="post-metadata">

**Author:** ![scienceapptest](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@scienceapptest](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fscienceapptest)%5C
**Post date:** [June 11, 2021, 9:41pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F9 "2021-06-11T21:41:57Z")

</div>

Any more developments here? My understanding is that we’d have to pre-train using the standard `Trainer` class with a custom Data Collator as described by @ncoop57. @valhalla would you be able to help/comment?

---

<div class="post-metadata">

**Author:** ![saichandra](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@saichandra](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsaichandra)%5C
**Post date:** [September 21, 2021, 8:37am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F10 "2021-09-21T08:37:00Z")

</div>

Hi @valhalla , any update on this ?

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [September 21, 2021, 9:45am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F11 "2021-09-21T09:45:32Z")

</div>

T5 pre-training is now supported in JAX/FLAX. You can check out the example script here: [transformers/examples/flax/language-modeling at master · huggingface/transformers · GitHub](https://github.com/huggingface/transformers/tree/master/examples/flax/language-modeling). It actually includes 2 scripts:

- [t5\_tokenizer\_model.py](https://github.com/huggingface/transformers/blob/master/examples/flax/language-modeling/t5_tokenizer_model.py), to train a T5 tokenizer (i.e. SentencePiece) from scratch.
- [run\_t5\_mlm\_flax.py](https://github.com/huggingface/transformers/blob/master/examples/flax/language-modeling/run_t5_mlm_flax.py), to pre-train T5. It’s suited to run on TPUs (for which you can obtain access for free by applying to Google’s [TFRC program](https://sites.research.google/trc/about/)).

@patrickvonplaten also demonstrates how to run the script in [this video](https://www.youtube.com/watch?v=__eG63ZP_5g) (starts around 13:35).

This script was developed for the [JAX/FLAX community event](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fopen-to-the-community-community-week-using-jax-flax-for-nlp-cv%2F7104). It would be really cool if someone contributes the PyTorch version of it. It would mean translating the script from FLAX to PyTorch, which is probably straightforward.

---

<div class="post-metadata">

**Author:** ![YiTian](https://avatars.discourse-cdn.com/v4/letter/y/d78d45/32.png) [@YiTian](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FYiTian)%5C
**Post date:** [January 13, 2022, 10:15am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F12 "2022-01-13T10:15:02Z")

</div>

Hi, I convert the parameters trained from JAX/FLAX to the pytorch version.  
model = FlaxT5ForConditionalGeneration.from\_pretrained(pretrained\_path)  
pt\_model = T5ForConditionalGeneration.from\_pretrained(tmp\_path, from\_flax=True)

However, some weights of T5ForConditionalGeneration were not initialized from the Flax model.  
Here are the details.  
All Flax model weights were used when initializing T5ForConditionalGeneration.  
Some weights of T5ForConditionalGeneration were not initialized from the Flax model and are newly initialized: [‘decoder.embed\_tokens.weight’, ‘encoder.embed\_tokens.weight’, ‘lm\_head.weight’]  
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.

I guess these three weights are shared.  
So, I add three lines before saving the parameters.  
pt\_model.encoder.embed\_tokens.weight.data = model.params[‘shared’][‘embedding’].\_value  
pt\_model.decoder.embed\_tokens.weight.data = model.params[‘shared’][‘embedding’].\_value  
pt\_model.lm\_head.weight.data = model.params[‘shared’][‘embedding’].\_value  
pt\_model.save\_pretrained(tmp\_path)

Is this RIGHT?

---

<div class="post-metadata">

**Author:** ![StephennFernandes](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/stephennfernandes/32/17230_2.png) [@StephennFernandes](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FStephennFernandes)%5C
**Post date:** [March 28, 2022, 2:11pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F13 "2022-03-28T14:11:48Z")

</div>

@lewtun @valhalla @nielsr @patrickvonplaten I am planing to pretrain multilingual T5 small and/or medium from scratch, i can across this post and the hugginface implementation for T5, my question is can i use the same pretraining script from T5 , by replace the T5Config with mT5Config ? WOULD THIS WORK ?

Also how should the dataset be arranged for multilingual languages pretraining ? should all the langages be arranged in a sequential order where a sequence of one lang followed by another eg: [French, German, Italian] or should all the languages be randomly shuffled ?

for the record i am planning to pretrain mT5 on indian languages on the oscar corpus and some additionally sourced text corpus.

---

<div class="post-metadata">

**Author:** ![AmineOueslati](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@AmineOueslati](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FAmineOueslati)%5C
**Post date:** [April 17, 2022, 4:07pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F14 "2022-04-17T16:07:45Z")

</div>

@StephennFernandes  
Hi, did it work for you with mT5?

---

<div class="post-metadata">

**Author:** ![jessicalopez](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/jessicalopez/32/26666_2.png) [@jessicalopez](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjessicalopez)%5C
**Post date:** [October 17, 2022, 3:47pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F15 "2022-10-17T15:47:27Z")

</div>

Hello @valhalla , do you have any updates? Thank you in advance

---

<div class="post-metadata">

**Author:** ![pnawrot](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@pnawrot](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fpnawrot)%5C
**Post date:** [March 16, 2023, 4:31pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexample-of-how-to-pretrain-t5%2F4129%2F16 "2023-03-16T16:31:17Z")

</div>

We’ve released [nanoT5](https://github.com/PiotrNawrot/nanoT5) which is a minimal codebase that reproduces T5-model (similar to BART) pre-training in PyTorch (not Flax), using Huggingface.

You can take a look!

Any suggestions are more than welcome.
