blob: 11c49199099ee07c8cba90e72a6c0b9ed851229d [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtonc2911442022-04-22 09:08:21 +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 Burtonc2911442022-04-22 09:08:21 +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>
26static TfLiteTensor GetTestTensor(
Richard Burtonb40ecf82022-04-22 16:14:57 +010027 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(
43 vectorBuf.data(), dims,
44 1, 0, "test-tensor");
45}
46
47TEST_CASE("Checking return value")
48{
49 SECTION("Mismatched post processing parameters and tensor size")
50 {
Richard Burtonc2911442022-04-22 09:08:21 +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 Burtonc2911442022-04-22 09:08:21 +010061 tensorShape, 100, tensorVec);
62
Richard Burtonb40ecf82022-04-22 16:14:57 +010063 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
Richard Burtonc2911442022-04-22 09:08:21 +010064 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 Burtonc2911442022-04-22 09:08:21 +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 Burtonc2911442022-04-22 09:08:21 +010081 tensorShape, 100, tensorVec);
82
Richard Burtonb40ecf82022-04-22 16:14:57 +010083 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
Richard Burtonc2911442022-04-22 09:08:21 +010084 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +000085
86 /* Copy elements to compare later. */
Richard Burtonc2911442022-04-22 09:08:21 +010087 std::vector<int8_t> originalVec = tensorVec;
alexander3c798932021-03-26 21:42:19 +000088
89 /* This step should not erase anything. */
Richard Burtonc2911442022-04-22 09:08:21 +010090 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +000091 }
92}
93
94
95TEST_CASE("Postprocessing - erasing required elements")
96{
Richard Burtonc2911442022-04-22 09:08:21 +010097 constexpr uint32_t outputCtxLen = 5;
alexander3c798932021-03-26 21:42:19 +000098 constexpr uint32_t innerLen = 3;
Richard Burtonc2911442022-04-22 09:08:21 +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 Burtonc2911442022-04-22 09:08:21 +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 Burtonc2911442022-04-22 09:08:21 +0100111 std::vector<int8_t> tensorVec;
112 TfLiteTensor tensor = GetTestTensor<int8_t>(tensorShape, 100, tensorVec);
Richard Burtonb40ecf82022-04-22 16:14:57 +0100113 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
Richard Burtonc2911442022-04-22 09:08:21 +0100114 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +0000115
116 /* Copy elements to compare later. */
Richard Burtonc2911442022-04-22 09:08:21 +0100117 std::vector<int8_t>originalVec = tensorVec;
alexander3c798932021-03-26 21:42:19 +0000118
119 /* This step should not erase anything. */
Richard Burtonc2911442022-04-22 09:08:21 +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 Burtonc2911442022-04-22 09:08:21 +0100129 tensorShape, 100, tensorVec);
Richard Burtonb40ecf82022-04-22 16:14:57 +0100130 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
Richard Burtonc2911442022-04-22 09:08:21 +0100131 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +0000132
133 /* Copy elements to compare later. */
Richard Burtonc2911442022-04-22 09:08:21 +0100134 std::vector<int8_t> originalVec = tensorVec;
alexander3c798932021-03-26 21:42:19 +0000135
Richard Burtonc2911442022-04-22 09:08:21 +0100136 //auto tensorData = tflite::GetTensorData<int8_t>(tensor);
alexander3c798932021-03-26 21:42:19 +0000137 /* This step should erase the right context only. */
Richard Burtonc2911442022-04-22 09:08:21 +0100138 post.m_lastIteration = false;
139 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +0000140 REQUIRE(originalVec != tensorVec);
141
142 /* The last ctxLen * 10 elements should be gone. */
Richard Burtonc2911442022-04-22 09:08:21 +0100143 for (size_t i = 0; i < outputCtxLen; ++i) {
alexander3c798932021-03-26 21:42:19 +0000144 for (size_t j = 0; j < nCols; ++j) {
Richard Burtonc2911442022-04-22 09:08:21 +0100145 /* Check right context elements are zeroed. Blank token idx should be set to 1 when erasing. */
alexander3c798932021-03-26 21:42:19 +0000146 if (j == blankTokenIdx) {
Richard Burtonc2911442022-04-22 09:08:21 +0100147 CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 1);
alexander3c798932021-03-26 21:42:19 +0000148 } else {
Richard Burtonc2911442022-04-22 09:08:21 +0100149 CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 0);
alexander3c798932021-03-26 21:42:19 +0000150 }
151
152 /* Check left context is preserved. */
153 CHECK(tensorVec[i*nCols + j] == originalVec[i*nCols + j]);
154 }
155 }
156
157 /* Check inner elements are preserved. */
Richard Burtonc2911442022-04-22 09:08:21 +0100158 for (size_t i = outputCtxLen * nCols; i < (outputCtxLen + innerLen) * nCols; ++i) {
alexander3c798932021-03-26 21:42:19 +0000159 CHECK(tensorVec[i] == originalVec[i]);
160 }
161 }
162
163 SECTION("Left and right context erase")
164 {
alexander3c798932021-03-26 21:42:19 +0000165 std::vector <int8_t> tensorVec;
166 TfLiteTensor tensor = GetTestTensor<int8_t>(
Richard Burtonc2911442022-04-22 09:08:21 +0100167 tensorShape, 100, tensorVec);
Richard Burtonb40ecf82022-04-22 16:14:57 +0100168 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
Richard Burtonc2911442022-04-22 09:08:21 +0100169 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +0000170
171 /* Copy elements to compare later. */
172 std::vector <int8_t> originalVec = tensorVec;
173
174 /* This step should erase right context. */
Richard Burtonc2911442022-04-22 09:08:21 +0100175 post.m_lastIteration = false;
176 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +0000177
178 /* Calling it the second time should erase the left context. */
Richard Burtonc2911442022-04-22 09:08:21 +0100179 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +0000180
181 REQUIRE(originalVec != tensorVec);
182
183 /* The first and last ctxLen * 10 elements should be gone. */
Richard Burtonc2911442022-04-22 09:08:21 +0100184 for (size_t i = 0; i < outputCtxLen; ++i) {
alexander3c798932021-03-26 21:42:19 +0000185 for (size_t j = 0; j < nCols; ++j) {
186 /* Check left and right context elements are zeroed. */
187 if (j == blankTokenIdx) {
Richard Burtonc2911442022-04-22 09:08:21 +0100188 CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 1);
alexander3c798932021-03-26 21:42:19 +0000189 CHECK(tensorVec[i*nCols + j] == 1);
190 } else {
Richard Burtonc2911442022-04-22 09:08:21 +0100191 CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 0);
alexander3c798932021-03-26 21:42:19 +0000192 CHECK(tensorVec[i*nCols + j] == 0);
193 }
194 }
195 }
196
197 /* Check inner elements are preserved. */
Richard Burtonc2911442022-04-22 09:08:21 +0100198 for (size_t i = outputCtxLen * nCols; i < (outputCtxLen + innerLen) * nCols; ++i) {
alexander3c798932021-03-26 21:42:19 +0000199 /* Check left context is preserved. */
200 CHECK(tensorVec[i] == originalVec[i]);
201 }
202 }
203
204 SECTION("Try left context erase")
205 {
alexander3c798932021-03-26 21:42:19 +0000206 std::vector <int8_t> tensorVec;
207 TfLiteTensor tensor = GetTestTensor<int8_t>(
Richard Burtonc2911442022-04-22 09:08:21 +0100208 tensorShape, 100, tensorVec);
209
210 /* Should not be able to erase the left context if it is the first iteration. */
Richard Burtonb40ecf82022-04-22 16:14:57 +0100211 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
Richard Burtonc2911442022-04-22 09:08:21 +0100212 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +0000213
214 /* Copy elements to compare later. */
215 std::vector <int8_t> originalVec = tensorVec;
216
217 /* Calling it the second time should erase the left context. */
Richard Burtonc2911442022-04-22 09:08:21 +0100218 post.m_lastIteration = true;
219 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +0000220
221 REQUIRE(originalVec == tensorVec);
222 }
223}