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