From 44392d6374265710b0408e3d3f729f1b074a3417 Mon Sep 17 00:00:00 2001 From: Tao Xu Date: Mon, 6 Jul 2020 01:49:39 -0700 Subject: [PATCH 1/2] [Mobile Perf Recipe] Add the benchmarking part for iOS --- recipes_source/mobile_perf.rst | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/recipes_source/mobile_perf.rst b/recipes_source/mobile_perf.rst index d0fbf3cb6fb..c3dedae2222 100644 --- a/recipes_source/mobile_perf.rst +++ b/recipes_source/mobile_perf.rst @@ -221,3 +221,45 @@ Now we are ready to benchmark your model: Main run finished. Microseconds per iter: 121318. Iters per second: 8.24281 +iOS +------- + +For iOS, we'll be using our `TestApp` as the benchmarking tool. + +To begin with, let's apply the ``optimize_for_mobile`` method to our python script located at `TestApp/benchmark/trace_mode.py`. Simply modify the code as below. + +:: + + import torch + import torchvision + from torch.utils.mobile_optimizer import optimize_for_mobile + + model = torchvision.models.mobilenet_v2(pretrained=True) + model.eval() + example = torch.rand(1, 3, 224, 224) + traced_script_module = torch.jit.trace(model, example) + torchscript_model_optimized = optimize_for_mobile(traced_script_module) + torch.jit.save(torchscript_model_optimized, "model.pt") + +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. + +Next, we're going to build the PyTorch libraries from source. + +:: + + BUILD_PYTORCH_MOBILE=1 IOS_ARCH=arm64 ./scripts/build_ios.sh + +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. + +:: + + ruby setup.rb + +Now open the `TestApp.xcodeproj` and plug in your iPhone, you're ready to go. Below is an example result from iPhoneX + +:: + + TestApp[2121:722447] Main runs + TestApp[2121:722447] Main run finished. Milliseconds per iter: 28.767 + TestApp[2121:722447] Iters per second: : 34.762 + TestApp[2121:722447] Done. From 89efbf2157798575df2a94a8546ea32e29103b5d Mon Sep 17 00:00:00 2001 From: Tao Xu Date: Mon, 6 Jul 2020 01:49:39 -0700 Subject: [PATCH 2/2] [Mobile Perf Recipe] Add the benchmarking part for iOS --- recipes_source/mobile_perf.rst | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/recipes_source/mobile_perf.rst b/recipes_source/mobile_perf.rst index d0fbf3cb6fb..7246b171a04 100644 --- a/recipes_source/mobile_perf.rst +++ b/recipes_source/mobile_perf.rst @@ -221,3 +221,45 @@ Now we are ready to benchmark your model: Main run finished. Microseconds per iter: 121318. Iters per second: 8.24281 +iOS +------- + +For iOS, we'll be using our `TestApp _` as the benchmarking tool. + +To begin with, let's apply the ``optimize_for_mobile`` method to our python script located at `TestApp/benchmark/trace_mode.py `_. Simply modify the code as below. + +:: + + import torch + import torchvision + from torch.utils.mobile_optimizer import optimize_for_mobile + + model = torchvision.models.mobilenet_v2(pretrained=True) + model.eval() + example = torch.rand(1, 3, 224, 224) + traced_script_module = torch.jit.trace(model, example) + torchscript_model_optimized = optimize_for_mobile(traced_script_module) + torch.jit.save(torchscript_model_optimized, "model.pt") + +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. + +Next, we're going to build the PyTorch libraries from source. + +:: + + BUILD_PYTORCH_MOBILE=1 IOS_ARCH=arm64 ./scripts/build_ios.sh + +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. + +:: + + ruby setup.rb + +Now open the `TestApp.xcodeproj` and plug in your iPhone, you're ready to go. Below is an example result from iPhoneX + +:: + + TestApp[2121:722447] Main runs + TestApp[2121:722447] Main run finished. Milliseconds per iter: 28.767 + TestApp[2121:722447] Iters per second: : 34.762 + TestApp[2121:722447] Done.