Skip to content

Commit

Permalink
[BUG] fix bug of float/int/long/index Tensor (#55568)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhwesky2010 authored Jul 26, 2023
1 parent ee506c2 commit a4644c5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
16 changes: 12 additions & 4 deletions python/paddle/fluid/dygraph/math_op_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,27 @@ def _float_(var):
), "only one element variable can be converted to float."
tensor = var.value().get_tensor()
assert tensor._is_initialized(), "variable's tensor is not initialized"
return float(np.array(var).flatten()[0])
if var.dtype == core.VarDesc.VarType.BF16:
var = var.astype('float32')
return float(np.array(var))

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"
return int(np.array(var).flatten()[0])
if var.dtype == core.VarDesc.VarType.BF16:
var = var.astype('float32')
return int(np.array(var))

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"
return int(np.array(var).flatten()[0])
if var.dtype == core.VarDesc.VarType.BF16:
var = var.astype('float32')
return int(np.array(var))

def _len_(var):
assert var.ndim > 0, "len() of a 0-D tensor is wrong"
Expand All @@ -146,7 +152,9 @@ def _index_(var):
), "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"
return int(np.array(var).flatten()[0])
if var.dtype == core.VarDesc.VarType.BF16:
var = var.astype('float32')
return int(np.array(var))

@property
def _ndim_(var):
Expand Down
15 changes: 15 additions & 0 deletions test/legacy_test/test_math_op_patch_var_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ def test_float_int_long(self):
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():
Expand All @@ -260,6 +265,16 @@ def test_index(self):
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)
Expand Down

0 comments on commit a4644c5

Please sign in to comment.