blob: b006f2d2d1a0f5afe6d63daf364de159f479d4f5 [file] [log] [blame]
Andrew Moylanff6be512018-07-03 11:05:01 +10001// 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"
alanlxlcb1f8562018-11-01 15:16:11 +11006#include "ml/request_metrics.h"
Andrew Moylanff6be512018-07-03 11:05:01 +10007
Michael Martisa74af932018-08-13 16:52:36 +10008#include <memory>
Andrew Moylanff6be512018-07-03 11:05:01 +10009#include <utility>
10
Michael Martisa74af932018-08-13 16:52:36 +100011#include <base/bind.h>
12#include <base/bind_helpers.h>
Honglin Yuf33dce32019-12-05 15:10:39 +110013#include <base/files/file.h>
14#include <base/files/file_util.h>
Michael Martis8783c8e2019-06-26 17:30:54 +100015#include <tensorflow/lite/model.h>
Honglin Yuf33dce32019-12-05 15:10:39 +110016#include <unicode/putil.h>
17#include <unicode/udata.h>
18#include <utils/memory/mmap.h>
Michael Martisa74af932018-08-13 16:52:36 +100019
charleszhao17777f92020-04-23 12:53:11 +100020#include "ml/handwriting.h"
charleszhao05c5a4a2020-06-09 16:49:54 +100021#include "ml/handwriting_path.h"
charleszhao17777f92020-04-23 12:53:11 +100022#include "ml/handwriting_recognizer_impl.h"
Michael Martisa74af932018-08-13 16:52:36 +100023#include "ml/model_impl.h"
charleszhao17777f92020-04-23 12:53:11 +100024#include "ml/mojom/handwriting_recognizer.mojom.h"
Hidehiko Abeaa488c32018-08-31 23:49:41 +090025#include "ml/mojom/model.mojom.h"
Honglin Yuf33dce32019-12-05 15:10:39 +110026#include "ml/text_classifier_impl.h"
Michael Martisa74af932018-08-13 16:52:36 +100027
Andrew Moylanff6be512018-07-03 11:05:01 +100028namespace ml {
29
Michael Martisa74af932018-08-13 16:52:36 +100030namespace {
31
Honglin Yu0ed72352019-08-27 17:42:01 +100032using ::chromeos::machine_learning::mojom::BuiltinModelId;
33using ::chromeos::machine_learning::mojom::BuiltinModelSpecPtr;
34using ::chromeos::machine_learning::mojom::FlatBufferModelSpecPtr;
charleszhao17777f92020-04-23 12:53:11 +100035using ::chromeos::machine_learning::mojom::HandwritingRecognizerRequest;
charleszhao05c5a4a2020-06-09 16:49:54 +100036using ::chromeos::machine_learning::mojom::HandwritingRecognizerSpec;
37using ::chromeos::machine_learning::mojom::HandwritingRecognizerSpecPtr;
Michael Martisa74af932018-08-13 16:52:36 +100038using ::chromeos::machine_learning::mojom::LoadModelResult;
Andrew Moylan2fb80af2020-07-08 10:52:08 +100039using ::chromeos::machine_learning::mojom::MachineLearningServiceRequest;
Michael Martisa74af932018-08-13 16:52:36 +100040using ::chromeos::machine_learning::mojom::ModelRequest;
Michael Martisa74af932018-08-13 16:52:36 +100041
42constexpr char kSystemModelDir[] = "/opt/google/chrome/ml_models/";
Andrew Moylan79b34a42020-07-08 11:13:11 +100043// Base name for UMA metrics related to model loading (`LoadBuiltinModel`,
44// `LoadFlatBufferModel`, `LoadTextClassifier` or LoadHandwritingModel).
Honglin Yu6adafcd2019-07-22 13:48:11 +100045constexpr char kMetricsRequestName[] = "LoadModelResult";
Michael Martisa74af932018-08-13 16:52:36 +100046
Honglin Yuf33dce32019-12-05 15:10:39 +110047constexpr char kTextClassifierModelFile[] =
48 "mlservice-model-text_classifier_en-v706.fb";
49
Honglin Yuc5100022020-07-09 11:54:27 +100050constexpr char kLanguageIdentificationModelFile[] =
51 "mlservice-model-language_identification-20190924.smfb";
52
Honglin Yuf33dce32019-12-05 15:10:39 +110053constexpr char kIcuDataFilePath[] = "/opt/google/chrome/icudtl.dat";
54
Michael Martisa74af932018-08-13 16:52:36 +100055} // namespace
56
Andrew Moylanff6be512018-07-03 11:05:01 +100057MachineLearningServiceImpl::MachineLearningServiceImpl(
Michael Martisa74af932018-08-13 16:52:36 +100058 mojo::ScopedMessagePipeHandle pipe,
59 base::Closure connection_error_handler,
60 const std::string& model_dir)
Honglin Yuf33dce32019-12-05 15:10:39 +110061 : icu_data_(nullptr),
62 text_classifier_model_filename_(kTextClassifierModelFile),
63 builtin_model_metadata_(GetBuiltinModelMetadata()),
Michael Martisa74af932018-08-13 16:52:36 +100064 model_dir_(model_dir),
hscham68867652020-01-06 11:40:47 +090065 binding_(this,
66 mojo::InterfaceRequest<
67 chromeos::machine_learning::mojom::MachineLearningService>(
Honglin Yuf33dce32019-12-05 15:10:39 +110068 std::move(pipe))) {
Andrew Moylanff6be512018-07-03 11:05:01 +100069 binding_.set_connection_error_handler(std::move(connection_error_handler));
70}
71
Michael Martisa74af932018-08-13 16:52:36 +100072MachineLearningServiceImpl::MachineLearningServiceImpl(
73 mojo::ScopedMessagePipeHandle pipe, base::Closure connection_error_handler)
74 : MachineLearningServiceImpl(std::move(pipe),
75 std::move(connection_error_handler),
76 kSystemModelDir) {}
77
Honglin Yuf33dce32019-12-05 15:10:39 +110078void MachineLearningServiceImpl::SetTextClassifierModelFilenameForTesting(
79 const std::string& filename) {
80 text_classifier_model_filename_ = filename;
81}
82
Andrew Moylan2fb80af2020-07-08 10:52:08 +100083void MachineLearningServiceImpl::Clone(MachineLearningServiceRequest request) {
84 clone_bindings_.AddBinding(this, std::move(request));
85}
86
Honglin Yu0ed72352019-08-27 17:42:01 +100087void MachineLearningServiceImpl::LoadBuiltinModel(
88 BuiltinModelSpecPtr spec,
89 ModelRequest request,
Qijiang Fan5d381a02020-04-19 23:42:37 +090090 LoadBuiltinModelCallback callback) {
Honglin Yu0ed72352019-08-27 17:42:01 +100091 // 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 Yua81145a2019-09-23 15:20:13 +100094 LOG(WARNING) << "LoadBuiltinModel requested for unsupported model ID "
95 << spec->id << ".";
Qijiang Fan5d381a02020-04-19 23:42:37 +090096 std::move(callback).Run(LoadModelResult::MODEL_SPEC_ERROR);
Honglin Yu0ed72352019-08-27 17:42:01 +100097 RecordModelSpecificationErrorEvent();
98 return;
99 }
100
101 const BuiltinModelMetadata& metadata = metadata_lookup->second;
102
103 DCHECK(!metadata.metrics_model_name.empty());
104
105 RequestMetrics<LoadModelResult> request_metrics(metadata.metrics_model_name,
106 kMetricsRequestName);
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 Fan5d381a02020-04-19 23:42:37 +0900115 std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR);
Honglin Yu0ed72352019-08-27 17:42:01 +1000116 request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR);
117 return;
118 }
119
Honglin Yuc0cef102020-01-17 15:26:01 +1100120 ModelImpl::Create(metadata.required_inputs, metadata.required_outputs,
121 std::move(model), std::move(request),
122 metadata.metrics_model_name);
Honglin Yu0ed72352019-08-27 17:42:01 +1000123
Qijiang Fan5d381a02020-04-19 23:42:37 +0900124 std::move(callback).Run(LoadModelResult::OK);
Honglin Yu0ed72352019-08-27 17:42:01 +1000125
126 request_metrics.FinishRecordingPerformanceMetrics();
127 request_metrics.RecordRequestEvent(LoadModelResult::OK);
128}
129
130void MachineLearningServiceImpl::LoadFlatBufferModel(
131 FlatBufferModelSpecPtr spec,
132 ModelRequest request,
Qijiang Fan5d381a02020-04-19 23:42:37 +0900133 LoadFlatBufferModelCallback callback) {
Honglin Yu0ed72352019-08-27 17:42:01 +1000134 DCHECK(!spec->metrics_model_name.empty());
135
136 RequestMetrics<LoadModelResult> request_metrics(spec->metrics_model_name,
137 kMetricsRequestName);
138 request_metrics.StartRecordingPerformanceMetrics();
139
Andrew Moylan79b34a42020-07-08 11:13:11 +1000140 // Take the ownership of the content of `model_string` because `ModelImpl` has
Honglin Yu0ed72352019-08-27 17:42:01 +1000141 // to hold the memory.
142 auto model_string_impl =
143 std::make_unique<std::string>(std::move(spec->model_string));
144
145 std::unique_ptr<tflite::FlatBufferModel> model =
146 tflite::FlatBufferModel::BuildFromBuffer(model_string_impl->c_str(),
147 model_string_impl->length());
148 if (model == nullptr) {
149 LOG(ERROR) << "Failed to load model string of metric name: "
150 << spec->metrics_model_name << "'.";
Qijiang Fan5d381a02020-04-19 23:42:37 +0900151 std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR);
Honglin Yu0ed72352019-08-27 17:42:01 +1000152 request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR);
153 return;
154 }
155
Honglin Yuc0cef102020-01-17 15:26:01 +1100156 ModelImpl::Create(
Honglin Yu0ed72352019-08-27 17:42:01 +1000157 std::map<std::string, int>(spec->inputs.begin(), spec->inputs.end()),
158 std::map<std::string, int>(spec->outputs.begin(), spec->outputs.end()),
159 std::move(model), std::move(model_string_impl), std::move(request),
160 spec->metrics_model_name);
161
Qijiang Fan5d381a02020-04-19 23:42:37 +0900162 std::move(callback).Run(LoadModelResult::OK);
Honglin Yu0ed72352019-08-27 17:42:01 +1000163
164 request_metrics.FinishRecordingPerformanceMetrics();
165 request_metrics.RecordRequestEvent(LoadModelResult::OK);
166}
167
Honglin Yuf33dce32019-12-05 15:10:39 +1100168void MachineLearningServiceImpl::LoadTextClassifier(
169 chromeos::machine_learning::mojom::TextClassifierRequest request,
170 LoadTextClassifierCallback callback) {
171 RequestMetrics<LoadModelResult> request_metrics("TextClassifier",
172 kMetricsRequestName);
173 request_metrics.StartRecordingPerformanceMetrics();
174
175 // Attempt to load model.
176 std::string model_path = model_dir_ + text_classifier_model_filename_;
177 auto scoped_mmap =
178 std::make_unique<libtextclassifier3::ScopedMmap>(model_path);
179 if (!scoped_mmap->handle().ok()) {
180 LOG(ERROR) << "Failed to load the text classifier model file '"
181 << model_path << "'.";
182 std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR);
183 request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR);
184 return;
185 }
186
187 // Create the TextClassifier.
Honglin Yuc5100022020-07-09 11:54:27 +1000188 if (!TextClassifierImpl::Create(&scoped_mmap,
189 model_dir_ + kLanguageIdentificationModelFile,
190 std::move(request))) {
Honglin Yuf33dce32019-12-05 15:10:39 +1100191 LOG(ERROR) << "Failed to create TextClassifierImpl object.";
192 std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR);
193 request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR);
194 return;
195 }
196
197 // initialize the icu library.
198 InitIcuIfNeeded();
199
200 std::move(callback).Run(LoadModelResult::OK);
201
202 request_metrics.FinishRecordingPerformanceMetrics();
203 request_metrics.RecordRequestEvent(LoadModelResult::OK);
204}
205
charleszhao17777f92020-04-23 12:53:11 +1000206void MachineLearningServiceImpl::LoadHandwritingModel(
207 HandwritingRecognizerRequest request,
208 LoadHandwritingModelCallback callback) {
charleszhao05c5a4a2020-06-09 16:49:54 +1000209 // Use english as default language.
210 LoadHandwritingModelWithSpec(HandwritingRecognizerSpec::New("en"),
211 std::move(request), std::move(callback));
212}
213
214void MachineLearningServiceImpl::LoadHandwritingModelWithSpec(
215 HandwritingRecognizerSpecPtr spec,
216 HandwritingRecognizerRequest request,
217 LoadHandwritingModelCallback callback) {
charleszhao17777f92020-04-23 12:53:11 +1000218 RequestMetrics<LoadModelResult> request_metrics("HandwritingModel",
219 kMetricsRequestName);
220 request_metrics.StartRecordingPerformanceMetrics();
221
222 // Load HandwritingLibrary.
223 auto* const hwr_library = ml::HandwritingLibrary::GetInstance();
224
225 if (hwr_library->GetStatus() ==
226 ml::HandwritingLibrary::Status::kNotSupported) {
227 LOG(ERROR) << "Initialize ml::HandwritingLibrary with error "
228 << static_cast<int>(hwr_library->GetStatus());
229
230 std::move(callback).Run(LoadModelResult::FEATURE_NOT_SUPPORTED_ERROR);
231 request_metrics.RecordRequestEvent(
232 LoadModelResult::FEATURE_NOT_SUPPORTED_ERROR);
233 return;
234 }
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 std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR);
241 request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR);
242 return;
243 }
244
charleszhao05c5a4a2020-06-09 16:49:54 +1000245 if (!GetModelPaths(spec.Clone()).has_value()) {
246 LOG(ERROR) << "LoadHandwritingRecognizer is not called because language "
247 "code is not supported.";
248
249 std::move(callback).Run(LoadModelResult::LANGUAGE_NOT_SUPPORTED_ERROR);
250 request_metrics.RecordRequestEvent(
251 LoadModelResult::LANGUAGE_NOT_SUPPORTED_ERROR);
252 return;
253 }
254
charleszhao17777f92020-04-23 12:53:11 +1000255 // Create HandwritingRecognizer.
charleszhao05c5a4a2020-06-09 16:49:54 +1000256 if (!HandwritingRecognizerImpl::Create(std::move(spec), std::move(request))) {
charleszhao17777f92020-04-23 12:53:11 +1000257 LOG(ERROR) << "LoadHandwritingRecognizer returned false.";
258 std::move(callback).Run(LoadModelResult::LOAD_MODEL_ERROR);
259 request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR);
260 return;
261 }
262
263 std::move(callback).Run(LoadModelResult::OK);
264 request_metrics.FinishRecordingPerformanceMetrics();
265 request_metrics.RecordRequestEvent(LoadModelResult::OK);
266}
267
Honglin Yuf33dce32019-12-05 15:10:39 +1100268void MachineLearningServiceImpl::InitIcuIfNeeded() {
269 if (icu_data_ == nullptr) {
270 // Need to load the data file again.
271 int64_t file_size;
272 const base::FilePath icu_data_file_path(kIcuDataFilePath);
273 CHECK(base::GetFileSize(icu_data_file_path, &file_size));
274 icu_data_ = new char[file_size];
275 CHECK(base::ReadFile(icu_data_file_path, icu_data_,
276 static_cast<int>(file_size)) == file_size);
277 // Init the Icu library.
278 UErrorCode err = U_ZERO_ERROR;
279 udata_setCommonData(reinterpret_cast<void*>(icu_data_), &err);
280 DCHECK(err == U_ZERO_ERROR);
281 // Never try to load Icu data from files.
282 udata_setFileAccess(UDATA_ONLY_PACKAGES, &err);
283 }
284}
285
Andrew Moylanff6be512018-07-03 11:05:01 +1000286} // namespace ml