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_autotune.py 9.3 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. Testing Autotune support in DE
  17. """
  18. import numpy as np
  19. import pytest
  20. import mindspore._c_dataengine as cde
  21. import mindspore.dataset as ds
  22. # pylint: disable=unused-variable
  23. @pytest.mark.forked
  24. class TestAutotuneWithProfiler:
  25. def test_autotune_after_profiler_with_1_pipeline(self, capfd):
  26. """
  27. Feature: Autotuning with Profiler
  28. Description: Test Autotune enabled together with MD Profiler with a single pipeline
  29. Expectation: Enable MD Profiler and print appropriate warning logs when trying to enable Autotune
  30. """
  31. md_profiler = cde.GlobalContext.profiling_manager()
  32. md_profiler.init()
  33. md_profiler.start()
  34. ds.config.set_enable_autotune(True)
  35. source = [(np.array([x]),) for x in range(1024)]
  36. data1 = ds.GeneratorDataset(source, ["data"])
  37. data1 = data1.shuffle(64)
  38. data1 = data1.batch(32)
  39. itr1 = data1.create_dict_iterator(num_epochs=5)
  40. _, err = capfd.readouterr()
  41. assert "Cannot enable AutoTune for the current data pipeline as Dataset Profiling is already enabled for the " \
  42. "current data pipeline." in err
  43. # Uncomment the following two lines to see complete stdout and stderr output in pytest summary output
  44. # sys.stdout.write(_)
  45. # sys.stderr.write(err)
  46. md_profiler.stop()
  47. ds.config.set_enable_autotune(False)
  48. def test_autotune_after_profiler_with_2_pipeline(self, capfd):
  49. """
  50. Feature: Autotuning with Profiler
  51. Description: Test Autotune enabled together with MD Profiler with two pipelines
  52. Expectation: Enable MD Profiler for first tree and print appropriate warning log when trying to
  53. enable Autotune for the first tree. Print appropriate warning logs when trying to enable both MD Profiler
  54. and Autotune for second tree.
  55. """
  56. md_profiler = cde.GlobalContext.profiling_manager()
  57. md_profiler.init()
  58. md_profiler.start()
  59. ds.config.set_enable_autotune(True)
  60. source = [(np.array([x]),) for x in range(1024)]
  61. data1 = ds.GeneratorDataset(source, ["data"])
  62. data1 = data1.shuffle(64)
  63. data1 = data1.batch(32)
  64. itr1 = data1.create_dict_iterator(num_epochs=5)
  65. _, err = capfd.readouterr()
  66. assert "Cannot enable AutoTune for the current data pipeline as Dataset Profiling is already enabled for the " \
  67. "current data pipeline." in err
  68. # Uncomment the following two lines to see complete stdout and stderr output in pytest summary output
  69. # sys.stdout.write(_)
  70. # sys.stderr.write(err)
  71. itr2 = data1.create_dict_iterator(num_epochs=5)
  72. _, err = capfd.readouterr()
  73. assert "Dataset Profiling is already enabled for a different data pipeline." in err
  74. assert "Cannot enable AutoTune for the current data pipeline as Dataset Profiling is enabled for another data" \
  75. " pipeline." in err
  76. # Uncomment the following two lines to see complete stdout and stderr output in pytest summary output
  77. # sys.stdout.write(_)
  78. # sys.stderr.write(err)
  79. md_profiler.stop()
  80. ds.config.set_enable_autotune(True)
  81. def test_autotune_with_2_pipeline(self, capfd):
  82. """
  83. Feature: Autotuning
  84. Description: Test Autotune two pipelines
  85. Expectation: Enable MD Profiler and print appropriate warning logs
  86. when trying to enable Autotune for second tree.
  87. """
  88. ds.config.set_enable_autotune(True)
  89. source = [(np.array([x]),) for x in range(1024)]
  90. data1 = ds.GeneratorDataset(source, ["data"])
  91. data1 = data1.shuffle(64)
  92. data1 = data1.batch(32)
  93. itr1 = data1.create_dict_iterator(num_epochs=5)
  94. itr2 = data1.create_dict_iterator(num_epochs=5)
  95. _, err = capfd.readouterr()
  96. assert "Cannot enable AutoTune for the current data pipeline as it is already enabled for another data " \
  97. "pipeline." in err
  98. # Uncomment the following two lines to see complete stdout and stderr output in pytest summary output
  99. # sys.stdout.write(_)
  100. # sys.stderr.write(err)
  101. ds.config.set_enable_autotune(False)
  102. def test_delayed_autotune_with_2_pipeline(self, capfd):
  103. """
  104. Feature: Autotuning
  105. Description: Test delayed Autotune with two pipelines
  106. Expectation: Enable MD Profiler for second tree and no warnings logs should be printed.
  107. """
  108. source = [(np.array([x]),) for x in range(1024)]
  109. data1 = ds.GeneratorDataset(source, ["data"])
  110. data1 = data1.shuffle(64)
  111. data1 = data1.batch(32)
  112. itr1 = data1.create_dict_iterator(num_epochs=5)
  113. ds.config.set_enable_autotune(True)
  114. itr2 = data1.create_dict_iterator(num_epochs=5)
  115. ds.config.set_enable_autotune(False)
  116. _, err = capfd.readouterr()
  117. assert err == ''
  118. # Uncomment the following two lines to see complete stdout and stderr output in pytest summary output
  119. # sys.stdout.write(_)
  120. # sys.stderr.write(err)
  121. def test_delayed_start_autotune_with_3_pipeline(self, capfd):
  122. """
  123. Feature: Autotuning
  124. Description: Test delayed Autotune and early stop with three pipelines
  125. Expectation: Enable MD Profiler for second tree and no warnings logs should be printed.
  126. """
  127. source = [(np.array([x]),) for x in range(1024)]
  128. data1 = ds.GeneratorDataset(source, ["data"])
  129. data1 = data1.shuffle(64)
  130. data1 = data1.batch(32)
  131. itr1 = data1.create_dict_iterator(num_epochs=5)
  132. ds.config.set_enable_autotune(True)
  133. itr2 = data1.create_dict_iterator(num_epochs=5)
  134. ds.config.set_enable_autotune(False)
  135. itr3 = data1.create_dict_iterator(num_epochs=5)
  136. _, err = capfd.readouterr()
  137. assert err == ''
  138. # Uncomment the following two lines to see complete stdout and stderr output in pytest summary output
  139. # sys.stdout.write(_)
  140. # sys.stderr.write(err)
  141. def test_autotune_before_profiler(self):
  142. """
  143. Feature: Autotuning with Profiler
  144. Description: Test Autotune with Profiler when Profiler is Initialized after autotune
  145. Expectation: Initialization of Profiler should throw an error.
  146. """
  147. # enable AT for 1st tree
  148. # profiler init should throw an error
  149. source = [(np.array([x]),) for x in range(1024)]
  150. data1 = ds.GeneratorDataset(source, ["data"])
  151. data1 = data1.shuffle(64)
  152. data1 = data1.batch(32)
  153. ds.config.set_enable_autotune(True)
  154. itr1 = data1.create_dict_iterator(num_epochs=5)
  155. ds.config.set_enable_autotune(False)
  156. md_profiler = cde.GlobalContext.profiling_manager()
  157. with pytest.raises(RuntimeError) as excinfo:
  158. md_profiler.init()
  159. assert "Unexpected error. Stop MD Autotune before initializing the MD Profiler." in str(excinfo.value)
  160. def test_autotune_simple_pipeline(self):
  161. """
  162. Feature: Autotuning
  163. Description: test simple pipeline of autotune - Generator -> Shuffle -> Batch
  164. Expectation: pipeline runs successfully
  165. """
  166. ds.config.set_enable_autotune(True)
  167. source = [(np.array([x]),) for x in range(1024)]
  168. data1 = ds.GeneratorDataset(source, ["data"])
  169. data1 = data1.shuffle(64)
  170. data1 = data1.batch(32)
  171. itr = data1.create_dict_iterator(num_epochs=5)
  172. for _ in range(5):
  173. for _ in itr:
  174. pass
  175. ds.config.set_enable_autotune(False)
  176. def test_autotune_config(self):
  177. """
  178. Feature: Autotuning
  179. Description: test basic config of autotune
  180. Expectation: config can be set successfully
  181. """
  182. autotune_state = ds.config.get_enable_autotune()
  183. assert autotune_state is False
  184. ds.config.set_enable_autotune(False)
  185. autotune_state = ds.config.get_enable_autotune()
  186. assert autotune_state is False
  187. with pytest.raises(TypeError):
  188. ds.config.set_enable_autotune(1)
  189. autotune_interval = ds.config.get_autotune_interval()
  190. assert autotune_interval == 100
  191. ds.config.set_autotune_interval(200)
  192. autotune_interval = ds.config.get_autotune_interval()
  193. assert autotune_interval == 200
  194. with pytest.raises(TypeError):
  195. ds.config.set_autotune_interval(20.012)
  196. with pytest.raises(ValueError):
  197. ds.config.set_autotune_interval(-999)