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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#include <gtest/gtest.h>
#include "test/cpp/jit/test_utils.h"
#include <torch/csrc/jit/testing/file_check.h>
#include "torch/csrc/jit/passes/common_subexpression_elimination.h"
#include "torch/csrc/jit/passes/utils/subgraph_utils.h"
namespace torch {
namespace jit {
TEST(SubgraphUtilsTest, Basic) {
auto graph = build_lstm();
EliminateCommonSubexpression(graph);
std::vector<Node*> originalNodes(
graph->nodes().begin(), graph->nodes().end());
for (bool reverse_iterate : {true, false}) {
// Merge everything into a single subgraph
bool first = true;
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
Node* subgraph;
auto it =
reverse_iterate ? graph->nodes().rbegin() : graph->nodes().begin();
auto end = reverse_iterate ? graph->nodes().rend() : graph->nodes().end();
for (; it != end;) {
if (first) {
subgraph = SubgraphUtils::createSingletonSubgraph(
*it, prim::DifferentiableGraph);
it = reverse_iterate ? ++subgraph->reverseIterator()
: ++subgraph->iterator();
first = false;
}
SubgraphUtils::mergeNodeIntoSubgraph(*it, subgraph);
it = reverse_iterate ? ++subgraph->reverseIterator()
: ++subgraph->iterator();
}
// Unmerge and compare with original node listing
// NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage)
SubgraphUtils::unmergeSubgraph(subgraph);
EliminateCommonSubexpression(graph);
std::vector<Node*> newNodes(graph->nodes().begin(), graph->nodes().end());
ASSERT_EQ(originalNodes.size(), newNodes.size());
}
}
TEST(SubgraphUtilsTest, MergeSubgraphs) {
auto graph = std::make_shared<Graph>();
std::unordered_map<std::string, Value*> parse_map;
parseIR(
R"IR(
graph(%a : Tensor, %b : Tensor, %c : Tensor):
%x : Tensor = aten::sigmoid(%a)
%y : Tensor = aten::mul(%a, %b)
%p : Tensor = aten::div(%c, %b)
%q1 : Tensor = aten::mul(%p, %a)
%q2 : Tensor = aten::tanh(%q1)
%q3 : Tensor = aten::tanh(%q2)
%q4 : Tensor = aten::tanh(%q3)
%q5 : Tensor = aten::hardsigmoid(%q4)
return (%x, %y, %q5))IR",
&*graph,
parse_map);
std::vector<Node*> originalNodes(
graph->nodes().begin(), graph->nodes().end());
for (bool reverse_merge : {true, false}) {
// Merge everything into two adjacent subgraphs
Node* graph1 = SubgraphUtils::createSingletonSubgraph(
*graph->nodes().begin(), prim::DifferentiableGraph);
while (true) {
Node* next = graph1->next();
if (next->kind() == aten::tanh) {
break;
}
SubgraphUtils::mergeNodeIntoSubgraph(next, graph1);
}
Node* graph2 = SubgraphUtils::createSingletonSubgraph(
graph1->next(), prim::DifferentiableGraph);
while (graph2->next() != *graph->nodes().end()) {
SubgraphUtils::mergeNodeIntoSubgraph(graph2->next(), graph2);
}
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
Node* subgraph;
if (reverse_merge) {
SubgraphUtils::mergeNodeIntoSubgraph(graph2, graph1);
subgraph = graph1;
} else {
SubgraphUtils::mergeNodeIntoSubgraph(graph1, graph2);
subgraph = graph2;
}
auto run_file_check = [](std::shared_ptr<Graph> graph) {
graph->lint();
testing::FileCheck()
.check("aten::sigmoid")
->check("aten::mul")
->check("aten::div")
->check("aten::mul")
->check_count("aten::tanh", 3)
->check("aten::hardsigmoid")
->run(*graph);
};
run_file_check(subgraph->g(attr::Subgraph));
// Unmerge and compare with original node listing
SubgraphUtils::unmergeSubgraph(subgraph);
EliminateCommonSubexpression(graph);
run_file_check(graph);
std::vector<Node*> newNodes(graph->nodes().begin(), graph->nodes().end());
ASSERT_EQ(originalNodes.size(), newNodes.size());
}
}
TEST(SubgraphUtilsTest, GraphName) {
auto graph = std::make_shared<Graph>();
std::unordered_map<std::string, Value*> parse_map;
parseIR(
R"IR(
graph(%a : Tensor, %b : Tensor, %c : Tensor):
%x : Tensor = aten::tanh(%a)
%y : Tensor = aten::mul(%a, %b)
%p : Tensor = aten::div(%c, %b)
%q1 : Tensor = aten::mul(%p, %a)
%q2 : Tensor = aten::tanh(%q1)
%q3 : Tensor = aten::tanh(%q2)
%q4 : Tensor = aten::tanh(%q3)
%q5 : Tensor = aten::tanh(%q4)
return (%x, %y, %q5))IR",
&*graph,
parse_map);
std::string ref_full_name = "graph_tanh_mul_div_mul_tanh_tanh_tanh_tanh";
std::string full_name =
SubgraphUtils::generateNameForGraph(graph, 80, "graph");
ASSERT_EQ(full_name, ref_full_name);
std::string truncated_name =
SubgraphUtils::generateNameForGraph(graph, 10, "graph");
ASSERT_LE(truncated_name.size(), ref_full_name.size());
}
} // namespace jit
} // namespace torch
|