Skip to content

[ET][Portable] Fix op permute_copy #726

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

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
27 changes: 16 additions & 11 deletions kernels/portable/cpu/util/copy_ops_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,25 @@ bool check_permute_copy_args(const Tensor& in, IntArrayRef dims, Tensor& out) {
ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(in, out));

// Make sure no dimensions are duplicated and all in the range [-in.dim(),
// in.dim() - 1]. Use gaussian sum to check this.
size_t expected_sum = (dims.size() * (dims.size() + 1)) / 2;
size_t gauss_sum = 0;
// in.dim() - 1].
bool dim_exist[kTensorDimensionLimit];
memset(dim_exist, false, sizeof(dim_exist));

for (int i = 0; i < dims.size(); i++) {
// Convert dimension to a non-negative number. dim_base is in the range
ET_LOG_AND_RETURN_IF_FALSE(tensor_has_dim(in, dims[i]));
// Convert dimension to a non-negative number in the range
// [0 .. in.dim() - 1].
size_t dim = dims[i] > -1 ? dims[i] : in.dim() + dims[i];
ET_LOG_AND_RETURN_IF_FALSE(tensor_has_dim(in, dim));
gauss_sum += dim + 1;
}
size_t dim = dims[i] >= 0 ? dims[i] : in.dim() + dims[i];

ET_LOG_MSG_AND_RETURN_IF_FALSE(
gauss_sum == expected_sum,
"The dims passed to permute_copy must contain one of each dim!");
// Internal check, since we have already validated this
ET_CHECK(dim < kTensorDimensionLimit && dim >= 0);

// Check that the dimension hasn't been seen previously.
ET_LOG_MSG_AND_RETURN_IF_FALSE(
dim_exist[dim] == false, "duplicate dims are not allowed.");

dim_exist[dim] = true;
}

return true;
}
Expand Down
28 changes: 28 additions & 0 deletions kernels/test/op_permute_copy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,20 @@ TEST(OpPermuteCopyKernelTest, DupeDimensionPos) {
t_int, ArrayRef<int64_t>(new_dim.data(), new_dim.size()), out));
}

TEST(OpPermuteCopyKernelTest, DupeDimensionPos2) {
TensorFactory<ScalarType::Int> tf;

const std::vector<int64_t> new_dim = {1, 1, 1};

const std::vector<int32_t> sizes = {1, 1, 1};
Tensor t_int = tf.make(sizes, {1});

Tensor out = tf.zeros(sizes);

ET_EXPECT_KERNEL_FAILURE(op_permute_copy_out(
t_int, ArrayRef<int64_t>(new_dim.data(), new_dim.size()), out));
}

TEST(OpPermuteCopyKernelTest, DupeDimensionNeg) {
TensorFactory<ScalarType::Int> tf;

Expand All @@ -341,6 +355,20 @@ TEST(OpPermuteCopyKernelTest, DupeDimensionNeg) {
t_int, ArrayRef<int64_t>(new_dim.data(), new_dim.size()), out));
}

TEST(OpPermuteCopyKernelTest, DupeDimensionNeg2) {
TensorFactory<ScalarType::Int> tf;

const std::vector<int64_t> new_dim = {0, 1, -5};

const std::vector<int32_t> sizes = {1, 1, 1};
Tensor t_int = tf.make(sizes, {1});

Tensor out = tf.zeros(sizes);

ET_EXPECT_KERNEL_FAILURE(op_permute_copy_out(
t_int, ArrayRef<int64_t>(new_dim.data(), new_dim.size()), out));
}

TEST(OpPermuteCopyKernelTest, MismatchDim) {
TensorFactory<ScalarType::Int> tf;

Expand Down