Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
35bc0e1f
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
403
Star
4705
Fork
582
代码
文件
提交
分支
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看板
提交
35bc0e1f
编写于
6月 01, 2020
作者:
M
Megvii Engine Team
提交者:
Xu Xinran
6月 19, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mge/function): do not deeply copy saved tensor in Function
GitOrigin-RevId: 3c89d1ceaa9f7338b167f80936549a4c364061de
上级
47377c7b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
65 addition
and
10 deletion
+65
-10
python_module/megengine/core/function.py
python_module/megengine/core/function.py
+15
-0
python_module/megengine/core/tensor.py
python_module/megengine/core/tensor.py
+6
-1
python_module/test/unit/core/test_function.py
python_module/test/unit/core/test_function.py
+44
-9
未找到文件。
python_module/megengine/core/function.py
浏览文件 @
35bc0e1f
...
...
@@ -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
copy
from
abc
import
ABCMeta
,
abstractmethod
from
typing
import
Iterable
,
Tuple
,
Union
...
...
@@ -142,6 +143,20 @@ class Function(metaclass=ABCMeta):
"""
self
.
saved_tensors
=
tensors
def
__deepcopy__
(
self
,
memo
):
"""
Defines how the operator is deeply copied
"""
cls
=
self
.
__class__
result
=
cls
.
__new__
(
cls
)
tmp
=
self
.
saved_tensors
self
.
saved_tensors
=
None
memo
[
id
(
self
)]
=
result
for
k
,
v
in
self
.
__dict__
.
items
():
setattr
(
result
,
k
,
copy
.
deepcopy
(
v
,
memo
))
self
.
saved_tensors
=
tmp
return
result
def
__call__
(
self
,
*
inputs
):
assert
(
not
self
.
_has_saved_state
...
...
python_module/megengine/core/tensor.py
浏览文件 @
35bc0e1f
...
...
@@ -495,7 +495,12 @@ class Tensor:
)
def
__getstate__
(
self
):
assert
(
self
.
__val
is
not
None
)
and
(
self
.
__sym
is
None
)
r
""" __getstate__ will be called for pickle serialization or deep copy
"""
assert
(
self
.
__val
is
not
None
)
and
(
self
.
__sym
is
None
),
"Only SharedND initialized Tensor can be serialized or deep copied"
metadata
=
{
"requires_grad"
:
self
.
requires_grad
}
state
=
{
"data"
:
self
.
numpy
(),
...
...
python_module/test/unit/core/test_function.py
浏览文件 @
35bc0e1f
# -*- coding: utf-8 -*-
# MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
#
# Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
...
...
@@ -6,10 +5,13 @@
# 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
copy
import
numpy
as
np
import
megengine.functional
as
F
from
megengine.core
import
Function
,
tensor
from
megengine.jit
import
trace
from
megengine.test
import
assertTensorClose
...
...
@@ -76,6 +78,27 @@ def test_ste():
)
def
test_deepcopy
():
class
Sigmoid
(
Function
):
def
__init__
(
self
,
param
):
super
().
__init__
()
self
.
param
=
param
def
forward
(
self
,
x
):
y
=
1
/
(
1
+
F
.
exp
(
-
x
))
self
.
save_for_backward
(
y
)
return
y
def
backward
(
self
,
grad_y
):
(
y
,)
=
self
.
saved_tensors
return
grad_y
*
y
*
(
1
-
y
)
origin
=
Sigmoid
(
0
)
new
=
copy
.
deepcopy
(
Sigmoid
(
0
))
assert
new
.
param
==
origin
.
param
assert
new
.
saved_tensors
==
None
def
test_save_context
():
class
Sigmoid
(
Function
):
def
forward
(
self
,
x
):
...
...
@@ -87,14 +110,26 @@ def test_save_context():
(
y
,)
=
self
.
saved_tensors
return
grad_y
*
y
*
(
1
-
y
)
a
=
tensor
(
np
.
array
([
1926.0817
],
dtype
=
np
.
float32
))
s
=
Sigmoid
()(
a
)
s2
=
F
.
sigmoid
(
a
)
assertTensorClose
(
s
.
numpy
(),
s2
.
numpy
())
assertTensorClose
(
F
.
grad
(
s
,
a
,
use_virtual_grad
=
False
).
numpy
(),
F
.
grad
(
s2
,
a
,
use_virtual_grad
=
False
).
numpy
(),
)
def
run_saved_context
(
a
,
net
=
None
):
return
net
(
a
)
def
run
(
use_trace
,
symbolic
):
a
=
tensor
(
np
.
array
([
1926.0817
],
dtype
=
np
.
float32
))
net
=
Sigmoid
()
func_run
=
run_saved_context
if
use_trace
:
func_run
=
trace
(
run_saved_context
,
symbolic
=
symbolic
)
s
=
func_run
(
a
,
net
=
net
)
s2
=
F
.
sigmoid
(
a
)
assertTensorClose
(
s
.
numpy
(),
s2
.
numpy
())
assertTensorClose
(
F
.
grad
(
s
,
a
,
use_virtual_grad
=
False
).
numpy
(),
F
.
grad
(
s2
,
a
,
use_virtual_grad
=
False
).
numpy
(),
)
run
(
False
,
False
)
run
(
True
,
False
)
run
(
True
,
True
)
def
test_none_in_out_grad
():
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录