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