File size: 164,048 Bytes
e39ff3a |
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 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 |
# PR #16104: [Model] Support Llama4 in vLLM
## § Overview
- **Author:** @houseroad
- **Status:** merged
- **Created:** 2025-04-05
- **Merged:** 2025-04-06
- **Base:** v0.8.3 ← **Head:** init_pr
## § Description
Add the support for Llama4 Scout (17B x 16 Experts) and Maverick (17B x 128 Experts) in vLLM.
Using 8xH100, vLLM can serve Scout with 1M context and Maverick with about 430K.
```
vllm serve meta-llama/Llama-4-Scout-17B-16E-Instruct \
--tensor-parallel-size 8 \
--max-model-len 1280000
vllm serve meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8 \
--tensor-parallel-size 8 \
--max-model-len 430000
```
Using 8xH200, vLLM can serve Scout with 3.6M context and Maverick with full 1M context.
```
vllm serve meta-llama/Llama-4-Scout-17B-16E-Instruct \
--tensor-parallel-size 8 \
--max-model-len 3600000
vllm serve meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8 \
--tensor-parallel-size 8
```
Using MI300x, we can run with default settings.
```
VLLM_WORKER_MULTIPROC_METHOD=spawn \
VLLM_USE_MODELSCOPE=False \
SAFETENSORS_FAST_GPU=1 VLLM_USE_V1=1 vllm serve meta-llama/Llama-4-Maverick-17B-128E-Instruct \
--disable-log-requests -tp 8 \
--max-num-seqs 64
```
Check out blog post [link coming soon] for performance enhancement and leveraging long context.
FIX #16106
## § Linked Issues
- **#16106: [New Model]: Llama4 Support** (CLOSED)
### 🚀 The feature, motivation and pitch
Meta released 2 Variants:
Llama 4 Scout:
A high-performing small model with 17B activated parameters across 16 experts. Extremely fast, natively multimodal, supports a 10M+ token context window, and runs on a single GPU.
Llama 4 Maverick:
A top-tier multimodal model outperforming GPT-4o and Gemini 2.0 Flash, with performance on par with DeepSeek V3 at half the active parameters. ELO 1417 on LMArena and runs on a single host.
### Alternatives
_No response_
### Additional context
_No response_
### Before submitting a new issue...
- [x] Make sure you already searched for relevant issues, and asked the chatbot living at the bottom right corner of the [documentation page](https://docs.vllm.ai/en/latest/), which can answer lots of frequently asked questions.
## § Files Changed (35 files)
- `benchmarks/kernels/benchmark_moe.py`
- `docs/source/models/supported_models.md`
- `examples/offline_inference/vision_language.py`
- `examples/offline_inference/vision_language_multi_image.py`
- `requirements/common.txt`
- `requirements/test.in`
- `requirements/test.txt`
- `tests/models/decoder_only/vision_language/test_models.py`
- `tests/models/multimodal/processing/test_common.py`
- `tests/models/multimodal/processing/test_llama4.py`
- `tests/models/registry.py`
- `tests/models/test_registry.py`
- `vllm/config.py`
- `vllm/entrypoints/chat_utils.py`
- `vllm/model_executor/layers/fused_moe/configs/E=16,N=1024,device_name=AMD_Instinct_MI300X.json`
- `vllm/model_executor/layers/fused_moe/cutlass_moe.py`
- `vllm/model_executor/layers/fused_moe/fused_moe.py`
- `vllm/model_executor/layers/fused_moe/layer.py`
- `vllm/model_executor/layers/layernorm.py`
- `vllm/model_executor/layers/quantization/awq_marlin.py`
- `vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py`
- `vllm/model_executor/layers/quantization/experts_int8.py`
- `vllm/model_executor/layers/quantization/fp8.py`
- `vllm/model_executor/layers/quantization/gguf.py`
- `vllm/model_executor/layers/quantization/gptq_marlin.py`
- `vllm/model_executor/layers/quantization/moe_wna16.py`
- `vllm/model_executor/layers/quantization/quark/quark_moe.py`
- `vllm/model_executor/layers/rotary_embedding.py`
- `vllm/model_executor/models/llama.py`
- `vllm/model_executor/models/llama4.py`
- `vllm/model_executor/models/mllama4.py`
- `vllm/model_executor/models/registry.py`
- `vllm/v1/attention/backends/flash_attn.py`
- `vllm/v1/attention/backends/triton_attn.py`
- `vllm/v1/worker/gpu_model_runner.py`
## § Code Review Comments
### § Review by @ywang96 (Commented)
> Multimodal part looks fine to me - left some nits but we can fix them later
**File:** `tests/models/registry.py:4`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -337,6 +337,7 @@ def check_available_online(
tokenizer="facebook/bart-base",
trust_remote_code=True), # noqa: E501
"MllamaForConditionalGeneration": _HfExamplesInfo("meta-llama/Llama-3.2-11B-Vision-Instruct"), # noqa: E501
+ "Llama4ForConditionalGeneration": _HfExamplesInfo("meta-llama/Llama-4-Scout-17B-16E-Instruct"), # noqa: E501
```
**Comment:** We need to add a `min_transformers_version` on this
**File:** `vllm/model_executor/models/mllama4.py:438`
**Context:**
```diff
...
+ grads on inputs.
+ """
+ return self.patch_embedding
```
**Comment:** I don't think this function is used anywhere? Can we remove it?
**File:** `vllm/model_executor/models/mllama4.py:474`
**Context:**
```diff
...
+ # Remove CLS token output
+ hidden_state = hidden_state[:, :-1, :]
+
+ # now, we use Llama4VisionPixelShuffle + mlp to project embeddings
+ hidden_state = self.vision_adapter(hidden_state)
...
```
**Comment:** We don't really need to wrap the output hidden state insinde `BaseModelOutput` class (this is something only `transformers` requires)
### § Review by @mgoin (Commented)
**File:** `vllm/model_executor/layers/fused_moe/cutlass_moe.py:13`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -96,8 +97,14 @@ def cutlass_moe_fp8(
n = w2_q.size(1)
topk = topk_ids.size(1)
+ assert topk == 1, \
```
**Comment:** Should we move this assert to be in the `if apply_router_weight_on_input:` conditional? This seems restrictive without checking if apply_router_weight_on_input is true
**File:** `vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py:26`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -240,24 +240,28 @@ def apply(
scoring_func=scoring_func,
e_score_correction_bias=e_score_correction_bias)
- return fused_experts(x,
```
**Comment:** Forgot to add attribute like in other method
### § Review by @simon-mo (Commented)
**File:** `vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py:26`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -240,24 +240,28 @@ def apply(
scoring_func=scoring_func,
e_score_correction_bias=e_score_correction_bias)
- return fused_experts(x,
```
**Comment:** This is WIP by @luccafong
### § Review by @ywang96 (Commented)
**File:** `tests/models/registry.py:4`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -337,6 +337,7 @@ def check_available_online(
tokenizer="facebook/bart-base",
trust_remote_code=True), # noqa: E501
"MllamaForConditionalGeneration": _HfExamplesInfo("meta-llama/Llama-3.2-11B-Vision-Instruct"), # noqa: E501
+ "Llama4ForConditionalGeneration": _HfExamplesInfo("meta-llama/Llama-4-Scout-17B-16E-Instruct"), # noqa: E501
```
**Comment:** Resolving per [a19cf7b](https://github.com/vllm-project/vllm/pull/16104/commits/a19cf7bafcb37adb359742cb4258da94605a7a3e)
### § Review by @ywang96 (Commented)
**File:** `vllm/model_executor/models/mllama4.py:474`
**Context:**
```diff
...
+ # Remove CLS token output
+ hidden_state = hidden_state[:, :-1, :]
+
+ # now, we use Llama4VisionPixelShuffle + mlp to project embeddings
+ hidden_state = self.vision_adapter(hidden_state)
...
```
**Comment:** Resolving - we will fix it in a later PR
### § Review by @ywang96 (Commented)
**File:** `vllm/model_executor/models/mllama4.py:438`
**Context:**
```diff
...
+ grads on inputs.
+ """
+ return self.patch_embedding
```
**Comment:** Resolved in [bacd195](https://github.com/vllm-project/vllm/pull/16104/commits/bacd1954f5df85b9bed917ae13179a4a2abba1e3)
### § Review by @tlrmchlsmth (Commented)
**File:** `examples/offline_inference/vision_language_multi_image.py:11`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -253,6 +253,43 @@ def load_internvl(question: str, image_urls: list[str]) -> ModelRequestData:
)
+def load_llama4(question: str, image_urls: list[str]) -> ModelRequestData:
```
**Comment:** Is tp 8 overkill for the scout model?
### § Review by @houseroad (Commented)
**File:** `examples/offline_inference/vision_language_multi_image.py:11`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -253,6 +253,43 @@ def load_internvl(question: str, image_urls: list[str]) -> ModelRequestData:
)
+def load_llama4(question: str, image_urls: list[str]) -> ModelRequestData:
```
**Comment:** we can do tp4.
### § Review by @ywang96 (Commented)
**File:** `examples/offline_inference/vision_language_multi_image.py:11`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -253,6 +253,43 @@ def load_internvl(question: str, image_urls: list[str]) -> ModelRequestData:
)
+def load_llama4(question: str, image_urls: list[str]) -> ModelRequestData:
```
**Comment:** ~I think TP=4 should be fine for this model on most devices?~ nvm - confirmed it's better to recommend running TP=8
### § Review by @houseroad (Commented)
**File:** `examples/offline_inference/vision_language_multi_image.py:11`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -253,6 +253,43 @@ def load_internvl(question: str, image_urls: list[str]) -> ModelRequestData:
)
+def load_llama4(question: str, image_urls: list[str]) -> ModelRequestData:
```
**Comment:** TP2 may not have enough HBM.
### § Review by @LucasWilkinson (Commented)
**File:** `vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py:26`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -240,24 +240,28 @@ def apply(
scoring_func=scoring_func,
e_score_correction_bias=e_score_correction_bias)
- return fused_experts(x,
```
**Comment:** Are you guys referring to the pre-commit failure? sorry I think this was from my changes, @luccafong I have a fix for this I can push if you want, otherwise I can send you a patch (if you haven't already fixed it)
### § Review by @yeqcharlotte (Commented)
**File:** `examples/offline_inference/vision_language_multi_image.py:11`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -253,6 +253,43 @@ def load_internvl(question: str, image_urls: list[str]) -> ModelRequestData:
)
+def load_llama4(question: str, image_urls: list[str]) -> ModelRequestData:
```
**Comment:** we can try to create an fp8 version with llm compressor for scout which is runnable on tp2 later
### § Review by @ywang96 (Commented)
**File:** `examples/offline_inference/vision_language_multi_image.py:11`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -253,6 +253,43 @@ def load_internvl(question: str, image_urls: list[str]) -> ModelRequestData:
)
+def load_llama4(question: str, image_urls: list[str]) -> ModelRequestData:
```
**Comment:** Yea this is fine for this release - examples aren't tied to release and they can be updated anytime anyways
### § Review by @tlrmchlsmth (Commented)
**File:** `vllm/v1/attention/backends/flash_attn.py:75`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -96,6 +96,183 @@ class FlashAttentionMetadata:
# For logging.
num_input_tokens: int = 0 # Number of tokens including padding.
+ # for local attention
```
**Comment:** Should it be named `q_seqlens_np`?
**File:** `vllm/v1/attention/backends/flash_attn.py` (File-level comment)
**Comment:** `LocalAttentionMetadata` and `make_local_attention_virtual_batches` look good to me. BTW has anybody profiled this? We should look at writing a "kernel" as a followup
### § Review by @yeqcharlotte (Commented)
**File:** `vllm/model_executor/models/registry.py:4`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -73,6 +73,7 @@
"JAISLMHeadModel": ("jais", "JAISLMHeadModel"),
"JambaForCausalLM": ("jamba", "JambaForCausalLM"),
"LlamaForCausalLM": ("llama", "LlamaForCausalLM"),
+ "Llama4ForCausalLM": ("llama4", "Llama4ForCausalLM"),
```
**Comment:** @houseroad @ywang96 do you think we can just delete this
### § Review by @ywang96 (Commented)
**File:** `vllm/model_executor/models/registry.py:4`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -73,6 +73,7 @@
"JAISLMHeadModel": ("jais", "JAISLMHeadModel"),
"JambaForCausalLM": ("jamba", "JambaForCausalLM"),
"LlamaForCausalLM": ("llama", "LlamaForCausalLM"),
+ "Llama4ForCausalLM": ("llama4", "Llama4ForCausalLM"),
```
**Comment:** We actually need it for the following to work
```python
self.language_model = init_vllm_registered_model(
vllm_config=vllm_config,
hf_config=config.text_config,
architectures=["Llama4ForCausalLM"],
prefix=maybe_prefix(prefix, "language_model"))
```
[b4533e3](https://github.com/vllm-project/vllm/pull/16104/commits/b4533e3aaa54ba48a3a4814b6d3b8d47347cad18) should fix the CI error on this.
### § Review by @ywang96 (Commented)
**File:** `vllm/model_executor/models/registry.py:4`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -73,6 +73,7 @@
"JAISLMHeadModel": ("jais", "JAISLMHeadModel"),
"JambaForCausalLM": ("jamba", "JambaForCausalLM"),
"LlamaForCausalLM": ("llama", "LlamaForCausalLM"),
+ "Llama4ForCausalLM": ("llama4", "Llama4ForCausalLM"),
```
**Comment:** We can also change the code in `mllama4.py` to import `Llama4ForCausalLM` directly so that we don't need to register it, but I feel like it's probably better to do it in a follow-up PR.
### § Review by @AlekseyKorshuk (Commented)
**File:** `vllm/model_executor/models/mllama4.py:491`
**Context:**
```diff
...
+
+ def get_supported_mm_limits(self) -> Mapping[str, Optional[int]]:
+ return {"image": 10}
```
**Comment:** Why limit it to 10 images only if the model has to support way more, given its context length and benchmark results published by Meta claiming of processing up to 20 hours of video?
### § Review by @ywang96 (Commented)
**File:** `vllm/model_executor/models/mllama4.py:491`
**Context:**
```diff
...
+
+ def get_supported_mm_limits(self) -> Mapping[str, Optional[int]]:
+ return {"image": 10}
```
**Comment:** I don't think video inference is the scope of this release yet?
This PR doesn't support video modality so I guess it'll come in the next model update?
### § Review by @yeqcharlotte (Commented)
**File:** `vllm/model_executor/models/mllama4.py:491`
**Context:**
```diff
...
+
+ def get_supported_mm_limits(self) -> Mapping[str, Optional[int]]:
+ return {"image": 10}
```
**Comment:** @AlekseyKorshuk 8-10 image is the recommended mm limit giving you acceptable quality although from the infra perspective it can do more.
Llama4’s video tokenizer works slightly different form image and we’ll update that once it’s available.
### § Review by @AlekseyKorshuk (Commented)
**File:** `vllm/model_executor/models/mllama4.py:491`
**Context:**
```diff
...
+
+ def get_supported_mm_limits(self) -> Mapping[str, Optional[int]]:
+ return {"image": 10}
```
**Comment:** That's a fair point, but it raises an error if set cli argument to >10 multimodal limit. Shouldn't 10 be a default value, but not the hard limit that is not possible to overcome without changing the code?
### § Review by @DarkLight1337 (Commented)
**File:** `docs/source/models/supported_models.md:43`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -982,10 +989,10 @@ See [this page](#generative-models) for more information on how to use generativ
* ✅︎
:::
-<sup>^</sup> You need to set the architecture name via `--hf-overrides` to match the one in vLLM.
```
**Comment:** These whitespaces should not be removed. They are intentional to add paragraph spacing
### § Review by @ywang96 (Commented)
**File:** `vllm/model_executor/models/mllama4.py:491`
**Context:**
```diff
...
+
+ def get_supported_mm_limits(self) -> Mapping[str, Optional[int]]:
+ return {"image": 10}
```
**Comment:** I think I'm okay with not capping it at 10, but setting a default value for this will be something model-dependent which we currently don't support today on vLLM (and it's tricky to do that since today there's no standard on how many images a model can support up to), so we let user do it by passing `limit-mm-per-prompt`.
### § Review by @AlekseyKorshuk (Commented)
**File:** `vllm/model_executor/models/mllama4.py:491`
**Context:**
```diff
...
+
+ def get_supported_mm_limits(self) -> Mapping[str, Optional[int]]:
+ return {"image": 10}
```
**Comment:** Sounds good, just wanted to make sure that this value is easy for users to change based on their needs. Thanks for the reply, gonna resolve the conversation
### § Review by @ywang96 (Commented)
**File:** `docs/source/models/supported_models.md:43`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -982,10 +989,10 @@ See [this page](#generative-models) for more information on how to use generativ
* ✅︎
:::
-<sup>^</sup> You need to set the architecture name via `--hf-overrides` to match the one in vLLM.
```
**Comment:** Given this is only added to the 0.8.3 release branch, I will fix it on main once it's merged there
### § Review by @LucasWilkinson (Commented)
**File:** `vllm/v1/attention/backends/flash_attn.py` (File-level comment)
**Comment:** I don't believe so, atleast I never did. I think as a first cut we could even just write a C++ op, this code is ALOT easier to understand as a loop and honestly would probably be faster as a loop (assuming its a C++ loop and not a python loop) since theres sooo many numpy calls in this version. I just wrote it this way assuming it would scale to larger batch sizes better than a python loop.
### § Review by @LucasWilkinson (Commented)
**File:** `vllm/v1/attention/backends/flash_attn.py:75`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -96,6 +96,183 @@ class FlashAttentionMetadata:
# For logging.
num_input_tokens: int = 0 # Number of tokens including padding.
+ # for local attention
```
**Comment:** could be, I just dropped the np suffixes in this function since they are all numpy arrays, but we could add them back in a future PR
### § Review by @wenmengzhou (Commented)
**File:** `examples/offline_inference/vision_language.py:23`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -582,6 +582,42 @@ def run_mllama(questions: list[str], modality: str) -> ModelRequestData:
)
+def run_llama4(questions: list[str], modality: str):
```
**Comment:** missing content of image, it should be
{
"type": "image",
"image": "https://path/to/your/image.jpg"
}
### § Review by @ywang96 (Commented)
**File:** `examples/offline_inference/vision_language.py:23`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -582,6 +582,42 @@ def run_mllama(questions: list[str], modality: str) -> ModelRequestData:
)
+def run_llama4(questions: list[str], modality: str):
```
**Comment:** The way it works with our offline inference `llm.generate` interface is actually a bit different from huggingface interface. In this case we're adding this chunk here only for it to insert the image placeholder token into the prompt when we apply the chat template from the tokenizer.
### § Review by @jianyuh (Commented)
**File:** `examples/offline_inference/vision_language_multi_image.py:11`
**Context:**
```diff
(Could not locate the specific line in the diff hunk)
@@ -253,6 +253,43 @@ def load_internvl(question: str, image_urls: list[str]) -> ModelRequestData:
)
+def load_llama4(question: str, image_urls: list[str]) -> ModelRequestData:
```
**Comment:** @tlrmchlsmth nice to see you here :) In Llama4 blog (https://ai.meta.com/blog/llama-4-multimodal-intelligence/), it mentions we can fit scout model in a single H100 GPU so using tp1:
The former fits on a single H100 GPU (with Int4 quantization) while the latter fits on a single H100 host.
https://github.com/meta-llama/llama-stack/blob/3f92b2bf85df6762b039c22ef54c6cad3c45f2c9/llama_stack/providers/inline/inference/meta_reference/llama4/generation.py#L44
-->
https://github.com/meta-llama/llama-stack/blob/3f92b2bf85df6762b039c22ef54c6cad3c45f2c9/llama_stack/providers/inline/inference/meta_reference/quantize_impls.py#L284
## § General Comments
### § @github-actions - 2025-04-05 19:09
👋 Hi! Thank you for contributing to the vLLM project.
💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.
Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run `fastcheck` CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your `fastcheck` build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping `simon-mo` or `khluu` to add you in our Buildkite org.
Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.
To run CI, PR reviewers can either: Add `ready` label to the PR or enable auto-merge.
🚀
### § @robertgshaw2-redhat - 2025-04-05 19:39
🔥
### § @dsingal0 - 2025-04-05 20:50
Is it expected to get this error:
File "/usr/local/lib/python3.12/dist-packages/vllm/model_executor/models/registry.py", line 451, in inspect_model_cls
return self._raise_for_unsupported(architectures)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/vllm/model_executor/models/registry.py", line 401, in _raise_for_unsupported
raise ValueError(
ValueError: Model architectures ['Llama4ForConditionalGeneration'] failed to be inspected. Please check the logs for more details.
### § @ywang96 - 2025-04-05 20:54
> Is it expected to get this error: File "/usr/local/lib/python3.12/dist-packages/vllm/model_executor/models/registry.py", line 451, in inspect_model_cls
>
> ```
> return self._raise_for_unsupported(architectures)
>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> ```
>
> File "/usr/local/lib/python3.12/dist-packages/vllm/model_executor/models/registry.py", line 401, in _raise_for_unsupported
>
> ```
> raise ValueError(
> ```
>
> ValueError: Model architectures ['Llama4ForConditionalGeneration'] failed to be inspected. Please check the logs for more details.
@dsingal0 Which version of ` transformers ` are you on?
### § @dsingal0 - 2025-04-05 20:58
> > Is it expected to get this error: File "/usr/local/lib/python3.12/dist-packages/vllm/model_executor/models/registry.py", line 451, in inspect_model_cls
> > ```
> > return self._raise_for_unsupported(architectures)
> >
> > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > ```
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > File "/usr/local/lib/python3.12/dist-packages/vllm/model_executor/models/registry.py", line 401, in _raise_for_unsupported
> > ```
> > raise ValueError(
> > ```
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > ValueError: Model architectures ['Llama4ForConditionalGeneration'] failed to be inspected. Please check the logs for more details.
>
> @dsingal0 Which version of `transformers` are you on?
transformers-4.52.0.dev0
### § @dsingal0 - 2025-04-05 21:33
I think transformers.models.llama4.image_processing_llama4 needs to be changed to transformers.models.llama4.image_processing_llama4_fast
### § @ywang96 - 2025-04-05 21:33
> I think transformers.models.llama4.image_processing_llama4 needs to be changed to transformers.models.llama4.image_processing_llama4_fast
Yea it's been addressed in [62e9744](https://github.com/vllm-project/vllm/pull/16104/commits/62e974401ae3cd4240a6eb109cb585c25a40da29) already
### § @fsaudm - 2025-04-06 16:25
Quantization support?
## § Commits (16 commits)
- `58d9c2f`: Initial Llama4 enablement for vLLM
- `dcb2c77`: revert changes in vllm/assets/image.py (#116)
- `89083a6`: fix inplace_fused_experts_fake (#117)
- `188bb52`: Bump transformers version to 4.51.0 (#119)
- `6ad393f`: clean up model names and whitespaces (#120)
- `ee170a7`: Revert "Bump transformers version to 4.51.0 (#119)"
- `a19cf7b`: Reapply "Bump transformers version to 4.51.0 (#119)"
- `bacd195`: remove used method
- `ec6cdaa`: fix MOE lint (#2)
- `62e9744`: fix llama4 processing (#3)
- `b4533e3`: skip llama4 standalone
- `0587bc7`: add marks
- `c0ca739`: Add apply_router_weight_on_input to CompressedTensorsWNA16MoEMethod's…
- `866b94a`: precommit
- `1b8b67a`: Add apply_router_weight_on_input to all FusedMoEMethodBase classes (#5)
- `4e45bfc`: fix basic model test
## § PR Diff
```diff
diff --git a/benchmarks/kernels/benchmark_moe.py b/benchmarks/kernels/benchmark_moe.py
index f1803b39c883..afe0b53077a7 100644
--- a/benchmarks/kernels/benchmark_moe.py
+++ b/benchmarks/kernels/benchmark_moe.py
@@ -553,6 +553,9 @@ def main(args: argparse.Namespace):
intermediate_size = config.moe_intermediate_size
shard_intermediate_size = 2 * intermediate_size // args.tp_size
else:
+ if not hasattr(config, "hidden_size"):
+ # Support for llama4
+ config = config.text_config
# Default: Mixtral.
E = config.num_local_experts
topk = config.num_experts_per_tok
diff --git a/docs/source/models/supported_models.md b/docs/source/models/supported_models.md
index 74b4eab92043..bb318be988b3 100644
--- a/docs/source/models/supported_models.md
+++ b/docs/source/models/supported_models.md
@@ -24,7 +24,7 @@ vLLM also supports model implementations that are available in Transformers. Thi
To check if the modeling backend is Transformers, you can simply do this:
-```python
+```python
from vllm import LLM
llm = LLM(model=..., task="generate") # Name or path of your model
llm.apply_model(lambda model: print(type(model)))
@@ -55,7 +55,7 @@ If your model is neither supported natively by vLLM or Transformers, you can sti
Simply set `trust_remote_code=True` and vLLM will run any model on the Model Hub that is compatible with Transformers.
Provided that the model writer implements their model in a compatible way, this means that you can run new models before they are officially supported in Transformers or vLLM!
-```python
+```python
from vllm import LLM
llm = LLM(model=..., task="generate", trust_remote_code=True) # Name or path of your model
llm.apply_model(lambda model: print(model.__class__))
@@ -840,6 +840,13 @@ See [this page](#generative-models) for more information on how to use generativ
*
* ✅︎
* ✅︎
+- * `Llama4ForConditionalGeneration`
+ * Llama-4-17B-Omni-Instruct
+ * T + I<sup>+</sup>
+ * `meta-llama/Llama-4-Scout-17B-16E-Instruct`, `meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8`, `meta-llama/Llama-4-Maverick-17B-128E-Instruct`, etc.
+ *
+ *
+ * ✅︎
- * `LlavaForConditionalGeneration`
* LLaVA-1.5
* T + I<sup>E+</sup>
@@ -982,10 +989,10 @@ See [this page](#generative-models) for more information on how to use generativ
* ✅︎
:::
-<sup>^</sup> You need to set the architecture name via `--hf-overrides` to match the one in vLLM.
- • For example, to use DeepSeek-VL2 series models:
- `--hf-overrides '{"architectures": ["DeepseekVLV2ForCausalLM"]}'`
-<sup>E</sup> Pre-computed embeddings can be inputted for this modality.
+<sup>^</sup> You need to set the architecture name via `--hf-overrides` to match the one in vLLM.
+ • For example, to use DeepSeek-VL2 series models:
+ `--hf-overrides '{"architectures": ["DeepseekVLV2ForCausalLM"]}'`
+<sup>E</sup> Pre-computed embeddings can be inputted for this modality.
<sup>+</sup> Multiple items can be inputted per text prompt for this modality.
:::{important}
diff --git a/examples/offline_inference/vision_language.py b/examples/offline_inference/vision_language.py
index c1115708505a..61d53dda1c47 100644
--- a/examples/offline_inference/vision_language.py
+++ b/examples/offline_inference/vision_language.py
@@ -582,6 +582,42 @@ def run_mllama(questions: list[str], modality: str) -> ModelRequestData:
)
+def run_llama4(questions: list[str], modality: str):
+ assert modality == "image"
+
+ model_name = "meta-llama/Llama-4-Scout-17B-16E-Instruct"
+
+ engine_args = EngineArgs(
+ model=model_name,
+ max_model_len=8192,
+ max_num_seqs=4,
+ tensor_parallel_size=8,
+ disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
+ gpu_memory_utilization=0.4,
+ )
+
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
+ messages = [[{
+ "role":
+ "user",
+ "content": [{
+ "type": "image"
+ }, {
+ "type": "text",
+ "text": f"{question}"
+ }]
+ }] for question in questions]
+ prompts = tokenizer.apply_chat_template(messages,
+ add_generation_prompt=True,
+ tokenize=False)
+ stop_token_ids = None
+ return ModelRequestData(
+ engine_args=engine_args,
+ prompts=prompts,
+ stop_token_ids=stop_token_ids,
+ )
+
+
# Molmo
def run_molmo(questions: list[str], modality: str) -> ModelRequestData:
assert modality == "image"
@@ -907,6 +943,7 @@ def run_skyworkr1v(questions: list[str], modality: str) -> ModelRequestData:
"minicpmv": run_minicpmv,
"mistral3": run_mistral3,
"mllama": run_mllama,
+ "llama4": run_llama4,
"molmo": run_molmo,
"NVLM_D": run_nvlm_d,
"paligemma": run_paligemma,
diff --git a/examples/offline_inference/vision_language_multi_image.py b/examples/offline_inference/vision_language_multi_image.py
index 39951e5e89c4..e03ebe485eaa 100644
--- a/examples/offline_inference/vision_language_multi_image.py
+++ b/examples/offline_inference/vision_language_multi_image.py
@@ -253,6 +253,43 @@ def load_internvl(question: str, image_urls: list[str]) -> ModelRequestData:
)
+def load_llama4(question: str, image_urls: list[str]) -> ModelRequestData:
+ model_name = "meta-llama/Llama-4-Scout-17B-16E-Instruct"
+
+ engine_args = EngineArgs(
+ model=model_name,
+ max_model_len=8192,
+ max_num_seqs=4,
+ tensor_parallel_size=8,
+ limit_mm_per_prompt={"image": len(image_urls)},
+ )
+
+ placeholders = [{"type": "image", "image": url} for url in image_urls]
+ messages = [{
+ "role":
+ "user",
+ "content": [
+ *placeholders,
+ {
+ "type": "text",
+ "text": question
+ },
+ ],
+ }]
+
+ processor = AutoProcessor.from_pretrained(model_name)
+
+ prompt = processor.apply_chat_template(messages,
+ tokenize=False,
+ add_generation_prompt=True)
+
+ return ModelRequestData(
+ engine_args=engine_args,
+ prompt=prompt,
+ image_data=[fetch_image(url) for url in image_urls],
+ )
+
+
def load_mistral3(question: str, image_urls: list[str]) -> ModelRequestData:
model_name = "mistralai/Mistral-Small-3.1-24B-Instruct-2503"
@@ -567,6 +604,7 @@ def load_qwen2_5_vl(question: str, image_urls: list[str]) -> ModelRequestData:
"h2ovl_chat": load_h2ovl,
"idefics3": load_idefics3,
"internvl_chat": load_internvl,
+ "llama4": load_llama4,
"mistral3": load_mistral3,
"mllama": load_mllama,
"NVLM_D": load_nvlm_d,
diff --git a/requirements/common.txt b/requirements/common.txt
index 7365a5b46a30..24a1e6d67ac2 100644
--- a/requirements/common.txt
+++ b/requirements/common.txt
@@ -6,7 +6,7 @@ requests >= 2.26.0
tqdm
blake3
py-cpuinfo
-transformers >= 4.50.3
+transformers >= 4.51.0
huggingface-hub[hf_xet] >= 0.30.0 # Required for Xet downloads.
tokenizers >= 0.19.1 # Required for Llama 3.
protobuf # Required by LlamaTokenizer.
diff --git a/requirements/test.in b/requirements/test.in
index 364747e9c08f..ac7f451e96a8 100644
--- a/requirements/test.in
+++ b/requirements/test.in
@@ -30,7 +30,7 @@ mistral_common[opencv] >= 1.5.4 # required for pixtral test
opencv-python-headless >= 4.11.0 # required for video test
datamodel_code_generator # required for minicpm3 test
lm-eval[api]==0.4.8 # required for model evaluation test
-transformers==4.50.3
+transformers==4.51.0
huggingface-hub[hf_xet]>=0.30.0 # Required for Xet downloads.
# quantization
bitsandbytes>=0.45.3
diff --git a/requirements/test.txt b/requirements/test.txt
index 236b8be32805..39d6ed1acff0 100644
--- a/requirements/test.txt
+++ b/requirements/test.txt
@@ -645,7 +645,7 @@ tqdm==4.66.6
# transformers
tqdm-multiprocess==0.0.11
# via lm-eval
-transformers==4.50.3
+transformers==4.51.0
# via
# -r requirements/test.in
# genai-perf
diff --git a/tests/models/decoder_only/vision_language/test_models.py b/tests/models/decoder_only/vision_language/test_models.py
index 3b34f012f626..a9386971a034 100644
--- a/tests/models/decoder_only/vision_language/test_models.py
+++ b/tests/models/decoder_only/vision_language/test_models.py
@@ -536,6 +536,22 @@
limit_mm_per_prompt={"image": 1},
)],
),
+ "llama4": VLMTestInfo(
+ models=["meta-llama/Llama-4-Scout-17B-16E-Instruct"],
+ prompt_formatter=lambda img_prompt: f"<|begin_of_text|><|header_start|>user<|header_end|>\n\n{img_prompt}<|eot|><|header_start|>assistant<|header_end|>\n\n", # noqa: E501
+ img_idx_to_prompt=lambda _: "<|image|>",
+ test_type=(VLMTestType.IMAGE, VLMTestType.MULTI_IMAGE),
+ distributed_executor_backend="mp",
+ image_size_factors=[(.25, 0.5, 1.0)],
+ hf_model_kwargs={"device_map": "auto"},
+ max_model_len=8192,
+ max_num_seqs=4,
+ dtype="bfloat16",
+ auto_cls=AutoModelForImageTextToText,
+ tensor_parallel_size=8,
+ vllm_runner_kwargs={"gpu_memory_utilization": 0.8},
+ marks=[large_gpu_mark(min_gb=80), multi_gpu_marks(num_gpus=8)],
+ ),
}
# yapf: enable
diff --git a/tests/models/multimodal/processing/test_common.py b/tests/models/multimodal/processing/test_common.py
index fdcd7a9e1738..cb4e4cdb3eee 100644
--- a/tests/models/multimodal/processing/test_common.py
+++ b/tests/models/multimodal/processing/test_common.py
@@ -280,6 +280,7 @@ def _test_processing_correctness_mistral(
"Skywork/Skywork-R1V-38B",
"fixie-ai/ultravox-v0_5-llama-3_2-1b",
"openai/whisper-large-v3",
+ "meta-llama/Llama-4-Scout-17B-16E-Instruct",
])
@pytest.mark.parametrize("hit_rate", [0.3, 0.5, 1.0])
@pytest.mark.parametrize("num_batches", [32])
diff --git a/tests/models/multimodal/processing/test_llama4.py b/tests/models/multimodal/processing/test_llama4.py
new file mode 100644
index 000000000000..7ec7c8002974
--- /dev/null
+++ b/tests/models/multimodal/processing/test_llama4.py
@@ -0,0 +1,99 @@
+# SPDX-License-Identifier: Apache-2.0
+"""Tests for Llama4's multimodal preprocessing kwargs."""
+
+import pytest
+
+from vllm.multimodal import MULTIMODAL_REGISTRY
+from vllm.transformers_utils.tokenizer import encode_tokens
+
+from ....conftest import _ImageAssets
+from ...utils import build_model_context
+
+
+@pytest.mark.parametrize("model_id",
+ ["meta-llama/Llama-4-Scout-17B-16E-Instruct"])
+@pytest.mark.parametrize("mm_processor_kwargs", [{}])
+@pytest.mark.parametrize("num_imgs", [1, 5])
+@pytest.mark.parametrize("disable_mm_preprocessor_cache", [True, False])
+@pytest.mark.parametrize("tokenized_prompt", [True, False])
+def test_processor_override(
+ image_assets: _ImageAssets,
+ model_id: str,
+ mm_processor_kwargs: dict,
+ num_imgs: int,
+ disable_mm_preprocessor_cache: bool,
+ tokenized_prompt: bool,
+):
+ """Ensure llama4 processor works properly."""
+ ctx = build_model_context(
+ model_id,
+ mm_processor_kwargs=mm_processor_kwargs,
+ limit_mm_per_prompt={"image": num_imgs},
+ disable_mm_preprocessor_cache=disable_mm_preprocessor_cache,
+ )
+ processor = MULTIMODAL_REGISTRY.create_processor(ctx.model_config)
+ config = processor.info.get_hf_config()
+ tokenizer = processor.info.get_tokenizer()
+ hf_processor = processor.info.get_hf_processor()
+ vocab = tokenizer.get_vocab()
+
+ prompt = "<|begin_of_text|><|header_start|>user<|header_end|>" \
+ + "<|image|>" * num_imgs \
+ + "<|eot|><|header_start|>assistant<|header_end|>"
+ mm_data = {
+ "image": [
+ image_assets[(i % len(image_assets))].pil_image
+ for i in range(num_imgs)
+ ]
+ }
+ if tokenized_prompt:
+ prompt = encode_tokens(tokenizer, prompt)
+
+ processed_inputs = processor.apply(prompt, mm_data, mm_processor_kwargs)
+ mm_kwargs = processed_inputs["mm_kwargs"]
+
+ # place holder replacements
+ prompt_token_ids = processed_inputs["prompt_token_ids"]
+ assert prompt_token_ids.count(config.boi_token_index) == num_imgs
+ assert prompt_token_ids.count(config.eoi_token_index) == num_imgs
+ assert prompt_token_ids.count(vocab[hf_processor.image_token]) == num_imgs
+ aspect_ratios = mm_kwargs["aspect_ratios"]
+ num_x_separators = num_y_separators = 0
+ for tiles_y, tiles_x in aspect_ratios:
+ if tiles_x * tiles_y > 1:
+ num_x_separators += (tiles_x - 1) * tiles_y
+ num_y_separators += tiles_y
+ assert prompt_token_ids.count(vocab[hf_processor.tile_token]) \
+ == num_x_separators
+ assert prompt_token_ids.count(vocab[hf_processor.tile_global_token]) \
+ == num_y_separators
+
+ # image token offsets
+ img_locs = processed_inputs["mm_placeholders"].get("image", [])
+ assert len(img_locs) == num_imgs
+ assert [img_loc["offset"] for img_loc in img_locs] == \
+ [i for i, v in enumerate(prompt_token_ids) \
+ if v == config.boi_token_index]
+
+ # patch sizes and masks
+ assert prompt_token_ids.count(config.image_token_index) \
+ == sum(img_patch.sum() for img_patch in mm_kwargs["embed_is_patch"])
+ patch_token_id = vocab[hf_processor.img_patch_token]
+ num_patches = processed_inputs["prompt_token_ids"].count(patch_token_id)
+ mm_counts = {"image": num_imgs}
+ assert num_patches / num_imgs <= \
+ processor.info.get_mm_max_tokens_per_item(32768, mm_counts)["image"]
+ num_patches_per_chunk = processor.info.get_patch_per_chunk(
+ config.vision_config)
+ assert prompt_token_ids.count(config.image_token_index) \
+ == mm_kwargs["patches_per_image"].sum() * num_patches_per_chunk
+ assert mm_kwargs["pixel_values"].shape[0] \
+ == mm_kwargs["patches_per_image"].sum()
+
+ for embed_is_patch, aspect_ratio in zip(mm_kwargs["embed_is_patch"],
+ mm_kwargs["aspect_ratios"]):
+ assert embed_is_patch.shape[0] == \
+ len(tokenizer.encode(
+ hf_processor._prompt_split_image(
+ aspect_ratio, num_patches_per_chunk),
+ add_special_tokens=False))
diff --git a/tests/models/registry.py b/tests/models/registry.py
index 39e104a11ab1..d508c5f44dab 100644
--- a/tests/models/registry.py
+++ b/tests/models/registry.py
@@ -337,6 +337,7 @@ def check_available_online(
tokenizer="facebook/bart-base",
trust_remote_code=True), # noqa: E501
"MllamaForConditionalGeneration": _HfExamplesInfo("meta-llama/Llama-3.2-11B-Vision-Instruct"), # noqa: E501
+ "Llama4ForConditionalGeneration": _HfExamplesInfo("meta-llama/Llama-4-Scout-17B-16E-Instruct"), # noqa: E501
"WhisperForConditionalGeneration": _HfExamplesInfo("openai/whisper-large-v3"), # noqa: E501
}
diff --git a/tests/models/test_registry.py b/tests/models/test_registry.py
index 3282284b6b27..4c5572a569ea 100644
--- a/tests/models/test_registry.py
+++ b/tests/models/test_registry.py
@@ -23,6 +23,11 @@
@pytest.mark.parametrize("model_arch", ModelRegistry.get_supported_archs())
def test_registry_imports(model_arch):
+
+ # Llama4ForCausalLM does not have a standalone model
+ if model_arch == "Llama4ForCausalLM":
+ return
+
model_info = HF_EXAMPLE_MODELS.get_hf_info(model_arch)
model_info.check_transformers_version(on_fail="skip")
@@ -91,8 +96,11 @@ def test_registry_is_pp(model_arch, is_pp, init_cuda):
def test_hf_registry_coverage():
- untested_archs = (ModelRegistry.get_supported_archs() -
- HF_EXAMPLE_MODELS.get_supported_archs())
+ untested_archs = set(ModelRegistry.get_supported_archs() -
+ HF_EXAMPLE_MODELS.get_supported_archs())
+
+ # Llama4ForCausalLM does not have a standalone model
+ untested_archs.discard("Llama4ForCausalLM")
assert not untested_archs, (
"Please add the following architectures to "
diff --git a/vllm/config.py b/vllm/config.py
index 2669d1a13b37..bd52fc90b0a2 100644
--- a/vllm/config.py
+++ b/vllm/config.py
@@ -354,6 +354,8 @@ def __init__(
self.hf_config = hf_config
self.hf_text_config = get_hf_text_config(self.hf_config)
+ self.attention_chunk_size = getattr(self.hf_text_config,
+ "attention_chunk_size", None)
self.encoder_config = self._get_encoder_config()
self.hf_image_processor_config = get_hf_image_processor_config(
self.model, revision)
diff --git a/vllm/entrypoints/chat_utils.py b/vllm/entrypoints/chat_utils.py
index ff2d1aacbece..e48ffae9bf58 100644
--- a/vllm/entrypoints/chat_utils.py
+++ b/vllm/entrypoints/chat_utils.py
@@ -500,7 +500,7 @@ def _placeholder_str(self, modality: ModalityStr,
"internvl_chat", "skywork_chat", "NVLM_D",
"h2ovl_chat"):
return "<image>"
- if model_type == "mllama":
+ if model_type in ("mllama", "llama4"):
return "<|image|>"
if model_type in ("qwen2_vl", "qwen2_5_vl"):
return "<|vision_start|><|image_pad|><|vision_end|>"
diff --git a/vllm/model_executor/layers/fused_moe/configs/E=16,N=1024,device_name=AMD_Instinct_MI300X.json b/vllm/model_executor/layers/fused_moe/configs/E=16,N=1024,device_name=AMD_Instinct_MI300X.json
new file mode 100644
index 000000000000..f10e39482e58
--- /dev/null
+++ b/vllm/model_executor/layers/fused_moe/configs/E=16,N=1024,device_name=AMD_Instinct_MI300X.json
@@ -0,0 +1,200 @@
+{
+ "1": {
+ "BLOCK_SIZE_M": 16,
+ "BLOCK_SIZE_N": 16,
+ "BLOCK_SIZE_K": 256,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 4,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 1
+ },
+ "2": {
+ "BLOCK_SIZE_M": 16,
+ "BLOCK_SIZE_N": 16,
+ "BLOCK_SIZE_K": 256,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 4,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 1
+ },
+ "4": {
+ "BLOCK_SIZE_M": 16,
+ "BLOCK_SIZE_N": 16,
+ "BLOCK_SIZE_K": 256,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 2,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 1
+ },
+ "8": {
+ "BLOCK_SIZE_M": 16,
+ "BLOCK_SIZE_N": 64,
+ "BLOCK_SIZE_K": 128,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 4,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 2
+ },
+ "16": {
+ "BLOCK_SIZE_M": 16,
+ "BLOCK_SIZE_N": 32,
+ "BLOCK_SIZE_K": 128,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 2,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 1
+ },
+ "24": {
+ "BLOCK_SIZE_M": 16,
+ "BLOCK_SIZE_N": 64,
+ "BLOCK_SIZE_K": 256,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 4,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 2
+ },
+ "32": {
+ "BLOCK_SIZE_M": 32,
+ "BLOCK_SIZE_N": 64,
+ "BLOCK_SIZE_K": 128,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 4,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 1
+ },
+ "48": {
+ "BLOCK_SIZE_M": 16,
+ "BLOCK_SIZE_N": 64,
+ "BLOCK_SIZE_K": 128,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 4,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 1
+ },
+ "64": {
+ "BLOCK_SIZE_M": 16,
+ "BLOCK_SIZE_N": 64,
+ "BLOCK_SIZE_K": 128,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 2,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 2
+ },
+ "96": {
+ "BLOCK_SIZE_M": 16,
+ "BLOCK_SIZE_N": 64,
+ "BLOCK_SIZE_K": 256,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 4,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 2
+ },
+ "128": {
+ "BLOCK_SIZE_M": 16,
+ "BLOCK_SIZE_N": 64,
+ "BLOCK_SIZE_K": 256,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 4,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 2
+ },
+ "256": {
+ "BLOCK_SIZE_M": 32,
+ "BLOCK_SIZE_N": 32,
+ "BLOCK_SIZE_K": 256,
+ "GROUP_SIZE_M": 8,
+ "num_warps": 4,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 2
+ },
+ "512": {
+ "BLOCK_SIZE_M": 64,
+ "BLOCK_SIZE_N": 64,
+ "BLOCK_SIZE_K": 128,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 8,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 2
+ },
+ "1024": {
+ "BLOCK_SIZE_M": 64,
+ "BLOCK_SIZE_N": 64,
+ "BLOCK_SIZE_K": 128,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 8,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 2
+ },
+ "1536": {
+ "BLOCK_SIZE_M": 128,
+ "BLOCK_SIZE_N": 128,
+ "BLOCK_SIZE_K": 128,
+ "GROUP_SIZE_M": 8,
+ "num_warps": 8,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 2
+ },
+ "2048": {
+ "BLOCK_SIZE_M": 64,
+ "BLOCK_SIZE_N": 64,
+ "BLOCK_SIZE_K": 128,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 8,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 2
+ },
+ "3072": {
+ "BLOCK_SIZE_M": 128,
+ "BLOCK_SIZE_N": 128,
+ "BLOCK_SIZE_K": 64,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 8,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 2
+ },
+ "4096": {
+ "BLOCK_SIZE_M": 64,
+ "BLOCK_SIZE_N": 128,
+ "BLOCK_SIZE_K": 64,
+ "GROUP_SIZE_M": 1,
+ "num_warps": 4,
+ "num_stages": 2,
+ "waves_per_eu": 0,
+ "matrix_instr_nonkdim": 16,
+ "kpack": 2
+ }
+}
diff --git a/vllm/model_executor/layers/fused_moe/cutlass_moe.py b/vllm/model_executor/layers/fused_moe/cutlass_moe.py
index a17afd1b357e..d6a27aa0ddc4 100644
--- a/vllm/model_executor/layers/fused_moe/cutlass_moe.py
+++ b/vllm/model_executor/layers/fused_moe/cutlass_moe.py
@@ -23,6 +23,7 @@ def cutlass_moe_fp8(
a1_scale: Optional[torch.Tensor] = None,
a2_scale: Optional[torch.Tensor] = None,
out_dtype: torch.dtype = torch.half,
+ apply_router_weight_on_input: bool = False,
) -> torch.Tensor:
"""
This function computes a a8w8-quantized Mixture of Experts (MoE) layer
@@ -96,8 +97,14 @@ def cutlass_moe_fp8(
n = w2_q.size(1)
topk = topk_ids.size(1)
+
per_act_token = a1_scale.numel() != 1 if a1_scale is not None else (
a2_scale.numel() != 1 if a2_scale is not None else False)
+ if apply_router_weight_on_input:
+ assert topk == 1, \
+ "apply_router_weight_on_input is only implemented for topk=1"
+ # TODO: this only works for topK=1, will need to update for topK>1
+ a = a * topk_weights.to(out_dtype)
a_q, a1_scale = ops.scaled_fp8_quant(
a, a1_scale, use_per_token_if_dynamic=per_act_token)
@@ -139,6 +146,8 @@ def cutlass_moe_fp8(
ops.cutlass_moe_mm(c2, intemediate_q, w2_q, a2_scale, w2_scale,
expert_offsets[:-1], problem_sizes2, ab_strides2,
ab_strides2, c_strides2)
-
- return (c2[c_map].view(m, topk, k) *
- topk_weights.view(m, topk, 1).to(out_dtype)).sum(dim=1)
+ # Gather tokens
+ c2 = c2[c_map].view(m, topk, k)
+ if not apply_router_weight_on_input:
+ c2 = c2 * topk_weights.view(m, topk, 1).to(out_dtype)
+ return c2.sum(dim=1)
diff --git a/vllm/model_executor/layers/fused_moe/fused_moe.py b/vllm/model_executor/layers/fused_moe/fused_moe.py
index 0817879c4d57..4ab99acb742f 100644
--- a/vllm/model_executor/layers/fused_moe/fused_moe.py
+++ b/vllm/model_executor/layers/fused_moe/fused_moe.py
@@ -954,6 +954,7 @@ def inplace_fused_experts(hidden_states: torch.Tensor,
topk_weights: torch.Tensor,
topk_ids: torch.Tensor,
activation: str = "silu",
+ apply_router_weight_on_input: bool = False,
use_fp8_w8a8: bool = False,
use_int8_w8a16: bool = False,
use_int4_w4a16: bool = False,
@@ -967,10 +968,10 @@ def inplace_fused_experts(hidden_states: torch.Tensor,
a2_scale: Optional[torch.Tensor] = None,
block_shape: Optional[List[int]] = None) -> None:
fused_experts_impl(hidden_states, w1, w2, topk_weights, topk_ids, True,
- activation, use_fp8_w8a8, use_int8_w8a16,
- use_int4_w4a16, global_num_experts, expert_map,
- w1_scale, w2_scale, w1_zp, w2_zp, a1_scale, a2_scale,
- block_shape)
+ activation, apply_router_weight_on_input, use_fp8_w8a8,
+ use_int8_w8a16, use_int4_w4a16, global_num_experts,
+ expert_map, w1_scale, w2_scale, w1_zp, w2_zp, a1_scale,
+ a2_scale, block_shape)
def inplace_fused_experts_fake(
@@ -980,6 +981,7 @@ def inplace_fused_experts_fake(
topk_weights: torch.Tensor,
topk_ids: torch.Tensor,
activation: str = "silu",
+ apply_router_weight_on_input: bool = False,
use_fp8_w8a8: bool = False,
use_int8_w8a16: bool = False,
use_int4_w4a16: bool = False,
@@ -1010,6 +1012,7 @@ def outplace_fused_experts(
topk_weights: torch.Tensor,
topk_ids: torch.Tensor,
activation: str = "silu",
+ apply_router_weight_on_input: bool = False,
use_fp8_w8a8: bool = False,
use_int8_w8a16: bool = False,
use_int4_w4a16: bool = False,
@@ -1023,10 +1026,11 @@ def outplace_fused_experts(
a2_scale: Optional[torch.Tensor] = None,
block_shape: Optional[List[int]] = None) -> torch.Tensor:
return fused_experts_impl(hidden_states, w1, w2, topk_weights, topk_ids,
- False, activation, use_fp8_w8a8, use_int8_w8a16,
- use_int4_w4a16, global_num_experts, expert_map,
- w1_scale, w2_scale, w1_zp, w2_zp, a1_scale,
- a2_scale, block_shape)
+ False, activation, apply_router_weight_on_input,
+ use_fp8_w8a8, use_int8_w8a16, use_int4_w4a16,
+ global_num_experts, expert_map, w1_scale,
+ w2_scale, w1_zp, w2_zp, a1_scale, a2_scale,
+ block_shape)
def outplace_fused_experts_fake(
@@ -1084,6 +1088,7 @@ def fused_experts(hidden_states: torch.Tensor,
topk_ids: torch.Tensor,
inplace: bool = False,
activation: str = "silu",
+ apply_router_weight_on_input: bool = False,
use_fp8_w8a8: bool = False,
use_int8_w8a16: bool = False,
use_int4_w4a16: bool = False,
@@ -1099,6 +1104,7 @@ def fused_experts(hidden_states: torch.Tensor,
allow_deep_gemm: bool = False) -> torch.Tensor:
if (allow_deep_gemm and use_fp8_w8a8
and _valid_deep_gemm(hidden_states, w1, w2, expert_map)):
+ assert apply_router_weight_on_input is False
return deep_gemm_moe_fp8(
hidden_states=hidden_states,
w1=w1,
@@ -1122,6 +1128,7 @@ def fused_experts(hidden_states: torch.Tensor,
topk_weights=topk_weights,
topk_ids=topk_ids,
activation=activation,
+ apply_router_weight_on_input=apply_router_weight_on_input,
use_fp8_w8a8=use_fp8_w8a8,
use_int8_w8a16=use_int8_w8a16,
use_int4_w4a16=use_int4_w4a16,
@@ -1143,6 +1150,7 @@ def fused_experts_impl(hidden_states: torch.Tensor,
topk_ids: torch.Tensor,
inplace: bool = False,
activation: str = "silu",
+ apply_router_weight_on_input: bool = False,
use_fp8_w8a8: bool = False,
use_int8_w8a16: bool = False,
use_int4_w4a16: bool = False,
@@ -1270,7 +1278,7 @@ def fused_experts_impl(hidden_states: torch.Tensor,
sorted_token_ids,
expert_ids,
num_tokens_post_padded,
- False,
+ apply_router_weight_on_input,
top_k_num,
config,
compute_type=compute_type,
@@ -1307,7 +1315,7 @@ def fused_experts_impl(hidden_states: torch.Tensor,
sorted_token_ids,
expert_ids,
num_tokens_post_padded,
- True,
+ not apply_router_weight_on_input,
1,
config,
compute_type=compute_type,
diff --git a/vllm/model_executor/layers/fused_moe/layer.py b/vllm/model_executor/layers/fused_moe/layer.py
index 661fb52bbee2..0e35d8a80988 100644
--- a/vllm/model_executor/layers/fused_moe/layer.py
+++ b/vllm/model_executor/layers/fused_moe/layer.py
@@ -65,7 +65,9 @@ def apply(
expert_map: Optional[torch.Tensor] = None,
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
- e_score_correction_bias: Optional[torch.Tensor] = None
+ e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
+ activation: str = "silu",
) -> torch.Tensor:
raise NotImplementedError
@@ -156,22 +158,25 @@ def apply(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
activation: str = "silu",
) -> torch.Tensor:
- return self.forward(x=x,
- layer=layer,
- router_logits=router_logits,
- top_k=top_k,
- renormalize=renormalize,
- use_grouped_topk=use_grouped_topk,
- topk_group=topk_group,
- num_expert_group=num_expert_group,
- global_num_experts=global_num_experts,
- expert_map=expert_map,
- custom_routing_function=custom_routing_function,
- scoring_func=scoring_func,
- e_score_correction_bias=e_score_correction_bias,
- activation=activation)
+ return self.forward(
+ x=x,
+ layer=layer,
+ router_logits=router_logits,
+ top_k=top_k,
+ renormalize=renormalize,
+ use_grouped_topk=use_grouped_topk,
+ topk_group=topk_group,
+ num_expert_group=num_expert_group,
+ global_num_experts=global_num_experts,
+ expert_map=expert_map,
+ custom_routing_function=custom_routing_function,
+ scoring_func=scoring_func,
+ e_score_correction_bias=e_score_correction_bias,
+ activation=activation,
+ apply_router_weight_on_input=apply_router_weight_on_input)
def forward_cuda(
self,
@@ -188,6 +193,7 @@ def forward_cuda(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
activation: str = "silu",
) -> torch.Tensor:
topk_weights, topk_ids = FusedMoE.select_experts(
@@ -202,15 +208,17 @@ def forward_cuda(
scoring_func=scoring_func,
e_score_correction_bias=e_score_correction_bias)
- return fused_experts(hidden_states=x,
- w1=layer.w13_weight,
- w2=layer.w2_weight,
- topk_weights=topk_weights,
- topk_ids=topk_ids,
- inplace=True,
- activation=activation,
- global_num_experts=global_num_experts,
- expert_map=expert_map)
+ return fused_experts(
+ hidden_states=x,
+ w1=layer.w13_weight,
+ w2=layer.w2_weight,
+ topk_weights=topk_weights,
+ topk_ids=topk_ids,
+ inplace=True,
+ activation=activation,
+ apply_router_weight_on_input=apply_router_weight_on_input,
+ global_num_experts=global_num_experts,
+ expert_map=expert_map)
def forward_cpu(
self,
@@ -228,9 +236,11 @@ def forward_cpu(
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
activation: str = "silu",
+ apply_router_weight_on_input: bool = False,
**kwargs,
):
assert activation == "silu", f"{activation} is not supported."
+ assert apply_router_weight_on_input is False
return layer.ipex_fusion(
x,
use_grouped_topk,
@@ -259,6 +269,7 @@ def forward_hpu(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
activation: str = "silu",
) -> torch.Tensor:
assert not use_grouped_topk
@@ -266,6 +277,7 @@ def forward_hpu(
assert topk_group is None
assert custom_routing_function is None
assert layer is not None
+ assert apply_router_weight_on_input is False
if scoring_func != "softmax":
raise NotImplementedError(
"Only softmax scoring function is supported for HPU.")
@@ -290,12 +302,14 @@ def forward_tpu(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
activation: str = "silu",
) -> torch.Tensor:
assert not use_grouped_topk
assert num_expert_group is None
assert topk_group is None
assert custom_routing_function is None
+ assert apply_router_weight_on_input is False
if scoring_func != "softmax":
raise NotImplementedError(
"Only softmax scoring function is supported for TPU.")
@@ -401,6 +415,7 @@ def __init__(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
activation: str = "silu",
):
super().__init__()
@@ -486,6 +501,7 @@ def __init__(
self.quant_method = quant_config.get_quant_method(self, prefix)
assert self.quant_method is not None
+ self.apply_router_weight_on_input = apply_router_weight_on_input
moe_quant_params = {
"num_experts": self.local_num_experts,
"hidden_size": hidden_size,
@@ -853,6 +869,7 @@ def forward_impl(self, hidden_states: torch.Tensor,
scoring_func=self.scoring_func,
e_score_correction_bias=self.e_score_correction_bias,
activation=self.activation,
+ apply_router_weight_on_input=self.apply_router_weight_on_input,
)
if self.dp_size > 1:
diff --git a/vllm/model_executor/layers/layernorm.py b/vllm/model_executor/layers/layernorm.py
index 76d3acb92fb8..5e8eb6c54c89 100644
--- a/vllm/model_executor/layers/layernorm.py
+++ b/vllm/model_executor/layers/layernorm.py
@@ -92,6 +92,7 @@ def __init__(
eps: float = 1e-6,
var_hidden_size: Optional[int] = None,
has_weight: bool = True,
+ dtype: Optional[torch.dtype] = None,
) -> None:
super().__init__()
@@ -100,8 +101,10 @@ def __init__(
self.variance_size_override = (None if var_hidden_size == hidden_size
else var_hidden_size)
self.has_weight = has_weight
-
- self.weight = torch.ones(hidden_size)
+ if dtype is not None:
+ self.weight = torch.ones(hidden_size, dtype=dtype)
+ else:
+ self.weight = torch.ones(hidden_size)
if self.has_weight:
self.weight = nn.Parameter(self.weight)
diff --git a/vllm/model_executor/layers/quantization/awq_marlin.py b/vllm/model_executor/layers/quantization/awq_marlin.py
index 473816fcc3ec..cb1d5400f3a0 100644
--- a/vllm/model_executor/layers/quantization/awq_marlin.py
+++ b/vllm/model_executor/layers/quantization/awq_marlin.py
@@ -469,6 +469,7 @@ def apply(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
activation: str = "silu",
) -> torch.Tensor:
assert activation == "silu", "Only SiLU activation is supported."
@@ -476,6 +477,10 @@ def apply(
raise NotImplementedError(
"Expert Parallelism is not supported for "
"fused Marlin MoE method.")
+ if apply_router_weight_on_input:
+ raise NotImplementedError(
+ "Apply router weight on input is not supported for"
+ "fused Marlin MoE method.")
topk_weights, topk_ids = FusedMoE.select_experts(
hidden_states=x,
diff --git a/vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py b/vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py
index bf32bee89e89..f573c8ae5131 100644
--- a/vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py
+++ b/vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py
@@ -224,6 +224,7 @@ def apply(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
activation: str = "silu",
) -> torch.Tensor:
from vllm.model_executor.layers.fused_moe import fused_experts
@@ -240,20 +241,22 @@ def apply(
scoring_func=scoring_func,
e_score_correction_bias=e_score_correction_bias)
- return fused_experts(x,
- layer.w13_weight,
- layer.w2_weight,
- topk_weights=topk_weights,
- topk_ids=topk_ids,
- inplace=True,
- activation=activation,
- use_fp8_w8a8=True,
- global_num_experts=global_num_experts,
- expert_map=expert_map,
- w1_scale=layer.w13_weight_scale,
- w2_scale=layer.w2_weight_scale,
- a1_scale=layer.w13_input_scale,
- a2_scale=layer.w2_input_scale)
+ return fused_experts(
+ x,
+ layer.w13_weight,
+ layer.w2_weight,
+ topk_weights=topk_weights,
+ topk_ids=topk_ids,
+ inplace=True,
+ activation=activation,
+ apply_router_weight_on_input=apply_router_weight_on_input,
+ use_fp8_w8a8=True,
+ global_num_experts=global_num_experts,
+ expert_map=expert_map,
+ w1_scale=layer.w13_weight_scale,
+ w2_scale=layer.w2_weight_scale,
+ a1_scale=layer.w13_input_scale,
+ a2_scale=layer.w2_input_scale)
class CompressedTensorsW8A8Fp8MoECutlassMethod(CompressedTensorsMoEMethod):
@@ -438,6 +441,7 @@ def apply(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
activation: str = "silu",
) -> torch.Tensor:
@@ -474,6 +478,7 @@ def apply(
a1_scale=layer.w13_input_scale,
a2_scale=layer.w2_input_scale,
out_dtype=x.dtype,
+ apply_router_weight_on_input=apply_router_weight_on_input,
)
@@ -778,6 +783,7 @@ def apply(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
activation: str = "silu",
) -> torch.Tensor:
assert activation == "silu", "Only SiLU activation is supported."
@@ -785,6 +791,10 @@ def apply(
raise NotImplementedError(
"Expert Parallelism is not supported for "
"fused Marlin MoE method.")
+ if apply_router_weight_on_input:
+ raise NotImplementedError(
+ "Apply router weight on input is not supported for "
+ "fused Marlin MoE method.")
topk_weights, topk_ids = FusedMoE.select_experts(
hidden_states=x,
diff --git a/vllm/model_executor/layers/quantization/experts_int8.py b/vllm/model_executor/layers/quantization/experts_int8.py
index d18ca55afebd..be19b80975ec 100644
--- a/vllm/model_executor/layers/quantization/experts_int8.py
+++ b/vllm/model_executor/layers/quantization/experts_int8.py
@@ -113,6 +113,7 @@ def apply(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
activation: str = "silu",
) -> torch.Tensor:
from vllm.model_executor.layers.fused_moe import fused_experts
@@ -129,18 +130,20 @@ def apply(
scoring_func=scoring_func,
e_score_correction_bias=e_score_correction_bias)
- return fused_experts(x,
- layer.w13_weight,
- layer.w2_weight,
- topk_weights=topk_weights,
- topk_ids=topk_ids,
- inplace=True,
- activation=activation,
- use_int8_w8a16=True,
- global_num_experts=global_num_experts,
- expert_map=expert_map,
- w1_scale=layer.w13_scale,
- w2_scale=layer.w2_scale)
+ return fused_experts(
+ x,
+ layer.w13_weight,
+ layer.w2_weight,
+ topk_weights=topk_weights,
+ topk_ids=topk_ids,
+ inplace=True,
+ activation=activation,
+ use_int8_w8a16=True,
+ global_num_experts=global_num_experts,
+ apply_router_weight_on_input=apply_router_weight_on_input,
+ expert_map=expert_map,
+ w1_scale=layer.w13_scale,
+ w2_scale=layer.w2_scale)
@staticmethod
def quantizing_weight_loader(layer, weight_loader):
diff --git a/vllm/model_executor/layers/quantization/fp8.py b/vllm/model_executor/layers/quantization/fp8.py
index e7c733db5c00..4435644c4f84 100644
--- a/vllm/model_executor/layers/quantization/fp8.py
+++ b/vllm/model_executor/layers/quantization/fp8.py
@@ -773,6 +773,7 @@ def apply(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
activation: str = "silu",
) -> torch.Tensor:
from vllm.model_executor.layers.fused_moe import fused_experts
@@ -800,6 +801,7 @@ def apply(
activation=activation,
use_fp8_w8a8=True,
global_num_experts=global_num_experts,
+ apply_router_weight_on_input=apply_router_weight_on_input,
expert_map=expert_map,
w1_scale=(layer.w13_weight_scale_inv
if self.block_quant else layer.w13_weight_scale),
diff --git a/vllm/model_executor/layers/quantization/gguf.py b/vllm/model_executor/layers/quantization/gguf.py
index 9861e0a85b3f..6b499f81c55f 100644
--- a/vllm/model_executor/layers/quantization/gguf.py
+++ b/vllm/model_executor/layers/quantization/gguf.py
@@ -338,9 +338,15 @@ def apply(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
activation: str = "silu",
):
assert activation == "silu", "Only SiLU activation is supported."
+ if apply_router_weight_on_input:
+ raise NotImplementedError(
+ "Apply router weight on input is not supported for"
+ "fused GGUF MoE method.")
+
topk_weights, topk_ids = FusedMoE.select_experts(
hidden_states=x,
router_logits=router_logits,
diff --git a/vllm/model_executor/layers/quantization/gptq_marlin.py b/vllm/model_executor/layers/quantization/gptq_marlin.py
index 9f53ffc1d7f6..0615bb4ab4df 100644
--- a/vllm/model_executor/layers/quantization/gptq_marlin.py
+++ b/vllm/model_executor/layers/quantization/gptq_marlin.py
@@ -592,9 +592,14 @@ def apply(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
activation: str = "silu",
) -> torch.Tensor:
assert activation == "silu", "Only SiLU activation is supported."
+ if apply_router_weight_on_input is not None:
+ raise NotImplementedError(
+ "Apply router weight on input is not supported for"
+ "fused Marlin MoE method.")
# The input must currently be float16
orig_dtype = x.dtype
diff --git a/vllm/model_executor/layers/quantization/moe_wna16.py b/vllm/model_executor/layers/quantization/moe_wna16.py
index 41b75c9be05a..00c4b661ef2c 100644
--- a/vllm/model_executor/layers/quantization/moe_wna16.py
+++ b/vllm/model_executor/layers/quantization/moe_wna16.py
@@ -293,6 +293,7 @@ def apply(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
activation: str = "silu",
) -> torch.Tensor:
from vllm.model_executor.layers.fused_moe import fused_experts
@@ -312,21 +313,23 @@ def apply(
weight_bits = self.quant_config.weight_bits
has_zp = self.quant_config.has_zp
- return fused_experts(x,
- layer.w13_qweight,
- layer.w2_qweight,
- topk_weights=topk_weights,
- topk_ids=topk_ids,
- inplace=True,
- use_int4_w4a16=weight_bits == 4,
- use_int8_w8a16=weight_bits == 8,
- global_num_experts=global_num_experts,
- expert_map=expert_map,
- w1_scale=layer.w13_scales,
- w2_scale=layer.w2_scales,
- w1_zp=layer.w13_qzeros if has_zp else None,
- w2_zp=layer.w2_qzeros if has_zp else None,
- block_shape=[0, layer.group_size])
+ return fused_experts(
+ x,
+ layer.w13_qweight,
+ layer.w2_qweight,
+ topk_weights=topk_weights,
+ topk_ids=topk_ids,
+ inplace=True,
+ use_int4_w4a16=weight_bits == 4,
+ use_int8_w8a16=weight_bits == 8,
+ global_num_experts=global_num_experts,
+ apply_router_weight_on_input=apply_router_weight_on_input,
+ expert_map=expert_map,
+ w1_scale=layer.w13_scales,
+ w2_scale=layer.w2_scales,
+ w1_zp=layer.w13_qzeros if has_zp else None,
+ w2_zp=layer.w2_qzeros if has_zp else None,
+ block_shape=[0, layer.group_size])
@staticmethod
def get_weight_loader(layer, weight_loader):
diff --git a/vllm/model_executor/layers/quantization/quark/quark_moe.py b/vllm/model_executor/layers/quantization/quark/quark_moe.py
index bc26a455c6f2..d1146c0f039d 100644
--- a/vllm/model_executor/layers/quantization/quark/quark_moe.py
+++ b/vllm/model_executor/layers/quantization/quark/quark_moe.py
@@ -202,6 +202,8 @@ def apply(
custom_routing_function: Optional[Callable] = None,
scoring_func: str = "softmax",
e_score_correction_bias: Optional[torch.Tensor] = None,
+ apply_router_weight_on_input: bool = False,
+ activation: str = "silu",
) -> torch.Tensor:
from vllm.model_executor.layers.fused_moe import fused_experts
@@ -217,16 +219,18 @@ def apply(
scoring_func=scoring_func,
e_score_correction_bias=e_score_correction_bias)
- return fused_experts(x,
- layer.w13_weight,
- layer.w2_weight,
- topk_weights=topk_weights,
- topk_ids=topk_ids,
- inplace=True,
- use_fp8_w8a8=True,
- global_num_experts=global_num_experts,
- expert_map=expert_map,
- w1_scale=layer.w13_weight_scale,
- w2_scale=layer.w2_weight_scale,
- a1_scale=layer.w13_input_scale,
- a2_scale=layer.w2_input_scale)
+ return fused_experts(
+ x,
+ layer.w13_weight,
+ layer.w2_weight,
+ topk_weights=topk_weights,
+ topk_ids=topk_ids,
+ inplace=True,
+ use_fp8_w8a8=True,
+ global_num_experts=global_num_experts,
+ apply_router_weight_on_input=apply_router_weight_on_input,
+ expert_map=expert_map,
+ w1_scale=layer.w13_weight_scale,
+ w2_scale=layer.w2_weight_scale,
+ a1_scale=layer.w13_input_scale,
+ a2_scale=layer.w2_input_scale)
diff --git a/vllm/model_executor/layers/rotary_embedding.py b/vllm/model_executor/layers/rotary_embedding.py
index fd27775b7dc0..624ed63ab8b4 100644
--- a/vllm/model_executor/layers/rotary_embedding.py
+++ b/vllm/model_executor/layers/rotary_embedding.py
@@ -851,6 +851,70 @@ def _compute_inv_freq(self, base: Union[int, float]) -> torch.Tensor:
return new_freqs
+class Llama4VisionRotaryEmbedding(RotaryEmbedding):
+
+ def __init__(
+ self,
+ head_size: int,
+ rotary_dim: int,
+ max_position_embeddings: int,
+ base: int,
+ is_neox_style: bool,
+ dtype: torch.dtype,
+ ):
+ super().__init__(head_size, rotary_dim, max_position_embeddings, base,
+ is_neox_style, dtype)
+
+ def _compute_inv_freq(self, base: Union[int, float]) -> torch.Tensor:
+ inv_freqs = super()._compute_inv_freq(base)
+ inv_freqs = inv_freqs[:(self.rotary_dim // 2)]
+ return inv_freqs
+
+ def _compute_cos_sin_cache(self) -> torch.Tensor:
+ inv_freq = self._compute_inv_freq(self.base)
+
+ # self.max_position_embeddings here is number of image patches
+ # i.e. (image_size // patch_size) ** 2
+ num_patches = self.max_position_embeddings
+ img_idx = torch.arange(num_patches,
+ dtype=torch.int32) \
+ .reshape(num_patches, 1)
+ img_idx = torch.cat([img_idx, img_idx[:1]], dim=0)
+ img_idx[-1, -1] = -2 # set to ID_CLS_TOKEN
+ num_patches_single_dim = int(math.sqrt(num_patches))
+ frequencies_x = img_idx % num_patches_single_dim
+ frequencies_y = img_idx // num_patches_single_dim
+ freqs_x = ((frequencies_x + 1)[..., None] *
+ inv_freq[None, None, :]).repeat_interleave(2, dim=-1)
+ freqs_y = ((frequencies_y + 1)[..., None] *
+ inv_freq[None, None, :]).repeat_interleave(2, dim=-1)
+ freqs = torch.cat([freqs_x, freqs_y],
+ dim=-1).float().contiguous()[..., ::2]
+ freqs = freqs.masked_fill(img_idx.reshape(-1, 1, 1) < 0, 0)
+ cache = torch.view_as_complex(
+ torch.stack([torch.cos(freqs), torch.sin(freqs)], dim=-1))
+ return cache
+
+ def forward(
+ self,
+ query: torch.Tensor,
+ key: torch.Tensor,
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
+ self.cos_sin_cache: torch.Tensor = self.cos_sin_cache.to(query.device)
+ query_ = torch.view_as_complex(query.float().reshape(
+ *query.shape[:-1], -1, 2))
+ key_ = torch.view_as_complex(key.float().reshape(
+ *key.shape[:-1], -1, 2))
+ broadcast_shape = [
+ d if i == 1 or i == (query_.ndim - 1) else 1
+ for i, d in enumerate(query_.shape)
+ ]
+ freqs_ci = self.cos_sin_cache.view(*broadcast_shape)
+ query_out = torch.view_as_real(query_ * freqs_ci).flatten(3)
+ key_out = torch.view_as_real(key_ * freqs_ci).flatten(3)
+ return query_out.type_as(query), key_out.type_as(key)
+
+
class MRotaryEmbedding(RotaryEmbedding):
"""Rotary Embedding with Multimodal Sections."""
@@ -1130,6 +1194,10 @@ def get_rope(
scaling_factor, low_freq_factor,
high_freq_factor,
original_max_position)
+ elif scaling_type == "mllama4":
+ rotary_emb = Llama4VisionRotaryEmbedding(head_size, rotary_dim,
+ max_position, base,
+ is_neox_style, dtype)
elif scaling_type == "default":
if "mrope_section" in rope_scaling:
rotary_emb = MRotaryEmbedding(
diff --git a/vllm/model_executor/models/llama.py b/vllm/model_executor/models/llama.py
index 81b5d9bda9ac..4a5982ecbcb6 100644
--- a/vllm/model_executor/models/llama.py
+++ b/vllm/model_executor/models/llama.py
@@ -65,6 +65,7 @@ def __init__(
quant_config: Optional[QuantizationConfig] = None,
bias: bool = False,
prefix: str = "",
+ reduce_results: bool = True,
) -> None:
super().__init__()
self.gate_up_proj = MergedColumnParallelLinear(
@@ -79,6 +80,7 @@ def __init__(
output_size=hidden_size,
bias=bias,
quant_config=quant_config,
+ reduce_results=reduce_results,
prefix=f"{prefix}.down_proj",
)
if hidden_act != "silu":
@@ -466,10 +468,14 @@ class LlamaForCausalLM(nn.Module, SupportsLoRA, SupportsPP):
"ffn_norm": "post_attention_layernorm",
"tok_embeddings": "model.embed_tokens",
"output": "lm_head",
- "norm": "model.norm"
+ "norm": "model.norm",
}
- def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
+ def __init__(self,
+ *,
+ vllm_config: VllmConfig,
+ prefix: str = "",
+ layer_type: Type[LlamaDecoderLayer] = LlamaDecoderLayer):
super().__init__()
config = vllm_config.model_config.hf_config
quant_config = vllm_config.quant_config
@@ -478,7 +484,8 @@ def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
self.lora_config = lora_config
self.model = self._init_model(vllm_config=vllm_config,
- prefix=maybe_prefix(prefix, "model"))
+ prefix=maybe_prefix(prefix, "model"),
+ layer_type=layer_type)
if get_pp_group().is_last_rank:
self.unpadded_vocab_size = config.vocab_size
@@ -513,8 +520,13 @@ def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
self.make_empty_intermediate_tensors = (
self.model.make_empty_intermediate_tensors)
- def _init_model(self, vllm_config: VllmConfig, prefix: str = ""):
- return LlamaModel(vllm_config=vllm_config, prefix=prefix)
+ def _init_model(self,
+ vllm_config: VllmConfig,
+ prefix: str = "",
+ layer_type: Type[LlamaDecoderLayer] = LlamaDecoderLayer):
+ return LlamaModel(vllm_config=vllm_config,
+ prefix=prefix,
+ layer_type=layer_type)
def get_input_embeddings(self, input_ids: torch.Tensor) -> torch.Tensor:
return self.model.get_input_embeddings(input_ids)
diff --git a/vllm/model_executor/models/llama4.py b/vllm/model_executor/models/llama4.py
new file mode 100644
index 000000000000..27c872072041
--- /dev/null
+++ b/vllm/model_executor/models/llama4.py
@@ -0,0 +1,530 @@
+# SPDX-License-Identifier: Apache-2.0
+#
+# Copyright 2025 the LLAMA4, Meta Inc., vLLM, and HuggingFace Inc. team.
+# All rights reserved.
+#
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""Inference-only LLaMA model compatible with HuggingFace weights."""
+from typing import Any, Dict, Iterable, List, Optional, Set, Tuple, Type
+
+import torch
+from torch import nn
+from transformers import Llama4TextConfig
+
+from vllm.attention import Attention
+from vllm.compilation.decorators import support_torch_compile
+from vllm.config import CacheConfig, VllmConfig
+from vllm.distributed import (get_tensor_model_parallel_world_size,
+ tensor_model_parallel_all_reduce)
+from vllm.model_executor.layers.fused_moe import FusedMoE
+from vllm.model_executor.layers.layernorm import RMSNorm
+from vllm.model_executor.layers.linear import (QKVParallelLinear,
+ ReplicatedLinear,
+ RowParallelLinear)
+from vllm.model_executor.layers.quantization import QuantizationConfig
+from vllm.model_executor.layers.rotary_embedding import get_rope
+from vllm.model_executor.model_loader.weight_utils import default_weight_loader
+
+from .llama import LlamaDecoderLayer, LlamaForCausalLM, LlamaMLP, LlamaModel
+from .utils import (AutoWeightsLoader, extract_layer_index,
+ is_pp_missing_parameter)
+
+
+class Llama4MoE(nn.Module):
+
+ @staticmethod
+ def custom_routing_function(
+ hidden_states: torch.Tensor,
+ gating_output: torch.Tensor,
+ topk: int,
+ renormalize: bool,
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
+ router_scores, router_indices = torch.topk(gating_output, topk, dim=-1)
+ router_scores = torch.sigmoid(router_scores.float()).to(
+ hidden_states.dtype)
+ return (router_scores, router_indices.to(torch.int32))
+
+ def __init__(self,
+ config: Llama4TextConfig,
+ quant_config: Optional[QuantizationConfig] = None,
+ prefix: str = ""):
+ super().__init__()
+ self.tp_size = get_tensor_model_parallel_world_size()
+ self.top_k = config.num_experts_per_tok
+
+ intermediate_size_moe = config.intermediate_size
+ self.router = ReplicatedLinear(config.hidden_size,
+ config.num_local_experts,
+ bias=False,
+ quant_config=None,
+ prefix=f"{prefix}.router")
+
+ self.experts = FusedMoE(
+ num_experts=config.num_local_experts,
+ top_k=config.num_experts_per_tok,
+ hidden_size=config.hidden_size,
+ custom_routing_function=Llama4MoE.custom_routing_function,
+ intermediate_size=intermediate_size_moe,
+ apply_router_weight_on_input=True,
+ reduce_results=False,
+ renormalize=False,
+ quant_config=quant_config,
+ prefix=f"{prefix}.experts")
+
+ self.shared_expert = LlamaMLP(
+ hidden_size=config.hidden_size,
+ intermediate_size=intermediate_size_moe,
+ hidden_act="silu",
+ quant_config=quant_config,
+ bias=False,
+ prefix=f"{prefix}.shared_expert",
+ reduce_results=False, # We need to do scatter before reduce
+ )
+
+ def forward(self, hidden_states):
+ router_logits, _ = self.router(hidden_states)
+ shared_out = self.shared_expert(hidden_states)
+ routed_out = self.experts(
+ hidden_states=hidden_states,
+ router_logits=router_logits,
+ )
+ experts_out = routed_out + shared_out
+
+ if self.tp_size > 1:
+ experts_out = tensor_model_parallel_all_reduce(experts_out)
+
+ return experts_out
+
+
+class Llama4Attention(nn.Module):
+
+ def __init__(self,
+ config: Llama4TextConfig,
+ hidden_size: int,
+ num_heads: int,
+ num_kv_heads: int,
+ rope_theta: float = 10000,
+ rope_scaling: Optional[Dict[str, Any]] = None,
+ max_position_embeddings: int = 8192,
+ quant_config: Optional[QuantizationConfig] = None,
+ bias: bool = False,
+ bias_o_proj: bool = False,
+ cache_config: Optional[CacheConfig] = None,
+ prefix: str = "") -> None:
+ super().__init__()
+ self.layer_idx = extract_layer_index(prefix)
+ self.hidden_size = hidden_size
+ self.no_rope_layers = config.no_rope_layers
+ self.nope = self.no_rope_layers[self.layer_idx] == 0
+ self.use_qk_norm = config.use_qk_norm and not self.nope
+ tp_size = get_tensor_model_parallel_world_size()
+ self.total_num_heads = num_heads
+ assert self.total_num_heads % tp_size == 0
+ self.num_heads = self.total_num_heads // tp_size
+ self.total_num_kv_heads = num_kv_heads
+ if self.total_num_kv_heads >= tp_size:
+ # Number of KV heads is greater than TP size, so we partition
+ # the KV heads across multiple tensor parallel GPUs.
+ assert self.total_num_kv_heads % tp_size == 0
+ else:
+ # Number of KV heads is less than TP size, so we replicate
+ # the KV heads across multiple tensor parallel GPUs.
+ assert tp_size % self.total_num_kv_heads == 0
+ self.num_kv_heads = max(1, self.total_num_kv_heads // tp_size)
+ self.head_dim = config.head_dim
+ self.q_size = self.num_heads * self.head_dim
+ self.kv_size = self.num_kv_heads * self.head_dim
+ self.scaling = self.head_dim**-0.5
+ # TODO: attn_temperature_tuning should be a bool in huggingface
+ self.attn_temperature_tuning = self.nope and \
+ config.attn_temperature_tuning > 0
+
+ self.floor_scale = getattr(config, "floor_scale", 8192.0)
+ self.attn_scale = getattr(config, "attn_scale", 0.1)
+ self.rope_theta = rope_theta
+ self.max_position_embeddings = max_position_embeddings
+ self.n_rep = self.num_heads // self.num_kv_heads
+ self.q_norm = RMSNorm(
+ hidden_size=self.q_size,
+ eps=config.rms_norm_eps,
+ has_weight=False,
+ dtype=torch.float32,
+ ) if self.use_qk_norm else None
+ self.k_norm = RMSNorm(
+ hidden_size=self.kv_size,
+ eps=config.rms_norm_eps,
+ has_weight=False,
+ dtype=torch.float32,
+ ) if self.use_qk_norm else None
+ self.qkv_proj = QKVParallelLinear(
+ hidden_size=hidden_size,
+ head_size=self.head_dim,
+ total_num_heads=self.total_num_heads,
+ total_num_kv_heads=self.total_num_kv_heads,
+ bias=bias,
+ quant_config=quant_config,
+ prefix=f"{prefix}.qkv_proj",
+ )
+
+ self.o_proj = RowParallelLinear(
+ input_size=self.total_num_heads * self.head_dim,
+ output_size=hidden_size,
+ bias=bias_o_proj,
+ quant_config=quant_config,
+ prefix=f"{prefix}.o_proj",
+ )
+ is_neox_style = True
+ is_gguf = quant_config and quant_config.get_name() == "gguf"
+ if is_gguf and config.model_type == "llama":
+ is_neox_style = False
+
+ self.rotary_emb = get_rope(
+ self.head_dim,
+ rotary_dim=self.head_dim,
+ max_position=max_position_embeddings,
+ base=int(rope_theta),
+ rope_scaling=rope_scaling if rope_scaling != "default" else None,
+ is_neox_style=is_neox_style,
+ ) if not self.nope else None
+
+ self.attn = Attention(
+ self.num_heads,
+ self.head_dim,
+ self.scaling,
+ num_kv_heads=self.num_kv_heads,
+ cache_config=cache_config,
+ quant_config=quant_config,
+ per_layer_sliding_window=None,
+ use_irope=not self.nope,
+ prefix=f"{prefix}.attn",
+ )
+
+ def _get_attn_scale(self, positions: torch.Tensor) -> torch.Tensor:
+ floor = torch.floor((positions + 1.0) / self.floor_scale)
+ attn_scale = torch.log(floor + 1.0) * self.attn_scale + 1.0
+
+ return attn_scale.unsqueeze(-1)
+
+ def forward(
+ self,
+ positions: torch.Tensor,
+ hidden_states: torch.Tensor,
+ ) -> torch.Tensor:
+ qkv, _ = self.qkv_proj(hidden_states)
+ q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
+
+ if self.rotary_emb is not None:
+ q, k = self.rotary_emb(positions, q, k)
+ if self.q_norm is not None:
+ q = self.q_norm(q.float()).to(q.dtype)
+ if self.k_norm is not None:
+ k = self.k_norm(k.float()).to(k.dtype)
+
+ # We are applying temperature tuning (https://arxiv.org/abs/2501.19399)
+ # to NoPE layers, where the inference-time temperature tuning function
+ # is customized to not affect short context
+ # while working at very long context
+ # https://arxiv.org/abs/2501.19399
+ #
+ # We should apply temperature tuning between (after) rotary / QK norm
+ # and (before) attention.
+ if self.attn_temperature_tuning and self.nope:
+ attn_scale = self._get_attn_scale(positions)
+ q = (q * attn_scale).to(q.dtype)
+ attn_output = self.attn(q, k, v)
+ output, _ = self.o_proj(attn_output)
+ return output
+
+
+class Llama4DecoderLayer(LlamaDecoderLayer):
+
+ def __init__(
+ self,
+ config: Llama4TextConfig,
+ cache_config: Optional[CacheConfig] = None,
+ quant_config: Optional[QuantizationConfig] = None,
+ prefix: str = "",
+ ) -> None:
+ self.layer_idx = extract_layer_index(prefix)
+ nn.Module.__init__(self)
+ self.hidden_size = config.hidden_size
+ rope_theta = config.rope_theta
+ rope_scaling = config.rope_scaling
+ max_position_embeddings = config.max_position_embeddings
+
+ self.self_attn = Llama4Attention(
+ config=config,
+ hidden_size=self.hidden_size,
+ num_heads=config.num_attention_heads,
+ num_kv_heads=config.num_key_value_heads,
+ rope_theta=rope_theta,
+ rope_scaling=rope_scaling,
+ max_position_embeddings=max_position_embeddings,
+ quant_config=quant_config,
+ bias=False,
+ bias_o_proj=False,
+ cache_config=cache_config,
+ prefix=f"{prefix}.self_attn",
+ )
+ is_moe_layer = (self.layer_idx +
+ 1) % config.interleave_moe_layer_step == 0
+ if is_moe_layer:
+ self.feed_forward = Llama4MoE(
+ config=config,
+ quant_config=quant_config,
+ prefix=f"{prefix}.feed_forward",
+ )
+ else:
+ self.feed_forward = LlamaMLP(
+ hidden_size=self.hidden_size,
+ intermediate_size=config.intermediate_size_mlp,
+ hidden_act="silu",
+ quant_config=quant_config,
+ bias=False,
+ prefix=f"{prefix}.feed_forward",
+ )
+ self.input_layernorm = RMSNorm(config.hidden_size,
+ eps=config.rms_norm_eps)
+ self.post_attention_layernorm = RMSNorm(config.hidden_size,
+ eps=config.rms_norm_eps)
+
+ def forward(
+ self,
+ positions: torch.Tensor,
+ hidden_states: torch.Tensor,
+ residual: Optional[torch.Tensor],
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
+ # Self Attention
+ if residual is None:
+ residual = hidden_states
+ hidden_states = self.input_layernorm(hidden_states)
+ else:
+ hidden_states, residual = self.input_layernorm(
+ hidden_states, residual)
+ hidden_states = self.self_attn(positions=positions,
+ hidden_states=hidden_states)
+
+ # Fully Connected
+ hidden_states, residual = self.post_attention_layernorm(
+ hidden_states, residual)
+ hidden_states = self.feed_forward(hidden_states)
+ return hidden_states, residual
+
+
+@support_torch_compile
+class Llama4Model(LlamaModel):
+
+ def __init__(self,
+ *,
+ vllm_config: VllmConfig,
+ prefix: str = "",
+ layer_type: Type[Llama4DecoderLayer] = Llama4DecoderLayer):
+ self.num_experts = vllm_config.model_config.hf_config.num_local_experts
+ super().__init__(vllm_config=vllm_config,
+ prefix=prefix,
+ layer_type=layer_type)
+
+ def load_moe_expert_weights(
+ self,
+ name: str,
+ loaded_weight: torch.Tensor,
+ params_dict: Dict[str, nn.Parameter],
+ loaded_params: Set[str],
+ expert_params_mapping: List[Tuple[str, str, int, str]],
+ fused: bool = True,
+ ) -> bool:
+ expert_param_loaded = False
+ if "experts.gate_up_proj" in name:
+ loaded_weight = loaded_weight.chunk(2, dim=-1)
+ for (param_name, weight_name, expert_id,
+ shard_id) in expert_params_mapping:
+ new_loaded_weight = loaded_weight
+ if fused:
+ e_str, _, proj_str, _ = weight_name.split('.')
+ weight_name = f"{e_str}.{proj_str}"
+ param_name = f"{param_name}weight"
+ if weight_name not in name:
+ continue
+ full_param_name = name.replace(weight_name, param_name)
+ # Skip layers on other devices.
+ if is_pp_missing_parameter(name, self):
+ continue
+ if ((name.endswith(".bias") or name.endswith("_bias"))
+ and name not in params_dict):
+ continue
+ param = params_dict[full_param_name]
+ weight_loader = param.weight_loader
+ if fused:
+ if "w13" in full_param_name:
+ shard_idx = 0 if shard_id == "w1" else 1
+ new_loaded_weight = new_loaded_weight[shard_idx]
+ new_loaded_weight = new_loaded_weight.transpose(-1, -2)
+ layer_idx = extract_layer_index(name)
+ # EP mapping
+ expert_map = self.layers[
+ layer_idx].feed_forward.experts.expert_map
+ if expert_map is not None:
+ local_expert_indices = (expert_map != -1) \
+ .nonzero() \
+ .flatten() \
+ .to(new_loaded_weight.device)
+ new_loaded_weight = new_loaded_weight[local_expert_indices]
+ expert_id = local_expert_indices[0].item()
+ else:
+ # TODO: add EP support for non fused weights
+ pass
+ weight_loader(param,
+ new_loaded_weight,
+ full_param_name,
+ shard_id=shard_id,
+ expert_id=expert_id)
+
+ loaded_params.add(full_param_name)
+ expert_param_loaded = True
+ return expert_param_loaded
+
+ def load_weights(self, weights: Iterable[Tuple[str,
+ torch.Tensor]]) -> Set[str]:
+ stacked_params_mapping = [
+ # (param_name, shard_name, shard_id)
+ (".qkv_proj", ".q_proj", "q"),
+ (".qkv_proj", ".k_proj", "k"),
+ (".qkv_proj", ".v_proj", "v"),
+ (".gate_up_proj", ".gate_proj", 0),
+ (".gate_up_proj", ".up_proj", 1),
+ ]
+ fused_experts_params = False
+ expert_params_mapping = FusedMoE.make_expert_params_mapping(
+ ckpt_gate_proj_name="gate_proj",
+ ckpt_down_proj_name="down_proj",
+ ckpt_up_proj_name="up_proj",
+ num_experts=self.num_experts)
+ expert_params_mapping_fused = FusedMoE.make_expert_params_mapping(
+ ckpt_gate_proj_name="gate_up_proj",
+ ckpt_down_proj_name="down_proj",
+ ckpt_up_proj_name="gate_up_proj",
+ num_experts=1)
+ params_dict = dict(self.named_parameters())
+ loaded_params: Set[str] = set()
+ for name, loaded_weight in weights:
+ if "experts.gate_up_proj" in name or "experts.down_proj" in name:
+ fused_experts_params = True
+ expert_params_mapping = expert_params_mapping_fused
+ if (self.quant_config is not None and
+ (scale_name := self.quant_config.get_cache_scale(name))):
+ # Loading kv cache quantization scales
+ param = params_dict[scale_name]
+ weight_loader = getattr(param, "weight_loader",
+ default_weight_loader)
+ loaded_weight = (loaded_weight if loaded_weight.dim() == 0 else
+ loaded_weight[0])
+ weight_loader(param, loaded_weight)
+ loaded_params.add(scale_name)
+ continue
+ for param_name, weight_name, shard_id in stacked_params_mapping:
+ if weight_name not in name or "experts" in name:
+ continue
+ name = name.replace(weight_name, param_name)
+ if is_pp_missing_parameter(name, self):
+ continue
+ param = params_dict[name]
+ weight_loader = param.weight_loader
+ weight_loader(param, loaded_weight, shard_id)
+ loaded_params.add(name)
+ break
+ else:
+ moe_loaded = self.load_moe_expert_weights(
+ name,
+ loaded_weight,
+ params_dict,
+ loaded_params,
+ expert_params_mapping,
+ fused=fused_experts_params)
+
+ if not moe_loaded:
+ if is_pp_missing_parameter(name, self):
+ continue
+ param = params_dict[name]
+ weight_loader = getattr(param, "weight_loader",
+ default_weight_loader)
+ weight_loader(param, loaded_weight)
+ loaded_params.add(name)
+ return loaded_params
+
+
+class Llama4ForCausalLM(LlamaForCausalLM):
+
+ packed_modules_mapping = {
+ "qkv_proj": ["q_proj", "k_proj", "v_proj"],
+ "gate_up_proj": ["gate_proj", "up_proj"],
+ }
+
+ def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
+ # Update temperature tuning config from generation config
+ gen_config = vllm_config.model_config.try_get_generation_config()
+ gen_config.update(vllm_config.model_config.override_generation_config)
+ vllm_config.model_config.hf_config.attn_temperature_tuning \
+ = gen_config.get("attn_temperature_tuning", False)
+ LlamaForCausalLM.__init__(self,
+ vllm_config=vllm_config,
+ prefix=prefix,
+ layer_type=Llama4DecoderLayer)
+
+ def _init_model(self,
+ vllm_config: VllmConfig,
+ prefix: str = "",
+ layer_type: Type[Llama4DecoderLayer] = Llama4DecoderLayer):
+ return Llama4Model(vllm_config=vllm_config,
+ prefix=prefix,
+ layer_type=layer_type)
+
+ def load_weights(self, weights: Iterable[Tuple[str,
+ torch.Tensor]]) -> Set[str]:
+ loader = AutoWeightsLoader(
+ self,
+ skip_prefixes=(["lm_head."]
+ if self.config.tie_word_embeddings else None),
+ )
+ weights = [
+ self.permute_qk_weight_for_rotary(name, loaded_weight)
+ for name, loaded_weight in weights
+ ]
+ return loader.load_weights(weights)
+
+ def permute_qk_weight_for_rotary(
+ self,
+ name: str,
+ loaded_weight: torch.Tensor,
+ ) -> Tuple[str, torch.Tensor]:
+
+ def permute(w: torch.Tensor, n_heads: int):
+ attn_in = self.config.head_dim * n_heads
+ attn_out = self.config.hidden_size
+
+ return w.view(n_heads, attn_in // n_heads // 2, 2,
+ attn_out).transpose(1, 2).reshape(attn_in, attn_out)
+
+ modules = name.split(".")
+
+ # rotary embeds should be sliced
+ if ("wk" in modules or "k_proj" in modules) \
+ and modules[-1] == "weight":
+ loaded_weight = permute(loaded_weight,
+ self.config.num_key_value_heads)
+ elif ("wq" in modules or "q_proj" in modules) \
+ and modules[-1] == "weight":
+ loaded_weight = permute(loaded_weight,
+ self.config.num_attention_heads)
+
+ return name, loaded_weight
diff --git a/vllm/model_executor/models/mllama4.py b/vllm/model_executor/models/mllama4.py
new file mode 100644
index 000000000000..012178c7c093
--- /dev/null
+++ b/vllm/model_executor/models/mllama4.py
@@ -0,0 +1,886 @@
+# SPDX-License-Identifier: Apache-2.0
+#
+# Copyright 2025 the LLAMA4, Meta Inc., vLLM, and HuggingFace Inc. team.
+# All rights reserved.
+#
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+import math
+from collections.abc import Iterable, Mapping
+from itertools import tee
+from typing import List, Literal, Optional, Set, Tuple, TypedDict, Union
+
+import torch
+from torch import nn
+from transformers import BatchFeature, Llama4Config, Llama4VisionConfig
+from transformers.image_utils import SizeDict
+from transformers.modeling_outputs import BaseModelOutput
+from transformers.models.llama4 import Llama4Processor
+from transformers.models.llama4.image_processing_llama4_fast import (
+ find_supported_resolutions, get_best_fit)
+
+from vllm.attention.layer import MultiHeadAttention
+from vllm.config import VllmConfig
+from vllm.distributed import get_tensor_model_parallel_world_size
+from vllm.inputs import InputProcessingContext
+from vllm.logger import init_logger
+from vllm.model_executor.layers.linear import (ColumnParallelLinear,
+ QKVParallelLinear,
+ RowParallelLinear)
+from vllm.model_executor.layers.quantization import QuantizationConfig
+from vllm.model_executor.layers.rotary_embedding import get_rope
+from vllm.model_executor.layers.sampler import SamplerOutput
+from vllm.model_executor.model_loader.weight_utils import default_weight_loader
+from vllm.model_executor.sampling_metadata import SamplingMetadata
+from vllm.multimodal import MULTIMODAL_REGISTRY
+from vllm.multimodal.inputs import (MultiModalFieldConfig, MultiModalKwargs,
+ NestedTensors)
+from vllm.multimodal.parse import (ImageProcessorItems, ImageSize,
+ MultiModalDataItems)
+from vllm.multimodal.processing import (BaseMultiModalProcessor,
+ BaseProcessingInfo, PromptReplacement,
+ PromptUpdate)
+from vllm.multimodal.profiling import BaseDummyInputsBuilder, ProcessorInputs
+from vllm.sequence import IntermediateTensors
+from vllm.transformers_utils.tokenizer import cached_tokenizer_from_config
+
+from .interfaces import MultiModalEmbeddings, SupportsMultiModal
+from .utils import (AutoWeightsLoader, flatten_bn, init_vllm_registered_model,
+ maybe_prefix, merge_multimodal_embeddings)
+from .vision import scatter_patch_features, select_patch_features
+
+logger = init_logger(__name__)
+
+
+class Llama4ImagePatchInputs(TypedDict):
+ type: Literal["pixel_values"]
+ flat_data: torch.Tensor
+ """
+ Shape:
+ `(batch_size * num_chunks, num_channels, image size, image size)`
+ """
+ patches_per_image: torch.Tensor
+ """
+ The number of total patches for each image in the batch.
+
+ This is used to split the embeddings which has the first two dimensions
+ flattened just like `flat_data`.
+ """
+ embed_is_patch: Union[torch.Tensor, list[torch.Tensor]]
+ """
+ A boolean mask indicating which image embeddings correspond
+ to patch tokens.
+ """
+ aspect_ratios: Union[torch.Tensor, list[torch.Tensor]]
+ """
+ A list of aspect ratios corresponding to the number of tiles
+ in each dimension that each image in the batch corresponds to.
+
+ Shape:
+ `(batch_size, ratio)` where ratio is a pair `(ratio_h, ratio_w)`
+ """
+
+
+class Llama4VisionMLP(nn.Module):
+
+ def __init__(self,
+ input_size: int,
+ intermediate_size: int,
+ output_size: int,
+ bias: bool,
+ output_activation: bool,
+ quant_config: Optional[QuantizationConfig] = None,
+ prefix: str = ""):
+ super().__init__()
+ self.fc1 = ColumnParallelLinear(
+ input_size=input_size,
+ output_size=intermediate_size,
+ bias=bias,
+ quant_config=quant_config,
+ prefix=f"{prefix}.fc1",
+ )
+ self.fc2 = RowParallelLinear(
+ input_size=intermediate_size,
+ output_size=output_size,
+ bias=bias,
+ quant_config=quant_config,
+ prefix=f"{prefix}.fc2",
+ )
+ self.activation_fn = nn.GELU()
+ self.output_activation = output_activation
+
+ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
+ hidden_states, _ = self.fc1(hidden_states)
+ hidden_states = self.activation_fn(hidden_states)
+ hidden_states, _ = self.fc2(hidden_states)
+ if self.output_activation:
+ return self.activation_fn(hidden_states)
+ return hidden_states
+
+
+class Llama4MultiModalProjector(nn.Module):
+
+ def __init__(
+ self,
+ config,
+ quant_config: Optional[QuantizationConfig] = None,
+ prefix: str = "",
+ ):
+ super().__init__()
+ self.linear_1 = ColumnParallelLinear(
+ input_size=config.vision_config.vision_output_dim,
+ output_size=config.text_config.hidden_size,
+ bias=False,
+ quant_config=quant_config,
+ gather_output=True,
+ prefix=f"{prefix}.linear_1",
+ )
+
+ def forward(self, image_features):
+ hidden_states, _ = self.linear_1(image_features)
+ return hidden_states
+
+
+def pixel_shuffle(input_tensor, shuffle_ratio):
+ # input_tensor: [batch_size, num_patches, channels]
+ batch_size, num_patches, channels = input_tensor.shape
+ patch_size = int(math.sqrt(num_patches))
+
+ input_tensor = input_tensor.view(batch_size, patch_size, patch_size, -1)
+ batch_size, height, width, channels = input_tensor.size()
+
+ reshaped_tensor = input_tensor.view(batch_size, height,
+ int(width * shuffle_ratio),
+ int(channels / shuffle_ratio))
+ reshaped_tensor = reshaped_tensor.permute(0, 2, 1, 3).contiguous()
+
+ reshaped_tensor = reshaped_tensor.view(batch_size,
+ int(height * shuffle_ratio),
+ int(width * shuffle_ratio),
+ int(channels / (shuffle_ratio**2)))
+ reshaped_tensor = reshaped_tensor.permute(0, 2, 1, 3).contiguous()
+
+ output_tensor = reshaped_tensor.view(batch_size, -1,
+ reshaped_tensor.shape[-1])
+ return output_tensor
+
+
+class Llama4VisionPixelShuffleMLP(nn.Module):
+
+ def __init__(
+ self,
+ config,
+ quant_config: Optional[QuantizationConfig] = None,
+ prefix: str = "",
+ ):
+ super().__init__()
+ self.pixel_shuffle_ratio = config.pixel_shuffle_ratio
+ self.inner_dim = int(config.projector_input_dim //
+ (self.pixel_shuffle_ratio**2))
+ self.output_dim = config.projector_output_dim
+ self.mlp = Llama4VisionMLP(
+ input_size=config.intermediate_size,
+ intermediate_size=config.projector_input_dim,
+ output_size=config.projector_output_dim,
+ bias=config.multi_modal_projector_bias,
+ output_activation=True,
+ quant_config=quant_config,
+ prefix=f"{prefix}.mlp")
+
+ def forward(self, encoded_patches: torch.Tensor) -> torch.Tensor:
+ encoded_patches = pixel_shuffle(encoded_patches,
+ self.pixel_shuffle_ratio)
+ return self.mlp(encoded_patches)
+
+
+class Llama4VisionAttention(nn.Module):
+
+ def __init__(
+ self,
+ config: Llama4VisionConfig,
+ quant_config: Optional[QuantizationConfig],
+ prefix: str = "",
+ ):
+ super().__init__()
+ self.config = config
+ self.tp_size = get_tensor_model_parallel_world_size()
+ self.embed_dim = config.hidden_size
+ self.num_heads = config.num_attention_heads
+ self.head_dim = config.hidden_size // self.num_heads
+ assert self.num_heads % self.tp_size == 0
+ self.num_local_heads = self.num_heads // self.tp_size
+ self.q_size = self.num_local_heads * self.head_dim
+ self.kv_size = self.num_local_heads * self.head_dim
+ self.attention_dropout = config.attention_dropout
+ self.scaling = self.head_dim**-0.5
+
+ self.attn = MultiHeadAttention(self.num_local_heads, self.head_dim,
+ self.scaling)
+ self.qkv_proj = QKVParallelLinear(
+ self.embed_dim,
+ self.head_dim,
+ self.num_heads,
+ bias=True,
+ quant_config=quant_config,
+ prefix=f"{prefix}.qkv_proj",
+ )
+ self.o_proj = RowParallelLinear(
+ self.num_heads * self.head_dim,
+ self.embed_dim,
+ bias=True,
+ input_is_parallel=True,
+ quant_config=quant_config,
+ prefix=f"{prefix}.o_proj",
+ )
+
+ self.rotary_emb = get_rope(
+ head_size=self.head_dim,
+ rotary_dim=config.hidden_size // config.num_attention_heads // 2,
+ # number of image patches
+ max_position=(config.image_size // config.patch_size)**2,
+ base=config.rope_theta,
+ rope_scaling={"rope_type": "mllama4"},
+ is_neox_style=False,
+ dtype=torch.complex64, # important
+ )
+
+ def forward(
+ self,
+ hidden_states: torch.Tensor,
+ ) -> torch.Tensor:
+ input_shape = hidden_states.shape[:-1]
+
+ qkv, _ = self.qkv_proj(hidden_states)
+ q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
+
+ q = q.view(q.shape[0], q.shape[1], self.num_local_heads, self.head_dim)
+ k = k.view(k.shape[0], k.shape[1], self.num_local_heads, self.head_dim)
+ q, k = self.rotary_emb(q, k)
+
+ q = q.view(q.shape[0], q.shape[1], -1)
+ k = k.view(k.shape[0], k.shape[1], -1)
+
+ attn_output = self.attn(q, k, v)
+ attn_output = attn_output.reshape(*input_shape, -1).contiguous()
+ attn_output, _ = self.o_proj(attn_output)
+
+ return attn_output
+
+
+class Llama4VisionEncoderLayer(nn.Module):
+
+ def __init__(
+ self,
+ config: Llama4VisionConfig,
+ quant_config: Optional[QuantizationConfig],
+ prefix: str = "",
+ ):
+ super().__init__()
+ self.hidden_size = config.hidden_size
+ self.num_attention_heads = config.num_attention_heads
+ self.intermediate_size = config.intermediate_size
+
+ self.self_attn = Llama4VisionAttention(config,
+ quant_config=quant_config,
+ prefix=f"{prefix}.self_attn")
+ self.mlp = Llama4VisionMLP(input_size=config.hidden_size,
+ intermediate_size=config.intermediate_size,
+ output_size=config.hidden_size,
+ bias=True,
+ output_activation=False,
+ quant_config=quant_config,
+ prefix=f"{prefix}.mlp")
+
+ self.input_layernorm = nn.LayerNorm(config.hidden_size)
+ self.post_attention_layernorm = nn.LayerNorm(config.hidden_size)
+
+ def forward(
+ self,
+ hidden_state: torch.Tensor,
+ ):
+ # Self Attention
+ residual = hidden_state
+ hidden_state = self.input_layernorm(hidden_state)
+ hidden_state = self.self_attn(hidden_state)
+ hidden_state = residual + hidden_state
+
+ # Feed forward
+ residual = hidden_state
+ hidden_state = self.post_attention_layernorm(hidden_state)
+ hidden_state = self.mlp(hidden_state)
+ hidden_state = residual + hidden_state
+
+ outputs = (hidden_state, )
+ return outputs
+
+
+class Llama4VisionEncoder(nn.Module):
+
+ def __init__(
+ self,
+ config: Llama4VisionConfig,
+ quant_config: Optional[QuantizationConfig],
+ prefix: str = "",
+ ):
+ super().__init__()
+ self.config = config
+ self.layers = nn.ModuleList([
+ Llama4VisionEncoderLayer(
+ config,
+ quant_config=quant_config,
+ prefix=f"{prefix}.layers.{layer_idx}",
+ ) for layer_idx in range(config.num_hidden_layers)
+ ])
+
+ def forward(
+ self,
+ hidden_states: torch.Tensor,
+ ) -> BaseModelOutput:
+ r"""
+ Args:
+ inputs_embeds (`torch.FloatTensor` of shape
+ `(batch_size, sequence_length, hidden_size)`):
+ Optionally, instead of passing `input_ids` you can choose to
+ directly pass an embedded representation. This is useful if you
+ want more control over how to convert `input_ids` indices into
+ associated vectors than the model's internal embedding
+ lookup matrix.
+ """
+
+ for encoder_layer in self.layers:
+ layer_outputs = encoder_layer(hidden_states)
+ hidden_states = layer_outputs[0]
+
+ return BaseModelOutput(last_hidden_state=hidden_states, )
+
+
+class Llama4UnfoldConvolution(nn.Module):
+
+ def __init__(self,
+ config: Llama4VisionConfig,
+ quant_config: Optional[QuantizationConfig] = None,
+ prefix: str = ""):
+ super().__init__()
+ kernel_size = config.patch_size
+ if isinstance(kernel_size, int):
+ kernel_size = (kernel_size, kernel_size)
+ self.unfold = torch.nn.Unfold(kernel_size=kernel_size,
+ stride=config.patch_size)
+ self.linear = ColumnParallelLinear(config.num_channels *
+ kernel_size[0] * kernel_size[1],
+ config.hidden_size,
+ bias=False,
+ quant_config=quant_config,
+ gather_output=True,
+ prefix=f"{prefix}.linear")
+
+ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
+ hidden_states = self.unfold(hidden_states)
+ hidden_states = hidden_states.permute(0, 2, 1)
+ hidden_states, _ = self.linear(hidden_states)
+ return hidden_states
+
+
+class Llama4VisionModel(nn.Module):
+
+ def __init__(
+ self,
+ config: Llama4VisionConfig,
+ quant_config: Optional[QuantizationConfig] = None,
+ prefix: str = "",
+ ):
+ super().__init__()
+ self.config = config
+ self.image_size = config.image_size
+ self.patch_size = config.patch_size
+ self.hidden_size = config.hidden_size
+ self.num_channels = config.num_channels
+
+ self.num_patches = (self.image_size // self.patch_size)**2 + 1
+ self.scale = config.hidden_size**-0.5
+
+ self.patch_embedding = Llama4UnfoldConvolution(
+ config,
+ quant_config=quant_config,
+ prefix=f"{prefix}.patch_embedding")
+
+ self.class_embedding = nn.Parameter(self.scale *
+ torch.randn(self.hidden_size))
+ self.positional_embedding_vlm = nn.Parameter(
+ self.scale * torch.randn(self.num_patches, self.hidden_size))
+
+ # layer norms
+ self.layernorm_pre = nn.LayerNorm(self.hidden_size, eps=1e-5)
+ self.layernorm_post = nn.LayerNorm(self.hidden_size, eps=1e-5)
+
+ # encoders
+ self.model = Llama4VisionEncoder(config,
+ quant_config=quant_config,
+ prefix=f"{prefix}.model")
+ self.vision_adapter = Llama4VisionPixelShuffleMLP(
+ config, quant_config, prefix=f"{prefix}.vision_adapter")
+
+ def forward(
+ self,
+ images_flattened: torch.Tensor,
+ ) -> BaseModelOutput:
+ # Patch embedding
+ hidden_state = self.patch_embedding(images_flattened)
+ num_tiles, num_patches, hidden_dim = hidden_state.shape
+
+ # Add cls token
+ class_embedding = self.class_embedding.expand(hidden_state.shape[0], 1,
+ hidden_state.shape[-1])
+ hidden_state = torch.cat([hidden_state, class_embedding], dim=1)
+ num_patches += 1
+
+ # Position embeddings
+ hidden_state = hidden_state.reshape(
+ num_tiles,
+ 1,
+ num_patches,
+ hidden_dim,
+ )
+ positional_embedding = self.positional_embedding_vlm.to(
+ dtype=hidden_state.dtype, device=hidden_state.device)
+ hidden_state = hidden_state + positional_embedding
+ hidden_state = self.layernorm_pre(hidden_state)
+ hidden_state = hidden_state.view(num_tiles, -1, hidden_dim)
+
+ # Apply encoder
+ output = self.model(hidden_state)
+ hidden_state = output.last_hidden_state
+ hidden_state = self.layernorm_post(hidden_state)
+
+ # Remove CLS token output
+ hidden_state = hidden_state[:, :-1, :]
+
+ # now, we use Llama4VisionPixelShuffle + mlp to project embeddings
+ hidden_state = self.vision_adapter(hidden_state)
+
+ return BaseModelOutput(
+ last_hidden_state=hidden_state,
+ attentions=None,
+ )
+
+
+class Mllama4ProcessingInfo(BaseProcessingInfo):
+
+ def __init__(self, ctx: InputProcessingContext) -> None:
+ super().__init__(ctx)
+
+ def get_hf_config(self) -> Llama4Config:
+ return self.ctx.get_hf_config(Llama4Config)
+
+ def get_hf_processor(self, **kwargs: object) -> Llama4Processor:
+ return self.ctx.get_hf_processor(Llama4Processor,
+ use_fast=True,
+ **kwargs)
+
+ def get_supported_mm_limits(self) -> Mapping[str, Optional[int]]:
+ return {"image": 10}
+
+ @staticmethod
+ def get_patch_per_chunk(vision_config: Llama4VisionConfig) -> int:
+ image_size = vision_config.image_size
+ patch_size = vision_config.patch_size
+
+ assert (
+ image_size %
+ patch_size == 0), f"chunk size {image_size} should be multiple of "
+ f"patch_size {patch_size}"
+
+ ds_ratio = int(round(1.0 / (vision_config.pixel_shuffle_ratio**2)))
+ return (image_size // patch_size)**2 // ds_ratio
+
+ def get_max_num_tiles(self) -> int:
+ image_processor = self.get_hf_processor().image_processor
+ return image_processor.max_patches
+
+ def get_mm_max_tokens_per_item(
+ self,
+ seq_len: int,
+ mm_counts: Mapping[str, int],
+ ) -> Mapping[str, int]:
+ vision_config = self.get_hf_config().vision_config
+ # image_start + local tiles * (patches + 1 x separator) +
+ # 1 global tile * (image x 1 + patches) + image_end
+ token_per_chunk = self.get_patch_per_chunk(vision_config) + 1
+ mm_max_tokens = (self.get_max_num_tiles() + 1) * token_per_chunk + 2
+ return {"image": mm_max_tokens}
+
+ def get_image_size_with_most_features(self) -> ImageSize:
+ vision_config = self.get_hf_config().vision_config
+ image_size = vision_config.image_size
+ # Result in the max possible feature size (h:w = 16:1)
+ return ImageSize(height=self.get_max_num_tiles() * image_size,
+ width=image_size)
+
+
+class Mllama4MultiModalProcessor(BaseMultiModalProcessor[Mllama4ProcessingInfo]
+ ):
+
+ def _call_hf_processor(
+ self,
+ prompt: str,
+ mm_data: Mapping[str, object],
+ mm_kwargs: Mapping[str, object],
+ ) -> BatchFeature:
+ tokenizer = self.info.get_tokenizer()
+
+ if mm_data is None:
+ return tokenizer(prompt, add_special_tokens=False) # exclude bos
+ processed_outputs = super()._call_hf_processor(
+ prompt=prompt,
+ mm_data=mm_data,
+ mm_kwargs=mm_kwargs,
+ )
+
+ processor = self.info.get_hf_processor(**mm_kwargs)
+ image_processor = processor.image_processor
+ vision_config = self.info.get_hf_config().vision_config
+
+ if processed_outputs.get("pixel_values") is not None:
+ assert "images" in mm_data, \
+ "images expected to be in mm_data when pixel_values is present"
+
+ images = mm_data["images"]
+ parsed_images = (self._get_data_parser().parse_mm_data({
+ "image":
+ images
+ }).get_items("image", ImageProcessorItems))
+
+ tile_size = vision_config.image_size
+ possible_resolutions = find_supported_resolutions(
+ max_num_chunks=self.info.get_max_num_tiles(),
+ patch_size=SizeDict(height=tile_size, width=tile_size),
+ )
+ best_fit_sizes = [
+ get_best_fit(
+ (image.size[1], image.size[0]),
+ torch.tensor(possible_resolutions),
+ resize_to_max_canvas=image_processor.resize_to_max_canvas)
+ for image in parsed_images
+ ]
+ # TODO tile height/width do not necessarily need to match
+ aspect_ratios = [(image_size[0] // tile_size,
+ image_size[1] // tile_size)
+ for image_size in best_fit_sizes]
+ patches_per_image = [
+ 1 if r_h * r_w == 1 else 1 + r_h * r_w
+ for (r_h, r_w) in aspect_ratios
+ ]
+
+ # embed_is_patch should have one feature per image-related token:
+ # <|image_start|>, <|tile_*_separator|>, <|image|>, <|image_end|>
+ # -> False
+ # <|patch|> -> True
+ # embed_is_patch has no entries corresponding to non-image-related
+ # tokens.
+ patch_id = tokenizer.get_vocab()[processor.img_patch_token]
+ num_patches_per_chunk = self.info.get_patch_per_chunk(
+ vision_config)
+ expanded_image_tokens_list = [
+ processor._prompt_split_image(aspect_ratio,
+ num_patches_per_chunk)
+ for aspect_ratio in aspect_ratios
+ ]
+ expanded_image_token_ids = [
+ tokenizer.encode(image_tokens, add_special_tokens=False)
+ for image_tokens in expanded_image_tokens_list
+ ]
+ embed_is_patch = [
+ torch.tensor(tokens) == patch_id
+ for tokens in expanded_image_token_ids
+ ]
+
+ processed_outputs["aspect_ratios"] = aspect_ratios
+ processed_outputs["patches_per_image"] = torch.tensor(
+ patches_per_image)
+ processed_outputs["embed_is_patch"] = embed_is_patch
+
+ return processed_outputs
+
+ def _get_mm_fields_config(
+ self,
+ hf_inputs: BatchFeature,
+ hf_processor_mm_kwargs: Mapping[str, object],
+ ) -> Mapping[str, MultiModalFieldConfig]:
+ patches_per_image = hf_inputs.get("patches_per_image", torch.empty(0))
+ return dict(
+ pixel_values=MultiModalFieldConfig.flat_from_sizes(
+ "image", patches_per_image),
+ patches_per_image=MultiModalFieldConfig.batched("image"),
+ aspect_ratios=MultiModalFieldConfig.batched("image"),
+ embed_is_patch=MultiModalFieldConfig.batched("image"),
+ )
+
+ def _get_prompt_updates(
+ self,
+ mm_items: MultiModalDataItems,
+ hf_processor_mm_kwargs: Mapping[str, object],
+ out_mm_kwargs: MultiModalKwargs,
+ ) -> List[PromptUpdate]:
+ assert (
+ mm_items.get_count("image", strict=False) == 0
+ or "aspect_ratios" in out_mm_kwargs
+ ), "Transformers expect to include aspect_ratios in out_mm_kwargs"
+
+ config = self.info.get_hf_config()
+ vision_config = config.vision_config
+
+ num_patches_per_chunk = self.info.get_patch_per_chunk(vision_config)
+ hf_processor = self.info.get_hf_processor(**hf_processor_mm_kwargs)
+ image_token = hf_processor.image_token
+
+ def get_replacement(item_idx: int):
+ aspect_ratio = out_mm_kwargs["aspect_ratios"][item_idx]
+ return hf_processor._prompt_split_image(
+ aspect_ratio=aspect_ratio,
+ num_patches_per_chunk=num_patches_per_chunk)
+
+ return [
+ PromptReplacement(
+ modality="image",
+ target=image_token,
+ replacement=get_replacement,
+ )
+ ]
+
+
+class Mllama4DummyInputsBuilder(BaseDummyInputsBuilder[Mllama4ProcessingInfo]):
+
+ def get_dummy_processor_inputs(
+ self,
+ seq_len: int,
+ mm_counts: Mapping[str, int],
+ ) -> ProcessorInputs:
+ num_images = mm_counts.get("image", 0)
+
+ (target_width,
+ target_height) = self.info.get_image_size_with_most_features()
+
+ mm_data = {
+ "image":
+ self._get_dummy_images(width=target_width,
+ height=target_height,
+ num_images=num_images)
+ }
+
+ image_token = self.info.get_hf_processor().fake_image_token
+ return ProcessorInputs(
+ prompt_text=image_token * num_images,
+ mm_data=mm_data,
+ )
+
+
+@MULTIMODAL_REGISTRY.register_processor(
+ Mllama4MultiModalProcessor,
+ info=Mllama4ProcessingInfo,
+ dummy_inputs=Mllama4DummyInputsBuilder,
+)
+class Llama4ForConditionalGeneration(nn.Module, SupportsMultiModal):
+ packed_modules_mapping = {
+ "qkv_proj": ["q_proj", "k_proj", "v_proj"],
+ }
+
+ def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
+ super().__init__()
+ config = vllm_config.model_config.hf_config
+ quant_config = vllm_config.quant_config
+ multimodal_config = vllm_config.model_config.multimodal_config
+ self.config = config
+ self.quant_config = quant_config
+ self.multimodal_config = multimodal_config
+ self.vision_model = Llama4VisionModel(config.vision_config,
+ None,
+ prefix=maybe_prefix(
+ prefix, "vision_model"))
+ self.multi_modal_projector = Llama4MultiModalProjector(
+ self.config,
+ None,
+ prefix=maybe_prefix(prefix, "multi_modal_projector"))
+ self.language_model = init_vllm_registered_model(
+ vllm_config=vllm_config,
+ hf_config=config.text_config,
+ architectures=["Llama4ForCausalLM"],
+ prefix=maybe_prefix(prefix, "language_model"))
+
+ self.tokenizer = cached_tokenizer_from_config(vllm_config.model_config)
+
+ def _parse_and_validate_image_input(
+ self, **kwargs: object) -> Optional[Llama4ImagePatchInputs]:
+ # num_images, 1, num_chunks, channel, image_size, image_size
+ pixel_values = kwargs.pop("pixel_values", None)
+ if pixel_values is None:
+ return None
+
+ # num_images x num_chunks, channel, image_size, image_size
+ # TODO: confirm handling for variable lengths
+ flat_pixel_values = flatten_bn(pixel_values, concat=True)
+ patches_per_image = flatten_bn(kwargs.pop("patches_per_image"))
+
+ embed_is_patch = kwargs.pop("embed_is_patch", None)
+ if not isinstance(embed_is_patch, (torch.Tensor, list)):
+ raise ValueError("Incorrect type of embed_is_patch. "
+ f"Got type: {type(embed_is_patch)}")
+
+ aspect_ratios = kwargs.pop("aspect_ratios", None)
+ if not isinstance(aspect_ratios, (torch.Tensor, list)):
+ raise ValueError("Incorrect type of aspect_ratios. "
+ f"Got type: {type(aspect_ratios)}")
+
+ return Llama4ImagePatchInputs(
+ type="pixel_values",
+ flat_data=flat_pixel_values,
+ patches_per_image=patches_per_image,
+ embed_is_patch=embed_is_patch,
+ aspect_ratios=aspect_ratios,
+ )
+
+ def _process_image_input(
+ self, image_input: Llama4ImagePatchInputs) -> MultiModalEmbeddings:
+ flat_data = image_input["flat_data"]
+ patches_per_image = image_input["patches_per_image"].tolist()
+ vision_embeddings_flat = self.vision_model(flat_data).last_hidden_state
+ return vision_embeddings_flat.split(patches_per_image, dim=0)
+
+ def get_multimodal_embeddings(self,
+ **kwargs) -> Optional[MultiModalEmbeddings]:
+ image_input = self._parse_and_validate_image_input(**kwargs)
+ if image_input is None:
+ return None
+
+ # num_images x [num_chunks, num_patches, hidden_dim]
+ image_features = self._process_image_input(image_input)
+ # num_images x [num_chunks x num_patches, hidden_dim]
+ image_features_flat = [img.flatten(0, 1) for img in image_features]
+ # num_images x [1, input_len] -> num_images x [input_len]
+ embed_is_patch_flat = [
+ is_patch.flatten(0, 1)
+ for is_patch in image_input["embed_is_patch"]
+ ]
+
+ return scatter_patch_features(
+ image_features_flat,
+ embed_is_patch_flat,
+ )
+
+ def get_input_embeddings(
+ self,
+ input_ids: torch.Tensor,
+ multimodal_embeddings: Optional[NestedTensors] = None,
+ ) -> torch.Tensor:
+ inputs_embeds = self.language_model.get_input_embeddings(input_ids)
+
+ if multimodal_embeddings is not None:
+ multimodal_embeddings = torch.cat(multimodal_embeddings)
+ mm_embeddings = self.multi_modal_projector(multimodal_embeddings)
+ inputs_embeds = merge_multimodal_embeddings(
+ input_ids, inputs_embeds, select_patch_features(mm_embeddings),
+ self.config.image_token_index)
+
+ return inputs_embeds
+
+ def forward(
+ self,
+ input_ids: torch.Tensor,
+ positions: torch.Tensor,
+ intermediate_tensors: Optional[IntermediateTensors] = None,
+ inputs_embeds: Optional[torch.Tensor] = None,
+ **kwargs: object,
+ ) -> Union[torch.Tensor, IntermediateTensors]:
+ # NOTE: In v1, inputs_embeds is always generated at model runner, this
+ # condition is for v0 compatibility.
+ if "pixel_values" in kwargs:
+ vision_embeddings = self.get_multimodal_embeddings(**kwargs)
+ inputs_embeds = self.get_input_embeddings(input_ids,
+ vision_embeddings)
+ input_ids = None
+
+ return self.language_model(input_ids, positions, intermediate_tensors,
+ inputs_embeds)
+
+ def compute_logits(
+ self,
+ hidden_states: torch.Tensor,
+ sampling_metadata: SamplingMetadata,
+ ) -> Optional[torch.Tensor]:
+ return self.language_model.compute_logits(hidden_states,
+ sampling_metadata)
+
+ def sample(self, logits: torch.Tensor,
+ sampling_metadata: SamplingMetadata) -> Optional[SamplerOutput]:
+ return self.language_model.sample(logits, sampling_metadata)
+
+ def separate_weights(
+ self,
+ weights: Iterable[Tuple[str, torch.Tensor]],
+ prefix: str,
+ ) -> Tuple[Iterable[Tuple[str, torch.Tensor]], Iterable[Tuple[
+ str, torch.Tensor]]]:
+ weights1, weights2 = tee(weights, 2)
+
+ def get_prefix_weights() -> Iterable[Tuple[str, torch.Tensor]]:
+ for name, data in weights1:
+ if name.startswith(prefix):
+ yield (name, data)
+
+ def get_other_weights() -> Iterable[Tuple[str, torch.Tensor]]:
+ for name, data in weights2:
+ if not name.startswith(prefix):
+ yield (name, data)
+
+ return get_prefix_weights(), get_other_weights()
+
+ def load_weights(self, weights: Iterable[Tuple[str,
+ torch.Tensor]]) -> Set[str]:
+
+ stacked_params_mapping = [
+ # (param_name, shard_name, shard_id)
+ (".self_attn.qkv_proj", ".self_attn.q_proj", "q"),
+ (".self_attn.qkv_proj", ".self_attn.k_proj", "k"),
+ (".self_attn.qkv_proj", ".self_attn.v_proj", "v"),
+ ]
+ params_dict = dict(self.named_parameters())
+ updated_params: Set[str] = set()
+
+ # language_model is an Llama4ForCausalLM instance. We load it's
+ # using llama4's load_weights routine.
+ language_model_prefix = "language_model.model."
+ language_model_weights, other_weights = self.separate_weights(
+ weights, prefix=language_model_prefix)
+ loader = AutoWeightsLoader(self)
+ loaded_language_model_params = loader.load_weights(
+ language_model_weights)
+ assert loaded_language_model_params is not None
+ updated_params.update(loaded_language_model_params)
+
+ for name, loaded_weight in other_weights:
+ for param_name, weight_name, shard_id in stacked_params_mapping:
+ if weight_name not in name:
+ continue
+ name = name.replace(weight_name, param_name)
+ param = params_dict[name]
+ updated_params.add(name)
+ weight_loader = param.weight_loader
+ weight_loader(param, loaded_weight, shard_id)
+ break
+ else:
+ param = params_dict[name]
+ weight_loader = getattr(param, "weight_loader",
+ default_weight_loader)
+
+ weight_loader(param, loaded_weight)
+ updated_params.add(name)
+ return updated_params
diff --git a/vllm/model_executor/models/registry.py b/vllm/model_executor/models/registry.py
index 6ead6509bfe8..c0a3c59ba257 100644
--- a/vllm/model_executor/models/registry.py
+++ b/vllm/model_executor/models/registry.py
@@ -73,6 +73,7 @@
"JAISLMHeadModel": ("jais", "JAISLMHeadModel"),
"JambaForCausalLM": ("jamba", "JambaForCausalLM"),
"LlamaForCausalLM": ("llama", "LlamaForCausalLM"),
+ "Llama4ForCausalLM": ("llama4", "Llama4ForCausalLM"),
# For decapoda-research/llama-*
"LLaMAForCausalLM": ("llama", "LlamaForCausalLM"),
"MambaForCausalLM": ("mamba", "MambaForCausalLM"),
@@ -194,6 +195,7 @@
# [Encoder-decoder]
"Florence2ForConditionalGeneration": ("florence2", "Florence2ForConditionalGeneration"), # noqa: E501
"MllamaForConditionalGeneration": ("mllama", "MllamaForConditionalGeneration"), # noqa: E501
+ "Llama4ForConditionalGeneration": ("mllama4", "Llama4ForConditionalGeneration"), # noqa: E501
"SkyworkR1VChatModel": ("skyworkr1v", "SkyworkR1VChatModel"),
"WhisperForConditionalGeneration": ("whisper", "WhisperForConditionalGeneration"), # noqa: E501
}
diff --git a/vllm/v1/attention/backends/flash_attn.py b/vllm/v1/attention/backends/flash_attn.py
index 92e4ffd0371a..c271f438e858 100755
--- a/vllm/v1/attention/backends/flash_attn.py
+++ b/vllm/v1/attention/backends/flash_attn.py
@@ -96,6 +96,183 @@ class FlashAttentionMetadata:
# For logging.
num_input_tokens: int = 0 # Number of tokens including padding.
+ # for local attention
+ @dataclass
+ class LocalAttentionMetadata:
+ local_query_start_loc: torch.Tensor
+ local_seqused_k: torch.Tensor
+ local_block_table: torch.Tensor
+ local_max_query_len: int
+ local_max_seq_len: int
+
+ local_attn_metadata: Optional[LocalAttentionMetadata] = None
+
+
+#
+# Take in `query_start_loc_np` and `seq_lens_np` and break the sequences into
+# local attention blocks, where each block is passed to the attention kernel
+# as an independent local ("virtual") batch item.
+#
+# For example, if are performing a chunked prefill a batch of 3 sequences:
+# q_seqlens = [4, 10, 5]
+# kv_seqlens = [6, 17, 9]
+# Then normally for regular attention we would compute with an attention mask
+# for batch idx 0 (q_seqlens = 4, kv_seqlens = 6) like:
+# batch idx: 0 (q_seqlens = 4, kv_seqlens = 6)
+# k_toks > 0 1 2 3 4 5
+# q_toks v _____________
+# 0 | 1 1 1
+# 1 | 1 1 1 1
+# 2 | 1 1 1 1 1
+# 3 | 1 1 1 1 1 1
+#
+# for local attention (with attn_chunk_size = 4) we would compute with an
+# attention mask like:
+# batch idx: 0 (q_seqlens = 4, kv_seqlens = 6, attn_chunk_size = 4)
+# k_toks > 0 1 2 3 4 5
+# q_toks v _____________
+# 0 | 1 1 1
+# 1 | 1 1 1 1
+# 2 | 1
+# 3 | 1 1
+#
+# We can simulate this mask using standard flash-attention by breaking the
+# sequences into local ("virtual") batches, where each local batch item is a
+# local attention block, so in this case batch idx 0 would be broken up into:
+#
+# local-batch idx: 0 (q_seqlens = 2, kv_seqlens = 4) (batch 0)
+# k_toks > 0 1 2 3
+# q_toks v _____________
+# 0 | 1 1 1
+# 1 | 1 1 1 1
+# local-batch idx: 1 (q_seqlens = 2, kv_seqlens = 2) (batch 0)
+# k_toks > 4 5
+# q_toks v _____________
+# 2 | 1
+# 3 | 1 1
+#
+# e.g. if we have:
+# attn_chunk_size = 4
+# query_start_loc_np = [0, 4, 14, 19] (q_seqlens = [4, 10, 5])
+# Then this function would return:
+# __b0__ ______b1______ __b2__ < orig batch indices
+# q_seqlens_local = [ 2, 2, 1, 4, 4, 1, 4, 1]
+# cu_seqlens_q_local = [0, 4, 6, 10, 14, 18, 19, 23, 24]
+# seqlens_k_local = [ 4, 2, 4, 4, 4, 1, 4, 1]
+# block_table_local : shape[local_virtual_batches, pages_per_local_batch]
+def make_local_attention_virtual_batches(
+ attn_chunk_size: int,
+ query_start_loc_np: np.ndarray,
+ seq_lens_np: np.ndarray,
+ block_table: torch.tensor,
+ page_size: int = 0,
+) -> tuple[np.ndarray, np.ndarray, np.ndarray, torch.tensor]:
+ q_seqlens = query_start_loc_np[1:] - query_start_loc_np[:-1]
+ actual_batch_size = seq_lens_np.shape[0]
+
+ # Handle if we are starting in the middle of a local attention block,
+ # we assume q_seqlens > 0 (for all elements), for each batch idx we compute
+ # the number of tokens that are not in the first local attention block and
+ # then we can simply use a cdiv for the rest.
+ # For example if we have:
+ # attn_chunk_size = 4
+ # q_seqlens = [4, 10, 5]
+ # k_seqlens = [6, 17, 9]
+ # Then we would get:
+ # new_tokens_in_first_block = [2, 1, 4]
+ # local_blocks = [2, 4, 2]
+ q_tokens_in_first_block = np.minimum(
+ attn_chunk_size - ((seq_lens_np - q_seqlens) % attn_chunk_size),
+ q_seqlens).astype(np.int32)
+ tokens_in_last_block = attn_chunk_size + (seq_lens_np % -attn_chunk_size)
+ local_blocks = 1 + cdiv(q_seqlens - q_tokens_in_first_block,
+ attn_chunk_size)
+
+ # Once we know the number of local blocks we can compute the request spans
+ # for each batch idx, we can figure out the number of "virtual" requests we
+ # have to make,
+ # For the above example we would get:
+ # seqlens_q_local = [2, 2, 1, 4, 4, 1, 4, 1]
+ #
+ # First Get batched arange. (E.g., [2, 4, 2] -> [0, 1, 0, 1, 2, 3, 0, 1])
+ # (TODO: max a utility to share this code with _prepare_inputs)
+ # arange step 1. [2, 4, 2] -> [2, 6, 8]
+ cu_num_blocks = np.cumsum(local_blocks)
+ virtual_batches = cu_num_blocks[-1]
+ # arange step 2. [2, 6, 8] -> [0, 0, 2, 2, 2, 2, 6, 6]
+ block_offsets = np.repeat(cu_num_blocks - local_blocks, local_blocks)
+ # arange step 3. [0, 1, 0, 1, 2, 3, 0, 1]
+ arange = np.arange(virtual_batches, dtype=np.int32) - block_offsets
+ # also compute reverse arange (i.e. [1, 0, 3, 2, 1, 0, 1, 0])
+ rarange = np.repeat(local_blocks, local_blocks) - arange - 1
+ # Then we can compute the seqlens_q_local, handling the fact that the
+ # first and last blocks could be partial
+ seqlens_q_local = \
+ np.repeat(q_seqlens - q_tokens_in_first_block, local_blocks)
+ # set the first block since this may be a partial block
+ seqlens_q_local[arange == 0] = q_tokens_in_first_block
+ # set the remaining blocks
+ seqlens_q_local[arange > 0] = np.minimum(
+ seqlens_q_local - attn_chunk_size * (arange - 1),
+ attn_chunk_size)[arange > 0]
+
+ # convert from q_seqlens to cu_seqlens_q
+ cu_seqlens_q_local = np.pad(np.cumsum(seqlens_q_local), (1, 0))\
+ .astype(np.int32)
+
+ # compute the seqlens_k_local,
+ # basically a full local attention block for all but the last block in each
+ # batch
+ # For our example this will be:
+ # seqlens_k_local = [4, 2, 4, 4, 4, 1, 4, 1]
+ seqlens_k_local = np.full(cu_num_blocks[-1],
+ attn_chunk_size,
+ dtype=np.int32)
+ seqlens_k_local[cu_num_blocks - 1] = tokens_in_last_block
+
+ k_seqstarts_absolute = np.repeat(seq_lens_np, local_blocks) - \
+ (rarange * attn_chunk_size + \
+ np.repeat(tokens_in_last_block, local_blocks))
+ # For the example the local attention blocks start at:
+ # _b0_ _____b1_____ _b2_
+ # k_seqstarts_absolute = [0, 4, 4, 8, 12, 16, 4, 8]
+ block_starts = k_seqstarts_absolute // page_size
+ assert attn_chunk_size % page_size == 0, \
+ f"attn_chunk_size {attn_chunk_size} is not " \
+ f"divisible by page_size {page_size}"
+ pages_per_local_batch = attn_chunk_size // page_size
+
+ # Create a block_table for the local attention blocks
+ # For out example if we have a block-table like (assuming page_size=2):
+ # block_table = [
+ # [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], < batch 0
+ # [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], < batch 1
+ # [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], < batch 2
+ # ]
+ # Then for the local batches we would want a block-table like
+ # block_table_local = [
+ # [ 0, 1 ], < local-batch 0, (batch 0, starting from k[0])
+ # [ 2, 3 ], < local-batch 1, (batch 0, starting from k[4])
+ # [ 12, 13 ], < local-batch 2, (batch 1, starting from k[4])
+ # [ 14, 15 ], < local-batch 3, (batch 1, starting from k[8])
+ # [ 16, 17 ], < local-batch 4, (batch 1, starting from k[12])
+ # [ 18, 19 ], < local-batch 5, (batch 1, starting from k[16])
+ # [ 22, 23 ], < local-batch 6, (batch 2, starting from k[4])
+ # [ 24, 25 ], < local-batch 7, (batch 2, starting from k[8])
+ # ]
+ block_indices= np.broadcast_to(
+ np.arange(pages_per_local_batch, dtype=np.int32),
+ (virtual_batches, pages_per_local_batch)) \
+ + np.expand_dims(block_starts, axis=1)
+ block_indices = block_indices.flatten()
+ batch_indices = np.repeat(np.arange(actual_batch_size, dtype=np.int32),
+ local_blocks * pages_per_local_batch)
+ block_table_local = block_table[batch_indices, block_indices]\
+ .view(virtual_batches, -1)
+
+ return seqlens_q_local, cu_seqlens_q_local, seqlens_k_local, \
+ block_table_local
+
class FlashAttentionMetadataBuilder:
@@ -109,18 +286,40 @@ def reorder_batch(self, input_batch: "InputBatch",
def build(self, num_reqs: int, num_actual_tokens: int, max_query_len: int,
common_prefix_len: int):
max_seq_len = self.runner.seq_lens_np[:num_reqs].max()
- query_start_loc = self.runner.query_start_loc_cpu[:num_reqs + 1].to(
- self.runner.device, non_blocking=True)
- seq_lens = self.runner.seq_lens_cpu[:num_reqs].to(self.runner.device,
- non_blocking=True)
+ query_start_loc_cpu = self.runner.query_start_loc_cpu[:num_reqs + 1]
+ query_start_loc = query_start_loc_cpu.to(self.runner.device,
+ non_blocking=True)
+ seq_lens_cpu = self.runner.seq_lens_cpu[:num_reqs]
+ seq_lens = seq_lens_cpu.to(self.runner.device, non_blocking=True)
block_table = (
self.runner.input_batch.block_table.get_device_tensor()[:num_reqs])
slot_mapping = self.runner.slot_mapping_cpu[:num_actual_tokens].to(
self.runner.device, non_blocking=True).long()
+ # for local attention
+ local_attn_metadata = None
+ if self.runner.attention_chunk_size is not None:
+ seqlens_q_local_np, virt_q_cu_seqlens_np, virt_k_seqlens_np, \
+ virt_block_table = make_local_attention_virtual_batches(
+ self.runner.attention_chunk_size,
+ self.runner.query_start_loc_np[:num_reqs + 1],
+ self.runner.seq_lens_np[:num_reqs],
+ block_table,
+ self.runner.block_size,
+ )
+ local_attn_metadata = FlashAttentionMetadata.LocalAttentionMetadata(
+ local_query_start_loc=torch.from_numpy(
+ virt_q_cu_seqlens_np).to(self.runner.device,
+ non_blocking=True),
+ local_seqused_k=torch.from_numpy(virt_k_seqlens_np).to(
+ self.runner.device, non_blocking=True),
+ local_block_table=virt_block_table,
+ local_max_query_len=seqlens_q_local_np.max(),
+ local_max_seq_len=virt_k_seqlens_np.max(),
+ )
+
use_cascade = common_prefix_len > 0
if use_cascade:
- # TODO: Optimize.
cu_prefix_query_lens = torch.tensor([0, num_actual_tokens],
dtype=torch.int32,
device=self.runner.device)
@@ -149,6 +348,7 @@ def build(self, num_reqs: int, num_actual_tokens: int, max_query_len: int,
cu_prefix_query_lens=cu_prefix_query_lens,
prefix_kv_lens=prefix_kv_lens,
suffix_kv_lens=suffix_kv_lens,
+ local_attn_metadata=local_attn_metadata,
)
return attn_metadata
@@ -167,6 +367,7 @@ def __init__(
blocksparse_params: Optional[dict[str, Any]] = None,
logits_soft_cap: Optional[float] = None,
attn_type: AttentionType = AttentionType.DECODER,
+ use_irope: bool = False,
) -> None:
if blocksparse_params is not None:
raise ValueError(
@@ -203,6 +404,7 @@ def __init__(
"encoder/decoder cross-attention "
"are not implemented for "
"FlashAttentionImpl")
+ self.use_irope = use_irope
self.vllm_flash_attn_version = get_flash_attn_version()
if is_quantized_kv_cache(self.kv_cache_dtype) \
and not flash_attn_supports_fp8():
@@ -265,8 +467,7 @@ def forward(
layer._k_scale,
layer._v_scale,
)
- descale_shape = (attn_metadata.query_start_loc.shape[0] - 1,
- key.shape[1])
+
if self.kv_cache_dtype.startswith("fp8"):
key_cache = key_cache.view(torch.float8_e4m3fn)
value_cache = value_cache.view(torch.float8_e4m3fn)
@@ -278,22 +479,41 @@ def forward(
query = query.reshape((num_tokens, num_heads, head_size))
# Compute attention and update output up to `num_actual_tokens`.
- if not attn_metadata.use_cascade:
- # Regular attention (common case).
+ use_local_attn = \
+ (self.use_irope and attn_metadata.local_attn_metadata is not None)
+
+ if not attn_metadata.use_cascade or use_local_attn:
+ if use_local_attn:
+ assert attn_metadata.local_attn_metadata is not None
+ local_metadata = attn_metadata.local_attn_metadata
+ cu_seqlens_q = local_metadata.local_query_start_loc
+ seqused_k = local_metadata.local_seqused_k
+ max_seqlen_q = local_metadata.local_max_query_len
+ max_seqlen_k = local_metadata.local_max_seq_len
+ block_table = local_metadata.local_block_table
+ else:
+ cu_seqlens_q = attn_metadata.query_start_loc
+ seqused_k = attn_metadata.seq_lens
+ max_seqlen_q = attn_metadata.max_query_len
+ max_seqlen_k = attn_metadata.max_seq_len
+ block_table = attn_metadata.block_table
+
+ descale_shape = (cu_seqlens_q.shape[0] - 1, key.shape[1])
+
flash_attn_varlen_func(
q=query[:num_actual_tokens],
k=key_cache,
v=value_cache,
out=output[:num_actual_tokens],
- cu_seqlens_q=attn_metadata.query_start_loc,
- max_seqlen_q=attn_metadata.max_query_len,
- seqused_k=attn_metadata.seq_lens,
- max_seqlen_k=attn_metadata.max_seq_len,
+ cu_seqlens_q=cu_seqlens_q,
+ max_seqlen_q=max_seqlen_q,
+ seqused_k=seqused_k,
+ max_seqlen_k=max_seqlen_k,
softmax_scale=self.scale,
causal=True,
alibi_slopes=self.alibi_slopes,
window_size=self.sliding_window,
- block_table=attn_metadata.block_table,
+ block_table=block_table,
softcap=self.logits_soft_cap,
fa_version=self.vllm_flash_attn_version,
q_descale=layer._q_scale.expand(descale_shape),
@@ -302,6 +522,8 @@ def forward(
)
return output
+ assert not use_local_attn, (
+ "Cascade attention does not support local attention.")
# Cascade attention (rare case).
cascade_attention(
output[:num_actual_tokens],
diff --git a/vllm/v1/attention/backends/triton_attn.py b/vllm/v1/attention/backends/triton_attn.py
index 15b49b14c1dd..5f9610470567 100644
--- a/vllm/v1/attention/backends/triton_attn.py
+++ b/vllm/v1/attention/backends/triton_attn.py
@@ -70,6 +70,7 @@ def __init__(
blocksparse_params: Optional[dict[str, Any]] = None,
logits_soft_cap: Optional[float] = None,
attn_type: AttentionType = AttentionType.DECODER,
+ use_irope: bool = False,
) -> None:
if blocksparse_params is not None:
raise ValueError(
@@ -86,6 +87,7 @@ def __init__(
else:
self.sliding_window = (sliding_window - 1, 0)
self.kv_cache_dtype = kv_cache_dtype
+ self.use_irope = use_irope
assert self.num_heads % self.num_kv_heads == 0
self.num_queries_per_kv = self.num_heads // self.num_kv_heads
@@ -156,24 +158,41 @@ def forward(
layer._v_scale,
)
+ use_local_attn = \
+ (self.use_irope and attn_metadata.local_attn_metadata is not None)
+
+ if use_local_attn:
+ assert attn_metadata.local_attn_metadata is not None
+ local_metadata = attn_metadata.local_attn_metadata
+ cu_seqlens_q = local_metadata.local_query_start_loc
+ sequesd_k = local_metadata.local_seqused_k
+ max_seqlen_q = local_metadata.local_max_query_len
+ max_seqlen_k = local_metadata.local_max_seq_len
+ block_table = local_metadata.local_block_table
+ else:
+ cu_seqlens_q = attn_metadata.query_start_loc
+ sequesd_k = attn_metadata.seq_lens
+ max_seqlen_q = attn_metadata.max_query_len
+ max_seqlen_k = attn_metadata.max_seq_len
+ block_table = attn_metadata.block_table
+
# Compute attention and update output up to `num_actual_tokens`.
- chunked_prefill_paged_decode(
- query=query[:num_actual_tokens],
- key=key[:num_actual_tokens],
- value=value[:num_actual_tokens],
- output=output[:num_actual_tokens],
- kv_cache_dtype=self.kv_cache_dtype,
- key_cache=key_cache,
- value_cache=value_cache,
- block_table=attn_metadata.block_table,
- query_start_loc=attn_metadata.query_start_loc,
- seq_lens=attn_metadata.seq_lens,
- max_seq_len=attn_metadata.max_seq_len,
- max_query_len=attn_metadata.max_query_len,
- k_scale=layer._k_scale,
- v_scale=layer._v_scale,
- alibi_slopes=self.alibi_slopes,
- sliding_window=self.sliding_window[0],
- sm_scale=self.scale)
+ chunked_prefill_paged_decode(query=query[:num_actual_tokens],
+ key=key[:num_actual_tokens],
+ value=value[:num_actual_tokens],
+ output=output[:num_actual_tokens],
+ kv_cache_dtype=self.kv_cache_dtype,
+ key_cache=key_cache,
+ value_cache=value_cache,
+ block_table=block_table,
+ query_start_loc=cu_seqlens_q,
+ seq_lens=sequesd_k,
+ max_seq_len=max_seqlen_k,
+ max_query_len=max_seqlen_q,
+ k_scale=layer._k_scale,
+ v_scale=layer._v_scale,
+ alibi_slopes=self.alibi_slopes,
+ sliding_window=self.sliding_window[0],
+ sm_scale=self.scale)
return output
diff --git a/vllm/v1/worker/gpu_model_runner.py b/vllm/v1/worker/gpu_model_runner.py
index 82b07c6cd327..5133c637f0e0 100644
--- a/vllm/v1/worker/gpu_model_runner.py
+++ b/vllm/v1/worker/gpu_model_runner.py
@@ -113,6 +113,7 @@ def __init__(
self.num_kv_heads = model_config.get_num_kv_heads(parallel_config)
self.head_size = model_config.get_head_size()
self.hidden_size = model_config.get_hidden_size()
+ self.attention_chunk_size = model_config.attention_chunk_size
self.attn_backend = get_attn_backend(
self.head_size,
``` |