{
  "source_file": "0930",
  "source_platform": "Medium",
  "author": "Maxime Labonne",
  "title_listed": "Fine-Tune Your Own Llama 2 Model in a Colab Notebook",
  "keywords": "微调,Llama2,QLoRA",
  "domain": "大模型算法",
  "content_type": "深度技术解析",
  "is_paid_flag": "部分免费/会员墙",
  "url": "https://towardsdatascience.com/fine-tune-your-own-llama-2-model-in-a-colab-notebook-df9823a04a32/",
  "publish_date": "2023-07",
  "corpus_stage": "预训练/SFT",
  "notes": "",
  "id": "84bf1f9f302e",
  "collected_at": "2026-10-01T21:53:55",
  "title": "Fine-Tune Your Own Llama 2 Model in a Colab Notebook | Towards Data Science",
  "body_markdown": "Toggle Search[](https://towardsdatascience.com/)Toggle Mobile Navigation\n[](https://towardsdatascience.com/)\nPublish AI, ML & data-science insights to a global community of data professionals.\nSign in[Submit an Article](https://write.insightmediagroup.io)\n[Latest](https://towardsdatascience.com/latest/)[Editor's Picks](https://towardsdatascience.com/tag/editors-pick/)[Newsletter](https://towardsdatascience.com/tag/the-variable/)[ShipAI](https://towardsdatascience.com/shipai/)\n* * *\n[Write For TDS](https://towardsdatascience.com/submissions/)\n[LinkedIn](https://www.linkedin.com/company/towards-data-science/)[X](https://x.com/TDataScience)Toggle Search\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\n# Fine-Tune Your Own Llama 2 Model in a Colab Notebook\nA practical introduction to LLM fine-tuning\n[Maxime Labonne](https://towardsdatascience.com/author/mlabonne/)\nJuly 25, 202312 min read\n![Image by author](https://assets.insightmediagroup.io/media/wp-content/uploads/2023/07/1jsksHuIVtuXDh-aLpwZDbw.png)\nImage by author\nWith the release of LLaMA v1, we saw a Cambrian explosion of fine-tuned models, including [Alpaca](https://github.com/tatsu-lab/stanford_alpaca), [Vicuna](https://huggingface.co/lmsys/vicuna-13b-v1.3), and [WizardLM](https://huggingface.co/WizardLM/WizardLM-13B-V1.1), among others. This trend encouraged different businesses to launch their own base models with licenses suitable for commercial use, such as [OpenLLaMA](https://github.com/openlm-research/open_llama), [Falcon](https://falconllm.tii.ae/), [XGen](https://github.com/salesforce/xgen), etc. The release of Llama 2 now combines the best elements from both sides: it offers a **highly efficient base model along with a more permissive license**.\nDuring the first half of 2023, the software landscape was significantly shaped by the **widespread use of APIs** (like OpenAI API) to create infrastructures based on Large Language Models (LLMs). Libraries such as [LangChain](https://python.langchain.com/docs/get_started/introduction.html) and [LlamaIndex](https://www.llamaindex.ai/) played a critical role in this trend. Moving into the latter half of the year, the process of **fine-tuning (or instruction tuning) these models is set to become a standard procedure** in the LLMOps workflow. This trend is driven by various factors: the potential for cost savings, the ability to process confidential data, and even the potential to develop models that exceed the performance of prominent models like ChatGPT and GPT-4 in certain specific tasks.\nIn this article, we will see why instruction tuning works and how to implement it in a Google [Colab](https://colab.research.google.com/drive/1PEQyJO1-f6j0S_XJ8DV50NkpzasXkrzd?usp=sharing) notebook to create your own Llama 2 model. As usual, the code is available on Colab and [GitHub](https://github.com/mlabonne/llm-course).\nLearn this step by step with the interactive [AI Engineer](https://roadmap.sh/ai-engineer), [AI and Data Scientist](https://roadmap.sh/ai-data-scientist) and [Computer Science](https://roadmap.sh/computer-science) roadmaps.\n##  **🔧** Background on fine-tuning LLMs\n![Image by author](https://assets.insightmediagroup.io/media/wp-content/uploads/2023/07/1DsOt1rVHLkPhVnhSSY20tA.png)\nImage by author\nLLMs are pretrained on an extensive corpus of text. In the case of [Llama 2](https://arxiv.org/abs/2307.09288), we know very little about the composition of the training set, besides its length of 2 trillion tokens. In comparison, [BERT](https://arxiv.org/abs/1810.04805) (2018) was \"only\" trained on the BookCorpus (800M words) and English Wikipedia (2,500M words). From experience, this is a **very costly and long process** with a lot of hardware issues. If you want to know more about it, I recommend reading [Meta's logbook](https://github.com/facebookresearch/metaseq/blob/main/projects/OPT/chronicles/OPT175B_Logbook.pdf) about the pretraining of the OPT-175B model.\nWhen the pretraining is complete, auto-regressive models like Llama 2 can **predict the next token** in a sequence. However, this does not make them particularly useful assistants since they don't reply to instructions. This is why we employ instruction tuning to align their answers with what humans expect. There are two main fine-tuning techniques:\n  * **Supervised Fine-Tuning** (SFT): Models are trained on a dataset of instructions and responses. It adjusts the weights in the LLM to minimize the difference between the generated answers and ground-truth responses, acting as labels.\n  * **Reinforcement Learning from Human Feedback** (RLHF): Models learn by interacting with their environment and receiving feedback. They are trained to maximize a reward signal (using [PPO](https://arxiv.org/abs/1707.06347)), which is often derived from human evaluations of model outputs.\n\n\nIn general, RLHF is shown to capture **more complex and nuanced** human preferences, but is also more challenging to implement effectively. Indeed, it requires careful design of the reward system and can be sensitive to the quality and consistency of human feedback. A possible alternative in the future is the [Direct Preference Optimization](https://arxiv.org/abs/2305.18290) (DPO) algorithm, which directly runs preference learning on the SFT model.\nIn our case, we will perform SFT, but this raises a question: why does fine-tuning work in the first place? As highlighted in the [Orca paper](https://mlabonne.github.io/blog/notes/Large%20Language%20Models/orca.html), our understanding is that fine-tuning **leverages knowledge learned during the pretraining** process. In other words, fine-tuning will be of little help if the model has never seen the kind of data you're interested in. However, if that's the case, SFT can be extremely performant.\nFor example, the [LIMA paper](https://mlabonne.github.io/blog/notes/Large%20Language%20Models/lima.html) showed how you could outperform GPT-3 (DaVinci003) by fine-tuning a LLaMA (v1) model with 65 billion parameters on only 1,000 high-quality samples. The **quality of the instruction dataset is essential** to reach this level of performance, which is why a lot of work is focused on this issue (like [evol-instruct](https://arxiv.org/abs/2304.12244), Orca, or [phi-1](https://mlabonne.github.io/blog/notes/Large%20Language%20Models/phi1.html)). Note that the size of the LLM (65b, not 13b or 7b) is also fundamental to leverage pre-existing knowledge efficiently.\nAnother important point related to the data quality is the **prompt template**. Prompts are comprised of similar elements: system prompt (optional) to guide the model, user prompt (required) to give the instruction, additional inputs (optional) to take into consideration, and the model's answer (required). In the case of Llama 2, the authors used the following template for the **chat models** :\ntext\n\n```\n<s>[INST] <<SYS>>System prompt<</SYS>>User prompt [/INST] Model answer </s>\n```\n\nThere are other templates, like the ones from Alpaca and Vicuna, and their impact is not very clear. In this example, we will reformat our instruction dataset to follow Llama 2's template. For the purpose of this tutorial, I've already done it using the excellent `[timdettmers/openassistant-guanaco](https://huggingface.co/datasets/timdettmers/openassistant-guanaco)` dataset. You can find it on Hugging Face under the name `[mlabonne/guanaco-llama2-1k](https://huggingface.co/datasets/mlabonne/guanaco-llama2-1k)`. Note that you don't need to follow a specific prompt template if you're using the base Llama 2 model instead of the chat version.\n## 🦙 How to fine-tune Llama 2\nIn this section, we will fine-tune a Llama 2 model with 7 billion parameters on a T4 GPU with high RAM using Google Colab (2.21 credits/hour). Note that a T4 only has 16 GB of VRAM, which is barely enough to **store Llama 2–7b's weights** (7b × 2 bytes = 14 GB in FP16). In addition, we need to consider the overhead due to optimizer states, gradients, and forward activations (see [this excellent article](https://huggingface.co/docs/transformers/perf_train_gpu_one#anatomy-of-models-memory) for more information). This means that a full fine-tuning is not possible here: we need parameter-efficient fine-tuning (PEFT) techniques like [LoRA](https://arxiv.org/abs/2106.09685) or [QLoRA](https://arxiv.org/abs/2305.14314).\nTo drastically reduce the VRAM usage, we must **fine-tune the model in 4-bit precision** , which is why we'll use QLoRA here. The good thing is that we can leverage the Hugging Face ecosystem with the `transformers`, `accelerate`, `peft`, `trl`, and `bitsandbytes` libraries. We'll do this in the following code based on Younes Belkada's [GitHub Gist](https://gist.github.com/younesbelkada/9f7f75c94bdc1981c8ca5cc937d4a4da). First, we install and load these libraries.\npython\n\n```\n!pip install -q accelerate==0.21.0 peft==0.4.0 bitsandbytes==0.40.2 transformers==4.31.0 trl==0.4.7\n```\n\npython\n\n```\nimport osimport torchfrom datasets import load_datasetfrom transformers import (    AutoModelForCausalLM,    AutoTokenizer,    BitsAndBytesConfig,    HfArgumentParser,    TrainingArguments,    pipeline,    logging,)from peft import LoraConfig, PeftModelfrom trl import SFTTrainer\n```\n\nLet's talk a bit about the parameters we can tune here. First, we want to load a `llama-2-7b-chat-hf` model (**chat** model) and train it on the `mlabonne/guanaco-llama2-1k` (1,000 samples), which will produce our fine-tuned model `llama-2-7b-miniguanaco`. If you're interested in how this dataset was created, you can check [this notebook](https://colab.research.google.com/drive/1Ad7a9zMmkxuXTOh1Z7-rNSICA4dybpM2?usp=sharing). Feel free to change it: there are many good datasets on the [Hugging Face Hub](https://huggingface.co/datasets), like `[databricks/databricks-dolly-15k](https://huggingface.co/datasets/databricks/databricks-dolly-15k)`.\nQLoRA will use a rank of 64 with a scaling parameter of 16 (see [this article](https://rentry.org/llm-training#low-rank-adaptation-lora_1) for more information about LoRA parameters). We'll load the Llama 2 model directly in 4-bit precision using the NF4 type and train it for one epoch. To get more information about the other parameters, check the [TrainingArguments](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments), [PeftModel](https://huggingface.co/docs/peft/package_reference/peft_model), and [SFTTrainer](https://huggingface.co/docs/trl/main/en/sft_trainer) documentation.\npython\n\n```\n# The model that you want to train from the Hugging Face hubmodel_name = \"NousResearch/Llama-2-7b-chat-hf\"# The instruction dataset to usedataset_name = \"mlabonne/guanaco-llama2-1k\"# Fine-tuned model namenew_model = \"llama-2-7b-miniguanaco\"################################################################################# QLoRA parameters################################################################################# LoRA attention dimensionlora_r = 64# Alpha parameter for LoRA scalinglora_alpha = 16# Dropout probability for LoRA layerslora_dropout = 0.1################################################################################# bitsandbytes parameters################################################################################# Activate 4-bit precision base model loadinguse_4bit = True# Compute dtype for 4-bit base modelsbnb_4bit_compute_dtype = \"float16\"# Quantization type (fp4 or nf4)bnb_4bit_quant_type = \"nf4\"# Activate nested quantization for 4-bit base models (double quantization)use_nested_quant = False################################################################################# TrainingArguments parameters################################################################################# Output directory where the model predictions and checkpoints will be storedoutput_dir = \"./results\"# Number of training epochsnum_train_epochs = 1# Enable fp16/bf16 training (set bf16 to True with an A100)fp16 = Falsebf16 = False# Batch size per GPU for trainingper_device_train_batch_size = 4# Batch size per GPU for evaluationper_device_eval_batch_size = 4# Number of update steps to accumulate the gradients forgradient_accumulation_steps = 1# Enable gradient checkpointinggradient_checkpointing = True# Maximum gradient normal (gradient clipping)max_grad_norm = 0.3# Initial learning rate (AdamW optimizer)learning_rate = 2e-4# Weight decay to apply to all layers except bias/LayerNorm weightsweight_decay = 0.001# Optimizer to useoptim = \"paged_adamw_32bit\"# Learning rate schedule (constant a bit better than cosine)lr_scheduler_type = \"constant\"# Number of training steps (overrides num_train_epochs)max_steps = -1# Ratio of steps for a linear warmup (from 0 to learning rate)warmup_ratio = 0.03# Group sequences into batches with same length# Saves memory and speeds up training considerablygroup_by_length = True# Save checkpoint every X updates stepssave_steps = 25# Log every X updates stepslogging_steps = 25################################################################################# SFT parameters################################################################################# Maximum sequence length to usemax_seq_length = None# Pack multiple short examples in the same input sequence to increase efficiencypacking = False# Load the entire model on the GPU 0device_map = {\"\": 0}\n```\n\nWe can now load everything and start the fine-tuning process. We're relying on multiple wrappers, so bear with me.\n  * First of all, we want to **load the dataset** we defined. Here, our dataset is already preprocessed but, usually, this is where you would reformat the prompt, filter out bad text, combine multiple datasets, etc.\n  * Then, we're configuring `bitsandbytes` for 4-bit quantization.\n  * Next, we're loading the Llama 2 model in 4-bit precision on a GPU with the corresponding tokenizer.\n  * Finally, we're loading configurations for QLoRA, regular training parameters, and passing everything to the `SFTTrainer`. The training can finally start!\n\n\npython\n\n```\n# Load dataset (you can process it here)dataset = load_dataset(dataset_name, split=\"train\")# Load tokenizer and model with QLoRA configurationcompute_dtype = getattr(torch, bnb_4bit_compute_dtype)bnb_config = BitsAndBytesConfig(    load_in_4bit=use_4bit,    bnb_4bit_quant_type=bnb_4bit_quant_type,    bnb_4bit_compute_dtype=compute_dtype,    bnb_4bit_use_double_quant=use_nested_quant,)# Check GPU compatibility with bfloat16if compute_dtype == torch.float16 and use_4bit:    major, _ = torch.cuda.get_device_capability()    if major >= 8:        print(\"=\" * 80)        print(\"Your GPU supports bfloat16: accelerate training with bf16=True\")        print(\"=\" * 80)# Load base modelmodel = AutoModelForCausalLM.from_pretrained(    model_name,    quantization_config=bnb_config,    device_map=device_map)model.config.use_cache = Falsemodel.config.pretraining_tp = 1# Load LLaMA tokenizertokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)tokenizer.pad_token = tokenizer.eos_tokentokenizer.padding_side = \"right\"# Load LoRA configurationpeft_config = LoraConfig(    lora_alpha=lora_alpha,    lora_dropout=lora_dropout,    r=lora_r,    bias=\"none\",    task_type=\"CAUSAL_LM\",)# Set training parameterstraining_arguments = TrainingArguments(    output_dir=output_dir,    num_train_epochs=num_train_epochs,    per_device_train_batch_size=per_device_train_batch_size,    gradient_accumulation_steps=gradient_accumulation_steps,    optim=optim,    save_steps=save_steps,    logging_steps=logging_steps,    learning_rate=learning_rate,    weight_decay=weight_decay,    fp16=fp16,    bf16=bf16,    max_grad_norm=max_grad_norm,    max_steps=max_steps,    warmup_ratio=warmup_ratio,    group_by_length=group_by_length,    lr_scheduler_type=lr_scheduler_type,    report_to=\"tensorboard\")# Set supervised fine-tuning parameterstrainer = SFTTrainer(    model=model,    train_dataset=dataset,    peft_config=peft_config,    dataset_text_field=\"text\",    max_seq_length=max_seq_length,    tokenizer=tokenizer,    args=training_arguments,    packing=packing,)# Train modeltrainer.train()# Save trained modeltrainer.model.save_pretrained(new_model)\n```\n\n![Image by author](https://assets.insightmediagroup.io/media/wp-content/uploads/2023/07/1gcOd4GGq20Cg8oxax-IYew.png)\nImage by author\nThe training can be very long, depending on the size of your dataset. Here, it took less than an hour on a T4 GPU. We can check the plots on tensorboard, as follows:\npython\n\n```\n%load_ext tensorboard%tensorboard --logdir results/runs\n```\n\n![Image by author](https://assets.insightmediagroup.io/media/wp-content/uploads/2023/07/11mUiX-A92ZbFFq0kjfsglw.png)\nImage by author\nLet's make sure that the model is behaving correctly. It would require a more exhaustive evaluation, but we can use the **text generation pipeline** to ask questions like \"What is a large language model?\" Note that I'm formatting the input to match Llama 2's prompt template.\npython\n\n```\n# Ignore warningslogging.set_verbosity(logging.CRITICAL)# Run text generation pipeline with our next modelprompt = \"What is a large language model?\"pipe = pipeline(task=\"text-generation\", model=model, tokenizer=tokenizer, max_length=200)result = pipe(f\"<s>[INST] {prompt} [/INST]\")print(result[0]['generated_text'])\n```\n\nThe model outputs the following response:\ntext\n\n```\nA large language model is a type of artificial intelligence that is trained on a large dataset of text to generate human-like language. It is typically trained on a dataset of text that is much larger than the dataset used for smaller language models. The large dataset allows the model to learn more complex patterns in language, which can result in more accurate and natural-sounding language generation.\n```\n\ntext\n\n```\nLarge language models are often used for tasks such as text summarization, language translation, and chatbots. They are also used for more complex tasks such as writing articles, generating creative content, and even composing music.\n```\n\ntext\n\n```\nLarge language models are trained using a technique called deep learning, which involves using many layers of artificial neural networks to learn complex patterns in the data. The model is trained on a large dataset of text, and the neural networks are trained to predict the next word in a sequence of text given\n```\n\nFrom experience, it is **very coherent** for a model with only 7 billion parameters. You can play with it and ask harder questions from evaluation datasets like [BigBench-Hard](https://github.com/suzgunmirac/BIG-Bench-Hard). Guanaco is an excellent dataset that has produced high-quality models in the past. You can train a Llama 2 model on the entire dataset using `[mlabonne/guanaco-llama2](https://huggingface.co/datasets/mlabonne/guanaco-llama2)`.\nHow can we store our new `llama-2-7b-miniguanaco` model now? We need to merge the weights from LoRA with the base model. Unfortunately, as far as I know, there is no straightforward way to do it: we need to reload the base model in FP16 precision and use the `peft` library to merge everything. Alas, it also creates a problem with the VRAM (despite emptying it), so I recommend **restarting the notebook** , re-executing the three first cells, and then executing the next one. Please contact me if you know a fix!\npython\n\n```\n# Reload model in FP16 and merge it with LoRA weightsbase_model = AutoModelForCausalLM.from_pretrained(    model_name,    low_cpu_mem_usage=True,    return_dict=True,    torch_dtype=torch.float16,    device_map=device_map,)model = PeftModel.from_pretrained(base_model, new_model)model = model.merge_and_unload()# Reload tokenizer to save ittokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)tokenizer.add_special_tokens({'pad_token': '[PAD]'})tokenizer.pad_token = tokenizer.eos_tokentokenizer.padding_side = \"right\"\n```\n\nOur weights are merged and we reloaded the tokenizer. We can now push everything to the Hugging Face Hub to save our model.\npython\n\n```\n!huggingface-cli loginmodel.push_to_hub(new_model, use_temp_dir=False)tokenizer.push_to_hub(new_model, use_temp_dir=False)\n```\n\nYou can now use this model for inference by loading it like any other Llama 2 model from the Hub. It is also possible to reload it for more fine-tuning - perhaps with another dataset?\nIf you're serious about fine-tuning models, **using a script** instead of a notebook is recommended. You can easily rent GPUs on Lambda Labs, Runpod, Vast.ai, for less than 0.3$/h. Once you're connected, you can install libraries, import your script, log in to Hugging Face and other tools (like Weights & Biases for logging your experiments), and start your fine-tuning.\nThe `trl` script is currently very limited, so I made my own based on the previous notebook. You can [**find it here on GitHub Gist**](https://gist.github.com/mlabonne/8eb9ad60c6340cb48a17385c68e3b1a5). If you're looking for a comprehensive solution, check out [Axolotl](https://github.com/OpenAccess-AI-Collective/axolotl) from the OpenAccess AI Collective, which also natively handles multiple datasets, Deepspeed, Flash Attention, etc.\n## Conclusion\nIn this article, we saw how to fine-tune a Llama 2 7b model using a Colab notebook. We introduced some necessary background on LLM training and fine-tuning, as well as important considerations related to instruction datasets. In the second section, we **successfully fine-tuned the Llama 2 model** with its native prompt template and custom parameters.\nThese fine-tuned models can then be integrated into LangChain and other architectures as advantageous alternatives to the OpenAI API. Remember, in this new paradigm, instruction datasets are the new gold, and the quality of your model heavily depends on the data on which it's been fine-tuned. So, good luck with building high-quality datasets!\nIf you're interested in more content about LLMs, follow me on Twitter [@maximelabonne](https://twitter.com/maximelabonne).\n## References\n  * Hugo Touvron, Thomas Scialom, et al. (2023). [Llama 2: Open Foundation and Fine-Tuned Chat Models](https://arxiv.org/abs/2307.09288).\n  * Philipp Schmid, Omar Sanseviero, Pedro Cuenca, & Lewis Tunstall. Llama 2 is here - get it on Hugging Face. <https://huggingface.co/blog/llama2>\n  * Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li, Carlos Guestrin, Percy Liang, & Tatsunori B. Hashimoto. (2023). [Stanford Alpaca: An Instruction-following LLaMA model](https://crfm.stanford.edu/2023/03/13/alpaca.html).\n  * Jacob Devlin, Ming-Wei Chang, Kenton Lee, & Kristina Toutanova. (2019). [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805).\n  * Tim Dettmers, Artidoro Pagnoni, Ari Holtzman, & Luke Zettlemoyer. (2023). [QLoRA: Efficient Finetuning of Quantized LLMs](https://arxiv.org/abs/2305.14314).\n\n\n## Related articles\n> [**4-bit Quantization with GPTQ**](https://towardsdatascience.com/4-bit-quantization-with-gptq-36b0f4f02c34)\n> [**Decoding Strategies in Large Language Models**](https://towardsdatascience.com/decoding-strategies-in-large-language-models-9733a8f70539)\n* * *\n_Learn more about machine learning and support my work with one click - become a Medium member here:_\n> [**Join Medium with my referral link - Maxime Labonne**](https://medium.com/@mlabonne/membership)\n_If you're already a member, you can_[ _follow me on Medium_](https://medium.com/@mlabonne) _._\n* * *\n![](https://assets.insightmediagroup.io/media/1787657499317_sxy3ci.png)\nWritten By\n## [Maxime Labonne](https://towardsdatascience.com/author/mlabonne/)\n[See all from Maxime Labonne](https://towardsdatascience.com/author/mlabonne/)\n[Artificial Intelligence](https://towardsdatascience.com/tag/artificial-intelligence/)[Data Science](https://towardsdatascience.com/tag/data-science/)[Hands On Tutorials](https://towardsdatascience.com/tag/hands-on-tutorials/)[Large Language Models](https://towardsdatascience.com/tag/large-language-models/)[Programming](https://towardsdatascience.com/tag/programming/)\nShare This Article\n  * [](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Ftowardsdatascience.com%2Ffine-tune-your-own-llama-2-model-in-a-colab-notebook-df9823a04a32%2F&title=Fine-Tune%20Your%20Own%20Llama%202%20Model%20in%20a%20Colab%20Notebook)\n  * [](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Ftowardsdatascience.com%2Ffine-tune-your-own-llama-2-model-in-a-colab-notebook-df9823a04a32%2F&title=Fine-Tune%20Your%20Own%20Llama%202%20Model%20in%20a%20Colab%20Notebook)\n  * [](https://x.com/share?url=https%3A%2F%2Ftowardsdatascience.com%2Ffine-tune-your-own-llama-2-model-in-a-colab-notebook-df9823a04a32%2F&text=Fine-Tune%20Your%20Own%20Llama%202%20Model%20in%20a%20Colab%20Notebook)\n\n\nTowards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.\n[Write for TDS](https://towardsdatascience.com/submissions/)\n## Related Articles\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2020/04/19gjKPb7BhB87KY-7fBxrAg.png)](https://towardsdatascience.com/google-colab-how-does-it-compare-to-a-gpu-enabled-laptop-851c1e0a2ca9/)\n## [Google Colab: How does it compare to a GPU-enabled laptop?](https://towardsdatascience.com/google-colab-how-does-it-compare-to-a-gpu-enabled-laptop-851c1e0a2ca9/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\nIntroduction to Colab, Runtimes, Performance Comparisons... and Gotchas\n[Dario Radečić](https://towardsdatascience.com/author/radecicdario/)\nApril 30, 20205 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2021/12/1rhV3URvLEWEfBWu4S07vCw-scaled.jpeg)](https://towardsdatascience.com/understanding-convolutional-neural-networks-cnns-81dffc813a69/)\n## [Understanding Convolutional Neural Networks (CNNs)](https://towardsdatascience.com/understanding-convolutional-neural-networks-cnns-81dffc813a69/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\nA gentle introduction to one of the most powerful deep learning tools and its building blocks\n[Vinicius Trevisan](https://towardsdatascience.com/author/vinicius_trevisan/)\nDecember 30, 20219 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2021/01/0omwi2P698g1omGb0-scaled.jpg)](https://towardsdatascience.com/8-data-science-project-ideas-from-kaggle-in-2021-83a3660e0342/)\n## [8 Data Science Project Ideas from Kaggle in 2021](https://towardsdatascience.com/8-data-science-project-ideas-from-kaggle-in-2021-83a3660e0342/)\n[Machine Learning](https://towardsdatascience.com/category/artificial-intelligence/machine-learning/)\nFinally, we are in year 2021 🎉 I've picked some interesting project ideas with extended readings for you to explore...\n[Cheng](https://towardsdatascience.com/author/kyoto-cheng/)\nJanuary 4, 20214 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2021/12/1xXggizQBtmoNLvC72m1JzQ-scaled.jpeg)](https://towardsdatascience.com/deepmind-is-now-the-undisputed-leader-in-language-ai-with-gopher-280b-79363106011f/)\n## [DeepMind Is Now the Undisputed Leader in Language AI with Gopher (280B)](https://towardsdatascience.com/deepmind-is-now-the-undisputed-leader-in-language-ai-with-gopher-280b-79363106011f/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\nVastly outperforming GPT-3, J1-Jumbo, and MT-NLG.\n[Alberto Romero](https://towardsdatascience.com/author/albertoromgar/)\nDecember 14, 202111 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2022/06/174iRQr3c1-zBnN3QVYIGkQ-scaled.jpeg)](https://towardsdatascience.com/classifying-music-genres-with-lightgbm-17662035cf4e/)\n## [Classifying Music Genres with LightGBM](https://towardsdatascience.com/classifying-music-genres-with-lightgbm-17662035cf4e/)\n[Machine Learning](https://towardsdatascience.com/category/artificial-intelligence/machine-learning/)\nOptuna Hyperparameter Optimization and Dimension Reduction\n[Louis Magowan](https://towardsdatascience.com/author/louismagowan42/)\nJune 27, 20223 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2020/10/1MkXejtNAt5HN2f6RgKk6Iw.png)](https://towardsdatascience.com/graphs-gradients-and-groupby-what-my-first-major-project-has-taught-me-a3c96561700a/)\n## [Graphs, Gradients, and Groupby](https://towardsdatascience.com/graphs-gradients-and-groupby-what-my-first-major-project-has-taught-me-a3c96561700a/)\n[Data Science](https://towardsdatascience.com/category/data-science/)\nWhat my first major Data Science project has taught me.\n[Diane Tunnicliffe](https://towardsdatascience.com/author/diane-tunnicliffe/)\nOctober 15, 202012 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2020/01/0iTV1CrvkwQ6EU8eG.jpg)](https://towardsdatascience.com/more-microsoft-data-science-interview-questions-and-answers-f9ee8337072c/)\n## [More Microsoft Data Science Interview Questions and Answers](https://towardsdatascience.com/more-microsoft-data-science-interview-questions-and-answers-f9ee8337072c/)\n[Machine Learning](https://towardsdatascience.com/category/artificial-intelligence/machine-learning/)\nAnother walkthrough of some data science questions from a Microsoft Interview\n[Terence Shin, MSc, MBA](https://towardsdatascience.com/author/terenceshin/)\nJanuary 20, 20208 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2020/10/06QNikD4u0fmshrde-scaled.jpg)](https://towardsdatascience.com/top-5-mistakes-companies-make-with-data-science-2d075831297c/)\n## [Top 5 Mistakes Companies Make with Data Science](https://towardsdatascience.com/top-5-mistakes-companies-make-with-data-science-2d075831297c/)\n[Data Science](https://towardsdatascience.com/category/data-science/)\nWhat to avoid in your journey towards data-driven decision-making\n[Tom Martin](https://towardsdatascience.com/author/tpgmartin/)\nOctober 8, 20204 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2023/11/1OVY32GUwlVqvSRCw9bXi0w.png)](https://towardsdatascience.com/putting-your-forecasting-model-to-the-test-a-guide-to-backtesting-24567d377fb5/)\n## [Putting Your Forecasting Model to the Test: A Guide to Backtesting](https://towardsdatascience.com/putting-your-forecasting-model-to-the-test-a-guide-to-backtesting-24567d377fb5/)\n[Data Science](https://towardsdatascience.com/category/data-science/)\nLearn how to properly evaluate the performance of time series models through backtesting\n[Eryk Lewinson](https://towardsdatascience.com/author/eryk-lewinson/)\nNovember 8, 20239 min read\n\n\n  * [YouTube](https://www.youtube.com/c/TowardsDataScience)\n  * [X](https://x.com/TDataScience)\n  * [LinkedIn](https://www.linkedin.com/company/towards-data-science/)\n  * [Threads](https://www.threads.net/@towardsdatascience)\n  * [Bluesky](https://bsky.app/profile/towardsdatascience.com)\n\n\n[](https://towardsdatascience.com/)\nYour home for data science and AI. The world's leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.\nSubscribe to Our Newsletter[Write For TDS](https://towardsdatascience.com/submissions/)•[About](https://towardsdatascience.com/about-towards-data-science-d691af11cc2f/)•[Advertise](https://contact.towardsdatascience.com/advertise-with-towards-data-science)•[Privacy Policy](https://towardsdatascience.com/privacy-policy/)•[Terms of Use](https://towardsdatascience.com/website-terms-of-use/)\n© Insight Media Group, LLC 2026•[The New Stack](https://thenewstack.io)•[roadmap.sh](https://roadmap.sh)•[Creator Network](https://insightmediagroup.io/creator-network)\n",
  "metadata": {
    "title": "Fine-Tune Your Own Llama 2 Model in a Colab Notebook | Towards Data Science",
    "description": "A practical introduction to LLM fine-tuning",
    "keywords": null,
    "author": "Maxime Labonne",
    "og:locale": "en_US",
    "og:type": "article",
    "og:title": "Fine-Tune Your Own Llama 2 Model in a Colab Notebook",
    "og:description": "A practical introduction to LLM fine-tuning",
    "og:url": "https://towardsdatascience.com/fine-tune-your-own-llama-2-model-in-a-colab-notebook-df9823a04a32/",
    "og:site_name": "Towards Data Science",
    "og:image": "https://assets.insightmediagroup.io/media/wp-content/uploads/2023/07/1jsksHuIVtuXDh-aLpwZDbw.png",
    "og:image:width": "1536",
    "og:image:height": "841",
    "og:image:type": "image/png",
    "twitter:card": "summary_large_image",
    "twitter:site": "@TDataScience",
    "twitter:creator": "@TDataScience",
    "twitter:label1": "Written by",
    "twitter:data1": "Maxime Labonne",
    "twitter:label2": "Est. reading time",
    "twitter:data2": "12 min read",
    "article:published_time": "2023-07-25T17:08:04.000Z",
    "article:modified_time": "2023-07-25T17:08:04.000Z",
    "article:author": "https://towardsdatascience.com/author/mlabonne/"
  },
  "paywall": {
    "listed_paid": true,
    "detected": false,
    "marker": "",
    "public_only": true,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "language": "txt",
      "file": "attachments/84bf1f9f302e/code/block_000.txt",
      "chars": 76
    },
    {
      "language": "txt",
      "file": "attachments/84bf1f9f302e/code/block_001.txt",
      "chars": 100
    },
    {
      "language": "txt",
      "file": "attachments/84bf1f9f302e/code/block_002.txt",
      "chars": 280
    },
    {
      "language": "txt",
      "file": "attachments/84bf1f9f302e/code/block_003.txt",
      "chars": 2825
    },
    {
      "language": "txt",
      "file": "attachments/84bf1f9f302e/code/block_004.txt",
      "chars": 2156
    },
    {
      "language": "txt",
      "file": "attachments/84bf1f9f302e/code/block_005.txt",
      "chars": 56
    },
    {
      "language": "txt",
      "file": "attachments/84bf1f9f302e/code/block_006.txt",
      "chars": 316
    },
    {
      "language": "txt",
      "file": "attachments/84bf1f9f302e/code/block_007.txt",
      "chars": 407
    },
    {
      "language": "txt",
      "file": "attachments/84bf1f9f302e/code/block_008.txt",
      "chars": 234
    },
    {
      "language": "txt",
      "file": "attachments/84bf1f9f302e/code/block_009.txt",
      "chars": 314
    },
    {
      "language": "txt",
      "file": "attachments/84bf1f9f302e/code/block_010.txt",
      "chars": 543
    },
    {
      "language": "txt",
      "file": "attachments/84bf1f9f302e/code/block_011.txt",
      "chars": 123
    }
  ],
  "images": [
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2023/07/1jsksHuIVtuXDh-aLpwZDbw.png",
      "alt": "Image by author",
      "local_path": "attachments/84bf1f9f302e/images/000.png",
      "bytes": 1941234,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2023/07/1DsOt1rVHLkPhVnhSSY20tA.png",
      "alt": "Image by author",
      "local_path": "attachments/84bf1f9f302e/images/001.png",
      "bytes": 685619,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2023/07/1gcOd4GGq20Cg8oxax-IYew.png",
      "alt": "Image by author",
      "local_path": "attachments/84bf1f9f302e/images/002.png",
      "bytes": 11661,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2023/07/11mUiX-A92ZbFFq0kjfsglw.png",
      "alt": "Image by author",
      "local_path": "attachments/84bf1f9f302e/images/003.png",
      "bytes": 60404,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2020/04/19gjKPb7BhB87KY-7fBxrAg.png",
      "alt": "",
      "local_path": "attachments/84bf1f9f302e/images/004.png",
      "bytes": 16502,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2021/12/1rhV3URvLEWEfBWu4S07vCw-scaled.jpeg",
      "alt": "",
      "local_path": "attachments/84bf1f9f302e/images/005.jpeg",
      "bytes": 622689,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2021/01/0omwi2P698g1omGb0-scaled.jpg",
      "alt": "",
      "local_path": "attachments/84bf1f9f302e/images/006.jpg",
      "bytes": 765148,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2022/06/174iRQr3c1-zBnN3QVYIGkQ-scaled.jpeg",
      "alt": "",
      "local_path": "attachments/84bf1f9f302e/images/007.jpeg",
      "bytes": 465131,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2020/10/1MkXejtNAt5HN2f6RgKk6Iw.png",
      "alt": "",
      "local_path": "attachments/84bf1f9f302e/images/008.png",
      "bytes": 29663,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2023/11/1OVY32GUwlVqvSRCw9bXi0w.png",
      "alt": "",
      "local_path": "attachments/84bf1f9f302e/images/009.png",
      "bytes": 2047515,
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [
    {
      "src": "https://www.youtube.com/c/TowardsDataScience",
      "quality": "unavailable",
      "local_path": "",
      "note": "无1080p且无720p，未交付更低清晰度"
    }
  ],
  "links": [
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/latest/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/editors-pick/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/the-variable/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/shipai/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/submissions/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/category/artificial-intelligence/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/author/mlabonne/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/4-bit-quantization-with-gptq-36b0f4f02c34"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/decoding-strategies-in-large-language-models-9733a8f70539"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/artificial-intelligence/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/data-science/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/hands-on-tutorials/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/large-language-models/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/programming/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/google-colab-how-does-it-compare-to-a-gpu-enabled-laptop-851c1e0a2ca9/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/author/radecicdario/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/understanding-convolutional-neural-networks-cnns-81dffc813a69/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/author/vinicius_trevisan/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/8-data-science-project-ideas-from-kaggle-in-2021-83a3660e0342/"
    },
    {
      "type": "external",
      "href": "https://write.insightmediagroup.io"
    },
    {
      "type": "external",
      "href": "https://www.linkedin.com/company/towards-data-science/"
    },
    {
      "type": "external",
      "href": "https://x.com/TDataScience"
    },
    {
      "type": "external",
      "href": "https://github.com/tatsu-lab/stanford_alpaca"
    },
    {
      "type": "external",
      "href": "https://huggingface.co/lmsys/vicuna-13b-v1.3"
    },
    {
      "type": "external",
      "href": "https://huggingface.co/WizardLM/WizardLM-13B-V1.1"
    },
    {
      "type": "external",
      "href": "https://github.com/openlm-research/open_llama"
    },
    {
      "type": "external",
      "href": "https://falconllm.tii.ae/"
    },
    {
      "type": "external",
      "href": "https://github.com/salesforce/xgen"
    },
    {
      "type": "external",
      "href": "https://python.langchain.com/docs/get_started/introduction.html"
    },
    {
      "type": "external",
      "href": "https://www.llamaindex.ai/"
    },
    {
      "type": "external",
      "href": "https://colab.research.google.com/drive/1PEQyJO1-f6j0S_XJ8DV50NkpzasXkrzd?usp=sharing"
    },
    {
      "type": "external",
      "href": "https://github.com/mlabonne/llm-course"
    },
    {
      "type": "external",
      "href": "https://roadmap.sh/ai-engineer"
    },
    {
      "type": "external",
      "href": "https://roadmap.sh/ai-data-scientist"
    },
    {
      "type": "external",
      "href": "https://roadmap.sh/computer-science"
    },
    {
      "type": "external",
      "href": "https://arxiv.org/abs/2307.09288"
    },
    {
      "type": "external",
      "href": "https://arxiv.org/abs/1810.04805"
    },
    {
      "type": "external",
      "href": "https://github.com/facebookresearch/metaseq/blob/main/projects/OPT/chronicles/OPT175B_Logbook.pdf"
    },
    {
      "type": "external",
      "href": "https://arxiv.org/abs/1707.06347"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T11:11:48Z",
  "source_url": "https://towardsdatascience.com/fine-tune-your-own-llama-2-model-in-a-colab-notebook-df9823a04a32/",
  "delivery_status": "excluded_paid",
  "discovery_status": "excluded_paid",
  "missing_reason": "按原约定排除付费/会员全文"
}