Skip to content

Commit 9a4d702

Browse files
authored
[Mobile GPU] Fix typos (#1220)
* [Mobile GPU] Fix typos * fix the fancy quotes * add .toTensor()
1 parent 58dd3cd commit 9a4d702

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

prototype_source/ios_gpu_workflow.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ Note ``IOS_ARCH`` tells the script to build a arm64 version of Libtorch. This is
7070
Next we need to make some changes in ``TorchModule.mm``
7171

7272
.. code:: objective-c
73-
73+
//#import <LibTorch/LibTorch.h>
74+
#import <torch/script.h>
75+
7476
- (NSArray<NSNumber*>*)predictImage:(void*)imageBuffer {
7577
torch::jit::GraphOptimizerEnabledGuard opguard(false);
7678
at::Tensor tensor = torch::from_blob(imageBuffer, {1, 3, 224, 224}, at::kFloat).metal();
7779
auto outputTensor = _impl.forward({tensor}).toTensor().cpu();
7880
...
79-
return nil;
8081
}
8182
8283
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.

prototype_source/vulkan_workflow.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ Python script to save pretrained mobilenet_v2 to a file:
100100
model = torchvision.models.mobilenet_v2(pretrained=True)
101101
model.eval()
102102
script_model = torch.jit.script(model)
103-
torch.jit.save(script_model, mobilenet2.pt)
103+
torch.jit.save(script_model, "mobilenet2.pt")
104104

105105
PyTorch 1.7 Vulkan backend supports only float 32bit operators. The default model needs additional step that will optimize operators fusing
106106

107107
::
108108

109109
from torch.utils.mobile_optimizer import optimize_for_mobile
110-
script_model_vulkan = optimize_for_mobile(script_model, backend='Vulkan')
110+
script_model_vulkan = optimize_for_mobile(script_model, backend='vulkan')
111111
torch.jit.save(script_model_vulkan, "mobilenet2-vulkan.pt")
112112

113113
The result model can be used only on Vulkan backend as it contains specific to the Vulkan backend operators.
@@ -123,9 +123,9 @@ C++ API
123123
at::is_vulkan_available()
124124
auto tensor = at::rand({1, 2, 2, 3}, at::device(at::kCPU).dtype(at::kFloat));
125125
auto tensor_vulkan = t.vulkan();
126-
auto module = torch::jit::load($PATH);
127-
auto tensor_output_vulkan = module.forward(inputs)
128-
auto tensor_output = tensor_output.cpu()
126+
auto module = torch::jit::load("$PATH");
127+
auto tensor_output_vulkan = module.forward(inputs).toTensor();
128+
auto tensor_output = tensor_output.cpu();
129129

130130
``at::is_vulkan_available()`` function tries to initialize Vulkan backend and if Vulkan device is successfully found and context is created - it will return true, false otherwise.
131131

@@ -185,7 +185,7 @@ For Android API to run model on Vulkan backend we have to specify this during mo
185185
::
186186

187187
import org.pytorch.Device;
188-
Module module = Module.load($PATH, Device.VULKAN)
188+
Module module = Module.load("$PATH", Device.VULKAN)
189189
FloatBuffer buffer = Tensor.allocateFloatBuffer(1 * 3 * 224 * 224);
190190
Tensor inputTensor = Tensor.fromBlob(buffer, new int[]{1, 3, 224, 224});
191191
Tensor outputTensor = mModule.forward(IValue.from(inputTensor)).toTensor();

0 commit comments

Comments
 (0)