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