LLMs-from-scratch
这是《从零构建大语言模型》图书的官方配套代码库,基于PyTorch实现类GPT大模型,覆盖预训练、微调全流程的学习资源。
这个项目值得继续研究吗?
这是《从零构建大语言模型》图书的官方配套代码库,基于PyTorch实现类GPT大模型,覆盖预训练、微调全流程的学习资源。
- 解决什么问题
- 企业落地大模型时普遍面临团队对大模型底层逻辑不熟悉、无法自主定制优化模型、缺乏系统可实操的大模型研发学习路径的问题,难以支撑定制化业务需求。
- 适合什么团队
- 有Python开发基础、计划自研或定制大模型的企业技术团队,以及需要搭建自有大模型能力的AI业务团队。
- 使用前注意
- 本项目为教学学习资源,不直接适用于生产场景;当前未标注开源许可证,商用前需确认授权,学习要求使用者具备扎实的Python基础。
本页用于缩短初步筛选时间,不构成技术、采购或法律结论。 正式使用前请在真实业务数据上验证,并以官方说明与许可证为准。
从官方资料看清能力、部署与采用边界
以下内容依据项目公开 README 或模型卡翻译整理,代码、命令和产品名保持原样。
项目导读
项目定位
本项目是图书《Build a Large Language Model (From Scratch)》(中文译名:《从零构建大语言模型》)的官方配套代码库,核心目标是通过从零编码实操的方式,帮助使用者完整掌握大语言模型(LLM)的底层逻辑与全流程研发方法,其实现逻辑与ChatGPT等商用大模型的底层研发逻辑一致。
核心学习内容
本项目的内容按照图书章节组织,覆盖大模型研发的全流程,所有代码均基于PyTorch原生实现,未依赖任何第三方大模型库,方便使用者理解每一步的技术逻辑:
- 基础模块:包含文本数据处理、注意力机制实现、类GPT模型从零搭建的完整代码;
- 预训练模块:包含无标注数据预训练大模型的全流程代码;
- 微调模块:包含文本分类场景微调、指令微调的实现代码,附录还提供LoRA(低秩适配,一种低成本大模型微调技术)等参数高效微调方法的实现。
所有章节均配套习题与解答,方便团队组织学习培训时检验学习效果。
典型使用方式
- 团队技术培训:可按照章节顺序组织技术团队实操学习,从基础模块开始逐步跑通全流程,最终搭建出一个小型可用的大模型,帮助团队建立大模型研发的完整知识体系;
- 研发参考:企业自研定制大模型时,可参考本项目的预训练、微调逻辑,优化自有大模型的研发流程;
- 辅助学习:本项目配套有17小时15分钟的同步视频课程,可作为学习的辅助材料,也可单独作为视频培训资源使用。
代码获取方式:执行git clone --depth 1 https://github.com/rasbt/LLMs-from-scratch.git即可拉取最新代码,也可直接下载ZIP压缩包获取。
使用要求
人员能力要求
使用者需要具备扎实的Python编程基础,有深度神经网络相关经验、了解PyTorch基础的人员上手更快;如果是PyTorch新手,可通过项目附录A的入门内容快速掌握基础操作。
硬件要求
项目主章节的代码经过优化,普通商用笔记本即可在合理时间内运行,不需要专用的高端GPU硬件;如果设备配备GPU,代码会自动调用GPU加速运行。
项目状态
本项目自2023年7月上线以来持续活跃维护,当前未归档,最新代码更新时间为2026年8月,运行稳定性高。目前官方已推出续作图书《Build A Reasoning Model (From Scratch)》,承接本项目内容,指导使用者优化大模型的推理能力。
许可证与采用建议
本项目当前未标注开源许可证,企业如果要将代码用于商业用途,需提前联系作者确认授权范围。
采用建议:如果企业团队刚启动大模型自研工作,可优先用本项目开展技术培训,跑通最小可用大模型流程后再逐步扩展到生产场景;如果已有成熟的大模型研发团队,可参考本项目的微调逻辑,优化自有大模型的定制效率。
官方资料与来源
- ai
- artificial-intelligence
- attention-mechanism
- deep-learning
- finetuning
- from-scratch
- generative-ai
- gpt
- instruction-tuning
- language-model
- large-language-models
- llm
## Prerequisites The most important prerequisite is a strong foundation in Python programming. With this knowledge, you will be well prepared to explore the fascinating world of LLMs and understand the concepts and code examples presented in this book. If you have some experience with deep neural networks, you may find certain concepts more familiar, as LLMs are built upon these architectures. This book uses PyTorch to implement the code from scratch without using any external LLM libraries. While proficiency in PyTorch is not a prerequisite, familiarity with PyTorch basics is certainly useful. If you are new to PyTorch, Appendix A provides a concise introduction to PyTorch. Alternatively, you may find my book, [PyTorch in One Hour: From Tensors to Training Neural Networks on Multiple GPUs](https://sebastianraschka.com/teaching/pytorch-1h/), helpful for learning about the essentials.
该片段来自项目 README,仅用于初步判断;实际部署请以官方文档为准。




核对上游原始说明节选
Implement a ChatGPT-like LLM in PyTorch from scratch, step by step
Build a Large Language Model (From Scratch)
This repository contains the code for developing, pretraining, and finetuning a GPT-like LLM and is the official code repository for the book Build a Large Language Model (From Scratch).
In Build a Large Language Model (From Scratch), you'll learn and understand how large language models (LLMs) work from the inside out by coding them from the ground up, step by step. In this book, I'll guide you through creating your own LLM, explaining each stage with clear text, diagrams, and examples.
The method described in this book for training and developing your own small-but-functional model for educational purposes mirrors the approach used in creating large-scale foundational models such as those behind ChatGPT. In addition, this book includes code for loading the weights of larger pretrained models for finetuning.
- Link to the official source code repository
- Link to the book at Manning (the publisher's website)
- Link to the book page on Amazon.com
- ISBN 9781633437166
To download a copy of this repository, click on the Download ZIP button or execute the following command in your terminal:
git clone --depth 1 https://github.com/rasbt/LLMs-from-scratch.git(If you downloaded the code bundle from the Manning website, please consider visiting the official code repository on GitHub at https://github.com/rasbt/LLMs-from-scratch for the latest updates.)
Table of Contents
Please note that this README.md file is a Markdown (.md) file. If you have downloaded this code bundle from the Manning website and are viewing it on your local computer, I recommend using a Markdown editor or previewer for proper viewing. If you haven't installed a Markdown editor yet, Ghostwriter is a good free option.
You can alternatively view this and other files on GitHub at https://github.com/rasbt/LLMs-from-scratch in your browser, which renders Markdown automatically.
Tip:
If you're seeking guidance on installing Python and Python packages and setting up your code environment, I suggest reading the README.md file located in the setup directory.
Code tests Linux Code tests Windows Code tests macOS
- Troubleshooting Guide
| Chapter Title | Main Code (for Quick Access) | All Code + Supplementary | |------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------|-------------------------------| | Setup recommendations How to best read this book | - | - | | Ch 1: Understanding Large Language Models | No code | - | | Ch 2: Working with Text Data | - ch02.ipynb- dataloader.ipynb (summary)- exercise-solutions.ipynb | ./ch02 | | Ch 3: Coding Attention Mechanisms | - ch03.ipynb- multihead-attention.ipynb (summary) - exercise-solutions.ipynb| ./ch03 | | Ch 4: Implementing a GPT Model from Scratch | - ch04.ipynb- gpt.py (summary)- exercise-solutions.ipynb | ./ch04 | | Ch 5: Pretraining on Unlabeled Data | - ch05.ipynb- gpttrain.py (summary) - gptgenerate.py (summary) - exercise-solutions.ipynb | ./ch05 | | Ch 6: Finetuning for Text Classification | - ch06.ipynb - gptclassfinetune.py - exercise-solutions.ipynb | ./ch06 | | Ch 7: Finetuning to Follow Instructions | - ch07.ipynb- gptinstructionfinetuning.py (summary)- ollamaevaluate.py (summary)- exercise-solutions.ipynb | ./ch07 | | Appendix A: Introduction to PyTorch | - code-part1.ipynb- code-part2.ipynb- DDP-script.py- exercise-solutions.ipynb | ./appendix-A | | Appendix B: References and Further Reading | No code | ./appendix-B | | Appendix C: Exercise Solutions | - list of exercise solutions | ./appendix-C | | Appendix D: Adding Bells and Whistles to the Training Loop | - appendix-D.ipynb | ./appendix-D | | Appendix E: Parameter-efficient Finetuning with LoRA | - appendix-E.ipynb | ./appendix-E |
The mental model below summarizes the contents covered in this book.
Prerequisites
The most important prerequisite is a strong foundation in Python programming. With this knowledge, you will be well prepared to explore the fascinating world of LLMs and understand the concepts and code examples presented in this book.
If you have some experience with deep neural networks, you may find certain concepts more familiar, as LLMs are built upon these architectures.
This book uses PyTorch to implement the code from scratch without using any external LLM libraries. While proficiency in PyTorch is not a prerequisite, familiarity with PyTorch basics is certainly useful. If you are new to PyTorch, Appendix A provides a concise introduction to PyTorch. Alternatively, you may find my book, PyTorch in One Hour: From Tensors to Training Neural Networks on Multiple GPUs, helpful for learning about the essentials.
Hardware Requirements
The code in the main chapters of this book is designed to run on conventional laptops within a reasonable timeframe and does not require specialized hardware. This approach ensures that a wide audience can engage with the material. Additionally, the code automatically utilizes GPUs if they are available. (Please see the setup doc for additional recommendations.)
Video Course
A 17-hour and 15-minute companion video course where I code through each chapter of the book. The course is organized into chapters and sections that mirror the book's structure so that it can be used as a standalone alternative to the book or complementary code-along resource.
Companion Book / Sequel
Build A Reasoning Model (From Scratch), while a standalone book, can be considered as a sequel to Build A Large Language Model (From Scratch).
It starts with a pretrained model and implements different reasoning approaches, including inference-time scaling, reinforcement learning, and distillation, to improve the model's reasoning capabilities.
Similar to Build A Large Language Model (From Scratch), Build A Reasoning Model (From Scratch) takes a hands-on approach implementin