blob: 029a6410bc0e207689e1ed654cf58a1f3751f5d8 [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 KWS_ASR_WAV2LET_POSTPROC_HPP
18#define KWS_ASR_WAV2LET_POSTPROC_HPP
19
20#include "TensorFlowLiteMicro.hpp" /* TensorFlow headers */
alexander3c798932021-03-26 21:42:19 +000021
22namespace arm {
23namespace app {
24namespace audio {
25namespace asr {
26
27 /**
28 * @brief Helper class to manage tensor post-processing for "wav2letter"
29 * output.
30 */
31 class Postprocess {
32 public:
33 /**
34 * @brief Constructor
Isabella Gottardi56ee6202021-05-12 08:27:15 +010035 * @param[in] contextLen Left and right context length for
36 * output tensor.
37 * @param[in] innerLen This is the length of the section
38 * between left and right context.
39 * @param[in] blankTokenIdx Blank token index.
alexander3c798932021-03-26 21:42:19 +000040 **/
41 Postprocess(uint32_t contextLen,
42 uint32_t innerLen,
43 uint32_t blankTokenIdx);
44
45 Postprocess() = delete;
46 ~Postprocess() = default;
47
48 /**
49 * @brief Erases the required part of the tensor based
50 * on context lengths set up during initialisation
51 * @param[in] tensor Pointer to the tensor
52 * @param[in] axisIdx Index of the axis on which erase is
53 * performed.
54 * @param[in] lastIteration Flag to signal is this is the
55 * last iteration in which case
56 * the right context is preserved.
57 * @return true if successful, false otherwise.
58 */
59 bool Invoke(TfLiteTensor* tensor,
60 uint32_t axisIdx,
61 bool lastIteration = false);
62
63 private:
Isabella Gottardi56ee6202021-05-12 08:27:15 +010064 uint32_t m_contextLen; /* Lengths of left and right contexts. */
65 uint32_t m_innerLen; /* Length of inner context. */
66 uint32_t m_totalLen; /* Total length of the required axis. */
67 uint32_t m_countIterations; /* Current number of iterations. */
68 uint32_t m_blankTokenIdx; /* Index of the labels blank token. */
alexander3c798932021-03-26 21:42:19 +000069 /**
70 * @brief Checks if the tensor and axis index are valid
71 * inputs to the object - based on how it has been
72 * initialised.
73 * @return true if valid, false otherwise.
74 */
alexanderc350cdc2021-04-29 20:36:09 +010075 bool IsInputValid(TfLiteTensor* tensor,
76 const uint32_t axisIdx) const;
alexander3c798932021-03-26 21:42:19 +000077
78 /**
79 * @brief Gets the tensor data element size in bytes based
80 * on the tensor type.
81 * @return Size in bytes, 0 if not supported.
82 */
alexanderc350cdc2021-04-29 20:36:09 +010083 uint32_t GetTensorElementSize(TfLiteTensor* tensor);
alexander3c798932021-03-26 21:42:19 +000084
85 /**
86 * @brief Erases sections from the data assuming row-wise
87 * arrangement along the context axis.
88 * @return true if successful, false otherwise.
89 */
alexanderc350cdc2021-04-29 20:36:09 +010090 bool EraseSectionsRowWise(uint8_t* ptrData,
91 const uint32_t strideSzBytes,
92 const bool lastIteration);
alexander3c798932021-03-26 21:42:19 +000093
94 };
95
96} /* namespace asr */
97} /* namespace audio */
98} /* namespace app */
99} /* namespace arm */
100
101#endif /* KWS_ASR_WAV2LET_POSTPROC_HPP */