Skip to content

Commit

Permalink
Merge pull request #7 from PaddlePaddle/feature/anakin-engine
Browse files Browse the repository at this point in the history
Feature/anakin engine
  • Loading branch information
Shixiaowei02 committed Mar 24, 2019
2 parents 1946d67 + e611eb3 commit 9166f6f
Show file tree
Hide file tree
Showing 50 changed files with 127 additions and 62 deletions.
2 changes: 1 addition & 1 deletion paddle/fluid/inference/anakin/convert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ cc_test(test_anakin_flatten SRCS test_flatten_op.cc DEPS anakin_op_converter fla
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_im2sequence SRCS test_im2sequence_op.cc DEPS anakin_op_converter im2sequence_op im2col)
# cc_test(test_anakin_im2sequence SRCS test_im2sequence_op.cc DEPS anakin_op_converter im2sequence_op im2col)
cc_test(test_anakin_sum SRCS test_sum_op.cc DEPS anakin_op_converter sum_op selected_rows_functor)
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/activation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ ActivationOpConverter::ActivationOpConverter(const std::string &op_type)
}

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/activation.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ActivationOpConverter : public AnakinOpConverter {
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() {}
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/batch_norm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/batch_norm.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BatchNormOpConverter : public AnakinOpConverter {
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() {}
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/concat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/concat.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ConcatOpConverter : public AnakinOpConverter {
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() {}
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/conv2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/conv2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Conv2dOpConverter : public AnakinOpConverter {
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() {}
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/conv2d_fusion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/conv2d_fusion.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Conv2dFusionOpConverter : public AnakinOpConverter {
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() {}
Expand Down
6 changes: 3 additions & 3 deletions paddle/fluid/inference/anakin/convert/density_prior_box.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ namespace paddle {
namespace inference {
namespace anakin {

void DensityPriorBoxOpConverter::operator()(const framework::proto::OpDesc& op,
const framework::Scope& scope,
bool test_mode) {
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();
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/density_prior_box.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class DensityPriorBoxOpConverter : public AnakinOpConverter {
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() {}
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/detection_out.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/detection_out.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class DetectionOutOpConverter : public AnakinOpConverter {
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() {}
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/dropout.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/dropout.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class DropoutOpConverter : public AnakinOpConverter {
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() {}
Expand Down
12 changes: 6 additions & 6 deletions paddle/fluid/inference/anakin/convert/elementwise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ namespace paddle {
namespace inference {
namespace anakin {

void ElementwiseAddOpConverter::operator()(const framework::proto::OpDesc &op,
const framework::Scope &scope,
bool test_mode) {
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);
Expand All @@ -50,9 +50,9 @@ void ElementwiseAddOpConverter::operator()(const framework::proto::OpDesc &op,
engine_->AddOpAttr<PTuple<float>>(op_name, "coeff", coeff);
}

void ElementwiseMulOpConverter::operator()(const framework::proto::OpDesc &op,
const framework::Scope &scope,
bool test_mode) {
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);
Expand Down
2 changes: 2 additions & 0 deletions paddle/fluid/inference/anakin/convert/elementwise.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ElementwiseAddOpConverter : public AnakinOpConverter {
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() {}
Expand All @@ -37,6 +38,7 @@ class ElementwiseMulOpConverter : public AnakinOpConverter {
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() {}
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/fc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/fc.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class FcBaseOpConverter : public AnakinOpConverter {
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() {}
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/flatten.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/flatten.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class FlattenOpConverter : public AnakinOpConverter {
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() {}
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/im2sequence.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/im2sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Im2SequenceConverter : public AnakinOpConverter {
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() {}
Expand Down
16 changes: 9 additions & 7 deletions paddle/fluid/inference/anakin/convert/op_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ class AnakinOpConverter {
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<std::string> &parameters,
const framework::Scope &scope, AnakinNvEngine *engine,
bool test_mode = false) {
Expand All @@ -58,16 +60,17 @@ class AnakinOpConverter {
}
PADDLE_ENFORCE_NOT_NULL(it, "no OpConverter for optype [%s]", op_type);
it->SetEngine(engine);
(*it)(op, scope, test_mode);
(*it)(op, block_desc, scope, test_mode);
}

void ConvertBlock(const framework::proto::BlockDesc &block,
void ConvertBlock(framework::BlockDesc *block_desc,
const std::unordered_set<std::string> &parameters,
const framework::Scope &scope, AnakinNvEngine *engine) {
std::unique_lock<std::mutex> lock(mutex_);
for (auto i = 0; i < block.ops_size(); i++) {
auto &op = block.ops(i);
ConvertOp(op, parameters, scope, engine);
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);
}
}

Expand All @@ -77,8 +80,7 @@ class AnakinOpConverter {
const std::vector<std::string> &inputs,
const std::unordered_set<std::string> &parameters,
const std::vector<std::string> &outputs, AnakinNvEngine *engine) {
framework::proto::BlockDesc *block_proto = block_desc->Proto();
ConvertBlock(*block_proto, parameters, *scope, engine);
ConvertBlock(block_desc, parameters, *scope, engine);

engine->Freeze();
// if the max_batch size
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/pool2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/pool2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Pool2dOpConverter : public AnakinOpConverter {
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() {}
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/relu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/relu.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ReluOpConverter : public AnakinOpConverter {
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() {}
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/reshape.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/reshape.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ReshapeOpConverter : public AnakinOpConverter {
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() {}
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/scale.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/scale.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ScaleOpConverter : public AnakinOpConverter {
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() {}
Expand Down
11 changes: 10 additions & 1 deletion paddle/fluid/inference/anakin/convert/softmax.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace inference {
namespace anakin {

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);
Expand All @@ -32,8 +33,16 @@ void SoftMaxOpConverter::operator()(const framework::proto::OpDesc &op,
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();

engine_->AddOp(op_name, "Softmax", {input}, {output});
engine_->AddOpAttr(op_name, "axis", 2);
engine_->AddOpAttr(op_name, "axis", static_cast<int>(input_dims - 1));
}

} // namespace anakin
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/softmax.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class SoftMaxOpConverter : public AnakinOpConverter {
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() {}
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/split.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/split.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class SplitOpConverter : public AnakinOpConverter {
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() {}
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/anakin/convert/sum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace inference {
namespace anakin {

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);
Expand Down
Loading

0 comments on commit 9166f6f

Please sign in to comment.