blob: d5d89df12aaccbd2c3c265fb3d727fc422a0363b [file] [log] [blame]
Sadik Armagan1153d1e2020-04-01 15:09:39 +01001//
2// Copyright © 2020 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include "ConversionUtils_1_2.hpp"
9
10using Half = half_float::half;
11
12namespace armnn_driver
13{
14
15using namespace armnn;
16using namespace android::nn;
17
18template<typename HalPolicy,
19 typename HalOperation = typename HalPolicy::Operation,
20 typename HalModel = typename HalPolicy::Model>
21bool ConvertElu(const HalOperation& operation, const HalModel& model, ConversionData& data)
22{
23 using HalOperandType = typename HalPolicy::OperandType;
24
25 LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
26 if (!input0.IsValid())
27 {
28 return Fail("%s: Operation has invalid inputs", __func__);
29 }
30
31 // Determine data type of input tensor
32 HalOperandType inputType;
33 if (!GetOperandType<HalPolicy>(operation, 0, model, inputType))
34 {
35 return Fail("%s: Operation has invalid inputs", __func__);
36 }
37
38 ActivationDescriptor desc;
39 desc.m_Function = ActivationFunction::Elu;
40
41 // Read alpha
42 if (inputType == HalOperandType::TENSOR_FLOAT16)
43 {
44 Half alpha;
45
46 if (!GetInputScalar<HalPolicy>(operation, 1, HalOperandType::FLOAT16, alpha, model, data))
47 {
48 return Fail("%s: Operation has invalid inputs (FLOAT16)", __func__);
49 }
50
51 desc.m_A = static_cast<float>(alpha);
52 }
53 else if (inputType == HalOperandType::TENSOR_FLOAT32)
54 {
55 if (!GetInputScalar<HalPolicy>(operation, 1, HalOperandType::FLOAT32, desc.m_A, model, data))
56 {
57 return Fail("%s: Operation has invalid inputs (FLOAT32)", __func__);
58 }
59 }
60 else
61 {
62 return Fail("%s: Unsupported input tensor type: %d", __func__, inputType);
63 }
64
65 return ::ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data);
66}
67
Sadik Armagan813f2302020-05-19 14:10:30 +010068template<typename HalPolicy,
Sadik Armagan2e329612020-06-24 10:57:23 +010069 typename HalOperation = typename HalPolicy::Operation,
70 typename HalModel = typename HalPolicy::Model>
71bool ConvertFill(const HalOperation& operation, const HalModel& model, ConversionData& data)
72{
73 using HalOperand = typename HalPolicy::Operand;
74 using HalOperandType = typename HalPolicy::OperandType;
75
76 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
77 if (!input.IsValid())
78 {
79 return Fail("%s: Operation has invalid inputs", __func__);
80 }
81
82 const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
83 if (!output)
84 {
85 return Fail("%s: Could not read output", __func__);
86 }
87
88 const TensorInfo& inputInfo = input.GetTensorInfo();
89 const TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
90 if (IsDynamicTensor(outputInfo))
91 {
92 return Fail("%s: Dynamic output tensors are not supported", __func__);
93 }
94
95 // Determine data type of output tensor
96 HalOperandType outputType = output->type;
97 FillDescriptor descriptor;
98 // Read the scalar fill value
99 if (outputType == HalOperandType::TENSOR_FLOAT16)
100 {
101 Half value;
102
103 if (!GetInputScalar<HalPolicy>(operation, 1, HalOperandType::FLOAT16, value, model, data))
104 {
105 return Fail("%s: Operation has invalid inputs %d", __func__, outputType);
106 }
107
108 descriptor.m_Value = static_cast<float>(value);
109 }
110 else if (outputType == HalOperandType::TENSOR_FLOAT32)
111 {
112 if (!GetInputScalar<HalPolicy>(operation, 1, HalOperandType::FLOAT32, descriptor.m_Value, model, data))
113 {
114 return Fail("%s: Operation has invalid inputs %d", __func__, outputType);
115 }
116 }
117 else if (outputType == HalOperandType::TENSOR_INT32)
118 {
119 int32_t value;
120
121 if (!GetInputScalar<HalPolicy>(operation, 1, HalOperandType::INT32, value, model, data))
122 {
123 return Fail("%s: Operation has invalid inputs %d", __func__, outputType);
124 }
125
126 descriptor.m_Value = static_cast<float>(value);
127 }
128 else
129 {
130 return Fail("%s: Unsupported input tensor type: %d", __func__, outputType);
131 }
132
133 bool isSupported = false;
134 FORWARD_LAYER_SUPPORT_FUNC(__func__,
135 IsFillSupported,
136 data.m_Backends,
137 isSupported,
138 inputInfo,
139 outputInfo,
140 descriptor);
141 if (!isSupported)
142 {
143 return false;
144 }
145
146 IConnectableLayer* const layer = data.m_Network->AddFillLayer(descriptor);
147 assert(layer != nullptr);
148 input.Connect(layer->GetInputSlot(0));
149 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
150
151 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
152}
153
154template<typename HalPolicy,
Sadik Armagan813f2302020-05-19 14:10:30 +0100155 typename HalOperation = typename HalPolicy::Operation,
156 typename HalModel = typename HalPolicy::Model>
157bool ConvertQuantizedLstm(const HalOperation& operation, const HalModel& model, ConversionData& data)
158{
159 using HalOperand = typename HalPolicy::Operand;
160 using HalOperandType = typename HalPolicy::OperandType;
161
162 ALOGV("HalPolicy::ConvertQuantizedLstm()");
163
164 //Inputs:
165 // 0: The input: A 2-D tensor of type ANEURALNETWORKS_TENSOR_QUANT8_ASYMM and shape [numBatches, inputSize]
166 // specifying the input to the LSTM cell. Tensor is quantized with a fixed quantization range of -1, 127/128.
167 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
168 if (!input.IsValid())
169 {
170 return Fail("%s: Could not read input 0: input", __func__);
171 }
172
173 // 18: The output state: A 2-D tensor of ANEURALNETWORKS_TENSOR_QUANT8_ASYMM, of shape [batch_size, output_size].
174 LayerInputHandle outputStatePrevTimeStep = ConvertToLayerInputHandle<HalPolicy>(operation, 18, model, data);
175 if (!outputStatePrevTimeStep.IsValid())
176 {
177 return Fail("%s: Could not read input 18: outputStatePrevTimeStep", __func__);
178 }
179
180 // 19: The cell state: A 2-D tensor of ANEURALNETWORKS_TENSOR_QUANT16_SYMM, of shape [batch_size, num_units].
181 LayerInputHandle cellStatePrevTimeStep = ConvertToLayerInputHandle<HalPolicy>(operation, 19, model, data);
182 if (!cellStatePrevTimeStep.IsValid())
183 {
184 return Fail("%s: Could not read input 19: cellStatePrevTimeStep", __func__);
185 }
186
187 // Get the mandatory input tensors:
188
189 // 02: The input-to-forget weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_QUANT8_SYMM, of shape
190 // [num_units, input_size].
191 const ConstTensorPin inputToForgetWeightsPin =
192 ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 2, model, data);
193
194 // 03: The input-to-cell weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_QUANT8_SYMM, of shape
195 // [num_units, input_size].
196 const ConstTensorPin inputToCellWeightsPin =
197 ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 3, model, data);
198
199 // 04: The input-to-output weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_QUANT8_SYMM, of shape
200 // [num_units, input_size].
201 const ConstTensorPin inputToOutputWeightsPin =
202 ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 4, model, data);
203
204 // 06: The recurrent-to-forget weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_QUANT8_SYMM, of shape
205 // [num_units, output_size].
206 const ConstTensorPin recurrentToForgetWeightsPin =
207 ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 6, model, data);
208
209 // 07: The recurrent-to-cell weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_QUANT8_SYMM, of shape
210 // [num_units, output_size].
211 const ConstTensorPin recurrentToCellWeightsPin =
212 ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 7, model, data);
213
214 // 08: The recurrent-to-output weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_QUANT8_SYMM, of shape
215 // [num_units, output_size].
216 const ConstTensorPin recurrentToOutputWeightsPin =
217 ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 8, model, data);
218
219 // 13: The forget gate bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_INT32, of shape [num_units].
220 const ConstTensorPin forgetGateBiasPin =
221 ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 13, model, data);
222
223 // 14: The cell bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_INT32, of shape [num_units].
224 const ConstTensorPin cellBiasPin =
225 ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 14, model, data);
226
227 // 15: The output gate bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_INT32, of shape [num_units].
228 const ConstTensorPin outputGateBiasPin =
229 ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 15, model, data);
230
231 if (!inputToForgetWeightsPin.IsValid() ||
232 !inputToCellWeightsPin.IsValid() ||
233 !inputToOutputWeightsPin.IsValid() ||
234 !recurrentToForgetWeightsPin.IsValid() ||
235 !recurrentToCellWeightsPin.IsValid() ||
236 !recurrentToOutputWeightsPin.IsValid() ||
237 !forgetGateBiasPin.IsValid() ||
238 !cellBiasPin.IsValid() ||
239 !outputGateBiasPin.IsValid())
240 {
241 return Fail("%s: Operation has invalid tensor inputs", __func__);
242 }
243
244 // Get the optional input tensors:
245
246 // 01: The input-to-input weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_QUANT8_SYMM, of shape
247 // [num_units, input_size], where “num_units” corresponds to the number of cell units.
248 const ConstTensorPin inputToInputWeightsPin =
249 ConvertOperationInputToConstTensorPin<HalPolicy>(operation,
250 1,
251 model,
252 data,
253 g_DontPermute,
254 nullptr,
255 true);
256
257 // 05: The recurrent-to-input weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_QUANT8_SYMM, of shape
258 // [num_units, output_size], where “output_size” corresponds to either the number of cell units (i.e.,
259 // “num_units”), or the second dimension of the “projection_weights”, if defined.
260 const ConstTensorPin recurrentToInputWeightsPin =
261 ConvertOperationInputToConstTensorPin<HalPolicy>(operation,
262 5,
263 model,
264 data,
265 g_DontPermute,
266 nullptr,
267 true);
268
269 // 09: The cell-to-input weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_QUANT16_SYMM, of shape
270 // [num_units].
271 const ConstTensorPin cellToInputWeightsPin =
272 ConvertOperationInputToConstTensorPin<HalPolicy>(operation,
273 9,
274 model,
275 data,
276 g_DontPermute,
277 nullptr,
278 true);
279
280 // 10: The cell-to-forget weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_QUANT16_SYMM, of shape
281 // [num_units].
282 const ConstTensorPin cellToForgetWeightsPin =
283 ConvertOperationInputToConstTensorPin<HalPolicy>(operation,
284 10,
285 model,
286 data,
287 g_DontPermute,
288 nullptr,
289 true);
290
291 // 11: The cell-to-output weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_QUANT16_SYMM, of shape
292 // [num_units].
293 const ConstTensorPin cellToOutputWeightsPin =
294 ConvertOperationInputToConstTensorPin<HalPolicy>(operation,
295 11,
296 model,
297 data,
298 g_DontPermute,
299 nullptr,
300 true);
301
302 // 12: The input gate bias: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_INT32, of shape [num_units].
303 const ConstTensorPin inputGateBiasPin =
304 ConvertOperationInputToConstTensorPin<HalPolicy>(operation,
305 12,
306 model,
307 data,
308 g_DontPermute,
309 nullptr,
310 true);
311
312 // 16: The projection weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_QUANT8_SYMM, of shape
313 // [output_size, num_units].
314 const ConstTensorPin projectionWeightsPin =
315 ConvertOperationInputToConstTensorPin<HalPolicy>(operation,
316 16,
317 model,
318 data,
319 g_DontPermute,
320 nullptr,
321 true);
322
323 // 17: The projection bias: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_INT32, of shape [output_size].
324 const ConstTensorPin projectionBiasPin =
325 ConvertOperationInputToConstTensorPin<HalPolicy>(operation,
326 17,
327 model,
328 data,
329 g_DontPermute,
330 nullptr,
331 true);
332
333 if ((!inputToInputWeightsPin.IsValid() && !inputToInputWeightsPin.IsOptional())
334 || (!recurrentToInputWeightsPin.IsValid() && !recurrentToInputWeightsPin.IsOptional())
335 || (!cellToInputWeightsPin.IsValid() && !cellToInputWeightsPin.IsOptional())
336 || (!cellToForgetWeightsPin.IsValid() && !cellToForgetWeightsPin.IsOptional())
337 || (!cellToOutputWeightsPin.IsValid() && !cellToOutputWeightsPin.IsOptional())
338 || (!inputGateBiasPin.IsValid() && !inputGateBiasPin.IsOptional())
339 || (!projectionWeightsPin.IsValid() && !projectionWeightsPin.IsOptional())
340 || (!projectionBiasPin.IsValid() && !projectionBiasPin.IsOptional()))
341 {
342 return Fail("%s: Operation has invalid tensor inputs", __func__);
343 }
344
345
346 // Get the optional normalization tensors
347
348 // 20: The input layer normalization weights. A 1-D tensor of shape [num_units] ANEURALNETWORKS_TENSOR_QUANT16_SYMM.
349 // Used to rescale normalized inputs to activation at input gate.
350 const ConstTensorPin inputLayerNormWeightsPin =
351 ConvertOperationInputToConstTensorPin<HalPolicy>(operation,
352 20,
353 model,
354 data,
355 g_DontPermute,
356 nullptr,
357 true);
358
359 // 21: The forget layer normalization weights. A 1-D tensor of shape [num_units] ANEURALNETWORKS_TENSOR_QUANT16_SYMM
360 // Used to rescale normalized inputs to activation at forget gate.
361 const ConstTensorPin forgetLayerNormWeightsPin =
362 ConvertOperationInputToConstTensorPin<HalPolicy>(operation,
363 21,
364 model,
365 data,
366 g_DontPermute,
367 nullptr,
368 true);
369
370 // 22: The cell layer normalization weights. A 1-D tensor of shape [num_units] ANEURALNETWORKS_TENSOR_QUANT16_SYMM.
371 // Used to rescale normalized inputs to activation at cell gate.
372 const ConstTensorPin cellLayerNormWeightsPin =
373 ConvertOperationInputToConstTensorPin<HalPolicy>(operation,
374 22,
375 model,
376 data,
377 g_DontPermute,
378 nullptr,
379 true);
380
381 // 23: The output layer normalization weights. A 1-D tensor of shape [num_units].
382 // Used to rescale normalized inputs to activation at output gate.
383 const ConstTensorPin outputLayerNormWeightsPin =
384 ConvertOperationInputToConstTensorPin<HalPolicy>(operation,
385 23,
386 model,
387 data,
388 g_DontPermute,
389 nullptr,
390 true);
391
392 if ((!inputLayerNormWeightsPin.IsValid() && !inputLayerNormWeightsPin.IsOptional())
393 || (!forgetLayerNormWeightsPin.IsValid() && !forgetLayerNormWeightsPin.IsOptional())
394 || (!cellLayerNormWeightsPin.IsValid() && !cellLayerNormWeightsPin.IsOptional())
395 || (!outputLayerNormWeightsPin.IsValid() && !outputLayerNormWeightsPin.IsOptional()))
396 {
397 return Fail("%s: Operation has invalid tensor inputs", __func__);
398 }
399
400 // Get the optional input scalars:
401 // 24: The cell clip: If provided the cell state is clipped by this value prior to the cell output activation.
402 // 25: The projection clip: If provided and projection is enabled, this is used for clipping the projected values.
403
404 // Get the mandatory input scalars:
405 // 26: The scale of the intermediate result of matmul, i.e. input to layer normalization, at input gate.
406 // 27: The scale of the intermediate result of matmul, i.e. input to layer normalization, at forget gate.
407 // 28: The scale of the intermediate result of matmul, i.e. input to layer normalization, at cell gate.
408 // 29: The scale of the intermediate result of matmul, i.e. input to layer normalization, at output gate.
409 // 30: The zero point of the hidden state, i.e. input to projection.
410 // 31: The scale of the hidden state, i.e. input to projection.
411 float cellClip, projClip, matMulInputGate, matMulForgetGate, matMulCellGate, matMulOutputGate, projInputScale;
412 int projInputZeroPoint;
413
414 if (!GetInputScalar<HalPolicy>(operation, 24, HalOperandType::FLOAT32, cellClip, model, data, true) ||
415 !GetInputScalar<HalPolicy>(operation, 25, HalOperandType::FLOAT32, projClip, model, data, true) ||
416 !GetInputScalar<HalPolicy>(operation, 26, HalOperandType::FLOAT32, matMulInputGate, model, data) ||
417 !GetInputScalar<HalPolicy>(operation, 27, HalOperandType::FLOAT32, matMulForgetGate, model, data) ||
418 !GetInputScalar<HalPolicy>(operation, 28, HalOperandType::FLOAT32, matMulCellGate, model, data) ||
419 !GetInputScalar<HalPolicy>(operation, 29, HalOperandType::FLOAT32, matMulOutputGate, model, data) ||
Sadik Armagan24af8b22020-05-22 08:34:16 +0100420 !GetInputScalar<HalPolicy>(operation, 30, HalOperandType::INT32, projInputZeroPoint, model, data) ||
421 !GetInputScalar<HalPolicy>(operation, 31, HalOperandType::FLOAT32, projInputScale, model, data))
Sadik Armagan813f2302020-05-19 14:10:30 +0100422 {
423 return Fail("%s: Operation has invalid scalar inputs", __func__);
424 }
425
426 // Outputs:
427 // 0: The output state (out): A 2-D tensor of ANEURALNETWORKS_TENSOR_QUANT8_ASYMM_SIGNED, of shape [batch_size,
428 // output_size].
429 const HalOperand* outputStateOut = GetOutputOperand<HalPolicy>(operation, 0, model);
430 if (!outputStateOut)
431 {
432 return Fail("%s: Could not read output 0: outputStateOut", __func__);
433 }
434
435 // 1: The cell state (out): A 2-D tensor of ANEURALNETWORKS_TENSOR_QUANT16_SYMM, of shape [batch_size, num_units].
436 const HalOperand* cellStateOut = GetOutputOperand<HalPolicy>(operation, 1, model);
437 if (!cellStateOut)
438 {
439 return Fail("%s: Could not read output 1: cellStateOut", __func__);
440 }
441
442 // 2: The output: A 2-D tensor of ANEURALNETWORKS_TENSOR_QUANT8_ASYMM_SIGNED, of shape [batch_size, output_size].
443 // This is effectively the same as the current “output state (out)” value.
444 const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 2, model);
445 if (!output)
446 {
447 return Fail("%s: Could not read output 2: output", __func__);
448 }
449
450 // set the params structure for the AddLstmLayer call
451 LstmInputParams params;
452 params.m_InputToInputWeights = inputToInputWeightsPin.GetConstTensorPtr();
453 params.m_InputToForgetWeights = inputToForgetWeightsPin.GetConstTensorPtr();
454 params.m_InputToCellWeights = inputToCellWeightsPin.GetConstTensorPtr();
455 params.m_InputToOutputWeights = inputToOutputWeightsPin.GetConstTensorPtr();
456 params.m_RecurrentToInputWeights = recurrentToInputWeightsPin.GetConstTensorPtr();
457 params.m_RecurrentToForgetWeights = recurrentToForgetWeightsPin.GetConstTensorPtr();
458 params.m_RecurrentToCellWeights = recurrentToCellWeightsPin.GetConstTensorPtr();
459 params.m_RecurrentToOutputWeights = recurrentToOutputWeightsPin.GetConstTensorPtr();
460 params.m_CellToInputWeights = cellToInputWeightsPin.GetConstTensorPtr();
461 params.m_CellToForgetWeights = cellToForgetWeightsPin.GetConstTensorPtr();
462 params.m_CellToOutputWeights = cellToOutputWeightsPin.GetConstTensorPtr();
463 params.m_InputGateBias = inputGateBiasPin.GetConstTensorPtr();
464 params.m_ForgetGateBias = forgetGateBiasPin.GetConstTensorPtr();
465 params.m_CellBias = cellBiasPin.GetConstTensorPtr();
466 params.m_OutputGateBias = outputGateBiasPin.GetConstTensorPtr();
467 params.m_ProjectionWeights = projectionWeightsPin.GetConstTensorPtr();
468 params.m_ProjectionBias = projectionBiasPin.GetConstTensorPtr();
469 params.m_InputLayerNormWeights = inputLayerNormWeightsPin.GetConstTensorPtr();
470 params.m_ForgetLayerNormWeights = forgetLayerNormWeightsPin.GetConstTensorPtr();
471 params.m_CellLayerNormWeights = cellLayerNormWeightsPin.GetConstTensorPtr();
472 params.m_OutputLayerNormWeights = outputLayerNormWeightsPin.GetConstTensorPtr();
473
474 // set the layer descriptor
475 QLstmDescriptor desc;
476 desc.m_CellClip = cellClip;
477 desc.m_ProjectionClip = projClip;
478 desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr ||
479 params.m_RecurrentToInputWeights == nullptr ||
480 params.m_InputGateBias == nullptr);
481 desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr ||
482 params.m_CellToOutputWeights != nullptr);
483 desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr);
484 desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr ||
485 params.m_ForgetLayerNormWeights != nullptr ||
486 params.m_CellLayerNormWeights != nullptr ||
487 params.m_OutputLayerNormWeights != nullptr);
488 desc.m_InputIntermediateScale = matMulInputGate;
489 desc.m_ForgetIntermediateScale = matMulForgetGate;
490 desc.m_CellIntermediateScale = matMulCellGate;
491 desc.m_OutputIntermediateScale = matMulOutputGate;
492 desc.m_HiddenStateScale = projInputScale;
493 desc.m_HiddenStateZeroPoint = projInputZeroPoint;
494
495 // validate the optional input groups
496 if (desc.m_CifgEnabled &&
497 (params.m_InputToInputWeights != nullptr ||
498 params.m_RecurrentToInputWeights != nullptr ||
499 params.m_InputGateBias != nullptr))
500 {
501 return Fail("%s: All, or none, of input-to-input weights, recurrent-to-input weights,"
502 " and input gate bias must be provided", __func__);
503 }
504
505 if (!desc.m_ProjectionEnabled && params.m_ProjectionBias != nullptr)
506 {
507 return Fail("%s: projection bias should not be provided without projection weights", __func__);
508 }
509
510 if (desc.m_PeepholeEnabled &&
511 (params.m_CellToForgetWeights == nullptr ||
512 params.m_CellToOutputWeights == nullptr ||
513 (!desc.m_CifgEnabled && params.m_CellToInputWeights == nullptr)))
514 {
515 return Fail("%s: All, or none, of cell-to-forget weights and cell-to-output weights must be provided"
516 " and, if CIFG is not enabled, cell-to-input weights must also be provided", __func__);
517 }
518
519 if (desc.m_LayerNormEnabled &&
520 (params.m_ForgetLayerNormWeights == nullptr ||
521 params.m_CellLayerNormWeights == nullptr ||
522 params.m_OutputLayerNormWeights == nullptr ||
523 (!desc.m_CifgEnabled && params.m_InputLayerNormWeights == nullptr)))
524 {
525 return Fail("%s: All, or none, of forget-norm weights, cell-norm weights and output-norm weights must be"
526 " provided and, if CIFG is not enabled, input-norm weights must also be provided", __func__);
527 }
528
529
530 // Basic parameters
531 LstmInputParamsInfo paramsInfo;
532 paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
533 paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
534 paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
535 paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
536 paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
537 paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
538 paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
539 paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
540 paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
541
542 // Inputs
543 const TensorInfo& inputInfo = input.GetTensorInfo();
544 const TensorInfo& outputStatePrevTimeStepInfo = outputStatePrevTimeStep.GetTensorInfo();
545 const TensorInfo& cellStatePrevTimeStepInfo = cellStatePrevTimeStep.GetTensorInfo();
546
547 // Outputs
548 TensorInfo outputStateOutInfo = GetTensorInfoForOperand(*outputStateOut);
549 TensorInfo outputInfo = GetTensorInfoForOperand(*output);
550 const TensorInfo& cellStateOutInfo = GetTensorInfoForOperand(*cellStateOut);
551
552 // Optional parameters
553 if (!desc.m_CifgEnabled)
554 {
555 paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
556 paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
557 if (desc.m_PeepholeEnabled)
558 {
559 paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo());
560 }
561 paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
562 }
563
564
565 if (desc.m_ProjectionEnabled)
566 {
567 paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo());
568 if (params.m_ProjectionBias != nullptr)
569 {
570 paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo());
571 }
572 }
573 else
574 {
575 // If Projection is disabled, override non-const outputs to change the quant info with hidden params, then
576 // create a new const TensorInfo based on this
577 outputStateOutInfo.SetQuantizationScale(projInputScale);
578 outputStateOutInfo.SetQuantizationOffset(projInputZeroPoint);
579 outputInfo.SetQuantizationScale(projInputScale);
580 outputInfo.SetQuantizationOffset(projInputZeroPoint);
581 }
582
583 const TensorInfo constOutputStateOutInfo(outputStateOutInfo);
584 const TensorInfo constOutputInfo(outputInfo);
585
586 if (desc.m_PeepholeEnabled)
587 {
588 paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo());
589 paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo());
590 }
591
592 if (desc.m_LayerNormEnabled)
593 {
594 if(!desc.m_CifgEnabled)
595 {
596 paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo());
597 }
598 paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo());
599 paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo());
600 paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo());
601 }
602
603 // Check if the layer is supported
604
605 if (IsDynamicTensor(constOutputStateOutInfo) ||
606 IsDynamicTensor(cellStateOutInfo) ||
607 IsDynamicTensor(constOutputInfo))
608 {
609 return Fail("%s: Dynamic output tensors are not supported %d %d %d %d", __func__,
610 IsDynamicTensor(constOutputStateOutInfo), IsDynamicTensor(cellStateOutInfo),
611 IsDynamicTensor(constOutputInfo));
612 }
613
614 bool isSupported = false;
615 FORWARD_LAYER_SUPPORT_FUNC(__func__,
616 IsQLstmSupported,
617 data.m_Backends,
618 isSupported,
619 inputInfo,
620 outputStatePrevTimeStepInfo,
621 cellStatePrevTimeStepInfo,
622 constOutputStateOutInfo,
623 cellStateOutInfo,
624 constOutputInfo,
625 desc,
626 paramsInfo);
627 if (!isSupported)
628 {
629 return false;
630 }
631
632 // Add the layer
633 IConnectableLayer* layer = data.m_Network->AddQLstmLayer(desc, params, "QLstm");
634
635 input.Connect(layer->GetInputSlot(0));
636 outputStatePrevTimeStep.Connect(layer->GetInputSlot(1));
637 cellStatePrevTimeStep.Connect(layer->GetInputSlot(2));
638
639 return ( SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, 0, model, data,
640 &constOutputStateOutInfo) &&
641 SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 1, *layer, 1, model, data) &&
642 SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 2, *layer, 2, model, data, &constOutputInfo));
643}
644
Finn Williamsfc884b42020-06-11 17:35:44 +0100645template<typename HalPolicy,
646 typename HalOperation = typename HalPolicy::Operation,
647 typename HalModel = typename HalPolicy::Model>
648bool ConvertRank(const HalOperation& operation, const HalModel& model, ConversionData& data)
649{
650 using HalOperand = typename HalPolicy::Operand;
651
652 const HalOperand* inputOperand = GetInputOperand<HalPolicy>(operation, 0, model);
653 const HalOperand* outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model);
654
655 if (inputOperand == nullptr || outputOperand == nullptr)
656 {
657 return Fail("%s: Operation has invalid inputs", __func__);
658 }
659
660 const Shape inputOperandShape = GetOperandShape(*inputOperand);
661 const Shape outputOperandShape = GetOperandShape(*outputOperand);
662
663 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
664 if (!input.IsValid())
665 {
666 return Fail("%s: Could not read input 0", __func__);
667 }
668
669 armnn::TensorInfo outInfo = GetTensorInfoForOperand(*outputOperand);
670
671 bool isSupported = false;
672 FORWARD_LAYER_SUPPORT_FUNC(__func__,
673 IsRankSupported,
674 data.m_Backends,
675 isSupported,
676 input.GetTensorInfo(),
677 outInfo);
678 if (!isSupported)
679 {
680 return false;
681 }
682
683 armnn::IConnectableLayer* layer = data.m_Network->AddRankLayer();
684 assert(layer != nullptr);
685 input.Connect(layer->GetInputSlot(0));
686
687 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, &outInfo);
688}
689
690} // armnn_driver namespace