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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "dataset/core/client.h"
  18. #include "dataset/core/global_context.h"
  19. #include "dataset/engine/datasetops/source/sampler/distributed_sampler.h"
  20. #include "dataset/engine/datasetops/source/sampler/random_sampler.h"
  21. #include "dataset/engine/datasetops/source/sampler/sampler.h"
  22. #include "dataset/engine/datasetops/source/sampler/sequential_sampler.h"
  23. #include "dataset/util/de_error.h"
  24. #include "dataset/util/status.h"
  25. #include "gtest/gtest.h"
  26. #include "utils/log_adapter.h"
  27. #include "securec.h"
  28. using namespace mindspore::dataset;
  29. Status CreateINT64Tensor(std::shared_ptr<Tensor> *sample_ids, int64_t num_elements, unsigned char *data = nullptr) {
  30. TensorShape shape(std::vector<int64_t>(1, num_elements));
  31. RETURN_IF_NOT_OK(Tensor::CreateTensor(sample_ids, TensorImpl::kFlexible, shape,
  32. DataType(DataType::DE_INT64), data));
  33. if (data == nullptr) {
  34. (*sample_ids)->GetMutableBuffer(); // allocate memory in case user forgets!
  35. }
  36. return Status::OK();
  37. }
  38. class MindDataTestStandAloneSampler : public UT::DatasetOpTesting {
  39. protected:
  40. class MockStorageOp : public RandomAccessOp {
  41. public:
  42. MockStorageOp(int64_t val) : m_val_(val) {}
  43. Status GetNumSamples(int64_t *ptr) const override {
  44. (*ptr) = m_val_;
  45. return Status::OK();
  46. }
  47. Status GetNumRowsInDataset(int64_t *ptr) const override {
  48. (*ptr) = m_val_;
  49. return Status::OK();
  50. }
  51. private:
  52. int64_t m_val_;
  53. };
  54. };
  55. TEST_F(MindDataTestStandAloneSampler, TestDistributedSampler) {
  56. std::vector<std::shared_ptr<Tensor>> row;
  57. 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},
  58. {0, 17, 4, 10, 14, 8, 15}, {13, 9, 16, 3, 2, 19, 12}, {1, 11, 6, 18, 7, 5, 0}};
  59. for (int i = 0; i < 6; i++) {
  60. std::shared_ptr<Tensor> t;
  61. Tensor::CreateTensor(&t, TensorImpl::kFlexible, TensorShape({7}),
  62. DataType(DataType::DE_INT64), (unsigned char *)(res[i]));
  63. row.push_back(t);
  64. }
  65. MockStorageOp mock(20);
  66. std::unique_ptr<DataBuffer> db;
  67. std::shared_ptr<Tensor> tensor;
  68. for (int i = 0; i < 6; i++) {
  69. std::unique_ptr<Sampler> sampler = std::make_unique<DistributedSampler>(3, i % 3, (i < 3 ? false : true));
  70. sampler->HandshakeRandomAccessOp(&mock);
  71. sampler->GetNextBuffer(&db);
  72. db->GetTensor(&tensor, 0, 0);
  73. MS_LOG(DEBUG) << (*tensor);
  74. if(i < 3) { // This is added due to std::shuffle()
  75. EXPECT_TRUE((*tensor) == (*row[i]));
  76. }
  77. }
  78. }
  79. TEST_F(MindDataTestStandAloneSampler, TestStandAoneSequentialSampler) {
  80. std::vector<std::shared_ptr<Tensor>> row;
  81. MockStorageOp mock(5);
  82. uint64_t res[5] = {0, 1, 2, 3, 4};
  83. std::shared_ptr<Tensor> label1, label2;
  84. CreateINT64Tensor(&label1, 3, reinterpret_cast<unsigned char *>(res));
  85. CreateINT64Tensor(&label2, 2, reinterpret_cast<unsigned char *>(res + 3));
  86. std::shared_ptr<Sampler> sampler = std::make_shared<SequentialSampler>(3);
  87. std::unique_ptr<DataBuffer> db;
  88. std::shared_ptr<Tensor> tensor;
  89. sampler->HandshakeRandomAccessOp(&mock);
  90. sampler->GetNextBuffer(&db);
  91. db->GetTensor(&tensor, 0, 0);
  92. EXPECT_TRUE((*tensor) == (*label1));
  93. sampler->GetNextBuffer(&db);
  94. db->GetTensor(&tensor, 0, 0);
  95. EXPECT_TRUE((*tensor) == (*label2));
  96. sampler->Reset();
  97. sampler->GetNextBuffer(&db);
  98. db->GetTensor(&tensor, 0, 0);
  99. EXPECT_TRUE((*tensor) == (*label1));
  100. sampler->GetNextBuffer(&db);
  101. db->GetTensor(&tensor, 0, 0);
  102. EXPECT_TRUE((*tensor) == (*label2));
  103. }