Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
正统之独孤求败
mindspore
提交
44b656a9
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,发现更多精彩内容 >>
提交
44b656a9
编写于
5月 29, 2020
作者:
K
kingfo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add data sync before hook function
上级
9cbed69e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
50 addition
and
10 deletion
+50
-10
mindspore/ccsrc/vm/vm.cc
mindspore/ccsrc/vm/vm.cc
+19
-0
mindspore/ccsrc/vm/vm.h
mindspore/ccsrc/vm/vm.h
+2
-1
mindspore/nn/cell.py
mindspore/nn/cell.py
+22
-6
mindspore/ops/operations/debug_ops.py
mindspore/ops/operations/debug_ops.py
+7
-3
未找到文件。
mindspore/ccsrc/vm/vm.cc
浏览文件 @
44b656a9
...
...
@@ -602,6 +602,19 @@ void FinalVM::InstPushPrim(const VectorRef &args) {
MS_LOG
(
DEBUG
)
<<
"End"
;
}
void
FinalVM
::
SyncData
(
const
py
::
object
&
arg
)
{
if
(
py
::
isinstance
<
py
::
tuple
>
(
arg
))
{
py
::
tuple
arg_list
=
py
::
cast
<
py
::
tuple
>
(
arg
);
for
(
size_t
i
=
0
;
i
<
arg_list
.
size
();
i
++
)
{
SyncData
(
arg_list
[
i
]);
}
}
if
(
py
::
isinstance
<
tensor
::
Tensor
>
(
arg
))
{
auto
tensor
=
py
::
cast
<
tensor
::
TensorPtr
>
(
arg
);
(
void
)
tensor
->
data_sync
();
}
}
BaseRef
FinalVM
::
RunHook
(
const
PrimitivePtr
&
prim
,
const
VectorRef
&
args
)
{
MS_LOG
(
DEBUG
)
<<
"input for operation:"
;
std
::
size_t
args_size
=
args
.
size
();
...
...
@@ -612,15 +625,20 @@ BaseRef FinalVM::RunHook(const PrimitivePtr &prim, const VectorRef &args) {
MS_LOG
(
DEBUG
)
<<
"arg: "
<<
i
<<
":"
;
i
++
;
}
// Hook operator for execute cell custom bprop function
py
::
object
obj
;
bool
is_bprop
=
prim
->
HasAttr
(
"bprop"
);
if
(
is_bprop
)
{
SyncData
(
py_args
);
py
::
function
fn_bprop
=
prim
->
hook
();
obj
=
fn_bprop
(
*
py_args
);
return
obj
;
}
// Sync gradient data from device to host
SyncData
(
py_args
[
2
]);
bool
is_cell
=
prim
->
HasAttr
(
"cell_hook"
);
if
(
is_cell
)
{
// Hook operator for execute cell hook function
std
::
string
cell_id
=
GetValue
<
std
::
string
>
(
prim
->
GetAttr
(
"cell_id"
));
if
(
_hook_grad
.
find
(
cell_id
)
!=
_hook_grad
.
end
())
{
std
::
size_t
hook_args_size
=
3
;
...
...
@@ -639,6 +657,7 @@ BaseRef FinalVM::RunHook(const PrimitivePtr &prim, const VectorRef &args) {
obj
=
py_args
[
2
];
}
}
else
{
// Hook operator for execute variable hook function
py
::
function
fn_hook
=
prim
->
hook
();
obj
=
fn_hook
(
py
::
make_tuple
(
py_args
[
2
]));
if
(
py
::
isinstance
<
py
::
none
>
(
obj
))
{
...
...
mindspore/ccsrc/vm/vm.h
浏览文件 @
44b656a9
...
...
@@ -115,7 +115,7 @@ class FinalVM {
void
InstPushPrim
(
const
VectorRef
&
args
);
void
InstSwitchReturn
(
const
VectorRef
&
args
);
void
set_insts
(
const
InstSet
&
value
)
{
insts_
=
value
;
}
BaseRef
RunHook
(
const
PrimitivePtr
&
prim
,
const
VectorRef
&
arg
s
);
BaseRef
RunHook
(
const
PrimitivePtr
&
prim
,
const
VectorRef
&
arg
);
protected:
BaseRef
Ref
(
int
i
);
...
...
@@ -129,6 +129,7 @@ class FinalVM {
void
PushStatus
(
bool
is_switch_call
);
bool
PopStatus
();
void
DoJmp
(
const
BaseRef
&
jmp
);
void
SyncData
(
const
py
::
object
&
args
);
void
MergeJmpArgs
(
const
BaseRef
&
jmp
,
const
BaseRef
&
c
);
BaseRef
MergeArgs
(
const
BaseRef
&
first
,
const
BaseRef
&
second
);
...
...
mindspore/nn/cell.py
浏览文件 @
44b656a9
...
...
@@ -77,7 +77,7 @@ class Cell:
if
flags
:
self
.
add_flags
(
**
flags
)
self
.
_backward_hook
=
None
self
.
_
enable_hook
=
False
self
.
enable_hook
=
False
self
.
_bprop_debug
=
False
@
property
...
...
@@ -97,10 +97,24 @@ class Cell:
@
property
def
bprop_debug
(
self
):
"""
Get whether cell custom bprop debug is enabled.
"""
return
self
.
_bprop_debug
@
bprop_debug
.
setter
def
bprop_debug
(
self
,
value
):
"""
Set whether to enable cell custom bprop debug.
Note:
When bprop is defined in cell, the bprop function will be executed
in python interpreter when bprop debug is true, and will be parsed
and add to graph when bprop debug is false.
Args:
value (bool): Specifies whether to enable bprop debug. Default: False.
"""
if
not
isinstance
(
value
,
bool
):
raise
TypeError
(
"'bprop debug' value must be bool type."
)
self
.
_bprop_debug
=
value
...
...
@@ -755,17 +769,19 @@ class Cell:
outputs
=
self
.
_backward_hook
(
inputs
)
return
outputs
@
property
def
enable_hook
(
self
):
"""Whether the cell register hook function"""
return
self
.
_enable_hook
def
register_backward_hook
(
self
,
fn
):
"""
Set the cell backward hook function.
Note:
fn should be defined as following code shows, `cell_name` is the name of registered cell,
`grad_input` is gradient passed to the cell, `grad_output` is the gradient computed and pass to
next cell or primitve, which may be modified and return.
>>> hook_fn(cell_name, grad_input, grad_output) -> Tensor or None
Args:
fn (function): Specifies the hook function with grad as input.
"""
self
.
_backward_hook
=
HookBackward
(
fn
,
self
.
cls_name
+
"("
+
str
(
id
(
self
))
+
")"
)
self
.
_enable_hook
=
True
mindspore/ops/operations/debug_ops.py
浏览文件 @
44b656a9
...
...
@@ -247,9 +247,11 @@ class HookBackward(PrimitiveWithInfer):
Used as tag to hook gradient in intermediate variables.
Note:
The hook function should have one input of gradient of the variable.
hook function will be executed in python environment, while callback
of InsertGradientOf will be parsed and added to the graph.
The hook function should be defined like `hook_fn(grad) -> Tensor or None`,
which grad is the gradient passed to the primitive and gradient may be
modified and passed to nex primitive. the difference between hook function and
callback of InsertGradientOf is that hook function is executed in python
environment while callback will be parsed and added to the graph.
Args:
hook_fn (Function): Python function. hook function.
...
...
@@ -312,6 +314,8 @@ class Print(PrimitiveWithInfer):
2. The data of tensor is a scalar type.
In pynative mode, please use python print function.
Inputs:
- **input_x** (Union[Tensor, str]) - The graph node to attach to. The input supports
multiple strings and tensors which are separated by ','.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录