Lightning-AI/litgpt
GitHub: Lightning-AI/litgpt
一个提供 20+ 高性能大语言模型及预训练、微调、部署完整方案的开源 AI 框架。
Stars: 13553 | Forks: 1477
# ⚡ LitGPT
**20+ 高性能 LLM,附带用于大规模预训练、微调和部署的方案。**
# 快如闪电地微调、预训练和推理 LLM ⚡⚡
每个 LLM 都是**无抽象**且**完全可控**的从头实现,使它们在企业级规模下极其快速、精简且高性能。
✅ **企业就绪 -** Apache 2.0 许可证,无限制的企业用途。
✅ **开发者友好 -** 无抽象层和单文件实现,便于调试。
✅ **性能优化 -** 专为最大化性能、降低成本和加速训练而设计的模型。
✅ **经过验证的方案 -** 在企业级规模下测试过的高度优化的训练/微调方案。
# 快速开始
安装 LitGPT
```
pip install 'litgpt[extra]'
```
加载并使用任何 [20+ LLMs](#choose-from-20-llms):
```
from litgpt import LLM
llm = LLM.load("microsoft/phi-2")
text = llm.generate("Fix the spelling: Every fall, the family goes to the mountains.")
print(text)
# 修正后的句子:每年秋天,这家人都会去山区。
```
✅ 针对快速推理进行了优化
✅ 量化
✅ 可在低内存 GPU 上运行
✅ 无内部抽象层
✅ 针对生产规模进行了优化
✅ 从头实现 ✅ 无抽象 ✅ 对初学者友好 ✅ Flash attention ✅ FSDP ✅ LoRA, QLoRA, Adapter ✅ 减少 GPU 内存 (fp4/8/16/32) ✅ 1-1000+ GPUs/TPUs ✅ 20+ LLMs   [](https://github.com/Lightning-AI/litgpt/blob/main/LICENSE.md) [](https://discord.gg/VptPCZkGNa)快速开始 • 模型 • 微调 • 部署 • 所有工作流 • 功能特性 • 方案 • Lightning AI • 教程
![]()
高级安装选项
从源码安装: ``` git clone https://github.com/Lightning-AI/litgpt cd litgpt # 如果使用 uv uv sync --all-extras # 如果使用 pip pip install -e ".[extra,compiler,test]" ```查看 20+ LLM 的完整列表
#### 所有模型 | 模型 | 模型规模 | 作者 | 参考 | |----|----|----|----| | CodeGemma | 7B | Google | [Google Team, Google Deepmind](https://ai.google.dev/gemma/docs/codegemma) | | Code Llama | 7B, 13B, 34B, 70B | Meta AI | [Rozière et al. 2023](https://arxiv.org/abs/2308.12950) | | Falcon | 7B, 40B, 180B | TII UAE | [TII 2023](https://falconllm.tii.ae) | | Falcon 3 | 1B, 3B, 7B, 10B | TII UAE | [TII 2024](https://huggingface.co/blog/falcon3) | | FreeWilly2 (Stable Beluga 2) | 70B | Stability AI | [Stability AI 2023](https://stability.ai/blog/stable-beluga-large-instruction-fine-tuned-models) | | Function Calling Llama 2 | 7B | Trelis | [Trelis et al. 2023](https://huggingface.co/Trelis/Llama-2-7b-chat-hf-function-calling-v2) | | Gemma | 2B, 7B | Google | [Google Team, Google Deepmind](https://storage.googleapis.com/deepmind-media/gemma/gemma-report.pdf) | | Gemma 2 | 9B, 27B | Google | [Google Team, Google Deepmind](https://storage.googleapis.com/deepmind-media/gemma/gemma-2-report.pdf) | | Gemma 3 | 1B, 4B, 12B, 27B | Google | [Google Team, Google Deepmind](https://arxiv.org/pdf/2503.19786) | | Llama 2 | 7B, 13B, 70B | Meta AI | [Touvron et al. 2023](https://arxiv.org/abs/2307.09288) | | Llama 3.1 | 8B, 70B | Meta AI | [Meta AI 2024](https://github.com/meta-llama/llama3) | | Llama 3.2 | 1B, 3B | Meta AI | [Meta AI 2024](https://ai.meta.com/blog/llama-3-2-connect-2024-vision-edge-mobile-devices/) | | Llama 3.3 | 70B | Meta AI | [Meta AI 2024](https://huggingface.co/meta-llama/Llama-3.3-70B-Instruct) | | Mathstral | 7B | Mistral AI | [Mistral AI 2024](https://mistral.ai/news/mathstral/) | | MicroLlama | 300M | Ken Wang | [MicroLlama repo](https://github.com/keeeeenw/MicroLlama) | | Mixtral MoE | 8x7B | Mistral AI | [Mistral AI 2023](https://mistral.ai/news/mixtral-of-experts/) | | Mistral | 7B, 123B | Mistral AI | [Mistral AI 2023](https://mistral.ai/news/announcing-mistral-7b/) | | Mixtral MoE | 8x22B | Mistral AI | [Mistral AI 2024](https://mistral.ai/news/mixtral-8x22b/) | | OLMo | 1B, 7B | Allen Institute for AI (AI2) | [Groeneveld et al. 2024](https://aclanthology.org/2024.acl-long.841/) | | OpenLLaMA | 3B, 7B, 13B | OpenLM Research | [Geng & Liu 2023](https://github.com/openlm-research/open_llama) | | Phi 1.5 & 2 | 1.3B, 2.7B | Microsoft Research | [Li et al. 2023](https://arxiv.org/abs/2309.05463) | | Phi 3 | 3.8B | Microsoft Research | [Abdin et al. 2024](https://arxiv.org/abs/2404.14219) | | Phi 4 | 14B | Microsoft Research | [Abdin et al. 2024](https://arxiv.org/abs/2412.08905) | | Phi 4 Mini Instruct | 3.8B | Microsoft Research | [Microsoft 2025](https://arxiv.org/abs/2503.01743) | | Phi 4 Mini Reasoning | 3.8B | Microsoft Research | [Xu, Peng et al. 2025](https://arxiv.org/abs/2504.21233) | | Phi 4 Reasoning | 3.8B | Microsoft Research | [Abdin et al. 2025](https://arxiv.org/abs/2504.21318) | | Phi 4 Reasoning Plus | 3.8B | Microsoft Research | [Abdin et al. 2025](https://arxiv.org/abs/2504.21318) | | Platypus | 7B, 13B, 70B | Lee et al. | [Lee, Hunter, and Ruiz 2023](https://arxiv.org/abs/2308.07317) | | Pythia | {14,31,70,160,410}M, {1,1.4,2.8,6.9,12}B | EleutherAI | [Biderman et al. 2023](https://arxiv.org/abs/2304.01373) | | Qwen2.5 | 0.5B, 1.5B, 3B, 7B, 14B, 32B, 72B | Alibaba Group | [Qwen Team 2024](https://qwenlm.github.io/blog/qwen2.5/) | | Qwen2.5 Coder | 0.5B, 1.5B, 3B, 7B, 14B, 32B | Alibaba Group | [Hui, Binyuan et al. 2024](https://arxiv.org/abs/2409.12186) | | Qwen2.5 1M (长上下文) | 7B, 14B | Alibaba Group | [Qwen Team 2025](https://qwenlm.github.io/blog/qwen2.5-1m/) | | Qwen2.5 Math | 1.5B, 7B, 72B | Alibaba Group | [An, Yang et al. 2024](https://arxiv.org/abs/2409.12122) | | QwQ | 32B | Alibaba Group | [Qwen Team 2025](https://qwenlm.github.io/blog/qwq-32b/) | | QwQ-Preview | 32B | Alibaba Group | [Qwen Team 2024](https://qwenlm.github.io/blog/qwq-32b-preview/) | | Qwen3 | 0.6B, 1.7B, 4B{Hybrid, Thinking-2507, Instruct-2507}, 8B, 14B, 32B | Alibaba Group | [Qwen Team 2025](https://arxiv.org/abs/2505.09388/) | | Qwen3 MoE | 30B{Hybrid, Thinking-2507, Instruct-2507}, 235B{Hybrid, Thinking-2507, Instruct-2507} | Alibaba Group | [Qwen Team 2025](https://arxiv.org/abs/2505.09388/) | | R1 Distill Llama | 8B, 70B | DeepSeek AI | [DeepSeek AI 2025](https://github.com/deepseek-ai/DeepSeek-R1/blob/main/DeepSeek_R1.pdf) | | SmolLM2 | 135M, 360M, 1.7B | Hugging Face | [Hugging Face 2024](https://github.com/huggingface/smollm) | | Salamandra | 2B, 7B | Barcelona Supercomputing Centre | [BSC-LTC 2024](https://github.com/BSC-LTC/salamandra) | | StableCode | 3B | Stability AI | [Stability AI 2023](https://stability.ai/blog/stablecode-llm-generative-ai-coding) | | StableLM | 3B, 7B | Stability AI | [Stability AI 2023](https://github.com/Stability-AI/StableLM) | | StableLM Zephyr | 3B | Stability AI | [Stability AI 2023](https://stability.ai/blog/stablecode-llm-generative-ai-coding) | | TinyLlama | 1.1B | Zhang et al. | [Zhang et al. 2023](https://github.com/jzhang38/TinyLlama) | **提示**:您可以通过运行 `litgpt download list` 命令列出所有可用的模型。微调 • 预训练 • 持续预训练 • 评估 • 部署 • 测试
使用命令行界面运行高级工作流,例如在您自己的数据上进行预训练或微调。 ## 所有工作流 安装 LitGPT 后,选择要运行的模型和工作流(微调、预训练、评估、部署等): ``` # litgpt [action] [model] litgpt serve meta-llama/Llama-3.2-3B-Instruct litgpt finetune meta-llama/Llama-3.2-3B-Instruct litgpt pretrain meta-llama/Llama-3.2-3B-Instruct litgpt chat meta-llama/Llama-3.2-3B-Instruct litgpt evaluate meta-llama/Llama-3.2-3B-Instruct ``` ## 微调 LLM 微调是指采用预训练的 AI 模型,并在针对特定任务或应用定制的更小、更专门的数据集上进一步训练的过程。 ``` # 0) 设置你的 dataset curl -L https://huggingface.co/datasets/ksaw008/finance_alpaca/resolve/main/finance_alpaca.json -o my_custom_dataset.json # 1) Finetune 一个 model(自动下载 weights) litgpt finetune microsoft/phi-2 \ --data JSON \ --data.json_path my_custom_dataset.json \ --data.val_split_fraction 0.1 \ --out_dir out/custom-model # 2) 测试 model litgpt chat out/custom-model/final # 3) 髟署 model litgpt serve out/custom-model/final ``` [阅读完整的微调文档](tutorials/finetune.md) ## 部署 LLM 部署预训练或微调的 LLM,以便在真实应用中使用它。部署后会自动设置一个 Web 服务器,可由网站或应用访问。 ``` # 部署一个开箱即用的 LLM litgpt serve microsoft/phi-2 # 部署你自己的 trained model litgpt serve path/to/microsoft/phi-2/checkpoint ```显示查询服务器的代码:
在单独的终端中测试服务器,并将模型 API 集成到您的 AI 产品中: ``` # 3) 使用 server(在单独的 Python session 中) import requests, json response = requests.post( "http://127.0.0.1:8000/predict", json={"prompt": "Fix typos in the following sentence: Example input"} ) print(response.json()["output"]) ```完整代码:
``` # 1) 列出所有支持的 LLM litgpt download list # 2) 使用一个 model(自动下载 weights) litgpt chat microsoft/phi-2 >> Prompt: What do Llamas eat? ``` 某些模型的下载需要额外的访问 token。您可以在[下载](tutorials/download_model_weights.md#specific-models-and-access-tokens)文档中了解更多相关信息。显示代码:
``` mkdir -p custom_texts curl https://www.gutenberg.org/cache/epub/24440/pg24440.txt --output custom_texts/book1.txt curl https://www.gutenberg.org/cache/epub/26393/pg26393.txt --output custom_texts/book2.txt # 1) 下载一个 tokenizer litgpt download EleutherAI/pythia-160m \ --tokenizer_only True # 2) Pretrain model litgpt pretrain EleutherAI/pythia-160m \ --tokenizer_dir EleutherAI/pythia-160m \ --data TextFiles \ --data.train_data_path "custom_texts/" \ --train.max_tokens 10_000_000 \ --out_dir out/custom-model # 3) 测试 model litgpt chat out/custom-model/final ```显示代码:
``` mkdir -p custom_texts curl https://www.gutenberg.org/cache/epub/24440/pg24440.txt --output custom_texts/book1.txt curl https://www.gutenberg.org/cache/epub/26393/pg26393.txt --output custom_texts/book2.txt # 1) 继续预训练一个 model(自动下载 weights) litgpt pretrain EleutherAI/pythia-160m \ --tokenizer_dir EleutherAI/pythia-160m \ --initial_checkpoint_dir EleutherAI/pythia-160m \ --data TextFiles \ --data.train_data_path "custom_texts/" \ --train.max_tokens 10_000_000 \ --out_dir out/custom-model # 2) 测试 model litgpt chat out/custom-model/final ```✅ 使用配置自定义训练
配置允许您自定义所有细粒度参数的训练,例如: ``` # 用于 finetuning 的 base model checkpoint 目录路径。(type:✅ 示例:LoRA 微调配置
``` # 用于 finetuning 的 base model checkpoint 目录路径。(type:✅ 在 CLI 中覆盖任何参数:
``` litgpt finetune \ --config https://raw.githubusercontent.com/Lightning-AI/litgpt/main/config_hub/finetune/llama-2-7b/lora.yaml \ --lora_r 4 ```📊 SAMBA: Simple Hybrid State Space Models for Efficient Unlimited Context Language Modeling
微软研究人员的 [Samba](https://github.com/microsoft/Samba) 项目建立在 LitGPT 代码库的基础之上,将状态空间模型与滑动窗口注意力机制结合起来,其表现优于纯状态空间模型。🏆 NeurIPS 2023 Large Language Model Efficiency Challenge: 1 LLM + 1 GPU + 1 Day
LitGPT 仓库是 [NeurIPS 2023 LLM Efficiency Challenge](https://llm-efficiency-challenge.github.io) 的官方入门套件,这是一项专注于在单个 GPU 上用 24 小时对现有的未经指令微调的 LLM 进行微调的比赛。🦙 TinyLlama: An Open-Source Small Language Model
LitGPT 为 [TinyLlama 项目](https://github.com/jzhang38/TinyLlama)和 [TinyLlama: An Open-Source Small Language Model](https://arxiv.org/abs/2401.02385) 研究论文提供了支持。🍪 MicroLlama: MicroLlama-300M
[MicroLlama](https://github.com/keeeeenw/MicroLlama) 是一个由 TinyLlama 和 LitGPT 提供支持的、在 50B token 上预训练的 300M Llama 模型。🔬 Pre-training Small Base LMs with Fewer Tokens
利用 LitGPT 的研究论文 [Pre-training Small Base LMs with Fewer Tokens](https://arxiv.org/abs/2404.08634) 通过从较大的模型中继承几个 transformer 块,并在较大模型使用的一小部分数据上进行训练,开发了更小的基础语言模型。它表明,尽管使用的训练数据和资源少得多,但这些较小的模型可以表现出与较大模型相当的性能。标签:DLL 劫持, PyTorch, 人工智能, 凭据扫描, 大语言模型, 模型微调, 模型训练, 深度学习, 用户模式Hook绕过, 逆向工具