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