alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 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 "Wav2LetterPostprocess.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 18 | #include "Wav2LetterModel.hpp" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame^] | 19 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 20 | |
| 21 | namespace arm { |
| 22 | namespace app { |
| 23 | namespace audio { |
| 24 | namespace asr { |
| 25 | |
| 26 | Postprocess::Postprocess(const uint32_t contextLen, |
| 27 | const uint32_t innerLen, |
| 28 | const uint32_t blankTokenIdx) |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 29 | : m_contextLen(contextLen), |
| 30 | m_innerLen(innerLen), |
| 31 | m_totalLen(2 * this->m_contextLen + this->m_innerLen), |
| 32 | m_countIterations(0), |
| 33 | m_blankTokenIdx(blankTokenIdx) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 34 | {} |
| 35 | |
| 36 | bool Postprocess::Invoke(TfLiteTensor* tensor, |
| 37 | const uint32_t axisIdx, |
| 38 | const bool lastIteration) |
| 39 | { |
| 40 | /* Basic checks. */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 41 | if (!this->IsInputValid(tensor, axisIdx)) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 42 | return false; |
| 43 | } |
| 44 | |
| 45 | /* Irrespective of tensor type, we use unsigned "byte" */ |
| 46 | uint8_t* ptrData = tflite::GetTensorData<uint8_t>(tensor); |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 47 | const uint32_t elemSz = this->GetTensorElementSize(tensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 48 | |
| 49 | /* Other sanity checks. */ |
| 50 | if (0 == elemSz) { |
| 51 | printf_err("Tensor type not supported for post processing\n"); |
| 52 | return false; |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 53 | } else if (elemSz * this->m_totalLen > tensor->bytes) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 54 | printf_err("Insufficient number of tensor bytes\n"); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | /* Which axis do we need to process? */ |
| 59 | switch (axisIdx) { |
| 60 | case arm::app::Wav2LetterModel::ms_outputRowsIdx: |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 61 | return this->EraseSectionsRowWise(ptrData, |
| 62 | elemSz * |
| 63 | tensor->dims->data[arm::app::Wav2LetterModel::ms_outputColsIdx], |
| 64 | lastIteration); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 65 | case arm::app::Wav2LetterModel::ms_outputColsIdx: |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 66 | return this->EraseSectionsColWise(ptrData, |
| 67 | elemSz * |
| 68 | tensor->dims->data[arm::app::Wav2LetterModel::ms_outputRowsIdx], |
| 69 | lastIteration); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 70 | default: |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 71 | printf_err("Unsupported axis index: %" PRIu32 "\n", axisIdx); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 77 | bool Postprocess::IsInputValid(TfLiteTensor* tensor, |
| 78 | const uint32_t axisIdx) const |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 79 | { |
| 80 | if (nullptr == tensor) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | if (static_cast<int>(axisIdx) >= tensor->dims->size) { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 85 | printf_err("Invalid axis index: %" PRIu32 "; Max: %d\n", |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 86 | axisIdx, tensor->dims->size); |
| 87 | return false; |
| 88 | } |
| 89 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 90 | if (static_cast<int>(this->m_totalLen) != |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 91 | tensor->dims->data[axisIdx]) { |
| 92 | printf_err("Unexpected tensor dimension for axis %d, \n", |
| 93 | tensor->dims->data[axisIdx]); |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 100 | uint32_t Postprocess::GetTensorElementSize(TfLiteTensor* tensor) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 101 | { |
| 102 | switch(tensor->type) { |
| 103 | case kTfLiteUInt8: |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 104 | case kTfLiteInt8: |
| 105 | return 1; |
| 106 | case kTfLiteInt16: |
| 107 | return 2; |
| 108 | case kTfLiteInt32: |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 109 | case kTfLiteFloat32: |
| 110 | return 4; |
| 111 | default: |
| 112 | printf_err("Unsupported tensor type %s\n", |
| 113 | TfLiteTypeGetName(tensor->type)); |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 119 | bool Postprocess::EraseSectionsRowWise( |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 120 | uint8_t* ptrData, |
| 121 | const uint32_t strideSzBytes, |
| 122 | const bool lastIteration) |
| 123 | { |
| 124 | /* In this case, the "zero-ing" is quite simple as the region |
| 125 | * to be zeroed sits in contiguous memory (row-major). */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 126 | const uint32_t eraseLen = strideSzBytes * this->m_contextLen; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 127 | |
| 128 | /* Erase left context? */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 129 | if (this->m_countIterations > 0) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 130 | /* Set output of each classification window to the blank token. */ |
| 131 | std::memset(ptrData, 0, eraseLen); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 132 | for (size_t windowIdx = 0; windowIdx < this->m_contextLen; windowIdx++) { |
| 133 | ptrData[windowIdx*strideSzBytes + this->m_blankTokenIdx] = 1; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | /* Erase right context? */ |
| 138 | if (false == lastIteration) { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 139 | uint8_t * rightCtxPtr = ptrData + (strideSzBytes * (this->m_contextLen + this->m_innerLen)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 140 | /* Set output of each classification window to the blank token. */ |
| 141 | std::memset(rightCtxPtr, 0, eraseLen); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 142 | for (size_t windowIdx = 0; windowIdx < this->m_contextLen; windowIdx++) { |
| 143 | rightCtxPtr[windowIdx*strideSzBytes + this->m_blankTokenIdx] = 1; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | if (lastIteration) { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 148 | this->m_countIterations = 0; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 149 | } else { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 150 | ++this->m_countIterations; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 156 | bool Postprocess::EraseSectionsColWise( |
| 157 | const uint8_t* ptrData, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 158 | const uint32_t strideSzBytes, |
| 159 | const bool lastIteration) |
| 160 | { |
| 161 | /* Not implemented. */ |
| 162 | UNUSED(ptrData); |
| 163 | UNUSED(strideSzBytes); |
| 164 | UNUSED(lastIteration); |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | } /* namespace asr */ |
| 169 | } /* namespace audio */ |
| 170 | } /* namespace app */ |
| 171 | } /* namespace arm */ |