diff --git a/CMakeLists.txt b/CMakeLists.txt index 5846dc22c32c297a0f1e8bd2a2cb4717c74139cd..c7a613c8e7529b42c48c4e0b10a9895c056f2e52 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,7 +151,6 @@ if(NOT WIN32) include(cupti) endif() -include(anakin_subgraph) include(flags) # set paddle compile flags include(cudnn) # set cudnn libraries, must before configure diff --git a/cmake/anakin_subgraph.cmake b/cmake/anakin_subgraph.cmake deleted file mode 100644 index 177f34438d6ac8731390e44255072718039bf01c..0000000000000000000000000000000000000000 --- a/cmake/anakin_subgraph.cmake +++ /dev/null @@ -1,45 +0,0 @@ -set(ANAKIN_ROOT "/usr" CACHE PATH "ANAKIN ROOT") -find_path(ANAKIN_INCLUDE_DIR anakin_config.h - PATHS ${ANAKIN_ROOT} ${ANAKIN_ROOT}/include - $ENV{ANAKIN_ROOT} $ENV{ANAKIN_ROOT}/include - NO_DEFAULT_PATH -) - -find_library(ANAKIN_LIBRARY NAMES libanakin_saber_common.so libanakin.so - PATHS ${ANAKIN_ROOT} - $ENV{ANAKIN_ROOT} $ENV{ANAKIN_ROOT}/lib - NO_DEFAULT_PATH - DOC "Path to ANAKIN library.") - -if(ANAKIN_INCLUDE_DIR AND ANAKIN_LIBRARY) - set(ANAKIN_FOUND ON) -else() - set(ANAKIN_FOUND OFF) -endif() - -if(ANAKIN_FOUND) - message(STATUS "Current ANAKIN header is ${ANAKIN_INCLUDE_DIR}/anakin_config.h. ") - include_directories(${ANAKIN_ROOT}) - include_directories(${ANAKIN_ROOT}/include) - include_directories(${ANAKIN_ROOT}/saber) - link_directories(${ANAKIN_ROOT}) - add_definitions(-DPADDLE_WITH_ANAKIN) -endif() - -if(ANAKIN_FOUND) - if (ANAKIN_MLU AND NOT WITH_GPU AND NOT ANAKIN_X86) - message(STATUS "Compile with anakin mlu place.") - add_definitions(-DANAKIN_MLU_PLACE) - elseif(ANAKIN_BM AND NOT WITH_GPU AND NOT ANAKIN_X86) - message(STATUS "Compile with anakin bm place.") - add_definitions(-DANAKIN_BM_PLACE) - elseif(ANAKIN_X86) - message(STATUS "Compile with anakin x86 place.") - add_definitions(-DANAKIN_X86_PLACE) - endif() -endif() - -if(ANAKIN_FOUND AND WITH_GPU AND WITH_DSO) - message(STATUS "Compile with anakin subgraph.") - set(ANAKIN_SUBGRAPH ON) -endif() diff --git a/cmake/configure.cmake b/cmake/configure.cmake index c8aaa6b10cde389a63dcda5ca0a98eacc5294fb2..72425f3f485b3fa547ad37b982b9c3bdb8756315 100644 --- a/cmake/configure.cmake +++ b/cmake/configure.cmake @@ -109,16 +109,6 @@ if(WITH_GPU) endif() include_directories(${TENSORRT_INCLUDE_DIR}) endif() - if(ANAKIN_FOUND) - if(${CUDA_VERSION_MAJOR} VERSION_LESS 8) - message(WARNING "Anakin needs CUDA >= 8.0 to compile. Force ANAKIN_FOUND = OFF") - set(ANAKIN_FOUND OFF CACHE STRING "Anakin is valid only when CUDA >= 8.0." FORCE) - endif() - if(${CUDNN_MAJOR_VERSION} VERSION_LESS 7) - message(WARNING "Anakin needs CUDNN >= 7.0 to compile. Force ANAKIN_FOUND = OFF") - set(ANAKIN_FOUND OFF CACHE STRING "Anakin is valid only when CUDNN >= 7.0." FORCE) - endif() - endif() elseif(WITH_AMD_GPU) add_definitions(-DPADDLE_WITH_HIP) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__HIP_PLATFORM_HCC__") diff --git a/cmake/inference_lib.cmake b/cmake/inference_lib.cmake index 6386eb68f74f707a6f72eb5b91f4c24b7d68a682..17dcf65966dcc9a0892fca991d8ae4a6999ab65f 100644 --- a/cmake/inference_lib.cmake +++ b/cmake/inference_lib.cmake @@ -137,13 +137,6 @@ function(copy_part_of_thrid_party TARGET DST) SRCS ${LITE_BINARY_DIR}/inference_lite_lib/* DSTS ${dst_dir}) endif() - - if (ANAKIN_FOUND) - set(dst_dir "${DST}/third_party/install/anakin") - copy(${TARGET} - SRCS ${ANAKIN_ROOT}/* - DSTS ${dst_dir}) - endif () endfunction() # inference library for only inference diff --git a/paddle/fluid/framework/ir/CMakeLists.txt b/paddle/fluid/framework/ir/CMakeLists.txt index 73264140f9452f738df2239431cbf780eddb7ba1..fe2e1ab30dd7411545400f3ee7f5737ecc866888 100644 --- a/paddle/fluid/framework/ir/CMakeLists.txt +++ b/paddle/fluid/framework/ir/CMakeLists.txt @@ -71,7 +71,6 @@ pass_library(identity_scale_op_clean_pass base) pass_library(sync_batch_norm_pass base) pass_library(runtime_context_cache_pass base) pass_library(quant_conv2d_dequant_fuse_pass inference) -pass_library(fillconstant_elementwisemul_fuse inference) pass_library(shuffle_channel_detect_pass inference) pass_library(delete_quant_dequant_op_pass inference) pass_library(simplify_with_basic_ops_pass base) @@ -81,10 +80,6 @@ if(WITH_GPU) pass_library(cudnn_placement_pass base DEPS placement_pass_base) endif() -if(ANAKIN_SUBGRAPH) -pass_library(simplify_anakin_priorbox_detection_out_pass inference) -endif() - if(WITH_MKLDNN) pass_library(mkldnn_placement_pass base DEPS placement_pass_base DIR mkldnn) pass_library(depthwise_conv_mkldnn_pass base DIR mkldnn) diff --git a/paddle/fluid/framework/ir/fillconstant_elementwisemul_fuse.cc b/paddle/fluid/framework/ir/fillconstant_elementwisemul_fuse.cc deleted file mode 100644 index 915a2f62bafa2baf98b7407cd87d3e69f20b44d2..0000000000000000000000000000000000000000 --- a/paddle/fluid/framework/ir/fillconstant_elementwisemul_fuse.cc +++ /dev/null @@ -1,83 +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 -#include - -#include "paddle/fluid/framework/ir/fillconstant_elementwisemul_fuse.h" -#include "paddle/fluid/framework/ir/graph_viz_pass.h" - -namespace paddle { -namespace framework { -namespace ir { - -#define GET_IR_NODE(node__) GET_IR_NODE_FROM_SUBGRAPH(node__, node__, pattern); -#define GET_NODES \ - GET_IR_NODE(fill_constant); \ - GET_IR_NODE(fill_constant_out); \ - GET_IR_NODE(elementwise_mul); \ - GET_IR_NODE(elementwise_mul_out); - -void FillconstantElementwisemulFuse::ApplyImpl(ir::Graph* graph) const { - const std::string pattern_name = "fillconstant_elementwisemul_fuse"; - FusePassBase::Init(pattern_name, graph); - - GraphPatternDetector gpd; - auto* x = gpd.mutable_pattern() - ->NewNode("x") - ->assert_is_op_input("elementwise_mul", "X") - ->AsInput(); - - patterns::FillConstantElementWiseMulFuse pattern(gpd.mutable_pattern(), - pattern_name); - pattern(x); - - auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph, - Graph* g) { - GET_NODES; - - PADDLE_ENFORCE(subgraph.count(x)); - auto* elementwise_in = subgraph.at(x); - float constant_value = - boost::get(fill_constant->Op()->GetAttr("value")); - - framework::OpDesc new_op_desc; - new_op_desc.SetType("scale"); - new_op_desc.SetInput("X", {elementwise_in->Name()}); - new_op_desc.SetAttr("scale", constant_value); - new_op_desc.SetAttr("bias", static_cast(0.0)); - new_op_desc.SetAttr("bias_after_scale", true); - new_op_desc.SetOutput("Out", {elementwise_mul_out->Name()}); - new_op_desc.Flush(); - - // Create a new node for the fused op. - auto* scale_op = graph->CreateOpNode(&new_op_desc); - - IR_NODE_LINK_TO(elementwise_in, scale_op); // Input - IR_NODE_LINK_TO(scale_op, elementwise_mul_out); // Output - - // Delete the unneeded nodes. - GraphSafeRemoveNodes(graph, - {fill_constant, fill_constant_out, elementwise_mul}); - }; - - gpd(graph, handler); -} - -} // namespace ir -} // namespace framework -} // namespace paddle - -REGISTER_PASS(fillconstant_elementwisemul_fuse, - paddle::framework::ir::FillconstantElementwisemulFuse); diff --git a/paddle/fluid/framework/ir/graph_pattern_detector.cc b/paddle/fluid/framework/ir/graph_pattern_detector.cc index be73ccb5f7b21cfd887f17d94090005867628a41..0b4b18c94b439c5cc17fe423fb414fee1569df74 100644 --- a/paddle/fluid/framework/ir/graph_pattern_detector.cc +++ b/paddle/fluid/framework/ir/graph_pattern_detector.cc @@ -1878,173 +1878,6 @@ PDNode *patterns::TransposeFlattenConcat::operator()( return concat_out; } -PDNode *patterns::AnakinDetectionPattern::operator()( - std::vector conv_in, int times, std::string priorbox_type, - bool is_reshape) { - // The times represents the repeat times of the - // {prior_box, prior_box_loc_out, flatten, prior_box_var_out, reshape} - const int kNumFields = 7; - const int kPriorBoxLocOffset = 1; - const int kReshape1Offset = 2; - const int kReshape1OutOffset = 3; - const int kPriorBoxVarOffset = 4; - const int kReshape2Offset = 5; - const int kReshape2OutOffset = 6; - - const int kBoxCoderThirdInputOffset = times; - const int kMultiClassSecondInputNmsOffset = times + 1; - - std::vector nodes; - std::string op_after_priorbox = is_reshape ? "reshape2" : "flatten2"; - - for (int i = 0; i < times; i++) { - nodes.push_back( - pattern->NewNode(GetNodeName("prior_box" + std::to_string(i))) - ->assert_is_op(priorbox_type)); - nodes.push_back(pattern->NewNode(GetNodeName("box_out" + std::to_string(i))) - ->assert_is_op_output(priorbox_type, "Boxes") - ->assert_is_op_input(op_after_priorbox, "X") - ->AsIntermediate()); - nodes.push_back( - pattern->NewNode(GetNodeName("reshape1" + std::to_string(i))) - ->assert_is_op(op_after_priorbox)); - - nodes.push_back( - pattern->NewNode(GetNodeName("reshape1_out" + std::to_string(i))) - ->assert_is_op_output(op_after_priorbox) - ->assert_is_op_nth_input("concat", "X", i) - ->AsIntermediate()); - - nodes.push_back( - pattern->NewNode(GetNodeName("box_var_out" + std::to_string(i))) - ->assert_is_op_output(priorbox_type, "Variances") - ->assert_is_op_input(op_after_priorbox, "X") - ->AsIntermediate()); - nodes.push_back( - pattern->NewNode(GetNodeName("reshape2" + std::to_string(i))) - ->assert_is_op(op_after_priorbox)); - - nodes.push_back( - pattern->NewNode(GetNodeName("reshape2_out" + std::to_string(i))) - ->assert_is_op_output(op_after_priorbox) - ->assert_is_op_nth_input("concat", "X", i) - ->AsIntermediate()); - } - - auto concat_op1 = pattern->NewNode(GetNodeName("concat1")) - ->assert_is_op("concat") - ->assert_op_has_n_inputs("concat", times); - auto concat_out1 = pattern->NewNode(GetNodeName("concat1_out")) - ->assert_is_op_output("concat") - ->AsIntermediate(); - - auto concat_op2 = pattern->NewNode(GetNodeName("concat2")) - ->assert_is_op("concat") - ->assert_op_has_n_inputs("concat", times); - auto concat_out2 = pattern->NewNode(GetNodeName("concat2_out")) - ->assert_is_op_output("concat") - ->AsIntermediate(); - - auto box_coder_op = pattern->NewNode(GetNodeName("box_coder")) - ->assert_is_op("box_coder") - ->assert_op_has_n_inputs("box_coder", 3); - - auto box_coder_out = pattern->NewNode(GetNodeName("box_coder_out")) - ->assert_is_op_output("box_coder") - ->AsIntermediate(); - - auto transpose_before_nms = - pattern->NewNode(GetNodeName("transpose_before_nms")) - ->assert_is_op("transpose2"); - - auto transpose_before_nms_out = - pattern->NewNode(GetNodeName("transpose_before_nms_out")) - ->assert_is_op_output("transpose2") - ->assert_is_op_input("multiclass_nms", "Scores") - ->AsIntermediate(); - - auto multiclass_nms_op = pattern->NewNode(GetNodeName("multiclass_nms")) - ->assert_is_op("multiclass_nms") - ->assert_op_has_n_inputs("multiclass_nms", 2); - - auto multiclass_nms_out = pattern->NewNode(GetNodeName("multiclass_nms_out")) - ->assert_is_op_output("multiclass_nms") - ->AsOutput(); - - std::vector reshape1_outs; - std::vector reshape2_outs; - - for (int i = 0; i < times; i++) { - conv_in[i]->AsInput(); - // prior_box - nodes[i * kNumFields]->LinksFrom({conv_in[i]}); - // prior_box box out - nodes[i * kNumFields + kPriorBoxLocOffset]->LinksFrom( - {nodes[i * kNumFields]}); - // reshape - nodes[i * kNumFields + kReshape1Offset]->LinksFrom( - {nodes[i * kNumFields + kPriorBoxLocOffset]}); - // reshape_out - nodes[i * kNumFields + kReshape1OutOffset]->LinksFrom( - {nodes[i * kNumFields + kReshape1Offset]}); - - nodes[i * kNumFields + kPriorBoxVarOffset]->LinksFrom( - {nodes[i * kNumFields]}); - // reshape - nodes[i * kNumFields + kReshape2Offset]->LinksFrom( - {nodes[i * kNumFields + kPriorBoxVarOffset]}); - // reshape_out - nodes[i * kNumFields + kReshape2OutOffset]->LinksFrom( - {nodes[i * kNumFields + kReshape2Offset]}); - - reshape1_outs.push_back(nodes[i * kNumFields + kReshape1OutOffset]); - reshape2_outs.push_back(nodes[i * kNumFields + kReshape2OutOffset]); - } - - concat_op1->LinksFrom(reshape1_outs); - concat_op2->LinksFrom(reshape2_outs); - concat_out1->LinksFrom({concat_op1}); - concat_out2->LinksFrom({concat_op2}); - - conv_in[kBoxCoderThirdInputOffset]->AsInput(); - conv_in[kMultiClassSecondInputNmsOffset]->AsInput(); - - box_coder_op->LinksFrom( - {concat_out1, concat_out2, conv_in[kBoxCoderThirdInputOffset]}); - box_coder_out->LinksFrom({box_coder_op}); - - transpose_before_nms->LinksFrom({conv_in[kMultiClassSecondInputNmsOffset]}); - transpose_before_nms_out->LinksFrom({transpose_before_nms}); - - multiclass_nms_op->LinksFrom({box_coder_out, transpose_before_nms_out}) - .LinksTo({multiclass_nms_out}); - - return multiclass_nms_out; -} - -PDNode *patterns::FillConstantElementWiseMulFuse::operator()( - PDNode *elementwise_op_input) { - auto fill_constant = - pattern->NewNode(fill_constant_repr())->assert_is_op("fill_constant"); - - auto fill_constant_out = pattern->NewNode(fill_constant_out_repr()) - ->assert_is_op_output("fill_constant") - ->assert_is_op_input("elementwise_mul", "Y") - ->AsIntermediate(); - - auto elementwise_mul_op = - pattern->NewNode(elementwise_mul_repr())->assert_is_op("elementwise_mul"); - - auto elementwise_mul_out = pattern->NewNode(elementwise_mul_out_repr()) - ->assert_is_op_output("elementwise_mul") - ->AsOutput(); - - fill_constant_out->LinksFrom({fill_constant}); - elementwise_mul_op->LinksFrom({elementwise_op_input, fill_constant_out}); - elementwise_mul_out->LinksFrom({elementwise_mul_op}); - return elementwise_mul_out; -} - void patterns::QuantDequantOpFuse::operator()(PDNode *quant_op_input, const std::string &op_type, const std::string &weight_name, diff --git a/paddle/fluid/framework/ir/graph_pattern_detector.h b/paddle/fluid/framework/ir/graph_pattern_detector.h index 735b23f27314d57a13b19281aafec3059195e7f8..db58c9e8fdd0f95c3c56876acbfba4d3dd85f495 100644 --- a/paddle/fluid/framework/ir/graph_pattern_detector.h +++ b/paddle/fluid/framework/ir/graph_pattern_detector.h @@ -1093,37 +1093,6 @@ struct TransposeFlattenConcat : public PatternBase { } }; -struct AnakinDetectionPattern : public PatternBase { - AnakinDetectionPattern(PDPattern* pattern, const std::string& name_scope) - : PatternBase(pattern, name_scope, "anakin_detect_pattern") {} - - PDNode* operator()(std::vector conv_inputs, int times, - std::string priorbox_type, bool is_reshape); - - std::string GetNodeName(const std::string& op_type) { - return PDNodeName(name_scope_, repr_, id_, op_type); - } - - PDNode* GetPDNode(const std::string& op_type) { - return pattern->RetrieveNode(GetNodeName(op_type)); - } -}; - -struct FillConstantElementWiseMulFuse : public PatternBase { - FillConstantElementWiseMulFuse(PDPattern* pattern, - const std::string& name_scope) - : PatternBase(pattern, name_scope, - "anakin_fillconstant_elementwisemul_fuse") {} - - PDNode* operator()(PDNode* elementwise_op_input); - - // declare operator node's name - PATTERN_DECL_NODE(fill_constant); - PATTERN_DECL_NODE(fill_constant_out); - PATTERN_DECL_NODE(elementwise_mul); - PATTERN_DECL_NODE(elementwise_mul_out); -}; - struct QuantDequantOpFuse : public PatternBase { QuantDequantOpFuse(PDPattern* pattern, const std::string& name_scope) : PatternBase(pattern, name_scope, "quant_dequant_fuse") {} diff --git a/paddle/fluid/framework/ir/simplify_anakin_priorbox_detection_out_pass.cc b/paddle/fluid/framework/ir/simplify_anakin_priorbox_detection_out_pass.cc deleted file mode 100644 index b3606e4d922cc8f59dca90904466a889f83f6094..0000000000000000000000000000000000000000 --- a/paddle/fluid/framework/ir/simplify_anakin_priorbox_detection_out_pass.cc +++ /dev/null @@ -1,233 +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 -#include - -#include "paddle/fluid/framework/ir/graph_viz_pass.h" -#include "paddle/fluid/framework/ir/node.h" -#include "paddle/fluid/framework/ir/simplify_anakin_priorbox_detection_out_pass.h" - -namespace paddle { -namespace framework { -namespace ir { - -void RunSimplifyAnakinDetection(ir::Graph *graph, int times, bool is_density, - bool is_reshape) { - const std::string pattern_name = - "simplify_anakin_detection_pattern_pass" + std::to_string(times); - std::string priorbox_type = is_density ? "density_prior_box" : "prior_box"; - - GraphPatternDetector gpd; - std::vector input_nodes; - for (int i = 0; i < times; i++) { - input_nodes.push_back(gpd.mutable_pattern() - ->NewNode("x" + std::to_string(i)) - ->assert_is_op_input(priorbox_type, "Input") - ->AsInput()); - } - input_nodes.push_back(gpd.mutable_pattern() - ->NewNode("x" + std::to_string(times)) - ->assert_is_op_input("box_coder", "TargetBox") - ->AsInput()); - - input_nodes.push_back(gpd.mutable_pattern() - ->NewNode("x" + std::to_string(times + 1)) - ->assert_is_op_input("transpose2") - ->AsInput()); - - patterns::AnakinDetectionPattern pattern(gpd.mutable_pattern(), pattern_name); - pattern(input_nodes, times, priorbox_type, is_reshape); - - auto handler = [&](const GraphPatternDetector::subgraph_t &subgraph, - Graph *g) { - const int kNumFields = 7; - const int kPriorBoxLocOffset = 1; - const int kReshape1Offset = 2; - const int kReshape1OutOffset = 3; - const int kPriorBoxVarOffset = 4; - const int kReshape2Offset = 5; - const int kReshape2OutOffset = 6; - std::vector nodes; - - for (int i = 0; i < times; i++) { - PADDLE_ENFORCE( - subgraph.at(pattern.GetPDNode("prior_box" + std::to_string(i)))); - PADDLE_ENFORCE( - subgraph.at(pattern.GetPDNode("box_out" + std::to_string(i)))); - PADDLE_ENFORCE( - subgraph.at(pattern.GetPDNode("reshape1" + std::to_string(i)))); - PADDLE_ENFORCE( - subgraph.at(pattern.GetPDNode("reshape1_out" + std::to_string(i)))); - PADDLE_ENFORCE( - subgraph.at(pattern.GetPDNode("reshape2" + std::to_string(i)))); - PADDLE_ENFORCE( - subgraph.at(pattern.GetPDNode("reshape2_out" + std::to_string(i)))); - - PADDLE_ENFORCE( - subgraph.at(pattern.GetPDNode("box_var_out" + std::to_string(i)))); - - nodes.push_back( - subgraph.at(pattern.GetPDNode("prior_box" + std::to_string(i)))); - nodes.push_back( - subgraph.at(pattern.GetPDNode("box_out" + std::to_string(i)))); - nodes.push_back( - subgraph.at(pattern.GetPDNode("reshape1" + std::to_string(i)))); - nodes.push_back( - subgraph.at(pattern.GetPDNode("reshape1_out" + std::to_string(i)))); - nodes.push_back( - subgraph.at(pattern.GetPDNode("box_var_out" + std::to_string(i)))); - nodes.push_back( - subgraph.at(pattern.GetPDNode("reshape2" + std::to_string(i)))); - nodes.push_back( - subgraph.at(pattern.GetPDNode("reshape2_out" + std::to_string(i)))); - } - - Node *concat_op1 = subgraph.at(pattern.GetPDNode("concat1")); - Node *concat_out1 = subgraph.at(pattern.GetPDNode("concat1_out")); - - Node *concat_op2 = subgraph.at(pattern.GetPDNode("concat2")); - Node *concat_out2 = subgraph.at(pattern.GetPDNode("concat2_out")); - - Node *box_coder_third_input = subgraph.at(input_nodes[times]); - Node *box_coder_op = subgraph.at(pattern.GetPDNode("box_coder")); - Node *box_coder_out = subgraph.at(pattern.GetPDNode("box_coder_out")); - - Node *multiclass_nms_second_input = subgraph.at(input_nodes[times + 1]); - Node *transpose_before_nms = - subgraph.at(pattern.GetPDNode("transpose_before_nms")); - Node *transpose_before_nms_out = - subgraph.at(pattern.GetPDNode("transpose_before_nms_out")); - - Node *multiclass_nms = subgraph.at(pattern.GetPDNode("multiclass_nms")); - Node *multiclass_nms_out = - subgraph.at(pattern.GetPDNode("multiclass_nms_out")); - - std::string code_type = - boost::get(box_coder_op->Op()->GetAttr("code_type")); - bool box_normalized = - boost::get(box_coder_op->Op()->GetAttr("box_normalized")); - - int background_label = - boost::get(multiclass_nms->Op()->GetAttr("background_label")); - float score_threshold = - boost::get(multiclass_nms->Op()->GetAttr("score_threshold")); - int nms_top_k = boost::get(multiclass_nms->Op()->GetAttr("nms_top_k")); - float nms_threshold = - boost::get(multiclass_nms->Op()->GetAttr("nms_threshold")); - float nms_eta = boost::get(multiclass_nms->Op()->GetAttr("nms_eta")); - int keep_top_k = - boost::get(multiclass_nms->Op()->GetAttr("keep_top_k")); - - std::vector concat1_input_names; - for (int i = 0; i < times; i++) { - concat1_input_names.push_back( - nodes[i * kNumFields + kPriorBoxLocOffset]->Name()); - } - - framework::OpDesc concat1_desc; - concat1_desc.SetType("concat"); - concat1_desc.SetInput("X", concat1_input_names); - concat1_desc.SetAttr("axis", 2); - concat1_desc.SetOutput("Out", {concat_out1->Name()}); - - auto *new_add_concat_op = graph->CreateOpNode(&concat1_desc); - - for (int i = 0; i < times; i++) { - nodes[i * kNumFields + kPriorBoxLocOffset]->outputs.push_back( - new_add_concat_op); - new_add_concat_op->inputs.push_back( - nodes[i * kNumFields + kPriorBoxLocOffset]); - } - - framework::OpDesc new_op_desc; - new_op_desc.SetType("detection_out"); - new_op_desc.SetInput("PriorBox", {concat_out1->Name()}); - new_op_desc.SetInput("TargetBox", {box_coder_third_input->Name()}); - new_op_desc.SetInput("Scores", {multiclass_nms_second_input->Name()}); - new_op_desc.SetAttr("code_type", code_type); - new_op_desc.SetAttr("box_normalized", box_normalized); - new_op_desc.SetAttr("background_label", background_label); - new_op_desc.SetAttr("score_threshold", score_threshold); - new_op_desc.SetAttr("nms_top_k", nms_top_k); - new_op_desc.SetAttr("nms_threshold", nms_threshold); - new_op_desc.SetAttr("nms_eta", nms_eta); - new_op_desc.SetAttr("keep_top_k", keep_top_k); - new_op_desc.SetOutput("Out", {multiclass_nms_out->Name()}); - new_op_desc.Flush(); - - // Create a new node for the fused op. - auto *detection_out_op = graph->CreateOpNode(&new_op_desc); - - std::unordered_set delete_nodes; - - for (int i = 0; i < times; i++) { - nodes[i * kNumFields + kPriorBoxLocOffset]->outputs.push_back(concat_op1); - delete_nodes.insert(nodes[i * kNumFields + kReshape1Offset]); - delete_nodes.insert(nodes[i * kNumFields + kReshape1OutOffset]); - delete_nodes.insert(nodes[i * kNumFields + kPriorBoxVarOffset]); - delete_nodes.insert(nodes[i * kNumFields + kReshape2Offset]); - delete_nodes.insert(nodes[i * kNumFields + kReshape2OutOffset]); - } - - delete_nodes.insert(concat_op1); - delete_nodes.insert(concat_op2); - delete_nodes.insert(concat_out2); - delete_nodes.insert(box_coder_op); - delete_nodes.insert(box_coder_out); - delete_nodes.insert(transpose_before_nms); - delete_nodes.insert(transpose_before_nms_out); - delete_nodes.insert(multiclass_nms); - - new_add_concat_op->outputs.push_back(concat_out1); - concat_out1->inputs.push_back(new_add_concat_op); - - detection_out_op->inputs.push_back(concat_out1); - detection_out_op->inputs.push_back(box_coder_third_input); - detection_out_op->inputs.push_back(multiclass_nms_second_input); - detection_out_op->outputs.push_back(multiclass_nms_out); - - concat_out1->outputs.push_back(detection_out_op); - box_coder_third_input->outputs.push_back(detection_out_op); - multiclass_nms_second_input->outputs.push_back(detection_out_op); - multiclass_nms_out->inputs.push_back(detection_out_op); - - // Delete the unneeded nodes. - GraphSafeRemoveNodes(graph, delete_nodes); - }; - - gpd(graph, handler); -} - -void SimplifyAnakinDetectionPatternPass::ApplyImpl(ir::Graph *graph) const { - const int pattern_nums = 6; - const std::string pattern_name = "simplify_anakin_detection_pattern_pass"; - FusePassBase::Init(pattern_name, graph); - std::vector options = {true, false}; - for (const auto &is_density : options) { - for (const auto &is_reshape : options) { - for (int i = 1; i <= pattern_nums; i++) { - RunSimplifyAnakinDetection(graph, i, is_density, is_reshape); - } - } - } -} - -} // namespace ir -} // namespace framework -} // namespace paddle - -typedef paddle::framework::ir::SimplifyAnakinDetectionPatternPass - priorbox_pattern; -REGISTER_PASS(simplify_anakin_priorbox_detection_out_pass, priorbox_pattern); diff --git a/paddle/fluid/framework/ir/simplify_anakin_priorbox_detection_out_pass.h b/paddle/fluid/framework/ir/simplify_anakin_priorbox_detection_out_pass.h deleted file mode 100644 index e882b9dc252e61a2e9e4e3666de49b7eee6d714a..0000000000000000000000000000000000000000 --- a/paddle/fluid/framework/ir/simplify_anakin_priorbox_detection_out_pass.h +++ /dev/null @@ -1,39 +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 -#include -#include "paddle/fluid/framework/ir/fuse_pass_base.h" -#include "paddle/fluid/framework/ir/graph_pattern_detector.h" - -namespace paddle { -namespace framework { -namespace ir { - -// There may be many transpose-flatten structures in a model, and the output of -// these structures will be used as inputs to the concat Op. This pattern will -// be detected by our pass. The times here represents the repeat times of this -// structure. -class SimplifyAnakinDetectionPatternPass : public FusePassBase { - public: - virtual ~SimplifyAnakinDetectionPatternPass() {} - - protected: - void ApplyImpl(ir::Graph* graph) const override; -}; - -} // namespace ir -} // namespace framework -} // namespace paddle diff --git a/paddle/fluid/framework/ir/subgraph_detector.h b/paddle/fluid/framework/ir/subgraph_detector.h index d329198eeab704a75a586d231bc77bd6033e1ec4..3d279e27e6ae97978e46f7282f58d4a4ca1efdde 100644 --- a/paddle/fluid/framework/ir/subgraph_detector.h +++ b/paddle/fluid/framework/ir/subgraph_detector.h @@ -65,7 +65,7 @@ class SubGraphFuser { using NodeInsideSubgraphTeller = SubgraphDetector::NodeInsideSubgraphTeller; SubGraphFuser(Graph *graph, const NodeInsideSubgraphTeller &teller, - int min_subgraph_size, std::string name = "anakin_engine") + int min_subgraph_size, std::string name = "tensorrt_engine") : graph_(graph), node_inside_subgraph_teller_(teller), min_subgraph_size_{min_subgraph_size}, diff --git a/paddle/fluid/inference/CMakeLists.txt b/paddle/fluid/inference/CMakeLists.txt index c97320f81b7bd36691015714afbdf8a1a3103491..e557df123c69f91cc7bad45a2a38e36ac7183e56 100644 --- a/paddle/fluid/inference/CMakeLists.txt +++ b/paddle/fluid/inference/CMakeLists.txt @@ -30,10 +30,6 @@ if (TENSORRT_FOUND) add_subdirectory(tensorrt) endif() -if (ANAKIN_SUBGRAPH) - add_subdirectory(anakin) -endif() - if (WITH_LITE) add_subdirectory(lite) endif() @@ -68,9 +64,6 @@ if(NOT APPLE) set_target_properties(paddle_fluid PROPERTIES LINK_FLAGS "${LINK_FLAGS}") endif() -if(ANAKIN_FOUND) - set(ANAKIN_SHARED_INFERENCE_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/api/api_anakin_engine.cc) -endif() set(SHARED_INFERENCE_SRCS io.cc ${CMAKE_CURRENT_SOURCE_DIR}/../framework/data_feed.cc @@ -80,8 +73,7 @@ set(SHARED_INFERENCE_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/api/api_impl.cc ${CMAKE_CURRENT_SOURCE_DIR}/api/analysis_predictor.cc ${CMAKE_CURRENT_SOURCE_DIR}/api/details/zero_copy_tensor.cc - ${mkldnn_quantizer_src_file} - ${ANAKIN_SHARED_INFERENCE_SRCS}) + ${mkldnn_quantizer_src_file}) # Create shared inference library defaultly cc_library(paddle_fluid_shared SHARED SRCS ${SHARED_INFERENCE_SRCS} diff --git a/paddle/fluid/inference/anakin/CMakeLists.txt b/paddle/fluid/inference/anakin/CMakeLists.txt deleted file mode 100644 index 8292af2225bbfd7ba8262bb1e56783c83d287e50..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -cc_library(anakin_engine SRCS engine.cc DEPS framework_proto boost) -cc_library(anakin_op_teller SRCS op_teller.cc DEPS framework_proto device_context boost) -target_link_libraries(anakin_engine anakin anakin_saber_common) -cc_test(test_anakin_engine SRCS test_anakin_engine.cc DEPS anakin_engine) -add_subdirectory(convert) diff --git a/paddle/fluid/inference/anakin/convert/CMakeLists.txt b/paddle/fluid/inference/anakin/convert/CMakeLists.txt deleted file mode 100644 index 5c9fb50c280c02011bd7c1bda405e6828cff19bd..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/CMakeLists.txt +++ /dev/null @@ -1,23 +0,0 @@ -cc_library(anakin_op_converter SRCS fc.cc conv2d.cc conv2d_fusion.cc -elementwise.cc activation.cc pool2d.cc concat.cc split.cc relu.cc softmax.cc -batch_norm.cc reshape.cc flatten.cc transpose.cc density_prior_box.cc -detection_out.cc scale.cc dropout.cc im2sequence.cc sum.cc affine_channel.cc -roi_align.cc shuffle_channel.cc helper.cc DEPS anakin_engine framework_proto -scope op_registry gtest gflags) - -cc_test(test_anakin_fc SRCS test_fc_op.cc DEPS anakin_op_converter mul_op) -cc_test(test_anakin_conv2d SRCS test_conv2d_op.cc DEPS anakin_op_converter conv_op im2col vol2col depthwise_conv) -cc_test(test_anakin_activation SRCS test_activation_op.cc DEPS activation_op anakin_op_converter) -cc_test(test_anakin_pool2d SRCS test_pool2d_op.cc DEPS anakin_op_converter pool_op pooling) -cc_test(test_anakin_concat SRCS test_concat_op.cc DEPS anakin_op_converter concat_op concat_and_split) -cc_test(test_anakin_split SRCS test_split_op.cc DEPS anakin_op_converter split_op concat_and_split) -cc_test(test_anakin_elementwise SRCS test_elementwise_op.cc DEPS anakin_op_converter elementwise_add_op elementwise_mul_op) -cc_test(test_anakin_relu SRCS test_relu_op.cc DEPS activation_op anakin_op_converter) -cc_test(test_anakin_softmax SRCS test_softmax_op.cc DEPS anakin_op_converter softmax_op softmax) -cc_test(test_anakin_reshape SRCS test_reshape_op.cc DEPS anakin_op_converter reshape_op) -cc_test(test_anakin_flatten SRCS test_flatten_op.cc DEPS anakin_op_converter flatten_op reshape_op) -cc_test(test_anakin_transpose SRCS test_transpose_op.cc DEPS anakin_op_converter transpose_op) -cc_test(test_anakin_batch_norm SRCS test_batch_norm_op.cc DEPS anakin_op_converter batch_norm_op) -cc_test(test_anakin_dropout SRCS test_dropout_op.cc DEPS anakin_op_converter dropout_op) -cc_test(test_anakin_sum SRCS test_sum_op.cc DEPS anakin_op_converter sum_op selected_rows_functor) -cc_test(test_anakin_affine_channel SRCS test_affine_channel_op.cc DEPS anakin_op_converter affine_channel_op) diff --git a/paddle/fluid/inference/anakin/convert/activation.cc b/paddle/fluid/inference/anakin/convert/activation.cc deleted file mode 100644 index 523571f1aa8b5a9d17f97a1fd765fe9f1ac95b22..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/activation.cc +++ /dev/null @@ -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. - -#include "paddle/fluid/inference/anakin/convert/activation.h" -#include -#include - -namespace paddle { -namespace inference { -namespace anakin { - -template -ActivationOpConverter::ActivationOpConverter( - const std::string &op_type) - : op_type_(op_type) { - auto it = anakin_op_types_.find(op_type_); - PADDLE_ENFORCE(it != anakin_op_types_.end(), - "activation op type is not support"); - anakin_op_type_ = it->second; -} - -template -void ActivationOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); - - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - auto input_name = op_desc.Input("X").front(); - auto output_name = op_desc.Output("Out").front(); - this->engine_->AddOp(op_name, "Activation", {input_name}, {output_name}); - this->engine_->AddOpAttr(op_name, "type", anakin_op_type_); - - if (op_type_ == "swish") { - float beta = boost::get(op_desc.GetAttr("beta")); - this->engine_->AddOpAttr(op_name, "clip_relu_num", beta); - } - if (op_type_ == "relu6") { - float threshold = boost::get(op_desc.GetAttr("threshold")); - this->engine_->AddOpAttr(op_name, "clip_relu_num", threshold); - } -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(sigmoid, SigmoidOpConverter); -REGISTER_ANAKIN_OP_CONVERTER(tanh, TanhOpConverter); -REGISTER_ANAKIN_OP_CONVERTER(swish, SwishOpConverter); -REGISTER_ANAKIN_OP_CONVERTER(relu6, Relu6OpConverter); diff --git a/paddle/fluid/inference/anakin/convert/activation.h b/paddle/fluid/inference/anakin/convert/activation.h deleted file mode 100644 index a2475e492c408008fd7b22815a03aedfd3d04650..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/activation.h +++ /dev/null @@ -1,72 +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 -#include -#include "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class ActivationOpConverter : public AnakinOpConverter { - public: - explicit ActivationOpConverter(const std::string &op_type); - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~ActivationOpConverter() {} - - private: - std::string op_type_; - std::string anakin_op_type_; - std::map anakin_op_types_{{"tanh", "TanH"}, - {"sigmoid", "Sigmoid"}, - {"relu6", "ClippedRelu"}, - {"swish", "Swish"}}; -}; - -template -class TanhOpConverter : public ActivationOpConverter { - public: - TanhOpConverter() : ActivationOpConverter("tanh") {} -}; - -template -class SigmoidOpConverter : public ActivationOpConverter { - public: - SigmoidOpConverter() - : ActivationOpConverter("sigmoid") {} -}; - -template -class Relu6OpConverter : public ActivationOpConverter { - public: - Relu6OpConverter() : ActivationOpConverter("relu6") {} -}; - -template -class SwishOpConverter : public ActivationOpConverter { - public: - SwishOpConverter() : ActivationOpConverter("swish") {} -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/affine_channel.cc b/paddle/fluid/inference/anakin/convert/affine_channel.cc deleted file mode 100644 index 534e7dca81db959115283d65018ec33cc7a0924c..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/affine_channel.cc +++ /dev/null @@ -1,55 +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 "paddle/fluid/inference/anakin/convert/affine_channel.h" -#include -#include -#include -#include "paddle/fluid/inference/anakin/convert/helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void AffineChannelOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); - - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - auto input_name = op_desc.Input("X").front(); - auto output_name = op_desc.Output("Out").front(); - this->engine_->AddOp(op_name, "AffineChannel", {input_name}, {output_name}); - - // Copy the Scale to CPUPlace and get the pointer. - auto *scale_v = scope.FindVar(op_desc.Input("Scale").front()); - PADDLE_ENFORCE_NOT_NULL(scale_v); - auto weight1 = pblock_from_var(*scale_v, this->engine_); - this->engine_->AddOpAttr(op_name, "weight_1", *weight1); - - // Copy the Bias to CPUPlace and get the pointer. - auto *bias_v = scope.FindVar(op_desc.Input("Bias").front()); - PADDLE_ENFORCE_NOT_NULL(bias_v); - auto weight2 = pblock_from_var(*bias_v, this->engine_); - this->engine_->AddOpAttr(op_name, "weight_2", *weight2); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(affine_channel, AffineChannelOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/affine_channel.h b/paddle/fluid/inference/anakin/convert/affine_channel.h deleted file mode 100644 index 443f6101288af4ef6b82a9370f83b7b0c07e23c5..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/affine_channel.h +++ /dev/null @@ -1,40 +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 -#include "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class AffineChannelOpConverter : public AnakinOpConverter { - public: - AffineChannelOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~AffineChannelOpConverter() {} - - private: -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/batch_norm.cc b/paddle/fluid/inference/anakin/convert/batch_norm.cc deleted file mode 100644 index b41f5dc925208d38ae233f0b3d1ca425537b9b47..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/batch_norm.cc +++ /dev/null @@ -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. - -#include "paddle/fluid/inference/anakin/convert/batch_norm.h" -#include -#include -#include -#include -#include -#include "paddle/fluid/inference/anakin/convert/helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void BatchNormOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Output("Y").size(), 1); - std::map inputs; - for (auto k : {"X", "Scale", "Bias", "Mean", "Variance"}) { - PADDLE_ENFORCE_EQ(op_desc.Input(k).size(), 1UL); - } - - auto input = op_desc.Input("X").front(); - auto output = op_desc.Output("Y").front(); - auto op_name = op_desc.Type() + ":" + op_desc.Output("Y").front(); - auto epsilon = boost::get(op_desc.GetAttr("epsilon")); - - auto bn_op_name = op_name + ":bn"; - auto bn_output = bn_op_name + "_output"; - this->engine_->AddOp(bn_op_name, "BatchNorm", {input}, {bn_output}); - this->engine_->AddOpAttr(bn_op_name, "epsilon", epsilon); - this->engine_->AddOpAttr(bn_op_name, "momentum", static_cast(1.0)); - - auto scale_op_name = op_name + ":scale"; - this->engine_->AddOp(scale_op_name, "Scale", {bn_output}, {output}); - this->engine_->AddOpAttr(scale_op_name, "axis", 1); - this->engine_->AddOpAttr(scale_op_name, "num_axes", 1); - this->engine_->AddOpAttr(scale_op_name, "bias_term", true); - - auto *mean_v = scope.FindVar(op_desc.Input("Mean").front()); - PADDLE_ENFORCE_NOT_NULL(mean_v); - auto weight1 = pblock_from_var(*mean_v, this->engine_); - this->engine_->AddOpAttr(bn_op_name, "weight_1", *weight1); - - auto *variance_v = scope.FindVar(op_desc.Input("Variance").front()); - PADDLE_ENFORCE_NOT_NULL(variance_v); - auto weight2 = - pblock_from_var(*variance_v, this->engine_); - this->engine_->AddOpAttr(bn_op_name, "weight_2", *weight2); - - auto *weight3 = pblock_from_vector( - std::vector({1}), this->engine_); - this->engine_->AddOpAttr(bn_op_name, "weight_3", *weight3); - - auto *scale_v = scope.FindVar(op_desc.Input("Scale").front()); - PADDLE_ENFORCE_NOT_NULL(scale_v); - auto scale = pblock_from_var(*scale_v, this->engine_); - this->engine_->AddOpAttr(scale_op_name, "weight_1", *scale); - - auto *bias_v = scope.FindVar(op_desc.Input("Bias").front()); - PADDLE_ENFORCE_NOT_NULL(bias_v); - auto bias = pblock_from_var(*bias_v, this->engine_); - this->engine_->AddOpAttr(scale_op_name, "weight_2", *bias); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(batch_norm, BatchNormOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/batch_norm.h b/paddle/fluid/inference/anakin/convert/batch_norm.h deleted file mode 100644 index 52156aeb0283af9419c77490bbaded5bb5f45f4b..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/batch_norm.h +++ /dev/null @@ -1,37 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class BatchNormOpConverter : public AnakinOpConverter { - public: - BatchNormOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~BatchNormOpConverter() {} -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/concat.cc b/paddle/fluid/inference/anakin/convert/concat.cc deleted file mode 100644 index 584a82ead43fa75f0117cf524151bbd75cf54ba6..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/concat.cc +++ /dev/null @@ -1,41 +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 "paddle/fluid/inference/anakin/convert/concat.h" -#include - -namespace paddle { -namespace inference { -namespace anakin { - -template -void ConcatOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - int axis = boost::get(op_desc.GetAttr("axis")); - auto input_names = op_desc.Input("X"); - - auto y_name = op_desc.Output("Out").front(); - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - - this->engine_->AddOp(op_name, "Concat", input_names, {y_name}); - this->engine_->AddOpAttr(op_name, "axis", axis); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(concat, ConcatOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/concat.h b/paddle/fluid/inference/anakin/convert/concat.h deleted file mode 100644 index fb5514affa78d254476faf41bd09e21f41d2090d..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/concat.h +++ /dev/null @@ -1,39 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class ConcatOpConverter : public AnakinOpConverter { - public: - ConcatOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~ConcatOpConverter() {} - - private: -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/conv2d.cc b/paddle/fluid/inference/anakin/convert/conv2d.cc deleted file mode 100644 index 7904d407c36b4c66f675c5dd9fd62f8fe91e1908..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/conv2d.cc +++ /dev/null @@ -1,109 +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 "paddle/fluid/inference/anakin/convert/conv2d.h" -#include -#include -#include -#include "paddle/fluid/inference/anakin/convert/helper.h" - -using anakin::PTuple; - -namespace paddle { -namespace inference { -namespace anakin { - -template -void Conv2dOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("Input").size(), 1UL); - PADDLE_ENFORCE_EQ(op_desc.Input("Filter").size(), 1UL); - PADDLE_ENFORCE_EQ(op_desc.Output("Output").size(), 1UL); - - auto input_name = op_desc.Input("Input").front(); - auto output_name = op_desc.Output("Output").front(); - auto op_name = op_desc.Type() + ":" + op_desc.Output("Output").front(); - this->engine_->AddOp(op_name, "Convolution", {input_name}, {output_name}); - - auto *filter_v = scope.FindVar(op_desc.Input("Filter").front()); - PADDLE_ENFORCE_NOT_NULL(filter_v); - auto weight_tensor = tensor_from_var(*filter_v, platform::CPUPlace()); - auto weight_shape = framework::vectorize(weight_tensor->dims()); - - PADDLE_ENFORCE_EQ(weight_tensor->dims().size(), 4UL); - - const int filter_h = weight_tensor->dims()[2]; - const int filter_w = weight_tensor->dims()[3]; - - auto filter_num = weight_tensor->dims()[0]; - this->engine_->template AddOpAttr(op_name, "filter_num", filter_num); - this->engine_->template AddOpAttr>(op_name, "kernel_size", - {filter_h, filter_w}); - auto strides = boost::get>(op_desc.GetAttr("strides")); - this->engine_->template AddOpAttr>(op_name, "strides", strides); - auto paddings = boost::get>(op_desc.GetAttr("paddings")); - this->engine_->template AddOpAttr>(op_name, "padding", paddings); - auto dilations = boost::get>(op_desc.GetAttr("dilations")); - this->engine_->template AddOpAttr>(op_name, "dilation_rate", - dilations); - const int groups = boost::get(op_desc.GetAttr("groups")); - this->engine_->AddOpAttr(op_name, "group", groups); - this->engine_->AddOpAttr(op_name, "axis", 1); - this->engine_->AddOpAttr(op_name, "bias_term", false); - - ::anakin::saber::Shape anakin_shape(weight_shape); - bool enable_int8 = boost::get(op_desc.HasAttr("enable_int8")); - - if (enable_int8) { - const float int8_range = 127.; - float in_scale = boost::get(op_desc.GetAttr("input_scale")); - auto weight_scale = - boost::get>(op_desc.GetAttr("weight_scale")); - PBlock *weight1 = - new PBlock(anakin_shape, ::anakin::AK_INT8); - this->engine_->RegistBlock(weight1); - float *weight_data = weight_tensor->data(); - std::vector weight_int8; - int weight_num = weight_tensor->numel(); - for (int i = 0; i < weight_tensor->numel(); i++) { - bool is_valid_int8 = - ((weight_data[i] >= -128) && (weight_data[i] <= 127)); - PADDLE_ENFORCE(is_valid_int8, - "We are in anakin subgraph int8 mode, the weight of conv " - "should be in range [-128, 127]"); - weight_int8.push_back(static_cast(weight_data[i])); - } - memcpy(static_cast(weight1->h_tensor().mutable_data()), - static_cast(weight_int8.data()), sizeof(char) * weight_num); - weight1->d_tensor().set_shape(anakin_shape); - weight1->d_tensor().copy_from(weight1->h_tensor()); - this->engine_->AddOpAttr(op_name, "weight_1", *weight1); - this->engine_->Graph()->SetOpPrec(op_name, ::anakin::AK_INT8); - this->engine_->Graph()->SetWeightsScale( - op_name, {weight_scale[0] / int8_range}, false); - this->engine_->AddTensorScale(input_name, in_scale / int8_range); - } else { - auto *weight1 = pblock_from_tensor( - *weight_tensor, weight_shape, this->engine_); - this->engine_->AddOpAttr(op_name, "weight_1", *weight1); - } -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(conv2d, Conv2dOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/conv2d.h b/paddle/fluid/inference/anakin/convert/conv2d.h deleted file mode 100644 index b22cb8ea9318cf75deacc681e3c1e7b271d1f86b..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/conv2d.h +++ /dev/null @@ -1,37 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class Conv2dOpConverter : public AnakinOpConverter { - public: - Conv2dOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~Conv2dOpConverter() {} -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/conv2d_fusion.cc b/paddle/fluid/inference/anakin/convert/conv2d_fusion.cc deleted file mode 100644 index 4d7a502dee341b44236749813bb982be8a003af7..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/conv2d_fusion.cc +++ /dev/null @@ -1,115 +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 "paddle/fluid/inference/anakin/convert/conv2d_fusion.h" -#include -#include -#include -#include "paddle/fluid/inference/anakin/convert/helper.h" - -using anakin::PTuple; - -namespace paddle { -namespace inference { -namespace anakin { - -template -void Conv2dFusionOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("Input").size(), 1UL); - PADDLE_ENFORCE_EQ(op_desc.Input("Filter").size(), 1UL); - PADDLE_ENFORCE_EQ(op_desc.Input("Bias").size(), 1UL); - PADDLE_ENFORCE_EQ(op_desc.Output("Output").size(), 1UL); - - auto input_name = op_desc.Input("Input").front(); - auto output_name = op_desc.Output("Output").front(); - auto op_name = op_desc.Type() + ":" + op_desc.Output("Output").front(); - this->engine_->AddOp(op_name, "Convolution", {input_name}, {output_name}); - - auto *filter_v = scope.FindVar(op_desc.Input("Filter").front()); - PADDLE_ENFORCE_NOT_NULL(filter_v); - - auto weight_tensor = tensor_from_var(*filter_v, platform::CPUPlace()); - auto weight_shape = framework::vectorize(weight_tensor->dims()); - - auto *b_v = scope.FindVar(op_desc.Input("Bias").front()); - PADDLE_ENFORCE_NOT_NULL(b_v); - - PADDLE_ENFORCE_EQ(weight_tensor->dims().size(), 4UL); - const int filter_h = weight_tensor->dims()[2]; - const int filter_w = weight_tensor->dims()[3]; - auto filter_num = weight_tensor->dims()[0]; - this->engine_->template AddOpAttr(op_name, "filter_num", filter_num); - this->engine_->template AddOpAttr>(op_name, "kernel_size", - {filter_h, filter_w}); - auto strides = boost::get>(op_desc.GetAttr("strides")); - this->engine_->template AddOpAttr>(op_name, "strides", strides); - auto paddings = boost::get>(op_desc.GetAttr("paddings")); - this->engine_->template AddOpAttr>(op_name, "padding", paddings); - auto dilations = boost::get>(op_desc.GetAttr("dilations")); - this->engine_->template AddOpAttr>(op_name, "dilation_rate", - dilations); - const int groups = boost::get(op_desc.GetAttr("groups")); - this->engine_->AddOpAttr(op_name, "group", groups); - this->engine_->AddOpAttr(op_name, "axis", 1); - this->engine_->AddOpAttr(op_name, "bias_term", true); - - ::anakin::saber::Shape anakin_shape(weight_shape); - bool enable_int8 = boost::get(op_desc.HasAttr("enable_int8")); - if (enable_int8) { - const float int8_range = 127.; - float in_scale = boost::get(op_desc.GetAttr("input_scale")); - auto weight_scale = - boost::get>(op_desc.GetAttr("weight_scale")); - PBlock *weight1 = - new PBlock(anakin_shape, ::anakin::AK_INT8); - this->engine_->RegistBlock(weight1); - float *weight_data = weight_tensor->data(); - std::vector weight_int8; - int weight_num = weight_tensor->numel(); - for (int i = 0; i < weight_tensor->numel(); i++) { - bool is_valid_int8 = - ((weight_data[i] >= -128) && (weight_data[i] <= 127)); - PADDLE_ENFORCE(is_valid_int8, - "We are in anakin subgraph int8 mode, the weight of conv " - "should be in range [-128, 127]"); - weight_int8.push_back(static_cast(weight_data[i])); - } - memcpy(static_cast(weight1->h_tensor().mutable_data()), - static_cast(weight_int8.data()), sizeof(char) * weight_num); - weight1->d_tensor().set_shape(anakin_shape); - weight1->d_tensor().copy_from(weight1->h_tensor()); - this->engine_->AddOpAttr(op_name, "weight_1", *weight1); - this->engine_->Graph()->SetOpPrec(op_name, ::anakin::AK_INT8); - this->engine_->Graph()->SetWeightsScale( - op_name, {weight_scale[0] / int8_range}, false); - this->engine_->AddTensorScale(input_name, in_scale / int8_range); - } else { - auto weight_tensor = tensor_from_var(*filter_v, platform::CPUPlace()); - auto weight_shape = framework::vectorize(weight_tensor->dims()); - auto *weight1 = pblock_from_tensor( - *weight_tensor, weight_shape, this->engine_); - this->engine_->AddOpAttr(op_name, "weight_1", *weight1); - auto weight2 = pblock_from_var(*b_v, this->engine_); - this->engine_->AddOpAttr(op_name, "weight_2", *weight2); - } -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(conv2d_fusion, Conv2dFusionOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/conv2d_fusion.h b/paddle/fluid/inference/anakin/convert/conv2d_fusion.h deleted file mode 100644 index 768814d3f996dd5c7224d5aebcbed9d430439ab5..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/conv2d_fusion.h +++ /dev/null @@ -1,37 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class Conv2dFusionOpConverter : public AnakinOpConverter { - public: - Conv2dFusionOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~Conv2dFusionOpConverter() {} -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/density_prior_box.cc b/paddle/fluid/inference/anakin/convert/density_prior_box.cc deleted file mode 100644 index 5bbaeb57a7da46adfaa47fb696b4b73c8e33c7f0..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/density_prior_box.cc +++ /dev/null @@ -1,112 +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 "paddle/fluid/inference/anakin/convert/density_prior_box.h" -#include -#include -#include - -using anakin::PTuple; - -namespace paddle { -namespace inference { -namespace anakin { - -template -void DensityPriorBoxOpConverter::operator()( - const framework::proto::OpDesc& op, const framework::BlockDesc& block_desc, - const framework::Scope& scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - auto input_name = op_desc.Input("Input").front(); - auto image_name = op_desc.Input("Image").front(); - auto output_name = op_desc.Output("Boxes").front(); - auto op_type = op_desc.Type(); - auto op_name = op_type + ":" + op_desc.Output("Boxes").front(); - - // only for density_prior_box - std::vector fixed_sizes = {}; - std::vector fixed_ratios = {}; - std::vector densities = {}; - - std::vector min_sizes = {}; - std::vector max_sizes = {}; - std::vector aspect_ratios = {}; - bool is_clip = false; - bool is_flip = false; - - if (op_type == "density_prior_box") { - fixed_sizes = - boost::get>(op_desc.GetAttr("fixed_sizes")); - fixed_ratios = - boost::get>(op_desc.GetAttr("fixed_ratios")); - densities = boost::get>(op_desc.GetAttr("densities")); - is_clip = boost::get(op_desc.GetAttr("clip")); - } else if (op_type == "prior_box") { - min_sizes = boost::get>(op_desc.GetAttr("min_sizes")); - max_sizes = boost::get>(op_desc.GetAttr("max_sizes")); - aspect_ratios = - boost::get>(op_desc.GetAttr("aspect_ratios")); - is_clip = boost::get(op_desc.GetAttr("clip")); - is_flip = boost::get(op_desc.GetAttr("flip")); - } - std::vector dens; - for (auto& ele : densities) { - dens.push_back(static_cast(ele)); - } - - auto variances = boost::get>(op_desc.GetAttr("variances")); - - // lack img_h, img_w - auto step_h = boost::get(op_desc.GetAttr("step_h")); - auto step_w = boost::get(op_desc.GetAttr("step_w")); - auto offset = boost::get(op_desc.GetAttr("offset")); - PTuple t_order; - t_order.push_back("MIN"); - t_order.push_back("COM"); - t_order.push_back("MAX"); - - std::vector temp_v = {}; - - this->engine_->AddOp(op_name, "PriorBox", {input_name, image_name}, - {output_name}); - this->engine_->template AddOpAttr>(op_name, "min_size", - min_sizes); - this->engine_->template AddOpAttr>(op_name, "max_size", - max_sizes); - this->engine_->template AddOpAttr>(op_name, "aspect_ratio", - aspect_ratios); - this->engine_->template AddOpAttr>(op_name, "fixed_size", - fixed_sizes); - this->engine_->template AddOpAttr>(op_name, "fixed_ratio", - fixed_ratios); - this->engine_->template AddOpAttr>(op_name, "density", dens); - this->engine_->AddOpAttr(op_name, "is_flip", is_flip); - this->engine_->AddOpAttr(op_name, "is_clip", is_clip); - this->engine_->template AddOpAttr>(op_name, "variance", - variances); - this->engine_->AddOpAttr(op_name, "img_h", static_cast(0)); - this->engine_->AddOpAttr(op_name, "img_w", static_cast(0)); - this->engine_->AddOpAttr(op_name, "step_h", step_h); - this->engine_->AddOpAttr(op_name, "step_w", step_w); - this->engine_->AddOpAttr(op_name, "offset", offset); - this->engine_->template AddOpAttr>(op_name, "order", - t_order); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(density_prior_box, DensityPriorBoxOpConverter); -REGISTER_ANAKIN_OP_CONVERTER(prior_box, DensityPriorBoxOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/density_prior_box.h b/paddle/fluid/inference/anakin/convert/density_prior_box.h deleted file mode 100644 index 5714f57a04b7b34581f4deb5cbdd2eb4318ba72c..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/density_prior_box.h +++ /dev/null @@ -1,40 +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 -#include -#include "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class DensityPriorBoxOpConverter - : public AnakinOpConverter { - public: - DensityPriorBoxOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~DensityPriorBoxOpConverter() {} -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/detection_out.cc b/paddle/fluid/inference/anakin/convert/detection_out.cc deleted file mode 100644 index 73dd6f2832541ecda119702f1779363e2950e413..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/detection_out.cc +++ /dev/null @@ -1,69 +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 "paddle/fluid/inference/anakin/convert/detection_out.h" -#include -#include - -namespace paddle { -namespace inference { -namespace anakin { - -template -void DetectionOutOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - auto target_name = op_desc.Input("TargetBox").front(); - auto prior_box_name = op_desc.Input("PriorBox").front(); - auto scores_name = op_desc.Input("Scores").front(); - auto output_name = op_desc.Output("Out").front(); - - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - - auto code_type = boost::get(op_desc.GetAttr("code_type")); - auto background_label = boost::get(op_desc.GetAttr("background_label")); - auto score_threshold = boost::get(op_desc.GetAttr("score_threshold")); - auto nms_top_k = boost::get(op_desc.GetAttr("nms_top_k")); - auto nms_threshold = boost::get(op_desc.GetAttr("nms_threshold")); - auto nms_eta = boost::get(op_desc.GetAttr("nms_eta")); - auto keep_top_k = boost::get(op_desc.GetAttr("keep_top_k")); - std::string anakin_code_type; - if (code_type == "decode_center_size") { - anakin_code_type = "CENTER_SIZE"; - } else if (code_type == "encode_center_size") { - PADDLE_THROW( - "Not support encode_center_size code_type in DetectionOut of anakin"); - } - - this->engine_->AddOp(op_name, "DetectionOutput", - {target_name, scores_name, prior_box_name}, - {output_name}); - this->engine_->AddOpAttr(op_name, "share_location", true); - this->engine_->AddOpAttr(op_name, "variance_encode_in_target", false); - this->engine_->AddOpAttr(op_name, "class_num", static_cast(0)); - this->engine_->AddOpAttr(op_name, "background_id", background_label); - this->engine_->AddOpAttr(op_name, "keep_top_k", keep_top_k); - this->engine_->AddOpAttr(op_name, "code_type", anakin_code_type); - this->engine_->AddOpAttr(op_name, "conf_thresh", score_threshold); - this->engine_->AddOpAttr(op_name, "nms_top_k", nms_top_k); - this->engine_->AddOpAttr(op_name, "nms_thresh", nms_threshold); - this->engine_->AddOpAttr(op_name, "nms_eta", nms_eta); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(detection_out, DetectionOutOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/detection_out.h b/paddle/fluid/inference/anakin/convert/detection_out.h deleted file mode 100644 index c34342a66c1c6c42585c4cb92d64ed3964f7f427..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/detection_out.h +++ /dev/null @@ -1,39 +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 -#include -#include "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class DetectionOutOpConverter : public AnakinOpConverter { - public: - DetectionOutOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~DetectionOutOpConverter() {} -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/dropout.cc b/paddle/fluid/inference/anakin/convert/dropout.cc deleted file mode 100644 index 6c5f80b5f8e07fd501348632ff3b4cda58de248c..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/dropout.cc +++ /dev/null @@ -1,55 +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 "paddle/fluid/inference/anakin/convert/dropout.h" -#include -#include -#include -#include "paddle/fluid/inference/anakin/convert/helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void DropoutOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Output("Mask").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); - - auto x_name = op_desc.Input("X").front(); - auto out_name = op_desc.Output("Out").front(); - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - - this->engine_->AddOp(op_name, "Scale", {x_name}, {out_name}); - - auto dropout_prob = boost::get(op_desc.GetAttr("dropout_prob")); - auto factor = 1 - dropout_prob; - auto *weight1 = pblock_from_vector( - std::vector({factor}), this->engine_); - - this->engine_->AddOpAttr(op_name, "weight_1", *weight1); - this->engine_->AddOpAttr(op_name, "axis", 0); - this->engine_->AddOpAttr(op_name, "num_axes", 0); - this->engine_->AddOpAttr(op_name, "bias_term", false); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(dropout, DropoutOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/dropout.h b/paddle/fluid/inference/anakin/convert/dropout.h deleted file mode 100644 index 801aa3dd16f8504360c75e251896f6bd4718925b..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/dropout.h +++ /dev/null @@ -1,39 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class DropoutOpConverter : public AnakinOpConverter { - public: - DropoutOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~DropoutOpConverter() {} - - private: -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/elementwise.cc b/paddle/fluid/inference/anakin/convert/elementwise.cc deleted file mode 100644 index d221f26e11934d6851972d304c6f859346fe3b61..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/elementwise.cc +++ /dev/null @@ -1,75 +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 "paddle/fluid/inference/anakin/convert/elementwise.h" -#include -#include -#include - -using anakin::PTuple; - -namespace paddle { -namespace inference { -namespace anakin { - -template -void ElementwiseAddOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Input("Y").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); - - auto x_name = op_desc.Input("X").front(); - auto y_name = op_desc.Input("Y").front(); - auto out_name = op_desc.Output("Out").front(); - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - - this->engine_->AddOp(op_name, "Eltwise", {x_name, y_name}, {out_name}); - std::string elementwise_type = "Add"; - this->engine_->template AddOpAttr(op_name, "type", - elementwise_type); - std::vector coeff = {1.0, 1.0}; - this->engine_->template AddOpAttr>(op_name, "coeff", coeff); -} - -template -void ElementwiseMulOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Input("Y").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); - - auto x_name = op_desc.Input("X").front(); - auto y_name = op_desc.Input("Y").front(); - auto out_name = op_desc.Output("Out").front(); - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - - this->engine_->AddOp(op_name, "Eltwise", {x_name, y_name}, {out_name}); - std::string elementwise_type = "Mul"; - this->engine_->template AddOpAttr(op_name, "type", - elementwise_type); - std::vector coeff = {1.0, 1.0}; - this->engine_->template AddOpAttr>(op_name, "coeff", coeff); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(elementwise_add, ElementwiseAddOpConverter); -REGISTER_ANAKIN_OP_CONVERTER(elementwise_mul, ElementwiseMulOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/elementwise.h b/paddle/fluid/inference/anakin/convert/elementwise.h deleted file mode 100644 index 190a8b55f0e3c29e9e9c8b254d4b4df824c3330b..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/elementwise.h +++ /dev/null @@ -1,55 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class ElementwiseAddOpConverter - : public AnakinOpConverter { - public: - ElementwiseAddOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~ElementwiseAddOpConverter() {} - - private: -}; - -template -class ElementwiseMulOpConverter - : public AnakinOpConverter { - public: - ElementwiseMulOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~ElementwiseMulOpConverter() {} - - private: -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/fc.cc b/paddle/fluid/inference/anakin/convert/fc.cc deleted file mode 100644 index 265d318aaee20d14350da3fb32a02ea66373c7fc..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/fc.cc +++ /dev/null @@ -1,122 +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 "paddle/fluid/inference/anakin/convert/fc.h" -#include -#include -#include -#include "paddle/fluid/inference/anakin/convert/helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void FcBaseOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - auto input_names = op_desc.InputNames(); - bool with_bias = input_names.size() >= 3; - - std::string w_name = "Y"; - std::string i_name = "X"; - if (with_bias) { - w_name = "W"; - i_name = "Input"; - } - - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - - // get weights - auto *y_v = scope.FindVar(op_desc.Input(w_name).front()); - PADDLE_ENFORCE_NOT_NULL(y_v); - auto weight_tensor = tensor_from_var(*y_v, platform::CPUPlace()); - auto weight_shape = framework::vectorize(weight_tensor->dims()); - - int out_dim = weight_shape[1]; - const int w_m = weight_shape[0]; - const int w_k = weight_shape[1]; - - auto input_name = op_desc.Input(i_name).front(); - auto output_name = op_desc.Output("Out").front(); - - this->engine_->AddOp(op_name, "Dense", {input_name}, {output_name}); - this->engine_->AddOpAttr(op_name, "bias_term", with_bias); - this->engine_->AddOpAttr(op_name, "axis", 1); - this->engine_->AddOpAttr(op_name, "out_dim", out_dim); - - auto *weight_data = weight_tensor->data(); - PADDLE_ENFORCE(w_m * w_k == weight_tensor->numel()); - - std::vector trans_weight_data(weight_tensor->numel()); - for (int i = 0; i < w_m; i++) { - for (int j = 0; j < w_k; j++) { - trans_weight_data[i + j * w_m] = weight_data[i * w_k + j]; - } - } - - int weight_num = weight_tensor->numel(); - bool enable_int8 = boost::get(op_desc.HasAttr("enable_int8")); - if (enable_int8) { - if (weight_shape.size() < 4UL) { - weight_shape.insert(weight_shape.begin(), 4UL - weight_shape.size(), 1); - } - ::anakin::saber::Shape anakin_shape(weight_shape); - const float int8_range = 127.; - float in_scale = boost::get(op_desc.GetAttr("input_scale")); - auto weight_scale = - boost::get>(op_desc.GetAttr("weight_scale")); - PBlock *weight1 = - new PBlock(anakin_shape, ::anakin::AK_INT8); - this->engine_->RegistBlock(weight1); - std::vector weight_int8; - for (int i = 0; i < weight_num; i++) { - bool is_valid_int8 = - ((trans_weight_data[i] >= -128) && (trans_weight_data[i] <= 127)); - PADDLE_ENFORCE(is_valid_int8, - "We are in anakin subgraph int8 mode, the weight of fc " - "should be in range [-128, 127]"); - weight_int8.push_back(static_cast(trans_weight_data[i])); - } - memcpy(static_cast(weight1->h_tensor().mutable_data()), - static_cast(weight_int8.data()), sizeof(char) * weight_num); - weight1->d_tensor().set_shape(anakin_shape); - weight1->d_tensor().copy_from(weight1->h_tensor()); - this->engine_->AddOpAttr(op_name, "weight_1", *weight1); - this->engine_->Graph()->SetOpPrec(op_name, ::anakin::AK_INT8); - this->engine_->Graph()->SetWeightsScale( - op_name, {weight_scale[0] / int8_range}, false); - this->engine_->AddTensorScale(input_name, in_scale / int8_range); - } else { - auto *weight1 = pblock_from_vector(trans_weight_data, - this->engine_); - this->engine_->AddOpAttr(op_name, "weight_1", *weight1); - } - - // get bias - if (with_bias) { - auto *b_v = scope.FindVar(op_desc.Input("Bias").front()); - PADDLE_ENFORCE_NOT_NULL(b_v); - auto weight2 = pblock_from_var(*b_v, this->engine_); - this->engine_->AddOpAttr(op_name, "weight_2", *weight2); - } -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(mul, MulOpConverter); -REGISTER_ANAKIN_OP_CONVERTER(fc, FcOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/fc.h b/paddle/fluid/inference/anakin/convert/fc.h deleted file mode 100644 index 6fe65e3ecd4ec42b6a1b5d874d0306cfba86c8b2..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/fc.h +++ /dev/null @@ -1,51 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class FcBaseOpConverter : public AnakinOpConverter { - public: - FcBaseOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~FcBaseOpConverter() {} -}; - -// with bias -template -class FcOpConverter : public FcBaseOpConverter { - public: - FcOpConverter() = default; -}; - -// without bias -template -class MulOpConverter : public FcBaseOpConverter { - public: - MulOpConverter() = default; -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/flatten.cc b/paddle/fluid/inference/anakin/convert/flatten.cc deleted file mode 100644 index 7ce519a4de36c950bef1b4e856452828398aa57e..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/flatten.cc +++ /dev/null @@ -1,48 +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 "paddle/fluid/inference/anakin/convert/flatten.h" -#include - -using anakin::PTuple; - -namespace paddle { -namespace inference { -namespace anakin { - -template -void FlattenOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1UL); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1UL); - - auto input = op_desc.Input("X").front(); - auto output = op_desc.Output("Out").front(); - int axis = boost::get(op_desc.GetAttr("axis")); - PADDLE_ENFORCE(axis == 1, - "the anakin flatten op converter now only support aixs == 1."); - - std::vector out_dims = {0, -1, 1, 1}; - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - this->engine_->AddOp(op_name, "Reshape", {input}, {output}); - this->engine_->template AddOpAttr>(op_name, "dims", out_dims); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(flatten, FlattenOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/flatten.h b/paddle/fluid/inference/anakin/convert/flatten.h deleted file mode 100644 index 6e5e059927d4d35cb28e383041a1c6ce1d59b282..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/flatten.h +++ /dev/null @@ -1,37 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class FlattenOpConverter : public AnakinOpConverter { - public: - FlattenOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~FlattenOpConverter() {} -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/helper.cc b/paddle/fluid/inference/anakin/convert/helper.cc deleted file mode 100644 index 7804619bf836d93beae5dba9b561da273936c381..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/helper.cc +++ /dev/null @@ -1,32 +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 "paddle/fluid/inference/anakin/convert/helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -std::unique_ptr tensor_from_var( - const framework::Variable& var, const platform::Place& place) { - auto& src = var.Get(); - std::unique_ptr dst(new framework::LoDTensor()); - dst->Resize(src.dims()); - TensorCopySync((src), place, dst.get()); - return dst; -} - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/helper.h b/paddle/fluid/inference/anakin/convert/helper.h deleted file mode 100644 index 6ba8fd6ceb5e7850263da224e07e08d88ec2992b..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/helper.h +++ /dev/null @@ -1,95 +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 -#include -#include -#include - -#include "paddle/fluid/framework/lod_tensor.h" -#include "paddle/fluid/framework/variable.h" -#include "paddle/fluid/inference/anakin/engine.h" - -#include "framework/core/net/net.h" -#include "framework/core/types.h" -#include "framework/graph/graph.h" -#include "framework/graph/graph_global_mem.h" -#include "saber/saber_types.h" - -using anakin::saber::Shape; -using anakin::AK_FLOAT; -using anakin::AK_INT8; -using anakin::PBlock; - -namespace paddle { -namespace inference { -namespace anakin { - -std::unique_ptr tensor_from_var( - const framework::Variable& var, const platform::Place& place); - -template -PBlock* pblock_from_tensor(const framework::LoDTensor& tensor, - std::vector shape_vec, - AnakinEngine* engine) { - while (shape_vec.size() < 4) { - shape_vec.insert(shape_vec.begin(), 1); - } - Shape shape(shape_vec); - PBlock* weight = new PBlock(shape, AK_FLOAT); - engine->RegistBlock(weight); - float* cpu_data = static_cast(weight->h_tensor().mutable_data()); - std::copy_n(tensor.data(), tensor.numel(), cpu_data); - weight->d_tensor().set_shape(shape); - weight->d_tensor().copy_from(weight->h_tensor()); - return weight; -} - -template -PBlock* pblock_from_vector(const std::vector& vec, - std::vector shape_vec, - AnakinEngine* engine) { - while (shape_vec.size() < 4) { - shape_vec.insert(shape_vec.begin(), 1); - } - Shape shape(shape_vec); - PBlock* weight = new PBlock(shape, AK_FLOAT); - engine->RegistBlock(weight); - auto* weight_data = static_cast(weight->h_tensor().mutable_data()); - std::copy(std::begin(vec), std::end(vec), weight_data); - weight->d_tensor().set_shape(shape); - weight->d_tensor().copy_from(weight->h_tensor()); - return weight; -} - -template -PBlock* pblock_from_vector(const std::vector& vec, - AnakinEngine* engine) { - int size = vec.size(); - return pblock_from_vector( - vec, std::vector({1, 1, 1, size}), engine); -} - -template -PBlock* pblock_from_var(const framework::Variable& var, - AnakinEngine* engine) { - auto tensor = tensor_from_var(var, platform::CPUPlace()); - auto shape = framework::vectorize(tensor->dims()); - return pblock_from_tensor(*tensor, shape, engine); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/im2sequence.cc b/paddle/fluid/inference/anakin/convert/im2sequence.cc deleted file mode 100644 index 5a4e3e61c5e4e40d8fe98fba8e098d89d916dde1..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/im2sequence.cc +++ /dev/null @@ -1,58 +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 "paddle/fluid/inference/anakin/convert/im2sequence.h" -#include -#include -#include - -using anakin::PTuple; - -namespace paddle { -namespace inference { -namespace anakin { - -template -void Im2SequenceConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Output("Y").size(), 0); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); - - auto x_name = op_desc.Input("X").front(); - auto out_name = op_desc.Output("Out").front(); - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - - this->engine_->AddOp(op_name, "Im2Sequence", {x_name}, {out_name}); - - std::vector dilations = {1, 1}; - auto paddings = boost::get>(op_desc.GetAttr("paddings")); - auto strides = boost::get>(op_desc.GetAttr("strides")); - auto kernels = boost::get>(op_desc.GetAttr("kernels")); - - this->engine_->template AddOpAttr>(op_name, "paddings", paddings); - this->engine_->template AddOpAttr>(op_name, "strides", strides); - this->engine_->template AddOpAttr>(op_name, "window_size", - kernels); - this->engine_->template AddOpAttr>(op_name, "dilations", - dilations); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(im2sequence, Im2SequenceConverter); diff --git a/paddle/fluid/inference/anakin/convert/im2sequence.h b/paddle/fluid/inference/anakin/convert/im2sequence.h deleted file mode 100644 index 8241d4d6f9ce78f57753bb7300e2fe968da4a927..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/im2sequence.h +++ /dev/null @@ -1,39 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class Im2SequenceConverter : public AnakinOpConverter { - public: - Im2SequenceConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~Im2SequenceConverter() {} - - private: -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/op_converter.h b/paddle/fluid/inference/anakin/convert/op_converter.h deleted file mode 100644 index 9692f42779c8f23a4918761e859bb3e28f9a09e9..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/op_converter.h +++ /dev/null @@ -1,237 +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 -#include -#include -#include -#include -#include -#include "framework/core/types.h" -#include "paddle/fluid/framework/block_desc.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/framework/scope.h" -#include "paddle/fluid/inference/anakin/engine.h" -#include "paddle/fluid/inference/utils/singleton.h" -#include "saber/saber_types.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class AnakinOpConverter { - using AnakinEngineT = AnakinEngine; - - public: - AnakinOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) {} - void ConvertOp(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const std::unordered_set ¶meters, - const framework::Scope &scope, AnakinEngineT *engine, - bool test_mode = false) { - framework::OpDesc op_desc(op, nullptr); - std::string op_type = op_desc.Type(); - AnakinOpConverter *it = nullptr; - if (op_type == "depthwise_conv2d") op_type = "conv2d"; - if (op_type == "reshape2") op_type = "reshape"; - if (op_type == "transpose2") op_type = "transpose"; - if (op_type == "flatten2") op_type = "flatten"; - - if (!it) { - it = Registry::Global().Lookup(op_type); - } - PADDLE_ENFORCE_NOT_NULL(it, "no OpConverter for optype [%s]", op_type); - it->SetEngine(engine); - (*it)(op, block_desc, scope, test_mode); - } - - void ConvertBlock(framework::BlockDesc *block_desc, - const std::unordered_set ¶meters, - const framework::Scope &scope, AnakinEngineT *engine) { - std::unique_lock lock(mutex_); - framework::proto::BlockDesc *block = block_desc->Proto(); - for (auto i = 0; i < block->ops_size(); i++) { - auto &op = block->ops(i); - ConvertOp(op, *block_desc, parameters, scope, engine); - } - } - - // The scope here should be inited with the parameter vars. - void ConvertBlockToAnakinEngine( - framework::BlockDesc *block_desc, framework::Scope *scope, - const std::vector &inputs, - const std::unordered_set ¶meters, - const std::vector &outputs, AnakinEngineT *engine) { - ConvertBlock(block_desc, parameters, *scope, engine); - // if the max_batch size - int max_batch_size = engine->GetMaxBatchSize(); - PADDLE_ENFORCE(max_batch_size > 0, - "the max_batch_size setted from config->EnableAnakinEngine " - "must largger than 0"); - // If the user does not specify this variable, we use the input shape from - // the block_desc. - auto max_input_shape = engine->GetMaxInputShape(); - std::map> temp_max_input_shape; - // Register outputs with anakin using the RegistVar interface before Freeze. - // Note that RegistVar's parameters can only be outputs, not inputs. - for (auto &output : outputs) { - engine->Graph()->RegistVar(output); - } - engine->Freeze(); - // Add scale for tensor in int8 mode. - auto tensor_scales = engine->GetTensorScales(); - - for (auto &item : tensor_scales) { - engine->Graph()->SetVarScale(item.first, item.second); - } - - for (auto &input : inputs) { - if (parameters.count(input)) continue; - std::vector input_shape; - input_shape.resize(4); - input_shape[0] = max_batch_size; - if (max_input_shape.count(input)) { - PADDLE_ENFORCE(max_input_shape[input].size() == 4, - "the dimensions of max_input_shape setted from " - "config->EnableAnakinEngine must be 4"); - for (int i = 1; i < 4; i++) { - input_shape[i] = max_input_shape[input][i]; - } - } else { - auto *var = block_desc->FindVar(input); - PADDLE_ENFORCE(var, "no variable called %s", input); - - auto var_shape = var->GetShape(); - std::cout << "input :" << input << std::endl; - PADDLE_ENFORCE(var_shape.size() == 4); - - for (size_t i = 1; i < var_shape.size(); i++) { - input_shape[i] = var_shape[i]; - } - } - temp_max_input_shape[input] = input_shape; - engine->SetInputShape(input, input_shape); - } - engine->SetMaxInputShape(temp_max_input_shape); - engine->Optimize(); - engine->InitNet(); - } - - void SetEngine(AnakinEngineT *engine) { engine_ = engine; } - virtual ~AnakinOpConverter() {} - - protected: - bool test_mode_; - AnakinEngineT *engine_{nullptr}; - - private: - std::unordered_map *> - converters_; - framework::Scope *scope_{nullptr}; - std::mutex mutex_; -}; - -template class AnakinOpConverter<::anakin::saber::NV, - ::anakin::Precision::FP32>; -template class AnakinOpConverter<::anakin::saber::NV, - ::anakin::Precision::INT8>; -#ifdef ANAKIN_X86_PLACE -template class AnakinOpConverter<::anakin::saber::X86, - ::anakin::Precision::FP32>; -template class AnakinOpConverter<::anakin::saber::X86, - ::anakin::Precision::INT8>; -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle - -#define REGISTER_ANAKIN_OP_CONVERTER_BASE(op_type__, Converter__, \ - place_type__, place_class__, \ - precision_type__, precision_class__) \ - struct anakin_##op_type__##_##place_type__##_##precision_type__##_converter \ - : public ::paddle::framework::Registrar { \ - anakin_##op_type__##_##place_type__##_##precision_type__##_converter() { \ - LOG(INFO) << "register convert " << #op_type__ << " "; \ - ::paddle::inference::Registry< \ - ::paddle::inference::anakin::AnakinOpConverter< \ - place_class__, precision_class__>>::Global() \ - .Register(#op_type__); \ - } \ - }; \ - anakin_##op_type__##_##place_type__##_##precision_type__##_converter \ - anakin_##op_type__##_##place_type__##_##precision_type__##_converter__; \ - int Touch_anakin_##op_type__##_##place_type__##_##precision_type__() { \ - anakin_##op_type__##_##place_type__##_##precision_type__##_converter__ \ - .Touch(); \ - return 0; \ - } - -#define WRAP(...) __VA_ARGS__ - -#define REGISTER_CUDA_ANAKIN_OP_CONVERTER(op_type__, Converter__, \ - precision_type__) \ - REGISTER_ANAKIN_OP_CONVERTER_BASE( \ - op_type__, \ - ::paddle::inference::anakin::Converter__, \ - CUDA, ::anakin::saber::NV, precision_type__, \ - ::anakin::Precision::precision_type__) - -#define REGISTER_CPU_ANAKIN_OP_CONVERTER(op_type__, Converter__, \ - precision_type__) \ - REGISTER_ANAKIN_OP_CONVERTER_BASE( \ - op_type__, \ - ::paddle::inference::anakin::Converter__, \ - CPU, ::anakin::saber::X86, precision_type__, \ - ::anakin::Precision::precision_type__) - -#if defined(PADDLE_WITH_CUDA) && defined(ANAKIN_X86_PLACE) -#define REGISTER_ANAKIN_OP_CONVERTER(op_type__, Converter__) \ - REGISTER_CUDA_ANAKIN_OP_CONVERTER(op_type__, Converter__, FP32); \ - REGISTER_CUDA_ANAKIN_OP_CONVERTER(op_type__, Converter__, INT8); \ - REGISTER_CPU_ANAKIN_OP_CONVERTER(op_type__, Converter__, FP32); \ - REGISTER_CPU_ANAKIN_OP_CONVERTER(op_type__, Converter__, INT8) -#elif defined(PADDLE_WITH_CUDA) -#define REGISTER_ANAKIN_OP_CONVERTER(op_type__, Converter__) \ - REGISTER_CUDA_ANAKIN_OP_CONVERTER(op_type__, Converter__, FP32); \ - REGISTER_CUDA_ANAKIN_OP_CONVERTER(op_type__, Converter__, INT8) -#endif - -#define USE_ANAKIN_CONVERTER_BASE(op_type__, place_type__, precision_type__) \ - extern int Touch_anakin_##op_type__##_##place_type__##_##precision_type__(); \ - int use_converter_anakin_##op_type__##_##place_type__##_##precision_type__ \ - UNUSED = \ - Touch_anakin_##op_type__##_##place_type__##_##precision_type__(); - -#if defined(PADDLE_WITH_CUDA) && defined(ANAKIN_X86_PLACE) -#define USE_ANAKIN_CONVERTER(op_type__) \ - USE_ANAKIN_CONVERTER_BASE(op_type__, CUDA, FP32) \ - USE_ANAKIN_CONVERTER_BASE(op_type__, CPU, FP32) -#define USE_INT8_ANAKIN_CONVERTER(op_type__) \ - USE_ANAKIN_CONVERTER_BASE(op_type__, CUDA, INT8) \ - USE_ANAKIN_CONVERTER_BASE(op_type__, CPU, INT8) -#elif defined(PADDLE_WITH_CUDA) -#define USE_ANAKIN_CONVERTER(op_type__) \ - USE_ANAKIN_CONVERTER_BASE(op_type__, CUDA, FP32) -#define USE_INT8_ANAKIN_CONVERTER(op_type__) \ - USE_ANAKIN_CONVERTER_BASE(op_type__, CUDA, INT8) -#endif diff --git a/paddle/fluid/inference/anakin/convert/pool2d.cc b/paddle/fluid/inference/anakin/convert/pool2d.cc deleted file mode 100644 index 11e7c717fd689b27a619a33bcac4037b30f97af8..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/pool2d.cc +++ /dev/null @@ -1,74 +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 "paddle/fluid/inference/anakin/convert/pool2d.h" -#include -#include -#include - -using anakin::PTuple; - -namespace paddle { -namespace inference { -namespace anakin { - -template -void Pool2dOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); - - auto x_name = op_desc.Input("X").front(); - auto y_name = op_desc.Output("Out").front(); - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - - bool global_pooling = boost::get(op_desc.GetAttr("global_pooling")); - std::string pool_type = - boost::get(op_desc.GetAttr("pooling_type")); - std::vector ksize = - boost::get>(op_desc.GetAttr("ksize")); - std::vector strides = - boost::get>(op_desc.GetAttr("strides")); - std::vector paddings = - boost::get>(op_desc.GetAttr("paddings")); - bool ceil_mode = boost::get(op_desc.GetAttr("ceil_mode")); - std::string anakin_pool_type; - if (pool_type == "max") { - anakin_pool_type = "MAX"; - } else if (pool_type == "avg") { - if (paddings[0] || paddings[1]) { - anakin_pool_type = "AVGEXC"; - } else { - anakin_pool_type = "AVG"; - } - } else { - PADDLE_THROW("TensorRT unsupported pooling type!"); - } - - this->engine_->AddOp(op_name, "Pooling", {x_name}, {y_name}); - this->engine_->template AddOpAttr>(op_name, "pool_size", ksize); - this->engine_->template AddOpAttr>(op_name, "strides", strides); - this->engine_->template AddOpAttr>(op_name, "padding", paddings); - this->engine_->AddOpAttr(op_name, "method", anakin_pool_type); - this->engine_->AddOpAttr(op_name, "global_pooling", global_pooling); - this->engine_->AddOpAttr(op_name, "cmp_out_shape_floor_as_conv", !ceil_mode); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(pool2d, Pool2dOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/pool2d.h b/paddle/fluid/inference/anakin/convert/pool2d.h deleted file mode 100644 index 7a06ff1b660a4ccd28570337b67aff68e7bce6a7..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/pool2d.h +++ /dev/null @@ -1,39 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class Pool2dOpConverter : public AnakinOpConverter { - public: - Pool2dOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~Pool2dOpConverter() {} - - private: -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/relu.cc b/paddle/fluid/inference/anakin/convert/relu.cc deleted file mode 100644 index 00853406634bdf5c488d21aca8289826f3a93a16..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/relu.cc +++ /dev/null @@ -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 "paddle/fluid/inference/anakin/convert/relu.h" -#include -#include - -namespace paddle { -namespace inference { -namespace anakin { - -template -void ReluOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); - - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - auto input_name = op_desc.Input("X").front(); - auto output_name = op_desc.Output("Out").front(); - - this->engine_->AddOp(op_name, "ReLU", {input_name}, {output_name}); - this->engine_->AddOpAttr(op_name, "alpha", 0); -} - -template -void LeakyReluOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); - - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - auto input_name = op_desc.Input("X").front(); - auto output_name = op_desc.Output("Out").front(); - - float alpha = boost::get(op_desc.GetAttr("alpha")); - this->engine_->AddOp(op_name, "ReLU", {input_name}, {output_name}); - this->engine_->AddOpAttr(op_name, "alpha", alpha); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(relu, ReluOpConverter); -REGISTER_ANAKIN_OP_CONVERTER(leaky_relu, LeakyReluOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/relu.h b/paddle/fluid/inference/anakin/convert/relu.h deleted file mode 100644 index f366f05a94ae937c98c72e179a7bf14015a695ea..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/relu.h +++ /dev/null @@ -1,51 +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 -#include -#include "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class ReluOpConverter : public AnakinOpConverter { - public: - ReluOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~ReluOpConverter() {} -}; - -template -class LeakyReluOpConverter : public AnakinOpConverter { - public: - LeakyReluOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~LeakyReluOpConverter() {} -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/reshape.cc b/paddle/fluid/inference/anakin/convert/reshape.cc deleted file mode 100644 index d73736b7fecc758a2965f2d2afff9a808d6e2adc..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/reshape.cc +++ /dev/null @@ -1,49 +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 "paddle/fluid/inference/anakin/convert/reshape.h" -#include - -using anakin::PTuple; - -namespace paddle { -namespace inference { -namespace anakin { - -template -void ReshapeOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1UL); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1UL); - - auto input = op_desc.Input("X").front(); - auto output = op_desc.Output("Out").front(); - - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - this->engine_->AddOp(op_name, "Reshape", {input}, {output}); - - auto shape = boost::get>(op_desc.GetAttr("shape")); - if (shape.size() < 4) { - shape.insert(shape.end(), 4 - shape.size(), 1); - } - this->engine_->template AddOpAttr>(op_name, "dims", shape); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(reshape, ReshapeOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/reshape.h b/paddle/fluid/inference/anakin/convert/reshape.h deleted file mode 100644 index 88de2641e60f1a08cf11b1206be28eb516c575f1..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/reshape.h +++ /dev/null @@ -1,37 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class ReshapeOpConverter : public AnakinOpConverter { - public: - ReshapeOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~ReshapeOpConverter() {} -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/roi_align.cc b/paddle/fluid/inference/anakin/convert/roi_align.cc deleted file mode 100644 index 8702f638e10bbf72fa43d45e0042c16ffae447f1..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/roi_align.cc +++ /dev/null @@ -1,54 +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 "paddle/fluid/inference/anakin/convert/roi_align.h" -#include -#include - -namespace paddle { -namespace inference { -namespace anakin { - -template -void RoiAlignOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Input("ROIs").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); - - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - auto input_x_name = op_desc.Input("X").front(); - auto input_rois_name = op_desc.Input("ROIs").front(); - auto output_name = op_desc.Output("Out").front(); - - auto spatial_scale = boost::get(op_desc.GetAttr("spatial_scale")); - auto pooled_height = boost::get(op_desc.GetAttr("pooled_height")); - auto pooled_width = boost::get(op_desc.GetAttr("pooled_width")); - auto sampling_ratio = boost::get(op_desc.GetAttr("sampling_ratio")); - - this->engine_->AddOp(op_name, "RoiAlign", {input_x_name, input_rois_name}, - {output_name}); - this->engine_->AddOpAttr(op_name, "spatial_scale", spatial_scale); - this->engine_->AddOpAttr(op_name, "pooled_height", pooled_height); - this->engine_->AddOpAttr(op_name, "pooled_width", pooled_width); - this->engine_->AddOpAttr(op_name, "sampling_ratio", sampling_ratio); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(roi_align, RoiAlignOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/roi_align.h b/paddle/fluid/inference/anakin/convert/roi_align.h deleted file mode 100644 index 8b5d23a01676f035174aa6fd2d2a79582fc1e2e0..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/roi_align.h +++ /dev/null @@ -1,39 +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 -#include -#include "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class RoiAlignOpConverter : public AnakinOpConverter { - public: - RoiAlignOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~RoiAlignOpConverter() {} -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/scale.cc b/paddle/fluid/inference/anakin/convert/scale.cc deleted file mode 100644 index 2559ec498c8ba423bf894b1ec67e24bd2567ff2b..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/scale.cc +++ /dev/null @@ -1,52 +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 "paddle/fluid/inference/anakin/convert/scale.h" -#include -#include - -namespace paddle { -namespace inference { -namespace anakin { - -template -void ScaleOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); - - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - - auto input_name = op_desc.Input("X").front(); - auto output_name = op_desc.Output("Out").front(); - float scale = boost::get(op_desc.GetAttr("scale")); - float bias = boost::get(op_desc.GetAttr("bias")); - float bias_after_scale = - boost::get(op_desc.GetAttr("bias_after_scale")); - PADDLE_ENFORCE(bias_after_scale, - "The anakin scale layer only support bias after scale now."); - - this->engine_->AddOp(op_name, "Power", {input_name}, {output_name}); - this->engine_->AddOpAttr(op_name, "shift", bias); - this->engine_->AddOpAttr(op_name, "scale", scale); - this->engine_->AddOpAttr(op_name, "power", static_cast(1.0)); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(scale, ScaleOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/scale.h b/paddle/fluid/inference/anakin/convert/scale.h deleted file mode 100644 index f19a92019349718ccd961d2dc2968ad479ff1a3c..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/scale.h +++ /dev/null @@ -1,39 +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 -#include -#include "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class ScaleOpConverter : public AnakinOpConverter { - public: - ScaleOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~ScaleOpConverter() {} -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/shuffle_channel.cc b/paddle/fluid/inference/anakin/convert/shuffle_channel.cc deleted file mode 100644 index fdd2e3182e34992205d7707b83efbc3c6421076c..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/shuffle_channel.cc +++ /dev/null @@ -1,47 +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 "paddle/fluid/inference/anakin/convert/shuffle_channel.h" -#include -#include -#include - -using anakin::PTuple; - -namespace paddle { -namespace inference { -namespace anakin { - -template -void ShuffleChannelOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); - - auto input = op_desc.Input("X").front(); - auto output = op_desc.Output("Out").front(); - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - this->engine_->AddOp(op_name, "ShuffleChannel", {input}, {output}); - - auto group = boost::get(op_desc.GetAttr("group")); - this->engine_->AddOpAttr(op_name, "group", group); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(shuffle_channel, ShuffleChannelOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/shuffle_channel.h b/paddle/fluid/inference/anakin/convert/shuffle_channel.h deleted file mode 100644 index 457a14865a91bd6cfa763513f01cda72e34186e8..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/shuffle_channel.h +++ /dev/null @@ -1,38 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class ShuffleChannelOpConverter - : public AnakinOpConverter { - public: - ShuffleChannelOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~ShuffleChannelOpConverter() {} -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/softmax.cc b/paddle/fluid/inference/anakin/convert/softmax.cc deleted file mode 100644 index a4dc5a9156b8f54cf8915e2a8829ada22d442ace..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/softmax.cc +++ /dev/null @@ -1,47 +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 "paddle/fluid/inference/anakin/convert/softmax.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void SoftMaxOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1UL); - - auto input = op_desc.Input("X").front(); - auto output = op_desc.Output("Out").front(); - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - - auto input_var_desc = block_desc.FindVar(input); - PADDLE_ENFORCE(input_var_desc, - "Cant find %s variable When runing Anakin Softmax converter.", - input); - auto input_shape_in_fluid = input_var_desc->GetShape(); - size_t input_dims = input_shape_in_fluid.size(); - - this->engine_->AddOp(op_name, "Softmax", {input}, {output}); - this->engine_->AddOpAttr(op_name, "axis", static_cast(input_dims - 1)); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(softmax, SoftMaxOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/softmax.h b/paddle/fluid/inference/anakin/convert/softmax.h deleted file mode 100644 index dc431b5b867a2679050fa5b0128640678f36d210..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/softmax.h +++ /dev/null @@ -1,37 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class SoftMaxOpConverter : public AnakinOpConverter { - public: - SoftMaxOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~SoftMaxOpConverter() {} -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/split.cc b/paddle/fluid/inference/anakin/convert/split.cc deleted file mode 100644 index e63edea94ae010f3bd2240fd21147642f647581e..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/split.cc +++ /dev/null @@ -1,59 +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 "paddle/fluid/inference/anakin/convert/split.h" -#include -#include - -using anakin::PTuple; - -namespace paddle { -namespace inference { -namespace anakin { - -template -void SplitOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - auto input_name = op_desc.Input("X").front(); - auto y_names = op_desc.Output("Out"); - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - int axis = boost::get(op_desc.GetAttr("axis")); - - std::vector output_lengths = - boost::get>(op_desc.GetAttr("sections")); - - int split_num = output_lengths.size(); - PADDLE_ENFORCE(split_num > 1, - "anakin split op converter: the split num should > 1"); - int num_sum = 0; - std::vector slice_point; - for (int i = 0; i < split_num - 1; i++) { - num_sum += output_lengths[i]; - slice_point.push_back(num_sum); - } - this->engine_->AddOp(op_name, "Slice", {input_name}, y_names); - this->engine_->AddOpAttr(op_name, "axis", axis); - this->engine_->template AddOpAttr>(op_name, "slice_point", - slice_point); - // slice_dim is useless in anakin - this->engine_->AddOpAttr(op_name, "slice_dim", 4); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(split, SplitOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/split.h b/paddle/fluid/inference/anakin/convert/split.h deleted file mode 100644 index 819915315d90a500772756d1a21a5454694b9c0a..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/split.h +++ /dev/null @@ -1,39 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class SplitOpConverter : public AnakinOpConverter { - public: - SplitOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~SplitOpConverter() {} - - private: -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/sum.cc b/paddle/fluid/inference/anakin/convert/sum.cc deleted file mode 100644 index 870c07934090370a05ad5e8a2e68af8f314e25ae..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/sum.cc +++ /dev/null @@ -1,50 +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 "paddle/fluid/inference/anakin/convert/sum.h" -#include -#include -#include - -using anakin::PTuple; - -namespace paddle { -namespace inference { -namespace anakin { - -template -void SumOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 2); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); - - auto input_names = op_desc.Input("X"); - auto out_name = op_desc.Output("Out").front(); - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - - std::vector coeff = {1, 1}; - std::string elementwise_type = "Add"; - this->engine_->AddOp(op_name, "Eltwise", input_names, {out_name}); - this->engine_->template AddOpAttr>(op_name, "coeff", coeff); - this->engine_->template AddOpAttr(op_name, "type", - elementwise_type); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(sum, SumOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/sum.h b/paddle/fluid/inference/anakin/convert/sum.h deleted file mode 100644 index aefc64c623e916ee42604fed771d6985c4dcfd1d..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/sum.h +++ /dev/null @@ -1,39 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class SumOpConverter : public AnakinOpConverter { - public: - SumOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~SumOpConverter() {} - - private: -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/test_activation_op.cc b/paddle/fluid/inference/anakin/convert/test_activation_op.cc deleted file mode 100644 index 5ac8b45882f5175f90db6c5ddb2f41a67ca145e2..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_activation_op.cc +++ /dev/null @@ -1,92 +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 -#include "paddle/fluid/inference/anakin/convert/activation.h" -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -static void test_activation_op(const std::string& op_type, - const platform::DeviceContext& context, - bool use_gpu) { - std::unordered_set parameters; - framework::Scope scope; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - validator.DeclInputVar("act-X", {10, 6, 1, 1}); - validator.DeclOutputVar("act-Out", {10, 6, 1, 1}); - framework::OpDesc desc; - desc.SetType(op_type); - desc.SetInput("X", {"act-X"}); - desc.SetOutput("Out", {"act-Out"}); - - if (op_type == "swish") { - desc.SetAttr("beta", 1.0f); - } - - if (op_type == "relu6") { - desc.SetAttr("threshold", 6.0f); - } - - LOG(INFO) << "set OP"; - validator.SetOp(*desc.Proto()); - LOG(INFO) << "execute"; - - validator.Execute(5); -} - -#ifdef PADDLE_WITH_CUDA -TEST(sigm_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_activation_op<::anakin::saber::NV>("sigmoid", ctx, true); -} - -TEST(tanh_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_activation_op<::anakin::saber::NV>("tanh", ctx, true); -} - -TEST(relu6_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_activation_op<::anakin::saber::NV>("relu6", ctx, true); -} - -TEST(swish_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_activation_op<::anakin::saber::NV>("swish", ctx, true); -} -#endif - -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(sigmoid); -USE_OP(tanh); -USE_OP(relu6); -USE_OP(swish); - -USE_ANAKIN_CONVERTER(sigmoid); -USE_ANAKIN_CONVERTER(tanh); -USE_ANAKIN_CONVERTER(relu6); -USE_ANAKIN_CONVERTER(swish); diff --git a/paddle/fluid/inference/anakin/convert/test_affine_channel_op.cc b/paddle/fluid/inference/anakin/convert/test_affine_channel_op.cc deleted file mode 100644 index 008537dc8a5a82326f243e73fc33ce1dbeb730ef..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_affine_channel_op.cc +++ /dev/null @@ -1,72 +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 -#include "paddle/fluid/inference/anakin/convert/affine_channel.h" -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void test_affine_channel_op(const platform::DeviceContext& context, - bool use_gpu) { - // Declare the difference between the inputs. - std::unordered_set parameters({"scale", "bias"}); - - framework::Scope scope; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - validator.DeclInputVar("x", {1, 3, 5, 2}); - validator.DeclOutputVar("out", {1, 3, 5, 2}); - validator.DeclParamVar("scale", {3}); - validator.DeclParamVar("bias", {3}); - - // Prepare Op descriptions. - framework::OpDesc desc; - desc.SetType("affine_channel"); - desc.SetInput("X", {"x"}); - desc.SetInput("Bias", {"bias"}); - desc.SetInput("Scale", {"scale"}); - desc.SetOutput("Out", {"out"}); - - // Layout must be explicitly specified here as NCHW. - desc.SetAttr("data_layout", std::string("NCHW")); - - validator.SetOp(*desc.Proto()); - validator.Execute(1); -} - -#ifdef PADDLE_WITH_CUDA -TEST(affine_channel_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_affine_channel_op<::anakin::saber::NV>(ctx, true); -} -#endif -#ifdef ANAKIN_X86_PLACE -TEST(affine_channel_op, cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_affine_channel_op<::anakin::saber::X86>(ctx, false); -} -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(affine_channel); -USE_ANAKIN_CONVERTER(affine_channel); diff --git a/paddle/fluid/inference/anakin/convert/test_batch_norm_op.cc b/paddle/fluid/inference/anakin/convert/test_batch_norm_op.cc deleted file mode 100644 index edba90235fac023a1c9712f308b535da9ba39e3a..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_batch_norm_op.cc +++ /dev/null @@ -1,87 +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 -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void test_batchnorm_op(const platform::DeviceContext& context, bool use_gpu) { - std::unordered_set parameters( - {"batch_norm_scale", "batch_norm_bias", "batch_norm_mean", - "batch_norm_variance"}); - framework::Scope scope; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - std::vector param_shape{2}; - - validator.DeclInputVar("batch_norm_X", {1, 2, 5, 5}); - validator.DeclParamVar("batch_norm_scale", param_shape); - validator.DeclParamVar("batch_norm_bias", param_shape); - validator.DeclParamVar("batch_norm_mean", param_shape); - validator.DeclParamVar("batch_norm_variance", param_shape); - validator.DeclOutputVar("batch_norm_Y", {1, 2, 5, 5}); - validator.DeclOutputVar("batch_norm_save_mean", param_shape); - validator.DeclOutputVar("batch_norm_save_variance", param_shape); - - // Prepare Op description - framework::OpDesc desc; - - desc.SetType("batch_norm"); - desc.SetInput("X", {"batch_norm_X"}); - desc.SetInput("Scale", {"batch_norm_scale"}); - desc.SetInput("Bias", {"batch_norm_bias"}); - desc.SetInput("Mean", {"batch_norm_mean"}); - desc.SetInput("Variance", {"batch_norm_variance"}); - desc.SetOutput("Y", {"batch_norm_Y"}); - desc.SetOutput("MeanOut", {"batch_norm_mean"}); - desc.SetOutput("VarianceOut", {"batch_norm_variance"}); - desc.SetOutput("SavedMean", {"batch_norm_save_mean"}); - desc.SetOutput("SavedVariance", {"batch_norm_save_variance"}); - - float eps = 1e-5f; - bool is_test = true; - desc.SetAttr("epsilon", eps); - desc.SetAttr("is_test", is_test); - - validator.SetOp(*desc.Proto()); - - std::unordered_set neglected_output = { - "batch_norm_save_mean", "batch_norm_save_variance", "batch_norm_mean", - "batch_norm_variance"}; - validator.Execute(1, neglected_output); -} - -#ifdef PADDLE_WITH_CUDA -TEST(batch_norm_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_batchnorm_op<::anakin::saber::NV>(ctx, true); -} -#endif -#ifdef ANAKIN_X86_PLACE -TEST(batch_norm_op, cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_batchnorm_op<::anakin::saber::X86>(ctx, false); -} -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle -USE_OP(batch_norm); -USE_ANAKIN_CONVERTER(batch_norm); diff --git a/paddle/fluid/inference/anakin/convert/test_concat_op.cc b/paddle/fluid/inference/anakin/convert/test_concat_op.cc deleted file mode 100644 index 6870260c865873874072e25721edafeba8dab234..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_concat_op.cc +++ /dev/null @@ -1,67 +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 -#include "paddle/fluid/inference/anakin/convert/concat.h" -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void test_concat_op(const platform::DeviceContext& context, bool use_gpu) { - std::unordered_set parameters({""}); - framework::Scope scope; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - validator.DeclInputVar("concat_x1", {1, 2, 1, 1}); - validator.DeclInputVar("concat_x2", {1, 3, 1, 1}); - validator.DeclInputVar("concat_x3", {1, 1, 1, 1}); - validator.DeclOutputVar("concat_out", {1, 6, 1, 1}); - - // Prepare Op description - framework::OpDesc desc; - desc.SetType("concat"); - desc.SetInput("X", {"concat_x1", "concat_x2", "concat_x3"}); - desc.SetOutput("Out", {"concat_out"}); - - int axis = 1; - desc.SetAttr("axis", axis); - - validator.SetOp(*desc.Proto()); - - validator.Execute(1); -} - -#ifdef PADDLE_WITH_CUDA -TEST(concat_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_concat_op<::anakin::saber::NV>(ctx, true); -} -#endif -#ifdef ANAKIN_X86_PLACE -TEST(concat_op, cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_concat_op<::anakin::saber::X86>(ctx, false); -} -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle -USE_OP(concat); -USE_ANAKIN_CONVERTER(concat); diff --git a/paddle/fluid/inference/anakin/convert/test_conv2d_op.cc b/paddle/fluid/inference/anakin/convert/test_conv2d_op.cc deleted file mode 100644 index 723a348b12e3b451b047514838a68e56238956a2..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_conv2d_op.cc +++ /dev/null @@ -1,75 +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 -#include "paddle/fluid/inference/anakin/convert/conv2d.h" -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void test_conv2d_op(const platform::DeviceContext& context, bool use_gpu) { - std::unordered_set parameters({"conv2d-Y"}); - framework::Scope scope; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - validator.DeclInputVar("conv2d-X", {1, 3, 3, 3}); - validator.DeclParamVar("conv2d-Y", {4, 3, 1, 1}); - validator.DeclOutputVar("conv2d-Out", {1, 4, 3, 3}); - - // Prepare Op description - framework::OpDesc desc; - desc.SetType("conv2d"); - desc.SetInput("Input", {"conv2d-X"}); - desc.SetInput("Filter", {"conv2d-Y"}); - desc.SetOutput("Output", {"conv2d-Out"}); - - const std::vector strides({1, 1}); - const std::vector paddings({0, 0}); - const std::vector dilations({1, 1}); - const int groups = 1; - - desc.SetAttr("strides", strides); - desc.SetAttr("paddings", paddings); - desc.SetAttr("dilations", dilations); - desc.SetAttr("groups", groups); - - validator.SetOp(*desc.Proto()); - - validator.Execute(3); -} - -#ifdef PADDLE_WITH_CUDA -TEST(conv2d_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_conv2d_op<::anakin::saber::NV>(ctx, true); -} -#endif -#ifdef ANAKIN_X86_PLACE -TEST(conv2d_op, cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_conv2d_op<::anakin::saber::X86>(ctx, false); -} -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(conv2d); -USE_ANAKIN_CONVERTER(conv2d); diff --git a/paddle/fluid/inference/anakin/convert/test_dropout_op.cc b/paddle/fluid/inference/anakin/convert/test_dropout_op.cc deleted file mode 100644 index 83792676a00440257d836c3fb50e7d685f5d110a..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_dropout_op.cc +++ /dev/null @@ -1,69 +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 -#include "paddle/fluid/inference/anakin/convert/dropout.h" -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void test_dropout_op(const platform::DeviceContext& context, bool use_gpu) { - std::unordered_set parameters; - framework::Scope scope; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - validator.DeclInputVar("x", {1, 1, 2, 2}); - validator.DeclOutputVar("out", {1, 1, 2, 2}); - validator.DeclOutputVar("mask", {1, 1, 2, 2}); - - // Prepare Op description - framework::OpDesc desc; - desc.SetType("dropout"); - desc.SetInput("X", {"x"}); - desc.SetOutput("Out", {"out"}); - desc.SetOutput("Mask", {"mask"}); - - float dropout_prob = 0.5; - desc.SetAttr("dropout_prob", dropout_prob); - desc.SetAttr("is_test", true); - - validator.SetOp(*desc.Proto()); - std::unordered_set neglected_output = {"mask"}; - validator.Execute(1, neglected_output); -} - -#ifdef PADDLE_WITH_CUDA -TEST(dropout_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_dropout_op<::anakin::saber::NV>(ctx, true); -} -#endif -#ifdef ANAKIN_X86_PLACE -TEST(dropout_op, cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_dropout_op<::anakin::saber::X86>(ctx, false); -} -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(dropout); -USE_ANAKIN_CONVERTER(dropout); diff --git a/paddle/fluid/inference/anakin/convert/test_elementwise_op.cc b/paddle/fluid/inference/anakin/convert/test_elementwise_op.cc deleted file mode 100644 index ee128c1ec9ad62998310e7faaef962fa251cca7f..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_elementwise_op.cc +++ /dev/null @@ -1,81 +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 -#include "paddle/fluid/inference/anakin/convert/elementwise.h" -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -static void test_elementwise_op(const std::string& op_type, - const platform::DeviceContext& context, - bool use_gpu) { - std::unordered_set parameters; - framework::Scope scope; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - validator.DeclInputVar("x", {1, 1, 2, 2}); - validator.DeclInputVar("y", {1, 1, 2, 2}); - validator.DeclOutputVar("out", {1, 1, 2, 2}); - - // Prepare Op description - framework::OpDesc desc; - desc.SetType(op_type); - desc.SetInput("X", {"x"}); - desc.SetInput("Y", {"y"}); - desc.SetOutput("Out", {"out"}); - - int axis = -1; - desc.SetAttr("axis", axis); - - validator.SetOp(*desc.Proto()); - validator.Execute(1); -} - -#ifdef PADDLE_WITH_CUDA -TEST(elementwise_op, native_add_gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_elementwise_op<::anakin::saber::NV>("elementwise_add", ctx, true); -} -TEST(elementwise_op, native_mul_gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_elementwise_op<::anakin::saber::NV>("elementwise_mul", ctx, true); -} -#endif -#ifdef ANAKIN_X86_PLACE -TEST(elementwise_op, native_add_cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_elementwise_op<::anakin::saber::X86>("elementwise_add", ctx, false); -} -TEST(elementwise_op, native_mul_cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_elementwise_op<::anakin::saber::X86>("elementwise_mul", ctx, false); -} -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(elementwise_add); -USE_OP(elementwise_mul); -USE_ANAKIN_CONVERTER(elementwise_add); -USE_ANAKIN_CONVERTER(elementwise_mul); diff --git a/paddle/fluid/inference/anakin/convert/test_fc_op.cc b/paddle/fluid/inference/anakin/convert/test_fc_op.cc deleted file mode 100644 index 3e68d8fed6a66423d5fc4c271445a41207417253..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_fc_op.cc +++ /dev/null @@ -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. */ - -#include -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void test_mul_op(const platform::DeviceContext& context, bool use_gpu) { - std::unordered_set parameters({"mul_y"}); - framework::Scope scope; - - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - validator.DeclInputVar("mul_x", {1, 1, 2, 2}); - validator.DeclParamVar("mul_y", {4, 2}); - validator.DeclOutputVar("mul_out", {1, 2}); - - // Prepare Op description - framework::OpDesc desc; - desc.SetType("mul"); - desc.SetInput("X", {"mul_x"}); - desc.SetInput("Y", {"mul_y"}); - desc.SetOutput("Out", {"mul_out"}); - validator.SetOp(*desc.Proto()); - - validator.Execute(10); -} - -#ifdef PADDLE_WITH_CUDA -TEST(mul_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_mul_op<::anakin::saber::NV>(ctx, true); -} -#endif -#ifdef ANAKIN_X86_PLACE -TEST(mul_op, cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_mul_op<::anakin::saber::X86>(ctx, false); -} -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(mul); -USE_ANAKIN_CONVERTER(fc); diff --git a/paddle/fluid/inference/anakin/convert/test_flatten_op.cc b/paddle/fluid/inference/anakin/convert/test_flatten_op.cc deleted file mode 100644 index 5e4cfdabfd7ca4dfc865ba3722030c5dbd44d036..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_flatten_op.cc +++ /dev/null @@ -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. */ - -#include -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void test_flatten_op(const platform::DeviceContext& context, bool use_gpu) { - std::unordered_set parameters; - framework::Scope scope; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - validator.DeclInputVar("flatten-X", {3, 10, 10, 4}); - validator.DeclOutputVar("flatten-Out", {3, 400, 1, 1}); - framework::OpDesc desc; - desc.SetType("flatten"); - desc.SetInput("X", {"flatten-X"}); - desc.SetOutput("Out", {"flatten-Out"}); - desc.SetAttr("axis", 1); - - LOG(INFO) << "set OP"; - validator.SetOp(*desc.Proto()); - LOG(INFO) << "execute"; - - validator.Execute(5); -} - -#ifdef PADDLE_WITH_CUDA -TEST(flatten_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_flatten_op<::anakin::saber::NV>(ctx, true); -} -#endif -#ifdef ANAKIN_X86_PLACE -TEST(flatten_op, cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_flatten_op<::anakin::saber::X86>(ctx, false); -} -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(reshape); -USE_OP_ITSELF(flatten); -USE_ANAKIN_CONVERTER(flatten); diff --git a/paddle/fluid/inference/anakin/convert/test_im2sequence_op.cc b/paddle/fluid/inference/anakin/convert/test_im2sequence_op.cc deleted file mode 100644 index 5e5764633125c867e27b0b52e0e6ef18714653b2..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_im2sequence_op.cc +++ /dev/null @@ -1,55 +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 -#include "paddle/fluid/inference/anakin/convert/im2sequence.h" -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -TEST(im2sequence_op, native) { - std::unordered_set parameters; - framework::Scope scope; - AnakinConvertValidation validator(parameters, &scope); - - std::vector kernels = {6, 1}; - std::vector strides = {1, 1}; - std::vector paddings = {0, 0, 0, 0}; - - validator.DeclInputVar("x", {1, 1, 2, 2}); - validator.DeclOutputVar("out", {1, 1 * kernels[0] * kernels[1]}); - - // Prepare Op description - framework::OpDesc desc; - desc.SetType("im2sequence"); - desc.SetInput("X", {"x"}); - desc.SetOutput("Out", {"out"}); - - desc.SetAttr("kernels", kernels); - desc.SetAttr("strides", strides); - desc.SetAttr("paddings", paddings); - - validator.SetOp(*desc.Proto()); - validator.Execute(1); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(im2sequence); -USE_ANAKIN_CONVERTER(im2sequence); diff --git a/paddle/fluid/inference/anakin/convert/test_pool2d_op.cc b/paddle/fluid/inference/anakin/convert/test_pool2d_op.cc deleted file mode 100644 index 9b23b5b93df16ae833fda891dc89c8dfe98cddcb..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_pool2d_op.cc +++ /dev/null @@ -1,119 +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 -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void test_pool2d(const platform::DeviceContext& context, bool use_gpu, - bool global_pooling, bool ceil_mode, - std::string pool_type = "max") { - framework::Scope scope; - std::unordered_set parameters; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - - // The ITensor's Dims should not contain the batch size. - // So, the ITensor's Dims of input and output should be C * H * W. - validator.DeclInputVar("pool2d_x", {1, 3, 6, 7}); - if (global_pooling) - validator.DeclOutputVar("pool2d_out", {1, 3, 1, 1}); - else if (ceil_mode) - validator.DeclOutputVar("pool2d_out", {1, 3, 3, 4}); - else - validator.DeclOutputVar("pool2d_out", {1, 3, 3, 3}); - - // Prepare Op description - framework::OpDesc desc; - desc.SetType("pool2d"); - desc.SetInput("X", {"pool2d_x"}); - desc.SetOutput("Out", {"pool2d_out"}); - - std::vector ksize({2, 2}); - std::vector strides({2, 2}); - std::vector paddings({0, 0}); - std::string pooling_t = pool_type; - - desc.SetAttr("pooling_type", pooling_t); - desc.SetAttr("ksize", ksize); - desc.SetAttr("strides", strides); - desc.SetAttr("paddings", paddings); - desc.SetAttr("global_pooling", global_pooling); - desc.SetAttr("ceil_mode", ceil_mode); - - LOG(INFO) << "set OP"; - validator.SetOp(*desc.Proto()); - LOG(INFO) << "execute"; - - validator.Execute(1); -} - -#ifdef PADDLE_WITH_CUDA -TEST(Pool2dOpConverter, normal) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_pool2d<::anakin::saber::NV>(ctx, true, false, false); -} -TEST(Pool2dOpConverter, test_global_pooling) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_pool2d<::anakin::saber::NV>(ctx, true, true, false); -} - -TEST(Pool2dOpConverter, max_ceil_test) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_pool2d<::anakin::saber::NV>(ctx, true, false, true); -} - -TEST(Pool2dOpConverter, avg_ceil_test) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_pool2d<::anakin::saber::NV>(ctx, true, false, true, "avg"); -} -#endif -#ifdef ANAKIN_X86_PLACE -TEST(Pool2dOpConverter, normal_cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_pool2d<::anakin::saber::X86>(ctx, false, false, false); -} -TEST(Pool2dOpConverter, test_global_pooling_cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_pool2d<::anakin::saber::X86>(ctx, false, true, false); -} - -TEST(Pool2dOpConverter, max_ceil_test_cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_pool2d<::anakin::saber::X86>(ctx, false, false, true); -} - -TEST(Pool2dOpConverter, avg_ceil_test_cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_pool2d<::anakin::saber::X86>(ctx, false, false, true, "avg"); -} -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(pool2d); -USE_ANAKIN_CONVERTER(pool2d); diff --git a/paddle/fluid/inference/anakin/convert/test_relu_op.cc b/paddle/fluid/inference/anakin/convert/test_relu_op.cc deleted file mode 100644 index eb6429f3383d2848a8b512009ada78d578dab919..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_relu_op.cc +++ /dev/null @@ -1,70 +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 -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/relu.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -static void test_activation_op(const std::string& op_type, - const platform::DeviceContext& context, - bool use_gpu) { - std::unordered_set parameters; - framework::Scope scope; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - validator.DeclInputVar("act-X", {10, 6, 1, 1}); - validator.DeclOutputVar("act-Out", {10, 6, 1, 1}); - framework::OpDesc desc; - desc.SetType(op_type); - desc.SetInput("X", {"act-X"}); - desc.SetOutput("Out", {"act-Out"}); - if (op_type == "leaky_relu") { - desc.SetAttr("alpha", 0.1f); - } - - LOG(INFO) << "set OP"; - validator.SetOp(*desc.Proto()); - LOG(INFO) << "execute"; - - validator.Execute(5); -} - -#ifdef PADDLE_WITH_CUDA -TEST(relu_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_activation_op<::anakin::saber::NV>("relu", ctx, true); -} - -TEST(leaky_relu_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_activation_op<::anakin::saber::NV>("leaky_relu", ctx, true); -} -#endif - -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(relu); -USE_OP(leaky_relu); -USE_ANAKIN_CONVERTER(relu); -USE_ANAKIN_CONVERTER(leaky_relu); diff --git a/paddle/fluid/inference/anakin/convert/test_reshape_op.cc b/paddle/fluid/inference/anakin/convert/test_reshape_op.cc deleted file mode 100644 index b1be42e542ce06cb1ea88af8db71f2dfcec8123b..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_reshape_op.cc +++ /dev/null @@ -1,102 +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 -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void test_reshape1_op(const platform::DeviceContext& context, bool use_gpu) { - framework::Scope scope; - std::unordered_set parameters; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - - // validator.DeclInputVar("reshape-X", {2, 3, 3, 1}); - // validator.DeclOutputVar("reshape-Out", {3, 2, 1, 3}); - validator.DeclInputVar("reshape-X", {1, 2, 4, 1}); - validator.DeclOutputVar("reshape-Out", {1, 8, 1, 1}); - - framework::OpDesc desc; - desc.SetType("reshape"); - desc.SetInput("X", {"reshape-X"}); - desc.SetOutput("Out", {"reshape-Out"}); - // desc.SetAttr("shape", std::vector({3, 2, 1, 3})); - desc.SetAttr("shape", std::vector({1, 8, 1, 1})); - - LOG(INFO) << "set OP"; - validator.SetOp(*desc.Proto()); - LOG(INFO) << "execute"; - validator.Execute(1); -} - -template -void test_reshape2_op(const platform::DeviceContext& context, bool use_gpu) { - framework::Scope scope; - std::unordered_set parameters; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - - validator.DeclInputVar("reshape-X", {1, 2, 4}); - validator.DeclOutputVar("reshape-Out", {1, 4, 2}); - - framework::OpDesc desc; - desc.SetType("reshape"); - desc.SetInput("X", {"reshape-X"}); - desc.SetOutput("Out", {"reshape-Out"}); - // desc.SetAttr("shape", std::vector({3, 2, 1, 3})); - desc.SetAttr("shape", std::vector({0, -1, 2})); - - LOG(INFO) << "set OP"; - validator.SetOp(*desc.Proto()); - LOG(INFO) << "execute"; - validator.Execute(1); -} - -#ifdef PADDLE_WITH_CUDA -TEST(reshape1_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_reshape1_op<::anakin::saber::NV>(ctx, true); -} - -TEST(reshape2_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_reshape2_op<::anakin::saber::NV>(ctx, true); -} -#endif -#ifdef ANAKIN_X86_PLACE -TEST(reshape1_op, cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_reshape2_op<::anakin::saber::X86>(ctx, false); -} - -TEST(reshape2_op, cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_reshape2_op<::anakin::saber::X86>(ctx, false); -} -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(reshape); -USE_ANAKIN_CONVERTER(reshape); diff --git a/paddle/fluid/inference/anakin/convert/test_softmax_op.cc b/paddle/fluid/inference/anakin/convert/test_softmax_op.cc deleted file mode 100644 index 1a324739d98534d3b5443cd5f2c2f57f7045543e..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_softmax_op.cc +++ /dev/null @@ -1,63 +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 -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void test_softmax_op(const platform::DeviceContext& context, bool use_gpu) { - framework::Scope scope; - std::unordered_set parameters; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - - validator.DeclInputVar("softmax-X", {1, 10, 2}); - validator.DeclOutputVar("softmax-Out", {1, 10, 2}); - - framework::OpDesc desc; - desc.SetType("softmax"); - desc.SetInput("X", {"softmax-X"}); - desc.SetOutput("Out", {"softmax-Out"}); - - LOG(INFO) << "set OP"; - validator.SetOp(*desc.Proto()); - LOG(INFO) << "execute"; - validator.Execute(1); -} - -#ifdef PADDLE_WITH_CUDA -TEST(softmax_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_softmax_op<::anakin::saber::NV>(ctx, true); -} -#endif -#ifdef ANAKIN_X86_PLACE -TEST(relu_op, cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_softmax_op<::anakin::saber::X86>(ctx, false); -} -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(softmax); -USE_ANAKIN_CONVERTER(softmax); diff --git a/paddle/fluid/inference/anakin/convert/test_split_op.cc b/paddle/fluid/inference/anakin/convert/test_split_op.cc deleted file mode 100644 index f9ef54fdcacecd7540becb5b8aff997d40c5872d..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_split_op.cc +++ /dev/null @@ -1,119 +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 -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/split.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void AnakinSliceTest(const platform::DeviceContext &context, bool use_gpu, - const std::vector &in_shape, - const std::vector §ions) { - std::unordered_set parameters({""}); - framework::Scope scope; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - - validator.DeclInputVar("split_input", in_shape); - std::vector output_vars; - for (size_t i = 0; i < sections.size(); ++i) { - auto out_shape = in_shape; - out_shape[Axis] = sections[i]; - std::string output_name = "split_out" + std::to_string(i); - validator.DeclOutputVar(output_name, out_shape); - output_vars.push_back(output_name); - } - - // Prepare Op description - framework::OpDesc desc; - desc.SetType("split"); - desc.SetInput("X", {"split_input"}); - desc.SetOutput("Out", output_vars); - - desc.SetAttr("axis", Axis); - desc.SetAttr("num", 0); - desc.SetAttr("sections", sections); - - validator.SetOp(*desc.Proto()); - - validator.Execute(1); -} - -// batch = 0, axis = 1, same shape -TEST(split_op, test_same_shape_axis1_batch1) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - AnakinSliceTest<::anakin::saber::NV, 1>(ctx, true, {1, 4, 2, 2}, {2, 2}); -} -// batch = 0, axis = 1, different shape -TEST(split_op, test_different_shape_axis1_batch1) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - AnakinSliceTest<::anakin::saber::NV, 1>(ctx, true, {1, 3, 2, 2}, {2, 1}); -} -// batch = 0, axis = 2, same shape -TEST(split_op, test_same_shape_axis2_batch1) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - AnakinSliceTest<::anakin::saber::NV, 2>(ctx, true, {1, 3, 4, 2}, {2, 2}); -} -// batch = 0, axis = 2, different shape -TEST(split_op, test_different_shape_axis2_batch1) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - AnakinSliceTest<::anakin::saber::NV, 2>(ctx, true, {1, 3, 3, 2}, {2, 1}); -} - -// batch = 0, axis = 3, same shape -TEST(split_op, test_same_shape_axis3_batch1) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - AnakinSliceTest<::anakin::saber::NV, 3>(ctx, true, {1, 3, 2, 4}, {2, 2}); -} -// batch = 0, axis = 3, different shape -TEST(split_op, test_different_shape_axis3_batch1) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - AnakinSliceTest<::anakin::saber::NV, 3>(ctx, true, {1, 3, 2, 3}, {2, 1}); -} -#ifdef ANAKIN_X86_PLACE -TEST(split_op, test_different_shape_axis1_batch1_cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - AnakinSliceTest<::anakin::saber::X86, 1>(ctx, false, {1, 3, 2, 3}, {2, 1}); -} - -TEST(split_op, test_different_shape_axis2_batch1_cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - AnakinSliceTest<::anakin::saber::X86, 2>(ctx, false, {1, 3, 4, 2}, {2, 2}); -} - -TEST(split_op, test_different_shape_axis3_batch1_cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - AnakinSliceTest<::anakin::saber::X86, 3>(ctx, false, {1, 3, 2, 4}, {2, 2}); -} -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(split); -USE_ANAKIN_CONVERTER(split); diff --git a/paddle/fluid/inference/anakin/convert/test_sum_op.cc b/paddle/fluid/inference/anakin/convert/test_sum_op.cc deleted file mode 100644 index 9d26430ea68c5b818b96365e36381a088c3725f6..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_sum_op.cc +++ /dev/null @@ -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. */ - -#include -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/sum.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" -#include "paddle/fluid/operators/sum_op.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -static void test_sum_op(const platform::DeviceContext& context, bool use_gpu) { - std::unordered_set parameters; - framework::Scope scope; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - validator.DeclInputVar("sum_x1", {1, 2, 1, 2}); - validator.DeclInputVar("sum_x2", {1, 2, 1, 2}); - validator.DeclOutputVar("sum_out", {1, 2, 1, 2}); - - // Prepare Op description - framework::OpDesc desc; - desc.SetType("sum"); - desc.SetInput("X", {"sum_x1", "sum_x2"}); - desc.SetOutput("Out", {"sum_out"}); - - validator.SetOp(*desc.Proto()); - validator.Execute(1); -} - -#ifdef PADDLE_WITH_CUDA -TEST(sum_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_sum_op<::anakin::saber::NV>(ctx, true); -} -#endif -#ifdef ANAKIN_X86_PLACE -TEST(sum_op, cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_sum_op<::anakin::saber::X86>(ctx, false); -} -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(sum); -USE_ANAKIN_CONVERTER(sum); diff --git a/paddle/fluid/inference/anakin/convert/test_transpose_op.cc b/paddle/fluid/inference/anakin/convert/test_transpose_op.cc deleted file mode 100644 index 466e2f1a49f21b30973553ae6cd7bd4f0864def3..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/test_transpose_op.cc +++ /dev/null @@ -1,100 +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 -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/convert/ut_helper.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -void test_transpose1_op(const platform::DeviceContext& context, bool use_gpu) { - std::unordered_set parameters; - framework::Scope scope; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - validator.DeclInputVar("transpose-X", {2, 3, 4, 5}); - validator.DeclOutputVar("transpose-Out", {4, 2, 5, 3}); - - // Prepare Op description - framework::OpDesc desc; - desc.SetType("transpose"); - desc.SetInput("X", {"transpose-X"}); - desc.SetOutput("Out", {"transpose-Out"}); - desc.SetAttr("axis", std::vector({2, 0, 3, 1})); - - LOG(INFO) << "set OP"; - validator.SetOp(*desc.Proto()); - LOG(INFO) << "execute"; - - validator.Execute(3); -} - -template -void test_transpose2_op(const platform::DeviceContext& context, bool use_gpu) { - std::unordered_set parameters; - framework::Scope scope; - AnakinConvertValidation validator( - parameters, &scope, context, use_gpu); - validator.DeclInputVar("transpose-X", {3, 4, 5}); - validator.DeclOutputVar("transpose-Out", {3, 5, 4}); - - // Prepare Op description - framework::OpDesc desc; - desc.SetType("transpose"); - desc.SetInput("X", {"transpose-X"}); - desc.SetOutput("Out", {"transpose-Out"}); - desc.SetAttr("axis", std::vector({0, 2, 1})); - - LOG(INFO) << "set OP"; - validator.SetOp(*desc.Proto()); - LOG(INFO) << "execute"; - - validator.Execute(1); -} - -#ifdef PADDLE_WITH_CUDA -TEST(transpose1_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_transpose1_op<::anakin::saber::NV>(ctx, true); -} - -TEST(transpose2_op, gpu) { - platform::CUDAPlace gpu_place(0); - platform::CUDADeviceContext ctx(gpu_place); - test_transpose2_op<::anakin::saber::NV>(ctx, true); -} -#endif -#ifdef ANAKIN_X86_PLACE -TEST(transpose1_op, cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_transpose2_op<::anakin::saber::X86>(ctx, false); -} - -TEST(transpose2_op, cpu) { - platform::CPUPlace cpu_place; - platform::CPUDeviceContext ctx(cpu_place); - test_transpose2_op<::anakin::saber::X86>(ctx, false); -} -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle - -USE_OP(transpose); -USE_ANAKIN_CONVERTER(transpose); diff --git a/paddle/fluid/inference/anakin/convert/transpose.cc b/paddle/fluid/inference/anakin/convert/transpose.cc deleted file mode 100644 index 28071ca8449cdd61799011308a992eacd51dfd38..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/transpose.cc +++ /dev/null @@ -1,52 +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 "paddle/fluid/inference/anakin/convert/transpose.h" -#include -#include -#include - -using anakin::PTuple; - -namespace paddle { -namespace inference { -namespace anakin { - -template -void TransposeOpConverter::operator()( - const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc, - const framework::Scope &scope, bool test_mode) { - framework::OpDesc op_desc(op, nullptr); - PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); - PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); - - auto input = op_desc.Input("X").front(); - auto output = op_desc.Output("Out").front(); - auto op_name = op_desc.Type() + ":" + op_desc.Output("Out").front(); - this->engine_->AddOp(op_name, "Permute", {input}, {output}); - - auto axis = boost::get>(op_desc.GetAttr("axis")); - size_t axis_size = axis.size(); - while (axis.size() < 4) { - axis.push_back(axis_size); - axis_size += 1; - } - this->engine_->template AddOpAttr>(op_name, "dims", axis); -} - -} // namespace anakin -} // namespace inference -} // namespace paddle - -REGISTER_ANAKIN_OP_CONVERTER(transpose, TransposeOpConverter); diff --git a/paddle/fluid/inference/anakin/convert/transpose.h b/paddle/fluid/inference/anakin/convert/transpose.h deleted file mode 100644 index b7b0a0f209e7d6402ad7d5e30d23cf0523d3cf90..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/transpose.h +++ /dev/null @@ -1,37 +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 "paddle/fluid/inference/anakin/convert/op_converter.h" - -namespace paddle { -namespace inference { -namespace anakin { - -template -class TransposeOpConverter : public AnakinOpConverter { - public: - TransposeOpConverter() = default; - - virtual void operator()(const framework::proto::OpDesc &op, - const framework::BlockDesc &block_desc, - const framework::Scope &scope, - bool test_mode) override; - virtual ~TransposeOpConverter() {} -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/convert/ut_helper.h b/paddle/fluid/inference/anakin/convert/ut_helper.h deleted file mode 100644 index ba8d0e3f6966bbc06973b02b0fa599e6b3fbc38e..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/convert/ut_helper.h +++ /dev/null @@ -1,228 +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 -#include -#include -#include -#include -#include -#include - -#include "paddle/fluid/framework/block_desc.h" -#include "paddle/fluid/framework/lod_tensor.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/framework/program_desc.h" -#include "paddle/fluid/framework/tensor_util.h" -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/engine.h" -#include "paddle/fluid/inference/analysis/helper.h" -#include "paddle/fluid/inference/utils/singleton.h" -#include "paddle/fluid/platform/enforce.h" - -using anakin::Precision; - -namespace paddle { -namespace inference { -namespace anakin { - -/* - * Get a random float value between [low, high] - */ -float random(float low, float high) { - static std::random_device rd; - static std::mt19937 mt(rd()); - std::uniform_real_distribution dist(low, high); - return dist(mt); -} - -void RandomizeTensor(framework::LoDTensor* tensor, - const platform::Place& place) { - auto dims = tensor->dims(); - size_t num_elements = analysis::AccuDims(dims, dims.size()); - PADDLE_ENFORCE_GT(num_elements, 0); - - platform::CPUPlace cpu_place; - framework::LoDTensor temp_tensor; - temp_tensor.Resize(dims); - auto* temp_data = temp_tensor.mutable_data(cpu_place); - - for (size_t i = 0; i < num_elements; i++) { - *(temp_data + i) = random(0., 1.); - } - - TensorCopySync(temp_tensor, place, tensor); -} - -/* - * Help to validate the correctness between Fluid Op and the corresponding - * anakin - * layer. - */ -template -class AnakinConvertValidation { - using AnakinNvEngineT = AnakinEngine; - - public: - AnakinConvertValidation() = delete; - - AnakinConvertValidation(const std::unordered_set& parameters, - framework::Scope* scope, - const platform::DeviceContext& ctx, - bool use_gpu = true) - : parameters_(parameters), scope_(scope), ctx_(ctx), use_gpu_(use_gpu) { - engine_.reset(new AnakinEngine(true)); - } - - // Declare a Variable as input with random initialization. - void DeclInputVar(const std::string& name, - const std::vector tensor_dims) { - DeclVar(name, tensor_dims); - // should decalre anakin input here. - } - - void DeclParamVar(const std::string& name, const std::vector dim_vec) { - DeclVar(name, dim_vec); - } - - void DeclOutputVar(const std::string& name, const std::vector dim_vec) { - DeclVar(name, dim_vec); - // should declare anakin output here. - } - - void DeclVar(const std::string& name, const std::vector dim_vec) { - auto* x = scope_->Var(name); - auto* x_tensor = x->GetMutable(); - x_tensor->Resize(framework::make_ddim(dim_vec)); - RandomizeTensor(x_tensor, ctx_.GetPlace()); - - std::vector dim_vec_int64; - for (auto& ele : dim_vec) { - dim_vec_int64.push_back(static_cast(ele)); - } - - // Add var_desc to block_desc - auto* block_desc = program_desc_.MutableBlock(framework::kRootBlockIndex); - - auto* var_desc = block_desc->Var(name); - var_desc->SetShape(dim_vec_int64); - } - - void SetOp(const framework::proto::OpDesc& desc) { - op_ = framework::OpRegistry::CreateOp(desc); - op_desc_.reset(new framework::OpDesc(desc, nullptr)); - // should init anakin engine here. - - auto& block_desc = program_desc_.Block(framework::kRootBlockIndex); - Singleton>::Global().ConvertOp( - desc, block_desc, parameters_, *scope_, engine_.get(), - true /*test_mode*/); - engine_->Freeze(); - - std::map> temp_max_input_shape; - for (const auto& input : op_desc_->InputArgumentNames()) { - if (parameters_.count(input)) continue; - auto& t = inference::analysis::GetFromScope(*scope_, - input); - auto t_shape = framework::vectorize(t.dims()); - while (t_shape.size() < 4) { - t_shape.push_back(1); - } - engine_->SetInputShape(input, t_shape); - temp_max_input_shape[input] = t_shape; - } - engine_->SetMaxInputShape(temp_max_input_shape); - engine_->Optimize(); - engine_->InitNet(); - } - - // We use the set 'neglected_output' here, because some Ops like batch norm, - // the outputs specified in the op des are only used during training, - // so we should neglect those output during inference. - void Execute(int batch_size, - std::unordered_set neglected_output = {}) { - // Execute Fluid Op - op_->Run(*scope_, ctx_.GetPlace()); - - std::map inputs; - for (const auto& input : op_desc_->InputArgumentNames()) { - if (parameters_.count(input)) continue; - auto* var = scope_->FindVar(input); - auto tensor = var->GetMutable(); - inputs.insert({input, tensor}); - } - - std::map outputs; - std::vector> fluid_outputs; - for (const auto& output : op_desc_->OutputArgumentNames()) { - if (neglected_output.count(output)) continue; - std::vector fluid_out; - auto* var = scope_->FindVar(output); - auto tensor = var->GetMutable(); - framework::TensorToVector(*tensor, ctx_, &fluid_out); - fluid_outputs.push_back(fluid_out); - - outputs.insert({output, tensor}); - } - - if (!use_gpu_) { - engine_->Execute(inputs, outputs); - } else { - cudaStream_t stream; - PADDLE_ENFORCE_EQ(cudaStreamCreate(&stream), 0); - engine_->Execute(inputs, outputs, stream); - } - - int i_output = 0; - for (const auto& output : op_desc_->OutputArgumentNames()) { - if (neglected_output.count(output)) continue; - std::vector anakin_out; - auto* var = scope_->FindVar(output); - auto tensor = var->GetMutable(); - framework::TensorToVector(*tensor, ctx_, &anakin_out); - - size_t anakin_out_size = anakin_out.size(); - auto fluid_out = fluid_outputs[i_output++]; - for (size_t i = 0; i < anakin_out_size; i++) { - EXPECT_LT(std::abs(fluid_out[i] - anakin_out[i]), 1e-3); - } - } - } - - private: - std::unique_ptr engine_{nullptr}; - std::unique_ptr op_; - std::unique_ptr op_desc_; - framework::ProgramDesc program_desc_; - const std::unordered_set& parameters_; - framework::Scope* scope_; - const platform::DeviceContext& ctx_; - bool use_gpu_{true}; -}; - -template class AnakinConvertValidation<::anakin::saber::NV, - ::anakin::Precision::FP32>; -template class AnakinConvertValidation<::anakin::saber::NV, - ::anakin::Precision::INT8>; -#ifdef ANAKIN_X86_PLACE -template class AnakinConvertValidation<::anakin::saber::X86, - ::anakin::Precision::FP32>; -template class AnakinConvertValidation<::anakin::saber::X86, - ::anakin::Precision::INT8>; -#endif -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/engine.cc b/paddle/fluid/inference/anakin/engine.cc deleted file mode 100644 index d57952db0d36051a219391868f19a55079b8d7bf..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/engine.cc +++ /dev/null @@ -1,207 +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 "paddle/fluid/inference/anakin/engine.h" -#include -#include -#include -#include -#include "paddle/fluid/framework/ddim.h" - -using anakin::Precision; -using anakin::OpRunType; -using paddle::framework::LoDTensor; -template -using AnakinNetT = anakin::Net; - -template -using AnakinGraphT = anakin::graph::Graph; - -namespace paddle { -namespace inference { -namespace anakin { - -template -extern std::once_flag - AnakinEngine::init_anakin_; - -template -AnakinEngine::AnakinEngine( - bool need_summary, int device, int max_batch_size, - std::map> max_input_shape, - std::vector program_inputs, bool auto_config_layout) - : device_(device), - max_batch_size_(max_batch_size), - max_input_shape_(max_input_shape), - program_inputs_(program_inputs), - auto_config_layout_(auto_config_layout) { - ::anakin::TargetWrapper::set_device(device_); - std::call_once(init_anakin_, - [this]() { ::anakin::Env::env_init(); }); - graph_.reset(new AnakinGraphT()); - net_.reset(new AnakinNetT(need_summary)); -} - -template -AnakinEngine::~AnakinEngine() {} - -template -void AnakinEngine::SetInputShape( - const std::string &name, std::vector shape) { - graph_->AddOpAttr<::anakin::PTuple>(name, "input_shape", - std::move(shape)); -} - -template -void AnakinEngine::InitNet() { - net_->init(*graph_, auto_config_layout_); -} - -template -void AnakinEngine::AddOp( - const std::string &name, const std::string &type, - const std::vector &inputs, - const std::vector &outputs) { - PADDLE_ENFORCE(graph_->AddOp(name, type, inputs, outputs), "Add operation."); -} - -template -void AnakinEngine::BindInput( - const std::map &inputs) { -#ifdef PADDLE_WITH_CUDA - cudaDeviceSynchronize(); -#endif - for (const auto &input : inputs) { - auto *tensor = input.second; - auto *data = tensor->data(); - - auto fluid_input_shape = framework::vectorize(tensor->dims()); - while (fluid_input_shape.size() < 4) { - fluid_input_shape.push_back(1); - } - auto *anakin_input = net_->get_in(input.first); - std::vector max_input_shape = max_input_shape_[input.first]; - int max_shape_sum = - std::accumulate(max_input_shape.begin(), max_input_shape.end(), 1, - std::multiplies()); - if (tensor->numel() > max_shape_sum) { - PADDLE_ENFORCE(std::find(program_inputs_.begin(), program_inputs_.end(), - input.first) == program_inputs_.end(), - "The anakin input max shape should be greater than" - " or equal to the real input shape, Please set the max " - "input shape using EnableAnakinEngine"); - VLOG(3) << "Anakin Net will be reset because of the inputs out of range: " - << input.first; - graph_->Reshape(input.first, fluid_input_shape); - net_.reset(new AnakinNetT(true)); - net_->init(*graph_); - anakin_input = net_->get_in(input.first); - } - anakin_input->reshape(fluid_input_shape); - ::anakin::saber::Tensor tmp_anakin_tensor(data, TargetT(), device_, - fluid_input_shape); - anakin_input->copy_from(tmp_anakin_tensor); - } -} - -template -void AnakinEngine::Execute( - const std::map &inputs, - const std::map &outputs) { - BindInput(inputs); - net_->prediction(); - for (const auto &output : outputs) { - platform::CPUPlace cpu_place; - auto *tensor = output.second; - auto *anakin_output = net_->get_out(output.first); - auto *anakin_data = anakin_output->data(); - auto anakin_output_shape = anakin_output->valid_shape(); - tensor->Resize(framework::make_ddim(anakin_output_shape)); - auto *fluid_data = tensor->mutable_data(cpu_place); - memory::Copy(cpu_place, static_cast(fluid_data), cpu_place, - static_cast(anakin_data), - tensor->numel() * sizeof(float)); - } -} - -#ifdef PADDLE_WITH_CUDA -template -void AnakinEngine::Execute( - const std::map &inputs, - const std::map &outputs, - cudaStream_t stream) { - BindInput(inputs); - net_->prediction(); - cudaDeviceSynchronize(); - for (const auto &output : outputs) { - platform::CUDAPlace gpu_place(device_); - auto *tensor = output.second; - auto *anakin_output = net_->get_out(output.first); - auto *anakin_data = anakin_output->data(); - auto anakin_output_shape = anakin_output->valid_shape(); - tensor->Resize(framework::make_ddim(anakin_output_shape)); - auto *fluid_data = tensor->mutable_data(gpu_place); - memory::Copy(gpu_place, static_cast(fluid_data), gpu_place, - static_cast(anakin_data), - tensor->numel() * sizeof(float), stream); - } - cudaDeviceSynchronize(); -} -#endif - -template -void AnakinEngine::Freeze() { - PADDLE_ENFORCE(graph_->Freeze(), "Freeze anakin subgraph."); -} - -template -void AnakinEngine::Optimize() { - PADDLE_ENFORCE(graph_->Optimize(), "Graph optimization."); -} - -template -void AnakinEngine::RegistBlock( - ::anakin::PBlock *block_p) { - PADDLE_ENFORCE(graph_->RegistBlock(block_p), "Block register."); -} - -template -std::unique_ptr> -AnakinEngine::Clone() { - auto *engine = new AnakinEngine(); - engine->net_ = std::move(net_->Clone()); - return std::unique_ptr(engine); -} - -#ifdef PADDLE_WITH_CUDA -template class AnakinEngine<::anakin::saber::NV, ::anakin::Precision::FP32>; -template class AnakinEngineManager<::anakin::saber::NV, - ::anakin::Precision::FP32>; - -template class AnakinEngine<::anakin::saber::NV, ::anakin::Precision::INT8>; -template class AnakinEngineManager<::anakin::saber::NV, - ::anakin::Precision::INT8>; -#endif -#ifdef ANAKIN_X86_PLACE -template class AnakinEngine<::anakin::saber::X86, ::anakin::Precision::FP32>; -template class AnakinEngineManager<::anakin::saber::X86, - ::anakin::Precision::FP32>; -template class AnakinEngine<::anakin::saber::X86, ::anakin::Precision::INT8>; -template class AnakinEngineManager<::anakin::saber::X86, - ::anakin::Precision::INT8>; -#endif -// template class AnakinEngine<::anakin::saber::X86, ::anakin::Precision::FP32>; -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/engine.h b/paddle/fluid/inference/anakin/engine.h deleted file mode 100644 index e62bb82fd12405fcb93b16310f9197e7c5fd63b5..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/engine.h +++ /dev/null @@ -1,168 +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 -#include -#include -#include -#include -#include -#include -#include "paddle/fluid/framework/lod_tensor.h" -#include "paddle/fluid/inference/engine.h" -#include "paddle/fluid/inference/utils/singleton.h" -#ifdef EXIT // NOLINT -#undef EXIT // NOLINT -#endif // NOLINT -#include "framework/core/net/net.h" -#include "framework/core/types.h" -#include "framework/graph/graph.h" -#include "framework/graph/graph_global_mem.h" -#include "saber/saber_types.h" - -using anakin::Precision; - -namespace anakin { - -template -class Net; - -namespace graph { -template -class Graph; -} // namespace graph -} // namespace anakin - -namespace paddle { -namespace inference { -namespace anakin { - -template -class AnakinEngine { - using NetT = ::anakin::Net; - using GraphT = ::anakin::graph::Graph; - - public: - explicit AnakinEngine( - bool need_summary = false, int device = 0, int max_batch_size = 1, - std::map> max_input_shape = {}, - std::vector program_inputs = {}, - bool auto_config_layout = false); - ~AnakinEngine(); - void InitNet(); - void SetInputShape(const std::string &name, std::vector shape); - void AddOp(const std::string &name, const std::string &type, - const std::vector &inputs, - const std::vector &outputs); - - template - void AddOpAttr(const std::string &op_name, const std::string &attr_name, - const T &attr_value) { - PADDLE_ENFORCE(graph_->AddOpAttr(op_name, attr_name, attr_value), - "Add operation's attribution."); - } - NetT *Net() { return net_.get(); } - GraphT *Graph() { return graph_.get(); } - std::unique_ptr Clone(); - const std::map> &GetMaxInputShape() { - return max_input_shape_; - } - void SetMaxInputShape(std::map> shape) { - max_input_shape_ = shape; - } - const std::vector &GetScalableInputs() { - return program_inputs_; - } - void SetScalableInputs(std::vector program_inputs) { - program_inputs_ = program_inputs; - } - int GetMaxBatchSize() { return max_batch_size_; } - void Freeze(); - void Optimize(); - void RegistBlock(::anakin::PBlock *block_p); - void Save(std::string path) { graph_->save(path); } - bool IsInit() { return initialized_; } - int GetDevice() { return device_; } - void AddTensorScale(const std::string &tensor_name, float scale) { - tensor_scales_[tensor_name] = scale; - } - std::unordered_map GetTensorScales() { - return tensor_scales_; - } - void Execute(const std::map &inputs, - const std::map &outputs); -#ifdef PADDLE_WITH_CUDA - void Execute(const std::map &inputs, - const std::map &outputs, - cudaStream_t stream); -#endif - - private: - void BindInput(const std::map &inputs); - - private: - bool initialized_{false}; - int device_; - int max_batch_size_; - std::map> max_input_shape_; - std::vector program_inputs_; - std::unique_ptr graph_; - std::unique_ptr net_; - static std::once_flag init_anakin_; - std::unordered_map tensor_scales_; - // Always be false in gpu mode but true in most cpu cases. - bool auto_config_layout_; -}; - -template -class AnakinEngineManager { - using AnakinEngineT = AnakinEngine; - - public: - bool HasEngine(const std::string &name) const { - if (engines_.count(name) == 0) return false; - return engines_.at(name).get() != nullptr; - } - AnakinEngineT *Get(const std::string &name) const { - return engines_.at(name).get(); - } - - AnakinEngineT *Create(bool need_summary, int device, int max_batch_size, - std::map> max_input_shape, - std::vector program_inputs, - bool auto_config_layout, std::string engine_name) { - std::unique_lock lk(mut_); - auto *p = new AnakinEngine( - need_summary, device, max_batch_size, max_input_shape, program_inputs, - auto_config_layout); - engines_[engine_name].reset(p); - return p; - } - - void DeleteALL() { - for (auto &item : engines_) { - item.second.reset(nullptr); - } - } - - private: - std::unordered_map> engines_; - std::mutex mut_; -}; -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/op_teller.cc b/paddle/fluid/inference/anakin/op_teller.cc deleted file mode 100644 index 6b2dedafcf9b3d7b423c314c1851f4dc19ee2e18..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/op_teller.cc +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright (c) 2019 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/anakin/op_teller.h" - -namespace paddle { -namespace inference { -namespace anakin { - -// Just tell by the op_types. -struct SimpleOpTypeSetTeller : public Teller { - SimpleOpTypeSetTeller() { - teller_set.insert("mul"); - teller_set.insert("fc"); - teller_set.insert("conv2d_fusion"); - teller_set.insert("split"); - teller_set.insert("relu"); - teller_set.insert("pool2d"); - teller_set.insert("elementwise_add"); - teller_set.insert("elementwise_mul"); - teller_set.insert("concat"); - teller_set.insert("tanh"); - teller_set.insert("conv2d"); - teller_set.insert("batch_norm"); - teller_set.insert("softmax"); - teller_set.insert("flatten2"); - teller_set.insert("reshape2"); - teller_set.insert("transpose2"); - teller_set.insert("density_prior_box"); - teller_set.insert("detection_out"); - teller_set.insert("dropout"); - teller_set.insert("sigmoid"); - teller_set.insert("sum"); - teller_set.insert("depthwise_conv2d"); - teller_set.insert("prior_box"); - teller_set.insert("leaky_relu"); - teller_set.insert("affine_channel"); - teller_set.insert("relu6"); - teller_set.insert("swish"); - teller_set.insert("shuffle_channel"); - } - - bool operator()(const std::string& op_type, - const framework::OpDesc& desc) override { - return teller_set.count(op_type); - } - - private: - std::unordered_set teller_set; -}; - -bool OpTeller::Tell(const std::string& op_type, const framework::OpDesc& desc) { - for (auto& teller : tellers_) { - if (op_type == "pool2d" || op_type == "conv2d" || - op_type == "depthwise_conv2d" || op_type == "conv2d_transpose") { - std::vector paddings = - boost::get>(desc.GetAttr("paddings")); - if (paddings.size() > 2) return false; - } - if ((*teller)(op_type, desc)) return true; - } - return false; -} - -OpTeller::OpTeller() { tellers_.emplace_back(new SimpleOpTypeSetTeller); } - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/op_teller.h b/paddle/fluid/inference/anakin/op_teller.h deleted file mode 100644 index 15a42067b8438e60851a50e454abde95782d90ee..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/op_teller.h +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright (c) 2019 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 -#include -#include -#include -#include "paddle/fluid/framework/op_desc.h" - -namespace paddle { -namespace inference { -namespace anakin { - -/* - * Single Op teller definition. - * One can override this and define a more complex tell logic, considerring more - * issues such as op_desc. - */ -struct Teller { - virtual bool operator()(const std::string& op_type, - const framework::OpDesc& desc) = 0; - - virtual ~Teller() = default; -}; -/* - * A real example: - * - * struct SomeTeller : public Teller { - * bool operator()(const std::string& op_type, - * const framework::OpDesc& desc) override { - * return op_type == "fc" && desc.Inputs().size() == 2; - * } - *}; - */ - -/* - * class OpTeller helps to tell whether a fluid - * operator can be transformed to a TensorRT layer. - */ -class OpTeller { - public: - static OpTeller& Global() { - static std::unique_ptr x(new OpTeller); - return *x; - } - - bool Tell(const std::string& op_type, const framework::OpDesc& desc); - - private: - OpTeller(); - - private: - std::vector> tellers_; -}; - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/anakin/test_anakin_engine.cc b/paddle/fluid/inference/anakin/test_anakin_engine.cc deleted file mode 100644 index f105d0e9931ea81307fe0c626627677adbdd2b99..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/anakin/test_anakin_engine.cc +++ /dev/null @@ -1,92 +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 -#include - -#include - -#include "paddle/fluid/inference/anakin/engine.h" - -using anakin::AK_FLOAT; -using anakin::Precision; -using anakin::saber::NV; -using anakin::saber::Shape; -using anakin::PBlock; -using anakin::PTuple; -namespace paddle { -namespace inference { -namespace anakin { - -class TestAnakinEngine : public ::testing::Test { - protected: - void SetUp() override; - void TearDown() override {} - - protected: - using AnakinNvEngineT = AnakinEngine; - std::unique_ptr engine_{nullptr}; -}; - -void TestAnakinEngine::SetUp() { - engine_.reset(new AnakinEngine(true)); -} - -TEST_F(TestAnakinEngine, Execute) { - engine_->AddOp("op1", "Dense", {"x"}, {"y"}); - engine_->AddOpAttr("op1", "out_dim", 2); - engine_->AddOpAttr("op1", "bias_term", false); - engine_->AddOpAttr("op1", "axis", 1); - std::vector shape = {1, 1, 1, 2}; - Shape tmp_shape(shape); - - PBlock *weight1 = new PBlock(tmp_shape, AK_FLOAT); - engine_->RegistBlock(weight1); - float *cpu_data = static_cast(weight1->h_tensor().mutable_data()); - cpu_data[0] = 2.; - weight1->d_tensor().set_shape(tmp_shape); - weight1->d_tensor().copy_from(weight1->h_tensor()); - engine_->AddOpAttr("op1", "weight_1", *weight1); - - engine_->Freeze(); - // PTuple input_shape = {1}; - // engine_->AddOpAttr("x", "input_shape", input_shape); - engine_->SetInputShape("x", {1, 1, 1, 1}); - engine_->Optimize(); - engine_->InitNet(); - framework::LoDTensor x; - framework::LoDTensor y; - x.Resize({1, 1, 1, 1}); - y.Resize({1, 1, 1, 2}); - auto *x_data = x.mutable_data(platform::CUDAPlace()); - float x_data_cpu[] = {1.}; - cudaMemcpy(x_data, x_data_cpu, sizeof(float), cudaMemcpyHostToDevice); - - std::map inputs = {{"x", &x}}; - auto *y_data = y.mutable_data(platform::CUDAPlace()); - std::map outputs = {{"y", &y}}; - - cudaStream_t stream; - cudaStreamCreate(&stream); - - engine_->Execute(inputs, outputs, stream); - auto *y_data_gpu = y_data; - float y_data_cpu[2]; - cudaMemcpy(y_data_cpu, y_data_gpu, sizeof(float) * 2, cudaMemcpyDeviceToHost); - LOG(INFO) << "output value: " << y_data_cpu[0] << ", " << y_data_cpu[1]; -} - -} // namespace anakin -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/analysis/argument.h b/paddle/fluid/inference/analysis/argument.h index a8076dd199da23d99a4b673f90db5b224525f432..630e375dd7e8e85767e0146cdceba08a3ab3724e 100644 --- a/paddle/fluid/inference/analysis/argument.h +++ b/paddle/fluid/inference/analysis/argument.h @@ -59,7 +59,6 @@ struct Argument { using unique_ptr_t = std::unique_ptr>; using fusion_statis_t = std::unordered_map; - using anakin_max_shape_t = std::map>; bool Has(const std::string& key) const { return valid_fields_.count(key); } // If we set the model using config.SetModelBuffer, @@ -184,19 +183,6 @@ struct Argument { bool); DECL_ARGUMENT_FIELD(tensorrt_use_calib_mode, TensorRtUseCalibMode, bool); - DECL_ARGUMENT_FIELD(anakin_max_input_shape, AnakinMaxInputShape, - anakin_max_shape_t); - DECL_ARGUMENT_FIELD(anakin_max_batch_size, AnakinMaxBatchSize, int); - DECL_ARGUMENT_FIELD(anakin_min_subgraph_size, AnakinMinSubgraphSize, int); - DECL_ARGUMENT_FIELD(anakin_precision_mode, AnakinPrecisionMode, - AnalysisConfig::Precision); - DECL_ARGUMENT_FIELD(anakin_auto_config_layout, AnakinAutoConfigLayout, bool); - DECL_ARGUMENT_FIELD(use_anakin, UseAnakin, bool); - DECL_ARGUMENT_FIELD(anakin_passes_filter, AnakinPassesFilter, - std::vector); - DECL_ARGUMENT_FIELD(anakin_ops_filter, AnakinOpsFilter, - std::vector); - DECL_ARGUMENT_FIELD(lite_passes_filter, LitePassesFilter, std::vector); DECL_ARGUMENT_FIELD(lite_ops_filter, LiteOpsFilter, std::vector); diff --git a/paddle/fluid/inference/analysis/ir_pass_manager.cc b/paddle/fluid/inference/analysis/ir_pass_manager.cc index 385cd760244c6349c001897bc30665209a07f846..65189b89375da755be5279e27e5dc71d27370c41 100644 --- a/paddle/fluid/inference/analysis/ir_pass_manager.cc +++ b/paddle/fluid/inference/analysis/ir_pass_manager.cc @@ -139,24 +139,6 @@ void IRPassManager::CreatePasses(Argument *argument, pass->Set("enable_int8", new bool(enable_int8)); pass->Set("use_gpu", new bool(argument->use_gpu())); } - if (pass_name == "anakin_subgraph_pass") { - pass->Set("program", - new framework::ProgramDesc *(&argument->main_program())); - pass->Set("use_gpu", new bool(argument->use_gpu())); - pass->Set("gpu_device_id", new int(argument->gpu_device_id())); - pass->Set("model_from_memory", new bool(argument->model_from_memory())); - pass->Set("predictor_id", new int(argument->predictor_id())); - pass->Set("max_input_shape", new std::map>( - argument->anakin_max_input_shape())); - pass->Set("max_batch_size", new int(argument->anakin_max_batch_size())); - bool enable_int8 = - argument->anakin_precision_mode() == AnalysisConfig::Precision::kInt8; - pass->Set("enable_int8", new bool(enable_int8)); - pass->Set("anakin_ops_filter", - new std::vector(argument->anakin_ops_filter())); - pass->Set("auto_config_layout", - new bool(argument->anakin_auto_config_layout())); - } disable_logs_ = argument->disable_logs(); if (pass_name == "fc_fuse_pass") { pass->Set("use_gpu", new bool(argument->use_gpu())); diff --git a/paddle/fluid/inference/analysis/ir_passes/CMakeLists.txt b/paddle/fluid/inference/analysis/ir_passes/CMakeLists.txt index 4ef3381ac5ca6b17ba7305266a92b1c91853fba0..7fcb07d020b6e847771f9ae01a83956e79eb7e09 100644 --- a/paddle/fluid/inference/analysis/ir_passes/CMakeLists.txt +++ b/paddle/fluid/inference/analysis/ir_passes/CMakeLists.txt @@ -12,18 +12,6 @@ if (WITH_GPU AND TENSORRT_FOUND) set(INFER_IR_PASSES ${INFER_IR_PASSES} tensorrt_subgraph_pass CACHE INTERNAL "") endif() -if (ANAKIN_SUBGRAPH) - cc_library(anakin_subgraph_pass SRCS anakin_subgraph_pass.cc DEPS subgraph_util anakin_op_teller) - - set(analysis_deps ${analysis_deps} - subgraph_util anakin_subgraph_pass - CACHE INTERNAL "") - - set(pass_file ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h) - file(APPEND ${pass_file} "USE_PASS(anakin_subgraph_pass);\n") - set(INFER_IR_PASSES ${INFER_IR_PASSES} anakin_subgraph_pass CACHE INTERNAL "") -endif() - if (WITH_LITE) cc_library(lite_subgraph_pass SRCS lite_subgraph_pass.cc DEPS ${analysis_deps} subgraph_util lite_op_teller) set(analysis_deps ${analysis_deps} subgraph_util lite_subgraph_pass CACHE INTERNAL "") diff --git a/paddle/fluid/inference/analysis/ir_passes/anakin_subgraph_pass.cc b/paddle/fluid/inference/analysis/ir_passes/anakin_subgraph_pass.cc deleted file mode 100644 index b27896f98f78774ec9a9caa5809351a31347eeaf..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/analysis/ir_passes/anakin_subgraph_pass.cc +++ /dev/null @@ -1,284 +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 -#include -#include -#include -#include -#include -#include -#include - -#include "paddle/fluid/framework/ir/graph_pattern_detector.h" -#include "paddle/fluid/framework/ir/subgraph_detector.h" -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/op_teller.h" -#include "paddle/fluid/inference/analysis/helper.h" -#include "paddle/fluid/inference/analysis/ir_passes/anakin_subgraph_pass.h" -#include "paddle/fluid/string/pretty_log.h" - -namespace paddle { -namespace inference { -namespace analysis { - -using framework::ir::Node; - -void analysis::AnakinSubgraphPass::ApplyImpl( - framework::ir::Graph *graph) const { - framework::ir::FusePassBase::Init("anakin_subgraph_pass", graph); - - auto &anakin_ops_filter = Get>("anakin_ops_filter"); - - auto teller = [&anakin_ops_filter](const framework::ir::Node *node) { - if (!node->IsOp() || !node->Op()) - return false; - else if (std::find(anakin_ops_filter.begin(), anakin_ops_filter.end(), - node->Op()->Type()) != anakin_ops_filter.end()) - return false; - return anakin::OpTeller::Global().Tell(node->Op()->Type(), *node->Op()); - }; - - framework::ir::SubGraphFuser fuser(graph, teller, 6 /* min_subgraph_size */); - fuser(); - - std::vector graph_param_names = - ExtractParameters(graph->Nodes()); - - // those parameter already exist in anakin, and should not have another copy - // in fluid. - std::vector repetitive_params; - - for (auto *node : graph->Nodes()) { - if (node->IsOp() && !framework::ir::Agent(node).subgraph()->empty()) { - CreateAnakinOp(node, graph, graph_param_names, &repetitive_params); - std::unordered_set nodes2remove( - framework::ir::Agent(node).subgraph()->begin(), - framework::ir::Agent(node).subgraph()->end()); - framework::ir::GraphSafeRemoveNodes(graph, nodes2remove); - } - } - - std::unordered_set nodes2remove; - for (auto *node : graph->Nodes()) { - if (node->IsOp() && framework::ir::Agent(node).deleted()) { - nodes2remove.insert(node); - } - } - framework::ir::GraphSafeRemoveNodes(graph, nodes2remove); - graph->Set(framework::ir::kRepetitiveParamAttr, - new std::vector(repetitive_params)); -} - -std::string GenerateAnakinEngineKey(const std::set &engine_inputs, - const std::set &engine_outputs, - std::string id) { - std::string engine_hash_key = ""; - for (auto name : engine_inputs) { - engine_hash_key += name; - } - for (auto name : engine_outputs) { - engine_hash_key += name; - } - engine_hash_key += id; - auto engine_key = std::to_string(std::hash()(engine_hash_key)); - return engine_key; -} - -void AnakinSubgraphPass::CreateAnakinOp( - framework::ir::Node *node, framework::ir::Graph *graph, - const std::vector &graph_params, - std::vector *repetitive_params) const { - auto *op_desc = node->Op(); - auto &subgraph = *framework::ir::Agent(node).subgraph(); - PADDLE_ENFORCE(!subgraph.empty()); - - framework::ProgramDesc *program_desc = - Get("program"); - // Add new block for TensorRTEngineOP - const framework::BlockDesc &main_block = - program_desc->Block(framework::kRootBlockIndex); - // const framework::BlockDesc& main_block = program_desc->Block(0); - framework::BlockDesc *new_block = program_desc->AppendBlock(main_block); - - // An fake block desc. - framework::proto::BlockDesc block_proto; - framework::BlockDesc block_desc(nullptr, &block_proto); - block_desc.Proto()->set_parent_idx(-1); - block_desc.Proto()->set_idx(0); - string::PrettyLogDetail("--- detect a sub-graph with %d nodes", - subgraph.size()); - - for (auto *node : subgraph) { - auto *new_block_op = new_block->AppendOp(); - auto *op = block_desc.AppendOp(); - *new_block_op->Proto() = *node->Op()->Proto(); - *op->Proto() = *node->Op()->Proto(); - } - - // Then, we will use the input_names_with_id and output_names_with_id to - // generate the eigine key. - // So, We use set instead of unordered_set here to ensure that the engine key - // is unique. - std::set input_names; - std::set input_names_with_id; - std::vector params; - for (auto *x : node->inputs) { - input_names.insert(x->Name()); - input_names_with_id.insert(x->Name() + std::to_string(x->id())); - if (std::count(graph_params.begin(), graph_params.end(), x->Name()) > 0) { - params.push_back(x->Name()); - } - } - std::copy(params.begin(), params.end(), - std::back_inserter(*repetitive_params)); - op_desc->SetInput( - "Xs", std::vector(input_names.begin(), input_names.end())); - - std::set output_names; - std::set output_names_with_id; - for (auto *x : node->outputs) { - output_names.insert(x->Name()); - output_names_with_id.insert(x->Name() + std::to_string(x->id())); - } - - op_desc->SetOutput( - "Ys", std::vector(output_names.begin(), output_names.end())); - op_desc->SetType("anakin_engine"); - - std::unordered_map output_name_map; - std::unordered_map graph_var_map; - - for (framework::ir::Node *node : graph->Nodes()) { - if (node->IsVar() && node->Var()) { - graph_var_map[node->Name()] = node; - } - } - auto &subgraph_nodes = *framework::ir::Agent(node).subgraph(); - - // The following procedure is used to rename all the intermediate - // variables and the output variables of the subgraph. - RenameAndGetOutputs(subgraph_nodes, &block_desc, input_names_with_id, - &output_names_with_id, &output_names, &output_name_map, - graph_var_map, false); - - // When anakin engine runs at the end of the operation, - // output_mapping help us copy the data from the renamed ITensor - // to Tensor. - std::vector output_mapping; - for (auto name : output_names) { - PADDLE_ENFORCE(output_name_map.count(name) != 0); - output_mapping.push_back(output_name_map[name]); - } - - PADDLE_ENFORCE(!block_desc.Proto()->vars().empty(), - "the block has no var-desc"); - PADDLE_ENFORCE(!output_mapping.empty()); - op_desc->SetBlockAttr("sub_block", new_block); - SetAttr(op_desc->Proto(), "subgraph", - block_desc.Proto()->SerializeAsString()); - // Set attrs - SetAttr(op_desc->Proto(), "parameters", params); - SetAttr(op_desc->Proto(), "output_name_mapping", output_mapping); - int predictor_id = Get("predictor_id"); - auto engine_key = GenerateAnakinEngineKey( - input_names_with_id, output_names_with_id, std::to_string(predictor_id)); - - SetAttr(op_desc->Proto(), "engine_key", engine_key); - auto max_input_shape = - Get>>("max_input_shape"); - auto program_inputs = program_desc->GetFeedTargetNames(); - - bool use_gpu = Get("use_gpu"); - SetAttr(op_desc->Proto(), "use_gpu", use_gpu); - bool enable_int8 = Get("enable_int8"); - SetAttr(op_desc->Proto(), "enable_int8", enable_int8); - if (enable_int8) { - CreateAnakinEngine<::anakin::Precision::INT8>(&block_desc, params, - input_names, output_mapping, - program_inputs, engine_key); - } else { - CreateAnakinEngine<::anakin::Precision::FP32>(&block_desc, params, - input_names, output_mapping, - program_inputs, engine_key); - } -} - -template <::anakin::Precision PrecisionT> -void AnakinSubgraphPass::CreateAnakinEngine( - framework::BlockDesc *block_desc, const std::vector ¶ms, - const std::set &input_names, - const std::vector &output_mapping, - const std::vector &program_inputs, - const std::string &engine_key) const { - framework::BlockDesc block_desc_temp(nullptr, block_desc->Proto()); - bool use_gpu = Get("use_gpu"); - auto max_batch_size = Get("max_batch_size"); - auto max_input_shape = - Get>>("max_input_shape"); - if (use_gpu) { -#ifdef PADDLE_WITH_CUDA - inference::Singleton< - anakin::AnakinEngineManager<::anakin::saber::NV, PrecisionT>>::Global() - .Create(true, Get("gpu_device_id"), max_batch_size, - max_input_shape, program_inputs, false, engine_key); -#endif - } else { -#ifdef ANAKIN_X86_PLACE - bool auto_config_layout = Get("auto_config_layout"); - inference::Singleton< - anakin::AnakinEngineManager<::anakin::saber::X86, PrecisionT>>::Global() - .Create(true, Get("gpu_device_id"), max_batch_size, - max_input_shape, program_inputs, auto_config_layout, - engine_key); -#endif - } - - auto *scope = param_scope(); - std::unordered_set param_set(params.begin(), params.end()); - if (use_gpu) { -#ifdef PADDLE_WITH_CUDA - auto *anakin_engine = - inference::Singleton>::Global() - .Get(engine_key); - inference::Singleton>::Global() - .ConvertBlockToAnakinEngine( - &block_desc_temp, scope, - std::vector(input_names.begin(), input_names.end()), - param_set, output_mapping, anakin_engine); -#endif - } else { -#ifdef ANAKIN_X86_PLACE - auto *anakin_engine = - inference::Singleton>::Global() - .Get(engine_key); - inference::Singleton>::Global() - .ConvertBlockToAnakinEngine( - &block_desc_temp, scope, - std::vector(input_names.begin(), input_names.end()), - param_set, output_mapping, anakin_engine); -#endif - } -} - -} // namespace analysis -} // namespace inference -} // namespace paddle - -REGISTER_PASS(anakin_subgraph_pass, - paddle::inference::analysis::AnakinSubgraphPass); diff --git a/paddle/fluid/inference/analysis/ir_passes/anakin_subgraph_pass.h b/paddle/fluid/inference/analysis/ir_passes/anakin_subgraph_pass.h deleted file mode 100644 index 4ab2297b2d48876a95f41deb715188b2476b6b38..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/analysis/ir_passes/anakin_subgraph_pass.h +++ /dev/null @@ -1,51 +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 -#include -#include -#include -#include -#include "paddle/fluid/framework/ir/pass.h" -#include "paddle/fluid/inference/anakin/engine.h" -#include "paddle/fluid/inference/analysis/ir_passes/subgraph_util.h" - -using anakin::Precision; -using anakin::saber::NV; -namespace paddle { -namespace inference { -namespace analysis { - -class AnakinSubgraphPass : public framework::ir::FusePassBase { - public: - void ApplyImpl(framework::ir::Graph *graph) const override; - - private: - void CreateAnakinOp(framework::ir::Node *x, framework::ir::Graph *graph, - const std::vector &graph_params, - std::vector *repetitive_params) const; - void CleanIntermediateOutputs(framework::ir::Node *node); - template <::anakin::Precision PrecisionT> - void CreateAnakinEngine(framework::BlockDesc *block_desc, - const std::vector ¶ms, - const std::set &input_names, - const std::vector &output_mapping, - const std::vector &program_inputs, - const std::string &engine_key) const; -}; - -} // namespace analysis -} // namespace inference -} // namespace paddle diff --git a/paddle/fluid/inference/api/CMakeLists.txt b/paddle/fluid/inference/api/CMakeLists.txt index 9e25b7c18be9cc5fffaede6c0afe625cf2314fa6..219a6b25481d41eece1d7bf46eefa41e20491130 100755 --- a/paddle/fluid/inference/api/CMakeLists.txt +++ b/paddle/fluid/inference/api/CMakeLists.txt @@ -43,10 +43,6 @@ if(WITH_GPU AND TENSORRT_FOUND) set(inference_deps ${inference_deps} tensorrt_engine tensorrt_converter) endif() -if (ANAKIN_SUBGRAPH) - set(inference_deps ${inference_deps} anakin_op_converter anakin_engine) -endif() - if(WITH_NGRAPH) set(inference_deps ${inference_deps} ngraph) endif() @@ -64,17 +60,3 @@ if(WITH_TESTING) endif() cc_test(test_analysis_predictor SRCS analysis_predictor_tester.cc DEPS analysis_predictor benchmark ${inference_deps} ARGS --dirname=${WORD2VEC_MODEL_DIR}) - -if(ANAKIN_FOUND) - # Do not turn warnings into errors. - set_source_files_properties(api.cc api_anakin_engine.cc PROPERTIES COMPILE_FLAGS "-Wno-error") - cc_library(inference_anakin_api SRCS api.cc api_anakin_engine.cc DEPS boost xxhash framework_proto eigen3) - target_link_libraries(inference_anakin_api anakin anakin_saber_common) - cc_library(inference_anakin_api_shared SHARED SRCS api.cc api_anakin_engine.cc DEPS boost xxhash framework_proto eigen3) - target_link_libraries(inference_anakin_api_shared anakin anakin_saber_common) - function(anakin_target target_name) - target_compile_options(${target_name} BEFORE PUBLIC ${ANAKIN_COMPILE_EXTRA_FLAGS}) - endfunction() - anakin_target(inference_anakin_api) - anakin_target(inference_anakin_api_shared) -endif() diff --git a/paddle/fluid/inference/api/analysis_config.cc b/paddle/fluid/inference/api/analysis_config.cc index 16cb5305f32dd899ef8a8ae4fb854c5397857eb4..3755d088bfe97427eaf6b120eacae03937cff7b9 100644 --- a/paddle/fluid/inference/api/analysis_config.cc +++ b/paddle/fluid/inference/api/analysis_config.cc @@ -22,7 +22,6 @@ namespace paddle { extern const std::vector kTRTSubgraphPasses; -extern const std::vector kAnakinSubgraphPasses; extern const std::vector kLiteSubgraphPasses; PassStrategy *AnalysisConfig::pass_builder() const { @@ -120,15 +119,6 @@ AnalysisConfig::AnalysisConfig(const AnalysisConfig &other) { CP_MEMBER(use_mkldnn_quantizer_); CP_MEMBER(mkldnn_quantizer_config_); - CP_MEMBER(use_anakin_); - CP_MEMBER(anakin_max_batchsize_); - CP_MEMBER(anakin_max_input_shape_); - CP_MEMBER(anakin_min_subgraph_size_); - CP_MEMBER(anakin_precision_mode_); - CP_MEMBER(anakin_auto_config_layout_); - CP_MEMBER(anakin_passes_filter_); - CP_MEMBER(anakin_ops_filter_); - CP_MEMBER(use_lite_); CP_MEMBER(lite_precision_mode_); CP_MEMBER(lite_passes_filter_); @@ -338,25 +328,6 @@ void AnalysisConfig::Update() { pass_builder()->AppendAnalysisPass("memory_optimize_pass"); } - if (use_anakin_) { - PADDLE_ENFORCE(!use_tensorrt_, - "Anakin sub-graph and TensorRT sub-graph are not allowed to " - "run at the same time!"); - if (use_gpu_) { - LOG(INFO) << "Run Anakin GPU mode"; - } else { - LOG(INFO) << "Run Anakin CPU mode"; - } - - pass_builder()->ClearPasses(); - for (const auto &pass : kAnakinSubgraphPasses) { - if (std::find(anakin_passes_filter_.begin(), anakin_passes_filter_.end(), - pass) == anakin_passes_filter_.end()) { - pass_builder()->AppendPass(pass); - } - } - } - if (use_lite_) { #ifndef PADDLE_WITH_LITE LOG(WARNING) << "You tried to enable the lite subgraph " @@ -413,10 +384,9 @@ std::string AnalysisConfig::SerializeInfoCache() { ss << specify_input_name_; ss << cpu_math_library_num_threads_; - ss << use_anakin_; - ss << anakin_min_subgraph_size_; ss << use_lite_; + return ss.str(); } @@ -490,22 +460,6 @@ void AnalysisConfig::DisableGlogInfo() { Update(); } -void AnalysisConfig::EnableAnakinEngine( - int max_batch_size, std::map> max_input_shape, - int min_subgraph_size, AnalysisConfig::Precision precision_mode, - bool auto_config_layout, std::vector passes_filter, - std::vector ops_filter) { - anakin_max_batchsize_ = max_batch_size; - anakin_max_input_shape_ = max_input_shape; - anakin_min_subgraph_size_ = min_subgraph_size; - anakin_passes_filter_ = passes_filter; - anakin_ops_filter_ = ops_filter; - use_anakin_ = true; - anakin_precision_mode_ = precision_mode; - anakin_auto_config_layout_ = auto_config_layout; - Update(); -} - void AnalysisConfig::EnableLiteEngine( AnalysisConfig::Precision precision_mode, const std::vector &passes_filter, diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc index 669bbf9b4ae7a55b983c1b3fdcf593cb9a002a67..cd12a8985ba9b467b0d649a74dd351b2420944c4 100644 --- a/paddle/fluid/inference/api/analysis_predictor.cc +++ b/paddle/fluid/inference/api/analysis_predictor.cc @@ -50,10 +50,6 @@ #include "paddle/fluid/inference/tensorrt/trt_int8_calibrator.h" #endif -#if PADDLE_WITH_ANAKIN -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#endif - namespace paddle { using inference::Singleton; @@ -390,7 +386,6 @@ void AnalysisPredictor::PrepareArgument() { argument_.SetEnableMemoryOptim(config_.enable_memory_optim()); argument_.SetModelFromMemory(config_.model_from_memory_); // Analyze inference_program - argument_.SetUseAnakin(config_.anakin_engine_enabled()); argument_.SetPredictorID(predictor_id_); argument_.SetOptimCacheDir(config_.opt_cache_dir_); if (!config_.model_dir().empty()) { @@ -417,17 +412,6 @@ void AnalysisPredictor::PrepareArgument() { argument_.SetTensorRtUseCalibMode(config_.trt_use_calib_mode_); } - if (config_.anakin_engine_enabled()) { - argument_.SetAnakinMaxBatchSize(config_.anakin_max_batchsize_); - argument_.SetAnakinMaxInputShape(config_.anakin_max_input_shape_); - argument_.SetAnakinMinSubgraphSize(config_.anakin_min_subgraph_size_); - argument_.SetAnakinPrecisionMode(config_.anakin_precision_mode_); - argument_.SetAnakinAutoConfigLayout(config_.anakin_auto_config_layout_); - argument_.SetAnakinPassesFilter(config_.anakin_passes_filter_); - argument_.SetAnakinOpsFilter(config_.anakin_ops_filter_); - LOG(INFO) << "Anakin subgraph engine is enabled"; - } - if (config_.lite_engine_enabled()) { argument_.SetLitePrecisionMode(config_.lite_precision_mode_); argument_.SetLitePassesFilter(config_.lite_passes_filter_); @@ -950,33 +934,3 @@ USE_TRT_CONVERTER(layer_norm); USE_TRT_CONVERTER(gelu); USE_TRT_CONVERTER(multihead_matmul); #endif - -#if PADDLE_WITH_ANAKIN -USE_ANAKIN_CONVERTER(mul); -USE_ANAKIN_CONVERTER(fc); -USE_ANAKIN_CONVERTER(conv2d); -USE_ANAKIN_CONVERTER(conv2d_fusion); -USE_ANAKIN_CONVERTER(concat); -USE_ANAKIN_CONVERTER(split); -USE_ANAKIN_CONVERTER(relu); -USE_ANAKIN_CONVERTER(sigmoid); -USE_ANAKIN_CONVERTER(tanh); -USE_ANAKIN_CONVERTER(pool2d); -USE_ANAKIN_CONVERTER(elementwise_add); -USE_ANAKIN_CONVERTER(elementwise_mul); -USE_ANAKIN_CONVERTER(batch_norm); -USE_ANAKIN_CONVERTER(flatten); -USE_ANAKIN_CONVERTER(reshape); -USE_ANAKIN_CONVERTER(transpose); -USE_ANAKIN_CONVERTER(softmax); -USE_ANAKIN_CONVERTER(detection_out); -USE_ANAKIN_CONVERTER(density_prior_box); -USE_ANAKIN_CONVERTER(dropout); -USE_ANAKIN_CONVERTER(sum); -USE_ANAKIN_CONVERTER(prior_box); -USE_ANAKIN_CONVERTER(leaky_relu); -USE_ANAKIN_CONVERTER(affine_channel); -USE_ANAKIN_CONVERTER(relu6); -USE_ANAKIN_CONVERTER(swish); -USE_ANAKIN_CONVERTER(shuffle_channel); -#endif diff --git a/paddle/fluid/inference/api/api_anakin_engine.cc b/paddle/fluid/inference/api/api_anakin_engine.cc deleted file mode 100644 index cf5b53b51ccc257a30cc3e90335ed71f67c212a1..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/api/api_anakin_engine.cc +++ /dev/null @@ -1,462 +0,0 @@ -// Copyright (c) 2019 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 -#include -#include -#include - -#include "paddle/fluid/inference/api/api_anakin_engine.h" -#include "paddle/fluid/inference/api/paddle_api.h" - -#include "framework/core/net/net.h" -#include "framework/operators/ops.h" -#include "saber/funcs/timer.h" - -namespace paddle { - -using paddle::contrib::AnakinConfig; -template -extern std::mutex PaddleInferenceAnakinPredictor::mutex_; -template -extern std::once_flag PaddleInferenceAnakinPredictor::init_anakin_; - -template -void PaddleInferenceAnakinPredictor::InitEnv() { - std::call_once(this->init_anakin_, [this]() { - anakin::Env::env_init(this->config_.max_stream); - }); - anakin::TargetWrapper::set_device(this->config_.device_id); -} -template -void PaddleInferenceAnakinPredictor::InitNet() { - std::unique_lock lock(this->mutex_); - delete this->executor_p_; - this->executor_p_ = new anakin::Net(*this->graph_p_, true); -} -template -void PaddleInferenceAnakinPredictor::SetContext() { - this->ctx_p_ = std::make_shared>( - this->config_.device_id, this->config_.data_stream_id, - this->config_.compute_stream_id); -} -template -void PaddleInferenceAnakinPredictor::InitGraph() { - this->graph_p_ = - std::make_shared>(); - if (!this->config_.model_file.empty()) { - this->graph_p_->load(this->config_.model_file); - } else if (this->config_.model_buf_p) { - this->graph_p_->load(this->config_.model_buf_p, - this->config_.model_buf_len); - } else { - LOG(FATAL) << "Model load error."; - } - this->input_names_ = this->graph_p_->get_ins(); - this->output_names_ = this->graph_p_->get_outs(); - for (auto &input_str : this->input_names_) { - if (this->config_.init_inputs_shape.find(input_str) == - this->config_.init_inputs_shape.end()) { - LOG(FATAL) << input_str << " should be set in init_inputs_shape."; - } - std::vector shape = - this->config_.init_inputs_shape.find(input_str)->second; - this->graph_p_->Reshape(input_str, shape); - } -} -template -void PaddleInferenceAnakinPredictor::OptimizeGraph() { - if (!this->graph_p_->Optimize()) { - LOG(FATAL) << "Graph optimization error."; - } -} -template -void PaddleInferenceAnakinPredictor::InitPredictor() { - this->InitEnv(); - this->SetContext(); - this->InitGraph(); - this->OptimizeGraph(); - this->InitNet(); -} -template -void PaddleInferenceAnakinPredictor::Predict(int batch_size) { - anakin::TargetWrapper::device_sync(); - this->executor_p_->prediction(); - anakin::TargetWrapper::device_sync(); -} -template -bool PaddleInferenceAnakinPredictor::Run( - const std::vector &inputs, - std::vector *output_data, int batch_size) { - if (this->config_.re_allocable) { - return this->RunImpl(inputs, output_data, batch_size); - } else { - // Run inputs data that exceeds batch size in batches. - // 1. Reassign the batch size. - if (batch_size == -1) { - if (!inputs[0].lod.empty()) { - batch_size = inputs[0].lod[0].size() - 1; - } else { - batch_size = inputs[0].shape[0]; - } - } - // 2. If the data don't need to be batched, run it directly. - if (batch_size <= this->config_.init_batch_size) { - return this->RunImpl(inputs, output_data); - } - // 3. Check the batch size and define temporary variables. - std::vector cur_inputs; - std::vector outputs_master; - std::vector> outputs_vec; - for (const auto &input : inputs) { - if (!input.lod.empty()) { - if (input.lod.size() != 1) { - return false; - } - if (input.lod[0].size() - 1 != batch_size) { - return false; - } - } else { - LOG(INFO) << "Non-lod mode to be implemented."; - return false; - } - PaddleTensor tensor; - tensor.name = input.name; - tensor.dtype = PaddleDType::FLOAT32; - cur_inputs.push_back(tensor); - } - for (auto output : *output_data) { - PaddleTensor tensor; - tensor.name = output.name; - outputs_master.push_back(tensor); - } - // 4. Batch execution. - for (size_t start_batch = 0; start_batch < batch_size;) { - auto end_batch = start_batch + this->config_.init_batch_size; - if (end_batch > batch_size) { - end_batch = batch_size; - } - auto cur_outputs = outputs_master; - for (size_t i = 0; i < inputs.size(); i++) { - auto start = inputs[i].lod[0][start_batch]; - auto end = inputs[i].lod[0][end_batch]; - std::vector offsets; - for (size_t j = start_batch; j <= end_batch; j++) { - offsets.push_back(inputs[i].lod[0][j] - - inputs[i].lod[0][start_batch]); - } - auto mem_start = static_cast(inputs[i].data.data()) + start; - cur_inputs[i].data = - PaddleBuf(mem_start, (end - start) * sizeof(float)); - cur_inputs[i].lod = std::vector>({offsets}); - cur_inputs[i].shape = - std::vector({static_cast(end - start), 1, 1, 1}); - } - if (!this->RunImpl(cur_inputs, &cur_outputs)) { - return false; - } - outputs_vec.push_back(cur_outputs); - start_batch = end_batch; - } - // 5. Copy the results to contiguous memory. - // Assume that each batch has the same final outputs size. - auto count = [](const std::vector &v) { - int cnt = 1; - for_each(v.begin(), v.end(), [&cnt](int n) { cnt *= n; }); - return cnt; - }; - for (size_t i = 0; i < output_data->size(); i++) { - std::vector shape = outputs_vec[i][0].shape; - shape[0] = batch_size; - int total_cnt = count(shape); - (*output_data)[i].shape = shape; - (*output_data)[i].data.Resize(total_cnt * sizeof(float)); - float *addr = static_cast((*output_data)[i].data.data()); - for (const auto &single_out : outputs_vec) { - int cnt = count(single_out[i].shape); - memcpy(addr, single_out[i].data.data(), cnt * sizeof(float)); - addr += cnt; - } - } - } - return true; -} -template -bool PaddleInferenceAnakinPredictor::RunImpl( - const std::vector &inputs, - std::vector *output_data, int batch_size) { - anakin::TargetWrapper::set_device(this->config_.device_id); - for (const auto &input : inputs) { - if (input.dtype != PaddleDType::FLOAT32) { - LOG(FATAL) << "Only support float type inputs. " << input.name - << "'s type is not float"; - } - auto d_tensor_p = this->executor_p_->get_in(input.name); - // For backward compatibility. - auto net_shape = d_tensor_p->shape(); - if (net_shape.size() != input.shape.size()) { - LOG(FATAL) << " input " << input.name - << "'s shape size should be equal to that of net"; - } -#ifndef ANAKIN_MLU_PLACE - int sum = 1; - for_each(input.shape.begin(), input.shape.end(), [&](int n) { sum *= n; }); - if (sum > net_shape.count()) { - if (this->config_.re_allocable) { - this->graph_p_->Reshape(input.name, input.shape); - this->InitNet(); - d_tensor_p = this->executor_p_->get_in(input.name); - } else { - LOG(FATAL) - << "Run failed because Anakin was expected not to reallocate " - "memory."; - } - } -#endif - std::vector tmp_shape; - for (auto s : input.shape) { - tmp_shape.push_back(s); - } - auto *data = static_cast(input.data.data()); - anakin::saber::Tensor::Host_type> - h_tensor(data, typename anakin::DefaultHostType::Host_type(), 0, - tmp_shape); -#ifndef ANAKIN_MLU_PLACE - d_tensor_p->reshape(tmp_shape); -#endif - if (input.lod.size() > 0) { - if (input.lod.size() > 1) { - LOG(FATAL) << " input lod first dim should <=1, but you set " - << input.lod.size(); - } - std::vector lod(input.lod[0].begin(), input.lod[0].end()); - std::vector> offset({lod}); - d_tensor_p->set_seq_offset(offset); - VLOG(3) << "offset.size(): " << offset[0].size(); - for (int i = 0; i < offset[0].size(); i++) { - VLOG(3) << offset[0][i]; - } - } - d_tensor_p->copy_from(h_tensor); - } - this->Predict(batch_size); - if (output_data->empty()) { - LOG(FATAL) << "The output param in the Run function is incorrect."; - } - for (auto &output : *output_data) { - if (std::find(this->output_names_.begin(), this->output_names_.end(), - output.name) == this->output_names_.end()) { - LOG(FATAL) << output.name << " is not in the outputs of the graph."; - } - auto *d_tensor_p = this->executor_p_->get_out(output.name); - auto tmp_shape = d_tensor_p->valid_shape(); -#ifdef ANAKIN_MLU_PLACE - tmp_shape.set_num(batch_size); -#endif - output.shape = tmp_shape; - if (output.data.length() < tmp_shape.count() * sizeof(float)) { - output.data.Resize(tmp_shape.count() * sizeof(float)); - } - auto *data = static_cast(output.data.data()); - anakin::saber::Tensor::Host_type> - h_tensor(data, typename anakin::DefaultHostType::Host_type(), 0, - tmp_shape); - h_tensor.copy_from(*d_tensor_p); - } - return true; -} -template -bool PaddleInferenceAnakinPredictor::Reset( - PaddleInferenceAnakinPredictor *predictor) { - this->config_ = predictor->GetConfig(); - this->graph_p_ = predictor->GetGraph(); - this->input_names_ = predictor->GetInputNames(); - this->output_names_ = predictor->GetOutputNames(); - this->ctx_p_ = std::make_shared>( - this->config_.device_id, this->config_.data_stream_id, - this->config_.compute_stream_id); - this->InitNet(); - return true; -} -template -std::unique_ptr -PaddleInferenceAnakinPredictor::New() { - return std::unique_ptr( - new PaddleInferenceAnakinPredictor()); -} -// the cloned new Predictor of anakin share the same net weights from original -// Predictor -template -std::unique_ptr -PaddleInferenceAnakinPredictor::Clone() { - VLOG(3) << "Anakin Predictor::clone"; - std::unique_ptr cls = std::move(this->New()); - auto anakin_predictor_p = - dynamic_cast *>(cls.get()); - if (!anakin_predictor_p) { - LOG(FATAL) << "fail to call Init"; - } - anakin_predictor_p->Reset(this); - return cls; -} - -#ifdef ANAKIN_MLU_PLACE -template -std::unique_ptr -PaddleInferenceAnakinMLUPredictor::New() { - return std::unique_ptr( - new PaddleInferenceAnakinMLUPredictor()); -} -template -void PaddleInferenceAnakinMLUPredictor::SetContext() { - this->ctx_p_ = std::make_shared>( - this->config_.device_id, this->config_.data_stream_id, - this->config_.compute_stream_id); - this->ctx_p_->set_model_parallel(this->config_.model_parallel); - this->ctx_p_->set_fusion(this->config_.op_fuse); - this->ctx_p_->enable_batch_changable(); - this->ctx_p_->enable_channel_duplicate(); -} -template -void PaddleInferenceAnakinMLUPredictor::OptimizeGraph() { - if (!this->graph_p_->fusion_optimize(this->config_.op_fuse)) { - LOG(FATAL) << "Graph optimization error."; - } -} -template -void PaddleInferenceAnakinMLUPredictor::InitNet() { - std::unique_lock lock(this->mutex_); - delete this->executor_p_; - this->executor_p_ = new anakin::Net(); - this->executor_p_->fusion_init(*this->graph_p_, this->ctx_p_, true); -} -template -void PaddleInferenceAnakinMLUPredictor::Predict(int batch_size) { - this->executor_p_->fusion_prediction(batch_size); -} -#endif - -#ifdef ANAKIN_BM_PLACE -template -std::unique_ptr PaddleInferenceAnakinBMPredictor::New() { - return std::unique_ptr( - new PaddleInferenceAnakinBMPredictor()); -} -template -void PaddleInferenceAnakinBMPredictor::OptimizeGraph() { - if (!this->graph_p_->fusion_optimize()) { - LOG(FATAL) << "Graph optimization error."; - } -} -template -void PaddleInferenceAnakinBMPredictor::InitNet() { - std::unique_lock lock(this->mutex_); - delete this->executor_p_; - this->executor_p_ = new anakin::Net(); - this->executor_p_->fusion_init(*this->graph_p_, this->ctx_p_, true); -} -template -void PaddleInferenceAnakinBMPredictor::Predict(int batch_size) { - this->executor_p_->fusion_prediction(); -} -#endif - -#ifdef PADDLE_WITH_CUDA -template class PaddleInferenceAnakinPredictor< - anakin::NV, anakin::Precision::FP32, ::anakin::OpRunType::ASYNC>; -#endif -#ifdef ANAKIN_X86_PLACE -template class PaddleInferenceAnakinPredictor< - anakin::X86, anakin::Precision::FP32, ::anakin::OpRunType::ASYNC>; -#endif -#ifdef ANAKIN_MLU_PLACE -template class PaddleInferenceAnakinMLUPredictor; -#endif -#ifdef ANAKIN_BM_PLACE -template class PaddleInferenceAnakinBMPredictor; -#endif - -// A factory to help create difference predictor. -template <> -std::unique_ptr -CreatePaddlePredictor( - const contrib::AnakinConfig &config) { -#ifdef PADDLE_WITH_CUDA - if (config.target_type == contrib::AnakinConfig::NVGPU) { - return std::unique_ptr( - new PaddleInferenceAnakinPredictor(config)); - } -#endif -#ifdef ANAKIN_X86_PLACE - if (config.target_type == contrib::AnakinConfig::X86) { - return std::unique_ptr( - new PaddleInferenceAnakinPredictor(config)); - } -#endif -#ifdef ANAKIN_MLU_PLACE - if (config.target_type == contrib::AnakinConfig::MLU) { - return std::unique_ptr( - new PaddleInferenceAnakinMLUPredictor( - config)); - } -#endif -#ifdef ANAKIN_BM_PLACE - if (config.target_type == contrib::AnakinConfig::BM) { - return std::unique_ptr( - new PaddleInferenceAnakinBMPredictor( - config)); - } -#endif - LOG(FATAL) << "Anakin Predictor create on unknown platform: " - << config.target_type; - return nullptr; -} -template -void DisplayOpTimer(anakin::Net *net_executor, int epoch) { -#ifdef PADDLE_ANAKIN_ENABLE_OP_TIMER - std::vector op_time = net_executor->get_op_time(); - auto exec_funcs = net_executor->get_exec_funcs(); - auto op_param = net_executor->get_op_param(); - for (int i = 0; i < op_time.size(); i++) { - LOG(INFO) << "name: " << exec_funcs[i].name - << " op_type: " << exec_funcs[i].op_name - << " op_param: " << op_param[i] << " time " << op_time[i] / epoch; - } - std::map op_map; - for (int i = 0; i < op_time.size(); i++) { - auto it = op_map.find(op_param[i]); - if (it != op_map.end()) - op_map[op_param[i]] += op_time[i]; - else - op_map.insert(std::pair(op_param[i], op_time[i])); - } - for (auto it = op_map.begin(); it != op_map.end(); ++it) { - LOG(INFO) << it->first << " " << (it->second) / epoch << " ms"; - } -#endif -} -template -PaddleInferenceAnakinPredictor::~PaddleInferenceAnakinPredictor() { - DisplayOpTimer(this->executor_p_, this->config_.init_batch_size); - delete this->executor_p_; - this->executor_p_ = nullptr; -} - -} // namespace paddle diff --git a/paddle/fluid/inference/api/api_anakin_engine.h b/paddle/fluid/inference/api/api_anakin_engine.h deleted file mode 100644 index 97fc00610e05d4362d705a13a45ee6a3e5d39ffe..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/api/api_anakin_engine.h +++ /dev/null @@ -1,126 +0,0 @@ -/* Copyright (c) 2019 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 contains the implementation of inference API with Anakin engine - * embeded, this API can only support Anakin models. - */ - -#pragma once - -#include -#include -#include - -#include "framework/core/net/net.h" -#include "framework/graph/graph.h" -#include "paddle/fluid/inference/api/paddle_anakin_config.h" -#include "saber/core/shape.h" -#include "saber/saber_types.h" - -namespace paddle { - -using contrib::AnakinConfig; -using anakin::Precision; -using anakin::OpRunType; - -template -class PaddleInferenceAnakinPredictor : public PaddlePredictor { - public: - PaddleInferenceAnakinPredictor() = default; - - explicit PaddleInferenceAnakinPredictor(const AnakinConfig& config) - : config_(config) { - this->InitPredictor(); - } - - // NOTE Unlike the native engine, the buffers of anakin engine's output_data - // should be allocated first. - bool Run(const std::vector& inputs, - std::vector* output_data, - int batch_size = -1) override; - - std::unique_ptr Clone() override; - bool Reset(PaddleInferenceAnakinPredictor* predictor); - void InitPredictor(); - std::shared_ptr> GetGraph() { - return this->graph_p_; - } - std::vector GetInputNames() override { - return this->input_names_; - } - std::vector GetOutputNames() override { - return this->output_names_; - } - const AnakinConfig& GetConfig() const { return this->config_; } - - ~PaddleInferenceAnakinPredictor() override; - - protected: - void InitEnv(); - void InitGraph(); - virtual void OptimizeGraph(); - virtual void InitNet(); - virtual void SetContext(); - virtual void Predict(int batch_size); - virtual std::unique_ptr New(); - static std::mutex mutex_; - AnakinConfig config_; - std::shared_ptr> ctx_p_; - std::shared_ptr> graph_p_; - anakin::Net* executor_p_{nullptr}; - std::vector input_names_; - std::vector output_names_; - - private: - bool RunImpl(const std::vector& inputs, - std::vector* output_data, int batch_size = -1); - static std::once_flag init_anakin_; -}; - -#ifdef ANAKIN_MLU_PLACE -template -class PaddleInferenceAnakinMLUPredictor final - : public PaddleInferenceAnakinPredictor { - public: - PaddleInferenceAnakinMLUPredictor() = default; - explicit PaddleInferenceAnakinMLUPredictor(const AnakinConfig& config) { - this->config_ = config; - this->InitPredictor(); - } - std::unique_ptr New() override; - void SetContext() override; - void OptimizeGraph() override; - void InitNet() override; - void Predict(int batch_size) override; -}; -#endif - -#ifdef ANAKIN_BM_PLACE -template -class PaddleInferenceAnakinBMPredictor final - : public PaddleInferenceAnakinPredictor { - public: - PaddleInferenceAnakinBMPredictor() = default; - explicit PaddleInferenceAnakinBMPredictor(const AnakinConfig& config) { - this->config_ = config; - this->InitPredictor(); - } - std::unique_ptr New() override; - void OptimizeGraph() override; - void InitNet() override; - void Predict(int batch_size) override; -}; -#endif -} // namespace paddle diff --git a/paddle/fluid/inference/api/high_level_api.md b/paddle/fluid/inference/api/high_level_api.md index 8b8b6916d7e2b1a2f9fd09e9dfd2fe5a332461f5..6c4471d868acde0b897db31a1aaa1a01f592aa8e 100644 --- a/paddle/fluid/inference/api/high_level_api.md +++ b/paddle/fluid/inference/api/high_level_api.md @@ -24,17 +24,16 @@ that is important when there are multiple inputs and need to distinguish which v ## engine The inference APIs has two different underlying engines -- the native engine, which is consists of the native operators and framework, -- the Anakin engine, which has an Anakin library embedded. +- the native engine +- the tensorrt engine -The native engine takes a native Paddle model as input, and supports any model that trained by Paddle, -the Anakin engine is faster for some model, -but it can only take the Anakin model as input(user need to transform the format first manually) and currently not all Paddle models are supported. +The native engine, which is consists of the native operators and framework, takes a native Paddle model +as input, and supports any model that trained by Paddle. ```c++ enum class PaddleEngineKind { kNative = 0, // Use the native Fluid facility. - kAnakin, // Use Anakin for inference. + kAutoMixedTensorRT // Automatically mixing TensorRT with the Fluid ops. }; ``` diff --git a/paddle/fluid/inference/api/high_level_api_cn.md b/paddle/fluid/inference/api/high_level_api_cn.md index 58a7b8f990dcf2ea22baec0badd2efca42712f3f..5a15bf7893f1106222ffa5d7eb0cafaf994322f8 100644 --- a/paddle/fluid/inference/api/high_level_api_cn.md +++ b/paddle/fluid/inference/api/high_level_api_cn.md @@ -29,10 +29,9 @@ struct PaddleTensor { ## engine -高层 API 底层有多种优化实现,我们称之为 engine,目前有三种 engine +高层 API 底层有多种优化实现,我们称之为 engine,目前有两种 engine - 原生 engine,由 paddle 原生的 forward operator 组成,可以天然支持所有paddle 训练出的模型, -- Anakin engine,封装了 [Anakin](https://github.com/PaddlePaddle/Anakin) ,在某些模型上性能不错,但只能接受自带模型格式,无法支持所有 paddle 模型, - TensorRT mixed engine,用子图的方式支持了 [TensorRT](https://developer.nvidia.com/tensorrt) ,支持所有paddle 模型,并自动切割部分计算子图到 TensorRT 上加速(WIP) 其实现为 @@ -40,7 +39,6 @@ struct PaddleTensor { ```c++ enum class PaddleEngineKind { kNative = 0, // Use the native Fluid facility. - kAnakin, // Use Anakin for inference. kAutoMixedTensorRT // Automatically mixing TensorRT with the Fluid ops. }; ``` diff --git a/paddle/fluid/inference/api/paddle_anakin_config.h b/paddle/fluid/inference/api/paddle_anakin_config.h deleted file mode 100644 index e9af13f526a2341a6e02c6979493e995c72dfa98..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/api/paddle_anakin_config.h +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) 2019 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 -#include -#include -#include -#include - -#include "paddle_api.h" // NOLINT - -namespace paddle { -namespace contrib { -// Configurations for Anakin engine. -struct AnakinConfig : public PaddlePredictor::Config { - enum TargetType { NVGPU = 0, X86, MLU, BM }; - int device_id{0}; - std::string model_file; - std::map> init_inputs_shape; - int init_batch_size{-1}; - bool re_allocable{true}; - int max_stream{4}; - int data_stream_id{0}; - int compute_stream_id{0}; - char* model_buf_p{nullptr}; - size_t model_buf_len{0}; - TargetType target_type; -#ifdef ANAKIN_MLU_PLACE - int model_parallel{8}; - int data_parallel{1}; - bool op_fuse{false}; - bool sparse{false}; -#endif -}; - -} // namespace contrib -} // namespace paddle diff --git a/paddle/fluid/inference/api/paddle_analysis_config.h b/paddle/fluid/inference/api/paddle_analysis_config.h index a47c9e0d5733cdf035369aa1d4d3a5280d339c65..292615a019ad648c3fcf5446453b99552451603f 100644 --- a/paddle/fluid/inference/api/paddle_analysis_config.h +++ b/paddle/fluid/inference/api/paddle_analysis_config.h @@ -161,25 +161,13 @@ struct AnalysisConfig { */ bool tensorrt_engine_enabled() const { return use_tensorrt_; } /** - * \brief Turn on the usage of Anakin sub-graph engine. + * \brief Turn on the usage of Lite sub-graph engine. */ - void EnableAnakinEngine( - int max_batch_size = 1, - std::map> max_input_shape = {}, - int min_subgraph_size = 6, Precision precision = Precision::kFloat32, - bool auto_config_layout = false, - std::vector passes_filter = {}, - std::vector ops_filter = {}); - void EnableLiteEngine( AnalysisConfig::Precision precision_mode = Precision::kFloat32, const std::vector& passes_filter = {}, const std::vector& ops_filter = {}); - /** A boolean state indicating whether the Anakin sub-graph engine is used. - */ - bool anakin_engine_enabled() const { return use_anakin_; } - /** A boolean state indicating whether the Lite sub-graph engine is used. */ bool lite_engine_enabled() const { return use_lite_; } @@ -350,15 +338,6 @@ struct AnalysisConfig { mutable std::unique_ptr pass_builder_; - bool use_anakin_{false}; - int anakin_max_batchsize_; - int anakin_min_subgraph_size_{6}; - std::map> anakin_max_input_shape_; - Precision anakin_precision_mode_; - bool anakin_auto_config_layout_{false}; - std::vector anakin_passes_filter_; - std::vector anakin_ops_filter_; - bool use_lite_{false}; std::vector lite_passes_filter_; std::vector lite_ops_filter_; diff --git a/paddle/fluid/inference/api/paddle_api.h b/paddle/fluid/inference/api/paddle_api.h index 8c0adfcb0688920163bd8a2f960fa5332ff206e1..b0f86c6d7c8dce45cc49790f2463191d21f9b8b4 100644 --- a/paddle/fluid/inference/api/paddle_api.h +++ b/paddle/fluid/inference/api/paddle_api.h @@ -352,7 +352,6 @@ enum class PaddleEngineKind { kNative = 0, /*!< Use the native Fluid facility. */ kAutoMixedTensorRT, /*!< Automatically mix Fluid with TensorRT. */ kAnalysis, /*!< More optimization. */ - kAnakin /*!< Use Anakin for inference, not mature yet. */ }; template diff --git a/paddle/fluid/inference/api/paddle_inference_api.h b/paddle/fluid/inference/api/paddle_inference_api.h index feb5373c1dc6206b657c325bd34fb1450eb197c8..6f30ad95f168cebe9702c90fbd2cca2c79a0e83f 100644 --- a/paddle/fluid/inference/api/paddle_inference_api.h +++ b/paddle/fluid/inference/api/paddle_inference_api.h @@ -28,6 +28,3 @@ limitations under the License. */ #include "paddle_analysis_config.h" // NOLINT #include "paddle_api.h" // NOLINT -#if (defined PADDLE_WITH_ANAKIN) -#include "paddle_anakin_config.h" // NOLINT -#endif diff --git a/paddle/fluid/inference/api/paddle_pass_builder.cc b/paddle/fluid/inference/api/paddle_pass_builder.cc index 2ad118df2ea534cf036f6b1aad25afea1b5f01b7..6f964b77d1e6158150b551c343b76aa24e72cc4b 100644 --- a/paddle/fluid/inference/api/paddle_pass_builder.cc +++ b/paddle/fluid/inference/api/paddle_pass_builder.cc @@ -92,19 +92,6 @@ const std::vector kTRTSubgraphPasses({ "transpose_flatten_concat_fuse_pass", }); -// The following passes works for Anakin sub-graph engine. -const std::vector kAnakinSubgraphPasses({ - "quant_conv2d_dequant_fuse_pass", // - "simplify_anakin_priorbox_detection_out_pass", // - "fillconstant_elementwisemul_fuse", // - "fc_fuse_pass", // - "conv_elementwise_add_fuse_pass", // - "fc_gru_fuse_pass", // - "shuffle_channel_detect_pass", // - "anakin_subgraph_pass", // - "fc_gru_fuse_pass", // -}); - const std::vector kLiteSubgraphPasses({ #ifdef PADDLE_WITH_LITE "lite_subgraph_pass", diff --git a/paddle/fluid/inference/api/paddle_pass_builder.h b/paddle/fluid/inference/api/paddle_pass_builder.h index d83f1ae6143c137655abb609f15e6984d1093659..1a83fabbdbcc9fe6284c70bd68396d190bb95761 100644 --- a/paddle/fluid/inference/api/paddle_pass_builder.h +++ b/paddle/fluid/inference/api/paddle_pass_builder.h @@ -162,7 +162,6 @@ class GpuPassStrategy : public PassStrategy { }; extern const std::vector kTRTSubgraphPasses; -extern const std::vector kAnakinSubgraphPasses; extern const std::vector kLiteSubgraphPasses; } // namespace paddle diff --git a/paddle/fluid/inference/capi/c_api.h b/paddle/fluid/inference/capi/c_api.h index 7a36587398c3e384003b227b93c7088b4c026da6..a79930466c9c54d27d164b0b7ea0b9eeb18d29b4 100644 --- a/paddle/fluid/inference/capi/c_api.h +++ b/paddle/fluid/inference/capi/c_api.h @@ -188,17 +188,6 @@ typedef struct PD_MaxInputShape { int shape_size; } PD_MaxInputShape; -PADDLE_CAPI_EXPORT extern void PD_EnableAnakinEngine( - PD_AnalysisConfig* config, int max_batch_size = 1, - PD_MaxInputShape* max_input_shape = NULL, int max_input_shape_size = 0, - int min_subgraph_size = 6, Precision precision = Precision::kFloat32, - bool auto_config_layout = false, char** passes_filter = NULL, - int passes_filter_size = 0, char** ops_filter = NULL, - int ops_filter_size = 0); - -PADDLE_CAPI_EXPORT extern bool PD_AnakinEngineEnabled( - const PD_AnalysisConfig* config); - PADDLE_CAPI_EXPORT extern void PD_SwitchIrDebug(PD_AnalysisConfig* config, bool x = true); diff --git a/paddle/fluid/inference/capi/pd_config.cc b/paddle/fluid/inference/capi/pd_config.cc index 657add6d6643daf21f409305b6c8c01c16abd277..3b5e5f01d420862abd2da7aa53ad9f31c82d9c24 100644 --- a/paddle/fluid/inference/capi/pd_config.cc +++ b/paddle/fluid/inference/capi/pd_config.cc @@ -165,42 +165,6 @@ bool PD_TensorrtEngineEnabled(const PD_AnalysisConfig* config) { return config->config.tensorrt_engine_enabled(); } -void PD_EnableAnakinEngine(PD_AnalysisConfig* config, int max_batch_size, - PD_MaxInputShape* max_input_shape, - int max_input_shape_size, int min_subgraph_size, - Precision precision, bool auto_config_layout, - char** passes_filter, int passes_filter_size, - char** ops_filter, int ops_filter_size) { - PADDLE_ENFORCE_NOT_NULL(config); - std::map> mis; - if (max_input_shape) { - for (int i = 0; i < max_input_shape_size; ++i) { - std::vector tmp_shape; - tmp_shape.assign( - max_input_shape[i].shape, - max_input_shape[i].shape + max_input_shape[i].shape_size); - mis[std::string(max_input_shape[i].name)] = std::move(tmp_shape); - } - } - std::vector pf; - std::vector of; - if (passes_filter) { - pf.assign(passes_filter, passes_filter + passes_filter_size); - } - if (ops_filter) { - of.assign(ops_filter, ops_filter + ops_filter_size); - } - - config->config.EnableAnakinEngine(max_batch_size, mis, min_subgraph_size, - paddle::ConvertToACPrecision(precision), - auto_config_layout, pf, of); -} - -bool PD_AnakinEngineEnabled(const PD_AnalysisConfig* config) { - PADDLE_ENFORCE_NOT_NULL(config); - return config->config.anakin_engine_enabled(); -} - void PD_SwitchIrDebug(PD_AnalysisConfig* config, bool x) { PADDLE_ENFORCE_NOT_NULL(config); config->config.SwitchIrDebug(x); diff --git a/paddle/fluid/inference/tests/api/anakin_mlu_tester.cc b/paddle/fluid/inference/tests/api/anakin_mlu_tester.cc deleted file mode 100644 index 8094c744fef50361c50eabe53799658985113f09..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/tests/api/anakin_mlu_tester.cc +++ /dev/null @@ -1,98 +0,0 @@ -/* Copyright (c) 2019 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 -#include "paddle/fluid/inference/api/paddle_inference_api.h" - -DEFINE_string(model, "", "Directory of the inference model."); - -namespace paddle { - -contrib::AnakinConfig Config() { - // Determine the use of memory here. - std::map> init_inputs_shape; - init_inputs_shape["input_0"] = std::vector({1, 3, 112, 112}); - - contrib::AnakinConfig config; - config.target_type = contrib::AnakinConfig::MLU; - config.model_file = FLAGS_model; - config.init_inputs_shape = init_inputs_shape; - - // Determine the device execution context. - config.device_id = 0; - config.data_stream_id = 0; - config.compute_stream_id = 0; - - // Set re_allocable and op_fuse TRUE. - config.re_allocable = true; - config.op_fuse = true; - - return config; -} - -void single_test() { - // 1. Defining basic data structures. - auto config = paddle::Config(); - auto predictor = - paddle::CreatePaddlePredictor(config); - - // 2. Define the data structure of the predictor inputs and outputs. - std::vector input_tensors; - std::vector output_tensors; - - // 3. Define and fill the inputs tensor. - int num = 1; - int channel = 3; - int height = 112; - int width = 112; - std::vector input(num * channel * height * width, 1); - std::vector> inputs({input}); - const std::vector input_names{"input_0"}; - for (auto& name : input_names) { - paddle::PaddleTensor tensor; - tensor.name = name; - tensor.dtype = PaddleDType::FLOAT32; - input_tensors.push_back(tensor); - } - for (size_t j = 0; j < input_tensors.size(); j++) { - input_tensors[j].data = - paddle::PaddleBuf(&inputs[j][0], inputs[j].size() * sizeof(float)); - // The shape of each execution can be changed. - input_tensors[j].shape = std::vector({num, channel, height, width}); - } - - // 4. Set the output placeholder of predictor. - PaddleTensor predict_out, score_out; - predict_out.name = "landmark_predict_out"; - score_out.name = "landmark_score_out"; - output_tensors.push_back(predict_out); - output_tensors.push_back(score_out); - - // 5. Execution predict. - predictor->Run(input_tensors, &output_tensors); - - // 6. Take out the output data. - for (auto out : output_tensors) { - float* data_o = static_cast(out.data.data()); - LOG(INFO) << out.name << " size = " << out.data.length() / sizeof(float); - } -} -} // namespace paddle - -int main(int argc, char** argv) { - google::ParseCommandLineFlags(&argc, &argv, true); - paddle::single_test(); - return 0; -} diff --git a/paddle/fluid/inference/tests/api/anakin_rnn2_tester.cc b/paddle/fluid/inference/tests/api/anakin_rnn2_tester.cc deleted file mode 100644 index 27abaa530b36f4a95f05ea16e8068be052d9711c..0000000000000000000000000000000000000000 --- a/paddle/fluid/inference/tests/api/anakin_rnn2_tester.cc +++ /dev/null @@ -1,261 +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 -#include -#include "paddle/fluid/inference/api/helper.h" -#include "paddle/fluid/inference/api/paddle_inference_api.h" - -#define BUFFER_SIZE (10000) -#define COMPARE_OUTPUTS (1) -#define PRINT_INPUTS (0) - -DEFINE_string(model, "", "Directory of the inference model."); -DEFINE_string(datapath, "", "Path of the dataset."); -DEFINE_string(truthpath, "", "Path of the dataset."); -DEFINE_int32(batch_size, 1, "Batch size per execution."); -DEFINE_int32(repeats, 1, "Number of iterations."); -DEFINE_int32( - start_line, 0, - "The starting line of the text file read (this line will be read)."); -DEFINE_int32(end_line, 1000000, - "The ending line of the text file read (this line will be read)."); -DEFINE_int32(init_batch_size, 40, - "Max batch size for Anakin memory allocation."); -DEFINE_int32(threads_num, 2, "Threads num for Anakin."); - -class Data { - public: - Data(std::string file_name, size_t batch_size, size_t start = 0, - size_t end = 1000000) - : _batch_size(batch_size), _total_length(0), _inputs_size(6) { - _file.open(file_name); - _file.seekg(_file.end); - _total_length = _file.tellg(); - _file.seekg(_file.beg); - read_file_to_vec(start, end); - reset_current_line(); - } - void reset_current_line(); - const std::vector& get_lines(); - void read_file_to_vec(const size_t start, const size_t end); - int get_next_batches(std::vector>* inputs, - std::vector>* seq_offsets); - - private: - std::fstream _file; - int _batch_size; - size_t _total_length; - size_t _inputs_size; - std::vector _lines; - size_t _current_line; -}; - -void Data::read_file_to_vec(const size_t start, const size_t end) { - std::string line; - size_t count = 0; - _lines.clear(); - while (std::getline(_file, line)) { - if (count >= start && count <= end) { - _lines.push_back(line); - } - count++; - } -} - -const std::vector& Data::get_lines() { return _lines; } - -void Data::reset_current_line() { _current_line = 0; } - -int Data::get_next_batches(std::vector>* data, - std::vector>* offsets) { - data->clear(); - offsets->clear(); - data->resize(_inputs_size); - offsets->resize(_inputs_size); - for (auto& offset : *offsets) { - offset.push_back(0); - } - - int seq_num = -1; - int pre_query_index = -1; - while (_current_line < _lines.size()) { - int cur_query_index = -1; - std::vector line; - paddle::inference::split(_lines[_current_line], ';', &line); - for (size_t i = 0; i < line.size(); i++) { - std::vector float_v; - paddle::inference::split_to_float(line[i], ' ', &float_v); - if (i == 0) { - cur_query_index = float_v[0]; - if (pre_query_index != -1 && cur_query_index != pre_query_index) { - return seq_num; - } - seq_num++; - _current_line++; - } else { - if (float_v.size() == 0) { - float_v.push_back(-1); - } - (*data)[i - 1].insert((*data)[i - 1].end(), float_v.begin(), - float_v.end()); - (*offsets)[i - 1].push_back((*offsets)[i - 1][seq_num] + - float_v.size()); - } - } - if (seq_num + 1 >= _batch_size) { - return seq_num; - } else { - pre_query_index = cur_query_index; - } - } - return seq_num; -} - -namespace paddle { - -contrib::AnakinConfig GetConfig() { - contrib::AnakinConfig config; - - std::map> init_inputs_shape; - init_inputs_shape["q_basic"] = std::vector({1000, 1, 1, 1}); - init_inputs_shape["q_bigram0"] = std::vector({1000, 1, 1, 1}); - init_inputs_shape["pt_basic"] = std::vector({2000, 1, 1, 1}); - init_inputs_shape["pa_basic"] = std::vector({4000, 1, 1, 1}); - init_inputs_shape["pa_bigram0"] = std::vector({4000, 1, 1, 1}); - init_inputs_shape["pt_bigram0"] = std::vector({2000, 1, 1, 1}); - - // using AnakinConfig::X86 if you need to use cpu to do inference - config.target_type = contrib::AnakinConfig::NVGPU; - config.model_file = FLAGS_model; - config.device_id = 0; - config.init_batch_size = FLAGS_init_batch_size; - config.init_inputs_shape = init_inputs_shape; - config.re_allocable = false; - return config; -} - -void single_test(PaddlePredictor* predictor_master) { - auto predictor = predictor_master->Clone(); - - Data data(FLAGS_datapath, FLAGS_batch_size, FLAGS_start_line, FLAGS_end_line); - - std::vector> inputs; - std::vector> seq_offsets; - std::vector compare_outputs; - - const std::vector input_names{"q_basic", "q_bigram0", - "pt_basic", "pt_bigram0", - "pa_basic", "pa_bigram0"}; - std::vector input_tensors; - std::vector output_tensors; - for (auto& name : input_names) { - PaddleTensor tensor; - tensor.name = name; - tensor.dtype = PaddleDType::FLOAT32; - input_tensors.push_back(tensor); - } - - PaddleTensor tensor_out; - tensor_out.name = "save_infer_model/scale_0"; - tensor_out.shape = std::vector({}); - tensor_out.data = PaddleBuf(); - tensor_out.dtype = PaddleDType::FLOAT32; - output_tensors.push_back(tensor_out); - - inference::Timer timer; - for (int i = 0; i < FLAGS_repeats; i++) { - data.reset_current_line(); - size_t count = 0; - float time_sum = 0; - while (data.get_next_batches(&inputs, &seq_offsets) >= 0) { -#if PRINT_INPUTS - for (size_t i = 0; i < inputs.size(); i++) { - LOG(INFO) << "data " << i; - for (size_t j = 0; j < inputs[i].size(); j++) { - LOG(INFO) << j << ": " << inputs[i][j]; - } - for (auto j : seq_offsets[i]) { - LOG(INFO) << "offsets: " << i << ": " << j; - } - } -#endif - for (size_t j = 0; j < input_tensors.size(); j++) { - input_tensors[j].data = - PaddleBuf(&inputs[j][0], inputs[j].size() * sizeof(float)); - input_tensors[j].lod = - std::vector>({seq_offsets[j]}); - input_tensors[j].shape = - std::vector({static_cast(inputs[j].size()), 1, 1, 1}); - } - timer.tic(); - predictor->Run(input_tensors, &output_tensors); - float time = timer.toc(); -#if COMPARE_OUTPUTS - float* data_o = static_cast(output_tensors[0].data.data()); - LOG(INFO) << "outputs[0].data.size() = " - << output_tensors[0].data.length() / sizeof(float); - size_t sum = 1; - for_each(output_tensors[0].shape.begin(), output_tensors[0].shape.end(), - [&](int n) { sum *= n; }); - for (size_t j = 0; j < sum; ++j) { - LOG(INFO) << "output[" << j << "]: " << data_o[j]; - compare_outputs.push_back(data_o[j]); - } -#endif - LOG(INFO) << "Single Time: " << time; - count++; - if (count > 10) { - time_sum += timer.toc(); - } - } - inference::PrintTime(FLAGS_batch_size, FLAGS_repeats, 1, 0, - time_sum / (count - 10)); -#if COMPARE_OUTPUTS - Data data(FLAGS_truthpath, 1); - const std::vector truth_vals = data.get_lines(); - for (size_t j = 0; j < truth_vals.size(); j++) { - float truth = std::atof(truth_vals[j].c_str()); - float compa = compare_outputs[j]; - float diff = std::abs(truth - compa); - LOG(INFO) << "[DIFF " << j << " ] " << diff; - if (diff > 0.0001) { - LOG(FATAL) << "The result is wrong!"; - } - } - LOG(INFO) << "The result is correct!"; -#endif - } -} -} // namespace paddle - -int main(int argc, char** argv) { - google::ParseCommandLineFlags(&argc, &argv, true); - std::vector threads; - - auto config = paddle::GetConfig(); - config.data_stream_id = 0; - config.compute_stream_id = 0; - std::unique_ptr predictor_master = - paddle::CreatePaddlePredictor(config); - - for (int i = 0; i < FLAGS_threads_num; i++) { - threads.push_back(std::thread(paddle::single_test, predictor_master.get())); - } - for (auto& t : threads) { - t.join(); - } - return 0; -} diff --git a/paddle/fluid/inference/tests/api/analyzer_capi_tester.cc b/paddle/fluid/inference/tests/api/analyzer_capi_tester.cc index ee4725f1c551d06867562fa7f50cc2979569e0ec..2c4ab70a010d51e911d8924b0c99f54917e479f6 100644 --- a/paddle/fluid/inference/tests/api/analyzer_capi_tester.cc +++ b/paddle/fluid/inference/tests/api/analyzer_capi_tester.cc @@ -92,9 +92,6 @@ TEST(PD_AnalysisConfig, profile_mkldnn) { CHECK(quantizer_enable) << "NO"; PD_SetMkldnnCacheCapacity(config, 0); PD_SetModel(config, prog_file.c_str(), params_file.c_str()); - PD_EnableAnakinEngine(config); - bool anakin_enable = PD_AnakinEngineEnabled(config); - LOG(INFO) << anakin_enable; PD_DeleteAnalysisConfig(config); } #endif diff --git a/paddle/fluid/operators/CMakeLists.txt b/paddle/fluid/operators/CMakeLists.txt index 947b4d8f0a59f7286965e695f9841d32ef53581e..675466bfb8d2e4f4806287d642101dd3cc7ad72c 100644 --- a/paddle/fluid/operators/CMakeLists.txt +++ b/paddle/fluid/operators/CMakeLists.txt @@ -34,10 +34,6 @@ if (WITH_GPU AND TENSORRT_FOUND) add_subdirectory(tensorrt) endif() -if (ANAKIN_SUBGRAPH) - add_subdirectory(anakin) -endif() - if (WITH_LITE) add_subdirectory(lite) endif() diff --git a/paddle/fluid/operators/anakin/CMakeLists.txt b/paddle/fluid/operators/anakin/CMakeLists.txt deleted file mode 100644 index 5eacefc645bab288da7c289a5d7701abbcbef03d..0000000000000000000000000000000000000000 --- a/paddle/fluid/operators/anakin/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -op_library(anakin_engine_op DEPS anakin_engine anakin_op_converter) -# file(APPEND ${pybind_file} "USE_NO_KERNEL_OP(anakin_engine);\n") diff --git a/paddle/fluid/operators/anakin/anakin_engine_op.cc b/paddle/fluid/operators/anakin/anakin_engine_op.cc deleted file mode 100644 index 58db16ea0c1347a366a4d5927e414d76864cb6ab..0000000000000000000000000000000000000000 --- a/paddle/fluid/operators/anakin/anakin_engine_op.cc +++ /dev/null @@ -1,53 +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. */ - -#ifdef PADDLE_WITH_CUDA - -#include -#include - -#include "paddle/fluid/operators/anakin/anakin_engine_op.h" - -namespace paddle { - -namespace operators { - -class AnakinEngineOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("Xs", "A list of inputs.").AsDuplicable(); - AddOutput("Ys", "A list of outputs").AsDuplicable(); - AddAttr("subgraph", "the subgraph."); - AddAttr( - "engine_key", - "The engine_key here is used to distinguish different TRT Engines"); - AddAttr("sub_block", "the trt block"); - AddComment("Anakin engine operator."); - } -}; - -class AnakinEngineInferVarType : public framework::VarTypeInference { - public: - void operator()(framework::InferVarTypeContext *ctx) const override {} -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; - -REGISTER_OPERATOR(anakin_engine, ops::AnakinEngineOp, ops::AnakinEngineOpMaker, - ops::AnakinEngineOpMaker); - -#endif // PADDLE_WITH_CUDA diff --git a/paddle/fluid/operators/anakin/anakin_engine_op.h b/paddle/fluid/operators/anakin/anakin_engine_op.h deleted file mode 100644 index b4aaa228693c8f438a2df3dd316f68b2acaafcc2..0000000000000000000000000000000000000000 --- a/paddle/fluid/operators/anakin/anakin_engine_op.h +++ /dev/null @@ -1,138 +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. */ - -#pragma once - -#ifdef PADDLE_WITH_CUDA - -#include -#include -#include -#include -#include -#include -#include - -#include "paddle/fluid/framework/executor.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/framework/operator.h" -#include "paddle/fluid/inference/anakin/convert/op_converter.h" -#include "paddle/fluid/inference/anakin/engine.h" -#include "paddle/fluid/inference/analysis/helper.h" - -namespace paddle { -namespace operators { - -using inference::Singleton; -using inference::anakin::AnakinEngine; - -class AnakinEngineOp : public framework::OperatorBase { - private: - std::vector input_names_; - std::unordered_set param_names_; - std::string engine_key_; - std::string engine_serialized_data_; - bool use_gpu_; - bool enable_int8_; - - public: - AnakinEngineOp(const std::string &type, - const framework::VariableNameMap &inputs, - const framework::VariableNameMap &outputs, - const framework::AttributeMap &attrs) - : framework::OperatorBase(type, inputs, outputs, attrs) { - input_names_ = Inputs("Xs"); - engine_key_ = Attr("engine_key"); - auto params = Attr>("parameters"); - use_gpu_ = Attr("use_gpu"); - enable_int8_ = Attr("enable_int8"); - for (const auto ¶m : params) { - param_names_.insert(param); - } - } - - protected: - void RunImpl(const framework::Scope &scope, - const platform::Place &dev_place) const override { - RunAnakin(scope, dev_place); - } - - void RunAnakin(const framework::Scope &scope, - const platform::Place &dev_place) const { - PADDLE_ENFORCE(!input_names_.empty(), "should pass more than one inputs"); - - std::vector output_maps = - Attr>("output_name_mapping"); - - std::map inputs; - for (const auto &x : Inputs("Xs")) { - if (param_names_.count(x)) continue; - auto &t = - inference::analysis::GetFromScope(scope, x); - - inputs.insert({x, &t}); - } - - std::map outputs; - int output_index = 0; - for (const auto &y : Outputs("Ys")) { - auto *fluid_v = scope.FindVar(y); - PADDLE_ENFORCE_NOT_NULL(fluid_v, "no output variable called %s", y); - auto *fluid_t = fluid_v->GetMutable(); - outputs.insert({output_maps[output_index], fluid_t}); - output_index += 1; - } - if (enable_int8_) { - Execute<::anakin::Precision::INT8>(inputs, outputs, dev_place); - } else { - Execute<::anakin::Precision::FP32>(inputs, outputs, dev_place); - } - } - - template <::anakin::Precision PrecisionT> - void Execute(const std::map &inputs, - const std::map &outputs, - const platform::Place &dev_place) const { - if (use_gpu_) { -#ifdef PADDLE_WITH_CUDA - platform::DeviceContextPool &pool = - platform::DeviceContextPool::Instance(); - auto &dev_ctx = *pool.Get(dev_place); - auto stream = - reinterpret_cast(dev_ctx) - .stream(); - auto *engine = - inference::Singleton>::Global() - .Get(engine_key_); - engine->Execute(inputs, outputs, stream); -#endif - } else { -#ifdef ANAKIN_X86_PLACE - auto *engine = - inference::Singleton>::Global() - .Get(engine_key_); - engine->Execute(inputs, outputs); -#else - LOG(FATAL) << "Unknown Platform for AnakinEngine!"; -#endif - } - } -}; - -} // namespace operators -} // namespace paddle - -#endif // PADDLE_WITH_CUDA diff --git a/paddle/fluid/pybind/inference_api.cc b/paddle/fluid/pybind/inference_api.cc index 57547f03d621d342722f40d67083d5369041761b..46babdcc6efa81fa445307276b32bd757ffbd7ed 100644 --- a/paddle/fluid/pybind/inference_api.cc +++ b/paddle/fluid/pybind/inference_api.cc @@ -422,15 +422,6 @@ void BindAnalysisConfig(py::module *m) { py::arg("min_subgraph_size") = 3, py::arg("precision_mode") = AnalysisConfig::Precision::kFloat32, py::arg("use_static") = false, py::arg("use_calib_mode") = true) - .def("enable_anakin_engine", &AnalysisConfig::EnableAnakinEngine, - py::arg("max_batch_size") = 1, - py::arg("max_input_shape") = - std::map>(), - py::arg("min_subgraph_size") = 6, - py::arg("precision_mode") = AnalysisConfig::Precision::kFloat32, - py::arg("auto_config_layout") = false, - py::arg("passes_filter") = std::vector(), - py::arg("ops_filter") = std::vector()) .def("tensorrt_engine_enabled", &AnalysisConfig::tensorrt_engine_enabled) .def("switch_ir_debug", &AnalysisConfig::SwitchIrDebug, py::arg("x") = true)