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_profiling_startstop.py 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. Test MindData Profiling Start and Stop Support
  17. """
  18. import json
  19. import os
  20. import numpy as np
  21. import pytest
  22. import mindspore.common.dtype as mstype
  23. import mindspore.dataset as ds
  24. import mindspore._c_dataengine as cde
  25. import mindspore.dataset.transforms.c_transforms as C
  26. FILES = ["../data/dataset/testTFTestAllTypes/test.data"]
  27. DATASET_ROOT = "../data/dataset/testTFTestAllTypes/"
  28. SCHEMA_FILE = "../data/dataset/testTFTestAllTypes/datasetSchema.json"
  29. @pytest.mark.forked
  30. class TestMindDataProfilingStartStop:
  31. """
  32. Test MindData Profiling Manager Start-Stop Support
  33. Note: Use pytest fixture tmp_path to create files within this temporary directory,
  34. which is automatically created for each test and deleted at the end of the test.
  35. """
  36. def setup_class(self):
  37. """
  38. Run once for the class
  39. """
  40. # Get instance pointer for MindData profiling manager
  41. self.md_profiler = cde.GlobalContext.profiling_manager()
  42. def setup_method(self):
  43. """
  44. Run before each test function.
  45. """
  46. # Set the MindData Profiling related environment variables
  47. os.environ['RANK_ID'] = "0"
  48. os.environ['DEVICE_ID'] = "0"
  49. def teardown_method(self):
  50. """
  51. Run after each test function.
  52. """
  53. # Disable MindData Profiling related environment variables
  54. del os.environ['RANK_ID']
  55. del os.environ['DEVICE_ID']
  56. def confirm_pipeline_file(self, pipeline_file, num_ops, op_list=None):
  57. """
  58. Confirm pipeline JSON file with <num_ops> in the pipeline and the given optional list of ops
  59. """
  60. with open(pipeline_file) as file1:
  61. data = json.load(file1)
  62. op_info = data["op_info"]
  63. # Confirm ops in pipeline file
  64. assert len(op_info) == num_ops
  65. if op_list:
  66. for i in range(num_ops):
  67. assert op_info[i]["op_type"] in op_list
  68. def confirm_cpuutil_file(self, cpu_util_file, num_pipeline_ops):
  69. """
  70. Confirm CPU utilization JSON file with <num_pipeline_ops> in the pipeline
  71. """
  72. with open(cpu_util_file) as file1:
  73. data = json.load(file1)
  74. op_info = data["op_info"]
  75. assert len(op_info) == num_pipeline_ops
  76. def confirm_dataset_iterator_file(self, dataset_iterator_file, num_batches):
  77. """
  78. Confirm dataset iterator file exists with the correct number of rows in the file
  79. """
  80. assert os.path.exists(dataset_iterator_file)
  81. actual_num_lines = sum(1 for _ in open(dataset_iterator_file))
  82. # Confirm there are 4 lines for each batch in the dataset iterator file
  83. assert actual_num_lines == 4 * num_batches
  84. def test_profiling_early_stop(self, tmp_path):
  85. """
  86. Test MindData Profiling with Early Stop; profile for some iterations and then stop profiling
  87. """
  88. def source1():
  89. for i in range(8000):
  90. yield (np.array([i]),)
  91. # Initialize MindData profiling manager
  92. self.md_profiler.init()
  93. # Start MindData Profiling
  94. self.md_profiler.start()
  95. # Create this basic and common pipeline
  96. # Leaf/Source-Op -> Map -> Batch
  97. data1 = ds.GeneratorDataset(source1, ["col1"])
  98. type_cast_op = C.TypeCast(mstype.int32)
  99. data1 = data1.map(operations=type_cast_op, input_columns="col1")
  100. data1 = data1.batch(16)
  101. num_iter = 0
  102. # Note: If create_dict_iterator() is called with num_epochs>1, then EpochCtrlOp is added to the pipeline
  103. for _ in data1.create_dict_iterator(num_epochs=2):
  104. if num_iter == 400:
  105. # Stop MindData Profiling and Save MindData Profiling Output
  106. self.md_profiler.stop()
  107. self.md_profiler.save(str(tmp_path))
  108. num_iter += 1
  109. assert num_iter == 500
  110. pipeline_file = str(tmp_path) + "/pipeline_profiling_0.json"
  111. cpu_util_file = str(tmp_path) + "/minddata_cpu_utilization_0.json"
  112. dataset_iterator_file = str(tmp_path) + "/dataset_iterator_profiling_0.txt"
  113. # Confirm the content of the profiling files, including 4 ops in the pipeline JSON file
  114. self.confirm_pipeline_file(pipeline_file, 4, ["GeneratorOp", "BatchOp", "MapOp", "EpochCtrlOp"])
  115. self.confirm_cpuutil_file(cpu_util_file, 4)
  116. self.confirm_dataset_iterator_file(dataset_iterator_file, 401)
  117. def test_profiling_delayed_start(self, tmp_path):
  118. """
  119. Test MindData Profiling with Delayed Start; profile for subset of iterations
  120. """
  121. def source1():
  122. for i in range(8000):
  123. yield (np.array([i]),)
  124. # Initialize MindData profiling manager
  125. self.md_profiler.init()
  126. # Create this basic and common pipeline
  127. # Leaf/Source-Op -> Map -> Batch
  128. data1 = ds.GeneratorDataset(source1, ["col1"])
  129. type_cast_op = C.TypeCast(mstype.int32)
  130. data1 = data1.map(operations=type_cast_op, input_columns="col1")
  131. data1 = data1.batch(16)
  132. num_iter = 0
  133. # Note: If create_dict_iterator() is called with num_epochs=1, then EpochCtrlOp is not added to the pipeline
  134. for _ in data1.create_dict_iterator(num_epochs=1):
  135. if num_iter == 5:
  136. # Start MindData Profiling
  137. self.md_profiler.start()
  138. elif num_iter == 400:
  139. # Stop MindData Profiling and Save MindData Profiling Output
  140. self.md_profiler.stop()
  141. self.md_profiler.save(str(tmp_path))
  142. num_iter += 1
  143. assert num_iter == 500
  144. pipeline_file = str(tmp_path) + "/pipeline_profiling_0.json"
  145. cpu_util_file = str(tmp_path) + "/minddata_cpu_utilization_0.json"
  146. dataset_iterator_file = str(tmp_path) + "/dataset_iterator_profiling_0.txt"
  147. # Confirm the content of the profiling files, including 3 ops in the pipeline JSON file
  148. self.confirm_pipeline_file(pipeline_file, 3, ["GeneratorOp", "BatchOp", "MapOp"])
  149. self.confirm_cpuutil_file(cpu_util_file, 3)
  150. self.confirm_dataset_iterator_file(dataset_iterator_file, 395)
  151. def test_profiling_multiple_start_stop(self, tmp_path):
  152. """
  153. Test MindData Profiling with Delayed Start and Multiple Start-Stop Sequences
  154. """
  155. def source1():
  156. for i in range(8000):
  157. yield (np.array([i]),)
  158. # Initialize MindData profiling manager
  159. self.md_profiler.init()
  160. # Create this basic and common pipeline
  161. # Leaf/Source-Op -> Map -> Batch
  162. data1 = ds.GeneratorDataset(source1, ["col1"])
  163. type_cast_op = C.TypeCast(mstype.int32)
  164. data1 = data1.map(operations=type_cast_op, input_columns="col1")
  165. data1 = data1.batch(16)
  166. num_iter = 0
  167. # Note: If create_dict_iterator() is called with num_epochs=1, then EpochCtrlOp is not added to the pipeline
  168. for _ in data1.create_dict_iterator(num_epochs=1):
  169. if num_iter == 5:
  170. # Start MindData Profiling
  171. self.md_profiler.start()
  172. elif num_iter == 40:
  173. # Stop MindData Profiling
  174. self.md_profiler.stop()
  175. if num_iter == 200:
  176. # Start MindData Profiling
  177. self.md_profiler.start()
  178. elif num_iter == 400:
  179. # Stop MindData Profiling
  180. self.md_profiler.stop()
  181. num_iter += 1
  182. # Save MindData Profiling Output
  183. self.md_profiler.save(str(tmp_path))
  184. assert num_iter == 500
  185. pipeline_file = str(tmp_path) + "/pipeline_profiling_0.json"
  186. cpu_util_file = str(tmp_path) + "/minddata_cpu_utilization_0.json"
  187. dataset_iterator_file = str(tmp_path) + "/dataset_iterator_profiling_0.txt"
  188. # Confirm the content of the profiling files, including 3 ops in the pipeline JSON file
  189. self.confirm_pipeline_file(pipeline_file, 3, ["GeneratorOp", "BatchOp", "MapOp"])
  190. self.confirm_cpuutil_file(cpu_util_file, 3)
  191. # Note: The dataset iterator file should only contain data for batches 200 to 400
  192. self.confirm_dataset_iterator_file(dataset_iterator_file, 200)
  193. def test_profiling_start_start(self):
  194. """
  195. Test MindData Profiling with Start followed by Start - user error scenario
  196. """
  197. # Initialize MindData profiling manager
  198. self.md_profiler.init()
  199. # Start MindData Profiling
  200. self.md_profiler.start()
  201. with pytest.raises(RuntimeError) as info:
  202. # Reissue Start MindData Profiling
  203. self.md_profiler.start()
  204. assert "MD ProfilingManager is already running." in str(info)
  205. # Stop MindData Profiling
  206. self.md_profiler.stop()
  207. def test_profiling_stop_stop(self, tmp_path):
  208. """
  209. Test MindData Profiling with Stop followed by Stop - user warning scenario
  210. """
  211. # Initialize MindData profiling manager
  212. self.md_profiler.init()
  213. # Start MindData Profiling
  214. self.md_profiler.start()
  215. # Stop MindData Profiling and Save MindData Profiling Output
  216. self.md_profiler.stop()
  217. self.md_profiler.save(str(tmp_path))
  218. # Reissue Stop MindData Profiling
  219. # A warning "MD ProfilingManager had already stopped" is produced.
  220. self.md_profiler.stop()
  221. def test_profiling_stop_nostart(self):
  222. """
  223. Test MindData Profiling with Stop not without prior Start - user error scenario
  224. """
  225. # Initialize MindData profiling manager
  226. self.md_profiler.init()
  227. with pytest.raises(RuntimeError) as info:
  228. # Stop MindData Profiling - without prior Start()
  229. self.md_profiler.stop()
  230. assert "MD ProfilingManager has not started yet." in str(info)
  231. # Start MindData Profiling
  232. self.md_profiler.start()
  233. # Stop MindData Profiling - to return profiler to a healthy state
  234. self.md_profiler.stop()