Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
54427b80
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
54427b80
编写于
9月 04, 2020
作者:
L
liym27
提交者:
GitHub
9月 04, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Api2.0] sum: bug fix - support attr(dtype) is float32 or int32 and add ValueError (#26946)
上级
5a48952a
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
53 addition
and
61 deletion
+53
-61
python/paddle/fluid/tests/unittests/test_reduce_op.py
python/paddle/fluid/tests/unittests/test_reduce_op.py
+40
-56
python/paddle/tensor/math.py
python/paddle/tensor/math.py
+13
-5
未找到文件。
python/paddle/fluid/tests/unittests/test_reduce_op.py
浏览文件 @
54427b80
...
...
@@ -475,87 +475,71 @@ 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
=
"float
32
"
)
paddle
.
sum
(
data
,
dtype
=
"
in
t32"
)
data
=
fluid
.
data
(
name
=
"data"
,
shape
=
[
10
],
dtype
=
"float
64
"
)
paddle
.
sum
(
data
,
dtype
=
"
floa
t32"
)
self
.
assertRaises
(
ValueError
,
test_dtype1
)
def
test_dtype2
():
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
data
=
fluid
.
data
(
name
=
"data"
,
shape
=
[
10
],
dtype
=
"
float32
"
)
paddle
.
sum
(
data
,
dtype
=
"
floa
t32"
)
data
=
fluid
.
data
(
name
=
"data"
,
shape
=
[
10
],
dtype
=
"
int64
"
)
paddle
.
sum
(
data
,
dtype
=
"
in
t32"
)
self
.
assertRaises
(
ValueError
,
test_dtype2
)
def
test_dtype3
():
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
data
=
fluid
.
data
(
name
=
"data"
,
shape
=
[
10
],
dtype
=
"
int32
"
)
paddle
.
sum
(
data
,
dtype
=
"
bool
"
)
data
=
fluid
.
data
(
name
=
"data"
,
shape
=
[
10
],
dtype
=
"
float64
"
)
paddle
.
sum
(
data
,
dtype
=
"
int32
"
)
self
.
assertRaises
(
ValueError
,
test_dtype3
)
def
test_
dtype4
():
def
test_
type
():
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
data
=
fluid
.
data
(
name
=
"data"
,
shape
=
[
10
],
dtype
=
"int32"
)
paddle
.
sum
(
data
,
dtype
=
"
int32
"
)
paddle
.
sum
(
data
,
dtype
=
"
bool
"
)
self
.
assertRaises
(
ValueError
,
test_dtype3
)
self
.
assertRaises
(
TypeError
,
test_type
)
class
API_TestSumOp
(
unittest
.
TestCase
):
def
test_static
(
self
):
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
data
=
fluid
.
data
(
"data"
,
shape
=
[
10
,
10
],
dtype
=
"float32"
)
result_sum
=
paddle
.
sum
(
x
=
data
,
axis
=
1
,
dtype
=
"float64"
)
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
input_data
=
np
.
random
.
rand
(
10
,
10
).
astype
(
np
.
float32
)
res
,
=
exe
.
run
(
feed
=
{
"data"
:
input_data
},
fetch_list
=
[
result_sum
])
self
.
assertEqual
(
(
res
==
np
.
sum
(
input_data
.
astype
(
np
.
float64
),
axis
=
1
)).
all
(),
True
)
def
run_static
(
self
,
shape
,
x_dtype
,
attr_axis
,
attr_dtype
=
None
,
np_axis
=
None
):
if
np_axis
is
None
:
np_axis
=
attr_axis
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
data
=
fluid
.
data
(
"data"
,
shape
=
[
10
,
10
],
dtype
=
"int32"
)
result_sum
=
paddle
.
sum
(
x
=
data
,
axis
=
1
,
dtype
=
"int64"
)
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
input_data
=
np
.
random
.
randint
(
10
,
size
=
(
10
,
10
)).
astype
(
np
.
int32
)
res
,
=
exe
.
run
(
feed
=
{
"data"
:
input_data
},
fetch_list
=
[
result_sum
])
self
.
assertEqual
(
(
res
==
np
.
sum
(
input_data
.
astype
(
np
.
int64
),
axis
=
1
)).
all
(),
True
)
data
=
fluid
.
data
(
"data"
,
shape
=
shape
,
dtype
=
x_dtype
)
result_sum
=
paddle
.
sum
(
x
=
data
,
axis
=
attr_axis
,
dtype
=
attr_dtype
)
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
data
=
fluid
.
data
(
"data"
,
shape
=
[
10
,
10
],
dtype
=
"int32"
)
result_sum
=
paddle
.
sum
(
x
=
data
,
axis
=
1
)
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
input_data
=
np
.
random
.
randint
(
10
,
size
=
(
10
,
10
)).
astype
(
np
.
int32
)
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
input_data
=
np
.
random
.
rand
(
*
shape
).
astype
(
x_dtype
)
res
,
=
exe
.
run
(
feed
=
{
"data"
:
input_data
},
fetch_list
=
[
result_sum
])
self
.
assertEqual
((
res
==
np
.
sum
(
input_data
,
axis
=
1
)).
all
(),
True
)
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
data
=
fluid
.
data
(
"data"
,
shape
=
[
10
,
10
],
dtype
=
"int32"
)
result_sum
=
paddle
.
sum
(
x
=
data
,
axis
=
1
)
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
input_data
=
np
.
random
.
randint
(
10
,
size
=
(
10
,
10
)).
astype
(
np
.
int32
)
res
,
=
exe
.
run
(
feed
=
{
"data"
:
input_data
},
fetch_list
=
[
result_sum
])
self
.
assertEqual
((
res
==
np
.
sum
(
input_data
,
axis
=
1
)).
all
(),
True
)
self
.
assertTrue
(
np
.
allclose
(
res
,
np
.
sum
(
input_data
.
astype
(
attr_dtype
),
axis
=
np_axis
)))
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
input_data
=
np
.
random
.
randint
(
10
,
size
=
(
5
,
5
,
5
)).
astype
(
np
.
int32
)
data
=
fluid
.
data
(
"data"
,
shape
=
[
5
,
5
,
5
],
dtype
=
"int32"
)
sum1
=
paddle
.
sum
(
x
=
data
,
axis
=
[
0
,
1
])
sum2
=
paddle
.
sum
(
x
=
data
,
axis
=
())
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
res1
,
res2
=
exe
.
run
(
feed
=
{
"data"
:
input_data
},
fetch_list
=
[
sum1
,
sum2
])
self
.
assertEqual
((
res1
==
np
.
sum
(
input_data
,
axis
=
(
0
,
1
))).
all
(),
True
)
self
.
assertEqual
(
(
res2
==
np
.
sum
(
input_data
,
axis
=
(
0
,
1
,
2
))).
all
(),
True
)
def
test_static
(
self
):
shape
=
[
10
,
10
]
axis
=
1
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
,
"float32"
,
axis
,
attr_dtype
=
None
)
self
.
run_static
(
shape
,
"float32"
,
axis
,
attr_dtype
=
"float32"
)
self
.
run_static
(
shape
,
"float32"
,
axis
,
attr_dtype
=
"float64"
)
shape
=
[
5
,
5
,
5
]
self
.
run_static
(
shape
,
"int32"
,
(
0
,
1
),
attr_dtype
=
"int32"
)
self
.
run_static
(
shape
,
"int32"
,
(),
attr_dtype
=
"int32"
,
np_axis
=
(
0
,
1
,
2
))
def
test_dygraph
(
self
):
np_x
=
np
.
random
.
random
([
2
,
3
,
4
]).
astype
(
'int32'
)
...
...
python/paddle/tensor/math.py
浏览文件 @
54427b80
...
...
@@ -760,7 +760,8 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None):
it's data type is the same as `x`.
Raises:
ValueError: The :attr:`dtype` must be float64 or int64.
ValueError: If the data type of `x` is float64, :attr:`dtype` can not be float32 or int32.
ValueError: If the data type of `x` is int64, :attr:`dtype` can not be int32.
TypeError: The type of :attr:`axis` must be int, list or tuple.
Examples:
...
...
@@ -815,10 +816,6 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None):
'out_dtype'
:
convert_np_dtype_to_dtype_
(
dtype
)
})
dtype_flag
=
True
else
:
raise
ValueError
(
"The value of 'dtype' in sum op must be float64, int64, but received of {}"
.
format
(
dtype
))
if
in_dygraph_mode
():
axis
=
axis
if
axis
!=
None
and
axis
!=
[]
else
[
0
]
...
...
@@ -832,6 +829,17 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None):
'reduce_all'
,
reduce_all_flag
)
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
))
check_type
(
axis
,
'axis'
,
(
int
,
list
,
tuple
,
type
(
None
)),
'sum'
)
helper
=
LayerHelper
(
'sum'
,
**
locals
())
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录