mm(x)
  end = time.perf_counter()
  print("Contiguous: ", end-start)
# inference on channels-last
  start = time.perf_counter()
  for i in range(20):
    mm(xcl)
  end = time.perf_counter()
  print("Channels-Last: ", end-start)
with torch.inference_mode():
  scripted, optimized = get_optimized_model(m)
print("Runtimes for torchscripted model: ")
  compare_contiguous_CL(scripted.eval())
  print()
  print("Runtimes for mobile-optimized model: ")
  compare_contiguous_CL(optimized.eval())
Outputs (on an Intel Core i9 CPU):
Runtimes for torchscripted model:
Contiguous:  1.6711160129999598
Channels-Last:  1.6678222839999535
Runtimes for mobile-optimized model:
Contiguous:  0.5712863490000473
Channels-Last:  0.46113000699995155
```
Conclusion
The Memory Layout of an input tensor can significantly impact a model’s running time. For Vision Models, prefer a Channels Last memory format to get the most out of your PyTorch models.
References