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