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