git cherry-pick from feature/anakin-engine: update anakin subgraph #16278
parent
c407dfa3cb
commit
07dcf2856c
@ -0,0 +1,66 @@
|
|||||||
|
// 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 <algorithm>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using anakin::graph::GraphGlobalMem;
|
||||||
|
using anakin::AK_FLOAT;
|
||||||
|
using anakin::Precision;
|
||||||
|
using anakin::saber::NV;
|
||||||
|
using anakin::saber::X86;
|
||||||
|
using anakin::saber::Shape;
|
||||||
|
using anakin::PBlock;
|
||||||
|
using anakin::PTuple;
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace inference {
|
||||||
|
namespace anakin {
|
||||||
|
|
||||||
|
void DropoutOpConverter::operator()(const framework::proto::OpDesc &op,
|
||||||
|
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();
|
||||||
|
|
||||||
|
engine_->AddOp(op_name, "Scale", {x_name}, {out_name});
|
||||||
|
|
||||||
|
auto dropout_prob = boost::get<float>(op_desc.GetAttr("dropout_prob"));
|
||||||
|
auto factor = 1 - dropout_prob;
|
||||||
|
Shape shape1(std::vector<int>({1, 1, 1, 1}));
|
||||||
|
auto *weight1 =
|
||||||
|
GraphGlobalMem<NV>::Global().template new_block<AK_FLOAT>(shape1);
|
||||||
|
auto *factor_data = static_cast<float *>(weight1->h_tensor().mutable_data());
|
||||||
|
float weight1_data[] = {factor};
|
||||||
|
std::copy(std::begin(weight1_data), std::end(weight1_data), factor_data);
|
||||||
|
|
||||||
|
engine_->AddOpAttr(op_name, "weight_1", *weight1);
|
||||||
|
engine_->AddOpAttr(op_name, "axis", 0);
|
||||||
|
engine_->AddOpAttr(op_name, "num_axes", 0);
|
||||||
|
engine_->AddOpAttr(op_name, "bias_term", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace anakin
|
||||||
|
} // namespace inference
|
||||||
|
} // namespace paddle
|
||||||
|
|
||||||
|
REGISTER_ANAKIN_OP_CONVERTER(dropout, DropoutOpConverter);
|
@ -0,0 +1,37 @@
|
|||||||
|
// 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 {
|
||||||
|
|
||||||
|
class DropoutOpConverter : public AnakinOpConverter {
|
||||||
|
public:
|
||||||
|
DropoutOpConverter() = default;
|
||||||
|
|
||||||
|
virtual void operator()(const framework::proto::OpDesc &op,
|
||||||
|
const framework::Scope &scope,
|
||||||
|
bool test_mode) override;
|
||||||
|
virtual ~DropoutOpConverter() {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace anakin
|
||||||
|
} // namespace inference
|
||||||
|
} // namespace paddle
|
@ -0,0 +1,62 @@
|
|||||||
|
// 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 <algorithm>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using anakin::graph::GraphGlobalMem;
|
||||||
|
using anakin::AK_FLOAT;
|
||||||
|
using anakin::Precision;
|
||||||
|
using anakin::saber::NV;
|
||||||
|
using anakin::saber::X86;
|
||||||
|
using anakin::saber::Shape;
|
||||||
|
using anakin::PBlock;
|
||||||
|
using anakin::PTuple;
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace inference {
|
||||||
|
namespace anakin {
|
||||||
|
|
||||||
|
void Im2SequenceConverter::operator()(const framework::proto::OpDesc &op,
|
||||||
|
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();
|
||||||
|
|
||||||
|
engine_->AddOp(op_name, "Im2Sequence", {x_name}, {out_name});
|
||||||
|
|
||||||
|
std::vector<int> dilations = {1, 1};
|
||||||
|
auto paddings = boost::get<std::vector<int>>(op_desc.GetAttr("paddings"));
|
||||||
|
auto strides = boost::get<std::vector<int>>(op_desc.GetAttr("strides"));
|
||||||
|
auto kernels = boost::get<std::vector<int>>(op_desc.GetAttr("kernels"));
|
||||||
|
|
||||||
|
engine_->AddOpAttr<PTuple<int>>(op_name, "paddings", paddings);
|
||||||
|
engine_->AddOpAttr<PTuple<int>>(op_name, "strides", strides);
|
||||||
|
engine_->AddOpAttr<PTuple<int>>(op_name, "window_size", kernels);
|
||||||
|
engine_->AddOpAttr<PTuple<int>>(op_name, "dilations", dilations);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace anakin
|
||||||
|
} // namespace inference
|
||||||
|
} // namespace paddle
|
||||||
|
|
||||||
|
REGISTER_ANAKIN_OP_CONVERTER(im2sequence, Im2SequenceConverter);
|
@ -0,0 +1,37 @@
|
|||||||
|
// 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 {
|
||||||
|
|
||||||
|
class Im2SequenceConverter : public AnakinOpConverter {
|
||||||
|
public:
|
||||||
|
Im2SequenceConverter() = default;
|
||||||
|
|
||||||
|
virtual void operator()(const framework::proto::OpDesc &op,
|
||||||
|
const framework::Scope &scope,
|
||||||
|
bool test_mode) override;
|
||||||
|
virtual ~Im2SequenceConverter() {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace anakin
|
||||||
|
} // namespace inference
|
||||||
|
} // namespace paddle
|
@ -0,0 +1,54 @@
|
|||||||
|
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include "paddle/fluid/inference/anakin/convert/sum.h"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using anakin::graph::GraphGlobalMem;
|
||||||
|
using anakin::AK_FLOAT;
|
||||||
|
using anakin::Precision;
|
||||||
|
using anakin::saber::NV;
|
||||||
|
using anakin::saber::X86;
|
||||||
|
using anakin::saber::Shape;
|
||||||
|
using anakin::PBlock;
|
||||||
|
using anakin::PTuple;
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace inference {
|
||||||
|
namespace anakin {
|
||||||
|
|
||||||
|
void SumOpConverter::operator()(const framework::proto::OpDesc &op,
|
||||||
|
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<float> coeff = {1, 1};
|
||||||
|
std::string elementwise_type = "Add";
|
||||||
|
engine_->AddOp(op_name, "Eltwise", input_names, {out_name});
|
||||||
|
engine_->AddOpAttr<PTuple<float>>(op_name, "coeff", coeff);
|
||||||
|
engine_->AddOpAttr<std::string>(op_name, "type", elementwise_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace anakin
|
||||||
|
} // namespace inference
|
||||||
|
} // namespace paddle
|
||||||
|
|
||||||
|
REGISTER_ANAKIN_OP_CONVERTER(sum, SumOpConverter);
|
@ -0,0 +1,37 @@
|
|||||||
|
// 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 {
|
||||||
|
|
||||||
|
class SumOpConverter : public AnakinOpConverter {
|
||||||
|
public:
|
||||||
|
SumOpConverter() = default;
|
||||||
|
|
||||||
|
virtual void operator()(const framework::proto::OpDesc &op,
|
||||||
|
const framework::Scope &scope,
|
||||||
|
bool test_mode) override;
|
||||||
|
virtual ~SumOpConverter() {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace anakin
|
||||||
|
} // namespace inference
|
||||||
|
} // namespace paddle
|
@ -0,0 +1,53 @@
|
|||||||
|
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License. */
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#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 {
|
||||||
|
|
||||||
|
TEST(dropout_op, native) {
|
||||||
|
std::unordered_set<std::string> parameters;
|
||||||
|
framework::Scope scope;
|
||||||
|
AnakinConvertValidation validator(parameters, scope);
|
||||||
|
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<std::string> neglected_output = {"mask"};
|
||||||
|
validator.Execute(1, neglected_output);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace anakin
|
||||||
|
} // namespace inference
|
||||||
|
} // namespace paddle
|
||||||
|
|
||||||
|
USE_OP(dropout);
|
||||||
|
USE_ANAKIN_CONVERTER(dropout);
|
@ -0,0 +1,55 @@
|
|||||||
|
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License. */
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#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<std::string> parameters;
|
||||||
|
framework::Scope scope;
|
||||||
|
AnakinConvertValidation validator(parameters, scope);
|
||||||
|
|
||||||
|
std::vector<int> kernels = {6, 1};
|
||||||
|
std::vector<int> strides = {1, 1};
|
||||||
|
std::vector<int> 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);
|
@ -0,0 +1,48 @@
|
|||||||
|
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License. */
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include "paddle/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 {
|
||||||
|
|
||||||
|
TEST(sum, native) {
|
||||||
|
std::unordered_set<std::string> parameters;
|
||||||
|
framework::Scope scope;
|
||||||
|
AnakinConvertValidation validator(parameters, scope);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace anakin
|
||||||
|
} // namespace inference
|
||||||
|
} // namespace paddle
|
||||||
|
|
||||||
|
USE_OP(sum);
|
||||||
|
USE_ANAKIN_CONVERTER(sum);
|
Loading…
Reference in new issue