Home

Awesome

<div align="center">

TinyLlama-1.1B

English | δΈ­ζ–‡

Chat Demo | Discord

</div>

The TinyLlama project aims to pretrain a 1.1B Llama model on 3 trillion tokens. With some proper optimization, we can achieve this within a span of "just" 90 days using 16 A100-40G GPUs πŸš€πŸš€. The training has started on 2023-09-01.

<div align="center"> <img src=".github/TinyLlama_logo.png" width="300"/> </div>

We adopted exactly the same architecture and tokenizer as Llama 2. This means TinyLlama can be plugged and played in many open-source projects built upon Llama. Besides, TinyLlama is compact with only 1.1B parameters. This compactness allows it to cater to a multitude of applications demanding a restricted computation and memory footprint.

News

Evaluation

You can find the evaluation results of TinyLlama in EVAL.md.

Releases Schedule

We will be rolling out intermediate checkpoints following the below schedule.

Base models:

DateHF CheckpointTokensStepCommonsense Avg
2023-09-01Pythia-1.0B300B143k48.30
2023-09-04TinyLlama-1.1B-intermediate-step-50k-105b105B50k46.11
2023-09-16TinyLlama-1.1B-intermediate-step-240k-503b503B240K48.28
2023-10-01TinyLlama-1.1B-intermediate-step-480k-1T1T480k50.22
2023-11-04TinyLlama-1.1B-intermediate-step-715k-1.5T1.5T715k51.28
2023-11-20TinyLlama-1.1B-intermediate-step-955k-2T2T955k51.64
2023-12-11TinyLlama-1.1B-intermediate-step-1195k-2.5T2.5T1195k53.86
2023-12-28TinyLlama-1.1B-intermediate-step-1431k-3T3T1431k52.99

We are crafting a note offering possible explaination on why there is a significant improvement from 2T to 2.5T checkpoint (It is related to bos_id issue)

Chat models:

DateHF CheckpointTokensStepCommonsense Avg
2023-09-16TinyLlama-1.1B-Chat-V0.1503B240K49.57
2023-10-1TinyLlama-1.1B-Chat-V0.31T480K51.36
2023-11-04TinyLlama-1.1B-Chat-V0.41.5T715K52.30

Note that the learning rate of the base model has not cooled down yet so we recommend you to also use the finetuned chat model.

Meanwhile, you can track the live cross entropy loss here.

Potential Usecase

Tiny but strong language models are useful for many applications. Here are some potential usecases:

Moreover, our code can be a reference for enthusiasts keen on pretraining language models under 5 billion parameters without diving too early into Megatron-LM.

Training Details

Below are some details of our training setup:

SettingDescription
Parameters1.1B
Attention VariantGrouped Query Attention
Model SizeLayers: 22, Heads: 32, Query Groups: 4, Embedding Size: 2048, Intermediate Size (Swiglu): 5632
Sequence Length2048
Batch Size2 million tokens (2048 * 1024)
Learning Rate4e-4
Learning Rate ScheduleCosine with 2000 warmup steps. See Issue 27 for a minor bug
Training DataSlimpajama & Starcoderdata
Data PreprocessingExcluded GitHub subset of Slimpajama; Sampled all code from Starcoderdata
Combined Dataset SizeAround 950B tokens
Total Tokens During Training3 trillion (slightly more than 3 epochs/1430k steps)
Natural Language to Code Ratio7:3
Hardware16 A100-40G GPUs

Blazingly Fast

Our codebase supports the following features:

Credit: flash attention 2, fused layernorm, fused cross entropy loss, and fused rotary positional embedding are from the FlashAttention repo. Fused swiglu is from xformers.

Thanks to those optimizations, we achieve a throughput of 24k tokens per second per A100-40G GPU, which translates to 56% model flops utilization without activation checkpointing (We expect the MFU to be even higher on A100-80G). It means you can train a chinchilla-optimal TinyLlama (1.1B param, 22B tokens) in 32 hours with 8 A100. Those optimizations also greatly reduce the memory footprint, allowing us to stuff our 1.1B model into 40GB GPU RAM and train with a per-gpu batch size of 16k tokens. You can also pretrain TinyLlama on 3090/4090 GPUs with a smaller per-gpu batch size. Below is a comparison of the training speed of our codebase with that of Pythia and MPT.

ModelA100 GPU hours taken on 300B tokens
TinyLlama-1.1B3456
Pythia-1.0B4830
MPT-1.3B7920

<small> The Pythia number comes from their paper. The MPT number comes from here, in which they say MPT-1.3B " was trained on 440 A100-40GBs for about half a day" on 200B tokens. </small>

The fact that TinyLlama is a relatively small model with grouped query attention means it is also fast during inference. Below are some throughputs that we measure:

FrameworkDeviceSettingsThroughput (tokens/sec)
Llama.cppMac M2 16GB RAMbatch_size=1; 4-bit inference71.8
vLLMA40 GPUbatch_size=100, n=107094.5

Pretrain

Please refer to PRETRAIN.md for instructions on how to pretrain TinyLlama.

Finetune

We include a simple full-parameter finetuning & inference script in sft. Our V0.1 chat model is finetuned using this script. The FT dataset we use is openassistant-guanaco. For finetuning with less than 4GB RAM, we refer you to the Qlora and bitsandbytes repos. We did not undergo extensive hyperparameter tuning nor choose more performant FT datasets. We hope the community can explore on finetuning TinyLlama and come up with better chat models. I will include community-finetuned models in this repo.

TODO

This project is still under active development. We are a really small team. Community feedback and contributions are highly appreciated. Here are some things we plan to work on:

Acknowledgements

This repository is built upon lit-gpt and flash-attention. Be sure to explore this fantastic open-source project if it's new to you!

@online{lit-gpt,
  author    = {Lightning AI},
  title     = {Lit-GPT},
  url       = {https://github.com/Lightning-AI/lit-gpt},
  year      = {2023},
}
@article{dao2023flashattention2,
  title     ={Flash{A}ttention-2: Faster Attention with Better Parallelism and Work Partitioning},
  author    ={Dao, Tri},
  year      ={2023}
}

Citation

This project is currently contributed by Peiyuan Zhang *, Guangtao Zeng *, Tianduo Wang and Wei Lu from the StatNLP Research Group of Singapore University of Technology and Design.

If you find our work valuable, please cite:

@misc{zhang2024tinyllama,
      title={TinyLlama: An Open-Source Small Language Model}, 
      author={Peiyuan Zhang and Guangtao Zeng and Tianduo Wang and Wei Lu},
      year={2024},
      eprint={2401.02385},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}

Frequently Asked Questions

1. Why would pretraining a 1.1B model for so long make sense? Doesn't it contradict the Chinchilla Scaling Law?

<img src=".github/llama2-training.png" alt="The training loss curve of Llama 2" width="500"/>

Above is the training loss curve taken from the Llama 2 paper. Here I quote from that paper: "We observe that after pretraining on 2T Tokens, the models still did not show any sign of saturation". That is why we believe pretraining a 1.1B model for 3T tokens is a reasonable thing to do. Even if the loss curve does not go down eventually, we can still study the phenomenon of saturation and learn something from it.

2. What does "saturation" mean?

<img src=".github/Pythia_saturation.png" alt="Figure 10 of the Pythia paper" width="500"/>

The figure from the Pythia paper displays the LAMBADA accuracy plotted against the total training tokens (300B). The term "saturation" pertains specifically to the 70M and 160M models. Notably, even the 410M model does not saturate with 300B tokens, as it continues to show an increasing trend, similar to the trend of larger models.

Star History

Star History Chart