Skip to content

Commit

Permalink
[CodeStyle][py2] remove six package (part2) (#47334)
Browse files Browse the repository at this point in the history
* [CodeStyle][py2] remove `six` package (part2)

* six.ensure_str

* remove unused `import six`

* remove six from BUILTIN_LIKELY_MODULES

* remove six in example code

* remove some decode

* try to fix example code

* fix MockEtcdClient get/get_prefix returns data type

* fix MockEtcdClient get_prefix returns data

* fix MockEtcdClient get returns data

* remove `six` in pypi and conda requirements

* fix MockEtcdClient add_watch_callback/add_watch_prefix_callback returns data type

* refine MockEtcdClient
  • Loading branch information
SigureMo authored Nov 1, 2022
1 parent 3097a66 commit 3592ba8
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 87 deletions.
2 changes: 0 additions & 2 deletions paddle/scripts/conda_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def __init__(self):
- protobuf>=3.1.0
- gast==0.3.3
- Pillow
- six
- decorator
- astor
"""
Expand All @@ -67,7 +66,6 @@ def __init__(self):
- protobuf>=3.1.0
- gast==0.3.3
- Pillow
- six
- decorator
- astor
"""
Expand Down
12 changes: 5 additions & 7 deletions python/paddle/distributed/fleet/elastic/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import time
import socket
import os
import six
import copy
import signal
import random
Expand Down Expand Up @@ -244,8 +243,7 @@ def __init__(self, args, etcd_client):
# register callback
def host_call_back(event):
self.hosts = [
six.ensure_str(i[0])
for i in self.etcd.get_prefix(self.node_prefix)
i[0].decode() for i in self.etcd.get_prefix(self.node_prefix)
]
self.hosts = list(set(self.hosts)) if self.hosts else self.hosts
logger.info(
Expand All @@ -266,7 +264,7 @@ def lease_heartbeat():
host_lease.refresh()

hosts = [
six.ensure_str(i[0])
i[0].decode()
for i in self.etcd.get_prefix(self.node_prefix)
]
hosts = list(set(hosts)) if hosts else hosts
Expand Down Expand Up @@ -311,7 +309,8 @@ def lease_heartbeat():
def endpoints_call_back(event):
if not self.dist_endpoints:
return
edps = six.ensure_str(self.etcd.get(self.endpoints_path)[0] or '')
value = self.etcd.get(self.endpoints_path)[0]
edps = value.decode() if value is not None else ''
self.dist_endpoints, self.trainers = edps.split('|')
logger.info(
"set DISTRIBUTED_TRAINER_ENDPOINTS {} ".format(
Expand Down Expand Up @@ -426,8 +425,7 @@ def _match(self, host_list: list = None):
self.hosts = host_list
else:
self.hosts = [
six.ensure_str(i[0])
for i in self.etcd.get_prefix(self.node_prefix)
i[0].decode() for i in self.etcd.get_prefix(self.node_prefix)
]
self.hosts = list(set(self.hosts)) if self.hosts else self.hosts

Expand Down
17 changes: 8 additions & 9 deletions python/paddle/distributed/launch/controllers/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import time
import sys
import six
import threading
import copy
import random
Expand Down Expand Up @@ -214,22 +213,22 @@ def sync_peers(self, prefix, key, value, size, rank=-1) -> (list, int):

if len(result) == size:
if rank < 0:
keys = [six.ensure_str(i[1].key) for i in result]
sorted_keys = [six.ensure_str(i[1].key) for i in result]
keys = [i[1].key.decode() for i in result]
sorted_keys = [i[1].key.decode() for i in result]
sorted_keys.sort()
values = [six.ensure_str(i[0]) for i in result]
values = [i[0].decode() for i in result]
ret = [values[keys.index(k)] for k in sorted_keys]
idx = ret.index(value)
return ret, idx
else:
ret = [None] * size
for v, k in result:
ii = int(six.ensure_str(k.key).split('/')[-1])
ii = int(k.key.decode().split('/')[-1])
if ii < 0:
self.ctx.logger.error(
"rank {} error in sync".format(ii)
)
ret[ii] = six.ensure_str(v)
ret[ii] = v.decode()
return ret, rank
else:
time.sleep(0.5)
Expand Down Expand Up @@ -278,8 +277,7 @@ def _heartbeat():

def fetch_peer_alive(self):
peer_alive = [
six.ensure_str(i[0])
for i in self.client.get_prefix(self.heartbeat_prefix)
i[0].decode() for i in self.client.get_prefix(self.heartbeat_prefix)
]
self.ctx.logger.debug("peer alive {}".format(peer_alive))
return peer_alive
Expand Down Expand Up @@ -319,7 +317,8 @@ def set_status(self, status):
), "set status failed {}".format(status)

def get_status(self):
return six.ensure_str(self.client.get(self.job_prefix)[0] or '')
value = self.client.get(self.job_prefix)[0]
return value.decode() if value is not None else ''

def stop(self):
if hasattr(self, 'beat_thread'):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import six
import abc
import copy
import math
Expand Down
13 changes: 6 additions & 7 deletions python/paddle/fluid/dataloader/dataloader_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

import os
import six
import sys
import time
import signal
Expand Down Expand Up @@ -284,9 +283,9 @@ def _thread_loop(self, legacy_expected_place):
except:
self._exit_thread_expectedly()

except:
except Exception as e:
self._exit_thread_unexpectedly()
six.reraise(*sys.exc_info())
raise e

self._exit_thread_expectedly()

Expand Down Expand Up @@ -334,7 +333,7 @@ def __next__(self):
except StopIteration:
self._reader.shutdown()
self._try_shutdown_all()
six.reraise(*sys.exc_info())
raise
finally:
if in_profiler_mode():
trace_event.end()
Expand Down Expand Up @@ -629,7 +628,7 @@ def _thread_loop(self, legacy_expected_place):
self._blocking_queue.close()
except Exception as e:
self._exit_thread_unexpectedly()
six.reraise(*sys.exc_info())
raise e
finally:
self._rcvd_idx += 1

Expand Down Expand Up @@ -715,7 +714,7 @@ def _get_data(self):
"DataLoader reader thread failed({}) to read data from "
"workers' result queue.".format(e)
)
six.reraise(*sys.exc_info())
raise e
else:
if self._dataset_kind == _DatasetKind.ITER and isinstance(
data, _IterableDatasetStopIteration
Expand Down Expand Up @@ -850,7 +849,7 @@ def __next__(self):
if not self._persistent_workers:
self._reader.shutdown()
self._try_shutdown_all()
six.reraise(*sys.exc_info())
raise
finally:
if in_profiler_mode():
trace_event.end()
Expand Down
3 changes: 1 addition & 2 deletions python/paddle/fluid/dataloader/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

import os
import six
import sys
import paddle
import numpy as np
Expand Down Expand Up @@ -395,7 +394,7 @@ def tensor_share_memory(tensor):
# NOTE: Main process will raise KeyboardInterrupt anyways, ignore it in child process
pass
except:
six.reraise(*sys.exc_info())
raise
finally:
if use_shared_memory:
_cleanup_mmap()
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import types

import numpy
import six
import builtins

from paddle.fluid.dygraph.container import Sequential
Expand Down Expand Up @@ -58,7 +57,6 @@
copy,
inspect,
re,
six,
numpy,
logging,
]
Expand Down
32 changes: 14 additions & 18 deletions python/paddle/fluid/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import warnings
import numpy as np
from .wrapped_decorator import signature_safe_contextmanager
import six
from .data_feeder import convert_dtype
from .framework import Program, default_main_program, Variable, Operator
from .framework import convert_np_dtype_to_dtype_, _apply_pass
Expand Down Expand Up @@ -1574,23 +1573,20 @@ def run(
]
self._log_force_set_program_cache(use_program_cache)

try:
res = self._run_impl(
program=program,
feed=feed,
fetch_list=fetch_list,
feed_var_name=feed_var_name,
fetch_var_name=fetch_var_name,
scope=scope,
return_numpy=return_numpy,
use_program_cache=use_program_cache,
use_prune=use_prune,
return_merged=return_merged,
)
core.update_autotune_status()
return res
except Exception as e:
six.reraise(*sys.exc_info())
res = self._run_impl(
program=program,
feed=feed,
fetch_list=fetch_list,
feed_var_name=feed_var_name,
fetch_var_name=fetch_var_name,
scope=scope,
return_numpy=return_numpy,
use_program_cache=use_program_cache,
use_prune=use_prune,
return_merged=return_merged,
)
core.update_autotune_status()
return res

def _run_impl(
self,
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -5777,10 +5777,10 @@ def clone(self, for_test=False):
.. code-block:: python
import six
import paddle
def print_prog(prog):
for name, value in sorted(six.iteritems(prog.block(0).vars)):
for name, value in sorted(prog.block(0).vars.items()):
print(value)
for op in prog.block(0).ops:
print("op type is {}".format(op.type))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import os
import warnings
import logging
import six
import paddle.fluid as fluid
from paddle.fluid import core
from paddle.fluid.core import CommContext
Expand Down
5 changes: 2 additions & 3 deletions python/paddle/fluid/layers/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import multiprocessing
import os
import six
import sys
import threading

Expand Down Expand Up @@ -523,10 +522,10 @@ def __provider_thread__(legacy_expected_place):
if reader.exited:
break
feed_queue.close()
except Exception as ex:
except Exception as e:
feed_queue.kill()
logging.warn('Your decorated reader has raised an exception!')
six.reraise(*sys.exc_info())
raise e

reader.thread = threading.Thread(
target=__provider_thread__, args=(_current_expected_place(),)
Expand Down
7 changes: 3 additions & 4 deletions python/paddle/fluid/layers/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -14998,7 +14998,6 @@ def py_func(func, x, out, backward_func=None, skip_vars_in_backward_input=None):

# example 1:
import paddle
import six
import numpy as np

paddle.enable_static()
Expand All @@ -15024,7 +15023,7 @@ def create_tmp_var(name, dtype, shape):

def simple_net(img, label):
hidden = img
for idx in six.moves.range(4):
for idx in range(4):
hidden = paddle.static.nn.fc(hidden, size=200)
new_hidden = create_tmp_var(name='hidden_{}'.format(idx),
dtype=hidden.dtype, shape=hidden.shape)
Expand All @@ -15042,13 +15041,13 @@ def simple_net(img, label):
return ce_loss(prediction, label)

x = paddle.static.data(name='x', shape=[1,4], dtype='float32')
y = paddle.static.data(name='y', shape=[1,10], dtype='int64')
y = paddle.static.data(name='y', shape=[1], dtype='int64')
res = simple_net(x, y)

exe = paddle.static.Executor(paddle.CPUPlace())
exe.run(paddle.static.default_startup_program())
input1 = np.random.random(size=[1,4]).astype('float32')
input2 = np.random.randint(1, 10, size=[1,10], dtype='int64')
input2 = np.random.randint(1, 10, size=[1], dtype='int64')
out = exe.run(paddle.static.default_main_program(),
feed={'x':input1, 'y':input2},
fetch_list=[res.name])
Expand Down
1 change: 0 additions & 1 deletion python/paddle/fluid/layers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import collections
import copy
import six
import numpy as np
from ..framework import Block, Variable, _non_static_mode
from ..data_feeder import (
Expand Down
Loading

0 comments on commit 3592ba8

Please sign in to comment.