1
- Pytorch Mobile Performance Recipes
2
- ==================================
1
+ PyTorch ๋ชจ๋ฐ์ผ ์ฑ๋ฅ ๋ ์ํผ
2
+ =========================
3
3
4
- Introduction
5
- ----------------
6
- Performance (aka latency) is crucial to most, if not all,
7
- applications and use-cases of ML model inference on mobile devices .
4
+ ์๊ฐ
5
+ ----
6
+ ์ ๋ถ๋ ์๋์ง๋ง, ๋ชจ๋ฐ์ผ ๊ธฐ๊ธฐ์์์ ์ ํ๋ฆฌ์ผ์ด์
๊ณผ ML ๋ชจ๋ธ ์ถ๋ก ์ฌ์ฉ ์ฌ๋ก์
7
+ ์ฑ๋ฅ(์ง์ฐ์๊ฐ)์ ๋งค์ฐ ์ค๋ํ ์ฌํญ์
๋๋ค .
8
8
9
- Today, PyTorch executes the models on the CPU backend pending availability
10
- of other hardware backends such as GPU, DSP, and NPU .
9
+ ์ค๋๋ PyTorch๋ GPU, DSP, NPU์ ๊ฐ์ ํ๋์จ์ด ๋ฐฑ์๋๊ฐ ์ฌ์ฉ ๊ฐ๋ฅํ ๋๊น์ง
10
+ CPU ๋ฐฑ์๋์์ ๋ชจ๋ธ์ ์คํํฉ๋๋ค .
11
11
12
- In this recipe, you will learn :
12
+ ์ด ๋ ์ํผ์์ ๋ฐฐ์ธ ๋ด์ฉ์ :
13
13
14
- - How to optimize your model to help decrease execution time (higher performance, lower latency) on the mobile device.
15
- - How to benchmark (to check if optimizations helped your use case).
14
+ - ๋ชจ๋ฐ์ผ ๊ธฐ๊ธฐ์์ ์คํ ์๊ฐ์ ์ค์ด๋๋ฐ ๋์์ด ๋ (์ฑ๋ฅ์ ๋์ด๊ณ , ์ง์ฐ์๊ฐ์ ์ค์ด๋) ๋ชจ๋ธ ์ต์ ํ ๋ฐฉ๋ฒ
15
+ - ๋ฒค์น๋งํน(์ต์ ํ๊ฐ ์ฌ์ฉ ์ฌ๋ก์ ๋์์ด ๋์๋์ง ํ์ธ) ํ๋ ๋ฐฉ๋ฒ
16
16
17
17
18
- Model preparation
19
- -----------------
18
+ ๋ชจ๋ธ ์ค๋น
19
+ --------
20
20
21
- We will start with preparing to optimize your model to help decrease execution time
22
- (higher performance, lower latency) on the mobile device .
21
+ ๋ชจ๋ฐ์ผ ๊ธฐ๊ธฐ์์ ์คํ ์๊ฐ์ ์ค์ด๋๋ฐ ๋์์ด ๋ (์ฑ๋ฅ์ ๋์ด๊ณ , ์ง์ฐ์๊ฐ์ ์ค์ด๋)
22
+ ๋ชจ๋ธ์ ์ต์ ํ๋ฅผ ์ํ ์ค๋น๋ถํฐ ์์ํฉ๋๋ค .
23
23
24
24
25
- Setup
26
- ^^^^^^^
25
+ ์ค์
26
+ ^^^^
27
27
28
- First we need to installed pytorch using conda or pip with version at least 1.5.0.
28
+ ์ฒซ๋ฒ์งธ๋ก ์ ์ด๋ ๋ฒ์ ์ด 1.5.0 ์ด์์ธ PyTorch๋ฅผ conda๋ pip์ผ๋ก ์ค์นํฉ๋๋ค .
29
29
30
30
::
31
31
32
32
conda install pytorch torchvision -c pytorch
33
33
34
- or
34
+ ๋๋
35
35
36
36
::
37
37
38
38
pip install torch torchvision
39
39
40
- Code your model :
40
+ ๋ชจ๋ธ ์ฝ๋ :
41
41
42
42
::
43
43
@@ -65,82 +65,82 @@ Code your model:
65
65
model = AnnotatedConvBnReLUModel()
66
66
67
67
68
- ``torch.quantization.QuantStub `` and ``torch.quantization.DeQuantStub() `` are no-op stubs, which will be used for quantization step .
68
+ ``torch.quantization.QuantStub `` ์ ``torch.quantization.DeQuantStub() `` ์ ๋ฏธ์ฌ์ฉ ์คํ
(stub)์ด๋ฉฐ, ์์ํ( quantization) ๋จ๊ณ์ ์ฌ์ฉํฉ๋๋ค .
69
69
70
70
71
- 1. Fuse operators using ``torch.quantization.fuse_modules ``
72
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
71
+ 1. ``torch.quantization.fuse_modules `` ์ด์ฉํ์ฌ ์ฐ์ฐ์ ๊ฒฐํฉ(fuse)ํ๊ธฐ
72
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
73
73
74
- Do not be confused that fuse_modules is in the quantization package .
75
- It works for all ``torch.nn.Module ``.
74
+ fuse_modules์ ์์ํ ํจํค์ง ๋ด๋ถ์ ์๋ค๋ ๊ฒ์ ํผ๋ํ์ง ๋ง์ญ์์ค .
75
+ fuse_modules์ ๋ชจ๋ ``torch.nn.Module `` ์์ ๋์ํฉ๋๋ค .
76
76
77
- ``torch.quantization.fuse_modules `` fuses a list of modules into a single module .
78
- It fuses only the following sequence of modules :
77
+ ``torch.quantization.fuse_modules `` ์ ๋ชจ๋๋ค์ ๋ฆฌ์คํธ๋ฅผ ํ๋์ ๋ชจ๋๋ก ๊ฒฐํฉํฉ๋๋ค .
78
+ ์ด๊ฒ์ ์๋ ์์์ ๋ชจ๋๋ค๋ง ๊ฒฐํฉ์ํต๋๋ค :
79
79
80
80
- Convolution, Batch normalization
81
81
- Convolution, Batch normalization, Relu
82
82
- Convolution, Relu
83
83
- Linear, Relu
84
84
85
- This script will fuse Convolution, Batch Normalization and Relu in previously declared model .
85
+ ์ด ์คํฌ๋ฆฝํธ๋ ์ด์ ์ ์ ์ธ๋ ๋ชจ๋ธ์์ Convolution, Batch Normalization, Relu๋ฅผ ๊ฒฐํฉํฉ๋๋ค .
86
86
87
87
::
88
88
89
89
torch.quantization.fuse_modules(model, [['conv', 'bn', 'relu']], inplace=True)
90
90
91
91
92
- 2. Quantize your model
93
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92
+ 2. ๋ชจ๋ธ ์์ํํ๊ธฐ
93
+ ^^^^^^^^^^^^^^^^^
94
94
95
- You can find more about PyTorch quantization in
96
- `the dedicated tutorial <https://pytorch.org/blog/introduction-to-quantization-on-pytorch/ >`_.
95
+ PyTorch ์์ํ์ ๋ํ ๋ด์ฉ์
96
+ `the dedicated tutorial <https://pytorch.org/blog/introduction-to-quantization-on-pytorch/ >`_ ์์ ์ฐพ์ ์ ์์ต๋๋ค .
97
97
98
- Quantization of the model not only moves computation to int8,
99
- but also reduces the size of your model on a disk .
100
- That size reduction helps to reduce disk read operations during the first load of the model and decreases the amount of RAM .
101
- Both of those resources can be crucial for the performance of mobile applications .
102
- This code does quantization, using stub for model calibration function, you can find more about it ` here <https://tutorials.pytorch.kr/advanced/static_quantization_tutorial.html#post-training-static-quantization >`__.
98
+ ๋ชจ๋ธ์ ์์ํ๋ ์ฐ์ฐ์ int8๋ก ์ฎ๊ธฐ๋ฉด์
99
+ ๋์คํฌ์์ ๋ชจ๋ธ ํฌ๊ธฐ๋ฅผ ์ค์ด๊ธฐ๋ ํฉ๋๋ค .
100
+ ์ด๋ฐ ํฌ๊ธฐ ๊ฐ์๋ ๋ชจ๋ธ์ ์ฒ์ ์ฝ์ด ๋ค์ผ ๋ ๋์คํฌ ์ฝ๊ธฐ ์ฐ์ฐ์ ์ค์ด๋๋ฐ ๋์์ ์ฃผ๊ณ ๋จ(RAM)์ ์ด๋๋ ์ค์
๋๋ค .
101
+ ์ด๋ฌํ ๋ ์์์ ๋ชจ๋ฐ์ผ ์ ํ๋ฆฌ์ผ์ด์
์ ์ฑ๋ฅ์ ๋งค์ฐ ์ค์ํ ์ ์์ต๋๋ค .
102
+ ์ด ์ฝ๋๋ ๋ชจ๋ธ ๋ณด์ ( calibration) ํจ์๋ฅผ ์ํด ์คํ
์ ์ฌ์ฉํด์ ์์ํ๋ฅผ ํฉ๋๋ค. ` ์ฌ๊ธฐ <https://tutorials.pytorch.kr/advanced/static_quantization_tutorial.html#post-training-static-quantization >`__ ์์ ๊ด๋ จ๋ ์ฌํญ์ ์ฐพ์ ์ ์์ต๋๋ค .
103
103
104
104
::
105
105
106
106
model.qconfig = torch.quantization.get_default_qconfig('qnnpack')
107
107
torch.quantization.prepare(model, inplace=True)
108
- # Calibrate your model
108
+ # ๋ชจ๋ธ ๋ณด์
109
109
def calibrate(model, calibration_data):
110
- # Your calibration code here
110
+ # ๋ชจ๋ธ ๋ณด์ ์ฝ๋
111
111
return
112
112
calibrate(model, [])
113
113
torch.quantization.convert(model, inplace=True)
114
114
115
115
116
116
117
- 3. Use torch.utils.mobile_optimizer
118
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
117
+ 3. torch.utils.mobile_optimizer ์ฌ์ฉํ๊ธฐ
118
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
119
119
120
- Torch mobile_optimizer package does several optimizations with the scripted model ,
121
- which will help to conv2d and linear operations .
122
- It pre-packs model weights in an optimized format and fuses ops above with relu
123
- if it is the next operation .
120
+ Torch mobile_optimizer ํจํค์ง๋ ์คํฌ๋ฆฝํธ๋ ๋ชจ๋ธ์ ์ด์ฉํด์ ๋ช ๊ฐ์ง ์ต์ ํ๋ฅผ ์ํํ๊ณ ,
121
+ ์ด๋ฌํ ์ต์ ํ๋ conv2d์ ์ ํ ์ฐ์ฐ์ ๋์์ด ๋ฉ๋๋ค .
122
+ ์ด ํจํค์ง๋ ์ต์ ํ๋ ํ์์ผ๋ก ๋ชจ๋ธ ๊ฐ์ค์น๋ฅผ ์ฐ์ ํจํค์งํ๋ฉฐ(pre-packs)
123
+ ๋ค์ ์ฐ์ฐ์ด relu์ด๋ฉด ์์ ์ฐ์ฐ๋ค๊ณผ relu ์ฐ์ฐ์ ๊ฒฐํฉ ์ํต๋๋ค .
124
124
125
- First we script the result model from previous step :
125
+ ๋จผ์ ์ด์ ๋จ๊ณ์์๋ถํฐ ๊ฒฐ๊ณผ ๋ชจ๋ธ์ ์์ฑํฉ๋๋ค :
126
126
127
127
::
128
128
129
129
torchscript_model = torch.jit.script(model)
130
130
131
- Next we call ``optimize_for_mobile `` and save model on the disk .
131
+ ๋ค์์ ``optimize_for_mobile `` ์ ํธ์ถํ๊ณ ๋์คํฌ์ ๋ชจ๋ธ์ ์ ์ฅํฉ๋๋ค .
132
132
133
133
::
134
134
135
135
torchscript_model_optimized = optimize_for_mobile(torchscript_model)
136
136
torch.jit.save(torchscript_model_optimized, "model.pt")
137
137
138
- 4. Prefer Using Channels Last Tensor memory format
139
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
138
+ 4. Channels Last Tensor ๋ฉ๋ชจ๋ฆฌ ํ์ ์ ํํ๊ธฐ
139
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
140
140
141
- Channels Last(NHWC) memory format was introduced in PyTorch 1.4.0. It is supported only for four-dimensional tensors. This memory format gives a better memory locality for most operators, especially convolution. Our measurements showed a 3x speedup of MobileNetV2 model compared with the default Channels First(NCHW) format .
141
+ Channels Last(NHWC) ๋ฉ๋ชจ๋ฆฌ ํ์์ PyTorch 1.4.0์์ ๋์
๋์์ต๋๋ค. ์ด ํ์์ ์ค์ง 4์ฐจ์ ํ
์๋ง์ ์ง์ํฉ๋๋ค. ์ด ๋ฉ๋ชจ๋ฆฌ ํ์์ ๋๋ถ๋ถ์ ์ฐ์ฐ์, ํนํ ํฉ์ฑ๊ณฑ ์ฐ์ฐ์ ๋ ๋์ ๋ฉ๋ชจ๋ฆฌ ์ง์ญ์ฑ์ ์ ๊ณตํฉ๋๋ค. ์ธก์ ๊ฒฐ๊ณผ๋ MobileNetV2 ๋ชจ๋ธ์์ ๊ธฐ๋ณธ Channels First(NCHW) ํ์์ ๋นํด 3๋ฐฐ์ ์๋ ํฅ์์ ๋ณด์ฌ ์ค๋๋ค .
142
142
143
- At the moment of writing this recipe , PyTorch Android java API does not support using inputs in Channels Last memory format. But it can be used on the TorchScript model level, by adding the conversion to it for model inputs .
143
+ ์ด ๋ ์ํผ๋ฅผ ์์ฑํ๋ ์์ ์์๋ , PyTorch Android ์๋ฐ API๋ Channels Last ๋ฉ๋ชจ๋ฆฌ ํ์์ผ๋ก ๋ ์
๋ ฅ์ ์ง์ํ์ง ์์ต๋๋ค. ํ์ง๋ง ๋ชจ๋ธ ์
๋ ฅ์ ์ํด ์ด ๋ฉ๋ชจ๋ฆฌ ํ์์ผ๋ก ๋ณํํ๋ฉด TorchScript ๋ชจ๋ธ ์์ค์์ ์ฌ์ฉ์ด ๊ฐ๋ฅํฉ๋๋ค .
144
144
145
145
.. code-block :: python
146
146
@@ -149,32 +149,32 @@ At the moment of writing this recipe, PyTorch Android java API does not support
149
149
...
150
150
151
151
152
- This conversion is zero cost if your input is already in Channels Last memory format. After it, all operators will work preserving ChannelsLast memory format .
152
+ ์ด ๋ณํ์ ์
๋ ฅ์ด Channels Last ๋ฉ๋ชจ๋ฆฌ ํ์์ด๋ฉด ๋น์ฉ์ด ๋ค์ง ์์ต๋๋ค. ๊ฒฐ๊ตญ์๋ ๋ชจ๋ ์ฐ์ฐ์๊ฐ Channels Last ๋ฉ๋ชจ๋ฆฌ ํ์์ ์ ์งํ๋ฉด์ ์์
์ ํฉ๋๋ค .
153
153
154
- 5. Android - Reusing tensors for forward
155
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
154
+ 5. Android - ์๋ฐฉํฅ ์ ๋ฌ์ ์ํ ํ
์ ์ฌ์ฌ์ฉํ๊ธฐ
155
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
156
156
157
- This part of the recipe is Android only .
157
+ ๋ ์ํผ์์ ์ด ๋ถ๋ถ์ Android์๋ง ํด๋นํฉ๋๋ค .
158
158
159
- Memory is a critical resource for android performance, especially on old devices .
160
- Tensors can need a significant amount of memory .
161
- For example, standard computer vision tensor contains 1*3*224*224 elements,
162
- assuming that data type is float and will need 588Kb of memory .
159
+ ๋ฉ๋ชจ๋ฆฌ๋ Android ์ฑ๋ฅ์ ๋งค์ฐ ์ค์ํ ์์์
๋๋ค. ์ค๋๋ ๋๋ฐ์ด์ค์์ ํนํ๋ ๋ ์ค์ํฉ๋๋ค .
160
+ ํ
์๋ ์๋นํ ์์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ์๋ก ํ ์ ์์ต๋๋ค .
161
+ ์๋ฅผ ๋ค์ด ํ์ค ์ปดํจํฐ ๋น์ ํ
์๋ 1*3*224*224๊ฐ์ ์์๋ฅผ ํฌํจํฉ๋๋ค.
162
+ ๋ฐ์ดํฐ ํ์
์ด float์ด๊ณ 588kb ๋ฉ๋ชจ๋ฆฌ๊ฐ ํ์ํ๋ค๊ณ ๊ฐ์ ํ ๊ฒฝ์ฐ์
๋๋ค .
163
163
164
164
::
165
165
166
166
FloatBuffer buffer = Tensor.allocateFloatBuffer(1*3*224*224);
167
167
Tensor tensor = Tensor.fromBlob(buffer, new long[]{1, 3, 224, 224});
168
168
169
169
170
- Here we allocate native memory as ``java.nio.FloatBuffer `` and creating ``org.pytorch.Tensor `` which storage will be pointing to the memory of the allocated buffer .
170
+ ์ฌ๊ธฐ์์ ๋ค์ดํฐ๋ธ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ``java.nio.FloatBuffer `` ๋ก ํ ๋นํ๊ณ ์ ์ฅ์๊ฐ ํ ๋น๋ ๋ฒํผ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ฐ๋ฆฌํฌ ``org.pytorch.Tensor `` ๋ฅผ ๋ง๋ญ๋๋ค .
171
171
172
- For most of the use cases, we do not do model forward only once, repeating it with some frequency or as fast as possible .
172
+ ๋๋ถ๋ถ์ ์ฌ์ฉ ์ฌ๋ก์์ ๋ชจ๋ธ ์๋ฐฉํฅ ์ ๋ฌ์ ๋จ ํ ๋ฒ๋ง ํ์ง ์๊ณ , ์ผ์ ํ ๋น๋๋ก ํน์ ๊ฐ๋ฅํ ํ ๋นจ๋ฆฌ ์งํํฉ๋๋ค .
173
173
174
- If we are doing new memory allocation for every module forward - that will be suboptimal .
175
- Instead of this, we can reuse the same memory that we allocated on the previous step, fill it with new data, and run module forward again on the same tensor object .
174
+ ๋ง์ฝ ๋ชจ๋ ๋ชจ๋ ์๋ฐฉํฅ ์ ๋ฌ์ ์ํด ๋ฉ๋ชจ๋ฆฌ ํ ๋น์ ์๋ก ํ๋ค๋ฉด - ๊ทธ๊ฑด ์ต์ ํ๊ฐ ์๋๋๋ค .
175
+ ๋์ ์, ์ด์ ๋จ๊ณ์์ ํ ๋นํ ๋์ผํ ๋ฉ๋ชจ๋ฆฌ์ ์ ๋ฐ์ดํฐ๋ฅผ ์ฑ์ฐ๊ณ ๋ชจ๋ ์๋ฐฉํฅ ์ ๋ฌ์ ๋์ผํ ํ
์ ๊ฐ์ฒด์์ ๋ค์ ์คํํจ์ผ๋ก์จ ๋์ผํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฌ์ฉ ํ ์ ์์ต๋๋ค .
176
176
177
- You can check how it looks in code in `pytorch android application example <https://github.com/pytorch/android-demo-app/blob/master/PyTorchDemoApp/app/src/main/java/org/pytorch/demo/vision/ImageClassificationActivity.java#L174 >`_.
177
+ ์ฝ๋๊ฐ ์ด๋ค ์์ผ๋ก ๊ตฌ์ฑ์ด ๋์ด ์๋์ง๋ `pytorch android application example <https://github.com/pytorch/android-demo-app/blob/master/PyTorchDemoApp/app/src/main/java/org/pytorch/demo/vision/ImageClassificationActivity.java#L174 >`_ ์์ ํ์ธํ ์ ์์ต๋๋ค .
178
178
179
179
::
180
180
@@ -196,44 +196,44 @@ You can check how it looks in code in `pytorch android application example <http
196
196
Tensor outputTensor = mModule.forward(IValue.from(mInputTensor)).toTensor();
197
197
}
198
198
199
- Member fields ``mModule ``, ``mInputTensorBuffer `` and ``mInputTensor `` are initialized only once
200
- and buffer is refilled using ``org.pytorch.torchvision.TensorImageUtils.imageYUV420CenterCropToFloatBuffer ``.
199
+ ๋ฉค๋ฒ ๋ณ์ ``mModule `` , ``mInputTensorBuffer `` , ``mInputTensor `` ๋ ๋จ ํ ๋ฒ ์ด๊ธฐํ๋ฅผ ํ๊ณ
200
+ ๋ฒํผ๋ ``org.pytorch.torchvision.TensorImageUtils.imageYUV420CenterCropToFloatBuffer `` ๋ฅผ ์ด์ฉํด์ ๋ค์ ์ฑ์์ง๋๋ค .
201
201
202
- Benchmarking
203
- ------------
202
+ ๋ฒค์น๋งํน
203
+ -------
204
204
205
- The best way to benchmark (to check if optimizations helped your use case) - is to measure your particular use case that you want to optimize, as performance behavior can vary in different environments .
205
+ ๋ฒค์น๋งํน(์ต์ ํ๊ฐ ์ฌ์ฉ ์ฌ๋ก์ ๋์์ด ๋์๋์ง ํ์ธ)ํ๋ ์ต๊ณ ์ ๋ฐฉ๋ฒ์ ์ต์ ํ๋ฅผ ํ๊ณ ์ถ์ ํน์ ํ ์ฌ์ฉ ์ฌ๋ก๋ฅผ ์ธก์ ํ๋ ๊ฒ์
๋๋ค. ์ฑ๋ฅ ์ธก์ ํ์๊ฐ ํ๊ฒฝ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค .
206
206
207
- PyTorch distribution provides a way to benchmark naked binary that runs the model forward,
208
- this approach can give more stable measurements rather than testing inside the application .
207
+ PyTorch ๋ฐฐํฌํ์ ๋ชจ๋ธ ์๋ฐฉํฅ ์ ๋ฌ์ ์คํํ๋ ๋ฐฉ์์ ์ฌ์ฉํด์ ์ํ ๊ทธ๋๋ก์(naked) ๋ฐ์ด๋๋ฆฌ๋ฅผ ๋ฒค์น๋งํนํ๋ ์๋จ์ ์ ๊ณตํฉ๋๋ค.
208
+ ์ด ์ ๊ทผ๋ฒ์ ์ ํ๋ฆฌ์ผ์ด์
๋ด๋ถ์์ ์ํํ๋ ๋ฐฉ๋ฒ๋ณด๋ค ๋ ์์ ์ ์ธ ์ธก์ ์น๋ฅผ ์ ๊ณตํฉ๋๋ค .
209
209
210
210
211
- Android - Benchmarking Setup
212
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
211
+ Android - ๋ฒค์น๋งํน ์ค์
212
+ ^^^^^^^^^^^^^^^^^^^^^^
213
213
214
- This part of the recipe is Android only .
214
+ ๋ ์ํผ์์ ์ด ๋ถ๋ถ์ Android์๋ง ํด๋นํฉ๋๋ค .
215
215
216
- For this you first need to build benchmark binary :
216
+ ๋ฒค์น๋งํน์ ์ํด ๋จผ์ ๋ฒค์น๋งํฌ ๋ฐ์ด๋๋ฆฌ๋ฅผ ๋น๋ํด์ผ ํฉ๋๋ค :
217
217
218
218
::
219
219
220
220
<from-your-root-pytorch-dir>
221
221
rm -rf build_android
222
222
BUILD_PYTORCH_MOBILE=1 ANDROID_ABI=arm64-v8a ./scripts/build_android.sh -DBUILD_BINARY=ON
223
223
224
- You should have arm64 binary at : ``build_android/bin/speed_benchmark_torch ``.
225
- This binary takes ``--model=<path-to-model> ``, ``--input_dim="1,3,224,224" `` as dimension information for the input and ``--input_type="float" `` as the type of the input as arguments .
224
+ ์ด ๊ณณ์ arm64 ๋ฐ์ด๋๋ฆฌ๊ฐ ์์ด์ผ ํฉ๋๋ค : ``build_android/bin/speed_benchmark_torch `` .
225
+ ์ด ๋ฐ์ด๋๋ฆฌ๋ ``--model=<path-to-model> ``, ``--input_dim="1,3,224,224" `` ์ ์
๋ ฅ์ ์ํ ์ฐจ์ ์ ๋ณด๋ก ๋ฐ๊ณ ``--input_type="float" `` ์ผ๋ก ์
๋ ฅ ํ์
์ ์ธ์๋ก ๋ฐ์ต๋๋ค .
226
226
227
- Once you have your android device connected ,
228
- push speedbenchark_torch binary and your model to the phone :
227
+ Android ๋๋ฐ์ด์ค๋ฅผ ์ฐ๊ฒฐํ ์ ์ด ์์ผ๋ฉด ,
228
+ speedbenchark_torch ๋ฐ์ด๋๋ฆฌ์ ๋ชจ๋ธ์ ํฐ์ผ๋ก ํธ์ํฉ๋๋ค :
229
229
230
230
::
231
231
232
232
adb push <speedbenchmark-torch> /data/local/tmp
233
233
adb push <path-to-scripted-model> /data/local/tmp
234
234
235
235
236
- Now we are ready to benchmark your model :
236
+ ์ด์ ๋ชจ๋ธ์ ๋ฒค์น๋งํนํ ์ค๋น๊ฐ ๋์์ต๋๋ค :
237
237
238
238
::
239
239
@@ -245,12 +245,12 @@ Now we are ready to benchmark your model:
245
245
Main run finished. Microseconds per iter: 121318. Iters per second: 8.24281
246
246
247
247
248
- iOS - Benchmarking Setup
249
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
248
+ iOS - ๋ฒค์น๋งํน ์ค์
249
+ ^^^^^^^^^^^^^^^^^^
250
250
251
- For iOS, we'll be using our `TestApp <https://github.com/pytorch/pytorch/tree/master/ios/TestApp >`_ as the benchmarking tool .
251
+ iOS์ ๊ฒฝ์ฐ , ๋ฒค์น๋งํน์ ๋๊ตฌ๋ก `TestApp <https://github.com/pytorch/pytorch/tree/master/ios/TestApp >`_ ์ ์ฌ์ฉํฉ๋๋ค .
252
252
253
- To begin with, let's apply the ``optimize_for_mobile `` method to our python script located at `TestApp/benchmark/trace_model.py <https://github.com/pytorch/pytorch/blob/master/ios/TestApp/benchmark/trace_model.py >`_. Simply modify the code as below .
253
+ ๋จผ์ ``optimize_for_mobile `` ๋ฉ์๋๋ฅผ `TestApp/benchmark/trace_model.py <https://github.com/pytorch/pytorch/blob/master/ios/TestApp/benchmark/trace_model.py >`_ ์ ์๋ ํ์ด์ฌ ์คํฌ๋ฆฝํธ์ ์ ์ฉํฉ๋๋ค. ๊ฐ๋จํ ์๋์ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ ํฉ๋๋ค .
254
254
255
255
::
256
256
@@ -265,21 +265,21 @@ To begin with, let's apply the ``optimize_for_mobile`` method to our python scri
265
265
torchscript_model_optimized = optimize_for_mobile(traced_script_module)
266
266
torch.jit.save(torchscript_model_optimized, "model.pt")
267
267
268
- Now let's run ``python trace_model.py ``. If everything works well, we should be able to generate our optimized model in the benchmark directory .
268
+ ์ด์ ``python trace_model.py `` ๋ฅผ ์คํํฉ์๋ค. ๋ชจ๋ ๊ฒ์ด ์ ์๋ํ๋ค๋ฉด ๋ฒค์น๋งํน ๋๋ ํ ๋ฆฌ ๋ด๋ถ์ ์ต์ ํ๋ ๋ชจ๋ธ์ ์์ฑํ ์ ์์ด์ผ ํฉ๋๋ค .
269
269
270
- Next, we're going to build the PyTorch libraries from source .
270
+ ๋ค์์ ์์ค์์๋ถํฐ PyTorch ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋น๋ํฉ๋๋ค .
271
271
272
272
::
273
273
274
274
BUILD_PYTORCH_MOBILE=1 IOS_ARCH=arm64 ./scripts/build_ios.sh
275
275
276
- Now that we have the optimized model and PyTorch ready, it's time to generate our XCode project and do benchmarking. To do that, we'll be using a ruby script - `setup.rb ` which does the heavy lifting jobs of setting up the XCode project .
276
+ ์ด์ ์ต์ ํ๋ ๋ชจ๋ธ๊ณผ PyTorch๊ฐ ์ค๋น๋์๊ธฐ์ XCode ํ๋ก์ ํธ๋ฅผ ๋ง๋ค๊ณ ๋ฒค์น๋งํนํ ์๊ฐ์
๋๋ค. ์ด๋ฅผ ์ํด XCode ํ๋ก์ ํธ๋ฅผ ์ค์ ํ๋ ๋ฌด๊ฑฐ์ด ์์
์ ์ํํ๋ ๋ฃจ๋น ์คํฌ๋ฆฝํธ `setup.rb ` ๋ฅผ ์ฌ์ฉํฉ๋๋ค .
277
277
278
278
::
279
279
280
280
ruby setup.rb
281
281
282
- Now open the `TestApp.xcodeproj ` and plug in your iPhone, you're ready to go. Below is an example result from iPhoneX
282
+ ์ด์ `TestApp.xcodeproj ` ๋ฅผ ์ด๊ณ iPhone์ ์ฐ๊ฒฐํ๋ฉด ์ค๋น๊ฐ ๋๋ฌ์ต๋๋ค. ์๋๋ iPhoneX์์์ ์์ ๊ฒฐ๊ณผ์
๋๋ค.
283
283
284
284
::
285
285
0 commit comments