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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_OPTIMIZATION_GUIDE_CORE_INFERENCE_MODEL_HANDLER_H_
#define COMPONENTS_OPTIMIZATION_GUIDE_CORE_INFERENCE_MODEL_HANDLER_H_
#include <optional>
#include "base/callback_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_functions.h"
#include "base/sequence_checker.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "base/types/optional_ref.h"
#include "components/optimization_guide/core/delivery/model_util.h"
#include "components/optimization_guide/core/delivery/optimization_guide_model_provider.h"
#include "components/optimization_guide/core/delivery/optimization_target_model_observer.h"
#include "components/optimization_guide/core/inference/model_executor.h"
#include "components/optimization_guide/core/optimization_guide_util.h"
#include "components/optimization_guide/proto/models.pb.h"
namespace optimization_guide {
namespace {
void RecordTaskExecutionLatency(proto::OptimizationTarget optimization_target,
base::TimeDelta execution_time) {
base::UmaHistogramMediumTimes(
"OptimizationGuide.ModelExecutor.TaskExecutionLatency." +
optimization_guide::GetStringNameForOptimizationTarget(
optimization_target),
execution_time);
}
} // namespace
// This class owns and handles the execution of models on the UI thread.
// Derived classes must provide an implementation of `ModelExecutor`
// which is then owned by `this`. The passed executor will be called
// and destroyed on the thread specified by `model_executor_task_runner`,
// which is all handled by this class.
//
// Derived classes that override `OnModelUpdated` must call the parent
// `OnModelUpdated` as the first step, for the internal state to be updated.
template <class OutputType, class InputType>
class ModelHandler : public OptimizationTargetModelObserver {
public:
ModelHandler(
OptimizationGuideModelProvider* model_provider,
scoped_refptr<base::SequencedTaskRunner> model_executor_task_runner,
std::unique_ptr<ModelExecutor<OutputType, InputType>> model_executor,
// Passing nullopt will use a default value.
std::optional<base::TimeDelta> model_inference_timeout,
proto::OptimizationTarget optimization_target,
const std::optional<proto::Any>& model_metadata)
: model_provider_(model_provider),
optimization_target_(optimization_target),
model_executor_(std::move(model_executor)),
model_executor_task_runner_(model_executor_task_runner) {
DCHECK(model_provider_);
DCHECK(model_executor_);
DCHECK_NE(optimization_target_,
proto::OptimizationTarget::OPTIMIZATION_TARGET_UNKNOWN);
base::UmaHistogramBoolean(
"OptimizationGuide.ModelHandler.HandlerCreated." +
GetStringNameForOptimizationTarget(optimization_target_),
true);
handler_created_time_ = base::TimeTicks::Now();
model_executor_->InitializeAndMoveToExecutionThread(
model_inference_timeout, optimization_target_,
model_executor_task_runner_,
base::SequencedTaskRunner::GetCurrentDefault());
// Run this after the executor is initialized in case the model is already
// available.
model_provider_->AddObserverForOptimizationTargetModel(
optimization_target_, model_metadata, this);
}
~ModelHandler() override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
model_provider_->RemoveObserverForOptimizationTargetModel(
optimization_target_, this);
// |model_executor_|'s WeakPtrs are used on the model thread, so
// that is also where the class must be destroyed.
model_executor_task_runner_->DeleteSoon(FROM_HERE,
std::move(model_executor_));
}
ModelHandler(const ModelHandler&) = delete;
ModelHandler& operator=(const ModelHandler&) = delete;
// Executes the model using |input| and invokes |callback| on the UI thread
// when completed. Virtual for testing.
// TODO(crbug.com/40167079): Add a way to surface errors.
using ExecutionCallback =
base::OnceCallback<void(const std::optional<OutputType>&)>;
virtual void ExecuteModelWithInput(ExecutionCallback callback,
InputType input) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
model_executor_task_runner_->PostTask(
FROM_HERE, GetExecutionTask(std::move(callback), input));
}
// Same as the method above. But also receives a `base::CancelableTaskTracker`
// for cancelling the execution. Keep in mind that CancelableTaskTracker
// cannot cancel tasks that have already started to run. Virtual for testing.
// TODO(crbug.com/40167079): Add a way to surface errors.
virtual void ExecuteModelWithInput(base::CancelableTaskTracker* tracker,
ExecutionCallback callback,
InputType input) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
tracker->PostTask(model_executor_task_runner_.get(), FROM_HERE,
GetExecutionTask(std::move(callback), input));
}
// Variants of the above |ExecuteModelWithInput| but which support running
// multiple model executions in the same call stack. It is guaranteed that the
// output passed to |BatchExecutionCallback| will always be in the same order
// as the input vector.
//
// IMPORTANT: Running the model multiple times in the same PostTask means that
// it will take longer for Chrome's threadpool to reuse these CPU cycles for
// other work, especially for high priority tasks. This method should only be
// used in time-sensitive applications, for example when the model output is
// used on UI surfaces. Otherwise use multiple calls to
// |ExecuteModelWithInput| with a |base::BarrierClosure| (see
// page_content_annotation_job_executor.cc for an example and explanation).
using BatchExecutionCallback =
base::OnceCallback<void(const std::vector<std::optional<OutputType>>&)>;
virtual void BatchExecuteModelWithInput(
BatchExecutionCallback callback,
typename ModelExecutor<OutputType, InputType>::ConstRefInputVector
batch_input) {
model_executor_task_runner_->PostTask(
FROM_HERE, GetBatchExecutionTask(std::move(callback), batch_input));
}
// See above comment.
virtual void BatchExecuteModelWithInput(
base::CancelableTaskTracker* tracker,
BatchExecutionCallback callback,
typename ModelExecutor<OutputType, InputType>::ConstRefInputVector
batch_input) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
tracker->PostTask(model_executor_task_runner_.get(), FROM_HERE,
GetBatchExecutionTask(std::move(callback), batch_input));
}
// Runs synchronous batch model execution.
// Returns batch model outputs.
std::vector<std::optional<OutputType>> BatchExecuteModelWithInputSync(
typename ModelExecutor<OutputType, InputType>::ConstRefInputVector
inputs) {
base::ElapsedTimer timer;
auto batch_model_outputs =
model_executor_->SendForBatchExecutionSync(inputs);
RecordTaskExecutionLatency(optimization_target_,
/*execution_time=*/timer.Elapsed());
return batch_model_outputs;
}
// Note that keeping the model in memory for a long duration may be detected
// as a memory leak in Chrome, and will always increase the private or shared
// memory used by the browser by the size of the model file and the
// constructed TFLite graph.
void SetShouldUnloadModelOnComplete(bool should_auto_unload) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
model_executor_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(
&ModelExecutor<OutputType,
InputType>::SetShouldUnloadModelOnComplete,
model_executor_->GetWeakPtrForExecutionThread(),
should_auto_unload));
}
// Note that keeping the model in memory for a long duration may be detected
// as a memory leak in Chrome, and will always increase the private or shared
// memory used by the browser by the size of the model file and the
// constructed TFLite graph.
void SetShouldPreloadModel(bool should_preload_model) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
model_executor_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(
&ModelExecutor<OutputType, InputType>::SetShouldPreloadModel,
model_executor_->GetWeakPtrForExecutionThread(),
should_preload_model));
}
// Requests that the model executor unload the model from memory, if it is
// currently loaded. Virtual to allow derived classes to also observe this
// signal.
virtual void UnloadModel() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
model_executor_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&ModelExecutor<OutputType, InputType>::UnloadModel,
model_executor_->GetWeakPtrForExecutionThread()));
}
// OptimizationTargetModelObserver:
void OnModelUpdated(proto::OptimizationTarget optimization_target,
base::optional_ref<const ModelInfo> model_info) override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::optional<base::FilePath> model_file_path;
if (optimization_target_ != optimization_target) {
return;
}
if (handler_created_time_) {
base::UmaHistogramMediumTimes(
"OptimizationGuide.ModelHandler.HandlerCreatedToModelAvailable." +
GetStringNameForOptimizationTarget(optimization_target_),
base::TimeTicks::Now() - *handler_created_time_);
handler_created_time_ = std::nullopt;
}
model_available_ = model_info.has_value();
if (model_info.has_value()) {
model_info_ = *model_info;
model_file_path = model_info->GetModelFilePath();
} else {
model_info_ = std::nullopt;
}
model_executor_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&ModelExecutor<OutputType, InputType>::UpdateModelFile,
model_executor_->GetWeakPtrForExecutionThread(),
model_file_path));
// Run any observing callbacks after the model file is posted to the
// model executor thread so that any model execution requests are posted to
// the model executor thread after the model update.
on_model_updated_callbacks_.Notify();
}
// Returns whether a model is available to be executed. Virtual for testing.
virtual bool ModelAvailable() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return model_available_;
}
// Runs |callback| now if |ModelAvailable()| or the next time |OnModelUpdated|
// is called.
void AddOnModelUpdatedCallback(base::OnceClosure callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (ModelAvailable()) {
std::move(callback).Run();
return;
}
// callbacks are not bound locally are are safe to be destroyed at any time.
on_model_updated_callbacks_.AddUnsafe(std::move(callback));
}
std::optional<ModelInfo> GetModelInfo() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return model_info_;
}
// Validates that the model info's metadata is of the same type and is
// parseable as |T|. Will return metadata if all checks pass.
template <class T>
requires(std::is_convertible_v<T*, google::protobuf::MessageLite*>)
std::optional<T> ParsedSupportedFeaturesForLoadedModel() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!model_info_ || !model_info_->GetModelMetadata()) {
return std::nullopt;
}
return ParsedAnyMetadata<T>(*model_info_->GetModelMetadata());
}
private:
// Returns a closure supplied with |callback| and |input| for model execution.
base::OnceClosure GetExecutionTask(ExecutionCallback callback,
InputType input) {
base::TimeTicks now = base::TimeTicks::Now();
ExecutionCallback on_complete_callback =
base::BindOnce(&ModelHandler::OnExecutionCompleted, std::move(callback),
optimization_target_, now);
return base::BindOnce(
&ModelExecutor<OutputType, InputType>::SendForExecution,
model_executor_->GetWeakPtrForExecutionThread(),
std::move(on_complete_callback), now, input);
}
// Returns a closure supplied with |callback| and |inputs| for model
// execution.
base::OnceClosure GetBatchExecutionTask(
BatchExecutionCallback callback,
typename ModelExecutor<OutputType, InputType>::ConstRefInputVector
inputs) {
base::TimeTicks now = base::TimeTicks::Now();
BatchExecutionCallback on_complete_callback =
base::BindOnce(&ModelHandler::OnBatchExecutionCompleted,
std::move(callback), optimization_target_, now);
return base::BindOnce(
&ModelExecutor<OutputType, InputType>::SendForBatchExecution,
model_executor_->GetWeakPtrForExecutionThread(),
std::move(on_complete_callback), now, inputs);
}
// This is called by |model_executor_|. This method does not have to be
// static, but because it is stateless we've made it static so that we don't
// have to have this class support WeakPointers on the calling thread.
static void OnExecutionCompleted(
ExecutionCallback callback,
proto::OptimizationTarget optimization_target,
base::TimeTicks model_execute_start_time,
const std::optional<OutputType>& output) {
RecordTaskExecutionLatency(
optimization_target,
/*execution_time=*/base::TimeTicks::Now() - model_execute_start_time);
std::move(callback).Run(output);
}
static void OnBatchExecutionCompleted(
BatchExecutionCallback callback,
proto::OptimizationTarget optimization_target,
base::TimeTicks model_execute_start_time,
const std::vector<std::optional<OutputType>>& output) {
RecordTaskExecutionLatency(
optimization_target,
/*execution_time=*/base::TimeTicks::Now() - model_execute_start_time);
std::move(callback).Run(output);
}
// Not owned. Guaranteed to outlive |this|.
raw_ptr<OptimizationGuideModelProvider> model_provider_
GUARDED_BY_CONTEXT(sequence_checker_);
const proto::OptimizationTarget optimization_target_;
// The time that |optimization_target_| was registered wih |model_provider_|
// when |this| is created.
//
// Will only be non-nullopt if a model has not been received yet after the
// target was registered.
std::optional<base::TimeTicks> handler_created_time_;
// The owned model executor.
std::unique_ptr<ModelExecutor<OutputType, InputType>> model_executor_;
// The model executor task runner. Note that whenever a task is posted here,
// the task takes a reference to the TaskRunner (in a cyclic dependency) so
// |base::Unretained| is not safe anywhere in this class or the
// |model_executor_|.
scoped_refptr<base::SequencedTaskRunner> model_executor_task_runner_;
// Set in |OnModelUpdated|.
std::optional<ModelInfo> model_info_ GUARDED_BY_CONTEXT(sequence_checker_);
// Populated with callbacks if |AddOnModelUpdatedCallback| is called before a
// model file is available, then is notified when |OnModelUpdated| is called.
base::OnceClosureList on_model_updated_callbacks_
GUARDED_BY_CONTEXT(sequence_checker_);
// Set in |OnModelUpdated|.
bool model_available_ GUARDED_BY_CONTEXT(sequence_checker_) = false;
SEQUENCE_CHECKER(sequence_checker_);
};
} // namespace optimization_guide
#endif // COMPONENTS_OPTIMIZATION_GUIDE_CORE_INFERENCE_MODEL_HANDLER_H_
|