[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-meta-pytorch--torchtune":3,"tool-meta-pytorch--torchtune":64},[4,17,27,35,43,56],{"id":5,"name":6,"github_repo":7,"description_zh":8,"stars":9,"difficulty_score":10,"last_commit_at":11,"category_tags":12,"status":16},3808,"stable-diffusion-webui","AUTOMATIC1111\u002Fstable-diffusion-webui","stable-diffusion-webui 是一个基于 Gradio 构建的网页版操作界面，旨在让用户能够轻松地在本地运行和使用强大的 Stable Diffusion 图像生成模型。它解决了原始模型依赖命令行、操作门槛高且功能分散的痛点，将复杂的 AI 绘图流程整合进一个直观易用的图形化平台。\n\n无论是希望快速上手的普通创作者、需要精细控制画面细节的设计师，还是想要深入探索模型潜力的开发者与研究人员，都能从中获益。其核心亮点在于极高的功能丰富度：不仅支持文生图、图生图、局部重绘（Inpainting）和外绘（Outpainting）等基础模式，还独创了注意力机制调整、提示词矩阵、负向提示词以及“高清修复”等高级功能。此外，它内置了 GFPGAN 和 CodeFormer 等人脸修复工具，支持多种神经网络放大算法，并允许用户通过插件系统无限扩展能力。即使是显存有限的设备，stable-diffusion-webui 也提供了相应的优化选项，让高质量的 AI 艺术创作变得触手可及。",162132,3,"2026-04-05T11:01:52",[13,14,15],"开发框架","图像","Agent","ready",{"id":18,"name":19,"github_repo":20,"description_zh":21,"stars":22,"difficulty_score":23,"last_commit_at":24,"category_tags":25,"status":16},1381,"everything-claude-code","affaan-m\u002Feverything-claude-code","everything-claude-code 是一套专为 AI 编程助手（如 Claude Code、Codex、Cursor 等）打造的高性能优化系统。它不仅仅是一组配置文件，而是一个经过长期实战打磨的完整框架，旨在解决 AI 代理在实际开发中面临的效率低下、记忆丢失、安全隐患及缺乏持续学习能力等核心痛点。\n\n通过引入技能模块化、直觉增强、记忆持久化机制以及内置的安全扫描功能，everything-claude-code 能显著提升 AI 在复杂任务中的表现，帮助开发者构建更稳定、更智能的生产级 AI 代理。其独特的“研究优先”开发理念和针对 Token 消耗的优化策略，使得模型响应更快、成本更低，同时有效防御潜在的攻击向量。\n\n这套工具特别适合软件开发者、AI 研究人员以及希望深度定制 AI 工作流的技术团队使用。无论您是在构建大型代码库，还是需要 AI 协助进行安全审计与自动化测试，everything-claude-code 都能提供强大的底层支持。作为一个曾荣获 Anthropic 黑客大奖的开源项目，它融合了多语言支持与丰富的实战钩子（hooks），让 AI 真正成长为懂上",138956,2,"2026-04-05T11:33:21",[13,15,26],"语言模型",{"id":28,"name":29,"github_repo":30,"description_zh":31,"stars":32,"difficulty_score":23,"last_commit_at":33,"category_tags":34,"status":16},2271,"ComfyUI","Comfy-Org\u002FComfyUI","ComfyUI 是一款功能强大且高度模块化的视觉 AI 引擎，专为设计和执行复杂的 Stable Diffusion 图像生成流程而打造。它摒弃了传统的代码编写模式，采用直观的节点式流程图界面，让用户通过连接不同的功能模块即可构建个性化的生成管线。\n\n这一设计巧妙解决了高级 AI 绘图工作流配置复杂、灵活性不足的痛点。用户无需具备编程背景，也能自由组合模型、调整参数并实时预览效果，轻松实现从基础文生图到多步骤高清修复等各类复杂任务。ComfyUI 拥有极佳的兼容性，不仅支持 Windows、macOS 和 Linux 全平台，还广泛适配 NVIDIA、AMD、Intel 及苹果 Silicon 等多种硬件架构，并率先支持 SDXL、Flux、SD3 等前沿模型。\n\n无论是希望深入探索算法潜力的研究人员和开发者，还是追求极致创作自由度的设计师与资深 AI 绘画爱好者，ComfyUI 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",107662,"2026-04-03T11:11:01",[13,14,15],{"id":36,"name":37,"github_repo":38,"description_zh":39,"stars":40,"difficulty_score":23,"last_commit_at":41,"category_tags":42,"status":16},3704,"NextChat","ChatGPTNextWeb\u002FNextChat","NextChat 是一款轻量且极速的 AI 助手，旨在为用户提供流畅、跨平台的大模型交互体验。它完美解决了用户在多设备间切换时难以保持对话连续性，以及面对众多 AI 模型不知如何统一管理的痛点。无论是日常办公、学习辅助还是创意激发，NextChat 都能让用户随时随地通过网页、iOS、Android、Windows、MacOS 或 Linux 端无缝接入智能服务。\n\n这款工具非常适合普通用户、学生、职场人士以及需要私有化部署的企业团队使用。对于开发者而言，它也提供了便捷的自托管方案，支持一键部署到 Vercel 或 Zeabur 等平台。\n\nNextChat 的核心亮点在于其广泛的模型兼容性，原生支持 Claude、DeepSeek、GPT-4 及 Gemini Pro 等主流大模型，让用户在一个界面即可自由切换不同 AI 能力。此外，它还率先支持 MCP（Model Context Protocol）协议，增强了上下文处理能力。针对企业用户，NextChat 提供专业版解决方案，具备品牌定制、细粒度权限控制、内部知识库整合及安全审计等功能，满足公司对数据隐私和个性化管理的高标准要求。",87618,"2026-04-05T07:20:52",[13,26],{"id":44,"name":45,"github_repo":46,"description_zh":47,"stars":48,"difficulty_score":23,"last_commit_at":49,"category_tags":50,"status":16},2268,"ML-For-Beginners","microsoft\u002FML-For-Beginners","ML-For-Beginners 是由微软推出的一套系统化机器学习入门课程，旨在帮助零基础用户轻松掌握经典机器学习知识。这套课程将学习路径规划为 12 周，包含 26 节精炼课程和 52 道配套测验，内容涵盖从基础概念到实际应用的完整流程，有效解决了初学者面对庞大知识体系时无从下手、缺乏结构化指导的痛点。\n\n无论是希望转型的开发者、需要补充算法背景的研究人员，还是对人工智能充满好奇的普通爱好者，都能从中受益。课程不仅提供了清晰的理论讲解，还强调动手实践，让用户在循序渐进中建立扎实的技能基础。其独特的亮点在于强大的多语言支持，通过自动化机制提供了包括简体中文在内的 50 多种语言版本，极大地降低了全球不同背景用户的学习门槛。此外，项目采用开源协作模式，社区活跃且内容持续更新，确保学习者能获取前沿且准确的技术资讯。如果你正寻找一条清晰、友好且专业的机器学习入门之路，ML-For-Beginners 将是理想的起点。",84991,"2026-04-05T10:45:23",[14,51,52,53,15,54,26,13,55],"数据工具","视频","插件","其他","音频",{"id":57,"name":58,"github_repo":59,"description_zh":60,"stars":61,"difficulty_score":10,"last_commit_at":62,"category_tags":63,"status":16},3128,"ragflow","infiniflow\u002Fragflow","RAGFlow 是一款领先的开源检索增强生成（RAG）引擎，旨在为大语言模型构建更精准、可靠的上下文层。它巧妙地将前沿的 RAG 技术与智能体（Agent）能力相结合，不仅支持从各类文档中高效提取知识，还能让模型基于这些知识进行逻辑推理和任务执行。\n\n在大模型应用中，幻觉问题和知识滞后是常见痛点。RAGFlow 通过深度解析复杂文档结构（如表格、图表及混合排版），显著提升了信息检索的准确度，从而有效减少模型“胡编乱造”的现象，确保回答既有据可依又具备时效性。其内置的智能体机制更进一步，使系统不仅能回答问题，还能自主规划步骤解决复杂问题。\n\n这款工具特别适合开发者、企业技术团队以及 AI 研究人员使用。无论是希望快速搭建私有知识库问答系统，还是致力于探索大模型在垂直领域落地的创新者，都能从中受益。RAGFlow 提供了可视化的工作流编排界面和灵活的 API 接口，既降低了非算法背景用户的上手门槛，也满足了专业开发者对系统深度定制的需求。作为基于 Apache 2.0 协议开源的项目，它正成为连接通用大模型与行业专有知识之间的重要桥梁。",77062,"2026-04-04T04:44:48",[15,14,13,26,54],{"id":65,"github_repo":66,"name":67,"description_en":68,"description_zh":69,"ai_summary_zh":70,"readme_en":71,"readme_zh":72,"quickstart_zh":73,"use_case_zh":74,"hero_image_url":75,"owner_login":76,"owner_name":77,"owner_avatar_url":78,"owner_bio":79,"owner_company":80,"owner_location":80,"owner_email":80,"owner_twitter":80,"owner_website":81,"owner_url":82,"languages":83,"stars":92,"forks":93,"last_commit_at":94,"license":95,"difficulty_score":10,"env_os":96,"env_gpu":97,"env_ram":98,"env_deps":99,"category_tags":103,"github_topics":80,"view_count":23,"oss_zip_url":80,"oss_zip_packed_at":80,"status":16,"created_at":104,"updated_at":105,"faqs":106,"releases":134},3327,"meta-pytorch\u002Ftorchtune","torchtune","PyTorch native post-training library","torchtune 是 PyTorch 官方推出的原生大模型后训练库，旨在让开发者轻松构建、微调和实验大型语言模型。它主要解决了大模型定制过程中配置复杂、内存效率低以及多设备扩展难等痛点，提供了一套从监督微调（SFT）、知识蒸馏到人类反馈强化学习（如 DPO、PPO）的全流程解决方案。\n\n这款工具特别适合 AI 研究人员和工程开发者使用。无论是希望快速上手的新手，还是需要深入定制算法的专家，都能通过其模块化的设计灵活调整训练策略。torchtune 内置了 Llama、Gemma、Qwen 等主流模型的简洁 PyTorch 实现，并支持最新的 Llama 3.3 及多模态版本。\n\n其核心技术亮点在于极致的内存优化与性能表现，充分利用了最新的 PyTorch API，支持激活值卸载和多模态 QLoRA 等先进特性。用户可以通过简单的 YAML 配置文件，轻松在单卡、多卡乃至多节点集群上运行全量微调或参数高效微调（LoRA\u002FQLoRA）。此外，torchtune 还紧跟社区步伐，迅速适配如 Llama 4、Qwen3 等前沿模型，让研究者能第一时间利用最新架构进行探索。借助 torchtu","torchtune 是 PyTorch 官方推出的原生大模型后训练库，旨在让开发者轻松构建、微调和实验大型语言模型。它主要解决了大模型定制过程中配置复杂、内存效率低以及多设备扩展难等痛点，提供了一套从监督微调（SFT）、知识蒸馏到人类反馈强化学习（如 DPO、PPO）的全流程解决方案。\n\n这款工具特别适合 AI 研究人员和工程开发者使用。无论是希望快速上手的新手，还是需要深入定制算法的专家，都能通过其模块化的设计灵活调整训练策略。torchtune 内置了 Llama、Gemma、Qwen 等主流模型的简洁 PyTorch 实现，并支持最新的 Llama 3.3 及多模态版本。\n\n其核心技术亮点在于极致的内存优化与性能表现，充分利用了最新的 PyTorch API，支持激活值卸载和多模态 QLoRA 等先进特性。用户可以通过简单的 YAML 配置文件，轻松在单卡、多卡乃至多节点集群上运行全量微调或参数高效微调（LoRA\u002FQLoRA）。此外，torchtune 还紧跟社区步伐，迅速适配如 Llama 4、Qwen3 等前沿模型，让研究者能第一时间利用最新架构进行探索。借助 torchtune，大模型的后训练工作变得更加透明、高效且易于复现。","\n\n\n# torchtune\n\n[![Unit Test](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Factions\u002Fworkflows\u002Funit_test.yaml\u002Fbadge.svg?branch=main)](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Factions\u002Fworkflows\u002Funit_test.yaml)\n![Integration Tests](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Factions\u002Fworkflows\u002Fgpu_test.yaml\u002Fbadge.svg)\n[![](https:\u002F\u002Fdcbadge.vercel.app\u002Fapi\u002Fserver\u002F4Xsdn8Rr9Q?style=flat)](https:\u002F\u002Fdiscord.gg\u002F4Xsdn8Rr9Q)\n\n[**Overview**](#overview-) | [**Installation**](#installation-%EF%B8%8F) | [**Get Started**](#get-started-) |  [**Documentation**](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Findex.html) | [**Community**](#community-) | [**Citing torchtune**](#citing-torchtune-) | [**License**](#license)\n\n### 📣 Recent updates 📣\n* *May 2025*: torchtune has added support for *Qwen3* models! Check out all the configs [here](recipes\u002Fconfigs\u002Fqwen3)\n* *April 2025*: **Llama4** is now available in torchtune! Try out our full and LoRA finetuning configs [here](recipes\u002Fconfigs\u002Fllama4)\n* *February 2025*: Multi-node training is officially [open for business in torchtune](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmultinode.html)! Full finetune on multiple nodes to take advantage of larger batch sizes and models.\n* *December 2024*: torchtune now supports **Llama 3.3 70B**! Try it out by following our installation instructions [here](#installation-%EF%B8%8F), then run any of the configs [here](recipes\u002Fconfigs\u002Fllama3_3).\n* *November 2024*: torchtune has released [v0.4.0](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Freleases\u002Ftag\u002Fv0.4.0) which includes stable support for exciting features like activation offloading and multimodal QLoRA\n* *November 2024*: torchtune has added [Gemma2](recipes\u002Fconfigs\u002Fgemma2) to its models!\n* *October 2024*: torchtune added support for Qwen2.5 models - find the configs [here](recipes\u002Fconfigs\u002Fqwen2_5\u002F)\n* *September 2024*: torchtune has support for **Llama 3.2 11B Vision**, **Llama 3.2 3B**, and **Llama 3.2 1B** models! Try them out by following our installation instructions [here](#installation-%EF%B8%8F), then run any of the text configs [here](recipes\u002Fconfigs\u002Fllama3_2) or vision configs [here](recipes\u002Fconfigs\u002Fllama3_2_vision).\n\n\n&nbsp;\n\n## Overview 📚\n\n\ntorchtune is a PyTorch library for easily authoring, post-training, and experimenting with LLMs. It provides:\n\n- Hackable training recipes for SFT, knowledge distillation, DPO, PPO, GRPO, and quantization-aware training\n- Simple PyTorch implementations of popular LLMs like Llama, Gemma, Mistral, Phi, Qwen, and more\n- Best-in-class memory efficiency, performance improvements, and scaling, utilizing the latest PyTorch APIs\n- YAML configs for easily configuring training, evaluation, quantization or inference recipes\n\n&nbsp;\n\n### Post-training recipes\n\ntorchtune supports [the entire post-training lifecycle](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Frecipes\u002Frecipes_overview.html). A successful post-trained model will likely utilize several of the below methods.\n\n#### Supervised Finetuning (SFT)\n\n| Type of Weight Update | 1 Device | >1 Device | >1 Node |\n|-----------------------|:--------:|:---------:|:-------:|\n| Full                  |    ✅    |     ✅    |   ✅    |\n| [LoRA\u002FQLoRA](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fstable\u002Frecipes\u002Flora_finetune_single_device.html)            |    ✅    |     ✅    |    ✅    |\n\nExample: ``tune run lora_finetune_single_device --config llama3_2\u002F3B_lora_single_device`` \u003Cbr \u002F>\nYou can also run e.g. ``tune ls lora_finetune_single_device`` for a full list of available configs.\n\n#### [Knowledge Distillation (KD)](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002F0.4\u002Ftutorials\u002Fllama_kd_tutorial.html)\n\n| Type of Weight Update | 1 Device | >1 Device | >1 Node |\n|-----------------------|:--------:|:---------:|:-------:|\n| Full                  |    ❌    |     ❌    |    ❌    |\n| LoRA\u002FQLoRA            |    ✅    |     ✅    |    ❌    |\n\nExample: ``tune run knowledge_distillation_distributed --config qwen2\u002F1.5B_to_0.5B_KD_lora_distributed`` \u003Cbr \u002F>\nYou can also run e.g. ``tune ls knowledge_distillation_distributed`` for a full list of available configs.\n\n#### Reinforcement Learning \u002F Reinforcement Learning from Human Feedback (RLHF)\n\n| Method | Type of Weight Update | 1 Device | >1 Device | >1 Node |\n|------------------------------|-----------------------|:--------:|:---------:|:-------:|\n| [DPO](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fstable\u002Frecipes\u002Fdpo.html)                          | Full                  |    ❌    |     ✅    |    ❌    |\n|                           | LoRA\u002FQLoRA            |    ✅    |     ✅    |    ❌    |\n| PPO                          | Full                  |    ✅    |     ❌    |    ❌    |\n|                           | LoRA\u002FQLoRA            |    ❌    |     ❌    |    ❌    |\n| GRPO                         | Full                  |    🚧    |     ✅    |  ✅   |\n|                           | LoRA\u002FQLoRA            |    ❌    |     ❌    |    ❌    |\n\nExample: ``tune run lora_dpo_single_device --config llama3_1\u002F8B_dpo_single_device`` \u003Cbr \u002F>\nYou can also run e.g. ``tune ls full_dpo_distributed`` for a full list of available configs.\n\n#### [Quantization-Aware Training (QAT)](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fqat_finetune.html)\n\n| Type of Weight Update | 1 Device | >1 Device | >1 Node |\n|-----------------------|:--------:|:---------:|:-------:|\n| [Full](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fstable\u002Frecipes\u002Fqat_distributed.html)                  |    ✅     |     ✅    |    ❌    |\n| LoRA\u002FQLoRA            |    ❌    |     ✅    |    ❌    |\n\nExample: ``tune run qat_distributed --config llama3_1\u002F8B_qat_lora`` \u003Cbr \u002F>\nYou can also run e.g. ``tune ls qat_distributed`` or ``tune ls qat_single_device`` for a full list of available configs.\n\nThe above configs are just examples to get you started. The full list of recipes can be found [here](recipes\u002F). If you'd like to work on one of the gaps you see, please submit a PR! If there's a entirely new post-training method you'd like to see implemented in torchtune, feel free to open an Issue.\n\n&nbsp;\n\n### Models\n\nFor the above recipes, torchtune supports many state-of-the-art models available on the [Hugging Face Hub](https:\u002F\u002Fhuggingface.co\u002Fmodels) or [Kaggle Hub](https:\u002F\u002Fwww.kaggle.com\u002Fmodels). Some of our supported models:\n\n| Model                                         | Sizes     |\n|-----------------------------------------------|-----------|\n| [Llama4](https:\u002F\u002Fwww.llama.com\u002Fdocs\u002Fmodel-cards-and-prompt-formats\u002Fllama4)    | Scout (17B x 16E) [[models](torchtune\u002Fmodels\u002Fllama4\u002F_model_builders.py), [configs](recipes\u002Fconfigs\u002Fllama4\u002F)]        |\n| [Llama3.3](https:\u002F\u002Fwww.llama.com\u002Fdocs\u002Fmodel-cards-and-prompt-formats\u002Fllama3_3)    | 70B [[models](torchtune\u002Fmodels\u002Fllama3_3\u002F_model_builders.py), [configs](recipes\u002Fconfigs\u002Fllama3_3\u002F)]        |\n| [Llama3.2-Vision](https:\u002F\u002Fwww.llama.com\u002Fdocs\u002Fmodel-cards-and-prompt-formats\u002Fllama3_2#-llama-3.2-vision-models-(11b\u002F90b)-)    | 11B, 90B [[models](torchtune\u002Fmodels\u002Fllama3_2_vision\u002F_model_builders.py), [configs](recipes\u002Fconfigs\u002Fllama3_2_vision\u002F)]        |\n| [Llama3.2](https:\u002F\u002Fwww.llama.com\u002Fdocs\u002Fmodel-cards-and-prompt-formats\u002Fllama3_2)    | 1B, 3B [[models](torchtune\u002Fmodels\u002Fllama3_2\u002F_model_builders.py), [configs](recipes\u002Fconfigs\u002Fllama3_2\u002F)]        |\n| [Llama3.1](https:\u002F\u002Fllama.meta.com\u002Fdocs\u002Fmodel-cards-and-prompt-formats\u002Fllama3_1)    | 8B, 70B, 405B [[models](torchtune\u002Fmodels\u002Fllama3_1\u002F_model_builders.py), [configs](recipes\u002Fconfigs\u002Fllama3_1\u002F)]        |\n| [Mistral](https:\u002F\u002Fhuggingface.co\u002Fmistralai)   | 7B [[models](torchtune\u002Fmodels\u002Fmistral\u002F_model_builders.py), [configs](recipes\u002Fconfigs\u002Fmistral\u002F)] |\n| [Gemma2](https:\u002F\u002Fhuggingface.co\u002Fdocs\u002Ftransformers\u002Fmain\u002Fen\u002Fmodel_doc\u002Fgemma2)   | 2B, 9B, 27B [[models](torchtune\u002Fmodels\u002Fgemma2\u002F_model_builders.py), [configs](recipes\u002Fconfigs\u002Fgemma2\u002F)] |\n| [Microsoft Phi4](https:\u002F\u002Fhuggingface.co\u002Fcollections\u002Fmicrosoft\u002Fphi-4-677e9380e514feb5577a40e4) | 14B [[models](torchtune\u002Fmodels\u002Fphi4\u002F), [configs](recipes\u002Fconfigs\u002Fphi4\u002F)]\n| [Microsoft Phi3](https:\u002F\u002Fhuggingface.co\u002Fcollections\u002Fmicrosoft\u002Fphi-3-6626e15e9585a200d2d761e3) | Mini [[models](torchtune\u002Fmodels\u002Fphi3\u002F), [configs](recipes\u002Fconfigs\u002Fphi3\u002F)]\n| [Qwen3](https:\u002F\u002Fqwenlm.github.io\u002Fblog\u002Fqwen3\u002F) | 0.6B, 1.7B, 4B, 8B, 14B, 32B [[models](torchtune\u002Fmodels\u002Fqwen3\u002F), [configs](recipes\u002Fconfigs\u002Fqwen3\u002F)]\n| [Qwen2.5](https:\u002F\u002Fqwenlm.github.io\u002Fblog\u002Fqwen2.5\u002F) | 0.5B, 1.5B, 3B, 7B, 14B, 32B, 72B [[models](torchtune\u002Fmodels\u002Fqwen2_5\u002F), [configs](recipes\u002Fconfigs\u002Fqwen2_5\u002F)]\n| [Qwen2](https:\u002F\u002Fqwenlm.github.io\u002Fblog\u002Fqwen2\u002F) | 0.5B, 1.5B, 7B [[models](torchtune\u002Fmodels\u002Fqwen2\u002F), [configs](recipes\u002Fconfigs\u002Fqwen2\u002F)]\n\nWe're always adding new models, but feel free to [file an issue](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Fissues\u002Fnew) if there's a new one you would like to see in torchtune.\n\n&nbsp;\n\n### Memory and training speed\n\nBelow is an example of the memory requirements and training speed for different Llama 3.1 models.\n\n> [!NOTE]\n> For ease of comparison, all the below numbers are provided for batch size 2 (without gradient accumulation), a dataset packed to sequence length 2048, and torch compile enabled.\n\nIf you are interested in running on different hardware or with different models, check out our documentation on memory optimizations [here](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmemory_optimizations.html) to find the right setup for you.\n\n| Model | Finetuning Method | Runnable On | Peak Memory per GPU | Tokens\u002Fsec * |\n|:-:|:-:|:-:|:-:|:-:|\n| Llama 3.1 8B | Full finetune | 1x 4090 | 18.9 GiB | 1650 |\n| Llama 3.1 8B | Full finetune | 1x A6000 | 37.4 GiB |  2579|\n| Llama 3.1 8B | LoRA | 1x 4090 |  16.2 GiB | 3083 |\n| Llama 3.1 8B | LoRA | 1x A6000 | 30.3 GiB  | 4699 |\n| Llama 3.1 8B | QLoRA | 1x 4090 | 7.4 GiB | 2413  |\n| Llama 3.1 70B | Full finetune | 8x A100  | 13.9 GiB ** | 1568  |\n| Llama 3.1 70B | LoRA | 8x A100 | 27.6 GiB  | 3497  |\n| Llama 3.1 405B | QLoRA | 8x A100 | 44.8 GB  | 653  |\n\n*= Measured over one full training epoch \u003Cbr \u002F>\n**= Uses CPU offload with fused optimizer\n\n&nbsp;\n\n### Optimization flags\n\ntorchtune exposes a number of levers for memory efficiency and performance. The table below demonstrates the effects of applying some of these techniques sequentially to the Llama 3.2 3B model. Each technique is added on top of the previous one, except for LoRA and QLoRA, which do not use `optimizer_in_bwd` or `AdamW8bit` optimizer.\n\n> Baseline uses Recipe=**full_finetune_single_device**, Model=**Llama 3.2 3B**, Batch size=**2**, Max sequence length=**4096**, Precision=**bf16**, Hardware=**A100**\n\n| Technique | Peak Memory Active (GiB) | % Change Memory vs Previous | Tokens Per Second | % Change Tokens\u002Fsec vs Previous|\n|:--|:-:|:-:|:-:|:-:|\n| Baseline | 25.5 | - | 2091 | - |\n| [+ Packed Dataset](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fbasics\u002Fpacking.html) | 60.0 | +135.16% | 7075 | +238.40% |\n| [+ Compile](https:\u002F\u002Fpytorch.org\u002Ftutorials\u002Fintermediate\u002Ftorch_compile_tutorial.html) | 51.0 | -14.93% | 8998 | +27.18% |\n| [+ Chunked Cross Entropy](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fgenerated\u002Ftorchtune.modules.loss.CEWithChunkedOutputLoss.html) | 42.9 | -15.83% | 9174 | +1.96% |\n| [+ Activation Checkpointing](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmemory_optimizations.html#activation-checkpointing) | 24.9 | -41.93% | 7210 | -21.41% |\n| [+ Fuse optimizer step into backward](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmemory_optimizations.html#fusing-optimizer-step-into-backward-pass) | 23.1 | -7.29% | 7309 | +1.38% |\n| [+ Activation Offloading](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmemory_optimizations.html#activation-offloading) | 21.8 | -5.48% | 7301 | -0.11% |\n| [+ 8-bit AdamW](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmemory_optimizations.html#lower-precision-optimizers) | 17.6 | -19.63% | 6960 | -4.67% |\n| [LoRA](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmemory_optimizations.html#glossary-lora) | 8.5 | -51.61% | 8210 | +17.96% |\n| [QLoRA](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmemory_optimizations.html#quantized-low-rank-adaptation-qlora) | 4.6 | -45.71% | 8035 | -2.13% |\n\nThe final row in the table vs baseline + Packed Dataset uses **81.9%** less memory with a **284.3%** increase in tokens per second.\n\n\u003Cdetails>\n\u003Csummary>Command to reproduce final row.\u003C\u002Fsummary>\n\n```bash\ntune run lora_finetune_single_device --config llama3_2\u002F3B_qlora_single_device \\\ndataset.packed=True \\\ncompile=True \\\nloss=torchtune.modules.loss.CEWithChunkedOutputLoss \\\nenable_activation_checkpointing=True \\\noptimizer_in_bwd=False \\\nenable_activation_offloading=True \\\noptimizer=torch.optim.AdamW \\\ntokenizer.max_seq_len=4096 \\\ngradient_accumulation_steps=1 \\\nepochs=1 \\\nbatch_size=2\n```\n\n\u003C\u002Fdetails>\n\n&nbsp;\n\n## Installation 🛠️\n\n\ntorchtune is **only** tested with the latest stable PyTorch release (currently 2.6.0) as well as the preview nightly version, and leverages\ntorchvision for finetuning multimodal LLMs and torchao for the latest in quantization techniques; you should install these as well.\n\n### Install stable release\n\n```bash\n# Install stable PyTorch, torchvision, torchao stable releases\npip install torch torchvision torchao\npip install torchtune\n```\n\n### Install nightly release\n\n```bash\n# Install PyTorch, torchvision, torchao nightlies.\npip install --pre --upgrade torch torchvision torchao --index-url https:\u002F\u002Fdownload.pytorch.org\u002Fwhl\u002Fnightly\u002Fcu126 # full options are cpu\u002Fcu118\u002Fcu124\u002Fcu126\u002Fxpu\u002Frocm6.2\u002Frocm6.3\u002Frocm6.4\npip install --pre --upgrade torchtune --extra-index-url https:\u002F\u002Fdownload.pytorch.org\u002Fwhl\u002Fnightly\u002Fcpu\n```\n\nYou can also check out our [install documentation](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Finstall.html) for more information, including installing torchtune from source.\n\n&nbsp;\n\nTo confirm that the package is installed correctly, you can run the following command:\n\n```bash\ntune --help\n```\n\nAnd should see the following output:\n\n```bash\nusage: tune [-h] {ls,cp,download,run,validate} ...\n\nWelcome to the torchtune CLI!\n\noptions:\n  -h, --help            show this help message and exit\n\n...\n```\n\n&nbsp;\n\n## Get Started 🚀\n\n\nTo get started with torchtune, see our [First Finetune Tutorial](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Ffirst_finetune_tutorial.html). Our [End-to-End Workflow Tutorial](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fe2e_flow.html) will show you how to evaluate, quantize, and run inference with a Llama model. The rest of this section will provide a quick overview of these steps with Llama3.1.\n\n\n### Downloading a model\n\nFollow the instructions on the official [`meta-llama`](https:\u002F\u002Fhuggingface.co\u002Fmeta-llama) repository to ensure you have access to the official Llama model weights. Once you have confirmed access, you can run the following command to download the weights to your local machine. This will also download the tokenizer model and a responsible use guide.\n\nTo download Llama3.1, you can run:\n\n```bash\ntune download meta-llama\u002FMeta-Llama-3.1-8B-Instruct \\\n--output-dir \u002Ftmp\u002FMeta-Llama-3.1-8B-Instruct \\\n--ignore-patterns \"original\u002Fconsolidated.00.pth\" \\\n--hf-token \u003CHF_TOKEN> \\\n```\n\n> [!Tip]\n> Set your environment variable `HF_TOKEN` or pass in `--hf-token` to the command in order to validate your access. You can find your token at https:\u002F\u002Fhuggingface.co\u002Fsettings\u002Ftokens\n\n### Running finetuning recipes\n\nYou can finetune Llama3.1 8B with LoRA on a single GPU using the following command:\n\n```bash\ntune run lora_finetune_single_device --config llama3_1\u002F8B_lora_single_device\n```\n\nFor distributed training, tune CLI integrates with [torchrun](https:\u002F\u002Fpytorch.org\u002Fdocs\u002Fstable\u002Felastic\u002Frun.html).\nTo run a full finetune of Llama3.1 8B on two GPUs:\n\n```bash\ntune run --nproc_per_node 2 full_finetune_distributed --config llama3_1\u002F8B_full\n```\n\n> [!Tip]\n> Make sure to place any torchrun commands **before** the recipe specification. Any CLI args after this will override the config and not impact distributed training.\n\n### Modify Configs\n\nThere are two ways in which you can modify configs:\n\n**Config Overrides**\n\nYou can directly overwrite config fields from the command line:\n\n```bash\ntune run lora_finetune_single_device \\\n--config llama2\u002F7B_lora_single_device \\\nbatch_size=8 \\\nenable_activation_checkpointing=True \\\nmax_steps_per_epoch=128\n```\n\n**Update a Local Copy**\n\nYou can also copy the config to your local directory and modify the contents directly:\n\n```bash\ntune cp llama3_1\u002F8B_full .\u002Fmy_custom_config.yaml\nCopied to .\u002Fmy_custom_config.yaml\n```\n\nThen, you can run your custom recipe by directing the `tune run` command to your local files:\n\n```bash\ntune run full_finetune_distributed --config .\u002Fmy_custom_config.yaml\n```\n\nCheck out `tune --help` for all possible CLI commands and options. For more information on using and updating configs, take a look at our [config deep-dive](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fdeep_dives\u002Fconfigs.html).\n\n### Custom Datasets\n\ntorchtune supports finetuning on a variety of different datasets, including [instruct-style](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fbasics\u002Finstruct_datasets.html), [chat-style](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fbasics\u002Fchat_datasets.html), [preference datasets](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fbasics\u002Fpreference_datasets.html), and more. If you want to learn more about how to apply these components to finetune on your own custom dataset, please check out the provided links along with our [API docs](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fapi_ref_datasets.html).\n\n### Custom Devices\ntorchtune supports finetuning on a variety of devices, including NVIDIA GPU, Intel XPU, AMD ROCm, Apple MPS, and Ascend NPU. If you're interested in running recipes on a custom device, such as Intel XPU, follow the steps below.\n\nStep 1: Refer to the [Getting Started on Intel GPU guide](https:\u002F\u002Fdocs.pytorch.org\u002Fdocs\u002Fstable\u002Fnotes\u002Fget_start_xpu.html) to configure your environment.\n\nStep 2: Update device information via either CLI override or config changes.\nYou can directly overwrite config fields from the command line:\n\n```bash\ntune run lora_finetune_single_device --config llama3_1\u002F8B_lora_single_device device=xpu\n```\nOr edit your local copy of configuration files and replace `device: cuda` with `device: xpu`\n\n&nbsp;\n\n## Community 🌍\n\ntorchtune focuses on integrating with popular tools and libraries from the ecosystem. These are just a few examples, with more under development:\n\n- [Hugging Face Hub](https:\u002F\u002Fhuggingface.co\u002Fdocs\u002Fhub\u002Fen\u002Findex) for [accessing model weights](torchtune\u002F_cli\u002Fdownload.py)\n- [EleutherAI's LM Eval Harness](https:\u002F\u002Fgithub.com\u002FEleutherAI\u002Flm-evaluation-harness) for [evaluating](recipes\u002Feleuther_eval.py) trained models\n- [Hugging Face Datasets](https:\u002F\u002Fhuggingface.co\u002Fdocs\u002Fdatasets\u002Fen\u002Findex) for [access](torchtune\u002Fdatasets\u002F_instruct.py) to training and evaluation datasets\n- [PyTorch FSDP2](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtitan\u002Fblob\u002Fmain\u002Fdocs\u002Ffsdp.md) for distributed training\n- [torchao](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Fao) for lower precision dtypes and [post-training quantization](recipes\u002Fquantize.py) techniques\n- [Weights & Biases](https:\u002F\u002Fwandb.ai\u002Fsite) for [logging](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fdeep_dives\u002Fwandb_logging.html) metrics and checkpoints, and tracking training progress\n- [Comet](https:\u002F\u002Fwww.comet.com\u002Fsite\u002F) as another option for [logging](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fdeep_dives\u002Fcomet_logging.html)\n- [ExecuTorch](https:\u002F\u002Fpytorch.org\u002Fexecutorch-overview) for [on-device inference](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Fexecutorch\u002Ftree\u002Fmain\u002Fexamples\u002Fmodels\u002Fllama2#optional-finetuning) using finetuned models\n- [bitsandbytes](https:\u002F\u002Fhuggingface.co\u002Fdocs\u002Fbitsandbytes\u002Fmain\u002Fen\u002Findex) for low memory optimizers for our [single-device recipes](recipes\u002Fconfigs\u002Fllama2\u002F7B_full_low_memory.yaml)\n- [PEFT](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Fpeft) for continued finetuning or inference with torchtune models in the Hugging Face ecosystem\n\n&nbsp;\n\n### Community Contributions\n\nWe really value our community and the contributions made by our wonderful users. We'll use this section to call out some of these contributions. If you'd like to help out as well, please see the [CONTRIBUTING](CONTRIBUTING.md) guide.\n\n- [@SalmanMohammadi](https:\u002F\u002Fgithub.com\u002Fsalmanmohammadi) for adding a comprehensive end-to-end recipe for [Reinforcement Learning from Human Feedback (RLHF)](recipes\u002Fppo_full_finetune_single_device.py) finetuning with PPO to torchtune\n- [@fyabc](https:\u002F\u002Fgithub.com\u002Ffyabc) for adding Qwen2 models, tokenizer, and recipe integration to torchtune\n- [@solitude-alive](https:\u002F\u002Fgithub.com\u002Fsolitude-alive) for adding the [Gemma 2B model](torchtune\u002Fmodels\u002Fgemma\u002F) to torchtune, including recipe changes, numeric validations of the models and recipe correctness\n- [@yechenzhi](https:\u002F\u002Fgithub.com\u002Fyechenzhi) for adding [Direct Preference Optimization (DPO)](recipes\u002Flora_dpo_single_device.py) to torchtune, including the recipe and config along with correctness checks\n- [@Optimox](https:\u002F\u002Fgithub.com\u002FOptimox) for adding all the [Gemma2 variants](torchtune\u002Fmodels\u002Fgemma2) to torchtune!\n\n\n&nbsp;\n\n## Acknowledgements 🙏\n\nThe transformer code in this repository is inspired by the original [Llama2 code](https:\u002F\u002Fgithub.com\u002Fmeta-llama\u002Fllama\u002Fblob\u002Fmain\u002Fllama\u002Fmodel.py). We also want to give a huge shout-out to EleutherAI, Hugging Face and\nWeights & Biases for being wonderful collaborators and for working with us on some of these integrations within torchtune. In addition, we want to acknowledge some other awesome libraries and tools from the ecosystem:\n\n- [gpt-fast](https:\u002F\u002Fgithub.com\u002Fmeta-pytorch\u002Fgpt-fast) for performant LLM inference techniques which we've adopted out-of-the-box\n- [llama recipes](https:\u002F\u002Fgithub.com\u002Fmeta-llama\u002Fllama-recipes) for spring-boarding the llama2 community\n- [bitsandbytes](https:\u002F\u002Fgithub.com\u002FTimDettmers\u002Fbitsandbytes) for bringing several memory and performance based techniques to the PyTorch ecosystem\n- [@winglian](https:\u002F\u002Fgithub.com\u002Fwinglian\u002F) and [axolotl](https:\u002F\u002Fgithub.com\u002FOpenAccess-AI-Collective\u002Faxolotl) for early feedback and brainstorming on torchtune's design and feature set.\n- [lit-gpt](https:\u002F\u002Fgithub.com\u002FLightning-AI\u002Flitgpt) for pushing the LLM finetuning community forward.\n- [HF TRL](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Ftrl) for making reward modeling more accessible to the PyTorch community.\n\n&nbsp;\n\n## Citing torchtune 📝\n\nIf you find the torchtune library useful, please cite it in your work as below.\n\n```bibtex\n@software{torchtune,\n  title = {torchtune: PyTorch's finetuning library},\n  author = {torchtune maintainers and contributors},\n  url = {https\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune},\n  license = {BSD-3-Clause},\n  month = apr,\n  year = {2024}\n}\n```\n\n&nbsp;\n\n## License\n\ntorchtune is released under the [BSD 3 license](.\u002FLICENSE). However you may have other legal obligations that govern your use of other content, such as the terms of service for third-party models.\n","# torchtune\n\n[![单元测试](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Factions\u002Fworkflows\u002Funit_test.yaml\u002Fbadge.svg?branch=main)](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Factions\u002Fworkflows\u002Funit_test.yaml)\n![集成测试](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Factions\u002Fworkflows\u002Fgpu_test.yaml\u002Fbadge.svg)\n[![](https:\u002F\u002Fdcbadge.vercel.app\u002Fapi\u002Fserver\u002F4Xsdn8Rr9Q?style=flat)](https:\u002F\u002Fdiscord.gg\u002F4Xsdn8Rr9Q)\n\n[**概述**](#overview-) | [**安装**](#installation-%EF%B8%8F) | [**开始使用**](#get-started-) |  [**文档**](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Findex.html) | [**社区**](#community-) | [**引用 torchtune**](#citing-torchtune-) | [**许可证**](#license)\n\n### 📣 最新更新 📣\n* *2025年5月*: torchtune 已新增对 *Qwen3* 模型的支持！所有配置请查看 [这里](recipes\u002Fconfigs\u002Fqwen3)\n* *2025年4月*: **Llama4** 现已在 torchtune 中可用！您可以尝试我们的全参数和 LoRA 微调配置 [这里](recipes\u002Fconfigs\u002Fllama4)\n* *2025年2月*: 多节点训练现已在 torchtune 中正式开放 [使用指南](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmultinode.html)！通过多节点进行全参数微调，以充分利用更大的批量大小和模型规模。\n* *2024年12月*: torchtune 现已支持 **Llama 3.3 70B**！按照我们的安装说明 [这里](#installation-%EF%B8%8F) 进行设置，然后运行任意一个配置 [这里](recipes\u002Fconfigs\u002Fllama3_3)。\n* *2024年11月*: torchtune 发布了 [v0.4.0](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Freleases\u002Ftag\u002Fv0.4.0)，其中包含了对激活卸载和多模态 QLoRA 等激动人心功能的稳定支持。\n* *2024年11月*: torchtune 已将 [Gemma2](recipes\u002Fconfigs\u002Fgemma2) 添加到其支持的模型列表中！\n* *2024年10月*: torchtune 增加了对 Qwen2.5 模型的支持——相关配置请见 [这里](recipes\u002Fconfigs\u002Fqwen2_5\u002F)。\n* *2024年9月*: torchtune 现已支持 **Llama 3.2 11B Vision**、**Llama 3.2 3B** 和 **Llama 3.2 1B** 模型！按照我们的安装说明 [这里](#installation-%EF%B8%8F) 进行设置，然后运行任意一个文本配置 [这里](recipes\u002Fconfigs\u002Fllama3_2) 或视觉配置 [这里](recipes\u002Fconfigs\u002Fllama3_2_vision)。\n\n&nbsp;\n\n## 概述 📚\n\n\ntorchtune 是一个基于 PyTorch 的库，用于轻松构建、后训练和实验大型语言模型。它提供：\n\n- 可自定义的训练配方，适用于 SFT、知识蒸馏、DPO、PPO、GRPO 以及量化感知训练；\n- Llama、Gemma、Mistral、Phi、Qwen 等流行 LLM 的简单 PyTorch 实现；\n- 利用最新 PyTorch API 实现的一流内存效率、性能优化和扩展能力；\n- 使用 YAML 配置文件，方便地配置训练、评估、量化或推理流程。\n\n&nbsp;\n\n### 后训练配方\n\ntorchtune 支持 [完整的后训练生命周期](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Frecipes\u002Frecipes_overview.html)。一个成功的后训练模型通常会结合以下几种方法。\n\n#### 监督微调 (SFT)\n\n| 权重更新方式 | 1 台设备 | >1 台设备 | >1 节点 |\n|-----------------------|:--------:|:---------:|:-------:|\n| 全参数                  |    ✅    |     ✅    |   ✅    |\n| [LoRA\u002FQLoRA](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fstable\u002Frecipes\u002Flora_finetune_single_device.html)            |    ✅    |     ✅    |    ✅    |\n\n示例：``tune run lora_finetune_single_device --config llama3_2\u002F3B_lora_single_device`` \u003Cbr \u002F>\n您也可以运行例如 ``tune ls lora_finetune_single_device`` 来查看所有可用配置。\n\n#### [知识蒸馏 (KD)](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002F0.4\u002Ftutorials\u002Fllama_kd_tutorial.html)\n\n| 权重更新方式 | 1 台设备 | >1 台设备 | >1 节点 |\n|-----------------------|:--------:|:---------:|:-------:|\n| 全参数                  |    ❌    |     ❌    |    ❌    |\n| LoRA\u002FQLoRA            |    ✅    |     ✅    |    ❌    |\n\n示例：``tune run knowledge_distillation_distributed --config qwen2\u002F1.5B_to_0.5B_KD_lora_distributed`` \u003Cbr \u002F>\n您还可以运行例如 ``tune ls knowledge_distillation_distributed`` 查看所有可用配置。\n\n#### 强化学习 \u002F 人类反馈强化学习 (RLHF)\n\n| 方法 | 权重更新方式 | 1 台设备 | >1 台设备 | >1 节点 |\n|------------------------------|-----------------------|:--------:|:---------:|:-------:|\n| [DPO](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fstable\u002Frecipes\u002Fdpo.html)                          | 全参数                  |    ❌    |     ✅    |    ❌    |\n|                           | LoRA\u002FQLoRA            |    ✅    |     ✅    |    ❌    |\n| PPO                          | 全参数                  |    ✅    |     ❌    |    ❌    |\n|                           | LoRA\u002FQLoRA            |    ❌    |     ❌    |    ❌    |\n| GRPO                         | 全参数                  |    🚧    |     ✅    |  ✅   |\n|                           | LoRA\u002FQLoRA            |    ❌    |     ❌    |    ❌    |\n\n示例：``tune run lora_dpo_single_device --config llama3_1\u002F8B_dpo_single_device`` \u003Cbr \u002F>\n您也可以运行例如 ``tune ls full_dpo_distributed`` 查看所有可用配置。\n\n#### [量化感知训练 (QAT)](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fqat_finetune.html)\n\n| 权重更新方式 | 1 台设备 | >1 台设备 | >1 节点 |\n|-----------------------|:--------:|:---------:|:-------:|\n| [全参数](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fstable\u002Frecipes\u002Fqat_distributed.html)                  |    ✅     |     ✅    |    ❌    |\n| LoRA\u002FQLoRA            |    ❌    |     ✅    |    ❌    |\n\n示例：``tune run qat_distributed --config llama3_1\u002F8B_qat_lora`` \u003Cbr \u002F>\n您还可以运行例如 ``tune ls qat_distributed`` 或 ``tune ls qat_single_device`` 查看所有可用配置。\n\n以上配置仅为入门示例。完整的配方列表可在 [这里](recipes\u002F) 找到。如果您希望填补其中的空白，请提交 PR！如果您有全新的后训练方法想要在 torchtune 中实现，也欢迎随时提出 Issue。\n\n### 模型\n\n对于上述配方，torchtune 支持许多在 [Hugging Face Hub](https:\u002F\u002Fhuggingface.co\u002Fmodels) 或 [Kaggle Hub](https:\u002F\u002Fwww.kaggle.com\u002Fmodels) 上可用的最先进模型。我们支持的部分模型如下：\n\n| 模型                                         | 尺寸     |\n|-----------------------------------------------|-----------|\n| [Llama4](https:\u002F\u002Fwww.llama.com\u002Fdocs\u002Fmodel-cards-and-prompt-formats\u002Fllama4)    | Scout (17B x 16E) [[模型](torchtune\u002Fmodels\u002Fllama4\u002F_model_builders.py), [配置](recipes\u002Fconfigs\u002Fllama4\u002F)]        |\n| [Llama3.3](https:\u002F\u002Fwww.llama.com\u002Fdocs\u002Fmodel-cards-and-prompt-formats\u002Fllama3_3)    | 70B [[模型](torchtune\u002Fmodels\u002Fllama3_3\u002F_model_builders.py), [配置](recipes\u002Fconfigs\u002Fllama3_3\u002F)]        |\n| [Llama3.2-Vision](https:\u002F\u002Fwww.llama.com\u002Fdocs\u002Fmodel-cards-and-prompt-formats\u002Fllama3_2#-llama-3.2-vision-models-(11b\u002F90b)-)    | 11B、90B [[模型](torchtune\u002Fmodels\u002Fllama3_2_vision\u002F_model_builders.py), [配置](recipes\u002Fconfigs\u002Fllama3_2_vision\u002F)]        |\n| [Llama3.2](https:\u002F\u002Fwww.llama.com\u002Fdocs\u002Fmodel-cards-and-prompt-formats\u002Fllama3_2)    | 1B、3B [[模型](torchtune\u002Fmodels\u002Fllama3_2\u002F_model_builders.py), [配置](recipes\u002Fconfigs\u002Fllama3_2\u002F)]        |\n| [Llama3.1](https:\u002F\u002Fllama.meta.com\u002Fdocs\u002Fmodel-cards-and-prompt-formats\u002Fllama3_1)    | 8B、70B、405B [[模型](torchtune\u002Fmodels\u002Fllama3_1\u002F_model_builders.py), [配置](recipes\u002Fconfigs\u002Fllama3_1\u002F)]        |\n| [Mistral](https:\u002F\u002Fhuggingface.co\u002Fmistralai)   | 7B [[模型](torchtune\u002Fmodels\u002Fmistral\u002F_model_builders.py), [配置](recipes\u002Fconfigs\u002Fmistral\u002F)] |\n| [Gemma2](https:\u002F\u002Fhuggingface.co\u002Fdocs\u002Ftransformers\u002Fmain\u002Fen\u002Fmodel_doc\u002Fgemma2)   | 2B、9B、27B [[模型](torchtune\u002Fmodels\u002Fgemma2\u002F_model_builders.py), [配置](recipes\u002Fconfigs\u002Fgemma2\u002F)] |\n| [Microsoft Phi4](https:\u002F\u002Fhuggingface.co\u002Fcollections\u002Fmicrosoft\u002Fphi-4-677e9380e514feb5577a40e4) | 14B [[模型](torchtune\u002Fmodels\u002Fphi4\u002F), [配置](recipes\u002Fconfigs\u002Fphi4\u002F)]\n| [Microsoft Phi3](https:\u002F\u002Fhuggingface.co\u002Fcollections\u002Fmicrosoft\u002Fphi-3-6626e15e9585a200d2d761e3) | Mini [[模型](torchtune\u002Fmodels\u002Fphi3\u002F), [配置](recipes\u002Fconfigs\u002Fphi3\u002F)]\n| [Qwen3](https:\u002F\u002Fqwenlm.github.io\u002Fblog\u002Fqwen3\u002F) | 0.6B、1.7B、4B、8B、14B、32B [[模型](torchtune\u002Fmodels\u002Fqwen3\u002F), [配置](recipes\u002Fconfigs\u002Fqwen3\u002F)]\n| [Qwen2.5](https:\u002F\u002Fqwenlm.github.io\u002Fblog\u002Fqwen2.5\u002F) | 0.5B、1.5B、3B、7B、14B、32B、72B [[模型](torchtune\u002Fmodels\u002Fqwen2_5\u002F), [配置](recipes\u002Fconfigs\u002Fqwen2_5\u002F)]\n| [Qwen2](https:\u002F\u002Fqwenlm.github.io\u002Fblog\u002Fqwen2\u002F) | 0.5B、1.5B、7B [[模型](torchtune\u002Fmodels\u002Fqwen2\u002F), [配置](recipes\u002Fconfigs\u002Fqwen2\u002F)]\n\n我们一直在添加新模型，但如果您希望在 torchtune 中看到某个新模型，请随时 [提交 issue](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Fissues\u002Fnew)。\n\n&nbsp;\n\n### 内存与训练速度\n\n以下是不同 Llama 3.1 模型的内存需求和训练速度示例。\n\n> [!注意]\n> 为了便于比较，以下所有数据均基于批次大小为 2（无梯度累积）、数据集打包至序列长度 2048，以及启用 Torch 编译的情况。\n\n如果您有兴趣在不同的硬件或使用不同的模型上运行，请查看我们的内存优化文档 [这里](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmemory_optimizations.html)，以找到适合您的设置。\n\n| 模型 | 微调方法 | 可运行设备 | 每 GPU 峰值内存 | 每秒处理的 token 数 * |\n|:-:|:-:|:-:|:-:|:-:|\n| Llama 3.1 8B | 全量微调 | 1x 4090 | 18.9 GiB | 1650 |\n| Llama 3.1 8B | 全量微调 | 1x A6000 | 37.4 GiB | 2579 |\n| Llama 3.1 8B | LoRA | 1x 4090 | 16.2 GiB | 3083 |\n| Llama 3.1 8B | LoRA | 1x A6000 | 30.3 GiB | 4699 |\n| Llama 3.1 8B | QLoRA | 1x 4090 | 7.4 GiB | 2413 |\n| Llama 3.1 70B | 全量微调 | 8x A100 | 13.9 GiB ** | 1568 |\n| Llama 3.1 70B | LoRA | 8x A100 | 27.6 GiB | 3497 |\n| Llama 3.1 405B | QLoRA | 8x A100 | 44.8 GB | 653 |\n\n*= 在一个完整训练周期内测量 \u003Cbr \u002F>\n**= 使用带有融合优化器的 CPU offload\n\n&nbsp;\n\n### 优化标志\n\ntorchtune 提供了多个用于提高内存效率和性能的工具。下表展示了将其中一些技术依次应用于 Llama 3.2 3B 模型的效果。每种技术都是在前一种技术的基础上添加的，除了 LoRA 和 QLoRA，它们不使用 `optimizer_in_bwd` 或 `AdamW8bit` 优化器。\n\n> 基线使用配方=**full_finetune_single_device**，模型=**Llama 3.2 3B**，批次大小=**2**，最大序列长度=**4096**，精度=**bf16**，硬件=**A100**\n\n| 技术 | 峰值活动内存 (GiB) | 相对于前一次的内存变化 (%) | 每秒处理的 token 数 | 相对于前一次的 token 数变化 (%) |\n|:--|:-:|:-:|:-:|:-:|\n| 基线 | 25.5 | - | 2091 | - |\n| [+ 数据集打包](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fbasics\u002Fpacking.html) | 60.0 | +135.16% | 7075 | +238.40% |\n| [+ 编译](https:\u002F\u002Fpytorch.org\u002Ftutorials\u002Fintermediate\u002Ftorch_compile_tutorial.html) | 51.0 | -14.93% | 8998 | +27.18% |\n| [+ 分块交叉熵](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fgenerated\u002Ftorchtune.modules.loss.CEWithChunkedOutputLoss.html) | 42.9 | -15.83% | 9174 | +1.96% |\n| [+ 激活检查点](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmemory_optimizations.html#activation-checkpointing) | 24.9 | -41.93% | 7210 | -21.41% |\n| [+ 将优化步骤融入反向传播](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmemory_optimizations.html#fusing-optimizer-step-into-backward-pass) | 23.1 | -7.29% | 7309 | +1.38% |\n| [+ 激活 offloading](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmemory_optimizations.html#activation-offloading) | 21.8 | -5.48% | 7301 | -0.11% |\n| [+ 8位 AdamW](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmemory_optimizations.html#lower-precision-optimizers) | 17.6 | -19.63% | 6960 | -4.67% |\n| [LoRA](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmemory_optimizations.html#glossary-lora) | 8.5 | -51.61% | 8210 | +17.96% |\n| [QLoRA](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmemory_optimizations.html#quantized-low-rank-adaptation-qlora) | 4.6 | -45.71% | 8035 | -2.13% |\n\n表格中的最后一行与基线 + 数据集打包相比，内存减少了 **81.9%**，而每秒处理的 token 数增加了 **284.3%**。\n\n\u003Cdetails>\n\u003Csummary>重现最后一行的命令。\u003C\u002Fsummary>\n\n```bash\ntune run lora_finetune_single_device --config llama3_2\u002F3B_qlora_single_device \\\ndataset.packed=True \\\ncompile=True \\\nloss=torchtune.modules.loss.CEWithChunkedOutputLoss \\\nenable_activation_checkpointing=True \\\noptimizer_in_bwd=False \\\nenable_activation_offloading=True \\\noptimizer=torch.optim.AdamW \\\ntokenizer.max_seq_len=4096 \\\ngradient_accumulation_steps=1 \\\nepochs=1 \\\nbatch_size=2\n```\n\n\u003C\u002Fdetails>\n\n&nbsp;\n\n## 安装 🛠️\n\n\ntorchtune **仅**在最新的稳定版 PyTorch（当前为 2.6.0）以及预览版 nightly 版本上进行了测试，并利用 torchvision 对多模态 LLM 进行微调，同时借助 torchao 实现最新的量化技术；您也应安装这些库。\n\n### 安装稳定版\n\n```bash\n\n# 安装稳定版 PyTorch、torchvision 和 torchao\npip install torch torchvision torchao\npip install torchtune\n```\n\n### 安装夜间版本\n\n```bash\n# 安装 PyTorch、torchvision 和 torchao 的夜间版本。\npip install --pre --upgrade torch torchvision torchao --index-url https:\u002F\u002Fdownload.pytorch.org\u002Fwhl\u002Fnightly\u002Fcu126 # 可选的完整选项包括 cpu\u002Fcu118\u002Fcu124\u002Fcu126\u002Fxpu\u002Frocm6.2\u002Frocm6.3\u002Frocm6.4\npip install --pre --upgrade torchtune --extra-index-url https:\u002F\u002Fdownload.pytorch.org\u002Fwhl\u002Fnightly\u002Fcpu\n```\n\n您还可以查看我们的[安装文档](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Finstall.html)，以获取更多信息，包括从源代码安装 torchtune。\n\n&nbsp;\n\n为了确认软件包是否正确安装，您可以运行以下命令：\n\n```bash\ntune --help\n```\n\n应该会看到如下输出：\n\n```bash\nusage: tune [-h] {ls,cp,download,run,validate} ...\n\n欢迎使用 torchtune 命令行界面！\n\n选项：\n  -h, --help            显示此帮助信息并退出\n\n...\n```\n\n&nbsp;\n\n## 开始使用 🚀\n\n\n要开始使用 torchtune，请参阅我们的[首次微调教程](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Ffirst_finetune_tutorial.html)。我们的[端到端工作流教程](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fe2e_flow.html)将向您展示如何评估、量化以及运行 Llama 模型的推理。本节的其余部分将以 Llama3.1 为例，快速概述这些步骤。\n\n\n### 下载模型\n\n请按照官方 [`meta-llama`](https:\u002F\u002Fhuggingface.co\u002Fmeta-llama) 仓库中的说明操作，以确保您有权访问官方 Llama 模型权重。确认权限后，您可以运行以下命令将权重下载到本地机器上。这还将下载分词器模型和负责任使用指南。\n\n要下载 Llama3.1，您可以运行：\n\n```bash\ntune download meta-llama\u002FMeta-Llama-3.1-8B-Instruct \\\n--output-dir \u002Ftmp\u002FMeta-Llama-3.1-8B-Instruct \\\n--ignore-patterns \"original\u002Fconsolidated.00.pth\" \\\n--hf-token \u003CHF_TOKEN> \\\n```\n\n> [!提示]\n> 设置您的环境变量 `HF_TOKEN` 或在命令中传递 `--hf-token`，以验证您的访问权限。您可以在 https:\u002F\u002Fhuggingface.co\u002Fsettings\u002Ftokens 找到您的令牌。\n\n### 运行微调配方\n\n您可以在单个 GPU 上使用 LoRA 对 Llama3.1 8B 进行微调，命令如下：\n\n```bash\ntune run lora_finetune_single_device --config llama3_1\u002F8B_lora_single_device\n```\n\n对于分布式训练，tune CLI 集成了 [torchrun](https:\u002F\u002Fpytorch.org\u002Fdocs\u002Fstable\u002Felastic\u002Frun.html)。\n要在两个 GPU 上运行 Llama3.1 8B 的完整微调：\n\n```bash\ntune run --nproc_per_node 2 full_finetune_distributed --config llama3_1\u002F8B_full\n```\n\n> [!提示]\n> 请务必将任何 torchrun 命令放在配方规范**之前**。此后输入的任何 CLI 参数都会覆盖配置，而不会影响分布式训练。\n\n### 修改配置\n\n有两种方法可以修改配置：\n\n**配置覆盖**\n\n您可以直接从命令行覆盖配置字段：\n\n```bash\ntune run lora_finetune_single_device \\\n--config llama2\u002F7B_lora_single_device \\\nbatch_size=8 \\\nenable_activation_checkpointing=True \\\nmax_steps_per_epoch=128\n```\n\n**更新本地副本**\n\n您也可以将配置复制到本地目录，并直接修改内容：\n\n```bash\ntune cp llama3_1\u002F8B_full .\u002Fmy_custom_config.yaml\n已复制到 .\u002Fmy_custom_config.yaml\n```\n\n然后，您可以通过将 `tune run` 命令指向本地文件来运行自定义配方：\n\n```bash\ntune run full_finetune_distributed --config .\u002Fmy_custom_config.yaml\n```\n\n请查看 `tune --help` 以了解所有可能的 CLI 命令和选项。有关使用和更新配置的更多信息，请参阅我们的[配置深度解析](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fdeep_dives\u002Fconfigs.html)。\n\n### 自定义数据集\n\ntorchtune 支持在各种不同类型的数据集上进行微调，包括 [指令式](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fbasics\u002Finstruct_datasets.html)、[聊天式](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fbasics\u002Fchat_datasets.html)、[偏好数据集](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fbasics\u002Fpreference_datasets.html)等。如果您想了解更多关于如何将这些组件应用于您自己的自定义数据集进行微调的信息，请查看提供的链接以及我们的[API 文档](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fapi_ref_datasets.html)。\n\n### 自定义设备\ntorchtune 支持在多种设备上进行微调，包括 NVIDIA GPU、Intel XPU、AMD ROCm、Apple MPS 和 Ascend NPU。如果您有兴趣在自定义设备上运行配方，例如 Intel XPU，请按照以下步骤操作。\n\n步骤 1：参考 [Intel GPU 入门指南](https:\u002F\u002Fdocs.pytorch.org\u002Fdocs\u002Fstable\u002Fnotes\u002Fget_start_xpu.html) 来配置您的环境。\n\n步骤 2：通过 CLI 覆盖或配置更改来更新设备信息。\n您可以直接从命令行覆盖配置字段：\n\n```bash\ntune run lora_finetune_single_device --config llama3_1\u002F8B_lora_single_device device=xpu\n```\n或者编辑您本地的配置文件，将 `device: cuda` 替换为 `device: xpu`。\n\n&nbsp;\n\n## 社区 🌍\n\ntorchtune 致力于与生态系统中的流行工具和库集成。以下仅是一些示例，更多仍在开发中：\n\n- [Hugging Face Hub](https:\u002F\u002Fhuggingface.co\u002Fdocs\u002Fhub\u002Fen\u002Findex) 用于[访问模型权重](torchtune\u002F_cli\u002Fdownload.py)\n- [EleutherAI 的 LM Eval Harness](https:\u002F\u002Fgithub.com\u002FEleutherAI\u002Flm-evaluation-harness) 用于[评估](recipes\u002Feleuther_eval.py)训练好的模型\n- [Hugging Face Datasets](https:\u002F\u002Fhuggingface.co\u002Fdocs\u002Fdatasets\u002Fen\u002Findex) 用于[访问](torchtune\u002Fdatasets\u002F_instruct.py)训练和评估数据集\n- [PyTorch FSDP2](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtitan\u002Fblob\u002Fmain\u002Fdocs\u002Ffsdp.md) 用于分布式训练\n- [torchao](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Fao) 用于低精度数据类型和[训练后量化](recipes\u002Fquantize.py)技术\n- [Weights & Biases](https:\u002F\u002Fwandb.ai\u002Fsite) 用于[记录](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fdeep_dives\u002Fwandb_logging.html)指标和检查点，并跟踪训练进度\n- [Comet](https:\u002F\u002Fwww.comet.com\u002Fsite\u002F) 是另一个用于[记录](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fdeep_dives\u002Fcomet_logging.html)的选项\n- [ExecuTorch](https:\u002F\u002Fpytorch.org\u002Fexecutorch-overview) 用于[设备端推理](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Fexecutorch\u002Ftree\u002Fmain\u002Fexamples\u002Fmodels\u002Fllama2#optional-finetuning)使用微调后的模型\n- [bitsandbytes](https:\u002F\u002Fhuggingface.co\u002Fdocs\u002Fbitsandbytes\u002Fmain\u002Fen\u002Findex) 用于我们[单设备配方](recipes\u002Fconfigs\u002Fllama2\u002F7B_full_low_memory.yaml)中的低内存优化器\n- [PEFT](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Fpeft) 用于在 Hugging Face 生态系统中对 torchtune 模型进行持续微调或推理\n\n&nbsp;\n\n### 社区贡献\n\n我们非常珍视我们的社区以及各位优秀用户所做的贡献。我们将在此部分特别感谢其中一些贡献。如果您也想参与贡献，请参阅 [CONTRIBUTING](CONTRIBUTING.md) 指南。\n\n- [@SalmanMohammadi](https:\u002F\u002Fgithub.com\u002Fsalmanmohammadi)，为 torchtune 添加了使用 PPO 进行 [人类反馈强化学习 (RLHF)](recipes\u002Fppo_full_finetune_single_device.py) 微调的完整端到端流程。\n- [@fyabc](https:\u002F\u002Fgithub.com\u002Ffyabc)，为 torchtune 添加了 Qwen2 模型、分词器及相应流程的集成。\n- [@solitude-alive](https:\u002F\u002Fgithub.com\u002Fsolitude-alive)，为 torchtune 添加了 [Gemma 2B 模型](torchtune\u002Fmodels\u002Fgemma\u002F) 的支持，包括流程修改、模型数值验证以及流程正确性检查。\n- [@yechenzhi](https:\u002F\u002Fgithub.com\u002Fyechenzhi)，为 torchtune 添加了 [直接偏好优化 (DPO)](recipes\u002Flora_dpo_single_device.py) 的支持，包含流程、配置文件及正确性验证。\n- [@Optimox](https:\u002F\u002Fgithub.com\u002FOptimox)，为 torchtune 添加了所有 [Gemma2 变体](torchtune\u002Fmodels\u002Fgemma2) 的支持！\n\n\n&nbsp;\n\n## 致谢 🙏\n\n本仓库中的 Transformer 代码灵感来源于原始的 [Llama2 代码](https:\u002F\u002Fgithub.com\u002Fmeta-llama\u002Fllama\u002Fblob\u002Fmain\u002Fllama\u002Fmodel.py)。同时，我们也衷心感谢 EleutherAI、Hugging Face 和 Weights & Biases，感谢他们作为出色的合作伙伴，与我们一起完成了 torchtune 中的一些集成工作。此外，我们还想感谢生态系统中其他一些优秀的库和工具：\n\n- [gpt-fast](https:\u002F\u002Fgithub.com\u002Fmeta-pytorch\u002Fgpt-fast)，提供了高效的 LLM 推理技术，我们直接采用了这些现成的实现。\n- [llama recipes](https:\u002F\u002Fgithub.com\u002Fmeta-llama\u002Fllama-recipes)，为 Llama2 社区的发展奠定了基础。\n- [bitsandbytes](https:\u002F\u002Fgithub.com\u002FTimDettmers\u002Fbitsandbytes)，将多项内存与性能优化技术引入了 PyTorch 生态系统。\n- [@winglian](https:\u002F\u002Fgithub.com\u002Fwinglian\u002F) 和 [axolotl](https:\u002F\u002Fgithub.com\u002FOpenAccess-AI-Collective\u002Faxolotl)，在 torchtune 的设计与功能集方面提供了早期反馈与头脑风暴。\n- [lit-gpt](https:\u002F\u002Fgithub.com\u002FLightning-AI\u002Flitgpt)，推动了 LLM 微调社区的进步。\n- [HF TRL](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Ftrl)，使奖励建模对 PyTorch 社区更加易用。\n\n&nbsp;\n\n## 引用 torchtune 📝\n\n如果您觉得 torchtune 库很有用，请在您的工作中按以下方式引用它。\n\n```bibtex\n@software{torchtune,\n  title = {torchtune: PyTorch 的微调库},\n  author = {torchtune 维护者及贡献者},\n  url = {https\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune},\n  license = {BSD-3-Clause},\n  month = apr,\n  year = {2024}\n}\n```\n\n&nbsp;\n\n## 许可证\n\ntorchtune 采用 [BSD 3 许可证](.\u002FLICENSE) 发布。然而，您可能还需遵守其他法律义务，例如关于第三方模型的服务条款等，以规范您对相关内容的使用。","# torchtune 快速上手指南\n\ntorchtune 是 PyTorch 官方推出的大语言模型（LLM）微调库，专为简化模型作者、后训练（Post-training）及实验流程而设计。它支持 SFT、LoRA\u002FQLoRA、DPO、知识蒸馏等多种前沿训练方法，并针对 Llama、Qwen、Gemma、Mistral 等主流模型提供了开箱即用的配置。\n\n## 环境准备\n\n在开始之前，请确保您的开发环境满足以下要求：\n\n*   **操作系统**: Linux (推荐 Ubuntu 20.04+) 或 macOS。Windows 支持有限，建议使用 WSL2。\n*   **Python**: 版本需为 3.9 或更高。\n*   **PyTorch**: 需安装与您的 CUDA 版本匹配的最新版 PyTorch (建议 2.4+)。\n*   **GPU**: 推荐使用 NVIDIA GPU。对于大模型全量微调或多卡训练，显存需求较高；若资源有限，可使用 LoRA 或 QLoRA 方案。\n*   **依赖管理**: 推荐使用 `conda` 或 `venv` 创建独立的虚拟环境。\n\n**前置检查：**\n确保已安装 `git` 和 `pip`。若使用国内网络，建议配置 pip 国内镜像源以加速下载：\n```bash\npip config set global.index-url https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple\n```\n\n## 安装步骤\n\n### 1. 创建并激活虚拟环境\n```bash\nconda create -n torchtune python=3.10 -y\nconda activate torchtune\n```\n\n### 2. 安装 PyTorch\n请访问 [PyTorch 官网](https:\u002F\u002Fpytorch.org\u002Fget-started\u002Flocally\u002F) 获取适合您硬件的安装命令。以下为 CUDA 12.1 的示例：\n```bash\npip3 install torch torchvision torchaudio --index-url https:\u002F\u002Fdownload.pytorch.org\u002Fwhl\u002Fcu121\n```\n*注：国内用户可使用清华源加速 PyTorch 安装（如果可用），或直接使用官方源。*\n\n### 3. 安装 torchtune\n通过 pip 直接安装最新稳定版：\n```bash\npip install torchtune\n```\n\n或者，若您希望体验最新功能（如最新的 Qwen3 或 Llama4 支持），可从源码安装：\n```bash\ngit clone https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune.git\ncd torchtune\npip install -e .\n```\n\n### 4. 验证安装\n运行以下命令查看可用食谱（recipes）列表，确认安装成功：\n```bash\ntune ls\n```\n\n## 基本使用\n\ntorchtune 的核心工作流是通过 `tune run` 命令加载预设的 YAML 配置文件来启动训练。以下是使用 **LoRA** 方法微调 **Llama 3.2 3B** 模型的最简示例。\n\n### 第一步：下载模型权重\ntorchtune 支持直接从 Hugging Face Hub 下载模型。首次运行时会自动下载，也可手动预下载。\n*(注：若访问 Hugging Face 受限，请配置 HF_ENDPOINT 或使用国内镜像站如 ModelScope)*\n\n```bash\n# 设置环境变量（可选，用于指定模型缓存目录）\nexport HF_HOME=~\u002F.cache\u002Fhuggingface\n\n# 运行命令时工具会自动拉取模型，无需单独执行下载脚本\n```\n\n### 第二步：运行微调任务\n使用内置配置文件启动单卡 LoRA 微调。以下命令将自动处理数据加载、模型构建和训练循环。\n\n```bash\ntune run lora_finetune_single_device --config llama3_2\u002F3B_lora_single_device\n```\n\n**命令解析：**\n*   `lora_finetune_single_device`: 指定使用单卡 LoRA 微调食谱。\n*   `--config llama3_2\u002F3B_lora_single_device`: 指定针对 Llama 3.2 3B 模型的预设配置。\n\n### 第三步：自定义配置（可选）\n您可以复制默认配置文件进行修改，以适应自己的数据集或超参数：\n\n```bash\n# 1. 查看配置内容\ntune cp llama3_2\u002F3B_lora_single_device .\u002Fmy_custom_config.yaml\n\n# 2. 编辑 my_custom_config.yaml (修改 dataset, batch_size, epochs 等)\n# vim my_custom_config.yaml \n\n# 3. 使用自定义配置运行\ntune run lora_finetune_single_device --config .\u002Fmy_custom_config.yaml\n```\n\n### 其他常用场景示例\n\n*   **全量微调 (Full Finetune)**:\n    ```bash\n    tune run full_finetune_single_device --config llama3_2\u002F3B_full_single_device\n    ```\n*   **多卡分布式训练 (DDP)**:\n    ```bash\n    tune run --nproc_per_node 4 full_finetune_distributed --config llama3_2\u002F3B_full_distributed\n    ```\n*   **知识蒸馏 (Knowledge Distillation)**:\n    ```bash\n    tune run knowledge_distillation_distributed --config qwen2\u002F1.5B_to_0.5B_KD_lora_distributed\n    ```\n*   **DPO (人类反馈强化学习)**:\n    ```bash\n    tune run lora_dpo_single_device --config llama3_1\u002F8B_dpo_single_device\n    ```\n\n训练完成后，生成的适配器权重（adapter weights）或完整模型权重将保存在配置文件中指定的输出目录下，可直接用于推理或进一步评估。","某初创教育科技公司希望基于最新的 Llama 3.2 3B 模型，快速定制一个专为中学生设计的理科辅导助手，需在有限的单卡资源上完成高效微调。\n\n### 没有 torchtune 时\n- **环境搭建繁琐**：团队需手动整合 Hugging Face Transformers、PEFT 和 Accelerate 等多个库，版本冲突频发，配置 LoRA 微调环境耗时数天。\n- **显存优化困难**：在单张消费级显卡上运行全量或大参数微调时，常因显存溢出（OOM）而被迫降低批次大小，导致训练极慢甚至无法启动。\n- **算法复现门槛高**：想要尝试知识蒸馏或 DPO 等进阶对齐算法，需从零编写复杂的损失函数和数据加载逻辑，研发周期被大幅拉长。\n- **配置管理混乱**：超参数分散在各个脚本文件中，修改实验配置如同“改代码”，难以追踪不同实验间的差异，复现性差。\n\n### 使用 torchtune 后\n- **开箱即用**：直接调用 torchtune 内置的 Llama 3.2 原生实现和预置 YAML 配置，一行命令即可启动 LoRA 微调，环境准备时间从几天缩短至几小时。\n- **极致显存效率**：借助 torchtune 集成的激活值卸载（Activation Offloading）和 QLoRA 技术，成功在单卡上流畅训练 3B 模型，显存占用降低 40% 以上。\n- **进阶算法轻松上手**：通过切换配置文件，无缝启用知识蒸馏或 DPO 食谱，无需重写底层代码，快速验证了“教师模型”指导下的性能提升。\n- **标准化实验管理**：所有训练参数集中在于清晰的 YAML 文件中，团队成员可轻松复制、修改并对比不同实验配置，协作效率显著提升。\n\ntorchtune 将原本复杂的后训练流程转化为标准化的配置驱动模式，让开发者能专注于算法策略而非工程基建，极大加速了垂直领域大模型的落地进程。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fmeta-pytorch_torchtune_8f21c832.png","meta-pytorch","Meta PyTorch","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002Fmeta-pytorch_1dfd3f76.jpg","",null,"https:\u002F\u002Fpytorch.org","https:\u002F\u002Fgithub.com\u002Fmeta-pytorch",[84,88],{"name":85,"color":86,"percentage":87},"Python","#3572A5",99.8,{"name":89,"color":90,"percentage":91},"Shell","#89e051",0.2,5720,714,"2026-04-04T10:53:31","BSD-3-Clause","未说明","需要 NVIDIA GPU（基于测试数据）。显存需求取决于模型大小和微调方法：单卡微调 Llama 3.1 8B (QLoRA) 需约 7.4GB，全量微调需 18.9GB+；多卡微调 Llama 3.1 70B\u002F405B 需 A100 等高性能显卡，单卡显存占用 13.9GB-44.8GB。支持 CPU Offload 以降低显存需求。","未说明（建议根据模型大小配置充足系统内存以支持数据加载和 CPU Offload）",{"notes":100,"python":96,"dependencies":101},"该工具专注于 PyTorch 原生实现，利用最新 PyTorch API 进行内存优化（如激活卸载、分块交叉熵、编译器优化等）。支持多种微调策略（SFT, LoRA, QLoRA, DPO, PPO, GRPO, QAT）及多节点训练。具体硬件需求高度依赖于所选模型参数量（从 0.5B 到 405B+）及微调配置，官方提供了针对不同显卡（如 RTX 4090, A6000, A100）的显存占用和吞吐量基准测试数据供参考。",[102,67],"torch",[26,13],"2026-03-27T02:49:30.150509","2026-04-06T06:53:11.518636",[107,112,117,121,126,130],{"id":108,"question_zh":109,"answer_zh":110,"source_url":111},15275,"如何保存微调后的模型以便使用 Hugging Face 的 `from_pretrained()` 加载？","Torchtune 现已支持将微调后的模型保存为 Hugging Face 格式。对于 LoRA 微调，运行命令时指定输出目录即可：\n`tune run lora_finetune_single_device --config llama2\u002F7B_lora_single_device checkpointer.output_dir=\u002Fmy\u002Foutput\u002Fdir`\n\n加载时使用 PEFT 库：\n```python\nfrom transformers import AutoModelForCausalLM\nfrom peft import PeftModel\n\nmodel_id = \"meta-llama\u002FLlama-2-7b-hf\" # 基础模型 ID\ncheckpoint_dir = \"\u002Fmy\u002Foutput\u002Fdir\" # 微调输出目录\n\nmodel = AutoModelForCausalLM.from_pretrained(model_id)\npeft_model = PeftModel.from_pretrained(model, checkpoint_dir)\n```\n完整模型权重的保存也可参考官方端到端教程中关于 'Use with Hugging Face from_pretrained' 的章节。","https:\u002F\u002Fgithub.com\u002Fmeta-pytorch\u002Ftorchtune\u002Fissues\u002F832",{"id":113,"question_zh":114,"answer_zh":115,"source_url":116},15276,"如何在单 GPU 上解决显存不足（OOM）的问题？","可以通过以下配置优化来降低显存占用：\n1. 启用编译：设置 `compile=True`。\n2. 调整批次大小与梯度累积：减小 `batch_size` 并增加 `gradient_accumulation_steps`，建议两者的乘积保持在 8 到 64 之间。如果损失波动大，可适当增大该乘积以增加稳定性。\n3. 使用 QLoRA 替代 LoRA：可以使用更小的 rank（设置 `alpha = 2 * rank`），并仅训练部分层（如仅 q, v 投影），但通常训练 MLP 层效果更好。\n4. 启用激活重计算：设置 `enable_activation_checkpointing = true`。\n5. 启用激活卸载：设置 `activation_offloading = True`。\n6. 使用夜间构建版本：安装最新的 PyTorch 和 Torchtune nightlies 版本。","https:\u002F\u002Fgithub.com\u002Fmeta-pytorch\u002Ftorchtune\u002Fissues\u002F1694",{"id":118,"question_zh":119,"answer_zh":120,"source_url":116},15277,"如何确定合适的训练时长或 epoch 数量？","目前 Torchtune 暂不支持自动早停（early stopping）。建议通过观察验证集损失（validation loss）来判断：持续训练直到验证损失趋于平稳或开始过拟合。训练集损失（train loss）不是最佳判断指标。\n\n推荐测试方法：分别训练 1 个 epoch 和 3 个 epoch，然后在两种情况下运行评估，对比结果差异以决定最佳训练时长。",{"id":122,"question_zh":123,"answer_zh":124,"source_url":125},15278,"是否可以将模型和损失函数一起编译（compile）以提升性能？","是的，该功能已实现。现在当设置 `compile=True` 时，不仅模型会被编译，损失函数（loss_fn）也会一同被编译。基准测试显示，这能带来显著的性能提升（例如在 RTX 4080 上，吞吐量可从 ~1100 tok\u002Fs 提升至 ~1450 tok\u002Fs，提升约 30%）。直接在配置文件中设置 `compile: true` 即可生效。","https:\u002F\u002Fgithub.com\u002Fmeta-pytorch\u002Ftorchtune\u002Fissues\u002F1228",{"id":127,"question_zh":128,"answer_zh":129,"source_url":116},15279,"配置文件中的 `shuffle: true` 是什么意思？如何调整输入输出序列长度？","`shuffle: true` 表示数据集会在采样前进行随机打乱，然后按照设定的 `batch_size` 进行批次采样。\n\n关于序列长度：目前在 YAML 配置文件中主要通过 `max_seq_len` 等参数控制最大序列长度。如果在单卡（如 RTX 4090）上设置过长（如 2048）导致显存溢出，建议降低该值，并结合上述显存优化技巧（如梯度累积、QLoRA 等）进行平衡。具体的输入\u002F输出长度调整通常依赖于数据预处理阶段的截断或填充策略，需在数据集组件配置中进一步确认。",{"id":131,"question_zh":132,"answer_zh":133,"source_url":125},15280,"如何使用 FSDP2 进行分布式微调？","FSDP2 的支持已合并到 `full_finetune_distributed` 配方（recipe）中。现在运行该配方即默认使用 FSDP2 而非 FSDP1。需要注意的是，在使用 FSDP2 时，`torch.compile` 可能会遇到一些兼容性问题（参考 Issue #1152），建议在启用编译功能前进行充分测试。",[135,140,145,150,155,160,165,170,175,180],{"id":136,"version":137,"summary_zh":138,"released_at":139},89945,"v0.6.1","# v0.6.1\n\n针对 #2561 的补丁。\n\n**完整更新日志**: https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Fcompare\u002Fv0.6.0...v0.6.1","2025-04-07T19:01:38",{"id":141,"version":142,"summary_zh":143,"released_at":144},89946,"v0.6.0","\r\n## 亮点\r\n\r\n我们发布了 torchtune v0.6.0，带来了令人兴奋的新功能和改进的分布式训练支持！此版本包括 TensorParallel (TP) + FSDP 训练、TP 推理、**多节点训练**以及完整的 DPO 分布式配方。此外，我们还新增了 Phi4 模型支持、MLFlow 日志记录，并进一步提升了对 NPU 的支持。\n\n### Tensor Parallel 训练 + 推理 (#2245) (#2330)\r\n\r\n张量并行是一种用于分布式训练的模型并行技术。与 FSDP 结合使用时，TP 能够在多 GPU 上更高效地训练大型模型，相比单独使用 FSDP 效果更好。FSDP 是将数据集拆分到不同 GPU 上，而 TP 则是将每个模型层拆分到不同的 GPU 上，从而在更大规模下显著加快模型层的计算速度。除了训练之外，我们还实现了 TP 推理功能，这在模型无法容纳于单个 GPU 上进行文本生成或强化学习时尤为重要。如需了解如何定义 TP 模型，请参阅[此处](https:\u002F\u002Fpytorch.org\u002Fdocs\u002Fstable\u002Fdistributed.tensor.parallel.html)。\n\n### 多节点训练支持 (#2301)\r\n\r\n现在支持多节点微调，使您能够更快地训练更大的模型。借助 SLURM，您可以在多个节点上启动 `tune run` 命令，以与在单机上相同的方式进行训练。我们在此提供一个 SLURM 配方示例以及入门教程[这里](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fmultinode.html)。\r\n\r\n\r\n### 完整分布式 DPO 配方 (#2275)\r\n\r\n我们此前已支持 DPO，但现在新增了使用所有分布式特性（包括上述内容）来训练 DPO 的能力。这进一步扩展了我们对日益增多的 70B+ 参数模型所适用的配方覆盖范围。要使用完整分布式 DPO 微调 Llama 3.1 8B 模型，您可以运行以下命令：\n\n```\r\n# 下载 Llama 3.1 8B\ntune download meta-llama\u002FMeta-Llama-3.1-8B-Instruct --ignore-patterns \"original\u002Fconsolidated.00.pth\"\n\n# 在四台设备上微调\ntune run --nnodes 1 --nproc_per_node 4 full_dpo_distributed --config llama3_1\u002F8B_full_dpo\n\n```\r\n\r\n特别感谢 @sam-pi 添加了这一配方。\n\n### Phi 4 模型 (#1835)\r\n\r\n我们现在支持 Phi 4 模型！目前包括 14B 参数的模型，并提供了在单台或多台设备上进行全量微调、LoRA 微调以及 QLoRA 微调的配方。例如，您可以在单个 GPU 上对 Phi 4 14B 模型进行全量微调，只需运行以下命令：\n\n```\n# 下载 Phi 4 14B\ntune download microsoft\u002Fphi-4\n\n# 安装 bitsandbytes\npip install bitsandbytes\n\n# 在单个 GPU 上微调\ntune run full_finetune_single_device --config phi4\u002F14B_full_low_memory\n```\r\n\r\n非常感谢 @krammnic 贡献了这些模型！\n\n### NPU 支持改进 (#2234)\r\n\r\n我们持续提升对 Ascend NPU 设备的支持。此版本包含多项修复和优化，旨在为您提供更好的 NPU 后端性能。感谢 @Nicorgi 的帮助！\n\n## 变更内容\n* 小型 README 和配置更新，由 @ebsmothers 在 https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Fpull\u002F2157 中完成\n* 使用 `FormattedC","2025-03-24T21:52:40",{"id":146,"version":147,"summary_zh":148,"released_at":149},89947,"v0.5.0","## 亮点\n\n我们发布了 torchtune v0.5.0，带来了许多令人兴奋的新功能！其中包括与 Kaggle 的集成、QAT + LoRA 训练配方、对 Hugging Face 和 vLLM 的更好支持、Gemma2 模型、通过早期退出实现 LayerSkip 微调的配方，以及对 NPU 设备的支持。\n\n\n### Kaggle 集成 (#2002)\n\ntorchtune 很荣幸地宣布与 Kaggle 的集成！现在，您可以在 Kaggle 笔记本中使用 torchtune，并结合 Kaggle 模型中心的功能，微调您喜爱的任何模型。您可以从 Kaggle 中心下载模型，使用任意 torchtune 配方在自己的数据集上进行微调，然后选择最佳模型并将最优检查点上传到 Kaggle 中心，与社区分享。请查看我们的示例 Kaggle 笔记本 [这里](https:\u002F\u002Fwww.kaggle.com\u002Fcode\u002Ffelipemello\u002Ftorchtune-in-kaggle)，开始您的旅程吧！\n\n### QAT + LoRA 训练配方 (#1931)\n\n如果您看过 [Llama 3.2 量化模型](https:\u002F\u002Fai.meta.com\u002Fblog\u002Fmeta-llama-quantized-lightweight-models\u002F)，可能知道它们是使用带有 LoRA 适配器的量化感知训练方法训练出来的。当您需要为设备端推理进行量化时，这是一种保持良好模型性能的有效方式。现在，您可以通过我们的 QAT + LoRA 配方，在 torchtune 中训练属于自己的量化友好型 LoRA 模型了！\n\n要使用 QAT + LoRA 微调 Llama 3.2 3B 模型，您可以运行以下命令：\n\n```bash\n# 下载 Llama 3.2 3B 模型\ntune download meta-llama\u002FLlama-3.2-3B-Instruct --ignore-patterns \"original\u002Fconsolidated.00.pth\"\n\n# 在两台设备上进行分布式微调\ntune run --nproc_per_node 2 qat_lora_finetune_distributed --config llama3_2\u002F3B_qat_lora\n```\n\n### 改进的 Hugging Face 和 vLLM 集成 (#2074)\n\n我们认真听取了大家的反馈，很高兴地宣布，现在将 torchtune 模型加载到 Hugging Face 或 vLLM 中比以往更加简单！只需如下操作即可：\n\n```python\nfrom transformers import AutoModelForCausalLM\n\ntrained_model_path = \"\u002Fpath\u002Fto\u002Fmy\u002Ftorchtune\u002Fcheckpoint\"\n\nmodel = AutoModelForCausalLM.from_pretrained(\n    pretrained_model_name_or_path=trained_model_path,\n)\n```\n\n更多完整示例请参阅我们的文档：[Hugging Face](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fe2e_flow.html#use-with-hugging-face-from-pretrained)、[vLLM](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Ftutorials\u002Fe2e_flow.html#use-with-vllm)\n\n### Gemma 2 模型 (#1835)\n\n我们现在支持 Gemma 2 系列模型！包括 2B、9B 和 27B 三种规模，并提供了在单机或多机环境下进行全参数、LoRA 和 QLoRA 微调的配方。例如，您可以通过以下命令使用 QLoRA 微调 Gemma 2 27B 模型：\n\n```bash\n# 下载 Gemma 2 27B 模型\ntune download google\u002Fgemma-2-27b --ignore-patterns \"gemma-2-27b.gguf\"\n\n# 在单个 GPU 上进行微调\ntune run lora_finetune_single_device --config gemma2\u002F27B_qlora_single_device\n```\n\n在此特别感谢 @Optimox 贡献了这些模型！\n\n### 早期退出训练配方 (#1076)\n\n[LayerSkip](https:\u002F\u002Farxiv.org\u002Fabs\u002F2404.16710) 是一种端到端加速大语言模型推理的解决方案。通过将层丢弃与适当的…","2024-12-20T20:50:31",{"id":151,"version":152,"summary_zh":153,"released_at":154},89948,"v0.4.0","## 亮点\n\n今天，我们发布了 torchtune v0.4.0 版本，带来了几项令人兴奋的新功能！其中一些值得关注的包括：对激活卸载的全面支持、Llama3.2V 90B 和 QLoRA 变体的配方、新的文档以及 Qwen2.5 模型！\n\n### 活动性卸载 (#1443, #1645, #1847)\n\n激活卸载是一种节省内存的技术，它会异步地将当前未运行的检查点激活数据移至 CPU。在 GPU 需要这些激活数据进行微批次反向传播之前，该功能会提前从 CPU 将卸载的激活数据预取回 GPU。启用此功能非常简单，只需在配置中设置以下选项即可：\n\n```yaml\nenable_activation_checkpointing: True\nenable_activation_offloading: True\n```\n\n在 [Llama3 8B 的实验](https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Fpull\u002F1443) 中，激活卸载大约减少了 24% 的内存占用，同时性能仅下降不到 1%。\n### Llama3.2V 90B 与 QLoRA (#1880, #1726)\n\n我们新增了 Llama3.2V 90B 版本的模型构建器和配置文件。该版本在常见基准测试中[优于 11B 版本](https:\u002F\u002Fhuggingface.co\u002Fmeta-llama\u002FLlama-3.2-90B-Vision#base-pretrained-models)。由于模型规模较大，我们还增加了使用 QLoRA 和 FSDP2 运行该模型的功能。\n\n```bash\n# 首先下载模型\ntune download meta-llama\u002FLlama-3.2-90B-Vision-Instruct --ignore-patterns \"original\u002Fconsolidated*\"\n# 使用例如 4 张 GPU 运行\ntune run --nproc_per_node 4 lora_finetune_distributed --config llama3_2_vision\u002F90B_qlora\n```\n\n### Qwen2.5 模型家族正式上线 (#1863)\n\n我们为 Qwen 家族的前沿模型——[Qwen2.5](https:\u002F\u002Fqwenlm.github.io\u002Fblog\u002Fqwen2.5\u002F)——添加了模型构建器！用他们自己的话说：“与 Qwen2 相比，Qwen2.5 获得了显著更多的知识（MMLU：85+），并在编程（HumanEval 85+）和数学（MATH 80+）方面的能力也有了极大提升。”\n\n您可以轻松开始使用这些模型：\n\n```bash\ntune download Qwen\u002FQwen2.5-1.5B-Instruct --ignore-patterns None\ntune run lora_finetune_single_device --config qwen2_5\u002F1.5B_lora_single_device\n```\n\n### 关于使用自定义配方、配置和组件的新文档 (#1910)\n\n我们认真听取了您的反馈，并编写了一篇简单的页面，介绍如何自定义配置、配方和单个组件！请在此处查看：[这里](https:\u002F\u002Fpytorch.org\u002Ftorchtune\u002Fmain\u002Fbasics\u002Fcustom_components.html)\n\n\n## 变更内容\n* 修复 PackedDataset 在 seq_len > 2 * max_seq_len 设置下的 bug。由 @mirceamironenco 在 https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Fpull\u002F1697 中完成。\n* 将版本升级至 0.3.1。由 @joecummings 在 https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Fpull\u002F1720 中完成。\n* 为分布式运行添加错误传播机制。由 @mirceamironenco 在 https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Fpull\u002F1719 中完成。\n* 更新 Llama 3.2 权重转换时的融合层计数逻辑。由 @ebsmothers 在 https:\u002F\u002Fgithub.com\u002Fpytorch\u002Ftorchtune\u002Fpull\u002F1722 中完成。\n* 可调整大小的图像位置嵌入。由 @felipemello 完成。","2024-11-14T15:37:13",{"id":156,"version":157,"summary_zh":158,"released_at":159},89949,"v0.3.1","## 概述\n在 [宣布](https:\u002F\u002Fai.meta.com\u002Fblog\u002Fllama-3-2-connect-2024-vision-edge-mobile-devices\u002F) Llama 3.2 之后，我们已为其添加了全面支持，这包括对 Llama3.2-1B、Llama3.2-3B 的基础和指令文本模型，以及 Llama3.2-11B-Vision 的基础和指令文本模型进行全量微调和 LoRA 微调。**这意味着我们现在支持 VLM 的完整端到端开发——微调、推理和评估！** 此外，在短短几周内，我们还增加了许多新功能：\n\n* Llama 3.2 1B\u002F3B\u002F11B Vision 配置，用于全量\u002FLoRA 微调\n* 更新的配方以支持 VLM\n* 通过 EleutherAI 进行多模态评估\n* 对 VLM 的 torch.compile 支持\n* 重新设计的生成工具，支持多模态并实现纯文本的批处理推理\n* 新的知识蒸馏配方及 Llama3.2 和 Qwen2 的配置\n* 在 8 张 A100 上进行 Llama 3.1 405B 的 QLoRA 微调\n* MPS 支持（测试版）——现在你可以在 Mac 上使用 torchtune 了！\n\n## 新特性\n### 模型\n* 使用 QLoRA 微调 Llama 3.1 405B (#1232)\n* Llama 3.2 (#1679, #1688, #1661)\n\n### 多模态\n* 更新多模态支持的配方 (#1548, #1628)\n* 通过 EleutherAI 进行多模态评估 (#1669, #1660)\n* 多模态编译支持 (#1670)\n* 可导出的多模态模型 (#1541)\n\n### 生成\n* 重新设计的生成配方，支持多模态 (#1559, #1563, #1674, #1686)\n* 纯文本模型的批处理推理 (#1424, #1449, #1603, #1622)\n\n### 知识蒸馏\n* 添加单机 KD 配方及 Llama 3.2、Qwen2 的配置 (#1539, #1690)\n\n### 内存与性能\n* 编译 FFT FSDP (#1573)\n* 为提高效率，在更早阶段对 k 应用 RoPE (#1558)\n* 在 (q)lora 单机微调中实现流式卸载 (#1443)\n\n### 量化\n* 更新量化以使用张量子类 (#1403)\n* 添加针对 tinygemm 内核的 int4 权重专用 QAT 流程 (#1570)\n\n### RLHF\n* 添加通用偏好数据集构建器 (#1623)\n\n### 其他\n* 向数据加载器添加 drop_last 参数 (#1654)\n* 为 qlora 添加 low_cpu_ram 配置 (#1580)\n* MPS 支持 (#1706)\n\n## 文档\n* 内存优化文档中的小错误修正 (#1585)\n* 分词器和提示模板文档 (#1567)\n* 将 IPOLoss 文档转换为 LaTeX 格式 (#1589)\n* 模块文档更新 (#1588)\n* 更多文档小错误修正 (#1611)\n* 更新文档 (#1602)\n* 更新 llama3 聊天教程 (#1608)\n* 指令和聊天数据集文档 (#1571)\n* 偏好数据集文档 (#1636)\n* 消息及消息变换文档 (#1574)\n* Readme 更新 (#1664)\n* 模型变换文档 (#1665)\n* 多模态数据集构建器及文档 (#1667)\n* 数据集概述文档 (#1668)\n* 更新 README.md (#1676)\n* Llama 3.2 的 Readme 更新 (#1680)\n* 将 3.2 模型加入 README (#1683)\n* 知识蒸馏教程 (#1698)\n* 文本补全数据集文档 (#1696)\n\n## 生活质量改进\n* 将调试日志级别设置为 debug，而非 info (#1560)\n* 从 Qwen 中移除 TiedEmbeddingTransformerDecoder (#1547)\n* 让 Gemma 使用常规的 TransformerDecoder (#1553)\n* 让 llama 3_1 只实例化一次位置嵌入 (#1554)\n* 运行单元测试 a","2024-10-02T21:26:16",{"id":161,"version":162,"summary_zh":163,"released_at":164},89950,"v0.3.0","## 概述\n我们有一段时间没有发布新版本了，因此这次更新内容非常丰富。亮点包括：用于全量微调和 LoRA（\u002FQLoRA）的 FSDP2 示例脚本、对 DoRA 微调的支持、用于 RLHF 的 PPO 示例脚本、多种规模的 Qwen2 模型，以及大量内存和性能方面的改进（建议搭配 PyTorch Nightly 使用我们的示例脚本！尝试使用 flex attention 进行样本打包！）；此外还集成了 Comet ML。为了获得完整的性能与内存优化效果，我们推荐使用 PyTorch Nightly 版本进行安装。\n\n## 新特性\n以下是 0.3.0 版本中的一些新功能亮点。\n\n### 示例脚本\n- 全量微调 FSDP2 示例脚本 (#1287)\n- LoRA FSDP2 示例脚本，训练速度比 FSDP1 更快 (#1517)\n- 基于 PPO 的 RLHF 示例脚本 (#1005)\n- DoRA 示例脚本 (#1115)\n- SimPO 示例脚本 (#1223)\n\n### 模型\n- Qwen2 0.5B、1.5B 和 7B 模型 (#1143、#1247)\n- Flamingo 模型组件 (#1357)\n- CLIP 编码器与视觉 Transformer (#1127)\n\n### 性能、内存与量化\n- 层级编译：编译时间缩短 90%，训练时间加快 75% (#1419)\n- 使用 flex attention 进行样本打包：配合编译时相比未打包情况，训练速度提升 80% (#1193)\n- 分块交叉熵损失以降低峰值内存占用 (#1390)\n- KV 缓存可选化 (#1207)\n- 仅保存适配器检查点的选项 (#1220)\n- 在反向传播前删除 logits，节省约 4 GB 内存 (#1235)\n- 在未应用 LoRA 的情况下将线性层量化为 NF4 格式 (#1119)\n- 对模型和损失函数进行编译 (#1296、#1319)\n- 加速 QLoRA 初始化过程 (#1294)\n- 将 LoRA Dropout 设置为 0.0 以节省内存 (#1492)\n\n### 数据\u002F数据集\n- 多模态数据集：The Cauldron 和 LLaVA-Instruct-150K (#1158)\n- 多模态 collator (#1156)\n- 重新设计分词器，更好地支持特定于模型的功能 (#1082)\n- 创建结合指令和对话的通用 SFT 数据集 (#1234)\n- 分词器中支持交错图像 (#1138)\n- CLIP 编码器的图像变换 (#1084)\n- 视觉交叉注意力掩码变换 (#1141)\n- 支持消息中包含图像 (#1504)\n\n### 其他\n- 深度融合模块 (#1338)\n- 集成 CometLogger (#1221)\n- 在全量微调示例脚本中添加性能分析器 (#1288)\n- 通过性能分析器支持内存可视化工具 (#1382、#1384)\n- 添加 RSO 损失 (#1197)\n- 支持非增量解码方式 (#973)\n- 将 utils 目录迁移到 training 模块下 (#1432、#1519 等)\n- 在 CPU 上添加 bf16 数据类型支持 (#1218)\n- 添加梯度范数日志记录功能 (#1451)\n\n## 文档更新\n- QAT 教程 (#1105)\n- 示例脚本文档页面及内存优化教程 (#1230)\n- 在模型 API 文档中添加下载命令 (#1167)\n- 更新 utils API 文档 (#1170)\n\n## Bug 修复\n- 防止生成过程中显示填充 ID 和特殊标记 (#1211)\n- 修复 Gemma 检查点逻辑回滚导致头部权重丢失的问题 (#1168)\n- 修复 PyTorch 2.4 下的编译问题 (#1512)\n- 修正 Llama 3.1 RoPE 初始化在编译模式下的问题 (#1544)\n- 修复 FSDP2 结合 CPU offload 时的检查点加载问题 (#1495)\n- 为 Llama 3.1 的部分层补充电量化设置 (#1485)\n- 修复 Eleuther 评估测试中准确率数值解析错误 (#1135)\n- 允许在消息中添加自定义系统提示","2024-09-18T01:57:51",{"id":166,"version":167,"summary_zh":168,"released_at":169},89951,"v0.2.1","### 概述\n\n此补丁增加了对使用 torchtune 对 [Llama3.1](https:\u002F\u002Fllama.meta.com\u002Fdocs\u002Fmodel-cards-and-prompt-formats\u002Fllama3_1\u002F) 进行微调的支持，并对库进行了多项改进。\n\n### 新功能与改进\n\n#### 模型\n* 添加了对 Llama3.1 的支持 (#1208)\n\n#### 模块\n* 重构分词器，以提高分词器组件的可扩展性 (#1082)\n","2024-07-25T19:51:08",{"id":171,"version":172,"summary_zh":173,"released_at":174},89952,"v0.2.0","## 概述\n我们有一段时间没有发布新版本了，而 torchtune 库这次带来了许多令人兴奋的新功能，包括分布式 QLoRA 支持、新模型、样本打包等！请查看 #new-contributors，了解仓库中新增贡献者的完整列表。\n\n祝大家使用新版本愉快，调试顺利！\n\n## 新特性\n以下是 v0.2.0 版本中的一些亮点：\n\n### 配方\n- 我们新增了对 FSDP2 的 QLoRA 支持！这意味着用户现在可以在多张 GPU 上运行 700 亿参数以上的模型。我们提供了 Llama2 70 亿和 70 亿参数规模的示例配置。注意：目前需要安装 PyTorch 夜间版才能使用 FSDP2 相关方法。（#909）\n- 同时，借助 FSDP2，与使用 LoRA 的 FSDP1 相比，每秒处理的 token 数提升了 12%，模型初始化速度提升了 3.2 倍。（#855）\n- 我们还为 Meta-Llama3 的其他变体配方增加了支持，包括：\n  - 700 亿参数的 LoRA 微调（#802）\n  - 700 亿参数的全量微调（#993）\n  - 80 亿参数的内存高效全量微调，相比旧版本可节省 46% 的峰值内存用量（#990）\n- 我们引入了一种量化感知训练（QAT）配方。如果计划在训练后对模型进行量化，采用 QAT 训练能够显著提升模型质量。（#980）\n- torchtune 对评估配方进行了多项更新，包括：\n  - 批量推理以加快评估速度（#947）\n  - 支持 EleutherAI Eval Harness 中的自由生成任务（#975）\n  - 支持自定义评估配置文件（#1055）\n\n### 模型\n- 来自微软的 Phi-3 Mini-4K-Instruct（#876）\n- 来自 Google 的 Gemma 7B（#971）\n- 来自 Meta 的 Code Llama2：70 亿、130 亿和 700 亿参数规模（#847）\n- @salman 设计并实现了 Mistral 模型的奖励建模功能（#840、#991）\n\n### 性能、内存与量化\n- 我们优化了 FSDP + Llama3 配方，使 80 亿参数模型的内存占用减少了 13%。（#865）\n- 新增了按 token 动态激活的 Int8 量化，以及按轴分组的 Int4 权重量化（8da4w）（#884）\n\n### 数据\u002F数据集\n- 我们添加了一个广受欢迎的功能——样本打包！该功能可以大幅加速模型训练，例如在 Alpaca 数据集上可实现 2 倍提速。（#875、#1109）\n- 除了指令微调之外，我们现在也支持继续预训练，并提供了多个示例数据集，如 wikitext 和 CNN DailyMail。（#868）\n- 用户现在可以使用 concat 数据集同时训练多个数据集。（#889）\n- 我们现在支持 OpenAI 对话风格的数据格式。（#890）\n\n### 其他\n- @jeromeku 添加了一个更为先进的性能分析工具，帮助用户准确识别其大语言模型训练中的瓶颈所在。（#1089）\n- 我们对指标日志记录进行了多项改进：\n  - 记录每秒处理的 token 数、每步日志以及可配置的内存日志记录（#831）\n  - 改进了 stdout 内存日志的格式化显示（#817）\n- 用户现在可以将模型保存为 safetensors 格式。（#1096）\n- 更新了激活检查点机制，支持选择性层和选择性算子的激活检查点功能（#785）\n- 我们与 Hugging Face 团队合作，提供了加载适配器权重的支持。","2024-07-16T16:26:49",{"id":176,"version":177,"summary_zh":178,"released_at":179},89953,"v0.1.1","### 概述\n\n此补丁增加了对使用 torchtune 对 [Llama3](https:\u002F\u002Fllama.meta.com\u002Fllama3) 进行微调的支持，以及对该库的多项改进。\n\n### 新功能与改进\n\n#### 配方\n* 添加了 Llama2 13B QLoRA 的配置 (#779)\n* 增加了对 Llama2 70B LoRA 的支持 (#788)\n\n#### 模型\n* 增加了对 Llama3 的支持 (#793)\n\n#### 工具\n* 对 Weights & Biases 日志记录器进行了改进 (#772, #777)\n\n### 文档\n* 添加了 Llama3 教程 (#793)\n* 更新了端到端教程，加入了上传至 Hugging Face Hub 的说明 (#773)\n* 更新了 README 文件 (#775, #778, #786)\n* 增加了安装 torchtune 夜间版的说明 (#792)\n\n","2024-04-18T18:51:11",{"id":181,"version":182,"summary_zh":183,"released_at":184},89954,"v0.1.0","### 概述\n\n我们很高兴地宣布 torchtune v0.1.0 正式发布！torchtune 是一个基于 PyTorch 的库，旨在简化大型语言模型（LLM）的构建、微调和实验。该库着重于以下四个关键方面：\n\n* 简洁与可扩展性：原生 PyTorch 实现、组件化设计以及易于复用的抽象层。\n* 正确性：对组件和配方的正确性有极高的验证标准。\n* 稳定性：PyTorch 本身运行稳定，torchtune 也应如此。\n* 普及 LLM 微调：无论是在消费级还是专业级硬件上，都能开箱即用。\n\ntorchtune 已在最新的稳定版 PyTorch（2.2.2）以及预览版 nightly 版本上进行了全面测试。\n\n### 新特性\n\n以下是本次发布中的一些亮点功能：\n\n#### 配方\n* 增加了使用单 GPU 运行 LoRA 微调的支持 (#454)  \n* 增加了使用单 GPU 运行 QLoRA 微调的支持 (#478)  \n* 增加了使用多 GPU 和 FSDP 运行 LoRA 微调的支持 (#454, #266)  \n* 增加了使用单 GPU 运行全量微调的支持 (#482)  \n* 增加了使用多 GPU 和 FSDP 运行全量微调的支持 (#251, #482)  \n* 增加了 DPO 的实验性支持 (#645)  \n* 集成了 EleutherAI Eval Harness 以提供评估配方 (#549)  \n* 通过与 torchao 集成，增加了量化支持 (#632)  \n* 增加了单 GPU 推理支持 (#619)  \n* 创建了一个配置解析系统，可通过 YAML 文件和命令行与配方交互 (#406, #456, #468)  \n\n#### 模型\n* 增加了对 Llama2 7B (#70, #137) 和 13B (#571) 的支持  \n* 增加了对 Mistral 7B (#571) 的支持  \n* 增加了对 Gemma 的支持 [开发中] (#630, #668)  \n\n#### 数据集\n* 增加了对指令式和聊天式数据集的支持 (#752, #624)  \n* 包含了数据集的示例实现 (#303, #116, #407, #541, #576, #645)  \n* 集成了 Hugging Face Datasets (#70)  \n\n#### 工具\n* 集成了 Weights & Biases 用于指标日志记录 (#162, #660)  \n* 创建了一个检查点工具，用于处理来自 HF 和 Meta 的模型文件 (#442)  \n* 增加了一个 tune CLI 工具 (#396)  \n\n### 文档\n\n除了记录 torchtune 的公开 API 外，我们在文档中还新增了多篇教程和深度解析文章：\n\n* 增加了 LoRA 教程 (#368)  \n* 增加了“使用 torchtune 的端到端工作流”教程 (#690)  \n* 增加了数据集教程 (#735)  \n* 增加了 QLoRA 教程 (#693)  \n* 增加了关于检查点工具的深度解析 (#674)  \n* 增加了关于配置的深度解析 (#311)  \n* 增加了关于配方的深度解析 (#316)  \n* 增加了关于 Weights & Biases 集成的深度解析 (#660)  \n\n### 社区贡献\n\n本次 torchtune 发布包含了社区成员的多项杰出贡献：\n\n* @solitude-alive 贡献的 Gemma 2B 模型 (#630)  \n* @yechenzhi 贡献的 DPO 微调配方 (#645)  \n* @tcapelle 贡献的 Weights & Biases 更新 (#660)","2024-04-16T01:57:21"]