Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
21ce7042
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看板
提交
21ce7042
编写于
11月 09, 2017
作者:
C
chengduoZH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine conv2d for filter size:(1,1)
上级
b6f9ba48
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
192 addition
and
83 deletion
+192
-83
paddle/operators/conv_op.h
paddle/operators/conv_op.h
+173
-83
python/paddle/v2/framework/tests/test_conv2d_op.py
python/paddle/v2/framework/tests/test_conv2d_op.py
+19
-0
未找到文件。
paddle/operators/conv_op.h
浏览文件 @
21ce7042
...
@@ -35,6 +35,18 @@ inline int OutputSize(int input_size, int filter_size, int dilation,
...
@@ -35,6 +35,18 @@ inline int OutputSize(int input_size, int filter_size, int dilation,
1
;
1
;
return
output_size
;
return
output_size
;
}
}
inline
bool
NotExpand
(
std
::
vector
<
int64_t
>&
filter_dim
,
std
::
vector
<
int
>&
strides
,
std
::
vector
<
int
>&
paddings
,
std
::
vector
<
int
>&
dilations
)
{
bool
filter_1
=
true
,
strides_1
=
true
,
padding_0
=
true
,
dilation_1
=
true
;
for
(
size_t
j
=
0
;
j
<
strides
.
size
();
++
j
)
{
filter_1
&=
(
static_cast
<
int
>
(
filter_dim
[
j
])
==
1
);
strides_1
&=
(
strides
[
j
]
==
1
);
padding_0
&=
(
paddings
[
j
]
==
0
);
dilation_1
&=
(
dilations
[
j
]
==
1
);
}
return
filter_1
&&
strides_1
&&
padding_0
&&
dilation_1
;
}
// Define Op classes in .h file so that other conv
// Define Op classes in .h file so that other conv
// operator implementations can reuse the code.
// operator implementations can reuse the code.
...
@@ -110,14 +122,17 @@ class GemmConvKernel : public framework::OpKernel<T> {
...
@@ -110,14 +122,17 @@ class GemmConvKernel : public framework::OpKernel<T> {
framework
::
DDim
col_matrix_shape
=
framework
::
DDim
col_matrix_shape
=
framework
::
flatten_to_2d
(
col_shape
,
filter_shape_vec
.
size
()
+
1
);
framework
::
flatten_to_2d
(
col_shape
,
filter_shape_vec
.
size
()
+
1
);
bool
not_expand
=
NotExpand
(
filter_shape_vec
,
strides
,
paddings
,
dilations
);
Tensor
col
;
Tensor
col
;
col
.
mutable_data
<
T
>
(
col_shape
,
context
.
GetPlace
());
// col_matrix shares the same piece of data with col,
// col_matrix shares the same piece of data with col,
// but will be reshaped into a two-dimensional matrix shape
// but will be reshaped into a two-dimensional matrix shape
// to call the matrix multiplication interface.
// to call the matrix multiplication interface.
Tensor
col_matrix
;
Tensor
col_matrix
;
col_matrix
.
ShareDataWith
(
col
);
if
(
!
not_expand
)
{
col_matrix
.
Resize
(
col_matrix_shape
);
col
.
mutable_data
<
T
>
(
col_shape
,
context
.
GetPlace
());
col_matrix
.
ShareDataWith
(
col
);
col_matrix
.
Resize
(
col_matrix_shape
);
}
framework
::
DDim
input_shape
=
framework
::
slice_ddim
(
framework
::
DDim
input_shape
=
framework
::
slice_ddim
(
input
->
dims
(),
1
,
static_cast
<
int
>
(
input
->
dims
().
size
()));
input
->
dims
(),
1
,
static_cast
<
int
>
(
input
->
dims
().
size
()));
...
@@ -134,31 +149,51 @@ class GemmConvKernel : public framework::OpKernel<T> {
...
@@ -134,31 +149,51 @@ class GemmConvKernel : public framework::OpKernel<T> {
int
in_step
=
static_cast
<
int
>
(
input
->
dims
()[
1
])
/
groups
;
int
in_step
=
static_cast
<
int
>
(
input
->
dims
()[
1
])
/
groups
;
int
out_step
=
static_cast
<
int
>
(
output
->
dims
()[
1
])
/
groups
;
int
out_step
=
static_cast
<
int
>
(
output
->
dims
()[
1
])
/
groups
;
for
(
int
i
=
0
;
i
<
batch_size
;
i
++
)
{
if
(
!
not_expand
)
{
Tensor
in_batch
=
input
->
Slice
(
i
,
i
+
1
).
Resize
(
input_shape
);
for
(
int
i
=
0
;
i
<
batch_size
;
i
++
)
{
Tensor
out_batch
=
output
->
Slice
(
i
,
i
+
1
).
Resize
(
output_matrix_shape
);
Tensor
in_batch
=
input
->
Slice
(
i
,
i
+
1
).
Resize
(
input_shape
);
for
(
int
g
=
0
;
g
<
groups
;
g
++
)
{
Tensor
out_batch
=
output
->
Slice
(
i
,
i
+
1
).
Resize
(
output_matrix_shape
);
Tensor
in_slice
=
in_batch
.
Slice
(
g
*
in_step
,
(
g
+
1
)
*
in_step
);
for
(
int
g
=
0
;
g
<
groups
;
g
++
)
{
Tensor
in_slice
=
in_batch
.
Slice
(
g
*
in_step
,
(
g
+
1
)
*
in_step
);
if
(
filter_shape_vec
.
size
()
==
2
)
{
// im2col
if
(
filter_shape_vec
.
size
()
==
2
)
{
math
::
Im2ColFunctor
<
math
::
ColFormat
::
kCFO
,
Place
,
T
>
im2col
;
// im2col
im2col
(
context
.
device_context
(),
in_slice
,
col
,
dilations
[
0
],
math
::
Im2ColFunctor
<
math
::
ColFormat
::
kCFO
,
Place
,
T
>
im2col
;
dilations
[
1
],
strides
[
0
],
strides
[
1
],
paddings
[
0
],
paddings
[
0
],
im2col
(
context
.
device_context
(),
in_slice
,
col
,
dilations
[
0
],
paddings
[
1
],
paddings
[
1
]);
dilations
[
1
],
strides
[
0
],
strides
[
1
],
paddings
[
0
],
}
else
if
(
filter_shape_vec
.
size
()
==
3
)
{
paddings
[
0
],
paddings
[
1
],
paddings
[
1
]);
// vol2col
}
else
if
(
filter_shape_vec
.
size
()
==
3
)
{
math
::
Vol2ColFunctor
<
Place
,
T
>
vol2col
;
// vol2col
vol2col
(
context
.
device_context
(),
in_slice
,
col
,
strides
[
0
],
math
::
Vol2ColFunctor
<
Place
,
T
>
vol2col
;
strides
[
1
],
strides
[
2
],
paddings
[
0
],
paddings
[
1
],
vol2col
(
context
.
device_context
(),
in_slice
,
col
,
strides
[
0
],
paddings
[
2
]);
strides
[
1
],
strides
[
2
],
paddings
[
0
],
paddings
[
1
],
paddings
[
2
]);
}
// gemm
Tensor
out_slice
=
out_batch
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
Tensor
filter_slice
=
filter
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
filter_slice
,
false
,
col_matrix
,
false
,
T
(
1.0
),
&
out_slice
,
T
(
0.0
));
}
}
}
}
else
{
for
(
int
i
=
0
;
i
<
batch_size
;
i
++
)
{
Tensor
in_batch
=
input
->
Slice
(
i
,
i
+
1
).
Resize
(
input_shape
);
Tensor
out_batch
=
output
->
Slice
(
i
,
i
+
1
).
Resize
(
output_matrix_shape
);
for
(
int
g
=
0
;
g
<
groups
;
g
++
)
{
Tensor
in_slice
=
in_batch
.
Slice
(
g
*
in_step
,
(
g
+
1
)
*
in_step
);
// gemm
col
.
ShareDataWith
(
in_slice
);
Tensor
out_slice
=
out_batch
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
col_matrix
.
ShareDataWith
(
col
);
Tensor
filter_slice
=
filter
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
col_matrix
.
Resize
(
col_matrix_shape
);
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
filter_slice
,
false
,
col_matrix
,
false
,
T
(
1.0
),
&
out_slice
,
T
(
0.0
));
// gemm
Tensor
out_slice
=
out_batch
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
Tensor
filter_slice
=
filter
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
filter_slice
,
false
,
col_matrix
,
false
,
T
(
1.0
),
&
out_slice
,
T
(
0.0
));
}
}
}
}
}
}
}
...
@@ -235,14 +270,17 @@ class GemmConvGradKernel : public framework::OpKernel<T> {
...
@@ -235,14 +270,17 @@ class GemmConvGradKernel : public framework::OpKernel<T> {
int
in_step
=
static_cast
<
int
>
(
input
->
dims
()[
1
])
/
groups
;
int
in_step
=
static_cast
<
int
>
(
input
->
dims
()[
1
])
/
groups
;
int
out_step
=
static_cast
<
int
>
(
output_grad
->
dims
()[
1
])
/
groups
;
int
out_step
=
static_cast
<
int
>
(
output_grad
->
dims
()[
1
])
/
groups
;
bool
not_expand
=
NotExpand
(
filter_shape_vec
,
strides
,
paddings
,
dilations
);
Tensor
col
;
Tensor
col
;
// col_matrix shares the same piece of data with col,
// col_matrix shares the same piece of data with col,
// but will be reshaped into a two-dimensional matrix shape
// but will be reshaped into a two-dimensional matrix shape
// to call the matrix multiplication interface.
// to call the matrix multiplication interface.
Tensor
col_matrix
;
Tensor
col_matrix
;
col
.
mutable_data
<
T
>
(
col_shape
,
context
.
GetPlace
());
if
(
!
not_expand
)
{
col_matrix
.
ShareDataWith
(
col
);
col
.
mutable_data
<
T
>
(
col_shape
,
context
.
GetPlace
());
col_matrix
.
Resize
(
col_matrix_shape
);
col_matrix
.
ShareDataWith
(
col
);
col_matrix
.
Resize
(
col_matrix_shape
);
}
math
::
SetConstant
<
Place
,
T
>
set_zero
;
math
::
SetConstant
<
Place
,
T
>
set_zero
;
...
@@ -250,33 +288,60 @@ class GemmConvGradKernel : public framework::OpKernel<T> {
...
@@ -250,33 +288,60 @@ class GemmConvGradKernel : public framework::OpKernel<T> {
input_grad
->
mutable_data
<
T
>
(
context
.
GetPlace
());
input_grad
->
mutable_data
<
T
>
(
context
.
GetPlace
());
set_zero
(
context
.
device_context
(),
input_grad
,
static_cast
<
T
>
(
0
));
set_zero
(
context
.
device_context
(),
input_grad
,
static_cast
<
T
>
(
0
));
for
(
int
i
=
0
;
i
<
batch_size
;
i
++
)
{
if
(
!
not_expand
)
{
Tensor
out_grad_batch
=
for
(
int
i
=
0
;
i
<
batch_size
;
i
++
)
{
output_grad
->
Slice
(
i
,
i
+
1
).
Resize
(
output_matrix_shape
);
Tensor
out_grad_batch
=
Tensor
in_grad_batch
=
input_grad
->
Slice
(
i
,
i
+
1
).
Resize
(
input_shape
);
output_grad
->
Slice
(
i
,
i
+
1
).
Resize
(
output_matrix_shape
);
for
(
int
g
=
0
;
g
<
groups
;
g
++
)
{
Tensor
in_grad_batch
=
// gemm
input_grad
->
Slice
(
i
,
i
+
1
).
Resize
(
input_shape
);
Tensor
out_grad_slice
=
for
(
int
g
=
0
;
g
<
groups
;
g
++
)
{
out_grad_batch
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
// gemm
Tensor
filter_slice
=
filter
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
Tensor
out_grad_slice
=
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
filter_slice
,
true
,
out_grad_batch
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
out_grad_slice
,
false
,
T
(
1.0
),
&
col_matrix
,
Tensor
filter_slice
=
T
(
0.0
));
filter
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
// col2im
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
filter_slice
,
true
,
Tensor
in_grad_slice
=
out_grad_slice
,
false
,
T
(
1.0
),
&
col_matrix
,
in_grad_batch
.
Slice
(
g
*
in_step
,
(
g
+
1
)
*
in_step
);
T
(
0.0
));
Tensor
in_grad_slice
=
if
(
filter_shape_vec
.
size
()
==
2
)
{
in_grad_batch
.
Slice
(
g
*
in_step
,
(
g
+
1
)
*
in_step
);
math
::
Col2ImFunctor
<
math
::
ColFormat
::
kCFO
,
Place
,
T
>
col2im
;
col2im
(
context
.
device_context
(),
in_grad_slice
,
col
,
dilations
[
0
],
if
(
filter_shape_vec
.
size
()
==
2
)
{
dilations
[
1
],
strides
[
0
],
strides
[
1
],
paddings
[
0
],
math
::
Col2ImFunctor
<
math
::
ColFormat
::
kCFO
,
Place
,
T
>
col2im
;
paddings
[
0
],
paddings
[
1
],
paddings
[
1
]);
col2im
(
context
.
device_context
(),
in_grad_slice
,
col
,
dilations
[
0
],
dilations
[
1
],
strides
[
0
],
strides
[
1
],
paddings
[
0
],
}
else
if
(
filter_shape_vec
.
size
()
==
3
)
{
paddings
[
0
],
paddings
[
1
],
paddings
[
1
]);
math
::
Col2VolFunctor
<
Place
,
T
>
col2vol
;
col2vol
(
context
.
device_context
(),
in_grad_slice
,
col
,
strides
[
0
],
}
else
if
(
filter_shape_vec
.
size
()
==
3
)
{
strides
[
1
],
strides
[
2
],
paddings
[
0
],
paddings
[
1
],
math
::
Col2VolFunctor
<
Place
,
T
>
col2vol
;
paddings
[
2
]);
col2vol
(
context
.
device_context
(),
in_grad_slice
,
col
,
strides
[
0
],
strides
[
1
],
strides
[
2
],
paddings
[
0
],
paddings
[
1
],
paddings
[
2
]);
}
}
}
}
else
{
for
(
int
i
=
0
;
i
<
batch_size
;
i
++
)
{
Tensor
out_grad_batch
=
output_grad
->
Slice
(
i
,
i
+
1
).
Resize
(
output_matrix_shape
);
Tensor
in_grad_batch
=
input_grad
->
Slice
(
i
,
i
+
1
).
Resize
(
input_shape
);
for
(
int
g
=
0
;
g
<
groups
;
g
++
)
{
// gemm
Tensor
out_grad_slice
=
out_grad_batch
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
Tensor
filter_slice
=
filter
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
Tensor
in_grad_slice
=
in_grad_batch
.
Slice
(
g
*
in_step
,
(
g
+
1
)
*
in_step
);
col_matrix
.
ShareDataWith
(
in_grad_slice
);
col_matrix
.
Resize
(
col_matrix_shape
);
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
filter_slice
,
true
,
out_grad_slice
,
false
,
T
(
1.0
),
&
col_matrix
,
T
(
0.0
));
}
}
}
}
}
}
...
@@ -288,34 +353,59 @@ class GemmConvGradKernel : public framework::OpKernel<T> {
...
@@ -288,34 +353,59 @@ class GemmConvGradKernel : public framework::OpKernel<T> {
filter_grad_
.
Resize
(
filter_matrix_shape
);
filter_grad_
.
Resize
(
filter_matrix_shape
);
set_zero
(
context
.
device_context
(),
filter_grad
,
static_cast
<
T
>
(
0
));
set_zero
(
context
.
device_context
(),
filter_grad
,
static_cast
<
T
>
(
0
));
for
(
int
i
=
0
;
i
<
batch_size
;
i
++
)
{
if
(
!
not_expand
)
{
Tensor
out_grad_batch
=
for
(
int
i
=
0
;
i
<
batch_size
;
i
++
)
{
output_grad
->
Slice
(
i
,
i
+
1
).
Resize
(
output_matrix_shape
);
Tensor
out_grad_batch
=
Tensor
in_batch
=
input
->
Slice
(
i
,
i
+
1
).
Resize
(
input_shape
);
output_grad
->
Slice
(
i
,
i
+
1
).
Resize
(
output_matrix_shape
);
for
(
int
g
=
0
;
g
<
groups
;
g
++
)
{
Tensor
in_batch
=
input
->
Slice
(
i
,
i
+
1
).
Resize
(
input_shape
);
// im2col
for
(
int
g
=
0
;
g
<
groups
;
g
++
)
{
Tensor
out_grad_slice
=
// im2col
out_grad_batch
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
Tensor
out_grad_slice
=
Tensor
in_slice
=
in_batch
.
Slice
(
g
*
in_step
,
(
g
+
1
)
*
in_step
);
out_grad_batch
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
Tensor
in_slice
=
in_batch
.
Slice
(
g
*
in_step
,
(
g
+
1
)
*
in_step
);
if
(
filter_shape_vec
.
size
()
==
2
)
{
math
::
Im2ColFunctor
<
math
::
ColFormat
::
kCFO
,
Place
,
T
>
im2col
;
if
(
filter_shape_vec
.
size
()
==
2
)
{
im2col
(
context
.
device_context
(),
in_slice
,
col
,
dilations
[
0
],
math
::
Im2ColFunctor
<
math
::
ColFormat
::
kCFO
,
Place
,
T
>
im2col
;
dilations
[
1
],
strides
[
0
],
strides
[
1
],
paddings
[
0
],
im2col
(
context
.
device_context
(),
in_slice
,
col
,
dilations
[
0
],
paddings
[
0
],
paddings
[
1
],
paddings
[
1
]);
dilations
[
1
],
strides
[
0
],
strides
[
1
],
paddings
[
0
],
}
else
if
(
filter_shape_vec
.
size
()
==
3
)
{
paddings
[
0
],
paddings
[
1
],
paddings
[
1
]);
math
::
Vol2ColFunctor
<
Place
,
T
>
vol2col
;
}
else
if
(
filter_shape_vec
.
size
()
==
3
)
{
vol2col
(
context
.
device_context
(),
in_slice
,
col
,
strides
[
0
],
math
::
Vol2ColFunctor
<
Place
,
T
>
vol2col
;
strides
[
1
],
strides
[
2
],
paddings
[
0
],
paddings
[
1
],
vol2col
(
context
.
device_context
(),
in_slice
,
col
,
strides
[
0
],
paddings
[
2
]);
strides
[
1
],
strides
[
2
],
paddings
[
0
],
paddings
[
1
],
paddings
[
2
]);
}
// gemm
Tensor
filter_grad_slice
=
filter_grad_
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
out_grad_slice
,
false
,
col_matrix
,
true
,
T
(
1.0
),
&
filter_grad_slice
,
T
(
1.0
));
}
}
}
else
{
for
(
int
i
=
0
;
i
<
batch_size
;
i
++
)
{
Tensor
out_grad_batch
=
output_grad
->
Slice
(
i
,
i
+
1
).
Resize
(
output_matrix_shape
);
Tensor
in_batch
=
input
->
Slice
(
i
,
i
+
1
).
Resize
(
input_shape
);
for
(
int
g
=
0
;
g
<
groups
;
g
++
)
{
// im2col
Tensor
out_grad_slice
=
out_grad_batch
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
Tensor
in_slice
=
in_batch
.
Slice
(
g
*
in_step
,
(
g
+
1
)
*
in_step
);
col
.
ShareDataWith
(
in_slice
);
col_matrix
.
ShareDataWith
(
col
);
col_matrix
.
Resize
(
col_matrix_shape
);
// gemm
Tensor
filter_grad_slice
=
filter_grad_
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
out_grad_slice
,
false
,
col_matrix
,
true
,
T
(
1.0
),
&
filter_grad_slice
,
T
(
1.0
));
}
}
// gemm
Tensor
filter_grad_slice
=
filter_grad_
.
Slice
(
g
*
out_step
,
(
g
+
1
)
*
out_step
);
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
out_grad_slice
,
false
,
col_matrix
,
true
,
T
(
1.0
),
&
filter_grad_slice
,
T
(
1.0
));
}
}
}
}
}
}
...
...
python/paddle/v2/framework/tests/test_conv2d_op.py
浏览文件 @
21ce7042
...
@@ -104,6 +104,25 @@ class TestWithGroup(TestConv2dOp):
...
@@ -104,6 +104,25 @@ class TestWithGroup(TestConv2dOp):
self
.
op_type
=
"conv2d"
self
.
op_type
=
"conv2d"
class
TestWith1x1
(
TestConv2dOp
):
def
init_test_case
(
self
):
self
.
pad
=
[
0
,
0
]
self
.
stride
=
[
1
,
1
]
self
.
input_size
=
[
2
,
3
,
5
,
5
]
# NCHW
assert
np
.
mod
(
self
.
input_size
[
1
],
self
.
groups
)
==
0
f_c
=
self
.
input_size
[
1
]
/
self
.
groups
self
.
filter_size
=
[
6
,
f_c
,
1
,
1
]
def
init_dilation
(
self
):
self
.
dilations
=
[
1
,
1
]
def
init_group
(
self
):
self
.
groups
=
3
def
init_op_type
(
self
):
self
.
op_type
=
"conv2d"
#----------------Conv2dCudnn----------------
#----------------Conv2dCudnn----------------
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录