Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
2f5fb031
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
2f5fb031
编写于
3月 16, 2022
作者:
Z
zhangkaihuo
提交者:
GitHub
3月 16, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Restructure sparse conv (#40570)
restructure conv
上级
3898080e
变更
9
展开全部
显示空白变更内容
内联
并排
Showing
9 changed file
with
555 addition
and
552 deletion
+555
-552
paddle/phi/kernels/funcs/sparse/convolution.h
paddle/phi/kernels/funcs/sparse/convolution.h
+10
-10
paddle/phi/kernels/sparse/convolution_grad_kernel.h
paddle/phi/kernels/sparse/convolution_grad_kernel.h
+2
-2
paddle/phi/kernels/sparse/cpu/convolution.h
paddle/phi/kernels/sparse/cpu/convolution.h
+7
-7
paddle/phi/kernels/sparse/cpu/convolution_grad_kernel.cc
paddle/phi/kernels/sparse/cpu/convolution_grad_kernel.cc
+2
-2
paddle/phi/kernels/sparse/cpu/convolution_kernel.cc
paddle/phi/kernels/sparse/cpu/convolution_kernel.cc
+7
-2
paddle/phi/kernels/sparse/gpu/convolution.cu.h
paddle/phi/kernels/sparse/gpu/convolution.cu.h
+493
-0
paddle/phi/kernels/sparse/gpu/convolution_grad_kernel.cu
paddle/phi/kernels/sparse/gpu/convolution_grad_kernel.cu
+6
-7
paddle/phi/kernels/sparse/gpu/convolution_kernel.cu
paddle/phi/kernels/sparse/gpu/convolution_kernel.cu
+6
-502
paddle/phi/tests/kernels/test_sparse_conv3d_dev_api.cc
paddle/phi/tests/kernels/test_sparse_conv3d_dev_api.cc
+22
-20
未找到文件。
paddle/phi/kernels/funcs/sparse/convolution.h
浏览文件 @
2f5fb031
...
...
@@ -93,7 +93,7 @@ inline HOSTDEVICE void IndexToPoint(
}
inline
void
GetOutShape
(
const
DDim
&
x_dims
,
const
DDim
&
kernel_dim
s
,
const
std
::
vector
<
int
>&
kernel_size
s
,
const
std
::
vector
<
int
>&
paddings
,
const
std
::
vector
<
int
>&
dilations
,
const
std
::
vector
<
int
>&
strides
,
...
...
@@ -102,17 +102,17 @@ inline void GetOutShape(const DDim& x_dims,
x_dims
.
size
(),
5
,
phi
::
errors
::
InvalidArgument
(
"the shape of x should be (N, D, H, W, C)"
));
PADDLE_ENFORCE_EQ
(
kernel_
dim
s
.
size
(),
PADDLE_ENFORCE_EQ
(
kernel_
size
s
.
size
(),
5
,
phi
::
errors
::
InvalidArgument
(
"the shape of kernel should be (D, H, W, C, OC)"
));
// infer out shape
(
*
out_dims
)[
0
]
=
x_dims
[
0
];
(
*
out_dims
)[
4
]
=
kernel_
dim
s
[
4
];
(
*
out_dims
)[
4
]
=
kernel_
size
s
[
4
];
for
(
int
i
=
1
;
i
<
4
;
i
++
)
{
(
*
out_dims
)[
i
]
=
(
x_dims
[
i
]
+
2
*
paddings
[
i
-
1
]
-
dilations
[
i
-
1
]
*
(
kernel_
dim
s
[
i
-
1
]
-
1
)
-
1
)
/
dilations
[
i
-
1
]
*
(
kernel_
size
s
[
i
-
1
]
-
1
)
-
1
)
/
strides
[
i
-
1
]
+
1
;
}
...
...
@@ -131,7 +131,7 @@ template <typename T, typename Context>
inline
void
SubmPreProcess
(
const
Context
&
dev_ctx
,
const
SparseCooTensor
&
x
,
const
DenseTensor
&
kernel
,
const
SparseCoo
Tensor
&
out_grad
,
const
Dense
Tensor
&
out_grad
,
const
int
in_channels
,
const
int
out_channels
,
const
int
half_kernel_size
,
...
...
@@ -142,11 +142,11 @@ inline void SubmPreProcess(const Context& dev_ctx,
blas
.
GEMM
(
CblasTrans
,
CblasNoTrans
,
x
.
non_zero_elements
().
dims
()[
1
],
out_grad
.
non_zero_elements
().
dims
()[
1
],
out_grad
.
dims
()[
1
],
x
.
non_zero_elements
().
dims
()[
0
],
static_cast
<
T
>
(
1
),
x
.
non_zero_elements
().
data
<
T
>
(),
out_grad
.
non_zero_elements
().
data
<
T
>
(),
out_grad
.
data
<
T
>
(),
static_cast
<
T
>
(
0
),
d_kernel_ptr
+
half_kernel_size
*
in_channels
*
out_channels
);
...
...
@@ -155,11 +155,11 @@ inline void SubmPreProcess(const Context& dev_ctx,
T
*
x_grad_ptr
=
x_grad
->
data
<
T
>
();
blas
.
GEMM
(
CblasNoTrans
,
CblasTrans
,
out_grad
.
non_zero_elements
().
dims
()[
0
],
out_grad
.
dims
()[
0
],
in_channels
,
out_grad
.
non_zero_elements
().
dims
()[
1
],
out_grad
.
dims
()[
1
],
static_cast
<
T
>
(
1
),
out_grad
.
non_zero_elements
().
data
<
T
>
(),
out_grad
.
data
<
T
>
(),
kernel
.
data
<
T
>
()
+
half_kernel_size
*
in_channels
*
out_channels
,
static_cast
<
T
>
(
0
),
x_grad_ptr
);
...
...
paddle/phi/kernels/sparse/convolution_grad_kernel.h
浏览文件 @
2f5fb031
...
...
@@ -27,7 +27,7 @@ void Conv3dGradKernel(const Context& dev_ctx,
const
SparseCooTensor
&
x
,
const
DenseTensor
&
rulebook
,
const
DenseTensor
&
kernel
,
const
SparseCoo
Tensor
&
out_grad
,
const
Dense
Tensor
&
out_grad
,
const
std
::
vector
<
int
>&
paddings
,
const
std
::
vector
<
int
>&
dilations
,
const
std
::
vector
<
int
>&
strides
,
...
...
@@ -41,7 +41,7 @@ std::vector<DenseTensor> Conv3dGrad(const Context& dev_ctx,
const
SparseCooTensor
&
x
,
const
DenseTensor
&
rulebook
,
const
DenseTensor
&
kernel
,
const
SparseCoo
Tensor
&
out_grad
,
const
Dense
Tensor
&
out_grad
,
const
std
::
vector
<
int
>&
paddings
,
const
std
::
vector
<
int
>&
dilations
,
const
std
::
vector
<
int
>&
strides
,
...
...
paddle/phi/kernels/sparse/cpu/convolution.h
浏览文件 @
2f5fb031
...
...
@@ -34,7 +34,7 @@ using Dims4D = phi::funcs::sparse::Dims4D;
template
<
typename
T
,
typename
Context
>
void
ProductRuleBook
(
const
Context
&
dev_ctx
,
const
SparseCooTensor
&
x
,
const
DenseTensor
&
kernel
,
const
std
::
vector
<
int
>&
kernel_sizes
,
const
std
::
vector
<
int
>&
paddings
,
const
std
::
vector
<
int
>&
dilations
,
const
std
::
vector
<
int
>&
strides
,
...
...
@@ -42,19 +42,19 @@ void ProductRuleBook(const Context& dev_ctx,
const
bool
subm
,
DenseTensor
*
rulebook
,
DenseTensor
*
counter_per_kernel
)
{
const
auto
&
kernel_dims
=
kernel
.
dims
();
const
int64_t
non_zero_num
=
x
.
nnz
();
const
auto
&
non_zero_indices
=
x
.
non_zero_indices
();
const
int
*
indices_ptr
=
non_zero_indices
.
data
<
int
>
();
int
*
counter_ptr
=
counter_per_kernel
->
data
<
int
>
();
int
kernel_size
=
kernel_
dims
[
0
]
*
kernel_dims
[
1
]
*
kernel_dim
s
[
2
];
int
kernel_size
=
kernel_
sizes
[
0
]
*
kernel_sizes
[
1
]
*
kernel_size
s
[
2
];
memset
(
counter_ptr
,
0
,
kernel_size
*
sizeof
(
int
));
int
rulebook_len
=
0
;
// calc the rulebook_len
const
auto
&
x_dims
=
x
.
dims
();
const
Dims4D
c_x_dims
(
x_dims
[
0
],
x_dims
[
3
],
x_dims
[
2
],
x_dims
[
1
]);
const
Dims4D
c_kernel_dims
(
1
,
kernel_dims
[
2
],
kernel_dims
[
1
],
kernel_dims
[
0
]);
const
Dims4D
c_kernel_dims
(
1
,
kernel_sizes
[
2
],
kernel_sizes
[
1
],
kernel_sizes
[
0
]);
const
Dims4D
c_out_dims
(
out_dims
[
0
],
out_dims
[
3
],
out_dims
[
2
],
out_dims
[
1
]);
const
Dims4D
c_paddings
(
1
,
paddings
[
2
],
paddings
[
1
],
paddings
[
0
]);
const
Dims4D
c_strides
(
1
,
strides
[
2
],
strides
[
1
],
strides
[
0
]);
...
...
@@ -75,9 +75,9 @@ void ProductRuleBook(const Context& dev_ctx,
auto
f_calc_rulebook
=
[
&
](
int
*
rulebook_ptr
)
{
int
kernel_index
=
0
,
rulebook_index
=
0
;
for
(
int
kz
=
0
;
kz
<
kernel_
dim
s
[
0
];
kz
++
)
{
for
(
int
ky
=
0
;
ky
<
kernel_
dim
s
[
1
];
ky
++
)
{
for
(
int
kx
=
0
;
kx
<
kernel_
dim
s
[
2
];
kx
++
)
{
for
(
int
kz
=
0
;
kz
<
kernel_
size
s
[
0
];
kz
++
)
{
for
(
int
ky
=
0
;
ky
<
kernel_
size
s
[
1
];
ky
++
)
{
for
(
int
kx
=
0
;
kx
<
kernel_
size
s
[
2
];
kx
++
)
{
++
kernel_index
;
for
(
int64_t
i
=
0
;
i
<
non_zero_num
;
i
++
)
{
int
batch
=
indices_ptr
[
i
];
...
...
paddle/phi/kernels/sparse/cpu/convolution_grad_kernel.cc
浏览文件 @
2f5fb031
...
...
@@ -33,7 +33,7 @@ void Conv3dGradKernel(const Context& dev_ctx,
const
SparseCooTensor
&
x
,
const
DenseTensor
&
rulebook
,
const
DenseTensor
&
kernel
,
const
SparseCoo
Tensor
&
out_grad
,
const
Dense
Tensor
&
out_grad
,
const
std
::
vector
<
int
>&
paddings
,
const
std
::
vector
<
int
>&
dilations
,
const
std
::
vector
<
int
>&
strides
,
...
...
@@ -113,7 +113,7 @@ void Conv3dGradKernel(const Context& dev_ctx,
rulebook_len
,
in_channels
,
in_features_ptr
);
Gather
<
T
>
(
out_grad
.
non_zero_elements
().
data
<
T
>
(),
Gather
<
T
>
(
out_grad
.
data
<
T
>
(),
rulebook_ptr
+
rulebook_len
*
2
,
rulebook_len
,
out_channels
,
...
...
paddle/phi/kernels/sparse/cpu/convolution_kernel.cc
浏览文件 @
2f5fb031
...
...
@@ -44,8 +44,13 @@ void Conv3dKernel(const Context& dev_ctx,
const
auto
&
kernel_dims
=
kernel
.
dims
();
int
kernel_size
=
kernel_dims
[
0
]
*
kernel_dims
[
1
]
*
kernel_dims
[
2
];
DDim
out_dims
=
{
1
,
1
,
1
,
1
,
1
};
std
::
vector
<
int
>
kernel_sizes
(
kernel_dims
.
size
());
for
(
int
i
=
0
;
i
<
kernel_dims
.
size
();
i
++
)
{
kernel_sizes
[
i
]
=
kernel_dims
[
i
];
}
phi
::
funcs
::
sparse
::
GetOutShape
(
x_dims
,
kernel_
dim
s
,
paddings
,
dilations
,
strides
,
&
out_dims
);
x_dims
,
kernel_
size
s
,
paddings
,
dilations
,
strides
,
&
out_dims
);
const
int
in_channels
=
kernel_dims
[
3
];
const
int
out_channels
=
kernel_dims
[
4
];
...
...
@@ -63,7 +68,7 @@ void Conv3dKernel(const Context& dev_ctx,
ProductRuleBook
<
T
,
Context
>
(
dev_ctx
,
x
,
kernel
,
kernel
_sizes
,
subm_paddings
,
dilations
,
subm_strides
,
...
...
paddle/phi/kernels/sparse/gpu/convolution.cu.h
浏览文件 @
2f5fb031
此差异已折叠。
点击以展开。
paddle/phi/kernels/sparse/gpu/convolution_grad_kernel.cu
浏览文件 @
2f5fb031
...
...
@@ -38,7 +38,7 @@ void Conv3dGradKernel(const Context& dev_ctx,
const
SparseCooTensor
&
x
,
const
DenseTensor
&
rulebook
,
const
DenseTensor
&
kernel
,
const
SparseCoo
Tensor
&
out_grad
,
const
Dense
Tensor
&
out_grad
,
const
std
::
vector
<
int
>&
paddings
,
const
std
::
vector
<
int
>&
dilations
,
const
std
::
vector
<
int
>&
strides
,
...
...
@@ -140,8 +140,7 @@ void Conv3dGradKernel(const Context& dev_ctx,
GatherKernel
<
T
,
int
><<<
config
.
block_per_grid
.
x
,
config
.
thread_per_block
.
x
,
0
,
dev_ctx
.
stream
()
>>>
(
out_grad
.
non_zero_elements
().
data
<
T
>
(),
dev_ctx
.
stream
()
>>>
(
out_grad
.
data
<
T
>
(),
rulebook_ptr
+
rulebook_len
*
2
,
out_grad_features_ptr
,
rulebook_len
,
...
...
paddle/phi/kernels/sparse/gpu/convolution_kernel.cu
浏览文件 @
2f5fb031
此差异已折叠。
点击以展开。
paddle/phi/tests/kernels/test_sparse_conv3d_dev_api.cc
浏览文件 @
2f5fb031
...
...
@@ -132,11 +132,12 @@ void TestConv3dBase(const std::vector<int>& indices,
f_verify
(
out
.
non_zero_elements
().
data
<
T
>
(),
correct_out_features
);
if
(
backward
)
{
std
::
vector
<
DenseTensor
>
grads
=
sparse
::
Conv3dGrad
<
T
>
(
dev_ctx_cpu
,
std
::
vector
<
DenseTensor
>
grads
=
sparse
::
Conv3dGrad
<
T
>
(
dev_ctx_cpu
,
x_tensor
,
rulebook
,
kernel_tensor
,
out
,
out
.
non_zero_elements
()
,
paddings
,
dilations
,
strides
,
...
...
@@ -231,11 +232,12 @@ void TestConv3dBase(const std::vector<int>& indices,
f_verify
(
h_features_tensor
.
data
<
T
>
(),
correct_out_features
);
if
(
backward
)
{
std
::
vector
<
DenseTensor
>
grads
=
sparse
::
Conv3dGrad
<
T
>
(
dev_ctx_gpu
,
std
::
vector
<
DenseTensor
>
grads
=
sparse
::
Conv3dGrad
<
T
>
(
dev_ctx_gpu
,
d_x_tensor
,
d_rulebook
,
d_kernel_tensor
,
d_out
,
d_out
.
non_zero_elements
()
,
paddings
,
dilations
,
strides
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录