aboutsummaryrefslogtreecommitdiff
path: root/src/backends/tosaCommon/operatorMappings/ActivationOperator.cpp
blob: c13555da6a26db7d56f0c2cfa66dfde2afbbb718 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//
// 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 "ActivationOperator.hpp"
#include "TosaRescaleOperatorUtils.hpp"

#include <layers/ActivationLayer.hpp>

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

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

    std::string inputName       = std::string("input_");
    std::string outputNameAlpha = std::string("intermediate1_") + GetUniqueTosaMappingID();
    std::string outputNameMul   = std::string("intermediate2_") + GetUniqueTosaMappingID();
    std::string outputName      = std::string("output0_");
    std::string blockName       = std::string("Op_ACTIVATION_block_") + GetUniqueTosaMappingID();

    // 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;

    // 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, {}));

#if TOSA_COMPAT_VERSION(0, 60, 0)
    std::string outputNameMAXMIN= std::string("intermediate3_") + GetUniqueTosaMappingID();

    if (inputDType0 == DType::DType_FP32 ||
        inputDType0 == DType::DType_FP16)
    {
        // const_alpha
        TosaSerializationOperator* alphaOp = nullptr;
        TosaSerializationTensor* alphaTensor = nullptr;
        CreateConstTosaOperator<float>(outputNameAlpha,
                                       activationDescriptor->m_A,
                                       inputDType0,
                                       inputShape0,
                                       alphaOp,
                                       alphaTensor);
        tensors.push_back(alphaTensor);

        // mul
        int32_t shift = 0;
        TosaMulAttribute mulAttribute(shift);
        TosaSerializationOperator* mulOp = new TosaSerializationOperator(Op_MUL,
                                                                         Attribute_MulAttribute,
                                                                         &mulAttribute,
                                                                         {inputName, outputNameAlpha},
                                                                         {outputNameMul});
        tensors.push_back(new TosaSerializationTensor(outputNameMul, inputShape0, inputDType0, {}));

        TosaSerializationOperator* op = nullptr;
        if (activationDescriptor->m_A <= 1.0)
        {
            op = new TosaSerializationOperator(Op_MAXIMUM,
                                               Attribute_NONE,
                                               nullptr,
                                               {inputName, outputNameMul},
                                               {outputName});
        }
        else
        {
            op = new TosaSerializationOperator(Op_MINIMUM,
                                               Attribute_NONE,
                                               nullptr,
                                               {inputName, outputNameMul},
                                               {outputName});

        }

        // 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
                                               {alphaOp, mulOp, op},   // operators
                                               tensors,                // tensors
                                               {inputName},            // inputs
                                               {outputName});          // outputs
    }
    else
    {
        std::string outputNameRescaleAlpha      = std::string("intermediate3_") + GetUniqueTosaMappingID();
        std::string outputNameRescaleIdentity   = std::string("intermediate4_") + GetUniqueTosaMappingID();
        std::string outputNameRescaleMaxMin     = std::string("intermediate5_") + GetUniqueTosaMappingID();

        DType rescale_type    = DType::DType_INT32;
        float alpha           = activationDescriptor->m_A;
        double scale_alpha    = inputs[0]->GetQuantizationScale() * alpha / outputs[0]->GetQuantizationScale();
        double scale_identity = inputs[0]->GetQuantizationScale() / outputs[0]->GetQuantizationScale();
        int32_t input_zp      = inputs[0]->GetQuantizationOffset();
        int32_t output_zp     = outputs[0]->GetQuantizationOffset();

        // Value op_rescale_alpha_in =
        //        buildRescale(rewriter, op, rescale_type, input, scale_alpha,
        //                     input_qtype.getZeroPoint(), 0, true, true);
        TosaSerializationOperator* rescaleAlphaOp = nullptr;
        CreateRescaleTosaOperator(inputName,
                                  outputNameRescaleAlpha,
                                  scale_alpha,
                                  input_zp,
                                  0,
                                  true,
                                  true,
                                  &rescaleAlphaOp);
        tensors.push_back(new TosaSerializationTensor(outputNameRescaleAlpha,
                                                      GetTosaTensorShape(inputs[0]->GetShape()),
                                                      rescale_type, {}));
        // Value op_rescale_identity_in =
        //       buildRescale(rewriter, op, rescale_type, input, scale_identity,
        //                    input_qtype.getZeroPoint(), 0, true, true);
        TosaSerializationOperator* rescaleIdentityOp = nullptr;
        CreateRescaleTosaOperator(inputName,
                                  outputNameRescaleIdentity,
                                  scale_identity,
                                  input_zp,
                                  0,
                                  true,
                                  true,
                                  &rescaleIdentityOp);
        tensors.push_back(new TosaSerializationTensor(outputNameRescaleIdentity,
                                                      GetTosaTensorShape(inputs[0]->GetShape()),
                                                      rescale_type, {}));
        // Value result_int32;
        // if (alpha <= 1.0) {
        //    auto max_op = CreateOpAndInfer<tosa::MaximumOp>(
        //            rewriter, op->getLoc(), rescale_type, op_rescale_identity_in,
        //            op_rescale_alpha_in);
        //    result_int32 = max_op.getResult();
        // } else {
        //    auto min_op = CreateOpAndInfer<tosa::MinimumOp>(
        //            rewriter, op->getLoc(), rescale_type, op_rescale_identity_in,
        //            op_rescale_alpha_in);
        //    result_int32 = min_op.getResult();
        // }
        TosaSerializationOperator* op = nullptr;
        if (alpha <= 1.0)
        {
            op = new TosaSerializationOperator(Op_MAXIMUM,
                                               Attribute_NONE,
                                               nullptr,
                                               {outputNameRescaleAlpha, outputNameRescaleIdentity},
                                               {outputNameRescaleMaxMin});
        }
        else
        {
            op = new TosaSerializationOperator(Op_MINIMUM,
                                               Attribute_NONE,
                                               nullptr,
                                               {outputNameRescaleAlpha, outputNameRescaleIdentity},
                                               {outputNameRescaleMaxMin});

        }
        tensors.push_back(new TosaSerializationTensor(outputNameRescaleMaxMin,
                                                      GetTosaTensorShape(inputs[0]->GetShape()),
                                                      rescale_type, {}));

        // Value output = buildRescaleFromInt32(rewriter, op, output_type, result_int32,
        //                                      1.0, output_qtype.getZeroPoint());
        TosaSerializationOperator* rescaleOutputOp = nullptr;
        CreateFromInt32RescaleTosaOperator(outputNameRescaleMaxMin,
                                           outputName,
                                           1.0,
                                           output_zp,
                                           &rescaleOutputOp);

        // 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
                                               {rescaleAlphaOp, rescaleIdentityOp, op, rescaleOutputOp}, // operators
                                               tensors,                // tensors
                                               {inputName},            // inputs
                                               {outputName});          // outputs
    }
#else
    std::string outputNameZero  = std::string("intermediate3_") + GetUniqueTosaMappingID();
    std::string outputNameGE    = std::string("intermediate4_") + GetUniqueTosaMappingID();

    // const_zero
    TosaSerializationOperator* zeroOp = nullptr;
    TosaSerializationTensor* zeroTensor = nullptr;
    CreateConstTosaOperator<float>(outputNameZero,
                                   0.0f,
                                   inputDType0,
                                   inputShape0,
                                   zeroOp,
                                   zeroTensor);
    tensors.push_back(zeroTensor);

    // const_alpha
    TosaSerializationOperator* alphaOp = nullptr;
    TosaSerializationTensor* alphaTensor = nullptr;
    CreateConstTosaOperator<float>(outputNameAlpha,
                                   activationDescriptor->m_A,
                                   inputDType0,
                                   inputShape0,
                                   alphaOp,
                                   alphaTensor);
    tensors.push_back(alphaTensor);

    // mul
    int32_t shift = 0;
    TosaMulAttribute mulAttribute(shift);
    TosaSerializationOperator* mulOp = new TosaSerializationOperator(Op_MUL,
                                                                     Attribute_MulAttribute,
                                                                     &mulAttribute,
                                                                     {inputName, outputNameAlpha},
                                                                     {outputNameMul});
    tensors.push_back(new TosaSerializationTensor(outputNameMul, inputShape0, inputDType0, {}));

    // greater_equal
    TosaSerializationOperator* geOp = new TosaSerializationOperator(Op_GREATER_EQUAL,
                                                                    Attribute_NONE,
                                                                    nullptr,
                                                                    {inputName, outputNameZero},
                                                                    {outputNameGE});
    tensors.push_back(new TosaSerializationTensor(outputNameGE, outputShape0, DType::DType_BOOL, {}));

    // select
    TosaSerializationOperator* selOp = new TosaSerializationOperator(Op_SELECT,
                                                                     Attribute_NONE,
                                                                     nullptr,
                                                                     {outputNameGE, inputName, outputNameMul},
                                                                     {outputName});

    // 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
                                           {zeroOp, alphaOp, mulOp, geOp, selOp},   // operators
                                           tensors,                                 // tensors
                                           {inputName},                             // inputs
                                           {outputName});                           // outputs
#endif
}