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.

label.cc 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Copyright 2019 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 "utils/label.h"
  17. #include <algorithm>
  18. #include <sstream>
  19. #include <utility>
  20. #include "utils/info.h"
  21. #include "ir/func_graph.h"
  22. namespace mindspore {
  23. namespace label_manage {
  24. static TraceLabelType trace_type = TraceLabelType::kShortSymbol;
  25. TraceLabelType GetGlobalTraceLabelType() { return trace_type; }
  26. void SetGlobalTraceLabelType(TraceLabelType label_type) { trace_type = label_type; }
  27. struct NameWithTrace {
  28. std::string name;
  29. std::vector<std::string> trace_labels;
  30. };
  31. static std::string GetTraceName(const TraceInfoPtr &trace_info, TraceLabelType trace_label) {
  32. switch (trace_label) {
  33. case TraceLabelType::kShortSymbol:
  34. return trace_info->symbol();
  35. case TraceLabelType::kFullName:
  36. return "_" + trace_info->full_name() + "_";
  37. default:
  38. return "";
  39. }
  40. }
  41. NameWithTrace RootName(const DebugInfoPtr &debug_info, TraceLabelType trace_label) {
  42. NameWithTrace trace_name;
  43. // find debug info after Resolve/ExpandJ/GenMetaFuncGraph, it is a new node
  44. auto temp_info = debug_info;
  45. while (temp_info != nullptr) {
  46. if (temp_info->trace_info() != nullptr) {
  47. if (temp_info->trace_info()->isa<TraceResolve>() || temp_info->trace_info()->isa<TraceExpandJ>() ||
  48. temp_info->trace_info()->isa<TraceGenMetaFuncGraph>() ||
  49. temp_info->trace_info()->isa<TraceGenerateVarArg>() || temp_info->trace_info()->isa<TraceGenerateKwArg>()) {
  50. break;
  51. }
  52. trace_name.trace_labels.push_back(GetTraceName(temp_info->trace_info(), trace_label));
  53. temp_info = temp_info->trace_info()->debug_info();
  54. } else {
  55. break;
  56. }
  57. }
  58. if (!temp_info->name().empty()) {
  59. trace_name.name = temp_info->name();
  60. } else {
  61. trace_name.name = temp_info->debug_name();
  62. }
  63. return trace_name;
  64. }
  65. std::string CombineTraceTypes(const std::string &root_name, const std::vector<std::string> &trace_labels) {
  66. std::string tags = "";
  67. for (auto &itr : trace_labels) {
  68. std::string symbol = itr;
  69. tags = tags + symbol;
  70. }
  71. return tags + root_name;
  72. }
  73. // get the label name of the node debug info
  74. std::string LabelString(const DebugInfoPtr &debug_info, TraceLabelType trace_label) {
  75. NameWithTrace trace_name = RootName(debug_info, trace_label);
  76. return CombineTraceTypes(trace_name.name, trace_name.trace_labels);
  77. }
  78. std::string CombineUniqueID(const DebugInfoPtr &debug_info) {
  79. auto temp_info = debug_info;
  80. std::string label = "";
  81. while (temp_info != nullptr) {
  82. if (!temp_info->name().empty()) {
  83. label = label + temp_info->name();
  84. } else {
  85. // the symbol 'U' is for identification of number
  86. label = label + "U" + std::to_string(temp_info->unique_id());
  87. }
  88. if (temp_info->trace_info() != nullptr) {
  89. label = label + "_" + temp_info->trace_info()->full_name() + "_";
  90. temp_info = temp_info->trace_info()->debug_info();
  91. } else {
  92. temp_info = nullptr;
  93. }
  94. }
  95. return label;
  96. }
  97. // get trace with unique id chain
  98. std::string LabelStringUnique(const DebugInfoPtr &debug_info) { return CombineUniqueID(debug_info); }
  99. std::string Label(const DebugInfoPtr &debug_info, TraceLabelType trace_label) {
  100. if (GetGlobalTraceLabelType() == TraceLabelType::kWithUniqueId) {
  101. return LabelStringUnique(debug_info);
  102. }
  103. return LabelString(debug_info, trace_label);
  104. }
  105. } // namespace label_manage
  106. } // namespace mindspore