Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
59ba0f95
P
Paddle
项目概览
Crayon鑫
/
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看板
提交
59ba0f95
编写于
9月 16, 2020
作者:
T
Thunderbrook
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add xpu slice op
test=xpu
上级
ef6dd6b8
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
305 addition
and
0 deletion
+305
-0
paddle/fluid/operators/xpu/slice_xpu_op.cc
paddle/fluid/operators/xpu/slice_xpu_op.cc
+190
-0
python/paddle/fluid/tests/unittests/test_slice_op.py
python/paddle/fluid/tests/unittests/test_slice_op.py
+115
-0
未找到文件。
paddle/fluid/operators/xpu/slice_xpu_op.cc
0 → 100644
浏览文件 @
59ba0f95
/* Copyright (c) 2018 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_XPU
#include "paddle/fluid/operators/slice_op.h"
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
DeviceContext
,
typename
T
>
class
SliceXPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
"Input"
);
auto
out
=
ctx
.
Output
<
framework
::
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
in_dims
=
in
->
dims
();
// prepare starts, ends on XPU
int
dim_value
=
0
,
start
=
0
,
end
=
0
;
// If a negative value is passed for any of the start or end indices,
// it represents number of elements before the end of that dimension.
// If the value passed to start or end is larger than the n
// (the number of elements in this dimension), it represents n.
for
(
size_t
i
=
0
;
i
<
axes
.
size
();
++
i
)
{
dim_value
=
in_dims
[
axes
[
i
]];
start
=
starts
[
i
];
end
=
ends
[
i
];
start
=
start
<
0
?
(
start
+
dim_value
)
:
start
;
end
=
end
<
0
?
(
end
+
dim_value
)
:
end
;
start
=
std
::
max
(
start
,
0
);
end
=
std
::
max
(
end
,
0
);
end
=
std
::
min
(
end
,
dim_value
);
PADDLE_ENFORCE_GT
(
end
,
start
,
"end should greater than start"
);
starts
[
i
]
=
start
;
ends
[
i
]
=
end
;
}
size_t
shape_size
=
in_dims
.
size
();
// the slice XPU kernel require that the length of `start`, `end` must be equal
// to the dims size of input tensor, therefore, if shape_size > axes.size(),
// the `starts_extension` and `ends_extension` is necessary.
std
::
vector
<
int
>
starts_extension
(
shape_size
,
0
);
std
::
vector
<
int
>
ends_extension
(
shape_size
,
0
);
if
(
shape_size
>
axes
.
size
())
{
for
(
size_t
i
=
0
;
i
<
shape_size
;
++
i
){
ends_extension
[
i
]
=
in_dims
[
i
];
}
for
(
size_t
i
=
0
;
i
<
axes
.
size
();
++
i
)
{
starts_extension
[
axes
[
i
]]
=
starts
[
i
];
ends_extension
[
axes
[
i
]]
=
ends
[
i
];
}
}
else
{
starts_extension
=
std
::
move
(
starts
);
ends_extension
=
std
::
move
(
ends
);
}
// prepare shape on XPU
std
::
vector
<
int
>
shape
(
shape_size
,
0
);
for
(
size_t
i
=
0
;
i
<
shape_size
;
++
i
)
{
shape
[
i
]
=
in_dims
[
i
];
}
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
auto
*
in_data
=
in
->
data
<
T
>
();
auto
*
out_data
=
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
int
r
=
xpu
::
slice_forward
(
dev_ctx
.
x_context
(),
shape
.
data
(),
starts_extension
.
data
(),
ends_extension
.
data
(),
shape_size
,
in_data
,
out_data
);
PADDLE_ENFORCE
(
r
==
xpu
::
Error_t
::
SUCCESS
,
"XPU kernel error!"
);
}
};
template
<
typename
DeviceContext
,
typename
T
>
class
SliceGradXPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
d_out
=
ctx
.
Input
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
d_in
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Input"
));
d_in
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
in_dims
=
d_in
->
dims
();
auto
axes
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"axes"
);
auto
starts
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"starts"
);
auto
ends
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"ends"
);
// prepare starts, ends on XPU
int
dim_value
=
0
,
start
=
0
,
end
=
0
;
// If a negative value is passed for any of the start or end indices,
// it represents number of elements before the end of that dimension.
// If the value passed to start or end is larger than the n
// (the number of elements in this dimension), it represents n.
for
(
size_t
i
=
0
;
i
<
axes
.
size
();
++
i
)
{
dim_value
=
in_dims
[
axes
[
i
]];
start
=
starts
[
i
];
end
=
ends
[
i
];
start
=
start
<
0
?
(
start
+
dim_value
)
:
start
;
end
=
end
<
0
?
(
end
+
dim_value
)
:
end
;
start
=
std
::
max
(
start
,
0
);
end
=
std
::
max
(
end
,
0
);
end
=
std
::
min
(
end
,
dim_value
);
PADDLE_ENFORCE_GT
(
end
,
start
,
"end should greater than start"
);
starts
[
i
]
=
start
;
ends
[
i
]
=
end
;
}
size_t
shape_size
=
in_dims
.
size
();
// the slice XPU kernel require that the length of `start`, `end` must be equal
// to the dims size of input tensor, therefore, if shape_size > axes.size(),
// the `starts_extension` and `ends_extension` is necessary.
std
::
vector
<
int
>
starts_extension
(
shape_size
,
0
);
std
::
vector
<
int
>
ends_extension
(
shape_size
,
0
);
if
(
shape_size
>
axes
.
size
())
{
for
(
size_t
i
=
0
;
i
<
shape_size
;
++
i
){
ends_extension
[
i
]
=
in_dims
[
i
];
}
for
(
size_t
i
=
0
;
i
<
axes
.
size
();
++
i
)
{
starts_extension
[
axes
[
i
]]
=
starts
[
i
];
ends_extension
[
axes
[
i
]]
=
ends
[
i
];
}
}
int
*
starts_device
=
nullptr
;
int
*
ends_device
=
nullptr
;
int
*
starts_host
=
shape_size
>
axes
.
size
()
?
starts_extension
.
data
()
:
starts
.
data
();
int
*
ends_host
=
shape_size
>
axes
.
size
()
?
ends_extension
.
data
()
:
ends
.
data
();
PADDLE_ENFORCE
(
xpu_malloc
((
void
**
)(
&
starts_device
),
shape_size
*
sizeof
(
int
))
==
XPU_SUCCESS
);
PADDLE_ENFORCE
(
xpu_malloc
((
void
**
)(
&
ends_device
),
shape_size
*
sizeof
(
int
))
==
XPU_SUCCESS
);
memory
::
Copy
(
boost
::
get
<
platform
::
XPUPlace
>
(
ctx
.
GetPlace
()),
starts_device
,
platform
::
CPUPlace
(),
starts_host
,
shape_size
*
sizeof
(
int
));
memory
::
Copy
(
boost
::
get
<
platform
::
XPUPlace
>
(
ctx
.
GetPlace
()),
ends_device
,
platform
::
CPUPlace
(),
ends_host
,
shape_size
*
sizeof
(
int
));
// prepare shape on XPU
std
::
vector
<
int
>
shape
(
shape_size
,
0
);
for
(
size_t
i
=
0
;
i
<
shape_size
;
++
i
)
{
shape
[
i
]
=
in_dims
[
i
];
}
int
*
shape_device
=
nullptr
;
PADDLE_ENFORCE
(
xpu_malloc
((
void
**
)(
&
shape_device
),
shape_size
*
sizeof
(
int
))
==
XPU_SUCCESS
);
memory
::
Copy
(
boost
::
get
<
platform
::
XPUPlace
>
(
ctx
.
GetPlace
()),
shape_device
,
platform
::
CPUPlace
(),
shape
.
data
(),
shape_size
*
sizeof
(
int
));
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
int
r
=
xpu
::
slice_backward
(
dev_ctx
.
x_context
(),
shape_device
,
starts_device
,
ends_device
,
shape_size
,
d_out
->
data
<
T
>
(),
d_in
->
data
<
T
>
(),
d_in
->
numel
(),
d_out
->
numel
());
PADDLE_ENFORCE
(
r
==
xpu
::
Error_t
::
SUCCESS
,
"XPU kernel error!"
);
dev_ctx
.
Wait
();
// free device data
xpu_free
(
shape_device
);
xpu_free
(
starts_device
);
xpu_free
(
ends_device
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_XPU_KERNEL
(
slice
,
ops
::
SliceXPUKernel
<
paddle
::
platform
::
XPUDeviceContext
,
float
>
);
REGISTER_OP_XPU_KERNEL
(
slice_grad
,
ops
::
SliceGradXPUKernel
<
paddle
::
platform
::
XPUDeviceContext
,
float
>
);
#endif
python/paddle/fluid/tests/unittests/test_slice_op.py
浏览文件 @
59ba0f95
...
...
@@ -677,6 +677,121 @@ class TestImperativeCUDAPinnedInput(unittest.TestCase):
zero_copy
=
False
)
sliced
=
var
[:,
10
:,
:
var
.
shape
[
1
]]
self
.
assertEqual
(
sliced
.
shape
,
[
2
,
70
,
80
])
# for xpu
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_xpu
(),
"core is not compiled with XPU"
)
class
TestXpuSliceOp
(
TestSliceOp
):
def
test_check_output
(
self
):
place
=
core
.
XPUPlace
(
0
)
self
.
check_output_with_place
(
place
)
def
test_check_grad_normal
(
self
):
place
=
core
.
XPUPlace
(
0
)
self
.
check_grad_with_place
(
place
,
[
'Input'
],
'Out'
,
max_relative_error
=
0.006
)
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_xpu
(),
"core is not compiled with XPU"
)
class
TestXpuCase1
(
TestXpuSliceOp
):
def
config
(
self
):
self
.
input
=
np
.
random
.
random
([
3
,
4
,
5
,
6
]).
astype
(
"float64"
)
self
.
starts
=
[
-
3
,
0
,
2
]
self
.
ends
=
[
3
,
100
,
-
1
]
self
.
axes
=
[
0
,
1
,
2
]
self
.
infer_flags
=
[
1
,
1
,
1
]
self
.
out
=
self
.
input
[
-
3
:
3
,
0
:
100
,
2
:
-
1
,
:]
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_xpu
(),
"core is not compiled with XPU"
)
class
TestXpuCase2
(
TestXpuSliceOp
):
def
config
(
self
):
self
.
input
=
np
.
random
.
random
([
3
,
4
,
5
,
6
]).
astype
(
"float64"
)
self
.
starts
=
[
-
3
,
0
,
2
]
self
.
ends
=
[
3
,
100
,
-
1
]
self
.
axes
=
[
0
,
1
,
3
]
self
.
infer_flags
=
[
1
,
1
,
1
]
self
.
out
=
self
.
input
[
-
3
:
3
,
0
:
100
,
:,
2
:
-
1
]
# 1.2 with attr(decrease)
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_xpu
(),
"core is not compiled with XPU"
)
class
TestXpuSliceOp_decs_dim
(
TestSliceOp_decs_dim
):
def
test_check_output
(
self
):
place
=
core
.
XPUPlace
(
0
)
self
.
check_output_with_place
(
place
)
def
test_check_grad_normal
(
self
):
place
=
core
.
XPUPlace
(
0
)
self
.
check_grad_with_place
(
place
,
[
'Input'
],
'Out'
,
max_relative_error
=
0.006
)
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_xpu
(),
"core is not compiled with XPU"
)
class
TestXpuSliceOp_decs_dim_2
(
TestXpuSliceOp_decs_dim
):
def
config
(
self
):
self
.
input
=
np
.
random
.
random
([
3
,
4
,
5
,
6
]).
astype
(
"float64"
)
self
.
starts
=
[
1
,
0
,
2
]
self
.
ends
=
[
2
,
1
,
4
]
self
.
axes
=
[
0
,
1
,
2
]
self
.
decrease_axis
=
[
0
,
1
]
self
.
infer_flags
=
[
1
,
1
,
1
]
self
.
out
=
self
.
input
[
1
,
0
,
2
:
4
,
:]
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_xpu
(),
"core is not compiled with XPU"
)
class
TestXpuSliceOp_decs_dim_3
(
TestXpuSliceOp_decs_dim
):
def
config
(
self
):
self
.
input
=
np
.
random
.
random
([
3
,
4
,
5
,
6
]).
astype
(
"float64"
)
self
.
starts
=
[
-
1
,
0
,
2
]
self
.
ends
=
[
1000000
,
1
,
4
]
self
.
axes
=
[
0
,
1
,
2
]
self
.
decrease_axis
=
[
0
,
1
]
self
.
infer_flags
=
[
1
,
1
,
1
]
self
.
out
=
self
.
input
[
-
1
,
0
,
2
:
4
,
:]
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_xpu
(),
"core is not compiled with XPU"
)
class
TestXpuSliceOp_decs_dim_4
(
TestXpuSliceOp_decs_dim
):
def
config
(
self
):
self
.
input
=
np
.
random
.
random
([
3
,
4
,
5
,
7
]).
astype
(
"float64"
)
self
.
starts
=
[
0
,
1
,
2
,
3
]
self
.
ends
=
[
1
,
2
,
3
,
4
]
self
.
axes
=
[
0
,
1
,
2
,
3
]
self
.
decrease_axis
=
[
0
,
1
,
2
,
3
]
self
.
infer_flags
=
[
1
,
1
,
1
]
self
.
out
=
self
.
input
[
0
,
1
,
2
,
3
:
4
]
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_xpu
(),
"core is not compiled with XPU"
)
class
TestXpuSliceOp_decs_dim_5
(
TestXpuSliceOp_decs_dim
):
def
config
(
self
):
self
.
input
=
np
.
random
.
random
([
3
,
4
,
5
,
6
]).
astype
(
"float64"
)
self
.
starts
=
[
-
1
]
self
.
ends
=
[
1000000
]
self
.
axes
=
[
3
]
self
.
decrease_axis
=
[
3
]
self
.
infer_flags
=
[
1
,
1
,
1
]
self
.
out
=
self
.
input
[:,
:,
:,
-
1
]
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_xpu
(),
"core is not compiled with XPU"
)
class
TestXpuSliceOp_decs_dim_6
(
TestXpuSliceOp_decs_dim
):
def
config
(
self
):
self
.
input
=
np
.
random
.
random
([
3
,
4
,
5
,
6
]).
astype
(
"float64"
)
self
.
starts
=
[
0
,
1
,
2
,
3
]
self
.
ends
=
[
1
,
2
,
3
,
4
]
self
.
axes
=
[
0
,
1
,
2
,
3
]
self
.
decrease_axis
=
[
0
,
1
,
2
,
3
]
self
.
infer_flags
=
[
1
,
1
,
1
]
self
.
out
=
self
.
input
[
0
,
1
,
2
,
3
:
4
]
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录