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

[2.0API]support set_default_dtype for to_tensor #26432

Merged
merged 3 commits into from
Aug 28, 2020
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
24 changes: 24 additions & 0 deletions python/paddle/fluid/tests/unittests/test_var_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ def setUp(self):
def test_to_tensor(self):
def _test_place(place):
with fluid.dygraph.guard():
paddle.set_default_dtype('float32')
x = paddle.to_tensor(1, place=place, stop_gradient=False)
self.assertTrue(np.array_equal(x.numpy(), [1]))
self.assertNotEqual(x.dtype, core.VarDesc.VarType.FP32)

x = paddle.to_tensor(1.2, place=place, stop_gradient=False)
self.assertTrue(
np.array_equal(x.numpy(), np.array([1.2]).astype(
'float32')))
self.assertEqual(x.dtype, core.VarDesc.VarType.FP32)

x = paddle.to_tensor(1 + 2j, place=place, stop_gradient=False)
self.assertTrue(np.array_equal(x.numpy(), [1 + 2j]))
self.assertEqual(x.dtype, 'complex64')

paddle.set_default_dtype('float64')
x = paddle.to_tensor(1.2, place=place, stop_gradient=False)
self.assertTrue(np.array_equal(x.numpy(), [1.2]))
self.assertEqual(x.dtype, core.VarDesc.VarType.FP64)

x = paddle.to_tensor(1 + 2j, place=place, stop_gradient=False)
self.assertTrue(np.array_equal(x.numpy(), [1 + 2j]))
self.assertEqual(x.dtype, 'complex128')

x = paddle.to_tensor(
1, dtype='float32', place=place, stop_gradient=False)
self.assertTrue(np.array_equal(x.numpy(), [1.]))
Expand Down
33 changes: 21 additions & 12 deletions python/paddle/tensor/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,30 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
Args:
data(scalar|tuple|list|ndarray|Tensor|ComplexTensor): Initial data for the tensor.
Can be a scalar, list, tuple, numpy\.ndarray, paddle\.Tensor, paddle\.ComplexTensor.
dtype(str, optional): The desired data type of returned tensor. Can be 'bool' , 'float16' ,
dtype(str|np.dtype, optional): The desired data type of returned tensor. Can be 'bool' , 'float16' ,
'float32' , 'float64' , 'int8' , 'int16' , 'int32' , 'int64' , 'uint8'. And
'complex64' , 'complex128' only for ComplexTensor.
Default: None, infers data type from ``data`` .
'complex64' , 'complex128' only for ComplexTensor. Default: None, for float point number,
get type from ``get_default_type``, for other type, infers from ``data`` .
place(CPUPlace|CUDAPinnedPlace|CUDAPlace, optional): The place to allocate Tensor. Can be
CPUPlace, CUDAPinnedPlace, CUDAPlace. Default: None, means global place.
stop_gradient(bool, optional): Whether to block the gradient propagation of Autograd. Default: True.

Returns:
Tensor: A Tensor or ComplexTensor constructed from ``data``.
Tensor: A Tensor or ComplexTensor constructed from ``data`` .

Raises:
TypeError: If the data type of ``data`` is not scalar, list, tuple, numpy.ndarray, paddle.Tensor, paddle.ComplexTensor
ValueError: If ``data`` is tuple|list, it can't contain nested tuple|list with different lengths , such as: [[1, 2], [3, 4, 5]]
TypeError: If ``dtype`` is not bool, float16, float32, float64, int8, int16, int32, int64, uint8, complex64, complex128
ValueError: If ``place`` is not paddle.Place, paddle.CUDAPinnedPlace, paddle.CUDAPlace
ValueError: If ``place`` is not paddle.CPUPlace, paddle.CUDAPinnedPlace, paddle.CUDAPlace

Examples:

.. code-block:: python

import paddle
import numpy as np
paddle.enable_imperative()
paddle.disable_static()

type(paddle.to_tensor(1))
# <class 'paddle.Tensor'>
Expand Down Expand Up @@ -132,7 +132,7 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
# - dtype: double
# - data: [0.1 0.2 0.3 0.4]

type(paddle.to_tensor([[1+1j, 2], [3+2j, 4]]), , dtype='complex64')
type(paddle.to_tensor([[1+1j, 2], [3+2j, 4]]), dtype='complex64')
# <class 'paddle.ComplexTensor'>

paddle.to_tensor([[1+1j, 2], [3+2j, 4]], dtype='complex64')
Expand Down Expand Up @@ -189,19 +189,28 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
"Can't constructs a 'paddle.Tensor' with data type {}, data type must be scalar|list|tuple|numpy.ndarray|paddle.Tensor|paddle.ComplexTensor".
format(type(data)))

if dtype:
dtype = convert_dtype(dtype)
if dtype != data.dtype:
data = data.astype(dtype)

if not np.iscomplexobj(data):
if dtype:
dtype = convert_dtype(dtype)
elif data.dtype in ['float16', 'float32', 'float64']:
dtype = paddle.framework.get_default_dtype()
if dtype and dtype != data.dtype:
data = data.astype(dtype)
return paddle.Tensor(
value=data,
place=place,
persistable=False,
zero_copy=True,
stop_gradient=stop_gradient)
else:
if dtype:
dtype = convert_dtype(dtype)
else:
dtype = paddle.framework.get_default_dtype()
dtype = 'complex64' if dtype in ['float16', 'float32'
] else 'complex128'
if dtype != data.dtype:
data = data.astype(dtype)
name = unique_name.generate('generated_tensor')
real_tensor = paddle.Tensor(
value=data.real,
Expand Down
1 change: 1 addition & 0 deletions tools/wlist.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
"prroi_pool"
],
"wlist_temp":[
"to_tensor",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要加到白名单里。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要加到白名单里。

现在示例代码不能跑GPU的,但这个示例代码要有GPU的,所以只能先加白名单,可以看下示例代码,本地跑了没问题

"ChunkEvaluator",
"EditDistance",
"ErrorClipByValue",
Expand Down