alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2021-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
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" |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 20 | #include "BufAttributes.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 21 | |
| 22 | #include <algorithm> |
| 23 | #include <catch.hpp> |
| 24 | #include <limits> |
| 25 | |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 26 | namespace arm { |
| 27 | namespace 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 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 37 | template <typename T> |
| 38 | static TfLiteTensor GetTestTensor( |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 39 | std::vector<int>& shape, |
| 40 | T initVal, |
| 41 | std::vector<T>& vectorBuf) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 42 | { |
| 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 | |
| 59 | TEST_CASE("Checking return value") |
| 60 | { |
| 61 | SECTION("Mismatched post processing parameters and tensor size") |
| 62 | { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 63 | const uint32_t outputCtxLen = 5; |
| 64 | arm::app::AsrClassifier classifier; |
| 65 | arm::app::Wav2LetterModel model; |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 66 | model.Init(arm::app::tensorArena, |
| 67 | sizeof(arm::app::tensorArena), |
| 68 | arm::app::asr::GetModelPointer(), |
| 69 | arm::app::asr::GetModelLen()); |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 70 | std::vector<std::string> placeholderLabels = {"a", "b", "$"}; |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 71 | const uint32_t blankTokenIdx = 2; |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 72 | std::vector<arm::app::ClassificationResult> placeholderResult; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 73 | std::vector <int> tensorShape = {1, 1, 1, 13}; |
| 74 | std::vector <int8_t> tensorVec; |
| 75 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 76 | tensorShape, 100, tensorVec); |
| 77 | |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 78 | arm::app::AsrPostProcess post{&tensor, classifier, placeholderLabels, placeholderResult, outputCtxLen, |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 79 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
| 80 | |
| 81 | REQUIRE(!post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | SECTION("Post processing succeeds") |
| 85 | { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 86 | const uint32_t outputCtxLen = 5; |
| 87 | arm::app::AsrClassifier classifier; |
| 88 | arm::app::Wav2LetterModel model; |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 89 | model.Init(arm::app::tensorArena, |
| 90 | sizeof(arm::app::tensorArena), |
| 91 | arm::app::asr::GetModelPointer(), |
| 92 | arm::app::asr::GetModelLen()); |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 93 | std::vector<std::string> placeholderLabels = {"a", "b", "$"}; |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 94 | const uint32_t blankTokenIdx = 2; |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 95 | std::vector<arm::app::ClassificationResult> placeholderResult; |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 96 | std::vector<int> tensorShape = {1, 1, 13, 1}; |
| 97 | std::vector<int8_t> tensorVec; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 98 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 99 | tensorShape, 100, tensorVec); |
| 100 | |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 101 | arm::app::AsrPostProcess post{&tensor, classifier, placeholderLabels, placeholderResult, outputCtxLen, |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 102 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 103 | |
| 104 | /* Copy elements to compare later. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 105 | std::vector<int8_t> originalVec = tensorVec; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 106 | |
| 107 | /* This step should not erase anything. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 108 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | TEST_CASE("Postprocessing - erasing required elements") |
| 114 | { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 115 | constexpr uint32_t outputCtxLen = 5; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 116 | constexpr uint32_t innerLen = 3; |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 117 | constexpr uint32_t nRows = 2*outputCtxLen + innerLen; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 118 | constexpr uint32_t nCols = 10; |
| 119 | constexpr uint32_t blankTokenIdx = nCols - 1; |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 120 | std::vector<int> tensorShape = {1, 1, nRows, nCols}; |
| 121 | arm::app::AsrClassifier classifier; |
| 122 | arm::app::Wav2LetterModel model; |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 123 | model.Init(arm::app::tensorArena, |
| 124 | sizeof(arm::app::tensorArena), |
| 125 | arm::app::asr::GetModelPointer(), |
| 126 | arm::app::asr::GetModelLen()); |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 127 | std::vector<std::string> placeholderLabels = {"a", "b", "$"}; |
| 128 | std::vector<arm::app::ClassificationResult> placeholderResult; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 129 | |
| 130 | SECTION("First and last iteration") |
| 131 | { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 132 | std::vector<int8_t> tensorVec; |
| 133 | TfLiteTensor tensor = GetTestTensor<int8_t>(tensorShape, 100, tensorVec); |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 134 | arm::app::AsrPostProcess post{&tensor, classifier, placeholderLabels, placeholderResult, outputCtxLen, |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 135 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 136 | |
| 137 | /* Copy elements to compare later. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 138 | std::vector<int8_t>originalVec = tensorVec; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 139 | |
| 140 | /* This step should not erase anything. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 141 | post.m_lastIteration = true; |
| 142 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 143 | REQUIRE(originalVec == tensorVec); |
| 144 | } |
| 145 | |
| 146 | SECTION("Right context erase") |
| 147 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 148 | std::vector <int8_t> tensorVec; |
| 149 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 150 | tensorShape, 100, tensorVec); |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 151 | arm::app::AsrPostProcess post{&tensor, classifier, placeholderLabels, placeholderResult, outputCtxLen, |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 152 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 153 | |
| 154 | /* Copy elements to compare later. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 155 | std::vector<int8_t> originalVec = tensorVec; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 156 | |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 157 | //auto tensorData = tflite::GetTensorData<int8_t>(tensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 158 | /* This step should erase the right context only. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 159 | post.m_lastIteration = false; |
| 160 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 161 | REQUIRE(originalVec != tensorVec); |
| 162 | |
| 163 | /* The last ctxLen * 10 elements should be gone. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 164 | for (size_t i = 0; i < outputCtxLen; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 165 | for (size_t j = 0; j < nCols; ++j) { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 166 | /* 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] | 167 | if (j == blankTokenIdx) { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 168 | CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 1); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 169 | } else { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 170 | CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 171 | } |
| 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 Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 179 | for (size_t i = outputCtxLen * nCols; i < (outputCtxLen + innerLen) * nCols; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 180 | CHECK(tensorVec[i] == originalVec[i]); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | SECTION("Left and right context erase") |
| 185 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 186 | std::vector <int8_t> tensorVec; |
| 187 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 188 | tensorShape, 100, tensorVec); |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 189 | arm::app::AsrPostProcess post{&tensor, classifier, placeholderLabels, placeholderResult, outputCtxLen, |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 190 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 191 | |
| 192 | /* Copy elements to compare later. */ |
| 193 | std::vector <int8_t> originalVec = tensorVec; |
| 194 | |
| 195 | /* This step should erase right context. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 196 | post.m_lastIteration = false; |
| 197 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 198 | |
| 199 | /* Calling it the second time should erase the left context. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 200 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 201 | |
| 202 | REQUIRE(originalVec != tensorVec); |
| 203 | |
| 204 | /* The first and last ctxLen * 10 elements should be gone. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 205 | for (size_t i = 0; i < outputCtxLen; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 206 | for (size_t j = 0; j < nCols; ++j) { |
| 207 | /* Check left and right context elements are zeroed. */ |
| 208 | if (j == blankTokenIdx) { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 209 | CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 1); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 210 | CHECK(tensorVec[i*nCols + j] == 1); |
| 211 | } else { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 212 | CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 213 | CHECK(tensorVec[i*nCols + j] == 0); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* Check inner elements are preserved. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 219 | for (size_t i = outputCtxLen * nCols; i < (outputCtxLen + innerLen) * nCols; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 220 | /* Check left context is preserved. */ |
| 221 | CHECK(tensorVec[i] == originalVec[i]); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | SECTION("Try left context erase") |
| 226 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 227 | std::vector <int8_t> tensorVec; |
| 228 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 229 | tensorShape, 100, tensorVec); |
| 230 | |
| 231 | /* Should not be able to erase the left context if it is the first iteration. */ |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 232 | arm::app::AsrPostProcess post{&tensor, classifier, placeholderLabels, placeholderResult, outputCtxLen, |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 233 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 234 | |
| 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 Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 239 | post.m_lastIteration = true; |
| 240 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 241 | |
| 242 | REQUIRE(originalVec == tensorVec); |
| 243 | } |
| 244 | } |