|
|
@ -21,112 +21,129 @@ namespace paddle {
|
|
|
|
namespace framework {
|
|
|
|
namespace framework {
|
|
|
|
namespace ir {
|
|
|
|
namespace ir {
|
|
|
|
|
|
|
|
|
|
|
|
void SetOp(ProgramDesc* prog, const std::string& type, const std::string& name,
|
|
|
|
class PlacementPassTest {
|
|
|
|
const std::vector<std::string>& inputs,
|
|
|
|
private:
|
|
|
|
const std::vector<std::string>& outputs, boost::tribool use_mkldnn) {
|
|
|
|
void SetOp(ProgramDesc* prog, const std::string& type,
|
|
|
|
auto* op = prog->MutableBlock(0)->AppendOp();
|
|
|
|
const std::string& name, const std::vector<std::string>& inputs,
|
|
|
|
|
|
|
|
const std::vector<std::string>& outputs,
|
|
|
|
op->SetType(type);
|
|
|
|
boost::tribool use_mkldnn) {
|
|
|
|
|
|
|
|
auto* op = prog->MutableBlock(0)->AppendOp();
|
|
|
|
if (!boost::indeterminate(use_mkldnn)) op->SetAttr("use_mkldnn", use_mkldnn);
|
|
|
|
|
|
|
|
|
|
|
|
op->SetType(type);
|
|
|
|
if (type == "conv2d") {
|
|
|
|
|
|
|
|
op->SetAttr("name", name);
|
|
|
|
if (!boost::indeterminate(use_mkldnn))
|
|
|
|
op->SetInput("Input", {inputs[0]});
|
|
|
|
op->SetAttr("use_mkldnn", use_mkldnn);
|
|
|
|
op->SetInput("Filter", {inputs[1]});
|
|
|
|
|
|
|
|
op->SetInput("Bias", {inputs[2]});
|
|
|
|
if (type == "conv2d") {
|
|
|
|
} else if (type == "relu") {
|
|
|
|
op->SetAttr("name", name);
|
|
|
|
op->SetInput("X", inputs);
|
|
|
|
op->SetInput("Input", {inputs[0]});
|
|
|
|
} else if (type == "concat") {
|
|
|
|
op->SetInput("Filter", {inputs[1]});
|
|
|
|
op->SetAttr("axis", 1);
|
|
|
|
op->SetInput("Bias", {inputs[2]});
|
|
|
|
op->SetInput("X", {inputs[0], inputs[1]});
|
|
|
|
} else if (type == "relu") {
|
|
|
|
} else if (type == "pool2d") {
|
|
|
|
op->SetInput("X", inputs);
|
|
|
|
op->SetInput("X", {inputs[0]});
|
|
|
|
} else if (type == "concat") {
|
|
|
|
} else {
|
|
|
|
op->SetAttr("axis", 1);
|
|
|
|
FAIL() << "Unexpected operator type.";
|
|
|
|
op->SetInput("X", {inputs[0], inputs[1]});
|
|
|
|
|
|
|
|
} else if (type == "pool2d") {
|
|
|
|
|
|
|
|
op->SetInput("X", {inputs[0]});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
FAIL() << "Unexpected operator type.";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
op->SetOutput("Out", {outputs[0]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
op->SetOutput("Out", {outputs[0]});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// operator use_mkldnn
|
|
|
|
// operator use_mkldnn
|
|
|
|
// ---------------------------------------
|
|
|
|
// ---------------------------------------
|
|
|
|
// (a,b)->concat->c none
|
|
|
|
// (a,b)->concat->c none
|
|
|
|
// (c,weights,bias)->conv->f none
|
|
|
|
// (c,weights,bias)->conv->f none
|
|
|
|
// f->relu->g false
|
|
|
|
// f->relu->g false
|
|
|
|
// g->pool->h false
|
|
|
|
// g->pool->h false
|
|
|
|
// (h,weights2,bias2)->conv->k true
|
|
|
|
// (h,weights2,bias2)->conv->k true
|
|
|
|
// k->relu->l true
|
|
|
|
// k->relu->l true
|
|
|
|
ProgramDesc BuildProgramDesc() {
|
|
|
|
ProgramDesc BuildProgramDesc() {
|
|
|
|
ProgramDesc prog;
|
|
|
|
ProgramDesc prog;
|
|
|
|
|
|
|
|
|
|
|
|
for (auto& v :
|
|
|
|
for (auto& v :
|
|
|
|
std::vector<std::string>({"a", "b", "c", "weights", "bias", "f", "g",
|
|
|
|
std::vector<std::string>({"a", "b", "c", "weights", "bias", "f", "g",
|
|
|
|
"h", "weights2", "bias2", "k", "l"})) {
|
|
|
|
"h", "weights2", "bias2", "k", "l"})) {
|
|
|
|
auto* var = prog.MutableBlock(0)->Var(v);
|
|
|
|
auto* var = prog.MutableBlock(0)->Var(v);
|
|
|
|
var->SetType(proto::VarType::SELECTED_ROWS);
|
|
|
|
var->SetType(proto::VarType::SELECTED_ROWS);
|
|
|
|
if (v == "weights" || v == "bias") {
|
|
|
|
if (v == "weights" || v == "bias") {
|
|
|
|
var->SetPersistable(true);
|
|
|
|
var->SetPersistable(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SetOp(&prog, "concat", "concat1", std::vector<std::string>({"a", "b"}),
|
|
|
|
|
|
|
|
std::vector<std::string>({"c"}), boost::indeterminate);
|
|
|
|
|
|
|
|
SetOp(&prog, "conv2d", "conv1",
|
|
|
|
|
|
|
|
std::vector<std::string>({"c", "weights", "bias"}),
|
|
|
|
|
|
|
|
std::vector<std::string>({"f"}), boost::indeterminate);
|
|
|
|
|
|
|
|
SetOp(&prog, "relu", "relu1", std::vector<std::string>({"f"}),
|
|
|
|
|
|
|
|
std::vector<std::string>({"g"}), false);
|
|
|
|
|
|
|
|
SetOp(&prog, "pool2d", "pool1", std::vector<std::string>({"g"}),
|
|
|
|
|
|
|
|
std::vector<std::string>({"h"}), false);
|
|
|
|
|
|
|
|
SetOp(&prog, "conv2d", "conv2",
|
|
|
|
|
|
|
|
std::vector<std::string>({"h", "weights2", "bias2"}),
|
|
|
|
|
|
|
|
std::vector<std::string>({"k"}), true);
|
|
|
|
|
|
|
|
SetOp(&prog, "relu", "relu2", std::vector<std::string>({"k"}),
|
|
|
|
|
|
|
|
std::vector<std::string>({"l"}), true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return prog;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SetOp(&prog, "concat", "concat1", std::vector<std::string>({"a", "b"}),
|
|
|
|
public:
|
|
|
|
std::vector<std::string>({"c"}), boost::indeterminate);
|
|
|
|
void MainTest(std::initializer_list<std::string> mkldnn_enabled_op_types,
|
|
|
|
SetOp(&prog, "conv2d", "conv1",
|
|
|
|
unsigned expected_use_mkldnn_true_count) {
|
|
|
|
std::vector<std::string>({"c", "weights", "bias"}),
|
|
|
|
auto prog = BuildProgramDesc();
|
|
|
|
std::vector<std::string>({"f"}), boost::indeterminate);
|
|
|
|
|
|
|
|
SetOp(&prog, "relu", "relu1", std::vector<std::string>({"f"}),
|
|
|
|
|
|
|
|
std::vector<std::string>({"g"}), false);
|
|
|
|
|
|
|
|
SetOp(&prog, "pool2d", "pool1", std::vector<std::string>({"g"}),
|
|
|
|
|
|
|
|
std::vector<std::string>({"h"}), false);
|
|
|
|
|
|
|
|
SetOp(&prog, "conv2d", "conv2",
|
|
|
|
|
|
|
|
std::vector<std::string>({"h", "weights2", "bias2"}),
|
|
|
|
|
|
|
|
std::vector<std::string>({"k"}), true);
|
|
|
|
|
|
|
|
SetOp(&prog, "relu", "relu2", std::vector<std::string>({"k"}),
|
|
|
|
|
|
|
|
std::vector<std::string>({"l"}), true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return prog;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MainTest(std::initializer_list<std::string> mkldnn_enabled_op_types,
|
|
|
|
std::unique_ptr<ir::Graph> graph(new ir::Graph(prog));
|
|
|
|
unsigned expected_use_mkldnn_true_count) {
|
|
|
|
|
|
|
|
auto prog = BuildProgramDesc();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<ir::Graph> graph(new ir::Graph(prog));
|
|
|
|
auto pass = PassRegistry::Instance().Get("mkldnn_placement_pass");
|
|
|
|
|
|
|
|
|
|
|
|
auto pass = PassRegistry::Instance().Get("mkldnn_placement_pass");
|
|
|
|
pass->Set("mkldnn_enabled_op_types",
|
|
|
|
pass->Set("mkldnn_enabled_op_types",
|
|
|
|
new std::unordered_set<std::string>(mkldnn_enabled_op_types));
|
|
|
|
new std::unordered_set<std::string>(mkldnn_enabled_op_types));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
graph.reset(pass->Apply(graph.release()));
|
|
|
|
graph.reset(pass->Apply(graph.release()));
|
|
|
|
|
|
|
|
|
|
|
|
unsigned use_mkldnn_true_count = 0;
|
|
|
|
unsigned use_mkldnn_true_count = 0;
|
|
|
|
|
|
|
|
|
|
|
|
for (auto* node : graph->Nodes()) {
|
|
|
|
for (auto* node : graph->Nodes()) {
|
|
|
|
if (node->IsOp()) {
|
|
|
|
if (node->IsOp()) {
|
|
|
|
auto* op = node->Op();
|
|
|
|
auto* op = node->Op();
|
|
|
|
if (op->HasAttr("use_mkldnn") &&
|
|
|
|
if (op->HasAttr("use_mkldnn") &&
|
|
|
|
boost::get<bool>(op->GetAttr("use_mkldnn"))) {
|
|
|
|
boost::get<bool>(op->GetAttr("use_mkldnn"))) {
|
|
|
|
++use_mkldnn_true_count;
|
|
|
|
++use_mkldnn_true_count;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(use_mkldnn_true_count, expected_use_mkldnn_true_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(use_mkldnn_true_count, expected_use_mkldnn_true_count);
|
|
|
|
void PlacementNameTest() {
|
|
|
|
}
|
|
|
|
auto pass = PassRegistry::Instance().Get("mkldnn_placement_pass");
|
|
|
|
|
|
|
|
EXPECT_EQ(static_cast<PlacementPassBase*>(pass.get())->GetPlacementName(),
|
|
|
|
|
|
|
|
"MKLDNN");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(MKLDNNPlacementPass, enable_conv_relu) {
|
|
|
|
TEST(MKLDNNPlacementPass, enable_conv_relu) {
|
|
|
|
// 1 conv (1 conv is always true) + 2 relu (1 relu is always true) + 0 pool
|
|
|
|
// 1 conv (1 conv is always true) + 2 relu (1 relu is always true) + 0 pool
|
|
|
|
MainTest({"conv2d", "relu"}, 3);
|
|
|
|
PlacementPassTest().MainTest({"conv2d", "relu"}, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(MKLDNNPlacementPass, enable_relu_pool) {
|
|
|
|
TEST(MKLDNNPlacementPass, enable_relu_pool) {
|
|
|
|
// 1 conv (1 conv is always true) + 2 relu (1 relu is always true) + 1 pool
|
|
|
|
// 1 conv (1 conv is always true) + 2 relu (1 relu is always true) + 1 pool
|
|
|
|
MainTest({"relu", "pool2d"}, 4);
|
|
|
|
PlacementPassTest().MainTest({"relu", "pool2d"}, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(MKLDNNPlacementPass, enable_all) {
|
|
|
|
TEST(MKLDNNPlacementPass, enable_all) {
|
|
|
|
// 1 conv (1 conv is always true) + 2 relu (1 relu is always true) + 1 pool
|
|
|
|
// 1 conv (1 conv is always true) + 2 relu (1 relu is always true) + 1 pool
|
|
|
|
MainTest({}, 4);
|
|
|
|
PlacementPassTest().MainTest({}, 4);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEST(MKLDNNPlacementPass, placement_name) {
|
|
|
|
|
|
|
|
PlacementPassTest().PlacementNameTest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace ir
|
|
|
|
} // namespace ir
|
|
|
|