File size: 108,559 Bytes
f6bc13b | 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 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 | import functools
from functools import lru_cache
from typing import Any, Optional, Tuple
import tilelang
import tilelang.language as T
import torch
from sglang.kernels.ops.quantization.fp8_kernel import is_fp8_fnuz
from sglang.srt.utils import is_gfx95_supported, is_hip
tilelang.set_log_level("WARNING")
# Workaround a tilelang bug: BaseKernelAdapter._legalize_result_idx mutates the
# `out_idx` list in place when normalising negative indices to positive ones.
# That breaks any @tilelang.jit factory that compiles two prim_funcs with
# different param counts (e.g. our unified single/dual partial kernel) — the
# second compile sees indices already-converted for the first's len(params)
# and silently builds the wrong adapter, leading to IndexError at call time.
# Patch once on import to copy the list before mutation.
from tilelang.jit.adapter.base import ( # noqa: E402
BaseKernelAdapter as _BaseKernelAdapter,
)
if not getattr(_BaseKernelAdapter, "_legalize_result_idx_patched", False):
_orig_legalize = _BaseKernelAdapter._legalize_result_idx
def _legalize_result_idx_safe(self, result_idx):
if isinstance(result_idx, list):
result_idx = list(result_idx)
return _orig_legalize(self, result_idx)
_BaseKernelAdapter._legalize_result_idx = _legalize_result_idx_safe
_BaseKernelAdapter._legalize_result_idx_patched = True
pass_configs = {
tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True,
tilelang.PassConfigKey.TL_DISABLE_TMA_LOWER: True,
}
# TL_DISABLE_FAST_MATH has deprecated in v0.1.7.post1 tilelang
if hasattr(tilelang.PassConfigKey, "TL_DISABLE_FAST_MATH"):
pass_configs[tilelang.PassConfigKey.TL_DISABLE_FAST_MATH] = True
elif hasattr(tilelang.PassConfigKey, "TL_ENABLE_FAST_MATH"):
pass_configs[tilelang.PassConfigKey.TL_ENABLE_FAST_MATH] = False
_is_hip = is_hip()
_is_gfx95_supported = is_gfx95_supported()
_is_fp8_fnuz = is_fp8_fnuz()
BF16 = "bfloat16"
FP8 = "float8_e4m3fnuz" if _is_fp8_fnuz else "float8_e4m3fn"
FP8_DTYPE = torch.float8_e4m3fnuz if _is_fp8_fnuz else torch.float8_e4m3fn
FP32 = "float32"
INT32 = "int32"
UINT8 = "uint8"
def fast_log2_ceil(x):
bits_x = T.reinterpret("uint32", x)
exp_x = (bits_x >> 23) & 0xFF
man_bits = bits_x & ((1 << 23) - 1)
return T.Cast("int32", exp_x - 127 + T.if_then_else(man_bits != 0, 1, 0))
def fast_pow2(x):
bits_x = (x + 127) << 23
return T.reinterpret("float32", bits_x)
def fast_round_scale(amax, fp8_max_inv):
return fast_pow2(fast_log2_ceil(amax * fp8_max_inv))
@lru_cache(maxsize=8)
def _pick_inner_iter(seq: int, ni: int, cu: int, block_per_cu: int) -> int:
"""
Pick the largest valid inner_iter (power-of-two divisor of ni) that keeps
enough work per CU (seq * ni / inner_iter / cu >= block_per_cu), so we avoid
under-utilization while minimizing the number of partial groups.
"""
max_it = int(seq * ni / (cu * block_per_cu))
it = ni
while it >= 2:
if it <= max_it and ni % it == 0:
return it
it //= 2
return 1
@tilelang.jit(pass_configs=pass_configs)
def act_quant_kernel(
N, in_dtype=BF16, out_dtype=FP8, scale_dtype=FP32, round_scale=False
):
M = T.symbolic("M")
fp8_min = -224.0 if _is_fp8_fnuz else -448.0
fp8_max = 224.0 if _is_fp8_fnuz else 448.0
fp8_max_inv = 1 / fp8_max
num_stages = 0 if round_scale else 2
blk_m = 32
group_size = 128
@T.prim_func
def act_quant_kernel_(
X: T.Tensor[(M, N), in_dtype],
Y: T.Tensor[(M, N), out_dtype],
S: T.Tensor[(M, T.ceildiv(N, group_size)), scale_dtype],
):
with T.Kernel(T.ceildiv(M, blk_m), T.ceildiv(N, group_size), threads=128) as (
pid_m,
pid_n,
):
x_shared = T.alloc_shared((blk_m, group_size), in_dtype)
x_local = T.alloc_fragment((blk_m, group_size), in_dtype)
amax_local = T.alloc_fragment((blk_m,), scale_dtype)
s_local = T.alloc_fragment((blk_m,), scale_dtype)
y_local = T.alloc_fragment((blk_m, group_size), out_dtype)
y_shared = T.alloc_shared((blk_m, group_size), out_dtype)
for _ in T.Pipelined(1, num_stages=num_stages):
T.copy(X[pid_m * blk_m, pid_n * group_size], x_shared)
T.copy(x_shared, x_local)
T.reduce_absmax(x_local, amax_local, dim=1)
for i in T.Parallel(blk_m):
amax_local[i] = T.max(amax_local[i], 1e-4)
if round_scale:
s_local[i] = fast_round_scale(amax_local[i], fp8_max_inv)
else:
s_local[i] = amax_local[i] * fp8_max_inv
for i, j in T.Parallel(blk_m, group_size):
y_local[i, j] = T.clamp(
x_local[i, j] / s_local[i], fp8_min, fp8_max
)
for i in T.Parallel(blk_m):
S[pid_m * blk_m + i, pid_n] = s_local[i]
T.copy(y_local, y_shared)
T.copy(y_shared, Y[pid_m * blk_m, pid_n * group_size])
return act_quant_kernel_
def act_quant(
x: torch.Tensor, block_size: int = 128, scale_fmt: Optional[str] = None
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Quantizes the input tensor `x` using block-wise quantization.
Args:
x (torch.Tensor): The input tensor to be quantized. Must be contiguous and its last dimension size must be divisible by `block_size`.
block_size (int, optional): The size of the blocks to be used for quantization. Default is 128.
scale_fmt (Optional[str], optional): The format of the scale. Default is None.
Returns:
Tuple[torch.Tensor, torch.Tensor]: A tuple containing:
- The quantized tensor with dtype `torch.float8_e4m3fn`.
- A tensor of scaling factors with dtype `torch.float32`.
"""
assert x.is_contiguous(), "Input tensor must be contiguous"
assert (
x.size(-1) % block_size == 0
), f"Last dimension size must be divisible by block_size (block_size={block_size})"
N = x.size(-1)
if _is_fp8_fnuz:
y = torch.empty_like(x, dtype=torch.float8_e4m3fnuz)
else:
y = torch.empty_like(x, dtype=torch.float8_e4m3fn)
s = x.new_empty(*x.size()[:-1], N // block_size, dtype=torch.float32)
kernel = act_quant_kernel(N, round_scale=scale_fmt is not None)
kernel(x.view(-1, N), y.view(-1, N), s.view(-1, N // block_size))
return y, s
@tilelang.jit(out_idx=[4], pass_configs=pass_configs)
def fp8_index_kernel(h: int, d: int, clear_accum=True):
b = T.symbolic("b")
m = T.symbolic("m")
n = T.symbolic("n")
blk_n1 = 512
blk_n2 = 128
@T.prim_func
def fp8_index_kernel_(
q: T.Tensor[(b, m, h, d), FP8],
q_s: T.Tensor[(b, m, h), FP32],
k: T.Tensor[(b, n, d), FP8],
k_s: T.Tensor[(b, n), FP32],
o: T.Tensor[(b, m, n), FP32],
) -> None:
with T.Kernel(b, m, T.ceildiv(n, blk_n1)) as (i_b, i_m, i1_n):
q_smem = T.alloc_shared((h, d), FP8)
T.copy(q[i_b, i_m, 0, 0], q_smem)
q_s_frag = T.alloc_fragment(h, FP32)
T.copy(q_s[i_b, i_m, 0], q_s_frag)
for i2_n in T.Pipelined(blk_n1 // blk_n2, num_stages=2):
k_smem = T.alloc_shared((blk_n2, d), FP8)
T.copy(k[i_b, i1_n * blk_n1 + i2_n * blk_n2, 0], k_smem)
k_s_frag = T.alloc_fragment(blk_n2, FP32)
T.copy(k_s[i_b, i1_n * blk_n1 + i2_n * blk_n2], k_s_frag)
logits = T.alloc_fragment((blk_n2, h), FP32)
if not clear_accum:
T.fill(logits, 0)
T.gemm(
k_smem,
q_smem,
logits,
transpose_A=False,
transpose_B=True,
clear_accum=clear_accum,
)
for i_h, i3_n in T.Parallel(h, blk_n2):
logits[i3_n, i_h] = T.max(logits[i3_n, i_h], 0) * q_s_frag[i_h]
logits_sum = T.alloc_fragment(blk_n2, FP32)
T.reduce_sum(logits, logits_sum, dim=1)
for i3_n in T.Parallel(blk_n2):
logits_sum[i3_n] *= k_s_frag[i3_n]
T.copy(logits_sum, o[i_b, i_m, i1_n * blk_n1 + i2_n * blk_n2])
return fp8_index_kernel_
def fp8_index(
q: torch.Tensor,
q_s: torch.Tensor,
k: torch.Tensor,
k_s: torch.Tensor,
) -> torch.Tensor:
"""
Perform index score using FP8 precision.
Args:
q (torch.Tensor): The Q tensor, must be contiguous.
q_s (torch.Tensor): The scaling factor for Q (float), must be contiguous.
k (torch.Tensor): The K tensor, must be contiguous.
k_s (torch.Tensor): The scaling factor for K (e8m0 here), must be contiguous.
fp8 q @ fp8 k -> fp32 logits
relu(fp32 logits) * q_s (weights) -> fp32 logits
fp32 logits -> fp32 logits_sum
fp32 logits_sum * k_s (e8m0) -> fp32 index_score
"""
if _is_hip:
return fp8_index_kernel(q.shape[2], q.shape[3], False)(q, q_s, k, k_s)
else:
return fp8_index_kernel(q.shape[2], q.shape[3])(q, q_s, k, k_s)
@tilelang.jit(
out_idx=[-1],
pass_configs={
tilelang.PassConfigKey.TL_DISABLE_TMA_LOWER: True,
tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True,
},
)
def sparse_attention_fwd_kernel_v1(
num_heads,
dim,
tail_dim,
topk,
*,
kv_group=1,
sm_scale=None,
is_causal=True,
block_I=32,
num_stages=1,
threads=128,
return_lse=False,
):
assert (
dim == tilelang.math.next_power_of_2(dim) or dim % 64 == 0
), f"dim={dim} must be a power of 2 or a multiple of 64"
assert tail_dim == 0 or tail_dim == tilelang.math.next_power_of_2(
tail_dim
), f"tail_dim={tail_dim} must be 0 or a power of 2"
has_tail = tail_dim > 0
assert is_causal == True, "non-casual is not supported"
assert (
topk % block_I == 0
), "otherwise will load some index=0 thus causing wrong kv to be loaded"
if sm_scale is None:
sm_scale = (1.0 / (dim + tail_dim)) ** 0.5 * 1.44269504 # log2(e)
else:
sm_scale = sm_scale * 1.44269504 # log2(e)
batch = T.symbolic("batch")
seq_len = T.symbolic("seq_len")
seq_len_kv = T.symbolic("seq_len_kv")
head_kv = num_heads // kv_group
q_shape = [batch, seq_len, num_heads, dim + tail_dim]
kv_shape = [batch, seq_len_kv, kv_group, dim + tail_dim]
o_shape = [batch, seq_len, num_heads, dim]
lse_shape = [batch, seq_len, num_heads]
indices_shape = [batch, seq_len, kv_group, topk]
indices_dtype = "int32"
dtype = "bfloat16"
accum_dtype = "float"
H = head_kv
padded_H = max(tilelang.math.next_power_of_2(head_kv), 16)
if padded_H != H:
assert kv_group == 1
BI = block_I
NI = tilelang.cdiv(topk, block_I)
D = dim
D_tail = tail_dim
if head_kv > 64:
assert head_kv % 64 == 0, "head_kv should be a multiple of 64"
REPLICATE_H = head_kv // 64
else:
REPLICATE_H = 1
H_per_block = padded_H if REPLICATE_H == 1 else 64
@T.prim_func
def main(
Q: T.Tensor(q_shape, dtype), # type: ignore
KV: T.Tensor(kv_shape, dtype), # type: ignore
Indices: T.Tensor(indices_shape, indices_dtype), # type: ignore
LSE: T.Tensor(lse_shape, accum_dtype), # type: ignore
Output: T.Tensor(o_shape, dtype), # type: ignore
):
with T.Kernel(seq_len * REPLICATE_H, batch, kv_group, threads=threads) as (
bx,
by,
bz,
):
Q_shared = T.alloc_shared([H_per_block, D], dtype)
if has_tail:
Q_tail_shared = T.alloc_shared([H_per_block, D_tail], dtype)
KV_shared = T.alloc_shared([BI, D], dtype)
if has_tail:
K_tail_shared = T.alloc_shared([BI, D_tail], dtype)
O_shared = T.alloc_shared([H_per_block, D], dtype)
mask = T.alloc_fragment([BI], "bool")
acc_o = T.alloc_fragment([H_per_block, D], accum_dtype)
acc_s = T.alloc_fragment([H_per_block, BI], accum_dtype)
S_shared = T.alloc_shared([H_per_block, BI], dtype)
sumexp = T.alloc_fragment([H_per_block], accum_dtype)
sumexp_i = T.alloc_fragment([H_per_block], accum_dtype)
alpha = T.alloc_fragment([H_per_block], accum_dtype)
m_i = T.alloc_fragment([H_per_block], accum_dtype)
m_i_prev = T.alloc_fragment([H_per_block], accum_dtype)
T.fill(acc_o, 0)
T.fill(sumexp, 0)
T.fill(m_i, -(2**30)) # avoid -inf - inf to cause nan
b_i, g_i = by, bz
s_i = bx if REPLICATE_H == 1 else (bx // REPLICATE_H)
q_i = s_i
max_kv_i = q_i
H0 = g_i * padded_H + (0 if REPLICATE_H == 1 else (bx % REPLICATE_H) * 64)
H1 = H0 + H_per_block
T.copy(Q[b_i, s_i, H0:H1, :D], Q_shared)
if has_tail:
T.copy(Q[b_i, s_i, H0:H1, D:], Q_tail_shared)
for i_i in T.Pipelined(NI, num_stages=num_stages):
for bi_i in T.Parallel(BI):
mask[bi_i] = Indices[b_i, s_i, g_i, i_i * BI + bi_i] >= 0
for bi_i, d_i in T.Parallel(BI, D):
KV_shared[bi_i, d_i] = KV[
b_i, Indices[b_i, s_i, g_i, i_i * BI + bi_i], g_i, d_i
]
if has_tail:
for bi_i, d_i in T.Parallel(BI, D_tail):
K_tail_shared[bi_i, d_i] = KV[
b_i,
Indices[b_i, s_i, g_i, i_i * BI + bi_i],
g_i,
D + d_i,
]
for h_i, bi_i in T.Parallel(H_per_block, BI):
acc_s[h_i, bi_i] = T.if_then_else(
mask[bi_i], 0, -T.infinity(acc_s.dtype)
)
T.gemm(
Q_shared,
KV_shared,
acc_s,
transpose_B=True,
policy=T.GemmWarpPolicy.FullCol,
)
if has_tail:
T.gemm(
Q_tail_shared,
K_tail_shared,
acc_s,
transpose_B=True,
policy=T.GemmWarpPolicy.FullCol,
)
T.copy(m_i, m_i_prev)
T.reduce_max(acc_s, m_i, dim=1, clear=False)
for h_i in T.Parallel(H_per_block):
alpha[h_i] = T.exp2((m_i_prev[h_i] - m_i[h_i]) * sm_scale)
for h_i, bi_i in T.Parallel(H_per_block, BI):
acc_s[h_i, bi_i] = T.exp2(
acc_s[h_i, bi_i] * sm_scale - m_i[h_i] * sm_scale
)
T.reduce_sum(acc_s, sumexp_i, dim=1) # is this a accumulate operator?
for h_i in T.Parallel(H_per_block):
sumexp[h_i] = sumexp[h_i] * alpha[h_i] + sumexp_i[h_i]
for h_i, d_i in T.Parallel(H_per_block, D):
acc_o[h_i, d_i] = acc_o[h_i, d_i] * alpha[h_i]
T.copy(acc_s, S_shared)
T.gemm(S_shared, KV_shared, acc_o, policy=T.GemmWarpPolicy.FullCol)
# Rescale
for h_i, d_i in T.Parallel(H_per_block, D):
acc_o[h_i, d_i] /= sumexp[h_i]
for h_i in T.Parallel(H_per_block):
sumexp[h_i] = T.log2(sumexp[h_i]) + m_i[h_i] * sm_scale
if return_lse:
T.copy(sumexp, LSE[b_i, s_i, H0:H1])
T.copy(acc_o, O_shared)
T.copy(acc_o, Output[b_i, s_i, H0:H1, :])
return main
@tilelang.jit(
out_idx=[-1],
compile_flags=[
"-O3",
"-Wno-deprecated-declarations",
"-U__CUDA_NO_HALF_OPERATORS__",
"-U__CUDA_NO_HALF_CONVERSIONS__",
"-U__CUDA_NO_HALF2_OPERATORS__",
"-U__CUDA_NO_BFLOAT16_CONVERSIONS__",
"--expt-relaxed-constexpr",
"--expt-extended-lambda",
"--ptxas-options=-v,--register-usage-level=10",
"-DNDEBUG",
],
) # type: ignore
def sparse_attention_fwd_kernel_v2(
num_heads: int,
dim: int,
tail_dim: int,
topk: int,
*,
kv_group: int = 1,
sm_scale: Optional[float] = None,
block_I: int = 32,
return_lse: bool = False,
):
assert dim == tilelang.math.next_power_of_2(
dim
), f"haven't check padding correctness yet, dim={dim}"
assert tail_dim == tilelang.math.next_power_of_2(
tail_dim
), f"haven't check padding correctness yet, dim={tail_dim}"
assert (
topk % block_I == 0
), "otherwise will load some index=0 thus causing wrong kv to be loaded"
if sm_scale is None:
sm_scale = (1.0 / (dim + tail_dim)) ** 0.5 * 1.44269504 # log2(e)
else:
sm_scale = sm_scale * 1.44269504 # log2(e)
threads = 128
batch = T.symbolic("batch")
qo_len = T.symbolic("seq_len")
num_pages = T.symbolic("num_pages")
q_shape = [batch, qo_len, num_heads, dim + tail_dim]
kv_shape = [batch, num_pages, kv_group, dim + tail_dim]
o_shape = [batch, qo_len, num_heads, dim]
lse_shape = [batch, qo_len, num_heads]
indices_shape = [batch, qo_len, kv_group, topk]
indices_dtype = "int32"
dtype = "bfloat16"
accum_dtype = "float"
H = num_heads
padded_H = max(tilelang.math.next_power_of_2(num_heads), 16)
if padded_H != H:
assert kv_group == 1
BI = block_I
NI = tilelang.cdiv(topk, block_I)
assert NI % 2 == 0, "NI should be a multiple of 2"
D = dim
D_tail = tail_dim
if num_heads > 64:
assert num_heads % 64 == 0, "head_kv should be a multiple of 64"
REPLICATE_H = num_heads // 64
else:
REPLICATE_H = 1
H_per_block = padded_H if REPLICATE_H == 1 else 64
@T.prim_func
def main(
Q: T.Tensor(q_shape, dtype), # type: ignore
KV: T.Tensor(kv_shape, dtype), # type: ignore
Indices: T.Tensor(indices_shape, indices_dtype), # type: ignore
LSE: T.Tensor(lse_shape, accum_dtype), # type: ignore
Output: T.Tensor(o_shape, dtype), # type: ignore
):
"""
Q: [b, qo_len, H, D + D_tail] (bfloat16)
KV: [b, num_pages, kv_group, D + D_tail] (bfloat16)
Indices: [b, qo_len, kv_group, topk] (int32)
"""
with T.Kernel(qo_len * REPLICATE_H, batch, 1, threads=threads) as (bx, by, bz): # type: ignore
Q_shared_l = T.alloc_shared([H_per_block, D // 2], dtype)
Q_shared_r = T.alloc_shared([H_per_block, D // 2], dtype)
Q_tail_shared = T.alloc_shared([H_per_block, D_tail], dtype)
KV_shared_0_l = T.alloc_shared([BI, D // 2], dtype)
KV_shared_0_r = T.alloc_shared([BI, D // 2], dtype)
KV_shared_1_l = T.alloc_shared([BI, D // 2], dtype)
KV_shared_1_r = T.alloc_shared([BI, D // 2], dtype)
K_tail_shared_0 = T.alloc_shared([BI, D_tail], dtype)
K_tail_shared_1 = T.alloc_shared([BI, D_tail], dtype)
O_shared_l = Q_shared_l
O_shared_r = Q_shared_r
is_kv_valid_0 = T.alloc_shared([BI], "bool", scope="shared")
is_kv_valid_1 = T.alloc_shared([BI], "bool", scope="shared")
acc_o_l = T.alloc_fragment([H_per_block, D // 2], accum_dtype)
acc_o_r = T.alloc_fragment([H_per_block, D // 2], accum_dtype)
acc_s = T.alloc_fragment([H_per_block, BI], accum_dtype)
S_shared = T.alloc_shared([H_per_block, BI], dtype)
sumexp = T.alloc_fragment([H_per_block], accum_dtype)
sum_exp_shared = T.alloc_shared([H_per_block], accum_dtype)
sumexp_i = T.alloc_fragment([H_per_block], accum_dtype)
alpha_shared = T.alloc_shared([H_per_block], accum_dtype, scope="shared")
alpha_local = T.alloc_fragment([H_per_block], accum_dtype)
m_i = T.alloc_fragment([H_per_block], accum_dtype)
m_i_prev = T.alloc_fragment([H_per_block], accum_dtype)
indices_local = T.alloc_local([1], indices_dtype)
indices_tmp = T.alloc_local([1], indices_dtype)
bar_q = T.alloc_barrier(arrive_count=384)
bar_k_0_ready = T.alloc_barrier(arrive_count=128)
bar_k_1_ready = T.alloc_barrier(arrive_count=128)
bar_k_0_free = T.alloc_barrier(arrive_count=256)
bar_k_1_free = T.alloc_barrier(arrive_count=256)
bar_sScale_and_sS_ready = T.alloc_barrier(arrive_count=256)
bar_sScale_and_sS_free = T.alloc_barrier(arrive_count=256)
bar_0_128 = T.alloc_barrier(arrive_count=128)
bar_1_128 = T.alloc_barrier(arrive_count=128)
bar_2_128 = T.alloc_barrier(arrive_count=128)
bar_final = T.alloc_barrier(arrive_count=128)
b_i, g_i = by, bz
s_i = bx if REPLICATE_H == 1 else bx // REPLICATE_H
H0 = g_i * padded_H + (0 if REPLICATE_H == 1 else (bx % REPLICATE_H) * 64)
H1 = H0 + H_per_block
tx = T.get_thread_binding()
T.copy(Q[b_i, s_i, H0:H1, 0 : D // 2], Q_shared_l)
T.copy(Q[b_i, s_i, H0:H1, D // 2 : D], Q_shared_r)
T.copy(Q[b_i, s_i, H0:H1, D:], Q_tail_shared)
T.barrier_arrive(bar_q)
if tx < 128:
T.set_max_nreg(240, 1)
T.fill(sumexp, 0)
T.fill(m_i, -(2**30)) # avoid -inf - inf to cause nan
T.fill(acc_o_l, 0)
T.barrier_wait(bar_q, 0)
for i_i in T.serial(T.ceildiv(NI, 2)):
# Buffer 0
# with sync_at(bar_0_128, 0):
T.barrier_wait(bar_k_0_ready[0], (i_i & 1))
T.barrier_arrive(bar_0_128)
T.barrier_wait(bar_0_128, 0)
for h_i, bi_i in T.Parallel(H_per_block, BI):
acc_s[h_i, bi_i] = T.if_then_else(
is_kv_valid_0[bi_i], 0, -T.infinity(acc_s.dtype)
)
T.gemm(Q_shared_l, KV_shared_0_l, acc_s, transpose_B=True)
T.gemm(Q_shared_r, KV_shared_0_r, acc_s, transpose_B=True)
T.gemm(
Q_tail_shared,
K_tail_shared_0,
acc_s,
transpose_B=True,
)
if i_i != 0:
T.barrier_arrive(bar_sScale_and_sS_free)
T.barrier_wait(bar_sScale_and_sS_free, ((i_i * 2) & 1) ^ 1)
T.copy(m_i, m_i_prev)
T.reduce_max(acc_s, m_i, dim=1, clear=False)
for h_i in T.Parallel(H_per_block):
alpha_local[h_i] = T.exp2((m_i_prev[h_i] - m_i[h_i]) * sm_scale)
for h_i, bi_i in T.Parallel(H_per_block, BI):
acc_s[h_i, bi_i] = T.exp2(
acc_s[h_i, bi_i] * sm_scale - m_i[h_i] * sm_scale
)
T.reduce_sum(
acc_s, sumexp_i, dim=1
) # is this a accumulate operator?
for h_i in T.Parallel(H_per_block):
sumexp[h_i] = sumexp[h_i] * alpha_local[h_i] + sumexp_i[h_i]
for h_i, d_i in T.Parallel(H_per_block, D // 2):
acc_o_l[h_i, d_i] *= alpha_local[h_i]
T.copy(alpha_local, alpha_shared)
T.copy(acc_s, S_shared)
T.gemm(S_shared, KV_shared_0_l, acc_o_l)
T.barrier_arrive(bar_sScale_and_sS_ready)
T.barrier_arrive(bar_k_0_free[0])
# Buffer 1
T.barrier_wait(bar_k_1_ready[0], (i_i & 1))
T.barrier_arrive(bar_0_128)
T.barrier_wait(bar_0_128, 1)
for h_i, bi_i in T.Parallel(H_per_block, BI):
acc_s[h_i, bi_i] = T.if_then_else(
is_kv_valid_1[bi_i], 0, -T.infinity(acc_s.dtype)
)
T.gemm(Q_shared_l, KV_shared_1_l, acc_s, transpose_B=True)
T.gemm(Q_shared_r, KV_shared_1_r, acc_s, transpose_B=True)
T.gemm(
Q_tail_shared,
K_tail_shared_1,
acc_s,
transpose_B=True,
)
T.barrier_arrive(bar_sScale_and_sS_free)
T.barrier_wait(bar_sScale_and_sS_free, ((i_i * 2 + 1) & 1) ^ 1)
T.copy(m_i, m_i_prev)
T.reduce_max(acc_s, m_i, dim=1, clear=False)
for h_i in T.Parallel(H_per_block):
alpha_local[h_i] = T.exp2((m_i_prev[h_i] - m_i[h_i]) * sm_scale)
for h_i, bi_i in T.Parallel(H_per_block, BI):
acc_s[h_i, bi_i] = T.exp2(
acc_s[h_i, bi_i] * sm_scale - m_i[h_i] * sm_scale
)
T.reduce_sum(
acc_s, sumexp_i, dim=1
) # is this a accumulate operator?
for h_i in T.Parallel(H_per_block):
sumexp[h_i] = sumexp[h_i] * alpha_local[h_i] + sumexp_i[h_i]
for h_i, d_i in T.Parallel(H_per_block, D // 2):
acc_o_l[h_i, d_i] *= alpha_local[h_i]
T.copy(alpha_local, alpha_shared)
T.copy(acc_s, S_shared)
T.gemm(S_shared, KV_shared_1_l, acc_o_l)
T.barrier_arrive(bar_sScale_and_sS_ready)
T.barrier_arrive(bar_k_1_free[0])
# Rescale
for h_i in T.Parallel(H_per_block):
sum_exp_shared[h_i] = sumexp[h_i]
T.barrier_arrive(bar_final)
for h_i, d_i in T.Parallel(H_per_block, D // 2):
acc_o_l[h_i, d_i] /= sumexp[h_i]
for h_i in T.Parallel(H_per_block):
sumexp[h_i] = T.log2(sumexp[h_i]) + m_i[h_i] * sm_scale
if return_lse:
T.copy(sumexp, LSE[b_i, s_i, H0:H1])
T.copy(acc_o_l, O_shared_l)
T.copy(O_shared_l, Output[b_i, s_i, H0:H1, 0 : D // 2])
elif tx >= 128 and tx < 256:
# T.set_max_nreg(168, 1)
T.fill(acc_o_r, 0)
for i_i in T.serial(T.ceildiv(NI, 2)):
# Buffer 0
T.barrier_arrive(bar_sScale_and_sS_ready)
T.barrier_wait(bar_sScale_and_sS_ready, ((i_i * 2) & 1))
T.barrier_arrive(bar_1_128)
T.barrier_wait(bar_1_128, 0)
for h_i, d_i in T.Parallel(H_per_block, D // 2):
acc_o_r[h_i, d_i] *= alpha_shared[h_i]
T.gemm(S_shared, KV_shared_0_r, acc_o_r)
T.barrier_arrive(bar_k_0_free[0])
T.barrier_arrive(bar_sScale_and_sS_free)
# Buffer 1
T.barrier_arrive(bar_sScale_and_sS_ready)
T.barrier_wait(bar_sScale_and_sS_ready, ((i_i * 2 + 1) & 1))
T.barrier_arrive(bar_1_128)
T.barrier_wait(bar_1_128, 1)
for h_i, d_i in T.Parallel(H_per_block, D // 2):
acc_o_r[h_i, d_i] *= alpha_shared[h_i]
T.gemm(S_shared, KV_shared_1_r, acc_o_r)
T.barrier_arrive(bar_k_1_free[0])
if i_i != T.ceildiv(NI, 2) - 1:
T.barrier_arrive(bar_sScale_and_sS_free)
# Rescale
T.barrier_wait(bar_final, 0)
for h_i, d_i in T.Parallel(H_per_block, D // 2):
acc_o_r[h_i, d_i] /= sum_exp_shared[h_i]
T.copy(acc_o_r, O_shared_r)
T.copy(O_shared_r, Output[b_i, s_i, H0:H1, D // 2 : D])
elif tx >= 256:
# producer
T.set_max_nreg(80, 0)
indices_local[0] = 0
for i_i in T.serial(T.ceildiv(NI, 2)):
# Buffer 0
T.barrier_wait(bar_k_0_free[0], ((i_i & 1) ^ 1))
T.barrier_arrive(bar_2_128)
T.barrier_wait(bar_2_128, 0)
for r in T.serial(4):
indices_tmp[0] = Indices[
b_i, s_i, g_i, (i_i * 2) * BI + r * 16 + (tx - 256) // 8
]
is_kv_valid_0[r * 16 + (tx - 256) // 8] = indices_tmp[0] >= 0
if is_kv_valid_0[r * 16 + (tx - 256) // 8]:
indices_local[0] = indices_tmp[0]
with T.attr("default", "async_scope", 1): # type: ignore
for u in T.serial(4):
for v in T.vectorized(8):
KV_shared_0_l[
r * 16 + (tx - 256) // 8,
64 * u + (tx - 256) % 8 * 8 + v,
] = KV[
b_i,
indices_local[0],
g_i,
64 * u + (tx - 256) % 8 * 8 + v,
]
KV_shared_0_r[
r * 16 + (tx - 256) // 8,
64 * u + (tx - 256) % 8 * 8 + v,
] = KV[
b_i,
indices_local[0],
g_i,
D // 2 + 64 * u + (tx - 256) % 8 * 8 + v,
]
with T.attr("default", "async_scope", 1): # type: ignore
for v in T.vectorized(8):
K_tail_shared_0[
r * 16 + (tx - 256) // 8, (tx - 256) % 8 * 8 + v
] = KV[
b_i,
indices_local[0],
g_i,
D + (tx - 256) % 8 * 8 + v,
]
T.cp_async_barrier_noinc(bar_k_0_ready[0])
# Buffer 1
T.barrier_wait(bar_k_1_free[0], ((i_i & 1) ^ 1))
T.barrier_arrive(bar_2_128)
T.barrier_wait(bar_2_128, 1)
for r in T.serial(4):
indices_tmp[0] = Indices[
b_i, s_i, g_i, (i_i * 2 + 1) * BI + r * 16 + (tx - 256) // 8
]
is_kv_valid_1[r * 16 + (tx - 256) // 8] = indices_tmp[0] >= 0
if is_kv_valid_1[r * 16 + (tx - 256) // 8]:
indices_local[0] = indices_tmp[0]
with T.attr("default", "async_scope", 1): # type: ignore
for u in T.serial(4):
for v in T.vectorized(8):
KV_shared_1_l[
r * 16 + (tx - 256) // 8,
64 * u + (tx - 256) % 8 * 8 + v,
] = KV[
b_i,
indices_local[0],
g_i,
64 * u + (tx - 256) % 8 * 8 + v,
]
KV_shared_1_r[
r * 16 + (tx - 256) // 8,
64 * u + (tx - 256) % 8 * 8 + v,
] = KV[
b_i,
indices_local[0],
g_i,
D // 2 + 64 * u + (tx - 256) % 8 * 8 + v,
]
with T.attr("default", "async_scope", 1): # type: ignore
for v in T.vectorized(8):
K_tail_shared_1[
r * 16 + (tx - 256) // 8, (tx - 256) % 8 * 8 + v
] = KV[
b_i,
indices_local[0],
g_i,
D + (tx - 256) % 8 * 8 + v,
]
T.cp_async_barrier_noinc(bar_k_1_ready[0])
return main
@tilelang.jit(
out_idx=[-2, -1],
pass_configs={
tilelang.PassConfigKey.TL_DISABLE_TMA_LOWER: True,
tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True,
},
)
def sparse_mla_fwd_decode_partial(
heads,
dim,
tail_dim,
topk,
*,
kv_group=1,
sm_scale=None,
is_causal=True,
block_I=32,
inner_iter=1,
num_stages=1,
threads=128,
):
"""
grid: (seq_len * REPLICATE_H, top_k / block_I / inner_iter)
Each GPU block processes `inner_iter` consecutive KV tiles and writes one (partial_o, partial_lse) entry.
"""
assert is_causal == True, "non-causal is not supported"
assert kv_group == 1
assert topk % block_I == 0
assert topk % (block_I * inner_iter) == 0, (
f"topk ({topk}) must be divisible by block_I * inner_iter = "
f"{block_I} * {inner_iter}"
)
# log2(e) = 1.44269504
if sm_scale is None:
sm_scale = (1.0 / (dim + tail_dim)) ** 0.5 * 1.44269504
else:
sm_scale = sm_scale * 1.44269504
batch = 1
seq_len = T.dynamic("seq_len")
seq_len_kv = T.dynamic("seq_len_kv")
head_kv = heads // kv_group
padded_H = max(tilelang.math.next_power_of_2(head_kv), 16)
REPLICATE_H = (head_kv // 64) if head_kv > 64 else 1
H_per_block = padded_H if REPLICATE_H == 1 else 64
N_GROUPS = topk // (block_I * inner_iter)
BI = block_I
D = dim
D_tail = tail_dim
q_shape = [batch, seq_len, heads, dim + tail_dim]
kv_shape = [batch, seq_len_kv, kv_group, dim + tail_dim]
indices_shape = [batch, seq_len, kv_group, topk]
partial_o_shape = [batch, seq_len, N_GROUPS, heads, dim]
partial_lse_shape = [batch, seq_len, N_GROUPS, heads]
indices_dtype = T.int32
dtype = T.bfloat16
accum_dtype = T.float32
_q_in_shared = inner_iter == 1
@T.prim_func
def main(
Q: T.Tensor(q_shape, dtype),
KV: T.Tensor(kv_shape, dtype),
Indices: T.Tensor(indices_shape, indices_dtype),
Partial_O: T.Tensor(partial_o_shape, dtype),
Partial_Lse: T.Tensor(partial_lse_shape, accum_dtype),
):
with T.Kernel(seq_len * REPLICATE_H, N_GROUPS, threads=threads) as (bx, by):
if _q_in_shared:
Q_buf = T.alloc_shared([H_per_block, D], dtype)
Q_tail_buf = T.alloc_shared([H_per_block, D_tail], dtype)
else:
Q_buf = T.alloc_fragment([H_per_block, D], dtype)
Q_tail_buf = T.alloc_fragment([H_per_block, D_tail], dtype)
KV_shared = T.alloc_shared([BI, D], dtype)
K_tail_shared = T.alloc_shared([BI, D_tail], dtype)
S_shared = T.alloc_shared([H_per_block, BI], dtype)
mask = T.alloc_fragment([BI], T.bool)
acc_o = T.alloc_fragment([H_per_block, D], accum_dtype)
acc_s = T.alloc_fragment([H_per_block, BI], accum_dtype)
sumexp = T.alloc_fragment([H_per_block], accum_dtype)
sumexp_i = T.alloc_fragment([H_per_block], accum_dtype)
alpha = T.alloc_fragment([H_per_block], accum_dtype)
m_i = T.alloc_fragment([H_per_block], accum_dtype)
m_i_prev = T.alloc_fragment([H_per_block], accum_dtype)
T.fill(acc_o, 0)
T.fill(sumexp, 0)
T.fill(m_i, -(2**30))
b_i, g_i = 0, 0
s_i = bx if REPLICATE_H == 1 else (bx // REPLICATE_H)
group_i = by
H0 = 0 if REPLICATE_H == 1 else (bx % REPLICATE_H) * 64
H1 = H0 + H_per_block
T.copy(Q[b_i, s_i, H0:H1, :D], Q_buf)
T.copy(Q[b_i, s_i, H0:H1, D:], Q_tail_buf)
for k_i in T.Pipelined(inner_iter, num_stages=num_stages):
topk_block_i = group_i * inner_iter + k_i
for bi_i in T.Parallel(BI):
mask[bi_i] = Indices[b_i, s_i, g_i, topk_block_i * BI + bi_i] >= 0
for bi_i, d_i in T.Parallel(BI, D):
idx = Indices[b_i, s_i, g_i, topk_block_i * BI + bi_i]
KV_shared[bi_i, d_i] = KV[
b_i, T.if_then_else(idx >= 0, idx, 0), g_i, d_i
]
for bi_i, d_i in T.Parallel(BI, D_tail):
idx = Indices[b_i, s_i, g_i, topk_block_i * BI + bi_i]
K_tail_shared[bi_i, d_i] = KV[
b_i, T.if_then_else(idx >= 0, idx, 0), g_i, D + d_i
]
for h_i, bi_i in T.Parallel(H_per_block, BI):
acc_s[h_i, bi_i] = T.if_then_else(
mask[bi_i], 0, -T.infinity(acc_s.dtype)
)
T.gemm(
Q_buf,
KV_shared,
acc_s,
transpose_B=True,
policy=T.GemmWarpPolicy.FullCol,
)
T.gemm(
Q_tail_buf,
K_tail_shared,
acc_s,
transpose_B=True,
policy=T.GemmWarpPolicy.FullCol,
)
T.copy(m_i, m_i_prev)
T.reduce_max(acc_s, m_i, dim=1, clear=False)
for h_i in T.Parallel(H_per_block):
alpha[h_i] = T.exp2((m_i_prev[h_i] - m_i[h_i]) * sm_scale)
for h_i, bi_i in T.Parallel(H_per_block, BI):
acc_s[h_i, bi_i] = T.exp2(
acc_s[h_i, bi_i] * sm_scale - m_i[h_i] * sm_scale
)
T.reduce_sum(acc_s, sumexp_i, dim=1)
for h_i in T.Parallel(H_per_block):
sumexp[h_i] = sumexp[h_i] * alpha[h_i] + sumexp_i[h_i]
for h_i, d_i in T.Parallel(H_per_block, D):
acc_o[h_i, d_i] *= alpha[h_i]
T.copy(acc_s, S_shared)
T.gemm(S_shared, KV_shared, acc_o, policy=T.GemmWarpPolicy.FullCol)
# sumexp==0 (all masked), divide by 1 to get 0 and avoid nan
for h_i, d_i in T.Parallel(H_per_block, D):
acc_o[h_i, d_i] = acc_o[h_i, d_i] / T.if_then_else(
sumexp[h_i] == 0.0, 1.0, sumexp[h_i]
)
# sumexp==0 (all masked), use large negative so combine ignores this split
for h_i in T.Parallel(H_per_block):
sumexp[h_i] = T.if_then_else(
sumexp[h_i] == 0.0,
-(2**30),
T.log2(sumexp[h_i]) + m_i[h_i] * sm_scale,
)
T.copy(acc_o, Partial_O[b_i, s_i, group_i, H0:H1, :])
T.copy(sumexp, Partial_Lse[b_i, s_i, group_i, H0:H1])
return main
@tilelang.jit(
out_idx=[-1],
pass_configs={
tilelang.PassConfigKey.TL_DISABLE_TMA_LOWER: True,
tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True,
},
)
def sparse_mla_fwd_decode_combine(
heads,
dim,
topk,
head_per_block,
*,
block_I=32,
threads=128,
):
"""
grid: (seq_len * REPLICATE_H). batch=1, kv_group=1.
Each block does one tile of heads (e.g. 4 or 8 for decode).
"""
assert heads % head_per_block == 0, f"head_per_block must divide heads"
batch = 1
seq_len = T.dynamic("seq_len")
NI = topk // block_I
H_per_block = head_per_block
REPLICATE_H = heads // H_per_block
partial_o_shape = [batch, seq_len, NI, heads, dim]
partial_lse_shape = [batch, seq_len, NI, heads]
o_shape = [batch, seq_len, heads, dim]
dtype = T.bfloat16
accum_dtype = T.float32
@T.prim_func
def main(
Partial_O: T.Tensor(partial_o_shape, dtype),
Partial_Lse: T.Tensor(partial_lse_shape, accum_dtype),
Output: T.Tensor(o_shape, dtype),
):
with T.Kernel(seq_len * REPLICATE_H, threads=threads) as (bx,):
shared_lse = T.alloc_shared([NI, H_per_block], accum_dtype)
lse_max = T.alloc_fragment([H_per_block], accum_dtype)
lse_sum = T.alloc_fragment([H_per_block], accum_dtype)
scale = T.alloc_fragment([H_per_block, NI], accum_dtype)
acc_o = T.alloc_fragment([H_per_block, dim], accum_dtype)
b_i = 0
s_i = bx if REPLICATE_H == 1 else (bx // REPLICATE_H)
H0 = 0 if REPLICATE_H == 1 else (bx % REPLICATE_H) * H_per_block
H1 = H0 + H_per_block
for k in T.serial(NI):
T.copy(Partial_Lse[b_i, s_i, k, H0:H1], shared_lse[k, :])
T.fill(lse_max, -(2**30))
for k in T.serial(NI):
for h_i in T.Parallel(H_per_block):
lse_max[h_i] = T.max(lse_max[h_i], shared_lse[k, h_i])
T.fill(lse_sum, 0)
for k in T.serial(NI):
for h_i in T.Parallel(H_per_block):
lse_sum[h_i] = lse_sum[h_i] + T.exp2(
shared_lse[k, h_i] - lse_max[h_i]
)
for k in T.serial(NI):
for h_i in T.Parallel(H_per_block):
scale[h_i, k] = T.exp2(
shared_lse[k, h_i] - lse_max[h_i] - T.log2(lse_sum[h_i])
)
T.fill(acc_o, 0)
for k in T.serial(NI):
for h_i, d_i in T.Parallel(H_per_block, dim):
acc_o[h_i, d_i] = acc_o[h_i, d_i] + scale[h_i, k] * Partial_O[
b_i, s_i, k, H0 + h_i, d_i
].astype(accum_dtype)
T.copy(acc_o, Output[b_i, s_i, H0:H1, :])
return main
@tilelang.jit(out_idx=[-2, -1], pass_configs=pass_configs)
def sparse_mla_fwd_decode_partial_fp8(
num_heads: int,
d_v: int,
d_tail: int,
topk: int,
*,
sm_scale=None,
block_I=32,
inner_iter=1,
threads=128,
):
assert d_v == 512, f"only support d_v=512"
assert (
topk % block_I == 0
), "otherwise will load some index=0 thus causing wrong kv to be loaded"
# Softmax scores are in [0, 1]. We scale by fp8_max_val before FP8 cast
# to better utilize FP8 dynamic range, then apply the inverse scale after GEMM.
# This is numerically safe because softmax output is bounded by 1.
fp8_dtype = "float8_e4m3fnuz" if _is_fp8_fnuz else "float8_e4m3fn"
fp8_max_val = 240.0 if _is_fp8_fnuz else 448.0
s_inv_scale_const = fp8_max_val
s_scale_const = 1.0 / fp8_max_val
BI = block_I
group_size = 128
dim_quant_fp8 = d_v + d_tail
rope_offset_fp8 = d_v
n_groups = topk // (BI * inner_iter)
if sm_scale is None:
sm_scale = (1.0 / (d_v + d_tail)) ** 0.5 * 1.44269504
else:
sm_scale = sm_scale * 1.44269504
h_per_block = 16
# Match bf16 partial behavior: keep fixed 16-head tiles and use
# sliced T.copy on H0:H1 for tail handling.
assert (
num_heads <= h_per_block or num_heads % h_per_block == 0
), "num_heads must be <=16 or divisible by 16"
head_blocks_per_seq = (num_heads + h_per_block - 1) // h_per_block
batch = 1
kv_group = 1
seq_len = T.symbolic("seq_len")
num_pages = T.symbolic("num_pages")
q_fp8_shape = [batch, seq_len, num_heads, d_v + d_tail]
kv_fp8_shape = [batch, num_pages, kv_group, dim_quant_fp8]
idx_shape = [batch, seq_len, kv_group, topk]
partial_o_shape = [batch, seq_len, n_groups, num_heads, d_v]
partial_lse_shape = [batch, seq_len, n_groups, num_heads]
accum_dtype = T.float32
dtype_bf16 = T.bfloat16
@T.prim_func
def main(
q_fp8: T.Tensor(q_fp8_shape, fp8_dtype),
kv_fp8: T.Tensor(kv_fp8_shape, fp8_dtype),
indices: T.Tensor(idx_shape, T.int32),
partial_o: T.Tensor(partial_o_shape, dtype_bf16),
partial_lse: T.Tensor(partial_lse_shape, accum_dtype),
):
with T.Kernel(seq_len * head_blocks_per_seq, n_groups, threads=threads) as (
bx,
by,
):
b_i, g_i = 0, 0
s_i = bx // head_blocks_per_seq
group_i = by
H0 = (bx % head_blocks_per_seq) * h_per_block
H1 = H0 + h_per_block
# We intentionally split the K=512 GEMM into 4x128 tiles.
# Although this adds extra intermediate memory traffic,
# it shortens the MFMA accumulation dependency chain and improves performance.
q_tile0 = T.alloc_shared([h_per_block, group_size], fp8_dtype)
q_tile1 = T.alloc_shared([h_per_block, group_size], fp8_dtype)
q_tile2 = T.alloc_shared([h_per_block, group_size], fp8_dtype)
q_tile3 = T.alloc_shared([h_per_block, group_size], fp8_dtype)
kv_tile0 = T.alloc_shared([BI, group_size], fp8_dtype)
kv_tile1 = T.alloc_shared([BI, group_size], fp8_dtype)
kv_tile2 = T.alloc_shared([BI, group_size], fp8_dtype)
kv_tile3 = T.alloc_shared([BI, group_size], fp8_dtype)
q_tail_buf = T.alloc_shared([h_per_block, d_tail], fp8_dtype)
k_tail_shared = T.alloc_shared([BI, d_tail], fp8_dtype)
s_fp8_shared = T.alloc_shared([h_per_block, BI], fp8_dtype)
page_idx_shared = T.alloc_shared([BI], T.int32)
mask = T.alloc_fragment([BI], T.bool)
acc_s = T.alloc_fragment([h_per_block, BI], accum_dtype)
acc_tile = T.alloc_fragment([h_per_block, BI], accum_dtype)
sv_tile = T.alloc_fragment([h_per_block, group_size], accum_dtype)
sumexp = T.alloc_fragment([h_per_block], accum_dtype)
sumexp_i = T.alloc_fragment([h_per_block], accum_dtype)
alpha = T.alloc_fragment([h_per_block], accum_dtype)
m_i = T.alloc_fragment([h_per_block], accum_dtype)
m_i_prev = T.alloc_fragment([h_per_block], accum_dtype)
inv_denom = T.alloc_fragment([h_per_block], accum_dtype)
acc_o_tile0 = T.alloc_fragment([h_per_block, group_size], accum_dtype)
acc_o_tile1 = T.alloc_fragment([h_per_block, group_size], accum_dtype)
acc_o_tile2 = T.alloc_fragment([h_per_block, group_size], accum_dtype)
acc_o_tile3 = T.alloc_fragment([h_per_block, group_size], accum_dtype)
T.fill(acc_o_tile0, 0)
T.fill(acc_o_tile1, 0)
T.fill(acc_o_tile2, 0)
T.fill(acc_o_tile3, 0)
T.fill(sumexp, 0)
T.fill(m_i, -(2**30))
T.copy(q_fp8[b_i, s_i, H0:H1, d_v:], q_tail_buf)
T.copy(q_fp8[b_i, s_i, H0:H1, 0 * group_size : 1 * group_size], q_tile0)
T.copy(q_fp8[b_i, s_i, H0:H1, 1 * group_size : 2 * group_size], q_tile1)
T.copy(q_fp8[b_i, s_i, H0:H1, 2 * group_size : 3 * group_size], q_tile2)
T.copy(q_fp8[b_i, s_i, H0:H1, 3 * group_size : 4 * group_size], q_tile3)
for k_i in T.serial(inner_iter):
topk_block_i = group_i * inner_iter + k_i
for bi_i in T.Parallel(BI):
idx = indices[b_i, s_i, g_i, topk_block_i * BI + bi_i]
valid = idx >= 0
page_idx_shared[bi_i] = T.if_then_else(valid, idx, 0)
mask[bi_i] = valid
for bi_i, j in T.Parallel(BI, group_size):
page = page_idx_shared[bi_i]
kv_tile0[bi_i, j] = kv_fp8[b_i, page, g_i, 0 * group_size + j]
kv_tile1[bi_i, j] = kv_fp8[b_i, page, g_i, 1 * group_size + j]
kv_tile2[bi_i, j] = kv_fp8[b_i, page, g_i, 2 * group_size + j]
kv_tile3[bi_i, j] = kv_fp8[b_i, page, g_i, 3 * group_size + j]
for bi_i, j in T.Parallel(BI, d_tail):
page = page_idx_shared[bi_i]
k_tail_shared[bi_i, j] = kv_fp8[b_i, page, g_i, rope_offset_fp8 + j]
for h_i, bi_i in T.Parallel(h_per_block, BI):
acc_s[h_i, bi_i] = T.if_then_else(
mask[bi_i], 0, -T.infinity(acc_s.dtype)
)
T.gemm(q_tile0, kv_tile0, acc_s, transpose_B=True, clear_accum=False)
T.gemm(q_tile1, kv_tile1, acc_tile, transpose_B=True, clear_accum=True)
for h_i, bi_i in T.Parallel(h_per_block, BI):
acc_s[h_i, bi_i] += acc_tile[h_i, bi_i]
T.gemm(q_tile2, kv_tile2, acc_tile, transpose_B=True, clear_accum=True)
for h_i, bi_i in T.Parallel(h_per_block, BI):
acc_s[h_i, bi_i] += acc_tile[h_i, bi_i]
T.gemm(q_tile3, kv_tile3, acc_tile, transpose_B=True, clear_accum=True)
for h_i, bi_i in T.Parallel(h_per_block, BI):
acc_s[h_i, bi_i] += acc_tile[h_i, bi_i]
T.gemm(
q_tail_buf,
k_tail_shared,
acc_s,
transpose_B=True,
policy=T.GemmWarpPolicy.FullCol,
)
T.copy(m_i, m_i_prev)
T.reduce_max(acc_s, m_i, dim=1, clear=False)
for h_i in T.Parallel(h_per_block):
alpha[h_i] = T.exp2((m_i_prev[h_i] - m_i[h_i]) * sm_scale)
for h_i, bi_i in T.Parallel(h_per_block, BI):
acc_s[h_i, bi_i] = T.exp2(
acc_s[h_i, bi_i] * sm_scale - m_i[h_i] * sm_scale
)
T.reduce_sum(acc_s, sumexp_i, dim=1)
for h_i in T.Parallel(h_per_block):
sumexp[h_i] = sumexp[h_i] * alpha[h_i] + sumexp_i[h_i]
for h_i, j in T.Parallel(h_per_block, group_size):
acc_o_tile0[h_i, j] = acc_o_tile0[h_i, j] * alpha[h_i]
acc_o_tile1[h_i, j] = acc_o_tile1[h_i, j] * alpha[h_i]
acc_o_tile2[h_i, j] = acc_o_tile2[h_i, j] * alpha[h_i]
acc_o_tile3[h_i, j] = acc_o_tile3[h_i, j] * alpha[h_i]
for h_i, bi_i in T.Parallel(h_per_block, BI):
s_fp8_shared[h_i, bi_i] = T.clamp(
acc_s[h_i, bi_i] * s_inv_scale_const,
-fp8_max_val,
fp8_max_val,
)
T.gemm(s_fp8_shared, kv_tile0, sv_tile, clear_accum=True)
for h_i, j in T.Parallel(h_per_block, group_size):
acc_o_tile0[h_i, j] = (
acc_o_tile0[h_i, j] + sv_tile[h_i, j] * s_scale_const
)
T.gemm(s_fp8_shared, kv_tile1, sv_tile, clear_accum=True)
for h_i, j in T.Parallel(h_per_block, group_size):
acc_o_tile1[h_i, j] = (
acc_o_tile1[h_i, j] + sv_tile[h_i, j] * s_scale_const
)
T.gemm(s_fp8_shared, kv_tile2, sv_tile, clear_accum=True)
for h_i, j in T.Parallel(h_per_block, group_size):
acc_o_tile2[h_i, j] = (
acc_o_tile2[h_i, j] + sv_tile[h_i, j] * s_scale_const
)
T.gemm(s_fp8_shared, kv_tile3, sv_tile, clear_accum=True)
for h_i, j in T.Parallel(h_per_block, group_size):
acc_o_tile3[h_i, j] = (
acc_o_tile3[h_i, j] + sv_tile[h_i, j] * s_scale_const
)
for h_i in T.Parallel(h_per_block):
denom = T.if_then_else(sumexp[h_i] == 0.0, 1.0, sumexp[h_i])
inv_denom[h_i] = 1.0 / denom
for h_i, j in T.Parallel(h_per_block, group_size):
acc_o_tile0[h_i, j] = acc_o_tile0[h_i, j] * inv_denom[h_i]
acc_o_tile1[h_i, j] = acc_o_tile1[h_i, j] * inv_denom[h_i]
acc_o_tile2[h_i, j] = acc_o_tile2[h_i, j] * inv_denom[h_i]
acc_o_tile3[h_i, j] = acc_o_tile3[h_i, j] * inv_denom[h_i]
for h_i in T.Parallel(h_per_block):
sumexp[h_i] = T.if_then_else(
sumexp[h_i] == 0.0,
-(2**30),
T.log2(sumexp[h_i]) + m_i[h_i] * sm_scale,
)
T.copy(
acc_o_tile0,
partial_o[b_i, s_i, group_i, H0:H1, 0 * group_size : 1 * group_size],
)
T.copy(
acc_o_tile1,
partial_o[b_i, s_i, group_i, H0:H1, 1 * group_size : 2 * group_size],
)
T.copy(
acc_o_tile2,
partial_o[b_i, s_i, group_i, H0:H1, 2 * group_size : 3 * group_size],
)
T.copy(
acc_o_tile3,
partial_o[b_i, s_i, group_i, H0:H1, 3 * group_size : 4 * group_size],
)
T.copy(sumexp, partial_lse[b_i, s_i, group_i, H0:H1])
return main
def tilelang_sparse_fwd(
q: torch.Tensor,
kv: torch.Tensor,
indices: torch.Tensor,
sm_scale: float,
d_v: int = 512,
return_lse: bool = False,
) -> torch.Tensor:
assert q.dim() == 3 and kv.dim() == 3 and indices.dim() == 3
num_heads = q.shape[1]
dim = q.shape[2]
tail_dim = dim - d_v
topk = indices.shape[-1]
assert topk % 64 == 0, "topk must be padded to a multiple of 64"
if _is_hip:
assert not return_lse, "tilelang HIP sparse fwd does not return LSE"
is_fp8_kv = kv.dtype in (torch.float8_e4m3fn, torch.float8_e4m3fnuz)
if is_fp8_kv:
if q.dtype != kv.dtype:
q = q.to(kv.dtype)
if _is_gfx95_supported:
block_I, threads, block_per_cu, cu = 64, 256, 2, 256
else:
block_I, threads, block_per_cu, cu = 64, 256, 1, 304
ni = topk // block_I
inner_iter = _pick_inner_iter(q.shape[0], ni, cu, block_per_cu)
kernel_partial = sparse_mla_fwd_decode_partial_fp8(
num_heads,
d_v,
tail_dim,
topk,
sm_scale=sm_scale,
block_I=block_I,
inner_iter=inner_iter,
threads=threads,
)
else:
if _is_gfx95_supported:
block_I, threads, block_per_cu, cu = 64, 256, 2, 256
else:
block_I, threads, block_per_cu, cu = 32, 128, 1, 304
ni = topk // block_I
inner_iter = _pick_inner_iter(q.shape[0], ni, cu, block_per_cu)
kernel_partial = sparse_mla_fwd_decode_partial(
num_heads,
d_v,
tail_dim,
topk,
sm_scale=sm_scale,
block_I=block_I,
inner_iter=inner_iter,
threads=threads,
)
partial_o_batched, partial_lse_batched = kernel_partial(
q.unsqueeze(0), kv.unsqueeze(0), indices.unsqueeze(0)
)
n_groups = ni // inner_iter
kernel_combine = sparse_mla_fwd_decode_combine(
num_heads,
d_v,
n_groups * block_I,
head_per_block=4,
block_I=block_I,
threads=threads,
)
out = kernel_combine(partial_o_batched, partial_lse_batched)
else:
kernel_factory = (
sparse_attention_fwd_kernel_v1
if tail_dim == 0
else sparse_attention_fwd_kernel_v2
)
kernel = kernel_factory(
num_heads, d_v, tail_dim, topk, sm_scale=sm_scale, return_lse=return_lse
)
# Caller-allocated LSE (in-place kernel arg): written only by kernels
# traced with return_lse=True, but the prim_func signature always has it.
lse = torch.empty(
(1, q.shape[0], num_heads), dtype=torch.float32, device=q.device
)
out = kernel(q.unsqueeze(0), kv.unsqueeze(0), indices.unsqueeze(0), lse) # type: ignore
if return_lse:
return out, lse
return out
@functools.cache
def fp8_paged_mqa_logits_kernel(
head_dim: int = 128,
num_heads: int = 64,
block_size: int = 64,
clear_accum: bool = True,
split_kv: int = 1,
) -> Any:
N = T.symbolic("batch_size")
L = T.symbolic("max_table_length")
S = T.symbolic("max_seq_len")
C = T.symbolic("num_blocks")
B = block_size
D = head_dim
H = num_heads
SK = int(split_kv)
BLOCK_BYTES = B * (D + 4)
SCALE_OFFSET = B * D
assert D % 4 == 0
assert H % 4 == 0
assert D == 128
assert SK >= 1
@tilelang.jit(
pass_configs={
**pass_configs,
tilelang.PassConfigKey.TL_DISABLE_SAFE_MEMORY_ACCESS: True,
}
)
def fp8_paged_mqa_logits(
q: T.Tensor[(N, H, D), FP8],
kvcache_u8: T.Tensor[(C, BLOCK_BYTES), UINT8],
weight: T.Tensor[(N, H), FP32],
seq_lens: T.Tensor[(N,), INT32],
page_table: T.Tensor[(N, L), INT32],
o: T.Tensor[(N, S), FP32],
) -> None:
_ = N, L, S, C, D, H, B
with T.Kernel(N * SK) as bxs:
bx = bxs % N
pid_split = bxs // N
seq_len = seq_lens[bx]
np_total = T.ceildiv(seq_len, B)
stride = T.ceildiv(np_total, SK)
i_start = pid_split * stride
n_iters = T.max(0, T.min(stride, np_total - i_start))
q_smem = T.alloc_shared((H, D), FP8)
q_s_frag = T.alloc_fragment((H,), FP32)
T.copy(q[bx, 0, 0], q_smem)
T.copy(weight[bx, 0], q_s_frag)
for j in T.Pipelined(n_iters, num_stages=2):
i = i_start + j
page = page_table[bx, i]
k_smem_u8 = T.alloc_shared((1, B * D), UINT8)
T.copy(kvcache_u8[page : page + 1, 0:SCALE_OFFSET], k_smem_u8)
k_smem = T.view(k_smem_u8, (B, D), FP8)
k_s_smem_u8 = T.alloc_shared((1, B * 4), UINT8)
T.copy(
kvcache_u8[page : page + 1, SCALE_OFFSET:BLOCK_BYTES],
k_s_smem_u8,
)
k_s_smem = T.view(k_s_smem_u8, (B,), FP32)
k_s_frag = T.alloc_fragment((B,), FP32)
T.copy(k_s_smem, k_s_frag)
logits = T.alloc_fragment((B, H), FP32)
if not clear_accum:
T.fill(logits, 0.0)
T.gemm(
k_smem,
q_smem,
logits,
transpose_A=False,
transpose_B=True,
clear_accum=clear_accum,
)
# post processing
for h, j2 in T.Parallel(H, B):
logits[j2, h] = T.max(logits[j2, h], 0.0) * q_s_frag[h]
logits_sum = T.alloc_fragment((B,), FP32)
T.reduce_sum(logits, logits_sum, dim=1)
for j2 in T.Parallel(B):
logits_sum[j2] *= k_s_frag[j2]
T.copy(logits_sum, o[bx, i * B])
return fp8_paged_mqa_logits
def tilelang_fp8_paged_mqa_logits(
q_fp8: torch.Tensor,
kvcache_fp8: torch.Tensor,
weight: torch.Tensor,
seq_lens: torch.Tensor,
page_table: torch.Tensor,
deep_gemm_metadata: Any,
max_seq_len: int,
clean_logits: bool = True,
) -> torch.Tensor:
_ = deep_gemm_metadata
batch_size, _, num_heads, head_dim = q_fp8.shape
block_size = kvcache_fp8.shape[1]
assert head_dim == 128, "TODO"
assert block_size == 64, "TODO"
assert q_fp8.shape == (batch_size, 1, num_heads, head_dim)
assert kvcache_fp8.shape[1:] == (block_size, 1, head_dim + 4)
assert weight.shape == (batch_size, num_heads)
assert seq_lens.shape == (batch_size,)
assert page_table.shape[0] == batch_size
assert clean_logits == False
logits = page_table.new_empty((batch_size, max_seq_len), dtype=torch.float32)
NUM_CU = 256
split_kv = split_kv = max(1, min(max_seq_len // block_size, NUM_CU // batch_size))
kernel = fp8_paged_mqa_logits_kernel(
head_dim=head_dim,
num_heads=num_heads,
block_size=block_size,
clear_accum=clean_logits,
split_kv=split_kv,
)
q_fp8 = q_fp8.view(batch_size, num_heads, head_dim)
kvcache_u8 = kvcache_fp8.view(-1, block_size * (head_dim + 4))
kernel(q_fp8, kvcache_u8, weight, seq_lens, page_table, logits)
return logits
def _build_fp8_combined_view(k_cache: torch.Tensor) -> Tuple[torch.Tensor, int, int]:
"""
Reinterpret a MODEL1_FP8Sparse KV cache as a contiguous uint32 view.
Input: k_cache (num_blocks, block_size, 1, d_qk) fp8/uint8
— per-block storage also holds scales + padding past d_qk.
Output: (num_blocks, block_pad_u32) uint32 covering the full block
stride. Same storage ashe input, no copy.
"""
k_u8 = k_cache.view(torch.uint8) if k_cache.dtype != torch.uint8 else k_cache
num_blocks = k_u8.shape[0]
block_size = k_u8.shape[1]
block_pad_u32 = k_u8.stride(0) // 4
storage = k_u8.untyped_storage()
flat_u32 = torch.empty(0, dtype=torch.uint32, device=k_u8.device).set_(
storage, 0, (storage.nbytes() // 4,), (1,)
)
k_combined = torch.as_strided(
flat_u32,
size=(num_blocks, block_pad_u32),
stride=(block_pad_u32, 1),
storage_offset=k_u8.storage_offset() // 4,
)
return k_combined, num_blocks, block_size
_TOPK_LEN_SENTINEL_CACHE: dict = {}
_INT32_MAX = 2**30
def _topk_length_sentinel(device: torch.device, batch: int) -> torch.Tensor:
"""Cached `(batch,) int32 INT_MAX` tensor used when `topk_length` is None."""
cur = _TOPK_LEN_SENTINEL_CACHE.get(device)
if cur is None or cur.numel() < batch:
cur = torch.full(
(max(batch, 256),), _INT32_MAX, dtype=torch.int32, device=device
)
_TOPK_LEN_SENTINEL_CACHE[device] = cur
return cur[:batch]
@tilelang.jit(
out_idx=[-2, -1],
pass_configs={
tilelang.PassConfigKey.TL_DISABLE_TMA_LOWER: True,
tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True,
tilelang.PassConfigKey.TL_ENABLE_FAST_MATH: True,
},
)
def dpsk_v4_fp8_partial_kernel(
num_heads: int,
topk_1: int,
block_size_kv_1: int,
topk_2: int = 0,
block_size_kv_2: int = 0,
*,
dim: int = 448,
tail_dim: int = 64,
sm_scale: float = 0.0,
block_I: int = 32,
inner_iter_1: int = 1,
inner_iter_2: int = 0,
num_stages: int = 0,
threads: int = 512,
) -> Any:
"""
Read FP8 K cache directly, dequantise to BF16 in-kernel, do flash-attn
online softmax with split-K. Supports a second cache (`topk_2>0`) and
`attn_sink` is folded later by the combine kernel.
"""
log2e: float = 1.44269504
if sm_scale <= 0.0:
sm_scale = (1.0 / (dim + tail_dim)) ** 0.5 * log2e
else:
sm_scale = sm_scale * log2e
assert dim == 448 and tail_dim == 64
assert topk_1 % block_I == 0
assert (
topk_1 // block_I
) % inner_iter_1 == 0, (
f"NI_1={topk_1 // block_I} must be divisible by inner_iter_1={inner_iter_1}"
)
assert block_size_kv_1 > 0 and (block_size_kv_1 & (block_size_kv_1 - 1)) == 0
is_dual = topk_2 > 0
if is_dual:
assert inner_iter_2 > 0, "dual-cache call requires inner_iter_2 > 0"
assert topk_2 % block_I == 0
assert (
topk_2 // block_I
) % inner_iter_2 == 0, (
f"NI_2={topk_2 // block_I} must be divisible by inner_iter_2={inner_iter_2}"
)
assert block_size_kv_2 > 0 and (block_size_kv_2 & (block_size_kv_2 - 1)) == 0
PACKED_W = dim + 2 * tail_dim
NOPE_TILE = 64
NUM_TILES = dim // NOPE_TILE
SCALE_W = 8
PACKED_W4 = PACKED_W // 4
SCALE_W4 = SCALE_W // 4
kv_group = 1
batch = T.symbolic("batch")
seq_len = T.symbolic("seq_len")
num_blocks_kv_1 = T.symbolic("num_blocks_kv_1")
block_pad_u32_1 = T.symbolic("block_pad_u32_1")
if is_dual:
num_blocks_kv_2 = T.symbolic("num_blocks_kv_2")
block_pad_u32_2 = T.symbolic("block_pad_u32_2")
head_kv = num_heads // kv_group
D = dim
D_tail = tail_dim
BI = block_I
padded_H = max(tilelang.math.next_power_of_2(head_kv), 16)
if head_kv > 64:
assert head_kv % 64 == 0
REPLICATE_H = (head_kv + 63) // 64 if head_kv > 64 else 1
H_per_block = 64 if REPLICATE_H > 1 else padded_H
NI_1 = topk_1 // BI
n_groups_1 = NI_1 // inner_iter_1
NI_2 = (topk_2 // BI) if is_dual else 0
n_groups_2 = (NI_2 // inner_iter_2) if is_dual else 0
n_groups = n_groups_1 + n_groups_2
BS_KV_1 = block_size_kv_1
NOPE_ROPE_U32_PER_BLOCK_1 = BS_KV_1 * PACKED_W4
if is_dual:
BS_KV_2 = block_size_kv_2
NOPE_ROPE_U32_PER_BLOCK_2 = BS_KV_2 * PACKED_W4
q_shape = [batch, seq_len, num_heads, D + D_tail]
k1_shape = [num_blocks_kv_1, block_pad_u32_1]
indices1_shape = [batch, seq_len, topk_1]
topk_length_shape = [batch]
partial_o_shape = [batch, seq_len, n_groups, num_heads, D + D_tail]
partial_lse_shape = [batch, seq_len, n_groups, num_heads]
if is_dual:
k2_shape = [num_blocks_kv_2, block_pad_u32_2]
indices2_shape = [batch, seq_len, topk_2]
accum_dtype = "float"
indices_dtype = INT32
if is_dual:
@T.prim_func
def main(
Q: T.Tensor(q_shape, BF16), # type: ignore
K_combined_1: T.Tensor(k1_shape, "uint32"), # type: ignore
Indices_1: T.Tensor(indices1_shape, indices_dtype), # type: ignore
Topk_length_1: T.Tensor(topk_length_shape, indices_dtype), # type: ignore
K_combined_2: T.Tensor(k2_shape, "uint32"), # type: ignore
Indices_2: T.Tensor(indices2_shape, indices_dtype), # type: ignore
Topk_length_2: T.Tensor(topk_length_shape, indices_dtype), # type: ignore
Partial_O: T.Tensor(partial_o_shape, BF16), # type: ignore
Partial_LSE: T.Tensor(partial_lse_shape, accum_dtype), # type: ignore
) -> None:
"""
grid: (seq_len * REPLICATE_H * n_groups, batch, 1)
Each block processes `inner_iter_1` (or `inner_iter_2`) consecutive
KV tiles of one phase and writes one (partial_o, partial_lse) entry.
"""
with T.Kernel(
seq_len * REPLICATE_H * n_groups, batch, kv_group, threads=threads
) as (bx, by, bz):
Q_shared = T.alloc_fragment([H_per_block, D], BF16)
Q_tail_shared = T.alloc_fragment([H_per_block, D_tail], BF16)
K_packed_shared = T.alloc_shared([BI, PACKED_W4], "uint32")
K_scale_shared = T.alloc_shared([BI, SCALE_W4], "uint32")
KV_shared = T.alloc_shared([BI, D], BF16)
K_tail_shared = T.alloc_shared([BI, D_tail], BF16)
S_shared = T.alloc_shared([H_per_block, BI], BF16)
page_idx_shared = T.alloc_shared([BI], INT32)
mask = T.alloc_fragment([BI], "bool")
scale_byte_local = T.alloc_fragment([BI, NUM_TILES], "uint32")
acc_o = T.alloc_fragment([H_per_block, D], accum_dtype)
acc_o_tail = T.alloc_fragment([H_per_block, D_tail], accum_dtype)
acc_s = T.alloc_fragment([H_per_block, BI], accum_dtype)
sumexp = T.alloc_fragment([H_per_block], accum_dtype)
sumexp_i = T.alloc_fragment([H_per_block], accum_dtype)
alpha = T.alloc_fragment([H_per_block], accum_dtype)
m_i = T.alloc_fragment([H_per_block], accum_dtype)
m_i_prev = T.alloc_fragment([H_per_block], accum_dtype)
T.fill(acc_o, 0)
T.fill(acc_o_tail, 0)
T.fill(sumexp, 0)
T.fill(m_i, -(2**30))
b_i, g_i = by, bz
# bx encodes (s_i, h_replicate, group_i).
spans_per_seq = REPLICATE_H * n_groups
s_i = bx // spans_per_seq
rest = bx % spans_per_seq
group_i = rest // REPLICATE_H
h_rep = rest % REPLICATE_H
H0 = g_i * padded_H + (0 if REPLICATE_H == 1 else h_rep * 64)
H1 = H0 + H_per_block
tk_len_1 = Topk_length_1[b_i]
tk_len_2 = Topk_length_2[b_i]
actual_n_groups_1 = T.ceildiv(tk_len_1, BI * inner_iter_1)
actual_n_groups_2 = T.ceildiv(tk_len_2, BI * inner_iter_2)
if (group_i < n_groups_1) & (group_i < actual_n_groups_1):
# Phase 1 active: SWA cache work + Partial_O write.
T.copy(Q[b_i, s_i, H0:H1, :D], Q_shared)
T.copy(Q[b_i, s_i, H0:H1, D : D + D_tail], Q_tail_shared)
for k_i in T.Pipelined(inner_iter_1, num_stages=num_stages):
iter_i = group_i * inner_iter_1 + k_i
for bi_i in T.Parallel(BI):
pos = iter_i * BI + bi_i
idx = Indices_1[b_i, s_i, pos]
valid = (idx >= 0) & (pos < tk_len_1)
page_idx_shared[bi_i] = T.if_then_else(valid, idx, 0)
mask[bi_i] = valid
for bi_i, w_i in T.Parallel(BI, PACKED_W4):
page = page_idx_shared[bi_i]
block_id = page // BS_KV_1
t_in_block = page % BS_KV_1
K_packed_shared[bi_i, w_i] = K_combined_1[
block_id, t_in_block * PACKED_W4 + w_i
]
for bi_i, w_i in T.Parallel(BI, SCALE_W4):
page = page_idx_shared[bi_i]
block_id = page // BS_KV_1
t_in_block = page % BS_KV_1
K_scale_shared[bi_i, w_i] = K_combined_1[
block_id,
NOPE_ROPE_U32_PER_BLOCK_1 + t_in_block * SCALE_W4 + w_i,
]
for bi_i, ti in T.Parallel(BI, NUM_TILES):
word_idx = ti // 4
byte_in_word = ti % 4
word = K_scale_shared[bi_i, word_idx]
scale_byte_local[bi_i, ti] = (
word >> T.Cast("uint32", byte_in_word * 8)
) & T.uint32(0xFF)
for bi_i, d_i in T.Parallel(BI, D):
word_idx = d_i // 4
byte_in_word = d_i % 4
word = K_packed_shared[bi_i, word_idx]
b_u32 = (
word >> T.Cast("uint32", byte_in_word * 8)
) & T.uint32(0xFF)
sign_bf = (b_u32 & T.uint32(0x80)) * T.uint32(0x100)
exp_e4 = (b_u32 & T.uint32(0x78)) >> T.uint32(3)
mant_bf = (b_u32 & T.uint32(0x7)) * T.uint32(0x10)
scale_byte = scale_byte_local[bi_i, d_i // NOPE_TILE]
exp_combined = exp_e4 + scale_byte - T.uint32(7)
bf16_bits = (
sign_bf | (exp_combined << T.uint32(7)) | mant_bf
)
KV_shared[bi_i, d_i] = T.reinterpret(
BF16, T.Cast("uint16", bf16_bits)
)
for bi_i, j in T.Parallel(BI, D_tail):
abs_off = D + 2 * j
word_idx = abs_off // 4
word_off = abs_off % 4
word = K_packed_shared[bi_i, word_idx]
half_u32 = T.if_then_else(
word_off == 0,
word & T.uint32(0xFFFF),
(word >> T.uint32(16)) & T.uint32(0xFFFF),
)
K_tail_shared[bi_i, j] = T.reinterpret(
BF16, T.Cast("uint16", half_u32)
)
for h_i, bi_i in T.Parallel(H_per_block, BI):
acc_s[h_i, bi_i] = T.if_then_else(
mask[bi_i], 0, -T.infinity(acc_s.dtype)
)
T.gemm(
Q_shared,
KV_shared,
acc_s,
transpose_B=True,
policy=T.GemmWarpPolicy.FullRow,
)
T.gemm(
Q_tail_shared,
K_tail_shared,
acc_s,
transpose_B=True,
policy=T.GemmWarpPolicy.FullRow,
)
T.copy(m_i, m_i_prev)
T.reduce_max(acc_s, m_i, dim=1, clear=False)
for h_i in T.Parallel(H_per_block):
m_i[h_i] = T.max(m_i[h_i], m_i_prev[h_i])
for h_i in T.Parallel(H_per_block):
alpha[h_i] = T.exp2((m_i_prev[h_i] - m_i[h_i]) * sm_scale)
for h_i, bi_i in T.Parallel(H_per_block, BI):
acc_s[h_i, bi_i] = T.exp2(
acc_s[h_i, bi_i] * sm_scale - m_i[h_i] * sm_scale
)
T.reduce_sum(acc_s, sumexp_i, dim=1)
for h_i in T.Parallel(H_per_block):
sumexp[h_i] = sumexp[h_i] * alpha[h_i] + sumexp_i[h_i]
for h_i, d_i in T.Parallel(H_per_block, D):
acc_o[h_i, d_i] *= alpha[h_i]
for h_i, d_i in T.Parallel(H_per_block, D_tail):
acc_o_tail[h_i, d_i] *= alpha[h_i]
T.copy(acc_s, S_shared)
T.gemm(
S_shared,
KV_shared,
acc_o,
policy=T.GemmWarpPolicy.FullRow,
)
T.gemm(
S_shared,
K_tail_shared,
acc_o_tail,
policy=T.GemmWarpPolicy.FullRow,
)
# ---- finalize phase 1 (active) ----
for h_i, d_i in T.Parallel(H_per_block, D):
acc_o[h_i, d_i] = acc_o[h_i, d_i] / T.if_then_else(
sumexp[h_i] == 0.0, 1.0, sumexp[h_i]
)
for h_i, d_i in T.Parallel(H_per_block, D_tail):
acc_o_tail[h_i, d_i] = acc_o_tail[h_i, d_i] / T.if_then_else(
sumexp[h_i] == 0.0, 1.0, sumexp[h_i]
)
for h_i in T.Parallel(H_per_block):
m_i[h_i] = T.if_then_else(
sumexp[h_i] == 0.0,
-(2.0**30),
T.log2(sumexp[h_i]) + m_i[h_i] * sm_scale,
)
T.copy(acc_o, Partial_O[b_i, s_i, group_i, H0:H1, :D])
T.copy(
acc_o_tail,
Partial_O[b_i, s_i, group_i, H0:H1, D : D + D_tail],
)
T.copy(m_i, Partial_LSE[b_i, s_i, group_i, H0:H1])
elif group_i < n_groups_1:
# Phase 1 skipped: m_i is still the -2^30
T.copy(m_i, Partial_LSE[b_i, s_i, group_i, H0:H1])
elif (group_i - n_groups_1) < actual_n_groups_2:
# Phase 2 active: c128 cache work + Partial_O write.
T.copy(Q[b_i, s_i, H0:H1, :D], Q_shared)
T.copy(Q[b_i, s_i, H0:H1, D : D + D_tail], Q_tail_shared)
for k_i in T.Pipelined(inner_iter_2, num_stages=num_stages):
iter_i = (group_i - n_groups_1) * inner_iter_2 + k_i
for bi_i in T.Parallel(BI):
pos = iter_i * BI + bi_i
idx = Indices_2[b_i, s_i, pos]
valid = (idx >= 0) & (pos < tk_len_2)
page_idx_shared[bi_i] = T.if_then_else(valid, idx, 0)
mask[bi_i] = valid
for bi_i, w_i in T.Parallel(BI, PACKED_W4):
page = page_idx_shared[bi_i]
block_id = page // BS_KV_2
t_in_block = page % BS_KV_2
K_packed_shared[bi_i, w_i] = K_combined_2[
block_id, t_in_block * PACKED_W4 + w_i
]
for bi_i, w_i in T.Parallel(BI, SCALE_W4):
page = page_idx_shared[bi_i]
block_id = page // BS_KV_2
t_in_block = page % BS_KV_2
K_scale_shared[bi_i, w_i] = K_combined_2[
block_id,
NOPE_ROPE_U32_PER_BLOCK_2 + t_in_block * SCALE_W4 + w_i,
]
for bi_i, ti in T.Parallel(BI, NUM_TILES):
word_idx = ti // 4
byte_in_word = ti % 4
word = K_scale_shared[bi_i, word_idx]
scale_byte_local[bi_i, ti] = (
word >> T.Cast("uint32", byte_in_word * 8)
) & T.uint32(0xFF)
for bi_i, d_i in T.Parallel(BI, D):
word_idx = d_i // 4
byte_in_word = d_i % 4
word = K_packed_shared[bi_i, word_idx]
b_u32 = (
word >> T.Cast("uint32", byte_in_word * 8)
) & T.uint32(0xFF)
sign_bf = (b_u32 & T.uint32(0x80)) * T.uint32(0x100)
exp_e4 = (b_u32 & T.uint32(0x78)) >> T.uint32(3)
mant_bf = (b_u32 & T.uint32(0x7)) * T.uint32(0x10)
scale_byte = scale_byte_local[bi_i, d_i // NOPE_TILE]
exp_combined = exp_e4 + scale_byte - T.uint32(7)
bf16_bits = (
sign_bf | (exp_combined << T.uint32(7)) | mant_bf
)
KV_shared[bi_i, d_i] = T.reinterpret(
BF16, T.Cast("uint16", bf16_bits)
)
for bi_i, j in T.Parallel(BI, D_tail):
abs_off = D + 2 * j
word_idx = abs_off // 4
word_off = abs_off % 4
word = K_packed_shared[bi_i, word_idx]
half_u32 = T.if_then_else(
word_off == 0,
word & T.uint32(0xFFFF),
(word >> T.uint32(16)) & T.uint32(0xFFFF),
)
K_tail_shared[bi_i, j] = T.reinterpret(
BF16, T.Cast("uint16", half_u32)
)
for h_i, bi_i in T.Parallel(H_per_block, BI):
acc_s[h_i, bi_i] = T.if_then_else(
mask[bi_i], 0, -T.infinity(acc_s.dtype)
)
T.gemm(
Q_shared,
KV_shared,
acc_s,
transpose_B=True,
policy=T.GemmWarpPolicy.FullRow,
)
T.gemm(
Q_tail_shared,
K_tail_shared,
acc_s,
transpose_B=True,
policy=T.GemmWarpPolicy.FullRow,
)
T.copy(m_i, m_i_prev)
T.reduce_max(acc_s, m_i, dim=1, clear=False)
for h_i in T.Parallel(H_per_block):
m_i[h_i] = T.max(m_i[h_i], m_i_prev[h_i])
for h_i in T.Parallel(H_per_block):
alpha[h_i] = T.exp2((m_i_prev[h_i] - m_i[h_i]) * sm_scale)
for h_i, bi_i in T.Parallel(H_per_block, BI):
acc_s[h_i, bi_i] = T.exp2(
acc_s[h_i, bi_i] * sm_scale - m_i[h_i] * sm_scale
)
T.reduce_sum(acc_s, sumexp_i, dim=1)
for h_i in T.Parallel(H_per_block):
sumexp[h_i] = sumexp[h_i] * alpha[h_i] + sumexp_i[h_i]
for h_i, d_i in T.Parallel(H_per_block, D):
acc_o[h_i, d_i] *= alpha[h_i]
for h_i, d_i in T.Parallel(H_per_block, D_tail):
acc_o_tail[h_i, d_i] *= alpha[h_i]
T.copy(acc_s, S_shared)
T.gemm(
S_shared,
KV_shared,
acc_o,
policy=T.GemmWarpPolicy.FullRow,
)
T.gemm(
S_shared,
K_tail_shared,
acc_o_tail,
policy=T.GemmWarpPolicy.FullRow,
)
# ---- finalize phase 2 (active) ----
for h_i, d_i in T.Parallel(H_per_block, D):
acc_o[h_i, d_i] = acc_o[h_i, d_i] / T.if_then_else(
sumexp[h_i] == 0.0, 1.0, sumexp[h_i]
)
for h_i, d_i in T.Parallel(H_per_block, D_tail):
acc_o_tail[h_i, d_i] = acc_o_tail[h_i, d_i] / T.if_then_else(
sumexp[h_i] == 0.0, 1.0, sumexp[h_i]
)
for h_i in T.Parallel(H_per_block):
m_i[h_i] = T.if_then_else(
sumexp[h_i] == 0.0,
-(2.0**30),
T.log2(sumexp[h_i]) + m_i[h_i] * sm_scale,
)
T.copy(acc_o, Partial_O[b_i, s_i, group_i, H0:H1, :D])
T.copy(
acc_o_tail,
Partial_O[b_i, s_i, group_i, H0:H1, D : D + D_tail],
)
T.copy(m_i, Partial_LSE[b_i, s_i, group_i, H0:H1])
else:
# Phase 2 skipped: m_i is still the -2^30
T.copy(m_i, Partial_LSE[b_i, s_i, group_i, H0:H1])
return main
@T.prim_func
def main(
Q: T.Tensor(q_shape, BF16), # type: ignore
K_combined_1: T.Tensor(k1_shape, "uint32"), # type: ignore
Indices_1: T.Tensor(indices1_shape, indices_dtype), # type: ignore
Topk_length_1: T.Tensor(topk_length_shape, indices_dtype), # type: ignore
Partial_O: T.Tensor(partial_o_shape, BF16), # type: ignore
Partial_LSE: T.Tensor(partial_lse_shape, accum_dtype), # type: ignore
) -> None:
"""
grid: (seq_len * REPLICATE_H * n_groups, batch, 1)
Each block processes `inner_iter_1` consecutive KV tiles and writes
one (partial_o, partial_lse) entry.
"""
with T.Kernel(
seq_len * REPLICATE_H * n_groups, batch, kv_group, threads=threads
) as (bx, by, bz):
Q_shared = T.alloc_fragment([H_per_block, D], BF16)
Q_tail_shared = T.alloc_fragment([H_per_block, D_tail], BF16)
K_packed_shared = T.alloc_shared([BI, PACKED_W4], "uint32")
K_scale_shared = T.alloc_shared([BI, SCALE_W4], "uint32")
KV_shared = T.alloc_shared([BI, D], BF16)
K_tail_shared = T.alloc_shared([BI, D_tail], BF16)
S_shared = T.alloc_shared([H_per_block, BI], BF16)
page_idx_shared = T.alloc_shared([BI], INT32)
mask = T.alloc_fragment([BI], "bool")
scale_byte_local = T.alloc_fragment([BI, NUM_TILES], "uint32")
acc_o = T.alloc_fragment([H_per_block, D], accum_dtype)
acc_o_tail = T.alloc_fragment([H_per_block, D_tail], accum_dtype)
acc_s = T.alloc_fragment([H_per_block, BI], accum_dtype)
sumexp = T.alloc_fragment([H_per_block], accum_dtype)
sumexp_i = T.alloc_fragment([H_per_block], accum_dtype)
alpha = T.alloc_fragment([H_per_block], accum_dtype)
m_i = T.alloc_fragment([H_per_block], accum_dtype)
m_i_prev = T.alloc_fragment([H_per_block], accum_dtype)
T.fill(acc_o, 0)
T.fill(acc_o_tail, 0)
T.fill(sumexp, 0)
T.fill(m_i, -(2**30))
b_i, g_i = by, bz
spans_per_seq = REPLICATE_H * n_groups
s_i = bx // spans_per_seq
rest = bx % spans_per_seq
group_i = rest // REPLICATE_H
h_rep = rest % REPLICATE_H
H0 = g_i * padded_H + (0 if REPLICATE_H == 1 else h_rep * 64)
H1 = H0 + H_per_block
T.copy(Q[b_i, s_i, H0:H1, :D], Q_shared)
T.copy(Q[b_i, s_i, H0:H1, D : D + D_tail], Q_tail_shared)
tk_len_1 = Topk_length_1[b_i]
for k_i in T.Pipelined(inner_iter_1, num_stages=num_stages):
iter_i = group_i * inner_iter_1 + k_i
for bi_i in T.Parallel(BI):
pos = iter_i * BI + bi_i
idx = Indices_1[b_i, s_i, pos]
valid = (idx >= 0) & (pos < tk_len_1)
page_idx_shared[bi_i] = T.if_then_else(valid, idx, 0)
mask[bi_i] = valid
for bi_i, w_i in T.Parallel(BI, PACKED_W4):
page = page_idx_shared[bi_i]
block_id = page // BS_KV_1
t_in_block = page % BS_KV_1
K_packed_shared[bi_i, w_i] = K_combined_1[
block_id, t_in_block * PACKED_W4 + w_i
]
for bi_i, w_i in T.Parallel(BI, SCALE_W4):
page = page_idx_shared[bi_i]
block_id = page // BS_KV_1
t_in_block = page % BS_KV_1
K_scale_shared[bi_i, w_i] = K_combined_1[
block_id,
NOPE_ROPE_U32_PER_BLOCK_1 + t_in_block * SCALE_W4 + w_i,
]
for bi_i, ti in T.Parallel(BI, NUM_TILES):
word_idx = ti // 4
byte_in_word = ti % 4
word = K_scale_shared[bi_i, word_idx]
scale_byte_local[bi_i, ti] = (
word >> T.Cast("uint32", byte_in_word * 8)
) & T.uint32(0xFF)
for bi_i, d_i in T.Parallel(BI, D):
word_idx = d_i // 4
byte_in_word = d_i % 4
word = K_packed_shared[bi_i, word_idx]
b_u32 = (word >> T.Cast("uint32", byte_in_word * 8)) & T.uint32(
0xFF
)
sign_bf = (b_u32 & T.uint32(0x80)) * T.uint32(0x100)
exp_e4 = (b_u32 & T.uint32(0x78)) >> T.uint32(3)
mant_bf = (b_u32 & T.uint32(0x7)) * T.uint32(0x10)
scale_byte = scale_byte_local[bi_i, d_i // NOPE_TILE]
exp_combined = exp_e4 + scale_byte - T.uint32(7)
bf16_bits = sign_bf | (exp_combined << T.uint32(7)) | mant_bf
KV_shared[bi_i, d_i] = T.reinterpret(
BF16, T.Cast("uint16", bf16_bits)
)
for bi_i, j in T.Parallel(BI, D_tail):
abs_off = D + 2 * j
word_idx = abs_off // 4
word_off = abs_off % 4
word = K_packed_shared[bi_i, word_idx]
half_u32 = T.if_then_else(
word_off == 0,
word & T.uint32(0xFFFF),
(word >> T.uint32(16)) & T.uint32(0xFFFF),
)
K_tail_shared[bi_i, j] = T.reinterpret(
BF16, T.Cast("uint16", half_u32)
)
for h_i, bi_i in T.Parallel(H_per_block, BI):
acc_s[h_i, bi_i] = T.if_then_else(
mask[bi_i], 0, -T.infinity(acc_s.dtype)
)
T.gemm(
Q_shared,
KV_shared,
acc_s,
transpose_B=True,
policy=T.GemmWarpPolicy.FullRow,
)
T.gemm(
Q_tail_shared,
K_tail_shared,
acc_s,
transpose_B=True,
policy=T.GemmWarpPolicy.FullRow,
)
T.copy(m_i, m_i_prev)
T.reduce_max(acc_s, m_i, dim=1, clear=False)
for h_i in T.Parallel(H_per_block):
m_i[h_i] = T.max(m_i[h_i], m_i_prev[h_i])
for h_i in T.Parallel(H_per_block):
alpha[h_i] = T.exp2((m_i_prev[h_i] - m_i[h_i]) * sm_scale)
for h_i, bi_i in T.Parallel(H_per_block, BI):
acc_s[h_i, bi_i] = T.exp2(
acc_s[h_i, bi_i] * sm_scale - m_i[h_i] * sm_scale
)
T.reduce_sum(acc_s, sumexp_i, dim=1)
for h_i in T.Parallel(H_per_block):
sumexp[h_i] = sumexp[h_i] * alpha[h_i] + sumexp_i[h_i]
for h_i, d_i in T.Parallel(H_per_block, D):
acc_o[h_i, d_i] *= alpha[h_i]
for h_i, d_i in T.Parallel(H_per_block, D_tail):
acc_o_tail[h_i, d_i] *= alpha[h_i]
T.copy(acc_s, S_shared)
T.gemm(S_shared, KV_shared, acc_o, policy=T.GemmWarpPolicy.FullRow)
T.gemm(
S_shared, K_tail_shared, acc_o_tail, policy=T.GemmWarpPolicy.FullRow
)
for h_i, d_i in T.Parallel(H_per_block, D):
acc_o[h_i, d_i] = acc_o[h_i, d_i] / T.if_then_else(
sumexp[h_i] == 0.0, 1.0, sumexp[h_i]
)
for h_i, d_i in T.Parallel(H_per_block, D_tail):
acc_o_tail[h_i, d_i] = acc_o_tail[h_i, d_i] / T.if_then_else(
sumexp[h_i] == 0.0, 1.0, sumexp[h_i]
)
for h_i in T.Parallel(H_per_block):
m_i[h_i] = T.if_then_else(
sumexp[h_i] == 0.0,
-(2.0**30),
T.log2(sumexp[h_i]) + m_i[h_i] * sm_scale,
)
T.copy(acc_o, Partial_O[b_i, s_i, group_i, H0:H1, :D])
T.copy(acc_o_tail, Partial_O[b_i, s_i, group_i, H0:H1, D : D + D_tail])
T.copy(m_i, Partial_LSE[b_i, s_i, group_i, H0:H1])
return main
@tilelang.jit(
out_idx=[-2, -1],
pass_configs={
tilelang.PassConfigKey.TL_DISABLE_TMA_LOWER: True,
tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True,
tilelang.PassConfigKey.TL_ENABLE_FAST_MATH: True,
},
)
def dpsk_v4_combine_kernel(
num_heads: int,
n_groups_1: int,
n_groups_2: int = 0,
*,
block_I: int = 32,
inner_iter_1: int = 1,
inner_iter_2: int = 1,
dim: int = 448,
tail_dim: int = 64,
head_per_block: int = 16,
threads: int = 256,
use_attn_sink: bool = False,
) -> Any:
"""
Combine `n_groups` flash-attention partials into the final output.
Inputs:
Partial_O : (batch, seq_len, n_groups, num_heads, dim+tail_dim) bf16
Partial_LSE : (batch, seq_len, n_groups, num_heads) fp32, log2 form
Topk_length_1: (batch,) int32, actual phase-1 length
Topk_length_2: (batch,) int32, actual phase-2 length (dual only)
Attn_sink : (num_heads,) fp32
Outputs:
Output : (batch, seq_len, num_heads, dim+tail_dim) bf16
LSE : (batch, seq_len, num_heads) fp32, natural log
Each grid block handles `head_per_block` heads of one (batch, seq) row.
"""
log2e: float = 1.44269504
ln2: float = 0.69314718
assert num_heads % head_per_block == 0
is_dual = n_groups_2 > 0
n_groups = n_groups_1 + n_groups_2
H_per_block = head_per_block
HEAD_BLOCKS = num_heads // H_per_block
DT = dim + tail_dim
batch = T.symbolic("batch")
seq_len = T.symbolic("seq_len")
accum_dtype = "float"
if is_dual:
@T.prim_func
def main(
Partial_O: T.Tensor(
[batch, seq_len, n_groups, num_heads, DT], BF16
), # type: ignore
Partial_LSE: T.Tensor(
[batch, seq_len, n_groups, num_heads], accum_dtype
), # type: ignore
Topk_length_1: T.Tensor([batch], INT32), # type: ignore
Topk_length_2: T.Tensor([batch], INT32), # type: ignore
Attn_sink: T.Tensor([num_heads], FP32), # type: ignore
Output: T.Tensor([batch, seq_len, num_heads, DT], BF16), # type: ignore
LSE: T.Tensor([batch, seq_len, num_heads], accum_dtype), # type: ignore
) -> None:
with T.Kernel(seq_len * HEAD_BLOCKS, batch, threads=threads) as (
bx,
by,
):
shared_lse = T.alloc_shared([n_groups, H_per_block], accum_dtype)
lse_max = T.alloc_fragment([H_per_block], accum_dtype)
lse_sum = T.alloc_fragment([H_per_block], accum_dtype)
scale = T.alloc_fragment([H_per_block, n_groups], accum_dtype)
acc_o = T.alloc_fragment([H_per_block, DT], accum_dtype)
attn_sink_frag = T.alloc_fragment([H_per_block], accum_dtype)
o_scale_frag = T.alloc_fragment([H_per_block], accum_dtype)
final_lse = T.alloc_fragment([H_per_block], accum_dtype)
b_i = by
s_i = bx // HEAD_BLOCKS
head_block = bx % HEAD_BLOCKS
H0 = head_block * H_per_block
H1 = H0 + H_per_block
# Clamp to the captured-shape upper bounds so callers passing
# the INT32_MAX sentinel (= "all valid") still iterate exactly
# n_groups groups, not 33M.
actual_n_groups_1 = T.min(
T.ceildiv(Topk_length_1[b_i], block_I * inner_iter_1),
n_groups_1,
)
actual_n_groups_2 = T.min(
T.ceildiv(Topk_length_2[b_i], block_I * inner_iter_2),
n_groups - n_groups_1,
)
actual_n_groups = actual_n_groups_1 + actual_n_groups_2
# Pass 1: load only active groups' LSE into compact slots.
for k_c in T.serial(actual_n_groups):
k = T.if_then_else(
k_c < actual_n_groups_1,
k_c,
n_groups_1 + (k_c - actual_n_groups_1),
)
T.copy(Partial_LSE[b_i, s_i, k, H0:H1], shared_lse[k_c, :])
T.fill(lse_max, -(2**30))
for k_c in T.serial(actual_n_groups):
for h_i in T.Parallel(H_per_block):
lse_max[h_i] = T.max(lse_max[h_i], shared_lse[k_c, h_i])
T.fill(lse_sum, 0)
for k_c in T.serial(actual_n_groups):
for h_i in T.Parallel(H_per_block):
lse_sum[h_i] = lse_sum[h_i] + T.exp2(
shared_lse[k_c, h_i] - lse_max[h_i]
)
for k_c in T.serial(actual_n_groups):
for h_i in T.Parallel(H_per_block):
scale[h_i, k_c] = T.exp2(
shared_lse[k_c, h_i] - lse_max[h_i] - T.log2(lse_sum[h_i])
)
T.fill(acc_o, 0)
for k_c in T.serial(actual_n_groups):
k = T.if_then_else(
k_c < actual_n_groups_1,
k_c,
n_groups_1 + (k_c - actual_n_groups_1),
)
for h_i, d_i in T.Parallel(H_per_block, DT):
acc_o[h_i, d_i] = acc_o[h_i, d_i] + scale[h_i, k_c] * Partial_O[
b_i, s_i, k, H0 + h_i, d_i
].astype(accum_dtype)
for h_i in T.Parallel(H_per_block):
empty = lse_max[h_i] <= -(2**29)
final_lse[h_i] = T.if_then_else(
empty,
T.infinity(accum_dtype),
(lse_max[h_i] + T.log2(lse_sum[h_i])) * ln2,
)
if use_attn_sink:
for h_i in T.Parallel(H_per_block):
attn_sink_frag[h_i] = Attn_sink[H0 + h_i]
for h_i in T.Parallel(H_per_block):
empty = lse_max[h_i] <= -(2**29)
o_scale_frag[h_i] = T.if_then_else(
empty,
0.0,
1.0
/ (
1.0
+ T.exp2((attn_sink_frag[h_i] - final_lse[h_i]) * log2e)
),
)
for h_i, d_i in T.Parallel(H_per_block, DT):
acc_o[h_i, d_i] = acc_o[h_i, d_i] * o_scale_frag[h_i]
T.copy(acc_o, Output[b_i, s_i, H0:H1, :])
T.copy(final_lse, LSE[b_i, s_i, H0:H1])
return main
@T.prim_func
def main(
Partial_O: T.Tensor(
[batch, seq_len, n_groups, num_heads, DT], BF16
), # type: ignore
Partial_LSE: T.Tensor(
[batch, seq_len, n_groups, num_heads], accum_dtype
), # type: ignore
Attn_sink: T.Tensor([num_heads], FP32), # type: ignore
Output: T.Tensor([batch, seq_len, num_heads, DT], BF16), # type: ignore
LSE: T.Tensor([batch, seq_len, num_heads], accum_dtype), # type: ignore
) -> None:
with T.Kernel(seq_len * HEAD_BLOCKS, batch, threads=threads) as (bx, by):
shared_lse = T.alloc_shared([n_groups, H_per_block], accum_dtype)
lse_max = T.alloc_fragment([H_per_block], accum_dtype)
lse_sum = T.alloc_fragment([H_per_block], accum_dtype)
scale = T.alloc_fragment([H_per_block, n_groups], accum_dtype)
acc_o = T.alloc_fragment([H_per_block, DT], accum_dtype)
attn_sink_frag = T.alloc_fragment([H_per_block], accum_dtype)
o_scale_frag = T.alloc_fragment([H_per_block], accum_dtype)
final_lse = T.alloc_fragment([H_per_block], accum_dtype)
b_i = by
s_i = bx // HEAD_BLOCKS
head_block = bx % HEAD_BLOCKS
H0 = head_block * H_per_block
H1 = H0 + H_per_block
for k in T.serial(n_groups):
T.copy(Partial_LSE[b_i, s_i, k, H0:H1], shared_lse[k, :])
T.fill(lse_max, -(2**30))
for k in T.serial(n_groups):
for h_i in T.Parallel(H_per_block):
lse_max[h_i] = T.max(lse_max[h_i], shared_lse[k, h_i])
T.fill(lse_sum, 0)
for k in T.serial(n_groups):
for h_i in T.Parallel(H_per_block):
lse_sum[h_i] = lse_sum[h_i] + T.exp2(
shared_lse[k, h_i] - lse_max[h_i]
)
for k in T.serial(n_groups):
for h_i in T.Parallel(H_per_block):
scale[h_i, k] = T.exp2(
shared_lse[k, h_i] - lse_max[h_i] - T.log2(lse_sum[h_i])
)
T.fill(acc_o, 0)
for k in T.serial(n_groups):
for h_i, d_i in T.Parallel(H_per_block, DT):
acc_o[h_i, d_i] = acc_o[h_i, d_i] + scale[h_i, k] * Partial_O[
b_i, s_i, k, H0 + h_i, d_i
].astype(accum_dtype)
for h_i in T.Parallel(H_per_block):
empty = lse_max[h_i] <= -(2**29)
final_lse[h_i] = T.if_then_else(
empty,
T.infinity(accum_dtype),
(lse_max[h_i] + T.log2(lse_sum[h_i])) * ln2,
)
if use_attn_sink:
for h_i in T.Parallel(H_per_block):
attn_sink_frag[h_i] = Attn_sink[H0 + h_i]
for h_i in T.Parallel(H_per_block):
empty = lse_max[h_i] <= -(2**29)
o_scale_frag[h_i] = T.if_then_else(
empty,
0.0,
1.0
/ (
1.0 + T.exp2((attn_sink_frag[h_i] - final_lse[h_i]) * log2e)
),
)
for h_i, d_i in T.Parallel(H_per_block, DT):
acc_o[h_i, d_i] = acc_o[h_i, d_i] * o_scale_frag[h_i]
T.copy(acc_o, Output[b_i, s_i, H0:H1, :])
T.copy(final_lse, LSE[b_i, s_i, H0:H1])
return main
"""
2-stage attention kernel (partial + combine) over an FP8 KV cache,
with optional second cache (`extra_k_cache`).
"""
def dpsk_v4_fp8_attention_fwd(
q: torch.Tensor,
k_cache: torch.Tensor,
block_table: Optional[torch.Tensor],
cache_seqlens: Optional[torch.Tensor],
head_dim_v: int,
tile_scheduler_metadata: Any,
num_splits: None = None,
softmax_scale: Optional[float] = None,
causal: bool = False,
is_fp8_kvcache: bool = False,
indices: Optional[torch.Tensor] = None,
attn_sink: Optional[torch.Tensor] = None,
extra_k_cache: Optional[torch.Tensor] = None,
extra_indices_in_kvcache: Optional[torch.Tensor] = None,
topk_length: Optional[torch.Tensor] = None,
extra_topk_length: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Follows the original `flash_mla.flash_mla_with_kvcache` signature.
"""
if _is_gfx95_supported:
block_I, threads, num_stages, block_per_cu, cu = 64, 512, 0, 2, 256
else:
block_I, threads, num_stages, block_per_cu, cu = 32, 128, 1, 1, 304
batch, seq_len, num_heads, _ = q.shape
# Partial grid is (seq_len * REPLICATE_H * n_groups, batch, kv_group); the
# heuristic in _pick_inner_iter assumes `total_blocks = seq * ni / inner_iter`,
# so `seq` must include REPLICATE_H or n_groups doubles for medium batches.
replicate_h = max((num_heads + 63) // 64, 1)
seq = batch * seq_len * replicate_h
k1, _, bs_kv_1 = _build_fp8_combined_view(k_cache)
topk_1 = indices.shape[-1]
ni_1 = topk_1 // block_I
tk_len_1 = (
topk_length
if topk_length is not None
else _topk_length_sentinel(q.device, batch)
)
if attn_sink is None:
attn_sink = torch.full(
(num_heads,), float("-inf"), dtype=torch.float32, device=q.device
)
has_extra = extra_k_cache is not None
if not has_extra:
inner_iter_1 = _pick_inner_iter(seq, ni_1, cu, block_per_cu)
inner_iter_2 = 1
n_groups_1 = ni_1 // inner_iter_1
n_groups_2 = 0
partial = dpsk_v4_fp8_partial_kernel(
num_heads,
topk_1,
bs_kv_1,
sm_scale=softmax_scale,
block_I=block_I,
inner_iter_1=inner_iter_1,
num_stages=num_stages,
threads=threads,
)
partial_o, partial_lse = partial(q, k1, indices, tk_len_1)
else:
k2, _, bs_kv_2 = _build_fp8_combined_view(extra_k_cache)
topk_2 = extra_indices_in_kvcache.shape[-1]
ni_2 = topk_2 // block_I
# Each phase picks its own optimal split-K independently — kernel
# body uses two T.Pipelined loops with separate compile-time iter
# counts, no shared-divisor constraint.
inner_iter_1 = _pick_inner_iter(seq, ni_1, cu, block_per_cu)
inner_iter_2 = _pick_inner_iter(seq, ni_2, cu, block_per_cu)
n_groups_1 = ni_1 // inner_iter_1
n_groups_2 = ni_2 // inner_iter_2
tk_len_2 = (
extra_topk_length
if extra_topk_length is not None
else _topk_length_sentinel(q.device, batch)
)
partial = dpsk_v4_fp8_partial_kernel(
num_heads,
topk_1,
bs_kv_1,
topk_2,
bs_kv_2,
sm_scale=softmax_scale,
block_I=block_I,
inner_iter_1=inner_iter_1,
inner_iter_2=inner_iter_2,
num_stages=num_stages,
threads=threads,
)
partial_o, partial_lse = partial(
q,
k1,
indices,
tk_len_1,
k2,
extra_indices_in_kvcache,
tk_len_2,
)
combine = dpsk_v4_combine_kernel(
num_heads,
n_groups_1,
n_groups_2,
block_I=block_I,
inner_iter_1=inner_iter_1,
inner_iter_2=inner_iter_2,
head_per_block=4,
threads=256,
use_attn_sink=True,
)
if has_extra:
return combine(partial_o, partial_lse, tk_len_1, tk_len_2, attn_sink)
return combine(partial_o, partial_lse, attn_sink)
|