blob: e343b66d526f2e7f8cb16367b1db0759ed9f1286 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burton4e002792022-05-04 09:45:02 +01002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * 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"
18#include "Wav2LetterModel.hpp"
Richard Burton4e002792022-05-04 09:45:02 +010019#include "ClassificationResult.hpp"
alexander3c798932021-03-26 21:42:19 +000020
21#include <algorithm>
22#include <catch.hpp>
23#include <limits>
24
25template <typename T>
Richard Burton4e002792022-05-04 09:45:02 +010026static TfLiteTensor GetTestTensor(
27 std::vector<int>& shape,
28 T initVal,
29 std::vector<T>& vectorBuf)
alexander3c798932021-03-26 21:42:19 +000030{
31 REQUIRE(0 != shape.size());
32
33 shape.insert(shape.begin(), shape.size());
34 uint32_t sizeInBytes = sizeof(T);
35 for (size_t i = 1; i < shape.size(); ++i) {
36 sizeInBytes *= shape[i];
37 }
38
39 /* Allocate mem. */
40 vectorBuf = std::vector<T>(sizeInBytes, initVal);
41 TfLiteIntArray* dims = tflite::testing::IntArrayFromInts(shape.data());
42 return tflite::testing::CreateQuantizedTensor(
Richard Burton4e002792022-05-04 09:45:02 +010043 vectorBuf.data(), dims,
44 1, 0, "test-tensor");
alexander3c798932021-03-26 21:42:19 +000045}
46
47TEST_CASE("Checking return value")
48{
49 SECTION("Mismatched post processing parameters and tensor size")
50 {
Richard Burton4e002792022-05-04 09:45:02 +010051 const uint32_t outputCtxLen = 5;
52 arm::app::AsrClassifier classifier;
53 arm::app::Wav2LetterModel model;
54 model.Init();
55 std::vector<std::string> dummyLabels = {"a", "b", "$"};
56 const uint32_t blankTokenIdx = 2;
57 std::vector<arm::app::ClassificationResult> dummyResult;
alexander3c798932021-03-26 21:42:19 +000058 std::vector <int> tensorShape = {1, 1, 1, 13};
59 std::vector <int8_t> tensorVec;
60 TfLiteTensor tensor = GetTestTensor<int8_t>(
Richard Burton4e002792022-05-04 09:45:02 +010061 tensorShape, 100, tensorVec);
62
63 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
64 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
65
66 REQUIRE(!post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +000067 }
68
69 SECTION("Post processing succeeds")
70 {
Richard Burton4e002792022-05-04 09:45:02 +010071 const uint32_t outputCtxLen = 5;
72 arm::app::AsrClassifier classifier;
73 arm::app::Wav2LetterModel model;
74 model.Init();
75 std::vector<std::string> dummyLabels = {"a", "b", "$"};
76 const uint32_t blankTokenIdx = 2;
77 std::vector<arm::app::ClassificationResult> dummyResult;
78 std::vector<int> tensorShape = {1, 1, 13, 1};
79 std::vector<int8_t> tensorVec;
alexander3c798932021-03-26 21:42:19 +000080 TfLiteTensor tensor = GetTestTensor<int8_t>(
Richard Burton4e002792022-05-04 09:45:02 +010081 tensorShape, 100, tensorVec);
82
83 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
84 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +000085
86 /* Copy elements to compare later. */
Richard Burton4e002792022-05-04 09:45:02 +010087 std::vector<int8_t> originalVec = tensorVec;
alexander3c798932021-03-26 21:42:19 +000088
89 /* This step should not erase anything. */
Richard Burton4e002792022-05-04 09:45:02 +010090 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +000091 }
92}
93
Richard Burton4e002792022-05-04 09:45:02 +010094
alexander3c798932021-03-26 21:42:19 +000095TEST_CASE("Postprocessing - erasing required elements")
96{
Richard Burton4e002792022-05-04 09:45:02 +010097 constexpr uint32_t outputCtxLen = 5;
alexander3c798932021-03-26 21:42:19 +000098 constexpr uint32_t innerLen = 3;
Richard Burton4e002792022-05-04 09:45:02 +010099 constexpr uint32_t nRows = 2*outputCtxLen + innerLen;
alexander3c798932021-03-26 21:42:19 +0000100 constexpr uint32_t nCols = 10;
101 constexpr uint32_t blankTokenIdx = nCols - 1;
Richard Burton4e002792022-05-04 09:45:02 +0100102 std::vector<int> tensorShape = {1, 1, nRows, nCols};
103 arm::app::AsrClassifier classifier;
104 arm::app::Wav2LetterModel model;
105 model.Init();
106 std::vector<std::string> dummyLabels = {"a", "b", "$"};
107 std::vector<arm::app::ClassificationResult> dummyResult;
alexander3c798932021-03-26 21:42:19 +0000108
109 SECTION("First and last iteration")
110 {
Richard Burton4e002792022-05-04 09:45:02 +0100111 std::vector<int8_t> tensorVec;
112 TfLiteTensor tensor = GetTestTensor<int8_t>(tensorShape, 100, tensorVec);
113 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
114 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +0000115
116 /* Copy elements to compare later. */
Richard Burton4e002792022-05-04 09:45:02 +0100117 std::vector<int8_t>originalVec = tensorVec;
alexander3c798932021-03-26 21:42:19 +0000118
119 /* This step should not erase anything. */
Richard Burton4e002792022-05-04 09:45:02 +0100120 post.m_lastIteration = true;
121 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +0000122 REQUIRE(originalVec == tensorVec);
123 }
124
125 SECTION("Right context erase")
126 {
alexander3c798932021-03-26 21:42:19 +0000127 std::vector <int8_t> tensorVec;
128 TfLiteTensor tensor = GetTestTensor<int8_t>(
Richard Burton4e002792022-05-04 09:45:02 +0100129 tensorShape, 100, tensorVec);
130 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
131 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +0000132
133 /* Copy elements to compare later. */
Richard Burton4e002792022-05-04 09:45:02 +0100134 std::vector<int8_t> originalVec = tensorVec;
alexander3c798932021-03-26 21:42:19 +0000135
136 /* This step should erase the right context only. */
Richard Burton4e002792022-05-04 09:45:02 +0100137 post.m_lastIteration = false;
138 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +0000139 REQUIRE(originalVec != tensorVec);
140
141 /* The last ctxLen * 10 elements should be gone. */
Richard Burton4e002792022-05-04 09:45:02 +0100142 for (size_t i = 0; i < outputCtxLen; ++i) {
alexander3c798932021-03-26 21:42:19 +0000143 for (size_t j = 0; j < nCols; ++j) {
Richard Burton4e002792022-05-04 09:45:02 +0100144 /* Check right context elements are zeroed. Blank token idx should be set to 1 when erasing. */
alexander3c798932021-03-26 21:42:19 +0000145 if (j == blankTokenIdx) {
Richard Burton4e002792022-05-04 09:45:02 +0100146 CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 1);
alexander3c798932021-03-26 21:42:19 +0000147 } else {
Richard Burton4e002792022-05-04 09:45:02 +0100148 CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 0);
alexander3c798932021-03-26 21:42:19 +0000149 }
150
151 /* Check left context is preserved. */
152 CHECK(tensorVec[i*nCols + j] == originalVec[i*nCols + j]);
153 }
154 }
155
156 /* Check inner elements are preserved. */
Richard Burton4e002792022-05-04 09:45:02 +0100157 for (size_t i = outputCtxLen * nCols; i < (outputCtxLen + innerLen) * nCols; ++i) {
alexander3c798932021-03-26 21:42:19 +0000158 CHECK(tensorVec[i] == originalVec[i]);
159 }
160 }
161
162 SECTION("Left and right context erase")
163 {
alexander3c798932021-03-26 21:42:19 +0000164 std::vector <int8_t> tensorVec;
Richard Burton4e002792022-05-04 09:45:02 +0100165 TfLiteTensor tensor = GetTestTensor<int8_t>(
166 tensorShape, 100, tensorVec);
167 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
168 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +0000169
170 /* Copy elements to compare later. */
171 std::vector <int8_t> originalVec = tensorVec;
172
173 /* This step should erase right context. */
Richard Burton4e002792022-05-04 09:45:02 +0100174 post.m_lastIteration = false;
175 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +0000176
177 /* Calling it the second time should erase the left context. */
Richard Burton4e002792022-05-04 09:45:02 +0100178 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +0000179
180 REQUIRE(originalVec != tensorVec);
181
182 /* The first and last ctxLen * 10 elements should be gone. */
Richard Burton4e002792022-05-04 09:45:02 +0100183 for (size_t i = 0; i < outputCtxLen; ++i) {
alexander3c798932021-03-26 21:42:19 +0000184 for (size_t j = 0; j < nCols; ++j) {
185 /* Check left and right context elements are zeroed. */
186 if (j == blankTokenIdx) {
Richard Burton4e002792022-05-04 09:45:02 +0100187 CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 1);
188 CHECK(tensorVec[i*nCols + j] == 1);
alexander3c798932021-03-26 21:42:19 +0000189 } else {
Richard Burton4e002792022-05-04 09:45:02 +0100190 CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 0);
191 CHECK(tensorVec[i*nCols + j] == 0);
alexander3c798932021-03-26 21:42:19 +0000192 }
193 }
194 }
195
196 /* Check inner elements are preserved. */
Richard Burton4e002792022-05-04 09:45:02 +0100197 for (size_t i = outputCtxLen * nCols; i < (outputCtxLen + innerLen) * nCols; ++i) {
alexander3c798932021-03-26 21:42:19 +0000198 /* Check left context is preserved. */
199 CHECK(tensorVec[i] == originalVec[i]);
200 }
201 }
202
203 SECTION("Try left context erase")
204 {
alexander3c798932021-03-26 21:42:19 +0000205 std::vector <int8_t> tensorVec;
206 TfLiteTensor tensor = GetTestTensor<int8_t>(
Richard Burton4e002792022-05-04 09:45:02 +0100207 tensorShape, 100, tensorVec);
208
209 /* Should not be able to erase the left context if it is the first iteration. */
210 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
211 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +0000212
213 /* Copy elements to compare later. */
214 std::vector <int8_t> originalVec = tensorVec;
215
216 /* Calling it the second time should erase the left context. */
Richard Burton4e002792022-05-04 09:45:02 +0100217 post.m_lastIteration = true;
218 REQUIRE(post.DoPostProcess());
219
alexander3c798932021-03-26 21:42:19 +0000220 REQUIRE(originalVec == tensorVec);
221 }
Richard Burton4e002792022-05-04 09:45:02 +0100222}