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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.debugger.proto import ms_graph_pb2
  20. from mindinsight.debugger.stream_handler.graph_handler import GraphHandler
  21. from mindinsight.debugger.stream_handler.watchpoint_handler import WatchpointHitHandler
  22. from tests.utils.tools import compare_result_with_file
  23. GRAPH_PROTO_FILE = os.path.join(
  24. os.path.dirname(__file__), '../../utils/resource/graph_pb/lenet.pb'
  25. )
  26. DEBUGGER_EXPECTED_RESULTS = os.path.join(os.path.dirname(__file__), 'expected_results')
  27. def get_graph_proto():
  28. """Get graph proto."""
  29. with open(GRAPH_PROTO_FILE, 'rb') as f:
  30. content = f.read()
  31. graph = ms_graph_pb2.GraphProto()
  32. graph.ParseFromString(content)
  33. return graph
  34. def init_graph_handler():
  35. """Init GraphHandler."""
  36. graph = get_graph_proto()
  37. graph_handler = GraphHandler()
  38. graph_handler.put({graph.name: graph})
  39. return graph_handler
  40. def init_watchpoint_hit_handler(value):
  41. """Init WatchpointHitHandler."""
  42. wph_handler = WatchpointHitHandler()
  43. wph_handler.put(value)
  44. return wph_handler
  45. def get_node_basic_infos(node_names):
  46. """Get node info according to node names."""
  47. if not node_names:
  48. return []
  49. graph_stream = init_graph_handler()
  50. graph_name = graph_stream.graph_names[0]
  51. node_infos = []
  52. for node_name in node_names:
  53. node_infos.append(graph_stream.get_node_basic_info(node_name, graph_name))
  54. return node_infos
  55. def get_watch_nodes_by_search(watch_nodes):
  56. """Get watched leaf nodes by search name."""
  57. watched_leaf_nodes = []
  58. graph_stream = init_graph_handler()
  59. graph_name = graph_stream.graph_names[0]
  60. for search_name in watch_nodes:
  61. search_node_info = graph_stream.get_node_basic_info_by_scope(search_name, graph_name)
  62. watched_leaf_nodes.extend(search_node_info)
  63. return watched_leaf_nodes
  64. def mock_tensor_proto():
  65. """Mock tensor proto."""
  66. tensor_dict = {
  67. "node_name":
  68. "Default/network-WithLossCell/_backbone-LeNet5/relu-ReLU/gradReLU/ReluGradV2-op92",
  69. "slot": "0"
  70. }
  71. tensor_proto = json_format.Parse(json.dumps(tensor_dict), ms_graph_pb2.TensorProto())
  72. return tensor_proto
  73. def mock_tensor_history():
  74. """Mock tensor history."""
  75. tensor_history = {
  76. "tensor_history": [
  77. {"name": "Default/TransData-op99:0",
  78. "full_name": "Default/TransData-op99:0",
  79. "node_type": "TransData",
  80. "type": "output",
  81. "step": 0,
  82. "dtype": "DT_FLOAT32",
  83. "shape": [2, 3],
  84. "has_prev_step": False,
  85. "value": "click to view"},
  86. {"name": "Default/args0:0",
  87. "full_name": "Default/args0:0",
  88. "node_type": "Parameter",
  89. "type": "input",
  90. "step": 0,
  91. "dtype": "DT_FLOAT32",
  92. "shape": [2, 3],
  93. "has_prev_step": False,
  94. "value": "click to view"}
  95. ],
  96. "metadata": {
  97. "state": "waiting",
  98. "step": 0,
  99. "device_name": "0",
  100. "pos": "0",
  101. "ip": "127.0.0.1:57492",
  102. "node_name": "",
  103. "backend": "Ascend"
  104. }
  105. }
  106. return tensor_history
  107. def compare_debugger_result_with_file(res, expect_file, save=False):
  108. """
  109. Compare debugger result with file.
  110. Args:
  111. res (dict): The debugger result in dict type.
  112. expect_file: The expected file name.
  113. """
  114. real_path = os.path.join(DEBUGGER_EXPECTED_RESULTS, expect_file)
  115. if save:
  116. with open(real_path, 'w') as file_handler:
  117. json.dump(res, file_handler)
  118. else:
  119. compare_result_with_file(res, real_path)