Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
02455941
MegEngine
项目概览
MegEngine 天元
/
MegEngine
接近 2 年 前同步成功
通知
414
Star
4708
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看板
提交
02455941
编写于
7月 07, 2021
作者:
M
Megvii Engine Team
提交者:
huangxinda
7月 19, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
test(autograd): test jvp emulated by 2nd grad
GitOrigin-RevId: 47114fcd99f53b335c1615896370dd6e45dd3693
上级
8480302d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
29 addition
and
1 deletion
+29
-1
imperative/python/src/grad_override.cpp
imperative/python/src/grad_override.cpp
+3
-1
imperative/python/test/unit/autodiff/test_grad_manger.py
imperative/python/test/unit/autodiff/test_grad_manger.py
+26
-0
未找到文件。
imperative/python/src/grad_override.cpp
浏览文件 @
02455941
...
...
@@ -163,7 +163,9 @@ apply_result_t indexingMultiAxisVec_grad_rule(ApplyContext& ctx, CustomBackward:
apply_result_t
reduce_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
auto
&
op
=
ctx
.
op
->
cast_final_safe
<
Reduce
>
();
if
(
op
.
mode
==
Reduce
::
Mode
::
SUM
)
{
mgb_assert
(
ctx
.
nargs
==
1
);
if
(
ctx
.
nargs
!=
1
)
{
throw
GradRuleFallback
();
}
std
::
array
<
std
::
shared_ptr
<
Tensor
>
,
1
>
input_shapes
;
if
(
input_requires_grad
(
ctx
,
0
))
{
input_shapes
[
0
]
=
get_shape
(
ctx
.
args
[
0
]);
...
...
imperative/python/test/unit/autodiff/test_grad_manger.py
浏览文件 @
02455941
...
...
@@ -349,3 +349,29 @@ def test_grad_manager_visibility_by_order():
gm
.
backward
(
x
.
grad
)
np
.
testing
.
assert_almost_equal
(
x
.
grad
.
numpy
(),
-
np
.
sin
(
x_np
),
decimal
=
5
)
@
pytest
.
mark
.
require_higher_order_directive
()
@
pytest
.
mark
.
parametrize
(
"target"
,
[
F
.
cos
,
F
.
sin
,
lambda
x
:
x
*
2
+
1
])
def
test_emulate_forward_mode_with_reverse_mode
(
target
):
def
jvp
(
inp
,
expr
):
with
GradManager
()
as
gm
:
with
GradManager
().
attach
([
inp
])
as
gm2
:
oup
=
expr
(
inp
)
oup_grad
=
F
.
zeros_like
(
oup
)
gm
.
attach
(
oup_grad
)
gm2
.
backward
(
oup
,
oup_grad
)
gm
.
backward
(
inp
.
grad
)
return
oup
,
oup_grad
.
grad
def
fake_jvp
(
inp
,
expr
):
delta
=
0.001
return
expr
(
inp
),
(
expr
(
inp
+
delta
)
-
expr
(
inp
-
delta
))
/
(
2
*
delta
)
x_np
=
np
.
random
.
rand
(
10
).
astype
(
"float32"
)
x
=
mge
.
tensor
(
x_np
)
y
,
dy
=
jvp
(
x
,
target
)
y1
,
dy1
=
fake_jvp
(
x
,
target
)
np
.
testing
.
assert_almost_equal
(
y
.
numpy
(),
y1
.
numpy
(),
decimal
=
5
)
np
.
testing
.
assert_almost_equal
(
dy
.
numpy
(),
dy1
.
numpy
(),
decimal
=
3
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录