Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
正统之独孤求败
mindspore
提交
e8ae576d
M
mindspore
项目概览
正统之独孤求败
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e8ae576d
编写于
4月 03, 2020
作者:
P
panyifeng
提交者:
高东海
4月 08, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix grad missing due to indirect dependent free morphism
上级
a86b31ee
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
54 addition
and
17 deletion
+54
-17
mindspore/ccsrc/optimizer/ad/dfunctor.cc
mindspore/ccsrc/optimizer/ad/dfunctor.cc
+29
-14
mindspore/ccsrc/optimizer/ad/dfunctor.h
mindspore/ccsrc/optimizer/ad/dfunctor.h
+1
-0
mindspore/ccsrc/pipeline/pass.cc
mindspore/ccsrc/pipeline/pass.cc
+1
-1
tests/ut/python/pynative_mode/test_cell_bprop.py
tests/ut/python/pynative_mode/test_cell_bprop.py
+1
-2
tests/ut/python/pynative_mode/test_framstruct.py
tests/ut/python/pynative_mode/test_framstruct.py
+22
-0
未找到文件。
mindspore/ccsrc/optimizer/ad/dfunctor.cc
浏览文件 @
e8ae576d
...
@@ -185,19 +185,32 @@ AdjointPtr DFunctor::MapMorphism(const AnfNodePtr &morph) {
...
@@ -185,19 +185,32 @@ AdjointPtr DFunctor::MapMorphism(const AnfNodePtr &morph) {
return
node_adjoint
;
return
node_adjoint
;
}
}
bool
DFunctor
::
IsFreeMorphism
(
const
AnfNodePtr
&
node
)
{
// Do not care about non-CNode
if
(
!
node
->
isa
<
CNode
>
())
{
return
false
;
}
// Do not care about kPrimReturn
if
(
IsPrimitiveCNode
(
node
,
prim
::
kPrimReturn
))
{
return
false
;
}
auto
&
users
=
primal_graph_
->
manager
()
->
node_users
()[
node
];
// Do not care about isolated morphisms
if
(
users
.
empty
())
{
return
false
;
}
// Not free if it's used by some node in primal_graph
bool
nonfree
=
std
::
any_of
(
std
::
begin
(
users
),
std
::
end
(
users
),
[
&
](
const
auto
&
kv
)
{
auto
&
user
=
kv
.
first
;
return
user
->
func_graph
()
==
primal_graph_
;
});
return
!
nonfree
;
}
void
DFunctor
::
MapFreeMorphism
()
{
void
DFunctor
::
MapFreeMorphism
()
{
// Handle cnode not attached to output, that might be refered in other functions.
// Handle cnode not attached to output, that might be refered in other functions.
for
(
auto
&
node
:
primal_graph_
->
nodes
())
{
for
(
auto
&
node
:
primal_graph_
->
nodes
())
{
auto
adjoint
=
FindAdjoint
(
node
);
if
(
!
IsFreeMorphism
(
node
))
{
if
(
adjoint
!=
nullptr
)
{
continue
;
}
if
(
!
node
->
isa
<
CNode
>
())
{
MS_LOG
(
DEBUG
)
<<
"MapFreeMorphism noncnode not mapped after MapMorphism "
<<
node
->
ToString
()
<<
" "
<<
node
->
type_name
()
<<
"."
;
continue
;
}
if
(
IsPrimitiveCNode
(
node
,
prim
::
kPrimReturn
))
{
continue
;
continue
;
}
}
MS_LOG
(
DEBUG
)
<<
"MapFreeMorphism map nonoutput cnode after MapMorphism "
<<
node
->
ToString
()
<<
"."
;
MS_LOG
(
DEBUG
)
<<
"MapFreeMorphism map nonoutput cnode after MapMorphism "
<<
node
->
ToString
()
<<
"."
;
...
@@ -256,9 +269,10 @@ void DFunctor::MapMorphism() {
...
@@ -256,9 +269,10 @@ void DFunctor::MapMorphism() {
// Set stop_gradient before MapMorphism.
// Set stop_gradient before MapMorphism.
BroadCastStopFlag
();
BroadCastStopFlag
();
// Handle free morphism before output, because in some case, free morphism might depend on output's fv tangent
MapFreeMorphism
();
// Handle morphism from output.
// Handle morphism from output.
(
void
)
MapMorphism
(
primal_graph_
->
output
());
(
void
)
MapMorphism
(
primal_graph_
->
output
());
MapFreeMorphism
();
// Construct K for primal_graph_
// Construct K for primal_graph_
auto
output_adjoint
=
anfnode_to_adjoin_
.
find
(
primal_graph_
->
output
());
auto
output_adjoint
=
anfnode_to_adjoin_
.
find
(
primal_graph_
->
output
());
...
@@ -298,9 +312,10 @@ FuncGraphPtr DFunctor::KUserDefined(const FuncGraphPtr &primal) {
...
@@ -298,9 +312,10 @@ FuncGraphPtr DFunctor::KUserDefined(const FuncGraphPtr &primal) {
const
size_t
param_diff
=
1
;
const
size_t
param_diff
=
1
;
if
(
bprop_graph
->
output
()
->
isa
<
CNode
>
()
&&
if
(
bprop_graph
->
output
()
->
isa
<
CNode
>
()
&&
bprop_graph
->
output
()
->
cast
<
CNodePtr
>
()
->
size
()
+
param_diff
!=
bprop_graph
->
parameters
().
size
())
{
bprop_graph
->
output
()
->
cast
<
CNodePtr
>
()
->
size
()
+
param_diff
!=
bprop_graph
->
parameters
().
size
())
{
MS_LOG
(
EXCEPTION
)
<<
"User defined Cell bprop "
<<
primal
->
ToString
()
<<
" in scope "
// It does not matter with the final tangents, just a tip for debugging
<<
primal
->
output
()
->
scope
()
->
name
()
MS_LOG
(
DEBUG
)
<<
"User defined Cell bprop "
<<
primal
->
ToString
()
<<
" in scope "
<<
" output must be a tuple and output number should be the same with inputs."
;
<<
primal
->
output
()
->
scope
()
->
name
()
<<
" output must be a tuple and output number should be the same with inputs."
;
}
}
resources_
->
manager
()
->
AddFuncGraph
(
bprop_graph
);
resources_
->
manager
()
->
AddFuncGraph
(
bprop_graph
);
...
...
mindspore/ccsrc/optimizer/ad/dfunctor.h
浏览文件 @
e8ae576d
...
@@ -61,6 +61,7 @@ class DFunctor {
...
@@ -61,6 +61,7 @@ class DFunctor {
private:
private:
// Map one morphism.
// Map one morphism.
AdjointPtr
MapMorphism
(
const
AnfNodePtr
&
morph
);
AdjointPtr
MapMorphism
(
const
AnfNodePtr
&
morph
);
bool
IsFreeMorphism
(
const
AnfNodePtr
&
node
);
// Map morphism that's not attached to output.
// Map morphism that's not attached to output.
void
MapFreeMorphism
();
void
MapFreeMorphism
();
void
BackPropagateFv
(
const
AnfNodePtr
&
fv
,
const
AnfNodePtr
&
din
);
void
BackPropagateFv
(
const
AnfNodePtr
&
fv
,
const
AnfNodePtr
&
din
);
...
...
mindspore/ccsrc/pipeline/pass.cc
浏览文件 @
e8ae576d
...
@@ -111,7 +111,7 @@ OptPassGroupMap GetOptPassesA(const opt::irpass::OptimizeIRPassLib& irpass) {
...
@@ -111,7 +111,7 @@ OptPassGroupMap GetOptPassesA(const opt::irpass::OptimizeIRPassLib& irpass) {
irpass
.
replace_applicator_
,
irpass
.
replace_applicator_
,
});
});
opt
::
OptPassConfig
virtual_dataset
=
opt
::
OptPassConfig
({
irpass
.
virtual_dataset_eliminate_
});
opt
::
OptPassConfig
virtual_dataset
=
opt
::
OptPassConfig
({
irpass
.
virtual_dataset_eliminate_
});
opt
::
OptPassConfig
grad
=
opt
::
OptPassConfig
({
irpass
.
inline_
,
irpass
.
expand_jprim_
},
true
);
opt
::
OptPassConfig
grad
=
opt
::
OptPassConfig
({
irpass
.
expand_jprim_
},
true
);
OptPassGroupMap
map_a
({{
"a_1"
,
a_1
},
OptPassGroupMap
map_a
({{
"a_1"
,
a_1
},
{
"a_2"
,
a_2
},
{
"a_2"
,
a_2
},
...
...
tests/ut/python/pynative_mode/test_cell_bprop.py
浏览文件 @
e8ae576d
...
@@ -304,5 +304,4 @@ class MulAddWithWrongOutputNum(nn.Cell):
...
@@ -304,5 +304,4 @@ class MulAddWithWrongOutputNum(nn.Cell):
def
test_grad_mul_add_with_wrong_output_num
():
def
test_grad_mul_add_with_wrong_output_num
():
mul_add
=
MulAddWithWrongOutputNum
()
mul_add
=
MulAddWithWrongOutputNum
()
with
pytest
.
raises
(
RuntimeError
):
C
.
grad_all
(
mul_add
)(
1
,
2
)
C
.
grad_all
(
mul_add
)(
1
,
2
)
tests/ut/python/pynative_mode/test_framstruct.py
浏览文件 @
e8ae576d
...
@@ -15,6 +15,7 @@
...
@@ -15,6 +15,7 @@
""" test_framstruct """
""" test_framstruct """
import
pytest
import
pytest
import
numpy
as
np
import
numpy
as
np
import
mindspore
as
ms
import
mindspore.nn
as
nn
import
mindspore.nn
as
nn
from
mindspore
import
context
from
mindspore
import
context
from
mindspore.ops
import
composite
as
C
from
mindspore.ops
import
composite
as
C
...
@@ -706,3 +707,24 @@ def grad_refactor_14(a, b):
...
@@ -706,3 +707,24 @@ def grad_refactor_14(a, b):
return
inner1
(
b
)
+
inner2
(
a
)
+
inner3
(
a
)
return
inner1
(
b
)
+
inner2
(
a
)
+
inner3
(
a
)
def
test_grad_refactor_14
():
def
test_grad_refactor_14
():
assert
C
.
grad_all
(
grad_refactor_14
)(
2
,
3
)
==
(
3
,
9
)
assert
C
.
grad_all
(
grad_refactor_14
)(
2
,
3
)
==
(
3
,
9
)
class
IfDeferInline
(
nn
.
Cell
):
def
__init__
(
self
,
mul_size
):
super
().
__init__
()
self
.
mul_weight
=
Tensor
(
np
.
full
(
mul_size
,
0.6
,
dtype
=
np
.
float32
))
self
.
mul
=
P
.
Mul
()
def
construct
(
self
,
inputs
):
x
=
self
.
mul
(
inputs
,
self
.
mul_weight
)
if
True
:
x
=
x
return
x
def
test_grad_if_defer_inline
():
""" test_grad_if_defer_inline """
network
=
IfDeferInline
([
128
,
96
])
network
.
add_flags
(
defer_inline
=
False
)
inp
=
Tensor
(
np
.
ones
([
128
,
96
]).
astype
(
np
.
float32
))
grads
=
C
.
grad_all
(
network
)(
inp
)
assert
grads
==
(
Tensor
(
np
.
full
([
128
,
96
],
0.6
,
dtype
=
np
.
float32
)),)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录