LLMs-from-scratch
本项目是《从零构建大语言模型》图书的官方配套代码库,可手把手教你用PyTorch从零实现、预训练、微调类似ChatGPT的GPT类大模型。
这个项目值得继续研究吗?
本项目是《从零构建大语言模型》图书的官方配套代码库,可手把手教你用PyTorch从零实现、预训练、微调类似ChatGPT的GPT类大模型。
- 解决什么问题
- 不少企业AI业务团队想搞懂大模型底层逻辑、掌握自主可控的小模型开发能力,但现有资料要么过于理论化,要么重度依赖第三方成熟库,缺乏可落地的分步实操指引。
- 适合什么团队
- 适合需要掌握大模型原理的企业AI业务团队、算法研发团队,以及计划自主开发、微调行业专用小模型的技术团队使用。
- 使用前注意
- 项目暂未标注开源许可,商用前需确认授权;使用者需具备扎实的Python编程基础,提前了解PyTorch基础操作可提升学习效率。
本页用于缩短初步筛选时间,不构成技术、采购或法律结论。 正式使用前请在真实业务数据上验证,并以官方说明与许可证为准。
从官方资料看清能力、部署与采用边界
以下内容依据项目公开 README 或模型卡翻译整理,代码、命令和产品名保持原样。
项目导读
1. 项目定位
本项目是图书《Build a Large Language Model (From Scratch)》(从零构建大语言模型)的官方配套代码库,完全基于PyTorch开发,不依赖任何第三方大模型库,所有代码均为从零实现。项目的目标是帮助使用者从底层吃透大语言模型(LLM,一种可理解、生成人类自然语言的人工智能模型)的完整运行逻辑,其介绍的小功能模型训练方法,和ChatGPT等商用大规模基础大模型的开发逻辑完全一致,兼顾学习价值和实操复用价值。
2. 核心能力
本项目覆盖大模型开发全链路流程,核心能力包括:
- 全流程代码覆盖:从文本数据处理、注意力机制编码、GPT模型从零搭建,到无标注数据预训练、文本分类场景微调、指令微调,再到LoRA(参数高效微调技术,仅需调整模型少量参数即可完成适配,大幅降低微调的计算成本)等进阶微调方法,均提供可直接运行的完整代码。
- 学习友好设计:每个章节都配套主代码、核心逻辑摘要代码、习题解答,还附带有PyTorch入门附录,对PyTorch不熟悉的使用者也可快速上手。
- 低硬件要求:主章节的代码无需专用AI计算硬件,在普通笔记本上即可在合理时间内运行,设备有GPU时会自动调用加速计算。
3. 典型使用方式
本项目主要有两类使用场景:
- 学习训练:配合对应图书或17小时15分钟的配套视频课程,按章节顺序依次学习练习,从基础概念到完整模型开发逐步掌握大模型全链路技术,适合团队做技术能力建设使用。
- 二次开发:可基于现有代码框架,替换为自身业务场景的数据集,开发、微调符合业务需求的小型专用大模型,比如行业问答、业务文本分类等场景的定制化模型。
4. 使用要求
前置知识要求
使用者需要具备扎实的Python编程基础,如果有深度神经网络相关经验,对相关概念的理解会更顺畅;PyTorch不是强制要求的前置技能,不熟悉的使用者可以参考附录A的PyTorch入门内容,也可参考作者公开的PyTorch入门学习资料补充基础。
环境获取方式
可以通过执行命令git clone --depth 1 https://github.com/rasbt/LLMs-from-scratch.git下载最新代码,环境配置步骤可参考项目内setup目录下的README文档。
5. 项目状态与周边资源
本项目2023年7月创建,至今仍在活跃更新,未进入归档状态,开放Issue渠道接受问题反馈。 周边配套资源丰富,除了正式出版的图书外,还有结构完全匹配图书的17小时15分钟配套视频课程,可作为代码实操的辅助资料;同系列续作《Build A Reasoning Model (From Scratch)》专注推理模型开发,可作为本项目的进阶学习内容。
6. 采用建议
本项目暂未标注开源许可证,企业如果要将代码用于商用场景,需先联系作者确认授权,避免版权风险。 如果是用于内部团队技术学习、培养大模型开发能力,可直接采用本项目的学习路径;如果是开发生产环境的业务专用模型,可基于本项目的代码框架做适配修改,配合自有业务数据训练即可。
官方资料与来源
- 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