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 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
|
#include <torch/csrc/jit/passes/tensorexpr_fuser.h>
#include <ATen/core/interned_strings.h>
#include <ATen/core/symbol.h>
#include <ATen/record_function.h>
#include <c10/util/FunctionRef.h>
#include <c10/util/irange.h>
#include <torch/csrc/jit/codegen/cuda/interface.h>
#include <torch/csrc/jit/codegen/fuser/interface.h>
#include <torch/csrc/jit/ir/alias_analysis.h>
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/jit_opt_limit.h>
#include <torch/csrc/jit/passes/common_subexpression_elimination.h>
#include <torch/csrc/jit/passes/constant_pooling.h>
#include <torch/csrc/jit/passes/dead_code_elimination.h>
#include <torch/csrc/jit/passes/pass_manager.h>
#include <torch/csrc/jit/passes/remove_redundant_profiles.h>
#include <torch/csrc/jit/passes/symbolic_shape_runtime_fusion.h>
#include <torch/csrc/jit/passes/utils/subgraph_utils.h>
#include <torch/csrc/jit/runtime/custom_operator.h>
#include <torch/csrc/jit/runtime/graph_executor.h>
#include <torch/csrc/jit/runtime/operator_options.h>
#include <torch/csrc/jit/runtime/symbolic_shape_registry.h>
#include <torch/csrc/jit/runtime/symbolic_shape_registry_util.h>
#include <torch/csrc/jit/tensorexpr/kernel.h>
#include <torch/csrc/utils/memory.h>
// NOLINTNEXTLINE
C10_DEFINE_bool(
torch_jit_disable_cat,
false,
"disable aten::cat in TE fusion groups");
C10_DEFINE_bool(
torch_jit_enable_dynamic_shape_fusion,
false,
"enable TE fusion using dynamic shapes");
namespace torch {
namespace jit {
static bool texpr_reductions_enabled = false;
bool isSupportedForBlock(Node* node) {
switch (node->kind()) {
case aten::add:
case aten::mul:
return true;
default:
return false;
}
}
bool usedOnlyInSize(Value* v) {
const auto& uses = v->uses();
return std::all_of(uses.begin(), uses.end(), [](const Use& u) {
return u.user->matches("aten::size(Tensor self) -> int[]");
});
}
Value* broadcastSizes(at::ArrayRef<Value*> sizes, AliasDb* db) {
AT_ASSERT(!sizes.empty());
Graph* graph = sizes[0]->owningGraph();
Node* broadcast_n =
graph->insertNode(graph->create(prim::BroadcastSizes, sizes));
broadcast_n->output()->setType(ListType::ofInts());
db->createValue(broadcast_n->output());
return broadcast_n->output();
}
namespace tensorexpr {
OperatorSet& getCustomOperatorSet() {
static OperatorSet _g_custom_operator_set{};
return _g_custom_operator_set;
}
static const OperatorSet& supported_non_eltwise_set() {
// clang-format off
static const OperatorSet supported_non_eltwise_set{
"aten::batch_norm(Tensor input, Tensor? weight, Tensor? bias, Tensor? running_mean, Tensor? running_var, bool training, float momentum, float eps, bool cudnn_enabled) -> Tensor",
"aten::conv2d(Tensor input, Tensor weight, Tensor? bias=None, int[2] stride=1, int[2] padding=0, int[2] dilation=1, int groups=1) -> Tensor",
"aten::_convolution(Tensor input, Tensor weight, Tensor? bias, int[] stride, int[] padding, int[] dilation, bool transposed, int[] output_padding, int groups, bool benchmark, bool deterministic, bool cudnn_enabled, bool allow_tf32) -> Tensor",
"aten::matmul(Tensor self, Tensor other) -> Tensor",
};
// clang-format on
return supported_non_eltwise_set;
};
bool isSupported(Node* node) {
// For Block codegen we allow limited ops.
if (tensorexpr::getTEGenerateBlockCode()) {
return isSupportedForBlock(node);
}
static const OperatorSet supported_reduction_set{
"aten::sum(Tensor self, *, ScalarType? dtype=None) -> Tensor",
"aten::sum.dim_IntList(Tensor self, int[1]? dim, bool keepdim=False, *, ScalarType? dtype=None) -> Tensor",
"aten::softmax.int(Tensor self, int dim , ScalarType? dtype=None) -> Tensor",
"aten::log_softmax.int(Tensor self, int dim, ScalarType? dtype=None) -> Tensor",
};
static const OperatorSet supported_misc_set{
"aten::cat(Tensor[] tensors, int dim=0) -> Tensor",
"aten::unsqueeze(Tensor(a) self, int dim) -> Tensor(a)",
};
// clang-format on
if (get_tensorexpr_elementwise_set().contains(node) ||
node->isMemberOf(supported_non_eltwise_set()) ||
node->isMemberOf(supported_misc_set) ||
node->isMemberOf(getCustomOperatorSet()) ||
(texpr_reductions_enabled && node->isMemberOf(supported_reduction_set))) {
// We only insert guards on Tensor types, so we rely on the output
// of a node being uniquely determined by its input types.
// bail if any non-Tensor input affects the output type
// and cannot be reasoned about statically
// Value is either an int or a float (can occur from .item())
for (Value* v : node->inputs()) {
if (v->type()->cast<NumberType>()) {
return false;
}
}
// non-const dtype / device
for (auto arg_name : {"dtype", "device"}) {
if (auto index = node->schema().argumentIndexWithName(arg_name)) {
if (!toIValue(node->input(*index))) {
return false;
}
}
}
if (FLAGS_torch_jit_disable_cat && node->kind() == aten::cat) {
return false;
}
return true;
}
// unschematized ops
switch (node->kind()) {
case prim::ConstantChunk:
case prim::ListConstruct:
case prim::TensorExprGroup:
return true;
}
return false;
}
} // namespace tensorexpr
static bool texpr_fuser_enabled_ = true;
void setTensorExprFuserEnabled(bool val) {
texpr_fuser_enabled_ = val;
}
bool tensorExprFuserEnabled() {
static const char* enable_c_str = std::getenv("PYTORCH_TENSOREXPR");
if (!enable_c_str) {
return texpr_fuser_enabled_;
}
if (std::string(enable_c_str) == "0") {
return false;
}
return true;
}
bool tensorExprDynamicShapeFusionEnabled() {
return FLAGS_torch_jit_enable_dynamic_shape_fusion;
}
void setTensorExprDynamicShapeFusionEnabled(bool val) {
FLAGS_torch_jit_enable_dynamic_shape_fusion = val;
}
bool setTexprReductionsEnabled(bool value) {
bool old_value = texpr_reductions_enabled;
texpr_reductions_enabled = value;
return old_value;
}
bool texprReductionsEnabled() {
return texpr_reductions_enabled;
}
void removeProfileNodesAndSpecializeTypes(Block* b) {
for (auto it = b->nodes().begin(); it != b->nodes().end(); it++) {
if (it->kind() == prim::profile) {
GRAPH_DEBUG("Removing prim::profile: %", it->output()->debugName());
it->output()->replaceAllUsesWith(it->input());
auto profiled_type = it->ty(attr::profiled_type)->expect<TensorType>();
TensorTypePtr input_tensor_type = nullptr;
bool input_is_optional = false;
if (it->input()->type()->kind() == c10::TypeKind::TensorType) {
input_tensor_type = it->input()->type()->expect<TensorType>();
} else {
input_tensor_type = it->input()
->type()
->expectRef<OptionalType>()
.getElementType()
->expect<TensorType>();
input_is_optional = true;
}
if (input_is_optional) {
it.destroyCurrent();
continue;
}
// A value can be profiled with differently typed uses.
// This can occur from:
// - having a use which is not executed, so the type will be
// TensorType::get()
// - control-flow that depends on tensor type:
// if x.size() == 2 op(x) else op(x)
// - mutation of the value on a field represented in the tensor type
// op(x); x.resize_([...]); op(x)
// The most common case today with num_profiles = 1 is from the first
// case. Here we can just ignore non-profiled uses, and choose any of the
// profiled uses. Because we guard all tensor types in the runtime, even
// if we set a Value to have a profiled type from one use and then execute
// a use with a different profiled type, we will still be correct.
// In the future we could consider unifying the types of uses, or adding a
// type refinement node so uses can have the correct corresponding type.
if (profiled_type == TensorType::get()) {
continue;
}
// If we encounter non-identical profiled types for the same value, merge
// them. This situation can happen if, e.g., loop unrolling duplicates
// profiled types in a loop body in a manner that isn't logically
// consistent (see TestTEFuser.test_unrolled_cat).
if (input_tensor_type == TensorType::get()) {
it->input()->setType(profiled_type);
} else {
it->input()->setType(input_tensor_type->merge(*profiled_type));
}
it.destroyCurrent();
} else {
for (Block* ib : it->blocks()) {
removeProfileNodesAndSpecializeTypes(ib);
}
}
}
}
void RemoveProfileNodesAndSpecializeTypes(std::shared_ptr<Graph>& graph) {
GRAPH_DEBUG("Before removeProfileNodesAndSpecializeTypes:\n", *graph);
removeProfileNodesAndSpecializeTypes(graph->block());
GRAPH_DEBUG("After removeProfileNodesAndSpecializeTypes:\n", *graph);
}
bool hasTensorTypeSpecialization(Value* v) {
if (!v->type()->cast<TensorType>()) {
return false;
}
// Constants & TensorExprGroup will always produce specialized tensor type,
// TypeCheck are inserted by this pass and only used by fusion groups that
// insert proper guards
if (v->node()->kind() == prim::Constant ||
v->node()->kind() == prim::TypeCheck ||
v->node()->kind() == prim::TensorExprGroup) {
return false;
}
if (v->type() == TensorType::get()) {
return false;
}
return true;
}
void removeTensorTypeSpecialization(Value* v) {
if (hasTensorTypeSpecialization(v)) {
v->setType(TensorType::get());
}
}
void removeTensorTypeSpecializations(Block* block) {
for (Value* v : block->inputs()) {
removeTensorTypeSpecialization(v);
}
for (Node* n : block->nodes()) {
for (Block* b : n->blocks()) {
removeTensorTypeSpecializations(b);
}
for (Value* v : n->outputs()) {
removeTensorTypeSpecialization(v);
}
}
}
void RemoveTensorTypeSpecializations(std::shared_ptr<Graph>& graph) {
removeTensorTypeSpecializations(graph->block());
}
void insertTypeGuard(
Node* guarded_node,
tensor_type_converter_t type_converter,
Symbol kind) {
GRAPH_DEBUG("Inserting a typecheck guard for a node", *guarded_node);
auto subgraph = SubgraphUtils::getSubgraph(guarded_node);
// Fixup types of the subgraph inputs
std::vector<Value*> inputs_to_check;
std::vector<TypePtr> guard_types;
for (Value* input : guarded_node->inputs()) {
// We only check inputs of the guarded nodes and expect user to infer
// intermediates and outputs shapes
if (!input->type()->cast<TensorType>()) {
continue;
}
// fusion outputs are already guarded
if (input->node()->kind() == prim::Constant ||
input->node()->kind() == prim::FusionGroup) {
continue;
}
inputs_to_check.push_back(input);
guard_types.push_back(type_converter(input->type()->expect<TensorType>()));
}
if (!inputs_to_check.size()) {
return;
}
// Add prim::TypeCheck node
//
// TypeCheck nodes look like the following:
// %out1 : Float(2, 3), %out2 : Int(10, 30), %types_match : bool =
// prim::TypeCheck(%inp1 : Tensor, %inp2 : Tensor)
//
// They have N inputs whose types we are going to check and N+1 outputs. The
// first N outputs specify expected types and N+1-th output holds the result
// of the check (bool).
Node* typecheck_node =
guarded_node->owningGraph()
->create(kind, inputs_to_check, inputs_to_check.size() + 1)
->insertBefore(guarded_node);
typecheck_node->tys_(attr::types, guard_types);
Value* typecheck_result = typecheck_node->output(inputs_to_check.size());
std::unordered_map<Value*, Value*> typechecked_inputs;
for (size_t i = 0; i < typecheck_node->inputs().size(); ++i) {
typechecked_inputs[typecheck_node->input(i)] = typecheck_node->output(i);
}
// Fixup types of the typecheck node outputs, which are used by the op in
// execution
typecheck_node->output(inputs_to_check.size())->setType(BoolType::get());
for (size_t i = 0; i < typecheck_node->inputs().size(); ++i) {
typecheck_node->output(i)->setType(typecheck_node->input(i)->type());
}
// Insert if
auto versioning_if =
guarded_node->owningGraph()
->create(prim::If, {typecheck_result}, guarded_node->outputs().size())
->insertAfter(typecheck_node);
for (size_t idx = 0; idx < guarded_node->outputs().size(); ++idx) {
versioning_if->output(idx)->setType(guarded_node->output(idx)->type());
guarded_node->output(idx)->replaceAllUsesWith(versioning_if->output(idx));
}
auto true_block = versioning_if->addBlock();
auto false_block = versioning_if->addBlock();
// Fill in the false block. It should contain the unoptimized
// copy of the fused subgraph.
WithInsertPoint guard(false_block->return_node());
const auto subgraph_outputs = insertGraph(
*guarded_node->owningGraph(), *subgraph, guarded_node->inputs());
for (Value* output : subgraph_outputs) {
false_block->registerOutput(output);
}
// types get copied to the fallback graph, so remove specializations before
// replacing
removeTensorTypeSpecializations(false_block);
replaceBlockWithFallbackGraph(false_block, guarded_node->inputs());
// Fill in the true block. It has all inputs type-checked and its
// body should be the fusion group node.
guarded_node->moveBefore(true_block->return_node());
for (size_t idx = 0; idx < guarded_node->inputs().size(); ++idx) {
if (typechecked_inputs.count(guarded_node->input(idx))) {
guarded_node->replaceInput(
idx, typechecked_inputs.at(guarded_node->input(idx)));
}
}
for (Value* output : guarded_node->outputs()) {
true_block->registerOutput(output);
}
}
namespace {
bool has_unsupported_pin_memory(const Node* node) {
// cant support non-constant pin_memory or pin_memory = True
if (auto maybe_index = node->schema().argumentIndexWithName("pin_memory")) {
int index = *maybe_index;
auto inp = node->input(index);
if (inp->type() != NoneType::get() &&
constant_as<bool>(inp).value_or(true)) {
return true;
}
}
return false;
}
} // namespace
class TensorExprFuser {
public:
TensorExprFuser(
std::shared_ptr<Graph> graph,
size_t min_group_size,
bool add_composed_op,
bool fuse_to_dynamic_shapes)
: graph_(std::move(graph)),
min_group_size_(min_group_size),
add_composed_op_(add_composed_op),
fuse_to_dynamic_shapes_(fuse_to_dynamic_shapes) {
parseTENotFuseOption();
}
// Builds up expressions that compute shapes of all intermediates (and
// outputs) of the fusion group, based on the sizes of inputs. You should run
// DCE to remove those that you end up not using.
std::unordered_map<Value*, Value*> buildShapeExpressions(Node* fusion_group) {
GRAPH_DUMP("buildShapeExpressions for ", fusion_group->g(attr::Subgraph));
WithInsertPoint insert_guard{fusion_group->next()};
std::unordered_map<Value*, Value*> shape_of;
Graph* graph = fusion_group->owningGraph();
auto subgraph = fusion_group->g(attr::Subgraph);
auto inputs = fusion_group->inputs();
auto sinputs = subgraph->inputs();
AT_ASSERT(inputs.size() == sinputs.size());
for (const auto i : c10::irange(inputs.size())) {
if (inputs[i]->type()->isSubtypeOf(*TensorType::get())) {
Value* soutput = graph->insert(aten::size, {inputs[i]});
aliasDb_->createValue(soutput);
GRAPH_DEBUG(
"Adding a mapping for %",
sinputs[i]->debugName(),
" ",
getHeader(soutput->node()));
shape_of[sinputs[i]] = soutput;
}
}
// When we have a guarantee that an output won't be removed, because it's
// used in expressions that don't involve size checks, we can use its size
// instead of computing a long chain of broadcasts, starting from the
// beginning of the kernel.
auto outputs = fusion_group->outputs();
auto soutputs = subgraph->outputs();
AT_ASSERT(outputs.size() == soutputs.size());
for (const auto i : c10::irange(outputs.size())) {
if (usedOnlyInSize(outputs[i]))
continue;
Value* soutput = graph->insert(aten::size, {outputs[i]});
aliasDb_->createValue(soutput);
shape_of[soutputs[i]] = soutput;
}
for (Node* n : subgraph->nodes()) {
auto tensor_inputs = filter(n->inputs(), [](Value* v) {
return v->type()->isSubtypeOf(*TensorType::get());
});
GRAPH_DEBUG("Building sizes for ", getHeader(n));
bool all_inputs_have_sizes = true;
auto shapes = fmap(tensor_inputs, [&](Value* v) {
GRAPH_DEBUG("Getting aten::size for %", v->debugName());
all_inputs_have_sizes &= shape_of.count(v);
return shape_of.count(v) != 0 ? shape_of.at(v) : nullptr;
});
if (!all_inputs_have_sizes) {
GRAPH_DEBUG(
"Not all tensor arguments have sizes available to compute the broadcasted size",
getHeader(n));
continue;
}
if (n->kind() == prim::ConstantChunk) {
Node* sizes_node = graph->insertNode(
graph->create(prim::ChunkSizes, shape_of.at(n->input()), 2));
sizes_node->i_(attr::dim, n->i(attr::dim));
sizes_node->i_(attr::chunks, n->i(attr::chunks));
for (Value* output : sizes_node->outputs()) {
aliasDb_->createValue(output);
}
Value* regular_size = sizes_node->outputs().at(0);
Value* last_size = sizes_node->outputs().at(1);
regular_size->setType(ListType::ofInts());
last_size->setType(ListType::ofInts());
auto outputs = n->outputs();
for (Value* o : outputs.slice(0, outputs.size() - 1)) {
shape_of.emplace(o, regular_size);
}
shape_of.emplace(outputs.at(outputs.size() - 1), last_size);
continue;
}
// we only support shape calculations for elementwise, some
// non-elementwise like batch_norm, conv, matmul, and
// a few exceptions (e.g. prim::ConstantChunk, etc) listed above
if (!(get_tensorexpr_elementwise_set().contains(n)) &&
!n->isMemberOf(tensorexpr::supported_non_eltwise_set())) {
continue;
}
shape_of.emplace(
n->output(),
shapes.size() == 1 ? shapes[0]
: broadcastSizes(shapes, aliasDb_.get()));
}
return shape_of;
}
void removeOutputsUsedOnlyInSize(Node* fusion_group) {
if (fusion_group->kind() != prim::TensorExprGroup)
return;
auto subgraph = fusion_group->g(attr::Subgraph);
auto shape_of = buildShapeExpressions(fusion_group);
auto outputs = fusion_group->outputs().vec();
auto soutputs = subgraph->outputs().vec();
// XXX: Iterating in this order is not only good for performance reasons!
// It is also crucial for correctness (i has to reflect the current true
// index of outputs[i])!
for (int64_t i = static_cast<int64_t>(outputs.size()) - 1; i >= 0; --i) {
auto output = outputs[i];
auto soutput = soutputs[i];
if (usedOnlyInSize(output) && shape_of.count(soutput) > 0) {
auto uses = output->uses();
for (Use u : uses) {
AT_ASSERT(u.user->matches("aten::size(Tensor self) -> int[]"));
u.user->output()->replaceAllUsesWith(shape_of.at(soutput));
u.user->destroy();
}
fusion_group->eraseOutput(i);
subgraph->eraseOutput(i);
}
}
}
void run() {
aliasDb_ = torch::make_unique<AliasDb>(graph_);
RemoveRedundantProfiles(graph_);
GRAPH_DUMP("After removing redundant profile nodes: ", graph_);
createFusionGroups(graph_->block());
GRAPH_DUMP("After creating fusion groups: ", graph_);
// we maintain alias db correctness during initial fusion, but it is
// difficult to maintain correctness after inlining so inline only after
// fusion is done.
inlineSmallFusionGroups(graph_->block());
GRAPH_DUMP("After inlining small fusion groups: ", graph_);
if (fuse_to_dynamic_shapes_) {
VLOG(1) << "TensorExpr fusion with dynamic shapes is enabled"
<< std::endl;
generalizeFusionGroups(graph_->block());
GRAPH_DUMP("After generalizing fusion groups: ", graph_);
} else {
prepareFusionGroupAndGuardOutputs(graph_->block());
GRAPH_DUMP("After guarding fusion groups: ", graph_);
}
}
private:
Node* getOrCreateTensorExprSubgraph(Node* n) {
if (n->hasAttribute(attr::Subgraph) && n->kind() == prim::TensorExprGroup) {
return n;
}
GRAPH_UPDATE("Creating a tensorexpr::Group node from: ", *n);
return SubgraphUtils::createSingletonSubgraphAndUpdateAliasing(
n, prim::TensorExprGroup, *aliasDb_);
}
value_list sortReverseTopological(ArrayRef<Value*> inputs, Block* b) {
value_list result;
for (auto i : inputs) {
if (i->node()->owningBlock() == b) {
result.push_back(i);
}
}
// Sort in reverse topological order
std::sort(result.begin(), result.end(), [&](Value* a, Value* b) {
return a->node()->isAfter(b->node());
});
return result;
}
// Create a fusion group starting from the node N.
// We then try to pull inputs into the fusion group and repeat that process
// until there is nothing we can pull in.
std::pair<graph_node_list::iterator, bool> createFusionGroup(
Node* fusion_node) {
// Allow single-node groups containing conv2d, since we'll only select
// those in cases where the tensorexpr implementation is faster than the
// aten implementation.
if (min_group_size_ == 1 || fusion_node->kind() == aten::conv2d) {
fusion_node = getOrCreateTensorExprSubgraph(fusion_node);
}
GRAPH_DEBUG("Iteratively pull input nodes into the fusion group...\n");
auto inputs = sortReverseTopological(
fusion_node->inputs(), fusion_node->owningBlock());
for (auto input : inputs) {
debugDumpFusionGroup("Current fusion group: ", fusion_node);
GRAPH_DEBUG("Trying to merge: ", *input->node());
if (auto maybe_fusion_group = tryMerge(fusion_node, input->node())) {
// we successfully merged, so the new group's `inputs` may have
// changed. So rescan the new group for more merging opportunities.
return std::make_pair(
maybe_fusion_group.value()->reverseIterator(), true);
}
}
return std::make_pair(++fusion_node->reverseIterator(), false);
}
static void debugDumpFusionGroup(const std::string& msg, Node* n) {
// NOLINTNEXTLINE(clang-analyzer-core.NonNullParamChecker)
GRAPH_DEBUG(msg, *n);
// NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage)
if (n->kind() == prim::TensorExprGroup) {
GRAPH_DEBUG(*n->g(attr::Subgraph));
}
}
// No Ops in eager shouldn't be outputs of Fusion Groups because it
// will degrade perf and change aliasing relationships
static bool unexecutedEagerOp(Node* n) {
if (n->kind() != aten::to &&
n->kind() != aten::_autocast_to_reduced_precision &&
n->kind() != aten::_autocast_to_full_precision) {
return false;
}
return *n->input(0)->type()->expect<TensorType>() ==
*n->output()->type()->expect<TensorType>();
}
std::pair<graph_node_list::iterator, bool> scanNode(Node* n) {
GRAPH_DEBUG("Considering node:", *n)
if (!canHandle(n)) {
return std::make_pair(++n->reverseIterator(), false);
}
// There are some nodes that we can support, but we don't want to start a
// fusion group from - skip them.
if (n->kind() == prim::ListConstruct || n->kind() == aten::slice ||
n->kind() == aten::unsqueeze || n->kind() == prim::ConstantChunk ||
n->kind() == prim::Constant || unexecutedEagerOp(n)) {
return std::make_pair(++n->reverseIterator(), false);
}
return createFusionGroup(n);
}
// Merge fusible nodes into subgraphs in prim::TensorExprGroup nodes.
void createFusionGroups(Block* block) {
bool any_changed = true;
while (any_changed) {
any_changed = false;
for (auto it = block->nodes().rbegin(); it != block->nodes().rend();) {
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
bool changed;
std::tie(it, changed) = scanNode(*it);
any_changed |= changed;
}
}
for (Node* n : block->nodes()) {
for (Block* b : n->blocks()) {
createFusionGroups(b);
}
}
// Try to merge adjacent fusion groups together. Because we have only merged
// by looking at graph inputs, without this we would not attempt to merge
// adjacent fusion groups that don't have a depdency on each other
std::vector<Node*> initial_fusion_groups;
for (Node* n : block->nodes()) {
if (n->kind() == prim::TensorExprGroup) {
initial_fusion_groups.push_back(n);
}
}
Node* prev_fusion_group =
initial_fusion_groups.size() ? initial_fusion_groups[0] : nullptr;
for (const auto i : c10::irange(1, initial_fusion_groups.size())) {
// Try merging the just created fusion group into the previous one.
// If it did not work, then put the previous fusion group into
// fusion_groups vector - we will not touch it anymore in this loop.
// If merging suceeded, save the merged group as the "previous" fusion
// group so that we can try to merge the next one into it.
Node* fusion_group = initial_fusion_groups[i];
debugDumpFusionGroup(
"Trying to merge into the previous fusion group: ",
prev_fusion_group);
if (auto merged_fusion_group =
tryMerge(prev_fusion_group, fusion_group)) {
prev_fusion_group = *merged_fusion_group;
debugDumpFusionGroup(
"Successfully merged into the previous fusion group: ",
prev_fusion_group);
} else {
GRAPH_DEBUG("Cannot merge into the previous fusion group");
prev_fusion_group = fusion_group;
}
}
}
size_t blockSize(Block* block) {
size_t num = 0;
for (Node* n : block->nodes()) {
// Don't count prim::Constants and prim::ListConstructs as these are nodes
// we only pull in along with another, "main", node. E.g. the
// ListConstruct nodes would also be pulled into a fusion group if they
// are inputs of an aten::cat node.
if (n->kind() == prim::Constant || n->kind() == prim::ListConstruct) {
continue;
}
for (Block* b : n->blocks()) {
num += blockSize(b);
}
num++;
}
return num;
}
bool hasConv(Block* block) {
for (Node* n : block->nodes()) {
if (n->kind() == aten::conv2d) {
return true;
}
}
return false;
}
bool inlineIfTooSmall(Node* n) {
if (n->kind() != prim::TensorExprGroup) {
return false;
}
auto subgraph = SubgraphUtils::getSubgraph(n);
size_t num_nodes = blockSize(subgraph->block());
// Allow small subgraphs containing conv2d, since we'll only select those
// in cases where the tensorexpr implementation is faster than the aten
// implementation.
if (num_nodes < min_group_size_ && !hasConv(subgraph->block())) {
GRAPH_UPDATE("Fusion group is too small, unmerging: ", *n);
SubgraphUtils::unmergeSubgraph(n);
return true;
}
// Cleanup the subgraph from duplicated constants while we're at it.
ConstantPooling(subgraph);
if (GRAPH_DEBUG_ENABLED) {
GRAPH_EXPORT("", subgraph);
}
return false;
}
void inlineSmallFusionGroups(Block* block) {
for (auto it = block->nodes().begin(); it != block->nodes().end();) {
Node* n = *it;
it++;
for (Block* b : n->blocks()) {
inlineSmallFusionGroups(b);
}
inlineIfTooSmall(n);
}
}
c10::optional<Node*> tryMerge(Node* fusion_group, Node* to_merge) {
if (!canMerge(fusion_group, to_merge)) {
return c10::nullopt;
}
std::vector<Node*> nodes_to_merge = {to_merge};
if (to_merge->kind() == aten::cat) {
Node* listconstruct = to_merge->input(0)->node();
nodes_to_merge.push_back(listconstruct);
}
// First, try to move all the nodes we want to fuse next to the fusion
// group.
Node* move_point = fusion_group;
for (auto n : nodes_to_merge) {
GRAPH_UPDATE("Trying to move node next to fusion group: ", getHeader(n));
if (!aliasDb_->moveBeforeTopologicallyValid(n, move_point)) {
GRAPH_UPDATE("Failed to move because of AliasDB checks!");
return c10::nullopt;
}
move_point = n;
}
// Now all the nodes that we're going to fuse are moved next to the fusion
// group, so we can safely merge them into the fusion group subgraph.
fusion_group = getOrCreateTensorExprSubgraph(fusion_group);
for (auto n : nodes_to_merge) {
GRAPH_UPDATE("Merging ", getHeader(n));
SubgraphUtils::mergeNodeIntoSubgraphAndUpdateAliasing(
n, fusion_group, *aliasDb_);
}
return fusion_group;
}
bool shapeIsKnown(Value* v) {
if (v->type()->cast<TensorType>()) {
if (!v->isCompleteTensor()) {
return false;
}
}
return true;
}
bool allShapesAreKnown(Node* node) {
// TODO: Relax the checks to support dynamic shapes
for (Value* input : node->inputs()) {
if (!shapeIsKnown(input)) {
return false;
}
if (input->node()->kind() == prim::ListConstruct) {
if (!allShapesAreKnown(input->node())) {
return false;
}
}
}
for (Value* output : node->outputs()) {
if (!shapeIsKnown(output)) {
return false;
}
}
return true;
}
bool canFuseOnDevice(Value* v) {
auto type = v->type()->cast<TensorType>();
if (!type) {
return true;
}
auto device = type->device();
if (!device) {
return false;
}
if (device->is_cpu()) {
return canFuseOnCPU();
} else if (device->is_cuda()) {
#ifndef C10_MOBILE
if (fuser::cuda::isEnabled()) {
return false;
}
#endif
return canFuseOnGPU();
} else if (device->is_xpu()) {
return false;
}
return false;
}
bool isFusableOnDevice(Node* node) {
for (const auto& input : node->inputs()) {
if (input->node()->kind() == prim::ListConstruct) {
if (!isFusableOnDevice(input->node())) {
return false;
}
}
if (!canFuseOnDevice(input)) {
return false;
}
}
return true;
}
bool typesAreSupported(Node* node) {
// clang-format off
// breaks up the schema strings so they are no longer discoverable with ctrl-F
static const OperatorSet float_only_operator_set{
"aten::fmod.Scalar(Tensor self, Scalar other) -> Tensor",
"aten::fmod.Tensor(Tensor self, Tensor other) -> Tensor",
"aten::remainder.Scalar(Tensor self, Scalar other) -> Tensor",
"aten::remainder.Tensor(Tensor self, Tensor other) -> Tensor",
};
static const OperatorSet int_only_operator_set{
"aten::__lshift__.Scalar(Tensor self, Scalar other) -> Tensor",
"aten::__lshift__.Tensor(Tensor self, Tensor other) -> Tensor",
"aten::__rshift__.Scalar(Tensor self, Scalar other) -> Tensor",
"aten::__rshift__.Tensor(Tensor self, Tensor other) -> Tensor",
};
static const OperatorSet cpu_compute_heavy_set{
"aten::conv2d(Tensor input, Tensor weight, Tensor? bias=None, int[2] stride=1, int[2] padding=0, int[2] dilation=1, int groups=1) -> Tensor",
"aten::_convolution(Tensor input, Tensor weight, Tensor? bias, int[] stride, int[] padding, int[] dilation, bool transposed, int[] output_padding, int groups, bool benchmark, bool deterministic, bool cudnn_enabled, bool allow_tf32) -> Tensor",
"aten::matmul(Tensor self, Tensor other) -> Tensor",
};
static const OperatorSet gpu_only_operator_set{
// On CPU, these are slower and less accurate than ATen kernels, because
// ATen is able to use MKL-VML, whereas the fuser currently can't. The
// fuser uses sleef instead because sleef provides functions that operate
// on vectors, instead of large buffers.
"aten::erf(Tensor self) -> Tensor",
"aten::erfc(Tensor self) -> Tensor",
};
static const OperatorSet pow{
"aten::pow.Tensor_Scalar(Tensor self, Scalar exponent) -> Tensor",
};
// clang-format on
// Check types of input values.
for (const Value* v : node->inputs()) {
if (auto const& tt = v->type()->cast<TensorType>()) {
auto const& st = tt->scalarType();
auto const& device = tt->device();
// All tensors must be typed.
if (!st || !device) {
return false;
}
// Byte tensors introduce too many corner cases in type promotion.
// Better not to try to handle them.
if (*st == c10::ScalarType::Byte) {
return false;
}
// Float16 support has some issues (see e.g. #61336 and #61382), so for
// now it's disabled. There seem to be some problems in HalfRewriter,
// but on top of that Float16 has a few kinks on LLVM. Thus, on CPU we
// additionally disable it until we either move to a more stable version
// or find workarounds.
if (*st == c10::ScalarType::Half && *device == c10::kCPU) {
return false;
}
if (*st == c10::ScalarType::BFloat16 && *device == c10::kCPU) {
#ifndef TORCH_ENABLE_LLVM
return false;
#endif
}
// These operators only support floats, because integer divisors need to
// raise ZeroDivisionError.
if (node->isMemberOf(float_only_operator_set) && !isFloatingType(*st)) {
return false;
}
// These operators have complicated casting rules for floats.
if (node->isMemberOf(int_only_operator_set) && isFloatingType(*st)) {
return false;
}
} else if (node->isMemberOf(float_only_operator_set)) {
// Check scalar operands of float-only ops.
if (!v->type()->cast<FloatType>()) {
return false;
}
} else if (node->isMemberOf(int_only_operator_set)) {
if (!v->type()->cast<IntType>()) {
return false;
}
}
}
// aten::pow has special rules to avoid complicated integer cases. We
// expect the first arg to be a floating point tensor, and if that's the
// case the type of the scalar exponent doesn't matter.
if (node->isMemberOf(pow)) {
auto const& tt = node->input(0)->type()->cast<TensorType>();
if (!tt) {
return false;
}
auto const& st = tt->scalarType();
if (!st || !isFloatingType(*st)) {
return false;
}
}
// Operator is only supported on CPU.
if (node->isMemberOf(cpu_compute_heavy_set)) {
if (fuse_to_dynamic_shapes_) {
return false;
}
auto device = tensorexpr::pickDeviceType(node->inputs());
if (!device) {
device = tensorexpr::pickDeviceType(node->outputs());
}
if (!device || !device->is_cpu()) {
return false;
}
}
// Operator is only supported on GPU.
if (node->isMemberOf(gpu_only_operator_set)) {
auto device = tensorexpr::pickDeviceType(node->inputs());
if (!device) {
device = tensorexpr::pickDeviceType(node->outputs());
}
if (!device || !device->is_cuda()) {
return false;
}
}
if (node->kind() == aten::to) {
// only support same-device conversion
auto device = tensorexpr::pickDeviceType(node->inputs());
auto output_device = tensorexpr::pickDeviceType(node->outputs());
if (!device || !output_device || *device != *output_device) {
return false;
}
// non_blocking only applies in cross-device conversion, which we bail on
// copy arg only applies if op is a no-op, which we dont start fusion
// group from memory format is separately handled in NNC output
// all non-Tensor arguments must be constant
for (size_t i = 1; i < node->inputs().size(); i++) {
if (node->inputs().at(i)->node()->kind() != prim::Constant) {
return false;
}
}
if (has_unsupported_pin_memory(node)) {
return false;
}
}
if (node->kind() == aten::_autocast_to_reduced_precision ||
node->kind() == aten::_autocast_to_full_precision) {
for (auto i : c10::irange(1, node->inputs().size())) {
if (node->inputs().at(i)->node()->kind() != prim::Constant) {
return false;
}
}
bool is_reduced_precision =
node->kind() == aten::_autocast_to_reduced_precision;
bool is_full_precision =
node->kind() == aten::_autocast_to_full_precision;
auto self_tensor = node->inputs()[0]; // input tensor
if (auto const& tt = self_tensor->type()->cast<TensorType>()) {
auto st = tt->scalarType();
if (!st.has_value()) {
return false;
}
auto device = tt->device();
if (!device.has_value()) {
return false;
}
bool is_cpu = device->is_cpu();
if (*st != at::kFloat && is_reduced_precision && is_cpu) {
// Regarding CPU, aten would do nothing if the data type is
// float. Then the aten performance is better than NNC. So NNC
// does not pull it into its fusion group.
return false;
}
if (*st != at::kBFloat16 && is_full_precision && is_cpu) {
// Regarding CPU, aten would do nothing if the data type is
// BFloat16. Then the aten performance is better than NNC. So NNC
// does not pull it into its fusion group.
return false;
}
}
if (has_unsupported_pin_memory(node)) {
return false;
}
}
if (node->kind() == aten::unsqueeze) {
// `dim` argument must be a constant.
if (node->input(1)->node()->kind() != prim::Constant) {
return false;
}
}
if (node->kind() == aten::_convolution && !tensorexpr::isConv2d(node)) {
GRAPH_DEBUG("This aten::_convolution node is not a 2D conv");
return false;
}
if (node->kind() == aten::_convolution || node->kind() == aten::conv2d) {
if (!tensorexpr::conv2dIsSupportedJit(node) &&
!tensorexpr::mkldnnPrepackedConvIsSupportedJit(node)) {
GRAPH_DEBUG("Params of conv2d are not supported");
return false;
}
}
if (node->kind() == aten::matmul) {
if (!tensorexpr::matmulIsSupported(node)) {
GRAPH_DEBUG("Shapes of matmul inputs are not supported");
return false;
}
}
return true;
}
#define REQ(cond) \
if (!(cond)) { \
GRAPH_DEBUG("Failed cond " #cond "\n"); \
return false; \
}
bool canHandle(Node* node) {
REQ(allShapesAreKnown(node));
REQ(isFusableOnDevice(node));
REQ(operators_not_to_fuse.find(node->kind()) ==
operators_not_to_fuse.end());
for (Value* input : node->inputs()) {
if (auto const& tt = input->type()->cast<TensorType>()) {
auto st = tt->scalarType();
if (!st) {
// All tensor types should be known.
return false;
}
if (c10::isComplexType(*st) || c10::isQIntType(*st)) {
return false;
}
}
}
if (node->kind() == aten::cat) {
REQ(node->input(0)->node()->kind() == prim::ListConstruct);
REQ(node->input(0)->uses().size() == 1);
REQ(node->input(1)->node()->kind() == prim::Constant);
auto const& listconstruct = node->input(0)->node();
REQ(tensorexpr::pickDeviceType(listconstruct->inputs()));
} else {
REQ(tensorexpr::pickDeviceType(node->inputs()));
}
// Only fuse aten::batch_norm when the parameter 'training' is false
if (node->kind() == aten::batch_norm) {
REQ(node->input(5)->node()->kind() == prim::Constant);
REQ(!toIValue(node->input(5)).value().toBool());
}
REQ(tensorexpr::isSupported(node));
REQ(typesAreSupported(node));
// A hook to optimizations limitter to allow bisecting the pass
REQ(JIT_OPT_ALLOWED);
if (fuse_to_dynamic_shapes_) {
// Allow only if the node has a shape function defined.
// ListConstruct node is an exception since that is needed to fuse
// aten::cat, though it does not have a shape function.
REQ(node->kind() == prim::ListConstruct ||
node->kind() == prim::TensorExprGroup ||
node->isMemberOf(tensorexpr::getCustomOperatorSet()) ||
(node->maybeSchema() && shapeComputeGraphForSchema(node->schema())));
}
return true;
}
bool canMerge(Node* consumer, Node* producer) {
// Only fuse within a block
REQ(consumer->owningBlock() == producer->owningBlock());
// Symbolic checks
REQ(canHandle(producer) || producer->kind() == prim::TensorExprGroup);
TORCH_INTERNAL_ASSERT(
consumer->kind() == prim::TensorExprGroup || canHandle(consumer));
// nvrtc has a limit on the number of arguments allowed in a CUDA kernel.
// The specific limit is a function of constant memory size, amount
// available to pass arguments, and some implementation dependence. Select a
// safe limit here.
constexpr size_t subgraphArgLimit = 128;
auto const nInputs = consumer->inputs().size() +
consumer->outputs().size() + producer->inputs().size() +
producer->outputs().size();
REQ(nInputs <= subgraphArgLimit);
// Device checks
if (consumer->kind() != aten::cat && producer->kind() != aten::cat) {
// aten::cat needs a special handling because it takes a Tensor[] as its
// input We deal with that in the code below.
auto consumer_device = tensorexpr::pickDeviceType(consumer->inputs());
REQ(consumer_device);
auto producer_device = tensorexpr::pickDeviceType(producer->inputs());
REQ(producer_device);
REQ(*consumer_device == *producer_device);
}
// Alias checks
REQ(aliasDb_->couldMoveBeforeTopologically(producer, consumer));
// Ops that return aliases can only be folded if this is the only use.
if (producer->kind() == aten::slice ||
producer->kind() == aten::unsqueeze ||
producer->kind() == prim::ConstantChunk) {
for (auto& use : producer->output(0)->uses()) {
REQ(use.user == consumer);
}
}
if (!consumer->hasAttribute(attr::Subgraph) &&
consumer->kind() != prim::TensorExprGroup) {
// Don't initiate a fusion group from prim::ListConstruct
REQ(consumer->kind() != prim::ListConstruct);
REQ(consumer->kind() != aten::slice);
REQ(consumer->kind() != aten::unsqueeze);
REQ(consumer->kind() != prim::ConstantChunk);
// Don't initiate a fusion group just for a constant operand
REQ(producer->kind() != prim::Constant);
}
if (producer->kind() == aten::cat) {
REQ(producer->input(0)->node()->kind() == prim::ListConstruct);
REQ(producer->input(0)->uses().size() == 1);
REQ(producer->input(1)->node()->kind() == prim::Constant);
auto const& listConstruct = producer->input(0)->node();
// We're merging listconstruct->cat->consumer. cat is the producer here
// and we cannot determine its device type - we should use device of the
// listconstruct instead
auto listconstruct_device =
tensorexpr::pickDeviceType(listConstruct->inputs());
auto consumer_device = tensorexpr::pickDeviceType(consumer->inputs());
REQ(listconstruct_device);
REQ(consumer_device);
REQ(*listconstruct_device == *consumer_device);
for (auto const& input : listConstruct->inputs()) {
REQ(isFusableOnDevice(input->node()));
}
REQ((nInputs + listConstruct->inputs().size()) <= subgraphArgLimit);
} else if (consumer->kind() == aten::cat) {
REQ(consumer->input(0)->node()->kind() == prim::ListConstruct);
REQ(consumer->input(0)->uses().size() == 1);
REQ(consumer->input(1)->node()->kind() == prim::Constant);
auto const& listConstruct = consumer->input(0)->node();
// We're merging listconstruct->cat. cat is the consumer and listconstruct
// is the producer. cat doesn't have its device type and thus the only
// thing we should check is that listconstruct's device is well defined
// (e.g. all its inputs has the same device).
auto listconstruct_device =
tensorexpr::pickDeviceType(listConstruct->inputs());
REQ(listconstruct_device);
REQ((nInputs + listConstruct->inputs().size()) <= subgraphArgLimit);
} else {
REQ(isFusableOnDevice(producer));
}
return true;
}
#undef REQ
void prepareFusionGroupAndGuardOutputs(Block* block) {
std::vector<Node*> fusion_groups;
for (Node* n : block->nodes()) {
for (Block* b : n->blocks()) {
prepareFusionGroupAndGuardOutputs(b);
}
if (n->kind() == prim::TensorExprGroup) {
fusion_groups.push_back(n);
}
}
for (Node* fusion_group : fusion_groups) {
removeOutputsUsedOnlyInSize(fusion_group);
insertTypeGuard(
fusion_group,
[](const TensorTypePtr& t) { return t; },
prim::TypeCheck);
}
}
void generalizeFusionGroups(Block* block) {
std::vector<Node*> fusion_groups;
for (Node* n : block->nodes()) {
for (Block* b : n->blocks()) {
generalizeFusionGroups(b);
}
if (n->kind() == prim::TensorExprGroup) {
fusion_groups.push_back(n);
}
}
for (Node* fusion_group : fusion_groups) {
removeOutputsUsedOnlyInSize(fusion_group);
VLOG(1) << "GenerateGuard for fusion group: " << *fusion_group;
if (!GenerateGuard(fusion_group, add_composed_op_)) {
VLOG(1) << " Unfusing the fusion group because GenerateGuard failed"
<< std::endl;
SubgraphUtils::unmergeSubgraph(fusion_group);
}
}
}
// This function parses the option provided by the environment variable
// "PYTORCH_TENSOREXPR_DONT_FUSE".
// This variable allows users to disable fusion on a list of specified
// operators that are separated by ':'. e.g.,
// 'PYTORCH_TENSOREXPR_DONT_FUSE="clamp:mul:add"' disables fusion on
// aten::clamp, aten::mul and aten::add.
void parseTENotFuseOption() {
const char* option = std::getenv("PYTORCH_TENSOREXPR_DONT_FUSE");
std::stringstream in_ss;
if (option) {
in_ss << option;
}
std::string line;
while (std::getline(in_ss, line, ':')) {
if (line.size() == 0) {
continue;
}
operators_not_to_fuse.insert(c10::Symbol::aten(line));
}
}
std::shared_ptr<Graph> graph_;
std::unique_ptr<AliasDb> aliasDb_ = nullptr;
std::set<NodeKind> operators_not_to_fuse;
// Minimal size of a fusion group
size_t min_group_size_;
// compose Runtime Type Guard and Kernel in one op
bool add_composed_op_;
// generalize static shapes to dynamic shapes
bool fuse_to_dynamic_shapes_;
};
void FuseTensorExprs(
std::shared_ptr<Graph>& graph,
size_t min_group_size,
bool add_composed_op,
bool fuse_to_dynamic_shapes) {
GRAPH_DUMP("Before TExprFuser: ", graph);
// Temporary change for Block code generation.
if (tensorexpr::getTEGenerateBlockCode()) {
min_group_size = 1;
}
if (add_composed_op) {
TORCH_INTERNAL_ASSERT(
fuse_to_dynamic_shapes, "Fusing static shapes with composed op NYI");
}
// Get rid of dead code so that we don't waste effort fusing it.
EliminateDeadCode(graph);
TensorExprFuser fuser(
graph, min_group_size, add_composed_op, fuse_to_dynamic_shapes);
fuser.run();
EliminateCommonSubexpression(graph);
EliminateDeadCode(graph);
GRAPH_DUMP("After TExprFuser: ", graph);
}
Operation createTensorExprOp(const Node* node) {
bool dynamic_shape_fusion_node =
node->hasAttribute(attr::striding_inputs_desc);
if (!dynamic_shape_fusion_node) {
auto kernel =
std::make_shared<tensorexpr::TensorExprKernel>(node->g(attr::Subgraph));
return [kernel](Stack& stack) {
RECORD_FUNCTION(kernel->getKernelName(), std::vector<c10::IValue>());
kernel->run(stack);
return 0;
};
}
// Handle the case when dynamic shape fusion is enabled.
VLOG(1) << "Compiling a new kernel for " << *node;
std::vector<int64_t> sym_shapes;
if (node->hasAttribute(attr::symbolic_shape_inputs)) {
sym_shapes = node->is(attr::symbolic_shape_inputs);
}
bool allow_stack_outputs = false;
if (node->hasAttribute(attr::allow_stack_outputs)) {
allow_stack_outputs = node->i(attr::allow_stack_outputs) == 1;
}
std::unordered_map<c10::Symbol, tensorexpr::NNCLoweringFunction>
custom_lowerings;
auto subgraph = node->g(attr::Subgraph);
IValue sym_strides = node->ival(attr::striding_inputs_desc);
// Striding Descriptor is serialized on the node as a vector of vector of
// strings, translate back to StrideInput enum
std::vector<std::vector<std::string>> sym_strides_strs =
sym_strides.to<std::vector<std::vector<std::string>>>();
std::vector<std::vector<StrideInput>> striding_inputs;
for (const auto& vec : sym_strides_strs) {
std::vector<StrideInput> input_desc;
input_desc.reserve(vec.size());
for (const std::string& str : vec) {
input_desc.push_back(strideInputFromString(str));
}
striding_inputs.push_back(input_desc);
}
std::unordered_map<const Value*, std::vector<StrideInput>> stride_map;
size_t index = 0;
for (Value* v : subgraph->inputs()) {
if (!v->type()->cast<TensorType>()) {
continue;
}
stride_map[v] = striding_inputs[index];
index++;
}
std::vector<std::string> output_desc =
node->ival(attr::striding_outputs_desc).to<std::vector<std::string>>();
for (size_t i = 0; i < subgraph->outputs().size(); ++i) {
stride_map[subgraph->outputs().at(i)] = {
strideInputFromString(output_desc.at(i))};
}
std::shared_ptr<tensorexpr::TensorExprKernel> kernel =
std::make_shared<tensorexpr::TensorExprKernel>(
subgraph,
custom_lowerings,
sym_shapes,
/*pre_alloc*/ false,
stride_map);
auto num_subgraph_inputs = subgraph->inputs().size();
return [kernel, num_subgraph_inputs, allow_stack_outputs](Stack& stack) {
RECORD_FUNCTION(kernel->getKernelName(), std::vector<c10::IValue>());
// Stack contents:
// [<outputs>] <inputs>
//
// If the number of graph inputs is same as the stack size, then no
// outputs are being passed in. Otherwise, output tensors are passed in
// at the bottom of the stack. So, we call the appropriate run function
// in TensorExprKernel.
if (num_subgraph_inputs == stack.size() || !allow_stack_outputs) {
kernel->run(stack);
} else {
kernel->runWithAllocatedOutputs(stack);
}
return 0;
};
}
RegisterOperators TensorExprOps({
torch::jit::Operator(
prim::TensorExprGroup,
createTensorExprOp,
AliasAnalysisKind::INTERNAL_SPECIAL_CASE),
});
} // namespace jit
} // namespace torch
|