Skip to content

Commit e90a8a2

Browse files
authored
Update iOS GPU tutorial based on the lite interpreter support (pytorch#1628)
* Update iOS GPU tutorial based on the lite interpreter support * few minor elabrations added
1 parent ac39d03 commit e90a8a2

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

prototype_source/ios_gpu_workflow.rst

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The next step is going to be converting the mobilenetv2 torchscript model to a M
3838
scripted_model = torch.jit.script(model)
3939
optimized_model = optimize_for_mobile(scripted_model, backend='metal')
4040
print(torch.jit.export_opnames(optimized_model))
41-
torch.jit.save(optimized_model, './mobilenetv2_metal.pt')
41+
torch.jit._save_for_lite_interpreter(optimized_model, './mobilenetv2_metal.pt')
4242
4343
Note that the ``torch.jit.export_opnames(optimized_model)`` is going to dump all the optimized operators from the ``optimized_mobile``. If everything works well, you should be able to see the following ops being printed out from the console
4444

@@ -65,31 +65,34 @@ In this section, we'll be using the `HelloWorld example <https://github.com/pyto
6565
6666
IOS_ARCH=arm64 USE_PYTORCH_METAL=1 ./scripts/build_ios.sh
6767
68-
Note ``IOS_ARCH`` tells the script to build a arm64 version of Libtorch. This is because in PyTorch, Metal is only available for the iOS devices that support the Apple A9 chip or above. Once the build finished, follow the `Build PyTorch iOS libraries from source <https://pytorch.org/mobile/ios/#build-pytorch-ios-libraries-from-source>`_ section from the iOS tutorial to setup the XCode settings properly. Don't forget to copy the `./mobilenetv2_metal.pt` to your XCode project.
68+
Note ``IOS_ARCH`` tells the script to build a arm64 version of Libtorch. This is because in PyTorch, Metal is only available for the iOS devices that support the Apple A9 chip or above. Once the build finished, follow the `Build PyTorch iOS libraries from source <https://pytorch.org/mobile/ios/#build-pytorch-ios-libraries-from-source>`_ section from the iOS tutorial to setup the XCode settings properly. Don't forget to copy the `./mobilenetv2_metal.pt` to your XCode project and modify the model file path accordingly.
6969

7070
Next we need to make some changes in ``TorchModule.mm``
7171

7272
.. code:: objective-c
7373
74-
// #import <Libtorch-Lite.h>
75-
// If it's built from source with xcode, comment out the line above
74+
...
75+
// #import <Libtorch-Lite/Libtorch-Lite.h>
76+
// If it's built from source with Xcode, comment out the line above
7677
// and use following headers
7778
#include <torch/csrc/jit/mobile/import.h>
7879
#include <torch/csrc/jit/mobile/module.h>
7980
#include <torch/script.h>
81+
...
8082
8183
- (NSArray<NSNumber*>*)predictImage:(void*)imageBuffer {
82-
torch::jit::GraphOptimizerEnabledGuard opguard(false);
84+
c10::InferenceMode mode;
8385
at::Tensor tensor = torch::from_blob(imageBuffer, {1, 3, 224, 224}, at::kFloat).metal();
8486
auto outputTensor = _impl.forward({tensor}).toTensor().cpu();
8587
...
8688
}
89+
...
8790
8891
As you can see, we simply just call ``.metal()`` to move our input tensor from CPU to GPU, and then call ``.cpu()`` to move the result back. Internally, ``.metal()`` will copy the input data from the CPU buffer to a GPU buffer with a GPU compatible memory format. When `.cpu()` is invoked, the GPU command buffer will be flushed and synced. After `forward` finished, the final result will then be copied back from the GPU buffer back to a CPU buffer.
8992

90-
The last step we have to do is to add the `Accelerate.framework` and the `MetalShaderPerformance.framework` to your xcode project.
93+
The last step we have to do is to add the `Accelerate.framework` and the `MetalShaderPerformance.framework` to your xcode project. (Open your project via XCode, go to your project target’s "General" tab, locate the "Frameworks, Libraries and Embedded Content" section and click the "+" button)
9194

92-
If everything works fine, you should be able to see the inference results on your phone. The result below was captured from an iPhone11 device
95+
If everything works fine, you should be able to see the inference results on your phone. The result below was captured from an iPhone 11 device
9396

9497
.. code:: shell
9598

0 commit comments

Comments
 (0)