Skip to content
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
2 changes: 1 addition & 1 deletion paddle/fluid/pybind/imperative.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static void InitVarBaseFromNumpyWithArgDefault(imperative::VarBase *self,
}

static void InitVarBaseFromTensorWithArgDefault(
imperative::VarBase *self, const framework::LoDTensor &tensor) {
imperative::VarBase *self, const framework::Tensor &tensor) {
VLOG(4) << "Init VarBase";
auto place = imperative::GetCurrentTracer()->ExpectedPlace();
new (self) imperative::VarBase(
Expand Down
19 changes: 18 additions & 1 deletion python/paddle/fluid/tests/unittests/test_var_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ def _test_place(place):

x = paddle.to_tensor(1, dtype='uint8')
self.assertEqual(x.item(), 1)
print(type(x.item()))
self.assertTrue(isinstance(x.item(), int))

x = paddle.to_tensor(1, dtype='int8')
Expand All @@ -203,6 +202,24 @@ def _test_place(place):
self.assertEqual(x.item(), 1 + 1j)
self.assertTrue(isinstance(x.item(), complex))

numpy_array = np.random.randn(3, 4)
# covert core.LoDTensor to paddle.Tensor
lod_tensor = paddle.fluid.core.LoDTensor()
place = paddle.fluid.framework._current_expected_place()
lod_tensor.set(numpy_array, place)
x = paddle.to_tensor(lod_tensor)
self.assertTrue(np.array_equal(x.numpy(), numpy_array))
self.assertEqual(x.type, core.VarDesc.VarType.LOD_TENSOR)
self.assertEqual(str(x.place), str(place))

# covert core.Tensor to paddle.Tensor
x = paddle.to_tensor(numpy_array)
dlpack = x.value().get_tensor()._to_dlpack()
tensor_from_dlpack = paddle.fluid.core.from_dlpack(dlpack)
x = paddle.to_tensor(tensor_from_dlpack)
self.assertTrue(np.array_equal(x.numpy(), numpy_array))
self.assertEqual(x.type, core.VarDesc.VarType.LOD_TENSOR)

with self.assertRaises(ValueError):
paddle.randn([3, 2, 2]).item()
with self.assertRaises(ValueError):
Expand Down
7 changes: 4 additions & 3 deletions python/paddle/tensor/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ def _handle_dtype(data, dtype):
data = data._copy_to(place, False)
ata = _handle_dtype(data, dtype)
data.stop_gradient = stop_gradient
elif isinstance(data, core.LoDTensor):
# convert LoDTensor to VarBase first
# Currenly, LoDTensor does no copy when places are same
elif isinstance(data, (core.LoDTensor, core.Tensor)):
# Note(zhouwei25): should't expose it to users, just for internal use.
# convert core.Tensor/core.LoDTensor to VarBase first
# Currenly, there is no copy when places are same
data = paddle.Tensor(data)
if not data.place._equals(place):
data = data._copy_to(place, False)
Expand Down