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> |
| 13 | #include <tensorflow/contrib/lite/model.h> |
| 14 | |
| 15 | #include "ml/model_impl.h" |
Hidehiko Abe | aa488c3 | 2018-08-31 23:49:41 +0900 | [diff] [blame] | 16 | #include "ml/mojom/model.mojom.h" |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 17 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 18 | namespace ml { |
| 19 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | using ::chromeos::machine_learning::mojom::LoadModelResult; |
| 23 | using ::chromeos::machine_learning::mojom::ModelId; |
| 24 | using ::chromeos::machine_learning::mojom::ModelRequest; |
| 25 | using ::chromeos::machine_learning::mojom::ModelSpecPtr; |
| 26 | |
| 27 | constexpr char kSystemModelDir[] = "/opt/google/chrome/ml_models/"; |
alanlxl | cb1f856 | 2018-11-01 15:16:11 +1100 | [diff] [blame] | 28 | // Base name for UMA metrics related to LoadModel requests |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame^] | 29 | constexpr char kMetricsRequestName[] = "LoadModelResult"; |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 30 | |
| 31 | // To avoid passing a lambda as a base::Closure. |
| 32 | void DeleteModelImpl(const ModelImpl* const model_impl) { |
| 33 | delete model_impl; |
| 34 | } |
| 35 | |
| 36 | } // namespace |
| 37 | |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 38 | MachineLearningServiceImpl::MachineLearningServiceImpl( |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 39 | mojo::ScopedMessagePipeHandle pipe, |
| 40 | base::Closure connection_error_handler, |
| 41 | const std::string& model_dir) |
| 42 | : model_metadata_(GetModelMetadata()), |
| 43 | model_dir_(model_dir), |
| 44 | binding_(this, std::move(pipe)) { |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 45 | binding_.set_connection_error_handler(std::move(connection_error_handler)); |
| 46 | } |
| 47 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 48 | MachineLearningServiceImpl::MachineLearningServiceImpl( |
| 49 | mojo::ScopedMessagePipeHandle pipe, base::Closure connection_error_handler) |
| 50 | : MachineLearningServiceImpl(std::move(pipe), |
| 51 | std::move(connection_error_handler), |
| 52 | kSystemModelDir) {} |
| 53 | |
| 54 | void MachineLearningServiceImpl::LoadModel(ModelSpecPtr spec, |
| 55 | ModelRequest request, |
| 56 | const LoadModelCallback& callback) { |
Andrew Moylan | 195a6f5 | 2019-05-16 20:57:32 +1000 | [diff] [blame] | 57 | // Unsupported models do not have metadata entries. |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 58 | const auto metadata_lookup = model_metadata_.find(spec->id); |
| 59 | if (metadata_lookup == model_metadata_.end()) { |
Andrew Moylan | 195a6f5 | 2019-05-16 20:57:32 +1000 | [diff] [blame] | 60 | LOG(WARNING) << "LoadModel requested for unsupported model ID " << spec->id |
| 61 | << "."; |
| 62 | callback.Run(LoadModelResult::MODEL_SPEC_ERROR); |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame^] | 63 | RecordModelSpecificationErrorEvent(); |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 64 | return; |
| 65 | } |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame^] | 66 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 67 | const ModelMetadata& metadata = metadata_lookup->second; |
| 68 | |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame^] | 69 | DCHECK(!metadata.metrics_model_name.empty()); |
| 70 | |
| 71 | RequestMetrics<LoadModelResult> request_metrics(metadata.metrics_model_name, |
| 72 | kMetricsRequestName); |
| 73 | request_metrics.StartRecordingPerformanceMetrics(); |
| 74 | |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 75 | // Attempt to load model. |
| 76 | const std::string model_path = model_dir_ + metadata.model_file; |
| 77 | std::unique_ptr<tflite::FlatBufferModel> model = |
| 78 | tflite::FlatBufferModel::BuildFromFile(model_path.c_str()); |
| 79 | if (model == nullptr) { |
| 80 | LOG(ERROR) << "Failed to load model file '" << model_path << "'."; |
| 81 | callback.Run(LoadModelResult::LOAD_MODEL_ERROR); |
alanlxl | cb1f856 | 2018-11-01 15:16:11 +1100 | [diff] [blame] | 82 | request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR); |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 83 | return; |
| 84 | } |
| 85 | |
| 86 | // Use a connection error handler to strongly bind |model_impl| to |request|. |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame^] | 87 | ModelImpl* const model_impl = new ModelImpl( |
| 88 | metadata.required_inputs, metadata.required_outputs, std::move(model), |
| 89 | std::move(request), metadata.metrics_model_name); |
Michael Martis | a74af93 | 2018-08-13 16:52:36 +1000 | [diff] [blame] | 90 | model_impl->set_connection_error_handler( |
| 91 | base::Bind(&DeleteModelImpl, base::Unretained(model_impl))); |
| 92 | callback.Run(LoadModelResult::OK); |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame^] | 93 | |
alanlxl | cb1f856 | 2018-11-01 15:16:11 +1100 | [diff] [blame] | 94 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 95 | request_metrics.RecordRequestEvent(LoadModelResult::OK); |
Andrew Moylan | ff6be51 | 2018-07-03 11:05:01 +1000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | } // namespace ml |