Skip to content

Commit

Permalink
fix collections.Sequence in python3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
pangyoki committed Apr 25, 2022
1 parent 30f65c2 commit 95fa22e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
10 changes: 7 additions & 3 deletions python/paddle/fluid/backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
import paddle.fluid
from .data_feeder import check_type
import warnings
try:
from collections.abc import Sequence
except:
from collections import Sequence
__all__ = [
'append_backward',
'gradients',
Expand Down Expand Up @@ -1309,8 +1313,8 @@ def _append_backward_vars_(block, start_op_idx, grad_to_var, grad_info_map):
if grad_var_ins:
existing_grad_var_ins = [
var for var in grad_var_ins
if block.desc.has_var_recursive(cpt.to_bytes(var)) or var in
parent_op_vars
if block.desc.has_var_recursive(cpt.to_bytes(var)) or
var in parent_op_vars
]
if not existing_grad_var_ins:
'''
Expand Down Expand Up @@ -1722,7 +1726,7 @@ def append_backward(loss,
def _as_list(x):
if x is None:
return []
return list(x) if isinstance(x, collections.Sequence) else [x]
return list(x) if isinstance(x, Sequence) else [x]


def _is_ancestor_block(ancestor_block, block):
Expand Down
6 changes: 5 additions & 1 deletion python/paddle/fluid/dygraph/dygraph_to_static/origin_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
from paddle.fluid import core
from paddle.fluid.dygraph.dygraph_to_static.utils import unwrap
from paddle.fluid.framework import Program
try:
from collections.abc import Sequence
except:
from collections import Sequence

# NOTE(liym27): Please use `getattr(ast_node, ORIGI_INFO)` instead of . operation to get the original information of ast node.
ORIGI_INFO = "Original information of source code for ast node."
Expand Down Expand Up @@ -214,7 +218,7 @@ def ast_walk(transformed_node, static_node):
def _as_list(x):
if x is None:
return []
return list(x) if isinstance(x, collections.Sequence) else [x]
return list(x) if isinstance(x, Sequence) else [x]

transformed_node_list = _as_list(transformed_node)
static_node_list = _as_list(static_node)
Expand Down
6 changes: 5 additions & 1 deletion python/paddle/fluid/layers/rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
from ..framework import _non_static_mode
from ..param_attr import ParamAttr
from ..data_feeder import check_variable_and_dtype, check_type, check_dtype
try:
from collections.abc import Sequence
except:
from collections import Sequence

__all__ = [
'RNNCell',
Expand Down Expand Up @@ -163,7 +167,7 @@ def _is_shape_sequence(seq):
# TODO: Add check for the illegal
if isinstance(seq, dict):
return True
return (isinstance(seq, collections.Sequence) and
return (isinstance(seq, Sequence) and
not isinstance(seq, six.string_types))

class Shape(object):
Expand Down
9 changes: 6 additions & 3 deletions python/paddle/fluid/layers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
from ..data_feeder import convert_dtype, check_variable_and_dtype, check_type, check_dtype
from ..layer_helper import LayerHelper
from sys import version_info
try:
from collections.abc import Sequence
except:
from collections import Sequence


def convert_to_list(value, n, name, dtype=int):
Expand Down Expand Up @@ -74,8 +78,7 @@ def is_sequence(seq):
"""
if isinstance(seq, dict):
return True
return (isinstance(seq, collections.Sequence) and
not isinstance(seq, six.string_types))
return (isinstance(seq, Sequence) and not isinstance(seq, six.string_types))


def _hash_with_id(*args):
Expand Down Expand Up @@ -148,7 +151,7 @@ def _sequence_like(instance, args):
return type(instance)((key, result[key])
for key in six.iterkeys(instance))
elif (isinstance(instance, tuple) and hasattr(instance, "_fields") and
isinstance(instance._fields, collections.Sequence) and
isinstance(instance._fields, Sequence) and
all(isinstance(f, six.string_types) for f in instance._fields)):
# This is a namedtuple
return type(instance)(*args)
Expand Down
8 changes: 6 additions & 2 deletions python/paddle/fluid/tests/unittests/gradient_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
from paddle.fluid.executor import Executor
from paddle.fluid.backward import _append_grad_suffix_, _as_list
from paddle.fluid.framework import _test_eager_guard
try:
from collections.abc import Sequence
except:
from collections import Sequence


def _product(t):
Expand Down Expand Up @@ -91,7 +95,7 @@ def var_to_np_array_in_scope(scope, place, name):
def make_jacobian(x, y_size, np_dtype):
if isinstance(x, fluid.framework.Variable):
return np.zeros((_product(x.shape), y_size), dtype=np_dtype)
elif isinstance(x, collections.Sequence):
elif isinstance(x, Sequence):
jacobians = list(
filter(lambda t: t is not None, (make_jacobian(
item, y_size, np_dtype) for item in x)))
Expand Down Expand Up @@ -310,7 +314,7 @@ def fail_test(msg):
_compute_analytical_jacobian(prog, clone_x, clone_y, place, scope))

for i, (x_idx,
y_idx) in enumerate(product(*[range(len(x)), range(len(y))])):
y_idx) in enumerate(product(* [range(len(x)), range(len(y))])):
a = analytical[y_idx][x_idx]
n = numerical[x_idx][y_idx]
if not np.allclose(a, n, rtol, atol):
Expand Down
6 changes: 5 additions & 1 deletion python/paddle/nn/layer/rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
from paddle.framework import core
from paddle.static import default_startup_program
from paddle.static import program_guard
try:
from collections.abc import Sequence
except:
from collections import Sequence

__all__ = []

Expand Down Expand Up @@ -197,7 +201,7 @@ def _is_shape_sequence(seq):
# TODO: Add check for the illegal
if isinstance(seq, dict):
return True
return (isinstance(seq, collections.Sequence) and
return (isinstance(seq, Sequence) and
not isinstance(seq, six.string_types))

class Shape(object):
Expand Down

0 comments on commit 95fa22e

Please sign in to comment.