1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
|
/*******************************************************************************
*
* MIT License
*
* Copyright (C) 2022-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*******************************************************************************/
#include <gtest/gtest.h>
#include <Tensile/AMDGPU.hpp>
#include <Tensile/ContractionLibrary.hpp>
#include <Tensile/ContractionProblemPredicates.hpp>
#include <Tensile/ContractionProblemProperties.hpp>
#include <Tensile/DecisionTree.hpp>
#include <Tensile/DecisionTreeLibrary.hpp>
#include <Tensile/ExactLogicLibrary.hpp>
using namespace Tensile;
using namespace DecisionTree;
using Key = std::array<float, 3>;
using DTree = Tree<Key, std::shared_ptr<ContractionLibrary>, std::shared_ptr<ContractionSolution>>;
/*
* Tests for invalid tree structures
* e.g. trees that try go out of bounds, trees with circular paths
*/
TEST(DecisionTree, ValidTree)
{
DTree test_tree{{
/* start */ {0, 700.f, 1, IDX_RETURN_FALSE},
/* - 1 - */ {1, 300.f, IDX_RETURN_FALSE, IDX_RETURN_TRUE},
}};
/*
Makes the following tree:
start:(Key[0] <= 700?)
|
|
|------------------------
| |
YES NO
| |
| |
1:(Key[1] <= 300?) 2:(RETURN false)
|
|
|------------------------
| |
YES NO
| |
| |
3:(RETURN false) 4:(RETURN true)
*/
EXPECT_TRUE(test_tree.valid());
}
TEST(DecisionTree, InvalidTreeEmpty)
{
DTree test_tree{{}};
EXPECT_FALSE(test_tree.valid());
}
TEST(DecisionTree, InvalidTreeIdxOOB)
{
DTree test_tree{{
/* start */ {0, 700.f, IDX_RETURN_TRUE, 7}, // right idx OOB
}};
EXPECT_FALSE(test_tree.valid());
}
TEST(DecisionTree, InvalidTreeIdxOOBNeg)
{
DTree test_tree{{
/* start */ {0, 700.f, -3, IDX_RETURN_TRUE}, // left idx OOB
}};
EXPECT_FALSE(test_tree.valid());
}
TEST(DecisionTree, InvalidTreeCircularShort)
{
DTree test_tree{{
/* start */ {0, 700.f, IDX_RETURN_TRUE, 0}, // right idx circular
}};
EXPECT_FALSE(test_tree.valid());
}
TEST(DecisionTree, InvalidTreeCircularLong)
{
DTree test_tree{{
/* start */ {0, 700.f, 1, IDX_RETURN_TRUE},
/* - 1 - */ {0, 700.f, 0, IDX_RETURN_TRUE}, // left idx circular
}};
EXPECT_FALSE(test_tree.valid());
}
TEST(DecisionTree, InvalidNoTrueNodes)
{
DTree test_tree{{
/* start */ {0, 700.f, 1, IDX_RETURN_FALSE},
/* - 1 - */ {0, 700.f, IDX_RETURN_FALSE, IDX_RETURN_FALSE},
}};
EXPECT_FALSE(test_tree.valid());
}
/*
* Tests for correct predictions.
*/
TEST(DecisionTree, SimplePrediction)
{
DTree test_tree{{
/* start */ {0, 700.f, IDX_RETURN_FALSE, IDX_RETURN_TRUE},
}};
Key test_input0{{800.f, 200.f, 200.f}};
Key test_input1{{600.f, 200.f, 200.f}};
EXPECT_EQ(test_tree.predict(test_input0), true); // Expected: start->true
EXPECT_EQ(test_tree.predict(test_input1), false); // Expected: start->false
}
TEST(DecisionTree, MultiStepPrediction)
{
DTree test_tree{{
/* start */ {0, 700.f, 1, IDX_RETURN_FALSE},
/* - 1 - */ {1, 300.f, IDX_RETURN_FALSE, IDX_RETURN_TRUE},
}};
Key test_input0{{800.f, 400.f, 200.f}};
Key test_input1{{600.f, 400.f, 200.f}};
Key test_input2{{600.f, 200.f, 200.f}};
EXPECT_EQ(test_tree.predict(test_input0), false); // Expected: start->false
EXPECT_EQ(test_tree.predict(test_input1), true); // Expected: start->1->true
EXPECT_EQ(test_tree.predict(test_input2), false); // Expected: start->1->false
}
/*
* Tests for libraries.
*/
TEST(DecisionTree, DecisionTreeLibrary)
{
// Solutions
auto Solution0 = std::make_shared<ContractionSolution>();
auto Solution1 = std::make_shared<ContractionSolution>();
auto Solution2 = std::make_shared<ContractionSolution>();
auto Solution3 = std::make_shared<ContractionSolution>();
Solution0->index = 0;
Solution1->index = 1;
Solution2->index = 2;
Solution3->index = 3;
auto Library0 = std::make_shared<SingleContractionLibrary>(Solution0);
auto Library1 = std::make_shared<SingleContractionLibrary>(Solution1);
auto Library2 = std::make_shared<SingleContractionLibrary>(Solution2);
auto LibraryFallback = std::make_shared<SingleContractionLibrary>(Solution3);
std::vector<std::shared_ptr<MLFeatures::MLFeature<ContractionProblem>>> features;
auto freeSizeA = std::make_shared<MLFeatures::FreeSizeA>();
freeSizeA->index = 0;
features.push_back(freeSizeA);
auto freeSizeB = std::make_shared<MLFeatures::FreeSizeB>();
freeSizeB->index = 0;
features.push_back(freeSizeB);
auto boundSize = std::make_shared<MLFeatures::BoundSize>();
boundSize->index = 0;
features.push_back(boundSize);
// Make trees library
std::vector<DTree> trees;
DTree tree0{{
{0, 700.f, IDX_RETURN_FALSE, IDX_RETURN_TRUE}, // YES for freeSizeA>7000
}};
tree0.value = Library0;
trees.push_back(tree0);
DTree tree1{{
{1, 700.f, IDX_RETURN_FALSE, IDX_RETURN_TRUE}, // YES for freeSizeB>700
}};
tree1.value = Library1;
trees.push_back(tree1);
DTree tree2{{
{0, 300.f, IDX_RETURN_TRUE, IDX_RETURN_FALSE}, // YES for freeSizeA<300
}};
tree2.value = Library2;
trees.push_back(tree2);
// Forest and full library - Note: Solution 3 as fallback
using BForest = BasicForest<Key,
ContractionProblem,
std::shared_ptr<ContractionLibrary>,
std::shared_ptr<ContractionSolution>>;
// this change in the constructor enables the template magic to
// handle null values as a default when serializing the fallback
// solutions if it is optionally not present.
auto forest = std::make_shared<BForest>(features);
forest->nullValue = LibraryFallback;
forest->trees = trees;
auto dtreelib = std::make_shared<DecisionTreeLibrary<ContractionProblem>>();
dtreelib->forest = forest;
// Problems
auto Problem0
= ContractionProblem::GEMM(false, false, 800, 800, 800, 800, 800, 800, 1.0, false, 1);
auto Problem1
= ContractionProblem::GEMM(false, false, 500, 800, 800, 500, 800, 500, 1.0, false, 1);
auto Problem2
= ContractionProblem::GEMM(false, false, 500, 500, 500, 500, 500, 500, 1.0, false, 1);
// Tests
AMDGPU gpu;
EXPECT_EQ(dtreelib->findBestSolution(Problem0, gpu), Solution0);
EXPECT_EQ(dtreelib->findBestSolution(Problem1, gpu), Solution1);
EXPECT_EQ(dtreelib->findBestSolution(Problem2, gpu), Solution3); // No match, goes to fallback
}
TEST(DecisionTree, DecisionTreeMultiLibrary)
{
using Predicate = Predicates::Predicate<ContractionProblem>;
using SizeInRange = Predicates::Contraction::SizeInRange;
using SizeEqual = Predicates::Contraction::SizeEqual;
using Range = Predicates::Contraction::Range;
using And = Predicates::And<ContractionProblem>;
using BForest = BasicForest<Key,
ContractionProblem,
std::shared_ptr<ContractionLibrary>,
std::shared_ptr<ContractionSolution>>;
// This will test the behavior of the dtree logic can handle multiple regions correctly.
// The two regions that are constructed have the opposite branching logic.
auto region1Solution0 = std::make_shared<ContractionSolution>();
auto region1Solution1 = std::make_shared<ContractionSolution>();
region1Solution0->index = 0;
region1Solution1->index = 1;
auto region1Library0 = std::make_shared<SingleContractionLibrary>(region1Solution0);
auto region1LibraryFallback = std::make_shared<SingleContractionLibrary>(region1Solution1);
auto region2Solution0 = std::make_shared<ContractionSolution>();
auto region2Solution1 = std::make_shared<ContractionSolution>();
region2Solution0->index = 0;
region2Solution1->index = 1;
auto region2Library0 = std::make_shared<SingleContractionLibrary>(region2Solution0);
auto region2LibraryFallback = std::make_shared<SingleContractionLibrary>(region2Solution1);
// Features (generic)
std::vector<std::shared_ptr<MLFeatures::MLFeature<ContractionProblem>>> features;
auto freeSizeA = std::make_shared<MLFeatures::FreeSizeA>();
freeSizeA->index = 0;
features.push_back(freeSizeA);
auto freeSizeB = std::make_shared<MLFeatures::FreeSizeB>();
freeSizeB->index = 0;
features.push_back(freeSizeB);
auto boundSize = std::make_shared<MLFeatures::BoundSize>();
boundSize->index = 0;
features.push_back(boundSize);
// Make trees library
std::vector<DTree> region1trees;
DTree region1tree0{{
{0, 5000.f, IDX_RETURN_FALSE, IDX_RETURN_TRUE}, // YES for freeSizeA > 5000
}};
region1tree0.value = region1Library0;
region1trees.push_back(region1tree0);
auto region1forest = std::make_shared<BForest>(features);
region1forest->nullValue = region1LibraryFallback;
region1forest->trees = region1trees;
auto region1dtreelib = std::make_shared<DecisionTreeLibrary<ContractionProblem>>();
region1dtreelib->forest = region1forest;
// Make trees library
std::vector<DTree> region2trees;
DTree region2tree0{{
{0, 5000.f, IDX_RETURN_TRUE, IDX_RETURN_FALSE}, // YES for freeSizeA <= 5000
}};
region2tree0.value = region2Library0;
region2trees.push_back(region2tree0);
auto region2forest = std::make_shared<BForest>(features);
region2forest->nullValue = region2LibraryFallback;
region2forest->trees = region2trees;
auto region2dtreelib = std::make_shared<DecisionTreeLibrary<ContractionProblem>>();
region2dtreelib->forest = region2forest;
/// region library
size_t max_size = std::numeric_limits<size_t>::max();
std::shared_ptr<Predicate> regionM = std::make_shared<SizeInRange>(0, Range{0, 40000});
std::shared_ptr<Predicate> regionB = std::make_shared<SizeEqual>(2, 1);
std::shared_ptr<Predicate> regionN1 = std::make_shared<SizeInRange>(1, Range{0, 8000});
std::shared_ptr<Predicate> regionN2 = std::make_shared<SizeInRange>(1, Range{8000, max_size});
// Create region predicate for (0 <= M < 40000), (0 <= N < 8000)
auto preds1 = {regionM, regionB, regionN1};
auto isRegion1 = std::make_shared<And>(preds1);
ContractionProblemSelectionLibrary::Row Region1Row_dtreelib(isRegion1, region1dtreelib);
// Create region predicate for (0 <= M < 40000), (8000 <= N < max)
auto preds2 = {regionM, regionB, regionN2};
auto isRegion2 = std::make_shared<And>(preds2);
ContractionProblemSelectionLibrary::Row Region2Row_dtreelib(isRegion2, region2dtreelib);
ContractionProblemSelectionLibrary lib({Region1Row_dtreelib, Region2Row_dtreelib});
// Problems
auto Region1Problem1
= ContractionProblem::GEMM(false, false, 7000, 6500, 1000, 7000, 1000, 7000, 1.0, false, 1);
auto Region1Problem2
= ContractionProblem::GEMM(false, false, 4000, 6500, 1000, 4000, 1000, 4000, 1.0, false, 1);
auto Region2Problem1 = ContractionProblem::GEMM(
false, false, 7000, 16500, 1000, 7000, 1000, 7000, 1.0, false, 1);
auto Region2Problem2 = ContractionProblem::GEMM(
false, false, 4000, 16500, 1000, 4000, 1000, 4000, 1.0, false, 1);
AMDGPU gpu;
EXPECT_EQ(lib.findBestSolution(Region1Problem1, gpu), region1Solution0);
EXPECT_EQ(lib.findBestSolution(Region1Problem2, gpu), region1Solution1);
EXPECT_EQ(lib.findBestSolution(Region2Problem1, gpu), region2Solution1);
EXPECT_EQ(lib.findBestSolution(Region2Problem2, gpu), region2Solution0);
}
TEST(DecisionTree, DecisionTreeBatch)
{
using Predicate = Predicates::Predicate<ContractionProblem>;
using SizeInRange = Predicates::Contraction::SizeInRange;
using Range = Predicates::Contraction::Range;
using And = Predicates::And<ContractionProblem>;
// Create a Key4 aliase specific to this test because this test uses four features which
// requires a size four Key type; whereas previous tests only use three features.
using Key4 = std::array<float, 4>;
using DTree4
= Tree<Key4, std::shared_ptr<ContractionLibrary>, std::shared_ptr<ContractionSolution>>;
using BForest = BasicForest<Key4,
ContractionProblem,
std::shared_ptr<ContractionLibrary>,
std::shared_ptr<ContractionSolution>>;
// This will test the behavior of the dtree logic can handle multiple regions correctly.
// The two regions that are constructed have the opposite branching logic.
auto region1Solution0 = std::make_shared<ContractionSolution>();
auto region1Solution1 = std::make_shared<ContractionSolution>();
region1Solution0->index = 0;
region1Solution1->index = 1;
auto region1Library0 = std::make_shared<SingleContractionLibrary>(region1Solution0);
auto region1LibraryFallback = std::make_shared<SingleContractionLibrary>(region1Solution1);
auto region2Solution0 = std::make_shared<ContractionSolution>();
auto region2Solution1 = std::make_shared<ContractionSolution>();
region2Solution0->index = 0;
region2Solution1->index = 1;
auto region2Library0 = std::make_shared<SingleContractionLibrary>(region2Solution0);
auto region2LibraryFallback = std::make_shared<SingleContractionLibrary>(region2Solution1);
// Features (generic)
std::vector<std::shared_ptr<MLFeatures::MLFeature<ContractionProblem>>> features;
auto freeSizeA = std::make_shared<MLFeatures::FreeSizeA>();
freeSizeA->index = 0;
features.push_back(freeSizeA);
auto freeSizeB = std::make_shared<MLFeatures::FreeSizeB>();
freeSizeB->index = 0;
features.push_back(freeSizeB);
auto batchSize = std::make_shared<MLFeatures::BatchSize>();
batchSize->index = 0;
features.push_back(batchSize);
auto boundSize = std::make_shared<MLFeatures::BoundSize>();
boundSize->index = 0;
features.push_back(boundSize);
// Make trees library
std::vector<DTree4> region1trees;
DTree4 region1tree0{{
{0, 5000.f, IDX_RETURN_FALSE, IDX_RETURN_TRUE}, // YES for freeSizeA > 5000
}};
region1tree0.value = region1Library0;
region1trees.push_back(region1tree0);
auto region1forest = std::make_shared<BForest>(features);
region1forest->nullValue = region1LibraryFallback;
region1forest->trees = region1trees;
auto region1dtreelib = std::make_shared<DecisionTreeLibrary<ContractionProblem>>();
region1dtreelib->forest = region1forest;
// Make trees library
std::vector<DTree4> region2trees;
DTree4 region2tree0{{
{0, 5000.f, IDX_RETURN_TRUE, IDX_RETURN_FALSE}, // YES for freeSizeA <= 5000
}};
region2tree0.value = region2Library0;
region2trees.push_back(region2tree0);
auto region2forest = std::make_shared<BForest>(features);
region2forest->nullValue = region2LibraryFallback;
region2forest->trees = region2trees;
auto region2dtreelib = std::make_shared<DecisionTreeLibrary<ContractionProblem>>();
region2dtreelib->forest = region2forest;
/// region library
size_t max_size = std::numeric_limits<size_t>::max();
std::shared_ptr<Predicate> regionM = std::make_shared<SizeInRange>(0, Range{0, max_size});
std::shared_ptr<Predicate> regionN = std::make_shared<SizeInRange>(1, Range{0, max_size});
std::shared_ptr<Predicate> regionK = std::make_shared<SizeInRange>(3, Range{0, max_size});
std::shared_ptr<Predicate> regionBa = std::make_shared<SizeInRange>(2, Range{0, 64});
std::shared_ptr<Predicate> regionBb = std::make_shared<SizeInRange>(2, Range{64, max_size});
// Create region predicate for (0 <= M,N,K < in_max), (0 <= B < 64)
auto preds1 = {regionM, regionN, regionBa, regionK};
auto isRegion1 = std::make_shared<And>(preds1);
ContractionProblemSelectionLibrary::Row Region1Row_dtreelib(isRegion1, region1dtreelib);
// Create region predicate for (0 <= M,N,K < intmax), (64 <= B < intmax)
auto preds2 = {regionM, regionN, regionBb, regionK};
auto isRegion2 = std::make_shared<And>(preds2);
ContractionProblemSelectionLibrary::Row Region2Row_dtreelib(isRegion2, region2dtreelib);
ContractionProblemSelectionLibrary lib({Region1Row_dtreelib, Region2Row_dtreelib});
// Problems
auto Region1Problem1 = ContractionProblem::GEMM(
false, false, 7000, 6500, 1000, 7000, 1000, 7000, 1.0, false, 10);
auto Region1Problem2 = ContractionProblem::GEMM(
false, false, 4000, 6500, 1000, 4000, 1000, 4000, 1.0, false, 10);
auto Region2Problem1 = ContractionProblem::GEMM(
false, false, 7000, 16500, 1000, 7000, 1000, 7000, 1.0, false, 100);
auto Region2Problem2 = ContractionProblem::GEMM(
false, false, 4000, 16500, 1000, 4000, 1000, 4000, 1.0, false, 100);
AMDGPU gpu;
EXPECT_EQ(lib.findBestSolution(Region1Problem1, gpu), region1Solution0);
EXPECT_EQ(lib.findBestSolution(Region1Problem2, gpu), region1Solution1);
EXPECT_EQ(lib.findBestSolution(Region2Problem1, gpu), region2Solution1);
EXPECT_EQ(lib.findBestSolution(Region2Problem2, gpu), region2Solution0);
}
|