Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
7316018d
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看板
未验证
提交
7316018d
编写于
8月 15, 2021
作者:
R
ronnywang
提交者:
GitHub
8月 16, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[NPU] add p_norm_op_npu (#34695)
* add p_norm_op_npu * remove p_norm_grad op * update
上级
2cd05d5d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
252 addition
and
0 deletion
+252
-0
paddle/fluid/operators/p_norm_op_npu.cc
paddle/fluid/operators/p_norm_op_npu.cc
+92
-0
python/paddle/fluid/tests/unittests/npu/test_p_norm_op_npu.py
...on/paddle/fluid/tests/unittests/npu/test_p_norm_op_npu.py
+160
-0
未找到文件。
paddle/fluid/operators/p_norm_op_npu.cc
0 → 100644
浏览文件 @
7316018d
/* Copyright (c) 2021 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/fluid/operators/p_norm_op.h"
#include "paddle/fluid/operators/npu_op_runner.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
DeviceContext
,
typename
T
>
class
PnormNPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
in_x
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
*
out_norm
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
out_norm
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
float
porder
=
ctx
.
Attr
<
float
>
(
"porder"
);
int
axis
=
ctx
.
Attr
<
int
>
(
"axis"
);
bool
keepdim
=
ctx
.
Attr
<
bool
>
(
"keepdim"
);
auto
xdim
=
in_x
->
dims
();
if
(
axis
<
0
)
axis
=
xdim
.
size
()
+
axis
;
auto
stream
=
ctx
.
template
device_context
<
paddle
::
platform
::
NPUDeviceContext
>()
.
stream
();
int
p
=
0
;
bool
combine_op
=
!
(
porder
==
0
||
porder
==
INFINITY
||
porder
==
-
INFINITY
);
if
(
porder
==
INFINITY
)
{
p
=
INT_MAX
;
}
else
if
(
porder
==
-
INFINITY
)
{
p
=
INT_MIN
;
}
else
{
p
=
static_cast
<
int
>
(
porder
);
float
t
=
0
;
float
diff
=
abs
(
std
::
modf
(
porder
,
&
t
));
if
(
diff
<
1e-5
)
{
combine_op
=
false
;
}
}
if
(
!
combine_op
)
{
const
auto
&
runner
=
NpuOpRunner
(
"LpNorm"
,
{
*
in_x
},
{
*
out_norm
},
{{
"p"
,
p
},
{
"axes"
,
std
::
vector
<
int32_t
>
({
axis
})},
{
"keep_dims"
,
keepdim
}});
runner
.
Run
(
stream
);
}
else
{
Tensor
tmp_x
;
tmp_x
.
mutable_data
<
T
>
(
xdim
,
ctx
.
GetPlace
());
const
auto
&
power_runner1
=
NpuOpRunner
(
"Power"
,
{
*
in_x
},
{
tmp_x
},
{{
"power"
,
porder
},
{
"scale"
,
1.0
f
},
{
"shift"
,
0.0
f
}});
power_runner1
.
Run
(
stream
);
const
auto
&
reduce_runner
=
NpuOpRunner
(
"ReduceSumD"
,
{
tmp_x
},
{
*
out_norm
},
{{
"axes"
,
std
::
vector
<
int32_t
>
({
axis
})},
{
"keep_dims"
,
keepdim
}});
reduce_runner
.
Run
(
stream
);
const
auto
&
power_runner2
=
NpuOpRunner
(
"Power"
,
{
*
out_norm
},
{
*
out_norm
},
{{
"power"
,
1
/
porder
},
{
"scale"
,
1.0
f
},
{
"shift"
,
0.0
f
}});
power_runner2
.
Run
(
stream
);
}
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
namespace
plat
=
paddle
::
platform
;
REGISTER_OP_NPU_KERNEL
(
p_norm
,
ops
::
PnormNPUKernel
<
plat
::
NPUDeviceContext
,
float
>
,
ops
::
PnormNPUKernel
<
plat
::
NPUDeviceContext
,
plat
::
float16
>
);
python/paddle/fluid/tests/unittests/npu/test_p_norm_op_npu.py
0 → 100644
浏览文件 @
7316018d
# Copyright (c) 2021 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.
import
sys
import
unittest
import
numpy
as
np
sys
.
path
.
append
(
".."
)
import
paddle
from
op_test
import
OpTest
from
test_norm_all
import
p_norm
paddle
.
enable_static
()
class
TestPnormOp
(
OpTest
):
def
set_npu
(
self
):
self
.
__class__
.
use_npu
=
True
self
.
__class__
.
no_need_check_grad
=
True
def
setUp
(
self
):
self
.
set_npu
()
self
.
op_type
=
"p_norm"
self
.
init_test_case
()
x
=
(
np
.
random
.
random
(
self
.
shape
)
+
0.5
).
astype
(
self
.
dtype
)
norm
=
p_norm
(
x
,
self
.
axis
,
self
.
porder
,
self
.
keepdim
)
self
.
inputs
=
{
'X'
:
x
}
self
.
attrs
=
{
'epsilon'
:
self
.
epsilon
,
'axis'
:
self
.
axis
,
'keepdim'
:
self
.
keepdim
,
'porder'
:
float
(
self
.
porder
)
}
self
.
outputs
=
{
'Out'
:
norm
}
self
.
gradient
=
self
.
calc_gradient
()
def
test_check_output
(
self
):
if
self
.
dtype
==
"float16"
:
self
.
check_output_with_place
(
paddle
.
NPUPlace
(
0
),
atol
=
5e-3
)
else
:
self
.
check_output_with_place
(
paddle
.
NPUPlace
(
0
))
def
init_test_case
(
self
):
self
.
shape
=
[
2
,
3
,
4
,
5
]
self
.
axis
=
1
self
.
epsilon
=
1e-12
self
.
porder
=
2.0
self
.
keepdim
=
False
self
.
init_dtype
()
def
init_dtype
(
self
):
self
.
dtype
=
"float32"
def
calc_gradient
(
self
):
self
.
attrs
=
{
'epsilon'
:
self
.
epsilon
,
'axis'
:
self
.
axis
,
'keepdim'
:
self
.
keepdim
,
'porder'
:
float
(
self
.
porder
)
}
x
=
self
.
inputs
[
"X"
]
porder
=
self
.
attrs
[
"porder"
]
axis
=
self
.
attrs
[
"axis"
]
if
porder
==
0
:
grad
=
np
.
zeros
(
x
.
shape
).
astype
(
x
.
dtype
)
elif
porder
in
[
float
(
"inf"
),
float
(
"-inf"
)]:
norm
=
p_norm
(
x
,
axis
=
axis
,
porder
=
porder
,
keepdims
=
True
)
x_abs
=
np
.
abs
(
x
)
grad
=
np
.
sign
(
x
)
grad
[
x_abs
!=
norm
]
=
0.0
else
:
norm
=
p_norm
(
x
,
axis
=
axis
,
porder
=
porder
,
keepdims
=
True
)
grad
=
np
.
power
(
norm
,
1
-
porder
)
*
np
.
power
(
np
.
abs
(
x
),
porder
-
1
)
*
np
.
sign
(
x
)
numel
=
1
for
s
in
x
.
shape
:
numel
*=
s
numel
/=
x
.
shape
[
axis
]
return
[
grad
.
astype
(
x
.
dtype
)
*
1
/
numel
]
class
TestPnormOp2
(
TestPnormOp
):
def
init_test_case
(
self
):
self
.
shape
=
[
3
,
20
,
3
]
self
.
axis
=
2
self
.
epsilon
=
1e-12
self
.
porder
=
2.0
self
.
keepdim
=
True
self
.
init_dtype
()
class
TestPnormOp3
(
TestPnormOp
):
def
init_test_case
(
self
):
self
.
shape
=
[
3
,
20
,
3
]
self
.
axis
=
2
self
.
epsilon
=
1e-12
self
.
porder
=
np
.
inf
self
.
keepdim
=
True
self
.
init_dtype
()
class
TestPnormOp4
(
TestPnormOp3
):
def
init_test_case
(
self
):
self
.
shape
=
[
3
,
20
,
3
]
self
.
axis
=
2
self
.
epsilon
=
1e-12
self
.
porder
=
-
np
.
inf
self
.
keepdim
=
True
self
.
init_dtype
()
class
TestPnormOp5
(
TestPnormOp3
):
def
init_test_case
(
self
):
self
.
shape
=
[
3
,
20
,
3
]
self
.
axis
=
2
self
.
epsilon
=
1e-12
self
.
porder
=
0
self
.
keepdim
=
True
self
.
init_dtype
()
class
TestPnormOpfp16
(
TestPnormOp
):
def
init_dtype
(
self
):
self
.
dtype
=
"float16"
class
TestPnormOp2fp16
(
TestPnormOp2
):
def
init_dtype
(
self
):
self
.
dtype
=
"float16"
class
TestPnormOp3fp16
(
TestPnormOp3
):
def
init_dtype
(
self
):
self
.
dtype
=
"float16"
class
TestPnormOp4fp16
(
TestPnormOp4
):
def
init_dtype
(
self
):
self
.
dtype
=
"float16"
class
TestPnormOp5fp16
(
TestPnormOp5
):
def
init_dtype
(
self
):
self
.
dtype
=
"float16"
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录