commit
f1f8327c31
@ -1,25 +0,0 @@
|
|||||||
# Copyright (c) 2016 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.
|
|
||||||
#
|
|
||||||
|
|
||||||
if(APPLE)
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=pessimizing-move")
|
|
||||||
endif(APPLE)
|
|
||||||
|
|
||||||
cc_library(tape_variable SRCS variable.cc DEPS ${FLUID_CORE_MODULES} device_context framework_proto proto_desc operator)
|
|
||||||
cc_library(tape SRCS tape.cc DEPS ${FLUID_CORE_MODULES} ${GLOB_OP_LIB} tape_variable)
|
|
||||||
|
|
||||||
cc_test(test_tape
|
|
||||||
SRCS test_tape.cc
|
|
||||||
DEPS tape tape_variable)
|
|
File diff suppressed because it is too large
Load Diff
Before Width: | Height: | Size: 94 KiB |
@ -1,131 +0,0 @@
|
|||||||
// 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 <string>
|
|
||||||
|
|
||||||
#include "paddle/contrib/tape/tape.h"
|
|
||||||
#include "paddle/contrib/tape/variable.h"
|
|
||||||
#include "paddle/fluid/framework/type_defs.h"
|
|
||||||
|
|
||||||
namespace paddle {
|
|
||||||
namespace tape {
|
|
||||||
|
|
||||||
class Function {};
|
|
||||||
|
|
||||||
class Fill {
|
|
||||||
public:
|
|
||||||
Fill(const std::string &initializer, const framework::AttributeMap &attrs)
|
|
||||||
: initializer_(initializer), attrs_(attrs) {}
|
|
||||||
|
|
||||||
void operator()(VariableHandle var) {
|
|
||||||
get_global_tape().AddOp(initializer_, {}, {{"Out", {var}}}, attrs_);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
const std::string initializer_;
|
|
||||||
const framework::AttributeMap attrs_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Mean {
|
|
||||||
public:
|
|
||||||
VariableHandle operator()(VariableHandle var) {
|
|
||||||
VariableHandle out(new Variable("mean"));
|
|
||||||
get_global_tape().AddOp("mean", {{"X", {var}}}, {{"Out", {out}}}, {});
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class Linear {
|
|
||||||
public:
|
|
||||||
Linear(int in_dim, int out_dim, const std::string &act)
|
|
||||||
: w_(new Variable("LinearWeight")),
|
|
||||||
b_(new Variable("LinearBias")),
|
|
||||||
act_(act) {
|
|
||||||
Tape init_tape;
|
|
||||||
|
|
||||||
std::string initializer = "fill_constant";
|
|
||||||
framework::AttributeMap attrs;
|
|
||||||
attrs["dtype"] = paddle::framework::proto::VarType::Type::VarType_Type_FP32;
|
|
||||||
attrs["shape"] = std::vector<int>{in_dim, out_dim};
|
|
||||||
attrs["value"] = 1.0f;
|
|
||||||
init_tape.AddOp(initializer, {}, {{"Out", {w_}}}, attrs);
|
|
||||||
|
|
||||||
attrs["dtype"] = paddle::framework::proto::VarType::Type::VarType_Type_FP32;
|
|
||||||
attrs["shape"] = std::vector<int>{out_dim};
|
|
||||||
attrs["value"] = 1.0f;
|
|
||||||
init_tape.AddOp(initializer, {}, {{"Out", {b_}}}, attrs);
|
|
||||||
|
|
||||||
init_tape.Forward();
|
|
||||||
}
|
|
||||||
|
|
||||||
VariableHandle operator()(VariableHandle input) {
|
|
||||||
VariableHandle pre_bias(new Variable("linear"));
|
|
||||||
get_global_tape().AddOp("mul",
|
|
||||||
{{"X", {input}}, {"Y", {w_}}},
|
|
||||||
{{"Out", {pre_bias}}},
|
|
||||||
{{"x_num_col_dims", 1}, {"y_num_col_dims", 1}});
|
|
||||||
VariableHandle pre_act(new Variable("linear"));
|
|
||||||
get_global_tape().AddOp("elementwise_add",
|
|
||||||
{{"X", {pre_bias}}, {"Y", {b_}}},
|
|
||||||
{{"Out", {pre_act}}},
|
|
||||||
{{"axis", 1}});
|
|
||||||
VariableHandle post_act(new Variable("linear"));
|
|
||||||
get_global_tape().AddOp(
|
|
||||||
act_, {{"X", {pre_act}}}, {{"Out", {post_act}}}, {});
|
|
||||||
return post_act;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<VariableHandle> Params() { return {w_, b_}; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
VariableHandle w_;
|
|
||||||
VariableHandle b_;
|
|
||||||
std::string act_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class SGD {
|
|
||||||
public:
|
|
||||||
SGD(float learning_rate) : learning_rate_(new Variable("sgd")) {
|
|
||||||
Tape init_tape;
|
|
||||||
|
|
||||||
std::string initializer = "fill_constant";
|
|
||||||
framework::AttributeMap attrs;
|
|
||||||
attrs["dtype"] = paddle::framework::proto::VarType::Type::VarType_Type_FP32;
|
|
||||||
attrs["shape"] = std::vector<int>{1};
|
|
||||||
attrs["value"] = learning_rate;
|
|
||||||
init_tape.AddOp(initializer, {}, {{"Out", {learning_rate_}}}, attrs);
|
|
||||||
|
|
||||||
init_tape.Forward();
|
|
||||||
}
|
|
||||||
|
|
||||||
void operator()(VariableHandle input) {
|
|
||||||
PADDLE_ENFORCE(get_global_tape().HasBeenBackwarded(),
|
|
||||||
"optimization must happen after the backward");
|
|
||||||
Tape temp_tape;
|
|
||||||
temp_tape.AddOp("sgd",
|
|
||||||
{{"Param", {input}},
|
|
||||||
{"LearningRate", {learning_rate_}},
|
|
||||||
{"Grad", {input->Grad()}}},
|
|
||||||
{{"ParamOut", {input}}},
|
|
||||||
{});
|
|
||||||
temp_tape.Forward();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
VariableHandle learning_rate_;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load Diff
@ -1,64 +0,0 @@
|
|||||||
// 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 <map>
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "paddle/contrib/tape/variable.h"
|
|
||||||
|
|
||||||
namespace paddle {
|
|
||||||
namespace tape {
|
|
||||||
|
|
||||||
using VariableHandleMap = std::map<std::string, std::vector<VariableHandle>>;
|
|
||||||
|
|
||||||
struct OpHandle {
|
|
||||||
OpHandle(const std::string &type,
|
|
||||||
const VariableHandleMap &in_vars,
|
|
||||||
const VariableHandleMap &out_vars,
|
|
||||||
const framework::AttributeMap &attrs)
|
|
||||||
: type_(type), inputs_(in_vars), outputs_(out_vars), attrs_(attrs) {}
|
|
||||||
|
|
||||||
std::string type_;
|
|
||||||
VariableHandleMap inputs_;
|
|
||||||
VariableHandleMap outputs_;
|
|
||||||
framework::AttributeMap attrs_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Tape {
|
|
||||||
public:
|
|
||||||
void AddOp(const std::string &type,
|
|
||||||
const VariableHandleMap &in_vars,
|
|
||||||
VariableHandleMap out_vars,
|
|
||||||
const framework::AttributeMap &attrs);
|
|
||||||
void Forward();
|
|
||||||
void Backward(VariableHandle target);
|
|
||||||
|
|
||||||
bool HasBeenBackwarded() { return has_been_backwarded_; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool has_been_backwarded_ = false;
|
|
||||||
size_t current_position_ = 0;
|
|
||||||
|
|
||||||
std::vector<OpHandle> tape_;
|
|
||||||
std::shared_ptr<Tape> backward_tape_;
|
|
||||||
};
|
|
||||||
|
|
||||||
Tape &get_global_tape();
|
|
||||||
|
|
||||||
void reset_global_tape();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,61 +0,0 @@
|
|||||||
// 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 "gtest/gtest.h"
|
|
||||||
#include "paddle/contrib/tape/function.h"
|
|
||||||
|
|
||||||
using namespace paddle::tape;
|
|
||||||
|
|
||||||
TEST(Tape, TestMLP) {
|
|
||||||
LOG(INFO) << "TestMLP";
|
|
||||||
Linear linear1(3, 3, "relu");
|
|
||||||
Linear linear2(3, 3, "relu");
|
|
||||||
Mean mean;
|
|
||||||
|
|
||||||
SGD sgd(0.001);
|
|
||||||
|
|
||||||
std::string initializer = "fill_constant";
|
|
||||||
paddle::framework::AttributeMap attrs;
|
|
||||||
attrs["dtype"] = paddle::framework::proto::VarType::Type::VarType_Type_FP32;
|
|
||||||
attrs["shape"] = std::vector<int>{3, 3};
|
|
||||||
attrs["value"] = 1.0f;
|
|
||||||
Fill filler(initializer, attrs);
|
|
||||||
|
|
||||||
for (int i = 0; i < 2; ++i) {
|
|
||||||
reset_global_tape();
|
|
||||||
|
|
||||||
VariableHandle input(new Variable("input"));
|
|
||||||
filler(input);
|
|
||||||
|
|
||||||
auto loss = mean(linear2(linear1(input)));
|
|
||||||
|
|
||||||
get_global_tape().Backward(loss);
|
|
||||||
|
|
||||||
for (auto w : linear1.Params()) {
|
|
||||||
sgd(w);
|
|
||||||
}
|
|
||||||
for (auto w : linear2.Params()) {
|
|
||||||
sgd(w);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
|
||||||
std::vector<paddle::platform::Place> places;
|
|
||||||
places.emplace_back(paddle::platform::CPUPlace());
|
|
||||||
paddle::platform::DeviceContextPool::Init(places);
|
|
||||||
|
|
||||||
testing::InitGoogleTest(&argc, argv);
|
|
||||||
return RUN_ALL_TESTS();
|
|
||||||
}
|
|
@ -1,85 +0,0 @@
|
|||||||
// 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 <memory>
|
|
||||||
|
|
||||||
#include "paddle/fluid/framework/operator.h" // framework::kGradVarSuffix
|
|
||||||
#include "paddle/fluid/framework/program_desc.h"
|
|
||||||
#include "paddle/fluid/framework/variable.h"
|
|
||||||
|
|
||||||
namespace paddle {
|
|
||||||
namespace tape {
|
|
||||||
|
|
||||||
class Variable;
|
|
||||||
using VariableHandle = std::shared_ptr<Variable>;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Combination of
|
|
||||||
* framework::VarDesc desc_;
|
|
||||||
* framework::Variable var_;
|
|
||||||
*/
|
|
||||||
class Variable {
|
|
||||||
public:
|
|
||||||
Variable(const std::string pre_fix)
|
|
||||||
: desc_(pre_fix + std::to_string(count())) {}
|
|
||||||
|
|
||||||
Variable(const std::string pre_fix, bool is_grad)
|
|
||||||
: desc_(pre_fix + (is_grad ? framework::kGradVarSuffix
|
|
||||||
: std::to_string(count()))) {}
|
|
||||||
|
|
||||||
~Variable() { LOG(INFO) << "Deleting " << Name(); }
|
|
||||||
|
|
||||||
// Instantiate LoDTensor/SelectedRow
|
|
||||||
void InitializeVariable();
|
|
||||||
|
|
||||||
VariableHandle Grad() {
|
|
||||||
if (grad_.expired()) {
|
|
||||||
VariableHandle new_grad(new Variable(desc_.Name(), true));
|
|
||||||
grad_ = new_grad;
|
|
||||||
return new_grad;
|
|
||||||
} else {
|
|
||||||
return VariableHandle(grad_);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stochastic Gradient Descent with Momentum
|
|
||||||
// VariableHandle Momentum ();
|
|
||||||
|
|
||||||
// void init(const std::string& initializer,
|
|
||||||
// const framework::AttributeMap& attrs);
|
|
||||||
|
|
||||||
// void value() {};
|
|
||||||
|
|
||||||
const framework::VarDesc& Desc() const { return desc_; }
|
|
||||||
framework::VarDesc* MutableDesc() { return &desc_; }
|
|
||||||
|
|
||||||
// TODO(tonyyang-svail): No need to expose name
|
|
||||||
std::string Name() const { return desc_.Name(); }
|
|
||||||
|
|
||||||
framework::Variable* Var() { return &var_; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
int count() {
|
|
||||||
static int counter = 0;
|
|
||||||
return counter++;
|
|
||||||
}
|
|
||||||
|
|
||||||
framework::VarDesc desc_;
|
|
||||||
framework::Variable var_;
|
|
||||||
|
|
||||||
std::weak_ptr<Variable> grad_;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,23 +1,32 @@
|
|||||||
set(FLUID_CORE_MODULES proto_desc memory lod_tensor executor init)
|
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
|
cc_library(analysis SRCS pass_manager.cc dot.cc node.cc data_flow_graph.cc graph_traits.cc subgraph_splitter.cc
|
||||||
DEPS paddle_fluid)
|
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_node SRCS node_tester.cc DEPS analysis)
|
||||||
cc_test(test_dot SRCS dot_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)
|
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
|
function (inference_analysis_test TARGET)
|
||||||
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model)
|
set(options "")
|
||||||
set_tests_properties(test_data_flow_graph PROPERTIES DEPENDS test_word2vec)
|
set(oneValueArgs "")
|
||||||
|
set(multiValueArgs SRCS)
|
||||||
|
cmake_parse_arguments(analysis_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
||||||
|
|
||||||
cc_test(test_subgraph_splitter
|
cc_test(${TARGET}
|
||||||
SRCS subgraph_splitter_tester.cc
|
SRCS "${analysis_test_SRCS}"
|
||||||
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(test_dfg_graphviz_draw_pass
|
|
||||||
SRCS dfg_graphviz_draw_pass_tester.cc
|
|
||||||
DEPS analysis
|
DEPS analysis
|
||||||
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model)
|
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model --fraction_of_gpu_memory_to_use=0.5)
|
||||||
set_tests_properties(test_dfg_graphviz_draw_pass PROPERTIES DEPENDS test_word2vec)
|
set_tests_properties(${TARGET} PROPERTIES DEPENDS test_word2vec)
|
||||||
|
endfunction(inference_analysis_test)
|
||||||
|
|
||||||
|
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,55 @@
|
|||||||
|
// 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#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
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue