summaryrefslogtreecommitdiff
path: root/onert-micro/luci-interpreter/src/kernels/SVDF.cpp
blob: a0ff30255680f497b3829a04aff53f66d14f5376 (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
/*
 * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "kernels/SVDF.h"
#include "kernels/Utils.h"
#include "PALSVDF.h"

#include <tensorflow/lite/kernels/internal/quantization_util.h>

namespace luci_interpreter
{
namespace kernels
{

namespace
{
TfLiteFusedActivation get_tflite_activation(Activation activation)
{
  switch (activation)
  {
    case FusedActFunc::RELU:
      return kTfLiteActRelu;
    case FusedActFunc::RELU6:
      return kTfLiteActRelu6;
    case FusedActFunc::RELU_N1_TO_1:
      return kTfLiteActReluN1To1;
    case FusedActFunc::TANH:
      return kTfLiteActTanh;
    case FusedActFunc::SIGN_BIT:
      return kTfLiteActSignBit;
    case FusedActFunc::NONE:
      return kTfLiteActNone;
    default:
      assert(false && "Unsupported activation type");
  }
}
} // namespace

SVDF::SVDF(const Tensor *input, const Tensor *weight_feature, const Tensor *weight_time,
           const Tensor *bias, const Tensor *input_activation_state, Tensor *output,
           Tensor *scratchpad_activation_state, Tensor *scratchpad_1, Tensor *scratchpad_2,
           Tensor *scratchpad_3, Tensor *scratchpad_4, Tensor *scratchpad_5, Tensor *scratchpad_6,
           const SVDFParams &params)
  : KernelWithParams<SVDFParams>({input, weight_feature, weight_time, bias, input_activation_state},
                                 {output, scratchpad_activation_state, scratchpad_1, scratchpad_2,
                                  scratchpad_3, scratchpad_4, scratchpad_5, scratchpad_6},
                                 params)
{
  // Do nothing
}

void SVDF::configure()
{
  const Shape &input_shape = input()->shape();
  const Shape &weight_features_shape = weight_feature()->shape();
  const Shape &weight_time_shape = weight_time()->shape();

  // Validate Input Tensor:
  LUCI_INTERPRETER_CHECK(input()->element_type() == DataType::FLOAT32 ||
                         input()->element_type() == DataType::S8);
  LUCI_INTERPRETER_CHECK(input_shape.num_dims() == 2);

  // Validate inputs and output types
  if (input()->element_type() == DataType::S8)
  {
    LUCI_INTERPRETER_CHECK(weight_feature()->element_type() == DataType::S8);
    LUCI_INTERPRETER_CHECK(weight_time()->element_type() == DataType::S16 ||
                           weight_time()->element_type() == DataType::S8);
    if (bias())
      LUCI_INTERPRETER_CHECK(bias()->element_type() == DataType::S32);

    LUCI_INTERPRETER_CHECK(input_activation_state()->element_type() == DataType::S16 ||
                           input_activation_state()->element_type() == DataType::S8);
    LUCI_INTERPRETER_CHECK(output()->element_type() == DataType::S8);

    // Note: now tflite support only ReLU activation for integer SVDF
    LUCI_INTERPRETER_CHECK(params().activation == FusedActFunc::RELU);
  }
  else if (weight_feature()->element_type() == DataType::FLOAT32)
  {
    LUCI_INTERPRETER_CHECK(weight_feature()->element_type() == DataType::FLOAT32);
    LUCI_INTERPRETER_CHECK(weight_time()->element_type() == DataType::FLOAT32);
    LUCI_INTERPRETER_CHECK(input_activation_state()->element_type() == DataType::FLOAT32);
    if (bias())
      LUCI_INTERPRETER_CHECK(bias()->element_type() == DataType::FLOAT32);
    LUCI_INTERPRETER_CHECK(output()->element_type() == DataType::FLOAT32);
  }
  else if ((weight_feature()->element_type() == DataType::U8 ||
            weight_feature()->element_type() == DataType::S8) &&
           input()->element_type() == DataType::FLOAT32)
  {
    // TODO:: support hybrid SVDF op
    assert(false && "Hybrid type is not currently supported");
  }
  else
  {
    assert(false && "Unsupported type.");
  }

  // Check all the parameters of tensor match within themselves and match the
  // input configuration.
  const int rank = params().svdf_rank;
  const int batch_size = input_shape.dim(0);
  const int num_filters = weight_features_shape.dim(0);
  LUCI_INTERPRETER_CHECK(rank != 0);
  LUCI_INTERPRETER_CHECK(num_filters % rank == 0);

  const int num_units = num_filters / rank;
  const int memory_size = weight_time_shape.dim(1);

  // Validate Weight_Feature Input Tensor:
  LUCI_INTERPRETER_CHECK(weight_features_shape.num_dims() == 2);
  LUCI_INTERPRETER_CHECK(weight_features_shape.dim(1) == input_shape.dim(1));

  // Validate Weight_Time Input Tensor:
  LUCI_INTERPRETER_CHECK(weight_time_shape.num_dims() == 2);
  LUCI_INTERPRETER_CHECK(weight_time_shape.dim(0) == num_filters);

  // Validate Bias
  if (bias())
    LUCI_INTERPRETER_CHECK(bias()->shape().dim(0) == num_units);

  // Validate Input Activation State
  LUCI_INTERPRETER_CHECK(input_activation_state()->shape().num_dims() == 2);
  LUCI_INTERPRETER_CHECK(input_activation_state()->shape().dim(0) == batch_size);
  LUCI_INTERPRETER_CHECK(input_activation_state()->shape().dim(1) == memory_size * num_filters);

  // Resize scratchpad_state to input_activation_state
  auto scratchpad_activation_state = getOutputTensors()[1];
  scratchpad_activation_state->resize({batch_size, memory_size * num_filters});

  // TODO: enable it only if kernel with dynamic shapes
  // Resize output tensor
  output()->resize({batch_size, num_units});

  luci_interpreter_pal::SetupScratchpadTensor(
    input()->element_type(), weight_feature()->element_type(), getOutputTensors()[2],
    getOutputTensors()[3], getOutputTensors()[4], getOutputTensors()[5], getOutputTensors()[6],
    getOutputTensors()[7], input_shape, weight_time_shape, batch_size, num_filters, num_units);
}

void SVDF::execute() const
{
  switch (weight_feature()->element_type())
  {
    case DataType::FLOAT32:
      evalFloat();
      break;
    case DataType::S8:
    {
      if (input()->element_type() == DataType::S8)
        evalInteger();
      else
        // TODO:: support hybrid SVDF op
        assert(false && "Hybrid type is not currently supported");
      break;
    }
    default:
      assert(false && "Unsupported type");
  }
}

void SVDF::evalInteger() const
{
  const auto effective_scale_1 = static_cast<double>(input()->scale() * weight_feature()->scale() /
                                                     input_activation_state()->scale());
  const auto effective_scale_2 = static_cast<double>(input_activation_state()->scale() *
                                                     weight_time()->scale() / output()->scale());

  int32_t effective_scale_1_a;
  int effective_scale_1_b;
  int32_t effective_scale_2_a;
  int effective_scale_2_b;

  tflite::QuantizeMultiplier(effective_scale_1, &effective_scale_1_a, &effective_scale_1_b);
  tflite::QuantizeMultiplier(effective_scale_2, &effective_scale_2_a, &effective_scale_2_b);

  TfLiteSVDFParams params_svdf{};
  params_svdf.asymmetric_quantize_inputs = params().asymmetric_quantize_inputs;
  params_svdf.rank = params().svdf_rank;
  params_svdf.activation = get_tflite_activation(params().activation);

  auto scratchpad_activation_state = getOutputTensors()[1];
  // Note: it is expected that activation_state input variable tensor reset to zero,
  // also expected that this variable tensor doesn't have buffer
  auto scratchpad_data = getTensorData<int16_t>(scratchpad_activation_state);
  std::fill_n(scratchpad_data, scratchpad_activation_state->shape().num_elements(), 0);

  auto scratchpad = getOutputTensors()[2];
  auto output_temp = getOutputTensors()[3];

  int32_t input_zp = input()->zero_point();
  int32_t output_zp = output()->zero_point();
  luci_interpreter_pal::IntegerSVDF(
    params_svdf, getTensorShape(input()), getTensorData<int8_t>(input()),
    getTensorShape(weight_feature()), getTensorData<int8_t>(weight_feature()),
    getTensorShape(weight_time()), getTensorData<int16_t>(weight_time()), getTensorShape(bias()),
    getTensorData<int32_t>(bias()), scratchpad_data, getTensorShape(output()),
    getTensorData<int8_t>(output()), getTensorData<int32_t>(scratchpad),
    getTensorData<int32_t>(output_temp), effective_scale_1_a, effective_scale_1_b,
    effective_scale_2_a, effective_scale_2_b, input_zp, output_zp);
}

void SVDF::evalFloat() const
{
  TfLiteSVDFParams params_svdf{};
  params_svdf.asymmetric_quantize_inputs = params().asymmetric_quantize_inputs;
  params_svdf.rank = params().svdf_rank;
  params_svdf.activation = get_tflite_activation(params().activation);

  auto scratchpad_activation_state = getOutputTensors()[1];
  // Note: it is expected that activation_state input variable tensor reset to zero,
  // also expected that this variable tensor doesn't have buffer
  auto scratchpad_data = getTensorData<float>(scratchpad_activation_state);
  std::fill_n(scratchpad_data, scratchpad_activation_state->shape().num_elements(), 0);

  auto scratchpad_1 = getOutputTensors()[2];

  luci_interpreter_pal::FloatSVDF(
    params_svdf, getTensorShape(input()), getTensorData<float>(input()),
    getTensorShape(weight_feature()), getTensorData<float>(weight_feature()),
    getTensorShape(weight_time()), getTensorData<float>(weight_time()), getTensorShape(bias()),
    getTensorData<float>(bias()), getTensorData<float>(scratchpad_1), scratchpad_data,
    getTensorShape(output()), getTensorData<float>(output()));
}

} // namespace kernels
} // namespace luci_interpreter