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_gpu_summary.py 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright 2019 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 pytest
  16. import os
  17. import time
  18. import shutil
  19. import random
  20. import numpy as np
  21. import mindspore.nn as nn
  22. import mindspore.context as context
  23. from mindspore.common.tensor import Tensor
  24. from mindspore.ops import operations as P
  25. from mindspore.train.summary.summary_record import SummaryRecord
  26. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  27. CUR_DIR = os.getcwd()
  28. SUMMARY_DIR_ME = CUR_DIR + "/test_me_summary_event_file/"
  29. SUMMARY_DIR_ME_TEMP = CUR_DIR + "/test_me_temp_summary_event_file/"
  30. def clean_environment_file(srcDir):
  31. if os.path.exists(srcDir):
  32. ls = os.listdir(srcDir)
  33. for line in ls:
  34. filePath = os.path.join(srcDir, line)
  35. os.remove(filePath)
  36. os.removedirs(srcDir)
  37. def save_summary_events_file(srcDir, desDir):
  38. if not os.path.exists(desDir):
  39. print("-- create desDir")
  40. os.makedirs(desDir)
  41. ls = os.listdir(srcDir)
  42. for line in ls:
  43. filePath = os.path.join(srcDir, line)
  44. if os.path.isfile(filePath):
  45. print("-- move events file : {}".format(filePath))
  46. shutil.copy(filePath, desDir)
  47. os.remove(filePath)
  48. os.removedirs(srcDir)
  49. class SummaryNet(nn.Cell):
  50. def __init__(self, tag_tuple=None, scalar=1):
  51. super(SummaryNet, self).__init__()
  52. self.summary_s = P.ScalarSummary()
  53. self.summary_i = P.ImageSummary()
  54. self.summary_t = P.TensorSummary()
  55. self.histogram_summary = P.HistogramSummary()
  56. self.add = P.TensorAdd()
  57. self.tag_tuple = tag_tuple
  58. self.scalar = scalar
  59. def construct(self, x, y):
  60. self.summary_i("image", x)
  61. self.summary_s("x1", x)
  62. z = self.add(x, y)
  63. self.summary_t("z1", z)
  64. self.histogram_summary("histogram", z)
  65. return z
  66. def train_summary_record_scalar_for_1(test_writer, steps, fwd_x, fwd_y):
  67. net = SummaryNet()
  68. out_me_dict = {}
  69. for i in range(0, steps):
  70. x = Tensor(np.array([1.1 + random.uniform(1, 10)]).astype(np.float32))
  71. y = Tensor(np.array([1.2 + random.uniform(1, 10)]).astype(np.float32))
  72. out_put = net(x, y)
  73. test_writer.record(i)
  74. print("-----------------output: %s-------------\n", out_put.asnumpy())
  75. out_me_dict[i] = out_put.asnumpy()
  76. return out_me_dict
  77. def me_scalar_summary(steps, tag=None, value=None):
  78. test_writer = SummaryRecord(SUMMARY_DIR_ME_TEMP)
  79. x = Tensor(np.array([1.1]).astype(np.float32))
  80. y = Tensor(np.array([1.2]).astype(np.float32))
  81. out_me_dict = train_summary_record_scalar_for_1(test_writer, steps, x, y)
  82. test_writer.close()
  83. return out_me_dict
  84. @pytest.mark.level0
  85. @pytest.mark.platform_x86_gpu_training
  86. @pytest.mark.env_onecard
  87. def test_scalarsummary_scalar1_step10_summaryrecord1():
  88. clean_environment_file(SUMMARY_DIR_ME_TEMP)
  89. output_dict = me_scalar_summary(10)
  90. print("test_scalarsummary_scalar1_step10_summaryrecord1 \n",output_dict)
  91. save_summary_events_file(SUMMARY_DIR_ME_TEMP, SUMMARY_DIR_ME)
  92. clean_environment_file(SUMMARY_DIR_ME)