新闻资讯建站服务商,wordpress带数据,百度搜索历史记录,南昌优易科 网站建设本篇文章主要介绍如何使用 Amazon SageMaker 进行 Llama 2 模型微调的示例。 这个示例主要包括: Llama 2 总体介绍Llama 2 微调介绍Llama 2 环境设置Llama 2 微调训练 前言 随着生成式 AI 的热度逐渐升高#xff0c;国内外各种基座大语言竞相出炉#xff0c;在其基础上衍生出… 本篇文章主要介绍如何使用 Amazon SageMaker 进行 Llama 2 模型微调的示例。 这个示例主要包括: Llama 2 总体介绍Llama 2 微调介绍Llama 2 环境设置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 FeedbackRLHF针对对话场景进行了大幅优化达到了非常出色的有用性和安全性基准。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_rolesess 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 Pathlocal_cache_path Path(./model)
local_cache_path.mkdir(exist_okTrue)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_idmodel_name,cache_dirlocal_cache_path,allow_patternsallow_patterns,revisionb2e65e8ad4bb35e5abaee0170ebd5fc2134a50bb
)# Get the model files path
import os
from glob import globlocal_model_path Nonepaths 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 LANGC.UTF-8
ENV PYTHONUNBUFFEREDTRUE
ENV PYTHONDONTWRITEBYTECODETRUERUN pip3 uninstall -y deepspeed \ pip3 install deepspeed0.10.0 \ pip3 install transformers4.30.2## Make all local GPUs visible
ENV NVIDIA_VISIBLE_DEVICESall 模型微调代码 模型微调源代码较多细节可以参考上述 git 仓库。 微调参数 为了节省显存采用 DeepSpeed Stage-3训练过程开启 bf16实现整数范围和精度的平衡训练数据集采用官方提供的 dummy_conversation.json也就是典型的 {instruction、input、output} 的格式同时可以支持多轮对话 DEEPSPEED_OPTSFastChat/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_RANK0for i in ${!hosts_array[]}; doif [[ ${hosts_array[$i]} *${CURRENT_HOST}* ]]; thenecho host index$iNODE_RANK$i fi
doneMASTER_PORT13579
export NCCL_SOCKET_IFNAMEeth0#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_PORTchmod x ./s5cmd
./s5cmd sync s3://$MODEL_S3_BUCKET/llm/models/llama2/TheBloke/Llama-2-13B-fp16/* /tmp/llama_pretrain/CMDtorchrun ${DISTRIBUTED_ARGS} ${DEEPSPEED_OPTS}
echo ${CMD}
${CMD} 21 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.12xlarge8 卡 A100 40GB作为训练机器。当微调完成后训练好的模型自动存储于指定的 S3 桶内可用于后续的模型部署推理。 import time
from sagemaker.estimator import Estimatorenvironment {MODEL_S3_BUCKET: sagemaker_default_bucket # The bucket to store pretrained model and fine-tune model
}base_job_name llama2-13b-finetuneinstance_type ml.p4d.24xlargeestimator Estimator(rolerole,entry_pointds-train-dist.sh,source_dir./src,base_job_namebase_job_name,instance_count1,instance_typeinstance_type,image_uriimage_uri,environmentenvironment,disable_profilerTrue,debugger_hook_configFalse)estimator.fit() 总结 大语言模型方兴未艾正在以各种方式改变和影响着整个世界。客户拥抱大语言模型亚马逊云科技团队同样在深耕客户需求和大语言模型技术可以在未来更好地协助客户实现需求提升业务价值。 本篇作者 高郁 亚马逊云科技解决方案架构师主要负责企业客户上云帮助客户进行云架构设计和技术咨询专注于智能湖仓、AI/ML 等技术方向。 星标不迷路开发更极速 关注后记得星标「亚马逊云开发者」 听说点完下面4个按钮 就不会碰到bug了