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.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. """Summary gpu st."""
  16. import os
  17. import random
  18. import tempfile
  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.common.tensor import Tensor
  25. from mindspore.ops import operations as P
  26. from mindspore.train.summary.summary_record import SummaryRecord
  27. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  28. class SummaryNet(nn.Cell):
  29. """Summary net."""
  30. def __init__(self, tag_tuple=None, scalar=1):
  31. super(SummaryNet, self).__init__()
  32. self.summary_s = P.ScalarSummary()
  33. self.summary_i = P.ImageSummary()
  34. self.summary_t = P.TensorSummary()
  35. self.histogram_summary = P.HistogramSummary()
  36. self.add = P.TensorAdd()
  37. self.tag_tuple = tag_tuple
  38. self.scalar = scalar
  39. def construct(self, x, y, image):
  40. """Run summary net."""
  41. self.summary_i("image", image)
  42. self.summary_s("x1", x)
  43. z = self.add(x, y)
  44. self.summary_t("z1", z)
  45. self.histogram_summary("histogram", z)
  46. return z
  47. def train_summary_record(test_writer, steps):
  48. """Train and record summary."""
  49. net = SummaryNet()
  50. out_me_dict = {}
  51. for i in range(0, steps):
  52. x = Tensor(np.array([1.1 + random.uniform(1, 10)]).astype(np.float32))
  53. y = Tensor(np.array([1.2 + random.uniform(1, 10)]).astype(np.float32))
  54. image = Tensor(np.array([[[[1.2]]]]).astype(np.float32))
  55. out_put = net(x, y, image)
  56. test_writer.record(i)
  57. out_me_dict[i] = out_put.asnumpy()
  58. return out_me_dict
  59. class TestGpuSummary:
  60. """Test Gpu summary."""
  61. summary_dir = tempfile.mkdtemp(suffix='_gpu_summary')
  62. def setup_method(self):
  63. """Run before method."""
  64. if not os.path.exists(self.summary_dir):
  65. os.mkdir(self.summary_dir)
  66. def teardown_method(self):
  67. """Run after method."""
  68. if os.path.exists(self.summary_dir):
  69. shutil.rmtree(self.summary_dir)
  70. @pytest.mark.level0
  71. @pytest.mark.platform_x86_gpu_training
  72. @pytest.mark.env_onecard
  73. def test_summary_step10_summaryrecord1(self):
  74. """Test record 10 step summary."""
  75. with SummaryRecord(self.summary_dir) as test_writer:
  76. train_summary_record(test_writer, steps=10)