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 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
|
#include <torch/csrc/jit/serialization/python_print.h>
#include <algorithm>
#include <ATen/core/ivalue.h>
#include <ATen/core/qualified_name.h>
#include <c10/util/Exception.h>
#include <c10/util/StringUtil.h>
#include <c10/util/irange.h>
#include <caffe2/serialize/versions.h>
#include <torch/csrc/jit/api/function_impl.h>
#include <torch/csrc/jit/api/module.h>
#include <torch/csrc/jit/frontend/error_report.h>
#include <torch/csrc/jit/frontend/versioned_symbols.h>
#include <torch/csrc/jit/ir/attributes.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/ir/ir_views.h>
#include <torch/csrc/jit/operator_upgraders/version_map.h>
#include <torch/csrc/jit/resource_guard.h>
#include <torch/csrc/jit/runtime/calculate_necessary_args.h>
#include <torch/csrc/jit/serialization/type_name_uniquer.h>
using c10::QualifiedName;
namespace torch {
namespace jit {
static bool isValidIdentifierChar(char c, size_t pos) {
return islower(c) || isupper(c) || c == '_' || (pos > 0 && isdigit(c));
}
static bool isValidIdentifier(const std::string& name) {
if (name.size() == 0)
return false;
for (const auto i : c10::irange(name.size())) {
if (!isValidIdentifierChar(name[i], i))
return false;
}
return true;
}
// some names are valid identifiers but off limits because
// they are keywords or namespaces used in the output
const static std::unordered_set<std::string> reserved_names = {
// identifiers in the environment while parsing
"_", // avoid the confusing unnamed _
"as",
"aten",
"attribute",
"CONSTANTS",
"fork",
"getattr",
"inf",
"nan",
"infj",
"nanj",
"ops",
"__torch__",
// the python keywords
"and",
"as",
"assert",
"async",
"await",
"break",
"class",
"continue",
"def",
"del",
"elif",
"else",
"except",
"False",
"finally",
"for",
"from",
"global",
"if",
"import",
"in",
"is",
"lambda",
"None",
"nonlocal",
"not",
"or",
"pass",
"raise",
"return",
"True",
"try",
"with",
"while",
"with",
"yield",
"uninitialized",
"unchecked_cast",
};
// Helper to avoid duplicating class types
void PrintDepsTable::add(const c10::NamedTypePtr& type) {
// Despite doing the linear search below, we don't want to do
// wasteful work and only try to insert each instance once.
if (!non_unique_.insert(type).second) {
return;
}
// Need to do actual equality comparison, not a pointer equality. This is
// because for some types (e.g. FunctionType), we may have multiple
// TypePtr's that represent the same underlying thing.
// TODO: this should be really swapped for something more efficient
auto it = std::find_if(
table_.cbegin(), table_.cend(), [&](const c10::NamedTypePtr& dep) {
return *dep == *type;
});
if (it == table_.cend()) {
table_.push_back(type);
}
}
struct PythonPrintImpl {
using SourceRangeStack = std::vector<SourceRange>;
SourceRangeStack source_range_stack_ = {SourceRange()};
struct WithSourceRange {
explicit WithSourceRange(SourceRangeStack* stack, Node* n) : stack(stack) {
TORCH_INTERNAL_ASSERT(stack);
if (auto gen_source = n->sourceRange().findSourceRangeThatGenerated()) {
stack->push_back(std::move(gen_source.value()));
} else {
stack->push_back(n->sourceRange());
}
}
~WithSourceRange() {
stack->pop_back();
}
SourceRangeStack* stack;
};
class TaggedStringStream {
public:
TaggedStringStream(const SourceRangeStack* srs) : srs_(srs) {}
TaggedStringStream& operator<<(const std::string& s) {
// This prevents having redundant entries at the same offset,
// which can happen for example in printValueList when begin
// and end are the empty string.
if (s.size() == 0) {
return *this;
}
if (!ranges_.size() || ranges_.back().range != srs_->back()) {
ranges_.emplace_back((size_t)oss_.tellp(), srs_->back());
}
oss_ << s;
return *this;
}
TaggedStringStream& operator<<(const TaggedStringStream& rhs) {
for (const auto& range : rhs.ranges_) {
if (!ranges_.size() || ranges_.back().range != range.range) {
ranges_.emplace_back((size_t)oss_.tellp() + range.bytes, range.range);
}
}
oss_ << rhs.oss_.str();
return *this;
}
// This overload is here to prevent people from shooting themselves in the
// foot. I would be highly surprised if someone actually wanted to write out
// the address of a TaggedStringStream in the pretty print.
TaggedStringStream& operator<<(
const std::shared_ptr<TaggedStringStream>& rhs) {
(*this) << *rhs;
return *this;
}
template <typename T>
TaggedStringStream& operator<<(const T& t) {
if (!ranges_.size() || ranges_.back().range != srs_->back()) {
ranges_.emplace_back((size_t)oss_.tellp(), srs_->back());
}
oss_ << t;
return *this;
}
std::string str() const {
return oss_.str();
}
const std::vector<TaggedRange>& ranges() const {
return ranges_;
}
private:
std::ostringstream oss_;
std::vector<TaggedRange> ranges_;
const SourceRangeStack* srs_;
};
// scanValue, scanNode, scanBlock:
// decide if it is safe to omit the output of a temporary variable,
// and inline the expression into its use
// we only do this if
// (1) it is a constant, or
// (2) the temporary is unnamed, is single output, is used once,
// and would appear in the same order when the expression tree is
// reparsed.
// The last case can be checked
// because when we emit a expresion tree in the parser,
// we do a left-to-right postorder traversal of the expression tree (emit
// children, then emit op). The reverse of this is a right-to-left preorder
// traversal of the tree. By doing a right-to-left preorder traversal of the
// inputs of a node, while also scanning the list of emitted nodes backward,
// we can see if they line up with what would happen when parsed the node as
// an expression. While they line up we collapse them into an inline
// expression.
// The inductive step is that the right-most input should be produced by the
// node immediatly before the current node if it is in tree order.
bool canInline(Value* v) {
Node* n = v->node();
// there must be only 1 values, otherwise we need an assignment to handle
// the multiple outout values
if (n->outputs().size() != 1)
return false;
// if it is used more than once, then we need a variable
if (v->uses().size() != 1)
return false;
auto use = v->uses().at(0);
// if it has a name set, then it was written as a variable so preserve that
// unless it is being fed directly to the end of the block.
// in which case it is not as useful to give it a name just to return it
if (v->hasDebugName() && use.user->kind() != prim::Return)
return false;
// don't try to inline control blocks
if (n->blocks().size() != 0)
return false;
// if it is a loop-carried input, we need a variable
// otherwise the condition or trip count may be emitted in the wrong order
// w.r.t. to it
if (use.user->kind() == prim::Loop && use.offset >= 2)
return false;
// subgraph may use this more than once, so disable inlining
if (use.user->kind() == prim::fork || use.user->kind() == prim::rpc_async ||
use.user->kind() == prim::rpc_sync ||
use.user->kind() == prim::rpc_remote)
return false;
// isinstance appearing in an if expression
// causes type refinement to occur, but we have
// already handled the refinement and inserted cast
// expressions. By not inlining it into the if condition,
// we prevent it from happening again.
if (v->node()->kind() == prim::isinstance) {
return false;
}
return true;
}
// block_point is the current node in the reverse linear scan of the emitted
// nodes v is the current value in the tree traversal that may match with
// block_point's output.
Node* scanValue(Node* block_point, Value* v) {
Node* n = v->node();
AT_ASSERT(n->kind() == prim::Constant || output_inline_.count(n) == 0);
if (n == block_point &&
canInline(v)) { // the node must be at the expected point of the typical
// tree traversal
// recursively see if we can inline the inputs to this input
block_point = scanNode(block_point);
output_inline_.insert(n);
} else if (n->kind() == prim::Constant) {
// constant nodes can always be inlined, we will de-dup them on parsing
// and put them at the top of the function regardless
output_inline_.insert(n);
}
return block_point;
}
Node* previousNonConstant(Node* n) {
do {
n = n->prev();
} while (n->kind() == prim::Constant);
return n;
}
Node* scanNode(Node* n) {
// don't bother to scan nodes we have already determined to be inline
if (output_inline_.count(n)) {
return n;
}
for (auto b : n->blocks()) {
scanBlock(b);
}
Node* block_point = previousNonConstant(n);
for (auto it = n->inputs().rbegin(), end = n->inputs().rend(); it != end;
++it) {
block_point = scanValue(block_point, *it);
}
return block_point;
}
void scanBlock(Block* b) {
scanNode(b->return_node());
for (auto node : b->nodes().reverse()) {
scanNode(node);
}
}
size_t getOrAddConstant(at::IValue val) {
// XXX - N^2 warning. This code does the exact same thing as
// ConstantPool, which is also N^2 in the size of the constants,
// because it doesn't hash any information about the tensors.
// We will probably need to optimize this at some point using hashing.
if (val.isTensor()) {
auto& t = val.toTensor();
for (const auto i : c10::irange(constant_table_.size())) {
if (!constant_table_[i].isTensor()) {
continue;
}
auto& t2 = constant_table_[i].toTensor();
if (t.options().type_equal(t2.options()) && t.equal(t2)) {
return i;
}
}
}
constant_table_.emplace_back(std::move(val));
return constant_table_.size() - 1;
}
std::unordered_set<Node*> seen_constants;
void buildConstantList(Node* n, std::vector<Node*>& constants) {
for (auto input : n->inputs()) {
if (input->node()->kind() == prim::Constant &&
seen_constants.count(input->node()) == 0) {
constants.push_back(input->node());
seen_constants.insert(input->node());
}
}
for (auto b : n->blocks()) {
buildConstantList(b, constants);
}
}
void buildConstantList(Block* b, std::vector<Node*>& constants) {
for (auto n : b->nodes())
buildConstantList(n, constants);
buildConstantList(b->return_node(), constants);
}
// get a new name unique across calls to debugName() and
// anything we have used.
std::unordered_map<std::string, size_t> next_id;
std::string genNameImpl(
const std::string& candidate,
std::unordered_set<std::string>& used) {
std::string name = candidate;
while (used.count(name) || reserved_names.count(name)) {
// NOLINTNEXTLINE(performance-inefficient-string-concatenation)
name = candidate + c10::to_string(next_id[name]++);
}
used.insert(name);
return name;
}
std::string genName(const std::string& candidate) {
return genNameImpl(candidate, used_names_);
}
// unique names might not be valid identifiers,
// force them to be by rewriting them
static std::string makeValidIdentifier(const std::string& candidate) {
std::stringstream ss;
if (candidate.size() == 0 || isdigit(candidate[0]))
ss << "_";
for (char c : candidate) {
if (isupper(c) || islower(c) || isdigit(c) || c == '_')
ss << c;
else
ss << '_';
}
return ss.str();
}
// if we have to assign 'v' a name, what should it be?
// use the debugName if it was set, otherwise generate a name.
std::string genUniqueNameFor(Value* v) {
return genName(
v->hasDebugName() ? makeValidIdentifier(v->debugNameBase()) : "_");
}
// map from Value to how it should be printed at each use
std::unordered_map<Value*, std::shared_ptr<TaggedStringStream>> expr_table_;
std::unordered_map<Value*, std::string> ident_refs_;
// NB: we MUST pass around the shared pointers to these streams by value.
// There is an interaction in splitLongInlines where the string value for
// both the RHS and the LHS of an expression are live at the same time,
// however the value for the RHS is overwritten in the table.
std::shared_ptr<TaggedStringStream> useOf(Value* v) const {
// Ident refs take precedent over expression refs, since presence in
// the ident ref table indicates we have already emitted a statement
// assigning the given value.
if (ident_refs_.count(v)) {
auto rv = std::make_shared<TaggedStringStream>(&source_range_stack_);
(*rv) << ident_refs_.at(v);
return rv;
}
if (expr_table_.count(v)) {
return expr_table_.at(v);
}
TORCH_INTERNAL_ASSERT(
false,
"Value (debug name: \"",
v->debugName(),
"\") was not present in either expressions table or ident refs table");
}
void assignValue(Value* v, const std::string& s) {
ident_refs_[v] = s;
}
void assignValue(Value* v, std::shared_ptr<TaggedStringStream> s) {
expr_table_[v] = std::move(s);
}
void assignValue(Value* v, Value* w) {
assignValue(v, useOf(w));
}
void assignValuesToTheirUniqueNames(at::ArrayRef<Value*> values) {
for (auto v : values) {
assignValue(v, genUniqueNameFor(v));
}
}
size_t level = 0;
// indent to the current indent level
TaggedStringStream& indent() {
for (const auto i : c10::irange(level)) {
(void)i; // Suppress unused variable warning
body_ << " ";
}
return body_;
}
ResourceGuard WithIndented() {
level++;
return ResourceGuard([this] { level--; });
}
template <class T0, class T1, class F>
void zipWith(at::ArrayRef<T0> list_a, at::ArrayRef<T1> list_b, F action)
const {
auto it_a = list_a.begin();
auto it_b = list_b.begin();
if (list_a.size() != list_b.size()) {
AT_ERROR("Python printer expected 2 lists of same size");
}
for (; it_a != list_a.end(); ++it_a, ++it_b) {
action(*it_a, *it_b);
}
}
void printValueList(
TaggedStringStream& stmt,
at::ArrayRef<Value*> list,
const char* begin = "",
const char* end = "") {
stmt << begin;
auto delimiter = "";
for (auto* value : list) {
stmt << delimiter;
stmt << useOf(value);
delimiter = ", ";
}
stmt << end;
}
void printValueIndex(TaggedStringStream& stmt, at::ArrayRef<Value*> inputs) {
const std::string val_name = useOf(inputs[0])->str();
if (isValidIdentifier(val_name)) {
stmt << val_name;
} else {
stmt << "(" << val_name << ")";
}
stmt << "[";
stmt << useOf(inputs[1]);
stmt << "]";
}
void printDict(
TaggedStringStream& stmt,
at::ArrayRef<Value*> key_value_pairs,
const char* begin = "{",
const char* end = "}") {
stmt << begin;
auto delimiter = "";
for (size_t i = 0; i < key_value_pairs.size(); i += 2) {
stmt << delimiter;
auto key = key_value_pairs[i];
auto value = key_value_pairs[i + 1];
stmt << useOf(key) << ": " << useOf(value);
delimiter = ", ";
}
stmt << end;
}
void printAssignment(at::ArrayRef<Value*> lhs, at::ArrayRef<Value*> rhs) {
if (lhs.size() == 0) {
return;
}
indent();
printValueList(body_, lhs);
// We need to preserve Union/Optional type annotations, but only if
// we're not assigning values as part of a tuple unpacking statement
// (Python doesn't allow type annotations in multiple assignment)
if (lhs.size() == 1) {
Value* v = lhs.at(0);
if (!annotated_unions_.count(v) && !expr_table_.count(v) &&
(v->type()->kind() == UnionType::Kind ||
v->type()->kind() == OptionalType::Kind)) {
body_ << " : " << v->type()->annotation_str();
annotated_unions_.insert(v);
}
}
body_ << " = ";
// or if value is being assigned to something of a union type
printValueList(body_, rhs);
body_ << "\n";
}
bool requiresAnnotation(Value* lhs, Value* rhs) {
if (lhs->type()->kind() == UnionType::Kind ||
lhs->type()->kind() == OptionalType::Kind) {
return annotated_unions_.insert(lhs).second;
} else {
return *lhs->type() != *rhs->type();
}
}
void printAnnotatedAssignment(
at::ArrayRef<Value*> lhs,
at::ArrayRef<Value*> rhs) {
for (const auto i : c10::irange(lhs.size())) {
indent();
body_ << useOf(lhs[i]);
if (requiresAnnotation(lhs[i], rhs[i])) {
body_ << ": " << lhs[i]->type()->annotation_str(type_printer_);
}
body_ << " = " << useOf(rhs[i]) << "\n";
}
}
void printIf(IfView stmt) {
assignValuesToTheirUniqueNames(stmt.outputs());
indent() << "if " << useOf(stmt.cond()) << ":\n";
{
auto guard = WithIndented();
// Print node contents
printBlock(stmt.thenBlock(), stmt.outputs().size() > 0);
printAssignment(stmt.outputs(), stmt.thenOutputs());
}
indent() << "else:\n";
{
auto guard = WithIndented();
printBlock(stmt.elseBlock(), stmt.outputs().size() > 0);
printAssignment(stmt.outputs(), stmt.elseOutputs());
}
}
void printLoop(LoopView stmt) {
// Loop carried dependencies are handled by assigning their initial
// values to the node->outputs() before the loop,
// and assign node->outputs() to the new values at the end of each trip.
auto loop_type = stmt.loopType();
if (loop_type == LoopView::ModifiedLoop) {
throw ErrorReport(stmt.node()->sourceRange())
<< "loop cannot be printed as python "
<< "because it has gone through an optimization "
<< "that combined while and for loops. File a bug";
}
bool emit_as_for_loop = loop_type == LoopView::For;
assignValuesToTheirUniqueNames(stmt.carriedOutputs());
// Add aliases for loop-carried dependencies
zipWith(
stmt.bodyCarriedInputs(), // Start at 1 to ignore trip count
stmt.carriedOutputs(),
[&](Value* block_input, Value* node_output) {
assignValue(block_input, node_output);
});
// Print initial assignments of loop node outputs = loop node inputs
printAnnotatedAssignment(stmt.carriedOutputs(), stmt.carriedInputs());
assignValuesToTheirUniqueNames(stmt.currentTripCount());
// Loop header
if (emit_as_for_loop) {
indent();
body_ << "for " << useOf(stmt.currentTripCount()) << " in range("
<< useOf(stmt.maxTripCount()) << "):\n";
} else {
// note: trip_count_in_block is unused because this is a while loop,
// so we reuse the Value* as a stand-in for the loop condition
printAssignment(stmt.currentTripCount(), stmt.inputCond());
indent();
body_ << "while " << useOf(stmt.currentTripCount()) << ":\n";
}
// Loop body
{
ResourceGuard indent = WithIndented();
// Update block outputs to block inputs for next loop iteration
// skip the assignment to the new condition in for loops because
// the condition is always True
size_t offset = emit_as_for_loop ? 1 : 0;
auto body_block = stmt.bodyBlock();
ArrayRef<Value*> loop_carried_block_inputs =
body_block->inputs().slice(offset);
printBlock(body_block, loop_carried_block_inputs.size() > 0);
printAssignment(
loop_carried_block_inputs, body_block->outputs().slice(offset));
}
}
bool isLongLine(const std::string& str) {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
return str.size() + level * 2 >= 40;
}
bool isLongInline(Node* node) {
return output_inline_.count(node) &&
isLongLine(useOf(node->output())->str());
}
bool isNonConstantInline(Value* input) {
return input->node()->kind() != prim::Constant &&
output_inline_.count(input->node());
}
// [reordering of inlines]
// We inline anything that is semantically legal to inline, but sometimes
// we find that these lines get too long. In that case we break the lines
/// and it is important that we un-inline all the inputs preceeding the long
/// input:
// r = foo(x.add_(b), some_long + expression)
// wrong!
// _0 = some_long + expression
// r = foo(x.add_(b), _0) # wrong! _0 runs before mutating add_
// legal!
// _0 = x.add_(b)
// _1 = some_long + expression
// r = foo(_0, _1)
void splitLongInlines(Value* v) {
std::vector<Value*> to_split_reversed;
Use u = v->uses().at(0);
scanLongInlines(u.user, u.offset, to_split_reversed);
for (auto it = to_split_reversed.rbegin(), end = to_split_reversed.rend();
it != end;
++it) {
printOutputDefinition((*it)->node(), *useOf(*it));
}
}
void scanLongInlines(
Node* user,
int64_t offset,
std::vector<Value*>& to_split_reversed) {
auto it = visited_split_inline_uses_.find(user);
bool present = it != visited_split_inline_uses_.end();
for (int64_t i = offset; i >= (present ? it->second + 1 : 0); --i) {
Value* prev_arg = user->input(i);
if (isNonConstantInline(prev_arg)) {
to_split_reversed.push_back(prev_arg);
}
}
visited_split_inline_uses_[user] = offset;
if (!present && output_inline_.count(user)) {
Use u = user->output()->uses().at(0);
scanLongInlines(u.user, int64_t(u.offset) - 1, to_split_reversed);
// -1 because the actual use is still being
// emitted so it cannot be split
}
}
template <typename T>
void printOutputDefinition(Node* node, const T& expr) {
assignValuesToTheirUniqueNames(node->outputs());
indent();
// Print outputs
if (node->outputs().size() > 0) {
printValueList(body_, node->outputs());
body_ << " = ";
}
body_ << expr << "\n";
}
// Recursively check contained types for any class dependencies
void registerClassDependencies(const TypePtr& type) {
if (const auto classType = type->cast<ClassType>()) {
deps_table_.add(classType);
} else if (const auto tupleType = type->cast<TupleType>()) {
if (tupleType->name()) {
deps_table_.add(tupleType);
}
} else if (const auto interfaceType = type->cast<InterfaceType>()) {
deps_table_.add(interfaceType);
} else if (const auto enumType = type->cast<EnumType>()) {
deps_table_.add(enumType);
}
for (const auto& containedType : type->containedTypes()) {
registerClassDependencies(containedType);
}
}
void scanTypeDependencies(Node* node) {
// Check for class dependencies. If this node inputs or outputs a class
// type, we need to add it to our table of dependencies.
for (const auto input : node->inputs()) {
registerClassDependencies(input->type());
}
for (const auto output : node->outputs()) {
registerClassDependencies(output->type());
}
for (const auto& name : node->attributeNames()) {
switch (node->kindOf(name)) {
case AttributeKind::ty:
registerClassDependencies(node->ty(name));
break;
case AttributeKind::tys:
for (const TypePtr& t : node->tys(name)) {
registerClassDependencies(t);
}
break;
default:
// noop
break;
}
}
}
void checkVersion(Node* node) {
if (auto schema = node->maybeSchema()) {
auto schema_name = getFullSchemaName(*schema);
auto version_entry = get_operator_version_map().find(schema_name);
if (version_entry != get_operator_version_map().end()) {
const auto& entry = version_entry->second;
// TODO (tugsuu) move this calculation into a separate step.
uint64_t current_version = entry[entry.size() - 1].bumped_at_version;
uint64_t legacy_version_map_version =
get_min_version_for_kind(node->kind());
// True means we solely calculate based on upgrader version
if (get_version_calculator_flag()) {
min_version_ = std::max(min_version_, current_version);
} else {
if (legacy_version_map_version != 0) {
min_version_ = std::max(min_version_, legacy_version_map_version);
} else {
min_version_ = std::max(min_version_, current_version);
}
}
}
}
}
void printNode(Node* node, bool print_const) {
WithSourceRange guard(&source_range_stack_, node);
scanTypeDependencies(node);
checkVersion(node);
if (!print_const && node->kind() == prim::Constant)
return;
switch (node->kind()) {
case prim::Return:
if (enforce_importable_ && node->inputs().size() != 1) {
throw ErrorReport(node->sourceRange())
<< "Exportable methods must have a single return value. "
<< "Normal use of ScriptMethods should enforce this";
}
if (node->inputs().size() > 0) {
indent();
body_ << "return ";
printValueList(body_, node->inputs());
body_ << "\n";
}
break;
case prim::Loop:
printLoop(LoopView(node));
break;
case prim::If:
printIf(IfView(node));
break;
case prim::TupleUnpack:
case prim::ListUnpack:
assignValuesToTheirUniqueNames(node->outputs());
indent();
// TupleUnpack(unpacked) turns into an assignment op that forces
// the unpack to be inserted when parsed back in:
// a, b, = unpacked
// a, = unpacked # trailing comma forces an unpack to happen
if (node->outputs().size() > 0) {
printValueList(body_, node->outputs(), "", ", = ");
}
body_ << useOf(node->input()) << "\n";
break;
case prim::SetAttr: {
const auto obj = node->inputs().at(0);
const auto newVal = node->inputs().at(1);
const auto type = obj->type()->expect<ClassType>();
const auto& attrname = node->s(attr::name);
indent();
body_ << useOf(obj) << "." << attrname << " = " << useOf(newVal)
<< "\n";
} break;
case prim::fork: {
// the subgraph gets emitted as another function
auto name = genName("__forked_function");
std::shared_ptr<Graph> graph = node->g(attr::Subgraph);
indent();
body_ << "def " << name << "():\n";
for (size_t i = 0; i < node->inputs().size(); ++i) {
assignValue(graph->inputs().at(i), node->inputs().at(i));
}
printBody(graph->block());
std::stringstream ss;
ss << "fork(" << name << ")";
printOutputDefinition(node, ss.str());
} break;
case prim::Enter: {
const auto in = node->inputs().at(0);
const auto out = node->outputs().at(0);
indent();
body_ << "with " << useOf(in);
if (out->uses().size() > 0) {
assignValue(out, genUniqueNameFor(out));
body_ << " as " << useOf(out);
}
body_ << ":\n";
level++;
} break;
case prim::Exit: {
// If the previous node is a prim::Enter, the with block the generated
// this Enter/Exit pair must have been empty.
if (node->prev()->kind() == prim::Enter) {
indent();
body_ << "pass\n";
}
level--;
} break;
case prim::Closure: {
if (enforce_importable_) {
throw ErrorReport(node->sourceRange())
<< "closures are not exportable";
}
assignValuesToTheirUniqueNames(node->outputs());
auto name = useOf(node->output())->str();
std::shared_ptr<Graph> graph = node->g(attr::Subgraph);
indent();
body_ << "def " << name << "(";
assignValuesToTheirUniqueNames(graph->inputs());
for (size_t i = 0; i < graph->inputs().size(); ++i) {
Value* v = graph->inputs().at(i);
if (i > 0) {
body_ << ", ";
}
body_ << useOf(v) << ": " << v->type()->annotation_str(type_printer_);
}
body_ << "):\n";
printBody(graph->block());
} break;
case prim::ModuleContainerIndex: {
const auto container = node->inputs().at(0);
const auto key = node->inputs().at(1);
const auto out = node->outputs().at(0);
assignValuesToTheirUniqueNames(out);
indent();
body_ << useOf(out) << " : " << out->type()->annotation_str() << " = "
<< useOf(container) << "[" << useOf(key) << "]\n";
} break;
default:
auto ss = std::make_shared<TaggedStringStream>(&source_range_stack_);
printRHS(*ss, node);
// we prevent long constants from inlining here.
// it is not safe to do the same thing for non-constants here
// because of [reordering of inlines]
if (output_inline_.count(node) == 0 ||
(node->kind() == prim::Constant && isLongLine(ss->str()))) {
printOutputDefinition(node, *ss);
} else {
// this node is safe to inline, so assign the output value
// to that expression directly
assignValue(node->output(), ss);
if (isLongLine(ss->str())) {
splitLongInlines(node->output());
}
}
}
}
static bool containsNonASCIIString(const IValue& val) {
bool hasNonASCII = false;
auto checkSubvalue = [&hasNonASCII](const IValue& val) {
if (val.isString()) {
const auto maxASCII = 0x7fu;
for (auto c : val.toStringRef()) {
if (c > maxASCII) {
hasNonASCII = true;
return true;
}
}
}
return false;
};
val.visit(checkSubvalue);
return hasNonASCII;
}
void printConstant(TaggedStringStream& stmt, const IValue& v) {
const auto customFormatter = [&](std::ostream& ss, const IValue& v) {
if (v.isTensor() || containsNonASCIIString(v) || v.isObject()) {
TORCH_INTERNAL_ASSERT(!v.type<c10::Type>()->is_module());
ss << "CONSTANTS.c" << getOrAddConstant(v);
return true;
}
auto type = v.type();
if (auto dyn = type->castRaw<c10::DynamicType>()) {
type = dyn->fallback();
}
if (v.isTuple() && type->expectRef<TupleType>().schema()) {
// print the namedtuple constructor and let rest of tuple printing
// continue
ss << type->expectRef<TupleType>().annotation_str(type_printer_);
}
return false;
};
std::stringstream ss;
v.repr(ss, customFormatter);
stmt << ss.str();
}
void printOpName(TaggedStringStream& stmt, Symbol kind) {
// Special overriding ops set that requires serializing differently to
// preserve the original code semantics.
// This will be more properly handled when we have namespace semantics
// for serializing the ops, and it right now hard coded these ops to
// ensure consistency and not breaking BC in the future.
const static std::unordered_map<Symbol, std::string> override_symbols = {
{aten::backward, "torch.autograd.backward"},
{aten::grad, "torch.autograd.grad"},
};
if (override_symbols.find(kind) != override_symbols.end()) {
stmt << override_symbols.at(kind);
} else if (kind.is_aten()) {
// special case aten -> torch because we want to rename
// the aten namespace, but this change will take more time
// doing it here ensures we do not have fix up archives later
stmt << "torch." << kind.toUnqualString();
} else {
stmt << "ops." << kind.ns().toUnqualString() << "."
<< kind.toUnqualString();
}
}
// Prints the RHS value of a Node, e.g. `aten.add(x, y)`
void printRHS(TaggedStringStream& stmt, Node* node) {
switch (node->kind()) {
case prim::PythonOp: {
auto value = static_cast<const PythonOp*>(node);
if (enforce_importable_) {
throw ErrorReport(node->sourceRange())
<< "Could not export Python function call '" << value->name()
<< "'. Remove calls to Python functions before export. "
<< "Did you forget to add @script or @script_method annotation? "
<< "If this is a nn.ModuleList, add it to __constants__";
}
std::stringstream scalars_stream;
stmt << "^" << value->name();
value->writeScalars(scalars_stream);
stmt << scalars_stream.str();
printValueList(stmt, node->inputs(), "(", ")");
} break;
case prim::Uninitialized: {
stmt << "uninitialized("
<< node->output()->type()->annotation_str(type_printer_) << ")";
} break;
case prim::Constant: {
if (node->outputs().size() == 1 &&
node->output()->type()->kind() == TypeKind::FunctionType) {
auto fn = node->output()->type()->expect<FunctionType>();
deps_table_.add(fn);
stmt << fn->annotation_str(type_printer_);
} else if (!node->mustBeNone()) {
IValue v = toIValue(node->output()).value();
printConstant(stmt, v);
} else {
stmt << "None";
}
} break;
case aten::ScalarImplicit:
case aten::FloatImplicit:
case aten::IntImplicit: {
stmt << "annotate("
<< node->output()->type()->annotation_str(type_printer_) << ", "
<< useOf(node->input()) << ")";
} break;
case aten::Int: {
printValueList(stmt, node->inputs(), "int(", ")");
} break;
case aten::Float: {
printValueList(stmt, node->inputs(), "float(", ")");
} break;
case aten::Bool: {
printValueList(stmt, node->inputs(), "bool(", ")");
} break;
case aten::str: {
printValueList(stmt, node->inputs(), "str(", ")");
} break;
case aten::__getitem__: {
printValueIndex(stmt, node->inputs());
} break;
case prim::Print: {
printValueList(stmt, node->inputs(), "print(", ")");
} break;
case aten::sorted: {
printValueList(stmt, node->inputs(), "sorted(", ")");
} break;
case prim::TupleConstruct: {
if (auto qualname =
node->output()->type()->expectRef<TupleType>().name()) {
stmt << node->output()->type()->annotation_str(type_printer_);
}
printValueList(
stmt, node->inputs(), "(", node->inputs().size() == 1 ? ",)" : ")");
} break;
case prim::TupleIndex: {
stmt << "(" << useOf(node->inputs().at(0)) << ")["
<< useOf(node->inputs().at(1)) << "]";
} break;
case prim::TupleSlice: {
stmt << "(" << useOf(node->input()) << ")[" << node->i(attr::beg) << ":"
<< node->i(attr::end) << "]";
} break;
case prim::ListConstruct: {
ListTypePtr list_type = node->output()->type()->expect<ListType>();
TypePtr elem_type = list_type->getElementType();
// Empty lists must be annotated with their type so the compiler knows
// what type is supposed to be inside them
if (node->inputs().size() == 0) {
stmt << "annotate("
<< node->output()->type()->annotation_str(type_printer_)
<< ", [])";
// If we can't infer the type based on what's inside, explicitly
// annotate it to disambiguate.
// This happens for List[Tensor] vs. List[Optional[Tensor]]
} else if (!elementTypeCanBeInferredFromMembers(elem_type)) {
stmt << "annotate("
<< node->output()->type()->annotation_str(type_printer_) << ", ";
printValueList(stmt, node->inputs(), "[", "]");
stmt << ")";
// Otherwise just print a list
} else {
printValueList(stmt, node->inputs(), "[", "]");
}
} break;
case prim::DictConstruct: {
auto dict_type = node->output()->type()->expect<DictType>();
// There are cases where we must annotate the dict with an explicit type
// to help the compiler out:
// - the dict is empty
// - the dict has potentially ambiguous element types
// (e.g. Tensor vs. Optional[Tensor])
if (node->inputs().size() == 0 ||
!elementTypeCanBeInferredFromMembers(dict_type->getKeyType()) ||
!elementTypeCanBeInferredFromMembers(dict_type->getValueType())) {
stmt << "annotate("
<< node->output()->type()->annotation_str(type_printer_) << ", ";
printDict(stmt, node->inputs());
stmt << ")";
// Otherwise just print a dict
} else {
printDict(stmt, node->inputs());
}
} break;
case prim::CreateObject: {
const auto classType = node->output()->type()->expect<ClassType>();
stmt << classType->annotation_str(type_printer_) << ".__new__("
<< classType->annotation_str(type_printer_) << ")";
} break;
case prim::GetAttr: {
const auto obj = node->inputs().at(0);
const auto classType = obj->type()->expect<ClassType>();
const auto& field = node->s(attr::name);
if (isValidIdentifier(field)) {
stmt << useOf(obj) << "." << field;
} else {
stmt << "getattr(" << useOf(obj) << ", ";
std::stringstream field_stream;
c10::printQuotedString(field_stream, field);
stmt << field_stream.str() << ")";
}
} break;
case prim::CallFunction: {
stmt << useOf(node->inputs().at(0)) << "(";
for (size_t i = 1; i < node->inputs().size(); i++) {
stmt << useOf(node->inputs()[i]) << ", ";
}
stmt << ")";
} break;
case prim::CallMethod: {
const auto& self = node->inputs().at(0);
const auto& methodName = node->s(attr::name);
stmt << "(" << useOf(self) << ")"
<< "." << methodName << "(";
for (size_t i = 1; i < node->inputs().size(); i++) {
stmt << useOf(node->inputs()[i]) << ", ";
}
stmt << ")";
if (auto selfClass = self->type()->cast<ClassType>()) {
deps_table_.add(selfClass);
const Function& method = selfClass->getMethod(node->s(attr::name));
TORCH_INTERNAL_ASSERT(
method.qualname() ==
QualifiedName(selfClass->name()->qualifiedName(), methodName));
} else if (auto selfInterface = self->type()->cast<InterfaceType>()) {
deps_table_.add(selfInterface);
} else {
TORCH_INTERNAL_ASSERT(
false, "method call to unhandled type in serialization");
}
} break;
case aten::_unwrap_optional: {
printOpName(stmt, node->kind());
stmt << "(";
// we cannot recover the type of unwrap_optional(None),
// using normal schema matching, so we route around this by rewriting
// the call to unwrap_optional(annotated(Optional[T], None))
if (node->input()->type()->isSubtypeOf(*NoneType::get()) ||
node->input()->mustBeNone()) {
auto input_type = OptionalType::create(node->output()->type());
stmt << "annotate(" << input_type->annotation_str(type_printer_)
<< ", " << useOf(node->input()) << ")";
} else {
stmt << useOf(node->input());
}
stmt << ")";
} break;
// unchecked_unwrap_optional is no longer generated by the compiler,
// but may end up here if it was first loaded from a old model and
// re-saved. On re-save we upgrade it to an unchecked_cast, which is an
// equivalent op
case prim::unchecked_unwrap_optional:
case prim::unchecked_cast: {
stmt << "unchecked_cast("
<< node->output()->type()->annotation_str(type_printer_) << ", "
<< useOf(node->input()) << ")";
} break;
case prim::isinstance: {
stmt << "isinstance(" << useOf(node->input()) << ", ";
const auto& types = node->tys(attr::types);
if (types.size() == 1) {
stmt << types.at(0)->annotation_str(type_printer_);
} else {
// check multiple things, e.g. (str, list, int)
stmt << "(";
bool first = true;
for (const TypePtr& typ : types) {
if (!first) {
stmt << ", ";
}
stmt << typ->annotation_str(type_printer_);
first = false;
}
stmt << ")";
}
stmt << ")";
} break;
case prim::tolist: {
stmt << "annotate("
<< node->output()->type()->annotation_str(type_printer_) << ", ";
stmt << useOf(node->input(0)) << ".tolist()"
<< ")";
} break;
case prim::EnumValue:
// Note: This CAN NOT be printed as raw operator ops.prim.EnumValue
// because its return type depends on type of enum and must be further
// resolved, but ops.prim.EnumValue construction does not provide such
// functionality.
stmt << "(" << useOf(node->input()) << ").value";
break;
case prim::EnumName:
stmt << "(" << useOf(node->input()) << ").name";
break;
default: {
printOpName(stmt, node->kind());
const FunctionSchema& schema = node->schema();
stmt << "(";
// calculate how many args are specified.
// see (https://github.com/pytorch/pytorch/pull/56079) for more
// details.
size_t num_schema_args = schema.arguments().size();
// we only want to do this extra logic only when necessary.
if (num_schema_args > 0) {
// calculate how many args are specified.
// see (https://github.com/pytorch/pytorch/pull/56079) for more
// details.
auto specified_args =
CalculateNecessaryArgs(schema.arguments(), node->inputs(), true);
auto num_necessary = specified_args.first;
auto num_out = specified_args.second;
for (size_t i = 0; i < num_necessary; ++i) {
if (i > 0)
stmt << ", ";
auto v = useOf(node->inputs().at(i));
// print the kwarg name if it is a kwarg only argument.
if (i < num_schema_args) {
auto arg = schema.arguments().at(i);
if (arg.kwarg_only()) {
stmt << arg.name() << "=";
}
} else {
// vararg functions like format can have extra arguments
AT_ASSERT(schema.is_vararg());
}
stmt << *v;
}
// print out args
for (size_t i = num_schema_args - num_out; i < num_schema_args; i++) {
stmt << ", ";
auto arg = schema.arguments().at(i);
TORCH_INTERNAL_ASSERT(arg.is_out());
// figure out the corresponding input at this index
auto input_idx = node->inputs().size() - (num_schema_args - i);
if (input_idx < node->inputs().size()) {
stmt << arg.name() << "=" << *useOf(node->inputs().at(input_idx));
}
}
}
stmt << ")";
} break;
}
}
TaggedStringStream& printBlock(Block* root, bool block_has_other_statements) {
// pythons weird 'pass' syntax creates a bunch of places where we have to
// check if this block would be empty. But not everything in a block is a
// node. Sometimes if, loop, and return statements will follow this block
// and block_has_other_statements == true.
if (!block_has_other_statements &&
root->nodes().begin() == root->nodes().end()) {
indent();
body_ << "pass\n";
}
for (auto* node : root->nodes()) {
printNode(node, /*print_const=*/false);
}
return body_;
}
template <typename dtype>
IValue createBroadList(dtype value, const int64_t& N) {
c10::List<dtype> repeated;
repeated.reserve(N);
for (const auto i : c10::irange(N)) {
(void)i; // Suppress unused variable warning
repeated.push_back(value);
}
return repeated;
}
void printDefaultValue(
const Argument& arg,
TaggedStringStream& stmt,
const IValue& value) {
stmt << "=";
// handle broadcasting lists
if (arg.type()->kind() == ListType::Kind &&
(value.isInt() || value.isDouble() || value.isBool())) {
TORCH_INTERNAL_ASSERT(arg.N(), "expected broadcastinglist");
if (value.isInt()) {
printConstant(stmt, createBroadList<int64_t>(value.toInt(), *arg.N()));
} else if (value.isBool()) {
printConstant(stmt, createBroadList<bool>(value.toBool(), *arg.N()));
} else if (value.isDouble()) {
printConstant(
stmt, createBroadList<double>(value.toDouble(), *arg.N()));
}
} else {
printConstant(stmt, value);
}
}
void printBody(Block* body) {
// we always print constants at the top of the function, in the order
// in which they are used.
std::vector<Node*> constants;
buildConstantList(body, constants);
// current graph is used to de-dup names within a single graph
scanBlock(body);
{
auto guard = WithIndented();
// Print initial constant table (most are just inlined into their use,
// but some like long strings do get emitted)
for (Node* n : constants) {
printNode(n, /*print_const=*/true);
}
// Print body
printBlock(body, body->return_node()->inputs().size() > 0);
printNode(body->return_node(), /*print_const=*/false);
}
}
public:
void printFunction(
const Function& func,
bool print_first_argument_type = true) {
const FunctionSchema& schema = func.getSchema();
Graph& graph = *toGraphFunction(func).graph();
used_names_.clear(); // each graph can reuse local names
WithSourceRange guard(&source_range_stack_, graph.param_node());
indent();
body_ << "def " << func.name() << "(";
auto param_it = graph.inputs().begin();
for (const Argument& arg : schema.arguments()) {
registerClassDependencies(arg.type());
std::string arg_name = genName(arg.name());
if (param_it == graph.inputs().begin()) {
// the first argument may omit its type when it is implied by context
// the flag print_first_argument_type determines when to do this
body_ << arg_name;
if (print_first_argument_type) {
body_ << ": " << arg.type()->annotation_str(type_printer_);
annotated_unions_.insert(*param_it);
}
} else {
body_ << ",\n " << arg_name << ": "
<< arg.type()->annotation_str(type_printer_);
annotated_unions_.insert(*param_it);
}
if (arg.default_value()) {
printDefaultValue(arg, body_, *arg.default_value());
}
assignValue(*param_it++, arg_name);
}
const auto& returnType = schema.returns().at(0).type();
body_ << ") -> " << returnType->annotation_str(type_printer_) << ":\n";
registerClassDependencies(returnType);
printBody(graph.block());
}
void printMethod(const Function& func) {
printFunction(func, /*print_first_argument_type=*/false);
}
PythonPrintImpl(
std::vector<at::IValue>& constant_table,
PrintDepsTable& deps_table,
c10::TypePrinter type_printer,
bool enforce_importable)
: body_(&source_range_stack_),
constant_table_(constant_table),
deps_table_(deps_table),
type_printer_(std::move(type_printer)),
enforce_importable_(enforce_importable) {}
void printClass(const ClassTypePtr& classType) {
// If any of the methods are not Graph funtions, this indicates that
// this class is a custom-bound C++ class. Skip serialization
// of this class, we will depend on the ClassType being defined
// in the target process.
for (auto& method : classType->methods()) {
if (!method->isGraphFunction()) {
return;
}
}
bool is_module = classType->is_module();
body_ << "class " << classType->name()->name();
if (is_module) {
body_ << "(Module)";
}
body_ << ":\n";
{
const auto guard = WithIndented();
size_t numAttrs = classType->numAttributes();
// For modules, we need to print special information about the module's
// attributes and parameters.
if (is_module) {
std::vector<std::string> params;
std::vector<std::string> buffers;
// Populate the __parameters__ field. This tells the importer which
// attributes are parameters.
for (const auto i : c10::irange(numAttrs)) {
if (classType->is_parameter(i)) {
params.push_back(classType->getAttributeName(i));
}
if (classType->is_buffer(i)) {
buffers.push_back(classType->getAttributeName(i));
}
}
indent();
body_ << "__parameters__ = [";
for (const auto& param : params) {
body_ << "\"" << param << "\", ";
}
body_ << "]\n";
indent();
body_ << "__buffers__ = [";
for (const auto& buffer : buffers) {
body_ << "\"" << buffer << "\", ";
}
body_ << "]\n";
auto forwardPreHooks = classType->getForwardPreHooks();
if (forwardPreHooks.size() > 0) {
indent();
body_ << "__forward_pre_hooks__ = [";
for (const auto& pre_hook : forwardPreHooks) {
body_ << "\"" << pre_hook->name() << "\", ";
}
body_ << "]\n";
}
auto forwardHooks = classType->getForwardHooks();
if (forwardHooks.size() > 0) {
indent();
body_ << "__forward_hooks__ = [";
for (const auto& hook : forwardHooks) {
body_ << "\"" << hook->name() << "\", ";
}
body_ << "]\n";
}
}
for (const auto i : c10::irange(numAttrs)) {
const auto& name = classType->getAttributeName(i);
const auto& type = classType->getAttribute(i);
registerClassDependencies(type);
indent();
// Handling for when the attribute name is not a valid Python
// identifier. This happens for, e.g. ModuleList.
if (!isValidIdentifier(name)) {
if (i == 0) {
// Initialize the annotations dict if necessary.
body_ << "__annotations__ = []\n";
indent();
}
// Print out a direct manipulation of the annotations dict, like:
// __annotations__["0"] = SomeType
body_ << "__annotations__["
<< "\"" << name
<< "\"] = " << type->annotation_str(type_printer_) << "\n";
} else {
// Otherwise: just emit a python 3 attribute annotation, like:
// foo : SomeType
body_ << name << " : " << type->annotation_str(type_printer_) << "\n";
}
}
size_t numConstants = classType->numConstants();
for (const auto i : c10::irange(numConstants)) {
const auto& name = classType->getConstantName(i);
IValue v = classType->getConstant(i);
indent();
body_ << name << " : "
<< "Final[" << v.type()->annotation_str(type_printer_) << "] = ";
auto ss = std::make_shared<TaggedStringStream>(&source_range_stack_);
printConstant(*ss, v);
body_ << ss->str() << "\n";
}
// TODO fields
for (auto& method : classType->methods()) {
printFunction(*method);
}
std::set<std::string> already_printed;
for (auto& hook : classType->getForwardHooks()) {
if (already_printed.count(hook->name()) == 0) {
already_printed.insert(hook->name());
printFunction(*hook);
}
}
for (auto& pre_hook : classType->getForwardPreHooks()) {
if (already_printed.count(pre_hook->name()) == 0) {
already_printed.insert(pre_hook->name());
printFunction(*pre_hook);
}
}
}
}
void printNamedType(const c10::NamedTypePtr& type) {
if (auto functionType = type->cast<FunctionType>()) {
printFunction(*functionType->function());
} else if (auto classType = type->cast<ClassType>()) {
printClass(classType);
} else if (auto tupleType = type->cast<TupleType>()) {
TORCH_INTERNAL_ASSERT(tupleType->schema());
body_ << "class " << tupleType->name()->name();
body_ << "(NamedTuple):\n";
{
const auto guard = WithIndented();
for (const auto& attr : tupleType->schema()->arguments()) {
TORCH_INTERNAL_ASSERT(attr.type());
indent();
body_ << attr.name() << " : "
<< attr.type()->annotation_str(type_printer_) << "\n";
}
}
} else if (auto interfaceType = type->cast<InterfaceType>()) {
body_ << "class " << interfaceType->name()->name();
if (interfaceType->is_module()) {
body_ << "(ModuleInterface):\n";
} else {
body_ << "(Interface):\n";
}
{
auto guard = WithIndented();
for (const FunctionSchema& method : interfaceType->methods()) {
indent();
body_ << "def " << method.name() << "(self";
TORCH_INTERNAL_ASSERT(
method.arguments().size() > 0 &&
method.arguments().at(0).name() == "self");
for (const Argument& arg :
at::ArrayRef<Argument>(method.arguments()).slice(1)) {
const auto& arg_type = arg.type();
registerClassDependencies(arg_type);
body_ << ", " << arg.name() << ": "
<< arg_type->annotation_str(type_printer_);
}
auto return_type = method.returns().at(0).type();
registerClassDependencies(return_type);
body_ << ") -> " << return_type->annotation_str(type_printer_)
<< ":\n";
indent();
body_ << " pass\n";
}
}
} else if (auto enumType = type->cast<EnumType>()) {
body_ << "class " << enumType->qualifiedClassName().name() << "(Enum):\n";
std::string value_wrapper = "";
if (enumType->getValueType() == StringType::get()) {
value_wrapper = "\"";
}
{
auto guard = WithIndented();
for (const auto& name_value : enumType->enumNamesValues()) {
indent();
body_ << name_value.first << " = " << value_wrapper
<< name_value.second << value_wrapper << "\n";
}
}
} else {
TORCH_INTERNAL_ASSERT(false, "Unhandled NamedType");
}
}
~PythonPrintImpl() = default;
TaggedStringStream body_;
// When printing this node, is it safe to write it inline (i.e. without
// assigning a temporary variable
std::unordered_set<Node*> output_inline_;
// see [reordering of inlines]
// used to track parts of an inline statement we already scanned
// for splitting long lines, so that we do not revisit them causing n^2
// behavior. stores the maximum offset into inputs that has already been
// scanned for the node.
std::unordered_map<Node*, int64_t> visited_split_inline_uses_;
// what valid identifiers are in use for the current function
std::unordered_set<std::string> used_names_;
// constants are written to this table, and given then named CONSTANTS.cN
// where N is the index into this table.
std::vector<at::IValue>& constant_table_;
// Any NamedTypes (classes, functions, NamedTuples) used are written to this
// table.
PrintDepsTable& deps_table_;
// We need to preserve Union/Optional type annotations, but we should
// only print the annotation on variable declaration (not on any
// following uses). This set tracks the Value*s that we've already
// printed with annotations
std::unordered_set<Value*> annotated_unions_;
// A function that, given a named type, returns us the correct string to print
// for it.
c10::TypePrinter type_printer_;
// when we print this, should we error if the resulting output would
// not be able to be reparsed?
bool enforce_importable_;
// The least version that supports all printed ops
uint64_t min_version_ = caffe2::serialize::kMinSupportedFileFormatVersion;
};
PythonPrint::PythonPrint(
std::vector<at::IValue>& constant_table,
PrintDepsTable& deps_table,
c10::TypePrinter type_printer,
bool enforce_importable)
: pImpl(std::make_shared<PythonPrintImpl>(
constant_table,
deps_table,
std::move(type_printer),
enforce_importable)) {}
void PythonPrint::printNamedType(const c10::NamedTypePtr& type) {
pImpl->printNamedType(type);
}
void PythonPrint::printFunction(const Function& func) {
pImpl->printFunction(func);
}
void PythonPrint::printMethod(const Function& func) {
pImpl->printMethod(func);
}
std::string PythonPrint::str() const {
return pImpl->body_.str();
}
const SourceRangeRecords& PythonPrint::ranges() const {
return pImpl->body_.ranges();
}
uint64_t PythonPrint::minVersion() const {
return pImpl->min_version_;
}
PythonPrint::~PythonPrint() = default;
std::vector<IValue> traverseIValueAndGetObjects(IValue ivalue) {
std::vector<IValue> result;
std::vector<IValue> stack;
stack.emplace_back(ivalue);
while (!stack.empty()) {
IValue head = stack.back();
stack.pop_back();
if (head.isObject()) {
result.push_back(head);
auto obj = head.toObject();
ClassTypePtr type = obj->type();
if (type->hasMethod("__getstate__")) {
Function& getstate = type->getMethod("__getstate__");
stack.emplace_back(getstate({obj}));
} else {
for (size_t i = 0, n = type->numAttributes(); i < n; ++i) {
stack.emplace_back(obj->getSlot(i));
}
}
} else if (ivalue.isGenericDict()) {
for (const auto& kv : ivalue.toGenericDict()) {
// skip key because key cannot be an object
stack.emplace_back(kv.value());
}
} else if (ivalue.isList()) {
for (const auto& v : ivalue.toList()) {
stack.emplace_back(v);
}
} else if (ivalue.isTuple()) {
for (const auto& v : ivalue.toTuple()->elements()) {
stack.emplace_back(v);
}
}
}
return result;
}
c10::optional<std::string> printType(
const c10::Type& type,
torch::jit::TypeNameUniquer& type_name_uniquer) {
if (auto dyn = type.castRaw<c10::DynamicType>()) {
return dyn->fallback()->annotation_str(
[&](auto&& t) { return printType(t, type_name_uniquer); });
}
auto namedType = type.cast<c10::NamedType>();
if (namedType && namedType->name()) {
return type_name_uniquer.getUniqueName(namedType).qualifiedName();
}
return c10::nullopt;
}
void jitModuleToPythonCodeAndConstants(
const Module& module,
ExtraFilesMap* jit_sources, // output
std::vector<IValue>* constants // output
) {
std::vector<IValue> objects = traverseIValueAndGetObjects(module._ivalue());
std::unordered_set<c10::QualifiedName> visited;
PrintDepsTable class_deps;
TypeNameUniquer uniquer;
auto type_printer = [&](const c10::Type& t) { return printType(t, uniquer); };
// Group by prefix; because every prefix is a file.
std::unordered_map<std::string, PythonPrint> grouped_by_prefix;
for (const IValue& obj : objects) {
ObjectPtr obj_ptr = obj.toObject();
ClassTypePtr class_type = obj_ptr->type();
class_deps.add(class_type);
}
for (int i = 0; i < class_deps.size(); ++i) {
auto type = class_deps[i];
auto qualname = uniquer.getUniqueName(type);
std::string qualifier = qualname.prefix();
auto pp_iter = grouped_by_prefix.find(qualifier);
if (pp_iter == grouped_by_prefix.end()) {
pp_iter = grouped_by_prefix
.emplace(
qualifier,
PythonPrint(
*constants,
class_deps,
type_printer,
/*enforce_importable=*/true))
.first;
}
pp_iter->second.printNamedType(type);
}
for (const auto& kv : grouped_by_prefix) {
(*jit_sources)[kv.first] = kv.second.str();
}
}
} // namespace jit
} // namespace torch
|