Home | History | Annotate | Download | only in operation_signatures
      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 #include "fuzzing/operation_signatures/OperationSignatureUtils.h"
     18 
     19 using namespace std::placeholders;
     20 
     21 namespace android {
     22 namespace nn {
     23 namespace fuzzing_test {
     24 
     25 static void fullyConnectedConstructor(Type, uint32_t rank, HalVersion ver, RandomOperation* op) {
     26     // Inputs, flattened to [batch_size, input_size]
     27     op->inputs[0]->dimensions.resize(rank);
     28     RandomVariable numElements = 1;
     29     for (uint32_t i = 0; i < rank; i++) {
     30         op->inputs[0]->dimensions[i] = RandomVariableType::FREE;
     31         numElements = numElements * op->inputs[0]->dimensions[i];
     32     }
     33 
     34     // Weights, [num_units, input_size]
     35     op->inputs[1]->dimensions = {RandomVariableType::FREE, RandomVariableType::FREE};
     36 
     37     // Bias, [num_units]
     38     op->inputs[2]->dimensions = {op->inputs[1]->dimensions[0]};
     39 
     40     // Output, [batch_size, num_units]
     41     op->outputs[0]->dimensions = {numElements.exactDiv(op->inputs[1]->dimensions[1]),
     42                                   op->inputs[1]->dimensions[0]};
     43 
     44     setConvFCScale(/*applyOutputScaleBound=*/ver == HalVersion::V1_0, op);
     45 }
     46 
     47 DEFINE_OPERATION_SIGNATURE(signature_FULLY_CONNECTED_V1_0){
     48         .opType = ANEURALNETWORKS_FULLY_CONNECTED,
     49         .supportedDataTypes = {Type::TENSOR_FLOAT32, Type::TENSOR_QUANT8_ASYMM},
     50         .supportedRanks = {2, 3, 4},
     51         .version = HalVersion::V1_0,
     52         .inputs = {INPUT_DEFAULT, INPUT_DEFAULT, INPUT_BIAS,
     53                    PARAMETER_CHOICE(Type::INT32, 0, 1, 2, 3)},
     54         .outputs = {OUTPUT_DEFAULT},
     55         .constructor = std::bind(fullyConnectedConstructor, _1, _2, HalVersion::V1_0, _3)};
     56 
     57 DEFINE_OPERATION_SIGNATURE(signature_FULLY_CONNECTED_V1_2){
     58         .opType = ANEURALNETWORKS_FULLY_CONNECTED,
     59         .supportedDataTypes = {Type::TENSOR_QUANT8_ASYMM, Type::TENSOR_FLOAT16},
     60         .supportedRanks = {2, 3, 4},
     61         .version = HalVersion::V1_2,
     62         .inputs = {INPUT_DEFAULT, INPUT_DEFAULT, INPUT_BIAS,
     63                    PARAMETER_CHOICE(Type::INT32, 0, 1, 2, 3)},
     64         .outputs = {OUTPUT_DEFAULT},
     65         .constructor = std::bind(fullyConnectedConstructor, _1, _2, HalVersion::V1_2, _3)};
     66 
     67 }  // namespace fuzzing_test
     68 }  // namespace nn
     69 }  // namespace android
     70