Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
9005cf74
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
410
Star
4707
Fork
583
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
MegEngine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
9005cf74
编写于
9月 15, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mge/imperative): fix tracer leak
GitOrigin-RevId: 1a8ac20b46e25897a34c62da088dbf49e0e19ee6
上级
70bc746c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
45 addition
and
1 deletion
+45
-1
imperative/python/megengine/core/autodiff/grad.py
imperative/python/megengine/core/autodiff/grad.py
+13
-1
imperative/python/test/unit/core/test_autodiff.py
imperative/python/test/unit/core/test_autodiff.py
+32
-0
未找到文件。
imperative/python/megengine/core/autodiff/grad.py
浏览文件 @
9005cf74
...
...
@@ -82,6 +82,7 @@ class Grad:
# ops forms the computational graph
self
.
ops
=
[]
self
.
_attached_tensors
=
weakref
.
WeakSet
()
self
.
_enabled
=
True
@
property
...
...
@@ -107,6 +108,7 @@ class Grad:
return
self
def
_new_variable
(
self
,
owner
,
opnode
=
None
,
callback
=
None
):
self
.
_attached_tensors
.
add
(
owner
)
return
VariableNode
(
self
,
owner
,
opnode
=
opnode
,
callback
=
callback
)
def
_new_opnode
(
self
,
inputs
,
outputs
):
...
...
@@ -131,13 +133,18 @@ class Grad:
def
__enter__
(
self
):
return
self
def
_
_exit__
(
self
,
*
_
):
def
_
exit
(
self
):
"""clear all resources"""
self
.
_enabled
=
False
for
o
in
self
.
ops
:
o
=
o
()
if
o
:
o
.
clear
()
for
i
in
self
.
_attached_tensors
:
i
.
_extra_data
.
pop
(
self
,
None
)
def
__exit__
(
self
,
*
_
):
self
.
_exit
()
def
__call__
(
self
,
ys
,
dys
):
""" Defines Grad().
...
...
@@ -275,6 +282,11 @@ class Grad:
for
v
in
cache
.
values
():
assert
v
is
None
self
.
_exit
()
def
__del__
(
self
):
self
.
_exit
()
class
clearable
:
__cleared
=
False
...
...
imperative/python/test/unit/core/test_autodiff.py
浏览文件 @
9005cf74
...
...
@@ -6,6 +6,7 @@
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import
gc
import
platform
import
weakref
...
...
@@ -158,6 +159,37 @@ def test_grad_with_tensor_wrapper():
np
.
testing
.
assert_almost_equal
(
x
.
grad
.
numpy
(),
4
*
x_np
**
3
,
decimal
=
6
)
def
test_release
():
def
check
(
f
):
n
=
0
d
=
None
for
i
in
range
(
3
):
f
()
m
=
len
(
gc
.
get_objects
())
d
=
m
-
n
n
=
m
assert
d
==
0
x
=
TensorWrapper
([
0.0
])
dy
=
TensorWrapper
(
np
.
ones_like
(
x
.
numpy
()))
@
check
def
_
():
g
=
Grad
().
wrt
(
x
)
y
=
x
*
x
g
(
y
,
dy
)
@
check
def
_
():
with
Grad
().
wrt
(
x
)
as
g
:
pass
@
check
def
_
():
with
Grad
().
wrt
(
x
)
as
g
:
y
=
x
*
x
def
test_grad_inplace
():
x_np
=
np
.
random
.
rand
(
10
).
astype
(
"float32"
)
x
=
TensorWrapper
(
x_np
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录