百川模型第二波,魔搭最佳实践教程来了!

本文涉及的产品
模型在线服务 PAI-EAS,A10/V100等 500元 1个月
模型训练 PAI-DLC,5000CU*H 3个月
交互式建模 PAI-DSW,5000CU*H 3个月
简介: 百川智能宣布正式发布并开源Baichuan 2!开源包括Baichuan 2-7B、Baichuan 2-13B、Baichuan 2-13B-Chat与其4bit量化版本,并且均为免费可商用。

导读


今天,百川智能宣布正式发布并开源Baichuan 2!开源包括Baichuan 2-7B、Baichuan 2-13B、Baichuan 2-13B-Chat与其4bit量化版本,并且均为免费可商用。


Baichuan 2 是百川智能推出的新一代开源大语言模型,采用 2.6 万亿 Tokens 的高质量语料训练,在权威的中文和英文 benchmark上均取得同尺寸最好的效果。


Baichuan 2 目前在魔搭社区已全面开源上线,大家可以体验起来啦!小编特将最新鲜的魔搭体验、推理最佳实践奉上。



环境配置与安装


  1. python 3.8及以上版本
  2. pytorch 1.12及以上版本,推荐2.0及以上版本
  3. 建议使用CUDA 11.4及以上


使用步骤

本文主要演示的模型为Baichuan2-7B-Chat和Baichuan2-7B-Base模型,在ModelScope的Notebook的环境(这里以PAI-DSW为例)的配置下运行(显存24G) :


服务器连接与环境准备

1、进入ModelScope首页:modelscope.cn,进入我的Notebook


2、选择GPU环境,进入PAI-DSW在线开发环境


3、新建Notebook



创空间体验


创空间描述:

据官方公布的基准测试数据,Baichuan2-13B相比上一代13B模型,在数学能力(↑49%)、代码能力(↑46%)、安全能力(↑37%)、逻辑推理能力(↑25%)、语义理解能力(↑15%)上均有显著提升。


魔搭社区上线了 Baichuan2-13B-Chat的体验Demo(Baichuan2-13B-Chat为Baichuan2-13B系列模型中对齐后的版本),欢迎大家体验实际效果!


创空间链接:

https://modelscope.cn/studios/baichuan-inc/Baichuan-13B-Chatdemo/summary


晒出一些小编基于各维度随机抽问的一次性测试案例:

  • 国际惯例先上自我认知


  • 数学


  • 编程


  • 安全


  • 逻辑推理


                             

  • 语义理解



模型链接及下载



Baichuan2系列模型现已在ModelScope社区开源,包括:


百川2-7B-预训练模型:

https://modelscope.cn/models/baichuan-inc/Baichuan2-7B-Base/summary


百川2-7B-对话模型:

https://modelscope.cn/models/baichuan-inc/Baichuan2-7B-Chat/summary


百川2-7B-对话模型-int4量化版:

https://modelscope.cn/models/baichuan-inc/Baichuan2-7B-Chat-int4/summary


百川2-13B-预训练模型:

https://modelscope.cn/models/baichuan-inc/Baichuan2-13B-Base/summary


百川2-13B-对话模型:

https://modelscope.cn/models/baichuan-inc/Baichuan2-13B-Chat/summary


百川2-13B-对话模型-4bits量化版:

https://modelscope.cn/models/baichuan-inc/Baichuan2-13B-Chat-4bits/summary


百川2-7B-训练过程模型:

https://modelscope.cn/models/baichuan-inc/Baichuan2-7B-Intermediate-Checkpoints/summary


社区支持直接下载模型的repo:

from modelscope.hub.snapshot_download import snapshot_download
model_dir = snapshot_download('baichuan-inc/Baichuan2-7B-Chat', 'v1.0.0')



模型推理


推理代码:

import torch
from modelscope import (
    AutoModelForCausalLM, AutoTokenizer, GenerationConfig, snapshot_download
)
model_id = 'baichuan-inc/Baichuan2-7B-Chat'
revision = 'v1.0.0'
model_dir = snapshot_download(model_id, revision=revision)
tokenizer = AutoTokenizer.from_pretrained(model_dir, use_fast=False, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", 
                                             torch_dtype=torch.bfloat16, 
                                             trust_remote_code=True)
model.generation_config = GenerationConfig.from_pretrained(model_dir)
messages = []
messages.append({"role": "user", "content": "世界第一高峰是哪个"})
response = model.chat(tokenizer, messages)
print(response)


资源消耗:



模型微调和微调后推理



微调代码开源地址:

https://github.com/modelscope/swift/blob/main/examples/pytorch/llm


clone swift仓库并安装swift

git clone https://github.com/modelscope/swift.git
cd swift
pip install .
cd examples/pytorch/llm


模型微调脚本 (lora_ddp)

# 4 * 22GB VRAM
nproc_per_node=4
CUDA_VISIBLE_DEVICES=0,1,2,3 \
torchrun \
    --nproc_per_node=$nproc_per_node \
    --master_port 29500 \
    src/llm_sft.py \
    --model_type baichuan2-7b-chat \
    --sft_type lora \
    --template_type baichuan \
    --dtype bf16 \
    --output_dir runs \
    --ddp_backend nccl \
    --dataset alpaca-en,alpaca-zh \
    --dataset_sample 20000 \
    --num_train_epochs 1 \
    --max_length 1024 \
    --lora_rank 8 \
    --lora_alpha 32 \
    --lora_dropout_p 0.05 \
    --lora_target_modules W_pack o_proj \
    --gradient_checkpointing true \
    --batch_size 1 \
    --weight_decay 0. \
    --learning_rate 1e-4 \
    --gradient_accumulation_steps $(expr 16 / $nproc_per_node) \
    --max_grad_norm 0.5 \
    --warmup_ratio 0.03 \
    --eval_steps 50 \
    --save_steps 50 \
    --save_total_limit 2 \
    --logging_steps 10 \
    --push_to_hub false \
    --hub_model_id baichuan2-7b-chat-lora \
    --hub_private_repo true \
    --hub_token 'your-sdk-token' \


模型微调后的推理脚本

# 16G
CUDA_VISIBLE_DEVICES=0 \
python src/llm_infer.py \
    --model_type baichuan2-7b-chat \
    --sft_type lora \
    --template_type baichuan \
    --dtype bf16 \
    --ckpt_dir "runs/baichuan2-7b-chat/vx_xxx/checkpoint-xxx" \
    --eval_human true \
    --max_new_tokens 1024 \
    --temperature 0.9 \
    --top_k 50 \
    --top_p 0.9 \
    --do_sample true \


微调的可视化结果

训练损失:


评估损失


资源消耗:4 * 22G



点击链接直达Baichuan2-13B-Chat创空间体验

https://modelscope.cn/studios/baichuan-inc/Baichuan-13B-Chatdemo/summary

相关文章
|
4月前
|
缓存 API 开发者
魔搭社区牵手FastChat&vLLM,打造极致LLM模型部署体验
FastChat是一个开放平台,用于训练、服务和评估基于LLM的ChatBot。
|
4月前
|
人工智能 自然语言处理 机器人
Jina AI 发布中英和英德双语 8K 向量模型,魔搭社区开源最佳实践!
在 Jina Embeddings 英语向量模型突破百万下载后,今天,Jina AI正式开源了两款双语向量模型:中英双语(Chinese-English)和英德双语(English-German)向量模型,这也是全球首次推出支持 8K 双语文本的开源向量模型。
|
9月前
|
数据可视化 PyTorch 算法框架/工具
零一万物Yi-34B-Chat 微调模型及量化版开源!魔搭社区最佳实践教程!
11月24日,零一万物基正式发布并开源微调模型 Yi-34B-Chat,可申请免费商用。同时,零一万物还为开发者提供了 4bit/8bit 量化版模型,Yi-34B-Chat 4bit 量化版模型可以直接在消费级显卡(如RTX3090)上使用。魔搭社区已支持下载、推理训练体验,并推出相关教程,欢迎大家来玩!
|
4月前
|
物联网 机器人 Swift
|
4月前
|
数据采集 自然语言处理 前端开发
社区供稿 | 猎户星空百亿参数大模型 Orion-14B系列开源,一张3060就能跑(附魔搭社区推理微调最佳实践)
1月21日,傅盛在猎户星空大模型发布会上宣布,“为企业应用而生” 的开源百亿参数猎户星空大模型正式发布。猎户星空大模型(Orion-14B)是由猎户星空研发的预训练多语言大语言模型,以其140亿参数规模展现出了卓越的性能。
|
10月前
|
数据可视化 测试技术 PyTorch
智谱ChatGLM3魔搭最佳实践教程来了!
ChatGLM3-6B 是 ChatGLM 系列最新一代的开源模型,在保留了前两代模型对话流畅、部署门槛低等众多优秀特性的基础上
|
2月前
|
自然语言处理 API Android开发
阿里Qwen2-72B大模型已是开源榜的王者,为什么还要推出其他参数模型,被其他模型打榜?
阿里云的Qwen2-72B模型在Hugging Face上荣登开源模型榜首,展现卓越性能,超越其他包括Meta的Llama-3在内的竞争者。Qwen2有多个参数版本,其中72B版本在自然语言理解、知识、代码等任务上表现出色。较小参数版本如7B模型在某些方面略逊一筹。推出不同参数模型可能是为了降低成本、加速迭代、构建丰富的模型生态。通过提供不同规模的模型,阿里云旨在促进技术研究和全场景应用,类似于微软Windows XP和阿里云OS生态的构建策略。
235 1
|
2月前
|
人工智能 JSON 文字识别
开源VLM新标杆 InternVL 2.0 怎么用?部署、微调尽在魔搭社区!
7月4日下午,世界人工智能大会科学前沿论坛,上海人工智能实验室OpenGVLab发布了InternVL 2.0 版本,中文名书生·万象。
|
4月前
|
数据可视化 物联网 测试技术
零一万物Yi-1.5系列模型发布并开源!34B/9B/6B 多尺寸魔搭社区推理微调最佳实践教程来啦!
Yi-1.5是Yi的升级版本。 它使用 500B tokens的高质量语料库在 Yi 上持续进行预训练,并在 3M 个多样化的微调样本上进行微调。