1 /*
2  * Copyright (C) 2017 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_FRAMEWORKS_ML_NN_COMMON_VALIDATE_HAL_H
18 #define ANDROID_FRAMEWORKS_ML_NN_COMMON_VALIDATE_HAL_H
19 
20 #include <set>
21 #include <tuple>
22 
23 #include "HalInterfaces.h"
24 
25 namespace android {
26 namespace nn {
27 
28 enum class HalVersion : int32_t {
29     UNKNOWN,
30     V1_0,
31     V1_1,
32     V1_2,
33     V1_3,
34     LATEST = V1_3,
35 };
36 
37 enum class IOType { INPUT, OUTPUT };
38 using PreparedModelRole = std::tuple<const hal::IPreparedModel*, IOType, uint32_t>;
39 
40 // 1.3 HAL does not support control flow operations with operands of unknown size.
41 // See http://b/132458982#comment63.
42 enum class ValidationMode { DRIVER, RUNTIME };
43 
44 // Verifies that the model is valid, i.e. it is consistent, takes
45 // only acceptable values, the constants don't extend outside the memory
46 // regions they are part of, etc.
47 // IMPORTANT: This function cannot validate that OEM operation and operands
48 // are correctly defined, as these are specific to each implementation.
49 // Each driver should do their own validation of OEM types.
50 template <class T_Model>
51 bool validateModel(const T_Model& model, ValidationMode mode = ValidationMode::DRIVER);
52 
53 // Verifies that the request for the given model is valid.
54 // IMPORTANT: This function cannot validate that OEM operation and operands
55 // are correctly defined, as these are specific to each implementation.
56 // Each driver should do their own validation of OEM types.
57 // For HAL version 1.3 or higher, this function cannot validate that the
58 // buffer tokens are valid. Each driver should do their own validation of
59 // buffer tokens.
60 template <class T_Request, class T_Model>
61 bool validateRequest(const T_Request& request, const T_Model& model,
62                      bool allowUnspecifiedOutput = true);
63 
64 // Verifies that the execution preference is valid.
65 bool validateExecutionPreference(hal::ExecutionPreference preference);
66 
67 // Verifies that the priority is valid.
68 bool validatePriority(hal::Priority priority);
69 
70 bool validOperationType(hal::V1_0::OperationType operation);
71 bool validOperationType(hal::V1_1::OperationType operation);
72 bool validOperationType(hal::V1_2::OperationType operation);
73 
74 bool validOperandType(hal::V1_0::OperandType operand);
75 bool validOperandType(hal::V1_2::OperandType operand);
76 bool validOperandType(hal::V1_3::OperandType operand);
77 
78 // Verifies that the memory pool is valid in the specified HAL version.
79 bool validatePool(const hal::hidl_memory& pool, HalVersion ver = HalVersion::LATEST);
80 bool validatePool(const hal::V1_3::Request::MemoryPool& pool, HalVersion ver = HalVersion::LATEST);
81 
82 // Verifies that the input arguments to IDevice::allocate are valid.
83 // Optionally, this function can return a flattened prepared model roles and a combined operand.
84 // Pass nullptr if either value is not needed.
85 // IMPORTANT: This function cannot validate dimensions and extraParams with extension operand type.
86 // Each driver should do their own validation of extension type dimensions and extraParams.
87 bool validateMemoryDesc(
88         const hal::V1_3::BufferDesc& desc,
89         const hal::hidl_vec<sp<hal::V1_3::IPreparedModel>>& preparedModels,
90         const hal::hidl_vec<hal::V1_3::BufferRole>& inputRoles,
91         const hal::hidl_vec<hal::V1_3::BufferRole>& outputRoles,
92         std::function<const hal::V1_3::Model*(const sp<hal::V1_3::IPreparedModel>&)> getModel,
93         std::set<PreparedModelRole>* preparedModelRoles, hal::V1_3::Operand* combinedOperand);
94 
95 }  // namespace nn
96 }  // namespace android
97 
98 #endif  // ANDROID_FRAMEWORKS_ML_NN_COMMON_VALIDATE_HAL_H
99