Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
马赛克医院
Paddle
提交
1ef21855
P
Paddle
项目概览
马赛克医院
/
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
1ef21855
编写于
8月 17, 2021
作者:
A
Aganlengzi
提交者:
GitHub
8月 17, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[NPU] add where_index op and tests (#34951)
上级
690f5831
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
203 addition
and
0 deletion
+203
-0
paddle/fluid/operators/where_index_op_npu.cc
paddle/fluid/operators/where_index_op_npu.cc
+97
-0
python/paddle/fluid/tests/unittests/npu/test_where_index_npu.py
.../paddle/fluid/tests/unittests/npu/test_where_index_npu.py
+106
-0
未找到文件。
paddle/fluid/operators/where_index_op_npu.cc
0 → 100644
浏览文件 @
1ef21855
/* 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/where_index_op.h"
#include "paddle/fluid/operators/npu_op_runner.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
T
>
class
NPUWhereIndexKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
&
dev_ctx
=
context
.
template
device_context
<
platform
::
NPUDeviceContext
>();
auto
*
condition
=
context
.
Input
<
Tensor
>
(
"Condition"
);
auto
*
out
=
context
.
Output
<
Tensor
>
(
"Out"
);
auto
dims
=
condition
->
dims
();
const
int
rank
=
dims
.
size
();
auto
place
=
context
.
GetPlace
();
const
aclrtStream
&
stream
=
dev_ctx
.
stream
();
// Run Cast and ReduceSum to get 0 dim of Out
Tensor
booled_cond
;
if
(
condition
->
type
()
!=
framework
::
proto
::
VarType
::
BOOL
)
{
auto
bool_type
=
ConvertToNpuDtype
(
framework
::
proto
::
VarType
::
BOOL
);
booled_cond
.
mutable_data
<
bool
>
(
dims
,
place
);
const
auto
&
booled_runner
=
NpuOpRunner
(
"Cast"
,
{
*
condition
},
{
booled_cond
},
{{
"dst_type"
,
static_cast
<
int
>
(
bool_type
)}});
booled_runner
.
Run
(
stream
);
}
else
{
booled_cond
.
ShareDataWith
(
*
condition
);
}
Tensor
casted_cond
;
auto
dst_dtype
=
ConvertToNpuDtype
(
framework
::
proto
::
VarType
::
INT64
);
casted_cond
.
mutable_data
<
int64_t
>
(
dims
,
place
);
const
auto
&
cast_runner
=
NpuOpRunner
(
"Cast"
,
{
booled_cond
},
{
casted_cond
},
{{
"dst_type"
,
static_cast
<
int
>
(
dst_dtype
)}});
cast_runner
.
Run
(
stream
);
Tensor
sumed_true_num
;
sumed_true_num
.
mutable_data
<
int64_t
>
({
1
},
place
);
Tensor
cond_axes
;
cond_axes
.
mutable_data
<
int
>
({
dims
.
size
()},
place
);
std
::
vector
<
int
>
axes_vec
;
for
(
int
i
=
0
;
i
<
dims
.
size
();
++
i
)
{
axes_vec
.
push_back
(
i
);
}
framework
::
TensorFromVector
<
int
>
(
axes_vec
,
dev_ctx
,
&
cond_axes
);
const
auto
&
sum_runner
=
NpuOpRunner
(
"ReduceSum"
,
{
casted_cond
,
cond_axes
},
{
sumed_true_num
},
{{
"keep_dims"
,
false
}});
sum_runner
.
Run
(
stream
);
Tensor
local_true_num
;
TensorCopySync
(
sumed_true_num
,
platform
::
CPUPlace
(),
&
local_true_num
);
auto
true_num
=
*
local_true_num
.
data
<
int64_t
>
();
out
->
Resize
(
framework
::
make_ddim
({
true_num
,
rank
}));
out
->
mutable_data
<
int64_t
>
(
place
);
if
(
true_num
==
0
)
{
return
;
}
out
->
set_layout
(
DataLayout
::
kAnyLayout
);
NpuOpRunner
runner
{
"Where"
,
{
*
condition
},
{
*
out
}};
runner
.
Run
(
stream
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_NPU_KERNEL
(
where_index
,
ops
::
NPUWhereIndexKernel
<
int64_t
>
,
ops
::
NPUWhereIndexKernel
<
int
>
,
ops
::
NPUWhereIndexKernel
<
bool
>
,
ops
::
NPUWhereIndexKernel
<
float
>
,
ops
::
NPUWhereIndexKernel
<
double
>
);
python/paddle/fluid/tests/unittests/npu/test_where_index_npu.py
0 → 100644
浏览文件 @
1ef21855
# 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.
from
__future__
import
print_function
import
numpy
as
np
import
unittest
import
paddle
import
sys
sys
.
path
.
append
(
".."
)
from
op_test
import
OpTest
from
paddle.fluid.op
import
Operator
import
paddle.fluid
as
fluid
from
paddle.fluid
import
Program
,
program_guard
paddle
.
enable_static
()
class
TestWhereIndexOp
(
OpTest
):
def
setUp
(
self
):
self
.
set_npu
()
self
.
op_type
=
"where_index"
self
.
place
=
paddle
.
NPUPlace
(
0
)
self
.
init_config
()
def
test_check_output
(
self
):
self
.
check_output_with_place
(
self
.
place
)
def
init_config
(
self
):
self
.
inputs
=
{
'Condition'
:
np
.
array
([
True
,
False
,
True
]),
}
self
.
outputs
=
{
'Out'
:
np
.
array
([[
0
],
[
2
]],
dtype
=
'int64'
)}
def
set_npu
(
self
):
self
.
__class__
.
use_npu
=
True
class
TestNotBool
(
TestWhereIndexOp
):
def
init_config
(
self
):
self
.
inputs
=
{
'Condition'
:
np
.
array
([
1
,
0
,
8
]),
}
self
.
outputs
=
{
'Out'
:
np
.
array
([[
0
],
[
2
]],
dtype
=
'int64'
)}
class
TestAllFalse
(
TestWhereIndexOp
):
def
init_config
(
self
):
self
.
inputs
=
{
'Condition'
:
np
.
array
([
False
,
False
,
False
]),
}
self
.
outputs
=
{
'Out'
:
np
.
array
([],
dtype
=
'int64'
)}
class
TestRank2
(
TestWhereIndexOp
):
def
init_config
(
self
):
self
.
inputs
=
{
'Condition'
:
np
.
array
([[
True
,
False
],
[
False
,
True
]]),
}
self
.
outputs
=
{
'Out'
:
np
.
array
([[
0
,
0
],
[
1
,
1
]],
dtype
=
'int64'
)}
class
TestRank3
(
TestWhereIndexOp
):
def
init_config
(
self
):
self
.
inputs
=
{
'Condition'
:
np
.
array
([[[
True
,
False
],
[
False
,
True
]],
[[
False
,
True
],
[
True
,
False
]],
[[
False
,
False
],
[
False
,
True
]]]),
}
self
.
outputs
=
{
'Out'
:
np
.
array
(
[[
0
,
0
,
0
],
[
0
,
1
,
1
],
[
1
,
0
,
1
],
[
1
,
1
,
0
],
[
2
,
1
,
1
]],
dtype
=
'int64'
)
}
class
TestWhereOpError
(
unittest
.
TestCase
):
def
test_api
(
self
):
with
program_guard
(
Program
(),
Program
()):
cond
=
fluid
.
layers
.
data
(
name
=
'cond'
,
shape
=
[
4
],
dtype
=
'bool'
)
result
=
fluid
.
layers
.
where
(
cond
)
exe
=
fluid
.
Executor
(
paddle
.
NPUPlace
(
0
))
exe
.
run
(
fluid
.
default_startup_program
())
cond_i
=
np
.
array
([
True
,
False
,
False
,
False
]).
astype
(
"bool"
)
out
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{
'cond'
:
cond_i
})
class
TestWhereRaiseError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
def
test_type
():
fluid
.
layers
.
where
([
10
])
self
.
assertRaises
(
TypeError
,
test_type
)
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录