blob: 244fa1a0074f4eebedcec3f290cc8c8c82d9787d [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"
alexander31ae9f02022-02-10 16:15:54 +000018#include "log_macros.h"
Richard Burton00553462021-11-10 16:27:14 +000019
20const tflite::MicroOpResolver& arm::app::RNNoiseModel::GetOpResolver()
21{
22 return this->m_opResolver;
23}
24
25bool arm::app::RNNoiseModel::EnlistOperations()
26{
27 this->m_opResolver.AddUnpack();
28 this->m_opResolver.AddFullyConnected();
29 this->m_opResolver.AddSplit();
30 this->m_opResolver.AddSplitV();
31 this->m_opResolver.AddAdd();
32 this->m_opResolver.AddLogistic();
33 this->m_opResolver.AddMul();
34 this->m_opResolver.AddSub();
35 this->m_opResolver.AddTanh();
36 this->m_opResolver.AddPack();
37 this->m_opResolver.AddReshape();
38 this->m_opResolver.AddQuantize();
39 this->m_opResolver.AddConcatenation();
40 this->m_opResolver.AddRelu();
41
42#if defined(ARM_NPU)
43 if (kTfLiteOk == this->m_opResolver.AddEthosU()) {
44 info("Added %s support to op resolver\n",
45 tflite::GetString_ETHOSU());
46 } else {
47 printf_err("Failed to add Arm NPU support to op resolver.");
48 return false;
49 }
50#endif /* ARM_NPU */
51 return true;
52}
53
54extern uint8_t* GetModelPointer();
55const uint8_t* arm::app::RNNoiseModel::ModelPointer()
56{
57 return GetModelPointer();
58}
59
60extern size_t GetModelLen();
61size_t arm::app::RNNoiseModel::ModelSize()
62{
63 return GetModelLen();
64}
65
66bool arm::app::RNNoiseModel::RunInference()
67{
68 return Model::RunInference();
69}
70
71void arm::app::RNNoiseModel::ResetGruState()
72{
73 for (auto& stateMapping: this->m_gruStateMap) {
74 TfLiteTensor* inputGruStateTensor = this->GetInputTensor(stateMapping.second);
75 auto* inputGruState = tflite::GetTensorData<int8_t>(inputGruStateTensor);
76 /* Initial value of states is 0, but this is affected by quantization zero point. */
77 auto quantParams = arm::app::GetTensorQuantParams(inputGruStateTensor);
78 memset(inputGruState, quantParams.offset, inputGruStateTensor->bytes);
79 }
80}
81
82bool arm::app::RNNoiseModel::CopyGruStates()
83{
84 std::vector<std::pair<size_t, std::vector<int8_t>>> tempOutGruStates;
85 /* Saving output states before copying them to input states to avoid output states modification in the tensor.
86 * tflu shares input and output tensors memory, thus writing to input tensor can change output tensor values. */
87 for (auto& stateMapping: this->m_gruStateMap) {
88 TfLiteTensor* outputGruStateTensor = this->GetOutputTensor(stateMapping.first);
89 std::vector<int8_t> tempOutGruState(outputGruStateTensor->bytes);
90 auto* outGruState = tflite::GetTensorData<int8_t>(outputGruStateTensor);
91 memcpy(tempOutGruState.data(), outGruState, outputGruStateTensor->bytes);
92 /* Index of the input tensor and the data to copy. */
93 tempOutGruStates.emplace_back(stateMapping.second, std::move(tempOutGruState));
94 }
95 /* Updating input GRU states with saved GRU output states. */
96 for (auto& stateMapping: tempOutGruStates) {
97 auto outputGruStateTensorData = stateMapping.second;
98 TfLiteTensor* inputGruStateTensor = this->GetInputTensor(stateMapping.first);
99 if (outputGruStateTensorData.size() != inputGruStateTensor->bytes) {
100 printf_err("Unexpected number of bytes for GRU state mapping. Input = %zuz, output = %zuz.\n",
101 inputGruStateTensor->bytes,
102 outputGruStateTensorData.size());
103 return false;
104 }
105 auto* inputGruState = tflite::GetTensorData<int8_t>(inputGruStateTensor);
106 auto* outGruState = outputGruStateTensorData.data();
107 memcpy(inputGruState, outGruState, inputGruStateTensor->bytes);
108 }
109 return true;
110}