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 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_DATASETOPS_SOURCE_SAMPLER_RANDOM_SAMPLER_H_ 17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_DATASETOPS_SOURCE_SAMPLER_RANDOM_SAMPLER_H_ 18 19 #include <limits> 20 #include <memory> 21 #include <vector> 22 23 #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h" 24 25 namespace mindspore { 26 namespace dataset { 27 class RandomSamplerRT : public SamplerRT { 28 public: 29 // Constructor 30 // @param bool replacement - put he id back / or not after a sample 31 // @param int64_t num_samples - number samples to draw 32 // @param reshuffle_each_epoch - T/F to reshuffle after epoch 33 // @param int64_t samples_per_tensor - Num of Sampler Ids to fetch via 1 GetNextSample call 34 RandomSamplerRT(bool replacement, int64_t num_samples, bool reshuffle_each_epoch, 35 int64_t samples_per_tensor = std::numeric_limits<int64_t>::max()); 36 37 // Destructor. 38 ~RandomSamplerRT() = default; 39 40 // Op calls this to get next Sample that contains all the sampleIds 41 // @param TensorRow to be returned to StorageOp 42 // @param int32_t workerId - not meant to be used 43 // @return Status The status code returned 44 Status GetNextSample(TensorRow *out) override; 45 46 // meant to be called by base class or python 47 Status InitSampler() override; 48 49 /// \brief Reset for next epoch. 50 /// \param[in] failover_reset A boolean to show whether we are resetting the pipeline 51 /// \return Status The status code returned 52 Status ResetSampler(const bool failover_reset = false) override; 53 54 void SamplerPrint(std::ostream &out, bool show_all) const override; 55 56 /// \brief Get the arguments of node 57 /// \param[out] out_json JSON string of all attributes 58 /// \return Status of the function 59 Status to_json(nlohmann::json *out_json) override; 60 61 private: 62 uint32_t seed_; 63 bool replacement_; 64 std::vector<int64_t> shuffled_ids_; // only used for NO REPLACEMENT 65 int64_t next_id_; 66 std::mt19937 rnd_; 67 std::unique_ptr<std::uniform_int_distribution<int64_t>> dist; 68 bool reshuffle_each_epoch_; 69 }; 70 } // namespace dataset 71 } // namespace mindspore 72 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_DATASETOPS_SOURCE_SAMPLER_RANDOM_SAMPLER_H_ 73