From 96c3107a0e072b21540015941da03a87971b6cec Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Mon, 5 Dec 2022 13:18:35 +0000 Subject: [PATCH 01/19] move inplace_apis_indygraph_only from paddle.flud.dygraph.inplace_utils to paddle.utils --- python/paddle/fluid/dygraph/__init__.py | 2 +- python/paddle/nn/functional/activation.py | 2 +- python/paddle/tensor/math.py | 2 +- python/paddle/utils.py | 39 +++++++++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 python/paddle/utils.py diff --git a/python/paddle/fluid/dygraph/__init__.py b/python/paddle/fluid/dygraph/__init__.py index 875b7e28107d44..103c9183979f59 100644 --- a/python/paddle/fluid/dygraph/__init__.py +++ b/python/paddle/fluid/dygraph/__init__.py @@ -56,7 +56,7 @@ from .math_op_patch import monkey_patch_math_varbase -from .inplace_utils import inplace_apis_in_dygraph_only +from paddle.utils import inplace_apis_in_dygraph_only __all__ = [] __all__ += layers.__all__ diff --git a/python/paddle/nn/functional/activation.py b/python/paddle/nn/functional/activation.py index af5fa1336f1f0b..5a0c82e9fda2be 100644 --- a/python/paddle/nn/functional/activation.py +++ b/python/paddle/nn/functional/activation.py @@ -16,7 +16,7 @@ from ...tensor.math import tanh # noqa: F401 from ...tensor.math import tanh_ # noqa: F401 -from ...fluid.dygraph.inplace_utils import inplace_apis_in_dygraph_only +from paddle.utils import inplace_apis_in_dygraph_only from ...tensor.manipulation import chunk from ...fluid.layer_helper import LayerHelper diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index cf6ff6633bb6f2..09b32fbce018bc 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -42,7 +42,7 @@ check_dtype, convert_dtype, ) -from ..fluid.dygraph.inplace_utils import inplace_apis_in_dygraph_only +from paddle.utils import inplace_apis_in_dygraph_only from ..fluid.layers import utils # TODO: define math functions diff --git a/python/paddle/utils.py b/python/paddle/utils.py new file mode 100644 index 00000000000000..39bff5003a167b --- /dev/null +++ b/python/paddle/utils.py @@ -0,0 +1,39 @@ +# 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. + +from paddle.fluid.wrapped_decorator import wrap_decorator +from paddle.fluid.framework import _non_static_mode +import warnings + + +# NOTE(pangyoki): The Inplace APIs with underline(`_`) is only valid for the method of calling `_C_ops` +# in dygraph mode. If static mode is used, the inplace mechanism will not be used, and the static method +# of the original API will be called. +def _inplace_apis_in_dygraph_only_(func): + def __impl__(*args, **kwargs): + if not _non_static_mode(): + origin_api_name = func.__name__[:-1] + warnings.warn( + "In static mode, {}() is the same as {}() and does not perform inplace operation.".format( + func.__name__, origin_api_name + ) + ) + origin_func = "{}.{}".format(func.__module__, origin_api_name) + return eval(origin_func)(*args, **kwargs) + return func(*args, **kwargs) + + return __impl__ + + +inplace_apis_in_dygraph_only = wrap_decorator(_inplace_apis_in_dygraph_only_) From 751241cce821c2d6f3851c7eee7ff6d34cbb90e2 Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Mon, 5 Dec 2022 13:33:57 +0000 Subject: [PATCH 02/19] modify conflict --- python/paddle/fluid/dygraph/inplace_utils.py | 41 -------------------- python/paddle/nn/functional/activation.py | 27 +++++++++---- python/paddle/tensor/math.py | 16 +++++++- 3 files changed, 34 insertions(+), 50 deletions(-) delete mode 100644 python/paddle/fluid/dygraph/inplace_utils.py diff --git a/python/paddle/fluid/dygraph/inplace_utils.py b/python/paddle/fluid/dygraph/inplace_utils.py deleted file mode 100644 index fb27a5674b7d8b..00000000000000 --- a/python/paddle/fluid/dygraph/inplace_utils.py +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright (c) 2021 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. - -from ..wrapped_decorator import wrap_decorator -from ..framework import _non_static_mode -import warnings -import paddle -from paddle import _C_ops, _legacy_C_ops - - -# NOTE(pangyoki): The Inplace APIs with underline(`_`) is only valid for the method of calling `_C_ops` -# in dygraph mode. If static mode is used, the inplace mechanism will not be used, and the static method -# of the original API will be called. -def _inplace_apis_in_dygraph_only_(func): - def __impl__(*args, **kwargs): - if not _non_static_mode(): - origin_api_name = func.__name__[:-1] - warnings.warn( - "In static mode, {}() is the same as {}() and does not perform inplace operation.".format( - func.__name__, origin_api_name - ) - ) - origin_func = "{}.{}".format(func.__module__, origin_api_name) - return eval(origin_func)(*args, **kwargs) - return func(*args, **kwargs) - - return __impl__ - - -inplace_apis_in_dygraph_only = wrap_decorator(_inplace_apis_in_dygraph_only_) diff --git a/python/paddle/nn/functional/activation.py b/python/paddle/nn/functional/activation.py index 5a0c82e9fda2be..f1a44a51903dca 100644 --- a/python/paddle/nn/functional/activation.py +++ b/python/paddle/nn/functional/activation.py @@ -13,21 +13,32 @@ # limitations under the License. from ...tensor.ops import sigmoid # noqa: F401 -from ...tensor.math import tanh # noqa: F401 -from ...tensor.math import tanh_ # noqa: F401 -from paddle.utils import inplace_apis_in_dygraph_only -from ...tensor.manipulation import chunk -from ...fluid.layer_helper import LayerHelper -from ...fluid.framework import convert_np_dtype_to_dtype_ -from ...fluid.framework import _in_legacy_dygraph, in_dygraph_mode -from ...fluid.data_feeder import check_variable_and_dtype, check_dtype +from paddle.utils import inplace_apis_in_dygraph_only import paddle from paddle import _C_ops, _legacy_C_ops, in_dynamic_mode from paddle.framework import core from paddle.fluid.framework import _in_legacy_dygraph, in_dygraph_mode +import paddle +from paddle.utils import inplace_apis_in_dygraph_only +from paddle import _C_ops, _legacy_C_ops, in_dynamic_mode +from paddle.framework import core + +from ...fluid.data_feeder import check_dtype, check_variable_and_dtype +from ...fluid.dygraph.inplace_utils import inplace_apis_in_dygraph_only +from ...fluid.framework import ( + _in_legacy_dygraph, + convert_np_dtype_to_dtype_, + in_dygraph_mode, +) +from ...fluid.layer_helper import LayerHelper +from ...tensor.manipulation import chunk +from ...tensor.math import tanh # noqa: F401 +from ...tensor.math import tanh_ # noqa: F401 +from ...tensor.ops import sigmoid # noqa: F401 + __all__ = [] diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 09b32fbce018bc..b43bbc7222850c 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -43,9 +43,23 @@ convert_dtype, ) from paddle.utils import inplace_apis_in_dygraph_only -from ..fluid.layers import utils # TODO: define math functions +from ..fluid.dygraph.inplace_utils import inplace_apis_in_dygraph_only +from ..fluid.layers import elementwise_sub, utils +from ..framework import ( + LayerHelper, + _in_legacy_dygraph, + _non_static_mode, + _varbase_creator, + convert_np_dtype_to_dtype_, + core, + in_dygraph_mode, +) +from ..static import Variable +from .creation import _complex_to_real_dtype +from .layer_function_generator import generate_layer_fn, templatedoc +from .manipulation import cast from .ops import abs # noqa: F401 from .ops import acos # noqa: F401 from .ops import asin # noqa: F401 From 9ec77cb8a291ba54b4b1cb0f8d6bac63c0bc8c4e Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Mon, 5 Dec 2022 13:41:49 +0000 Subject: [PATCH 03/19] modify conflict --- python/paddle/nn/functional/activation.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/python/paddle/nn/functional/activation.py b/python/paddle/nn/functional/activation.py index f1a44a51903dca..3a4e9909f2e964 100644 --- a/python/paddle/nn/functional/activation.py +++ b/python/paddle/nn/functional/activation.py @@ -12,15 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ...tensor.ops import sigmoid # noqa: F401 - - -from paddle.utils import inplace_apis_in_dygraph_only -import paddle -from paddle import _C_ops, _legacy_C_ops, in_dynamic_mode -from paddle.framework import core -from paddle.fluid.framework import _in_legacy_dygraph, in_dygraph_mode - import paddle from paddle.utils import inplace_apis_in_dygraph_only from paddle import _C_ops, _legacy_C_ops, in_dynamic_mode From 654caca656c6e5ce5ef5639b2f6ff865a1657301 Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Mon, 5 Dec 2022 13:47:23 +0000 Subject: [PATCH 04/19] modify conflict --- python/paddle/tensor/math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index b43bbc7222850c..f2e7c98d192ef8 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -42,10 +42,10 @@ check_dtype, convert_dtype, ) + from paddle.utils import inplace_apis_in_dygraph_only # TODO: define math functions -from ..fluid.dygraph.inplace_utils import inplace_apis_in_dygraph_only from ..fluid.layers import elementwise_sub, utils from ..framework import ( LayerHelper, From f440ac82587a463eefee1d4d635e096d0a44bf68 Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Mon, 5 Dec 2022 13:54:16 +0000 Subject: [PATCH 05/19] modify conflict --- python/paddle/tensor/math.py | 49 ++++++++++++------------------------ 1 file changed, 16 insertions(+), 33 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index f2e7c98d192ef8..bd218ff0e2d0b4 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -14,32 +14,18 @@ """ math functions """ -import numpy as np - -from paddle.common_ops_import import VarDesc -from paddle.common_ops_import import dygraph_only -from paddle.common_ops_import import templatedoc -from paddle.common_ops_import import dygraph_utils +# TODO: define math functions -from .manipulation import cast -from .creation import _complex_to_real_dtype -from .layer_function_generator import generate_layer_fn +import numpy as np import paddle -from ..static import Variable -from ..framework import ( - core, - in_dygraph_mode, - _non_static_mode, - LayerHelper, - _in_legacy_dygraph, -) -from ..fluid.framework import _in_legacy_dygraph -from ..framework import _varbase_creator, convert_np_dtype_to_dtype_ +from paddle import _C_ops, _legacy_C_ops +from paddle.common_ops_import import VarDesc, dygraph_only, dygraph_utils + from ..fluid.data_feeder import ( - check_variable_and_dtype, - check_type, check_dtype, + check_type, + check_variable_and_dtype, convert_dtype, ) @@ -62,13 +48,16 @@ from .manipulation import cast from .ops import abs # noqa: F401 from .ops import acos # noqa: F401 +from .ops import acosh # noqa: F401 from .ops import asin # noqa: F401 +from .ops import asinh # noqa: F401 +from .ops import atan # noqa: F401 +from .ops import atanh # noqa: F401 from .ops import ceil # noqa: F401 from .ops import ceil_ # noqa: F401 from .ops import cos # noqa: F401 -from .ops import tan # noqa: F401 -from .ops import sinh # noqa: F401 from .ops import cosh # noqa: F401 +from .ops import erf # noqa: F401 from .ops import exp # noqa: F401 from .ops import exp_ # noqa: F401 from .ops import expm1 # noqa: F401 @@ -80,18 +69,12 @@ from .ops import round_ # noqa: F401 from .ops import rsqrt # noqa: F401 from .ops import rsqrt_ # noqa: F401 -from .ops import square # noqa: F401 -from .ops import atan # noqa: F401 -from .ops import erf # noqa: F401 +from .ops import sin # noqa: F401 +from .ops import sinh # noqa: F401 from .ops import sqrt # noqa: F401 from .ops import sqrt_ # noqa: F401 -from .ops import sin # noqa: F401 -from .ops import asinh # noqa: F401 -from .ops import acosh # noqa: F401 -from .ops import atanh # noqa: F401 - -from ..fluid.layers import elementwise_sub -from paddle import _C_ops, _legacy_C_ops +from .ops import square # noqa: F401 +from .ops import tan # noqa: F401 __all__ = [] From a0a878c4b1767b99993c7d8442e62463c8d39747 Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Mon, 5 Dec 2022 13:56:14 +0000 Subject: [PATCH 06/19] modify conflict --- python/paddle/tensor/math.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index bd218ff0e2d0b4..02ec96054152ae 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -29,9 +29,8 @@ convert_dtype, ) -from paddle.utils import inplace_apis_in_dygraph_only - # TODO: define math functions +from paddle.utils import inplace_apis_in_dygraph_only from ..fluid.layers import elementwise_sub, utils from ..framework import ( LayerHelper, From 3ea1763d1e79316ca75457ff0ed1e79023408033 Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Mon, 5 Dec 2022 14:04:31 +0000 Subject: [PATCH 07/19] modify conflict --- python/paddle/nn/functional/activation.py | 3 +-- python/paddle/tensor/math.py | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/python/paddle/nn/functional/activation.py b/python/paddle/nn/functional/activation.py index 3a4e9909f2e964..3fc441d4144f18 100644 --- a/python/paddle/nn/functional/activation.py +++ b/python/paddle/nn/functional/activation.py @@ -13,12 +13,11 @@ # limitations under the License. import paddle -from paddle.utils import inplace_apis_in_dygraph_only from paddle import _C_ops, _legacy_C_ops, in_dynamic_mode from paddle.framework import core +from paddle.utils import inplace_apis_in_dygraph_only from ...fluid.data_feeder import check_dtype, check_variable_and_dtype -from ...fluid.dygraph.inplace_utils import inplace_apis_in_dygraph_only from ...fluid.framework import ( _in_legacy_dygraph, convert_np_dtype_to_dtype_, diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 58a13efc2b61ff..0c747d8f72f761 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -22,15 +22,15 @@ from paddle import _C_ops, _legacy_C_ops from paddle.common_ops_import import VarDesc, dygraph_only, dygraph_utils +# TODO: define math functions +from paddle.utils import inplace_apis_in_dygraph_only + from ..fluid.data_feeder import ( check_dtype, check_type, check_variable_and_dtype, convert_dtype, ) - -# TODO: define math functions -from paddle.utils import inplace_apis_in_dygraph_only from ..fluid.layers import elementwise_sub, utils from ..framework import ( LayerHelper, From 1f344556a864996f7d5d62d7371285c617a1b939 Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Mon, 5 Dec 2022 14:07:06 +0000 Subject: [PATCH 08/19] modify conflict --- python/paddle/utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/paddle/utils.py b/python/paddle/utils.py index 39bff5003a167b..0e104afe4a3269 100644 --- a/python/paddle/utils.py +++ b/python/paddle/utils.py @@ -12,10 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.fluid.wrapped_decorator import wrap_decorator -from paddle.fluid.framework import _non_static_mode import warnings +from paddle.fluid.framework import _non_static_mode +from paddle.fluid.wrapped_decorator import wrap_decorator + # NOTE(pangyoki): The Inplace APIs with underline(`_`) is only valid for the method of calling `_C_ops` # in dygraph mode. If static mode is used, the inplace mechanism will not be used, and the static method From e7ee1d9ba5062f7cf1097e3c1de36f71cd318253 Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Mon, 5 Dec 2022 14:40:46 +0000 Subject: [PATCH 09/19] modify static-check ci error --- python/paddle/fluid/dygraph/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/paddle/fluid/dygraph/__init__.py b/python/paddle/fluid/dygraph/__init__.py index fe8f4eda994657..47ddae2a9b8221 100644 --- a/python/paddle/fluid/dygraph/__init__.py +++ b/python/paddle/fluid/dygraph/__init__.py @@ -51,7 +51,6 @@ from .math_op_patch import monkey_patch_math_varbase -from paddle.utils import inplace_apis_in_dygraph_only __all__ = [] __all__ += layers.__all__ From b70ca482bb137529221db1573513b243d0c5f1a9 Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Mon, 5 Dec 2022 14:41:51 +0000 Subject: [PATCH 10/19] fix conflict --- python/paddle/fluid/dygraph/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/paddle/fluid/dygraph/__init__.py b/python/paddle/fluid/dygraph/__init__.py index 47ddae2a9b8221..1132ef393d552c 100644 --- a/python/paddle/fluid/dygraph/__init__.py +++ b/python/paddle/fluid/dygraph/__init__.py @@ -51,7 +51,6 @@ from .math_op_patch import monkey_patch_math_varbase - __all__ = [] __all__ += layers.__all__ __all__ += base.__all__ From 28327b94a1116a258b04dab21d118443041265c0 Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Tue, 6 Dec 2022 01:30:31 +0000 Subject: [PATCH 11/19] modify failed tests --- python/paddle/tensor/manipulation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 8c47809d222a90..989164b9068bb8 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -20,6 +20,7 @@ import paddle from paddle import _C_ops, _legacy_C_ops +from paddle.utils import inplace_apis_in_dygraph_only from ..common_ops_import import _varbase_creator, fill_constant from ..fluid.data_feeder import ( @@ -28,7 +29,6 @@ check_variable_and_dtype, convert_dtype, ) -from ..fluid.dygraph.inplace_utils import inplace_apis_in_dygraph_only from ..fluid.framework import _in_legacy_dygraph, _non_static_mode from ..fluid.layers import utils from ..framework import ( From 552b206a4ebb965da34317310830f8d0247fb5d2 Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Tue, 6 Dec 2022 02:41:55 +0000 Subject: [PATCH 12/19] fix conflict --- python/paddle/nn/functional/activation.py | 2 +- python/paddle/tensor/math.py | 2 +- python/paddle/{utils.py => utils/inplace_utils.py} | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename python/paddle/{utils.py => utils/inplace_utils.py} (95%) diff --git a/python/paddle/nn/functional/activation.py b/python/paddle/nn/functional/activation.py index 3fc441d4144f18..ca855b057b3a55 100644 --- a/python/paddle/nn/functional/activation.py +++ b/python/paddle/nn/functional/activation.py @@ -15,7 +15,7 @@ import paddle from paddle import _C_ops, _legacy_C_ops, in_dynamic_mode from paddle.framework import core -from paddle.utils import inplace_apis_in_dygraph_only +from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only from ...fluid.data_feeder import check_dtype, check_variable_and_dtype from ...fluid.framework import ( diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 0c747d8f72f761..9ab909fa2dfb22 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -23,7 +23,7 @@ from paddle.common_ops_import import VarDesc, dygraph_only, dygraph_utils # TODO: define math functions -from paddle.utils import inplace_apis_in_dygraph_only +from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only from ..fluid.data_feeder import ( check_dtype, diff --git a/python/paddle/utils.py b/python/paddle/utils/inplace_utils.py similarity index 95% rename from python/paddle/utils.py rename to python/paddle/utils/inplace_utils.py index 0e104afe4a3269..f1f0050d41e32e 100644 --- a/python/paddle/utils.py +++ b/python/paddle/utils/inplace_utils.py @@ -1,4 +1,4 @@ -# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# 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. From c0372933757f335723b475b2865b0480313db04f Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Tue, 6 Dec 2022 02:43:49 +0000 Subject: [PATCH 13/19] fix conflict --- python/paddle/tensor/manipulation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 989164b9068bb8..ef8d154714aecd 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -20,7 +20,7 @@ import paddle from paddle import _C_ops, _legacy_C_ops -from paddle.utils import inplace_apis_in_dygraph_only +from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only from ..common_ops_import import _varbase_creator, fill_constant from ..fluid.data_feeder import ( From ac5e94fc94d38dd1604468ac08f8ff368cf1f578 Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Tue, 6 Dec 2022 04:09:39 +0000 Subject: [PATCH 14/19] fix pool2d examples --- python/paddle/fluid/dygraph/nn.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/dygraph/nn.py b/python/paddle/fluid/dygraph/nn.py index abef927af86fd3..3754edaf0bd7c7 100644 --- a/python/paddle/fluid/dygraph/nn.py +++ b/python/paddle/fluid/dygraph/nn.py @@ -13,11 +13,13 @@ # limitations under the License. import paddle + from .. import core from ..layers import utils from ..layers import nn as F from .. import dygraph_utils from . import layers + from ..framework import ( Variable, _non_static_mode, @@ -602,7 +604,7 @@ class Pool2D(layers.Layer): import paddle.fluid as fluid from paddle.fluid.dygraph.base import to_variable - import numpy as np + import numpy with fluid.dygraph.guard(): data = numpy.random.random((3, 32, 32, 5)).astype('float32') From 942933a89c3c25da0d1a4641fc3e1d8762f5d6ff Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Tue, 6 Dec 2022 04:12:42 +0000 Subject: [PATCH 15/19] modify conflict --- python/paddle/fluid/dygraph/nn.py | 232 ------------------------------ 1 file changed, 232 deletions(-) diff --git a/python/paddle/fluid/dygraph/nn.py b/python/paddle/fluid/dygraph/nn.py index 3754edaf0bd7c7..e52ab166e7264c 100644 --- a/python/paddle/fluid/dygraph/nn.py +++ b/python/paddle/fluid/dygraph/nn.py @@ -510,238 +510,6 @@ def forward(self, input): return self._helper.append_activation(pre_act, act=self._act) -class Pool2D(layers.Layer): - r""" - - This interface is used to construct a callable object of the ``Pool2D`` class. - For more details, refer to code examples. - The pooling2d operation calculates the output based on the input, pool_type and pool_size, pool_stride, - pool_padding parameters.Input and output are in NCHW format, where N is batch size, C is the number of feature map, - H is the height of the feature map, and W is the width of the feature map. - Parameters(ksize, strides, paddings) are two elements. These two elements represent height and width, respectively. - The input(X) size and output(Out) size may be different. - - Example: - - - Input: - - Input shape: :math:`(N, C, H_{in}, W_{in})` - - - Output: - - Output shape: :math:`(N, C, H_{out}, W_{out})` - - If ``ceil_mode`` = False: - - .. math:: - - H_{out} = \\frac{(H_{in} - ksize[0] + 2 * paddings[0])}{strides[0]} + 1 \\\\ - W_{out} = \\frac{(W_{in} - ksize[1] + 2 * paddings[1])}{strides[1]} + 1 - - If ``ceil_mode`` = True: - - .. math:: - - H_{out} = \\frac{(H_{in} - ksize[0] + 2 * paddings[0] + strides[0] - 1)}{strides[0]} + 1 \\\\ - W_{out} = \\frac{(W_{in} - ksize[1] + 2 * paddings[1] + strides[1] - 1)}{strides[1]} + 1 - - If ``exclusive`` = False: - - .. math:: - - hstart &= i * strides[0] - paddings[0] \\\\ - hend &= hstart + ksize[0] \\\\ - wstart &= j * strides[1] - paddings[1] \\\\ - wend &= wstart + ksize[1] \\\\ - Output(i ,j) &= \\frac{sum(Input[hstart:hend, wstart:wend])}{ksize[0] * ksize[1]} - - If ``exclusive`` = True: - - .. math:: - - hstart &= max(0, i * strides[0] - paddings[0])\\\\ - hend &= min(H, hstart + ksize[0]) \\\\ - wstart &= max(0, j * strides[1] - paddings[1]) \\\\ - wend & = min(W, wstart + ksize[1]) \\\\ - Output(i ,j) & = \\frac{sum(Input[hstart:hend, wstart:wend])}{(hend - hstart) * (wend - wstart)} - - Parameters: - pool_size (int or list or tuple, optional): The pool kernel size. If pool kernel size is a tuple or list, - it must contain two integers, (pool_size_Height, pool_size_Width). - Otherwise, the pool kernel size will be a square of an int. Default: -1. - pool_type(str, optional) : The pooling type, can be "max" for max-pooling and "avg" for average-pooling. - Default: max. - pool_stride (int or list or tuple, optional): The pool stride size. If pool stride size is a tuple or list, - it must contain two integers, (pool_stride_Height, pool_stride_Width). Otherwise, - the pool stride size will be a square of an int. Default: 1. - pool_padding (int or list or tuple, optional): The padding size for pooling operation. - If ``pool_padding`` is a tuple, - it must contain two integers, (pool_padding_on_Height, pool_padding_on_Width). - Otherwise, the padding size for pooling operation will be a square of an int. Default: 0. - global_pooling (bool, optional): Whether to use the global pooling. If global_pooling = true, - kernel size and paddings will be ignored. Default: False. - use_cudnn (bool, optional): Only used in cudnn kernel, need install cudnn. Default: True. - ceil_mode (bool, optional): 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. - exclusive (bool, optional): Whether to exclude padding points in average pooling mode. Default: True. - data_format (string): The data format of the input and output data. An optional string from: `"NCHW"`, `"NHWC"`. - The default is `"NCHW"`. When it is `"NCHW"`, the data is stored in the order of: - ``[batch_size, input_channels, input_height, input_width]``. When it is `"NHWC"`, the data is - stored in the order of: ``[batch_size, input_height, input_width, input_channels]`` - - Returns: - None - - Raises: - ValueError: If ``pool_type`` is not "max" nor "avg". - ValueError: If ``global_pooling`` is False and ``pool_size`` is -1. - ValueError: If ``use_cudnn`` is not a bool value. - ValueError: If ``data_format`` is not "NCHW" nor "NHWC". - - Examples: - - .. code-block:: python - - import paddle.fluid as fluid - from paddle.fluid.dygraph.base import to_variable - import numpy - - with fluid.dygraph.guard(): - data = numpy.random.random((3, 32, 32, 5)).astype('float32') - pool2d = fluid.dygraph.Pool2D(pool_size=2, - pool_type='max', - pool_stride=1, - global_pooling=False) - pool2d_res = pool2d(to_variable(data)) - - """ - - def __init__( - self, - pool_size=-1, - pool_type="max", - pool_stride=1, - pool_padding=0, - global_pooling=False, - use_cudnn=True, - ceil_mode=False, - exclusive=True, - data_format="NCHW", - ): - data_format = data_format.upper() # supprt NHWC, nhwc, etc. - pool_type = pool_type.lower() # supprt max, Max, etc. - if pool_type not in ["max", "avg"]: - raise ValueError( - "Unknown pool_type: '%s'. It can only be 'max' or 'avg'.", - str(pool_type), - ) - - if global_pooling is False and pool_size == -1: - raise ValueError( - "When the global_pooling is False, pool_size must be passed " - "and be a valid value. Received pool_size: " + str(pool_size) - ) - - if not isinstance(use_cudnn, bool): - raise ValueError("use_cudnn should be True or False") - - self._use_mkldnn = _global_flags()["FLAGS_use_mkldnn"] - - if data_format not in ["NCHW", "NHWC"]: - raise ValueError( - "Attr(data_format) should be 'NCHW' or 'NHWC'. Received " - "Attr(data_format): %s." % str(data_format) - ) - - super().__init__() - - self._pool_type = pool_type - self._pool_size = utils.convert_to_list(pool_size, 2, 'pool_size') - self._pool_padding = utils.convert_to_list( - pool_padding, 2, 'pool_padding' - ) - self._pool_stride = utils.convert_to_list(pool_stride, 2, 'pool_stride') - self._global_pooling = global_pooling - self._use_cudnn = use_cudnn - self._ceil_mode = ceil_mode - self._exclusive = exclusive - self._data_format = data_format - self._l_type = 'pool2d' - - def forward(self, input): - if _non_static_mode(): - if not self._use_mkldnn and in_dygraph_mode(): - input = input._use_gpudnn(self._use_cudnn) - return _C_ops.pool2d( - input, - self._pool_size, - self._pool_stride, - self._pool_padding, - self._ceil_mode, - self._exclusive, - self._data_format, - self._pool_type, - self._global_pooling, - False, - "EXPLICIT", - ) - - attrs = ( - 'pooling_type', - self._pool_type, - 'ksize', - self._pool_size, - 'global_pooling', - self._global_pooling, - 'strides', - self._pool_stride, - 'paddings', - self._pool_padding, - 'use_cudnn', - self._use_cudnn, - 'ceil_mode', - self._ceil_mode, - 'use_mkldnn', - self._use_mkldnn, - 'exclusive', - self._exclusive, - 'data_format', - self._data_format, - ) - return _legacy_C_ops.pool2d(input, *attrs) - - check_variable_and_dtype( - input, - 'input', - ['int8', 'uint8', 'float16', 'float32', 'float64'], - 'Pool2D', - ) - - attrs = { - "pooling_type": self._pool_type, - "ksize": self._pool_size, - "global_pooling": self._global_pooling, - "strides": self._pool_stride, - "paddings": self._pool_padding, - "use_cudnn": self._use_cudnn, - "ceil_mode": self._ceil_mode, - "use_mkldnn": self._use_mkldnn, - "exclusive": self._exclusive, - "data_format": self._data_format, - } - inputs = {"X": [input]} - - pool_out = self._helper.create_variable_for_type_inference(self._dtype) - - self._helper.append_op( - type=self._l_type, - inputs={"X": input}, - outputs={"Out": pool_out}, - attrs=attrs, - ) - return pool_out - - class Linear(layers.Layer): """ From a9cc93c02ad3147378a7b4a61889d47e51cacfaf Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Tue, 6 Dec 2022 14:45:39 +0000 Subject: [PATCH 16/19] fix failed tests --- python/paddle/fluid/dygraph/nn.py | 208 +++++++++++++++++++++++++++++- 1 file changed, 206 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/dygraph/nn.py b/python/paddle/fluid/dygraph/nn.py index e52ab166e7264c..fc156de8966ad2 100644 --- a/python/paddle/fluid/dygraph/nn.py +++ b/python/paddle/fluid/dygraph/nn.py @@ -13,13 +13,11 @@ # limitations under the License. import paddle - from .. import core from ..layers import utils from ..layers import nn as F from .. import dygraph_utils from . import layers - from ..framework import ( Variable, _non_static_mode, @@ -510,6 +508,212 @@ def forward(self, input): return self._helper.append_activation(pre_act, act=self._act) +class Pool2D(layers.Layer): + r""" + This interface is used to construct a callable object of the ``Pool2D`` class. + For more details, refer to code examples. + The pooling2d operation calculates the output based on the input, pool_type and pool_size, pool_stride, + pool_padding parameters.Input and output are in NCHW format, where N is batch size, C is the number of feature map, + H is the height of the feature map, and W is the width of the feature map. + Parameters(ksize, strides, paddings) are two elements. These two elements represent height and width, respectively. + The input(X) size and output(Out) size may be different. + Example: + - Input: + Input shape: :math:`(N, C, H_{in}, W_{in})` + - Output: + Output shape: :math:`(N, C, H_{out}, W_{out})` + If ``ceil_mode`` = False: + .. math:: + H_{out} = \\frac{(H_{in} - ksize[0] + 2 * paddings[0])}{strides[0]} + 1 \\\\ + W_{out} = \\frac{(W_{in} - ksize[1] + 2 * paddings[1])}{strides[1]} + 1 + If ``ceil_mode`` = True: + .. math:: + H_{out} = \\frac{(H_{in} - ksize[0] + 2 * paddings[0] + strides[0] - 1)}{strides[0]} + 1 \\\\ + W_{out} = \\frac{(W_{in} - ksize[1] + 2 * paddings[1] + strides[1] - 1)}{strides[1]} + 1 + If ``exclusive`` = False: + .. math:: + hstart &= i * strides[0] - paddings[0] \\\\ + hend &= hstart + ksize[0] \\\\ + wstart &= j * strides[1] - paddings[1] \\\\ + wend &= wstart + ksize[1] \\\\ + Output(i ,j) &= \\frac{sum(Input[hstart:hend, wstart:wend])}{ksize[0] * ksize[1]} + If ``exclusive`` = True: + .. math:: + hstart &= max(0, i * strides[0] - paddings[0])\\\\ + hend &= min(H, hstart + ksize[0]) \\\\ + wstart &= max(0, j * strides[1] - paddings[1]) \\\\ + wend & = min(W, wstart + ksize[1]) \\\\ + Output(i ,j) & = \\frac{sum(Input[hstart:hend, wstart:wend])}{(hend - hstart) * (wend - wstart)} + Parameters: + pool_size (int or list or tuple, optional): The pool kernel size. If pool kernel size is a tuple or list, + it must contain two integers, (pool_size_Height, pool_size_Width). + Otherwise, the pool kernel size will be a square of an int. Default: -1. + pool_type(str, optional) : The pooling type, can be "max" for max-pooling and "avg" for average-pooling. + Default: max. + pool_stride (int or list or tuple, optional): The pool stride size. If pool stride size is a tuple or list, + it must contain two integers, (pool_stride_Height, pool_stride_Width). Otherwise, + the pool stride size will be a square of an int. Default: 1. + pool_padding (int or list or tuple, optional): The padding size for pooling operation. + If ``pool_padding`` is a tuple, + it must contain two integers, (pool_padding_on_Height, pool_padding_on_Width). + Otherwise, the padding size for pooling operation will be a square of an int. Default: 0. + global_pooling (bool, optional): Whether to use the global pooling. If global_pooling = true, + kernel size and paddings will be ignored. Default: False. + use_cudnn (bool, optional): Only used in cudnn kernel, need install cudnn. Default: True. + ceil_mode (bool, optional): 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. + exclusive (bool, optional): Whether to exclude padding points in average pooling mode. Default: True. + data_format (string): The data format of the input and output data. An optional string from: `"NCHW"`, `"NHWC"`. + The default is `"NCHW"`. When it is `"NCHW"`, the data is stored in the order of: + ``[batch_size, input_channels, input_height, input_width]``. When it is `"NHWC"`, the data is + stored in the order of: ``[batch_size, input_height, input_width, input_channels]`` + Returns: + None + Raises: + ValueError: If ``pool_type`` is not "max" nor "avg". + ValueError: If ``global_pooling`` is False and ``pool_size`` is -1. + ValueError: If ``use_cudnn`` is not a bool value. + ValueError: If ``data_format`` is not "NCHW" nor "NHWC". + Examples: + .. code-block:: python + import paddle.fluid as fluid + from paddle.fluid.dygraph.base import to_variable + import numpy as np + with fluid.dygraph.guard(): + data = numpy.random.random((3, 32, 32, 5)).astype('float32') + pool2d = fluid.dygraph.Pool2D(pool_size=2, + pool_type='max', + pool_stride=1, + global_pooling=False) + pool2d_res = pool2d(to_variable(data)) + """ + + def __init__( + self, + pool_size=-1, + pool_type="max", + pool_stride=1, + pool_padding=0, + global_pooling=False, + use_cudnn=True, + ceil_mode=False, + exclusive=True, + data_format="NCHW", + ): + data_format = data_format.upper() # supprt NHWC, nhwc, etc. + pool_type = pool_type.lower() # supprt max, Max, etc. + if pool_type not in ["max", "avg"]: + raise ValueError( + "Unknown pool_type: '%s'. It can only be 'max' or 'avg'.", + str(pool_type), + ) + + if global_pooling is False and pool_size == -1: + raise ValueError( + "When the global_pooling is False, pool_size must be passed " + "and be a valid value. Received pool_size: " + str(pool_size) + ) + + if not isinstance(use_cudnn, bool): + raise ValueError("use_cudnn should be True or False") + + self._use_mkldnn = _global_flags()["FLAGS_use_mkldnn"] + + if data_format not in ["NCHW", "NHWC"]: + raise ValueError( + "Attr(data_format) should be 'NCHW' or 'NHWC'. Received " + "Attr(data_format): %s." % str(data_format) + ) + + super().__init__() + + self._pool_type = pool_type + self._pool_size = utils.convert_to_list(pool_size, 2, 'pool_size') + self._pool_padding = utils.convert_to_list( + pool_padding, 2, 'pool_padding' + ) + self._pool_stride = utils.convert_to_list(pool_stride, 2, 'pool_stride') + self._global_pooling = global_pooling + self._use_cudnn = use_cudnn + self._ceil_mode = ceil_mode + self._exclusive = exclusive + self._data_format = data_format + self._l_type = 'pool2d' + + def forward(self, input): + if _non_static_mode(): + if not self._use_mkldnn and in_dygraph_mode(): + input = input._use_gpudnn(self._use_cudnn) + return _C_ops.pool2d( + input, + self._pool_size, + self._pool_stride, + self._pool_padding, + self._ceil_mode, + self._exclusive, + self._data_format, + self._pool_type, + self._global_pooling, + False, + "EXPLICIT", + ) + + attrs = ( + 'pooling_type', + self._pool_type, + 'ksize', + self._pool_size, + 'global_pooling', + self._global_pooling, + 'strides', + self._pool_stride, + 'paddings', + self._pool_padding, + 'use_cudnn', + self._use_cudnn, + 'ceil_mode', + self._ceil_mode, + 'use_mkldnn', + self._use_mkldnn, + 'exclusive', + self._exclusive, + 'data_format', + self._data_format, + ) + return _legacy_C_ops.pool2d(input, *attrs) + + check_variable_and_dtype( + input, + 'input', + ['int8', 'uint8', 'float16', 'float32', 'float64'], + 'Pool2D', + ) + + attrs = { + "pooling_type": self._pool_type, + "ksize": self._pool_size, + "global_pooling": self._global_pooling, + "strides": self._pool_stride, + "paddings": self._pool_padding, + "use_cudnn": self._use_cudnn, + "ceil_mode": self._ceil_mode, + "use_mkldnn": self._use_mkldnn, + "exclusive": self._exclusive, + "data_format": self._data_format, + } + inputs = {"X": [input]} + + pool_out = self._helper.create_variable_for_type_inference(self._dtype) + + self._helper.append_op( + type=self._l_type, + inputs={"X": input}, + outputs={"Out": pool_out}, + attrs=attrs, + ) + return pool_out + + class Linear(layers.Layer): """ From 07af54601f46fc954f49193da1d6b891c87bea8f Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Tue, 6 Dec 2022 14:51:17 +0000 Subject: [PATCH 17/19] fix conflict --- python/paddle/fluid/dygraph/nn.py | 206 ------------------------------ 1 file changed, 206 deletions(-) diff --git a/python/paddle/fluid/dygraph/nn.py b/python/paddle/fluid/dygraph/nn.py index fc156de8966ad2..8323469d44f654 100644 --- a/python/paddle/fluid/dygraph/nn.py +++ b/python/paddle/fluid/dygraph/nn.py @@ -508,212 +508,6 @@ def forward(self, input): return self._helper.append_activation(pre_act, act=self._act) -class Pool2D(layers.Layer): - r""" - This interface is used to construct a callable object of the ``Pool2D`` class. - For more details, refer to code examples. - The pooling2d operation calculates the output based on the input, pool_type and pool_size, pool_stride, - pool_padding parameters.Input and output are in NCHW format, where N is batch size, C is the number of feature map, - H is the height of the feature map, and W is the width of the feature map. - Parameters(ksize, strides, paddings) are two elements. These two elements represent height and width, respectively. - The input(X) size and output(Out) size may be different. - Example: - - Input: - Input shape: :math:`(N, C, H_{in}, W_{in})` - - Output: - Output shape: :math:`(N, C, H_{out}, W_{out})` - If ``ceil_mode`` = False: - .. math:: - H_{out} = \\frac{(H_{in} - ksize[0] + 2 * paddings[0])}{strides[0]} + 1 \\\\ - W_{out} = \\frac{(W_{in} - ksize[1] + 2 * paddings[1])}{strides[1]} + 1 - If ``ceil_mode`` = True: - .. math:: - H_{out} = \\frac{(H_{in} - ksize[0] + 2 * paddings[0] + strides[0] - 1)}{strides[0]} + 1 \\\\ - W_{out} = \\frac{(W_{in} - ksize[1] + 2 * paddings[1] + strides[1] - 1)}{strides[1]} + 1 - If ``exclusive`` = False: - .. math:: - hstart &= i * strides[0] - paddings[0] \\\\ - hend &= hstart + ksize[0] \\\\ - wstart &= j * strides[1] - paddings[1] \\\\ - wend &= wstart + ksize[1] \\\\ - Output(i ,j) &= \\frac{sum(Input[hstart:hend, wstart:wend])}{ksize[0] * ksize[1]} - If ``exclusive`` = True: - .. math:: - hstart &= max(0, i * strides[0] - paddings[0])\\\\ - hend &= min(H, hstart + ksize[0]) \\\\ - wstart &= max(0, j * strides[1] - paddings[1]) \\\\ - wend & = min(W, wstart + ksize[1]) \\\\ - Output(i ,j) & = \\frac{sum(Input[hstart:hend, wstart:wend])}{(hend - hstart) * (wend - wstart)} - Parameters: - pool_size (int or list or tuple, optional): The pool kernel size. If pool kernel size is a tuple or list, - it must contain two integers, (pool_size_Height, pool_size_Width). - Otherwise, the pool kernel size will be a square of an int. Default: -1. - pool_type(str, optional) : The pooling type, can be "max" for max-pooling and "avg" for average-pooling. - Default: max. - pool_stride (int or list or tuple, optional): The pool stride size. If pool stride size is a tuple or list, - it must contain two integers, (pool_stride_Height, pool_stride_Width). Otherwise, - the pool stride size will be a square of an int. Default: 1. - pool_padding (int or list or tuple, optional): The padding size for pooling operation. - If ``pool_padding`` is a tuple, - it must contain two integers, (pool_padding_on_Height, pool_padding_on_Width). - Otherwise, the padding size for pooling operation will be a square of an int. Default: 0. - global_pooling (bool, optional): Whether to use the global pooling. If global_pooling = true, - kernel size and paddings will be ignored. Default: False. - use_cudnn (bool, optional): Only used in cudnn kernel, need install cudnn. Default: True. - ceil_mode (bool, optional): 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. - exclusive (bool, optional): Whether to exclude padding points in average pooling mode. Default: True. - data_format (string): The data format of the input and output data. An optional string from: `"NCHW"`, `"NHWC"`. - The default is `"NCHW"`. When it is `"NCHW"`, the data is stored in the order of: - ``[batch_size, input_channels, input_height, input_width]``. When it is `"NHWC"`, the data is - stored in the order of: ``[batch_size, input_height, input_width, input_channels]`` - Returns: - None - Raises: - ValueError: If ``pool_type`` is not "max" nor "avg". - ValueError: If ``global_pooling`` is False and ``pool_size`` is -1. - ValueError: If ``use_cudnn`` is not a bool value. - ValueError: If ``data_format`` is not "NCHW" nor "NHWC". - Examples: - .. code-block:: python - import paddle.fluid as fluid - from paddle.fluid.dygraph.base import to_variable - import numpy as np - with fluid.dygraph.guard(): - data = numpy.random.random((3, 32, 32, 5)).astype('float32') - pool2d = fluid.dygraph.Pool2D(pool_size=2, - pool_type='max', - pool_stride=1, - global_pooling=False) - pool2d_res = pool2d(to_variable(data)) - """ - - def __init__( - self, - pool_size=-1, - pool_type="max", - pool_stride=1, - pool_padding=0, - global_pooling=False, - use_cudnn=True, - ceil_mode=False, - exclusive=True, - data_format="NCHW", - ): - data_format = data_format.upper() # supprt NHWC, nhwc, etc. - pool_type = pool_type.lower() # supprt max, Max, etc. - if pool_type not in ["max", "avg"]: - raise ValueError( - "Unknown pool_type: '%s'. It can only be 'max' or 'avg'.", - str(pool_type), - ) - - if global_pooling is False and pool_size == -1: - raise ValueError( - "When the global_pooling is False, pool_size must be passed " - "and be a valid value. Received pool_size: " + str(pool_size) - ) - - if not isinstance(use_cudnn, bool): - raise ValueError("use_cudnn should be True or False") - - self._use_mkldnn = _global_flags()["FLAGS_use_mkldnn"] - - if data_format not in ["NCHW", "NHWC"]: - raise ValueError( - "Attr(data_format) should be 'NCHW' or 'NHWC'. Received " - "Attr(data_format): %s." % str(data_format) - ) - - super().__init__() - - self._pool_type = pool_type - self._pool_size = utils.convert_to_list(pool_size, 2, 'pool_size') - self._pool_padding = utils.convert_to_list( - pool_padding, 2, 'pool_padding' - ) - self._pool_stride = utils.convert_to_list(pool_stride, 2, 'pool_stride') - self._global_pooling = global_pooling - self._use_cudnn = use_cudnn - self._ceil_mode = ceil_mode - self._exclusive = exclusive - self._data_format = data_format - self._l_type = 'pool2d' - - def forward(self, input): - if _non_static_mode(): - if not self._use_mkldnn and in_dygraph_mode(): - input = input._use_gpudnn(self._use_cudnn) - return _C_ops.pool2d( - input, - self._pool_size, - self._pool_stride, - self._pool_padding, - self._ceil_mode, - self._exclusive, - self._data_format, - self._pool_type, - self._global_pooling, - False, - "EXPLICIT", - ) - - attrs = ( - 'pooling_type', - self._pool_type, - 'ksize', - self._pool_size, - 'global_pooling', - self._global_pooling, - 'strides', - self._pool_stride, - 'paddings', - self._pool_padding, - 'use_cudnn', - self._use_cudnn, - 'ceil_mode', - self._ceil_mode, - 'use_mkldnn', - self._use_mkldnn, - 'exclusive', - self._exclusive, - 'data_format', - self._data_format, - ) - return _legacy_C_ops.pool2d(input, *attrs) - - check_variable_and_dtype( - input, - 'input', - ['int8', 'uint8', 'float16', 'float32', 'float64'], - 'Pool2D', - ) - - attrs = { - "pooling_type": self._pool_type, - "ksize": self._pool_size, - "global_pooling": self._global_pooling, - "strides": self._pool_stride, - "paddings": self._pool_padding, - "use_cudnn": self._use_cudnn, - "ceil_mode": self._ceil_mode, - "use_mkldnn": self._use_mkldnn, - "exclusive": self._exclusive, - "data_format": self._data_format, - } - inputs = {"X": [input]} - - pool_out = self._helper.create_variable_for_type_inference(self._dtype) - - self._helper.append_op( - type=self._l_type, - inputs={"X": input}, - outputs={"Out": pool_out}, - attrs=attrs, - ) - return pool_out - - class Linear(layers.Layer): """ From 3b384651dbfffafb2e1962d9b12ea4e4b6f9ce01 Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Wed, 7 Dec 2022 01:57:38 +0000 Subject: [PATCH 18/19] fix failed tests --- python/paddle/utils/inplace_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/paddle/utils/inplace_utils.py b/python/paddle/utils/inplace_utils.py index f1f0050d41e32e..2ee809facab4fe 100644 --- a/python/paddle/utils/inplace_utils.py +++ b/python/paddle/utils/inplace_utils.py @@ -13,7 +13,8 @@ # limitations under the License. import warnings - +import paddle +from paddle import _C_ops, _legacy_C_ops from paddle.fluid.framework import _non_static_mode from paddle.fluid.wrapped_decorator import wrap_decorator From 0b8e3cc0ea67f3873cca01b56c95a5927a4b7434 Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Wed, 7 Dec 2022 06:48:35 +0000 Subject: [PATCH 19/19] modfiy problem of deleting pool2d --- python/paddle/fluid/dygraph/nn.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/paddle/fluid/dygraph/nn.py b/python/paddle/fluid/dygraph/nn.py index 4dfb67ab4aa429..4390ef6a9b365b 100644 --- a/python/paddle/fluid/dygraph/nn.py +++ b/python/paddle/fluid/dygraph/nn.py @@ -30,6 +30,7 @@ in_dygraph_mode, _in_legacy_dygraph, ) + from ..data_feeder import ( convert_dtype, check_variable_and_dtype,