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