Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
02346930
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看板
未验证
提交
02346930
编写于
3月 13, 2021
作者:
L
Leo Chen
提交者:
GitHub
3月 13, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix gather_grad bug (#31607)
上级
5e851bff
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
60 addition
and
8 deletion
+60
-8
paddle/fluid/operators/gather_op_npu.cc
paddle/fluid/operators/gather_op_npu.cc
+6
-8
python/paddle/fluid/tests/unittests/npu/test_gather_op_npu.py
...on/paddle/fluid/tests/unittests/npu/test_gather_op_npu.py
+54
-0
未找到文件。
paddle/fluid/operators/gather_op_npu.cc
浏览文件 @
02346930
...
...
@@ -19,6 +19,7 @@ limitations under the License. */
#include "paddle/fluid/framework/tensor_util.h"
#include "paddle/fluid/operators/kron_op.h"
#include "paddle/fluid/operators/npu_op_runner.h"
#include "paddle/fluid/platform/npu_info.h"
namespace
paddle
{
namespace
operators
{
...
...
@@ -65,20 +66,17 @@ class GatherGradOpNPUKernel : public framework::OpKernel<T> {
.
stream
();
// step2: ZerosLike x in device
Tensor
*
tmp_zerox
=
const_cast
<
Tensor
*>
(
x
);
Tensor
zeroslike_xout
(
x
->
type
());
zeroslike_xout
.
Resize
(
x
->
dims
());
zeroslike_xout
.
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
p
=
zeroslike_xout
.
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
runner_zeroslike
=
NpuOpRunner
(
"ZerosLike"
,
{
*
x
},
{
zeroslike_xout
},
{});
runner_zeroslike
.
Run
(
stream
);
tmp_zerox
=
&
zeroslike_xout
;
platform
::
NPUMemsetAsync
(
static_cast
<
void
*>
(
p
),
0
,
zeroslike_xout
.
numel
()
*
sizeof
(
T
),
stream
);
// step3: scatter(x_grad)
dx
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
runner_scatter
=
NpuOpRunner
(
"TensorScatterUpdate"
,
{
*
tmp_zerox
,
*
index
,
*
dout
},
{
*
dx
},
{});
auto
runner_scatter
=
NpuOpRunner
(
"TensorScatterUpdate"
,
{
zeroslike_xout
,
*
index
,
*
dout
},
{
*
dx
},
{});
runner_scatter
.
Run
(
stream
);
}
};
...
...
python/paddle/fluid/tests/unittests/npu/test_gather_op_npu.py
浏览文件 @
02346930
...
...
@@ -23,6 +23,7 @@ import paddle
import
paddle.fluid
as
fluid
paddle
.
enable_static
()
SEED
=
2021
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
...
...
@@ -105,5 +106,58 @@ class TestGatherAPI(unittest.TestCase):
pass
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
"core is not compiled with NPU"
)
class
TestPowNet
(
unittest
.
TestCase
):
def
_test
(
self
,
run_npu
=
True
):
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
SEED
np
.
random
.
seed
(
SEED
)
a_np
=
np
.
random
.
random
(
size
=
(
8192
,
768
)).
astype
(
'float32'
)
index_np
=
np
.
random
.
randint
(
0
,
8192
,
size
=
(
1232
,
1
)).
astype
(
'int32'
)
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
a
=
paddle
.
static
.
data
(
name
=
"a"
,
shape
=
[
8192
,
768
],
dtype
=
'float32'
)
index
=
paddle
.
static
.
data
(
name
=
"index"
,
shape
=
[
1232
,
1
],
dtype
=
'int32'
)
a
.
stop_gradient
=
False
b
=
paddle
.
gather
(
a
,
index
)
loss
=
fluid
.
layers
.
reduce_mean
(
b
)
sgd
=
fluid
.
optimizer
.
SGD
(
learning_rate
=
0.01
)
sgd
.
minimize
(
loss
)
if
run_npu
:
place
=
paddle
.
NPUPlace
(
0
)
else
:
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
print
(
"Start run on {}"
.
format
(
place
))
for
epoch
in
range
(
100
):
pred_res
,
loss_res
=
exe
.
run
(
main_prog
,
feed
=
{
"a"
:
a_np
,
"index"
:
index_np
},
fetch_list
=
[
b
,
loss
])
if
epoch
%
10
==
0
:
print
(
"Epoch {} | Prediction[0]: {}, Loss: {}"
.
format
(
epoch
,
pred_res
[
0
],
loss_res
[
0
]))
return
pred_res
,
loss_res
def
test_npu
(
self
):
npu_pred
,
npu_loss
=
self
.
_test
(
True
)
cpu_pred
,
cpu_loss
=
self
.
_test
(
False
)
self
.
assertTrue
(
np
.
allclose
(
npu_pred
,
cpu_pred
))
self
.
assertTrue
(
np
.
allclose
(
npu_loss
,
cpu_loss
))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录