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_profiler.py 7.9 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 shutil
  17. import pytest
  18. from mindspore import dataset as ds
  19. from mindspore import nn, Tensor, context
  20. from mindspore.nn.metrics import Accuracy
  21. from mindspore.nn.optim import Momentum
  22. from mindspore.dataset.transforms import c_transforms as C
  23. from mindspore.dataset.vision import c_transforms as CV
  24. from mindspore.dataset.vision import Inter
  25. from mindspore.common import dtype as mstype
  26. from mindspore.common.initializer import TruncatedNormal
  27. from mindspore.train import Model
  28. from mindspore.profiler import Profiler
  29. def conv(in_channels, out_channels, kernel_size, stride=1, padding=0):
  30. """weight initial for conv layer"""
  31. weight = weight_variable()
  32. return nn.Conv2d(in_channels, out_channels,
  33. kernel_size=kernel_size, stride=stride, padding=padding,
  34. weight_init=weight, has_bias=False, pad_mode="valid")
  35. def fc_with_initialize(input_channels, out_channels):
  36. """weight initial for fc layer"""
  37. weight = weight_variable()
  38. bias = weight_variable()
  39. return nn.Dense(input_channels, out_channels, weight, bias)
  40. def weight_variable():
  41. """weight initial"""
  42. return TruncatedNormal(0.02)
  43. class LeNet5(nn.Cell):
  44. """Define LeNet5 network."""
  45. def __init__(self, num_class=10, channel=1):
  46. super(LeNet5, self).__init__()
  47. self.num_class = num_class
  48. self.conv1 = conv(channel, 6, 5)
  49. self.conv2 = conv(6, 16, 5)
  50. self.fc1 = fc_with_initialize(16 * 5 * 5, 120)
  51. self.fc2 = fc_with_initialize(120, 84)
  52. self.fc3 = fc_with_initialize(84, self.num_class)
  53. self.relu = nn.ReLU()
  54. self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
  55. self.flatten = nn.Flatten()
  56. self.channel = Tensor(channel)
  57. def construct(self, data):
  58. """define construct."""
  59. output = self.conv1(data)
  60. output = self.relu(output)
  61. output = self.max_pool2d(output)
  62. output = self.conv2(output)
  63. output = self.relu(output)
  64. output = self.max_pool2d(output)
  65. output = self.flatten(output)
  66. output = self.fc1(output)
  67. output = self.relu(output)
  68. output = self.fc2(output)
  69. output = self.relu(output)
  70. output = self.fc3(output)
  71. return output
  72. def create_dataset(data_path, batch_size=32, repeat_size=1, num_parallel_workers=1):
  73. """create dataset for train"""
  74. # define dataset
  75. mnist_ds = ds.MnistDataset(data_path, num_samples=batch_size*100)
  76. resize_height, resize_width = 32, 32
  77. rescale = 1.0 / 255.0
  78. rescale_nml = 1 / 0.3081
  79. shift_nml = -1 * 0.1307 / 0.3081
  80. # define map operations
  81. resize_op = CV.Resize((resize_height, resize_width), interpolation=Inter.LINEAR) # Bilinear mode
  82. rescale_nml_op = CV.Rescale(rescale_nml, shift_nml)
  83. rescale_op = CV.Rescale(rescale, shift=0.0)
  84. hwc2chw_op = CV.HWC2CHW()
  85. type_cast_op = C.TypeCast(mstype.int32)
  86. # apply map operations on images
  87. mnist_ds = mnist_ds.map(operations=type_cast_op, input_columns="label", num_parallel_workers=num_parallel_workers)
  88. mnist_ds = mnist_ds.map(operations=resize_op, input_columns="image", num_parallel_workers=num_parallel_workers)
  89. mnist_ds = mnist_ds.map(operations=rescale_op, input_columns="image", num_parallel_workers=num_parallel_workers)
  90. mnist_ds = mnist_ds.map(operations=rescale_nml_op, input_columns="image", num_parallel_workers=num_parallel_workers)
  91. mnist_ds = mnist_ds.map(operations=hwc2chw_op, input_columns="image", num_parallel_workers=num_parallel_workers)
  92. # apply DatasetOps
  93. mnist_ds = mnist_ds.batch(batch_size, drop_remainder=True)
  94. mnist_ds = mnist_ds.repeat(repeat_size)
  95. return mnist_ds
  96. def cleanup():
  97. data_path = os.path.join(os.getcwd(), "data")
  98. kernel_meta_path = os.path.join(os.getcwd(), "kernel_data")
  99. cache_path = os.path.join(os.getcwd(), "__pycache__")
  100. if os.path.exists(data_path):
  101. shutil.rmtree(data_path)
  102. if os.path.exists(kernel_meta_path):
  103. shutil.rmtree(kernel_meta_path)
  104. if os.path.exists(cache_path):
  105. shutil.rmtree(cache_path)
  106. class TestProfiler:
  107. device_id = int(os.getenv('DEVICE_ID')) if os.getenv('DEVICE_ID') else 0
  108. mnist_path = '/home/workspace/mindspore_dataset/mnist'
  109. profiler_path = os.path.join(os.getcwd(), 'data/profiler/')
  110. @classmethod
  111. def teardown_class(cls):
  112. """ Run after class end."""
  113. cleanup()
  114. @pytest.mark.level0
  115. @pytest.mark.platform_x86_gpu_training
  116. @pytest.mark.env_onecard
  117. def test_gpu_profiler(self):
  118. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  119. profiler = Profiler()
  120. ds_train = create_dataset(os.path.join(self.mnist_path, "train"))
  121. if ds_train.get_dataset_size() == 0:
  122. raise ValueError("Please check dataset size > 0 and batch_size <= dataset size")
  123. lenet = LeNet5()
  124. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  125. optim = Momentum(lenet.trainable_params(), learning_rate=0.1, momentum=0.9)
  126. model = Model(lenet, loss_fn=loss, optimizer=optim, metrics={'acc': Accuracy()})
  127. model.train(1, ds_train, dataset_sink_mode=True)
  128. profiler.analyse()
  129. self._check_gpu_profiling_file()
  130. def _check_gpu_profiling_file(self):
  131. op_detail_file = self.profiler_path + f'gpu_op_detail_info_{self.device_id}.csv'
  132. op_type_file = self.profiler_path + f'gpu_op_type_info_{self.device_id}.csv'
  133. activity_file = self.profiler_path + f'gpu_activity_data_{self.device_id}.csv'
  134. timeline_file = self.profiler_path + f'gpu_timeline_display_{self.device_id}.json'
  135. getnext_file = self.profiler_path + f'minddata_getnext_profiling_{self.device_id}.txt'
  136. pipeline_file = self.profiler_path + f'minddata_pipeline_raw_{self.device_id}.csv'
  137. assert os.path.exists(op_detail_file)
  138. assert os.path.exists(op_type_file)
  139. assert os.path.exists(activity_file)
  140. assert os.path.exists(timeline_file)
  141. assert os.path.exists(getnext_file)
  142. assert os.path.exists(pipeline_file)
  143. def _check_d_profiling_file(self):
  144. aicore_file = self.profiler_path + f'aicore_intermediate_{self.device_id}_detail.csv'
  145. step_trace_file = self.profiler_path + f'step_trace_raw_{self.device_id}_detail_time.csv'
  146. timeline_file = self.profiler_path + f'ascend_timeline_display_{self.device_id}.json'
  147. aicpu_file = self.profiler_path + f'aicpu_intermediate_{self.device_id}.csv'
  148. minddata_pipeline_file = self.profiler_path + f'minddata_pipeline_raw_{self.device_id}.csv'
  149. queue_profiling_file = self.profiler_path + f'device_queue_profiling_{self.device_id}.txt'
  150. assert os.path.exists(aicore_file)
  151. assert os.path.exists(step_trace_file)
  152. assert os.path.exists(timeline_file)
  153. assert os.path.exists(queue_profiling_file)
  154. assert os.path.exists(minddata_pipeline_file)
  155. assert os.path.exists(aicpu_file)