环境安装
'''
# 设置pip全局镜像
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
# 安装ms-swift
git clone https://github.com/modelscope/swift.git
cd swift
pip install -e .[llm]
#环境对齐 (如果你运行错误, 可以跑下面的代码, 仓库使用最新环境测试)
pip install -r requirements/framework.txt -U
pip install -r requirements/llm.txt -U
'''
超参数设置
模型采用qwen7bchat
微调超参数:
traindatasetsample=500,
eval_steps=20,
logging_steps=5,
output_dir='output',
loratargetmodules='ALL',
selfcognitionsample=500
微调
提示: 因为自我认知训练涉及到知识编辑, 建议对MLP加loratargetmodules. 可以通过指定--loratargetmodules ALL在所有的linear层(包括qkvo以及mlp)加lora. 这通常是效果最好的.
使用python:
# Experimental environment: A10, 3090, V100, ...
# 18GB GPU memory
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from swift.llm import DatasetName, ModelType, SftArguments, sft_main
sft_args = SftArguments(
model_type=ModelType.qwen_7b_chat,
dataset=[DatasetName.alpaca_zh, DatasetName.alpaca_en],
train_dataset_sample=500,
eval_steps=20,
logging_steps=5,
output_dir='output',
lora_target_modules='ALL',
self_cognition_sample=500,
model_name=['夜咏', 'Night Ode'],
model_author=['李岚泽', 'Lanzer'])
output = sft_main(sft_args)
best_model_checkpoint = output['best_model_checkpoint']
print(f'best_model_checkpoint: {best_model_checkpoint}')
微调实验记录
微调后推理
需要设置bestmodelcheckpoint的值, 该值会在sft的最后被打印出来.
使用python:
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from swift.llm import InferArguments, merge_lora_main, infer_main
best_model_checkpoint = 'output/qwen-7b-chat/v0-20240122-205735/checkpoint-62'
infer_args = InferArguments(
ckpt_dir=best_model_checkpoint,
eval_human=True)
# merge_lora_main(infer_args)
result = infer_main(infer_args)
评论