• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_DELEGATES_NNAPI_NNAPI_DELEGATE_MOCK_TEST_H_
16 #define TENSORFLOW_LITE_DELEGATES_NNAPI_NNAPI_DELEGATE_MOCK_TEST_H_
17 
18 // Cannot mock the delegate when using the disabled version
19 // (see the condition in the BUILD file).
20 #ifndef NNAPI_DELEGATE_DISABLED
21 
22 #include <fcntl.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 
26 #include <memory>
27 
28 #include <gtest/gtest.h>
29 #include "absl/memory/memory.h"
30 #include "tensorflow/lite/delegates/nnapi/nnapi_delegate.h"
31 #include "tensorflow/lite/nnapi/NeuralNetworksTypes.h"
32 #include "tensorflow/lite/nnapi/nnapi_handler.h"
33 #include "tensorflow/lite/nnapi/nnapi_implementation.h"
34 
35 namespace tflite {
36 namespace delegate {
37 namespace nnapi {
38 
39 class NnApiMock : public ::tflite::nnapi::NnApiHandler {
40  public:
41   explicit NnApiMock(NnApi* nnapi, int android_sdk_version = 29)
NnApiHandler(nnapi)42       : ::tflite::nnapi::NnApiHandler(nnapi) {
43     nnapi_->nnapi_exists = true;
44     nnapi_->android_sdk_version = android_sdk_version;
45     nnapi_->nnapi_runtime_feature_level = android_sdk_version;
46 
47     nnapi_->ANeuralNetworksCompilation_free =
48         [](ANeuralNetworksCompilation* compilation) {};
49     nnapi_->ANeuralNetworksMemory_free = [](ANeuralNetworksMemory* memory) {};
50     nnapi_->ANeuralNetworksModel_free = [](ANeuralNetworksModel* model) {};
51     nnapi_->ANeuralNetworksExecution_free =
52         [](ANeuralNetworksExecution* execution) {};
53     nnapi_->ASharedMemory_create = [](const char* name, size_t size) -> int {
54       return open("/dev/zero", O_RDWR);
55     };
56     nnapi_->ANeuralNetworksEvent_free = [](ANeuralNetworksEvent* event) {};
57 
58     ModelCreateReturns<ANEURALNETWORKS_NO_ERROR>();
59     AddOperandReturns<ANEURALNETWORKS_NO_ERROR>();
60     SetOperandValueReturns<ANEURALNETWORKS_NO_ERROR>();
61     AddOperationReturns<ANEURALNETWORKS_NO_ERROR>();
62     IdentifyInputAndOutputsReturns<ANEURALNETWORKS_NO_ERROR>();
63     RelaxComputationFloatReturns<ANEURALNETWORKS_NO_ERROR>();
64     ModelFinishReturns<ANEURALNETWORKS_NO_ERROR>();
65     MemoryCreateFromFdReturns<ANEURALNETWORKS_NO_ERROR>();
66     CompilationCreateReturns<ANEURALNETWORKS_NO_ERROR>();
67     CompilationCreateForDevicesReturns<ANEURALNETWORKS_NO_ERROR>();
68     CompilationFinishReturns<ANEURALNETWORKS_NO_ERROR>();
69     ExecutionCreateReturns<ANEURALNETWORKS_NO_ERROR>();
70     ExecutionSetInputFromMemoryReturns<ANEURALNETWORKS_NO_ERROR>();
71     ExecutionSetOutputFromMemoryReturns<ANEURALNETWORKS_NO_ERROR>();
72     ExecutionComputeReturns<ANEURALNETWORKS_NO_ERROR>();
73     ExecutionStartComputeReturns<ANEURALNETWORKS_NO_ERROR>();
74     EventWaitReturns<ANEURALNETWORKS_NO_ERROR>();
75     SetPriorityReturns<ANEURALNETWORKS_NO_ERROR>();
76     SetOperandSymmPerChannelQuantParamsReturns<ANEURALNETWORKS_NO_ERROR>();
77     SetNnapiSupportedDevice("test-device", android_sdk_version);
78   }
79 
~NnApiMock()80   ~NnApiMock() { Reset(); }
81 };
82 
83 class NnApiDelegateMockTest : public ::testing::Test {
84  protected:
SetUp()85   void SetUp() override {
86     nnapi_ = *NnApiImplementation();
87     nnapi_mock_ = std::make_unique<NnApiMock>(&nnapi_);
88   }
89 
90   std::unique_ptr<NnApiMock> nnapi_mock_;
91 
92  private:
93   NnApi nnapi_;
94 };
95 
96 }  // namespace nnapi
97 }  // namespace delegate
98 }  // namespace tflite
99 
100 #endif  // #ifndef NNAPI_DELEGATE_DISABLED
101 
102 #endif  // TENSORFLOW_LITE_DELEGATES_NNAPI_NNAPI_DELEGATE_MOCK_TEST_H_
103