Instructions to use Timonafri/e2b_fin2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Local Apps Settings
- Unsloth Desktop
File size: 311,434 Bytes
65f521e | 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 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 | """
2026.6.7
2026.6.9
5.5.0
1.7.0
__UNSLOTH_VERSIONING__
"""
# Unsloth auto generated code
# Copyright 2023-present Daniel Han-Chen, Michael Han-Chen & the Unsloth team. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from torch import Tensor
import torch
import torch.nn as nn
from torch.nn import functional as F
from unsloth_zoo.temporary_patches.common import torch_compile
from typing import Any, List, Optional, Tuple, Union, Dict, Set, Callable
from trl.trainer.grpo_trainer import (Any, AutoModelForSequenceClassification, AutoProcessor, AutoTokenizer, BaseTunerLayer, Callable, CommitScheduler, Dataset, DatasetCard, DatasetCardData, DistributedBackend, EnvironmentFactory, GRPOConfig, GRPOTrainer, GenerationConfig, IterableDataset, LoraConfig, Path, PeftConfig, PeftModel, PreTrainedModel, PreTrainedTokenizerBase, ProcessorMixin, RepeatSampler, RewardFunc, RolloutFunc, Sampler, SyncRefModelCallback, TrainerCallback, VLLMGeneration, Version, _BaseTrainer, _ForwardRedirection, add_response_schema, apply_chat_template, asyncio, atexit, copy, create_model_from_path, create_repo, defaultdict, deque, disable_dropout_in_model, disable_gradient_checkpointing, gather, gather_object, get_config_model_id, get_peft_model, get_training_chat_template, identity, inspect, is_chat_template_prefix_preserving, is_conversational, is_jmespath_available, is_liger_kernel_available, is_peft_available, is_peft_model, is_rich_available, logger, math, nanmax, nanmin, nanstd, nn, np, nullcontext, os, pad, parse_response, pd, peft, pkg_resources, prepare_deepspeed, prepare_fsdp, prepare_multimodal_messages, print_prompt_completions_sample, profiling_context, profiling_decorator, selective_log_softmax, set_seed, shuffle_sequence_dict, shutdown_event_loop_in_daemon, split_pixel_values_by_grid, split_tensor_dict, start_event_loop_in_daemon, supports_tool_calling, sys, textwrap, time, torch, transformers, unsplit_pixel_values_by_grid, unwrap_model_for_generation, use_adapter, warnings, AutoModelForSequenceClassification, AutoProcessor, AutoTokenizer, BaseTunerLayer, Callable, CommitScheduler, Dataset, DatasetCard, DatasetCardData, DistributedBackend, EnvironmentFactory, GRPOConfig, GRPOTrainer, GenerationConfig, IterableDataset, LoraConfig, PeftConfig, PeftModel, PreTrainedModel, PreTrainedTokenizerBase, ProcessorMixin, RewardFunc, RolloutFunc, SyncRefModelCallback, TrainerCallback, VLLMGeneration, Version, add_response_schema, atexit, copy, create_model_from_path, create_repo, defaultdict, deque, disable_dropout_in_model, gather, get_config_model_id, get_peft_model, get_training_chat_template, identity, inspect, is_chat_template_prefix_preserving, is_jmespath_available, is_liger_kernel_available, is_peft_available, is_peft_model, logger, nn, np, os, pad, parse_response, pd, peft, pkg_resources, prepare_deepspeed, prepare_fsdp, set_seed, shutdown_event_loop_in_daemon, start_event_loop_in_daemon, supports_tool_calling, sys, time, torch, transformers, warnings, Version, copy, gather, is_conversational, np, os, pad, parse_response, profiling_context, torch, transformers, Any, apply_chat_template, copy, disable_gradient_checkpointing, gather, gather_object, is_conversational, math, nanmax, nanmin, nanstd, np, os, pad, pd, peft, prepare_multimodal_messages, torch, use_adapter, gather, np, os, pad, profiling_context, torch, transformers, unwrap_model_for_generation, math, np, os, pad, selective_log_softmax, torch, transformers, Any, np, profiling_decorator, shuffle_sequence_dict, split_pixel_values_by_grid, split_tensor_dict, torch, unsplit_pixel_values_by_grid, PeftModel, PreTrainedModel, is_peft_available, logger, os, peft, torch, GRPOTrainer, gather, inspect, nanmax, nanmin, np, os, pad, time, torch)
import os
import math
import logging
from typing import *
from dataclasses import dataclass, field
from packaging.version import Version
import torch
import numpy as np
from contextlib import nullcontext
from torch.nn import functional as F
import inspect
from transformers import DataCollatorForSeq2Seq, DataCollatorForLanguageModeling as TransformersDataCollatorForLanguageModeling
from transformers.training_args import ParallelMode
from unsloth_zoo.device_type import DEVICE_TYPE, device_synchronize
# Wrap trainer with padding to right and enable training mode
import functools
from types import MethodType
try:
from unsloth_zoo.gradient_checkpointing import reset_unsloth_gradient_checkpointing_buffers
except:
def reset_unsloth_gradient_checkpointing_buffers(): pass
# Canonical reset lives in unsloth.models._utils so the SFT auto-packing wrapper and the plain
# Trainer loop can import the same helper; fall back to a no-op only if it can't be imported.
try:
from unsloth.models._utils import _unsloth_reset_stray_compile_cache
except Exception:
def _unsloth_reset_stray_compile_cache(self): pass
def prepare_for_training_mode(f):
@functools.wraps(f)
def wrapper(self, *args, **kwargs):
# Drop any torch.compile graph cache poisoned by a stray pre-train forward.
try:
_unsloth_reset_stray_compile_cache(self)
except Exception:
pass
# Finish the previous W&B run if this is a subsequent train() call.
# We do this at the START of train() (not the end) so that
# evaluate() / log() still work after train() completes.
# HF's WandbCallback.setup() will call wandb.init() for the new run.
# See: https://github.com/unslothai/unsloth/issues/3954
if getattr(self, '_unsloth_training_completed', False):
try:
import wandb
if wandb.run is not None:
wandb.finish()
# Reset HF's WandbCallback so it calls wandb.init() for the new run
for cb in self.callback_handler.callbacks:
if type(cb).__name__ == 'WandbCallback':
cb._initialized = False
break
except:
pass
# Enable training mode
_was_training = None
# Get gradient checkpointing setting from training arguments
use_gc = getattr(self.args, 'gradient_checkpointing', True)
if hasattr(self, 'model') and hasattr(self.model, "training"):
_was_training = self.model.training
if hasattr(self, 'model') and hasattr(self.model, "for_training"):
self.model.for_training(use_gradient_checkpointing=use_gc)
output = f(self, *args, **kwargs)
# Restore previous mode when possible
if hasattr(self, 'model') and hasattr(self.model, "for_inference"):
if _was_training is False:
self.model.for_inference()
elif _was_training is True and hasattr(self.model, "for_training"):
self.model.for_training(use_gradient_checkpointing=use_gc)
# Reset gradient checkpointing buffers to free memory while staying ready for next run
try:
reset_unsloth_gradient_checkpointing_buffers()
except:
pass
# Mark that training completed so the next train() call can
# finish this W&B run before starting a new one
self._unsloth_training_completed = True
return output
return wrapper
pass
torch_compile_options = {
"epilogue_fusion" : True,
"max_autotune" : False,
"shape_padding" : True,
"trace.enabled" : False,
"triton.enable_persistent_tma_matmul": torch.cuda.get_device_capability()[0] >= 9,
"cuda.cutlass_epilogue_fusion_enabled": torch.cuda.get_device_capability()[0] >= 9,
"cuda.cutlass_tma_only": torch.cuda.get_device_capability()[0] >= 9,
"cuda.compile_opt_level" : "-O2",
"cuda.enable_cuda_lto" : True,
}
@torch.compile(dynamic = True, fullgraph = True, options = torch_compile_options,)
def chunked_hidden_states_selective_log_softmax(
hidden_states: torch.Tensor,
lm_head: torch.Tensor,
index: torch.Tensor,
chunks: int = 4,
logit_scale_multiply: float = 0.0,
logit_scale_divide: float = 0.0,
logit_softcapping: float = 0.0,
temperature: float = 1.0,
) -> torch.Tensor:
# All Unsloth Zoo code licensed under AGPL3
flat_hidden_states = hidden_states.reshape(-1, hidden_states.shape[-1])
flat_index = index.reshape(-1)
chunked_hidden_states = torch.chunk(flat_hidden_states, chunks=chunks, dim=0)
chunked_index = torch.chunk(flat_index, chunks=chunks, dim=0)
all_per_token_logps = []
for chunk_hidden_states, chunk_index in zip(chunked_hidden_states, chunked_index):
chunk_logits = chunk_hidden_states.to(lm_head.dtype) @ lm_head.t()
if logit_scale_multiply != 0.0:
chunk_logits = chunk_logits * logit_scale_multiply
if logit_scale_divide != 0.0:
chunk_logits = chunk_logits / logit_scale_divide
if logit_softcapping != 0.0:
chunk_logits = logit_softcapping * torch.tanh(chunk_logits / logit_softcapping)
chunk_logits = chunk_logits.to(torch.float32)
if temperature != 1.0:
chunk_logits = chunk_logits / temperature
selected_logits = torch.gather(chunk_logits, dim=-1, index=chunk_index.unsqueeze(-1)).squeeze(-1)
logsumexp_values = torch.logsumexp(chunk_logits, dim=-1)
per_token_logps = selected_logits - logsumexp_values
all_per_token_logps.append(per_token_logps)
all_per_token_logps = torch.concat(all_per_token_logps)
all_per_token_logps = all_per_token_logps.reshape((hidden_states.shape[0], hidden_states.shape[1]))
return all_per_token_logps
@torch.compile(dynamic = True, fullgraph = True, options = torch_compile_options,)
def chunked_selective_log_softmax(
logits,
index,
temperature: float = 1.0,
chunks: int = 4,
):
chunked_logits = torch.chunk(logits.reshape(-1, logits.shape[-1]), chunks = chunks, dim = 0)
chunked_index = torch.chunk(index.reshape(-1), chunks = chunks, dim = 0)
all_per_token_logps = []
# Per-chunk selective_log_softmax.
for chunk_logits, chunk_index in zip(chunked_logits, chunked_index):
chunk_logits = chunk_logits.to(torch.float32)
if temperature != 1.0:
chunk_logits = chunk_logits / temperature
selected_logits = torch.gather(chunk_logits, dim = -1, index = chunk_index.unsqueeze(-1)).squeeze(-1)
logsumexp_values = torch.logsumexp(chunk_logits, dim = -1)
per_token_logps = selected_logits - logsumexp_values
all_per_token_logps.append(per_token_logps)
pass
all_per_token_logps = torch.concat(all_per_token_logps)
all_per_token_logps = all_per_token_logps.reshape((logits.shape[0], logits.shape[1]))
return all_per_token_logps
def calculate_pad_tokens_in_prompt(
input_ids: torch.Tensor,
logits_to_keep: int,
pad_token_id: int
) -> torch.Tensor:
"""Count left-padded tokens per sequence, e.g. [pad, pad, pad, cat] -> 3."""
if logits_to_keep >= input_ids.shape[1]:
raise ValueError("logits_to_keep must be smaller than the sequence length.")
prompt_section = input_ids[:, :-logits_to_keep]
padding_mask = (prompt_section == pad_token_id)
pad_token_counts = padding_mask.sum(dim=1)
return pad_token_counts
def create_completion_attention_mask(
completion_input_ids: torch.Tensor,
left_pad_tokens_per_prompt: torch.Tensor,
max_left_pad: int,
pad_token_id: int
) -> torch.Tensor:
"""Build a completion mask that zeros leading prompt and trailing pad tokens.
For [p,p,p,c,c,c,pad,pad,pad] (p=sliced prompt, c=completion, pad=padding)
this returns [0,0,0,1,1,1,0,0,0].
"""
batch_size, completion_len = completion_input_ids.shape
device = completion_input_ids.device
num_tokens_to_mask = max_left_pad - left_pad_tokens_per_prompt
indices = torch.arange(completion_len, device=device).unsqueeze(0)
shift_mask = indices >= num_tokens_to_mask.unsqueeze(1)
non_padding_mask = (completion_input_ids != pad_token_id)
final_mask = shift_mask & non_padding_mask
return final_mask
def left_pack_padding(tensor: torch.Tensor, pad_id: int) -> torch.Tensor:
"""Move all padding tokens in each sequence to the right."""
mask = (tensor != pad_id)
# stable=True since the binary mask is unordered.
sorted_indices = torch.argsort(mask, dim=1, descending=True, stable=True)
packed_tensor = torch.gather(tensor, 1, sorted_indices)
return packed_tensor
def align_logprobs_with_mask(
logprob_tensor: torch.Tensor,
attention_mask: torch.Tensor,
pad_value: float = 0.0
) -> torch.Tensor:
"""Align a log probability tensor with a given attention mask."""
device = logprob_tensor.device
batch_size, logprob_seq_len = logprob_tensor.shape
mask_seq_len = attention_mask.shape[1]
padded_logprobs = torch.full(
attention_mask.shape,
fill_value=pad_value,
dtype=logprob_tensor.dtype,
device=device
)
left_pad_counts = torch.argmax(attention_mask, dim=1)
cols = torch.arange(logprob_seq_len, device=device)
dest_indices = left_pad_counts.unsqueeze(1) + cols
# Destination row indices, shape [batch_size, logprob_seq_len].
row_indices = torch.arange(batch_size, device=device).unsqueeze(1).expand_as(dest_indices)
# Keep only in-bounds destinations, then scatter via advanced indexing.
valid_mask = dest_indices < mask_seq_len
valid_rows = row_indices[valid_mask]
valid_cols = dest_indices[valid_mask]
valid_vals = logprob_tensor[valid_mask]
padded_logprobs[valid_rows, valid_cols] = valid_vals
return padded_logprobs
def align_completion_tool_mask(
tool_mask: torch.Tensor,
completion_mask: torch.Tensor,
) -> torch.Tensor:
"""Align a raw completion-length tool/env mask with Unsloth's repacked loss mask."""
if tool_mask is None:
return completion_mask
if tool_mask.shape[0] != completion_mask.shape[0]:
raise ValueError("tool_mask batch size must match completion_mask batch size.")
tool_mask = tool_mask.to(device=completion_mask.device)
if tool_mask.shape == completion_mask.shape:
aligned_tool_mask = tool_mask
else:
aligned_tool_mask = align_logprobs_with_mask(
tool_mask,
completion_mask,
pad_value=0,
)
return completion_mask * aligned_tool_mask.to(dtype=completion_mask.dtype)
def autotune_batch_and_chunks(
total_input_rows,
seq_len,
hidden_size,
vocab_size,
dtype_bytes=16,
multiplier=None
):
if multiplier is None:
final_m = max(4, seq_len // 4096)
else:
final_m = multiplier
if torch.cuda.is_available():
free_bytes, _ = torch.cuda.mem_get_info()
limit_gb = (free_bytes / (1024**3))*.80
elif hasattr(torch, "xpu") and torch.xpu.is_available():
# XPU: estimate free memory as total - reserved.
total_mem = torch.xpu.get_device_properties(0).total_memory
reserved_mem = torch.xpu.memory_reserved()
free_bytes = total_mem - reserved_mem
limit_gb = (free_bytes / (1024**3)) * 0.80
else:
# Fallback: assume 8GB available.
limit_gb = 8.0
bytes_to_gb = 1024**3
b_vals = torch.arange(total_input_rows, 0, -1, device='cpu', dtype=torch.float32)
hidden_gb = (b_vals * seq_len * hidden_size * dtype_bytes) / bytes_to_gb
base_logits = ((b_vals/total_input_rows) * b_vals * seq_len * vocab_size * dtype_bytes) / bytes_to_gb
logits_gb = base_logits / final_m
total_mem_gb = hidden_gb + logits_gb
valid_mask = total_mem_gb <= limit_gb
valid_indices = torch.nonzero(valid_mask, as_tuple=False)
if valid_indices.shape[0] == 0:
#This means your GPU will OOM
return 4, final_m
best_idx = valid_indices[0].item()
final_b = int(b_vals[best_idx].item())
return final_b, final_m
def sanitize_logprob(logprob):
"""Local port of trl.scripts.vllm_serve.sanitize_logprob.
Filters NaN logprobs from vLLM outputs."""
value = logprob.logprob
if math.isnan(value):
logging.getLogger(__name__).warning(
f"Generated NaN logprob, token logprob '{logprob}' will be ignored"
)
return None
return value
def _unsloth_get_final_logit_softcapping(config):
"""Return final_logit_softcapping for a model config, falling back to the
nested text sub-config for composite models. Handles both:
- Gemma-4-style configs where the attribute lives on ``config.text_config``
- T5Gemma-style composite configs where the text sub-config is only
reachable via ``config.get_text_config()``
Returns 0 if unset, matching the previous behaviour.
"""
softcap = getattr(config, "final_logit_softcapping", None)
if softcap is None:
text_cfg = getattr(config, "text_config", None)
if text_cfg is None:
get_text_config = getattr(config, "get_text_config", None)
if callable(get_text_config):
try:
text_cfg = get_text_config()
except (TypeError, ValueError):
text_cfg = None
if text_cfg is not None and text_cfg is not config:
softcap = getattr(text_cfg, "final_logit_softcapping", None)
return 0 if softcap is None else softcap
def _unsloth_get_mm_token_id(processing_class, attr_name, token):
tokenizer = getattr(processing_class, "tokenizer", processing_class)
token_id = getattr(processing_class, attr_name, None)
if token_id is None:
token_id = getattr(tokenizer, attr_name, None)
convert_tokens_to_ids = getattr(tokenizer, "convert_tokens_to_ids", None)
if token_id is None and convert_tokens_to_ids is not None:
token_id = convert_tokens_to_ids(token)
if type(token_id) is int and token_id >= 0:
if token_id != getattr(tokenizer, "unk_token_id", None):
return token_id
return None
def _unsloth_fix_mm_token_type_ids(
processing_class, input_ids, mm_token_type_ids = None, completion_ids = None
):
image_token_id = _unsloth_get_mm_token_id(
processing_class, "image_token_id", "<|image_pad|>"
)
video_token_id = _unsloth_get_mm_token_id(
processing_class, "video_token_id", "<|video_pad|>"
)
if image_token_id is not None or video_token_id is not None:
rebuilt = input_ids.new_zeros(input_ids.shape)
if image_token_id is not None:
rebuilt = rebuilt.masked_fill(input_ids == image_token_id, 1)
if video_token_id is not None:
rebuilt = rebuilt.masked_fill(input_ids == video_token_id, 2)
return rebuilt
if (
mm_token_type_ids is not None
and completion_ids is not None
and mm_token_type_ids.shape[0] == input_ids.shape[0]
and mm_token_type_ids.shape[1] + completion_ids.shape[1] == input_ids.shape[1]
):
return torch.cat(
[mm_token_type_ids, mm_token_type_ids.new_zeros(completion_ids.shape)],
dim = 1,
)
return mm_token_type_ids
def _unsloth_clear_stateful_mrope(model):
modules = getattr(model, "modules", None)
if modules is None:
return False
cleared = False
for module in modules():
if hasattr(module, "compute_3d_position_ids") and hasattr(module, "rope_deltas"):
module.rope_deltas = None
cleared = True
return cleared
def grpo_compute_loss(
ref,
new,
old,
sampling_per_token_logps,
input_ids,
mask,
beta,
advantages,
**kwargs
):
# All Unsloth Zoo code licensed under AGPL3
# Optional argument defaults.
loss_type = kwargs.get("loss_type", "grpo")
epsilon_low = kwargs.get("epsilon_low", 0.2)
epsilon_high = kwargs.get("epsilon_high", 0.2)
max_completion_length = kwargs.get("max_completion_length", 8192)
delta = kwargs.get("delta", None)
importance_sampling_level = kwargs.get("importance_sampling_level", "token")
num_items_in_batch = kwargs.get("num_items_in_batch", None)
current_gradient_accumulation_steps = kwargs.get("current_gradient_accumulation_steps", 1)
num_processes = kwargs.get("num_processes", 1)
use_vllm = kwargs.get("use_vllm", False)
vllm_importance_sampling_cap = kwargs.get("vllm_importance_sampling_cap", 2.0)
get_sapo_token_loss = kwargs.get("get_sapo_token_loss", None)
sapo_temperature_pos = kwargs.get("sapo_temperature_pos", 1.0)
sapo_temperature_neg = kwargs.get("sapo_temperature_neg", 1.05)
get_gamma_weights = kwargs.get("get_gamma_weights", None)
vespo_k_pos = kwargs.get("vespo_k_pos", 2.0)
vespo_lambda_pos = kwargs.get("vespo_lambda_pos", 3.0)
vespo_k_neg = kwargs.get("vespo_k_neg", 3.0)
vespo_lambda_neg = kwargs.get("vespo_lambda_neg", 2.0)
get_off_policy_mask = kwargs.get("get_off_policy_mask", None)
off_policy_mask_threshold = kwargs.get("off_policy_mask_threshold", None)
input_ids = input_ids.unsqueeze(-1)
if advantages.dim() == 1:
advantages = advantages.unsqueeze(1)
if off_policy_mask_threshold is not None:
off_policy_mask = get_off_policy_mask(
advantages=advantages,
per_token_logps=new,
old_per_token_logps=old,
mask=mask,
off_policy_threshold=off_policy_mask_threshold,
)
with torch.no_grad():
if use_vllm and sampling_per_token_logps is not None:
# Filter out extra leading prompt tokens after left-padding input_ids.
importance_sampling_ratio = torch.exp((old * mask) - sampling_per_token_logps)
importance_sampling_ratio = torch.clamp(
importance_sampling_ratio, max=vllm_importance_sampling_cap
)
pass
# Must detach when old is None: exp(new - new.detach()) == 1 but keeps grads correct.
if old is not None:
log_ratio = new - old
else:
log_ratio = new - new.detach()
if importance_sampling_level == "token":
log_importance_weights = log_ratio
elif importance_sampling_level == "sequence":
log_importance_weights = (log_ratio * mask).sum(-1) / mask.sum(-1).clamp(min=1.0)
log_importance_weights = log_importance_weights.unsqueeze(-1)
else:
raise ValueError(
f"Unknown importance sampling level: {importance_sampling_level}. Possible values are 'token' "
"and 'sequence'."
)
coef_1 = torch.exp(log_importance_weights)
# Reverse KL: low-variance low-bias estimator as used in the GRPO paper.
if beta != 0.0:
kl_i = torch.exp(ref - new) - (ref - new) - 1.0
else:
# Zeros with the correct shape.
if importance_sampling_level == "sequence":
kl_i = new.new_zeros(new.size(0), 1)
else:
kl_i = torch.zeros_like(new)
if loss_type == "cispo":
clamped_ratios = torch.clamp(coef_1, max=epsilon_high).detach()
loss_i = -clamped_ratios * advantages * new
elif loss_type in ["grpo", "bnpo", "dr_grpo", "dapo"]:
coef_2 = torch.clamp(coef_1, 1 - epsilon_low, 1 + epsilon_high)
if delta is not None:
loss_1 = torch.clamp(coef_1, max=delta) * advantages
else:
loss_1 = coef_1 * advantages
pass
loss_2 = coef_2 * advantages
loss_i = -torch.min(loss_1, loss_2)
elif loss_type == "sapo":
if get_sapo_token_loss is None:
raise Exception(f"sapo is only available in TRL 0.26.0+")
loss_i = torch.empty_like(coef_1)
positive_advantages_mask = advantages.repeat([1, coef_1.shape[1]]) > 0
# With n_chunks some tensors may be empty; guard the indexing.
if coef_1[positive_advantages_mask].numel() != 0:
loss_i[positive_advantages_mask] = get_sapo_token_loss(
coef_1[positive_advantages_mask], sapo_temperature_pos
)
if coef_1[~positive_advantages_mask].numel() != 0:
loss_i[~positive_advantages_mask] = get_sapo_token_loss(
coef_1[~positive_advantages_mask], sapo_temperature_neg
)
loss_i = -loss_i * advantages
elif loss_type == "vespo":
if get_gamma_weights is None:
raise Exception("vespo is only available in TRL 0.26.0+")
phi_seq = get_gamma_weights(
advantages=advantages,
log_ratio_per_token=log_ratio,
mask=mask,
importance_sampling_ratio=kwargs.get("importance_sampling_ratio"),
k_pos=vespo_k_pos,
lambda_pos=vespo_lambda_pos,
k_neg=vespo_k_neg,
lambda_neg=vespo_lambda_neg,
)
loss_i = -phi_seq * advantages * new
else:
raise ValueError(f"Unknown loss type: {loss_type}")
if off_policy_mask_threshold is not None:
loss_i = loss_i * off_policy_mask
if use_vllm and sampling_per_token_logps is not None:
loss_i = loss_i * importance_sampling_ratio
# delta for the metric.
with torch.no_grad():
delta = torch.abs(old - sampling_per_token_logps)
delta = delta * mask
flat_is_ratio = importance_sampling_ratio * mask
else:
delta = torch.tensor([]).detach()
flat_is_ratio = torch.tensor([]).detach()
if beta != 0.0:
loss_i = loss_i + beta * kl_i
mask = mask.to(torch.float32)
n_mask_per_reward = mask.sum(1)
# https://github.com/huggingface/trl/blob/e8b8499f1f8d76838155b515e414ee98f757d6d5/trl/trainer/grpo_trainer.py#L1624
if loss_type in ["grpo", "sapo"]:
loss = ((loss_i * mask).sum(-1) / mask.sum(-1).clamp(min=1.0)).mean()
loss = loss / current_gradient_accumulation_steps
elif loss_type == "bnpo":
loss = (loss_i * mask).sum() / mask.sum().clamp(min=1.0)
loss = loss / current_gradient_accumulation_steps
elif loss_type == "dr_grpo":
loss = (loss_i * mask).sum() / (loss_i.size(0) * max_completion_length)
loss = loss / current_gradient_accumulation_steps
elif loss_type in ["cispo", "dapo", "vespo"]:
normalizer = num_items_in_batch/ num_processes
loss = (loss_i * mask).sum() / normalizer
else:
raise ValueError(f"Unknown loss type: {loss_type}")
# Folded metrics.
def masked_batch_mean(x):
with torch.inference_mode():
completion_length = n_mask_per_reward.mean()
if x.shape[1] == 1: # when importance_sampling_level == "sequence"
return completion_length, x.mean()
else:
mean_kl_per_reward = (x * mask).sum(1) / n_mask_per_reward
mean_kl = mean_kl_per_reward.mean()
return completion_length, mean_kl
completion_length, mean_kl = masked_batch_mean(kl_i)
return loss, completion_length, mean_kl, delta, flat_is_ratio, coef_1, mask
class UnslothEfficientGRPO(torch.autograd.Function):
# All Unsloth Zoo code licensed under AGPL3
@staticmethod
def forward(ctx, _new_logps, _old_logps, _ref_logps, _sampling_per_token_logps, lm_head, _input_ids, _mask, _advantages, beta, scaler = None, n_chunks = 1, extra_kwargs=None):
if extra_kwargs is None:
extra_kwargs = {}
def compute_loss(new_logps, old_logps, ref_logps, sampling_per_token_logps, input_ids, mask, advantages, scaling):
loss, completion_length, mean_kl, delta, flat_is_ratio, coef_1, _mask = grpo_compute_loss(
ref_logps,
new_logps,
old_logps,
sampling_per_token_logps,
input_ids,
mask,
beta,
advantages,
**extra_kwargs,
)
# Scale for mixed precision; return loss.detach() or autograd uses 2x VRAM.
scaled_loss = loss * scaling
return scaled_loss, (loss.detach(), completion_length, mean_kl, delta, flat_is_ratio, coef_1)
pass
device =_new_logps.device
grad_inputs = torch.empty_like(_new_logps)
accumulated_loss = torch.zeros(1, device = device)[0]
accumulated_completion_length = torch.zeros(1, device = device)[0]
accumulated_mean_kl = torch.zeros(1, device = device)[0]
accumulated_delta = []
accumulated_flat_is_ratio = []
accumulated_coef_1 = []
def accumulate_chunk(
new_logps_j,
old_logps_j,
ref_logps_j,
sampling_per_token_logps_j,
input_ids_j,
mask_j,
advantages_j,
scaling,
grad_inputs_j,
):
(chunk_grad_input,), (chunk_loss, (unscaled_loss, chunk_completion_length, chunk_mean_kl, chunk_delta, chunk_flat_is_ratio, chunk_coef_1)) = torch.func.grad_and_value(
compute_loss,
argnums = (0,),
has_aux = True,
)(new_logps_j, old_logps_j, ref_logps_j, sampling_per_token_logps_j, input_ids_j, mask_j, advantages_j, scaling)
accumulated_loss .add_(unscaled_loss)
accumulated_completion_length.add_(chunk_completion_length)
accumulated_mean_kl .add_(chunk_mean_kl)
accumulated_delta .append(chunk_delta)
accumulated_flat_is_ratio .append(chunk_flat_is_ratio)
accumulated_coef_1 .append(chunk_coef_1)
grad_inputs_j[:] = chunk_grad_input
pass
accumulate_chunk = torch.compile(
accumulate_chunk,
fullgraph = True,
# [TODO] Dynamic marking causes torch.compile errors if sequence length is long
dynamic = True,
options = torch_compile_options,
)
grad_inputs_chunks = torch.chunk(grad_inputs, chunks = n_chunks, dim = 0)
new_logps = torch.chunk(_new_logps, chunks = n_chunks, dim = 0)
if _old_logps is not None:
old_logps = torch.chunk(_old_logps, chunks = n_chunks, dim = 0)
else:
old_logps = [None] * n_chunks
if _ref_logps is not None:
ref_logps = torch.chunk(_ref_logps, chunks = n_chunks, dim = 0)
else:
ref_logps = [None] * n_chunks
if _sampling_per_token_logps is not None:
sampling_per_token_logps = torch.chunk(_sampling_per_token_logps, chunks = n_chunks, dim = 0)
else:
sampling_per_token_logps = [None] * n_chunks
input_ids = torch.chunk(_input_ids, chunks = n_chunks, dim = 0)
mask = torch.chunk(_mask, chunks = n_chunks, dim = 0)
advantages = torch.chunk(_advantages, chunks = n_chunks, dim = 0)
# Mixed precision scaling if present.
scaling = scaler.get_scale() if scaler is not None else 1.0
for (grad_inputs_j, new_logps_j, old_logps_j, ref_logps_j, sampling_per_token_logps_j, input_ids_j, mask_j, advantages_j, ) in \
zip(grad_inputs_chunks, new_logps, old_logps, ref_logps, sampling_per_token_logps, input_ids, mask, advantages):
# [TODO] Dynamic marking causes torch.compile errors if sequence length is long
# mark_dynamic(new_hidden_states_j)
# mark_dynamic(ref_hidden_states_j)
# if old_hidden_states_j is not None:
# mark_dynamic(old_hidden_states_j)
# mark_dynamic(input_ids_j)
# mark_dynamic(mask_j)
accumulate_chunk(
new_logps_j,
old_logps_j,
ref_logps_j,
sampling_per_token_logps_j,
input_ids_j,
mask_j,
advantages_j,
scaling,
grad_inputs_j,
)
pass
grad_inputs .div_(n_chunks)
accumulated_loss .div_(n_chunks)
accumulated_completion_length.div_(n_chunks)
accumulated_mean_kl .div_(n_chunks)
if _sampling_per_token_logps is not None:
accumulated_delta = torch.cat(accumulated_delta, dim=0)
accumulated_flat_is_ratio = torch.cat(accumulated_flat_is_ratio, dim=0)
else:
accumulated_delta = None
accumulated_flat_is_ratio = None
accumulated_coef_1 = torch.cat(accumulated_coef_1, dim=0)
ctx.save_for_backward(grad_inputs)
return (
accumulated_loss,
accumulated_completion_length,
accumulated_mean_kl,
accumulated_delta,
accumulated_flat_is_ratio,
accumulated_coef_1
)
pass
@staticmethod
def backward(ctx, grad_output, dcompletion_length, dmean_kl, ddelta, ddflat_is_ratio, dcoef_1):
(grad_input,) = ctx.saved_tensors
return (grad_input, None, None, None, None, None, None, None, None, None, None, None)
pass
def grpo_accumulated_loss(
trainer,
input_ids,
attention_mask,
logits_to_keep,
completion_mask,
advantages,
old_logps,
ref_logps,
n_chunks = -1,
tool_mask = None,
**kwargs,
):
# All Unsloth Zoo code licensed under AGPL3
bsz, qlen = input_ids.shape
pixel_values = kwargs.get('pixel_values',None)
image_grid_thw = kwargs.get('image_grid_thw',None)
pixel_attention_mask = kwargs.get('pixel_attention_mask',None)
image_sizes = kwargs.get('image_sizes',None)
num_images = kwargs.get('num_images',None)
# Transformers 5.x requires token_type_ids/mm_token_type_ids for some vision models
token_type_ids = kwargs.get('token_type_ids',None)
mm_token_type_ids = kwargs.get('mm_token_type_ids',None)
if mm_token_type_ids is not None or image_grid_thw is not None:
mm_token_type_ids = _unsloth_fix_mm_token_type_ids(
trainer.processing_class, input_ids, mm_token_type_ids
)
sampling_per_token_logps = kwargs.get("sampling_per_token_logps", None) if getattr(trainer, "vllm_importance_sampling_correction", False) else None
temperature = kwargs.get("temperature", 1.0)
logit_scale_multiply = kwargs.get("logit_scale_multiply", 0.0)
logit_scale_divide = kwargs.get("logit_scale_divide", 0.0)
logit_softcapping = kwargs.get("logit_softcapping", 0.0)
prev_max_left_pad = kwargs.get("max_left_pad", 0) # max_left_pad for LLM training, enabled by default.
# Pop from kwargs to avoid downstream issues.
_ = kwargs.pop("sampling_per_token_logps", None)
kwargs["vllm_importance_sampling_cap"] = trainer.vllm_importance_sampling_cap if sampling_per_token_logps is not None else None
kwargs["get_sapo_token_loss"] = trainer.get_sapo_token_loss if hasattr(trainer, "get_sapo_token_loss") else None
kwargs["sapo_temperature_pos"] = trainer.args.sapo_temperature_pos if hasattr(trainer.args, "sapo_temperature_pos") else None
kwargs["sapo_temperature_neg"] = trainer.args.sapo_temperature_neg if hasattr(trainer.args, "sapo_temperature_neg") else None
kwargs["get_gamma_weights"] = trainer.get_gamma_weights if hasattr(trainer, "get_gamma_weights") else None
kwargs["vespo_k_pos"] = trainer.args.vespo_k_pos if hasattr(trainer.args, "vespo_k_pos") else 2.0
kwargs["vespo_k_neg"] = trainer.args.vespo_k_neg if hasattr(trainer.args, "vespo_k_neg") else 3.0
kwargs["vespo_lambda_pos"] = trainer.args.vespo_lambda_pos if hasattr(trainer.args, "vespo_lambda_pos") else 3.0
kwargs["vespo_lambda_neg"] = trainer.args.vespo_lambda_neg if hasattr(trainer.args, "vespo_lambda_neg") else 2.0
kwargs["get_off_policy_mask"] = trainer.get_off_policy_mask if hasattr(trainer, "get_off_policy_mask") else None
kwargs["off_policy_mask_threshold"] = trainer.args.off_policy_mask_threshold if hasattr(trainer.args, "off_policy_mask_threshold") else None
kwargs["use_vllm"] = trainer.use_vllm
# Snap n_chunks to the closest divisor of bsz.
factors = [i for i in range(1, bsz + 1) if bsz % i == 0]
if n_chunks == -1: n_chunks = bsz
n_chunks = factors[min(np.searchsorted(factors, n_chunks), len(factors)-1)]
if not hasattr(trainer, '_autocast_dtype'):
trainer._autocast_dtype = torch.float16 if os.environ.get('ACCELERATE_MIXED_PRECISION', 'fp16') == 'fp16' else torch.bfloat16
if os.environ.get('UNSLOTH_FORCE_FLOAT32', '0') == '1': trainer._autocast_dtype = None
pass
os.environ["UNSLOTH_RETURN_HIDDEN_STATES"] = "1"
lm_head = trainer.model.get_output_embeddings().weight
dtype_bytes = 16 if trainer._autocast_dtype in [torch.float16, torch.bfloat16] else 32
total_rows = input_ids.shape[0]
seq_len = input_ids.shape[1]
hidden_dim = lm_head.shape[1]
vocab_dim = lm_head.shape[0]
if trainer.args.unsloth_grpo_mini_batch is None:
if not hasattr(trainer, "_has_autotuned"):
trainer._has_autotuned = True
B, multiplier = autotune_batch_and_chunks(
total_rows, seq_len, hidden_dim, vocab_dim, dtype_bytes, trainer.args.unsloth_logit_chunk_multiplier
)
trainer.args.unsloth_grpo_mini_batch = max(1, total_rows//B)
trainer.args.unsloth_logit_chunk_multiplier = multiplier
B = trainer.args.unsloth_grpo_mini_batch
multiplier = trainer.args.unsloth_logit_chunk_multiplier
elif trainer._step % trainer.current_gradient_accumulation_steps == 0:
B = trainer.args.unsloth_grpo_mini_batch
multiplier = trainer.args.unsloth_logit_chunk_multiplier
del trainer._has_autotuned
del trainer.args.unsloth_grpo_mini_batch
del trainer.args.unsloth_logit_chunk_multiplier
else:
B = trainer.unsloth_grpo_mini_batch
multiplier = trainer.args.unsloth_logit_chunk_multiplier
else:
if trainer.args.unsloth_grpo_mini_batch > total_rows:
B = total_rows
else:
B = trainer.args.unsloth_grpo_mini_batch
if trainer.args.unsloth_logit_chunk_multiplier is None:
multiplier = max(4, seq_len // 4096)
else:
multiplier = trainer.args.unsloth_logit_chunk_multiplier
if pixel_values is None:
left_pad_tokens_per_prompt = calculate_pad_tokens_in_prompt(input_ids, logits_to_keep, trainer.processing_class.pad_token_id)
# Determine max_left_pad from precomputed logprobs shape for consistency
if old_logps is not None:
max_left_pad = old_logps.shape[1] - logits_to_keep
elif ref_logps is not None:
max_left_pad = ref_logps.shape[1] - logits_to_keep
else:
max_left_pad = torch.max(left_pad_tokens_per_prompt).item()
input_ids = left_pack_padding(input_ids, trainer.processing_class.pad_token_id)
completion_input_ids = input_ids[:, -(logits_to_keep +max_left_pad):]
completion_mask = create_completion_attention_mask(completion_input_ids, left_pad_tokens_per_prompt, max_left_pad, trainer.processing_class.pad_token_id).to(attention_mask.dtype)
if trainer.use_vllm and sampling_per_token_logps is not None and getattr(trainer, "vllm_importance_sampling_correction", False):
sampling_per_token_logps = align_logprobs_with_mask(sampling_per_token_logps, completion_mask)
else:
sampling_per_token_logps = None
completion_mask = align_completion_tool_mask(tool_mask, completion_mask)
attention_mask = input_ids != trainer.processing_class.pad_token_id
attention_mask = attention_mask.to(attention_mask.dtype)
else:
completion_input_ids = input_ids[:, -logits_to_keep:]
completion_mask = align_completion_tool_mask(tool_mask, completion_mask)
unwrapped_model = trainer.accelerator.unwrap_model(trainer.model, keep_fp32_wrapper = False)
for module in unwrapped_model.modules():
if hasattr(module, "_hf_hook") and hasattr(module._hf_hook, "io_same_decice"):
module._hf_hook.io_same_decice = False
pass
all_logprobs_list = []
def slice_sample_axis(value, start, end):
if value is None:
return None
return value[start:end]
import math
total_samples = input_ids.shape[0]
batch_size = math.ceil(total_samples / B)
if isinstance(num_images, torch.Tensor):
num_images = num_images.detach().cpu().reshape(-1).tolist()
if image_grid_thw is not None and pixel_values is not None and num_images is not None:
rows_per_image = image_grid_thw.prod(dim=-1)
rows_per_sample = torch.split(rows_per_image, num_images)
rows_per_sample = torch.stack([s.sum() for s in rows_per_sample])
cum_rows = torch.cat(
[
torch.tensor([0], device=rows_per_sample.device),
rows_per_sample.cumsum(0),
]
)
cum_imgs = torch.tensor([0] + num_images).cumsum(0)
else:
cum_rows = None
cum_imgs = None
input_ids_chunks = []
attention_mask_chunks = []
completion_ids_chunks = []
pixel_values_chunks = []
image_grid_thw_chunks = []
pixel_attention_mask_chunks = []
image_sizes_chunks = []
token_type_ids_chunks = []
mm_token_type_ids_chunks = []
current_pixel_idx = 0
#TRL 0.23.0 batching logic
for start in range(0, total_samples, batch_size):
end = min(start + batch_size, total_samples)
input_ids_chunks.append(input_ids[start:end])
attention_mask_chunks.append(attention_mask[start:end])
completion_ids_chunks.append(completion_input_ids[start:end])
image_sizes_chunks.append(slice_sample_axis(image_sizes, start, end))
token_type_ids_chunks.append(slice_sample_axis(token_type_ids, start, end))
mm_token_type_ids_chunks.append(
slice_sample_axis(mm_token_type_ids, start, end)
)
if image_grid_thw is not None and pixel_values is not None:
if num_images is None:
grid_slice = image_grid_thw[start:end]
batch_pixel_count = grid_slice.prod(dim=-1).sum().item()
start_pixel_idx = current_pixel_idx
end_pixel_idx = current_pixel_idx + batch_pixel_count
current_pixel_idx = end_pixel_idx
else:
start_pixel_idx = cum_rows[start].item()
end_pixel_idx = cum_rows[end].item()
img_start, img_end = cum_imgs[start], cum_imgs[end]
grid_slice = image_grid_thw[img_start:img_end]
image_grid_thw_chunks.append(grid_slice)
pixel_values_chunks.append(pixel_values[start_pixel_idx:end_pixel_idx])
if pixel_attention_mask is not None:
if pixel_attention_mask.shape[0] == pixel_values.shape[0]:
pixel_attention_mask_chunks.append(pixel_attention_mask[start_pixel_idx:end_pixel_idx])
else:
pixel_attention_mask_chunks.append(pixel_attention_mask[start:end])
else:
pixel_attention_mask_chunks.append(None)
else:
pixel_values_chunks.append(None)
image_grid_thw_chunks.append(None)
pixel_attention_mask_chunks.append(None)
zipped_inputs = zip(
input_ids_chunks,
attention_mask_chunks,
pixel_values_chunks,
image_grid_thw_chunks,
pixel_attention_mask_chunks,
image_sizes_chunks,
token_type_ids_chunks,
mm_token_type_ids_chunks,
completion_ids_chunks
)
if trainer._autocast_dtype is None:
autocaster = nullcontext()
else:
autocaster = torch.amp.autocast(device_type = trainer.model.device.type, dtype = trainer._autocast_dtype)
def to_device(tensor, device, non_blocking=True):
if tensor is None: return None
return tensor.to(device, non_blocking=non_blocking)
class Unsloth_Offloaded_Log_Softmax(torch.autograd.Function):
"""Manual gradient checkpointing / CPU offloading for log softmax."""
@staticmethod
def forward(ctx, hidden_states, lm_head, index, chunks,
logit_scale_multiply, logit_scale_divide,
logit_softcapping, temperature):
# Detach so we don't keep the graph (and extra memory) on CPU.
ctx.saved_hidden_states = hidden_states.detach().contiguous().to("cpu", non_blocking=True)
ctx.device = hidden_states.device
ctx.dtype = hidden_states.dtype
ctx.lm_head = lm_head
ctx.lm_head_requires_grad = lm_head.requires_grad
ctx.index = index
ctx.args = (chunks, logit_scale_multiply, logit_scale_divide, logit_softcapping, temperature)
with torch.no_grad():
output = chunked_hidden_states_selective_log_softmax(
hidden_states, lm_head, index, *ctx.args
)
return output
@staticmethod
def backward(ctx, grad_output):
hidden_states = to_device(ctx.saved_hidden_states, ctx.device)
hidden_states = hidden_states.to(ctx.dtype)
hidden_states.requires_grad_(True)
lm_head = ctx.lm_head
# #Possibly redundant lines
# if ctx.lm_head_requires_grad:
# hidden_states.requires_grad_(True)
# else:
# lm_head = lm_head.detach()
index = ctx.index
with torch.enable_grad():
output = chunked_hidden_states_selective_log_softmax(
hidden_states, lm_head, index, *ctx.args
)
torch.autograd.backward(output, grad_output)
return (
hidden_states.grad,
lm_head.grad if ctx.lm_head_requires_grad else None,
None,
None,
None,
None,
None,
None,
)
def efficient_log_softmax(hidden_states, lm_head, index, chunks=32,
logit_scale_multiply=0.0, logit_scale_divide=0.0,
logit_softcapping=0.0, temperature=1, batch_size=8):
if (index.shape[1] <= 1024 and batch_size <= 8) or batch_size==1:
# Normal path is faster / saves a GB under these conditions.
return chunked_hidden_states_selective_log_softmax(
hidden_states,
lm_head,
index,
chunks,
logit_scale_multiply,
logit_scale_divide,
logit_softcapping,
temperature
)
else:
return Unsloth_Offloaded_Log_Softmax.apply(
hidden_states, lm_head, index, chunks,
logit_scale_multiply, logit_scale_divide,
logit_softcapping, temperature
)
def compute_logprobs_chunk(new_hidden_states_chunk, completion_ids, input_ids_chunk):
# Hidden states -> lm_head matmul path; raw logits -> skip matmul and
# skip scale/softcap (model forward already applied them).
chunks = input_ids_chunk.shape[0] * multiplier
if new_hidden_states_chunk.shape[-1] == lm_head.shape[1]:
return efficient_log_softmax(
new_hidden_states_chunk,
lm_head,
completion_ids,
chunks = chunks,
logit_scale_multiply = logit_scale_multiply,
logit_scale_divide = logit_scale_divide,
logit_softcapping = logit_softcapping,
temperature = temperature,
batch_size = B,
)
return chunked_selective_log_softmax(
new_hidden_states_chunk,
completion_ids,
temperature = temperature,
chunks = chunks,
)
for (
input_ids_chunk,
attention_mask_chunk,
pixel_values_chunk,
image_grid_thw_chunk,
pixel_attention_mask_chunk,
image_sizes_chunk,
token_type_ids_chunk,
mm_token_type_ids_chunk,
completion_ids
) in zipped_inputs:
_extra_vision_kwargs = {}
if token_type_ids_chunk is not None:
_extra_vision_kwargs["token_type_ids"] = token_type_ids_chunk
if mm_token_type_ids_chunk is not None:
_extra_vision_kwargs["mm_token_type_ids"] = mm_token_type_ids_chunk
with autocaster:
if pixel_values is None:
new_hidden_states_chunk = unwrapped_model(
input_ids = input_ids_chunk,
attention_mask = attention_mask_chunk,
pixel_values = pixel_values_chunk,
image_grid_thw = image_grid_thw_chunk,
pixel_attention_mask = pixel_attention_mask_chunk,
image_sizes = image_sizes_chunk,
**_extra_vision_kwargs,
).logits
new_hidden_states_chunk = new_hidden_states_chunk[:, -(logits_to_keep + max_left_pad + 1): , :]
new_hidden_states_chunk = new_hidden_states_chunk[:, :-1, :]
logprobs_chunk = compute_logprobs_chunk(new_hidden_states_chunk, completion_ids, input_ids_chunk)
else:
new_hidden_states_chunk = unwrapped_model(
input_ids = input_ids_chunk,
attention_mask = attention_mask_chunk,
pixel_values = pixel_values_chunk,
image_grid_thw = image_grid_thw_chunk,
pixel_attention_mask = pixel_attention_mask_chunk,
image_sizes = image_sizes_chunk,
logits_to_keep = logits_to_keep + 1,
**_extra_vision_kwargs,
).logits
new_hidden_states_chunk = new_hidden_states_chunk[:, :-1, :]
logprobs_chunk = compute_logprobs_chunk(new_hidden_states_chunk, completion_ids, input_ids_chunk)
# Avoids race conditions with GPT OSS offload_embbed=True; no measurable slowdown.
device_synchronize()
all_logprobs_list.append(logprobs_chunk)
new_logprobs = torch.cat(all_logprobs_list, dim=0)
with autocaster:
loss, completion_length, mean_kl, delta, flat_is_ratio, coef_1 = UnslothEfficientGRPO.apply(
new_logprobs,
old_logps,
ref_logps,
sampling_per_token_logps,
lm_head,
completion_input_ids,
completion_mask,
advantages,
trainer.beta,
trainer.accelerator.scaler,
1,
kwargs
)
# Force logits (not hidden states) again or output is gibberish.
os.environ["UNSLOTH_RETURN_HIDDEN_STATES"] = "0"
return loss, completion_length, mean_kl, delta, flat_is_ratio, coef_1, completion_mask
# Old non-efficient code path (dead).
new_logits = torch.matmul(new_hidden_states, lm_head.t())
new_logits = new_logits[:, :-1, :] # exclude the last logit: it corresponds to the next token pred
old_logits = torch.matmul(old_hidden_states, lm_head.t())
old_logits = old_logits[:, :-1, :] # exclude the last logit: it corresponds to the next token pred
loss, completion_length, mean_kl = grpo_compute_loss(
old_logits,
new_logits,
completion_input_ids,
completion_mask,
trainer.beta,
advantages,
)
return loss, completion_length, mean_kl
pass
@torch.compile(dynamic = True, fullgraph = True, options = torch_compile_options)
def grpo_compute_loss_slow(
ref,
new,
old,
sampling_per_token_logps,
input_ids,
mask,
beta,
advantages,
**kwargs
):
# All Unsloth Zoo code licensed under AGPL3
# Optional argument defaults.
loss_type = kwargs.get("loss_type", "grpo")
epsilon_low = kwargs.get("epsilon_low", 0.2)
epsilon_high = kwargs.get("epsilon_high", 0.2)
max_completion_length = kwargs.get("max_completion_length", 8192)
delta = kwargs.get("delta", None)
importance_sampling_level = kwargs.get("importance_sampling_level", "token")
num_items_in_batch = kwargs.get("num_items_in_batch", None)
current_gradient_accumulation_steps = kwargs.get("current_gradient_accumulation_steps", 1)
num_processes = kwargs.get("num_processes", 1)
use_vllm = kwargs.get("use_vllm", False)
vllm_importance_sampling_cap = kwargs.get("vllm_importance_sampling_cap", 2.0)
get_sapo_token_loss = kwargs.get("get_sapo_token_loss", None)
sapo_temperature_pos = kwargs.get("sapo_temperature_pos", 1.0)
sapo_temperature_neg = kwargs.get("sapo_temperature_neg", 1.05)
get_gamma_weights = kwargs.get("get_gamma_weights", None)
vespo_k_pos = kwargs.get("vespo_k_pos", 2.0)
vespo_lambda_pos = kwargs.get("vespo_lambda_pos", 3.0)
vespo_k_neg = kwargs.get("vespo_k_neg", 3.0)
vespo_lambda_neg = kwargs.get("vespo_lambda_neg", 2.0)
get_off_policy_mask = kwargs.get("get_off_policy_mask", None)
off_policy_mask_threshold = kwargs.get("off_policy_mask_threshold", None)
input_ids = input_ids.unsqueeze(-1)
if advantages.dim() == 1:
advantages = advantages.unsqueeze(1)
if off_policy_mask_threshold is not None:
off_policy_mask = get_off_policy_mask(
advantages=advantages,
per_token_logps=new,
old_per_token_logps=old,
mask=mask,
off_policy_threshold=off_policy_mask_threshold,
)
with torch.no_grad():
if use_vllm and sampling_per_token_logps is not None:
# Filter out extra leading prompt tokens after left-padding input_ids.
importance_sampling_ratio = torch.exp((old * mask) - sampling_per_token_logps)
importance_sampling_ratio = torch.clamp(
importance_sampling_ratio, max=vllm_importance_sampling_cap
)
pass
# Must detach when old is None: exp(new - new.detach()) == 1 but keeps grads correct.
if old is not None:
log_ratio = new - old
else:
log_ratio = new - new.detach()
if importance_sampling_level == "token":
log_importance_weights = log_ratio
elif importance_sampling_level == "sequence":
log_importance_weights = (log_ratio * mask).sum(-1) / mask.sum(-1).clamp(min=1.0)
log_importance_weights = log_importance_weights.unsqueeze(-1)
else:
raise ValueError(
f"Unknown importance sampling level: {importance_sampling_level}. Possible values are 'token' "
"and 'sequence'."
)
coef_1 = torch.exp(log_importance_weights)
# Reverse KL: low-variance low-bias estimator as used in the GRPO paper.
if beta != 0.0:
kl_i = torch.exp(ref - new) - (ref - new) - 1.0
else:
# Zeros with the correct shape.
if importance_sampling_level == "sequence":
kl_i = new.new_zeros(new.size(0), 1)
else:
kl_i = torch.zeros_like(new)
if loss_type == "cispo":
clamped_ratios = torch.clamp(coef_1, max=epsilon_high).detach()
loss_i = -clamped_ratios * advantages * new
elif loss_type in ["grpo", "bnpo", "dr_grpo", "dapo"]:
coef_2 = torch.clamp(coef_1, 1 - epsilon_low, 1 + epsilon_high)
if delta is not None:
loss_1 = torch.clamp(coef_1, max=delta) * advantages
else:
loss_1 = coef_1 * advantages
pass
loss_2 = coef_2 * advantages
loss_i = -torch.min(loss_1, loss_2)
elif loss_type == "sapo":
if get_sapo_token_loss is None:
raise Exception(f"sapo is only available in TRL 0.26.0+")
loss_i = torch.empty_like(coef_1)
positive_advantages_mask = advantages.repeat([1, coef_1.shape[1]]) > 0
# With n_chunks some tensors may be empty; guard the indexing.
if coef_1[positive_advantages_mask].numel() != 0:
loss_i[positive_advantages_mask] = get_sapo_token_loss(
coef_1[positive_advantages_mask], sapo_temperature_pos
)
if coef_1[~positive_advantages_mask].numel() != 0:
loss_i[~positive_advantages_mask] = get_sapo_token_loss(
coef_1[~positive_advantages_mask], sapo_temperature_neg
)
loss_i = -loss_i * advantages
elif loss_type == "vespo":
if get_gamma_weights is None:
raise Exception("vespo is only available in TRL 0.26.0+")
phi_seq = get_gamma_weights(
advantages=advantages,
log_ratio_per_token=log_ratio,
mask=mask,
importance_sampling_ratio=kwargs.get("importance_sampling_ratio"),
k_pos=vespo_k_pos,
lambda_pos=vespo_lambda_pos,
k_neg=vespo_k_neg,
lambda_neg=vespo_lambda_neg,
)
loss_i = -phi_seq * advantages * new
else:
raise ValueError(f"Unknown loss type: {loss_type}")
if off_policy_mask_threshold is not None:
loss_i = loss_i * off_policy_mask
if use_vllm and sampling_per_token_logps is not None:
loss_i = loss_i * importance_sampling_ratio
# delta for the metric.
with torch.no_grad():
delta = torch.abs(old - sampling_per_token_logps)
delta = delta * mask
flat_is_ratio = importance_sampling_ratio * mask
else:
delta = torch.tensor([]).detach()
flat_is_ratio = torch.tensor([]).detach()
if beta != 0.0:
loss_i = loss_i + beta * kl_i
mask = mask.to(torch.float32)
n_mask_per_reward = mask.sum(1)
# https://github.com/huggingface/trl/blob/e8b8499f1f8d76838155b515e414ee98f757d6d5/trl/trainer/grpo_trainer.py#L1624
if loss_type in ["grpo", "sapo"]:
loss = ((loss_i * mask).sum(-1) / mask.sum(-1).clamp(min=1.0)).mean()
loss = loss / current_gradient_accumulation_steps
elif loss_type == "bnpo":
loss = (loss_i * mask).sum() / mask.sum().clamp(min=1.0)
loss = loss / current_gradient_accumulation_steps
elif loss_type == "dr_grpo":
loss = (loss_i * mask).sum() / (loss_i.size(0) * max_completion_length)
loss = loss / current_gradient_accumulation_steps
elif loss_type in ["cispo", "dapo", "vespo"]:
normalizer = num_items_in_batch/ num_processes
loss = (loss_i * mask).sum() / normalizer
else:
raise ValueError(f"Unknown loss type: {loss_type}")
# Folded metrics.
def masked_batch_mean(x):
with torch.inference_mode():
completion_length = n_mask_per_reward.mean()
if x.shape[1] == 1: # when importance_sampling_level == "sequence"
return completion_length, x.mean()
else:
mean_kl_per_reward = (x * mask).sum(1) / n_mask_per_reward
mean_kl = mean_kl_per_reward.mean()
return completion_length, mean_kl
completion_length, mean_kl = masked_batch_mean(kl_i)
return loss, completion_length, mean_kl, delta, flat_is_ratio, coef_1, mask
def grpo_update_SamplingParams(SamplingParams, generation_kwargs, vllm_sampling_params = None):
good_sampling_params_keys = inspect.signature(SamplingParams).parameters.keys()
new_generation_kwargs = {}
for key in generation_kwargs.keys():
if key in good_sampling_params_keys:
new_generation_kwargs[key] = generation_kwargs[key]
generation_kwargs = new_generation_kwargs
if vllm_sampling_params is not None:
for key in good_sampling_params_keys:
if hasattr(vllm_sampling_params, key):
overwrited_key = getattr(vllm_sampling_params, key)
if overwrited_key is not None and (type(overwrited_key) in (list, tuple,) and len(overwrited_key) != 0):
generation_kwargs[key] = overwrited_key
return generation_kwargs
def _get_inference_mode_context_manager(model: torch.nn.Module):
"""
If the state dict was quantized using torchao, we will run into
the following error when calling ops like aten.t() in inference mode.
This is a bug in PyTorch that affects all tensor subclasses.
Cannot set version_counter for inference tensor
For now, we work around this issue by using `torch.no_grad()` in this case.
See https://github.com/pytorch/pytorch/issues/164872 for more details.
Otherwise, just return `torch.inference_mode()`.
"""
torchao_config = getattr(model, "torchao_config", None)
if torchao_config is not None and torchao_config.qat_scheme is None:
return torch.no_grad()
else:
return torch.inference_mode()
@dataclass
class UnslothGRPOConfig(GRPOConfig):
"""
Configuration class for the [`GRPOTrainer`].
This class includes only the parameters that are specific to GRPO training. For a full list of training arguments,
please refer to the [`~transformers.TrainingArguments`] documentation. Note that default values in this class may
differ from those in [`~transformers.TrainingArguments`].
Using [`~transformers.HfArgumentParser`] we can turn this class into
[argparse](https://docs.python.org/3/library/argparse#module-argparse) arguments that can be specified on the
command line.
Parameters:
> Parameters that control the model and reference model
model_init_kwargs (`str`, `dict[str, Any]`, *optional*):
Keyword arguments for [`~transformers.AutoModelForCausalLM.from_pretrained`], used when the `model`
argument of the [`GRPOTrainer`] is provided as a string.
trust_remote_code (`bool`, *optional*, defaults to `False`):
Whether to allow loading models and tokenizers that ship custom Python code from the Hub. Forwarded to
[`~transformers.AutoModelForCausalLM.from_pretrained`] and
[`~transformers.AutoProcessor.from_pretrained`]. Also applied to reward-model and reward-tokenizer loads.
router_aux_loss_coef (`float`, *optional*, defaults to `0.001`):
Coefficient of the load-balancing auxiliary loss. Only has an effect when training a Mixture-of-Experts
(MoE) model; for other models it does nothing. The auxiliary loss is added to the training loss with this
weight. Set to `0.0` to disable it.
disable_dropout (`bool`, *optional*, defaults to `False`):
Whether to disable dropout in the model. This is useful for training with a reference model, as it prevents
the model from generating different logprobs for the same input.
cast_lm_head_to_fp32 (`bool`, *optional*, defaults to `False`):
Whether to cast the language modeling head of the policy and reference models to float32. As recommended by
the [ScaleRL](https://huggingface.co/papers/2510.13786) recipe. This flag is only supported when the model
has untied word embedding and language modeling head layers i.e. `tie_word_embeddings` in the model config
is False.
> Parameters that control the data preprocessing
remove_unused_columns (`bool`, *optional*, defaults to `False`):
Whether to only keep the column `"prompt"` in the dataset. If you use a custom reward function that
requires any column other than `"prompts"` and `"completions"`, you should keep this to `False`.
num_generations (`int`, *optional*, defaults to `8`):
Number of generations per prompt to sample. The effective batch size (num_processes * per_device_batch_size
* gradient_accumulation_steps) must be evenly divisible by this value.
num_generations_eval (`int` or `None`, *optional*):
Number of generations to sample during evaluation. This allows using fewer generations during evaluation to
save computation. If `None`, uses the value of `num_generations`.
max_completion_length (`int` or `None`, *optional*, defaults to `256`):
Maximum length of the generated completion.
ds3_gather_for_generation (`bool`, *optional*, defaults to `True`):
This setting applies to DeepSpeed ZeRO-3. If enabled, the policy model weights are gathered for generation,
improving generation speed. However, disabling this option allows training models that exceed the VRAM
capacity of a single GPU, albeit at the cost of slower generation. Disabling this option is not compatible
with vLLM generation.
shuffle_dataset (`bool`, *optional*, defaults to `True`):
Whether to shuffle the training dataset.
pad_to_multiple_of (`int`, *optional*):
If set, the prompts ids and completions ids will be padded to a multiple of this value.
> Parameters that control generation
generation_batch_size (`int`, *optional*):
Batch size to use for generation. If `None`, it defaults to the effective training batch size:
`per_device_train_batch_size * num_processes * steps_per_generation`. In other words, there is one
generation batch processed per optimization step. Mutually exclusive with `steps_per_generation`.
steps_per_generation (`int`, *optional*):
Number of steps per generation. If `None`, it defaults to `gradient_accumulation_steps`. Mutually exclusive
with `generation_batch_size`.
temperature (`float`, defaults to `1.0`):
Temperature for sampling. The higher the temperature, the more random the completions.
top_p (`float`, *optional*, defaults to `1.0`):
Float that controls the cumulative probability of the top tokens to consider. Must be in (0, 1]. Set to
`1.0` to consider all tokens.
top_k (`int`, *optional*, defaults to `0`):
Number of highest probability vocabulary tokens to keep for top-k-filtering. If `0`, top-k-filtering is
disabled and all tokens are considered.
min_p (`float`, *optional*):
Minimum token probability, which will be scaled by the probability of the most likely token. It must be a
value between `0.0` and `1.0`. Typical values are in the `0.01-0.2` range.
generation_kwargs (`dict[str, Any]`, *optional*):
Additional keyword arguments to pass to [`~transformers.GenerationConfig`] (if using transformers) or
`SamplingParams` (if using vLLM) when sampling completions. This can be used to further customize the
generation behavior, such as setting `suppress_tokens`, `num_beams`, etc. If it contains keys that conflict
with the other generation parameters (like `min_p`, `top_p`, etc.), they will override them.
chat_template_kwargs (`dict[str, Any]`, *optional*):
Additional keyword arguments to pass to the `apply_chat_template` function when generating completions.
repetition_penalty (`float`, *optional*, defaults to `1.0`):
Float that penalizes new tokens based on whether they appear in the prompt and the generated text so far.
Values > `1.0` encourage the model to use new tokens, while values < `1.0` encourage the model to repeat
tokens.
cache_implementation (`str`, *optional*):
Implementation of the cache method for faster generation when `use_vllm` is set to `False`.
> Parameters that control generation acceleration powered by vLLM
use_vllm (`bool`, *optional*, defaults to `False`):
Whether to use vLLM for generating completions. If set to `True`, the trainer will use vLLM for generation
instead of the default model.generate(). Requires `vllm` to be installed.
vllm_mode (`str`, *optional*, defaults to `"colocate"`):
Mode to use for vLLM integration when `use_vllm` is set to `True`. Must be one of `"server"` or
`"colocate"`.
- `"server"`: The trainer will send generation requests to a separate vLLM server. Make sure a TRL vLLM
server is running (start with `trl vllm-serve`).
- `"colocate"`: vLLM will run in the same process and share the training GPUs. This avoids the need for a
separate server but may cause resource contention with training.
vllm_model_impl (`str`, *optional*, defaults to `"vllm"`):
Model implementation to use for vLLM. Must be one of `"transformers"` or `"vllm"`. `"transformers"`: Use
the `transformers` backend for model implementation. `"vllm"`: Use the `vllm` library for model
implementation.
vllm_structured_outputs_regex (`str`, *optional*):
Regex for vLLM structured outputs. If `None` (default), structured outputs is disabled.
> Parameters that control the vLLM server (only used when `vllm_mode` is `"server"`)
vllm_server_base_url (`str`, *optional*):
Base URL for the vLLM server (e.g., `"http://localhost:8000"`). If provided, `vllm_server_host` and
`vllm_server_port` are ignored.
vllm_server_host (`str`, *optional*, defaults to `"0.0.0.0"`):
Host of the vLLM server to connect to. Ignored if `vllm_server_base_url` is provided.
vllm_server_port (`int`, *optional*, defaults to `8000`):
Port of the vLLM server to connect to. Ignored if `vllm_server_base_url` is provided.
vllm_server_timeout (`float`, *optional*, defaults to `240.0`):
Total timeout duration in seconds to wait for the vLLM server to be up. If the server is not up after the
timeout, a `ConnectionError` is raised.
vllm_group_port (`int`, *optional*, defaults to `51216`):
Port number for the weight update group. This is used to communicate with the vLLM server. Unless the port
is occupied, there is no need to change it.
> Parameters that control colocated vLLM execution (only used when `vllm_mode` is `"colocate"`)
vllm_gpu_memory_utilization (`float`, *optional*, defaults to `0.3`):
Control the GPU memory utilization for vLLM. This setting only applies when `vllm_mode` is set to
`"colocate"`. If you are using `vllm_mode="server"`, this parameter must be passed separately when
launching the vLLM server via the `--vllm_gpu_memory_utilization` flag.
vllm_max_model_length (`int`, *optional*):
Context window for vLLM. Set it to at least the maximum prompt length in the dataset plus
`max_completion_length`; if omitted, it is inferred from the model config.
vllm_tensor_parallel_size (`int`, *optional*, defaults to `1`):
Control the tensor parallel size for vLLM. This setting only applies when `vllm_mode` is set to
`"colocate"`. If you are using `vllm_mode="server"`, this parameter must be passed separately when
launching the vLLM server via the `--vllm_tensor_parallel_size` flag.
vllm_enable_sleep_mode (`bool`, *optional*, defaults to `False`):
Enable vLLM sleep mode to offload weights/cache during the optimizer step. Keeps GPU memory usage low, but
waking the engine adds host–device transfer latency.
> Parameters that control generation acceleration powered by transformers continuous batching
use_transformers_continuous_batching (`bool`, *optional*, defaults to `False`):
Whether to use transformers' continuous batching engine for generating completions. Requires
`transformers>=5.8.0`.
transformers_continuous_batching_config (`dict`, *optional*):
Keyword arguments for [`~transformers.generation.ContinuousBatchingConfig`].
> Parameters that control the training
beta (`float`, *optional*, defaults to `0.0`):
KL coefficient. If `0.0` (default), the reference model is not loaded, reducing memory usage and improving
training speed. [DeepSeek-R1 incentivizes reasoning in LLMs through reinforcement
learning](https://huggingface.co/papers/2501.12948) use a value of `0.001`.
num_iterations (`int`, *optional*, defaults to `1`):
Number of iterations per batch (denoted as μ in the algorithm).
epsilon (`float`, *optional*, defaults to `0.2`):
Epsilon value for clipping.
delta (`float`, *optional*):
Enables the upper clipping bound in two-sided GRPO loss when set to a float. If `None` (default), standard
GRPO clipping is used. Recommended to be greater than `1 + ε` when enabled. This method is introduced in
the [INTELLECT-2 tech report](https://huggingface.co/papers/2505.07291).
epsilon_high (`float`, *optional*):
Upper-bound epsilon value for clipping. If not specified, it defaults to the same value as the lower-bound
specified in argument `epsilon`. Paper [DAPO](https://huggingface.co/papers/2503.14476) recommends `0.28`.
When used with `loss_type='cispo'`, this corresponds to the ε_max param specified in the [ScaleRL
paper](https://huggingface.co/papers/2510.13786) and the recommended value is `5.0`.
sapo_temperature_neg (`float`, *optional*, defaults to `1.05`):
Temperature for tokens with non-positive advantage scores used in the `sapo` loss function. This parameter
is introduced in the [Soft Adaptive Policy Optimization paper](https://huggingface.co/papers/2511.20347).
sapo_temperature_pos (`float`, *optional*, defaults to `1.0`):
Temperature for tokens with positive advantage scores used in the `sapo` loss function. This parameter is
introduced in the [Soft Adaptive Policy Optimization paper](https://huggingface.co/papers/2511.20347).
vespo_k_pos (`float`, *optional*, defaults to `2.0`):
k parameter for positive advantages, it is the power exponent in the VESPO loss. Controls how aggressively
we down-weight samples with low importance weights (when the importance sampling ratio < 1).
vespo_lambda_pos (`float`, *optional*, defaults to `3.0`):
lambda parameter for positive advantages, it is the decay factor in the VESPO loss. Controls how
aggressively we down-weight samples with high importance weights (when the importance sampling ratio > 1).
vespo_k_neg (`float`, *optional*, defaults to `3.0`):
k parameter for negative advantages, it is the power exponent in the VESPO loss. Controls how aggressively
we down-weight samples with low importance weights (when the importance sampling ratio < 1).
vespo_lambda_neg (`float`, *optional*, defaults to `2.0`):
lambda parameter for negative advantages, it is the exponential decay factor in the VESPO loss. Controls
how aggressively we down-weight samples with high importance weights (when the importance sampling ratio >
1).
importance_sampling_level (`str`, *optional*, defaults to `"token"`):
Controls whether importance sampling ratios are computed at the `"token"` or `"sequence"` level. `"token"`
keeps the raw per-token log-probability ratios (one weight per token). `"sequence"` averages the
log-probability ratios across valid tokens to produce a single ratio per sequence. The [GSPO
paper](https://huggingface.co/papers/2507.18071) shows that sequence-level sampling often yields more
stable training and better alignment with sequence-level rewards.
reward_weights (`list[float]`, *optional*):
Weights for each reward function. Must match the number of reward functions. If `None`, all rewards are
weighted equally with weight `1.0`.
multi_objective_aggregation (`str`, *optional*, defaults to `"sum_then_normalize"`):
Method to aggregate multiple reward functions. Supported values are:
- `"sum_then_normalize"` (default): First sums the weighted rewards from each reward function, then applies
reward scaling/normalization as specified by `scale_rewards` (see `scale_rewards` for details).
- `"normalize_then_sum"`: First normalizes/scales each reward function across generations (within each
group), then sums the normalized rewards using the specified weights. The aggregated reward is then
normalized at the batch level when forming advantages. This is the suggested approach from the paper
[GDPO: Group reward-Decoupled Normalization Policy Optimization for Multi-reward RL
Optimization](https://huggingface.co/papers/2601.05242).
scale_rewards (`str` or `bool`, *optional*, defaults to `"group"`):
Specifies the scaling strategy for rewards. Supported values are:
- `True` or `"group"` (default): rewards are scaled by the standard deviation within each group, ensuring
unit variance within a group.
- `"batch"`: rewards are scaled by the standard deviation across the entire batch, as recommended in the
[PPO Lite paper](https://huggingface.co/papers/2508.08221).
- `False` or `"none"`: no scaling is applied. The [Dr. GRPO
paper](https://huggingface.co/papers/2503.20783) recommends not scaling rewards, as scaling by the
standard deviation introduces a question-level difficulty bias.
loss_type (`str`, *optional*, defaults to `"dapo"`):
Specifies the loss formulation to use. Supported values are:
- `"grpo"`: Aggregates token-level losses by normalizing over sequence length. Not recommended due to
length bias—this approach tends to prefer shorter completions with positive advantages and longer ones
with negative advantages.
- `"dr_grpo"`: Aggregates token-level losses by normalizing with a global constant. This method was
introduced in the [Dr. GRPO paper](https://huggingface.co/papers/2503.20783) to eliminate length bias.
The value of the constant corresponds to `max_completion_length`.
- `"dapo"` (default): Aggregates token-level losses by normalizing with the number of active token in the
global accumulated batch. This method was introduced in the [DAPO
paper](https://huggingface.co/papers/2503.14476) to eliminate length bias.
- `"bnpo"`: Aggregates token-level losses by normalizing with the number of active token in the local
batch. Note that normalization is performed over the local batch only, so results may slightly vary
depending on the local batch size, despite a constant effective batch size. When using
`per_device_train_batch_size==1`, the loss is equivalent to the GRPO loss.
- `"cispo"`: Clips the importance sampling weights instead of the advantage scaled importance weights. The
clipped weights are then multiplied with the advantages and policy model's log probs. Individual token
losses are aggregated by normalizing with the number of active tokens in the global accumulated batch.
This method was introduced in the [MiniMax-M1 paper](https://huggingface.co/papers/2506.13585).
- `"sapo"`: Soft Adaptive Policy Optimization loss, as introduced in the [Soft Adaptive Policy Optimization
paper](https://huggingface.co/papers/2511.20347). Replaces hard clipping with a smooth,
temperature-controlled gate that adaptively attenuates off-policy updates while preserving useful
learning signals.
- `"luspo"`: Length-Unbiased Sequence Policy Optimization loss. A sequence-level loss that scales each
sequence's loss by its length. This is a modification of GSPO and requires
`importance_sampling_level="sequence"`. Introduced in the [LUSPO
paper](https://huggingface.co/papers/2602.05261).
- `"vespo"`: Variational Sequence-Level Soft Policy Optimization. Replaces hard clipping with a smooth,
asymmetric Gamma weighting function applied directly to sequence-level importance weights. Introduced in
the [VESPO paper](https://huggingface.co/papers/2602.10693).
mask_truncated_completions (`bool`, *optional*, defaults to `False`):
When enabled, truncated completions are excluded from the loss calculation, preventing them from being
incorrectly penalized and introducing noise during training. According to the
[DAPO](https://huggingface.co/papers/2503.14476) paper, this is a good practice for training stability.
sync_ref_model (`bool`, *optional*, defaults to `False`):
Whether to synchronize the reference model with the active model every `ref_model_sync_steps` steps, using
the `ref_model_mixup_alpha` parameter. This synchronization originates from the
[TR-DPO](https://huggingface.co/papers/2404.09656) paper.
ref_model_mixup_alpha (`float`, *optional*, defaults to `0.6`):
α parameter from the [TR-DPO](https://huggingface.co/papers/2404.09656) paper, which controls the mix
between the current policy and the previous reference policy during updates. The reference policy is
updated according to the equation: `π_ref = α * π_θ + (1 - α) * π_ref_prev`. To use this parameter, you
must set `sync_ref_model=True`.
ref_model_sync_steps (`int`, *optional*, defaults to `512`):
τ parameter from the [TR-DPO](https://huggingface.co/papers/2404.09656) paper, which determines how
frequently the current policy is synchronized with the reference policy. To use this parameter, you must
set `sync_ref_model=True`.
top_entropy_quantile (`float`, *optional*, defaults to `1.0`):
ρ parameter from [Beyond the 80/20 Rule](https://huggingface.co/papers/2506.01939). Keeps in the policy
loss term only the top-ρ quantile of tokens by entropy of the probability distribution at each sequence
position, improving results. Range: `[0.0-1.0]`. A value of `0.0` masks all but the highest entropy token;
`1.0` keeps all tokens. The paper recommends a value of `0.2`. If used with
`mask_truncated_completions=True`, only tokens from non-truncated completions are considered.
max_tool_calling_iterations (`int`, *optional*):
Maximum number of tool-calling turns when training an agent. If `None`, there is no limit and generation
stops when the model generates a response turn with no tool calls or when the total response length reaches
`max_model_length`.
vllm_importance_sampling_correction (`bool`, *optional*, defaults to `True`):
Whether to apply Importance Sampling (IS) to correct for the mismatch between vLLM completion logprobs and
recomputed training logprobs. If set to `False`, no IS is applied regardless of
`vllm_importance_sampling_mode`. When `True`, the selected mode determines how the IS ratios are computed
and constrained.
vllm_importance_sampling_mode (`str`, *optional*, defaults to `"sequence_mask"`):
Specifies how Importance Sampling is performed when `vllm_importance_sampling_correction=True`. Possible
values are:
- `"token_truncate"`: Token-level truncated IS (default). Per-token ratios are clipped to
[C_min, C_max].
- `"token_mask"`: Token-level masked IS. Per-token ratios outside [C_min, C_max] are set to zero.
- `"sequence_truncate"`: Sequence-level truncated IS. A single sequence ratio is clipped to
[C_min, C_max] and applied to all tokens in the sequence.
- `"sequence_mask"`: Sequence-level masked IS. Sequences with ratios outside [C_min, C_max] are masked
out.
vllm_importance_sampling_clip_max (`float`, *optional*, defaults to `3.0`):
Importance sampling upper bound C_max used by `vllm_importance_sampling_mode`. For `*_truncate` modes,
importance ratios are clipped from above at C_max. For `*_mask` modes, ratios larger than C_max are set to
zero.
vllm_importance_sampling_clip_min (`float`, *optional*):
Importance sampling lower bound C_min used by `vllm_importance_sampling_mode`. For `*_truncate` modes,
ratios are clipped from below at C_min. For `*_mask` modes, ratios below C_min are set to zero. To strictly
mask ratios below C_min without upper bound, set `vllm_importance_sampling_clip_max=None`.
off_policy_mask_threshold (`float`, *optional*):
Threshold for off-policy sequence masking. If `None`, off-policy sequence masking is disabled. When set,
sequences with negative advantages and high KL divergence are masked out to stabilize training. This
parameter corresponds to the `delta` threshold in Equation 9 of the [DeepSeek-V3.2
paper](https://huggingface.co/papers/2512.02556). It expects a positive value (e.g., 0.5).
use_bias_correction_kl (`bool`, *optional*, defaults to `False`):
Whether to use the unbiased KL divergence estimator with importance sampling correction. This corrects the
KL divergence estimate by multiplying it with the importance sampling ratio. This is described in the
[DeepSeek-V3.2 paper](https://huggingface.co/papers/2512.02556).
> Parameters that control the logging
log_completions (`bool`, *optional*, defaults to `False`):
Whether to log a sample of (prompt, completion) pairs every `logging_steps` steps. If `rich` is installed,
it prints the sample. If `wandb` and/or `trackio` logging is enabled, it logs it to `wandb` and/or
`trackio`.
num_completions_to_print (`int`, *optional*):
Number of completions to print with `rich`. If `None`, all completions are logged.
log_unique_prompts (`bool`, *optional*, defaults to `False`):
Whether to log unique prompts. If `True`, only unique prompts are logged. If `False`, all prompts are
logged.
log_completions_hub_repo (`str`, *optional*):
Hugging Face Hub repository to save the completions. Should be a complete repository name like
`'username/reponame'` or `'orgname/reponame'`, or just `'reponame'` in which case the repository will be
created in the currently-logged-in Hugging Face user's namespace. Note that this repository will be public
unless you set `hub_private_repo=True` or your organization's default is to create private repositories."
> Deprecated parameters
use_transformers_paged:
<Deprecated version="1.2.0">
Parameter `use_transformers_paged` is deprecated and will be removed in version v2.0.0. Use
`use_transformers_continuous_batching` instead.
</Deprecated>
vllm_importance_sampling_cap:
<Deprecated version="1.6.0">
Parameter `vllm_importance_sampling_cap` is deprecated and will be removed in v2.0.0. Use
`vllm_importance_sampling_clip_max` instead.
</Deprecated>
> [!NOTE]
> These parameters have default values different from [`~transformers.TrainingArguments`]:
> - `logging_steps`: Defaults to `10` instead of `500`.
> - `gradient_checkpointing`: Defaults to `True` instead of `False`.
> - `bf16`: Defaults to `True` if `fp16` is not set, instead of `False`.
> - `learning_rate`: Defaults to `1e-6` instead of `5e-5`.
"""
vllm_sampling_params: Optional[Any] = field(
default = None,
metadata = {'help': 'vLLM SamplingParams'},
)
unsloth_num_chunks : Optional[int] = field(
default = -1,
metadata = {'help': 'Chunk size to reduce memory usage. -1 is most efficient.'},
)
unsloth_logit_chunk_multiplier : Optional[int] = field(
default = None,
metadata = {'help': 'Multiplier for chunked logit computations.'},
)
unsloth_grpo_mini_batch : Optional[int] = field(
default = None,
metadata = {'help': 'Mini batch size for GRPO hidden state accumulation. Default is None unless user defines it.'},
)
def __init__(
self,
output_dir = None,
per_device_train_batch_size = 4,
num_train_epochs = 3.0,
max_steps = -1,
learning_rate = 5e-05,
lr_scheduler_type = 'linear',
lr_scheduler_kwargs = None,
warmup_steps = 0.1,
optim = 'adamw_8bit',
optim_args = None,
weight_decay = 0.001,
adam_beta1 = 0.9,
adam_beta2 = 0.999,
adam_epsilon = 1e-08,
optim_target_modules = None,
gradient_accumulation_steps = 2,
average_tokens_across_devices = True,
max_grad_norm = 1.0,
label_smoothing_factor = 0.0,
bf16 = False,
fp16 = False,
bf16_full_eval = False,
fp16_full_eval = False,
tf32 = None,
gradient_checkpointing = True,
gradient_checkpointing_kwargs = None,
torch_compile = False,
torch_compile_backend = None,
torch_compile_mode = None,
use_liger_kernel = False,
liger_kernel_config = None,
use_cache = False,
neftune_noise_alpha = None,
torch_empty_cache_steps = 250,
auto_find_batch_size = False,
logging_strategy = 'steps',
logging_steps = 1,
logging_first_step = False,
log_on_each_node = True,
logging_nan_inf_filter = False,
include_num_input_tokens_seen = False,
log_level = 'passive',
log_level_replica = 'warning',
disable_tqdm = None,
report_to = 'none',
run_name = None,
project = 'huggingface',
trackio_space_id = 'trackio',
eval_strategy = 'no',
eval_steps = None,
eval_delay = 0,
per_device_eval_batch_size = 4,
prediction_loss_only = False,
eval_on_start = False,
eval_do_concat_batches = True,
eval_use_gather_object = False,
eval_accumulation_steps = 2,
batch_eval_metrics = False,
save_only_model = False,
save_strategy = 'steps',
save_steps = 500,
save_on_each_node = False,
save_total_limit = None,
enable_jit_checkpoint = False,
push_to_hub = False,
hub_token = None,
hub_private_repo = None,
hub_model_id = None,
hub_strategy = 'every_save',
hub_always_push = False,
hub_revision = None,
load_best_model_at_end = False,
metric_for_best_model = None,
greater_is_better = None,
ignore_data_skip = False,
restore_callback_states_from_checkpoint = False,
full_determinism = False,
seed = 3407,
data_seed = 3407,
use_cpu = False,
accelerator_config = None,
parallelism_config = None,
dataloader_drop_last = False,
dataloader_num_workers = 0,
dataloader_pin_memory = True,
dataloader_persistent_workers = False,
dataloader_prefetch_factor = None,
remove_unused_columns = False,
label_names = None,
train_sampling_strategy = 'random',
length_column_name = 'length',
ddp_find_unused_parameters = None,
ddp_bucket_cap_mb = None,
ddp_broadcast_buffers = None,
ddp_backend = None,
ddp_timeout = 1800,
fsdp = None,
fsdp_config = None,
deepspeed = None,
debug = '',
skip_memory_metrics = True,
do_train = False,
do_eval = False,
do_predict = False,
resume_from_checkpoint = None,
warmup_ratio = None,
logging_dir = None,
local_rank = -1,
model_init_kwargs = None,
trust_remote_code = False,
router_aux_loss_coef = 0.001,
disable_dropout = False,
cast_lm_head_to_fp32 = False,
num_generations = 8,
num_generations_eval = None,
max_completion_length = 256,
ds3_gather_for_generation = True,
shuffle_dataset = True,
pad_to_multiple_of = None,
generation_batch_size = None,
steps_per_generation = None,
temperature = 1.0,
top_p = 1.0,
top_k = None,
min_p = None,
generation_kwargs = {},
chat_template_kwargs = None,
repetition_penalty = 1.0,
cache_implementation = None,
use_vllm = False,
vllm_mode = 'colocate',
vllm_model_impl = 'vllm',
vllm_enable_sleep_mode = False,
vllm_structured_outputs_regex = None,
vllm_server_base_url = None,
vllm_server_host = '0.0.0.0',
vllm_server_port = 8000,
vllm_server_timeout = 240.0,
vllm_group_port = 51216,
vllm_gpu_memory_utilization = 0.3,
vllm_max_model_length = None,
vllm_tensor_parallel_size = 1,
beta = 0.001,
num_iterations = 1,
epsilon = 0.2,
delta = None,
epsilon_high = None,
sapo_temperature_neg = 1.05,
sapo_temperature_pos = 1.0,
vespo_k_pos = 2.0,
vespo_lambda_pos = 3.0,
vespo_k_neg = 3.0,
vespo_lambda_neg = 2.0,
importance_sampling_level = 'token',
reward_weights = None,
multi_objective_aggregation = 'sum_then_normalize',
scale_rewards = 'group',
loss_type = 'bnpo',
mask_truncated_completions = False,
sync_ref_model = False,
ref_model_mixup_alpha = 0.6,
ref_model_sync_steps = 512,
top_entropy_quantile = 1.0,
max_tool_calling_iterations = None,
vllm_importance_sampling_correction = False,
vllm_importance_sampling_mode = 'sequence_mask',
vllm_importance_sampling_clip_max = 3.0,
vllm_importance_sampling_clip_min = None,
off_policy_mask_threshold = None,
use_bias_correction_kl = False,
log_completions = False,
num_completions_to_print = None,
log_unique_prompts = False,
log_completions_hub_repo = None,
use_transformers_continuous_batching = False,
transformers_continuous_batching_config = None,
use_transformers_paged = False,
vllm_importance_sampling_cap = None,
vllm_sampling_params = None,
unsloth_num_chunks = -1,
unsloth_logit_chunk_multiplier = None,
unsloth_grpo_mini_batch = None,
**kwargs,
):
if learning_rate < 1e-7: print(f'Unsloth: Your learning rate of `{learning_rate}` is too small and less than 1e-7! Consider increasing it, otherwise gradient updates will be close to 0!')
if learning_rate > 1: print(f'Unsloth: Your learning rate of `{learning_rate}` is way too larger > 1! Consider decreasing it to 1e-1, otherwise gradient updates will explode!')
if num_train_epochs is None:
num_train_epochs = 3.0 # Default to 3 epochs if None, max_steps will override
if output_dir is None and save_strategy == 'steps' and save_steps == 500:
output_dir = 'unsloth_training_checkpoints'
save_strategy = 'no'
if os.environ.get('UNSLOTH_ENABLE_FLEX_ATTENTION', '0') == '1':
from unsloth_zoo.flex_attention import HAS_FLEX_ATTENTION
if HAS_FLEX_ATTENTION and pad_to_multiple_of is None:
from unsloth_zoo.flex_attention import FLEX_ATTENTION_BLOCK_SIZE
pad_to_multiple_of = FLEX_ATTENTION_BLOCK_SIZE
if loss_type.lower() == 'dr_grpo':
loss_type = 'dr_grpo'
elif loss_type.lower() == 'dapo':
loss_type = 'dapo'
if loss_type.lower() == 'dr_grpo':
if scale_rewards == None:
scale_rewards = True
elif scale_rewards == True:
print('Unsloth: The Dr GRPO paper recommends setting `scale_rewards` to False! Will override. Set it to `None` to force False.')
scale_rewards = False
elif loss_type.lower() == 'dapo':
if mask_truncated_completions != True:
print('Unsloth: The DAPO paper recommends `mask_truncated_completions = True` - we will set it.')
if epsilon_high != 0.28:
print('Unsloth: The DAPO paper recommends `epsilon_high = 0.28` - we will set it.')
if beta != 0.0:
print(f'[WARNING] Unsloth: The DAPO paper recommends setting `beta = 0.0` to remove the KL term - You have set it to {beta}.')
mask_truncated_completions = True
epsilon_high = 0.28
if steps_per_generation is None and generation_batch_size is None:
ga = gradient_accumulation_steps
world_size = int(os.environ.get('WORLD_SIZE', '1'))
if (ga * world_size * per_device_train_batch_size) % num_generations != 0:
print('Unsloth: We now expect `per_device_train_batch_size` * `gradient_accumulation_steps` * `world_size` to be a multiple of `num_generations`.\nWe will change the batch size of ' + str(per_device_train_batch_size) + ' to the `num_generations` of ' + str(num_generations))
per_device_train_batch_size = num_generations
if temperature <= 0:
raise ValueError('Unsloth: Please set a positive non-zero temperature since your results will be wrong.')
elif temperature >= 10:
raise ValueError('Unsloth: Please set a positive non-zero temperature less than 10, since sampling will be quite erratic.')
if use_vllm and (top_k is None or top_k == 0): top_k = -1
super().__init__(
output_dir = output_dir,
per_device_train_batch_size = per_device_train_batch_size,
num_train_epochs = num_train_epochs,
max_steps = max_steps,
learning_rate = learning_rate,
lr_scheduler_type = lr_scheduler_type,
lr_scheduler_kwargs = lr_scheduler_kwargs,
warmup_steps = warmup_steps,
optim = optim,
optim_args = optim_args,
weight_decay = weight_decay,
adam_beta1 = adam_beta1,
adam_beta2 = adam_beta2,
adam_epsilon = adam_epsilon,
optim_target_modules = optim_target_modules,
gradient_accumulation_steps = gradient_accumulation_steps,
average_tokens_across_devices = average_tokens_across_devices,
max_grad_norm = max_grad_norm,
label_smoothing_factor = label_smoothing_factor,
bf16 = bf16,
fp16 = fp16,
bf16_full_eval = bf16_full_eval,
fp16_full_eval = fp16_full_eval,
tf32 = tf32,
gradient_checkpointing = gradient_checkpointing,
gradient_checkpointing_kwargs = gradient_checkpointing_kwargs,
torch_compile = torch_compile,
torch_compile_backend = torch_compile_backend,
torch_compile_mode = torch_compile_mode,
use_liger_kernel = use_liger_kernel,
liger_kernel_config = liger_kernel_config,
use_cache = use_cache,
neftune_noise_alpha = neftune_noise_alpha,
torch_empty_cache_steps = torch_empty_cache_steps,
auto_find_batch_size = auto_find_batch_size,
logging_strategy = logging_strategy,
logging_steps = logging_steps,
logging_first_step = logging_first_step,
log_on_each_node = log_on_each_node,
logging_nan_inf_filter = logging_nan_inf_filter,
include_num_input_tokens_seen = include_num_input_tokens_seen,
log_level = log_level,
log_level_replica = log_level_replica,
disable_tqdm = disable_tqdm,
report_to = report_to,
run_name = run_name,
project = project,
trackio_space_id = trackio_space_id,
eval_strategy = eval_strategy,
eval_steps = eval_steps,
eval_delay = eval_delay,
per_device_eval_batch_size = per_device_eval_batch_size,
prediction_loss_only = prediction_loss_only,
eval_on_start = eval_on_start,
eval_do_concat_batches = eval_do_concat_batches,
eval_use_gather_object = eval_use_gather_object,
eval_accumulation_steps = eval_accumulation_steps,
batch_eval_metrics = batch_eval_metrics,
save_only_model = save_only_model,
save_strategy = save_strategy,
save_steps = save_steps,
save_on_each_node = save_on_each_node,
save_total_limit = save_total_limit,
enable_jit_checkpoint = enable_jit_checkpoint,
push_to_hub = push_to_hub,
hub_token = hub_token,
hub_private_repo = hub_private_repo,
hub_model_id = hub_model_id,
hub_strategy = hub_strategy,
hub_always_push = hub_always_push,
hub_revision = hub_revision,
load_best_model_at_end = load_best_model_at_end,
metric_for_best_model = metric_for_best_model,
greater_is_better = greater_is_better,
ignore_data_skip = ignore_data_skip,
restore_callback_states_from_checkpoint = restore_callback_states_from_checkpoint,
full_determinism = full_determinism,
seed = seed,
data_seed = data_seed,
use_cpu = use_cpu,
accelerator_config = accelerator_config,
parallelism_config = parallelism_config,
dataloader_drop_last = dataloader_drop_last,
dataloader_num_workers = dataloader_num_workers,
dataloader_pin_memory = dataloader_pin_memory,
dataloader_persistent_workers = dataloader_persistent_workers,
dataloader_prefetch_factor = dataloader_prefetch_factor,
remove_unused_columns = remove_unused_columns,
label_names = label_names,
train_sampling_strategy = train_sampling_strategy,
length_column_name = length_column_name,
ddp_find_unused_parameters = ddp_find_unused_parameters,
ddp_bucket_cap_mb = ddp_bucket_cap_mb,
ddp_broadcast_buffers = ddp_broadcast_buffers,
ddp_backend = ddp_backend,
ddp_timeout = ddp_timeout,
fsdp = fsdp,
fsdp_config = fsdp_config,
deepspeed = deepspeed,
debug = debug,
skip_memory_metrics = skip_memory_metrics,
do_train = do_train,
do_eval = do_eval,
do_predict = do_predict,
resume_from_checkpoint = resume_from_checkpoint,
warmup_ratio = warmup_ratio,
logging_dir = logging_dir,
local_rank = local_rank,
model_init_kwargs = model_init_kwargs,
trust_remote_code = trust_remote_code,
router_aux_loss_coef = router_aux_loss_coef,
disable_dropout = disable_dropout,
cast_lm_head_to_fp32 = cast_lm_head_to_fp32,
num_generations = num_generations,
num_generations_eval = num_generations_eval,
max_completion_length = max_completion_length,
ds3_gather_for_generation = ds3_gather_for_generation,
shuffle_dataset = shuffle_dataset,
pad_to_multiple_of = pad_to_multiple_of,
generation_batch_size = generation_batch_size,
steps_per_generation = steps_per_generation,
temperature = temperature,
top_p = top_p,
top_k = top_k,
min_p = min_p,
generation_kwargs = generation_kwargs,
chat_template_kwargs = chat_template_kwargs,
repetition_penalty = repetition_penalty,
cache_implementation = cache_implementation,
use_vllm = use_vllm,
vllm_mode = vllm_mode,
vllm_model_impl = vllm_model_impl,
vllm_enable_sleep_mode = vllm_enable_sleep_mode,
vllm_structured_outputs_regex = vllm_structured_outputs_regex,
vllm_server_base_url = vllm_server_base_url,
vllm_server_host = vllm_server_host,
vllm_server_port = vllm_server_port,
vllm_server_timeout = vllm_server_timeout,
vllm_group_port = vllm_group_port,
vllm_gpu_memory_utilization = vllm_gpu_memory_utilization,
vllm_max_model_length = vllm_max_model_length,
vllm_tensor_parallel_size = vllm_tensor_parallel_size,
beta = beta,
num_iterations = num_iterations,
epsilon = epsilon,
delta = delta,
epsilon_high = epsilon_high,
sapo_temperature_neg = sapo_temperature_neg,
sapo_temperature_pos = sapo_temperature_pos,
vespo_k_pos = vespo_k_pos,
vespo_lambda_pos = vespo_lambda_pos,
vespo_k_neg = vespo_k_neg,
vespo_lambda_neg = vespo_lambda_neg,
importance_sampling_level = importance_sampling_level,
reward_weights = reward_weights,
multi_objective_aggregation = multi_objective_aggregation,
scale_rewards = scale_rewards,
loss_type = loss_type,
mask_truncated_completions = mask_truncated_completions,
sync_ref_model = sync_ref_model,
ref_model_mixup_alpha = ref_model_mixup_alpha,
ref_model_sync_steps = ref_model_sync_steps,
top_entropy_quantile = top_entropy_quantile,
max_tool_calling_iterations = max_tool_calling_iterations,
vllm_importance_sampling_correction = vllm_importance_sampling_correction,
vllm_importance_sampling_mode = vllm_importance_sampling_mode,
vllm_importance_sampling_clip_max = vllm_importance_sampling_clip_max,
vllm_importance_sampling_clip_min = vllm_importance_sampling_clip_min,
off_policy_mask_threshold = off_policy_mask_threshold,
use_bias_correction_kl = use_bias_correction_kl,
log_completions = log_completions,
num_completions_to_print = num_completions_to_print,
log_unique_prompts = log_unique_prompts,
log_completions_hub_repo = log_completions_hub_repo,
use_transformers_continuous_batching = use_transformers_continuous_batching,
transformers_continuous_batching_config = transformers_continuous_batching_config,
use_transformers_paged = use_transformers_paged,
vllm_importance_sampling_cap = vllm_importance_sampling_cap,**kwargs)
self.vllm_sampling_params = vllm_sampling_params
self.unsloth_num_chunks = unsloth_num_chunks
if unsloth_grpo_mini_batch is not None:
if self.generation_batch_size >= unsloth_grpo_mini_batch:
self.unsloth_grpo_mini_batch = unsloth_grpo_mini_batch
else:
raise ValueError(
f"Unsloth GRPO mini batch size needs to be less than or equal to the effective generation batch size, "
f"which is self.per_device_train_batch_size * gradient_accumulation_steps."
)
self.unsloth_logit_chunk_multiplier = unsloth_logit_chunk_multiplier
# Unsloth: Remove use_reentrant=False forced by TRL 0.27.0+
if getattr(self, 'gradient_checkpointing_kwargs', None) is not None:
if 'use_reentrant' in self.gradient_checkpointing_kwargs:
del self.gradient_checkpointing_kwargs['use_reentrant']
pass
class _UnslothGRPOTrainer(_BaseTrainer):
""""""
_tag_names = ["trl", "grpo"]
_name = "GRPO"
_paper = {
"title": "DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models",
"id": "2402.03300",
# docstyle-ignore
"citation": textwrap.dedent("""\
@article{shao2024deepseekmath,
title = {{DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models}},
author = {Zhihong Shao and Peiyi Wang and Qihao Zhu and Runxin Xu and Junxiao Song and Mingchuan Zhang and Y. K. Li and Y. Wu and Daya Guo},
year = 2024,
eprint = {arXiv:2402.03300},
}"""),
}
def __init__(
self,
model: "str | PreTrainedModel | PeftModel",
reward_funcs: RewardFunc | list[RewardFunc],
args: GRPOConfig | None = None,
train_dataset: Dataset | IterableDataset | None = None,
eval_dataset: Dataset | IterableDataset | dict[str, Dataset | IterableDataset] | None = None,
processing_class: PreTrainedTokenizerBase | ProcessorMixin | None = None,
reward_processing_classes: PreTrainedTokenizerBase | list[PreTrainedTokenizerBase] | None = None,
callbacks: list[TrainerCallback] | None = None,
optimizers: tuple[torch.optim.Optimizer | None, torch.optim.lr_scheduler.LambdaLR | None] = (None, None),
peft_config: "PeftConfig | None" = None,
tools: list[Callable] | None = None,
rollout_func: RolloutFunc | None = None,
environment_factory: EnvironmentFactory | None = None,
):
if hasattr(model, 'vllm_engine') and hasattr(args, 'use_vllm'):
if (getattr(args, 'use_vllm', False) == False):
args.use_vllm = True
args.vllm_mode='colocate'
_unsloth_esm = getattr(getattr(getattr(getattr(model.vllm_engine, 'llm_engine', None), 'vllm_config', None), 'model_config', None), 'enable_sleep_mode', None)
if (_unsloth_esm if _unsloth_esm is not None else os.environ.get('UNSLOTH_VLLM_STANDBY', '0') != '0'):
args.vllm_enable_sleep_mode=True
# Args
if args is None:
model_name = model if isinstance(model, str) else get_config_model_id(model.config)
model_name = model_name.split("/")[-1]
args = GRPOConfig(f"{model_name}-GRPO")
# Model
if isinstance(model, str):
model_init_kwargs = args.model_init_kwargs or {}
# Distributed training requires device_map=None ["auto" fails]
if args.distributed_state.distributed_type in ["MULTI_GPU", "DEEPSPEED"]:
model_init_kwargs["device_map"] = None
model_init_kwargs.setdefault("trust_remote_code", args.trust_remote_code)
model = create_model_from_path(model, **model_init_kwargs)
else:
if args.model_init_kwargs is not None:
logger.warning(
"You passed `model_init_kwargs` to the `GRPOConfig`, but your model is already instantiated. "
"The `model_init_kwargs` will be ignored."
)
# Non-quantized models do not have the `is_loaded_in_{8,4}bit` attributes, whereas quantized models do
_is_quantized_model = getattr(model, "is_loaded_in_4bit", False) or getattr(model, "is_loaded_in_8bit", False)
# Some models [SmolVLM/Idefics3] don't support `logits_to_keep` argument and error out if we pass it
# Inspect the forward method before we wrap the model with PEFT
self.model_kwarg_keys = (
inspect.signature(model.forward).parameters.keys()
if not hasattr(model, "get_base_model")
else inspect.signature(model.get_base_model().forward).parameters.keys()
)
# Processing class
if processing_class is None:
processing_class = AutoProcessor.from_pretrained(
get_config_model_id(model.config),
truncation_side="left",
padding_side="left",
trust_remote_code=args.trust_remote_code,
)
if args.use_transformers_continuous_batching and isinstance(processing_class, ProcessorMixin):
raise ValueError(
"`use_transformers_continuous_batching` does not support multimodal models. Use `use_vllm` instead."
)
# Handle pad token for processors or tokenizers
if isinstance(processing_class, ProcessorMixin):
self._tokenizer = processing_class.tokenizer
self._is_vlm = True
elif isinstance(processing_class, PreTrainedTokenizerBase):
self._tokenizer = processing_class
self._is_vlm = False
else:
raise TypeError("The `processing_class` must be either a `PreTrainedTokenizerBase` or a `ProcessorMixin`")
if self._tokenizer.pad_token is None:
self._tokenizer.pad_token = self._tokenizer.eos_token
# Resolve vision placeholder token IDs once. Used by the forward pass to rebuild mm_token_type_ids
# when tool responses inject images into the completion [see _generate forward_kwargs block].
self._image_pad_token_id = None
self._video_pad_token_id = None
if self._is_vlm:
for candidate in ("<|image_pad|>", "<|image|>"):
tid = self._tokenizer.convert_tokens_to_ids(candidate)
if tid != self._tokenizer.unk_token_id:
self._image_pad_token_id = tid
break
tid = self._tokenizer.convert_tokens_to_ids("<|video_pad|>")
if tid != self._tokenizer.unk_token_id:
self._video_pad_token_id = tid
# PEFT
if False:
if not is_peft_available():
raise ImportError(
"You passed `peft_config` but the `peft` library is not installed. "
"Install it with `pip install trl[peft]`."
)
if not isinstance(peft_config, PeftConfig):
raise TypeError(
f"`peft_config` must be a `peft.PeftConfig` instance (e.g. `peft.LoraConfig`), "
f"got {type(peft_config).__name__}."
)
if is_peft_model(model):
raise ValueError(
"You passed a `PeftModel` instance together with a `peft_config` to the trainer. Please first merge "
"and unload the existing adapter, save the resulting base model, and then pass that base model along "
"with the new `peft_config` to the trainer."
)
# Create PEFT model
# ZeRO-3 + PEFT for non-quantized models:
# - PEFT's default autocast_adapter_dtype=True upcasts LoRA adapter params to fp32 even when the base model is bf16.
# - ZeRO-3's _allgather_params_coalesced allocates output buffers using the dtype of the first persistent parameter,
# so mixed-dtype persistent_parameters [bf16 base + fp32 LoRA] cause a TypeError on the first optimizer step.
# - Passing autocast_adapter_dtype=False keeps adapter params in the base model dtype [bf16], fixing the mismatch.
# - This is safe: the fp32 upcast is a QLoRA-specific concern [low-bit quantized base models], not needed for
# non-quantized bf16 training.
# - See:
# - TRL issue: https://github.com/huggingface/trl/issues/6089
# - Upstream issue: https://github.com/deepspeedai/DeepSpeed/issues/8072
# - autocast_adapter_dtype was introduced in PEFT 0.12.0; before, no upcast existed: no need to pass the kwarg
get_peft_model_kwargs = {}
if (
args.deepspeed_plugin is not None
and args.deepspeed_plugin.zero_stage == 3
and not _is_quantized_model
and Version(peft.__version__) >= Version("0.12.0")
):
get_peft_model_kwargs["autocast_adapter_dtype"] = False
model = get_peft_model(model, peft_config, **get_peft_model_kwargs)
elif is_peft_model(model) and args.beta != 0.0:
# If the model is a PEFT model with a pretrained adapter, we need to create a "ref" adapter that is a copy
# of the "default" adapter, so that we can use it as the reference model during GRPO training. PEFT only
# supports one adapter per model when the LoRA config uses `target_parameters` [see peft#3340], so in that
# case we skip the "ref" adapter and compute the reference log probs with adapters disabled, i.e. with the
# base model.
default_config = model.peft_config["default"]
if isinstance(default_config, LoraConfig) and default_config.target_parameters:
logger.warning(
"PEFT can't add a frozen reference adapter alongside one that uses `target_parameters` "
"(peft#3340], so the reference log probs are computed from the base model [adapters disabled]. "
"If you wrapped the model only to apply LoRA, pass a `peft_config` to the trainer instead; if you "
"wrapped it deliberately (pretrained adapter or custom init), note that the base model matches "
"your adapter only when it's freshly zero-initialized. If it is, this warning is safe to ignore."
)
else:
model.add_adapter("ref", default_config)
for name, param in model.named_parameters():
if ".default." in name:
ref_name = name.replace(".default.", ".ref.")
ref_param = model.get_parameter(ref_name)
ref_param.data.copy_(param.data)
# When using gradient checkpointing with PEFT, we need to enable input gradients. transformers.Trainer normally
# handles this, but a bug currently prevents it; see https://github.com/huggingface/transformers/issues/42489
if is_peft_model(model) and args.gradient_checkpointing:
model.enable_input_require_grads()
# When using QLoRA, the PEFT adapter weights are converted to bf16 to follow the recommendations from the
# original paper [see https://huggingface.co/papers/2305.14314, paragraph 3]. Normally, this can be done by
# passing `autocast_adapter_dtype=False` to `get_peft_model`, but this option is not yet supported for
# quantized models. See: https://github.com/huggingface/peft/issues/2889
if _is_quantized_model:
for param in model.parameters():
if param.requires_grad:
param.data = param.data.to(torch.bfloat16)
# Reward functions
if not isinstance(reward_funcs, list):
reward_funcs = [reward_funcs]
self.reward_func_names = []
for i, reward_func in enumerate(reward_funcs):
if isinstance(reward_func, str):
model_init_kwargs = args.model_init_kwargs or {}
# Distributed training requires device_map=None ["auto" fails]
if args.distributed_state.distributed_type in ["MULTI_GPU", "DEEPSPEED"]:
model_init_kwargs["device_map"] = None
model_init_kwargs.setdefault("trust_remote_code", args.trust_remote_code)
reward_funcs[i] = AutoModelForSequenceClassification.from_pretrained(
reward_func, num_labels=1, **model_init_kwargs
)
if isinstance(reward_funcs[i], nn.Module): # Use Module over PretrainedModel for compat w/ compiled models
self.reward_func_names.append(get_config_model_id(reward_funcs[i].config).split("/")[-1])
else:
self.reward_func_names.append(reward_funcs[i].__name__)
self.reward_funcs = reward_funcs
# Reward weights
if args.reward_weights is not None:
if len(args.reward_weights) != len(reward_funcs):
raise ValueError(
f"Number of reward weights ({len(args.reward_weights)}) must match number of reward "
f"functions ({len(reward_funcs)})"
)
self.reward_weights = torch.tensor(args.reward_weights, dtype=torch.float32)
else:
self.reward_weights = torch.ones(len(reward_funcs), dtype=torch.float32)
# Reward processing class
if reward_processing_classes is None:
reward_processing_classes = [None] * len(reward_funcs)
elif not isinstance(reward_processing_classes, list):
reward_processing_classes = [reward_processing_classes]
if len(reward_processing_classes) != len(reward_funcs):
raise ValueError(
f"The number of reward processing classes ({len(reward_processing_classes)}) must match the number of "
f"reward functions ({len(reward_funcs)})."
)
for i, (reward_processing_class, reward_func) in enumerate(
zip(reward_processing_classes, reward_funcs, strict=True)
):
if isinstance(reward_func, PreTrainedModel):
if reward_processing_class is None:
reward_processing_class = AutoTokenizer.from_pretrained(
get_config_model_id(reward_func.config), trust_remote_code=args.trust_remote_code
)
if reward_processing_class.pad_token_id is None:
reward_processing_class.pad_token = reward_processing_class.eos_token
# The reward model computes the reward for the latest non-padded token in the input sequence.
# So it's important to set the pad token ID to the padding token ID of the processing class.
reward_func.config.pad_token_id = reward_processing_class.pad_token_id
reward_processing_classes[i] = reward_processing_class
self.reward_processing_classes = reward_processing_classes
# Rollout function
if rollout_func is not None and os.environ.get("TRL_EXPERIMENTAL_SILENCE", "0") != "1":
warnings.warn(
"You are using 'rollout_func', which is an experimental feature. This API may change or be removed at "
"any time without prior notice. Silence this warning by setting environment variable "
"TRL_EXPERIMENTAL_SILENCE=1.",
UserWarning,
stacklevel=2,
)
self.rollout_func = rollout_func
if environment_factory is not None and os.environ.get("TRL_EXPERIMENTAL_SILENCE", "0") != "1":
warnings.warn(
"You are using 'environment_factory', which is an experimental feature. This API may change or be "
"removed at any time without prior notice. Silence this warning by setting environment variable "
"TRL_EXPERIMENTAL_SILENCE=1.",
UserWarning,
stacklevel=2,
)
# Tools
if tools:
if not Version(transformers.__version__) >= Version("5.0.0"):
raise ImportError(
"Using tools with GRPOTrainer requires transformers version 5.0.0 or higher. Please upgrade "
"transformers with `pip install --upgrade transformers` to use this feature."
)
if environment_factory:
if not Version(transformers.__version__) >= Version("5.2.0"):
raise ImportError(
"Using `environment_factory` with GRPOTrainer requires transformers version 5.2.0 or higher. "
"Please install transformers from the main branch with `pip install "
"git+https://github.com/huggingface/transformers.git@main` to use this feature."
)
if tools or environment_factory:
if not is_jmespath_available():
raise ImportError(
"Using tools with GRPOTrainer requires the jmespath library for response parsing. Please install "
"it with `pip install jmespath` to use this feature."
)
if not supports_tool_calling(processing_class):
raise ValueError(
"The provided chat template does not support tool calling. The template must be able to render a "
"full tool-calling conversation (user -> assistant with tool_calls -> tool)."
)
# Create the environments and extract their methods to be used as tools. We create one environment per rollout
generation_batch_size = args.per_device_train_batch_size * args.steps_per_generation
if environment_factory is not None:
self.environments = [environment_factory() for _ in range(generation_batch_size)]
environment_methods = [[] for _ in range(generation_batch_size)]
for i, environment in enumerate(self.environments):
has_reset = False
for name, member in inspect.getmembers(environment, predicate=inspect.ismethod):
if name == "reset":
has_reset = True
elif not name.startswith("_"):
environment_methods[i].append(member)
if not has_reset:
raise ValueError(
"Each environment instance returned by `environment_factory` must define a callable `reset` "
)
else:
self.environments = None
tools = tools or []
self._sync_tool_dicts = [{} for _ in range(generation_batch_size)]
self._async_tool_dicts = [{} for _ in range(generation_batch_size)]
for i in range(generation_batch_size):
for tool in tools + (environment_methods[i] if self.environments is not None else []):
if inspect.iscoroutinefunction(tool):
self._async_tool_dicts[i][tool.__name__] = tool
else:
self._sync_tool_dicts[i][tool.__name__] = tool
self.tools = tools + (environment_methods[0] if self.environments is not None else [])
# Check for async functions to start an event loop on a daemon thread
self._has_async_funcs = any(inspect.iscoroutinefunction(func) for func in self.reward_funcs + self.tools)
if self._has_async_funcs:
self.async_loop_thread, self.async_loop, self.async_loop_ready_event = start_event_loop_in_daemon(
name="GRPOTrainer-AsyncLoop"
)
# wait until the event loop is running in the daemon thread
self.async_loop_ready_event.wait()
atexit.register(shutdown_event_loop_in_daemon, self.async_loop_thread, self.async_loop)
# At the time of initial implementation, most tokenizers do not have built-in support for response schemas.
# While waiting for broader adoption, we provide this utility function to manually set the response schema for
# known chat templates. `response_schema` lives on the [inner] tokenizer, since `parse_response` is a tokenizer
# method that reads `self.response_schema`.
if self.tools and getattr(self._tokenizer, "response_schema", None) is None:
processing_class = add_response_schema(processing_class)
# In multi-turn training, the chat template *must* be prefix-preserving. If the tokenizer's original template
# isn't, we replace it at initialization with a training-safe, prefix-preserving template.
if self.tools and not is_chat_template_prefix_preserving(processing_class):
self.chat_template = get_training_chat_template(processing_class)
else:
self.chat_template = None
# Training arguments
self.max_completion_length = args.max_completion_length # = |o_i| in the GRPO paper
self.num_generations = args.num_generations # = G in the GRPO paper
self.max_tool_calling_iterations = args.max_tool_calling_iterations or sys.maxsize
self.num_generations_eval = args.num_generations_eval or self.num_generations
self.chat_template_kwargs = args.chat_template_kwargs or {}
self.temperature = args.temperature
self.top_p = args.top_p
self.top_k = args.top_k
self.min_p = args.min_p
self.repetition_penalty = args.repetition_penalty
self.use_transformers_continuous_batching = args.use_transformers_continuous_batching
if self.use_transformers_continuous_batching:
if not Version(transformers.__version__) >= Version("5.8.0"):
raise ImportError(
"Using `use_transformers_continuous_batching` requires transformers>=5.8.0. "
"Please upgrade with `pip install --upgrade transformers`."
)
from transformers.generation import ContinuousBatchingConfig
cb_kwargs = dict(args.transformers_continuous_batching_config or {})
# The transformers default [0.9] leaves almost no VRAM for the training backward pass;
# use a training-aware default unless the user has set it explicitly.
cb_kwargs.setdefault("max_memory_percent", 0.5)
self.continuous_batching_config = ContinuousBatchingConfig(**cb_kwargs)
else:
self.continuous_batching_config = None
self.pad_to_multiple_of = args.pad_to_multiple_of
self.use_vllm = args.use_vllm
self.vllm_mode = args.vllm_mode
self.vllm_gpu_memory_utilization = args.vllm_gpu_memory_utilization # only applies to colocation mode
self.vllm_tensor_parallel_size = args.vllm_tensor_parallel_size # only applies to colocation mode
self.vllm_importance_sampling_correction = args.vllm_importance_sampling_correction
self.vllm_importance_sampling_mode = args.vllm_importance_sampling_mode
self.vllm_importance_sampling_clip_max = args.vllm_importance_sampling_clip_max
self.vllm_importance_sampling_clip_min = args.vllm_importance_sampling_clip_min
self.use_liger_kernel = args.use_liger_kernel
self.loss_type = args.loss_type
self.multi_objective_aggregation = args.multi_objective_aggregation
# MoE load-balancing auxiliary loss, applied to Mixture-of-Experts models [no effect otherwise]
text_config = model.config.get_text_config()
is_moe = getattr(text_config, "output_router_logits", None) is not None
self.aux_loss_enabled = is_moe and args.router_aux_loss_coef != 0.0
self.router_aux_loss_coef = args.router_aux_loss_coef
self.scale_rewards = args.scale_rewards
self.importance_sampling_level = args.importance_sampling_level
self.off_policy_mask_threshold = args.off_policy_mask_threshold
if self.use_liger_kernel and self.off_policy_mask_threshold is not None:
raise ValueError("Liger kernel does not support off-policy sequence masking yet.")
if self.use_liger_kernel and is_peft_model(model):
# The Liger fused GRPO loss multiplies the hidden states by `lm_head.weight` directly. When the LM head is
# targeted by a PEFT adapter [`"lm_head"` in `target_modules`], `lm_head.weight` is the frozen base weight
# and the trainable adapter parameters live in separate submodules that Liger never sees. The head adapter
# would silently receive no gradient, so the model trains as if `lm_head` were frozen. Fail loudly rather
# than train a silently-frozen head.
output_embeddings = model.get_output_embeddings()
if isinstance(output_embeddings, BaseTunerLayer):
raise ValueError(
"`use_liger_kernel=True` is incompatible with applying a PEFT adapter to `lm_head`. The Liger "
"fused GRPO loss reads `lm_head.weight` directly, so the adapter on the head is ignored and never "
"trained. Either remove `'lm_head'` from your `target_modules`, or set `use_liger_kernel=False`."
)
self.mask_truncated_completions = args.mask_truncated_completions
self.top_entropy_quantile = args.top_entropy_quantile
if self.use_liger_kernel and self.top_entropy_quantile < 1.0:
raise NotImplementedError(
"Liger Kernels don't currently support masking token positions based on entropy."
)
if self.use_liger_kernel and self.importance_sampling_level not in ("token", "sequence"):
raise ValueError(
f"Unknown importance sampling level: {self.importance_sampling_level}. "
"Possible values are 'token' and 'sequence'."
)
# Datasets
self.shuffle_dataset = args.shuffle_dataset
if train_dataset is None:
raise ValueError("`train_dataset` is required")
elif (
isinstance(train_dataset, IterableDataset)
or isinstance(eval_dataset, IterableDataset)
or (
isinstance(eval_dataset, dict) and any(isinstance(ds, IterableDataset) for ds in eval_dataset.values())
)
):
# See https://github.com/huggingface/trl/issues/3213
raise NotImplementedError(
"Iterable datasets are not yet supported in GRPOTrainer. Please use a standard dataset instead."
)
if args.loss_type == "luspo" and args.importance_sampling_level != "sequence":
logger.warning(
"When using `'luspo'` loss, `importance_sampling_level` should be set to `'sequence'` to mirror the "
"paper's setup."
)
if args.loss_type == "vespo" and args.importance_sampling_level != "token":
logger.warning(
"VESPO computes sequence-level importance weights internally. `importance_sampling_level` should be "
"set to `'token'` (the default)."
)
if args.importance_sampling_level == "sequence" and args.loss_type in ["bnpo", "dr_grpo", "dapo", "cispo"]:
logger.warning(
f"When using `importance_sampling_level='sequence'`, the `'{args.loss_type}'` loss sums per-token "
"contributions, which effectively weights each sequence by its completion length instead of "
"optimizing the per-sequence objective. To reproduce the GSPO paper's setup, set `loss_type='grpo'` "
"(see https://huggingface.co/docs/trl/main/en/paper_index#group-sequence-policy-optimization]."
)
if self.loss_type == "vespo" and self.use_vllm and self.vllm_importance_sampling_correction:
if self.vllm_importance_sampling_mode not in ["token_truncate", "token_mask"]:
raise ValueError(
f"VESPO loss requires `vllm_importance_sampling_mode` to be either 'token_truncate' or "
f"'token_mask'. Got: {self.vllm_importance_sampling_mode}."
)
# Multi-step
self.num_iterations = args.num_iterations # = 𝜇 in the GRPO paper
self.epsilon_low = args.epsilon
self.epsilon_high = args.epsilon_high if args.epsilon_high is not None else args.epsilon
# Tracks the number of iterations [forward + backward passes], including those within a grad accum cycle
self._step = 0
# Buffer the batch to reuse generated outputs across multiple updates. For more details, see
# `_get_train_sampler` and `_prepare_inputs`.
self._buffered_inputs = None
# Transformers explicitly set use_reentrant=True in the past to silence a PyTorch warning, but the default was
# never updated once PyTorch switched to recommending use_reentrant=False. Until that change lands upstream
# [see https://github.com/huggingface/transformers/pull/43203] and is released [most likely in 5.0.0], we
# default to the recommended non-reentrant behavior here, while preserving any user-provided value.
if args.gradient_checkpointing and Version(transformers.__version__) < Version("5.0.0"):
args.gradient_checkpointing_kwargs = args.gradient_checkpointing_kwargs or {}
args.gradient_checkpointing_kwargs.setdefault("use_reentrant", False)
super().__init__(
model=model,
args=args,
data_collator=identity, # No data collation is needed in GRPO
train_dataset=train_dataset,
eval_dataset=eval_dataset,
processing_class=processing_class,
callbacks=callbacks,
optimizers=optimizers,
# In Trainer, `training_step` scales the loss by `gradient_accumulation_steps` only if `compute_loss_func`
# is None. For DAPO, loss scaling instead depends on the total number of completions tokens across the
# global accumulated batch. To control scaling ourselves, we must disable Trainer’s built-in scaling. The
# simplest [though a bit hacky] way is to set `compute_loss_func` to any non-None value, which bypasses
# that behavior without rewriting `training_step`.
compute_loss_func="non-None value to disable scaling",
)
# Reference model
self.beta = args.beta
if self.beta == 0.0:
# If beta is 0.0, the reference model is not needed
self.ref_model = None
elif is_peft_model(model):
# If PEFT is used, the reference model is not needed since the adapter can be disabled
# to revert to the initial model.
self.ref_model = None
else:
# For deepspeed, fsdp or non-distributed models, create a reference model from scratch
model_init_kwargs = args.model_init_kwargs or {}
# Distributed training requires device_map=None ["auto" fails]
if self.args.distributed_state.distributed_type in ["MULTI_GPU", "DEEPSPEED"]:
model_init_kwargs["device_map"] = None
model_init_kwargs.setdefault("trust_remote_code", args.trust_remote_code)
self.ref_model = create_model_from_path(get_config_model_id(self.model.config), **model_init_kwargs)
# Disable dropout in the models
if args.disable_dropout:
disable_dropout_in_model(model)
if self.ref_model is not None:
disable_dropout_in_model(self.ref_model)
# Cast LM Head To FP32
if args.cast_lm_head_to_fp32:
def _cast_lm_head_to_fp32(target_model: PreTrainedModel):
"""Cast lm_head to fp32 while preserving embedding output dtype if tied."""
def cast_inputs_to_fp32(module, inputs):
# Preserve other positional args and kwargs untouched
if not inputs:
return inputs
return (inputs[0].to(torch.float32),) + inputs[1:]
original_dtype_local = target_model.lm_head.weight.dtype
target_model.lm_head = target_model.lm_head.float()
target_model.lm_head.register_forward_pre_hook(cast_inputs_to_fp32)
if target_model.config.tie_word_embeddings:
def cast_outputs_to_original_dtype(module, args, output):
return output.to(original_dtype_local)
# Only cast activations; weights are now fp32 [intentional for numerical stability of logits]
target_model.model.embed_tokens.register_forward_hook(cast_outputs_to_original_dtype)
_cast_lm_head_to_fp32(model)
if self.ref_model is not None:
_cast_lm_head_to_fp32(self.ref_model)
# Liger loss
if self.use_liger_kernel:
if not is_liger_kernel_available():
raise ImportError(
"Liger is required to use `use_liger_kernel` as the GRPO loss. Run `pip install liger-kernel`."
)
# redirect the model.module forward to the model forward to ensure pre-forward hooks are called
self._forward_redirection = _ForwardRedirection()
self.liger_grpo_loss = LigerFusedLinearGRPOLoss(
beta=self.beta,
epsilon_low=self.epsilon_low,
epsilon_high=self.epsilon_high,
temperature=self.temperature,
use_ref_model=self.beta != 0.0,
loss_type=self.loss_type,
max_completion_length=self.max_completion_length,
importance_sampling_level=self.importance_sampling_level,
delta=args.delta,
use_bias_correction_kl=args.use_bias_correction_kl,
sapo_temperature_pos=args.sapo_temperature_pos,
sapo_temperature_neg=args.sapo_temperature_neg,
vespo_k_pos=args.vespo_k_pos,
vespo_lambda_pos=args.vespo_lambda_pos,
vespo_k_neg=args.vespo_k_neg,
vespo_lambda_neg=args.vespo_lambda_neg,
)
# Initialize the metrics
self._metrics = {"train": defaultdict(list), "eval": defaultdict(list)}
self._total_train_tokens = 0
self._current_train_step_time = 0.0
self.log_completions = args.log_completions
self.log_unique_prompts = args.log_unique_prompts
self.num_completions_to_print = args.num_completions_to_print
# Keep logs sized to the generation batch to record only outputs from the latest model update.
self._logs = {
"images": deque(maxlen=args.generation_batch_size),
"prompt": deque(maxlen=args.generation_batch_size),
"completion": deque(maxlen=args.generation_batch_size),
"rewards": defaultdict(lambda: deque(maxlen=args.generation_batch_size)),
"advantages": deque(maxlen=args.generation_batch_size),
"extra": defaultdict(lambda: deque(maxlen=args.generation_batch_size)),
}
# Buffers for user-logged data from reward functions, flushed after gathering
self._pending_extra_logs = defaultdict(list)
self._pending_metrics = defaultdict(list)
# Ensure each process receives a unique seed to prevent duplicate completions when generating with
# transformers if num_generations exceeds per_device_train_batch_size. We could skip it if we use vLLM, but
# it's safer to set it in all cases.
set_seed(args.seed, device_specific=True)
if self.use_vllm:
self.vllm_generation = VLLMGeneration(
model=self.model,
accelerator=self.accelerator,
processing_class=self.processing_class,
mode=args.vllm_mode,
structured_outputs_regex=args.vllm_structured_outputs_regex,
server_base_url=args.vllm_server_base_url,
server_host=args.vllm_server_host,
server_port=args.vllm_server_port,
group_port=args.vllm_group_port,
server_timeout=args.vllm_server_timeout,
tensor_parallel_size=args.vllm_tensor_parallel_size,
gpu_memory_utilization=args.vllm_gpu_memory_utilization,
max_model_length=args.vllm_max_model_length,
max_num_seqs=args.per_device_train_batch_size
* args.vllm_tensor_parallel_size
* args.steps_per_generation,
enable_sleep_mode=args.vllm_enable_sleep_mode,
model_impl=args.vllm_model_impl,
repetition_penalty=self.repetition_penalty,
temperature=self.temperature,
top_p=self.top_p,
top_k=self.top_k,
min_p=self.min_p,
max_completion_length=self.max_completion_length,
logprobs=0,
generation_kwargs=args.generation_kwargs,
)
self._last_loaded_step = -1
else:
generation_kwargs = {
"max_new_tokens": self.max_completion_length,
"do_sample": True,
"pad_token_id": self._tokenizer.pad_token_id,
"bos_token_id": self._tokenizer.bos_token_id,
"eos_token_id": self._tokenizer.eos_token_id,
"temperature": self.temperature,
"top_p": self.top_p,
"top_k": self.top_k,
"min_p": self.min_p,
"repetition_penalty": self.repetition_penalty,
"cache_implementation": args.cache_implementation,
}
if args.generation_kwargs is not None:
generation_kwargs.update(args.generation_kwargs)
self.generation_config = GenerationConfig(**generation_kwargs, disable_compile=True)
# Keep training-specific generation kwargs to overwrite model's original generation config
self.generation_kwargs = generation_kwargs
# Gradient accumulation requires scaled loss. Normally, loss scaling in the parent class depends on whether the
# model accepts loss-related kwargs. Since we compute our own loss, this check is irrelevant. We set
# self.model_accepts_loss_kwargs to False to enable scaling.
self.model_accepts_loss_kwargs = False
self._dist = DistributedBackend(self.accelerator)
# Add tags to the model
self.model.add_model_tags(self._tag_names)
if self.ref_model is not None:
if self.is_deepspeed_enabled:
self.ref_model = prepare_deepspeed(self.ref_model, self.accelerator)
elif self.is_fsdp_enabled:
self.ref_model = prepare_fsdp(self.ref_model, self.accelerator)
else:
self.ref_model = self.accelerator.prepare_model(self.ref_model, evaluation_mode=True)
if args.sync_ref_model:
if self.beta == 0.0:
raise ValueError(
"You passed `sync_ref_model=True` while `beta=0.0`, which means the reference model is not used "
"during training. Consequently, GRPOTrainer does not create a `ref_model` instance, and there is "
"nothing to synchronize. Please set `sync_ref_model=False`, or set `beta` to a non-zero value."
)
if is_peft_model(model):
raise NotImplementedError(
"You passed `sync_ref_model=True` while using a PEFT model, which is currently not supported. "
"With PEFT, GRPOTrainer does not keep a separate reference model in memory; instead, it recovers "
"reference behavior by temporarily disabling the adapter. As a result, there is no standalone "
"`ref_model` instance to synchronize. Use `sync_ref_model=False`, or opt for full fine-tuning if "
"you need a synced reference model. If you need `sync_ref_model` to work with PEFT, please open a "
"feature request at https://github.com/huggingface/trl/issues."
)
self.add_callback(SyncRefModelCallback(ref_model=self.ref_model, accelerator=self.accelerator))
for i, reward_func in enumerate(self.reward_funcs):
if isinstance(reward_func, PreTrainedModel):
if self.is_deepspeed_enabled:
self.reward_funcs[i] = prepare_deepspeed(reward_func, self.accelerator)
else:
# set device placement to True to make `prepare_model` move `reward_func` to device when using fsdp
self.reward_funcs[i] = self.accelerator.prepare_model(
reward_func, evaluation_mode=True, device_placement=True
)
if self.accelerator.is_main_process and self.log_completions:
os.makedirs(os.path.join(self.args.output_dir, "completions"), exist_ok=True)
if self.args.log_completions_hub_repo is not None:
repo_id = self.args.log_completions_hub_repo
create_repo(repo_id, private=self.args.hub_private_repo, repo_type="dataset", exist_ok=True)
template_path = pkg_resources.files("trl").joinpath("templates/completions_dataset_card.md")
card_data = DatasetCardData(
pretty_name="TRL Completion logs",
tags=["trl", "trl-logs", "completions"],
)
card = DatasetCard.from_template(
card_data=card_data,
template_path=str(template_path),
repo_id=repo_id,
hub_model_id=self.args.hub_model_id,
)
card.push_to_hub(repo_id)
self.commit_scheduler = CommitScheduler(
repo_id=repo_id,
repo_type="dataset",
folder_path=f"{self.args.output_dir}/completions",
every=2, # minutes
allow_patterns=["*.parquet"],
)
def _set_signature_columns_if_needed(self):
# If `self.args.remove_unused_columns` is True, non-signature columns are removed.
# By default, this method sets `self._signature_columns` to the model's expected inputs (usually, "input_ids"
# and "attention_mask"). In GRPOTrainer, we preprocess data, so using the model's signature columns doesn't
# work. Instead, we set them to the columns expected by the `training_step` method, hence the override.
if self._signature_columns is None:
self._signature_columns = ["prompt", "image", "images"]
# This method overrides `Trainer.get_train_dataloader` to support our custom batching strategy.
# Instead of returning a standard per-step batch (i.e., `per_device_batch_size), our dataloader loads an
# *generation* batch (i.e., `per_device_batch_size × steps_per_generation`). This allows us to generate completions
# once every steps_per_generation step—rather than once per accumulation step—which is significantly more
# efficient. The only change from the original implementation is multiplying the batch size by
# `steps_per_generation`. Thus, `_prepare_inputs` is called with this *generation* batch, and it handles the
# splitting internally.
# Maintenance note: This method is a copy-paste of the original `Trainer.get_train_dataloader` with only one line
# modification.
def get_train_dataloader(self):
return self._get_dataloader(
dataset=self.train_dataset,
description="Training",
batch_size=self._train_batch_size * self.args.steps_per_generation, # < this is the change
sampler_fn=self._get_train_sampler,
is_training=True,
)
def _get_train_sampler(self, dataset: Dataset | None = None) -> Sampler:
# Returns a sampler that
# 1. ensures each prompt is repeated across multiple processes. This guarantees that identical prompts are
# distributed to different GPUs, allowing rewards to be computed and normalized correctly within each prompt
# group. Using the same seed across processes ensures consistent prompt assignment, preventing discrepancies
# in group formation.
# 2. repeats the batch multiple times to allow reusing generations across multiple updates. Refer to
# _prepare_inputs to see how the generations are stored and reused.
# In the following figure, the values are the prompt indices. Each row shows the per-step batch
# returned by `_prepare_inputs`; rows within a `steps_per_generation` block are slices of the same
# generated batch. When `num_iterations > 1`, that block is reused for multiple optimization passes
# before regenerating.
#
# | GPU 0 | GPU 1 |
#
# global_step step <-───> num_generations=2
# <-───────> per_device_train_batch_size=3
# grad_accum ▲ ▲ 0 0 0 0 1 1 2 2 <- Generate for the first `steps_per_generation` (prompts 0 to 11); store the completions; use the first slice to compute the loss
# =2 ▼ | 0 1 3 3 4 4 5 5 <- Take the stored generations and use the second slice to compute the loss
# |
# | 1 2 6 6 7 7 8 8 <- Take the stored generations and use the third slice to compute the loss
# steps_per_gen=4 ▼ 1 3 9 9 10 10 11 11 <- Take the stored generations and use the fourth slice to compute the loss
#
# 2 4 12 12 13 13 14 14 <- Generate for the second `steps_per_generation` (prompts 12 to 23); store the completions; use the first slice to compute the loss
# 2 5 15 15 16 16 17 17 <- Take the stored generations and use the second slice to compute the loss
# ...
if dataset is None:
dataset = self.train_dataset
return RepeatSampler(
data_source=dataset,
mini_repeat_count=self.num_generations,
batch_size=self.args.generation_batch_size // self.num_generations,
repeat_count=self.num_iterations * self.args.steps_per_generation,
shuffle=self.shuffle_dataset,
seed=self.args.seed,
)
def _get_eval_sampler(self, eval_dataset) -> Sampler:
# See _get_train_sampler for an explanation of the sampler.
return RepeatSampler(
data_source=eval_dataset,
mini_repeat_count=self.num_generations_eval,
seed=self.args.seed,
)
@profiling_decorator
def _get_last_hidden_state(
self,
unwrapped_model,
input_ids,
attention_mask,
logits_to_keep,
pixel_values=None,
image_grid_thw=None,
pixel_attention_mask=None,
spatial_shapes=None,
image_sizes=None,
image_position_ids=None,
):
if is_peft_model(unwrapped_model):
unwrapped_model = unwrapped_model.base_model.model
# Build model inputs - check if the model supports logits_to_keep (some models and VLMs don't)
model_inputs = {"input_ids": input_ids, "attention_mask": attention_mask}
# For Qwen models:
if image_grid_thw is not None and pixel_values is not None:
model_inputs["image_grid_thw"] = image_grid_thw
# For Gemma, SmolVLM2, LLaVa-Next etc.:
if pixel_values is not None:
model_inputs["pixel_values"] = pixel_values
# For SmolVLM2
if pixel_attention_mask is not None:
model_inputs["pixel_attention_mask"] = pixel_attention_mask
# For LFM2-VL
if spatial_shapes is not None:
model_inputs["spatial_shapes"] = spatial_shapes
# For LLaVa-Next
if image_sizes is not None:
model_inputs["image_sizes"] = image_sizes
if image_position_ids is not None:
model_inputs["image_position_ids"] = image_position_ids
# Only add logits_to_keep if the model supports it
if "logits_to_keep" in self.model_kwarg_keys:
# We add 1 to `logits_to_keep` because the last logits of the sequence is later excluded
model_inputs["logits_to_keep"] = logits_to_keep + 1
model_inputs["use_cache"] = False # only used in generation; set False to suppress warnings
# `base_model` gives the backbone model (skipping `lm_head`) — text decoder for LMs, multimodal wrapper for
# VLMs (so vision-token injection runs before the text decoder). `get_decoder()` won't do: on VLMs it
# returns just the text stack and feeds image-placeholder IDs through it.
# Pre-5.0 transformers VLMs set `base_model_prefix = ""` so `base_model is self` (re-runs `lm_head`).
# Fall back to `.model` there.
if self._is_vlm and Version(transformers.__version__) < Version("5.0.0"):
backbone = unwrapped_model.model
else:
backbone = unwrapped_model.base_model
last_hidden_state = backbone(**model_inputs).last_hidden_state
# Exclude the last value: it corresponds to the next token pred
last_hidden_state = last_hidden_state[:, :-1, :] # (B, L-1, H)
# Only keep the last logits_to_keep. For model that support logits_to_keep, this is a no-op.
last_hidden_state = last_hidden_state[:, -logits_to_keep:, :] # (B, logits_to_keep, H)
return last_hidden_state
def get_high_entropy_mask(self, entropies: torch.Tensor, mask: torch.Tensor, threshold: float) -> torch.Tensor:
"""
Returns a binary mask identifying tokens whose entropy exceeds a given quantile threshold.
Args:
entropies (`torch.Tensor`):
Tensor of shape (batch_size, seq_len) with per-token entropy values.
mask (`torch.Tensor`):
Binary mask of the same shape as `entropies`, where `1` indicates valid tokens and `0` padding.
threshold (`float`):
Quantile threshold between `0.0` and `1.0` to select high-entropy tokens.
Returns:
`torch.Tensor`:
Boolean mask of shape (batch_size, seq_len), where `True` indicates tokens with entropy >= threshold
and `False` otherwise.
"""
local = entropies[mask.bool()].float()
# Use a negative pad_value as a sentinel because entropy values are always >= 0.
# This guarantees that the sentinel cannot collide with any real entropy value.
pad_value = -1e9
# Pad across processes so that every rank has the same tensor length
padded = self.accelerator.pad_across_processes(local, dim=0, pad_index=pad_value)
gathered = self.accelerator.gather(padded)
# Drop sentinel values (safe because no entropy can be negative)
gathered = gathered[gathered != pad_value]
if gathered.numel() == 0:
return torch.zeros_like(entropies, dtype=torch.bool)
entropy_threshold = torch.quantile(gathered, threshold)
masked_entropies = entropies * mask.float()
entropy_mask = masked_entropies >= entropy_threshold
return entropy_mask & mask.bool() # ensure padding tokens are always masked out
def _get_per_token_logps_and_entropies(
self,
model,
input_ids,
attention_mask,
logits_to_keep,
batch_size = None,
compute_entropy = False,
compute_efficient = False,
*args,
**kwargs,
):
# All Unsloth code here in this function is licensed under AGPL3
# if True: # os.environ.get('UNSLOTH_USE_NEW_MODEL', '0') == '0':
# return None, None # logps, entropies Unsloth efficient GRPO
if compute_efficient:
return None, None
else:
if not hasattr(self, "_autocast_dtype"):
self._autocast_dtype = (
torch.float16
if os.environ.get("ACCELERATE_MIXED_PRECISION", "fp16") == "fp16"
else torch.bfloat16
)
if os.environ.get("UNSLOTH_FORCE_FLOAT32", "0") == "1":
self._autocast_dtype = torch.float16
pixel_values, image_grid_thw = (
kwargs.get("pixel_values", None),
kwargs.get("image_grid_thw", None),
)
pixel_attention_mask, image_sizes = (
kwargs.get("pixel_attention_mask", None),
kwargs.get("image_sizes", None),
)
num_images = kwargs.get("num_images", None)
# Transformers 5.x needs token_type_ids/mm_token_type_ids for some vision models
token_type_ids = kwargs.get("token_type_ids", None)
mm_token_type_ids = kwargs.get("mm_token_type_ids", None)
if mm_token_type_ids is not None or image_grid_thw is not None:
mm_token_type_ids = _unsloth_fix_mm_token_type_ids(
self.processing_class, input_ids, mm_token_type_ids
)
unwrapped_model = self.accelerator.unwrap_model(model, keep_fp32_wrapper = False)
lm_head = self.model.get_output_embeddings().weight
dtype_bytes = 16 if self._autocast_dtype in [torch.float16, torch.bfloat16] else 32
total_rows = input_ids.shape[0]
seq_len = input_ids.shape[1]
hidden_dim = lm_head.shape[1]
vocab_dim = lm_head.shape[0]
if self.args.unsloth_grpo_mini_batch is None:
B, multiplier = autotune_batch_and_chunks(
total_rows,
seq_len,
hidden_dim,
vocab_dim,
dtype_bytes,
self.args.unsloth_logit_chunk_multiplier,
)
B = total_rows // B
else:
B = self.args.unsloth_grpo_mini_batch
if self.args.unsloth_logit_chunk_multiplier is None:
multiplier = max(4, seq_len // 4096)
else:
multiplier = self.args.unsloth_logit_chunk_multiplier
all_logprobs_list = []
if pixel_values is None:
left_pad_tokens_per_prompt = calculate_pad_tokens_in_prompt(
input_ids, logits_to_keep, self.processing_class.pad_token_id
)
max_left_pad = torch.max(left_pad_tokens_per_prompt).item()
input_ids = left_pack_padding(input_ids, self.processing_class.pad_token_id)
attention_mask = input_ids != self.processing_class.pad_token_id
attention_mask = attention_mask.to(attention_mask.dtype)
else:
max_left_pad = 0
def slice_sample_axis(value, start, end):
if value is None:
return None
return value[start:end]
import math
total_samples = input_ids.shape[0]
batch_size = math.ceil(total_samples / B)
if isinstance(num_images, torch.Tensor):
num_images = num_images.detach().cpu().reshape(-1).tolist()
if image_grid_thw is not None and pixel_values is not None and num_images is not None:
rows_per_image = image_grid_thw.prod(dim = -1)
rows_per_sample = torch.split(rows_per_image, num_images)
rows_per_sample = torch.stack([s.sum() for s in rows_per_sample])
# why: cum_rows is indexed via .item() inside the per-chunk loop;
# keeping it on CPU avoids per-iteration GPU->CPU sync.
cum_rows = torch.cat(
[
torch.tensor([0], device = rows_per_sample.device),
rows_per_sample.cumsum(0),
]
).cpu()
cum_imgs = torch.tensor([0] + num_images).cumsum(0)
else:
cum_rows = None
cum_imgs = None
def _first_dim_len(value):
if value is None:
return None
if hasattr(value, "shape"):
return value.shape[0]
try:
return len(value)
except TypeError:
return None
total_images = sum(num_images) if num_images is not None else None
_image_sizes_n = _first_dim_len(image_sizes)
input_ids_chunks = []
attention_mask_chunks = []
pixel_values_chunks = []
image_grid_thw_chunks = []
pixel_attention_mask_chunks = []
image_sizes_chunks = []
token_type_ids_chunks = []
mm_token_type_ids_chunks = []
current_pixel_idx = 0
# TRL 0.23.0 batching logic
for start in range(0, total_samples, batch_size):
end = min(start + batch_size, total_samples)
input_ids_chunks.append(input_ids[start:end])
attention_mask_chunks.append(attention_mask[start:end])
token_type_ids_chunks.append(slice_sample_axis(token_type_ids, start, end))
mm_token_type_ids_chunks.append(slice_sample_axis(mm_token_type_ids, start, end))
if image_grid_thw is not None and pixel_values is not None:
if num_images is None:
grid_slice = image_grid_thw[start:end]
batch_pixel_count = grid_slice.prod(dim = -1).sum().item()
start_pixel_idx = current_pixel_idx
end_pixel_idx = current_pixel_idx + batch_pixel_count
current_pixel_idx = end_pixel_idx
img_start = img_end = None
else:
start_pixel_idx = cum_rows[start].item()
end_pixel_idx = cum_rows[end].item()
img_start = cum_imgs[start].item()
img_end = cum_imgs[end].item()
grid_slice = image_grid_thw[img_start:img_end]
image_grid_thw_chunks.append(grid_slice)
pixel_values_chunks.append(pixel_values[start_pixel_idx:end_pixel_idx])
if image_sizes is None:
image_sizes_chunks.append(None)
elif (
num_images is not None
and _image_sizes_n == total_images
and img_start is not None
):
image_sizes_chunks.append(image_sizes[img_start:img_end])
else:
image_sizes_chunks.append(slice_sample_axis(image_sizes, start, end))
if pixel_attention_mask is None:
pixel_attention_mask_chunks.append(None)
elif (
num_images is not None
and img_start is not None
and pixel_attention_mask.shape[0] == image_grid_thw.shape[0]
):
pixel_attention_mask_chunks.append(pixel_attention_mask[img_start:img_end])
elif (
pixel_attention_mask.shape[0] == pixel_values.shape[0]
and pixel_attention_mask.shape[0] != input_ids.shape[0]
):
pixel_attention_mask_chunks.append(
pixel_attention_mask[start_pixel_idx:end_pixel_idx]
)
else:
pixel_attention_mask_chunks.append(pixel_attention_mask[start:end])
else:
pixel_values_chunks.append(None)
image_grid_thw_chunks.append(None)
pixel_attention_mask_chunks.append(None)
image_sizes_chunks.append(slice_sample_axis(image_sizes, start, end))
temperature = self.temperature
logit_softcapping = _unsloth_get_final_logit_softcapping(model.config)
logit_scale_multiply = getattr(model.config, "logit_scale", 0)
if logit_scale_multiply is None:
logit_scale_multiply = 0
logit_scale_divide = getattr(model.config, "logits_scaling", 0)
if logit_scale_divide is None:
logit_scale_divide = 0
zipped_inputs = zip(
input_ids_chunks,
attention_mask_chunks,
pixel_values_chunks,
image_grid_thw_chunks,
pixel_attention_mask_chunks,
image_sizes_chunks,
token_type_ids_chunks,
mm_token_type_ids_chunks,
)
os.environ["UNSLOTH_RETURN_HIDDEN_STATES"] = "1"
with _get_inference_mode_context_manager(model):
for (
input_ids_chunk,
attention_mask_chunk,
pixel_values_chunk,
image_grid_thw_chunk,
pixel_attention_mask_chunk,
image_sizes_chunk,
token_type_ids_chunk,
mm_token_type_ids_chunk,
) in zipped_inputs:
_extra_vision_kwargs = {}
if token_type_ids_chunk is not None:
_extra_vision_kwargs["token_type_ids"] = token_type_ids_chunk
if mm_token_type_ids_chunk is not None:
_extra_vision_kwargs["mm_token_type_ids"] = mm_token_type_ids_chunk
with torch.amp.autocast(device_type = "cuda", dtype = self._autocast_dtype):
if pixel_values is None:
logits_chunk = unwrapped_model(
input_ids = input_ids_chunk,
attention_mask = attention_mask_chunk,
pixel_values = pixel_values_chunk,
image_grid_thw = image_grid_thw_chunk,
pixel_attention_mask = pixel_attention_mask_chunk,
image_sizes = image_sizes_chunk,
**_extra_vision_kwargs,
).logits
completion_input_ids_chunk = input_ids_chunk[
:, -(logits_to_keep + max_left_pad) :
]
logits_chunk = logits_chunk[
:, -(logits_to_keep + max_left_pad + 1) :, :
]
logits_chunk = logits_chunk[:, :-1, :]
logprobs_chunk = chunked_hidden_states_selective_log_softmax(
logits_chunk,
lm_head,
completion_input_ids_chunk,
chunks = input_ids_chunk.shape[0] * multiplier,
logit_scale_multiply = logit_scale_multiply,
logit_scale_divide = logit_scale_divide,
logit_softcapping = logit_softcapping,
temperature = temperature,
)
else:
# Essentially, for VLMs we do not go via the optimized path in models/,
# so we don't encounter the Flash Attn left-padding issue.
logits_chunk = unwrapped_model(
input_ids = input_ids_chunk,
attention_mask = attention_mask_chunk,
pixel_values = pixel_values_chunk,
image_grid_thw = image_grid_thw_chunk,
pixel_attention_mask = pixel_attention_mask_chunk,
image_sizes = image_sizes_chunk,
logits_to_keep = logits_to_keep + 1,
**_extra_vision_kwargs,
).logits
logits_chunk = logits_chunk[:, :-1, :]
completion_input_ids_chunk = input_ids_chunk[:, -logits_to_keep:]
# Guard: check if model returned hidden states or logits
if logits_chunk.shape[-1] == lm_head.shape[1]:
logprobs_chunk = chunked_hidden_states_selective_log_softmax(
logits_chunk,
lm_head,
completion_input_ids_chunk,
chunks = input_ids_chunk.shape[0] * multiplier,
logit_scale_multiply = logit_scale_multiply,
logit_scale_divide = logit_scale_divide,
logit_softcapping = logit_softcapping,
temperature = temperature,
)
else:
# Model returned logits directly - scaling/softcapping already applied by model forward
logprobs_chunk = chunked_selective_log_softmax(
logits_chunk,
completion_input_ids_chunk,
temperature,
)
# This is needed to avoid race conditions with GPT OSS offload_embbed=True
# However, it seems that this line does not slow down or disrupt models.
device_synchronize()
all_logprobs_list.append(logprobs_chunk)
logprobs = torch.cat(all_logprobs_list, dim = 0)
entropies = None
os.environ["UNSLOTH_RETURN_HIDDEN_STATES"] = "0"
return logprobs.detach(), entropies # logps, entropies
# input_ids = input_ids[:, -logits_to_keep:]
# For transformers<=4.48, logits_to_keep argument isn't supported, so here we drop logits ourselves.
# See https://github.com/huggingface/trl/issues/2770
# logits = logits[:, -logits_to_keep:]
# return logits
# See https://huggingface.co/blog/the_n_implementation_details_of_rlhf_with_ppo#policy-training-implementation-details
# logits = logits / self.temperature
# logps = selective_log_softmax(logits, input_ids)
# row_indices, col_indices = torch.where(logps < -20)
# # Method 1: Check if tensors have elements
# if len(row_indices) > 0 and len(col_indices) > 0:
# breakpoint() # Breakpoint triggered here
# print("Found high values!")
# return logps # compute logprobs for the input tokens
def training_step(self, model, inputs, num_items_in_batch):
time_before = time.perf_counter()
output = super().training_step(model, inputs, num_items_in_batch)
self._step += 1
time_after = time.perf_counter()
self._current_train_step_time += time_after - time_before
if self._step % self.current_gradient_accumulation_steps == 0:
self._metrics["train"]["step_time"].append(self._current_train_step_time)
self._current_train_step_time = 0.0
return output
@profiling_decorator
def _prepare_inputs(self, generation_batch: dict[str, torch.Tensor | Any]) -> dict[str, torch.Tensor | Any]:
# Prepares inputs for model training/evaluation by managing completion generation and batch handling.
# During training:
# - Receives the local generation batch (Per-GPU batch size × steps per generation)
# from the modified training dataloader instead of the standard local batch
# - Generates completions once for the entire generation batch and splits it into batches of size
# `per_device_train_batch_size`
# - Buffers these completions and returns the appropriate slice for the current accumulation step
# - Optimizes by regenerating completions only periodically (every steps_per_generation * num_iterations)
# During evaluation:
# - The input is treated as a standard local batch (no accumulation, no multiple iterations)
# - Completions are generated for each batch without buffering or reuse
# Returns a single local batch in both cases.
mode = "train" if self.model.training else "eval"
if mode == "train":
generate_every = self.args.steps_per_generation * self.num_iterations
if self._step % generate_every == 0 or self._buffered_inputs is None:
# self._buffered_inputs=None can occur when resuming from a checkpoint
generation_batch = self._generate_and_score_completions(generation_batch)
generation_batch = split_pixel_values_by_grid(generation_batch)
try: generation_batch = shuffle_sequence_dict(generation_batch)
except: pass
generation_batches = split_tensor_dict(generation_batch, self.args.steps_per_generation)
self._buffered_inputs = [unsplit_pixel_values_by_grid(batch) for batch in generation_batches]
inputs = self._buffered_inputs[self._step % self.args.steps_per_generation]
else:
# In evaluation, there is neither batch grouping for generation, nor multiple iterations, hence
# local generation batch == local eval batch
inputs = self._generate_and_score_completions(generation_batch)
return inputs
def _log_completion_extra(self, column: str, values: list):
"""
Log extra columns to the completions table. Called from reward functions via the `log_extra` kwarg.
Args:
column (`str`):
Name of the column to add.
values (`list`):
Values for the column, one per sample in the batch.
"""
self._pending_extra_logs[column].extend(values)
def _log_metric(self, name: str, value: float):
"""
Log a scalar metric from a reward function. Called via the `log_metric` kwarg. Values are averaged over each
logging step and reported alongside built-in metrics like `kl` and `entropy`.
Args:
name (`str`):
Name of the metric.
value (`float`):
Scalar value for this batch.
"""
self._pending_metrics[name].append(value)
@profiling_decorator
def _calculate_rewards(self, inputs, prompts, completions, completion_ids_list):
device = self.accelerator.device
rewards_per_func = torch.zeros(len(prompts), len(self.reward_funcs), device=device)
# Repeat all input columns (but "prompt", "completion", and "completion_ids") to match the num of generations
keys = [key for key in inputs[0] if key not in ["prompt", "completion", "completion_ids"]]
reward_kwargs = {key: [example[key] for example in inputs] for key in keys}
# This allows for dynamic reward shaping based on training progress.
reward_kwargs["trainer_state"] = self.state
# Allow reward functions to log extra columns to the completions table.
reward_kwargs["log_extra"] = self._log_completion_extra
# Allow reward functions to log additional scalar metrics.
reward_kwargs["log_metric"] = self._log_metric
async_funcs_info = [] # async custom functions for asyncio.gather
for i, (reward_func, reward_processing_class, reward_func_name) in enumerate(
zip(self.reward_funcs, self.reward_processing_classes, self.reward_func_names, strict=True)
):
if isinstance(reward_func, nn.Module): # Module (no PretrainedModel) for compat with compiled models
with profiling_context(self, reward_func_name):
if is_conversational(inputs[0]):
messages = [{"messages": p + c} for p, c in zip(prompts, completions, strict=True)]
texts = [
apply_chat_template(x, reward_processing_class, **self.chat_template_kwargs)["text"]
for x in messages
]
else:
texts = [p + c for p, c in zip(prompts, completions, strict=True)]
reward_inputs = reward_processing_class(
text=texts, return_tensors="pt", padding=True, padding_side="right", add_special_tokens=False
)
reward_inputs = super()._prepare_inputs(reward_inputs)
with torch.inference_mode():
rewards_per_func[:, i] = reward_func(**reward_inputs).logits[:, 0] # Shape (B*G,)
elif inspect.iscoroutinefunction(reward_func): # Separate async reward funcs to run them in parallel later
async_funcs_info.append((i, reward_func, reward_func_name))
else:
# Run synchronous reward function
with profiling_context(self, reward_func_name):
if self.environments is not None:
reward_kwargs["environments"] = self.environments
output_reward_func = reward_func(
prompts=prompts, completions=completions, completion_ids=completion_ids_list, **reward_kwargs
)
# Convert None values to NaN
output_reward_func = [reward if reward is not None else torch.nan for reward in output_reward_func]
rewards_per_func[:, i] = torch.tensor(output_reward_func, dtype=torch.float32, device=device)
# Execute async custom functions in parallel using asyncio.gather
if async_funcs_info:
async def _invoke_async(index, func, func_name):
with profiling_context(self, func_name):
output = await func(
prompts=prompts, completions=completions, completion_ids=completion_ids_list, **reward_kwargs
)
output = [r if r is not None else torch.nan for r in output]
return index, output
async def _run_async_funcs():
coros = [_invoke_async(i, func, func_name) for (i, func, func_name) in async_funcs_info]
return await asyncio.gather(*coros)
async_results = asyncio.run_coroutine_threadsafe(_run_async_funcs(), self.async_loop).result()
for idx, output_reward_func in async_results:
rewards_per_func[:, idx] = torch.tensor(output_reward_func, dtype=torch.float32, device=device)
# If all reward functions return None for a given row, issue a detailed warning
if torch.isnan(rewards_per_func).all(dim=1).any():
nan_row_idx = torch.isnan(rewards_per_func).all(dim=1).nonzero(as_tuple=True)[0][0]
row_reward_kwargs = {
key: value[nan_row_idx]
for key, value in reward_kwargs.items()
if key not in ("trainer_state", "log_extra", "log_metric")
}
row_reward_kwargs["prompt"] = prompts[nan_row_idx]
row_reward_kwargs["completion"] = completions[nan_row_idx]
logger.warning(
f"All reward functions returned None for the following kwargs:\n{row_reward_kwargs}\n"
"Please ensure that at least one reward function returns a valid reward."
)
# Gather the reward per function: this part is crucial, because the rewards are normalized per group and the
# completions may be distributed across processes
rewards_per_func = gather(rewards_per_func)
return rewards_per_func
def _tokenize_prompts(self, prompts: list):
"""Tokenize prompts and extract images/multimodal fields for generation."""
if is_conversational({"prompt": prompts[0]}):
# Normalize string content to content blocks for VLM processors that don't handle plain strings.
if self._is_vlm:
prompts = [prepare_multimodal_messages(prompt) for prompt in prompts]
# Extract images from messages for VLM support
images = []
has_images = False
for prompt in prompts:
prompt_images = []
for message in prompt:
if isinstance(message["content"], list):
for part in message["content"]:
if part["type"] == "image":
prompt_images.append(part["image"])
has_images = True
images.append(prompt_images if prompt_images else None)
images = images if has_images else None
# Workaround for a bug in transformers 5.3.0 where some processors (e.g. Qwen2.5-VL) crash on
# batched unpadded input (transformers#44514).
# Fixed in transformers 5.4.0 (transformers#44563).
needs_padding_workaround = Version("5.3.0") <= Version(transformers.__version__) < Version("5.4.0")
tokenized = self.processing_class.apply_chat_template(
conversation=prompts,
tools=self.tools or None, # `or None`: Llama bug: it renders tool boilerplate for tools=[]
chat_template=self.chat_template,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
**({"padding": True} if needs_padding_workaround else {}),
**self.chat_template_kwargs,
)
if needs_padding_workaround:
# Unpad input_ids: remove padding tokens using attention_mask to get per-sequence lists
prompt_ids = [
[tok for tok, m in zip(ids, mask, strict=True) if m]
for ids, mask in zip(tokenized["input_ids"], tokenized["attention_mask"], strict=True)
]
else:
prompt_ids = tokenized["input_ids"]
# For VLMs, the processor returns extra multimodal fields (pixel_values, image_grid_thw, etc.)
multimodal_fields = {k: v for k, v in tokenized.items() if k not in ("input_ids", "attention_mask")}
else:
prompt_ids = self.processing_class(text=prompts)["input_ids"]
images = None
multimodal_fields = {}
return prompt_ids, images, multimodal_fields
def _generate_single_turn(self, prompt_ids, images, multimodal_fields):
device = self.accelerator.device
mode = "train" if self.model.training else "eval"
# Generate completions using either vLLM or regular generation
if self.use_vllm:
# Sync weights if training step changed
if self.state.global_step != self._last_loaded_step:
if not getattr(getattr(self.vllm_generation, 'llm', None), 'shared_weights', False):
with profiling_context(self, 'sync_weights'):
self.vllm_generation.sync_weights()
self._last_loaded_step = self.state.global_step
# Generate using vLLM with raw token IDs
num_generations = self.num_generations if mode == "train" else self.num_generations_eval
_, completion_ids, logprobs, _ = self.vllm_generation.generate(
prompts=prompt_ids,
images=images,
num_generations=num_generations,
profiler=profiling_context(self, "vLLM.generate"),
)
# vLLM returns per-token top-k logprobs; keep only the top-1 (sampled token) logprob
logprobs = [[lp[0] for lp in seq] for seq in logprobs]
elif self.use_transformers_continuous_batching:
with (
profiling_context(self, "transformers.generate_batch"),
unwrap_model_for_generation(
self.model_wrapped, self.accelerator, gather_deepspeed3_params=self.args.ds3_gather_for_generation
) as unwrapped_model,
torch.no_grad(),
self._dist.summon_full_params(self.model_wrapped, recurse=False),
):
# Cast to the appropriate dtype based on training configuration
if self.args.bf16:
unwrapped_model.to(torch.bfloat16)
elif self.args.fp16:
unwrapped_model.to(torch.float16)
if self.args.cast_lm_head_to_fp32:
unwrapped_model.lm_head.to(torch.float32)
all_outputs = unwrapped_model.generate_batch(
prompt_ids,
generation_config=self.generation_config,
continuous_batching_config=self.continuous_batching_config,
progress_bar=False,
)
unwrapped_model.train()
completion_ids = [output.generated_tokens for output in all_outputs.values()]
logprobs = None
else:
# Regular generation path: left-pad token IDs into tensors
prompt_tensors = [torch.tensor(ids) for ids in prompt_ids]
padded_ids = pad(prompt_tensors, padding_value=self._tokenizer.pad_token_id, padding_side="left")
attention_mask = pad([torch.ones_like(t) for t in prompt_tensors], padding_value=0, padding_side="left")
generate_inputs = {"input_ids": padded_ids, "attention_mask": attention_mask}
# For VLMs, include multimodal fields as tensors (pixel_values, image_grid_thw, etc.)
for k, v in multimodal_fields.items():
if isinstance(v, torch.Tensor):
generate_inputs[k] = v
elif isinstance(v, list) and v and isinstance(v[0], list):
# Per-token field (e.g., token_type_ids): left-pad like input_ids
generate_inputs[k] = pad([torch.tensor(x) for x in v], padding_value=0, padding_side="left")
else:
generate_inputs[k] = torch.tensor(np.array(v))
generate_inputs = super()._prepare_inputs(generate_inputs)
if "mm_token_type_ids" in generate_inputs or "image_grid_thw" in generate_inputs:
mm_token_type_ids = _unsloth_fix_mm_token_type_ids(
self.processing_class,
generate_inputs["input_ids"],
generate_inputs.get("mm_token_type_ids", None),
)
if mm_token_type_ids is not None:
generate_inputs["mm_token_type_ids"] = mm_token_type_ids
with (
profiling_context(self, "transformers.generate"),
unwrap_model_for_generation(
self.model_wrapped,
self.accelerator,
gather_deepspeed3_params=self.args.ds3_gather_for_generation,
generation_kwargs=self.generation_kwargs, # Override model.generation_config with generation_kwargs to fix transformers#42762
) as unwrapped_model,
torch.no_grad(),
self._dist.summon_full_params(self.model_wrapped, recurse=False),
):
prompt_completion_ids = unwrapped_model.generate(
**generate_inputs, generation_config=self.generation_config
)
# Compute prompt length and extract completion ids
prompt_length = generate_inputs["input_ids"].size(1)
completion_ids = prompt_completion_ids[:, prompt_length:]
# Mask everything after the first EOS token
is_eos = completion_ids == self._tokenizer.eos_token_id
eos_idx = torch.full((is_eos.size(0),), is_eos.size(1), dtype=torch.long, device=device)
eos_idx[is_eos.any(dim=1)] = is_eos.int().argmax(dim=1)[is_eos.any(dim=1)]
sequence_indices = torch.arange(is_eos.size(1), device=device).expand(is_eos.size(0), -1)
completion_mask = (sequence_indices <= eos_idx.unsqueeze(1)).int()
completion_ids = [
c[m].tolist() for c, m in zip(completion_ids.cpu(), completion_mask.bool().cpu(), strict=True)
]
logprobs = None # not used in this case
return completion_ids, logprobs
def _get_tool_suffix_ids(self, tool_messages):
"""Get token IDs for tool result formatting by using a minimal dummy conversation."""
# Use the real tool name instead of a dummy: some templates (e.g. GPT-OSS) derive the tool response
# header from the assistant's tool call name.
dummy_tool_calls = [{"type": "function", "function": {"name": tool_messages[0]["name"], "arguments": {}}}]
dummy_messages = [
{"role": "user", "content": "dummy"},
{
"role": "assistant",
# "content" is required here because VLM processors crash on tokenize=True without it
# (KeyError in processing_utils.py). See huggingface/transformers#45290.
"content": "",
"tool_calls": dummy_tool_calls,
},
]
if self._is_vlm:
dummy_messages = prepare_multimodal_messages(dummy_messages)
tool_messages = prepare_multimodal_messages(tool_messages)
prefix_ids = self.processing_class.apply_chat_template(
dummy_messages,
add_generation_prompt=False,
tokenize=True,
chat_template=self.chat_template,
return_dict=False,
**self.chat_template_kwargs,
)
full_ids = self.processing_class.apply_chat_template(
dummy_messages + tool_messages,
add_generation_prompt=True,
tokenize=True,
chat_template=self.chat_template,
return_dict=False,
**self.chat_template_kwargs,
)
# VLM processors return batched output (list of lists), unbatch for single conversation
if self._is_vlm:
prefix_ids = prefix_ids[0]
full_ids = full_ids[0]
# Some chat templates (notably Qwen3/Qwen3.5) render "...<|im_end|>\n" after an assistant/tool block.
# When we compute `suffix_ids` by slicing `full_ids`, we must align the slicing boundary to
# EOS (not EOS + newline). Templates that don't use EOS as end-of-turn (e.g. Gemma uses
# <turn|>) skip this trimming.
eos_positions = [i for i, tok_id in enumerate(prefix_ids) if tok_id == self._tokenizer.eos_token_id]
if eos_positions:
prefix_ids = prefix_ids[: eos_positions[-1] + 1]
if full_ids[: len(prefix_ids)] != prefix_ids:
raise ValueError("Unexpected tokenization: the EOS-trimmed prefix IDs are not a prefix of the full IDs.")
return full_ids[len(prefix_ids) :]
def _tool_call_loop(self, prompts, prompt_ids, completion_ids, completions, logprobs, images, multimodal_fields):
# Tool execution loop: execute tools, then regenerate completions with tool results appended to the prompt
tool_calls = [completion[0].get("tool_calls") for completion in completions]
idxs_with_tool = [idx for idx, tool_call in enumerate(tool_calls) if tool_call]
tool_calls = [tool_calls[idx] for idx in idxs_with_tool]
tool_mask = [[1] * len(ids) for ids in completion_ids] # 0 for tool result tokens, 1 elsewhere
# Collect images from multimodal tool responses for the forward pass
tool_images = [[] for _ in completion_ids]
tool_call_count = 0
tool_failure_count = 0
iteration_num = 0
while idxs_with_tool and iteration_num < self.max_tool_calling_iterations:
prompt_completion_tools = [prompts[i] for i in idxs_with_tool] # select only prompts that need tool calls
# Snapshot state so we can rollback tool results that would exceed max_completion_length
completions_len_before = [len(completions[i]) for i in idxs_with_tool]
tool_images_len_before = [len(tool_images[i]) for i in idxs_with_tool]
prompts_len_before = [len(prompts[i]) for i in idxs_with_tool]
# Call the tools, and build the new prompt for generation
for idx in range(len(idxs_with_tool)):
idx_with_tool = idxs_with_tool[idx]
tool_call_list = tool_calls[idx]
prompt_completion_tool = prompt_completion_tools[idx]
sync_tool_dict = self._sync_tool_dicts[idx_with_tool]
async_tool_dict = self._async_tool_dicts[idx_with_tool]
# Append the last assistant message (which triggered tool_calls) to the prompt
prompt_completion_tool.append(completions[idx_with_tool][-1])
async_coros = []
tool_call_results = []
for tool_call in tool_call_list:
tool_call_count += 1
if tool_call["type"] == "function":
function = tool_call["function"]
name = function["name"]
try:
if name in sync_tool_dict:
tool_call_results.append((name, sync_tool_dict[name](**function["arguments"])))
elif name in async_tool_dict:
async_coros.append((name, async_tool_dict[name](**function["arguments"])))
else:
raise ValueError(f"Tool {name} not found.")
except Exception as e:
tool_failure_count += 1
result = {"error": str(e)}
tool_call_results.append((name, result))
else:
tool_failure_count += 1
name = tool_call.get("name", "unknown")
tool_call_results.append((name, {"error": f"Unsupported tool call type: {tool_call['type']}"}))
if async_coros:
async def _run_async_tools(async_coros):
coros = [coro for _, coro in async_coros]
results = await asyncio.gather(*coros, return_exceptions=True)
return [(name, result) for (name, _), result in zip(async_coros, results, strict=False)]
async_results = asyncio.run_coroutine_threadsafe(
_run_async_tools(async_coros), self.async_loop
).result()
for name, result in async_results:
if isinstance(result, Exception):
tool_failure_count += 1
tool_call_results.append((name, {"error": str(result)}))
else:
tool_call_results.append((name, result))
for name, result in tool_call_results:
# Support multimodal tool responses: if the tool returns a list of content blocks
# (e.g., [{"type": "image", "image": ...}, {"type": "text", "text": "..."}]),
# pass them through directly so _tokenize_prompts can extract images for VLMs.
content = result if isinstance(result, list) else str(result)
tool_message = {"role": "tool", "name": name, "content": content}
# Collect images from multimodal tool responses
if isinstance(content, list):
for part in content:
if isinstance(part, dict) and part.get("type") == "image":
tool_images[idx_with_tool].append(part["image"])
prompt_completion_tool.append(tool_message)
completions[idx_with_tool].append(tool_message)
# Build token IDs by concatenation: prompt + completion + tool_suffix.
prompt_completion_tool_ids = []
for idx in range(len(idxs_with_tool)):
idx_with_tool = idxs_with_tool[idx]
# Extract trailing tool messages from completions
tool_messages = []
for message in reversed(completions[idx_with_tool]):
if message["role"] == "tool":
tool_messages.insert(0, message)
else:
break
suffix_ids = self._get_tool_suffix_ids(tool_messages)
prompt_completion_tool_ids.append(
prompt_ids[idx_with_tool] + completion_ids[idx_with_tool] + suffix_ids
)
# Drop tool results whose addition would push the sequence past max_completion_length (the completion
# budget) or past the backend context ceiling (vLLM and transformers will error out on inputs longer than
# the model's max length). The sample exits the loop with its completion as-is, and the tool
# messages/images appended this iteration are rolled back so completions and tool_images stay consistent
# with completion_ids.
if self.use_vllm and self.vllm_mode == "colocate":
max_model_len = self.vllm_generation.llm.llm_engine.model_config.max_model_len
else:
config = self.model.config.text_config if self._is_vlm else self.model.config
max_model_len = config.max_position_embeddings
overlong = [
len(pct) - len(prompt_ids[i]) > self.max_completion_length or len(pct) >= max_model_len
for i, pct in zip(idxs_with_tool, prompt_completion_tool_ids, strict=True)
]
for idx in range(len(idxs_with_tool)):
if overlong[idx]:
idx_with_tool = idxs_with_tool[idx]
del completions[idx_with_tool][completions_len_before[idx] :]
del tool_images[idx_with_tool][tool_images_len_before[idx] :]
del prompts[idx_with_tool][prompts_len_before[idx] :]
# Keep only non-overlong items for further processing
idxs_with_tool = [idx for idx, o in zip(idxs_with_tool, overlong, strict=True) if not o]
prompt_completion_tool_ids = [
pct for pct, o in zip(prompt_completion_tool_ids, overlong, strict=True) if not o
]
if not idxs_with_tool:
break # all overlong, exit tool loop
# Filter images and multimodal fields to match the current subset (index into full batch).
# Merge tool response images so the model can see visual feedback during generation.
merged_images = images
if any(imgs for imgs in tool_images):
if merged_images is None:
merged_images = [imgs if imgs else None for imgs in tool_images]
else:
merged_images = [
(existing or []) + new for existing, new in zip(merged_images, tool_images, strict=True)
]
loop_images = [merged_images[i] for i in idxs_with_tool] if merged_images else None
if multimodal_fields:
loop_multimodal_fields = {}
for k, v in multimodal_fields.items():
selected = [v[i] for i in idxs_with_tool]
# Per-token fields (e.g. token_type_ids) need zero-padding to match extended prompt length
if isinstance(selected[0], list):
selected = [
s + [0] * (len(pct) - len(s))
for s, pct in zip(selected, prompt_completion_tool_ids, strict=True)
]
loop_multimodal_fields[k] = selected
else:
loop_multimodal_fields = {}
# Generate new completions after tool execution (using concatenated IDs, no re-tokenization)
post_tool_ids, post_tool_logprobs = self._generate_single_turn(
prompt_completion_tool_ids, loop_images, loop_multimodal_fields
)
# Truncate so that pct[len(prompt_ids[idx]) :] + post_tool does not exceed max_completion_length.
# The pre-regen check guarantees len(completion_tool_ids) <= max_completion_length, so any
# excess can only come from post_tool_ids. post_tool_ids is model-generated text and never
# contains image tokens, so a plain slice is safe.
for idx in range(len(idxs_with_tool)):
idx_with_tool = idxs_with_tool[idx]
completion_tool_length = len(prompt_completion_tool_ids[idx]) - len(prompt_ids[idx_with_tool])
excess_length = completion_tool_length + len(post_tool_ids[idx]) - self.max_completion_length
if excess_length > 0:
new_len = len(post_tool_ids[idx]) - excess_length
post_tool_ids[idx] = post_tool_ids[idx][:new_len]
if logprobs is not None:
post_tool_logprobs[idx] = post_tool_logprobs[idx][:new_len]
# Update tool_mask: the tool result should be 0 and the post-tool 1
for idx in range(len(idxs_with_tool)):
idx_with_tool = idxs_with_tool[idx]
prompt_completion_tool_length = len(prompt_completion_tool_ids[idx])
prompt_length = len(prompt_ids[idx_with_tool])
completion_length = len(completion_ids[idx_with_tool])
post_tool_length = len(post_tool_ids[idx])
tool_length = prompt_completion_tool_length - prompt_length - completion_length
tool_mask[idx_with_tool] += [0] * tool_length + [1] * post_tool_length
if logprobs is not None:
logprobs[idx_with_tool] += [0.0] * tool_length + post_tool_logprobs[idx]
# Update completion_ids with the new completions (after tool execution)
for idx in range(len(idxs_with_tool)):
idx_with_tool = idxs_with_tool[idx]
prompt_length = len(prompt_ids[idx_with_tool])
pct = prompt_completion_tool_ids[idx] # = prompt-completion-tool
completion_ids[idx_with_tool] = pct[prompt_length:] + post_tool_ids[idx]
# Decode post-tool completions.
post_tool_completions = [parse_response(self._tokenizer, ids) if ids else {} for ids in post_tool_ids]
# Add post-tool completions to the existing completions
for idx in range(len(idxs_with_tool)):
idx_with_tool = idxs_with_tool[idx]
if post_tool_completions[idx]: # {} if post-tool completions completely truncated
completions[idx_with_tool].append(post_tool_completions[idx])
# Check for further tool calls
tool_calls = [completion.get("tool_calls") for completion in post_tool_completions]
idxs_with_tool = [idx for idx, tool_call in zip(idxs_with_tool, tool_calls, strict=True) if tool_call]
tool_calls = [tool_call for tool_call in tool_calls if tool_call]
iteration_num += 1
return tool_mask, completions, completion_ids, logprobs, tool_call_count, tool_failure_count, tool_images
def _generate(self, prompts: list):
device = self.accelerator.device
mode = "train" if self.model.training else "eval"
# Copy the prompts to avoid modifying the original list
prompts = copy.deepcopy(prompts)
if self.rollout_func is not None:
# Keep vLLM weights in sync for custom rollouts that rely on vLLM utilities.
if self.use_vllm and self.state.global_step != self._last_loaded_step:
if not getattr(getattr(self.vllm_generation, 'llm', None), 'shared_weights', False):
with profiling_context(self, 'sync_weights'):
self.vllm_generation.sync_weights()
self._last_loaded_step = self.state.global_step
# Pass prompts to rollout_func preserving structured messages.
# Chat templating must happen inside rollout_func, at the backend boundary, so that
# multimodal content (images, typed content blocks) is not lost before rollout logic runs.
output = self.rollout_func(prompts, self)
required_keys = {"prompt_ids", "completion_ids", "logprobs"}
missing_keys = required_keys - output.keys()
if missing_keys:
missing_keys_list = sorted(missing_keys)
raise ValueError(f"rollout_func must return keys {missing_keys_list} in its output dict.")
extra_fields = {k: v for k, v in output.items() if k not in required_keys}
prompt_ids, completion_ids, logprobs = output["prompt_ids"], output["completion_ids"], output["logprobs"]
images = None
multimodal_fields = {}
else:
prompt_ids, images, multimodal_fields = self._tokenize_prompts(prompts)
completion_ids, logprobs = self._generate_single_turn(prompt_ids, images, multimodal_fields)
extra_fields = {}
# Decode completions. It's important to use `parse_response` when possible, because it handles tool calls.
if is_conversational({"prompt": prompts[0]}):
if (
Version(transformers.__version__) >= Version("5.0.0") # parse_response added in v5
and hasattr(self._tokenizer, "response_schema") # attribute not set by default for now
and self._tokenizer.response_schema is not None # only works if the tokenizer has a schema
):
completions = [[parse_response(self._tokenizer, ids)] for ids in completion_ids]
else:
contents = self.processing_class.batch_decode(completion_ids, skip_special_tokens=True)
completions = [[{"role": "assistant", "content": content}] for content in contents]
else:
completions = self.processing_class.batch_decode(completion_ids, skip_special_tokens=True)
# Extract tool calls from the completions and (possibly) execute them
tool_images = []
if self.tools:
(
tool_mask,
completions,
completion_ids,
logprobs,
tool_call_count,
tool_failure_count,
tool_images,
) = self._tool_call_loop(
prompts, prompt_ids, completion_ids, completions, logprobs, images, multimodal_fields
)
# Merge tool response images into the images list for the forward pass
if any(imgs for imgs in tool_images):
if images is None:
images = [imgs if imgs else None for imgs in tool_images]
else:
images = [(existing or []) + new for existing, new in zip(images, tool_images, strict=True)]
else:
# Support custom env_mask from rollout_func (e.g., for environment feedback masking)
# Internally treated as tool_mask - marks model tokens (1) vs external tokens (0)
tool_mask = extra_fields.pop("env_mask", None)
# Get completion length per sequence, used for logging
prompt_lengths = torch.tensor([len(ids) for ids in prompt_ids], device=device)
if tool_mask is not None: # count only model-generated tokens (tool_mask=1)
completion_lengths = torch.tensor([sum(mask) for mask in tool_mask], device=device)
else:
completion_lengths = torch.tensor([len(ids) for ids in completion_ids], device=device)
agg_prompt_lengths = self.accelerator.gather(prompt_lengths)
agg_completion_lengths = self.accelerator.gather(completion_lengths)
total_prompt_tokens = agg_prompt_lengths.sum()
total_completion_tokens = agg_completion_lengths.sum() # = num_items_in_batch, required for the DAPO loss
# Log the metrics
if mode == "train":
self.state.num_input_tokens_seen += (total_prompt_tokens + total_completion_tokens).item()
self._metrics[mode]["num_tokens"] = [self.state.num_input_tokens_seen]
# Log completion lengths, mean, min, max
self._metrics[mode]["completions/mean_length"].append(agg_completion_lengths.float().mean().item())
self._metrics[mode]["completions/min_length"].append(agg_completion_lengths.float().min().item())
self._metrics[mode]["completions/max_length"].append(agg_completion_lengths.float().max().item())
# Identify sequences that terminated with EOS and log their lengths
eos_and_pad = [self._tokenizer.eos_token_id, self._tokenizer.pad_token_id]
is_truncated = torch.tensor([ids[-1] not in eos_and_pad for ids in completion_ids], device=device)
agg_is_truncated = self.accelerator.gather(is_truncated)
self._metrics[mode]["completions/clipped_ratio"].append(agg_is_truncated.float().mean().item())
term_completion_lengths = agg_completion_lengths[~agg_is_truncated]
if len(term_completion_lengths) == 0: # edge case where no terminated sequences are found
term_completion_lengths = torch.zeros(1, device=device)
self._metrics[mode]["completions/mean_terminated_length"].append(term_completion_lengths.float().mean().item())
self._metrics[mode]["completions/min_terminated_length"].append(term_completion_lengths.float().min().item())
self._metrics[mode]["completions/max_terminated_length"].append(term_completion_lengths.float().max().item())
if self.tools:
agg_tool_call_count = self.accelerator.gather(torch.tensor(tool_call_count, device=device)).sum()
tool_call_frequency = (agg_tool_call_count / len(agg_prompt_lengths)).item()
self._metrics[mode]["tools/call_frequency"].append(tool_call_frequency)
agg_tool_failure_count = self.accelerator.gather(torch.tensor(tool_failure_count, device=device)).sum()
failure_frequency = (
(agg_tool_failure_count / agg_tool_call_count).item() if agg_tool_call_count > 0 else 0.0
)
self._metrics[mode]["tools/failure_frequency"].append(failure_frequency)
return (
prompt_ids,
completion_ids,
tool_mask,
completions,
total_completion_tokens,
logprobs,
extra_fields,
images,
tool_images,
)
def _generate_and_score_completions(
self, inputs: list[dict[str, torch.Tensor | Any]]
) -> dict[str, torch.Tensor | Any]:
device = self.accelerator.device
mode = "train" if self.model.training else "eval"
prompts = [x["prompt"] for x in inputs]
# Unsloth: Extract per-sample chat_template_kwargs before metadata is lost
_ct_ = getattr(self.processing_class, 'chat_template', None) or ''
_sk_ = {'prompt', 'chosen', 'rejected', 'completion', 'messages', 'label',
'images', 'image', 'videos', 'video', 'audios', 'audio'}
self._unsloth_batch_chat_kwargs = []
for _inp_ in inputs:
_kw_ = {}
if isinstance(_inp_, dict):
for _k_ in _inp_.keys() - _sk_:
if _k_ in _ct_ and isinstance(_inp_[_k_], str):
_kw_[_k_] = _inp_[_k_]
self._unsloth_batch_chat_kwargs.append(_kw_)
if self.environments:
for prompt, environment, reset_kwargs in zip(prompts, self.environments, inputs, strict=True):
observation = environment.reset(**reset_kwargs)
if observation is None:
continue
if isinstance(observation, list) and isinstance(prompt[-1]["content"], str):
prompt[-1]["content"] = [{"type": "text", "text": prompt[-1]["content"]}]
if isinstance(observation, str) and isinstance(prompt[-1]["content"], list):
observation = [{"type": "text", "text": observation}]
prompt[-1]["content"] += observation
if "images" in inputs[0]:
images = [example.get("images") for example in inputs]
elif "image" in inputs[0]:
images = [[example.get("image")] if example.get("image") is not None else None for example in inputs]
else:
images = None
# Transformers requires at least one image in the batch, otherwise it throws an error
if images is not None and all(img_list == [] for img_list in images):
images = None
# If the prompts are conversational and the inputs contain images, we need to convert the prompts from
# [{"role": "user", "content": "What color is the sky?"}] to
# [{"role": "user", "content": [{"type": "image", "image": <Image>}, {"type": "text", "text": "What color is the sky?"}]}]
if images is not None:
if not is_conversational(inputs[0]):
raise ValueError(
"Multimodal training requires conversational prompts. It looks like the dataset contains "
"non-conversational inputs, likely because a chat template was applied before passing the dataset "
"to the trainer. Please provide the raw conversational prompts and let the trainer apply the chat "
"template internally."
)
prompts = [
prepare_multimodal_messages(prompt, images=image_list)
for prompt, image_list in zip(prompts, images, strict=True)
]
dataset_images = images # preserve dataset images before _generate may overwrite
(
prompt_ids_list,
completion_ids_list,
tool_mask_list,
completions,
num_items_in_batch,
sampling_per_token_logps_list,
extra_fields,
images,
tool_images,
) = self._generate(prompts)
_unsloth_clear_stateful_mrope(
self.accelerator.unwrap_model(self.model, keep_fp32_wrapper = False)
)
if images is None:
images = dataset_images # restore dataset images (rollout_func path returns None)
# Convert lists of token IDs to padded tensors
prompt_ids = [torch.tensor(ids) for ids in prompt_ids_list]
prompt_mask = [torch.ones_like(ids, dtype=torch.long) for ids in prompt_ids]
prompt_ids = pad(
prompt_ids,
padding_value=self._tokenizer.pad_token_id,
padding_side="left",
pad_to_multiple_of=self.pad_to_multiple_of,
).to(device=device)
prompt_mask = pad(
prompt_mask, padding_value=0, padding_side="left", pad_to_multiple_of=self.pad_to_multiple_of
).to(device=device)
completion_ids = [torch.tensor(ids) for ids in completion_ids_list]
completion_mask = [torch.ones_like(ids, dtype=torch.long) for ids in completion_ids]
completion_ids = pad(
completion_ids,
padding_value=self._tokenizer.pad_token_id,
padding_side="right",
pad_to_multiple_of=self.pad_to_multiple_of,
).to(device=device)
completion_mask = pad(
completion_mask, padding_value=0, padding_side="right", pad_to_multiple_of=self.pad_to_multiple_of
).to(device=device)
if sampling_per_token_logps_list is not None:
sampling_per_token_logps = [torch.tensor(logps) for logps in sampling_per_token_logps_list]
sampling_per_token_logps = pad(
sampling_per_token_logps,
padding_value=0.0,
padding_side="right",
pad_to_multiple_of=self.pad_to_multiple_of,
).to(device=device)
else:
sampling_per_token_logps = None
if tool_mask_list is not None:
tool_mask = [torch.tensor(mask) for mask in tool_mask_list]
tool_mask = pad(
tool_mask, padding_value=1, padding_side="right", pad_to_multiple_of=self.pad_to_multiple_of
).to(device=device)
else:
tool_mask = None
# If mask_truncated_completions is enabled, zero out truncated completions for attention and loss masking
if self.mask_truncated_completions:
eos_and_pad = [self._tokenizer.eos_token_id, self._tokenizer.pad_token_id]
is_truncated = torch.tensor([ids[-1] not in eos_and_pad for ids in completion_ids_list], device=device)
# Mask completion_mask for attention masking
completion_mask = completion_mask * (~is_truncated).unsqueeze(1).int()
# Also mask tool_mask for consistency in multi-turn training
if tool_mask is not None:
tool_mask = tool_mask * (~is_truncated).unsqueeze(1).int()
# Concatenate prompt_mask with completion_mask for logit computation
prompt_completion_ids = torch.cat([prompt_ids, completion_ids], dim=1) # (B, P+C)
attention_mask = torch.cat([prompt_mask, completion_mask], dim=1) # (B, P+C)
logits_to_keep = completion_ids.size(1) # we only need to compute the logits for the completion tokens
max_left_pad = None
batch_size = self.args.per_device_train_batch_size if mode == "train" else self.args.per_device_eval_batch_size
try:
# TRL 0.23.1 and below path
if not has_images:
# Left pad prompt before calculation old and ref hidden states
left_pad_tokens_per_prompt = calculate_pad_tokens_in_prompt(prompt_completion_ids, logits_to_keep, self.processing_class.pad_token_id)
max_left_pad = torch.max(left_pad_tokens_per_prompt).item()
except:
# TRL 0.24.0 and below path
if images is None:
# Left pad prompt before calculation old and ref hidden states
left_pad_tokens_per_prompt = calculate_pad_tokens_in_prompt(prompt_completion_ids, logits_to_keep, self.processing_class.pad_token_id)
max_left_pad = torch.max(left_pad_tokens_per_prompt).item()
self.model.for_training(use_gradient_checkpointing=getattr(self.args, 'gradient_checkpointing', True))
num_images = [len(img_list) if img_list else 0 for img_list in images] if images is not None else None
# Get forward_kwargs for models with multimodal inputs.
# When tool images are present (from _tool_call_loop), use image_processor directly and build
# mm_token_type_ids from prompt_completion_ids. Otherwise, use the full processor pipeline
# which returns model-specific keys (image_sizes, pixel_attention_mask, etc.).
if self.tools and any(imgs for imgs in tool_images) and self._is_vlm:
flat_images = [img for img_list in images if img_list for img in img_list]
image_inputs = self.processing_class.image_processor(images=flat_images, return_tensors="pt")
image_inputs = super()._prepare_inputs(image_inputs)
forward_kwargs = dict(image_inputs)
elif images is not None:
prompts_text = [
apply_chat_template(
{"prompt": prompt}, self.processing_class, tools=self.tools, **self.chat_template_kwargs
)["prompt"]
for prompt in prompts
]
prompt_inputs = self.processing_class(images=images, text=prompts_text, padding=True, return_tensors="pt")
prompt_inputs = super()._prepare_inputs(prompt_inputs)
forward_kwargs = {k: v for k, v in prompt_inputs.items() if k not in ["input_ids", "attention_mask"]}
else:
forward_kwargs = {}
# Recover LFM2-VL tile counts; the full processor drops row/column metadata.
num_tiles = None
if images is not None and "spatial_shapes" in forward_kwargs:
image_info = self.processing_class.image_processor(
images=images, return_tensors="pt", return_row_col_info=True
)
tiles_per_image = image_info["image_rows"] * image_info["image_cols"]
if self.processing_class.image_processor.use_thumbnail:
tiles_per_image = tiles_per_image + (tiles_per_image > 1).to(tiles_per_image.dtype)
num_tiles = [group.sum().item() for group in torch.split(tiles_per_image, num_images)]
# If token_type_ids are used, extend them with zeros for the completion part
if "token_type_ids" in forward_kwargs:
token_type_ids = forward_kwargs["token_type_ids"]
if self.pad_to_multiple_of is not None:
# Needed only with pad_to_multiple_of: otherwise prompt_ids and token_type_ids must have equal len
padding_size = prompt_ids.size(1) - token_type_ids.size(1)
if padding_size > 0:
token_type_ids = torch.cat(
[token_type_ids.new_zeros((token_type_ids.size(0), padding_size)), token_type_ids], dim=1
)
forward_kwargs["token_type_ids"] = torch.cat(
[token_type_ids, token_type_ids.new_zeros(completion_ids.shape)], dim=1
)
# If mm_token_type_ids are used, extend them with zeros for the completion part
if "mm_token_type_ids" in forward_kwargs:
mm_token_type_ids = forward_kwargs["mm_token_type_ids"]
if self.pad_to_multiple_of is not None:
# Needed only with pad_to_multiple_of: otherwise prompt_ids and mm_token_type_ids must have equal len
padding_size = prompt_ids.size(1) - mm_token_type_ids.size(1)
if padding_size > 0:
mm_token_type_ids = torch.cat(
[mm_token_type_ids.new_zeros((mm_token_type_ids.size(0), padding_size)), mm_token_type_ids],
dim=1,
)
forward_kwargs["mm_token_type_ids"] = torch.cat(
[mm_token_type_ids, mm_token_type_ids.new_zeros(completion_ids.shape)], dim=1
)
if "mm_token_type_ids" in forward_kwargs or "image_grid_thw" in forward_kwargs:
_mm_token_type_ids = _unsloth_fix_mm_token_type_ids(
self.processing_class,
prompt_completion_ids,
forward_kwargs.get("mm_token_type_ids", None),
completion_ids = completion_ids,
)
if _mm_token_type_ids is not None:
forward_kwargs["mm_token_type_ids"] = _mm_token_type_ids
# For VLM tool images: build token type IDs from the full prompt_completion_ids.
# This must happen AFTER the token_type_ids/mm_token_type_ids extension blocks above,
# because our version already covers the full sequence (images are in the completion,
# not just the prompt).
if self.tools and any(imgs for imgs in tool_images) and self._is_vlm:
mm_ids = torch.zeros_like(prompt_completion_ids)
if self._image_pad_token_id is not None:
mm_ids[prompt_completion_ids == self._image_pad_token_id] = 1
if self._video_pad_token_id is not None:
mm_ids[prompt_completion_ids == self._video_pad_token_id] = 2
# Use the same key the model expects: token_type_ids for models like Gemma,
# mm_token_type_ids for models like Qwen.
image_grid_thw = forward_kwargs.get("image_grid_thw")
if image_grid_thw is not None:
forward_kwargs["mm_token_type_ids"] = mm_ids
else:
forward_kwargs["token_type_ids"] = mm_ids
# Truncation safety (Qwen-style models with image_grid_thw only): if
# max_completion_length truncated some image tokens, the number of image pad tokens
# in input_ids won't match pixel_values features. Check per-sample and drop ALL
# images for any sample with a mismatch (safe fallback).
if image_grid_thw is not None and num_images is not None:
merge_length = getattr(self.processing_class.image_processor, "merge_size", 2) ** 2
img_offset = 0
has_mismatch = False
for b in range(mm_ids.shape[0]):
sample_tokens = (mm_ids[b] == 1).sum().item()
sample_features = 0
for i in range(num_images[b]):
grid_idx = img_offset + i
if grid_idx < image_grid_thw.shape[0]:
sample_features += image_grid_thw[grid_idx].prod().item() // merge_length
if sample_tokens != sample_features:
has_mismatch = True
break
img_offset += num_images[b]
if has_mismatch:
# Drop all images: safer than partial trim which is error-prone
forward_kwargs.pop("pixel_values", None)
forward_kwargs.pop("image_grid_thw", None)
mm_ids.zero_()
forward_kwargs["mm_token_type_ids"] = mm_ids
num_images = None
# When gradient checkpointing is enabled with use_reentrant=True (non default), calling the model inside a
# torch.no_grad() block triggers a harmless PyTorch warning ("None of the inputs have requires_grad=True").
# Temporarily disable checkpointing to avoid this warning during inference.
with torch.no_grad(), disable_gradient_checkpointing(self.model, self.args.gradient_checkpointing_kwargs):
# If the generation and optimization steps are misaligned—i.e., if generation does not occur at the end of
# a full optimizer step (when gradient_accumulation_steps is not a multiple of generate_every)—then the
# samples may come from an earlier version of the model. In that case, we need to track old_per_token_logps
# for importance sampling. If the steps are aligned, importance sampling isn't necessary and we set
# old_per_token_logps to None.
# When using vLLM, we always compute old_per_token_logps for importance sampling, it was shown that the
# distribution mismatch between vLLM and the training model can be large and harm the training.
generate_every = self.args.steps_per_generation * self.num_iterations # generation frequency
if self.args.gradient_accumulation_steps % generate_every != 0 or (
self.use_vllm
):
old_per_token_logps, _, _ = self._get_per_token_logps_and_entropies(
self.model,
prompt_completion_ids,
attention_mask,
logits_to_keep,
batch_size,
num_images=num_images,
num_tiles=num_tiles,
**forward_kwargs, # may contain pixel_values, image_grid_thw, pixel_attention_mask, spatial_shapes, image_sizes, image_position_ids
)
else:
old_per_token_logps = None
# Compute the importance sampling ratio when using vLLM, to correct for potential distribution mismatch
if False and self.use_vllm and self.vllm_importance_sampling_correction:
mask = completion_mask if tool_mask is None else completion_mask * tool_mask
per_token_logps_diff = (old_per_token_logps - sampling_per_token_logps) * mask
sequence_level_is = self.vllm_importance_sampling_mode in ["sequence_mask", "sequence_truncate"]
if sequence_level_is:
per_sequence_logps_diff = per_token_logps_diff.sum(dim=-1, keepdim=True)
logps_diff = per_sequence_logps_diff
else:
logps_diff = per_token_logps_diff
vllm_importance_sampling_ratio = torch.exp(logps_diff)
# vllm_importance_sampling_ratio.shape:
# token_* modes: (B, T) (per-token ratio)
# sequence_* modes: (B, 1) (per-sequence ratio)
if self.vllm_importance_sampling_mode in ["sequence_truncate", "token_truncate"]:
vllm_importance_sampling_ratio = torch.clamp(
vllm_importance_sampling_ratio,
min=self.vllm_importance_sampling_clip_min,
max=self.vllm_importance_sampling_clip_max,
)
elif self.vllm_importance_sampling_mode in ["sequence_mask", "token_mask"]:
min_val = (
self.vllm_importance_sampling_clip_min
if self.vllm_importance_sampling_clip_min is not None
else -math.inf
)
max_val = (
self.vllm_importance_sampling_clip_max
if self.vllm_importance_sampling_clip_max is not None
else math.inf
)
invalid_mis_mask = (vllm_importance_sampling_ratio < min_val) | (
vllm_importance_sampling_ratio > max_val
)
vllm_importance_sampling_ratio = vllm_importance_sampling_ratio.masked_fill(
invalid_mis_mask, value=0.0
)
else:
raise ValueError(
f"Unknown vLLM importance sampling level: {self.vllm_importance_sampling_mode}. Possible values are 'token_truncate', 'token_mask', 'sequence_truncate', and 'sequence_mask'."
)
# Compute the per-token log probabilities for the reference model
if self.beta != 0.0:
if self.ref_model is not None:
ref_per_token_logps, _, _ = self._get_per_token_logps_and_entropies(
self.ref_model,
prompt_completion_ids,
attention_mask,
logits_to_keep,
batch_size=batch_size,
num_images=num_images,
num_tiles=num_tiles,
**forward_kwargs, # may contain pixel_values, image_grid_thw, pixel_attention_mask, spatial_shapes, image_sizes, image_position_ids
)
else:
# When training a PEFT adapter, how we obtain the reference depends on the setup:
# - New adapter: disabling adapters yields the base model.
# - Re-training an existing adapter: an initial copy is loaded under the name "ref".
model = self.accelerator.unwrap_model(self.model)
with use_adapter(model, adapter_name="ref" if "ref" in model.peft_config else None):
ref_per_token_logps, _, _ = self._get_per_token_logps_and_entropies(
self.model,
prompt_completion_ids,
attention_mask,
logits_to_keep,
batch_size=batch_size,
num_images=num_images,
num_tiles=num_tiles,
**forward_kwargs, # may contain pixel_values, image_grid_thw, pixel_attention_mask, spatial_shapes, image_sizes, image_position_ids
)
else:
ref_per_token_logps = None
# Decode
prompts_text = self.processing_class.batch_decode(prompt_ids, skip_special_tokens=True)
completions_text = self.processing_class.batch_decode(completion_ids, skip_special_tokens=True)
# Merge extra_fields from rollout_func into inputs for reward functions
if extra_fields:
for i, inp in enumerate(inputs):
for key, values in extra_fields.items():
if isinstance(values, list) and i < len(values):
inp[key] = values[i]
elif not isinstance(values, list):
inp[key] = values
# Calculate rewards for each reward function. rewards_per_func aggregates rewards across all processes. This is
# important because rewards will be normalized per group, and completions are distributed. We will later slice
# rewards_per_func to extract each process's subset.
if images is not None:
rewards_per_func = self._calculate_rewards(inputs, prompts_text, completions_text, completion_ids_list)
else:
rewards_per_func = self._calculate_rewards(inputs, prompts, completions, completion_ids_list)
num_generations = self.num_generations if mode == "train" else self.num_generations_eval
# A completion for which every reward function returned None is unscorable. nansum would collapse it to 0,
# which both biases the per-group baseline and hands the completion a spurious advantage. Mark these rows NaN
# so they're excluded from the (nan-aware) baseline below; their advantage is forced to 0 afterwards.
unscorable_mask = torch.isnan(rewards_per_func).all(dim=1)
if self.multi_objective_aggregation == "sum_then_normalize":
# Apply weights to each reward function's output and sum
rewards = (rewards_per_func * self.reward_weights.to(device).unsqueeze(0)).nansum(dim=1)
rewards[unscorable_mask] = torch.nan
mean_grouped_rewards = torch.nanmean(rewards.view(-1, num_generations), dim=1)
mean_grouped_rewards = mean_grouped_rewards.repeat_interleave(num_generations, dim=0)
if self.scale_rewards in ["group", "none"]:
# If self.scale_rewards = "none", we'll only use std_rewards to check for zero std for logging
if num_generations > 1:
std_rewards = nanstd(rewards.view(-1, num_generations), dim=1)
std_rewards = std_rewards.repeat_interleave(num_generations, dim=0)
else: # doesn't occur during training, but could occur in eval when num_generations_eval=1
std_rewards = torch.zeros_like(rewards)
elif self.scale_rewards == "batch":
# Compute global std
if rewards.numel() > 1:
std_rewards = nanstd(rewards).expand_as(rewards)
else: # doesn't occur during training, but could occur in eval when num_generations_eval=batch_size=1
std_rewards = torch.zeros_like(rewards)
else:
raise ValueError(
f"Invalid value for scale_rewards: {self.scale_rewards}. Must be one of 'batch', 'group', or 'none'."
)
advantages = rewards - mean_grouped_rewards
if self.scale_rewards != "none":
advantages = advantages / (std_rewards + 1e-4)
is_std_zero = torch.isclose(std_rewards, torch.zeros_like(std_rewards)) # for logging
elif self.multi_objective_aggregation == "normalize_then_sum":
grouped = rewards_per_func.view(-1, num_generations, len(self.reward_funcs))
mean_k = torch.nanmean(grouped, dim=1, keepdim=True)
std_k = nanstd(grouped, dim=1, keepdim=True) if num_generations > 1 else torch.zeros_like(mean_k)
reward_k = (grouped - mean_k) / (std_k + 1e-4)
reward_k = reward_k.view(-1, len(self.reward_funcs))
rewards = (reward_k * self.reward_weights.to(device).unsqueeze(0)).nansum(dim=1)
rewards[unscorable_mask] = torch.nan
std_rewards = nanstd(rewards).expand_as(rewards) if rewards.numel() > 1 else torch.zeros_like(rewards)
advantages = (rewards - torch.nanmean(rewards)) / (std_rewards + 1e-4)
is_std_zero = torch.isclose(std_rewards, torch.zeros_like(std_rewards)) # for logging
else:
raise ValueError(
f"Invalid multi_objective_aggregation: {self.multi_objective_aggregation}. Must be "
"'sum_then_normalize' or 'normalize_then_sum'."
)
# Unscorable completions (every reward func returned None) carry no learning signal: their reward is NaN here,
# so zero their advantage to keep them from moving the policy.
advantages = torch.nan_to_num(advantages, nan=0.0)
# Slice to keep only the local part of the data
process_slice = slice(
self.accelerator.process_index * len(prompts),
(self.accelerator.process_index + 1) * len(prompts),
)
all_process_advantages = advantages.clone() # keep the aggregated advantages for logging
advantages = advantages[process_slice]
# Calculate mean reward per function, but only for samples where the function was applied (non-NaN values)
for i, reward_func_name in enumerate(self.reward_func_names):
mean_rewards = torch.nanmean(rewards_per_func[:, i]).item()
self._metrics[mode][f"rewards/{reward_func_name}/mean"].append(mean_rewards)
std_func_rewards = nanstd(rewards_per_func[:, i]).item()
self._metrics[mode][f"rewards/{reward_func_name}/std"].append(std_func_rewards)
rewards = (rewards_per_func * self.reward_weights.to(rewards_per_func.device).unsqueeze(0)).nansum(dim=1)
rewards[unscorable_mask] = torch.nan # exclude unscorable rows from the logged reward stats
self._metrics[mode]["reward"].append(torch.nanmean(rewards).item())
self._metrics[mode]["reward_std"].append(nanstd(rewards).item())
self._metrics[mode]["frac_reward_zero_std"].append(is_std_zero.float().mean().item())
# Log prompt and completion texts
self._logs["prompt"].extend(gather_object(prompts_text))
self._logs["completion"].extend(gather_object(completions_text))
for i, name in enumerate(self.reward_func_names):
self._logs["rewards"][name].extend(rewards_per_func[:, i].tolist())
self._logs["advantages"].extend(all_process_advantages.tolist())
# Flush user-logged extra columns (from log_extra), gathering across processes.
# Keys must be sorted so that all ranks call gather_object in the same order, otherwise values
# get mis-attributed across columns (dict insertion order may differ between processes).
for column in sorted(self._pending_extra_logs):
self._logs["extra"][column].extend(gather_object(self._pending_extra_logs[column]))
self._pending_extra_logs.clear()
# Flush user-logged metrics (from log_metric), averaging across processes.
# Keys must be sorted so that all ranks call accelerator.gather in the same order, otherwise values
# get mis-attributed across metrics (dict insertion order may differ between processes).
for name in sorted(self._pending_metrics):
values = self._pending_metrics[name]
local_mean = sum(values) / len(values)
global_mean = self.accelerator.gather(torch.tensor(local_mean, device=device)).mean().item()
self._metrics[mode][name].append(global_mean)
self._pending_metrics.clear()
if images is not None:
self._logs["images"].extend(gather_object(images))
if False and self.use_vllm and self.vllm_importance_sampling_correction:
delta = torch.abs(old_per_token_logps - sampling_per_token_logps)
mask = completion_mask.bool() if tool_mask is None else (completion_mask * tool_mask).bool()
delta = delta[mask]
mean_delta = torch.mean(delta) if delta.numel() > 0 else torch.tensor(0.0, device=device)
max_delta = torch.max(delta) if delta.numel() > 0 else torch.tensor(0.0, device=device)
self._metrics[mode]["sampling/sampling_logp_difference/mean"].append(
self.accelerator.gather(mean_delta).mean().item()
)
self._metrics[mode]["sampling/sampling_logp_difference/max"].append(
self.accelerator.gather(max_delta).max().item()
)
if sequence_level_is:
flat_is_ratio = vllm_importance_sampling_ratio.flatten()
else:
flat_is_ratio = vllm_importance_sampling_ratio[mask]
min_importance_sampling_ratio = (
torch.min(flat_is_ratio) if flat_is_ratio.numel() > 0 else torch.tensor(0.0, device=device)
)
mean_importance_sampling_ratio = (
torch.mean(flat_is_ratio) if flat_is_ratio.numel() > 0 else torch.tensor(0.0, device=device)
)
max_importance_sampling_ratio = (
torch.max(flat_is_ratio) if flat_is_ratio.numel() > 0 else torch.tensor(0.0, device=device)
)
self._metrics[mode]["sampling/importance_sampling_ratio/min"].append(
nanmin(self.accelerator.gather(min_importance_sampling_ratio)).item()
)
self._metrics[mode]["sampling/importance_sampling_ratio/mean"].append(
self.accelerator.gather(mean_importance_sampling_ratio).nanmean().item()
)
self._metrics[mode]["sampling/importance_sampling_ratio/max"].append(
nanmax(self.accelerator.gather(max_importance_sampling_ratio)).item()
)
output = {
"prompt_ids": prompt_ids,
"prompt_mask": prompt_mask,
"completion_ids": completion_ids,
"completion_mask": completion_mask,
"advantages": advantages,
"num_items_in_batch": num_items_in_batch,
}
if old_per_token_logps is not None:
output["old_per_token_logps"] = old_per_token_logps
if False and self.use_vllm and self.vllm_importance_sampling_correction:
output["importance_sampling_ratio"] = vllm_importance_sampling_ratio
if sampling_per_token_logps is not None:
output["sampling_per_token_logps"] = sampling_per_token_logps
if ref_per_token_logps is not None:
output["ref_per_token_logps"] = ref_per_token_logps
if "pixel_values" in forward_kwargs:
output["pixel_values"] = forward_kwargs["pixel_values"]
if "image_grid_thw" in forward_kwargs:
output["image_grid_thw"] = forward_kwargs["image_grid_thw"]
if "pixel_attention_mask" in forward_kwargs:
output["pixel_attention_mask"] = forward_kwargs["pixel_attention_mask"]
if "spatial_shapes" in forward_kwargs:
output["spatial_shapes"] = forward_kwargs["spatial_shapes"]
if "image_sizes" in forward_kwargs:
output["image_sizes"] = forward_kwargs["image_sizes"]
if "token_type_ids" in forward_kwargs:
output["token_type_ids"] = forward_kwargs["token_type_ids"]
if "mm_token_type_ids" in forward_kwargs:
output["mm_token_type_ids"] = forward_kwargs["mm_token_type_ids"]
if "image_position_ids" in forward_kwargs:
output["image_position_ids"] = forward_kwargs["image_position_ids"]
if images is not None:
output["num_images"] = num_images
if max_left_pad is not None:
output["max_left_pad"] = torch.tensor(prompt_ids.shape[0] * [max_left_pad]).unsqueeze(-1)
try:
if self.use_vllm and getattr(self, "vllm_importance_sampling_correction", False):
output["sampling_per_token_logps"] = sampling_per_token_logps
except NameError:
output["sampling_per_token_logps"] = None
if num_tiles is not None:
output["num_tiles"] = num_tiles
if tool_mask is not None:
output["tool_mask"] = tool_mask
return output
def compute_liger_loss(self, unwrapped_model, inputs):
# Compute the per-token log probabilities for the model
prompt_ids, prompt_mask = inputs["prompt_ids"], inputs["prompt_mask"]
completion_ids, completion_mask = inputs["completion_ids"], inputs["completion_mask"]
input_ids = torch.cat([prompt_ids, completion_ids], dim=1)
attention_mask = torch.cat([prompt_mask, completion_mask], dim=1)
logits_to_keep = completion_ids.size(1) # we only need to compute the logits for the completion tokens
# Get the last hidden state of the model
last_hidden_state = self._get_last_hidden_state(
unwrapped_model,
input_ids,
attention_mask,
logits_to_keep,
inputs.get("pixel_values"),
inputs.get("image_grid_thw"),
inputs.get("pixel_attention_mask"),
inputs.get("spatial_shapes"),
inputs.get("image_sizes"),
inputs.get("image_position_ids"),
)
# Apply tool_mask (from env_mask) for loss computation in multi-turn training scenarios
loss_mask = completion_mask if "tool_mask" not in inputs else completion_mask * inputs["tool_mask"]
lm_head_weight = unwrapped_model.lm_head.weight
lm_head_bias = unwrapped_model.lm_head.bias
# Liger reads `lm_head` directly instead of through `model.forward()`, so its ZeRO-3 gather hook never fires
# and the fused matmul gets an empty shard. Gather the weight/bias ourselves for the call (the weight grad is
# computed during this forward, so it isn't needed in the backward). Skip it when already gathered: with tied
# embeddings `embed_tokens` keeps the weight `AVAILABLE`, and re-partitioning on exit breaks its tracking.
deepspeed_plugin = self.accelerator.state.deepspeed_plugin
gather_ctx = nullcontext()
if deepspeed_plugin is not None and deepspeed_plugin.zero_stage == 3:
from deepspeed.runtime.zero.partition_parameters import ZeroParamStatus
params = [lm_head_weight] if lm_head_bias is None else [lm_head_weight, lm_head_bias]
if any(p.ds_status != ZeroParamStatus.AVAILABLE for p in params):
import deepspeed
gather_ctx = deepspeed.zero.GatheredParameters(params, modifier_rank=None)
with gather_ctx:
loss, metrics = self.liger_grpo_loss(
_input=last_hidden_state,
lin_weight=lm_head_weight,
selected_token_ids=completion_ids,
# The attention_mask parameter in liger loss is actually used as a loss mask (not model attention)
attention_mask=loss_mask,
advantages=inputs["advantages"],
bias=lm_head_bias,
old_per_token_logps=inputs.get("old_per_token_logps"),
ref_per_token_logps=inputs.get("ref_per_token_logps"),
vllm_is_ratio=inputs.get("importance_sampling_ratio"),
)
# Extract metrics from the liger_grpo_loss output
# KL divergence is the first metric when beta is non-zero
mean_kl = metrics[0] if self.beta != 0.0 else None
clip_ratio = metrics[-1]
mode = "train" if self.model.training else "eval"
if self.beta != 0.0:
self._metrics[mode]["kl"].append(self.accelerator.gather(mean_kl).mean().item())
self._metrics[mode]["clip_ratio"].append(self.accelerator.gather(clip_ratio).mean().item())
normalizer = self.current_gradient_accumulation_steps if mode == "train" else 1.0 # no accum in eval
return loss / normalizer
def compute_loss(
self,
model,
inputs,
return_outputs = False,
num_items_in_batch = None,
):
if return_outputs:
raise ValueError("The GRPOTrainer does not support returning outputs")
# Compute the per-token log probabilities for the model
prompt_ids, prompt_mask = inputs["prompt_ids"], inputs["prompt_mask"]
completion_ids, completion_mask = (
inputs["completion_ids"],
inputs["completion_mask"],
)
pixel_values, image_grid_thw = (
inputs.get("pixel_values", None),
inputs.get("image_grid_thw", None),
)
pixel_attention_mask, image_sizes = (
inputs.get("pixel_attention_mask", None),
inputs.get("image_sizes", None),
)
num_images = inputs.get("num_images", None)
# Transformers 5.x needs token_type_ids/mm_token_type_ids for some vision models
token_type_ids = inputs.get("token_type_ids", None)
mm_token_type_ids = inputs.get("mm_token_type_ids", None)
num_items_in_batch = inputs.get("num_items_in_batch", None)
sampling_per_token_logps = inputs.get("sampling_per_token_logps", None)
tool_mask = inputs.get("tool_mask", None)
# Missing when evaluate() runs standalone; eval does not accumulate, so
# fall back to 1 to avoid underreporting eval_loss (#2464).
current_gradient_accumulation_steps = getattr(
self, "current_gradient_accumulation_steps", 1
)
num_processes = self.accelerator.num_processes
input_ids = torch.cat([prompt_ids, completion_ids], dim = 1)
bsz, qlen = input_ids.shape
attention_mask = torch.cat([prompt_mask, completion_mask], dim = 1)
if mm_token_type_ids is not None or image_grid_thw is not None:
mm_token_type_ids = _unsloth_fix_mm_token_type_ids(
self.processing_class,
input_ids,
mm_token_type_ids,
completion_ids = completion_ids,
)
# attention_mask = None
logits_to_keep = completion_ids.size(
1
) # we only need to compute the logits for the completion tokens
_input_ids = input_ids
_logits_to_keep = logits_to_keep
get_logps_func = (
lambda model,
input_ids,
attention_mask,
logits_to_keep,
batch_size = None,
compute_entropy = False,
compute_efficient = False: self._get_per_token_logps(
model, input_ids, attention_mask, logits_to_keep, compute_efficient
)
if hasattr(self, "_get_per_token_logps")
else self._get_per_token_logps_and_entropies(
model,
input_ids,
attention_mask,
logits_to_keep,
batch_size,
compute_entropy,
compute_efficient,
)[0]
) # logps
per_token_logps = get_logps_func(
model, input_ids, attention_mask, logits_to_keep, compute_efficient = True
)
# Compute the KL divergence between the model and the reference model
# _prepare_inputs doesn't return reference log probs anymore. We need to calculate it ourselves.
# https://github.com/huggingface/trl/blob/05bc43e960396581e458195b8388efe6b82cae1f/trl/trainer/grpo_trainer.py#L1328
# if self.beta != 0.0:
# with torch.inference_mode(), model.disable_adapter():
# ref_per_token_logps = per_token_logps = get_logps_func(model, input_ids, attention_mask, logits_to_keep)
# else:
# ref_per_token_logps = None
ref_logps = inputs.get("ref_per_token_logps", None)
# per_token_kl = torch.exp(ref_per_token_logps - per_token_logps) - (ref_per_token_logps - per_token_logps) - 1
# x - x.detach() allows for preserving gradients from x
advantages = inputs["advantages"]
# per_token_loss = torch.exp(per_token_logps - per_token_logps.detach()) * advantages.unsqueeze(1)
# per_token_loss = -(per_token_loss - self.beta * per_token_kl)
# loss = ((per_token_loss * completion_mask).sum(dim=1) / completion_mask.sum(dim=1)).mean()
old_logps = inputs.get("old_per_token_logps", None)
input_ids = input_ids[:, -logits_to_keep:]
# Get logit softcapping and logit scale
logit_softcapping = _unsloth_get_final_logit_softcapping(model.config) # Gemma
logit_scale_multiply = getattr(model.config, "logit_scale", 0) # Cohere
if logit_scale_multiply is None:
logit_scale_multiply = 0
logit_scale_divide = getattr(model.config, "logits_scaling", 0) # Granite
if logit_scale_divide is None:
logit_scale_divide = 0
max_left_pad = inputs.get("max_left_pad", 0)
if per_token_logps is not None:
loss_mask = completion_mask
if tool_mask is not None:
if tool_mask.shape != completion_mask.shape:
raise ValueError(
"tool_mask/env_mask must have the same shape as completion_mask"
)
loss_mask = completion_mask * tool_mask.to(
device = completion_mask.device,
dtype = completion_mask.dtype,
)
(
loss,
completion_length,
mean_kl,
delta,
flat_is_ratio,
coef_1,
completion_mask,
) = grpo_compute_loss_slow(
ref_logps,
per_token_logps,
old_logps,
sampling_per_token_logps,
input_ids,
loss_mask,
self.beta,
advantages,
pixel_values = pixel_values,
image_grid_thw = image_grid_thw,
loss_type = self.args.loss_type,
importance_sampling_level = self.importance_sampling_level,
epsilon_low = self.epsilon_low,
epsilon_high = self.epsilon_high,
max_completion_length = self.args.max_completion_length,
delta = self.args.delta,
temperature = self.args.temperature,
max_left_pad = max_left_pad,
logit_softcapping = logit_softcapping,
logit_scale_multiply = logit_scale_multiply,
logit_scale_divide = logit_scale_divide,
num_items_in_batch = num_items_in_batch,
current_gradient_accumulation_steps = current_gradient_accumulation_steps,
num_processes = num_processes,
)
else:
def _unsloth_requires_multi_image_zoo(value):
if value is None:
return False
if isinstance(value, torch.Tensor):
counts = value.detach().cpu().reshape(-1).tolist()
else:
counts = list(value)
return any(int(n) != 1 for n in counts)
if _unsloth_requires_multi_image_zoo(num_images) and not getattr(
self, "_unsloth_grpo_zoo_checked", False
):
_supports_num_images = (
"num_images" in inspect.signature(grpo_accumulated_loss).parameters
)
if not _supports_num_images:
try:
_zoo_src = inspect.getsource(grpo_accumulated_loss)
except (TypeError, OSError):
_zoo_src = ""
_supports_num_images = "num_images" in _zoo_src
if not _supports_num_images:
raise RuntimeError(
"Multi-image GRPO requires an unsloth_zoo build whose "
"grpo_accumulated_loss handles num_images. Please upgrade "
"unsloth_zoo (see https://github.com/unslothai/unsloth-zoo/pull/613)."
)
self._unsloth_grpo_zoo_checked = True
if tool_mask is not None and not getattr(
self, "_unsloth_grpo_tool_mask_zoo_checked", False
):
_supports_tool_mask = (
"tool_mask" in inspect.signature(grpo_accumulated_loss).parameters
)
if not _supports_tool_mask:
try:
_zoo_src = inspect.getsource(grpo_accumulated_loss)
except (TypeError, OSError):
_zoo_src = ""
_supports_tool_mask = "tool_mask" in _zoo_src
if not _supports_tool_mask:
raise RuntimeError(
"env_mask/tool_mask GRPO requires an unsloth_zoo build whose "
"grpo_accumulated_loss handles tool_mask. Please upgrade "
"unsloth_zoo."
)
self._unsloth_grpo_tool_mask_zoo_checked = True
_grpo_accumulated_loss_kwargs = {}
if tool_mask is not None:
_grpo_accumulated_loss_kwargs["tool_mask"] = tool_mask
if hasattr(self.args, "loss_type"):
(
loss,
completion_length,
mean_kl,
delta,
flat_is_ratio,
coef_1,
completion_mask,
) = grpo_accumulated_loss(
trainer = self,
input_ids = _input_ids,
pixel_values = pixel_values,
image_grid_thw = image_grid_thw,
pixel_attention_mask = pixel_attention_mask,
image_sizes = image_sizes,
num_images = num_images,
logits_to_keep = logits_to_keep,
completion_mask = completion_mask,
advantages = advantages,
old_logps = old_logps,
ref_logps = ref_logps,
n_chunks = self.args.unsloth_num_chunks,
loss_type = self.args.loss_type,
importance_sampling_level = self.importance_sampling_level,
epsilon_low = self.epsilon_low,
epsilon_high = self.epsilon_high,
max_completion_length = self.args.max_completion_length,
delta = self.args.delta,
temperature = self.args.temperature,
max_left_pad = max_left_pad,
logit_softcapping = logit_softcapping,
logit_scale_multiply = logit_scale_multiply,
logit_scale_divide = logit_scale_divide,
attention_mask = attention_mask,
num_items_in_batch = num_items_in_batch,
current_gradient_accumulation_steps = current_gradient_accumulation_steps,
num_processes = num_processes,
sampling_per_token_logps = sampling_per_token_logps,
token_type_ids = token_type_ids,
mm_token_type_ids = mm_token_type_ids,
**_grpo_accumulated_loss_kwargs,
)
else:
# to ensure backwards compatibility with trl 0.15.2 and maybe even 0.17
loss, completion_length, mean_kl, coef_1, completion_mask = grpo_accumulated_loss(
trainer = self,
input_ids = _input_ids,
pixel_values = pixel_values,
image_grid_thw = image_grid_thw,
pixel_attention_mask = pixel_attention_mask,
image_sizes = image_sizes,
num_images = num_images,
logits_to_keep = logits_to_keep,
completion_mask = completion_mask,
advantages = advantages,
old_logps = old_logps,
ref_logps = ref_logps,
n_chunks = self.args.unsloth_num_chunks,
temperature = self.args.temperature,
logit_softcapping = logit_softcapping,
logit_scale_multiply = logit_scale_multiply,
logit_scale_divide = logit_scale_divide,
attention_mask = attention_mask,
token_type_ids = token_type_ids,
mm_token_type_ids = mm_token_type_ids,
**_grpo_accumulated_loss_kwargs,
)
if "train" in self._metrics:
mode = "eval" if self.control.should_evaluate else "train"
self._metrics[mode]["completion_length"].append(completion_length.item())
self._metrics[mode]["kl"].append(mean_kl.item())
else:
self._metrics["completion_length"].append(completion_length.item())
self._metrics["kl"].append(mean_kl.item())
if (
self.use_vllm
and delta is not None
and getattr(self, "vllm_importance_sampling_correction", False)
):
mean_delta = (
torch.mean(delta)
if delta.numel() > 0
else torch.tensor(0.0, device = self.model.device)
)
max_delta = (
torch.max(delta)
if delta.numel() > 0
else torch.tensor(0.0, device = self.model.device)
)
self._metrics[mode]["sampling/sampling_logp_difference/mean"].append(
self.accelerator.gather(mean_delta).mean().item()
)
self._metrics[mode]["sampling/sampling_logp_difference/max"].append(
self.accelerator.gather(max_delta).max().item()
)
min_importance_sampling_ratio = (
torch.min(flat_is_ratio)
if flat_is_ratio.numel() > 0
else torch.tensor(0.0, device = self.model.device)
)
mean_importance_sampling_ratio = (
torch.mean(flat_is_ratio)
if flat_is_ratio.numel() > 0
else torch.tensor(0.0, device = self.model.device)
)
max_importance_sampling_ratio = (
torch.max(flat_is_ratio)
if flat_is_ratio.numel() > 0
else torch.tensor(0.0, device = self.model.device)
)
self._metrics[mode]["sampling/importance_sampling_ratio/min"].append(
self.accelerator.gather(min_importance_sampling_ratio)
.nan_to_num(nan = float("inf"))
.min()
.item()
)
self._metrics[mode]["sampling/importance_sampling_ratio/mean"].append(
self.accelerator.gather(mean_importance_sampling_ratio).nanmean().item()
)
self._metrics[mode]["sampling/importance_sampling_ratio/max"].append(
self.accelerator.gather(max_importance_sampling_ratio)
.nan_to_num(nan = float("-inf"))
.max()
.item()
)
completion_token_count = completion_mask.sum().clamp(min = 1.0)
def masked_batch_mean(x):
if x.shape[1] == 1: # when importance_sampling_level == "sequence"
return x.mean()
else:
return (x * completion_mask).sum() / completion_token_count
if advantages.dim() == 1:
advantages = advantages.unsqueeze(1)
if self.loss_type in ["grpo", "bnpo", "dr_grpo", "dapo"]:
# Compute the clipped probability ratios
is_low_clipped = (coef_1 < 1 - self.epsilon_low) & (advantages < 0)
is_high_clipped = (coef_1 > 1 + self.epsilon_high) & (advantages > 0)
is_region_clipped = is_low_clipped | is_high_clipped
low_clip = masked_batch_mean(is_low_clipped.float())
high_clip = masked_batch_mean(is_high_clipped.float())
clip_ratio = masked_batch_mean(is_region_clipped.float())
gathered_low_clip = self.accelerator.gather(low_clip)
self._metrics[mode]["clip_ratio/low_mean"].append(gathered_low_clip.nanmean().item())
self._metrics[mode]["clip_ratio/low_min"].append(nanmin(gathered_low_clip).item())
gathered_high_clip = self.accelerator.gather(high_clip)
self._metrics[mode]["clip_ratio/high_mean"].append(gathered_high_clip.nanmean().item())
self._metrics[mode]["clip_ratio/high_max"].append(nanmax(gathered_high_clip).item())
gathered_clip_ratio = self.accelerator.gather(clip_ratio)
self._metrics[mode]["clip_ratio/region_mean"].append(
gathered_clip_ratio.nanmean().item()
)
elif self.loss_type == "cispo":
is_cispo_clipped = (coef_1 > self.epsilon_high) & (advantages > 0)
cispo_clip_ratio = masked_batch_mean(is_cispo_clipped.float())
gathered_cispo_clip_ratio = self.accelerator.gather(cispo_clip_ratio)
self._metrics[mode]["cispo_clip_ratio"].append(
gathered_cispo_clip_ratio.nanmean().item()
)
return loss
@staticmethod
def get_off_policy_mask(
advantages: torch.Tensor,
per_token_logps: torch.Tensor,
sampling_per_token_logps: torch.Tensor,
mask: torch.Tensor,
off_policy_threshold: float,
) -> torch.Tensor:
"""
Computes the Off-Policy Sequence Mask from DeepSeek-V3.2 paper. Returns a (B, 1) tensor where 1.0 indicates
"Keep" and 0.0 indicates "Drop".
"""
# forward KL div: log(pi_old) - log(pi_theta)
kl_div = sampling_per_token_logps - per_token_logps.detach()
# Sequence-level Mean KL (ignoring prompt+padding)
seq_kl_sum = (kl_div * mask).sum(dim=1, keepdim=True)
avg_seq_kl = seq_kl_sum / mask.sum(dim=1, keepdim=True).clamp(min=1.0)
# Keep if (Advantage >= 0) OR (KL <= delta)
is_pos_adv = advantages >= 0
is_low_kl = avg_seq_kl <= off_policy_threshold
return (is_pos_adv | is_low_kl).to(dtype=mask.dtype) # (B, 1)
@staticmethod
@torch.no_grad()
def get_gamma_weights(
advantages: torch.Tensor,
log_ratio_per_token: torch.Tensor,
mask: torch.Tensor,
importance_sampling_ratio: torch.Tensor | None, # (B, T)
k_pos: float = 2.0,
lambda_pos: float = 3.0,
k_neg: float = 3.0,
lambda_neg: float = 2.0,
) -> torch.Tensor:
"""
Computes the Gamma weights for the VESPO loss. For reference:
φ(w) = e^λ × w^k × e^{-λw} is the gamma weighting (normalized so φ(1)=1)
with w = sequence-level importance sampling ratio
note: we will compute φ(w) in log space
φ(w) is detached via @torch.no_grad(), only acts as gradient scaling coefficient
VESPO loss = -φ(w) × A × log_prob, gradient naturally gives φ(w) × A × ∇log π
"""
# reducing clamp range directly to log(1e-8) ~ -18.42, to avoid recomputing log_w=log(w.clamp(min=1e-8)) later
# This is solely for matching truthfully the original implementation, otherwise keeping -20 could be fine.
lower_clamp = math.log(1e-8)
# Sequence-level log ratio Σ log(π_θ/π_old) (not a mean like for `log_importance_weights`)
log_ratio_clamped = torch.clamp(log_ratio_per_token, -20.0, 20.0)
seq_log_ratio = torch.sum(log_ratio_clamped * mask, dim=-1, keepdim=True) # (B, 1)
# Apply token-level TIS or MIS correction (in log space)
if importance_sampling_ratio is not None:
log_is_ratio = torch.clamp(torch.log(importance_sampling_ratio), lower_clamp, 20.0)
# log(w) = log(π_θ/π_old) + log(π_old/π_sampler)
seq_log_ratio += torch.sum(log_is_ratio, dim=-1, keepdim=True)
log_w_seq = torch.clamp(seq_log_ratio, lower_clamp, 20.0)
w_seq = torch.exp(log_w_seq)
# compute k and lambda based on advantage sign
is_nonneg_adv = advantages >= 0
k_seq = torch.where(is_nonneg_adv, k_pos, k_neg)
lambda_seq = torch.where(is_nonneg_adv, lambda_pos, lambda_neg).clamp(min=1e-4)
# log(φ(w)) = λ + k × log(w) - λ × w
log_phi = lambda_seq + k_seq * log_w_seq - lambda_seq * w_seq
phi_seq = torch.exp(log_phi).nan_to_num(nan=0.0, posinf=0.0, neginf=0.0)
return phi_seq # (B, 1)
def _compute_loss(self, model, inputs):
# Compute the per-token log probabilities for the model
prompt_ids, prompt_mask = inputs["prompt_ids"], inputs["prompt_mask"]
completion_ids, completion_mask = inputs["completion_ids"], inputs["completion_mask"]
input_ids = torch.cat([prompt_ids, completion_ids], dim=1)
attention_mask = torch.cat([prompt_mask, completion_mask], dim=1)
logits_to_keep = completion_ids.size(1) # we only need to compute the logits for the completion tokens
mask = completion_mask if "tool_mask" not in inputs else completion_mask * inputs["tool_mask"]
# Compute the per_token_logps and the entropy at each position in the completion
per_token_logps, entropies, aux_loss = self._get_per_token_logps_and_entropies(
model,
input_ids,
attention_mask,
logits_to_keep,
compute_entropy=True,
compute_aux_loss=self.aux_loss_enabled,
pixel_values=inputs.get("pixel_values"),
image_grid_thw=inputs.get("image_grid_thw"),
num_images=inputs.get("num_images"),
pixel_attention_mask=inputs.get("pixel_attention_mask"),
spatial_shapes=inputs.get("spatial_shapes"),
num_tiles=inputs.get("num_tiles"),
image_sizes=inputs.get("image_sizes"),
token_type_ids=inputs.get("token_type_ids"),
mm_token_type_ids=inputs.get("mm_token_type_ids"),
image_position_ids=inputs.get("image_position_ids"),
)
if self.top_entropy_quantile < 1.0:
entropy_mask = self.get_high_entropy_mask(entropies, mask, 1 - self.top_entropy_quantile)
else:
entropy_mask = None
# Compute the loss
advantages = inputs["advantages"]
# In the base GRPO implementation, advantages are expected to have shape (B,). To support subclasses that
# provide advantages with shape (B, T) (e.g., MiniLLM), we *conditionally* unsqueeze the tensor.
if advantages.dim() == 1:
advantages = advantages.unsqueeze(1)
# When num_iterations == 1 and steps_per_generation <= gradient_accumulation_steps,
# old_per_token_logps == per_token_logps. In this case we can skip its computation
# (see _generate_and_score_completions) and instead use per_token_logps.detach().
# The exception is when using vLLM, where we always compute old_per_token_logps
# for importance sampling
old_per_token_logps = inputs.get("old_per_token_logps")
old_per_token_logps = per_token_logps.detach() if old_per_token_logps is None else old_per_token_logps
if self.off_policy_mask_threshold is not None:
# OPSM should use inference-time logprobs to detect both sources of off-policyness:
# 1. Drift from gradient updates (always present)
# 2. Drift from training-inference mismatch (when using vLLM)
# When using vLLM, prioritize sampling_per_token_logps, otherwise use old_per_token_logps
sampling_per_token_logps = inputs.get("sampling_per_token_logps", old_per_token_logps)
off_policy_mask = self.get_off_policy_mask(
advantages=advantages,
per_token_logps=per_token_logps,
sampling_per_token_logps=sampling_per_token_logps,
mask=mask,
off_policy_threshold=self.off_policy_mask_threshold,
)
log_ratio = per_token_logps - old_per_token_logps
if self.importance_sampling_level == "token":
log_importance_weights = log_ratio
elif self.importance_sampling_level == "sequence":
log_importance_weights = (log_ratio * mask).sum(-1) / mask.sum(-1).clamp(min=1.0)
log_importance_weights = log_importance_weights.unsqueeze(-1)
else:
raise ValueError(
f"Unknown importance sampling level: {self.importance_sampling_level}. Possible values are 'token' "
"and 'sequence'."
)
coef_1 = torch.exp(log_importance_weights)
# Compute the KL divergence between the model and the reference model
if self.beta != 0.0:
ref_per_token_logps = inputs["ref_per_token_logps"]
per_token_kl = (
torch.exp(ref_per_token_logps - per_token_logps) - (ref_per_token_logps - per_token_logps) - 1
)
# Importance sampling correction for the KL divergence
if self.args.use_bias_correction_kl:
per_token_kl = per_token_kl * coef_1
# From here, log_importance_weights (and all subsequent tensors, coef_1, coef_2, etc.) shape depends on
# importance_sampling_level: "token" level: (B, T); "sequence" level: (B, 1)
if self.loss_type == "cispo":
clamped_ratios = torch.clamp(coef_1, max=self.epsilon_high).detach()
per_token_loss = -clamped_ratios * advantages * per_token_logps
elif self.loss_type in ["grpo", "bnpo", "dr_grpo", "dapo", "luspo"]:
coef_2 = torch.clamp(coef_1, 1 - self.epsilon_low, 1 + self.epsilon_high)
# Two-sided clipping
if self.args.delta is not None:
coef_1 = torch.clamp(coef_1, max=self.args.delta)
per_token_loss1 = coef_1 * advantages
per_token_loss2 = coef_2 * advantages
per_token_loss = -torch.min(per_token_loss1, per_token_loss2)
elif self.loss_type == "sapo":
temperatures = torch.where(advantages > 0, self.args.sapo_temperature_pos, self.args.sapo_temperature_neg)
soft_coef_1 = torch.sigmoid(temperatures * (coef_1 - 1)) * 4 / temperatures
per_token_loss = -soft_coef_1 * advantages
elif self.loss_type == "vespo":
phi_seq = self.get_gamma_weights(
advantages=advantages,
log_ratio_per_token=log_ratio,
mask=mask,
importance_sampling_ratio=inputs.get("importance_sampling_ratio"),
k_pos=self.args.vespo_k_pos,
lambda_pos=self.args.vespo_lambda_pos,
k_neg=self.args.vespo_k_neg,
lambda_neg=self.args.vespo_lambda_neg,
)
per_token_loss = -phi_seq * advantages * per_token_logps
else:
raise ValueError(f"Unknown loss type: {self.loss_type}")
if self.off_policy_mask_threshold is not None:
per_token_loss = per_token_loss * off_policy_mask
if entropy_mask is not None:
per_token_loss = per_token_loss * entropy_mask
if self.use_vllm and self.vllm_importance_sampling_correction and self.loss_type != "vespo":
per_token_loss = per_token_loss * inputs["importance_sampling_ratio"]
if self.beta != 0.0:
per_token_loss = per_token_loss + self.beta * per_token_kl
mode = "train" if self.model.training else "eval"
if self.loss_type in ["grpo", "sapo"]:
loss = ((per_token_loss * mask).sum(-1) / mask.sum(-1).clamp(min=1.0)).mean()
normalizer = self.current_gradient_accumulation_steps if mode == "train" else 1.0 # no accum in eval
loss = loss / normalizer
elif self.loss_type == "bnpo":
loss = (per_token_loss * mask).sum() / mask.sum().clamp(min=1.0)
normalizer = self.current_gradient_accumulation_steps if mode == "train" else 1.0 # no accum in eval
loss = loss / normalizer
elif self.loss_type == "dr_grpo":
loss = (per_token_loss * mask).sum() / (per_token_loss.size(0) * self.max_completion_length)
normalizer = self.current_gradient_accumulation_steps if mode == "train" else 1.0 # no accum in eval
loss = loss / normalizer
elif self.loss_type in ["cispo", "dapo", "vespo"]:
normalizer = inputs["num_items_in_batch"] / self.accelerator.num_processes
loss = (per_token_loss * mask).sum() / normalizer
elif self.loss_type == "luspo":
# Unless importance_sampling_level="token" (not recommended here), per_token_loss is expected to be (B, 1)
loss = (per_token_loss * mask.sum(1, keepdim=True)).mean()
normalizer = self.current_gradient_accumulation_steps if mode == "train" else 1.0
loss = loss / normalizer
else:
raise ValueError(f"Unknown loss type: {self.loss_type}")
# The policy loss above is scaled for gradient accumulation (HF auto-scaling is off here), so scale aux too
if self.aux_loss_enabled:
normalizer = self.current_gradient_accumulation_steps if mode == "train" else 1.0
loss = loss + self.router_aux_loss_coef * aux_loss / normalizer
self._metrics[mode]["aux_loss"].append(self.accelerator.gather_for_metrics(aux_loss).mean().item())
# Log the metrics
completion_token_count = mask.sum().clamp(min=1.0)
def masked_batch_mean(x):
if x.shape[1] == 1: # when importance_sampling_level == "sequence"
return x.mean()
else:
return (x * mask).sum() / completion_token_count
if self.beta != 0.0:
mean_kl = masked_batch_mean(per_token_kl)
self._metrics[mode]["kl"].append(self.accelerator.gather(mean_kl).nanmean().item())
mean_entropy = masked_batch_mean(entropies)
self._metrics[mode]["entropy"].append(self.accelerator.gather(mean_entropy).nanmean().item())
if self.loss_type in ["grpo", "bnpo", "dr_grpo", "dapo", "luspo"]:
# Compute the clipped probability ratios
is_low_clipped = (coef_1 < 1 - self.epsilon_low) & (advantages < 0)
is_high_clipped = (coef_1 > 1 + self.epsilon_high) & (advantages > 0)
is_region_clipped = is_low_clipped | is_high_clipped
low_clip = masked_batch_mean(is_low_clipped.float())
high_clip = masked_batch_mean(is_high_clipped.float())
clip_ratio = masked_batch_mean(is_region_clipped.float())
gathered_low_clip = self.accelerator.gather(low_clip)
self._metrics[mode]["clip_ratio/low_mean"].append(gathered_low_clip.nanmean().item())
self._metrics[mode]["clip_ratio/low_min"].append(nanmin(gathered_low_clip).item())
gathered_high_clip = self.accelerator.gather(high_clip)
self._metrics[mode]["clip_ratio/high_mean"].append(gathered_high_clip.nanmean().item())
self._metrics[mode]["clip_ratio/high_max"].append(nanmax(gathered_high_clip).item())
gathered_clip_ratio = self.accelerator.gather(clip_ratio)
self._metrics[mode]["clip_ratio/region_mean"].append(gathered_clip_ratio.nanmean().item())
elif self.loss_type == "cispo":
is_cispo_clipped = (coef_1 > self.epsilon_high) & (advantages > 0)
cispo_clip_ratio = masked_batch_mean(is_cispo_clipped.float())
gathered_cispo_clip_ratio = self.accelerator.gather(cispo_clip_ratio)
self._metrics[mode]["cispo_clip_ratio"].append(gathered_cispo_clip_ratio.nanmean().item())
elif self.loss_type == "vespo":
gathered_phi_seq = self.accelerator.gather(phi_seq)
self._metrics[mode]["vespo/phi_seq_mean"].append(gathered_phi_seq.nanmean().item())
return loss
# During eval, Trainer calls prediction_step. If no labels are present in the inputs, it only runs forward and
# returns logits. We override prediction_step to force compute_loss, because this trainer doesn't involve labels.
def prediction_step(self, model, inputs, prediction_loss_only, ignore_keys: list[str] | None = None):
inputs = self._prepare_inputs(inputs)
with torch.no_grad():
with self.compute_loss_context_manager():
loss = self.compute_loss(model, inputs)
loss = loss.mean().detach()
return loss, None, None
def log(self, logs: dict[str, float], start_time: float | None = None) -> None:
mode = "train" if self.model.training else "eval"
# Average the metrics
metrics = {}
for key, val in self._metrics[mode].items():
# Filter out NaN values before averaging. A reward function that returns None for all samples
# in a batch produces NaN for that batch's metric. With logging_steps > 1, a naive sum()/len()
# would let a single NaN contaminate valid data from other batches. Only return None when no
# valid values remain (e.g. JSON loggers crash on float NaN).
valid = [v for v in val if not math.isnan(v)]
metrics[key] = sum(valid) / len(valid) if valid else None
# This method can be called both in training and evaluation. When called in evaluation, the keys in `logs`
# start with "eval_". We need to add the prefix "eval_" to the keys in `metrics` to match the format.
if mode == "eval":
metrics = {f"eval_{key}": val for key, val in metrics.items()}
logs.update(metrics)
super().log(logs, start_time)
self._metrics[mode].clear()
if self.accelerator.is_main_process and self.log_completions:
if is_rich_available():
print_prompt_completions_sample(
self._logs["prompt"],
self._logs["completion"],
self._logs["rewards"],
self._logs["advantages"],
self.state.global_step,
self.num_completions_to_print,
extra=dict(self._logs["extra"]),
)
logging_backends = []
if self.args.report_to and "wandb" in self.args.report_to and wandb.run is not None:
logging_backends.append(wandb)
if self.args.report_to and "trackio" in self.args.report_to:
logging_backends.append(trackio)
table = {
"step": [self.state.global_step] * len(self._logs["prompt"]),
"prompt": self._logs["prompt"],
"completion": self._logs["completion"],
**self._logs["rewards"],
**self._logs["extra"],
"advantage": self._logs["advantages"],
}
df_base = pd.DataFrame(table)
df_base.to_parquet(
os.path.join(
self.args.output_dir,
"completions",
f"completions_{self.state.global_step:05d}.parquet",
)
)
images_raw = self._logs["images"] or []
for logging_backend in logging_backends:
if images_raw:
images = []
for image_list in self._logs["images"]:
if image_list:
images.append([logging_backend.Image(image) for image in image_list])
else:
images.append([])
df = pd.concat(
[df_base, pd.Series(images, name="image")],
axis=1,
copy=False,
)
else:
df = df_base
if self.log_unique_prompts:
df = df.drop_duplicates(subset=["prompt"])
logging_backend.log({"completions": logging_backend.Table(dataframe=df)})
# Ensure the model card is saved along with the checkpoint
def _save_checkpoint(self, model, trial):
if self.args.hub_model_id is None:
model_name = Path(self.args.output_dir).name
else:
model_name = self.args.hub_model_id.split("/")[-1]
self.create_model_card(model_name=model_name)
super()._save_checkpoint(model, trial)
class UnslothGRPOTrainer(_UnslothGRPOTrainer):
"""
Trainer for the Group Relative Policy Optimization (GRPO) method. This algorithm was initially proposed in the
paper [DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language
Models](https://huggingface.co/papers/2402.03300).
Example:
```python
>>> from trl import GRPOTrainer
>>> from trl.rewards import accuracy_reward
>>> from datasets import load_dataset
>>> dataset = load_dataset("trl-lib/DeepMath-103K", split="train")
>>> trainer = GRPOTrainer(
... model="Qwen/Qwen2.5-0.5B-Instruct",
... reward_funcs=accuracy_reward,
... train_dataset=dataset,
... )
>>> trainer.train()
```
Args:
model (`str` or [`~transformers.PreTrainedModel`] or [`~peft.PeftModel`]):
Model to be trained. Can be either:
- A string, being the *model id* of a pretrained model hosted inside a model repo on huggingface.co, or a
path to a *directory* containing model weights saved using
[`~transformers.PreTrainedModel.save_pretrained`], e.g., `'./my_model_directory/'`. The model is loaded
using `<ModelArchitecture>.from_pretrained` (where `<ModelArchitecture>` is derived from the model
config) with the keyword arguments in `args.model_init_kwargs`.
- A [`~transformers.PreTrainedModel`] object. Only causal language models are supported.
- A [`~peft.PeftModel`] object. Only causal language models are supported.
reward_funcs (`RewardFunc | list[RewardFunc]`):
Reward functions to be used for computing the rewards. To compute the rewards, we call all the reward
functions with the prompts and completions and sum the rewards. Can be either:
- A single reward function, such as:
- A string: The *model ID* of a pretrained model hosted inside a model repo on huggingface.co, or a
path to a *directory* containing model weights saved using
[`~transformers.PreTrainedModel.save_pretrained`], e.g., `'./my_model_directory/'`. The model is loaded
using [`~transformers.AutoModelForSequenceClassification.from_pretrained`] with `num_labels=1` and the
keyword arguments in `args.model_init_kwargs`.
- A [`~transformers.PreTrainedModel`] object: Only sequence classification models are supported.
- A custom reward function: The function is provided with the prompts and the generated completions,
plus any additional columns in the dataset. It should return a list of rewards. Custom reward
functions can be either synchronous or asynchronous and can also return `None` when the reward is
not applicable to those samples. This is useful for multi-task training where different reward
functions apply to different types of samples. When a reward function returns `None` for a sample,
that reward function is excluded from the reward calculation for that sample. For more details, see
[Using a custom reward
function](#using-a-custom-reward-function).
The trainer's state is also passed to the reward function. The trainer's state is an instance of
[`~transformers.TrainerState`] and can be accessed by accessing the `trainer_state` argument to the
reward function's signature.
- A list of reward functions, where each item can independently be any of the above types. Mixing different
types within the list (e.g., a string model ID and a custom reward function) is allowed.
args ([`GRPOConfig`], *optional*):
Configuration for this trainer. If `None`, a default configuration is used.
train_dataset ([`~datasets.Dataset`] or [`~datasets.IterableDataset`]):
Dataset to use for training. It must include a column `"prompt"`. Any additional columns in the dataset is
ignored. The format of the samples can be either:
- [Standard](dataset_formats#standard): Each sample contains plain text.
- [Conversational](dataset_formats#conversational): Each sample contains structured messages (e.g., role
and content).
eval_dataset ([`~datasets.Dataset`], [`~datasets.IterableDataset`] or `dict[str, Dataset | IterableDataset]`):
Dataset to use for evaluation. It must meet the same requirements as `train_dataset`.
processing_class ([`~transformers.PreTrainedTokenizerBase`], [`~transformers.ProcessorMixin`], *optional*):
Processing class used to process the data. The padding side must be set to "left". If `None`, the
processing class is loaded from the model's name with [`~transformers.AutoProcessor.from_pretrained`]. A
padding token, `tokenizer.pad_token`, must be set. If the processing class has not set a padding token,
`tokenizer.eos_token` will be used as the default.
reward_processing_classes ([`~transformers.PreTrainedTokenizerBase`] or `list[PreTrainedTokenizerBase]`, *optional*):
Processing classes corresponding to the reward functions specified in `reward_funcs`. Can be either:
- A single processing class: Used when `reward_funcs` contains only one reward function.
- A list of processing classes: Must match the order and length of the reward functions in `reward_funcs`.
If set to `None`, or if an element of the list corresponding to a [`~transformers.PreTrainedModel`] is
`None`, the tokenizer for the model is automatically loaded using
[`~transformers.AutoTokenizer.from_pretrained`]. For elements in `reward_funcs` that are custom reward
functions (not [`~transformers.PreTrainedModel`]), the corresponding entries in `reward_processing_classes`
are ignored.
callbacks (list of [`~transformers.TrainerCallback`], *optional*):
List of callbacks to customize the training loop. Will add those to the list of default callbacks detailed
in [here](https://huggingface.co/docs/transformers/main_classes/callback).
If you want to remove one of the default callbacks used, use the [`~transformers.Trainer.remove_callback`]
method.
optimizers (`tuple[torch.optim.Optimizer | None, torch.optim.lr_scheduler.LambdaLR | None]`, *optional*, defaults to `(None, None)`):
A tuple containing the optimizer and the scheduler to use. Will default to an instance of `AdamW` on your
model and a scheduler given by [`~transformers.get_linear_schedule_with_warmup`] controlled by `args`.
peft_config ([`~peft.PeftConfig`], *optional*):
PEFT configuration used to wrap the model. If `None`, the model is not wrapped.
tools (list of `Callable`, *optional*):
A list of callable tool functions (sync or async) that the model can invoke during generation. Each tool
should be a standard Python function with properly type-hinted arguments and return values, and a
Google-style docstring describing its purpose, arguments, and return value. For more details, see:
https://huggingface.co/docs/transformers/en/chat_extras#passing-tools. The model uses the function's name,
type hints, and docstring to determine how to call it. Ensure that the model's chat template supports tool
use and that it has been fine-tuned for tool calling.
rollout_func (`RolloutFunc`, *optional*):
Function to use for generating completions. It receives the list of prompts allocated to the current
process and the trainer instance. It must return a dict with `"prompt_ids"`, `"completion_ids"`, and
`"logprobs"` fields, and can optionally return `"logprob_token_ids"` (same shape as `"logprobs"`). Any
other fields are forwarded to the reward functions. The function receives the raw per-process prompt slice
with no duplication; it is responsible for returning the correct number of completions per prompt (see
`num_generations` / `num_generations_eval` on the trainer). This feature is experimental and may change or
be removed at any time without prior notice.
environment_factory (`EnvironmentFactory`, *optional*):
A callable that creates and returns an environment instance. The environment class should define methods
that can be invoked as tools during generation. Each method should comply with the same requirements as the
`tools` described above. If `environment_factory` is provided, an instance of the environment is created
for each generation in the batch, allowing for parallel and independent interactions. The environment must
also implement a callable `reset` method that can be used to reset state between generations. The `reset`
method should return either `None` or a string: when it returns a string, that string is appended to the
last user message before generation. This feature is experimental and may change or be removed at any time
without prior notice.
"""
def __init__(
self,
model,
reward_funcs,
args = None,
train_dataset = None,
eval_dataset = None,
processing_class = None,
reward_processing_classes = None,
callbacks = None,
peft_config = None,
tools = None,
rollout_func = None,
environment_factory = None,
**kwargs
):
if args is None: args = UnslothGRPOConfig()
use_bf16 = getattr(args, 'bf16', False)
if type(use_bf16) is not bool: use_bf16 = False
use_fp16 = getattr(args, 'fp16', False)
if type(use_fp16) is not bool: use_fp16 = False
force_float32 = False
full_finetuning = os.environ.get('UNSLOTH_ENABLE_FULL_FINETUNING', '0') == '1'
if not full_finetuning and (os.environ.get('UNSLOTH_FORCE_FLOAT32', '0') == '1'):
print('Unsloth: Switching to float32 training since model cannot work with float16')
force_float32 = True
mixed_precision_dtype = os.environ.get('UNSLOTH_MIXED_PRECISION', 'float32')
dtype = getattr(model.config, 'dtype', None) or getattr(model.config, 'torch_dtype', None)
if dtype is None: dtype = model.get_input_embeddings().weight.dtype
from unsloth_zoo.utils import _get_dtype
dtype = _get_dtype(dtype)
float16 = dtype == torch.float16
if not force_float32 and (float16 and use_bf16): raise TypeError('Unsloth: Model is in float16 precision but you want to use bfloat16 precision. Set fp16 to `True` and bf16 to `False`')
if not force_float32 and (not float16 and use_fp16): raise TypeError('Unsloth: Model is in bfloat16 precision but you want to use float16 precision. Set fp16 to `False` and bf16 to `True`')
if force_float32:
# Forced float32 training
args.fp16 = False
args.bf16 = False
os.environ['ACCELERATE_MIXED_PRECISION'] = 'no'
if hasattr(args, 'mixed_precision'): args.mixed_precision = 'no'
# args.mixed_precision is a new argument which needs to be set now
elif (not use_bf16 and not use_fp16) and mixed_precision_dtype == 'float32':
# Mixed precision training
args.fp16 = float16
args.bf16 = not float16
os.environ['ACCELERATE_MIXED_PRECISION'] = 'fp16' if float16 else 'bf16'
if hasattr(args, 'mixed_precision'): args.mixed_precision = 'fp16' if float16 else 'bf16'
# args.mixed_precision is a new argument which needs to be set now
elif mixed_precision_dtype == 'bfloat16':
# Both False since bfloat16 full finetuning doesn't do any autocasting.
args.fp16 = False
args.bf16 = False
os.environ['ACCELERATE_MIXED_PRECISION'] = 'no'
if hasattr(args, 'mixed_precision'): args.mixed_precision = 'no'
# args.mixed_precision is a new argument which needs to be set now
if getattr(args, 'eval_dataset', None) is not None and getattr(args, 'eval_strategy', 'no') == 'no':
args.eval_strategy = 'steps'
if getattr(args, 'eval_steps', None) is None: args.eval_steps = 0.1
ga_steps = getattr(args, 'gradient_accumulation_steps', None)
if ga_steps is not None and ga_steps > 1:
from transformers import __version__ as transformers_version
if Version(transformers_version) <= Version('4.45.2'):
print('**** Unsloth: Please use our fixed gradient_accumulation_steps by updating transformers, TRL and Unsloth!\n'
'`pip install --upgrade --no-cache-dir --force-reinstall --no-deps unsloth transformers trl unsloth_zoo`')
if getattr(args, 'eval_strategy', 'no') != 'no':
eval_bsz = getattr(args, 'per_device_eval_batch_size', 8)
if eval_bsz == 8 and args.per_device_train_batch_size < eval_bsz: args.per_device_eval_batch_size = args.per_device_train_batch_size
if getattr(args, 'eval_accumulation_steps', None) is None and ga_steps is not None: args.eval_accumulation_steps = ga_steps
fp16_full_eval = getattr(args, 'fp16_full_eval', False)
if type(fp16_full_eval) is not bool: fp16_full_eval = False
bf16_full_eval = getattr(args, 'bf16_full_eval', False)
if type(bf16_full_eval) is not bool: bf16_full_eval = False
if args.fp16 and bf16_full_eval: args.bf16_full_eval = False; args.fp16_full_eval = True
if args.bf16 and fp16_full_eval: args.bf16_full_eval = True; args.fp16_full_eval = False
if force_float32:
args.bf16_full_eval = False
args.fp16_full_eval = False
elif os.environ.get('UNSLOTH_MIXED_PRECISION', 'float32') == 'bfloat16':
args.bf16_full_eval = True
args.fp16_full_eval = False
elif not bf16_full_eval and not fp16_full_eval:
args.bf16_full_eval = args.bf16
args.fp16_full_eval = args.fp16
_output_logits = False
if locals().get('compute_metrics', None) is not None: _output_logits = True
if locals().get('preprocess_logits_for_metrics', None) is not None: _output_logits = True
if _output_logits:
os.environ['UNSLOTH_RETURN_LOGITS'] = '1'
if model is not None:
_warnings_issued = getattr(model, 'warnings_issued', None)
if _warnings_issued is None:
model.warnings_issued = {}
elif not isinstance(_warnings_issued, dict):
try:
model.warnings_issued = dict(_warnings_issued)
except Exception:
model.warnings_issued = {}
if 'max_seq_length' not in locals() and not hasattr(args, 'max_seq_length'):
pass
else:
model_max_seq_length = getattr(model, 'max_seq_length', None)
args_max_seq_length = getattr(args, 'max_seq_length', None)
if args_max_seq_length is None and model_max_seq_length is not None:
max_seq_length = model.max_seq_length
if hasattr(args, 'max_seq_length'): args.max_seq_length = max_seq_length
elif args_max_seq_length is not None and model_max_seq_length is not None:
if args_max_seq_length > model_max_seq_length:
print('Unsloth: You set `max_seq_length` as ' + str(args_max_seq_length) + ' but '
'the maximum the model supports is ' + str(model_max_seq_length) + '. We shall reduce it.')
args.max_seq_length = model_max_seq_length
if model is not None and hasattr(model, 'for_training'):
model.for_training(use_gradient_checkpointing=getattr(args, 'gradient_checkpointing', True))
if 'tokenizer' in locals() and hasattr(tokenizer, 'padding_side'): tokenizer.padding_side = 'right'
if 'processing_class' in locals():
if hasattr(processing_class, 'padding_side'): processing_class.padding_side = 'right'
if hasattr(processing_class, 'tokenizer') and hasattr(processing_class.tokenizer, 'padding_side'): processing_class.tokenizer.padding_side = 'right'
other_metrics = []
if not isinstance(reward_funcs, list): _reward_funcs = [reward_funcs]
else: _reward_funcs = reward_funcs
for reward_func in _reward_funcs:
try:
reward_func_name = reward_func.__name__
if True:
other_metrics.append(f'rewards/{reward_func_name}/mean')
if True:
other_metrics.append(f'rewards/{reward_func_name}/std')
if False:
other_metrics.append(f'rewards/{reward_func_name}')
except: pass
from unsloth_zoo.logging_utils import PatchRLStatistics
PatchRLStatistics('grpo_trainer', other_metrics)
# [TODO] Fix up DataParallel multiplying batch sizes
# [TODO] DDP works, but DP seems to not work? [TODO]
if getattr(args, "parallel_mode", None) == ParallelMode.NOT_DISTRIBUTED and args.n_gpu > 1:
if getattr(args, "_n_gpu", 1) != 1:
args._n_gpu = 1
if "model" in locals() and hasattr(model, "for_training"):
model.for_training(use_gradient_checkpointing=getattr(args, 'gradient_checkpointing', True))
super().__init__(
model = model,
reward_funcs = reward_funcs,
args = args,
train_dataset = train_dataset,
eval_dataset = eval_dataset,
processing_class = processing_class,
reward_processing_classes = reward_processing_classes,
callbacks = callbacks,
peft_config = peft_config,
tools = tools,
rollout_func = rollout_func,
environment_factory = environment_factory,**kwargs)
if "model" in locals() and hasattr(model, "for_inference"):
model.for_inference()
if hasattr(self, 'neftune_hook_handle'):
self.neftune_hook_handle.remove()
if hasattr(self, 'neftune_hook_handle'): del self.neftune_hook_handle
if getattr(args, 'neftune_noise_alpha', None) is not None:
model.get_input_embeddings().neftune_noise_alpha = self.neftune_noise_alpha
pass
if hasattr(self, 'accelerator'):
scaler = self.accelerator.scaler
current_model = model
while hasattr(current_model, 'model'):
current_model.accelerator_scaler = scaler
current_model = current_model.model
current_model.accelerator_scaler = scaler
pass
if hasattr(self, 'train'):
self.train = MethodType(prepare_for_training_mode(self.__class__.train), self)
pass
if hasattr(self, 'llm') and self.llm is not None and hasattr(self.llm, 'get_tokenizer'):
_vllm_tok = self.llm.get_tokenizer()
_pc = getattr(self, 'processing_class', None) or getattr(self, 'tokenizer', None)
if _vllm_tok is not None and _pc is not None and getattr(_pc, 'chat_template', None) is not None and getattr(_vllm_tok, 'chat_template', None) is None:
_vllm_tok.chat_template = _pc.chat_template
pass
pass
if hasattr(logger, "addFilter"):
import logging
class HideLoggingMessage(logging.Filter):
def __init__(self, text): self.text = text
def filter(self, x): return not (self.text in x.getMessage())
pass
logger.addFilter(HideLoggingMessage("`use_cache=True`"))
|