Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
72531f2b
MegEngine
项目概览
MegEngine 天元
/
MegEngine
大约 1 年 前同步成功
通知
399
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看板
提交
72531f2b
编写于
6月 18, 2021
作者:
M
Megvii Engine Team
提交者:
huangxinda
7月 19, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
test(autograd): add more tests for higher order grad
GitOrigin-RevId: 5fc308f87a6c4cb2de9b8edb654fe7a2416333ec
上级
522e556b
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
73 addition
and
8 deletion
+73
-8
imperative/python/megengine/autodiff/grad_manager.py
imperative/python/megengine/autodiff/grad_manager.py
+2
-4
imperative/python/src/grad.cpp
imperative/python/src/grad.cpp
+3
-3
imperative/python/src/tensor.cpp
imperative/python/src/tensor.cpp
+1
-0
imperative/python/test/unit/autodiff/test_grad_manger.py
imperative/python/test/unit/autodiff/test_grad_manger.py
+66
-0
imperative/python/test/unit/core/test_autodiff.py
imperative/python/test/unit/core/test_autodiff.py
+1
-1
未找到文件。
imperative/python/megengine/autodiff/grad_manager.py
浏览文件 @
72531f2b
...
...
@@ -345,12 +345,12 @@ class GradManager:
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
self
.
release
()
def
__
and
__
(
self
,
other
):
def
__
or
__
(
self
,
other
):
if
isinstance
(
other
,
GradManager
):
return
GradManagerGroup
([
self
,
other
])
return
NotImplemented
__r
and__
=
__and
__
__r
or__
=
__or
__
class
GradManagerGroup
:
...
...
@@ -364,8 +364,6 @@ class GradManagerGroup:
return
NotImplemented
return
GradManagerGroup
([
*
self
.
_gms
,
*
other
.
_gms
])
__and__
=
merge_with
__rand__
=
merge_with
__or__
=
merge_with
__ror__
=
merge_with
...
...
imperative/python/src/grad.cpp
浏览文件 @
72531f2b
...
...
@@ -468,7 +468,7 @@ PyObject* GradKeyWrapper::get_priority() {
}
void
GradKeyWrapper
::
set_priority
(
pybind11
::
handle
priority
)
{
m_key
->
name
=
py
::
cast
<
int
>
(
priority
);
m_key
->
priority
=
py
::
cast
<
int
>
(
priority
);
}
void
GradKeyWrapper
::
attach
(
PyObject
*
const
*
args
,
size_t
nargs
)
{
...
...
@@ -535,7 +535,7 @@ void GradKey::backward(std::vector<TensorWrapper*> tensors, std::vector<TensorWr
size_t
priority_backup
;
CleanupGuard
(
GradKey
*
this_
)
:
owner
(
this_
)
{
priority_backup
=
sm_min_priority
;
sm_min_priority
=
owner
->
priority
;
sm_min_priority
=
owner
->
priority
+
1
;
}
~
CleanupGuard
()
{
owner
->
cleanup
();
...
...
@@ -636,7 +636,7 @@ PyObject* GradKeyWrapper::is_attached_to(PyObject*const* args, size_t nargs) {
Py_RETURN_FALSE
;
}
int
GradKey
::
sm_min_priority
=
0
;
int
GradKey
::
sm_min_priority
=
std
::
numeric_limits
<
int
>::
min
()
;
GradKey
::~
GradKey
()
{
cleanup
();
...
...
imperative/python/src/tensor.cpp
浏览文件 @
72531f2b
...
...
@@ -966,6 +966,7 @@ void init_tensor(py::module m) {
.
def
<&
GradKeyWrapper
::
attach
>
(
"attach"
)
.
def
<&
GradKeyWrapper
::
is_attached_to
>
(
"is_attached_to"
)
.
def_getset
<&
GradKeyWrapper
::
get_name
,
&
GradKeyWrapper
::
set_name
>
(
"name"
)
.
def_getset
<&
GradKeyWrapper
::
get_priority
,
&
GradKeyWrapper
::
set_priority
>
(
"priority"
)
.
finalize
();
if
(
!
grad_key_type
)
throw
py
::
error_already_set
();
py
::
setattr
(
m
,
"GradKey"
,
grad_key_type
);
...
...
imperative/python/test/unit/autodiff/test_grad_manger.py
浏览文件 @
72531f2b
...
...
@@ -279,3 +279,69 @@ def test_broadcast_grad(trace_mode):
func
()
worker
()
def
test_2nd_grad_with_manager
():
x_np
=
np
.
random
.
rand
(
10
).
astype
(
"float32"
)
x
=
mge
.
tensor
(
x_np
)
gm
=
GradManager
().
attach
([
x
])
gm2
=
GradManager
().
attach
([
x
])
with
gm
:
with
gm2
:
y
=
F
.
cos
(
x
)
gm2
.
backward
(
y
)
np
.
testing
.
assert_almost_equal
(
x
.
grad
.
numpy
(),
-
np
.
sin
(
x_np
),
decimal
=
5
)
gm
.
backward
(
x
.
grad
)
np
.
testing
.
assert_almost_equal
(
x
.
grad
.
numpy
(),
-
np
.
sin
(
x_np
)
-
np
.
cos
(
x_np
),
decimal
=
5
)
def
test_grad_manager_group
():
x_np
=
np
.
random
.
rand
(
10
).
astype
(
"float32"
)
x
=
mge
.
tensor
(
x_np
)
gm
=
GradManager
().
attach
([
x
])
gm2
=
GradManager
().
attach
([
x
])
with
gm
|
gm2
:
y
=
F
.
cos
(
x
)
gm
.
backward
(
y
)
gm2
.
backward
(
y
)
np
.
testing
.
assert_almost_equal
(
x
.
grad
.
numpy
(),
-
2
*
np
.
sin
(
x_np
),
decimal
=
5
)
x
.
grad
=
None
def
test_grad_manager_group_visibility
():
x_np
=
np
.
random
.
rand
(
10
).
astype
(
"float32"
)
x
=
mge
.
tensor
(
x_np
)
gm
=
GradManager
().
attach
([
x
])
gm2
=
GradManager
().
attach
([
x
])
with
gm
|
gm2
:
y
=
F
.
cos
(
x
)
gm2
.
backward
(
y
)
np
.
testing
.
assert_almost_equal
(
x
.
grad
.
numpy
(),
-
np
.
sin
(
x_np
),
decimal
=
5
)
gm
.
backward
(
x
.
grad
)
np
.
testing
.
assert_almost_equal
(
x
.
grad
.
numpy
(),
-
np
.
sin
(
x_np
),
decimal
=
5
)
def
test_grad_manager_visibility_by_order
():
x_np
=
np
.
random
.
rand
(
10
).
astype
(
"float32"
)
x
=
mge
.
tensor
(
x_np
)
gm
=
GradManager
().
attach
([
x
])
gm2
=
GradManager
().
attach
([
x
])
with
gm2
:
with
gm
:
y
=
F
.
cos
(
x
)
gm2
.
backward
(
y
)
np
.
testing
.
assert_almost_equal
(
x
.
grad
.
numpy
(),
-
np
.
sin
(
x_np
),
decimal
=
5
)
gm
.
backward
(
x
.
grad
)
np
.
testing
.
assert_almost_equal
(
x
.
grad
.
numpy
(),
-
np
.
sin
(
x_np
),
decimal
=
5
)
imperative/python/test/unit/core/test_autodiff.py
浏览文件 @
72531f2b
...
...
@@ -126,7 +126,7 @@ def test_2nd_grad():
x
.
grad
=
None
grad2
(
z
,
ones
)
np
.
testing
.
assert_almost_equal
(
x
.
grad
.
numpy
(),
-
np
.
cos
(
x_np
))
np
.
testing
.
assert_almost_equal
(
x
.
grad
.
numpy
(),
-
np
.
cos
(
x_np
)
,
decimal
=
5
)
def
test_grad_with_tensor_wrapper
():
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录