Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PyTorch][Tensor] Introduce tensor.dim_order #106835

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/tensors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ Tensor class reference
Tensor.digamma
Tensor.digamma_
Tensor.dim
Tensor.dim_order
Tensor.dist
Tensor.div
Tensor.div_
Expand Down
20 changes: 20 additions & 0 deletions test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7736,6 +7736,26 @@ def test_helper(dim1, dim2, memory_format):
test_helper((3, 3), (3, 3, 3, 3), torch.channels_last)
test_helper((3, 3, 3), (3, 3, 3, 3, 3), torch.channels_last_3d)

def test_dim_order(self):
shape = (2, 3, 5, 7)

t = torch.empty(shape)
self.assertSequenceEqual(t.dim_order(), (0, 1, 2, 3), seq_type=tuple)
# transpose doesn't really change the underlying physical memory
# so expecting dim_order change to reflect that (like strides)
self.assertSequenceEqual(t.transpose(0, 1).dim_order(), (1, 0, 2, 3))

t = torch.empty(shape, memory_format=torch.channels_last)
self.assertSequenceEqual(t.dim_order(), (0, 2, 3, 1))

t = torch.empty((2, 3, 5, 7, 8), memory_format=torch.channels_last_3d)
self.assertSequenceEqual(t.dim_order(), (0, 2, 3, 4, 1))

for dim_order in itertools.permutations(range(4)):
self.assertSequenceEqual(
dim_order, torch.empty_permuted(shape, dim_order).dim_order()
)

def test_subclass_tensors(self):
# raise an error when trying to subclass FloatTensor
with self.assertRaisesRegex(TypeError, "type 'torch.FloatTensor' is not an acceptable base type"):
Expand Down
30 changes: 30 additions & 0 deletions torch/_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,36 @@ def to_sparse_coo(self):
"""
return self.to_sparse()

def dim_order(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to add torch function override support here, look for things like has_torch_function_unary in this file

"""

dim_order() -> tuple

Returns a tuple of int describing the dim order or physical layout of :attr:`self`.

Args:
None

Dim order represents how dimensions are laid out in memory,
starting from the outermost to the innermost dimension.

Example::
>>> torch.empty((2, 3, 5, 7)).dim_order()
(0, 1, 2, 3)
>>> torch.empty((2, 3, 5, 7), memory_format=torch.channels_last).dim_order()
(0, 2, 3, 1)

.. warning::
The dim_order tensor API is experimental and subject to change.

"""
if has_torch_function_unary(self):
return handle_torch_function(Tensor.dim_order, (self,), self)

import torch._prims_common as utils

return tuple(utils.compute_elementwise_output_logical_to_physical_perm(self))

def _update_names(self, names, inplace):
if has_torch_function_unary(self):
return handle_torch_function(
Expand Down
2 changes: 2 additions & 0 deletions torch/_torch_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12437,6 +12437,8 @@ def merge_dicts(*dicts):
(105, 1, 21, 3)
>>> torch.empty_permuted((2, 3, 5, 7), (0, 2, 3, 1)).stride()
(105, 1, 21, 3)
>>> torch.empty_permuted((2, 3, 5, 7), (0, 2, 3, 1)).dim_order()
(0, 2, 3, 1)
""".format(
**factory_common_args
),
Expand Down
1 change: 1 addition & 0 deletions torch/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,7 @@ def get_testing_overrides() -> Dict[Callable, Callable]:
Tensor.dense_dim: lambda self: -1,
Tensor.diagonal_scatter: lambda self, src, offset=0, dim1=0, dim2=1: -1,
Tensor.dim: lambda self: -1,
Tensor.dim_order: lambda self: -1,
Tensor.double: lambda self, memory_format=torch.preserve_format: -1,
Tensor.cdouble: lambda self, memory_format=torch.preserve_format: -1,
Tensor.element_size: lambda self: -1,
Expand Down