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

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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():
  73. context.set_context(device_target='Ascend', mode=context.GRAPH_MODE)
  74. GlobalComm.CHECK_ENVS = False
  75. init("hccl")
  76. GlobalComm.CHECK_ENVS = True
  77. dataset = get_dataset(32)
  78. DatasetHelper(dataset, dataset_sink_mode=True, sink_size=10)