Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
ad5e7cc0
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
ad5e7cc0
编写于
9月 13, 2017
作者:
Y
yangyaming
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implemented by boost preprocessor.
上级
8be9930f
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
347 addition
and
0 deletion
+347
-0
paddle/operators/expand_op.cc
paddle/operators/expand_op.cc
+103
-0
paddle/operators/expand_op.cu
paddle/operators/expand_op.cu
+23
-0
paddle/operators/expand_op.h
paddle/operators/expand_op.h
+152
-0
paddle/pybind/pybind.cc
paddle/pybind/pybind.cc
+1
-0
python/paddle/v2/framework/tests/CMakeLists.txt
python/paddle/v2/framework/tests/CMakeLists.txt
+1
-0
python/paddle/v2/framework/tests/test_expand_op.py
python/paddle/v2/framework/tests/test_expand_op.py
+67
-0
未找到文件。
paddle/operators/expand_op.cc
0 → 100644
浏览文件 @
ad5e7cc0
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/expand_op.h"
namespace
paddle
{
namespace
operators
{
using
framework
::
Tensor
;
class
ExpandOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"X"
),
"X must be initialized."
);
std
::
vector
<
int
>
expand_times
=
Attr
<
std
::
vector
<
int
>>
(
"expandTimes"
);
auto
*
x
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
x_dims
=
x
->
dims
();
PADDLE_ENFORCE_EQ
(
static_cast
<
size_t
>
(
framework
::
arity
(
x_dims
)),
expand_times
.
size
(),
"Number of attribute (expandTimes) value must be equal "
"to rank of X."
);
PADDLE_ENFORCE_LE
(
framework
::
arity
(
x_dims
),
6
,
"Rank of X must not be greater than 6."
);
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 expand times should not be "
"less than 1."
);
out_shape
[
i
]
=
x_dims
[
i
]
*
expand_times
[
i
];
}
auto
*
out
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
out
->
Resize
(
framework
::
make_ddim
(
out_shape
));
}
};
class
ExpandOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
ExpandOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input tensor."
);
AddOutput
(
"Out"
,
"Expanded result by tiling input X."
);
AddAttr
<
std
::
vector
<
int
>>
(
"expandTimes"
,
"Expand times for each dimension."
);
AddComment
(
R"DOC(
Expand operator tiles the input by given times. You should set times for each
dimension by providing attribute 'expandTimes'. Rank of input tensor should be
in [1, 6]. Please draw an inttention that size of 'expandTimes' must be same
with rank of input tensor.
)DOC"
);
}
};
class
ExpandGradOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"X"
),
"X must be initialized."
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
framework
::
GradVarName
(
"Out"
)),
"Input(Out@GRAD) should not be null."
);
auto
x_dims
=
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
();
std
::
vector
<
int
>
expand_times
=
Attr
<
std
::
vector
<
int
>>
(
"expandTimes"
);
auto
out_dims
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
))
->
dims
();
auto
*
x_grad
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
for
(
size_t
i
=
0
;
i
<
expand_times
.
size
();
++
i
)
{
PADDLE_ENFORCE_EQ
(
x_dims
[
i
]
*
expand_times
[
i
],
out_dims
[
i
],
"Size of each dimension of Input(Out@GRAD) should be "
"equal to multiplication of crroresponding sizes of "
"Input(X) and expandTimes."
);
}
if
(
x_grad
)
x_grad
->
Resize
(
x_dims
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
expand
,
ops
::
ExpandOp
,
ops
::
ExpandOpMaker
,
expand_grad
,
ops
::
ExpandGradOp
);
REGISTER_OP_CPU_KERNEL
(
expand
,
ops
::
ExpandKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
expand_grad
,
ops
::
ExpandGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/expand_op.cu
0 → 100644
浏览文件 @
ad5e7cc0
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#define EIGEN_USE_GPU
#include "paddle/operators/expand_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
expand
,
ops
::
ExpandKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
expand_grad
,
ops
::
ExpandGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/expand_op.h
0 → 100644
浏览文件 @
ad5e7cc0
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include <boost/preprocessor/arithmetic/div.hpp>
#include <boost/preprocessor/arithmetic/mod.hpp>
#include <boost/preprocessor/comparison/greater.hpp>
#include <boost/preprocessor/comparison/greater_equal.hpp>
#include <boost/preprocessor/control/if.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <iostream>
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"
#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 EXPAND_GRAD_CASE(n) \
case n: { \
ExpandBackward<n>(context, reshape_dims_vec, reduce_dims_vec); \
break; \
}
#define EXPAND_TEMPLATE_GRAD(z, n, data) \
BOOST_PP_IF(COND(n), EXPAND_GRAD_CASE(n), )
#define REP_EXPAND_GRAD_TEMPLATE(n) BOOST_PP_REPEAT(n, EXPAND_TEMPLATE_GRAD, ~)
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
>
;
template
<
typename
T
,
size_t
D
,
int
MajorType
=
Eigen
::
RowMajor
,
typename
IndexType
=
Eigen
::
DenseIndex
>
using
EigenTensor
=
framework
::
EigenTensor
<
T
,
D
,
MajorType
,
IndexType
>
;
template
<
typename
Place
,
typename
T
>
class
ExpandKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
rank
=
framework
::
arity
(
context
.
Input
<
Tensor
>
(
"X"
)
->
dims
());
switch
(
rank
)
{
REP_EXPAND_TEMPLATE
(
6
)
default:
PADDLE_ENFORCE
(
false
,
"Only support tensor whose rank in [1, 6]."
);
};
}
protected:
template
<
int
Rank
>
void
Expand
(
const
framework
::
ExecutionContext
&
context
)
const
{
auto
*
in0
=
context
.
Input
<
Tensor
>
(
"X"
);
auto
expand_times
=
context
.
Attr
<
std
::
vector
<
int
>>
(
"expandTimes"
);
auto
*
out0
=
context
.
Output
<
Tensor
>
(
"Out"
);
Eigen
::
DSizes
<
int
,
Rank
>
bcast_dims
;
auto
x_dims
=
in0
->
dims
();
for
(
size_t
i
=
0
;
i
<
expand_times
.
size
();
++
i
)
{
bcast_dims
[
i
]
=
expand_times
[
i
];
}
auto
x
=
EigenTensor
<
T
,
Rank
>::
From
(
*
in0
);
out0
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
y
=
EigenTensor
<
T
,
Rank
>::
From
(
*
out0
);
auto
place
=
context
.
GetEigenDevice
<
Place
>
();
y
.
device
(
place
)
=
x
.
broadcast
(
bcast_dims
);
}
};
template
<
typename
Place
,
typename
T
>
class
ExpandGradKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
in0
=
context
.
Input
<
Tensor
>
(
"X"
);
auto
expand_times
=
context
.
Attr
<
std
::
vector
<
int
>>
(
"expandTimes"
);
auto
x_dims
=
in0
->
dims
();
std
::
vector
<
int
>
reshape_dims_vec
;
std
::
vector
<
int
>
reduce_dims_vec
;
for
(
size_t
i
=
0
;
i
<
expand_times
.
size
();
++
i
)
{
if
(
expand_times
[
i
]
==
1
)
{
reshape_dims_vec
.
push_back
(
x_dims
[
i
]);
}
else
{
if
(
x_dims
[
i
]
==
1
)
{
reduce_dims_vec
.
push_back
(
reshape_dims_vec
.
size
());
reshape_dims_vec
.
push_back
(
expand_times
[
i
]);
}
else
{
reduce_dims_vec
.
push_back
(
reshape_dims_vec
.
size
());
reshape_dims_vec
.
push_back
(
expand_times
[
i
]);
reshape_dims_vec
.
push_back
(
x_dims
[
i
]);
}
}
}
int
dims
=
reshape_dims_vec
.
size
()
*
6
+
reduce_dims_vec
.
size
()
-
7
;
switch
(
dims
)
{
REP_EXPAND_GRAD_TEMPLATE
(
72
)
default:
PADDLE_ENFORCE
(
false
,
"Only support tensor whose rank in [1, 6]."
);
};
}
protected:
template
<
int
Dims
>
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
;
PADDLE_ENFORCE_EQ
(
reshape_size
,
reshape_dims_vec
.
size
(),
"Inconsistent size between Dims and "
"reshape dimensions."
);
PADDLE_ENFORCE_EQ
(
reduce_size
,
reduce_dims_vec
.
size
(),
"Inconsistent size between Dims and "
"reduce dimensions."
);
auto
*
in0
=
context
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
out0
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
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
;
for
(
size_t
i
=
0
;
i
<
reshape_size
;
++
i
)
{
reshape_dims
[
i
]
=
reshape_dims_vec
[
i
];
}
Eigen
::
DSizes
<
int
,
Dims
%
6
+
1
>
reduce_dims
;
for
(
size_t
i
=
0
;
i
<
reduce_size
;
++
i
)
{
reduce_dims
[
i
]
=
reduce_dims_vec
[
i
];
}
auto
out_grad
=
EigenVector
<
T
>::
Flatten
(
*
in0
);
x_grad
.
device
(
context
.
GetEigenDevice
<
Place
>
())
=
out_grad
.
reshape
(
reshape_dims
).
sum
(
reduce_dims
).
reshape
(
x
.
dimensions
());
}
};
}
// operators
}
// paddle
paddle/pybind/pybind.cc
浏览文件 @
ad5e7cc0
...
...
@@ -54,6 +54,7 @@ USE_CPU_ONLY_OP(concat);
USE_OP
(
top_k
);
USE_OP
(
squared_l2_distance
);
USE_OP
(
sum
);
USE_OP
(
expand
);
namespace
paddle
{
namespace
framework
{
...
...
python/paddle/v2/framework/tests/CMakeLists.txt
浏览文件 @
ad5e7cc0
...
...
@@ -35,3 +35,4 @@ py_test(test_sum_op SRCS test_sum_op.py)
py_test
(
mnist SRCS mnist.py
)
py_test
(
test_concat_op SRCS test_concat_op.py
)
py_test
(
test_squared_l2_distance_op SRCS test_squared_l2_distance_op.py
)
py_test
(
test_expand_op SRCS test_expand_op.py
)
python/paddle/v2/framework/tests/test_expand_op.py
0 → 100644
浏览文件 @
ad5e7cc0
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
class
TestExpandOpRank1
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"expand"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
(
12
).
astype
(
"float32"
)}
self
.
attrs
=
{
'expandTimes'
:
[
2
]}
output
=
np
.
tile
(
self
.
inputs
[
'X'
],
2
)
self
.
outputs
=
{
'Out'
:
output
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestExpandOpRank2
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"expand"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
12
,
14
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'expandTimes'
:
[
3
,
4
]}
output
=
np
.
tile
(
self
.
inputs
[
'X'
],
(
3
,
4
))
self
.
outputs
=
{
'Out'
:
output
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestExpandOpRank3
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"expand"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
2
,
4
,
5
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'expandTimes'
:
[
3
,
2
,
1
]}
output
=
np
.
tile
(
self
.
inputs
[
'X'
],
(
3
,
2
,
1
))
self
.
outputs
=
{
'Out'
:
output
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestExpandOpRank4
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"expand"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
2
,
4
,
5
,
7
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'expandTimes'
:
[
3
,
2
,
1
,
2
]}
output
=
np
.
tile
(
self
.
inputs
[
'X'
],
(
3
,
2
,
1
,
2
))
self
.
outputs
=
{
'Out'
:
output
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录