diff --git a/mindspore/ccsrc/plugin/device/ascend/kernel/acl/acl_kernel_utils.cc b/mindspore/ccsrc/plugin/device/ascend/kernel/acl/acl_kernel_utils.cc index e0df6015eb7..8dd82e6cd74 100644 --- a/mindspore/ccsrc/plugin/device/ascend/kernel/acl/acl_kernel_utils.cc +++ b/mindspore/ccsrc/plugin/device/ascend/kernel/acl/acl_kernel_utils.cc @@ -29,6 +29,7 @@ namespace mindspore { namespace kernel { namespace { constexpr size_t kMaxAttrToInputSize = 1024; +constexpr auto kParamDynamic = "dynamic"; static const std::map<::ge::DataType, aclDataType> kMsTypeToAclType = { {::ge::DT_BOOL, ACL_BOOL}, {::ge::DT_INT8, ACL_INT8}, {::ge::DT_INT16, ACL_INT16}, @@ -529,11 +530,34 @@ std::vector AclUtils::GetInputTensorDesc(const AnfNodePtr &anf_ return res; } +std::set AclUtils::GetUselessOutputs(const AnfNodePtr &node) { + MS_EXCEPTION_IF_NULL(node); + static const std::map> kMsUselessOutputs = { + {prim::kPrimApplyMomentum->name(), {"accum"}}}; + auto op_name = common::AnfAlgo::GetCNodeName(node); + auto iter = kMsUselessOutputs.find(op_name); + if (iter != kMsUselessOutputs.end()) { + return iter->second; + } + return {}; +} + std::vector AclUtils::GetOutputTensorDesc(const AnfNodePtr &anf_node) { MS_EXCEPTION_IF_NULL(anf_node); size_t output_num = common::AnfAlgo::GetOutputTensorNum(anf_node); std::vector res; + auto useless_outputs = GetUselessOutputs(anf_node); + auto out_anchor_names = GetOpOutputAnchorNames(anf_node); for (size_t i = 0; i < output_num; ++i) { + if (out_anchor_names.size() <= i) { + MS_LOG(EXCEPTION) << "Index [" << i + << "] exceed the size of all input names, node:" << anf_node->fullname_with_scope(); + } + if (useless_outputs.count(out_anchor_names[i])) { + MS_LOG(INFO) << "For op: [" << anf_node->fullname_with_scope() << "],current out anchor name:[" + << out_anchor_names[i] << "] is useless, need skip."; + continue; + } auto ori_shape = common::AnfAlgo::GetOutputInferShape(anf_node, i); auto output_shape = AnfAlgo::GetOutputDeviceShape(anf_node, i); auto output_type = AnfAlgo::GetOutputDeviceDataType(anf_node, i); @@ -545,5 +569,63 @@ std::vector AclUtils::GetOutputTensorDesc(const AnfNodePtr &anf } return res; } + +std::shared_ptr AclUtils::GetKernelOpInfo(const AnfNodePtr &node) { + MS_EXCEPTION_IF_NULL(node); + auto node_name = common::AnfAlgo::GetCNodeName(node); + auto op_info_ptr = kernel::OpLib::FindOp(node_name, kernel::kImplyTBE); + return op_info_ptr; +} + +std::vector AclUtils::GetOpInputAnchorNames(const AnfNodePtr &node) { + auto op_info_ptr = GetKernelOpInfo(node); + MS_EXCEPTION_IF_NULL(op_info_ptr); + auto inputs_ptr = op_info_ptr->inputs_ptr(); + auto primitive = common::AnfAlgo::GetCNodePrimitive(node); + MS_EXCEPTION_IF_NULL(primitive); + size_t dynamic_input_index = 0; + std::vector dynamic_inputs_list; + std::vector input_names; + if (primitive->GetAttr(kAttrDynInputSizes) != nullptr) { + dynamic_inputs_list = GetValue>(primitive->GetAttr(kAttrDynInputSizes)); + } + for (const auto &item : inputs_ptr) { + MS_EXCEPTION_IF_NULL(item); + if (item->param_type() == kParamDynamic) { + if (dynamic_input_index > dynamic_inputs_list.size()) { + MS_LOG(EXCEPTION) << "Dynamic input index should be less than the dynamic input's size."; + } + auto real_inputs_num = dynamic_inputs_list[dynamic_input_index]; + for (auto k = 0; k < real_inputs_num; k++) { + std::string input_name = item->name() + "_dynamic_" + std::to_string(k); + (void)input_names.emplace_back(input_name); + } + } else { + (void)input_names.emplace_back(item->name()); + } + dynamic_input_index++; + } + return input_names; +} + +std::vector AclUtils::GetOpOutputAnchorNames(const AnfNodePtr &node) { + auto op_info_ptr = GetKernelOpInfo(node); + auto outputs_ptr = op_info_ptr->outputs_ptr(); + std::vector output_names; + for (const auto &out_item : outputs_ptr) { + MS_EXCEPTION_IF_NULL(out_item); + if (out_item->param_type() == kParamDynamic && outputs_ptr.size() == 1) { + std::string output_name; + auto real_outputs_size = common::AnfAlgo::GetOutputTensorNum(node); + for (size_t i = 0; i < real_outputs_size; i++) { + output_name = out_item->name() + "_dynamic_" + std::to_string(i); + (void)output_names.emplace_back(output_name); + } + } else { + (void)output_names.emplace_back(out_item->name()); + } + } + return output_names; +} } // namespace kernel } // namespace mindspore diff --git a/mindspore/ccsrc/plugin/device/ascend/kernel/acl/acl_kernel_utils.h b/mindspore/ccsrc/plugin/device/ascend/kernel/acl/acl_kernel_utils.h index e65f007bb88..5276c767d68 100644 --- a/mindspore/ccsrc/plugin/device/ascend/kernel/acl/acl_kernel_utils.h +++ b/mindspore/ccsrc/plugin/device/ascend/kernel/acl/acl_kernel_utils.h @@ -17,6 +17,7 @@ #define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_ACL_ACL_UTILS_H_ #include #include +#include #include #include #include "kernel/kernel.h" @@ -24,6 +25,7 @@ #include "acl/acl_op_compiler.h" #include "acl/acl_base.h" #include "transform/graph_ir/convert.h" +#include "kernel/oplib/oplib.h" namespace mindspore { namespace kernel { @@ -104,6 +106,10 @@ class AclUtils { static std::vector GetInputTensorDesc(const AnfNodePtr &anf_node); static std::vector GetOutputTensorDesc(const AnfNodePtr &anf_node); + static std::shared_ptr GetKernelOpInfo(const AnfNodePtr &node); + static std::vector GetOpInputAnchorNames(const AnfNodePtr &node); + static std::vector GetOpOutputAnchorNames(const AnfNodePtr &node); + static std::set GetUselessOutputs(const AnfNodePtr &node); }; } // namespace kernel } // namespace mindspore diff --git a/mindspore/ccsrc/plugin/device/ascend/kernel/tbe/tiling/op_tiling_adapter.cc b/mindspore/ccsrc/plugin/device/ascend/kernel/tbe/tiling/op_tiling_adapter.cc index 6ec5209c772..b341966a377 100644 --- a/mindspore/ccsrc/plugin/device/ascend/kernel/tbe/tiling/op_tiling_adapter.cc +++ b/mindspore/ccsrc/plugin/device/ascend/kernel/tbe/tiling/op_tiling_adapter.cc @@ -31,6 +31,7 @@ #include "graph/utils/op_desc_utils.h" #include "plugin/device/ascend/kernel/ascend_kernel_mod.h" #include "graph/utils/tensor_utils.h" +#include "plugin/device/ascend/kernel/acl/acl_kernel_utils.h" namespace mindspore { namespace device { @@ -410,48 +411,8 @@ void OpTilingCalculateAdapter::AddEdge(const ::ge::NodePtr &ge_node, void OpTilingCalculateAdapter::InitOpIoName(const CNodePtr &node) { MS_EXCEPTION_IF_NULL(node); MS_LOG(DEBUG) << "Get the every input name of " << op_name_; - auto op_info_ptr = mindspore::kernel::tbe::TbeDynamicShapeUtil::FindOp(op_name_, node); - MS_EXCEPTION_IF_NULL(op_info_ptr); - auto primitive = common::AnfAlgo::GetCNodePrimitive(node); - MS_EXCEPTION_IF_NULL(primitive); - auto inputs_ptr = op_info_ptr->inputs_ptr(); - size_t dynamic_input_index = 0; - std::vector dynamic_inputs_list; - if (primitive->GetAttr(kAttrDynInputSizes) != nullptr) { - dynamic_inputs_list = GetValue>(primitive->GetAttr(kAttrDynInputSizes)); - } - for (const auto &item : inputs_ptr) { - MS_EXCEPTION_IF_NULL(item); - if (item->param_type() == PARAM_DYNAMIC) { - if (dynamic_input_index > dynamic_inputs_list.size()) { - MS_LOG(EXCEPTION) << "Dynamic input index should be less than the dynamic input's size."; - } - auto real_inputs_num = dynamic_inputs_list[dynamic_input_index]; - for (auto k = 0; k < real_inputs_num; k++) { - std::string input_name = item->name() + "_dynamic_" + std::to_string(k); - input_names_.emplace_back(input_name); - } - } else { - input_names_.emplace_back(item->name()); - } - dynamic_input_index++; - } - - // output io names - auto outputs_ptr = op_info_ptr->outputs_ptr(); - for (const auto &out_item : outputs_ptr) { - MS_EXCEPTION_IF_NULL(out_item); - if (out_item->param_type() == PARAM_DYNAMIC && outputs_ptr.size() == 1) { - std::string output_name; - auto real_outputs_size = common::AnfAlgo::GetOutputTensorNum(node); - for (size_t i = 0; i < real_outputs_size; i++) { - output_name = out_item->name() + "_dynamic_" + std::to_string(i); - output_names_.emplace_back(output_name); - } - } else { - output_names_.emplace_back(out_item->name()); - } - } + input_names_ = kernel::AclUtils::GetOpInputAnchorNames(node); + output_names_ = kernel::AclUtils::GetOpOutputAnchorNames(node); } ::ge::NodePtr OpTilingCalculateAdapter::CreateGeNode(const CNodePtr &node, ::ge::ComputeGraphPtr *ge_graph, diff --git a/mindspore/ccsrc/transform/graph_ir/convert.cc b/mindspore/ccsrc/transform/graph_ir/convert.cc index 700d8b45c91..3002b9b5924 100644 --- a/mindspore/ccsrc/transform/graph_ir/convert.cc +++ b/mindspore/ccsrc/transform/graph_ir/convert.cc @@ -2926,5 +2926,15 @@ std::map GeOpConvertor::GetNeedAddInput(const AnfNode return adpt->getAttrInputMap(); } + +bool GeOpConvertor::IsDynamicInput(const AnfNodePtr &node, const size_t idx) { + MS_EXCEPTION_IF_NULL(node); + OpAdapterPtr adapterPtr = FindAdapter(node, true); + if (adapterPtr == nullptr) { + MS_LOG(INFO) << "Can't find a adapter for op:" << node->DebugString(); + return false; + } + return adapterPtr->IsDynInputOp(idx); +} } // namespace transform } // namespace mindspore diff --git a/mindspore/ccsrc/transform/graph_ir/convert.h b/mindspore/ccsrc/transform/graph_ir/convert.h index d1c62c224b1..aca1b333805 100644 --- a/mindspore/ccsrc/transform/graph_ir/convert.h +++ b/mindspore/ccsrc/transform/graph_ir/convert.h @@ -67,6 +67,7 @@ class GeOpConvertor { static mindspore::HashSet GetNeedRemoveInput(const AnfNodePtr &node, const bool training); static std::map GetNeedAddInput(const AnfNodePtr &node, const bool training); + static bool IsDynamicInput(const AnfNodePtr &node, const size_t idx); }; class DfGraphConvertor { diff --git a/mindspore/ccsrc/transform/graph_ir/op_declare/math_ops_declare.cc b/mindspore/ccsrc/transform/graph_ir/op_declare/math_ops_declare.cc index 670e20f35d6..00a38409ae2 100644 --- a/mindspore/ccsrc/transform/graph_ir/op_declare/math_ops_declare.cc +++ b/mindspore/ccsrc/transform/graph_ir/op_declare/math_ops_declare.cc @@ -113,4 +113,13 @@ INPUT_MAP(IsNan) = {{1, INPUT_DESC(x)}}; ATTR_MAP(IsNan) = EMPTY_ATTR_MAP; OUTPUT_MAP(IsNan) = {{0, OUTPUT_DESC(y)}}; REG_ADPT_DESC(IsNan, kNameIsNan, ADPT_DESC(IsNan)) + +// LpNorm +INPUT_MAP(LpNorm) = {{1, INPUT_DESC(x)}}; +ATTR_MAP(LpNorm) = {{"p", ATTR_DESC(p, AnyTraits())}, + {"axis", ATTR_DESC(axes, AnyTraits>())}, + {"keep_dims", ATTR_DESC(keepdim, AnyTraits())}, + {"epsilon", ATTR_DESC(epsilon, AnyTraits())}}; +OUTPUT_MAP(LpNorm) = {{0, OUTPUT_DESC(y)}}; +REG_ADPT_DESC(LpNorm, prim::kPrimLpNorm->name(), ADPT_DESC(LpNorm)) } // namespace mindspore::transform diff --git a/mindspore/ccsrc/transform/graph_ir/op_declare/math_ops_declare.h b/mindspore/ccsrc/transform/graph_ir/op_declare/math_ops_declare.h index 8b9f3f929c1..22433dc05a3 100644 --- a/mindspore/ccsrc/transform/graph_ir/op_declare/math_ops_declare.h +++ b/mindspore/ccsrc/transform/graph_ir/op_declare/math_ops_declare.h @@ -60,5 +60,8 @@ DECLARE_OP_USE_OUTPUT(IsFinite) DECLARE_OP_ADAPTER(IsNan) DECLARE_OP_USE_OUTPUT(IsNan) + +DECLARE_OP_ADAPTER(LpNorm) +DECLARE_OP_USE_OUTPUT(LpNorm) } // namespace mindspore::transform #endif // MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_OP_DECLARE_MATH_OPS_DECLARE_H_