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_data_dump.py 8.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. # Copyright 2020 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 json
  17. import sys
  18. import time
  19. import shutil
  20. import numpy as np
  21. import pytest
  22. import mindspore.context as context
  23. import mindspore.nn as nn
  24. from mindspore import Tensor
  25. from mindspore.ops import operations as P
  26. from mindspore.nn import Cell
  27. from mindspore.nn import Dense
  28. from mindspore.nn import SoftmaxCrossEntropyWithLogits
  29. from mindspore.nn import Momentum
  30. from mindspore.nn import TrainOneStepCell
  31. from mindspore.nn import WithLossCell
  32. class Net(nn.Cell):
  33. def __init__(self):
  34. super(Net, self).__init__()
  35. self.add = P.Add()
  36. def construct(self, x_, y_):
  37. return self.add(x_, y_)
  38. x = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
  39. y = np.array([[7, 8, 9], [10, 11, 12]]).astype(np.float32)
  40. def change_current_dump_json(file_name, dump_path):
  41. with open(file_name, 'r+') as f:
  42. data = json.load(f)
  43. data["common_dump_settings"]["path"] = dump_path
  44. with open(file_name, 'w') as f:
  45. json.dump(data, f)
  46. @pytest.mark.level0
  47. @pytest.mark.platform_arm_ascend_training
  48. @pytest.mark.platform_x86_ascend_training
  49. @pytest.mark.env_onecard
  50. def test_async_dump():
  51. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  52. pwd = os.getcwd()
  53. dump_path = pwd + "/async_dump"
  54. change_current_dump_json('async_dump.json', dump_path)
  55. os.environ['MINDSPORE_DUMP_CONFIG'] = pwd + "/async_dump.json"
  56. device_id = context.get_context("device_id")
  57. dump_file_path = dump_path + '/device_{}/Net_graph_0/0/0/'.format(device_id)
  58. if os.path.isdir(dump_path):
  59. shutil.rmtree(dump_path)
  60. add = Net()
  61. add(Tensor(x), Tensor(y))
  62. time.sleep(5)
  63. assert len(os.listdir(dump_file_path)) == 1
  64. def run_e2e_dump_bin():
  65. if sys.platform != 'linux':
  66. return
  67. pwd = os.getcwd()
  68. dump_path = pwd + '/e2e_dump'
  69. change_current_dump_json('e2e_dump_bin.json', dump_path)
  70. os.environ['MINDSPORE_DUMP_CONFIG'] = pwd + '/e2e_dump_bin.json'
  71. device_id = context.get_context("device_id")
  72. dump_file_path = dump_path + '/rank_{}/Net/graph_0/iteration_1/'.format(device_id)
  73. if os.path.isdir(dump_path):
  74. shutil.rmtree(dump_path)
  75. add = Net()
  76. add(Tensor(x), Tensor(y))
  77. if context.get_context("device_target") == "Ascend":
  78. output_name = "Default--Add-op1_output_0_shape_2_3_Float32_DefaultFormat.bin"
  79. else:
  80. output_name = "Default--Add-op3_output_0_shape_2_3_Float32_DefaultFormat.bin"
  81. output_path = dump_file_path + output_name
  82. real_path = os.path.realpath(output_path)
  83. output = np.fromfile(real_path, dtype=np.float32)
  84. expect = np.array([8, 10, 12, 14, 16, 18], np.float32)
  85. assert output.dtype == expect.dtype
  86. assert np.array_equal(output, expect)
  87. def run_e2e_dump_npy():
  88. if sys.platform != 'linux':
  89. return
  90. pwd = os.getcwd()
  91. dump_path = pwd + '/e2e_dump'
  92. change_current_dump_json('e2e_dump_npy.json', dump_path)
  93. os.environ['MINDSPORE_DUMP_CONFIG'] = pwd + '/e2e_dump_npy.json'
  94. device_id = context.get_context("device_id")
  95. dump_file_path = dump_path + '/rank_{}/Net/graph_0/iteration_1/'.format(device_id)
  96. if os.path.isdir(dump_path):
  97. shutil.rmtree(dump_path)
  98. add = Net()
  99. add(Tensor(x), Tensor(y))
  100. if context.get_context("device_target") == "Ascend":
  101. output_name = "Default--Add-op1_output_0_shape_2_3_Float32_DefaultFormat.npy"
  102. else:
  103. output_name = "Default--Add-op3_output_0_shape_2_3_Float32_DefaultFormat.npy"
  104. output_path = dump_file_path + output_name
  105. real_path = os.path.realpath(output_path)
  106. output = np.load(real_path)
  107. expect = np.array([[8, 10, 12], [14, 16, 18]], np.float32)
  108. assert output.dtype == expect.dtype
  109. assert np.array_equal(output, expect)
  110. @pytest.mark.level0
  111. @pytest.mark.platform_arm_ascend_training
  112. @pytest.mark.platform_x86_ascend_training
  113. @pytest.mark.env_onecard
  114. def test_e2e_dump_bin():
  115. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  116. run_e2e_dump_bin()
  117. @pytest.mark.level0
  118. @pytest.mark.platform_arm_ascend_training
  119. @pytest.mark.platform_x86_ascend_training
  120. @pytest.mark.env_onecard
  121. def test_e2e_dump_npy():
  122. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  123. run_e2e_dump_npy()
  124. @pytest.mark.level0
  125. @pytest.mark.platform_x86_cpu
  126. @pytest.mark.env_onecard
  127. def test_cpu_e2e_dump_bin():
  128. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  129. run_e2e_dump_bin()
  130. @pytest.mark.level0
  131. @pytest.mark.platform_x86_cpu
  132. @pytest.mark.env_onecard
  133. def test_cpu_e2e_dump_npy():
  134. context.set_context(mode=context.GRAPH_MODE, save_graphs=True, device_target="CPU")
  135. run_e2e_dump_npy()
  136. class ReluReduceMeanDenseRelu(Cell):
  137. def __init__(self, kernel, bias, in_channel, num_class):
  138. super().__init__()
  139. self.relu = P.ReLU()
  140. self.mean = P.ReduceMean(keep_dims=False)
  141. self.dense = Dense(in_channel, num_class, kernel, bias)
  142. def construct(self, x_):
  143. x_ = self.relu(x_)
  144. x_ = self.mean(x_, (2, 3))
  145. x_ = self.dense(x_)
  146. x_ = self.relu(x_)
  147. return x_
  148. def search_path(path, keyword):
  149. content = os.listdir(path)
  150. for each in content:
  151. each_path = path + os.sep + each
  152. if keyword in each:
  153. return each_path
  154. read_write = os.access(each_path, os.W_OK) and os.access(each_path, os.R_OK)
  155. if not read_write:
  156. continue
  157. if os.path.isdir(each_path):
  158. search_path(each_path, keyword)
  159. return None
  160. @pytest.mark.level0
  161. @pytest.mark.platform_arm_ascend_training
  162. @pytest.mark.platform_x86_ascend_training
  163. @pytest.mark.env_onecard
  164. def test_async_dump_net_multi_layer_mode1():
  165. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  166. test_name = "test_async_dump_net_multi_layer_mode1"
  167. json_file = os.path.join(os.getcwd(), "{}.json".format(test_name))
  168. device_id = context.get_context("device_id")
  169. dump_full_path = os.path.join("/tmp/async_dump/", "{}_{}".format(test_name, device_id))
  170. os.system("rm -rf {}/*".format(dump_full_path))
  171. os.environ["MINDSPORE_DUMP_CONFIG"] = json_file
  172. weight = Tensor(np.ones((1000, 2048)).astype(np.float32))
  173. bias = Tensor(np.ones((1000,)).astype(np.float32))
  174. net = ReluReduceMeanDenseRelu(weight, bias, 2048, 1000)
  175. criterion = SoftmaxCrossEntropyWithLogits(sparse=False)
  176. optimizer = Momentum(learning_rate=0.1, momentum=0.1,
  177. params=filter(lambda x: x.requires_grad, net.get_parameters()))
  178. net_with_criterion = WithLossCell(net, criterion)
  179. train_network = TrainOneStepCell(net_with_criterion, optimizer)
  180. train_network.set_train()
  181. inputs = Tensor(np.random.randn(32, 2048, 7, 7).astype(np.float32))
  182. label = Tensor(np.zeros(shape=(32, 1000)).astype(np.float32))
  183. net_dict = train_network(inputs, label)
  184. dump_path = "/tmp/async_dump/{}/device_{}/test_graph_0/0/0/".format(test_name, device_id)
  185. dump_file = os.listdir(dump_path)
  186. dump_file_name = ""
  187. for file in dump_file:
  188. if "SoftmaxCrossEntropyWithLogits" in file:
  189. dump_file_name = file
  190. dump_file_full_path = os.path.join(dump_path, dump_file_name)
  191. npy_path = os.path.join(os.getcwd(), "./{}".format(test_name))
  192. if os.path.exists(npy_path):
  193. shutil.rmtree(npy_path)
  194. os.mkdir(npy_path)
  195. tool_path = search_path('/usr/local/Ascend', 'msaccucmp.pyc')
  196. if tool_path:
  197. cmd = "python {0} convert -d {1} -out {2}".format(tool_path, dump_file_full_path, npy_path)
  198. os.system(cmd)
  199. npy_file_list = os.listdir(npy_path)
  200. dump_result = {}
  201. for file in npy_file_list:
  202. if "output.0.npy" in file:
  203. dump_result["output0"] = np.load(os.path.join(npy_path, file))
  204. for index, value in enumerate(net_dict):
  205. assert value.asnumpy() == dump_result["output0"][index]
  206. else:
  207. print('not find convert tools msaccucmp.pyc')