-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[XLA:Python] Modify DLPack behavior with unit dimensions.
As discovered in jax-ml/jax#24680, when a PyTorch tensor has a dimension with size `1`, it seems to report the DLPack stride for that dimension as `1`. This means that even when the torch Tensor is formally row-major, the imported array isn't. This shouldn't really matter (the placement of unit dimensions can be arbitrary!), but in practice (since XLA:CPU ignores layouts - that's another issue that is being worked on!) it can be annoying. This change updates the behavior to always produce row-major layouts for unit dimensions wrt to their neighbors. PiperOrigin-RevId: 696341186
- Loading branch information
1 parent
73ddc48
commit 78a7de8
Showing
5 changed files
with
211 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* Copyright 2024 The OpenXLA Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include "xla/python/dlpack_strides.h" | ||
|
||
#include <algorithm> | ||
#include <cstdint> | ||
#include <limits> | ||
#include <numeric> | ||
#include <vector> | ||
|
||
#include "absl/algorithm/container.h" | ||
#include "absl/status/statusor.h" | ||
#include "absl/strings/str_join.h" | ||
#include "absl/types/span.h" | ||
#include "xla/util.h" | ||
#include "tsl/platform/logging.h" | ||
|
||
namespace xla { | ||
|
||
absl::StatusOr<std::vector<int64_t>> StridesToLayout( | ||
absl::Span<int64_t const> dims, absl::Span<int64_t const> strides) { | ||
CHECK_EQ(dims.size(), strides.size()); | ||
|
||
// Handle unit dimensions by inserting the previous stride. This has the | ||
// effect of always producing row-major layouts for unit dimensions, which | ||
// isn't strictly necessary, but is convenient since XLA defaults to row-major | ||
// layouts. | ||
std::vector<int64_t> strides_(strides.size()); | ||
for (int64_t i = 0; i < strides.size(); ++i) { | ||
if (i == 0 || dims[i] > 1) { | ||
strides_[i] = strides[i]; | ||
} else { | ||
strides_[i] = strides[i - 1]; | ||
} | ||
} | ||
|
||
std::vector<int64_t> minor_to_major(dims.size()); | ||
std::iota(minor_to_major.begin(), minor_to_major.end(), 0); | ||
absl::c_sort(minor_to_major, [&](int a, int b) { | ||
if (strides_[a] < strides_[b]) { | ||
return true; | ||
} | ||
if (strides_[a] > strides_[b]) { | ||
return false; | ||
} | ||
// If two dimensions have the same stride, prefer the major-to-minor | ||
// interpretation of the ordering, since that's what JAX wants. | ||
return b < a; | ||
}); | ||
int64_t stride = 1; | ||
for (int64_t d : minor_to_major) { | ||
if (dims[d] > 1 && strides[d] != stride) { | ||
return Unimplemented( | ||
"Only DLPack tensors with trivial (compact) striding are supported; " | ||
"i.e., tensors whose striding represents a transposition of the " | ||
"underlying buffer but not broadcasting. Dimensions were: [%s], " | ||
"strides were [%s].", | ||
absl::StrJoin(dims, ","), absl::StrJoin(strides, ",")); | ||
} | ||
stride *= dims[d]; | ||
} | ||
return minor_to_major; | ||
} | ||
|
||
} // namespace xla |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* Copyright 2024 The OpenXLA Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#ifndef XLA_PYTHON_DLPACK_STRIDES_H_ | ||
#define XLA_PYTHON_DLPACK_STRIDES_H_ | ||
|
||
#include <cstdint> | ||
#include <vector> | ||
|
||
#include "absl/status/statusor.h" | ||
#include "absl/types/span.h" | ||
|
||
namespace xla { | ||
|
||
absl::StatusOr<std::vector<int64_t>> StridesToLayout( | ||
absl::Span<int64_t const> dims, absl::Span<int64_t const> strides); | ||
|
||
} // namespace xla | ||
|
||
#endif // XLA_PYTHON_DLPACK_STRIDES_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* Copyright 2024 The OpenXLA Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include "xla/python/dlpack_strides.h" | ||
|
||
#include <cstdint> | ||
#include <vector> | ||
|
||
#include "absl/types/span.h" | ||
#include "tsl/platform/test.h" | ||
|
||
namespace xla { | ||
namespace { | ||
|
||
TEST(DlpackStridesTest, basic) { | ||
std::vector<int64_t> dims = {2, 3, 4}; | ||
std::vector<int64_t> strides = {12, 4, 1}; | ||
auto layout = StridesToLayout(absl::MakeSpan(dims), absl::MakeSpan(strides)); | ||
EXPECT_TRUE(layout.ok()); | ||
EXPECT_EQ(layout.value(), std::vector<int64_t>({2, 1, 0})); | ||
|
||
std::vector<int64_t> strides_cm = {1, 2, 6}; | ||
auto layout_cm = | ||
StridesToLayout(absl::MakeSpan(dims), absl::MakeSpan(strides_cm)); | ||
EXPECT_TRUE(layout_cm.ok()); | ||
EXPECT_EQ(layout_cm.value(), std::vector<int64_t>({0, 1, 2})); | ||
} | ||
|
||
TEST(DlpackStridesTest, unitDim) { | ||
// Row-major | ||
std::vector<int64_t> dims = {2, 1, 3, 4}; | ||
std::vector<int64_t> strides = {12, 12, 4, 1}; | ||
auto layout = StridesToLayout(absl::MakeSpan(dims), absl::MakeSpan(strides)); | ||
EXPECT_TRUE(layout.ok()); | ||
EXPECT_EQ(layout.value(), std::vector<int64_t>({3, 2, 1, 0})); | ||
|
||
std::vector<int64_t> strides2 = {12, 1, 4, 1}; | ||
auto layout2 = | ||
StridesToLayout(absl::MakeSpan(dims), absl::MakeSpan(strides2)); | ||
EXPECT_TRUE(layout2.ok()); | ||
EXPECT_EQ(layout2.value(), std::vector<int64_t>({3, 2, 1, 0})); | ||
|
||
// Column-major. Note that in these cases, since one of the dimensions is 1, | ||
// there are several valid layouts that we could produce. We choose to prefer | ||
// row-major whenever there are multiple valid layouts, so the output layouts | ||
// here aren't completely column-major. | ||
std::vector<int64_t> strides_cm = {1, 2, 2, 6}; | ||
auto layout_cm = | ||
StridesToLayout(absl::MakeSpan(dims), absl::MakeSpan(strides_cm)); | ||
EXPECT_TRUE(layout_cm.ok()); | ||
EXPECT_EQ(layout_cm.value(), std::vector<int64_t>({1, 0, 2, 3})); | ||
|
||
std::vector<int64_t> strides2_cm = {1, 1, 2, 6}; | ||
auto layout2_cm = | ||
StridesToLayout(absl::MakeSpan(dims), absl::MakeSpan(strides2_cm)); | ||
EXPECT_TRUE(layout2_cm.ok()); | ||
EXPECT_EQ(layout2_cm.value(), std::vector<int64_t>({1, 0, 2, 3})); | ||
} | ||
|
||
} // namespace | ||
} // namespace xla |