Skip to content

Commit df87e63

Browse files
authored
add dfg graphviz pass (#11211)
1 parent b74362f commit df87e63

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

paddle/fluid/inference/analysis/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ cc_test(test_subgraph_splitter
1515
DEPS analysis paddle_fluid tensor
1616
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model)
1717
set_tests_properties(test_subgraph_splitter PROPERTIES DEPENDS test_word2vec)
18+
19+
cc_test(test_dfg_graphviz_draw_pass
20+
SRCS dfg_graphviz_draw_pass_tester.cc
21+
DEPS analysis
22+
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model)
23+
set_tests_properties(test_dfg_graphviz_draw_pass PROPERTIES DEPENDS test_word2vec)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
/*
16+
* This file create an DFG_GraphvizDrawPass which helps to draw a data flow
17+
* graph's structure using graphviz.
18+
*/
19+
20+
#pragma once
21+
22+
#include <fstream>
23+
#include <string>
24+
#include "paddle/fluid/inference/analysis/pass.h"
25+
26+
namespace paddle {
27+
namespace inference {
28+
namespace analysis {
29+
30+
/*
31+
* Output a dot file and write to some place.
32+
*/
33+
class DFG_GraphvizDrawPass : public DataFlowGraphPass {
34+
public:
35+
DFG_GraphvizDrawPass(const std::string& dir, const std::string& id)
36+
: dir_(dir), id_(id) {}
37+
38+
bool Initialize() override { return Pass::Initialize(); }
39+
void Run(DataFlowGraph* graph) override {
40+
auto content = Draw(graph);
41+
std::ofstream file(GenDotPath());
42+
file.write(content.c_str(), content.size());
43+
file.close();
44+
LOG(INFO) << "draw dot to " << GenDotPath();
45+
}
46+
47+
bool Finalize() override { return Pass::Finalize(); }
48+
49+
Pass* CreatePrinterPass(std::ostream& os,
50+
const std::string& banner) const override {
51+
return nullptr;
52+
}
53+
54+
private:
55+
// Path of the dot file to output.
56+
std::string GenDotPath() const {
57+
return dir_ + "/" + "graph_" + id_ + ".dot";
58+
}
59+
60+
std::string Draw(DataFlowGraph* graph) { return graph->DotString(); }
61+
62+
std::string dir_;
63+
std::string id_;
64+
};
65+
66+
} // namespace analysis
67+
} // namespace inference
68+
} // namespace paddle
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/fluid/inference/analysis/dfg_graphviz_draw_pass.h"
16+
17+
#include <gtest/gtest.h>
18+
#include <fstream>
19+
#include <string>
20+
#include "paddle/fluid/inference/analysis/ut_helper.h"
21+
22+
namespace paddle {
23+
namespace inference {
24+
namespace analysis {
25+
26+
TEST_F(DFG_Tester, dfg_graphviz_draw_pass_tester) {
27+
auto dfg = ProgramDescToDFG(desc);
28+
DFG_GraphvizDrawPass pass("./", "test");
29+
pass.Initialize();
30+
pass.Run(&dfg);
31+
32+
// test content
33+
std::ifstream file("./graph_test.dot");
34+
ASSERT_TRUE(file.is_open());
35+
36+
std::string line;
37+
int no{0};
38+
while (std::getline(file, line)) {
39+
no++;
40+
}
41+
ASSERT_EQ(no, 82);
42+
}
43+
44+
} // namespace analysis
45+
} // namespace inference
46+
} // namespace paddle

0 commit comments

Comments
 (0)