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; |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 37 | using ::chromeos::machine_learning::mojom::LoadModelResult; |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 38 | using ::chromeos::machine_learning::mojom::MachineLearningService; |
| 39 | using ::chromeos::machine_learning::mojom::Model; |
| 40 | using ::chromeos::machine_learning::mojom::TextClassifier; |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 41 | |
| 42 | constexpr char kSystemModelDir[] = "/opt/google/chrome/ml_models/"; |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 43 | // Base name for UMA metrics related to model loading (`LoadBuiltinModel`, |
| 44 | // `LoadFlatBufferModel`, `LoadTextClassifier` or LoadHandwritingModel). |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame] | 45 | constexpr char kMetricsRequestName[] = "LoadModelResult"; |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 46 | |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 47 | constexpr char kTextClassifierModelFile[] = |
| 48 | "mlservice-model-text_classifier_en-v706.fb"; |
| 49 | |
Honglin Yu | c510002 | 2020-07-09 11:54:27 +1000 | [diff] [blame] | 50 | constexpr char kLanguageIdentificationModelFile[] = |
| 51 | "mlservice-model-language_identification-20190924.smfb"; |
| 52 | |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 53 | constexpr char kIcuDataFilePath[] = "/opt/google/chrome/icudtl.dat"; |
| 54 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 55 | } // namespace |
| 56 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 57 | MachineLearningServiceImpl::MachineLearningServiceImpl( |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 58 | mojo::ScopedMessagePipeHandle pipe, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 59 | base::Closure disconnect_handler, |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 60 | const std::string& model_dir) |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 61 | : icu_data_(nullptr), |
| 62 | text_classifier_model_filename_(kTextClassifierModelFile), |
| 63 | builtin_model_metadata_(GetBuiltinModelMetadata()), |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 64 | model_dir_(model_dir), |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 65 | receiver_(this, |
| 66 | mojo::InterfaceRequest< |
| 67 | chromeos::machine_learning::mojom::MachineLearningService>( |
| 68 | std::move(pipe))) { |
| 69 | receiver_.set_disconnect_handler(std::move(disconnect_handler)); |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 70 | } |
| 71 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 72 | MachineLearningServiceImpl::MachineLearningServiceImpl( |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 73 | mojo::ScopedMessagePipeHandle pipe, base::Closure disconnect_handler) |
| 74 | : MachineLearningServiceImpl( |
| 75 | std::move(pipe), std::move(disconnect_handler), kSystemModelDir) {} |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 76 | |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 77 | void MachineLearningServiceImpl::SetTextClassifierModelFilenameForTesting( |
| 78 | const std::string& filename) { |
| 79 | text_classifier_model_filename_ = filename; |
| 80 | } |
| 81 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 82 | void MachineLearningServiceImpl::Clone( |
| 83 | mojo::PendingReceiver<MachineLearningService> receiver) { |
| 84 | clone_receivers_.Add(this, std::move(receiver)); |
Andrew Moylan | 2fb80af | 2020-07-08 10:52:08 +1000 | [diff] [blame] | 85 | } |
| 86 | |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 87 | void MachineLearningServiceImpl::LoadBuiltinModel( |
| 88 | BuiltinModelSpecPtr spec, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 89 | mojo::PendingReceiver<Model> receiver, |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 90 | LoadBuiltinModelCallback callback) { |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 91 | // Unsupported models do not have metadata entries. |
| 92 | const auto metadata_lookup = builtin_model_metadata_.find(spec->id); |
| 93 | if (metadata_lookup == builtin_model_metadata_.end()) { |
Honglin Yu | a81145a | 2019-09-23 15:20:13 +1000 | [diff] [blame] | 94 | LOG(WARNING) << "LoadBuiltinModel requested for unsupported model ID " |
| 95 | << spec->id << "."; |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 96 | std::move(callback).Run(LoadModelResult::MODEL_SPEC_ERROR); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 97 | RecordModelSpecificationErrorEvent(); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | const BuiltinModelMetadata& metadata = metadata_lookup->second; |
| 102 | |
| 103 | DCHECK(!metadata.metrics_model_name.empty()); |
| 104 | |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 105 | RequestMetrics request_metrics(metadata.metrics_model_name, |
| 106 | kMetricsRequestName); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 107 | request_metrics.StartRecordingPerformanceMetrics(); |
| 108 | |
| 109 | // Attempt to load model. |
| 110 | const std::string model_path = model_dir_ + metadata.model_file; |
| 111 | std::unique_ptr<tflite::FlatBufferModel> model = |
| 112 | tflite::FlatBufferModel::BuildFromFile(model_path.c_str()); |
| 113 | if (model == nullptr) { |
| 114 | LOG(ERROR) << "Failed to load model file '" << model_path << "'."; |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 115 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 116 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 117 | return; |
| 118 | } |
| 119 | |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 120 | ModelImpl::Create(metadata.required_inputs, metadata.required_outputs, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 121 | std::move(model), std::move(receiver), |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 122 | metadata.metrics_model_name); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 123 | |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 124 | std::move(callback).Run(LoadModelResult::OK); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 125 | |
| 126 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 127 | request_metrics.RecordRequestEvent(LoadModelResult::OK); |
| 128 | } |
| 129 | |
| 130 | void MachineLearningServiceImpl::LoadFlatBufferModel( |
| 131 | FlatBufferModelSpecPtr spec, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 132 | mojo::PendingReceiver<Model> receiver, |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 133 | LoadFlatBufferModelCallback callback) { |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 134 | DCHECK(!spec->metrics_model_name.empty()); |
| 135 | |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 136 | RequestMetrics request_metrics(spec->metrics_model_name, kMetricsRequestName); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 137 | request_metrics.StartRecordingPerformanceMetrics(); |
| 138 | |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 139 | // Take the ownership of the content of `model_string` because `ModelImpl` has |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 140 | // to hold the memory. |
| 141 | auto model_string_impl = |
| 142 | std::make_unique<std::string>(std::move(spec->model_string)); |
| 143 | |
| 144 | std::unique_ptr<tflite::FlatBufferModel> model = |
| 145 | tflite::FlatBufferModel::BuildFromBuffer(model_string_impl->c_str(), |
| 146 | model_string_impl->length()); |
| 147 | if (model == nullptr) { |
| 148 | LOG(ERROR) << "Failed to load model string of metric name: " |
| 149 | << spec->metrics_model_name << "'."; |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 150 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 151 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 152 | return; |
| 153 | } |
| 154 | |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 155 | ModelImpl::Create( |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 156 | std::map<std::string, int>(spec->inputs.begin(), spec->inputs.end()), |
| 157 | std::map<std::string, int>(spec->outputs.begin(), spec->outputs.end()), |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 158 | std::move(model), std::move(model_string_impl), std::move(receiver), |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 159 | spec->metrics_model_name); |
| 160 | |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 161 | std::move(callback).Run(LoadModelResult::OK); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 162 | |
| 163 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 164 | request_metrics.RecordRequestEvent(LoadModelResult::OK); |
| 165 | } |
| 166 | |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 167 | void MachineLearningServiceImpl::LoadTextClassifier( |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 168 | mojo::PendingReceiver<TextClassifier> receiver, |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 169 | LoadTextClassifierCallback callback) { |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 170 | RequestMetrics request_metrics("TextClassifier", kMetricsRequestName); |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 171 | request_metrics.StartRecordingPerformanceMetrics(); |
| 172 | |
| 173 | // Attempt to load model. |
| 174 | std::string model_path = model_dir_ + text_classifier_model_filename_; |
| 175 | auto scoped_mmap = |
| 176 | std::make_unique<libtextclassifier3::ScopedMmap>(model_path); |
| 177 | if (!scoped_mmap->handle().ok()) { |
| 178 | LOG(ERROR) << "Failed to load the text classifier model file '" |
| 179 | << model_path << "'."; |
| 180 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
| 181 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | // Create the TextClassifier. |
Honglin Yu | c510002 | 2020-07-09 11:54:27 +1000 | [diff] [blame] | 186 | if (!TextClassifierImpl::Create(&scoped_mmap, |
| 187 | model_dir_ + kLanguageIdentificationModelFile, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 188 | std::move(receiver))) { |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 189 | LOG(ERROR) << "Failed to create TextClassifierImpl object."; |
| 190 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
| 191 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | // initialize the icu library. |
| 196 | InitIcuIfNeeded(); |
| 197 | |
| 198 | std::move(callback).Run(LoadModelResult::OK); |
| 199 | |
| 200 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 201 | request_metrics.RecordRequestEvent(LoadModelResult::OK); |
| 202 | } |
| 203 | |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame^] | 204 | void MachineLearningServiceImpl::RemovedFunction_3() { |
| 205 | NOTREACHED() << "RemovedFunction_3 should not be called"; |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void MachineLearningServiceImpl::LoadHandwritingModelWithSpec( |
| 209 | HandwritingRecognizerSpecPtr spec, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 210 | mojo::PendingReceiver<HandwritingRecognizer> receiver, |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame^] | 211 | LoadHandwritingModelWithSpecCallback callback) { |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 212 | RequestMetrics request_metrics("HandwritingModel", kMetricsRequestName); |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 213 | request_metrics.StartRecordingPerformanceMetrics(); |
| 214 | |
| 215 | // Load HandwritingLibrary. |
| 216 | auto* const hwr_library = ml::HandwritingLibrary::GetInstance(); |
| 217 | |
| 218 | if (hwr_library->GetStatus() == |
| 219 | ml::HandwritingLibrary::Status::kNotSupported) { |
| 220 | LOG(ERROR) << "Initialize ml::HandwritingLibrary with error " |
| 221 | << static_cast<int>(hwr_library->GetStatus()); |
| 222 | |
| 223 | std::move(callback).Run(LoadModelResult::FEATURE_NOT_SUPPORTED_ERROR); |
| 224 | request_metrics.RecordRequestEvent( |
| 225 | LoadModelResult::FEATURE_NOT_SUPPORTED_ERROR); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | if (hwr_library->GetStatus() != ml::HandwritingLibrary::Status::kOk) { |
| 230 | LOG(ERROR) << "Initialize ml::HandwritingLibrary with error " |
| 231 | << static_cast<int>(hwr_library->GetStatus()); |
| 232 | |
| 233 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
| 234 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | // Create HandwritingRecognizer. |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 239 | if (!HandwritingRecognizerImpl::Create(std::move(spec), |
| 240 | std::move(receiver))) { |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 241 | LOG(ERROR) << "LoadHandwritingRecognizer returned false."; |
| 242 | std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR); |
| 243 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | std::move(callback).Run(LoadModelResult::OK); |
| 248 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 249 | request_metrics.RecordRequestEvent(LoadModelResult::OK); |
| 250 | } |
| 251 | |
Honglin Yu | f33dce3 | 2019-12-05 15:10:39 +1100 | [diff] [blame] | 252 | void MachineLearningServiceImpl::InitIcuIfNeeded() { |
| 253 | if (icu_data_ == nullptr) { |
| 254 | // Need to load the data file again. |
| 255 | int64_t file_size; |
| 256 | const base::FilePath icu_data_file_path(kIcuDataFilePath); |
| 257 | CHECK(base::GetFileSize(icu_data_file_path, &file_size)); |
| 258 | icu_data_ = new char[file_size]; |
| 259 | CHECK(base::ReadFile(icu_data_file_path, icu_data_, |
| 260 | static_cast<int>(file_size)) == file_size); |
| 261 | // Init the Icu library. |
| 262 | UErrorCode err = U_ZERO_ERROR; |
| 263 | udata_setCommonData(reinterpret_cast<void*>(icu_data_), &err); |
| 264 | DCHECK(err == U_ZERO_ERROR); |
| 265 | // Never try to load Icu data from files. |
| 266 | udata_setFileAccess(UDATA_ONLY_PACKAGES, &err); |
| 267 | } |
| 268 | } |
| 269 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 270 | } // namespace ml |