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.

distributed_sampler_test.cc 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common/common.h"
  17. #include "gtest/gtest.h"
  18. #include "minddata/dataset/include/constants.h"
  19. #include "minddata/dataset/core/tensor.h"
  20. #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h"
  21. #include "minddata/dataset/engine/datasetops/source/sampler/distributed_sampler.h"
  22. #include "utils/log_adapter.h"
  23. #include <vector>
  24. #include <unordered_set>
  25. using namespace mindspore::dataset;
  26. using mindspore::MsLogLevel::INFO;
  27. using mindspore::ExceptionType::NoExceptionType;
  28. using mindspore::LogStream;
  29. class MindDataTestDistributedSampler : public UT::Common {
  30. public:
  31. class DummyRandomAccessOp : public RandomAccessOp {
  32. public:
  33. DummyRandomAccessOp(uint64_t num_rows) {
  34. // row count is in base class as protected member
  35. // GetNumRowsInDataset does not need an override, the default from base class is fine.
  36. num_rows_ = num_rows;
  37. }
  38. };
  39. };
  40. TEST_F(MindDataTestDistributedSampler, TestTwoShardsOne) {
  41. // num samples to draw.
  42. uint64_t num_samples = 7;
  43. // create sampler with replacement = true
  44. DistributedSamplerRT m_sampler(num_samples, 2, 0, false, 0, -1, false);
  45. DummyRandomAccessOp dummyRandomAccessOp(num_samples);
  46. m_sampler.HandshakeRandomAccessOp(&dummyRandomAccessOp);
  47. TensorRow row;
  48. std::vector<uint64_t> out;
  49. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  50. for (const auto &t : row) {
  51. for (auto it = t->begin<uint64_t>(); it != t->end<uint64_t>(); it++) {
  52. out.push_back(*it);
  53. }
  54. }
  55. ASSERT_EQ(4, out.size());
  56. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  57. ASSERT_EQ(row.eoe(), true);
  58. }
  59. TEST_F(MindDataTestDistributedSampler, TestTwoShardsTwo) {
  60. // num samples to draw.
  61. uint64_t num_samples = 7;
  62. // create sampler with replacement = true
  63. DistributedSamplerRT m_sampler(num_samples, 2, 1, false, 0, -1, false);
  64. DummyRandomAccessOp dummyRandomAccessOp(num_samples);
  65. m_sampler.HandshakeRandomAccessOp(&dummyRandomAccessOp);
  66. TensorRow row;
  67. std::vector<uint64_t> out;
  68. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  69. for (const auto &t : row) {
  70. for (auto it = t->begin<uint64_t>(); it != t->end<uint64_t>(); it++) {
  71. out.push_back(*it);
  72. }
  73. }
  74. ASSERT_EQ(3, out.size());
  75. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  76. ASSERT_EQ(row.eoe(), true);
  77. }
  78. TEST_F(MindDataTestDistributedSampler, TestThreeShards) {
  79. // num samples to draw.
  80. uint64_t num_samples = 2;
  81. // create sampler with replacement = true
  82. DistributedSamplerRT m_sampler(num_samples, 3, 2, false, 0, -1, false);
  83. DummyRandomAccessOp dummyRandomAccessOp(num_samples);
  84. m_sampler.HandshakeRandomAccessOp(&dummyRandomAccessOp);
  85. TensorRow row;
  86. std::vector<uint64_t> out;
  87. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  88. for (const auto &t : row) {
  89. for (auto it = t->begin<uint64_t>(); it != t->end<uint64_t>(); it++) {
  90. out.push_back(*it);
  91. }
  92. }
  93. ASSERT_EQ(0, out.size());
  94. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  95. ASSERT_EQ(row.eoe(), true);
  96. }