[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-baichuan-inc--Baichuan2":3,"tool-baichuan-inc--Baichuan2":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 真正成长为懂上",155373,2,"2026-04-14T11:34:08",[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":10,"last_commit_at":59,"category_tags":60,"status":17},4487,"LLMs-from-scratch","rasbt\u002FLLMs-from-scratch","LLMs-from-scratch 是一个基于 PyTorch 的开源教育项目，旨在引导用户从零开始一步步构建一个类似 ChatGPT 的大型语言模型（LLM）。它不仅是同名技术著作的官方代码库，更提供了一套完整的实践方案，涵盖模型开发、预训练及微调的全过程。\n\n该项目主要解决了大模型领域“黑盒化”的学习痛点。许多开发者虽能调用现成模型，却难以深入理解其内部架构与训练机制。通过亲手编写每一行核心代码，用户能够透彻掌握 Transformer 架构、注意力机制等关键原理，从而真正理解大模型是如何“思考”的。此外，项目还包含了加载大型预训练权重进行微调的代码，帮助用户将理论知识延伸至实际应用。\n\nLLMs-from-scratch 特别适合希望深入底层原理的 AI 开发者、研究人员以及计算机专业的学生。对于不满足于仅使用 API，而是渴望探究模型构建细节的技术人员而言，这是极佳的学习资源。其独特的技术亮点在于“循序渐进”的教学设计：将复杂的系统工程拆解为清晰的步骤，配合详细的图表与示例，让构建一个虽小但功能完备的大模型变得触手可及。无论你是想夯实理论基础，还是为未来研发更大规模的模型做准备",90106,"2026-04-06T11:19:32",[35,15,13,14],{"id":62,"github_repo":63,"name":64,"description_en":65,"description_zh":66,"ai_summary_zh":66,"readme_en":67,"readme_zh":68,"quickstart_zh":69,"use_case_zh":70,"hero_image_url":71,"owner_login":72,"owner_name":73,"owner_avatar_url":74,"owner_bio":75,"owner_company":76,"owner_location":76,"owner_email":77,"owner_twitter":76,"owner_website":76,"owner_url":78,"languages":79,"stars":84,"forks":85,"last_commit_at":86,"license":87,"difficulty_score":10,"env_os":75,"env_gpu":88,"env_ram":88,"env_deps":89,"category_tags":92,"github_topics":94,"view_count":32,"oss_zip_url":76,"oss_zip_packed_at":76,"status":17,"created_at":107,"updated_at":108,"faqs":109,"releases":138},7558,"baichuan-inc\u002FBaichuan2","Baichuan2","A series of large language models developed by Baichuan Intelligent Technology","Baichuan 2 是百川智能推出的新一代开源大语言模型系列，旨在提供高性能的中文及多语言智能交互能力。它基于 2.6 万亿高质量 Tokens 训练而成，有效解决了大模型在中文语境下理解不足、逻辑推理薄弱以及复杂指令跟随困难等痛点，在通用知识、法律医疗、数学代码等多个权威评测基准中表现卓越，部分指标甚至超越了同尺寸的国际主流模型。\n\n这款模型非常适合开发者、科研人员以及企业技术团队使用。研究者可以利用其开放的基座版本（Base）进行学术探索；开发者则能直接调用对话版本（Chat），快速构建智能客服、内容创作助手或行业专用应用。值得一提的是，Baichuan 2 不仅提供了 7B 和 13B 两种参数量规格以适应不同算力需求，还推出了 4bits 量化版本，显著降低了部署门槛，让资源有限的设备也能流畅运行。此外，其在数学推理和复杂任务处理上的专项优化，使其在处理专业领域问题时更加精准可靠。在合规方面，Baichuan 2 对学术研究完全开放，开发者经申请后也可免费用于商业场景，极大地促进了生态的繁荣与技术落地。","\u003C!-- markdownlint-disable first-line-h1 -->\n\u003C!-- markdownlint-disable html -->\n\n\u003Cdiv align=\"center\">\n\u003Ch1>\n  Baichuan 2\n\u003C\u002Fh1>\n\u003C\u002Fdiv>\n\n\u003Cp align=\"center\">\n🤗 \u003Ca href=\"https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002F\" target=\"_blank\">Hugging Face\u003C\u002Fa> • 🤖 \u003Ca href=\"https:\u002F\u002Fmodelscope.cn\u002Forganization\u002Fbaichuan-inc\" target=\"_blank\">ModelScope\u003C\u002Fa> • 💬 \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan-7B\u002Fblob\u002Fmain\u002Fmedia\u002Fwechat.jpeg?raw=true\" target=\"_blank\">WeChat\u003C\u002Fa>• 🧩 \u003Ca href=\"https:\u002F\u002Fmodelers.cn\u002Fspaces\u002FBaichuan\u002FBaichuan2-7B-Chat\" target=\"_blank\">Modelers\u003C\u002Fa>\n\u003C\u002Fp>\n\n\u003Cdiv align=\"center\">\n\n\u003Cdiv align=\"center\">\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbaichuan-inc_Baichuan2_readme_d923268e724c.jpeg\" width=20% \u002F>\n\u003C\u002Fdiv>\n\n🚀 百小应APP已正式上线！懂搜索、会提问的AI助手，各大应用商店搜索百小应下载，欢迎下载体验 🎉\n\n\n[![license](https:\u002F\u002Fimg.shields.io\u002Fgithub\u002Flicense\u002Fmodelscope\u002Fmodelscope.svg)](https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan2\u002Fblob\u002Fmain\u002FLICENSE)\n\n\n\u003Ch4 align=\"center\">\n    \u003Cp>\n        \u003Cb>中文\u003C\u002Fb> |\n        \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan2\u002Fblob\u002Fmain\u002FREADME_EN.md\">English\u003C\u002Fa>\n    \u003Cp>\n\u003C\u002Fh4>\n\n\u003C\u002Fdiv>\n\n# 目录\n\n- [📖 模型介绍](#模型介绍)\n- [📊 Benchmark 结果 🥇🥇🔥🔥](#Benchmark-结果)\n- [⚙️ 推理和部署](#推理和部署)\n- [🛠️ 模型微调](#模型微调)\n- [💾 中间 Checkpoints 🔥🔥](#中间-Checkpoints)\n- [👥 社区与生态](#社区与生态)\n- [📜 声明、协议、引用](#声明协议引用)\n\n# 更新\n[2023.12.29] 🎉🎉🎉 我们发布了 **[Baichuan2-13B-Chat v2](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-13B-Chat\u002Ftree\u002Fv2.0)** 版本。其中：\n- 大幅提升了模型的综合能力，特别是数学和逻辑推理、复杂指令跟随能力。\n\n# 模型介绍\n\n- Baichuan 2 是百川智能推出的**新一代开源大语言模型**，采用 **2.6 万亿**  Tokens 的高质量语料训练。\n- Baichuan 2 在多个权威的中文、英文和多语言的通用、领域 benchmark 上取得同尺寸**最佳**的效果。\n- 本次发布包含有 **7B**、**13B** 的 **Base** 和 **Chat** 版本，并提供了 Chat 版本的 **4bits 量化**。\n- 所有版本对学术研究完全开放。同时，开发者通过邮件申请并获得官方商用许可后，即可**免费商用**，请参考[协议](#协议)章节。\n- 欢迎阅读我们的技术报告 [Baichuan 2: Open Large-scale Language Models](https:\u002F\u002Farxiv.org\u002Fabs\u002F2309.10305) 获取更多信息。\n\n本次发布版本和下载链接见下表：\n|         | 基座模型  | 对齐模型 | 对齐模型 4bits 量化 |\n|:-------:|:-------:|:-------:|:-----------------:|\n| 7B      | 🤗 [Baichuan2-7B-Base](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-7B-Base) | 🤗 [Baichuan2-7B-Chat](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-7B-Chat) | 🤗 [Baichuan2-7B-Chat-4bits](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-7B-Chat-4bits) |\n| 13B     | 🤗 [Baichuan2-13B-Base](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-13B-Base) | 🤗 [Baichuan2-13B-Chat](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-13B-Chat) | 🤗 [Baichuan2-13B-Chat-4bits](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-13B-Chat-4bits) |\n\n# Benchmark 结果\n\n我们在[通用](#通用领域)、[法律](#法律医疗)、[医疗](#法律医疗)、[数学](#数学代码)、[代码](#数学代码)和[多语言翻译](#多语言翻译)六个领域的中英文和多语言权威数据集上对模型进行了广泛测试。\n\n## 通用领域\n\n在通用领域我们在以下数据集上进行了 5-shot 测试。\n- [C-Eval](https:\u002F\u002Fcevalbenchmark.com\u002Findex.html#home) 是一个全面的中文基础模型评测数据集，涵盖了 52 个学科和四个难度的级别。我们使用该数据集的 dev 集作为 few-shot 的来源，在 test 集上进行测试。我们采用了 [Baichuan-7B](https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan-7B\u002Ftree\u002Fmain) 的评测方案。\n- [MMLU](https:\u002F\u002Farxiv.org\u002Fabs\u002F2009.03300) 是包含 57 个任务的英文评测数据集，涵盖了初等数学、美国历史、计算机科学、法律等，难度覆盖高中水平到专家水平，是目前主流的 LLM 评测数据集。我们采用了[开源](https:\u002F\u002Fgithub.com\u002Fhendrycks\u002Ftest)的评测方案。\n- [CMMLU](https:\u002F\u002Fgithub.com\u002Fhaonan-li\u002FCMMLU) 是一个包含 67 个主题的综合性性中文评估基准，专门用于评估语言模型在中文语境下的知识和推理能力。我们采用了其[官方](https:\u002F\u002Fgithub.com\u002Fhaonan-li\u002FCMMLU)的评测方案。\n- [Gaokao](https:\u002F\u002Fgithub.com\u002FOpenLMLab\u002FGAOKAO-Bench) 是一个以中国高考题作为评测大语言模型能力的数据集，用以评估模型的语言能力和逻辑推理能力。 我们只保留了其中的单项选择题，并进行了随机划分。我们采用了与 C-Eval 类似的评测方案。\n- [AGIEval](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002FAGIEval) 旨在评估模型的认知和解决问题相关的任务中的一般能力。 我们只保留了其中的四选一单项选择题，并进行了随机划分。我们采用了与 C-Eval 类似的评测方案。\n- [BBH](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002Flukaemon\u002Fbbh) 是一个挑战性任务 Big-Bench 的子集。Big-Bench 目前包括 204 项任务。任务主题涉及语言学、儿童发展、数学、常识推理、生物学、物理学、社会偏见、软件开发等方面。BBH 是从 204 项 Big-Bench 评测基准任务中大模型表现不好的任务单独拿出来形成的评测基准。\n\n### 7B 模型结果\n\n|                       | **C-Eval** | **MMLU** | **CMMLU** | **Gaokao** | **AGIEval** | **BBH** |\n|:---------------------:|:----------:|:--------:|:---------:|:----------:|:-----------:|:-------:|\n|                       |  5-shot    |  5-shot  |  5-shot   | 5-shot     | 5-shot      | 3-shot  |\n| **GPT-4**             | 68.40      | 83.93    | 70.33     | 66.15      | 63.27       | 75.12   |\n| **GPT-3.5 Turbo**     | 51.10      | 68.54    | 54.06     | 47.07      | 46.13       | 61.59   |\n| **LLaMA-7B**          | 27.10      | 35.10    | 26.75     | 27.81      | 28.17       | 32.38   |\n| **LLaMA2-7B**         | 28.90      | 45.73    | 31.38     | 25.97      | 26.53       | 39.16   |\n| **MPT-7B**            | 27.15      | 27.93    | 26.00     | 26.54      | 24.83       | 35.20   |\n| **Falcon-7B**         | 24.23      | 26.03    | 25.66     | 24.24      | 24.10       | 28.77   |\n| **ChatGLM2-6B**       | 50.20      | 45.90    | 49.00     | 49.44      | 45.28       | 31.65   |\n| **Baichuan-7B**       | 42.80      | 42.30    | 44.02     | 36.34      | 34.44       | 32.48   |\n| **Baichuan2-7B-Base** | 54.00      | 54.16    | 57.07     | 47.47      | 42.73       | 41.56   |\n\n### 13B 模型结果\n\n|                             | **C-Eval** | **MMLU** | **CMMLU** | **Gaokao** | **AGIEval** | **BBH** |\n|:---------------------------:|:----------:|:--------:|:---------:|:----------:|:-----------:|:-------:|\n|                             |  5-shot    |  5-shot  |  5-shot   | 5-shot     | 5-shot      | 3-shot  |\n| **GPT-4**                   | 68.40      | 83.93    | 70.33     | 66.15      | 63.27       | 75.12   |\n| **GPT-3.5 Turbo**           | 51.10      | 68.54    | 54.06     | 47.07      | 46.13       | 61.59   |\n| **LLaMA-13B**               | 28.50      | 46.30    | 31.15     | 28.23      | 28.22       | 37.89   |\n| **LLaMA2-13B**              | 35.80      | 55.09    | 37.99     | 30.83      | 32.29       | 46.98   |\n| **Vicuna-13B**              | 32.80      | 52.00    | 36.28     | 30.11      | 31.55       | 43.04   |\n| **Chinese-Alpaca-Plus-13B** | 38.80      | 43.90    | 33.43     | 34.78      | 35.46       | 28.94   |\n| **XVERSE-13B**              | 53.70      | 55.21    | 58.44     | 44.69      | 42.54       | 38.06   |\n| **Baichuan-13B-Base**       | 52.40      | 51.60    | 55.30     | 49.69      | 43.20       | 43.01   |\n| **Baichuan2-13B-Base**      | 58.10      | 59.17    | 61.97     | 54.33      | 48.17       | 48.78   |\n\n## 法律、医疗\n\n法律领域我们使用了 [JEC-QA](https:\u002F\u002Fjecqa.thunlp.org\u002F) 数据集。JEC-QA 数据集来源于中国国家司法考试。我们只保留了其中的单选题。我们采用了与 C-Eval 类似的评测方案。\n\n医疗领域则使用通用领域数据集（C-Eval、MMLU、CMMLU）中的医学相关学科、[MedQA](https:\u002F\u002Farxiv.org\u002Fabs\u002F2009.13081) 和 [MedMCQA](https:\u002F\u002Fmedmcqa.github.io\u002F)。我们采用了与 C-Eval 类似的评测方案。\n- 为了测试方便，我们使用了 C-Eval 的 val 集进行测试。\n- MedQA 数据集来源于美国、中国的医学考试。我们测试了 [MedQA数据集](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002Fbigbio\u002Fmed_qa) 中的 USMLE 和 MCMLE 两个子集，并采用了五个候选的版本。\n- MedMCQA 数据集来源于印度医学院的入学考试。我们只保留了其中的单选题。由于 test 集没有答案，我们使用 dev 集进行测试。\n- 通用领域数据集包含的医学相关学科如下：\n    - C-Eval: clinical_medicine, basic_medicine\n    - MMLU: clinical_knowledge, anatomy, college_medicine, college_biology, nutrition, virology, medical_genetics, professional_medicine\n    - CMMLU: anatomy, clinical_knowledge, college_medicine, genetics, nutrition, traditional_chinese_medicine, virology \n\n我们对以上数据集进行了 5-shot 测试。\n\n### 7B 模型结果\n\n|                       | **JEC-QA** | **CEval-MMLU-CMMLU** | **MedQA-USMLE** | **MedQA-MCMLE** | **MedMCQA** |\n|:---------------------:|:----------:|:--------------------:|:---------------:|:---------------:|:-----------:|\n|                       | 5-shot     | 5-shot               | 5-shot          | 5-shot          | 5-shot      |\n| **GPT-4**             | 59.32      | 77.16                | 80.28           | 74.58           | 72.51       |\n| **GPT-3.5 Turbo**     | 42.31      | 61.17                | 53.81           | 52.92           | 56.25       |\n| **LLaMA-7B**          | 27.45      | 33.34                | 24.12           | 21.72           | 27.45       |\n| **LLaMA2-7B**         | 29.20      | 36.75                | 27.49           | 24.78           | 37.93       |\n| **MPT-7B**            | 27.45      | 26.67                | 16.97           | 19.79           | 31.96       |\n| **Falcon-7B**         | 23.66      | 25.33                | 21.29           | 18.07           | 33.88       |\n| **ChatGLM2-6B**       | 40.76      | 44.54                | 26.24           | 45.53           | 30.22       |\n| **Baichuan-7B**       | 34.64      | 42.37                | 27.42           | 39.46           | 31.39       |\n| **Baichuan2-7B-Base** | 44.46      | 56.39                | 32.68           | 54.93           | 41.73       |\n\n### 13B 模型结果\n\n|                             | **JEC-QA** | **CEval-MMLU-CMMLU** | **MedQA-USMLE** | **MedQA-MCMLE** | **MedMCQA** |\n|:---------------------------:|:----------:|:--------------------:|:---------------:|:---------------:|:-----------:|\n|                             | 5-shot     |  5-shot              |  5-shot         |  5-shot         | 5-shot      |\n| **GPT-4**                   | 59.32      | 77.16                | 80.28           | 74.58           | 72.51       |\n| **GPT-3.5 Turbo**           | 42.31      | 61.17                | 53.81           | 52.92           | 56.25       |\n| **LLaMA-13B**               | 27.54      | 35.14                | 28.83           | 23.38           | 39.52       |\n| **LLaMA2-13B**              | 34.08      | 47.42                | 35.04           | 29.74           | 42.12       |\n| **Vicuna-13B**              | 28.38      | 40.99                | 34.80           | 27.67           | 40.66       |\n| **Chinese-Alpaca-Plus-13B** | 35.32      | 46.31                | 27.49           | 32.66           | 35.87       |\n| **XVERSE-13B**              | 46.42      | 58.08                | 32.99           | 58.76           | 41.34       |\n| **Baichuan-13B-Base**       | 41.34      | 51.77                | 29.07           | 43.67           | 39.60       |\n| **Baichuan2-13B-Base**      | 47.40      | 59.33                | 40.38           | 61.62           | 42.86       |\n\n## 数学、代码\n\n数学领域我们使用 [OpenCompass](https:\u002F\u002Fopencompass.org.cn\u002F) 评估框架，对 [GSM8K](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002Fgsm8k) 和 [MATH](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002Fcompetition_math) 数据集进行了 4-shot 测试。\n\n- GSM8K 是由 OpenAI 发布的一个由 8.5K 高质量的语言多样化的小学数学应用题组成的数据集，要求根据给定的场景和两个可能的解决方案，选择最合理的方案。\n- MATH 数据集包含 12,500 个数学问题（其中 7500 个属于训练集，5000 个属于测试集），这些问题收集自 AMC 10、AMC 12、AIME 等数学竞赛。\n\n代码领域则采用了 [HumanEval](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002Fopenai_humaneval) 和 [MBPP](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002Fmbpp) 数据集。我们使用 OpenCompass，对 HumanEval 进行了 0-shot 测试，MBPP 数据集进行了 3-shot 测试。\n- HumanEval 中的编程任务包括模型语言理解、推理、算法和简单数学，以评估模型功能正确性，并衡量模型的问题解决能力。\n- MBPP 包括 974 个 Python 短函数、程序的文字描述以及用于检查功能正确性的测试用例的数据集。\n\n### 7B 模型结果\n\n|                       | **GSM8K** | **MATH** | **HumanEval** | **MBPP** |\n|:---------------------:|:---------:|:--------:|:-------------:|:--------:|\n|                       |  4-shot   | 4-shot   |  0-shot       |  3-shot  |\n| **GPT-4**             |   89.99   | 40.20    | 69.51         |  63.60   |\n| **GPT-3.5 Turbo**     |   57.77   | 13.96    | 52.44         |  61.40   |\n| **LLaMA-7B**          |   9.78    | 3.02     | 11.59         |  14.00   |\n| **LLaMA2-7B**         |   16.22   | 3.24     | 12.80         |  14.80   |\n| **MPT-7B**            |   8.64    | 2.90     | 14.02         |  23.40   |\n| **Falcon-7B**         |   5.46    | 1.68     | -             |  10.20   |\n| **ChatGLM2-6B**       |   28.89   | 6.40     | 9.15          |   9.00   |\n| **Baichuan-7B**       |   9.17    | 2.54     | 9.20          |   6.60   |\n| **Baichuan2-7B-Base** |   24.49   | 5.58     | 18.29         |  24.20   |\n\n### 13B 模型结果\n\n|                             | **GSM8K** | **MATH** | **HumanEval** | **MBPP** |\n|:---------------------------:|:---------:|:--------:|:-------------:|:--------:|\n|                             |  4-shot   | 4-shot   |  0-shot       |  3-shot  |\n| **GPT-4**                   |   89.99   | 40.20    | 69.51         |  63.60   |\n| **GPT-3.5 Turbo**           |   57.77   | 13.96    | 52.44         |  61.40   |\n| **LLaMA-13B**               |   20.55   | 3.68     | 15.24         |  21.40   |\n| **LLaMA2-13B**              |   28.89   | 4.96     | 15.24         |  27.00   |\n| **Vicuna-13B**              |   28.13   | 4.36     | 16.46         |  15.00   |\n| **Chinese-Alpaca-Plus-13B** |   11.98   | 2.50     | 16.46         |  20.00   |\n| **XVERSE-13B**              |   18.20   | 2.18     | 15.85         |  16.80   |\n| **Baichuan-13B-Base**       |   26.76   | 4.84     | 11.59         |  22.80   |\n| **Baichuan2-13B-Base**      |   52.77   | 10.08    | 17.07         |  30.20   |\n\n## 多语言翻译\n\n我们采用了 [Flores-101](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002Ffacebook\u002Fflores) 数据集来评估模型的多语言能力。Flores-101 涵盖了世界各地的 101 种语言。它的数据来源于新闻、旅游指南和书籍等多个不同领域。我们选择了联合国官方语言（阿拉伯文、中文、英文、法文、俄文和西班牙文）以及德文和日文作为测试语种。我们使用 OpenCompass 对 Flores-101 中的中-英、中-法、中-西班牙、中-阿拉伯、中-俄、中-日、中-德等七个子任务分别进行了 8-shot 测试。\n\n### 7B 模型结果\n\n|             | **CN-EN** | **CN-FR** | **CN-ES** | **CN-AR** | **CN-RU** | **CN-JP** | **CN-DE** | Average |\n|:---------------------:|:-------:|:-------:|:---------:|:---------:|:-------:|:-------:|:-------:|:-------:|\n| **GPT-4**             | 29.94   | 29.56   | 20.01     | 10.76     | 18.62   | 13.26   | 20.83   | 20.43   |\n| **GPT-3.5 Turbo**     | 27.67   | 26.15   | 19.58     | 10.73     | 17.45   | 1.82    | 19.70   | 17.59   |\n| **LLaMA-7B**          | 17.27   | 12.02   | 9.54      | 0.00      | 4.47    | 1.41    | 8.73    | 7.63    |\n| **LLaMA2-7B**         | 25.76   | 15.14   | 11.92     | 0.79      | 4.99    | 2.20    | 10.15   | 10.14   |\n| **MPT-7B**            | 20.77   | 9.53    | 8.96      | 0.10      | 3.54    | 2.91    | 6.54    | 7.48    |\n| **Falcon-7B**         | 22.13   | 15.67   | 9.28      | 0.11      | 1.35    | 0.41    | 6.41    | 7.91    |\n| **ChatGLM2-6B**       | 22.28   | 9.42    | 7.77      | 0.64      | 1.78    | 0.26    | 4.61    | 6.68    |\n| **Baichuan-7B**       | 25.07   | 16.51   | 12.72     | 0.41      | 6.66    | 2.24    | 9.86    | 10.50   |\n| **Baichuan2-7B-Base** | 27.27   | 20.87   | 16.17     | 1.39      | 11.21   | 3.11    | 12.76   | 13.25   |\n\n### 13B 模型结果\n\n|                   | **CN-EN** | **CN-FR** | **CN-ES** | **CN-AR** | **CN-RU** | **CN-JP** | **CN-DE** | Average |\n|:---------------------------:|:-------:|:-------:|:---------:|:---------:|:-------:|:-------:|:-------:|:-------:|\n|          **GPT-4**          | 29.94   | 29.56   | 20.01     | 10.76     | 18.62   | 13.26   | 20.83   | 20.43   |\n|      **GPT-3.5 Turbo**      | 27.67   | 26.15   | 19.58     | 10.73     | 17.45   | 1.82    | 19.70   | 17.59   |\n|        **LLaMA-13B**        | 21.75   | 16.16   | 13.29     | 0.58      | 7.61    | 0.41    | 10.66   | 10.07   |\n|       **LLaMA2-13B**        | 25.44   | 19.25   | 17.49     | 1.38      | 10.34   | 0.13    | 11.13   | 12.17   |\n|       **Vicuna-13B**        | 22.63   | 18.04   | 14.67     | 0.70      | 9.27    | 3.59    | 10.25   | 11.31   |\n| **Chinese-Alpaca-Plus-13B** | 22.53   | 13.82   | 11.29     | 0.28      | 1.52    | 0.31    | 8.13    | 8.27    |\n|       **XVERSE-13B**        | 29.26   | 24.03   | 16.67     | 2.78      | 11.61   | 3.08    | 14.26   | 14.53   |\n|    **Baichuan-13B-Base**    | 30.24   | 20.90   | 15.92     | 0.98      | 9.65    | 2.64    | 12.00   | 13.19   |\n|    **Baichuan2-13B-Base**   | 30.61   | 22.11   | 17.27     | 2.39      | 14.17   | 11.58   | 14.53   | 16.09   |\n\n# 推理和部署\n\n推理所需的模型权重、源码、配置已发布在 Hugging Face，下载链接见本文档最开始的表格。我们在此示范多种推理方式。程序会自动从 Hugging Face 下载所需资源。\n\n## 安装依赖\n```shell\npip install -r requirements.txt\n```\n\n## Python 代码方式\n\n### Chat 模型推理方法示范\n```python\n>>> import torch\n>>> from transformers import AutoModelForCausalLM, AutoTokenizer\n>>> from transformers.generation.utils import GenerationConfig\n>>> tokenizer = AutoTokenizer.from_pretrained(\"baichuan-inc\u002FBaichuan2-13B-Chat\", use_fast=False, trust_remote_code=True)\n>>> model = AutoModelForCausalLM.from_pretrained(\"baichuan-inc\u002FBaichuan2-13B-Chat\", device_map=\"auto\", torch_dtype=torch.bfloat16, trust_remote_code=True)\n>>> model.generation_config = GenerationConfig.from_pretrained(\"baichuan-inc\u002FBaichuan2-13B-Chat\")\n>>> messages = []\n>>> messages.append({\"role\": \"user\", \"content\": \"解释一下“温故而知新”\"})\n>>> response = model.chat(tokenizer, messages)\n>>> print(response)\n\"温故而知新\"是一句中国古代的成语，出自《论语·为政》篇。这句话的意思是：通过回顾过去，我们可以发现新的知识和理解。换句话说，学习历史和经验可以让我们更好地理解现在和未来。\n\n这句话鼓励我们在学习和生活中不断地回顾和反思过去的经验，从而获得新的启示和成长。通过重温旧的知识和经历，我们可以发现新的观点和理解，从而更好地应对不断变化的世界和挑战。\n```\n\n### Base 模型推理方法示范\n```python\n>>> from transformers import AutoModelForCausalLM, AutoTokenizer\n>>> tokenizer = AutoTokenizer.from_pretrained(\"baichuan-inc\u002FBaichuan2-13B-Base\", trust_remote_code=True)\n>>> model = AutoModelForCausalLM.from_pretrained(\"baichuan-inc\u002FBaichuan2-13B-Base\", device_map=\"auto\", trust_remote_code=True)\n>>> inputs = tokenizer('登鹳雀楼->王之涣\\n夜雨寄北->', return_tensors='pt')\n>>> inputs = inputs.to('cuda:0')\n>>> pred = model.generate(**inputs, max_new_tokens=64, repetition_penalty=1.1)\n>>> print(tokenizer.decode(pred.cpu()[0], skip_special_tokens=True))\n登鹳雀楼->王之涣\n夜雨寄北->李商隐\n```\n\n> 在上述两段代码中，模型加载指定 `device_map='auto'`，会使用所有可用显卡。如需指定使用的设备，可以使用类似 `export CUDA_VISIBLE_DEVICES=0,1`（使用了0、1号显卡）的方式控制。\n\n## 命令行工具方式\n\n```shell\npython cli_demo.py\n```\n本命令行工具是为 Chat 场景设计，因此我们不支持使用该工具调用 Base 模型。\n\n## 网页 demo 方式\n\n依靠 streamlit 运行以下命令，会在本地启动一个 web 服务，把控制台给出的地址放入浏览器即可访问。本网页 demo 工具是为 Chat 场景设计，因此我们不支持使用该工具调用 Base 模型。\n\n```shell\nstreamlit run web_demo.py\n```\n\n## Baichuan2-13B-Chat 示例输出\n\n\u003Cdetails>\u003Csummary>\u003Cb>代码\u003C\u002Fb>\u003C\u002Fsummary>\n\n`````\n用户：找到下列数组的中位数[3.1,6.2,1.3,8.4,10.5,11.6,2.1]，请用python代码完成以上功能\n\nBaichuan 2：def find_median(numbers):\n    numbers.sort()\n    length = len(numbers)\n    if length % 2 == 0:\n        return (numbers[length \u002F\u002F 2 - 1] + numbers[length \u002F\u002F 2]) \u002F 2\n    else:\n        return numbers[length \u002F\u002F 2]\nnumbers = [3.1, 6.2, 1.3, 8.4, 10.5, 11.6, 2.1]\nprint(find_median(numbers))\n`````\n\n\u003C\u002Fdetails>\n\n\u003Cdetails>\u003Csummary>\u003Cb>数学\u003C\u002Fb>\u003C\u002Fsummary>\n\n`````\n用户：鸡和兔在一个笼子里，共有26个头，68只脚，那么鸡有多少只，兔有多少只？\n\nBaichuan 2：我们可以用代数方法来解决这个问题。假设鸡的数量为x，兔子的数量为y。根据题目给出的信息，我们可以得到以下两个方程：\n\n1. x + y = 26 (头的总数)\n2. 2x + 4y = 68 (脚的总数)\n\n现在我们需要解这个二元一次方程组。首先从第一个方程中解出x：\n\nx = 26 - y\n\n然后将x的表达式代入第二个方程：\n\n2(26 - y) + 4y = 68\n52 - 2y + 4y = 68\n2y = 16\n\n接下来我们求解y的值：\n\ny = 16 \u002F 2\ny = 8\n\n现在我们得到了兔子的数量是8只。将y的值代入x的表达式求得鸡的数量：\n\nx = 26 - 8\nx = 18\n\n所以鸡有18只，兔子有8只。\n`````\n\n\u003C\u002Fdetails>\n\n\u003Cdetails>\u003Csummary>\u003Cb>指令跟随\u003C\u002Fb>\u003C\u002Fsummary>\n\n`````\n用户：从现在开始，请你将我接下来说的每一句话翻译成英语，准备好了吗？\n\nBaichuan 2：I'm ready to translate every word you say next into English.\n\n用户：你好\n\nBaichuan 2：Hello\n\n用户：我是Baichuan 2，很高兴认识大家\n\nBaichuan 2：I am Baichuan 2, and I am very happy to meet everyone.\n\n用户：希望通过大家的共同努力，早日迎接AGI时代的到来\n\nBaichuan 2：I hope through the joint efforts of all of us, we can welcome the arrival of the AGI era as soon as possible.\n`````\n\u003C\u002Fdetails>\n\n\u003Cdetails>\u003Csummary>\u003Cb>解题\u003C\u002Fb>\u003C\u002Fsummary>\n\n`````\n用户：以下物理常识题目，哪一个是错误的?A.在自然环境下，声音在固体中传播速度最快。B.牛顿第一定律:一个物体如果不受力作用，将保持静止或匀速直线运动的状态。C.牛顿第三定律:对于每个作用力，都有一个相等而反向的反作用力。D.声音在空气中的传播速度为1000m\u002Fs。\n\nBaichuan 2：本题考查物理基本概念和原理的理解和应用能力。\nA项：声音的传播需要介质，在固体、液体和气体的三种介质中，声音在固体中的传播速度是最快的，故A正确； B项：牛顿第一定律是惯性定律，其内容是：一个物体如果没有受到外力的作用，要么保持静止状态，要么保持匀速直线运动状态，直到有外力迫使它改变运动状态为止，故B正确； C项：牛顿第三定律的内容是：对于每一个作用力，都有一个大小相等、方向相反的反作用力，这两个力分别作用在两个相互作用的物体上，故C错误； D项：声音在空气中以波的形式传播，其传播速度与介质的温度、湿度等因素有关，一般情况下，声音在空气中的传播速度约为340m\u002Fs左右，而不是1000m\u002Fs，故D错误。 因此，本题的正确答案是 D 。\n`````\n`````\n用户：I'm Mike 。I am going to have a busy weekend。On Saturday，I am going to learn how to swim。I will go with my father。Then we are going to have lunch in the restaurant。In the afternoon，we are going to the zoo。We are going to see the pandas。On Sunday，I will finish my homework in the morning。In the afternoon，my parents and I are going to visit my grandparents。We will have dinner together。\n阅读短文，判断正确和错误！正确请写T，错误请写F:\n1.Mike is going to have a busy weekend()\n2.Mike is going to learn how to swim with his father()\n3.They are going to have lunch at home()\n\nBaichuan 2：1. T（正确的）\n2. T（正确的）\n3. F（错误的）\n`````\n\n\u003C\u002Fdetails>\n\n## 量化部署\n\n为了让不同的用户以及不同的平台都能运行 Baichuan 2 模型，我们针对 Baichuan 2 模型做了相应地量化工作（包括 Baichuan2-7B-Chat 和 Baichuan2-13B-Chat），方便用户快速高效地在自己的平台部署 Baichuan 2 模型。\n\n### 量化方法\n\nBaichuan 2 的采用社区主流的量化方法：[BitsAndBytes](https:\u002F\u002Fgithub.com\u002FTimDettmers\u002Fbitsandbytes)。该方法可以保证量化后的效果基本不掉点，目前已经集成到 transformers 库里，并在社区得到了广泛应用。BitsAndBytes 支持 8bits 和 4bits 两种量化，其中 4bits 支持 FP4 和 NF4 两种格式，Baichuan 2 选用 NF4 作为 4bits 量化的数据类型。  \n  \n基于该量化方法，Baichuan 2 支持在线量化和离线量化两种模式。\n\n### 在线量化\n\n对于在线量化，我们支持 8bits 和 4bits 量化，使用方式和 [Baichuan-13B](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan-13B-Chat) 项目中的方式类似，只需要先加载模型到 CPU 的内存里，再调用`quantize()`接口量化，最后调用 `cuda()`函数，将量化后的权重拷贝到 GPU 显存中。实现整个模型加载的代码非常简单，我们以 Baichuan2-7B-Chat 为例：\n\n8bits 在线量化:\n```python\nmodel = AutoModelForCausalLM.from_pretrained(\"baichuan-inc\u002FBaichuan2-7B-Chat\", torch_dtype=torch.float16, trust_remote_code=True)\nmodel = model.quantize(8).cuda() \n```\n4bits 在线量化:\n```python\nmodel = AutoModelForCausalLM.from_pretrained(\"baichuan-inc\u002FBaichuan2-7B-Chat\", torch_dtype=torch.float16, trust_remote_code=True)\nmodel = model.quantize(4).cuda() \n```\n需要注意的是，在用 `from_pretrained` 接口的时候，用户一般会加上 `device_map=\"auto\"`，在使用在线量化时，需要去掉这个参数，否则会报错。\n\n### 离线量化\n\n为了方便用户的使用，我们提供了离线量化好的 4bits 的版本 [Baichuan2-7B-Chat-4bits](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-7B-Chat-4bits\u002Ftree\u002Fmain)，供用户下载。\n用户加载 Baichuan2-7B-Chat-4bits 模型很简单，只需要执行:\n```python\nmodel = AutoModelForCausalLM.from_pretrained(\"baichuan-inc\u002FBaichuan2-7B-Chat-4bits\", device_map=\"auto\", trust_remote_code=True)\n```\n对于 8bits 离线量化，我们没有提供相应的版本，因为 Hugging Face transformers 库提供了相应的 API 接口，可以很方便的实现 8bits 量化模型的保存和加载。用户可以自行按照如下方式实现 8bits 的模型保存和加载：\n```python\n# Model saving: model_id is the original model directory, and quant8_saved_dir is the directory where the 8bits quantized model is saved.\nmodel = AutoModelForCausalLM.from_pretrained(model_id, load_in_8bit=True, device_map=\"auto\", trust_remote_code=True)\nmodel.save_pretrained(quant8_saved_dir)\nmodel = AutoModelForCausalLM.from_pretrained(quant8_saved_dir, device_map=\"auto\", trust_remote_code=True)\n```\n\n### 量化效果\n\n量化前后显存占用对比 (GPU Mem in GB)：\n| Precision   | Baichuan2-7B |Baichuan2-13B |\n|-------------|:------------:|:------------:|\n| bf16 \u002F fp16 | 15.3         | 27.5         |\n| 8bits       | 8.0          | 16.1         |\n| 4bits       | 5.1          | 8.6          |\n\n量化后在各个 benchmark 上的结果和原始版本对比如下：\n\n| Model 5-shot           | C-Eval | MMLU | CMMLU |\n|------------------------|:------:|:----:|:-----:|\n| Baichuan2-13B-Chat      | 56.74  | 57.32| 59.68  |\n| Baichuan2-13B-Chat-4bits | 56.05   | 56.24 | 58.82  |\n| Baichuan2-7B-Chat       | 54.35   | 52.93 | 54.99  |\n| Baichuan2-7B-Chat-4bits | 53.04   | 51.72 | 52.84  |\n> C-Eval 是在其 val set 上进行的评测\n\n可以看到，4bits 相对 bfloat16 精度损失在 1 - 2 个百分点左右。\n\n## CPU 部署\n\nBaichuan 2 模型支持 CPU 推理，但需要强调的是，CPU 的推理速度相对较慢。需按如下方式修改模型加载的方式：\n```python\n# Taking Baichuan2-7B-Chat as an example\nmodel = AutoModelForCausalLM.from_pretrained(\"baichuan-inc\u002FBaichuan2-7B-Chat\", torch_dtype=torch.float32, trust_remote_code=True)\n```\n## 对 Baichuan 1 的推理优化迁移到 Baichuan 2\n\n由于很多用户在 Baichuan 1 (Baichuan-7B, Baichuan-13B)上做了很多优化的工作，例如编译优化、量化等，为了将这些工作零成本地应用于 Baichuan 2，用户可以对 Baichuan 2 模型做一个离线转换，转换后就可以当做 Baichuan 1 模型来使用。具体来说，用户只需要利用以下脚本离线对 Baichuan 2 模型的最后一层 lm_head 做归一化，并替换掉`lm_head.weight`即可。替换完后，就可以像对 Baichuan 1 模型一样对转换后的模型做编译优化等工作了。\n```python\nimport torch\nimport os\nori_model_dir = 'your Baichuan 2 model directory'\n# To avoid overwriting the original model, it's best to save the converted model to another directory before replacing it\nnew_model_dir = 'your normalized lm_head weight Baichuan 2 model directory'\nmodel = torch.load(os.path.join(ori_model_dir, 'pytorch_model.bin'))\nlm_head_w = model['lm_head.weight']\nlm_head_w = torch.nn.functional.normalize(lm_head_w)\nmodel['lm_head.weight'] = lm_head_w\ntorch.save(model, os.path.join(new_model_dir, 'pytorch_model.bin'))\n```\n\n# 模型微调\n\n## 依赖安装\n\n```shell\ngit clone https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan2.git\ncd Baichuan2\u002Ffine-tune\npip install -r requirements.txt\n```\n- 如需使用 LoRA 等轻量级微调方法需额外安装 [peft](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Fpeft)\n- 如需使用 xFormers 进行训练加速需额外安装 [xFormers](https:\u002F\u002Fgithub.com\u002Ffacebookresearch\u002Fxformers)\n\n## 单机训练\n\n下面我们给一个微调 Baichuan2-7B-Base 的单机训练例子。\n\n训练数据：`data\u002Fbelle_chat_ramdon_10k.json`，该样例数据是从 [multiturn_chat_0.8M](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FBelleGroup\u002Fmultiturn_chat_0.8M) 采样出 1 万条，并且做了格式转换。主要是展示多轮数据怎么训练，不保证效果。\n\n```shell\nhostfile=\"\"\ndeepspeed --hostfile=$hostfile fine-tune.py  \\\n    --report_to \"none\" \\\n    --data_path \"data\u002Fbelle_chat_ramdon_10k.json\" \\\n    --model_name_or_path \"baichuan-inc\u002FBaichuan2-7B-Base\" \\\n    --output_dir \"output\" \\\n    --model_max_length 512 \\\n    --num_train_epochs 4 \\\n    --per_device_train_batch_size 16 \\\n    --gradient_accumulation_steps 1 \\\n    --save_strategy epoch \\\n    --learning_rate 2e-5 \\\n    --lr_scheduler_type constant \\\n    --adam_beta1 0.9 \\\n    --adam_beta2 0.98 \\\n    --adam_epsilon 1e-8 \\\n    --max_grad_norm 1.0 \\\n    --weight_decay 1e-4 \\\n    --warmup_ratio 0.0 \\\n    --logging_steps 1 \\\n    --gradient_checkpointing True \\\n    --deepspeed ds_config.json \\\n    --bf16 True \\\n    --tf32 True\n```\n\n## 多机训练\n\n多机训练只需要给一下 hostfile ，内容类似如下：\n```\nip1 slots=8\nip2 slots=8\nip3 slots=8\nip4 slots=8\n....\n```\n同时在训练脚本里面指定 hosftfile 的路径：\n```shell\nhostfile=\"\u002Fpath\u002Fto\u002Fhostfile\"\ndeepspeed --hostfile=$hostfile fine-tune.py  \\\n    --report_to \"none\" \\\n    --data_path \"data\u002Fbelle_chat_ramdon_10k.json\" \\\n    --model_name_or_path \"baichuan-inc\u002FBaichuan2-7B-Base\" \\\n    --output_dir \"output\" \\\n    --model_max_length 512 \\\n    --num_train_epochs 4 \\\n    --per_device_train_batch_size 16 \\\n    --gradient_accumulation_steps 1 \\\n    --save_strategy epoch \\\n    --learning_rate 2e-5 \\\n    --lr_scheduler_type constant \\\n    --adam_beta1 0.9 \\\n    --adam_beta2 0.98 \\\n    --adam_epsilon 1e-8 \\\n    --max_grad_norm 1.0 \\\n    --weight_decay 1e-4 \\\n    --warmup_ratio 0.0 \\\n    --logging_steps 1 \\\n    --gradient_checkpointing True \\\n    --deepspeed ds_config.json \\\n    --bf16 True \\\n    --tf32 True\n```\n\n## 轻量化微调\n\n代码已经支持轻量化微调如 LoRA，如需使用仅需在上面的脚本中加入以下参数：\n```shell\n--use_lora True\n```\nLoRA 具体的配置可见 `fine-tune.py` 脚本。\n\n使用 LoRA 微调后可以使用下面的命令加载模型：\n```python\nfrom peft import AutoPeftModelForCausalLM\nmodel = AutoPeftModelForCausalLM.from_pretrained(\"output\", trust_remote_code=True)\n```\n\n# 中间 Checkpoints\n\n除了训练了 2.6 万亿 Tokens 的 Baichuan2-7B-Base 模型，我们还提供了在此之前的另外 11 个中间 checkpoints（分别对应训练了约 0.2 ~ 2.4 万亿 Tokens）供社区研究使用（[下载地址](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-7B-Intermediate-Checkpoints)）。下图给出了这些 checkpoints 在 C-Eval、MMLU、CMMLU 三个 benchmark 上的效果变化：\n\n\u003Cdiv align=\"center\">\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbaichuan-inc_Baichuan2_readme_a923a43d9e16.jpeg\" width=50% \u002F>\n\u003C\u002Fdiv>\n\n# 社区与生态\n\n**📢📢📢 我们会在此持续更新社区和生态对 Baichuan 2 的支持 😀😀😀**\n\n## Intel 酷睿 Ultra 平台运行百川大模型\n\n### 使用方式\n\n使用酷睿™\u002F至强® 可扩展处理器或配合锐炫™ GPU等进行部署BaiChuan2 - 7B\u002FChat，BaiChuan2 - 13B\u002FChat模型\n\n推荐使用 BigDL-LLM（[CPU](https:\u002F\u002Fgithub.com\u002Fintel-analytics\u002FBigDL\u002Ftree\u002Fmain\u002Fpython\u002Fllm\u002Fexample\u002FCPU\u002FHF-Transformers-AutoModels\u002FModel\u002Fbaichuan2), [GPU](https:\u002F\u002Fgithub.com\u002Fintel-analytics\u002FBigDL\u002Ftree\u002Fmain\u002Fpython\u002Fllm\u002Fexample\u002FGPU\u002FHF-Transformers-AutoModels\u002FModel\u002Fbaichuan2)）以发挥更好推理性能。\n\n### 帮助文档\n\n[中文操作手册](https:\u002F\u002Fgithub.com\u002Fintel-analytics\u002Fbigdl-llm-tutorial\u002Ftree\u002Fmain\u002FChinese_Version)，包括用notebook支持\n\n[加载，优化，保存方法等](https:\u002F\u002Fgithub.com\u002Fintel-analytics\u002Fbigdl-llm-tutorial\u002Fblob\u002Fmain\u002FChinese_Version\u002Fch_3_AppDev_Basic\u002F3_BasicApp.ipynb)\n\n## 华为昇腾\n\n### Pytorch 框架\n\n模型微调：Baichuan 2 （7B）已原生支持基于昇腾 NPU 的 PyTorch（2.1.0）+ Transformers（4.36.0）+ DeepSpeed（0.12.4）+ Accelerate（0.25.0）模型微调，无需额外适配即可使用。\n\n推理部署：Baichuan 2 （7B）已原生支持昇腾 NPU 推理，无需额外适配即可使用。\n\n### MindSpore 框架\n\n[MindFormers]( https:\u002F\u002Fgitee.com\u002Fmindspore\u002Fmindformers) 是一个基于昇思框架（MindSpore）并支持大模型训练、微调、评估、推理、部署的全流程开发套件，[Baichuan2-7B \u002F 13B]( https:\u002F\u002Fgitee.com\u002Fmindspore\u002Fmindformers\u002Ftree\u002Fdev\u002Fresearch\u002Fbaichuan2) 已集成于此套件，支持用户进行模型微调、部署，具体使用方式可见 [README]( https:\u002F\u002Fgitee.com\u002Fmindspore\u002Fmindformers\u002Ftree\u002Fdev\u002Fresearch\u002Fbaichuan2\u002Fbaichuan2.md)。\n\n### 大模型体验平台\n\n[昇思大模型平台](https:\u002F\u002Fxihe.mindspore.cn) 基于昇思 MindSpore AI 框架、MindFormers 大模型开发套件与昇腾硬件算力，将 [Baichuan2-7B](https:\u002F\u002Fxihe.mindspore.cn\u002Fmodelzoo\u002Fbaichuan2_7b_chat) 大模型能力开放给公众，欢迎大家在线体验。\n\n## LLaMA-Efficient-Tuning\n[LLaMA-Efficient-Tuning](https:\u002F\u002Fgithub.com\u002Fhiyouga\u002FLLaMA-Efficient-Tuning) 已支持对 Baichuan 2 模型的微调和继续训练。\n\n## 太初元碁\nBaichuan2（7B\u002F13B）支持太初 T100 加速卡推理，现[试用通道](http:\u002F\u002Fwww.tecorigin.com\u002Fcn\u002Fdeveloper.html)已正式对外开启。\n\n# 声明、协议、引用\n\n## 声明\n\n我们在此声明，我们的开发团队并未基于 Baichuan 2 模型开发任何应用，无论是在 iOS、Android、网页或任何其他平台。我们强烈呼吁所有使用者，不要利用 Baichuan 2 模型进行任何危害国家社会安全或违法的活动。另外，我们也要求使用者不要将 Baichuan 2 模型用于未经适当安全审查和备案的互联网服务。我们希望所有的使用者都能遵守这个原则，确保科技的发展能在规范和合法的环境下进行。\n\n我们已经尽我们所能，来确保模型训练过程中使用的数据的合规性。然而，尽管我们已经做出了巨大的努力，但由于模型和数据的复杂性，仍有可能存在一些无法预见的问题。因此，如果由于使用 Baichuan 2 开源模型而导致的任何问题，包括但不限于数据安全问题、公共舆论风险，或模型被误导、滥用、传播或不当利用所带来的任何风险和问题，我们将不承担任何责任。\n\n## 协议\n社区使用 Baichuan 2 模型需要遵循 [Apache 2.0](https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan2\u002Fblob\u002Fmain\u002FLICENSE) 和[《Baichuan 2 模型社区许可协议》](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-7B-Base\u002Fresolve\u002Fmain\u002FBaichuan%202%E6%A8%A1%E5%9E%8B%E7%A4%BE%E5%8C%BA%E8%AE%B8%E5%8F%AF%E5%8D%8F%E8%AE%AE.pdf)。Baichuan 2 模型支持商业用途，如果您计划将 Baichuan 2 模型或其衍生品用于商业目的，请您确认您的主体符合以下情况：\n  1. 您或您的关联方的服务或产品的日均用户活跃量（DAU）低于100万。\n  2. 您或您的关联方不是软件服务提供商、云服务提供商。\n  3. 您或您的关联方不存在将授予您的商用许可，未经百川许可二次授权给其他第三方的可能。\n\n在符合以上条件的前提下，您需要通过以下联系邮箱 opensource@baichuan-inc.com ，提交《Baichuan 2 模型社区许可协议》要求的申请材料。审核通过后，百川将特此授予您一个非排他性、全球性、不可转让、不可再许可、可撤销的商用版权许可。\n\n## 引用\n如需引用我们的工作，请使用如下 reference:\n```\n@article{baichuan2023baichuan2,\n  title={Baichuan 2: Open Large-scale Language Models},\n  author={Baichuan},\n  journal={arXiv preprint arXiv:2309.10305},\n  url={https:\u002F\u002Farxiv.org\u002Fabs\u002F2309.10305},\n  year={2023}\n}\n```\n","\u003C!-- markdownlint-disable first-line-h1 -->\n\u003C!-- markdownlint-disable html -->\n\n\u003Cdiv align=\"center\">\n\u003Ch1>\n  百川2\n\u003C\u002Fh1>\n\u003C\u002Fdiv>\n\n\u003Cp align=\"center\">\n🤗 \u003Ca href=\"https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002F\" target=\"_blank\">Hugging Face\u003C\u002Fa> • 🤖 \u003Ca href=\"https:\u002F\u002Fmodelscope.cn\u002Forganization\u002Fbaichuan-inc\" target=\"_blank\">ModelScope\u003C\u002Fa> • 💬 \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan-7B\u002Fblob\u002Fmain\u002Fmedia\u002Fwechat.jpeg?raw=true\" target=\"_blank\">WeChat\u003C\u002Fa>• 🧩 \u003Ca href=\"https:\u002F\u002Fmodelers.cn\u002Fspaces\u002FBaichuan\u002FBaichuan2-7B-Chat\" target=\"_blank\">Modelers\u003C\u002Fa>\n\u003C\u002Fp>\n\n\u003Cdiv align=\"center\">\n\n\u003Cdiv align=\"center\">\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbaichuan-inc_Baichuan2_readme_d923268e724c.jpeg\" width=20% \u002F>\n\u003C\u002Fdiv>\n\n🚀 百小应APP已正式上线！懂搜索、会提问的AI助手，各大应用商店搜索百小应下载，欢迎下载体验 🎉\n\n\n[![license](https:\u002F\u002Fimg.shields.io\u002Fgithub\u002Flicense\u002Fmodelscope\u002Fmodelscope.svg)](https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan2\u002Fblob\u002Fmain\u002FLICENSE)\n\n\n\u003Ch4 align=\"center\">\n    \u003Cp>\n        \u003Cb>中文\u003C\u002Fb> |\n        \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan2\u002Fblob\u002Fmain\u002FREADME_EN.md\">English\u003C\u002Fa>\n    \u003Cp>\n\u003C\u002Fh4>\n\n\u003C\u002Fdiv>\n\n# 目录\n\n- [📖 模型介绍](#模型介绍)\n- [📊 Benchmark 结果 🥇🥇🔥🔥](#Benchmark-结果)\n- [⚙️ 推理和部署](#推理和部署)\n- [🛠️ 模型微调](#模型微调)\n- [💾 中间 Checkpoints 🔥🔥](#中间-Checkpoints)\n- [👥 社区与生态](#社区与生态)\n- [📜 声明、协议、引用](#声明协议引用)\n\n# 更新\n[2023.12.29] 🎉🎉🎉 我们发布了 **[Baichuan2-13B-Chat v2](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-13B-Chat\u002Ftree\u002Fv2.0)** 版本。其中：\n- 大幅提升了模型的综合能力，特别是数学和逻辑推理、复杂指令跟随能力。\n\n# 模型介绍\n\n- Baichuan 2 是百川智能推出的**新一代开源大语言模型**，采用 **2.6 万亿**  Tokens 的高质量语料训练。\n- Baichuan 2 在多个权威的中文、英文和多语言的通用、领域 benchmark 上取得同尺寸**最佳**的效果。\n- 本次发布包含有 **7B**、**13B** 的 **Base** 和 **Chat** 版本，并提供了 Chat 版本的 **4bits 量化**。\n- 所有版本对学术研究完全开放。同时，开发者通过邮件申请并获得官方商用许可后，即可**免费商用**，请参考[协议](#协议)章节。\n- 欢迎阅读我们的技术报告 [Baichuan 2: Open Large-scale Language Models](https:\u002F\u002Farxiv.org\u002Fabs\u002F2309.10305) 获取更多信息。\n\n本次发布版本和下载链接见下表：\n|         | 基座模型  | 对齐模型 | 对齐模型 4bits 量化 |\n|:-------:|:-------:|:-------:|:-----------------:|\n| 7B      | 🤗 [Baichuan2-7B-Base](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-7B-Base) | 🤗 [Baichuan2-7B-Chat](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-7B-Chat) | 🤗 [Baichuan2-7B-Chat-4bits](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-7B-Chat-4bits) |\n| 13B     | 🤗 [Baichuan2-13B-Base](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-13B-Base) | 🤗 [Baichuan2-13B-Chat](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-13B-Chat) | 🤗 [Baichuan2-13B-Chat-4bits](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-13B-Chat-4bits) |\n\n# Benchmark 结果\n\n我们在[通用](#通用领域)、[法律](#法律医疗)、[医疗](#法律医疗)、[数学](#数学代码)、[代码](#数学代码)和[多语言翻译](#多语言翻译)六个领域的中英文和多语言权威数据集上对模型进行了广泛测试。\n\n## 通用领域\n\n在通用领域我们在以下数据集上进行了 5-shot 测试。\n- [C-Eval](https:\u002F\u002Fcevalbenchmark.com\u002Findex.html#home) 是一个全面的中文基础模型评测数据集，涵盖了 52 个学科和四个难度的级别。我们使用该数据集的 dev 集作为 few-shot 的来源，在 test 集上进行测试。我们采用了 [Baichuan-7B](https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan-7B\u002Ftree\u002Fmain) 的评测方案。\n- [MMLU](https:\u002F\u002Farxiv.org\u002Fabs\u002F2009.03300) 是包含 57 个任务的英文评测数据集，涵盖了初等数学、美国历史、计算机科学、法律等，难度覆盖高中水平到专家水平，是目前主流的 LLM 评测数据集。我们采用了[开源](https:\u002F\u002Fgithub.com\u002Fhendrycks\u002Ftest)的评测方案。\n- [CMMLU](https:\u002F\u002Fgithub.com\u002Fhaonan-li\u002FCMMLU) 是一个包含 67 个主题的综合性性中文评估基准，专门用于评估语言模型在中文语境下的知识和推理能力。我们采用了其[官方](https:\u002F\u002Fgithub.com\u002Fhaonan-li\u002FCMMLU)的评测方案。\n- [Gaokao](https:\u002F\u002Fgithub.com\u002FOpenLMLab\u002FGAOKAO-Bench) 是一个以中国高考题作为评测大语言模型能力的数据集，用以评估模型的语言能力和逻辑推理能力。 我们只保留了其中的单项选择题，并进行了随机划分。我们采用了与 C-Eval 类似的评测方案。\n- [AGIEval](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002FAGIEval) 旨在评估模型的认知和解决问题相关的任务中的一般能力。 我们只保留了其中的四选一单项选择题，并进行了随机划分。我们采用了与 C-Eval 类似的评测方案。\n- [BBH](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002Flukaemon\u002Fbbh) 是一个挑战性任务 Big-Bench 的子集。Big-Bench 目前包括 204 项任务。任务主题涉及语言学、儿童发展、数学、常识推理、生物学、物理学、社会偏见、软件开发等方面。BBH 是从 204 项 Big-Bench 评测基准任务中大模型表现不好的任务单独拿出来形成的评测基准。\n\n### 7B 模型结果\n\n|                       | **C-Eval** | **MMLU** | **CMMLU** | **Gaokao** | **AGIEval** | **BBH** |\n|:---------------------:|:----------:|:--------:|:---------:|:----------:|:-----------:|:-------:|\n|                       |  5-shot    |  5-shot  |  5-shot   | 5-shot     | 5-shot      | 3-shot  |\n| **GPT-4**             | 68.40      | 83.93    | 70.33     | 66.15      | 63.27       | 75.12   |\n| **GPT-3.5 Turbo**     | 51.10      | 68.54    | 54.06     | 47.07      | 46.13       | 61.59   |\n| **LLaMA-7B**          | 27.10      | 35.10    | 26.75     | 27.81      | 28.17       | 32.38   |\n| **LLaMA2-7B**         | 28.90      | 45.73    | 31.38     | 25.97      | 26.53       | 39.16   |\n| **MPT-7B**            | 27.15      | 27.93    | 26.00     | 26.54      | 24.83       | 35.20   |\n| **Falcon-7B**         | 24.23      | 26.03    | 25.66     | 24.24      | 24.10       | 28.77   |\n| **ChatGLM2-6B**       | 50.20      | 45.90    | 49.00     | 49.44      | 45.28       | 31.65   |\n| **Baichuan-7B**       | 42.80      | 42.30    | 44.02     | 36.34      | 34.44       | 32.48   |\n| **Baichuan2-7B-Base** | 54.00      | 54.16    | 57.07     | 47.47      | 42.73       | 41.56   |\n\n### 13B 模型结果\n\n|                             | **C-Eval** | **MMLU** | **CMMLU** | **Gaokao** | **AGIEval** | **BBH** |\n|:---------------------------:|:----------:|:--------:|:---------:|:----------:|:-----------:|:-------:|\n|                             |  5-shot    |  5-shot  |  5-shot   | 5-shot     | 5-shot      | 3-shot  |\n| **GPT-4**                   | 68.40      | 83.93    | 70.33     | 66.15      | 63.27       | 75.12   |\n| **GPT-3.5 Turbo**           | 51.10      | 68.54    | 54.06     | 47.07      | 46.13       | 61.59   |\n| **LLaMA-13B**               | 28.50      | 46.30    | 31.15     | 28.23      | 28.22       | 37.89   |\n| **LLaMA2-13B**              | 35.80      | 55.09    | 37.99     | 30.83      | 32.29       | 46.98   |\n| **Vicuna-13B**              | 32.80      | 52.00    | 36.28     | 30.11      | 31.55       | 43.04   |\n| **Chinese-Alpaca-Plus-13B** | 38.80      | 43.90    | 33.43     | 34.78      | 35.46       | 28.94   |\n| **XVERSE-13B**              | 53.70      | 55.21    | 58.44     | 44.69      | 42.54       | 38.06   |\n| **Baichuan-13B-Base**       | 52.40      | 51.60    | 55.30     | 49.69      | 43.20       | 43.01   |\n| **Baichuan2-13B-Base**      | 58.10      | 59.17    | 61.97     | 54.33      | 48.17       | 48.78   |\n\n## 法律、医疗\n\n法律领域我们使用了 [JEC-QA](https:\u002F\u002Fjecqa.thunlp.org\u002F) 数据集。JEC-QA 数据集来源于中国国家司法考试。我们只保留了其中的单选题。我们采用了与 C-Eval 类似的评测方案。\n\n医疗领域则使用通用领域数据集（C-Eval、MMLU、CMMLU）中的医学相关学科、[MedQA](https:\u002F\u002Farxiv.org\u002Fabs\u002F2009.13081) 和 [MedMCQA](https:\u002F\u002Fmedmcqa.github.io\u002F)。我们采用了与 C-Eval 类似的评测方案。\n- 为了测试方便，我们使用了 C-Eval 的 val 集进行测试。\n- MedQA 数据集来源于美国、中国的医学考试。我们测试了 [MedQA数据集](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002Fbigbio\u002Fmed_qa) 中的 USMLE 和 MCMLE 两个子集，并采用了五个候选的版本。\n- MedMCQA 数据集来源于印度医学院的入学考试。我们只保留了其中的单选题。由于 test 集没有答案，我们使用 dev 集进行测试。\n- 通用领域数据集包含的医学相关学科如下：\n    - C-Eval: clinical_medicine, basic_medicine\n    - MMLU: clinical_knowledge, anatomy, college_medicine, college_biology, nutrition, virology, medical_genetics, professional_medicine\n    - CMMLU: anatomy, clinical_knowledge, college_medicine, genetics, nutrition, traditional_chinese_medicine, virology \n\n我们对以上数据集进行了 5-shot 测试。\n\n### 7B 模型结果\n\n|                       | **JEC-QA** | **CEval-MMLU-CMMLU** | **MedQA-USMLE** | **MedQA-MCMLE** | **MedMCQA** |\n|:---------------------:|:----------:|:--------------------:|:---------------:|:---------------:|:-----------:|\n|                       | 5-shot     | 5-shot               | 5-shot          | 5-shot          | 5-shot      |\n| **GPT-4**             | 59.32      | 77.16                | 80.28           | 74.58           | 72.51       |\n| **GPT-3.5 Turbo**     | 42.31      | 61.17                | 53.81           | 52.92           | 56.25       |\n| **LLaMA-7B**          | 27.45      | 33.34                | 24.12           | 21.72           | 27.45       |\n| **LLaMA2-7B**         | 29.20      | 36.75                | 27.49           | 24.78           | 37.93       |\n| **MPT-7B**            | 27.45      | 26.67                | 16.97           | 19.79           | 31.96       |\n| **Falcon-7B**         | 23.66      | 25.33                | 21.29           | 18.07           | 33.88       |\n| **ChatGLM2-6B**       | 40.76      | 44.54                | 26.24           | 45.53           | 30.22       |\n| **Baichuan-7B**       | 34.64      | 42.37                | 27.42           | 39.46           | 31.39       |\n| **Baichuan2-7B-Base** | 44.46      | 56.39                | 32.68           | 54.93           | 41.73       |\n\n### 13B 模型结果\n\n|                             | **JEC-QA** | **CEval-MMLU-CMMLU** | **MedQA-USMLE** | **MedQA-MCMLE** | **MedMCQA** |\n|:---------------------------:|:----------:|:--------------------:|:---------------:|:---------------:|:-----------:|\n|                             | 5-shot     | 5-shot              | 5-shot         | 5-shot         | 5-shot      |\n| **GPT-4**                   | 59.32      | 77.16                | 80.28           | 74.58           | 72.51       |\n| **GPT-3.5 Turbo**           | 42.31      | 61.17                | 53.81           | 52.92           | 56.25       |\n| **LLaMA-13B**               | 27.54      | 35.14                | 28.83           | 23.38           | 39.52       |\n| **LLaMA2-13B**              | 34.08      | 47.42                | 35.04           | 29.74           | 42.12       |\n| **Vicuna-13B**              | 28.38      | 40.99                | 34.80           | 27.67           | 40.66       |\n| **Chinese-Alpaca-Plus-13B** | 35.32      | 46.31                | 27.49           | 32.66           | 35.87       |\n| **XVERSE-13B**              | 46.42      | 58.08                | 32.99           | 58.76           | 41.34       |\n| **Baichuan-13B-Base**       | 41.34      | 51.77                | 29.07           | 43.67           | 39.60       |\n| **Baichuan2-13B-Base**      | 47.40      | 59.33                | 40.38           | 61.62           | 42.86       |\n\n## 数学、代码\n\n数学领域我们使用 [OpenCompass](https:\u002F\u002Fopencompass.org.cn\u002F) 评估框架，对 [GSM8K](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002Fgsm8k) 和 [MATH](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002Fcompetition_math) 数据集进行了 4-shot 测试。\n\n- GSM8K 是由 OpenAI 发布的一个由 8.5K 高质量的语言多样化的小学数学应用题组成的数据集，要求根据给定的场景和两个可能的解决方案，选择最合理的方案。\n- MATH 数据集包含 12,500 个数学问题（其中 7500 个属于训练集，5000 个属于测试集），这些问题收集自 AMC 10、AMC 12、AIME 等数学竞赛。\n\n代码领域则采用了 [HumanEval](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002Fopenai_humaneval) 和 [MBPP](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002Fmbpp) 数据集。我们使用 OpenCompass，对 HumanEval 进行了 0-shot 测试，MBPP 数据集进行了 3-shot 测试。\n- HumanEval 中的编程任务包括模型语言理解、推理、算法和简单数学，以评估模型功能正确性，并衡量模型的问题解决能力。\n- MBPP 包括 974 个 Python 短函数、程序的文字描述以及用于检查功能正确性的测试用例的数据集。\n\n### 7B 模型结果\n\n|                       | **GSM8K** | **MATH** | **HumanEval** | **MBPP** |\n|:---------------------:|:---------:|:--------:|:-------------:|:--------:|\n|                       |  4-shot   | 4-shot   |  0-shot       |  3-shot  |\n| **GPT-4**             |   89.99   | 40.20    | 69.51         |  63.60   |\n| **GPT-3.5 Turbo**     |   57.77   | 13.96    | 52.44         |  61.40   |\n| **LLaMA-7B**          |   9.78    | 3.02     | 11.59         |  14.00   |\n| **LLaMA2-7B**         |   16.22   | 3.24     | 12.80         |  14.80   |\n| **MPT-7B**            |   8.64    | 2.90     | 14.02         |  23.40   |\n| **Falcon-7B**         |   5.46    | 1.68     | -             |  10.20   |\n| **ChatGLM2-6B**       |   28.89   | 6.40     | 9.15          |   9.00   |\n| **Baichuan-7B**       |   9.17    | 2.54     | 9.20          |   6.60   |\n| **Baichuan2-7B-Base** |   24.49   | 5.58     | 18.29         |  24.20   |\n\n### 13B 模型结果\n\n|                             | **GSM8K** | **MATH** | **HumanEval** | **MBPP** |\n|:---------------------------:|:---------:|:--------:|:-------------:|:--------:|\n|                             |  4-shot   | 4-shot   |  0-shot       |  3-shot  |\n| **GPT-4**                   |   89.99   | 40.20    | 69.51         |  63.60   |\n| **GPT-3.5 Turbo**           |   57.77   | 13.96    | 52.44         |  61.40   |\n| **LLaMA-13B**               |   20.55   | 3.68     | 15.24         |  21.40   |\n| **LLaMA2-13B**              |   28.89   | 4.96     | 15.24         |  27.00   |\n| **Vicuna-13B**              |   28.13   | 4.36     | 16.46         |  15.00   |\n| **Chinese-Alpaca-Plus-13B** |   11.98   | 2.50     | 16.46         |  20.00   |\n| **XVERSE-13B**              |   18.20   | 2.18     | 15.85         |  16.80   |\n| **Baichuan-13B-Base**       |   26.76   | 4.84     | 11.59         |  22.80   |\n| **Baichuan2-13B-Base**      |   52.77   | 10.08    | 17.07         |  30.20   |\n\n## 多语言翻译\n\n我们采用了 [Flores-101](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002Ffacebook\u002Fflores) 数据集来评估模型的多语言能力。Flores-101 涵盖了世界各地的 101 种语言。它的数据来源于新闻、旅游指南和书籍等多个不同领域。我们选择了联合国官方语言（阿拉伯文、中文、英文、法文、俄文和西班牙文）以及德文和日文作为测试语种。我们使用 OpenCompass 对 Flores-101 中的中-英、中-法、中-西班牙、中-阿拉伯、中-俄、中-日、中-德等七个子任务分别进行了 8-shot 测试。\n\n### 7B 模型结果\n\n|             | **CN-EN** | **CN-FR** | **CN-ES** | **CN-AR** | **CN-RU** | **CN-JP** | **CN-DE** | Average |\n|:---------------------:|:-------:|:-------:|:---------:|:---------:|:-------:|:-------:|:-------:|:-------:|\n| **GPT-4**             | 29.94   | 29.56   | 20.01     | 10.76     | 18.62   | 13.26   | 20.83   | 20.43   |\n| **GPT-3.5 Turbo**     | 27.67   | 26.15   | 19.58     | 10.73     | 17.45   | 1.82    | 19.70   | 17.59   |\n| **LLaMA-7B**          | 17.27   | 12.02   | 9.54      | 0.00      | 4.47    | 1.41    | 8.73    | 7.63    |\n| **LLaMA2-7B**         | 25.76   | 15.14   | 11.92     | 0.79      | 4.99    | 2.20    | 10.15   | 10.14   |\n| **MPT-7B**            | 20.77   | 9.53    | 8.96      | 0.10      | 3.54    | 2.91    | 6.54    | 7.48    |\n| **Falcon-7B**         | 22.13   | 15.67   | 9.28      | 0.11      | 1.35    | 0.41    | 6.41    | 7.91    |\n| **ChatGLM2-6B**       | 22.28   | 9.42    | 7.77      | 0.64      | 1.78    | 0.26    | 4.61    | 6.68    |\n| **Baichuan-7B**       | 25.07   | 16.51   | 12.72     | 0.41      | 6.66    | 2.24    | 9.86    | 10.50   |\n| **Baichuan2-7B-Base** | 27.27   | 20.87   | 16.17     | 1.39      | 11.21   | 3.11    | 12.76   | 13.25   |\n\n### 13B 模型结果\n\n|                   | **中-英** | **中-法** | **中-西** | **中-阿** | **中-俄** | **中-日** | **中-德** | 平均 |\n|:---------------------------:|:-------:|:-------:|:---------:|:---------:|:-------:|:-------:|:-------:|:-------:|\n|          **GPT-4**          | 29.94   | 29.56   | 20.01     | 10.76     | 18.62   | 13.26   | 20.83   | 20.43   |\n|      **GPT-3.5 Turbo**      | 27.67   | 26.15   | 19.58     | 10.73     | 17.45   | 1.82    | 19.70   | 17.59   |\n|        **LLaMA-13B**        | 21.75   | 16.16   | 13.29     | 0.58      | 7.61    | 0.41    | 10.66   | 10.07   |\n|       **LLaMA2-13B**        | 25.44   | 19.25   | 17.49     | 1.38      | 10.34   | 0.13    | 11.13   | 12.17   |\n|       **Vicuna-13B**        | 22.63   | 18.04   | 14.67     | 0.70      | 9.27    | 3.59    | 10.25   | 11.31   |\n| **Chinese-Alpaca-Plus-13B** | 22.53   | 13.82   | 11.29     | 0.28      | 1.52    | 0.31    | 8.13    | 8.27    |\n|       **XVERSE-13B**        | 29.26   | 24.03   | 16.67     | 2.78      | 11.61   | 3.08    | 14.26   | 14.53   |\n|    **Baichuan-13B-Base**    | 30.24   | 20.90   | 15.92     | 0.98      | 9.65    | 2.64    | 12.00   | 13.19   |\n|    **Baichuan2-13B-Base**   | 30.61   | 22.11   | 17.27     | 2.39      | 14.17   | 11.58   | 14.53   | 16.09   |\n\n# 推理和部署\n\n推理所需的模型权重、源码、配置已发布在 Hugging Face，下载链接见本文档最开始的表格。我们在此示范多种推理方式。程序会自动从 Hugging Face 下载所需资源。\n\n## 安装依赖\n```shell\npip install -r requirements.txt\n```\n\n## Python 代码方式\n\n### Chat 模型推理方法示范\n```python\n>>> import torch\n>>> from transformers import AutoModelForCausalLM, AutoTokenizer\n>>> from transformers.generation.utils import GenerationConfig\n>>> tokenizer = AutoTokenizer.from_pretrained(\"baichuan-inc\u002FBaichuan2-13B-Chat\", use_fast=False, trust_remote_code=True)\n>>> model = AutoModelForCausalLM.from_pretrained(\"baichuan-inc\u002FBaichuan2-13B-Chat\", device_map=\"auto\", torch_dtype=torch.bfloat16, trust_remote_code=True)\n>>> model.generation_config = GenerationConfig.from_pretrained(\"baichuan-inc\u002FBaichuan2-13B-Chat\")\n>>> messages = []\n>>> messages.append({\"role\": \"user\", \"content\": \"解释一下“温故而知新”\"})\n>>> response = model.chat(tokenizer, messages)\n>>> print(response)\n\"温故而知新\"是一句中国古代的成语，出自《论语·为政》篇。这句话的意思是：通过回顾过去，我们可以发现新的知识和理解。换句话说，学习历史和经验可以让我们更好地理解现在和未来。\n\n这句话鼓励我们在学习和生活中不断地回顾和反思过去的经验，从而获得新的启示和成长。通过重温旧的知识和经历，我们可以发现新的观点和理解，从而更好地应对不断变化的世界和挑战。\n```\n\n### Base 模型推理方法示范\n```python\n>>> from transformers import AutoModelForCausalLM, AutoTokenizer\n>>> tokenizer = AutoTokenizer.from_pretrained(\"baichuan-inc\u002FBaichuan2-13B-Base\", trust_remote_code=True)\n>>> model = AutoModelForCausalLM.from_pretrained(\"baichuan-inc\u002FBaichuan2-13B-Base\", device_map=\"auto\", trust_remote_code=True)\n>>> inputs = tokenizer('登鹳雀楼->王之涣\\n夜雨寄北->', return_tensors='pt')\n>>> inputs = inputs.to('cuda:0')\n>>> pred = model.generate(**inputs, max_new_tokens=64, repetition_penalty=1.1)\n>>> print(tokenizer.decode(pred.cpu()[0], skip_special_tokens=True))\n登鹳雀楼->王之涣\n夜雨寄北->李商隐\n```\n\n> 在上述两段代码中，模型加载指定 `device_map='auto'`，会使用所有可用显卡。如需指定使用的设备，可以使用类似 `export CUDA_VISIBLE_DEVICES=0,1`（使用了0、1号显卡）的方式控制。\n\n## 命令行工具方式\n\n```shell\npython cli_demo.py\n```\n本命令行工具是为 Chat 场景设计，因此我们不支持使用该工具调用 Base 模型。\n\n## 网页 demo 方式\n\n依靠 streamlit 运行以下命令，会在本地启动一个 web 服务，把控制台给出的地址放入浏览器即可访问。本网页 demo 工具是为 Chat 场景设计，因此我们不支持使用该工具调用 Base 模型。\n\n```shell\nstreamlit run web_demo.py\n```\n\n## Baichuan2-13B-Chat 示例输出\n\n\u003Cdetails>\u003Csummary>\u003Cb>代码\u003C\u002Fb>\u003C\u002Fsummary>\n\n````\n用户：找到下列数组的中位数[3.1,6.2,1.3,8.4,10.5,11.6,2.1]，请用python代码完成以上功能\n\nBaichuan 2：def find_median(numbers):\n    numbers.sort()\n    length = len(numbers)\n    if length % 2 == 0:\n        return (numbers[length \u002F\u002F 2 - 1] + numbers[length \u002F\u002F 2]) \u002F 2\n    else:\n        return numbers[length \u002F\u002F 2]\nnumbers = [3.1, 6.2, 1.3, 8.4, 10.5, 11.6, 2.1]\nprint(find_median(numbers))\n````\n\n\u003C\u002Fdetails>\n\n\u003Cdetails>\u003Csummary>\u003Cb>数学\u003C\u002Fb>\u003C\u002Fsummary>\n\n````\n用户：鸡和兔在一个笼子里，共有26个头，68只脚，那么鸡有多少只，兔有多少只？\n\nBaichuan 2：我们可以用代数方法来解决这个问题。假设鸡的数量为x，兔子的数量为y。根据题目给出的信息，我们可以得到以下两个方程：\n\n1. x + y = 26 (头的总数)\n2. 2x + 4y = 68 (脚的总数)\n\n现在我们需要解这个二元一次方程组。首先从第一个方程中解出x：\n\nx = 26 - y\n\n然后将x的表达式代入第二个方程：\n\n2(26 - y) + 4y = 68\n52 - 2y + 4y = 68\n2y = 16\n\n接下来我们求解y的值：\n\ny = 16 \u002F 2\ny = 8\n\n现在我们得到了兔子的数量是8只。将y的值代入x的表达式求得鸡的数量：\n\nx = 26 - 8\nx = 18\n\n所以鸡有18只，兔子有8只。\n````\n\n\u003C\u002Fdetails>\n\n\u003Cdetails>\u003Csummary>\u003Cb>指令跟随\u003C\u002Fb>\u003C\u002Fsummary>\n\n````\n用户：从现在开始，请你将我接下来说的每一句话翻译成英语，准备好了吗？\n\nBaichuan 2：I'm ready to translate every word you say next into English.\n\n用户：你好\n\nBaichuan 2：Hello\n\n用户：我是Baichuan 2，很高兴认识大家\n\nBaichuan 2：I am Baichuan 2, and I am very happy to meet everyone.\n\n用户：希望通过大家的共同努力，早日迎接AGI时代的到来\n\nBaichuan 2：I hope through the joint efforts of all of us, we can welcome the arrival of the AGI era as soon as possible.\n````\n\n\u003C\u002Fdetails>\n\n\u003Cdetails>\u003Csummary>\u003Cb>解题\u003C\u002Fb>\u003C\u002Fsummary>\n\n````\n用户：以下物理常识题目，哪一个是错误的?A.在自然环境下，声音在固体中传播速度最快。B.牛顿第一定律:一个物体如果不受力作用，将保持静止或匀速直线运动的状态。C.牛顿第三定律:对于每个作用力，都有一个相等而反向的反作用力。D.声音在空气中的传播速度为1000m\u002Fs。\n\nBaichuan 2：本题考查物理基本概念和原理的理解和应用能力。\nA项：声音的传播需要介质，在固体、液体和气体的三种介质中，声音在固体中的传播速度是最快的，故A正确； B项：牛顿第一定律是惯性定律，其内容是：一个物体如果没有受到外力的作用，要么保持静止状态，要么保持匀速直线运动状态，直到有外力迫使它改变运动状态为止，故B正确； C项：牛顿第三定律的内容是：对于每一个作用力，都有一个大小相等、方向相反的反作用力，这两个力分别作用在两个相互作用的物体上，故C错误； D项：声音在空气中以波的形式传播，其传播速度与介质的温度、湿度等因素有关，一般情况下，声音在空气中的传播速度约为340m\u002Fs左右，而不是1000m\u002Fs，故D错误。 因此，本题的正确答案是 D 。\n````\n\n````\n用户：I'm Mike 。I am going to have a busy weekend。On Saturday，I am going to learn how to swim。I will go with my father。Then we are going to have lunch in the restaurant。In the afternoon，we are going to the zoo。We are going to see the pandas。On Sunday，I will finish my homework in the morning。In the afternoon，my parents and I are going to visit my grandparents。We will have dinner together。\n阅读短文，判断正确和错误！正确请写T，错误请写F:\n1.Mike is going to have a busy weekend()\n2.Mike is going to learn how to swim with his father()\n3.They are going to have lunch at home()\n\nBaichuan 2：1. T（正确的）\n2. T（正确的）\n3. F（错误的）\n````\n\n\u003C\u002Fdetails>\n\n## 量化部署\n\n为了让不同的用户以及不同的平台都能运行 Baichuan 2 模型，我们针对 Baichuan 2 模型做了相应地量化工作（包括 Baichuan2-7B-Chat 和 Baichuan2-13B-Chat），方便用户快速高效地在自己的平台部署 Baichuan 2 模型。\n\n### 量化方法\n\nBaichuan 2 的采用社区主流的量化方法：[BitsAndBytes](https:\u002F\u002Fgithub.com\u002FTimDettmers\u002Fbitsandbytes)。该方法可以保证量化后的效果基本不掉点，目前已经集成到 transformers 库里，并在社区得到了广泛应用。BitsAndBytes 支持 8bits 和 4bits 两种量化，其中 4bits 支持 FP4 和 NF4 两种格式，Baichuan 2 选用 NF4 作为 4bits 量化的数据类型。  \n  \n基于该量化方法，Baichuan 2 支持在线量化和离线量化两种模式。\n\n### 在线量化\n\n对于在线量化，我们支持 8bits 和 4bits 量化，使用方式和 [Baichuan-13B](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan-13B-Chat) 项目中的方式类似，只需要先加载模型到 CPU 的内存里，再调用`quantize()`接口量化，最后调用 `cuda()`函数，将量化后的权重拷贝到 GPU 显存中。实现整个模型加载的代码非常简单，我们以 Baichuan2-7B-Chat 为例：\n\n8bits 在线量化:\n```python\nmodel = AutoModelForCausalLM.from_pretrained(\"baichuan-inc\u002FBaichuan2-7B-Chat\", torch_dtype=torch.float16, trust_remote_code=True)\nmodel = model.quantize(8).cuda() \n```\n4bits 在线量化:\n```python\nmodel = AutoModelForCausalLM.from_pretrained(\"baichuan-inc\u002FBaichuan2-7B-Chat\", torch_dtype=torch.float16, trust_remote_code=True)\nmodel = model.quantize(4).cuda() \n```\n需要注意的是，在用 `from_pretrained` 接口的时候，用户一般会加上 `device_map=\"auto\"`，在使用在线量化时，需要去掉这个参数，否则会报错。\n\n### 离线量化\n\n为了方便用户的使用，我们提供了离线量化好的 4bits 的版本 [Baichuan2-7B-Chat-4bits](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-7B-Chat-4bits\u002Ftree\u002Fmain)，供用户下载。\n用户加载 Baichuan2-7B-Chat-4bits 模型很简单，只需要执行:\n```python\nmodel = AutoModelForCausalLM.from_pretrained(\"baichuan-inc\u002FBaichuan2-7B-Chat-4bits\", device_map=\"auto\", trust_remote_code=True)\n```\n对于 8bits 离线量化，我们没有提供相应的版本，因为 Hugging Face transformers 库提供了相应的 API 接口，可以很方便的实现 8bits 量化模型的保存和加载。用户可以自行按照如下方式实现 8bits 的模型保存和加载：\n```python\n# Model saving: model_id is the original model directory, and quant8_saved_dir is the directory where the 8bits quantized model is saved.\nmodel = AutoModelForCausalLM.from_pretrained(model_id, load_in_8bit=True, device_map=\"auto\", trust_remote_code=True)\nmodel.save_pretrained(quant8_saved_dir)\nmodel = AutoModelForCausalLM.from_pretrained(quant8_saved_dir, device_map=\"auto\", trust_remote_code=True)\n```\n\n### 量化效果\n\n量化前后显存占用对比 (GPU Mem in GB)：\n| Precision   | Baichuan2-7B |Baichuan2-13B |\n|-------------|:------------:|:------------:|\n| bf16 \u002F fp16 | 15.3         | 27.5         |\n| 8bits       | 8.0          | 16.1         |\n| 4bits       | 5.1          | 8.6          |\n\n量化后在各个 benchmark 上的结果和原始版本对比如下：\n\n| Model 5-shot           | C-Eval | MMLU | CMMLU |\n|------------------------|:------:|:----:|:-----:|\n| Baichuan2-13B-Chat      | 56.74  | 57.32| 59.68  |\n| Baichuan2-13B-Chat-4bits | 56.05   | 56.24 | 58.82  |\n| Baichuan2-7B-Chat       | 54.35   | 52.93 | 54.99  |\n| Baichuan2-7B-Chat-4bits | 53.04   | 51.72 | 52.84  |\n> C-Eval 是在其 val set 上进行的评测\n\n可以看到，4bits 相对 bfloat16 精度损失在 1 - 2 个百分点左右。\n\n## CPU 部署\n\nBaichuan 2 模型支持 CPU 推理，但需要强调的是，CPU 的推理速度相对较慢。需按如下方式修改模型加载的方式：\n```python\n# Taking Baichuan2-7B-Chat as an example\nmodel = AutoModelForCausalLM.from_pretrained(\"baichuan-inc\u002FBaichuan2-7B-Chat\", torch_dtype=torch.float32, trust_remote_code=True)\n```\n## 对 Baichuan 1 的推理优化迁移到 Baichuan 2\n\n由于很多用户在 Baichuan 1 (Baichuan-7B, Baichuan-13B)上做了很多优化的工作，例如编译优化、量化等，为了将这些工作零成本地应用于 Baichuan 2，用户可以对 Baichuan 2 模型做一个离线转换，转换后就可以当做 Baichuan 1 模型来使用。具体来说，用户只需要利用以下脚本离线对 Baichuan 2 模型的最后一层 lm_head 做归一化，并替换掉`lm_head.weight`即可。替换完后，就可以像对 Baichuan 1 模型一样对转换后的模型做编译优化等工作了。\n```python\nimport torch\nimport os\nori_model_dir = 'your Baichuan 2 model directory'\n# To avoid overwriting the original model, it's best to save the converted model to another directory before replacing it\nnew_model_dir = 'your normalized lm_head weight Baichuan 2 model directory'\nmodel = torch.load(os.path.join(ori_model_dir, 'pytorch_model.bin'))\nlm_head_w = model['lm_head.weight']\nlm_head_w = torch.nn.functional.normalize(lm_head_w)\nmodel['lm_head.weight'] = lm_head_w\ntorch.save(model, os.path.join(new_model_dir, 'pytorch_model.bin'))\n```\n\n# 模型微调\n\n## 依赖安装\n\n```shell\ngit clone https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan2.git\ncd Baichuan2\u002Ffine-tune\npip install -r requirements.txt\n```\n- 如需使用 LoRA 等轻量级微调方法需额外安装 [peft](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Fpeft)\n- 如需使用 xFormers 进行训练加速需额外安装 [xFormers](https:\u002F\u002Fgithub.com\u002Ffacebookresearch\u002Fxformers)\n\n## 单机训练\n\n下面我们给一个微调 Baichuan2-7B-Base 的单机训练例子。\n\n训练数据：`data\u002Fbelle_chat_ramdon_10k.json`，该样例数据是从 [multiturn_chat_0.8M](https:\u002F\u002Fhuggingface.co\u002Fdatasets\u002FBelleGroup\u002Fmultiturn_chat_0.8M) 采样出 1 万条，并且做了格式转换。主要是展示多轮数据怎么训练，不保证效果。\n\n```shell\nhostfile=\"\"\ndeepspeed --hostfile=$hostfile fine-tune.py  \\\n    --report_to \"none\" \\\n    --data_path \"data\u002Fbelle_chat_ramdon_10k.json\" \\\n    --model_name_or_path \"baichuan-inc\u002FBaichuan2-7B-Base\" \\\n    --output_dir \"output\" \\\n    --model_max_length 512 \\\n    --num_train_epochs 4 \\\n    --per_device_train_batch_size 16 \\\n    --gradient_accumulation_steps 1 \\\n    --save_strategy epoch \\\n    --learning_rate 2e-5 \\\n    --lr_scheduler_type constant \\\n    --adam_beta1 0.9 \\\n    --adam_beta2 0.98 \\\n    --adam_epsilon 1e-8 \\\n    --max_grad_norm 1.0 \\\n    --weight_decay 1e-4 \\\n    --warmup_ratio 0.0 \\\n    --logging_steps 1 \\\n    --gradient_checkpointing True \\\n    --deepspeed ds_config.json \\\n    --bf16 True \\\n    --tf32 True\n```\n\n## 多机训练\n\n多机训练只需要给一下 hostfile ，内容类似如下：\n```\nip1 slots=8\nip2 slots=8\nip3 slots=8\nip4 slots=8\n....\n```\n同时在训练脚本里面指定 hosftfile 的路径：\n```shell\nhostfile=\"\u002Fpath\u002Fto\u002Fhostfile\"\ndeepspeed --hostfile=$hostfile fine-tune.py  \\\n    --report_to \"none\" \\\n    --data_path \"data\u002Fbelle_chat_ramdon_10k.json\" \\\n    --model_name_or_path \"baichuan-inc\u002FBaichuan2-7B-Base\" \\\n    --output_dir \"output\" \\\n    --model_max_length 512 \\\n    --num_train_epochs 4 \\\n    --per_device_train_batch_size 16 \\\n    --gradient_accumulation_steps 1 \\\n    --save_strategy epoch \\\n    --learning_rate 2e-5 \\\n    --lr_scheduler_type constant \\\n    --adam_beta1 0.9 \\\n    --adam_beta2 0.98 \\\n    --adam epsilon 1e-8 \\\n    --max grad norm 1.0 \\\n    -- weight decay 1e-4 \\\n    -- warm up ratio 0.0 \\\n    -- logging steps 1 \\\n    -- gradient checkpointing true \\\n    -- deepspeed ds config json \\\n    -- bf16 true \\\n    -- tf32 true\n```\n\n## 轻量化微调\n\n代码已经支持轻量化微调如 LoRA，如需使用仅需在上面的脚本中加入以下参数：\n```shell\n--use_lora True\n```\nLoRA 具体的配置可见 `fine-tune.py` 脚本。\n\n使用 LoRA 微调后可以使用下面的命令加载模型：\n```python\nfrom peft import AutoPeftModelForCausalLM\nmodel = AutoPeftModelForCausalLM.from_pretrained(\"output\", trust_remote_code=True)\n```\n\n# 中间 Checkpoints\n\n除了训练了 2.6 万亿 Tokens 的 Baichuan2-7B-Base 模型，我们还提供了在此之前的另外 11 个中间 checkpoints（分别对应训练了约 0.2 ~ 2.4 万亿 Tokens）供社区研究使用（[下载地址](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-7B-Intermediate-Checkpoints)）。下图给出了这些 checkpoints 在 C-Eval、MMLU、CMMLU 三个 benchmark 上的效果变化：\n\n\u003Cdiv align=\"center\">\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbaichuan-inc_Baichuan2_readme_a923a43d9e16.jpeg\" width=50% \u002F>\n\u003C\u002Fdiv>\n\n# 社区与生态\n\n**📢📢📢 我们会在此持续更新社区和生态对 Baichuan 2 的支持 😀😀😀**\n\n## Intel 酷睿 Ultra 平台运行百川大模型\n\n### 使用方式\n\n使用酷睿™\u002F至强® 可扩展处理器或配合锐炫™ GPU等进行部署BaiChuan2 - 7B\u002FChat，BaiChuan2 - 13B\u002FChat模型\n\n推荐使用 BigDL-LLM（[CPU](https:\u002F\u002Fgithub.com\u002Fintel-analytics\u002FBigDL\u002Ftree\u002Fmain\u002Fpython\u002Fllm\u002Fexample\u002FCPU\u002FHF-Transformers-AutoModels\u002FModel\u002Fbaichuan2), [GPU](https:\u002F\u002Fgithub.com\u002Fintel-analytics\u002FBigDL\u002Ftree\u002Fmain\u002Fpython\u002Fllm\u002Fexample\u002FGPU\u002FHF-Transformers-AutoModels\u002FModel\u002Fbaichuan2)）以发挥更好推理性能。\n\n### 帮助文档\n\n[中文操作手册](https:\u002F\u002Fgithub.com\u002Fintel-analytics\u002Fbigdl-llm-tutorial\u002Ftree\u002Fmain\u002FChinese_Version)，包括用notebook支持\n\n[加载，优化，保存方法等](https:\u002F\u002Fgithub.com\u002Fintel-analytics\u002Fbigdl-llm-tutorial\u002Fblob\u002Fmain\u002FChinese_Version\u002Fch_3_AppDev_Basic\u002F3_BasicApp.ipynb)\n\n## 华为昇腾\n\n### Pytorch 框架\n\n模型微调：Baichuan 2 （7B）已原生支持基于昇腾 NPU 的 PyTorch（2.1.0）+ Transformers（4.36.0）+ DeepSpeed（0.12.4）+ Accelerate（0.25.0）模型微调，无需额外适配即可使用。\n\n推理部署：Baichuan 2 （7B）已原生支持昇腾 NPU 推理，无需额外适配即可使用。\n\n### MindSpore 框架\n\n[MindFormers]( https:\u002F\u002Fgitee.com\u002Fmindspore\u002Fmindformers) 是一个基于昇思框架（MindSpore）并支持大模型训练、微调、评估、推理、部署的全流程开发套件，[Baichuan2-7B \u002F 13B]( https:\u002F\u002Fgitee.com\u002Fmindspore\u002Fmindformers\u002Ftree\u002Fdev\u002Fresearch\u002Fbaichuan2) 已集成于此套件，支持用户进行模型微调、部署，具体使用方式可见 [README]( https:\u002F\u002Fgitee.com\u002Fmindspore\u002Fmindformers\u002Ftree\u002Fdev\u002Fresearch\u002Fbaichuan2\u002Fbaichuan2.md)。\n\n### 大模型体验平台\n\n[昇思大模型平台](https:\u002F\u002Fxihe.mindspore.cn) 基于昇思 MindSpore AI 框架、MindFormers 大模型开发套件与昇腾硬件算力，将 [Baichuan2-7B](https:\u002F\u002Fxihe.mindspore.cn\u002Fmodelzoo\u002Fbaichuan2_7b_chat) 大模型能力开放给公众，欢迎大家在线体验。\n\n## LLaMA-高效微调\n[LLaMA-高效微调](https:\u002F\u002Fgithub.com\u002Fhiyouga\u002FLLaMA-Efficient-Tuning) 已支持对 Baichuan 2 模型的微调和继续训练。\n\n## 太初元碁\nBaichuan2（7B\u002F13B）支持太初 T100 加速卡推理，现[试用通道](http:\u002F\u002Fwww.tecorigin.com\u002Fcn\u002Fdeveloper.html)已正式对外开启。\n\n# 声明、协议、引用\n\n## 声明\n\n我们在此声明，我们的开发团队并未基于 Baichuan 2 模型开发任何应用，无论是在 iOS、Android、网页或任何其他平台。我们强烈呼吁所有使用者，不要利用 Baichuan 2 模型进行任何危害国家社会安全或违法的活动。另外，我们也要求使用者不要将 Baichuan 2 模型用于未经适当安全审查和备案的互联网服务。我们希望所有的使用者都能遵守这个原则，确保科技的发展能在规范和合法的环境下进行。\n\n我们已经尽我们所能，来确保模型训练过程中使用的数据的合规性。然而，尽管我们已经做出了巨大的努力，但由于模型和数据的复杂性，仍有可能存在一些无法预见的问题。因此，如果由于使用 Baichuan 2 开源模型而导致的任何问题，包括但不限于数据安全问题、公共舆论风险，或模型被误导、滥用、传播或不当利用所带来的任何风险和问题，我们将不承担任何责任。\n\n## 协议\n社区使用 Baichuan 2 模型需要遵循 [Apache 2.0](https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan2\u002Fblob\u002Fmain\u002FLICENSE) 和[《Baichuan 2 模型社区许可协议》](https:\u002F\u002Fhuggingface.co\u002Fbaichuan-inc\u002FBaichuan2-7B-Base\u002Fresolve\u002Fmain\u002FBaichuan%202%E6%A8%A1%E5%9E%8B%E7%A4%BE%E5%8C%BA%E8%AE%B8%E5%8F%AF%E5%8D%8F%E8%AE%AE.pdf)。Baichuan 2 模型支持商业用途，如果您计划将 Baichuan 2 模型或其衍生品用于商业目的，请您确认您的主体符合以下情况：\n  1. 您或您的关联方的服务或产品的日均用户活跃量（DAU）低于100万。\n  2. 您或您的关联方不是软件服务提供商、云服务提供商。\n  3. 您或您的关联方不存在将授予您的商用许可，未经百川许可二次授权给其他第三方的可能。\n\n在符合以上条件的前提下，您需要通过以下联系邮箱 opensource@baichuan-inc.com ，提交《Baichuan 2 模型社区许可协议》要求的申请材料。审核通过后，百川将特此授予您一个非排他性、全球性、不可转让、不可再许可、可撤销的商用版权许可。\n\n## 引用\n如需引用我们的工作，请使用如下 reference:\n```\n@article{baichuan2023baichuan2,\n  title={Baichuan 2: Open Large-scale Language Models},\n  author={Baichuan},\n  journal={arXiv preprint arXiv:2309.10305},\n  url={https:\u002F\u002Farxiv.org\u002Fabs\u002F2309.10305},\n  year={2023}\n}\n```","# Baichuan2 快速上手指南\n\nBaichuan2 是百川智能推出的新一代开源大语言模型，基于 2.6 万亿 Tokens 高质量语料训练，提供 7B 和 13B 两种尺寸的基座（Base）与对话（Chat）版本。本指南将帮助您快速完成环境配置并运行模型。\n\n## 环境准备\n\n### 系统要求\n- **操作系统**: Linux (推荐 Ubuntu 20.04+) 或 macOS\n- **GPU**: NVIDIA GPU，显存建议：\n  - 7B 模型 (FP16): 至少 16GB\n  - 13B 模型 (FP16): 至少 24GB\n  - 若使用 4bits 量化版本，显存需求可降低约 50%\n- **CUDA**: 11.8 或更高版本\n- **Python**: 3.8 及以上版本\n\n### 前置依赖\n确保已安装以下基础库：\n```bash\npip install torch torchvision torchaudio --index-url https:\u002F\u002Fdownload.pytorch.org\u002Fwhl\u002Fcu118\npip install transformers>=4.34.0 accelerate sentencepiece protobuf\n```\n\n> **提示**：国内开发者推荐使用清华或阿里镜像源加速安装：\n> ```bash\n> pip install -i https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple torch torchvision torchaudio --extra-index-url https:\u002F\u002Fdownload.pytorch.org\u002Fwhl\u002Fcu118\n> pip install -i https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple transformers accelerate sentencepiece protobuf\n> ```\n\n## 安装步骤\n\n### 方式一：通过 Hugging Face Transformers 直接加载（推荐）\n\n无需克隆仓库，直接使用 `transformers` 库加载模型。\n\n```python\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\nimport torch\n\n# 配置模型路径（支持国内 ModelScope 镜像）\nmodel_name = \"baichuan-inc\u002FBaichuan2-7B-Chat\" \n# 若网络受限，可使用 ModelScope 加载（需安装 modelscope）\n# from modelscope import snapshot_download\n# model_path = snapshot_download('baichuan-inc\u002FBaichuan2-7B-Chat')\n\ntokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)\nmodel = AutoModelForCausalLM.from_pretrained(\n    model_name, \n    device_map=\"auto\", \n    trust_remote_code=True,\n    torch_dtype=torch.float16  # 显存不足可改为 torch.bfloat16 或使用 4bits 版本\n)\n```\n\n### 方式二：使用 4bits 量化版本（低显存方案）\n\n若显存有限，推荐使用 4bits 量化版本：\n\n```python\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\nimport torch\n\nmodel_name = \"baichuan-inc\u002FBaichuan2-7B-Chat-4bits\"\n\ntokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)\nmodel = AutoModelForCausalLM.from_pretrained(\n    model_name, \n    device_map=\"auto\", \n    trust_remote_code=True,\n    torch_dtype=torch.float16\n)\n```\n\n## 基本使用\n\n以下是最简单的对话示例：\n\n```python\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\nimport torch\n\nmodel_name = \"baichuan-inc\u002FBaichuan2-7B-Chat\"\ntokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)\nmodel = AutoModelForCausalLM.from_pretrained(model_name, device_map=\"auto\", trust_remote_code=True, torch_dtype=torch.float16)\n\nmessages = [\n    {\"role\": \"user\", \"content\": \"你好，请介绍一下你自己。\"}\n]\n\ninputs = tokenizer.apply_chat_template(messages, return_tensors=\"pt\").to(model.device)\noutputs = model.generate(inputs, max_new_tokens=512, do_sample=True, temperature=0.7, top_p=0.9)\nresponse = tokenizer.decode(outputs[0], skip_special_tokens=True)\n\nprint(response)\n```\n\n### 多轮对话示例\n\n```python\nmessages = [\n    {\"role\": \"user\", \"content\": \"什么是人工智能？\"},\n    {\"role\": \"assistant\", \"content\": \"人工智能是模拟人类智能的技术...\"},\n    {\"role\": \"user\", \"content\": \"它有哪些应用场景？\"}\n]\n\ninputs = tokenizer.apply_chat_template(messages, return_tensors=\"pt\").to(model.device)\noutputs = model.generate(inputs, max_new_tokens=512, do_sample=True, temperature=0.7, top_p=0.9)\nresponse = tokenizer.decode(outputs[0], skip_special_tokens=True)\n\nprint(response)\n```\n\n> **注意**：\n> - `trust_remote_code=True` 必须设置，因为 Baichuan2 使用了自定义代码。\n> - 若使用 13B 模型，请将 `model_name` 替换为 `\"baichuan-inc\u002FBaichuan2-13B-Chat\"`。\n> - 生成参数可根据实际需求调整 `temperature`（创造性）和 `max_new_tokens`（输出长度）。","某中型电商公司的技术团队正急需构建一个能精准理解中文语境、处理复杂客服指令并辅助内部数据分析的智能助手。\n\n### 没有 Baichuan2 时\n- **中文理解生硬**：通用模型对“亲，这个包邮吗”等本土化电商术语理解偏差大，常答非所问，导致客户满意度下降。\n- **逻辑推理薄弱**：面对涉及多步计算的促销规则（如“满 300 减 50 再打九折”），模型经常算错金额，无法提供准确建议。\n- **部署成本高昂**：高精度模型显存占用过大，公司现有服务器资源难以支撑实时并发请求，需额外采购昂贵硬件。\n- **商用授权风险**：开源协议限制严格，团队担心直接商用会引发法律纠纷，迟迟不敢将原型系统上线。\n\n### 使用 Baichuan2 后\n- **地道中文交互**：Baichuan2 基于 2.6 万亿 Tokens 高质量语料训练，完美识别电商黑话与口语表达，回复自然且精准。\n- **复杂指令跟随**：升级版模型大幅强化数学与逻辑能力，能准确拆解多重优惠算法，即时生成无误的价格清单。\n- **高效量化部署**：利用 Baichuan2 提供的 4bits 量化版本，在保持性能的同时显著降低显存需求，顺利在现有设备上高并发运行。\n- **免费商用许可**：通过邮件申请即可获得官方免费商用授权，消除了法律顾虑，助力项目快速从测试走向生产环境。\n\nBaichuan2 凭借卓越的中文基座能力、强大的逻辑推理升级以及友好的商用政策，成为企业构建低成本、高效率智能应用的首选引擎。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbaichuan-inc_Baichuan2_7e0e4df6.png","baichuan-inc","Baichuan Intelligent Technology","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002Fbaichuan-inc_0ae0f3f0.png","",null,"opensource@baichuan-inc.com","https:\u002F\u002Fgithub.com\u002Fbaichuan-inc",[80],{"name":81,"color":82,"percentage":83},"Python","#3572A5",100,4112,293,"2026-04-14T01:15:52","Apache-2.0","未说明",{"notes":90,"python":88,"dependencies":91},"提供的 README 内容主要包含模型介绍、基准测试（Benchmark）结果及更新日志，未包含具体的运行环境需求（如操作系统、GPU\u002F内存配置、Python 版本及依赖库列表）。文中提到提供了 4bits 量化版本以降低资源需求，并指出商用需申请许可，但缺乏技术部署的具体参数说明。",[],[13,35,93,15],"其他",[95,96,97,98,99,100,101,102,103,104,105,106],"artificial-intelligence","benchmark","ceval","chatgpt","chinese","gpt","gpt-4","huggingface","large-language-models","llama2","mmlu","natural-language-processing","2026-03-27T02:49:30.150509","2026-04-15T07:13:34.607823",[110,115,120,125,130,134],{"id":111,"question_zh":112,"answer_zh":113,"source_url":114},33877,"使用 Baichuan2-13B-Chat-4bits 模型时遇到 \"No module named 'scipy'\" 或量化初始化报错怎么办？","这是依赖版本不兼容导致的。请严格按照以下命令安装特定版本的 bitsandbytes 和 scipy：\npip install bitsandbytes==0.41.1\npip install scipy\n注意：bitsandbytes 版本不能过低（如 0.37 缺少 Param4bit）也不能过高（如 0.43 实测不行），必须锁定在 0.41.1 版本。","https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan2\u002Fissues\u002F35",{"id":116,"question_zh":117,"answer_zh":118,"source_url":119},33878,"Baichuan2 Chat 模型在多轮对话中使用的 Prompt Template 格式是什么？","百川模型使用两个保留 token 来区分用户和模型：Token ID 195（对应明文 \u003Creserved_106>）代表用户输入开始，Token ID 196（对应明文 \u003Creserved_107>）代表模型输出开始。\n具体格式取决于 Tokenizer 的实现方式：\n1. 如果支持字符串 + token_id 混合方式：\n   系统 prompt \\n [195] + 用户输入 1 + [196] + 模型输出 1 + [195] + 用户输入 2 + [196] + 模型输出 2 ... + [196]\n2. 如果仅支持纯明文方式：\n   系统 prompt \\n \u003Creserved_106> + 用户输入 1 + \u003Creserved_107> + 模型输出 1 + \u003Creserved_106> + 用户输入 2 + \u003Creserved_107> + 模型输出 2 ... + \u003Creserved_107>\n注意：若自行实现 Tokenizer（如 llama.cpp），需测试是否能正确将 \u003Creserved_106> 和 \u003Creserved_107> 序列化为 195 和 196，避免编码不对齐。官方示例参考：https:\u002F\u002Fmodelscope.cn\u002Fmodels\u002Fbaichuan-inc\u002FBaichuan2-13B-Chat\u002Ffile\u002Fview\u002Fmaster\u002Fgeneration_utils.py","https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan2\u002Fissues\u002F227",{"id":121,"question_zh":122,"answer_zh":123,"source_url":124},33879,"如何对 Baichuan2 模型进行 4-bit 离线量化并保存模型？","官方提供的 4-bit 模型并非直接使用 bitsandbytes 的 save_pretrained 保存（因为 bitsandbytes 原生不支持保存 4-bit 权重且直接保存 state_dict 会导致维度错误）。推荐的解决方案是使用 AutoGPTQ 进行量化和保存。\n虽然早期 AutoGPTQ 未完全继承 Baichuan 架构，但后续版本已通过 PR #164 增加支持。请使用更新后的 AutoGPTQ 库对 baichuan2-13b-base 或 chat 模型进行量化处理。","https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan2\u002Fissues\u002F127",{"id":126,"question_zh":127,"answer_zh":128,"source_url":129},33880,"LoRA 微调后加载模型进行推理时出现 Hugging Face Hub 验证错误或路径错误怎么办？","在使用 PEFT 加载 LoRA 微调后的模型时，如果直接传入 LoRA 适配器的路径（output_dir\u002Fcheckpoint-xxx）到 from_pretrained，可能会因为该目录下缺少基础模型配置文件而触发 Hugging Face 的 repo_id 验证错误或加载失败。\n正确的做法是：确保加载逻辑指向包含 adapter_config.json 的完整检查点目录，并且代码能够自动识别并加载对应的基础模型（base_model_name_or_path 通常在 adapter_config.json 中指定）。如果手动加载，需先加载基座模型，再使用 PeftModel.from_pretrained(model, lora_path) 的方式挂载适配器，而不是直接用 AutoPeftModelForCausalLM 从局部路径加载所有配置。","https:\u002F\u002Fgithub.com\u002Fbaichuan-inc\u002FBaichuan2\u002Fissues\u002F112",{"id":131,"question_zh":132,"answer_zh":133,"source_url":114},33881,"为什么安装了 bitsandbytes 仍然报 \"Needs import model weight init func to run quantize\" 错误？","该错误通常是因为 bitsandbytes 版本不正确。即使安装了该库，如果版本不是 0.41.1，量化初始化函数可能无法被正确导入或识别。请务必卸载当前版本并重新安装指定版本：\npip uninstall bitsandbytes\npip install bitsandbytes==0.41.1\n同时确保已安装 scipy 依赖。",{"id":135,"question_zh":136,"answer_zh":137,"source_url":119},33882,"在自定义 Tokenizer 中如何正确处理 Baichuan2 的特殊对话标记？","Baichuan2 依赖特殊的保留 Token（195 和 196）来构建对话结构。在自定义或第三方 Tokenizer（如 llama.cpp 服务端实现）中，必须确保：\u003Creserved_106> 能被准确映射为 ID 195，\u003Creserved_107> 能被准确映射为 ID 196。\n如果在预处理或编码过程中这些特殊字符串被错误转换或未对齐，会导致模型无法识别对话角色，从而产生乱码或逻辑错误。建议在正式使用前编写测试脚本，验证 tokenizer.encode(\"\u003Creserved_106>\") 是否返回 [195]。",[]]