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
|
#include <torch/csrc/jit/codegen/cuda/ir_graphviz.h>
#include <torch/csrc/jit/codegen/cuda/fusion.h>
#include <torch/csrc/jit/codegen/cuda/ir_all_nodes.h>
#include <torch/csrc/jit/codegen/cuda/ir_builder.h>
#include <torch/csrc/jit/codegen/cuda/type.h>
#include <fstream>
namespace torch {
namespace jit {
namespace fuser {
namespace cuda {
namespace {
// Private helper, generating node labels for IrGraphGenerator
class IrNodeLabel : private OptInConstDispatch {
using DetailLevel = IrGraphGenerator::DetailLevel;
public:
static std::string gen(
const Statement* node,
DetailLevel detail_level = DetailLevel::Basic) {
IrNodeLabel generator(detail_level);
generator.OptInConstDispatch::handle(node);
return generator.label_.str();
}
private:
explicit IrNodeLabel(DetailLevel detail_level)
: detail_level_(detail_level) {}
~IrNodeLabel() override = default;
void handle(const Bool* b) override {
if (b->isSymbolic()) {
label_ << "b" << b->name();
} else {
if (detail_level_ >= DetailLevel::Explicit) {
label_ << "b" << b->name() << "=";
}
label_ << *b->value();
}
}
void handle(const Double* d) override {
if (d->isSymbolic()) {
label_ << "d" << d->name();
} else {
if (detail_level_ >= DetailLevel::Explicit) {
label_ << "d" << d->name() << "=";
}
label_ << *d->value();
}
}
void handle(const Int* i) override {
if (i->isSymbolic()) {
label_ << "i" << i->name();
} else {
if (detail_level_ >= DetailLevel::Explicit) {
label_ << "i" << i->name() << "=";
}
label_ << *i->value();
}
}
void handle(const NamedScalar* ns) override {
label_ << ns->name();
}
void handle(const IterDomain* id) override {
label_ << id->getIterType();
label_ << id->getParallelType();
label_ << "(";
if (!id->start()->isZeroInt()) {
label_ << IrNodeLabel::gen(id->start()) << " : ";
}
label_ << IrNodeLabel::gen(id->extent());
label_ << ")";
}
void handle(const Split* split) override {
label_ << "Split(inner=" << (split->innerSplit() ? "true" : "false")
<< ", factor=" << IrNodeLabel::gen(split->factor()) << ")";
}
void handle(const Merge* merge) override {
label_ << "Merge";
}
private:
std::stringstream label_;
const DetailLevel detail_level_;
};
// Small color palette from the X11 theme
static const char* getColorFromIndex(size_t index) {
const size_t number_of_colors = 10;
index = index % number_of_colors;
switch (index) {
case 0: // NOLINT(cppcoreguidelines-avoid-magic-numbers)
return "azure";
case 1: // NOLINT(cppcoreguidelines-avoid-magic-numbers)
return "pink";
case 2: // NOLINT(cppcoreguidelines-avoid-magic-numbers)
return "green";
case 3: // NOLINT(cppcoreguidelines-avoid-magic-numbers)
return "grey";
case 4: // NOLINT(cppcoreguidelines-avoid-magic-numbers)
return "yellow";
case 5: // NOLINT(cppcoreguidelines-avoid-magic-numbers)
return "lavender";
case 6: // NOLINT(cppcoreguidelines-avoid-magic-numbers)
return "cyan";
case 7: // NOLINT(cppcoreguidelines-avoid-magic-numbers)
return "white";
case 8: // NOLINT(cppcoreguidelines-avoid-magic-numbers)
return "magenta";
case 9: // NOLINT(cppcoreguidelines-avoid-magic-numbers)
return "red";
default:
break;
}
return "";
}
} // anonymous namespace
void IrGraphGenerator::print(
const Fusion* fusion,
const char* filename,
DetailLevel detail_level,
ExprColorMap* expr_color_map) {
std::ofstream dot_file(filename);
TORCH_CHECK(dot_file.good(), "Failed to open the IR graph file");
dot_file << toGraphviz(fusion, detail_level, expr_color_map);
}
std::string IrGraphGenerator::toGraphviz(
const Fusion* fusion,
DetailLevel detail_level,
ExprColorMap* expr_color_map) {
IrGraphGenerator ir_graph(fusion, detail_level, expr_color_map);
return ir_graph.generate();
}
IrGraphGenerator::IrGraphGenerator(
const Fusion* fusion,
DetailLevel detail_level,
ExprColorMap* expr_color_map)
: detail_level_(detail_level),
fusion_(fusion),
expr_color_map_(expr_color_map) {
// setup inputs & outputs
// (indexes used to quickly check if a value is fusion input or output)
for (const auto* input : fusion->inputs()) {
TORCH_CHECK(inputs_.count(input) == 0);
inputs_.insert(input);
}
for (const auto* output : fusion->outputs()) {
TORCH_CHECK(outputs_.count(output) == 0);
outputs_.insert(output);
}
}
std::string IrGraphGenerator::getid(const Statement* stm) {
const auto it = id_map_.find(stm);
if (it == id_map_.end()) {
// First reference, generate a new id
std::stringstream new_id;
new_id << "stm_" << next_id_++;
id_map_.insert({stm, new_id.str()});
return new_id.str();
} else {
return it->second;
}
}
void IrGraphGenerator::addArc(
const Statement* src,
const Statement* dst,
const std::string& style) {
// We automatically visit (handle) the arc's source and destination
handle(src);
handle(dst);
// generate and queue the arc definition
std::stringstream arc_def;
arc_def << getid(src) << " -> " << getid(dst) << " " << style;
arcs_.push_back(arc_def.str());
}
void IrGraphGenerator::printExpr(const Expr* expr, const std::string& label) {
graph_def_ << " " << getid(expr) << " "
<< "[label=\"" << label << "\", shape=oval, color=blue, "
<< "style=filled, fillcolor=";
if (expr_color_map_ != nullptr && expr_color_map_->count(expr)) {
graph_def_ << getColorFromIndex(expr_color_map_->at(expr));
} else {
graph_def_ << "azure";
}
graph_def_ << "];\n";
}
void IrGraphGenerator::printValue(const Val* val, const std::string& label) {
graph_def_ << " " << getid(val) << " [label=\"" << label
<< "\", shape=rect, color=green, fontsize=10];\n";
}
std::string IrGraphGenerator::generate() {
// IrGraphGenerator instances are not reusable
TORCH_CHECK(graph_def_.str().empty());
TORCH_CHECK(visited_.empty());
// record detail level
graph_def_ << "// detail level: ";
switch (detail_level_) {
case DetailLevel::ComputeOnly:
graph_def_ << "compute only\n";
break;
case DetailLevel::Basic:
graph_def_ << "minimal\n";
break;
case DetailLevel::Explicit:
graph_def_ << "explicit\n";
break;
case DetailLevel::Verbose:
graph_def_ << "verbose\n";
break;
default:
TORCH_CHECK(!"Unexpected detail level");
}
graph_def_ << "digraph fusion_ir {\n"
<< " node [shape=circle, color=gray];\n"
<< " edge [color=black];\n";
// Compute graph
generateComputeGraph();
// Schedule graph
if (detail_level_ > DetailLevel::ComputeOnly) {
generateScheduleGraph();
}
// All expressions & values
// (These are otherwise unreacheable (dead) nodes)
if (detail_level_ >= DetailLevel::Verbose) {
for (const auto* expr : fusion_->unordered_exprs()) {
handle(expr);
}
for (const auto* val : fusion_->vals()) {
handle(val);
}
}
// Finally, print all arc definitions
for (const auto& arc : arcs_) {
graph_def_ << " " << arc << ";\n";
}
graph_def_ << "}\n";
// Make sure that all referenced nodes have been visited
for (const auto& kv : id_map_) {
TORCH_CHECK(visited(kv.first));
}
return graph_def_.str();
}
void IrGraphGenerator::generateComputeGraph() {
graph_def_ << " subgraph cluster_compute {\n"
<< " label=\"compute\";\n"
<< " style=dashed;\n";
// Inputs
for (const auto* input : fusion_->inputs()) {
handle(input);
}
// Outputs
for (const auto* output : fusion_->outputs()) {
handle(output);
}
graph_def_ << " }\n";
}
void IrGraphGenerator::generateScheduleGraph() {
graph_def_ << " subgraph cluster_schedule {\n"
<< " label=\"schedule\";\n"
<< " style=dashed;\n";
// Connect TensorView with their TensorDomain
// (this will trigger the traversal of the schedule graph)
for (auto tv : tensor_views_) {
addArc(tv->domain(), tv, "[style=dashed, arrowhead=none]");
if (detail_level_ >= DetailLevel::Explicit) {
// Maybe not the best way to handle the root domain, but should be okay
addArc(
tv,
IrBuilder::create<TensorDomain>(tv->getRootDomain()),
"[style=dashed, color=green, arrowhead=none]");
if (tv->domain()->hasRFactor())
addArc(
tv,
IrBuilder::create<TensorDomain>(tv->domain()->getRFactorDomain()),
"[style=dashed, color=green, arrowhead=none]");
}
}
graph_def_ << " }\n";
}
void IrGraphGenerator::handle(const Statement* s) {
OptInConstDispatch::handle(s);
}
void IrGraphGenerator::handle(const Val* v) {
if (!visited(v)) {
visited_.insert(v);
if (const auto* def = v->definition()) {
handle(def);
}
OptInConstDispatch::handle(v);
}
}
void IrGraphGenerator::handle(const Expr* e) {
if (!visited(e)) {
visited_.insert(e);
OptInConstDispatch::handle(e);
}
}
void IrGraphGenerator::handle(const TensorDomain* td) {
graph_def_ << " " << getid(td) << " [label=\"TensorDomain\", "
<< "shape=note, color=gray, "
<< "style=filled, fillcolor=gray90, fontsize=10];\n";
for (auto iter_domain : td->domain()) {
addArc(iter_domain, td, "[color=gray]");
}
}
void IrGraphGenerator::handle(const IterDomain* id) {
graph_def_ << " " << getid(id) << " [label=\"" << IrNodeLabel::gen(id)
<< "\", shape=cds, color=gray, fontsize=10];\n";
if (!id->start()->isZeroInt()) {
addArc(id->start(), id, "[color=gray]");
}
addArc(id->extent(), id, "[color=gray]");
}
void IrGraphGenerator::handle(const Bool* b) {
printValue(b, IrNodeLabel::gen(b, detail_level_));
}
void IrGraphGenerator::handle(const Double* d) {
printValue(d, IrNodeLabel::gen(d, detail_level_));
}
void IrGraphGenerator::handle(const Int* i) {
printValue(i, IrNodeLabel::gen(i, detail_level_));
}
void IrGraphGenerator::handle(const ComplexDouble* i) {
printValue(i, IrNodeLabel::gen(i, detail_level_));
}
void IrGraphGenerator::handle(const NamedScalar* i) {
printValue(i, IrNodeLabel::gen(i, detail_level_));
}
void IrGraphGenerator::handle(const TensorView* tv) {
std::stringstream label;
label << "{T" << tv->name() << "|";
label << "{";
bool first_axis = true;
for (auto iter_domain : tv->domain()->domain()) {
if (first_axis) {
first_axis = false;
} else {
label << "|";
}
label << IrNodeLabel::gen(iter_domain);
}
label << "}}";
const bool is_input = inputs_.find(tv) != inputs_.end();
const bool is_output = outputs_.find(tv) != outputs_.end();
const char* style = is_input ? "style=filled, fillcolor=palegreen"
: is_output ? "style=filled, fillcolor=lightblue"
: "style=filled, fillcolor=beige";
graph_def_ << " " << getid(tv) << " [label=\"" << label.str()
<< "\", shape=Mrecord, color=brown, " << style << "];\n";
tensor_views_.push_back(tv);
}
void IrGraphGenerator::handle(const ARangeOp* uop) {
// node
printExpr(uop, "arange");
// inputs & outputs
addArc(uop->start(), uop);
addArc(uop->end(), uop);
addArc(uop->step(), uop);
addArc(uop, uop->output(0));
}
void IrGraphGenerator::handle(const UnaryOp* uop) {
// node
std::stringstream label;
label << uop->getUnaryOpType();
printExpr(uop, label.str());
// inputs & outputs
addArc(uop->in(), uop);
addArc(uop, uop->out());
}
void IrGraphGenerator::handle(const BinaryOp* bop) {
// node
std::stringstream label;
label << bop->getBinaryOpType();
printExpr(bop, label.str());
// inputs & outputs
addArc(bop->lhs(), bop);
addArc(bop->rhs(), bop, "[color=blue]");
addArc(bop, bop->out());
}
void IrGraphGenerator::handle(const TernaryOp* op) {
// node
std::stringstream label;
label << op->getTernaryOpType();
printExpr(op, label.str());
// inputs & outputs
addArc(op->in1(), op);
addArc(op->in2(), op, "[color=blue]");
addArc(op->in3(), op, "[color=brown]");
addArc(op, op->out());
}
void IrGraphGenerator::handle(const RNGOp* op) {
// node
std::stringstream label;
label << op->getRNGOpType();
printExpr(op, label.str());
// inputs & outputs
addArc(op, op->output(0));
}
void IrGraphGenerator::handle(const BroadcastOp* op) {
printExpr(op, "Broadcast");
addArc(op->in(), op);
addArc(op, op->out());
}
void IrGraphGenerator::handle(const ReductionOp* op) {
// node
std::stringstream label;
label << "Reduction(" << op->getReductionOpType() << ")";
printExpr(op, label.str());
// inputs & outputs
addArc(op->in(), op);
addArc(op->init(), op, "[color=blue]");
addArc(op, op->out());
}
void IrGraphGenerator::handle(const Split* split) {
printExpr(split, IrNodeLabel::gen(split));
addArc(split->in(), split);
addArc(split, split->outer());
addArc(split, split->inner());
}
void IrGraphGenerator::handle(const Merge* merge) {
printExpr(merge, IrNodeLabel::gen(merge));
addArc(merge->outer(), merge);
addArc(merge->inner(), merge);
addArc(merge, merge->out());
}
} // namespace cuda
} // namespace fuser
} // namespace jit
} // namespace torch
|