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_dataset_helper.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. """test dataset helper."""
  16. import pytest
  17. import numpy as np
  18. import mindspore.context as context
  19. from mindspore.communication.management import init
  20. from mindspore.train.dataset_helper import DatasetHelper
  21. from mindspore.communication._comm_helper import GlobalComm
  22. from ....dataset_mock import MindData
  23. def get_dataset(batch_size=1):
  24. dataset_types = (np.int32, np.int32, np.int32, np.int32, np.int32, np.int32, np.int32)
  25. dataset_shapes = ((batch_size, 128), (batch_size, 128), (batch_size, 128), (batch_size, 1),
  26. (batch_size, 20), (batch_size, 20), (batch_size, 20))
  27. dataset = MindData(size=2, batch_size=batch_size, np_types=dataset_types,
  28. output_shapes=dataset_shapes, input_indexs=(0, 1))
  29. return dataset
  30. def test_dataset_helper_dataset_sink_mode_str():
  31. dataset = get_dataset(32)
  32. with pytest.raises(TypeError):
  33. DatasetHelper(dataset, dataset_sink_mode="True")
  34. def test_dataset_helper_dataset_sink_mode_int():
  35. dataset = get_dataset(32)
  36. with pytest.raises(TypeError):
  37. DatasetHelper(dataset, dataset_sink_mode=1)
  38. def test_dataset_helper_sink_size_bool():
  39. dataset = get_dataset(32)
  40. with pytest.raises(TypeError):
  41. DatasetHelper(dataset, dataset_sink_mode=True, sink_size=True)
  42. def test_dataset_helper_sink_size_float():
  43. dataset = get_dataset(32)
  44. with pytest.raises(TypeError):
  45. DatasetHelper(dataset, dataset_sink_mode=True, sink_size=1.0)
  46. def test_dataset_helper_sink_size_negative():
  47. dataset = get_dataset(32)
  48. with pytest.raises(ValueError):
  49. DatasetHelper(dataset, dataset_sink_mode=True, sink_size=-2)
  50. def test_dataset_iter_normal():
  51. dataset = get_dataset(32)
  52. dataset_helper = DatasetHelper(dataset, dataset_sink_mode=False)
  53. count = 0
  54. for _ in range(2):
  55. for _ in dataset_helper:
  56. count += 1
  57. dataset.reset()
  58. assert count == 6
  59. @pytest.mark.skipif('not context.get_context("enable_ge")')
  60. def test_dataset_iter_ge():
  61. GlobalComm.CHECK_ENVS = False
  62. init("hccl")
  63. GlobalComm.CHECK_ENVS = True
  64. dataset = get_dataset(32)
  65. dataset_helper = DatasetHelper(dataset, dataset_sink_mode=True, sink_size=10)
  66. count = 0
  67. for _ in range(2):
  68. for _ in dataset_helper:
  69. count += 1
  70. assert count == 2
  71. @pytest.mark.skipif('context.get_context("enable_ge")')
  72. def test_dataset_iter_ms_loop_sink():
  73. GlobalComm.CHECK_ENVS = False
  74. init("hccl")
  75. GlobalComm.CHECK_ENVS = True
  76. context.set_context(enable_loop_sink=True)
  77. dataset = get_dataset(32)
  78. dataset_helper = DatasetHelper(dataset, dataset_sink_mode=True, sink_size=10)
  79. count = 0
  80. for _ in range(2):
  81. for inputs in dataset_helper:
  82. count += 1
  83. assert inputs == tuple()
  84. assert count == 2
  85. @pytest.mark.skipif('context.get_context("enable_ge")')
  86. def test_dataset_iter_ms():
  87. GlobalComm.CHECK_ENVS = False
  88. init("hccl")
  89. GlobalComm.CHECK_ENVS = True
  90. context.set_context(enable_loop_sink=False)
  91. dataset = get_dataset(32)
  92. DatasetHelper(dataset, dataset_sink_mode=True, sink_size=10)