Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
b346f0b3
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
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看板
提交
b346f0b3
编写于
8月 27, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
8月 27, 2020
浏览文件
操作
浏览文件
下载
差异文件
!5026 Fix the problem of resource clear
Merge pull request !5026 from Simson/enhancement-API
上级
fc9161a4
d4babf19
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
47 addition
and
19 deletion
+47
-19
mindspore/ccsrc/pipeline/pynative/pynative_execute.cc
mindspore/ccsrc/pipeline/pynative/pynative_execute.cc
+33
-9
mindspore/ccsrc/pybind_api/ir/primitive_py.cc
mindspore/ccsrc/pybind_api/ir/primitive_py.cc
+11
-7
mindspore/common/dtype.py
mindspore/common/dtype.py
+2
-0
tests/ut/python/pynative_mode/test_user_define_bprop_check.py
...s/ut/python/pynative_mode/test_user_define_bprop_check.py
+1
-3
未找到文件。
mindspore/ccsrc/pipeline/pynative/pynative_execute.cc
浏览文件 @
b346f0b3
...
...
@@ -130,6 +130,13 @@ static std::string GetId(const py::object &obj) {
}
return
prefix
+
key
;
}
if
(
py
::
isinstance
<
mindspore
::
Type
>
(
to_process
))
{
auto
type_ptr
=
py
::
cast
<
mindspore
::
TypePtr
>
(
to_process
);
return
prefix
+
type_ptr
->
ToString
();
}
if
(
py
::
isinstance
<
py
::
str
>
(
to_process
))
{
return
prefix
+
std
::
string
(
py
::
str
(
to_process
));
}
if
(
py
::
isinstance
<
py
::
int_
>
(
to_process
))
{
return
prefix
+
std
::
string
(
py
::
str
(
to_process
));
}
...
...
@@ -1258,17 +1265,24 @@ void PynativeExecutor::GradNetInner(const GradOperationPtr &grad, const py::obje
pipeline
::
ReclaimOptimizer
();
}
template
<
typename
T
>
void
MapClear
(
T
map
,
const
std
::
string
&
flag
)
{
for
(
auto
it
=
map
.
begin
();
it
!=
map
.
end
();)
{
if
(
it
->
first
.
find
(
flag
)
!=
std
::
string
::
npos
)
{
it
->
second
=
nullptr
;
it
=
map
.
erase
(
it
);
}
else
{
it
++
;
}
}
}
void
PynativeExecutor
::
Clear
(
const
std
::
string
&
flag
)
{
if
(
!
flag
.
empty
())
{
MS_LOG
(
DEBUG
)
<<
"Clear res"
;
auto
key_value
=
std
::
find_if
(
graph_map_
.
begin
(),
graph_map_
.
end
(),
[
&
flag
](
const
auto
&
item
)
{
return
item
.
first
.
find
(
flag
)
!=
std
::
string
::
npos
;
});
if
(
key_value
!=
graph_map_
.
end
())
{
std
::
string
key
=
key_value
->
first
;
(
void
)
graph_map_
.
erase
(
key
);
(
void
)
cell_graph_map_
.
erase
(
key
);
(
void
)
cell_resource_map_
.
erase
(
key
);
}
MapClear
<
std
::
unordered_map
<
std
::
string
,
FuncGraphPtr
>>
(
graph_map_
,
flag
);
MapClear
<
std
::
unordered_map
<
std
::
string
,
FuncGraphPtr
>>
(
cell_graph_map_
,
flag
);
MapClear
<
std
::
unordered_map
<
std
::
string
,
ResourcePtr
>>
(
cell_resource_map_
,
flag
);
Clean
();
// Maybe exit in the pynative runing op, so need reset pynative flag.
auto
ms_context
=
MsContext
::
GetInstance
();
...
...
@@ -1286,7 +1300,6 @@ void PynativeExecutor::Clear(const std::string &flag) {
curr_g_
=
nullptr
;
graph_info_map_
.
clear
();
op_id_map_
.
clear
();
// node_abs_map_.clear();
std
::
stack
<
FuncGraphPtr
>
().
swap
(
graph_p_
);
ConfigManager
::
GetInstance
().
ResetIterNum
();
}
...
...
@@ -1300,7 +1313,18 @@ void PynativeExecutor::Clean() {
pipeline
::
ReclaimOptimizer
();
}
template
<
typename
T
>
void
MapErase
(
T
map
)
{
for
(
auto
it
=
map
.
begin
();
it
!=
map
.
end
();)
{
it
=
map
.
erase
(
it
++
);
}
}
void
PynativeExecutor
::
ClearRes
()
{
MapErase
<
std
::
unordered_map
<
std
::
string
,
FuncGraphPtr
>>
(
graph_map_
);
MapErase
<
std
::
unordered_map
<
std
::
string
,
FuncGraphPtr
>>
(
cell_graph_map_
);
MapErase
<
std
::
unordered_map
<
std
::
string
,
ResourcePtr
>>
(
cell_resource_map_
);
MapErase
<
std
::
unordered_map
<
std
::
string
,
abstract
::
AbstractBasePtr
>>
(
node_abs_map_
);
Clean
();
resource_
.
reset
();
}
...
...
mindspore/ccsrc/pybind_api/ir/primitive_py.cc
浏览文件 @
b346f0b3
...
...
@@ -102,13 +102,17 @@ py::tuple check_bprop_out(const py::object &grads_obj, const py::tuple &py_args)
py
::
object
grad_dtype
=
grads
[
i
].
attr
(
"dtype"
);
py
::
tuple
arg_shape
=
py_args
[
i
].
attr
(
"shape"
);
py
::
object
arg_dtype
=
py_args
[
i
].
attr
(
"dtype"
);
if
(
!
grad_shape
.
equal
(
arg_shape
)
||
!
grad_dtype
.
is
(
arg_dtype
))
{
MS_EXCEPTION
(
ValueError
)
<<
"For user define net bprop, the gradient of the "
<<
i
<<
"th arg should have the same shape and dtype as the "
<<
i
<<
"th arg, but the "
<<
i
<<
"th arg shape: "
<<
py
::
cast
<
py
::
str
>
(
arg_shape
)
<<
" and dtype: "
<<
py
::
cast
<
py
::
str
>
(
arg_dtype
)
<<
", the gradient shape: "
<<
py
::
cast
<
py
::
str
>
(
grad_shape
)
<<
" and dtype: "
<<
py
::
cast
<
py
::
str
>
(
grad_dtype
)
<<
"."
;
if
(
!
grad_shape
.
equal
(
arg_shape
))
{
MS_EXCEPTION
(
ValueError
)
<<
"When user defines the net bprop, the gradient of the "
<<
i
<<
"th arg should have the same shape as the "
<<
i
<<
"th arg, but the "
<<
i
<<
"th arg shape is: "
<<
py
::
cast
<
py
::
str
>
(
arg_shape
)
<<
", the gradient shape is: "
<<
py
::
cast
<
py
::
str
>
(
grad_shape
)
<<
"."
;
}
if
(
!
grad_dtype
.
is
(
arg_dtype
))
{
MS_EXCEPTION
(
TypeError
)
<<
"When user defines the net bprop, the gradient of the "
<<
i
<<
"th arg should have the same dtype as the "
<<
i
<<
"th arg, but the "
<<
i
<<
"th arg dtype is: "
<<
py
::
cast
<
py
::
str
>
(
arg_dtype
)
<<
", the gradient dtype is: "
<<
py
::
cast
<
py
::
str
>
(
grad_dtype
)
<<
"."
;
}
}
}
...
...
mindspore/common/dtype.py
浏览文件 @
b346f0b3
...
...
@@ -227,6 +227,7 @@ def dtype_to_pytype(type_):
return
{
bool_
:
bool
,
int_
:
int
,
int8
:
int
,
int16
:
int
,
int32
:
int
,
...
...
@@ -235,6 +236,7 @@ def dtype_to_pytype(type_):
uint16
:
int
,
uint32
:
int
,
uint64
:
int
,
float_
:
float
,
float16
:
float
,
float32
:
float
,
float64
:
float
,
...
...
tests/ut/python/pynative_mode/test_user_define_bprop_check.py
浏览文件 @
b346f0b3
...
...
@@ -116,7 +116,6 @@ def test_user_define_bprop_check_shape():
grad_net
=
GradNet
(
net
)
with
pytest
.
raises
(
ValueError
)
as
ex
:
ret
=
grad_net
(
x
,
sens
)
assert
"the gradient of the 0th arg should have the same shape and dtype as the 0th arg"
in
str
(
ex
.
value
)
def
test_user_define_bprop_check_dtype
():
...
...
@@ -145,9 +144,8 @@ def test_user_define_bprop_check_dtype():
context
.
set_context
(
mode
=
context
.
PYNATIVE_MODE
,
check_bprop
=
True
)
net
=
Net
()
grad_net
=
GradNet
(
net
)
with
pytest
.
raises
(
Valu
eError
)
as
ex
:
with
pytest
.
raises
(
Typ
eError
)
as
ex
:
ret
=
grad_net
(
x
,
sens
)
assert
"the gradient of the 0th arg should have the same shape and dtype as the 0th arg"
in
str
(
ex
.
value
)
def
test_user_define_bprop_check_parameter
():
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录