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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. import os
  16. import sys
  17. import tempfile
  18. import time
  19. import shutil
  20. import glob
  21. import numpy as np
  22. import pytest
  23. from mindspore import Tensor, set_dump
  24. from mindspore.ops import operations as P
  25. from mindspore.nn import Cell
  26. from mindspore.nn import Dense
  27. from mindspore.nn import SoftmaxCrossEntropyWithLogits
  28. from mindspore.nn import Momentum
  29. from mindspore.nn import TrainOneStepCell
  30. from mindspore.nn import WithLossCell
  31. from dump_test_utils import generate_cell_dump_json, check_dump_structure
  32. from tests.security_utils import security_off_wrap
  33. class ReluReduceMeanDenseRelu(Cell):
  34. def __init__(self, kernel, bias, in_channel, num_class):
  35. super().__init__()
  36. self.relu = P.ReLU()
  37. self.mean = P.ReduceMean(keep_dims=False)
  38. self.dense = Dense(in_channel, num_class, kernel, bias)
  39. def construct(self, x_):
  40. x_ = self.relu(x_)
  41. x_ = self.mean(x_, (2, 3))
  42. x_ = self.dense(x_)
  43. x_ = self.relu(x_)
  44. return x_
  45. def run_multi_layer_train(is_set_dump):
  46. weight = Tensor(np.ones((1000, 2048)).astype(np.float32))
  47. bias = Tensor(np.ones((1000,)).astype(np.float32))
  48. net = ReluReduceMeanDenseRelu(weight, bias, 2048, 1000)
  49. if is_set_dump:
  50. set_dump(net.relu)
  51. criterion = SoftmaxCrossEntropyWithLogits(sparse=False)
  52. optimizer = Momentum(learning_rate=0.1, momentum=0.1,
  53. params=filter(lambda x: x.requires_grad, net.get_parameters()))
  54. net_with_criterion = WithLossCell(net, criterion)
  55. train_network = TrainOneStepCell(net_with_criterion, optimizer)
  56. train_network.set_train()
  57. inputs = Tensor(np.random.randn(32, 2048, 7, 7).astype(np.float32))
  58. label = Tensor(np.zeros(shape=(32, 1000)).astype(np.float32))
  59. train_network(inputs, label)
  60. @pytest.mark.level0
  61. @pytest.mark.platform_arm_ascend_training
  62. @pytest.mark.platform_x86_ascend_training
  63. @pytest.mark.env_onecard
  64. @security_off_wrap
  65. def test_ascend_cell_dump():
  66. """
  67. Feature: Cell Dump
  68. Description: Test cell dump
  69. Expectation: Only dump cell set by set_dump when dump_mode = 2
  70. """
  71. if sys.platform != 'linux':
  72. return
  73. with tempfile.TemporaryDirectory(dir='/tmp') as tmp_dir:
  74. dump_path = os.path.join(tmp_dir, 'cell_dump')
  75. dump_config_path = os.path.join(tmp_dir, 'cell_dump.json')
  76. generate_cell_dump_json(dump_path, dump_config_path, 'test_async_dump', 2)
  77. os.environ['MINDSPORE_DUMP_CONFIG'] = dump_config_path
  78. if os.path.isdir(dump_path):
  79. shutil.rmtree(dump_path)
  80. run_multi_layer_train(True)
  81. dump_file_path = os.path.join(dump_path, 'rank_0', 'Net', '0', '0')
  82. for _ in range(5):
  83. if not os.path.exists(dump_file_path):
  84. time.sleep(2)
  85. check_dump_structure(dump_path, dump_config_path, 1, 1, 1)
  86. # make sure 2 relu dump files are generated with correct name prefix
  87. assert len(os.listdir(dump_file_path)) == 2
  88. relu_file_name = "ReLU.Default_network-WithLossCell__backbone-ReluReduceMeanDenseRelu_ReLU-op*.*.*.*"
  89. relu_file1 = glob.glob(os.path.join(dump_file_path, relu_file_name))[0]
  90. relu_file2 = glob.glob(os.path.join(dump_file_path, relu_file_name))[1]
  91. assert relu_file1
  92. assert relu_file2
  93. del os.environ['MINDSPORE_DUMP_CONFIG']
  94. @pytest.mark.level0
  95. @pytest.mark.platform_arm_ascend_training
  96. @pytest.mark.platform_x86_ascend_training
  97. @pytest.mark.env_onecard
  98. @security_off_wrap
  99. def test_ascend_not_cell_dump():
  100. """
  101. Feature: Cell Dump
  102. Description: Test cell dump
  103. Expectation: Should ignore set_dump when dump_mode != 2
  104. """
  105. if sys.platform != 'linux':
  106. return
  107. with tempfile.TemporaryDirectory(dir='/tmp') as tmp_dir:
  108. dump_path = os.path.join(tmp_dir, 'cell_dump')
  109. dump_config_path = os.path.join(tmp_dir, 'cell_dump.json')
  110. generate_cell_dump_json(dump_path, dump_config_path, 'test_async_dump', 0)
  111. os.environ['MINDSPORE_DUMP_CONFIG'] = dump_config_path
  112. if os.path.isdir(dump_path):
  113. shutil.rmtree(dump_path)
  114. run_multi_layer_train(True)
  115. dump_file_path = os.path.join(dump_path, 'rank_0', 'Net', '0', '0')
  116. for _ in range(5):
  117. if not os.path.exists(dump_file_path):
  118. time.sleep(2)
  119. check_dump_structure(dump_path, dump_config_path, 1, 1, 1)
  120. # make sure set_dump is ignored and all cell layer are dumped
  121. assert len(os.listdir(dump_file_path)) == 10
  122. del os.environ['MINDSPORE_DUMP_CONFIG']
  123. @pytest.mark.level0
  124. @pytest.mark.platform_arm_ascend_training
  125. @pytest.mark.platform_x86_ascend_training
  126. @pytest.mark.env_onecard
  127. @security_off_wrap
  128. def test_ascend_cell_empty_dump():
  129. """
  130. Feature: Cell Dump
  131. Description: Test cell dump
  132. Expectation: Should dump nothing when set_dump is not set and dump_mode = 2
  133. """
  134. if sys.platform != 'linux':
  135. return
  136. with tempfile.TemporaryDirectory(dir='/tmp') as tmp_dir:
  137. dump_path = os.path.join(tmp_dir, 'cell_dump')
  138. dump_config_path = os.path.join(tmp_dir, 'cell_dump.json')
  139. generate_cell_dump_json(dump_path, dump_config_path, 'test_async_dump', 2)
  140. os.environ['MINDSPORE_DUMP_CONFIG'] = dump_config_path
  141. if os.path.isdir(dump_path):
  142. shutil.rmtree(dump_path)
  143. run_multi_layer_train(False)
  144. dump_file_path = os.path.join(dump_path, 'rank_0', 'Net')
  145. time.sleep(5)
  146. # make sure set_dump is ignored and all cell layer are dumped
  147. assert not os.path.exists(dump_file_path)
  148. del os.environ['MINDSPORE_DUMP_CONFIG']