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 | #include <memory> |
| 6 | #include <string> |
| 7 | #include <utility> |
| 8 | #include <vector> |
| 9 | |
Hidehiko Abe | 7ac22bb | 2018-11-08 00:29:25 +0900 | [diff] [blame] | 10 | #include <base/bind.h> |
hscham | 3d0632f | 2019-12-11 15:58:57 +0900 | [diff] [blame] | 11 | #include <base/containers/flat_map.h> |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 12 | #include <base/macros.h> |
| 13 | #include <base/run_loop.h> |
| 14 | #include <gmock/gmock.h> |
| 15 | #include <gtest/gtest.h> |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 16 | #include <mojo/public/cpp/bindings/remote.h> |
Michael Martis | 8783c8e | 2019-06-26 17:30:54 +1000 | [diff] [blame] | 17 | #include <tensorflow/lite/model.h> |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 18 | |
| 19 | #include "ml/model_impl.h" |
Hidehiko Abe | 8ab64a6 | 2018-09-19 00:04:39 +0900 | [diff] [blame] | 20 | #include "ml/mojom/graph_executor.mojom.h" |
| 21 | #include "ml/mojom/model.mojom.h" |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 22 | #include "ml/tensor_view.h" |
| 23 | #include "ml/test_utils.h" |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 24 | |
| 25 | namespace ml { |
| 26 | namespace { |
| 27 | |
| 28 | using ::chromeos::machine_learning::mojom::CreateGraphExecutorResult; |
| 29 | using ::chromeos::machine_learning::mojom::ExecuteResult; |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 30 | using ::chromeos::machine_learning::mojom::GraphExecutor; |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 31 | using ::chromeos::machine_learning::mojom::Model; |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 32 | using ::chromeos::machine_learning::mojom::TensorPtr; |
| 33 | using ::testing::ElementsAre; |
| 34 | |
| 35 | class ModelImplTest : public testing::Test { |
| 36 | protected: |
| 37 | // Metadata for the example model: |
| 38 | // A simple model that adds up two tensors. Inputs and outputs are 1x1 float |
| 39 | // tensors. |
| 40 | const std::string model_path_ = |
Michael Martis | c22823d | 2018-09-14 12:54:04 +1000 | [diff] [blame] | 41 | GetTestModelDir() + "mlservice-model-test_add-20180914.tflite"; |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 42 | const std::map<std::string, int> model_inputs_ = {{"x", 1}, {"y", 2}}; |
| 43 | const std::map<std::string, int> model_outputs_ = {{"z", 0}}; |
| 44 | }; |
| 45 | |
| 46 | // Test loading an invalid model. |
| 47 | TEST_F(ModelImplTest, TestBadModel) { |
| 48 | // Pass nullptr instead of a valid model. |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 49 | mojo::Remote<Model> model; |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 50 | ModelImpl::Create(model_inputs_, model_outputs_, nullptr /*model*/, |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 51 | model.BindNewPipeAndPassReceiver(), "TestModel"); |
| 52 | ASSERT_TRUE(model.is_bound()); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 53 | |
| 54 | // Ensure that creating a graph executor fails. |
| 55 | bool callback_done = false; |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 56 | mojo::Remote<GraphExecutor> graph_executor; |
| 57 | model->CreateGraphExecutor( |
| 58 | graph_executor.BindNewPipeAndPassReceiver(), |
Hidehiko Abe | 7ac22bb | 2018-11-08 00:29:25 +0900 | [diff] [blame] | 59 | base::Bind( |
| 60 | [](bool* callback_done, const CreateGraphExecutorResult result) { |
| 61 | EXPECT_EQ(result, |
| 62 | CreateGraphExecutorResult::MODEL_INTERPRETATION_ERROR); |
| 63 | *callback_done = true; |
| 64 | }, |
| 65 | &callback_done)); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 66 | |
| 67 | base::RunLoop().RunUntilIdle(); |
| 68 | ASSERT_TRUE(callback_done); |
| 69 | } |
| 70 | |
| 71 | // Test loading the valid example model. |
| 72 | TEST_F(ModelImplTest, TestExampleModel) { |
| 73 | // Read the example TF model from disk. |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 74 | std::unique_ptr<tflite::FlatBufferModel> tflite_model = |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 75 | tflite::FlatBufferModel::BuildFromFile(model_path_.c_str()); |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 76 | ASSERT_NE(tflite_model.get(), nullptr); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 77 | |
| 78 | // Create model object. |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 79 | mojo::Remote<Model> model; |
| 80 | ModelImpl::Create(model_inputs_, model_outputs_, std::move(tflite_model), |
| 81 | model.BindNewPipeAndPassReceiver(), "TestModel"); |
| 82 | ASSERT_TRUE(model.is_bound()); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 83 | |
| 84 | // Create a graph executor. |
| 85 | bool cge_callback_done = false; |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 86 | mojo::Remote<GraphExecutor> graph_executor; |
| 87 | model->CreateGraphExecutor( |
| 88 | graph_executor.BindNewPipeAndPassReceiver(), |
Hidehiko Abe | 7ac22bb | 2018-11-08 00:29:25 +0900 | [diff] [blame] | 89 | base::Bind( |
| 90 | [](bool* cge_callback_done, const CreateGraphExecutorResult result) { |
| 91 | EXPECT_EQ(result, CreateGraphExecutorResult::OK); |
| 92 | *cge_callback_done = true; |
| 93 | }, |
| 94 | &cge_callback_done)); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 95 | |
| 96 | base::RunLoop().RunUntilIdle(); |
| 97 | ASSERT_TRUE(cge_callback_done); |
| 98 | |
| 99 | // Construct input/output for graph execution. |
hscham | 3d0632f | 2019-12-11 15:58:57 +0900 | [diff] [blame] | 100 | base::flat_map<std::string, TensorPtr> inputs; |
Hidehiko Abe | 7ac22bb | 2018-11-08 00:29:25 +0900 | [diff] [blame] | 101 | inputs.emplace("x", NewTensor<double>({1}, {0.5})); |
| 102 | inputs.emplace("y", NewTensor<double>({1}, {0.25})); |
| 103 | std::vector<std::string> outputs({"z"}); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 104 | |
| 105 | // Execute graph. |
| 106 | bool exe_callback_done = false; |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 107 | graph_executor->Execute( |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 108 | std::move(inputs), std::move(outputs), |
Hidehiko Abe | 7ac22bb | 2018-11-08 00:29:25 +0900 | [diff] [blame] | 109 | base::Bind( |
| 110 | [](bool* exe_callback_done, const ExecuteResult result, |
| 111 | base::Optional<std::vector<TensorPtr>> outputs) { |
| 112 | // Check that the inference succeeded and gives the expected number |
| 113 | // of outputs. |
| 114 | EXPECT_EQ(result, ExecuteResult::OK); |
| 115 | ASSERT_TRUE(outputs.has_value()); |
| 116 | ASSERT_EQ(outputs->size(), 1); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 117 | |
Hidehiko Abe | 7ac22bb | 2018-11-08 00:29:25 +0900 | [diff] [blame] | 118 | // Check that the output tensor has the right type and format. |
| 119 | const TensorView<double> out_tensor((*outputs)[0]); |
| 120 | EXPECT_TRUE(out_tensor.IsValidType()); |
| 121 | EXPECT_TRUE(out_tensor.IsValidFormat()); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 122 | |
Hidehiko Abe | 7ac22bb | 2018-11-08 00:29:25 +0900 | [diff] [blame] | 123 | // Check the output tensor has the expected shape and values. |
| 124 | EXPECT_THAT(out_tensor.GetShape(), ElementsAre(1)); |
| 125 | EXPECT_THAT(out_tensor.GetValues(), ElementsAre(0.75)); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 126 | |
Hidehiko Abe | 7ac22bb | 2018-11-08 00:29:25 +0900 | [diff] [blame] | 127 | *exe_callback_done = true; |
| 128 | }, |
| 129 | &exe_callback_done)); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 130 | |
| 131 | base::RunLoop().RunUntilIdle(); |
| 132 | ASSERT_TRUE(exe_callback_done); |
| 133 | } |
| 134 | |
| 135 | TEST_F(ModelImplTest, TestGraphExecutorCleanup) { |
| 136 | // Read the example TF model from disk. |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 137 | std::unique_ptr<tflite::FlatBufferModel> tflite_model = |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 138 | tflite::FlatBufferModel::BuildFromFile(model_path_.c_str()); |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 139 | ASSERT_NE(tflite_model.get(), nullptr); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 140 | |
| 141 | // Create model object. |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 142 | mojo::Remote<Model> model; |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 143 | const ModelImpl* model_impl = |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 144 | ModelImpl::Create(model_inputs_, model_outputs_, std::move(tflite_model), |
| 145 | model.BindNewPipeAndPassReceiver(), "TestModel"); |
| 146 | ASSERT_TRUE(model.is_bound()); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 147 | |
| 148 | // Create one graph executor. |
| 149 | bool cge1_callback_done = false; |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 150 | mojo::Remote<GraphExecutor> graph_executor_1; |
| 151 | model->CreateGraphExecutor( |
| 152 | graph_executor_1.BindNewPipeAndPassReceiver(), |
Hidehiko Abe | 7ac22bb | 2018-11-08 00:29:25 +0900 | [diff] [blame] | 153 | base::Bind( |
| 154 | [](bool* cge1_callback_done, const CreateGraphExecutorResult result) { |
| 155 | EXPECT_EQ(result, CreateGraphExecutorResult::OK); |
| 156 | *cge1_callback_done = true; |
| 157 | }, |
| 158 | &cge1_callback_done)); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 159 | |
| 160 | base::RunLoop().RunUntilIdle(); |
| 161 | ASSERT_TRUE(cge1_callback_done); |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 162 | ASSERT_TRUE(graph_executor_1.is_bound()); |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 163 | ASSERT_EQ(model_impl->num_graph_executors_for_testing(), 1); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 164 | |
| 165 | // Create another graph executor. |
| 166 | bool cge2_callback_done = false; |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 167 | mojo::Remote<GraphExecutor> graph_executor_2; |
| 168 | model->CreateGraphExecutor( |
| 169 | graph_executor_2.BindNewPipeAndPassReceiver(), |
Hidehiko Abe | 7ac22bb | 2018-11-08 00:29:25 +0900 | [diff] [blame] | 170 | base::Bind( |
| 171 | [](bool* cge2_callback_done, const CreateGraphExecutorResult result) { |
| 172 | EXPECT_EQ(result, CreateGraphExecutorResult::OK); |
| 173 | *cge2_callback_done = true; |
| 174 | }, |
| 175 | &cge2_callback_done)); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 176 | |
| 177 | base::RunLoop().RunUntilIdle(); |
| 178 | ASSERT_TRUE(cge2_callback_done); |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 179 | ASSERT_TRUE(graph_executor_2.is_bound()); |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 180 | ASSERT_EQ(model_impl->num_graph_executors_for_testing(), 2); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 181 | |
| 182 | // Destroy one graph executor. |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 183 | graph_executor_1.reset(); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 184 | base::RunLoop().RunUntilIdle(); |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 185 | ASSERT_TRUE(graph_executor_2.is_bound()); |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 186 | ASSERT_EQ(model_impl->num_graph_executors_for_testing(), 1); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 187 | |
| 188 | // Destroy the other graph executor. |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 189 | graph_executor_2.reset(); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 190 | base::RunLoop().RunUntilIdle(); |
Honglin Yu | c0cef10 | 2020-01-17 15:26:01 +1100 | [diff] [blame] | 191 | ASSERT_EQ(model_impl->num_graph_executors_for_testing(), 0); |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | } // namespace |
| 195 | } // namespace ml |