大模型底座 · 中文选型解读

LLMs-from-scratch

已解读GitHubJupyter Notebook

这是《从零构建大语言模型》一书的官方配套代码库,基于PyTorch逐步实现类GPT大模型,覆盖预训练、微调等全研发流程环节。

104.6kstar近 30 天仍在维护维护状态许可待核对商用提醒
在 GitHub 查看官方项目
适合解决作为问答、生成和智能体应用的基础模型
更适合正在比较模型能力、成本与部署方式的团队
投入判断上手门槛:较高。需要评测真实业务数据与许可边界
一分钟看懂

这个项目值得继续研究吗?

AI 依据上游资料解读 · 2026/9/6

这是《从零构建大语言模型》一书的官方配套代码库,基于PyTorch逐步实现类GPT大模型,覆盖预训练、微调等全研发流程环节。

解决什么问题
企业大模型相关团队想要吃透大模型底层逻辑、自行搭建小型验证模型时,常面临资料零散、现有工具封装度过高看不到内部实现,缺乏体系化从0到1实操指导的问题。
适合什么团队
适合企业AI算法研发团队、需要掌握大模型底层逻辑的技术负责人,以及计划自研小型定制大模型的相关团队使用。
使用前注意
项目暂未明确标注开源许可证,商业使用前需确认授权规则;使用者需具备扎实的Python基础,有深度学习、PyTorch基础可提升上手效率。

本页用于缩短初步筛选时间,不构成技术、采购或法律结论。 正式使用前请在真实业务数据上验证,并以官方说明与许可证为准。

项目导读

从官方资料看清能力、部署与采用边界

AI 翻译整理 · 保留官方来源

以下内容依据项目公开 README 或模型卡翻译整理,代码、命令和产品名保持原样。

项目导读

1. 项目定位

本项目是图书《Build a Large Language Model (From Scratch)》的官方配套代码库,核心目标是帮助使用者通过从0编写代码的方式,彻底理解大语言模型(LLM)的内部工作原理。项目实现的类GPT大模型,底层构建逻辑和ChatGPT等商用大模型的研发逻辑完全一致,既可以作为学习材料,也可以参考其逻辑搭建企业自用的小型验证大模型。

2. 核心能力

项目代码覆盖大模型研发全流程环节,所有实现均基于PyTorch完成,未依赖任何外部大模型专用库,可清晰看到每一步的实现细节:

  • 基础环节:文本数据处理、注意力机制编码、GPT模型结构从零搭建
  • 训练环节:无标注数据预训练、文本分类场景微调、指令微调
  • 扩展内容:附录提供PyTorch入门指引、LoRA(低秩适配,一种参数高效的大模型微调方法,仅需调整少量参数即可完成微调)实现、训练环优化、习题解答等补充内容。

3. 典型使用方式

项目代码和图书章节一一对应,有两种典型使用方式:

  1. 配合图书体系化学习:每章均提供可直接运行的.ipynb格式交互式代码文件,以及提炼完成的核心逻辑.py文件,同时配套习题解答,可逐章跑通全流程,吃透大模型研发的每一个环节。
  2. 参考实现自研模型:可单独复用各环节的代码逻辑,搭建企业自用的小型定制大模型,完成预训练、微调等相关工作。

代码可通过以下命令获取,也可直接在GitHub仓库下载ZIP包:

git clone --depth 1 https://github.com/rasbt/LLMs-from-scratch.git

若从出版社Manning官网下载代码包,建议前往GitHub官方仓库获取最新更新版本。

4. 使用前提与硬件要求

前置知识要求

  • 必备条件:扎实的Python编程基础
  • 加分条件:有深度神经网络相关基础,可更快理解大模型相关概念;熟悉PyTorch基础可提升上手效率,无相关基础的使用者可参考附录A的PyTorch入门内容,或配套的《PyTorch in One Hour》学习资料。

硬件要求

主章节代码无需专用硬件,在常规笔记本上即可在合理时间内运行完成;若有可用GPU,代码会自动调用GPU加速运行。具体环境配置可参考仓库setup目录下的README文档。

5. 配套资源

  • 配套视频课程:共17小时15分钟,结构和图书章节完全对应,可作为代码实操的跟学资料,也可单独作为学习材料使用。
  • 续作图书:《Build A Reasoning Model (From Scratch)》作为本项目内容的延伸,主打从预训练模型出发,实现推理时缩放、强化学习、知识蒸馏等推理能力优化方案,同样采用实操导向的编写方式。

6. 许可证与采用建议

本项目暂未明确标注开源许可证,若计划用于商业用途,需先联系项目作者确认授权规则。 若用于企业内部技术学习、大模型研发预研,可直接参考其代码逻辑,其实现完全对齐工业级大模型的研发流程,相关逻辑可直接复用至企业自有大模型的研发工作中。项目目前处于活跃维护状态,使用中遇到的问题可在仓库提交Issue反馈。

可核对的事实层

官方资料与来源

查看来源 →
  • ai
  • artificial-intelligence
  • attention-mechanism
  • deep-learning
  • finetuning
  • from-scratch
  • generative-ai
  • gpt
  • instruction-tuning
  • language-model
  • large-language-models
  • llm
默认分支main
关注仓库847
复刻次数16.0k
开放议题1
近期更新2026/9/5
仓库状态未标记归档
上游部署线索
## 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