1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
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 
16 #ifndef NEURAL_NETWORK_RUNTIME_SYSTEM_TEST_NNRT_TEST
17 #define NEURAL_NETWORK_RUNTIME_SYSTEM_TEST_NNRT_TEST
18 
19 #include <cstdint>
20 #include <gtest/gtest.h>
21 #include <memory>
22 #include <vector>
23 
24 #include "interfaces/kits/c/neural_network_runtime/neural_network_runtime.h"
25 
26 namespace OHOS {
27 namespace NeuralNetworkRuntime {
28 namespace SystemTest {
29 struct CppQuantParam {
30     std::vector<uint32_t> numBits;
31     std::vector<double> scale;
32     std::vector<int32_t> zeroPoint;
33 };
34 
35 struct CppTensor {
36     OH_NN_DataType dataType{OH_NN_UNKNOWN};
37     std::vector<int32_t> dimensions;
38     void* data{nullptr};
39     size_t dataLength{0};
40     CppQuantParam quantParam;
41     OH_NN_TensorType type{OH_NN_TENSOR};
42 };
43 
44 struct Node {
45     OH_NN_OperationType opType;
46     std::vector<uint32_t> inputs;
47     std::vector<uint32_t> outputs;
48     std::vector<uint32_t> params;
49 };
50 
51 class NNRtTest : public testing::Test {
52 public:
53     virtual OH_NN_ReturnCode AddTensors(const std::vector<CppTensor>& cppTensors);
54     virtual OH_NN_ReturnCode AddOperation(OH_NN_OperationType opType,
55                                           const std::vector<uint32_t>& paramIndices,
56                                           const std::vector<uint32_t>& inputIndices,
57                                           const std::vector<uint32_t>& outputIndices);
58     virtual OH_NN_ReturnCode SpecifyInputAndOutput(const std::vector<uint32_t>& inputIndices,
59                                                    const std::vector<uint32_t>& outputIndices);
60     virtual OH_NN_ReturnCode SetInput(uint32_t index,
61                                       const std::vector<int32_t>& dimensions,
62                                       const void* buffer,
63                                       size_t length);
64     virtual OH_NN_ReturnCode SetOutput(uint32_t index, void* buffer, size_t length);
65     virtual OH_NN_ReturnCode SetInputFromMemory(uint32_t index,
66                                                 const std::vector<int32_t>& dimensions,
67                                                 const void* buffer,
68                                                 size_t length,
69                                                 OH_NN_Memory** pMemory);
70     virtual OH_NN_ReturnCode SetOutputFromMemory(uint32_t index, size_t length, OH_NN_Memory** pMemory);
71     virtual OH_NN_ReturnCode GetDevices();
72 
73 protected:
74     OH_NNModel* m_model{nullptr};
75     OH_NNCompilation* m_compilation{nullptr};
76     OH_NNExecutor* m_executor{nullptr};
77 
78     std::vector<OH_NN_Tensor> m_tensors;
79     std::vector<std::unique_ptr<OH_NN_QuantParam>> m_quantParams;
80     std::vector<Node> m_nodes;
81     std::vector<uint32_t> m_inputs;
82     std::vector<uint32_t> m_outputs;
83     std::vector<size_t> m_devices;
84 };
85 } // namespace SystemTest
86 } // NeuralNetworkRuntime
87 } // OHOS
88 
89 #endif // NEURAL_NETWORK_RUNTIME_SYSTEM_TEST_NNRT_TEST