企业网站 需求,wordpress 返回上一页,兰州网站建设咨询薇,群晖 多个wordpressHugging Face系列2#xff1a;详细剖析Hugging Face网站资源——实战六类开源库 前言本篇摘要2. Hugging Face开源库2.1 transformers2.1.1 简介2.1.2 实战1. 文本分类2. 图像识别3. 在Pytorch和TensorFlow中使用pipeline 2.2 diffusers2.2.1 简介2.2.2 实战1. 管线2. 模型和调… Hugging Face系列2详细剖析Hugging Face网站资源——实战六类开源库 前言本篇摘要2. Hugging Face开源库2.1 transformers2.1.1 简介2.1.2 实战1. 文本分类2. 图像识别3. 在Pytorch和TensorFlow中使用pipeline 2.2 diffusers2.2.1 简介2.2.2 实战1. 管线2. 模型和调度器 2.3 datasets2.3.1 简介2.3.2 实战 2.4 PEFT2.4.1 简介2.4.2 实战1. peft模型2. 新旧模型结构对比3. PEFT模型推理 2.5 accelerate2.5.1 简介2.5.2 实战1. 与pytorch融合2. 命令行accelerate configlaunch3. DeepSpeed4. notebook_launcher 2.6 optimum2.6.1 简介2.6.2 实战1. ONNXONNX Runtime2. 加速推理3. 加速训练 参考文献 前言
本系列文章旨在全面系统的介绍Hugging Face让小白也能熟练使用Hugging Face上的各种开源资源并上手创建自己的第一个Space App在本地加载Hugging Face管线训练自己的第一个模型并使用模型生成采样数据同时详细解决部署中出现的各种问题。后续文章会分别介绍采样器及其加速、显示分类器引导扩散模型、CLIP多模态图像引导生成、DDMI反转及控制类大模型ControlNet等根据反馈情况可能再增加最底层的逻辑公式和从零开始训练LLM等让您从原理到实践彻底搞懂扩散模型和大语言模型。欢迎点赞评论、收藏和关注。
本系列文章如下
《详细剖析Hugging Face网站资源——models/datasets/spaces》全面系统的介绍Hugging Face资源《详细剖析Hugging Face网站资源——实战六类开源库》《从0到1使用Hugging Face管线加载Diffusion模型生成第一张图像》在本地加载Hugging Face管线训练自己的第一个模型并使用模型生成采样数据同时详细解决部署中出现的各种问题。
本篇摘要
本篇主要介绍Hugging Face。Hugging Face是一个人工智能的开源社区是相关从业者协作和交流的平台。它的核心产品是Hugging Face Hub这是一个基于Git进行版本管理的存储库截至2024年5月已托管了65万个模型、14.5万个数据集以及超过17万个Space应用。另外Hugging Face还开源了一系列的机器学习库如Transformers、Datasets和Diffusers等以及界面演示工具Gradio。此外Hugging Face设计开发了很多学习资源比如与NLP大语言模型、扩散模型及深度强化学习等相关课程。最后介绍一些供大家交流学习的平台。为了更有趣本篇介绍了大量有趣的Spaces应用比如换装IDM-VTON、灯光特效IC-Light、LLM性能排行Artificial Analysis LLM Performance Leaderboard和自己部署的文生图模型stable-diffusion-xl-base-1.0、对图片精细化的stable-diffusion-xl-refiner-1.0等。只要读者认真按着文章操作上述操作都可自己实现。下面对以上内容逐一介绍。
2. Hugging Face开源库
除了Hugging Face Hub提供的models、datasets和spacesHugging Face还在GitHub上开源了一系列的机器学习库和工具。在GitHub的Hugging Face组织页面其置顶了一些开源代码仓库包括transformers、diffusers、datasets、peft、accelerate以及optimum如下图所示。本篇逐一详细介绍并给出对应的实战用例方便读者更直观的理解和应用。 2.1 transformers
2.1.1 简介
transformers 提供了数以千计的可执行不同任务的SOTA1预训练模型这些任务包括1支持 100 多种语言的文本分类、信息抽取、问答、摘要、翻译、文本生成的文本任务2用于图像分类目标检测和分割等图像任务3用于语音识别和音频分类等音频任务。Transformer模型还可以在几种模式的组合上执行任务例如表格问答、光学字符识别、从扫描文档中提取信息、视频分类和视觉问答等。
Transformers 提供了便于快速下载和使用的API让你可以将预训练模型在给定文本、数据集上微调后通过 model hub 与社区共享同时每个定义的 Python 模块均完全独立方便修改和快速研究实验。Transformers 支持三个最热门的深度学习库 Jax, PyTorch 以及 TensorFlow——并与之无缝整合。你可以直接使用一个框架训练你的模型然后用另一个加载和推理以支持框架之间的互操作。模型还可以导出成ONNX和TorchScript等格式以方便在生产环境中部署。
用户可以通过transformers直接测试和使用model hub中的大部分模型以节省训练时间和资源降低成本。
2.1.2 实战
Hugging Face提供pipeline API以便在给定的输入(文本、图像、音频等)上使用模型。模型训练期间管道用预训练模型实现预处理。
在使用任何库之前需要先安装
# upgrade参数保证安装最新版
pip install --upgrade XXX下面看三个例子。
1. 文本分类
使用pipeline实现文本分类 from transformers import pipeline# Allocate a pipeline for sentiment-analysis. The second line of code downloads and caches the pretrained model used by the pipeline, while the third evaluates it on the given text.classifier pipeline(sentiment-analysis)classifier(We are very happy to introduce pipeline to the transformers repository.)
[{label: POSITIVE, score: 0.9996980428695679}]2. 图像识别
使用pipeline实现图像识别 import requestsfrom PIL import Imagefrom transformers import pipeline# Download an image with cute catsurl https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/coco_sample.pngimage_data requests.get(url, streamTrue).rawimage Image.open(image_data)# Allocate a pipeline for object detectionobject_detector pipeline(object-detection)object_detector(image)
[{score: 0.9982201457023621,label: remote,box: {xmin: 40, ymin: 70, xmax: 175, ymax: 117}},{score: 0.9960021376609802,label: remote,box: {xmin: 333, ymin: 72, xmax: 368, ymax: 187}},{score: 0.9954745173454285,label: couch,box: {xmin: 0, ymin: 1, xmax: 639, ymax: 473}},{score: 0.9988006353378296,label: cat,box: {xmin: 13, ymin: 52, xmax: 314, ymax: 470}},{score: 0.9986783862113953,label: cat,box: {xmin: 345, ymin: 23, xmax: 640, ymax: 368}}]3. 在Pytorch和TensorFlow中使用pipeline
代码如下 from transformers import AutoTokenizer, AutoModel tokenizer AutoTokenizer.from_pretrained(google-bert/bert-base-uncased)model AutoModel.from_pretrained(google-bert/bert-base-uncased)# pytorch uses pt and tensorflow uses tfinputs tokenizer(Hello world!, return_tensorspt)outputs model(**inputs)代码解释tokenizer负责为预训练模型预处理字符串并且可指定返回张量格式然后传入model作训练产生最终输出。示例代码演示了如何在PyTorch或TensorFlow框架中使用模型并在新数据集上使用训练API快速微调预处理模型。
2.2 diffusers
2.2.1 简介
diffusers是SOTA预训练扩散模型的首选库可用于生成图像、音频甚至分子的3D结构。diffusers是一个模块化工具箱既可支持简单的推理解决方案也支持训练自己的扩散模型。diffusers库设计注重可用性而非性能简单而非简易可定制化而非抽象。
diffusers提供三个核心组件1最先进的扩散管道diffusion pipelines只需几行代码即可进行推理2可互换噪声调度器noise schedulers可用于调节推理过程中模型生成中的扩散速度和输出质量3预训练模型pretrained models可用作构建块并与调度器相结合以创建自己的端到端扩散系统。
2.2.2 实战
实战包括管线、模型和调度器。
1. 管线
使用diffusers生成输出非常容易如文生图可使用from_pretrained方法加载预训练的扩散模型stable-diffusion
from diffusers import DiffusionPipeline
import torchpipeline DiffusionPipeline.from_pretrained(runwayml/stable-diffusion-v1-5, torch_dtypetorch.float16)
pipeline.to(cuda)
pipeline(An image of a squirrel in Picasso style).images[0]2. 模型和调度器
还可以深入研究预训练模型和噪声调度器的工具箱构建自己的扩散系统
from diffusers import DDPMScheduler, UNet2DModel
from PIL import Image
import torchscheduler DDPMScheduler.from_pretrained(google/ddpm-cat-256)
model UNet2DModel.from_pretrained(google/ddpm-cat-256).to(cuda)
scheduler.set_timesteps(50)sample_size model.config.sample_size
noise torch.randn((1, 3, sample_size, sample_size), devicecuda)
input noisefor t in scheduler.timesteps:with torch.no_grad():noisy_residual model(input, t).sampleprev_noisy_sample scheduler.step(noisy_residual, t, input).prev_sampleinput prev_noisy_sampleimage (input / 2 0.5).clamp(0, 1)
image image.cpu().permute(0, 2, 3, 1).numpy()[0]
image Image.fromarray((image * 255).round().astype(uint8))
image2.3 datasets
2.3.1 简介
datasets是一个轻量级库旨在让社区轻松添加和共享新的数据集它提供两个主要功能
可用于众多公共数据集的dataloaders可一行代码下载和预处理HuggingFace Datasets Hub提供的任意数据集包括图像数据集、音频数据集、467种语言/方言的文本数据集等。示例如squad_datasetload_dataset“squad”。高效的数据预处理可简单、快速和可复制的预处理众多公共数据集及本地的CSV、JSON、text、PNG、JPEG、WAV、MP3、Parquet等格式数据。示例如processed_datasetdataset.mapprocess_example。
此外datasets还有许多其他有趣的功能
适配于大型数据集datasets将用户从RAM内存限制中解放出来所有数据集都使用高效的零序列化成本后端Apache Arrow进行内存映射。智能缓存重复处理数据无需等待。支持轻量快速透明的Python APIPython API还具有多处理器、缓存及内存映射等特性。内置支持与NumPy、pandas、PyTorch、TensorFlow 2和JAX进行互操作。原生支持音频和图像数据。支持启用流模式以节省磁盘空间可快速开启对数据集进行迭代。
需要指出的是HuggingFace Datasets源于优秀的TensorFlow数据集的一个分支两者差异主要是HuggingFace Datasets动态加载python脚本而不是在库中提供、后端序列化基于Apache Arrow而不是TF Records、面向用户的datasets对象封装了一个内存映射的Arrow表缓存基于与框架无关的由tf.data的方法激发的数据集类而不是基于tf.data.Dataset。
2.3.2 实战
datasets的API以函数datasets.load_dataset(dataset_name, **kwargs)为中心该函数用于实例化数据集。 加载文本数据集的示例
from datasets import load_dataset# Print all the available datasets
from huggingface_hub import list_datasets
print([dataset.id for dataset in list_datasets()])# Load a dataset and print the first example in the training set
squad_dataset load_dataset(squad)
print(squad_dataset[train][0])# Process the dataset - add a column with the length of the context texts
dataset_with_length squad_dataset.map(lambda x: {length: len(x[context])})# Process the dataset - tokenize the context texts (using a tokenizer from the Transformers library)
from transformers import AutoTokenizer
tokenizer AutoTokenizer.from_pretrained(bert-base-cased)tokenized_dataset squad_dataset.map(lambda x: tokenizer(x[context]), batchedTrue)当数据集比磁盘空间大或者不想下载数据时可以使用流媒体示例如下
# If you want to use the dataset immediately and efficiently stream the data as you iterate over the dataset
image_dataset load_dataset(cifar100, streamingTrue)
for example in image_dataset[train]:break2.4 PEFT
2.4.1 简介
PEFTParameter-Efficient Fine-Tuning即SOTA高效参数微调方法由于大型模型的参数规模完全微调模型成本极高而PEFT仅通过微调少量参数就可以达到完全微调的性能使型预训练模型有效地适应各种下游应用显著降低计算和存储成本。
PEFT与Transformers集成可轻松进行模型训练和推理与Diffuser集成可方便地管理不同的适配器与Accelerate集成可用于真正大型模型的分布式训练和推理。
提示1PEFT方法如何用于下游各类任务可参考PEFT 组织部分jupyter notebook的演示2查看PEFT API Reference的 Adapters部分可得到PEFT支持的方法列表。阅读CONCEPTUAL GUIDES部分学习如何使用。如下图
2.4.2 实战
实战包括示例、新旧模型对比和模型推理三部分。
1. peft模型
在使用PEFT中方法如LoRA训练模型之前需要通过get_peft_model()将基础模型和PEFT配置包装在一起。
比如下例的bigscience/mt0-large只需训练完全模型0.19%的参数即可
from transformers import AutoModelForSeq2SeqLM
from peft import get_peft_config, get_peft_model, LoraConfig, TaskType
model_name_or_path bigscience/mt0-large
tokenizer_name_or_path bigscience/mt0-largepeft_config LoraConfig(task_typeTaskType.SEQ_2_SEQ_LM, inference_modeFalse, r8, lora_alpha32, lora_dropout0.1
)model AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path)
model_peft get_peft_model(model, peft_config)
model.print_trainable_parameters()
trainable params: 2359296 || all params: 1231940608 || trainable%: 0.191510531001182822. 新旧模型结构对比
现在打印出新旧模型作对比看PEFT到底做了什么。也可参考Spaces空间中使用PEFT的示例如下
# 查看peft配置
model.peft_configPrefixTuningConfig(peft_typePeftType.PREFIX_TUNING: PREFIX_TUNING, base_model_name_or_pathbigscience/mt0-large,
task_typeTaskType.SEQ_2_SEQ_LM: SEQ_2_SEQ_LM, inference_modeFalse, num_virtual_tokens30, token_dim1024, num_transformer_submodules1,
num_attention_heads16, num_layers24, encoder_hidden_size1024, prefix_projectionFalse)# 打印旧模型
print(model)
MT5ForConditionalGeneration((shared): Embedding(250112, 1024)(encoder): MT5Stack((embed_tokens): Embedding(250112, 1024)(block): ModuleList((0): MT5Block((layer): ModuleList((0): MT5LayerSelfAttention((SelfAttention): MT5Attention((q): Linear(in_features1024, out_features1024, biasFalse)(k): Linear(in_features1024, out_features1024, biasFalse)(v): Linear(in_features1024, out_features1024, biasFalse)(o): Linear(in_features1024, out_features1024, biasFalse)(relative_attention_bias): Embedding(32, 16))(layer_norm): MT5LayerNorm()(dropout): Dropout(p0.1, inplaceFalse))(1): MT5LayerFF((DenseReluDense): MT5DenseGatedActDense((wi_0): Linear(in_features1024, out_features2816, biasFalse)(wi_1): Linear(in_features1024, out_features2816, biasFalse)(wo): Linear(in_features2816, out_features1024, biasFalse)(dropout): Dropout(p0.1, inplaceFalse)(act): NewGELUActivation())(layer_norm): MT5LayerNorm()(dropout): Dropout(p0.1, inplaceFalse))))(1-23): 23 x MT5Block(...)(final_layer_norm): MT5LayerNorm()(dropout): Dropout(p0.1, inplaceFalse))(decoder): MT5Stack((embed_tokens): Embedding(250112, 1024)(block): ModuleList((0): MT5Block((layer): ModuleList((0): MT5LayerSelfAttention((SelfAttention): MT5Attention((q): Linear(in_features1024, out_features1024, biasFalse)(k): Linear(in_features1024, out_features1024, biasFalse)(v): Linear(in_features1024, out_features1024, biasFalse)(o): Linear(in_features1024, out_features1024, biasFalse)(relative_attention_bias): Embedding(32, 16))(layer_norm): MT5LayerNorm()(dropout): Dropout(p0.1, inplaceFalse))(1): MT5LayerCrossAttention((EncDecAttention): MT5Attention((q): Linear(in_features1024, out_features1024, biasFalse)(k): Linear(in_features1024, out_features1024, biasFalse)(v): Linear(in_features1024, out_features1024, biasFalse)(o): Linear(in_features1024, out_features1024, biasFalse))(layer_norm): MT5LayerNorm()(dropout): Dropout(p0.1, inplaceFalse))(2): MT5LayerFF((DenseReluDense): MT5DenseGatedActDense((wi_0): Linear(in_features1024, out_features2816, biasFalse)(wi_1): Linear(in_features1024, out_features2816, biasFalse)(wo): Linear(in_features2816, out_features1024, biasFalse)(dropout): Dropout(p0.1, inplaceFalse)(act): NewGELUActivation())(layer_norm): MT5LayerNorm()(dropout): Dropout(p0.1, inplaceFalse))))(1-23): 23 x MT5Block((layer): ModuleList(...)(final_layer_norm): MT5LayerNorm()(dropout): Dropout(p0.1, inplaceFalse))(lm_head): Linear(in_features1024, out_features250112, biasFalse)
)# 打印peft模型
print(model_peft)
PeftModelForSeq2SeqLM((base_model): IA3Model((model): MT5ForConditionalGeneration((shared): Embedding(250112, 1024)(encoder): MT5Stack((embed_tokens): Embedding(250112, 1024)(block): ModuleList((0): MT5Block((layer): ModuleList((0): MT5LayerSelfAttention((SelfAttention): MT5Attention((q): Linear(in_features1024, out_features1024, biasFalse)(k): Linear(in_features1024, out_features1024, biasFalse(ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1]))(v): Linear(in_features1024, out_features1024, biasFalse(ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1]))(o): Linear(in_features1024, out_features1024, biasFalse)(relative_attention_bias): Embedding(32, 16))(layer_norm): MT5LayerNorm()(dropout): Dropout(p0.1, inplaceFalse))(1): MT5LayerFF((DenseReluDense): MT5DenseGatedActDense((wi_0): Linear(in_features1024, out_features2816, biasFalse)(wi_1): Linear(in_features1024, out_features2816, biasFalse(ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 2816x1]))(wo): Linear(in_features2816, out_features1024, biasFalse)(dropout): Dropout(p0.1, inplaceFalse)(act): NewGELUActivation())(layer_norm): MT5LayerNorm()(dropout): Dropout(p0.1, inplaceFalse))))(1-23): 23 x MT5Block(...)(final_layer_norm): MT5LayerNorm()(dropout): Dropout(p0.1, inplaceFalse))(decoder): MT5Stack((embed_tokens): Embedding(250112, 1024)(block): ModuleList((0): MT5Block((layer): ModuleList((0): MT5LayerSelfAttention((SelfAttention): MT5Attention((q): Linear(in_features1024, out_features1024, biasFalse)(k): Linear(in_features1024, out_features1024, biasFalse(ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1]))(v): Linear(in_features1024, out_features1024, biasFalse(ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1]))(o): Linear(in_features1024, out_features1024, biasFalse)(relative_attention_bias): Embedding(32, 16))(layer_norm): MT5LayerNorm()(dropout): Dropout(p0.1, inplaceFalse))(1): MT5LayerCrossAttention((EncDecAttention): MT5Attention((q): Linear(in_features1024, out_features1024, biasFalse)(k): Linear(in_features1024, out_features1024, biasFalse(ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1]))(v): Linear(in_features1024, out_features1024, biasFalse(ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1]))(o): Linear(in_features1024, out_features1024, biasFalse))(layer_norm): MT5LayerNorm()(dropout): Dropout(p0.1, inplaceFalse))(2): MT5LayerFF((DenseReluDense): MT5DenseGatedActDense((wi_0): Linear(in_features1024, out_features2816, biasFalse)(wi_1): Linear(in_features1024, out_features2816, biasFalse(ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 2816x1]))(wo): Linear(in_features2816, out_features1024, biasFalse)(dropout): Dropout(p0.1, inplaceFalse)(act): NewGELUActivation())(layer_norm): MT5LayerNorm()(dropout): Dropout(p0.1, inplaceFalse))))(1-23): 23 x MT5Block(...)(final_layer_norm): MT5LayerNorm()(dropout): Dropout(p0.1, inplaceFalse))(lm_head): Linear(in_features1024, out_features250112, biasFalse)))
)对比以上输出新旧模型主要的区别在于下面三点可以看出peft主要通过减少输出特征参数量来缩小训练规模
model:(k): Linear(in_features1024, out_features1024, biasFalse)(v): Linear(in_features1024, out_features1024, biasFalse)(wi_1): Linear(in_features1024, out_features2816, biasFalse)
model_peft:(k): Linear(in_features1024, out_features1024, biasFalse(ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1]))(v): Linear(in_features1024, out_features1024, biasFalse(ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1]))(wi_1): Linear(in_features1024, out_features2816, biasFalse(ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 2816x1]))3. PEFT模型推理
下面使用PEFT中模型AutoPeftModelForCausalLM进行推理
from peft import AutoPeftModelForCausalLM
from transformers import AutoTokenizer
import torchmodel AutoPeftModelForCausalLM.from_pretrained(ybelkada/opt-350m-lora).to(cuda)
tokenizer AutoTokenizer.from_pretrained(facebook/opt-350m)model.eval()
inputs tokenizer(Preheat the oven to 350 degrees and place the cookie dough, return_tensorspt)outputs model.generate(input_idsinputs[input_ids].to(cuda), max_new_tokens50)
print(tokenizer.batch_decode(outputs, skip_special_tokensTrue)[0])Preheat the oven to 350 degrees and place the cookie dough in the center of the oven. In a large bowl, combine the flour, baking powder, baking soda, salt, and cinnamon. In a separate bowl, combine the egg yolks, sugar, and vanilla.2.5 accelerate
2.5.1 简介
accelerate可以在任何类型的设备上运行raw格式的PyTorch训练脚本。accelerate就是为PyTorch用户创建的这些用户喜欢编写PyTorch模型的训练循环但不愿意编写和维护使用多GPU/TPU/fp16所需的样板代码。accelerate替用户做了这部分工作它可以准确地抽出与多GPU/TPU/fp16相关的样板代码并保持代码的其余部分不变。
2.5.2 实战
accelerate除了支持pytorch还支持多种其他应用方式下面逐一详细介绍。
1. 与pytorch融合
下面来看一个用accelerate如何改造使用pytorch框架的训练脚本 import torchimport torch.nn.functional as Ffrom datasets import load_datasetfrom accelerate import Accelerator accelerator Accelerator()
- device cpudevice accelerator.devicemodel torch.nn.Transformer().to(device)optimizer torch.optim.Adam(model.parameters())dataset load_dataset(my_dataset)data torch.utils.data.DataLoader(dataset, shuffleTrue) model, optimizer, data accelerator.prepare(model, optimizer, data)model.train()for epoch in range(10):for source, targets in data:source source.to(device)targets targets.to(device)optimizer.zero_grad()output model(source)loss F.cross_entropy(output, targets)- loss.backward()accelerator.backward(loss)optimizer.step()在上例中通过向任何标准PyTorch训练脚本添加5行就可以在任何类型的单个或分布式节点单CPU、单GPU、多GPU和TPU、以及混合精度的情况下运行fp8、fp16、bf16。特别是在本地机器上调试和训练环境中运行相同的代码而无需进行任何修改。
2. 命令行accelerate configlaunch
accelerate提供了一个可选的CLI工具可在启动脚本之前快速配置和测试培训环境。无需记住如何使用torch.distributed.run或编写TPU训练的特定启动器只需运行命令
(pytorch) shoeshoe-tp14:~$ accelerate config
In which compute environment are you running?
This machine
Which type of machine are you using?
multi-GPU
How many different machines will you use (use more than 1 for multi-node training)? [1]: 1
Should distributed operations be checked while running for errors? This can avoid timeout issues but will be slower. [yes/NO]: yes
Do you wish to optimize your script with torch dynamo?[yes/NO]:yes
Which dynamo backend would you like to use?
tensorrt
Do you want to customize the defaults sent to torch.compile? [yes/NO]: yes
Which mode do you want to use?
default
Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: yes
Do you want to enable dynamic shape tracing? [yes/NO]: yes
Do you want to use DeepSpeed? [yes/NO]: NO
Do you want to use FullyShardedDataParallel? [yes/NO]: NO
Do you want to use Megatron-LM ? [yes/NO]: yes
What is the Tensor Parallelism degree/size? [1]:1
What is the Pipeline Parallelism degree/size? [1]:1
Do you want to enable selective activation recomputation? [YES/no]: 1
Please enter yes or no.
Do you want to enable selective activation recomputation? [YES/no]: yes
Do you want to use distributed optimizer which shards optimizer state and gradients across data parallel ranks? [YES/no]: yes
What is the gradient clipping value based on global L2 Norm (0 to disable)? [1.0]: 1
How many GPU(s) should be used for distributed training? [1]:1Do you wish to use FP16 or BF16 (mixed precision)?
bf16
accelerate configuration saved at /home/shoe/.cache/huggingface/accelerate/default_config.yaml
以下是您提供的配置的简要概述以及每个选项的含义
计算环境您正在使用本地机器这可能意味着您将在单台物理服务器或工作站上使用多个GPU。机器类型您正在使用多GPU机器。多机器训练您只计划使用一台机器进行训练这意味着您将在单节点上进行训练。分布式操作检查您希望在运行时检查分布式操作是否有错误这样可以避免超时问题但可能会使训练变慢。使用torch dynamo优化您希望使用torch dynamo来优化您的PyTorch代码这可以提高性能。dynamo后端您选择使用tensorrt作为后端这通常用于生产环境可以提供优化的代码。DeepSpeed您不打算使用DeepSpeed这是一个用于深度学习训练的优化库。FullyShardedDataParallel您不打算使用FullyShardedDataParallel这是一个用于数据并行的PyTorch分布式训练的库。Megatron-LM您打算使用Megatron-LM这是一个用于大规模语言模型训练的PyTorch扩展。Tensor并行度您设置为1这意味着您不会使用Tensor并行。流水线并行度您设置为1这意味着您不会使用流水线并行。选择性激活重计算您启用了选择性激活重计算这可以提高效率。分布式优化器您启用了分布式优化器这意味着优化器状态和梯度将在数据并行等级上分片。梯度裁剪您设置了一个基于全局L2范数的梯度裁剪值。用于分布式训练的GPU数量您指定了使用3个GPU进行分布式训练。FP16或BF16混合精度您选择了BF16这是英伟达的混合精度之一可以提高训练性能。
逐一根据您的具体需求和配置回答所问就会生成配置文件default_config.yaml当使用命令accelerate launch时它将被自动设置到配置中示例如下
!accelerate launch --multi_gpu --num_processes 2 train_unconditional.py \--dataset_namehuggan/smithsonian_butterflies_subset \--resolution64 \--output_dir{model_name} \--train_batch_size32 \--num_epochs50 \--gradient_accumulation_steps1 \--learning_rate1e-4 \--lr_warmup_steps500 \--mixed_precisionno可以在accelerate config中配置多CPU也可以直接使用命令mpirun启用多CPU命令示例如下
# np参数设置CPU数量
mpirun -np 2 python examples/nlp_example.py3. DeepSpeed
accelerate支持在单/多GPU上使用DeepSpeed。你即可以通过上面accelerate config中配置DeepSpeed也可以将DeepSpeed相关参数通过DeepSpeedPlugin集成到python脚本中。其中DeepSpeedPlugin 是一个用于 PyTorch 模型的插件可以帮助用户更快地训练深度学习模型。具体来说DeepSpeedPlugin 可以实现以下功能
自动混合精度训练DeepSpeedPlugin 可以自动将模型参数和梯度转换为半精度浮点数从而减少 GPU 存储器的使用量加快训练速度。分布式训练DeepSpeedPlugin 可以自动启动多个进程将训练任务分配到多个 GPU 或多台机器上进行分布式训练。梯度累积DeepSpeedPlugin 可以实现梯度累积从而减少 GPU 存储器的使用量允许使用更大的批量大小进行训练。
DeepSpeedPlugin 中的参数解释
zero_stage指定使用哪个阶段的 ZeRO 内存优化技术。ZeRO 是一种内存优化技术可以将模型参数和梯度分成多个分片从而减少 GPU 存储器的使用量。zero_stage 的取值范围为 0、1、2分别表示不使用 ZeRO、使用 ZeRO 1 和使用 ZeRO 2。gradient_accumulation_steps指定梯度累积的步数取值为正整数。梯度累积是一种训练技巧可以将多个小批量数据的梯度累积起来从而实现大批量数据的训练。
示例如下
from accelerate import Accelerator, DeepSpeedPlugin# deepspeed needs to know your gradient accumulation steps beforehand, so dont forget to pass it
# Remember you still need to do gradient accumulation by yourself, just like you would have done without deepspeed
deepspeed_plugin DeepSpeedPlugin(zero_stage2, gradient_accumulation_steps2)
accelerator Accelerator(mixed_precisionfp16, deepspeed_plugindeepspeed_plugin)# How to save your Transformer?
accelerator.wait_for_everyone()
unwrapped_model accelerator.unwrap_model(model)
unwrapped_model.save_pretrained(save_dir, save_functionaccelerator.save, state_dictaccelerator.get_state_dict(model))注意目前DeepSpeed仍处于试验阶段不支持手动编写的Config将来可能支持同时底层API可能也会有变化。
4. notebook_launcher
在Accelerate 0.3.0版本中引入了notebook_launcher()它支持在jupyter notebook中启动分布式培训。这对于使用TPU后端训练的Colab或Kaggle型notebook尤其有用。只需在training_function中定义训练循环然后在最后一个单元格中添加下列代码即可
from accelerate import notebook_launchernotebook_launcher(training_function)2.6 optimum
2.6.1 简介
optimum是Transformers和Diffusers的扩展它提供了一组性能优化工具保持易用性的同时可以在特定的目标硬件上以最高效率训练和运行模型。optimum支持多种类型的加速器比如ONNX Runtime、Intel Neural Compressor、OpenVINO、NVIDIA TensorRT-LLM、AMD Instinct GPUs and Ryzen AI NPU、AWS Trainum Inferentia、Habana Gaudi Processor (HPU)、FuriosaAI等具体可参照optimum官方文档。
2.6.2 实战
在使用optimum特定加速器的功能之前需要安装对应依赖包如下表所示
AcceleratorInstallationONNX Runtimepip install --upgrade --upgrade-strategy eager optimum[onnxruntime]Intel Neural Compressorpip install --upgrade --upgrade-strategy eager optimum[neural-compressor]OpenVINOpip install --upgrade --upgrade-strategy eager optimum[openvino]NVIDIA TensorRT-LLMdocker run -it --gpus all --ipc host huggingface/optimum-nvidiaAMD Instinct GPUs and Ryzen AI NPUpip install --upgrade --upgrade-strategy eager optimum[amd]AWS Trainum Inferentiapip install --upgrade --upgrade-strategy eager optimum[neuronx]Habana Gaudi Processor (HPU)pip install --upgrade --upgrade-strategy eager optimum[habana]FuriosaAIpip install --upgrade --upgrade-strategy eager optimum[furiosa]
optimum提供不同工具在不同框架中导出和运行优化后模型导出和优化操作即可通过编程的方式也可由命令行完成。可操作框架包括
ONNX / ONNX RuntimeTensorFlow LiteOpenVINOHabana first-gen Gaudi / Gaudi2AWS Inferentia 2 / Inferentia 1NVIDIA TensorRT-LLM
其中ONNX Runtime、Neural Compressor、OpenVINO、TensorFlow Lite的特点总结如下
FeaturesONNX RuntimeNeural CompressorOpenVINOTensorFlow LiteGraph optimization✔️N/A✔️N/APost-training dynamic quantization✔️✔️N/A✔️Post-training static quantization✔️✔️✔️✔️Quantization Aware Training (QAT)N/A✔️✔️N/AFP16 (half precision)✔️N/A✔️✔️PruningN/A✔️✔️N/AKnowledge DistillationN/A✔️✔️N/A
下面我们选择ONNX/ONNX Runtime作为实战对象。
1. ONNXONNX Runtime
ONNX(Open Neural Network Exchange)是一个由微软提出的一种开放的神经网络交换格式用于在不同的深度学习框架之间进行模型的转换和交流。它的设计目标是提供一个中间表示格式使得用户可以在不同的深度学习训练和推理框架之间无缝地转换模型。
使用ONNX你可以使用不同的深度学习框架如PyTorch、TensorFlow、Paddle等进行模型的训练和定义然后将模型导出为ONNX格式。导出后的ONNX模型包含了模型的结构和权重参数等信息但不包含模型的硬件信息。
一旦模型导出为ONNX格式你可以使用ONNX Runtime或其他支持ONNX的框架将模型转换为目标设备所支持的模型格式如TensorRT、Core ML、OpenVINO等。这样你可以在不同的设备上部署和运行模型无需重新训练或重新实现模型。同时它还支持修改模型的Graph、Node和Tensor。
ONNX的优势在于它提供了跨框架和跨平台的灵活性使得深度学习模型在不同的环境中更易于部署和集成。它还支持许多常见的深度学习操作和网络架构使得模型转换过程更加方便和高效。
ONNX RuntimeONNX Runtime是一个跨平台的高性能推理引擎支持将ONNX模型转换为多种设备和框架所支持的格式如TensorFlow、TensorRT、Core ML等。它提供了一套API和工具使得在不同平台上部署和运行ONNX模型更加便捷。
2. 加速推理
transformers和diffusers格式的模型可导出为ONNX格式并易于执行图优化和量化操作。先执行格式转换
optimum-cli export onnx -m deepset/roberta-base-squad2 --optimize O2 roberta_base_qa_onnx然后使用onnxruntime进行量化
optimum-cli onnxruntime quantize \--avx512 \--onnx_model roberta_base_qa_onnx \-o quantized_roberta_base_qa_onnx这些命令将deepset/roberta-base-squad2导出为roberta_base_qa_onnx并对导出的模型进行O2图优化最后使用avx512配置进行量化。
完成量化后就可以在后端使用Python类ONNX Runtime以无缝衔接地运行ONNX模型示例代码如下
- from transformers import AutoModelForQuestionAnsweringfrom optimum.onnxruntime import ORTModelForQuestionAnsweringfrom transformers import AutoTokenizer, pipelinemodel_id deepset/roberta-base-squad2tokenizer AutoTokenizer.from_pretrained(model_id)
- model AutoModelForQuestionAnswering.from_pretrained(model_id)model ORTModelForQuestionAnswering.from_pretrained(roberta_base_qa_onnx)qa_pipe pipeline(question-answering, modelmodel, tokenizertokenizer)question Whats Optimum?context Optimum is an awesome library everyone should use!results qa_pipe(questionquestion, contextcontext)更多使用ORTModelForXXX类运行ONNX模型的细节可参考 Optimum Inference with ONNX Runtime
3. 加速训练
optimum为原生Transformers Trainer提供类型包装器以便在强大硬件上进行训练。支持如下加速训练方式
Habana’s Gaudi processorsAWS Trainium instancesONNX Runtime (optimized for GPUs) 以ONNX Runtime作为加速训练包装器为例
- from transformers import Trainer, TrainingArgumentsfrom optimum.onnxruntime import ORTTrainer, ORTTrainingArguments# Download a pretrained model from the Hubmodel AutoModelForSequenceClassification.from_pretrained(bert-base-uncased)# Define the training arguments
- training_args TrainingArguments(training_args ORTTrainingArguments(output_dirpath/to/save/folder/,optimadamw_ort_fused,...)# Create a ONNX Runtime Trainer
- trainer Trainer(trainer ORTTrainer(modelmodel,argstraining_args,train_datasettrain_dataset,...)# Use ONNX Runtime for training!trainer.train()除了上面列举的开源库之外其他常用的开源库还有timm、text-generation-inference、candle、trl、tokenizers、evaluate等。其中timm(PyTorch Image Models)是一个深度学习库其中包含图像模型、优化器、调度器以及训练/验证脚本等内容。text-generation-inference(TGI)文本生成推理是一个用于部署和服务大型语言模型LLM的工具包它为当前流行的开源LLM实现高性能文本生成包括Llama、Falcon、StarCoder、BLOOM、GPT-NeoX等。candle是Rust的一个极简ML框架专注于性能支持GPU和易用性。trl库是一个全栈工具使用包括监督微调步骤SFT、奖励建模RM和近端策略优化PPO以及直接偏好优化DPO等方法对transformer结构和diffusion模型进行微调和对齐。Tokenizers是适用于研究和生产环境的高性能分词器使用Rust语言实现在20秒内就能使用CPU完成对1GB文本数据的分词。Evaluate则使用数十种流行的指标对数据集和模型进行评估。详细介绍可在GitHub Hugging Face组织页面查看。
参考文献
llama factory学习笔记 DeepSpeedPlugin的作用 ONNX 模型格式分析与使用 State of the arts ↩︎