Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
3dd992e2
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
3dd992e2
编写于
3月 16, 2021
作者:
M
Meiyim
提交者:
GitHub
3月 16, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[NPU] Support npu op `expand` (#31405)
* [npu] support npu kernel for `expand`
上级
444c2852
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
305 addition
and
3 deletion
+305
-3
paddle/fluid/operators/CMakeLists.txt
paddle/fluid/operators/CMakeLists.txt
+2
-3
paddle/fluid/operators/expand_op.h
paddle/fluid/operators/expand_op.h
+6
-0
paddle/fluid/operators/expand_op_npu.cc
paddle/fluid/operators/expand_op_npu.cc
+82
-0
paddle/fluid/operators/expand_op_npu_test.cc
paddle/fluid/operators/expand_op_npu_test.cc
+74
-0
python/paddle/fluid/tests/unittests/npu/test_expand_op_npu.py
...on/paddle/fluid/tests/unittests/npu/test_expand_op_npu.py
+141
-0
未找到文件。
paddle/fluid/operators/CMakeLists.txt
浏览文件 @
3dd992e2
...
...
@@ -156,12 +156,11 @@ cc_library(tensor_formatter SRCS tensor_formatter.cc DEPS ${OP_HEADER_DEPS})
if
(
WITH_PYTHON
)
cc_library
(
py_func_op SRCS py_func_op.cc DEPS op_registry python pybind
)
endif
()
if
(
WITH_ASCEND_CL
)
cc_test
(
lookup_table_v2_op_npu_test SRCS lookup_table_v2_op_npu_test.cc DEPS op_registry lookup_table_v2_op scope device_context enforce executor compare_op
)
endif
()
if
(
WITH_ASCEND_CL
)
cc_test
(
range_op_npu_test SRCS range_op_npu_test.cc DEPS op_registry range_op scope device_context enforce executor
)
cc_test
(
lookup_table_v2_op_npu_test SRCS lookup_table_v2_op_npu_test.cc DEPS op_registry lookup_table_v2_op scope device_context enforce executor compare_op
)
cc_test
(
expand_op_npu_test SRCS expand_op_npu_test.cc DEPS op_registry expand_op scope device_context enforce executor compare_op
)
endif
()
set
(
GLOB_OP_LIB
${
OP_LIBRARY
}
CACHE INTERNAL
"Global OP library"
)
...
...
paddle/fluid/operators/expand_op.h
浏览文件 @
3dd992e2
...
...
@@ -56,6 +56,12 @@ inline std::vector<int> get_expand_times(
TensorCopySync
(
*
expand_tensor
,
platform
::
CPUPlace
(),
&
cpu_expand_tensor
);
expand_data
=
cpu_expand_tensor
.
data
<
int
>
();
}
#ifdef PADDLE_WITH_ASCEND_CL
if
(
platform
::
is_npu_place
(
expand_tensor
->
place
()))
{
TensorCopySync
(
*
expand_tensor
,
platform
::
CPUPlace
(),
&
cpu_expand_tensor
);
expand_data
=
cpu_expand_tensor
.
data
<
int
>
();
}
#endif
#ifdef PADDLE_WITH_XPU
if
(
platform
::
is_xpu_place
(
expand_tensor
->
place
()))
{
TensorCopySync
(
*
expand_tensor
,
platform
::
CPUPlace
(),
&
cpu_expand_tensor
);
...
...
paddle/fluid/operators/expand_op_npu.cc
0 → 100644
浏览文件 @
3dd992e2
/* 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. */
#ifdef PADDLE_WITH_ASCEND_CL
#include <iostream>
#include <memory>
#include <string>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/expand_op.h"
#include "paddle/fluid/operators/npu_op_runner.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
DeviceContext
,
typename
T
>
class
ExpandNPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
rank
=
context
.
Input
<
Tensor
>
(
"X"
)
->
dims
().
size
();
PADDLE_ENFORCE_GE
(
rank
,
1
,
platform
::
errors
::
InvalidArgument
(
"The number of dimensions of the input 'x' for Op(expand) "
"must be greater than or equal to 1, but the value received is %d."
,
rank
));
PADDLE_ENFORCE_LE
(
rank
,
MAX_RANK_SUPPORTED
,
platform
::
errors
::
InvalidArgument
(
"The number of dimensions of the input 'x' for Op(expand) "
"must be less than or equal to %d, but the value received is %d."
,
MAX_RANK_SUPPORTED
,
rank
));
switch
(
rank
)
{
REP_EXPAND_TEMPLATE
(
MAX_RANK_SUPPORTED
)
}
}
protected:
template
<
int
Rank
>
void
Expand
(
const
framework
::
ExecutionContext
&
context
)
const
{
auto
*
in0
=
context
.
Input
<
framework
::
LoDTensor
>
(
"X"
);
auto
in_dims
=
in0
->
dims
();
auto
expand_times
=
get_expand_times
(
context
);
PADDLE_ENFORCE_EQ
(
static_cast
<
size_t
>
(
in_dims
.
size
()),
expand_times
.
size
(),
platform
::
errors
::
InvalidArgument
(
"The number of elements (%d) of 'expand_times' for "
"Op(expand) must be equal to the number "
"of dimensions (%d) of the input."
,
expand_times
.
size
(),
static_cast
<
size_t
>
(
in_dims
.
size
())));
auto
*
out0
=
context
.
Output
<
framework
::
LoDTensor
>
(
"Out"
);
framework
::
DDim
out_dims
(
in_dims
);
for
(
size_t
i
=
0
;
i
<
expand_times
.
size
();
++
i
)
{
out_dims
[
i
]
*=
expand_times
[
i
];
}
out0
->
Resize
(
out_dims
);
out0
->
mutable_data
<
T
>
(
context
.
device_context
().
GetPlace
());
auto
runner
=
NpuOpRunner
(
"TileD"
,
{
*
in0
},
{
*
out0
},
{{
"multiples"
,
expand_times
}});
auto
stream
=
context
.
template
device_context
<
paddle
::
platform
::
NPUDeviceContext
>()
.
stream
();
runner
.
Run
(
stream
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_NPU_KERNEL
(
expand
,
ops
::
ExpandNPUKernel
<
paddle
::
platform
::
NPUDeviceContext
,
float
>
,
ops
::
ExpandNPUKernel
<
paddle
::
platform
::
NPUDeviceContext
,
paddle
::
platform
::
float16
>
);
#endif
paddle/fluid/operators/expand_op_npu_test.cc
0 → 100644
浏览文件 @
3dd992e2
/* 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. */
#ifndef _WIN32
#include <unistd.h>
#endif
#include <iostream>
#include <string>
#include <thread> // NOLINT
#include <vector>
#include "gtest/gtest.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/operators/dropout_op.h"
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/string/printf.h"
namespace
f
=
paddle
::
framework
;
namespace
p
=
paddle
::
platform
;
namespace
m
=
paddle
::
operators
::
math
;
USE_OP
(
expand
);
USE_OP_DEVICE_KERNEL
(
expand
,
NPU
);
template
<
typename
T
>
void
Compare
(
f
::
Scope
*
scope
,
const
p
::
DeviceContext
&
ctx
)
{
// init
auto
in
=
scope
->
Var
(
"X"
);
auto
expand_times
=
scope
->
Var
(
"ExpandTimes"
);
auto
out
=
scope
->
Var
(
"Out"
);
auto
in_t
=
in
->
GetMutable
<
f
::
LoDTensor
>
();
auto
out_t
=
out
->
GetMutable
<
f
::
LoDTensor
>
();
auto
expand_times_t
=
expand_times
->
GetMutable
<
f
::
LoDTensor
>
();
auto
place
=
ctx
.
GetPlace
();
TensorFromVector
(
std
::
vector
<
T
>
(
3
*
1
*
7
,
1
),
ctx
,
in_t
);
TensorFromVector
(
std
::
vector
<
int
>
({
1
,
10
,
1
}),
ctx
,
expand_times_t
);
in_t
->
Resize
(
f
::
make_ddim
({
3
,
1
,
7
}));
expand_times_t
->
Resize
(
f
::
make_ddim
({
3
}));
out_t
->
Resize
(
f
::
make_ddim
({
3
,
10
,
7
}));
out_t
->
mutable_data
<
T
>
(
place
);
f
::
AttributeMap
attrs
=
{{}};
auto
op
=
f
::
OpRegistry
::
CreateOp
(
"expand"
,
{{
"X"
,
{
"X"
}},
{
"ExpandTimes"
,
{
"ExpandTimes"
}}},
{{
"Out"
,
{
"Out"
}}},
attrs
);
op
->
Run
(
*
scope
,
place
);
ctx
.
Wait
();
auto
out_dim
=
out_t
->
dims
();
EXPECT_EQ
(
out_dim
.
at
(
0
),
3
);
EXPECT_EQ
(
out_dim
.
at
(
1
),
10
);
EXPECT_EQ
(
out_dim
.
at
(
2
),
7
);
}
TEST
(
expand
,
NPU_fp32
)
{
f
::
Scope
scope
;
p
::
NPUDeviceContext
ctx
(
p
::
NPUPlace
(
0
));
Compare
<
float
>
(
&
scope
,
ctx
);
}
python/paddle/fluid/tests/unittests/npu/test_expand_op_npu.py
0 → 100644
浏览文件 @
3dd992e2
# 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
sys
sys
.
path
.
append
(
".."
)
from
op_test
import
OpTest
import
paddle
import
paddle.fluid
as
fluid
paddle
.
enable_static
()
SEED
=
2021
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
"core is not compiled with NPU"
)
class
TestExpand
(
OpTest
):
def
setUp
(
self
):
self
.
set_npu
()
self
.
op_type
=
"expand"
self
.
place
=
paddle
.
NPUPlace
(
0
)
self
.
init_dtype
()
np
.
random
.
seed
(
SEED
)
x
=
np
.
random
.
randn
(
3
,
1
,
7
).
astype
(
self
.
dtype
)
out
=
np
.
tile
(
x
,
[
1
,
10
,
1
])
self
.
inputs
=
{
'X'
:
OpTest
.
np_dtype_to_fluid_dtype
(
x
)}
self
.
attrs
=
{
'expand_times'
:
[
1
,
10
,
1
]}
self
.
outputs
=
{
'Out'
:
out
}
def
set_npu
(
self
):
self
.
__class__
.
use_npu
=
True
def
init_dtype
(
self
):
self
.
dtype
=
np
.
float32
def
test_check_output
(
self
):
self
.
check_output_with_place
(
self
.
place
,
check_dygraph
=
False
)
# TODO(ascendrc): Add grad test
# def test_check_grad(self):
# if self.dtype == np.float16:
# return
# self.check_grad(['X'], 'Out')
#
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
"core is not compiled with NPU"
)
class
TestExpandV2
(
TestExpand
):
def
setUp
(
self
):
self
.
set_npu
()
self
.
op_type
=
"expand"
self
.
place
=
paddle
.
NPUPlace
(
0
)
self
.
init_dtype
()
np
.
random
.
seed
(
SEED
)
x
=
np
.
random
.
randn
(
3
,
1
,
7
).
astype
(
self
.
dtype
)
out
=
np
.
tile
(
x
,
[
1
,
10
,
1
])
expand_times
=
np
.
array
([
1
,
10
,
1
]).
astype
(
np
.
int32
)
self
.
inputs
=
{
'X'
:
OpTest
.
np_dtype_to_fluid_dtype
(
x
),
'ExpandTimes'
:
OpTest
.
np_dtype_to_fluid_dtype
(
expand_times
)}
self
.
attrs
=
{}
self
.
outputs
=
{
'Out'
:
out
}
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
"core is not compiled with NPU"
)
class
TestExpandFp16
(
TestExpand
):
no_need_check_grad
=
True
def
init_dtype
(
self
):
self
.
dtype
=
np
.
float16
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
"core is not compiled with NPU"
)
class
TestExpandNet
(
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
=
(
32
,
1
)).
astype
(
'float32'
)
label_np
=
np
.
random
.
randint
(
2
,
size
=
(
32
,
1
)).
astype
(
'int64'
)
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
a
=
paddle
.
static
.
data
(
name
=
"a"
,
shape
=
[
32
,
1
],
dtype
=
'float32'
)
label
=
paddle
.
static
.
data
(
name
=
"label"
,
shape
=
[
32
,
1
],
dtype
=
'int64'
)
res
=
paddle
.
fluid
.
layers
.
expand
(
a
,
[
1
,
32
])
loss
=
res
.
sum
()
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
)
for
epoch
in
range
(
100
):
loss_res
=
exe
.
run
(
main_prog
,
feed
=
{
"a"
:
a_np
,
"label"
:
label_np
},
fetch_list
=
[
loss
])
if
epoch
%
10
==
0
:
print
(
"Epoch {} | Loss: {}"
.
format
(
epoch
,
loss
))
return
loss_res
def
test_npu
(
self
):
cpu_loss
=
self
.
_test
(
False
)
npu_loss
=
self
.
_test
(
True
)
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录