Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

TinyLlama

> 编程语言
Open source

The TinyLlama project is an open endeavor to pretrain a 1.1B Llama model on 3 trillion tokens.

9.0K stars0 likes1 views
WebsiteGitHub

About

The TinyLlama project is an open endeavor to pretrain a 1.1B Llama model on 3 trillion tokens.

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.

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

  • 2023-12-18: Add two notes 1, 2 explaining the changes of training curves, project schedules, and bug fixes.
  • 2023-10-03: Add examples in speculative decoding with llama.cpp. Do check out the speculative_decoding/README.md.
  • 2023-10-02: 1. 1T-token checkpoint just dropped. 2. We document all intermediate checkpoints here.
  • 2023-09-28: Add a discord server.
  • 2023-09-18: 1. We added a chat demo so that you can play with TinyLlama-Chat-V0.1 right away.
  • 2023-09-16: 1. We released the intermediate checkpoint trained on 503B tokens. 2. We released a chat model finetuned on OpenAssisant and simple finetuning scripts is added. 3. More eval benchmarks are added and documented in EVAL.md.

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:

Date HF Checkpoint Tokens Step Commonsense Avg
2023-09-01 Pythia-1.0B 300B 143k 48.30
2023-09-04 TinyLlama-1.1B-intermediate-step-50k-105b 105B 50k 46.11
2023-09-16 TinyLlama-1.1B-intermediate-step-240k-503b 503B 240K 48.28
2023-10-01 TinyLlama-1.1B-intermediate-step-480k-1T 1T 480k 50.22
2023-11-04 TinyLlama-1.1B-intermediate-step-715k-1.5T 1.5T 715k 51.28
2023-11-20 TinyLlama-1.1B-intermediate-step-955k-2T 2T 955k 51.64
2023-12-11 TinyLlama-1.1B-intermediate-step-1195k-2.5T 2.5T 1195k 53.86
2023-12-28 TinyLlama-1.1B-intermediate-step-1431k-3T 3T 1431k 52.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:

Date HF Checkpoint Tokens Step Commonsense Avg
2023-09-16 TinyLlama-1.1B-Chat-V0.1 503B 240K 49.57
2023-10-1 TinyLlama-1.1B-Chat-V0.3 1T 480K 51.36
2023-11-04 TinyLlama-1.1B-Chat-V0.4 1.5T 715K 52.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:

  • Assisting speculative decoding of larger models. (See this tutorial by Andrej Karpathy)
  • Deployment on edge devices with restricted memory and computational capacities, for functionalities like real-time machine translation without an internet connection (the 4bit-quantized TinyLlama-1.1B's weight only takes up 637 MB).
  • Enabling real-time dialogue generation in video games.

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:

Setting Description
Parameters 1.1B
Attention Variant Grouped Query Attention
Model Size Layers: 22, Heads: 32, Query Groups: 4, Embedding Size: 2048, Intermediate Size (Swiglu): 5632
Sequence Length 2048
Batch Size 2 million tokens (2048 * 1024)
Learning Rate 4e-4
Learning Rate Schedule Cosine with 2000 warmup steps. See Issue 27 for a minor bug
Training Data Slimpajama & Starcoderdata
Data Preprocessing Excluded GitHub subset of Slimpajama; Sampled all code from Starcoderdata
Combined Dataset Size Around 950B tokens
Total Tokens During Training 3 trillion (slightly more than 3 epochs/1430k steps)
Natural Language to Code Ratio 7:3
Hardware 16 A100-40G GPUs

Blazingly Fast

Our codebase supports the following features:

  • multi-gpu and multi-node distributed training with FSDP.
  • flash attention 2.
  • fused layernorm.
  • fused swiglu.
  • fused cross entropy loss .
  • fused rotary positional embedding.

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.

Model A100 GPU hours taken on 300B tokens
TinyLlama-1.1B 3456
Pythia-1.0B 4830
MPT-1.3B 7920

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.

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:

Framework Device Settings Throughput (tokens/sec)
Llama.cpp Mac M2 16GB RAM batch_size=1; 4-bit inference 71.8
vLLM A40 GPU batch_size=100, n=10 7094.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:

  • Add scripts for pretraining on other datasets.
  • Sequence length extrapolation.
  • Test out speculative decoding for Llama-2-7B.
  • Test the throughput on RTX 3090/4090.
  • Add fine-tuning scripts.
  • Properly evaluate the model on downstream tasks.
  • A demo running on mobile phones.
  • Explore retrieval-augmentation.

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](https://github.com/C

Issues· 47 open

View all issuesOpen on GitHub
  • #207

    What is the recommended Python version for this project?

    Updated Apr 7, 2025
  • #206

    Training Tokens of Checkpoints for tinyllama_math_code

    Updated Mar 27, 2025
  • #198

    error while loading Tiny llama

    Updated Mar 27, 2025
  • #203

    is somebody knows how many validation file there are?

    Updated Mar 20, 2025
  • #201

    unable to install flash_attn

    Updated Mar 6, 2025
  • #202

    How to write the pre-training startup script on a single physical machine with 16 A100 cards?

    Updated Feb 20, 2025
  • #200

    Can this model reply correct answer?

    Updated Nov 14, 2024
  • #168

    Help me pls

    Updated Nov 10, 2024
  • #196

    inconsistent size when I use the huggingface model

    Updated Oct 30, 2024
  • #199

    How to Load the Pretrained Dataset after Downloading?

    Updated Oct 7, 2024

> Tags

Python

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 29, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言