From 3cdc7a01267e2ecb0666b2887fdcbffcbc98c2e7 Mon Sep 17 00:00:00 2001 From: ShiningZhang Date: Tue, 26 Apr 2022 21:03:24 +0800 Subject: [PATCH] range can not return shape when enable_static (#42275) --- python/paddle/fluid/layers/tensor.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/python/paddle/fluid/layers/tensor.py b/python/paddle/fluid/layers/tensor.py index 693fbf20e64..a9b1fa6ff02 100644 --- a/python/paddle/fluid/layers/tensor.py +++ b/python/paddle/fluid/layers/tensor.py @@ -1470,6 +1470,11 @@ def range(start, end, step, dtype, name=None): # [3, 4, 5, 6] """ + out_shape = None + if not isinstance(start, Variable) and not isinstance( + end, Variable) and not isinstance(step, Variable): + out_shape = [int(math.ceil((end - start) / step))] + if not isinstance(dtype, core.VarDesc.VarType): dtype = convert_np_dtype_to_dtype_(dtype) @@ -1500,11 +1505,6 @@ def range(start, end, step, dtype, name=None): out.stop_gradient = True return out - out_shape = None - if not isinstance(start, Variable) and not isinstance( - end, Variable) and not isinstance(step, Variable): - out_shape = [int(math.ceil((end - start) / step))] - check_dtype(dtype, 'dtype', ['float32', 'float64', 'int32', 'int64'], 'range/arange') helper = LayerHelper('range', **locals()) @@ -1516,6 +1516,8 @@ def range(start, end, step, dtype, name=None): 'Step': step}, outputs={'Out': out}) out.stop_gradient = True + if out_shape is not None: + out.desc.set_shape(out_shape) return out -- GitLab