Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
d04c8538
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
d04c8538
编写于
11月 10, 2017
作者:
Y
yangyaming
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refine .cc and .h, more unit test more readable.
上级
0d9ba3da
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
46 addition
and
32 deletion
+46
-32
paddle/operators/expand_op.cc
paddle/operators/expand_op.cc
+16
-11
paddle/operators/expand_op.h
paddle/operators/expand_op.h
+20
-11
python/paddle/v2/framework/tests/test_expand_op.py
python/paddle/v2/framework/tests/test_expand_op.py
+10
-10
未找到文件。
paddle/operators/expand_op.cc
浏览文件 @
d04c8538
...
...
@@ -25,13 +25,15 @@ class ExpandOp : public framework::OperatorWithKernel {
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) must be initialized."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) should not be null."
);
std
::
vector
<
int
>
expand_times
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"expand
T
imes"
);
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"expand
_t
imes"
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
PADDLE_ENFORCE_EQ
(
static_cast
<
size_t
>
(
x_dims
.
size
()),
expand_times
.
size
(),
"The number of Attr(expand
T
imes)'s value must be equal "
"The number of Attr(expand
_t
imes)'s value must be equal "
"to the rank of Input(X)."
);
PADDLE_ENFORCE_LE
(
x_dims
.
size
(),
6
,
"The rank of Input(X) must not be greater than 6."
);
...
...
@@ -39,14 +41,16 @@ class ExpandOp : public framework::OperatorWithKernel {
std
::
vector
<
int64_t
>
out_shape
(
x_dims
.
size
());
for
(
size_t
i
=
0
;
i
<
expand_times
.
size
();
++
i
)
{
PADDLE_ENFORCE_GE
(
expand_times
[
i
],
1
,
"Each value of Attr(expand
T
imes) should not be "
"Each value of Attr(expand
_t
imes) should not be "
"less than 1."
);
out_shape
[
i
]
=
x_dims
[
i
]
*
expand_times
[
i
];
}
ctx
->
SetOutputDim
(
"Out"
,
framework
::
make_ddim
(
out_shape
));
if
(
out_shape
[
0
]
==
x_dims
[
0
])
{
ctx
->
ShareLoD
(
"X"
,
"Out"
);
}
}
};
class
ExpandOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
...
...
@@ -61,13 +65,13 @@ class ExpandOpMaker : public framework::OpProtoAndCheckerMaker {
"The rank of Output(Out) is same as Input(X) except that each "
"dimension size of Output(Out) is equal to corresponding "
"dimension size of Input(X) multiplying corresponding value of "
"Attr(expand
T
imes)."
);
AddAttr
<
std
::
vector
<
int
>>
(
"expand
T
imes"
,
"Attr(expand
_t
imes)."
);
AddAttr
<
std
::
vector
<
int
>>
(
"expand
_t
imes"
,
"Expand times number for each dimension."
);
AddComment
(
R"DOC(
Expand operator tiles the input by given times number. You should set times
number for each dimension by providing attribute 'expand
T
imes'. The rank of X
should be in [1, 6]. Please notice that size of 'expand
T
imes' must be same with
number for each dimension by providing attribute 'expand
_t
imes'. The rank of X
should be in [1, 6]. Please notice that size of 'expand
_t
imes' must be same with
X's rank.
)DOC"
);
}
...
...
@@ -82,16 +86,17 @@ class ExpandGradOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
"Input(Out@GRAD) should not be null."
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
std
::
vector
<
int
>
expand_times
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"expand
T
imes"
);
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"expand
_t
imes"
);
auto
out_dims
=
ctx
->
GetInputDim
(
framework
::
GradVarName
(
"Out"
));
for
(
size_t
i
=
0
;
i
<
expand_times
.
size
();
++
i
)
{
PADDLE_ENFORCE_EQ
(
x_dims
[
i
]
*
expand_times
[
i
],
out_dims
[
i
],
"Each dimension size of Input(Out@GRAD) should be "
"equal to multiplication of crroresponding dimension "
"size of Input(X) and Attr(expand
T
imes) value."
);
"size of Input(X) and Attr(expand
_t
imes) value."
);
}
auto
x_grad_name
=
framework
::
GradVarName
(
"X"
);
...
...
paddle/operators/expand_op.h
浏览文件 @
d04c8538
...
...
@@ -25,14 +25,17 @@
#include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"
#define MAX_RANK_SUPPORTED 6
#define EXPAND_TEMPLATE(z, n, data) \
case n + 1: { \
Expand<n + 1>(context); \
break; \
}
#define REP_EXPAND_TEMPLATE(n) BOOST_PP_REPEAT(n, EXPAND_TEMPLATE, ~)
#define COND(n) BOOST_PP_GREATER_EQUAL(BOOST_PP_DIV(n, 6), BOOST_PP_MOD(n, 6))
#define COND(n) \
BOOST_PP_GREATER_EQUAL(BOOST_PP_DIV(n, MAX_RANK_SUPPORTED), \
BOOST_PP_MOD(n, MAX_RANK_SUPPORTED))
#define EXPAND_GRAD_CASE(n) \
case n: { \
ExpandBackward<n>(context, reshape_dims_vec, reduce_dims_vec); \
...
...
@@ -46,7 +49,6 @@ namespace paddle {
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
T
,
int
MajorType
=
Eigen
::
RowMajor
,
typename
IndexType
=
Eigen
::
DenseIndex
>
using
EigenVector
=
framework
::
EigenVector
<
T
,
MajorType
,
IndexType
>
;
...
...
@@ -60,7 +62,7 @@ class ExpandKernel : public framework::OpKernel<T> {
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
rank
=
context
.
Input
<
Tensor
>
(
"X"
)
->
dims
().
size
();
switch
(
rank
)
{
REP_EXPAND_TEMPLATE
(
6
)
REP_EXPAND_TEMPLATE
(
MAX_RANK_SUPPORTED
)
default:
PADDLE_ENFORCE
(
false
,
"Only support tensor with rank being between 1 and 6."
);
...
...
@@ -71,7 +73,7 @@ class ExpandKernel : public framework::OpKernel<T> {
template
<
int
Rank
>
void
Expand
(
const
framework
::
ExecutionContext
&
context
)
const
{
auto
*
in0
=
context
.
Input
<
Tensor
>
(
"X"
);
auto
&
expand_times
=
context
.
Attr
<
std
::
vector
<
int
>>
(
"expand
T
imes"
);
auto
&
expand_times
=
context
.
Attr
<
std
::
vector
<
int
>>
(
"expand
_t
imes"
);
auto
*
out0
=
context
.
Output
<
Tensor
>
(
"Out"
);
Eigen
::
DSizes
<
int
,
Rank
>
bcast_dims
;
auto
x_dims
=
in0
->
dims
();
...
...
@@ -91,8 +93,14 @@ class ExpandGradKernel : public framework::OpKernel<T> {
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
in0
=
context
.
Input
<
Tensor
>
(
"X"
);
auto
&
expand_times
=
context
.
Attr
<
std
::
vector
<
int
>>
(
"expand
T
imes"
);
auto
&
expand_times
=
context
.
Attr
<
std
::
vector
<
int
>>
(
"expand
_t
imes"
);
auto
x_dims
=
in0
->
dims
();
// 1. reshape_dims_vec is the broadcast parameter. For each dimension i,
// if expand_times[i] > 1 and x_dims[i] > 1, i will be splitted to two
// dimensions [expand_times[i], x_dims[i]].
// 2. reduce_dims_vec is the dimension parameter to compute gradients. For
// each dimension expanded, the gradients should be summed to original
// size.
std
::
vector
<
int
>
reshape_dims_vec
;
std
::
vector
<
int
>
reduce_dims_vec
;
for
(
size_t
i
=
0
;
i
<
expand_times
.
size
();
++
i
)
{
...
...
@@ -110,7 +118,8 @@ class ExpandGradKernel : public framework::OpKernel<T> {
}
}
int
dims
=
reshape_dims_vec
.
size
()
*
6
+
reduce_dims_vec
.
size
()
-
7
;
int
dims
=
reshape_dims_vec
.
size
()
*
MAX_RANK_SUPPORTED
+
reduce_dims_vec
.
size
()
-
MAX_RANK_SUPPORTED
-
1
;
// no need reduce, just copy
if
(
reduce_dims_vec
.
size
()
==
0
)
{
auto
*
in0
=
context
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
...
...
@@ -132,8 +141,8 @@ class ExpandGradKernel : public framework::OpKernel<T> {
void
ExpandBackward
(
const
framework
::
ExecutionContext
&
context
,
const
std
::
vector
<
int
>&
reshape_dims_vec
,
const
std
::
vector
<
int
>&
reduce_dims_vec
)
const
{
size_t
reshape_size
=
Dims
/
6
+
1
;
size_t
reduce_size
=
Dims
%
6
+
1
;
size_t
reshape_size
=
Dims
/
MAX_RANK_SUPPORTED
+
1
;
size_t
reduce_size
=
Dims
%
MAX_RANK_SUPPORTED
+
1
;
PADDLE_ENFORCE_EQ
(
reshape_size
,
reshape_dims_vec
.
size
(),
"Inconsistent size between template Dims and "
"reshape dimensions."
);
...
...
@@ -145,11 +154,11 @@ class ExpandGradKernel : public framework::OpKernel<T> {
auto
x
=
EigenVector
<
T
>::
Flatten
(
*
(
context
.
Input
<
Tensor
>
(
"X"
)));
out0
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
x_grad
=
EigenVector
<
T
>::
Flatten
(
*
out0
);
Eigen
::
DSizes
<
int
,
Dims
/
6
+
1
>
reshape_dims
;
Eigen
::
DSizes
<
int
,
Dims
/
MAX_RANK_SUPPORTED
+
1
>
reshape_dims
;
for
(
size_t
i
=
0
;
i
<
reshape_size
;
++
i
)
{
reshape_dims
[
i
]
=
reshape_dims_vec
[
i
];
}
Eigen
::
DSizes
<
int
,
Dims
%
6
+
1
>
reduce_dims
;
Eigen
::
DSizes
<
int
,
Dims
%
MAX_RANK_SUPPORTED
+
1
>
reduce_dims
;
for
(
size_t
i
=
0
;
i
<
reduce_size
;
++
i
)
{
reduce_dims
[
i
]
=
reduce_dims_vec
[
i
];
}
...
...
python/paddle/v2/framework/tests/test_expand_op.py
浏览文件 @
d04c8538
...
...
@@ -7,7 +7,7 @@ class TestExpandOpRank1(OpTest):
def
setUp
(
self
):
self
.
op_type
=
"expand"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
(
12
).
astype
(
"float32"
)}
self
.
attrs
=
{
'expand
T
imes'
:
[
2
]}
self
.
attrs
=
{
'expand
_t
imes'
:
[
2
]}
output
=
np
.
tile
(
self
.
inputs
[
'X'
],
2
)
self
.
outputs
=
{
'Out'
:
output
}
...
...
@@ -18,11 +18,11 @@ class TestExpandOpRank1(OpTest):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestExpandOpRank2_
1
(
OpTest
):
class
TestExpandOpRank2_
Corner
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"expand"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
12
,
14
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'expand
T
imes'
:
[
1
,
1
]}
self
.
attrs
=
{
'expand
_t
imes'
:
[
1
,
1
]}
output
=
np
.
tile
(
self
.
inputs
[
'X'
],
(
1
,
1
))
self
.
outputs
=
{
'Out'
:
output
}
...
...
@@ -33,11 +33,11 @@ class TestExpandOpRank2_1(OpTest):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestExpandOpRank2
_2
(
OpTest
):
class
TestExpandOpRank2
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"expand"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
12
,
14
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'expand
T
imes'
:
[
2
,
3
]}
self
.
attrs
=
{
'expand
_t
imes'
:
[
2
,
3
]}
output
=
np
.
tile
(
self
.
inputs
[
'X'
],
(
2
,
3
))
self
.
outputs
=
{
'Out'
:
output
}
...
...
@@ -48,11 +48,11 @@ class TestExpandOpRank2_2(OpTest):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestExpandOpRank3_
1
(
OpTest
):
class
TestExpandOpRank3_
Corner
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"expand"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
2
,
4
,
5
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'expand
T
imes'
:
[
1
,
1
,
1
]}
self
.
attrs
=
{
'expand
_t
imes'
:
[
1
,
1
,
1
]}
output
=
np
.
tile
(
self
.
inputs
[
'X'
],
(
1
,
1
,
1
))
self
.
outputs
=
{
'Out'
:
output
}
...
...
@@ -63,11 +63,11 @@ class TestExpandOpRank3_1(OpTest):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestExpandOpRank3
_2
(
OpTest
):
class
TestExpandOpRank3
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"expand"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
2
,
4
,
5
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'expand
T
imes'
:
[
2
,
1
,
4
]}
self
.
attrs
=
{
'expand
_t
imes'
:
[
2
,
1
,
4
]}
output
=
np
.
tile
(
self
.
inputs
[
'X'
],
(
2
,
1
,
4
))
self
.
outputs
=
{
'Out'
:
output
}
...
...
@@ -82,7 +82,7 @@ class TestExpandOpRank4(OpTest):
def
setUp
(
self
):
self
.
op_type
=
"expand"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
2
,
4
,
5
,
7
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'expand
T
imes'
:
[
3
,
2
,
1
,
2
]}
self
.
attrs
=
{
'expand
_t
imes'
:
[
3
,
2
,
1
,
2
]}
output
=
np
.
tile
(
self
.
inputs
[
'X'
],
(
3
,
2
,
1
,
2
))
self
.
outputs
=
{
'Out'
:
output
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录