fix bug that to_tensor not support paddle.Place (#28717)

musl/fix_failed_unittests_in_musl
Zhou Wei 5 years ago committed by GitHub
parent e1c8d6bce5
commit 3b0dd5f620
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -69,9 +69,12 @@ static const platform::Place PyObjectToPlace(const py::object &place_obj) {
return place_obj.cast<platform::XPUPlace>();
} else if (py::isinstance<platform::CUDAPinnedPlace>(place_obj)) {
return place_obj.cast<platform::CUDAPinnedPlace>();
} else if (py::isinstance<platform::Place>(place_obj)) {
return place_obj.cast<platform::Place>();
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
"Place should be one of CPUPlace/XPUPlace/CUDAPlace/CUDAPinnedPlace"));
"Place should be one of "
"Place/CPUPlace/XPUPlace/CUDAPlace/CUDAPinnedPlace"));
}
}

@ -40,6 +40,9 @@ class TestVarBase(unittest.TestCase):
self.assertTrue(np.array_equal(x.numpy(), [1]))
self.assertNotEqual(x.dtype, core.VarDesc.VarType.FP32)
y = paddle.to_tensor(2, place=x.place)
self.assertEqual(str(x.place), str(y.place))
# set_default_dtype should not take effect on numpy
x = paddle.to_tensor(
np.array([1.2]).astype('float16'),

@ -126,10 +126,10 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
if place is None:
place = _current_expected_place()
elif not isinstance(place,
(core.CPUPlace, core.CUDAPinnedPlace, core.CUDAPlace)):
elif not isinstance(place, (core.Place, core.CPUPlace, core.CUDAPinnedPlace,
core.CUDAPlace)):
raise ValueError(
"'place' must be any of paddle.Place, paddle.CUDAPinnedPlace, paddle.CUDAPlace"
"'place' must be any of paddle.Place, paddle.CPUPlace, paddle.CUDAPinnedPlace, paddle.CUDAPlace"
)
#Todo(zhouwei): Support allocate tensor on any other specified card

Loading…
Cancel
Save