魔搭社区使用——记一次模型自我认知微调经历

1. 前言

模型自我认知简单理解即是模型认为自己是什么,也就是在你询问模型“你是谁”时,模型给出的答案。自我认知微调主要调整模型对自己的名字以及训练者的认知。
本次,我使用AutoDl的云服务器对qwen1.5-7b-chat模型进行了微调。

2. 训练模型

2.1 环境准备

2.1.1 源码安装

modelscope提供了三种安装方式(swift/README_CN.md at main · modelscope/swift (github.com)),这里采用源码安装:

git clone https://github.com/modelscope/swift.git
cd swift
pip install -e '.[llm]'

我使用AutoDl云服务器,其提供了github加速方法,在命令行运行:

source /etc/network_turbo

2.2 模型微调

源码安装后即可进行模型微调

2.2.1 修改自我认知

swift脚本:

# Experimental environment: A10, 3090
# 16GB GPU memory
CUDA_VISIBLE_DEVICES=0 \
swift sft \
    --model_id_or_path qwen1half-7b-chat \
    --model_revision master \
    --sft_type lora \
    --tuner_backend peft \
    --template_type AUTO \
    --dtype AUTO \
    --output_dir output \
    --dataset alpaca-zh#500 alpaca-en#500 self-cognition#500 \
    --train_dataset_sample -1 \
    --num_train_epochs 1 \
    --max_length 2048 \
    --check_dataset_strategy warning \
    --quantization_bit 0 \
    --bnb_4bit_comp_dtype AUTO \
    --lora_rank 8 \
    --lora_alpha 32 \
    --lora_dropout_p 0.05 \
    --lora_target_modules ALL \
    --gradient_checkpointing true \
    --batch_size 1 \
    --weight_decay 0.1 \
    --learning_rate 1e-4 \
    --gradient_accumulation_steps 16 \
    --max_grad_norm 0.5 \
    --warmup_ratio 0.03 \
    --eval_steps 100 \
    --save_steps 100 \
    --save_total_limit 2 \
    --logging_steps 10 \
    --use_flash_attn false \
    --model_name 模型的名字 模型的英文名 \
    --model_author 作者名 作者英文名

将上述内容保存为脚本运行即可。

注意:

modelscope训练模型时会将模型缓存下来,可能导致缓存目录所在盘空间不足的问题。对linux系统,缓存目录位于~/.cache/modelscope/hub下。可通过手动指定环境变量MODELSCOPE_CACHE来修改缓存地址:

export MODELSCOPE_CACHE=[new_path]

该命令仅在当前shell下生效,若想持续生效,则可修改/etc/profile等文件。

3. 验证并合并模型

运行swift脚本完成模型训练后,进行模型的验证与合并:

CUDA_VISIBLE_DEVICES=0 \
swift infer \
    --ckpt_dir "/root/autodl-tmp/swift/output/llama3-8b-instruct/v3-20240512-193259/checkpoint-16800" \
    --load_dataset_config true \
    --max_new_tokens 2048 \
    --temperature 0.1 \
    --top_p 0.7 \
    --repetition_penalty 1. \
    --dataset_test_ratio 0.01 \
    --do_sample true \
    --merge_lora true \   # 该参数必须为True才可以

将上述内容保存为脚本运行即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值