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