一、準(zhǔn)備工作
pip install -ihttps://mirrors.aliyun.com/pypi/simple-U 'ms-swift[llm]'
二、微調(diào)前推理
// infer.py
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from swift.llm import ModelType, InferArguments, infer_main
infer_args = InferArguments(ModelType.qwen2_5_1_5b_instruct)
infer_main(infer_args)
運(yùn)行如上代碼

微調(diào)前.png
三、微調(diào)
// finetune.py
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from swift.llm import DatasetName, ModelType, SftArguments, sft_main
sft_args = SftArguments(
model_type=ModelType.qwen2_5_1_5b_instruct,
dataset=[f'{DatasetName.alpaca_zh}#500', f'{DatasetName.alpaca_en}#500',
f'{DatasetName.self_cognition}#500'],
max_length=2048,
learning_rate=1e-4,
output_dir='output',
lora_target_modules=['ALL'],
model_name=['大聰明', 'Big Smarter'],
model_author=['東子', 'Donzy'])
output = sft_main(sft_args)
last_model_checkpoint = output['last_model_checkpoint']
print(f'last_model_checkpoint: {last_model_checkpoint}')
日志打印如下
[INFO:swift] Saving model checkpoint to /home/donzy/workspace/qwen2.5-1.5b-finetune/output/qwen2_5-1_5b-instruct/v1-20241018-153113/checkpoint-93
{'train_runtime': 296.5846, 'train_samples_per_second': 5.02, 'train_steps_per_second': 0.314, 'train_loss': 1.19108627, 'epoch': 1.0, 'global_step/max_steps': '93/93', 'percentage': '100.00%', 'elapsed_time': '4m 56s', 'remaining_time': '0s'}
Train: 100%|█████████████████████████████████████████████████████████████████████████| 93/93 [04:56<00:00, 3.19s/it]
[INFO:swift] last_model_checkpoint: /home/donzy/workspace/qwen2.5-1.5b-finetune/output/qwen2_5-1_5b-instruct/v1-20241018-153113/checkpoint-93
[INFO:swift] best_model_checkpoint: /home/donzy/workspace/qwen2.5-1.5b-finetune/output/qwen2_5-1_5b-instruct/v1-20241018-153113/checkpoint-93
[INFO:swift] images_dir: /home/donzy/workspace/qwen2.5-1.5b-finetune/output/qwen2_5-1_5b-instruct/v1-20241018-153113/images
[INFO:swift] End time of running main: 2024-10-18 15:36:30.487763
last_model_checkpoint: /home/donzy/workspace/qwen2.5-1.5b-finetune/output/qwen2_5-1_5b-instruct/v1-20241018-153113/checkpoint-93
微調(diào)過程中GPU使用情況

微調(diào)資源消耗_副本.png
微調(diào)后推理
// infer_after_finetuning.py
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from swift.llm import InferArguments, merge_lora, infer_main
last_model_checkpoint = 'output/qwen2_5-1_5b-instruct/v1-20241018-153113/checkpoint-93'
infer_args = InferArguments(ckpt_dir=last_model_checkpoint)
merge_lora(infer_args, device_map='cpu')
infer_main(infer_args)
輸出結(jié)果

微調(diào)后.png