Given a dim_order try to map it to torch.memory_format
(dim_order: Optional[List[int]])
| 32 | |
| 33 | |
| 34 | def get_memory_format(dim_order: Optional[List[int]]) -> torch.memory_format: |
| 35 | """ |
| 36 | Given a dim_order try to map it to torch.memory_format |
| 37 | """ |
| 38 | if dim_order is None: |
| 39 | return torch.preserve_format |
| 40 | elif dim_order == _get_contiguous_dim_order(len(dim_order)): |
| 41 | return torch.contiguous_format |
| 42 | elif len(dim_order) == 4 and dim_order == _get_channels_last_dim_order( |
| 43 | len(dim_order) |
| 44 | ): |
| 45 | return torch.channels_last |
| 46 | |
| 47 | raise AssertionError( |
| 48 | f"Failed to map a given dim_order: {dim_order} to a torch.memory_format" |
| 49 | ) |
| 50 | |
| 51 | |
| 52 | def get_dim_order( |