当前位置: 代码网 > it编程>游戏开发>ar > 使用 Amazon SageMaker 微调 Llama 2 模型

使用 Amazon SageMaker 微调 Llama 2 模型

2024年08月04日 ar 我要评论
本篇文章主要介绍如何使用 Amazon SageMaker 进行 Llama 2 模型微调的示例。这个示例主要包括:Llama 2 总体介绍Llama 2 微调介绍Llama 2 环境设置Llama 2 微调训练前言随着生成式 AI 的热度逐渐升高,国内外各种基座大语言竞相出炉,在其基础上衍生出种类繁多的应用场景。训练优异的基座大语言模型在通用性方面表现较好,但模型可能并未涉及到特定领域的专业术语...

1c3aea7b4c3c494fd29dcbc241c8a7fd.gif

本篇文章主要介绍如何使用 amazon sagemaker 进行 llama 2 模型微调的示例。

这个示例主要包括:

  1. llama 2 总体介绍

  2. llama 2 微调介绍

  3. llama 2 环境设置

  4. llama 2 微调训练

前言

随着生成式 ai 的热度逐渐升高,国内外各种基座大语言竞相出炉,在其基础上衍生出种类繁多的应用场景。训练优异的基座大语言模型在通用性方面表现较好,但模型可能并未涉及到特定领域的专业术语、领域内的特定用语或上下文等。采用微调技术可以通过在领域特定数据上进行训练,使模型更好地适应目标领域的特殊语言模式和结构;结合基座模型的通用性和领域特定性,使得模型更具实际应用价值。

llama 2 总体介绍

llama 2 是 meta 最新开源的 llm,包括 7b、13b 和 70b 三个版本,训练数据集超过了 llama 2 的 40%,达到 2 万亿 token;上下文长度也提升到 4k,可以极大扩展多轮对话的轮数、提示词输入数据;与此同时,llama 2 chat 模型使用基于人类反馈的强化学习(reinforcement learning from human feedback,rlhf),针对对话场景进行了大幅优化,达到了非常出色的有用性和安全性基准。huggingface 的 tgi 和 vllm 等框架均有针对 llama 2 的推理优化,进一步强化了 llama 2 的可用性。

llama 2 被认为是开源界大语言模型的首选,众多的垂类大模型均采用 llama 2 作为基座大模型,在此基础上添加行业数据进行模型的预训练或者微调,适配更多的行业场景。

llama 2 微调介绍

模型微调主要分为 full fine-tune 和 peft (performance-efficient fine-tune),前者模型全部参数都会进行更新,训练时间较长,训练资源较大;而后者会冻结大部分参数、微调训练网络结构,常见的方式是 lora 和 p-tuning v2。

peft 微调方式由于参数更新较少,可能导致模型无法学习到全部领域知识,对于特定任务或领域来说会出现推理不稳定的情况,因此大多数生产系统均使用全参数方式进行模型的微调。基于上述原因,本文会以全参数微调方式介绍 llama 2 在 amazon sagemaker 上的微调。

llama 2 环境设置

备注:项目中的示例代码均保存于代码仓库,地址如下: 

https://github.com/aws-samples/llm-workshop-on-amazon-sagemaker

1. 升级 python sdk 

pip install -u sagemaker

2. 获取运行时资源,包括区域、角色、账号、s3 桶等 

import boto3
import sagemaker
from sagemaker import get_execution_role




sess                     = sagemaker.session()
role                     = get_execution_role()
sagemaker_default_bucket = sess.default_bucket()


account                  = sess.boto_session.client("sts").get_caller_identity()["account"]
region                   = sess.boto_session.region_name

llama 2 微调训练

微调准备

克隆代码

  • 采用 lm-sys 团队发布的 fastchat 平台进行 llama 2 的微调,fastchat 也用于训练了知名的 vicuna 模型,具有良好的代码规范和性能优化。

git clone https://github.com/lm-sys/fastchat.git
cd fastchat
git reset --hard 974537efbd82093b45e64d07904efe7728193a52

下载 llama 2 原始模型

from huggingface_hub import snapshot_download
from pathlib import path




local_cache_path = path("./model")
local_cache_path.mkdir(exist_ok=true)


model_name = "thebloke/llama-2-13b-fp16"


# only download pytorch checkpoint files
allow_patterns = ["*.json", "*.pt", "*.bin", "*.model", "*.py"]


model_download_path = snapshot_download(
    repo_id=model_name,
    cache_dir=local_cache_path,
    allow_patterns=allow_patterns,
    revision='b2e65e8ad4bb35e5abaee0170ebd5fc2134a50bb'
)


# get the model files path
import os
from glob import glob


local_model_path = none


paths = os.walk(r'./model')
for root, dirs, files in paths:
    for file in files:
        if file == 'config.json':
            print(os.path.join(root,file))
            local_model_path = str(os.path.join(root,file))[0:-11]
            print(local_model_path)
if local_model_path == none:
    print("model download may failed, please check prior step!")

拷贝模型和数据到 amazon s3

chmod +x ./s5cmd
./s5cmd sync ${local_model_path} s3://${sagemaker_default_bucket}/llm/models/llama2/thebloke/llama-2-13b-fp16/ 
rm -rf model

模型微调

  • 模型的微调使用全参数模型,以实现微调后模型的稳定性。

  • 模型的微调使用开源框架 deepspeed 进行加速。

准备基础镜像

使用 amazon sagemaker 定制的深度学习训练镜像作为基础镜像,再安装 llama 2 训练所需的依赖包。dockerfile 如下:

%%writefile dockerfile
## you should change below region code to the region you used, here sample is use us-west-2
from 763104351884.dkr.ecr.us-west-2.amazonaws.com/huggingface-pytorch-training:1.13.1-transformers4.26.0-gpu-py39-cu117-ubuntu20.04 




env lang=c.utf-8
env pythonunbuffered=true
env pythondontwritebytecode=true


run pip3 uninstall -y deepspeed \
    && pip3 install deepspeed==0.10.0 \
    && pip3 install transformers==4.30.2


## make all local gpus visible
env nvidia_visible_devices="all"

模型微调代码

模型微调源代码较多,细节可以参考上述 git 仓库。

微调参数

  • 为了节省显存,采用 deepspeed stage-3

  • 训练过程开启 bf16,实现整数范围和精度的平衡

  • 训练数据集采用官方提供的 dummy_conversation.json,也就是典型的 {"instruction"、"input"、"output"} 的格式,同时可以支持多轮对话

deepspeed_opts="""
    fastchat/fastchat/train/train_mem.py 
    --deepspeed ds.json 
    --model_name_or_path "/tmp/llama_pretrain/" 
    --data_path fastchat/data/dummy_conversation.json 
    --output_dir "/tmp/llama_out" 
    --num_train_epochs 1 
    --per_device_train_batch_size 1 
    --per_device_eval_batch_size  1 
    --gradient_accumulation_steps 4 
    --evaluation_strategy "no" 
    --save_strategy "no" 
    --save_steps 2000 
    --save_total_limit 1 
    --learning_rate 2e-5 
    --weight_decay 0. 
    --warmup_ratio 0.03 
    --lr_scheduler_type "cosine" 
    --logging_steps 1 
    --cache_dir '/tmp' 
    --model_max_length 2048 
    --gradient_checkpointing true 
    --lazy_preprocess true 
    --bf16 true 
    --tf32 true 
    --report_to "none"
"""

微调脚本

  • 微调使用 torchrun + deepspeed 进行分布式训练

%%writefile ./src/ds-train-dist.sh
#!/bin/bash
current_host="${sm_current_host}"




ifs=',' read -ra hosts_array <<< "${sm_hosts}"
nnodes=${#hosts_array[@]}
node_rank=0


for i in "${!hosts_array[@]}"; do
    if [[ "${hosts_array[$i]}" == *${current_host}* ]]; then
        echo "host index:$i"
        node_rank="$i" 
    fi
done
   
    
master_port="13579"
export nccl_socket_ifname="eth0"


#configure the distributed arguments for torch.distributed.launch.
gpus_per_node="$sm_num_gpus"
distributed_args="--nproc_per_node $gpus_per_node \
                  --nnodes $nnodes \
                  --node_rank $node_rank \
                  --master_addr $master_addr \
                  --master_port $master_port"


chmod +x ./s5cmd
./s5cmd sync s3://$model_s3_bucket/llm/models/llama2/thebloke/llama-2-13b-fp16/* /tmp/llama_pretrain/


cmd="torchrun ${distributed_args} ${deepspeed_opts}"
echo ${cmd}
${cmd} 2>&1 


if [[ "${current_host}" == "${master_addr}" ]]; then  
     ./s5cmd sync /tmp/llama_out s3://$model_s3_bucket/llm/models/llama2/output/thebloke/llama-2-13b-fp16/$(date +%y-%m-%d-%h-%m-%s)/
fi

启动微调

  • 全参数微调,需要使用至少一台 p4de.12xlarge(8 卡 a100 40gb)作为训练机器。

  • 当微调完成后,训练好的模型自动存储于指定的 s3 桶内,可用于后续的模型部署推理。

import time
from sagemaker.estimator import estimator




environment = {
    'model_s3_bucket': sagemaker_default_bucket # the bucket to store pretrained model and fine-tune model
}


base_job_name = 'llama2-13b-finetune'


instance_type = 'ml.p4d.24xlarge'


estimator = estimator(role=role,
                      entry_point='ds-train-dist.sh',
                      source_dir='./src',
                      base_job_name=base_job_name,
                      instance_count=1,
                      instance_type=instance_type,
                      image_uri=image_uri,
                      environment=environment,
                      disable_profiler=true,
                      debugger_hook_config=false)




estimator.fit()

总结

大语言模型方兴未艾,正在以各种方式改变和影响着整个世界。客户拥抱大语言模型,亚马逊云科技团队同样在深耕客户需求和大语言模型技术,可以在未来更好地协助客户实现需求,提升业务价值。

本篇作者

6ce443c21a564a6109595741d2da8d7c.jpeg

高郁

亚马逊云科技解决方案架构师,主要负责企业客户上云,帮助客户进行云架构设计和技术咨询,专注于智能湖仓、ai/ml 等技术方向。

3c7b3572aea34ef2ee8fc45077824270.gif

星标不迷路,开发更极速!

关注后记得星标「亚马逊云开发者」

952455b80801542c984978e00d9d8e6e.gif

听说,点完下面4个按钮

就不会碰到bug了!

c330e0b3208f52226bf51d8e36d2c4f1.gif

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com