blob: 6f92aff54e44016ea69f598947125e74d53cca9e [file] [log] [blame]
Michael Martisa967f632018-08-10 10:39:00 +10001// 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>
Michael Martis8783c8e2019-06-26 17:30:54 +100015#include <tensorflow/lite/model.h>
Michael Martisa967f632018-08-10 10:39:00 +100016
17#include "ml/graph_executor_impl.h"
Hidehiko Abeaa488c32018-08-31 23:49:41 +090018#include "ml/mojom/model.mojom.h"
Michael Martisa967f632018-08-10 10:39:00 +100019
20namespace 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.
28class 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.
Honglin Yu0ed72352019-08-27 17:42:01 +100035 // |model_string| is optional string data that this class can take ownership
36 // of (presumably the backing data for |model|) and that is guaranteed to be
37 // destroyed *after* |model|. This is required by function
38 // |tflite::FlatBufferModel::BuildFromBuffer|.
39 ModelImpl(std::map<std::string, int> required_inputs,
40 std::map<std::string, int> required_outputs,
41 std::unique_ptr<tflite::FlatBufferModel> model,
42 std::unique_ptr<std::string> model_string,
43 chromeos::machine_learning::mojom::ModelRequest request,
44 const std::string& metrics_model_name);
45
46 // Use when constructed from file where no need to pass the |model_string|.
47 ModelImpl(std::map<std::string, int> required_inputs,
48 std::map<std::string, int> required_outputs,
Michael Martisa967f632018-08-10 10:39:00 +100049 std::unique_ptr<tflite::FlatBufferModel> model,
Honglin Yu6adafcd2019-07-22 13:48:11 +100050 chromeos::machine_learning::mojom::ModelRequest request,
51 const std::string& metrics_model_name);
Michael Martisa967f632018-08-10 10:39:00 +100052
53 void set_connection_error_handler(base::Closure connection_error_handler);
54
55 int num_graph_executors_for_testing() const;
56
57 private:
58 // chromeos::machine_learning::mojom::Model:
59 void CreateGraphExecutor(
60 chromeos::machine_learning::mojom::GraphExecutorRequest request,
61 const CreateGraphExecutorCallback& callback) override;
62
63 // Remove a graph executor from our hosted set.
64 void EraseGraphExecutor(std::list<GraphExecutorImpl>::const_iterator it);
65
Honglin Yu0ed72352019-08-27 17:42:01 +100066 const std::map<std::string, int> required_inputs_;
67 const std::map<std::string, int> required_outputs_;
68
69 // Must be above |model_|.
70 const std::unique_ptr<std::string> model_string_;
Michael Martisa967f632018-08-10 10:39:00 +100071
72 const std::unique_ptr<tflite::FlatBufferModel> model_;
73
74 mojo::Binding<chromeos::machine_learning::mojom::Model> binding_;
75
76 // Emulate a strong binding set: hold a set of GraphExecutors, specific
77 // elements of which are erased on connection error.
78 //
79 // That is, when a pipe to a GraphExecutorImpl closes, that object is removed
80 // from this set (by its binding connection error handler). Further, when a
81 // ModelImpl is destoyed, its entire collection of GraphExecutorImpls is also
82 // destroyed.
83 std::list<GraphExecutorImpl> graph_executors_;
84
Honglin Yu6adafcd2019-07-22 13:48:11 +100085 // Model name as it should appear in UMA histogram names.
86 const std::string metrics_model_name_;
87
Michael Martisa967f632018-08-10 10:39:00 +100088 DISALLOW_COPY_AND_ASSIGN(ModelImpl);
89};
90
91} // namespace ml
92
93#endif // ML_MODEL_IMPL_H_