Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
69aae171
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看板
未验证
提交
69aae171
编写于
2月 17, 2023
作者:
C
Ccc
提交者:
GitHub
2月 17, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Zero-dim] Zero-dim Tensor for XPU prelu, softmax and log_softmax (#50433)
上级
cea6a7c6
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
75 addition
and
18 deletion
+75
-18
paddle/phi/kernels/xpu/log_softmax_grad_kernel.cc
paddle/phi/kernels/xpu/log_softmax_grad_kernel.cc
+1
-0
paddle/phi/kernels/xpu/log_softmax_kernel.cc
paddle/phi/kernels/xpu/log_softmax_kernel.cc
+1
-0
paddle/phi/kernels/xpu/prelu_grad_kernel.cc
paddle/phi/kernels/xpu/prelu_grad_kernel.cc
+12
-4
paddle/phi/kernels/xpu/prelu_kernel.cc
paddle/phi/kernels/xpu/prelu_kernel.cc
+12
-6
paddle/phi/kernels/xpu/softmax_grad_kernel.cc
paddle/phi/kernels/xpu/softmax_grad_kernel.cc
+7
-0
python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
+13
-8
python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py
...dle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py
+29
-0
未找到文件。
paddle/phi/kernels/xpu/log_softmax_grad_kernel.cc
浏览文件 @
69aae171
...
...
@@ -33,6 +33,7 @@ void LogSoftmaxGradKernel(const Context& dev_ctx,
// For 0D Tensor
if
(
rank
==
0
)
{
dev_ctx
.
template
Alloc
<
T
>(
x_grad
);
phi
::
funcs
::
set_constant
(
dev_ctx
,
x_grad
,
0.0
);
return
;
}
...
...
paddle/phi/kernels/xpu/log_softmax_kernel.cc
浏览文件 @
69aae171
...
...
@@ -32,6 +32,7 @@ void LogSoftmaxKernel(const Context& dev_ctx,
// For 0D Tensor
if
(
rank
==
0
)
{
dev_ctx
.
template
Alloc
<
T
>(
out
);
phi
::
funcs
::
set_constant
(
dev_ctx
,
out
,
0.0
);
return
;
}
...
...
paddle/phi/kernels/xpu/prelu_grad_kernel.cc
浏览文件 @
69aae171
...
...
@@ -40,17 +40,25 @@ void PReluGradKernel(const Context& dev_ctx,
auto
x_rank
=
x_dim
.
size
();
std
::
vector
<
int
>
x_shape
(
x_rank
);
if
(
x_rank
==
0
)
{
x_shape
=
std
::
vector
<
int
>
({
1
});
}
else
{
for
(
int
i
=
0
;
i
<
x_rank
;
i
++
)
{
x_shape
[
i
]
=
x_dim
[
i
];
}
}
auto
alpha_dim
=
alpha
.
dims
();
auto
alpha_rank
=
alpha_dim
.
size
();
std
::
vector
<
int
>
alpha_shape
(
alpha_rank
);
if
(
alpha_rank
==
0
)
{
alpha_shape
=
std
::
vector
<
int
>
({
1
});
}
else
{
for
(
int
i
=
0
;
i
<
x_rank
;
i
++
)
{
alpha_shape
[
i
]
=
alpha_dim
[
i
];
}
}
// mode = 0: channel_nchw, slope_shape = {c}, default. meanwhile, xhsape = {n,
// c, h, w}
...
...
paddle/phi/kernels/xpu/prelu_kernel.cc
浏览文件 @
69aae171
...
...
@@ -34,21 +34,27 @@ void PReluKernel(const Context& dev_ctx,
auto
x_dim
=
x
.
dims
();
auto
x_rank
=
x_dim
.
size
();
std
::
vector
<
int
>
x_shape
(
x_rank
);
if
(
x_rank
==
0
)
{
x_shape
=
std
::
vector
<
int
>
({
1
});
}
else
{
for
(
int
i
=
0
;
i
<
x_rank
;
i
++
)
{
x_shape
[
i
]
=
x_dim
[
i
];
}
}
auto
alpha_dim
=
alpha
.
dims
();
auto
alpha_rank
=
alpha_dim
.
size
();
std
::
vector
<
int
>
alpha_shape
(
x_rank
,
1
);
// same size with x_shape
if
(
x_rank
==
0
)
{
alpha_shape
=
std
::
vector
<
int
>
({
1
});
}
else
{
for
(
int
i
=
0
;
i
<
alpha_rank
;
i
++
)
{
alpha_shape
[
i
]
=
alpha_dim
[
i
];
}
}
int
r
=
xpu
::
prelu
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUType
*>
(
x_ptr
),
...
...
paddle/phi/kernels/xpu/softmax_grad_kernel.cc
浏览文件 @
69aae171
...
...
@@ -16,6 +16,7 @@ limitations under the License. */
#include "paddle/phi/backends/xpu/enforce_xpu.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/axis_utils.h"
#include "paddle/phi/kernels/funcs/math_function.h"
namespace
phi
{
...
...
@@ -35,6 +36,12 @@ void SoftmaxGradKernel(const Context& dev_ctx,
return
;
}
// For 0D Tensor
if
(
rank
==
0
)
{
phi
::
funcs
::
set_constant
(
dev_ctx
,
x_grad
,
0.0
);
return
;
}
std
::
vector
<
int
>
x_dims
;
for
(
int
i
=
0
;
i
<
rank
;
i
++
)
{
x_dims
.
push_back
(
x_grad
->
dims
()[
i
]);
...
...
python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
浏览文件 @
69aae171
...
...
@@ -1628,24 +1628,29 @@ class TestSundryAPI(unittest.TestCase):
self
.
assertEqual
(
x
.
grad
.
shape
,
[])
def
test_prelu
(
self
):
x
=
paddle
.
full
([],
1.0
,
'float32'
)
x
.
stop_gradient
=
False
w1
=
paddle
.
to_tensor
([
0.25
],
dtype
=
'float32'
)
out1
=
paddle
.
nn
.
functional
.
prelu
(
x
,
w1
)
x1
=
paddle
.
full
([],
1.0
,
'float32'
)
x1
.
stop_gradient
=
False
w1
=
paddle
.
full
([],
0.25
,
dtype
=
'float32'
)
out1
=
paddle
.
nn
.
functional
.
prelu
(
x1
,
w1
)
out1
.
retain_grads
()
out1
.
backward
()
self
.
assertEqual
(
out1
.
shape
,
[])
self
.
assertEqual
(
out1
.
numpy
(),
1.0
)
self
.
assertEqual
(
out1
.
grad
.
shape
,
[])
self
.
assertEqual
(
x
.
grad
.
shape
,
[])
self
.
assertEqual
(
x1
.
grad
.
shape
,
[])
self
.
assertEqual
(
x1
.
grad
.
numpy
(),
1.0
)
x2
=
paddle
.
full
([],
-
1.0
,
'float32'
)
x2
.
stop_gradient
=
False
w2
=
paddle
.
full
([],
0.25
,
dtype
=
'float32'
)
out2
=
paddle
.
nn
.
functional
.
prelu
(
x
,
w2
)
out2
=
paddle
.
nn
.
functional
.
prelu
(
x
2
,
w2
)
out2
.
retain_grads
()
out2
.
backward
()
self
.
assertEqual
(
out2
.
shape
,
[])
self
.
assertEqual
(
out2
.
numpy
(),
-
0.25
)
self
.
assertEqual
(
out2
.
grad
.
shape
,
[])
self
.
assertEqual
(
x
.
grad
.
shape
,
[])
self
.
assertEqual
(
x2
.
grad
.
shape
,
[])
self
.
assertEqual
(
x2
.
grad
.
numpy
(),
0.25
)
def
test_while_loop
(
self
):
def
cond
(
i
,
x
):
...
...
python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py
浏览文件 @
69aae171
...
...
@@ -84,6 +84,8 @@ unary_api_list = [
paddle
.
lgamma
,
paddle
.
poisson
,
paddle
.
bernoulli
,
paddle
.
nn
.
functional
.
softmax
,
paddle
.
nn
.
functional
.
log_softmax
,
]
inplace_api_list
=
[
...
...
@@ -1033,6 +1035,33 @@ class TestSundryAPI(unittest.TestCase):
out2
.
backward
()
self
.
assertEqual
(
out2
.
shape
,
[
1
])
def
test_prelu
(
self
):
x1
=
paddle
.
full
([],
1.0
,
'float32'
)
x1
.
stop_gradient
=
False
w1
=
paddle
.
full
([],
0.25
,
dtype
=
'float32'
)
w1
.
stop_gradient
=
False
out1
=
paddle
.
nn
.
functional
.
prelu
(
x1
,
w1
)
out1
.
retain_grads
()
out1
.
backward
()
self
.
assertEqual
(
out1
.
shape
,
[])
self
.
assertEqual
(
out1
.
numpy
(),
1.0
)
self
.
assertEqual
(
out1
.
grad
.
shape
,
[])
self
.
assertEqual
(
x1
.
grad
.
shape
,
[])
self
.
assertEqual
(
x1
.
grad
.
numpy
(),
1.0
)
x2
=
paddle
.
full
([],
-
1.0
,
'float32'
)
x2
.
stop_gradient
=
False
w2
=
paddle
.
full
([],
0.25
,
dtype
=
'float32'
)
w2
.
stop_gradient
=
False
out2
=
paddle
.
nn
.
functional
.
prelu
(
x2
,
w2
)
out2
.
retain_grads
()
out2
.
backward
()
self
.
assertEqual
(
out2
.
shape
,
[])
self
.
assertEqual
(
out2
.
numpy
(),
-
0.25
)
self
.
assertEqual
(
out2
.
grad
.
shape
,
[])
self
.
assertEqual
(
x2
.
grad
.
shape
,
[])
self
.
assertEqual
(
x2
.
grad
.
numpy
(),
0.25
)
# Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest.
class
TestNoBackwardAPI
(
unittest
.
TestCase
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录