Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
09b43728
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看板
提交
09b43728
编写于
7月 21, 2020
作者:
K
kingfo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support mix precision in pynative
上级
8f4bab4e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
47 addition
and
4 deletion
+47
-4
mindspore/nn/cell.py
mindspore/nn/cell.py
+46
-3
tests/st/ops/cpu/test_lstm_op.py
tests/st/ops/cpu/test_lstm_op.py
+1
-1
未找到文件。
mindspore/nn/cell.py
浏览文件 @
09b43728
...
...
@@ -26,6 +26,7 @@ from ..common.parameter import Parameter, ParameterTuple
from
.._c_expression
import
init_backend
from
..ops.primitive
import
Primitive
from
..ops.operations
import
HookBackward
from
..ops.functional
import
cast
from
..parallel._tensor
import
_load_tensor_by_layout
from
..common.tensor
import
Tensor
...
...
@@ -60,6 +61,7 @@ class Cell:
def
__init__
(
self
,
auto_prefix
=
True
,
flags
=
None
):
self
.
_params
=
OrderedDict
()
self
.
_cells
=
OrderedDict
()
self
.
_params_list
=
OrderedDict
()
self
.
training
=
False
self
.
requires_grad
=
False
self
.
pynative
=
False
...
...
@@ -188,11 +190,22 @@ class Cell:
if
'_params'
in
self
.
__dict__
:
params
=
self
.
__dict__
[
'_params'
]
if
name
in
params
:
if
context
.
get_context
(
"mode"
)
==
context
.
PYNATIVE_MODE
:
return
self
.
cast_param
(
params
[
name
])
return
params
[
name
]
if
'_cells'
in
self
.
__dict__
:
cells
=
self
.
__dict__
[
'_cells'
]
if
name
in
cells
:
return
cells
[
name
]
if
context
.
get_context
(
"mode"
)
==
context
.
PYNATIVE_MODE
and
'_params_list'
in
self
.
__dict__
:
params_list
=
self
.
__dict__
[
'_params_list'
]
if
name
in
params_list
:
para_list
=
params_list
[
name
]
cast_list
=
list
()
for
para
in
para_list
:
cast_list
.
append
(
self
.
cast_param
(
para
))
para_list
=
ParameterTuple
(
cast_list
)
return
para_list
raise
AttributeError
(
"'{}' object has no attribute '{}'."
.
format
(
type
(
self
).
__name__
,
name
))
def
__del__
(
self
):
...
...
@@ -225,10 +238,21 @@ class Cell:
cell
.
set_grad
(
True
)
else
:
_pynative_exec
.
set_grad_flag
(
False
)
cast_inputs
=
list
()
if
hasattr
(
self
,
"_mindspore_flags"
)
and
self
.
_mindspore_flags
.
get
(
'fp16'
):
for
item
in
inputs
:
cast_inputs
.
append
(
cast
(
item
,
mstype
.
float16
))
if
hasattr
(
self
,
"_mindspore_flags"
)
and
self
.
_mindspore_flags
.
get
(
'fp32'
):
for
item
in
inputs
:
cast_inputs
.
append
(
cast
(
item
,
mstype
.
float32
))
if
cast_inputs
:
cast_inputs
=
tuple
(
cast_inputs
)
else
:
cast_inputs
=
inputs
if
self
.
enable_hook
:
output
=
self
.
_hook_construct
(
*
inputs
)
output
=
self
.
_hook_construct
(
*
cast_
inputs
)
else
:
output
=
self
.
construct
(
*
inputs
)
output
=
self
.
construct
(
*
cast_
inputs
)
if
isinstance
(
output
,
Parameter
):
output
=
output
.
data
if
self
.
requires_grad
is
True
:
...
...
@@ -241,6 +265,7 @@ class Cell:
def
__setattr__
(
self
,
name
,
value
):
cells
=
self
.
__dict__
.
get
(
'_cells'
)
params
=
self
.
__dict__
.
get
(
'_params'
)
params_list
=
self
.
__dict__
.
get
(
'_params_list'
)
if
isinstance
(
value
,
Parameter
):
if
params
is
None
:
raise
AttributeError
(
"Can not assign params before Cell.__init__() call."
)
...
...
@@ -256,7 +281,12 @@ class Cell:
raise
AttributeError
(
"Can not assign params before Cell.__init__() call."
)
for
item
in
value
:
self
.
insert_param_to_cell
(
item
.
name
,
item
,
check_name
=
False
)
object
.
__setattr__
(
self
,
name
,
value
)
if
context
.
get_context
(
"mode"
)
==
context
.
PYNATIVE_MODE
:
if
name
in
self
.
__dict__
:
del
self
.
__dict__
[
name
]
params_list
[
name
]
=
value
else
:
object
.
__setattr__
(
self
,
name
,
value
)
elif
isinstance
(
value
,
Cell
):
if
cells
is
None
:
raise
AttributeError
(
"Can not assign cells before Cell.__init__() call."
)
...
...
@@ -458,6 +488,19 @@ class Cell:
raise
TypeError
(
"The type of parameter should be 'Parameter' if not None."
)
self
.
_params
[
param_name
]
=
param
def
cast_param
(
self
,
param
):
"""
Cast parameter according to auto mix precison level in pynative mode.
Args:
param (Parameter): The parameter to cast.
"""
if
hasattr
(
self
,
"_mindspore_flags"
)
and
self
.
_mindspore_flags
.
get
(
'fp16'
):
return
cast
(
param
,
mstype
.
float16
)
if
hasattr
(
self
,
"_mindspore_flags"
)
and
self
.
_mindspore_flags
.
get
(
'fp32'
):
return
cast
(
param
,
mstype
.
float32
)
return
param
def
insert_child_to_cell
(
self
,
child_name
,
child
):
"""
Adds a child cell to the current cell.
...
...
tests/st/ops/cpu/test_lstm_op.py
浏览文件 @
09b43728
...
...
@@ -23,7 +23,7 @@ from mindspore.ops import composite as C
from
mindspore.common.tensor
import
Tensor
from
mindspore.common.parameter
import
ParameterTuple
,
Parameter
context
.
set_context
(
device_target
=
'CPU'
)
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
'CPU'
)
class
LstmNet
(
nn
.
Cell
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录