Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions python/paddle/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@

__all__ = []

if six.PY2:
int_type = int
long_type = long # noqa: F821
else:
int_type = int
long_type = int
int_type = int
long_type = int


# str and bytes related functions
Expand Down Expand Up @@ -262,7 +258,4 @@ def get_exception_message(exc):
"""
assert exc is not None

if six.PY2:
return exc.message
else:
return str(exc)
return str(exc)
6 changes: 1 addition & 5 deletions python/paddle/dataset/cifar.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ def reader():
if sub_name in each_item.name)

for name in names:
if six.PY2:
batch = pickle.load(f.extractfile(name))
else:
batch = pickle.load(
f.extractfile(name), encoding='bytes')
batch = pickle.load(f.extractfile(name), encoding='bytes')
for item in read_batch(batch):
yield item

Expand Down
2 changes: 0 additions & 2 deletions python/paddle/dataset/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ def download(url, module_name, md5sum, save_name=None):
bar = paddle.hapi.progressbar.ProgressBar(
total_iter, name='item')
for data in r.iter_content(chunk_size=chunk_size):
if six.PY2:
data = six.b(data)
f.write(data)
log_index += 1
bar.update(log_index, {})
Expand Down
5 changes: 1 addition & 4 deletions python/paddle/dataset/flowers.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ def reader():
file = file.strip()
batch = None
with open(file, 'rb') as f:
if six.PY2:
batch = pickle.load(f)
else:
batch = pickle.load(f, encoding='bytes')
batch = pickle.load(f, encoding='bytes')

if six.PY3:
batch = cpt.to_text(batch)
Expand Down
8 changes: 2 additions & 6 deletions python/paddle/distributed/fleet/utils/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@

import six
# NOTE: HTTPServer has a different name in python2 and python3
if six.PY2:
from BaseHTTPServer import HTTPServer
import SimpleHTTPServer
else:
from http.server import HTTPServer
import http.server as SimpleHTTPServer
from http.server import HTTPServer
import http.server as SimpleHTTPServer

import time
import threading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,7 @@ def _predict(self,
if iters == skip_batch_num:
total_samples = 0
infer_start_time = time.time()
if six.PY2:
images = map(lambda x: x[0].reshape(dshape), data)
if six.PY3:
images = list(map(lambda x: x[0].reshape(dshape), data))
images = list(map(lambda x: x[0].reshape(dshape), data))
images = np.array(images).astype('float32')
labels = np.array([x[1] for x in data]).astype('int64')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,7 @@ def _predict(self,
if iters == skip_batch_num:
total_samples = 0
infer_start_time = time.time()
if six.PY2:
images = map(lambda x: x[0].reshape(dshape), data)
if six.PY3:
images = list(map(lambda x: x[0].reshape(dshape), data))
images = list(map(lambda x: x[0].reshape(dshape), data))
images = np.array(images).astype('float32')
labels = np.array([x[1] for x in data]).astype('int64')

Expand Down
5 changes: 1 addition & 4 deletions python/paddle/fluid/dataloader/dataloader_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
from paddle.fluid.framework import _set_expected_place, _current_expected_place

# NOTE: queue has a different name in python2 and python3
if six.PY2:
import Queue as queue
else:
import queue
import queue

import paddle
from .. import core, layers
Expand Down
5 changes: 1 addition & 4 deletions python/paddle/fluid/dataloader/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
from .flat import _flatten_batch

# NOTE: queue has a different name in python2 and python3
if six.PY2:
import Queue as queue
else:
import queue
import queue

__all__ = ['get_worker_info']

Expand Down
7 changes: 2 additions & 5 deletions python/paddle/fluid/dygraph/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import functools
from ..framework import Variable, default_main_program, in_dygraph_mode, dygraph_only, Parameter, ParamBase, _varbase_creator, _dygraph_tracer
import pickle
import six
from . import learning_rate_scheduler
import warnings
from .. import core
Expand Down Expand Up @@ -194,16 +193,14 @@ def load_dygraph(model_path, **configs):
para_dict = {}
if os.path.exists(params_file_path):
with open(params_file_path, 'rb') as f:
para_dict = pickle.load(f) if six.PY2 else pickle.load(
f, encoding='latin1')
para_dict = pickle.load(f, encoding='latin1')

if not config.keep_name_table and "StructuredToParameterName@@" in para_dict:
del para_dict["StructuredToParameterName@@"]

if os.path.exists(opti_file_path):
with open(opti_file_path, 'rb') as f:
opti_dict = pickle.load(f) if six.PY2 else pickle.load(
f, encoding='latin1')
opti_dict = pickle.load(f, encoding='latin1')
else:
# check model path
if not os.path.isdir(model_prefix):
Expand Down
18 changes: 4 additions & 14 deletions python/paddle/fluid/dygraph/dygraph_to_static/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ def visit(self, node):


# imp is deprecated in python3
if six.PY2:
import imp
else:
from importlib.machinery import SourceFileLoader
from importlib.machinery import SourceFileLoader

dygraph_class_to_static_api = {
"CosineDecay": "cosine_decay",
Expand Down Expand Up @@ -490,12 +487,8 @@ def remove_if_exit(filepath):
import_fluid = "import paddle\nimport paddle.fluid as fluid\n"
source = import_fluid + source

if six.PY2:
source = source.encode('utf-8')
f = tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False)
else:
f = tempfile.NamedTemporaryFile(
mode='w', suffix='.py', delete=False, encoding='utf-8')
f = tempfile.NamedTemporaryFile(
mode='w', suffix='.py', delete=False, encoding='utf-8')
with f:
module_name = os.path.basename(f.name[:-3])
f.write(source)
Expand All @@ -504,10 +497,7 @@ def remove_if_exit(filepath):
atexit.register(lambda: remove_if_exit(f.name))
atexit.register(lambda: remove_if_exit(f.name[:-3] + ".pyc"))

if six.PY2:
module = imp.load_source(module_name, f.name)
else:
module = SourceFileLoader(module_name, f.name).load_module()
module = SourceFileLoader(module_name, f.name).load_module()
func_name = dyfunc.__name__
# The 'forward' or 'another_forward' of 'TranslatedLayer' cannot be obtained
# through 'func_name'. So set the special function name '__i_m_p_l__'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,9 @@ def create_fill_constant_node(name, value):
func_code += "dtype='float64', value={})".format(value)
return gast.parse(func_code).body[0]

if six.PY2:
if isinstance(value, int):
func_code += "dtype='int32', value={})".format(value)
return gast.parse(func_code).body[0]
if isinstance(value, long):
func_code += "dtype='int64', value={})".format(value)
return gast.parse(func_code).body[0]
else:
if isinstance(value, int):
func_code += "dtype='int64', value={})".format(value)
return gast.parse(func_code).body[0]
if isinstance(value, int):
func_code += "dtype='int64', value={})".format(value)
return gast.parse(func_code).body[0]


def to_static_variable(x):
Expand Down
11 changes: 2 additions & 9 deletions python/paddle/fluid/dygraph/math_op_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from . import no_grad

import numpy as np
import six
import warnings

_supported_int_dtype_ = [
Expand Down Expand Up @@ -121,10 +120,7 @@ def _long_(var):
assert numel == 1, "only one element variable can be converted to long."
tensor = var.value().get_tensor()
assert tensor._is_initialized(), "variable's tensor is not initialized"
if six.PY2:
return long(var.numpy().flatten()[0])
else:
return int(var.numpy().flatten()[0])
return int(var.numpy().flatten()[0])

def _int_(var):
numel = np.prod(var.shape)
Expand All @@ -141,10 +137,7 @@ def _index_(var):
assert numel == 1, "only one element variable can be converted to python index."
tensor = var.value().get_tensor()
assert tensor._is_initialized(), "variable's tensor is not initialized"
if six.PY2:
return long(var.numpy().flatten()[0])
else:
return int(var.numpy().flatten()[0])
return int(var.numpy().flatten()[0])

@property
def _ndim_(var):
Expand Down
15 changes: 5 additions & 10 deletions python/paddle/fluid/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1940,8 +1940,7 @@ def _pickle_loads_mac(path, f):
max_bytes = 2**30
for _ in range(0, file_size, max_bytes):
pickle_bytes += f.read(max_bytes)
load_result = pickle.loads(pickle_bytes) if six.PY2 else pickle.loads(
pickle_bytes, encoding='latin1')
load_result = pickle.loads(pickle_bytes, encoding='latin1')
return load_result


Expand Down Expand Up @@ -2113,8 +2112,7 @@ def set_var(var, ndarray):
if sys.platform == 'darwin' and sys.version_info.major == 3:
load_dict = _pickle_loads_mac(parameter_file_name, f)
else:
load_dict = pickle.load(f) if six.PY2 else pickle.load(
f, encoding='latin1')
load_dict = pickle.load(f, encoding='latin1')
load_dict = _pack_loaded_dict(load_dict)
for v in parameter_list:
assert v.name in load_dict, \
Expand All @@ -2135,8 +2133,7 @@ def set_var(var, ndarray):
optimizer_var_list, global_scope(), executor._default_executor)

with open(opt_file_name, 'rb') as f:
load_dict = pickle.load(f) if six.PY2 else pickle.load(
f, encoding='latin1')
load_dict = pickle.load(f, encoding='latin1')
for v in optimizer_var_list:
assert v.name in load_dict, \
"Can not find [{}] in model file [{}]".format(
Expand Down Expand Up @@ -2297,15 +2294,13 @@ def _load_vars_with_try_catch(exe,
if sys.platform == 'darwin' and sys.version_info.major == 3:
para_dict = _pickle_loads_mac(parameter_file_name, f)
else:
para_dict = pickle.load(f) if six.PY2 else pickle.load(
f, encoding='latin1')
para_dict = pickle.load(f, encoding='latin1')
para_dict = _pack_loaded_dict(para_dict)

opt_file_name = model_prefix + ".pdopt"
if os.path.exists(opt_file_name):
with open(opt_file_name, 'rb') as f:
opti_dict = pickle.load(f) if six.PY2 else pickle.load(
f, encoding='latin1')
opti_dict = pickle.load(f, encoding='latin1')

para_dict.update(opti_dict)

Expand Down
24 changes: 6 additions & 18 deletions python/paddle/fluid/layers/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

import math
import numpy
import six
import warnings
from six.moves import reduce

from ..layer_helper import LayerHelper
from ..param_attr import ParamAttr
Expand Down Expand Up @@ -134,14 +132,9 @@ def create_parameter(shape,
"""
check_type(shape, 'shape', (list, tuple, numpy.ndarray), 'create_parameter')
for item in shape:
if six.PY2:
check_type(item, 'item of shape',
(int, long, numpy.uint8, numpy.int8, numpy.int16,
numpy.int32, numpy.int64), 'create_parameter')
else:
check_type(item, 'item of shape',
(int, numpy.uint8, numpy.int8, numpy.int16, numpy.int32,
numpy.int64), 'create_parameter')
check_type(item, 'item of shape',
(int, numpy.uint8, numpy.int8, numpy.int16, numpy.int32,
numpy.int64), 'create_parameter')

check_dtype(dtype, 'dtype', [
'bool', 'float16', 'float32', 'float64', 'int8', 'int16', 'int32',
Expand Down Expand Up @@ -194,14 +187,9 @@ def create_global_var(shape,
check_type(shape, 'shape', (list, tuple, numpy.ndarray),
'create_global_var')
for item in shape:
if six.PY2:
check_type(item, 'item of shape',
(int, long, numpy.uint8, numpy.int8, numpy.int16,
numpy.int32, numpy.int64), 'create_global_var')
else:
check_type(item, 'item of shape',
(int, numpy.uint8, numpy.int8, numpy.int16, numpy.int32,
numpy.int64), 'create_global_var')
check_type(item, 'item of shape',
(int, numpy.uint8, numpy.int8, numpy.int16, numpy.int32,
numpy.int64), 'create_global_var')

check_dtype(dtype, 'dtype', [
'bool', 'float16', 'float32', 'float64', 'int8', 'int16', 'int32',
Expand Down
6 changes: 1 addition & 5 deletions python/paddle/fluid/multiprocess_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import six
import sys
import signal
import atexit

from . import core

# NOTE: queue has a different name in python2 and python3
if six.PY2:
import Queue as queue
else:
import queue
import queue

# multi-process worker check indices queue interval, avoid
# hanging in subprocess data loading
Expand Down
5 changes: 1 addition & 4 deletions python/paddle/fluid/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@
import signal

# NOTE: queue has a different name in python2 and python3
if six.PY2:
import Queue as queue
else:
import queue
import queue

# NOTE: [ avoid hanging & failed quickly ] These value is used in getting data from another process
QUEUE_GET_TIMEOUT = 60
Expand Down
10 changes: 2 additions & 8 deletions python/paddle/fluid/tests/unittests/dist_save_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ def get_data():

var = np.array(fluid.global_scope().find_var('__fc_b__').get_tensor(
))
if six.PY2:
print(pickle.dumps(np.ravel(var).tolist()))
else:
sys.stdout.buffer.write(pickle.dumps(np.ravel(var).tolist()))
sys.stdout.buffer.write(pickle.dumps(np.ravel(var).tolist()))

elif save_mode == "DIST":
skip_steps = int(os.getenv("SKIP_STEPS"))
Expand All @@ -191,10 +188,7 @@ def get_data():
continue
loss, = exe.run(fetch_list=[avg_cost.name],
feed=feeder.feed(data))
if six.PY2:
print(pickle.dumps(loss.tolist()))
else:
sys.stdout.buffer.write(pickle.dumps(loss.tolist()))
sys.stdout.buffer.write(pickle.dumps(loss.tolist()))
else:
raise Exception("save_mode must be LOCAL or DIST")

Expand Down
Loading