File: contextual_page_actions_model.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (177 lines) | stat: -rw-r--r-- 7,136 bytes parent folder | download | duplicates (3)
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
// 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/embedder/default_model/contextual_page_actions_model.h"

#include "base/feature_list.h"
#include "base/task/sequenced_task_runner.h"
#include "components/segmentation_platform/internal/metadata/metadata_writer.h"
#include "components/segmentation_platform/public/constants.h"
#include "components/segmentation_platform/public/features.h"
#include "components/segmentation_platform/public/model_provider.h"
#include "components/segmentation_platform/public/proto/model_metadata.pb.h"

namespace segmentation_platform {

namespace {
using proto::SegmentId;

// Label input size
constexpr int kLabelInputSize = 5;
// Default parameters for contextual page actions model.
constexpr SegmentId kSegmentId =
    SegmentId::OPTIMIZATION_TARGET_CONTEXTUAL_PAGE_ACTION_PRICE_TRACKING;
constexpr int64_t kOneDayInSeconds = 86400;
// Parameters for share action model.
constexpr std::array<MetadataWriter::UMAFeature, 6> kShareUMAFeatures = {
    MetadataWriter::UMAFeature::FromUserAction(
        "MobileMenuShare",
        ContextualPageActionsModel::kShareOutputCollectionDelayInSec),
    MetadataWriter::UMAFeature::FromUserAction(
        "Omnibox.EditUrlSuggestion.Share",
        ContextualPageActionsModel::kShareOutputCollectionDelayInSec),
    MetadataWriter::UMAFeature::FromUserAction(
        "MobileActionMode.Share",
        ContextualPageActionsModel::kShareOutputCollectionDelayInSec),
    MetadataWriter::UMAFeature::FromUserAction(
        "MobileMenuDirectShare",
        ContextualPageActionsModel::kShareOutputCollectionDelayInSec),
    MetadataWriter::UMAFeature::FromUserAction(
        "Omnibox.EditUrlSuggestion.Copy",
        ContextualPageActionsModel::kShareOutputCollectionDelayInSec),
    MetadataWriter::UMAFeature::FromUserAction(
        "Tab.Screenshot",
        ContextualPageActionsModel::kShareOutputCollectionDelayInSec),
};

constexpr std::array<const char*, kLabelInputSize>
    kContextualPageActionModelLabels = {
        kContextualPageActionModelLabelDiscounts,
        kContextualPageActionModelLabelPriceInsights,
        kContextualPageActionModelLabelPriceTracking,
        kContextualPageActionModelLabelReaderMode,
        kContextualPageActionModelLabelTabGrouping};

MetadataWriter::CustomInput CreateCustomInput(std::string name) {
  return MetadataWriter::CustomInput{
      .tensor_length = 1,
      .fill_policy = proto::CustomInput::FILL_FROM_INPUT_CONTEXT,
      .name = name.c_str()};
}

}  // namespace

ContextualPageActionsModel::ContextualPageActionsModel()
    : DefaultModelProvider(kSegmentId) {}

std::unique_ptr<DefaultModelProvider::ModelConfig>
ContextualPageActionsModel::GetModelConfig() {
  proto::SegmentationModelMetadata metadata;
  MetadataWriter writer(&metadata);
  writer.SetSegmentationMetadataConfig(
      proto::TimeUnit::SECOND, /*bucket_duration=*/1,
      /*signal_storage_length=*/kOneDayInSeconds,
      /*min_signal_collection_length=*/kOneDayInSeconds,
      /*result_time_to_live=*/kOneDayInSeconds);

  // Add discounts custom input.
  proto::CustomInput* discounts_input =
      writer.AddCustomInput(CreateCustomInput("discounts_input"));
  (*discounts_input->mutable_additional_args())["name"] =
      kContextualPageActionModelInputDiscounts;

  // Add price insights custom input.
  proto::CustomInput* price_insights_input =
      writer.AddCustomInput(CreateCustomInput("price_insights_input"));
  (*price_insights_input->mutable_additional_args())["name"] =
      kContextualPageActionModelInputPriceInsights;

  // Add price tracking custom input.
  proto::CustomInput* price_tracking_input =
      writer.AddCustomInput(CreateCustomInput("price_tracking_input"));
  (*price_tracking_input->mutable_additional_args())["name"] =
      kContextualPageActionModelInputPriceTracking;

  // Add reader mode custom input.
  proto::CustomInput* reader_mode_input =
      writer.AddCustomInput(CreateCustomInput("reader_mode_input"));
  (*reader_mode_input->mutable_additional_args())["name"] =
      kContextualPageActionModelInputReaderMode;

  // Add tab grouping cusotm input.
  proto::CustomInput* tab_grouping_input =
      writer.AddCustomInput(CreateCustomInput("tab_grouping_input"));
  (*tab_grouping_input->mutable_additional_args())["name"] =
      kContextualPageActionModelInputTabGrouping;

  if (base::FeatureList::IsEnabled(features::kContextualPageActionShareModel)) {
    // Add share related input features.
    writer.AddUmaFeatures(kShareUMAFeatures.data(), kShareUMAFeatures.size(),
                          false);

    metadata.set_upload_tensors(true);

    // Add share output collection with delay.
    writer.AddDelayTrigger(
        ContextualPageActionsModel::kShareOutputCollectionDelayInSec);
    writer.AddUmaFeatures(kShareUMAFeatures.data(), kShareUMAFeatures.size(),
                          true);
  }

  // A threshold used to differentiate labels with score zero from non-zero
  // values.
  const float threshold = 0.1f;

  // Set output config, labels, and classifier.
  writer.AddOutputConfigForMultiClassClassifier(
      kContextualPageActionModelLabels,
      /*top_k_outputs=*/1, threshold);

  constexpr int kModelVersion = 1;
  return std::make_unique<ModelConfig>(std::move(metadata), kModelVersion);
}

void ContextualPageActionsModel::ExecuteModelWithInput(
    const ModelProvider::Request& inputs,
    ExecutionCallback callback) {
  size_t expected_input_size =
      base::FeatureList::IsEnabled(features::kContextualPageActionShareModel)
          ? kShareUMAFeatures.size() + kLabelInputSize
          : kLabelInputSize;

  // Invalid inputs.
  if (inputs.size() != expected_input_size) {
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback), std::nullopt));
    return;
  }

  // TODO(haileywang): Use input[4] to input[9] to show share button.
  bool has_discounts = inputs[0];
  bool has_price_insights = inputs[1];
  bool can_track_price = inputs[2];
  bool has_reader_mode = inputs[3];
  bool has_tab_grouping_suggestions = inputs[4];

  // Create response.
  ModelProvider::Response response(kLabelInputSize, 0);
  response[0] = has_discounts;
  response[1] = has_price_insights;
  response[2] = can_track_price;
  response[3] = has_reader_mode;
  response[4] = has_tab_grouping_suggestions;
  // TODO(crbug.com/40249852): Set a classifier threshold.

  // TODO(shaktisahu): This class needs some rethinking to correctly associate
  // the labeled outputs to the flattened vector. Maybe have this method return
  // a map of labeled outputs which callls a superclass method internally to
  // flatten to the vector. Similar association is needed for the inputs as
  // well, but a different topic. Should have same kind of utility in python /
  // server side as well.

  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback), response));
}

}  // namespace segmentation_platform