Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
df81915a
P
Paddle
项目概览
机器未来
/
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看板
未验证
提交
df81915a
编写于
9月 26, 2021
作者:
R
ronnywang
提交者:
GitHub
9月 26, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[NPU] add randperm_op_npu (#35763) (#36026)
* add randperm_op_npu * fix test_set_value_op_npu
上级
2e473f23
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
151 addition
and
1 deletion
+151
-1
paddle/fluid/operators/randperm_op_npu.cc
paddle/fluid/operators/randperm_op_npu.cc
+23
-0
paddle/fluid/operators/utils.h
paddle/fluid/operators/utils.h
+5
-0
python/paddle/fluid/tests/unittests/npu/test_randperm_op_npu.py
.../paddle/fluid/tests/unittests/npu/test_randperm_op_npu.py
+122
-0
python/paddle/fluid/tests/unittests/npu/test_set_value_op_npu.py
...paddle/fluid/tests/unittests/npu/test_set_value_op_npu.py
+1
-1
未找到文件。
paddle/fluid/operators/randperm_op_npu.cc
0 → 100644
浏览文件 @
df81915a
/* 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/randperm_op.h"
#include "paddle/fluid/framework/op_registry.h"
template
<
typename
T
>
using
kernel
=
paddle
::
operators
::
RandpermKernel
<
paddle
::
platform
::
NPUDeviceContext
,
T
>
;
REGISTER_OP_NPU_KERNEL
(
randperm
,
kernel
<
int64_t
>
,
kernel
<
int
>
,
kernel
<
float
>
,
kernel
<
double
>
);
paddle/fluid/operators/utils.h
浏览文件 @
df81915a
...
...
@@ -114,6 +114,11 @@ inline T GetValue(const framework::Tensor* x) {
if
(
!
platform
::
is_cpu_place
(
x
->
place
()))
{
framework
::
Tensor
cpu_x
;
framework
::
TensorCopy
(
*
x
,
platform
::
CPUPlace
(),
&
cpu_x
);
#ifdef PADDLE_WITH_ASCEND_CL
platform
::
DeviceContextPool
&
pool
=
platform
::
DeviceContextPool
::
Instance
();
const
platform
::
DeviceContext
*
dev_ctx
=
pool
.
Get
(
x
->
place
());
dev_ctx
->
Wait
();
#endif
value
=
cpu_x
.
data
<
T
>
()[
0
];
}
else
{
value
=
x
->
data
<
T
>
()[
0
];
...
...
python/paddle/fluid/tests/unittests/npu/test_randperm_op_npu.py
0 → 100644
浏览文件 @
df81915a
# 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
unittest
import
numpy
as
np
import
sys
sys
.
path
.
append
(
".."
)
from
op_test
import
OpTest
import
paddle
import
paddle.fluid.core
as
core
from
paddle.static
import
program_guard
,
Program
from
test_randperm_op
import
check_randperm_out
,
error_msg
,
convert_dtype
paddle
.
enable_static
()
class
TestRandpermOp
(
OpTest
):
""" Test randperm op."""
def
setUp
(
self
):
self
.
set_npu
()
self
.
op_type
=
"randperm"
self
.
n
=
200
self
.
dtype
=
"int64"
self
.
inputs
=
{}
self
.
outputs
=
{
"Out"
:
np
.
zeros
((
self
.
n
)).
astype
(
self
.
dtype
)}
self
.
init_attrs
()
self
.
attrs
=
{
"n"
:
self
.
n
,
"dtype"
:
convert_dtype
(
self
.
dtype
),
}
def
set_npu
(
self
):
self
.
__class__
.
use_npu
=
True
def
_get_places
(
self
):
return
[
paddle
.
NPUPlace
(
0
)]
def
init_attrs
(
self
):
pass
def
test_check_output
(
self
):
self
.
check_output_customized
(
self
.
verify_output
)
def
verify_output
(
self
,
outs
):
out_np
=
np
.
array
(
outs
[
0
])
self
.
assertTrue
(
check_randperm_out
(
self
.
n
,
out_np
),
msg
=
error_msg
(
out_np
))
class
TestRandpermOpN
(
TestRandpermOp
):
def
init_attrs
(
self
):
self
.
n
=
10000
class
TestRandpermOpInt32
(
TestRandpermOp
):
def
init_attrs
(
self
):
self
.
dtype
=
"int32"
class
TestRandpermOpFloat32
(
TestRandpermOp
):
def
init_attrs
(
self
):
self
.
dtype
=
"float32"
class
TestRandpermOpFloat64
(
TestRandpermOp
):
def
init_attrs
(
self
):
self
.
dtype
=
"float64"
class
TestRandpermOpError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
with
program_guard
(
Program
(),
Program
()):
self
.
assertRaises
(
ValueError
,
paddle
.
randperm
,
-
3
)
self
.
assertRaises
(
TypeError
,
paddle
.
randperm
,
10
,
'int8'
)
class
TestRandpermAPI
(
unittest
.
TestCase
):
def
test_out
(
self
):
n
=
10
place
=
paddle
.
NPUPlace
(
0
)
with
program_guard
(
Program
(),
Program
()):
x1
=
paddle
.
randperm
(
n
)
x2
=
paddle
.
randperm
(
n
,
'float32'
)
exe
=
paddle
.
static
.
Executor
(
place
)
res
=
exe
.
run
(
fetch_list
=
[
x1
,
x2
])
self
.
assertEqual
(
res
[
0
].
dtype
,
np
.
int64
)
self
.
assertEqual
(
res
[
1
].
dtype
,
np
.
float32
)
self
.
assertTrue
(
check_randperm_out
(
n
,
res
[
0
]))
self
.
assertTrue
(
check_randperm_out
(
n
,
res
[
1
]))
class
TestRandpermImperative
(
unittest
.
TestCase
):
def
test_out
(
self
):
paddle
.
disable_static
(
paddle
.
NPUPlace
(
0
))
n
=
10
for
dtype
in
[
'int32'
,
np
.
int64
,
'float32'
,
'float64'
]:
data_p
=
paddle
.
randperm
(
n
,
dtype
)
data_np
=
data_p
.
numpy
()
self
.
assertTrue
(
check_randperm_out
(
n
,
data_np
),
msg
=
error_msg
(
data_np
))
paddle
.
enable_static
()
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/npu/test_set_value_op_npu.py
浏览文件 @
df81915a
...
...
@@ -68,7 +68,7 @@ class TestSetValueApi(TestSetValueBase):
return
out
def
_run_dynamic
(
self
):
paddle
.
disable_static
()
paddle
.
disable_static
(
paddle
.
NPUPlace
(
0
)
)
x
=
paddle
.
ones
(
shape
=
self
.
shape
,
dtype
=
self
.
dtype
)
self
.
_call_setitem
(
x
)
out
=
x
.
numpy
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录