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_analyse.py 6.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # Copyright 2019 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. Fuction:
  17. Test profiler to watch the performance of training.
  18. Usage:
  19. pytest tests/st/func/profiler
  20. """
  21. import os
  22. from unittest import mock, TestCase
  23. import pytest
  24. from mindinsight.profiler.analyser.analyser_factory import AnalyserFactory
  25. from mindinsight.profiler.common.exceptions.exceptions import StepNumNotSupportedException, \
  26. ProfilerParamValueErrorException
  27. from mindinsight.profiler.profiling import Profiler, FrameworkParser
  28. from tests.st.func.profiler import RAW_DATA_BASE
  29. from tests.st.func.profiler.conftest import BASE_SUMMARY_DIR
  30. @pytest.mark.usefixtures('create_summary_dir')
  31. class TestProfilerAnalyse(TestCase):
  32. """Test Converter module."""
  33. JOB_ID = 'JOB3'
  34. @classmethod
  35. def setup_class(cls):
  36. """Generate parsed files."""
  37. cls.step_trace_file = 'step_trace_raw_1_detail_time.csv'
  38. cls.generate_parsed_files()
  39. def setUp(self):
  40. """Setup before each test."""
  41. self.step_trace_analyser = AnalyserFactory.instance().get_analyser(
  42. 'step_trace', self.profiler, '1')
  43. @classmethod
  44. def generate_parsed_files(cls):
  45. """Test parse raw info about profiler."""
  46. cls.summary_dir = os.path.join(BASE_SUMMARY_DIR, 'normal_run')
  47. cls.profiler = os.path.join(cls.summary_dir, 'profiler')
  48. FrameworkParser._raw_data_dir = RAW_DATA_BASE
  49. if not os.path.exists(cls.summary_dir):
  50. os.makedirs(cls.summary_dir)
  51. Profiler._base_profiling_container_path = os.path.join(RAW_DATA_BASE, 'container')
  52. with mock.patch('mindinsight.profiler.profiling.PROFILING_LOG_BASE_PATH', RAW_DATA_BASE):
  53. profiler = Profiler(subgraph='all', is_detail=True, is_show_op_path=False,
  54. output_path=cls.summary_dir, job_id=cls.JOB_ID)
  55. profiler.analyse()
  56. @pytest.mark.level0
  57. @pytest.mark.env_single
  58. @pytest.mark.platform_x86_cpu
  59. @pytest.mark.platform_arm_ascend_training
  60. @pytest.mark.platform_x86_gpu_training
  61. @pytest.mark.platform_x86_ascend_training
  62. def test_step_trace_file_exist(self):
  63. """Test the step trace file has been generated"""
  64. output_files = os.listdir(self.profiler)
  65. assert self.step_trace_file in output_files
  66. @pytest.mark.level0
  67. @pytest.mark.env_single
  68. @pytest.mark.platform_x86_cpu
  69. @pytest.mark.platform_arm_ascend_training
  70. @pytest.mark.platform_x86_gpu_training
  71. @pytest.mark.platform_x86_ascend_training
  72. def test_graph_api(self):
  73. """Test step trace restful api."""
  74. condition = {
  75. 'filter_condition': {
  76. 'mode': 'step',
  77. 'step_id': 0
  78. }
  79. }
  80. analyser = self.step_trace_analyser
  81. res = analyser.query(condition)
  82. assert res['size'] == 322
  83. assert len(res['training_trace_graph']) == 13
  84. assert res['training_trace_graph'][-1] == [
  85. {'name': '', 'start': 0.2038, 'duration': 118.1667},
  86. {'name': 'stream_540_parallel_0', 'start': 118.3705, 'duration': 49.281},
  87. {'name': '', 'start': 167.6515, 'duration': 37.7294}]
  88. @pytest.mark.level0
  89. @pytest.mark.env_single
  90. @pytest.mark.platform_x86_cpu
  91. @pytest.mark.platform_arm_ascend_training
  92. @pytest.mark.platform_x86_gpu_training
  93. @pytest.mark.platform_x86_ascend_training
  94. def test_graph_api_error(self):
  95. """Test graph api without mode."""
  96. condition = {
  97. 'filter_condition': {
  98. 'step_id': -1
  99. }}
  100. self.assertRaisesRegex(
  101. StepNumNotSupportedException,
  102. 'The step num must be in',
  103. self.step_trace_analyser.query,
  104. condition
  105. )
  106. @pytest.mark.level0
  107. @pytest.mark.env_single
  108. @pytest.mark.platform_x86_cpu
  109. @pytest.mark.platform_arm_ascend_training
  110. @pytest.mark.platform_x86_gpu_training
  111. @pytest.mark.platform_x86_ascend_training
  112. def test_target_info_api(self):
  113. """Test step trace restful api."""
  114. condition = {
  115. 'filter_condition': {
  116. 'mode': 'proc',
  117. 'step_id': None
  118. }
  119. }
  120. analyser = AnalyserFactory.instance().get_analyser('step_trace', self.profiler, '1')
  121. for proc_name in ['iteration_interval', 'fp_and_bp', 'tail']:
  122. condition['filter_condition']['proc_name'] = proc_name
  123. res = analyser.query(condition)
  124. assert res['size'] == 322
  125. assert len(res['info'][proc_name]) == res['size']
  126. @pytest.mark.level0
  127. @pytest.mark.env_single
  128. @pytest.mark.platform_x86_cpu
  129. @pytest.mark.platform_arm_ascend_training
  130. @pytest.mark.platform_x86_gpu_training
  131. @pytest.mark.platform_x86_ascend_training
  132. def test_summary_for_step_trace(self):
  133. """Test summary for step trace."""
  134. analyser = AnalyserFactory.instance().get_analyser('step_trace', self.profiler, '1')
  135. summary = analyser.summary
  136. assert summary == {
  137. 'total_time': 205.3809,
  138. 'iteration_interval': '0.1%',
  139. 'fp_and_bp': '57.48%',
  140. 'tail': '42.42%',
  141. 'total_steps': 322}
  142. @pytest.mark.level0
  143. @pytest.mark.env_single
  144. @pytest.mark.platform_x86_cpu
  145. @pytest.mark.platform_arm_ascend_training
  146. @pytest.mark.platform_x86_gpu_training
  147. @pytest.mark.platform_x86_ascend_training
  148. def test_target_info_api_error(self):
  149. """Test graph api without mode."""
  150. condition = {
  151. 'filter_condition': {
  152. 'proc_name': 'fake name'
  153. }}
  154. self.assertRaisesRegex(
  155. ProfilerParamValueErrorException,
  156. 'Param value error',
  157. self.step_trace_analyser.query,
  158. condition
  159. )