-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reland "Repurpose the BatchAnnotateValidation feature to be general f…
…or all PCA" This is a reland of commit e04d17b Original change's description: > Repurpose the BatchAnnotateValidation feature to be general for all PCA > > Changes the existing BatchAnnotateValidation feature to be fully > configurable for all annotation types, changing its name in the process. > > This can be enabled via the command line for integration testing (where > it will also output the inference via JSON), or by feature flag for > performance testing in the wild. > > Also moves the feature to a separate class which is null whenever the > feature is disabled. > > Bug: 1288877 > Change-Id: I85757e5e401d4f75898bc45d83b4e93ccd079175 > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3635390 > Reviewed-by: Sophie Chang <sophiechang@chromium.org> > Commit-Queue: Robert Ogden <robertogden@chromium.org> > Reviewed-by: Yao Xiao <yaoxia@chromium.org> > Cr-Commit-Position: refs/heads/main@{#1001913} Cq-Include-Trybots: luci.chromium.try:linux_chromium_chromeos_asan_rel_ng Cq-Include-Trybots: luci.chromium.try:linux_chromium_tsan_rel_ng Cq-Include-Trybots: luci.chromium.try:linux_chromium_asan_rel_ng Bug: 1288877 Change-Id: I7d03cd387343857e7a7a2845fb0a63ff40a31be4 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3640407 Reviewed-by: Sophie Chang <sophiechang@chromium.org> Commit-Queue: Robert Ogden <robertogden@chromium.org> Reviewed-by: Yao Xiao <yaoxia@chromium.org> Cr-Commit-Position: refs/heads/main@{#1002504}
- Loading branch information
Robert Ogden
authored and
Chromium LUCI CQ
committed
May 12, 2022
1 parent
a62a5a8
commit f7e2f97
Showing
24 changed files
with
861 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
components/optimization_guide/content/browser/page_content_annotations_validator.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "components/optimization_guide/content/browser/page_content_annotations_validator.h" | ||
|
||
#include "base/bind.h" | ||
#include "base/callback.h" | ||
#include "base/logging.h" | ||
#include "base/memory/ptr_util.h" | ||
#include "base/rand_util.h" | ||
#include "base/strings/strcat.h" | ||
#include "base/strings/stringprintf.h" | ||
#include "base/time/default_tick_clock.h" | ||
#include "components/optimization_guide/content/browser/page_content_annotator.h" | ||
#include "components/optimization_guide/core/optimization_guide_features.h" | ||
#include "components/optimization_guide/core/optimization_guide_switches.h" | ||
#include "components/optimization_guide/core/page_content_annotations_common.h" | ||
|
||
namespace optimization_guide { | ||
|
||
namespace { | ||
|
||
const char* kRandomNouns[] = { | ||
"Airplane", "Boat", "Book", "Dinosaur", "Earth", | ||
"Football", "Fork", "Hummingbird", "Magic Wand", "Mailbox", | ||
"Molecule", "Pizza", "Record Player", "Skeleton", "Soda", | ||
"Sphere", "Strawberry", "Tiger", "Turkey", "Wolf", | ||
}; | ||
const size_t kCountRandomNouns = 20; | ||
|
||
void LogAnnotationResultToConsole( | ||
const std::vector<BatchAnnotationResult>& results) { | ||
if (!switches::LogPageContentAnnotationsValidationToConsole()) { | ||
return; | ||
} | ||
|
||
LOG(ERROR) << "PageContentAnnotations Validation Complete:"; | ||
for (size_t i = 0; i < results.size(); i++) { | ||
LOG(ERROR) << " " << i << ": " << results[i].ToJSON(); | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
PageContentAnnotationsValidator::~PageContentAnnotationsValidator() = default; | ||
PageContentAnnotationsValidator::PageContentAnnotationsValidator( | ||
PageContentAnnotator* annotator) | ||
: annotator_(annotator) { | ||
DCHECK(annotator); | ||
for (AnnotationType type : { | ||
AnnotationType::kPageTopics, | ||
AnnotationType::kPageEntities, | ||
AnnotationType::kContentVisibility, | ||
}) { | ||
if (features::PageContentAnnotationValidationEnabledForType(type)) { | ||
enabled_annotation_types_.push_back(type); | ||
annotator_->RequestAndNotifyWhenModelAvailable(type, base::DoNothing()); | ||
} | ||
} | ||
|
||
timer_.Start(FROM_HERE, | ||
features::PageContentAnnotationValidationStartupDelay(), | ||
base::BindOnce(&PageContentAnnotationsValidator::Run, | ||
weak_ptr_factory_.GetWeakPtr())); | ||
} | ||
|
||
// static | ||
std::unique_ptr<PageContentAnnotationsValidator> | ||
PageContentAnnotationsValidator::MaybeCreateAndStartTimer( | ||
PageContentAnnotator* annotator) { | ||
// This can happen with certain build/feature flags. | ||
if (!annotator) { | ||
return nullptr; | ||
} | ||
|
||
bool enabled_for_any_type = false; | ||
for (AnnotationType type : { | ||
AnnotationType::kPageTopics, | ||
AnnotationType::kPageEntities, | ||
AnnotationType::kContentVisibility, | ||
}) { | ||
enabled_for_any_type |= | ||
features::PageContentAnnotationValidationEnabledForType(type); | ||
} | ||
if (!enabled_for_any_type) { | ||
return nullptr; | ||
} | ||
|
||
// This is done because |PageContentAnnotationsValidator| has a private ctor. | ||
return base::WrapUnique(new PageContentAnnotationsValidator(annotator)); | ||
} | ||
|
||
void PageContentAnnotationsValidator::Run() { | ||
for (AnnotationType type : enabled_annotation_types_) { | ||
annotator_->Annotate(base::BindOnce(&LogAnnotationResultToConsole), | ||
BuildInputsForType(type), type); | ||
} | ||
} | ||
|
||
// static | ||
std::vector<std::string> PageContentAnnotationsValidator::BuildInputsForType( | ||
AnnotationType type) { | ||
absl::optional<std::vector<std::string>> cmd_line_input = | ||
switches::PageContentAnnotationsValidationInputForType(type); | ||
if (cmd_line_input) { | ||
return *cmd_line_input; | ||
} | ||
|
||
std::vector<std::string> inputs; | ||
for (size_t i = 0; i < features::PageContentAnnotationsValidationBatchSize(); | ||
i++) { | ||
const char* word1 = kRandomNouns[base::RandGenerator(kCountRandomNouns)]; | ||
const char* word2 = kRandomNouns[base::RandGenerator(kCountRandomNouns)]; | ||
inputs.emplace_back(base::StrCat({word1, " ", word2})); | ||
} | ||
return inputs; | ||
} | ||
|
||
} // namespace optimization_guide |
57 changes: 57 additions & 0 deletions
57
components/optimization_guide/content/browser/page_content_annotations_validator.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef COMPONENTS_OPTIMIZATION_GUIDE_CONTENT_BROWSER_PAGE_CONTENT_ANNOTATIONS_VALIDATOR_H_ | ||
#define COMPONENTS_OPTIMIZATION_GUIDE_CONTENT_BROWSER_PAGE_CONTENT_ANNOTATIONS_VALIDATOR_H_ | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "base/memory/raw_ptr.h" | ||
#include "base/memory/weak_ptr.h" | ||
#include "base/timer/timer.h" | ||
#include "components/optimization_guide/core/page_content_annotation_type.h" | ||
|
||
namespace optimization_guide { | ||
|
||
class PageContentAnnotator; | ||
|
||
// This class manages validation runs of the PageContentAnnotationsService, | ||
// running the ML model for a given AnnotationType on dummy data after some | ||
// delay from browser startup. This feature can be controlled by experimental | ||
// feature flags and command line. | ||
class PageContentAnnotationsValidator { | ||
public: | ||
~PageContentAnnotationsValidator(); | ||
|
||
// If the appropriate feature flag or command line switch is given, an | ||
// instance of |this| is created, else nullptr. | ||
static std::unique_ptr<PageContentAnnotationsValidator> | ||
MaybeCreateAndStartTimer(PageContentAnnotator* annotator); | ||
|
||
private: | ||
explicit PageContentAnnotationsValidator(PageContentAnnotator* annotator); | ||
|
||
// Runs the validation for all enabled AnnotationTypes. | ||
void Run(); | ||
|
||
// Creates a set of dummy input data to run for the given |type|, either | ||
// randomly generated off of experiment parameters or given on the command | ||
// line. | ||
static std::vector<std::string> BuildInputsForType(AnnotationType type); | ||
|
||
std::vector<AnnotationType> enabled_annotation_types_; | ||
|
||
// Out lives |this|, not owned. | ||
raw_ptr<PageContentAnnotator> annotator_; | ||
|
||
// Starts in the ctor, roughly on browser start, and calls |Run|. | ||
base::OneShotTimer timer_; | ||
|
||
base::WeakPtrFactory<PageContentAnnotationsValidator> weak_ptr_factory_{this}; | ||
}; | ||
|
||
} // namespace optimization_guide | ||
|
||
#endif // COMPONENTS_OPTIMIZATION_GUIDE_CONTENT_BROWSER_PAGE_CONTENT_ANNOTATIONS_VALIDATOR_H_ |
Oops, something went wrong.