1
- Fast Transformer Inference with Better Transformer
1
+ Better Transformer๋ฅผ ์ด์ฉํ ๊ณ ์ ํธ๋์คํฌ๋จธ ์ถ๋ก
2
2
===============================================================
3
3
4
- **Author **: `Michael Gschwind <https://github.com/mikekgfb >`__
4
+ **์ ์ **: `๋ง์ดํด ๊ทธ์ฌ๋น๋ <https://github.com/mikekgfb >`__
5
+ **๋ฒ์ญ **: `์ด์งํ <https://github.com/uddk6215 >`__
5
6
6
- This tutorial introduces Better Transformer (BT) as part of the PyTorch 1.12 release.
7
- In this tutorial, we show how to use Better Transformer for production
8
- inference with torchtext. Better Transformer is a production ready fastpath to
9
- accelerate deployment of Transformer models with high performance on CPU and GPU.
10
- The fastpath feature works transparently for models based either directly on
11
- PyTorch core ``nn.module `` or with torchtext.
7
+
8
+ ์ด ํํ ๋ฆฌ์ผ์์๋ PyTorch 1.12 ๋ฒ์ ์ ์ผ๋ถ๋ก Better Transformer (BT)๋ฅผ ์๊ฐํฉ๋๋ค.
9
+ ์ฌ๊ธฐ์๋ torchtext๋ฅผ ์ฌ์ฉํด ์์ฉํ๋ ์ ํ ์์ค์ ์ถ๋ก ์์ Better Transformer๋ฅผ ์ ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
10
+ Better Transformer๋ ์์ฉ ์ ํ ์์ค์ผ๋ก ๋ฐ๋ก ์ ์ฉ๊ฐ๋ฅํ fastpath์
๋๋ค.
11
+ ์ด๋, CPU์ GPU์์ ๊ณ ์ฑ๋ฅ์ผ๋ก ๋ ๋น ๋ฅด๊ฒ Transformer ๋ชจ๋ธ์ ๋ฐฐํฌํ ์ ์๊ฒ๋ ํด์ค๋๋ค.
12
+ ์ด fastpath ๊ธฐ๋ฅ์ PyTorch ์ฝ์ด nn.module์ ์ง์ ๊ธฐ๋ฐ์ผ๋ก ํ๊ฑฐ๋ torchtext๋ฅผ ์ฌ์ฉํ๋ ๋ชจ๋ธ์ ๋ํด ์ดํดํ๊ธฐ ์ฝ๊ณ ๋ช
ํํ๊ฒ ์๋ํฉ๋๋ค.
13
+
14
+ Better Transformer fastpath๋ก ๊ฐ์ํ๋ ์ ์๋ ๋ชจ๋ธ์ PyTorch ์ฝ์ด torch.nn.module ํด๋์ค์ธ TransformerEncoder, TransformerEncoderLayer,
15
+ ๊ทธ๋ฆฌ๊ณ MultiHeadAttention์ ์ฌ์ฉํ๋ ๋ชจ๋ธ์
๋๋ค.
16
+ ๋ํ, torchtext๋ fastpath ๊ฐ์ํ์ ์ด์ ์ ์ป๊ธฐ ์ํด ์ฝ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ชจ๋๋ค์ ์ฌ์ฉํ๋๋ก ์
๋ฐ์ดํธ๋์์ต๋๋ค.
17
+ (์ถํ ๋ ๋ง์ ๋ชจ๋์ด fastpath ์คํ์ ์ง์ํ ์ ์์ต๋๋ค.)
12
18
13
- Models which can be accelerated by Better Transformer fastpath execution are those
14
- using the following PyTorch core ``torch.nn.module `` classes ``TransformerEncoder ``,
15
- ``TransformerEncoderLayer ``, and ``MultiHeadAttention ``. In addition, torchtext has
16
- been updated to use the core library modules to benefit from fastpath acceleration.
17
- (Additional modules may be enabled with fastpath execution in the future.)
18
19
19
- Better Transformer offers two types of acceleration :
20
+ Better Transformer๋ ๋ ๊ฐ์ง ์ ํ์ ๊ฐ์ํ๋ฅผ ์ ๊ณตํฉ๋๋ค :
20
21
21
- * Native multihead attention (MHA) implementation for CPU and GPU to improve overall execution efficiency.
22
- * Exploiting sparsity in NLP inference. Because of variable input lengths, input
23
- tokens may contain a large number of padding tokens for which processing may be
24
- skipped, delivering significant speedups.
22
+ * CPU์ GPU์ ๋ํ Native multihead attention(MHA) ๊ตฌํ์ผ๋ก ์ ๋ฐ์ ์ธ ์คํ ํจ์จ์ฑ์ ํฅ์์ํต๋๋ค.
23
+ * NLP ์ถ๋ก ์์์ sparsity๋ฅผ ํ์ฉํฉ๋๋ค. ๊ฐ๋ณ ๊ธธ์ด ์
๋ ฅ(variable input lengths)์ผ๋ก ์ธํด ์
๋ ฅ ํ ํฐ์ ๋ง์ ์์
24
+ ํจ๋ฉ ํ ํฐ์ด ํฌํจ๋ ์ ์๋๋ฐ, ์ด๋ฌํ ํ ํฐ๋ค์ ์ฒ๋ฆฌ๋ฅผ ๊ฑด๋๋ฐ์ด ์๋นํ ์๋ ํฅ์์ ์ ๊ณตํฉ๋๋ค.
25
25
26
- Fastpath execution is subject to some criteria. Most importantly, the model
27
- must be executed in inference mode and operate on input tensors that do not collect
28
- gradient tape information (e.g., running with torch.no_grad).
26
+ Fastpath ์คํ์ ๋ช ๊ฐ์ง ๊ธฐ์ค์ ์ถฉ์กฑํด์ผ ํฉ๋๋ค. ๊ฐ์ฅ ์ค์ํ ๊ฑด, ๋ชจ๋ธ์ด ์ถ๋ก ๋ชจ๋์์ ์คํ๋์ด์ผ ํ๋ฉฐ
27
+ gradient tape ์ ๋ณด๋ฅผ ์์งํ์ง ์๋ ์
๋ ฅ ํ
์์ ๋ํด ์๋ํด์ผ ํ๋ค๋ ๊ฒ์
๋๋ค(์: torch.no_grad๋ฅผ ์ฌ์ฉํ์ฌ ์คํ).
29
28
30
- To follow this example in Google Colab , `click here
29
+ ์ด ์์ ๋ฅผ Google Colab์์ ๋ฐ๋ผํ๋ ค๋ฉด , `์ฌ๊ธฐ๋ฅผ ํด๋ฆญ
31
30
<https://colab.research.google.com/drive/1KZnMJYhYkOMYtNIX5S3AGIYnjyG0AojN?usp=sharing> `__.
32
31
33
- Better Transformer Features in This Tutorial
32
+
33
+
34
+ ์ด ํํ ๋ฆฌ์ผ์์ Better Transformer์ ๊ธฐ๋ฅ๋ค
34
35
--------------------------------------------
35
36
36
- * Load pretrained models (created before PyTorch version 1.12 without Better Transformer)
37
- * Run and benchmark inference on CPU with and without BT fastpath (native MHA only)
38
- * Run and benchmark inference on (configurable) DEVICE with and without BT fastpath (native MHA only)
39
- * Enable sparsity support
40
- * Run and benchmark inference on (configurable) DEVICE with and without BT fastpath (native MHA + sparsity)
37
+ * ์ฌ์ ํ๋ จ๋ ๋ชจ๋ธ ๋ก๋ (Better Transformer ์์ด PyTorch ๋ฒ์ 1.12 ์ด์ ์ ์์ฑ๋ ๋ชจ๋ธ)
38
+ * CPU์์ BT fastpath๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ์ ์ฌ์ฉํ์ง ์์ ๊ฒฝ์ฐ์ ์ถ๋ก ์ ์คํ ๋ฐ ๋ฒค์น๋งํฌ (๋ค์ดํฐ๋ธ MHA๋ง ํด๋น)
39
+ * (๊ตฌ์ฑ ๊ฐ๋ฅํ)๋๋ฐ์ด์ค์์ BT fastpath๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ์ ์ฌ์ฉํ์ง ์์ ๊ฒฝ์ฐ์ ์ถ๋ก ์ ์คํ ๋ฐ ๋ฒค์น๋งํฌ (๋ค์ดํฐ๋ธ MHA๋ง ํด๋น)
40
+ * sparsity ์ง์ ํ์ฑํ
41
+ * (๊ตฌ์ฑ ๊ฐ๋ฅํ) ๋๋ฐ์ด์ค์์ BT fastpath๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ์ ์ฌ์ฉํ์ง ์์ ๊ฒฝ์ฐ์ ์ถ๋ก ์ ์คํ ๋ฐ ๋ฒค์น๋งํฌ (๋ค์ดํฐ๋ธ MHA + ํฌ์์ฑ)
42
+
41
43
42
- Additional Information
44
+
45
+ ์ถ๊ฐ์ ์ธ ์ ๋ณด๋ค
43
46
-----------------------
44
- Additional information about Better Transformer may be found in the PyTorch.Org blog
45
- `A Better Transformer for Fast Transformer Inference
47
+ ๋ ๋์ ํธ๋์คํฌ๋จธ์ ๋ํ ์ถ๊ฐ ์ ๋ณด๋ PyTorch.Org ๋ธ๋ก๊ทธ์์ ํ์ธํ ์ ์์ต๋๋ค.
48
+ `๊ณ ์ ํธ๋์คํฌ๋จธ ์ถ๋ก ์ ์ํ Better Transformer
46
49
<https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference//> `__.
47
50
48
51
49
52
50
- 1. Setup
53
+ 1. ์ค์
51
54
52
- 1.1 Load pretrained models
55
+ 1.1 ์ฌ์ ํ๋ จ๋ ๋ชจ๋ธ ๋ถ๋ฌ์ค๊ธฐ
53
56
54
- We download the XLM-R model from the predefined torchtext models by following the instructions in
55
- `torchtext.models <https://pytorch.org/text/main/models.html >`__. We also set the DEVICE to execute
56
- on-accelerator tests. (Enable GPU execution for your environment as appropriate.)
57
+ `torchtext.models <https://pytorch.org/text/main/models.html >`__ ์ ์ง์นจ์ ๋ฐ๋ผ ๋ฏธ๋ฆฌ ์ ์๋ torchtext ๋ชจ๋ธ์์ XLM-R ๋ชจ๋ธ์ ๋ค์ด๋ก๋ํฉ๋๋ค.
58
+ ๋ํ ๊ฐ์๊ธฐ ์์์์ ํ
์คํธ๋ฅผ ์คํํ๊ธฐ ์ํด DEVICE๋ฅผ ์ค์ ํฉ๋๋ค. (ํ์์ ๋ฐ๋ผ ์ฌ์ฉ ํ๊ฒฝ์ ๋ง๊ฒ GPU ์คํ์ ํ์ฑํ๋ฉด ๋ฉ๋๋ค.)
57
59
58
60
.. code-block :: python
59
61
@@ -74,9 +76,9 @@ on-accelerator tests. (Enable GPU execution for your environment as appropriate
74
76
model = xlmr_large.get_model(head = classifier_head)
75
77
transform = xlmr_large.transform()
76
78
77
- 1.2 Dataset Setup
79
+ 1.2 ๋ฐ์ดํฐ์
์ค์
78
80
79
- We set up two types of inputs: a small input batch and a big input batch with sparsity .
81
+ ๋ ๊ฐ์ง ์ ํ์ ์
๋ ฅ์ ์ค์ ํ๊ฒ ์ต๋๋ค. ์์ ์
๋ ฅ ๋ฐฐ์น์ sparsity๋ฅผ ๊ฐ์ง ํฐ ์
๋ ฅ ๋ฐฐ์น์
๋๋ค .
80
82
81
83
.. code-block :: python
82
84
@@ -104,7 +106,7 @@ We set up two types of inputs: a small input batch and a big input batch with sp
104
106
St. Petersburg, used only by the elite."""
105
107
]
106
108
107
- Next, we select either the small or large input batch, preprocess the inputs and test the model.
109
+ ๋ค์์ผ๋ก, ์์ ์
๋ ฅ ๋ฐฐ์น ๋๋ ํฐ ์
๋ ฅ ๋ฐฐ์น ์ค ํ๋๋ฅผ ์ ํํ๊ณ , ์
๋ ฅ์ ์ ์ฒ๋ฆฌํ ํ ๋ชจ๋ธ์ ํ
์คํธํฉ๋๋ค.
108
110
109
111
.. code-block :: python
110
112
@@ -114,23 +116,23 @@ Next, we select either the small or large input batch, preprocess the inputs and
114
116
output = model(model_input)
115
117
output.shape
116
118
117
- Finally, we set the benchmark iteration count:
119
+ ๋ง์ง๋ง์ผ๋ก, ๋ฒค์น๋งํฌ ๋ฐ๋ณต ํ์๋ฅผ ์ค์ ํฉ๋๋ค.
118
120
119
121
.. code-block :: python
120
122
121
123
ITERATIONS = 10
122
124
123
- 2. Execution
125
+ 2. ์คํ
124
126
125
- 2.1 Run and benchmark inference on CPU with and without BT fastpath (native MHA only )
127
+ 2.1 CPU์์ BT fastpath๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ์ ์ฌ์ฉํ์ง ์์ ๊ฒฝ์ฐ์ ์ถ๋ก ์ ์คํ ๋ฐ ๋ฒค์น๋งํฌ (๋ค์ดํฐ๋ธ MHA๋ง ํด๋น )
126
128
127
- We run the model on CPU, and collect profile information:
129
+ CPU์์ ๋ชจ๋ธ์ ์คํํ๊ณ ํ๋กํ์ผ ์ ๋ณด๋ฅผ ์์งํฉ๋๋ค:
128
130
129
- * The first run uses traditional (" slow path") execution .
130
- * The second run enables BT fastpath execution by putting the model in inference mode using ` model .eval()` and disables gradient collection with ` torch.no_grad() ` .
131
+ * ์ฒซ ๋ฒ์งธ ์คํ์ ์ ํต์ ์ธ ์คํ(' slow path')์ ์ฌ์ฉํฉ๋๋ค .
132
+ * ๋ ๋ฒ์งธ ์คํ์ model.eval()์ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ์ถ๋ก ๋ชจ๋๋ก ์ค์ ํ๊ณ torch.no_grad()๋ก ๋ณํ๋(gradient) ์์ง์ ๋นํ์ฑํํ์ฌ BT fastpath ์คํ์ ํ์ฑํํฉ๋๋ค .
131
133
132
- You can see an improvement (whose magnitude will depend on the CPU model) when the model is executing on CPU. Notice that the fastpath profile shows most of the execution time
133
- in the native `TransformerEncoderLayer ` implementation `aten::_transformer_encoder_layer_fwd `.
134
+ CPU์์ ๋ชจ๋ธ์ ์คํํ ๋ ์ฑ๋ฅ์ด ํฅ์๋ ๊ฒ์ ๋ณผ ์ ์์ ๊ฒ๋๋ค.(ํฅ์ ์ ๋๋ CPU ๋ชจ๋ธ์ ๋ฐ๋ผ ๋ค๋ฆ
๋๋ค)
135
+ fastpath ํ๋กํ์ผ์์ ๋๋ถ๋ถ์ ์คํ ์๊ฐ์ด ๋ค์ดํฐ๋ธ `TransformerEncoderLayer`์ ์ ์์ค ์ฐ์ฐ์ ๊ตฌํํ `aten::_transformer_encoder_layer_fwd`์ ์์๋๋ ๊ฒ์ ์ฃผ๋ชฉํ์ธ์:
134
136
135
137
.. code-block :: python
136
138
@@ -152,29 +154,28 @@ in the native `TransformerEncoderLayer` implementation `aten::_transformer_encod
152
154
print (prof)
153
155
154
156
155
- 2.2 Run and benchmark inference on (configurable) DEVICE with and without BT fastpath (native MHA only )
157
+ 2.2 (๊ตฌ์ฑ ๊ฐ๋ฅํ)๋๋ฐ์ด์ค์์ BT fastpath๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ์ ์ฌ์ฉํ์ง ์์ ๊ฒฝ์ฐ์ ์ถ๋ก ์ ์คํ ๋ฐ ๋ฒค์น๋งํฌ (๋ค์ดํฐ๋ธ MHA๋ง ํด๋น )
156
158
157
- We check the BT sparsity setting:
159
+ BT sparsity ์ค์ ์ ํ์ธํด๋ณด๊ฒ ์ต๋๋ค.
158
160
159
161
.. code-block :: python
160
162
161
163
model.encoder.transformer.layers.enable_nested_tensor
162
164
163
165
164
- We disable the BT sparsity:
166
+ ์ด๋ฒ์ BT sparsity์ ๋นํ์ฑํํฉ๋๋ค.
165
167
166
168
.. code-block :: python
167
169
168
170
model.encoder.transformer.layers.enable_nested_tensor= False
169
171
170
172
171
- We run the model on DEVICE, and collect profile information for native MHA execution on DEVICE:
173
+ DEVICE์์ ๋ชจ๋ธ์ ์คํํ๊ณ , DEVICE์์์ ๋ค์ดํฐ๋ธ MHA ์คํ์ ๋ํ ํ๋กํ์ผ ์ ๋ณด๋ฅผ ์์งํฉ๋๋ค:
172
174
173
- * The first run uses traditional ("slow path") execution.
174
- * The second run enables BT fastpath execution by putting the model in inference mode using `model.eval() `
175
- and disables gradient collection with `torch.no_grad() `.
175
+ * ์ฒซ ๋ฒ์งธ ์คํ์ ์ ํต์ ์ธ ('slow path') ์คํ์ ์ฌ์ฉํฉ๋๋ค.
176
+ * ๋ ๋ฒ์งธ ์คํ์ model.eval()์ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ์ถ๋ก ๋ชจ๋๋ก ์ค์ ํ๊ณ torch.no_grad()๋ก ๋ณํ๋(gradient) ์์ง์ ๋นํ์ฑํํ์ฌ BT fastpath ์คํ์ ํ์ฑํํฉ๋๋ค.
176
177
177
- When executing on a GPU, you should see a significant speedup, in particular for the small input batch setting:
178
+ GPU์์ ์คํํ ๋, ํนํ ์์ ์
๋ ฅ ๋ฐฐ์น๋ก ์ค์ ํ ๊ฒฝ์ฐ ์๋๊ฐ ํฌ๊ฒ ํฅ์๋๋ ๊ฒ์ ๋ณผ ์ ์์ ๊ฒ๋๋ค.
178
179
179
180
.. code-block :: python
180
181
@@ -199,20 +200,20 @@ When executing on a GPU, you should see a significant speedup, in particular for
199
200
print (prof)
200
201
201
202
202
- 2.3 Run and benchmark inference on (configurable) DEVICE with and without BT fastpath (native MHA + sparsity )
203
+ 2.3 (๊ตฌ์ฑ ๊ฐ๋ฅํ) ๋๋ฐ์ด์ค์์ BT fastpath๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ์ ์ฌ์ฉํ์ง ์์ ๊ฒฝ์ฐ์ ์ถ๋ก ์ ์คํ ๋ฐ ๋ฒค์น๋งํฌ (๋ค์ดํฐ๋ธ MHA + ํฌ์์ฑ )
203
204
204
- We enable sparsity support:
205
+ sparsity ์ง์์ ํ์ฑํํฉ๋๋ค.
205
206
206
207
.. code-block :: python
207
208
208
209
model.encoder.transformer.layers.enable_nested_tensor = True
209
210
210
- We run the model on DEVICE, and collect profile information for native MHA and sparsity support execution on DEVICE:
211
+ DEVICE์์ ๋ชจ๋ธ์ ์คํํ๊ณ , DEVICE์์์ ๋ค์ดํฐ๋ธ MHA์ sparsity ์ง์ ์คํ์ ๋ํ ํ๋กํ์ผ ์ ๋ณด๋ฅผ ์์งํฉ๋๋ค:
211
212
212
- * The first run uses traditional (" slow path") execution .
213
- * The second run enables BT fastpath execution by putting the model in inference mode using ` model .eval()` and disables gradient collection with ` torch.no_grad() ` .
213
+ * ์ฒซ ๋ฒ์งธ ์คํ์ ์ ํต์ ์ธ (' slow path') ์คํ์ ์ฌ์ฉํฉ๋๋ค .
214
+ * ๋ ๋ฒ์งธ ์คํ์ model.eval()์ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ์ถ๋ก ๋ชจ๋๋ก ์ค์ ํ๊ณ torch.no_grad()๋ก ๋ณํ๋(gradient) ์์ง์ ๋นํ์ฑํํ์ฌ BT fastpath ์คํ์ ํ์ฑํํฉ๋๋ค .
214
215
215
- When executing on a GPU, you should see a significant speedup, in particular for the large input batch setting which includes sparsity:
216
+ GPU์์ ์คํํ ๋, ํนํ sparsity๋ฅผ ํฌํจํ๋ ํฐ ์
๋ ฅ ๋ฐฐ์น ์ค์ ์์ ์๋นํ ์๋ ํฅ์์ ๋ณผ ์ ์์ ๊ฒ๋๋ค.
216
217
217
218
.. code-block :: python
218
219
@@ -237,15 +238,10 @@ When executing on a GPU, you should see a significant speedup, in particular for
237
238
print (prof)
238
239
239
240
240
- Summary
241
+ ์์ฝ
241
242
-------
242
-
243
- In this tutorial, we have introduced fast transformer inference with
244
- Better Transformer fastpath execution in torchtext using PyTorch core
245
- Better Transformer support for Transformer Encoder models. We have
246
- demonstrated the use of Better Transformer with models trained prior to
247
- the availability of BT fastpath execution. We have demonstrated and
248
- benchmarked the use of both BT fastpath execution modes, native MHA execution
249
- and BT sparsity acceleration.
250
-
251
-
243
+
244
+ ์ด ํํ ๋ฆฌ์ผ์์๋ torchtext์์ PyTorch ์ฝ์ด์ ํธ๋์คํฌ๋จธ ์ธ์ฝ๋ ๋ชจ๋ธ์ ์ํ Better Transformer ์ง์์ ํ์ฉํ์ฌ,
245
+ Better Transformer๋ฅผ ์ด์ฉํ ๊ณ ์ ํธ๋์คํฌ๋จธ ์ถ๋ก ์ ์๊ฐํ์ต๋๋ค.
246
+ BT fastpath ์คํ์ด ๊ฐ๋ฅํด์ง๊ธฐ ์ด์ ์ ํ๋ จ๋ ๋ชจ๋ธ์์ Better Transformer์ ์ฌ์ฉ์ ์์ฐํ์ต๋๋ค.
247
+ ๋ํ BT fastpath ์คํ์ ๋ ๊ฐ์ง ๋ชจ๋์ธ ๋ค์ดํฐ๋ธ MHA ์คํ๊ณผ BT sparsity ๊ฐ์ํ์ ์ฌ์ฉ์ ์์ฐ ๋ฐ ๋ฒค์น๋งํฌ๋ฅผ ํด๋ณด์์ต๋๋ค.
0 commit comments