File: dependency_parser_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 (202 lines) | stat: -rw-r--r-- 7,014 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
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/renderer/accessibility/phrase_segmentation/dependency_parser_model.h"

#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/histogram_macros_local.h"
#include "base/timer/elapsed_timer.h"
#include "build/build_config.h"
#include "chrome/renderer/accessibility/phrase_segmentation/dependency_parser_op_resolver.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "third_party/tensorflow-text/src/tensorflow_text/core/kernels/mst_solver.h"
#include "third_party/tensorflow_models/src/research/seq_flow_lite/tflite_ops/quantization_util.h"
#include "third_party/tflite/src/tensorflow/lite/string_util.h"
#include "third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/tflite_engine.h"

namespace {

// Util class for recording the result of loading the dependency parser model.
// The result is recorded when it goes out of scope and its destructor is
// called.
class ScopedDependencyParserModelStateRecorder {
 public:
  explicit ScopedDependencyParserModelStateRecorder(
      DependencyParserModelState state)
      : state_(state) {}

  ScopedDependencyParserModelStateRecorder(
      const ScopedDependencyParserModelStateRecorder&) = delete;
  ScopedDependencyParserModelStateRecorder& operator=(
      const ScopedDependencyParserModelStateRecorder&) = delete;

  ~ScopedDependencyParserModelStateRecorder() {
    UMA_HISTOGRAM_ENUMERATION(
        "Accessibility.DependencyParserModel.DependencyParserModelState",
        state_);
  }

  void set_state(DependencyParserModelState state) { state_ = state; }

 private:
  DependencyParserModelState state_;
};

}  // namespace

DependencyParserModel::DependencyParserModel()
    : num_threads_(optimization_guide::features::OverrideNumThreadsForOptTarget(
                       optimization_guide::proto::
                           OPTIMIZATION_TARGET_PHRASE_SEGMENTATION)
                       .value_or(-1)) {}

DependencyParserModel::~DependencyParserModel() = default;

void DependencyParserModel::UpdateWithFile(base::File model_file) {
  ScopedDependencyParserModelStateRecorder recorder(
      DependencyParserModelState::kModelFileInvalid);

  model_file_ = std::move(model_file);
  if (!model_file_.IsValid()) {
    return;
  }

  base::ElapsedTimer timer;
  auto tflite_engine = std::make_unique<tflite::task::core::TfLiteEngine>(
      std::make_unique<DependencyParserOpResolver>());
#if BUILDFLAG(IS_WIN)
  absl::Status model_load_status =
      tflite_engine->BuildModelFromFileHandle(model_file_.GetPlatformFile());
#else
  absl::Status model_load_status = tflite_engine->BuildModelFromFileDescriptor(
      model_file_.GetPlatformFile());
#endif
  if (!model_load_status.ok()) {
    LOCAL_HISTOGRAM_BOOLEAN(
        "Accessibility.DependencyParserModel.InvalidModelFile", true);
    DLOG(ERROR) << "Failed to load model: " << model_load_status.ToString();
    return;
  }

  recorder.set_state(DependencyParserModelState::kModelFileValid);

  auto compute_settings = tflite::proto::ComputeSettings();
  compute_settings.mutable_tflite_settings()
      ->mutable_cpu_settings()
      ->set_num_threads(num_threads_);
  absl::Status interpreter_status =
      tflite_engine->InitInterpreter(compute_settings);
  if (!interpreter_status.ok()) {
    DLOG(ERROR) << "Failed to initialize model interpreter: "
                << interpreter_status.ToString();
    return;
  }
  base::UmaHistogramTimes("Accessibility.DependencyParserModel.Create.Duration",
                          timer.Elapsed());

  recorder.set_state(DependencyParserModelState::kModelAvailable);

  dependency_parser_model_ = std::move(tflite_engine);
}

bool DependencyParserModel::IsAvailable() const {
  return dependency_parser_model_ != nullptr;
}

int64_t DependencyParserModel::GetModelVersion() const {
  // TODO(b/339037155): Return the model version provided
  // by the model itself.
  return 1;
}

std::vector<size_t> DependencyParserModel::GetDependencyHeads(
    base::span<const std::string> input) {
  DCHECK(IsAvailable());
  base::ElapsedTimer timer;

  // Perform the following operations to identify the dependency heads for each
  // token:
  // 1. Processes the input (tokenized string, length N) using the TFLite
  // model to generate a dependency probability matrix (NxN).
  // 2. Utilizes a Minimum Spanning Tree (MST) algorithm to identify the
  // dependency head for each token.
  auto* interpreter = dependency_parser_model_->interpreter();
  interpreter->ResizeInputTensor(0, {1, static_cast<int>(input.size())});
  TfLiteTensor* input_tensor = interpreter->input_tensor(0);
  tflite::DynamicBuffer input_buffer;

  for (const auto& token : input) {
    tflite::StringRef string_ref;
    string_ref.str = token.data();
    string_ref.len = token.size();
    input_buffer.AddString(string_ref);
  }
  // Populate tensors.
  input_buffer.WriteToTensor(input_tensor, /*new_shape=*/nullptr);
  interpreter->AllocateTensors();

  interpreter->Invoke();
  base::UmaHistogramTimes(
      "Accessibility.DependencyParserModel.Inference.Duration",
      timer.Elapsed());
  base::UmaHistogramCounts1M(
      "Accessibility.DependencyParserModel.Inference.LengthInTokens",
      input.size());

  const TfLiteTensor* output_tensor = interpreter->output_tensor(0);
  if (output_tensor == nullptr) {
    DLOG(ERROR) << "Error: output tensor is null.";
    return {};
  }
  size_t size = output_tensor->dims->data[0];
  base::UmaHistogramBoolean(
      "Accessibility.DependencyParserModel.Inference.Succeed",
      size == input.size());
  if (size != input.size()) {
    DLOG(ERROR) << "Error: output tensor size does not match input size.";
    return {};
  }

  std::vector<std::vector<float>> dependency_graph;
  dependency_graph.reserve(size);
  for (size_t j = 0; j < size; j++) {
    std::vector<float> dependency_graph_inner;
    dependency_graph_inner.reserve(size);
    for (size_t i = 0; i < size; i++) {
      dependency_graph_inner.push_back(
          seq_flow_lite::PodDequantize<uint8_t>(*output_tensor, j * size + i));
    }
    dependency_graph.emplace_back(dependency_graph_inner);
  }

  return SolveDependencies(dependency_graph);
}

std::vector<size_t> DependencyParserModel::SolveDependencies(
    base::span<const std::vector<float>> input) {
  tensorflow::text::MstSolver<size_t, float> solver;
  size_t size = input.size();
  if (!solver.Init(/*forest=*/false, size).ok()) {
    return {};
  }

  for (size_t i = 0; i < size; i++) {
    for (size_t j = 0; j < size; j++) {
      if (i == j) {
        solver.AddRoot(i, input[i][j]);
      } else {
        solver.AddArc(j, i, input[i][j]);
      }
    }
  }

  std::vector<size_t> heads;
  heads.resize(size);
  if (!solver.Solve(&heads).ok()) {
    return {};
  }
  return heads;
}