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 | default: |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 66 | printf_err("Unsupported axis index: %" PRIu32 "\n", axisIdx); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 72 | bool Postprocess::IsInputValid(TfLiteTensor* tensor, |
| 73 | const uint32_t axisIdx) const |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 74 | { |
| 75 | if (nullptr == tensor) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | if (static_cast<int>(axisIdx) >= tensor->dims->size) { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 80 | printf_err("Invalid axis index: %" PRIu32 "; Max: %d\n", |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 81 | axisIdx, tensor->dims->size); |
| 82 | return false; |
| 83 | } |
| 84 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 85 | if (static_cast<int>(this->m_totalLen) != |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 86 | tensor->dims->data[axisIdx]) { |
| 87 | printf_err("Unexpected tensor dimension for axis %d, \n", |
| 88 | tensor->dims->data[axisIdx]); |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | return true; |
| 93 | } |
| 94 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 95 | uint32_t Postprocess::GetTensorElementSize(TfLiteTensor* tensor) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 96 | { |
| 97 | switch(tensor->type) { |
| 98 | case kTfLiteUInt8: |
| 99 | return 1; |
| 100 | case kTfLiteInt8: |
| 101 | return 1; |
| 102 | case kTfLiteInt16: |
| 103 | return 2; |
| 104 | case kTfLiteInt32: |
| 105 | return 4; |
| 106 | case kTfLiteFloat32: |
| 107 | return 4; |
| 108 | default: |
| 109 | printf_err("Unsupported tensor type %s\n", |
| 110 | TfLiteTypeGetName(tensor->type)); |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 116 | bool Postprocess::EraseSectionsRowWise( |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 117 | uint8_t* ptrData, |
| 118 | const uint32_t strideSzBytes, |
| 119 | const bool lastIteration) |
| 120 | { |
| 121 | /* In this case, the "zero-ing" is quite simple as the region |
| 122 | * to be zeroed sits in contiguous memory (row-major). */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 123 | const uint32_t eraseLen = strideSzBytes * this->m_contextLen; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 124 | |
| 125 | /* Erase left context? */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 126 | if (this->m_countIterations > 0) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 127 | /* Set output of each classification window to the blank token. */ |
| 128 | std::memset(ptrData, 0, eraseLen); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 129 | for (size_t windowIdx = 0; windowIdx < this->m_contextLen; windowIdx++) { |
| 130 | ptrData[windowIdx*strideSzBytes + this->m_blankTokenIdx] = 1; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | /* Erase right context? */ |
| 135 | if (false == lastIteration) { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 136 | uint8_t * rightCtxPtr = ptrData + (strideSzBytes * (this->m_contextLen + this->m_innerLen)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 137 | /* Set output of each classification window to the blank token. */ |
| 138 | std::memset(rightCtxPtr, 0, eraseLen); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 139 | for (size_t windowIdx = 0; windowIdx < this->m_contextLen; windowIdx++) { |
| 140 | rightCtxPtr[windowIdx*strideSzBytes + this->m_blankTokenIdx] = 1; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | if (lastIteration) { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 145 | this->m_countIterations = 0; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 146 | } else { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 147 | ++this->m_countIterations; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | } /* namespace asr */ |
| 154 | } /* namespace audio */ |
| 155 | } /* namespace app */ |
| 156 | } /* namespace arm */ |