Skip to content

Commit 5d9f687

Browse files
committed
fix bug when dtype of to_tensor is core.VarType
1 parent bf09dcb commit 5d9f687

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

python/paddle/fluid/tests/unittests/test_var_base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ def _test_place(place):
7676
y = x.cuda(blocking=True)
7777
self.assertEqual(y.place.__repr__(), "CUDAPlace(0)")
7878

79+
# support 'dtype' is core.VarType
80+
x = paddle.rand((2, 2))
81+
y = paddle.to_tensor([2, 2], dtype=x.dtype)
82+
self.assertEqual(y.dtype, core.VarDesc.VarType.FP32)
83+
7984
# set_default_dtype take effect on complex
8085
x = paddle.to_tensor(1 + 2j, place=place, stop_gradient=False)
8186
self.assertTrue(np.array_equal(x.numpy(), [1 + 2j]))

python/paddle/tensor/creation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
168168
data = data.astype(default_type)
169169

170170
if dtype and convert_dtype(dtype) != data.dtype:
171-
data = data.astype(dtype)
171+
data = data.astype(convert_dtype(dtype))
172172

173173
return paddle.Tensor(
174174
value=data,

0 commit comments

Comments
 (0)