Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
604b7a53
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看板
未验证
提交
604b7a53
编写于
3月 20, 2023
作者:
J
Jiabin Yang
提交者:
GitHub
3月 20, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support relue custom vjp (#51742)
上级
702fc894
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
137 addition
and
0 deletion
+137
-0
paddle/fluid/prim/api/api.yaml
paddle/fluid/prim/api/api.yaml
+1
-0
paddle/fluid/prim/api/composite_backward/composite_backward_api.h
...luid/prim/api/composite_backward/composite_backward_api.h
+12
-0
paddle/phi/api/yaml/backward.yaml
paddle/phi/api/yaml/backward.yaml
+1
-0
python/paddle/fluid/tests/unittests/prim/composite_ops/test_composite_relu_custom_vjp.py
...ests/prim/composite_ops/test_composite_relu_custom_vjp.py
+122
-0
python/paddle/incubate/autograd/composite_rules.py
python/paddle/incubate/autograd/composite_rules.py
+1
-0
未找到文件。
paddle/fluid/prim/api/api.yaml
浏览文件 @
604b7a53
...
@@ -39,3 +39,4 @@
...
@@ -39,3 +39,4 @@
-
put_along_axis
-
put_along_axis
-
greater_than
-
greater_than
-
less_equal
-
less_equal
-
where
paddle/fluid/prim/api/composite_backward/composite_backward_api.h
浏览文件 @
604b7a53
...
@@ -30,6 +30,18 @@ using Tensor = paddle::Tensor;
...
@@ -30,6 +30,18 @@ using Tensor = paddle::Tensor;
using
IntArray
=
paddle
::
experimental
::
IntArrayBase
<
paddle
::
Tensor
>
;
using
IntArray
=
paddle
::
experimental
::
IntArrayBase
<
paddle
::
Tensor
>
;
// This function should have as same signature as phi, which defined in
// This function should have as same signature as phi, which defined in
// paddle/phi/api/backward/backward_api.h
// paddle/phi/api/backward/backward_api.h
template
<
typename
T
>
void
relu_grad
(
const
Tensor
&
out
,
const
Tensor
&
out_grad
,
Tensor
*
x_grad
)
{
if
(
x_grad
)
{
auto
condition
=
greater_than
<
T
>
(
out
,
full
<
T
>
(
phi
::
vectorize
(
out
.
dims
()),
0.0
,
out
.
dtype
()));
auto
res
=
where
<
T
>
(
condition
,
out_grad
,
full
<
T
>
(
phi
::
vectorize
(
out
.
dims
()),
0.0
,
out
.
dtype
()));
set_output
<
T
>
(
res
,
x_grad
);
}
}
template
<
typename
T
>
template
<
typename
T
>
void
softmax_grad
(
const
Tensor
&
out
,
void
softmax_grad
(
const
Tensor
&
out
,
const
Tensor
&
out_grad
,
const
Tensor
&
out_grad
,
...
...
paddle/phi/api/yaml/backward.yaml
浏览文件 @
604b7a53
...
@@ -1142,6 +1142,7 @@
...
@@ -1142,6 +1142,7 @@
kernel
:
kernel
:
func
:
relu_grad
func
:
relu_grad
backward
:
relu_double_grad
backward
:
relu_double_grad
composite
:
relu_grad(out, out_grad, x_grad)
inplace
:
(out_grad -> x_grad)
inplace
:
(out_grad -> x_grad)
-
backward_op
:
renorm_grad
-
backward_op
:
renorm_grad
...
...
python/paddle/fluid/tests/unittests/prim/composite_ops/test_composite_relu_custom_vjp.py
0 → 100644
浏览文件 @
604b7a53
# 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.
import
unittest
import
numpy
as
np
from
utils
import
TOLERANCE
import
paddle
import
paddle.nn.functional
as
F
from
paddle.fluid
import
core
def
generate_data
(
shape
,
dtype
=
"float32"
):
np_data
=
np
.
random
.
random
(
shape
).
astype
(
dtype
)
return
np_data
class
Attr
:
def
__init__
(
self
)
->
None
:
self
.
dtype
=
None
self
.
shape
=
None
def
set_dtype
(
self
,
dtype
)
->
None
:
self
.
dtype
=
dtype
return
def
set_shape
(
self
,
shape
)
->
None
:
self
.
shape
=
shape
return
def
get_rtol
(
self
,
flag
):
rtol
=
TOLERANCE
[
self
.
dtype
][
flag
].
get
(
"rtol"
)
return
rtol
def
get_atol
(
self
,
flag
):
atol
=
TOLERANCE
[
self
.
dtype
][
flag
].
get
(
"atol"
)
return
atol
attrs
=
Attr
()
def
fn
(
x
):
return
F
.
relu
(
x
)
def
expect_grad
(
inputs
):
paddle
.
disable_static
()
inputs
.
stop_gradient
=
False
res
=
fn
(
inputs
)
gradients
=
paddle
.
grad
(
res
,
inputs
)
return
gradients
class
TestCompositeSoftmaxPrimBackward
(
unittest
.
TestCase
):
"test composite softmax and prim backward"
def
setUp
(
self
):
core
.
_set_prim_backward_enabled
(
True
)
self
.
dtypes
=
[
"float16"
,
"float32"
,
"float64"
]
self
.
shapes
=
[[
2
,
3
,
4
],
[
2
,
3
]]
def
cal_composite_grad
(
self
,
inputs
):
paddle
.
enable_static
()
core
.
_set_prim_all_enabled
(
True
)
startup_program
=
paddle
.
static
.
Program
()
main_program
=
paddle
.
static
.
Program
()
with
paddle
.
static
.
program_guard
(
main_program
,
startup_program
):
x
=
paddle
.
static
.
data
(
'x'
,
shape
=
inputs
.
shape
,
dtype
=
str
(
inputs
.
dtype
)
)
x
.
stop_gradient
=
False
y
=
fn
(
x
)
blocks
=
main_program
.
blocks
z
=
paddle
.
static
.
gradients
([
y
],
x
)
paddle
.
incubate
.
autograd
.
primapi
.
to_prim
(
blocks
)
exe
=
paddle
.
static
.
Executor
()
exe
.
run
(
startup_program
)
res
=
exe
.
run
(
main_program
,
feed
=
{
'x'
:
inputs
},
fetch_list
=
[
z
])
paddle
.
disable_static
()
core
.
_set_prim_all_enabled
(
False
)
return
res
def
compare_backward
(
self
):
np_data
=
generate_data
(
attrs
.
shape
)
tensor_data
=
paddle
.
to_tensor
(
np_data
)
expect
=
expect_grad
(
tensor_data
)[
0
].
numpy
()
actual
=
self
.
cal_composite_grad
(
np_data
)[
0
]
assert
expect
.
dtype
==
actual
.
dtype
np
.
testing
.
assert_allclose
(
expect
,
actual
,
rtol
=
attrs
.
get_rtol
(
"prim_backward"
),
atol
=
attrs
.
get_rtol
(
"prim_backward"
),
)
def
test_prim_backward
(
self
):
for
j
in
self
.
dtypes
:
for
t
in
self
.
shapes
:
attrs
.
set_dtype
(
j
)
attrs
.
set_shape
(
t
)
self
.
compare_backward
()
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/incubate/autograd/composite_rules.py
浏览文件 @
604b7a53
...
@@ -97,6 +97,7 @@ def composite_batchnorm(
...
@@ -97,6 +97,7 @@ def composite_batchnorm(
batch_mean
=
zeros
(
run_mean
.
shape
,
run_mean
.
dtype
)
batch_mean
=
zeros
(
run_mean
.
shape
,
run_mean
.
dtype
)
batch_var
=
zeros
(
run_var
.
shape
,
run_var
.
dtype
)
batch_var
=
zeros
(
run_var
.
shape
,
run_var
.
dtype
)
if
not
use_run_stat
:
if
not
use_run_stat
:
batch_mean
=
mean
(
x
,
reduce_axes
,
keepdim
=
True
)
batch_mean
=
mean
(
x
,
reduce_axes
,
keepdim
=
True
)
temp
=
mean
(
x
*
x
,
reduce_axes
,
keepdim
=
True
)
temp
=
mean
(
x
*
x
,
reduce_axes
,
keepdim
=
True
)
batch_var
=
temp
-
batch_mean
*
batch_mean
batch_var
=
temp
-
batch_mean
*
batch_mean
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录