This commit is contained in:
huanghui 2021-05-28 17:28:13 +08:00
parent 09b6b35ad3
commit 0610a8d99c
15 changed files with 51 additions and 47 deletions

View File

@ -165,7 +165,7 @@ def resolve_symbol(namespace, symbol):
resolve_ = convert_object_map.get(resolve_)
logger.debug("convert resolve = %r", resolve_)
if resolve_ == NO_IMPLEMENT:
raise NotImplementedError("not implemented for ", str(symbol))
raise NotImplementedError(f"Not support for `{symbol}`")
except Exception as e:
if isinstance(e, NotImplementedError):
raise e
@ -249,7 +249,7 @@ def get_obj_type(obj):
else:
# here for ndarray, just print its shape (in case of the array to large and print many data in screen)
is_ndarray = type(obj).__name__ == 'ndarray' and hasattr(obj, 'shape')
raise TypeError(f'Invalid object with type `{type(obj)}` and {"shape" if is_ndarray else "value"} '
raise TypeError(f'Not support for this object with type `{type(obj)}` and {"shape" if is_ndarray else "value"} '
f'`{obj.shape if is_ndarray else obj}`.')
return obj_type

View File

@ -120,13 +120,13 @@ convert_object_map = {
T.iter: M.ms_iter,
T.next: M.ms_next,
T.hasnext: M.hasnext,
T.MakeTuple: F.make_tuple,
T.MakeTuple: F.make_tuple,
T.make_dict: F.make_dict,
T.make_list: F.make_list,
T.make_slice: F.make_slice,
T.range: F.make_range,
T.while_cond: M.while_cond,
# lib function
math.floor: NO_IMPLEMENT,
math.trunc: NO_IMPLEMENT,
@ -137,6 +137,6 @@ convert_object_map = {
math.tan: NO_IMPLEMENT,
# user defined
RowTensor: F.make_row_tensor,
RowTensor: F.make_row_tensor,
SparseTensor: F.make_sparse_tensor,
}

View File

@ -506,7 +506,7 @@ void GetEvalStackInfo(std::ostringstream &oss) {
}
OutputAnalyzedGraphWithType(file_name);
oss << "\nThe function call stack (See file '" << file_name << "' for details):\n";
oss << "\nThe function call stack (See file '" << file_name << "' for more details):\n";
int index = 0;
std::string last_location_info = "";

View File

@ -130,7 +130,7 @@ class ResolverResolveAndGetAttr : public OptimizerCaller {
resolver_optimizers_ = {std::make_shared<ResolverGetAttrResolve>(), std::make_shared<ResolverResolve>(),
std::make_shared<ResolverGetAttr>()};
}
~ResolverResolveAndGetAttr() = default;
virtual ~ResolverResolveAndGetAttr() = default;
AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) override {
AnfNodePtr new_node;

View File

@ -756,13 +756,14 @@ AnfNodePtr Parser::ParseAttribute(const FunctionBlockPtr &block, const py::objec
// Process comparison expression : a == b. a > b etc.
AnfNodePtr Parser::ParseCompare(const FunctionBlockPtr &block, const py::object &node) {
MS_LOG(DEBUG) << "Process ast Compare";
TraceGuard guard(GetLocation(node));
// For python comparison ,there may be if x>y>5 ,
// Which there is two ops , but we only support one now
py::list ops = python_adapter::GetPyObjAttr(node, "ops");
if (ops.size() > MAX_COMPARISON_OPS_SUPPORTED) {
MS_LOG(ERROR) << "MindSpore does not support comparison with operators more than one now, ops size =" << ops.size();
return nullptr;
MS_EXCEPTION(NotSupportError)
<< "MindSpore does not support comparison with operators more than one now, ops size =" << ops.size();
}
py::object left = python_adapter::GetPyObjAttr(node, "left");
@ -1500,14 +1501,17 @@ void Parser::HandleAssignClassMember(const FunctionBlockPtr &block, const py::ob
// Now only support the self.xxx = yyy, where self.xxx must be a defined Parameter type
if (!py::hasattr(ast()->obj(), common::SafeCStr(attr_name))) {
MS_EXCEPTION(TypeError) << "'" << var_name << "' should be a Parameter, but not defined.";
MS_EXCEPTION(TypeError) << "'" << var_name << "' should be defined in the class '__init__' function. \n\n"
<< trace::GetDebugInfo(target_node->debug_info());
}
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 '"
MS_EXCEPTION(TypeError) << "'" << var_name
<< "' should be defined with a Parameter type in the class '__init__' function, but got '"
<< py::str(obj).cast<std::string>() << "' with type '"
<< py::str(obj_type).cast<std::string>() << ".";
<< py::str(obj_type).cast<std::string>() << ".\n\n"
<< trace::GetDebugInfo(target_node->debug_info());
}
MS_EXCEPTION_IF_NULL(block);
@ -1530,14 +1534,17 @@ void Parser::HandleAssignSubscript(const FunctionBlockPtr &block, const py::obje
auto 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.";
MS_EXCEPTION(TypeError) << "'" << var_name << "' was not defined in the class '__init__' function.\n\n"
<< trace::GetDebugInfo(value_node->debug_info());
}
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 '"
MS_EXCEPTION(TypeError) << "'" << var_name
<< "' should be defined with a Parameter in the class '__init__' function, but got '"
<< py::str(obj).cast<std::string>() << "' with type '"
<< py::str(obj_type).cast<std::string>() << "'.";
<< py::str(obj_type).cast<std::string>() << "'.\n\n"
<< trace::GetDebugInfo(value_node->debug_info());
}
block->WriteVariable(var_name, setitem_app);
return;
@ -1548,7 +1555,8 @@ void Parser::HandleAssignSubscript(const FunctionBlockPtr &block, const py::obje
return;
}
if (!py::hasattr(value_obj, "id")) {
MS_EXCEPTION(TypeError) << "Attribute id not found in " << py::str(value_obj).cast<std::string>();
MS_EXCEPTION(TypeError) << "Attribute id not found in " << py::str(value_obj).cast<std::string>() << "\n\n"
<< trace::GetDebugInfo(value_node->debug_info());
}
var_name = value_obj.attr("id").cast<std::string>();
block->WriteVariable(var_name, setitem_app);
@ -1567,11 +1575,11 @@ void Parser::WriteAssignVars(const FunctionBlockPtr &block, const py::object &ta
} else if (ast_->IsClassMember(targ)) {
HandleAssignClassMember(block, targ, value_node);
} else if (ast_type == AST_SUB_TYPE_ATTRIBUTE) {
MS_LOG(EXCEPTION) << "The subnet attributes cannot be changed in the network"
<< " NodeInfo: " << trace::GetDebugInfo(value_node->debug_info());
MS_LOG(EXCEPTION) << "The subnet attributes cannot be changed in the network. \n\n"
<< trace::GetDebugInfo(value_node->debug_info());
} else {
MS_LOG(EXCEPTION) << "Not supported assign type: " << ast_type
<< " NodeInfo: " << trace::GetDebugInfo(value_node->debug_info());
MS_LOG(EXCEPTION) << "Not support this assign type: " << ast_type << "\n\n"
<< trace::GetDebugInfo(value_node->debug_info());
}
}
@ -1855,6 +1863,7 @@ FuncGraphPtr MakeTopGraph(const py::object &cell, const ValuePtr &cell_ptr) {
MS_LOG(EXCEPTION) << "Current graph cast failed from " << cell_ptr->ToString();
}
TraceGuard guard(current_graph->debug_info()->location());
auto func_graph = std::make_shared<FuncGraph>();
func_graph->debug_info()->set_name(current_graph->debug_info()->name() + "_wrapper");

View File

@ -207,9 +207,9 @@ EvalResultPtr BaseFuncGraphEvaluator::Eval(AnalysisEnginePtr engine, const Abstr
MS_EXCEPTION_IF_NULL(fg);
std::size_t nargs = fg->parameters().size();
if (args_abs_list.size() != nargs) {
MS_EXCEPTION(TypeError) << "Function " << fg->ToString() << ", The number of parameters of this function is "
MS_EXCEPTION(TypeError) << "For function " << fg->ToString() << ", the number of parameters of this function is "
<< fg->parameters().size() << ", but the number of provided arguments is "
<< args_abs_list.size() << ". NodeInfo: " << trace::GetDebugInfo(fg->debug_info());
<< args_abs_list.size() << ".";
}
MS_EXCEPTION_IF_NULL(parent_context_);
context_ = parent_context_->NewFuncGraphContext(fg, args_abs_list);

View File

@ -916,7 +916,8 @@ EvalResultPtr GetEvaluatedValueForBuiltinTypeAttrOrMethod(const AnalysisEnginePt
if (require.empty()) {
require = pipeline::Resource::GetAttrPtr(data_type->type_id(), item_name);
if (require.empty()) {
MS_LOG(EXCEPTION) << "The object of type: " << data_type->ToString() << " has no method or attr: " << item_name;
MS_LOG(EXCEPTION) << "Mindspore don't support this usage '" << item_name << "' for the object with type <"
<< data_type->ToString() << ">.";
}
require_type = REQUIRE_TYPE::ATTR;
}
@ -1153,8 +1154,8 @@ class CreateInstanceEvaluator : public TransitionPrimEvaluator {
// Create class instance.
auto obj = parse::data_converter::CreatePythonObject(class_type, params);
if (py::isinstance<py::none>(obj)) {
MS_LOG(EXCEPTION) << "Create python object" << py::str(class_type)
<< " failed, only support create Cell or Primitive object.";
MS_LOG(EXCEPTION) << "Create python object `" << py::str(class_type)
<< "` failed, only support create Cell or Primitive object.";
}
// Process the object.

View File

@ -283,8 +283,7 @@ EvalResultPtr AnalysisEngine::EvalCNode(const CNodePtr &cnode, const AnfNodeConf
}
AbstractFunctionPtr func = dyn_cast<AbstractFunction>(maybe_func);
if (func == nullptr) {
MS_LOG(EXCEPTION) << "Not AbstractFunction: " << maybe_func->ToString()
<< ", NodeInfo: " << trace::GetDebugInfo(cnode->debug_info());
MS_LOG(EXCEPTION) << "Not AbstractFunction: " << maybe_func->ToString() << ".";
}
ConfigPtrList args_conf_list;

View File

@ -85,7 +85,7 @@ std::string AbstractBase::ToString() const {
MS_EXCEPTION_IF_NULL(type_);
MS_EXCEPTION_IF_NULL(shape_);
buffer << type_name() << "("
<< "Type: " << type_->ToString() << " Value: " << value << " Shape: " << shape_->ToString() << ")";
<< "Type: " << type_->ToString() << ", Value: " << value << ", Shape: " << shape_->ToString() << ")";
return buffer.str();
}

View File

@ -63,7 +63,7 @@ AbstractBasePtr InferImplSwitch(const AnalysisEnginePtr &, const PrimitivePtr &p
}
}
MS_LOG(EXCEPTION) << "Invalid condition value for switch " << cond->ToString();
MS_LOG(EXCEPTION) << "Not support this condition value: " << cond->GetValueTrack()->ToString();
}
AbstractBasePtr InferImplSwitchLayer(const AnalysisEnginePtr &, const PrimitivePtr &primitive,
@ -130,8 +130,8 @@ AbstractBasePtr InferImplIs_(const AnalysisEnginePtr &, const PrimitivePtr &prim
CheckArgsSize(op_name, args_spec_list, 2);
ValuePtr t = args_spec_list[1]->BuildValue();
if (!SupportedIsTargetValue(t)) {
MS_LOG(EXCEPTION) << "Not supported type:" << t->ToString()
<< " for statement is, supported list is:None, False, True ";
MS_LOG(EXCEPTION) << "This comparator '" << t->ToString()
<< "' is not supported. For statement 'is', only support compare with 'None', 'False' or 'True'";
}
ValuePtr x = args_spec_list[0]->BuildValue();
@ -146,8 +146,9 @@ AbstractBasePtr InferImplIsNot(const AnalysisEnginePtr &, const PrimitivePtr &pr
CheckArgsSize(op_name, args_spec_list, 2);
ValuePtr t = args_spec_list[1]->BuildValue();
if (!SupportedIsTargetValue(t)) {
MS_LOG(EXCEPTION) << "Not supported type:" << t->ToString()
<< " for statement is not, supported list is:None, False, True ";
MS_LOG(EXCEPTION)
<< "This comparator '" << t->ToString()
<< "' is not supported. For statement 'is not' , only support compare with 'None', 'False' or 'True'";
}
ValuePtr x = args_spec_list[0]->BuildValue();

View File

@ -32,7 +32,7 @@ def _valid_index(cell_num, index):
def _valid_cell(cell):
if issubclass(cell.__class__, Cell):
return True
raise TypeError('Cell {} is not subclass of Cell'.format(cell))
raise TypeError('`{}` is not a subclass of Cell. Please check your code'.format(cell))
def _get_prefix_and_index(cells):

View File

@ -90,9 +90,8 @@ def test_dict_set_or_get_item():
return ret
net = DictNet()
with pytest.raises(TypeError) as ex:
with pytest.raises(TypeError):
net()
assert "'self.dict_' should be a Parameter" in str(ex.value)
def test_dict_set_or_get_item_2():
@ -138,9 +137,8 @@ def test_dict_set_or_get_item_3():
return self.dict_["x"]
net = DictNet()
with pytest.raises(TypeError) as ex:
with pytest.raises(TypeError):
net()
assert "'self.dict_' should be a Parameter" in str(ex.value)
def test_dict_set_item():

View File

@ -142,9 +142,8 @@ def test_class_member_list_append():
y = Tensor(np.zeros([3, 4, 5], np.int32))
z = [[1, 2], 3]
net = Net(z)
with pytest.raises(TypeError) as ex:
with pytest.raises(TypeError):
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():
@ -180,9 +179,8 @@ def test_change_list_element():
y = Tensor(np.zeros([3, 4, 5], np.int32))
z = [[1, 2], 3]
net = Net(z)
with pytest.raises(TypeError) as ex:
with pytest.raises(TypeError):
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):

View File

@ -71,9 +71,8 @@ def test_type_not_have_the_attr():
net = Net()
x = Tensor(np.ones([1, 2, 3], np.int32))
with pytest.raises(RuntimeError) as ex:
with pytest.raises(RuntimeError):
net(x)
assert "The object of type: Tensor[Int32] has no method or attr: shapes" in str(ex.value)
def test_type_not_have_the_method():
@ -87,6 +86,5 @@ def test_type_not_have_the_method():
net = Net()
x = Tensor(np.ones([1, 2, 3], np.int32))
with pytest.raises(RuntimeError) as ex:
with pytest.raises(RuntimeError):
net(x)
assert "The object of type: Tensor[Int32] has no method or attr: dtypes" in str(ex.value)

View File

@ -394,4 +394,4 @@ def test_use_defined_class_obj_in_for():
net = Net()
with pytest.raises(TypeError) as err:
net(Tensor([1, 2, 3], mstype.float32))
assert "Invalid object with type" in str(err.value)
assert "Not support for this object with type" in str(err.value)