{"id":14959650,"url":"https://github.com/baichuan-inc/baichuan-7b","last_synced_at":"2025-04-09T02:18:18.037Z","repository":{"id":175373714,"uuid":"653595084","full_name":"baichuan-inc/Baichuan-7B","owner":"baichuan-inc","description":"A large-scale 7B pretraining language model developed by BaiChuan-Inc.","archived":false,"fork":false,"pushed_at":"2024-07-18T14:23:01.000Z","size":4012,"stargazers_count":5670,"open_issues_count":90,"forks_count":506,"subscribers_count":67,"default_branch":"main","last_synced_at":"2024-10-29T14:50:43.017Z","etag":null,"topics":["artificial-intelligence","ceval","chatgpt","chinese","gpt-4","huggingface","large-language-models","llama","mmlu","natural-language-processing"],"latest_commit_sha":null,"homepage":"https://huggingface.co/baichuan-inc/baichuan-7B","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/baichuan-inc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-14T10:57:51.000Z","updated_at":"2024-10-29T04:01:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"48be2a32-d555-42f0-bb53-69adbb73a37f","html_url":"https://github.com/baichuan-inc/Baichuan-7B","commit_stats":{"total_commits":63,"total_committers":13,"mean_commits":4.846153846153846,"dds":"0.39682539682539686","last_synced_commit":"6f3ef4633a90c2d8a3e0763d0dec1b8dc11588f5"},"previous_names":["baichuan-inc/baichuan-7b"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baichuan-inc%2FBaichuan-7B","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baichuan-inc%2FBaichuan-7B/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baichuan-inc%2FBaichuan-7B/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baichuan-inc%2FBaichuan-7B/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/baichuan-inc","download_url":"https://codeload.github.com/baichuan-inc/Baichuan-7B/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247962605,"owners_count":21024871,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["artificial-intelligence","ceval","chatgpt","chinese","gpt-4","huggingface","large-language-models","llama","mmlu","natural-language-processing"],"created_at":"2024-09-24T13:20:29.175Z","updated_at":"2025-04-09T02:18:18.019Z","avatar_url":"https://github.com/baichuan-inc.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!-- markdownlint-disable first-line-h1 --\u003e\n\u003c!-- markdownlint-disable html --\u003e\n\n\u003cdiv align=\"center\"\u003e\n\u003ch1\u003e\n  Baichuan-7B\n\u003c/h1\u003e\n\u003c/div\u003e\n\n\u003cp align=\"center\"\u003e\n🤗 \u003ca href=\"https://huggingface.co/baichuan-inc/Baichuan-7B\" target=\"_blank\"\u003eHugging Face\u003c/a\u003e • 🤖 \u003ca href=\"https://modelscope.cn/organization/baichuan-inc\" target=\"_blank\"\u003eModelScope\u003c/a\u003e • 💬 \u003ca href=\"https://github.com/baichuan-inc/Baichuan-7B/blob/main/media/wechat.jpeg?raw=true\" target=\"_blank\"\u003eWeChat\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cdiv align=\"center\"\u003e\n\n[![license](https://img.shields.io/github/license/modelscope/modelscope.svg)](https://github.com/baichuan-inc/Baichuan-7B/blob/main/LICENSE)\n\u003ch4 align=\"center\"\u003e\n    \u003cp\u003e\n        \u003cb\u003e中文\u003c/b\u003e |\n        \u003ca href=\"https://github.com/baichuan-inc/Baichuan-7B/blob/main/README_EN.md\"\u003eEnglish\u003c/a\u003e\n    \u003cp\u003e\n\u003c/h4\u003e\n\u003c/div\u003e\n\n# 更新信息\n- [2023.09.06] 我们发布了新一代开源模型 [Baichuan 2](https://github.com/baichuan-inc/Baichuan2)，包含 7B、13B 尺寸 🔥🔥🔥\n\n# 介绍\n\nBaichuan-7B 是由百川智能开发的一个开源可商用的大规模预训练语言模型。基于 Transformer 结构，在大约 1.2 万亿 tokens 上训练的 70 亿参数模型，支持中英双语，上下文窗口长度为 4096。在标准的中文和英文 benchmark（C-Eval/MMLU）上均取得同尺寸最好的效果。\n\n# 公开benchmark榜单\n\n## 中文评测\n\n### C-Eval\n\n[C-Eval 数据集](https://cevalbenchmark.com/index.html)是一个全面的中文基础模型评测数据集，涵盖了 52 个学科和四个难度的级别。我们使用该数据集的 dev 集作为 few-shot 的来源，在 test 集上进行了 `5-shot` 测试。通过执行执行下面的命令：\n\n```bash\ncd evaluation\npython evaluate_zh.py --model_name_or_path 'your/model/path'\n```\n\n### 结果\n\n|        Model 5-shot         | Average | Avg(Hard) | STEM  | Social Sciences | Humanities | Others |\n| :-------------------------: | :-----: | :-------: | :---: | :-------------: | :--------: | :----: |\n|            GPT-4            |  68.7   |   54.9    | 67.1  |      77.6       |    64.5    |  67.8  |\n|           ChatGPT           |  54.4   |   41.4    | 52.9  |      61.8       |    50.9    |  53.6  |\n|         Claude-v1.3         |  54.2   |   39.0    | 51.9  |      61.7       |    52.1    |  53.7  |\n|     Claude-instant-v1.0     |  45.9   |   35.5    | 43.1  |      53.8       |    44.2    |  45.4  |\n|          BLOOMZ-7B          |  35.7   |   25.8    | 31.3  |      43.5       |    36.6    |  35.6  |\n|         ChatGLM-6B          |  34.5   |   23.1    | 30.4  |      39.6       |    37.4    |  34.5  |\n|   Ziya-LLaMA-13B-pretrain   |  30.2   |   22.7    | 27.7  |      34.4       |    32.0    |  28.9  |\n|  moss-moon-003-base (16B)   |  27.4   |   24.5    | 27.0  |      29.1       |    27.2    |  26.9  |\n|         LLaMA-7B-hf         |  27.1   |   25.9    | 27.1  |      26.8       |    27.9    |  26.3  |\n|          Falcon-7B          |  25.8   |   24.3    | 25.8  |      26.0       |    25.8    |  25.6  |\n|      TigerBot-7B-base       |  25.7   |   27.0    | 27.3  |      24.7       |    23.4    |  26.1  |\n|    Aquila-7B\u003csup\u003e*\u003c/sup\u003e    |  25.5   |   25.2    | 25.6  |      24.6       |    25.2    |  26.6  |\n| Open-LLaMA-v2-pretrain (7B) |  24.0   |   22.5    | 23.1  |      25.3       |    25.2    |  23.2  |\n|          BLOOM-7B           |  22.8   |   20.2    | 21.8  |      23.3       |    23.9    |  23.3  |\n|       **Baichuan-7B**       |  42.8   |   31.5    | 38.2  |      52.0       |    46.2    |  39.3  |\n\n### Gaokao\n\n[Gaokao](https://github.com/OpenLMLab/GAOKAO-Bench) 是一个以中国高考题作为评测大语言模型能力的数据集，用以评估模型的语言能力和逻辑推理能力。\n我们只保留了其中的单项选择题，随机划分后对所有模型进行统一 `5-shot` 测试。\n\n### 结果\n\n以下是测试的结果。\n\n|          Model          |  Average  |\n| :---------------------: | :-------: |\n|        BLOOMZ-7B        |   28.72   |\n|        LLaMA-7B         |   27.81   |\n|        BLOOM-7B         |   26.96   |\n|    TigerBot-7B-base     |   25.94   |\n|        Falcon-7B        |   23.98   |\n| Ziya-LLaMA-13B-pretrain |   23.17   |\n|       ChatGLM-6B        |   21.41   |\n| Open-LLaMA-v2-pretrain  |   21.41   |\n|  Aquila-7B\u003csup\u003e*\u003c/sup\u003e  |   24.39   |\n|     **Baichuan-7B**     | **36.24** |\n\n### AGIEval\n\n[AGIEval](https://github.com/microsoft/AGIEval) 旨在评估模型的认知和解决问题相关的任务中的一般能力。\n我们只保留了其中的四选一单项选择题，随机划分后对所有模型进行了统一 `5-shot` 测试。\n\n### 结果\n\n|          Model          |  Average  |\n| :---------------------: | :-------: |\n|        BLOOMZ-7B        |   30.27   |\n|        LLaMA-7B         |   28.17   |\n| Ziya-LLaMA-13B-pretrain |   27.64   |\n|        Falcon-7B        |   27.18   |\n|        BLOOM-7B         |   26.55   |\n|  Aquila-7B\u003csup\u003e*\u003c/sup\u003e  |   25.58   |\n|    TigerBot-7B-base     |   25.19   |\n|       ChatGLM-6B        |   23.49   |\n| Open-LLaMA-v2-pretrain  |   23.49   |\n|     **Baichuan-7B**     | **34.44** |\n\n\u003csup\u003e*\u003c/sup\u003e其中 Aquila 模型来源于智源官方网站(\u003chttps://model.baai.ac.cn/model-detail/100098\u003e) 仅做参考\n\n## 英文榜单\n\n除了中文之外，Baichuan-7B也测试了模型在英文上的效果，[MMLU](https://arxiv.org/abs/2009.03300) 是包含 57 个多选任务的英文评测数据集，涵盖了初等数学、美国历史、计算机科学、法律等，难度覆盖高中水平到专家水平，是目前主流的LLM评测数据集。我们采用了[开源](https://github.com/hendrycks/test) 的评测方案，最终 `5-shot` 结果如下所示：\n\n### 结果\n\n|                Model                 | Humanities | Social Sciences |   STEM   |  Other   | Average  |\n| :----------------------------------: | :--------: | :-------------: | :------: | :------: | :------: |\n|        ChatGLM-6B\u003csup\u003e0\u003c/sup\u003e        |    35.4    |      41.0       |   31.3   |   40.5   |   36.9   |\n|        BLOOMZ-7B\u003csup\u003e0\u003c/sup\u003e         |    31.3    |      42.1       |   34.4   |   39.0   |   36.1   |\n|          mpt-7B\u003csup\u003e1\u003c/sup\u003e          |     -      |        -        |    -     |    -     |   35.6   |\n|         LLaMA-7B\u003csup\u003e2\u003c/sup\u003e         |    34.0    |      38.3       |   30.5   |   38.1   |   35.1   |\n|        Falcon-7B\u003csup\u003e1\u003c/sup\u003e         |     -      |        -        |    -     |    -     |   35.0   |\n| moss-moon-003-sft (16B)\u003csup\u003e0\u003c/sup\u003e  |    30.5    |      33.8       |   29.3   |   34.4   |   31.9   |\n|         BLOOM-7B\u003csup\u003e0\u003c/sup\u003e         |    25.0    |      24.4       |   26.5   |   26.4   |   25.5   |\n| moss-moon-003-base (16B)\u003csup\u003e0\u003c/sup\u003e |    24.2    |      22.8       |   22.4   |   24.4   |   23.6   |\n|     **Baichuan-7B\u003csup\u003e0\u003c/sup\u003e**      |  **38.4**  |    **48.9**     | **35.6** | **48.1** | **42.3** |\n\n\u003csup\u003e0: 重新复现\u003c/sup\u003e\u003cbr/\u003e\n\u003csup\u003e1: https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard\u003c/sup\u003e\u003cbr/\u003e\n\u003csup\u003e2: https://paperswithcode.com/sota/multi-task-language-understanding-on-mmlu\u003c/sup\u003e\u003cbr/\u003e\n\n### 复现方法\n\n```bash\ngit clone https://github.com/hendrycks/test\ncd test\nwget https://people.eecs.berkeley.edu/~hendrycks/data.tar\ntar xf data.tar\nmkdir results\ncp ../evaluate_mmlu.py .\npython evaluate_mmlu.py -m /path/to/Baichuan-7B\n```\n\n其中在 MMLU 上57个任务的具体细指标如下图：\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"media/MMLU-57-tasks.png\" width=\"90%\"/\u003e\n\u003c/p\u003e\n\n其中各个学科的指标如下图：\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"media/MMLU 21 Subjects.png\" width=\"90%\"/\u003e\n\u003c/p\u003e\n\n# 推理方法\n\n推理代码已经在[官方 Huggingface 库](https://huggingface.co/baichuan-inc/Baichuan-7B)\n\n```python\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\n\ntokenizer = AutoTokenizer.from_pretrained(\"baichuan-inc/Baichuan-7B\", trust_remote_code=True)\nmodel = AutoModelForCausalLM.from_pretrained(\"baichuan-inc/Baichuan-7B\", device_map=\"auto\", trust_remote_code=True)\ninputs = tokenizer('登鹳雀楼-\u003e王之涣\\n夜雨寄北-\u003e', return_tensors='pt')\ninputs = inputs.to('cuda:0')\npred = model.generate(**inputs, max_new_tokens=64,repetition_penalty=1.1)\nprint(tokenizer.decode(pred.cpu()[0], skip_special_tokens=True))\n\n```\n\n# 数据\n\n* 原始数据包括开源的中英文数据和自行抓取的中文互联网数据，以及部分高质量知识性数据。\n* 参考相关数据工作，频率和质量是数据处理环节重点考虑的两个维度。 我们基于启发式规则和质量模型打分，对原始数据集进行篇章和句子粒度的过滤。在全量数据上，利用局部敏感哈希方法，对篇章和句子粒度做滤重。\n\n整体流程如下所示：\n\u003cp align=\"center\"\u003e\n    \u003cbr\u003e\n    \u003cimg src=\"media/data_process.png\" width=\"90%\"/\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\n* 经过不断的调整和多轮测试，最终确认了一个在下游任务上表现最好的中英文配比。\n* 我们使用了一个基于自动学习的数据权重策略，对不同类别的数据进行配比。\n\n# 分词\n\n我们参考学术界方案使用 SentencePiece 中的 Byte-Pair Encoding (BPE) 作为分词算法，并且进行了以下的优化：\n\n1. 目前大部分开源模型主要基于英文优化，因此对中文语料存在效率较低的问题。我们使用 2000 万条以中英为主的多语言语料训练分词模型，显著提升对于中文的压缩率。\n2. 对于数学领域，我们参考了 LLaMA 和 Galactica 中的方案，对数字的每一位单独分开，避免出现数字不一致的问题，对于提升数学能力有重要帮助。\n3. 对于罕见字词（如特殊符号等），支持 UTF-8 characters 的 byte 编码，因此做到未知字词的全覆盖。\n4. 我们分析了不同分词器对语料的压缩率，如下表，可见我们的分词器明显优于 LLaMA, Falcon 等开源模型，并且对比其他中文分词器在压缩率相当的情况下，训练和推理效率更高。\n\n|     Model     | Baichuan-7B | LLaMA  | Falcon | mpt-7B | ChatGLM | moss-moon-003 |\n| :-----------: | :---------: | :----: | :----: | :----: | :-----: | :-----------: |\n| Compress Rate |    0.737    | 1.312  | 1.049  | 1.206  |  0.631  |     0.659     |\n|  Vocab Size   |   64,000    | 32,000 | 65,024 | 50,254 | 130,344 |    106,029    |\n\n# 模型结构\n\n整体模型基于标准的 Transformer 结构，我们采用了和 LLaMA 一样的模型设计\n\n* 位置编码：[rotary-embedding](https://arxiv.org/abs/2104.09864) 是现阶段被大多模型采用的位置编码方案，具有更好的外延效果。虽然训练过程中最大长度为4096，但是实际测试中模型可以很好的扩展到 5000 tokens 以上，如下图：\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"media/long-context-ppl.png\" width=\"90%\"/\u003e\n\u003c/p\u003e\n\n* 激活层：SwiGLU, Feedforward 变化为 8/3 倍的隐含层大小，即 11,008\n* Layer-Normalization: 基于 [RMSNorm](https://arxiv.org/abs/1910.07467) 的 Pre-Normalization\n\n# 训练稳定性和吞吐\n\n我们在原本的 LLaMA 框架上进行诸多修改以提升训练时的吞吐，具体包括：\n\n1. 算子优化技术：采用更高效算子，如 Flash-Attention，NVIDIA apex 的 RMSNorm 等。\n2. 算子切分技术：将部分计算算子进行切分，减小内存峰值。\n3. 混合精度技术：降低在不损失模型精度的情况下加速计算过程。\n4. 训练容灾技术：训练平台和训练框架联合优化，IaaS + PaaS 实现分钟级的故障定位和任务恢复。\n5. 通信优化技术，具体包括：\n   1. 采用拓扑感知的集合通信算法，避免网络拥塞问题，提高通信效率。\n   2. 根据卡数自适应设置 bucket size，提高带宽利用率。\n   3. 根据模型和集群环境，调优通信原语的触发时机，从而将计算和通信重叠。\n\n基于上述的几个优化技术，我们在千卡 A800 显卡上达到了 7B 模型 182 TFLOPS 的吞吐，GPU 峰值算力利用率高达 58.3%。\n\n最终的loss如下图：\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"media/7b.loss.png\" width=\"90%\"/\u003e\n\u003c/p\u003e\n\n# 训练方法\n\n## 安装依赖\n\n```bash\npip install -r requirements.txt\n```\n\n## 准备数据\n\n用户将训练语料按总rank数的倍数均匀切分成多个 UTF-8 文本文件，放置在语料目录（默认为 `data_dir` ）下。各个rank进程将会读取语料目录下的不同文件，全部加载到内存后，开始后续训练过程。以上是简化的示范流程，建议用户在正式训练任务中，根据需求调整数据生产逻辑。\n\n## 下载 tokenizer 模型\n\n下载 tokenizer 模型文件 [tokenizer.model](https://huggingface.co/baichuan-inc/Baichuan-7B/blob/main/tokenizer.model) ，放置在项目目录下。\n\n## 配置 DeepSpeed\n\n本示范代码采用 DeepSpeed 框架进行训练。用户需根据集群情况，修改 `config/hostfile` ，如果是多机多卡，需要修改 ssh 中各个节点的 IP 配置。具体可以参见 DeepSpeed [官方说明](https://www.deepspeed.ai/) 。\n\n## 执行训练\n\n```python\nscripts/train.sh\n```\n\n# 协议\n\n对本仓库源码的使用遵循开源许可协议 [Apache 2.0](https://github.com/baichuan-inc/Baichuan-7B/blob/main/LICENSE)。\n\nBaichuan-7B 支持商用。如果将 Baichuan-7B 模型或其衍生品用作商业用途，请您按照如下方式联系许可方，以进行登记并向许可方申请书面授权：联系邮箱：\u003copensource@baichuan-inc.com\u003e， 具体许可协议可见[《Baichuan-7B 模型许可协议》](https://huggingface.co/baichuan-inc/Baichuan-7B/resolve/main/baichuan-7B%20%E6%A8%A1%E5%9E%8B%E8%AE%B8%E5%8F%AF%E5%8D%8F%E8%AE%AE.pdf)。\n\n# Third-Party Resources\n\n1. [LLaMA Efficient Tuning](https://github.com/hiyouga/LLaMA-Efficient-Tuning) 支持Baichuan-7B使用Qlora进行Finetune，支持RLHF，支持WebDemo。使用经过sft的模型见 [hiyouga/baichuan-7b-sft](https://huggingface.co/hiyouga/baichuan-7b-sft)。\n2. [fireballoon/baichuan-vicuna-chinese-7b](https://huggingface.co/fireballoon/baichuan-vicuna-chinese-7b) 使用 ShareGPT, ShareGPT-ZH, COT \u0026 COT-ZH, Leetcode, dummy等包含中英文的数据Finetune后的模型，训练代码参考FastChat。\n3. [fireballoon/baichuan-vicuna-7b](https://huggingface.co/fireballoon/baichuan-vicuna-7b) 使用ShareGPT, COT 和 Leetcode等数据混合Finetune后的模型，训练代码参考FastChat。\n4. [Efficient-Tuning-LLMs](https://github.com/jianzhnie/Efficient-Tuning-LLMs) 支持Baichuan-7B使用Qlora进行Finetune和4bit inference。\n5. [fastllm](https://github.com/ztxz16/fastllm) fastllm是纯c++实现，无第三方依赖的大模型库，支持Baichuan-7B在手机端运行。\n6. [TheBloke/baichuan-7B-GPTQ](https://huggingface.co/TheBloke/baichuan-7B-GPTQ) 对Baichuan-7B的GPTQ 4bit量化。\n\n# Star History\n\n[![Star History Chart](https://api.star-history.com/svg?repos=baichuan-inc/Baichuan-7B\u0026type=Date)](https://star-history.com/#baichuan-inc/Baichuan-7B\u0026Date)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaichuan-inc%2Fbaichuan-7b","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbaichuan-inc%2Fbaichuan-7b","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaichuan-inc%2Fbaichuan-7b/lists"}