File: common_subexpression_elimination_test.cc

package info (click to toggle)
pytorch 1.7.1-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 80,340 kB
  • sloc: cpp: 670,830; python: 343,991; ansic: 67,845; asm: 5,503; sh: 2,924; java: 2,888; xml: 266; makefile: 244; ruby: 148; yacc: 144; objc: 51; lex: 44
file content (103 lines) | stat: -rw-r--r-- 3,479 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
#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;
  OperatorDef* op;

  // This operator simply reads input and outputs it.
  op = AddOp(&netdef, "FC", {"in", "w", "b"}, {"in1"});
  op = AddOp(&netdef, "FC", {"in1", "w", "b"}, {"mid1"});
  op = AddOp(&netdef, "FC", {"in1", "w", "b"}, {"mid2"});
  op = AddOp(&netdef, "FC", {"in1", "w", "b"}, {"mid3"});
  op = AddOp(&netdef, "Relu", {"mid1"}, {"out1"});
  op = AddOp(&netdef, "Relu", {"mid2"}, {"out2"});
  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;
  OperatorDef* op;

  // This operator simply reads input and outputs it.
  op = AddOp(&netdef, "FC", {"in", "w", "b"}, {"mid1"});
  op = AddOp(&netdef, "FC", {"in", "w", "b"}, {"mid2"});
  op = AddOp(&netdef, "FC", {"in", "w", "b"}, {"mid3"});
  op = AddOp(&netdef, "Relu", {"mid1"}, {"out1"});
  op = AddOp(&netdef, "Relu", {"mid2"}, {"out2"});
  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