Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into feature/polish_infer_shape
commit
69fd376bca
@ -0,0 +1,68 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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/operators/fill_constant_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class FillConstantOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(framework::InferShapeContext *ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasOutput("Out"),
|
||||
"Output(Out) of FillConstantOp should not be null.");
|
||||
auto &shape = ctx->Attrs().Get<std::vector<int>>("shape");
|
||||
std::vector<int64_t> shape_int64(shape.size(), 0);
|
||||
std::transform(shape.begin(), shape.end(), shape_int64.begin(),
|
||||
[](int a) { return static_cast<int64_t>(a); });
|
||||
auto dims = framework::make_ddim(shape_int64);
|
||||
ctx->SetOutputDim("Out", dims);
|
||||
}
|
||||
|
||||
framework::DataType IndicateDataType(
|
||||
const framework::ExecutionContext &ctx) const override {
|
||||
return static_cast<framework::DataType>(ctx.Attr<int>("dataType"));
|
||||
}
|
||||
};
|
||||
|
||||
class FillConstantOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
FillConstantOpMaker(framework::OpProto *proto,
|
||||
framework::OpAttrChecker *op_checker)
|
||||
: framework::OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddAttr<int>("dataType",
|
||||
"(int, default 5 (FP32)) "
|
||||
"Output data type")
|
||||
.SetDefault(framework::DataType::FP32);
|
||||
AddAttr<std::vector<int>>("shape", "(vector<int>) The shape of the output");
|
||||
AddAttr<float>("value", "(float, default 0) The value to be filled")
|
||||
.SetDefault(0.0f);
|
||||
AddOutput("Out",
|
||||
"(Tensor) Tensor of specified shape will be filled "
|
||||
"with the specified value");
|
||||
AddComment(R"DOC(Fill up a variable with specified constant value.)DOC");
|
||||
}
|
||||
};
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_WITHOUT_GRADIENT(fill_constant, ops::FillConstantOp,
|
||||
ops::FillConstantOpMaker);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
fill_constant,
|
||||
ops::FillConstantOpKernel<paddle::platform::CPUPlace, float>);
|
@ -0,0 +1,22 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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. */
|
||||
|
||||
#define EIGEN_USE_GPU
|
||||
#include "paddle/framework/op_registry.h"
|
||||
#include "paddle/operators/fill_constant_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
fill_constant,
|
||||
ops::FillConstantOpKernel<paddle::platform::GPUPlace, float>);
|
@ -0,0 +1,37 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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/framework/eigen.h"
|
||||
#include "paddle/framework/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
template <typename Place, typename T>
|
||||
class FillConstantOpKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& ctx) const override {
|
||||
auto* out = ctx.Output<framework::Tensor>("Out");
|
||||
out->mutable_data<T>(ctx.GetPlace());
|
||||
auto value = ctx.Attr<T>("value");
|
||||
|
||||
auto out_eigen = framework::EigenVector<T>::Flatten(*out);
|
||||
auto place = ctx.GetEigenDevice<Place>();
|
||||
out_eigen.device(place) = out_eigen.constant(static_cast<T>(value));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,113 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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/framework/op_registry.h"
|
||||
#include "paddle/operators/net_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class InterpOp : public NetOp {
|
||||
public:
|
||||
InterpOp(const std::string &type, const framework::VariableNameMap &inputs,
|
||||
const framework::VariableNameMap &outputs,
|
||||
const framework::AttributeMap &attrs)
|
||||
: NetOp(type, inputs, outputs, attrs) {
|
||||
PADDLE_ENFORCE_NE(Input("X"), framework::kEmptyVarName,
|
||||
"Input(X) of InterpOp should not be null.");
|
||||
PADDLE_ENFORCE_NE(Input("Y"), framework::kEmptyVarName,
|
||||
"Input(Y) of InterpOp should not be null.");
|
||||
PADDLE_ENFORCE_NE(Input("W"), framework::kEmptyVarName,
|
||||
"Input(W) of InterpOp should not be null.");
|
||||
PADDLE_ENFORCE_NE(Output("SubOut"), framework::kEmptyVarName,
|
||||
"Output(SubOut) of InterpOp should not be null.");
|
||||
PADDLE_ENFORCE_NE(Output("MulOut"), framework::kEmptyVarName,
|
||||
"Output(MulOut) of InterpOp should not be null.");
|
||||
PADDLE_ENFORCE_NE(Output("Out"), framework::kEmptyVarName,
|
||||
"Output(Out) of InterpOp should not be null.");
|
||||
|
||||
// SubOut = X - Y
|
||||
auto x = Input("X");
|
||||
auto y = Input("Y");
|
||||
auto sub_out = Output("SubOut");
|
||||
AppendOp(framework::OpRegistry::CreateOp(
|
||||
"elementwise_sub", {{"X", {x}}, {"Y", {y}}}, {{"Out", {sub_out}}}, {}));
|
||||
|
||||
// MulOut = SubOut * W = (X - Y) * W
|
||||
auto w = Input("W");
|
||||
auto mul_out = Output("MulOut");
|
||||
AppendOp(framework::OpRegistry::CreateOp(
|
||||
"elementwise_mul", {{"X", {sub_out}}, {"Y", {w}}}, {{"Out", {mul_out}}},
|
||||
{{"axis", 0}}));
|
||||
|
||||
// Out = MulOut + Y = (X - Y) * W + Y = X * W + Y * (1 - W)
|
||||
AppendOp(framework::OpRegistry::CreateOp("elementwise_add",
|
||||
{{"X", {mul_out}}, {"Y", {y}}},
|
||||
{{"Out", {Output("Out")}}}, {}));
|
||||
|
||||
CompleteAddOp(false);
|
||||
}
|
||||
};
|
||||
|
||||
class InterpOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
InterpOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
|
||||
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput("X",
|
||||
"(Tensor), 2-D Matrix of shape [batch_size, data_dim]"
|
||||
"containing data samples, the first input of interp_op");
|
||||
AddInput("Y",
|
||||
"(Tensor), 2-D Matrix of shape `[batch_size, data_dim]`"
|
||||
"containing data samples, the second input of interp_op");
|
||||
AddInput("W",
|
||||
"(Tensor), 1-D Vector of shape [batch_size],"
|
||||
"the interpolated values in the half-open interval [0.0, 1.0)");
|
||||
AddOutput("SubOut",
|
||||
"(Tensor), the intermediate subtraction outputs, saving X - Y.")
|
||||
.AsIntermediate();
|
||||
AddOutput("MulOut",
|
||||
"(Tensor), the intermediate multiplication outputs,"
|
||||
"saving the elementwise multiplication of (X - Y) and W.")
|
||||
.AsIntermediate();
|
||||
AddOutput("Out",
|
||||
"(Tensor), the output of interp_op, same shape with X,"
|
||||
"returns the first-dimensional piecewise linear interpolant "
|
||||
"between X and Y");
|
||||
AddComment(R"DOC(
|
||||
Linear Interpolation with two inputs, used in NEURAL TURING MACHINE.
|
||||
|
||||
Equation:
|
||||
Out.row[i] = X.row[i] * W[i] + Y.row[i] * (1 - W[i])
|
||||
= (X.row[i] - Y.row[i]) * W[i] + Y.row[i]
|
||||
|
||||
Example:
|
||||
X = [[1,2],[3,4]],
|
||||
Y = [[2,1],[4,3]],
|
||||
W = [0.3, 0.4]
|
||||
|
||||
Then, Out = [[1.7,1.3],[3.6,3.4]]
|
||||
|
||||
where 1.7 = 1*0.3+2*(1-0.3),
|
||||
1.3 = 2*0.3+1*(1-0.3),
|
||||
3.6 = 3*0.4+4*(1-0.4),
|
||||
3.4 = 4*0.4+3*(1-0.4)
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_WITHOUT_GRADIENT(interp, ops::InterpOp, ops::InterpOpMaker);
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,228 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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/operators/pool_with_index_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
inline int OutputSizeMaxPool(int input_size, int filter_size, int padding,
|
||||
int stride) {
|
||||
int output_size = (input_size - filter_size + 2 * padding) / stride + 1;
|
||||
return output_size;
|
||||
}
|
||||
|
||||
class MaxPoolWithIndexOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(framework::InferShapeContext *ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("X"),
|
||||
"X(Input) of Pooling should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("Out"),
|
||||
"Out(Output) of Pooling should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("Mask"),
|
||||
"Mask(Output) of Pooling should not be null.");
|
||||
|
||||
auto in_x_dims = ctx->GetInputDim("X");
|
||||
|
||||
std::vector<int> ksize = ctx->Attrs().Get<std::vector<int>>("ksize");
|
||||
std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides");
|
||||
std::vector<int> paddings = ctx->Attrs().Get<std::vector<int>>("paddings");
|
||||
|
||||
PADDLE_ENFORCE(in_x_dims.size() == 4 || in_x_dims.size() == 5,
|
||||
"Pooling intput should be 4-D or 5-D");
|
||||
|
||||
if (ctx->Attrs().Get<bool>("globalPooling")) {
|
||||
ksize.resize(static_cast<size_t>(in_x_dims.size()) - 2);
|
||||
for (size_t i = 0; i < ksize.size(); ++i)
|
||||
ksize[i] = static_cast<int>(in_x_dims[i + 2]);
|
||||
}
|
||||
|
||||
PADDLE_ENFORCE(in_x_dims.size() - ksize.size() == 2U,
|
||||
"Intput size and pooling size should be consistent.");
|
||||
PADDLE_ENFORCE_EQ(ksize.size(), strides.size(),
|
||||
"Strides size and pooling size should be the same.");
|
||||
PADDLE_ENFORCE_EQ(ksize.size(), paddings.size(),
|
||||
"Paddings size and pooling size should be the same.");
|
||||
|
||||
std::vector<int64_t> output_shape({in_x_dims[0], in_x_dims[1]});
|
||||
for (size_t i = 0; i < ksize.size(); ++i) {
|
||||
output_shape.push_back(OutputSizeMaxPool(in_x_dims[i + 2], ksize[i],
|
||||
paddings[i], strides[i]));
|
||||
}
|
||||
ctx->SetOutputDim("Out", framework::make_ddim(output_shape));
|
||||
ctx->SetOutputDim("Mask", framework::make_ddim(output_shape));
|
||||
}
|
||||
};
|
||||
|
||||
class MaxPoolWithIndexOpGrad : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(framework::InferShapeContext *ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) must not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
|
||||
"Input(X@GRAD) should not be null.");
|
||||
ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
|
||||
}
|
||||
};
|
||||
|
||||
class MaxPool2dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
MaxPool2dWithIndexOpMaker(framework::OpProto *proto,
|
||||
framework::OpAttrChecker *op_checker)
|
||||
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput(
|
||||
"X",
|
||||
"The input tensor of pooling operator. "
|
||||
"The format of input tensor is NCHW. Where N is batch size, C is the "
|
||||
"number of channels, H and W is the height and width of image.");
|
||||
AddOutput("Out",
|
||||
"The output tensor of pooling operator."
|
||||
"The format of output tensor is also NCHW."
|
||||
"Where N is batch size, C is "
|
||||
"the number of channels, H and W is the height and "
|
||||
"width of image.");
|
||||
AddOutput("Mask",
|
||||
"The Mask tensor of pooling operator."
|
||||
"The format of output tensor is also NCHW."
|
||||
"Where N is batch size, C is the number of channels, H and W "
|
||||
"is the height and width of image."
|
||||
"The value in it is the index in current feature map");
|
||||
|
||||
AddAttr<std::vector<int>>(
|
||||
"ksize",
|
||||
"The pooling size(height, width) of pooling operator."
|
||||
"If globalPooling = true, ksize is ignored and need not be "
|
||||
"specified."); // TODO(Chengduo): Add checker. (Currently,
|
||||
// TypedAttrChecker don't support vector type.)
|
||||
AddAttr<bool>(
|
||||
"globalPooling",
|
||||
"Whether to use the globalPooling."
|
||||
"Bool constant equal to false or true."
|
||||
"Default false."
|
||||
"If globalPooling = true, ksize is ignored and need not be specified.")
|
||||
.SetDefault(false);
|
||||
AddAttr<std::vector<int>>("strides",
|
||||
"Strides(height, width) of pooling operator."
|
||||
"Default {1,1}.")
|
||||
.SetDefault({1, 1}); // TODO(Chengduo): Add checker. (Currently,
|
||||
// TypedAttrChecker don't support vector type.)
|
||||
AddAttr<std::vector<int>>("paddings",
|
||||
"Paddings(height, width) of pooling operator."
|
||||
"Default {0,0}.")
|
||||
.SetDefault({0, 0}); // TODO(Chengduo): Add checker. (Currently,
|
||||
// TypedAttrChecker don't support vector type.)
|
||||
|
||||
AddComment(R"DOC(
|
||||
The maxPooling2d with index operation calculates the output and the mask
|
||||
based on the input and ksize, strides, paddings parameters. Input(X) and
|
||||
output(Out, Mask) are in NCHW format. Where N is batch size, C is the
|
||||
number of channels, H and W is the height and width of feature.
|
||||
Parameters(ksize, strides, paddings) are two elements.
|
||||
These two elements represent height and width, respectively.
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
|
||||
class MaxPool3dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
MaxPool3dWithIndexOpMaker(framework::OpProto *proto,
|
||||
framework::OpAttrChecker *op_checker)
|
||||
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput(
|
||||
"X",
|
||||
"The input tensor of pooling operator. "
|
||||
"The format of input tensor is NCDHW. Where N is batch size, C is "
|
||||
"the number of channels, D, H and W is the depth, height and width of "
|
||||
"image.");
|
||||
AddOutput("Out",
|
||||
"The output tensor of pooling operator."
|
||||
"The format of output tensor is also NCDHW."
|
||||
"Where N is batch size, C is "
|
||||
"the number of channels, D, H and W is the depth, height and "
|
||||
"width of image.");
|
||||
AddOutput("Mask",
|
||||
"The Mask tensor of pooling operator."
|
||||
"The format of output tensor is also NCDHW."
|
||||
"Where N is batch size, C is the number of channels, D, H and W "
|
||||
"is the depth, height and width of image."
|
||||
"The value in it is the index in current feature map");
|
||||
|
||||
AddAttr<std::vector<int>>(
|
||||
"ksize",
|
||||
"The pooling size(depth, height, width) of pooling operator."
|
||||
"If globalPooling = true, ksize is ignored and need not be "
|
||||
"specified."); // TODO(Chengduo): Add checker. (Currently,
|
||||
// TypedAttrChecker don't support vector type.)
|
||||
AddAttr<bool>(
|
||||
"globalPooling",
|
||||
"Whether to use the globalPooling."
|
||||
"Bool constant equal to false or true."
|
||||
"Default false."
|
||||
"If globalPooling = true, ksize is ignored and need not be specified.")
|
||||
.SetDefault(false);
|
||||
AddAttr<std::vector<int>>(
|
||||
"strides",
|
||||
"Strides(depth, height, width) of pooling operator."
|
||||
"Default {1,1,1}.")
|
||||
.SetDefault({1, 1, 1}); // TODO(Chengduo): Add checker. (Currently,
|
||||
// TypedAttrChecker don't support vector type.)
|
||||
AddAttr<std::vector<int>>(
|
||||
"paddings",
|
||||
"Paddings(depth, height, width) of pooling operator."
|
||||
"Default {0,0,0}.")
|
||||
.SetDefault({0, 0, 0}); // TODO(Chengduo): Add checker. (Currently,
|
||||
// TypedAttrChecker don't support vector type.)
|
||||
|
||||
AddComment(R"DOC(
|
||||
The maxpooling3d with index operation calculates the output and the mask
|
||||
based on the input and ksize, strides, paddings parameters.
|
||||
Input(X) and output(Out, Mask) are in NCDHW format. Where N is batch
|
||||
size, C is the number of channels, D, H and W is the depth, height and
|
||||
width of feature. Parameters(ksize, strides, paddings) are three elements.
|
||||
These three elements represent depth, height and width, respectively.
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
|
||||
REGISTER_OP(max_pool2d_with_index, ops::MaxPoolWithIndexOp,
|
||||
ops::MaxPool2dWithIndexOpMaker, max_pool2d_with_index_grad,
|
||||
ops::MaxPoolWithIndexOpGrad);
|
||||
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
max_pool2d_with_index,
|
||||
ops::MaxPoolWithIndexKernel<paddle::platform::CPUPlace, float>);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
max_pool2d_with_index_grad,
|
||||
ops::MaxPoolWithIndexGradKernel<paddle::platform::CPUPlace, float>)
|
||||
|
||||
REGISTER_OP(max_pool3d_with_index, ops::MaxPoolWithIndexOp,
|
||||
ops::MaxPool3dWithIndexOpMaker, max_pool3d_with_index_grad,
|
||||
ops::MaxPoolWithIndexOpGrad);
|
||||
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
max_pool3d_with_index,
|
||||
ops::MaxPoolWithIndexKernel<paddle::platform::CPUPlace, float>);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
max_pool3d_with_index_grad,
|
||||
ops::MaxPoolWithIndexGradKernel<paddle::platform::CPUPlace, float>)
|
@ -0,0 +1,31 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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/operators/pool_with_index_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
max_pool2d_with_index,
|
||||
ops::MaxPoolWithIndexKernel<paddle::platform::GPUPlace, float>);
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
max_pool2d_with_index_grad,
|
||||
ops::MaxPoolWithIndexGradKernel<paddle::platform::GPUPlace, float>)
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
max_pool3d_with_index,
|
||||
ops::MaxPoolWithIndexKernel<paddle::platform::GPUPlace, float>);
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
max_pool3d_with_index_grad,
|
||||
ops::MaxPoolWithIndexGradKernel<paddle::platform::GPUPlace, float>)
|
@ -0,0 +1,103 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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/framework/eigen.h"
|
||||
#include "paddle/framework/op_registry.h"
|
||||
#include "paddle/operators/math/math_function.h"
|
||||
#include "paddle/operators/math/pooling.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using Tensor = framework::Tensor;
|
||||
|
||||
template <typename Place, typename T>
|
||||
class MaxPoolWithIndexKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& context) const override {
|
||||
const Tensor* in_x = context.Input<Tensor>("X");
|
||||
Tensor* out = context.Output<Tensor>("Out");
|
||||
Tensor* mask = context.Output<Tensor>("Mask");
|
||||
|
||||
std::vector<int> ksize = context.Attr<std::vector<int>>("ksize");
|
||||
std::vector<int> strides = context.Attr<std::vector<int>>("strides");
|
||||
std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
|
||||
if (context.Attr<bool>("globalPooling")) {
|
||||
for (size_t i = 0; i < ksize.size(); ++i) {
|
||||
ksize[i] = static_cast<int>(in_x->dims()[i + 2]);
|
||||
}
|
||||
}
|
||||
|
||||
switch (ksize.size()) {
|
||||
case 2: {
|
||||
paddle::operators::math::MaxPool2dWithIndexFunctor<Place, T>
|
||||
pool2d_forward;
|
||||
pool2d_forward(context.device_context(), *in_x, *out, *mask, ksize,
|
||||
strides, paddings);
|
||||
} break;
|
||||
case 3: {
|
||||
paddle::operators::math::MaxPool3dWithIndexFunctor<Place, T>
|
||||
pool3d_forward;
|
||||
pool3d_forward(context.device_context(), *in_x, *out, *mask, ksize,
|
||||
strides, paddings);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Place, typename T>
|
||||
class MaxPoolWithIndexGradKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& context) const override {
|
||||
const Tensor* mask = context.Input<Tensor>("Mask");
|
||||
const Tensor* out_grad =
|
||||
context.Input<Tensor>(framework::GradVarName("Out"));
|
||||
Tensor* in_x_grad = context.Output<Tensor>(framework::GradVarName("X"));
|
||||
|
||||
std::vector<int> ksize = context.Attr<std::vector<int>>("ksize");
|
||||
std::vector<int> strides = context.Attr<std::vector<int>>("strides");
|
||||
std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
|
||||
if (context.Attr<bool>("globalPooling")) {
|
||||
for (size_t i = 0; i < ksize.size(); ++i) {
|
||||
ksize[i] = static_cast<int>(in_x_grad->dims()[i + 2]);
|
||||
}
|
||||
}
|
||||
|
||||
if (in_x_grad) {
|
||||
in_x_grad->mutable_data<T>(context.GetPlace());
|
||||
auto temp = framework::EigenVector<T>::Flatten(*in_x_grad);
|
||||
temp.device(context.GetEigenDevice<Place>()) =
|
||||
temp.constant(static_cast<T>(0));
|
||||
|
||||
switch (ksize.size()) {
|
||||
case 2: {
|
||||
paddle::operators::math::MaxPool2dWithIndexGradFunctor<Place, T>
|
||||
pool2d_backward;
|
||||
pool2d_backward(context.device_context(), *in_x_grad, *out_grad,
|
||||
*mask, ksize, strides, paddings);
|
||||
} break;
|
||||
case 3: {
|
||||
paddle::operators::math::MaxPool3dWithIndexGradFunctor<Place, T>
|
||||
pool3d_backward;
|
||||
pool3d_backward(context.device_context(), *in_x_grad, *out_grad,
|
||||
*mask, ksize, strides, paddings);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue