|
5 | 5 | #include "hipdnn_backend.h" |
6 | 6 | #include <array> |
7 | 7 | #include <filesystem> |
| 8 | +#include <string> |
| 9 | +#include <vector> |
| 10 | + |
8 | 11 | #include <gtest/gtest.h> |
9 | 12 | #include <hipdnn_data_sdk/utilities/PlatformUtils.hpp> |
10 | 13 | #include <hipdnn_flatbuffers_sdk/data_objects/graph_generated.h> |
11 | 14 | #include <hipdnn_test_sdk/utilities/ScopedEnvironmentVariableSetter.hpp> |
12 | | -#include <vector> |
13 | 15 |
|
14 | 16 | using namespace hipdnn_data_sdk::utilities; |
15 | 17 | using namespace hipdnn_tests::plugin_constants; |
16 | 18 | namespace fs = std::filesystem; |
17 | 19 |
|
| 20 | +namespace |
| 21 | +{ |
| 22 | +struct HeuristicPolicyInfoForTest |
| 23 | +{ |
| 24 | + std::string policyName; |
| 25 | + std::string pluginName; |
| 26 | +}; |
| 27 | + |
| 28 | +bool getHeuristicPolicyInfos(hipdnnHandle_t handle, |
| 29 | + std::vector<HeuristicPolicyInfoForTest>& policyInfos) |
| 30 | +{ |
| 31 | + size_t numPolicies = 0; |
| 32 | + auto status = hipdnnGetHeuristicPolicyCount_ext(handle, &numPolicies); |
| 33 | + if(status != HIPDNN_STATUS_SUCCESS) |
| 34 | + { |
| 35 | + ADD_FAILURE() << "Failed to query heuristic policy count: " << status; |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + policyInfos.clear(); |
| 40 | + policyInfos.reserve(numPolicies); |
| 41 | + |
| 42 | + for(size_t i = 0; i < numPolicies; ++i) |
| 43 | + { |
| 44 | + size_t policyNameLen = 0; |
| 45 | + size_t pluginNameLen = 0; |
| 46 | + size_t pluginVersionLen = 0; |
| 47 | + size_t apiVersionLen = 0; |
| 48 | + status = hipdnnGetHeuristicPolicyInfo_ext(handle, |
| 49 | + i, |
| 50 | + nullptr, |
| 51 | + nullptr, |
| 52 | + &policyNameLen, |
| 53 | + nullptr, |
| 54 | + &pluginNameLen, |
| 55 | + nullptr, |
| 56 | + &pluginVersionLen, |
| 57 | + nullptr, |
| 58 | + &apiVersionLen); |
| 59 | + if(status != HIPDNN_STATUS_SUCCESS) |
| 60 | + { |
| 61 | + ADD_FAILURE() << "Failed to query heuristic policy info sizes for index " << i << ": " |
| 62 | + << status; |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + if(policyNameLen == 0 || pluginNameLen == 0 || pluginVersionLen == 0 || apiVersionLen == 0) |
| 67 | + { |
| 68 | + ADD_FAILURE() << "Heuristic policy info for index " << i |
| 69 | + << " reported an empty metadata field"; |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + std::vector<char> policyName(policyNameLen); |
| 74 | + std::vector<char> pluginName(pluginNameLen); |
| 75 | + std::vector<char> pluginVersion(pluginVersionLen); |
| 76 | + std::vector<char> apiVersion(apiVersionLen); |
| 77 | + status = hipdnnGetHeuristicPolicyInfo_ext(handle, |
| 78 | + i, |
| 79 | + nullptr, |
| 80 | + policyName.data(), |
| 81 | + &policyNameLen, |
| 82 | + pluginName.data(), |
| 83 | + &pluginNameLen, |
| 84 | + pluginVersion.data(), |
| 85 | + &pluginVersionLen, |
| 86 | + apiVersion.data(), |
| 87 | + &apiVersionLen); |
| 88 | + if(status != HIPDNN_STATUS_SUCCESS) |
| 89 | + { |
| 90 | + ADD_FAILURE() << "Failed to retrieve heuristic policy info for index " << i << ": " |
| 91 | + << status; |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + policyInfos.push_back({policyName.data(), pluginName.data()}); |
| 96 | + } |
| 97 | + |
| 98 | + return true; |
| 99 | +} |
| 100 | + |
| 101 | +bool heuristicPolicyIsLoaded(hipdnnHandle_t handle, const char* expectedPolicyName) |
| 102 | +{ |
| 103 | + std::vector<HeuristicPolicyInfoForTest> policyInfos; |
| 104 | + if(!getHeuristicPolicyInfos(handle, policyInfos)) |
| 105 | + { |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + for(const auto& policyInfo : policyInfos) |
| 110 | + { |
| 111 | + if(policyInfo.policyName == expectedPolicyName) |
| 112 | + { |
| 113 | + return true; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return false; |
| 118 | +} |
| 119 | + |
| 120 | +bool onlyBuiltInHeuristicPoliciesAreLoaded(hipdnnHandle_t handle) |
| 121 | +{ |
| 122 | + std::vector<HeuristicPolicyInfoForTest> policyInfos; |
| 123 | + if(!getHeuristicPolicyInfos(handle, policyInfos)) |
| 124 | + { |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + for(const auto& policyInfo : policyInfos) |
| 129 | + { |
| 130 | + if(policyInfo.pluginName.rfind("BuiltIn", 0) != 0) |
| 131 | + { |
| 132 | + ADD_FAILURE() << "Loaded path-discovered heuristic policy '" << policyInfo.policyName |
| 133 | + << "' from plugin '" << policyInfo.pluginName << "'"; |
| 134 | + return false; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return true; |
| 139 | +} |
| 140 | +} // namespace |
| 141 | + |
18 | 142 | TEST(IntegrationSetPluginPathsExt, ValidInputs) |
19 | 143 | { |
20 | 144 | std::array<const char*, 3> paths = {getTestPluginCustomDir().c_str(), "./", "../directory/"}; |
@@ -45,6 +169,70 @@ TEST(IntegrationSetPluginPathsExt, InvalidAndValidNullptrCorrectness) |
45 | 169 | EXPECT_EQ(hipdnnDestroy(handle), HIPDNN_STATUS_SUCCESS); |
46 | 170 | } |
47 | 171 |
|
| 172 | +TEST(IntegrationSetPluginPathsExt, HeuristicInvalidAndValidNullptrCorrectness) |
| 173 | +{ |
| 174 | + const hipdnn_test_sdk::utilities::ScopedEnvironmentVariableSetter envSetter( |
| 175 | + "HIPDNN_HEURISTIC_PLUGIN_DIR", |
| 176 | + fs::path(testGoodHeuristicPluginPath()).parent_path().string()); |
| 177 | + |
| 178 | + hipdnnStatus_t status |
| 179 | + = hipdnnSetHeuristicPluginPaths_ext(1, nullptr, HIPDNN_PLUGIN_LOADING_ABSOLUTE); |
| 180 | + EXPECT_EQ(status, HIPDNN_STATUS_BAD_PARAM_NULL_POINTER); |
| 181 | + |
| 182 | + status = hipdnnSetHeuristicPluginPaths_ext(0, nullptr, HIPDNN_PLUGIN_LOADING_ABSOLUTE); |
| 183 | + EXPECT_EQ(status, HIPDNN_STATUS_SUCCESS); |
| 184 | + |
| 185 | + hipdnnHandle_t handle = nullptr; |
| 186 | + status = hipdnnCreate(&handle); |
| 187 | + ASSERT_EQ(status, HIPDNN_STATUS_SUCCESS); |
| 188 | + ASSERT_NE(handle, nullptr); |
| 189 | + |
| 190 | + // Built-in heuristic policies are always registered. Empty ABSOLUTE paths should suppress |
| 191 | + // only path-discovered external heuristic plugins, including the env-sourced test plugin. |
| 192 | + EXPECT_TRUE(onlyBuiltInHeuristicPoliciesAreLoaded(handle)); |
| 193 | + |
| 194 | + EXPECT_EQ(hipdnnDestroy(handle), HIPDNN_STATUS_SUCCESS); |
| 195 | + EXPECT_EQ(hipdnnSetHeuristicPluginPaths_ext(0, nullptr, HIPDNN_PLUGIN_LOADING_ADDITIVE), |
| 196 | + HIPDNN_STATUS_SUCCESS); |
| 197 | +} |
| 198 | + |
| 199 | +TEST(IntegrationSetPluginPathsExt, HeuristicNullStringInList) |
| 200 | +{ |
| 201 | + std::array<const char*, 2> paths = {testGoodHeuristicPluginPath().c_str(), nullptr}; |
| 202 | + |
| 203 | + const hipdnnStatus_t status = hipdnnSetHeuristicPluginPaths_ext( |
| 204 | + paths.size(), paths.data(), HIPDNN_PLUGIN_LOADING_ABSOLUTE); |
| 205 | + |
| 206 | + EXPECT_EQ(status, HIPDNN_STATUS_BAD_PARAM_NULL_POINTER); |
| 207 | +} |
| 208 | + |
| 209 | +TEST(IntegrationSetPluginPathsExt, HeuristicEmptyAdditiveLoadsDefault) |
| 210 | +{ |
| 211 | + const hipdnn_test_sdk::utilities::ScopedEnvironmentVariableSetter envSetter( |
| 212 | + "HIPDNN_HEURISTIC_PLUGIN_DIR", |
| 213 | + fs::path(testGoodHeuristicPluginPath()).parent_path().string()); |
| 214 | + |
| 215 | + hipdnnStatus_t status |
| 216 | + = hipdnnSetHeuristicPluginPaths_ext(0, nullptr, HIPDNN_PLUGIN_LOADING_ABSOLUTE); |
| 217 | + ASSERT_EQ(status, HIPDNN_STATUS_SUCCESS); |
| 218 | + |
| 219 | + status = hipdnnSetHeuristicPluginPaths_ext(0, nullptr, HIPDNN_PLUGIN_LOADING_ADDITIVE); |
| 220 | + ASSERT_EQ(status, HIPDNN_STATUS_SUCCESS); |
| 221 | + |
| 222 | + hipdnnHandle_t handle = nullptr; |
| 223 | + status = hipdnnCreate(&handle); |
| 224 | + ASSERT_EQ(status, HIPDNN_STATUS_SUCCESS); |
| 225 | + ASSERT_NE(handle, nullptr); |
| 226 | + |
| 227 | + EXPECT_TRUE(heuristicPolicyIsLoaded(handle, testGoodHeuristicPolicyName())); |
| 228 | + |
| 229 | + EXPECT_EQ(hipdnnDestroy(handle), HIPDNN_STATUS_SUCCESS); |
| 230 | + EXPECT_EQ(hipdnnSetHeuristicPluginPaths_ext(0, nullptr, HIPDNN_PLUGIN_LOADING_ABSOLUTE), |
| 231 | + HIPDNN_STATUS_SUCCESS); |
| 232 | + EXPECT_EQ(hipdnnSetHeuristicPluginPaths_ext(0, nullptr, HIPDNN_PLUGIN_LOADING_ADDITIVE), |
| 233 | + HIPDNN_STATUS_SUCCESS); |
| 234 | +} |
| 235 | + |
48 | 236 | TEST(IntegrationSetPluginPathsExt, NullStringInList) |
49 | 237 | { |
50 | 238 | std::array<const char*, 2> paths = {"./valid/path.so", nullptr}; |
@@ -76,6 +264,22 @@ TEST(IntegrationSetPluginPathsExt, IneligibleHandle) |
76 | 264 | EXPECT_EQ(status, HIPDNN_STATUS_BAD_PARAM_NULL_POINTER); |
77 | 265 | } |
78 | 266 |
|
| 267 | +TEST(IntegrationSetPluginPathsExt, HeuristicIneligibleHandle) |
| 268 | +{ |
| 269 | + hipdnnHandle_t handle = nullptr; |
| 270 | + hipdnnStatus_t status = hipdnnCreate(&handle); |
| 271 | + ASSERT_EQ(status, HIPDNN_STATUS_SUCCESS); |
| 272 | + ASSERT_NE(handle, nullptr); |
| 273 | + |
| 274 | + const std::array<const char*, 1> paths = {testGoodHeuristicPluginPath().c_str()}; |
| 275 | + status = hipdnnSetHeuristicPluginPaths_ext( |
| 276 | + paths.size(), paths.data(), HIPDNN_PLUGIN_LOADING_ABSOLUTE); |
| 277 | + |
| 278 | + EXPECT_EQ(status, HIPDNN_STATUS_NOT_SUPPORTED); |
| 279 | + |
| 280 | + EXPECT_EQ(hipdnnDestroy(handle), HIPDNN_STATUS_SUCCESS); |
| 281 | +} |
| 282 | + |
79 | 283 | TEST(IntegrationSetPluginPathsExt, GetLoadedPluginPathsLoadsDefault) |
80 | 284 | { |
81 | 285 | const hipdnn_test_sdk::utilities::ScopedEnvironmentVariableSetter envSetter( |
|
0 commit comments