Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix alloc and unroll logic #214

Merged
merged 2 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions test/cpp/jit/test_gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1219,8 +1219,8 @@ void testGPU_FusionSimplePWise() {
tv3->merge(0);

// Split by n_threads
tv3->split(-1, 128 * 2);
tv3->split(-1, 128);
tv3->split(0, 128);
tv3->split(0, 4);

// For all inputs, computeAt the output inline, temporaries should be squeezed
// between them
Expand All @@ -1229,7 +1229,7 @@ void testGPU_FusionSimplePWise() {

// Parallelize TV3
tv3->axis(0)->parallelize(ParallelType::BIDx);
tv3->axis(-2)->parallelize(ParallelType::TIDy);
tv3->axis(-2)->parallelize(ParallelType::Unroll);
tv3->axis(-1)->parallelize(ParallelType::TIDx);

auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
Expand Down
24 changes: 17 additions & 7 deletions torch/csrc/jit/codegen/cuda/lower_loops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,35 @@ Expr* LoopNestGenerator::pushAlloc(TensorView* tv) {
if (tv->hasComputeAt() && alloc_pos == tv->getThisComputeAtAxis()) {
break;
}
// If we found an unroll, we want to place the allocation outside the unroll
if (alloc_pos < tv->nDims() &&
tv->getComputeAtAxis(alloc_pos).first->parallel_method() ==
ParallelType::Unroll) {
break;
}
alloc_pos++;
}

// Grab the dimensions the allocation will be based on
std::vector<Val*> alloc_dims;
for (auto i = alloc_pos; i < tv->nDims(); i++) {
IterDomain* dim = tv->getComputeAtAxis(i).first;
IterDomain* compute_at_dim = tv->getComputeAtAxis(i).first;
IterDomain* local_dim = tv->axis(i);
if (
// If shared memory, don't use any IDs bound to a grid dimension
(tv->memory_type_ == MemoryType::Shared && dim->isBlockDim()) ||
(tv->memory_type_ == MemoryType::Shared &&
compute_at_dim->isBlockDim()) ||
// If local memory, don't use any IDs bound to a grid or block dimension
(tv->memory_type_ == MemoryType::Local && dim->isThread()) ||
(tv->memory_type_ == MemoryType::Local && compute_at_dim->isThread()) ||
// If we're reducing this dimension, don't use it in the allocation
// computation
dim->isReduction() ||
local_dim->isReduction() ||
// If this is a broadcast dimension, don't use it in the allocation
// computation
dim->isBroadcast())
local_dim->isBroadcast()) {
continue;
alloc_dims.push_back(dim->extent());
}
alloc_dims.push_back(compute_at_dim->extent());
}

// Multiply all the dimensions we're going to use for the allocation together
Expand Down Expand Up @@ -282,8 +291,9 @@ void LoopNestGenerator::handle(Expr* expr) {
Expr* alloc_stmt = nullptr;
// 3) Allocate the output.
if (!FusionGuard::getCurFusion()->hasInput(out) &&
!FusionGuard::getCurFusion()->hasOutput(out))
!FusionGuard::getCurFusion()->hasOutput(out)) {
alloc_stmt = pushAlloc(out);
}

// 4) If this is a reduction, initialize the output (open for loops to inner
// most, predicate, initialize, place next after allocation if exists, close
Expand Down