Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
000517c6
MegEngine
项目概览
MegEngine 天元
/
MegEngine
大约 1 年 前同步成功
通知
396
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
000517c6
编写于
9月 14, 2021
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(grad): stop using exception in grad_override
GitOrigin-RevId: 00ae38d48b91e93f6de31d208de7ca469d79a938
上级
0bb058c1
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
17 addition
and
18 deletion
+17
-18
imperative/python/src/grad.cpp
imperative/python/src/grad.cpp
+3
-5
imperative/python/src/grad.h
imperative/python/src/grad.h
+2
-1
imperative/python/src/grad_override.cpp
imperative/python/src/grad_override.cpp
+12
-12
未找到文件。
imperative/python/src/grad.cpp
浏览文件 @
000517c6
...
...
@@ -393,13 +393,11 @@ apply_result_t apply_grad(ApplyContext& ctx) {
auto
&&
it
=
registry
.
find
(
ctx
.
op
->
dyn_typeinfo
());
if
(
it
!=
registry
.
end
())
{
auto
&&
maker
=
grad_fn_holder
.
emplace
<
CustomBackward
>
().
maker
(
ctx
);
try
{
auto
ret
=
it
->
second
(
ctx
,
maker
);
if
(
auto
ret
=
it
->
second
(
ctx
,
maker
))
{
maker
.
finalize
();
return
ret
;
}
catch
(
GradRuleFallback
&
)
{
grad_fn_holder
.
reset
();
return
*
ret
;
}
grad_fn_holder
.
reset
();
}
return
backward_graph_grad_rule
(
ctx
,
grad_fn_holder
);
}();
...
...
imperative/python/src/grad.h
浏览文件 @
000517c6
...
...
@@ -16,6 +16,7 @@
#include <megbrain/utils/small_vector.h>
#include <memory>
#include <optional>
namespace
mgb
::
imperative
::
python
{
...
...
@@ -154,7 +155,7 @@ public:
Maker
maker
(
ApplyContext
&
ctx
)
{
return
{
*
this
,
ctx
};}
};
using
GradRuleFn
=
std
::
function
<
apply_result_t
(
ApplyContext
&
,
CustomBackward
::
Maker
&
)
>
;
using
GradRuleFn
=
std
::
function
<
std
::
optional
<
apply_result_t
>
(
ApplyContext
&
,
CustomBackward
::
Maker
&
)
>
;
std
::
unordered_map
<
Typeinfo
*
,
GradRuleFn
>&
grad_rule_registry
();
...
...
imperative/python/src/grad_override.cpp
浏览文件 @
000517c6
...
...
@@ -37,14 +37,14 @@ std::shared_ptr<Tensor> broadcast_to(Tensor* x, Tensor* s) {
std
::
shared_ptr
<
Tensor
>
make_empty_tensor
(
CompNode
cn
,
Tensor
*
shape
,
DType
dtype
)
{
HostTensorND
scalar
{
cn
,
{{
1
},
dtype
}};
std:
memset
(
scalar
.
raw_ptr
(),
0
,
dtype
.
size
());
std
:
:
memset
(
scalar
.
raw_ptr
(),
0
,
dtype
.
size
());
interpreter
::
Interpreter
::
Handle
handle
=
interpreter_for_py
->
put
(
scalar
,
false
);
auto
&&
t
=
std
::
make_shared
<
Tensor
>
(
handle
);
auto
res
=
broadcast_to
(
t
.
get
(),
shape
);
return
res
;
}
apply_result_t
elemwise_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
std
::
optional
<
apply_result_t
>
elemwise_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
auto
&
op
=
ctx
.
op
->
cast_final_safe
<
Elemwise
>
();
if
(
op
.
mode
==
Elemwise
::
Mode
::
ADD
)
{
mgb_assert
(
ctx
.
nargs
==
2
);
...
...
@@ -71,10 +71,10 @@ apply_result_t elemwise_grad_rule(ApplyContext& ctx, CustomBackward::Maker& make
});
return
apply
(
ctx
);
}
throw
GradRuleFallback
()
;
return
{}
;
}
apply_result_t
reshape_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
std
::
optional
<
apply_result_t
>
reshape_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
mgb_assert
(
ctx
.
nargs
==
2
);
std
::
array
<
std
::
shared_ptr
<
Tensor
>
,
2
>
input_shapes
;
for
(
size_t
i
=
0
;
i
<
2
;
++
i
)
{
...
...
@@ -100,7 +100,7 @@ apply_result_t reshape_grad_rule(ApplyContext& ctx, CustomBackward::Maker& maker
return
apply
(
ctx
);
}
apply_result_t
subtensor_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
std
::
optional
<
apply_result_t
>
subtensor_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
auto
&&
op
=
ctx
.
op
->
cast_final_safe
<
Subtensor
>
();
auto
&&
grad_op
=
SetSubtensor
::
make
(
op
.
items
);
SmallVector
<
std
::
shared_ptr
<
Tensor
>>
inputs
;
...
...
@@ -130,7 +130,7 @@ apply_result_t subtensor_grad_rule(ApplyContext& ctx, CustomBackward::Maker& mak
return
apply
(
ctx
);
}
apply_result_t
indexingMultiAxisVec_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
std
::
optional
<
apply_result_t
>
indexingMultiAxisVec_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
auto
&&
op
=
ctx
.
op
->
cast_final_safe
<
IndexingMultiAxisVec
>
();
auto
&&
grad_op
=
IndexingSetMultiAxisVec
::
make
(
op
.
items
);
SmallVector
<
std
::
shared_ptr
<
Tensor
>>
inputs
;
...
...
@@ -160,11 +160,11 @@ apply_result_t indexingMultiAxisVec_grad_rule(ApplyContext& ctx, CustomBackward:
return
apply
(
ctx
);
}
apply_result_t
reduce_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
std
::
optional
<
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
)
{
if
(
ctx
.
nargs
!=
1
)
{
throw
GradRuleFallback
()
;
return
{}
;
}
std
::
array
<
std
::
shared_ptr
<
Tensor
>
,
1
>
input_shapes
;
if
(
input_requires_grad
(
ctx
,
0
))
{
...
...
@@ -182,10 +182,10 @@ apply_result_t reduce_grad_rule(ApplyContext& ctx, CustomBackward::Maker& maker)
});
return
apply
(
ctx
);
}
throw
GradRuleFallback
()
;
return
{}
;
}
apply_result_t
addAxis_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
std
::
optional
<
apply_result_t
>
addAxis_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
auto
&&
op
=
ctx
.
op
->
cast_final_safe
<
AddAxis
>
();
mgb_assert
(
ctx
.
nargs
==
1
);
bool
flag
=
input_requires_grad
(
ctx
,
0
);
...
...
@@ -204,7 +204,7 @@ apply_result_t addAxis_grad_rule(ApplyContext& ctx, CustomBackward::Maker& maker
return
apply
(
ctx
);
}
apply_result_t
removeAxis_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
std
::
optional
<
apply_result_t
>
removeAxis_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
auto
&&
op
=
ctx
.
op
->
cast_final_safe
<
RemoveAxis
>
();
mgb_assert
(
ctx
.
nargs
==
1
);
bool
flag
=
input_requires_grad
(
ctx
,
0
);
...
...
@@ -223,7 +223,7 @@ apply_result_t removeAxis_grad_rule(ApplyContext& ctx, CustomBackward::Maker& ma
return
apply
(
ctx
);
}
apply_result_t
fastpathcopy_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
std
::
optional
<
apply_result_t
>
fastpathcopy_grad_rule
(
ApplyContext
&
ctx
,
CustomBackward
::
Maker
&
maker
)
{
mgb_assert
(
ctx
.
nargs
==
1
);
maker
.
output_size
(
1
).
output_captured
(
0
,
false
);
maker
.
backward
([](
BackwardContext
&
,
Tensor
*
const
*
grads
,
size_t
ngrads
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录