aboutsummaryrefslogtreecommitdiff
path: root/src/backends/tosaCommon/operatorMappings/ReluOperator.cpp
blob: 541b39cd8d52a9090bbcc2c503dba96d3e7909d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// Copyright © 2024 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//
//
// Copyright © 2020 The TensorFlow Authors. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//

#include "LeakyReluOperator.hpp"
#include "TosaRescaleOperatorUtils.hpp"

#include <layers/ActivationLayer.hpp>

// This function is paraphrased from:
// tensorflow/compiler/mlir/tosa/transforms/legalize_tfl.cc from function ConvertTFLReluOp
TosaSerializationBasicBlock* ConvertReluToTosaOperator(const Layer* layer,
                                                       const std::vector<const TensorInfo*>& inputs,
                                                       const std::vector<const TensorInfo*>& outputs,
                                                       const ActivationDescriptor* desc)
{
    if (inputs.size() != 1)
    {
        throw armnn::Exception("ConvertReluToTosaOperator: 1 input tensors required.");
    }

    if (outputs.size() != 1)
    {
        throw armnn::Exception("ConvertReluToTosaOperator: 1 output tensor required.");
    }

    std::string inputName  = std::string("input_");
    std::string outputName = std::string("output0_");
    std::string blockName  = "";

    int32_t clamp_min = 0;
    int32_t clamp_max = 0;
    float float_max = 0.0f;
    switch (desc->m_Function)
    {
        case ActivationFunction::ReLu:
        {
            clamp_max = std::numeric_limits<int32_t>::max();
            float_max = std::numeric_limits<float>::max();
            blockName = std::string("Op_RELU_block_") + GetUniqueTosaMappingID();
            break;
        }
        case ActivationFunction::BoundedReLu:
        {
            clamp_max = static_cast<int32_t>(desc->m_A);
            float_max = desc->m_A;
            blockName = std::string("Op_BOUNDED_RELU_block_") + GetUniqueTosaMappingID();
            break;
        }
        case ActivationFunction::LeakyReLu:
        {
            throw Exception("LeakyRelu TOSA mappings are performed in ConvertLeakyReluToTosaOperator().");
        }
        default:
        {
            throw Exception("Activation function is not supported in ConvertReluToTosaOperator().");
        }
    }

    // If a layer is present then the block will be used for execution, so input and output names need to be determined
    // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
    if (layer != nullptr)
    {
        inputName  = GenerateUniqueInputName(layer->GetInputSlot(0));
        outputName = GenerateUniqueOutputName(*layer);
    }

    std::vector<TosaSerializationTensor*> tensors;
    std::vector<TosaSerializationOperator*> operators;

    // Only add input tensors if connected layer is an input layer.
    // As intermediate or constant tensors will be created separately.
    // There also can't be duplicate tensor.
    std::vector<int32_t> inputShape0;
    DType inputDType0 = DType::DType_UNKNOWN;
    if(inputName.find("input_") != std::string::npos)
    {
        inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
        inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
        tensors.push_back(new TosaSerializationTensor(inputName, inputShape0, inputDType0, {}));
    }

    std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
    DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
    tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));

    std::string clampInputNameStr = inputName;
    if (inputDType0 == tosa::DType::DType_INT8 || inputDType0 == tosa::DType::DType_INT16)
    {
        std::string outputNameRescale = std::string("intermediate0_") + GetUniqueTosaMappingID();
        clampInputNameStr = outputNameRescale;

        double scale = inputs[0]->GetQuantizationScale() / outputs[0]->GetQuantizationScale();
        int32_t input_zp = inputs[0]->GetQuantizationOffset();
        int32_t output_zp = outputs[0]->GetQuantizationOffset();

        clamp_min = output_zp;

        if (desc->m_Function == ActivationFunction::BoundedReLu)
        {
            clamp_max = static_cast<int32_t>(std::round(desc->m_A / outputs[0]->GetQuantizationScale())) + output_zp;
        }

        if (inputDType0 == tosa::DType::DType_INT8)
        {
            clamp_min =
                clamp_min < std::numeric_limits<int8_t>::min() ? std::numeric_limits<int8_t>::min() : clamp_min;
            clamp_max =
                clamp_max > std::numeric_limits<int8_t>::max() ? std::numeric_limits<int8_t>::max() : clamp_max;
        }
        else
        {
            clamp_min =
                clamp_min < std::numeric_limits<int16_t>::min() ? std::numeric_limits<int16_t>::min() : clamp_min;
            clamp_max =
                clamp_max > std::numeric_limits<int16_t>::max() ? std::numeric_limits<int16_t>::max() : clamp_max;
        }

        TosaSerializationOperator* rescaleOp = nullptr;
        CreateRescaleTosaOperator(inputName,
                                  outputNameRescale,
                                  scale,
                                  input_zp,
                                  output_zp,
                                  false,
                                  true,
                                  &rescaleOp);
        operators.push_back(rescaleOp);
        tensors.push_back(new TosaSerializationTensor(outputNameRescale,
                                                      inputShape0,
                                                      inputDType0,
                                                      {}));
    }
    
    TosaClampAttribute attribute(clamp_min, clamp_max, 0, float_max);
    auto* clamp_op = new TosaSerializationOperator(Op_CLAMP,
                                                   Attribute_ClampAttribute,
                                                   &attribute,
                                                   {clampInputNameStr},
                                                   {outputName});
    operators.push_back(clamp_op);

    // operatorInputNames/operatorOutputNames ends up being the same as
    // blockInputNames/blockOutputNames for one-to-one ArmNN to Tosa mappings
    return new TosaSerializationBasicBlock(blockName,      // name
                                           mainName,       // region name
                                           operators,      // operators
                                           tensors,        // tensors
                                           {inputName},    // inputs
                                           {outputName});  // outputs
}