diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index fc8a9b52bc08f1c691703aaeba0da72667a7dfc1..95e179bb97dc89bbcb08a61ce699be15004c06b7 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 dfa9f1822bff8dd0bf538bd31eb0aa8ec8501372..0000000000000000000000000000000000000000 --- 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 16708268bd5a0b044a6790a7db42747089789382..7823b3f7bd47a97ab53ef3f171b02e0c3ca8e105 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 00f7abee30e21b71dbec894d5819e31e7ad0b0d3..718ddd4bd56310d58362e38a231254f810090795 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 d2e7ada85a7568bceb1d87e389eafebb20813cc3..c5eb445cc610d2fa3d07f239fa3f69737e7d174d 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 46bd46d706ae2451b3fbd48ca2bb68a09f560d0e..2cceeabfc4c23c9d1e0578423782e2c42b279096 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 1046c27fef54dfdfd634ce5050b1668326364b1f..18b2ec496499d1e2decdd0fc0b03bd771a9c5085 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 0711bddb45dd58ec802257e9e41a5a61ce77792d..097875a75f2e8a831fb8d90443c8652eb4eabd72 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 ce58c0cfe02fbce2bbf08b8083603de6a8ddd1eb..f06c380838f151d37a2e383e42eb0ecf0d8cce25 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 26e7c2972b3311acc6cdc555f44f3a637e408327..5adcf7a29875af054fad9e7b20633de9462cfae7 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 ab089aa26b7741f8869712d03a5ccd6002027db7..3e005bafb4131bc073daea78f9539a82215b9629 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 79b5d7abe7d82e28b6d026a7e5abc10512b066f5..927c0627e2f397edc8ca021f8b7edf7d3ed92e49 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 dd842800198cb42487ff7e943567c533466f23e0..cb7a890adb22271d993c05323c45ae9bd407e1e7 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 6b7e056a7f86a6832be2899dd4e29abbf64f2fd2..55bc9dce1863ef15ffd1bd58939e38393270c8ad 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 1629c8e8fb20a8d70190482f0598c12719115dd1..101a749f424913d3e57a138b6b308cb8383319e4 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 d4ad8a8274e3a8b36f4eb195fae2bade5716b280..0000000000000000000000000000000000000000 --- 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()