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 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249
|
#include <torch/csrc/jit/codegen/cuda/arith.h>
#include <c10/util/BFloat16.h>
#include <c10/util/Exception.h>
#include <c10/util/Half.h>
#include <c10/util/irange.h>
#include <torch/csrc/jit/codegen/cuda/ir_all_nodes.h>
#include <torch/csrc/jit/codegen/cuda/ir_builder.h>
#include <torch/csrc/jit/codegen/cuda/ir_iostream.h>
#include <torch/csrc/jit/codegen/cuda/ir_utils.h>
#include <torch/csrc/jit/codegen/cuda/type.h>
#include <torch/csrc/jit/codegen/cuda/type_promotion.h>
#include <cfloat>
namespace torch {
namespace jit {
namespace fuser {
namespace cuda {
namespace {
TensorView* maybe_broadcast_inner_to_rank(TensorView* t, size_t rank) {
size_t t_rank = TensorDomain::noReductions(t->getMaybeRFactorDomain()).size();
// broadcast inner on inp to match rank with other.
if (t_rank < rank) {
const int num_bcast = static_cast<int>(rank - t_rank);
std::vector<bool> inner_bcast_dims(rank, false);
std::fill(
inner_bcast_dims.begin(), inner_bcast_dims.begin() + num_bcast, true);
t = broadcast(t, inner_bcast_dims);
}
return t;
}
Val* simplifiedInt(Val* val) {
TORCH_INTERNAL_ASSERT(
val->isConstInt(), "Expecting Const Int's only in this routine.");
if (val->as<Int>()->value().has_value()) {
return val;
}
return IrBuilder::create<Int>(val->evaluateInt());
}
// If one size is nullptr, return the other. If both symbolic just return v1. If
// one's concrete, prefer that one (simplified). If both concrete make sure
// they're the same size.
Val* promoteSize(Val* v1, Val* v2) {
if (v1 == nullptr) {
TORCH_INTERNAL_ASSERT(
v2 == nullptr || v2->isAnInt(),
"Expecting Int's only in this routine.");
return v2;
}
if (v2 == nullptr) {
return v1;
}
TORCH_INTERNAL_ASSERT(
v1->isAnInt() && v2->isAnInt(), "Expecting Int's only in this routine.");
if (!v1->isConstInt() && !v2->isConstInt()) {
return v1;
} else if (v1->isConstInt() && v2->isConstInt()) {
TORCH_INTERNAL_ASSERT(
v1->evaluateInt() == v2->evaluateInt(),
"Expected sizes of, ",
v1->toString(),
" and ",
v2->toString(),
" to match but found ",
v1->evaluateInt(),
" and ",
v2->evaluateInt(),
".");
return simplifiedInt(v1);
} else if (v1->isConstInt()) {
return simplifiedInt(v1);
}
return simplifiedInt(v2);
}
// Will return a new value of type val with the DataType dtype.
Val* newScalar(ValType vtype, DataType dtype) {
switch (vtype) {
case (ValType::NamedScalar):
case (ValType::Scalar):
switch (dtype) {
case DataType::Bool:
return IrBuilder::create<Bool>();
case DataType::Double:
case DataType::Float:
case DataType::Half:
case DataType::BFloat16:
return IrBuilder::create<Double>();
case DataType::Int32:
case DataType::Int:
return IrBuilder::create<Int>();
case DataType::ComplexFloat:
case DataType::ComplexDouble:
return IrBuilder::create<ComplexDouble>();
default:
break;
}
default:
break;
}
TORCH_CHECK(
false,
"Cannot handle ValType: ",
vtype,
" with DataType:",
dtype,
" in newScalar.");
}
IterType promoteIterType(IterType type1, IterType type2) {
// Iteration: Default
// Reduction: Should not appear here
// Broadcast: Propagated only if type1 and type2 are Broadcast
// Gather: Converted to Iteration
// Stride: Shold not appear here
// VectorComponent: Converted to Iteration
TORCH_INTERNAL_ASSERT(
type1 != IterType::Reduction && type1 != IterType::Stride,
"Invalid IterType: ",
type1)
TORCH_INTERNAL_ASSERT(
type2 != IterType::Reduction && type2 != IterType::Stride,
"Invalid IterType: ",
type2);
// Do not propagate Gather and VectorComponent
if (type1 == IterType::Gather || type1 == IterType::VectorComponent) {
type1 = IterType::Iteration;
}
if (type2 == IterType::Gather || type2 == IterType::VectorComponent) {
type2 = IterType::Iteration;
}
// At this point, type1 and type2 must be either Iteration or
// Broadcast
TORCH_INTERNAL_ASSERT(
type1 == IterType::Iteration || type1 == IterType::Broadcast,
"Unexpected IterType: ",
type1);
TORCH_INTERNAL_ASSERT(
type2 == IterType::Iteration || type2 == IterType::Broadcast,
"Unexpected IterType: ",
type2);
if (type1 == IterType::Broadcast) {
return type2;
} else {
return type1;
}
}
TensorView* newOutputTV(const std::vector<Val*>& vals, DataType dtype) {
std::vector<TensorView*> tvs;
for (auto val : vals) {
if (val->getValType() == ValType::TensorView) {
tvs.push_back(val->as<TensorView>());
}
}
TORCH_CHECK(
!tvs.empty(),
"Tried to create new output TensorView but received empty list.");
std::vector<IterDomain*> out_domain(
TensorDomain::noReductions(tvs[0]->getMaybeRFactorDomain()).size(),
nullptr);
// For the start and stop offsets, take the maximum of input axes.
// For now, the offsets of both start and stop are always integer
// constant, so we can statically compute them. It is unclear
// whether we would need to support dynamic offsetting, e.g.,
// shifting by a dynamic offset.
std::vector<int64_t> start_offsets(out_domain.size(), 0);
std::vector<int64_t> stop_offsets(out_domain.size(), 0);
std::vector<Val*> extent_vals(out_domain.size(), nullptr);
std::vector<Val*> expanded_extent_vals(out_domain.size(), nullptr);
std::vector<c10::optional<IterType>> iter_types(
out_domain.size(), c10::nullopt);
for (auto tv : tvs) {
auto dom = TensorDomain::noReductions(tv->getMaybeRFactorDomain());
TORCH_INTERNAL_ASSERT(
dom.size() == out_domain.size(),
"Invalid tensor view found while producing an output, it has ",
dom.size(),
" dimensions but expected ",
out_domain.size());
for (const auto i : c10::irange(dom.size())) {
if (dom[i]->isBroadcast()) {
if (dom[i]->hasExpandedExtent()) {
expanded_extent_vals[i] =
promoteSize(expanded_extent_vals[i], dom[i]->expandedExtent());
}
continue;
}
extent_vals[i] = promoteSize(extent_vals[i], dom[i]->extent());
if (iter_types[i].has_value()) {
iter_types[i] =
promoteIterType(iter_types[i].value(), dom[i]->getIterType());
} else {
iter_types[i] = dom[i]->getIterType();
}
auto start_offset = dom[i]->start()->as<Int>();
auto stop_offset = dom[i]->stopOffset()->as<Int>();
// Currently, start is always constant
TORCH_INTERNAL_ASSERT(
start_offset->isConstInt(),
"Invalid IterDomain start: ",
start_offset);
TORCH_INTERNAL_ASSERT(
stop_offset->isConstInt(),
"Invalid IterDomain stop offset: ",
stop_offset);
start_offsets[i] =
std::max(start_offsets[i], start_offset->evaluateInt());
stop_offsets[i] = std::max(stop_offsets[i], stop_offset->evaluateInt());
}
}
for (const auto dim_i : c10::irange(out_domain.size())) {
if (extent_vals[dim_i] != nullptr) {
TORCH_INTERNAL_ASSERT(
iter_types[dim_i].has_value(),
"Could not deduce iter type for new tensor view.");
out_domain[dim_i] =
IterDomainBuilder(
IrBuilder::create<Int>(start_offsets[dim_i]), extent_vals[dim_i])
.stop_offset(IrBuilder::create<Int>(stop_offsets[dim_i]))
.iter_type(iter_types[dim_i].value())
.build();
} else {
out_domain[dim_i] = IterDomainBuilder(
FusionGuard::getCurFusion()->zeroVal(),
FusionGuard::getCurFusion()->oneVal())
.expanded_extent(expanded_extent_vals[dim_i])
.iter_type(IterType::Broadcast)
.build();
}
}
return IrBuilder::create<TensorView>(
IrBuilder::create<TensorDomain>(
out_domain, std::vector<bool>(out_domain.size(), true)),
dtype);
}
std::vector<Val*> maybeBroadcast(const std::vector<Val*>& vals) {
std::vector<Val*> out_vals(vals.size(), nullptr);
size_t n_dims = 0;
for (auto val : vals) {
if (val->getValType().value() == ValType::TensorView) {
n_dims = std::max(
n_dims,
TensorDomain::noReductions(
val->as<TensorView>()->getMaybeRFactorDomain())
.size());
}
}
for (const auto i : c10::irange(vals.size())) {
if (vals[i]->getValType().value() == ValType::TensorView) {
auto tv = vals[i]->as<TensorView>();
out_vals[i] = maybe_broadcast_inner_to_rank(tv, n_dims);
} else {
out_vals[i] = vals[i];
}
}
return out_vals;
}
Val* newValLike(Val* val, DataType dtype) {
TORCH_CHECK(
dtype != DataType::Null, "Invalid datatype provided for new value.");
const ValType vtype = val->getValType().value();
if (vtype == ValType::TensorView)
return newOutputTV({val}, dtype);
return newScalar(vtype, dtype);
}
// returns the minimum init value for reduction:
// -inf for floating type;
// lowest value for integer type;
// false for bool.
Val* getMinimumValue(DataType v) {
switch (v) {
case (DataType::Double):
return IrBuilder::create<Double>(
-std::numeric_limits<double>::infinity());
break;
case (DataType::Float):
return IrBuilder::create<Double>(-std::numeric_limits<float>::infinity());
break;
case (DataType::Half):
return IrBuilder::create<Double>(
static_cast<double>(-std::numeric_limits<c10::Half>::infinity()));
break;
case DataType::BFloat16:
return IrBuilder::create<Double>(
static_cast<double>(-std::numeric_limits<c10::BFloat16>::infinity()));
break;
case (DataType::Int):
return IrBuilder::create<Int>(std::numeric_limits<int64_t>::lowest());
break;
case (DataType::Int32):
return IrBuilder::create<Int>(std::numeric_limits<int32_t>::lowest());
break;
case (DataType::Bool):
return IrBuilder::create<Bool>(false);
break;
default:
TORCH_CHECK(
false, "Could not generate a min op for tensor with type: ", v);
}
return nullptr;
}
// returns the maximum init value for reduction:
// inf for floating type;
// highest value for integer type;
// true for bool.
Val* getMaximumValue(DataType v) {
switch (v) {
case (DataType::Double):
return IrBuilder::create<Double>(std::numeric_limits<double>::infinity());
break;
case (DataType::Float):
return IrBuilder::create<Double>(std::numeric_limits<float>::infinity());
break;
case (DataType::Half):
return IrBuilder::create<Double>(
static_cast<double>(std::numeric_limits<c10::Half>::infinity()));
break;
case DataType::BFloat16:
return IrBuilder::create<Double>(
static_cast<double>(std::numeric_limits<c10::BFloat16>::infinity()));
break;
case (DataType::Int):
return IrBuilder::create<Int>(std::numeric_limits<int64_t>::max());
break;
case (DataType::Int32):
return IrBuilder::create<Int>(std::numeric_limits<int32_t>::max());
break;
case (DataType::Bool):
return IrBuilder::create<Bool>(true);
break;
default:
TORCH_CHECK(
false, "Could not generate a max op for tensor with type: ", v);
}
return nullptr;
}
} // namespace
Val* castOp(DataType dtype, Val* v1) {
if (v1->getDataType().value() == dtype) {
return set(v1);
}
if (cast_func_str(std::make_pair(v1->getDataType().value(), dtype)) ==
c10::nullopt) {
TORCH_CHECK(
false,
"Illegal Cast value from DataType: ",
v1->getDataType().value(),
" to DataType: ",
dtype);
}
Val* out = newValLike(v1, dtype);
IrBuilder::create<UnaryOp>(UnaryOpType::Cast, out, v1);
return out;
}
TensorView* castOp(DataType dtype, TensorView* v1) {
return castOp(dtype, v1->as<Val>())->as<TensorView>();
}
Val* bitCastOp(DataType dtype, Val* v1) {
if (v1->getDataType().value() == dtype) {
return v1;
}
TORCH_CHECK(
dataTypeSize(v1->getDataType().value()) == dataTypeSize(dtype),
"BitCast only works for types of the same size");
Val* out = newValLike(v1, dtype);
IrBuilder::create<UnaryOp>(UnaryOpType::BitCast, out, v1);
return out;
}
TensorView* bitCastOp(DataType dtype, TensorView* v1) {
return bitCastOp(dtype, v1->as<Val>())->as<TensorView>();
}
Val* unaryOp(UnaryOpType type, Val* v1) {
TORCH_INTERNAL_ASSERT(
type != UnaryOpType::Address,
"The reference operator & is not accessible in the Fusion IR");
Val* out = newValLike(v1, v1->getDataType().value());
IrBuilder::create<UnaryOp>(type, out, v1);
return out;
}
TensorView* unaryOp(UnaryOpType type, TensorView* v1) {
return unaryOp(type, v1->as<Val>())->as<TensorView>();
}
Val* unaryIsOp(UnaryOpType type, Val* v) {
Val* out = newValLike(v, DataType::Bool);
IrBuilder::create<UnaryOp>(type, out, v);
return out;
}
TensorView* unaryIsOp(UnaryOpType type, TensorView* v) {
return unaryOp(type, v->asVal())->as<TensorView>();
}
Val* unaryOp(UnaryOpType type, Val* v1, const TypePromotionConfig& config) {
auto cast_v1 = promoteValues(config, {v1}).front();
return unaryOp(type, cast_v1);
}
TensorView* unaryOp(
UnaryOpType type,
TensorView* v1,
const TypePromotionConfig& config) {
auto cast_v1 = promoteValues(config, {v1}).front();
return unaryOp(type, cast_v1)->as<TensorView>();
}
// TENSOR FACTORIES
TensorView* rand(const std::vector<Val*>& shape, DataType dtype) {
auto n = shape.size();
auto out = TensorViewBuilder()
.ndims(n)
.dtype(dtype)
.contiguity(std::vector<bool>(n, true))
.shape(shape)
.build();
IrBuilder::create<RNGOp>(RNGOpType::Uniform, out);
return out;
}
TensorView* arange(Val* end, DataType dtype) {
return arange(FusionGuard::getCurFusion()->zeroVal(), end, dtype);
}
TensorView* arange(Val* start, Val* end, DataType dtype) {
return arange(start, end, FusionGuard::getCurFusion()->oneVal(), dtype);
}
TensorView* arange(Val* start, Val* end, Val* step, DataType dtype) {
if (isIntegralType(dtype)) {
start = castOp(DataType::Int, start);
end = castOp(DataType::Int, end);
step = castOp(DataType::Int, step);
} else if (isFloatingPointType(dtype)) {
start = castOp(DataType::Double, start);
end = castOp(DataType::Double, end);
step = castOp(DataType::Double, step);
}
auto size = castOp(DataType::Int, ceilDiv(sub(end, start), step));
auto out = TensorViewBuilder()
.ndims(1)
.dtype(dtype)
.contiguity({true})
.shape({size})
.build();
IrBuilder::create<ARangeOp>(out, start, end, step);
return out;
}
// UNARY OPERATIONS
#define NVFUSER_DEFINE_UNARY_OP(op_name, op_type) \
Val* op_name(Val* v) { \
return unaryOp(UnaryOpType::op_type, v); \
} \
TensorView* op_name(TensorView* tv) { \
return unaryOp(UnaryOpType::op_type, tv); \
}
NVFUSER_DEFINE_UNARY_OP(set, Set)
NVFUSER_DEFINE_UNARY_OP(ceil, Ceil)
NVFUSER_DEFINE_UNARY_OP(floor, Floor)
NVFUSER_DEFINE_UNARY_OP(frac, Frac)
NVFUSER_DEFINE_UNARY_OP(neg, Neg)
NVFUSER_DEFINE_UNARY_OP(relu, Relu)
NVFUSER_DEFINE_UNARY_OP(round, Round)
NVFUSER_DEFINE_UNARY_OP(silu, Silu)
NVFUSER_DEFINE_UNARY_OP(trunc, Trunc)
NVFUSER_DEFINE_UNARY_OP(print, Print)
#undef NVFUSER_DEFINE_UNARY_OP
TensorView* randlike(TensorView* v) {
TORCH_CHECK(
isFloatingPointType(v->dtype()),
"input must have floating point type, but got ",
v->dtype());
std::vector<Val*> shape;
shape.reserve(v->getMaybeRFactorDomain().size());
for (auto id : v->getMaybeRFactorDomain()) {
shape.emplace_back(id->getMaybeExpandedExtent());
}
return rand(shape, v->dtype());
}
Val* randlike(Val* v) {
return randlike(v->as<TensorView>());
}
Val* bitwise_not(Val* v) {
TORCH_CHECK(
isIntegralType(v->dtype()) || v->dtype() == DataType::Bool,
"input must have integral or boolean type, but got ",
v->dtype());
return unaryOp(UnaryOpType::Not, v);
}
TensorView* bitwise_not(TensorView* tv) {
TORCH_CHECK(
isIntegralType(tv->dtype()) || tv->dtype() == DataType::Bool,
"input must have integral or boolean type, but got ",
tv->dtype());
return unaryOp(UnaryOpType::Not, tv);
}
// The output of abs(complex_tensor) are real numbers
Val* abs(Val* v) {
if (v->getDataType() == DataType::ComplexDouble) {
Val* out = newValLike(v, DataType::Double);
IrBuilder::create<UnaryOp>(UnaryOpType::Abs, out, v);
return out;
}
if (v->getDataType() == DataType::ComplexFloat) {
Val* out = newValLike(v, DataType::Float);
IrBuilder::create<UnaryOp>(UnaryOpType::Abs, out, v);
return out;
}
return unaryOp(UnaryOpType::Abs, v);
}
TensorView* abs(TensorView* tv) {
return abs(tv->as<Val>())->as<TensorView>();
}
// The output of real(complex_tensor) are real numbers
Val* real(Val* v) {
if (v->getDataType() == DataType::ComplexDouble) {
Val* out = newValLike(v, DataType::Double);
IrBuilder::create<UnaryOp>(UnaryOpType::Real, out, v);
return out;
}
if (v->getDataType() == DataType::ComplexFloat) {
Val* out = newValLike(v, DataType::Float);
IrBuilder::create<UnaryOp>(UnaryOpType::Real, out, v);
return out;
}
// We use UnaryOpType::Set instead of UnaryOpType::Real to support non-complex
// tensors
return unaryOp(UnaryOpType::Set, v);
}
TensorView* real(TensorView* tv) {
return real(tv->as<Val>())->as<TensorView>();
}
// The output of imag(complex_tensor) are real numbers
Val* imag(Val* v) {
if (v->getDataType() == DataType::ComplexDouble) {
Val* out = newValLike(v, DataType::Double);
IrBuilder::create<UnaryOp>(UnaryOpType::Imag, out, v);
return out;
}
if (v->getDataType() == DataType::ComplexFloat) {
Val* out = newValLike(v, DataType::Float);
IrBuilder::create<UnaryOp>(UnaryOpType::Imag, out, v);
return out;
}
TORCH_CHECK(false, "imag not supported for non-complex tensors");
}
TensorView* imag(TensorView* tv) {
return imag(tv->as<Val>())->as<TensorView>();
}
// UNARY FLOAT CAST OPERATIONS
#define NVFUSER_DEFINE_UNARY_FLOAT_OP(op_name, op_type) \
Val* op_name(Val* v) { \
return unaryOp(UnaryOpType::op_type, v, TypePromotion::float_op_config); \
} \
TensorView* op_name(TensorView* tv) { \
return unaryOp(UnaryOpType::op_type, tv, TypePromotion::float_op_config); \
}
NVFUSER_DEFINE_UNARY_FLOAT_OP(acos, Acos)
NVFUSER_DEFINE_UNARY_FLOAT_OP(asin, Asin)
NVFUSER_DEFINE_UNARY_FLOAT_OP(atan, Atan)
NVFUSER_DEFINE_UNARY_FLOAT_OP(atanh, Atanh)
NVFUSER_DEFINE_UNARY_FLOAT_OP(cos, Cos)
NVFUSER_DEFINE_UNARY_FLOAT_OP(cosh, Cosh)
NVFUSER_DEFINE_UNARY_FLOAT_OP(exp, Exp)
NVFUSER_DEFINE_UNARY_FLOAT_OP(expm1, Expm1)
NVFUSER_DEFINE_UNARY_FLOAT_OP(erf, Erf)
NVFUSER_DEFINE_UNARY_FLOAT_OP(erfc, Erfc)
NVFUSER_DEFINE_UNARY_FLOAT_OP(lgamma, Lgamma)
NVFUSER_DEFINE_UNARY_FLOAT_OP(log, Log)
NVFUSER_DEFINE_UNARY_FLOAT_OP(log10, Log10)
NVFUSER_DEFINE_UNARY_FLOAT_OP(log1p, Log1p)
NVFUSER_DEFINE_UNARY_FLOAT_OP(log2, Log2)
NVFUSER_DEFINE_UNARY_FLOAT_OP(reciprocal, Reciprocal)
NVFUSER_DEFINE_UNARY_FLOAT_OP(rsqrt, Rsqrt)
NVFUSER_DEFINE_UNARY_FLOAT_OP(sigmoid, Sigmoid)
NVFUSER_DEFINE_UNARY_FLOAT_OP(sin, Sin)
NVFUSER_DEFINE_UNARY_FLOAT_OP(sinh, Sinh)
NVFUSER_DEFINE_UNARY_FLOAT_OP(sqrt, Sqrt)
NVFUSER_DEFINE_UNARY_FLOAT_OP(tan, Tan)
NVFUSER_DEFINE_UNARY_FLOAT_OP(tanh, Tanh)
#undef NVFUSER_DEFINE_UNARY_FLOAT_OP
#define NVFUSER_DEFINE_UNARY_IS_OP(op_name, op_type) \
Val* op_name(Val* v) { \
return unaryIsOp(UnaryOpType::op_type, v); \
} \
TensorView* op_name(TensorView* tv) { \
return unaryIsOp(UnaryOpType::op_type, tv); \
}
NVFUSER_DEFINE_UNARY_IS_OP(isfinite, IsFinite)
NVFUSER_DEFINE_UNARY_IS_OP(isinf, IsInf)
NVFUSER_DEFINE_UNARY_IS_OP(isnan, IsNan)
NVFUSER_DEFINE_UNARY_IS_OP(isneginf, IsNegInf)
NVFUSER_DEFINE_UNARY_IS_OP(isposinf, IsPosInf)
NVFUSER_DEFINE_UNARY_IS_OP(isreal, IsReal)
#undef NVFUSER_DEFINE_UNARY_IS_OP
// BINARY OPERATIONS
namespace {
// Helper function to reduce repetitive code
template <typename T1, typename T2>
TensorView* arithOpOverloads(Val* (*func)(Val*, Val*), T1* v1, T2* v2) {
Val* out = func(v1->template as<Val>(), v2->template as<Val>());
TORCH_INTERNAL_ASSERT(out->isA<TensorView>());
return out->as<TensorView>();
}
template <typename T1, typename T2>
TensorView* arithOpOverloads(
BinaryOpType type,
T1* v1,
T2* v2,
DataType common_dtype) {
Val* out = binaryOp(
type, v1->template as<Val>(), v2->template as<Val>(), common_dtype);
TORCH_INTERNAL_ASSERT(out->isA<TensorView>());
return out->as<TensorView>();
}
template <typename T1, typename T2, typename T3>
TensorView* arithOpOverloads(
Val* (*func)(Val*, Val*, Val*),
T1* v1,
T2* v2,
T3* v3) {
auto vals = maybeBroadcast({v1, v2, v3});
Val* out = func(
vals[0]->template as<Val>(),
vals[1]->template as<Val>(),
vals[2]->template as<Val>());
TORCH_INTERNAL_ASSERT(out->isA<TensorView>());
return out->as<TensorView>();
}
template <typename T1, typename T2, typename T3, typename T4>
TensorView* arithOpOverloads(
Val* (*func)(Val*, Val*, Val*, Val*),
T1* v1,
T2* v2,
T3* v3,
T4* v4) {
auto vals = maybeBroadcast({v1, v2, v3, v4});
Val* out = func(
vals[0]->template as<Val>(),
vals[1]->template as<Val>(),
vals[2]->template as<Val>(),
vals[3]->template as<Val>());
TORCH_INTERNAL_ASSERT(out->isA<TensorView>());
return out->as<TensorView>();
}
// Output type promotion logic for binary operators
DataType getOutputType(
BinaryOpType op_type,
Val* v1,
Val* v2,
DataType common_dtype) {
if (isLogicalOp(op_type)) {
return DataType::Bool;
} else if (common_dtype == DataType::Null) {
return promote_type(v1->getDataType().value(), v2->getDataType().value());
} else {
return common_dtype;
}
}
} // namespace
Val* binaryOp(BinaryOpType type, Val* v1, Val* v2, DataType common_dtype) {
const auto out_dtype = getOutputType(type, v1, v2, common_dtype);
const auto out_vtype =
promote_type(v1->getValType().value(), v2->getValType().value());
auto vals = maybeBroadcast({v1, v2});
Val* out = nullptr;
if (out_vtype == ValType::TensorView) {
out = newOutputTV(vals, out_dtype);
} else {
out = newScalar(out_vtype, out_dtype);
}
IrBuilder::create<BinaryOp>(type, out, vals[0], vals[1]);
return out;
}
TensorView* binaryOp(
BinaryOpType type,
TensorView* v1,
Val* v2,
DataType common_dtype) {
return arithOpOverloads(type, v1, v2, common_dtype);
}
TensorView* binaryOp(
BinaryOpType type,
Val* v1,
TensorView* v2,
DataType common_dtype) {
return arithOpOverloads(type, v1, v2, common_dtype);
}
TensorView* binaryOp(
BinaryOpType type,
TensorView* v1,
TensorView* v2,
DataType common_dtype) {
return arithOpOverloads(type, v1, v2, common_dtype);
}
Val* binaryOp(
BinaryOpType type,
Val* v1,
Val* v2,
const TypePromotionConfig& config) {
std::vector<Val*> operands = {v1, v2};
auto common_dtype = computeTypes(config, operands);
auto cast_values = promoteValues(operands, common_dtype);
return binaryOp(type, cast_values.front(), cast_values.back(), common_dtype);
}
TensorView* binaryOp(
BinaryOpType type,
TensorView* v1,
Val* v2,
const TypePromotionConfig& config) {
std::vector<Val*> operands = {v1, v2};
auto common_dtype = computeTypes(config, operands);
auto cast_values = promoteValues(operands, common_dtype);
return binaryOp(
type,
cast_values.front()->as<TensorView>(),
cast_values.back(),
common_dtype);
}
TensorView* binaryOp(
BinaryOpType type,
Val* v1,
TensorView* v2,
const TypePromotionConfig& config) {
std::vector<Val*> operands = {v1, v2};
auto common_dtype = computeTypes(config, operands);
auto cast_values = promoteValues(operands, common_dtype);
return binaryOp(
type,
cast_values.front(),
cast_values.back()->as<TensorView>(),
common_dtype);
}
TensorView* binaryOp(
BinaryOpType type,
TensorView* v1,
TensorView* v2,
const TypePromotionConfig& config) {
std::vector<Val*> operands = {v1, v2};
auto common_dtype = computeTypes(config, operands);
auto cast_values = promoteValues(operands, common_dtype);
return binaryOp(
type,
cast_values.front()->as<TensorView>(),
cast_values.back()->as<TensorView>(),
common_dtype);
}
#define NVFUSER_DEFINE_BINARY_FLOAT_OP(op_name, op_type) \
Val* op_name(Val* v1, Val* v2) { \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::float_op_config); \
} \
TensorView* op_name(TensorView* v1, Val* v2) { \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::float_op_config); \
} \
TensorView* op_name(Val* v1, TensorView* v2) { \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::float_op_config); \
} \
TensorView* op_name(TensorView* v1, TensorView* v2) { \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::float_op_config); \
}
NVFUSER_DEFINE_BINARY_FLOAT_OP(div, Div)
NVFUSER_DEFINE_BINARY_FLOAT_OP(atan2, Atan2)
#undef NVFUSER_DEFINE_BINARY_FLOAT_OP
#define NVFUSER_DEFINE_BINARY_CAST_OP(op_name, op_type) \
Val* op_name(Val* v1, Val* v2) { \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::default_op_config); \
} \
TensorView* op_name(TensorView* v1, Val* v2) { \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::default_op_config); \
} \
TensorView* op_name(Val* v1, TensorView* v2) { \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::default_op_config); \
} \
TensorView* op_name(TensorView* v1, TensorView* v2) { \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::default_op_config); \
}
// Integer binary ops
NVFUSER_DEFINE_BINARY_CAST_OP(mod, Mod)
NVFUSER_DEFINE_BINARY_CAST_OP(ceilDiv, CeilDiv)
NVFUSER_DEFINE_BINARY_CAST_OP(add, Add)
NVFUSER_DEFINE_BINARY_CAST_OP(fmod, Fmod)
NVFUSER_DEFINE_BINARY_CAST_OP(mul, Mul)
NVFUSER_DEFINE_BINARY_CAST_OP(pow, Pow)
NVFUSER_DEFINE_BINARY_CAST_OP(remainder, Remainder)
NVFUSER_DEFINE_BINARY_CAST_OP(sub, Sub)
#undef NVFUSER_DEFINE_BINARY_CAST_OP
#define NVFUSER_DEFINE_BITWISE_OP(op_name, op_type) \
Val* op_name(Val* v1, Val* v2) { \
TORCH_CHECK( \
(isIntegralType(v1->dtype()) || v1->dtype() == DataType::Bool) && \
(isIntegralType(v2->dtype()) || v2->dtype() == DataType::Bool), \
"input must have integral or boolean type, but got ", \
v1->dtype(), \
" and ", \
v2->dtype()); \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::default_op_config); \
} \
TensorView* op_name(TensorView* v1, Val* v2) { \
TORCH_CHECK( \
(isIntegralType(v1->dtype()) || v1->dtype() == DataType::Bool) && \
(isIntegralType(v2->dtype()) || v2->dtype() == DataType::Bool), \
"input must have integral or boolean type, but got ", \
v1->dtype(), \
" and ", \
v2->dtype()); \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::default_op_config); \
} \
TensorView* op_name(Val* v1, TensorView* v2) { \
TORCH_CHECK( \
(isIntegralType(v1->dtype()) || v1->dtype() == DataType::Bool) && \
(isIntegralType(v2->dtype()) || v2->dtype() == DataType::Bool), \
"input must have integral or boolean type, but got ", \
v1->dtype(), \
" and ", \
v2->dtype()); \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::default_op_config); \
} \
TensorView* op_name(TensorView* v1, TensorView* v2) { \
TORCH_CHECK( \
(isIntegralType(v1->dtype()) || v1->dtype() == DataType::Bool) && \
(isIntegralType(v2->dtype()) || v2->dtype() == DataType::Bool), \
"input must have integral or boolean type, but got ", \
v1->dtype(), \
" and ", \
v2->dtype()); \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::default_op_config); \
}
NVFUSER_DEFINE_BITWISE_OP(bitwise_and, And)
NVFUSER_DEFINE_BITWISE_OP(bitwise_or, Or)
NVFUSER_DEFINE_BITWISE_OP(bitwise_xor, Xor)
#undef NVFUSER_DEFINE_BITWISE_OP
#define NVFUSER_DEFINE_BITWISE_SHIFT_OP(op_name, op_type) \
Val* op_name(Val* v1, Val* v2) { \
TORCH_CHECK( \
isIntegralType(v1->dtype()) && isIntegralType(v2->dtype()), \
"input must have integral type, but got ", \
v1->dtype(), \
" and ", \
v2->dtype()); \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::default_op_config); \
} \
TensorView* op_name(TensorView* v1, Val* v2) { \
TORCH_CHECK( \
isIntegralType(v1->dtype()) && isIntegralType(v2->dtype()), \
"input must have integral type, but got ", \
v1->dtype(), \
" and ", \
v2->dtype()); \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::default_op_config); \
} \
TensorView* op_name(Val* v1, TensorView* v2) { \
TORCH_CHECK( \
isIntegralType(v2->dtype()) && isIntegralType(v2->dtype()), \
"input must have integral type, but got ", \
v1->dtype(), \
" and ", \
v2->dtype()); \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::default_op_config); \
} \
TensorView* op_name(TensorView* v1, TensorView* v2) { \
TORCH_CHECK( \
isIntegralType(v1->dtype()) && isIntegralType(v2->dtype()), \
"input must have integral type, but got ", \
v1->dtype(), \
" and ", \
v2->dtype()); \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::default_op_config); \
}
NVFUSER_DEFINE_BITWISE_SHIFT_OP(bitwise_left_shift, Lshift)
NVFUSER_DEFINE_BITWISE_SHIFT_OP(bitwise_right_shift, Rshift)
#undef NVFUSER_DEFINE_BITWISE_SHIFT_OP
#define NVFUSER_DEFINE_BINARY_COMPARE_OP(op_name, op_type) \
Val* op_name(Val* v1, Val* v2) { \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::comparison_op_config); \
} \
TensorView* op_name(TensorView* v1, Val* v2) { \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::comparison_op_config); \
} \
TensorView* op_name(Val* v1, TensorView* v2) { \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::comparison_op_config); \
} \
TensorView* op_name(TensorView* v1, TensorView* v2) { \
return binaryOp( \
BinaryOpType::op_type, v1, v2, TypePromotion::comparison_op_config); \
}
// Logical binary ops
NVFUSER_DEFINE_BINARY_COMPARE_OP(eq, Eq)
NVFUSER_DEFINE_BINARY_COMPARE_OP(ge, GE)
NVFUSER_DEFINE_BINARY_COMPARE_OP(gt, GT)
NVFUSER_DEFINE_BINARY_COMPARE_OP(le, LE)
NVFUSER_DEFINE_BINARY_COMPARE_OP(lt, LT)
NVFUSER_DEFINE_BINARY_COMPARE_OP(ne, NE)
#undef NVFUSER_DEFINE_BINARY_COMPARE_OP
// REDUCTION OPERATIONS
// TODO: How do we adjust this so we can reduce to a single scalar value?
static TensorView* newForReduction(
TensorView* tv,
const std::vector<unsigned int>& axes,
DataType data_type = DataType::Null) {
auto orig_domain = TensorDomain::noReductions(tv->getMaybeRFactorDomain());
std::set<unsigned int> axes_set(axes.begin(), axes.end());
std::vector<IterDomain*> new_domain;
TORCH_INTERNAL_ASSERT(
!axes_set.empty(),
"Asked for ouput of reduction, but no reduction axis provided.");
TORCH_INTERNAL_ASSERT(
(*(axes_set.rbegin())) < orig_domain.size(),
"Error setting up reduction, reduction axis (",
*(axes_set.rbegin()),
") is outside nDims (",
orig_domain.size(),
"). Keep in mind reductions are relative to root domains, not modified views.");
auto axis_iter = axes_set.begin();
for (const auto dim : c10::irange(orig_domain.size())) {
bool isReduction = false;
if (axis_iter != axes_set.end() && *axis_iter == dim) {
isReduction = true;
axis_iter++;
}
const IterDomain* id = orig_domain[dim];
TORCH_CHECK(
!(isReduction && id->isBroadcast() && !id->isImplicitBroadcast()),
"Cannot reduce an axis that is marked as broadcasted as it has an undetermined size. Tried to reduce ID = ",
id,
" of tensor ",
tv);
new_domain.push_back(
IterDomainBuilder(id)
// If the domain is being reduced, but it's coming in as an expanded
// extent, we need to realize the expand.
.extent(
isReduction && id->hasExpandedExtent() ? id->expandedExtent()
: id->extent())
.resetSchedulingParams()
.iter_type(isReduction ? IterType::Reduction : id->getIterType())
.build());
}
TensorDomain* td = IrBuilder::create<TensorDomain>(
new_domain, std::vector<bool>(new_domain.size(), true));
data_type =
data_type == DataType::Null ? tv->getDataType().value() : data_type;
return IrBuilder::create<TensorView>(td, data_type);
}
namespace {
// PyTorch accepts reductions of zero-dimensional tensors, which are
// just ignored.
TensorView* reductionOpZeroDimTensor(TensorView* inp) {
TORCH_INTERNAL_ASSERT(inp->domain()->noReductions().size() == 0);
return set(inp);
}
} // namespace
TensorView* reductionOp(
BinaryOpType reduction_op_type,
const std::vector<int>& axes,
Val* init,
TensorView* tv,
bool keep_dim /*=false*/,
DataType dtype /* DataType::Null */) {
TORCH_CHECK(
init->isConstScalar(),
"Cannot create a reduction operation where the initial value is not a const scalar.");
TORCH_CHECK(
TensorDomain::sameAs(tv->getMaybeRFactorDomain(), tv->domain()->domain()),
"Reducing a tensor once it's gone under transformations is not permitted at this time. Please set reductions before calling split/merge/computeAt.");
TORCH_CHECK(axes.size() > 0, "No reduction axis specified");
// PyTorch allows reduction of 0-dim tensors
if (tv->domain()->noReductions().size() == 0) {
return reductionOpZeroDimTensor(tv);
}
std::vector<unsigned int> uint_axes;
const int ndims = tv->domain()->noReductions().size();
for (int axis : axes) {
if (axis < 0) {
axis += ndims;
}
TORCH_CHECK(
axis >= 0 && axis < ndims,
"Reduction on invalid axis, recieved: ",
axis,
" however tensor view only has ",
ndims,
" non-reduction dims.");
uint_axes.push_back((unsigned int)axis);
}
TensorView* out = newForReduction(tv, uint_axes, dtype);
const auto out_type = out->getDataType().value();
const auto init_type = init->getDataType().value();
TORCH_CHECK(
(isFloatingPointType(out_type) && isFloatingPointType(init_type)) ||
(isComplexType(out_type) && isComplexType(init_type)) ||
(isIntegralType(out_type) && isIntegralType(init_type)) ||
(isBooleanType(out_type) && isBooleanType(init_type)),
"Types should match for reduction ops but received: ",
out_type,
" and ",
init_type);
IrBuilder::create<ReductionOp>(reduction_op_type, init, out, tv);
if (keep_dim) {
auto tv_root = TensorDomain::noReductions(tv->getMaybeRFactorDomain());
std::vector<bool> is_broadcast(tv_root.size(), false);
for (auto axis : uint_axes) {
is_broadcast.at(axis) = true;
}
out = broadcast(out, is_broadcast);
}
return out;
}
TensorView* sum(
TensorView* v1,
const std::vector<int>& axes,
bool keep_dim /*=false*/,
DataType dtype /* DataType::Null */) {
if (dtype == DataType::Null) {
auto initial_v1_dtype = v1->getDataType().value();
if (isBooleanType(initial_v1_dtype) || isIntegralType(initial_v1_dtype)) {
dtype = DataType::Int;
}
}
// Cast input tensor to dtype before the operation is performed
if (dtype != DataType::Null) {
v1 = optionalCastStrict(dtype, v1)->as<TensorView>();
}
Val* init = nullptr;
auto v1_dtype = v1->getDataType().value();
if (isFloatingPointType(v1_dtype)) {
init = IrBuilder::create<Double>(0.0);
} else if (isComplexType(v1_dtype)) {
init = IrBuilder::create<ComplexDouble>(c10::complex<double>(0.0, 0.0));
} else if (isIntegralType(v1_dtype)) {
init = FusionGuard::getCurFusion()->zeroVal();
} else if (isBooleanType(v1_dtype)) {
init = IrBuilder::create<Bool>(false);
} else {
TORCH_CHECK(
false, "Could not generate a sum op for tensor with type: ", v1_dtype);
}
return reductionOp(BinaryOpType::Add, axes, init, v1, keep_dim, dtype);
}
TensorView* max(
TensorView* v1,
const std::vector<int>& axes,
bool keep_dim /*=false*/,
DataType dtype /* DataType::Null */) {
TORCH_CHECK(
dtype == DataType::Null,
"A dtype other than Null is not currently supported.");
Val* init = getMinimumValue(v1->getDataType().value());
TORCH_CHECK(init != nullptr, "Missing initial value");
return reductionOp(BinaryOpType::Max, axes, init, v1, keep_dim);
}
TensorView* min(
TensorView* v1,
const std::vector<int>& axes,
bool keep_dim /*=false*/,
DataType dtype /* DataType::Null */) {
TORCH_CHECK(
dtype == DataType::Null,
"A dtype other than Null is not currently supported.");
Val* init = getMaximumValue(v1->getDataType().value());
TORCH_CHECK(init != nullptr, "Missing initial value");
return reductionOp(BinaryOpType::Min, axes, init, v1, keep_dim);
}
TensorView* broadcast(
TensorView* inp,
const std::vector<bool>& is_broadcast_dim) {
auto nBCastDims = is_broadcast_dim.size();
// Validate is_broadcast_dim
unsigned int n_broadcasts = 0;
for (auto ent : is_broadcast_dim) {
if (ent) {
n_broadcasts++;
}
}
TORCH_CHECK(
nBCastDims - n_broadcasts ==
TensorDomain::noReductions(inp->getMaybeRFactorDomain()).size(),
"Invalid broadcast, number of false entries in is_broadcast_dim expected to be ",
TensorDomain::noReductions(inp->getMaybeRFactorDomain()).size(),
" but received ",
nBCastDims - n_broadcasts);
if (n_broadcasts == 0) {
auto identity = set(inp);
TORCH_INTERNAL_ASSERT(
identity->getValType().value() == ValType::TensorView,
"Expected identity op, but didn't get a TensorView back.");
return identity->as<TensorView>();
}
std::vector<IterDomain*> out_domain;
// Don't propagate reduction IDs through arith ops.
auto inp_domain = TensorDomain::noReductions(inp->getMaybeRFactorDomain());
size_t iinp = 0, ibdim = 0;
while (ibdim < is_broadcast_dim.size()) {
if (is_broadcast_dim[ibdim]) {
out_domain.push_back(IterDomainBuilder(
FusionGuard::getCurFusion()->zeroVal(),
FusionGuard::getCurFusion()->oneVal())
.iter_type(IterType::Broadcast)
.build());
} else {
out_domain.push_back(
IterDomainBuilder(inp_domain[iinp]).resetSchedulingParams().build());
iinp++;
}
ibdim++;
}
TensorView* out_tensor = IrBuilder::create<TensorView>(
IrBuilder::create<TensorDomain>(
out_domain, std::vector<bool>(out_domain.size(), true)),
inp->getDataType().value());
IrBuilder::create<BroadcastOp>(out_tensor, inp, is_broadcast_dim);
return out_tensor;
}
TensorView* expand(TensorView* inp, const std::vector<Val*>& expanded_sizes) {
auto inp_domain = TensorDomain::noReductions(inp->getMaybeRFactorDomain());
TORCH_CHECK(
expanded_sizes.size() >= inp_domain.size(),
"Invalid expand, number of sizes provided is expected to be at least ",
inp_domain.size(),
" but received ",
expanded_sizes.size());
inp = maybe_broadcast_inner_to_rank(inp, expanded_sizes.size());
inp_domain = TensorDomain::noReductions(inp->getMaybeRFactorDomain());
std::vector<Val*> maybe_expanded_sizes;
maybe_expanded_sizes.resize(inp_domain.size(), nullptr);
// Did a dimension actually get expanded
bool expanded = false;
std::vector<IterDomain*> out_domain;
for (auto i : c10::irange(inp_domain.size())) {
auto inp_id = inp_domain[i];
auto out_id_builder = IterDomainBuilder(inp_id);
maybe_expanded_sizes[i] = inp_domain[i]->extent();
auto expanded_size_int = expanded_sizes[i]->getInt();
// If the expanded size is -1, let the input extent be propagated
// as is
if (expanded_size_int == -1) {
// This is just done for clarity. It isn't necessary as it's
// already done when constructing out_id_builder.
out_id_builder.extent(inp_id->extent());
} else if (inp_id->isBroadcast() && expanded_size_int != 1) {
// When input id is a broadcast, expand the extent to the given
// size, which can be concrete or symbolic.
expanded = true;
out_id_builder.expanded_extent(expanded_sizes[i]);
maybe_expanded_sizes[i] = expanded_sizes[i];
} else if (!inp_id->extent()->isConstInt()) {
// Input id is non-broadcast and its extent is symbolic. Promote
// the extent to the given expanded size.
// Note that expansion to 1 just means its extent becomes 1 and
// does not mean the ID becomes a broadcast.
out_id_builder.extent(expanded_sizes[i]);
} else {
// Input id is non-expand and its extent is concrete. Nothing
// to expand, but the input and expanded sizes should match if
// the expanded size is also concrete.
auto inp_id_size_int = inp_id->extent()->getInt();
if (expanded_size_int.has_value()) {
TORCH_CHECK(
inp_id_size_int == expanded_size_int,
"Invalid expand size, ",
expanded_sizes[i]->toString(),
", for ",
inp_id->toString());
}
}
out_domain.push_back(out_id_builder.build());
}
TensorView* out_tensor = IrBuilder::create<TensorView>(
IrBuilder::create<TensorDomain>(
out_domain, std::vector<bool>(out_domain.size(), true)),
inp->getDataType().value());
if (!expanded) {
IrBuilder::create<UnaryOp>(UnaryOpType::Set, out_tensor, inp);
} else {
IrBuilder::create<ExpandOp>(out_tensor, inp, maybe_expanded_sizes);
}
return out_tensor;
}
TensorView* expand_as(TensorView* inp, TensorView* other) {
auto inp_domain = TensorDomain::noReductions(inp->getMaybeRFactorDomain());
auto other_domain =
TensorDomain::noReductions(other->getMaybeRFactorDomain());
TORCH_CHECK(
inp_domain.size() <= other_domain.size(),
"Invalid expand_as, dimensions of inp is higher than dimensions of other, expected other to be at least ",
inp_domain.size(),
" but received ",
other_domain.size());
inp = maybe_broadcast_inner_to_rank(inp, other_domain.size());
inp_domain = TensorDomain::noReductions(inp->getMaybeRFactorDomain());
std::vector<IterDomain*> out_domain;
std::vector<Val*> maybe_expanded_sizes;
bool expanded = false;
for (auto i : c10::irange(inp_domain.size())) {
auto inp_id = inp_domain[i];
auto other_id = other_domain[i];
auto out_id_builder = IterDomainBuilder(inp_id);
Val* maybe_expanded_size = inp_id->extent();
if (!inp_id->isBroadcast()) {
TORCH_INTERNAL_ASSERT(
!other_id->isBroadcast(),
"Cannot expand as a tensor if other has broadcast dimensions that don't map to broadcast dimensions in the input.");
if (!inp_id->isConstInt() && other_id->isConstInt()) {
out_id_builder.extent(
promoteSize(inp_id->extent(), other_id->extent()));
}
} else {
if (!other_id->isBroadcast()) {
expanded = true;
out_id_builder.expanded_extent(other_id->extent());
maybe_expanded_size = other_id->extent();
} else if (other_id->isBroadcast() && other_id->hasExpandedExtent()) {
expanded = true;
out_id_builder.expanded_extent(other_id->expandedExtent());
maybe_expanded_size = other_id->expandedExtent();
}
}
out_domain.push_back(out_id_builder.build());
maybe_expanded_sizes.push_back(maybe_expanded_size);
}
TensorView* out_tensor = IrBuilder::create<TensorView>(
IrBuilder::create<TensorDomain>(
out_domain, std::vector<bool>(out_domain.size(), true)),
inp->getDataType().value());
if (!expanded) {
IrBuilder::create<UnaryOp>(UnaryOpType::Set, out_tensor, inp);
} else {
IrBuilder::create<ExpandOp>(out_tensor, inp, maybe_expanded_sizes);
}
return out_tensor;
}
WelfordResult Welford(
TensorView* tv,
const std::vector<int>& axes,
TensorView* init_avg,
TensorView* init_var,
Int* init_N) {
TORCH_CHECK(
TensorDomain::sameAs(tv->getRootDomain(), tv->domain()->domain()),
"Reducing a tensor once it's gone under transformations is not permitted at this time. Please set reductions before calling split/merge/computeAt.");
TORCH_CHECK(tv->nDims() > 0, "Tried to reduce a 0-dim tensor");
TORCH_CHECK(axes.size() > 0, "No reduction axis specified");
if (init_N == nullptr) {
init_N = FusionGuard::getCurFusion()->zeroVal();
}
// Initial values for welford op are tensors, so their dims have to match the
// output dim,
// i.e. original_dims - dims_to_be_reduced
Val* init_avg_val = nullptr;
Val* init_var_val = nullptr;
if (!init_N->isZeroInt()) {
TORCH_CHECK(
init_avg != nullptr && init_var != nullptr && init_N != nullptr,
"welford op: all init values need to be provided");
TORCH_CHECK(
(axes.size() + init_avg->getRootDomain().size()) ==
tv->getRootDomain().size(),
"welford op: initial tensor mismatch");
TORCH_CHECK(
(axes.size() + init_var->getRootDomain().size()) ==
tv->getRootDomain().size(),
"welford op: initial tensor mismatch");
init_avg_val = init_avg;
init_var_val = init_var;
} else {
init_avg_val = IrBuilder::create<Double>(0);
init_var_val = IrBuilder::create<Double>(0);
}
// Check and collect reduction axes
std::vector<unsigned int> uint_axes;
const int ndims = tv->domain()->noReductions().size();
for (int axis : axes) {
if (axis < 0) {
axis += ndims;
}
TORCH_CHECK(
axis >= 0 && axis < ndims,
"Reduction on invalid axis, recieved: ",
axis,
" however tensor view only has ",
ndims,
" non-reduction dims.");
uint_axes.push_back((unsigned int)axis);
}
// Create tensor outputs
TensorView* out_avg = newForReduction(tv, uint_axes);
TensorView* out_var = newForReduction(tv, uint_axes);
TensorView* out_N = newForReduction(tv, uint_axes, DataType::Index);
IrBuilder::create<WelfordOp>(
out_avg,
out_var,
out_N, /*out var/avg/count */
tv, /*in var/avg/count */
FusionGuard::getCurFusion()->zeroVal(),
FusionGuard::getCurFusion()->oneVal(),
init_avg_val,
init_var_val,
init_N); /*init var/avg/count */
return WelfordResult(out_avg, out_var, out_N);
}
WelfordResult::WelfordResult(
TensorView* in_avg,
TensorView* in_var_sum,
TensorView* in_n)
: avg(in_avg), var_sum(in_var_sum), n(in_n) {
TORCH_INTERNAL_ASSERT(avg->definition()->sameAs(var_sum->definition()));
TORCH_INTERNAL_ASSERT(avg->definition()->sameAs(n->definition()));
}
// COMPOUND OPERATIONS
// add_alpha
Val* add_alpha(Val* v1, Val* v2, Val* s) {
TORCH_CHECK(
s->getValType().value() == ValType::Scalar,
"Alpha value should be a Scalar Valtype and not ",
s->getValType().value());
std::vector<Val*> operands = {v1, v2};
auto common_dtype = computeTypes(TypePromotion::default_op_config, operands);
auto cast_values = promoteValues({v1, v2, s}, common_dtype);
auto vals = maybeBroadcast(cast_values);
Val* intrm = mul(vals[1], vals[2]);
return add(vals[0], intrm);
}
TensorView* add_alpha(TensorView* v1, Val* v2, Val* v3) {
return arithOpOverloads(add_alpha, v1, v2, v3);
}
TensorView* add_alpha(Val* v1, TensorView* v2, Val* v3) {
return arithOpOverloads(add_alpha, v1, v2, v3);
}
TensorView* add_alpha(TensorView* v1, TensorView* v2, Val* v3) {
return arithOpOverloads(add_alpha, v1, v2, v3);
}
// sub_alpha
Val* sub_alpha(Val* v1, Val* v2, Val* s) {
TORCH_CHECK(
s->getValType().value() == ValType::Scalar,
"Alpha value should be a Scalar Valtype and not ",
s->getValType().value());
std::vector<Val*> operands = {v1, v2};
auto common_dtype = computeTypes(TypePromotion::default_op_config, operands);
auto cast_values = promoteValues({v1, v2, s}, common_dtype);
auto vals = maybeBroadcast(cast_values);
Val* intrm = mul(vals[1], vals[2]);
return sub(vals[0], intrm);
}
TensorView* sub_alpha(TensorView* v1, Val* v2, Val* v3) {
return arithOpOverloads(sub_alpha, v1, v2, v3);
}
TensorView* sub_alpha(Val* v1, TensorView* v2, Val* v3) {
return arithOpOverloads(sub_alpha, v1, v2, v3);
}
TensorView* sub_alpha(TensorView* v1, TensorView* v2, Val* v3) {
return arithOpOverloads(sub_alpha, v1, v2, v3);
}
// lerp
Val* lerp(Val* start, Val* end, Val* weight) {
auto cast_values =
promoteValues(TypePromotion::default_op_config, {start, end, weight});
start = cast_values[0];
end = cast_values[1];
weight = cast_values[2];
auto out_dtype =
promote_type(start->getDataType().value(), end->getDataType().value());
auto out_vtype =
promote_type(start->getValType().value(), end->getValType().value());
auto vals = maybeBroadcast({start, end, weight});
Val* out = nullptr;
if (out_vtype == ValType::TensorView) {
out = newOutputTV(vals, out_dtype);
} else {
out = newScalar(out_vtype, out_dtype);
}
IrBuilder::create<TernaryOp>(
TernaryOpType::Lerp, out, vals[0], vals[1], vals[2]);
return out;
}
TensorView* lerp(TensorView* v1, Val* v2, Val* v3) {
return arithOpOverloads(lerp, v1, v2, v3);
}
TensorView* lerp(Val* v1, TensorView* v2, Val* v3) {
return arithOpOverloads(lerp, v1, v2, v3);
}
TensorView* lerp(Val* v1, Val* v2, TensorView* v3) {
return arithOpOverloads(lerp, v1, v2, v3);
}
TensorView* lerp(TensorView* v1, TensorView* v2, Val* v3) {
return arithOpOverloads(lerp, v1, v2, v3);
}
TensorView* lerp(TensorView* v1, Val* v2, TensorView* v3) {
return arithOpOverloads(lerp, v1, v2, v3);
}
TensorView* lerp(Val* v1, TensorView* v2, TensorView* v3) {
return arithOpOverloads(lerp, v1, v2, v3);
}
TensorView* lerp(TensorView* v1, TensorView* v2, TensorView* v3) {
return arithOpOverloads(lerp, v1, v2, v3);
}
// addcmul
Val* addcmul(Val* v1, Val* v2, Val* v3, Val* s) {
TORCH_CHECK(
s->getValType().value() == ValType::Scalar,
"Alpha value should be a Scalar Valtype and not ",
s->getValType().value());
std::vector<Val*> operands = {v1, v2, v3};
auto common_dtype = computeTypes(TypePromotion::default_op_config, operands);
auto cast_values = promoteValues({v1, v2, v3, s}, common_dtype);
auto vals = maybeBroadcast(cast_values);
Val* intrm1 = mul(vals[2], vals[3]);
Val* intrm2 = mul(vals[1], intrm1);
return add(vals[0], intrm2);
}
TensorView* addcmul(TensorView* v1, Val* v2, Val* v3, Val* v4) {
return arithOpOverloads(addcmul, v1, v2, v3, v4);
}
TensorView* addcmul(Val* v1, TensorView* v2, Val* v3, Val* v4) {
return arithOpOverloads(addcmul, v1, v2, v3, v4);
}
TensorView* addcmul(Val* v1, Val* v2, TensorView* v3, Val* v4) {
return arithOpOverloads(addcmul, v1, v2, v3, v4);
}
TensorView* addcmul(TensorView* v1, TensorView* v2, Val* v3, Val* v4) {
return arithOpOverloads(addcmul, v1, v2, v3, v4);
}
TensorView* addcmul(TensorView* v1, Val* v2, TensorView* v3, Val* v4) {
return arithOpOverloads(addcmul, v1, v2, v3, v4);
}
TensorView* addcmul(Val* v1, TensorView* v2, TensorView* v3, Val* v4) {
return arithOpOverloads(addcmul, v1, v2, v3, v4);
}
TensorView* addcmul(TensorView* v1, TensorView* v2, TensorView* v3, Val* v4) {
return arithOpOverloads(addcmul, v1, v2, v3, v4);
}
// TERNARY OPERATIONS
// where (c ? v1 : v2)
Val* where(Val* c, Val* v1, Val* v2) {
TORCH_CHECK(
c->getDataType().value() == DataType::Bool,
"Condition should be of DataType Bool, not ",
c->getDataType().value());
std::vector<Val*> operands = {v1, v2};
auto common_dtype = computeTypes(TypePromotion::default_op_config, operands);
auto cast_values = promoteValues(operands, common_dtype);
v1 = cast_values[0];
v2 = cast_values[1];
TORCH_CHECK(c->getDataType().value() == DataType::Bool);
auto out_dtype = common_dtype;
auto out_vtype =
promote_type(v1->getValType().value(), v2->getValType().value());
// Even when v1 and v2 are scalar, the output is a tensor if the
// conditional input is a tensor.
if (c->getValType() == ValType::TensorView) {
out_vtype = ValType::TensorView;
}
auto vals = maybeBroadcast({c, v1, v2});
Val* out = nullptr;
if (out_vtype == ValType::TensorView) {
out = newOutputTV(vals, out_dtype);
} else {
out = newScalar(out_vtype, out_dtype);
}
IrBuilder::create<TernaryOp>(
TernaryOpType::Where, out, vals[0], vals[1], vals[2]);
return out;
}
TensorView* where(TensorView* v1, Val* v2, Val* v3) {
return arithOpOverloads(where, v1, v2, v3);
}
TensorView* where(Val* v1, TensorView* v2, Val* v3) {
return arithOpOverloads(where, v1, v2, v3);
}
TensorView* where(Val* v1, Val* v2, TensorView* v3) {
return arithOpOverloads(where, v1, v2, v3);
}
TensorView* where(TensorView* v1, TensorView* v2, Val* v3) {
return arithOpOverloads(where, v1, v2, v3);
}
TensorView* where(TensorView* v1, Val* v2, TensorView* v3) {
return arithOpOverloads(where, v1, v2, v3);
}
TensorView* where(Val* v1, TensorView* v2, TensorView* v3) {
return arithOpOverloads(where, v1, v2, v3);
}
TensorView* where(TensorView* v1, TensorView* v2, TensorView* v3) {
return arithOpOverloads(where, v1, v2, v3);
}
// TERNARY OPERATIONS
Val* threshold(Val* in, Val* thresh, Val* value) {
TORCH_CHECK(
(thresh->getValType().value() == ValType::Scalar ||
thresh->getValType().value() == ValType::NamedScalar) &&
(value->getValType().value() == ValType::Scalar ||
value->getValType().value() == ValType::NamedScalar),
"For Threshold operation: Thresh and Value values should be Scalars.");
thresh = optionalCast(in->getDataType().value(), thresh);
value = optionalCast(in->getDataType().value(), value);
Val* out = newValLike(in, in->getDataType().value());
IrBuilder::create<TernaryOp>(
TernaryOpType::Threshold, out, in, thresh, value);
return out;
}
TensorView* threshold(TensorView* in, Val* thresh, Val* value) {
return threshold(in->as<Val>(), thresh, value)->as<TensorView>();
}
Val* clamp(Val* in, Val* min_val, Val* max_val) {
TORCH_CHECK(
(min_val == nullptr || min_val->getValType().value() == ValType::Scalar ||
min_val->getValType().value() == ValType::NamedScalar) &&
(max_val == nullptr ||
max_val->getValType().value() == ValType::Scalar ||
max_val->getValType().value() == ValType::NamedScalar),
"For Clamp operation: Min and Max values should be Scalars.");
min_val = (min_val == nullptr)
? getMinimumValue(in->getDataType().value())
: optionalCast(in->getDataType().value(), min_val);
TORCH_CHECK(min_val != nullptr, "Missing minimum value");
max_val = (max_val == nullptr)
? getMaximumValue(in->getDataType().value())
: optionalCast(in->getDataType().value(), max_val);
TORCH_CHECK(max_val != nullptr, "Missing maximum value");
Val* out = newValLike(in, in->getDataType().value());
IrBuilder::create<TernaryOp>(TernaryOpType::Clamp, out, in, min_val, max_val);
return out;
}
TensorView* clamp(TensorView* in, Val* min_val, Val* max_val) {
return clamp(in->as<Val>(), min_val, max_val)->as<TensorView>();
}
// sum_to operator
TensorView* sum_to(TensorView* in, const std::vector<Int*>& sum_to_size) {
const auto& root = TensorDomain::noReductions(in->getMaybeRFactorDomain());
TORCH_CHECK(
root.size() >= sum_to_size.size(),
"sum_to: Error trying to reduce",
in,
"into a shape of size",
sum_to_size.size());
// If no reduction is needed sum_to returns the input tv
TensorView* out = in;
const int64_t leading_dims = root.size() - sum_to_size.size();
// Generate reduction axes for leading dims
std::vector<int> reduce_dims(leading_dims);
std::iota(reduce_dims.begin(), reduce_dims.end(), 0);
// Generate reduction axes for dims within sum_to_size
std::vector<bool> inner_red_dims(sum_to_size.size(), false);
bool reduction_within_shape = false;
// Reduce rest of the dims with keep_dim
for (const auto i : c10::irange(leading_dims, root.size())) {
if (sum_to_size[i - leading_dims]->isOneInt() &&
!root[i]->extent()->isOneInt()) {
inner_red_dims[i - leading_dims] = true;
reduce_dims.push_back(i);
reduction_within_shape = true;
}
}
// Reduction step
if (!reduce_dims.empty()) {
out = sum(in, reduce_dims);
}
// Broadcast back reduced dims within shape
if (reduction_within_shape) {
out = broadcast(out, inner_red_dims);
}
return out;
}
TensorView* sum_to(TensorView* in, const std::vector<int64_t>& sum_to_size) {
const auto& root = TensorDomain::noReductions(in->getMaybeRFactorDomain());
TORCH_CHECK(
root.size() >= sum_to_size.size(),
"sum_to: Error trying to reduce",
in,
"into a shape of size",
sum_to_size.size());
// If no reduction is needed sum_to returns the input tv
TensorView* out = in;
const int64_t leading_dims = root.size() - sum_to_size.size();
// Generate reduction axes for leading dims
std::vector<int> reduce_dims(leading_dims);
std::iota(reduce_dims.begin(), reduce_dims.end(), 0);
// Generate reduction axes for dims within sum_to_size
std::vector<bool> inner_red_dims(sum_to_size.size(), false);
bool reduction_within_shape = false;
// Reduce rest of the dims with keep_dim
for (const auto i : c10::irange(leading_dims, root.size())) {
if (sum_to_size[i - leading_dims] == 1 && !root[i]->extent()->isOneInt()) {
inner_red_dims[i - leading_dims] = true;
reduce_dims.push_back(i);
reduction_within_shape = true;
}
}
// Reduction step
if (!reduce_dims.empty()) {
out = sum(in, reduce_dims);
}
// Broadcast back reduced dims within shape
if (reduction_within_shape) {
out = broadcast(out, inner_red_dims);
}
return out;
}
TensorView* shift(TensorView* inp, const std::vector<int>& offsets, bool pad) {
// When pad is false, no padding is given. When it is true, padding
// sizes are set so that output domains have the same extents as
// input domains.
std::vector<int> pad_width(offsets.size(), 0);
if (pad) {
for (const auto i : c10::irange(offsets.size())) {
pad_width[i] = std::abs(offsets[i]);
}
}
return shift(inp, offsets, pad_width);
}
TensorView* shift(
TensorView* inp,
const std::vector<int>& offsets,
const std::vector<int>& pad_width_param) {
auto inp_dom = TensorDomain::noReductions(inp->getRootDomain());
const auto ndims = inp_dom.size();
auto pad_width = pad_width_param;
// Default padding is set so that the extent is kept unchanged
if (pad_width.empty()) {
pad_width = offsets;
for (auto& p : pad_width) {
p = std::abs(p);
}
}
TORCH_CHECK(
ndims == offsets.size(),
"Invalid shift offsets, number of entries in offsets expected to be ",
ndims,
" but received ",
offsets.size());
TORCH_CHECK(
ndims == pad_width.size(),
"Invalid padding width list, number of entries in pad_width expected to be ",
ndims,
" but received ",
pad_width.size());
std::for_each(pad_width.begin(), pad_width.end(), [](const auto& pad) {
TORCH_CHECK(pad >= 0, "Padding width must be >= 0: ", pad);
});
TensorView* out = nullptr;
std::vector<IterDomain*> out_dom;
for (const auto i : c10::irange(ndims)) {
const auto inp_axis = inp_dom[i];
const auto offset = offsets[i];
const auto pad = pad_width[i];
if (offset == 0) {
out_dom.push_back(inp_axis->cloneWithoutRFactor());
continue;
}
Int* current_start_offset = dynamic_cast<Int*>(inp_axis->start());
TORCH_INTERNAL_ASSERT(
current_start_offset != nullptr && current_start_offset->isConst(),
"Invalid IterDomain start value:",
current_start_offset);
Int* current_stop_offset = dynamic_cast<Int*>(inp_axis->stopOffset());
TORCH_INTERNAL_ASSERT(
current_stop_offset != nullptr && current_stop_offset->isConst(),
"Invalid IterDomain stop offset value:",
current_stop_offset);
const auto cur_start_offset_value = current_start_offset->value().value();
const auto cur_stop_offset_value = current_stop_offset->value().value();
int64_t out_start_offset = 0;
int64_t out_stop_offset = 0;
if (offset > 0) {
// shift to right; extent remains the same, start and stop
// positions are moved right
out_start_offset = cur_start_offset_value + offset - pad;
out_stop_offset = std::max(cur_stop_offset_value - offset, int64_t(0));
// If pad > offset, the extent of the output ID could be larger than the
// input, and the start offset of the output domain could become
// negative, which is not supported.
TORCH_CHECK(
out_start_offset >= 0,
"Invalid shift offset and padding. Padding must not be larger than the absolute extent of shift offset. Padding: ",
pad,
". Shift: ",
offset,
".");
} else {
// shift to left; extent remains the same, start and stop
// positions are moved left
out_start_offset = std::max(cur_start_offset_value + offset, int64_t(0));
out_stop_offset = cur_stop_offset_value - offset - pad;
// Similar to the above case whwere offset is positive, if pad >
// -offset (note offset is negative), the extent of the output
// ID could be larger than the input, and the stop offset of the
// output domain could become negative.
TORCH_CHECK(
out_stop_offset >= 0,
"Invalid shift offset and padding. Padding must not be larger than the absolute extent of shift offset. Padding: ",
pad,
". Shift: ",
offset,
".");
}
out_dom.push_back(
IterDomainBuilder(
IrBuilder::create<Int>(out_start_offset), inp_axis->extent())
.stop_offset(IrBuilder::create<Int>(out_stop_offset))
.iter_type(inp_axis->getIterType())
.build());
}
out = IrBuilder::create<TensorView>(
IrBuilder::create<TensorDomain>(
out_dom, std::vector<bool>(out_dom.size(), true)),
inp->getDataType().value());
IrBuilder::create<ShiftOp>(out, inp, offsets, pad_width);
return out;
}
namespace {
// Return a new TensorDomain with given root domains. Apply
// strides if necessary. With non-unit strides, strided domains become an
// rfactor domain.
TensorDomain* generateTensorDomainWithStrides(
const std::vector<IterDomain*>& root_domains,
const std::vector<int>& strides,
bool skip_unit_stride) {
std::vector<IterDomain*> strided_domains;
// If strides are just unit strides, don't apply striding
if (strides.empty() ||
(skip_unit_stride &&
std::all_of(
strides.begin(), strides.end(), [](int s) { return s == 1; }))) {
return IrBuilder::create<TensorDomain>(
root_domains, std::vector<bool>(root_domains.size(), true));
}
for (const auto i : c10::irange(root_domains.size())) {
auto root_dom = root_domains.at(i);
if (i >= strides.size() || (skip_unit_stride && strides[i] == 1)) {
strided_domains.push_back(root_dom);
continue;
}
// Split the root domain by the stride
auto split_out = root_dom->stridedSplit(strides[i]);
strided_domains.push_back(split_out.first);
strided_domains.push_back(split_out.second);
}
auto contig_vector_size = strided_domains.size();
auto strided_td = IrBuilder::create<TensorDomain>(
root_domains,
strided_domains,
strided_domains,
std::vector<bool>(contig_vector_size, true));
return strided_td;
}
} // namespace
TensorView* gather(
TensorView* inp,
const std::vector<int>& window_shape,
const std::vector<std::vector<int>>& pad_width,
const std::vector<int>& strides,
bool trim_out_of_bounds) {
auto inp_dom = TensorDomain::noReductions(inp->getMaybeRFactorDomain());
const auto ndims = inp_dom.size();
TORCH_CHECK(
ndims == window_shape.size(),
"Invalid window shape: number of entries expected to be ",
ndims,
" but received ",
window_shape.size());
std::for_each(window_shape.begin(), window_shape.end(), [](const auto& w) {
TORCH_CHECK(w > 0, "Window size must be > 0: ", w);
});
TORCH_CHECK(
ndims == pad_width.size(),
"Invalid pad width: number of entries expected to be ",
ndims,
" but received ",
pad_width.size());
std::for_each(pad_width.begin(), pad_width.end(), [](const auto& p) {
TORCH_CHECK(
p.size() == 2,
"Each entry of pad_width must have two non-negative integers.");
std::for_each(p.begin(), p.end(), [](const auto& p_left_or_right) {
TORCH_CHECK(
p_left_or_right >= 0, "Padding must be >= 0: ", p_left_or_right);
});
});
TORCH_CHECK(
strides.empty() || ndims == strides.size(),
"Invalid strides: number of entries expected to be ",
ndims,
" but received ",
strides.size());
std::for_each(strides.begin(), strides.end(), [](const auto& s) {
TORCH_CHECK(s > 0, "Stride must be > 0: ", s);
});
std::vector<IterDomain*> out_root_domains;
std::vector<IterDomain*> out_gather_dom;
for (const auto i : c10::irange(ndims)) {
const auto inp_axis = inp_dom[i];
const auto window_dim = window_shape[i];
const auto pad_left = pad_width[i][0];
const auto pad_right = pad_width[i][1];
// This may be over-conservative
TORCH_INTERNAL_ASSERT(inp_axis->start()->isZeroInt());
TORCH_INTERNAL_ASSERT(
inp_axis->stopOffset()->isConstInt(),
"Dynamic stop offset not supported: ",
inp_axis);
const auto inp_stop_offset = inp_axis->stopOffset()->evaluateInt();
const auto extent_adjustment = window_dim - 1 - pad_left - pad_right;
TORCH_CHECK(
extent_adjustment >= 0,
"Invalid gather window and padding as output extent would be larger than input.",
" Window: ",
window_dim,
". Padding left: ",
pad_left,
". Padding right: ",
pad_right);
const auto out_stop_offset = inp_stop_offset + extent_adjustment;
out_root_domains.push_back(
IterDomainBuilder(
FusionGuard::getCurFusion()->zeroVal(), inp_axis->extent())
.stop_offset(IrBuilder::create<Int>(out_stop_offset))
.iter_type(inp_axis->getIterType())
.build());
// create a new axis for the gathered domain
out_gather_dom.push_back(IterDomainBuilder(
FusionGuard::getCurFusion()->zeroVal(),
IrBuilder::create<Int>(window_dim))
.iter_type(IterType::Gather)
.build());
}
out_root_domains.insert(
out_root_domains.end(), out_gather_dom.begin(), out_gather_dom.end());
TensorDomain* out_td = nullptr;
if (trim_out_of_bounds) {
// If no stride vector is given, just use stride 1. It does not do
// any striding effect, but out-of-bounds values are trimmed.
auto s = strides.empty() ? std::vector<int>(ndims, 1) : strides;
out_td = generateTensorDomainWithStrides(out_root_domains, strides, false);
} else {
out_td = generateTensorDomainWithStrides(out_root_domains, strides, true);
}
auto out_tv =
IrBuilder::create<TensorView>(out_td, inp->getDataType().value());
IrBuilder::create<GatherOp>(out_tv, inp, window_shape, pad_width);
return out_tv;
}
TORCH_CUDA_CU_API TensorView* viewAsScalar(TensorView* inp) {
auto inp_type = inp->getDataType().value();
TORCH_CHECK(
isVectorType(inp_type),
"Invalid type to viewAsScalar. A vector type is expected but ",
inp_type,
" is given.");
int vec_size = getVectorSizeFromType(inp_type);
auto out_type = getTypeFromVectorType(inp_type);
std::vector<IterDomain*> out_domain;
auto inp_domain = TensorDomain::noReductions(inp->getMaybeRFactorDomain());
out_domain.reserve(inp_domain.size());
for (auto d : inp_domain) {
out_domain.push_back(d->cloneWithoutRFactor());
}
IterDomain* id = IterDomainBuilder(
inp_domain[0]->container()->zeroVal(),
IrBuilder::create<Int>(vec_size))
.iter_type(IterType::VectorComponent)
.build();
out_domain.push_back(id);
auto out = IrBuilder::create<TensorView>(
inp->container(),
IrBuilder::create<TensorDomain>(
out_domain, std::vector<bool>(out_domain.size(), true)),
out_type);
IrBuilder::create<ViewAsScalar>(inp->container(), out, inp, id);
return out;
}
namespace {
//! Create new output for mma
static TensorView* newForMma(
TensorView* tv_a,
TensorView* tv_b,
const std::vector<unsigned int>& axes,
DataType data_type = DataType::Float) {
auto orig_domain_a =
TensorDomain::noReductions(tv_a->getMaybeRFactorDomain());
auto orig_domain_b =
TensorDomain::noReductions(tv_b->getMaybeRFactorDomain());
TORCH_INTERNAL_ASSERT(
orig_domain_a.size() == orig_domain_b.size(),
"MMA op: need matching dim input");
std::set<unsigned int> axes_set(axes.begin(), axes.end());
std::vector<IterDomain*> new_domain;
TORCH_INTERNAL_ASSERT(
!axes_set.empty(),
"Asked for ouput of reduction, but no reduction axis provided.");
TORCH_INTERNAL_ASSERT(
(*(axes_set.rbegin())) < orig_domain_a.size(),
"Error setting up reduction, reduction axis (",
*(axes_set.rbegin()),
") is outside nDims (",
orig_domain_a.size(),
"). Keep in mind reductions are relative to root domains, not modified views.");
auto axis_iter = axes_set.begin();
for (const auto dim : c10::irange(orig_domain_a.size())) {
bool isReduction = false;
if (axis_iter != axes_set.end() && *axis_iter == dim) {
isReduction = true;
axis_iter++;
}
const IterDomain* id = orig_domain_a[dim]->isBroadcast()
? orig_domain_b[dim]
: orig_domain_a[dim];
TORCH_CHECK(
!(isReduction && id->isBroadcast() && !id->isImplicitBroadcast()),
"Cannot reduce an axis that is marked as broadcasted as it has an undetermined size. Tried to reduce ID = ",
id,
" of tensor ",
tv_a,
"and",
tv_b);
new_domain.push_back(
IterDomainBuilder(id->start(), id->extent())
.stop_offset(id->stopOffset())
.iter_type(isReduction ? IterType::Reduction : id->getIterType())
.build());
}
TensorDomain* td = IrBuilder::create<TensorDomain>(
new_domain, std::vector<bool>(new_domain.size(), true));
return IrBuilder::create<TensorView>(td, data_type);
}
} // namespace
TensorView* fusedMultiplySum(
TensorView* tv_a,
TensorView* tv_b,
const std::vector<int>& axes,
Val* init) {
if (init == nullptr) {
init = IrBuilder::create<Double>(0);
}
// TODO:
// We will want to support initialize and rfactor with
// mma as well, for maybe fusing bias in prolog.
// TODO: check init type if given a tv,
// not supported currently though.
TORCH_CHECK(
init->isConstScalar(),
"Cannot create a reduction operation where the initial value is not a const scalar.");
// TODO:
// Validate axis relationships between a and b
TORCH_CHECK(tv_a->nDims() > 0, "Tried to reduce a 0-dim tensor");
// TODO:
// Add tf32 and other mma data types
// Add fallback path for non-mma data types.
TORCH_CHECK(tv_a->getDataType().value() == DataType::Half);
TORCH_CHECK(tv_b->getDataType().value() == DataType::Half);
TORCH_CHECK(axes.size() > 0, "No reduction axis specified");
// TODO:
// will lift this in a follow up when we have a
// more generic axes matching.
TORCH_CHECK(
axes.size() == 1, "Single axis reduction only for mma op instantiation.")
std::vector<unsigned int> uint_axes;
const int ndims = tv_a->domain()->noReductions().size();
for (int axis : axes) {
if (axis < 0) {
axis += ndims;
}
TORCH_CHECK(
axis >= 0 && axis < ndims,
"Reduction on invalid axis, recieved: ",
axis,
" however tensor view only has ",
ndims,
" non-reduction dims.");
uint_axes.push_back((unsigned int)axis);
}
TensorView* out = newForMma(tv_a, tv_b, uint_axes);
IrBuilder::create<MmaOp>(out, tv_a, tv_b, init);
return out;
}
} // namespace cuda
} // namespace fuser
} // namespace jit
} // namespace torch
|