pytorch/test/cpp/lazy/test_permutation_util.cpp
Bin Bao e8f4c9cc40 [LT] Upstream LazyView and view ops IR Nodes (#69277)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/69277

LazyView is the main class for tracking alias caused by view
ops. The corresponding IR classes for view ops are hand-written now, and
we can switch to code-gen them in future. For certain view ops, they
have a reverse IR class to perform inplace update in the backward
direction on a chain of alias ops.

As part of the future work, we will simplify the logic for LazyView once
the functionalization pass in core is ready to use.

Test Plan: Imported from OSS

Reviewed By: wconstab

Differential Revision: D32820014

Pulled By: desertfire

fbshipit-source-id: d9eb526cb23885f667e4815dc9dd291a7b7e4256
2021-12-04 08:44:54 -08:00

48 lines
1.6 KiB
C++

#include <gtest/gtest.h>
#include <c10/util/Exception.h>
#include <torch/csrc/lazy/core/permutation_util.h>
namespace torch {
namespace lazy {
TEST(PermutationUtilTest, TestInversePermutation) {
EXPECT_EQ(InversePermutation({0}), std::vector<int64_t>({0}));
EXPECT_EQ(InversePermutation({0, 1, 2}), std::vector<int64_t>({0, 1, 2}));
EXPECT_EQ(
InversePermutation({1, 3, 2, 0}), std::vector<int64_t>({3, 0, 2, 1}));
// Not a valid permutation
EXPECT_THROW(InversePermutation({-1}), c10::Error);
EXPECT_THROW(InversePermutation({1, 1}), c10::Error);
}
TEST(PermutationUtilTest, TestIsPermutation) {
EXPECT_TRUE(IsPermutation({0}));
EXPECT_TRUE(IsPermutation({0, 1, 2, 3}));
EXPECT_FALSE(IsPermutation({-1}));
EXPECT_FALSE(IsPermutation({5, 3}));
EXPECT_FALSE(IsPermutation({1, 2, 3}));
}
TEST(PermutationUtilTest, TestPermute) {
EXPECT_EQ(
PermuteDimensions({0}, std::vector<int64_t>({224})),
std::vector<int64_t>({224}));
EXPECT_EQ(
PermuteDimensions({1, 2, 0}, std::vector<int64_t>({3, 224, 224})),
std::vector<int64_t>({224, 224, 3}));
// Not a valid permutation
EXPECT_THROW(
PermuteDimensions({-1}, std::vector<int64_t>({244})), c10::Error);
EXPECT_THROW(
PermuteDimensions({3, 2}, std::vector<int64_t>({244})), c10::Error);
// Permutation size is different from the to-be-permuted vector size
EXPECT_THROW(
PermuteDimensions({0, 1}, std::vector<int64_t>({244})), c10::Error);
EXPECT_THROW(
PermuteDimensions({0}, std::vector<int64_t>({3, 244, 244})), c10::Error);
}
} // namespace lazy
} // namespace torch