Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
423dda37
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看板
未验证
提交
423dda37
编写于
5月 22, 2023
作者:
L
lijin23
提交者:
GitHub
5月 22, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[XPU][PHI Kernels] fix errors when numel is zero for xpu (#54010)
* fix empty bugs for xpu * fix empty bugs for xpu
上级
664a2753
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
77 addition
and
67 deletion
+77
-67
paddle/phi/kernels/xpu/cast_kernel.cc
paddle/phi/kernels/xpu/cast_kernel.cc
+38
-62
paddle/phi/kernels/xpu/gather_kernel.cc
paddle/phi/kernels/xpu/gather_kernel.cc
+1
-1
paddle/phi/kernels/xpu/scale_kernel.cc
paddle/phi/kernels/xpu/scale_kernel.cc
+3
-4
test/xpu/test_cast_op_xpu.py
test/xpu/test_cast_op_xpu.py
+12
-0
test/xpu/test_gather_op_xpu.py
test/xpu/test_gather_op_xpu.py
+12
-0
test/xpu/test_scale_op_xpu.py
test/xpu/test_scale_op_xpu.py
+11
-0
未找到文件。
paddle/phi/kernels/xpu/cast_kernel.cc
浏览文件 @
423dda37
...
...
@@ -15,90 +15,66 @@
#include "paddle/phi/kernels/cast_kernel.h"
#include "paddle/phi/backends/xpu/enforce_xpu.h"
#include "paddle/phi/backends/xpu/xpu_context.h"
#include "paddle/phi/backends/xpu/xpu_header.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/kernel_registry.h"
namespace
phi
{
template
<
typename
InT
,
typename
OutT
,
typename
Context
>
void
CastXPUKernelImpl
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
DenseTensor
*
out
)
{
using
XPUInT
=
typename
XPUTypeTrait
<
InT
>::
Type
;
using
XPUOutT
=
typename
XPUTypeTrait
<
OutT
>::
Type
;
const
auto
*
in_data
=
x
.
data
<
InT
>
();
auto
*
out_data
=
dev_ctx
.
template
Alloc
<
OutT
>(
out
);
auto
numel
=
x
.
numel
();
if
(
numel
==
0
)
{
return
;
}
int
r
=
xpu
::
cast
<
XPUInT
,
XPUOutT
>
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUInT
*>
(
in_data
),
reinterpret_cast
<
XPUOutT
*>
(
out_data
),
numel
);
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"cast"
);
}
template
<
typename
T
,
typename
Context
>
void
CastKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
DataType
out_dtype
,
DenseTensor
*
out
)
{
using
XPUInTDType
=
typename
XPUTypeTrait
<
T
>::
Type
;
using
XPUTypeFP16
=
typename
XPUTypeTrait
<
phi
::
dtype
::
float16
>::
Type
;
auto
*
in_data
=
x
.
data
<
T
>
();
auto
numel
=
x
.
numel
();
int
r
=
-
1
;
switch
(
out_dtype
)
{
case
phi
::
DataType
::
FLOAT32
:
r
=
xpu
::
cast
<
XPUInTDType
,
float
>
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUInTDType
*>
(
in_data
),
dev_ctx
.
template
Alloc
<
float
>(
out
),
numel
);
case
DataType
::
INT32
:
CastXPUKernelImpl
<
T
,
int
,
Context
>
(
dev_ctx
,
x
,
out
);
break
;
case
phi
::
DataType
::
FLOAT16
:
r
=
xpu
::
cast
<
XPUInTDType
,
XPUTypeFP16
>
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUInTDType
*>
(
in_data
),
reinterpret_cast
<
XPUTypeFP16
*>
(
dev_ctx
.
template
Alloc
<
phi
::
dtype
::
float16
>(
out
)),
numel
);
case
DataType
::
FLOAT32
:
CastXPUKernelImpl
<
T
,
float
,
Context
>
(
dev_ctx
,
x
,
out
);
break
;
case
phi
::
DataType
::
INT64
:
r
=
xpu
::
cast
<
XPUInTDType
,
int64_t
>
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUInTDType
*>
(
in_data
),
dev_ctx
.
template
Alloc
<
int64_t
>(
out
),
numel
);
case
DataType
::
FLOAT16
:
CastXPUKernelImpl
<
T
,
dtype
::
float16
,
Context
>
(
dev_ctx
,
x
,
out
);
break
;
case
phi
::
DataType
::
INT32
:
r
=
xpu
::
cast
<
XPUInTDType
,
int32_t
>
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUInTDType
*>
(
in_data
),
dev_ctx
.
template
Alloc
<
int
>(
out
),
numel
);
case
DataType
::
INT64
:
CastXPUKernelImpl
<
T
,
int64_t
,
Context
>
(
dev_ctx
,
x
,
out
);
break
;
case
phi
::
DataType
::
BOOL
:
r
=
xpu
::
cast
<
XPUInTDType
,
bool
>
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUInTDType
*>
(
in_data
),
dev_ctx
.
template
Alloc
<
bool
>(
out
),
numel
);
case
DataType
::
BOOL
:
CastXPUKernelImpl
<
T
,
bool
,
Context
>
(
dev_ctx
,
x
,
out
);
break
;
case
phi
::
DataType
::
INT8
:
r
=
xpu
::
cast
<
XPUInTDType
,
int8_t
>
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUInTDType
*>
(
in_data
),
dev_ctx
.
template
Alloc
<
int8_t
>(
out
),
numel
);
case
DataType
::
INT8
:
CastXPUKernelImpl
<
T
,
int8_t
,
Context
>
(
dev_ctx
,
x
,
out
);
break
;
case
phi
::
DataType
::
UINT8
:
r
=
xpu
::
cast
<
XPUInTDType
,
uint8_t
>
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUInTDType
*>
(
in_data
),
dev_ctx
.
template
Alloc
<
uint8_t
>(
out
),
numel
);
case
DataType
::
UINT8
:
CastXPUKernelImpl
<
T
,
uint8_t
,
Context
>
(
dev_ctx
,
x
,
out
);
break
;
case
phi
::
DataType
::
FLOAT64
:
r
=
xpu
::
cast
<
XPUInTDType
,
double
>
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUInTDType
*>
(
in_data
),
dev_ctx
.
template
Alloc
<
double
>(
out
),
numel
);
case
DataType
::
FLOAT64
:
CastXPUKernelImpl
<
T
,
double
,
Context
>
(
dev_ctx
,
x
,
out
);
break
;
default:
PADDLE_THROW
(
phi
::
errors
::
Unavailable
(
"Not supported cast %d -> %d"
,
x
.
dtype
(),
out_dtype
));
}
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"cast"
);
}
}
// namespace phi
...
...
paddle/phi/kernels/xpu/gather_kernel.cc
浏览文件 @
423dda37
...
...
@@ -29,7 +29,7 @@ void GatherKernel(const Context& dev_ctx,
const
auto
&
index_type
=
index
.
dtype
();
dev_ctx
.
template
Alloc
<
T
>(
out
);
if
(
x
.
numel
()
==
0
)
return
;
if
(
x
.
numel
()
==
0
||
index
.
numel
()
==
0
)
return
;
const
auto
index_dims
=
index
.
dims
();
if
(
index_dims
.
size
()
==
2
)
{
...
...
paddle/phi/kernels/xpu/scale_kernel.cc
浏览文件 @
423dda37
...
...
@@ -15,10 +15,6 @@
#include "paddle/phi/kernels/scale_kernel.h"
#include "paddle/phi/backends/xpu/enforce_xpu.h"
#include "paddle/phi/backends/xpu/xpu_context.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/kernel_registry.h"
namespace
phi
{
...
...
@@ -39,6 +35,9 @@ void ScaleKernel(const Context& dev_ctx,
" expected %s, but got %s."
,
x
.
dims
().
to_str
().
c_str
(),
out
->
dims
().
to_str
().
c_str
()));
if
(
x
.
numel
()
==
0
||
!
x
.
IsInitialized
())
{
return
;
}
using
XPUType
=
typename
XPUTypeTrait
<
T
>::
Type
;
int
r
=
xpu
::
scale
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUType
*>
(
x
.
data
<
T
>
()),
...
...
test/xpu/test_cast_op_xpu.py
浏览文件 @
423dda37
...
...
@@ -99,6 +99,18 @@ class TestCastOpError(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
paddle
.
cast
,
x1
,
'int32'
)
class
TestCastOpEmpty
(
unittest
.
TestCase
):
def
test_cast_op_empty
(
self
):
if
paddle
.
is_compiled_with_xpu
():
paddle
.
set_device
(
'xpu'
)
paddle
.
disable_static
()
data
=
paddle
.
ones
([
0
,
10
],
dtype
=
'float32'
)
out
=
paddle
.
cast
(
data
,
'int32'
)
self
.
assertEqual
(
out
.
shape
,
data
.
shape
)
self
.
assertEqual
(
out
.
dtype
,
paddle
.
int32
)
paddle
.
enable_static
()
if
__name__
==
'__main__'
:
paddle
.
enable_static
()
unittest
.
main
()
test/xpu/test_gather_op_xpu.py
浏览文件 @
423dda37
...
...
@@ -112,6 +112,18 @@ class XPUTestGather(XPUOpTestWrapper):
self
.
index_type
=
np
.
int64
class
TestGatherOpEmpty
(
unittest
.
TestCase
):
def
test_gather_empty_index
(
self
):
if
paddle
.
is_compiled_with_xpu
():
paddle
.
set_device
(
'xpu'
)
paddle
.
disable_static
()
data
=
paddle
.
ones
([
10
],
dtype
=
'int32'
)
index
=
paddle
.
ones
([],
dtype
=
'int32'
)
out
=
paddle
.
gather
(
data
,
index
)
self
.
assertEqual
(
out
.
shape
,
index
.
shape
)
paddle
.
enable_static
()
support_types
=
get_xpu_op_support_types
(
'gather'
)
for
stype
in
support_types
:
create_test_class
(
globals
(),
XPUTestGather
,
stype
)
...
...
test/xpu/test_scale_op_xpu.py
浏览文件 @
423dda37
...
...
@@ -136,6 +136,17 @@ class TestScaleInplaceApiDygraph(TestScaleApiDygraph):
return
x
.
scale_
(
scale
,
bias
)
class
TestScaleOpZeroNumelVariable
(
unittest
.
TestCase
):
def
test_check_zero_numel_xpu
(
self
):
if
paddle
.
is_compiled_with_xpu
():
paddle
.
disable_static
()
paddle
.
set_device
(
'xpu'
)
data
=
paddle
.
ones
([
0
,
1
])
out
=
paddle
.
scale
(
data
,
2
)
self
.
assertEqual
(
out
.
shape
,
data
.
shape
)
paddle
.
enable_static
()
support_types
=
get_xpu_op_support_types
(
'scale'
)
for
stype
in
support_types
:
create_test_class
(
globals
(),
XPUTestScaleOp
,
stype
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录