blob: 513c0844fa707cd1f41ab64aab49dde8e130aee6 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
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"
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010020#include "BufAttributes.hpp"
alexander3c798932021-03-26 21:42:19 +000021
22#include <algorithm>
23#include <catch.hpp>
24#include <limits>
25
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010026namespace arm {
27namespace app {
28 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
29
30 namespace asr {
31 extern uint8_t* GetModelPointer();
32 extern size_t GetModelLen();
33 } /* namespace asr */
34} /* namespace app */
35} /* namespace arm */
36
alexander3c798932021-03-26 21:42:19 +000037template <typename T>
38static TfLiteTensor GetTestTensor(
Richard Burtonb40ecf82022-04-22 16:14:57 +010039 std::vector<int>& shape,
40 T initVal,
41 std::vector<T>& vectorBuf)
alexander3c798932021-03-26 21:42:19 +000042{
43 REQUIRE(0 != shape.size());
44
45 shape.insert(shape.begin(), shape.size());
46 uint32_t sizeInBytes = sizeof(T);
47 for (size_t i = 1; i < shape.size(); ++i) {
48 sizeInBytes *= shape[i];
49 }
50
51 /* Allocate mem. */
52 vectorBuf = std::vector<T>(sizeInBytes, initVal);
53 TfLiteIntArray* dims = tflite::testing::IntArrayFromInts(shape.data());
54 return tflite::testing::CreateQuantizedTensor(
55 vectorBuf.data(), dims,
56 1, 0, "test-tensor");
57}
58
59TEST_CASE("Checking return value")
60{
61 SECTION("Mismatched post processing parameters and tensor size")
62 {
Richard Burtonc2911442022-04-22 09:08:21 +010063 const uint32_t outputCtxLen = 5;
64 arm::app::AsrClassifier classifier;
65 arm::app::Wav2LetterModel model;
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010066 model.Init(arm::app::tensorArena,
67 sizeof(arm::app::tensorArena),
68 arm::app::asr::GetModelPointer(),
69 arm::app::asr::GetModelLen());
Richard Burtonc2911442022-04-22 09:08:21 +010070 std::vector<std::string> dummyLabels = {"a", "b", "$"};
71 const uint32_t blankTokenIdx = 2;
72 std::vector<arm::app::ClassificationResult> dummyResult;
alexander3c798932021-03-26 21:42:19 +000073 std::vector <int> tensorShape = {1, 1, 1, 13};
74 std::vector <int8_t> tensorVec;
75 TfLiteTensor tensor = GetTestTensor<int8_t>(
Richard Burtonc2911442022-04-22 09:08:21 +010076 tensorShape, 100, tensorVec);
77
Richard Burtonb40ecf82022-04-22 16:14:57 +010078 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
Richard Burtonc2911442022-04-22 09:08:21 +010079 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
80
81 REQUIRE(!post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +000082 }
83
84 SECTION("Post processing succeeds")
85 {
Richard Burtonc2911442022-04-22 09:08:21 +010086 const uint32_t outputCtxLen = 5;
87 arm::app::AsrClassifier classifier;
88 arm::app::Wav2LetterModel model;
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010089 model.Init(arm::app::tensorArena,
90 sizeof(arm::app::tensorArena),
91 arm::app::asr::GetModelPointer(),
92 arm::app::asr::GetModelLen());
Richard Burtonc2911442022-04-22 09:08:21 +010093 std::vector<std::string> dummyLabels = {"a", "b", "$"};
94 const uint32_t blankTokenIdx = 2;
95 std::vector<arm::app::ClassificationResult> dummyResult;
96 std::vector<int> tensorShape = {1, 1, 13, 1};
97 std::vector<int8_t> tensorVec;
alexander3c798932021-03-26 21:42:19 +000098 TfLiteTensor tensor = GetTestTensor<int8_t>(
Richard Burtonc2911442022-04-22 09:08:21 +010099 tensorShape, 100, tensorVec);
100
Richard Burtonb40ecf82022-04-22 16:14:57 +0100101 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
Richard Burtonc2911442022-04-22 09:08:21 +0100102 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +0000103
104 /* Copy elements to compare later. */
Richard Burtonc2911442022-04-22 09:08:21 +0100105 std::vector<int8_t> originalVec = tensorVec;
alexander3c798932021-03-26 21:42:19 +0000106
107 /* This step should not erase anything. */
Richard Burtonc2911442022-04-22 09:08:21 +0100108 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +0000109 }
110}
111
112
113TEST_CASE("Postprocessing - erasing required elements")
114{
Richard Burtonc2911442022-04-22 09:08:21 +0100115 constexpr uint32_t outputCtxLen = 5;
alexander3c798932021-03-26 21:42:19 +0000116 constexpr uint32_t innerLen = 3;
Richard Burtonc2911442022-04-22 09:08:21 +0100117 constexpr uint32_t nRows = 2*outputCtxLen + innerLen;
alexander3c798932021-03-26 21:42:19 +0000118 constexpr uint32_t nCols = 10;
119 constexpr uint32_t blankTokenIdx = nCols - 1;
Richard Burtonc2911442022-04-22 09:08:21 +0100120 std::vector<int> tensorShape = {1, 1, nRows, nCols};
121 arm::app::AsrClassifier classifier;
122 arm::app::Wav2LetterModel model;
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100123 model.Init(arm::app::tensorArena,
124 sizeof(arm::app::tensorArena),
125 arm::app::asr::GetModelPointer(),
126 arm::app::asr::GetModelLen());
Richard Burtonc2911442022-04-22 09:08:21 +0100127 std::vector<std::string> dummyLabels = {"a", "b", "$"};
128 std::vector<arm::app::ClassificationResult> dummyResult;
alexander3c798932021-03-26 21:42:19 +0000129
130 SECTION("First and last iteration")
131 {
Richard Burtonc2911442022-04-22 09:08:21 +0100132 std::vector<int8_t> tensorVec;
133 TfLiteTensor tensor = GetTestTensor<int8_t>(tensorShape, 100, tensorVec);
Richard Burtonb40ecf82022-04-22 16:14:57 +0100134 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
Richard Burtonc2911442022-04-22 09:08:21 +0100135 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +0000136
137 /* Copy elements to compare later. */
Richard Burtonc2911442022-04-22 09:08:21 +0100138 std::vector<int8_t>originalVec = tensorVec;
alexander3c798932021-03-26 21:42:19 +0000139
140 /* This step should not erase anything. */
Richard Burtonc2911442022-04-22 09:08:21 +0100141 post.m_lastIteration = true;
142 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +0000143 REQUIRE(originalVec == tensorVec);
144 }
145
146 SECTION("Right context erase")
147 {
alexander3c798932021-03-26 21:42:19 +0000148 std::vector <int8_t> tensorVec;
149 TfLiteTensor tensor = GetTestTensor<int8_t>(
Richard Burtonc2911442022-04-22 09:08:21 +0100150 tensorShape, 100, tensorVec);
Richard Burtonb40ecf82022-04-22 16:14:57 +0100151 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
Richard Burtonc2911442022-04-22 09:08:21 +0100152 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +0000153
154 /* Copy elements to compare later. */
Richard Burtonc2911442022-04-22 09:08:21 +0100155 std::vector<int8_t> originalVec = tensorVec;
alexander3c798932021-03-26 21:42:19 +0000156
Richard Burtonc2911442022-04-22 09:08:21 +0100157 //auto tensorData = tflite::GetTensorData<int8_t>(tensor);
alexander3c798932021-03-26 21:42:19 +0000158 /* This step should erase the right context only. */
Richard Burtonc2911442022-04-22 09:08:21 +0100159 post.m_lastIteration = false;
160 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +0000161 REQUIRE(originalVec != tensorVec);
162
163 /* The last ctxLen * 10 elements should be gone. */
Richard Burtonc2911442022-04-22 09:08:21 +0100164 for (size_t i = 0; i < outputCtxLen; ++i) {
alexander3c798932021-03-26 21:42:19 +0000165 for (size_t j = 0; j < nCols; ++j) {
Richard Burtonc2911442022-04-22 09:08:21 +0100166 /* Check right context elements are zeroed. Blank token idx should be set to 1 when erasing. */
alexander3c798932021-03-26 21:42:19 +0000167 if (j == blankTokenIdx) {
Richard Burtonc2911442022-04-22 09:08:21 +0100168 CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 1);
alexander3c798932021-03-26 21:42:19 +0000169 } else {
Richard Burtonc2911442022-04-22 09:08:21 +0100170 CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 0);
alexander3c798932021-03-26 21:42:19 +0000171 }
172
173 /* Check left context is preserved. */
174 CHECK(tensorVec[i*nCols + j] == originalVec[i*nCols + j]);
175 }
176 }
177
178 /* Check inner elements are preserved. */
Richard Burtonc2911442022-04-22 09:08:21 +0100179 for (size_t i = outputCtxLen * nCols; i < (outputCtxLen + innerLen) * nCols; ++i) {
alexander3c798932021-03-26 21:42:19 +0000180 CHECK(tensorVec[i] == originalVec[i]);
181 }
182 }
183
184 SECTION("Left and right context erase")
185 {
alexander3c798932021-03-26 21:42:19 +0000186 std::vector <int8_t> tensorVec;
187 TfLiteTensor tensor = GetTestTensor<int8_t>(
Richard Burtonc2911442022-04-22 09:08:21 +0100188 tensorShape, 100, tensorVec);
Richard Burtonb40ecf82022-04-22 16:14:57 +0100189 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
Richard Burtonc2911442022-04-22 09:08:21 +0100190 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +0000191
192 /* Copy elements to compare later. */
193 std::vector <int8_t> originalVec = tensorVec;
194
195 /* This step should erase right context. */
Richard Burtonc2911442022-04-22 09:08:21 +0100196 post.m_lastIteration = false;
197 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +0000198
199 /* Calling it the second time should erase the left context. */
Richard Burtonc2911442022-04-22 09:08:21 +0100200 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +0000201
202 REQUIRE(originalVec != tensorVec);
203
204 /* The first and last ctxLen * 10 elements should be gone. */
Richard Burtonc2911442022-04-22 09:08:21 +0100205 for (size_t i = 0; i < outputCtxLen; ++i) {
alexander3c798932021-03-26 21:42:19 +0000206 for (size_t j = 0; j < nCols; ++j) {
207 /* Check left and right context elements are zeroed. */
208 if (j == blankTokenIdx) {
Richard Burtonc2911442022-04-22 09:08:21 +0100209 CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 1);
alexander3c798932021-03-26 21:42:19 +0000210 CHECK(tensorVec[i*nCols + j] == 1);
211 } else {
Richard Burtonc2911442022-04-22 09:08:21 +0100212 CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 0);
alexander3c798932021-03-26 21:42:19 +0000213 CHECK(tensorVec[i*nCols + j] == 0);
214 }
215 }
216 }
217
218 /* Check inner elements are preserved. */
Richard Burtonc2911442022-04-22 09:08:21 +0100219 for (size_t i = outputCtxLen * nCols; i < (outputCtxLen + innerLen) * nCols; ++i) {
alexander3c798932021-03-26 21:42:19 +0000220 /* Check left context is preserved. */
221 CHECK(tensorVec[i] == originalVec[i]);
222 }
223 }
224
225 SECTION("Try left context erase")
226 {
alexander3c798932021-03-26 21:42:19 +0000227 std::vector <int8_t> tensorVec;
228 TfLiteTensor tensor = GetTestTensor<int8_t>(
Richard Burtonc2911442022-04-22 09:08:21 +0100229 tensorShape, 100, tensorVec);
230
231 /* Should not be able to erase the left context if it is the first iteration. */
Richard Burtonb40ecf82022-04-22 16:14:57 +0100232 arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen,
Richard Burtonc2911442022-04-22 09:08:21 +0100233 blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx};
alexander3c798932021-03-26 21:42:19 +0000234
235 /* Copy elements to compare later. */
236 std::vector <int8_t> originalVec = tensorVec;
237
238 /* Calling it the second time should erase the left context. */
Richard Burtonc2911442022-04-22 09:08:21 +0100239 post.m_lastIteration = true;
240 REQUIRE(post.DoPostProcess());
alexander3c798932021-03-26 21:42:19 +0000241
242 REQUIRE(originalVec == tensorVec);
243 }
244}