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> |
| 14 | #include <mojo/public/cpp/bindings/binding.h> |
| 15 | #include <tensorflow/contrib/lite/model.h> |
| 16 | |
| 17 | #include "ml/graph_executor_impl.h" |
Hidehiko Abe | aa488c3 | 2018-08-31 23:49:41 +0900 | [diff] [blame^] | 18 | #include "ml/mojom/model.mojom.h" |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 19 | |
| 20 | namespace ml { |
| 21 | |
| 22 | // Holds a TensorFlow lite graph and produces GraphExecutors that may run the |
| 23 | // graph. |
| 24 | // |
| 25 | // All GraphExecutors created by a ModelImpl reference its model definition (and |
| 26 | // hence may not outlive the ModelImpl). Multiple such GraphExecutors may be |
| 27 | // used concurrently from different sequences. |
| 28 | class ModelImpl : public chromeos::machine_learning::mojom::Model { |
| 29 | public: |
| 30 | // Creates an instance bound to |request|. |
| 31 | // |
| 32 | // The |required_inputs| and |required_outputs| arguments specify a mapping |
| 33 | // from required input / output tensor names to their indices in the TF lite |
| 34 | // graph, and must outlive this object. |
| 35 | ModelImpl(const std::map<std::string, int>& required_inputs, |
| 36 | const std::map<std::string, int>& required_outputs, |
| 37 | std::unique_ptr<tflite::FlatBufferModel> model, |
| 38 | chromeos::machine_learning::mojom::ModelRequest request); |
| 39 | |
| 40 | void set_connection_error_handler(base::Closure connection_error_handler); |
| 41 | |
| 42 | int num_graph_executors_for_testing() const; |
| 43 | |
| 44 | private: |
| 45 | // chromeos::machine_learning::mojom::Model: |
| 46 | void CreateGraphExecutor( |
| 47 | chromeos::machine_learning::mojom::GraphExecutorRequest request, |
| 48 | const CreateGraphExecutorCallback& callback) override; |
| 49 | |
| 50 | // Remove a graph executor from our hosted set. |
| 51 | void EraseGraphExecutor(std::list<GraphExecutorImpl>::const_iterator it); |
| 52 | |
| 53 | const std::map<std::string, int>& required_inputs_; |
| 54 | const std::map<std::string, int>& required_outputs_; |
| 55 | |
| 56 | const std::unique_ptr<tflite::FlatBufferModel> model_; |
| 57 | |
| 58 | mojo::Binding<chromeos::machine_learning::mojom::Model> binding_; |
| 59 | |
| 60 | // Emulate a strong binding set: hold a set of GraphExecutors, specific |
| 61 | // elements of which are erased on connection error. |
| 62 | // |
| 63 | // That is, when a pipe to a GraphExecutorImpl closes, that object is removed |
| 64 | // from this set (by its binding connection error handler). Further, when a |
| 65 | // ModelImpl is destoyed, its entire collection of GraphExecutorImpls is also |
| 66 | // destroyed. |
| 67 | std::list<GraphExecutorImpl> graph_executors_; |
| 68 | |
| 69 | DISALLOW_COPY_AND_ASSIGN(ModelImpl); |
| 70 | }; |
| 71 | |
| 72 | } // namespace ml |
| 73 | |
| 74 | #endif // ML_MODEL_IMPL_H_ |