引言

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

环境准备

安装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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
安装openMind Library以及openMind Hub Client
  • 安装openMind Hub Client
pip install openmind_hub
  • 1.
  • 安装openMind Library,并安装PyTorch框架及其依赖。
pip install openmind[pt]
  • 1.

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

安装llama-factory
git clone https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -e ".[torch-npu,metrics]"
  • 1.
  • 2.
  • 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
  • 1.
  • 2.
  • 3.

模型推理

用户可以使用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()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.

执行推理脚本:

python inference_llama3.2_3b_chat.py
  • 1.

推理结果如下:

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

  • 使用LLaMa Factory与模型交互

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

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

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

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

交互结果如下:

Llama 3.2-3B-Instruct PyTorch模型微调最佳实践_人工智能_02

模型微调

数据集

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

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

微调

新建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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.

使用以下命令进行微调:

llamafactory-cli train examples/train_lora/llama3.2_3b_lora_sft.yaml
  • 1.
微调可视化
  • 训练Loss可视化:

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

微调后推理

模型推理

修改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
  • 1.
  • 2.
  • 3.

使用以下命令进行推理:

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

推理结果:

Llama 3.2-3B-Instruct PyTorch模型微调最佳实践_人工智能_04

结语

应用使能套件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>