Feature Extraction
Transformers
Safetensors
qwen3_5
image-text-to-text
multimodal-embedding
retrieval
evaluation
custom_code
Instructions to use woooooz/WeMM-Embedding-9B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use woooooz/WeMM-Embedding-9B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="woooooz/WeMM-Embedding-9B", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("woooooz/WeMM-Embedding-9B", trust_remote_code=True) model = AutoModelForMultimodalLM.from_pretrained("woooooz/WeMM-Embedding-9B", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 165,255 Bytes
bb2b5cc | 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 | # Copyright 2025 HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import inspect
import os
from collections.abc import Mapping
from functools import lru_cache
from pathlib import Path
from types import UnionType
from typing import ClassVar, Union, get_args, get_origin
import regex as re
import typing_extensions
from .doc import (
MODELS_TO_PIPELINE,
PIPELINE_TASKS_TO_SAMPLE_DOCSTRINGS,
PT_SAMPLE_DOCSTRINGS,
)
from .generic import ModelOutput
PATH_TO_TRANSFORMERS = Path("src").resolve() / "transformers"
AUTODOC_FILES = [
"configuration_*.py",
"modeling_*.py",
"tokenization_*.py",
"processing_*.py",
"image_processing_pil_*.py",
"image_processing_*.py",
"feature_extractor_*.py",
]
PLACEHOLDER_TO_AUTO_MODULE = {
"image_processor_class": ("image_processing_auto", "IMAGE_PROCESSOR_MAPPING_NAMES"),
"tokenizer_class": ("tokenization_auto", "TOKENIZER_MAPPING_NAMES"),
"video_processor_class": ("video_processing_auto", "VIDEO_PROCESSOR_MAPPING_NAMES"),
"feature_extractor_class": ("feature_extraction_auto", "FEATURE_EXTRACTOR_MAPPING_NAMES"),
"processor_class": ("processing_auto", "PROCESSOR_MAPPING_NAMES"),
"config_class": ("configuration_auto", "CONFIG_MAPPING_NAMES"),
"model_class": ("modeling_auto", "MODEL_MAPPING_NAMES"),
}
UNROLL_KWARGS_METHODS = {
"preprocess",
"__call__",
}
UNROLL_KWARGS_CLASSES = {
"BaseImageProcessor",
"ProcessorMixin",
}
BASIC_KWARGS_TYPES = ["TextKwargs", "ImagesKwargs", "VideosKwargs", "AudioKwargs"]
# Short indicator added to unrolled kwargs to distinguish them from regular args
KWARGS_INDICATOR = ", *kwargs*"
HARDCODED_CONFIG_FOR_MODELS = {
"openai": "OpenAIGPTConfig",
"x-clip": "XCLIPConfig",
"kosmos2": "Kosmos2Config",
"kosmos2-5": "Kosmos2_5Config",
"donut": "DonutSwinConfig",
"esmfold": "EsmConfig",
"parakeet": "ParakeetCTCConfig",
"lasr": "LasrCTCConfig",
"wav2vec2-with-lm": "Wav2Vec2Config",
}
_re_checkpoint = re.compile(r"\[(.+?)\]\((https://huggingface\.co/.+?)\)")
# Pre-compiled patterns used repeatedly at runtime. Compiling once here avoids
# repeated compilation overhead (and cache lookups) on every decorator call.
_re_example_or_return = re.compile(r"(?m)^([ \t]*)(?=Example|Return|```)")
_re_return = re.compile(r"(?m)^([ \t]*)(?=Return)")
_re_example = re.compile(r"(?m)^([ \t]*)(?=Example|```)")
_re_args_section = re.compile(r"(?:Args:)(\n.*)?(\n)?$", re.DOTALL)
_re_shape = re.compile(r"(of shape\s*(?:`.*?`|\(.*?\)))")
_re_default = re.compile(r"(defaults to \s*[^)]*)")
_re_param = re.compile(
r"^\s{0,0}(\w+)\s*\(\s*([^, \)]*)(\s*.*?)\s*\)\s*:\s*((?:(?!\n^\s{0,0}\w+\s*\().)*)",
re.DOTALL | re.MULTILINE,
)
_re_forward_ref = re.compile(r"ForwardRef\('([\w.]+)'\)")
_re_optional = re.compile(r"Optional\[(.*?)\]")
_re_placeholders = re.compile(r"{(.*?)}")
_re_model_task = None # built lazily because PT_SAMPLE_DOCSTRINGS isn't available yet
class ImageProcessorArgs:
images = {
"description": """
Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
passing in images with pixel values between 0 and 1, set `do_rescale=False`.
""",
"shape": None,
}
videos = {
"description": """
Video to preprocess. Expects a single or batch of videos with pixel values ranging from 0 to 255. If
passing in videos with pixel values between 0 and 1, set `do_rescale=False`.
""",
"shape": None,
}
do_resize = {
"description": """
Whether to resize the image.
""",
"shape": None,
}
size = {
"description": """
Describes the maximum input dimensions to the model.
""",
"shape": None,
}
size_divisor = {
"description": """
The size by which to make sure both the height and width can be divided.
""",
"shape": None,
}
default_to_square = {
"description": """
Whether to default to a square image when resizing, if size is an int.
""",
"shape": None,
}
resample = {
"description": """
Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`. Only
has an effect if `do_resize` is set to `True`.
""",
"shape": None,
}
do_center_crop = {
"description": """
Whether to center crop the image.
""",
"shape": None,
}
crop_size = {
"description": """
Size of the output image after applying `center_crop`.
""",
"shape": None,
}
do_pad = {
"description": """
Whether to pad the image. Padding is done either to the largest size in the batch
or to a fixed square size per image. The exact padding strategy depends on the model.
""",
"shape": None,
}
pad_size = {
"description": """
The size in `{"height": int, "width" int}` to pad the images to. Must be larger than any image size
provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest
height and width in the batch. Applied only when `do_pad=True.`
""",
"shape": None,
}
do_rescale = {
"description": """
Whether to rescale the image.
""",
"shape": None,
}
rescale_factor = {
"description": """
Rescale factor to rescale the image by if `do_rescale` is set to `True`.
""",
"shape": None,
}
do_normalize = {
"description": """
Whether to normalize the image.
""",
"shape": None,
}
image_mean = {
"description": """
Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`.
""",
"shape": None,
}
image_std = {
"description": """
Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to
`True`.
""",
"shape": None,
}
do_convert_rgb = {
"description": """
Whether to convert the image to RGB.
""",
"shape": None,
}
return_tensors = {
"description": """
Returns stacked tensors if set to `'pt'`, otherwise returns a list of tensors.
""",
"shape": None,
}
data_format = {
"description": """
Only `ChannelDimension.FIRST` is supported. Added for compatibility with slow processors.
""",
"shape": None,
}
input_data_format = {
"description": """
The channel dimension format for the input image. If unset, the channel dimension format is inferred
from the input image. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
- `"none"` or `ChannelDimension.NONE`: image in (height, width) format.
""",
"shape": None,
}
device = {
"description": """
The device to process the images on. If unset, the device is inferred from the input images.
""",
"shape": None,
}
disable_grouping = {
"description": """
Whether to disable grouping of images by size to process them individually and not in batches.
If None, will be set to True if the images are on CPU, and False otherwise. This choice is based on
empirical observations, as detailed here: https://github.com/huggingface/transformers/pull/38157
""",
"shape": None,
}
image_seq_length = {
"description": """
The number of image tokens to be used for each image in the input.
Added for backward compatibility but this should be set as a processor attribute in future models.
""",
"shape": None,
}
# Used for the **kwargs summary line when unrolling typed kwargs (key: "__kwargs__")
__kwargs__ = {
"description": """
Additional image preprocessing options. Model-specific kwargs are listed above; see the TypedDict class
for the complete list of supported arguments.
""",
"shape": None,
}
class ProcessorArgs:
# __init__ arguments
image_processor = {
"description": """
The image processor is a required input.
""",
"type": "{image_processor_class}",
}
tokenizer = {
"description": """
The tokenizer is a required input.
""",
"type": "{tokenizer_class}",
}
video_processor = {
"description": """
The video processor is a required input.
""",
"type": "{video_processor_class}",
}
audio_processor = {
"description": """
The audio processor is a required input.
""",
"type": "{audio_processor_class}",
}
feature_extractor = {
"description": """
The feature extractor is a required input.
""",
"type": "{feature_extractor_class}",
}
chat_template = {
"description": """
A Jinja template to convert lists of messages in a chat into a tokenizable string.
""",
"type": "str",
}
# __call__ arguments
text = {
"description": """
The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
(pretokenized string). If you pass a pretokenized input, set `is_split_into_words=True` to avoid ambiguity with batched inputs.
""",
}
audio = {
"description": """
The audio or batch of audios to be prepared. Each audio can be a NumPy array or PyTorch tensor.
In case of a NumPy array/PyTorch tensor, each audio should be of shape (C, T), where C is a number of channels,
and T is the sample length of the audio.
""",
}
audios = {
"description": """
The audio or batch of audios to be prepared. Each audio can be a NumPy array or PyTorch tensor.
In case of a NumPy array/PyTorch tensor, each audio should be of shape (C, T), where C is a number of channels,
and T is the sample length of the audio.
""",
}
return_tensors = {
"description": """
If set, will return tensors of a particular framework. Acceptable values are:
- `'pt'`: Return PyTorch `torch.Tensor` objects.
- `'np'`: Return NumPy `np.ndarray` objects.
""",
"shape": None,
}
# Standard tokenizer arguments
add_special_tokens = {
"description": """
Whether or not to add special tokens when encoding the sequences. This will use the underlying
[`PretrainedTokenizerBase.build_inputs_with_special_tokens`] function, which defines which tokens are
automatically added to the input ids. This is useful if you want to add `bos` or `eos` tokens
automatically.
""",
"type": "bool",
}
padding = {
"description": """
Activates and controls padding. Accepts the following values:
- `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single
sequence is provided).
- `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum
acceptable input length for the model if that argument is not provided.
- `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different
lengths).
""",
"type": "bool, str or [`~utils.PaddingStrategy`]",
}
truncation = {
"description": """
Activates and controls truncation. Accepts the following values:
- `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or
to the maximum acceptable input length for the model if that argument is not provided. This will
truncate token by token, removing a token from the longest sequence in the pair if a pair of
sequences (or a batch of pairs) is provided.
- `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
- `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
- `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths
greater than the model maximum admissible input size).
""",
"type": "bool, str or [`~tokenization_utils_base.TruncationStrategy`]",
}
max_length = {
"description": """
Controls the maximum length to use by one of the truncation/padding parameters.
If left unset or set to `None`, this will use the predefined model maximum length if a maximum length
is required by one of the truncation/padding parameters. If the model has no specific maximum input
length (like XLNet) truncation/padding to a maximum length will be deactivated.
""",
"type": "int",
}
stride = {
"description": """
If set to a number along with `max_length`, the overflowing tokens returned when
`return_overflowing_tokens=True` will contain some tokens from the end of the truncated sequence
returned to provide some overlap between truncated and overflowing sequences. The value of this
argument defines the number of overlapping tokens.
""",
"type": "int",
}
pad_to_multiple_of = {
"description": """
If set will pad the sequence to a multiple of the provided value. Requires `padding` to be activated.
This is especially useful to enable using Tensor Cores on NVIDIA hardware with compute capability
`>= 7.5` (Volta).
""",
"type": "int",
}
return_token_type_ids = {
"description": """
Whether to return token type IDs. If left to the default, will return the token type IDs according to
the specific tokenizer's default, defined by the `return_outputs` attribute.
[What are token type IDs?](../glossary#token-type-ids)
""",
"type": "bool",
}
return_attention_mask = {
"description": """
Whether to return the attention mask. If left to the default, will return the attention mask according
to the specific tokenizer's default, defined by the `return_outputs` attribute.
[What are attention masks?](../glossary#attention-mask)
""",
"type": "bool",
}
return_overflowing_tokens = {
"description": """
Whether or not to return overflowing token sequences. If a pair of sequences of input ids (or a batch
of pairs) is provided with `truncation_strategy = longest_first` or `True`, an error is raised instead
of returning overflowing tokens.
""",
"type": "bool",
}
return_special_tokens_mask = {
"description": """
Whether or not to return special tokens mask information.
""",
"type": "bool",
}
return_offsets_mapping = {
"description": """
Whether or not to return `(char_start, char_end)` for each token.
This is only available on fast tokenizers inheriting from [`PreTrainedTokenizerFast`], if using
Python's tokenizer, this method will raise `NotImplementedError`.
""",
"type": "bool",
}
return_length = {
"description": """
Whether or not to return the lengths of the encoded inputs.
""",
"type": "bool",
}
verbose = {
"description": """
Whether or not to print more information and warnings.
""",
"type": "bool",
}
text_pair = {
"description": """
Optional second sequence to be encoded. This can be a string, a list of strings (tokenized string using
the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids`
method).
""",
"type": "str, list[str] or list[int]",
}
text_target = {
"description": """
The sequence or batch of sequences to be encoded as target texts. Each sequence can be a string or a
list of strings (pretokenized string). If you pass pretokenized input, set `is_split_into_words=True`
to avoid ambiguity with batched inputs.
""",
"type": "str, list[str] or list[list[str]]",
}
text_pair_target = {
"description": """
The sequence or batch of sequences to be encoded as target texts. Each sequence can be a string or a
list of strings (pretokenized string). If you pass pretokenized input, set `is_split_into_words=True`
to avoid ambiguity with batched inputs.
""",
"type": "str, list[str] or list[list[str]]",
}
is_split_into_words = {
"description": """
Whether or not the input is already pre-tokenized (e.g., split into words). If set to `True`, the
tokenizer assumes the input is already split into words (for instance, by splitting it on whitespace)
which it will tokenize. This is useful for NER or token classification.
""",
"type": "bool",
}
boxes = {
"description": """
Word-level bounding boxes. Each bounding box should be normalized to be on a 0-1000 scale.
""",
"type": "list[list[int]] or list[list[list[int]]]",
}
word_labels = {
"description": """
Word-level integer labels (for token classification tasks such as FUNSD, CORD).
""",
"type": "list[int] or list[list[int]]",
}
# Used for the **kwargs summary line when unrolling typed kwargs (key: "__kwargs__")
__kwargs__ = {
"description": """
Additional processing options for each modality (text, images, videos, audio). Model-specific parameters
are listed above; see the TypedDict class for the complete list of supported arguments.
""",
"shape": None,
}
class ConfigArgs:
output_hidden_states = {
"description": """
Whether or not the model should return all hidden-states.
""",
}
chunk_size_feed_forward = {
"description": """
The `dtype` of the weights. This attribute can be used to initialize the model to a non-default `dtype`
(which is normally `float32`) and thus allow for optimal storage allocation. For example, if the saved
model is `float16`, ideally we want to load it back using the minimal amount of memory needed to load
`float16` weights.
""",
}
dtype = {
"description": """
The chunk size of all feed forward layers in the residual attention blocks. A chunk size of `0` means that
the feed forward layer is not chunked. A chunk size of n means that the feed forward layer processes `n` <
sequence_length embeddings at a time. For more information on feed forward chunking, see [How does Feed
Forward Chunking work?](../glossary.html#feed-forward-chunking).
""",
}
id2label = {
"description": """
A map from index (for instance prediction index, or target index) to label.
""",
}
label2id = {
"description": """
A map from label to index for the model.
""",
}
problem_type = {
"description": """
Problem type for `XxxForSequenceClassification` models. Can be one of `"regression"`,
`"single_label_classification"` or `"multi_label_classification"`.
""",
}
tokenizer_class = {
"description": """
The class name of model's tokenizer.
""",
}
vocab_size = {
"description": """
Vocabulary size of the model. Defines the number of different tokens that can be represented by the `input_ids`.
""",
}
hidden_size = {
"description": """
Dimension of the hidden representations.
""",
}
intermediate_size = {
"description": """
Dimension of the MLP representations.
""",
}
head_dim = {
"description": """
The attention head dimension. If None, it will default to hidden_size // num_attention_heads
"""
}
num_hidden_layers = {
"description": """
Number of hidden layers in the Transformer decoder.
""",
}
num_attention_heads = {
"description": """
Number of attention heads for each attention layer in the Transformer decoder.
""",
}
num_key_value_heads = {
"description": """
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
`num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
`num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
by meanpooling all the original heads within that group. For more details, check out [this
paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to
`num_attention_heads`.
""",
}
hidden_act = {
"description": """
The non-linear activation function (function or string) in the decoder. For example, `"gelu"`,
`"relu"`, `"silu"`, etc.
""",
}
max_position_embeddings = {
"description": """
The maximum sequence length that this model might ever be used with.
""",
}
initializer_range = {
"description": """
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
""",
}
rms_norm_eps = {
"description": """
The epsilon used by the rms normalization layers.
""",
}
use_cache = {
"description": """
Whether or not the model should return the last key/values attentions (not used by all models). Only
relevant if `config.is_decoder=True` or when the model is a decoder-only generative model.
""",
}
rope_parameters = {
"description": """
Dictionary containing the configuration parameters for the RoPE embeddings. The dictionary should contain
a value for `rope_theta` and optionally parameters used for scaling in case you want to use RoPE
with longer `max_position_embeddings`.
""",
}
attention_bias = {
"description": """
Whether to use a bias in the query, key, value and output projection layers during self-attention.
""",
}
mlp_bias = {
"description": """
Whether to use a bias in up_proj, down_proj and gate_proj layers in the MLP layers.
""",
}
attention_dropout = {
"description": """
The dropout ratio for the attention probabilities.
""",
}
pretraining_tp = {
"description": """
Experimental feature. Tensor parallelism rank used during pretraining. Please refer to [this
document](https://huggingface.co/docs/transformers/main/perf_train_gpu_many#tensor-parallelism) to
understand more about it. This value is necessary to ensure exact reproducibility of the pretraining
results. Please refer to [this issue](https://github.com/pytorch/pytorch/issues/76232).
""",
}
pad_token_id = {
"description": """
Token id used for padding in the vocabulary.
""",
}
eos_token_id = {
"description": """
Token id used for end-of-stream in the vocabulary.
""",
}
bos_token_id = {
"description": """
Token id used for beginning-of-stream in the vocabulary.
""",
}
sep_token_id = {
"description": """
Token id used for separator in the vocabulary.
""",
}
cls_token_id = {
"description": """
Token id used for CLS in the vocabulary.
""",
}
tie_word_embeddings = {
"description": """
Whether to tie weight embeddings according to model's `tied_weights_keys` mapping.
""",
}
d_model = {
"description": """
Size of the encoder layers and the pooler layer.
""",
}
d_kv = {
"description": """
Size of the key, query, value projections per attention head. The `inner_dim` of the projection layer will
be defined as `num_heads * d_kv`.
""",
}
num_decoder_layers = {
"description": """
Number of hidden layers in the Transformer decoder. Will use the same value as `num_layers` if not set.
""",
}
num_encoder_layers = {
"description": """
Number of hidden layers in the Transformer encoder. Will use the same value as `num_layers` if not set.
""",
}
dropout_rate = {
"description": """
The ratio for all dropout layers.
""",
}
classifier_dropout = {
"description": """
The dropout ratio for classifier.
""",
}
layer_norm_eps = {
"description": """
The epsilon used by the layer normalization layers.
""",
}
initializer_factor = {
"description": """
A factor for initializing all weight matrices (should be kept to 1, used internally for initialization
testing).
""",
}
encoder_attention_heads = {
"description": """
Number of attention heads for each attention layer in the Transformer encoder.
""",
}
decoder_attention_heads = {
"description": """
Number of attention heads for each attention layer in the Transformer decoder.
""",
}
decoder_ffn_dim = {
"description": """
Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
""",
}
encoder_ffn_dim = {
"description": """
Dimensionality of the "intermediate" (often named feed-forward) layer in encoder.
""",
}
activation_dropout = {
"description": """
The dropout ratio for activations inside the fully connected layer.
""",
}
encoder_layerdrop = {
"description": """
The LayerDrop probability for the encoder. See the [LayerDrop paper](see https://huggingface.co/papers/1909.11556)
for more details.
""",
}
decoder_layerdrop = {
"description": """
The LayerDrop probability for the decoder. See the [LayerDrop paper](see https://huggingface.co/papers/1909.11556)
for more details.
""",
}
scale_embedding = {
"description": """
Whether to scale embeddings by dividing by sqrt(d_model).
""",
}
forced_eos_token_id = {
"description": """
The id of the token to force as the last generated token when `max_length` is reached. Usually set to
`eos_token_id`.
""",
}
moe_intermediate_size = {
"description": """
Intermediate size of the routed expert MLPs.
""",
}
num_experts = {
"description": """
Number of routed experts in MoE layers.
""",
}
num_experts_per_tok = {
"description": """
Number of experts to route each token to. This is the top-k value for the token-choice routing.
""",
}
num_shared_experts = {
"description": """
Number of shared experts that are always activated for all tokens.
""",
}
layer_types = {
"description": """
A list that explicitly maps each layer index with its layer type. If not provided, it will be automatically
generated based on config values.
""",
}
norm_topk_prob = {
"description": """
Whether to normalize the weights of the routed experts.
""",
}
topk_group = {
"description": """
Number of selected groups for each token (for each token, ensuring the selected experts is only within `topk_group` groups).
""",
}
qk_rope_head_dim = {
"description": """
Dimension of the query/key heads that use rotary position embeddings.
""",
}
v_head_dim = {
"description": """
Dimension of the value heads.
""",
}
qk_nope_head_dim = {
"description": """
Dimension of the query/key heads that don't use rotary position embeddings.
""",
}
kv_lora_rank = {
"description": """
Rank of the LoRA matrices for key and value projections.
""",
}
q_lora_rank = {
"description": """
Rank of the LoRA matrices for query projections.
""",
}
routed_scaling_factor = {
"description": """
Scaling factor or routed experts.
""",
}
n_routed_experts = {
"description": """
Number of routed experts.
""",
}
n_shared_experts = {
"description": """
Number of shared experts.
""",
}
vision_config = {
"description": """
The config object or dictionary of the vision backbone.
""",
}
text_config = {
"description": """
The config object or dictionary of the text backbone.
""",
}
projector_hidden_act = {
"description": """
The activation function used by the multimodal projector.
""",
}
vision_feature_select_strategy = {
"description": """
The feature selection strategy used to select the vision feature from the vision backbone.
""",
}
vision_feature_layer = {
"description": """
The index of the layer to select the vision feature. If multiple indices are provided,
the vision feature of the corresponding indices will be concatenated to form the
vision features.
""",
}
multimodal_projector_bias = {
"description": """
Whether to use bias in the multimodal projector.
""",
}
image_token_id = {
"description": """
The image token index used as a placeholder for input images.
""",
}
video_token_id = {
"description": """
The video token index used as a placeholder for input videos.
""",
}
audio_token_id = {
"description": """
The audio token index used as a placeholder for input audio.
""",
}
image_seq_length = {
"description": """
Sequence length of one image embedding.
""",
}
video_seq_length = {
"description": """
Sequence length of one video embedding.
""",
}
add_cross_attention = {
"description": """
Whether cross-attention layers should be added to the model.
""",
}
is_decoder = {
"description": """
Whether the model is used as a decoder or not. If `False`, the model is used as an encoder.
""",
}
sliding_window = {
"description": """
Sliding window attention window size. If `None`, no sliding window is applied.
""",
}
use_sliding_window = {
"description": """
Whether to use sliding window attention.
""",
}
shared_expert_intermediate_size = {
"description": """
Intermediate size of the shared expert MLPs.
""",
}
decoder_sparse_step = {
"description": """
The frequency of adding a sparse MoE layer. The default is 1, which means all decoder layers are sparse MoE.
""",
}
output_router_logits = {
"description": """
Whether or not the router logits should be returned by the model. Enabling this will also allow the model
to output the auxiliary loss, including load balancing loss and router z-loss.
""",
}
router_aux_loss_coef = {
"description": """
Auxiliary load balancing loss coefficient. Used to penalize uneven expert routing in MoE models.
""",
}
out_indices = {
"description": """
Indices of the intermediate hidden states (feature maps) to return from the backbone. Each index
corresponds to one stage of the model.
""",
}
out_features = {
"description": """
Names of the intermediate hidden states (feature maps) to return from the backbone. One of `"stem"`,
`"stage1"`, `"stage2"`, etc.
""",
}
image_size = {
"description": """
The size (resolution) of each image.
""",
}
patch_size = {
"description": """
The size (resolution) of each patch.
""",
}
num_channels = {
"description": """
The number of input channels.
""",
}
num_mel_bins = {
"description": """
Number of mel features used per input frame. Should correspond to the value used in the
`AutoFeatureExtractor` class.
""",
}
sampling_rate = {
"description": """
The sampling rate at which the audio files should be digitalized expressed in hertz (Hz).
""",
}
hidden_dropout = {
"description": """
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
""",
}
mlp_ratio = {
"description": """
Ratio of the MLP hidden dim to the embedding dim.
""",
}
qkv_bias = {
"description": """
Whether to add a bias to the queries, keys and values.
""",
}
n_embd = {
"description": """
Dimensionality of the embeddings and hidden states.
""",
}
resid_pdrop = {
"description": """
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
""",
}
embd_pdrop = {
"description": """
The dropout ratio for the embeddings.
""",
}
clip_qkv = {
"description": """
If not `None`, cap the absolute value of the query, key, and value tensors to this value.
""",
}
type_vocab_size = {
"description": """
The vocabulary size of the `token_type_ids`.
""",
}
audio_config = {
"description": """
The config object or dictionary of the audio backbone.
""",
}
layerdrop = {
"description": """
The LayerDrop probability. See the [LayerDrop paper](see https://huggingface.co/papers/1909.11556) for
more details.
""",
}
expert_capacity = {
"description": """
The number of tokens that each expert can process. If `None`, `expert_capacity` will be set to
`(sequence_length / num_experts) * capacity_factor`.
""",
}
decoder_start_token_id = {
"description": """
If an encoder-decoder model starts decoding with a different token than `bos`, the id of that token.
""",
}
is_encoder_decoder = {
"description": """
Whether the model is used as an encoder/decoder or not.
""",
}
num_codebooks = {
"description": """
The number of parallel codebooks used by the model.
""",
}
codebook_dim = {
"description": """
Dimensionality of each codebook embedding vector.
""",
}
hidden_sizes = {
"description": """
Dimensionality (hidden size) at each stage of the model.
""",
}
depths = {
"description": """
Depth of each layer in the Transformer.
""",
}
patch_sizes = {
"description": """
Patch size at each stage of the model.
""",
}
strides = {
"description": """
Stride at each stage of the model.
""",
}
router_jitter_noise = {
"description": """
Amount of noise to add to the router logits during training for better load balancing.
""",
}
num_local_experts = {
"description": """
Number of local experts on each device. `num_experts` should be divisible by `num_local_experts`.
""",
}
qk_layernorm = {
"description": """
Whether to use query-key normalization in the attention.
""",
}
backbone_config = {
"description": """
The configuration of the backbone model.
""",
}
no_object_weight = {
"description": """
Relative classification weight of the no-object class in the object detection loss.
""",
}
class_weight = {
"description": """
Relative weight of the classification error in the Hungarian matching cost.
""",
}
mask_weight = {
"description": """
Relative weight of the focal loss in the panoptic segmentation loss.
""",
}
dice_weight = {
"description": """
Relative weight of the dice loss in the panoptic segmentation loss.
""",
}
class_cost = {
"description": """
Relative weight of the classification error in the Hungarian matching cost.
""",
}
bbox_cost = {
"description": """
Relative weight of the L1 bounding box error in the Hungarian matching cost.
""",
}
giou_cost = {
"description": """
Relative weight of the generalized IoU loss in the Hungarian matching cost.
""",
}
focal_alpha = {
"description": """
Alpha parameter in the focal loss.
""",
}
mask_loss_coefficient = {
"description": """
Relative weight of the focal loss in the panoptic segmentation loss.
""",
}
giou_loss_coefficient = {
"description": """
Relative weight of the generalized IoU loss in the panoptic segmentation loss.
""",
}
bbox_loss_coefficient = {
"description": """
Relative weight of the L1 bounding box loss in the panoptic segmentation loss.
""",
}
cls_loss_coefficient = {
"description": """
Relative weight of the classification loss in the panoptic segmentation loss.
""",
}
dice_loss_coefficient = {
"description": """
Relative weight of the dice loss in the panoptic segmentation loss.
""",
}
semantic_loss_ignore_index = {
"description": """
The index that is ignored by the loss function of the semantic segmentation model.
""",
}
projection_dim = {
"description": """
Dimensionality of text and vision projection layers.
""",
}
logit_scale_init_value = {
"description": """
The initial value of the *logit_scale* parameter.
""",
}
num_dense_layers = {
"description": """
Number of initial dense layers before MoE layers begin. Layers with index < num_dense_layers will use
standard dense MLPs instead of MoE.
""",
}
drop_path_rate = {
"description": """
Drop path rate for the patch fusion.
""",
}
vq_config = {
"description": """
Configuration dict of the vector quantize module.
""",
}
num_embeddings = {
"description": """
Number of codebook embeddings.
""",
}
double_latent = {
"description": """
Whether to use double z channels.
""",
}
latent_channels = {
"description": """
Number of channels for the latent space.
""",
}
qformer_config = {
"description": """
Configuration dict of the Q-Former module.
""",
}
conv_kernel_size = {
"description": """
The size of the convolutional kernel.
""",
}
output_stride = {
"description": """
The ratio between the spatial resolution of the input and output feature maps.
""",
}
depth_multiplier = {
"description": """
Shrinks or expands the number of channels in each layer. This is sometimes also called "alpha" or "width multiplier".
""",
}
use_absolute_position_embeddings = {
"description": """
Whether to use absolute position embeddings.
""",
}
use_relative_position_bias = {
"description": """
Whether to use relative position bias in the self-attention layers.
""",
}
layer_scale_init_value = {
"description": """
Scale to use in the self-attention layers. 0.1 for base, 1e-6 for large. Set 0 to disable layer scale.
""",
}
vlm_config = {
"description": """
The config object or dictionary of the vision-language backbone.
""",
}
init_xavier_std = {
"description": """
The scaling factor used for the Xavier initialization of the cross-attention weights.
""",
}
auxiliary_loss = {
"description": """
Whether auxiliary decoding losses (losses at each decoder layer) are to be used.
""",
}
encoder_config = {
"description": """
The config object or dictionary of the encoder backbone.
""",
}
decoder_config = {
"description": """
The config object or dictionary of the decoder backbone.
""",
}
embedding_multiplier = {
"description": """
Scaling factor applied to the word embeddings. Used to scale the embeddings relative to the hidden size.
""",
}
logits_scaling = {
"description": """
Scaling factor applied to the output logits before computing the probability distribution.
""",
}
residual_multiplier = {
"description": """
Scaling factor applied to the residual connections.
""",
}
attention_multiplier = {
"description": """
Scaling factor applied to the attention weights.
""",
}
classifier_activation = {
"description": """
The activation function for the classification head.
""",
}
return_dict = {
"description": """
Whether to return a `ModelOutput` (dataclass) instead of a plain tuple.
""",
}
router_z_loss_coef = {
"description": """
Coefficient for the router z-loss, which penalizes large router logits to improve training stability.
""",
}
final_logit_softcapping = {
"description": """
Soft-capping value applied to the final logits before computing the probability distribution. Logits are
scaled by `tanh(logit / cap) * cap`.
""",
}
cross_attention_hidden_size = {
"description": """
Hidden size of the encoder outputs projected into the cross-attention key/value space of the decoder. Used
when the encoder and decoder have different hidden sizes.
""",
}
input_dim = {
"description": """
Dimensionality of the input acoustic features (e.g., number of mel-filterbank channels).
""",
}
use_auxiliary_loss = {
"description": """
Whether to calculate loss using intermediate predictions from transformer decoder.
""",
}
batch_norm_eps = {
"description": """
The epsilon used by the batch normalization layers.
""",
}
max_window_layers = {
"description": """
The number of layers using full attention. The first `max_window_layers` layers will use full attention, while any
additional layer afterwards will use SWA (Sliding Window Attention).
""",
}
ctc_loss_reduction = {
"description": """
Specifies the reduction to apply to the output of `torch.nn.CTCLoss`. Only relevant when training.
""",
}
mask_feature_prob = {
"description": """
Percentage (between 0 and 1) of all feature vectors along the feature axis which will be masked. The
masking procedure generates `mask_feature_prob*len(feature_axis)/mask_time_length` independent masks over
the axis. If reasoning from the probability of each feature vector to be chosen as the start of the vector
span to be masked, *mask_feature_prob* should be `prob_vector_start*mask_feature_length`. Note that overlap
may decrease the actual percentage of masked vectors. This is only relevant if `apply_spec_augment` is
`True`.
""",
}
eos_coefficient = {
"description": """
Relative classification weight of the 'no-object' class in the object detection loss.
""",
}
num_labels = {
"description": """
Number of labels to use in the last layer added to the model, typically for a classification task.
""",
}
depth = {
"description": """
Number of Transformer layers in the vision encoder.
""",
}
temporal_patch_size = {
"description": """
Temporal patch size used in the 3D patch embedding for video inputs.
""",
}
spatial_merge_size = {
"description": """
The size of the spatial merge window used to reduce the number of visual tokens by merging neighboring patches.
""",
}
vision_start_token_id = {
"description": """
Token ID that marks the start of a visual segment in the multimodal input sequence.
""",
}
vision_end_token_id = {
"description": """
Token ID that marks the end of a visual segment in the multimodal input sequence.
""",
}
mamba_n_heads = {
"description": """
The number of mamba heads used in the v2 implementation.
""",
}
mamba_d_head = {
"description": """
Head embedding dimension size
""",
}
mamba_n_groups = {
"description": """
The number of the mamba groups used in the v2 implementation.
""",
}
mamba_d_conv = {
"description": """
The size of the mamba convolution kernel
""",
}
mamba_expand = {
"description": """
Expanding factor (relative to hidden_size) used to determine the mamba intermediate size
""",
}
mamba_chunk_size = {
"description": """
The chunks in which to break the sequence when doing prefill/training
""",
}
mamba_conv_bias = {
"description": """
Flag indicating whether or not to use bias in the convolution layer of the mamba mixer block.
""",
}
mamba_proj_bias = {
"description": """
Flag indicating whether or not to use bias in the input and output projections (["in_proj", "out_proj"]) of the mamba mixer block
""",
}
time_step_min = {
"description": """
Minimum `time_step` used to bound `dt_proj.bias`.
""",
}
time_step_max = {
"description": """
Maximum `time_step` used to bound `dt_proj.bias`.
""",
}
time_step_limit = {
"description": """
Accepted range of time step values for clamping.
""",
}
expand_ratio = {
"description": """
Expand ratio to set the output dimensions for the expansion
""",
}
state_size = {
"description": """
Size of the SSM state (latent state dimension) in the Mamba layers.
""",
}
time_step_rank = {
"description": """
Rank of the delta (time step) projection. Can be `"auto"` to set it automatically.
""",
}
time_step_floor = {
"description": """
Minimum allowed value for the discrete time step delta after softplus activation.
""",
}
time_step_scale = {
"description": """
Scale applied to the time step delta before discretization.
""",
}
time_step_init_scheme = {
"description": """
Initialization scheme for the time step delta. Can be `"random"` or `"uniform"`.
""",
}
mamba_d_ssm = {
"description": """
Inner state size of the SSM (state-space model) in the Mamba layers of FalconH1.
""",
}
mamba_norm_before_gate = {
"description": """
Whether to apply normalization before the gating mechanism in the Mamba mixer.
""",
}
mamba_rms_norm = {
"description": """
Whether to use RMS normalization in the Mamba layers (as opposed to standard LayerNorm).
""",
}
mamba_d_state = state_size
mamba_num_heads = mamba_n_heads
mamba_head_dim = mamba_d_head
num_input_channels = num_channels
audio_channels = num_channels
input_channels = num_channels
in_channels = num_channels
in_chans = num_channels
scale_attn_weights = scale_embedding
attention_probs_dropout_prob = attention_dropout
attn_pdrop = attention_dropout
attn_dropout = attention_dropout
dropout = dropout_rate
resid_dropout = resid_pdrop
residual_dropout = resid_pdrop
emb_pdrop = embd_pdrop
embed_dropout = embd_pdrop
embedding_dropout = embd_pdrop
hidden_dropout_prob = hidden_dropout
hidden_dropout_rate = hidden_dropout
classifier_dropout_prob = classifier_dropout
classifier_dropout_rate = classifier_dropout
dropout_prob = dropout
dropout_p = dropout
decoder_attention_dropout = attention_dropout
decoder_dropout = dropout
encoder_dropout = dropout
route_scale = routed_scaling_factor
activation_function = hidden_act
hidden_dim = hidden_size
num_decoder_attention_heads = decoder_attention_heads
num_encoder_attention_heads = encoder_attention_heads
decoder_num_heads = decoder_attention_heads
decoder_num_attention_heads = decoder_attention_heads
encoder_num_heads = encoder_attention_heads
encoder_num_attention_heads = encoder_attention_heads
encoder_layers = num_encoder_layers
decoder_layers = num_decoder_layers
decoder_num_layers = num_decoder_layers
encoder_num_layers = num_encoder_layers
d_ff = intermediate_size
dim_ff = intermediate_size
n_inner = intermediate_size
decoder_intermediate_size = intermediate_size
num_kv_heads = num_key_value_heads
num_layers = num_hidden_layers
n_layers = num_hidden_layers
n_layer = num_hidden_layers
layers = num_layers
encoder_num_hidden_layers = encoder_layers
decoder_num_hidden_layers = decoder_layers
num_heads = num_attention_heads
n_heads = num_attention_heads
n_head = num_attention_heads
hidden_activation = hidden_act
activation = hidden_act
mlp_hidden_act = hidden_act
d_head = head_dim
d_inner = intermediate_size
dim_head = head_dim
ffn_dim = intermediate_size
attention_heads = num_attention_heads
n_positions = max_position_embeddings
init_std = initializer_range
initializer_std = initializer_range
projector_bias = multimodal_projector_bias
image_token_index = image_token_id
video_token_index = video_token_id
audio_token_index = audio_token_id
embedding_size = n_embd
embed_dim = n_embd
projection_hidden_act = projector_hidden_act
layer_norm_epsilon = layer_norm_eps
rms_norm = rms_norm_eps
norm_eps = layer_norm_eps
eps = layer_norm_eps
norm_epsilon = layer_norm_eps
qk_layernorms = qk_layernorm
use_qk_norm = qk_layernorm
use_qkv_bias = qkv_bias
decoder_hidden_act = hidden_act
decoder_hidden_dim = hidden_size
decoder_hidden_size = hidden_size
encoder_hidden_dim = hidden_size
encoder_hidden_size = hidden_size
layer_scale_initial_scale = layer_scale_init_value
multi_modal_projector_bias = projector_bias
projector_hidden_size = projection_dim
projection_size = projection_dim
kernel_size = conv_kernel_size
conv_kernel = conv_kernel_size
use_absolute_embeddings = use_absolute_position_embeddings
use_abs_pos = use_absolute_position_embeddings
use_rel_pos = use_relative_position_bias
aux_loss_coef = router_aux_loss_coef
embedding_dimension = embed_dim
embedding_dim = embed_dim
emb_dim = embed_dim
n_codebooks = num_codebooks
codebook_size = num_codebooks
layers_block_type = layer_types
sample_rate = sampling_rate
text_vocab_size = vocab_size
class ModelArgs:
labels = {
"description": """
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
""",
"shape": "of shape `(batch_size, sequence_length)`",
}
num_logits_to_keep = {
"description": """
Calculate logits for the last `num_logits_to_keep` tokens. If `0`, calculate logits for all
`input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
""",
"shape": None,
}
input_ids = {
"description": """
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default.
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
[`PreTrainedTokenizer.__call__`] for details.
[What are input IDs?](../glossary#input-ids)
""",
"shape": "of shape `(batch_size, sequence_length)`",
}
input_values = {
"description": """
Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file
into an array of type `list[float]`, a `numpy.ndarray` or a `torch.Tensor`, *e.g.* via the torchcodec library
(`pip install torchcodec`) or the soundfile library (`pip install soundfile`).
To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion
into a tensor of type `torch.FloatTensor`. See [`{processor_class}.__call__`] for details.
""",
"shape": "of shape `(batch_size, sequence_length)`",
}
attention_mask = {
"description": """
Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
- 1 for tokens that are **not masked**,
- 0 for tokens that are **masked**.
[What are attention masks?](../glossary#attention-mask)
""",
"shape": "of shape `(batch_size, sequence_length)`",
}
decoder_attention_mask = {
"description": """
Mask to avoid performing attention on certain token indices. By default, a causal mask will be used, to
make sure the model can only look at previous inputs in order to predict the future.
""",
"shape": "of shape `(batch_size, target_sequence_length)`",
}
encoder_hidden_states = {
"description": """
Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention
if the model is configured as a decoder.
""",
"shape": "of shape `(batch_size, sequence_length, hidden_size)`",
}
encoder_attention_mask = {
"description": """
Mask to avoid performing attention on the padding token indices of the encoder input. This mask is used in
the cross-attention if the model is configured as a decoder. Mask values selected in `[0, 1]`:
- 1 for tokens that are **not masked**,
- 0 for tokens that are **masked**.
""",
"shape": "of shape `(batch_size, sequence_length)`",
}
token_type_ids = {
"description": """
Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0, 1]`:
- 0 corresponds to a *sentence A* token,
- 1 corresponds to a *sentence B* token.
[What are token type IDs?](../glossary#token-type-ids)
""",
"shape": "of shape `(batch_size, sequence_length)`",
}
mm_token_type_ids = {
"description": """
Indices of input sequence tokens matching each modality. For example text (0), image (1), video (2).
Multimodal token type ids can be obtained using [`AutoProcessor`]. See [`ProcessorMixin.__call__`] for details.
""",
"shape": "of shape `(batch_size, sequence_length)`",
}
position_ids = {
"description": """
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, config.n_positions - 1]`.
[What are position IDs?](../glossary#position-ids)
""",
"shape": "of shape `(batch_size, sequence_length)`",
}
past_key_values = {
"description": """
Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.
Only [`~cache_utils.Cache`] instance is allowed as input, see our [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache).
If no `past_key_values` are passed, [`~cache_utils.DynamicCache`] will be initialized by default.
The model will output the same cache format that is fed as input.
If `past_key_values` are used, the user is expected to input only unprocessed `input_ids` (those that don't
have their past key value states given to this model) of shape `(batch_size, unprocessed_length)` instead of all `input_ids`
of shape `(batch_size, sequence_length)`.
""",
"shape": None,
}
inputs_embeds = {
"description": """
Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
model's internal embedding lookup matrix.
""",
"shape": "of shape `(batch_size, sequence_length, hidden_size)`",
}
decoder_input_ids = {
"description": """
Indices of decoder input sequence tokens in the vocabulary.
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
[`PreTrainedTokenizer.__call__`] for details.
[What are decoder input IDs?](../glossary#decoder-input-ids)
""",
"shape": "of shape `(batch_size, target_sequence_length)`",
}
decoder_inputs_embeds = {
"description": """
Optionally, instead of passing `decoder_input_ids` you can choose to directly pass an embedded
representation. If `past_key_values` is used, optionally only the last `decoder_inputs_embeds` have to be
input (see `past_key_values`). This is useful if you want more control over how to convert
`decoder_input_ids` indices into associated vectors than the model's internal embedding lookup matrix.
If `decoder_input_ids` and `decoder_inputs_embeds` are both unset, `decoder_inputs_embeds` takes the value
of `inputs_embeds`.
""",
"shape": "of shape `(batch_size, target_sequence_length, hidden_size)`",
}
use_cache = {
"description": """
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
`past_key_values`).
""",
"shape": None,
}
output_attentions = {
"description": """
Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
tensors for more detail.
""",
"shape": None,
}
output_hidden_states = {
"description": """
Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
more detail.
""",
"shape": None,
}
return_dict = {
"description": """
Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
""",
"shape": None,
}
hidden_states = {
"description": """ input to the layer of shape `(batch, seq_len, embed_dim)""",
"shape": None,
}
interpolate_pos_encoding = {
"description": """
Whether to interpolate the pre-trained position encodings.
""",
"shape": None,
}
position_embeddings = {
"description": """
Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`,
with `head_dim` being the embedding dimension of each attention head.
""",
"shape": None,
}
config = {
"description": """
Model configuration class with all the parameters of the model. Initializing with a config file does not
load the weights associated with the model, only the configuration. Check out the
[`~PreTrainedModel.from_pretrained`] method to load the model weights.
""",
"shape": None,
}
start_positions = {
"description": """
Labels for position (index) of the start of the labelled span for computing the token classification loss.
Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
are not taken into account for computing the loss.
""",
"shape": "of shape `(batch_size,)`",
}
end_positions = {
"description": """
Labels for position (index) of the end of the labelled span for computing the token classification loss.
Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
are not taken into account for computing the loss.
""",
"shape": "of shape `(batch_size,)`",
}
encoder_outputs = {
"description": """
Tuple consists of (`last_hidden_state`, *optional*: `hidden_states`, *optional*: `attentions`)
`last_hidden_state` of shape `(batch_size, sequence_length, hidden_size)`, *optional*) is a sequence of
hidden-states at the output of the last layer of the encoder. Used in the cross-attention of the decoder.
""",
"shape": None,
}
output_router_logits = {
"description": """
Whether or not to return the logits of all the routers. They are useful for computing the router loss, and
should not be returned during inference.
""",
"shape": None,
}
logits_to_keep = {
"description": """
If an `int`, compute logits for the last `logits_to_keep` tokens. If `0`, calculate logits for all
`input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
If a `torch.Tensor`, must be 1D corresponding to the indices to keep in the sequence length dimension.
This is useful when using packed tensor format (single dimension for batch and sequence length).
""",
"shape": None,
}
pixel_values = {
"description": """
The tensors corresponding to the input images. Pixel values can be obtained using
[`{image_processor_class}`]. See [`{image_processor_class}.__call__`] for details ([`{processor_class}`] uses
[`{image_processor_class}`] for processing images).
""",
"shape": "of shape `(batch_size, num_channels, image_size, image_size)`",
}
pixel_values_videos = {
"description": """
The tensors corresponding to the input video. Pixel values for videos can be obtained using
[`{video_processor_class}`]. See [`{video_processor_class}.__call__`] for details ([`{processor_class}`] uses
[`{video_processor_class}`] for processing videos).
""",
"shape": "of shape `(batch_size, num_frames, num_channels, frame_size, frame_size)`",
}
vision_feature_layer = {
"description": """
The index of the layer to select the vision feature. If multiple indices are provided,
the vision feature of the corresponding indices will be concatenated to form the
vision features.
""",
"shape": None,
}
vision_feature_select_strategy = {
"description": """
The feature selection strategy used to select the vision feature from the vision backbone.
Can be one of `"default"` or `"full"`.
""",
"shape": None,
}
image_sizes = {
"description": """
The sizes of the images in the batch, being (height, width) for each image.
""",
"shape": "of shape `(batch_size, 2)`",
}
pixel_mask = {
"description": """
Mask to avoid performing attention on padding pixel values. Mask values selected in `[0, 1]`:
- 1 for pixels that are real (i.e. **not masked**),
- 0 for pixels that are padding (i.e. **masked**).
[What are attention masks?](../glossary#attention-mask)
""",
"shape": "of shape `(batch_size, height, width)`",
}
input_features = {
"description": """
The tensors corresponding to the input audio features. Audio features can be obtained using
[`{feature_extractor_class}`]. See [`{feature_extractor_class}.__call__`] for details ([`{processor_class}`] uses
[`{feature_extractor_class}`] for processing audios).
""",
"shape": "of shape `(batch_size, sequence_length, feature_dim)`",
}
class ModelOutputArgs:
last_hidden_state = {
"description": """
Sequence of hidden-states at the output of the last layer of the model.
""",
"shape": "of shape `(batch_size, sequence_length, hidden_size)`",
}
past_key_values = {
"description": """
It is a [`~cache_utils.Cache`] instance. For more details, see our [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache).
Contains pre-computed hidden-states (key and values in the self-attention blocks and optionally if
`config.is_encoder_decoder=True` in the cross-attention blocks) that can be used (see `past_key_values`
input) to speed up sequential decoding.
""",
"shape": None,
"additional_info": "returned when `use_cache=True` is passed or when `config.use_cache=True`",
}
hidden_states = {
"description": """
Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
""",
"shape": None,
"additional_info": "returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`",
}
attentions = {
"description": """
Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
sequence_length)`.
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
heads.
""",
"shape": None,
"additional_info": "returned when `output_attentions=True` is passed or when `config.output_attentions=True`",
}
pooler_output = {
"description": """
Last layer hidden-state after a pooling operation on the spatial dimensions.
""",
"shape": "of shape `(batch_size, hidden_size)`",
}
cross_attentions = {
"description": """
Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
sequence_length)`.
Attentions weights of the decoder's cross-attention layer, after the attention softmax, used to compute the
weighted average in the cross-attention heads.
""",
"shape": None,
"additional_info": "returned when `output_attentions=True` is passed or when `config.output_attentions=True`",
}
decoder_hidden_states = {
"description": """
Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
Hidden-states of the decoder at the output of each layer plus the initial embedding outputs.
""",
"shape": None,
"additional_info": "returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`",
}
decoder_attentions = {
"description": """
Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
sequence_length)`.
Attentions weights of the decoder, after the attention softmax, used to compute the weighted average in the
self-attention heads.
""",
"shape": None,
"additional_info": "returned when `output_attentions=True` is passed or when `config.output_attentions=True`",
}
encoder_last_hidden_state = {
"description": """
Sequence of hidden-states at the output of the last layer of the encoder of the model.
""",
"shape": "of shape `(batch_size, sequence_length, hidden_size)`",
}
encoder_hidden_states = {
"description": """
Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
Hidden-states of the encoder at the output of each layer plus the initial embedding outputs.
""",
"shape": None,
"additional_info": "returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`",
}
encoder_attentions = {
"description": """
Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
sequence_length)`.
Attentions weights of the encoder, after the attention softmax, used to compute the weighted average in the
self-attention heads.
""",
"shape": None,
"additional_info": "returned when `output_attentions=True` is passed or when `config.output_attentions=True`",
}
router_logits = {
"description": """
Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, sequence_length, num_experts)`.
Router logits of the model, useful to compute the auxiliary loss for Mixture of Experts models.
""",
"shape": None,
"additional_info": "returned when `output_router_logits=True` is passed or when `config.add_router_probs=True`",
}
router_probs = {
"description": """
Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, sequence_length, num_experts)`.
Raw router probabilities that are computed by MoE routers, these terms are used to compute the auxiliary
loss and the z_loss for Mixture of Experts models.
""",
"shape": None,
"additional_info": "returned when `output_router_probs=True` and `config.add_router_probs=True` is passed or when `config.output_router_probs=True`",
}
z_loss = {
"description": """
z_loss for the sparse modules.
""",
"shape": None,
"additional_info": "returned when `labels` is provided",
}
aux_loss = {
"description": """
aux_loss for the sparse modules.
""",
"shape": None,
"additional_info": "returned when `labels` is provided",
}
start_logits = {
"description": """
Span-start scores (before SoftMax).
""",
"shape": "of shape `(batch_size, sequence_length)`",
}
end_logits = {
"description": """
Span-end scores (before SoftMax).
""",
"shape": "of shape `(batch_size, sequence_length)`",
}
feature_maps = {
"description": """
Feature maps of the stages.
""",
"shape": "of shape `(batch_size, num_channels, height, width)`",
}
reconstruction = {
"description": """
Reconstructed / completed images.
""",
"shape": "of shape `(batch_size, num_channels, height, width)`",
}
spectrogram = {
"description": """
The predicted spectrogram.
""",
"shape": "of shape `(batch_size, sequence_length, num_bins)`",
}
predicted_depth = {
"description": """
Predicted depth for each pixel.
""",
"shape": "of shape `(batch_size, height, width)`",
}
sequences = {
"description": """
Sampled values from the chosen distribution.
""",
"shape": "of shape `(batch_size, num_samples, prediction_length)` or `(batch_size, num_samples, prediction_length, input_size)`",
}
params = {
"description": """
Parameters of the chosen distribution.
""",
"shape": "of shape `(batch_size, num_samples, num_params)`",
}
loc = {
"description": """
Shift values of each time series' context window which is used to give the model inputs of the same
magnitude and then used to shift back to the original magnitude.
""",
"shape": "of shape `(batch_size,)` or `(batch_size, input_size)`",
}
scale = {
"description": """
Scaling values of each time series' context window which is used to give the model inputs of the same
magnitude and then used to rescale back to the original magnitude.
""",
"shape": "of shape `(batch_size,)` or `(batch_size, input_size)`",
}
static_features = {
"description": """
Static features of each time series' in a batch which are copied to the covariates at inference time.
""",
"shape": "of shape `(batch_size, feature size)`",
}
embeddings = {
"description": """
Utterance embeddings used for vector similarity-based retrieval.
""",
"shape": "of shape `(batch_size, config.xvector_output_dim)`",
}
extract_features = {
"description": """
Sequence of extracted feature vectors of the last convolutional layer of the model.
""",
"shape": "of shape `(batch_size, sequence_length, conv_dim[-1])`",
}
projection_state = {
"description": """
Text embeddings before the projection layer, used to mimic the last hidden state of the teacher encoder.
""",
"shape": "of shape `(batch_size,config.project_dim)`",
}
image_hidden_states = {
"description": """
Image hidden states of the model produced by the vision encoder and after projecting the last hidden state.
""",
"shape": "of shape `(batch_size, num_images, sequence_length, hidden_size)`",
}
video_hidden_states = {
"description": """
Video hidden states of the model produced by the vision encoder and after projecting the last hidden state.
""",
"shape": "of shape `(batch_size * num_frames, num_images, sequence_length, hidden_size)`",
}
class ClassDocstring:
Config = r"""
This is the configuration class to store the configuration of a {model_base_class}. It is used to instantiate a {model_name}
model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
defaults will yield a similar configuration to that of the [{model_checkpoint}](https://huggingface.co/{model_checkpoint})
Configuration objects inherit from [`PreTrainedConfig`] and can be used to control the model outputs. Read the
documentation from [`PreTrainedConfig`] for more information.
"""
PreTrainedModel = r"""
This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
etc.)
This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
and behavior.
"""
Model = r"""
The bare {model_name} Model outputting raw hidden-states without any specific head on top.
"""
ForPreTraining = r"""
The {model_name} Model with a specified pretraining head on top.
"""
Decoder = r"""
The bare {model_name} Decoder outputting raw hidden-states without any specific head on top.
"""
TextModel = r"""
The bare {model_name} Text Model outputting raw hidden-states without any specific head on to.
"""
ForSequenceClassification = r"""
The {model_name} Model with a sequence classification/regression head on top e.g. for GLUE tasks.
"""
ForQuestionAnswering = r"""
The {model_name} transformer with a span classification head on top for extractive question-answering tasks like
SQuAD (a linear layer on top of the hidden-states output to compute `span start logits` and `span end logits`).
"""
ForMultipleChoice = r"""
The {model_name} Model with a multiple choice classification head on top (a linear layer on top of the pooled output and a
softmax) e.g. for RocStories/SWAG tasks.
"""
ForMaskedLM = r"""
The {model_name} Model with a `language modeling` head on top."
"""
ForTokenClassification = r"""
The {model_name} transformer with a token classification head on top (a linear layer on top of the hidden-states
output) e.g. for Named-Entity-Recognition (NER) tasks.
"""
ForConditionalGeneration = r"""
The {model_name} Model for token generation conditioned on other modalities (e.g. image-text-to-text generation).
"""
ForCausalLM = r"""
The {model_name} Model for causal language modeling.
"""
Backbone = r"""
The {model_name} backbone.
"""
ForImageClassification = r"""
The {model_name} Model with an image classification head on top e.g. for ImageNet.
"""
ForSemanticSegmentation = r"""
The {model_name} Model with a semantic segmentation head on top e.g. for ADE20K, CityScapes.
"""
ForAudioClassification = r"""
The {model_name} Model with an audio classification head on top (a linear layer on top of the pooled
output).
"""
ForAudioFrameClassification = r"""
The {model_name} Model with a frame classification head on top for tasks like Speaker Diarization.
"""
ForPrediction = r"""
The {model_name} Model with a distribution head on top for time-series forecasting.
"""
WithProjection = r"""
The {model_name} Model with a projection layer on top (a linear layer on top of the pooled output).
"""
class ClassAttrs:
# fmt: off
base_model_prefix = r"""
A string indicating the attribute associated to the base model in derived classes of the same architecture adding modules on top of the base model.
"""
supports_gradient_checkpointing = r"""
Whether the model supports gradient checkpointing or not. Gradient checkpointing is a memory-saving technique that trades compute for memory, by storing only a subset of activations (checkpoints) and recomputing the activations that are not stored during the backward pass.
"""
_no_split_modules = r"""
Layers of modules that should not be split across devices should be added to `_no_split_modules`. This can be useful for modules that contains skip connections or other operations that are not compatible with splitting the module across devices. Setting this attribute will enable the use of `device_map="auto"` in the `from_pretrained` method.
"""
_skip_keys_device_placement = r"""
A list of keys to ignore when moving inputs or outputs between devices when using the `accelerate` library.
"""
_supports_flash_attn = r"""
Whether the model's attention implementation supports FlashAttention.
"""
_supports_sdpa = r"""
Whether the model's attention implementation supports SDPA (Scaled Dot Product Attention).
"""
_supports_flex_attn = r"""
Whether the model's attention implementation supports FlexAttention.
"""
_can_compile_fullgraph = r"""
Whether the model can `torch.compile` fullgraph without graph breaks. Models will auto-compile if this flag is set to `True`
in inference, if a compilable cache is used.
"""
_supports_attention_backend = r"""
Whether the model supports attention interface functions. This flag signal that the model can be used as an efficient backend in TGI and vLLM.
"""
_tied_weights_keys = r"""
A list of `state_dict` keys that are potentially tied to another key in the state_dict.
"""
# fmt: on
ARGS_TO_IGNORE = {"self", "kwargs", "args", "deprecated_arguments"}
ARGS_TO_RENAME = {"_out_features": "out_features", "_out_indices": "out_indices"}
def get_indent_level(func):
# Use this instead of `inspect.getsource(func)` as getsource can be very slow
return (len(func.__qualname__.split(".")) - 1) * 4
def equalize_indent(docstring: str, indent_level: int) -> str:
"""
Adjust the indentation of a docstring to match the specified indent level.
"""
prefix = " " * indent_level
# Uses splitlines() (no keepends) to match previous behaviour that dropped
# any trailing newline via the old splitlines() + "\n".join() + textwrap.indent path.
return "\n".join(prefix + line.lstrip() if line.strip() else "" for line in docstring.splitlines())
def set_min_indent(docstring: str, indent_level: int) -> str:
"""
Adjust the indentation of a docstring to match the specified indent level.
"""
# Equivalent to textwrap.dedent + textwrap.indent but avoids the two regex
# passes that textwrap uses internally (one per call in dedent, one in indent).
lines = docstring.split("\n")
min_indent = min(
(len(line) - len(line.lstrip()) for line in lines if line.strip()),
default=0,
)
prefix = " " * indent_level
return "\n".join(prefix + line[min_indent:] if line.strip() else "" for line in lines)
def parse_shape(docstring):
match = _re_shape.search(docstring)
if match:
return " " + match.group(1)
return None
def parse_default(docstring):
match = _re_default.search(docstring)
if match:
return " " + match.group(1)
return None
def parse_docstring(docstring, max_indent_level=0, return_intro=False):
"""
Parse the docstring to extract the Args section and return it as a dictionary.
The docstring is expected to be in the format:
Args:
arg1 (type):
Description of arg1.
arg2 (type):
Description of arg2.
# This function will also return the remaining part of the docstring after the Args section.
Returns:/Example:
...
"""
match = _re_example_or_return.search(docstring)
if match:
remainder_docstring = docstring[match.start() :]
docstring = docstring[: match.start()]
else:
remainder_docstring = ""
args_match = _re_args_section.search(docstring)
# still try to find args description in the docstring, if args are not preceded by "Args:"
docstring_intro = None
if args_match:
docstring_intro = docstring[: args_match.start()]
if docstring_intro.split("\n")[-1].strip() == '"""':
docstring_intro = "\n".join(docstring_intro.split("\n")[:-1])
if docstring_intro.split("\n")[0].strip() == 'r"""' or docstring_intro.split("\n")[0].strip() == '"""':
docstring_intro = "\n".join(docstring_intro.split("\n")[1:])
if docstring_intro.strip() == "":
docstring_intro = None
args_section = args_match.group(1).lstrip("\n") if args_match else docstring
if args_section.split("\n")[-1].strip() == '"""':
args_section = "\n".join(args_section.split("\n")[:-1])
if args_section.split("\n")[0].strip() == 'r"""' or args_section.split("\n")[0].strip() == '"""':
args_section = "\n".join(args_section.split("\n")[1:])
args_section = set_min_indent(args_section, 0)
params = {}
if args_section:
# Use the pre-compiled pattern (max_indent_level is always 0 at all call
# sites; if a non-zero value is ever needed, compile a fresh pattern).
if max_indent_level == 0:
param_pattern = _re_param
else:
param_pattern = re.compile(
# |--- Group 1 ---|| Group 2 ||- Group 3 -||---------- Group 4 ----------|
rf"^\s{{0,{max_indent_level}}}(\w+)\s*\(\s*([^, \)]*)(\s*.*?)\s*\)\s*:\s*((?:(?!\n^\s{{0,{max_indent_level}}}\w+\s*\().)*)",
re.DOTALL | re.MULTILINE,
)
for match in param_pattern.finditer(args_section):
param_name = match.group(1)
param_type = match.group(2)
additional_info = match.group(3)
optional = "optional" in additional_info
shape = parse_shape(additional_info)
default = parse_default(additional_info)
param_description = match.group(4).strip()
# indent the first line of param_description to 4 spaces:
param_description = " " * 4 + param_description
param_description = f"\n{param_description}"
params[param_name] = {
"type": param_type,
"description": param_description,
"optional": optional,
"shape": shape,
"default": default,
"additional_info": additional_info,
}
if params and remainder_docstring:
remainder_docstring = "\n" + remainder_docstring
remainder_docstring = set_min_indent(remainder_docstring, 0)
if return_intro:
return params, remainder_docstring, docstring_intro
return params, remainder_docstring
def contains_type(type_hint, target_type) -> tuple[bool, object | None]:
"""
Check if a "nested" type hint contains a specific target type,
return the first-level type containing the target_type if found.
"""
args = get_args(type_hint)
if args == ():
try:
return issubclass(type_hint, target_type), type_hint
except Exception:
return issubclass(type(type_hint), target_type), type_hint
found_type_tuple = [contains_type(arg, target_type)[0] for arg in args]
found_type = any(found_type_tuple)
if found_type:
type_hint = args[found_type_tuple.index(True)]
return found_type, type_hint
def get_model_name(obj):
"""
Get the model name from the file path of the object.
"""
path = inspect.getsourcefile(obj)
if path is None:
return None
if path.split(os.path.sep)[-3] != "models":
return None
file_name = path.split(os.path.sep)[-1]
model_name_lowercase_from_folder = path.split(os.path.sep)[-2]
model_name_lowercase_from_file = None
for file_type in AUTODOC_FILES:
start = file_type.split("*")[0]
end = file_type.split("*")[-1] if "*" in file_type else ""
if file_name.startswith(start) and file_name.endswith(end):
model_name_lowercase_from_file = file_name[len(start) : -len(end)]
break
if model_name_lowercase_from_file and model_name_lowercase_from_folder != model_name_lowercase_from_file:
from transformers.models.auto.configuration_auto import SPECIAL_MODEL_TYPE_TO_MODULE_NAME
if (
model_name_lowercase_from_file in SPECIAL_MODEL_TYPE_TO_MODULE_NAME
or model_name_lowercase_from_file.replace("_", "-") in SPECIAL_MODEL_TYPE_TO_MODULE_NAME
):
return model_name_lowercase_from_file
return model_name_lowercase_from_folder
return model_name_lowercase_from_folder
def generate_processor_intro(cls) -> str:
"""
Generate the intro docstring for a processor class based on its attributes.
Args:
cls: Processor class to generate intro for
Returns:
str: Generated intro text
"""
class_name = cls.__name__
# Get attributes and their corresponding class names
attributes = cls.get_attributes()
if not attributes:
return ""
# Build list of component names and their classes
components = []
component_classes = []
for attr in attributes:
# Get the class name for this attribute
class_attr = f"{attr}_class"
# Format attribute name for display
attr_display = attr.replace("_", " ")
components.append(attr_display)
component_classes.append(f"[`{{{class_attr}}}`]")
if not components:
return ""
# Generate the intro text
if len(components) == 1:
components_text = f"a {components[0]}"
classes_text = component_classes[0]
classes_text_short = component_classes[0].replace("[`", "[`~")
elif len(components) == 2:
components_text = f"a {components[0]} and a {components[1]}"
classes_text = f"{component_classes[0]} and {component_classes[1]}"
classes_text_short = (
f"{component_classes[0].replace('[`', '[`~')} and {component_classes[1].replace('[`', '[`~')}"
)
else:
components_text = ", ".join(f"a {c}" for c in components[:-1]) + f", and a {components[-1]}"
classes_text = ", ".join(component_classes[:-1]) + f", and {component_classes[-1]}"
classes_short = [c.replace("[`", "[`~") for c in component_classes]
classes_text_short = ", ".join(classes_short[:-1]) + f", and {classes_short[-1]}"
intro = f"""Constructs a {class_name} which wraps {components_text} into a single processor.
[`{class_name}`] offers all the functionalities of {classes_text}. See the
{classes_text_short} for more information.
"""
return intro
def get_placeholders_dict(placeholders: set[str], model_name: str) -> Mapping[str, str | None]:
"""
Get the dictionary of placeholders for the given model name.
"""
# import here to avoid circular import
from transformers.models import auto as auto_module
placeholders_dict = {}
for placeholder in placeholders:
# Infer placeholders from the model name and the auto modules
if placeholder in PLACEHOLDER_TO_AUTO_MODULE:
try:
place_holder_value = getattr(
getattr(auto_module, PLACEHOLDER_TO_AUTO_MODULE[placeholder][0]),
PLACEHOLDER_TO_AUTO_MODULE[placeholder][1],
).get(model_name, None)
except ImportError:
# In case a library is not installed, we don't want to fail the docstring generation
place_holder_value = None
if place_holder_value is not None:
if isinstance(place_holder_value, (list, tuple)):
place_holder_value = (
place_holder_value[-1] if place_holder_value[-1] is not None else place_holder_value[0]
)
placeholders_dict[placeholder] = place_holder_value if place_holder_value is not None else placeholder
else:
placeholders_dict[placeholder] = placeholder
return placeholders_dict
def format_args_docstring(docstring: str, model_name: str) -> str:
"""
Replaces placeholders such as {image_processor_class} in the docstring with the actual values,
deducted from the model name and the auto modules.
"""
# first check if there are any placeholders in the docstring, if not return it as is
placeholders = set(_re_placeholders.findall(docstring))
if not placeholders:
return docstring
# get the placeholders dictionary for the given model name
placeholders_dict = get_placeholders_dict(placeholders, model_name)
# replace the placeholders in the docstring with the values from the placeholders_dict
for placeholder, value in placeholders_dict.items():
if isinstance(value, dict) and placeholder == "image_processor_class":
value = value.get("torchvision", value.get("pil", None))
if placeholder is not None:
docstring = docstring.replace(f"{{{placeholder}}}", value)
return docstring
def get_args_doc_from_source(args_classes: object | list[object]) -> dict:
if isinstance(args_classes, (list, tuple)):
return _merge_args_dicts(tuple(args_classes))
return args_classes.__dict__
@lru_cache(maxsize=16)
def _merge_args_dicts(args_classes_tuple: tuple) -> dict:
"""Cached merger of args-doc dicts. The input classes are static so caching is safe."""
result = {}
for cls in args_classes_tuple:
result.update(cls.__dict__)
return result
def get_checkpoint_from_config_class(config_class):
checkpoint = None
# source code of `config_class`
# config_source = inspect.getsource(config_class)
config_source = config_class.__doc__
if not config_source:
return None
checkpoints = _re_checkpoint.findall(config_source)
# Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
# For example, `('google-bert/bert-base-uncased', 'https://huggingface.co/google-bert/bert-base-uncased')`
for ckpt_name, ckpt_link in checkpoints:
# allow the link to end with `/`
ckpt_link = ckpt_link.removesuffix("/")
# verify the checkpoint name corresponds to the checkpoint link
ckpt_link_from_name = f"https://huggingface.co/{ckpt_name}"
if ckpt_link == ckpt_link_from_name:
checkpoint = ckpt_name
break
return checkpoint
def add_intro_docstring(func, class_name, indent_level=0):
intro_docstring = ""
if func.__name__ == "forward":
intro_docstring = rf"""The [`{class_name}`] forward method, overrides the `__call__` special method.
<Tip>
Although the recipe for forward pass needs to be defined within this function, one should call the [`Module`]
instance afterwards instead of this since the former takes care of running the pre and post processing steps while
the latter silently ignores them.
</Tip>
"""
intro_docstring = equalize_indent(intro_docstring, indent_level + 4)
return intro_docstring
def _get_model_info(func, parent_class):
"""
Extract model information from a function or its parent class.
Args:
func (`function`): The function to extract information from
parent_class (`class`): Optional parent class of the function
"""
# import here to avoid circular import
from transformers.models import auto as auto_module
# Get model name from either parent class or function
if parent_class is not None:
model_name_lowercase = get_model_name(parent_class)
else:
model_name_lowercase = get_model_name(func)
# Normalize model name if needed
if model_name_lowercase and model_name_lowercase not in getattr(
getattr(auto_module, PLACEHOLDER_TO_AUTO_MODULE["config_class"][0]),
PLACEHOLDER_TO_AUTO_MODULE["config_class"][1],
):
model_name_lowercase = model_name_lowercase.replace("_", "-")
# Get class name from function's qualified name
class_name = func.__qualname__.split(".")[0]
# Get config class for the model
if model_name_lowercase is None:
config_class = None
else:
try:
config_class = getattr(
getattr(auto_module, PLACEHOLDER_TO_AUTO_MODULE["config_class"][0]),
PLACEHOLDER_TO_AUTO_MODULE["config_class"][1],
)[model_name_lowercase]
except KeyError:
if model_name_lowercase in HARDCODED_CONFIG_FOR_MODELS:
config_class = HARDCODED_CONFIG_FOR_MODELS[model_name_lowercase]
else:
config_class = "ModelConfig"
print(
f"[ERROR] Config not found for {model_name_lowercase}. You can manually add it to HARDCODED_CONFIG_FOR_MODELS in utils/auto_docstring.py"
)
return model_name_lowercase, class_name, config_class
def _format_type_annotation_recursive(type_hint):
"""
Recursively format a type annotation object as a string, preserving generic type arguments.
This is an internal helper used by process_type_annotation for the type object path.
Args:
type_hint: A type annotation object
Returns:
str: Formatted type string
"""
# Handle special cases
if type_hint is type(...) or type_hint is Ellipsis:
return "..."
# Note: NoneType handling is done later to preserve "NoneType" in Union[] but "None" in | syntax
# Check if this is a generic type (e.g., list[str], dict[str, int])
origin = get_origin(type_hint)
args = get_args(type_hint)
if origin is not None and args:
# This is a generic type - format it with its arguments
# Get the origin type name
if hasattr(origin, "__module__") and hasattr(origin, "__name__"):
# Clean up module name - need to handle both 'typing.' prefix and just 'typing'
module_name = origin.__module__
if module_name in ("typing", "types", "builtins"):
module_name = ""
else:
module_name = (
module_name.replace("transformers.", "~")
.replace("typing.", "")
.replace("types.", "")
.replace("builtins.", "")
)
if module_name:
origin_str = f"{module_name}.{origin.__name__}"
else:
origin_str = origin.__name__
else:
origin_str = str(origin)
# Handle special origin types
if origin_str == "UnionType":
# Python 3.13's X | Y syntax - format it nicely
arg_strs = [_format_type_annotation_recursive(arg) for arg in args]
return " | ".join(arg_strs)
# Special handling for Annotated[Union[...], ...] and Annotated[UnionType[...], ...]
# Check if first arg is a Union/UnionType and format it specially
if origin_str == "Annotated" and args:
first_arg_origin = get_origin(args[0])
# Check if it's a UnionType (modern | syntax) or Union (old Union[] syntax)
if first_arg_origin is UnionType:
# Modern union type - format as X | Y | Z (with None not NoneType)
union_args = get_args(args[0])
union_strs = []
for arg in union_args:
if arg is type(None):
union_strs.append("None") # Modern syntax uses "None"
else:
union_strs.append(_format_type_annotation_recursive(arg))
formatted_union = " | ".join(union_strs)
# Include the rest of the Annotated metadata
remaining_args = [_format_type_annotation_recursive(arg) for arg in args[1:]]
all_args = [formatted_union] + remaining_args
return f"{origin_str}[{', '.join(all_args)}]"
elif first_arg_origin is Union:
# Old-style Union - format as Union[X, Y, Z]
union_args = get_args(args[0])
union_strs = [_format_type_annotation_recursive(arg) for arg in union_args]
formatted_union = f"Union[{', '.join(union_strs)}]"
# Include the rest of the Annotated metadata
remaining_args = [_format_type_annotation_recursive(arg) for arg in args[1:]]
all_args = [formatted_union] + remaining_args
return f"{origin_str}[{', '.join(all_args)}]"
# Recursively format the generic arguments
arg_strs = [_format_type_annotation_recursive(arg) for arg in args]
return f"{origin_str}[{', '.join(arg_strs)}]"
elif hasattr(type_hint, "__module__") and hasattr(type_hint, "__name__"):
# Simple type with module and name
# Clean up module name - need to handle both 'typing.' prefix and just 'typing'
module_name = type_hint.__module__
if module_name in ("typing", "types", "builtins"):
module_name = ""
else:
module_name = (
module_name.replace("transformers.", "~")
.replace("typing.", "")
.replace("types.", "")
.replace("builtins.", "")
)
if module_name:
type_name = f"{module_name}.{type_hint.__name__}"
else:
type_name = type_hint.__name__
return type_name
else:
# Fallback to string representation
type_str = str(type_hint)
# Clean up ForwardRef
if "ForwardRef" in type_str:
type_str = _re_forward_ref.sub(r"\1", type_str)
# Clean up module prefixes
type_str = type_str.replace("typing.", "").replace("types.", "")
return type_str
def process_type_annotation(type_input, param_name: str | None = None) -> tuple[str, bool]:
"""
Unified function to process and format a parameter's type annotation.
This function intelligently handles both type objects (from inspect.Parameter.annotation)
and string representations of types. It will:
- Use type introspection when given a type object (preserves generic arguments)
- Parse string representations when that's all that's available
- Always return a formatted type string and optional flag
Handles various type representations including:
- Type objects with generics (e.g., list[str], Optional[int])
- Union types (both Union[X, Y] and X | Y syntax)
- Modern union syntax with | (e.g., "bool | None")
- Complex typing constructs (Union, Optional, Annotated, etc.)
- Generic types with brackets
- Class type strings
- Simple types and module paths
Args:
type_input: Either a type annotation object or a string representation of a type
param_name (`str | None`): The parameter name (used for legacy module path handling)
Returns:
tuple[str, bool]: (formatted_type_string, is_optional)
"""
optional = False
# Path 1: Type object (best approach - preserves generic type information)
if not isinstance(type_input, str):
# Handle None type
if type_input is None or type_input is type(None):
return "None", True
# Handle Union types and modern UnionType (X | Y)
if get_origin(type_input) is Union or get_origin(type_input) is UnionType:
subtypes = get_args(type_input)
out_str = []
for subtype in subtypes:
if subtype is type(None):
optional = True
continue
formatted_type = _format_type_annotation_recursive(subtype)
out_str.append(formatted_type)
if not out_str:
return "", optional
elif len(out_str) == 1:
return out_str[0], optional
else:
return f"Union[{', '.join(out_str)}]", optional
# Single type (not a Union)
formatted_type = _format_type_annotation_recursive(type_input)
return formatted_type, optional
# Path 2: String representation (fallback when we only have strings)
param_type = type_input
# Handle Union types with | syntax
if " | " in param_type:
# Modern union syntax (e.g., "bool | None")
parts = [p.strip() for p in param_type.split(" | ")]
if "None" in parts:
optional = True
parts = [p for p in parts if p != "None"]
param_type = " | ".join(parts) if parts else ""
# Clean up module prefixes including typing
param_type = "".join(param_type.split("typing.")).replace("transformers.", "~").replace("builtins.", "")
elif "typing" in param_type or "Union[" in param_type or "Optional[" in param_type or "[" in param_type:
# Complex typing construct or generic type - clean up typing module references
param_type = "".join(param_type.split("typing.")).replace("transformers.", "~")
elif "<class '" in param_type:
# This is a class type like "<class 'module.ClassName'>" - should NOT append param_name
param_type = (
param_type.replace("transformers.", "~").replace("builtins.", "").replace("<class '", "").replace("'>", "")
)
else:
# Simple type or module path - only append param_name if it looks like a module path
# This is legacy behavior for backwards compatibility
if param_name and "." in param_type and not param_type.split(".")[-1][0].isupper():
# Looks like a module path ending with an attribute
param_type = f"{param_type.replace('transformers.', '~').replace('builtins', '')}.{param_name}"
else:
# Simple type name, don't append param_name
param_type = param_type.replace("transformers.", "~").replace("builtins.", "")
# Clean up ForwardRef
if "ForwardRef" in param_type:
param_type = _re_forward_ref.sub(r"\1", param_type)
# Handle Optional wrapper
if "Optional" in param_type:
param_type = _re_optional.sub(r"\1", param_type)
optional = True
return param_type, optional
def _process_parameter_type(param):
"""
Process and format a parameter's type annotation from an inspect.Parameter object.
Args:
param (`inspect.Parameter`): The parameter from the function signature
Returns:
tuple[str, bool]: (formatted_type_string, is_optional)
"""
if param.annotation == inspect.Parameter.empty:
return "", False
# Use the unified function to process the type annotation
formatted_type, optional = process_type_annotation(param.annotation)
# Check if parameter has a default value (makes it optional)
if param.default is not inspect.Parameter.empty:
optional = True
return formatted_type, optional
def _get_parameter_info(param_name, documented_params, source_args_dict, param_type, optional):
"""
Get parameter documentation details from the appropriate source.
Tensor shape, optional status and description are taken from the custom docstring in priority if available.
Type is taken from the function signature first, then from the custom docstring if missing from the signature
Args:
param_name (`str`): Name of the parameter
documented_params (`dict`): Dictionary of documented parameters (manually specified in the docstring)
source_args_dict (`dict`): Default source args dictionary to use if not in documented_params
param_type (`str`): Current parameter type (may be updated)
optional (`bool`): Whether the parameter is optional (may be updated)
"""
description = None
shape = None
shape_string = ""
is_documented = True
additional_info = None
optional_string = r", *optional*" if optional else ""
if param_name in documented_params:
# Parameter is documented in the function's docstring
if (
param_type == ""
and documented_params[param_name].get("type", None) is not None
or documented_params[param_name]["additional_info"]
):
param_type = documented_params[param_name]["type"]
optional = documented_params[param_name]["optional"]
shape = documented_params[param_name].get("shape", None)
shape_string = shape if shape else ""
additional_info = documented_params[param_name]["additional_info"] or ""
description = f"{documented_params[param_name]['description']}\n"
elif param_name in source_args_dict:
# Parameter is documented in ModelArgs or ImageProcessorArgs
param_type = source_args_dict[param_name].get("type", param_type)
shape = source_args_dict[param_name].get("shape", None)
shape_string = " " + shape if shape else ""
description = source_args_dict[param_name]["description"]
additional_info = source_args_dict[param_name].get("additional_info", None)
if additional_info:
additional_info = shape_string + optional_string + ", " + additional_info
else:
# Parameter is not documented
is_documented = False
return param_type, optional_string, shape_string, additional_info, description, is_documented
def _process_regular_parameters(
sig,
func,
class_name,
documented_params,
indent_level,
undocumented_parameters,
source_args_dict,
parent_class,
allowed_params=None,
):
"""
Process all regular parameters (not kwargs parameters) from the function signature.
Args:
sig (`inspect.Signature`): Function signature
func (`function`): Function the parameters belong to
class_name (`str`): Name of the class
documented_params (`dict`): Dictionary of parameters that are already documented
indent_level (`int`): Indentation level
undocumented_parameters (`list`): List to append undocumented parameters to
"""
docstring = ""
# Check if this is a processor by inspecting class hierarchy
is_processor = _is_processor_class(func, parent_class)
# Use appropriate args source based on whether it's a processor or not
if source_args_dict is None:
if is_processor:
source_args_dict = get_args_doc_from_source([ModelArgs, ImageProcessorArgs, ProcessorArgs])
else:
source_args_dict = get_args_doc_from_source([ModelArgs, ImageProcessorArgs])
missing_args = {}
for param_name, param in sig.parameters.items():
# Skip parameters that should be ignored
if (
param_name in ARGS_TO_IGNORE
or param_name.startswith("_") # Private/internal params (e.g. ClassVar-backed fields in configs)
or param.kind == inspect.Parameter.VAR_POSITIONAL
or param.kind == inspect.Parameter.VAR_KEYWORD
):
continue
# When a filter is active (e.g. config classes: only own annotations), skip inherited params
if allowed_params is not None and param_name not in allowed_params:
continue
# When a filter is active (e.g. config classes: only own annotations), skip inherited params
if allowed_params is not None and param_name not in allowed_params:
continue
param_name = ARGS_TO_RENAME.get(param_name, param_name)
# Process parameter type and optional status
param_type, optional = _process_parameter_type(param)
# Check for default value
param_default = ""
if param.default != inspect._empty and param.default is not None:
param_default = f", defaults to `{str(param.default)}`"
param_type, optional_string, shape_string, additional_info, description, is_documented = _get_parameter_info(
param_name, documented_params, source_args_dict, param_type, optional
)
if is_documented:
if param_name == "config":
if param_type == "":
param_type = f"[`{class_name}`]"
else:
param_type = f"[`{param_type.split('.')[-1]}`]"
# elif param_type == "" and False: # TODO: Enforce typing for all parameters
# print(f"[ERROR] {param_name} for {func.__qualname__} in file {func.__code__.co_filename} has no type")
param_type = param_type if "`" in param_type else f"`{param_type}`"
# Format the parameter docstring
if additional_info:
param_docstring = f"{param_name} ({param_type}{additional_info}):{description}"
else:
param_docstring = (
f"{param_name} ({param_type}{shape_string}{optional_string}{param_default}):{description}"
)
docstring += set_min_indent(
param_docstring,
indent_level + 8,
)
else:
missing_args[param_name] = {
"type": param_type if param_type else "<fill_type>",
"optional": optional,
"shape": shape_string,
"description": description if description else "\n <fill_description>",
"default": param_default,
}
# Try to get the correct source file; for classes decorated with @strict (huggingface_hub),
# func.__code__.co_filename points to the wrapper in huggingface_hub, not the config file.
try:
if parent_class is not None:
_source_file = inspect.getsourcefile(parent_class) or func.__code__.co_filename
else:
_source_file = inspect.getsourcefile(inspect.unwrap(func)) or func.__code__.co_filename
except (TypeError, OSError):
_source_file = func.__code__.co_filename
undocumented_parameters.append(
f"[ERROR] `{param_name}` is part of {func.__qualname__}'s signature, but not documented. Make sure to add it to the docstring of the function in {_source_file}."
)
return docstring, missing_args
def find_sig_line(lines, line_end):
parenthesis_count = 0
sig_line_end = line_end
found_sig = False
while not found_sig:
for char in lines[sig_line_end]:
if char == "(":
parenthesis_count += 1
elif char == ")":
parenthesis_count -= 1
if parenthesis_count == 0:
found_sig = True
break
sig_line_end += 1
return sig_line_end
def _is_image_processor_class(func, parent_class):
"""
Check if a function belongs to a ProcessorMixin class.
Uses two methods:
1. Check parent_class inheritance (if provided)
2. Check if the source file is named processing_*.py (multimodal processors)
vs image_processing_*.py, video_processing_*.py, etc. (single-modality processors)
Args:
func: The function to check
parent_class: Optional parent class (if available)
Returns:
bool: True if this is a multimodal processor (inherits from ProcessorMixin), False otherwise
"""
# First, check if parent_class is provided and use it
if parent_class is not None:
return "BaseImageProcessor" in parent_class.__name__ or any(
"BaseImageProcessor" in base.__name__ for base in parent_class.__mro__
)
# If parent_class is None, check the filename
# Multimodal processors are in files named "processing_*.py"
# Single-modality processors are in "image_processing_*.py", "video_processing_*.py", etc.
try:
source_file = inspect.getsourcefile(func)
except TypeError:
return False
if not source_file:
return False
# Exception for DummyProcessorForTest
if func.__qualname__.split(".")[0] == "DummyForTestImageProcessorFast":
return True
filename = os.path.basename(source_file)
# Multimodal processors are implemented in processing_*.py modules
# (single-modality processors use image_processing_*, video_processing_*, etc.)self.
return filename.startswith("image_processing_") and filename.endswith(".py")
def _is_processor_class(func, parent_class):
"""
Check if a function belongs to a ProcessorMixin class.
Uses two methods:
1. Check parent_class inheritance (if provided)
2. Check if the source file is named processing_*.py (multimodal processors)
vs image_processing_*.py, video_processing_*.py, etc. (single-modality processors)
Args:
func: The function to check
parent_class: Optional parent class (if available)
Returns:
bool: True if this is a multimodal processor (inherits from ProcessorMixin), False otherwise
"""
# First, check if parent_class is provided and use it
if parent_class is not None:
return "ProcessorMixin" in parent_class.__name__ or any(
"ProcessorMixin" in base.__name__ for base in parent_class.__mro__
)
# If parent_class is None, check the filename
# Multimodal processors are in files named "processing_*.py"
# Single-modality processors are in "image_processing_*.py", "video_processing_*.py", etc.
try:
source_file = inspect.getsourcefile(func)
except TypeError:
return False
if not source_file:
return False
# Exception for DummyProcessorForTest
if func.__qualname__.split(".")[0] == "DummyProcessorForTest":
return True
filename = os.path.basename(source_file)
# Multimodal processors are implemented in processing_*.py modules
# (single-modality processors use image_processing_*, video_processing_*, etc.)self.
return filename.startswith("processing_") and filename.endswith(".py")
# Python < 3.12 fallback: naming heuristics when __orig_bases__ is not set (cpython#103699).
# Order matters: check ImageProcessorKwargs before ProcessorKwargs.
_BASIC_KWARGS_NAMES = frozenset({"ImagesKwargs", "ProcessingKwargs", "TextKwargs", "VideosKwargs", "AudioKwargs"})
_BASIC_KWARGS_CLASSES = None # Lazy-loaded name -> class mapping
def _get_base_kwargs_class_from_name(cls_name: str) -> str | None:
"""Map kwargs class name to base using naming conventions. Returns base class name or None."""
if cls_name in _BASIC_KWARGS_NAMES:
return cls_name
if "ImageProcessorKwargs" in cls_name or cls_name.endswith("ImagesKwargs"):
return "ImagesKwargs"
if "ProcessorKwargs" in cls_name:
return "ProcessingKwargs"
if "VideoProcessorKwargs" in cls_name or cls_name.endswith("VideosKwargs"):
return "VideosKwargs"
if "AudioProcessorKwargs" in cls_name or cls_name.endswith("AudioKwargs"):
return "AudioKwargs"
if "TextKwargs" in cls_name:
return "TextKwargs"
return None
def _get_base_kwargs_class(cls):
"""
Get the root/base TypedDict class by walking the inheritance chain.
For model-specific kwargs like ComplexProcessingKwargs(ProcessingKwargs), returns ProcessingKwargs.
For model-specific kwargs like DummyImageProcessorKwargs(ImagesKwargs), returns ImagesKwargs.
Compatibility: On Python < 3.12, non-generic TypedDict subclasses do not have __orig_bases__ set
(cpython#103699). We fall back to naming heuristics (e.g. *ImageProcessorKwargs -> ImagesKwargs).
"""
current = cls
while True:
bases = typing_extensions.get_original_bases(current)
parent = None
for base in bases:
if isinstance(base, type) and base not in (dict, object):
if getattr(base, "__name__", "") == "TypedDict" and getattr(base, "__module__", "") == "typing":
continue
parent = base
break
if parent is None:
# Python < 3.12 fallback: use naming heuristics
base_name = _get_base_kwargs_class_from_name(current.__name__)
if base_name is not None:
global _BASIC_KWARGS_CLASSES
if _BASIC_KWARGS_CLASSES is None:
from transformers.processing_utils import (
AudioKwargs,
ImagesKwargs,
ProcessingKwargs,
TextKwargs,
VideosKwargs,
)
_BASIC_KWARGS_CLASSES = {
"ImagesKwargs": ImagesKwargs,
"ProcessingKwargs": ProcessingKwargs,
"TextKwargs": TextKwargs,
"VideosKwargs": VideosKwargs,
"AudioKwargs": AudioKwargs,
}
parent = _BASIC_KWARGS_CLASSES[base_name]
if parent is None or parent == current:
return current
current = parent
def _process_kwargs_parameters(sig, func, parent_class, documented_kwargs, indent_level, undocumented_parameters):
"""
Process **kwargs parameters if needed.
Args:
sig (`inspect.Signature`): Function signature
func (`function`): Function the parameters belong to
parent_class (`class`): Parent class of the function
documented_kwargs (`dict`): Dictionary of kwargs that are already documented
indent_level (`int`): Indentation level
undocumented_parameters (`list`): List to append undocumented parameters to
Returns:
tuple[str, str]: (kwargs docstring, kwargs summary line to add after return_tensors)
"""
docstring = ""
kwargs_summary = ""
# Check if we need to add typed kwargs description to the docstring
unroll_kwargs = func.__name__ in UNROLL_KWARGS_METHODS
if not unroll_kwargs and parent_class is not None:
# Check if the function has a parent class with unroll kwargs
unroll_kwargs = any(
any(unroll_kwargs_class in base.__name__ for base in parent_class.__mro__)
for unroll_kwargs_class in UNROLL_KWARGS_CLASSES
)
if not unroll_kwargs:
return docstring, kwargs_summary
# Check if this is a processor by inspecting class hierarchy
is_processor = _is_processor_class(func, parent_class)
is_image_processor = _is_image_processor_class(func, parent_class)
# Use appropriate args source based on whether it's a processor or not
if is_processor:
source_args_dict = get_args_doc_from_source([ImageProcessorArgs, ProcessorArgs])
elif is_image_processor:
source_args_dict = get_args_doc_from_source(ImageProcessorArgs)
else:
raise ValueError(
f"Unrolling kwargs is not supported for {func.__name__} of {parent_class.__name__ if parent_class else 'None'} class"
)
# get all unpackable "kwargs" parameters
kwargs_parameters = [
kwargs_param
for _, kwargs_param in sig.parameters.items()
if kwargs_param.kind == inspect.Parameter.VAR_KEYWORD
]
for kwarg_param in kwargs_parameters:
# If kwargs not typed, skip
if kwarg_param.annotation == inspect.Parameter.empty:
continue
if not hasattr(kwarg_param.annotation, "__args__") or not hasattr(
kwarg_param.annotation.__args__[0], "__name__"
):
continue
if kwarg_param.annotation.__args__[0].__name__ not in BASIC_KWARGS_TYPES:
# Extract documentation for kwargs
kwargs_documentation = kwarg_param.annotation.__args__[0].__doc__
if kwargs_documentation is not None:
documented_kwargs = parse_docstring(kwargs_documentation)[0]
# Process each kwarg parameter
for param_name, param_type_annotation in kwarg_param.annotation.__args__[0].__annotations__.items():
# Handle nested kwargs structures for processors
if is_processor and param_name.endswith("_kwargs"):
# Check if this is a basic kwargs type that should be skipped
# Basic kwargs types are generic containers that shouldn't be documented as individual params
# Get the actual type (unwrap Optional if needed)
actual_type = param_type_annotation
type_name = getattr(param_type_annotation, "__name__", None)
if type_name is None and hasattr(param_type_annotation, "__origin__"):
# Handle Optional[Type] or Union cases
args = getattr(param_type_annotation, "__args__", ())
for arg in args:
if arg is not type(None):
actual_type = arg
type_name = getattr(arg, "__name__", None)
break
# Skip only if it's one of the basic kwargs types
if type_name in BASIC_KWARGS_TYPES:
continue
# Otherwise, unroll the custom typed kwargs
# Get the nested TypedDict's annotations
if hasattr(actual_type, "__annotations__"):
nested_kwargs_doc = getattr(actual_type, "__doc__", None)
documented_nested_kwargs = {}
if nested_kwargs_doc:
documented_nested_kwargs = parse_docstring(nested_kwargs_doc)[0]
# Only process fields that are documented in the custom kwargs class's own docstring
# This prevents showing too many inherited parameters
if not documented_nested_kwargs:
# No documentation in the custom kwargs class, skip unrolling
continue
# Process each field in the custom typed kwargs
for nested_param_name, nested_param_type in actual_type.__annotations__.items():
# Only document parameters that are explicitly documented in the TypedDict's docstring
if nested_param_name not in documented_nested_kwargs:
continue
nested_param_type_str, nested_optional = process_type_annotation(
nested_param_type, nested_param_name
)
# Check for default value
nested_param_default = ""
if parent_class is not None:
nested_param_default = str(getattr(parent_class, nested_param_name, ""))
nested_param_default = (
f", defaults to `{nested_param_default}`" if nested_param_default != "" else ""
)
# Only use the TypedDict's own docstring, not source_args_dict
# This prevents pulling in too many inherited parameters
(
nested_param_type_str,
nested_optional_string,
nested_shape_string,
nested_additional_info,
nested_description,
nested_is_documented,
) = _get_parameter_info(
nested_param_name,
documented_nested_kwargs,
{}, # Empty dict - only use TypedDict's own docstring
nested_param_type_str,
nested_optional,
)
# nested_is_documented should always be True here since we filter for it above
# Check if type is missing
if nested_param_type_str == "":
print(
f"🚨 {nested_param_name} for {type_name} in file {func.__code__.co_filename} has no type"
)
nested_param_type_str = (
nested_param_type_str if "`" in nested_param_type_str else f"`{nested_param_type_str}`"
)
# Format the parameter docstring (KWARGS_INDICATOR distinguishes from regular args)
if nested_additional_info:
docstring += set_min_indent(
f"{nested_param_name} ({nested_param_type_str}{KWARGS_INDICATOR}{nested_additional_info}):{nested_description}",
indent_level + 8,
)
else:
docstring += set_min_indent(
f"{nested_param_name} ({nested_param_type_str}{KWARGS_INDICATOR}{nested_shape_string}{nested_optional_string}{nested_param_default}):{nested_description}",
indent_level + 8,
)
# Skip processing the _kwargs parameter itself since we've processed its contents
continue
else:
# If we can't get annotations, skip this parameter
continue
if documented_kwargs and param_name not in documented_kwargs:
continue
param_type, optional = process_type_annotation(param_type_annotation, param_name)
# Check for default value
param_default = ""
if parent_class is not None:
param_default = str(getattr(parent_class, param_name, ""))
param_default = f", defaults to `{param_default}`" if param_default != "" else ""
param_type, optional_string, shape_string, additional_info, description, is_documented = (
_get_parameter_info(param_name, documented_kwargs, source_args_dict, param_type, optional)
)
if is_documented:
# Check if type is missing
if param_type == "":
print(
f"[ERROR] {param_name} for {kwarg_param.annotation.__args__[0].__qualname__} in file {func.__code__.co_filename} has no type"
)
param_type = param_type if "`" in param_type else f"`{param_type}`"
# Format the parameter docstring (KWARGS_INDICATOR distinguishes from regular args)
if additional_info:
docstring += set_min_indent(
f"{param_name} ({param_type}{KWARGS_INDICATOR}{additional_info}):{description}",
indent_level + 8,
)
else:
docstring += set_min_indent(
f"{param_name} ({param_type}{KWARGS_INDICATOR}{shape_string}{optional_string}{param_default}):{description}",
indent_level + 8,
)
else:
undocumented_parameters.append(
f"[ERROR] `{param_name}` is part of {kwarg_param.annotation.__args__[0].__qualname__}, but not documented. Make sure to add it to the docstring of the function in {func.__code__.co_filename}."
)
# Build **kwargs summary line (added after return_tensors in _process_parameters_section)
kwargs_annot_cls = kwarg_param.annotation.__args__[0]
kwargs_type_name = _get_base_kwargs_class(kwargs_annot_cls).__name__
kwargs_info = source_args_dict.get("__kwargs__", {})
kwargs_description = kwargs_info.get(
"description",
"Additional keyword arguments. Model-specific parameters are listed above.",
)
kwargs_summary = set_min_indent(
f"**kwargs ([`{kwargs_type_name}`], *optional*):{kwargs_description}",
indent_level + 8,
)
return docstring, kwargs_summary
def _add_return_tensors_to_docstring(func, parent_class, docstring, indent_level):
"""
Add return_tensors parameter documentation for processor __call__ methods if not already present.
Args:
func (`function`): Function being processed
parent_class (`class`): Parent class of the function
docstring (`str`): Current docstring being built
indent_level (`int`): Indentation level
Returns:
str: Updated docstring with return_tensors if applicable
"""
# Check if this is a processor __call__ method or an image processor preprocess method
is_processor_call = False
is_image_processor_preprocess = False
if func.__name__ == "__call__":
# Check if this is a processor by inspecting class hierarchy
is_processor_call = _is_processor_class(func, parent_class)
if func.__name__ == "preprocess":
is_image_processor_preprocess = _is_image_processor_class(func, parent_class)
# If it's a processor __call__ method or an image processor preprocess method and return_tensors is not already documented
if (is_processor_call or is_image_processor_preprocess) and "return_tensors" not in docstring:
# Get the return_tensors documentation from ImageProcessorArgs
source_args_dict = (
get_args_doc_from_source(ProcessorArgs)
if is_processor_call
else get_args_doc_from_source(ImageProcessorArgs)
)
return_tensors_info = source_args_dict["return_tensors"]
param_type = return_tensors_info.get("type", "`str` or [`~utils.TensorType`]")
description = return_tensors_info["description"]
# Format the parameter type
param_type = param_type if "`" in param_type else f"`{param_type}`"
# Format the parameter docstring
param_docstring = f"return_tensors ({param_type}, *optional*):{description}"
docstring += set_min_indent(param_docstring, indent_level + 8)
return docstring
def _process_parameters_section(
func_documentation,
sig,
func,
class_name,
model_name_lowercase,
parent_class,
indent_level,
source_args_dict,
allowed_params,
):
"""
Process the parameters section of the docstring.
Args:
func_documentation (`str`): Existing function documentation (manually specified in the docstring)
sig (`inspect.Signature`): Function signature
func (`function`): Function the parameters belong to
class_name (`str`): Name of the class the function belongs to
model_name_lowercase (`str`): Lowercase model name
parent_class (`class`): Parent class of the function (if any)
indent_level (`int`): Indentation level
"""
# Start Args section — constant string, min_indent is always 0, so skip set_min_indent
docstring = " " * (indent_level + 4) + "Args:\n"
undocumented_parameters = []
documented_params = {}
documented_kwargs = {}
# Parse existing docstring if available
if func_documentation is not None:
documented_params, func_documentation = parse_docstring(func_documentation)
# Process regular parameters
param_docstring, missing_args = _process_regular_parameters(
sig,
func,
class_name,
documented_params,
indent_level,
undocumented_parameters,
source_args_dict,
parent_class,
allowed_params,
)
docstring += param_docstring
# Process **kwargs parameters if needed
kwargs_docstring, kwargs_summary = _process_kwargs_parameters(
sig, func, parent_class, documented_kwargs, indent_level, undocumented_parameters
)
docstring += kwargs_docstring
# Add return_tensors for processor __call__ methods if not already present
docstring = _add_return_tensors_to_docstring(func, parent_class, docstring, indent_level)
# Add **kwargs summary line after return_tensors
docstring += kwargs_summary
# Report undocumented parameters
if len(undocumented_parameters) > 0:
print("\n".join(undocumented_parameters))
return docstring
def _prepare_return_docstring(output_type, config_class, add_intro=True):
"""
Prepare the return docstring from a ModelOutput class.
This is a robust replacement for the old _prepare_output_docstrings from doc.py,
using the same parsing and formatting methods as the rest of auto_docstring.
Args:
output_type: The ModelOutput class to generate documentation for
config_class (`str`): Config class for the model
add_intro (`bool`): Whether to add the introduction text
Returns:
str: Formatted return docstring
"""
output_docstring = output_type.__doc__
# If the class has no docstring, try to use the parent class's docstring
if output_docstring is None and hasattr(output_type, "__mro__"):
for base in output_type.__mro__[1:]: # Skip the class itself
if base.__doc__ is not None:
output_docstring = base.__doc__
break
if output_docstring is None:
if add_intro:
raise ValueError(
f"No docstring found for `{output_type.__name__}` or its parent classes. "
"Make sure the ModelOutput class or one of its parents has a docstring."
)
return ""
# Parse the output class docstring to extract parameters
documented_params, _ = parse_docstring(output_docstring)
if not documented_params and add_intro:
raise ValueError(
f"No `Args` or `Parameters` section is found in the docstring of `{output_type.__name__}`. "
"Make sure it has a docstring and contains either `Args` or `Parameters`."
)
# Build the return section
full_output_type, _ = process_type_annotation(output_type)
if add_intro:
# Import here to avoid circular import
from .doc import PT_RETURN_INTRODUCTION
intro = PT_RETURN_INTRODUCTION.format(full_output_type=full_output_type, config_class=config_class)
else:
intro = f"Returns:\n `{full_output_type}`"
if documented_params:
intro += ":\n"
else:
intro += "\n"
# Build the parameters section
params_text = ""
if documented_params:
for param_name, param_info in documented_params.items():
param_type = param_info.get("type", "")
param_description = param_info.get("description", "").strip()
additional_info = param_info.get("additional_info", "")
# Handle types with unbalanced backticks due to nested parentheses
# The parse_docstring function splits types like `tuple(torch.FloatTensor)` incorrectly
# so we need to reconstruct the complete type by grabbing the closing part from additional_info
if param_type.startswith("`") and not param_type.endswith("`"):
# Find the closing backtick in additional_info
closing_backtick_idx = additional_info.find("`")
if closing_backtick_idx != -1:
# Grab everything up to and including the closing backtick
param_type += additional_info[: closing_backtick_idx + 1]
# Remove that part from additional_info
additional_info = additional_info[closing_backtick_idx + 1 :]
# Strip backticks from type to add them back consistently
param_type = param_type.strip("`")
# Use process_type_annotation to ensure consistent type formatting
# This applies the same formatting rules as the rest of auto_docstring
if param_type:
param_type, _ = process_type_annotation(param_type)
# Build the parameter line
if additional_info:
# additional_info contains shape and optional status
param_line = f"- **{param_name}** (`{param_type}`{additional_info}) -- {param_description}"
else:
param_line = f"- **{param_name}** (`{param_type}`) -- {param_description}"
# Handle multi-line descriptions:
# Split the description to handle continuations with proper indentation
lines = param_line.split("\n")
formatted_lines = []
for i, line in enumerate(lines):
if i == 0:
# First line gets no extra indent (just the bullet point)
formatted_lines.append(line)
else:
# Continuation lines: strip existing indentation and add 2 spaces (relative to the bullet)
formatted_lines.append(" " + line.lstrip())
param_text = "\n".join(formatted_lines)
# Indent everything to 4 spaces and append with newline
param_text_indented = set_min_indent(param_text, 4)
params_text += param_text_indented + "\n"
result = intro + params_text
return result
def _process_returns_section(func_documentation, sig, config_class, indent_level):
"""
Process the returns section of the docstring.
Args:
func_documentation (`str`): Existing function documentation (manually specified in the docstring)
sig (`inspect.Signature`): Function signature
config_class (`str`): Config class for the model
indent_level (`int`): Indentation level
"""
return_docstring = ""
# Extract returns section from existing docstring if available
if func_documentation is not None and (match_start := _re_return.search(func_documentation)) is not None:
match_end = _re_example.search(func_documentation)
if match_end:
return_docstring = func_documentation[match_start.start() : match_end.start()]
func_documentation = func_documentation[match_end.start() :]
else:
return_docstring = func_documentation[match_start.start() :]
func_documentation = ""
return_docstring = set_min_indent(return_docstring, indent_level + 4)
# Otherwise, generate return docstring from return annotation if available
elif sig.return_annotation is not None and sig.return_annotation != inspect._empty:
add_intro, return_annotation = contains_type(sig.return_annotation, ModelOutput)
return_docstring = _prepare_return_docstring(return_annotation, config_class, add_intro=add_intro)
# PT_RETURN_INTRODUCTION already starts with \n, so only add blank line if it doesn't start with one
if not return_docstring.startswith("\n"):
return_docstring = "\n" + return_docstring
return_docstring = set_min_indent(return_docstring, indent_level + 4)
return return_docstring, func_documentation
def _process_example_section(
func_documentation, func, parent_class, class_name, model_name_lowercase, config_class, checkpoint, indent_level
):
"""
Process the example section of the docstring.
Args:
func_documentation (`str`): Existing function documentation (manually specified in the docstring)
func (`function`): Function being processed
parent_class (`class`): Parent class of the function
class_name (`str`): Name of the class
model_name_lowercase (`str`): Lowercase model name
config_class (`str`): Config class for the model
checkpoint: Checkpoint to use in examples
indent_level (`int`): Indentation level
"""
# Import here to avoid circular import
from transformers.models import auto as auto_module
example_docstring = ""
# Use existing example section if available (with or without an "Example:" header)
if func_documentation is not None and (match := _re_example.search(func_documentation)):
example_docstring = func_documentation[match.start() :]
example_docstring = "\n" + set_min_indent(example_docstring, indent_level + 4)
# Skip examples for processors
elif _is_processor_class(func, parent_class):
# Processors don't get auto-generated examples
return example_docstring
# No examples for __init__ methods or if the class is not a model
elif parent_class is None and model_name_lowercase is not None:
global _re_model_task
if _re_model_task is None:
_re_model_task = re.compile(rf"({'|'.join(PT_SAMPLE_DOCSTRINGS.keys())})")
model_task = _re_model_task.search(class_name)
CONFIG_MAPPING = auto_module.configuration_auto.CONFIG_MAPPING
# Get checkpoint example
if (checkpoint_example := checkpoint) is None:
try:
checkpoint_example = get_checkpoint_from_config_class(CONFIG_MAPPING[model_name_lowercase])
except KeyError:
# For models with inconsistent lowercase model name
if model_name_lowercase in HARDCODED_CONFIG_FOR_MODELS:
CONFIG_MAPPING_NAMES = auto_module.configuration_auto.CONFIG_MAPPING_NAMES
config_class_name = HARDCODED_CONFIG_FOR_MODELS[model_name_lowercase]
if config_class_name in CONFIG_MAPPING_NAMES.values():
model_name_for_auto_config = [
k for k, v in CONFIG_MAPPING_NAMES.items() if v == config_class_name
][0]
if model_name_for_auto_config in CONFIG_MAPPING:
checkpoint_example = get_checkpoint_from_config_class(
CONFIG_MAPPING[model_name_for_auto_config]
)
# Add example based on model task
if model_task is not None:
if checkpoint_example is not None:
example_annotation = ""
task = model_task.group()
example_annotation = PT_SAMPLE_DOCSTRINGS[task].format(
model_class=class_name,
checkpoint=checkpoint_example,
expected_output="...",
expected_loss="...",
qa_target_start_index=14,
qa_target_end_index=15,
mask="<mask>",
)
example_docstring = set_min_indent(example_annotation, indent_level + 4)
else:
print(
f"[ERROR] No checkpoint found for {class_name}.{func.__name__}. Please add a `checkpoint` arg to `auto_docstring` or add one in {config_class}'s docstring"
)
else:
# Check if the model is in a pipeline to get an example
for name_model_list_for_task in MODELS_TO_PIPELINE:
try:
model_list_for_task = getattr(auto_module.modeling_auto, name_model_list_for_task)
except (ImportError, AttributeError):
continue
if class_name in model_list_for_task.values():
pipeline_name = MODELS_TO_PIPELINE[name_model_list_for_task]
example_annotation = PIPELINE_TASKS_TO_SAMPLE_DOCSTRINGS[pipeline_name].format(
model_class=class_name,
checkpoint=checkpoint_example,
expected_output="...",
expected_loss="...",
qa_target_start_index=14,
qa_target_end_index=15,
)
example_docstring = set_min_indent(example_annotation, indent_level + 4)
break
return example_docstring
def auto_method_docstring(
func,
parent_class=None,
custom_intro=None,
custom_args=None,
checkpoint=None,
source_args_dict=None,
allowed_params=None,
):
"""
Wrapper that automatically generates docstring.
"""
# Use inspect to retrieve the method's signature
sig = inspect.signature(func)
indent_level = get_indent_level(func) if not parent_class else get_indent_level(parent_class)
# Get model information
model_name_lowercase, class_name, config_class = _get_model_info(func, parent_class)
func_documentation = func.__doc__
if custom_args is not None and func_documentation is not None:
func_documentation = "\n" + set_min_indent(custom_args.strip("\n"), 0) + "\n" + func_documentation
elif custom_args is not None:
func_documentation = "\n" + set_min_indent(custom_args.strip("\n"), 0)
# Add intro to the docstring before args description if needed
if custom_intro is not None:
docstring = set_min_indent(custom_intro, indent_level + 4)
if not docstring.strip().endswith("\n"):
docstring += "\n"
else:
docstring = add_intro_docstring(func, class_name=class_name, indent_level=indent_level)
# Process Parameters section
docstring += _process_parameters_section(
func_documentation,
sig,
func,
class_name,
model_name_lowercase,
parent_class,
indent_level,
source_args_dict,
allowed_params,
)
# Process Returns section
return_docstring, func_documentation = _process_returns_section(
func_documentation, sig, config_class, indent_level
)
docstring += return_docstring
# Process Example section
example_docstring = _process_example_section(
func_documentation,
func,
parent_class,
class_name,
model_name_lowercase,
config_class,
checkpoint,
indent_level,
)
docstring += example_docstring
# Format the docstring with the placeholders
docstring = format_args_docstring(docstring, model_name_lowercase)
# Assign the dynamically generated docstring to the wrapper function
func.__doc__ = docstring
return func
def auto_class_docstring(cls, custom_intro=None, custom_args=None, checkpoint=None):
"""
Wrapper that automatically generates a docstring for classes based on their attributes and methods.
"""
# import here to avoid circular import
from transformers.models import auto as auto_module
is_dataclass = False
is_processor = False
is_config = False
is_image_processor = False
docstring_init = ""
docstring_args = ""
if "PreTrainedModel" in (x.__name__ for x in cls.__mro__):
docstring_init = auto_method_docstring(
cls.__init__, parent_class=cls, custom_args=custom_args, checkpoint=checkpoint
).__doc__.replace("Args:", "Parameters:")
elif "ProcessorMixin" in (x.__name__ for x in cls.__mro__):
is_processor = True
docstring_init = auto_method_docstring(
cls.__init__,
parent_class=cls,
custom_args=custom_args,
checkpoint=checkpoint,
source_args_dict=get_args_doc_from_source([ModelArgs, ImageProcessorArgs, ProcessorArgs]),
).__doc__.replace("Args:", "Parameters:")
elif "ModelOutput" in (x.__name__ for x in cls.__mro__):
# We have a data class
is_dataclass = True
doc_class = cls.__doc__
if custom_args is None and doc_class:
custom_args = doc_class
docstring_args = auto_method_docstring(
cls.__init__,
parent_class=cls,
custom_args=custom_args,
checkpoint=checkpoint,
source_args_dict=get_args_doc_from_source(ModelOutputArgs),
).__doc__
elif any("BaseImageProcessor" in x.__name__ for x in cls.__mro__):
is_image_processor = True
docstring_init = auto_method_docstring(
cls.__init__,
parent_class=cls,
custom_args=custom_args,
checkpoint=checkpoint,
source_args_dict=get_args_doc_from_source(ImageProcessorArgs),
).__doc__
elif "PreTrainedConfig" in (x.__name__ for x in cls.__mro__):
is_config = True
doc_class = cls.__doc__
if custom_args is None and doc_class:
custom_args = doc_class
# Collect all non-ClassVar annotations from the class and its ancestors up to
# (but not including) PreTrainedConfig. This allows inherited params from intermediate
# config base classes to be documented, while naturally excluding PreTrainedConfig-specific
# quasi-ClassVar params (e.g. `transformers_version`, `architectures`).
own_config_params = set()
for ancestor in cls.__mro__:
if ancestor.__name__ == "PreTrainedConfig":
break
own_config_params |= {
k for k, v in getattr(ancestor, "__annotations__", {}).items() if get_origin(v) is not ClassVar
}
allowed_params = own_config_params if own_config_params else None
docstring_init = auto_method_docstring(
cls.__init__,
parent_class=cls,
custom_args=custom_args,
checkpoint=checkpoint,
source_args_dict=get_args_doc_from_source([ConfigArgs]),
allowed_params=allowed_params,
).__doc__
indent_level = get_indent_level(cls)
model_name_lowercase = get_model_name(cls)
model_name_title = " ".join([k.title() for k in model_name_lowercase.split("_")]) if model_name_lowercase else None
model_base_class = f"{model_name_title.title()}Model" if model_name_title is not None else None
if model_name_lowercase is not None:
try:
model_base_class = getattr(
getattr(auto_module, PLACEHOLDER_TO_AUTO_MODULE["model_class"][0]),
PLACEHOLDER_TO_AUTO_MODULE["model_class"][1],
)[model_name_lowercase]
except KeyError:
pass
except ImportError:
# In some environments, certain model classes might not be available. In that case, we can skip this part.
pass
if model_name_lowercase and model_name_lowercase not in getattr(
getattr(auto_module, PLACEHOLDER_TO_AUTO_MODULE["config_class"][0]),
PLACEHOLDER_TO_AUTO_MODULE["config_class"][1],
):
model_name_lowercase = model_name_lowercase.replace("_", "-")
name = re.findall(rf"({'|'.join(ClassDocstring.__dict__.keys())})$", cls.__name__)
if name == [] and custom_intro is None and not is_dataclass and not is_processor and not is_image_processor:
raise ValueError(
f"`{cls.__name__}` is not registered in the auto doc. Here are the available classes: {ClassDocstring.__dict__.keys()}.\n"
"Add a `custom_intro` to the decorator if you want to use `auto_docstring` on a class not registered in the auto doc."
)
if name != [] or custom_intro is not None or is_config or is_dataclass or is_processor or is_image_processor:
name = name[0] if name else None
formatting_kwargs = {"model_name": model_name_title}
if name == "Config":
formatting_kwargs.update({"model_base_class": model_base_class, "model_checkpoint": checkpoint})
if custom_intro is not None:
pre_block = equalize_indent(custom_intro, indent_level)
if not pre_block.endswith("\n"):
pre_block += "\n"
elif is_processor:
# Generate processor intro dynamically
pre_block = generate_processor_intro(cls)
if pre_block:
pre_block = equalize_indent(pre_block, indent_level)
pre_block = format_args_docstring(pre_block, model_name_lowercase)
elif is_image_processor:
pre_block = r"Constructs a {image_processor_class} image processor."
if pre_block:
pre_block = equalize_indent(pre_block, indent_level)
pre_block = format_args_docstring(pre_block, model_name_lowercase)
elif model_name_title is None or name is None:
pre_block = ""
else:
pre_block = getattr(ClassDocstring, name).format(**formatting_kwargs)
# Start building the docstring
docstring = set_min_indent(f"{pre_block}", indent_level) if len(pre_block) else ""
if name != "PreTrainedModel" and "PreTrainedModel" in (x.__name__ for x in cls.__mro__):
docstring += set_min_indent(f"{ClassDocstring.PreTrainedModel}", indent_level)
# Add the __init__ docstring
if docstring_init:
docstring += set_min_indent(f"\n{docstring_init}", indent_level)
elif is_dataclass or is_config:
# No init function, we have a data class
docstring += docstring_args if docstring_args else "\nArgs:\n"
source_args_dict = get_args_doc_from_source(ModelOutputArgs)
doc_class = cls.__doc__ if cls.__doc__ else ""
documented_kwargs = parse_docstring(doc_class)[0]
for param_name, param_type_annotation in cls.__annotations__.items():
param_type, optional = process_type_annotation(param_type_annotation, param_name)
# Check for default value
param_default = ""
param_default = str(getattr(cls, param_name, ""))
param_default = f", defaults to `{param_default}`" if param_default != "" else ""
param_type, optional_string, shape_string, additional_info, description, is_documented = (
_get_parameter_info(param_name, documented_kwargs, source_args_dict, param_type, optional)
)
if is_documented:
# Check if type is missing
if param_type == "":
print(
f"[ERROR] {param_name} for {cls.__qualname__} in file {cls.__code__.co_filename} has no type"
)
param_type = param_type if "`" in param_type else f"`{param_type}`"
# Format the parameter docstring
if additional_info:
docstring += set_min_indent(
f"{param_name} ({param_type}{additional_info}):{description}",
indent_level + 8,
)
else:
docstring += set_min_indent(
f"{param_name} ({param_type}{shape_string}{optional_string}{param_default}):{description}",
indent_level + 8,
)
# TODO (Yoni): Add support for Attributes section in docs
else:
print(
f"You used `@auto_class_docstring` decorator on `{cls.__name__}` but this class is not part of the AutoMappings. Remove the decorator"
)
# Assign the dynamically generated docstring to the wrapper class
cls.__doc__ = docstring
return cls
def auto_docstring(obj=None, *, custom_intro=None, custom_args=None, checkpoint=None):
r"""
Automatically generates comprehensive docstrings for model classes and methods in the Transformers library.
This decorator reduces boilerplate by automatically including standard argument descriptions while allowing
overrides to add new or custom arguments. It inspects function signatures, retrieves predefined docstrings
for common arguments (like `input_ids`, `attention_mask`, etc.), and generates complete documentation
including examples and return value descriptions.
For complete documentation and examples, read this [guide](https://huggingface.co/docs/transformers/auto_docstring).
Examples of usage:
Basic usage (no parameters):
```python
@auto_docstring
class MyAwesomeModel(PreTrainedModel):
def __init__(self, config, custom_parameter: int = 10):
r'''
custom_parameter (`int`, *optional*, defaults to 10):
Description of the custom parameter for MyAwesomeModel.
'''
super().__init__(config)
self.custom_parameter = custom_parameter
```
Using `custom_intro` with a class:
```python
@auto_docstring(
custom_intro="This model implements a novel attention mechanism for improved performance."
)
class MySpecialModel(PreTrainedModel):
def __init__(self, config, attention_type: str = "standard"):
r'''
attention_type (`str`, *optional*, defaults to "standard"):
Type of attention mechanism to use.
'''
super().__init__(config)
```
Using `custom_intro` with a method, and specify custom arguments and example directly in the docstring:
```python
@auto_docstring(
custom_intro="Performs forward pass with enhanced attention computation."
)
def forward(
self,
input_ids: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None,
):
r'''
custom_parameter (`int`, *optional*, defaults to 10):
Description of the custom parameter for MyAwesomeModel.
Example:
```python
>>> model = MyAwesomeModel(config)
>>> model.forward(input_ids=torch.tensor([1, 2, 3]), attention_mask=torch.tensor([1, 1, 1]))
```
'''
```
Using `custom_args` to define reusable arguments:
```python
VISION_ARGS = r'''
pixel_values (`torch.FloatTensor`, *optional*):
Pixel values of the input images.
image_features (`torch.FloatTensor`, *optional*):
Pre-computed image features for efficient processing.
'''
@auto_docstring(custom_args=VISION_ARGS)
def encode_images(self, pixel_values=None, image_features=None):
# ... method implementation
```
Combining `custom_intro` and `custom_args`:
```python
MULTIMODAL_ARGS = r'''
vision_features (`torch.FloatTensor`, *optional*):
Pre-extracted vision features from the vision encoder.
fusion_strategy (`str`, *optional*, defaults to "concat"):
Strategy for fusing text and vision modalities.
'''
@auto_docstring(
custom_intro="Processes multimodal inputs combining text and vision.",
custom_args=MULTIMODAL_ARGS
)
def forward(
self,
input_ids,
attention_mask=None,
vision_features=None,
fusion_strategy="concat"
):
# ... multimodal processing
```
Using with ModelOutput classes:
```python
@dataclass
@auto_docstring(
custom_intro="Custom model outputs with additional fields."
)
class MyModelOutput(ImageClassifierOutput):
r'''
loss (`torch.FloatTensor`, *optional*):
The loss of the model.
custom_field (`torch.FloatTensor` of shape `(batch_size, hidden_size)`, *optional*):
A custom output field specific to this model.
'''
# Standard fields like hidden_states, logits, attentions etc. can be automatically documented
# However, given that the loss docstring is often different per model, you should document it above
loss: Optional[torch.FloatTensor] = None
logits: Optional[torch.FloatTensor] = None
hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None
attentions: Optional[tuple[torch.FloatTensor, ...]] = None
custom_field: Optional[torch.FloatTensor] = None
```
Args:
custom_intro (`str`, *optional*):
Custom introduction text to add to the docstring. This replaces the default
introduction text generated by the decorator before the Args section. Use this to describe what
makes your model or method special.
custom_args (`str`, *optional*):
Custom argument documentation in docstring format. This allows you to define
argument descriptions once and reuse them across multiple methods. The format should follow the
standard docstring convention: `arg_name (`type`, *optional*, defaults to `value`): Description.`
checkpoint (`str`, *optional*):
Checkpoint name to use in examples within the docstring. This is typically
automatically inferred from the model configuration class, but can be overridden if needed for
custom examples.
Note:
- Standard arguments (`input_ids`, `attention_mask`, `pixel_values`, etc.) are automatically documented
from predefined descriptions and should not be redefined unless their behavior differs in your model.
- New or custom arguments should be documented in the method's docstring using the `r''' '''` block
or passed via the `custom_args` parameter.
- For model classes, the decorator derives parameter descriptions from the `__init__` method's signature
and docstring.
- Return value documentation is automatically generated for methods that return ModelOutput subclasses.
"""
def auto_docstring_decorator(obj):
if len(obj.__qualname__.split(".")) > 1:
return auto_method_docstring(
obj, custom_args=custom_args, custom_intro=custom_intro, checkpoint=checkpoint
)
else:
return auto_class_docstring(obj, custom_args=custom_args, custom_intro=custom_intro, checkpoint=checkpoint)
if obj:
return auto_docstring_decorator(obj)
return auto_docstring_decorator
|