-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
GH-34796: [C++] Add FromTensor, ToTensor and strides methods to FixedShapeTensorArray #34797
Conversation
5a62709
to
f72e7a2
Compare
4bfc601
to
a12a2ef
Compare
a12a2ef
to
7279c09
Compare
In my last review of #8510 before splitting it (#8510 (review)), I still had some comments/questions on the FromTensor/ToTensor implementation. I am assuming that nothing fundamentally changed here (just split off what was at that point in that PR?). |
auto cell_shapes = std::vector<std::vector<int64_t>>{{3, 4}, {3, 4}, {4, 3}, {4, 3}}; | ||
auto permutations = std::vector<std::vector<int64_t>>{{0, 1}, {1, 0}, {0, 1}, {1, 0}}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I assume this is the equivalent place compared to where I commented before on the previous tests: for the second test case (shape {3, 3, 4}, cell_shape {3, 4} and permutation of {1, 0}), shouldn't the expected fixed_shape_tensor type's shape be {4, 3}?
The tensor itself has shape {3, 3, 4} (so {3, 4} for the individual tensor element), but has a permutation, so {4, 3} ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed ToTensor/FromTensor
to permute original shape to cell_shape and back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose we should permute also dim_names
? My original thought wast to keep the original shape and only use permutation generating strides.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spec says "dim_names ... map to the physical layout (row-major)". So they should correspond to the shape/dimensions of the extension type (and so not necessarily the original tensor).
(so I assume that is a yes)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I changed the FromTensor/ToTensor
to permute the dim_names
accordingly.
A change introduced late in #8510 (and then removed) was that import torch
def print_strides(shape, permutation):
permutation2 = [p - 1 for p in permutation[1:]]
shape2 = shape[1:]
x = torch.randn(shape).permute(permutation)
y = torch.randn(shape2).permute(permutation2)
strides = [z * 8 for z in x.stride()]
strides2 = [z * 8 for z in y.stride()]
print("[full tensor] shape:", shape, "permutation:", permutation, "strides:", strides)
print("[cell tensor] shape:", shape2, "permutation:", permutation2, "strides:", strides2)
shapes = [
(3, 3, 4),
(3, 3, 4),
(3, 4, 3),
(3, 4, 3),
]
permutations = [
(0, 1, 2),
(0, 2, 1),
(0, 1, 2),
(0, 2, 1),
]
for shape, permutation in zip(shapes, permutations):
print_strides(shape, permutation)
|
/// | ||
/// This method will create a Tensor from a FixedShapeTensorArray, setting its | ||
/// first dimension as length equal to the FixedShapeTensorArray's length and the | ||
/// remaining dimensions as the FixedShapeTensorType's shape. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add here to the docstring that this will automatically reshape the resulting Tensor according to the permutation
metadata of the FixedShapeTensorArray?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
/// \brief Compute strides of FixedShapeTensorType | ||
static Status ComputeStrides(const FixedWidthType& type, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a need for having this public in addition to strides()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason it's like this now is because both FixedShapeTensorType
and FixedShapeTensorArray
are using ComputeStrides
, but ComputeStrides
is a FixedShapeTensorType
method. I'll see if I can put it into another namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to anonymous namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates, the converted shape and dim_names look correct to me now.
Benchmark runs are scheduled for baseline = c40e658 and contender = aff876a. aff876a is a master commit associated with this PR. Results will be available as each benchmark for each run completes. |
) ### Rationale for this change `std::reinterpret_pointer_cast` was introduced with FixedShapeTensor PR (#34797) but is not available on OSX (see #35143). ### What changes are included in this PR? This change switches to using `internal::checked_pointer_cast`. ### Are these changes tested? Change is tested in CI, but should also be verified on crossbow. ### Are there any user-facing changes? No. * Closes: #35143 Authored-by: Rok Mihevc <rok@mihevc.org> Signed-off-by: Dewey Dunnington <dewey@voltrondata.com>
) ### Rationale for this change `std::reinterpret_pointer_cast` was introduced with FixedShapeTensor PR (#34797) but is not available on OSX (see #35143). ### What changes are included in this PR? This change switches to using `internal::checked_pointer_cast`. ### Are these changes tested? Change is tested in CI, but should also be verified on crossbow. ### Are there any user-facing changes? No. * Closes: #35143 Authored-by: Rok Mihevc <rok@mihevc.org> Signed-off-by: Dewey Dunnington <dewey@voltrondata.com>
… FixedShapeTensorArray (apache#34797) ### Rationale for this change We want to enable converting Tensors to FixedShapeTensorArrays and the other way around. ### What changes are included in this PR? This adds FromTensor, ToTensor to FixedShapeTensorArrays and strides method to FixedShapeTensorType. ### Are these changes tested? Yes. ### Are there any user-facing changes? This adds FromTensor, ToTensor and strides are user facing methods. * Closes: apache#34796 Authored-by: Rok Mihevc <rok@mihevc.org> Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
apache#35154) ### Rationale for this change `std::reinterpret_pointer_cast` was introduced with FixedShapeTensor PR (apache#34797) but is not available on OSX (see apache#35143). ### What changes are included in this PR? This change switches to using `internal::checked_pointer_cast`. ### Are these changes tested? Change is tested in CI, but should also be verified on crossbow. ### Are there any user-facing changes? No. * Closes: apache#35143 Authored-by: Rok Mihevc <rok@mihevc.org> Signed-off-by: Dewey Dunnington <dewey@voltrondata.com>
… FixedShapeTensorArray (apache#34797) ### Rationale for this change We want to enable converting Tensors to FixedShapeTensorArrays and the other way around. ### What changes are included in this PR? This adds FromTensor, ToTensor to FixedShapeTensorArrays and strides method to FixedShapeTensorType. ### Are these changes tested? Yes. ### Are there any user-facing changes? This adds FromTensor, ToTensor and strides are user facing methods. * Closes: apache#34796 Authored-by: Rok Mihevc <rok@mihevc.org> Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
apache#35154) ### Rationale for this change `std::reinterpret_pointer_cast` was introduced with FixedShapeTensor PR (apache#34797) but is not available on OSX (see apache#35143). ### What changes are included in this PR? This change switches to using `internal::checked_pointer_cast`. ### Are these changes tested? Change is tested in CI, but should also be verified on crossbow. ### Are there any user-facing changes? No. * Closes: apache#35143 Authored-by: Rok Mihevc <rok@mihevc.org> Signed-off-by: Dewey Dunnington <dewey@voltrondata.com>
… FixedShapeTensorArray (apache#34797) ### Rationale for this change We want to enable converting Tensors to FixedShapeTensorArrays and the other way around. ### What changes are included in this PR? This adds FromTensor, ToTensor to FixedShapeTensorArrays and strides method to FixedShapeTensorType. ### Are these changes tested? Yes. ### Are there any user-facing changes? This adds FromTensor, ToTensor and strides are user facing methods. * Closes: apache#34796 Authored-by: Rok Mihevc <rok@mihevc.org> Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
apache#35154) ### Rationale for this change `std::reinterpret_pointer_cast` was introduced with FixedShapeTensor PR (apache#34797) but is not available on OSX (see apache#35143). ### What changes are included in this PR? This change switches to using `internal::checked_pointer_cast`. ### Are these changes tested? Change is tested in CI, but should also be verified on crossbow. ### Are there any user-facing changes? No. * Closes: apache#35143 Authored-by: Rok Mihevc <rok@mihevc.org> Signed-off-by: Dewey Dunnington <dewey@voltrondata.com>
Rationale for this change
We want to enable converting Tensors to FixedShapeTensorArrays and the other way around.
What changes are included in this PR?
This adds FromTensor, ToTensor to FixedShapeTensorArrays and strides method to FixedShapeTensorType.
Are these changes tested?
Yes.
Are there any user-facing changes?
This adds FromTensor, ToTensor and strides are user facing methods.