Skip to content

Commit 89efbf2

Browse files
committed
[Mobile Perf Recipe] Add the benchmarking part for iOS
1 parent 6285b8f commit 89efbf2

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

recipes_source/mobile_perf.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,45 @@ Now we are ready to benchmark your model:
221221
Main run finished. Microseconds per iter: 121318. Iters per second: 8.24281
222222

223223

224+
iOS
225+
-------
226+
227+
For iOS, we'll be using our `TestApp <https://github.com/pytorch/pytorch/tree/master/ios/TestApp>_` as the benchmarking tool.
228+
229+
To begin with, let's apply the ``optimize_for_mobile`` method to our python script located at `TestApp/benchmark/trace_mode.py <https://github.com/pytorch/pytorch/blob/master/ios/TestApp/benchmark/trace_model.py>`_. Simply modify the code as below.
230+
231+
::
232+
233+
import torch
234+
import torchvision
235+
from torch.utils.mobile_optimizer import optimize_for_mobile
236+
237+
model = torchvision.models.mobilenet_v2(pretrained=True)
238+
model.eval()
239+
example = torch.rand(1, 3, 224, 224)
240+
traced_script_module = torch.jit.trace(model, example)
241+
torchscript_model_optimized = optimize_for_mobile(traced_script_module)
242+
torch.jit.save(torchscript_model_optimized, "model.pt")
243+
244+
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.
245+
246+
Next, we're going to build the PyTorch libraries from source.
247+
248+
::
249+
250+
BUILD_PYTORCH_MOBILE=1 IOS_ARCH=arm64 ./scripts/build_ios.sh
251+
252+
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.
253+
254+
::
255+
256+
ruby setup.rb
257+
258+
Now open the `TestApp.xcodeproj` and plug in your iPhone, you're ready to go. Below is an example result from iPhoneX
259+
260+
::
261+
262+
TestApp[2121:722447] Main runs
263+
TestApp[2121:722447] Main run finished. Milliseconds per iter: 28.767
264+
TestApp[2121:722447] Iters per second: : 34.762
265+
TestApp[2121:722447] Done.

0 commit comments

Comments
 (0)