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_TYPES_OPERATIONS_VALIDATION_UTILS_H
18 #define ANDROID_PACKAGES_MODULES_NEURALNETWORKS_COMMON_TYPES_OPERATIONS_VALIDATION_UTILS_H
19 
20 #include <string>
21 #include <vector>
22 
23 #include "OperationsUtils.h"
24 #include "nnapi/TypeUtils.h"
25 #include "nnapi/Types.h"
26 
27 namespace android::nn {
28 
29 #define NN_VALIDATION_FUNCTION_NAME(opType) validate_##opType
30 
31 #define NN_VALIDATION_FUNCTION_SIGNATURE(opType) \
32     Result<Version> NN_VALIDATION_FUNCTION_NAME(opType)(const IOperationValidationContext* context)
33 
34 #define NN_DEFINE_VALIDATION_FUNCTION(opType, validate) \
35     NN_VALIDATION_FUNCTION_SIGNATURE(opType) { return validate(context); }
36 
37 // Provides information available during graph creation to validate an operation.
38 class IOperationValidationContext {
39    public:
~IOperationValidationContext()40     virtual ~IOperationValidationContext() {}
41 
42     virtual const char* getOperationName() const = 0;
43 
44     virtual uint32_t getNumInputs() const = 0;
45     virtual OperandType getInputType(uint32_t index) const = 0;
46     virtual Shape getInputShape(uint32_t index) const = 0;
47     virtual const Operand::ExtraParams& getInputExtraParams(uint32_t index) const = 0;
48 
49     virtual uint32_t getNumOutputs() const = 0;
50     virtual OperandType getOutputType(uint32_t index) const = 0;
51     virtual Shape getOutputShape(uint32_t index) const = 0;
52 
53     std::string invalidInOutNumberMessage(int expIn, int expOut) const;
54 
55     Result<void> validateOperationOperandTypes(
56             const std::vector<OperandType>& inExpectedTypes,
57             const std::vector<OperandType>& outExpectedInTypes) const;
58 };
59 
60 // Verifies that the number and types of operation inputs are as expected.
61 bool validateInputTypes(const IOperationValidationContext* context,
62                         const std::vector<OperandType>& expectedTypes);
63 
64 // Verifies that the number and types of operation outputs are as expected.
65 bool validateOutputTypes(const IOperationValidationContext* context,
66                          const std::vector<OperandType>& expectedTypes);
67 
68 // Verifies that the HAL version specified in the context is greater or equal
69 // than the minimal supported HAL version.
70 bool validateVersion(const IOperationValidationContext* context, Version contextVersion,
71                      Version minSupportedVersion);
72 
73 }  // namespace android::nn
74 
75 #endif  // ANDROID_PACKAGES_MODULES_NEURALNETWORKS_COMMON_TYPES_OPERATIONS_VALIDATION_UTILS_H
76