潍坊专业网站建设价格,青海做高端网站建设的公司,百度给做网站公司,2022年最近十大新闻前言
使用XTuner 微调个人小助手认知
一、下载模型
#安装魔搭依赖包
pip install modelscope新建download.py内容如下 其中Shanghai_AI_Laboratory/internlm2-chat-1_8b是魔搭对应的模型ID cache_dir/home/aistudio/data/model’为指定下载到本地的目录
from modelscope im…前言
使用XTuner 微调个人小助手认知
一、下载模型
#安装魔搭依赖包
pip install modelscope新建download.py内容如下 其中Shanghai_AI_Laboratory/internlm2-chat-1_8b是魔搭对应的模型ID cache_dir/home/aistudio/data/model’为指定下载到本地的目录
from modelscope import snapshot_download
model_dir snapshot_download(Shanghai_AI_Laboratory/internlm2-chat-1_8b,cache_dir/home/aistudio/data/model)二、安装 XTuner
1.创建环境
#新建一个code文件夹
mkdir -p /home/aistudio/data/code
#切换到该目录下
cd /home/aistudio/data/code
#从 Github 上下载源码
git clone -b v0.1.21 https://github.com/InternLM/XTuner
#进入源码目录
cd XTuner
# 执行安装
pip install -e .[deepspeed]2.结果验证
xtuner version三. 快速开始
这里我们用 internlm2-chat-1_8b 模型通过 QLoRA 的方式来微调一个自己的小助手认知作为案例来进行演示
1.准备数据
#新建datas文件夹
mkdir -p datas
#创建json文件
touch datas/assistant.json2.数据生成
1.新建一个xtuner_generate_assistant.py内容如下 2.修改neme由“伍鲜同志”改为“阿豪” 3.修改数据写入路径为刚刚创建的json文件
import json# 设置用户的名字
name 阿豪
# 设置需要重复添加的数据次数
n 8000# 初始化数据
data [{conversation: [{input: 请介绍一下你自己, output: 我是{}的小助手内在是上海AI实验室书生·浦语的1.8B大模型哦.format(name)}]},{conversation: [{input: 你在实战营做什么, output: 我在这里帮助{}完成XTuner微调个人小助手的任务.format(name)}]}
]# 通过循环将初始化的对话数据重复添加到data列表中
for i in range(n):data.append(data[0])data.append(data[1])# 将data列表中的数据写入到datas/assistant.json文件中
with open(datas/assistant.json, w, encodingutf-8) as f:# 使用json.dump方法将数据以JSON格式写入文件# ensure_asciiFalse 确保中文字符正常显示# indent4 使得文件内容格式化便于阅读json.dump(data, f, ensure_asciiFalse, indent4)
3.初始化数据
#执行
python xtuner_generate_assistant.py 4.获取训练脚本
xtuner copy-cfg internlm2_chat_1_8b_qlora_alpaca_e3 .修改内容如下
# Copyright (c) OpenMMLab. All rights reserved.
import torch
from datasets import load_dataset
from mmengine.dataset import DefaultSampler
from mmengine.hooks import (CheckpointHook, DistSamplerSeedHook, IterTimerHook,LoggerHook, ParamSchedulerHook)
from mmengine.optim import AmpOptimWrapper, CosineAnnealingLR, LinearLR
from peft import LoraConfig
from torch.optim import AdamW
from transformers import (AutoModelForCausalLM, AutoTokenizer,BitsAndBytesConfig)from xtuner.dataset import process_hf_dataset
from xtuner.dataset.collate_fns import default_collate_fn
from xtuner.dataset.map_fns import alpaca_map_fn, template_map_fn_factory
from xtuner.engine.hooks import (DatasetInfoHook, EvaluateChatHook,VarlenAttnArgsToMessageHubHook)
from xtuner.engine.runner import TrainLoop
from xtuner.model import SupervisedFinetune
from xtuner.parallel.sequence import SequenceParallelSampler
from xtuner.utils import PROMPT_TEMPLATE, SYSTEM_TEMPLATE#######################################################################
# PART 1 Settings #
#######################################################################
# Model
pretrained_model_name_or_path /mnt/workspace/model/Shanghai_AI_Laboratory/internlm2-chat-1_8b
use_varlen_attn False# Data
alpaca_en_path /mnt/workspace/code/datas/assistant.json
prompt_template PROMPT_TEMPLATE.internlm2_chat
max_length 2048
pack_to_max_length True# parallel
sequence_parallel_size 1# Scheduler Optimizer
batch_size 1 # per_device
accumulative_counts 16
accumulative_counts * sequence_parallel_size
dataloader_num_workers 0
max_epochs 3
optim_type AdamW
lr 2e-4
betas (0.9, 0.999)
weight_decay 0
max_norm 1 # grad clip
warmup_ratio 0.03# Save
save_steps 500
save_total_limit 2 # Maximum checkpoints to keep (-1 means unlimited)# Evaluate the generation performance during the training
evaluation_freq 500
SYSTEM SYSTEM_TEMPLATE.alpaca
evaluation_inputs [请介绍一下你自己, Please introduce yourself
]#######################################################################
# PART 2 Model Tokenizer #
#######################################################################
tokenizer dict(typeAutoTokenizer.from_pretrained,pretrained_model_name_or_pathpretrained_model_name_or_path,trust_remote_codeTrue,padding_sideright)model dict(typeSupervisedFinetune,use_varlen_attnuse_varlen_attn,llmdict(typeAutoModelForCausalLM.from_pretrained,pretrained_model_name_or_pathpretrained_model_name_or_path,trust_remote_codeTrue,torch_dtypetorch.float16,quantization_configdict(typeBitsAndBytesConfig,load_in_4bitTrue,load_in_8bitFalse,llm_int8_threshold6.0,llm_int8_has_fp16_weightFalse,bnb_4bit_compute_dtypetorch.float16,bnb_4bit_use_double_quantTrue,bnb_4bit_quant_typenf4)),loradict(typeLoraConfig,r64,lora_alpha16,lora_dropout0.1,biasnone,task_typeCAUSAL_LM))#######################################################################
# PART 3 Dataset Dataloader #
#######################################################################
alpaca_en dict(typeprocess_hf_dataset,datasetdict(typeload_dataset, pathjson, data_filesdict(trainalpaca_en_path)),tokenizertokenizer,max_lengthmax_length,dataset_map_fnNone,template_map_fndict(typetemplate_map_fn_factory, templateprompt_template),remove_unused_columnsTrue,shuffle_before_packTrue,pack_to_max_lengthpack_to_max_length,use_varlen_attnuse_varlen_attn)sampler SequenceParallelSampler \if sequence_parallel_size 1 else DefaultSampler
train_dataloader dict(batch_sizebatch_size,num_workersdataloader_num_workers,datasetalpaca_en,samplerdict(typesampler, shuffleTrue),collate_fndict(typedefault_collate_fn, use_varlen_attnuse_varlen_attn))#######################################################################
# PART 4 Scheduler Optimizer #
#######################################################################
# optimizer
optim_wrapper dict(typeAmpOptimWrapper,optimizerdict(typeoptim_type, lrlr, betasbetas, weight_decayweight_decay),clip_graddict(max_normmax_norm, error_if_nonfiniteFalse),accumulative_countsaccumulative_counts,loss_scaledynamic,dtypefloat16)# learning policy
# More information: https://github.com/open-mmlab/mmengine/blob/main/docs/en/tutorials/param_scheduler.md # noqa: E501
param_scheduler [dict(typeLinearLR,start_factor1e-5,by_epochTrue,begin0,endwarmup_ratio * max_epochs,convert_to_iter_basedTrue),dict(typeCosineAnnealingLR,eta_min0.0,by_epochTrue,beginwarmup_ratio * max_epochs,endmax_epochs,convert_to_iter_basedTrue)
]# train, val, test setting
train_cfg dict(typeTrainLoop, max_epochsmax_epochs)#######################################################################
# PART 5 Runtime #
#######################################################################
# Log the dialogue periodically during the training process, optional
custom_hooks [dict(typeDatasetInfoHook, tokenizertokenizer),dict(typeEvaluateChatHook,tokenizertokenizer,every_n_itersevaluation_freq,evaluation_inputsevaluation_inputs,systemSYSTEM,prompt_templateprompt_template)
]if use_varlen_attn:custom_hooks [dict(typeVarlenAttnArgsToMessageHubHook)]# configure default hooks
default_hooks dict(# record the time of every iteration.timerdict(typeIterTimerHook),# print log every 10 iterations.loggerdict(typeLoggerHook, log_metric_by_epochFalse, interval10),# enable the parameter scheduler.param_schedulerdict(typeParamSchedulerHook),# save checkpoint per save_steps.checkpointdict(typeCheckpointHook,by_epochFalse,intervalsave_steps,max_keep_ckptssave_total_limit),# set sampler seed in distributed evrionment.sampler_seeddict(typeDistSamplerSeedHook),
)# configure environment
env_cfg dict(# whether to enable cudnn benchmarkcudnn_benchmarkFalse,# set multi process parametersmp_cfgdict(mp_start_methodfork, opencv_num_threads0),# set distributed parametersdist_cfgdict(backendnccl),
)# set visualizer
visualizer None# set log level
log_level INFO# load from which checkpoint
load_from None# whether to resume training from the loaded checkpoint
resume False# Defaults to use random seed and disable deterministic
randomness dict(seedNone, deterministicFalse)# set log processor
log_processor dict(by_epochFalse)
5.开启训练
xtuner train ./internlm2_chat_1_8b_qlora_alpaca_e3_copy.py微调前 6. 模型格式转换
pth_filels -t ./work_dirs/internlm2_chat_1_8b_qlora_alpaca_e3_copy/*.pth | head -n 1
export MKL_SERVICE_FORCE_INTEL1
export MKL_THREADING_LAYERGNU
xtuner convert pth_to_hf ./internlm2_chat_1_8b_qlora_alpaca_e3_copy.py ${pth_file} ./hf7.模型合并
export MKL_SERVICE_FORCE_INTEL1
export MKL_THREADING_LAYERGNU
xtuner convert merge /root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b ./hf ./merged --max-shard-size 2GB8.测试效果
python -m streamlit run xtuner_streamlit_demo.py 文章转载自: http://www.morning.rmfh.cn.gov.cn.rmfh.cn http://www.morning.rmxgk.cn.gov.cn.rmxgk.cn http://www.morning.nssjy.cn.gov.cn.nssjy.cn http://www.morning.zpzys.cn.gov.cn.zpzys.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.brwei.com.gov.cn.brwei.com http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.qyxnf.cn.gov.cn.qyxnf.cn http://www.morning.mtrfz.cn.gov.cn.mtrfz.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.drtgt.cn.gov.cn.drtgt.cn http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.cnfxr.cn.gov.cn.cnfxr.cn http://www.morning.mnkz.cn.gov.cn.mnkz.cn http://www.morning.wctqc.cn.gov.cn.wctqc.cn http://www.morning.nbiotank.com.gov.cn.nbiotank.com http://www.morning.qfwfj.cn.gov.cn.qfwfj.cn http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.xnflx.cn.gov.cn.xnflx.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.ktntj.cn.gov.cn.ktntj.cn http://www.morning.bpmmq.cn.gov.cn.bpmmq.cn http://www.morning.zlhcw.cn.gov.cn.zlhcw.cn http://www.morning.hrjrt.cn.gov.cn.hrjrt.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.kabaifu.com.gov.cn.kabaifu.com http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.rwbh.cn.gov.cn.rwbh.cn http://www.morning.mxhgy.cn.gov.cn.mxhgy.cn http://www.morning.jxmjr.cn.gov.cn.jxmjr.cn http://www.morning.dplmq.cn.gov.cn.dplmq.cn http://www.morning.zcqgf.cn.gov.cn.zcqgf.cn http://www.morning.llfwg.cn.gov.cn.llfwg.cn http://www.morning.fmqw.cn.gov.cn.fmqw.cn http://www.morning.hqwtm.cn.gov.cn.hqwtm.cn http://www.morning.mznqz.cn.gov.cn.mznqz.cn http://www.morning.ngqty.cn.gov.cn.ngqty.cn http://www.morning.trqsm.cn.gov.cn.trqsm.cn http://www.morning.lpmjr.cn.gov.cn.lpmjr.cn http://www.morning.xtlty.cn.gov.cn.xtlty.cn http://www.morning.lmxrt.cn.gov.cn.lmxrt.cn http://www.morning.jfnlj.cn.gov.cn.jfnlj.cn http://www.morning.phxns.cn.gov.cn.phxns.cn http://www.morning.rkgyx.cn.gov.cn.rkgyx.cn http://www.morning.bmrqz.cn.gov.cn.bmrqz.cn http://www.morning.qkxnw.cn.gov.cn.qkxnw.cn http://www.morning.xcfmh.cn.gov.cn.xcfmh.cn http://www.morning.gqcsd.cn.gov.cn.gqcsd.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.zhnpj.cn.gov.cn.zhnpj.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.lfgql.cn.gov.cn.lfgql.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.gktds.cn.gov.cn.gktds.cn http://www.morning.mmynk.cn.gov.cn.mmynk.cn http://www.morning.mnsts.cn.gov.cn.mnsts.cn http://www.morning.djpzg.cn.gov.cn.djpzg.cn http://www.morning.yfmwg.cn.gov.cn.yfmwg.cn http://www.morning.gjwkl.cn.gov.cn.gjwkl.cn http://www.morning.dwfzm.cn.gov.cn.dwfzm.cn http://www.morning.qkrz.cn.gov.cn.qkrz.cn http://www.morning.jxjrm.cn.gov.cn.jxjrm.cn http://www.morning.zfhzx.cn.gov.cn.zfhzx.cn http://www.morning.jwtjf.cn.gov.cn.jwtjf.cn http://www.morning.wjhdn.cn.gov.cn.wjhdn.cn http://www.morning.kryxk.cn.gov.cn.kryxk.cn http://www.morning.hnhsym.cn.gov.cn.hnhsym.cn http://www.morning.bwrbm.cn.gov.cn.bwrbm.cn http://www.morning.zcxjg.cn.gov.cn.zcxjg.cn http://www.morning.fgsqz.cn.gov.cn.fgsqz.cn http://www.morning.smfbw.cn.gov.cn.smfbw.cn http://www.morning.bcngs.cn.gov.cn.bcngs.cn http://www.morning.tnjkg.cn.gov.cn.tnjkg.cn http://www.morning.pfgln.cn.gov.cn.pfgln.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.xrftt.cn.gov.cn.xrftt.cn http://www.morning.flqkp.cn.gov.cn.flqkp.cn