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

fix squeeze op lowering issue when dim is not in sorted order #5751

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions test/cpp/test_aten_xla_tensor_4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,18 @@ TEST_F(AtenXlaTensorTest, TestSqueezeMultipleDims) {
});
}

TEST_F(AtenXlaTensorTest, TestSqueezeDimWithNegativeOne) {
torch::Tensor input =
torch::rand({2, 1, 3, 1}, torch::TensorOptions(torch::kFloat));
std::vector<int64_t> dims = {-1};
torch::Tensor output = torch::squeeze(input, dims);
ForEachDevice([&](const torch::Device& device) {
torch::Tensor xla_input = CopyToDevice(input, device);
torch::Tensor xla_output = torch::squeeze(xla_input, dims);
AllClose(output, xla_output);
});
}

TEST_F(AtenXlaTensorTest, TestSqueezeOneInPlace) {
int rank = 4;
for (int dim = -rank; dim < rank; ++dim) {
Expand Down
20 changes: 20 additions & 0 deletions torch_xla/csrc/data_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,26 @@ std::vector<int64_t> BuildSqueezedDimensions(
return output_dimensions;
}

std::vector<int64_t> BuildSqueezedDimensions(
absl::Span<const int64_t> dimensions, std::vector<int64_t>& squeeze_dims) {
std::sort(squeeze_dims.begin(), squeeze_dims.end());
std::vector<int64_t> output_dimensions;
size_t i = 0;
for (size_t j = 0; j < dimensions.size(); j++) {
auto dim = dimensions[j];
if (i == squeeze_dims.size() || j < squeeze_dims[i]) {
output_dimensions.push_back(dim);
continue;
}
// Checks to see if we need to squeeze the dim or not.
if (dim != 1) {
output_dimensions.push_back(dim);
}
i++;
}
return output_dimensions;
}

std::vector<int64_t> BuildUnsqueezeDimensions(
absl::Span<const int64_t> dimensions, int64_t dim) {
XLA_CHECK_LE(dim, dimensions.size());
Expand Down
3 changes: 3 additions & 0 deletions torch_xla/csrc/data_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ xla::XlaOp BuildMaskedFillScalar(xla::XlaOp input, xla::XlaOp mask,
std::vector<int64_t> BuildSqueezedDimensions(
Copy link
Collaborator

Choose a reason for hiding this comment

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

The above one should be a special case of the 2nd one, shall we just keep the second one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the point, I leave the original signature there in case program still relays on the 1st one. However, I just made the update in 1st one function to call 2nd function to simplify the coding.

absl::Span<const int64_t> dimensions, int64_t squeeze_dim);

std::vector<int64_t> BuildSqueezedDimensions(
absl::Span<const int64_t> dimensions, std::vector<int64_t>& squeeze_dim);

std::vector<int64_t> BuildUnsqueezeDimensions(
absl::Span<const int64_t> dimensions, int64_t dim);

Expand Down
13 changes: 7 additions & 6 deletions torch_xla/csrc/tensor_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2471,16 +2471,17 @@ XLATensorPtr squeeze(const XLATensorPtr& input, std::vector<int64_t> dims) {
std::vector<int64_t> input_dimensions =
torch_xla::runtime::util::ToVector<int64_t>(
input_shape.get().dimensions());
std::vector<int64_t> output_dimensions;
std::vector<int64_t> squeeze_dims;
for (int64_t dim : dims) {
if (dim >= input_dimensions.size()) {
continue;
}
int64_t squeeze_dim =
torch::lazy::GetCanonicalDimensionIndex(dim, input_dimensions.size());
output_dimensions = BuildSqueezedDimensions(input_dimensions, squeeze_dim);
input_dimensions = output_dimensions;
if (squeeze_dim >= input_dimensions.size()) {
continue;
}
squeeze_dims.push_back(squeeze_dim);
}
std::vector<int64_t> output_dimensions =
BuildSqueezedDimensions(input_dimensions, squeeze_dims);
return view(input, output_dimensions);
}

Expand Down