alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 | #include "Wav2LetterPostprocess.hpp" |
| 18 | #include "Wav2LetterModel.hpp" |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 19 | #include "ClassificationResult.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 20 | |
| 21 | #include <algorithm> |
| 22 | #include <catch.hpp> |
| 23 | #include <limits> |
| 24 | |
| 25 | template <typename T> |
| 26 | static TfLiteTensor GetTestTensor( |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame^] | 27 | std::vector<int>& shape, |
| 28 | T initVal, |
| 29 | std::vector<T>& vectorBuf) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 30 | { |
| 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 | |
| 47 | TEST_CASE("Checking return value") |
| 48 | { |
| 49 | SECTION("Mismatched post processing parameters and tensor size") |
| 50 | { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 51 | 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; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 58 | std::vector <int> tensorShape = {1, 1, 1, 13}; |
| 59 | std::vector <int8_t> tensorVec; |
| 60 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 61 | tensorShape, 100, tensorVec); |
| 62 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame^] | 63 | arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen, |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 64 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
| 65 | |
| 66 | REQUIRE(!post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | SECTION("Post processing succeeds") |
| 70 | { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 71 | 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; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 80 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 81 | tensorShape, 100, tensorVec); |
| 82 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame^] | 83 | arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen, |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 84 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 85 | |
| 86 | /* Copy elements to compare later. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 87 | std::vector<int8_t> originalVec = tensorVec; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 88 | |
| 89 | /* This step should not erase anything. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 90 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | TEST_CASE("Postprocessing - erasing required elements") |
| 96 | { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 97 | constexpr uint32_t outputCtxLen = 5; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 98 | constexpr uint32_t innerLen = 3; |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 99 | constexpr uint32_t nRows = 2*outputCtxLen + innerLen; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 100 | constexpr uint32_t nCols = 10; |
| 101 | constexpr uint32_t blankTokenIdx = nCols - 1; |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 102 | 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; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 108 | |
| 109 | SECTION("First and last iteration") |
| 110 | { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 111 | std::vector<int8_t> tensorVec; |
| 112 | TfLiteTensor tensor = GetTestTensor<int8_t>(tensorShape, 100, tensorVec); |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame^] | 113 | arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen, |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 114 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 115 | |
| 116 | /* Copy elements to compare later. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 117 | std::vector<int8_t>originalVec = tensorVec; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 118 | |
| 119 | /* This step should not erase anything. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 120 | post.m_lastIteration = true; |
| 121 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 122 | REQUIRE(originalVec == tensorVec); |
| 123 | } |
| 124 | |
| 125 | SECTION("Right context erase") |
| 126 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 127 | std::vector <int8_t> tensorVec; |
| 128 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 129 | tensorShape, 100, tensorVec); |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame^] | 130 | arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen, |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 131 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 132 | |
| 133 | /* Copy elements to compare later. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 134 | std::vector<int8_t> originalVec = tensorVec; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 135 | |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 136 | //auto tensorData = tflite::GetTensorData<int8_t>(tensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 137 | /* This step should erase the right context only. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 138 | post.m_lastIteration = false; |
| 139 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 140 | REQUIRE(originalVec != tensorVec); |
| 141 | |
| 142 | /* The last ctxLen * 10 elements should be gone. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 143 | for (size_t i = 0; i < outputCtxLen; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 144 | for (size_t j = 0; j < nCols; ++j) { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 145 | /* Check right context elements are zeroed. Blank token idx should be set to 1 when erasing. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 146 | if (j == blankTokenIdx) { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 147 | CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 1); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 148 | } else { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 149 | CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 150 | } |
| 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 Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 158 | for (size_t i = outputCtxLen * nCols; i < (outputCtxLen + innerLen) * nCols; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 159 | CHECK(tensorVec[i] == originalVec[i]); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | SECTION("Left and right context erase") |
| 164 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 165 | std::vector <int8_t> tensorVec; |
| 166 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 167 | tensorShape, 100, tensorVec); |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame^] | 168 | arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen, |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 169 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 170 | |
| 171 | /* Copy elements to compare later. */ |
| 172 | std::vector <int8_t> originalVec = tensorVec; |
| 173 | |
| 174 | /* This step should erase right context. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 175 | post.m_lastIteration = false; |
| 176 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 177 | |
| 178 | /* Calling it the second time should erase the left context. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 179 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 180 | |
| 181 | REQUIRE(originalVec != tensorVec); |
| 182 | |
| 183 | /* The first and last ctxLen * 10 elements should be gone. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 184 | for (size_t i = 0; i < outputCtxLen; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 185 | for (size_t j = 0; j < nCols; ++j) { |
| 186 | /* Check left and right context elements are zeroed. */ |
| 187 | if (j == blankTokenIdx) { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 188 | CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 1); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 189 | CHECK(tensorVec[i*nCols + j] == 1); |
| 190 | } else { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 191 | CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 192 | CHECK(tensorVec[i*nCols + j] == 0); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* Check inner elements are preserved. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 198 | for (size_t i = outputCtxLen * nCols; i < (outputCtxLen + innerLen) * nCols; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 199 | /* Check left context is preserved. */ |
| 200 | CHECK(tensorVec[i] == originalVec[i]); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | SECTION("Try left context erase") |
| 205 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 206 | std::vector <int8_t> tensorVec; |
| 207 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 208 | tensorShape, 100, tensorVec); |
| 209 | |
| 210 | /* Should not be able to erase the left context if it is the first iteration. */ |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame^] | 211 | arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen, |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 212 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 213 | |
| 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 Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 218 | post.m_lastIteration = true; |
| 219 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 220 | |
| 221 | REQUIRE(originalVec == tensorVec); |
| 222 | } |
| 223 | } |