Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ml/machine_learning_service_impl.h" |
| 6 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 7 | #include <memory> |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 10 | #include <base/bind.h> |
hscham | 4ce3c99 | 2021-02-19 16:37:23 +0900 | [diff] [blame] | 11 | #include <base/callback_helpers.h> |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 12 | #include <base/check.h> |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 13 | #include <base/files/file.h> |
| 14 | #include <base/files/file_util.h> |
Honglin Yu | 20391cd | 2020-10-27 15:58:49 +1100 | [diff] [blame] | 15 | #include <base/files/memory_mapped_file.h> |
Michael Martis | 8783c8e | 2019-06-26 17:30:54 +1000 | [diff] [blame] | 16 | #include <tensorflow/lite/model.h> |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 17 | #include <unicode/putil.h> |
| 18 | #include <unicode/udata.h> |
| 19 | #include <utils/memory/mmap.h> |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 20 | |
Jing Wang | 961b8af | 2020-10-26 12:40:35 +1100 | [diff] [blame] | 21 | #include "ml/grammar_checker_impl.h" |
| 22 | #include "ml/grammar_library.h" |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 23 | #include "ml/handwriting.h" |
| 24 | #include "ml/handwriting_recognizer_impl.h" |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 25 | #include "ml/model_impl.h" |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 26 | #include "ml/mojom/handwriting_recognizer.mojom.h" |
Hidehiko Abe | aa488c3 | 2018-08-31 23:49:41 +0900 | [diff] [blame] | 27 | #include "ml/mojom/model.mojom.h" |
Honglin Yu | d220427 | 2020-08-26 14:21:37 +1000 | [diff] [blame] | 28 | #include "ml/mojom/soda.mojom.h" |
Andrew Moylan | 44c352f | 2020-11-04 15:19:46 +1100 | [diff] [blame] | 29 | #include "ml/request_metrics.h" |
Honglin Yu | d220427 | 2020-08-26 14:21:37 +1000 | [diff] [blame] | 30 | #include "ml/soda_recognizer_impl.h" |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 31 | #include "ml/text_classifier_impl.h" |
Curtis McMullan | f11584f | 2021-01-13 12:21:36 +1100 | [diff] [blame] | 32 | #include "ml/text_suggester_impl.h" |
| 33 | #include "ml/text_suggestions.h" |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 34 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 35 | namespace ml { |
| 36 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 37 | namespace { |
| 38 | |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 39 | using ::chromeos::machine_learning::mojom::BuiltinModelId; |
| 40 | using ::chromeos::machine_learning::mojom::BuiltinModelSpecPtr; |
| 41 | using ::chromeos::machine_learning::mojom::FlatBufferModelSpecPtr; |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 42 | using ::chromeos::machine_learning::mojom::HandwritingRecognizer; |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 43 | using ::chromeos::machine_learning::mojom::HandwritingRecognizerSpec; |
| 44 | using ::chromeos::machine_learning::mojom::HandwritingRecognizerSpecPtr; |
Charles Zhao | 6d467e6 | 2020-08-31 10:02:03 +1000 | [diff] [blame] | 45 | using ::chromeos::machine_learning::mojom::LoadHandwritingModelResult; |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 46 | using ::chromeos::machine_learning::mojom::LoadModelResult; |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 47 | using ::chromeos::machine_learning::mojom::MachineLearningService; |
| 48 | using ::chromeos::machine_learning::mojom::Model; |
Honglin Yu | d220427 | 2020-08-26 14:21:37 +1000 | [diff] [blame] | 49 | using ::chromeos::machine_learning::mojom::SodaClient; |
| 50 | using ::chromeos::machine_learning::mojom::SodaConfigPtr; |
| 51 | using ::chromeos::machine_learning::mojom::SodaRecognizer; |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 52 | using ::chromeos::machine_learning::mojom::TextClassifier; |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 53 | |
| 54 | constexpr char kSystemModelDir[] = "/opt/google/chrome/ml_models/"; |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 55 | // Base name for UMA metrics related to model loading (`LoadBuiltinModel`, |
| 56 | // `LoadFlatBufferModel`, `LoadTextClassifier` or LoadHandwritingModel). |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame] | 57 | constexpr char kMetricsRequestName[] = "LoadModelResult"; |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 58 | |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 59 | constexpr char kIcuDataFilePath[] = "/opt/google/chrome/icudtl.dat"; |
| 60 | |
Honglin Yu | 20391cd | 2020-10-27 15:58:49 +1100 | [diff] [blame] | 61 | // Used to hold the mmap object of the icu data file. Each process should only |
| 62 | // have one instance of it. Intentionally never close it. |
| 63 | // We can not make it as a member of `MachineLearningServiceImpl` because it |
| 64 | // will crash the unit test (because in that case, when the |
| 65 | // `MachineLearningServiceImpl` object is destructed, the file will be |
| 66 | // unmapped but the icu data can not be reset in the testing process). |
| 67 | base::MemoryMappedFile* g_icu_data_mmap_file = nullptr; |
| 68 | |
| 69 | void InitIcuIfNeeded() { |
| 70 | if (!g_icu_data_mmap_file) { |
| 71 | g_icu_data_mmap_file = new base::MemoryMappedFile(); |
| 72 | CHECK(g_icu_data_mmap_file->Initialize( |
| 73 | base::FilePath(kIcuDataFilePath), |
| 74 | base::MemoryMappedFile::Access::READ_ONLY)); |
| 75 | // Init the Icu library. |
| 76 | UErrorCode err = U_ZERO_ERROR; |
| 77 | udata_setCommonData(const_cast<uint8_t*>(g_icu_data_mmap_file->data()), |
| 78 | &err); |
| 79 | DCHECK(err == U_ZERO_ERROR); |
| 80 | // Never try to load Icu data from files. |
| 81 | udata_setFileAccess(UDATA_ONLY_PACKAGES, &err); |
| 82 | DCHECK(err == U_ZERO_ERROR); |
| 83 | } |
| 84 | } |
| 85 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 86 | } // namespace |
| 87 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 88 | MachineLearningServiceImpl::MachineLearningServiceImpl( |
hscham | d13deea | 2021-03-08 09:46:15 +0900 | [diff] [blame] | 89 | mojo::PendingReceiver< |
| 90 | chromeos::machine_learning::mojom::MachineLearningService> receiver, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 91 | base::Closure disconnect_handler, |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 92 | const std::string& model_dir) |
Honglin Yu | 20391cd | 2020-10-27 15:58:49 +1100 | [diff] [blame] | 93 | : builtin_model_metadata_(GetBuiltinModelMetadata()), |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 94 | model_dir_(model_dir), |
hscham | d13deea | 2021-03-08 09:46:15 +0900 | [diff] [blame] | 95 | receiver_(this, std::move(receiver)) { |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 96 | receiver_.set_disconnect_handler(std::move(disconnect_handler)); |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 97 | } |
| 98 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 99 | MachineLearningServiceImpl::MachineLearningServiceImpl( |
hscham | d13deea | 2021-03-08 09:46:15 +0900 | [diff] [blame] | 100 | mojo::PendingReceiver< |
| 101 | chromeos::machine_learning::mojom::MachineLearningService> receiver, |
Charles Zhao | d4fb7b6 | 2020-08-25 17:21:58 +1000 | [diff] [blame] | 102 | base::Closure disconnect_handler, |
| 103 | dbus::Bus* bus) |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 104 | : MachineLearningServiceImpl( |
hscham | d13deea | 2021-03-08 09:46:15 +0900 | [diff] [blame] | 105 | std::move(receiver), std::move(disconnect_handler), kSystemModelDir) { |
Charles Zhao | d4fb7b6 | 2020-08-25 17:21:58 +1000 | [diff] [blame] | 106 | if (bus) { |
| 107 | dlcservice_client_ = std::make_unique<DlcserviceClient>(bus); |
| 108 | } |
| 109 | } |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 110 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 111 | void MachineLearningServiceImpl::Clone( |
| 112 | mojo::PendingReceiver<MachineLearningService> receiver) { |
| 113 | clone_receivers_.Add(this, std::move(receiver)); |
Andrew Moylan | 2fb80af | 2020-07-08 10:52:08 +1000 | [diff] [blame] | 114 | } |
| 115 | |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 116 | void MachineLearningServiceImpl::LoadBuiltinModel( |
| 117 | BuiltinModelSpecPtr spec, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 118 | mojo::PendingReceiver<Model> receiver, |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 119 | LoadBuiltinModelCallback callback) { |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 120 | // Unsupported models do not have metadata entries. |
| 121 | const auto metadata_lookup = builtin_model_metadata_.find(spec->id); |
| 122 | if (metadata_lookup == builtin_model_metadata_.end()) { |
Honglin Yu | a81145a | 2019-09-23 15:20:13 +1000 | [diff] [blame] | 123 | LOG(WARNING) << "LoadBuiltinModel requested for unsupported model ID " |
| 124 | << spec->id << "."; |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 125 | std::move(callback).Run(LoadModelResult::MODEL_SPEC_ERROR); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 126 | RecordModelSpecificationErrorEvent(); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | const BuiltinModelMetadata& metadata = metadata_lookup->second; |
| 131 | |
| 132 | DCHECK(!metadata.metrics_model_name.empty()); |
| 133 | |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 134 | RequestMetrics request_metrics(metadata.metrics_model_name, |
| 135 | kMetricsRequestName); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 136 | request_metrics.StartRecordingPerformanceMetrics(); |
| 137 | |
| 138 | // Attempt to load model. |
| 139 | const std::string model_path = model_dir_ + metadata.model_file; |
| 140 | std::unique_ptr<tflite::FlatBufferModel> model = |
| 141 | tflite::FlatBufferModel::BuildFromFile(model_path.c_str()); |
| 142 | if (model == nullptr) { |
| 143 | LOG(ERROR) << "Failed to load model file '" << model_path << "'."; |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 144 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 145 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 146 | return; |
| 147 | } |
| 148 | |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 149 | ModelImpl::Create(metadata.required_inputs, metadata.required_outputs, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 150 | std::move(model), std::move(receiver), |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 151 | metadata.metrics_model_name); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 152 | |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 153 | std::move(callback).Run(LoadModelResult::OK); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 154 | |
| 155 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 156 | request_metrics.RecordRequestEvent(LoadModelResult::OK); |
| 157 | } |
| 158 | |
| 159 | void MachineLearningServiceImpl::LoadFlatBufferModel( |
| 160 | FlatBufferModelSpecPtr spec, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 161 | mojo::PendingReceiver<Model> receiver, |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 162 | LoadFlatBufferModelCallback callback) { |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 163 | DCHECK(!spec->metrics_model_name.empty()); |
| 164 | |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 165 | RequestMetrics request_metrics(spec->metrics_model_name, kMetricsRequestName); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 166 | request_metrics.StartRecordingPerformanceMetrics(); |
| 167 | |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 168 | // Take the ownership of the content of `model_string` because `ModelImpl` has |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 169 | // to hold the memory. |
Andrew Moylan | 44c352f | 2020-11-04 15:19:46 +1100 | [diff] [blame] | 170 | auto model_data = |
| 171 | std::make_unique<AlignedModelData>(std::move(spec->model_string)); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 172 | |
| 173 | std::unique_ptr<tflite::FlatBufferModel> model = |
Andrew Moylan | 44c352f | 2020-11-04 15:19:46 +1100 | [diff] [blame] | 174 | tflite::FlatBufferModel::VerifyAndBuildFromBuffer(model_data->data(), |
| 175 | model_data->size()); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 176 | if (model == nullptr) { |
| 177 | LOG(ERROR) << "Failed to load model string of metric name: " |
| 178 | << spec->metrics_model_name << "'."; |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 179 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 180 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 181 | return; |
| 182 | } |
| 183 | |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 184 | ModelImpl::Create( |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 185 | std::map<std::string, int>(spec->inputs.begin(), spec->inputs.end()), |
| 186 | std::map<std::string, int>(spec->outputs.begin(), spec->outputs.end()), |
Andrew Moylan | 44c352f | 2020-11-04 15:19:46 +1100 | [diff] [blame] | 187 | std::move(model), std::move(model_data), std::move(receiver), |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 188 | spec->metrics_model_name); |
| 189 | |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 190 | std::move(callback).Run(LoadModelResult::OK); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 191 | |
| 192 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 193 | request_metrics.RecordRequestEvent(LoadModelResult::OK); |
| 194 | } |
| 195 | |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 196 | void MachineLearningServiceImpl::LoadTextClassifier( |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 197 | mojo::PendingReceiver<TextClassifier> receiver, |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 198 | LoadTextClassifierCallback callback) { |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 199 | RequestMetrics request_metrics("TextClassifier", kMetricsRequestName); |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 200 | request_metrics.StartRecordingPerformanceMetrics(); |
| 201 | |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 202 | // Create the TextClassifier. |
Honglin Yu | 3f99ff1 | 2020-10-15 00:40:11 +1100 | [diff] [blame] | 203 | if (!TextClassifierImpl::Create(std::move(receiver))) { |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 204 | LOG(ERROR) << "Failed to create TextClassifierImpl object."; |
| 205 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
| 206 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | // initialize the icu library. |
| 211 | InitIcuIfNeeded(); |
| 212 | |
| 213 | std::move(callback).Run(LoadModelResult::OK); |
| 214 | |
| 215 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 216 | request_metrics.RecordRequestEvent(LoadModelResult::OK); |
| 217 | } |
| 218 | |
Charles Zhao | 6d467e6 | 2020-08-31 10:02:03 +1000 | [diff] [blame] | 219 | void LoadHandwritingModelFromDir( |
| 220 | HandwritingRecognizerSpecPtr spec, |
| 221 | mojo::PendingReceiver<HandwritingRecognizer> receiver, |
| 222 | MachineLearningServiceImpl::LoadHandwritingModelCallback callback, |
| 223 | const std::string& root_path) { |
| 224 | RequestMetrics request_metrics("HandwritingModel", kMetricsRequestName); |
| 225 | request_metrics.StartRecordingPerformanceMetrics(); |
| 226 | |
| 227 | // Returns error if root_path is empty. |
| 228 | if (root_path.empty()) { |
| 229 | std::move(callback).Run(LoadHandwritingModelResult::DLC_GET_PATH_ERROR); |
| 230 | request_metrics.RecordRequestEvent( |
| 231 | LoadHandwritingModelResult::DLC_GET_PATH_ERROR); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | // Load HandwritingLibrary. |
| 236 | auto* const hwr_library = ml::HandwritingLibrary::GetInstance(root_path); |
| 237 | |
| 238 | if (hwr_library->GetStatus() != ml::HandwritingLibrary::Status::kOk) { |
| 239 | LOG(ERROR) << "Initialize ml::HandwritingLibrary with error " |
| 240 | << static_cast<int>(hwr_library->GetStatus()); |
| 241 | |
| 242 | switch (hwr_library->GetStatus()) { |
| 243 | case ml::HandwritingLibrary::Status::kLoadLibraryFailed: { |
| 244 | std::move(callback).Run( |
| 245 | LoadHandwritingModelResult::LOAD_NATIVE_LIB_ERROR); |
| 246 | request_metrics.RecordRequestEvent( |
| 247 | LoadHandwritingModelResult::LOAD_NATIVE_LIB_ERROR); |
| 248 | return; |
| 249 | } |
| 250 | case ml::HandwritingLibrary::Status::kFunctionLookupFailed: { |
| 251 | std::move(callback).Run( |
| 252 | LoadHandwritingModelResult::LOAD_FUNC_PTR_ERROR); |
| 253 | request_metrics.RecordRequestEvent( |
| 254 | LoadHandwritingModelResult::LOAD_FUNC_PTR_ERROR); |
| 255 | return; |
| 256 | } |
| 257 | default: { |
| 258 | std::move(callback).Run(LoadHandwritingModelResult::LOAD_MODEL_ERROR); |
| 259 | request_metrics.RecordRequestEvent( |
| 260 | LoadHandwritingModelResult::LOAD_MODEL_ERROR); |
| 261 | return; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // Create HandwritingRecognizer. |
| 267 | if (!HandwritingRecognizerImpl::Create(std::move(spec), |
| 268 | std::move(receiver))) { |
| 269 | LOG(ERROR) << "LoadHandwritingRecognizer returned false."; |
| 270 | std::move(callback).Run(LoadHandwritingModelResult::LOAD_MODEL_FILES_ERROR); |
| 271 | request_metrics.RecordRequestEvent( |
| 272 | LoadHandwritingModelResult::LOAD_MODEL_FILES_ERROR); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | std::move(callback).Run(LoadHandwritingModelResult::OK); |
| 277 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 278 | request_metrics.RecordRequestEvent(LoadHandwritingModelResult::OK); |
| 279 | } |
| 280 | |
| 281 | void MachineLearningServiceImpl::LoadHandwritingModel( |
| 282 | chromeos::machine_learning::mojom::HandwritingRecognizerSpecPtr spec, |
| 283 | mojo::PendingReceiver< |
| 284 | chromeos::machine_learning::mojom::HandwritingRecognizer> receiver, |
| 285 | LoadHandwritingModelCallback callback) { |
| 286 | // If handwriting is installed on rootfs, load it from there. |
| 287 | if (ml::HandwritingLibrary::IsUseLibHandwritingEnabled()) { |
| 288 | LoadHandwritingModelFromDir( |
| 289 | std::move(spec), std::move(receiver), std::move(callback), |
| 290 | ml::HandwritingLibrary::kHandwritingDefaultModelDir); |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | // If handwriting is installed as DLC, get the dir and subsequently load it |
| 295 | // from there. |
| 296 | if (ml::HandwritingLibrary::IsUseLibHandwritingDlcEnabled()) { |
| 297 | dlcservice_client_->GetDlcRootPath( |
| 298 | "libhandwriting", |
| 299 | base::BindOnce(&LoadHandwritingModelFromDir, std::move(spec), |
| 300 | std::move(receiver), std::move(callback))); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | // If handwriting is not on rootfs and not in DLC, this function should not |
| 305 | // be called. |
| 306 | LOG(ERROR) << "Calling LoadHandwritingModel without Handwriting enabled " |
| 307 | "should never happen."; |
| 308 | std::move(callback).Run(LoadHandwritingModelResult::LOAD_MODEL_ERROR); |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | void MachineLearningServiceImpl::LoadHandwritingModelWithSpec( |
| 312 | HandwritingRecognizerSpecPtr spec, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 313 | mojo::PendingReceiver<HandwritingRecognizer> receiver, |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 314 | LoadHandwritingModelWithSpecCallback callback) { |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 315 | RequestMetrics request_metrics("HandwritingModel", kMetricsRequestName); |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 316 | request_metrics.StartRecordingPerformanceMetrics(); |
| 317 | |
| 318 | // Load HandwritingLibrary. |
| 319 | auto* const hwr_library = ml::HandwritingLibrary::GetInstance(); |
| 320 | |
| 321 | if (hwr_library->GetStatus() == |
| 322 | ml::HandwritingLibrary::Status::kNotSupported) { |
| 323 | LOG(ERROR) << "Initialize ml::HandwritingLibrary with error " |
| 324 | << static_cast<int>(hwr_library->GetStatus()); |
| 325 | |
| 326 | std::move(callback).Run(LoadModelResult::FEATURE_NOT_SUPPORTED_ERROR); |
| 327 | request_metrics.RecordRequestEvent( |
| 328 | LoadModelResult::FEATURE_NOT_SUPPORTED_ERROR); |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | if (hwr_library->GetStatus() != ml::HandwritingLibrary::Status::kOk) { |
| 333 | LOG(ERROR) << "Initialize ml::HandwritingLibrary with error " |
| 334 | << static_cast<int>(hwr_library->GetStatus()); |
| 335 | |
| 336 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
| 337 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | // Create HandwritingRecognizer. |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 342 | if (!HandwritingRecognizerImpl::Create(std::move(spec), |
| 343 | std::move(receiver))) { |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 344 | LOG(ERROR) << "LoadHandwritingRecognizer returned false."; |
| 345 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
| 346 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | std::move(callback).Run(LoadModelResult::OK); |
| 351 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 352 | request_metrics.RecordRequestEvent(LoadModelResult::OK); |
| 353 | } |
| 354 | |
Honglin Yu | d220427 | 2020-08-26 14:21:37 +1000 | [diff] [blame] | 355 | void MachineLearningServiceImpl::LoadSpeechRecognizer( |
| 356 | SodaConfigPtr config, |
| 357 | mojo::PendingRemote<SodaClient> soda_client, |
| 358 | mojo::PendingReceiver<SodaRecognizer> soda_recognizer, |
| 359 | LoadSpeechRecognizerCallback callback) { |
| 360 | RequestMetrics request_metrics("Soda", kMetricsRequestName); |
| 361 | request_metrics.StartRecordingPerformanceMetrics(); |
| 362 | |
| 363 | // Create the SodaRecognizer. |
| 364 | if (!SodaRecognizerImpl::Create(std::move(config), std::move(soda_client), |
| 365 | std::move(soda_recognizer))) { |
| 366 | LOG(ERROR) << "Failed to create SodaRecognizerImpl object."; |
| 367 | // TODO(robsc): it may be better that SODA has its specific enum values to |
| 368 | // return, similar to handwriting. So before we finalize the impl of SODA |
| 369 | // Mojo API, we may revisit this return value. |
| 370 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
| 371 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | std::move(callback).Run(LoadModelResult::OK); |
| 376 | |
| 377 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 378 | request_metrics.RecordRequestEvent(LoadModelResult::OK); |
| 379 | } |
| 380 | |
Jing Wang | 961b8af | 2020-10-26 12:40:35 +1100 | [diff] [blame] | 381 | void MachineLearningServiceImpl::LoadGrammarChecker( |
| 382 | mojo::PendingReceiver<chromeos::machine_learning::mojom::GrammarChecker> |
| 383 | receiver, |
| 384 | LoadGrammarCheckerCallback callback) { |
| 385 | RequestMetrics request_metrics("GrammarChecker", kMetricsRequestName); |
| 386 | request_metrics.StartRecordingPerformanceMetrics(); |
| 387 | |
| 388 | // Load GrammarLibrary. |
| 389 | auto* const grammar_library = ml::GrammarLibrary::GetInstance(); |
| 390 | |
| 391 | if (grammar_library->GetStatus() == |
| 392 | ml::GrammarLibrary::Status::kNotSupported) { |
| 393 | LOG(ERROR) << "Initialize ml::GrammarLibrary with error " |
| 394 | << static_cast<int>(grammar_library->GetStatus()); |
| 395 | |
| 396 | std::move(callback).Run(LoadModelResult::FEATURE_NOT_SUPPORTED_ERROR); |
| 397 | request_metrics.RecordRequestEvent( |
| 398 | LoadModelResult::FEATURE_NOT_SUPPORTED_ERROR); |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | if (grammar_library->GetStatus() != ml::GrammarLibrary::Status::kOk) { |
| 403 | LOG(ERROR) << "Initialize ml::GrammarLibrary with error " |
| 404 | << static_cast<int>(grammar_library->GetStatus()); |
| 405 | |
| 406 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
| 407 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | // Create GrammarChecker. |
| 412 | if (!GrammarCheckerImpl::Create(std::move(receiver))) { |
| 413 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
| 414 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | std::move(callback).Run(LoadModelResult::OK); |
| 419 | |
| 420 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 421 | request_metrics.RecordRequestEvent(LoadModelResult::OK); |
| 422 | } |
| 423 | |
Curtis McMullan | f11584f | 2021-01-13 12:21:36 +1100 | [diff] [blame] | 424 | void MachineLearningServiceImpl::LoadTextSuggester( |
| 425 | mojo::PendingReceiver<chromeos::machine_learning::mojom::TextSuggester> |
| 426 | receiver, |
| 427 | LoadTextSuggesterCallback callback) { |
| 428 | RequestMetrics request_metrics("TextSuggester", kMetricsRequestName); |
| 429 | request_metrics.StartRecordingPerformanceMetrics(); |
| 430 | |
| 431 | // Load TextSuggestions library. |
| 432 | auto* const text_suggestions = ml::TextSuggestions::GetInstance(); |
| 433 | |
| 434 | if (text_suggestions->GetStatus() == |
| 435 | ml::TextSuggestions::Status::kNotSupported) { |
| 436 | LOG(ERROR) << "Initialize ml::TextSuggestions with error " |
| 437 | << static_cast<int>(text_suggestions->GetStatus()); |
| 438 | |
| 439 | std::move(callback).Run(LoadModelResult::FEATURE_NOT_SUPPORTED_ERROR); |
| 440 | request_metrics.RecordRequestEvent( |
| 441 | LoadModelResult::FEATURE_NOT_SUPPORTED_ERROR); |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | if (text_suggestions->GetStatus() != ml::TextSuggestions::Status::kOk) { |
| 446 | LOG(ERROR) << "Initialize ml::TextSuggestions with error " |
| 447 | << static_cast<int>(text_suggestions->GetStatus()); |
| 448 | |
| 449 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
| 450 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | // Create TextSuggester. |
| 455 | if (!TextSuggesterImpl::Create(std::move(receiver))) { |
| 456 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
| 457 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | std::move(callback).Run(LoadModelResult::OK); |
| 462 | |
| 463 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 464 | request_metrics.RecordRequestEvent(LoadModelResult::OK); |
| 465 | } |
| 466 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 467 | } // namespace ml |