!26496 clean code, fix cyclomatic complexity problems

Merge pull request !26496 from huangbingjian/clean_1.5
This commit is contained in:
i-robot 2021-11-23 14:36:33 +00:00 committed by Gitee
commit 76a2a6290c
11 changed files with 206 additions and 182 deletions

View File

@ -340,49 +340,42 @@ std::string AnfExporter::GetOtherValueText(const FuncGraphPtr &, const ValuePtr
return oss.str();
}
static bool CanUseDumpText(const ValuePtr &value) {
return (value->isa<RefKey>() || value->isa<Scalar>() || value->isa<StringImm>() || value->isa<tensor::Tensor>() ||
value->isa<parse::Symbol>() || value->isa<None>() || value->isa<Null>() || value->isa<ValueSlice>() ||
value->isa<Type>() || value->isa<KeywordArg>());
}
std::string AnfExporter::GetValueText(const FuncGraphPtr &func_graph, const ValuePtr &value) {
std::ostringstream oss;
bool is_null_ptr = (func_graph == nullptr || value == nullptr);
if (is_null_ptr) {
return oss.str();
if (func_graph == nullptr || value == nullptr) {
return "";
}
if (value->isa<Primitive>()) {
oss << GetPrimitiveText(value->cast<PrimitivePtr>());
} else if (value->isa<MetaFuncGraph>()) {
MetaFuncGraphPtr meta_func_graph = value->cast<MetaFuncGraphPtr>();
oss << GetMetaFuncGraphText(meta_func_graph);
} else if (value->isa<SymbolicKeyInstance>()) {
oss << GetSymbolicKeyInstanceText(func_graph, value->cast<SymbolicKeyInstancePtr>());
} else if (value->isa<RefKey>()) {
oss << value->DumpText();
} else if (value->isa<Scalar>() || value->isa<StringImm>()) {
oss << value->DumpText();
} else if (value->isa<tensor::Tensor>()) {
oss << value->DumpText();
} else if (value->isa<parse::Symbol>() || value->isa<None>() || value->isa<Null>()) {
oss << value->DumpText();
} else if (value->isa<ValueSequeue>()) {
oss << GetSequenceText(func_graph, value);
} else if (value->isa<ValueDictionary>()) {
oss << GetDictText(func_graph, value);
} else if (value->isa<ValueSlice>()) {
ValueSlicePtr slice = value->cast<ValueSlicePtr>();
oss << slice->DumpText();
} else if (value->isa<Type>()) {
oss << value->DumpText();
} else if (value->isa<parse::NameSpace>()) {
oss << GetNameSpaceText(value->cast<parse::NameSpacePtr>());
} else if (value->isa<parse::PyObjectWrapper>()) {
oss << value->type_name();
} else if (value->isa<KeywordArg>()) {
KeywordArgPtr keyword_arg = value->cast<KeywordArgPtr>();
oss << keyword_arg->DumpText();
} else {
return GetOtherValueText(func_graph, value);
return GetPrimitiveText(value->cast<PrimitivePtr>());
}
return oss.str();
if (value->isa<MetaFuncGraph>()) {
MetaFuncGraphPtr meta_func_graph = value->cast<MetaFuncGraphPtr>();
return GetMetaFuncGraphText(meta_func_graph);
}
if (value->isa<SymbolicKeyInstance>()) {
return GetSymbolicKeyInstanceText(func_graph, value->cast<SymbolicKeyInstancePtr>());
}
if (value->isa<ValueSequeue>()) {
return GetSequenceText(func_graph, value);
}
if (value->isa<ValueDictionary>()) {
return GetDictText(func_graph, value);
}
if (value->isa<parse::NameSpace>()) {
return GetNameSpaceText(value->cast<parse::NameSpacePtr>());
}
if (value->isa<parse::PyObjectWrapper>()) {
return value->type_name();
}
if (CanUseDumpText(value)) {
return value->DumpText();
}
return GetOtherValueText(func_graph, value);
}
// This function is used to output node in CNode's inputs

View File

@ -45,6 +45,7 @@ class ProtoExporter {
const std::map<AnfNodePtr, size_t> &apply_map,
std::map<AnfNodePtr, size_t> *const_map_ptr);
void SetValueToProto(const ValuePtr &attr_value, irpb::ValueProto *value_proto);
void SetNumberToProto(const ValuePtr &attr_value, irpb::ValueProto *value_proto);
void SetScalarToProto(const ScalarPtr &val, irpb::ValueProto *value_proto);
void SetSequenceToProto(const ValueSequeuePtr &val, irpb::ValueProto *value_proto);
void SetDictionaryToProto(const ValueDictionaryPtr &val, irpb::ValueProto *value_proto);
@ -122,6 +123,18 @@ void CheckIfValidType(const TypePtr &type) {
}
}
void SetTensorType(const TypePtr &type, const BaseShapePtr &shape, irpb::TypeProto *type_proto) {
TypePtr elem_type = dyn_cast<TensorType>(type)->element();
type_proto->mutable_tensor_type()->set_elem_type(GetNumberDataType(elem_type));
type_proto->set_data_type(irpb::DT_TENSOR);
if (shape != nullptr && shape->isa<abstract::Shape>()) {
abstract::ShapePtr shape_info = dyn_cast<abstract::Shape>(shape);
for (const auto &elem : shape_info->shape()) {
type_proto->mutable_tensor_type()->mutable_shape()->add_dim()->set_size(elem);
}
}
}
void ProtoExporter::SetNodeOutputType(const TypePtr &type, const BaseShapePtr &shape, irpb::TypeProto *type_proto) {
if (type_proto == nullptr) {
return;
@ -134,15 +147,7 @@ void ProtoExporter::SetNodeOutputType(const TypePtr &type, const BaseShapePtr &s
} else if (type->isa<Number>()) {
type_proto->set_data_type(GetNumberDataType(type));
} else if (type->isa<TensorType>()) {
TypePtr elem_type = dyn_cast<TensorType>(type)->element();
type_proto->mutable_tensor_type()->set_elem_type(GetNumberDataType(elem_type));
type_proto->set_data_type(irpb::DT_TENSOR);
if (shape != nullptr && shape->isa<abstract::Shape>()) {
abstract::ShapePtr shape_info = dyn_cast<abstract::Shape>(shape);
for (const auto &elem : shape_info->shape()) {
type_proto->mutable_tensor_type()->mutable_shape()->add_dim()->set_size(elem);
}
}
SetTensorType(type, shape, type_proto);
} else if (type->isa<Tuple>()) {
TuplePtr tuple_type = dyn_cast<Tuple>(type);
type_proto->set_data_type(irpb::DT_TUPLE);
@ -179,18 +184,8 @@ void ProtoExporter::SetNodeOutputType(const AnfNodePtr &node, irpb::TypeProto *t
SetNodeOutputType(node->Type(), node->Shape(), type_proto);
}
void ProtoExporter::SetValueToProto(const ValuePtr &val, irpb::ValueProto *value_proto) {
if (val == nullptr || value_proto == nullptr) {
return;
}
if (val->isa<StringImm>()) {
const StringImmPtr &value = dyn_cast<StringImm>(val);
value_proto->set_dtype(irpb::DT_STRING);
value_proto->set_str_val(value->value());
} else if (val->isa<Scalar>()) {
SetScalarToProto(dyn_cast<Scalar>(val), value_proto);
} else if (val->isa<Bool>()) {
void ProtoExporter::SetNumberToProto(const ValuePtr &val, irpb::ValueProto *value_proto) {
if (val->isa<Bool>()) {
value_proto->set_dtype(irpb::DT_TYPE);
value_proto->mutable_type_val()->set_data_type(irpb::DT_BOOL);
} else if (val->isa<Int>()) {
@ -202,6 +197,24 @@ void ProtoExporter::SetValueToProto(const ValuePtr &val, irpb::ValueProto *value
} else if (val->isa<Float>()) {
value_proto->set_dtype(irpb::DT_TYPE);
value_proto->mutable_type_val()->set_data_type(irpb::DT_BASE_FLOAT);
} else {
MS_LOG(DEBUG) << "Unsupported type " << val->type_name();
}
}
void ProtoExporter::SetValueToProto(const ValuePtr &val, irpb::ValueProto *value_proto) {
if (val == nullptr || value_proto == nullptr) {
return;
}
if (val->isa<Number>()) {
SetNumberToProto(val, value_proto);
} else if (val->isa<StringImm>()) {
const StringImmPtr &value = dyn_cast<StringImm>(val);
value_proto->set_dtype(irpb::DT_STRING);
value_proto->set_str_val(value->value());
} else if (val->isa<Scalar>()) {
SetScalarToProto(dyn_cast<Scalar>(val), value_proto);
} else if (val->isa<ValueSequeue>()) {
SetSequenceToProto(dyn_cast<ValueSequeue>(val), value_proto);
} else if (val->isa<None>()) {

View File

@ -91,6 +91,28 @@ bool GetTensorOrScalarTypeInfo(const TypePtr &arg_type_origin, TypeId *arg_type_
return false;
}
TypeId GetMaxTypeIdForNumber(TypeId max_type_id, bool has_int8, bool has_scalar_int64, bool has_scalar_float32) {
if (max_type_id == kNumberTypeUInt8 && has_int8) {
max_type_id = kNumberTypeInt16;
}
// if bool is the max type, see if there is scalar input
// if so, it means that max is bool tensor, use scalar type instead.
// for example: Tensor([True, True]) * 2, expect result is Tensor([2, 2])
if (max_type_id == kNumberTypeBool) {
if (has_scalar_int64) {
max_type_id = kNumberTypeInt64;
}
if (has_scalar_float32) {
max_type_id = kNumberTypeFloat32;
}
}
if (max_type_id != kNumberTypeFloat16 && max_type_id != kNumberTypeFloat32 && max_type_id != kNumberTypeFloat64 &&
max_type_id != kTypeUnknown && has_scalar_float32) {
max_type_id = kNumberTypeFloat32;
}
return max_type_id;
}
TypeId GetMaxTypeId(const std::vector<TypePtr> &input_types, const std::vector<size_t> &indices) {
TypeId max_type_id = kTypeUnknown;
size_t max_type_number = 0;
@ -126,26 +148,7 @@ TypeId GetMaxTypeId(const std::vector<TypePtr> &input_types, const std::vector<s
SetMaxType(&max_type_id, &max_type_number, arg_type_id, it->second);
}
}
if (max_type_id == kNumberTypeUInt8 && has_int8) {
max_type_id = kNumberTypeInt16;
}
// if bool is the max type, see if there is scalar input
// if so, it means that max is bool tensor, use scalar type instead.
// for example: Tensor([True, True]) * 2, expect result is Tensor([2, 2])
if (max_type_id == kNumberTypeBool) {
if (has_scalar_int64) {
max_type_id = kNumberTypeInt64;
}
if (has_scalar_float32) {
max_type_id = kNumberTypeFloat32;
}
}
if (max_type_id != kNumberTypeFloat16 && max_type_id != kNumberTypeFloat32 && max_type_id != kNumberTypeFloat64 &&
max_type_id != kTypeUnknown && has_scalar_float32) {
max_type_id = kNumberTypeFloat32;
}
return max_type_id;
return GetMaxTypeIdForNumber(max_type_id, has_int8, has_scalar_int64, has_scalar_float32);
}
// Get the largest type of index in the same SignatureEnumDType of arguments.
@ -257,6 +260,18 @@ void CheckSigSize(const size_t &sig_size, const bool &has_var, const AbstractBas
}
}
SignatureEnumRW GetSignatureEnumRW(size_t index, const std::vector<Signature> &signature, bool has_var) {
SignatureEnumRW sig = SignatureEnumRW::kRWDefault;
// If sig_size is 0 use default.
std::size_t sig_size = signature.size();
if (index < sig_size) {
sig = signature[index].rw;
} else if (has_var && index >= sig_size) {
sig = signature[sig_size - 1].rw;
}
return sig;
}
AnfNodePtr BuildNewCNode(const FuncGraphPtr &func_graph, const std::string &func_name, const ValuePtr &function,
const AbstractBasePtrList &args_spec_list, const std::vector<AnfNodePtr> &params_list) {
// args: original inputs
@ -277,14 +292,8 @@ AnfNodePtr BuildNewCNode(const FuncGraphPtr &func_graph, const std::string &func
op_inputs.push_back(param);
continue;
}
SignatureEnumRW sig = SignatureEnumRW::kRWDefault;
// If sig_size is 0 use default.
if (sig_size > 0 && i < sig_size) {
sig = signature[i].rw;
} else if (has_var && i >= sig_size) {
sig = signature[sig_size - 1].rw;
}
SignatureEnumRW sig = GetSignatureEnumRW(i, signature, has_var);
TypePtr type = args_spec_list[i]->BuildType();
if (type && type->isa<RefType>()) {
if (sig == SignatureEnumRW::kRWRead) {

View File

@ -31,7 +31,6 @@
namespace mindspore {
namespace opt {
namespace {
using ParamUserMap = std::unordered_map<std::string, std::vector<size_t>>;
using LoadGraphMap = OrderedMap<std::string, std::vector<size_t>>;

View File

@ -146,9 +146,8 @@ class InlinerBase : public AnfVisitor {
if (IsForceInline(this, fg, node)) {
if (IsUniqueUse(nullptr, fg, nullptr)) {
return InlineMove(node, fg, args, inputs);
} else {
return InlineClone(fg, node->func_graph(), args, inputs[0]->scope());
}
return InlineClone(fg, node->func_graph(), args, inputs[0]->scope());
}
if (IsUniqueUse(nullptr, fg, nullptr)) {

View File

@ -169,24 +169,13 @@ class ChoicePartialEliminater : public AnfVisitor {
return true;
}
// f(x1, x2, x3, z1, z2)
// g(x4, x2, z1, z2)
// h(x5, x2, x7, x8, z1, z2)
// --> anchor_fg = h
// h(x5, x2, x7, x8, x1, x3, x4, z1, z2)
// f(x5, x2, x7, x8, x1, x3, x4, z1, z2)
// g(x5, x2, x7, x8, x1, x3, x4, z1, z2)
// as z1, z2 maybe U or IO monad.
AnfNodePtrList UnifyParameters(const size_t &anchor_index, const AnfNodePtrList &fg_list,
const std::vector<AnfNodePtrList> args_list) {
std::vector<size_t> inputs_index_list[args_list.size()];
size_t extra_input_counter = 0;
AnfNodePtrList extra_inputs;
// Find the new location of the old_inputs except Zs.
size_t FindNewLocation(const std::vector<AnfNodePtrList> &args_list, size_t anchor_index,
std::vector<size_t> *inputs_index_list, AnfNodePtrList *extra_inputs_ptr) {
const auto &anchor_args = args_list[anchor_index];
auto &extra_inputs = *extra_inputs_ptr;
size_t extra_input_counter = 0;
size_t anchor_args_size = anchor_args.size();
auto anchor_fg = GetValueNode<FuncGraphPtr>(fg_list[anchor_index]);
MS_EXCEPTION_IF_NULL(anchor_fg);
// Find the new location of the old_inputs except Zs;
for (size_t i = 0; i < args_list.size(); ++i) {
if (i == anchor_index) {
continue;
@ -217,6 +206,26 @@ class ChoicePartialEliminater : public AnfVisitor {
}
}
}
return extra_input_counter;
}
// f(x1, x2, x3, z1, z2)
// g(x4, x2, z1, z2)
// h(x5, x2, x7, x8, z1, z2)
// --> anchor_fg = h
// h(x5, x2, x7, x8, x1, x3, x4, z1, z2)
// f(x5, x2, x7, x8, x1, x3, x4, z1, z2)
// g(x5, x2, x7, x8, x1, x3, x4, z1, z2)
// as z1, z2 maybe U or IO monad.
AnfNodePtrList UnifyParameters(size_t anchor_index, const AnfNodePtrList &fg_list,
const std::vector<AnfNodePtrList> args_list) {
std::vector<size_t> inputs_index_list[args_list.size()];
AnfNodePtrList extra_inputs;
const auto &anchor_args = args_list[anchor_index];
size_t anchor_args_size = anchor_args.size();
auto anchor_fg = GetValueNode<FuncGraphPtr>(fg_list[anchor_index]);
MS_EXCEPTION_IF_NULL(anchor_fg);
size_t extra_input_counter = FindNewLocation(args_list, anchor_index, inputs_index_list, &extra_inputs);
auto manager = anchor_fg->manager();
MS_EXCEPTION_IF_NULL(manager);

View File

@ -387,6 +387,36 @@ PrimitivePtr GetPrimitiveFromValueNode(const AnfNodePtr &node) {
return value->cast<PrimitivePtr>();
}
static std::string GetNodeTargetForVarInputNode(const CNodePtr &cnode) {
auto &inputs = cnode->inputs();
std::vector<AnfNodePtr> real_inputs;
const size_t update_state_valid_input_index = 2;
const size_t make_tuple_valid_input_index = 1;
if (cnode->IsApply(prim::kPrimUpdateState) && inputs.size() > update_state_valid_input_index) {
(void)std::copy(inputs.begin() + SizeToLong(update_state_valid_input_index), inputs.end(),
std::back_inserter(real_inputs));
} else if (cnode->IsApply(prim::kPrimMakeTuple) && inputs.size() > make_tuple_valid_input_index) {
(void)std::copy(inputs.begin() + SizeToLong(make_tuple_valid_input_index), inputs.end(),
std::back_inserter(real_inputs));
}
std::string first_input_target = kTargetUnDefined;
bool has_diff_target =
std::any_of(std::rbegin(real_inputs), std::rend(real_inputs), [&first_input_target](const AnfNodePtr &n) {
auto target = GetOriginNodeTarget(n);
if (target == kTargetUnDefined) {
return false;
}
if (first_input_target == kTargetUnDefined) {
first_input_target = target;
}
return target != first_input_target;
});
if (!has_diff_target) {
return first_input_target;
}
return kTargetUnDefined;
}
std::string GetVirtualNodeTargetFromInputs(const AnfNodePtr &node) {
MS_EXCEPTION_IF_NULL(node);
auto cnode = node->cast<CNodePtr>();
@ -402,7 +432,7 @@ std::string GetVirtualNodeTargetFromInputs(const AnfNodePtr &node) {
}
#endif
if (IsPrimitiveCNode(node, prim::kPrimDepend) || IsPrimitiveCNode(node, prim::kPrimLoad)) {
const size_t node_inputs_num = 3;
constexpr size_t node_inputs_num = 3;
if (inputs.size() >= node_inputs_num) {
size_t use_index = 1;
if (!inputs[use_index]->isa<CNode>()) {
@ -411,31 +441,7 @@ std::string GetVirtualNodeTargetFromInputs(const AnfNodePtr &node) {
return GetOriginNodeTarget(inputs[use_index]);
}
} else if (IsPrimitiveCNode(node, prim::kPrimMakeTuple) || IsPrimitiveCNode(node, prim::kPrimUpdateState)) {
std::vector<AnfNodePtr> real_inputs;
const size_t update_state_valid_input_index = 2;
const size_t make_tuple_valid_input_index = 1;
if (IsPrimitiveCNode(node, prim::kPrimUpdateState) && inputs.size() > update_state_valid_input_index) {
(void)std::copy(inputs.begin() + SizeToLong(update_state_valid_input_index), inputs.end(),
std::back_inserter(real_inputs));
} else if (IsPrimitiveCNode(node, prim::kPrimMakeTuple) && inputs.size() > make_tuple_valid_input_index) {
(void)std::copy(inputs.begin() + SizeToLong(make_tuple_valid_input_index), inputs.end(),
std::back_inserter(real_inputs));
}
std::string first_input_target = kTargetUnDefined;
bool has_diff_target =
std::any_of(std::rbegin(real_inputs), std::rend(real_inputs), [&first_input_target](const AnfNodePtr &n) {
auto target = GetOriginNodeTarget(n);
if (target == kTargetUnDefined) {
return false;
}
if (first_input_target == kTargetUnDefined) {
first_input_target = target;
}
return target != first_input_target;
});
if (!has_diff_target) {
return first_input_target;
}
return GetNodeTargetForVarInputNode(node->cast<CNodePtr>());
} else if (IsPrimitiveCNode(node, prim::kPrimTupleGetItem)) {
return GetOriginNodeTarget(cnode->input(1));
}

View File

@ -850,9 +850,7 @@ AnfNodePtr MSANFModelParser::BuildOperatorNode(const mind_ir::NodeProto &node_pr
return std::make_shared<ValueNode>(prim);
}
// Set CNode abstract.
void MSANFModelParser::SetCNodeAbastract(const mind_ir::NodeProto &node_proto, CNodePtr cnode_ptr) {
const std::string &node_type = node_proto.op_type();
bool MSANFModelParser::CheckCNodePrim(CNodePtr cnode_ptr) {
// Handle control flow operator.
auto operatorPtr = cnode_ptr->input(0);
// Set abstract of switch(c,f,t),switchLayer(c,tup) and
@ -861,13 +859,43 @@ void MSANFModelParser::SetCNodeAbastract(const mind_ir::NodeProto &node_proto, C
if (IsPrimitiveEquals(prim::kPrimSwitch, prim) || IsPrimitiveEquals(prim::kPrimSwitchLayer, prim) ||
IsPrimitiveEquals(prim::kPrimPartial, prim)) {
cnode_ptr->set_abstract(nullptr);
return;
return true;
}
// If the operator is not a primitive, the abstract will been set to null.
// Because there are not some operators in front end, the abstract of primitive should be reserved.
if (prim == nullptr) {
cnode_ptr->set_abstract(nullptr);
return true;
}
return false;
}
void MSANFModelParser::SetEmptyTensorProtoCNodeAbstract(CNodePtr cnode_ptr, const std::string &node_type) {
if (node_type == "UpdateState") {
cnode_ptr->set_abstract(kUMonad->ToAbstract());
} else if (node_type == "Depend") {
cnode_ptr->set_abstract(kBool->ToAbstract());
} else {
AbstractBasePtrList elem;
for (size_t index = 1; index < cnode_ptr->inputs().size(); ++index) {
auto abs = cnode_ptr->input(index)->abstract();
if (abs != nullptr) {
if (abs->GetValueTrack() == nullptr) {
abs->set_value(kAnyValue);
}
elem.push_back(abs);
}
}
if (!elem.empty()) {
cnode_ptr->set_abstract(std::make_shared<abstract::AbstractTuple>(elem));
}
}
}
// Set CNode abstract.
void MSANFModelParser::SetCNodeAbastract(const mind_ir::NodeProto &node_proto, CNodePtr cnode_ptr) {
if (CheckCNodePrim(cnode_ptr)) {
return;
}
@ -885,26 +913,9 @@ void MSANFModelParser::SetCNodeAbastract(const mind_ir::NodeProto &node_proto, C
// Because there is not context in unit test,
// abstract->broaden() is replaced by abstract->set_value(kAnyValue).
const std::string &node_type = node_proto.op_type();
if (kv.size() == 0) {
if (node_type == "UpdateState") {
cnode_ptr->set_abstract(kUMonad->ToAbstract());
} else if (node_type == "Depend") {
cnode_ptr->set_abstract(kBool->ToAbstract());
} else {
AbstractBasePtrList elem;
for (size_t index = 1; index < cnode_ptr->inputs().size(); ++index) {
auto abs = cnode_ptr->input(index)->abstract();
if (abs != nullptr) {
if (abs->GetValueTrack() == nullptr) {
abs->set_value(kAnyValue);
}
elem.push_back(abs);
}
}
if (!elem.empty()) {
cnode_ptr->set_abstract(std::make_shared<abstract::AbstractTuple>(elem));
}
}
SetEmptyTensorProtoCNodeAbstract(cnode_ptr, node_type);
} else if (kv.size() == 1) {
std::unordered_map<std::string, abstract::AbstractBasePtr>::iterator iter = kv.begin();
if (iter->second != nullptr) {

View File

@ -63,6 +63,8 @@ class MSANFModelParser {
bool ObtainCNodeAttrInTensorForm(const PrimitivePtr &prim, const mind_ir::AttributeProto &attr_proto);
bool BuildValueNodeForFuncGraph(const mind_ir::NodeProto &node_proto);
AnfNodePtr BuildOperatorNode(const mind_ir::NodeProto &node_proto);
bool CheckCNodePrim(CNodePtr cnode_ptr);
void SetEmptyTensorProtoCNodeAbstract(CNodePtr cnode_ptr, const std::string &node_type);
void SetCNodeAbastract(const mind_ir::NodeProto &node_proto, CNodePtr cnode_ptr);
bool ObtainValueNodeInTensorForm(const string &value_node_name, const mind_ir::TensorProto &attr_tensor);
bool ObtainValueNodeInTupleTensorForm(const string &value_node_name, const mind_ir::AttributeProto &attr_proto);

View File

@ -552,7 +552,8 @@ ShapeVector CheckAndConvertUtils::CheckTensorIntValue(const std::string &type_na
}
TypePtr CheckAndConvertUtils::CheckTensorSubClass(const string &type_name, const TypePtr &type,
const std::set<TypePtr> &template_types, const string &prim_name) {
const std::set<TypePtr> &template_types, const string &prim_name,
bool is_mix) {
if (CheckType(type, template_types)) {
return type;
}
@ -565,6 +566,11 @@ TypePtr CheckAndConvertUtils::CheckTensorSubClass(const string &type_name, const
}
buffer << " Tensor[" << item->ToString() << "],";
}
if (is_mix) {
for (const auto &item : template_types) {
buffer << " " << item->ToString() << "],";
}
}
buffer << "}, but got " << type->ToString();
buffer << ".";
MS_EXCEPTION(TypeError) << buffer.str();
@ -594,7 +600,7 @@ TypePtr CheckAndConvertUtils::CheckScalarOrTensorTypesSame(const std::map<std::s
(void)input_names.append(item.first);
(void)input_names.append(", ");
}
return CheckMixSubClass(input_names, arg_, valid_values, prim_name);
return CheckTensorSubClass(input_names, arg_, valid_values, prim_name, true);
}
TypePtr CheckAndConvertUtils::_CheckTypeSame(const std::map<std::string, TypePtr> &args, const std::string &prim_name,
@ -790,26 +796,4 @@ bool CheckAndConvertUtils::HasDynamicShapeInput(const AbstractBasePtrList &abs_l
}
return false;
}
TypePtr CheckAndConvertUtils::CheckMixSubClass(const string &type_name, const TypePtr &type,
const std::set<TypePtr> &template_types, const string &prim_name) {
if (CheckType(type, template_types)) {
return type;
}
std::ostringstream buffer;
buffer << "Primitive[" << prim_name << "]'s input argument[" << type_name << "] must be a type of {";
for (const auto &item : template_types) {
if (item->isa<TensorType>()) {
buffer << item->ToString();
continue;
}
buffer << " Tensor[" << item->ToString() << "],";
}
for (const auto &item : template_types) {
buffer << " " << item->ToString() << "],";
}
buffer << "}, but got " << type->ToString();
buffer << ".";
MS_EXCEPTION(TypeError) << buffer.str();
}
} // namespace mindspore

View File

@ -321,9 +321,8 @@ class CheckAndConvertUtils {
static TypePtr _CheckTypeSame(const std::map<std::string, TypePtr> &args, const std::string &prim_name,
const bool allow_mix);
static TypePtr CheckTensorSubClass(const std::string &type_name, const TypePtr &type,
const std::set<TypePtr> &template_types, const std::string &prim_name);
static TypePtr CheckMixSubClass(const std::string &type_name, const TypePtr &type,
const std::set<TypePtr> &template_types, const std::string &prim_name);
const std::set<TypePtr> &template_types, const std::string &prim_name,
bool is_mix = false);
};
} // namespace mindspore
#endif // MINDSPORE_CORE_UTILS_CHECK_CONVERT_UTILS_H_