LoRA微调1:模型自我认知微调实践

一、准备工作

pip install -ihttps://mirrors.aliyun.com/pypi/simple-U 'ms-swift[llm]'

二、微调前推理

// 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)

运行如上代码


微调前.png

三、微调

// 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

微调过程中GPU使用情况


微调资源消耗_副本.png

微调后推理

// 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)

输出结果


微调后.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容