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