Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "neuralnetworks_hidl_hal_test" |
| 18 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | #include <android/hidl/memory/1.0/IMemory.h> |
| 21 | #include <ftw.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | #include <hidlmemory/mapping.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <cstdio> |
| 27 | #include <cstdlib> |
| 28 | #include <random> |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame] | 29 | #include <thread> |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 30 | |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 31 | #include "1.2/Callbacks.h" |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 32 | #include "GeneratedTestHarness.h" |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 33 | #include "MemoryUtils.h" |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 34 | #include "TestHarness.h" |
| 35 | #include "Utils.h" |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 36 | #include "VtsHalNeuralnetworks.h" |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 37 | |
Slava Shklyaev | e8b2446 | 2019-07-17 15:50:57 +0100 | [diff] [blame^] | 38 | namespace android::hardware::neuralnetworks::V1_2::generated_tests:: |
| 39 | mobilenet_224_gender_basic_fixed { |
| 40 | Model createTestModel(); |
| 41 | } // namespace android::hardware::neuralnetworks::V1_2::generated_tests::mobilenet_224_gender_basic_fixed |
| 42 | |
| 43 | namespace generated_tests::mobilenet_224_gender_basic_fixed { |
| 44 | std::vector<test_helper::MixedTypedExample>& get_examples(); |
| 45 | } // namespace generated_tests::mobilenet_224_gender_basic_fixed |
| 46 | |
| 47 | namespace android::hardware::neuralnetworks::V1_2::generated_tests::mobilenet_quantized { |
| 48 | Model createTestModel(); |
| 49 | } // namespace android::hardware::neuralnetworks::V1_2::generated_tests::mobilenet_quantized |
| 50 | |
| 51 | namespace generated_tests::mobilenet_quantized { |
| 52 | std::vector<test_helper::MixedTypedExample>& get_examples(); |
| 53 | } // namespace generated_tests::mobilenet_quantized |
| 54 | |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 55 | namespace android { |
| 56 | namespace hardware { |
| 57 | namespace neuralnetworks { |
| 58 | namespace V1_2 { |
| 59 | namespace vts { |
| 60 | namespace functional { |
| 61 | |
Michael Butler | 3835f61 | 2019-07-11 15:43:22 -0700 | [diff] [blame] | 62 | using ::android::hardware::neuralnetworks::V1_0::OperandLifeTime; |
| 63 | using ::android::hardware::neuralnetworks::V1_1::ExecutionPreference; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 64 | using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback; |
| 65 | using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback; |
Michael Butler | 3835f61 | 2019-07-11 15:43:22 -0700 | [diff] [blame] | 66 | using ::android::hidl::memory::V1_0::IMemory; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 67 | using ::android::nn::allocateSharedMemory; |
| 68 | using ::test_helper::MixedTypedExample; |
| 69 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 70 | namespace float32_model { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 71 | |
Slava Shklyaev | e8b2446 | 2019-07-17 15:50:57 +0100 | [diff] [blame^] | 72 | constexpr auto createTestModel = ::android::hardware::neuralnetworks::V1_2::generated_tests:: |
| 73 | mobilenet_224_gender_basic_fixed::createTestModel; |
| 74 | constexpr auto get_examples = ::generated_tests::mobilenet_224_gender_basic_fixed::get_examples; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 75 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 76 | // MixedTypedExample is defined in frameworks/ml/nn/tools/test_generator/include/TestHarness.h. |
| 77 | // This function assumes the operation is always ADD. |
| 78 | std::vector<MixedTypedExample> getLargeModelExamples(uint32_t len) { |
| 79 | float outputValue = 1.0f + static_cast<float>(len); |
| 80 | return {{.operands = { |
| 81 | // Input |
| 82 | {.operandDimensions = {{0, {1}}}, .float32Operands = {{0, {1.0f}}}}, |
| 83 | // Output |
| 84 | {.operandDimensions = {{0, {1}}}, .float32Operands = {{0, {outputValue}}}}}}}; |
| 85 | } |
| 86 | |
| 87 | } // namespace float32_model |
| 88 | |
| 89 | namespace quant8_model { |
| 90 | |
Slava Shklyaev | e8b2446 | 2019-07-17 15:50:57 +0100 | [diff] [blame^] | 91 | constexpr auto createTestModel = ::android::hardware::neuralnetworks::V1_2::generated_tests:: |
| 92 | mobilenet_quantized::createTestModel; |
| 93 | constexpr auto get_examples = ::generated_tests::mobilenet_quantized::get_examples; |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 94 | |
| 95 | // MixedTypedExample is defined in frameworks/ml/nn/tools/test_generator/include/TestHarness.h. |
| 96 | // This function assumes the operation is always ADD. |
| 97 | std::vector<MixedTypedExample> getLargeModelExamples(uint32_t len) { |
| 98 | uint8_t outputValue = 1 + static_cast<uint8_t>(len); |
| 99 | return {{.operands = {// Input |
| 100 | {.operandDimensions = {{0, {1}}}, .quant8AsymmOperands = {{0, {1}}}}, |
| 101 | // Output |
| 102 | {.operandDimensions = {{0, {1}}}, |
| 103 | .quant8AsymmOperands = {{0, {outputValue}}}}}}}; |
| 104 | } |
| 105 | |
| 106 | } // namespace quant8_model |
| 107 | |
| 108 | namespace { |
| 109 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 110 | enum class AccessMode { READ_WRITE, READ_ONLY, WRITE_ONLY }; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 111 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 112 | // Creates cache handles based on provided file groups. |
| 113 | // The outer vector corresponds to handles and the inner vector is for fds held by each handle. |
| 114 | void createCacheHandles(const std::vector<std::vector<std::string>>& fileGroups, |
| 115 | const std::vector<AccessMode>& mode, hidl_vec<hidl_handle>* handles) { |
| 116 | handles->resize(fileGroups.size()); |
| 117 | for (uint32_t i = 0; i < fileGroups.size(); i++) { |
| 118 | std::vector<int> fds; |
| 119 | for (const auto& file : fileGroups[i]) { |
| 120 | int fd; |
| 121 | if (mode[i] == AccessMode::READ_ONLY) { |
| 122 | fd = open(file.c_str(), O_RDONLY); |
| 123 | } else if (mode[i] == AccessMode::WRITE_ONLY) { |
| 124 | fd = open(file.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); |
| 125 | } else if (mode[i] == AccessMode::READ_WRITE) { |
| 126 | fd = open(file.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); |
| 127 | } else { |
| 128 | FAIL(); |
| 129 | } |
| 130 | ASSERT_GE(fd, 0); |
| 131 | fds.push_back(fd); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 132 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 133 | native_handle_t* cacheNativeHandle = native_handle_create(fds.size(), 0); |
| 134 | ASSERT_NE(cacheNativeHandle, nullptr); |
| 135 | std::copy(fds.begin(), fds.end(), &cacheNativeHandle->data[0]); |
| 136 | (*handles)[i].setTo(cacheNativeHandle, /*shouldOwn=*/true); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 137 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void createCacheHandles(const std::vector<std::vector<std::string>>& fileGroups, AccessMode mode, |
| 141 | hidl_vec<hidl_handle>* handles) { |
| 142 | createCacheHandles(fileGroups, std::vector<AccessMode>(fileGroups.size(), mode), handles); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 145 | // Create a chain of broadcast operations. The second operand is always constant tensor [1]. |
| 146 | // For simplicity, activation scalar is shared. The second operand is not shared |
| 147 | // in the model to let driver maintain a non-trivial size of constant data and the corresponding |
| 148 | // data locations in cache. |
| 149 | // |
| 150 | // --------- activation -------- |
| 151 | // ↓ ↓ ↓ ↓ |
| 152 | // E.g. input -> ADD -> ADD -> ADD -> ... -> ADD -> output |
| 153 | // ↑ ↑ ↑ ↑ |
| 154 | // [1] [1] [1] [1] |
| 155 | // |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 156 | // This function assumes the operation is either ADD or MUL. |
| 157 | template <typename CppType, OperandType operandType> |
| 158 | Model createLargeTestModelImpl(OperationType op, uint32_t len) { |
| 159 | EXPECT_TRUE(op == OperationType::ADD || op == OperationType::MUL); |
| 160 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 161 | // Model operations and operands. |
| 162 | std::vector<Operation> operations(len); |
| 163 | std::vector<Operand> operands(len * 2 + 2); |
| 164 | |
| 165 | // The constant buffer pool. This contains the activation scalar, followed by the |
| 166 | // per-operation constant operands. |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 167 | std::vector<uint8_t> operandValues(sizeof(int32_t) + len * sizeof(CppType)); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 168 | |
| 169 | // The activation scalar, value = 0. |
| 170 | operands[0] = { |
| 171 | .type = OperandType::INT32, |
| 172 | .dimensions = {}, |
| 173 | .numberOfConsumers = len, |
| 174 | .scale = 0.0f, |
| 175 | .zeroPoint = 0, |
| 176 | .lifetime = OperandLifeTime::CONSTANT_COPY, |
| 177 | .location = {.poolIndex = 0, .offset = 0, .length = sizeof(int32_t)}, |
| 178 | }; |
| 179 | memset(operandValues.data(), 0, sizeof(int32_t)); |
| 180 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 181 | // The buffer value of the constant second operand. The logical value is always 1.0f. |
| 182 | CppType bufferValue; |
| 183 | // The scale of the first and second operand. |
| 184 | float scale1, scale2; |
| 185 | if (operandType == OperandType::TENSOR_FLOAT32) { |
| 186 | bufferValue = 1.0f; |
| 187 | scale1 = 0.0f; |
| 188 | scale2 = 0.0f; |
| 189 | } else if (op == OperationType::ADD) { |
| 190 | bufferValue = 1; |
| 191 | scale1 = 1.0f; |
| 192 | scale2 = 1.0f; |
| 193 | } else { |
| 194 | // To satisfy the constraint on quant8 MUL: input0.scale * input1.scale < output.scale, |
| 195 | // set input1 to have scale = 0.5f and bufferValue = 2, i.e. 1.0f in floating point. |
| 196 | bufferValue = 2; |
| 197 | scale1 = 1.0f; |
| 198 | scale2 = 0.5f; |
| 199 | } |
| 200 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 201 | for (uint32_t i = 0; i < len; i++) { |
| 202 | const uint32_t firstInputIndex = i * 2 + 1; |
| 203 | const uint32_t secondInputIndex = firstInputIndex + 1; |
| 204 | const uint32_t outputIndex = secondInputIndex + 1; |
| 205 | |
| 206 | // The first operation input. |
| 207 | operands[firstInputIndex] = { |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 208 | .type = operandType, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 209 | .dimensions = {1}, |
| 210 | .numberOfConsumers = 1, |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 211 | .scale = scale1, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 212 | .zeroPoint = 0, |
| 213 | .lifetime = (i == 0 ? OperandLifeTime::MODEL_INPUT |
| 214 | : OperandLifeTime::TEMPORARY_VARIABLE), |
| 215 | .location = {}, |
| 216 | }; |
| 217 | |
| 218 | // The second operation input, value = 1. |
| 219 | operands[secondInputIndex] = { |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 220 | .type = operandType, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 221 | .dimensions = {1}, |
| 222 | .numberOfConsumers = 1, |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 223 | .scale = scale2, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 224 | .zeroPoint = 0, |
| 225 | .lifetime = OperandLifeTime::CONSTANT_COPY, |
| 226 | .location = {.poolIndex = 0, |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 227 | .offset = static_cast<uint32_t>(i * sizeof(CppType) + sizeof(int32_t)), |
| 228 | .length = sizeof(CppType)}, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 229 | }; |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 230 | memcpy(operandValues.data() + sizeof(int32_t) + i * sizeof(CppType), &bufferValue, |
| 231 | sizeof(CppType)); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 232 | |
| 233 | // The operation. All operations share the same activation scalar. |
| 234 | // The output operand is created as an input in the next iteration of the loop, in the case |
| 235 | // of all but the last member of the chain; and after the loop as a model output, in the |
| 236 | // case of the last member of the chain. |
| 237 | operations[i] = { |
| 238 | .type = op, |
| 239 | .inputs = {firstInputIndex, secondInputIndex, /*activation scalar*/ 0}, |
| 240 | .outputs = {outputIndex}, |
| 241 | }; |
| 242 | } |
| 243 | |
| 244 | // The model output. |
| 245 | operands.back() = { |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 246 | .type = operandType, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 247 | .dimensions = {1}, |
| 248 | .numberOfConsumers = 0, |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 249 | .scale = scale1, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 250 | .zeroPoint = 0, |
| 251 | .lifetime = OperandLifeTime::MODEL_OUTPUT, |
| 252 | .location = {}, |
| 253 | }; |
| 254 | |
| 255 | const std::vector<uint32_t> inputIndexes = {1}; |
| 256 | const std::vector<uint32_t> outputIndexes = {len * 2 + 1}; |
| 257 | const std::vector<hidl_memory> pools = {}; |
| 258 | |
| 259 | return { |
| 260 | .operands = operands, |
| 261 | .operations = operations, |
| 262 | .inputIndexes = inputIndexes, |
| 263 | .outputIndexes = outputIndexes, |
| 264 | .operandValues = operandValues, |
| 265 | .pools = pools, |
| 266 | }; |
| 267 | } |
| 268 | |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 269 | } // namespace |
| 270 | |
| 271 | // Tag for the compilation caching tests. |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 272 | class CompilationCachingTestBase : public NeuralnetworksHidlTest { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 273 | protected: |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 274 | CompilationCachingTestBase(OperandType type) : kOperandType(type) {} |
| 275 | |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 276 | void SetUp() override { |
| 277 | NeuralnetworksHidlTest::SetUp(); |
Hervé Guihot | ac7ac52 | 2019-02-12 16:22:44 -0800 | [diff] [blame] | 278 | ASSERT_NE(device.get(), nullptr); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 279 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 280 | // Create cache directory. The cache directory and a temporary cache file is always created |
| 281 | // to test the behavior of prepareModelFromCache, even when caching is not supported. |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 282 | char cacheDirTemp[] = "/data/local/tmp/TestCompilationCachingXXXXXX"; |
| 283 | char* cacheDir = mkdtemp(cacheDirTemp); |
| 284 | ASSERT_NE(cacheDir, nullptr); |
Xusong Wang | 6824cc1 | 2019-02-12 18:00:37 -0800 | [diff] [blame] | 285 | mCacheDir = cacheDir; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 286 | mCacheDir.push_back('/'); |
Xusong Wang | 6824cc1 | 2019-02-12 18:00:37 -0800 | [diff] [blame] | 287 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 288 | Return<void> ret = device->getNumberOfCacheFilesNeeded( |
| 289 | [this](ErrorStatus status, uint32_t numModelCache, uint32_t numDataCache) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 290 | EXPECT_EQ(ErrorStatus::NONE, status); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 291 | mNumModelCache = numModelCache; |
| 292 | mNumDataCache = numDataCache; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 293 | }); |
| 294 | EXPECT_TRUE(ret.isOk()); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 295 | mIsCachingSupported = mNumModelCache > 0 || mNumDataCache > 0; |
| 296 | |
| 297 | // Create empty cache files. |
| 298 | mTmpCache = mCacheDir + "tmp"; |
| 299 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
| 300 | mModelCache.push_back({mCacheDir + "model" + std::to_string(i)}); |
| 301 | } |
| 302 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
| 303 | mDataCache.push_back({mCacheDir + "data" + std::to_string(i)}); |
| 304 | } |
| 305 | // Dummy handles, use AccessMode::WRITE_ONLY for createCacheHandles to create files. |
| 306 | hidl_vec<hidl_handle> modelHandle, dataHandle, tmpHandle; |
| 307 | createCacheHandles(mModelCache, AccessMode::WRITE_ONLY, &modelHandle); |
| 308 | createCacheHandles(mDataCache, AccessMode::WRITE_ONLY, &dataHandle); |
| 309 | createCacheHandles({{mTmpCache}}, AccessMode::WRITE_ONLY, &tmpHandle); |
| 310 | |
| 311 | if (!mIsCachingSupported) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 312 | LOG(INFO) << "NN VTS: Early termination of test because vendor service does not " |
| 313 | "support compilation caching."; |
| 314 | std::cout << "[ ] Early termination of test because vendor service does not " |
| 315 | "support compilation caching." |
| 316 | << std::endl; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 317 | } |
Xusong Wang | 6824cc1 | 2019-02-12 18:00:37 -0800 | [diff] [blame] | 318 | } |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 319 | |
Xusong Wang | 6824cc1 | 2019-02-12 18:00:37 -0800 | [diff] [blame] | 320 | void TearDown() override { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 321 | // If the test passes, remove the tmp directory. Otherwise, keep it for debugging purposes. |
| 322 | if (!::testing::Test::HasFailure()) { |
| 323 | // Recursively remove the cache directory specified by mCacheDir. |
| 324 | auto callback = [](const char* entry, const struct stat*, int, struct FTW*) { |
| 325 | return remove(entry); |
| 326 | }; |
| 327 | nftw(mCacheDir.c_str(), callback, 128, FTW_DEPTH | FTW_MOUNT | FTW_PHYS); |
Xusong Wang | 6824cc1 | 2019-02-12 18:00:37 -0800 | [diff] [blame] | 328 | } |
| 329 | NeuralnetworksHidlTest::TearDown(); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 332 | // Model and examples creators. According to kOperandType, the following methods will return |
| 333 | // either float32 model/examples or the quant8 variant. |
| 334 | Model createTestModel() { |
| 335 | if (kOperandType == OperandType::TENSOR_FLOAT32) { |
| 336 | return float32_model::createTestModel(); |
| 337 | } else { |
| 338 | return quant8_model::createTestModel(); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | std::vector<MixedTypedExample> get_examples() { |
| 343 | if (kOperandType == OperandType::TENSOR_FLOAT32) { |
| 344 | return float32_model::get_examples(); |
| 345 | } else { |
| 346 | return quant8_model::get_examples(); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | Model createLargeTestModel(OperationType op, uint32_t len) { |
| 351 | if (kOperandType == OperandType::TENSOR_FLOAT32) { |
| 352 | return createLargeTestModelImpl<float, OperandType::TENSOR_FLOAT32>(op, len); |
| 353 | } else { |
| 354 | return createLargeTestModelImpl<uint8_t, OperandType::TENSOR_QUANT8_ASYMM>(op, len); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | std::vector<MixedTypedExample> getLargeModelExamples(uint32_t len) { |
| 359 | if (kOperandType == OperandType::TENSOR_FLOAT32) { |
| 360 | return float32_model::getLargeModelExamples(len); |
| 361 | } else { |
| 362 | return quant8_model::getLargeModelExamples(len); |
| 363 | } |
| 364 | } |
| 365 | |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 366 | // See if the service can handle the model. |
| 367 | bool isModelFullySupported(const V1_2::Model& model) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 368 | bool fullySupportsModel = false; |
| 369 | Return<void> supportedCall = device->getSupportedOperations_1_2( |
| 370 | model, |
| 371 | [&fullySupportsModel, &model](ErrorStatus status, const hidl_vec<bool>& supported) { |
| 372 | ASSERT_EQ(ErrorStatus::NONE, status); |
| 373 | ASSERT_EQ(supported.size(), model.operations.size()); |
| 374 | fullySupportsModel = std::all_of(supported.begin(), supported.end(), |
| 375 | [](bool valid) { return valid; }); |
| 376 | }); |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 377 | EXPECT_TRUE(supportedCall.isOk()); |
| 378 | return fullySupportsModel; |
| 379 | } |
| 380 | |
| 381 | void saveModelToCache(const V1_2::Model& model, const hidl_vec<hidl_handle>& modelCache, |
| 382 | const hidl_vec<hidl_handle>& dataCache, |
| 383 | sp<IPreparedModel>* preparedModel = nullptr) { |
| 384 | if (preparedModel != nullptr) *preparedModel = nullptr; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 385 | |
| 386 | // Launch prepare model. |
| 387 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
| 388 | ASSERT_NE(nullptr, preparedModelCallback.get()); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 389 | hidl_array<uint8_t, sizeof(mToken)> cacheToken(mToken); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 390 | Return<ErrorStatus> prepareLaunchStatus = |
| 391 | device->prepareModel_1_2(model, ExecutionPreference::FAST_SINGLE_ANSWER, modelCache, |
| 392 | dataCache, cacheToken, preparedModelCallback); |
| 393 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
| 394 | ASSERT_EQ(static_cast<ErrorStatus>(prepareLaunchStatus), ErrorStatus::NONE); |
| 395 | |
| 396 | // Retrieve prepared model. |
| 397 | preparedModelCallback->wait(); |
| 398 | ASSERT_EQ(preparedModelCallback->getStatus(), ErrorStatus::NONE); |
| 399 | if (preparedModel != nullptr) { |
| 400 | *preparedModel = |
| 401 | V1_2::IPreparedModel::castFrom(preparedModelCallback->getPreparedModel()) |
| 402 | .withDefault(nullptr); |
| 403 | } |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | bool checkEarlyTermination(ErrorStatus status) { |
| 407 | if (status == ErrorStatus::GENERAL_FAILURE) { |
| 408 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 409 | "save the prepared model that it does not support."; |
| 410 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 411 | "save the prepared model that it does not support." |
| 412 | << std::endl; |
| 413 | return true; |
| 414 | } |
| 415 | return false; |
| 416 | } |
| 417 | |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 418 | bool checkEarlyTermination(const V1_2::Model& model) { |
| 419 | if (!isModelFullySupported(model)) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 420 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 421 | "prepare model that it does not support."; |
| 422 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 423 | "prepare model that it does not support." |
| 424 | << std::endl; |
| 425 | return true; |
| 426 | } |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | void prepareModelFromCache(const hidl_vec<hidl_handle>& modelCache, |
| 431 | const hidl_vec<hidl_handle>& dataCache, |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 432 | sp<IPreparedModel>* preparedModel, ErrorStatus* status) { |
| 433 | // Launch prepare model from cache. |
| 434 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
| 435 | ASSERT_NE(nullptr, preparedModelCallback.get()); |
| 436 | hidl_array<uint8_t, sizeof(mToken)> cacheToken(mToken); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 437 | Return<ErrorStatus> prepareLaunchStatus = device->prepareModelFromCache( |
| 438 | modelCache, dataCache, cacheToken, preparedModelCallback); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 439 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
| 440 | if (static_cast<ErrorStatus>(prepareLaunchStatus) != ErrorStatus::NONE) { |
| 441 | *preparedModel = nullptr; |
| 442 | *status = static_cast<ErrorStatus>(prepareLaunchStatus); |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | // Retrieve prepared model. |
| 447 | preparedModelCallback->wait(); |
| 448 | *status = preparedModelCallback->getStatus(); |
| 449 | *preparedModel = V1_2::IPreparedModel::castFrom(preparedModelCallback->getPreparedModel()) |
| 450 | .withDefault(nullptr); |
| 451 | } |
| 452 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 453 | // Absolute path to the temporary cache directory. |
Xusong Wang | 6824cc1 | 2019-02-12 18:00:37 -0800 | [diff] [blame] | 454 | std::string mCacheDir; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 455 | |
| 456 | // Groups of file paths for model and data cache in the tmp cache directory, initialized with |
| 457 | // outer_size = mNum{Model|Data}Cache, inner_size = 1. The outer vector corresponds to handles |
| 458 | // and the inner vector is for fds held by each handle. |
| 459 | std::vector<std::vector<std::string>> mModelCache; |
| 460 | std::vector<std::vector<std::string>> mDataCache; |
| 461 | |
| 462 | // A separate temporary file path in the tmp cache directory. |
| 463 | std::string mTmpCache; |
| 464 | |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 465 | uint8_t mToken[static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)] = {}; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 466 | uint32_t mNumModelCache; |
| 467 | uint32_t mNumDataCache; |
| 468 | uint32_t mIsCachingSupported; |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 469 | |
| 470 | // The primary data type of the testModel. |
| 471 | const OperandType kOperandType; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 472 | }; |
| 473 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 474 | // A parameterized fixture of CompilationCachingTestBase. Every test will run twice, with the first |
| 475 | // pass running with float32 models and the second pass running with quant8 models. |
| 476 | class CompilationCachingTest : public CompilationCachingTestBase, |
| 477 | public ::testing::WithParamInterface<OperandType> { |
| 478 | protected: |
| 479 | CompilationCachingTest() : CompilationCachingTestBase(GetParam()) {} |
| 480 | }; |
| 481 | |
| 482 | TEST_P(CompilationCachingTest, CacheSavingAndRetrieval) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 483 | // Create test HIDL model and compile. |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 484 | const Model testModel = createTestModel(); |
| 485 | if (checkEarlyTermination(testModel)) return; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 486 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 487 | |
| 488 | // Save the compilation to cache. |
| 489 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 490 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 491 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 492 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 493 | saveModelToCache(testModel, modelCache, dataCache); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // Retrieve preparedModel from cache. |
| 497 | { |
| 498 | preparedModel = nullptr; |
| 499 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 500 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 501 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 502 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 503 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 504 | if (!mIsCachingSupported) { |
| 505 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 506 | ASSERT_EQ(preparedModel, nullptr); |
| 507 | return; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 508 | } else if (checkEarlyTermination(status)) { |
| 509 | ASSERT_EQ(preparedModel, nullptr); |
| 510 | return; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 511 | } else { |
| 512 | ASSERT_EQ(status, ErrorStatus::NONE); |
| 513 | ASSERT_NE(preparedModel, nullptr); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | // Execute and verify results. |
| 518 | generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; }, get_examples(), |
| 519 | testModel.relaxComputationFloat32toFloat16, |
| 520 | /*testDynamicOutputShape=*/false); |
| 521 | } |
| 522 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 523 | TEST_P(CompilationCachingTest, CacheSavingAndRetrievalNonZeroOffset) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 524 | // Create test HIDL model and compile. |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 525 | const Model testModel = createTestModel(); |
| 526 | if (checkEarlyTermination(testModel)) return; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 527 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 528 | |
| 529 | // Save the compilation to cache. |
| 530 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 531 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 532 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 533 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 534 | uint8_t dummyBytes[] = {0, 0}; |
| 535 | // Write a dummy integer to the cache. |
| 536 | // The driver should be able to handle non-empty cache and non-zero fd offset. |
| 537 | for (uint32_t i = 0; i < modelCache.size(); i++) { |
| 538 | ASSERT_EQ(write(modelCache[i].getNativeHandle()->data[0], &dummyBytes, |
| 539 | sizeof(dummyBytes)), |
| 540 | sizeof(dummyBytes)); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 541 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 542 | for (uint32_t i = 0; i < dataCache.size(); i++) { |
| 543 | ASSERT_EQ( |
| 544 | write(dataCache[i].getNativeHandle()->data[0], &dummyBytes, sizeof(dummyBytes)), |
| 545 | sizeof(dummyBytes)); |
| 546 | } |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 547 | saveModelToCache(testModel, modelCache, dataCache); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | // Retrieve preparedModel from cache. |
| 551 | { |
| 552 | preparedModel = nullptr; |
| 553 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 554 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 555 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 556 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 557 | uint8_t dummyByte = 0; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 558 | // Advance the offset of each handle by one byte. |
| 559 | // The driver should be able to handle non-zero fd offset. |
| 560 | for (uint32_t i = 0; i < modelCache.size(); i++) { |
| 561 | ASSERT_GE(read(modelCache[i].getNativeHandle()->data[0], &dummyByte, 1), 0); |
| 562 | } |
| 563 | for (uint32_t i = 0; i < dataCache.size(); i++) { |
| 564 | ASSERT_GE(read(dataCache[i].getNativeHandle()->data[0], &dummyByte, 1), 0); |
| 565 | } |
| 566 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 567 | if (!mIsCachingSupported) { |
| 568 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 569 | ASSERT_EQ(preparedModel, nullptr); |
| 570 | return; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 571 | } else if (checkEarlyTermination(status)) { |
| 572 | ASSERT_EQ(preparedModel, nullptr); |
| 573 | return; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 574 | } else { |
| 575 | ASSERT_EQ(status, ErrorStatus::NONE); |
| 576 | ASSERT_NE(preparedModel, nullptr); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | // Execute and verify results. |
| 581 | generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; }, get_examples(), |
| 582 | testModel.relaxComputationFloat32toFloat16, |
| 583 | /*testDynamicOutputShape=*/false); |
| 584 | } |
| 585 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 586 | TEST_P(CompilationCachingTest, SaveToCacheInvalidNumCache) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 587 | // Create test HIDL model and compile. |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 588 | const Model testModel = createTestModel(); |
| 589 | if (checkEarlyTermination(testModel)) return; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 590 | |
| 591 | // Test with number of model cache files greater than mNumModelCache. |
| 592 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 593 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 594 | // Pass an additional cache file for model cache. |
| 595 | mModelCache.push_back({mTmpCache}); |
| 596 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 597 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 598 | mModelCache.pop_back(); |
| 599 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 600 | saveModelToCache(testModel, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 601 | ASSERT_NE(preparedModel, nullptr); |
| 602 | // Execute and verify results. |
| 603 | generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; }, |
| 604 | get_examples(), |
| 605 | testModel.relaxComputationFloat32toFloat16, |
| 606 | /*testDynamicOutputShape=*/false); |
| 607 | // Check if prepareModelFromCache fails. |
| 608 | preparedModel = nullptr; |
| 609 | ErrorStatus status; |
| 610 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 611 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 612 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 613 | } |
| 614 | ASSERT_EQ(preparedModel, nullptr); |
| 615 | } |
| 616 | |
| 617 | // Test with number of model cache files smaller than mNumModelCache. |
| 618 | if (mModelCache.size() > 0) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 619 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 620 | // Pop out the last cache file. |
| 621 | auto tmp = mModelCache.back(); |
| 622 | mModelCache.pop_back(); |
| 623 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 624 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 625 | mModelCache.push_back(tmp); |
| 626 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 627 | saveModelToCache(testModel, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 628 | ASSERT_NE(preparedModel, nullptr); |
| 629 | // Execute and verify results. |
| 630 | generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; }, |
| 631 | get_examples(), |
| 632 | testModel.relaxComputationFloat32toFloat16, |
| 633 | /*testDynamicOutputShape=*/false); |
| 634 | // Check if prepareModelFromCache fails. |
| 635 | preparedModel = nullptr; |
| 636 | ErrorStatus status; |
| 637 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 638 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 639 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 640 | } |
| 641 | ASSERT_EQ(preparedModel, nullptr); |
| 642 | } |
| 643 | |
| 644 | // Test with number of data cache files greater than mNumDataCache. |
| 645 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 646 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 647 | // Pass an additional cache file for data cache. |
| 648 | mDataCache.push_back({mTmpCache}); |
| 649 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 650 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 651 | mDataCache.pop_back(); |
| 652 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 653 | saveModelToCache(testModel, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 654 | ASSERT_NE(preparedModel, nullptr); |
| 655 | // Execute and verify results. |
| 656 | generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; }, |
| 657 | get_examples(), |
| 658 | testModel.relaxComputationFloat32toFloat16, |
| 659 | /*testDynamicOutputShape=*/false); |
| 660 | // Check if prepareModelFromCache fails. |
| 661 | preparedModel = nullptr; |
| 662 | ErrorStatus status; |
| 663 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 664 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 665 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 666 | } |
| 667 | ASSERT_EQ(preparedModel, nullptr); |
| 668 | } |
| 669 | |
| 670 | // Test with number of data cache files smaller than mNumDataCache. |
| 671 | if (mDataCache.size() > 0) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 672 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 673 | // Pop out the last cache file. |
| 674 | auto tmp = mDataCache.back(); |
| 675 | mDataCache.pop_back(); |
| 676 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 677 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 678 | mDataCache.push_back(tmp); |
| 679 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 680 | saveModelToCache(testModel, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 681 | ASSERT_NE(preparedModel, nullptr); |
| 682 | // Execute and verify results. |
| 683 | generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; }, |
| 684 | get_examples(), |
| 685 | testModel.relaxComputationFloat32toFloat16, |
| 686 | /*testDynamicOutputShape=*/false); |
| 687 | // Check if prepareModelFromCache fails. |
| 688 | preparedModel = nullptr; |
| 689 | ErrorStatus status; |
| 690 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 691 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 692 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 693 | } |
| 694 | ASSERT_EQ(preparedModel, nullptr); |
| 695 | } |
| 696 | } |
| 697 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 698 | TEST_P(CompilationCachingTest, PrepareModelFromCacheInvalidNumCache) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 699 | // Create test HIDL model and compile. |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 700 | const Model testModel = createTestModel(); |
| 701 | if (checkEarlyTermination(testModel)) return; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 702 | |
| 703 | // Save the compilation to cache. |
| 704 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 705 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 706 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 707 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 708 | saveModelToCache(testModel, modelCache, dataCache); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | // Test with number of model cache files greater than mNumModelCache. |
| 712 | { |
| 713 | sp<IPreparedModel> preparedModel = nullptr; |
| 714 | ErrorStatus status; |
| 715 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 716 | mModelCache.push_back({mTmpCache}); |
| 717 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 718 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 719 | mModelCache.pop_back(); |
| 720 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 721 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 722 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
| 723 | } |
| 724 | ASSERT_EQ(preparedModel, nullptr); |
| 725 | } |
| 726 | |
| 727 | // Test with number of model cache files smaller than mNumModelCache. |
| 728 | if (mModelCache.size() > 0) { |
| 729 | sp<IPreparedModel> preparedModel = nullptr; |
| 730 | ErrorStatus status; |
| 731 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 732 | auto tmp = mModelCache.back(); |
| 733 | mModelCache.pop_back(); |
| 734 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 735 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 736 | mModelCache.push_back(tmp); |
| 737 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 738 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 739 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
| 740 | } |
| 741 | ASSERT_EQ(preparedModel, nullptr); |
| 742 | } |
| 743 | |
| 744 | // Test with number of data cache files greater than mNumDataCache. |
| 745 | { |
| 746 | sp<IPreparedModel> preparedModel = nullptr; |
| 747 | ErrorStatus status; |
| 748 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 749 | mDataCache.push_back({mTmpCache}); |
| 750 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 751 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 752 | mDataCache.pop_back(); |
| 753 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 754 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 755 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
| 756 | } |
| 757 | ASSERT_EQ(preparedModel, nullptr); |
| 758 | } |
| 759 | |
| 760 | // Test with number of data cache files smaller than mNumDataCache. |
| 761 | if (mDataCache.size() > 0) { |
| 762 | sp<IPreparedModel> preparedModel = nullptr; |
| 763 | ErrorStatus status; |
| 764 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 765 | auto tmp = mDataCache.back(); |
| 766 | mDataCache.pop_back(); |
| 767 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 768 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 769 | mDataCache.push_back(tmp); |
| 770 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 771 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 772 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
| 773 | } |
| 774 | ASSERT_EQ(preparedModel, nullptr); |
| 775 | } |
| 776 | } |
| 777 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 778 | TEST_P(CompilationCachingTest, SaveToCacheInvalidNumFd) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 779 | // Create test HIDL model and compile. |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 780 | const Model testModel = createTestModel(); |
| 781 | if (checkEarlyTermination(testModel)) return; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 782 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 783 | // Go through each handle in model cache, test with NumFd greater than 1. |
| 784 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 785 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 786 | // Pass an invalid number of fds for handle i. |
| 787 | mModelCache[i].push_back(mTmpCache); |
| 788 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 789 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 790 | mModelCache[i].pop_back(); |
| 791 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 792 | saveModelToCache(testModel, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 793 | ASSERT_NE(preparedModel, nullptr); |
| 794 | // Execute and verify results. |
| 795 | generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; }, |
| 796 | get_examples(), |
| 797 | testModel.relaxComputationFloat32toFloat16, |
| 798 | /*testDynamicOutputShape=*/false); |
| 799 | // Check if prepareModelFromCache fails. |
| 800 | preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 801 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 802 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 803 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 804 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 805 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 806 | ASSERT_EQ(preparedModel, nullptr); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 807 | } |
| 808 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 809 | // Go through each handle in model cache, test with NumFd equal to 0. |
| 810 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 811 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 812 | // Pass an invalid number of fds for handle i. |
| 813 | auto tmp = mModelCache[i].back(); |
| 814 | mModelCache[i].pop_back(); |
| 815 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 816 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 817 | mModelCache[i].push_back(tmp); |
| 818 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 819 | saveModelToCache(testModel, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 820 | ASSERT_NE(preparedModel, nullptr); |
| 821 | // Execute and verify results. |
| 822 | generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; }, |
| 823 | get_examples(), |
| 824 | testModel.relaxComputationFloat32toFloat16, |
| 825 | /*testDynamicOutputShape=*/false); |
| 826 | // Check if prepareModelFromCache fails. |
| 827 | preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 828 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 829 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 830 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 831 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 832 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 833 | ASSERT_EQ(preparedModel, nullptr); |
| 834 | } |
| 835 | |
| 836 | // Go through each handle in data cache, test with NumFd greater than 1. |
| 837 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 838 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 839 | // Pass an invalid number of fds for handle i. |
| 840 | mDataCache[i].push_back(mTmpCache); |
| 841 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 842 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 843 | mDataCache[i].pop_back(); |
| 844 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 845 | saveModelToCache(testModel, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 846 | ASSERT_NE(preparedModel, nullptr); |
| 847 | // Execute and verify results. |
| 848 | generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; }, |
| 849 | get_examples(), |
| 850 | testModel.relaxComputationFloat32toFloat16, |
| 851 | /*testDynamicOutputShape=*/false); |
| 852 | // Check if prepareModelFromCache fails. |
| 853 | preparedModel = nullptr; |
| 854 | ErrorStatus status; |
| 855 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 856 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 857 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 858 | } |
| 859 | ASSERT_EQ(preparedModel, nullptr); |
| 860 | } |
| 861 | |
| 862 | // Go through each handle in data cache, test with NumFd equal to 0. |
| 863 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 864 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 865 | // Pass an invalid number of fds for handle i. |
| 866 | auto tmp = mDataCache[i].back(); |
| 867 | mDataCache[i].pop_back(); |
| 868 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 869 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 870 | mDataCache[i].push_back(tmp); |
| 871 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 872 | saveModelToCache(testModel, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 873 | ASSERT_NE(preparedModel, nullptr); |
| 874 | // Execute and verify results. |
| 875 | generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; }, |
| 876 | get_examples(), |
| 877 | testModel.relaxComputationFloat32toFloat16, |
| 878 | /*testDynamicOutputShape=*/false); |
| 879 | // Check if prepareModelFromCache fails. |
| 880 | preparedModel = nullptr; |
| 881 | ErrorStatus status; |
| 882 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 883 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 884 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 885 | } |
| 886 | ASSERT_EQ(preparedModel, nullptr); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 890 | TEST_P(CompilationCachingTest, PrepareModelFromCacheInvalidNumFd) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 891 | // Create test HIDL model and compile. |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 892 | const Model testModel = createTestModel(); |
| 893 | if (checkEarlyTermination(testModel)) return; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 894 | |
| 895 | // Save the compilation to cache. |
| 896 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 897 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 898 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 899 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 900 | saveModelToCache(testModel, modelCache, dataCache); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 901 | } |
| 902 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 903 | // Go through each handle in model cache, test with NumFd greater than 1. |
| 904 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
| 905 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 906 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 907 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 908 | mModelCache[i].push_back(mTmpCache); |
| 909 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 910 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 911 | mModelCache[i].pop_back(); |
| 912 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 913 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 914 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 915 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 916 | ASSERT_EQ(preparedModel, nullptr); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 917 | } |
| 918 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 919 | // Go through each handle in model cache, test with NumFd equal to 0. |
| 920 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
| 921 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 922 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 923 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 924 | auto tmp = mModelCache[i].back(); |
| 925 | mModelCache[i].pop_back(); |
| 926 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 927 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 928 | mModelCache[i].push_back(tmp); |
| 929 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 930 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 931 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 932 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 933 | ASSERT_EQ(preparedModel, nullptr); |
| 934 | } |
| 935 | |
| 936 | // Go through each handle in data cache, test with NumFd greater than 1. |
| 937 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
| 938 | sp<IPreparedModel> preparedModel = nullptr; |
| 939 | ErrorStatus status; |
| 940 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 941 | mDataCache[i].push_back(mTmpCache); |
| 942 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 943 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 944 | mDataCache[i].pop_back(); |
| 945 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 946 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 947 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
| 948 | } |
| 949 | ASSERT_EQ(preparedModel, nullptr); |
| 950 | } |
| 951 | |
| 952 | // Go through each handle in data cache, test with NumFd equal to 0. |
| 953 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
| 954 | sp<IPreparedModel> preparedModel = nullptr; |
| 955 | ErrorStatus status; |
| 956 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 957 | auto tmp = mDataCache[i].back(); |
| 958 | mDataCache[i].pop_back(); |
| 959 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 960 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 961 | mDataCache[i].push_back(tmp); |
| 962 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 963 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 964 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
| 965 | } |
| 966 | ASSERT_EQ(preparedModel, nullptr); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 970 | TEST_P(CompilationCachingTest, SaveToCacheInvalidAccessMode) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 971 | // Create test HIDL model and compile. |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 972 | const Model testModel = createTestModel(); |
| 973 | if (checkEarlyTermination(testModel)) return; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 974 | std::vector<AccessMode> modelCacheMode(mNumModelCache, AccessMode::READ_WRITE); |
| 975 | std::vector<AccessMode> dataCacheMode(mNumDataCache, AccessMode::READ_WRITE); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 976 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 977 | // Go through each handle in model cache, test with invalid access mode. |
| 978 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 979 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 980 | modelCacheMode[i] = AccessMode::READ_ONLY; |
| 981 | createCacheHandles(mModelCache, modelCacheMode, &modelCache); |
| 982 | createCacheHandles(mDataCache, dataCacheMode, &dataCache); |
| 983 | modelCacheMode[i] = AccessMode::READ_WRITE; |
| 984 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 985 | saveModelToCache(testModel, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 986 | ASSERT_NE(preparedModel, nullptr); |
| 987 | // Execute and verify results. |
| 988 | generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; }, |
| 989 | get_examples(), |
| 990 | testModel.relaxComputationFloat32toFloat16, |
| 991 | /*testDynamicOutputShape=*/false); |
| 992 | // Check if prepareModelFromCache fails. |
| 993 | preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 994 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 995 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 996 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 997 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 998 | } |
| 999 | ASSERT_EQ(preparedModel, nullptr); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1002 | // Go through each handle in data cache, test with invalid access mode. |
| 1003 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1004 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1005 | dataCacheMode[i] = AccessMode::READ_ONLY; |
| 1006 | createCacheHandles(mModelCache, modelCacheMode, &modelCache); |
| 1007 | createCacheHandles(mDataCache, dataCacheMode, &dataCache); |
| 1008 | dataCacheMode[i] = AccessMode::READ_WRITE; |
| 1009 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1010 | saveModelToCache(testModel, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1011 | ASSERT_NE(preparedModel, nullptr); |
| 1012 | // Execute and verify results. |
| 1013 | generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; }, |
| 1014 | get_examples(), |
| 1015 | testModel.relaxComputationFloat32toFloat16, |
| 1016 | /*testDynamicOutputShape=*/false); |
| 1017 | // Check if prepareModelFromCache fails. |
| 1018 | preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1019 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1020 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 1021 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 1022 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 1023 | } |
| 1024 | ASSERT_EQ(preparedModel, nullptr); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1028 | TEST_P(CompilationCachingTest, PrepareModelFromCacheInvalidAccessMode) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1029 | // Create test HIDL model and compile. |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1030 | const Model testModel = createTestModel(); |
| 1031 | if (checkEarlyTermination(testModel)) return; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1032 | std::vector<AccessMode> modelCacheMode(mNumModelCache, AccessMode::READ_WRITE); |
| 1033 | std::vector<AccessMode> dataCacheMode(mNumDataCache, AccessMode::READ_WRITE); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1034 | |
| 1035 | // Save the compilation to cache. |
| 1036 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1037 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1038 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1039 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1040 | saveModelToCache(testModel, modelCache, dataCache); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1041 | } |
| 1042 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1043 | // Go through each handle in model cache, test with invalid access mode. |
| 1044 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
| 1045 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1046 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1047 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1048 | modelCacheMode[i] = AccessMode::WRITE_ONLY; |
| 1049 | createCacheHandles(mModelCache, modelCacheMode, &modelCache); |
| 1050 | createCacheHandles(mDataCache, dataCacheMode, &dataCache); |
| 1051 | modelCacheMode[i] = AccessMode::READ_WRITE; |
| 1052 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1053 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 1054 | ASSERT_EQ(preparedModel, nullptr); |
| 1055 | } |
| 1056 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1057 | // Go through each handle in data cache, test with invalid access mode. |
| 1058 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
| 1059 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1060 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1061 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1062 | dataCacheMode[i] = AccessMode::WRITE_ONLY; |
| 1063 | createCacheHandles(mModelCache, modelCacheMode, &modelCache); |
| 1064 | createCacheHandles(mDataCache, dataCacheMode, &dataCache); |
| 1065 | dataCacheMode[i] = AccessMode::READ_WRITE; |
| 1066 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1067 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 1068 | ASSERT_EQ(preparedModel, nullptr); |
| 1069 | } |
| 1070 | } |
| 1071 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1072 | // Copy file contents between file groups. |
| 1073 | // The outer vector corresponds to handles and the inner vector is for fds held by each handle. |
| 1074 | // The outer vector sizes must match and the inner vectors must have size = 1. |
| 1075 | static void copyCacheFiles(const std::vector<std::vector<std::string>>& from, |
| 1076 | const std::vector<std::vector<std::string>>& to) { |
| 1077 | constexpr size_t kBufferSize = 1000000; |
| 1078 | uint8_t buffer[kBufferSize]; |
| 1079 | |
| 1080 | ASSERT_EQ(from.size(), to.size()); |
| 1081 | for (uint32_t i = 0; i < from.size(); i++) { |
| 1082 | ASSERT_EQ(from[i].size(), 1u); |
| 1083 | ASSERT_EQ(to[i].size(), 1u); |
| 1084 | int fromFd = open(from[i][0].c_str(), O_RDONLY); |
| 1085 | int toFd = open(to[i][0].c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); |
| 1086 | ASSERT_GE(fromFd, 0); |
| 1087 | ASSERT_GE(toFd, 0); |
| 1088 | |
| 1089 | ssize_t readBytes; |
| 1090 | while ((readBytes = read(fromFd, &buffer, kBufferSize)) > 0) { |
| 1091 | ASSERT_EQ(write(toFd, &buffer, readBytes), readBytes); |
| 1092 | } |
| 1093 | ASSERT_GE(readBytes, 0); |
| 1094 | |
| 1095 | close(fromFd); |
| 1096 | close(toFd); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | // Number of operations in the large test model. |
| 1101 | constexpr uint32_t kLargeModelSize = 100; |
| 1102 | constexpr uint32_t kNumIterationsTOCTOU = 100; |
| 1103 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1104 | TEST_P(CompilationCachingTest, SaveToCache_TOCTOU) { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1105 | if (!mIsCachingSupported) return; |
| 1106 | |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1107 | // Create test models and check if fully supported by the service. |
| 1108 | const Model testModelMul = createLargeTestModel(OperationType::MUL, kLargeModelSize); |
| 1109 | if (checkEarlyTermination(testModelMul)) return; |
| 1110 | const Model testModelAdd = createLargeTestModel(OperationType::ADD, kLargeModelSize); |
| 1111 | if (checkEarlyTermination(testModelAdd)) return; |
| 1112 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1113 | // Save the testModelMul compilation to cache. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1114 | auto modelCacheMul = mModelCache; |
| 1115 | for (auto& cache : modelCacheMul) { |
| 1116 | cache[0].append("_mul"); |
| 1117 | } |
| 1118 | { |
| 1119 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1120 | createCacheHandles(modelCacheMul, AccessMode::READ_WRITE, &modelCache); |
| 1121 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1122 | saveModelToCache(testModelMul, modelCache, dataCache); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | // Use a different token for testModelAdd. |
| 1126 | mToken[0]++; |
| 1127 | |
| 1128 | // This test is probabilistic, so we run it multiple times. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1129 | for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) { |
| 1130 | // Save the testModelAdd compilation to cache. |
| 1131 | { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1132 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1133 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1134 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 1135 | |
| 1136 | // Spawn a thread to copy the cache content concurrently while saving to cache. |
| 1137 | std::thread thread(copyCacheFiles, std::cref(modelCacheMul), std::cref(mModelCache)); |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1138 | saveModelToCache(testModelAdd, modelCache, dataCache); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1139 | thread.join(); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | // Retrieve preparedModel from cache. |
| 1143 | { |
| 1144 | sp<IPreparedModel> preparedModel = nullptr; |
| 1145 | ErrorStatus status; |
| 1146 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1147 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1148 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 1149 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 1150 | |
| 1151 | // The preparation may fail or succeed, but must not crash. If the preparation succeeds, |
| 1152 | // the prepared model must be executed with the correct result and not crash. |
| 1153 | if (status != ErrorStatus::NONE) { |
| 1154 | ASSERT_EQ(preparedModel, nullptr); |
| 1155 | } else { |
| 1156 | ASSERT_NE(preparedModel, nullptr); |
| 1157 | generated_tests::EvaluatePreparedModel( |
| 1158 | preparedModel, [](int) { return false; }, |
| 1159 | getLargeModelExamples(kLargeModelSize), |
| 1160 | testModelAdd.relaxComputationFloat32toFloat16, |
| 1161 | /*testDynamicOutputShape=*/false); |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1167 | TEST_P(CompilationCachingTest, PrepareFromCache_TOCTOU) { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1168 | if (!mIsCachingSupported) return; |
| 1169 | |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1170 | // Create test models and check if fully supported by the service. |
| 1171 | const Model testModelMul = createLargeTestModel(OperationType::MUL, kLargeModelSize); |
| 1172 | if (checkEarlyTermination(testModelMul)) return; |
| 1173 | const Model testModelAdd = createLargeTestModel(OperationType::ADD, kLargeModelSize); |
| 1174 | if (checkEarlyTermination(testModelAdd)) return; |
| 1175 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1176 | // Save the testModelMul compilation to cache. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1177 | auto modelCacheMul = mModelCache; |
| 1178 | for (auto& cache : modelCacheMul) { |
| 1179 | cache[0].append("_mul"); |
| 1180 | } |
| 1181 | { |
| 1182 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1183 | createCacheHandles(modelCacheMul, AccessMode::READ_WRITE, &modelCache); |
| 1184 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1185 | saveModelToCache(testModelMul, modelCache, dataCache); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | // Use a different token for testModelAdd. |
| 1189 | mToken[0]++; |
| 1190 | |
| 1191 | // This test is probabilistic, so we run it multiple times. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1192 | for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) { |
| 1193 | // Save the testModelAdd compilation to cache. |
| 1194 | { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1195 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1196 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1197 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1198 | saveModelToCache(testModelAdd, modelCache, dataCache); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | // Retrieve preparedModel from cache. |
| 1202 | { |
| 1203 | sp<IPreparedModel> preparedModel = nullptr; |
| 1204 | ErrorStatus status; |
| 1205 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1206 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1207 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 1208 | |
| 1209 | // Spawn a thread to copy the cache content concurrently while preparing from cache. |
| 1210 | std::thread thread(copyCacheFiles, std::cref(modelCacheMul), std::cref(mModelCache)); |
| 1211 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 1212 | thread.join(); |
| 1213 | |
| 1214 | // The preparation may fail or succeed, but must not crash. If the preparation succeeds, |
| 1215 | // the prepared model must be executed with the correct result and not crash. |
| 1216 | if (status != ErrorStatus::NONE) { |
| 1217 | ASSERT_EQ(preparedModel, nullptr); |
| 1218 | } else { |
| 1219 | ASSERT_NE(preparedModel, nullptr); |
| 1220 | generated_tests::EvaluatePreparedModel( |
| 1221 | preparedModel, [](int) { return false; }, |
| 1222 | getLargeModelExamples(kLargeModelSize), |
| 1223 | testModelAdd.relaxComputationFloat32toFloat16, |
| 1224 | /*testDynamicOutputShape=*/false); |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1230 | TEST_P(CompilationCachingTest, ReplaceSecuritySensitiveCache) { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1231 | if (!mIsCachingSupported) return; |
| 1232 | |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1233 | // Create test models and check if fully supported by the service. |
| 1234 | const Model testModelMul = createLargeTestModel(OperationType::MUL, kLargeModelSize); |
| 1235 | if (checkEarlyTermination(testModelMul)) return; |
| 1236 | const Model testModelAdd = createLargeTestModel(OperationType::ADD, kLargeModelSize); |
| 1237 | if (checkEarlyTermination(testModelAdd)) return; |
| 1238 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1239 | // Save the testModelMul compilation to cache. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1240 | auto modelCacheMul = mModelCache; |
| 1241 | for (auto& cache : modelCacheMul) { |
| 1242 | cache[0].append("_mul"); |
| 1243 | } |
| 1244 | { |
| 1245 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1246 | createCacheHandles(modelCacheMul, AccessMode::READ_WRITE, &modelCache); |
| 1247 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1248 | saveModelToCache(testModelMul, modelCache, dataCache); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | // Use a different token for testModelAdd. |
| 1252 | mToken[0]++; |
| 1253 | |
| 1254 | // Save the testModelAdd compilation to cache. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1255 | { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1256 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1257 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1258 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1259 | saveModelToCache(testModelAdd, modelCache, dataCache); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | // Replace the model cache of testModelAdd with testModelMul. |
| 1263 | copyCacheFiles(modelCacheMul, mModelCache); |
| 1264 | |
| 1265 | // Retrieve the preparedModel from cache, expect failure. |
| 1266 | { |
| 1267 | sp<IPreparedModel> preparedModel = nullptr; |
| 1268 | ErrorStatus status; |
| 1269 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1270 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1271 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 1272 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 1273 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 1274 | ASSERT_EQ(preparedModel, nullptr); |
| 1275 | } |
| 1276 | } |
| 1277 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1278 | static const auto kOperandTypeChoices = |
| 1279 | ::testing::Values(OperandType::TENSOR_FLOAT32, OperandType::TENSOR_QUANT8_ASYMM); |
| 1280 | |
| 1281 | INSTANTIATE_TEST_CASE_P(TestCompilationCaching, CompilationCachingTest, kOperandTypeChoices); |
| 1282 | |
| 1283 | class CompilationCachingSecurityTest |
| 1284 | : public CompilationCachingTestBase, |
| 1285 | public ::testing::WithParamInterface<std::tuple<OperandType, uint32_t>> { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1286 | protected: |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1287 | CompilationCachingSecurityTest() : CompilationCachingTestBase(std::get<0>(GetParam())) {} |
| 1288 | |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1289 | void SetUp() { |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1290 | CompilationCachingTestBase::SetUp(); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1291 | generator.seed(kSeed); |
| 1292 | } |
| 1293 | |
| 1294 | // Get a random integer within a closed range [lower, upper]. |
| 1295 | template <typename T> |
| 1296 | T getRandomInt(T lower, T upper) { |
| 1297 | std::uniform_int_distribution<T> dis(lower, upper); |
| 1298 | return dis(generator); |
| 1299 | } |
| 1300 | |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1301 | // Randomly flip one single bit of the cache entry. |
| 1302 | void flipOneBitOfCache(const std::string& filename, bool* skip) { |
| 1303 | FILE* pFile = fopen(filename.c_str(), "r+"); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1304 | ASSERT_EQ(fseek(pFile, 0, SEEK_END), 0); |
| 1305 | long int fileSize = ftell(pFile); |
| 1306 | if (fileSize == 0) { |
| 1307 | fclose(pFile); |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1308 | *skip = true; |
| 1309 | return; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1310 | } |
| 1311 | ASSERT_EQ(fseek(pFile, getRandomInt(0l, fileSize - 1), SEEK_SET), 0); |
| 1312 | int readByte = fgetc(pFile); |
| 1313 | ASSERT_NE(readByte, EOF); |
| 1314 | ASSERT_EQ(fseek(pFile, -1, SEEK_CUR), 0); |
| 1315 | ASSERT_NE(fputc(static_cast<uint8_t>(readByte) ^ (1U << getRandomInt(0, 7)), pFile), EOF); |
| 1316 | fclose(pFile); |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1317 | *skip = false; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1318 | } |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1319 | |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1320 | // Randomly append bytes to the cache entry. |
| 1321 | void appendBytesToCache(const std::string& filename, bool* skip) { |
| 1322 | FILE* pFile = fopen(filename.c_str(), "a"); |
| 1323 | uint32_t appendLength = getRandomInt(1, 256); |
| 1324 | for (uint32_t i = 0; i < appendLength; i++) { |
| 1325 | ASSERT_NE(fputc(getRandomInt<uint8_t>(0, 255), pFile), EOF); |
| 1326 | } |
| 1327 | fclose(pFile); |
| 1328 | *skip = false; |
| 1329 | } |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1330 | |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1331 | enum class ExpectedResult { GENERAL_FAILURE, NOT_CRASH }; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1332 | |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1333 | // Test if the driver behaves as expected when given corrupted cache or token. |
| 1334 | // The modifier will be invoked after save to cache but before prepare from cache. |
| 1335 | // The modifier accepts one pointer argument "skip" as the returning value, indicating |
| 1336 | // whether the test should be skipped or not. |
| 1337 | void testCorruptedCache(ExpectedResult expected, std::function<void(bool*)> modifier) { |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1338 | const Model testModel = createTestModel(); |
| 1339 | if (checkEarlyTermination(testModel)) return; |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1340 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1341 | // Save the compilation to cache. |
| 1342 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1343 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1344 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1345 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1346 | saveModelToCache(testModel, modelCache, dataCache); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1347 | } |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1348 | |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1349 | bool skip = false; |
| 1350 | modifier(&skip); |
| 1351 | if (skip) return; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1352 | |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1353 | // Retrieve preparedModel from cache. |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1354 | { |
| 1355 | sp<IPreparedModel> preparedModel = nullptr; |
| 1356 | ErrorStatus status; |
| 1357 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1358 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1359 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 1360 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1361 | |
| 1362 | switch (expected) { |
| 1363 | case ExpectedResult::GENERAL_FAILURE: |
| 1364 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 1365 | ASSERT_EQ(preparedModel, nullptr); |
| 1366 | break; |
| 1367 | case ExpectedResult::NOT_CRASH: |
| 1368 | ASSERT_EQ(preparedModel == nullptr, status != ErrorStatus::NONE); |
| 1369 | break; |
| 1370 | default: |
| 1371 | FAIL(); |
| 1372 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1373 | } |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1374 | } |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1375 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1376 | const uint32_t kSeed = std::get<1>(GetParam()); |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1377 | std::mt19937 generator; |
| 1378 | }; |
| 1379 | |
| 1380 | TEST_P(CompilationCachingSecurityTest, CorruptedModelCache) { |
| 1381 | if (!mIsCachingSupported) return; |
| 1382 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
| 1383 | testCorruptedCache(ExpectedResult::GENERAL_FAILURE, |
| 1384 | [this, i](bool* skip) { flipOneBitOfCache(mModelCache[i][0], skip); }); |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | TEST_P(CompilationCachingSecurityTest, WrongLengthModelCache) { |
| 1389 | if (!mIsCachingSupported) return; |
| 1390 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
| 1391 | testCorruptedCache(ExpectedResult::GENERAL_FAILURE, |
| 1392 | [this, i](bool* skip) { appendBytesToCache(mModelCache[i][0], skip); }); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | TEST_P(CompilationCachingSecurityTest, CorruptedDataCache) { |
| 1397 | if (!mIsCachingSupported) return; |
| 1398 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
| 1399 | testCorruptedCache(ExpectedResult::NOT_CRASH, |
| 1400 | [this, i](bool* skip) { flipOneBitOfCache(mDataCache[i][0], skip); }); |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | TEST_P(CompilationCachingSecurityTest, WrongLengthDataCache) { |
| 1405 | if (!mIsCachingSupported) return; |
| 1406 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
| 1407 | testCorruptedCache(ExpectedResult::NOT_CRASH, |
| 1408 | [this, i](bool* skip) { appendBytesToCache(mDataCache[i][0], skip); }); |
| 1409 | } |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | TEST_P(CompilationCachingSecurityTest, WrongToken) { |
| 1413 | if (!mIsCachingSupported) return; |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1414 | testCorruptedCache(ExpectedResult::GENERAL_FAILURE, [this](bool* skip) { |
| 1415 | // Randomly flip one single bit in mToken. |
| 1416 | uint32_t ind = |
| 1417 | getRandomInt(0u, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN) - 1); |
| 1418 | mToken[ind] ^= (1U << getRandomInt(0, 7)); |
| 1419 | *skip = false; |
| 1420 | }); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | INSTANTIATE_TEST_CASE_P(TestCompilationCaching, CompilationCachingSecurityTest, |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1424 | ::testing::Combine(kOperandTypeChoices, ::testing::Range(0U, 10U))); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1425 | |
| 1426 | } // namespace functional |
| 1427 | } // namespace vts |
| 1428 | } // namespace V1_2 |
| 1429 | } // namespace neuralnetworks |
| 1430 | } // namespace hardware |
| 1431 | } // namespace android |