TinyLlama Documentation
repository·main·Indexed 27 days ago
https://github.com/jzhang38/tinyllamaTinyLlama is a project for pretraining a highly efficient 1.1B parameter Llama-architecture model on 3 trillion tokens. Designed for high-performance training and low-footprint inference, it is ideal for edge devices and speculative decoding. The repository provides instructions for pretraining on multiple nodes, full-parameter and QLora finetuning, and running a Gradio-based chatbot demo. It includes technical specifications for its 22-layer architecture and supports optimizations like Flash Attention 2 and FSDP.
What's inside TinyLlama
- TinyLlama is a project aimed at pre-training a 1.1B parameter Llama model on 3 trillion tokens. It uses the same architecture and tokenizer as Llama 2, making it compatible with many open-source Llama-based projects. Due to its small size, it is suitable for applications with limited compute and memory, such as edge devices (a 4-bit quantized version requires only ~550MB RAM), speculative decoding for larger models, and real-time dialogue in games.
Run the Tinyllama Gradio Chatbot locally
mainLaunch the chatbot interface by running the
app.pyscript. Once the server starts, the terminal will display a local URL. Open this URL in your web browser to interact with the chatbot.If you are running this on a remote server, use SSH local port forwarding to access the interface:
ssh -L [local port]:localhost:[remote port] [username]@[server address]python TinyLlama/chat_gradio/app.pyFine-tuning TinyLlama
mainFine-tuning code and inference scripts are located in the
sftdirectory.- Chat Models: The project has released chat models (e.g.,
TinyLlama-1.1B-Chat-v0.1) fine-tuned on the OpenAssistant dataset. - Low-Memory Fine-tuning: For GPUs with less than 4GB of RAM, it is recommended to use
Qloraandbitsandbytes.
- Chat Models: The project has released chat models (e.g.,
Evaluate TinyLlama using Instruct-Eval Benchmarks
mainTo evaluate TinyLlama's problem-solving capabilities (MMLU, BBH, HumanEval, and DROP) using the
instruct-evalsuite, run the correspondingmain.pycommands for each task. You can specify different GPUs usingCUDA_VISIBLE_DEVICESto run evaluations in parallel.CUDA_VISIBLE_DEVICES=0 python main.py mmlu --model_name llama --model_path PY007/TinyLlama-1.1B-intermediate-step-480K-1T CUDA_VISIBLE_DEVICES=1 python main.py bbh --model_name llama --model_path PY007/TinyLlama-1.1B-intermediate-step-480K-1T CUDA_VISIBLE_DEVICES=2 python main.py drop --model_name llama --model_path PY007/TinyLlama-1.1B-intermediate-step-480K-1T CUDA_VISIBLE_DEVICES=3 python main.py humaneval --model_name llama --n_sample 1 --model_path PY007/TinyLlama-1.1B-intermediate-step-480K-1TUse TinyLlama for Speculative Decoding
mainTinyLlama can be used to assist in the speculative decoding of larger models. Examples and instructions for using TinyLlama withllama.cppfor speculative decoding can be found in thespeculative_decoding/README.mdfile within the repository.Pre-training TinyLlama
mainFor instructions on how to start pre-training the model, please refer to thePRETRAIN.mdfile in the repository.Pretrain TinyLlama
mainFor detailed instructions on how to perform pretraining of the TinyLlama model, refer to thePRETRAIN.mdfile in the repository.Finetune TinyLlama
mainThe repository includes a simple full-parameter finetuning and inference script located in the
sftdirectory. The V0.1 chat model was finetuned using this script with theopenassistant-guanacodataset.For environments with limited memory (less than 4GB RAM), it is recommended to use
QLoraandbitsandbytesinstead of the provided full-parameter script.Use HuggingFace Assisted Generation for Speculative Decoding
mainYou can implement speculative decoding using HuggingFace's 'Assisted Generation' feature. This involves using a small assistant model (e.g.,
PY007/TinyLlama-1.1B-Chat-v0.1) to speed up a larger target model (e.g.,guanaco-7btoguanaco-33b).Note: Due to INT8 quantization and causal masking in assisted generation, the output of greedy decoding may differ from native decoding in rare occasions. Implementation code for testing this setup can be found in
instruct_hf_assisted_decoding.py.Install dependencies for TinyLlama pretraining
mainTo prepare the environment for pretraining, ensure CUDA 11.8 is installed. You must install PyTorch Nightly, build
xformersfrom source, and installflash-attentionalong with its fused operators. Finally, install the remaining requirements.Note: Building
xformersandflash-attentionmay take 5 minutes or longer and may appear to hang or produce many warnings.# Install Pytorch Nightly pip install --index-url https://download.pytorch.org/whl/nightly/cu118 --pre 'torch>=2.1.0dev' # Build XFormers from Source pip uninstall ninja -y && pip install ninja -U pip install -v -U git+https://github.com/facebookresearch/xformers.git@main#egg=xformers # Install Flash-Attention 2 and fused operators git clone https://github.com/Dao-AILab/flash-attention cd flash-attention python setup.py install cd csrc/rotary && pip install . cd ../layer_norm && pip install . cd ../xentropy && pip install . cd ../.. && rm -rf flash-attention # Install Remaining Dependencies pip install -r requirements.txt tokenizers sentencepieceEvaluate TinyLlama using GPT4All Benchmarks
mainTo evaluate TinyLlama's commonsense reasoning ability following the GPT4All evaluation suite, use the
lm-eval-harnesstool. The evaluation typically reportsacc_normby default across tasks like HellaSwag, OpenBookQA, WinoGrande, ARC (Easy and Challenge), BoolQ, and PIQA.python main.py \ --model hf-causal \ --model_args pretrained=PY007/TinyLlama-1.1B-Chat-v0.1,dtype="float" \ --tasks hellaswag,openbookqa,winogrande,arc_easy,arc_challenge,boolq,piqa\ --device cuda:0 --batch_size 32Prepare datasets for pretraining
mainTinyLlama pretraining requires the SlimPajama and Starcoderdata datasets.
- Download: Use
git lfsto clone the datasets from HuggingFace. Note that SlimPajama requires ~893GB and Starcoderdata requires ~290GB. - Tokenize: Use the provided scripts to tokenize the datasets and divide them into chunks. The processed data will require approximately 1.8T of storage.
# Download Datasets cd /path/to/dataset git lfs install git clone https://huggingface.co/datasets/cerebras/SlimPajama-627B git clone https://huggingface.co/datasets/bigcode/starcoderdata # Tokenize data python scripts/prepare_starcoder.py --source_path /path/to/starcoderdata/ --tokenizer_path data/llama --destination_path data/slim_star_combined --split train --percentage 1.0 python scripts/prepare_slimpajama.py --source_path /path/to/SlimPajama --tokenizer_path data/llama --destination_path data/slim_star_combined --split validation --percentage 1.0 python scripts/prepare_slimpajama.py --source_path /path/to/SlimPajama --tokenizer_path data/llama --destination_path data/slim_star_combined --split train --percentage 1.0- Download: Use