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.

configurations.py 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Common configurations for debugger unit testing."""
  16. import json
  17. import os
  18. from google.protobuf import json_format
  19. from mindinsight.datavisual.data_transform.graph import NodeTypeEnum
  20. from mindinsight.debugger.common.utils import NodeBasicInfo
  21. from mindinsight.debugger.proto import ms_graph_pb2
  22. from mindinsight.debugger.stream_handler.graph_handler import GraphHandler
  23. from mindinsight.debugger.stream_handler.watchpoint_handler import WatchpointHitHandler
  24. from tests.utils.tools import compare_result_with_file
  25. GRAPH_PROTO_FILE = os.path.join(
  26. os.path.dirname(__file__), '../../utils/resource/graph_pb/lenet.pb'
  27. )
  28. DEBUGGER_EXPECTED_RESULTS = os.path.join(os.path.dirname(__file__), 'expected_results')
  29. def get_graph_proto():
  30. """Get graph proto."""
  31. with open(GRAPH_PROTO_FILE, 'rb') as f:
  32. content = f.read()
  33. graph = ms_graph_pb2.GraphProto()
  34. graph.ParseFromString(content)
  35. return graph
  36. def init_graph_handler():
  37. """Init GraphHandler."""
  38. graph = get_graph_proto()
  39. graph_handler = GraphHandler()
  40. graph_handler.put(graph)
  41. return graph_handler
  42. def init_watchpoint_hit_handler(value):
  43. """Init WatchpointHitHandler."""
  44. wph_handler = WatchpointHitHandler()
  45. wph_handler.put(value)
  46. return wph_handler
  47. def get_node_basic_infos(node_names):
  48. """Get node info according to node names."""
  49. if not node_names:
  50. return []
  51. graph_stream = init_graph_handler()
  52. node_infos = []
  53. for node_name in node_names:
  54. node_type = graph_stream.get_node_type(node_name)
  55. if node_type == NodeTypeEnum.AGGREGATION_SCOPE.value:
  56. sub_nodes = graph_stream.get_nodes_by_scope(node_name)
  57. sub_infos = [NodeBasicInfo(name=node.name, full_name=node.full_name, type=node.type)
  58. for node in sub_nodes]
  59. node_infos.extend(sub_infos)
  60. full_name = graph_stream.get_full_name(node_name)
  61. node_infos.append(NodeBasicInfo(name=node_name, full_name=full_name, type=node_type))
  62. return node_infos
  63. def get_watch_nodes_by_search(watch_nodes):
  64. """Get watched leaf nodes by search name."""
  65. watched_leaf_nodes = []
  66. graph_stream = init_graph_handler()
  67. for search_name in watch_nodes:
  68. search_nodes = graph_stream.get_searched_node_list()
  69. search_node_names = [
  70. NodeBasicInfo(name=node.name, full_name=node.full_name, type=node.type)
  71. for node in search_nodes
  72. if node.name.startswith(search_name)]
  73. watched_leaf_nodes.extend(search_node_names)
  74. return watched_leaf_nodes
  75. def mock_tensor_proto():
  76. """Mock tensor proto."""
  77. tensor_dict = {
  78. "node_name":
  79. "Default/network-WithLossCell/_backbone-LeNet5/relu-ReLU/gradReLU/ReluGradV2-op92",
  80. "slot": "0"
  81. }
  82. tensor_proto = json_format.Parse(json.dumps(tensor_dict), ms_graph_pb2.TensorProto())
  83. return tensor_proto
  84. def mock_tensor_history():
  85. """Mock tensor history."""
  86. tensor_history = {
  87. "tensor_history": [
  88. {"name": "Default/TransData-op99:0",
  89. "full_name": "Default/TransData-op99:0",
  90. "node_type": "TransData",
  91. "type": "output",
  92. "step": 0,
  93. "dtype": "DT_FLOAT32",
  94. "shape": [2, 3],
  95. "has_prev_step": False,
  96. "value": "click to view"},
  97. {"name": "Default/args0:0",
  98. "full_name": "Default/args0:0",
  99. "node_type": "Parameter",
  100. "type": "input",
  101. "step": 0,
  102. "dtype": "DT_FLOAT32",
  103. "shape": [2, 3],
  104. "has_prev_step": False,
  105. "value": "click to view"}
  106. ],
  107. "metadata": {
  108. "state": "waiting",
  109. "step": 0,
  110. "device_name": "0",
  111. "pos": "0",
  112. "ip": "127.0.0.1:57492",
  113. "node_name": "",
  114. "backend": "Ascend"
  115. }
  116. }
  117. return tensor_history
  118. def compare_debugger_result_with_file(res, expect_file):
  119. """
  120. Compare debugger result with file.
  121. Args:
  122. res (dict): The debugger result in dict type.
  123. expect_file: The expected file name.
  124. """
  125. real_path = os.path.join(DEBUGGER_EXPECTED_RESULTS, expect_file)
  126. compare_result_with_file(res, real_path)