Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
1d128326
P
Paddle
项目概览
机器未来
/
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看板
未验证
提交
1d128326
编写于
7月 18, 2022
作者:
X
Xiaoxu Chen
提交者:
GitHub
7月 18, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix duplicate slice logic in _grad (#44396)
上级
4c1e77d1
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
20 addition
and
6 deletion
+20
-6
python/paddle/fluid/tests/unittests/autograd/test_autograd_functional_dynamic.py
...ts/unittests/autograd/test_autograd_functional_dynamic.py
+5
-0
python/paddle/incubate/autograd/functional.py
python/paddle/incubate/autograd/functional.py
+6
-4
python/paddle/incubate/autograd/primapi.py
python/paddle/incubate/autograd/primapi.py
+9
-2
未找到文件。
python/paddle/fluid/tests/unittests/autograd/test_autograd_functional_dynamic.py
浏览文件 @
1d128326
...
...
@@ -223,6 +223,11 @@ class TestVJP(TestAutogradFunctional):
self
.
func_vjp_nested
()
self
.
func_vjp_aliased_input
()
def
test_input_single_tensor
(
self
):
self
.
assertIsInstance
(
paddle
.
incubate
.
autograd
.
vjp
(
paddle
.
tanh
,
paddle
.
rand
((
3
,
4
)))[
1
],
paddle
.
fluid
.
framework
.
Variable
)
@
utils
.
place
(
config
.
DEVICES
)
@
utils
.
parameterize
(
...
...
python/paddle/incubate/autograd/functional.py
浏览文件 @
1d128326
...
...
@@ -565,13 +565,15 @@ def _grad(ys, xs, v=None):
inputs.
"""
if
paddle
.
fluid
.
_non_static_mode
():
# paddle.grad returns a list though the inputs is a signle Tensor. The
# follow code snippet fixes the problem by return the first element of
# xs_grad when the xs is a signle Tensor.
xs_grad
=
paddle
.
grad
(
ys
,
xs
,
v
,
create_graph
=
True
,
allow_unused
=
True
)
if
isinstance
(
xs
,
paddle
.
fluid
.
framework
.
Variable
)
and
isinstance
(
xs_grad
,
typing
.
Sequence
)
and
len
(
xs_grad
)
>
0
:
xs_grad
=
xs_grad
[
0
]
else
:
xs_grad
=
paddle
.
incubate
.
autograd
.
grad
(
ys
,
xs
,
v
)
if
isinstance
(
xs
,
paddle
.
fluid
.
framework
.
Variable
):
xs_grad
=
xs_grad
[
0
]
return
_replace_none_with_zero_tensor
(
xs_grad
,
xs
)
...
...
python/paddle/incubate/autograd/primapi.py
浏览文件 @
1d128326
...
...
@@ -132,9 +132,16 @@ def grad(outputs, inputs, grad_outputs=None):
paddle.incubate.autograd.disable_prim()
paddle.disable_static()
"""
if
not
utils
.
prim_enabled
():
return
backward
.
gradients
(
outputs
,
inputs
,
grad_outputs
)
grad_inputs
=
backward
.
gradients
(
outputs
,
inputs
,
grad_outputs
)
# backward.gradients returns a list though the inputs is a signle Tensor.
# The follow code snippet fixes the problem by return the first element
# of grad_inputs when the inputs is a signle Tensor.
if
isinstance
(
inputs
,
framework
.
Variable
)
and
isinstance
(
grad_inputs
,
typing
.
Sequence
)
and
len
(
grad_inputs
)
>
0
:
return
grad_inputs
[
0
]
else
:
return
grad_inputs
if
not
isinstance
(
outputs
,
(
framework
.
Variable
,
typing
.
Sequence
)):
raise
TypeError
(
f
'Expected outputs is Tensor|Sequence[Tesnor], '
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录