alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +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 | 4e00279 | 2022-05-04 09:45:02 +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 | } |
| 34 | namespace kws { |
| 35 | extern uint8_t* GetModelPointer(); |
| 36 | extern size_t GetModelLen(); |
| 37 | } |
| 38 | } /* namespace app */ |
| 39 | } /* namespace arm */ |
| 40 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 41 | template <typename T> |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 42 | static TfLiteTensor GetTestTensor( |
| 43 | std::vector<int>& shape, |
| 44 | T initVal, |
| 45 | std::vector<T>& vectorBuf) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 46 | { |
| 47 | REQUIRE(0 != shape.size()); |
| 48 | |
| 49 | shape.insert(shape.begin(), shape.size()); |
| 50 | uint32_t sizeInBytes = sizeof(T); |
| 51 | for (size_t i = 1; i < shape.size(); ++i) { |
| 52 | sizeInBytes *= shape[i]; |
| 53 | } |
| 54 | |
| 55 | /* Allocate mem. */ |
| 56 | vectorBuf = std::vector<T>(sizeInBytes, initVal); |
| 57 | TfLiteIntArray* dims = tflite::testing::IntArrayFromInts(shape.data()); |
| 58 | return tflite::testing::CreateQuantizedTensor( |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 59 | vectorBuf.data(), dims, |
| 60 | 1, 0, "test-tensor"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | TEST_CASE("Checking return value") |
| 64 | { |
| 65 | SECTION("Mismatched post processing parameters and tensor size") |
| 66 | { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 67 | const uint32_t outputCtxLen = 5; |
| 68 | arm::app::AsrClassifier classifier; |
| 69 | arm::app::Wav2LetterModel model; |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 70 | model.Init(arm::app::tensorArena, |
| 71 | sizeof(arm::app::tensorArena), |
| 72 | arm::app::asr::GetModelPointer(), |
| 73 | arm::app::asr::GetModelLen()); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 74 | std::vector<std::string> dummyLabels = {"a", "b", "$"}; |
| 75 | const uint32_t blankTokenIdx = 2; |
| 76 | std::vector<arm::app::ClassificationResult> dummyResult; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 77 | std::vector <int> tensorShape = {1, 1, 1, 13}; |
| 78 | std::vector <int8_t> tensorVec; |
| 79 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 80 | tensorShape, 100, tensorVec); |
| 81 | |
| 82 | arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen, |
| 83 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
| 84 | |
| 85 | REQUIRE(!post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | SECTION("Post processing succeeds") |
| 89 | { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 90 | const uint32_t outputCtxLen = 5; |
| 91 | arm::app::AsrClassifier classifier; |
| 92 | arm::app::Wav2LetterModel model; |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 93 | model.Init(arm::app::tensorArena, |
| 94 | sizeof(arm::app::tensorArena), |
| 95 | arm::app::asr::GetModelPointer(), |
| 96 | arm::app::asr::GetModelLen()); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 97 | std::vector<std::string> dummyLabels = {"a", "b", "$"}; |
| 98 | const uint32_t blankTokenIdx = 2; |
| 99 | std::vector<arm::app::ClassificationResult> dummyResult; |
| 100 | std::vector<int> tensorShape = {1, 1, 13, 1}; |
| 101 | std::vector<int8_t> tensorVec; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 102 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 103 | tensorShape, 100, tensorVec); |
| 104 | |
| 105 | arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen, |
| 106 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 107 | |
| 108 | /* Copy elements to compare later. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 109 | std::vector<int8_t> originalVec = tensorVec; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 110 | |
| 111 | /* This step should not erase anything. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 112 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 116 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 117 | TEST_CASE("Postprocessing - erasing required elements") |
| 118 | { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 119 | constexpr uint32_t outputCtxLen = 5; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 120 | constexpr uint32_t innerLen = 3; |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 121 | constexpr uint32_t nRows = 2*outputCtxLen + innerLen; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 122 | constexpr uint32_t nCols = 10; |
| 123 | constexpr uint32_t blankTokenIdx = nCols - 1; |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 124 | std::vector<int> tensorShape = {1, 1, nRows, nCols}; |
| 125 | arm::app::AsrClassifier classifier; |
| 126 | arm::app::Wav2LetterModel model; |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 127 | model.Init(arm::app::tensorArena, |
| 128 | sizeof(arm::app::tensorArena), |
| 129 | arm::app::asr::GetModelPointer(), |
| 130 | arm::app::asr::GetModelLen()); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 131 | std::vector<std::string> dummyLabels = {"a", "b", "$"}; |
| 132 | std::vector<arm::app::ClassificationResult> dummyResult; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 133 | |
| 134 | SECTION("First and last iteration") |
| 135 | { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 136 | std::vector<int8_t> tensorVec; |
| 137 | TfLiteTensor tensor = GetTestTensor<int8_t>(tensorShape, 100, tensorVec); |
| 138 | arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen, |
| 139 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 140 | |
| 141 | /* Copy elements to compare later. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 142 | std::vector<int8_t>originalVec = tensorVec; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 143 | |
| 144 | /* This step should not erase anything. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 145 | post.m_lastIteration = true; |
| 146 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 147 | REQUIRE(originalVec == tensorVec); |
| 148 | } |
| 149 | |
| 150 | SECTION("Right context erase") |
| 151 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 152 | std::vector <int8_t> tensorVec; |
| 153 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 154 | tensorShape, 100, tensorVec); |
| 155 | arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen, |
| 156 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 157 | |
| 158 | /* Copy elements to compare later. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 159 | std::vector<int8_t> originalVec = tensorVec; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 160 | |
| 161 | /* This step should erase the right context only. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 162 | post.m_lastIteration = false; |
| 163 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 164 | REQUIRE(originalVec != tensorVec); |
| 165 | |
| 166 | /* The last ctxLen * 10 elements should be gone. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 167 | for (size_t i = 0; i < outputCtxLen; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 168 | for (size_t j = 0; j < nCols; ++j) { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 169 | /* 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] | 170 | if (j == blankTokenIdx) { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 171 | CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 1); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 172 | } else { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 173 | CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* Check left context is preserved. */ |
| 177 | CHECK(tensorVec[i*nCols + j] == originalVec[i*nCols + j]); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /* Check inner elements are preserved. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 182 | for (size_t i = outputCtxLen * nCols; i < (outputCtxLen + innerLen) * nCols; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 183 | CHECK(tensorVec[i] == originalVec[i]); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | SECTION("Left and right context erase") |
| 188 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 189 | std::vector <int8_t> tensorVec; |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 190 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
| 191 | tensorShape, 100, tensorVec); |
| 192 | arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen, |
| 193 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 194 | |
| 195 | /* Copy elements to compare later. */ |
| 196 | std::vector <int8_t> originalVec = tensorVec; |
| 197 | |
| 198 | /* This step should erase right context. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 199 | post.m_lastIteration = false; |
| 200 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 201 | |
| 202 | /* Calling it the second time should erase the left context. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 203 | REQUIRE(post.DoPostProcess()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 204 | |
| 205 | REQUIRE(originalVec != tensorVec); |
| 206 | |
| 207 | /* The first and last ctxLen * 10 elements should be gone. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 208 | for (size_t i = 0; i < outputCtxLen; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 209 | for (size_t j = 0; j < nCols; ++j) { |
| 210 | /* Check left and right context elements are zeroed. */ |
| 211 | if (j == blankTokenIdx) { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 212 | CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 1); |
| 213 | CHECK(tensorVec[i*nCols + j] == 1); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 214 | } else { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 215 | CHECK(tensorVec[(outputCtxLen + innerLen) * nCols + i*nCols + j] == 0); |
| 216 | CHECK(tensorVec[i*nCols + j] == 0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* Check inner elements are preserved. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 222 | for (size_t i = outputCtxLen * nCols; i < (outputCtxLen + innerLen) * nCols; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 223 | /* Check left context is preserved. */ |
| 224 | CHECK(tensorVec[i] == originalVec[i]); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | SECTION("Try left context erase") |
| 229 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 230 | std::vector <int8_t> tensorVec; |
| 231 | TfLiteTensor tensor = GetTestTensor<int8_t>( |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 232 | tensorShape, 100, tensorVec); |
| 233 | |
| 234 | /* Should not be able to erase the left context if it is the first iteration. */ |
| 235 | arm::app::AsrPostProcess post{&tensor, classifier, dummyLabels, dummyResult, outputCtxLen, |
| 236 | blankTokenIdx, arm::app::Wav2LetterModel::ms_outputRowsIdx}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 237 | |
| 238 | /* Copy elements to compare later. */ |
| 239 | std::vector <int8_t> originalVec = tensorVec; |
| 240 | |
| 241 | /* Calling it the second time should erase the left context. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 242 | post.m_lastIteration = true; |
| 243 | REQUIRE(post.DoPostProcess()); |
| 244 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 245 | REQUIRE(originalVec == tensorVec); |
| 246 | } |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 247 | } |