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.

samplers.cc 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "minddata/dataset/include/samplers.h"
  17. #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h"
  18. #include "minddata/dataset/engine/datasetops/source/sampler/distributed_sampler.h"
  19. #include "minddata/dataset/engine/datasetops/source/sampler/random_sampler.h"
  20. #include "minddata/dataset/engine/datasetops/source/sampler/sequential_sampler.h"
  21. #include "minddata/dataset/engine/datasetops/source/sampler/subset_random_sampler.h"
  22. #include "minddata/dataset/engine/datasetops/source/sampler/weighted_random_sampler.h"
  23. #include "minddata/dataset/engine/datasetops/source/sampler/pk_sampler.h"
  24. #ifndef ENABLE_ANDROID
  25. #include "minddata/mindrecord/include/shard_distributed_sample.h"
  26. #include "minddata/mindrecord/include/shard_operator.h"
  27. #include "minddata/mindrecord/include/shard_pk_sample.h"
  28. #include "minddata/mindrecord/include/shard_sample.h"
  29. #include "minddata/mindrecord/include/shard_sequential_sample.h"
  30. #include "minddata/mindrecord/include/shard_shuffle.h"
  31. #include "minddata/dataset/util/random.h"
  32. #endif
  33. namespace mindspore {
  34. namespace dataset {
  35. namespace api {
  36. #define RETURN_NULL_IF_ERROR(_s) \
  37. do { \
  38. Status __rc = (_s); \
  39. if (__rc.IsError()) { \
  40. MS_LOG(ERROR) << __rc; \
  41. return nullptr; \
  42. } \
  43. } while (false)
  44. // Constructor
  45. SamplerObj::SamplerObj() {}
  46. /// Function to create a Distributed Sampler.
  47. std::shared_ptr<DistributedSamplerObj> DistributedSampler(int64_t num_shards, int64_t shard_id, bool shuffle,
  48. int64_t num_samples, uint32_t seed, int64_t offset,
  49. bool even_dist) {
  50. auto sampler =
  51. std::make_shared<DistributedSamplerObj>(num_shards, shard_id, shuffle, num_samples, seed, offset, even_dist);
  52. // Input validation
  53. if (!sampler->ValidateParams()) {
  54. return nullptr;
  55. }
  56. return sampler;
  57. }
  58. /// Function to create a PK Sampler.
  59. std::shared_ptr<PKSamplerObj> PKSampler(int64_t num_val, bool shuffle, int64_t num_samples) {
  60. auto sampler = std::make_shared<PKSamplerObj>(num_val, shuffle, num_samples);
  61. // Input validation
  62. if (!sampler->ValidateParams()) {
  63. return nullptr;
  64. }
  65. return sampler;
  66. }
  67. /// Function to create a Random Sampler.
  68. std::shared_ptr<RandomSamplerObj> RandomSampler(bool replacement, int64_t num_samples) {
  69. auto sampler = std::make_shared<RandomSamplerObj>(replacement, num_samples);
  70. // Input validation
  71. if (!sampler->ValidateParams()) {
  72. return nullptr;
  73. }
  74. return sampler;
  75. }
  76. /// Function to create a Sequential Sampler.
  77. std::shared_ptr<SequentialSamplerObj> SequentialSampler(int64_t start_index, int64_t num_samples) {
  78. auto sampler = std::make_shared<SequentialSamplerObj>(start_index, num_samples);
  79. // Input validation
  80. if (!sampler->ValidateParams()) {
  81. return nullptr;
  82. }
  83. return sampler;
  84. }
  85. /// Function to create a Subset Random Sampler.
  86. std::shared_ptr<SubsetRandomSamplerObj> SubsetRandomSampler(std::vector<int64_t> indices, int64_t num_samples) {
  87. auto sampler = std::make_shared<SubsetRandomSamplerObj>(std::move(indices), num_samples);
  88. // Input validation
  89. if (!sampler->ValidateParams()) {
  90. return nullptr;
  91. }
  92. return sampler;
  93. }
  94. /// Function to create a Weighted Random Sampler.
  95. std::shared_ptr<WeightedRandomSamplerObj> WeightedRandomSampler(std::vector<double> weights, int64_t num_samples,
  96. bool replacement) {
  97. auto sampler = std::make_shared<WeightedRandomSamplerObj>(std::move(weights), num_samples, replacement);
  98. // Input validation
  99. if (!sampler->ValidateParams()) {
  100. return nullptr;
  101. }
  102. return sampler;
  103. }
  104. /* ####################################### Derived Sampler classes ################################# */
  105. // DistributedSampler
  106. DistributedSamplerObj::DistributedSamplerObj(int64_t num_shards, int64_t shard_id, bool shuffle, int64_t num_samples,
  107. uint32_t seed, int64_t offset, bool even_dist)
  108. : num_shards_(num_shards),
  109. shard_id_(shard_id),
  110. shuffle_(shuffle),
  111. num_samples_(num_samples),
  112. seed_(seed),
  113. offset_(offset),
  114. even_dist_(even_dist) {}
  115. bool DistributedSamplerObj::ValidateParams() {
  116. if (num_shards_ <= 0) {
  117. MS_LOG(ERROR) << "DistributedSampler: invalid num_shards: " << num_shards_;
  118. return false;
  119. }
  120. if (shard_id_ < 0 || shard_id_ >= num_shards_) {
  121. MS_LOG(ERROR) << "DistributedSampler: invalid input, shard_id: " << shard_id_ << ", num_shards: " << num_shards_;
  122. return false;
  123. }
  124. if (num_samples_ < 0) {
  125. MS_LOG(ERROR) << "DistributedSampler: invalid num_samples: " << num_samples_;
  126. return false;
  127. }
  128. return true;
  129. }
  130. std::shared_ptr<Sampler> DistributedSamplerObj::Build() {
  131. // runtime sampler object
  132. auto sampler = std::make_shared<dataset::DistributedSampler>(num_samples_, num_shards_, shard_id_, shuffle_, seed_,
  133. offset_, even_dist_);
  134. return sampler;
  135. }
  136. #ifndef ENABLE_ANDROID
  137. std::shared_ptr<mindrecord::ShardOperator> DistributedSamplerObj::BuildForMindDataset() {
  138. // runtime mindrecord sampler object
  139. auto mind_sampler = std::make_shared<mindrecord::ShardDistributedSample>(num_shards_, shard_id_, shuffle_, seed_,
  140. num_samples_, offset_);
  141. return mind_sampler;
  142. }
  143. #endif
  144. // PKSampler
  145. PKSamplerObj::PKSamplerObj(int64_t num_val, bool shuffle, int64_t num_samples)
  146. : num_val_(num_val), shuffle_(shuffle), num_samples_(num_samples) {}
  147. bool PKSamplerObj::ValidateParams() {
  148. if (num_val_ <= 0) {
  149. MS_LOG(ERROR) << "PKSampler: invalid num_val: " << num_val_;
  150. return false;
  151. }
  152. if (num_samples_ < 0) {
  153. MS_LOG(ERROR) << "PKSampler: invalid num_samples: " << num_samples_;
  154. return false;
  155. }
  156. return true;
  157. }
  158. std::shared_ptr<Sampler> PKSamplerObj::Build() {
  159. // runtime sampler object
  160. auto sampler = std::make_shared<dataset::PKSampler>(num_samples_, num_val_, shuffle_);
  161. return sampler;
  162. }
  163. #ifndef ENABLE_ANDROID
  164. std::shared_ptr<mindrecord::ShardOperator> PKSamplerObj::BuildForMindDataset() {
  165. // runtime mindrecord sampler object
  166. std::shared_ptr<mindrecord::ShardOperator> mind_sampler;
  167. if (shuffle_ == true) {
  168. mind_sampler = std::make_shared<mindrecord::ShardPkSample>("label", num_val_, std::numeric_limits<int64_t>::max(),
  169. GetSeed(), num_samples_);
  170. } else {
  171. mind_sampler = std::make_shared<mindrecord::ShardPkSample>("label", num_val_, num_samples_);
  172. }
  173. return mind_sampler;
  174. }
  175. #endif
  176. // RandomSampler
  177. RandomSamplerObj::RandomSamplerObj(bool replacement, int64_t num_samples)
  178. : replacement_(replacement), num_samples_(num_samples) {}
  179. bool RandomSamplerObj::ValidateParams() {
  180. if (num_samples_ < 0) {
  181. MS_LOG(ERROR) << "RandomSampler: invalid num_samples: " << num_samples_;
  182. return false;
  183. }
  184. return true;
  185. }
  186. std::shared_ptr<Sampler> RandomSamplerObj::Build() {
  187. // runtime sampler object
  188. bool reshuffle_each_epoch = true;
  189. auto sampler = std::make_shared<dataset::RandomSampler>(num_samples_, replacement_, reshuffle_each_epoch);
  190. return sampler;
  191. }
  192. #ifndef ENABLE_ANDROID
  193. std::shared_ptr<mindrecord::ShardOperator> RandomSamplerObj::BuildForMindDataset() {
  194. // runtime mindrecord sampler object
  195. bool reshuffle_each_epoch_ = true;
  196. auto mind_sampler =
  197. std::make_shared<mindrecord::ShardShuffle>(GetSeed(), num_samples_, replacement_, reshuffle_each_epoch_);
  198. return mind_sampler;
  199. }
  200. #endif
  201. // SequentialSampler
  202. SequentialSamplerObj::SequentialSamplerObj(int64_t start_index, int64_t num_samples)
  203. : start_index_(start_index), num_samples_(num_samples) {}
  204. bool SequentialSamplerObj::ValidateParams() {
  205. if (num_samples_ < 0) {
  206. MS_LOG(ERROR) << "SequentialSampler: invalid num_samples: " << num_samples_;
  207. return false;
  208. }
  209. if (start_index_ < 0) {
  210. MS_LOG(ERROR) << "SequentialSampler: invalid start_index: " << start_index_;
  211. return false;
  212. }
  213. return true;
  214. }
  215. std::shared_ptr<Sampler> SequentialSamplerObj::Build() {
  216. // runtime sampler object
  217. auto sampler = std::make_shared<dataset::SequentialSampler>(num_samples_, start_index_);
  218. return sampler;
  219. }
  220. #ifndef ENABLE_ANDROID
  221. std::shared_ptr<mindrecord::ShardOperator> SequentialSamplerObj::BuildForMindDataset() {
  222. // runtime mindrecord sampler object
  223. auto mind_sampler = std::make_shared<mindrecord::ShardSequentialSample>(num_samples_, start_index_);
  224. return mind_sampler;
  225. }
  226. #endif
  227. // SubsetRandomSampler
  228. SubsetRandomSamplerObj::SubsetRandomSamplerObj(std::vector<int64_t> indices, int64_t num_samples)
  229. : indices_(std::move(indices)), num_samples_(num_samples) {}
  230. bool SubsetRandomSamplerObj::ValidateParams() {
  231. if (num_samples_ < 0) {
  232. MS_LOG(ERROR) << "SubsetRandomSampler: invalid num_samples: " << num_samples_;
  233. return false;
  234. }
  235. return true;
  236. }
  237. std::shared_ptr<Sampler> SubsetRandomSamplerObj::Build() {
  238. // runtime sampler object
  239. auto sampler = std::make_shared<dataset::SubsetRandomSampler>(num_samples_, indices_);
  240. return sampler;
  241. }
  242. #ifndef ENABLE_ANDROID
  243. std::shared_ptr<mindrecord::ShardOperator> SubsetRandomSamplerObj::BuildForMindDataset() {
  244. // runtime mindrecord sampler object
  245. auto mind_sampler = std::make_shared<mindrecord::ShardSample>(indices_, GetSeed());
  246. return mind_sampler;
  247. }
  248. #endif
  249. // WeightedRandomSampler
  250. WeightedRandomSamplerObj::WeightedRandomSamplerObj(std::vector<double> weights, int64_t num_samples, bool replacement)
  251. : weights_(std::move(weights)), num_samples_(num_samples), replacement_(replacement) {}
  252. bool WeightedRandomSamplerObj::ValidateParams() {
  253. if (weights_.empty()) {
  254. MS_LOG(ERROR) << "WeightedRandomSampler: weights vector must not be empty";
  255. return false;
  256. }
  257. int32_t zero_elem = 0;
  258. for (int32_t i = 0; i < weights_.size(); ++i) {
  259. if (weights_[i] < 0) {
  260. MS_LOG(ERROR) << "WeightedRandomSampler: weights vector must not contain negative number, got: " << weights_[i];
  261. return false;
  262. }
  263. if (weights_[i] == 0.0) {
  264. zero_elem++;
  265. }
  266. }
  267. if (zero_elem == weights_.size()) {
  268. MS_LOG(ERROR) << "WeightedRandomSampler: elements of weights vector must not be all zero";
  269. return false;
  270. }
  271. if (num_samples_ < 0) {
  272. MS_LOG(ERROR) << "WeightedRandomSampler: invalid num_samples: " << num_samples_;
  273. return false;
  274. }
  275. return true;
  276. }
  277. std::shared_ptr<Sampler> WeightedRandomSamplerObj::Build() {
  278. auto sampler = std::make_shared<dataset::WeightedRandomSampler>(num_samples_, weights_, replacement_);
  279. return sampler;
  280. }
  281. } // namespace api
  282. } // namespace dataset
  283. } // namespace mindspore