LoRA微調(diào)1:模型自我認(rèn)知微調(diào)實(shí)踐

一、準(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容