Skip to content

Commit

Permalink
[XLA:Python] Modify DLPack behavior with unit dimensions.
Browse files Browse the repository at this point in the history
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
dfm authored and Google-ML-Automation committed Nov 14, 2024
1 parent 73ddc48 commit 78a7de8
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 31 deletions.
27 changes: 27 additions & 0 deletions xla/python/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,32 @@ cc_library(
),
)

cc_library(
name = "dlpack_strides",
srcs = ["dlpack_strides.cc"],
hdrs = ["dlpack_strides.h"],
deps = [
"//xla:util",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
"@tsl//tsl/platform:logging",
],
)

xla_cc_test(
name = "dlpack_strides_test",
srcs = ["dlpack_strides_test.cc"],
deps = [
":dlpack_strides",
"@com_google_absl//absl/types:span",
"@tsl//tsl/platform:test",
"@tsl//tsl/platform:test_main",
],
)

cc_library(
name = "dlpack",
srcs = ["dlpack.cc"],
Expand All @@ -524,6 +550,7 @@ cc_library(
],
features = ["-use_header_modules"],
deps = [
":dlpack_strides",
":nb_class_ptr",
":py_client",
":python_ref_manager",
Expand Down
32 changes: 1 addition & 31 deletions xla/python/dlpack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ limitations under the License.
#include "xla/pjrt/pjrt_common.h"
#include "xla/pjrt/pjrt_compiler.h"
#include "xla/pjrt/pjrt_layout.h"
#include "xla/python/dlpack_strides.h"
#include "xla/python/ifrt/array.h"
#include "xla/python/ifrt/device.h"
#include "xla/python/nb_class_ptr.h"
Expand Down Expand Up @@ -212,37 +213,6 @@ absl::StatusOr<PrimitiveType> DLDataTypeToPrimitiveType(DLDataType type) {
}
}

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());
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;
}

absl::StatusOr<DLDeviceType> DLDeviceTypeForDevice(const PjRtDevice& device) {
if (device.client()->platform_id() == CpuId()) {
return kDLCPU;
Expand Down
78 changes: 78 additions & 0 deletions xla/python/dlpack_strides.cc
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
32 changes: 32 additions & 0 deletions xla/python/dlpack_strides.h
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_
73 changes: 73 additions & 0 deletions xla/python/dlpack_strides_test.cc
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

0 comments on commit 78a7de8

Please sign in to comment.