• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_TEST_GENERATED_TEST_UTILS_H
18 #define ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_TEST_GENERATED_TEST_UTILS_H
19 
20 #include <gtest/gtest.h>
21 
22 #include <memory>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #include "TestHarness.h"
28 
29 #ifdef NNTEST_SLTS
30 #include "SupportLibraryWrapper.h"
31 #else
32 #include "TestNeuralNetworksWrapper.h"
33 #endif
34 
35 namespace android::nn::generated_tests {
36 
37 #ifdef NNTEST_SLTS
38 namespace test_wrapper = android::nn::sl_wrapper;
39 #endif
40 
41 class GeneratedTestBase
42     : public ::testing::TestWithParam<test_helper::TestModelManager::TestParam> {
43    protected:
44     const std::string& kTestName = GetParam().first;
45     const test_helper::TestModel& testModel = *GetParam().second;
46 };
47 
48 #define INSTANTIATE_GENERATED_TEST(TestSuite, filter)                                          \
49     INSTANTIATE_TEST_SUITE_P(                                                                  \
50             TestGenerated, TestSuite,                                                          \
51             ::testing::ValuesIn(::test_helper::TestModelManager::get().getTestModels(filter)), \
52             [](const auto& info) { return info.param.first; })
53 
54 // A generated NDK model.
55 class GeneratedModel : public test_wrapper::Model {
56    public:
57 #ifdef NNTEST_SLTS
GeneratedModel(const NnApiSupportLibrary * nnapi)58     GeneratedModel(const NnApiSupportLibrary* nnapi) : sl_wrapper::Model(nnapi) {}
59 #endif
60 
61     // A helper method to simplify referenced model lifetime management.
62     //
63     // Usage:
64     //     GeneratedModel model;
65     //     std::vector<Model> refModels;
66     //     createModel(&model, &refModels);
67     //     model.setRefModels(std::move(refModels));
68     //
69     // This makes sure referenced models live as long as the main model.
70     //
setRefModels(std::vector<test_wrapper::Model> refModels)71     void setRefModels(std::vector<test_wrapper::Model> refModels) {
72         mRefModels = std::move(refModels);
73     }
74 
75     // A helper method to simplify CONSTANT_REFERENCE memory lifetime management.
setConstantReferenceMemory(std::unique_ptr<test_wrapper::Memory> memory)76     void setConstantReferenceMemory(std::unique_ptr<test_wrapper::Memory> memory) {
77         mConstantReferenceMemory = std::move(memory);
78     }
79 
80    private:
81     std::vector<test_wrapper::Model> mRefModels;
82     std::unique_ptr<test_wrapper::Memory> mConstantReferenceMemory;
83 };
84 
85 // Convert TestModel to NDK model.
86 #ifdef NNTEST_SLTS
87 void createModel(const NnApiSupportLibrary* nnapi, const test_helper::TestModel& testModel,
88                  bool testDynamicOutputShape, GeneratedModel* model);
createModel(const NnApiSupportLibrary * nnapi,const test_helper::TestModel & testModel,GeneratedModel * model)89 inline void createModel(const NnApiSupportLibrary* nnapi, const test_helper::TestModel& testModel,
90                         GeneratedModel* model) {
91     createModel(nnapi, testModel, /*testDynamicOutputShape=*/false, model);
92 }
93 #else
94 void createModel(const test_helper::TestModel& testModel, bool testDynamicOutputShape,
95                  GeneratedModel* model);
createModel(const test_helper::TestModel & testModel,GeneratedModel * model)96 inline void createModel(const test_helper::TestModel& testModel, GeneratedModel* model) {
97     createModel(testModel, /*testDynamicOutputShape=*/false, model);
98 }
99 #endif
100 
101 void createRequest(const test_helper::TestModel& testModel, test_wrapper::Execution* execution,
102                    std::vector<test_helper::TestBuffer>* outputs);
103 
104 }  // namespace android::nn::generated_tests
105 
106 #endif  // ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_TEST_GENERATED_TEST_UTILS_H
107