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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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::unique_ptr<DataBuffer> db;
  55. std::shared_ptr<Tensor> tensor;
  56. int64_t num_samples = 0;
  57. for (int i = 0; i < 6; i++) {
  58. std::shared_ptr<Sampler> sampler = std::make_shared<DistributedSampler>(num_samples, 3, i % 3, (i < 3 ? false : true));
  59. sampler->HandshakeRandomAccessOp(&mock);
  60. sampler->GetNextSample(&db);
  61. db->GetTensor(&tensor, 0, 0);
  62. MS_LOG(DEBUG) << (*tensor);
  63. if(i < 3) { // This is added due to std::shuffle()
  64. EXPECT_TRUE((*tensor) == (*row[i]));
  65. }
  66. }
  67. }
  68. TEST_F(MindDataTestStandAloneSampler, TestStandAoneSequentialSampler) {
  69. std::vector<std::shared_ptr<Tensor>> row;
  70. MockStorageOp mock(5);
  71. uint64_t res[5] = {0, 1, 2, 3, 4};
  72. std::shared_ptr<Tensor> label1, label2;
  73. CreateINT64Tensor(&label1, 3, reinterpret_cast<unsigned char *>(res));
  74. CreateINT64Tensor(&label2, 2, reinterpret_cast<unsigned char *>(res + 3));
  75. int64_t num_samples = 0;
  76. int64_t start_index = 0;
  77. std::shared_ptr<Sampler> sampler = std::make_shared<SequentialSampler>(num_samples, start_index, 3);
  78. std::unique_ptr<DataBuffer> db;
  79. std::shared_ptr<Tensor> tensor;
  80. sampler->HandshakeRandomAccessOp(&mock);
  81. sampler->GetNextSample(&db);
  82. db->GetTensor(&tensor, 0, 0);
  83. EXPECT_TRUE((*tensor) == (*label1));
  84. sampler->GetNextSample(&db);
  85. db->GetTensor(&tensor, 0, 0);
  86. EXPECT_TRUE((*tensor) == (*label2));
  87. sampler->ResetSampler();
  88. sampler->GetNextSample(&db);
  89. db->GetTensor(&tensor, 0, 0);
  90. EXPECT_TRUE((*tensor) == (*label1));
  91. sampler->GetNextSample(&db);
  92. db->GetTensor(&tensor, 0, 0);
  93. EXPECT_TRUE((*tensor) == (*label2));
  94. }