Skip to content

Commit fff3fba

Browse files
author
Jessica Lin
authored
Merge pull request #1042 from IvanKobzarev/mobile_perf_channels_last
[mobile_perf][recipe] Add ChannelsLast recommendation
2 parents 7b615c1 + e6ac673 commit fff3fba

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

recipes_source/mobile_perf.rst

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Code your model:
5454
self.dequant = torch.quantization.DeQuantStub()
5555

5656
def forward(self, x):
57+
x.contiguous(memory_format=torch.channels_last)
5758
x = self.quant(x)
5859
x = self.conv(x)
5960
x = self.bn(x)
@@ -134,9 +135,24 @@ Next we call ``optimize_for_mobile`` and save model on the disk.
134135
torchscript_model_optimized = optimize_for_mobile(torchscript_model)
135136
torch.jit.save(torchscript_model_optimized, "model.pt")
136137

138+
4. Prefer Using Channels Last Tensor memory format
139+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
137140

138-
4. Android. Reusing tensors for forward.
139-
#############################################################
141+
Channels Last(NHWC) memory format was introduced in PyTorch 1.4.0. It is supported only for four-dimensional tensors. This memory format gives a better memory locality for most operators, especially convolution. Our measurements showed a 3x speedup of MobileNetV2 model compared with the default Channels First(NCHW) format.
142+
143+
At the moment of writing this recipe, PyTorch Android java API does not support using inputs in Channels Last memory format. But it can be used on the TorchScript model level, by adding the conversion to it for model inputs.
144+
145+
.. code-block:: python
146+
147+
def forward(self, x):
148+
x.contiguous(memory_format=torch.channels_last)
149+
...
150+
151+
152+
This conversion is zero cost if your input is already in Channels Last memory format. After it, all operators will work preserving ChannelsLast memory format.
153+
154+
5. Android. Reusing tensors for forward
155+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
140156

141157
This recipe is Android only.
142158
Memory is a critical resource for android performance, especially on old devices.

0 commit comments

Comments
 (0)