File: builtin_worker.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 (104 lines) | stat: -rw-r--r-- 3,399 bytes parent folder | download | duplicates (7)
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
// Copyright 2020 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/browser/ash/power/ml/smart_dim/builtin_worker.h"

#include "ash/constants/ash_features.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ref_counted_memory.h"
#include "chrome/browser/ash/power/ml/smart_dim/ml_agent_util.h"
#include "chrome/grit/browser_resources.h"
#include "chromeos/services/machine_learning/public/cpp/service_connection.h"
#include "components/assist_ranker/proto/example_preprocessor.pb.h"
#include "ui/base/resource/resource_bundle.h"

namespace ash {
namespace power {
namespace ml {

namespace {

using chromeos::machine_learning::mojom::BuiltinModelId;
using chromeos::machine_learning::mojom::BuiltinModelSpec;
using chromeos::machine_learning::mojom::BuiltinModelSpecPtr;

constexpr size_t k20190521ModelInputVectorSize = 592;

constexpr double k20190521ModelDefaultDimThreshold = -0.5;

}  // namespace

BuiltinWorker::BuiltinWorker() : SmartDimWorker() {}

BuiltinWorker::~BuiltinWorker() = default;

const assist_ranker::ExamplePreprocessorConfig*
BuiltinWorker::GetPreprocessorConfig() {
  LazyInitialize();
  return preprocessor_config_.get();
}

const mojo::Remote<chromeos::machine_learning::mojom::GraphExecutor>&
BuiltinWorker::GetExecutor() {
  LazyInitialize();
  return executor_;
}

void BuiltinWorker::LazyInitialize() {
  // Initialize builtin meta info.
  dim_threshold_ = k20190521ModelDefaultDimThreshold;
  expected_feature_size_ = k20190521ModelInputVectorSize;

  if (!preprocessor_config_) {
    preprocessor_config_ =
        std::make_unique<assist_ranker::ExamplePreprocessorConfig>();

    const int resource_id =
        IDR_SMART_DIM_20190521_EXAMPLE_PREPROCESSOR_CONFIG_PB;

    const scoped_refptr<base::RefCountedMemory> raw_config =
        ui::ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
            resource_id);
    if (!raw_config || !raw_config->front()) {
      DLOG(FATAL)
          << "Failed to load Builtin SmartDimModel example preprocessor "
             "config.";
      return;
    }

    if (!preprocessor_config_->ParseFromArray(raw_config->front(),
                                              raw_config->size())) {
      DLOG(FATAL) << "Failed to parse Builtin SmartDimModel example "
                     "preprocessor config.";
      preprocessor_config_.reset();
      return;
    }
  }

  if (!model_) {
    // Load the model.
    BuiltinModelSpecPtr spec =
        BuiltinModelSpec::New(BuiltinModelId::SMART_DIM_20190521);
    // Builtin model is supposed to be always available and valid, using
    // base::DoNothing as callbacks.
    chromeos::machine_learning::ServiceConnection::GetInstance()
        ->GetMachineLearningService()
        .LoadBuiltinModel(std::move(spec), model_.BindNewPipeAndPassReceiver(),
                          base::DoNothing());
  }

  if (!executor_) {
    // Get the graph executor.
    model_->CreateGraphExecutor(
        chromeos::machine_learning::mojom::GraphExecutorOptions::New(),
        executor_.BindNewPipeAndPassReceiver(), base::DoNothing());
    executor_.set_disconnect_handler(base::BindOnce(
        &BuiltinWorker::OnConnectionError, base::Unretained(this)));
  }
}

}  // namespace ml
}  // namespace power
}  // namespace ash