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.

ir_sampler_test.cc 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright 2021 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/engine/datasetops/source/sampler/sampler.h"
  18. #include "minddata/dataset/engine/ir/datasetops/source/samplers/distributed_sampler_ir.h"
  19. #include "minddata/dataset/engine/ir/datasetops/source/samplers/pk_sampler_ir.h"
  20. #include "minddata/dataset/engine/ir/datasetops/source/samplers/prebuilt_sampler_ir.h"
  21. #include "minddata/dataset/engine/ir/datasetops/source/samplers/random_sampler_ir.h"
  22. #include "minddata/dataset/engine/ir/datasetops/source/samplers/samplers_ir.h"
  23. #include "minddata/dataset/engine/ir/datasetops/source/samplers/sequential_sampler_ir.h"
  24. #include "minddata/dataset/engine/ir/datasetops/source/samplers/subset_random_sampler_ir.h"
  25. #include "minddata/dataset/engine/ir/datasetops/source/samplers/subset_sampler_ir.h"
  26. #include "minddata/dataset/engine/ir/datasetops/source/samplers/weighted_random_sampler_ir.h"
  27. #include "minddata/dataset/core/tensor.h"
  28. using namespace mindspore::dataset;
  29. using mindspore::dataset::Tensor;
  30. class MindDataTestIrSampler : public UT::DatasetOpTesting {
  31. protected:
  32. };
  33. TEST_F(MindDataTestIrSampler, TestCalculateNumSamples) {
  34. int64_t num_rows = 30; // dummy variable for number of rows in the dataset
  35. std::shared_ptr<SamplerObj> sampl = std::make_shared<DistributedSamplerObj>(2, 1, false, 6, 1, -1, true);
  36. EXPECT_NE(sampl, nullptr);
  37. std::shared_ptr<SamplerRT> sampler_rt;
  38. sampl->SamplerBuild(&sampler_rt);
  39. EXPECT_EQ(sampler_rt->CalculateNumSamples(num_rows), 6);
  40. sampl = std::make_shared<PKSamplerObj>(3, false, 0);
  41. EXPECT_NE(sampl, nullptr);
  42. sampl->SamplerBuild(&sampler_rt);
  43. EXPECT_EQ(sampler_rt->CalculateNumSamples(num_rows), -1);
  44. sampl = std::make_shared<RandomSamplerObj>(false, 12);
  45. EXPECT_NE(sampl, nullptr);
  46. sampl->SamplerBuild(&sampler_rt);
  47. EXPECT_EQ(sampler_rt->CalculateNumSamples(num_rows), 12);
  48. sampl = std::make_shared<SequentialSamplerObj>(0, 10);
  49. EXPECT_NE(sampl, nullptr);
  50. sampl->SamplerBuild(&sampler_rt);
  51. EXPECT_EQ(sampler_rt->CalculateNumSamples(num_rows), 10);
  52. std::vector<double> weights = {0.9, 0.8, 0.68, 0.7, 0.71, 0.6, 0.5, 0.4, 0.3, 0.5, 0.2, 0.1};
  53. sampl = std::make_shared<WeightedRandomSamplerObj>(weights, 12);
  54. EXPECT_NE(sampl, nullptr);
  55. sampl->SamplerBuild(&sampler_rt);
  56. EXPECT_EQ(sampler_rt->CalculateNumSamples(num_rows), 12);
  57. std::vector<int64_t> indices = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21};
  58. sampl = std::make_shared<SubsetRandomSamplerObj>(indices, 11);
  59. EXPECT_NE(sampl, nullptr);
  60. sampl->SamplerBuild(&sampler_rt);
  61. EXPECT_EQ(sampler_rt->CalculateNumSamples(num_rows), 11);
  62. // Testing chains
  63. // Parent and child have num_samples
  64. std::shared_ptr<SamplerObj> sampl1 = std::make_shared<WeightedRandomSamplerObj>(weights, 12);
  65. EXPECT_NE(sampl1, nullptr);
  66. std::shared_ptr<SamplerRT> sampler_rt1;
  67. sampl1->SamplerBuild(&sampler_rt1);
  68. std::shared_ptr<SamplerObj> sampl2 = std::make_shared<SequentialSamplerObj>(0, 10);
  69. EXPECT_NE(sampl2, nullptr);
  70. std::shared_ptr<SamplerRT> sampler_rt2;
  71. sampl2->SamplerBuild(&sampler_rt2);
  72. sampler_rt2->AddChild(sampler_rt1);
  73. EXPECT_EQ(sampler_rt2->CalculateNumSamples(num_rows), 10);
  74. // Parent doesn't have num_samples
  75. std::shared_ptr<SamplerObj> sampl3 = std::make_shared<WeightedRandomSamplerObj>(weights, 12);
  76. EXPECT_NE(sampl3, nullptr);
  77. std::shared_ptr<SamplerRT> sampler_rt3;
  78. sampl3->SamplerBuild(&sampler_rt3);
  79. std::shared_ptr<SamplerObj> sampl4 = std::make_shared<SubsetRandomSamplerObj>(indices, 0);
  80. EXPECT_NE(sampl4, nullptr);
  81. std::shared_ptr<SamplerRT> sampler_rt4;
  82. sampl4->SamplerBuild(&sampler_rt4);
  83. sampler_rt4->AddChild(sampler_rt3);
  84. EXPECT_EQ(sampler_rt4->CalculateNumSamples(num_rows), 11);
  85. // Child doesn't have num_samples
  86. std::shared_ptr<SamplerObj> sampl5 = std::make_shared<RandomSamplerObj>(false, 0);
  87. EXPECT_NE(sampl5, nullptr);
  88. std::shared_ptr<SamplerRT> sampler_rt5;
  89. sampl5->SamplerBuild(&sampler_rt5);
  90. std::shared_ptr<SamplerObj> sampl6 = std::make_shared<PKSamplerObj>(3, false, 7);
  91. EXPECT_NE(sampl6, nullptr);
  92. std::shared_ptr<SamplerRT> sampler_rt6;
  93. sampl6->SamplerBuild(&sampler_rt6);
  94. sampler_rt6->AddChild(sampler_rt5);
  95. EXPECT_EQ(sampler_rt6->CalculateNumSamples(num_rows), -1);
  96. }
  97. TEST_F(MindDataTestIrSampler, TestSamplersMoveParameters) {
  98. std::vector<int64_t> indices = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
  99. std::shared_ptr<SamplerObj> sampl1 = std::make_shared<SubsetRandomSamplerObj>(indices, 0);
  100. EXPECT_FALSE(indices.empty());
  101. std::shared_ptr<SamplerRT> sampler_rt = nullptr;
  102. sampl1->SamplerBuild(&sampler_rt);
  103. EXPECT_NE(sampler_rt, nullptr);
  104. std::shared_ptr<SamplerObj> sampl2 = std::make_shared<SubsetRandomSamplerObj>(std::move(indices), 0);
  105. EXPECT_TRUE(indices.empty());
  106. std::shared_ptr<SamplerRT> sampler_rt2 = nullptr;
  107. sampl2->SamplerBuild(&sampler_rt2);
  108. EXPECT_NE(sampler_rt, nullptr);
  109. }