Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
4291ea82
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看板
提交
4291ea82
编写于
6月 17, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
6月 17, 2020
浏览文件
操作
浏览文件
下载
差异文件
!2024 Add MixedPrecisionCast for Dict
Merge pull request !2024 from Kang/master
上级
17c3148f
3ba20742
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
51 addition
and
1 deletion
+51
-1
mindspore/ccsrc/pipeline/static_analysis/prim.cc
mindspore/ccsrc/pipeline/static_analysis/prim.cc
+16
-0
mindspore/ccsrc/pipeline/static_analysis/static_analysis.cc
mindspore/ccsrc/pipeline/static_analysis/static_analysis.cc
+1
-1
tests/ut/python/model/test_mix_precision.py
tests/ut/python/model/test_mix_precision.py
+34
-0
未找到文件。
mindspore/ccsrc/pipeline/static_analysis/prim.cc
浏览文件 @
4291ea82
...
...
@@ -286,6 +286,22 @@ AnfNodePtr MixedPrecisionCastHelper(AnfNodePtr source_node, AbstractBasePtr node
++
idx
;
}
target_node
=
func_graph
->
NewCNode
(
nodes
);
}
else
if
(
node_type
->
isa
<
AbstractDictionary
>
())
{
auto
x
=
node_type
->
cast
<
AbstractDictionaryPtr
>
();
auto
&
items
=
x
->
elements
();
std
::
vector
<
AnfNodePtr
>
dict_key_nodes
;
std
::
vector
<
AnfNodePtr
>
dict_value_nodes
;
dict_key_nodes
.
emplace_back
(
NewValueNode
(
prim
::
kPrimMakeTuple
));
dict_value_nodes
.
emplace_back
(
NewValueNode
(
prim
::
kPrimMakeTuple
));
for
(
const
auto
&
item
:
items
)
{
AnfNodePtr
dict_value_node
=
func_graph
->
NewCNode
({
NewValueNode
(
prim
::
kPrimDictGetItem
),
source_node
,
NewValueNode
(
item
.
first
)});
AnfNodePtr
node
=
MixedPrecisionCastHelper
(
dict_value_node
,
item
.
second
,
target_type
,
func_graph
);
dict_key_nodes
.
emplace_back
(
NewValueNode
(
item
.
first
));
dict_value_nodes
.
emplace_back
(
node
);
}
target_node
=
func_graph
->
NewCNode
({
NewValueNode
(
prim
::
kPrimMakeDict
),
func_graph
->
NewCNode
(
dict_key_nodes
),
func_graph
->
NewCNode
(
dict_value_nodes
)});
}
return
target_node
;
}
...
...
mindspore/ccsrc/pipeline/static_analysis/static_analysis.cc
浏览文件 @
4291ea82
...
...
@@ -308,7 +308,7 @@ EvaluatorPtr GetPrimEvaluator(const PrimitivePtr &prim, const AnalysisEnginePtr
evaluator
=
std
::
make_shared
<
UnpackGraphEvaluator
>
(
prim
);
return
evaluator
;
}
if
(
prim
->
name
()
==
prim
::
kPrimMixedPrecisionCast
->
name
())
{
if
(
prim
->
Hash
()
==
prim
::
kPrimMixedPrecisionCast
->
Hash
()
&&
prim
->
name
()
==
prim
::
kPrimMixedPrecisionCast
->
name
())
{
evaluator
=
std
::
make_shared
<
MixedPrecisionCastEvaluator
>
(
prim
);
return
evaluator
;
}
...
...
tests/ut/python/model/test_mix_precision.py
浏览文件 @
4291ea82
...
...
@@ -25,6 +25,7 @@ from mindspore.nn import Momentum
from
mindspore.nn
import
TrainOneStepCell
,
WithLossCell
from
mindspore.ops
import
composite
as
C
from
mindspore.ops
import
operations
as
P
from
mindspore.ops
import
functional
as
F
from
mindspore.train.parallel_utils
import
ParallelMode
from
tests.ops_common
import
convert
from
....train_step_wrap
import
train_step_with_loss_warp
...
...
@@ -185,3 +186,36 @@ def test_grad_conv_prelu():
net
=
GetParamGrad
(
net
)
net
.
set_train
()
net
(
*
all_inputs
)
def
test_dict_cast
():
class
FirstNet
(
nn
.
Cell
):
def
__init__
(
self
):
super
(
FirstNet
,
self
).
__init__
()
self
.
net
=
SecondNet
()
self
.
sub
=
P
.
Sub
()
def
construct
(
self
,
tensor_a
,
tensor_b
):
a
=
F
.
mixed_precision_cast
(
mstype
.
float16
,
tensor_a
)
b
=
F
.
mixed_precision_cast
(
mstype
.
float16
,
tensor_b
)
c
=
self
.
sub
(
a
,
b
)
dictionary
=
{
"key"
:
a
}
result
=
self
.
net
(
c
,
key1
=
a
,
key2
=
dictionary
)
return
result
class
SecondNet
(
nn
.
Cell
):
def
__init__
(
self
):
super
(
SecondNet
,
self
).
__init__
()
self
.
add
=
P
.
TensorAdd
()
def
construct
(
self
,
tensor_c
,
**
kwargs
):
d
=
F
.
mixed_precision_cast
(
mstype
.
float16
,
tensor_c
)
dict_cast
=
F
.
mixed_precision_cast
(
mstype
.
float16
,
kwargs
)
e
=
self
.
add
(
d
,
dict_cast
[
"key1"
])
f
=
self
.
add
(
e
,
dict_cast
[
"key2"
][
"key"
])
return
f
x
=
Tensor
(
np
.
array
([
1
,
2.5
,
3.5
]),
mstype
.
float32
)
y
=
Tensor
(
np
.
array
([
4
,
5.5
,
6.5
]),
mstype
.
float32
)
net
=
FirstNet
()
net
(
x
,
y
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录