FineTuningLLMs

dvgodoyMIT更新放缓

配套《Fine-Tuning LLMs with PyTorch and Hugging Face》一书的实战代码库,用 LoRA、PEFT、bitsandbytes 量化等手把手教你在消费级显卡上微调大模型。

Official repository of my book "A Hands-On Guide to Fine-Tuning LLMs with PyTorch and Hugging Face"

暂无版本878star122fork同步于 2026-11-07 07:29 UTC

怎么上手

由 README 提炼,执行前请确认
README 未给出本地安装命令;最简方式是用 Colab 打开章节 notebook,例如 https://colab.research.google.com/github/dvgodoy/FineTuningLLMs/blob/main/Chapter0.ipynb
硬件要求按 README 说明

README 未给出具体显存或 GPU 型号要求;只说明可在 Google Colab 提供的 GPU 上运行,并提到面向消费级单卡 GPU 微调,另有附录讲在 runpod.io 上开 GPU Pod。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

配套书籍的实战代码库,用 PyTorch 和 Hugging Face 走通 LLM 微调全流程

配套《Fine-Tuning LLMs with PyTorch and Hugging Face》一书的实战代码库,用 LoRA、PEFT、bitsandbytes 量化等手把手教你在消费级显卡上微调大模型。

核心能力

  • 用 Colab 直接加载各章 notebook 并跑在 GPU 上
  • 讲解 8-bit 与 4-bit 量化及 BitsAndBytes 配置
  • 用 PEFT 配置低秩适配器 LoRA 并准备量化基座
  • 处理数据集格式、chat 模板、padding 与 packing
  • 用 SFTTrainer 调参并对比 Flash Attention 与 SDPA

容易踩的坑

这是配套书籍的代码仓库,不是可直接安装的库,需按章节 notebook 逐个运行;Colab 运行需要登录 Google 账号并使用其提供的 GPU;本地复现需自行按 FAQ 和附录 A 配置环境与 GPU 机器。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    A Hands-On Guide to Fine-Tuning LLMs with PyTorch and Hugging Face

    Setup

    Google Colab

    You can easily load the notebooks directly from GitHub using Colab and run them using a GPU provided by Google. You need to be logged in a Google Account of your own.

    You can go through the chapters already using the links below:

    Preface

    If you’re reading this, I probably don’t need to tell you that large language models are pretty much everywhere, right?

    Since the release of ChatGPT in November 2022, it feels almost impossible to keep up with the rapid pace of developments. Every day, there’s a new technique, a new model, or a groundbreaking announcement. These are surely exciting times—but they can also feel overwhelming, exhausting and, at times, frustrating.

    "Where do I even begin to learn this?" is a perfectly valid question—and a tough one to answer on your own. I wrote this book as a tentative response to that question. It focuses on key concepts that, in my view, have proven to be stable and are likely to remain central to the fine-tuning process for the foreseeable future: quantization, low-rank adapters, and formatting templates.

    Mastering these concepts is crucial for understanding the current landscape and will also empower you to handle future developments. They might also be useful to train or fine-tune a variety of large models, not just language models. They are essential tools in any data scientist's toolkit.


    This is an intermediate-level book, so to make the most of its content, you need a solid foundation. If Transformers, attention, Adam, tokens, embeddings, and GPUs do not ring any bells, I'd suggest you to start with my beginner-friendly series, Deep Learning with PyTorch Step-by-Step.


    I chose the Hugging Face ecosystem as the foundation for this book because it is the de facto standard for working with deep learning models, whether they're language models or not. The concepts I discuss—quantization, adapters, and templates—are neatly implemented and integrated into the ecosystem, making them relatively straightforward to use. But you have to understand how to configure them effectively and what those configurations are actually doing under the hood. It wasn't easy to find out such information out there, though. I missed a comprehensive overview explaining how these techniques work together in practice, especially when fine-tuning LLMs on a single GPU. This is the gap I aim to fill with this book.

    Originally, its title was "a short guide," but its scope grew so much that I eventually renamed it "a hands-on guide." It covers a lot of ground, and I truly hope it supports you on your learning journey. Along the way, I've included plenty of fun examples, made-up quotes, and movie references—after all, I believe learning should be fun.

    There's nothing cooler than learning about something new, trying it out yourself, and watching it work just fine, don't you agree? That's what you'll do in Chapter 0, the "TL;DR" chapter that will guide you through the entire journey of fine-tuning an LLM: quantization, low-rank adapters, dataset formatting, training, and querying the model. Next, we'll take a step back and have a brief discussion about language models, Transformers, the attention mechanism, and the different types of fine-tuning in Chapter 1.

    The following chapters, two through six, correspond to the sections introduced in Chapter 0. In Chapter 2, "Loading a Quantized Model," we'll discuss 8-bit and 4-bit quantization in more detail, as well as the BitsAndBytes configuration. In Chapter 3, "Low-Rank Adaptation (LoRA)," we'll explore the role and usage of low-rank adapters, including how to configure them using the PEFT package and how to prepare the (quantized) base model to improve numerical stability during training. Then, in Chapter 4, "Formatting Your Dataset," we'll focus on data formatting, chat templates, and the role of tokenizers, padding, packing, and data collators.

    The next step in our journey is Chapter 5, "Fine-Tuning with SFTTrainer," where we'll explore a wide range of configuration possibilites to maximize the performance of a consumer-grade GPU and effectively fine-tune our models. We'll also discuss different implementations of the attention mechanism—Flash Attention and PyTorch's SDPA—and compare their speed and memory requirements. Chapter 6, "Deploying It Locally," is an engineering-focused chapter. It covers the details and alternatives for converting your fine-tuned models to the GGUF format and how to serve your models using either Ollama or llama.cpp.

    Every learning journey has its difficulties and pitfalls, and in our case, warnings and raised exceptions. The last chapter, Chapter -1, "Troubleshooting," serves as a reference to help you understand and solve the typical errors you may encounter.

    Finally, there are also two appendices: the first one, "Appendix A: Setting Up Your GPU Pod," is a step-by-step tutorial for using a cloud provider (runpod.io, a personal favorite) to spin up a GPU-powered Jupyter Notebook; the second, "Appendix B: Data Types' Internal Representation," offers an (optional) overview of how integers and floating-point numbers are internally represented by their bits, for those interested in understanding the advantages and disadvantages of each data type in more detail.

    That's it! Have fun!