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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/core/constants.h"
  19. #include "minddata/dataset/core/tensor.h"
  20. #include "minddata/dataset/engine/data_buffer.h"
  21. #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h"
  22. #include "minddata/dataset/engine/datasetops/source/sampler/distributed_sampler.h"
  23. #include "utils/log_adapter.h"
  24. #include <vector>
  25. #include <unordered_set>
  26. using namespace mindspore::dataset;
  27. using mindspore::MsLogLevel::INFO;
  28. using mindspore::ExceptionType::NoExceptionType;
  29. using mindspore::LogStream;
  30. class MindDataTestDistributedSampler : public UT::Common {
  31. public:
  32. class DummyRandomAccessOp : public RandomAccessOp {
  33. public:
  34. DummyRandomAccessOp(uint64_t num_rows) {
  35. // row count is in base class as protected member
  36. // GetNumRowsInDataset does not need an override, the default from base class is fine.
  37. num_rows_ = num_rows;
  38. }
  39. };
  40. };
  41. TEST_F(MindDataTestDistributedSampler, TestTwoShardsOne) {
  42. // num samples to draw.
  43. uint64_t num_samples = 7;
  44. // create sampler with replacement = true
  45. DistributedSampler m_sampler(num_samples, 2, 0, false, 0, false);
  46. DummyRandomAccessOp dummyRandomAccessOp(num_samples);
  47. m_sampler.HandshakeRandomAccessOp(&dummyRandomAccessOp);
  48. std::unique_ptr<DataBuffer> db;
  49. TensorRow row;
  50. std::vector<uint64_t> out;
  51. ASSERT_EQ(m_sampler.GetNextSample(&db), Status::OK());
  52. db->PopRow(&row);
  53. for (const auto &t : row) {
  54. for (auto it = t->begin<uint64_t>(); it != t->end<uint64_t>(); it++) {
  55. out.push_back(*it);
  56. }
  57. }
  58. ASSERT_EQ(4, out.size());
  59. ASSERT_EQ(m_sampler.GetNextSample(&db), Status::OK());
  60. ASSERT_EQ(db->eoe(), true);
  61. }
  62. TEST_F(MindDataTestDistributedSampler, TestTwoShardsTwo) {
  63. // num samples to draw.
  64. uint64_t num_samples = 7;
  65. // create sampler with replacement = true
  66. DistributedSampler m_sampler(num_samples, 2, 1, false, 0, false);
  67. DummyRandomAccessOp dummyRandomAccessOp(num_samples);
  68. m_sampler.HandshakeRandomAccessOp(&dummyRandomAccessOp);
  69. std::unique_ptr<DataBuffer> db;
  70. TensorRow row;
  71. std::vector<uint64_t> out;
  72. ASSERT_EQ(m_sampler.GetNextSample(&db), Status::OK());
  73. db->PopRow(&row);
  74. for (const auto &t : row) {
  75. for (auto it = t->begin<uint64_t>(); it != t->end<uint64_t>(); it++) {
  76. out.push_back(*it);
  77. }
  78. }
  79. ASSERT_EQ(3, out.size());
  80. ASSERT_EQ(m_sampler.GetNextSample(&db), Status::OK());
  81. ASSERT_EQ(db->eoe(), true);
  82. }
  83. TEST_F(MindDataTestDistributedSampler, TestThreeShards) {
  84. // num samples to draw.
  85. uint64_t num_samples = 2;
  86. // create sampler with replacement = true
  87. DistributedSampler m_sampler(num_samples, 3, 2, false, 0, false);
  88. DummyRandomAccessOp dummyRandomAccessOp(num_samples);
  89. m_sampler.HandshakeRandomAccessOp(&dummyRandomAccessOp);
  90. std::unique_ptr<DataBuffer> db;
  91. TensorRow row;
  92. std::vector<uint64_t> out;
  93. ASSERT_EQ(m_sampler.GetNextSample(&db), Status::OK());
  94. db->PopRow(&row);
  95. for (const auto &t : row) {
  96. for (auto it = t->begin<uint64_t>(); it != t->end<uint64_t>(); it++) {
  97. out.push_back(*it);
  98. }
  99. }
  100. ASSERT_EQ(0, out.size());
  101. ASSERT_EQ(m_sampler.GetNextSample(&db), Status::OK());
  102. ASSERT_EQ(db->eoe(), true);
  103. }