From ea8e87fa7c3818c1f6e871c8476710768c4c1127 Mon Sep 17 00:00:00 2001 From: Nyakku Shigure Date: Tue, 25 Oct 2022 15:19:50 +0800 Subject: [PATCH] [CodeStyle][py2] remove `paddle.compat` (#47269) * [CodeStyle][py2] remove `paddle.compat` * remove compat from `paddle.__init__` * enable_static in sample code * Revert "enable_static in sample code" This reverts commit ffccaa633900154ea5f3d056e746aae9a1927399. * enable_static in sample code --- python/paddle/__init__.py | 1 - python/paddle/compat.py | 198 --------------- python/paddle/fluid/backward.py | 1 - python/paddle/fluid/core.py | 4 - python/paddle/fluid/dygraph/io.py | 1 - python/paddle/fluid/framework.py | 1 - python/paddle/fluid/layers/control_flow.py | 1 - python/paddle/fluid/layers/detection.py | 1 - python/paddle/fluid/optimizer.py | 6 +- .../mlu/test_collective_api_base_mlu.py | 1 - .../unittests/mlu/test_collective_base_mlu.py | 1 - ...st_fill_constant_batch_size_like_op_mlu.py | 1 - .../mlu/test_sync_batch_norm_base_mlu.py | 1 - .../unittests/npu/test_collective_base_npu.py | 1 - .../npu/test_sync_batch_norm_base_npu.py | 1 - .../fluid/tests/unittests/test_compat.py | 240 ------------------ 16 files changed, 3 insertions(+), 457 deletions(-) delete mode 100644 python/paddle/compat.py delete mode 100644 python/paddle/fluid/tests/unittests/test_compat.py diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index fc8a9b52bc..95e179bb97 100755 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -61,7 +61,6 @@ else: from .framework import VarBase as Tensor # noqa: F401 Tensor.__qualname__ = 'Tensor' # noqa: F401 -import paddle.compat # noqa: F401 import paddle.distributed # noqa: F401 import paddle.sysconfig # noqa: F401 import paddle.distribution # noqa: F401 diff --git a/python/paddle/compat.py b/python/paddle/compat.py deleted file mode 100644 index dfa9f1822b..0000000000 --- a/python/paddle/compat.py +++ /dev/null @@ -1,198 +0,0 @@ -# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import six - -__all__ = [] - - -# str and bytes related functions -def to_text(obj, encoding='utf-8', inplace=False): - """ - All string in PaddlePaddle should be represented as a literal string. - - This function will convert object to a literal string without any encoding. - Especially, if the object type is a list or set container, we will iterate - all items in the object and convert them to literal string. - - In Python3: - Decode the bytes type object to str type with specific encoding - - In Python2: - Decode the str type object to unicode type with specific encoding - - Args: - obj(unicode|str|bytes|list|set) : The object to be decoded. - encoding(str) : The encoding format to decode a string - inplace(bool) : If we change the original object or we create a new one - - Returns: - Decoded result of obj - - Examples: - - .. code-block:: python - - import paddle - - data = "paddlepaddle" - data = paddle.compat.to_text(data) - # paddlepaddle - - """ - if obj is None: - return obj - - if isinstance(obj, list): - if inplace: - for i in six.moves.xrange(len(obj)): - obj[i] = _to_text(obj[i], encoding) - return obj - else: - return [_to_text(item, encoding) for item in obj] - elif isinstance(obj, set): - if inplace: - for item in obj: - obj.remove(item) - obj.add(_to_text(item, encoding)) - return obj - else: - return set([_to_text(item, encoding) for item in obj]) - elif isinstance(obj, dict): - if inplace: - new_obj = {} - for key, value in six.iteritems(obj): - new_obj[_to_text(key, encoding)] = _to_text(value, encoding) - obj.update(new_obj) - return obj - else: - new_obj = {} - for key, value in six.iteritems(obj): - new_obj[_to_text(key, encoding)] = _to_text(value, encoding) - return new_obj - else: - return _to_text(obj, encoding) - - -def _to_text(obj, encoding): - """ - In Python3: - Decode the bytes type object to str type with specific encoding - - In Python2: - Decode the str type object to unicode type with specific encoding, - or we just return the unicode string of object - - Args: - obj(unicode|str|bytes) : The object to be decoded. - encoding(str) : The encoding format - - Returns: - decoded result of obj - """ - if obj is None: - return obj - - if isinstance(obj, six.binary_type): - return obj.decode(encoding) - elif isinstance(obj, six.text_type): - return obj - elif isinstance(obj, (bool, float)): - return obj - else: - return six.u(obj) - - -def to_bytes(obj, encoding='utf-8', inplace=False): - """ - All string in PaddlePaddle should be represented as a literal string. - - This function will convert object to a bytes with specific encoding. - Especially, if the object type is a list or set container, we will iterate - all items in the object and convert them to bytes. - - In Python3: - Encode the str type object to bytes type with specific encoding - - In Python2: - Encode the unicode type object to str type with specific encoding, - or we just return the 8-bit string of object - - Args: - obj(unicode|str|bytes|list|set) : The object to be encoded. - encoding(str) : The encoding format to encode a string - inplace(bool) : If we change the original object or we create a new one - - Returns: - Decoded result of obj - - Examples: - - .. code-block:: python - - import paddle - - data = "paddlepaddle" - data = paddle.compat.to_bytes(data) - # b'paddlepaddle' - - """ - if obj is None: - return obj - - if isinstance(obj, list): - if inplace: - for i in six.moves.xrange(len(obj)): - obj[i] = _to_bytes(obj[i], encoding) - return obj - else: - return [_to_bytes(item, encoding) for item in obj] - elif isinstance(obj, set): - if inplace: - for item in obj: - obj.remove(item) - obj.add(_to_bytes(item, encoding)) - return obj - else: - return set([_to_bytes(item, encoding) for item in obj]) - else: - return _to_bytes(obj, encoding) - - -def _to_bytes(obj, encoding): - """ - In Python3: - Encode the str type object to bytes type with specific encoding - - In Python2: - Encode the unicode type object to str type with specific encoding, - or we just return the 8-bit string of object - - Args: - obj(unicode|str|bytes) : The object to be encoded. - encoding(str) : The encoding format - - Returns: - encoded result of obj - """ - if obj is None: - return obj - - assert encoding is not None - if isinstance(obj, six.text_type): - return obj.encode(encoding) - elif isinstance(obj, six.binary_type): - return obj - else: - return six.b(obj) diff --git a/python/paddle/fluid/backward.py b/python/paddle/fluid/backward.py index 16708268bd..7823b3f7bd 100755 --- a/python/paddle/fluid/backward.py +++ b/python/paddle/fluid/backward.py @@ -20,7 +20,6 @@ from . import core import collections import copy import logging -from .. import compat as cpt from . import unique_name from . import log_helper import paddle.fluid diff --git a/python/paddle/fluid/core.py b/python/paddle/fluid/core.py index 00f7abee30..718ddd4bd5 100644 --- a/python/paddle/fluid/core.py +++ b/python/paddle/fluid/core.py @@ -44,8 +44,6 @@ try: os.add_dll_directory(third_lib_path) except ImportError as e: - from .. import compat as cpt - if os.name == 'nt': executable_path = os.path.abspath(os.path.dirname(sys.executable)) raise ImportError( @@ -71,8 +69,6 @@ def avx_supported(): """ Whether current system(Linux, MacOS, Windows) is supported with AVX. """ - from .. import compat as cpt - sysstr = platform.system().lower() has_avx = False if sysstr == 'linux': diff --git a/python/paddle/fluid/dygraph/io.py b/python/paddle/fluid/dygraph/io.py index d2e7ada85a..c5eb445cc6 100644 --- a/python/paddle/fluid/dygraph/io.py +++ b/python/paddle/fluid/dygraph/io.py @@ -17,7 +17,6 @@ import pickle import numpy as np import paddle -from paddle import compat as cpt from paddle.fluid import core from paddle.fluid import framework from paddle.fluid import backward diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 46bd46d706..2cceeabfc4 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -28,7 +28,6 @@ import subprocess import multiprocessing import sys import logging -from .. import compat as cpt from .proto import framework_pb2 from . import core diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py index 1046c27fef..18b2ec4964 100755 --- a/python/paddle/fluid/layers/control_flow.py +++ b/python/paddle/fluid/layers/control_flow.py @@ -48,7 +48,6 @@ from ..data_feeder import ( check_type, check_dtype, ) -from ... import compat as cpt from ..backward import _infer_var_data_type_shape_ from paddle import _C_ops, _legacy_C_ops diff --git a/python/paddle/fluid/layers/detection.py b/python/paddle/fluid/layers/detection.py index 0711bddb45..097875a75f 100644 --- a/python/paddle/fluid/layers/detection.py +++ b/python/paddle/fluid/layers/detection.py @@ -26,7 +26,6 @@ from .loss import softmax_with_cross_entropy from . import tensor from . import nn from . import ops -from ... import compat as cpt from ..data_feeder import check_variable_and_dtype, check_type, check_dtype import math import numpy as np diff --git a/python/paddle/fluid/optimizer.py b/python/paddle/fluid/optimizer.py index ce58c0cfe0..f06c380838 100755 --- a/python/paddle/fluid/optimizer.py +++ b/python/paddle/fluid/optimizer.py @@ -60,7 +60,6 @@ from paddle.fluid.layers import tensor from functools import reduce from functools import cmp_to_key from .wrapped_decorator import signature_safe_contextmanager -from .. import compat as cpt import warnings from paddle import _C_ops, _legacy_C_ops from ..fluid.framework import ( @@ -6933,9 +6932,10 @@ class RecomputeOptimizer(Optimizer): Examples: .. code-block:: python + import paddle import paddle.fluid as fluid - import paddle.compat as cpt + paddle.enable_static() def mlp(input_x, input_y, hid_dim=128, label_dim=2): fc_1 = fluid.layers.fc(input=input_x, size=hid_dim) prediction = fluid.layers.fc(input=[fc_1], size=label_dim, act='softmax') @@ -6955,7 +6955,7 @@ class RecomputeOptimizer(Optimizer): state_dict = {} sgd.load(state_dict) except NotImplementedError as e: - print(cpt.get_exception_message(e)) + print(e) """ raise NotImplementedError( "load function is not supported by Recompute Optimizer for now" diff --git a/python/paddle/fluid/tests/unittests/mlu/test_collective_api_base_mlu.py b/python/paddle/fluid/tests/unittests/mlu/test_collective_api_base_mlu.py index 26e7c2972b..5adcf7a298 100644 --- a/python/paddle/fluid/tests/unittests/mlu/test_collective_api_base_mlu.py +++ b/python/paddle/fluid/tests/unittests/mlu/test_collective_api_base_mlu.py @@ -89,7 +89,6 @@ def runtime_main(test_class, col_type): model.run_trainer(args) -import paddle.compat as cpt import socket from contextlib import closing diff --git a/python/paddle/fluid/tests/unittests/mlu/test_collective_base_mlu.py b/python/paddle/fluid/tests/unittests/mlu/test_collective_base_mlu.py index ab089aa26b..3e005bafb4 100644 --- a/python/paddle/fluid/tests/unittests/mlu/test_collective_base_mlu.py +++ b/python/paddle/fluid/tests/unittests/mlu/test_collective_base_mlu.py @@ -163,7 +163,6 @@ def runtime_main(test_class): model.run_trainer(args) -import paddle.compat as cpt import socket from contextlib import closing diff --git a/python/paddle/fluid/tests/unittests/mlu/test_fill_constant_batch_size_like_op_mlu.py b/python/paddle/fluid/tests/unittests/mlu/test_fill_constant_batch_size_like_op_mlu.py index 79b5d7abe7..927c0627e2 100644 --- a/python/paddle/fluid/tests/unittests/mlu/test_fill_constant_batch_size_like_op_mlu.py +++ b/python/paddle/fluid/tests/unittests/mlu/test_fill_constant_batch_size_like_op_mlu.py @@ -18,7 +18,6 @@ sys.path.append("..") import paddle import paddle.fluid.core as core from paddle.static import program_guard, Program -import paddle.compat as cpt import unittest import numpy as np from op_test import OpTest diff --git a/python/paddle/fluid/tests/unittests/mlu/test_sync_batch_norm_base_mlu.py b/python/paddle/fluid/tests/unittests/mlu/test_sync_batch_norm_base_mlu.py index dd84280019..cb7a890adb 100644 --- a/python/paddle/fluid/tests/unittests/mlu/test_sync_batch_norm_base_mlu.py +++ b/python/paddle/fluid/tests/unittests/mlu/test_sync_batch_norm_base_mlu.py @@ -434,7 +434,6 @@ def runtime_main(test_class, col_type, sub_type): model.run_trainer(args) -import paddle.compat as cpt import socket from contextlib import closing diff --git a/python/paddle/fluid/tests/unittests/npu/test_collective_base_npu.py b/python/paddle/fluid/tests/unittests/npu/test_collective_base_npu.py index 6b7e056a7f..55bc9dce18 100644 --- a/python/paddle/fluid/tests/unittests/npu/test_collective_base_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_collective_base_npu.py @@ -138,7 +138,6 @@ def runtime_main(test_class, col_type, sub_type): model.run_trainer(args) -import paddle.compat as cpt import socket from contextlib import closing diff --git a/python/paddle/fluid/tests/unittests/npu/test_sync_batch_norm_base_npu.py b/python/paddle/fluid/tests/unittests/npu/test_sync_batch_norm_base_npu.py index 1629c8e8fb..101a749f42 100644 --- a/python/paddle/fluid/tests/unittests/npu/test_sync_batch_norm_base_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_sync_batch_norm_base_npu.py @@ -451,7 +451,6 @@ def runtime_main(test_class, col_type, sub_type): model.run_trainer(args) -import paddle.compat as cpt import socket from contextlib import closing diff --git a/python/paddle/fluid/tests/unittests/test_compat.py b/python/paddle/fluid/tests/unittests/test_compat.py deleted file mode 100644 index d4ad8a8274..0000000000 --- a/python/paddle/fluid/tests/unittests/test_compat.py +++ /dev/null @@ -1,240 +0,0 @@ -# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest -import paddle.compat as cpt - - -class TestCompatible(unittest.TestCase): - def test_to_text(self): - self.assertIsNone(cpt.to_text(None)) - - self.assertTrue(isinstance(cpt.to_text(str("")), str)) - self.assertTrue(isinstance(cpt.to_text(str("123")), str)) - self.assertTrue(isinstance(cpt.to_text(b""), str)) - self.assertTrue(isinstance(cpt.to_text(b""), str)) - self.assertTrue(isinstance(cpt.to_text(u""), str)) - self.assertTrue(isinstance(cpt.to_text(u""), str)) - - self.assertEqual("", cpt.to_text(str(""))) - self.assertEqual("123", cpt.to_text(str("123"))) - self.assertEqual("", cpt.to_text(b"")) - self.assertEqual("123", cpt.to_text(b"123")) - self.assertEqual("", cpt.to_text(u"")) - self.assertEqual("123", cpt.to_text(u"123")) - - # check list types, not inplace - l = [""] - l2 = cpt.to_text(l) - self.assertTrue(isinstance(l2, list)) - self.assertFalse(l is l2) - self.assertEqual(l, l2) - self.assertEqual([""], l2) - l = ["", "123"] - l2 = cpt.to_text(l) - self.assertTrue(isinstance(l2, list)) - self.assertFalse(l is l2) - self.assertEqual(l, l2) - self.assertEqual(["", "123"], l2) - l = ["", b"123", u"321"] - l2 = cpt.to_text(l) - self.assertTrue(isinstance(l2, list)) - self.assertFalse(l is l2) - self.assertNotEqual(l, l2) - self.assertEqual(["", "123", "321"], l2) - - # check list types, inplace - l = [""] - l2 = cpt.to_text(l, inplace=True) - self.assertTrue(isinstance(l2, list)) - self.assertTrue(l is l2) - self.assertEqual(l, l2) - self.assertEqual([""], l2) - l = ["", b"123"] - l2 = cpt.to_text(l, inplace=True) - self.assertTrue(isinstance(l2, list)) - self.assertTrue(l is l2) - self.assertEqual(l, l2) - self.assertEqual(["", "123"], l2) - l = ["", b"123", u"321"] - l2 = cpt.to_text(l, inplace=True) - self.assertTrue(isinstance(l2, list)) - self.assertTrue(l is l2) - self.assertEqual(l, l2) - self.assertEqual(["", "123", "321"], l2) - for i in l2: - self.assertTrue(isinstance(i, str)) - - # check set types, not inplace - l = set("") - l2 = cpt.to_text(l, inplace=False) - self.assertTrue(isinstance(l2, set)) - self.assertFalse(l is l2) - self.assertEqual(l, l2) - self.assertEqual(set(""), l2) - l = set([b"", b"123"]) - l2 = cpt.to_text(l, inplace=False) - self.assertTrue(isinstance(l2, set)) - self.assertFalse(l is l2) - self.assertNotEqual(l, l2) - self.assertEqual(set(["", "123"]), l2) - l = set(["", b"123", u"321"]) - l2 = cpt.to_text(l, inplace=False) - self.assertTrue(isinstance(l2, set)) - self.assertFalse(l is l2) - self.assertNotEqual(l, l2) - self.assertEqual(set(["", "123", "321"]), l2) - - # check set types, inplace - l = set("") - l2 = cpt.to_text(l, inplace=True) - self.assertTrue(isinstance(l2, set)) - self.assertTrue(l is l2) - self.assertEqual(l, l2) - self.assertEqual(set(""), l2) - l = set([b"", b"123"]) - l2 = cpt.to_text(l, inplace=True) - self.assertTrue(isinstance(l2, set)) - self.assertTrue(l is l2) - self.assertEqual(l, l2) - self.assertEqual(set(["", "123"]), l2) - l = set(["", b"123", u"321"]) - l2 = cpt.to_text(l, inplace=True) - self.assertTrue(isinstance(l2, set)) - self.assertTrue(l is l2) - self.assertEqual(l, l2) - self.assertEqual(set(["", "123", "321"]), l2) - for i in l2: - self.assertTrue(isinstance(i, str)) - - # check dict types, not inplace - l = {"": ""} - l2 = cpt.to_text(l, inplace=False) - self.assertTrue(isinstance(l2, dict)) - self.assertFalse(l is l2) - self.assertEqual(l, l2) - self.assertEqual({"": ""}, l2) - - # check dict types, inplace - l = {"": ""} - l2 = cpt.to_text(l, inplace=True) - self.assertTrue(isinstance(l2, dict)) - self.assertTrue(l is l2) - self.assertEqual(l, l2) - self.assertEqual({"": ""}, l2) - - def test_to_bytes(self): - self.assertIsNone(cpt.to_bytes(None)) - - self.assertTrue(isinstance(cpt.to_bytes(str("")), bytes)) - self.assertTrue(isinstance(cpt.to_bytes(str("123")), bytes)) - self.assertTrue(isinstance(cpt.to_bytes(b""), bytes)) - self.assertTrue(isinstance(cpt.to_bytes(b""), bytes)) - self.assertTrue(isinstance(cpt.to_bytes(u""), bytes)) - self.assertTrue(isinstance(cpt.to_bytes(u""), bytes)) - - self.assertEqual(b"", cpt.to_bytes(str(""))) - self.assertEqual(b"123", cpt.to_bytes(str("123"))) - self.assertEqual(b"", cpt.to_bytes(b"")) - self.assertEqual(b"123", cpt.to_bytes(b"123")) - self.assertEqual(b"", cpt.to_bytes(u"")) - self.assertEqual(b"123", cpt.to_bytes(u"123")) - - # check list types, not inplace - l = [""] - l2 = cpt.to_bytes(l) - self.assertTrue(isinstance(l2, list)) - self.assertFalse(l is l2) - self.assertNotEqual(l, l2) - self.assertEqual([b""], l2) - l = ["", "123"] - l2 = cpt.to_bytes(l) - self.assertTrue(isinstance(l2, list)) - self.assertFalse(l is l2) - self.assertNotEqual(l, l2) - self.assertEqual([b"", b"123"], l2) - l = ["", b"123", u"321"] - l2 = cpt.to_bytes(l) - self.assertTrue(isinstance(l2, list)) - self.assertFalse(l is l2) - self.assertNotEqual(l, l2) - self.assertEqual([b"", b"123", b"321"], l2) - - # check list types, inplace - l = [""] - l2 = cpt.to_bytes(l, inplace=True) - self.assertTrue(isinstance(l2, list)) - self.assertTrue(l is l2) - self.assertEqual(l, l2) - self.assertEqual([b""], l2) - l = ["", b"123"] - l2 = cpt.to_bytes(l, inplace=True) - self.assertTrue(isinstance(l2, list)) - self.assertTrue(l is l2) - self.assertEqual(l, l2) - self.assertEqual([b"", b"123"], l2) - l = ["", b"123", u"321"] - l2 = cpt.to_bytes(l, inplace=True) - self.assertTrue(isinstance(l2, list)) - self.assertTrue(l is l2) - self.assertEqual(l, l2) - self.assertEqual([b"", b"123", b"321"], l2) - for i in l2: - self.assertTrue(isinstance(i, bytes)) - - # check set types, not inplace - l = set([""]) - l2 = cpt.to_bytes(l, inplace=False) - self.assertTrue(isinstance(l2, set)) - self.assertFalse(l is l2) - self.assertNotEqual(l, l2) - self.assertEqual(set([b""]), l2) - l = set([u"", u"123"]) - l2 = cpt.to_bytes(l, inplace=False) - self.assertTrue(isinstance(l2, set)) - self.assertFalse(l is l2) - self.assertNotEqual(l, l2) - self.assertEqual(set([b"", b"123"]), l2) - l = set(["", b"123", u"321"]) - l2 = cpt.to_bytes(l, inplace=False) - self.assertTrue(isinstance(l2, set)) - self.assertFalse(l is l2) - self.assertNotEqual(l, l2) - self.assertEqual(set([b"", b"123", b"321"]), l2) - - # check set types, inplace - l = set("") - l2 = cpt.to_bytes(l, inplace=True) - self.assertTrue(isinstance(l2, set)) - self.assertTrue(l is l2) - self.assertEqual(l, l2) - self.assertEqual(set(b""), l2) - l = set([u"", u"123"]) - l2 = cpt.to_bytes(l, inplace=True) - self.assertTrue(isinstance(l2, set)) - self.assertTrue(l is l2) - self.assertEqual(l, l2) - self.assertEqual(set([b"", b"123"]), l2) - l = set(["", b"123", u"321"]) - l2 = cpt.to_bytes(l, inplace=True) - self.assertTrue(isinstance(l2, set)) - self.assertTrue(l is l2) - self.assertEqual(l, l2) - self.assertEqual(set([b"", b"123", b"321"]), l2) - for i in l2: - self.assertTrue(isinstance(i, bytes)) - - -if __name__ == "__main__": - unittest.main() -- GitLab