[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-uclaml--SPIN":3,"tool-uclaml--SPIN":61},[4,18,26,36,44,53],{"id":5,"name":6,"github_repo":7,"description_zh":8,"stars":9,"difficulty_score":10,"last_commit_at":11,"category_tags":12,"status":17},4358,"openclaw","openclaw\u002Fopenclaw","OpenClaw 是一款专为个人打造的本地化 AI 助手，旨在让你在自己的设备上拥有完全可控的智能伙伴。它打破了传统 AI 助手局限于特定网页或应用的束缚，能够直接接入你日常使用的各类通讯渠道，包括微信、WhatsApp、Telegram、Discord、iMessage 等数十种平台。无论你在哪个聊天软件中发送消息，OpenClaw 都能即时响应，甚至支持在 macOS、iOS 和 Android 设备上进行语音交互，并提供实时的画布渲染功能供你操控。\n\n这款工具主要解决了用户对数据隐私、响应速度以及“始终在线”体验的需求。通过将 AI 部署在本地，用户无需依赖云端服务即可享受快速、私密的智能辅助，真正实现了“你的数据，你做主”。其独特的技术亮点在于强大的网关架构，将控制平面与核心助手分离，确保跨平台通信的流畅性与扩展性。\n\nOpenClaw 非常适合希望构建个性化工作流的技术爱好者、开发者，以及注重隐私保护且不愿被单一生态绑定的普通用户。只要具备基础的终端操作能力（支持 macOS、Linux 及 Windows WSL2），即可通过简单的命令行引导完成部署。如果你渴望拥有一个懂你",349277,3,"2026-04-06T06:32:30",[13,14,15,16],"Agent","开发框架","图像","数据工具","ready",{"id":19,"name":20,"github_repo":21,"description_zh":22,"stars":23,"difficulty_score":10,"last_commit_at":24,"category_tags":25,"status":17},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,"2026-04-05T11:01:52",[14,15,13],{"id":27,"name":28,"github_repo":29,"description_zh":30,"stars":31,"difficulty_score":32,"last_commit_at":33,"category_tags":34,"status":17},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 真正成长为懂上",156804,2,"2026-04-15T11:34:33",[14,13,35],"语言模型",{"id":37,"name":38,"github_repo":39,"description_zh":40,"stars":41,"difficulty_score":32,"last_commit_at":42,"category_tags":43,"status":17},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 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",108322,"2026-04-10T11:39:34",[14,15,13],{"id":45,"name":46,"github_repo":47,"description_zh":48,"stars":49,"difficulty_score":32,"last_commit_at":50,"category_tags":51,"status":17},6121,"gemini-cli","google-gemini\u002Fgemini-cli","gemini-cli 是一款由谷歌推出的开源 AI 命令行工具，它将强大的 Gemini 大模型能力直接集成到用户的终端环境中。对于习惯在命令行工作的开发者而言，它提供了一条从输入提示词到获取模型响应的最短路径，无需切换窗口即可享受智能辅助。\n\n这款工具主要解决了开发过程中频繁上下文切换的痛点，让用户能在熟悉的终端界面内直接完成代码理解、生成、调试以及自动化运维任务。无论是查询大型代码库、根据草图生成应用，还是执行复杂的 Git 操作，gemini-cli 都能通过自然语言指令高效处理。\n\n它特别适合广大软件工程师、DevOps 人员及技术研究人员使用。其核心亮点包括支持高达 100 万 token 的超长上下文窗口，具备出色的逻辑推理能力；内置 Google 搜索、文件操作及 Shell 命令执行等实用工具；更独特的是，它支持 MCP（模型上下文协议），允许用户灵活扩展自定义集成，连接如图像生成等外部能力。此外，个人谷歌账号即可享受免费的额度支持，且项目基于 Apache 2.0 协议完全开源，是提升终端工作效率的理想助手。",100752,"2026-04-10T01:20:03",[52,13,15,14],"插件",{"id":54,"name":55,"github_repo":56,"description_zh":57,"stars":58,"difficulty_score":32,"last_commit_at":59,"category_tags":60,"status":17},4721,"markitdown","microsoft\u002Fmarkitdown","MarkItDown 是一款由微软 AutoGen 团队打造的轻量级 Python 工具，专为将各类文件高效转换为 Markdown 格式而设计。它支持 PDF、Word、Excel、PPT、图片（含 OCR）、音频（含语音转录）、HTML 乃至 YouTube 链接等多种格式的解析，能够精准提取文档中的标题、列表、表格和链接等关键结构信息。\n\n在人工智能应用日益普及的今天，大语言模型（LLM）虽擅长处理文本，却难以直接读取复杂的二进制办公文档。MarkItDown 恰好解决了这一痛点，它将非结构化或半结构化的文件转化为模型“原生理解”且 Token 效率极高的 Markdown 格式，成为连接本地文件与 AI 分析 pipeline 的理想桥梁。此外，它还提供了 MCP（模型上下文协议）服务器，可无缝集成到 Claude Desktop 等 LLM 应用中。\n\n这款工具特别适合开发者、数据科学家及 AI 研究人员使用，尤其是那些需要构建文档检索增强生成（RAG）系统、进行批量文本分析或希望让 AI 助手直接“阅读”本地文件的用户。虽然生成的内容也具备一定可读性，但其核心优势在于为机器",93400,"2026-04-06T19:52:38",[52,14],{"id":62,"github_repo":63,"name":64,"description_en":65,"description_zh":66,"ai_summary_zh":67,"readme_en":68,"readme_zh":69,"quickstart_zh":70,"use_case_zh":71,"hero_image_url":72,"owner_login":73,"owner_name":74,"owner_avatar_url":75,"owner_bio":76,"owner_company":77,"owner_location":78,"owner_email":78,"owner_twitter":78,"owner_website":79,"owner_url":80,"languages":81,"stars":90,"forks":91,"last_commit_at":92,"license":93,"difficulty_score":10,"env_os":94,"env_gpu":95,"env_ram":96,"env_deps":97,"category_tags":106,"github_topics":107,"view_count":32,"oss_zip_url":78,"oss_zip_packed_at":78,"status":17,"created_at":112,"updated_at":113,"faqs":114,"releases":145},7824,"uclaml\u002FSPIN","SPIN","The official implementation of Self-Play Fine-Tuning (SPIN)","SPIN（Self-Play Fine-Tuning）是一种创新的开源大语言模型微调技术，旨在通过“自我博弈”机制显著提升模型能力。它核心解决了传统对齐方法过度依赖昂贵且稀缺的人工标注偏好数据这一痛点。SPIN 无需额外的人类反馈，仅利用现有的监督微调（SFT）数据集，就能让模型与自己之前的版本进行“对弈”：模型生成新的回答，并学习区分这些自生成内容与原始高质量数据，从而在迭代中不断自我进化。\n\n这项技术的独特亮点在于其高效的数据闭环策略。实验表明，经过几轮自我博弈训练，原本表现平平的模型在开放大模型排行榜（Open LLM Leaderboard）和 MT-Bench 等权威评测中的得分均大幅超越直接使用人类偏好数据训练的模型，甚至能媲美更强大的基线。\n\nSPIN 非常适合 AI 研究人员、大模型开发者以及希望以低成本优化私有模型的企业团队使用。对于缺乏大规模人工标注资源但拥有基础 SFT 数据的用户而言，SPIN 提供了一条极具性价比的性能提升路径。该项目已公开完整的训练代码、复现脚本及预训练模型，支持基于 Mistral-7B 等主流架构快速上手，助力社区以更少的资源构建更强的语","SPIN（Self-Play Fine-Tuning）是一种创新的开源大语言模型微调技术，旨在通过“自我博弈”机制显著提升模型能力。它核心解决了传统对齐方法过度依赖昂贵且稀缺的人工标注偏好数据这一痛点。SPIN 无需额外的人类反馈，仅利用现有的监督微调（SFT）数据集，就能让模型与自己之前的版本进行“对弈”：模型生成新的回答，并学习区分这些自生成内容与原始高质量数据，从而在迭代中不断自我进化。\n\n这项技术的独特亮点在于其高效的数据闭环策略。实验表明，经过几轮自我博弈训练，原本表现平平的模型在开放大模型排行榜（Open LLM Leaderboard）和 MT-Bench 等权威评测中的得分均大幅超越直接使用人类偏好数据训练的模型，甚至能媲美更强大的基线。\n\nSPIN 非常适合 AI 研究人员、大模型开发者以及希望以低成本优化私有模型的企业团队使用。对于缺乏大规模人工标注资源但拥有基础 SFT 数据的用户而言，SPIN 提供了一条极具性价比的性能提升路径。该项目已公开完整的训练代码、复现脚本及预训练模型，支持基于 Mistral-7B 等主流架构快速上手，助力社区以更少的资源构建更强的语言模型。","\u003Cp align=\"center\">\n    \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fuclaml_SPIN_readme_a29416a691cf.png\" width=\"30%\"> \u003Cbr>\n\u003C\u002Fp>\n\u003Cp align=\"center\">\n    🤗 \u003Ca href=\"https:\u002F\u002Fhuggingface.co\u002Fcollections\u002FUCLA-AGI\u002Fzephyr-7b-sft-full-spin-65c361dfca65637272a02c40\" target=\"_blank\">Models\u003C\u002Fa> | 🤗 \u003Ca href=\"https:\u002F\u002Fhuggingface.co\u002Fcollections\u002FUCLA-AGI\u002Fdatasets-spin-65c3624e98d4b589bbc76f3a\" target=\"_blank\">Datasets\u003C\u002Fa>\n\u003C\u002Fp>\n\n# Self-Play Fine-Tuning (SPIN)\n\n![Mistral-7B](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FModel-Mistral--7B--v0.1-green) ![Open LLM](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FTask-Open_LLM_Leaderboard-red) ![MT-Bench](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FTask-MT--Bench-red)\n\nThis repository contains the official code for the paper \"[Self-Play Fine-Tuning Converts Weak Language Models to Strong Language Models](https:\u002F\u002Farxiv.org\u002Fabs\u002F2401.01335)\".\n\nAuthors: [Zixiang Chen](https:\u002F\u002Fsites.google.com\u002Fview\u002Fzxchen)\\*, [Yihe Deng](https:\u002F\u002Fsites.google.com\u002Fg.ucla.edu\u002Fyihedeng\u002F)\\*, [Huizhuo Yuan](https:\u002F\u002Fscholar.google.com\u002Fcitations?user=8foZzX4AAAAJ)\\*, [Kaixuan Ji](https:\u002F\u002Fscholar.google.com\u002Fcitations?user=FOoKDukAAAAJ), [Quanquan Gu](https:\u002F\u002Fweb.cs.ucla.edu\u002F~qgu\u002F)\n\n[[Webpage](https:\u002F\u002Fuclaml.github.io\u002FSPIN\u002F)] [[Huggingface](https:\u002F\u002Fhuggingface.co\u002Fpapers\u002F2401.01335)] \n\n\n## 🔔 News \n- **[05\u002F01\u002F2024]** SPIN has been accepted by ICML2024!\n- **[04\u002F06\u002F2024]** We released the training scripts to reproduce our results.\n- **[04\u002F04\u002F2024]** ❗ We've discovered that the datasets previously uploaded are incorrect. We've re-upload the corrected datasets.\n- **[02\u002F13\u002F2024]** We updated our arXiv v2: https:\u002F\u002Farxiv.org\u002Fabs\u002F2401.01335.\n- **[02\u002F09\u002F2024]** Our code is open-sourced!\n- **[01\u002F02\u002F2024]** Our paper is released on arXiv: https:\u002F\u002Farxiv.org\u002Fabs\u002F2401.01335.\n\n❗ __Note__: We notice that the [Alignment Handbook](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Falignment-handbook) has updated their configuration and SFT checkpoint since our experiments. The configuration and SFT model from the Alignment Handbook that we used in our experiments for data generation and fine-tuning are the older version ([Config](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Falignment-handbook\u002Fblob\u002F61a11a5c7d66179ed0a930b0dd12e532fce701dd\u002Frecipes\u002Fzephyr-7b-beta\u002Fdpo\u002Fconfig_full.yaml), [Model](https:\u002F\u002Fhuggingface.co\u002Falignment-handbook\u002Fzephyr-7b-sft-full\u002Ftree\u002Fac6e600eefcce74f5e8bae1035d4f66019e93190)). The model checkpoint on Hugging Face (`alignment-handbook\u002Fzephyr-7b-sft-full`) has been updated once with the new config. If you wish to use the newest SFT model, you need to either load the checkpoint with `revision=ac6e600eefcce74f5e8bae1035d4f66019e93190` or generate your own data instead of using the [datasets](https:\u002F\u002Fhuggingface.co\u002Fcollections\u002FUCLA-AGI\u002Fdatasets-spin-65c3624e98d4b589bbc76f3a) we provided on Hugging Face.\n\n\n## Table of Contents\n- [About SPIN](#🌀-about-spin)\n- [Setup](#Setup)\n    - [Data](#Data)\n    - [Model](#Model)\n- [Usage](#Usage)\n    - [Step 1: Generation](#step-1-generation)\n        - [Faster generation with vLLM](#🚀-faster-generation-with-vllm)\n    - [Step 1.5: Gather generations and convert data type](#step-15-gather-generations-and-convert-data-type)\n    - [Step 2: Fine-tuning](#step-2-fine-tuning)\n- [Reproducing Our Results](#Reproducing-Our-Results)\n- [Evaluation](#Evaluation)\n- [Citation](#Citation)\n- [Acknowledgement](#Acknowledgement)\n\n\n\n## 🌀 About SPIN\n**SPIN** utilizes a self-play mechanism, allowing an LLM to improve itself by playing against its previous iterations, without needing additional human-annotated preference data than the SFT dataset itself. More specifically, the LLM generates its own training data from its previous iterations, refining its policy by discerning these self-generated responses from the original SFT data. \n\u003Cp align=\"center\">\n    \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fuclaml_SPIN_readme_9e6dae753c84.png\" width=\"35%\"> \u003Cbr>\n  Average score of \u003Cb>SPIN\u003C\u002Fb> at different iterations on the HuggingFace Open LLM leaderboard. \n\u003C\u002Fp>\nSPIN can significantly enhance the performance of an LLM after SFT across various benchmarks, outperforming the model trained with direct preference optimization (DPO) on labelled preference datasets. The approach is theoretically grounded, ensuring that the LLM aligns with the target data distribution, and empirically validated through extensive evaluations on multiple datasets. \n\u003Cp align=\"center\">\n    \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fuclaml_SPIN_readme_0b150cc4a8b8.png\" width=\"80%\"> \u003Cbr>\n  Performance comparison with DPO training across the six benchmark datasets. SPIN at iteration 0 achieves comparable performance to DPO training with 62k new data. At iteration 1, SPIN has already surpassed DPO training on the majority of datasets. \n\u003C\u002Fp>\n\nFor more details, you can check our paper [here](https:\u002F\u002Farxiv.org\u002Fabs\u002F2401.01335).\n\n## Setup\nThe following steps provide the necessary setup to run our codes.\n1. Create a Python virtual environment with Conda:\n```\nconda create -n myenv python=3.10\nconda activate myenv\n```\n2. Install the following Python dependencies to run the codes.\n```\npython -m pip install .\npython -m pip install flash-attn --no-build-isolation\n```\n3. Login to your huggingface account for downloading models\n```\nhuggingface-cli login --token \"${your_access_token}\"\n```\n\n### Data \nWe provide the data used in our experiments along with the synthetic data we generated in this repo as well as on HuggingFace. These data are converted to .parquet format for fine-tuning. \n\n❗ On 04\u002F04\u002F2024, we've discovered that the datasets previously uploaded are incorrect. We've re-upload the corrected datasets.\n\n| Dataset                    |                           Download                           |\n| :----------------------- | :----------------------------------------------------------: |\n| SPIN_iter0     | 🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter0) |\n| SPIN_iter1 | 🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter1) |\n| SPIN_iter2      |   🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter2) |\n| SPIN_iter3      |   🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter3) |\n\nThe input data for our code is required to be of the same format where each data contains the following attributes, as similar to [HuggingFaceH4\u002Fultrafeedback_binarized](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FHuggingFaceH4\u002Fultrafeedback_binarized):\n```\n{\n    \"real\": [{\"role\": \"user\", \"content\": \u003Cprompt>}, \n               {\"role\": \"assistant\", \"content\": \u003Cground truth>}],\n    \"generated\": [{\"role\": \"user\", \"content\": \u003Cprompt>}, \n                 {\"role\": \"assistant\", \"content\": \u003Cgeneration>}]\n}\n```\n🔍 Note: During data generation, the content for generated response can be empty, as we only uses prompt to generate model responses. \n\n### Model\nWe also provide our model checkpoints at iteration 0,1,2,3 on HuggingFace.\n\n| Model                    |                           Download                           |\n| :----------------------- | :----------------------------------------------------------: |\n| zephyr-7b-sft-full-SPIN-iter0     | 🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002FUCLA-AGI\u002Fzephyr-7b-sft-full-SPIN-iter0) |\n| zephyr-7b-sft-full-SPIN-iter1 | 🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002FUCLA-AGI\u002Fzephyr-7b-sft-full-SPIN-iter1) |\n| zephyr-7b-sft-full-SPIN-iter2      |   🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002FUCLA-AGI\u002Fzephyr-7b-sft-full-SPIN-iter2) |\n| zephyr-7b-sft-full-SPIN-iter3     |   🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002FUCLA-AGI\u002Fzephyr-7b-sft-full-SPIN-iter3) |\n\n🔍 __Note__: \u003Cspan style=\"color:red;\">With the provided data, you can directly jump to [Step 2: Fine-tuning](#step-2-fine-tuning) without doing data generation on your own. You may also start from any iteration to reproduce our results using our open-sourced model checkpoints.\u003C\u002Fspan>\n\n## Usage\nFor SPIN, we generate all synthetic data at once for an iteration, and fine-tune the LLM based on the real and synthetic data pairs. \n\n### Step 0 (optional): Reformatting SFT dataset\n```\npython spin\u002Freformat.py [options]\n```\nOptions\n- `--data`: directory to the SFT dataset (local or huggingface)\n    - default: `HuggingFaceH4\u002Fultrachat_200k`\n- `--output_dir`: local directory to the reformated data files \n    - default: `UCLA-AGI\u002FSPIN_iter0`\n\n🔍 Note: If choosing to use SPIN on the entire dataset of `HuggingFaceH4\u002Fultrachat_200k` instead of our 50k subset, one can reformat the original data with `spin\u002Freformat.py`. To use other datasets, simply convert the data into the same format and resume with the following steps. \n\n### Step 1: Generation\n```\naccelerate launch spin\u002Fgenerate.py [options]\n```\nOptions\n- `--model`: load model checkpoint for generation.\n    - default: `alignment-handbook\u002Fzephyr-7b-sft-full`\n- `--input_dir`: directory to the data files with prompts for generation\n    - The code is for generation based on data of the format given below. \n    - default: `UCLA-AGI\u002FSPIN_iter0`\n- `--output_dir`: directory to save the output data. \n- `--batch_size`: per device batch size\n    - default: 16\n- `--data_frac`: break data into fractions for generations across server.\n    - `--frac_len`: length of the data fraction. Default is 0 which uses the entire dataset for generation. Set `frac_len` to a positive number to generate only for a fraction of data. __Note__: we recommend using a smaller frac_len (e.g. 800) to generate data by small batches to avoid unexpected crashes as data generation can be very time-consuming.  \n    - Setting `data_frac` to be 0, 1, 2... to generate for different fractions of length `frac_len`.\n    - Note: maintain the same frac length when doing generation using data_frac. It's recommended to set a smaller `frac_len` to 800.\n- `--split`: choose the split for data generation\n    - default: `train`\n\nThe generated data is in json format where each data contains the following attributes:\n```\n{\n    \"real\": [{\"role\": \"user\", \"content\": \u003Cprompt>}, \n               {\"role\": \"assistant\", \"content\": \u003Cground truth>}],\n    \"generated\": [{\"role\": \"user\", \"content\": \u003Cprompt>}, \n                 {\"role\": \"assistant\", \"content\": \u003Cgeneration>}]\n}\n```\n\n**Note**: the iteration number for data generation solely depends on which model is used for generation (e.g. use original SFT model for iter0 data generation and SPIN iter0 model for iter1 data generation). The `generate.py` script will only use the prompts\u002Fquestions for the data model.  \n\n__Example__. \nThe following code generates 8k synthetic data for iteration 0.\n```\nbash scripts\u002Fgenerate.sh\n``` \n\n#### 🚀 Faster generation with vLLM\nAlternatively, you could use the following example script to generate LLM responses with speedup. Larger `frac_len` can be used with vllm.\n```\nbash scripts\u002Fgenerate_vllm.sh\n```  \nThanks to @sumo43 for implementing vLLM for generation. \n\n### Step 1.5: Gather generations and convert data type\n```\npython spin\u002Fconvert_data.py [options]\n```\nOptions\n- `--num_fracs`: number of files to load in.\n- `--input_dir`: directory to the generated data files.\n- `--output_dir`: directory for the unified data that will be used for fine-tuning. \n\nThe code will generate the two final data files including `train_prefs-00000-of-00001.parquet` and `test_prefs-00000-of-00001.parquet`, which will be used for fine-tuning.\n\nNote: make sure to collect the generated data filed into the same directory of `--input_dir`. \n\n__Example__.\n```\npython spin\u002Fconvert_data.py --output_dir new_data\u002Fiter0 --input_dir generated\u002Fiter0 --num_fracs 63\n```\n\n### Step 2: Fine-tuning\n```\naccelerate launch --config_file configs\u002Fmulti_gpu.yaml --num_processes=8 --main_process_port 29500 spin\u002Frun_spin.py configs\u002Fconfig.yaml\n```\n\u003C!-- **[TODO]**: wrap up necessary codes into the folder spin. Add explainations\u002Finstructions here.  -->\n\nYou might need to change the configuration in `configs\u002Fconfig.yaml`. Here are some key configs you might need to customize:\n\n- `model_name_or_path`: load model checkpoint for finetuning.\n    - default: `alignment-handbook\u002Fzephyr-7b-sft-full`\n- `dataset_mixer`: choose data to mix for fine-tuning.\n    - default: `UCLA-AGI\u002FSPIN_iter0: 1.0`\n    - For SPIN at iteration 1 and 2, we included both the current iteration and the previous iteration (e.g. for iteration 1 we included both `UCLA-AGI\u002FSPIN_iter0: 1.0` and `UCLA-AGI\u002FSPIN_iter1: 1.0`, summing to 100k data.)\n- `output_dir`: the output directory of finetuned model and checkpoints. \n    - default: `outputs`\n- `per_device_train_batch_size`: batch size on one GPU.\n    - default: 16\n- `gradient_accumulation_steps`: make sure that the product per_device_train_batch_size\\*num_processes\\*gradient_accumulation_steps equals to your true batch size.\n- `num_train_epochs`: the training epochs of this iteration.\n    - default: 3\n- `beta`: beta in SPIN.\n    - default: 0.1\n\nIn our experiments, we do full fine-tuning on a multi-GPU machine with DeepSpeed ZeRO-3 (requires A100 (80GB)).\n\n__Example__.\n```\nbash scripts\u002Ffinetune.sh\n```\n## Reproducing Our Results\n\nTo help reproducing our results, we have made available the scripts corresponding to all four iterations of our study. These scripts are pre-configured with the exact parameters and model versions used in our paper. For each iteration, the base model is initialized with the version released on 🤗 HuggingFace, which can be found at the following links:\n\n| Dataset                    |                           Download                           |\n| :----------------------- | :----------------------------------------------------------: |\n| SPIN_iter0     | 🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter0) |\n| SPIN_iter1 | 🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter1) |\n| SPIN_iter2      |   🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter2) |\n| SPIN_iter3      |   🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter3) |\n\nTo execute the full pipeline using your locally trained models as the base, modify the `model_name_or_path` parameter in the configuration files to point to your model's path.\n\nTo start the full fine-tuning process, run the corresponding script from your terminal:\n\n```bash\nbash scripts\u002Ffinetune.sh\nbash scripts\u002Ffinetune_iter1.sh\nbash scripts\u002Ffinetune_iter2.sh\nbash scripts\u002Ffinetune_iter3.sh\n```\n\nBy following these steps, you should be able to reproduce our results.\n\n---\n\n## Evaluation\nFor our evaluation on the Open LLM Leaderboard, please use this [lm-evaluation-harness](https:\u002F\u002Fgithub.com\u002FEleutherAI\u002Flm-evaluation-harness\u002Ftree\u002F46c796644913cd99da7eee868e64f9ed6af33407) which is v0.4.0. Also, note that we set the number of few shot examples to be the same as instructed on the [Leaderboard](https:\u002F\u002Fhuggingface.co\u002Fspaces\u002FHuggingFaceH4\u002Fopen_llm_leaderboard). Different evaluation versions results in different scores, but the trend will remain the same.\n\n\n## Star History\n\n[![Star History Chart](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fuclaml_SPIN_readme_176732116822.png)](https:\u002F\u002Fstar-history.com\u002F#uclaml\u002FSPIN&Date)\n\n## Citation\nIf you find this repo useful for your research, please consider citing the paper\n```\n@misc{chen2024selfplay,\n      title={Self-Play Fine-Tuning Converts Weak Language Models to Strong Language Models}, \n      author={Zixiang Chen and Yihe Deng and Huizhuo Yuan and Kaixuan Ji and Quanquan Gu},\n      year={2024},\n      eprint={2401.01335},\n      archivePrefix={arXiv},\n      primaryClass={cs.LG}\n}\n```\n\n## Acknowledgement\nThis repo is built upon [The Alignment Handbook](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Falignment-handbook). We thank the authors for their great work.\n","\u003Cp align=\"center\">\n    \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fuclaml_SPIN_readme_a29416a691cf.png\" width=\"30%\"> \u003Cbr>\n\u003C\u002Fp>\n\u003Cp align=\"center\">\n    🤗 \u003Ca href=\"https:\u002F\u002Fhuggingface.co\u002Fcollections\u002FUCLA-AGI\u002Fzephyr-7b-sft-full-spin-65c361dfca65637272a02c40\" target=\"_blank\">模型\u003C\u002Fa> | 🤗 \u003Ca href=\"https:\u002F\u002Fhuggingface.co\u002Fcollections\u002FUCLA-AGI\u002Fdatasets-spin-65c3624e98d4b589bbc76f3a\" target=\"_blank\">数据集\u003C\u002Fa>\n\u003C\u002Fp>\n\n# 自对弈微调（SPIN）\n\n![Mistral-7B](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FModel-Mistral--7B--v0.1-green) ![Open LLM](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FTask-Open_LLM_Leaderboard-red) ![MT-Bench](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FTask-MT--Bench-red)\n\n本仓库包含论文《自对弈微调将弱语言模型转化为强语言模型》（https:\u002F\u002Farxiv.org\u002Fabs\u002F2401.01335）的官方代码。\n\n作者：[Zixiang Chen](https:\u002F\u002Fsites.google.com\u002Fview\u002Fzxchen)\\*, [Yihe Deng](https:\u002F\u002Fsites.google.com\u002Fg.ucla.edu\u002Fyihedeng\u002F)\\*, [Huizhuo Yuan](https:\u002F\u002Fscholar.google.com\u002Fcitations?user=8foZzX4AAAAJ)\\*, [Kaixuan Ji](https:\u002F\u002Fscholar.google.com\u002Fcitations?user=FOoKDukAAAAJ), [Quanquan Gu](https:\u002F\u002Fweb.cs.ucla.edu\u002F~qgu\u002F)\n\n[[网页](https:\u002F\u002Fuclaml.github.io\u002FSPIN\u002F)] [[HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fpapers\u002F2401.01335)]\n\n\n## 🔔 新闻 \n- **[05\u002F01\u002F2024]** SPIN已被ICML2024接收！\n- **[04\u002F06\u002F2024]** 我们发布了用于复现实验结果的训练脚本。\n- **[04\u002F04\u002F2024]** ❗ 我们发现之前上传的数据集存在错误，现已重新上传修正后的数据集。\n- **[02\u002F13\u002F2024]** 我们更新了arXiv v2版本：https:\u002F\u002Farxiv.org\u002Fabs\u002F2401.01335。\n- **[02\u002F09\u002F2024]** 我们的代码已开源！\n- **[01\u002F02\u002F2024]** 我们的论文已在arXiv上发布：https:\u002F\u002Farxiv.org\u002Fabs\u002F2401.01335。\n\n❗ __注意__：我们注意到[HuggingFace对齐手册](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Falignment-handbook)自我们的实验以来已经更新了其配置和SFT检查点。我们在实验中用于数据生成和微调的对齐手册中的配置及SFT模型均为旧版本（[配置](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Falignment-handbook\u002Fblob\u002F61a11a5c7d66179ed0a930b0dd12e532fce701dd\u002Frecipes\u002Fzephyr-7b-beta\u002Fdpo\u002Fconfig_full.yaml)，[模型](https:\u002F\u002Fhuggingface.co\u002Falignment-handbook\u002Fzephyr-7b-sft-full\u002Ftree\u002Fac6e600eefcce74f5e8bae1035d4f66019e93190)）。HuggingFace上的该模型检查点（`alignment-handbook\u002Fzephyr-7b-sft-full`）已使用新配置更新过一次。如果您希望使用最新的SFT模型，可以加载带有`revision=ac6e600eefcce74f5e8bae1035d4f66019e93190`的检查点，或者自行生成数据，而非使用我们在HuggingFace上提供的[数据集](https:\u002F\u002Fhuggingface.co\u002Fcollections\u002FUCLA-AGI\u002Fdatasets-spin-65c3624e98d4b589bbc76f3a)。\n\n## 目录\n- [关于SPIN](#🌀-about-spin)\n- [设置](#Setup)\n    - [数据](#Data)\n    - [模型](#Model)\n- [使用方法](#Usage)\n    - [步骤1：生成](#step-1-generation)\n        - [使用vLLM加速生成](#🚀-faster-generation-with-vllm)\n    - [步骤1.5：收集生成内容并转换数据类型](#step-15-gather-generations-and-convert-data-type)\n    - [步骤2：微调](#step-2-fine-tuning)\n- [复现我们的结果](#Reproducing-Our-Results)\n- [评估](#Evaluation)\n- [引用](#Citation)\n- [致谢](#Acknowledgement)\n\n\n\n## 🌀 关于SPIN\n**SPIN**采用自对弈机制，允许语言模型通过与自身先前版本的交互来不断提升性能，而无需额外的人工标注偏好数据，仅依赖于SFT数据集本身即可。具体而言，语言模型会基于其先前版本生成自己的训练数据，并通过区分这些自动生成的响应与原始SFT数据，不断优化自身的策略。\n\u003Cp align=\"center\">\n    \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fuclaml_SPIN_readme_9e6dae753c84.png\" width=\"35%\"> \u003Cbr>\n  HuggingFace Open LLM排行榜上，不同迭代次数下\u003Cb>SPIN\u003C\u002Fb>的平均得分。\n\u003C\u002Fp>\nSPIN能够在多种基准测试中显著提升SFT后语言模型的性能，甚至超越了在标注偏好数据集上进行直接偏好优化（DPO）训练的模型。该方法具有坚实的理论基础，确保语言模型能够与目标数据分布保持一致，并通过在多个数据集上的广泛评估得到了实证验证。\n\u003Cp align=\"center\">\n    \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fuclaml_SPIN_readme_0b150cc4a8b8.png\" width=\"80%\"> \u003Cbr>\n  在六个基准数据集上与DPO训练的性能对比。SPIN在第0次迭代时，性能已可与使用6.2万条新增数据进行DPO训练的模型相媲美；而在第1次迭代时，SPIN已经在大多数数据集上超越了DPO训练的结果。\n\u003C\u002Fp>\n\n更多详细信息，请参阅我们的论文[此处](https:\u002F\u002Farxiv.org\u002Fabs\u002F2401.01335)。\n\n## 设置\n以下步骤提供了运行我们代码所需的必要设置。\n1. 使用Conda创建一个Python虚拟环境：\n```\nconda create -n myenv python=3.10\nconda activate myenv\n```\n2. 安装以下Python依赖以运行代码：\n```\npython -m pip install .\npython -m pip install flash-attn --no-build-isolation\n```\n3. 登录您的HuggingFace账户以便下载模型：\n```\nhuggingface-cli login --token \"${your_access_token}\"\n```\n\n### 数据 \n我们在此仓库以及HuggingFace上提供了实验中使用的数据，同时还包含了我们生成的合成数据。这些数据已被转换为.parquet格式，以便进行微调。\n\n❗ 2024年4月4日，我们发现之前上传的数据集存在错误，现已重新上传了修正后的数据集。\n\n| 数据集                    |                           下载                           |\n| :----------------------- | :----------------------------------------------------------: |\n| SPIN_iter0     | 🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter0) |\n| SPIN_iter1 | 🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter1) |\n| SPIN_iter2      |   🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter2) |\n| SPIN_iter3      |   🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter3) |\n\n我们代码所需的输入数据必须采用相同格式，每条数据应包含以下属性，类似于[HuggingFaceH4\u002Fultrafeedback_binarized](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FHuggingFaceH4\u002Fultrafeedback_binarized)：\n```\n{\n    \"real\": [{\"role\": \"user\", \"content\": \u003Cprompt>}, \n               {\"role\": \"assistant\", \"content\": \u003Cground truth>}],\n    \"generated\": [{\"role\": \"user\", \"content\": \u003Cprompt>}, \n                 {\"role\": \"assistant\", \"content\": \u003Cgeneration>}]\n}\n```\n🔍 注意：在数据生成过程中，生成的回答内容可能为空，因为我们仅使用提示词来生成模型响应。\n\n### 模型\n我们还在 HuggingFace 上提供了模型在第 0、1、2、3 迭代时的检查点。\n\n| 模型                    |                           下载                           |\n| :----------------------- | :----------------------------------------------------------: |\n| zephyr-7b-sft-full-SPIN-iter0     | 🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002FUCLA-AGI\u002Fzephyr-7b-sft-full-SPIN-iter0) |\n| zephyr-7b-sft-full-SPIN-iter1 | 🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002FUCLA-AGI\u002Fzephyr-7b-sft-full-SPIN-iter1) |\n| zephyr-7b-sft-full-SPIN-iter2      |   🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002FUCLA-AGI\u002Fzephyr-7b-sft-full-SPIN-iter2) |\n| zephyr-7b-sft-full-SPIN-iter3     |   🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002FUCLA-AGI\u002Fzephyr-7b-sft-full-SPIN-iter3) |\n\n🔍 __注意__: \u003Cspan style=\"color:red;\">使用提供的数据，您可以直接跳转到[步骤 2：微调](#step-2-fine-tuning)，而无需自行生成数据。您也可以从任意迭代开始，利用我们开源的模型检查点复现我们的结果。\u003C\u002Fspan>\n\n## 使用方法\n对于 SPIN，我们会在一个迭代中一次性生成所有合成数据，并基于真实数据和合成数据对齐后的样本对大语言模型进行微调。\n\n### 步骤 0（可选）：重新格式化 SFT 数据集\n```\npython spin\u002Freformat.py [options]\n```\n选项\n- `--data`: SFT 数据集所在的目录（本地或 HuggingFace）\n    - 默认值：`HuggingFaceH4\u002Fultrachat_200k`\n- `--output_dir`: 重新格式化后数据文件的本地目录\n    - 默认值：`UCLA-AGI\u002FSPIN_iter0`\n\n🔍 注意：如果选择在整个 `HuggingFaceH4\u002Fultrachat_200k` 数据集上使用 SPIN，而不是我们提供的 5 万条子集，可以使用 `spin\u002Freformat.py` 对原始数据进行重新格式化。若要使用其他数据集，只需将其转换为相同格式，然后继续执行后续步骤即可。\n\n### 步骤 1：生成\n```\naccelerate launch spin\u002Fgenerate.py [options]\n```\n选项\n- `--model`: 加载用于生成的模型检查点。\n    - 默认值：`alignment-handbook\u002Fzephyr-7b-sft-full`\n- `--input_dir`: 包含生成提示的数据文件所在目录\n    - 代码适用于以下格式的数据生成。\n    - 默认值：`UCLA-AGI\u002FSPIN_iter0`\n- `--output_dir`: 保存生成数据的目录。\n- `--batch_size`: 每个设备的批量大小\n    - 默认值：16\n- `--data_frac`: 将数据分成若干份，在不同服务器上分别生成。\n    - `--frac_len`: 数据分片的长度。默认为 0，表示使用整个数据集进行生成。将 `frac_len` 设置为正数，则只针对数据的一部分进行生成。__注意__: 我们建议使用较小的 `frac_len`（例如 800），以小批量生成数据，避免因数据生成耗时较长而导致意外崩溃。\n    - 将 `data_frac` 设置为 0、1、2…，即可针对不同长度的分片进行生成。\n    - 注意：使用 `data_frac` 进行生成时，请保持相同的分片长度。建议将 `frac_len` 设置为 800。\n\n生成的数据为 JSON 格式，每条数据包含以下属性：\n```\n{\n    \"real\": [{\"role\": \"user\", \"content\": \u003Cprompt>}, \n               {\"role\": \"assistant\", \"content\": \u003Cground truth>}],\n    \"generated\": [{\"role\": \"user\", \"content\": \u003Cprompt>}, \n                 {\"role\": \"assistant\", \"content\": \u003Cgeneration>}]\n}\n```\n\n**注意**: 数据生成的迭代次数完全取决于用于生成的模型版本（例如，使用原始 SFT 模型生成第 0 次迭代的数据，使用 SPIN 第 0 次迭代的模型生成第 1 次迭代的数据）。`generate.py` 脚本只会使用对应数据模型的提示\u002F问题进行生成。\n\n__示例__. \n以下代码生成第 0 次迭代的 8,000 条合成数据。\n```\nbash scripts\u002Fgenerate.sh\n``` \n\n#### 🚀 使用 vLLM 加速生成\n或者，您也可以使用以下示例脚本以更快的速度生成 LLM 回答。使用 vLLM 时可以采用更大的 `frac_len`。\n```\nbash scripts\u002Fgenerate_vllm.sh\n```  \n感谢 @sumo43 实现了基于 vLLM 的生成功能。\n\n### 步骤 1.5：收集生成结果并转换数据类型\n```\npython spin\u002Fconvert_data.py [options]\n```\n选项\n- `--num_fracs`: 需要加载的文件数量。\n- `--input_dir`: 生成数据文件所在的目录。\n- `--output_dir`: 用于微调的统一数据存放目录。\n\n该代码将生成两个最终数据文件，分别为 `train_prefs-00000-of-00001.parquet` 和 `test_prefs-00000-of-00001.parquet`，这些文件将用于微调。\n\n注意：请确保将生成的数据文件收集到与 `--input_dir` 相同的目录中。\n\n__示例__。\n```\npython spin\u002Fconvert_data.py --output_dir new_data\u002Fiter0 --input_dir generated\u002Fiter0 --num_fracs 63\n```\n\n### 步骤 2：微调\n```\naccelerate launch --config_file configs\u002Fmulti_gpu.yaml --num_processes=8 --main_process_port 29500 spin\u002Frun_spin.py configs\u002Fconfig.yaml\n```\n\u003C!-- **[TODO]**: 将必要代码封装到 spin 文件夹中。在此处添加说明和指令。  -->\n\n您可能需要修改 `configs\u002Fconfig.yaml` 中的配置。以下是一些您可能需要自定义的关键配置项：\n\n- `model_name_or_path`: 加载用于微调的模型检查点。\n    - 默认值：`alignment-handbook\u002Fzephyr-7b-sft-full`\n- `dataset_mixer`: 选择用于微调的数据混合比例。\n    - 默认值：`UCLA-AGI\u002FSPIN_iter0: 1.0`\n    - 对于 SPIN 的第 1 和第 2 次迭代，我们同时包含了当前迭代和上一迭代的数据（例如，第 1 次迭代时，我们加入了 `UCLA-AGI\u002FSPIN_iter0: 1.0` 和 `UCLA-AGI\u002FSPIN_iter1: 1.0`，合计 10 万条数据）。\n- `output_dir`: 微调后模型及检查点的输出目录。\n    - 默认值：`outputs`\n- `per_device_train_batch_size`: 单个 GPU 上的训练批次大小。\n    - 默认值：16\n- `gradient_accumulation_steps`: 确保 `per_device_train_batch_size * num_processes * gradient_accumulation_steps` 等于您的实际批次大小。\n- `num_train_epochs`: 当前迭代的训练轮数。\n    - 默认值：3\n- `beta`: SPIN 中的 beta 参数。\n    - 默认值：0.1\n\n在我们的实验中，我们使用 DeepSpeed ZeRO-3 在多 GPU 机器上进行了全量微调（需要 A100 显卡，80GB 版本）。\n\n__示例__。\n```\nbash scripts\u002Ffinetune.sh\n```\n\n## 复现我们的结果\n\n为帮助复现我们的结果，我们已公开与本研究四次迭代相关的脚本。这些脚本已预先配置好论文中使用的精确参数和模型版本。对于每次迭代，基础模型均使用 🤗 HuggingFace 上发布的版本进行初始化，相关链接如下：\n\n| 数据集                    |                           下载                           |\n| :----------------------- | :----------------------------------------------------------: |\n| SPIN_iter0     | 🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter0) |\n| SPIN_iter1 | 🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter1) |\n| SPIN_iter2      |   🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter2) |\n| SPIN_iter3      |   🤗 [HuggingFace](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FUCLA-AGI\u002FSPIN_iter3) |\n\n若要使用您本地训练的模型作为基础来执行完整流程，请将配置文件中的 `model_name_or_path` 参数修改为您模型的路径。\n\n要开始完整的微调过程，可在终端中运行相应的脚本：\n\n```bash\nbash scripts\u002Ffinetune.sh\nbash scripts\u002Ffinetune_iter1.sh\nbash scripts\u002Ffinetune_iter2.sh\nbash scripts\u002Ffinetune_iter3.sh\n```\n\n按照上述步骤操作，您应该能够复现我们的结果。\n\n---\n\n## 评估\n针对 Open LLM Leaderboard 的评估，请使用此 [lm-evaluation-harness](https:\u002F\u002Fgithub.com\u002FEleutherAI\u002Flm-evaluation-harness\u002Ftree\u002F46c796644913cd99da7eee868e64f9ed6af33407)，其版本为 v0.4.0。此外，请注意，我们将少样本示例的数量设置为与 [Leaderboard](https:\u002F\u002Fhuggingface.co\u002Fspaces\u002FHuggingFaceH4\u002Fopen_llm_leaderboard) 中的说明一致。不同版本的评估会导致分数有所差异，但整体趋势将保持不变。\n\n---\n\n## 星标历史\n\n[![星标历史图表](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fuclaml_SPIN_readme_176732116822.png)](https:\u002F\u002Fstar-history.com\u002F#uclaml\u002FSPIN&Date)\n\n## 引用\n若您认为本仓库对您的研究有所帮助，请考虑引用以下论文：\n```\n@misc{chen2024selfplay,\n      title={自对弈微调可将弱语言模型转化为强语言模型}, \n      author={陈子翔、邓一鹤、袁慧卓、季凯旋、顾全全},\n      year={2024},\n      eprint={2401.01335},\n      archivePrefix={arXiv},\n      primaryClass={cs.LG}\n}\n```\n\n## 致谢\n本仓库基于 [Alignment Handbook](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Falignment-handbook) 构建。我们感谢作者们的杰出工作。","# SPIN 快速上手指南\n\nSPIN (Self-Play Fine-Tuning) 是一种通过自博弈机制提升大语言模型性能的技术。它无需额外的人工标注偏好数据，仅利用 SFT 数据集和模型自身生成的迭代数据即可显著增强模型能力。\n\n## 环境准备\n\n在开始之前，请确保您的开发环境满足以下要求：\n\n*   **操作系统**: Linux (推荐 Ubuntu)\n*   **Python 版本**: 3.10\n*   **硬件要求**: 支持 CUDA 的 NVIDIA GPU (建议使用多卡环境进行训练)\n*   **依赖管理**: Conda\n\n## 安装步骤\n\n### 1. 创建虚拟环境\n使用 Conda 创建并激活 Python 3.10 环境：\n\n```bash\nconda create -n spin_env python=3.10\nconda activate spin_env\n```\n\n### 2. 安装项目依赖\n安装本项目及 `flash-attn` 加速库：\n\n```bash\npython -m pip install .\npython -m pip install flash-attn --no-build-isolation\n```\n\n> **提示**：国内用户若遇到 PyPI 下载缓慢，可添加 `-i https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple` 参数加速安装。\n\n### 3. 登录 Hugging Face\n由于需要下载模型和数据集，请登录您的 Hugging Face 账户：\n\n```bash\nhuggingface-cli login --token \"${your_access_token}\"\n```\n\n## 基本使用\n\nSPIN 的核心流程包含“数据生成”与“微调”两个阶段。您可以直接使用官方提供的预处理数据集跳过生成步骤，直接从微调开始。\n\n### 方案 A：直接使用官方数据集进行微调（推荐新手）\n\n如果您希望快速复现结果或测试效果，可以直接使用 Hugging Face 上已准备好的 `SPIN_iter0` 数据集。\n\n**执行微调命令：**\n\n```bash\naccelerate launch --config_file configs\u002Fmulti_gpu.yaml --num_processes=8 --main_process_port 29500 spin\u002Frun_spin.py configs\u002Fconfig.yaml\n```\n\n**关键配置修改：**\n在执行前，请检查 `configs\u002Fconfig.yaml` 文件，确保以下参数设置正确：\n*   `model_name_or_path`: 指定基础模型，例如 `alignment-handbook\u002Fzephyr-7b-sft-full` (注意：如需完全复现论文，可能需要指定特定 revision 或使用旧版 checkpoint)。\n*   `dataset_mixer`: 指定使用的数据集，例如 `UCLA-AGI\u002FSPIN_iter0: 1.0`。\n\n### 方案 B：完整流程（从数据生成到微调）\n\n如果您拥有自己的 SFT 数据集并希望从头开始迭代：\n\n#### 第一步：数据格式化 (可选)\n将原始 SFT 数据集转换为 SPIN 所需格式：\n\n```bash\npython spin\u002Freformat.py --data HuggingFaceH4\u002Fultrachat_200k --output_dir UCLA-AGI\u002FSPIN_iter0\n```\n\n#### 第二步：生成合成数据\n利用当前模型对提示词进行回答，生成自博弈数据：\n\n```bash\naccelerate launch spin\u002Fgenerate.py --model alignment-handbook\u002Fzephyr-7b-sft-full --input_dir UCLA-AGI\u002FSPIN_iter0 --output_dir generated\u002Fiter0\n```\n\n> **加速技巧**：若需更快生成速度，可使用 vLLM 版本：\n> ```bash\n> bash scripts\u002Fgenerate_vllm.sh\n> ```\n\n#### 第三步：整合数据\n将生成的分散数据文件合并并转换为 Parquet 格式，供训练使用：\n\n```bash\npython spin\u002Fconvert_data.py --output_dir new_data\u002Fiter0 --input_dir generated\u002Fiter0 --num_fracs 63\n```\n\n#### 第四步：执行微调\n使用整合后的数据进行模型微调（命令同方案 A，需修改 config 中的数据集路径）。\n\n---\n**注意**：关于模型版本的重要说明\n官方指出 Hugging Face 上的 `alignment-handbook\u002Fzephyr-7b-sft-full` 已更新配置。若要严格复现论文实验中的数据生成环节，建议加载特定版本的 checkpoint：\n`revision=ac6e600eefcce74f5e8bae1035d4f66019e93190`","某初创团队希望基于开源的 Mistral-7B 模型构建专属客服助手，但受限于预算无法承担昂贵的人工偏好标注成本。\n\n### 没有 SPIN 时\n- **数据依赖重**：为了提升模型回答的自然度和逻辑性，必须雇佣专业人员编写大量“人类偏好数据”，资金和时间成本极高。\n- **迭代周期长**：每次调整模型策略都需要等待新一轮的人工标注，导致模型优化流程停滞，难以快速响应业务需求。\n- **性能瓶颈明显**：仅依靠基础的监督微调（SFT），模型在面对复杂多轮对话时容易出现机械重复或逻辑断层，难以达到商用标准。\n- **资源利用率低**：已有的 SFT 数据集被一次性使用后便闲置，缺乏机制让模型从自身历史表现中挖掘更多训练价值。\n\n### 使用 SPIN 后\n- **实现自我进化**：SPIN 让模型通过“自我博弈”机制，利用上一版本的输出作为负样本，自动生成训练数据，完全省去了额外的人工标注环节。\n- **加速迭代闭环**：团队只需运行脚本即可让模型在内部进行多轮对抗训练，将原本数周的数据准备周期缩短至几天甚至几小时。\n- **显著突破上限**：经过几轮自博弈微调，模型在 Open LLM Leaderboard 和 MT-Bench 上的得分显著提升，能够更精准地区分并模仿高质量回答，对话流畅度媲美强基座模型。\n- **盘活存量数据**：无需新数据输入，SPIN 巧妙地将原有的 SFT 数据集转化为持续的进化动力，最大化了初始数据的价值。\n\nSPIN 通过让大模型在自我对抗中不断“左右互搏”，以零额外标注成本将弱模型转化为强模型，彻底打破了高性能对齐的数据壁垒。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fuclaml_SPIN_a29416a6.png","uclaml","UCLA Artificial General Intelligence Lab","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002Fuclaml_9c96392c.png","The artificial general intelligence lab (formerly known as statistical machine learning lab) at UCLA is led by Prof. Quanquan Gu in the computer science dept.","Department of Computer Science, UCLA",null,"http:\u002F\u002Fweb.cs.ucla.edu\u002F~qgu","https:\u002F\u002Fgithub.com\u002Fuclaml",[82,86],{"name":83,"color":84,"percentage":85},"Python","#3572A5",87.8,{"name":87,"color":88,"percentage":89},"Shell","#89e051",12.2,1237,105,"2026-04-14T00:58:33","Apache-2.0","Linux","必需 NVIDIA GPU（用于安装 flash-attn 及 accelerate 多卡训练），具体显存需求未说明（建议 24GB+ 以运行 7B 模型），需支持 CUDA","未说明",{"notes":98,"python":99,"dependencies":100},"1. 必须使用 Conda 创建 Python 3.10 环境。2. 安装 flash-attn 时需添加 --no-build-isolation 参数。3. 需登录 Hugging Face 账号以下载模型和数据。4. 项目依赖 Alignment Handbook 的旧版本配置和模型检查点（特定 revision），若使用新版模型需自行生成数据或指定版本号。5. 数据生成阶段推荐使用 vLLM 加速，或将数据分块（frac_len=800）以避免崩溃。6. 训练脚本使用 accelerate 启动，支持多 GPU 并行。","3.10",[101,102,103,104,105],"flash-attn","accelerate","transformers","datasets","vllm (可选，用于加速生成)",[14,35],[108,109,110,111],"deep-learning","fine-tuning","large-language-models","self-play","2026-03-27T02:49:30.150509","2026-04-16T01:49:45.462184",[115,120,125,130,135,140],{"id":116,"question_zh":117,"answer_zh":118,"source_url":119},35047,"SPIN 的每次迭代中使用了哪些数据集组合？是否累积之前的合成数据？","是的，实验表明结合之前迭代的数据有助于在较大迭代次数时稳定模型性能，这被视为一种正则化形式，确保模型不会显著偏离前一次迭代的性能。具体来说：\n1. 迭代 0 使用 50k 样本（SPIN_iter0）。\n2. 迭代 1、2、3 等后续迭代会将新生成的合成数据与前一次迭代的数据合并，使数据集大小达到 100k。\n3. 所有迭代始终基于相同的 50k 个提示词（prompts）进行生成，因为如果使用 100k 个提示词会导致重复，所以直接复用相同的 50k 个提示词。","https:\u002F\u002Fgithub.com\u002Fuclaml\u002FSPIN\u002Fissues\u002F11",{"id":121,"question_zh":122,"answer_zh":123,"source_url":124},35048,"为什么 SPIN 使用 RMSProp 优化器和线性调度，而不是常见的 AdamW 和余弦调度？提示词模板为何与 Zephyr 不同？","1. 关于优化器：虽然用户询问了原因，但维护者主要确认了默认配置的有效性。\n2. 关于提示词模板：使用的模板并非特殊技巧，而是默认模板。为了复现论文结果，应使用提供的模板，但用户也可以尝试其他模板进行探索。\n3. 关于生成长度：最大输出长度是可调整的。设置为 256 是为了快速演示生成过程，但设置为 512 或 1024 也是完全可行的，且在实验中表现良好。\n4. 关于迭代 0 数据准备：通常使用 `alignment-handbook\u002Fzephyr-7b-sft-full` 模型在迭代 0 生成数据。若要复现 50k 数据生成，可直接使用 `UCLA-AGI\u002FSPIN_iter0` 数据集。对于其他数据，只需将其转换为相同格式即可。","https:\u002F\u002Fgithub.com\u002Fuclaml\u002FSPIN\u002Fissues\u002F9",{"id":126,"question_zh":127,"answer_zh":128,"source_url":129},35049,"SPIN 损失函数中 logit 的计算公式是什么？代码实现是否正确？","经过社区讨论和修正，正确的 SPIN 损失计算逻辑如下：\n1. 策略对数比率 (pi_logratios) = policy_real_logps - opponent_generated_logps\n2. 参考对数比率 (ref_logratios) = opponent_real_logps - policy_generated_logps\n3. 最终 logits = pi_logratios + ref_logratios\n注意：原始代码中可能存在变量对应关系的混淆，需确保 real 和 generated 部分分别与对手（opponent）和策略（policy）正确对应，且两者之间是相加关系。","https:\u002F\u002Fgithub.com\u002Fuclaml\u002FSPIN\u002Fissues\u002F41",{"id":131,"question_zh":132,"answer_zh":133,"source_url":134},35050,"训练过程中的四个奖励指标（rewards\u002Freal, rewards\u002Fgenerated, accuracies, margins）是如何变化的？","这些指标在训练过程中的变化行为可能因设置不同而有所差异。维护者提供了一个迭代 3 (iter3) 的示例图供参考。需要注意的是，这些中间指标与最终评估结果之间的联系并不明显，因此不能单纯依赖这些指标的走势来判断最终模型性能。建议关注最终的基准测试得分。","https:\u002F\u002Fgithub.com\u002Fuclaml\u002FSPIN\u002Fissues\u002F16",{"id":136,"question_zh":137,"answer_zh":138,"source_url":139},35051,"运行 vllm 生成脚本时遇到参数错误或脚本无法工作的问题如何解决？","这是一个已修复的参数命名问题：\n1. 在最新的提交中，`generate_vllm.py` 和 `generate_vllm.sh` 中的参数已统一从 `world_size` 改为 `batch_size`（与 `generate.py` 保持一致）。如果您使用的是旧版本代码，请手动将脚本中的 `world_size` 参数更改为 `batch_size`。\n2. 请确保 `generate_vllm.sh` 脚本调用的是 `generate_vllm.py` 而不是旧的 `generate.py`。\n3. 由于 vllm 更新频繁，建议在 setup 文件中指定特定的 vllm 版本，并确保安装了 `ray` 以支持多 GPU 推理。","https:\u002F\u002Fgithub.com\u002Fuclaml\u002FSPIN\u002Fissues\u002F3",{"id":141,"question_zh":142,"answer_zh":143,"source_url":144},35052,"如何在 SPIN 中使用 PEFT (LoRA) 进行微调？遇到报错怎么办？","在 `config.yaml` 中添加 `use_peft: true`, `lora_r`, `lora_alpha` 后可能会遇到错误。这是因为 `run_spin.py` 第 144 行附近的代码在处理参考模型（red_model\u002Fopponent）时未兼容 PEFT 模式。\n解决方案：\n1. 临时方案：注释掉 `run_spin.py` 中导致冲突的代码行（通常在加载参考模型部分）。\n2. 代码修改方案：在 `trainer.py` 中调用参考模型时添加检查逻辑，确保当启用 PEFT 时正确处理模型加载和调用，避免属性访问错误。","https:\u002F\u002Fgithub.com\u002Fuclaml\u002FSPIN\u002Fissues\u002F29",[]]