blob: a744e0fbef4987e8d24255c1a2de9d724f6c2e24 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +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#ifndef ASR_WAV2LETTER_POSTPROCESS_HPP
18#define ASR_WAV2LETTER_POSTPROCESS_HPP
19
20#include "TensorFlowLiteMicro.hpp" /* TensorFlow headers. */
21#include "hal.h" /* stdout facility. */
22
23namespace arm {
24namespace app {
25namespace audio {
26namespace 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 Gottardi56ee6202021-05-12 08:27:15 +010040 * @param[in] blankTokenIdx Blank token index.
alexander3c798932021-03-26 21:42:19 +000041 **/
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 Gottardi56ee6202021-05-12 08:27:15 +010065 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. */
alexander3c798932021-03-26 21:42:19 +000070 /**
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 */
alexanderc350cdc2021-04-29 20:36:09 +010076 bool IsInputValid(TfLiteTensor* tensor,
77 const uint32_t axisIdx) const;
alexander3c798932021-03-26 21:42:19 +000078
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 */
alexanderc350cdc2021-04-29 20:36:09 +010084 static uint32_t GetTensorElementSize(TfLiteTensor* tensor);
alexander3c798932021-03-26 21:42:19 +000085
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 */
alexanderc350cdc2021-04-29 20:36:09 +010091 bool EraseSectionsRowWise(uint8_t* ptrData,
92 const uint32_t strideSzBytes,
93 const bool lastIteration);
alexander3c798932021-03-26 21:42:19 +000094
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 */
alexanderc350cdc2021-04-29 20:36:09 +0100100 static bool EraseSectionsColWise(const uint8_t* ptrData,
101 const uint32_t strideSzBytes,
102 const bool lastIteration);
alexander3c798932021-03-26 21:42:19 +0000103 };
104
105} /* namespace asr */
106} /* namespace audio */
107} /* namespace app */
108} /* namespace arm */
109
110#endif /* ASR_WAV2LETTER_POSTPROCESS_HPP */