Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
ff062a43
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
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看板
未验证
提交
ff062a43
编写于
8月 05, 2021
作者:
G
Guoxia Wang
提交者:
GitHub
8月 05, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix output dtype for paddle.sum (#34313)
* support bool dtype for paddle.sum
上级
a842828a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
31 addition
and
58 deletion
+31
-58
python/paddle/fluid/tests/unittests/dygraph_to_static/test_break_continue.py
.../tests/unittests/dygraph_to_static/test_break_continue.py
+1
-1
python/paddle/fluid/tests/unittests/test_reduce_op.py
python/paddle/fluid/tests/unittests/test_reduce_op.py
+12
-31
python/paddle/tensor/math.py
python/paddle/tensor/math.py
+18
-26
未找到文件。
python/paddle/fluid/tests/unittests/dygraph_to_static/test_break_continue.py
浏览文件 @
ff062a43
...
...
@@ -184,7 +184,7 @@ def test_optim_break_in_while(x):
class
TestContinueInFor
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
input
=
np
.
zeros
((
1
)).
astype
(
'int
32
'
)
self
.
input
=
np
.
zeros
((
1
)).
astype
(
'int
64
'
)
self
.
place
=
fluid
.
CUDAPlace
(
0
)
if
fluid
.
is_compiled_with_cuda
(
)
else
fluid
.
CPUPlace
()
self
.
init_dygraph_func
()
...
...
python/paddle/fluid/tests/unittests/test_reduce_op.py
浏览文件 @
ff062a43
...
...
@@ -748,37 +748,6 @@ class TestReduceSumOpError(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
reduce_sum
,
x2
)
class
API_TestSumOpError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
def
test_dtype1
():
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
data
=
fluid
.
data
(
name
=
"data"
,
shape
=
[
10
],
dtype
=
"float64"
)
paddle
.
sum
(
data
,
dtype
=
"float32"
)
self
.
assertRaises
(
ValueError
,
test_dtype1
)
def
test_dtype2
():
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
data
=
fluid
.
data
(
name
=
"data"
,
shape
=
[
10
],
dtype
=
"int64"
)
paddle
.
sum
(
data
,
dtype
=
"int32"
)
self
.
assertRaises
(
ValueError
,
test_dtype2
)
def
test_dtype3
():
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
data
=
fluid
.
data
(
name
=
"data"
,
shape
=
[
10
],
dtype
=
"float64"
)
paddle
.
sum
(
data
,
dtype
=
"int32"
)
self
.
assertRaises
(
ValueError
,
test_dtype3
)
def
test_type
():
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
data
=
fluid
.
data
(
name
=
"data"
,
shape
=
[
10
],
dtype
=
"int32"
)
paddle
.
sum
(
data
,
dtype
=
"bool"
)
self
.
assertRaises
(
TypeError
,
test_type
)
class
API_TestSumOp
(
unittest
.
TestCase
):
def
run_static
(
self
,
shape
,
...
...
@@ -805,14 +774,26 @@ class API_TestSumOp(unittest.TestCase):
shape
=
[
10
,
10
]
axis
=
1
self
.
run_static
(
shape
,
"bool"
,
axis
,
attr_dtype
=
None
)
self
.
run_static
(
shape
,
"bool"
,
axis
,
attr_dtype
=
"int32"
)
self
.
run_static
(
shape
,
"bool"
,
axis
,
attr_dtype
=
"int64"
)
self
.
run_static
(
shape
,
"int32"
,
axis
,
attr_dtype
=
None
)
self
.
run_static
(
shape
,
"int32"
,
axis
,
attr_dtype
=
"int32"
)
self
.
run_static
(
shape
,
"int32"
,
axis
,
attr_dtype
=
"int64"
)
self
.
run_static
(
shape
,
"int64"
,
axis
,
attr_dtype
=
None
)
self
.
run_static
(
shape
,
"int64"
,
axis
,
attr_dtype
=
"int64"
)
self
.
run_static
(
shape
,
"int64"
,
axis
,
attr_dtype
=
"int32"
)
self
.
run_static
(
shape
,
"float32"
,
axis
,
attr_dtype
=
None
)
self
.
run_static
(
shape
,
"float32"
,
axis
,
attr_dtype
=
"float32"
)
self
.
run_static
(
shape
,
"float32"
,
axis
,
attr_dtype
=
"float64"
)
self
.
run_static
(
shape
,
"float64"
,
axis
,
attr_dtype
=
None
)
self
.
run_static
(
shape
,
"float64"
,
axis
,
attr_dtype
=
"float32"
)
self
.
run_static
(
shape
,
"float64"
,
axis
,
attr_dtype
=
"float64"
)
shape
=
[
5
,
5
,
5
]
self
.
run_static
(
shape
,
"int32"
,
(
0
,
1
),
attr_dtype
=
"int32"
)
self
.
run_static
(
...
...
python/paddle/tensor/math.py
浏览文件 @
ff062a43
...
...
@@ -716,13 +716,15 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None):
else
:
reduce_all_flag
=
False
dtype_flag
=
False
if
dtype
is
not
None
:
if
dtype
in
[
'float64'
,
'int64'
]:
if
(
convert_dtype
(
x
.
dtype
)
==
"float32"
and
dtype
==
"float64"
)
or
\
(
convert_dtype
(
x
.
dtype
)
==
"int32"
and
dtype
==
"int64"
):
dtype_flag
=
True
def
get_dtype
(
x
,
dtype
):
if
dtype
is
not
None
:
return
(
True
,
dtype
)
src_type
=
convert_dtype
(
x
.
dtype
)
if
src_type
in
[
'bool'
,
'int32'
,
'int64'
]:
return
(
True
,
'int64'
)
return
(
False
,
src_type
)
dtype_flag
,
dtype
=
get_dtype
(
x
,
dtype
)
if
in_dygraph_mode
():
axis
=
axis
if
axis
!=
None
and
axis
!=
[]
else
[
0
]
if
dtype_flag
:
...
...
@@ -740,27 +742,17 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None):
'reduce_all'
:
reduce_all_flag
}
if
dtype
is
not
None
:
if
dtype
in
[
'float64'
,
'int64'
]:
if
(
convert_dtype
(
x
.
dtype
)
==
"float32"
and
dtype
==
"float64"
)
or
\
(
convert_dtype
(
x
.
dtype
)
==
"int32"
and
dtype
==
"int64"
):
attrs
.
update
({
'in_dtype'
:
x
.
dtype
,
'out_dtype'
:
convert_np_dtype_to_dtype_
(
dtype
)
})
if
dtype_flag
:
attrs
.
update
({
'in_dtype'
:
x
.
dtype
,
'out_dtype'
:
convert_np_dtype_to_dtype_
(
dtype
)
})
check_variable_and_dtype
(
x
,
'x'
,
[
'float32'
,
'float64'
,
'int32'
,
'int64'
],
'sum'
)
if
dtype
is
not
None
:
check_dtype
(
dtype
,
'dtype'
,
[
'float32'
,
'float64'
,
'int32'
,
'int64'
],
'sum'
)
x_dtype
=
convert_dtype
(
x
.
dtype
)
if
(
x_dtype
==
"float64"
and
dtype
in
[
"float32"
,
"int32"
])
or
\
(
x_dtype
==
"int64"
and
dtype
==
"int32"
):
raise
ValueError
(
"The input(x)'s dtype is {} but the attr(dtype) of sum is {}, "
"which may cause data type overflows. Please reset attr(dtype) of sum."
.
format
(
x_dtype
,
dtype
))
x
,
'x'
,
[
'bool'
,
'float16'
,
'float32'
,
'float64'
,
'int32'
,
'int64'
,
'complex64'
,
'complex128'
,
u
'bool'
,
u
'float16'
,
u
'float32'
,
u
'float64'
,
u
'int32'
,
u
'int64'
,
u
'complex64'
,
u
'complex128'
],
'sum'
)
check_type
(
axis
,
'axis'
,
(
int
,
list
,
tuple
,
type
(
None
)),
'sum'
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录