Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
20a84820
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
20a84820
编写于
9月 11, 2020
作者:
A
Aurelius84
提交者:
GitHub
9月 11, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix unused var with zero gradient bug in fluid.gradient (#27246)
* fix calcu_gradients * fix code place * fix embedding interface usage
上级
33ff833a
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
53 addition
and
1 deletion
+53
-1
python/paddle/fluid/backward.py
python/paddle/fluid/backward.py
+6
-0
python/paddle/fluid/tests/unittests/dygraph_to_static/test_partial_program.py
...tests/unittests/dygraph_to_static/test_partial_program.py
+29
-0
python/paddle/fluid/tests/unittests/test_calc_gradient.py
python/paddle/fluid/tests/unittests/test_calc_gradient.py
+18
-1
未找到文件。
python/paddle/fluid/backward.py
浏览文件 @
20a84820
...
...
@@ -1756,6 +1756,12 @@ def calc_gradient(targets, inputs, target_gradients=None, no_grad_set=None):
op_path_dict
=
dict
()
op_path
=
_find_op_path_
(
block
,
targets
,
inputs
,
block_no_grad_set
,
op_path_dict
)
# find no grad var by op_path
no_grad_vars
=
_find_no_grad_vars
(
block
,
op_path
,
targets
,
block_no_grad_set
)
block_no_grad_set
.
update
(
no_grad_vars
)
no_grad_dict
[
0
].
update
(
list
(
map
(
_append_grad_suffix_
,
block_no_grad_set
)))
grad_to_var
=
dict
()
grad_info_map
=
dict
()
...
...
python/paddle/fluid/tests/unittests/dygraph_to_static/test_partial_program.py
浏览文件 @
20a84820
...
...
@@ -14,6 +14,7 @@
from
__future__
import
print_function
import
numpy
as
np
import
paddle
import
paddle.fluid
as
fluid
from
paddle.fluid.layers.utils
import
flatten
from
paddle.fluid.dygraph
import
declarative
,
ProgramTranslator
...
...
@@ -151,5 +152,33 @@ class TestWithTrainAndEval(unittest.TestCase):
partial_layer
.
_train_program
)
class
GPT2LMHeadModel
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
):
super
(
GPT2LMHeadModel
,
self
).
__init__
()
self
.
embedding0
=
paddle
.
nn
.
Embedding
(
20
,
16
)
self
.
embedding1
=
paddle
.
nn
.
Embedding
(
20
,
32
)
self
.
lm_head_weight
=
paddle
.
to_tensor
(
np
.
random
.
rand
(
2
,
3
).
astype
(
'float32'
))
@
declarative
def
forward
(
self
,
x
):
x
=
fluid
.
layers
.
reshape
(
x
,
shape
=
[
-
1
,
6
])
x1
,
x2
,
x3
=
fluid
.
layers
.
split
(
input
=
x
,
dim
=
1
,
num_or_sections
=
3
)
return
x1
class
TestPruneUnusedParamInProgram
(
unittest
.
TestCase
):
def
test_prune
(
self
):
input_ids
=
np
.
array
([[
15
,
11
,
6
,
3
,
18
,
13
]]).
astype
(
"float32"
)
place
=
fluid
.
CPUPlace
()
with
fluid
.
dygraph
.
guard
(
place
):
model
=
GPT2LMHeadModel
()
model
.
eval
()
input_ids
=
paddle
.
to_tensor
(
input_ids
)
out
=
model
(
input_ids
)
self
.
assertTrue
(
np
.
array_equal
(
out
.
numpy
(),
[[
15
,
11
]]))
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_calc_gradient.py
浏览文件 @
20a84820
...
...
@@ -15,7 +15,7 @@
from
__future__
import
print_function
import
unittest
import
numpy
as
np
import
paddle.fluid
as
fluid
import
paddle.fluid.layers
as
layers
from
paddle.fluid.backward
import
calc_gradient
...
...
@@ -81,5 +81,22 @@ class TestDoubleGrad(unittest.TestCase):
self
.
assertEqual
(
12
,
out
[
0
])
class
TestGradientWithPrune
(
unittest
.
TestCase
):
def
test_prune
(
self
):
x
=
fluid
.
data
(
name
=
'x'
,
shape
=
[
3
],
dtype
=
'float32'
)
x
.
stop_gradient
=
False
x1
,
x2
,
x3
=
fluid
.
layers
.
split
(
x
,
dim
=
0
,
num_or_sections
=
3
)
y
=
x1
*
2
x1_grad
=
fluid
.
gradients
(
y
,
x
)
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
main
=
fluid
.
default_main_program
()
exe
.
run
(
fluid
.
default_startup_program
())
out
=
exe
.
run
(
main
,
feed
=
{
'x'
:
np
.
ones
([
3
]).
astype
(
'float32'
)},
fetch_list
=
[
x1_grad
])
self
.
assertTrue
(
np
.
array_equal
(
out
[
0
],
[
2.
,
0.
,
0.
]))
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录