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 5.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 time
  18. import shutil
  19. import numpy as np
  20. import pytest
  21. import mindspore.context as context
  22. import mindspore.nn as nn
  23. from mindspore import Tensor
  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. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  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.random.randn(1, 3, 3, 4).astype(np.float32)
  39. y = np.random.randn(1, 3, 3, 4).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 = pwd + '/async_dump/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. @pytest.mark.level0
  65. @pytest.mark.platform_arm_ascend_training
  66. @pytest.mark.platform_x86_ascend_training
  67. @pytest.mark.env_onecard
  68. def test_e2e_dump():
  69. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  70. pwd = os.getcwd()
  71. dump_path = pwd + "/e2e_dump"
  72. change_current_dump_json('e2e_dump.json', dump_path)
  73. os.environ['MINDSPORE_DUMP_CONFIG'] = pwd + "/e2e_dump.json"
  74. device_id = context.get_context("device_id")
  75. dump_file_path = pwd + '/e2e_dump/Net/device_{}/iteration_1/'.format(device_id)
  76. if os.path.isdir(dump_path):
  77. shutil.rmtree(dump_path)
  78. add = Net()
  79. add(Tensor(x), Tensor(y))
  80. time.sleep(5)
  81. assert len(os.listdir(dump_file_path)) == 5
  82. class ReluReduceMeanDenseRelu(Cell):
  83. def __init__(self, kernel, bias, in_channel, num_class):
  84. super().__init__()
  85. self.relu = P.ReLU()
  86. self.mean = P.ReduceMean(keep_dims=False)
  87. self.dense = Dense(in_channel, num_class, kernel, bias)
  88. def construct(self, x_):
  89. x_ = self.relu(x_)
  90. x_ = self.mean(x_, (2, 3))
  91. x_ = self.dense(x_)
  92. x_ = self.relu(x_)
  93. return x_
  94. @pytest.mark.level0
  95. @pytest.mark.platform_arm_ascend_training
  96. @pytest.mark.platform_x86_ascend_training
  97. @pytest.mark.env_onecard
  98. def test_async_dump_net_multi_layer_mode1():
  99. test_name = "test_async_dump_net_multi_layer_mode1"
  100. json_file = os.path.join(os.getcwd(), "{}.json".format(test_name))
  101. device_id = context.get_context("device_id")
  102. dump_full_path = os.path.join("/tmp/async_dump/", "{}_{}".format(test_name, device_id))
  103. os.system("rm -rf {}/*".format(dump_full_path))
  104. os.environ["MINDSPORE_DUMP_CONFIG"] = json_file
  105. weight = Tensor(np.ones((1000, 2048)).astype(np.float32))
  106. bias = Tensor(np.ones((1000,)).astype(np.float32))
  107. net = ReluReduceMeanDenseRelu(weight, bias, 2048, 1000)
  108. criterion = SoftmaxCrossEntropyWithLogits(sparse=False)
  109. optimizer = Momentum(learning_rate=0.1, momentum=0.1, params=filter(lambda x: x.requires_grad, net.get_parameters()))
  110. net_with_criterion = WithLossCell(net, criterion)
  111. train_network = TrainOneStepCell(net_with_criterion, optimizer)
  112. train_network.set_train()
  113. inputs = Tensor(np.random.randn(32, 2048, 7, 7).astype(np.float32))
  114. label = Tensor(np.zeros(shape=(32, 1000)).astype(np.float32))
  115. net_dict = train_network(inputs, label)
  116. dump_path = "/tmp/async_dump/{}/device_{}/test_graph_0/0/0/".format(test_name, device_id)
  117. dump_file = os.listdir(dump_path)
  118. dump_file_name = ""
  119. for file in dump_file:
  120. if "SoftmaxCrossEntropyWithLogits" in file:
  121. dump_file_name = file
  122. dump_file_full_path = os.path.join(dump_path, dump_file_name)
  123. npy_path = os.path.join(os.getcwd(), "./{}".format(test_name))
  124. if os.path.exists(npy_path):
  125. shutil.rmtree(npy_path)
  126. os.mkdir(npy_path)
  127. cmd = "python /usr/local/Ascend/toolkit/tools/operator_cmp/compare/msaccucmp.pyc " \
  128. "convert -d {0} -out {1}".format(dump_file_full_path, npy_path)
  129. os.system(cmd)
  130. npy_file_list = os.listdir(npy_path)
  131. dump_result = {}
  132. for file in npy_file_list:
  133. if "output.0.npy" in file:
  134. dump_result["output0"] = np.load(os.path.join(npy_path, file))
  135. for index, value in enumerate(net_dict):
  136. assert value.asnumpy() == dump_result["output0"][index]