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.

stand_alone_samplers_test.cc 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright 2019 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 "minddata/dataset/core/client.h"
  18. #include "minddata/dataset/core/global_context.h"
  19. #include "minddata/dataset/engine/datasetops/source/sampler/distributed_sampler.h"
  20. #include "minddata/dataset/engine/datasetops/source/sampler/random_sampler.h"
  21. #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h"
  22. #include "minddata/dataset/engine/datasetops/source/sampler/sequential_sampler.h"
  23. #include "minddata/dataset/util/status.h"
  24. #include "gtest/gtest.h"
  25. #include "utils/log_adapter.h"
  26. #include "securec.h"
  27. using namespace mindspore::dataset;
  28. Status CreateINT64Tensor(std::shared_ptr<Tensor> *sample_ids, int64_t num_elements, unsigned char *data = nullptr) {
  29. TensorShape shape(std::vector<int64_t>(1, num_elements));
  30. RETURN_IF_NOT_OK(Tensor::CreateFromMemory(shape, DataType(DataType::DE_INT64), data, sample_ids));
  31. return Status::OK();
  32. }
  33. class MindDataTestStandAloneSampler : public UT::DatasetOpTesting {
  34. protected:
  35. class MockStorageOp : public RandomAccessOp {
  36. public:
  37. MockStorageOp(int64_t val) {
  38. // row count is in base class as protected member
  39. // GetNumRowsInDataset does not need an override, the default from base class is fine.
  40. num_rows_ = val;
  41. }
  42. };
  43. };
  44. TEST_F(MindDataTestStandAloneSampler, TestDistributedSampler) {
  45. std::vector<std::shared_ptr<Tensor>> row;
  46. uint64_t res[6][7] = {{0, 3, 6, 9, 12, 15, 18}, {1, 4, 7, 10, 13, 16, 19}, {2, 5, 8, 11, 14, 17, 0},
  47. {0, 17, 4, 10, 14, 8, 15}, {13, 9, 16, 3, 2, 19, 12}, {1, 11, 6, 18, 7, 5, 0}};
  48. for (int i = 0; i < 6; i++) {
  49. std::shared_ptr<Tensor> t;
  50. Tensor::CreateFromMemory(TensorShape({7}), DataType(DataType::DE_INT64), (unsigned char *)(res[i]), &t);
  51. row.push_back(t);
  52. }
  53. MockStorageOp mock(20);
  54. std::shared_ptr<Tensor> tensor;
  55. int64_t num_samples = 0;
  56. TensorRow sample_row;
  57. for (int i = 0; i < 6; i++) {
  58. std::shared_ptr<SamplerRT> sampler =
  59. std::make_shared<DistributedSamplerRT>(num_samples, 3, i % 3, (i < 3 ? false : true));
  60. sampler->HandshakeRandomAccessOp(&mock);
  61. sampler->GetNextSample(&sample_row);
  62. tensor = sample_row[0];
  63. MS_LOG(DEBUG) << (*tensor);
  64. if (i < 3) { // This is added due to std::shuffle()
  65. EXPECT_TRUE((*tensor) == (*row[i]));
  66. }
  67. }
  68. }
  69. TEST_F(MindDataTestStandAloneSampler, TestStandAoneSequentialSampler) {
  70. std::vector<std::shared_ptr<Tensor>> row;
  71. MockStorageOp mock(5);
  72. uint64_t res[5] = {0, 1, 2, 3, 4};
  73. std::shared_ptr<Tensor> label1, label2;
  74. CreateINT64Tensor(&label1, 3, reinterpret_cast<unsigned char *>(res));
  75. CreateINT64Tensor(&label2, 2, reinterpret_cast<unsigned char *>(res + 3));
  76. int64_t num_samples = 0;
  77. int64_t start_index = 0;
  78. std::shared_ptr<SamplerRT> sampler = std::make_shared<SequentialSamplerRT>(num_samples, start_index, 3);
  79. std::shared_ptr<Tensor> tensor;
  80. TensorRow sample_row;
  81. sampler->HandshakeRandomAccessOp(&mock);
  82. sampler->GetNextSample(&sample_row);
  83. tensor = sample_row[0];
  84. EXPECT_TRUE((*tensor) == (*label1));
  85. sampler->GetNextSample(&sample_row);
  86. tensor = sample_row[0];
  87. EXPECT_TRUE((*tensor) == (*label2));
  88. sampler->ResetSampler();
  89. sampler->GetNextSample(&sample_row);
  90. tensor = sample_row[0];
  91. EXPECT_TRUE((*tensor) == (*label1));
  92. sampler->GetNextSample(&sample_row);
  93. tensor = sample_row[0];
  94. EXPECT_TRUE((*tensor) == (*label2));
  95. }