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 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "debug/label.h"
  17. #include <algorithm>
  18. #include <sstream>
  19. #include <utility>
  20. #include "debug/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. break;
  50. }
  51. trace_name.trace_labels.push_back(GetTraceName(temp_info->trace_info(), trace_label));
  52. temp_info = temp_info->trace_info()->debug_info();
  53. } else {
  54. break;
  55. }
  56. }
  57. if (!temp_info->name().empty()) {
  58. trace_name.name = temp_info->name();
  59. } else {
  60. trace_name.name = temp_info->debug_name();
  61. }
  62. return trace_name;
  63. }
  64. std::string CombineTraceTypes(const std::string &root_name, const std::vector<std::string> &trace_labels) {
  65. std::string tags = "";
  66. for (auto &itr : trace_labels) {
  67. std::string symbol = itr;
  68. tags = tags + symbol;
  69. }
  70. return tags + root_name;
  71. }
  72. // get the label name of the node debug info
  73. std::string LabelString(const DebugInfoPtr &debug_info, TraceLabelType trace_label) {
  74. NameWithTrace trace_name = RootName(debug_info, trace_label);
  75. return CombineTraceTypes(trace_name.name, trace_name.trace_labels);
  76. }
  77. std::string CombineUniqueID(const DebugInfoPtr &debug_info) {
  78. auto temp_info = debug_info;
  79. std::string label = "";
  80. while (temp_info != nullptr) {
  81. if (!temp_info->name().empty()) {
  82. label = label + temp_info->name();
  83. } else {
  84. // the symbol 'U' is for identification of number
  85. label = label + "U" + std::to_string(temp_info->unique_id());
  86. }
  87. if (temp_info->trace_info() != nullptr) {
  88. label = label + "_" + temp_info->trace_info()->full_name() + "_";
  89. temp_info = temp_info->trace_info()->debug_info();
  90. } else {
  91. temp_info = nullptr;
  92. }
  93. }
  94. return label;
  95. }
  96. // get trace with unique id chain
  97. std::string LabelStringUnique(const DebugInfoPtr &debug_info) { return CombineUniqueID(debug_info); }
  98. std::string Label(const DebugInfoPtr &debug_info, TraceLabelType trace_label) {
  99. if (GetGlobalTraceLabelType() == TraceLabelType::kWithUniqueId) {
  100. return LabelStringUnique(debug_info);
  101. }
  102. return LabelString(debug_info, trace_label);
  103. }
  104. } // namespace label_manage
  105. } // namespace mindspore