!14447 show accurate error info and error lines when use unsupported builtin function

From: @zhangbuxue
Reviewed-by: @ginfung,@ginfung,@zh_qh
Signed-off-by: @zh_qh
This commit is contained in:
mindspore-ci-bot 2021-03-31 14:24:37 +08:00 committed by Gitee
commit d78463d2f5
5 changed files with 222 additions and 183 deletions

View File

@ -21,13 +21,16 @@ from .parser import (Parser, create_obj_instance, generate_scope,
get_class_member_namespace_symbol, create_slice_obj, get_class_member_namespace_symbol, create_slice_obj,
get_dataclass_attributes, get_dataclass_methods, get_obj_id, get_dataclass_attributes, get_dataclass_methods, get_obj_id,
get_module_namespace, get_obj_type, get_object_key, get_module_namespace, get_obj_type, get_object_key,
get_ast_type, get_node_type, get_args, get_args_default_values,
get_ast_namespace_symbol, get_operation_namespace_symbol,
get_parse_method_of_class, get_scope_name, expand_expr_statement, get_parse_method_of_class, get_scope_name, expand_expr_statement,
is_class_member, parse_cb, resolve_symbol, convert_to_ms_tensor, get_object_description) is_class_member, parse_cb, resolve_symbol, convert_to_ms_tensor, get_object_description)
from .serialize import * from .serialize import *
__all__ = ['parse_cb', 'get_parse_method_of_class', 'get_bprop_method_of_class', 'resolve_symbol', __all__ = ['parse_cb', 'get_parse_method_of_class', 'get_bprop_method_of_class', 'resolve_symbol',
'get_object_key', 'get_class_instance_type', 'is_class_member', 'get_object_key', 'get_class_instance_type', 'is_class_member', 'get_ast_type', 'get_node_type',
'get_obj_type', 'get_obj_id', 'create_obj_instance', 'get_module_namespace', 'get_args_default_values', 'get_ast_namespace_symbol', 'get_operation_namespace_symbol',
'get_args', 'get_obj_type', 'get_obj_id', 'create_obj_instance', 'get_module_namespace',
'get_class_member_namespace_symbol', 'get_obj_id', 'Parser', 'get_dataclass_attributes', 'get_class_member_namespace_symbol', 'get_obj_id', 'Parser', 'get_dataclass_attributes',
'get_dataclass_methods', 'dump_obj', 'load_obj', 'get_dataclass_methods', 'get_scope_name', 'get_dataclass_methods', 'dump_obj', 'load_obj', 'get_dataclass_methods', 'get_scope_name',
'create_slice_obj', 'convert_to_ms_tensor', 'get_object_description', 'expand_expr_statement'] 'create_slice_obj', 'convert_to_ms_tensor', 'get_object_description', 'expand_expr_statement']

View File

@ -371,6 +371,89 @@ def expand_expr_statement(node):
return (False,) return (False,)
def get_ast_namespace_symbol(obj):
"""Get obj type and namespace and symbol."""
# step 1:get symbol from object map
ops_info = parse_object_map.get(type(obj), SYMBOL_UNDEFINE)
logger.debug("ops info = %r", ops_info)
return ops_info
def get_operation_namespace_symbol(var: str):
"""Get operation namespace and symbol."""
ops_info = (trope_ns, var)
logger.debug("get operation ops info = %r", ops_info)
return ops_info
def get_ast_type(node):
"""Get the ast type."""
ast_type = AST_SUB_TYPE_UNKNOWN
if isinstance(node, ast.And):
ast_type = AST_SUB_TYPE_AND
elif isinstance(node, ast.Or):
ast_type = AST_SUB_TYPE_OR
elif isinstance(node, ast.Name):
ast_type = AST_SUB_TYPE_NAME
elif isinstance(node, ast.Tuple):
ast_type = AST_SUB_TYPE_TUPLE
elif isinstance(node, ast.Subscript):
ast_type = AST_SUB_TYPE_SUBSCRIPT
elif isinstance(node, ast.Starred):
ast_type = AST_SUB_TYPE_STARRED
elif isinstance(node, ast.Attribute):
ast_type = AST_SUB_TYPE_ATTRIBUTE
else:
ast_type = AST_SUB_TYPE_UNKNOWN
return ast_type
def get_node_type(node):
"""Process an ast node."""
method_name = f'{node.__class__.__name__}'
node_type = [method_name]
# judge the ast main type
if isinstance(node, ast.stmt):
node_type.append(AST_MAIN_TYPE_STMT)
elif isinstance(node, (ast.expr, ast.slice)) or node is None:
# ast.slice and ast.expr should be expr
node_type.append(AST_MAIN_TYPE_EXPR)
else:
node_type.append(AST_MAIN_TYPE_UNKNOWN)
return node_type
def get_args_default_values(node):
"""get the args'default values of parse object."""
nondefaults = [None] * (len(node.args.args) - len(node.args.defaults))
defaults = nondefaults + node.args.defaults + node.args.kw_defaults
if node.args.vararg:
defaults.append(None)
if node.args.kwarg:
defaults.append(None)
return defaults
def get_args(node):
"""Get the arg of parse object."""
args = []
# process position args
for arg in node.args.args:
args.append(arg)
# process kwonlyargs: kwonlyargs is append after position args
if node.args.kwonlyargs:
for kwarg in node.args.kwonlyargs:
args.append(kwarg)
# process vararg: vararg is append after kwonlyargs
if node.args.vararg:
args.append(node.args.vararg)
# process kwarg: kwarg is append after vararg
if node.args.kwarg:
args.append(node.args.kwarg)
return args
class Parser: class Parser:
""" """
Parser python code to ast tree. Parser python code to ast tree.
@ -416,102 +499,28 @@ class Parser:
idt_err.filename = self.filename idt_err.filename = self.filename
idt_err.lineno = self.line_offset idt_err.lineno = self.line_offset
idt_err.msg = f"There are incorrect indentations in definition or comment of function: " \ idt_err.msg = f"There are incorrect indentations in definition or comment of function: " \
f"'{self.fn.__qualname__}'." f"'{self.fn.__qualname__}'."
raise idt_err raise idt_err
Parser.ast_cache[hexstr] = tree Parser.ast_cache[hexstr] = tree
else: else:
logger.error("Fn type is invalid") logger.error("Fn type is invalid")
return tree return tree
def get_args(self, node):
"""Get the arg of parse object."""
args = []
# process position args
for arg in node.args.args:
args.append(arg)
# process kwonlyargs: kwonlyargs is append after position args
if node.args.kwonlyargs:
for kwarg in node.args.kwonlyargs:
args.append(kwarg)
# process vararg: vararg is append after kwonlyargs
if node.args.vararg:
args.append(node.args.vararg)
# process kwarg: kwarg is append after vararg
if node.args.kwarg:
args.append(node.args.kwarg)
return args
def get_args_default_values(self, node):
"""get the args'default values of parse object."""
nondefaults = [None] * (len(node.args.args) - len(node.args.defaults))
defaults = nondefaults + node.args.defaults + node.args.kw_defaults
if node.args.vararg:
defaults.append(None)
if node.args.kwarg:
defaults.append(None)
return defaults
def get_node_type(self, node):
"""Process an ast node."""
method_name = f'{node.__class__.__name__}'
node_type = [method_name]
# judge the ast main type
if isinstance(node, ast.stmt):
node_type.append(AST_MAIN_TYPE_STMT)
elif isinstance(node, (ast.expr, ast.slice)) or node is None:
# ast.slice and ast.expr should be expr
node_type.append(AST_MAIN_TYPE_EXPR)
else:
node_type.append(AST_MAIN_TYPE_UNKNOWN)
return node_type
def get_ast_type(self, node):
"""Get the ast type."""
ast_type = AST_SUB_TYPE_UNKNOWN
if isinstance(node, ast.And):
ast_type = AST_SUB_TYPE_AND
elif isinstance(node, ast.Or):
ast_type = AST_SUB_TYPE_OR
elif isinstance(node, ast.Name):
ast_type = AST_SUB_TYPE_NAME
elif isinstance(node, ast.Tuple):
ast_type = AST_SUB_TYPE_TUPLE
elif isinstance(node, ast.Subscript):
ast_type = AST_SUB_TYPE_SUBSCRIPT
elif isinstance(node, ast.Starred):
ast_type = AST_SUB_TYPE_STARRED
elif isinstance(node, ast.Attribute):
ast_type = AST_SUB_TYPE_ATTRIBUTE
else:
ast_type = AST_SUB_TYPE_UNKNOWN
return ast_type
def get_namespace_symbol(self, var: str): def get_namespace_symbol(self, var: str):
"""Get symbol type and namespace and symbol.""" """Get symbol type and namespace and symbol."""
if var in self.closure_namespace: if var in self.closure_namespace:
ops_info = (self.closure_namespace, var)
logger.debug("in closure_namespace") logger.debug("in closure_namespace")
elif var in self.global_namespace: return self.closure_namespace, var
ops_info = (self.global_namespace, var) if var in self.global_namespace:
logger.debug("in global_namespace") logger.debug("in global_namespace")
else: value = self.global_namespace[var]
ops_info = parse_object_map.get(SYMBOL_UNDEFINE) if isinstance(value, type(abs)) and self.global_namespace[var] not in convert_object_map:
ops_info = [ops_info[0], var] error_info = f"The builtin function '{var}' is not supported in graph mode."
return ops_info return None, var, error_info
return self.global_namespace, var
def get_operation_namespace_symbol(self, var: str): error_info = f"The name '{var}' is not defined."
"""Get operation namespace and symbol.""" return None, var, error_info
ops_info = (trope_ns, var)
logger.debug("get operation ops info = %r", ops_info)
return ops_info
def get_ast_namespace_symbol(self, obj):
"""Get obj type and namespace and symbol."""
# step 1:get symbol from object map
ops_info = parse_object_map.get(type(obj), SYMBOL_UNDEFINE)
logger.debug("ops info = %r", ops_info)
return ops_info
def analyze_super(self, class_type_node, subclass_instance): def analyze_super(self, class_type_node, subclass_instance):
"""Analyze super and return a class instance.""" """Analyze super and return a class instance."""

View File

@ -139,7 +139,7 @@ AnfNodePtr FunctionBlock::MakeResolveAstOp(const py::object &op) {
auto ast = parser_.ast(); auto ast = parser_.ast();
MS_EXCEPTION_IF_NULL(ast); MS_EXCEPTION_IF_NULL(ast);
TraceGuard trace_guard(parser_.GetLocation(op)); TraceGuard trace_guard(parser_.GetLocation(op));
py::tuple namespace_var = ast->CallParserObjMethod(PYTHON_PARSE_GET_AST_NAMESPACE_SYMBOL, op); py::tuple namespace_var = ast->CallParseModFunction(PYTHON_PARSE_GET_AST_NAMESPACE_SYMBOL, op);
if (namespace_var.size() != 2) { if (namespace_var.size() != 2) {
MS_LOG(EXCEPTION) << "Resolve ast op failed, get namespace tuple size=" << namespace_var.size(); MS_LOG(EXCEPTION) << "Resolve ast op failed, get namespace tuple size=" << namespace_var.size();
} }
@ -170,6 +170,9 @@ AnfNodePtr FunctionBlock::MakeResolveSymbol(const std::string &value) {
} }
py::tuple namespace_var = parser_.ast()->CallParserObjMethod(PYTHON_PARSE_GET_NAMESPACE_SYMBOL, value); py::tuple namespace_var = parser_.ast()->CallParserObjMethod(PYTHON_PARSE_GET_NAMESPACE_SYMBOL, value);
if (namespace_var[0].is_none()) { if (namespace_var[0].is_none()) {
if (namespace_var.size() >= 3) {
MS_EXCEPTION(NameError) << namespace_var[2].cast<std::string>();
}
MS_EXCEPTION(NameError) << "The name \'" << value << "\' is not defined."; MS_EXCEPTION(NameError) << "The name \'" << value << "\' is not defined.";
} }
@ -179,7 +182,7 @@ AnfNodePtr FunctionBlock::MakeResolveSymbol(const std::string &value) {
} }
AnfNodePtr FunctionBlock::MakeResolveOperation(const std::string &value) { AnfNodePtr FunctionBlock::MakeResolveOperation(const std::string &value) {
py::tuple namespace_var = parser_.ast()->CallParserObjMethod(PYTHON_PARSE_GET_OPERATION_NAMESPACE_SYMBOL, value); py::tuple namespace_var = parser_.ast()->CallParseModFunction(PYTHON_PARSE_GET_OPERATION_NAMESPACE_SYMBOL, value);
NameSpacePtr name_space = std::make_shared<NameSpace>(RESOLVE_NAMESPACE_NAME_COMMON_OPS, namespace_var[0]); NameSpacePtr name_space = std::make_shared<NameSpace>(RESOLVE_NAMESPACE_NAME_COMMON_OPS, namespace_var[0]);
SymbolPtr symbol = std::make_shared<Symbol>(namespace_var[1].cast<std::string>()); SymbolPtr symbol = std::make_shared<Symbol>(namespace_var[1].cast<std::string>());
return MakeResolve(name_space, symbol); return MakeResolve(name_space, symbol);

View File

@ -619,7 +619,7 @@ AnfNodePtr Parser::ParseSuper(const FunctionBlockPtr &block, const py::list &arg
father_class = py::none(); father_class = py::none();
} else if (args.size() == 2) { } else if (args.size() == 2) {
father_class = args[0]; father_class = args[0];
auto arg_type = AstSubType(py::cast<int32_t>(ast_->CallParserObjMethod(PYTHON_PARSE_GET_AST_TYPE, args[1]))); auto arg_type = AstSubType(py::cast<int32_t>(ast_->CallParseModFunction(PYTHON_PARSE_GET_AST_TYPE, args[1])));
if (arg_type != AST_SUB_TYPE_NAME || py::cast<std::string>(python_adapter::GetPyObjAttr(args[1], "id")) != "self") { if (arg_type != AST_SUB_TYPE_NAME || py::cast<std::string>(python_adapter::GetPyObjAttr(args[1], "id")) != "self") {
MS_EXCEPTION(ArgumentError) << "When call 'super', the second arg should be 'self'."; MS_EXCEPTION(ArgumentError) << "When call 'super', the second arg should be 'self'.";
} }
@ -641,7 +641,7 @@ AnfNodePtr Parser::ParseCall(const FunctionBlockPtr &block, const py::object &no
py::list args = python_adapter::GetPyObjAttr(node, "args"); py::list args = python_adapter::GetPyObjAttr(node, "args");
auto arg_type = auto arg_type =
AstSubType(py::cast<int32_t>(ast_->CallParserObjMethod(PYTHON_PARSE_GET_AST_TYPE, function_ast_node))); AstSubType(py::cast<int32_t>(ast_->CallParseModFunction(PYTHON_PARSE_GET_AST_TYPE, function_ast_node)));
if (arg_type == AST_SUB_TYPE_NAME) { if (arg_type == AST_SUB_TYPE_NAME) {
auto name_id = py::cast<std::string>(python_adapter::GetPyObjAttr(function_ast_node, "id")); auto name_id = py::cast<std::string>(python_adapter::GetPyObjAttr(function_ast_node, "id"));
if (name_id == "super") { if (name_id == "super") {
@ -694,7 +694,7 @@ bool Parser::ParseArgsInCall(const FunctionBlockPtr &block, const py::list &args
std::vector<AnfNodePtr> *packed_arguments, std::vector<AnfNodePtr> *group_arguments) { std::vector<AnfNodePtr> *packed_arguments, std::vector<AnfNodePtr> *group_arguments) {
bool need_unpack = false; bool need_unpack = false;
for (size_t i = 0; i < args.size(); i++) { for (size_t i = 0; i < args.size(); i++) {
auto arg_node = AstSubType(py::cast<int32_t>(ast_->CallParserObjMethod(PYTHON_PARSE_GET_AST_TYPE, args[i]))); auto arg_node = AstSubType(py::cast<int32_t>(ast_->CallParseModFunction(PYTHON_PARSE_GET_AST_TYPE, args[i])));
if (arg_node == AST_SUB_TYPE_STARRED) { if (arg_node == AST_SUB_TYPE_STARRED) {
if (!group_arguments->empty()) { if (!group_arguments->empty()) {
packed_arguments->push_back(GenerateMakeTuple(block, *group_arguments)); packed_arguments->push_back(GenerateMakeTuple(block, *group_arguments));
@ -1058,7 +1058,7 @@ FunctionBlockPtr Parser::ParseAugAssign(const FunctionBlockPtr &block, const py:
AnfNodePtr target_node = nullptr; AnfNodePtr target_node = nullptr;
AnfNodePtr op_node = block->MakeResolveAstOp(op_obj); AnfNodePtr op_node = block->MakeResolveAstOp(op_obj);
AnfNodePtr value_node = ParseExprNode(block, value_obj); AnfNodePtr value_node = ParseExprNode(block, value_obj);
auto ast_type = AstSubType(py::cast<int32_t>(ast_->CallParserObjMethod(PYTHON_PARSE_GET_AST_TYPE, target_obj))); auto ast_type = AstSubType(py::cast<int32_t>(ast_->CallParseModFunction(PYTHON_PARSE_GET_AST_TYPE, target_obj)));
if (ast_type == AST_SUB_TYPE_NAME) { if (ast_type == AST_SUB_TYPE_NAME) {
target_node = ParseName(block, target_obj); target_node = ParseName(block, target_obj);
@ -1575,7 +1575,7 @@ void Parser::HandleAssignSubscript(const FunctionBlockPtr &block, const py::obje
block->WriteVariable(var_name, setitem_app); block->WriteVariable(var_name, setitem_app);
return; return;
} }
if (AstSubType(py::cast<int32_t>(ast_->CallParserObjMethod(PYTHON_PARSE_GET_AST_TYPE, value_obj))) == if (AstSubType(py::cast<int32_t>(ast_->CallParseModFunction(PYTHON_PARSE_GET_AST_TYPE, value_obj))) ==
AST_SUB_TYPE_SUBSCRIPT) { AST_SUB_TYPE_SUBSCRIPT) {
HandleAssignSubscript(block, value_obj, setitem_app); HandleAssignSubscript(block, value_obj, setitem_app);
return; return;
@ -1590,7 +1590,7 @@ void Parser::HandleAssignSubscript(const FunctionBlockPtr &block, const py::obje
void Parser::WriteAssignVars(const FunctionBlockPtr &block, const py::object &targ, const AnfNodePtr &value_node) { void Parser::WriteAssignVars(const FunctionBlockPtr &block, const py::object &targ, const AnfNodePtr &value_node) {
MS_EXCEPTION_IF_NULL(value_node); MS_EXCEPTION_IF_NULL(value_node);
MS_LOG(DEBUG) << "Process WriteAssignVars"; MS_LOG(DEBUG) << "Process WriteAssignVars";
auto ast_type = AstSubType(py::cast<int32_t>(ast_->CallParserObjMethod(PYTHON_PARSE_GET_AST_TYPE, targ))); auto ast_type = AstSubType(py::cast<int32_t>(ast_->CallParseModFunction(PYTHON_PARSE_GET_AST_TYPE, targ)));
if (ast_type == AST_SUB_TYPE_NAME) { if (ast_type == AST_SUB_TYPE_NAME) {
HandleAssignName(block, targ, value_node); HandleAssignName(block, targ, value_node);
} else if (ast_type == AST_SUB_TYPE_TUPLE) { } else if (ast_type == AST_SUB_TYPE_TUPLE) {
@ -1778,17 +1778,17 @@ py::object ParseAst::GetAstNode() {
} }
py::list ParseAst::GetArgs(const py::object &func_node) { py::list ParseAst::GetArgs(const py::object &func_node) {
py::list ret = python_adapter::CallPyObjMethod(parser_, PYTHON_PARSE_GET_ARGS, func_node); py::list ret = python_adapter::CallPyModFn(module_, PYTHON_PARSE_GET_ARGS, func_node);
return ret; return ret;
} }
py::list ParseAst::GetArgsDefaultValues(const py::object &func_node) { py::list ParseAst::GetArgsDefaultValues(const py::object &func_node) {
py::list ret = python_adapter::CallPyObjMethod(parser_, PYTHON_PARSE_GET_ARGS_DEFAULT_VALUES, func_node); py::list ret = python_adapter::CallPyModFn(module_, PYTHON_PARSE_GET_ARGS_DEFAULT_VALUES, func_node);
return ret; return ret;
} }
AstNodeTypePtr ParseAst::GetNodeType(const py::object &node) { AstNodeTypePtr ParseAst::GetNodeType(const py::object &node) {
py::list list_value = python_adapter::CallPyObjMethod(parser_, PYTHON_PARSE_GET_NODE_TYPE, node); py::list list_value = python_adapter::CallPyModFn(module_, PYTHON_PARSE_GET_NODE_TYPE, node);
if (list_value.size() < 2) { if (list_value.size() < 2) {
MS_LOG(ERROR) << "The node of python method must has 2 values."; MS_LOG(ERROR) << "The node of python method must has 2 values.";
return nullptr; return nullptr;
@ -1799,7 +1799,7 @@ AstNodeTypePtr ParseAst::GetNodeType(const py::object &node) {
} }
AstSubType ParseAst::GetOpType(const py::object &node) { AstSubType ParseAst::GetOpType(const py::object &node) {
auto op_type = AstSubType(python_adapter::CallPyObjMethod(parser_, PYTHON_PARSE_GET_AST_TYPE, node).cast<int32_t>()); auto op_type = AstSubType(python_adapter::CallPyModFn(module_, PYTHON_PARSE_GET_AST_TYPE, node).cast<int32_t>());
return op_type; return op_type;
} }

View File

@ -12,41 +12,34 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================ # ============================================================================
""" test use undefined var""" """ test use undefined name or unsupported builtin function"""
import numpy as np
import pytest import pytest
import mindspore.nn as nn import mindspore.nn as nn
from mindspore import Tensor from mindspore import Tensor
from mindspore import context from mindspore import context
from mindspore import dtype as mstype
context.set_context(mode=context.GRAPH_MODE) context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
def test_use_undefined_var(): def test_use_undefined_name():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.value = [11, 22, 33, 44]
def construct(self, x): def construct(self, x):
ret = x + a ret = x + a
return ret return ret
net = Net() net = Net()
with pytest.raises(NameError) as err: with pytest.raises(NameError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "The name 'a' is not defined" in str(err.value) assert "The name 'a' is not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(33)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(29)" in \
str(err.value)
assert "ret = x + a" in str(err.value) assert "ret = x + a" in str(err.value)
def test_insert_undefined_var(): def test_insert_undefined_name():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.value = [11, 22, 33, 44]
def construct(self, x): def construct(self, x):
b b
ret = x + x ret = x + x
@ -54,17 +47,14 @@ def test_insert_undefined_var():
net = Net() net = Net()
with pytest.raises(NameError) as err: with pytest.raises(NameError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "The name 'b' is not defined" in str(err.value) assert "The name 'b' is not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(51)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(44)" in \
str(err.value)
def test_insert_undefined_var_compute(): def test_insert_undefined_name_compute():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.value = [11, 22, 33, 44]
def construct(self, x): def construct(self, x):
c + x c + x
ret = x + x ret = x + x
@ -72,18 +62,15 @@ def test_insert_undefined_var_compute():
net = Net() net = Net()
with pytest.raises(NameError) as err: with pytest.raises(NameError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "The name 'c' is not defined" in str(err.value) assert "The name 'c' is not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(69)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(59)" in \
str(err.value)
assert "c + x" in str(err.value) assert "c + x" in str(err.value)
def test_insert_undefined_var_in_if(): def test_insert_undefined_name_in_if():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.value = [11, 22, 33, 44]
def construct(self, x): def construct(self, x):
if x > 0: if x > 0:
i i
@ -92,17 +79,14 @@ def test_insert_undefined_var_in_if():
net = Net() net = Net()
with pytest.raises(NameError) as err: with pytest.raises(NameError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "The name 'i' is not defined" in str(err.value) assert "The name 'i' is not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(89)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(76)" in \
str(err.value)
def test_insert_undefined_var_in_while_inner_if(): def test_insert_undefined_name_in_while_inner_if():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.value = [11, 22, 33, 44]
def construct(self, x): def construct(self, x):
while x > 0: while x > 0:
if x > 1: if x > 1:
@ -113,17 +97,14 @@ def test_insert_undefined_var_in_while_inner_if():
net = Net() net = Net()
with pytest.raises(NameError) as err: with pytest.raises(NameError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "The name 'j' is not defined" in str(err.value) assert "The name 'j' is not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(109)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(93)" in \
str(err.value)
def test_insert_undefined_var_compute__in_while_inner_if(): def test_insert_undefined_name_compute__in_while_inner_if():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.value = [11, 22, 33, 44]
def construct(self, x): def construct(self, x):
while x > 0: while x > 0:
if x > 1: if x > 1:
@ -134,13 +115,14 @@ def test_insert_undefined_var_compute__in_while_inner_if():
net = Net() net = Net()
with pytest.raises(NameError) as err: with pytest.raises(NameError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "The name 'p' is not defined" in str(err.value) assert "The name 'p' is not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(130)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(111)" in \
str(err.value)
assert "p + x" in str(err.value) assert "p + x" in str(err.value)
def test_insert_undefined_var_compute__in_for_inner_if(): def test_insert_undefined_name_compute__in_if_in_for():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self): def __init__(self):
super(Net, self).__init__() super(Net, self).__init__()
@ -156,13 +138,14 @@ def test_insert_undefined_var_compute__in_for_inner_if():
net = Net() net = Net()
with pytest.raises(NameError) as err: with pytest.raises(NameError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "The name 'w' is not defined" in str(err.value) assert "The name 'w' is not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(152)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(134)" in \
str(err.value)
assert "w" in str(err.value) assert "w" in str(err.value)
def test_use_undefined_var_for_inner_if(): def test_use_undefined_name_for_inner_if():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self): def __init__(self):
super(Net, self).__init__() super(Net, self).__init__()
@ -177,13 +160,14 @@ def test_use_undefined_var_for_inner_if():
net = Net() net = Net()
with pytest.raises(NameError) as err: with pytest.raises(NameError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "The name 'y' is not defined" in str(err.value) assert "The name 'y' is not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(174)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(157)" in \
str(err.value)
assert "y" in str(err.value) assert "y" in str(err.value)
def test_use_undefined_var_in_for(): def test_use_undefined_name_in_for():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self): def __init__(self):
super(Net, self).__init__() super(Net, self).__init__()
@ -196,13 +180,14 @@ def test_use_undefined_var_in_for():
net = Net() net = Net()
with pytest.raises(NameError) as err: with pytest.raises(NameError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "The name 'd' is not defined" in str(err.value) assert "The name 'd' is not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(194)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(178)" in \
str(err.value)
assert "x = x + d + i" in str(err.value) assert "x = x + d + i" in str(err.value)
def test_insert_undefined_var_in_for(): def test_insert_undefined_name_in_for():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self): def __init__(self):
super(Net, self).__init__() super(Net, self).__init__()
@ -216,13 +201,14 @@ def test_insert_undefined_var_in_for():
net = Net() net = Net()
with pytest.raises(NameError) as err: with pytest.raises(NameError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "The name 'e' is not defined" in str(err.value) assert "The name 'e' is not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(213)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(198)" in \
str(err.value)
assert "e" in str(err.value) assert "e" in str(err.value)
def test_insert_undefined_var_compute_in_for(): def test_insert_undefined_name_compute_in_for():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self): def __init__(self):
super(Net, self).__init__() super(Net, self).__init__()
@ -236,17 +222,15 @@ def test_insert_undefined_var_compute_in_for():
net = Net() net = Net()
with pytest.raises(NameError) as err: with pytest.raises(NameError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "The name 'f' is not defined" in str(err.value) assert "The name 'f' is not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(233)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(219)" in \
str(err.value)
assert "f + i" in str(err.value) assert "f + i" in str(err.value)
def test_use_undefined_var_in_while(): def test_use_undefined_name_in_while():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
def construct(self, x): def construct(self, x):
while x < 0: while x < 0:
x = x - g x = x - g
@ -254,18 +238,15 @@ def test_use_undefined_var_in_while():
net = Net() net = Net()
with pytest.raises(NameError) as err: with pytest.raises(NameError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "The name 'g' is not defined" in str(err.value) assert "The name 'g' is not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(252)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(236)" in \
str(err.value)
assert "x = x - g" in str(err.value) assert "x = x - g" in str(err.value)
def test_insert_undefined_var_in_while(): def test_insert_undefined_name_in_while():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.value = [11, 22, 33, 44]
def construct(self, x): def construct(self, x):
while x < 0: while x < 0:
h h
@ -274,18 +255,15 @@ def test_insert_undefined_var_in_while():
net = Net() net = Net()
with pytest.raises(NameError) as err: with pytest.raises(NameError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "The name 'h' is not defined" in str(err.value) assert "The name 'h' is not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(271)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(252)" in \
str(err.value)
assert "h" in str(err.value) assert "h" in str(err.value)
def test_insert_undefined_var_compute_while(): def test_insert_undefined_name_compute_while():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.value = [11, 22, 33, 44]
def construct(self, x): def construct(self, x):
while x < 0: while x < 0:
x + i x + i
@ -294,9 +272,10 @@ def test_insert_undefined_var_compute_while():
net = Net() net = Net()
with pytest.raises(NameError) as err: with pytest.raises(NameError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "The name 'i' is not defined" in str(err.value) assert "The name 'i' is not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(291)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(269)" in \
str(err.value)
assert "x + i" in str(err.value) assert "x + i" in str(err.value)
@ -314,9 +293,10 @@ def test_call_none_in_if():
net = Net() net = Net()
with pytest.raises(RuntimeError) as err: with pytest.raises(RuntimeError) as err:
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
assert "Not AbstractFunction: AbstractNone(Value: None)" in str(err.value) assert "Not AbstractFunction: AbstractNone(Value: None)" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_var.py(312)" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(291)" in \
str(err.value)
assert "ret = self.func(x)" in str(err.value) assert "ret = self.func(x)" in str(err.value)
@ -332,7 +312,7 @@ def test_insert_defined_var():
return ret return ret
net = Net() net = Net()
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
def test_insert_defined_var_compute(): def test_insert_defined_var_compute():
@ -347,4 +327,48 @@ def test_insert_defined_var_compute():
return ret return ret
net = Net() net = Net()
net(Tensor(np.arange(4))) net(Tensor([1, 2, 3], mstype.float32))
def test_call_unsupported_builtin_function_in_while():
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
def construct(self, x, y):
ret = 0
number = 5
while number > 0:
ret = divmod(x, y)
number -= 1
return ret
net = Net()
with pytest.raises(NameError) as err:
net(Tensor([1, 2, 3]), Tensor([4, 5, 6]))
assert "The builtin function 'divmod' is not supported in graph mode" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(342)" in str(
err.value)
assert "ret = divmod(x, y)" in str(err.value)
def test_call_unsupported_builtin_function_in_if_in_for():
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.value = [11, 22, 33, 44]
def construct(self, x):
for i in self.value:
if x > 1:
x = divmod(x, i)
ret = x + x
return ret
net = Net()
with pytest.raises(NameError) as err:
net(Tensor([1, 2, 3], mstype.float32))
assert "The builtin function 'divmod' is not supported in graph mode" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(364)" in \
str(err.value)
assert "x = divmod(x, i)" in str(err.value)