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
|
#include <torch/csrc/jit/codegen/cuda/scheduler.h>
#include <torch/csrc/jit/codegen/cuda/arith.h>
#include <torch/csrc/jit/codegen/cuda/executor_utils.h>
#include <torch/csrc/jit/codegen/cuda/expr_evaluator.h>
#include <torch/csrc/jit/codegen/cuda/instrumentation.h>
#include <torch/csrc/jit/codegen/cuda/ir_all_nodes.h>
#include <torch/csrc/jit/codegen/cuda/ir_iostream.h>
#include <torch/csrc/jit/codegen/cuda/ir_utils.h>
#include <torch/csrc/jit/codegen/cuda/parser.h>
#include <ATen/cuda/CUDAContext.h>
namespace torch {
namespace jit {
namespace fuser {
namespace cuda {
constexpr int kUnrollFactor = 1;
namespace {
std::vector<int> reductionAxes(TensorView* tv) {
size_t n_dims = tv->nDims();
std::vector<int> reduction_axes;
for (size_t i = 0; i < n_dims; i++) {
if (tv->axis(i)->isReduction()) {
reduction_axes.emplace_back(i);
}
}
return reduction_axes;
}
// Merge all reduction to the right side and returns total number of
// reduction axes
size_t mergeReduction(TensorView* tv) {
int prev_i = -1;
size_t num_merged = 0;
for (int i = static_cast<int>(tv->nDims()) - 1; i >= 0; i--) {
if (!tv->axis(i)->isReduction()) {
continue;
}
if (prev_i == -1) {
prev_i = i;
} else {
tv->merge(i, prev_i);
prev_i = i;
num_merged++;
}
}
if (prev_i == 0) {
tv->reorder({{prev_i, -1}});
}
return prev_i == -1 ? 0 : num_merged + 1;
}
// merge all non-reduction axes to the left side and returns total number of
// iteration axes
size_t mergeNonReduction(TensorView* tv) {
int prev_i = -1;
size_t num_merged = 0;
for (int i = static_cast<int>(tv->nDims()) - 1; i >= 0; i--) {
if (tv->axis(i)->isReduction()) {
continue;
}
if (prev_i == -1) {
prev_i = i;
} else {
tv->merge(i, prev_i);
prev_i = i;
num_merged++;
}
}
if (prev_i != 0) {
tv->reorder({{prev_i, 0}});
}
return prev_i == -1 ? 0 : num_merged + 1;
}
} // namespace
// This one is a total mess and it should go.
bool scheduleFusion(Fusion* fusion, const at::ArrayRef<c10::IValue> inputs) {
FUSER_PERF_SCOPE("scheduleFusion");
FusionGuard fg(fusion);
// maybe has_reduction for scheudling should be done on a per output tensor
// basis.
TORCH_INTERNAL_ASSERT(
!fusion->hasReduction(), "This scheduler only handles pointwise ops.");
const bool disable_unroll = fusion->isStochastic();
for (auto out_val : fusion->outputs()) {
auto out = out_val->as<TensorView>();
// Merge all dimensions because we're only supporting pointwise
while (out->nDims() > 1) {
out->merge(-2, -1);
}
}
// Run through outputs, grab all inputs of outputs
// squeeze with computeAt to set overall structure.
for (auto output : fusion->outputs()) {
if (output->getValType() != ValType::TensorView)
continue;
TensorView* out_tv = output->as<TensorView>();
// Split into 128 which will be bockDim.x
out_tv->split(0, kPwThreadX);
// Split by another 4 which will be our unroll factor
auto ur_factor = disable_unroll ? 1 : kUnrollFactor;
if (!disable_unroll) {
out_tv->split(0, ur_factor);
}
}
for (auto output : fusion->outputs()) {
if (output->getValType() != ValType::TensorView)
continue;
TensorView* out_tv = output->as<TensorView>();
for (Val* inp : fusion->inputsOf(output)) {
if (inp->getValType().value() == ValType::TensorView)
inp->as<TensorView>()->computeAt(out_tv, -1);
}
out_tv->axis(0)->parallelize(ParallelType::BIDx);
out_tv->axis(1)->parallelize(ParallelType::Unroll);
out_tv->axis(2)->parallelize(ParallelType::TIDx);
}
return true;
}
namespace {
// Largest Power of 2 less-than n
constexpr int lastPow2(int n) {
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
n |= (n >> 16); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
return std::max(1, n - (n >> 1));
}
ReductionParams reductionHeuristic(
int red_elems,
int red_outputs,
bool red_on_fastest_dim) {
ReductionParams rparams;
rparams.fastest_dim = red_on_fastest_dim;
int gdimx = LaunchParams::UNINITIALIZED_VAL;
int gdimy = LaunchParams::UNINITIALIZED_VAL;
int bdimx = LaunchParams::UNINITIALIZED_VAL;
int bdimy = LaunchParams::UNINITIALIZED_VAL;
// 1. Initial Assumptions
// Evaluate Dimensions of Reduction TensorView
TORCH_INTERNAL_ASSERT(red_elems > 0 && red_outputs > 0);
// 2. Initial Definition of Block Dimensions
// Is fastest dimension a reduction dimension?
if (rparams.fastest_dim) {
if (red_elems < rparams.loop_unroll) {
rparams.loop_unroll = 1;
}
bdimx = ceilDiv(red_elems, rparams.loop_unroll);
bdimy = red_outputs;
} else {
bdimx = red_outputs;
bdimy = red_elems;
}
// 3. Applying Power of 2 Blocking based on the Maximum Number of threads
constexpr int kMaxNumThreads = 512;
int num_threads = kMaxNumThreads;
int device_warp_size = at::cuda::warp_size();
if (bdimx < num_threads) {
bdimx = lastPow2(bdimx);
} else {
bdimx = num_threads;
}
if (bdimy < num_threads) {
bdimy = lastPow2(bdimy);
} else {
bdimy = num_threads;
}
int bdimx_prev = bdimx;
bdimx = std::min(bdimx, device_warp_size);
bdimy = std::min(bdimy, num_threads / bdimx);
bdimx = std::min(bdimx_prev, num_threads / bdimy);
// 4. Distributing work across a block
// Magic numbers of calculations allowed per thread.
constexpr int kMinValuesPerThread = 16;
constexpr int kMaxValuesPerThread = 256;
int inputs_consumed_per_block_iter = 1;
int red_elems_per_thread = red_elems;
int outputs_produced_per_block_iter = 1;
// Reduction is performed across warp threads (cross-thread reduction)
if (rparams.fastest_dim) {
inputs_consumed_per_block_iter *= bdimx;
red_elems_per_thread =
ceilDiv(red_elems_per_thread, inputs_consumed_per_block_iter);
// Warp threads are applied across the output
} else {
outputs_produced_per_block_iter *= bdimx;
}
// Decision to do a cross-warp reduction per block
if (red_elems_per_thread >= (bdimy * kMinValuesPerThread) ||
red_elems_per_thread >= kMaxValuesPerThread || !rparams.fastest_dim) {
inputs_consumed_per_block_iter *= bdimy;
red_elems_per_thread = ceilDiv(red_elems_per_thread, bdimy);
rparams.cross_block = true;
rparams.mul_reds_per_blk = false;
// Do multiple reductions per block
} else {
rparams.cross_block = false;
rparams.mul_reds_per_blk = true;
outputs_produced_per_block_iter *= bdimy;
}
// 5. Distributing work across blocks
// WARNING: Current device for codegen may not be the target device
int device_max_threads_per_multiprocessor =
at::cuda::getCurrentDeviceProperties()->maxThreadsPerMultiProcessor;
int device_multiprocessor_count =
at::cuda::getCurrentDeviceProperties()->multiProcessorCount;
int blocks_per_sm = device_max_threads_per_multiprocessor / (bdimx * bdimy);
int target_grid_size = device_multiprocessor_count * blocks_per_sm;
// Setting the number of blocks based on the number of outputs
gdimx = ceilDiv(red_outputs, outputs_produced_per_block_iter);
// Cross-block reductions (if necessary)
if (rparams.cross_block && red_elems_per_thread >= kMaxValuesPerThread &&
gdimx <= target_grid_size) {
int blks_per_out_1 = ceilDiv(target_grid_size, gdimx);
int blks_per_out_2 = ceilDiv(red_elems_per_thread, kMinValuesPerThread);
int blks_per_out_3 = ceilDiv(red_elems_per_thread, kMaxValuesPerThread);
int blks_per_output =
std::max(std::min(blks_per_out_1, blks_per_out_2), blks_per_out_3);
gdimy = std::max(1, blks_per_output);
// If a cross-block reduction was generated
if (blks_per_output > 1) {
rparams.cross_grid = true;
}
}
const char* debug_env = getenv("PYTORCH_CUDA_FUSER_RED_SCHED_DEBUG");
if (debug_env && atoi(debug_env)) {
std::cout << "\n===== Reduction Parameters ========" << std::endl
<< "Inputs:" << std::endl
<< "\tRed Elems: " << red_elems << " Red Outputs: " << red_outputs
<< " Red On Fastest Dim? " << red_on_fastest_dim << std::endl
<< "Reduction Characteristics:" << std::endl
<< "\tMultiple Reds Per Block? " << rparams.mul_reds_per_blk
<< " Cross Block? " << rparams.cross_block << " Cross Grid? "
<< rparams.cross_grid << std::endl
<< "Recommended Blocking:" << std::endl
<< "\tGridX: " << gdimx << " GridY: " << gdimy
<< " BlckX: " << bdimx << " BlckY: " << bdimy << std::endl
<< "====================================" << std::endl;
}
rparams.lparams = LaunchParams(
LaunchParams::UNINITIALIZED_VAL,
gdimy,
LaunchParams::UNINITIALIZED_VAL,
bdimx,
bdimy,
LaunchParams::UNINITIALIZED_VAL);
return rparams;
}
} // anonymous namespace
TORCH_CUDA_API c10::optional<ReductionParams> getReductionHeuristics(
Fusion* fusion,
const at::ArrayRef<c10::IValue>& fusion_inputs,
TensorView* red_tv) {
FUSER_PERF_SCOPE("scheduleReduction");
FusionGuard fg(fusion);
if (!fusion->hasReduction()) {
return c10::nullopt;
}
auto red_root_dom = red_tv->getRootDomain();
const bool red_on_fastest_dim =
red_root_dom[red_root_dom.size() - 1]->isReduction();
TORCH_INTERNAL_ASSERT(
red_tv != nullptr, "Reduction TensorView wasn't found.");
if (!fusion->hasReduction()) {
return c10::nullopt;
}
TORCH_INTERNAL_ASSERT(
red_tv->hasReduction(), "TensorView doesn't have a reduction.");
const auto red_expr = fusion->origin(red_tv);
TORCH_INTERNAL_ASSERT(
red_expr->getExprType() != c10::nullopt &&
red_expr->getExprType().value() == ExprType::ReductionOp,
"TensorView doesn't have a reduction.");
StatefulExpressionEvaluator evaluator(
executor_utils::statefulBindInputs(fusion_inputs, fusion));
int64_t red_outputs = 1;
int64_t red_elements = 1;
for (auto id : red_tv->getRootDomain()) {
auto inferred_val = evaluator.inferValue(id->rawExtent());
TORCH_INTERNAL_ASSERT(
inferred_val.has_value(), "Error inferring reduction size.");
if (id->isReduction()) {
red_elements *= inferred_val.value();
} else {
red_outputs *= inferred_val.value();
}
}
return reductionHeuristic(red_elements, red_outputs, red_on_fastest_dim);
}
// fusion is the input IR that will be modified by this function
void scheduleReduction(
Fusion* fusion,
const ReductionParams& rparams,
TensorView* red_tv,
std::vector<TensorView*> outs_of_red) {
FusionGuard fg(fusion);
// We coalesc all reduction axes to the right;
mergeReduction(red_tv);
// Merge all iteration dimensions
mergeNonReduction(red_tv);
for (auto iter_tv : outs_of_red) {
mergeNonReduction(iter_tv);
}
// Evaluate Dimensions of Reduction TensorView
auto red_ids = red_tv->domain()->domain();
TORCH_INTERNAL_ASSERT(
red_ids.size() == 2, "We coalesced all dimensions into 2 previously.");
constexpr int kLoopUnrollSplit = 4;
// Scheduling the Reduction
if (rparams.fastest_dim) {
// Do multiple reductions per block
if (rparams.mul_reds_per_blk) {
// Reduction Splits
// [outputs, |rF-Leftover, X-Warp, rf-Unroll|]
// Idx: 0 | 1(-1) 2(-2) 3(-1) |
// --------------------------------
// Reduction Dimensions
red_tv->split(1, rparams.loop_unroll);
red_tv->split(1, NamedScalar::getParallelDim(ParallelType::TIDx));
// Output Splits
// [|Out-Leftover, Out-PerBlock|, <Reduction Dims>]
// Idx: | 0 1 | 2(-2) -- 3(-1)
// ----------------------------
// Output Dimensions
red_tv->split(0, NamedScalar::getParallelDim(ParallelType::TIDy));
for (auto iter_tv : outs_of_red) {
iter_tv->split(0, NamedScalar::getParallelDim(ParallelType::TIDy));
}
auto red_tv_rf = red_tv->rFactor({-3, -1});
// WARNING: computeAt will coalesce the rFactored dimensions
// rFactored Reduction Tensor after computeAt():
// [<output dims>, | rF-Leftover, X-Warp, rF-Unroll|]
// Idx: 0 -- 1 | 2(-3) 3(-2) 4(-1) |
// ---------------------------------
// Reduction Dimensions
red_tv_rf->computeAt(red_tv, -1);
// After the Reduction Tensor has rFactoring applied
// Reduction Output Tensor:
// [Out-Leftover, Out-PerBlock, X-Warp]
// Idx: 0 1 2(-1)
if (!outs_of_red.empty()) {
red_tv->computeAt(outs_of_red[0], -1);
}
red_tv_rf->axis(-1)->parallelize(ParallelType::Unroll);
red_tv->axis(0)->parallelize(ParallelType::BIDx);
for (auto iter_tv : outs_of_red) {
iter_tv->axis(0)->parallelize(ParallelType::BIDx);
}
red_tv->axis(1)->parallelize(ParallelType::TIDy);
for (auto iter_tv : outs_of_red) {
iter_tv->axis(1)->parallelize(ParallelType::TIDy);
}
red_tv->axis(-1)->parallelize(ParallelType::TIDx);
// Bind Inputs to Reduction
for (auto input : fusion->inputsOf(red_tv_rf)) {
if (input->getValType().value() == ValType::TensorView) {
input->as<TensorView>()->computeAt(red_tv_rf, -1);
}
}
// Do a cross-warp reduction per block
} else {
if (rparams.cross_grid) {
// Reduction Splits
// [outputs, |rF-Leftover, X-Grid, X-Block, X-Warp, rf-Unroll|]
// Idx: 0 | 1(-5) 2(-4) 3(-3) 4(-2) 5(-1) |
// -------------------------------------------------
// Reduction Dimensions
red_tv->split(1, rparams.loop_unroll);
red_tv->split(1, NamedScalar::getParallelDim(ParallelType::TIDx));
red_tv->split(1, NamedScalar::getParallelDim(ParallelType::TIDy));
red_tv->split(1, NamedScalar::getParallelDim(ParallelType::BIDy));
auto red_tv_rf = red_tv->rFactor(
{-5, -1}); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
// WARNING: computeAt will coalesce the rFactored dimensions
// rFactored Reduction Tensor after computeAt():
// [Outputs, |X-Grid, X-Block, X-Warp, rF-Leftover, rF-Unroll|]
// Idx: 0 | 1(-5) 2(-4) 3(-3) 4(-2) 5(-1) |
// -------------------------------------------------
// Reduction Dimensions
red_tv_rf->computeAt(red_tv, -1);
// After the Reduction Tensor has rFactoring applied
// Reduction Output Tensor:
// [Outputs, X-Grid, X-Block, X-Warp]
// Idx: 0 1(-3) 2(-2) 3(-1)
if (!outs_of_red.empty()) {
red_tv->computeAt(outs_of_red[0], -1);
}
red_tv_rf->axis(-1)->parallelize(ParallelType::Unroll);
red_tv->axis(0)->parallelize(ParallelType::BIDx);
for (auto iter_tv : outs_of_red) {
iter_tv->axis(0)->parallelize(ParallelType::BIDx);
}
red_tv->axis(-1)->parallelize(ParallelType::TIDx);
red_tv->axis(-2)->parallelize(ParallelType::TIDy);
red_tv->axis(-3)->parallelize(ParallelType::BIDy);
// Bind Inputs to Reduction
for (auto input : fusion->inputsOf(red_tv_rf)) {
if (input->getValType().value() == ValType::TensorView) {
input->as<TensorView>()->computeAt(red_tv_rf, -1);
}
}
} else {
// Reduction Splits
// [outputs, |rF-Leftover, X-Block, X-Warp, rf-Unroll|]
// Idx: 0 | 1(-4) 2(-3) 3(-2) 4(-1) |
// -----------------------------------------
// Reduction Dimensions
red_tv->split(1, rparams.loop_unroll);
red_tv->split(1, NamedScalar::getParallelDim(ParallelType::TIDx));
red_tv->split(1, NamedScalar::getParallelDim(ParallelType::TIDy));
auto red_tv_rf = red_tv->rFactor({-4, -1});
// WARNING: computeAt will coalesce the rFactored dimensions
// rFactored Reduction Tensor after computeAt():
// [Outputs, |X-Block, X-Warp, rF-Leftover, rF-Unroll|]
// Idx: 0 | 1(-4) 2(-3) 3(-2) 4(-1) |
// -----------------------------------------
// Reduction Dimensions
red_tv_rf->computeAt(red_tv, -1);
// After the Reduction Tensor has rFactoring applied
// Reduction Output Tensor:
// [Outputs, X-Block, X-Warp]
// Idx: 0 1(-2) 2(-1)
if (!outs_of_red.empty()) {
red_tv->computeAt(outs_of_red[0], -1);
}
red_tv_rf->axis(-1)->parallelize(ParallelType::Unroll);
red_tv->axis(0)->parallelize(ParallelType::BIDx);
for (auto iter_tv : outs_of_red) {
iter_tv->axis(0)->parallelize(ParallelType::BIDx);
}
red_tv->axis(-1)->parallelize(ParallelType::TIDx);
red_tv->axis(-2)->parallelize(ParallelType::TIDy);
// Bind Inputs to Reduction
for (auto input : fusion->inputsOf(red_tv_rf)) {
if (input->getValType().value() == ValType::TensorView) {
input->as<TensorView>()->computeAt(red_tv_rf, -1);
}
}
}
}
} else {
if (rparams.cross_block) {
if (rparams.cross_grid) {
// Reduction Splits
// [outputs, |rF-Leftover, rf-Unroll, X-Grid, X-Block|]
// Idx: 0 | 1(-4) 2(-3) 3(-2) 4(-1) |
// -----------------------------------------
// Reduction Dimensions
red_tv->split(1, NamedScalar::getParallelDim(ParallelType::TIDy));
red_tv->split(1, NamedScalar::getParallelDim(ParallelType::BIDy));
red_tv->split(1, kLoopUnrollSplit);
// Reordering the Unroll dimension eases applying computeAt()
// for preceeding operations and the rFactored Tensor.
// |--- Reordered ----|
// V V
// [outputs, |rF-Leftover, X-Block, X-Grid, rF-Unroll|]
// Idx: 0 | 1(-4) 2(-3) 3(-2) 4(-1) |
// -----------------------------------------
// Reduction Dimensions
red_tv->reorder({{-1, -3}, {-3, -1}});
// Output Splits
// [|Out-Leftover, Out-PerBlock|, <Reduction Dims>]
// Idx: | 0 1 | 2(-4) -- 5(-1)
// ----------------------------
// Output Dimensions
red_tv->split(0, NamedScalar::getParallelDim(ParallelType::TIDx));
for (auto iter_tv : outs_of_red) {
iter_tv->split(0, NamedScalar::getParallelDim(ParallelType::TIDx));
}
auto red_tv_rf = red_tv->rFactor({-4, -1});
// WARNING: computeAt will coalesce the rFactored dimensions
// rFactored Reduction Tensor after computeAt():
// [<output dims>, |X-Block, X-Grid, rF-Leftover, rF-Unroll|]
// Idx: 0 -- 1 | 2(-4) 3(-3) 4(-2) 5(-1) |
// -----------------------------------------
// Reduction Dimensions
red_tv_rf->computeAt(red_tv, -1);
// After the Reduction Tensor has rFactoring applied
// Reduction Output Tensor:
// [Out-Leftover, Out-PerBlock, X-Block, X-Grid]
// Idx: 0 1 2(-2) 3(-1)
if (!outs_of_red.empty()) {
red_tv->computeAt(outs_of_red[0], -1);
}
red_tv_rf->axis(-1)->parallelize(ParallelType::Unroll);
red_tv->axis(0)->parallelize(ParallelType::BIDx);
for (auto iter_tv : outs_of_red) {
iter_tv->axis(0)->parallelize(ParallelType::BIDx);
iter_tv->axis(1)->parallelize(ParallelType::TIDx);
}
red_tv->axis(-3)->parallelize(ParallelType::TIDx);
red_tv->axis(-2)->parallelize(ParallelType::TIDy);
red_tv->axis(-1)->parallelize(ParallelType::BIDy);
// Bind Inputs to Reduction
for (auto input : fusion->inputsOf(red_tv_rf)) {
if (input->getValType().value() == ValType::TensorView) {
input->as<TensorView>()->computeAt(red_tv_rf, -1);
}
}
} else {
// Reduction Splits
// [outputs, |rF-Leftover, rf-Unroll, X-Block|]
// Idx: 0 | 1(-3) 2(-2) 3(-1) |
// ---------------------------------
// Reduction Dimensions
red_tv->split(1, NamedScalar::getParallelDim(ParallelType::TIDy));
red_tv->split(1, kLoopUnrollSplit);
// Reordering the Unroll dimension eases applying computeAt()
// for preceeding operations and the rFactored Tensor.
// |- Reordered -|
// V V
// [outputs, |rF-Leftover, X-Block, rF-Unroll|]
// Idx: 0 | 1(-3) 2(-2) 3(-1) |
// ---------------------------------
// Reduction Dimensions
red_tv->reorder({{-1, -2}, {-2, -1}});
// Output Splits
// [|Out-Leftover, Out-PerBlock|, <Reduction Dims>]
// Idx: | 0 1 | 2(-3) -- 4(-1)
// ----------------------------
// Output Dimensions
red_tv->split(0, NamedScalar::getParallelDim(ParallelType::TIDx));
for (auto iter_tv : outs_of_red) {
iter_tv->split(0, NamedScalar::getParallelDim(ParallelType::TIDx));
}
auto red_tv_rf = red_tv->rFactor({-3, -1});
// WARNING: computeAt will coalesce the rFactored dimensions
// rFactored Reduction Tensor after computeAt():
// [<output dims>, |X-Block, rF-Leftover, rF-Unroll|]
// Idx: 0 -- 1 | 2(-3) 3(-2) 4(-1) |
// ---------------------------------
// Reduction Dimensions
red_tv_rf->computeAt(red_tv, -1);
// After the Reduction Tensor has rFactoring applied
// Reduction Output Tensor:
// [Out-Leftover, Out-PerBlock, X-Block]
// Idx: 0 1 2(-1)
if (!outs_of_red.empty()) {
red_tv->computeAt(outs_of_red[0], -1);
}
red_tv_rf->axis(-1)->parallelize(ParallelType::Unroll);
red_tv->axis(0)->parallelize(ParallelType::BIDx);
for (auto iter_tv : outs_of_red) {
iter_tv->axis(0)->parallelize(ParallelType::BIDx);
iter_tv->axis(1)->parallelize(ParallelType::TIDx);
}
red_tv->axis(-2)->parallelize(ParallelType::TIDx);
red_tv->axis(-1)->parallelize(ParallelType::TIDy);
// Bind Inputs to Reduction
for (auto input : fusion->inputsOf(red_tv_rf)) {
if (input->getValType().value() == ValType::TensorView) {
input->as<TensorView>()->computeAt(red_tv_rf, -1);
}
}
}
} else {
red_tv->split(0, NamedScalar::getParallelDim(ParallelType::TIDx));
for (auto iter_tv : outs_of_red) {
iter_tv->split(0, NamedScalar::getParallelDim(ParallelType::TIDx));
}
if (!outs_of_red.empty()) {
red_tv->computeAt(outs_of_red[0], -1);
}
red_tv->axis(0)->parallelize(ParallelType::BIDx);
red_tv->axis(1)->parallelize(ParallelType::TIDx);
for (auto iter_tv : outs_of_red) {
iter_tv->axis(0)->parallelize(ParallelType::BIDx);
iter_tv->axis(1)->parallelize(ParallelType::TIDx);
}
for (auto input : fusion->inputsOf(red_tv)) {
if (input->getValType().value() == ValType::TensorView) {
input->as<TensorView>()->computeAt(red_tv, -1);
}
}
}
}
}
} // namespace cuda
} // namespace fuser
} // namespace jit
} // namespace torch
|