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 | #ifndef ASR_WAV2LETTER_POSTPROCESS_HPP |
| 18 | #define ASR_WAV2LETTER_POSTPROCESS_HPP |
| 19 | |
| 20 | #include "TensorFlowLiteMicro.hpp" /* TensorFlow headers. */ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 21 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 22 | |
| 23 | namespace arm { |
| 24 | namespace app { |
| 25 | namespace audio { |
| 26 | namespace asr { |
| 27 | |
| 28 | /** |
| 29 | * @brief Helper class to manage tensor post-processing for "wav2letter" |
| 30 | * output. |
| 31 | */ |
| 32 | class Postprocess { |
| 33 | public: |
| 34 | /** |
| 35 | * @brief Constructor |
| 36 | * @param[in] contextLen Left and right context length for |
| 37 | * output tensor. |
| 38 | * @param[in] innerLen This is the length of the section |
| 39 | * between left and right context. |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 40 | * @param[in] blankTokenIdx Blank token index. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 41 | **/ |
| 42 | Postprocess(uint32_t contextLen, |
| 43 | uint32_t innerLen, |
| 44 | uint32_t blankTokenIdx); |
| 45 | |
| 46 | Postprocess() = delete; |
| 47 | ~Postprocess() = default; |
| 48 | |
| 49 | /** |
| 50 | * @brief Erases the required part of the tensor based |
| 51 | * on context lengths set up during initialisation. |
| 52 | * @param[in] tensor Pointer to the tensor. |
| 53 | * @param[in] axisIdx Index of the axis on which erase is |
| 54 | * performed. |
| 55 | * @param[in] lastIteration Flag to signal this is the |
| 56 | * last iteration in which case |
| 57 | * the right context is preserved. |
| 58 | * @return true if successful, false otherwise. |
| 59 | */ |
| 60 | bool Invoke(TfLiteTensor* tensor, |
| 61 | uint32_t axisIdx, |
| 62 | bool lastIteration = false); |
| 63 | |
| 64 | private: |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 65 | uint32_t m_contextLen; /* lengths of left and right contexts. */ |
| 66 | uint32_t m_innerLen; /* Length of inner context. */ |
| 67 | uint32_t m_totalLen; /* Total length of the required axis. */ |
| 68 | uint32_t m_countIterations; /* Current number of iterations. */ |
| 69 | uint32_t m_blankTokenIdx; /* Index of the labels blank token. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 70 | /** |
| 71 | * @brief Checks if the tensor and axis index are valid |
| 72 | * inputs to the object - based on how it has been |
| 73 | * initialised. |
| 74 | * @return true if valid, false otherwise. |
| 75 | */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 76 | bool IsInputValid(TfLiteTensor* tensor, |
| 77 | const uint32_t axisIdx) const; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * @brief Gets the tensor data element size in bytes based |
| 81 | * on the tensor type. |
| 82 | * @return Size in bytes, 0 if not supported. |
| 83 | */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 84 | static uint32_t GetTensorElementSize(TfLiteTensor* tensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * @brief Erases sections from the data assuming row-wise |
| 88 | * arrangement along the context axis. |
| 89 | * @return true if successful, false otherwise. |
| 90 | */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 91 | bool EraseSectionsRowWise(uint8_t* ptrData, |
| 92 | const uint32_t strideSzBytes, |
| 93 | const bool lastIteration); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * @brief Erases sections from the data assuming col-wise |
| 97 | * arrangement along the context axis. |
| 98 | * @return true if successful, false otherwise. |
| 99 | */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 100 | static bool EraseSectionsColWise(const uint8_t* ptrData, |
| 101 | const uint32_t strideSzBytes, |
| 102 | const bool lastIteration); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } /* namespace asr */ |
| 106 | } /* namespace audio */ |
| 107 | } /* namespace app */ |
| 108 | } /* namespace arm */ |
| 109 | |
| 110 | #endif /* ASR_WAV2LETTER_POSTPROCESS_HPP */ |