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
|
#include <torch/csrc/jit/codegen/cuda/arith.h>
#include <torch/csrc/jit/codegen/cuda/index_compute.h>
#include <torch/csrc/jit/codegen/cuda/ir_iostream.h>
#include <torch/csrc/jit/codegen/cuda/kernel_ir_builder.h>
#include <torch/csrc/jit/codegen/cuda/lower2device.h>
#include <torch/csrc/jit/codegen/cuda/lower_utils.h>
#include <torch/csrc/jit/codegen/cuda/predicate_compute.h>
#include <torch/csrc/jit/codegen/cuda/lower_index.h>
namespace torch {
namespace jit {
namespace fuser {
IndexLowering::IndexLowering() : ir_builder_(GpuLower::current()->kernel()) {}
Val* IndexLowering::lowerOperand(Val* op, Val* out) const {
if (ir_utils::isTV(op)) {
return Index::getProducerIndex(
ir_utils::asTV(op),
ir_utils::asTV(out),
scope_utils::getLoops(active_scope_expr));
} else {
return GpuLower::lowerValue(op);
}
}
Val* IndexLowering::lowerOutput(Expr* expr) const {
TORCH_CHECK(expr->outputs().size() == 1);
const auto out = expr->output(0);
if (ir_utils::isTVOp(expr)) {
return Index::getConsumerIndex(
ir_utils::asTV(out), scope_utils::getLoops(active_scope_expr));
} else {
return GpuLower::lowerValue(out);
}
}
void IndexLowering::pushBack(Expr* expr) {
if (active_scope == nullptr) {
lowered_exprs.push_back(expr);
} else {
active_scope->push_back(expr);
}
}
void IndexLowering::handle(kir::IfThenElse* ite) {
Expr* prev_scope_expr = active_scope_expr;
kir::Scope* prev_scope = active_scope;
auto new_ite =
ir_builder_.create<kir::IfThenElse>(ite->cond(), prev_scope_expr);
pushBack(new_ite);
active_scope_expr = new_ite;
active_scope = &new_ite->thenBody();
for (auto expr : ite->thenBody().exprs()) {
OptInDispatch::handle(expr);
}
active_scope = &new_ite->elseBody();
for (auto expr : ite->elseBody().exprs()) {
OptInDispatch::handle(expr);
}
active_scope = prev_scope;
active_scope_expr = prev_scope_expr;
}
void IndexLowering::handle(kir::ForLoop* fl) {
Expr* prev_scope_expr = active_scope_expr;
kir::Scope* prev_scope = active_scope;
auto newFl = ir_builder_.create<kir::ForLoop>(
fl->index(), fl->iter_domain(), prev_scope_expr);
pushBack(newFl);
active_scope_expr = newFl;
active_scope = &newFl->body();
for (auto expr : fl->body().exprs()) {
OptInDispatch::handle(expr);
}
active_scope = prev_scope;
active_scope_expr = prev_scope_expr;
}
void IndexLowering::handle(UnaryOp* uop) {
if (ir_utils::isTVOp(uop)) {
const auto in = lowerOperand(uop->in(), uop->out());
const auto out = lowerOutput(uop);
pushBack(ir_builder_.create<kir::UnaryOp>(uop->getUnaryOpType(), out, in));
} else {
// This will automatically lower the expression defining the value
pushBack(GpuLower::lowerValue(uop->out())->getOrigin());
}
}
void IndexLowering::handle(BinaryOp* bop) {
if (ir_utils::isTVOp(bop)) {
const auto lhs = lowerOperand(bop->lhs(), bop->out());
const auto rhs = lowerOperand(bop->rhs(), bop->out());
const auto out = lowerOutput(bop);
pushBack(ir_builder_.create<kir::BinaryOp>(
bop->getBinaryOpType(), out, lhs, rhs));
} else {
// This will automatically lower the expression defining the value
pushBack(GpuLower::lowerValue(bop->out())->getOrigin());
}
}
void IndexLowering::handle(TernaryOp* top) {
if (ir_utils::isTVOp(top)) {
const auto in1 = lowerOperand(top->in1(), top->out());
const auto in2 = lowerOperand(top->in2(), top->out());
const auto in3 = lowerOperand(top->in3(), top->out());
const auto out = lowerOutput(top);
pushBack(ir_builder_.create<kir::TernaryOp>(
top->getTernaryOpType(), out, in1, in2, in3));
} else {
// This will automatically lower the expression defining the value
pushBack(GpuLower::lowerValue(top->out())->getOrigin());
}
}
namespace {
void allocateGridReductionFlag(TensorView* out_tv, Expr* current_scope_expr) {
kir::IrBuilder ir_builder(GpuLower::current()->kernel());
auto flag_name = kir::GridReduction::getPredicateFlagName(out_tv);
auto flag_var = ir_builder.create<kir::Allocate>(
ir_builder.create<kir::NamedScalar>(flag_name, DataType::Bool),
MemoryType::Local,
ir_builder.create<kir::Int>(1));
// When enclosed by IfThenElse, place the variable outside of the
// IfThenElse. This IfThenElse is assumed to be the prediate for
// this grid reduction expression.
if (current_scope_expr->getExprType() == ExprType::IfThenElse) {
scope_utils::insertBefore(
scope_utils::getParent(current_scope_expr),
current_scope_expr,
flag_var);
} else {
scope_utils::pushBack(current_scope_expr, flag_var);
}
}
} // namespace
void IndexLowering::handle(ReductionOp* rop) {
TORCH_INTERNAL_ASSERT(
ir_utils::isTVOp(rop),
"Cannot have a reduction operation on something other than a tensor view, but received ",
rop);
auto out_tv = ir_utils::asTV(rop->out());
const bool is_block_reduce = out_tv->hasBlockReduction();
const bool is_grid_reduce = out_tv->hasGridReduction();
// If we do a grid reduction we can't have a reduction axis that is not bound
// to a grid or block dim ()
if (is_grid_reduce) {
TORCH_INTERNAL_ASSERT(
std::none_of(
out_tv->domain()->domain().begin(),
out_tv->domain()->domain().end(),
[](IterDomain* id) {
return !id->isThread() && id->isReduction();
}),
"Found a reduction stage that has both a non-parallelized reduction and a grid reduction.",
" This is not supported, please use rfactor to do the serialized reduction first, then the grid reduction.");
}
const auto loops = scope_utils::getLoops(active_scope_expr);
kir::TensorIndex* out = Index::getConsumerIndex(out_tv, loops);
kir::TensorIndex* in = Index::getProducerIndex(
ir_utils::asTV(rop->in()), ir_utils::asTV(rop->out()), loops);
kir::ReductionOp* block_reduction_op = nullptr;
if (is_block_reduce) {
auto pred =
PredicateCompute::getInlinePredicate(rop, loops, nullptr, false);
block_reduction_op = ir_builder_.create<kir::ReductionOp>(
rop->getReductionOpType(),
GpuLower::lowerValue(rop->init()),
out,
in,
pred);
pushBack(block_reduction_op);
}
if (is_grid_reduce) {
// First, declare a boolean flag variable storing the return value
// of gridReduce.
allocateGridReductionFlag(out_tv, active_scope_expr);
std::vector<IterDomain*> buffer_ids(out_tv->domain()->domain());
buffer_ids.erase(
std::remove_if(
buffer_ids.begin(),
buffer_ids.end(),
[](IterDomain* id) {
return id->isReduction() & !id->isBlockDim();
}),
buffer_ids.end());
Val* buffer_size =
buffer_ids.empty() ? new Int(1) : buffer_ids[0]->rawExtent();
for (size_t i = 1; i < buffer_ids.size(); i++) {
buffer_size = mul(buffer_size, buffer_ids[i]->rawExtent());
}
std::vector<IterDomain*> sync_ids(out_tv->domain()->domain());
sync_ids.erase(
std::remove_if(
sync_ids.begin(),
sync_ids.end(),
[](IterDomain* id) {
return id->isReduction() || !id->isBlockDim();
}),
sync_ids.end());
Val* sync_size = sync_ids.empty() ? new Int(1) : sync_ids[0]->rawExtent();
for (size_t i = 1; i < sync_ids.size(); i++) {
sync_size = mul(sync_size, sync_ids[i]->rawExtent());
}
IterDomain* buffer_id = new IterDomain(new Int(0), buffer_size);
TensorView* reduce_buffer_tv = new TensorView(
new TensorDomain({buffer_id}),
out->getDataType().value(),
MemoryType::Global);
IterDomain* sync_id = new IterDomain(new Int(0), sync_size);
TensorView* reduce_sync_tv = new TensorView(
new TensorDomain({sync_id}), DataType::Int, MemoryType::Global);
const auto reduce_buffer = ir_builder_.create<kir::Allocate>(
GpuLower::lowerValue(reduce_buffer_tv),
reduce_sync_tv->getMemoryType());
const auto sync_buffer = ir_builder_.create<kir::Allocate>(
GpuLower::lowerValue(reduce_sync_tv),
reduce_sync_tv->getMemoryType(),
nullptr,
true);
const auto grid_reduction_op = block_reduction_op == nullptr
? ir_builder_.create<kir::ReductionOp>(
rop->getReductionOpType(),
GpuLower::lowerValue(rop->init()),
out,
in)
: block_reduction_op;
auto pred =
PredicateCompute::getInlinePredicate(rop, loops, nullptr, false);
const auto grid_reduction = ir_builder_.create<kir::GridReduction>(
grid_reduction_op, reduce_buffer, sync_buffer, pred);
pushBack(reduce_buffer);
pushBack(sync_buffer);
pushBack(grid_reduction);
}
if (!is_block_reduce && !is_grid_reduce) {
pushBack(ir_builder_.create<kir::BinaryOp>(
rop->getReductionOpType(), out, out, in));
}
}
void IndexLowering::handle(BroadcastOp* bop) {
TORCH_INTERNAL_ASSERT(
ir_utils::isTVOp(bop),
"Cannot have a broadcast operation on something other than a tensor view, but received ",
bop);
auto loops = scope_utils::getLoops(active_scope_expr);
kir::TensorIndex* out =
Index::getConsumerIndex(ir_utils::asTV(bop->out()), loops);
Val* in = bop->in();
if (ir_utils::isTV(in))
in = Index::getProducerIndex(
ir_utils::asTV(in), ir_utils::asTV(bop->out()), loops);
pushBack(ir_builder_.create<kir::BroadcastOp>(out, in));
}
void IndexLowering::handle(kir::Allocate* allocate) {
pushBack(allocate);
}
void IndexLowering::handle(kir::Sync* sync) {
pushBack(sync);
}
void IndexLowering::generate(const std::vector<Expr*>& exprs) {
// Run through loop nests and further lower the expressions
for (auto* expr : exprs) {
OptInDispatch::handle(expr);
}
}
} // namespace fuser
} // namespace jit
} // namespace torch
|