Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
ddb3868e
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
ddb3868e
编写于
5月 12, 2022
作者:
F
fwenguang
提交者:
GitHub
5月 12, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[MLU] add slice kernel (#42245)
上级
bf44034c
变更
3
展开全部
显示空白变更内容
内联
并排
Showing
3 changed file
with
830 addition
and
2 deletion
+830
-2
paddle/fluid/operators/mlu/mlu_baseop.cc
paddle/fluid/operators/mlu/mlu_baseop.cc
+3
-2
paddle/fluid/operators/slice_op_mlu.cc
paddle/fluid/operators/slice_op_mlu.cc
+196
-0
python/paddle/fluid/tests/unittests/mlu/test_slice_op_mlu.py
python/paddle/fluid/tests/unittests/mlu/test_slice_op_mlu.py
+631
-0
未找到文件。
paddle/fluid/operators/mlu/mlu_baseop.cc
浏览文件 @
ddb3868e
...
...
@@ -688,8 +688,9 @@ MLUCnnlTrigonDesc::~MLUCnnlTrigonDesc() {
const
cnnlTensorDescriptor_t
diff_y_desc
,
void
*
back_out
)
{
cnnlHandle_t
handle
=
GetHandleFromCTX
(
ctx
);
PADDLE_ENFORCE_MLU_SUCCESS
(
cnnlSparseSoftmaxCrossEntropyWithLogits
(
handle
,
mode
,
x_desc
,
input
,
label_desc
,
label
,
y_desc
,
output
,
const
cnnlComputationPreference_t
prefer
=
CNNL_COMPUTATION_HIGH_PRECISION
;
PADDLE_ENFORCE_MLU_SUCCESS
(
cnnlSparseSoftmaxCrossEntropyWithLogits_v2
(
handle
,
prefer
,
mode
,
x_desc
,
input
,
label_desc
,
label
,
y_desc
,
output
,
diff_y_desc
,
back_out
));
}
...
...
paddle/fluid/operators/slice_op_mlu.cc
0 → 100644
浏览文件 @
ddb3868e
/* 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/operators/slice_op.h"
#include "paddle/fluid/operators/mlu/mlu_baseop.h"
#include "paddle/phi/kernels/funcs/slice_utils.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
T
>
class
SliceMLUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
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
decrease_axis
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"decrease_axis"
);
auto
infer_flags
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"infer_flags"
);
// 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."
));
const
auto
&
in_dims
=
input
->
dims
();
auto
slice_dims
=
out
->
dims
();
bool
reset_slice_dims
=
false
;
if
(
ctx
.
HasInput
(
"StartsTensor"
)
||
ctx
.
HasInput
(
"EndsTensor"
)
||
starts_tensor_list
.
size
()
>
0
||
ends_tensor_list
.
size
()
>
0
)
{
// Infer output 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
]];
}
}
}
phi
::
funcs
::
CheckAndUpdateSliceAttrs
(
in_dims
,
axes
,
&
starts
,
&
ends
);
slice_dims
=
phi
::
funcs
::
GetSliceDims
<
int
>
(
in_dims
,
axes
,
starts
,
ends
,
nullptr
,
nullptr
);
reset_slice_dims
=
true
;
auto
out_dims
=
phi
::
funcs
::
GetDecreasedDims
(
slice_dims
,
decrease_axis
);
out
->
Resize
(
out_dims
);
}
if
(
slice_dims
.
size
()
!=
in_dims
.
size
()
&&
!
reset_slice_dims
)
{
phi
::
funcs
::
CheckAndUpdateSliceAttrs
(
in_dims
,
axes
,
&
starts
,
&
ends
);
slice_dims
=
phi
::
funcs
::
GetSliceDims
<
int
>
(
in_dims
,
axes
,
starts
,
ends
,
nullptr
,
nullptr
);
}
int
in_dim_size
=
input
->
dims
().
size
();
if
(
static_cast
<
int
>
(
axes
.
size
())
!=
in_dim_size
)
{
std
::
vector
<
int
>
tmp_starts
(
in_dim_size
,
0
);
const
auto
&
in_dims_vec
=
phi
::
vectorize
(
input
->
dims
());
std
::
vector
<
int
>
tmp_ends
(
in_dims_vec
.
begin
(),
in_dims_vec
.
end
());
for
(
size_t
i
=
0
;
i
<
axes
.
size
();
++
i
)
{
tmp_starts
[
axes
[
i
]]
=
starts
[
i
];
tmp_ends
[
axes
[
i
]]
=
ends
[
i
];
}
starts
.
swap
(
tmp_starts
);
ends
.
swap
(
tmp_ends
);
}
std
::
vector
<
int
>
strides
(
in_dim_size
,
1
);
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
MLUCnnlTensorDesc
input_desc
(
*
input
);
MLUCnnlTensorDesc
out_desc
(
slice_dims
.
size
(),
phi
::
vectorize
(
slice_dims
).
data
(),
ToCnnlDataType
<
T
>
());
MLUCnnl
::
StridedSlice
(
ctx
,
starts
.
data
(),
ends
.
data
(),
strides
.
data
(),
input_desc
.
get
(),
GetBasePtr
(
input
),
out_desc
.
get
(),
GetBasePtr
(
out
));
}
};
template
<
typename
T
>
class
SliceGradMLUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
input
=
ctx
.
Input
<
Tensor
>
(
"Input"
);
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"
);
// 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
();
auto
slice_dims
=
dout
->
dims
();
if
(
slice_dims
.
size
()
!=
in_dims
.
size
())
{
phi
::
funcs
::
CheckAndUpdateSliceAttrs
(
in_dims
,
axes
,
&
starts
,
&
ends
);
slice_dims
=
phi
::
funcs
::
GetSliceDims
<
int
>
(
in_dims
,
axes
,
starts
,
ends
,
nullptr
,
nullptr
);
}
int
in_dim_size
=
input
->
dims
().
size
();
if
(
static_cast
<
int
>
(
axes
.
size
())
!=
in_dim_size
)
{
std
::
vector
<
int
>
tmp_starts
(
in_dim_size
,
0
);
const
auto
&
in_dims_vec
=
phi
::
vectorize
(
input
->
dims
());
std
::
vector
<
int
>
tmp_ends
(
in_dims_vec
.
begin
(),
in_dims_vec
.
end
());
for
(
size_t
i
=
0
;
i
<
axes
.
size
();
++
i
)
{
tmp_starts
[
axes
[
i
]]
=
starts
[
i
];
tmp_ends
[
axes
[
i
]]
=
ends
[
i
];
}
starts
.
swap
(
tmp_starts
);
ends
.
swap
(
tmp_ends
);
}
std
::
vector
<
int
>
strides
(
in_dim_size
,
1
);
dinput
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
MLUCnnlTensorDesc
dout_desc
(
slice_dims
.
size
(),
phi
::
vectorize
(
slice_dims
).
data
(),
ToCnnlDataType
<
T
>
());
MLUCnnlTensorDesc
dinput_desc
(
*
dinput
);
MLUCnnl
::
StridedSliceGrad
(
ctx
,
starts
.
data
(),
ends
.
data
(),
strides
.
data
(),
dout_desc
.
get
(),
GetBasePtr
(
dout
),
dinput_desc
.
get
(),
GetBasePtr
(
dinput
));
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_MLU_KERNEL
(
slice
,
ops
::
SliceMLUKernel
<
float
>
,
ops
::
SliceMLUKernel
<
int
>
,
ops
::
SliceMLUKernel
<
bool
>
,
ops
::
SliceMLUKernel
<
int64_t
>
,
ops
::
SliceMLUKernel
<
double
>
,
ops
::
SliceMLUKernel
<
paddle
::
platform
::
float16
>
);
REGISTER_OP_MLU_KERNEL
(
slice_grad
,
ops
::
SliceGradMLUKernel
<
float
>
,
ops
::
SliceGradMLUKernel
<
int
>
,
ops
::
SliceGradMLUKernel
<
bool
>
,
ops
::
SliceGradMLUKernel
<
int64_t
>
,
ops
::
SliceGradMLUKernel
<
paddle
::
platform
::
float16
>
);
python/paddle/fluid/tests/unittests/mlu/test_slice_op_mlu.py
0 → 100644
浏览文件 @
ddb3868e
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录