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