-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
custom_input_processor.cc
332 lines (296 loc) · 12.4 KB
/
custom_input_processor.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/segmentation_platform/internal/execution/processing/custom_input_processor.h"
#include "base/strings/string_number_conversions.h"
#include "base/system/sys_info.h"
#include "base/task/sequenced_task_runner.h"
#include "components/segmentation_platform/internal/database/ukm_types.h"
#include "components/segmentation_platform/internal/execution/processing/feature_processor_state.h"
#include "components/segmentation_platform/internal/execution/processing/processing_utils.h"
#include "components/segmentation_platform/internal/metadata/metadata_utils.h"
#include "components/segmentation_platform/public/input_delegate.h"
#include "components/segmentation_platform/public/proto/model_metadata.pb.h"
#if BUILDFLAG(IS_ANDROID)
#include "components/segmentation_platform/internal/android/execution/processing/custom_device_utils.h"
#endif // BUILDFLAG(IS_ANDROID)
namespace segmentation_platform::processing {
namespace {
absl::optional<int> GetArgAsInt(
const google::protobuf::Map<std::string, std::string>& args,
const std::string& key) {
int value;
auto iter = args.find(key);
// Did not find target key.
if (iter == args.end())
return absl::optional<int>();
// Perform string to int conversion, return empty value if the conversion
// failed.
if (!base::StringToInt(base::StringPiece(iter->second), &value))
return absl::optional<int>();
return absl::optional<int>(value);
}
} // namespace
CustomInputProcessor::CustomInputProcessor(
const base::Time prediction_time,
InputDelegateHolder* input_delegate_holder)
: input_delegate_holder_(input_delegate_holder),
prediction_time_(prediction_time) {}
CustomInputProcessor::CustomInputProcessor(
base::flat_map<FeatureIndex, Data>&& data,
const base::Time prediction_time,
InputDelegateHolder* input_delegate_holder)
: input_delegate_holder_(input_delegate_holder),
prediction_time_(prediction_time) {
for (const auto& item : data) {
custom_inputs_[item.first] =
std::move(item.second.input_feature->custom_input());
}
}
CustomInputProcessor::~CustomInputProcessor() = default;
void CustomInputProcessor::Process(
std::unique_ptr<FeatureProcessorState> feature_processor_state,
QueryProcessorCallback callback) {
auto result = std::make_unique<base::flat_map<FeatureIndex, Tensor>>();
ProcessIndexType<FeatureIndex>(std::move(custom_inputs_),
std::move(feature_processor_state),
std::move(result), std::move(callback));
}
template <typename IndexType>
void CustomInputProcessor::ProcessIndexType(
base::flat_map<IndexType, proto::CustomInput> custom_inputs,
std::unique_ptr<FeatureProcessorState> feature_processor_state,
std::unique_ptr<base::flat_map<IndexType, Tensor>> result,
TemplateCallback<IndexType> callback) {
bool success = true;
auto it = custom_inputs.begin();
for (; it != custom_inputs.end(); it = custom_inputs.begin()) {
// Get the next feature in the list to process.
const proto::CustomInput custom_input(std::move(it->second));
const IndexType index = it->first;
custom_inputs.erase(it);
InputDelegate* input_delegate = nullptr;
if (input_delegate_holder_) {
input_delegate =
input_delegate_holder_->GetDelegate(custom_input.fill_policy());
}
if (input_delegate) {
// If a delegate is available then use it to process the input. All the
// state in this method is moved, so it is ok even if the client ran the
// callback without posting it.
const FeatureProcessorState& state = *feature_processor_state;
input_delegate->Process(
custom_input, state,
base::BindOnce(
&CustomInputProcessor::OnGotProcessedValue<IndexType>,
weak_ptr_factory_.GetWeakPtr(), std::move(custom_inputs),
std::move(feature_processor_state), std::move(result),
std::move(callback), index, custom_input.tensor_length()));
return;
}
DCHECK(custom_input.tensor_length() != 0);
// Validate the proto::CustomInput metadata.
if (metadata_utils::ValidateMetadataCustomInput(custom_input) !=
metadata_utils::ValidationResult::kValidationSuccess) {
success = false;
} else {
(*result)[index] =
ProcessSingleCustomInput(custom_input, feature_processor_state.get());
}
}
// Processing of the feature list has completed.
DCHECK(custom_inputs.empty());
if (!success || feature_processor_state->error()) {
result->clear();
feature_processor_state->SetError(
stats::FeatureProcessingError::kCustomInputError);
}
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), std::move(feature_processor_state),
std::move(*result)));
}
template <typename IndexType>
void CustomInputProcessor::OnGotProcessedValue(
base::flat_map<IndexType, proto::CustomInput> custom_inputs,
std::unique_ptr<FeatureProcessorState> feature_processor_state,
std::unique_ptr<base::flat_map<IndexType, Tensor>> result,
TemplateCallback<IndexType> callback,
IndexType current_index,
size_t current_tensor_length,
bool error,
Tensor current_value) {
if (error) {
feature_processor_state->SetError(
stats::FeatureProcessingError::kCustomInputError);
} else {
DCHECK_EQ(current_tensor_length, current_value.size());
}
(*result)[current_index] = std::move(current_value);
ProcessIndexType<IndexType>(std::move(custom_inputs),
std::move(feature_processor_state),
std::move(result), std::move(callback));
}
using SqlCustomInputIndex = std::pair<int, int>;
template void CustomInputProcessor::ProcessIndexType(
base::flat_map<SqlCustomInputIndex, proto::CustomInput> custom_inputs,
std::unique_ptr<FeatureProcessorState> feature_processor_state,
std::unique_ptr<base::flat_map<SqlCustomInputIndex, Tensor>> result,
TemplateCallback<std::pair<int, int>> callback);
template void CustomInputProcessor::OnGotProcessedValue(
base::flat_map<SqlCustomInputIndex, proto::CustomInput> custom_inputs,
std::unique_ptr<FeatureProcessorState> feature_processor_state,
std::unique_ptr<base::flat_map<SqlCustomInputIndex, Tensor>> result,
TemplateCallback<SqlCustomInputIndex> callback,
SqlCustomInputIndex current_index,
size_t current_tensor_length,
bool success,
Tensor current_value);
QueryProcessor::Tensor CustomInputProcessor::ProcessSingleCustomInput(
const proto::CustomInput& custom_input,
FeatureProcessorState* feature_processor_state) {
std::vector<ProcessedValue> tensor_result;
if (custom_input.fill_policy() == proto::CustomInput::UNKNOWN_FILL_POLICY) {
// When parsing a CustomInput object, if the fill policy is not
// supported by the current version of the client, the fill policy field
// will not be filled. When this happens, the custom input processor
// will either use the default values to generate an input tensor or
// fail the model execution.
tensor_result = std::vector<ProcessedValue>(
custom_input.default_value().begin(),
custom_input.default_value().begin() + custom_input.tensor_length());
} else if (custom_input.fill_policy() ==
proto::CustomInput::FILL_PREDICTION_TIME) {
if (!AddPredictionTime(custom_input, tensor_result))
feature_processor_state->SetError(
stats::FeatureProcessingError::kCustomInputError);
} else if (custom_input.fill_policy() ==
proto::CustomInput::TIME_RANGE_BEFORE_PREDICTION) {
if (!AddTimeRangeBeforePrediction(custom_input, tensor_result))
feature_processor_state->SetError(
stats::FeatureProcessingError::kCustomInputError);
} else if (custom_input.fill_policy() ==
proto::CustomInput::FILL_FROM_INPUT_CONTEXT) {
if (!AddFromInputContext(custom_input, feature_processor_state,
tensor_result))
feature_processor_state->SetError(
stats::FeatureProcessingError::kCustomInputError);
} else if (custom_input.fill_policy() ==
proto::CustomInput::FILL_DEVICE_RAM_MB) {
if (!AddDeviceRAMInMB(custom_input, tensor_result)) {
feature_processor_state->SetError(
stats::FeatureProcessingError::kCustomInputError);
}
} else if (custom_input.fill_policy() ==
proto::CustomInput::FILL_DEVICE_OS_VERSION_NUMBER) {
if (!AddDeviceOSVersionNumber(custom_input, tensor_result)) {
feature_processor_state->SetError(
stats::FeatureProcessingError::kCustomInputError);
}
} else if (custom_input.fill_policy() ==
proto::CustomInput::FILL_DEVICE_PPI) {
if (!AddDevicePPI(custom_input, tensor_result)) {
feature_processor_state->SetError(
stats::FeatureProcessingError::kCustomInputError);
}
} else if (custom_input.fill_policy() ==
proto::CustomInput::PRICE_TRACKING_HINTS) {
feature_processor_state->SetError(
stats::FeatureProcessingError::kCustomInputError);
NOTREACHED() << "InputDelegate is not found";
}
return tensor_result;
}
bool CustomInputProcessor::AddFromInputContext(
const proto::CustomInput& custom_input,
FeatureProcessorState* feature_processor_state,
std::vector<ProcessedValue>& out_tensor) {
if (custom_input.tensor_length() != 1) {
return false;
}
const auto& input_context = feature_processor_state->input_context();
if (!input_context) {
feature_processor_state->SetError(
stats::FeatureProcessingError::kCustomInputError,
"input context missing");
return false;
}
auto input_name = custom_input.name();
auto custom_input_iter = custom_input.additional_args().find("name");
if (custom_input_iter != custom_input.additional_args().end()) {
input_name = custom_input_iter->second;
}
auto input_context_iter = input_context->metadata_args.find(input_name);
if (input_context_iter == input_context->metadata_args.end()) {
feature_processor_state->SetError(
stats::FeatureProcessingError::kCustomInputError,
"The model expects an input '" + input_name +
"' which wasn't found in the input context.");
return false;
}
out_tensor.emplace_back(input_context_iter->second);
return true;
}
bool CustomInputProcessor::AddPredictionTime(
const proto::CustomInput& custom_input,
std::vector<ProcessedValue>& out_tensor) {
if (custom_input.tensor_length() != 1) {
return false;
}
out_tensor.emplace_back(prediction_time_);
return true;
}
bool CustomInputProcessor::AddTimeRangeBeforePrediction(
const proto::CustomInput& custom_input,
std::vector<ProcessedValue>& out_tensor) {
if (custom_input.tensor_length() != 2) {
return false;
}
constexpr char kBucketCountArg[] = "bucket_count";
absl::optional<int> bucket_count =
GetArgAsInt(custom_input.additional_args(), kBucketCountArg);
if (bucket_count.has_value()) {
out_tensor.emplace_back(prediction_time_ -
base::Days(bucket_count.value()));
out_tensor.emplace_back(prediction_time_);
} else {
return false;
}
return true;
}
bool CustomInputProcessor::AddDeviceRAMInMB(
const proto::CustomInput& custom_input,
std::vector<ProcessedValue>& out_tensor) {
if (custom_input.tensor_length() != 1) {
return false;
}
float device_ram_in_mb = base::SysInfo::AmountOfPhysicalMemoryMB();
out_tensor.emplace_back(device_ram_in_mb);
return true;
}
bool CustomInputProcessor::AddDeviceOSVersionNumber(
const proto::CustomInput& custom_input,
std::vector<ProcessedValue>& out_tensor) {
if (custom_input.tensor_length() != 1) {
return false;
}
std::string os_version = base::SysInfo::OperatingSystemVersion();
float device_os_version = processing::ProcessOsVersionString(os_version);
out_tensor.emplace_back(device_os_version);
return true;
}
bool CustomInputProcessor::AddDevicePPI(
const proto::CustomInput& custom_input,
std::vector<ProcessedValue>& out_tensor) {
if (custom_input.tensor_length() != 1) {
return false;
}
#if BUILDFLAG(IS_ANDROID)
float device_ppi = CustomDeviceUtils::GetDevicePPI();
out_tensor.emplace_back(device_ppi);
return true;
#else
return false;
#endif // BUILDFLAG(IS_ANDROID)
}
} // namespace segmentation_platform::processing