From d353e2fee480702626e7638164911966beb62feb Mon Sep 17 00:00:00 2001 From: zhwesky2010 <1183042833@qq.com> Date: Mon, 28 Aug 2023 14:39:38 +0800 Subject: [PATCH] Revert "[BUG] fix bug of float/int/long/index Tensor (#55568)" This reverts commit a4644c507e65ec3f38a790d9057cd82b0ee4c6cf. --- python/paddle/fluid/dygraph/math_op_patch.py | 16 ++++------------ test/legacy_test/test_math_op_patch_var_base.py | 15 --------------- 2 files changed, 4 insertions(+), 27 deletions(-) diff --git a/python/paddle/fluid/dygraph/math_op_patch.py b/python/paddle/fluid/dygraph/math_op_patch.py index 1a73e6b5e9a..b82434d0588 100644 --- a/python/paddle/fluid/dygraph/math_op_patch.py +++ b/python/paddle/fluid/dygraph/math_op_patch.py @@ -114,27 +114,21 @@ def monkey_patch_math_tensor(): ), "only one element variable can be converted to float." tensor = var.value().get_tensor() assert tensor._is_initialized(), "variable's tensor is not initialized" - if var.dtype == core.VarDesc.VarType.BF16: - var = var.astype('float32') - return float(np.array(var)) + return float(np.array(var).flatten()[0]) def _long_(var): numel = np.prod(var.shape) assert numel == 1, "only one element variable can be converted to long." tensor = var.value().get_tensor() assert tensor._is_initialized(), "variable's tensor is not initialized" - if var.dtype == core.VarDesc.VarType.BF16: - var = var.astype('float32') - return int(np.array(var)) + return int(np.array(var).flatten()[0]) def _int_(var): numel = np.prod(var.shape) assert numel == 1, "only one element variable can be converted to int." tensor = var.value().get_tensor() assert tensor._is_initialized(), "variable's tensor is not initialized" - if var.dtype == core.VarDesc.VarType.BF16: - var = var.astype('float32') - return int(np.array(var)) + return int(np.array(var).flatten()[0]) def _len_(var): assert var.ndim > 0, "len() of a 0-D tensor is wrong" @@ -152,9 +146,7 @@ def monkey_patch_math_tensor(): ), "only one element variable can be converted to python index." tensor = var.value().get_tensor() assert tensor._is_initialized(), "variable's tensor is not initialized" - if var.dtype == core.VarDesc.VarType.BF16: - var = var.astype('float32') - return int(np.array(var)) + return int(np.array(var).flatten()[0]) @property def _ndim_(var): diff --git a/test/legacy_test/test_math_op_patch_var_base.py b/test/legacy_test/test_math_op_patch_var_base.py index c392fb97269..0100f364bcd 100644 --- a/test/legacy_test/test_math_op_patch_var_base.py +++ b/test/legacy_test/test_math_op_patch_var_base.py @@ -242,11 +242,6 @@ class TestMathOpPatchesVarBase(unittest.TestCase): self.assertTrue(int(a) == 100) self.assertTrue(int(a) == 100) - a = paddle.to_tensor(1000000.0, dtype='bfloat16') - self.assertTrue(float(a) == 999424.0) - self.assertTrue(int(a) == 999424) - self.assertTrue(int(a) == 999424) - def test_len(self): a_np = np.random.uniform(-1, 1, self.shape).astype(self.dtype) with fluid.dygraph.guard(): @@ -265,16 +260,6 @@ class TestMathOpPatchesVarBase(unittest.TestCase): str1 = "just test" self.assertTrue(str1[var1] == 's') - var1 = paddle.to_tensor(2.0, dtype='bfloat16') - i_tmp = 0 - for i in range(var1): - self.assertTrue(i == i_tmp) - i_tmp = i_tmp + 1 - list1 = [1, 2, 3, 4, 5] - self.assertTrue(list1[var1] == 3) - str1 = "just test" - self.assertTrue(str1[var1] == 's') - def test_np_left_mul(self): with fluid.dygraph.guard(): t = np.sqrt(2.0 * np.pi) -- GitLab