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.

test_read_tensors.py 7.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Copyright 2021 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. """
  16. Read tensor test script for offline debugger APIs.
  17. """
  18. import os
  19. import json
  20. import tempfile
  21. import mindspore.offline_debug.dbg_services as d
  22. import numpy as np
  23. import pytest
  24. from tests.security_utils import security_off_wrap
  25. from dump_test_utils import build_dump_structure
  26. GENERATE_GOLDEN = False
  27. tensor_json = []
  28. def run_read_tensors(is_sync):
  29. if is_sync:
  30. test_name = "sync_read_tensors"
  31. else:
  32. test_name = "async_read_tensors"
  33. # input tensor with zero slot
  34. tensor1 = np.array([32.0, 4096.0], np.float32)
  35. name1 = "CudnnUniformReal.CudnnUniformReal-op391.0.0."
  36. info1 = d.TensorInfo(node_name="Default/CudnnUniformReal-op391",
  37. slot=0, iteration=0, rank_id=0, root_graph_id=0, is_output=False)
  38. # input tensor with non-zero slot
  39. tensor2 = np.array([[0.0, 32.0, 4096.0], [4.5, 6.78, -11.0]], np.float32)
  40. name2 = "ReluGradV2.ReluGradV2-op406.0.0."
  41. info2 = d.TensorInfo(node_name="Gradients/Default/network-WithLossCell/_backbone-AlexNet/"
  42. "gradReLU/ReluGradV2-op406",
  43. slot=1, iteration=1, rank_id=0, root_graph_id=0, is_output=False)
  44. # output tensor with zero slot
  45. tensor3 = np.array([[[7.963e-05, 4.750e-05, 2.587e-05],
  46. [8.339e-05, 5.025e-05, 2.694e-05],
  47. [8.565e-05, 5.156e-05, 2.658e-05]],
  48. [[8.017e-05, 4.804e-05, 2.724e-05],
  49. [8.392e-05, 5.126e-05, 2.843e-05],
  50. [8.613e-05, 5.257e-05, 2.819e-05]],
  51. [[7.617e-05, 3.827e-05, 5.305e-06],
  52. [7.474e-05, 3.719e-05, 3.040e-06],
  53. [7.081e-05, 3.338e-05, -2.086e-06]]], np.float32)
  54. name3 = "Conv2DBackpropFilter.Conv2DBackpropFilter-op424.0.0."
  55. info3 = d.TensorInfo(node_name="Gradients/Default/network-WithLossCell/_backbone-AlexNet/conv5-Conv2d/"
  56. "gradConv2D/Conv2DBackpropFilter-op424",
  57. slot=0, iteration=1, rank_id=0, root_graph_id=0, is_output=True)
  58. # output tensor with non-zero slot
  59. tensor4 = np.array([2705090541, 1099111076, 4276637100, 3586562544, 890060077, 1869062900], np.float32)
  60. name4 = "ReLUV2.ReLUV2-op381.0.0."
  61. info4 = d.TensorInfo(node_name="Default/network-WithLossCell/_backbone-AlexNet/ReLUV2-op381",
  62. slot=1, iteration=0, rank_id=0, root_graph_id=0, is_output=True)
  63. tensor_name = [name1, name2, name3, name4]
  64. tensor_list = [tensor1, tensor2, tensor3, tensor4]
  65. tensor_info = [info1, info2, info3, info4]
  66. pwd = os.getcwd()
  67. with tempfile.TemporaryDirectory(dir=pwd) as tmp_dir:
  68. temp_dir = build_dump_structure(tmp_dir, tensor_name, tensor_list, "Test", tensor_info)
  69. debugger_backend = d.DbgServices(dump_file_path=temp_dir)
  70. debugger_backend.initialize(net_name="Test", is_sync_mode=is_sync)
  71. tensor_data = debugger_backend.read_tensors(tensor_info)
  72. if GENERATE_GOLDEN:
  73. print_read_tensors(tensor_info, tensor_data, 0, True, test_name)
  74. else:
  75. compare_expect_actual_result(tensor_info, tensor_data, 0, test_name)
  76. @pytest.mark.level0
  77. @pytest.mark.platform_arm_ascend_training
  78. @pytest.mark.platform_x86_ascend_training
  79. @pytest.mark.env_onecard
  80. @security_off_wrap
  81. def test_sync_read_tensors():
  82. run_read_tensors(True)
  83. @pytest.mark.level0
  84. @pytest.mark.platform_arm_ascend_training
  85. @pytest.mark.platform_x86_ascend_training
  86. @pytest.mark.env_onecard
  87. @security_off_wrap
  88. def test_async_read_tensors():
  89. run_read_tensors(False)
  90. def compare_expect_actual_result(tensor_info_list, tensor_data_list, test_index, test_name):
  91. """Compare actual result with golden file."""
  92. pwd = os.getcwd()
  93. golden_file = os.path.realpath(os.path.join(pwd, "golden", test_name + "_expected.json"))
  94. with open(golden_file) as f:
  95. expected_list = json.load(f)
  96. for x, (tensor_info, tensor_data) in enumerate(zip(tensor_info_list, tensor_data_list)):
  97. test_id = "tensor_"+ str(test_index+x+1)
  98. info = expected_list[x+test_index][test_id]
  99. assert tensor_info.node_name == info['tensor_info']['node_name']
  100. assert tensor_info.slot == info['tensor_info']['slot']
  101. assert tensor_info.iteration == info['tensor_info']['iteration']
  102. assert tensor_info.rank_id == info['tensor_info']['rank_id']
  103. assert tensor_info.root_graph_id == info['tensor_info']['root_graph_id']
  104. assert tensor_info.is_output == info['tensor_info']['is_output']
  105. actual_data = np.frombuffer(
  106. tensor_data.data_ptr, np.uint8, tensor_data.data_size).tolist()
  107. assert actual_data == info['tensor_data']['data']
  108. assert tensor_data.data_size == info['tensor_data']['size_in_bytes']
  109. assert tensor_data.dtype == info['tensor_data']['debugger_dtype']
  110. assert tensor_data.shape == info['tensor_data']['shape']
  111. def print_read_tensors(tensor_info_list, tensor_data_list, test_index, is_print, test_name):
  112. """Print read tensors result if GENERATE_GOLDEN is True."""
  113. for x, (tensor_info, tensor_data) in enumerate(zip(tensor_info_list, tensor_data_list)):
  114. tensor = "tensor_" + str(test_index+x+1)
  115. data = np.frombuffer(
  116. tensor_data.data_ptr, np.uint8, tensor_data.data_size).tolist()
  117. py_byte_size = len(tensor_data.data_ptr)
  118. c_byte_size = tensor_data.data_size
  119. if c_byte_size != py_byte_size:
  120. print("The python byte size of " + str(py_byte_size) +
  121. " does not match the C++ byte size of " + str(c_byte_size) + "\n")
  122. tensor_json.append({
  123. tensor: {
  124. 'tensor_info': {
  125. 'node_name': tensor_info.node_name,
  126. 'slot': tensor_info.slot,
  127. 'iteration': tensor_info.iteration,
  128. 'rank_id': tensor_info.rank_id,
  129. 'root_graph_id': tensor_info.root_graph_id,
  130. 'is_output': tensor_info.is_output
  131. },
  132. 'tensor_data': {
  133. 'data': data,
  134. 'size_in_bytes': tensor_data.data_size,
  135. 'debugger_dtype': tensor_data.dtype,
  136. 'shape': tensor_data.shape
  137. }
  138. }
  139. })
  140. if is_print:
  141. with open(test_name + "_expected.json", "w") as dump_f:
  142. json.dump(tensor_json, dump_f, indent=4, separators=(',', ': '))