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

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