Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
c701e114
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看板
未验证
提交
c701e114
编写于
6月 21, 2022
作者:
C
cifar10
提交者:
GitHub
6月 21, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add mlu stack kernel (#43423)
上级
9b031026
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
258 addition
and
0 deletion
+258
-0
paddle/fluid/operators/stack_op_mlu.cc
paddle/fluid/operators/stack_op_mlu.cc
+70
-0
python/paddle/fluid/tests/unittests/mlu/test_stack_op_mlu.py
python/paddle/fluid/tests/unittests/mlu/test_stack_op_mlu.py
+188
-0
未找到文件。
paddle/fluid/operators/stack_op_mlu.cc
0 → 100644
浏览文件 @
c701e114
/* Copyright (c) 2022 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/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/operators/mlu/mlu_baseop.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
T
>
class
StackMLUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
x
=
ctx
.
MultiInput
<
Tensor
>
(
"X"
);
auto
*
y
=
ctx
.
Output
<
Tensor
>
(
"Y"
);
int
axis
=
ctx
.
Attr
<
int
>
(
"axis"
);
if
(
axis
<
0
)
axis
+=
(
x
[
0
]
->
dims
().
size
()
+
1
);
int
num
=
static_cast
<
int
>
(
x
.
size
());
PADDLE_ENFORCE_GT
(
num
,
0
,
platform
::
errors
::
InvalidArgument
(
"number of input Tensor <= 0"
));
std
::
vector
<
MLUCnnlTensorDesc
>
x_descs
;
std
::
vector
<
cnnlTensorDescriptor_t
>
x_raw_descs
;
std
::
vector
<
const
void
*>
x_ptrs
;
for
(
int
i
=
0
;
i
<
num
;
i
++
)
{
if
(
x
[
i
]
->
dims
().
size
()
!=
0
)
{
std
::
vector
<
int64_t
>
in_dims
=
phi
::
vectorize
(
x
[
i
]
->
dims
());
in_dims
.
insert
(
in_dims
.
begin
()
+
axis
,
1
);
x_descs
.
emplace_back
(
MLUCnnlTensorDesc
(
in_dims
.
size
(),
in_dims
.
data
(),
ToCnnlDataType
<
T
>
()));
}
else
{
int
input_dims
=
1
;
x_descs
.
emplace_back
(
MLUCnnlTensorDesc
(
1
,
&
input_dims
,
ToCnnlDataType
<
T
>
()));
}
x_raw_descs
.
push_back
(
x_descs
.
back
().
get
());
x_ptrs
.
push_back
(
GetBasePtr
(
x
[
i
]));
}
y
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
MLUCnnlTensorDesc
y_desc
(
*
y
);
MLUCnnl
::
Concat
(
ctx
,
num
,
axis
,
x_raw_descs
.
data
(),
x_ptrs
.
data
(),
y_desc
.
get
(),
GetBasePtr
(
y
));
}
};
}
// namespace operators
}
// namespace paddle
REGISTER_OP_MLU_KERNEL
(
stack
,
paddle
::
operators
::
StackMLUKernel
<
int64_t
>
,
paddle
::
operators
::
StackMLUKernel
<
int
>
,
paddle
::
operators
::
StackMLUKernel
<
float
>
,
paddle
::
operators
::
StackMLUKernel
<
paddle
::
platform
::
float16
>
);
python/paddle/fluid/tests/unittests/mlu/test_stack_op_mlu.py
0 → 100644
浏览文件 @
c701e114
# Copyright (c) 2022 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.fluid
as
fluid
import
paddle
paddle
.
enable_static
()
class
TestStackOpBase
(
OpTest
):
def
initDefaultParameters
(
self
):
self
.
num_inputs
=
4
self
.
input_dim
=
(
5
,
6
,
7
)
self
.
axis
=
0
def
initParameters
(
self
):
pass
def
get_x_names
(
self
):
x_names
=
[]
for
i
in
range
(
self
.
num_inputs
):
x_names
.
append
(
'x{}'
.
format
(
i
))
return
x_names
def
setUp
(
self
):
self
.
initDefaultParameters
()
self
.
initParameters
()
self
.
op_type
=
'stack'
self
.
set_mlu
()
self
.
init_dtype
()
self
.
x
=
[]
for
i
in
range
(
self
.
num_inputs
):
self
.
x
.
append
(
np
.
random
.
random
(
size
=
self
.
input_dim
).
astype
(
self
.
dtype
))
tmp
=
[]
x_names
=
self
.
get_x_names
()
for
i
in
range
(
self
.
num_inputs
):
tmp
.
append
((
x_names
[
i
],
self
.
x
[
i
]))
self
.
inputs
=
{
'X'
:
tmp
}
self
.
outputs
=
{
'Y'
:
np
.
stack
(
self
.
x
,
axis
=
self
.
axis
)}
self
.
attrs
=
{
'axis'
:
self
.
axis
}
def
set_mlu
(
self
):
self
.
__class__
.
use_mlu
=
True
self
.
place
=
paddle
.
MLUPlace
(
0
)
self
.
__class__
.
no_need_check_grad
=
True
def
init_dtype
(
self
):
self
.
dtype
=
np
.
float32
def
test_check_output
(
self
):
self
.
check_output_with_place
(
self
.
place
)
class
TestStackOp1
(
TestStackOpBase
):
def
initParameters
(
self
):
self
.
num_inputs
=
16
class
TestStackOp2
(
TestStackOpBase
):
def
initParameters
(
self
):
self
.
num_inputs
=
20
class
TestStackOp3
(
TestStackOpBase
):
def
initParameters
(
self
):
self
.
axis
=
-
1
class
TestStackOp4
(
TestStackOpBase
):
def
initParameters
(
self
):
self
.
axis
=
-
4
class
TestStackOp5
(
TestStackOpBase
):
def
initParameters
(
self
):
self
.
axis
=
1
class
TestStackOp6
(
TestStackOpBase
):
def
initParameters
(
self
):
self
.
axis
=
3
class
TestStackOpINT32
(
TestStackOpBase
):
def
init_dtype
(
self
):
self
.
dtype
=
np
.
int32
class
TestStackOpINT64
(
TestStackOpBase
):
def
init_dtype
(
self
):
self
.
dtype
=
np
.
int64
class
TestStackOpHalf
(
TestStackOpBase
):
def
init_dtype
(
self
):
self
.
dtype
=
np
.
float16
class
API_test
(
unittest
.
TestCase
):
def
test_out
(
self
):
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
data1
=
fluid
.
layers
.
data
(
'data1'
,
shape
=
[
1
,
2
],
dtype
=
'float32'
)
data2
=
fluid
.
layers
.
data
(
'data2'
,
shape
=
[
1
,
2
],
dtype
=
'float32'
)
data3
=
fluid
.
layers
.
data
(
'data3'
,
shape
=
[
1
,
2
],
dtype
=
'float32'
)
result_stack
=
paddle
.
stack
([
data1
,
data2
,
data3
],
axis
=
0
)
place
=
paddle
.
MLUPlace
(
0
)
exe
=
fluid
.
Executor
(
place
)
input1
=
np
.
random
.
random
([
1
,
2
]).
astype
(
'float32'
)
input2
=
np
.
random
.
random
([
1
,
2
]).
astype
(
'float32'
)
input3
=
np
.
random
.
random
([
1
,
2
]).
astype
(
'float32'
)
result
,
=
exe
.
run
(
feed
=
{
"data1"
:
input1
,
"data2"
:
input2
,
"data3"
:
input3
},
fetch_list
=
[
result_stack
])
expected_result
=
np
.
stack
([
input1
,
input2
,
input3
],
axis
=
0
)
self
.
assertTrue
(
np
.
allclose
(
expected_result
,
result
))
def
test_single_tensor_error
(
self
):
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
x
=
paddle
.
rand
([
2
,
3
])
self
.
assertRaises
(
TypeError
,
paddle
.
stack
,
x
)
class
API_DygraphTest
(
unittest
.
TestCase
):
def
test_out
(
self
):
data1
=
np
.
array
([[
1.0
,
2.0
]]).
astype
(
"float32"
)
data2
=
np
.
array
([[
3.0
,
4.0
]]).
astype
(
"float32"
)
data3
=
np
.
array
([[
5.0
,
6.0
]]).
astype
(
"float32"
)
with
fluid
.
dygraph
.
guard
(
place
=
paddle
.
MLUPlace
(
0
)):
x1
=
fluid
.
dygraph
.
to_variable
(
data1
)
x2
=
fluid
.
dygraph
.
to_variable
(
data2
)
x3
=
fluid
.
dygraph
.
to_variable
(
data3
)
result
=
paddle
.
stack
([
x1
,
x2
,
x3
])
result_np
=
result
.
numpy
()
expected_result
=
np
.
stack
([
data1
,
data2
,
data3
])
self
.
assertTrue
(
np
.
allclose
(
expected_result
,
result_np
))
with
fluid
.
dygraph
.
guard
(
place
=
paddle
.
MLUPlace
(
0
)):
y1
=
fluid
.
dygraph
.
to_variable
(
data1
)
result
=
paddle
.
stack
([
y1
],
axis
=
0
)
result_np_2
=
result
.
numpy
()
expected_result_2
=
np
.
stack
([
data1
],
axis
=
0
)
self
.
assertTrue
(
np
.
allclose
(
expected_result_2
,
result_np_2
))
def
test_single_tensor_error
(
self
):
with
fluid
.
dygraph
.
guard
(
place
=
paddle
.
MLUPlace
(
0
)):
x
=
paddle
.
to_tensor
([
1
,
2
,
3
])
self
.
assertRaises
(
Exception
,
paddle
.
stack
,
x
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录