Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OPTHREE
Paddle
提交
a85edddb
P
Paddle
项目概览
OPTHREE
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
a85edddb
编写于
5月 21, 2021
作者:
L
Leo Chen
提交者:
GitHub
5月 21, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
paddle.to_tensor supports LoDTensor (#33027)
上级
44668a7a
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
30 addition
and
7 deletion
+30
-7
python/paddle/fluid/tests/unittests/test_var_base.py
python/paddle/fluid/tests/unittests/test_var_base.py
+15
-0
python/paddle/tensor/creation.py
python/paddle/tensor/creation.py
+15
-7
未找到文件。
python/paddle/fluid/tests/unittests/test_var_base.py
浏览文件 @
a85edddb
...
...
@@ -248,6 +248,21 @@ class TestVarBase(unittest.TestCase):
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"
)
...
...
python/paddle/tensor/creation.py
浏览文件 @
a85edddb
...
...
@@ -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
)):
...
...
@@ -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"
.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录