MatrixYao commited on
Commit
f3ef856
1 Parent(s): 347b252

intel-sapphire-rapids-inference-cn done

Browse files

Signed-off-by: Yao, Matrix <matrix.yao@intel.com>

assets/129_intel_sapphire_rapids_inference/01.png ADDED

Git LFS Details

  • SHA256: 79dd2454114541d0812a2f35172c29751450601413bdb6ca19ec0baa4c8286ae
  • Pointer size: 130 Bytes
  • Size of remote file: 26.8 kB
assets/129_intel_sapphire_rapids_inference/02.png ADDED

Git LFS Details

  • SHA256: 16149ded4bfb460631e5540a29992d0e35bbfb137da5014e5777199b68af0f58
  • Pointer size: 131 Bytes
  • Size of remote file: 251 kB
intel-sapphire-rapids-inference-cn.md ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 使用英特尔 Sapphire Rapids 加速 PyTorch Transformers,第 2 部分
2
+
3
+ 在 [最近的一篇文章](https://huggingface.co/blog/intel-sapphire-rapids) 中,我们介绍了代号为 [Sapphire Rapids](https://en.wikipedia.org/wiki/Sapphire_Rapids) 的第四代英特尔至强 CPU 及其新的先进矩阵扩展 ([AMX](https://en.wikipedia.org/wiki/Advanced_Matrix_Extensions)) 指令集。通过使用 Amazon EC2 上的 Sapphire Rapids 服务器集群并结合相应的英特尔优化库,如 [英特尔 PyTorch 扩展(IPEX)](https://github.com/intel/intel-extension-for-pytorch),我们展示了如何使用 CPU 进行高效的分布式大规模训练,与上一代至强 (Ice Lake) 相比,Sapphire Rapids 实现了 8 倍的加速,取得了近线性的扩展比。
4
+
5
+ 本文我们将重点关注推理。使用基于 PyTorch 的 HuggingFace transformers 模型,我们首先在 Ice Lake 服务器上分别测量它们在长、短两种文本序列上的性能。然后,我们在 Sapphire Rapids 服务器和最新版本的 Hugging Face [Optimum Intel](https://github.com/huggingface/optimum-intel) 上执行相同的测试,并比较两代 CPU 的性能。这里,Optimum Intel 是一个专用于英特尔平台的硬件加速开源库。
6
+
7
+ 让我们开始吧!
8
+
9
+
10
+ ## 为什么你应该考虑使用 CPU 推理
11
+
12
+ 在决定使用 CPU 还是 GPU 进行深度学习推理时需要考虑多个因素。最重要的当然是模型的大小。一般来说,较大的模型能更多地受益于 GPU 提供的强大算力,而较小的模型可以在 CPU 上高效运行。
13
+
14
+ 另一个需要考虑的因素是模型和推理任务本身的并行度。 GPU 为大规模并行处理而设计,因此它们可能对那些可以高度并行化的任务更高效。而另一方面,如果模型或推理任务并没有特别高的并行度,CPU 可能是更有效的选择。
15
+
16
+ 成本也是一个需要考虑的重要因素。 GPU 可能很昂贵,而使用 CPU 可能是一种性价比更高的选择,尤其是在业务应用并不需要极低延迟的情况下。此外,如果你需要能够轻松扩缩推理实例的数量,或者如果你需要能够在各种平台上进行推理,使用 CPU 可能是更灵活的选择。
17
+
18
+ 现在,让我们开始配置我们的测试服务器。
19
+
20
+ ## 配置我们的测试服务器
21
+ 和上一篇文章一样,我们将使用 Amazon EC2 实例:
22
+
23
+ * 一个基于 Ice Lake 架构 `c6i.16xlarge` 实例,
24
+ * 一个基于 Sapphire Rapids 架构的 `r7iz.16xlarge-metal` 实例。你可以在 [AWS 网站](https://aws.amazon.com/ec2/instance-types/r7iz/) 上获取有关新 r7iz 系列的更多信息。
25
+
26
+ 两个实例都有 32 个物理核(因此有 64 个 vCPU)。我们将用相同的方式来设置它们:
27
+
28
+ * Ubuntu 22.04 和 Linux 5.15.0 (`ami-0574da719dca65348`),
29
+ * PyTorch 1.13 与 IPEX(Intel Extension for PyTorch) 1.13,
30
+ * Transformers 4.25.1。
31
+
32
+ 唯一的区别是在 r7iz 实例上我们多装一个 Optimum Intel 库。
33
+
34
+ 以下是设置步骤。像往常一样,我们建议使用虚拟环境来保证环境纯净。
35
+
36
+ ```
37
+ sudo apt-get update
38
+
39
+ # 安装 libtcmalloc,获取更好性能
40
+ sudo apt install libgoogle-perftools-dev -y
41
+ export LD_PRELOAD="/usr/lib/x86_64-linux-gnu/libtcmalloc.so"
42
+
43
+ sudo apt-get install python3-pip -y
44
+ pip install pip --upgrade
45
+ export PATH=/home/ubuntu/.local/bin:$PATH
46
+ pip install virtualenv
47
+ virtualenv inference_env
48
+ source inference_env/bin/activate
49
+
50
+ pip3 install torch==1.13.0 -f https://download.pytorch.org/whl/cpu
51
+ pip3 install intel_extension_for_pytorch==1.13.0 -f https://developer.intel.com/ipex-whl-stable-cpu
52
+ pip3 install transformers
53
+
54
+ # 仅需在 r7iz 实例上安装
55
+ pip3 install optimum [intel]
56
+ ```
57
+
58
+ 在两个实例上完成上述步骤后,我们就可以开始运行测试了。
59
+
60
+ ## 对流行的 NLP 模型进行基准测试
61
+
62
+ 在这个例子中,我们将在文本分类任务上对几个 NLP 模型进行基准测试:[distilbert-base-uncased](https://huggingface.co/distilbert-base-uncased), [bert-base-uncased]( https://huggingface.co/bert-base-uncased) 和 [roberta-base](https://huggingface.co/roberta-base)。你可以在 Github 上找到 [完整脚本](https://gist.github.com/juliensimon/7ae1c8d12e8a27516e1392a3c73ac1cc)。当然,你也可以用你自己的模型随意尝试!
63
+
64
+ ```
65
+ models = ["distilbert-base-uncased", "bert-base-uncased", "roberta-base"]
66
+ ```
67
+
68
+ 我们使用序列长度分别为 16 和 128 的两种句子来测试,同时我们也将在这两种句子上分别测量单句推理和批量推理的平均预测延迟和 p99 预测延迟。该测试方案模拟了真实场景,因此可以较好地近似在真实场景中的预期加速比。
69
+
70
+
71
+ ```
72
+ sentence_short = "This is a really nice pair of shoes, I am completely satisfied with my purchase"
73
+ sentence_short_array = [sentence_short] * 8
74
+
75
+ sentence_long = "These Adidas Lite Racer shoes hit a nice sweet spot for comfort shoes. Despite being a little snug in the toe box, these are very comfortable to wear and provide nice support while wearing. I would stop short of saying they are good running shoes or cross-trainers because they simply lack the ankle and arch support most would desire in those type of shoes and the treads wear fairly quickly, but they are definitely comfortable. I actually walked around Disney World all day in these without issue if that is any reference. Bottom line, I use these as the shoes they are best; versatile, inexpensive, and comfortable, without expecting the performance of a high-end athletic sneaker or expecting the comfort of my favorite pair of slippers."
76
+ sentence_long_array = [sentence_long] * 8
77
+ ```
78
+
79
+ 基准测试功能非常简单。在几次预热迭代后,我们使用 `pipeline` API 运行 1000 次预测,把预测时间存下来,并计算它们的均值和 p99 值。
80
+
81
+ ```
82
+ import time
83
+ import numpy as np
84
+
85
+ def benchmark (pipeline, data, iterations=1000):
86
+ # 预热 100 次
87
+ for i in range (100):
88
+ result = pipeline (data)
89
+ times = []
90
+ for i in range (iterations):
91
+ tick = time.time ()
92
+ result = pipeline (data)
93
+ tock = time.time ()
94
+ times.append (tock - tick)
95
+ return "{:.2f}".format (np.mean (times) * 1000), "{:.2f}".format (
96
+ np.percentile (times, 99) * 1000
97
+ )
98
+ ```
99
+
100
+ 在 c6i (Ice Lake) 实例上,我们只使用普通的 Transformers `pipeline`。
101
+
102
+ ```
103
+ from transformers import pipeline
104
+
105
+ for model in models:
106
+ print (f"Benchmarking {model}")
107
+ pipe = pipeline ("sentiment-analysis", model=model)
108
+ result = benchmark (pipe, sentence_short)
109
+ print (f"Transformers pipeline, short sentence: {result}")
110
+ result = benchmark (pipe, sentence_long)
111
+ print (f"Transformers pipeline, long sentence: {result}")
112
+ result = benchmark (pipe, sentence_short_array)
113
+ print (f"Transformers pipeline, short sentence array: {result}")
114
+ result = benchmark (pipe, sentence_long_array)
115
+ print (f"Transformers pipeline, long sentence array: {result}")
116
+ ```
117
+
118
+ 在 r7iz (Sapphire Rapids) 实例上,我们同时使用普通 `pipeline` 和 Optimum `pipeline`。在 Optimum `pipeline` 中,我们启用 `bfloat16` 模式以利用到 AMX 指令,并将 `jit` 设置为 `True` 以使用即时编译进一步优化模型。
119
+
120
+
121
+ ```
122
+ import torch
123
+ from optimum.intel import inference_mode
124
+
125
+ with inference_mode (pipe, dtype=torch.bfloat16, jit=True) as opt_pipe:
126
+ result = benchmark (opt_pipe, sentence_short)
127
+ print (f"Optimum pipeline, short sentence: {result}")
128
+ result = benchmark (opt_pipe, sentence_long)
129
+ print (f"Optimum pipeline, long sentence: {result}")
130
+ result = benchmark (opt_pipe, sentence_short_array)
131
+ print (f"Optimum pipeline, short sentence array: {result}")
132
+ result = benchmark (opt_pipe, sentence_long_array)
133
+ print (f"Optimum pipeline, long sentence array: {result}")
134
+ ```
135
+
136
+ 为简洁起见,我们先看下 [distilbert-base-uncased](https://huggingface.co/distilbert-base-uncased) 的 p99 结果。你可以在文章末尾找到所有测例的完整结果。
137
+
138
+ <kbd>
139
+ <img src="assets/129_intel_sapphire_rapids_inference/01.png">
140
+ </kbd>
141
+
142
+ 如上图所示,与上一代至强 CPU 相比,Sapphire Rapids 上单个句子的预测延迟加速了 **60-65%**。也就是说,由于结合了英特尔 Sapphire Rapids 平台以及 Hugging Face Optimum 的优化,你只需对代码进行很少改动就可将预测速度提高 3 倍。
143
+
144
+ 这让我们即使在长文本序列上也可以达到 **个位数的预测延迟**。在 Sapphire Rapids 之前,这样的性能只有通过 GPU 才能实现。
145
+
146
+ ## 结论
147
+
148
+ 第四代英特尔至强 CPU 提供了出色的推理性能,尤其是在与 Hugging Face Optimum 结合使用时。这是深度学习在更易得和更具成本效益的道路上的又一个进步,我们期待与英特尔的朋友们在这条道路上继续合作。
149
+
150
+ 以下是一些可帮助你入门的其他资源:
151
+
152
+ * [英特尔 IPEX GitHub](https://github.com/intel/intel-extension-for-pytorch)
153
+ * [Hugging Face Optimum GitHub](https://github.com/huggingface/optimum)
154
+
155
+
156
+ 如果你有任何问题或反馈,我们很乐意在 [Hugging Face 论坛](https://discuss.huggingface.co/) 上与你交流。
157
+
158
+ 感谢阅读!
159
+
160
+
161
+ ## 附录:完整结果
162
+
163
+ <kbd>
164
+ <img src="assets/129_intel_sapphire_rapids_inference/02.png">
165
+ </kbd>
166
+
167
+ * 基准测试软件环境:Ubuntu 22.04 with libtcmalloc, Linux 5.15.0 patched for Intel AMX support, PyTorch 1.13 with Intel Extension for PyTorch, Transformers 4.25.1, Optimum 1.6.1, Optimum Intel 1.7.0.dev0*
168
+
169
+ >>>> 英文原文: <url> https://huggingface.co/blog/intel-sapphire-rapids-inference </url>
170
+ >>>>
171
+ >>>> 译者: Matrix Yao (姚伟峰),英特尔深度学习工程师,工作方向为 transformer-family 模型在各模态数据上的应用及大规模模型的训练推理。