From e8cb250354e3ed528480c621194f27a1cca4d036 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Wed, 17 Jan 2024 10:56:05 +0000 Subject: [PATCH 1/9] [Dy2St] Make `bool(Value)` always throw error --- python/paddle/pir/math_op_patch.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/python/paddle/pir/math_op_patch.py b/python/paddle/pir/math_op_patch.py index 7cbe07b1b6b0f..1b565bca23372 100644 --- a/python/paddle/pir/math_op_patch.py +++ b/python/paddle/pir/math_op_patch.py @@ -434,6 +434,13 @@ def _T_(self): return _C_ops.transpose(self, perm) + def _bool_(self): + raise TypeError( + "bool(Value) is not supported in static graph mode. If you are using @to_static, you can try this:\n" + "1. If you want to get the value of Value, you can switch to non-fullgraph mode by setting @to_static(full_graph=True).\n" + "2. If you want to run it in full graph mode, you need use Value.astype(paddle.bool), and do not use bool(Value)." + ) + def clone(self): """ Returns a new static Value, which is the clone of the original static @@ -653,6 +660,7 @@ def value_hash(self): '__ge__', paddle.tensor.greater_equal, False, None ), ), + ('__bool__', _bool_), ] global _already_patch_value From 50e743d58d44eb7e79043de03aa17e4664862973 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Fri, 19 Jan 2024 02:09:14 +0000 Subject: [PATCH 2/9] add ut for bool --- test/legacy_test/test_math_op_patch_pir.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/legacy_test/test_math_op_patch_pir.py b/test/legacy_test/test_math_op_patch_pir.py index 3bc78195af6f4..4b1c90e397a8a 100644 --- a/test/legacy_test/test_math_op_patch_pir.py +++ b/test/legacy_test/test_math_op_patch_pir.py @@ -533,6 +533,8 @@ def test_builtin_type_conversion(self): int(x) with self.assertRaises(TypeError): float(x) + with self.assertRaises(TypeError): + bool(x) def test_math_exists(self): with paddle.pir_utils.IrGuard(): From 936dba774b2ff6b64e077ce8e49fc49388c01508 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Fri, 19 Jan 2024 17:01:39 +0000 Subject: [PATCH 3/9] fix test_adamw_op --- python/paddle/optimizer/adamw.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/python/paddle/optimizer/adamw.py b/python/paddle/optimizer/adamw.py index 6077c60221eb1..3460720b98d4b 100644 --- a/python/paddle/optimizer/adamw.py +++ b/python/paddle/optimizer/adamw.py @@ -175,11 +175,20 @@ def __init__( assert beta1 is not None assert beta2 is not None assert epsilon is not None - if not 0 <= beta1 < 1: + if ( + not isinstance(beta1, (framework.Variable, Value)) + and not 0 <= beta1 < 1 + ): raise ValueError("Invaild value of beta1, expect beta1 in [0,1).") - if not 0 <= beta2 < 1: + if ( + not isinstance(beta1, (framework.Variable, Value)) + and not 0 <= beta2 < 1 + ): raise ValueError("Invaild value of beta2, expect beta2 in [0,1).") - if not 0 <= epsilon: + if ( + not isinstance(beta1, (framework.Variable, Value)) + and not 0 <= epsilon + ): raise ValueError("Invaild value of epsilon, expect epsilon >= 0.") if not isinstance(weight_decay, float) and not isinstance( weight_decay, (framework.Variable, Value) From bea8858f2aee50a070d2aac661b7691478fe8848 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Fri, 19 Jan 2024 17:15:09 +0000 Subject: [PATCH 4/9] fix test_triplet_margin_with_distance_loss --- python/paddle/nn/functional/loss.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/python/paddle/nn/functional/loss.py b/python/paddle/nn/functional/loss.py index f4e44a0dd9cc0..b18b3367ab40e 100644 --- a/python/paddle/nn/functional/loss.py +++ b/python/paddle/nn/functional/loss.py @@ -3733,7 +3733,13 @@ def triplet_margin_with_distance_loss( swap_dist = distance_function(positive, negative) negative_dist = paddle.minimum(negative_dist, swap_dist) - if not paddle.all(positive_dist > 0) or not paddle.all(negative_dist > 0): + if ( + not isinstance(positive_dist, (Variable, paddle.pir.Value)) + and not paddle.all(positive_dist > 0) + ) or ( + not isinstance(negative_dist, (Variable, paddle.pir.Value)) + and not paddle.all(negative_dist > 0) + ): raise ValueError( "The positive distance or negative distance should be greater than 0, " "The distance functions should be checked." From 37dc5e305cc28c8feaea9f6ff4dd177a7c45f1a4 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 20 Jan 2024 08:46:05 +0000 Subject: [PATCH 5/9] remove Variable check, since it will skip the Tensor check --- python/paddle/nn/functional/loss.py | 4 ++-- python/paddle/optimizer/adamw.py | 15 +++------------ 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/python/paddle/nn/functional/loss.py b/python/paddle/nn/functional/loss.py index b18b3367ab40e..98605b04415dc 100644 --- a/python/paddle/nn/functional/loss.py +++ b/python/paddle/nn/functional/loss.py @@ -3734,10 +3734,10 @@ def triplet_margin_with_distance_loss( negative_dist = paddle.minimum(negative_dist, swap_dist) if ( - not isinstance(positive_dist, (Variable, paddle.pir.Value)) + not isinstance(positive_dist, paddle.pir.Value) and not paddle.all(positive_dist > 0) ) or ( - not isinstance(negative_dist, (Variable, paddle.pir.Value)) + not isinstance(negative_dist, paddle.pir.Value) and not paddle.all(negative_dist > 0) ): raise ValueError( diff --git a/python/paddle/optimizer/adamw.py b/python/paddle/optimizer/adamw.py index 3460720b98d4b..55b333630a745 100644 --- a/python/paddle/optimizer/adamw.py +++ b/python/paddle/optimizer/adamw.py @@ -175,20 +175,11 @@ def __init__( assert beta1 is not None assert beta2 is not None assert epsilon is not None - if ( - not isinstance(beta1, (framework.Variable, Value)) - and not 0 <= beta1 < 1 - ): + if not isinstance(beta1, Value) and not 0 <= beta1 < 1: raise ValueError("Invaild value of beta1, expect beta1 in [0,1).") - if ( - not isinstance(beta1, (framework.Variable, Value)) - and not 0 <= beta2 < 1 - ): + if not isinstance(beta1, Value) and not 0 <= beta2 < 1: raise ValueError("Invaild value of beta2, expect beta2 in [0,1).") - if ( - not isinstance(beta1, (framework.Variable, Value)) - and not 0 <= epsilon - ): + if not isinstance(beta1, Value) and not 0 <= epsilon: raise ValueError("Invaild value of epsilon, expect epsilon >= 0.") if not isinstance(weight_decay, float) and not isinstance( weight_decay, (framework.Variable, Value) From cda3f252c2ace18488baddcd477f1212ab746471 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Tue, 23 Jan 2024 03:16:37 +0000 Subject: [PATCH 6/9] fix test_decompose_op --- python/paddle/decomposition/decomp.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/decomposition/decomp.py b/python/paddle/decomposition/decomp.py index ed158eae331b3..4a8fa8bd439e5 100644 --- a/python/paddle/decomposition/decomp.py +++ b/python/paddle/decomposition/decomp.py @@ -84,7 +84,7 @@ def _prepare_python_api_arguments(op): inputs = [] for x in op.operands(): input = x.source() - if input and input.initialized(): + if input.initialized(): prev_op = input.get_defining_op() if ( isinstance(prev_op, Operation) @@ -111,7 +111,7 @@ def _check_prim_dynamic(op): inputs = [] for x in op.operands(): input = x.source() - if input and input.initialized(): + if input.initialized(): prev_op = input.get_defining_op() if ( isinstance(prev_op, Operation) From f2ba08242a48ca2d962bad528e82d4eefa1523c3 Mon Sep 17 00:00:00 2001 From: zoooo0820 Date: Tue, 23 Jan 2024 06:42:31 +0000 Subject: [PATCH 7/9] Separate py-int and 0-D Variable/Value in static-indexing --- python/paddle/base/variable_index.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/python/paddle/base/variable_index.py b/python/paddle/base/variable_index.py index 83b0ad108bd31..0aa491858fdc8 100644 --- a/python/paddle/base/variable_index.py +++ b/python/paddle/base/variable_index.py @@ -82,12 +82,10 @@ def replace_none(item): return new_item, none_axes -def is_integer_or_scalar_tensor(ele): +def is_scalar_tensor(ele): from .framework import Variable - if type(ele) is int: - return True - elif isinstance(ele, Variable): + if isinstance(ele, Variable): if len(ele.shape) == 0 and ele.dtype != paddle.bool: return True elif isinstance(ele, paddle.pir.Value): @@ -285,10 +283,9 @@ def parse_index(x, indices): dim = 0 for i, slice_item in enumerate(indices): start, end, step = None, None, None - if is_integer_or_scalar_tensor(slice_item): + if type(slice_item) is int: if ( not is_tensor_array - and isinstance(slice_item, int) and x.shape[dim] is not None and x.shape[dim] >= 0 and slice_item >= x.shape[dim] @@ -309,6 +306,13 @@ def parse_index(x, indices): step = 1 end = slice_item + 1 if slice_item != -1 else MAX_INTEGER dim += 1 + elif is_scalar_tensor(slice_item): + # not calculate result to reduce call times for slice OP. + decrease_axes.append(dim) + start = slice_item + step = 1 + end = slice_item + 1 + dim += 1 elif isinstance(slice_item, bool): # single bool is advanced-indexing none_axes.append(dim) From 81fab8dde731625a4bd4150ef6cdf8495694f076 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Tue, 23 Jan 2024 07:06:59 +0000 Subject: [PATCH 8/9] fix test_put_along_axis_op --- python/paddle/tensor/manipulation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index d62771bb8ed53..8547ab0a26286 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -5802,7 +5802,7 @@ def take_along_axis(arr, indices, axis, broadcast=True): ) axis_max_size = arr.shape[axis] - if not (indices < axis_max_size).all(): + if in_dynamic_mode() and not (indices < axis_max_size).all(): raise RuntimeError( "one of element of indices is out of bounds for dimension {} with size {}".format( axis, axis_max_size @@ -5958,7 +5958,7 @@ def put_along_axis( if elements == 1: # paddle.pir.Value has no attribute 'size' values = paddle.broadcast_to(values, indices.shape) axis_max_size = arr.shape[axis] - if not (indices < axis_max_size).all(): + if in_dynamic_mode() and not (indices < axis_max_size).all(): raise RuntimeError( "one of element of indices is out of bounds for dimension {} with size {}".format( axis, axis_max_size From f7de825604e736f9cb6ad6be726967a74b44be5c Mon Sep 17 00:00:00 2001 From: SigureMo Date: Tue, 23 Jan 2024 17:34:18 +0800 Subject: [PATCH 9/9] update op num --- test/dygraph_to_static/test_tensor_shape.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dygraph_to_static/test_tensor_shape.py b/test/dygraph_to_static/test_tensor_shape.py index 1bd2c6ffbebd5..88b2425f45d12 100644 --- a/test/dygraph_to_static/test_tensor_shape.py +++ b/test/dygraph_to_static/test_tensor_shape.py @@ -748,7 +748,7 @@ def _set_test_func(self): self.dygraph_func = dyfunc_with_for_1 def _set_expected_op_num(self): - self.expected_op_num = 29 + self.expected_op_num = 27 self.expected_shape_op_num = 2 self.expected_slice_op_num = 3