提交 84a2512b 编写于 作者: C chengduoZH

fix parameter name and function define

上级 50b8ec05
...@@ -21,10 +21,10 @@ namespace math { ...@@ -21,10 +21,10 @@ namespace math {
template <typename PoolProcess, typename T> template <typename PoolProcess, typename T>
class Pool2dForwardFunctor<platform::CPUPlace, PoolProcess, T> { class Pool2dForwardFunctor<platform::CPUPlace, PoolProcess, T> {
public: public:
void operator()(const framework::Tensor& input, framework::Tensor& output, void operator()(const platform::DeviceContext& context,
const framework::Tensor& input, framework::Tensor& output,
std::vector<int>& ksize, std::vector<int>& strides, std::vector<int>& ksize, std::vector<int>& strides,
std::vector<int>& paddings, PoolProcess pool_process, std::vector<int>& paddings, PoolProcess pool_process) {
const platform::DeviceContext& context) {
const int batch_size = input.dims()[0]; const int batch_size = input.dims()[0];
const int input_height = input.dims()[2]; const int input_height = input.dims()[2];
const int input_width = input.dims()[3]; const int input_width = input.dims()[3];
...@@ -75,12 +75,12 @@ class Pool2dForwardFunctor<platform::CPUPlace, PoolProcess, T> { ...@@ -75,12 +75,12 @@ class Pool2dForwardFunctor<platform::CPUPlace, PoolProcess, T> {
template <typename PoolProcess, class T> template <typename PoolProcess, class T>
class Pool2dBackwardFunctor<platform::CPUPlace, PoolProcess, T> { class Pool2dBackwardFunctor<platform::CPUPlace, PoolProcess, T> {
public: public:
void operator()(const framework::Tensor& input, framework::Tensor& input_grad, void operator()(const platform::DeviceContext& context,
const framework::Tensor& input, framework::Tensor& input_grad,
const framework::Tensor& output, const framework::Tensor& output,
const framework::Tensor& output_grad, std::vector<int>& ksize, const framework::Tensor& output_grad, std::vector<int>& ksize,
std::vector<int>& strides, std::vector<int>& paddings, std::vector<int>& strides, std::vector<int>& paddings,
PoolProcess pool_process, PoolProcess pool_process) {
const platform::DeviceContext& context) {
const int batch_size = input.dims()[0]; const int batch_size = input.dims()[0];
const int input_height = input.dims()[2]; const int input_height = input.dims()[2];
const int input_width = input.dims()[3]; const int input_width = input.dims()[3];
...@@ -154,10 +154,10 @@ template class Pool2dBackwardFunctor< ...@@ -154,10 +154,10 @@ template class Pool2dBackwardFunctor<
template <typename PoolProcess, class T> template <typename PoolProcess, class T>
class Pool3dForwardFunctor<platform::CPUPlace, PoolProcess, T> { class Pool3dForwardFunctor<platform::CPUPlace, PoolProcess, T> {
public: public:
void operator()(const framework::Tensor& input, framework::Tensor& output, void operator()(const platform::DeviceContext& context,
const framework::Tensor& input, framework::Tensor& output,
std::vector<int>& ksize, std::vector<int>& strides, std::vector<int>& ksize, std::vector<int>& strides,
std::vector<int>& paddings, PoolProcess pool_process, std::vector<int>& paddings, PoolProcess pool_process) {
const platform::DeviceContext& context) {
const int batch_size = input.dims()[0]; const int batch_size = input.dims()[0];
const int input_depth = input.dims()[2]; const int input_depth = input.dims()[2];
const int input_height = input.dims()[3]; const int input_height = input.dims()[3];
...@@ -224,12 +224,12 @@ class Pool3dForwardFunctor<platform::CPUPlace, PoolProcess, T> { ...@@ -224,12 +224,12 @@ class Pool3dForwardFunctor<platform::CPUPlace, PoolProcess, T> {
template <typename PoolProcess, class T> template <typename PoolProcess, class T>
class Pool3dBackwardFunctor<platform::CPUPlace, PoolProcess, T> { class Pool3dBackwardFunctor<platform::CPUPlace, PoolProcess, T> {
public: public:
void operator()(const framework::Tensor& input, framework::Tensor& input_grad, void operator()(const platform::DeviceContext& context,
const framework::Tensor& input, framework::Tensor& input_grad,
const framework::Tensor& output, const framework::Tensor& output,
const framework::Tensor& output_grad, std::vector<int>& ksize, const framework::Tensor& output_grad, std::vector<int>& ksize,
std::vector<int>& strides, std::vector<int>& paddings, std::vector<int>& strides, std::vector<int>& paddings,
PoolProcess pool_process, PoolProcess pool_process) {
const platform::DeviceContext& context) {
const int batch_size = input.dims()[0]; const int batch_size = input.dims()[0];
const int input_depth = input.dims()[2]; const int input_depth = input.dims()[2];
const int input_height = input.dims()[3]; const int input_height = input.dims()[3];
......
...@@ -105,10 +105,10 @@ __global__ void KernelPool2dBackward( ...@@ -105,10 +105,10 @@ __global__ void KernelPool2dBackward(
template <typename PoolProcess, typename T> template <typename PoolProcess, typename T>
class Pool2dForwardFunctor<platform::GPUPlace, PoolProcess, T> { class Pool2dForwardFunctor<platform::GPUPlace, PoolProcess, T> {
public: public:
void operator()(const framework::Tensor& input, framework::Tensor& output, void operator()(const platform::DeviceContext& context,
const framework::Tensor& input, framework::Tensor& output,
std::vector<int>& ksize, std::vector<int>& strides, std::vector<int>& ksize, std::vector<int>& strides,
std::vector<int>& paddings, PoolProcess pool_process, std::vector<int>& paddings, PoolProcess pool_process) {
const platform::DeviceContext& context) {
const int batch_size = input.dims()[0]; const int batch_size = input.dims()[0];
const int input_channels = input.dims()[1]; const int input_channels = input.dims()[1];
const int input_height = input.dims()[2]; const int input_height = input.dims()[2];
...@@ -148,12 +148,12 @@ class Pool2dForwardFunctor<platform::GPUPlace, PoolProcess, T> { ...@@ -148,12 +148,12 @@ class Pool2dForwardFunctor<platform::GPUPlace, PoolProcess, T> {
template <typename PoolProcess, typename T> template <typename PoolProcess, typename T>
class Pool2dBackwardFunctor<platform::GPUPlace, PoolProcess, T> { class Pool2dBackwardFunctor<platform::GPUPlace, PoolProcess, T> {
public: public:
void operator()(const framework::Tensor& input, framework::Tensor& input_grad, void operator()(const platform::DeviceContext& context,
const framework::Tensor& input, framework::Tensor& input_grad,
const framework::Tensor& output, const framework::Tensor& output,
const framework::Tensor& output_grad, std::vector<int>& ksize, const framework::Tensor& output_grad, std::vector<int>& ksize,
std::vector<int>& strides, std::vector<int>& paddings, std::vector<int>& strides, std::vector<int>& paddings,
PoolProcess pool_process, PoolProcess pool_process) {
const platform::DeviceContext& context) {
const int batch_size = input.dims()[0]; const int batch_size = input.dims()[0];
const int input_channels = input.dims()[1]; const int input_channels = input.dims()[1];
const int input_height = input.dims()[2]; const int input_height = input.dims()[2];
...@@ -319,10 +319,10 @@ __global__ void KernelPool3DBackward( ...@@ -319,10 +319,10 @@ __global__ void KernelPool3DBackward(
template <typename PoolProcess, class T> template <typename PoolProcess, class T>
class Pool3dForwardFunctor<platform::GPUPlace, PoolProcess, T> { class Pool3dForwardFunctor<platform::GPUPlace, PoolProcess, T> {
public: public:
void operator()(const framework::Tensor& input, framework::Tensor& output, void operator()(const platform::DeviceContext& context,
const framework::Tensor& input, framework::Tensor& output,
std::vector<int>& ksize, std::vector<int>& strides, std::vector<int>& ksize, std::vector<int>& strides,
std::vector<int>& paddings, PoolProcess pool_process, std::vector<int>& paddings, PoolProcess pool_process) {
const platform::DeviceContext& context) {
const int batch_size = input.dims()[0]; const int batch_size = input.dims()[0];
const int input_channels = input.dims()[1]; const int input_channels = input.dims()[1];
const int input_depth = input.dims()[2]; const int input_depth = input.dims()[2];
...@@ -369,12 +369,12 @@ class Pool3dForwardFunctor<platform::GPUPlace, PoolProcess, T> { ...@@ -369,12 +369,12 @@ class Pool3dForwardFunctor<platform::GPUPlace, PoolProcess, T> {
template <typename PoolProcess, class T> template <typename PoolProcess, class T>
class Pool3dBackwardFunctor<platform::GPUPlace, PoolProcess, T> { class Pool3dBackwardFunctor<platform::GPUPlace, PoolProcess, T> {
public: public:
void operator()(const framework::Tensor& input, framework::Tensor& input_grad, void operator()(const platform::DeviceContext& context,
const framework::Tensor& input, framework::Tensor& input_grad,
const framework::Tensor& output, const framework::Tensor& output,
const framework::Tensor& output_grad, std::vector<int>& ksize, const framework::Tensor& output_grad, std::vector<int>& ksize,
std::vector<int>& strides, std::vector<int>& paddings, std::vector<int>& strides, std::vector<int>& paddings,
PoolProcess pool_process, PoolProcess pool_process) {
const platform::DeviceContext& context) {
const int batch_size = input.dims()[0]; const int batch_size = input.dims()[0];
const int input_channels = input.dims()[1]; const int input_channels = input.dims()[1];
const int input_depth = input.dims()[2]; const int input_depth = input.dims()[2];
......
...@@ -59,41 +59,41 @@ class avePool { ...@@ -59,41 +59,41 @@ class avePool {
template <typename Place, typename PoolProcess, typename T> template <typename Place, typename PoolProcess, typename T>
class Pool2dForwardFunctor { class Pool2dForwardFunctor {
public: public:
void operator()(const framework::Tensor& input, framework::Tensor& output, void operator()(const platform::DeviceContext& context,
const framework::Tensor& input, framework::Tensor& output,
std::vector<int>& ksize, std::vector<int>& strides, std::vector<int>& ksize, std::vector<int>& strides,
std::vector<int>& paddings, PoolProcess pool_process, std::vector<int>& paddings, PoolProcess pool_process);
const platform::DeviceContext& context);
}; };
template <typename Place, typename PoolProcess, typename T> template <typename Place, typename PoolProcess, typename T>
class Pool2dBackwardFunctor { class Pool2dBackwardFunctor {
public: public:
void operator()(const framework::Tensor& input, framework::Tensor& input_grad, void operator()(const platform::DeviceContext& context,
const framework::Tensor& input, framework::Tensor& input_grad,
const framework::Tensor& output, const framework::Tensor& output,
const framework::Tensor& output_grad, std::vector<int>& ksize, const framework::Tensor& output_grad, std::vector<int>& ksize,
std::vector<int>& strides, std::vector<int>& paddings, std::vector<int>& strides, std::vector<int>& paddings,
PoolProcess pool_process, PoolProcess pool_process);
const platform::DeviceContext& context);
}; };
template <typename Place, typename PoolProcess, typename T> template <typename Place, typename PoolProcess, typename T>
class Pool3dForwardFunctor { class Pool3dForwardFunctor {
public: public:
void operator()(const framework::Tensor& input, framework::Tensor& output, void operator()(const platform::DeviceContext& context,
const framework::Tensor& input, framework::Tensor& output,
std::vector<int>& ksize, std::vector<int>& strides, std::vector<int>& ksize, std::vector<int>& strides,
std::vector<int>& paddings, PoolProcess pool_process, std::vector<int>& paddings, PoolProcess pool_process);
const platform::DeviceContext& context);
}; };
template <typename Place, typename PoolProcess, typename T> template <typename Place, typename PoolProcess, typename T>
class Pool3dBackwardFunctor { class Pool3dBackwardFunctor {
public: public:
void operator()(const framework::Tensor& input, framework::Tensor& input_grad, void operator()(const platform::DeviceContext& context,
const framework::Tensor& input, framework::Tensor& input_grad,
const framework::Tensor& output, const framework::Tensor& output,
const framework::Tensor& output_grad, std::vector<int>& ksize, const framework::Tensor& output_grad, std::vector<int>& ksize,
std::vector<int>& strides, std::vector<int>& paddings, std::vector<int>& strides, std::vector<int>& paddings,
PoolProcess pool_process, PoolProcess pool_process);
const platform::DeviceContext& context);
}; };
} // namespace math } // namespace math
......
...@@ -28,18 +28,18 @@ class PoolOp : public framework::OperatorWithKernel { ...@@ -28,18 +28,18 @@ class PoolOp : public framework::OperatorWithKernel {
protected: protected:
void InferShape(const framework::InferShapeContext &ctx) const override { void InferShape(const framework::InferShapeContext &ctx) const override {
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Input"), PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("X"),
"Input(Input) of Pooling should not be null."); "X(Input) of Pooling should not be null.");
PADDLE_ENFORCE_NOT_NULL(ctx.OutputVar("Output"), PADDLE_ENFORCE_NOT_NULL(ctx.OutputVar("Out"),
"Output(Output) of Pooling should not be null."); "Out(Output) of Pooling should not be null.");
// PADDLE_ENFORCE_NOT_NULL(Attr<std::string>("pooling_type"), // PADDLE_ENFORCE_NOT_NULL(Attr<std::string>("poolingType"),
// "pooling_type should not be null."); // "pooling_type should not be null.");
// PADDLE_ENFORCE_NOT_NULL(Attr<std::vector<int>>("ksize"), "ksize should // PADDLE_ENFORCE_NOT_NULL(Attr<std::vector<int>>("ksize"), "ksize should
// not be null."); // not be null.");
auto input = ctx.Input<Tensor>("Input"); auto in_X = ctx.Input<Tensor>("X");
auto output = ctx.Output<framework::LoDTensor>("Output"); auto out = ctx.Output<framework::LoDTensor>("Out");
int global_pooling = Attr<int>("global_pooling"); int global_pooling = Attr<int>("globalPooling");
std::string pooling_type = Attr<std::string>("pooling_type"); std::string pooling_type = Attr<std::string>("poolingType");
std::vector<int> ksize = Attr<std::vector<int>>("ksize"); std::vector<int> ksize = Attr<std::vector<int>>("ksize");
std::vector<int> strides = Attr<std::vector<int>>("strides"); std::vector<int> strides = Attr<std::vector<int>>("strides");
std::vector<int> paddings = Attr<std::vector<int>>("paddings"); std::vector<int> paddings = Attr<std::vector<int>>("paddings");
...@@ -50,25 +50,25 @@ class PoolOp : public framework::OperatorWithKernel { ...@@ -50,25 +50,25 @@ class PoolOp : public framework::OperatorWithKernel {
"Pooling ksize should be 2-D or 3-D"); "Pooling ksize should be 2-D or 3-D");
if (global_pooling == 1) { if (global_pooling == 1) {
for (size_t i = 0; i < ksize.size(); ++i) ksize[i] = input->dims()[i + 2]; for (size_t i = 0; i < ksize.size(); ++i) ksize[i] = in_X->dims()[i + 2];
} }
if (ksize.size() == 2) { if (ksize.size() == 2) {
PADDLE_ENFORCE_EQ(input->dims().size(), 4, PADDLE_ENFORCE_EQ(in_X->dims().size(), 4,
"Pool2DOp intput should be 4-D."); "Pool2DOp intput should be 4-D.");
PADDLE_ENFORCE_EQ(strides.size(), 2, "Pool2DOp strides should be 2-D."); PADDLE_ENFORCE_EQ(strides.size(), 2, "Pool2DOp strides should be 2-D.");
PADDLE_ENFORCE_EQ(paddings.size(), 2, "Pool2DOp paddings should be 2-D."); PADDLE_ENFORCE_EQ(paddings.size(), 2, "Pool2DOp paddings should be 2-D.");
} else { } else {
PADDLE_ENFORCE_EQ(input->dims().size(), 5, PADDLE_ENFORCE_EQ(in_X->dims().size(), 5,
"Pool3DOp intput should be 5-D."); "Pool3DOp intput should be 5-D.");
PADDLE_ENFORCE_EQ(strides.size(), 3, "Pool3DOp strides should be 3-D."); PADDLE_ENFORCE_EQ(strides.size(), 3, "Pool3DOp strides should be 3-D.");
PADDLE_ENFORCE_EQ(paddings.size(), 3, "Pool3DOp paddings should be 3-D."); PADDLE_ENFORCE_EQ(paddings.size(), 3, "Pool3DOp paddings should be 3-D.");
} }
std::vector<int64_t> output_shape({input->dims()[0], input->dims()[1]}); std::vector<int64_t> output_shape({in_X->dims()[0], in_X->dims()[1]});
for (size_t i = 0; i < ksize.size(); ++i) { for (size_t i = 0; i < ksize.size(); ++i) {
output_shape.push_back(outputSize_pool(input->dims()[i + 2], ksize[i], output_shape.push_back(outputSize_pool(in_X->dims()[i + 2], ksize[i],
paddings[i], strides[i])); paddings[i], strides[i]));
} }
output->Resize(framework::make_ddim(output_shape)); out->Resize(framework::make_ddim(output_shape));
} }
}; };
...@@ -78,9 +78,8 @@ class PoolOpGrad : public framework::OperatorWithKernel { ...@@ -78,9 +78,8 @@ class PoolOpGrad : public framework::OperatorWithKernel {
protected: protected:
void InferShape(const framework::InferShapeContext &ctx) const override { void InferShape(const framework::InferShapeContext &ctx) const override {
auto in = ctx.Input<Tensor>("Input"); auto in = ctx.Input<Tensor>("X");
auto d_in = auto d_in = ctx.Output<framework::LoDTensor>(framework::GradVarName("X"));
ctx.Output<framework::LoDTensor>(framework::GradVarName("Input"));
if (d_in) d_in->Resize(in->dims()); if (d_in) d_in->Resize(in->dims());
} }
}; };
...@@ -90,27 +89,36 @@ class Pool3dOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -90,27 +89,36 @@ class Pool3dOpMaker : public framework::OpProtoAndCheckerMaker {
Pool3dOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker) Pool3dOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
: OpProtoAndCheckerMaker(proto, op_checker) { : OpProtoAndCheckerMaker(proto, op_checker) {
AddInput( AddInput(
"Input", "X",
"The input tensor of pooling operator. " "The input tensor of pooling operator. "
"The format of input tensor is NCDHW. Where N is batch size, C is the " "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 " "number of channels, D, H and W is the depth, height and width of "
"image."); "image.");
AddOutput("Output", AddOutput("Out",
"The output tensor of pooling operator." "The output tensor of pooling operator."
"The format of output tensor is also NCDHW."); "The format of output tensor is also NCDHW.");
AddAttr<std::string>("pooling_type", AddAttr<std::string>("poolingType",
"pooling_type of pooling operator.['max' or 'ave']"); "poolingType of pooling operator.['max' or 'ave']");
AddAttr<std::vector<int>>("ksize", "strides of pooling operator."); AddAttr<std::vector<int>>(
AddAttr<int>("global_pooling", "whether to use the global_pooling.") "ksize", "pooling size(depth, height, width) of pooling operator.");
AddAttr<int>("globalPooling",
"default 0"
"whether to use the globalPooling.")
.SetDefault(0); .SetDefault(0);
AddAttr<std::vector<int>>("strides", "strides of pooling operator.") AddAttr<std::vector<int>>(
"strides",
"default {1,1,1}"
"strides(depth, height, width) of pooling operator.")
.SetDefault({1, 1, 1}); .SetDefault({1, 1, 1});
AddAttr<std::vector<int>>("paddings", "paddings of pooling operator.") AddAttr<std::vector<int>>(
"paddings",
"default {0,0,0}"
"paddings(depth, height, width) of pooling operator.")
.SetDefault({0, 0, 0}); .SetDefault({0, 0, 0});
AddComment(R"DOC( AddComment(R"DOC(
The pooling3d operation calculates the output based on The pooling3d operation calculates the output based on
the input, pooling_type and ksize, strides, paddings parameters. the input, poolingType and ksize, strides, paddings parameters.
)DOC"); )DOC");
} }
}; };
...@@ -120,26 +128,33 @@ class Pool2dOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -120,26 +128,33 @@ class Pool2dOpMaker : public framework::OpProtoAndCheckerMaker {
Pool2dOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker) Pool2dOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
: OpProtoAndCheckerMaker(proto, op_checker) { : OpProtoAndCheckerMaker(proto, op_checker) {
AddInput( AddInput(
"Input", "X",
"The input tensor of pooling operator. " "The input tensor of pooling operator. "
"The format of input tensor is NCHW. Where N is batch size, C is the " "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."); "number of channels, H and W is the height and width of image.");
AddOutput("Output", AddOutput("Out",
"The output tensor of pooling operator." "The output tensor of pooling operator."
"The format of output tensor is also NCHW."); "The format of output tensor is also NCHW.");
AddAttr<std::string>("pooling_type", AddAttr<std::string>("poolingType",
"pooling_type of pooling operator.['max' or 'ave']"); "poolingType of pooling operator.['max' or 'ave']");
AddAttr<std::vector<int>>("ksize", "strides of pooling operator."); AddAttr<std::vector<int>>(
AddAttr<int>("global_pooling", "whether to use the global_pooling.") "ksize", "pooling size(height, width) of pooling operator.");
AddAttr<int>("globalPooling",
"default 0"
"whether to use the globalPooling.[0 or 1]")
.SetDefault(0); .SetDefault(0);
AddAttr<std::vector<int>>("strides", "strides of pooling operator.") AddAttr<std::vector<int>>("strides",
"default {1, 1}"
"strides(height, width) of pooling operator.")
.SetDefault({1, 1}); .SetDefault({1, 1});
AddAttr<std::vector<int>>("paddings", "paddings of pooling operator.") AddAttr<std::vector<int>>("paddings",
"default {0, 0}"
"paddings(height, width) of pooling operator.")
.SetDefault({0, 0}); .SetDefault({0, 0});
AddComment(R"DOC( AddComment(R"DOC(
The pooling2d operation calculates the output based on The pooling2d operation calculates the output based on
the input, pooling_type and ksize, strides, paddings parameters. the input, poolingType and ksize, strides, paddings parameters.
)DOC"); )DOC");
} }
}; };
......
...@@ -28,17 +28,17 @@ template <typename Place, typename T> ...@@ -28,17 +28,17 @@ template <typename Place, typename T>
class PoolKernel : public framework::OpKernel { class PoolKernel : public framework::OpKernel {
public: public:
void Compute(const framework::ExecutionContext& context) const override { void Compute(const framework::ExecutionContext& context) const override {
const Tensor* input = context.Input<Tensor>("Input"); const Tensor* in_X = context.Input<Tensor>("X");
Tensor* output = context.Output<Tensor>("Output"); Tensor* out = context.Output<Tensor>("Out");
int global_pooling = context.Attr<int>("global_pooling"); int global_pooling = context.Attr<int>("globalPooling");
std::string pooling_type = context.Attr<std::string>("pooling_type"); std::string pooling_type = context.Attr<std::string>("poolingType");
std::vector<int> ksize = context.Attr<std::vector<int>>("ksize"); std::vector<int> ksize = context.Attr<std::vector<int>>("ksize");
std::vector<int> strides = context.Attr<std::vector<int>>("strides"); std::vector<int> strides = context.Attr<std::vector<int>>("strides");
std::vector<int> paddings = context.Attr<std::vector<int>>("paddings"); std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
if (global_pooling == 1) { if (global_pooling == 1) {
for (size_t i = 0; i < ksize.size(); ++i) { for (size_t i = 0; i < ksize.size(); ++i) {
ksize[i] = input->dims()[i + 2]; ksize[i] = in_X->dims()[i + 2];
} }
} }
...@@ -49,16 +49,16 @@ class PoolKernel : public framework::OpKernel { ...@@ -49,16 +49,16 @@ class PoolKernel : public framework::OpKernel {
Place, paddle::operators::math::pool::maxPool<T>, T> Place, paddle::operators::math::pool::maxPool<T>, T>
pool2d_forward; pool2d_forward;
paddle::operators::math::pool::maxPool<T> pool_process; paddle::operators::math::pool::maxPool<T> pool_process;
pool2d_forward(*input, *output, ksize, strides, paddings, pool2d_forward(context.device_context(), *in_X, *out, ksize, strides,
pool_process, context.device_context()); paddings, pool_process);
} else if (pooling_type == "ave") { } else if (pooling_type == "ave") {
paddle::operators::math::Pool2dForwardFunctor< paddle::operators::math::Pool2dForwardFunctor<
Place, paddle::operators::math::pool::avePool<T>, T> Place, paddle::operators::math::pool::avePool<T>, T>
pool2d_forward; pool2d_forward;
paddle::operators::math::pool::avePool<T> pool_process; paddle::operators::math::pool::avePool<T> pool_process;
pool2d_forward(*input, *output, ksize, strides, paddings, pool2d_forward(context.device_context(), *in_X, *out, ksize, strides,
pool_process, (context.device_context())); paddings, pool_process);
} }
} break; } break;
case 3: { case 3: {
...@@ -67,15 +67,15 @@ class PoolKernel : public framework::OpKernel { ...@@ -67,15 +67,15 @@ class PoolKernel : public framework::OpKernel {
Place, paddle::operators::math::pool::maxPool<T>, T> Place, paddle::operators::math::pool::maxPool<T>, T>
pool3d_forward; pool3d_forward;
paddle::operators::math::pool::maxPool<T> pool_process; paddle::operators::math::pool::maxPool<T> pool_process;
pool3d_forward(*input, *output, ksize, strides, paddings, pool3d_forward(context.device_context(), *in_X, *out, ksize, strides,
pool_process, context.device_context()); paddings, pool_process);
} else if (pooling_type == "ave") { } else if (pooling_type == "ave") {
paddle::operators::math::Pool3dForwardFunctor< paddle::operators::math::Pool3dForwardFunctor<
Place, paddle::operators::math::pool::avePool<T>, T> Place, paddle::operators::math::pool::avePool<T>, T>
pool3d_forward; pool3d_forward;
paddle::operators::math::pool::avePool<T> pool_process; paddle::operators::math::pool::avePool<T> pool_process;
pool3d_forward(*input, *output, ksize, strides, paddings, pool3d_forward(context.device_context(), *in_X, *out, ksize, strides,
pool_process, context.device_context()); paddings, pool_process);
} }
} break; } break;
} }
...@@ -86,26 +86,26 @@ template <typename Place, typename T> ...@@ -86,26 +86,26 @@ template <typename Place, typename T>
class PoolGradKernel : public framework::OpKernel { class PoolGradKernel : public framework::OpKernel {
public: public:
void Compute(const framework::ExecutionContext& context) const override { void Compute(const framework::ExecutionContext& context) const override {
const Tensor* input = context.Input<Tensor>("Input"); const Tensor* in_X = context.Input<Tensor>("X");
const Tensor* output = context.Input<Tensor>("Output"); const Tensor* out = context.Input<Tensor>("Out");
const Tensor* output_grad = const Tensor* out_grad =
context.Input<Tensor>(framework::GradVarName("Output")); context.Input<Tensor>(framework::GradVarName("Out"));
Tensor* input_grad = Tensor* in_X_grad =
context.Output<framework::LoDTensor>(framework::GradVarName("Input")); context.Output<framework::LoDTensor>(framework::GradVarName("X"));
int global_pooling = context.Attr<int>("global_pooling"); int global_pooling = context.Attr<int>("globalPooling");
std::string pooling_type = context.Attr<std::string>("pooling_type"); std::string pooling_type = context.Attr<std::string>("poolingType");
std::vector<int> ksize = context.Attr<std::vector<int>>("ksize"); std::vector<int> ksize = context.Attr<std::vector<int>>("ksize");
std::vector<int> strides = context.Attr<std::vector<int>>("strides"); std::vector<int> strides = context.Attr<std::vector<int>>("strides");
std::vector<int> paddings = context.Attr<std::vector<int>>("paddings"); std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
if (global_pooling == 1) { if (global_pooling == 1) {
for (size_t i = 0; i < ksize.size(); ++i) ksize[i] = input->dims()[i + 2]; for (size_t i = 0; i < ksize.size(); ++i) ksize[i] = in_X->dims()[i + 2];
} }
if (input_grad) { if (in_X_grad) {
input_grad->mutable_data<T>(context.GetPlace()); in_X_grad->mutable_data<T>(context.GetPlace());
auto temp = framework::EigenVector<T>::Flatten(*input_grad); auto temp = framework::EigenVector<T>::Flatten(*in_X_grad);
temp.device(context.GetEigenDevice<Place>()) = temp.device(context.GetEigenDevice<Place>()) =
temp.constant(static_cast<T>(0)); temp.constant(static_cast<T>(0));
...@@ -116,17 +116,15 @@ class PoolGradKernel : public framework::OpKernel { ...@@ -116,17 +116,15 @@ class PoolGradKernel : public framework::OpKernel {
Place, paddle::operators::math::pool::maxPool<T>, T> Place, paddle::operators::math::pool::maxPool<T>, T>
pool2d_backward; pool2d_backward;
paddle::operators::math::pool::maxPool<T> pool_process; paddle::operators::math::pool::maxPool<T> pool_process;
pool2d_backward(*input, *input_grad, *output, *output_grad, ksize, pool2d_backward(context.device_context(), *in_X, *in_X_grad, *out,
strides, paddings, pool_process, *out_grad, ksize, strides, paddings, pool_process);
context.device_context());
} else if (pooling_type == "ave") { } else if (pooling_type == "ave") {
paddle::operators::math::Pool2dBackwardFunctor< paddle::operators::math::Pool2dBackwardFunctor<
Place, paddle::operators::math::pool::avePool<T>, T> Place, paddle::operators::math::pool::avePool<T>, T>
pool2d_backward; pool2d_backward;
paddle::operators::math::pool::avePool<T> pool_process; paddle::operators::math::pool::avePool<T> pool_process;
pool2d_backward(*input, *input_grad, *output, *output_grad, ksize, pool2d_backward(context.device_context(), *in_X, *in_X_grad, *out,
strides, paddings, pool_process, *out_grad, ksize, strides, paddings, pool_process);
context.device_context());
} }
} break; } break;
case 3: { case 3: {
...@@ -135,17 +133,15 @@ class PoolGradKernel : public framework::OpKernel { ...@@ -135,17 +133,15 @@ class PoolGradKernel : public framework::OpKernel {
Place, paddle::operators::math::pool::maxPool<T>, T> Place, paddle::operators::math::pool::maxPool<T>, T>
pool3d_backward; pool3d_backward;
paddle::operators::math::pool::maxPool<T> pool_process; paddle::operators::math::pool::maxPool<T> pool_process;
pool3d_backward(*input, *input_grad, *output, *output_grad, ksize, pool3d_backward(context.device_context(), *in_X, *in_X_grad, *out,
strides, paddings, pool_process, *out_grad, ksize, strides, paddings, pool_process);
context.device_context());
} else if (pooling_type == "ave") { } else if (pooling_type == "ave") {
paddle::operators::math::Pool3dBackwardFunctor< paddle::operators::math::Pool3dBackwardFunctor<
Place, paddle::operators::math::pool::avePool<T>, T> Place, paddle::operators::math::pool::avePool<T>, T>
pool3d_backward; pool3d_backward;
paddle::operators::math::pool::avePool<T> pool_process; paddle::operators::math::pool::avePool<T> pool_process;
pool3d_backward(*input, *input_grad, *output, *output_grad, ksize, pool3d_backward(context.device_context(), *in_X, *in_X_grad, *out,
strides, paddings, pool_process, *out_grad, ksize, strides, paddings, pool_process);
context.device_context());
} }
} break; } break;
} }
......
...@@ -47,23 +47,23 @@ class TestPool2d_Op(OpTest): ...@@ -47,23 +47,23 @@ class TestPool2d_Op(OpTest):
input = np.random.random(self.shape).astype("float32") input = np.random.random(self.shape).astype("float32")
output = self.pool2D_forward_naive(input, self.ksize, self.strides, output = self.pool2D_forward_naive(input, self.ksize, self.strides,
self.paddings) self.paddings)
self.inputs = {'Input': input} self.inputs = {'X': input}
self.attrs = { self.attrs = {
'strides': self.strides, 'strides': self.strides,
'paddings': self.paddings, 'paddings': self.paddings,
'ksize': self.ksize, 'ksize': self.ksize,
'pooling_type': self.pool_type, 'poolingType': self.pool_type,
} }
self.outputs = {'Output': output} self.outputs = {'Out': output}
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output()
def test_check_grad(self): def test_check_grad(self):
if self.pool_type != "max": if self.pool_type != "max":
self.check_grad(set(['Input']), 'Output', max_relative_error=0.07) self.check_grad(set(['X']), 'Out', max_relative_error=0.07)
def initTestCase(self): def initTestCase(self):
self.pool_type = "ave" self.pool_type = "ave"
......
...@@ -57,23 +57,23 @@ class TestPool3d_Op(OpTest): ...@@ -57,23 +57,23 @@ class TestPool3d_Op(OpTest):
input = np.random.random(self.shape).astype("float32") input = np.random.random(self.shape).astype("float32")
output = self.pool3D_forward_naive(input, self.ksize, self.strides, output = self.pool3D_forward_naive(input, self.ksize, self.strides,
self.paddings) self.paddings)
self.inputs = {'Input': input} self.inputs = {'X': input}
self.attrs = { self.attrs = {
'strides': self.strides, 'strides': self.strides,
'paddings': self.paddings, 'paddings': self.paddings,
'ksize': self.ksize, 'ksize': self.ksize,
'pooling_type': self.pool_type, 'poolingType': self.pool_type,
} }
self.outputs = {'Output': output} self.outputs = {'Out': output}
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output()
def test_check_grad(self): def test_check_grad(self):
if self.pool_type != "max": if self.pool_type != "max":
self.check_grad(set(['Input']), 'Output', max_relative_error=0.07) self.check_grad(set(['X']), 'Out', max_relative_error=0.07)
def initTestCase(self): def initTestCase(self):
self.pool_type = "ave" self.pool_type = "ave"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册