Convert Figma logo to code with AI

baichuan-inc logoBaichuan-13B

A 13B large language model developed by Baichuan Intelligent Technology

2,951
236
2,951
96

Top Related Projects

59,042

Inference code for Llama models

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

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

7,842

A series of large language models trained from scratch by developers @01-ai

Welcome to the Llama Cookbook! This is your go to guide for Building with Llama: Getting started with Inference, Fine-Tuning, RAG. We also show you how to solve end to end problems using Llama model family and using them on various provider services

Quick Overview

Baichuan-13B is an open-source large language model (LLM) developed by Baichuan Intelligence. It is a 13 billion parameter model trained on a diverse multilingual dataset, with a focus on Chinese and English languages. The model aims to provide high-quality natural language processing capabilities for various applications.

Pros

  • Open-source and freely available for research and commercial use
  • Strong performance in both Chinese and English language tasks
  • Supports efficient inference on consumer-grade hardware
  • Actively maintained and updated by the Baichuan team

Cons

  • Limited documentation and examples compared to some other popular LLMs
  • May require fine-tuning for specific domain applications
  • Performance in languages other than Chinese and English may be less robust
  • Potential biases and limitations inherent to large language models

Getting Started

To use Baichuan-13B, 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-13B-Chat", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan-13B-Chat", trust_remote_code=True)
  1. Generate text:
inputs = tokenizer("Hello, how are you?", return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=50)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(response)

Note: This is a basic example. For more advanced usage and fine-tuning, refer to the project's documentation and the Hugging Face Transformers library documentation.

Competitor Comparisons

59,042

Inference code for Llama models

Pros of Llama

  • More extensive documentation and community support
  • Broader language support and multilingual capabilities
  • Higher flexibility for fine-tuning and customization

Cons of Llama

  • Larger model size, requiring more computational resources
  • Potentially slower inference time for certain tasks
  • More complex licensing and usage restrictions

Code Comparison

Baichuan-13B example:

from transformers import AutoTokenizer, AutoModelForCausalLM

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

Llama example:

from transformers import LlamaTokenizer, LlamaForCausalLM

tokenizer = LlamaTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
model = LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf")

Both repositories provide pre-trained language models, but they differ in their approach and focus. Llama offers a more versatile and widely-supported model, while Baichuan-13B is more specialized for certain tasks and languages. The code examples demonstrate similar usage patterns, with minor differences in model initialization and tokenizer selection.

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
  • Broader community support and contributions
  • Designed for distributed training across multiple GPUs and nodes

Cons of gpt-neox

  • Larger model size, requiring more computational resources
  • More complex setup and configuration process
  • Less focus on multilingual capabilities compared to Baichuan-13B

Code Comparison

gpt-neox:

from megatron import get_args
from megatron import print_rank_0
from megatron import get_tokenizer
from megatron import get_model
from megatron.training import train

train(model_provider=get_model,
      optimizer=None,
      model_type='GPT')

Baichuan-13B:

from transformers import AutoTokenizer, AutoModelForCausalLM

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

The code snippets highlight the different approaches to model initialization and training. gpt-neox uses a custom training loop with Megatron-LM, while Baichuan-13B leverages the Hugging Face Transformers library for easier integration and use.

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

Pros of ChatGLM-6B

  • Smaller model size (6B parameters) requires less computational resources
  • Designed for efficient inference on consumer-grade hardware
  • Supports both Chinese and English languages

Cons of ChatGLM-6B

  • Lower parameter count may result in less sophisticated responses
  • Limited fine-tuning options compared to Baichuan-13B
  • Less extensive training data, potentially affecting performance on diverse tasks

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-13B:

from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan-13B-Chat", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan-13B-Chat", trust_remote_code=True).half().cuda()

The code snippets show similar usage patterns, with minor differences in model loading and class names. Both repositories utilize the Hugging Face Transformers library for easy integration and deployment.

7,842

A series of large language models trained from scratch by developers @01-ai

Pros of Yi

  • More extensive documentation and examples provided in the repository
  • Offers pre-trained models in both 6B and 34B parameter sizes
  • Includes detailed model cards with performance metrics and benchmarks

Cons of Yi

  • Less community engagement and fewer third-party contributions
  • Limited multilingual support compared to Baichuan-13B
  • Fewer fine-tuned variants available for specific tasks

Code Comparison

Yi:

from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("01-ai/Yi-6B")
model = AutoModelForCausalLM.from_pretrained("01-ai/Yi-6B")

Baichuan-13B:

from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan-13B-Chat")
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan-13B-Chat")

Both repositories use similar code for loading models and tokenizers through the Hugging Face Transformers library. The main difference lies in the model names and sizes available. Yi offers 6B and 34B variants, while Baichuan-13B focuses on the 13B parameter size with chat-specific fine-tuning.

Welcome to the Llama Cookbook! This is your go to guide for Building with Llama: Getting started with Inference, Fine-Tuning, RAG. We also show you how to solve end to end problems using Llama model family and using them on various provider services

Pros of llama-cookbook

  • Comprehensive documentation and examples for working with LLaMA models
  • Broader scope, covering various aspects of LLM usage and fine-tuning
  • Active community support and regular updates

Cons of llama-cookbook

  • Not a standalone model, requires access to Meta's LLaMA models
  • May have higher computational requirements for running examples

Code Comparison

Baichuan-13B (model loading):

from transformers import AutoTokenizer, AutoModelForCausalLM

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

llama-cookbook (model loading):

from transformers import LlamaTokenizer, LlamaForCausalLM

tokenizer = LlamaTokenizer.from_pretrained("path/to/llama/tokenizer")
model = LlamaForCausalLM.from_pretrained("path/to/llama/model")

The main difference in code is that Baichuan-13B can be directly loaded from the Hugging Face model hub, while llama-cookbook requires local model files. Additionally, Baichuan-13B uses the trust_remote_code=True parameter for custom model implementations.

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-13B

🤗 Baichuan-13B-Base • 🤗 Baichuan-13B-Chat • 🤖 ModelScope • 💬 WeChat

license

中文 | English

更新信息

  • [2023.09.06] 我们发布了新一代开源模型 Baichuan 2,包含 7B、13B 尺寸 🔥🔥🔥
  • [2023.08.01] 更新了对齐模型 Baichuan-13B-Chat 权重,优化了部分场景的效果

目录

介绍

Baichuan-13B 是由百川智能继 Baichuan-7B 之后开发的包含 130 亿参数的开源可商用的大规模语言模型,在权威的中文和英文 benchmark 上均取得同尺寸最好的效果。本次发布包含有预训练 (Baichuan-13B-Base) 和对齐 (Baichuan-13B-Chat) 两个版本。Baichuan-13B 有如下几个特点:

  1. **更大尺寸、更多数据**:Baichuan-13B 在 Baichuan-7B 的基础上进一步扩大参数量到 130 亿,并且在高质量的语料上训练了 1.4 万亿 tokens,超过 LLaMA-13B 40%,是当前开源 13B 尺寸下训练数据量最多的模型。支持中英双语,使用 ALiBi 位置编码,上下文窗口长度为 4096。
  2. 同时开源预训练和对齐模型:预训练模型是适用开发者的『 基座 』,而广大普通用户对有对话功能的对齐模型具有更强的需求。因此本次开源我们同时发布了对齐模型(Baichuan-13B-Chat),具有很强的对话能力,开箱即用,几行代码即可简单的部署。
  3. 更高效的推理:为了支持更广大用户的使用,我们本次同时开源了 int8 和 int4 的量化版本,相对非量化版本在几乎没有效果损失的情况下大大降低了部署的机器资源门槛,可以部署在如 Nvidia 3090 这样的消费级显卡上。
  4. **开源免费可商用**:Baichuan-13B 不仅对学术研究完全开放,开发者也仅需邮件申请并获得官方商用许可后,即可以免费商用。

Benchmark结果

我们在各个权威大语言模型的中英文 benchmark 上进行了5-shot评测。结果如下:

C-Eval

Model 5-shotSTEMSocial SciencesHumanitiesOthersAverage
Baichuan-7B38.252.046.239.342.8
Chinese-Alpaca-Plus-13B35.245.640.038.238.8
Vicuna-13B30.538.232.532.532.8
Chinese-LLaMA-Plus-13B30.338.032.929.132.1
Ziya-LLaMA-13B-Pretrain27.634.432.028.630.0
LLaMA-13B27.033.627.727.628.5
moss-moon-003-base (16B)27.029.127.226.927.4
Baichuan-13B-Base45.963.557.249.352.4
Baichuan-13B-Chat43.764.656.249.251.5

MMLU

Model 5-shotSTEMSocial SciencesHumanitiesOthersAverage
Vicuna-13B40.460.549.558.452.0
LLaMA-13B36.153.044.052.846.3
Chinese-Alpaca-Plus-13B36.948.940.550.543.9
Ziya-LLaMA-13B-Pretrain35.647.640.149.442.9
Baichuan-7B35.648.938.448.142.3
Chinese-LLaMA-Plus-13B33.142.837.044.639.2
moss-moon-003-base (16B)22.422.824.224.423.6
Baichuan-13B-Base41.660.947.458.551.6
Baichuan-13B-Chat40.960.948.859.052.1

说明:我们采用了 MMLU 官方的评测方案。

CMMLU

Model 5-shotSTEMHumanitiesSocial SciencesOthersChina SpecificAverage
Baichuan-7B34.447.547.646.644.344.0
Vicuna-13B31.836.237.639.534.336.3
Chinese-Alpaca-Plus-13B29.833.433.237.932.133.4
Chinese-LLaMA-Plus-13B28.133.135.435.133.533.0
Ziya-LLaMA-13B-Pretrain29.030.733.834.431.932.1
LLaMA-13B29.230.831.633.030.531.2
moss-moon-003-base (16B)27.230.428.832.628.729.6
Baichuan-13B-Base41.761.159.859.056.455.3
Baichuan-13B-Chat42.862.659.759.056.155.8

说明:CMMLU 是一个综合性的中文评估基准,专门用于评估语言模型在中文语境下的知识和推理能力。我们采用了其官方的评测方案。

模型细节

模型名称隐藏层维度层数注意力头数词表大小总参数量训练数据(tokens)位置编码最大长度
Baichuan-7B4,096323264,0007,000,559,6161.2 万亿RoPE4,096
Baichuan-13B5,120404064,00013,264,901,1201.4 万亿ALiBi4,096

推理和部署

推理所需的模型权重、源码、配置已发布在 Hugging Face:Baichuan-13B-Base 和 Baichuan-13B-Chat。下面以 Baichuan-13B-Chat 为例示范多种推理方式。程序会自动从 Hugging Face 下载所需资源。

推理前请安装依赖:

pip install -r requirements.txt

Python代码方式

>>> import torch
>>> from transformers import AutoModelForCausalLM, AutoTokenizer
>>> from transformers.generation.utils import GenerationConfig
>>> tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan-13B-Chat", use_fast=False, trust_remote_code=True)
>>> model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan-13B-Chat", device_map="auto", torch_dtype=torch.float16, trust_remote_code=True)
>>> model.generation_config = GenerationConfig.from_pretrained("baichuan-inc/Baichuan-13B-Chat")
>>> messages = []
>>> messages.append({"role": "user", "content": "世界上第二高的山峰是哪座"})
>>> response = model.chat(tokenizer, messages)
>>> print(response)
乔戈里峰。世界第二高峰———乔戈里峰西方登山者称其为k2峰,海拔高度是8611米,位于喀喇昆仑山脉的中巴边境上

在上述代码中,模型加载指定 device_map='auto',会使用所有可用显卡。如需指定使用的设备,可以使用类似 export CUDA_VISIBLE_DEVICES=0,1(使用了0、1号显卡)的方式控制。

命令行工具方式

python cli_demo.py

最后输出示例如下:

网页 demo 方式

依靠streamlit运行以下命令,会在本地启动一个 web 服务,把控制台给出的地址放入浏览器即可访问。

streamlit run web_demo.py

效果如下:

Baichuan-13B-Chat 示例输出

内容创作
用户:
你是一个内容创作者,编写一段关于对马斯克和比尔盖茨访问中国视角的短视频脚本

Baichun-13B-Chat:
探索马斯克与比尔·盖茨的中国之旅

镜头1:(背景音乐)画面切入,展示中国的繁华都市景象。

旁白:欢迎来到我们的节目,今天我们将带您一起了解两位科技巨头——埃隆·马斯克和比尔·盖茨在中国的一次特殊访问。

镜头2:(背景音乐)画面切换至马斯克和比尔·盖茨抵达中国的机场。

旁白:这次访问是他们首次共同踏上中国的土地,他们将在这里展开一场关于科技创新、环保和教育的对话。

镜头3:(背景音乐)画面展示马斯克和比尔·盖茨参观一家电动汽车制造厂。

旁白:在这次访问中,他们首先参观了一家电动汽车制造厂,了解了中国在新能源汽车领域的最新发展。

镜头4:(背景音乐)画面展示马斯克和比尔·盖茨参加一场关于人工智能的论坛。

旁白:随后,他们参加了关于人工智能的一场论坛,探讨了这一领域的发展趋势及其对未来的影响。

镜头5:(背景音乐)画面展示马斯克和比尔·盖茨参观一所学校。

旁白:在这次访问中,他们还参观了一所学校,了解了中国在教育领域的创新实践。

镜头6:(背景音乐)画面展示马斯克和比尔·盖茨接受媒体采访。

旁白:最后,他们在媒体采访环节表示,希望通过这次访问,能够为中美两国在科技创新、环保和教育等领域的合作提供一些启示。

镜头7:(背景音乐)画面切回繁华都市景象。

旁白:这就是马斯克和比尔·盖茨的中国之行,他们的访问不仅让我们看到了中国在科技创新方面的巨大潜力,也为我们提供了关于未来发展的更多思考。感谢收看,我们下期再见!
广告文案
用户:
请以销售员的角色,将洗衣液的干净、无残留、无异味以及除菌等优点进行推销

Baichun-13B-Chat:
尊敬的顾客,您好!我是一名洗衣液销售员,非常高兴为您介绍我们这款产品。我们的洗衣液具有以下几个显著优点:

干净:我们的洗衣液采用了先进的洗涤技术,能够深入衣物纤维,有效去除各种污渍,让您的衣物焕然一新。同时,它不会对衣物的颜色和质地造成任何损害,让您的衣物保持原有的品质。

无残留:与传统的洗衣粉相比,我们的洗衣液在清洗过程中不会产生过多的泡沫,因此清洗后的衣物更加柔软,不会有黏腻的感觉。此外,它的去污能力强,能彻底洗净衣物上的污渍,避免了传统洗衣粉容易残留在衣物上的问题。

无异味:我们的洗衣液采用天然植物提取物作为原料,不含有害化学物质,因此在使用过程中不会产生刺鼻的气味。即使是敏感肌肤的人群也可以放心使用,让您的衣物散发出淡雅的清香。

除菌:为了满足现代人对健康生活的需求,我们的洗衣液特别添加了高效除菌成分,能够有效杀灭衣物上的细菌,保护您和家人的身体健康。无论是婴幼儿的衣物还是贴身内衣,都可以放心使用。

总之,我们的洗衣液凭借其干净、无残留、无异味以及除菌等优点,成为了广大消费者的首选。如果您还没有尝试过我们的产品,那么现在就是最好的时机。相信它一定会给您带来全新的洗衣体验!
用户:
准备一份以宠物食品为主题的报告,包括潜在消费者,市场规模,市场机遇,及盈利模式等内容


Baichun-13B-Chat:

一、引言

随着人们生活水平的提高和对宠物的关爱程度不断加深,宠物食品行业得到了快速发展。本报告将对宠物食品行业的潜在消费者、市场规模、市场机遇以及盈利模式等方面进行分析,以期为相关企业提供有益的参考信息。

二、潜在消费者

1. 养宠人群:随着养宠人数的增加,宠物食品的需求也在不断增长。根据统计数据显示,2019年中国城镇养宠(犬猫)人口数量已达7355万人,同比增长4.6%。预计到2023年,这一数字将达到8742万人。

2. 新生代消费者:新生代消费者在消费观念上更加注重健康、环保和个性化,这使得他们更愿意为宠物购买高品质的食品。此外,他们也更倾向于通过网络平台了解和购买宠物食品。

三、市场规模

1. 根据国家统计局数据,2019年我国宠物食品市场规模达到了1,020亿元人民币,同比增长约10%。预计到2023年,市场规模将达到1,  500亿元人民币。

2. 从产品类型来看,宠物干粮市场占比最高,约占总市场的70%;其次是宠物湿粮,占比约为20%;宠物零食市场占比约为10%。

四、市场机遇

1. 功能性宠物食品:随着消费者对健康的关注度不断提高,具有特定功能的宠物食品如处方粮、减肥粮等需求逐渐增大。

2. 天然有机宠物食品:越来越多的消费者倾向于选择天然、无添加剂的宠物食品,这也为市场带来了新的商机。

3. 定制化宠物食品:针对不同品种、年龄、体型的宠物,推出定制化的宠物食品,满足消费者个性化需求。

五、盈利模式

1. 传统销售渠道:通过超市、宠物店、电商平台等传统渠道销售宠物食品。

2. 直销模式:通过自建网站或与第三方平台合作,直接向消费者销售宠物食品。

3. 会员制:通过设立会员制度,为会员提供专属优惠和服务,增加客户粘性。

4. 增值服务:提供宠物美容、寄养、训练等附加服务,实现多元化盈利。

六、结论

宠物食品行业具有巨大的市场潜力和发展空间。企业应抓住市场机遇,关注消费者需求变化,创新产品
精准问答
用户:
世界上第二高的山是什么山

Baichun-13B-Chat:
乔戈里峰(K2) 海拔8611米,位于喀喇昆仑山脉的中巴边境上,是中国以外登山者的最大考验,也是世界上最难征服的山峰之一。它的高度仅次于珠穆朗玛峰,位居世界第二。
用户:
意兴阑珊是什么意思

Baichun-13B-Chat:
"意兴阑珊"是一个汉语成语,它的意思是形容一个人兴致不高,兴趣减弱。这个词通常用来描述一个人在某个活动中失去了原有的热情和兴趣。
用户:
请问强化学习里面的Reward Shaping是什么意思

Baichun-13B-Chat:
奖励塑造(Reward Shaping)是强化学习中的一个概念,它是指通过调整环境或设计特定的奖励函数来引导智能体(Agent)学习到更符合人类期望的行为。在传统的强化学习中,智能体通过与环境交互来学习最优策略,但这种方法可能导致智能体学习到一些次优甚至不良行为。

奖励塑造的目的是为了解决这个问题,通过人为地调整奖励函数,使得智能体更容易学到我们希望它学会的行为。这可以通过以下几种方式实现:

1. 增加正向奖励:当智能体表现出期望行为时,给予其更多的奖励,以鼓励这种行为。
2. 减少负向奖励:当智能体表现出不期望行为时,减少对其进行惩罚,降低其对这种行为的偏好。
3. 引入新奖励:为智能体提供额外的奖励,以便更好地引导其学习特定行为。
4. 改变奖励分布:通过调整奖励函数,使其具有不同的奖励分布,从而影响智能体的学习过程。

总之,奖励塑造是一种通过调整环境和奖励函数来引导智能体学习更好行为的方法,有助于提高强化学习的性能。
语言理解
用户:
小张给领导买了一个礼物说,说“意思一下”,领导说“什么意思”,小张回答说:“没什么意思,就是随便意思意思”。请依次解读一下小张说的这几个“意思”的含义。

Baichun-13B-Chat:
1. 第一个“意思”:指的是小张送给领导的那个礼物,表示他已经买了这个礼物并准备送给他。
2. 第二个“意思”:指的是小张对领导的一种礼貌和尊重,表示自己并没有忘记他的身份和地位,在购买礼物时也考虑到了这一点。
3. 第三个“意思”:指的是小张对于这次赠送礼物的行为本身的态度,即不希望这件事情引起过多的关注或争议,只是想表达一种心意而已。

推理性能

Baichuan-13B 使用了 ALiBi 线性偏置技术,相对于 Rotary Embedding 计算量更小,对推理性能有显著提升;与标准的 LLaMA-13B 相比,平均推理速度 (tokens/s) 实测提升 31.6%:

Modeltokens/s
LLaMA-13B19.4
Baichuan-13B25.4

测试环境和参数:GPU A100-SXM4-80G, PyTorch 2.0.0+cu117, transformers 4.29.1, batch size = 1, 生成长度 = 2048, 精度 fp16, 基于 Baichuan-13B-Base

量化部署

Baichuan-13B 支持 int8 和 int4 量化,用户只需在推理代码中简单修改两行即可实现。

使用量化的用户请务必注意!

请仔细阅读接下来的的示例代码,尤其是第一行模型加载部分,和上面的推理示例是不同的。

开发者可以按照自己的需求修改模型的加载方式,但是请注意:如果是为了节省显存而进行量化,应加载原始精度模型到 CPU 后再开始量化;避免在from_pretrained时添加device_map='auto'或者其它会导致把原始精度模型直接加载到 GPU 的行为的参数。

如需使用 int8 量化:

model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan-13B-Chat", torch_dtype=torch.float16, trust_remote_code=True)
model = model.quantize(8).cuda() 

同样的,如需使用 int4 量化:

model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan-13B-Chat", torch_dtype=torch.float16, trust_remote_code=True)
model = model.quantize(4).cuda()

另外,如果你不想调用 quantize 在线量化,我们有量化好的 int8 Chat 模型可供使用:Baichuan-13B-Chat-int8:

model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan-13B-Chat-int8", torch_dtype=torch.float16, trust_remote_code=True).cuda()

量化前后占用显存情况如下:

PrecisionGPU Mem (GB)
bf16 / fp1626.0
int815.8
int49.7

量化后在各个 benchmark 上的结果和原始版本对比如下:

Model 5-shotC-EvalMMLUCMMLU
Baichuan-13B-Base52.451.655.3
Baichuan-13B-Base-int851.249.954.5
Baichuan-13B-Base-int447.646.051.0

CPU 部署

Baichuan-13B 支持 CPU 推理,但需要强调的是,CPU 的推理速度相对较慢。需按如下方式修改模型加载的方式:

model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan-13B-Chat", torch_dtype=torch.float32, trust_remote_code=True)

使用CPU进行推理大概需要 60GB 内存。

对模型进行微调

开发者可以对 Baichuan-13B-Base 或 Baichuan-13B-Chat 进行微调使用。在此我们测试了与 Baichuan-13B 兼容的微调工具 LLaMA Efficient Tuning,并给出全量微调和 LoRA微调的两种示范。

在开始之前,开发者需下载 LLaMA Efficient Tuning 项目并按其要求安装依赖。

输入数据为放置在项目data目录下的 json 文件,用--dataset选项指定(参考下面示例),多个输入文件用,分隔。json 文件示例格式和字段说明如下:

[
    {
        "instruction": "What are the three primary colors?",
        "input": "",
        "output": "The three primary colors are red, blue, and yellow."
    },
    ....
]

json 文件中存储一个列表,列表的每个元素是一个 sample。其中instruction代表用户输入,input是可选项,如果开发者同时指定了instruction和input,会把二者用\n连接起来代表用户输入;output代表期望的模型输出。

下面我们给出两种微调场景下测试跑通的示范脚本。

全量微调

我们在 8 * Nvidia A100 80 GB + deepspeed 的环境下进行了全量微调测试。

训练启动脚本示例:

deepspeed --num_gpus=8 src/train_bash.py \
    --stage sft \
    --model_name_or_path baichuan-inc/Baichuan-13B-Base \
    --do_train \
    --dataset alpaca_gpt4_en,alpaca_gpt4_zh \
    --finetuning_type full \
    --output_dir path_to_your_sft_checkpoint \
    --overwrite_cache \
    --per_device_train_batch_size 4 \ 
    --per_device_eval_batch_size 4 \ 
    --gradient_accumulation_steps 8 \ 
    --preprocessing_num_workers 16 \
    --lr_scheduler_type cosine \
    --logging_steps 10 \
    --save_steps 100 \
    --eval_steps 100 \
    --learning_rate 5e-5 \
    --max_grad_norm 0.5 \
    --num_train_epochs 2.0 \
    --dev_ratio 0.01 \
    --evaluation_strategy steps \
    --load_best_model_at_end \
    --plot_loss \
    --fp16 \
    --deepspeed deepspeed.json

deep_speed.json 配置示例:

{
  "train_micro_batch_size_per_gpu": "auto",
  "zero_allow_untested_optimizer": true,
  "fp16": {
    "enabled": "auto",
    "loss_scale": 0,
    "initial_scale_power": 16, 
    "loss_scale_window": 1000,
    "hysteresis": 2,
    "min_loss_scale": 1
  },  
  "zero_optimization": {
    "stage": 2,
    "allgather_partitions": true,
    "allgather_bucket_size": 5e8,
    "overlap_comm": false,
    "reduce_scatter": true,
    "reduce_bucket_size": 5e8,
    "contiguous_gradients" : true
  }
}

LoRA微调

我们在单张 Nvidia A100 80G 显卡上进行了 LoRA 微调测试。

训练启动脚本示例:

CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \
    --stage sft \
    --model_name_or_path baichuan-inc/Baichuan-13B-Base \
    --do_train \
    --dataset alpaca_gpt4_en,alpaca_gpt4_zh \
    --finetuning_type lora \
    --lora_rank 8 \ 
    --lora_target W_pack \
    --output_dir path_to_your_sft_checkpoint \
    --overwrite_cache \
    --per_device_train_batch_size 4 \ 
    --per_device_eval_batch_size 4 \ 
    --gradient_accumulation_steps 8 \ 
    --preprocessing_num_workers 16 \
    --lr_scheduler_type cosine \
    --logging_steps 10 \
    --save_steps 100 \
    --eval_steps 100 \
    --learning_rate 5e-5 \
    --max_grad_norm 0.5 \
    --num_train_epochs 2.0 \
    --dev_ratio 0.01 \
    --evaluation_strategy steps \
    --load_best_model_at_end \
    --plot_loss \
    --fp16

关于使用 LLaMA Efficient Tuning 的更详细的用法,请参阅其项目主页说明。

声明

我们在此声明,我们的开发团队并未基于 Baichuan-13B 模型开发任何应用,无论是在 iOS、Android、网页或任何其他平台。我们强烈呼吁所有使用者,不要利用 Baichuan-13B 模型进行任何危害国家社会安全或违法的活动。另外,我们也要求使用者不要将 Baichuan-13B 模型用于未经适当安全审查和备案的互联网服务。我们希望所有的使用者都能遵守这个原则,确保科技的发展能在规范和合法的环境下进行。

我们已经尽我们所能,来确保模型训练过程中使用的数据的合规性。然而,尽管我们已经做出了巨大的努力,但由于模型和数据的复杂性,仍有可能存在一些无法预见的问题。因此,如果由于使用 Baichuan-13B 开源模型而导致的任何问题,包括但不限于数据安全问题、公共舆论风险,或模型被误导、滥用、传播或不当利用所带来的任何风险和问题,我们将不承担任何责任。

协议

对本仓库源码的使用遵循开源许可协议 Apache 2.0。对 Baichuan-13B 模型的社区使用见《Baichuan-13B 模型社区许可协议》。Baichuan-13B 支持商用。如果将 Baichuan-13B 模型或其衍生品用作商业用途,请您按照如下方式联系许可方,以进行登记并向许可方申请书面授权:联系邮箱 opensource@baichuan-inc.com。