Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
4617c1b2
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2305
Star
20932
Fork
5423
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
4617c1b2
编写于
3月 01, 2022
作者:
zhouweiwei2014
提交者:
GitHub
3月 01, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix bug of paddle.to_tensor and paddle.moveaxis (#39662)
* fix bug of paddle.to_tensor and paddle.moveaxis * fix CI
上级
69ab2700
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
31 addition
and
19 deletion
+31
-19
python/paddle/fluid/tests/unittests/test_transpose_op.py
python/paddle/fluid/tests/unittests/test_transpose_op.py
+8
-0
python/paddle/fluid/tests/unittests/test_var_base.py
python/paddle/fluid/tests/unittests/test_var_base.py
+4
-0
python/paddle/tensor/creation.py
python/paddle/tensor/creation.py
+15
-16
python/paddle/tensor/manipulation.py
python/paddle/tensor/manipulation.py
+4
-3
未找到文件。
python/paddle/fluid/tests/unittests/test_transpose_op.py
浏览文件 @
4617c1b2
...
...
@@ -423,6 +423,14 @@ class TestMoveAxis(unittest.TestCase):
self
.
assertEqual
(
np
.
array_equal
(
out
.
numpy
(),
expected
),
True
)
paddle
.
enable_static
()
def
test_moveaxis3
(
self
):
paddle
.
disable_static
()
x
=
paddle
.
to_tensor
(
[[
1
+
1j
,
-
1
-
1j
],
[
1
+
1j
,
-
1
-
1j
],
[
1
+
1j
,
-
1
-
1j
]])
out
=
x
.
moveaxis
(
0
,
1
)
self
.
assertEqual
(
out
.
shape
,
[
2
,
3
])
paddle
.
enable_static
()
def
test_error
(
self
):
x
=
paddle
.
randn
([
2
,
3
,
4
,
5
])
# src must have the same number with dst
...
...
python/paddle/fluid/tests/unittests/test_var_base.py
浏览文件 @
4617c1b2
...
...
@@ -51,6 +51,10 @@ class TestVarBase(unittest.TestCase):
np
.
array_equal
(
x
.
numpy
(),
np
.
array
([
1.2
],
'float16'
)))
self
.
assertEqual
(
x
.
dtype
,
core
.
VarDesc
.
VarType
.
FP16
)
# set_default_dtype take effect on int
x
=
paddle
.
to_tensor
(
1
,
place
=
place
)
self
.
assertTrue
(
x
.
dtype
,
core
.
VarDesc
.
VarType
.
INT64
)
# set_default_dtype take effect on float
x
=
paddle
.
to_tensor
(
1.2
,
place
=
place
,
stop_gradient
=
False
)
self
.
assertTrue
(
...
...
python/paddle/tensor/creation.py
浏览文件 @
4617c1b2
...
...
@@ -110,12 +110,6 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
"'place' must be any of paddle.Place, paddle.CPUPlace, paddle.CUDAPinnedPlace, paddle.CUDAPlace, paddle.NPUPlace, paddle.XPUPlace, paddle.CustomPlace"
)
#Todo(zhouwei): Support allocate tensor on any other specified card
if
isinstance
(
place
,
core
.
CUDAPlace
)
and
isinstance
(
_current_expected_place
(),
core
.
CUDAPlace
)
and
place
.
_get_device_id
(
)
!=
_current_expected_place
().
_get_device_id
():
place
=
_current_expected_place
()
if
not
isinstance
(
data
,
np
.
ndarray
):
def
_handle_dtype
(
data
,
dtype
):
...
...
@@ -139,7 +133,7 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
data
.
stop_gradient
=
stop_gradient
return
data
elif
isinstance
(
data
,
(
core
.
LoDTensor
,
core
.
Tensor
)):
#
Note(zhouwei25):
should't expose it to users, just for internal use.
# 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
)
...
...
@@ -152,15 +146,20 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
raise
TypeError
(
"Can't constructs a 'paddle.Tensor' with data type {}, data type must be scalar|list|tuple|numpy.ndarray|paddle.Tensor"
.
format
(
type
(
data
)))
if
not
dtype
and
data
.
dtype
in
[
'float16'
,
'float32'
,
'float64'
,
'complex64'
,
'complex128'
]:
default_type
=
paddle
.
get_default_dtype
()
if
np
.
iscomplexobj
(
data
):
default_type
=
'complex64'
if
default_type
in
[
'float16'
,
'float32'
]
else
'complex128'
data
=
data
.
astype
(
default_type
)
if
not
dtype
:
if
data
.
dtype
in
[
'float16'
,
'float32'
,
'float64'
,
'complex64'
,
'complex128'
]:
default_type
=
paddle
.
get_default_dtype
()
if
np
.
iscomplexobj
(
data
):
default_type
=
'complex64'
if
default_type
in
[
'float16'
,
'float32'
]
else
'complex128'
data
=
data
.
astype
(
default_type
)
# Windows default type is 'int32', while Linux/Mac is 'int64'. Unify they.
if
data
.
dtype
in
[
'int32'
]:
default_type
=
"int64"
data
=
data
.
astype
(
default_type
)
if
dtype
and
convert_dtype
(
dtype
)
!=
data
.
dtype
:
data
=
data
.
astype
(
convert_dtype
(
dtype
))
...
...
python/paddle/tensor/manipulation.py
浏览文件 @
4617c1b2
...
...
@@ -2737,9 +2737,10 @@ def moveaxis(x, source, destination, name=None):
out
,
_
=
_C_ops
.
transpose2
(
x
,
'axis'
,
perm
)
return
out
check_variable_and_dtype
(
x
,
'x'
,
[
'bool'
,
'float16'
,
'float32'
,
'float64'
,
'int32'
,
'int64'
],
'moveaxis'
)
check_variable_and_dtype
(
x
,
'x'
,
[
'bool'
,
'float16'
,
'float32'
,
'float64'
,
'int32'
,
'int64'
,
'complex64'
,
'complex128'
],
'moveaxis'
)
helper
=
LayerHelper
(
'moveaxis'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录