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