train-llm-from-scratch
13,199
Stars
1,830
Forks
93
Watchers
2
Open Issues
MIT
许可证
Python
语言
2025-01-12
创建时间
2026-10-02
最近更新
项目能做什么
从零训练专属 LLM 的通俗指南,涵盖数据下载到文本生成的全流程。
来源:基于仓库元数据
· 内容以仓库实际 README 为准
使用说明 查看原始 README 原仓库文档
中文使用说明暂未生成。下面保留原仓库 README 摘录,命令与配置均以原文为准。
原始 README 摘录(英文原文,节选前 3000 字符)

<div align="center">
<!-- omit in toc -->
# Train LLM From Scratch
 [](https://github.com/FareedKhan-dev/train-llm-from-scratch/actions/workflows/ci.yml)   [](https://fareedkhan-dev.github.io/train-llm-from-scratch/)
**I am Looking for a PhD position in AI**. [GitHub](https://github.com/FareedKhan-dev)
</div>
I implemented a transformer model from scratch using PyTorch, based on the paper [Attention is All You Need](https://arxiv.org/abs/1706.03762). You can use my scripts to train your own **billion** or **million** parameter LLM using a single GPU, or a small one on a laptop CPU with no GPU at all.
This started as a pretraining tutorial. It now goes all the way from raw text to an aligned, reasoning style model, with every algorithm hand written in plain PyTorch (no `trl`, no `peft`, no `transformers`). The whole journey is one idea repeated: turn text into numbers, predict the next token, then keep changing the data and the loss until the model does what we want.
Next to the original model there is now a modern one, built the way current open models are (rotary positions, RMSNorm, SwiGLU, grouped-query and latent attention, Mixture of Experts, a KV cache), and every stage can train either. The newer training tricks are here too, each written out by hand: the Muon optimizer, LoRA, IPO and SimPO, Dr. GRPO, DAPO and GSPO, speculative decoding and int8 weights.

Here is the path we will walk, end to end:
```
raw text -> tokens -> a Transformer -> next-token loss -> a base model
base model -> SFT -> Reward Model -> {PPO, DPO} -> GRPO -> evaluation and chat
```
Below is the output of a trained 13 million parameter LLM, just so you can see where the small end of this starts:
```
In ***1978, The park was returned to the factory-plate that
the public share to the lower of the electronic fence that
follow from the Station's cities. The Canal of ancient Western
nations were confined to the city spot. The villages were directly
linked to cities in China that revolt that the US budget and in
Odambinais is uncertain and fortune established in rural areas.
```
<!-- omit in toc -->
## Table of Contents
- [Who this is for](#who-this-is-for)
- [Prerequisites and Training Time](#prerequisites-and-training-time)
- [Setup](#setup)
- [No GPU? Train on Your Laptop](#no-gpu-train-on-your-laptop)
- [Code Structure](#code-structure)
- [Step 1: Preparing the Data](#step-1-preparing-the-data)
- [Step 2: The Model, Built From Small Pieces](#step-2
阅读完整原文
商业机会分析 规则生成(AI 未配置)
综合评分 73.7 分。该项目在“个人可实施性”维度得 60 分,“付费可能性”维度得 84.0 分,差异化空间 100.0 分(评分依据见评分卡)。
将 train-llm-from-scratch 改造为垂直场景 SaaS
| 产品构想 | 保留核心能力,针对某一类用户(如某行业团队)做托管版,收取月费。 |
| 目标用户 | 有该需求的中小团队 / 独立站长 |
| 难度/周期 | 中 · 2~4 周(MVP) |
| 收费模式 | 订阅制(如 ¥29~99/月) |
| 竞争风险 | 需验证付费意愿;注意许可证合规 |
| 依据 | 数据支持的判断:许可证与关键词显示存在商用与付费场景 |
面向细分需求的二次开发产品
| 产品构想 | 基于 train-llm-from-scratch 的核心能力,聚焦一个更窄的场景(如某平台、某行业)做差异化。 |
| 目标用户 | 该细分场景用户 |
| 难度/周期 | 中 · 2~6 周 |
| 收费模式 | 买断制 / 订阅制 / 广告 |
| 竞争风险 | 细分需求规模需验证 |
| 依据 | AI 推测:基于项目主题标签的判断 |
封装为在线小工具或 API 服务
| 产品构想 | 把项目最常用的一个功能做成网页小工具,靠流量变现或按量计费。 |
| 目标用户 | 搜索引擎带来的长尾用户 |
| 难度/周期 | 低 · 1~3 周 |
| 收费模式 | 免费引流 + 增值功能 / API 按量计费 |
| 竞争风险 | 同质化竞争,需做好 SEO |
| 依据 | AI 推测:基于技术栈与部署特征的判断 |
以上为规则引擎基于仓库公开数据生成的参考结论,包含推测成分,不构成投资或创业承诺;配置 AI 后可获得更深入的分析。
报告会保存到 个人中心
竞品与相似项目 GitHub Search 真实检索
| 项目 | 中文简介 | Stars | 语言 |
|---|---|---|---|
| f/prompts.chat | 中文介绍暂未生成 f.k.a. Awesome ChatGPT Prompts. Share, discover, and collect |
176,843 | MDX |
| langchain-ai/langchain | 中文介绍暂未生成 The agent engineering platform. |
149,327 | Python |
| Graphify-Labs/graphify | 中文介绍暂未生成 Turn any codebase, with its docs, SQL schemas, configs, and |
145,650 | Python |
| thedotmack/claude-mem | 中文介绍暂未生成 Persistent Context Across Sessions for Every Agent – Captur |
115,819 | TypeScript |
| google-gemini/gemini-cli | 中文介绍暂未生成 An open-source AI agent that brings the power of Gemini dire |
108,766 | TypeScript |
| Egonex-AI/Understand-Anything | 中文介绍暂未生成 Graphs that teach > graphs that impress. Turn any code into |
91,886 | TypeScript |
以上为通过 GitHub 官方 Search API 检索的相似项目,功能差异与优劣势需进一步人工确认,配置 AI 后可生成对比分析。
项目优势与风险
| 技术成熟度 | 创建于 2025-01-12,最近推送 2026-10-02 |
| 社区活跃 | Star 13,199 / Fork 1,830 / Issue 2(数据支持,可自行核实) |
| 许可证 | MIT |
| 主要风险 | 上游项目方向可能变化;同赛道竞争者较多(见竞品表);热度与需求不完全等价 |
安装与使用
快速开始(以仓库 README 为准)
git clone https://github.com/FareedKhan-dev/train-llm-from-scratch.git cd train-llm-from-scratch # 请阅读仓库 README 获取确切安装步骤(依赖、配置、启动命令)
术语说明:Docker=应用容器(把程序和依赖打包运行);API Key=调用外部服务的密钥;Git Clone=把仓库代码下载到本地。常见错误:API Key 未配置 / 端口占用 / 数据库连接失败,多数项目在 README 的 Troubleshooting 章节有说明。