• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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_RUNTIME_TEST_FUZZING_OPERATION_MANAGER_H
18 #define ANDROID_FRAMEWORKS_ML_NN_RUNTIME_TEST_FUZZING_OPERATION_MANAGER_H
19 
20 #include <functional>
21 #include <map>
22 #include <string>
23 #include <vector>
24 
25 #include "TestHarness.h"
26 #include "TestNeuralNetworksWrapper.h"
27 #include "fuzzing/RandomGraphGenerator.h"
28 #include "fuzzing/RandomVariable.h"
29 
30 namespace android {
31 namespace nn {
32 namespace fuzzing_test {
33 
34 struct OperandSignature {
35     // Possible values are [INPUT | CONST | OUTPUT].
36     // If CONST, the generator will avoid feeding the operand with another operation’s output.
37     RandomOperandType type;
38 
39     // The operand constructor is invoked before the operation constructor. This is for
40     // setting the data type, quantization parameters, or optionally the scalar value.
41     std::function<void(test_helper::TestOperandType, uint32_t, RandomOperand*)> constructor =
42             nullptr;
43 
44     // The operand finalizer is invoked after the graph structure is frozen but before the operation
45     // finalizer. This is for generating the buffer values for the operand.
46     std::function<void(RandomOperand*)> finalizer = nullptr;
47 };
48 
49 // A filter applied to OperationSignatures. An OperationSignature is filtered out if the opType, all
50 // of supportedDataTypes, all of supportedRanks, or the version does not match with any entry in the
51 // corresponding vector in the filter. Use empty vector in the filter for "no filter".
52 //
53 // E.g.
54 // - To get all 1.0 ADD operation signatures
55 //       {.opcodes = {TestOperationType::ADD}, .versions = {TestHalVersion::V1_0}}
56 //
57 // - To get all 1.0 and 1.1 operations with rank 2 or 4
58 //       {.ranks = {2, 4}, .versions = {TestHalVersion::V1_0, TestHalVersion::V1_1}}
59 //
60 struct OperationFilter {
61     std::vector<test_helper::TestOperationType> opcodes;
62     std::vector<test_helper::TestOperandType> dataTypes;
63     std::vector<uint32_t> ranks;
64     std::vector<test_helper::TestHalVersion> versions;
65 };
66 
67 struct OperationSignature {
68     // Defines the basic metadata of an operation filterable by OperationFilter.
69     // Upon generation, the random graph generator will randomly choose a supported data type and
70     // rank, and pass the information to the constructors.
71     test_helper::TestOperationType opType;
72     std::vector<test_helper::TestOperandType> supportedDataTypes;
73     std::vector<uint32_t> supportedRanks;
74     test_helper::TestHalVersion version;
75 
76     // OperandSignatures for inputs and outputs.
77     std::vector<OperandSignature> inputs;
78     std::vector<OperandSignature> outputs;
79 
80     // The operation constructor is invoked after the inputs and outputs constructor. This is for
81     // setting the dimension relationship of random operands, and/or generating parameter values at
82     // the operation level, e.g. a parameter depends on or affects another operand in the same
83     // operation.
84     std::function<void(test_helper::TestOperandType, uint32_t, RandomOperation*)> constructor =
85             nullptr;
86 
87     // The operation finalizer is invoked after the graph structure is frozen and inputs and outputs
88     // constructors are invoked. This is for generating operand buffers at the operation level, e.g.
89     // an operand buffer that depends on another operand in the same operation.
90     std::function<void(RandomOperation*)> finalizer = nullptr;
91 
92     // Check if the OperationSignature matches with the filter. Shrink supported* if needed.
93     bool matchFilter(const OperationFilter& filter);
94 };
95 
96 // A manager manages all available OperationSignatures.
97 class OperationManager {
98    public:
99     // Returns the singleton manager.
100     static OperationManager* get();
101 
102     // Add the signature to the managed mOperationSignatures if matches with mFilter.
103     void addSignature(const std::string& name, const OperationSignature& signature);
104 
105     // Reinitialize mFilteredSignatures by applying a filter to all of the operation signatures in
106     // mOperationSignatures.
107     void applyFilter(const OperationFilter& filter);
108 
109     // Randomly returns a valid OperationSignature. RandomGraphGenerator uses this method to
110     // generate a random list of operations in the graph.
111     const OperationSignature& getRandomOperation() const;
112 
113    protected:
114     OperationManager() = default;
115 
116     // Contains all operation signatures defined in operation_signatures/*.cpp.
117     // The signatures are sorted by name to ensure a predictable order.
118     std::map<std::string, const OperationSignature> mOperationSignatures;
119 
120     // A subset of mOperationSignatures using by the random graph generator.
121     // This vector should be initialized by applyFilter() before generating the graph.
122     std::vector<OperationSignature> mFilteredSignatures;
123 };
124 
125 }  // namespace fuzzing_test
126 }  // namespace nn
127 }  // namespace android
128 
129 #endif  // ANDROID_FRAMEWORKS_ML_NN_RUNTIME_TEST_FUZZING_OPERATION_MANAGER_H
130