{
  "source_file": "0930",
  "source_platform": "Medium",
  "author": "Maxime Labonne",
  "title_listed": "Fine-Tune Llama 3.1 Ultra-Efficiently with Unsloth",
  "keywords": "微调,Unsloth,SFT",
  "domain": "大模型算法",
  "content_type": "深度技术解析",
  "is_paid_flag": "部分免费/会员墙",
  "url": "https://towardsdatascience.com/fine-tune-llama-3-1-ultra-efficiently-with-unsloth-7196c7165bab/",
  "publish_date": "2024-07",
  "corpus_stage": "预训练/SFT",
  "notes": "",
  "id": "294830feb6f9",
  "collected_at": "2026-10-01T21:47:53",
  "title": "Fine-Tune Llama 3.1 Ultra-Efficiently with Unsloth | 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[Programming](https://towardsdatascience.com/category/programming/)\n# Fine-Tune Llama 3.1 Ultra-Efficiently with Unsloth\nA beginner's guide to state-of-the-art supervised fine-tuning\n[Maxime Labonne](https://towardsdatascience.com/author/mlabonne/)\nJuly 29, 202412 min read\n### _A beginner's guide to state-of-the-art supervised fine-tuning_\n![Image generated with DALL-E 3 by author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/07/0204gvY4y_NVT-Kd4.jpeg)\nImage generated with DALL-E 3 by author\nThe recent release of Llama 3.1 offers models with an incredible level of performance, closing the gap between closed-source and open-weight models. Instead of using frozen, general-purpose LLMs like GPT-4o and Claude 3.5, you can fine-tune Llama 3.1 for your specific use cases to achieve better performance and customizability at a lower cost.\n![Image by author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/07/01tpK40n6bj_kRKOR.png)\nImage by author\nIn this article, we will provide a comprehensive overview of supervised fine-tuning. We will compare it to prompt engineering to understand when it makes sense to use it, detail the main techniques with their pros and cons, and introduce major concepts, such as LoRA hyperparameters, storage formats, and chat templates. Finally, we will implement it in practice by fine-tuning Llama 3.1 8B in Google Colab with state-of-the-art optimization using Unsloth.\nAll the code used in this article is available on [Google Colab](https://colab.research.google.com/drive/164cg_O7SV7G8kZr_JXqLd6VC7pd86-1Z?usp=sharing) and in the [LLM Course](https://github.com/mlabonne/llm-course).\n## 🔧 Supervised Fine-Tuning\n![Image by author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/07/0g4lKCyFKV_BxlDv_.png)\nImage by author\nSupervised Fine-Tuning (SFT) is a method to **improve and customize** pre-trained LLMs. It involves retraining base models on a smaller dataset of instructions and answers. The main goal is to transform a basic model that predicts text into an assistant that can follow instructions and answer questions. SFT can also enhance the model's overall performance, add new knowledge, or adapt it to specific tasks and domains. Fine-tuned models can then go through an optional preference alignment stage (see [my article about DPO](https://mlabonne.github.io/blog/posts/Fine_tune_Mistral_7b_with_DPO.html)) to remove unwanted responses, modify their style, and more.\nThe following figure shows an instruction sample. It includes a system prompt to steer the model, a user prompt to provide a task, and the output the model is expected to generate. You can find a list of high-quality open-source instruction datasets in the [💾 LLM Datasets](https://github.com/mlabonne/llm-datasets) GitHub repo.\n![Image by author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/07/0aaxURA0K2PFyoe1J.png)\nImage by author\nBefore considering SFT, I recommend trying prompt engineering techniques like **few-shot prompting** or **retrieval augmented generation** (RAG). In practice, these methods can solve many problems without the need for fine-tuning, using either closed-source or open-weight models (e.g., Llama 3.1 Instruct). If this approach doesn't meet your objectives (in terms of quality, cost, latency, etc.), then SFT becomes a viable option when instruction data is available. Note that SFT also offers benefits like additional control and customizability to create personalized LLMs.\nHowever, SFT has limitations. It works best when leveraging knowledge already present in the base model. Learning completely new information like an unknown language can be challenging and lead to more frequent hallucinations. For new domains unknown to the base model, it is recommended to continuously pre-train it on a raw dataset first.\nOn the opposite end of the spectrum, instruct models (i.e., already fine-tuned models) can already be very close to your requirements. For example, a model might perform very well but state that it was trained by OpenAI or Meta instead of you. In this case, you might want to slightly steer the instruct model's behavior using preference alignment. By providing chosen and rejected samples for a small set of instructions (between 100 and 1000 samples), you can force the LLM to say that you trained it instead of OpenAI.\n## ⚖️ SFT Techniques\nThe three most popular SFT techniques are full fine-tuning, LoRA, and QLoRA.\n![Image by author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/07/0nidQUa3pO20RW3KI.png)\nImage by author\n**Full fine-tuning** is the most straightforward SFT technique. It involves retraining all parameters of a pre-trained model on an instruction dataset. This method often provides the best results but requires significant computational resources (several high-end GPUs are required to fine-tune a 8B model). Because it modifies the entire model, it is also the most destructive method and can lead to the catastrophic forgetting of previous skills and knowledge.\n**Low-Rank Adaptation (LoRA)** is a popular parameter-efficient fine-tuning technique. Instead of retraining the entire model, it freezes the weights and introduces small adapters (low-rank matrices) at each targeted layer. This allows LoRA to train a number of parameters that is drastically lower than full fine-tuning (less than 1%), reducing both memory usage and training time. This method is non-destructive since the original parameters are frozen, and adapters can then be switched or combined at will.\n**QLoRA (Quantization-aware Low-Rank Adaptation)** is an extension of LoRA that offers even greater memory savings. It provides up to 33% additional memory reduction compared to standard LoRA, making it particularly useful when GPU memory is constrained. This increased efficiency comes at the cost of longer training times, with QLoRA typically taking about 39% more time to train than regular LoRA.\nWhile QLoRA requires more training time, its substantial memory savings can make it the only viable option in scenarios where GPU memory is limited. For this reason, this is the technique we will use in the next section to fine-tune a Llama 3.1 8B model on Google Colab.\n## 🦙 Fine-Tune Llama 3.1 8B\nTo efficiently fine-tune a [Llama 3.1 8B](https://huggingface.co/meta-llama/Meta-Llama-3.1-8B) model, we'll use the [Unsloth](https://github.com/unslothai/unsloth) library by Daniel and Michael Han. Thanks to its custom kernels, Unsloth provides 2x faster training and 60% memory use compared to other options, making it ideal in a constrained environment like Colab. Unfortunately, Unsloth only supports single-GPU settings at the moment. For multi-GPU settings, I recommend popular alternatives like [TRL](https://huggingface.co/docs/trl/en/index) and [Axolotl](https://github.com/OpenAccess-AI-Collective/axolotl) (both also include Unsloth as a backend).\nIn this example, we will QLoRA fine-tune it on the [mlabonne/FineTome-100k](https://huggingface.co/datasets/mlabonne/FineTome-100k) dataset. It's a subset of [arcee-ai/The-Tome](https://huggingface.co/datasets/arcee-ai/The-Tome) (without [arcee-ai/qwen2–72b-magpie-en](https://huggingface.co/datasets/arcee-ai/qwen2-72b-magpie-en)) that I re-filtered using [HuggingFaceFW/fineweb-edu-classifier](https://huggingface.co/HuggingFaceFW/fineweb-edu-classifier). Note that this classifier wasn't designed for instruction data quality evaluation, but we can use it as a rough proxy. The resulting FineTome is an ultra-high quality dataset that includes conversations, reasoning problems, function calling, and more.\nLet's start by installing all the required libraries.\npython\n\n```\n!pip install \"unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git\"!pip install --no-deps \"xformers<0.0.27\" \"trl<0.9.0\" peft accelerate bitsandbytes\n```\n\nOnce installed, we can import them as follows.\npython\n\n```\nimport torchfrom trl import SFTTrainerfrom datasets import load_datasetfrom transformers import TrainingArguments, TextStreamerfrom unsloth.chat_templates import get_chat_templatefrom unsloth import FastLanguageModel, is_bfloat16_supported\n```\n\nLet's now load the model. Since we want to use QLoRA, I chose the pre-quantized [unsloth/Meta-Llama-3.1–8B-bnb-4bit](https://huggingface.co/unsloth/Meta-Llama-3.1-8B-bnb-4bit). This 4-bit precision version of [meta-llama/Meta-Llama-3.1–8B](https://markdown-to-medium.surge.sh/meta-llama/Meta-Llama-3.1-8B) is significantly smaller (5.4 GB) and faster to download compared to the original 16-bit precision model (16 GB). We load in NF4 format using the bitsandbytes library.\nWhen loading the model, we must specify a maximum sequence length, which restricts its context window. Llama 3.1 supports up to 128k context length, but we will set it to 2,048 in this example since it consumes more compute and VRAM. Finally, the `dtype` parameter automatically detects if your GPU supports the [BF16 format](https://mlabonne.github.io/blog/posts/Introduction_to_Weight_Quantization.html#background-on-floating-point-representation) for more stability during training (this feature is restricted to Ampere and more recent GPUs).\npython\n\n```\nmax_seq_length = 2048model, tokenizer = FastLanguageModel.from_pretrained(    model_name=\"unsloth/Meta-Llama-3.1-8B-bnb-4bit\",    max_seq_length=max_seq_length,    load_in_4bit=True,    dtype=None,)\n```\n\nNow that our model is loaded in 4-bit precision, we want to prepare it for parameter-efficient fine-tuning with LoRA adapters. LoRA has three important parameters:\n  * **Rank** (r), which determines LoRA matrix size. Rank typically starts at 8 but can go up to 256. Higher ranks can store more information but increase the computational and memory cost of LoRA. We set it to 16 here.\n  * **Alpha** (α), a scaling factor for updates. Alpha directly impacts the adapters' contribution and is often set to 1x or 2x the rank value.\n  * **Target modules** : LoRA can be applied to various model components, including attention mechanisms (Q, K, V matrices), output projections, feed-forward blocks, and linear output layers. While initially focused on attention mechanisms, extending LoRA to other components has shown benefits. However, adapting more modules increases the number of trainable parameters and memory needs.\n\n\nHere, we set r=16, α=16, and target every linear module to maximize quality. We don't use dropout and biases for faster training.\nIn addition, we will use [Rank-Stabilized LoRA](https://arxiv.org/abs/2312.03732) (rsLoRA), which modifies the scaling factor of LoRA adapters to be proportional to 1/√r instead of 1/r. This stabilizes learning (especially for higher adapter ranks) and allows for improved fine-tuning performance as rank increases. Gradient checkpointing is handled by Unsloth to offload input and output embeddings to disk and save VRAM.\npython\n\n```\nmodel = FastLanguageModel.get_peft_model(    model,    r=16,    lora_alpha=16,    lora_dropout=0,    target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"up_proj\", \"down_proj\", \"o_proj\", \"gate_proj\"],     use_rslora=True,    use_gradient_checkpointing=\"unsloth\")\n```\n\nWith this LoRA configuration, we'll only train 42 million out of 8 billion parameters (0.5196%). This shows how much more efficient LoRA is compared to full fine-tuning.\nLet's now load and prepare our dataset. Instruction datasets are stored in a **particular format** : it can be Alpaca, ShareGPT, OpenAI, etc. First, we want to parse this format to retrieve our instructions and answers. Our [mlabonne/FineTome-100k](https://huggingface.co/datasets/mlabonne/FineTome-100k) dataset uses the ShareGPT format with a unique \"conversations\" column containing messages in JSONL. Unlike simpler formats like Alpaca, ShareGPT is ideal for storing multi-turn conversations, which is closer to how users interact with LLMs.\nOnce our instruction-answer pairs are parsed, we want to reformat them to follow a **chat template**. Chat templates are a way to structure conversations between users and models. They typically include special tokens to identify the beginning and the end of a message, who's speaking, etc. Base models don't have chat templates so we can choose any: ChatML, Llama3, Mistral, etc. In the open-source community, the ChatML template (originally from OpenAI) is a popular option. It simply adds two special tokens (`<|im_start|>` and `<|im_end|>`) to indicate who's speaking.\nIf we apply this template to the previous instruction sample, here's what we get:\ntext\n\n```\n<|im_start|>systemYou are a helpful assistant, who always provide explanation. Think like you are answering to a five year old.<|im_end|><|im_start|>userRemove the spaces from the following sentence: It prevents users to suspect that there are some hidden products installed on theirs device.<|im_end|><|im_start|>assistantItpreventsuserstosuspectthattherearesomehiddenproductsinstalledontheirsdevice.<|im_end|>\n```\n\nIn the following code block, we parse our ShareGPT dataset with the `mapping` parameter and include the ChatML template. We then load and process the entire dataset to apply the chat template to every conversation.\npython\n\n```\ntokenizer = get_chat_template(    tokenizer,    mapping={\"role\": \"from\", \"content\": \"value\", \"user\": \"human\", \"assistant\": \"gpt\"},    chat_template=\"chatml\",)def apply_template(examples):    messages = examples[\"conversations\"]    text = [tokenizer.apply_chat_template(message, tokenize=False, add_generation_prompt=False) for message in messages]    return {\"text\": text}dataset = load_dataset(\"mlabonne/FineTome-100k\", split=\"train\")dataset = dataset.map(apply_template, batched=True)\n```\n\nWe're now ready to specify the training parameters for our run. I want to briefly introduce the most important hyperparameters:\n  * **Learning rate** : It controls how strongly the model updates its parameters. Too low, and training will be slow and may get stuck in local minima. Too high, and training may become unstable or diverge, which degrades performance.\n  * **LR scheduler** : It adjusts the learning rate (LR) during training, starting with a higher LR for rapid initial progress and then decreasing it in later stages. Linear and cosine schedulers are the two most common options.\n  * **Batch size** : Number of samples processed before the weights are updated. Larger batch sizes generally lead to more stable gradient estimates and can improve training speed, but they also require more memory. Gradient accumulation allows for effectively larger batch sizes by accumulating gradients over multiple forward/backward passes before updating the model.\n  * **Num epochs** : The number of complete passes through the training dataset. More epochs allow the model to see the data more times, potentially leading to better performance. However, too many epochs can cause overfitting.\n  * **Optimizer** : Algorithm used to adjust the parameters of a model to minimize the loss function. In practice, AdamW 8-bit is strongly recommended: it performs as well as the 32-bit version while using less GPU memory. The paged version of AdamW is only interesting in distributed settings.\n  * **Weight decay** : A regularization technique that adds a penalty for large weights to the loss function. It helps prevent overfitting by encouraging the model to learn simpler, more generalizable features. However, too much weight decay can impede learning.\n  * **Warmup steps** : A period at the beginning of training where the learning rate is gradually increased from a small value to the initial learning rate. Warmup can help stabilize early training, especially with large learning rates or batch sizes, by allowing the model to adjust to the data distribution before making large updates.\n  * **Packing** : Batches have a pre-defined sequence length. Instead of assigning one batch per sample, we can combine multiple small samples in one batch, increasing efficiency.\n\n\nI trained the model on the entire dataset (100k samples) using an A100 GPU (40 GB of VRAM) on Google Colab. The training took 4 hours and 45 minutes. Of course, you can use smaller GPUs with less VRAM and a smaller batch size, but they're not nearly as fast. For example, it takes roughly 19 hours and 40 minutes on an L4 and a whopping 47 hours on a free T4.\nIn this case, I recommend only loading a subset of the dataset to speed up training. You can do it by modifying the previous code block, like `dataset = load_dataset(\"mlabonne/FineTome-100k\", split=\"train[:10000]\")` to only load 10k samples. Alternatively, you can use cheaper cloud GPU providers like Paperspace, RunPod, or Lambda Labs.\npython\n\n```\ntrainer=SFTTrainer(    model=model,    tokenizer=tokenizer,    train_dataset=dataset,    dataset_text_field=\"text\",    max_seq_length=max_seq_length,    dataset_num_proc=2,    packing=True,    args=TrainingArguments(        learning_rate=3e-4,        lr_scheduler_type=\"linear\",        per_device_train_batch_size=4,        gradient_accumulation_steps=4,        num_train_epochs=1,        fp16=not is_bfloat16_supported(),        bf16=is_bfloat16_supported(),        logging_steps=1,        optim=\"adamw_8bit\",        weight_decay=0.01,        warmup_steps=10,        output_dir=\"output\",        seed=0,    ),)trainer.train()\n```\n\nNow that the model is trained, let's test it with a simple prompt. This is not a rigorous evaluation but just a quick check to detect potential issues. We use `FastLanguageModel.for_inference()` to get 2x faster inference.\npython\n\n```\nmodel = FastLanguageModel.for_inference(model)messages = [    {\"from\": \"human\", \"value\": \"Is 9.11 larger than 9.9?\"},]inputs = tokenizer.apply_chat_template(    messages,    tokenize=True,    add_generation_prompt=True,    return_tensors=\"pt\",).to(\"cuda\")text_streamer = TextStreamer(tokenizer)_ = model.generate(input_ids=inputs, streamer=text_streamer, max_new_tokens=128, use_cache=True)\n```\n\nThe model's response is \"9.9\", which is correct!\nLet's now save our trained model. If you remember the part about LoRA and QLoRA, what we trained is not the model itself but a set of adapters. There are three save methods in Unsloth: `lora` to only save the adapters, and `merged_16bit`/`merged_4bit` to merge the adapters with the model in 16-bit/ 4-bit precision.\nIn the following, we merge them in 16-bit precision to maximize the quality. We first save it locally in the \"model\" directory and then upload it to the Hugging Face Hub. You can find the trained model on [mlabonne/FineLlama-3.1–8B](https://huggingface.co/mlabonne/FineLlama-3.1-8B).\npython\n\n```\nmodel.save_pretrained_merged(\"model\", tokenizer, save_method=\"merged_16bit\")model.push_to_hub_merged(\"mlabonne/FineLlama-3.1-8B\", tokenizer, save_method=\"merged_16bit\")\n```\n\nUnsloth also allows you to directly convert your model into GGUF format. This is a quantization format created for llama.cpp and compatible with most inference engines, like [LM Studio](https://lmstudio.ai/), [Ollama](https://ollama.com/), and oobabooga's [text-generation-webui](https://github.com/oobabooga/text-generation-webui). Since you can specify different precisions (see [my article about GGUF and llama.cpp](https://mlabonne.github.io/blog/posts/Quantize_Llama_2_models_using_ggml.html)), we'll loop over a list to quantize it in `q2_k`, `q3_k_m`, `q4_k_m`, `q5_k_m`, `q6_k`, `q8_0` and upload these quants on Hugging Face. The [mlabonne/FineLlama-3.1-8B-GGUF](https://huggingface.co/mlabonne/FineLlama-3.1-8B-GGUF) contains all our GGUFs.\npython\n\n```\nquant_methods = [\"q2_k\", \"q3_k_m\", \"q4_k_m\", \"q5_k_m\", \"q6_k\", \"q8_0\"]for quant in quant_methods:    model.push_to_hub_gguf(\"mlabonne/FineLlama-3.1-8B-GGUF\", tokenizer, quant)\n```\n\nCongratulations, we fine-tuned a model from scratch and uploaded quants you can now use in your favorite inference engine. Feel free to try the final model available on [mlabonne/FineLlama-3.1–8B-GGUF](https://huggingface.co/mlabonne/FineLlama-3.1-8B-GGUF). What to do now? Here are some ideas on how to use your model:\n  * **Evaluate** it on the [Open LLM Leaderboard](https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard) (you can submit it for free) or using other evals like in [LLM AutoEval](https://github.com/mlabonne/llm-autoeval).\n  * **Align** it with Direct Preference Optimization using a preference dataset like [mlabonne/orpo-dpo-mix-40k](https://huggingface.co/datasets/mlabonne/orpo-dpo-mix-40k) to boost performance.\n  * **Quantize** it in other formats like EXL2, AWQ, GPTQ, or HQQ for faster inference or lower precision using [AutoQuant](https://colab.research.google.com/drive/1b6nqC7UZVt8bx4MksX7s656GXPM-eWw4?usp=sharing).\n  * **Deploy** it on a Hugging Face Space with [ZeroChat](https://colab.research.google.com/drive/1LcVUW5wsJTO2NGmozjji5CkC--646LgC) for models that have been sufficiently trained to follow a chat template (~20k samples).\n\n\n## Conclusion\nThis article provided a comprehensive overview of supervised fine-tuning and how to apply it in practice to a Llama 3.1 8B model. By leveraging QLoRA's efficient memory usage, we managed to fine-tune an 8B LLM on a super high-quality dataset with limited GPU resources. We also provided more efficient alternatives for bigger runs and suggestions for further steps, including evaluation, preference alignment, quantization, and deployment.\nI hope this guide was useful. If you're interested in learning more about LLMs, I recommend checking the [LLM Course](https://github.com/mlabonne/llm-course). If you enjoyed this article, follow me on X [@maximelabonne](https://x.com/maximelabonne) and on Hugging Face [@mlabonne](https://huggingface.co/mlabonne). Good luck fine-tuning models!\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[Hands On Tutorials](https://towardsdatascience.com/tag/hands-on-tutorials/)[Large Language Models](https://towardsdatascience.com/tag/large-language-models/)[Machine Learning](https://towardsdatascience.com/tag/machine-learning/)[Programming](https://towardsdatascience.com/tag/programming/)[Technology](https://towardsdatascience.com/tag/technology/)\nShare This Article\n  * [](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Ftowardsdatascience.com%2Ffine-tune-llama-3-1-ultra-efficiently-with-unsloth-7196c7165bab%2F&title=Fine-Tune%20Llama%203.1%20Ultra-Efficiently%20with%20Unsloth)\n  * [](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Ftowardsdatascience.com%2Ffine-tune-llama-3-1-ultra-efficiently-with-unsloth-7196c7165bab%2F&title=Fine-Tune%20Llama%203.1%20Ultra-Efficiently%20with%20Unsloth)\n  * [](https://x.com/share?url=https%3A%2F%2Ftowardsdatascience.com%2Ffine-tune-llama-3-1-ultra-efficiently-with-unsloth-7196c7165bab%2F&text=Fine-Tune%20Llama%203.1%20Ultra-Efficiently%20with%20Unsloth)\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/2024/09/1fR9s-aV6bRjTaU8WR-qm4g.png)](https://towardsdatascience.com/calibrating-classification-probabilities-the-right-way-da935caee18d/)\n## [Calibrating Classification Probabilities the Right Way](https://towardsdatascience.com/calibrating-classification-probabilities-the-right-way-da935caee18d/)\n[Machine Learning](https://towardsdatascience.com/category/artificial-intelligence/machine-learning/)\nOr why you should not trust predict_proba methods\n[Jonte Dancker](https://towardsdatascience.com/author/jodancker/)\nSeptember 18, 20248 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2020/10/1r8jGQPI80_uLd1BcqNwGFQ-scaled.jpeg)](https://towardsdatascience.com/business-intelligence-visualizations-with-python-part-2-92f8a8463026/)\n## [Business Intelligence Visualizations with Python - Part 2](https://towardsdatascience.com/business-intelligence-visualizations-with-python-part-2-92f8a8463026/)\n[Programming](https://towardsdatascience.com/category/programming/)\nAs a follow up of the first article, I prepared an extensive guide to display customized features to the already introduced plots.\n[Julian Herrera](https://towardsdatascience.com/author/julianh/)\nOctober 8, 202010 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2022/05/0_OD79hy5cwDRMI8m-scaled.jpg)](https://towardsdatascience.com/a-guide-to-pythons-dunder-methods-3b8104fce335/)\n## [A Guide To Python's Dunder Methods](https://towardsdatascience.com/a-guide-to-pythons-dunder-methods-3b8104fce335/)\n[Programming](https://towardsdatascience.com/category/programming/)\nThe Magic Behind Python\n[Kurtis Pykes](https://towardsdatascience.com/author/kurtispykes/)\nMay 7, 20225 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2019/10/1f7YOaE4TWubwaFF7Z1fzNw.png)](https://towardsdatascience.com/unet-line-by-line-explanation-9b191c76baf5/)\n## [UNet Line by Line Explanation](https://towardsdatascience.com/unet-line-by-line-explanation-9b191c76baf5/)\n[Machine Learning](https://towardsdatascience.com/category/artificial-intelligence/machine-learning/)\nExample UNet Implementation\n[Jeremy Zhang](https://towardsdatascience.com/author/meatba11/)\nOctober 18, 20194 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2021/10/1zU5XcxCMLm81mwkN660V9A.gif)](https://towardsdatascience.com/svm-talking-maths-formulating-support-vector-machine-as-a-quadratic-programming-problem-ab5d30a8d73e/)\n## [SVM Talking maths: Formulating Support Vector Machine as a Quadratic Programming problem](https://towardsdatascience.com/svm-talking-maths-formulating-support-vector-machine-as-a-quadratic-programming-problem-ab5d30a8d73e/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\nFrom the idea of separating hyperplane to the final quadratic program in 4 steps\n[Maria](https://towardsdatascience.com/author/marialavrovskaya/)\nOctober 7, 20216 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2025/07/logo2.jpg)](https://towardsdatascience.com/exploratory-data-analysis-gamma-spectroscopy-in-python-part-2/)\n## [Exploratory Data Analysis: Gamma Spectroscopy in Python (Part 2)](https://towardsdatascience.com/exploratory-data-analysis-gamma-spectroscopy-in-python-part-2/)\n[Machine Learning](https://towardsdatascience.com/category/artificial-intelligence/machine-learning/)\nLet’s observe the matter on the atomic level\n[Dmitrii Eliuseev](https://towardsdatascience.com/author/dmitryelj/)\nJuly 18, 202517 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/07/1MwkkYIz3xkpev7qi6wqHNw.png)](https://towardsdatascience.com/handling-hierarchies-in-dimensional-modeling-176156f20f61/)\n## [Handling Hierarchies in Dimensional Modeling](https://towardsdatascience.com/handling-hierarchies-in-dimensional-modeling-176156f20f61/)\n[Data Engineering](https://towardsdatascience.com/category/data-science/data-engineering/)\nHierarchies play a crucial role in dimensional modeling for Data Warehouses. See my recommendations on how to handle them effectively.\n[Krzysztof K. Zdeb](https://towardsdatascience.com/author/krzysztof.kornel/)\nJuly 24, 202414 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2021/02/0Nsdyg6z5cNb2kTab-scaled.jpg)](https://towardsdatascience.com/discrete-random-variables-and-pmfs-explained-using-python-23b15948f9bc/)\n## [Discrete random variables and PMFs explained using Python](https://towardsdatascience.com/discrete-random-variables-and-pmfs-explained-using-python-23b15948f9bc/)\n[Machine Learning](https://towardsdatascience.com/category/artificial-intelligence/machine-learning/)\nLearn how to compute the Probability Mass Function (PMF) and use it to compute probabilities\n[Cody Mazza-Anthony](https://towardsdatascience.com/author/cmazzaanthony/)\nFebruary 20, 20214 min read\n  * [![](https://towardsdatascience.com/images/fallback-image.jpg)](https://towardsdatascience.com/an-introduction-to-graph-neural-networks-part-1-57335bca9a79/)\n## [An Introduction to Graph Neural Networks (Part 1)](https://towardsdatascience.com/an-introduction-to-graph-neural-networks-part-1-57335bca9a79/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\nGraph Neural Networks and their applications\n[Arun](https://towardsdatascience.com/author/cleanpegasus/)\nOctober 14, 20204 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 Llama 3.1 Ultra-Efficiently with Unsloth | Towards Data Science",
    "description": "A beginner's guide to state-of-the-art supervised fine-tuning",
    "keywords": null,
    "author": "Maxime Labonne",
    "og:locale": "en_US",
    "og:type": "article",
    "og:title": "Fine-Tune Llama 3.1 Ultra-Efficiently with Unsloth",
    "og:description": "A beginner's guide to state-of-the-art supervised fine-tuning",
    "og:url": "https://towardsdatascience.com/fine-tune-llama-3-1-ultra-efficiently-with-unsloth-7196c7165bab/",
    "og:site_name": "Towards Data Science",
    "og:image": "https://assets.insightmediagroup.io/media/wp-content/uploads/2024/07/0204gvY4y_NVT-Kd4.jpeg",
    "og:image:width": "1792",
    "og:image:height": "1024",
    "og:image:type": "image/jpeg",
    "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": "2024-07-29T08:39:30.000Z",
    "article:modified_time": "2024-07-29T08:39:30.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/294830feb6f9/code/block_000.txt",
      "chars": 162
    },
    {
      "language": "txt",
      "file": "attachments/294830feb6f9/code/block_001.txt",
      "chars": 240
    },
    {
      "language": "txt",
      "file": "attachments/294830feb6f9/code/block_002.txt",
      "chars": 199
    },
    {
      "language": "txt",
      "file": "attachments/294830feb6f9/code/block_003.txt",
      "chars": 257
    },
    {
      "language": "txt",
      "file": "attachments/294830feb6f9/code/block_004.txt",
      "chars": 412
    },
    {
      "language": "txt",
      "file": "attachments/294830feb6f9/code/block_005.txt",
      "chars": 487
    },
    {
      "language": "txt",
      "file": "attachments/294830feb6f9/code/block_006.txt",
      "chars": 626
    },
    {
      "language": "txt",
      "file": "attachments/294830feb6f9/code/block_007.txt",
      "chars": 391
    },
    {
      "language": "txt",
      "file": "attachments/294830feb6f9/code/block_008.txt",
      "chars": 169
    },
    {
      "language": "txt",
      "file": "attachments/294830feb6f9/code/block_009.txt",
      "chars": 176
    }
  ],
  "images": [
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2024/07/0204gvY4y_NVT-Kd4.jpeg",
      "alt": "Image generated with DALL-E 3 by author",
      "local_path": "attachments/294830feb6f9/images/000.jpeg",
      "bytes": 314272,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2024/07/01tpK40n6bj_kRKOR.png",
      "alt": "Image by author",
      "local_path": "attachments/294830feb6f9/images/001.png",
      "bytes": 2072436,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2024/07/0g4lKCyFKV_BxlDv_.png",
      "alt": "Image by author",
      "local_path": "attachments/294830feb6f9/images/002.png",
      "bytes": 196477,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2024/07/0aaxURA0K2PFyoe1J.png",
      "alt": "Image by author",
      "local_path": "attachments/294830feb6f9/images/003.png",
      "bytes": 428657,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2024/07/0nidQUa3pO20RW3KI.png",
      "alt": "Image by author",
      "local_path": "attachments/294830feb6f9/images/004.png",
      "bytes": 212495,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2020/10/1r8jGQPI80_uLd1BcqNwGFQ-scaled.jpeg",
      "alt": "",
      "local_path": "attachments/294830feb6f9/images/005.jpeg",
      "bytes": 532815,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2024/07/1MwkkYIz3xkpev7qi6wqHNw.png",
      "alt": "",
      "local_path": "attachments/294830feb6f9/images/006.png",
      "bytes": 7926,
      "downloaded": true
    },
    {
      "src": "https://assets.insightmediagroup.io/media/wp-content/uploads/2021/02/0Nsdyg6z5cNb2kTab-scaled.jpg",
      "alt": "",
      "local_path": "attachments/294830feb6f9/images/007.jpg",
      "bytes": 192534,
      "downloaded": true
    },
    {
      "src": "/images/fallback-image.jpg",
      "alt": "",
      "local_path": "",
      "bytes": 0,
      "downloaded": false
    }
  ],
  "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/programming/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/author/mlabonne/"
    },
    {
      "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/machine-learning/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/programming/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/technology/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/calibrating-classification-probabilities-the-right-way-da935caee18d/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/category/artificial-intelligence/machine-learning/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/author/jodancker/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/business-intelligence-visualizations-with-python-part-2-92f8a8463026/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/author/julianh/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/a-guide-to-pythons-dunder-methods-3b8104fce335/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/author/kurtispykes/"
    },
    {
      "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://colab.research.google.com/drive/164cg_O7SV7G8kZr_JXqLd6VC7pd86-1Z?usp=sharing"
    },
    {
      "type": "external",
      "href": "https://github.com/mlabonne/llm-course"
    },
    {
      "type": "external",
      "href": "https://mlabonne.github.io/blog/posts/Fine_tune_Mistral_7b_with_DPO.html"
    },
    {
      "type": "external",
      "href": "https://github.com/mlabonne/llm-datasets"
    },
    {
      "type": "external",
      "href": "https://huggingface.co/meta-llama/Meta-Llama-3.1-8B"
    },
    {
      "type": "external",
      "href": "https://github.com/unslothai/unsloth"
    },
    {
      "type": "external",
      "href": "https://huggingface.co/docs/trl/en/index"
    },
    {
      "type": "external",
      "href": "https://github.com/OpenAccess-AI-Collective/axolotl"
    },
    {
      "type": "external",
      "href": "https://huggingface.co/datasets/mlabonne/FineTome-100k"
    },
    {
      "type": "external",
      "href": "https://huggingface.co/datasets/arcee-ai/The-Tome"
    },
    {
      "type": "external",
      "href": "https://huggingface.co/datasets/arcee-ai/qwen2-72b-magpie-en"
    },
    {
      "type": "external",
      "href": "https://huggingface.co/HuggingFaceFW/fineweb-edu-classifier"
    },
    {
      "type": "external",
      "href": "https://huggingface.co/unsloth/Meta-Llama-3.1-8B-bnb-4bit"
    },
    {
      "type": "external",
      "href": "https://markdown-to-medium.surge.sh/meta-llama/Meta-Llama-3.1-8B"
    },
    {
      "type": "external",
      "href": "https://mlabonne.github.io/blog/posts/Introduction_to_Weight_Quantization.html"
    },
    {
      "type": "external",
      "href": "https://arxiv.org/abs/2312.03732"
    },
    {
      "type": "external",
      "href": "https://huggingface.co/mlabonne/FineLlama-3.1-8B"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T11:11:48Z",
  "source_url": "https://towardsdatascience.com/fine-tune-llama-3-1-ultra-efficiently-with-unsloth-7196c7165bab/",
  "delivery_status": "excluded_paid",
  "discovery_status": "excluded_paid",
  "missing_reason": "按原约定排除付费/会员全文"
}