Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +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_GRAPH_EXECUTOR_IMPL_H_ |
| 6 | #define ML_GRAPH_EXECUTOR_IMPL_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <memory> |
| 10 | #include <string> |
Hidehiko Abe | 31bb963 | 2018-11-23 02:49:56 +0900 | [diff] [blame] | 11 | #include <vector> |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 12 | |
| 13 | #include <base/callback_forward.h> |
hscham | 3d0632f | 2019-12-11 15:58:57 +0900 | [diff] [blame] | 14 | #include <base/containers/flat_map.h> |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 15 | #include <base/macros.h> |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 16 | #include <mojo/public/cpp/bindings/pending_receiver.h> |
| 17 | #include <mojo/public/cpp/bindings/receiver.h> |
Michael Martis | 8783c8e | 2019-06-26 17:30:54 +1000 | [diff] [blame] | 18 | #include <tensorflow/lite/model.h> |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 19 | |
Hidehiko Abe | aa488c3 | 2018-08-31 23:49:41 +0900 | [diff] [blame] | 20 | #include "ml/mojom/graph_executor.mojom.h" |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 21 | |
| 22 | namespace ml { |
| 23 | |
| 24 | // Allows execution of TensorFlow lite graphs using input / output specified |
| 25 | // with Mojo types. |
| 26 | // |
| 27 | // Holds as little state as possible (with the remainder living in the parent |
| 28 | // Model object and shared between all sibling GraphExecutors). Hence, a |
| 29 | // GraphExecutor becomes invalid when its parent Model object is destroyed. |
| 30 | // |
| 31 | // A given GraphExecutorImpl may not be used concurrently from different |
| 32 | // sequences. |
| 33 | class GraphExecutorImpl |
| 34 | : public chromeos::machine_learning::mojom::GraphExecutor { |
| 35 | public: |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 36 | // Creates an instance bound to `receiver`. |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 37 | // |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 38 | // The `required_inputs` and `required_outputs` arguments specify a mapping |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 39 | // from required input / output tensor names to their indices in the TF lite |
| 40 | // graph, and must outlive this object. |
| 41 | // |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 42 | // UMA metrics will be logged with the specified `metrics_model_name`. |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame] | 43 | // |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 44 | // As is standard, `interpreter` must outlive the model with which it was |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 45 | // constructed. |
| 46 | GraphExecutorImpl( |
| 47 | const std::map<std::string, int>& required_inputs, |
| 48 | const std::map<std::string, int>& required_outputs, |
| 49 | std::unique_ptr<tflite::Interpreter> interpreter, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 50 | mojo::PendingReceiver<chromeos::machine_learning::mojom::GraphExecutor> |
| 51 | receiver, |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame] | 52 | const std::string& metrics_model_name); |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 53 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 54 | void set_disconnect_handler(base::Closure disconnect_handler); |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | // chromeos::machine_learning::mojom::GraphExecutor: |
Hidehiko Abe | 31bb963 | 2018-11-23 02:49:56 +0900 | [diff] [blame] | 58 | void Execute( |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 59 | base::flat_map<std::string, chromeos::machine_learning::mojom::TensorPtr> |
| 60 | inputs, |
Hidehiko Abe | 31bb963 | 2018-11-23 02:49:56 +0900 | [diff] [blame] | 61 | const std::vector<std::string>& output_names, |
Qijiang Fan | 5d381a0 | 2020-04-19 23:42:37 +0900 | [diff] [blame] | 62 | ExecuteCallback callback); |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 63 | |
| 64 | const std::map<std::string, int>& required_inputs_; |
| 65 | const std::map<std::string, int>& required_outputs_; |
| 66 | |
| 67 | const std::unique_ptr<tflite::Interpreter> interpreter_; |
| 68 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 69 | mojo::Receiver<chromeos::machine_learning::mojom::GraphExecutor> receiver_; |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 70 | |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame] | 71 | // Model name as it should appear in UMA histogram names. |
| 72 | const std::string metrics_model_name_; |
| 73 | |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 74 | DISALLOW_COPY_AND_ASSIGN(GraphExecutorImpl); |
| 75 | }; |
| 76 | |
| 77 | } // namespace ml |
| 78 | |
| 79 | #endif // ML_GRAPH_EXECUTOR_IMPL_H_ |