Feature/pass manager (#11440)
parent
16a0f746f8
commit
d734595978
@ -1,23 +1,32 @@
|
||||
set(FLUID_CORE_MODULES proto_desc memory lod_tensor executor init)
|
||||
cc_library(analysis SRCS dot.cc node.cc data_flow_graph.cc graph_traits.cc subgraph_splitter.cc fluid_to_data_flow_graph_pass.cc
|
||||
DEPS paddle_fluid)
|
||||
cc_library(analysis SRCS pass_manager.cc dot.cc node.cc data_flow_graph.cc graph_traits.cc subgraph_splitter.cc
|
||||
fluid_to_data_flow_graph_pass.cc
|
||||
data_flow_graph_to_fluid_pass.cc
|
||||
tensorrt_subgraph_pass.cc
|
||||
dfg_graphviz_draw_pass.cc
|
||||
DEPS framework_proto)
|
||||
cc_test(test_node SRCS node_tester.cc DEPS analysis)
|
||||
cc_test(test_dot SRCS dot_tester.cc DEPS analysis)
|
||||
|
||||
set(PYTHON_TESTS_DIR ${PADDLE_BINARY_DIR}/python/paddle/fluid/tests)
|
||||
|
||||
cc_test(test_data_flow_graph SRCS data_flow_graph_tester.cc DEPS analysis ${FLUID_CORE_MODULES} paddle_fluid
|
||||
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model)
|
||||
set_tests_properties(test_data_flow_graph PROPERTIES DEPENDS test_word2vec)
|
||||
function (inference_analysis_test TARGET)
|
||||
set(options "")
|
||||
set(oneValueArgs "")
|
||||
set(multiValueArgs SRCS)
|
||||
cmake_parse_arguments(analysis_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
||||
|
||||
cc_test(test_subgraph_splitter
|
||||
SRCS subgraph_splitter_tester.cc
|
||||
DEPS analysis paddle_fluid tensor
|
||||
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model)
|
||||
set_tests_properties(test_subgraph_splitter PROPERTIES DEPENDS test_word2vec)
|
||||
cc_test(${TARGET}
|
||||
SRCS "${analysis_test_SRCS}"
|
||||
DEPS analysis
|
||||
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model --fraction_of_gpu_memory_to_use=0.5)
|
||||
set_tests_properties(${TARGET} PROPERTIES DEPENDS test_word2vec)
|
||||
endfunction(inference_analysis_test)
|
||||
|
||||
cc_test(test_dfg_graphviz_draw_pass
|
||||
SRCS dfg_graphviz_draw_pass_tester.cc
|
||||
DEPS analysis
|
||||
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model)
|
||||
set_tests_properties(test_dfg_graphviz_draw_pass PROPERTIES DEPENDS test_word2vec)
|
||||
inference_analysis_test(test_data_flow_graph SRCS data_flow_graph_tester.cc)
|
||||
inference_analysis_test(test_data_flow_graph_to_fluid_pass SRCS data_flow_graph_to_fluid_pass_tester.cc)
|
||||
inference_analysis_test(test_fluid_to_data_flow_graph_pass SRCS fluid_to_data_flow_graph_pass_tester.cc)
|
||||
inference_analysis_test(test_subgraph_splitter SRCS subgraph_splitter_tester.cc)
|
||||
inference_analysis_test(test_dfg_graphviz_draw_pass SRCS dfg_graphviz_draw_pass_tester.cc)
|
||||
#inference_analysis_test(test_tensorrt_subgraph_pass SRCS tensorrt_subgraph_pass_tester.cc)
|
||||
inference_analysis_test(test_pass_manager SRCS pass_manager_tester.cc)
|
||||
|
@ -0,0 +1,15 @@
|
||||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "paddle/fluid/inference/analysis/argument.h"
|
@ -0,0 +1,53 @@
|
||||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
/*
|
||||
* This file defines the class Argument, which is the input and output of the
|
||||
* analysis module. All the fields that needed either by Passes or PassManagers
|
||||
* are contained in Argument.
|
||||
*
|
||||
* TODO(Superjomn) Find some way better to contain the fields when it grow too
|
||||
* big.
|
||||
*/
|
||||
|
||||
#include "paddle/fluid/framework/program_desc.h"
|
||||
#include "paddle/fluid/inference/analysis/data_flow_graph.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace inference {
|
||||
namespace analysis {
|
||||
|
||||
/*
|
||||
* The argument definition of both Pass and PassManagers.
|
||||
*
|
||||
* All the fields should be registered here for clearness.
|
||||
*/
|
||||
struct Argument {
|
||||
// The graph that process by the Passes or PassManagers.
|
||||
std::unique_ptr<DataFlowGraph> main_dfg;
|
||||
|
||||
// The original program desc.
|
||||
std::unique_ptr<framework::proto::ProgramDesc> origin_program_desc;
|
||||
};
|
||||
|
||||
#define UNLIKELY(condition) __builtin_expect(static_cast<bool>(condition), 0)
|
||||
#define ANALYSIS_ARGUMENT_CHECK_FIELD(field__) \
|
||||
if (!UNLIKELY(field__)) { \
|
||||
LOG(ERROR) << "field " << #field__ << " should be set."; \
|
||||
return false; \
|
||||
}
|
||||
|
||||
} // namespace analysis
|
||||
} // namespace inference
|
||||
} // namespace paddle
|
@ -0,0 +1,77 @@
|
||||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "paddle/fluid/inference/analysis/data_flow_graph_to_fluid_pass.h"
|
||||
#include "paddle/fluid/framework/proto_desc.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace inference {
|
||||
namespace analysis {
|
||||
|
||||
bool DataFlowGraphToFluidPass::Initialize(Argument* argument) {
|
||||
ANALYSIS_ARGUMENT_CHECK_FIELD(argument)
|
||||
ANALYSIS_ARGUMENT_CHECK_FIELD(argument->origin_program_desc)
|
||||
desc_ = argument->origin_program_desc.get();
|
||||
// Here some logic from program_desc.cc and will not add new interfaces into
|
||||
// framework::ProgramDesc class, use some UT to assure the correctness.
|
||||
auto* block = desc_->mutable_blocks()->Add();
|
||||
block->set_idx(framework::kRootBlockIndex);
|
||||
block->set_parent_idx(framework::kNoneBlockIndex);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DataFlowGraphToFluidPass::Finalize() { return true; }
|
||||
|
||||
void DataFlowGraphToFluidPass::Run(DataFlowGraph* graph) {
|
||||
auto traits = GraphTraits<DataFlowGraph>(graph);
|
||||
for (auto it = traits.nodes().begin(); it != traits.nodes().end(); ++it) {
|
||||
if (it->deleted()) continue;
|
||||
switch (it->type()) {
|
||||
case Node::Type::kFunction:
|
||||
LOG(INFO) << "add function " << it->name();
|
||||
AddFluidOp(&(*it));
|
||||
break;
|
||||
case Node::Type::kFunctionBlock:
|
||||
AddEngineOp(&(*it));
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DataFlowGraphToFluidPass::AddFluidOp(Node* node) {
|
||||
LOG(INFO) << "processing func " << node->name();
|
||||
auto* ori_op = static_cast<framework::proto::OpDesc*>(node->pb_desc());
|
||||
// currently only the main block is analyzed.
|
||||
auto* main_block = desc_->mutable_blocks(framework::kRootBlockIndex);
|
||||
auto* op = main_block->add_ops();
|
||||
LOG(INFO) << "to copy the op";
|
||||
*op = *ori_op; // copy the attributes, by default, these will not be changed
|
||||
// by analysis phrase.
|
||||
// The inputs and outputs of the existing ops are not changed by tensorrt
|
||||
// subgraph pass.
|
||||
// NOTE It might be changed by other passes in the long run.
|
||||
}
|
||||
|
||||
void DataFlowGraphToFluidPass::AddEngineOp(Node* node) {
|
||||
// auto* ori_op = static_cast<framework::proto::OpDesc*>(node->extra_info());
|
||||
// auto* main_block = desc_->mutable_blocks(framework::kRootBlockIndex);
|
||||
// auto* op = main_block->add_ops();
|
||||
// TODO(Superjomn) Here need to expose some arguments for default setting.
|
||||
}
|
||||
|
||||
} // namespace analysis
|
||||
} // namespace inference
|
||||
} // namespace paddle
|
@ -0,0 +1,59 @@
|
||||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
/*
|
||||
* This file implements the transformation from fluid ProgramDesc to data flow
|
||||
* graph.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "paddle/fluid/framework/program_desc.h"
|
||||
#include "paddle/fluid/inference/analysis/data_flow_graph.h"
|
||||
#include "paddle/fluid/inference/analysis/pass.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace inference {
|
||||
namespace analysis {
|
||||
class DataFlowGraphToFluidPass final : public DataFlowGraphPass {
|
||||
public:
|
||||
DataFlowGraphToFluidPass() = default;
|
||||
|
||||
bool Initialize(Argument *argument) override;
|
||||
bool Finalize() override;
|
||||
|
||||
void Run(DataFlowGraph *graph) override;
|
||||
|
||||
std::string repr() const override { return "DFG to fluid"; }
|
||||
std::string description() const override {
|
||||
return "Transform a DFG to a Fluid ProgramDesc";
|
||||
}
|
||||
|
||||
Pass *CreatePrinterPass(std::ostream &os,
|
||||
const std::string &banner) const override {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
protected:
|
||||
// Add a Fluid Op into the ProgramDesc.
|
||||
void AddFluidOp(Node *node);
|
||||
// Add a EngineOp into the ProgramDesc.
|
||||
void AddEngineOp(Node *node);
|
||||
|
||||
private:
|
||||
framework::proto::ProgramDesc *desc_;
|
||||
};
|
||||
} // namespace analysis
|
||||
} // namespace inference
|
||||
} // namespace paddle
|
@ -0,0 +1,54 @@
|
||||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#include "paddle/fluid/inference/analysis/dfg_graphviz_draw_pass.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace inference {
|
||||
namespace analysis {
|
||||
|
||||
void DFG_GraphvizDrawPass::Run(DataFlowGraph *graph) {
|
||||
auto content = Draw(graph);
|
||||
std::ofstream file(GenDotPath());
|
||||
file.write(content.c_str(), content.size());
|
||||
file.close();
|
||||
LOG(INFO) << "draw dot to " << GenDotPath();
|
||||
}
|
||||
|
||||
std::string DFG_GraphvizDrawPass::Draw(DataFlowGraph *graph) {
|
||||
Dot dot;
|
||||
// Add nodes
|
||||
for (size_t i = 0; i < graph->nodes.size(); i++) {
|
||||
const Node &node = graph->nodes.Get(i);
|
||||
if (config_.display_deleted_node || !node.deleted()) {
|
||||
dot.AddNode(node.repr(), node.dot_attrs());
|
||||
}
|
||||
}
|
||||
// Add edges
|
||||
for (size_t i = 0; i < graph->nodes.size(); i++) {
|
||||
const Node &node = graph->nodes.Get(i);
|
||||
if (!config_.display_deleted_node && node.deleted()) continue;
|
||||
for (auto &in : node.inlinks) {
|
||||
if (!config_.display_deleted_node && in->deleted()) continue;
|
||||
for (auto &in : node.inlinks) {
|
||||
dot.AddEdge(in->repr(), node.repr(), {});
|
||||
}
|
||||
}
|
||||
}
|
||||
return dot.Build();
|
||||
}
|
||||
|
||||
} // namespace analysis
|
||||
} // namespace inference
|
||||
} // namespace paddle
|
@ -0,0 +1,44 @@
|
||||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#include "paddle/fluid/inference/analysis/pass_manager.h"
|
||||
#include "paddle/fluid/inference/analysis/fluid_to_data_flow_graph_pass.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace inference {
|
||||
namespace analysis {
|
||||
|
||||
void DfgPassManager::RunAll() {
|
||||
PADDLE_ENFORCE(argument_);
|
||||
for (auto& pass : data_) {
|
||||
VLOG(4) << "Running pass [" << pass->repr() << "]";
|
||||
pass->Run(argument_->main_dfg.get());
|
||||
}
|
||||
}
|
||||
|
||||
void NodePassManager::RunAll() {
|
||||
PADDLE_ENFORCE(argument_);
|
||||
PADDLE_ENFORCE(argument_->main_dfg.get());
|
||||
auto trait =
|
||||
GraphTraits<DataFlowGraph>(argument_->main_dfg.get()).nodes_in_DFS();
|
||||
for (auto& node : trait) {
|
||||
for (auto& pass : data_) {
|
||||
pass->Run(&node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace analysis
|
||||
} // namespace inference
|
||||
} // namespace paddle
|
@ -0,0 +1,116 @@
|
||||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
/*
|
||||
* This file defines the logic of pass management. The analysis for inference is
|
||||
* a pipeline of Passes, a PassManager is a agency that helps to manage the
|
||||
* executation of the Passes.
|
||||
*
|
||||
* There are two modes of Passes, the first one is called NodePass and takes
|
||||
* an Node as input and output; the second one is called DFGPass and takes a
|
||||
* DFG(Data Flow Graph) as input and output. It is hard to put all the passes in
|
||||
* the same pipeline, there are two kinds of PassManagers, both takes a DFG as
|
||||
* input and output a DFG, but the Passes inside are different:
|
||||
*
|
||||
* 1. NodePassManager: the passes inside are all NodePasses, it can have
|
||||
* different graph trivial algorithm, for example, DFS_NodePassManager will
|
||||
* trigger the passes in depth first order;
|
||||
* 2. DfgPassManager: the passes inside are all DfgPasses.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "paddle/fluid/framework/program_desc.h"
|
||||
#include "paddle/fluid/inference/analysis/pass.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace inference {
|
||||
namespace analysis {
|
||||
|
||||
/*
|
||||
* PassManager is the base class for all pass managers, a pass manager has
|
||||
* several Pass-es registered, and execute them in the linear order.
|
||||
*/
|
||||
class PassManager : public OrderedRegistry<Pass> {
|
||||
public:
|
||||
PassManager() = default;
|
||||
// Call all the passes' Initialize methods. The desc and data_flow_graph are
|
||||
// globally shared, so pass them as the arguemnts for all the pass managers.
|
||||
virtual bool Initialize(const Argument& argument) { return false; }
|
||||
|
||||
virtual bool Initialize(Argument* argument) {
|
||||
argument_ = argument;
|
||||
for (auto& pass : data_) {
|
||||
LOG(INFO) << "Initializing pass " << pass->repr();
|
||||
if (!pass->Initialize(argument)) {
|
||||
LOG(ERROR) << "Failed to initialize pass [" << pass->repr() << "]";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Call all the passes' Finalize methods.
|
||||
virtual bool Finalize() {
|
||||
for (auto& pass : data_) {
|
||||
if (!pass->Finalize()) {
|
||||
LOG(ERROR) << "Failed to finalize pass [" << pass->repr() << "]";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Run all the passes.
|
||||
virtual void RunAll() = 0;
|
||||
|
||||
// Short identifier.
|
||||
virtual std::string repr() const = 0;
|
||||
// Long description.
|
||||
virtual std::string description() const = 0;
|
||||
|
||||
virtual ~PassManager() = default;
|
||||
|
||||
protected:
|
||||
Argument* argument_{nullptr};
|
||||
};
|
||||
|
||||
/*
|
||||
* A pass manager that process a DFG.
|
||||
*/
|
||||
class DfgPassManager : public PassManager {
|
||||
public:
|
||||
DfgPassManager() = default;
|
||||
|
||||
void RunAll() override;
|
||||
|
||||
virtual ~DfgPassManager() = default;
|
||||
};
|
||||
|
||||
/*
|
||||
* A pass manager that process a Node each time.
|
||||
*/
|
||||
class NodePassManager : public PassManager {
|
||||
public:
|
||||
NodePassManager() = default;
|
||||
|
||||
void RunAll() override;
|
||||
|
||||
virtual ~NodePassManager() = default;
|
||||
};
|
||||
|
||||
} // namespace analysis
|
||||
} // namespace inference
|
||||
} // namespace paddle
|
@ -0,0 +1,85 @@
|
||||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#include "paddle/fluid/inference/analysis/pass_manager.h"
|
||||
#include "paddle/fluid/inference/analysis/data_flow_graph_to_fluid_pass.h"
|
||||
#include "paddle/fluid/inference/analysis/dfg_graphviz_draw_pass.h"
|
||||
#include "paddle/fluid/inference/analysis/fluid_to_data_flow_graph_pass.h"
|
||||
#include "paddle/fluid/inference/analysis/ut_helper.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace paddle {
|
||||
namespace inference {
|
||||
namespace analysis {
|
||||
|
||||
class TestDfgPassManager final : public DfgPassManager {
|
||||
public:
|
||||
TestDfgPassManager() = default;
|
||||
virtual ~TestDfgPassManager() = default;
|
||||
// Short identifier.
|
||||
std::string repr() const override { return "test-pass-manager"; }
|
||||
// Long description.
|
||||
std::string description() const override { return "test doc"; }
|
||||
};
|
||||
|
||||
class TestNodePassManager final : public NodePassManager {
|
||||
public:
|
||||
virtual ~TestNodePassManager() = default;
|
||||
|
||||
std::string repr() const override { return "test-node-pass-manager"; }
|
||||
std::string description() const override { return "test doc"; }
|
||||
};
|
||||
|
||||
class TestNodePass final : public NodePass {
|
||||
public:
|
||||
virtual ~TestNodePass() = default;
|
||||
|
||||
bool Initialize(Argument* argument) override { return true; }
|
||||
|
||||
void Run(Node* node) override {
|
||||
LOG(INFO) << "- Processing node " << node->repr();
|
||||
}
|
||||
|
||||
std::string repr() const override { return "test-node"; }
|
||||
std::string description() const override { return "some doc"; }
|
||||
};
|
||||
|
||||
TEST_F(DFG_Tester, DFG_pass_manager) {
|
||||
TestDfgPassManager manager;
|
||||
DFG_GraphvizDrawPass::Config config("./", "dfg.dot");
|
||||
|
||||
manager.Register("fluid-to-flow-graph", new FluidToDataFlowGraphPass);
|
||||
manager.Register("graphviz", new DFG_GraphvizDrawPass(config));
|
||||
manager.Register("dfg-to-fluid", new DataFlowGraphToFluidPass);
|
||||
|
||||
ASSERT_TRUE(manager.Initialize(&argument));
|
||||
manager.RunAll();
|
||||
}
|
||||
|
||||
TEST_F(DFG_Tester, Node_pass_manager) {
|
||||
// Pre-process: initialize the DFG with the ProgramDesc first.
|
||||
FluidToDataFlowGraphPass pass0;
|
||||
pass0.Initialize(&argument);
|
||||
pass0.Run(argument.main_dfg.get());
|
||||
|
||||
TestNodePassManager manager;
|
||||
manager.Register("test-node-pass", new TestNodePass);
|
||||
ASSERT_TRUE(manager.Initialize(&argument));
|
||||
manager.RunAll();
|
||||
}
|
||||
|
||||
} // namespace analysis
|
||||
} // namespace inference
|
||||
} // namespace paddle
|
@ -0,0 +1,33 @@
|
||||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "paddle/fluid/inference/analysis/tensorrt_subgraph_pass.h"
|
||||
#include "paddle/fluid/inference/analysis/subgraph_splitter.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace inference {
|
||||
namespace analysis {
|
||||
|
||||
TensorRTSubGraphPass::TensorRTSubGraphPass(
|
||||
const TensorRTSubGraphPass::NodeInsideSubgraphTeller &teller)
|
||||
: node_inside_subgraph_teller_(teller) {}
|
||||
|
||||
void TensorRTSubGraphPass::Run(DataFlowGraph *graph) {
|
||||
SubGraphFuse(graph, node_inside_subgraph_teller_);
|
||||
}
|
||||
|
||||
} // analysis
|
||||
} // inference
|
||||
|
||||
} // paddle
|
@ -0,0 +1,47 @@
|
||||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "paddle/fluid/inference/analysis/node.h"
|
||||
#include "paddle/fluid/inference/analysis/pass.h"
|
||||
#include "paddle/fluid/inference/analysis/subgraph_splitter.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace inference {
|
||||
namespace analysis {
|
||||
|
||||
/*
|
||||
* Parse the graph and replace TensorRT supported nodes with SubGraphNode
|
||||
*/
|
||||
class TensorRTSubGraphPass : public DataFlowGraphPass {
|
||||
public:
|
||||
// Tell whether to transform a sub-graph into TensorRT.
|
||||
using NodeInsideSubgraphTeller = SubGraphFuse::NodeInsideSubgraphTeller;
|
||||
|
||||
TensorRTSubGraphPass(const NodeInsideSubgraphTeller& teller);
|
||||
|
||||
bool Initialize(Argument* argument) override { return true; }
|
||||
|
||||
// This class get a sub-graph as input and determine whether to transform this
|
||||
// sub-graph into TensorRT.
|
||||
void Run(DataFlowGraph* graph) override;
|
||||
|
||||
private:
|
||||
NodeInsideSubgraphTeller node_inside_subgraph_teller_;
|
||||
};
|
||||
|
||||
} // namespace analysis
|
||||
} // namespace inference
|
||||
} // paddle
|
@ -0,0 +1,71 @@
|
||||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#include "paddle/fluid/inference/analysis/tensorrt_subgraph_pass.h"
|
||||
|
||||
#include <gflags/gflags.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include "paddle/fluid/inference/analysis/dfg_graphviz_draw_pass.h"
|
||||
#include "paddle/fluid/inference/analysis/ut_helper.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace inference {
|
||||
namespace analysis {
|
||||
|
||||
DEFINE_string(model_dir, "", "inference test model dir");
|
||||
|
||||
TEST(TensorRTSubGraph, single_pass) {
|
||||
auto desc = LoadProgramDesc();
|
||||
auto dfg = ProgramDescToDFG(desc);
|
||||
|
||||
SubGraphSplitter::NodeInsideSubgraphTeller teller = [](const Node* node) {
|
||||
if (node->type() != Node::Type::kFunction) return false;
|
||||
const auto* func = static_cast<const Function*>(node);
|
||||
if (func->func_type() == "elementwise_add" || func->func_type() == "relu" ||
|
||||
func->func_type() == "conv2d" || func->func_type() == "mul" ||
|
||||
func->func_type() == "sigmoid" || func->func_type() == "softmax") {
|
||||
LOG(INFO) << "sub-graph marked " << node->repr();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
DFG_GraphvizDrawPass::Config config{"./", "test"};
|
||||
DFG_GraphvizDrawPass dfg_pass(config);
|
||||
dfg_pass.Initialize();
|
||||
|
||||
DFG_GraphvizDrawPass dfg_pass1(config);
|
||||
dfg_pass1.Initialize();
|
||||
|
||||
dfg_pass.Run(&dfg);
|
||||
|
||||
TensorRTSubGraphPass trt_pass(std::move(teller));
|
||||
trt_pass.Initialize();
|
||||
|
||||
trt_pass.Run(&dfg);
|
||||
|
||||
dfg_pass1.Run(&dfg);
|
||||
|
||||
// Check the TRT op's block desc
|
||||
for (auto node : dfg.nodes.nodes()) {
|
||||
if (node->IsFunctionBlock()) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(TensorRTSubGraph, pass_manager) {}
|
||||
|
||||
} // namespace analysis
|
||||
} // namespace inference
|
||||
} // namespace paddle
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue