diff --git a/python/paddle/fluid/dygraph/math_op_patch.py b/python/paddle/fluid/dygraph/math_op_patch.py index 1a73e6b5e9ae0607476f420bf3e2eaf8fa252463..b82434d0588accd967c1929d4380427ce90b17ee 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 c392fb972697d4f4a8ae9cefeaffa8c8d2753014..0100f364bcddf58155742c9bce9f892d545173b4 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)