Michael Martis | a967f63 | 2018-08-10 10:39:00 +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 | #ifndef ML_MODEL_IMPL_H_ |
| 6 | #define ML_MODEL_IMPL_H_ |
| 7 | |
| 8 | #include <list> |
| 9 | #include <map> |
| 10 | #include <memory> |
| 11 | #include <string> |
| 12 | |
| 13 | #include <base/macros.h> |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 14 | #include <mojo/public/cpp/bindings/pending_receiver.h> |
| 15 | #include <mojo/public/cpp/bindings/receiver.h> |
Michael Martis | 8783c8e | 2019-06-26 17:30:54 +1000 | [diff] [blame] | 16 | #include <tensorflow/lite/model.h> |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 17 | |
| 18 | #include "ml/graph_executor_impl.h" |
Hidehiko Abe | aa488c3 | 2018-08-31 23:49:41 +0900 | [diff] [blame] | 19 | #include "ml/mojom/model.mojom.h" |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 20 | |
| 21 | namespace ml { |
| 22 | |
| 23 | // Holds a TensorFlow lite graph and produces GraphExecutors that may run the |
| 24 | // graph. |
| 25 | // |
| 26 | // All GraphExecutors created by a ModelImpl reference its model definition (and |
| 27 | // hence may not outlive the ModelImpl). Multiple such GraphExecutors may be |
| 28 | // used concurrently from different sequences. |
| 29 | class ModelImpl : public chromeos::machine_learning::mojom::Model { |
| 30 | public: |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 31 | // Creates an instance bound to `receiver`. |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 32 | // |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 33 | // The `required_inputs` and `required_outputs` arguments specify a mapping |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 34 | // from required input / output tensor names to their indices in the TF lite |
| 35 | // graph, and must outlive this object. |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 36 | // `model_string` is optional string data that this class can take ownership |
| 37 | // of (presumably the backing data for `model`) and that is guaranteed to be |
| 38 | // destroyed *after* `model`. This is required by function |
| 39 | // `tflite::FlatBufferModel::BuildFromBuffer`. |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 40 | // |
| 41 | // The RAM of the returned model is not owned by the caller. The model object |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 42 | // will be deleted when the corresponding mojo connection is closed. |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 43 | static ModelImpl* Create( |
| 44 | std::map<std::string, int> required_inputs, |
| 45 | std::map<std::string, int> required_outputs, |
| 46 | std::unique_ptr<tflite::FlatBufferModel> model, |
| 47 | std::unique_ptr<std::string> model_string, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 48 | mojo::PendingReceiver<chromeos::machine_learning::mojom::Model> receiver, |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 49 | const std::string& metrics_model_name); |
| 50 | |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 51 | // Use when constructed from file where no need to pass the `model_string`. |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 52 | // The RAM of the returned model is not owned by the caller. The model object |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 53 | // will be deleted when the corresponding mojo connection is closed. |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 54 | static ModelImpl* Create( |
| 55 | std::map<std::string, int> required_inputs, |
| 56 | std::map<std::string, int> required_outputs, |
| 57 | std::unique_ptr<tflite::FlatBufferModel> model, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 58 | mojo::PendingReceiver<chromeos::machine_learning::mojom::Model> receiver, |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 59 | const std::string& metrics_model_name); |
| 60 | |
| 61 | int num_graph_executors_for_testing() const; |
| 62 | |
| 63 | private: |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 64 | // Constructor is private, call `Create` to create objects. |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 65 | ModelImpl( |
| 66 | std::map<std::string, int> required_inputs, |
| 67 | std::map<std::string, int> required_outputs, |
| 68 | std::unique_ptr<tflite::FlatBufferModel> model, |
| 69 | std::unique_ptr<std::string> model_string, |
| 70 | mojo::PendingReceiver<chromeos::machine_learning::mojom::Model> receiver, |
| 71 | const std::string& metrics_model_name); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 72 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 73 | void set_disconnect_handler(base::Closure disconnect_handler); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 74 | |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 75 | // chromeos::machine_learning::mojom::Model: |
| 76 | void CreateGraphExecutor( |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 77 | mojo::PendingReceiver<chromeos::machine_learning::mojom::GraphExecutor> |
| 78 | receiver, |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 79 | CreateGraphExecutorCallback callback) override; |
Alan Green | 55e1654 | 2020-05-11 14:06:46 +1000 | [diff] [blame] | 80 | void CreateGraphExecutorWithOptions( |
| 81 | chromeos::machine_learning::mojom::GraphExecutorOptionsPtr options, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 82 | mojo::PendingReceiver<chromeos::machine_learning::mojom::GraphExecutor> |
| 83 | receiver, |
Alan Green | 55e1654 | 2020-05-11 14:06:46 +1000 | [diff] [blame] | 84 | CreateGraphExecutorCallback callback) override; |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 85 | |
| 86 | // Remove a graph executor from our hosted set. |
| 87 | void EraseGraphExecutor(std::list<GraphExecutorImpl>::const_iterator it); |
| 88 | |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 89 | const std::map<std::string, int> required_inputs_; |
| 90 | const std::map<std::string, int> required_outputs_; |
| 91 | |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 92 | // Must be above `model_`. |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 93 | const std::unique_ptr<std::string> model_string_; |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 94 | |
| 95 | const std::unique_ptr<tflite::FlatBufferModel> model_; |
| 96 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 97 | mojo::Receiver<chromeos::machine_learning::mojom::Model> receiver_; |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 98 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 99 | // Emulate a strongly bound receiver set: hold a set of GraphExecutors, |
| 100 | // specific elements of which are erased on connection closure. |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 101 | // |
| 102 | // That is, when a pipe to a GraphExecutorImpl closes, that object is removed |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 103 | // from this set (by its binding disconnection handler). Further, when a |
| 104 | // ModelImpl is destroyed, its entire collection of GraphExecutorImpls is also |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 105 | // destroyed. |
| 106 | std::list<GraphExecutorImpl> graph_executors_; |
| 107 | |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame] | 108 | // Model name as it should appear in UMA histogram names. |
| 109 | const std::string metrics_model_name_; |
| 110 | |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 111 | DISALLOW_COPY_AND_ASSIGN(ModelImpl); |
| 112 | }; |
| 113 | |
| 114 | } // namespace ml |
| 115 | |
| 116 | #endif // ML_MODEL_IMPL_H_ |