blob: be0f369604fbb18340acaad20d80b6c107c70921 [file] [log] [blame]
Richard Burton00553462021-11-10 16:27:14 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#include "RNNoiseModel.hpp"
18
19#include "hal.h"
20
21const tflite::MicroOpResolver& arm::app::RNNoiseModel::GetOpResolver()
22{
23 return this->m_opResolver;
24}
25
26bool arm::app::RNNoiseModel::EnlistOperations()
27{
28 this->m_opResolver.AddUnpack();
29 this->m_opResolver.AddFullyConnected();
30 this->m_opResolver.AddSplit();
31 this->m_opResolver.AddSplitV();
32 this->m_opResolver.AddAdd();
33 this->m_opResolver.AddLogistic();
34 this->m_opResolver.AddMul();
35 this->m_opResolver.AddSub();
36 this->m_opResolver.AddTanh();
37 this->m_opResolver.AddPack();
38 this->m_opResolver.AddReshape();
39 this->m_opResolver.AddQuantize();
40 this->m_opResolver.AddConcatenation();
41 this->m_opResolver.AddRelu();
42
43#if defined(ARM_NPU)
44 if (kTfLiteOk == this->m_opResolver.AddEthosU()) {
45 info("Added %s support to op resolver\n",
46 tflite::GetString_ETHOSU());
47 } else {
48 printf_err("Failed to add Arm NPU support to op resolver.");
49 return false;
50 }
51#endif /* ARM_NPU */
52 return true;
53}
54
55extern uint8_t* GetModelPointer();
56const uint8_t* arm::app::RNNoiseModel::ModelPointer()
57{
58 return GetModelPointer();
59}
60
61extern size_t GetModelLen();
62size_t arm::app::RNNoiseModel::ModelSize()
63{
64 return GetModelLen();
65}
66
67bool arm::app::RNNoiseModel::RunInference()
68{
69 return Model::RunInference();
70}
71
72void arm::app::RNNoiseModel::ResetGruState()
73{
74 for (auto& stateMapping: this->m_gruStateMap) {
75 TfLiteTensor* inputGruStateTensor = this->GetInputTensor(stateMapping.second);
76 auto* inputGruState = tflite::GetTensorData<int8_t>(inputGruStateTensor);
77 /* Initial value of states is 0, but this is affected by quantization zero point. */
78 auto quantParams = arm::app::GetTensorQuantParams(inputGruStateTensor);
79 memset(inputGruState, quantParams.offset, inputGruStateTensor->bytes);
80 }
81}
82
83bool arm::app::RNNoiseModel::CopyGruStates()
84{
85 std::vector<std::pair<size_t, std::vector<int8_t>>> tempOutGruStates;
86 /* Saving output states before copying them to input states to avoid output states modification in the tensor.
87 * tflu shares input and output tensors memory, thus writing to input tensor can change output tensor values. */
88 for (auto& stateMapping: this->m_gruStateMap) {
89 TfLiteTensor* outputGruStateTensor = this->GetOutputTensor(stateMapping.first);
90 std::vector<int8_t> tempOutGruState(outputGruStateTensor->bytes);
91 auto* outGruState = tflite::GetTensorData<int8_t>(outputGruStateTensor);
92 memcpy(tempOutGruState.data(), outGruState, outputGruStateTensor->bytes);
93 /* Index of the input tensor and the data to copy. */
94 tempOutGruStates.emplace_back(stateMapping.second, std::move(tempOutGruState));
95 }
96 /* Updating input GRU states with saved GRU output states. */
97 for (auto& stateMapping: tempOutGruStates) {
98 auto outputGruStateTensorData = stateMapping.second;
99 TfLiteTensor* inputGruStateTensor = this->GetInputTensor(stateMapping.first);
100 if (outputGruStateTensorData.size() != inputGruStateTensor->bytes) {
101 printf_err("Unexpected number of bytes for GRU state mapping. Input = %zuz, output = %zuz.\n",
102 inputGruStateTensor->bytes,
103 outputGruStateTensorData.size());
104 return false;
105 }
106 auto* inputGruState = tflite::GetTensorData<int8_t>(inputGruStateTensor);
107 auto* outGruState = outputGruStateTensorData.data();
108 memcpy(inputGruState, outGruState, inputGruStateTensor->bytes);
109 }
110 return true;
111}