• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "minddata/dataset/engine/datasetops/source/sampler/subset_sampler.h"
17 
18 namespace mindspore {
19 namespace dataset {
20 // Constructor.
SubsetSamplerRT(const std::vector<int64_t> & indices,int64_t num_samples,int64_t samples_per_tensor)21 SubsetSamplerRT::SubsetSamplerRT(const std::vector<int64_t> &indices, int64_t num_samples, int64_t samples_per_tensor)
22     : SamplerRT(num_samples, samples_per_tensor), indices_(indices), sample_id_(0) {}
23 
24 // Initialized this Sampler.
InitSampler()25 Status SubsetSamplerRT::InitSampler() {
26   if (is_initialized) {
27     return Status::OK();
28   }
29   CHECK_FAIL_RETURN_UNEXPECTED(
30     num_rows_ > 0, "Invalid parameter, num_rows must be greater than 0, but got " + std::to_string(num_rows_) + ".\n");
31 
32   // Special value of 0 for num_samples means that the user wants to sample the entire set of data.
33   // In this case, the id's are provided by the user.  Cap the num_samples on the number of id's given.
34   if (num_samples_ == 0 || num_samples_ > static_cast<int64_t>(indices_.size())) {
35     num_samples_ = static_cast<int64_t>(indices_.size());
36   }
37 
38   if (samples_per_tensor_ > num_samples_) {
39     samples_per_tensor_ = num_samples_;
40   }
41 
42   is_initialized = true;
43   return Status::OK();
44 }
45 
46 // Reset the internal variable to the initial state.
ResetSampler()47 Status SubsetSamplerRT::ResetSampler() {
48   // Reset the internal counters.
49   sample_id_ = 0;
50 
51   if (HasChildSampler()) {
52     RETURN_IF_NOT_OK(child_[0]->ResetSampler());
53   }
54 
55   return Status::OK();
56 }
57 
58 // Get the sample ids.
GetNextSample(TensorRow * out)59 Status SubsetSamplerRT::GetNextSample(TensorRow *out) {
60   // All samples have been drawn
61   if (sample_id_ == num_samples_) {
62     (*out) = TensorRow(TensorRow::kFlagEOE);
63   } else {
64     if (HasChildSampler()) {
65       RETURN_IF_NOT_OK(child_[0]->GetNextSample(&child_ids_));
66     }
67 
68     std::shared_ptr<Tensor> outputIds;
69 
70     int64_t last_id = sample_id_ + samples_per_tensor_;
71     // Handling the return all samples at once, and when last draw is not a full batch.
72     if (last_id > num_samples_) {
73       last_id = num_samples_;
74     }
75 
76     // Allocate tensor
77     RETURN_IF_NOT_OK(CreateSamplerTensor(&outputIds, last_id - sample_id_));
78 
79     // Initialize tensor
80     auto id_ptr = outputIds->begin<int64_t>();
81     while (sample_id_ < last_id) {
82       if (indices_[sample_id_] >= num_rows_ || indices_[sample_id_] < 0) {
83         std::string err_msg = "Sample ID (" + std::to_string(indices_[sample_id_]) +
84                               ") is out of bound, expected range [0, " + std::to_string(num_rows_ - 1) + "]";
85         RETURN_STATUS_UNEXPECTED(err_msg);
86       }
87 
88       int64_t sampled_id = ((indices_[sample_id_] % num_rows_) + num_rows_) % num_rows_;
89       if (HasChildSampler()) {
90         RETURN_IF_NOT_OK(GetAssociatedChildId(&sampled_id, sampled_id));
91       }
92 
93       *id_ptr = sampled_id;
94       ++id_ptr;
95       sample_id_++;
96     }
97 
98     (*out) = {outputIds};
99   }
100 
101   return Status::OK();
102 }
103 
SamplerPrint(std::ostream & out,bool show_all) const104 void SubsetSamplerRT::SamplerPrint(std::ostream &out, bool show_all) const {
105   out << "\nSampler: SubsetSampler";
106   if (show_all) {
107     // Call the super class for displaying any common detailed info
108     SamplerRT::SamplerPrint(out, show_all);
109     // Then add our own info if any
110   }
111 }
112 
to_json(nlohmann::json * out_json)113 Status SubsetSamplerRT::to_json(nlohmann::json *out_json) {
114   nlohmann::json args;
115   RETURN_IF_NOT_OK(SamplerRT::to_json(&args));
116   args["sampler_name"] = "SubsetSampler";
117   args["indices"] = indices_;
118 
119   *out_json = args;
120   return Status::OK();
121 }
122 
CalculateNumSamples(int64_t num_rows)123 int64_t SubsetSamplerRT::CalculateNumSamples(int64_t num_rows) {
124   int64_t child_num_rows = num_rows;
125   if (!child_.empty()) {
126     child_num_rows = child_[0]->CalculateNumSamples(num_rows);
127     // return -1 if child_num_rows is undetermined
128     if (child_num_rows == -1) return child_num_rows;
129   }
130   int64_t res = (num_samples_ > 0) ? std::min(child_num_rows, num_samples_) : child_num_rows;
131   res = std::min(res, static_cast<int64_t>(indices_.size()));
132   return res;
133 }
134 }  // namespace dataset
135 }  // namespace mindspore
136