Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
8757fc5b
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看板
未验证
提交
8757fc5b
编写于
10月 18, 2021
作者:
Q
Qi Li
提交者:
GitHub
10月 18, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[NPU] fix dtype for arg_max, test=develop (#36457)
上级
3845afff
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
139 addition
and
37 deletion
+139
-37
paddle/fluid/operators/arg_max_op_npu.cc
paddle/fluid/operators/arg_max_op_npu.cc
+37
-20
paddle/fluid/operators/npu_op_runner.cc
paddle/fluid/operators/npu_op_runner.cc
+15
-0
paddle/fluid/operators/npu_op_runner.h
paddle/fluid/operators/npu_op_runner.h
+6
-0
python/paddle/fluid/tests/unittests/npu/test_arg_max_op_npu.py
...n/paddle/fluid/tests/unittests/npu/test_arg_max_op_npu.py
+71
-12
python/paddle/nn/functional/loss.py
python/paddle/nn/functional/loss.py
+10
-5
未找到文件。
paddle/fluid/operators/arg_max_op_npu.cc
浏览文件 @
8757fc5b
...
@@ -17,30 +17,49 @@ limitations under the Licnse. */
...
@@ -17,30 +17,49 @@ limitations under the Licnse. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
using
Tensor
=
framework
::
Tensor
;
using
NPUDeviceContext
=
platform
::
NPUDeviceContext
;
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
T
>
class
ArgMaxNPUKernel
:
public
framework
::
OpKernel
<
T
>
{
struct
VisitDataArgNPUMaxFunctor
{
public:
const
framework
::
ExecutionContext
&
ctx
;
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
int64_t
axis
=
ctx
.
Attr
<
int64_t
>
(
"axis"
);
auto
dtype
=
ctx
.
Attr
<
int
>
(
"dtype"
);
auto
*
out
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
explicit
VisitDataArgNPUMaxFunctor
(
const
framework
::
ExecutionContext
&
ctx
)
out
->
mutable_data
<
int32_t
>
(
ctx
.
GetPlace
());
:
ctx
(
ctx
)
{}
template
<
typename
Tout
>
void
apply
()
const
{
auto
&
x
=
*
(
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
));
auto
&
out
=
*
(
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
));
out
.
template
mutable_data
<
Tout
>(
ctx
.
GetPlace
());
auto
axis
=
ctx
.
Attr
<
int64_t
>
(
"axis"
);
auto
dtype
=
ctx
.
Attr
<
int
>
(
"dtype"
);
auto
stream
=
ctx
.
template
device_context
<
NPUDeviceContext
>().
stream
();
NpuOpRunner
runner
;
NpuOpRunner
runner
;
runner
.
SetType
(
"ArgMaxV2"
)
runner
.
SetType
(
"ArgMaxV2"
)
.
AddInput
(
*
x
)
.
AddInput
(
x
)
.
AddInput
(
std
::
vector
<
int64_t
>
{
axis
})
.
AddInput
(
std
::
vector
<
int64_t
>
{
axis
})
.
AddOutput
(
*
out
)
.
AddOutput
(
out
)
.
AddAttr
(
"dtype"
,
dtype
);
.
AddAttrDataType
(
"dtype"
,
dtype
)
.
Run
(
stream
);
}
};
auto
stream
=
template
<
typename
T
>
ctx
.
template
device_context
<
paddle
::
platform
::
NPUDeviceContext
>()
class
ArgMaxNPUKernel
:
public
framework
::
OpKernel
<
T
>
{
.
stream
();
public:
runner
.
Run
(
stream
);
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
&
dtype
=
ctx
.
Attr
<
int
>
(
"dtype"
);
if
(
dtype
<
0
)
{
framework
::
VisitDataTypeTiny
(
static_cast
<
framework
::
proto
::
VarType
::
Type
>
(
framework
::
proto
::
VarType
::
INT64
),
VisitDataArgNPUMaxFunctor
<
T
>
(
ctx
));
return
;
}
framework
::
VisitDataTypeTiny
(
static_cast
<
framework
::
proto
::
VarType
::
Type
>
(
dtype
),
VisitDataArgNPUMaxFunctor
<
T
>
(
ctx
));
}
}
};
};
...
@@ -48,7 +67,5 @@ class ArgMaxNPUKernel : public framework::OpKernel<T> {
...
@@ -48,7 +67,5 @@ class ArgMaxNPUKernel : public framework::OpKernel<T> {
}
// namespace paddle
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_NPU_KERNEL
(
REGISTER_OP_NPU_KERNEL
(
arg_max
,
ops
::
ArgMaxNPUKernel
<
float
>
,
arg_max
,
ops
::
ArgMaxNPUKernel
<
paddle
::
platform
::
NPUDeviceContext
,
float
>
,
ops
::
ArgMaxNPUKernel
<
paddle
::
platform
::
float16
>
);
ops
::
ArgMaxNPUKernel
<
paddle
::
platform
::
NPUDeviceContext
,
paddle
::
platform
::
float16
>
);
paddle/fluid/operators/npu_op_runner.cc
浏览文件 @
8757fc5b
...
@@ -188,6 +188,21 @@ NpuOpRunner &NpuOpRunner::AddAttr(const std::string &name,
...
@@ -188,6 +188,21 @@ NpuOpRunner &NpuOpRunner::AddAttr(const std::string &name,
return
*
this
;
return
*
this
;
}
}
NpuOpRunner
&
NpuOpRunner
::
AddAttrDataType
(
const
std
::
string
&
name
,
const
NPUAttribute
&
attr
)
{
PADDLE_ENFORCE_EQ
(
(
attr
.
type
()
==
typeid
(
int
)),
true
,
platform
::
errors
::
InvalidArgument
(
"Attr type is NOT equal to framework::proto::VarType::Type."
));
if
(
!
attr_
)
{
attr_
=
aclopCreateAttr
();
}
auto
dtype
=
ConvertToNpuDtype
(
static_cast
<
framework
::
proto
::
VarType
::
Type
>
(
BOOST_GET_CONST
(
int
,
attr
)));
PADDLE_ENFORCE_NPU_SUCCESS
(
aclopSetAttrDataType
(
attr_
,
name
.
c_str
(),
dtype
));
return
*
this
;
}
NpuOpRunner
&
NpuOpRunner
::
AddAttrs
(
const
NPUAttributeMap
&
attrs
)
{
NpuOpRunner
&
NpuOpRunner
::
AddAttrs
(
const
NPUAttributeMap
&
attrs
)
{
for
(
const
auto
&
pair
:
attrs
)
{
for
(
const
auto
&
pair
:
attrs
)
{
AddAttr
(
pair
.
first
,
pair
.
second
);
AddAttr
(
pair
.
first
,
pair
.
second
);
...
...
paddle/fluid/operators/npu_op_runner.h
浏览文件 @
8757fc5b
...
@@ -58,6 +58,12 @@ class NpuOpRunner {
...
@@ -58,6 +58,12 @@ class NpuOpRunner {
NpuOpRunner
&
AddAttr
(
const
std
::
string
&
name
,
const
NPUAttribute
&
attr
);
NpuOpRunner
&
AddAttr
(
const
std
::
string
&
name
,
const
NPUAttribute
&
attr
);
// NOTE(qili93): need to add indivisual api for aclopSetAttrDataType
// as typeid(aclDataType) and typeid(framework::proto::VarType::Type)
// always go to attr.type() == typeid(int) to call aclopSetAttrInt
NpuOpRunner
&
AddAttrDataType
(
const
std
::
string
&
name
,
const
NPUAttribute
&
attr
);
NpuOpRunner
&
AddAttrs
(
const
NPUAttributeMap
&
attrs
);
NpuOpRunner
&
AddAttrs
(
const
NPUAttributeMap
&
attrs
);
NpuOpRunner
&
AddInput
(
const
Tensor
&
tensor
);
NpuOpRunner
&
AddInput
(
const
Tensor
&
tensor
);
...
...
python/paddle/fluid/tests/unittests/npu/test_arg_max_op_npu.py
浏览文件 @
8757fc5b
#
Copyright (c) 2021
PaddlePaddle Authors. All Rights Reserved.
#
Copyright (c) 2018
PaddlePaddle Authors. All Rights Reserved.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing, software
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# distributed under the License is distributed on an "AS IS" BASIS,
...
@@ -20,30 +20,31 @@ import sys
...
@@ -20,30 +20,31 @@ import sys
sys
.
path
.
append
(
".."
)
sys
.
path
.
append
(
".."
)
from
op_test
import
OpTest
from
op_test
import
OpTest
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.core
as
core
import
paddle.fluid.core
as
core
from
paddle.fluid
import
Program
,
program_guard
paddle
.
enable_static
()
paddle
.
enable_static
()
class
BaseTestCase
(
OpTest
):
class
BaseTestCase
(
OpTest
):
def
set_npu
(
self
):
self
.
__class__
.
use_npu
=
True
self
.
place
=
paddle
.
NPUPlace
(
0
)
def
initTestCase
(
self
):
def
initTestCase
(
self
):
self
.
op_type
=
'arg_max'
self
.
op_type
=
'arg_max'
self
.
dims
=
(
3
,
4
)
self
.
dims
=
(
3
,
4
,
5
)
self
.
dtype
=
'float32'
self
.
dtype
=
'float32'
self
.
axis
=
1
self
.
axis
=
0
def
setUp
(
self
):
def
setUp
(
self
):
self
.
set_npu
()
self
.
initTestCase
()
self
.
initTestCase
()
self
.
__class__
.
use_npu
=
True
self
.
x
=
(
1000
*
np
.
random
.
random
(
self
.
dims
)).
astype
(
self
.
dtype
)
self
.
place
=
paddle
.
NPUPlace
(
0
)
np
.
random
.
seed
(
2021
)
self
.
x
=
(
np
.
random
.
random
(
self
.
dims
)).
astype
(
self
.
dtype
)
self
.
inputs
=
{
'X'
:
self
.
x
}
self
.
inputs
=
{
'X'
:
self
.
x
}
self
.
attrs
=
{
'axis'
:
self
.
axis
}
self
.
attrs
=
{
'axis'
:
self
.
axis
}
if
self
.
op_type
==
"arg_min"
:
self
.
outputs
=
{
'Out'
:
np
.
argmax
(
self
.
x
,
axis
=
self
.
axis
)}
self
.
outputs
=
{
'Out'
:
np
.
argmin
(
self
.
x
,
axis
=
self
.
axis
)}
else
:
self
.
outputs
=
{
'Out'
:
np
.
argmax
(
self
.
x
,
axis
=
self
.
axis
)}
def
test_check_output
(
self
):
def
test_check_output
(
self
):
self
.
check_output_with_place
(
self
.
place
)
self
.
check_output_with_place
(
self
.
place
)
...
@@ -211,6 +212,64 @@ class TestArgMaxFloat32Case10(BaseTestCase):
...
@@ -211,6 +212,64 @@ class TestArgMaxFloat32Case10(BaseTestCase):
self
.
axis
=
0
self
.
axis
=
0
class
BaseTestComplex1_1
(
OpTest
):
def
set_npu
(
self
):
self
.
__class__
.
use_npu
=
True
self
.
place
=
paddle
.
NPUPlace
(
0
)
def
initTestCase
(
self
):
self
.
op_type
=
'arg_max'
self
.
dims
=
(
4
,
5
,
6
)
self
.
dtype
=
'float32'
self
.
axis
=
2
def
setUp
(
self
):
self
.
set_npu
()
self
.
initTestCase
()
self
.
x
=
(
np
.
random
.
random
(
self
.
dims
)).
astype
(
self
.
dtype
)
self
.
inputs
=
{
'X'
:
self
.
x
}
self
.
attrs
=
{
'axis'
:
self
.
axis
,
'dtype'
:
int
(
core
.
VarDesc
.
VarType
.
INT32
)
}
self
.
outputs
=
{
'Out'
:
np
.
argmax
(
self
.
x
,
axis
=
self
.
axis
).
astype
(
"int32"
)
}
def
test_check_output
(
self
):
self
.
check_output_with_place
(
self
.
place
)
class
BaseTestComplex1_2
(
OpTest
):
def
set_npu
(
self
):
self
.
__class__
.
use_npu
=
True
self
.
place
=
paddle
.
NPUPlace
(
0
)
def
initTestCase
(
self
):
self
.
op_type
=
'arg_max'
self
.
dims
=
(
4
,
5
,
6
)
self
.
dtype
=
'float16'
self
.
axis
=
2
def
setUp
(
self
):
self
.
set_npu
()
self
.
initTestCase
()
self
.
x
=
(
np
.
random
.
random
(
self
.
dims
)).
astype
(
self
.
dtype
)
self
.
inputs
=
{
'X'
:
self
.
x
}
self
.
attrs
=
{
'axis'
:
self
.
axis
,
'dtype'
:
int
(
core
.
VarDesc
.
VarType
.
INT32
)
}
self
.
outputs
=
{
'Out'
:
np
.
argmax
(
self
.
x
,
axis
=
self
.
axis
).
astype
(
"int32"
)
}
def
test_check_output
(
self
):
self
.
check_output_with_place
(
self
.
place
)
class
TestArgMaxAPI
(
unittest
.
TestCase
):
class
TestArgMaxAPI
(
unittest
.
TestCase
):
def
initTestCase
(
self
):
def
initTestCase
(
self
):
self
.
dims
=
(
3
,
4
,
5
)
self
.
dims
=
(
3
,
4
,
5
)
...
...
python/paddle/nn/functional/loss.py
浏览文件 @
8757fc5b
...
@@ -1675,11 +1675,16 @@ def cross_entropy(input,
...
@@ -1675,11 +1675,16 @@ def cross_entropy(input,
raise
ValueError
(
raise
ValueError
(
"Target({}) is out of class_dimension's upper bound({})"
.
"Target({}) is out of class_dimension's upper bound({})"
.
format
(
invalid_label
[
0
],
input
.
shape
[
axis
]
-
1
))
format
(
invalid_label
[
0
],
input
.
shape
[
axis
]
-
1
))
if
core
.
is_compiled_with_npu
():
_
,
out
=
_C_ops
.
softmax_with_cross_entropy
(
_
,
_
,
out
=
_C_ops
.
softmax_with_cross_entropy
(
input
,
label
,
'soft_label'
,
soft_label
,
'ignore_index'
,
input
,
label
,
'soft_label'
,
soft_label
,
'ignore_index'
,
ignore_index
,
'numeric_stable_mode'
,
True
,
'axis'
,
axis
,
ignore_index
,
'numeric_stable_mode'
,
True
,
'axis'
,
axis
,
'use_softmax'
,
use_softmax
)
'use_softmax'
,
use_softmax
)
else
:
_
,
out
=
_C_ops
.
softmax_with_cross_entropy
(
input
,
label
,
'soft_label'
,
soft_label
,
'ignore_index'
,
ignore_index
,
'numeric_stable_mode'
,
True
,
'axis'
,
axis
,
'use_softmax'
,
use_softmax
)
if
weight
is
not
None
:
if
weight
is
not
None
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录