未验证 提交 ea8e87fa 编写于 作者: N Nyakku Shigure 提交者: GitHub

[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
上级 95079695
...@@ -61,7 +61,6 @@ else: ...@@ -61,7 +61,6 @@ else:
from .framework import VarBase as Tensor # noqa: F401 from .framework import VarBase as Tensor # noqa: F401
Tensor.__qualname__ = 'Tensor' # noqa: F401 Tensor.__qualname__ = 'Tensor' # noqa: F401
import paddle.compat # noqa: F401
import paddle.distributed # noqa: F401 import paddle.distributed # noqa: F401
import paddle.sysconfig # noqa: F401 import paddle.sysconfig # noqa: F401
import paddle.distribution # noqa: F401 import paddle.distribution # noqa: F401
......
# 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)
...@@ -20,7 +20,6 @@ from . import core ...@@ -20,7 +20,6 @@ from . import core
import collections import collections
import copy import copy
import logging import logging
from .. import compat as cpt
from . import unique_name from . import unique_name
from . import log_helper from . import log_helper
import paddle.fluid import paddle.fluid
......
...@@ -44,8 +44,6 @@ try: ...@@ -44,8 +44,6 @@ try:
os.add_dll_directory(third_lib_path) os.add_dll_directory(third_lib_path)
except ImportError as e: except ImportError as e:
from .. import compat as cpt
if os.name == 'nt': if os.name == 'nt':
executable_path = os.path.abspath(os.path.dirname(sys.executable)) executable_path = os.path.abspath(os.path.dirname(sys.executable))
raise ImportError( raise ImportError(
...@@ -71,8 +69,6 @@ def avx_supported(): ...@@ -71,8 +69,6 @@ def avx_supported():
""" """
Whether current system(Linux, MacOS, Windows) is supported with AVX. Whether current system(Linux, MacOS, Windows) is supported with AVX.
""" """
from .. import compat as cpt
sysstr = platform.system().lower() sysstr = platform.system().lower()
has_avx = False has_avx = False
if sysstr == 'linux': if sysstr == 'linux':
......
...@@ -17,7 +17,6 @@ import pickle ...@@ -17,7 +17,6 @@ import pickle
import numpy as np import numpy as np
import paddle import paddle
from paddle import compat as cpt
from paddle.fluid import core from paddle.fluid import core
from paddle.fluid import framework from paddle.fluid import framework
from paddle.fluid import backward from paddle.fluid import backward
......
...@@ -28,7 +28,6 @@ import subprocess ...@@ -28,7 +28,6 @@ import subprocess
import multiprocessing import multiprocessing
import sys import sys
import logging import logging
from .. import compat as cpt
from .proto import framework_pb2 from .proto import framework_pb2
from . import core from . import core
......
...@@ -48,7 +48,6 @@ from ..data_feeder import ( ...@@ -48,7 +48,6 @@ from ..data_feeder import (
check_type, check_type,
check_dtype, check_dtype,
) )
from ... import compat as cpt
from ..backward import _infer_var_data_type_shape_ from ..backward import _infer_var_data_type_shape_
from paddle import _C_ops, _legacy_C_ops from paddle import _C_ops, _legacy_C_ops
......
...@@ -26,7 +26,6 @@ from .loss import softmax_with_cross_entropy ...@@ -26,7 +26,6 @@ from .loss import softmax_with_cross_entropy
from . import tensor from . import tensor
from . import nn from . import nn
from . import ops from . import ops
from ... import compat as cpt
from ..data_feeder import check_variable_and_dtype, check_type, check_dtype from ..data_feeder import check_variable_and_dtype, check_type, check_dtype
import math import math
import numpy as np import numpy as np
......
...@@ -60,7 +60,6 @@ from paddle.fluid.layers import tensor ...@@ -60,7 +60,6 @@ from paddle.fluid.layers import tensor
from functools import reduce from functools import reduce
from functools import cmp_to_key from functools import cmp_to_key
from .wrapped_decorator import signature_safe_contextmanager from .wrapped_decorator import signature_safe_contextmanager
from .. import compat as cpt
import warnings import warnings
from paddle import _C_ops, _legacy_C_ops from paddle import _C_ops, _legacy_C_ops
from ..fluid.framework import ( from ..fluid.framework import (
...@@ -6933,9 +6932,10 @@ class RecomputeOptimizer(Optimizer): ...@@ -6933,9 +6932,10 @@ class RecomputeOptimizer(Optimizer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid as fluid 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): def mlp(input_x, input_y, hid_dim=128, label_dim=2):
fc_1 = fluid.layers.fc(input=input_x, size=hid_dim) fc_1 = fluid.layers.fc(input=input_x, size=hid_dim)
prediction = fluid.layers.fc(input=[fc_1], size=label_dim, act='softmax') prediction = fluid.layers.fc(input=[fc_1], size=label_dim, act='softmax')
...@@ -6955,7 +6955,7 @@ class RecomputeOptimizer(Optimizer): ...@@ -6955,7 +6955,7 @@ class RecomputeOptimizer(Optimizer):
state_dict = {} state_dict = {}
sgd.load(state_dict) sgd.load(state_dict)
except NotImplementedError as e: except NotImplementedError as e:
print(cpt.get_exception_message(e)) print(e)
""" """
raise NotImplementedError( raise NotImplementedError(
"load function is not supported by Recompute Optimizer for now" "load function is not supported by Recompute Optimizer for now"
......
...@@ -89,7 +89,6 @@ def runtime_main(test_class, col_type): ...@@ -89,7 +89,6 @@ def runtime_main(test_class, col_type):
model.run_trainer(args) model.run_trainer(args)
import paddle.compat as cpt
import socket import socket
from contextlib import closing from contextlib import closing
......
...@@ -163,7 +163,6 @@ def runtime_main(test_class): ...@@ -163,7 +163,6 @@ def runtime_main(test_class):
model.run_trainer(args) model.run_trainer(args)
import paddle.compat as cpt
import socket import socket
from contextlib import closing from contextlib import closing
......
...@@ -18,7 +18,6 @@ sys.path.append("..") ...@@ -18,7 +18,6 @@ sys.path.append("..")
import paddle import paddle
import paddle.fluid.core as core import paddle.fluid.core as core
from paddle.static import program_guard, Program from paddle.static import program_guard, Program
import paddle.compat as cpt
import unittest import unittest
import numpy as np import numpy as np
from op_test import OpTest from op_test import OpTest
......
...@@ -434,7 +434,6 @@ def runtime_main(test_class, col_type, sub_type): ...@@ -434,7 +434,6 @@ def runtime_main(test_class, col_type, sub_type):
model.run_trainer(args) model.run_trainer(args)
import paddle.compat as cpt
import socket import socket
from contextlib import closing from contextlib import closing
......
...@@ -138,7 +138,6 @@ def runtime_main(test_class, col_type, sub_type): ...@@ -138,7 +138,6 @@ def runtime_main(test_class, col_type, sub_type):
model.run_trainer(args) model.run_trainer(args)
import paddle.compat as cpt
import socket import socket
from contextlib import closing from contextlib import closing
......
...@@ -451,7 +451,6 @@ def runtime_main(test_class, col_type, sub_type): ...@@ -451,7 +451,6 @@ def runtime_main(test_class, col_type, sub_type):
model.run_trainer(args) model.run_trainer(args)
import paddle.compat as cpt
import socket import socket
from contextlib import closing from contextlib import closing
......
# 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()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册