The instructions for speed_benchmark_torch
worked for me on the first try!
If anyone else wants try this on a Pixel 3 android phone, here is the setup that worked for me:
# in bash shell
cd pytorch #where I have my `git clone` of pytorch
export ANDROID_ABI=arm64-v8a
export ANDROID_NDK=/path/to/Android/Sdk/ndk/21.0.6113669/
./scripts/build_android.sh \
-DBUILD_BINARY=ON \
-DBUILD_CAFFE2_MOBILE=OFF \
-DCMAKE_PREFIX_PATH=$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())') \
-DPYTHON_EXECUTABLE=$(python -c 'import sys; print(sys.executable)') \
# speed_benchmark_torch appears in pytorch/build_android/install/bin/speed_benchmark_torch
Next, I followed these instructions to export a resnet18 torchscript model:
#in python
import torch
import torchvision
model = torchvision.models.resnet18(pretrained=True)
model.eval()
example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save("resnet18.pt")
Then, I put the files onto the android device
# in bash shell on linux host computer that's plugged into Pixel 3 phone
adb shell mkdir /data/local/tmp/pt
adb push build_android/install/bin/speed_benchmark_torch /data/local/tmp/pt
adb push resnet18.pt /data/local/tmp/pt
And finally I run on the android device
# in bash shell on linux host computer that's plugged into Pixel 3 phone
adb shell /data/local/tmp/pt/speed_benchmark_torch \
--model /data/local/tmp/pt/resnet18.pt --input_dims="1,3,224,224" \
--input_type=float --warmup=5 --iter 20
It prints:
Starting benchmark.
Running warmup runs.
Main runs.
Main run finished. Milliseconds per iter: 188.382. Iters per second: 5.30836
Pretty good! I believe resnet18 is about 4 gflop (that is, 2 gmac) per frame, so (4 gmac) / (188 ms) = 21 gflop/s. Not bad for ARM CPUs! (At least I assume it’s executing on the ARM CPUs and not any GPUs or other accelerators.)
Also, this whole process took me about 25 minutes, and everything worked on the first try. I use pytorch day-to-day, but I have very little experience with android, and this was also my first time using torchscript, so I’m surprised and impressed that it was so straightforward.