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 | |
Andrew Moylan | 44c352f | 2020-11-04 15:19:46 +1100 | [diff] [blame] | 23 | // Holds 4-byte aligned char[] data suitable for a flatbuffer model. |
| 24 | class AlignedModelData { |
| 25 | public: |
| 26 | // Constructs from a std::string. If its .c_str() is not 4-byte aligned, an |
| 27 | // aligned copy is made. |
| 28 | explicit AlignedModelData(std::string model_str); |
| 29 | |
| 30 | ~AlignedModelData(); |
| 31 | |
| 32 | AlignedModelData(const AlignedModelData&) = delete; |
| 33 | AlignedModelData& operator=(const AlignedModelData&) = delete; |
| 34 | |
| 35 | // The start of the model data. The result will be 4-byte aligned. |
| 36 | const char* data() const; |
| 37 | // The length of the buffer starting at `data()`. |
| 38 | size_t size() const; |
| 39 | |
| 40 | private: |
| 41 | // Original std::string containing model data. May be empty. |
| 42 | std::unique_ptr<std::string> original_model_str_; |
| 43 | // Aligned copy of the original std::string. May be empty. |
| 44 | std::unique_ptr<char[]> aligned_copy_; |
| 45 | size_t aligned_copy_size_; |
| 46 | }; |
| 47 | |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 48 | // Holds a TensorFlow lite graph and produces GraphExecutors that may run the |
| 49 | // graph. |
| 50 | // |
| 51 | // All GraphExecutors created by a ModelImpl reference its model definition (and |
| 52 | // hence may not outlive the ModelImpl). Multiple such GraphExecutors may be |
| 53 | // used concurrently from different sequences. |
| 54 | class ModelImpl : public chromeos::machine_learning::mojom::Model { |
| 55 | public: |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 56 | // Creates an instance bound to `receiver`. |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 57 | // |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 58 | // The `required_inputs` and `required_outputs` arguments specify a mapping |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 59 | // from required input / output tensor names to their indices in the TF lite |
| 60 | // graph, and must outlive this object. |
Andrew Moylan | 44c352f | 2020-11-04 15:19:46 +1100 | [diff] [blame] | 61 | // `model_data` is backing data for `model` which this class will take |
| 62 | // ownership of. It will be destroyed *after* `model`. |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 63 | // |
| 64 | // 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] | 65 | // will be deleted when the corresponding mojo connection is closed. |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 66 | static ModelImpl* Create( |
| 67 | std::map<std::string, int> required_inputs, |
| 68 | std::map<std::string, int> required_outputs, |
| 69 | std::unique_ptr<tflite::FlatBufferModel> model, |
Andrew Moylan | 44c352f | 2020-11-04 15:19:46 +1100 | [diff] [blame] | 70 | std::unique_ptr<AlignedModelData> model_data, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 71 | mojo::PendingReceiver<chromeos::machine_learning::mojom::Model> receiver, |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 72 | const std::string& metrics_model_name); |
| 73 | |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 74 | // 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] | 75 | // 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] | 76 | // will be deleted when the corresponding mojo connection is closed. |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 77 | static ModelImpl* Create( |
| 78 | std::map<std::string, int> required_inputs, |
| 79 | std::map<std::string, int> required_outputs, |
| 80 | std::unique_ptr<tflite::FlatBufferModel> model, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 81 | mojo::PendingReceiver<chromeos::machine_learning::mojom::Model> receiver, |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 82 | const std::string& metrics_model_name); |
| 83 | |
| 84 | int num_graph_executors_for_testing() const; |
| 85 | |
| 86 | private: |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 87 | // Constructor is private, call `Create` to create objects. |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 88 | ModelImpl( |
| 89 | std::map<std::string, int> required_inputs, |
| 90 | std::map<std::string, int> required_outputs, |
| 91 | std::unique_ptr<tflite::FlatBufferModel> model, |
Andrew Moylan | 44c352f | 2020-11-04 15:19:46 +1100 | [diff] [blame] | 92 | std::unique_ptr<AlignedModelData> model_data, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 93 | mojo::PendingReceiver<chromeos::machine_learning::mojom::Model> receiver, |
| 94 | const std::string& metrics_model_name); |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 95 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 96 | void set_disconnect_handler(base::Closure disconnect_handler); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 97 | |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 98 | // chromeos::machine_learning::mojom::Model: |
| 99 | void CreateGraphExecutor( |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 100 | mojo::PendingReceiver<chromeos::machine_learning::mojom::GraphExecutor> |
| 101 | receiver, |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 102 | CreateGraphExecutorCallback callback) override; |
Alan Green | 55e1654 | 2020-05-11 14:06:46 +1000 | [diff] [blame] | 103 | void CreateGraphExecutorWithOptions( |
| 104 | chromeos::machine_learning::mojom::GraphExecutorOptionsPtr options, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 105 | mojo::PendingReceiver<chromeos::machine_learning::mojom::GraphExecutor> |
| 106 | receiver, |
Alan Green | 55e1654 | 2020-05-11 14:06:46 +1000 | [diff] [blame] | 107 | CreateGraphExecutorCallback callback) override; |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 108 | |
| 109 | // Remove a graph executor from our hosted set. |
| 110 | void EraseGraphExecutor(std::list<GraphExecutorImpl>::const_iterator it); |
| 111 | |
Honglin Yu | 0ed7235 | 2019-08-27 17:42:01 +1000 | [diff] [blame] | 112 | const std::map<std::string, int> required_inputs_; |
| 113 | const std::map<std::string, int> required_outputs_; |
| 114 | |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 115 | // Must be above `model_`. |
Andrew Moylan | 44c352f | 2020-11-04 15:19:46 +1100 | [diff] [blame] | 116 | const std::unique_ptr<AlignedModelData> model_data_; |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 117 | |
| 118 | const std::unique_ptr<tflite::FlatBufferModel> model_; |
| 119 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 120 | mojo::Receiver<chromeos::machine_learning::mojom::Model> receiver_; |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 121 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 122 | // Emulate a strongly bound receiver set: hold a set of GraphExecutors, |
| 123 | // specific elements of which are erased on connection closure. |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 124 | // |
| 125 | // 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] | 126 | // from this set (by its binding disconnection handler). Further, when a |
| 127 | // ModelImpl is destroyed, its entire collection of GraphExecutorImpls is also |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 128 | // destroyed. |
| 129 | std::list<GraphExecutorImpl> graph_executors_; |
| 130 | |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame] | 131 | // Model name as it should appear in UMA histogram names. |
| 132 | const std::string metrics_model_name_; |
| 133 | |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 134 | DISALLOW_COPY_AND_ASSIGN(ModelImpl); |
| 135 | }; |
| 136 | |
| 137 | } // namespace ml |
| 138 | |
| 139 | #endif // ML_MODEL_IMPL_H_ |