Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
94c9019d
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看板
提交
94c9019d
编写于
6月 02, 2020
作者:
B
buxue
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
restricting modify non_Parameter class members
上级
5d397d84
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
64 addition
and
9 deletion
+64
-9
mindspore/ccsrc/pipeline/parse/parse.cc
mindspore/ccsrc/pipeline/parse/parse.cc
+15
-5
tests/ut/python/dtype/test_dictionary.py
tests/ut/python/dtype/test_dictionary.py
+7
-2
tests/ut/python/dtype/test_list.py
tests/ut/python/dtype/test_list.py
+42
-2
未找到文件。
mindspore/ccsrc/pipeline/parse/parse.cc
浏览文件 @
94c9019d
...
...
@@ -1175,11 +1175,11 @@ void Parser::HandleAssignClassMember(const FunctionBlockPtr &block, const py::ob
auto
filename
=
location
[
0
].
cast
<
std
::
string
>
();
auto
line_no
=
location
[
1
].
cast
<
int
>
();
// Now only support the self.xxx = yyy, where self.xxx must be a defined Parameter type
if
(
!
py
::
hasattr
(
ast
()
->
obj
(),
attr_name
.
c_str
(
)))
{
if
(
!
py
::
hasattr
(
ast
()
->
obj
(),
common
::
SafeCStr
(
attr_name
)))
{
MS_EXCEPTION
(
TypeError
)
<<
"'"
<<
var_name
<<
"' should be a Parameter, but not defined, at "
<<
filename
<<
":"
<<
line_no
;
}
auto
obj
=
ast
()
->
obj
().
attr
(
attr_name
.
c_str
(
));
auto
obj
=
ast
()
->
obj
().
attr
(
common
::
SafeCStr
(
attr_name
));
auto
obj_type
=
obj
.
attr
(
"__class__"
).
attr
(
"__name__"
);
if
(
!
py
::
hasattr
(
obj
,
"__parameter__"
))
{
MS_EXCEPTION
(
TypeError
)
<<
"'"
<<
var_name
<<
"' should be a Parameter, but got '"
...
...
@@ -1205,8 +1205,18 @@ void Parser::HandleAssignSubscript(const FunctionBlockPtr &block, const py::obje
// getitem apply should return the sequence data structure itself
std
::
string
var_name
=
""
;
if
(
ast_
->
IsClassMember
(
value_obj
))
{
var_name
=
"self."
;
(
void
)
var_name
.
append
(
value_obj
.
attr
(
"attr"
).
cast
<
std
::
string
>
());
std
::
string
attr_name
=
value_obj
.
attr
(
"attr"
).
cast
<
std
::
string
>
();
var_name
=
"self."
+
attr_name
;
if
(
!
py
::
hasattr
(
ast
()
->
obj
(),
common
::
SafeCStr
(
attr_name
)))
{
MS_EXCEPTION
(
TypeError
)
<<
"'"
<<
var_name
<<
"' was not defined in the class '__init__' function."
;
}
auto
obj
=
ast
()
->
obj
().
attr
(
common
::
SafeCStr
(
attr_name
));
auto
obj_type
=
obj
.
attr
(
"__class__"
).
attr
(
"__name__"
);
if
(
!
py
::
hasattr
(
obj
,
"__parameter__"
))
{
MS_EXCEPTION
(
TypeError
)
<<
"'"
<<
var_name
<<
"' should be a Parameter, but got '"
<<
py
::
str
(
obj
).
cast
<
std
::
string
>
()
<<
"' with type '"
<<
py
::
str
(
obj_type
).
cast
<
std
::
string
>
()
<<
"'."
;
}
}
else
{
var_name
=
value_obj
.
attr
(
"id"
).
cast
<
std
::
string
>
();
}
...
...
@@ -1231,7 +1241,7 @@ void Parser::WriteAssignVars(const FunctionBlockPtr &block, const py::object &ta
}
}
// process a assign statement
, such as a =b, a,b = tup
// process a assign statement, such as a =b, a,b = tup
FunctionBlockPtr
Parser
::
ParseAssign
(
const
FunctionBlockPtr
&
block
,
const
py
::
object
&
node
)
{
MS_LOG
(
DEBUG
)
<<
"Process ast assgin"
;
py
::
object
value_object
=
python_adapter
::
GetPyObjAttr
(
node
,
"value"
);
...
...
tests/ut/python/dtype/test_dictionary.py
浏览文件 @
94c9019d
...
...
@@ -17,6 +17,7 @@
@Desc : test_dictionary
"""
import
numpy
as
np
import
pytest
from
mindspore
import
Tensor
,
context
from
mindspore.nn
import
Cell
...
...
@@ -89,7 +90,9 @@ def test_dict_set_or_get_item():
return
ret
net
=
DictNet
()
assert
net
()
==
(
88
,
99
,
4
,
5
,
6
)
with
pytest
.
raises
(
TypeError
)
as
ex
:
net
()
assert
"'self.dict_' should be a Parameter"
in
str
(
ex
.
value
)
def
test_dict_set_or_get_item_2
():
...
...
@@ -135,7 +138,9 @@ def test_dict_set_or_get_item_3():
return
self
.
dict_
[
"x"
]
net
=
DictNet
()
assert
net
()
==
Tensor
(
np
.
ones
([
4
,
2
,
3
],
np
.
float32
))
with
pytest
.
raises
(
TypeError
)
as
ex
:
net
()
assert
"'self.dict_' should be a Parameter"
in
str
(
ex
.
value
)
def
test_dict_set_item
():
...
...
tests/ut/python/dtype/test_list.py
浏览文件 @
94c9019d
...
...
@@ -15,6 +15,7 @@
import
functools
import
numpy
as
np
import
pytest
import
mindspore.nn
as
nn
import
mindspore.context
as
context
from
mindspore
import
Tensor
...
...
@@ -24,6 +25,7 @@ from tests.mindspore_test_framework.mindspore_test import mindspore_test
from
tests.mindspore_test_framework.pipeline.forward.compile_forward
\
import
pipeline_for_compile_forward_ge_graph_for_case_by_case_config
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
)
def
test_list_equal
():
class
Net
(
nn
.
Cell
):
...
...
@@ -109,7 +111,7 @@ def test_list_append():
assert
net
(
x
,
y
)
==
y
def
test_
list_append_2
():
def
test_
class_member_list_append
():
class
Net
(
nn
.
Cell
):
def
__init__
(
self
,
z
:
list
):
super
(
Net
,
self
).
__init__
()
...
...
@@ -129,7 +131,45 @@ def test_list_append_2():
y
=
Tensor
(
np
.
zeros
([
3
,
4
,
5
],
np
.
int32
))
z
=
[[
1
,
2
],
3
]
net
=
Net
(
z
)
assert
net
(
x
,
y
)
==
x
with
pytest
.
raises
(
TypeError
)
as
ex
:
net
(
x
,
y
)
assert
"'self.z' should be a Parameter, but got '[[1, 2], 3]' with type 'list'."
in
str
(
ex
.
value
)
def
test_class_member_not_defined
():
class
Net
(
nn
.
Cell
):
def
__init__
(
self
,
z
:
list
):
super
(
Net
,
self
).
__init__
()
self
.
z
=
z
def
construct
(
self
,
x
,
y
):
self
.
x
[
0
]
=
9
return
self
.
x
z
=
[[
1
,
2
],
3
]
net
=
Net
(
z
)
with
pytest
.
raises
(
TypeError
)
as
ex
:
net
()
assert
"'self.x' was not defined in the class '__init__' function."
in
str
(
ex
.
value
)
def
test_change_list_element
():
class
Net
(
nn
.
Cell
):
def
__init__
(
self
,
z
:
list
):
super
(
Net
,
self
).
__init__
()
self
.
z
=
z
def
construct
(
self
,
x
,
y
):
self
.
z
[
0
]
=
x
return
self
.
z
[
0
]
x
=
Tensor
(
np
.
ones
([
6
,
8
,
10
],
np
.
int32
))
y
=
Tensor
(
np
.
zeros
([
3
,
4
,
5
],
np
.
int32
))
z
=
[[
1
,
2
],
3
]
net
=
Net
(
z
)
with
pytest
.
raises
(
TypeError
)
as
ex
:
net
(
x
,
y
)
assert
"'self.z' should be a Parameter, but got '[[1, 2], 3]' with type 'list'."
in
str
(
ex
.
value
)
class
ListOperate
(
nn
.
Cell
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录