Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
871edade
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看板
未验证
提交
871edade
编写于
7月 12, 2021
作者:
P
pangyoki
提交者:
GitHub
7月 12, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[NPU] slice support Tensor Input (#34067)
上级
113539eb
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
238 addition
and
6 deletion
+238
-6
paddle/fluid/operators/slice_op_npu.cc
paddle/fluid/operators/slice_op_npu.cc
+80
-6
python/paddle/fluid/tests/unittests/npu/test_slice_op_npu.py
python/paddle/fluid/tests/unittests/npu/test_slice_op_npu.py
+158
-0
未找到文件。
paddle/fluid/operators/slice_op_npu.cc
浏览文件 @
871edade
...
...
@@ -61,11 +61,66 @@ class SliceNPUKernel : public framework::OpKernel<T> {
auto
*
input
=
ctx
.
Input
<
Tensor
>
(
"Input"
);
auto
*
out
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
auto
axes
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"axes"
);
auto
starts
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"starts"
);
auto
ends
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"ends"
);
auto
axes_int
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"axes"
);
auto
starts_int
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"starts"
);
auto
ends_int
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"ends"
);
std
::
vector
<
int
>
axes
(
axes_int
.
begin
(),
axes_int
.
end
());
std
::
vector
<
int
>
starts
(
starts_int
.
begin
(),
starts_int
.
end
());
std
::
vector
<
int
>
ends
(
ends_int
.
begin
(),
ends_int
.
end
());
auto
decrease_axis
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"decrease_axis"
);
auto
infer_flags
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"infer_flags"
);
const
auto
&
in_dims
=
input
->
dims
();
// Get the accurate attribute value of starts and ends
auto
starts_tensor_list
=
ctx
.
MultiInput
<
Tensor
>
(
"StartsTensorList"
);
if
(
ctx
.
HasInput
(
"StartsTensor"
))
{
starts
=
GetDataFromTensor
<
int
>
(
ctx
.
Input
<
Tensor
>
(
"StartsTensor"
));
}
else
if
(
starts_tensor_list
.
size
()
>
0
)
{
starts
=
GetDataFromTensorList
<
int
>
(
starts_tensor_list
);
}
auto
ends_tensor_list
=
ctx
.
MultiInput
<
Tensor
>
(
"EndsTensorList"
);
if
(
ctx
.
HasInput
(
"EndsTensor"
))
{
ends
=
GetDataFromTensor
<
int
>
(
ctx
.
Input
<
Tensor
>
(
"EndsTensor"
));
}
else
if
(
ends_tensor_list
.
size
()
>
0
)
{
ends
=
GetDataFromTensorList
<
int
>
(
ends_tensor_list
);
}
PADDLE_ENFORCE_EQ
(
starts
.
size
(),
axes
.
size
(),
platform
::
errors
::
InvalidArgument
(
"The size of starts must be equal to the size of axes."
));
PADDLE_ENFORCE_EQ
(
ends
.
size
(),
axes
.
size
(),
platform
::
errors
::
InvalidArgument
(
"The size of ends must be equal to the size of axes."
));
if
(
ctx
.
HasInput
(
"StartsTensor"
)
||
ctx
.
HasInput
(
"EndsTensor"
)
||
starts_tensor_list
.
size
()
>
0
||
ends_tensor_list
.
size
()
>
0
)
{
// Infer output dims
auto
out_dims
=
out
->
dims
();
auto
slice_dims
=
out_dims
;
for
(
size_t
i
=
0
;
i
<
axes
.
size
();
++
i
)
{
// when start == -1 && end == start+1
if
(
starts
[
i
]
==
-
1
&&
ends
[
i
]
==
0
&&
infer_flags
[
i
]
==
-
1
)
{
auto
ret
=
std
::
find
(
decrease_axis
.
begin
(),
decrease_axis
.
end
(),
axes
[
i
]);
if
(
ret
!=
decrease_axis
.
end
())
{
ends
[
i
]
=
in_dims
[
axes
[
i
]];
}
}
}
CheckAndUpdateSliceAttrs
(
in_dims
,
axes
,
&
starts
,
&
ends
);
slice_dims
=
GetSliceDims
<
int
>
(
in_dims
,
axes
,
starts
,
ends
,
nullptr
,
nullptr
);
out_dims
=
GetDecreasedDims
(
slice_dims
,
decrease_axis
);
out
->
Resize
(
out_dims
);
}
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
std
::
vector
<
int
>
offsets
(
in_dims
.
size
());
...
...
@@ -91,9 +146,28 @@ class SliceGradNPUKernel : public framework::OpKernel<T> {
auto
*
dout
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
dinput
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Input"
));
auto
axes
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"axes"
);
auto
starts
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"starts"
);
auto
ends
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"ends"
);
auto
axes_int
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"axes"
);
auto
starts_int
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"starts"
);
auto
ends_int
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"ends"
);
std
::
vector
<
int
>
axes
(
axes_int
.
begin
(),
axes_int
.
end
());
std
::
vector
<
int
>
starts
(
starts_int
.
begin
(),
starts_int
.
end
());
std
::
vector
<
int
>
ends
(
ends_int
.
begin
(),
ends_int
.
end
());
// Get the accurate attribute value of starts and ends
auto
starts_tensor_list
=
ctx
.
MultiInput
<
Tensor
>
(
"StartsTensorList"
);
if
(
ctx
.
HasInput
(
"StartsTensor"
))
{
starts
=
GetDataFromTensor
<
int
>
(
ctx
.
Input
<
Tensor
>
(
"StartsTensor"
));
}
else
if
(
starts_tensor_list
.
size
()
>
0
)
{
starts
=
GetDataFromTensorList
<
int
>
(
starts_tensor_list
);
}
auto
ends_tensor_list
=
ctx
.
MultiInput
<
Tensor
>
(
"EndsTensorList"
);
if
(
ctx
.
HasInput
(
"EndsTensor"
))
{
ends
=
GetDataFromTensor
<
int
>
(
ctx
.
Input
<
Tensor
>
(
"EndsTensor"
));
}
else
if
(
ends_tensor_list
.
size
()
>
0
)
{
ends
=
GetDataFromTensorList
<
int
>
(
ends_tensor_list
);
}
const
auto
&
in_dims
=
input
->
dims
();
int
rank
=
in_dims
.
size
();
...
...
python/paddle/fluid/tests/unittests/npu/test_slice_op_npu.py
浏览文件 @
871edade
...
...
@@ -91,6 +91,164 @@ class TestSliceOpFp16(TestSliceOp):
self
.
place
=
paddle
.
NPUPlace
(
0
)
class
TestSliceOpTensor
(
TestSliceOp
):
def
setUp
(
self
):
self
.
op_type
=
"slice"
self
.
set_npu
()
self
.
init_dtype
()
self
.
config
()
self
.
inputs
=
{
'Input'
:
self
.
input
,
'StartsTensor'
:
self
.
starts
,
'EndsTensor'
:
self
.
ends
}
self
.
outputs
=
{
'Out'
:
self
.
out
}
self
.
attrs
=
{
'axes'
:
self
.
axes
,
'starts'
:
[
-
1
,
-
1
,
-
1
],
'ends'
:
[
-
1
,
-
1
,
-
1
],
'infer_flags'
:
self
.
infer_flags
}
def
config
(
self
):
self
.
input
=
np
.
random
.
random
([
3
,
4
,
5
,
6
]).
astype
(
self
.
dtype
)
self
.
starts
=
np
.
array
([
1
,
0
,
2
]).
astype
(
'int32'
)
self
.
ends
=
np
.
array
([
3
,
3
,
4
]).
astype
(
'int32'
)
self
.
axes
=
[
0
,
1
,
2
]
self
.
infer_flags
=
[
-
1
,
-
1
,
-
1
]
self
.
out
=
self
.
input
[
1
:
3
,
0
:
3
,
2
:
4
,
:]
class
TestSliceOpTensor2
(
TestSliceOpTensor
):
def
setUp
(
self
):
self
.
op_type
=
"slice"
self
.
set_npu
()
self
.
init_dtype
()
self
.
config
()
self
.
inputs
=
{
'Input'
:
self
.
input
,
'StartsTensor'
:
self
.
starts
,
'EndsTensor'
:
self
.
ends
}
self
.
outputs
=
{
'Out'
:
self
.
out
}
self
.
attrs
=
{
'axes'
:
self
.
axes
,
'starts'
:
[
-
1
],
'ends'
:
[
-
1
],
'infer_flags'
:
self
.
infer_flags
}
def
config
(
self
):
self
.
input
=
np
.
random
.
random
([
10
,
5
,
6
]).
astype
(
self
.
dtype
)
self
.
starts
=
np
.
array
([
0
]).
astype
(
'int32'
)
self
.
ends
=
np
.
array
([
1
]).
astype
(
'int32'
)
self
.
axes
=
[
1
]
self
.
infer_flags
=
[
-
1
]
self
.
out
=
self
.
input
[:,
0
:
1
,
:]
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
"core is not compiled with NPU"
)
class
TestSliceOpFp16Tensor
(
TestSliceOpTensor
):
def
init_dtype
(
self
):
self
.
dtype
=
np
.
float16
def
set_npu
(
self
):
self
.
__class__
.
use_npu
=
True
self
.
__class__
.
no_need_check_grad
=
True
self
.
place
=
paddle
.
NPUPlace
(
0
)
class
TestSliceOpTensorList
(
TestSliceOp
):
def
setUp
(
self
):
self
.
op_type
=
"slice"
self
.
set_npu
()
self
.
init_dtype
()
self
.
config
()
self
.
starts_tensor_list
=
[]
for
index
,
ele
in
enumerate
(
self
.
starts
):
self
.
starts_tensor_list
.
append
((
"start"
+
str
(
index
),
np
.
ones
(
(
1
)).
astype
(
'int32'
)
*
ele
))
self
.
ends_tensor_list
=
[]
for
index
,
ele
in
enumerate
(
self
.
ends
):
self
.
ends_tensor_list
.
append
((
"end"
+
str
(
index
),
np
.
ones
(
(
1
)).
astype
(
'int32'
)
*
ele
))
self
.
inputs
=
{
'Input'
:
self
.
input
,
'StartsTensorList'
:
self
.
starts_tensor_list
,
'EndsTensorList'
:
self
.
ends_tensor_list
}
self
.
outputs
=
{
'Out'
:
self
.
out
}
self
.
attrs
=
{
'axes'
:
self
.
axes
,
'starts'
:
[
-
1
,
-
1
,
-
1
],
'ends'
:
[
-
1
,
-
1
,
-
1
],
'infer_flags'
:
self
.
infer_flags
}
def
config
(
self
):
self
.
input
=
np
.
random
.
random
([
3
,
4
,
5
,
6
]).
astype
(
self
.
dtype
)
self
.
starts
=
[
1
,
0
,
2
]
self
.
ends
=
[
3
,
3
,
4
]
self
.
axes
=
[
0
,
1
,
2
]
self
.
infer_flags
=
[
-
1
,
-
1
,
-
1
]
self
.
out
=
self
.
input
[
1
:
3
,
0
:
3
,
2
:
4
,
:]
class
TestSliceOpTensorList2
(
TestSliceOpTensorList
):
def
setUp
(
self
):
self
.
op_type
=
"slice"
self
.
set_npu
()
self
.
init_dtype
()
self
.
config
()
self
.
starts_tensor_list
=
[]
for
index
,
ele
in
enumerate
(
self
.
starts
):
self
.
starts_tensor_list
.
append
((
"start"
+
str
(
index
),
np
.
ones
(
(
1
)).
astype
(
'int32'
)
*
ele
))
self
.
ends_tensor_list
=
[]
for
index
,
ele
in
enumerate
(
self
.
ends
):
self
.
ends_tensor_list
.
append
((
"end"
+
str
(
index
),
np
.
ones
(
(
1
)).
astype
(
'int32'
)
*
ele
))
self
.
inputs
=
{
'Input'
:
self
.
input
,
'StartsTensorList'
:
self
.
starts_tensor_list
,
'EndsTensorList'
:
self
.
ends_tensor_list
}
self
.
outputs
=
{
'Out'
:
self
.
out
}
self
.
attrs
=
{
'axes'
:
self
.
axes
,
'starts'
:
[
-
1
],
'ends'
:
[
-
1
],
'infer_flags'
:
self
.
infer_flags
}
def
config
(
self
):
self
.
input
=
np
.
random
.
random
([
10
,
5
,
6
]).
astype
(
self
.
dtype
)
self
.
starts
=
np
.
array
([
0
]).
astype
(
'int32'
)
self
.
ends
=
np
.
array
([
1
]).
astype
(
'int32'
)
self
.
axes
=
[
1
]
self
.
infer_flags
=
[
-
1
]
self
.
out
=
self
.
input
[:,
0
:
1
,
:]
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
"core is not compiled with NPU"
)
class
TestSliceOpFp16TensorList
(
TestSliceOpTensorList
):
def
init_dtype
(
self
):
self
.
dtype
=
np
.
float16
def
set_npu
(
self
):
self
.
__class__
.
use_npu
=
True
self
.
__class__
.
no_need_check_grad
=
True
self
.
place
=
paddle
.
NPUPlace
(
0
)
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
"core is not compiled with NPU"
)
class
TestSliceNet
(
unittest
.
TestCase
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录