Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
d98e1182
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看板
未验证
提交
d98e1182
编写于
5月 13, 2020
作者:
D
danleifeng
提交者:
GitHub
5月 13, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix check and error message for flatten hash is_empty op (#24434)
fix check info for flatten hash is_empty op; test=develop
上级
30efee33
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
130 addition
and
34 deletion
+130
-34
paddle/fluid/operators/flatten_op.cc
paddle/fluid/operators/flatten_op.cc
+17
-18
paddle/fluid/operators/hash_op.cc
paddle/fluid/operators/hash_op.cc
+4
-5
paddle/fluid/operators/is_empty_op.cc
paddle/fluid/operators/is_empty_op.cc
+2
-4
python/paddle/fluid/layers/control_flow.py
python/paddle/fluid/layers/control_flow.py
+6
-6
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+5
-0
python/paddle/fluid/tests/unittests/test_flatten2_op.py
python/paddle/fluid/tests/unittests/test_flatten2_op.py
+21
-1
python/paddle/fluid/tests/unittests/test_hash_op.py
python/paddle/fluid/tests/unittests/test_hash_op.py
+37
-0
python/paddle/fluid/tests/unittests/test_is_empty_op.py
python/paddle/fluid/tests/unittests/test_is_empty_op.py
+38
-0
未找到文件。
paddle/fluid/operators/flatten_op.cc
浏览文件 @
d98e1182
...
...
@@ -29,17 +29,17 @@ class FlattenOp : public framework::OperatorWithKernel {
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE_EQ
(
ctx
->
HasInput
(
"X"
),
true
,
"Input (X) of Flatten op should not be null."
);
PADDLE_ENFORCE_EQ
(
ctx
->
HasOutput
(
"Out"
),
true
,
"Output (Output) of Flatten op should not be null."
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"X"
),
"Input"
,
"X"
,
"Flatten"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"Flatten"
);
const
auto
&
axis
=
ctx
->
Attrs
().
Get
<
int
>
(
"axis"
);
const
auto
&
in_dims
=
ctx
->
GetInputDim
(
"X"
);
PADDLE_ENFORCE_GE
(
axis
,
0
,
"The axis should be greater than or equal to 0."
);
platform
::
errors
::
InvalidArgument
(
"The axis should be greater than or equal to 0."
));
PADDLE_ENFORCE_LE
(
axis
,
in_dims
.
size
(),
"The axis should be less than or equal to input tensor's rank."
);
platform
::
errors
::
InvalidArgument
(
"The axis should be less than or equal to input tensor's rank."
));
const
auto
&
out_dims
=
GetOutputShape
(
axis
,
in_dims
);
ctx
->
SetOutputDim
(
"Out"
,
framework
::
make_ddim
(
out_dims
));
...
...
@@ -161,17 +161,17 @@ class Flatten2Op : public framework::OperatorWithKernel {
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE_EQ
(
ctx
->
HasInput
(
"X"
),
true
,
"Input (X) of Flatten op should not be null."
);
PADDLE_ENFORCE_EQ
(
ctx
->
HasOutput
(
"Out"
),
true
,
"Output (Output) of Flatten op should not be null."
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"X"
),
"Input"
,
"X"
,
"Flatten2"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"Flatten2"
);
const
auto
&
axis
=
ctx
->
Attrs
().
Get
<
int
>
(
"axis"
);
const
auto
&
in_dims
=
ctx
->
GetInputDim
(
"X"
);
PADDLE_ENFORCE_GE
(
axis
,
0
,
"The axis should be greater than or equal to 0."
);
platform
::
errors
::
InvalidArgument
(
"The axis should be greater than or equal to 0."
));
PADDLE_ENFORCE_LE
(
axis
,
in_dims
.
size
(),
"The axis should be less than or equal to input tensor's rank."
);
platform
::
errors
::
InvalidArgument
(
"The axis should be less than or equal to input tensor's rank"
));
const
auto
&
out_dims
=
FlattenOp
::
GetOutputShape
(
axis
,
in_dims
);
ctx
->
SetOutputDim
(
"Out"
,
framework
::
make_ddim
(
out_dims
));
...
...
@@ -181,8 +181,7 @@ class Flatten2Op : public framework::OperatorWithKernel {
ctx
->
ShareLoD
(
"X"
,
"Out"
);
}
PADDLE_ENFORCE_EQ
(
ctx
->
HasOutput
(
"XShape"
),
true
,
"Output (XShape) of Flatten op should not be null."
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"XShape"
),
"Output"
,
"XShape"
,
"Flatten2"
);
std
::
vector
<
int64_t
>
xshape_dims
(
in_dims
.
size
()
+
1
);
xshape_dims
[
0
]
=
0
;
for
(
int
i
=
0
;
i
<
in_dims
.
size
();
++
i
)
{
...
...
@@ -223,10 +222,10 @@ class Flatten2GradOp : public framework::OperatorWithKernel {
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
context
)
const
override
{
PADDLE_ENFORCE_EQ
(
context
->
HasInput
(
"XShape"
),
true
,
"Input(XShape) shouldn't be null.
"
);
PADDLE_ENFORCE_EQ
(
context
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
true
,
"Input(Out@GRAD) shouldn't be null.
"
);
OP_INOUT_CHECK
(
context
->
HasInput
(
"XShape"
),
"Input"
,
"XShape"
,
"Flatten2Grad
"
);
OP_INOUT_CHECK
(
context
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
"Input"
,
framework
::
GradVarName
(
"Out"
),
"Flatten2Grad
"
);
auto
xshape_dims
=
context
->
GetInputDim
(
"XShape"
);
auto
x_dims
=
framework
::
slice_ddim
(
xshape_dims
,
1
,
xshape_dims
.
size
());
context
->
SetOutputDim
(
framework
::
GradVarName
(
"X"
),
x_dims
);
...
...
paddle/fluid/operators/hash_op.cc
浏览文件 @
d98e1182
...
...
@@ -26,14 +26,13 @@ class HashOp : public framework::OperatorWithKernel {
:
OperatorWithKernel
(
type
,
inputs
,
outputs
,
attrs
)
{}
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) of HashOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of HashOp should not be null."
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"X"
),
"Input"
,
"X"
,
"Hash"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"Hash"
);
auto
dims
=
ctx
->
GetInputDim
(
"X"
);
PADDLE_ENFORCE_EQ
(
dims
.
size
(),
2UL
,
"The input of hash_op's dimensions must be 2"
);
platform
::
errors
::
InvalidArgument
(
"The input of hash_op's dimensions must be 2"
));
std
::
vector
<
int64_t
>
out_dims
;
int
num_hash
=
ctx
->
Attrs
().
Get
<
int
>
(
"num_hash"
);
HashOutputSize
(
dims
,
out_dims
,
num_hash
);
...
...
paddle/fluid/operators/is_empty_op.cc
浏览文件 @
d98e1182
...
...
@@ -25,10 +25,8 @@ class IsEmptyOp : public framework::OperatorWithKernel {
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) of IsEmptyOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of IsEmptyOp should not be null."
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"X"
),
"Input"
,
"X"
,
"IsEmpty"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"IsEmpty"
);
ctx
->
SetOutputDim
(
"Out"
,
{
1
});
}
...
...
python/paddle/fluid/layers/control_flow.py
浏览文件 @
d98e1182
...
...
@@ -26,7 +26,7 @@ import numpy
import
warnings
import
six
from
functools
import
reduce
,
partial
from
..data_feeder
import
convert_dtype
,
check_variable_and_dtype
,
check_type
from
..data_feeder
import
convert_dtype
,
check_variable_and_dtype
,
check_type
,
check_dtype
from
...
import
compat
as
cpt
from
..backward
import
_infer_var_data_type_shape_
...
...
@@ -3725,15 +3725,15 @@ def is_empty(x, cond=None):
# fluid.layers.is_empty(x=input, cond=res)
"""
check_variable_and_dtype
(
x
,
'x'
,
[
'float32'
,
'float64'
,
'int32'
,
'int64'
],
'is_empty'
)
check_type
(
cond
,
'cond'
,
(
Variable
,
type
(
None
)),
'is_empty'
)
helper
=
LayerHelper
(
"is_empty"
,
**
locals
())
if
cond
is
None
:
cond
=
helper
.
create_variable_for_type_inference
(
dtype
=
'bool'
)
cond
.
stop_gradient
=
True
elif
not
isinstance
(
cond
,
Variable
):
raise
TypeError
(
"cond takes a variable"
)
elif
cond
.
dtype
!=
'bool'
:
raise
TypeError
(
"The data type of cond must be bool"
)
else
:
check_dtype
(
cond
.
dtype
,
'cond'
,
[
'bool'
],
'is_empty'
)
helper
.
append_op
(
type
=
'is_empty'
,
inputs
=
{
'X'
:
[
x
]},
outputs
=
{
'Out'
:
[
cond
]})
return
cond
python/paddle/fluid/layers/nn.py
浏览文件 @
d98e1182
...
...
@@ -9628,6 +9628,8 @@ def flatten(x, axis=1, name=None):
out = fluid.layers.flatten(x=x, axis=2)
# out shape is [16, 3]
"""
check_variable_and_dtype(
x, 'x', ['float32', 'float64', 'int8', 'int32', 'int64'], 'flatten')
helper = LayerHelper('flatten', **locals())
if not (isinstance(x, Variable)):
...
...
@@ -12466,6 +12468,9 @@ def hash(input, hash_size, num_hash=1, name=None):
# [386]
# [901]]]
"""
check_variable_and_dtype(input, 'input', ['int32', 'int64'], 'hash')
check_type(hash_size, 'hash_size', ['int32', 'int64'], 'hash')
check_type(num_hash, 'num_hash', ['int32', 'int64'], 'hash')
helper = LayerHelper('hash', **locals())
out = helper.create_variable_for_type_inference(
helper.input_dtype(), stop_gradient=True)
...
...
python/paddle/fluid/tests/unittests/test_flatten2_op.py
浏览文件 @
d98e1182
...
...
@@ -16,7 +16,7 @@ from __future__ import print_function
import
unittest
import
numpy
as
np
import
paddle.fluid
as
fluid
from
op_test
import
OpTest
...
...
@@ -69,5 +69,25 @@ class TestFlattenOpSixDims(TestFlattenOp):
self
.
new_shape
=
(
36
,
16
)
class
TestFlatten2OpError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
input_data
=
np
.
random
.
random
((
3
,
2
,
4
,
5
)).
astype
(
"float64"
)
def
test_Variable
():
# the input type must be Variable
fluid
.
layers
.
flatten
(
input_data
,
axis
=
1
)
self
.
assertRaises
(
TypeError
,
test_Variable
)
def
test_type
():
# dtype must be float32, float64, int8, int32, int64.
x2
=
fluid
.
layers
.
data
(
name
=
'x2'
,
shape
=
[
3
,
2
,
4
,
5
],
dtype
=
'float16'
)
fluid
.
layers
.
flatten
(
x2
,
axis
=
1
)
self
.
assertRaises
(
TypeError
,
test_type
)
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_hash_op.py
浏览文件 @
d98e1182
...
...
@@ -15,6 +15,7 @@
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
import
paddle.fluid
as
fluid
class
TestHashOp
(
OpTest
):
...
...
@@ -102,5 +103,41 @@ class TestHashOp3(TestHashOp):
self
.
check_output
()
class
TestHashOpError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
input_data
=
np
.
random
.
randint
(
0
,
10
,
(
8
,
1
)).
astype
(
"int32"
)
def
test_Variable
():
# the input type must be Variable
fluid
.
layers
.
hash
(
input
=
input_data
,
hash_size
=
2
**
32
)
self
.
assertRaises
(
TypeError
,
test_Variable
)
def
test_type
():
# dtype must be int32, int64.
x2
=
fluid
.
layers
.
data
(
name
=
'x2'
,
shape
=
[
1
],
dtype
=
"float32"
,
lod_level
=
1
)
fluid
.
layers
.
hash
(
input
=
x2
,
hash_size
=
2
**
32
)
self
.
assertRaises
(
TypeError
,
test_type
)
def
test_hash_size_type
():
# hash_size dtype must be int32, int64.
x3
=
fluid
.
layers
.
data
(
name
=
'x3'
,
shape
=
[
1
],
dtype
=
"int32"
,
lod_level
=
1
)
fluid
.
layers
.
hash
(
input
=
x3
,
hash_size
=
1024.5
)
self
.
assertRaises
(
TypeError
,
test_hash_size_type
)
def
test_num_hash_type
():
# num_hash dtype must be int32, int64.
x4
=
fluid
.
layers
.
data
(
name
=
'x4'
,
shape
=
[
1
],
dtype
=
"int32"
,
lod_level
=
1
)
fluid
.
layers
.
hash
(
input
=
x4
,
hash_size
=
2
**
32
,
num_hash
=
2.5
)
self
.
assertRaises
(
TypeError
,
test_num_hash_type
)
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_is_empty_op.py
浏览文件 @
d98e1182
...
...
@@ -17,6 +17,7 @@ from __future__ import print_function
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
import
paddle.fluid
as
fluid
class
TestEmpty
(
OpTest
):
...
...
@@ -36,5 +37,42 @@ class TestNotEmpty(TestEmpty):
self
.
outputs
=
{
'Out'
:
np
.
array
([
True
])}
class
TestIsEmptyOpError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
input_data
=
np
.
random
.
random
((
3
,
2
)).
astype
(
"float64"
)
def
test_Variable
():
# the input type must be Variable
fluid
.
layers
.
is_empty
(
x
=
input_data
)
self
.
assertRaises
(
TypeError
,
test_Variable
)
def
test_cond_Variable
():
# cond type must be Variable or None
x2
=
fluid
.
layers
.
data
(
name
=
"x2"
,
shape
=
[
3
,
2
],
dtype
=
"float32"
)
cond_data
=
np
.
random
.
random
((
3
,
2
)).
astype
(
"float32"
)
fluid
.
layers
.
is_empty
(
x
=
x2
,
cond
=
cond_data
)
self
.
assertRaises
(
TypeError
,
test_cond_Variable
)
def
test_type
():
# dtype must be float32, float64, int32, int64
x3
=
fluid
.
layers
.
data
(
name
=
"x3"
,
shape
=
[
4
,
32
,
32
],
dtype
=
"bool"
)
res
=
fluid
.
layers
.
is_empty
(
x
=
x3
)
self
.
assertRaises
(
TypeError
,
test_type
)
def
test_cond_type
():
# cond dtype must be bool.
x4
=
fluid
.
layers
.
data
(
name
=
"x4"
,
shape
=
[
3
,
2
],
dtype
=
"float32"
)
cond
=
fluid
.
layers
.
data
(
name
=
"cond"
,
shape
=
[
1
],
dtype
=
"float32"
)
fluid
.
layers
.
is_empty
(
x
=
x4
,
cond
=
cond
)
self
.
assertRaises
(
TypeError
,
test_cond_type
)
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录