未验证 提交 5414694b 编写于 作者: Y YuanRisheng 提交者: GitHub

[BugFix]Fix randint_like bugs when save program that don't need use tensor's value (#44446)

* fix bugs of random

* fix unittest error

* fix unittest bugs
上级 d373f4ff
...@@ -42,24 +42,7 @@ class TestRandintLikeAPI(unittest.TestCase): ...@@ -42,24 +42,7 @@ class TestRandintLikeAPI(unittest.TestCase):
x_bool = paddle.fluid.data(name="x_bool", x_bool = paddle.fluid.data(name="x_bool",
shape=[10, 12], shape=[10, 12],
dtype="bool") dtype="bool")
x_int32 = paddle.fluid.data(name="x_int32",
shape=[10, 12],
dtype="int32")
x_int64 = paddle.fluid.data(name="x_int64",
shape=[10, 12],
dtype="int64")
x_float16 = paddle.fluid.data(name="x_float16",
shape=[10, 12],
dtype="float16")
x_float32 = paddle.fluid.data(name="x_float32",
shape=[10, 12],
dtype="float32")
x_float64 = paddle.fluid.data(name="x_float64",
shape=[10, 12],
dtype="float64")
exe = paddle.static.Executor(self.place) exe = paddle.static.Executor(self.place)
# x dtype is bool output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"] # x dtype is bool output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"]
outlist1 = [ outlist1 = [
paddle.randint_like(x_bool, low=-10, high=10, dtype=dtype) paddle.randint_like(x_bool, low=-10, high=10, dtype=dtype)
...@@ -69,7 +52,11 @@ class TestRandintLikeAPI(unittest.TestCase): ...@@ -69,7 +52,11 @@ class TestRandintLikeAPI(unittest.TestCase):
for out, dtype in zip(outs1, self.dtype): for out, dtype in zip(outs1, self.dtype):
self.assertTrue(out.dtype, np.dtype(dtype)) self.assertTrue(out.dtype, np.dtype(dtype))
self.assertTrue(((out >= -10) & (out <= 10)).all(), True) self.assertTrue(((out >= -10) & (out <= 10)).all(), True)
with program_guard(Program(), Program()):
x_int32 = paddle.fluid.data(name="x_int32",
shape=[10, 12],
dtype="int32")
exe = paddle.static.Executor(self.place)
# x dtype is int32 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"] # x dtype is int32 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"]
outlist2 = [ outlist2 = [
paddle.randint_like(x_int32, low=-5, high=10, dtype=dtype) paddle.randint_like(x_int32, low=-5, high=10, dtype=dtype)
...@@ -80,6 +67,11 @@ class TestRandintLikeAPI(unittest.TestCase): ...@@ -80,6 +67,11 @@ class TestRandintLikeAPI(unittest.TestCase):
self.assertTrue(out.dtype, np.dtype(dtype)) self.assertTrue(out.dtype, np.dtype(dtype))
self.assertTrue(((out >= -5) & (out <= 10)).all(), True) self.assertTrue(((out >= -5) & (out <= 10)).all(), True)
with program_guard(Program(), Program()):
x_int64 = paddle.fluid.data(name="x_int64",
shape=[10, 12],
dtype="int64")
exe = paddle.static.Executor(self.place)
# x dtype is int64 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"] # x dtype is int64 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"]
outlist3 = [ outlist3 = [
paddle.randint_like(x_int64, low=-100, high=100, dtype=dtype) paddle.randint_like(x_int64, low=-100, high=100, dtype=dtype)
...@@ -89,7 +81,12 @@ class TestRandintLikeAPI(unittest.TestCase): ...@@ -89,7 +81,12 @@ class TestRandintLikeAPI(unittest.TestCase):
for out, dtype in zip(outs3, self.dtype): for out, dtype in zip(outs3, self.dtype):
self.assertTrue(out.dtype, np.dtype(dtype)) self.assertTrue(out.dtype, np.dtype(dtype))
self.assertTrue(((out >= -100) & (out <= 100)).all(), True) self.assertTrue(((out >= -100) & (out <= 100)).all(), True)
if paddle.is_compiled_with_cuda():
with program_guard(Program(), Program()):
x_float16 = paddle.fluid.data(name="x_float16",
shape=[10, 12],
dtype="float16")
exe = paddle.static.Executor(self.place)
# x dtype is float16 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"] # x dtype is float16 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"]
outlist4 = [ outlist4 = [
paddle.randint_like(x_float16, low=-3, high=25, dtype=dtype) paddle.randint_like(x_float16, low=-3, high=25, dtype=dtype)
...@@ -101,6 +98,11 @@ class TestRandintLikeAPI(unittest.TestCase): ...@@ -101,6 +98,11 @@ class TestRandintLikeAPI(unittest.TestCase):
self.assertTrue(out.dtype, np.dtype(dtype)) self.assertTrue(out.dtype, np.dtype(dtype))
self.assertTrue(((out >= -3) & (out <= 25)).all(), True) self.assertTrue(((out >= -3) & (out <= 25)).all(), True)
with program_guard(Program(), Program()):
x_float32 = paddle.fluid.data(name="x_float32",
shape=[10, 12],
dtype="float32")
exe = paddle.static.Executor(self.place)
# x dtype is float32 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"] # x dtype is float32 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"]
outlist5 = [ outlist5 = [
paddle.randint_like(x_float32, low=-25, high=25, dtype=dtype) paddle.randint_like(x_float32, low=-25, high=25, dtype=dtype)
...@@ -112,6 +114,11 @@ class TestRandintLikeAPI(unittest.TestCase): ...@@ -112,6 +114,11 @@ class TestRandintLikeAPI(unittest.TestCase):
self.assertTrue(out.dtype, np.dtype(dtype)) self.assertTrue(out.dtype, np.dtype(dtype))
self.assertTrue(((out >= -25) & (out <= 25)).all(), True) self.assertTrue(((out >= -25) & (out <= 25)).all(), True)
with program_guard(Program(), Program()):
x_float64 = paddle.fluid.data(name="x_float64",
shape=[10, 12],
dtype="float64")
exe = paddle.static.Executor(self.place)
# x dtype is float64 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"] # x dtype is float64 output dtype in ["bool", "int32", "int64", "float16", "float32", "float64"]
outlist6 = [ outlist6 = [
paddle.randint_like(x_float64, low=-16, high=16, dtype=dtype) paddle.randint_like(x_float64, low=-16, high=16, dtype=dtype)
...@@ -125,10 +132,10 @@ class TestRandintLikeAPI(unittest.TestCase): ...@@ -125,10 +132,10 @@ class TestRandintLikeAPI(unittest.TestCase):
def test_dygraph_api(self): def test_dygraph_api(self):
paddle.disable_static(self.place) paddle.disable_static(self.place)
# x dtype ["bool", "int32", "int64", "float16", "float32", "float64"] # x dtype ["bool", "int32", "int64", "float32", "float64"]
for x in [ for x in [
self.x_bool, self.x_int32, self.x_int64, self.x_float16, self.x_bool, self.x_int32, self.x_int64, self.x_float32,
self.x_float32, self.x_float64 self.x_float64
]: ]:
x_inputs = paddle.to_tensor(x) x_inputs = paddle.to_tensor(x)
# self.dtype ["bool", "int32", "int64", "float16", "float32", "float64"] # self.dtype ["bool", "int32", "int64", "float16", "float32", "float64"]
...@@ -140,7 +147,18 @@ class TestRandintLikeAPI(unittest.TestCase): ...@@ -140,7 +147,18 @@ class TestRandintLikeAPI(unittest.TestCase):
self.assertTrue(out.numpy().dtype, np.dtype(dtype)) self.assertTrue(out.numpy().dtype, np.dtype(dtype))
self.assertTrue( self.assertTrue(
((out.numpy() >= -100) & (out.numpy() <= 100)).all(), True) ((out.numpy() >= -100) & (out.numpy() <= 100)).all(), True)
# x dtype ["float16"]
if paddle.is_compiled_with_cuda():
x_inputs = paddle.to_tensor(self.x_float16)
# self.dtype ["bool", "int32", "int64", "float16", "float32", "float64"]
for dtype in self.dtype:
out = paddle.randint_like(x_inputs,
low=-100,
high=100,
dtype=dtype)
self.assertTrue(out.numpy().dtype, np.dtype(dtype))
self.assertTrue(
((out.numpy() >= -100) & (out.numpy() <= 100)).all(), True)
paddle.enable_static() paddle.enable_static()
def test_errors(self): def test_errors(self):
...@@ -203,6 +221,7 @@ class TestRandintLikeAPI(unittest.TestCase): ...@@ -203,6 +221,7 @@ class TestRandintLikeAPI(unittest.TestCase):
# x dtype is float16 # x dtype is float16
# low is 5 and high is 5, low must less then high # low is 5 and high is 5, low must less then high
if paddle.is_compiled_with_cuda():
self.assertRaises(ValueError, self.assertRaises(ValueError,
paddle.randint_like, paddle.randint_like,
x_float16, x_float16,
......
...@@ -869,7 +869,7 @@ def randint_like(x, low=0, high=None, dtype=None, name=None): ...@@ -869,7 +869,7 @@ def randint_like(x, low=0, high=None, dtype=None, name=None):
dtype = x.dtype dtype = x.dtype
if not isinstance(dtype, core.VarDesc.VarType): if not isinstance(dtype, core.VarDesc.VarType):
dtype = convert_np_dtype_to_dtype_(dtype) dtype = convert_np_dtype_to_dtype_(dtype)
shape = x.shape shape = paddle.shape(x)
if low >= high: if low >= high:
raise ValueError( raise ValueError(
...@@ -888,17 +888,13 @@ def randint_like(x, low=0, high=None, dtype=None, name=None): ...@@ -888,17 +888,13 @@ def randint_like(x, low=0, high=None, dtype=None, name=None):
['bool', 'float16', 'float32', 'float64', 'int32', 'int64'], ['bool', 'float16', 'float32', 'float64', 'int32', 'int64'],
'randint_like') 'randint_like')
inputs = dict() inputs = {"ShapeTensor": shape}
attrs = { attrs = {
'low': low, 'low': low,
'high': high, 'high': high,
'seed': 0, 'seed': 0,
'dtype': core.VarDesc.VarType.INT64 'dtype': core.VarDesc.VarType.INT64
} }
utils.get_shape_tensor_inputs(inputs=inputs,
attrs=attrs,
shape=shape,
op_type='randint_like')
helper = LayerHelper("randint", **locals()) helper = LayerHelper("randint", **locals())
out = helper.create_variable_for_type_inference( out = helper.create_variable_for_type_inference(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册