1 /** 2 * Copyright 2020-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 17 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_IR_DATASETOPS_SOURCE_SAMPLERS_DISTRIBUTED_SAMPLER_IR_H_ 18 #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_IR_DATASETOPS_SOURCE_SAMPLERS_DISTRIBUTED_SAMPLER_IR_H_ 19 20 #include <limits> 21 #include <memory> 22 #include <string> 23 #include <utility> 24 #include <vector> 25 #include <nlohmann/json.hpp> 26 27 #include "minddata/dataset/engine/ir/datasetops/source/samplers/samplers_ir.h" 28 #include "include/api/status.h" 29 #ifndef ENABLE_ANDROID 30 #include "minddata/mindrecord/include/shard_operator.h" 31 #endif 32 33 namespace mindspore { 34 namespace dataset { 35 36 // Internal Sampler class forward declaration 37 class SamplerRT; 38 39 class DistributedSamplerObj : public SamplerObj { 40 public: 41 DistributedSamplerObj(int64_t num_shards, int64_t shard_id, bool shuffle, int64_t num_samples, uint32_t seed, 42 int64_t offset, bool even_dist); 43 44 ~DistributedSamplerObj(); 45 46 Status SamplerBuild(std::shared_ptr<SamplerRT> *sampler) override; 47 48 std::shared_ptr<SamplerObj> SamplerCopy() override; 49 50 #ifndef ENABLE_ANDROID 51 std::shared_ptr<mindrecord::ShardOperator> BuildForMindDataset() override; 52 #endif 53 54 /// \brief Get the arguments of node 55 /// \param[out] out_json JSON string of all attributes 56 /// \return Status of the function 57 Status to_json(nlohmann::json *const out_json) override; 58 59 #ifndef ENABLE_ANDROID 60 /// \brief Function for read sampler from JSON object 61 /// \param[in] json_obj JSON object to be read 62 /// \param[in] num_samples number of sample in the sampler 63 /// \param[out] sampler Sampler constructed from parameters in JSON object 64 /// \return Status of the function 65 static Status from_json(nlohmann::json json_obj, int64_t num_samples, std::shared_ptr<SamplerObj> *sampler); 66 #endif 67 68 Status ValidateParams() override; 69 70 /// \brief Function to get the shard id of sampler 71 /// \return The shard id of sampler 72 int64_t ShardId() override; 73 74 private: 75 int64_t num_shards_; 76 int64_t shard_id_; 77 bool shuffle_; 78 int64_t num_samples_; 79 uint32_t seed_; 80 int64_t offset_; 81 bool even_dist_; 82 }; 83 84 } // namespace dataset 85 } // namespace mindspore 86 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_IR_DATASETOPS_SOURCE_SAMPLERS_DISTRIBUTED_SAMPLER_IR_H_ 87