File: common_subexpression_elimination_test.cc

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 (118 lines) | stat: -rw-r--r-- 4,315 bytes parent folder | download
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <gtest/gtest.h>
#include "caffe2/core/net.h"
#include "caffe2/core/operator.h"
#include "caffe2/transforms/common_subexpression_elimination.h"

namespace caffe2 {

namespace {

using transform::Graph;

/**
 *            /--->(FC)-->(Relu)
 *  Before: (FC)-->(FC)-->(Relu)
 *            \--->(FC)-->(Relu)
 *
 *                    /-->(Relu)
 *  After : (FC)-->(FC)-->(Relu)
 *                    \-->(Relu)
 *
 */
TEST(CommonSubexpressionEliminationTest, TestSimple) {
  NetDef netdef;
  // NOLINTNEXTLINE(cppcoreguidelines-init-variables)
  OperatorDef* op;

  // This operator simply reads input and outputs it.
  // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
  op = AddOp(&netdef, "FC", {"in", "w", "b"}, {"in1"});
  // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
  op = AddOp(&netdef, "FC", {"in1", "w", "b"}, {"mid1"});
  // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
  op = AddOp(&netdef, "FC", {"in1", "w", "b"}, {"mid2"});
  // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
  op = AddOp(&netdef, "FC", {"in1", "w", "b"}, {"mid3"});
  // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
  op = AddOp(&netdef, "Relu", {"mid1"}, {"out1"});
  // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
  op = AddOp(&netdef, "Relu", {"mid2"}, {"out2"});
  // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
  op = AddOp(&netdef, "Relu", {"mid3"}, {"out3"});

  auto t = TransformRegistry()->Create("CommonSubexpressionElimination");
  CHECK(t);
  NetDef transformed_netdef = t->ApplyTo(netdef);

  EXPECT_EQ(t->PatternMatch(Graph(netdef)).size(), 1); // one match
  EXPECT_EQ(t->PatternMatch(Graph(netdef)).at(0).size(), 3); // 3 ops matched
  EXPECT_EQ(transformed_netdef.op_size(), 5);
  EXPECT_EQ(transformed_netdef.op(1).output_size(), 1);
  EXPECT_EQ(transformed_netdef.op(2).input_size(), 1);
  EXPECT_EQ(transformed_netdef.op(3).input_size(), 1);
  EXPECT_EQ(transformed_netdef.op(4).input_size(), 1);

  // make sure op 1 writes to the blob read by 2, 3, and 4.
  EXPECT_EQ(
      transformed_netdef.op(1).output(0), transformed_netdef.op(2).input(0));
  EXPECT_EQ(
      transformed_netdef.op(1).output(0), transformed_netdef.op(3).input(0));
  EXPECT_EQ(
      transformed_netdef.op(1).output(0), transformed_netdef.op(4).input(0));
}

/**
 * Almost the same as the one above, but it has to be able to merge from
 * external input as well.
 *
 *            ->(FC)-->(Relu)
 *  Before:   ->(FC)-->(Relu)
 *            ->(FC)-->(Relu)
 *
 *                 /-->(Relu)
 *  After :   ->(FC)-->(Relu)
 *                 \-->(Relu)
 *
 */
TEST(CommonSubexpressionEliminationTest, TestFromExternal) {
  NetDef netdef;
  // NOLINTNEXTLINE(cppcoreguidelines-init-variables)
  OperatorDef* op;

  // This operator simply reads input and outputs it.
  // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
  op = AddOp(&netdef, "FC", {"in", "w", "b"}, {"mid1"});
  // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
  op = AddOp(&netdef, "FC", {"in", "w", "b"}, {"mid2"});
  // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
  op = AddOp(&netdef, "FC", {"in", "w", "b"}, {"mid3"});
  // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
  op = AddOp(&netdef, "Relu", {"mid1"}, {"out1"});
  // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
  op = AddOp(&netdef, "Relu", {"mid2"}, {"out2"});
  // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
  op = AddOp(&netdef, "Relu", {"mid3"}, {"out3"});

  auto t = TransformRegistry()->Create("CommonSubexpressionElimination");
  CHECK(t);
  NetDef transformed_netdef = t->ApplyTo(netdef);

  EXPECT_EQ(t->PatternMatch(Graph(netdef)).size(), 1); // one match
  EXPECT_EQ(t->PatternMatch(Graph(netdef)).at(0).size(), 3); // 3 ops matched
  EXPECT_EQ(transformed_netdef.op_size(), 4);
  EXPECT_EQ(transformed_netdef.op(0).output_size(), 1);
  EXPECT_EQ(transformed_netdef.op(1).input_size(), 1);
  EXPECT_EQ(transformed_netdef.op(2).input_size(), 1);
  EXPECT_EQ(transformed_netdef.op(3).input_size(), 1);

  EXPECT_EQ(
      transformed_netdef.op(0).output(0), transformed_netdef.op(1).input(0));
  EXPECT_EQ(
      transformed_netdef.op(0).output(0), transformed_netdef.op(2).input(0));
  EXPECT_EQ(
      transformed_netdef.op(0).output(0), transformed_netdef.op(3).input(0));
}

} // namespace

} // namespace caffe2