pytorch/test/cpp_extensions
Vitaly Fedyunin 516c7e4456 Adding memory_format to empty and empty_like operators (#20558)
Summary:
Original RFC https://github.com/pytorch/pytorch/issues/19092

To ensure that we are not introducing BC breaking change, empty_like returns contiguous tensor by default.

```python
nCwh = torch.randn(N, C, H, W)
nhwC = nCwh.contiguous(memory_format=torch.channels_last)

new_nCwh = torch.empty_like(nhwC)
new_nCwh.is_contiguous(memory_format=torch.channels_last) == False
```

Now we need a way to preserve memory format in `empty_like`

```python
nCwh = torch.randn(N, C, H, W)
nhwC = nCwh.contiguous(memory_format=torch.channels_last)

new_nhwC = torch.empty_like(nhwC, memory_format=torch.preserve_format)
new_nhwC.is_contiguous(memory_format=torch.channels_last) == True

like_nCwh = torch.empty_like(nCwh, memory_format=torch.preserve_format)
like_nCwh.is_contiguous(memory_format=torch.channels_last) == False
```

Usage of `torch.preserve_format` allows us to avoid `if` constructs.

We can also generate different memory format outputs

```python
nCwh = torch.randn(N, C, H, W)
nhwC = nCwh.contiguous(memory_format=torch.channels_last)

new_nhwC = torch.empty_like(nCwh, memory_format=torch.channels_last)
new_nhwC.is_contiguous(memory_format=torch.channels_last) == True

new_nCwh = torch.empty_like(nhwC, memory_format=torch.contiguous_format)
new_nCwh.is_contiguous(memory_format=torch.channels_last) == False
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/20558

Differential Revision: D15502474

Pulled By: VitalyFedyunin

fbshipit-source-id: 2e120d57eefad6fb8e04b8322c79871392f64331
2019-06-26 11:48:27 -07:00
..
no_python_abi_suffix_test Allow building libraries with setuptools that dont have abi suffix (#14130) 2018-11-27 17:35:53 -08:00
torch_test_cpp_extension Add support for dotted names in CPP Extensions (#6986) 2018-04-29 18:10:03 +02:00
complex_registration_extension.cpp Adding memory_format to empty and empty_like operators (#20558) 2019-06-26 11:48:27 -07:00
cpp_frontend_extension.cpp Better tests/support for Python/C++ inter-op (#15193) 2018-12-14 08:42:10 -08:00
cuda_extension_kernel.cu Improve CUDA extension support (#5324) 2018-02-23 10:15:30 -05:00
cuda_extension_kernel2.cu Improve CUDA extension support (#5324) 2018-02-23 10:15:30 -05:00
cuda_extension.cpp Replace AT_CHECK with TORCH_CHECK [shard 9/10] 2019-05-15 08:05:59 -07:00
cuda_extension.cu Improve CUDA extension support (#5324) 2018-02-23 10:15:30 -05:00
cudnn_extension.cpp Use torch:: instead of at:: in all C++ APIs (#13523) 2018-11-06 14:32:25 -08:00
doubler.h Use torch:: instead of at:: in all C++ APIs (#13523) 2018-11-06 14:32:25 -08:00
extension.cpp Use torch:: instead of at:: in all C++ APIs (#13523) 2018-11-06 14:32:25 -08:00
jit_extension.cpp Unify C++ API with C++ extensions (#11510) 2018-09-24 14:44:21 -07:00
jit_extension2.cpp Unify C++ API with C++ extensions (#11510) 2018-09-24 14:44:21 -07:00
msnpu_extension.cpp Revert D15944971: [pytorch][PR] merge interfaces that have an optional scalartype parameter 2019-06-24 09:41:33 -07:00
setup.py Expose backend extensions to python 2019-02-01 11:00:18 -08:00