blob: 3a9d401829e572426cb4a1c09c01531ded13290d [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 */
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.
40 **/
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:
64 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. */
69 /**
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 */
75 bool _IsInputValid(TfLiteTensor* tensor,
76 uint32_t axisIdx) const;
77
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 */
83 uint32_t _GetTensorElementSize(TfLiteTensor* tensor);
84
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 */
90 bool _EraseSectionsRowWise(uint8_t* ptrData,
91 uint32_t strideSzBytes,
92 bool lastIteration);
93
94 };
95
96} /* namespace asr */
97} /* namespace audio */
98} /* namespace app */
99} /* namespace arm */
100
101#endif /* KWS_ASR_WAV2LET_POSTPROC_HPP */