Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

paddle.to_tensor supports LoDTensor #33027

Merged
merged 1 commit into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions python/paddle/fluid/tests/unittests/test_var_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ def test_to_tensor_change_place(self):
a = paddle.to_tensor(a, place=paddle.CUDAPinnedPlace())
self.assertEqual(a.place.__repr__(), "CUDAPinnedPlace")

def test_to_tensor_with_lodtensor(self):
if core.is_compiled_with_cuda():
a_np = np.random.rand(1024, 1024)
with paddle.fluid.dygraph.guard(core.CPUPlace()):
lod_tensor = core.LoDTensor()
lod_tensor.set(a_np, core.CPUPlace())
a = paddle.to_tensor(lod_tensor)
self.assertTrue(np.array_equal(a_np, a.numpy()))

with paddle.fluid.dygraph.guard(core.CUDAPlace(0)):
lod_tensor = core.LoDTensor()
lod_tensor.set(a_np, core.CUDAPlace(0))
a = paddle.to_tensor(lod_tensor)
self.assertTrue(np.array_equal(a_np, a.numpy()))

def test_to_variable(self):
with fluid.dygraph.guard():
var = fluid.dygraph.to_variable(self.array, name="abc")
Expand Down
22 changes: 15 additions & 7 deletions python/paddle/tensor/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
place = _current_expected_place()

if not isinstance(data, np.ndarray):

def _handle_diff_place_dtype(data, dtype, place, stop_gradient):
data.stop_gradient = stop_gradient
if not data.place._equals(place):
data = data._copy_to(place, False)
if dtype:
if convert_dtype(dtype) != convert_dtype(data.dtype):
return data.astype(convert_dtype(dtype))
return data

if np.isscalar(data) and not isinstance(data, str):
data = np.array([data])
elif isinstance(data, (list, tuple)):
Expand All @@ -128,13 +138,11 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
"this means the input data contains nested lists with different lengths. "
)
elif isinstance(data, paddle.Tensor):
data.stop_gradient = stop_gradient
if not data.place._equals(place):
data = data._copy_to(place, False)
if dtype:
if convert_dtype(dtype) != convert_dtype(data.dtype):
return data.astype(convert_dtype(dtype))
return data
return _handle_diff_place_dtype(data, dtype, place, stop_gradient)
elif isinstance(data, (core.Tensor, core.LoDTensor)):
# convert LoDTensor to VarBase first, and then process it as input VarBase
data = paddle.Tensor(data)
return _handle_diff_place_dtype(data, dtype, place, stop_gradient)
else:
raise TypeError(
"Can't constructs a 'paddle.Tensor' with data type {}, data type must be scalar|list|tuple|numpy.ndarray|paddle.Tensor".
Expand Down