Copilot commented on code in PR #40354:
URL: https://github.com/apache/arrow/pull/40354#discussion_r4762582070
##########
cpp/src/arrow/extension/tensor_internal.cc:
##########
@@ -70,44 +70,32 @@ Result<std::vector<int64_t>> ComputeStrides(const
std::shared_ptr<DataType>& val
std::span<const int64_t> shape,
std::span<const int64_t>
permutation) {
const auto ndim = shape.size();
- const int byte_width = value_type->byte_width();
-
- // Use identity permutation if none provided
- std::vector<int64_t> perm;
- if (permutation.empty()) {
- perm.resize(ndim);
- std::iota(perm.begin(), perm.end(), 0);
- } else {
- perm.assign(permutation.begin(), permutation.end());
+ const int64_t byte_width = value_type->byte_width();
+
+ if (ndim == 0) {
+ return std::vector<int64_t>{};
}
- int64_t remaining = 0;
- if (!shape.empty() && shape[0] > 0) {
- remaining = byte_width;
- for (auto i : perm) {
- if (i > 0) {
- if (MultiplyWithOverflow(remaining, shape[i], &remaining)) {
- return Status::Invalid(
- "Strides computed from shape would not fit in 64-bit integer");
- }
- }
+ for (const auto dim : shape) {
+ if (dim == 0) {
+ return std::vector<int64_t>(ndim, byte_width);
}
}
- std::vector<int64_t> strides;
- if (remaining == 0) {
- strides.assign(ndim, byte_width);
- return strides;
+ std::vector<int64_t> strides(ndim);
+ int64_t stride = byte_width;
+ for (size_t i = ndim; i-- > 0;) {
+ strides[i] = stride;
+ if (i > 0 && MultiplyWithOverflow(stride, shape[i], &stride)) {
Review Comment:
The stride correction leaves existing C++ and Python tests expecting the old
layouts, so those tests fail. Update
`cpp/src/arrow/extension/tensor_extension_array_test.cc:679` from `{8, 24}` to
`{8, 32}` for physical shape `{3, 4}` with permutation `{1, 0}`. Also update
both expectations in `python/pyarrow/tests/test_extension_type.py:1741,1747`
from `2*bw` to `3*bw` for shape `[2, 2, 3]` with permutation `[2, 0, 1]`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]