1 #include "caffe2/predictor/emulator/data_filler.h" 2 #include "caffe2/predictor/emulator/utils.h" 7 void DataNetFiller::fill_parameter(Workspace* ws)
const {
11 ws->RunNetOnce(init_net_),
12 "Failed running the init_net: ",
13 ProtoDebugString(init_net_));
16 void DataNetFiller::fill_input_internal(TensorList_t* input_data)
const {
18 CAFFE_ENFORCE(ws.RunNetOnce(data_net_));
19 for (
const auto& name : input_names_) {
20 input_data->emplace_back(
21 BlobGetMutableTensor(ws.GetBlob(name), CPU)->Clone());
25 static void fill_with_type(
26 const TensorFiller& filler,
27 const std::string& type,
30 if (type ==
"float") {
31 filler.Fill<
float>(output, &context);
32 }
else if (type ==
"double") {
33 filler.Fill<
double>(output, &context);
34 }
else if (type ==
"uint8_t" || type ==
"unsigned char") {
35 filler.Fill<uint8_t>(output, &context);
36 }
else if (type ==
"uint16_t") {
37 filler.Fill<uint16_t>(output, &context);
38 }
else if (type ==
"int8_t") {
39 filler.Fill<int8_t>(output, &context);
40 }
else if (type ==
"int16_t") {
41 filler.Fill<int16_t>(output, &context);
42 }
else if (type ==
"int32_t" || type ==
"int") {
43 filler.Fill<int32_t>(output, &context);
44 }
else if (type ==
"int64_t" || type ==
"long") {
45 filler.Fill<int64_t>(output, &context);
46 }
else if (type ==
"bool") {
47 auto mutable_filler = filler;
48 mutable_filler.Min(0).Max(2).Fill<uint8_t>(output, &context);
50 throw std::invalid_argument(
"filler does not support type " + type);
54 DataRandomFiller::DataRandomFiller(
55 const NetDef& run_net,
56 const std::vector<std::vector<std::vector<int64_t>>>& input_dims,
57 const std::vector<std::vector<std::string>>& input_types) {
59 CAFFE_ENFORCE_EQ(input_dims.size(), run_net.op_size());
60 CAFFE_ENFORCE_EQ(input_types.size(), run_net.op_size());
63 std::unordered_set<std::string> output_names;
64 for (
size_t i = 0; i < run_net.op_size(); ++i) {
65 const auto& op = run_net.op(i);
66 const auto& op_dims = input_dims[i];
67 const auto& op_types = input_types[i];
69 op_dims.size() == op.input_size(),
70 op.name() +
" has " + c10::to_string(op.input_size()) +
71 " inputs; while the input dimension size is " +
72 c10::to_string(op_dims.size()));
74 op_types.size() == op.input_size(),
75 op.name() +
" has " + c10::to_string(op.input_size()) +
76 " inputs; while the input type size is " +
77 c10::to_string(op_types.size()));
79 for (
size_t j = 0; j < op.input_size(); ++j) {
80 inputs_[op.input(j)] =
81 std::make_pair(get_tensor_filler(op, j, op_dims), op_types[j]);
89 if (op.type().find(
"SparseLengthsWeighted") == 0 && i > 0) {
90 const auto& prev_op = run_net.op(i - 1);
91 if (prev_op.type() ==
"Gather") {
92 const auto& prev_dims = input_dims[i - 1];
93 VLOG(1) <<
"Setting max length value to " << prev_dims[0].front()
94 <<
" for " << op.input(3);
95 inputs_[op.input(3)].first.Max(prev_dims[0].front());
99 for (
size_t j = 0; j < op.output_size(); ++j) {
100 output_names.emplace(op.output(j));
105 std::unordered_set<std::string> parameters;
106 for (
size_t i = 0; i < run_net.arg_size(); ++i) {
107 const auto& arg = run_net.arg(i);
109 if (arg.has_name() && arg.name() ==
"PredictorParameters") {
110 parameters.reserve(arg.strings_size());
111 for (
size_t j = 0; j < arg.strings_size(); ++j) {
112 parameters.emplace(arg.strings(j));
117 if (parameters.size() == 0) {
118 VLOG(1) <<
"Fail to find any parameters";
120 for (
const auto& param : parameters) {
122 if (inputs_.find(param) != inputs_.end()) {
124 parameters_.emplace(param, inputs_[param]);
128 for (
const auto& param : parameters_) {
129 inputs_.erase(param.first);
131 for (
const auto& name : output_names) {
134 CAFFE_ENFORCE(inputs_.size() > 0,
"Empty input for run net");
137 for (
const auto& input : inputs_) {
138 input_names_.push_back(input.first);
142 void DataRandomFiller::fill_parameter(Workspace* ws)
const {
143 for (
auto& param : parameters_) {
144 Blob* blob = ws->CreateBlob(param.first);
148 BlobGetMutableTensor(blob, CPU));
149 CAFFE_ENFORCE(ws->GetBlob(param.first)->GetRaw());
153 void DataRandomFiller::fill_input_internal(TensorList_t* input_data)
const {
154 for (
auto& name : input_names_) {
155 input_data->emplace_back(CPU);
156 const auto& it = inputs_.find(name);
157 CAFFE_ENFORCE(it != inputs_.end());
158 fill_with_type(it->second.first, it->second.second, &input_data->back());
162 TestDataRandomFiller::TestDataRandomFiller(
164 const std::vector<std::vector<std::vector<int64_t>>>& inputDims,
165 const std::vector<std::vector<std::string>>& inputTypes)
166 : DataRandomFiller() {
167 std::unordered_set<std::string> outputNames;
169 for (
auto opIdx = 0; opIdx < net.op_size(); ++opIdx) {
170 const auto& op = net.op(opIdx);
171 for (
auto outputIdx = 0; outputIdx < op.output_size(); ++outputIdx) {
172 outputNames.emplace(op.output(outputIdx));
176 std::unordered_set<size_t> opWithRequiredInputs;
177 for (
auto opIdx = 0; opIdx < net.op_size(); ++opIdx) {
178 const auto& op = net.op(opIdx);
179 for (
auto inputIdx = 0; inputIdx < op.input_size(); ++inputIdx) {
180 if (!outputNames.count(op.input(inputIdx))) {
181 opWithRequiredInputs.emplace(opIdx);
187 CAFFE_ENFORCE_EQ(inputDims.size(), opWithRequiredInputs.size());
188 CAFFE_ENFORCE_EQ(inputTypes.size(), opWithRequiredInputs.size());
191 for (
auto opIdx = 0; opIdx < net.op_size(); ++opIdx) {
192 if (!opWithRequiredInputs.count(opIdx)) {
196 const auto& op = net.op(opIdx);
197 const auto& op_dims = inputDims[counter];
198 const auto& op_types = inputTypes[counter];
201 int countRequiredInputs = 0;
202 for (
auto inputIdx = 0; inputIdx < op.input_size(); ++inputIdx) {
203 if (!outputNames.count(op.input(inputIdx))) {
204 ++countRequiredInputs;
209 op_dims.size() == countRequiredInputs,
210 op.name() +
" has " + c10::to_string(op.input_size()) +
211 " (required) inputs; while the input dimension size is " +
212 c10::to_string(op_dims.size()));
214 op_types.size() == countRequiredInputs,
215 op.name() +
" has " + c10::to_string(op.input_size()) +
216 " (required) inputs; while the input type size is " +
217 c10::to_string(op_types.size()));
220 for (
auto inputIdx = 0; inputIdx < op.input_size(); ++inputIdx) {
221 auto inputName = op.input(inputIdx);
222 if (outputNames.count(inputName)) {
226 inputs_[inputName] = std::make_pair(
227 get_tensor_filler(op, dimCounter, op_dims), op_types[dimCounter]);
231 CAFFE_ENFORCE(inputs_.size() > 0,
"Empty input for run net");
233 for (
const auto& input : inputs_) {
234 input_names_.push_back(input.first);
238 void TestDataRandomFiller::fillInputToWorkspace(Workspace* workspace)
const {
239 for (
auto& name : input_names_) {
240 const auto& it = inputs_.find(name);
241 CAFFE_ENFORCE(it != inputs_.end());
243 BlobGetMutableTensor(workspace->CreateBlob(name), caffe2::CPU);
244 fill_with_type(it->second.first, it->second.second, tensor);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...