You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

anf_dump_utils.cc 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright 2022 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "include/common/debug/anf_dump_utils.h"
  17. #include "abstract/abstract_function.h"
  18. namespace mindspore {
  19. namespace {
  20. std::string GetAbstractFuncStr(const abstract::AbstractFunctionPtr &abs) {
  21. std::ostringstream oss;
  22. if (abs->isa<abstract::FuncGraphAbstractClosure>()) {
  23. const auto &abstract_func_graph = abs->cast<abstract::FuncGraphAbstractClosurePtr>();
  24. if (abstract_func_graph->func_graph() != nullptr) {
  25. oss << abstract_func_graph->func_graph()->ToString();
  26. }
  27. }
  28. if (abs->isa<abstract::PartialAbstractClosure>()) {
  29. const auto &abstract_partial_func = abs->cast<abstract::PartialAbstractClosurePtr>();
  30. const auto &abstract_fn = abstract_partial_func->fn();
  31. if (abstract_fn->isa<abstract::FuncGraphAbstractClosure>()) {
  32. const auto &abstract_func_graph = abstract_fn->cast<abstract::FuncGraphAbstractClosurePtr>();
  33. if (abstract_func_graph->func_graph() != nullptr) {
  34. oss << "Partial(" << abstract_func_graph->func_graph()->ToString() << ")";
  35. }
  36. }
  37. }
  38. return oss.str();
  39. }
  40. } // namespace
  41. std::string GetNodeFuncStr(const AnfNodePtr &nd) {
  42. MS_EXCEPTION_IF_NULL(nd);
  43. std::ostringstream oss;
  44. std::string str;
  45. const auto &abs = nd->abstract();
  46. if (IsValueNode<FuncGraph>(nd) || abs == nullptr || !abs->isa<abstract::AbstractFunction>()) {
  47. return str;
  48. }
  49. const auto &abs_func = abs->cast<abstract::AbstractFunctionPtr>();
  50. if (abs_func->isa<abstract::AbstractFuncUnion>()) {
  51. oss << "FuncUnion(";
  52. bool first_item = true;
  53. auto build_oss = [&oss, &first_item](const abstract::AbstractFuncAtomPtr &poss) {
  54. auto abs_str = GetAbstractFuncStr(poss);
  55. if (!first_item) {
  56. oss << ", ";
  57. } else {
  58. first_item = false;
  59. }
  60. if (!abs_str.empty()) {
  61. oss << abs_str;
  62. }
  63. };
  64. abs_func->Visit(build_oss);
  65. oss << ")";
  66. return oss.str();
  67. }
  68. return GetAbstractFuncStr(abs_func);
  69. }
  70. std::string TypeToShortString(const TypeId &typeId) {
  71. std::string label = TypeIdLabel(typeId);
  72. std::string prefix = "kNumberType";
  73. if (prefix.length() > label.length()) {
  74. return label;
  75. }
  76. auto position = label.find(prefix);
  77. // Position is 0 when label begins with prefix
  78. if (position != 0) {
  79. return label;
  80. }
  81. auto sub_position = position + prefix.length();
  82. if (sub_position >= label.length()) {
  83. return label;
  84. }
  85. return label.substr(sub_position);
  86. }
  87. std::string GetKernelNodeName(const AnfNodePtr &anf_node) {
  88. MS_EXCEPTION_IF_NULL(anf_node);
  89. std::string kernel_name = anf_node->fullname_with_scope();
  90. if (kernel_name.empty()) {
  91. kernel_name = anf_node->ToString();
  92. }
  93. MS_LOG(DEBUG) << "Full scope kernel name is " << kernel_name << ".";
  94. return kernel_name;
  95. }
  96. } // namespace mindspore