Llama-3.2-3B-Instruct PyTorch模型微调最佳实践

1 引言

Meta Llama 3.2多语言大型语言模型集合(LM)是一个1B和3B大小(文本输入/文本输出)的预训练和指令微调模型集合。Llama 3.2指令调整的纯文本模型针对多语言对话用例进行了优化,包括智能检索和总结任务。它们在常见的行业基准上优于许多可用的开源和闭源聊天模型。

2 环境准备

2.1 安装Ascend CANN Toolkit和Kernels

安装方法请参考安装教程或使用以下命令。

# 请替换URL为CANN版本和设备型号对应的URL
# 安装CANN Toolkit
wget https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/Milan-ASL/Milan-ASL%20V100R001C17SPC701/Ascend-cann-toolkit_8.0.RC1.alpha001_linux-"$(uname -i)".run
bash Ascend-cann-toolkit_8.0.RC1.alpha001_linux-"$(uname -i)".run --install

# 安装CANN Kernels
wget https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/Milan-ASL/Milan-ASL%20V100R001C17SPC701/Ascend-cann-kernels-910b_8.0.RC1.alpha001_linux.run
bash Ascend-cann-kernels-910b_8.0.RC1.alpha001_linux.run --install

# 设置环境变量
source /usr/local/Ascend/ascend-toolkit/set_env.sh

2.2 安装openMind Library以及openMind Hub Client

  • 安装openMind Hub Client
pip install openmind_hub
  • 安装openMind Library,并安装PyTorch框架及其依赖。
pip install openmind[pt]

更详细的安装信息请参考openMind官方的环境安装章节。

2.3 安装llama-factory

git clone https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -e ".[torch-npu,metrics]"

3 模型链接和下载

Llama-3.2-3B模型系列由社区开发者在魔乐社区贡献,包括:

通过Git从魔乐社区下载模型的repo,以Llama-3.2-3B-Instruct为例:

# 首先保证已安装git-lfs(https://git-lfs.com)
git lfs install
git clone https://modelers.cn/AI-Research/Llama-3.2-3B-Instruct.git

4 模型推理

用户可以使用openMind Library或者LLaMa Factory进行模型推理,以Llama-3.2-3B-Instruct为例,具体如下:

  • 使用openMind Library进行模型推理

新建推理脚本inference_llama3.2_3b_chat.py,推理脚本内容为:

import argparse
import torch
from openmind import pipeline
from openmind_hub import snapshot_download

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--model_name_or_path",
        type=str,
        help="Path to model",
        default=None,
    )

    args = parser.parse_args()

    return args

def main():
    args = parse_args()
    if args.model_name_or_path:
        model_path = args.model_name_or_path
    else:
        model_path = snapshot_download("AI-Research/Llama-3.2-3B-Instruct", revision="main", resume_download=True,
                                    ignore_patterns=["*.h5", "*.ot", "*.mspack"])

    pipe = pipeline(
        "text-generation",
        model=model_path,
        torch_dtype=torch.bfloat16,
        device_map="auto",
    )
    messages = [
        {"role": "system", "content": ""},
        {"role": "user", "content": "你是谁"},
    ]
    outputs = pipe(
        messages,
        max_new_tokens=256,
    )
    print(outputs[0]["generated_text"][-1])

if __name__ == "__main__":
    main()

执行推理脚本:

python inference_llama3.2_3b_chat.py

推理结果如下:

  • 使用LLaMa Factory与模型交互

在LLaMa Factory路径下新建examples/inference/llama3.2_3b_chat.yaml推理配置文件,文件内容为:

model_name_or_path: xxx # 当前仅支持本地加载,填写Llama-3.2-3B-Instruct本地权重路径
template: llama3

使用以下命令与模型进行交互:

llamafactory-cli examples/inference/llama3.2_3b_chat.yaml

交互结果如下:

5 模型微调

5.1 数据集

使用Llama-Factory集成的identity数据集。

修改data/identity.json,将{{name}}替换为openmind{{author}}替换为shengteng

5.2 微调

新建examples/train_lora/llama3.2_3b_lora_sft.yaml 微调配置文件,微调配置文件如下:

### model
model_name_or_path: xxx/xxx  # 预训练模型路径

### method
stage: sft
do_train: true
finetuning_type: lora
lora_target: all

### dataset
dataset: identity
template: llama3
cutoff_len: 1024
overwrite_cache: true
preprocessing_num_workers: 16

### output
output_dir: ./saves/llama3.2-3b/lora/sft
logging_steps: 10
save_steps: 500
plot_loss: true
overwrite_output_dir: true

### train
per_device_train_batch_size: 1
gradient_accumulation_steps: 8
learning_rate: 1.0e-4
num_train_epochs: 3.0
lr_scheduler_type: cosine
warmup_ratio: 0.1
bf16: true
ddp_timeout: 180000000

### eval
val_size: 0.1
per_device_eval_batch_size: 1
eval_strategy: steps
eval_steps: 500

使用以下命令进行微调:

llamafactory-cli train examples/train_lora/llama3.2_3b_lora_sft.yaml

5.3 微调可视化

  • 训练Loss可视化:

6 微调后推理

模型推理

修改examples/inference/llama3.2_3b_lora_sft.yaml推理配置文件,文件内容为:

model_name_or_path: xxx # 当前仅支持本地加载,填写Llama-3.2-3B-Instruct本地权重路径
adapter_name_or_path: ./saves/llama3.2-3b/lora/sft
template: llama3

使用以下命令进行推理:

llamafactory-cli chat examples/inference/llama3.2_3b_lora_sft.yaml

推理结果:

7 结语

应用使能套件openMind在华为全联接大会2024的展示吸引了我们的注意。通过专家们的分享,得以了解魔乐社区,也了解到openMind在其中发挥的技术能力和未来发展。

通过本次微调的实践,更能体会到openMind套件的魅力。它让微调过程变得更加高效和直观,希望每一位开发者都来尝试它,一起交流经验,更好地提升它的能力。

相关链接:

[1] openMind Library介绍: https://modelers.cn/docs/zh/openmind-library/overview.html

[2] openMind Hub Client介绍: https://modelers.cn/docs/zh/openmind-hub-client/overview.html

微软开源基于 Rust 的 OpenHCL 字节跳动商业化团队模型训练被“投毒”,内部人士称未影响豆包大模型 华为正式发布原生鸿蒙系统 OpenJDK 新提案:将 JDK 大小减少约 25% Node.js 23 正式发布,不再支持 32 位 Windows 系统 Linux 大规模移除疑似俄开发者,开源药丸? QUIC 在高速网络下不够快 RustDesk 远程桌面 Web 客户端 V2 预览 前端开发框架 Svelte 5 发布,历史上最重要的版本 开源日报 | 北大实习生攻击字节AI训练集群;Bitwarden进一步脱离开源;新一代MoE架构;给手机装Linux;英伟达真正的护城河是什么?
{{o.name}}
{{m.name}}

猜你喜欢

转载自my.oschina.net/u/8066678/blog/16227503