// Copyright 2023 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_BROWSING_TOPICS_ANNOTATOR_IMPL_H_ #define COMPONENTS_BROWSING_TOPICS_ANNOTATOR_IMPL_H_ #include <optional> #include <string> #include <unordered_map> #include <vector> #include "base/callback_list.h" #include "base/files/file_path.h" #include "base/functional/callback.h" #include "base/memory/weak_ptr.h" #include "base/sequence_checker.h" #include "base/task/sequenced_task_runner.h" #include "components/browsing_topics/annotator.h" #include "components/optimization_guide/core/bert_model_handler.h" namespace optimization_guide { class OptimizationGuideModelProvider; } namespace browsing_topics { // An implementation of the |Annotator| base class. This Annotator supports // concurrent batch annotations and manages the lifetimes of all underlying // components. This class must only be owned and called on the UI thread. // // |BatchAnnotate| is the main entry point for callers. The callback given to // |BatchAnnotate| is forwarded through many subsequent PostTasks until all // annotations are ready to be returned to the caller. // // Life of an Annotation: // 1. |BatchAnnotate| checks if the override list needs to be loaded. If so, it // is done on a background thread. After that check and possibly loading the // list in |OnOverrideListLoadAttemptDone|, |StartBatchAnnotate| is called. // 2. |StartBatchAnnotate| shares ownership of the |BatchAnnotationCallback| // among a series of callbacks (using |base::BarrierClosure|), one for each // input. Ownership of the inputs is moved to the heap where all individual // model executions can reference their input and set their output. // 3. |AnnotateSingleInput| runs a single annotation, first checking the // override list if available. If the input is not covered in the override list, // the ML model is run on a background thread. // 4. |PostprocessCategoriesToBatchAnnotationResult| is called to post-process // the output of the ML model. // 5. |OnBatchComplete| is called by the barrier closure which passes the // annotations back to the caller and unloads the model if no other batches are // in progress. class AnnotatorImpl : public Annotator, public optimization_guide::BertModelHandler { … }; } // namespace browsing_topics #endif // COMPONENTS_BROWSING_TOPICS_ANNOTATOR_IMPL_H_