diff --git a/paddle/fluid/operators/generator/get_expected_kernel_func.cc b/paddle/fluid/operators/generator/get_expected_kernel_func.cc index 96060b6de5bdfe1e3777522a1accfe5ac4f1de7a..81b150de30a0d182174f884e6142b14e76953279 100644 --- a/paddle/fluid/operators/generator/get_expected_kernel_func.cc +++ b/paddle/fluid/operators/generator/get_expected_kernel_func.cc @@ -61,6 +61,20 @@ static bool ReduceOpHasOptimizedOneDNNKernel( return true; } +// only poolop +bool CanMKLDNNSupportPool(const framework::ExecutionContext& ctx) { + if (ctx.Attr("adaptive") == false) return true; + // oneDNN is supporting only unchangable in size pool window + auto src_tz = phi::vectorize(ctx.Input("X")->dims()); + if (!ctx.HasAttr("ksize")) { + return false; + } + std::vector ksize = ctx.Attr>("ksize"); + // Fast but not exhustive check + return ((src_tz[src_tz.size() - 1] % ksize[1] == 0) && + (src_tz[src_tz.size() - 2] % ksize[0] == 0)); +} + phi::KernelKey GetCheckFiniteAndUnscaleExpectedKernelType( const framework::ExecutionContext& ctx, const framework::OperatorWithKernel* op_ptr) { @@ -136,6 +150,31 @@ phi::KernelKey GetAssignExpectedKernelType( ctx.device_context().GetPlace()); } +phi::KernelKey GetPoolExpectedKernelType( + const framework::ExecutionContext& ctx, + const framework::OperatorWithKernel* op_ptr) { + auto data_type = op_ptr->OperatorWithKernel::IndicateVarDataType(ctx, "X"); + + // NOTE(jiahongyu): Below codes originally enclosed by PADDLE_WITH_MKLDNN + op_ptr->SetDnnFallback(!CanMKLDNNSupportPool(ctx)); + // NOTE(jiahongyu) END: Above codes originally enclosed by PADDLE_WITH_MKLDNN + + return phi::KernelKey(data_type, ctx.GetPlace()); +} + +phi::KernelKey GetPoolDoubleGradExpectedKernelType( + const framework::ExecutionContext& ctx, + const framework::OperatorWithKernel* op_ptr) { + auto data_type = + op_ptr->OperatorWithKernel::IndicateVarDataType(ctx, "grad_x@GRAD"); + + // NOTE(jiahongyu): Below codes originally enclosed by PADDLE_WITH_MKLDNN + op_ptr->SetDnnFallback(!CanMKLDNNSupportPool(ctx)); + // NOTE(jiahongyu) END: Above codes originally enclosed by PADDLE_WITH_MKLDNN + + return phi::KernelKey(data_type, ctx.GetPlace()); +} + phi::KernelKey GetSgdExpectedKernelType( const framework::ExecutionContext& ctx, const framework::OperatorWithKernel* op_ptr) { diff --git a/paddle/fluid/operators/generator/get_expected_kernel_func.h b/paddle/fluid/operators/generator/get_expected_kernel_func.h index 37dfc7607da5e869a0a3733fe7c0e2c3db2bf97d..ed67932ba45efc1b35a2ee4f11cfc76cd26bc95a 100644 --- a/paddle/fluid/operators/generator/get_expected_kernel_func.h +++ b/paddle/fluid/operators/generator/get_expected_kernel_func.h @@ -40,6 +40,14 @@ phi::KernelKey GetAssignExpectedKernelType( const framework::ExecutionContext& ctx, const framework::OperatorWithKernel* op_ptr); +phi::KernelKey GetPoolExpectedKernelType( + const framework::ExecutionContext& ctx, + const framework::OperatorWithKernel* op_ptr); + +phi::KernelKey GetPoolDoubleGradExpectedKernelType( + const framework::ExecutionContext& ctx, + const framework::OperatorWithKernel* op_ptr); + phi::KernelKey GetSgdExpectedKernelType( const framework::ExecutionContext& ctx, const framework::OperatorWithKernel* op_ptr); diff --git a/paddle/fluid/operators/pool_op.cc b/paddle/fluid/operators/pool_op.cc deleted file mode 100644 index f6ba1c624b5c6e9f198dd3949a0e86062da9a5b1..0000000000000000000000000000000000000000 --- a/paddle/fluid/operators/pool_op.cc +++ /dev/null @@ -1,539 +0,0 @@ -/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. */ - -#include "paddle/fluid/operators/pool_op.h" - -#include - -#include "paddle/fluid/framework/infershape_utils.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/platform/device/gpu/gpu_dnn.h" -#include "paddle/phi/core/infermeta_utils.h" -#include "paddle/phi/infermeta/backward.h" -#include "paddle/phi/infermeta/unary.h" -#ifdef PADDLE_WITH_MKLDNN -#include "paddle/fluid/platform/mkldnn_helper.h" -#endif - -namespace paddle { -namespace operators { - -bool CanMKLDNNSupportPool(const framework::ExecutionContext& ctx) { - if (ctx.Attr("adaptive") == false) return true; - // oneDNN is supporting only unchangable in size pool window - auto src_tz = phi::vectorize(ctx.Input("X")->dims()); - if (!ctx.HasAttr("ksize")) { - return false; - } - std::vector ksize = ctx.Attr>("ksize"); - // Fast but not exhustive check - return ((src_tz[src_tz.size() - 1] % ksize[1] == 0) && - (src_tz[src_tz.size() - 2] % ksize[0] == 0)); -} - -phi::KernelKey PoolOp::GetExpectedKernelType( - const framework::ExecutionContext& ctx) const { - auto data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X"); - - // NOTE(jiahongyu): Below codes originally enclosed by PADDLE_WITH_MKLDNN - this->SetDnnFallback(!CanMKLDNNSupportPool(ctx)); - // NOTE(jiahongyu) END: Above codes originally enclosed by PADDLE_WITH_MKLDNN - - return phi::KernelKey(data_type, ctx.GetPlace()); -} - -phi::KernelKey PoolOp::GetKernelTypeForVar( - const std::string& var_name, - const phi::DenseTensor& tensor, - const phi::KernelKey& expected_kernel_type) const { -#ifdef PADDLE_WITH_MKLDNN - if ((expected_kernel_type.layout() == phi::DataLayout::ONEDNN) && - (tensor.layout() != phi::DataLayout::ONEDNN)) { - auto attrs = Attrs(); - auto ar = paddle::framework::AttrReader(attrs); - const std::string data_format = ar.Get("data_format"); - auto dl = phi::StringToDataLayout(data_format); - // Some models may have intentionally set "AnyLayout" for pool - // op. Treat this as NCHW (default data_format value) - if (dl != phi::DataLayout::kAnyLayout) { - return phi::KernelKey(tensor.place(), dl, expected_kernel_type.dtype()); - } - } -#endif - return phi::KernelKey( - tensor.place(), tensor.layout(), expected_kernel_type.dtype()); -} - -phi::KernelKey PoolOpGrad::GetExpectedKernelType( - const framework::ExecutionContext& ctx) const { - auto input_data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X"); - - // NOTE(jiahongyu): Below codes originally enclosed by PADDLE_WITH_MKLDNN - this->SetDnnFallback(!CanMKLDNNSupportPool(ctx)); - // NOTE(jiahongyu): Above codes originally enclosed by PADDLE_WITH_MKLDNN - - return phi::KernelKey(input_data_type, ctx.GetPlace()); -} - -phi::KernelKey PoolOpGrad::GetKernelTypeForVar( - const std::string& var_name, - const phi::DenseTensor& tensor, - const phi::KernelKey& expected_kernel_type) const { -#ifdef PADDLE_WITH_MKLDNN - if ((expected_kernel_type.layout() == phi::DataLayout::ONEDNN) && - (tensor.layout() != phi::DataLayout::ONEDNN)) { - auto attrs = Attrs(); - auto ar = paddle::framework::AttrReader(attrs); - const std::string data_format = ar.Get("data_format"); - return phi::KernelKey(tensor.place(), - phi::StringToDataLayout(data_format), - expected_kernel_type.dtype()); - } -#endif - return phi::KernelKey( - tensor.place(), tensor.layout(), expected_kernel_type.dtype()); -} - -void Pool2dOpMaker::Make() { - AddInput( - "X", - "(phi::DenseTensor) 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 is the height of the feature, " - "and W is the width of the feature."); - AddOutput("Out", - "(phi::DenseTensor) 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 is the height of the feature, " - "and W is the width of the feature."); - - AddAttr("pooling_type", - "(string), pooling type, can be \"max\" for max-pooling " - "and \"avg\" for average-pooling.") - .InEnum({"max", "avg"}); - AddAttr>("ksize", - "(vector) The pooling window " - "size(height, width) of the pooling operator. " - "If global_pooling = true, ksize and paddings will " - "be ignored.") - .SupportTensor(); - AddAttr( - "global_pooling", - "(bool) Whether to use the global pooling. " - "If global_pooling = true, kernel size and paddings will be ignored. " - "Default False.") - .SetDefault(false); - AddAttr>("strides", - "(vector, default {1, 1}), strides(height, " - "width) of pooling operator.") - .SetDefault({1, 1}); - // TODO(Chengduo): Add checker. (Currently, - // TypedAttrChecker don't support vector type.) - AddAttr>( - "paddings", - "(vector, default {0,0}), paddings(height_top, height_bottom, " - "width_left, wifth_right) of pooling operator." - "If global_pooling = true, paddings and kernel size will be ignored.") - .SetDefault({0, 0}); - AddAttr( - "exclusive", - "(bool) When true, will exclude the zero-padding in the " - "averaging calculating, otherwise, include the zero-padding. Note, it " - "is only used when pooling_type is avg. The default is True. " - "Default True.") - .SetDefault(true); - AddAttr( - "adaptive", - "(bool) When true, will perform adaptive pooling instead, " - "output shape in H and W dimensions will be same as ksize, input data " - "will be divided into grids specify by ksize averagely and perform " - "pooling in each grid area to get output pooling value. " - "Default False.") - .SetDefault(false); - AddAttr( - "ceil_mode", - "(bool) Whether to use the ceil function to calculate " - "output height and width. False is the default. If it is set to False, " - "the floor function will be used. Default False") - .SetDefault(false); - AddAttr( - "data_format", - "(string, default NCHW) Only used in " - "An optional string from: \"NHWC\", \"NCHW\". " - "Defaults to \"NHWC\". Specify the data format of the output data, " - "the input will be transformed automatically. ") - .SetDefault("NCHW"); - AddAttr( - "padding_algorithm", - "(string, default \"EXPLICIT\") An optional string from: \"EXPLICIT\"," - "\"SAME\",\"VALID\". Set to \"EXPLICIT\" for explicit padding. " - "Set to \"SAME\" or \"VALID\" for algorithm of padding. ") - .SetDefault("EXPLICIT"); - // TODO(dzhwinter): need to registered layout transform function - AddAttr( - "use_cudnn", - "(bool) Only used in cudnn kernel, need install cudnn. Default False") - .SetDefault(false) - .AsExtra(); - AddComment(R"DOC( -This operation calculates the pooling output based on -the input, pooling_type and pool_size, pool_stride, pool_padding parameters. -Input(X) and Output(Out) are in NCHW or NHWC format, where N is batch size, C is the -number of channels, H is the height of the feature, and W is the width of the feature. -Parameters(pool_size, pool_stride, pool_padding) hold two integer elements. -These two elements represent height and width, respectively. -The input(X) size and output(Out) size may be different. - -Example: - - Input: - - X shape: $(N, C, H_{in}, W_{in})$ - - Output: - - Out shape: $(N, C, H_{out}, W_{out})$ - - For pool_padding = "SAME": - $$ - H_{out} = \\frac{(H_{in} + strides[0] - 1)}{strides[0]} - $$ - $$ - W_{out} = \\frac{(W_{in} + strides[1] - 1)}{strides[1]} - $$ - - For pool_padding = "VALID": - $$ - H_{out} = \\frac{(H_{in} - ksize[0] + strides[0])}{strides[0]} - $$ - $$ - W_{out} = \\frac{(W_{in} - ksize[1] + strides[1])}{strides[1]} - $$ - - For ceil_mode = false: - $$ - H_{out} = \\frac{(H_{in} - ksize[0] + pad_height_top + pad_height_bottom}{strides[0]} + 1 - $$ - $$ - W_{out} = \\frac{(W_{in} - ksize[1] + pad_width_left + pad_width_right}{strides[1]} + 1 - $$ - - For ceil_mode = true: - $$ - H_{out} = \\frac{(H_{in} - ksize[0] + pad_height_top + pad_height_bottom + strides[0] - 1)}{strides[0]} + 1 - $$ - $$ - W_{out} = \\frac{(W_{in} - ksize[1] + pad_width_left + pad_width_right + strides[1] - 1)}{strides[1]} + 1 - $$ - - For exclusive = false: - $$ - hstart = i * strides[0] - pad_height_top - $$ - $$ - hend = hstart + ksize[0] - $$ - $$ - wstart = j * strides[1] - pad_width_left - $$ - $$ - wend = wstart + ksize[1] - $$ - $$ - Output(i ,j) = \\frac{sum(Input[hstart:hend, wstart:wend])}{ksize[0] * ksize[1]} - $$ - - For exclusive = true: - $$ - hstart = max(0, i * strides[0] - pad_height_top) - $$ - $$ - hend = min(H, hstart + ksize[0]) - $$ - $$ - wstart = max(0, j * strides[1] - pad_width_left) - $$ - $$ - wend = min(W, wstart + ksize[1]) - $$ - $$ - Output(i ,j) = \\frac{sum(Input[hstart:hend, wstart:wend])}{(hend - hstart) * (wend - wstart)} - $$ - -)DOC"); -} - -template -class Pool2dOpGradGradMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - - protected: - void Apply(GradOpPtr grad_op) const override { - grad_op->SetType("pool2d_double_grad"); - grad_op->SetInput("X", this->OutputGrad(framework::GradVarName("X"))); - grad_op->SetOutput("Out", this->InputGrad(framework::GradVarName("Out"))); - grad_op->SetAttrMap(this->Attrs()); - } -}; - -class PoolOpInferVarType : public framework::PassInDtypeAndVarTypeToOutput { - protected: - std::unordered_map& GetInputOutputWithSameType() - const override { - static std::unordered_map m{{"X", /*->*/ "Out"}}; - return m; - } -}; - -void Pool3dOpMaker::Make() { - AddInput("X", - "(phi::DenseTensor) The input tensor of pooling operator. " - "The format of input tensor is NCDHW or NDHWC, where N is batch " - "size, C is " - "the number of channels, and D, H and W is the depth, height and " - "width of " - "the feature, respectively."); - AddOutput("Out", - "(phi::DenseTensor) The output tensor of pooling operator." - "The format of output tensor is also NCDHW or NDHWC, " - "where N is batch size, C is " - "the number of channels, and D, H and W is the depth, height and " - "width of the feature, respectively."); - - AddAttr("pooling_type", - "(string) Pooling type, can be \"max\" for max-pooling " - "and \"avg\" for average-pooling.") - .InEnum({"max", "avg"}); - AddAttr>( - "ksize", - "(vector) The pooling window size(depth, height, " - "width) of pooling operator. " - "If global_pooling = true, ksize and paddings will " - "be ignored."); - AddAttr( - "global_pooling", - "(bool) Whether to use the global pooling. " - "If global_pooling = true, kernel size and paddings will be ignored. " - "Default False") - .SetDefault(false); - AddAttr>( - "strides", - "(vector, default {1,1,1}) Strides(depth, height, " - "width) of the pooling operator.") - .SetDefault({1, 1, 1}); // TODO(Chengduo): Add checker. (Currently, - // TypedAttrChecker don't support vector type.) - AddAttr>( - "paddings", - "(vector, default {0,0,0}), paddings(pad_depth_front, " - "pad_depth_back, " - "pad_height_top, pad_height_bottom, pad_width_left, pad_width_right" - ") of pooling operator. " - "If global_pooling = true, ksize and paddings will be ignored.") - .SetDefault({0, 0, 0}); // TODO(Chengduo): Add checker. (Currently, - // TypedAttrChecker don't support vector type.) - AddAttr( - "exclusive", - "(bool) When true, will exclude the zero-padding in the " - "averaging calculating, otherwise, include the zero-padding. Note, it " - "is only used when pooling_type is avg. The default is True. " - "Default True") - .SetDefault(true); - AddAttr( - "adaptive", - "(bool) When true, will perform adaptive pooling instead, " - "output shape in H and W dimensions will be same as ksize, input data " - "will be divided into grids specify by ksize averagely and perform " - "pooling in each grid area to get output pooling value. " - "Default False") - .SetDefault(false); - AddAttr( - "ceil_mode", - "(bool) Whether to use the ceil function to calculate " - "output height and width. False is the default. If it is set to False, " - "the floor function will be used. Default False") - .SetDefault(false); - AddAttr( - "data_format", - "(string, default NCDHW) Only used in " - "An optional string from: \"NDHWC\", \"NCDHW\". " - "Defaults to \"NDHWC\". Specify the data format of the output data, " - "the input will be transformed automatically. ") - .SetDefault("NCDHW"); - AddAttr( - "padding_algorithm", - "(string, default \"EXPLICIT\") An optional string from: \"EXPLICIT\"," - "\"SAME\",\"VALID\". Set to \"EXPLICIT\" for explicit padding. " - "Set to \"SAME\" or \"VALID\" for algorithm of padding. ") - .SetDefault("EXPLICIT"); - AddAttr( - "use_cudnn", - "(bool) Only used in cudnn kernel, need install cudnn. Default False") - .SetDefault(false) - .AsExtra(); - AddComment(R"DOC( -This operation calculates the output based on -the input, pooling_type, pool_size, pool_stride, and pool_padding parameters. -Input(X) and output(Out) are in NCDHW or NDHWC format, where N is batch -size, C is the number of channels, and D, H and W are the depth, height and -width of the feature, respectively. Parameters(pool_size, pool_stride, pool_padding) -hold three integer elements. These three elements represent depth, height and -width, respectively. The input(X) size and output(Out) size may be different. - -Example: - Input: - X shape: $(N, C, D_{in}, H_{in}, W_{in})$ - Output: - Out shape: $(N, C, D_{out}, H_{out}, W_{out})$ - - For pool_padding = "SAME": - $$ - D_{out} = \\frac{(D_{in} + strides[0] - 1)}{strides[0]} - $$ - $$ - H_{out} = \\frac{(H_{in} + strides[1] - 1)}{strides[1]} - $$ - $$ - W_{out} = \\frac{(W_{in} + strides[2] - 1)}{strides[2]} - $$ - - For pool_padding = "VALID": - $$ - D_{out} = \\frac{(D_{in} - ksize[0] + strides[0])}{strides[0]} - $$ - $$ - H_{out} = \\frac{(H_{in} - ksize[1] + strides[1])}{strides[1]} - $$ - $$ - W_{out} = \\frac{(W_{in} - ksize[2] + strides[2])}{strides[2]} - $$ - - For ceil_mode = false: - $$ - D_{out} = \\frac{(D_{in} - ksize[0] + pad_depth_front + pad_depth_back)}{strides[0]} + 1 - $$ - $$ - H_{out} = \\frac{(H_{in} - ksize[1] + pad_height_top + pad_height_bottom)}{strides[1]} + 1 - $$ - $$ - W_{out} = \\frac{(W_{in} - ksize[2] + pad_width_left + pad_width_right)}{strides[2]} + 1 - $$ - For ceil_mode = true: - $$ - D_{out} = \\frac{(D_{in} - ksize[0] + pad_depth_front + pad_depth_back + strides[0] -1)}{strides[0]} + 1 - $$ - $$ - H_{out} = \\frac{(H_{in} - ksize[1] + pad_height_top + pad_height_bottom + strides[1] -1)}{strides[1]} + 1 - $$ - $$ - W_{out} = \\frac{(W_{in} - ksize[2] + pad_width_left + pad_width_right + strides[2] -1)}{strides[2]} + 1 - $$ - - For exclusive = false: - $$ - dstart = i * strides[0] - pad_depth_front - $$ - $$ - dend = dstart + ksize[0] - $$ - $$ - hstart = j * strides[1] - pad_height_top - $$ - $$ - hend = hstart + ksize[1] - $$ - $$ - wstart = k * strides[2] - pad_width_left - $$ - $$ - wend = wstart + ksize[2] - $$ - $$ - Output(i ,j, k) = \\frac{sum(Input[dstart:dend, hstart:hend, wstart:wend])}{ksize[0] * ksize[1] * ksize[2]} - $$ - - For exclusive = true: - $$ - dstart = max(0, i * strides[0] - pad_depth_front) - $$ - $$ - dend = min(D, dstart + ksize[0]) - $$ - $$ - hstart = max(0, j * strides[1] - pad_height_top) - $$ - $$ - hend = min(H, hstart + ksize[1]) - $$ - $$ - wstart = max(0, k * strides[2] - pad_width_left) - $$ - $$ - wend = min(W, wstart + ksize[2]) - $$ - $$ - Output(i ,j, k) = \\frac{sum(Input[dstart:dend, hstart:hend, wstart:wend])}{(dend - dstart) * (hend - hstart) * (wend - wstart)} - $$ - -)DOC"); -} -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; - -DECLARE_INFER_SHAPE_FUNCTOR(pool2d, - Pool2dInferShapeFunctor, - PD_INFER_META(phi::Pool2DInferMeta)); -DECLARE_INFER_SHAPE_FUNCTOR(pool2d_grad, - Pool2dGradInferShapeFunctor, - PD_INFER_META(phi::UnchangedInferMeta)); -DECLARE_INFER_SHAPE_FUNCTOR(pool2d_double_grad, - Pool2dDoubleGradInferShapeFunctor, - PD_INFER_META(phi::Pool2DInferMeta)); - -REGISTER_OPERATOR( - pool2d, - ops::PoolOp, - ops::Pool2dOpMaker, - ops::PoolOpInferVarType, - paddle::framework::DefaultGradOpMaker, - paddle::framework::DefaultGradOpMaker, - Pool2dInferShapeFunctor); -REGISTER_OPERATOR(pool2d_grad, - ops::PoolOpGrad, - ops::Pool2dOpGradGradMaker, - ops::Pool2dOpGradGradMaker, - Pool2dGradInferShapeFunctor); -REGISTER_OPERATOR(pool2d_double_grad, - ops::PoolOp, - Pool2dDoubleGradInferShapeFunctor); - -DECLARE_INFER_SHAPE_FUNCTOR(pool3d, - Pool3dInferShapeFunctor, - PD_INFER_META(phi::PoolInferMeta)); -DECLARE_INFER_SHAPE_FUNCTOR(pool3d_grad, - Pool3dGradInferShapeFunctor, - PD_INFER_META(phi::UnchangedInferMeta)); - -REGISTER_OPERATOR( - pool3d, - ops::PoolOp, - ops::Pool3dOpMaker, - ops::PoolOpInferVarType, - paddle::framework::DefaultGradOpMaker, - paddle::framework::DefaultGradOpMaker, - Pool3dInferShapeFunctor); -REGISTER_OPERATOR(pool3d_grad, ops::PoolOpGrad, Pool3dGradInferShapeFunctor); diff --git a/paddle/fluid/operators/pool_op.h b/paddle/fluid/operators/pool_op.h deleted file mode 100644 index a935c6b14fd5abd9f44823eb01b2d36cc4d57840..0000000000000000000000000000000000000000 --- a/paddle/fluid/operators/pool_op.h +++ /dev/null @@ -1,61 +0,0 @@ -/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. */ - -#pragma once - -#include "paddle/fluid/framework/op_registry.h" - -namespace paddle { -namespace operators { - -class PoolOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext& ctx) const override; - - phi::KernelKey GetKernelTypeForVar( - const std::string& var_name, - const phi::DenseTensor& tensor, - const phi::KernelKey& expected_kernel_type) const override; -}; - -class PoolOpGrad : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext& ctx) const override; - - phi::KernelKey GetKernelTypeForVar( - const std::string& var_name, - const phi::DenseTensor& tensor, - const phi::KernelKey& expected_kernel_type) const override; -}; - -class Pool2dOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override; -}; - -class Pool3dOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override; -}; - -} // namespace operators -} // namespace paddle diff --git a/paddle/fluid/operators/unity_build_rule.cmake b/paddle/fluid/operators/unity_build_rule.cmake index cde64bfc6bb4dee990c0a59c758e311f240fe61f..4de9ed86fd06428cc753f04d463f31ced663d1a5 100644 --- a/paddle/fluid/operators/unity_build_rule.cmake +++ b/paddle/fluid/operators/unity_build_rule.cmake @@ -200,7 +200,6 @@ register_unity_group( cc partial_sum_op.cc pixel_shuffle_op.cc - pool_op.cc pool_with_index_op.cc positive_negative_pair_op.cc prelu_op.cc diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index c8dd1afa1cf93514fbf320629751d21640a70931..4917af23ac7f4a918c7aad8b64d6df91f9f7ba11 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -1887,17 +1887,38 @@ out : Out - op : pool2d - backward : pool2d_grad - attrs: - kernel_size: ksize + backward : pool2d_grad, pool2d_double_grad + inputs : + {x : X} + outputs : + {out : Out} + attrs : + {kernel_size : ksize} + int_array: + kernel_size : + data_type : int + support_tensor : true + get_expected_kernel_type : + pool2d : GetPoolExpectedKernelType + pool2d_grad : GetPoolExpectedKernelType + pool2d_double_grad : GetPoolDoubleGradExpectedKernelType extra : attrs : [bool use_mkldnn = false, bool use_quantizer = false, - str mkldnn_data_type = "float32", bool is_test = false] + str mkldnn_data_type = "float32", bool is_test = false, bool use_cudnn = false] - op : pool3d backward : pool3d_grad + inputs : + {x : X} + outputs : + {out : Out} + attrs : + {kernel_size : ksize} + get_expected_kernel_type : + pool3d : GetPoolExpectedKernelType + pool3d_grad : GetPoolExpectedKernelType extra : - attrs : [bool use_mkldnn = false] + attrs : [bool use_mkldnn = false, bool use_cudnn = false] - op : pow backward : pow_grad, pow_double_grad, pow_triple_grad diff --git a/paddle/phi/api/yaml/static_backward.yaml b/paddle/phi/api/yaml/static_backward.yaml index 108d88fb3eb8ca7eea8cd20cb1498be200477659..736dcc01722d9508c1f3c534a22ca7b0c8d730aa 100755 --- a/paddle/phi/api/yaml/static_backward.yaml +++ b/paddle/phi/api/yaml/static_backward.yaml @@ -123,6 +123,40 @@ func : max_grad composite: max_grad(x, out, out_grad, axis, keepdim, reduce_all, x_grad) +- backward_op : pool2d_double_grad + forward : pool2d_grad(Tensor x, Tensor out, Tensor grad_out, IntArray kernel_size, int[] strides, int[] paddings, bool ceil_mode, bool exclusive, str data_format, str pooling_type, bool global_pooling, bool adaptive, str padding_algorithm) -> Tensor(grad_x) + args : (Tensor grad_x_grad, IntArray kernel_size, int[] strides, int[] paddings, bool ceil_mode, bool exclusive, str data_format, str pooling_type, bool global_pooling, bool adaptive, str padding_algorithm) + output : Tensor(grad_out_grad) + infer_meta : + func : Pool2DInferMeta + param : [grad_x_grad, kernel_size, strides, paddings, ceil_mode, exclusive, data_format, pooling_type, global_pooling, adaptive, padding_algorithm] + kernel : + func : pool2d_double_grad + param : [grad_x_grad, kernel_size, strides, paddings, ceil_mode, exclusive, data_format, pooling_type, global_pooling, adaptive, padding_algorithm] + +- backward_op : pool2d_grad + forward : pool2d(Tensor x, IntArray kernel_size, int[] strides = {1,1}, int[] paddings = {0,0}, bool ceil_mode = false, bool exclusive = true, str data_format = "NCHW", str pooling_type = "", bool global_pooling = false, bool adaptive = false, str padding_algorithm = "EXPLICIT") -> Tensor(out) + args : (Tensor x, Tensor out, Tensor out_grad, IntArray kernel_size, int[] strides, int[] paddings, bool ceil_mode, bool exclusive, str data_format, str pooling_type, bool global_pooling, bool adaptive, str padding_algorithm) + output : Tensor(x_grad) + infer_meta : + func : UnchangedInferMeta + param: [x] + kernel : + func : pool2d_grad + param : [x, out, out_grad, kernel_size, strides, paddings, ceil_mode, exclusive, data_format, pooling_type, global_pooling, adaptive, padding_algorithm] + backward : pool2d_double_grad + +- backward_op : pool3d_grad + forward : pool3d(Tensor x, int[] kernel_size, int[] strides = {1,1,1}, int[] paddings = {0,0,0}, bool ceil_mode = false, bool exclusive = true, str data_format = "NCDHW", str pooling_type = "", bool global_pooling = false, bool adaptive = false, str padding_algorithm = "EXPLICIT") -> Tensor(out) + args : (Tensor x, Tensor out, Tensor out_grad, int[] ksize, int[] strides, int[] paddings, bool ceil_mode, bool exclusive, str data_format, str pooling_type, bool global_pooling, bool adaptive, str padding_algorithm) + output : Tensor(x_grad) + infer_meta : + func : UnchangedInferMeta + param: [x] + kernel : + func : pool3d_grad + param : [x, out, out_grad, kernel_size, strides, paddings, ceil_mode, exclusive, data_format, pooling_type, global_pooling, adaptive, padding_algorithm] + - backward_op : relu6_grad forward : relu6 (Tensor x, float threshold = 6.0f) -> Tensor(out) args : (Tensor out, Tensor out_grad) diff --git a/paddle/phi/api/yaml/static_ops.yaml b/paddle/phi/api/yaml/static_ops.yaml index a6b713d80f9fea91cad7622a7ba6a39fb478c1bc..27bffc64fe595387601cd3328e16c3ed48d57282 100755 --- a/paddle/phi/api/yaml/static_ops.yaml +++ b/paddle/phi/api/yaml/static_ops.yaml @@ -351,6 +351,28 @@ func : p_recv_array param : [peer, dtype, out_shape] +- op : pool2d + args : (Tensor x, IntArray kernel_size, int[] strides = {1,1}, int[] paddings = {0,0}, bool ceil_mode = false, bool exclusive = true, str data_format = "NCHW", str pooling_type = "", bool global_pooling = false, bool adaptive = false, str padding_algorithm = "EXPLICIT") + output : Tensor(out) + infer_meta : + func : Pool2DInferMeta + param : [x, kernel_size, strides, paddings, ceil_mode, exclusive, data_format, pooling_type, global_pooling, adaptive, padding_algorithm] + kernel : + func : pool2d + param : [x, kernel_size, strides, paddings, ceil_mode, exclusive, data_format, pooling_type, global_pooling, adaptive, padding_algorithm] + backward : pool2d_grad + +- op : pool3d + args : (Tensor x, int[] kernel_size, int[] strides = {1,1,1}, int[] paddings = {0,0,0}, bool ceil_mode = false, bool exclusive = true, str data_format = "NCDHW", str pooling_type = "", bool global_pooling = false, bool adaptive = false, str padding_algorithm = "EXPLICIT") + output : Tensor(out) + infer_meta : + func : PoolInferMeta + param : [x, kernel_size, strides, paddings, ceil_mode, exclusive, data_format, pooling_type, global_pooling, adaptive, padding_algorithm] + kernel : + func : pool3d + param : [x, kernel_size, strides, paddings, ceil_mode, exclusive, data_format, pooling_type, global_pooling, adaptive, padding_algorithm] + backward : pool3d_grad + - op : randint args : (int low, int high, IntArray shape = {}, DataType dtype = DataType::INT64, int seed = 0) output : Tensor(out) diff --git a/paddle/phi/kernels/onednn/pool_grad_kernel.cc b/paddle/phi/kernels/onednn/pool_grad_kernel.cc index df68de6a059129957bf80a2f5e705102d55159ff..5c735ac48c6d693f74da48644b008c27f01f9365 100644 --- a/paddle/phi/kernels/onednn/pool_grad_kernel.cc +++ b/paddle/phi/kernels/onednn/pool_grad_kernel.cc @@ -71,6 +71,26 @@ void Pool2dGradKernel(const Context& dev_ctx, dx->set_mem_desc(diff_src_memory->get_desc()); } + +phi::KernelKey PoolOpGradGetKernelTypeForVar( + const GetKernelTypeForVarContext* ctx) { + const DenseTensor& tensor = ctx->GetTensor(); + const KernelKey& expected_kernel_type = ctx->GetKernelKey(); +#ifdef PADDLE_WITH_MKLDNN + if ((expected_kernel_type.layout() == phi::DataLayout::ONEDNN) && + (tensor.layout() != phi::DataLayout::ONEDNN)) { + const AttributeMap& attrs = ctx->GetAttrs(); + auto it = attrs.find("data_format"); + const std::string data_format = PADDLE_GET_CONST(std::string, it->second); + return phi::KernelKey(tensor.place(), + phi::StringToDataLayout(data_format), + expected_kernel_type.dtype()); + } +#endif + return phi::KernelKey( + tensor.place(), tensor.layout(), expected_kernel_type.dtype()); +} + } // namespace phi PD_REGISTER_KERNEL(pool2d_grad, @@ -78,4 +98,6 @@ PD_REGISTER_KERNEL(pool2d_grad, ONEDNN, phi::Pool2dGradKernel, float, - phi::dtype::bfloat16) {} + phi::dtype::bfloat16) { + kernel->get_kerneltype_forvar_fn_ = phi::PoolOpGradGetKernelTypeForVar; +} diff --git a/paddle/phi/kernels/onednn/pool_kernel.cc b/paddle/phi/kernels/onednn/pool_kernel.cc index 726014396b1241aff7bd97d1cc3e92a3c47c1b4c..d4605e3d818b6d637e24c3c25354ead7c059df88 100644 --- a/paddle/phi/kernels/onednn/pool_kernel.cc +++ b/paddle/phi/kernels/onednn/pool_kernel.cc @@ -70,6 +70,29 @@ void Pool2dKernel(const Context& dev_ctx, out->set_mem_desc(dst_memory->get_desc()); } + +phi::KernelKey PoolOpGetKernelTypeForVar( + const GetKernelTypeForVarContext* ctx) { + const phi::DenseTensor& tensor = ctx->GetTensor(); + const phi::KernelKey& expected_kernel_type = ctx->GetKernelKey(); +#ifdef PADDLE_WITH_MKLDNN + if ((expected_kernel_type.layout() == phi::DataLayout::ONEDNN) && + (tensor.layout() != phi::DataLayout::ONEDNN)) { + const AttributeMap& attrs = ctx->GetAttrs(); + auto it = attrs.find("data_format"); + const std::string data_format = PADDLE_GET_CONST(std::string, it->second); + auto dl = phi::StringToDataLayout(data_format); + // Some models may have intentionally set "AnyLayout" for pool + // op. Treat this as NCHW (default data_format value) + if (dl != phi::DataLayout::kAnyLayout) { + return phi::KernelKey(tensor.place(), dl, expected_kernel_type.dtype()); + } + } +#endif + return phi::KernelKey( + tensor.place(), tensor.layout(), expected_kernel_type.dtype()); +} + } // namespace phi PD_REGISTER_KERNEL(pool2d, @@ -79,4 +102,6 @@ PD_REGISTER_KERNEL(pool2d, float, int8_t, uint8_t, - phi::dtype::bfloat16) {} + phi::dtype::bfloat16) { + kernel->get_kerneltype_forvar_fn_ = phi::PoolOpGetKernelTypeForVar; +} diff --git a/paddle/phi/ops/compat/pool_sig.cc b/paddle/phi/ops/compat/pool_sig.cc deleted file mode 100644 index 9d5fda52cf2e32e04a3b135e3afbdae6e26965f0..0000000000000000000000000000000000000000 --- a/paddle/phi/ops/compat/pool_sig.cc +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright (c) 2022 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/phi/core/compat/op_utils.h" - -namespace phi { - -KernelSignature Pool2dOpArgumentMapping( - const ArgumentMappingContext& ctx UNUSED) { - return KernelSignature("pool2d", - {"X"}, - {"ksize", - "strides", - "paddings", - "ceil_mode", - "exclusive", - "data_format", - "pooling_type", - "global_pooling", - "adaptive", - "padding_algorithm"}, - {"Out"}); -} - -KernelSignature Pool2dGradOpArgumentMapping( - const ArgumentMappingContext& ctx UNUSED) { - return KernelSignature("pool2d_grad", - {"X", "Out", "Out@GRAD"}, - {"ksize", - "strides", - "paddings", - "ceil_mode", - "exclusive", - "data_format", - "pooling_type", - "global_pooling", - "adaptive", - "padding_algorithm"}, - {"X@GRAD"}); -} - -KernelSignature Pool2dDoubleGradOpArgumentMapping( - const ArgumentMappingContext& ctx UNUSED) { - return KernelSignature("pool2d_double_grad", - {"X"}, - {"ksize", - "strides", - "paddings", - "ceil_mode", - "exclusive", - "data_format", - "pooling_type", - "global_pooling", - "adaptive", - "padding_algorithm"}, - {"Out"}); -} - -KernelSignature Pool3dOpArgumentMapping( - const ArgumentMappingContext& ctx UNUSED) { - return KernelSignature("pool3d", - {"X"}, - {"ksize", - "strides", - "paddings", - "ceil_mode", - "exclusive", - "data_format", - "pooling_type", - "global_pooling", - "adaptive", - "padding_algorithm"}, - {"Out"}); -} - -KernelSignature Pool3dGradOpArgumentMapping( - const ArgumentMappingContext& ctx UNUSED) { - return KernelSignature("pool3d_grad", - {"X", "Out", "Out@GRAD"}, - {"ksize", - "strides", - "paddings", - "ceil_mode", - "exclusive", - "data_format", - "pooling_type", - "global_pooling", - "adaptive", - "padding_algorithm"}, - {"X@GRAD"}); -} - -} // namespace phi - -PD_REGISTER_ARG_MAPPING_FN(pool2d, phi::Pool2dOpArgumentMapping); -PD_REGISTER_ARG_MAPPING_FN(pool2d_grad, phi::Pool2dGradOpArgumentMapping); -PD_REGISTER_ARG_MAPPING_FN(pool2d_double_grad, - phi::Pool2dDoubleGradOpArgumentMapping); - -PD_REGISTER_ARG_MAPPING_FN(pool3d, phi::Pool3dOpArgumentMapping); -PD_REGISTER_ARG_MAPPING_FN(pool3d_grad, phi::Pool3dGradOpArgumentMapping); diff --git a/test/cpp/fluid/mkldnn/CMakeLists.txt b/test/cpp/fluid/mkldnn/CMakeLists.txt index c3621eaff0fce8bf8e41fa694a99b2aa9a0f090e..fb8415977ad03049eac1598afbb629364346a6e4 100644 --- a/test/cpp/fluid/mkldnn/CMakeLists.txt +++ b/test/cpp/fluid/mkldnn/CMakeLists.txt @@ -41,7 +41,7 @@ cc_test_old( recurrent_op_helper recurrent_op op_registry - pool_op + generated_static_op crop_op activation_op generated_op