Convert Figma logo to code with AI

baichuan-inc logoBaichuan-7B

A large-scale 7B pretraining language model developed by BaiChuan-Inc.

5,685
504
5,685
90

Top Related Projects

An implementation of model parallel autoregressive transformers on GPUs, based on the Megatron and DeepSpeed libraries

ChatGLM-6B: An Open Bilingual Dialogue Language Model | 开源双语对话语言模型

Quick Overview

Baichuan-7B is an open-source large language model (LLM) with 7 billion parameters, developed by Baichuan Intelligence. It is designed to understand and generate human-like text in both Chinese and English, offering a powerful foundation for various natural language processing tasks.

Pros

  • Open-source and freely available for research and commercial use
  • Bilingual capabilities in Chinese and English
  • Competitive performance compared to other 7B parameter models
  • Extensive training on diverse datasets, including web pages, books, and code

Cons

  • Limited documentation and examples available in English
  • Relatively new project, which may lead to potential instability or bugs
  • Requires significant computational resources for fine-tuning or deployment
  • May have biases or limitations inherent to its training data

Getting Started

To use Baichuan-7B, follow these steps:

  1. Install the required dependencies:
pip install transformers torch
  1. Load the model and tokenizer:
from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan-7B", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan-7B", trust_remote_code=True)
  1. Generate text:
input_text = "Tell me a short story about a robot."
input_ids = tokenizer.encode(input_text, return_tensors="pt")
output = model.generate(input_ids, max_length=100, num_return_sequences=1)
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
print(generated_text)

Note: Ensure you have sufficient GPU memory to load and run the model. If you encounter memory issues, consider using a smaller model or running on a machine with more resources.

Competitor Comparisons

An implementation of model parallel autoregressive transformers on GPUs, based on the Megatron and DeepSpeed libraries

Pros of GPT-NeoX

  • More extensive documentation and examples for training and fine-tuning
  • Larger community support and contributions
  • Designed for distributed training across multiple GPUs

Cons of GPT-NeoX

  • Higher computational requirements for training
  • More complex setup and configuration process
  • Less focus on multilingual capabilities compared to Baichuan-7B

Code Comparison

GPT-NeoX configuration example:

{
    "num_layers": 32,
    "hidden_size": 6144,
    "num_attention_heads": 64,
    "seq_length": 2048,
    "max_position_embeddings": 2048,
    "norm": "layernorm",
    "pos_emb": "rotary",
    "rotary_pct": 0.25,
    "no_weight_tying": true,
    "gpt_j_residual": true,
    "output_layer_parallelism": "column"
}

Baichuan-7B configuration example:

{
    "hidden_size": 4096,
    "num_attention_heads": 32,
    "num_hidden_layers": 32,
    "rms_norm_eps": 1e-6,
    "vocab_size": 64000
}

The code comparison shows that GPT-NeoX offers more detailed configuration options, including specific settings for position embeddings and parallelism. Baichuan-7B's configuration is simpler and more straightforward, focusing on essential model parameters.

ChatGLM-6B: An Open Bilingual Dialogue Language Model | 开源双语对话语言模型

Pros of ChatGLM-6B

  • Smaller model size (6B parameters) potentially leading to faster inference and lower resource requirements
  • Extensive documentation and examples for various use cases and deployment scenarios
  • Strong support for Chinese language tasks and multilingual capabilities

Cons of ChatGLM-6B

  • Slightly older release date compared to Baichuan-7B, which may incorporate more recent advancements
  • Limited fine-tuning options and tools compared to Baichuan-7B's comprehensive training pipeline

Code Comparison

ChatGLM-6B:

from transformers import AutoTokenizer, AutoModel

tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()

Baichuan-7B:

from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan-7B", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan-7B", trust_remote_code=True)

Both repositories provide similar code for loading and using the models, with minor differences in the model class and initialization parameters. ChatGLM-6B explicitly converts the model to half-precision and moves it to CUDA, while Baichuan-7B leaves these optimizations to the user's discretion.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Baichuan-7B

🤗 Hugging Face • 🤖 ModelScope • 💬 WeChat

license

中文 | English

更新信息

  • [2023.09.06] 我们发布了新一代开源模型 Baichuan 2,包含 7B、13B 尺寸 🔥🔥🔥

介绍

Baichuan-7B 是由百川智能开发的一个开源可商用的大规模预训练语言模型。基于 Transformer 结构,在大约 1.2 万亿 tokens 上训练的 70 亿参数模型,支持中英双语,上下文窗口长度为 4096。在标准的中文和英文 benchmark(C-Eval/MMLU)上均取得同尺寸最好的效果。

公开benchmark榜单

中文评测

C-Eval

C-Eval 数据集是一个全面的中文基础模型评测数据集,涵盖了 52 个学科和四个难度的级别。我们使用该数据集的 dev 集作为 few-shot 的来源,在 test 集上进行了 5-shot 测试。通过执行执行下面的命令:

cd evaluation
python evaluate_zh.py --model_name_or_path 'your/model/path'

结果

Model 5-shotAverageAvg(Hard)STEMSocial SciencesHumanitiesOthers
GPT-468.754.967.177.664.567.8
ChatGPT54.441.452.961.850.953.6
Claude-v1.354.239.051.961.752.153.7
Claude-instant-v1.045.935.543.153.844.245.4
BLOOMZ-7B35.725.831.343.536.635.6
ChatGLM-6B34.523.130.439.637.434.5
Ziya-LLaMA-13B-pretrain30.222.727.734.432.028.9
moss-moon-003-base (16B)27.424.527.029.127.226.9
LLaMA-7B-hf27.125.927.126.827.926.3
Falcon-7B25.824.325.826.025.825.6
TigerBot-7B-base25.727.027.324.723.426.1
Aquila-7B*25.525.225.624.625.226.6
Open-LLaMA-v2-pretrain (7B)24.022.523.125.325.223.2
BLOOM-7B22.820.221.823.323.923.3
Baichuan-7B42.831.538.252.046.239.3

Gaokao

Gaokao 是一个以中国高考题作为评测大语言模型能力的数据集,用以评估模型的语言能力和逻辑推理能力。 我们只保留了其中的单项选择题,随机划分后对所有模型进行统一 5-shot 测试。

结果

以下是测试的结果。

ModelAverage
BLOOMZ-7B28.72
LLaMA-7B27.81
BLOOM-7B26.96
TigerBot-7B-base25.94
Falcon-7B23.98
Ziya-LLaMA-13B-pretrain23.17
ChatGLM-6B21.41
Open-LLaMA-v2-pretrain21.41
Aquila-7B*24.39
Baichuan-7B36.24

AGIEval

AGIEval 旨在评估模型的认知和解决问题相关的任务中的一般能力。 我们只保留了其中的四选一单项选择题,随机划分后对所有模型进行了统一 5-shot 测试。

结果

ModelAverage
BLOOMZ-7B30.27
LLaMA-7B28.17
Ziya-LLaMA-13B-pretrain27.64
Falcon-7B27.18
BLOOM-7B26.55
Aquila-7B*25.58
TigerBot-7B-base25.19
ChatGLM-6B23.49
Open-LLaMA-v2-pretrain23.49
Baichuan-7B34.44

*其中 Aquila 模型来源于智源官方网站(https://model.baai.ac.cn/model-detail/100098) 仅做参考

英文榜单

除了中文之外,Baichuan-7B也测试了模型在英文上的效果,MMLU 是包含 57 个多选任务的英文评测数据集,涵盖了初等数学、美国历史、计算机科学、法律等,难度覆盖高中水平到专家水平,是目前主流的LLM评测数据集。我们采用了开源 的评测方案,最终 5-shot 结果如下所示:

结果

ModelHumanitiesSocial SciencesSTEMOtherAverage
ChatGLM-6B035.441.031.340.536.9
BLOOMZ-7B031.342.134.439.036.1
mpt-7B1----35.6
LLaMA-7B234.038.330.538.135.1
Falcon-7B1----35.0
moss-moon-003-sft (16B)030.533.829.334.431.9
BLOOM-7B025.024.426.526.425.5
moss-moon-003-base (16B)024.222.822.424.423.6
Baichuan-7B038.448.935.648.142.3

0: 重新复现
1: https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard
2: https://paperswithcode.com/sota/multi-task-language-understanding-on-mmlu

复现方法

git clone https://github.com/hendrycks/test
cd test
wget https://people.eecs.berkeley.edu/~hendrycks/data.tar
tar xf data.tar
mkdir results
cp ../evaluate_mmlu.py .
python evaluate_mmlu.py -m /path/to/Baichuan-7B

其中在 MMLU 上57个任务的具体细指标如下图:

其中各个学科的指标如下图:

推理方法

推理代码已经在官方 Huggingface 库

from transformers import AutoModelForCausalLM, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan-7B", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan-7B", device_map="auto", trust_remote_code=True)
inputs = tokenizer('登鹳雀楼->王之涣\n夜雨寄北->', return_tensors='pt')
inputs = inputs.to('cuda:0')
pred = model.generate(**inputs, max_new_tokens=64,repetition_penalty=1.1)
print(tokenizer.decode(pred.cpu()[0], skip_special_tokens=True))

数据

  • 原始数据包括开源的中英文数据和自行抓取的中文互联网数据,以及部分高质量知识性数据。
  • 参考相关数据工作,频率和质量是数据处理环节重点考虑的两个维度。 我们基于启发式规则和质量模型打分,对原始数据集进行篇章和句子粒度的过滤。在全量数据上,利用局部敏感哈希方法,对篇章和句子粒度做滤重。

整体流程如下所示:



  • 经过不断的调整和多轮测试,最终确认了一个在下游任务上表现最好的中英文配比。
  • 我们使用了一个基于自动学习的数据权重策略,对不同类别的数据进行配比。

分词

我们参考学术界方案使用 SentencePiece 中的 Byte-Pair Encoding (BPE) 作为分词算法,并且进行了以下的优化:

  1. 目前大部分开源模型主要基于英文优化,因此对中文语料存在效率较低的问题。我们使用 2000 万条以中英为主的多语言语料训练分词模型,显著提升对于中文的压缩率。
  2. 对于数学领域,我们参考了 LLaMA 和 Galactica 中的方案,对数字的每一位单独分开,避免出现数字不一致的问题,对于提升数学能力有重要帮助。
  3. 对于罕见字词(如特殊符号等),支持 UTF-8 characters 的 byte 编码,因此做到未知字词的全覆盖。
  4. 我们分析了不同分词器对语料的压缩率,如下表,可见我们的分词器明显优于 LLaMA, Falcon 等开源模型,并且对比其他中文分词器在压缩率相当的情况下,训练和推理效率更高。
ModelBaichuan-7BLLaMAFalconmpt-7BChatGLMmoss-moon-003
Compress Rate0.7371.3121.0491.2060.6310.659
Vocab Size64,00032,00065,02450,254130,344106,029

模型结构

整体模型基于标准的 Transformer 结构,我们采用了和 LLaMA 一样的模型设计

  • 位置编码:rotary-embedding 是现阶段被大多模型采用的位置编码方案,具有更好的外延效果。虽然训练过程中最大长度为4096,但是实际测试中模型可以很好的扩展到 5000 tokens 以上,如下图:

  • 激活层:SwiGLU, Feedforward 变化为 8/3 倍的隐含层大小,即 11,008
  • Layer-Normalization: 基于 RMSNorm 的 Pre-Normalization

训练稳定性和吞吐

我们在原本的 LLaMA 框架上进行诸多修改以提升训练时的吞吐,具体包括:

  1. 算子优化技术:采用更高效算子,如 Flash-Attention,NVIDIA apex 的 RMSNorm 等。
  2. 算子切分技术:将部分计算算子进行切分,减小内存峰值。
  3. 混合精度技术:降低在不损失模型精度的情况下加速计算过程。
  4. 训练容灾技术:训练平台和训练框架联合优化,IaaS + PaaS 实现分钟级的故障定位和任务恢复。
  5. 通信优化技术,具体包括:
    1. 采用拓扑感知的集合通信算法,避免网络拥塞问题,提高通信效率。
    2. 根据卡数自适应设置 bucket size,提高带宽利用率。
    3. 根据模型和集群环境,调优通信原语的触发时机,从而将计算和通信重叠。

基于上述的几个优化技术,我们在千卡 A800 显卡上达到了 7B 模型 182 TFLOPS 的吞吐,GPU 峰值算力利用率高达 58.3%。

最终的loss如下图:

训练方法

安装依赖

pip install -r requirements.txt

准备数据

用户将训练语料按总rank数的倍数均匀切分成多个 UTF-8 文本文件,放置在语料目录(默认为 data_dir )下。各个rank进程将会读取语料目录下的不同文件,全部加载到内存后,开始后续训练过程。以上是简化的示范流程,建议用户在正式训练任务中,根据需求调整数据生产逻辑。

下载 tokenizer 模型

下载 tokenizer 模型文件 tokenizer.model ,放置在项目目录下。

配置 DeepSpeed

本示范代码采用 DeepSpeed 框架进行训练。用户需根据集群情况,修改 config/hostfile ,如果是多机多卡,需要修改 ssh 中各个节点的 IP 配置。具体可以参见 DeepSpeed 官方说明 。

执行训练

scripts/train.sh

协议

对本仓库源码的使用遵循开源许可协议 Apache 2.0。

Baichuan-7B 支持商用。如果将 Baichuan-7B 模型或其衍生品用作商业用途,请您按照如下方式联系许可方,以进行登记并向许可方申请书面授权:联系邮箱:opensource@baichuan-inc.com, 具体许可协议可见《Baichuan-7B 模型许可协议》。

Third-Party Resources

  1. LLaMA Efficient Tuning 支持Baichuan-7B使用Qlora进行Finetune,支持RLHF,支持WebDemo。使用经过sft的模型见 hiyouga/baichuan-7b-sft。
  2. fireballoon/baichuan-vicuna-chinese-7b 使用 ShareGPT, ShareGPT-ZH, COT & COT-ZH, Leetcode, dummy等包含中英文的数据Finetune后的模型,训练代码参考FastChat。
  3. fireballoon/baichuan-vicuna-7b 使用ShareGPT, COT 和 Leetcode等数据混合Finetune后的模型,训练代码参考FastChat。
  4. Efficient-Tuning-LLMs 支持Baichuan-7B使用Qlora进行Finetune和4bit inference。
  5. fastllm fastllm是纯c++实现,无第三方依赖的大模型库,支持Baichuan-7B在手机端运行。
  6. TheBloke/baichuan-7B-GPTQ 对Baichuan-7B的GPTQ 4bit量化。

Star History

Star History Chart