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