CoolFace
Modelpublic

AlpachinoNLP/Baichuan-7B-Instruction

sourceHugging Faceupdated 1y agoView on Hugging Face
2likes40downloads
Model Card

Baichuan-7B-Instruction

[image]

<!-- Provide a quick summary of what the model is/does. -->

介绍

Baichuan-7B-Instruction 为 Baichuan-7B 系列模型进行指令微调后的版本,预训练模型可见 Baichuan-7B。

Demo

如下是一个使用 gradio 的模型 demo

python
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("AlpachinoNLP/Baichuan-7B-Instruction",trust_remote_code=True,use_fast=False)
model = AutoModelForCausalLM.from_pretrained("AlpachinoNLP/Baichuan-7B-Instruction",trust_remote_code=True ).half()
model.cuda()

def generate(histories,  max_new_tokens=2048, do_sample = True, top_p = 0.95, temperature = 0.35, repetition_penalty=1.1):
    prompt = ""
    for history in histories:
        history_with_identity = "\nHuman:" + history[0] + "\n\nAssistant:" + history[1]
        prompt += history_with_identity
    input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)
    outputs = model.generate(
                    input_ids = input_ids,
                    max_new_tokens=max_new_tokens,
                    early_stopping=True,
                    do_sample=do_sample,
                    top_p=top_p, 
                    temperature=temperature,
                    repetition_penalty=repetition_penalty,
        )
    rets = tokenizer.batch_decode(outputs, skip_special_tokens=True)
    generate_text = rets[0].replace(prompt, "")
    return generate_text
    
with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    msg = gr.Textbox()
    clear = gr.Button("clear")

    def user(user_message, history):
        return "", history + [[user_message, ""]]

    def bot(history):
        print(history)
        bot_message = generate(history)
        history[-1][1] = bot_message
        return history

    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, chatbot, chatbot
    )
    clear.click(lambda: None, None, chatbot, queue=False)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0")


量化部署

Baichuan-7B 支持 int8 和 int4 量化,用户只需在推理代码中简单修改两行即可实现。请注意,如果是为了节省显存而进行量化,应加载原始精度模型到 CPU 后再开始量化;避免在 from_pretrained 时添加 device_map='auto' 或者其它会导致把原始精度模型直接加载到 GPU 的行为的参数。

使用 int8 量化 (To use int8 quantization):

python
model = AutoModelForCausalLM.from_pretrained("AlpachinoNLP/Baichuan-7B-Instruction", torch_dtype=torch.float16, trust_remote_code=True)
model = model.quantize(8).cuda() 

同样的,如需使用 int4 量化 (Similarly, to use int4 quantization):

python
model = AutoModelForCausalLM.from_pretrained("AlpachinoNLP/Baichuan-7B-Instruction", torch_dtype=torch.float16, trust_remote_code=True)
model = model.quantize(4).cuda()

训练详情

数据集:https://huggingface.co/datasets/shareAI/ShareGPT-Chinese-English-90k。

硬件:8*A40

测评结果

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
Baichuan-13B-Instruction44.5061.1659.0758.3455.5555.61
Baichuan-7B-Instruction34.6847.3847.1345.1144.5143.57
Model zero-shotSTEMHumanitiesSocial SciencesOthersChina SpecificAverage
ChatGLM2-6B41.2852.8553.3752.2450.5849.95
Baichuan-7B32.7944.4346.7844.7943.1142.33
ChatGLM-6B32.2242.9144.8142.6041.9340.79
BatGPT-15B33.7236.5338.0746.9438.3238.51
Chinese-LLaMA-7B26.7626.5727.4228.3326.7327.34
MOSS-SFT-16B25.6826.3527.2127.9226.7026.88
Chinese-GLM-10B25.5725.0126.3325.9425.8125.80
Baichuan-13B42.0460.4959.5556.6055.7254.63
Baichuan-13B-Chat37.3256.2454.7954.0752.2350.48
Baichuan-13B-Instruction42.5662.0960.4158.9756.9555.88
Baichuan-7B-Instruction33.9446.3147.7345.8444.8843.53
说明:CMMLU 是一个综合性的中文评估基准,专门用于评估语言模型在中文语境下的知识和推理能力。我们直接使用其官方的评测脚本对模型进行评测。Model zero-shot 表格中 Baichuan-13B-Chat 的得分来自我们直接运行 CMMLU 官方的评测脚本得到,其他模型的的得分来自于 CMMLU 官方的评测结果.

英文能力评测

除了中文榜单的测试,我们同样测试了模型在英文榜单 MMLU 上的能力。

MMLU

MMLU 是一个包含了57种任务的英文评测数据集。 我们采用了开源的评测方案) , 评测结果如下:

ModelHumanitiesSocial SciencesSTEMOtherAverage
LLaMA-7B<sup>2</sup>34.038.330.538.135.1
Falcon-7B<sup>1</sup>----35.0
mpt-7B<sup>1</sup>----35.6
ChatGLM-6B<sup>0</sup>35.441.031.340.536.9
BLOOM 7B<sup>0</sup>25.024.426.526.425.5
BLOOMZ 7B<sup>0</sup>31.342.134.439.036.1
moss-moon-003-base (16B)<sup>0</sup>24.222.822.424.423.6
moss-moon-003-sft (16B)<sup>0</sup>30.533.829.334.431.9
Baichuan-7B<sup>0</sup>38.448.935.648.142.3
Baichuan-7B-Instruction(5-shot)38.949.035.348.842.6
Baichuan-7B-Instruction(0-shot)38.747.934.548.242.0