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 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
|
#include <algorithm>
#include <random>
#include <stdexcept>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/jit_opt_limit.h>
#include <torch/csrc/jit/tensorexpr/ir_simplifier.h>
#include <torch/csrc/jit/tensorexpr/loopnest.h>
namespace torch {
namespace jit {
namespace tensorexpr {
namespace randomization_helper {
int64_t max_transformations(int n_max_transforms) {
// Reuse the env variable PYTORCH_JIT_OPT_LIMIT to control the max number of
// transformations. Example - set the env variable
// PYTORCH_JIT_OPT_LIMIT="loopnest_randomization=10" to set max
// transformations to 10. This can be helpful in gradually reducing the
// number of transformations when we see an error.
if (!JIT_OPT_ALLOWED) {
return n_max_transforms;
}
int max_transforms = 1;
while (JIT_OPT_ALLOWED && max_transforms < n_max_transforms) {
max_transforms++;
}
return max_transforms;
}
std::vector<std::vector<ForPtr>> GetAllPerfectlyNestedLoopNests(
std::vector<ForPtr> loops) {
// Find the first set of loops that can be reordered
std::vector<std::vector<ForPtr>> all_nested_loops;
std::vector<ForPtr> nested_loops;
if (loops.size() == 0) {
return all_nested_loops;
}
nested_loops.push_back(loops[0]);
for (size_t i = 1; i < loops.size(); i++) {
auto last_loop = nested_loops.back();
auto next_loop = loops[i];
if (last_loop->body()->nstmts() == 1 &&
last_loop->body()->front() == next_loop) {
nested_loops.push_back(next_loop);
} else {
if (nested_loops.size() > 1) {
all_nested_loops.push_back(nested_loops);
}
nested_loops.clear();
nested_loops.push_back(next_loop);
}
}
return all_nested_loops;
}
template <typename T>
std::tuple<std::vector<T>, std::vector<int>> select_n_randomly(
std::vector<T>& objects,
int n,
std::default_random_engine& random_engine) {
std::vector<int> indices(objects.size());
std::iota(indices.begin(), indices.end(), 0);
std::shuffle(indices.begin(), indices.end(), random_engine);
std::vector<T> selected_objects;
std::vector<int> selected_indices;
if (indices.size() < n) {
return std::make_tuple(selected_objects, selected_indices);
}
for (int i = 0; i < n; i++) {
int index = indices[i];
selected_indices.push_back(index);
selected_objects.push_back(objects[index]);
}
return std::make_tuple(selected_objects, selected_indices);
}
int find_factor(ForPtr loop) {
// Find valid factors
ExprPtr loop_stop = loop->stop();
auto loop_imm = intValue(loop_stop);
if (loop_imm) {
int loop_bound = *loop_imm;
int factor = rand() % (loop_bound - 1) + 1;
return factor;
}
return -1;
}
void printHistory(int index, std::string message) {
message = "Random Transform Sequence - Transformations[" +
std::to_string(index) + "] = " + message;
GRAPH_DEBUG(message);
}
template <typename T>
std::string join(std::vector<T> indices, char sep = ',') {
std::string s = "";
for (const auto& index : indices) {
s += std::to_string(index) + sep;
}
return s;
}
std::string join(std::vector<std::string> indices, char sep = ',') {
std::string s = "";
for (const auto& index : indices) {
s += index + sep;
}
return s;
}
template <typename T>
std::string indexOf(const std::vector<T>& objects, const T& object) {
return std::to_string(std::distance(
objects.begin(), std::find(objects.begin(), objects.end(), object)));
}
} // namespace randomization_helper
void loopnestRandomization(int64_t seed, LoopNest& l) {
// This is to help with determinstic testing of randomized infrastructure.
// When seed value is 1, we perform preset loop transformations. This allows
// testing of interface.
if (seed == 1) {
l.simplify();
return;
}
std::default_random_engine random_engine(seed);
std::srand(seed);
// Set the maximum allowed number of transformations beyong which it is hard
// to track and debug. Arbitratily choosing 20 as maximum number.
int max_allowed_transformations = 20;
int n_transforms = randomization_helper::max_transformations(
std::rand() % max_allowed_transformations);
std::string message = "";
// clang-format off
// Transformations list:
//
// StmtPtr simplify();
// bool computeInline(BufPtr b);
// void inlineIntermediateBufs(bool allow_duplicated_work);
// bool optimizeConditionals();
// static void splitWithTail(ForPtr f, int factor);
// static void splitWithMask(ForPtr f, int factor);
// static std::vector<ForPtr> distributeLoop(ForPtr loop, const std::unordered_set<StmtPtr>& pivots);
// static std::vector<ForPtr> distributeLoop(ForPtr loop);
// static std::vector<ForPtr> distributeLoopAndParents(ForPtr loop);
// static std::vector<ForPtr> distributeLoopOverInnerLoops(ForPtr loop);
// static std::vector<ForPtr> distributeLoopAndParentsOverInnerLoops(ForPtr loop);
// static bool fuseLoops(const std::vector<ForPtr>& loops, ForPtr* fused);
// static void reorderAxis(ForPtr a, ForPtr b);
// static std::vector<ForPtr> reorder(const std::vector<ForPtr>& loops, const std::vector<size_t>& permutation);
// ForPtr tile(ForPtr x, ForPtr y, int x_factor, int y_factor);
// static void fullUnroll(ForPtr f);
// static bool normalize(ForPtr f);
// static bool flatten(const std::vector<ForPtr>& f, ForPtr* flattened);
// static void compressBuffer(BufPtr buf, StmtPtr stmt);
// static void compressAllBuffers(StmtPtr stmt);
// static void sliceHead(ForPtr f, int factor, ForPtr* head, ForPtr* tail);
// static void sliceHead(ForPtr f, int factor);
// static void sliceTail(ForPtr f, int factor, ForPtr* head, ForPtr* tail);
// static void sliceTail(ForPtr f, int factor);
// static AccessResult cacheAccesses(BufPtr producer, const std::string& name, StmtPtr consumer);
// static void computeAt(StmtPtr s, ForPtr at);
// static bool rfactor(StmtPtr s, ForPtr outer_reduction_for);
// static bool vectorize(ForPtr);
// void vectorizeInnerLoops();
// void eliminateDeadStores();
// void prepareForCodegen();
// clang-format on
enum TransformKind {
SIMPLIFY = 0,
COMPUTE_INLINE,
INLINE_ALL,
OPT_COND,
SPLIT_TAIL,
SPLIT_MASK,
DIST1,
DIST2,
DIST3,
DIST4,
DIST5,
FUSE_LOOPS,
REORDER_AXIS,
REORDER,
TILE,
FULL_UNROLL,
NORMALIZE,
FLATTEN,
COMPRESS_BUFFER,
COMPRESS_ALL_BUFFERS,
SLICE_HEAD,
SLICE_TAIL,
CACHE_ACCESSES,
COMPUTE_AT,
RFACTOR,
VECTORIZE,
VECTORIZE_INNER_LOOPS,
ELIMINATE_DEAD_STORES,
MAX_TRANSFORM,
};
bool can_inline = true;
try {
for (int n_transform = 0; n_transform < n_transforms; n_transform++) {
int transform = std::rand() % MAX_TRANSFORM;
switch (transform) {
case SIMPLIFY: {
message = "simplify();\n";
randomization_helper::printHistory(n_transform, message);
l.simplify();
break;
}
case COMPUTE_INLINE: {
if (can_inline) {
auto bufs = NodeFinder<Buf>::find(l.root_stmt());
if (bufs.size() > 0) {
int buf_number = std::rand() % (int)bufs.size();
message =
"computeInline(" + bufs[buf_number]->name_hint() + ");\n";
randomization_helper::printHistory(n_transform, message);
l.computeInline(bufs[buf_number]);
}
}
break;
}
case INLINE_ALL: {
if (can_inline) {
int allow_dup = std::rand() % 2;
message =
"inlineIntermediateBufs(" + std::to_string(allow_dup) + ");\n";
randomization_helper::printHistory(n_transform, message);
l.inlineIntermediateBufs(allow_dup);
can_inline = false;
}
break;
}
case OPT_COND: {
message = "optimizeConditionals();\n";
randomization_helper::printHistory(n_transform, message);
l.optimizeConditionals();
break;
}
case SPLIT_TAIL: {
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() == 0) {
break;
}
int loop_n = std::rand() % (int)loops.size();
auto loop = loops[loop_n];
int factor = (std::rand() % 20) + 1;
message = "splitWithTail(loops[" + std::to_string(loop_n) + "], " +
std::to_string(factor) + ");\n";
randomization_helper::printHistory(n_transform, message);
l.splitWithTail(loop, factor);
break;
}
case SPLIT_MASK: {
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() == 0) {
break;
}
int loop_n = std::rand() % (int)loops.size();
auto loop = loops[loop_n];
int factor = (std::rand() % 20) + 1;
message = "splitWithMask(loops[" + std::to_string(loop_n) + "], " +
std::to_string(factor) + ")\n";
randomization_helper::printHistory(n_transform, message);
l.splitWithMask(loop, factor);
break;
}
case DIST1: {
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() == 0) {
break;
}
int loop_n = std::rand() % (int)loops.size();
auto loop = loops[loop_n];
std::vector<StmtPtr> stmts(
loop->body()->begin(), loop->body()->end());
if (stmts.size() == 0) {
break;
}
int n_pivots = (std::rand() % (int)stmts.size()) + 1;
std::vector<StmtPtr> pivots;
std::vector<int> chosen_indices;
std::tie(pivots, chosen_indices) =
randomization_helper::select_n_randomly<StmtPtr>(
stmts, n_pivots, random_engine);
std::unordered_set<StmtPtr> pivots_set(pivots.begin(), pivots.end());
message = "distributeLoop(loops[" + std::to_string(loop_n) +
"], pivots=stmts(" + randomization_helper::join(chosen_indices) +
"))\n";
randomization_helper::printHistory(n_transform, message);
l.distributeLoop(loop, pivots_set);
break;
}
case DIST2: {
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() == 0) {
break;
}
int loop_n = std::rand() % (int)loops.size();
auto loop = loops[loop_n];
message = "distributeLoop(loops[" + std::to_string(loop_n) + "])\n";
randomization_helper::printHistory(n_transform, message);
l.distributeLoop(loop);
break;
}
case DIST3: {
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() == 0) {
break;
}
int loop_n = std::rand() % (int)loops.size();
auto loop = loops[loop_n];
message = "distributeLoopAndParents(loops[" + std::to_string(loop_n) +
"])\n";
randomization_helper::printHistory(n_transform, message);
l.distributeLoopAndParents(loop);
break;
}
case DIST4: {
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() == 0) {
break;
}
int loop_n = std::rand() % (int)loops.size();
auto loop = loops[loop_n];
message = "distributeLoopOverInnerLoops(loops[" +
std::to_string(loop_n) + "])\n";
randomization_helper::printHistory(n_transform, message);
l.distributeLoopOverInnerLoops(loop);
break;
}
case DIST5: {
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() == 0) {
break;
}
int loop_n = std::rand() % (int)loops.size();
auto loop = loops[loop_n];
message = "distributeLoopAndParentsOverInnerLoops(loops[" +
std::to_string(loop_n) + "])\n";
randomization_helper::printHistory(n_transform, message);
l.distributeLoopAndParentsOverInnerLoops(loop);
break;
}
case FUSE_LOOPS: {
// Get all the loops
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() <= 1) {
break;
}
// Find a random number of loops to fuse
int num_loops_to_fuse =
std::max(2, (int)(std::rand() % (int)loops.size()));
std::vector<ForPtr> loops_to_fuse;
std::vector<int> chosen_indices;
std::tie(loops_to_fuse, chosen_indices) =
randomization_helper::select_n_randomly<ForPtr>(
loops, num_loops_to_fuse, random_engine);
message = "fuseLoops(loops[" +
randomization_helper::join(chosen_indices) + "], &fused_loop);\n";
randomization_helper::printHistory(n_transform, message);
// Fuse the loops
ForPtr fused_loop;
l.fuseLoops(loops_to_fuse, &fused_loop);
break;
}
case REORDER_AXIS: {
// Get all the loops
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() <= 1) {
break;
}
// Find pairs of axes that can be reordered
std::vector<std::pair<ForPtr, ForPtr>> valid_pairs;
for (int i = 0; i < loops.size(); i++) {
for (int j = i + 1; j < loops.size(); j++) {
if (LoopNest::findOuterFor(loops[i], loops[j])) {
valid_pairs.emplace_back(loops[i], loops[j]);
}
}
}
// Choose a pair randomly
if (valid_pairs.size() == 0) {
break;
}
int valid_pair_n = std::rand() % (int)valid_pairs.size();
auto loop_pair = valid_pairs.at(valid_pair_n);
auto first_loop = std::get<0>(loop_pair);
auto second_loop = std::get<1>(loop_pair);
std::string first_index =
randomization_helper::indexOf(loops, first_loop);
std::string second_index =
randomization_helper::indexOf(loops, second_loop);
message = "reorderAxis(loops[";
message += first_index;
message += "], loops[";
message += second_index + "]);\n";
randomization_helper::printHistory(n_transform, message);
// reorder the axis
l.reorderAxis(first_loop, second_loop);
break;
}
case REORDER: {
// Get all the loops
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() <= 1) {
break;
}
// Find all perfectly nested loop nests
auto all_nested_loops =
randomization_helper::GetAllPerfectlyNestedLoopNests(loops);
if (all_nested_loops.size() == 0) {
break;
}
// Randomly pick a set of consecutive loops to reorder
int index = rand() % (int)all_nested_loops.size();
auto nested_loops = all_nested_loops.at(index);
// Create a random permutation for reordering
std::vector<size_t> permutation(nested_loops.size());
std::iota(permutation.begin(), permutation.end(), 0);
std::shuffle(permutation.begin(), permutation.end(), random_engine);
// Generate a good history message
std::vector<std::string> indices;
indices.reserve(nested_loops.size());
for (const auto& l : nested_loops) {
indices.push_back(randomization_helper::indexOf(loops, l));
}
message = "reorder(loops[" + randomization_helper::join(indices) +
"], permutation=[" + randomization_helper::join(permutation) +
"]);\n";
randomization_helper::printHistory(n_transform, message);
// reorder
l.reorder(nested_loops, permutation);
break;
}
case TILE: {
// Get all the loops
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() <= 1) {
break;
}
// Tile needs two perfectly nested loops. To find such loops, we find
// all perfectly nested loop nests, randomly pick one of them, and
// randomly pick 2 consecutive loops in that loop nest.
// Find all perfectly nested loop nests
auto all_nested_loops =
randomization_helper::GetAllPerfectlyNestedLoopNests(loops);
if (all_nested_loops.size() == 0) {
break;
}
int index = rand() % (int)all_nested_loops.size();
auto nested_loops = all_nested_loops.at(index);
if (nested_loops.size() < 2) {
break;
}
int loop_number = rand() % ((int)nested_loops.size() - 1);
auto x_loop = nested_loops.at(loop_number);
auto y_loop = nested_loops.at(loop_number + 1);
int x_factor = randomization_helper::find_factor(x_loop);
int y_factor = randomization_helper::find_factor(y_loop);
if (x_factor == -1 || y_factor == -1) {
break;
}
std::string x_loop_index =
randomization_helper::indexOf(loops, x_loop);
std::string y_loop_index =
randomization_helper::indexOf(loops, y_loop);
message = "tile(loops[";
message += x_loop_index;
message += "], loops[";
message += y_loop_index + "], ";
message += std::to_string(x_factor);
message += ", " + std::to_string(y_factor) + ");\n";
randomization_helper::printHistory(n_transform, message);
// tile
l.tile(x_loop, y_loop, x_factor, y_factor);
break;
}
case FULL_UNROLL: {
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() == 0) {
break;
}
int loop_n = std::rand() % (int)loops.size();
auto loop = loops[loop_n];
message = "fullUnroll(loops[" + std::to_string(loop_n) + "]);\n";
randomization_helper::printHistory(n_transform, message);
LoopNest::fullUnroll(loop);
break;
}
case NORMALIZE: {
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() == 0) {
break;
}
int loop_n = std::rand() % (int)loops.size();
auto loop = loops[loop_n];
message = "normalize(loops[" + std::to_string(loop_n) + "]);\n";
randomization_helper::printHistory(n_transform, message);
l.normalize(loop);
break;
}
case FLATTEN: {
// Get all the loops
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() <= 1) {
break;
}
// Find all perfectly nested loop nests
auto all_nested_loops =
randomization_helper::GetAllPerfectlyNestedLoopNests(loops);
if (all_nested_loops.size() == 0) {
break;
}
// Randomly pick a set of consecutive loops to flatten
int index = rand() % (int)all_nested_loops.size();
auto nested_loops = all_nested_loops.at(index);
// Generate a good history message
std::vector<std::string> indices;
indices.reserve(nested_loops.size());
for (const auto& l : nested_loops) {
indices.push_back(randomization_helper::indexOf(loops, l));
}
message =
"flatten(loops[" + randomization_helper::join(indices) + "]);\n";
randomization_helper::printHistory(n_transform, message);
// flatten
l.flatten(nested_loops);
break;
}
case COMPRESS_BUFFER: {
auto buffers = NodeFinder<Buf>::find(l.root_stmt());
int buffer_n = std::rand() % (int)buffers.size();
auto buffer = buffers[buffer_n];
message = "compressBuffer(buffers[" + std::to_string(buffer_n) +
"], l.root_stmt());\n";
randomization_helper::printHistory(n_transform, message);
l.compressBuffer(buffer, l.root_stmt());
break;
}
case COMPRESS_ALL_BUFFERS: {
auto buffers = BufFinder::find(l.root_stmt());
message = "compressAllBuffers(l.root_stmt());\n";
randomization_helper::printHistory(n_transform, message);
l.compressAllBuffers(l.root_stmt());
break;
}
case SLICE_HEAD: {
// Get all the loops
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() == 0) {
break;
}
int loop_n = std::rand() % (int)loops.size();
auto loop = loops[loop_n];
int factor = randomization_helper::find_factor(loop);
if (factor == -1) {
break;
}
message = "sliceHead(loops[" + std::to_string(loop_n) + "]);\n";
randomization_helper::printHistory(n_transform, message);
l.sliceHead(loop, factor);
break;
}
case SLICE_TAIL: {
// Get all the loops
auto loops = NodeFinder<For>::find(l.root_stmt());
if (loops.size() == 0) {
break;
}
int loop_n = std::rand() % (int)loops.size();
auto loop = loops[loop_n];
int factor = randomization_helper::find_factor(loop);
if (factor == -1) {
break;
}
message = "sliceTail(loops[" + std::to_string(loop_n) + "]);\n";
randomization_helper::printHistory(n_transform, message);
l.sliceTail(loop, factor);
break;
}
case CACHE_ACCESSES: {
// TODO - Implement cache_access
break;
}
case COMPUTE_AT: {
// To find valid compute at pairs, we need to collect the producer
// consumer pairs. For now, we do not collect all such pairs for
// simplicity. For now, we collect producer and the immediate parent
// loop of the consumer. We could collect all the consumer enclosing
// loops, but then we will have to clean up the ones that are shared
// with the producer encloser loop. Currently, we only test on the
// immediate parent loop.
auto buffers = BufFinder::find(l.root_stmt());
std::vector<std::pair<StmtPtr, ForPtr>> producer_consumer_pairs;
for (const auto& buffer : buffers) {
auto producers = l.getAllWritesToBuf(buffer);
auto consumers = StmtsReadingBuf::find(l.root_stmt(), buffer);
if (producers.size() != 1 || consumers.empty()) {
continue;
}
for (const auto& producer : producers) {
for (const auto& consumer : consumers) {
auto parent_loop = LoopNest::getParentLoop(consumer);
auto pc_pair = std::make_pair(producer, parent_loop);
producer_consumer_pairs.push_back(pc_pair);
}
}
}
if (producer_consumer_pairs.size() == 0) {
break;
}
// Choose a random pair
int pair_n = std::rand() % (int)producer_consumer_pairs.size();
auto pc_pair = producer_consumer_pairs.at(pair_n);
auto store = std::get<0>(pc_pair);
auto for_ptr = std::get<1>(pc_pair);
// TODO - come up with better message
message = "computeAt(....);\n";
randomization_helper::printHistory(n_transform, message);
l.computeAt(store, for_ptr);
break;
}
case RFACTOR: {
// TODO - Implement rfactor
break;
}
case VECTORIZE: {
auto loops = NodeFinder<For>::find(l.root_stmt());
std::vector<ForPtr> innermost_loops;
for (const auto& loop : loops) {
bool containsSubLoops = false;
if (BlockPtr body = to<Block>(loop->body())) {
for (const StmtPtr& stmt : *body) {
if (ForPtr f2 = to<For>(stmt)) {
containsSubLoops = true;
}
}
}
if (!containsSubLoops) {
innermost_loops.push_back(loop);
}
}
if (innermost_loops.size() == 0) {
break;
}
int loop_n = std::rand() % (int)innermost_loops.size();
auto loop = innermost_loops[loop_n];
message = "vectorize(loops[" + std::to_string(loop_n) + "]);\n";
randomization_helper::printHistory(n_transform, message);
l.vectorize(loop);
break;
}
case VECTORIZE_INNER_LOOPS: {
message = "vectorizeInnerLoops();\n";
randomization_helper::printHistory(n_transform, message);
l.vectorizeInnerLoops();
break;
}
case ELIMINATE_DEAD_STORES: {
message = "eliminateDeadStores();\n";
randomization_helper::printHistory(n_transform, message);
l.eliminateDeadStores();
break;
}
// TODO: Add remaining transforms
default:
break;
}
}
} catch (...) {
std::cout << "EXCEPTION THROWN!\n";
std::cout << "SEED: " << seed << "\n";
throw std::runtime_error("Random test failed");
}
message = "End of transformations;\n";
randomization_helper::printHistory(n_transforms, message);
return;
}
} // namespace tensorexpr
} // namespace jit
} // namespace torch
|