File: test_permutation_util.cpp

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (47 lines) | stat: -rw-r--r-- 1,609 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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