Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
da33f7b0
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看板
未验证
提交
da33f7b0
编写于
9月 14, 2022
作者:
Z
zhangkaihuo
提交者:
GitHub
9月 14, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Sparse]Sparse add support gpu (#45974)
上级
2d45f68f
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
175 addition
and
1 deletion
+175
-1
paddle/phi/kernels/sparse/elementwise_grad_kernel.h
paddle/phi/kernels/sparse/elementwise_grad_kernel.h
+1
-0
paddle/phi/kernels/sparse/gpu/elementwise_grad_kernel.cu
paddle/phi/kernels/sparse/gpu/elementwise_grad_kernel.cu
+56
-0
paddle/phi/kernels/sparse/gpu/elementwise_kernel.cu
paddle/phi/kernels/sparse/gpu/elementwise_kernel.cu
+88
-0
python/paddle/fluid/tests/unittests/test_sparse_elementwise_op.py
...addle/fluid/tests/unittests/test_sparse_elementwise_op.py
+30
-1
未找到文件。
paddle/phi/kernels/sparse/elementwise_grad_kernel.h
浏览文件 @
da33f7b0
...
...
@@ -15,6 +15,7 @@ limitations under the License. */
#pragma once
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/sparse_coo_tensor.h"
#include "paddle/phi/core/sparse_csr_tensor.h"
#include "paddle/phi/kernels/empty_kernel.h"
...
...
paddle/phi/kernels/sparse/gpu/elementwise_grad_kernel.cu
0 → 100644
浏览文件 @
da33f7b0
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
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/phi/kernels/sparse/elementwise_grad_kernel.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/tensor_utils.h"
#include "paddle/phi/kernels/sparse/empty_kernel.h"
namespace
phi
{
namespace
sparse
{
template
<
typename
T
,
typename
Context
>
void
ElementWiseAddCooGradKernel
(
const
Context
&
dev_ctx
,
const
SparseCooTensor
&
x
,
const
SparseCooTensor
&
y
,
const
SparseCooTensor
&
dout
,
SparseCooTensor
*
dx
,
SparseCooTensor
*
dy
)
{
if
(
dx
)
{
EmptyLikeCooKernel
<
T
,
Context
>
(
dev_ctx
,
x
,
dx
);
Copy
(
dev_ctx
,
dout
,
dev_ctx
.
GetPlace
(),
false
,
dx
);
}
if
(
dy
)
{
EmptyLikeCooKernel
<
T
,
Context
>
(
dev_ctx
,
y
,
dy
);
Copy
(
dev_ctx
,
dout
,
dev_ctx
.
GetPlace
(),
false
,
dy
);
}
}
}
// namespace sparse
}
// namespace phi
PD_REGISTER_KERNEL
(
add_coo_coo_grad
,
GPU
,
ALL_LAYOUT
,
phi
::
sparse
::
ElementWiseAddCooGradKernel
,
float
,
double
,
int16_t
,
int
,
int64_t
,
phi
::
dtype
::
float16
)
{
kernel
->
InputAt
(
0
).
SetDataLayout
(
phi
::
DataLayout
::
SPARSE_COO
);
kernel
->
InputAt
(
1
).
SetDataLayout
(
phi
::
DataLayout
::
SPARSE_COO
);
}
paddle/phi/kernels/sparse/gpu/elementwise_kernel.cu
0 → 100644
浏览文件 @
da33f7b0
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
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 <thrust/equal.h>
#include <thrust/execution_policy.h>
#include "paddle/phi/kernels/elementwise_add_kernel.h"
#include "paddle/phi/kernels/sparse/elementwise_kernel.h"
#include "paddle/phi/kernels/sparse/empty_kernel.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/visit_type.h"
namespace
phi
{
namespace
sparse
{
template
<
typename
T
,
typename
IntT
>
void
ElementWiseAddCooGPUKernel
(
const
GPUContext
&
dev_ctx
,
const
SparseCooTensor
&
x
,
const
SparseCooTensor
&
y
,
SparseCooTensor
*
out
)
{
const
auto
&
x_indices
=
x
.
indices
();
const
auto
&
y_indices
=
y
.
indices
();
PADDLE_ENFORCE_EQ
(
x_indices
.
numel
(),
y_indices
.
numel
(),
phi
::
errors
::
PreconditionNotMet
(
"The numel of x.indices() and y.indices() should be equal"
));
const
IntT
*
x_indices_ptr
=
x_indices
.
data
<
IntT
>
();
const
IntT
*
y_indices_ptr
=
y_indices
.
data
<
IntT
>
();
#ifdef PADDLE_WITH_HIP
bool
is_same
=
thrust
::
equal
(
thrust
::
hip
::
par
.
on
(
dev_ctx
.
stream
()),
#else
bool
is_same
=
thrust
::
equal
(
thrust
::
cuda
::
par
.
on
(
dev_ctx
.
stream
()),
#endif
x_indices_ptr
,
x_indices_ptr
+
x_indices
.
numel
(),
y_indices_ptr
);
PADDLE_ENFORCE_EQ
(
is_same
,
true
,
phi
::
errors
::
PreconditionNotMet
(
"Currently, ElementWiseAddCooKernel only supports the case "
"where x and y have the same indices"
));
EmptyLikeCooKernel
<
T
,
GPUContext
>
(
dev_ctx
,
x
,
out
);
phi
::
AddKernel
<
T
,
GPUContext
>
(
dev_ctx
,
x
.
values
(),
y
.
values
(),
out
->
mutable_values
());
}
template
<
typename
T
,
typename
Context
>
void
ElementWiseAddCooKernel
(
const
Context
&
dev_ctx
,
const
SparseCooTensor
&
x
,
const
SparseCooTensor
&
y
,
SparseCooTensor
*
out
)
{
PD_VISIT_BASE_INTEGRAL_TYPES
(
x
.
indices
().
dtype
(),
"VerifyIndices"
,
([
&
]
{
ElementWiseAddCooGPUKernel
<
T
,
data_t
>
(
dev_ctx
,
x
,
y
,
out
);
}));
}
}
// namespace sparse
}
// namespace phi
PD_REGISTER_KERNEL
(
add_coo_coo
,
GPU
,
ALL_LAYOUT
,
phi
::
sparse
::
ElementWiseAddCooKernel
,
float
,
double
,
int16_t
,
int
,
int64_t
,
phi
::
dtype
::
float16
)
{
kernel
->
InputAt
(
0
).
SetDataLayout
(
phi
::
DataLayout
::
SPARSE_COO
);
kernel
->
InputAt
(
1
).
SetDataLayout
(
phi
::
DataLayout
::
SPARSE_COO
);
}
python/paddle/fluid/tests/unittests/test_sparse_elementwise_op.py
浏览文件 @
da33f7b0
...
...
@@ -18,7 +18,7 @@ from operator import __add__, __sub__, __mul__, __truediv__
import
numpy
as
np
import
paddle
from
paddle.fluid.framework
import
_test_eager_guard
import
paddle.incubate.sparse
as
sparse
op_list
=
[
__add__
,
__sub__
,
__mul__
,
__truediv__
]
...
...
@@ -134,6 +134,35 @@ class TestSparseElementWiseAPI(unittest.TestCase):
for
op
in
op_list
:
self
.
func_test_coo
(
op
)
def
test_add_same_indices
(
self
):
indices_data
=
[[
0
,
1
],
[
0
,
3
]]
values1_data
=
[[
1.0
],
[
2.0
]]
values2_data
=
[[
1.0
],
[
2.0
]]
shape
=
[
2
,
4
,
2
]
sp_a
=
sparse
.
sparse_coo_tensor
(
indices_data
,
values1_data
,
shape
,
stop_gradient
=
False
)
sp_b
=
sparse
.
sparse_coo_tensor
(
indices_data
,
values2_data
,
shape
,
stop_gradient
=
False
)
values1
=
paddle
.
to_tensor
(
values1_data
,
stop_gradient
=
False
)
values2
=
paddle
.
to_tensor
(
values2_data
,
stop_gradient
=
False
)
#c.values() = a.values() + b.values()
sp_c
=
sparse
.
add
(
sp_a
,
sp_b
)
sp_c
.
backward
()
ref_c
=
values1
+
values2
ref_c
.
backward
()
np
.
testing
.
assert_allclose
(
sp_c
.
values
().
numpy
(),
ref_c
.
numpy
())
np
.
testing
.
assert_allclose
(
sp_a
.
grad
.
values
().
numpy
(),
values1
.
grad
.
numpy
())
np
.
testing
.
assert_allclose
(
sp_b
.
grad
.
values
().
numpy
(),
values2
.
grad
.
numpy
())
if
__name__
==
"__main__"
:
paddle
.
device
.
set_device
(
'cpu'
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录