File size: 133,665 Bytes
f440f03 | 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 | """Modeļa apmācība ar HuggingFace Trainer."""
from __future__ import annotations
import asyncio
import hashlib
import importlib.util
import inspect
import json
import logging
import math
import os
import re
import subprocess
import sys
import tempfile
import time
from contextlib import suppress
from dataclasses import dataclass, replace
from datetime import UTC, datetime
from importlib.metadata import PackageNotFoundError
from importlib.metadata import version as get_installed_package_version
from pathlib import Path
from typing import Any
from maris_core.data.datasets import HFDatasetError, load_hf_dataset
from maris_core.data.preprocessing import record_to_training_text
from maris_core.data.quality import (
DatasetQualityGateConfig,
DatasetQualityReport,
DatasetQualitySplitReport,
apply_quality_gate_to_records,
build_dataset_quality_report,
)
from maris_core.data.scoring import (
DatasetBenchmarkFeedback,
DatasetScoringConfig,
DatasetScoringReport,
DatasetScoringSplitReport,
apply_scoring_to_records,
build_benchmark_feedback_artifact,
build_dataset_scoring_report,
load_benchmark_feedback,
)
from maris_core.orchestrator.routing import build_system_prompt
from maris_core.text.benchmark import (
build_chat_benchmark_history_artifact,
build_chat_benchmark_manifest,
build_chat_benchmark_regression_report,
load_chat_benchmark_dataset,
run_chat_benchmark_with_responder,
select_chat_benchmark_cases,
)
from maris_core.text.generate import (
_extract_response_text,
_extract_usage_tokens,
call_generation_pipeline,
)
from maris_core.training.config import (
DEFAULT_BENCHMARK_NAME,
DEFAULT_BRANCH_DATASET_FILTER_RULES,
TrainingConfig,
load_training_config,
)
from maris_core.training.hf_compat import (
MARIS_COMPATIBILITY_ARTIFACT_NAME,
apply_maris_compatibility_identity,
maris_hf_compatible_path,
write_maris_compatibility_artifact,
)
from maris_core.training.preferences import (
PreferenceExample,
build_blind_side_by_side_artifact,
build_human_eval_summary,
load_preference_dataset,
summarize_preference_dataset,
)
from maris_core.utils.env import get_env_any
logger = logging.getLogger(__name__)
TEXT_TRAINABLE_BRANCHES = {"master", "coder", "planner"}
MAX_LOSS_FOR_PERPLEXITY = 20.0
MARIS_ORIGIN_NAME = "Maris AI"
MARIS_FRAMEWORK_NAME = "maris-ai-core"
TOKENIZER_LOAD_RETRY_EXCEPTIONS = (ImportError, OSError, RuntimeError, TypeError, ValueError)
SANITIZED_ARTIFACT_KEYS = (
"_name_or_path",
"name_or_path",
"base_model_name_or_path",
)
IDENTITY_TEXT_KEYS = frozenset(
{
"base_model_lineage",
"base_model_name",
"chat_template",
"default_system_prompt",
"description",
"model_name",
"system_prompt",
}
)
TEXT_SANITIZED_ARTIFACT_EXTENSIONS = frozenset({".jinja", ".md", ".template", ".txt"})
FOREIGN_MODEL_REFERENCE_RE = re.compile(
r"(?i)\b(?:"
r"allenai|anthropic|cohereforai|deepseek-ai|google|huggingfaceh4|meta-llama|"
r"microsoft|mistralai|nousresearch|openai|qwen|stabilityai|tiiuae|tinyllama"
r")/[A-Za-z0-9][\w.-]*\b"
)
FOREIGN_AI_BRAND_PATTERNS = (
re.compile(r"(?i)\bTinyLlama\b"),
re.compile(r"(?i)\bDeepSeek\b"),
re.compile(r"(?i)\bMistral\b"),
re.compile(r"(?i)\bLlama\b"),
re.compile(r"(?i)\bQwen\b"),
re.compile(r"(?i)\bChatGPT\b"),
re.compile(r"(?i)\bClaude\b"),
re.compile(r"(?i)\bGemini\b"),
re.compile(r"(?i)\bOpenAI\b"),
re.compile(r"(?i)\bAnthropic\b"),
)
MARIS_IDENTITY_VARIANT_RE = re.compile(
rf"{re.escape(MARIS_ORIGIN_NAME)}(?:[-_/][A-Za-z0-9][\w.-]*)+"
)
TRAINING_ARGUMENT_ALIASES = {
"evaluation_strategy": "eval_strategy",
}
MODEL_CARD_TAGS = ("maris-ai", "maris-origin", "conversational-ai")
PEFT_ADAPTER_TYPES = {"lora", "qlora"}
PREFERENCE_OPTIMIZATION_TYPES = {"dpo", "orpo"}
MODEL_SIZE_HINT_RE = re.compile(r"(\d+(?:\.\d+)?)B")
LARGE_MODEL_RESOURCE_THRESHOLD_B = 30.0
GIANT_MODEL_AUTO_QLORA_THRESHOLD_B = 70.0
LONG_CONTEXT_MIN_SEQ_LENGTH = 32_768
GIANT_MODEL_NAME_HINTS = (
"deepseek-v3",
"deepseek-r1",
"qwen3-coder-480b",
"405b",
"480b",
"671b",
)
# Reusable fingerprints for detecting a previously saved model or adapter inside
# persistent storage before starting the next training run.
LOCAL_TRAINING_ARTIFACT_FILES = (
"config.json",
"adapter_config.json",
"model.safetensors",
"pytorch_model.bin",
)
MODEL_SOURCE_FINGERPRINT_KEY = "model_source_fingerprint"
REPO_ROOT = Path(__file__).resolve().parents[3]
def _emit_training_progress_event(event: str, **payload: Any) -> None:
body = {
"maris_training_event": True,
"event": event,
**{key: value for key, value in payload.items() if value is not None},
}
print(json.dumps(body, ensure_ascii=False), flush=True)
def _build_training_progress_label(
*,
stage: str,
epoch: float | None = None,
total_epochs: int | None = None,
step: int | None = None,
total_steps: int | None = None,
) -> str:
if stage == "saving":
return "Saglabā modeļa artefaktus"
if stage == "evaluating":
return "Veic eval metriku aprēķinu"
if stage == "publishing":
return "Publicē modeli origin repozitorijā"
if stage == "benchmarking":
return "Palaiž benchmark un release gate pārbaudes"
if stage == "starting":
return "Inicializē treniņu"
if step is not None and total_steps:
return f"Trenē modeli · solis {step}/{total_steps}"
if epoch is not None and total_epochs:
return f"Trenē modeli · epoha {epoch:g}/{total_epochs}"
if epoch is not None:
return f"Trenē modeli · epoha {epoch:g}"
return "Trenē modeli"
class MarisTrainingProgressCallback:
"""Emitē strukturētus progress notikumus Space UI monitoringam."""
def __init__(self, *, total_epochs: int | None = None) -> None:
self.total_epochs = total_epochs
self.started_monotonic: float | None = None
def on_train_begin(self, args, state, control, **kwargs) -> None: # noqa: ANN001, D401
self.started_monotonic = time.monotonic()
total_steps = int(state.max_steps) if getattr(state, "max_steps", 0) else None
_emit_training_progress_event(
"train_begin",
stage="starting",
label=_build_training_progress_label(stage="starting"),
epoch=_coerce_float(getattr(state, "epoch", None)),
total_epochs=self.total_epochs,
step=_coerce_int(getattr(state, "global_step", None)),
total_steps=total_steps,
learning_rate=getattr(args, "learning_rate", None),
)
def on_log(self, args, state, control, logs=None, **kwargs) -> None: # noqa: ANN001, D401
logs = dict(logs or {})
step = _coerce_int(getattr(state, "global_step", None))
total_steps = int(state.max_steps) if getattr(state, "max_steps", 0) else None
epoch = _coerce_float(logs.get("epoch", getattr(state, "epoch", None)))
learning_rate = logs.get("learning_rate", getattr(args, "learning_rate", None))
_emit_training_progress_event(
"log",
stage="training",
label=_build_training_progress_label(
stage="training",
epoch=epoch,
total_epochs=self.total_epochs,
step=step,
total_steps=total_steps,
),
epoch=epoch,
total_epochs=self.total_epochs,
step=step,
total_steps=total_steps,
loss=_coerce_float(logs.get("loss")),
eval_loss=_coerce_float(logs.get("eval_loss")),
learning_rate=_coerce_float(learning_rate),
eta_seconds=self._estimate_eta_seconds(step=step, total_steps=total_steps),
)
def on_evaluate(self, args, state, control, metrics=None, **kwargs) -> None: # noqa: ANN001, D401
metrics = dict(metrics or {})
step = _coerce_int(getattr(state, "global_step", None))
total_steps = int(state.max_steps) if getattr(state, "max_steps", 0) else None
epoch = _coerce_float(getattr(state, "epoch", None))
_emit_training_progress_event(
"evaluate",
stage="evaluating",
label=_build_training_progress_label(stage="evaluating"),
epoch=epoch,
total_epochs=self.total_epochs,
step=step,
total_steps=total_steps,
eval_loss=_coerce_float(metrics.get("eval_loss")),
eta_seconds=self._estimate_eta_seconds(step=step, total_steps=total_steps),
)
def on_save(self, args, state, control, **kwargs) -> None: # noqa: ANN001, D401
_emit_training_progress_event(
"save",
stage="saving",
label=_build_training_progress_label(stage="saving"),
epoch=_coerce_float(getattr(state, "epoch", None)),
total_epochs=self.total_epochs,
step=_coerce_int(getattr(state, "global_step", None)),
total_steps=int(state.max_steps) if getattr(state, "max_steps", 0) else None,
output_dir=getattr(args, "output_dir", None),
)
def on_train_end(self, args, state, control, **kwargs) -> None: # noqa: ANN001, D401
_emit_training_progress_event(
"train_end",
stage="saving",
label="Treniņš pabeigts, sagatavo artefaktus",
epoch=_coerce_float(getattr(state, "epoch", None)),
total_epochs=self.total_epochs,
step=_coerce_int(getattr(state, "global_step", None)),
total_steps=int(state.max_steps) if getattr(state, "max_steps", 0) else None,
)
def _estimate_eta_seconds(self, *, step: int | None, total_steps: int | None) -> float | None:
if self.started_monotonic is None or not step or not total_steps or step <= 0:
return None
elapsed = time.monotonic() - self.started_monotonic
if elapsed <= 0:
return None
remaining_steps = max(total_steps - step, 0)
if remaining_steps <= 0:
return 0.0
return elapsed / step * remaining_steps
def _coerce_float(value: Any) -> float | None:
if isinstance(value, bool) or value in (None, ""):
return None
try:
return float(value)
except (TypeError, ValueError):
return None
def _coerce_int(value: Any) -> int | None:
if isinstance(value, bool) or value in (None, ""):
return None
try:
return int(float(value))
except (TypeError, ValueError):
return None
@dataclass(slots=True, frozen=True)
class DatasetBranchFilterReport:
split_name: str
branch_name: str
input_records: int
kept_records: int
dropped_records: int
@dataclass(slots=True, frozen=True)
class BranchFilterSignals:
explicit_branches: frozenset[str] = frozenset()
record_types: frozenset[str] = frozenset()
task_types: frozenset[str] = frozenset()
languages: frozenset[str] = frozenset()
repo_context_terms: frozenset[str] = frozenset()
presence_keys: frozenset[str] = frozenset()
def _get_split(dataset: Any, *names: str) -> Any:
for name in names:
if isinstance(dataset, dict) and name in dataset:
return dataset[name]
getter = getattr(dataset, "get", None)
if callable(getter):
candidate = getter(name)
if candidate is not None:
return candidate
return None
def _dataset_split_names(dataset: Any) -> list[str]:
if isinstance(dataset, dict):
return [str(name) for name in dataset]
keys = getattr(dataset, "keys", None)
if callable(keys):
try:
return [str(name) for name in keys()]
except Exception: # noqa: BLE001
pass
return [
name
for name in ("train", "validation", "eval", "test")
if _get_split(dataset, name) is not None
]
def _merge_dataset_splits(splits: list[Any]) -> Any:
merged_splits = [split for split in splits if split is not None]
if not merged_splits:
return None
if len(merged_splits) == 1:
return merged_splits[0]
if all(type(split).__module__.startswith("datasets") for split in merged_splits):
try:
from datasets import concatenate_datasets # type: ignore
return concatenate_datasets(merged_splits)
except Exception as exc: # noqa: BLE001
logger.warning(
"HF split concatenation neizdevās; pārslēdzamies uz ierakstu līmeņa merge: %s",
exc,
)
records: list[dict[str, Any]] = []
for split in merged_splits:
records.extend(_materialize_split_records(split))
return _rebuild_split_like(merged_splits[0], records)
def _load_combined_hf_dataset(repo_ids: list[str]) -> Any:
datasets_by_repo = [(repo_id, load_hf_dataset(repo_id)) for repo_id in repo_ids]
split_names: list[str] = []
for _, dataset in datasets_by_repo:
for split_name in _dataset_split_names(dataset):
if split_name not in split_names:
split_names.append(split_name)
if not split_names:
raise ValueError("Neviens no norādītajiem dataset repo nesatur pieejamus splitus.")
return {
split_name: _merge_dataset_splits(
[_get_split(dataset, split_name) for _, dataset in datasets_by_repo]
)
for split_name in split_names
}
def _resolve_training_dataset_repos(config: TrainingConfig) -> list[str]:
return list(config.dataset_repos or [config.dataset_repo])
def _resolve_eval_dataset_repos(config: TrainingConfig) -> list[str]:
if config.eval_dataset_repos:
return list(config.eval_dataset_repos)
if config.eval_dataset_repo:
return [config.eval_dataset_repo]
return []
def _resolve_primary_eval_dataset_repo(config: TrainingConfig) -> str:
eval_dataset_repos = _resolve_eval_dataset_repos(config)
if eval_dataset_repos:
return eval_dataset_repos[0]
return config.eval_dataset_repo or config.dataset_repo
def _prepare_train_eval_splits(
dataset: Any,
config: TrainingConfig,
) -> tuple[Any, Any | None]:
train_split = _get_split(dataset, "train")
if train_split is None:
raise ValueError("Datasetam nav pieejams 'train' split apmācībai.")
eval_split = _get_split(dataset, "validation", "eval", "test")
if eval_split is not None:
return train_split, eval_split
if (
config.validation_split_ratio <= 0
or not hasattr(train_split, "train_test_split")
or len(train_split) < 2
):
return train_split, None
split_result = train_split.train_test_split(
test_size=config.validation_split_ratio,
seed=config.seed,
)
return split_result["train"], split_result["test"]
def _select_eval_split(dataset: Any, config: TrainingConfig, *, allow_train_fallback: bool) -> Any:
"""Atrod drošāko eval splitu novērtēšanai vai ārējam benchmark datasetam."""
train_split = _get_split(dataset, "train")
eval_split = _get_split(dataset, "validation", "eval", "test")
if eval_split is not None:
return eval_split
if allow_train_fallback and train_split is not None:
return train_split
if train_split is None:
raise ValueError("Datasetam nav pieejams ne 'train', ne eval split novērtēšanai.")
_, derived_eval_split = _prepare_train_eval_splits(dataset, config)
if derived_eval_split is None:
raise ValueError("Datasetam nav pieejams stabils eval split novērtēšanai.")
return derived_eval_split
def _tokenize_dataset(split: Any, tokenizer: Any, max_seq_length: int) -> Any:
if not hasattr(split, "map"):
raise TypeError("HF apmācībai nepieciešams datasets.Dataset objekts ar map() atbalstu.")
remove_columns = list(getattr(split, "column_names", []))
def tokenize_batch(batch: dict[str, list[Any]]) -> dict[str, Any]:
batch_size = len(next(iter(batch.values()))) if batch else 0
texts = []
for index in range(batch_size):
record = {key: values[index] for key, values in batch.items()}
texts.append(record_to_training_text(record, max_chars=max_seq_length * 8))
return tokenizer(
texts,
truncation=True,
max_length=max_seq_length,
padding=False,
)
return split.map(
tokenize_batch,
batched=True,
remove_columns=remove_columns,
desc="Tokenizing training data",
)
def _configure_tokenizer(tokenizer: Any, config: TrainingConfig) -> None:
if getattr(tokenizer, "pad_token", None) is None:
fallback_token = getattr(tokenizer, "eos_token", None) or getattr(
tokenizer, "unk_token", None
)
if fallback_token is not None:
tokenizer.pad_token = fallback_token
if (
getattr(tokenizer, "pad_token_id", None) is None
and getattr(tokenizer, "eos_token_id", None) is not None
):
tokenizer.pad_token_id = tokenizer.eos_token_id
current_model_max_length = getattr(tokenizer, "model_max_length", None)
if (
isinstance(current_model_max_length, int)
and 0 < current_model_max_length < config.max_seq_length
and (
_is_large_training_model(config.model_name)
or config.max_seq_length >= LONG_CONTEXT_MIN_SEQ_LENGTH
)
):
tokenizer.model_max_length = config.max_seq_length
def _uses_peft(config: TrainingConfig) -> bool:
return config.adapter_type in PEFT_ADAPTER_TYPES
def _uses_qlora(config: TrainingConfig) -> bool:
return config.adapter_type == "qlora"
def _uses_preference_optimization(config: TrainingConfig) -> bool:
return (
bool(config.preference_dataset_path)
and config.preference_optimization in PREFERENCE_OPTIMIZATION_TYPES
)
def _resolve_torch_dtype(dtype_name: str) -> Any | None:
normalized = dtype_name.strip().lower()
if not normalized:
return None
try:
import torch # type: ignore
except ImportError:
return None
return getattr(torch, normalized, None)
def _build_model_load_kwargs(config: TrainingConfig) -> dict[str, Any]:
model_load_kwargs: dict[str, Any] = {}
model_load_kwargs["trust_remote_code"] = True
model_load_kwargs["low_cpu_mem_usage"] = True
large_model = _is_large_training_model(config.model_name)
if large_model or _uses_qlora(config):
model_load_kwargs["device_map"] = "auto"
model_load_kwargs["offload_folder"] = str(Path(config.output_dir) / ".offload")
if not _uses_qlora(config):
return model_load_kwargs
try:
from transformers import BitsAndBytesConfig # type: ignore
except ImportError as exc:
raise ImportError(
"QLoRA apmācībai vajag transformers BitsAndBytesConfig un bitsandbytes atkarību."
) from exc
quantization_kwargs: dict[str, Any] = {
"load_in_4bit": True,
"bnb_4bit_quant_type": config.qlora_quant_type,
"bnb_4bit_use_double_quant": config.qlora_use_double_quant,
}
compute_dtype = _resolve_torch_dtype(config.qlora_compute_dtype)
if compute_dtype is not None:
quantization_kwargs["bnb_4bit_compute_dtype"] = compute_dtype
model_load_kwargs["quantization_config"] = BitsAndBytesConfig(**quantization_kwargs)
return model_load_kwargs
def _extract_model_size_billions(model_name: str) -> float | None:
matches = [float(match) for match in MODEL_SIZE_HINT_RE.findall(model_name)]
if matches:
return max(matches)
normalized = model_name.strip().lower()
if any(hint in normalized for hint in GIANT_MODEL_NAME_HINTS):
return GIANT_MODEL_AUTO_QLORA_THRESHOLD_B
return None
def _is_large_training_model(model_name: str) -> bool:
size_hint = _extract_model_size_billions(model_name)
return bool(size_hint is not None and size_hint >= LARGE_MODEL_RESOURCE_THRESHOLD_B)
def _build_hf_auth_kwargs(callable_obj: Any) -> dict[str, str]:
auth_token = get_env_any("MARIS_REPO_TOKEN", "MARIS_TOKEN", "HF_TOKEN")
if not auth_token:
return {}
try:
parameters = inspect.signature(callable_obj).parameters
except (TypeError, ValueError):
return {"token": auth_token}
if "token" in parameters:
return {"token": auth_token}
if "use_auth_token" in parameters:
return {"use_auth_token": auth_token}
if any(parameter.kind is inspect.Parameter.VAR_KEYWORD for parameter in parameters.values()):
return {"token": auth_token}
return {}
def _normalize_training_runtime_config(config: TrainingConfig) -> TrainingConfig:
runtime_updates: dict[str, Any] = {}
if not config.fp16 and not config.bf16:
if _runtime_supports_bf16():
runtime_updates["bf16"] = True
elif _runtime_has_cuda():
runtime_updates["fp16"] = True
size_hint = _extract_model_size_billions(config.model_name)
if size_hint is not None:
if size_hint >= GIANT_MODEL_AUTO_QLORA_THRESHOLD_B and config.adapter_type != "qlora":
runtime_updates["adapter_type"] = "qlora"
if (
size_hint >= GIANT_MODEL_AUTO_QLORA_THRESHOLD_B
and config.max_seq_length >= LONG_CONTEXT_MIN_SEQ_LENGTH
):
if config.distributed_strategy == "none":
runtime_updates["distributed_strategy"] = "deepspeed"
if not config.use_accelerate:
runtime_updates["use_accelerate"] = True
if size_hint >= LARGE_MODEL_RESOURCE_THRESHOLD_B and not config.gradient_checkpointing:
runtime_updates["gradient_checkpointing"] = True
if (
size_hint >= LARGE_MODEL_RESOURCE_THRESHOLD_B
and config.per_device_train_batch_size != 1
):
runtime_updates["per_device_train_batch_size"] = 1
if size_hint >= LARGE_MODEL_RESOURCE_THRESHOLD_B and config.per_device_eval_batch_size != 1:
runtime_updates["per_device_eval_batch_size"] = 1
if (
size_hint >= GIANT_MODEL_AUTO_QLORA_THRESHOLD_B
and config.gradient_accumulation_steps < 16
):
runtime_updates["gradient_accumulation_steps"] = 16
if not runtime_updates:
return config
updated_config = replace(config, **runtime_updates)
logger.info(
"Pielāgoju training runtime %s: %s",
config.model_name,
", ".join(f"{key}={value}" for key, value in sorted(runtime_updates.items())),
)
return updated_config
def _get_torch_module() -> Any | None:
try:
import torch # type: ignore
except ImportError:
return None
return torch
def _runtime_has_cuda() -> bool:
torch = _get_torch_module()
cuda = getattr(torch, "cuda", None)
return bool(
cuda is not None and callable(getattr(cuda, "is_available", None)) and cuda.is_available()
)
def _runtime_supports_bf16() -> bool:
torch = _get_torch_module()
cuda = getattr(torch, "cuda", None)
is_available = getattr(cuda, "is_available", None)
if cuda is None or not callable(is_available) or not is_available():
return False
is_bf16_supported = getattr(cuda, "is_bf16_supported", None)
return bool(callable(is_bf16_supported) and is_bf16_supported())
def _runtime_has_accelerator() -> bool:
if _runtime_has_cuda():
return True
torch = _get_torch_module()
backends = getattr(torch, "backends", None)
mps = getattr(backends, "mps", None)
is_available = getattr(mps, "is_available", None)
return bool(callable(is_available) and is_available())
def _build_runtime_training_argument_overrides() -> dict[str, Any]:
overrides: dict[str, Any] = {}
if not _runtime_has_accelerator():
overrides["dataloader_pin_memory"] = False
stderr = getattr(sys, "stderr", None)
is_interactive = bool(callable(getattr(stderr, "isatty", None)) and stderr.isatty())
if not is_interactive:
overrides["disable_tqdm"] = True
overrides["logging_first_step"] = True
return overrides
def _resolve_repo_relative_path(path_value: str) -> str:
normalized = path_value.strip()
if not normalized:
return ""
candidate = Path(normalized)
if candidate.is_absolute():
return str(candidate)
return str((REPO_ROOT / candidate).resolve())
def _resolve_distributed_config_path(config: TrainingConfig) -> str:
if config.distributed_config_path:
return _resolve_repo_relative_path(config.distributed_config_path)
if config.distributed_strategy == "fsdp":
return str((REPO_ROOT / "huggingface" / "fsdp-config.json").resolve())
if config.distributed_strategy == "deepspeed":
return str((REPO_ROOT / "huggingface" / "deepspeed-zero3.json").resolve())
return ""
def _load_json_object(path_value: str, *, label: str) -> dict[str, Any]:
resolved_path = Path(path_value)
if not resolved_path.is_file():
raise FileNotFoundError(f"{label} nav atrasts: {resolved_path}")
payload = json.loads(resolved_path.read_text(encoding="utf-8"))
if not isinstance(payload, dict):
raise ValueError(f"{label} jābūt JSON objektam.")
return payload
def _require_runtime_package(package_name: str, *, context_label: str) -> None:
try:
get_installed_package_version(package_name)
except (PackageNotFoundError, StopIteration) as exc:
raise ImportError(
f"{context_label} nepieciešams instalēt '{package_name}' Python pakotni."
) from exc
def _build_distributed_training_argument_overrides(config: TrainingConfig) -> dict[str, Any]:
overrides: dict[str, Any] = {}
if config.use_accelerate and config.num_processes > 1:
overrides["ddp_find_unused_parameters"] = False
if config.distributed_strategy == "fsdp":
fsdp_config_path = _resolve_distributed_config_path(config)
fsdp_config = (
_load_json_object(fsdp_config_path, label="FSDP konfigurācija")
if fsdp_config_path
else {}
)
fsdp_config.setdefault("min_num_params", config.fsdp_min_num_params)
if config.fsdp_transformer_layer_cls_to_wrap:
fsdp_config["transformer_layer_cls_to_wrap"] = list(
config.fsdp_transformer_layer_cls_to_wrap
)
overrides.update(
{
"fsdp": "full_shard auto_wrap",
"fsdp_config": fsdp_config,
"ddp_find_unused_parameters": False,
}
)
elif config.distributed_strategy == "deepspeed":
deepspeed_config_path = _resolve_distributed_config_path(config)
if not deepspeed_config_path:
raise ValueError(
"DeepSpeed režīmam vajag distributed_config_path vai repo default konfigurāciju."
)
if not Path(deepspeed_config_path).is_file():
raise FileNotFoundError(f"DeepSpeed konfigurācija nav atrasta: {deepspeed_config_path}")
_require_runtime_package("deepspeed", context_label="DeepSpeed režīms")
overrides.update(
{
"deepspeed": deepspeed_config_path,
"ddp_find_unused_parameters": False,
}
)
return overrides
def _load_tokenizer(model_name: str, config: TrainingConfig) -> Any:
from transformers import AutoTokenizer # type: ignore
load_kwargs = _filter_supported_kwargs(
AutoTokenizer.from_pretrained,
{
"trust_remote_code": True,
**_build_hf_auth_kwargs(AutoTokenizer.from_pretrained),
"use_fast": True,
},
)
with maris_hf_compatible_path(model_name, allow_remote_snapshot=True) as compatible_model_path:
try:
return _load_tokenizer_from_path(
compatible_model_path,
config=config,
auto_tokenizer_class=AutoTokenizer,
load_kwargs=load_kwargs,
)
except TOKENIZER_LOAD_RETRY_EXCEPTIONS as exc:
if not _should_retry_after_installing_tokenizer_backends(exc):
raise
_install_missing_tokenizer_backends()
logger.warning(
"Retrying tokenizer load for %s after installing missing runtime backends.",
config.model_name,
)
return _load_tokenizer_from_path(
compatible_model_path,
config=config,
auto_tokenizer_class=AutoTokenizer,
load_kwargs=load_kwargs,
)
def _load_tokenizer_from_path(
compatible_model_path: str,
*,
config: TrainingConfig,
auto_tokenizer_class: Any,
load_kwargs: dict[str, Any],
) -> Any:
try:
return auto_tokenizer_class.from_pretrained(compatible_model_path, **load_kwargs)
# Tokenizer backends often fail with implementation-specific exceptions
# before a slower Python tokenizer can succeed, so we retry once broadly.
except TOKENIZER_LOAD_RETRY_EXCEPTIONS as exc:
if "use_fast" not in load_kwargs:
raise
retry_kwargs = dict(load_kwargs)
retry_kwargs["use_fast"] = False
logger.warning(
"Fast tokenizer load failed for %s; retrying with use_fast=False: %s",
config.model_name,
exc,
)
try:
return auto_tokenizer_class.from_pretrained(compatible_model_path, **retry_kwargs)
except TOKENIZER_LOAD_RETRY_EXCEPTIONS:
explicit_slow_tokenizer = _load_explicit_slow_tokenizer(
compatible_model_path,
retry_kwargs,
)
if explicit_slow_tokenizer is not None:
return explicit_slow_tokenizer
raise
def _should_retry_after_installing_tokenizer_backends(exc: BaseException) -> bool:
message = str(exc).lower()
missing_backend_markers = (
"you need to have sentencepiece or tiktoken installed",
"requires the sentencepiece library",
"requires the tiktoken library",
"no module named 'sentencepiece'",
'no module named "sentencepiece"',
"no module named 'tiktoken'",
'no module named "tiktoken"',
)
return any(marker in message for marker in missing_backend_markers)
def _install_missing_tokenizer_backends() -> bool:
missing_packages = [
package_name
for package_name in ("sentencepiece", "tiktoken")
if importlib.util.find_spec(package_name) is None
]
if not missing_packages:
return False
logger.warning(
"Installing missing tokenizer runtime backends for training: %s",
", ".join(missing_packages),
)
try:
completed = subprocess.run(
[sys.executable, "-m", "pip", "install", "--no-cache-dir", *missing_packages],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)
except subprocess.CalledProcessError as exc:
output = (exc.stdout or "").strip()
if output:
logger.error("Tokenizer backend install failed: %s", output)
raise RuntimeError("Couldn't install missing tokenizer backends automatically.") from exc
output = (completed.stdout or "").strip()
if output:
logger.info("Tokenizer backend install output: %s", output)
importlib.invalidate_caches()
return True
def _load_local_tokenizer_artifact(path: Path) -> dict[str, Any] | None:
"""Nolasa lokālu tokenizer JSON artefaktu un atgriež dict vai None."""
if not path.is_file():
return None
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError) as exc:
logger.warning("Couldn't read tokenizer artifact %s: %s", path, exc)
return None
return payload if isinstance(payload, dict) else None
def _collect_explicit_slow_tokenizer_classes(model_name_or_path: str) -> list[str]:
"""Savāc deduplicētus slow tokenizer klašu nosaukumus no lokāliem HF artefaktiem."""
model_dir = Path(model_name_or_path)
if not model_dir.is_dir():
return []
class_names: list[str] = []
seen: set[str] = set()
def add_class_name(candidate: Any) -> None:
if not isinstance(candidate, str):
return
normalized = candidate.strip()
if not normalized:
return
if normalized.endswith("Fast"):
normalized = normalized[:-4]
if not normalized or normalized in seen:
return
seen.add(normalized)
class_names.append(normalized)
for artifact_name in ("tokenizer_config.json", "config.json"):
payload = _load_local_tokenizer_artifact(model_dir / artifact_name)
if payload is None:
continue
add_class_name(payload.get("tokenizer_class"))
auto_map = payload.get("auto_map")
if not isinstance(auto_map, dict):
continue
auto_tokenizer_entry = auto_map.get("AutoTokenizer")
if isinstance(auto_tokenizer_entry, list):
for candidate in auto_tokenizer_entry:
add_class_name(candidate)
else:
add_class_name(auto_tokenizer_entry)
return class_names
def _load_explicit_slow_tokenizer(
model_name_or_path: str, load_kwargs: dict[str, Any]
) -> Any | None:
"""Mēģina ielādēt explicit slow tokenizer klasi pēc AutoTokenizer fallback neveiksmes."""
class_names = _collect_explicit_slow_tokenizer_classes(model_name_or_path)
if not class_names:
return None
import transformers # type: ignore
retry_kwargs = dict(load_kwargs)
retry_kwargs.pop("use_fast", None)
for class_name in class_names:
tokenizer_class = getattr(transformers, class_name, None)
if tokenizer_class is None:
continue
try:
logger.warning(
"AutoTokenizer retry still failed for %s; loading explicit slow tokenizer %s.",
model_name_or_path,
class_name,
)
return tokenizer_class.from_pretrained(model_name_or_path, **retry_kwargs)
except TOKENIZER_LOAD_RETRY_EXCEPTIONS as exc:
logger.warning(
"Explicit slow tokenizer %s failed for %s: %s",
class_name,
model_name_or_path,
exc,
)
return None
def _load_model(
model_name_or_path: str,
config: TrainingConfig,
*,
trainable_adapter: bool = False,
) -> Any:
from transformers import AutoModelForCausalLM # type: ignore
with maris_hf_compatible_path(
model_name_or_path, allow_remote_snapshot=True
) as compatible_model_path:
local_path = Path(compatible_model_path)
has_local_adapter_config = (
local_path.exists() and local_path.joinpath("adapter_config.json").is_file()
)
should_try_auto_peft = _uses_peft(config) and (
trainable_adapter or has_local_adapter_config
)
if should_try_auto_peft:
try:
import peft # type: ignore
except ImportError:
auto_peft_model_cls = None # type: ignore[assignment]
else:
auto_peft_model_cls = getattr(peft, "AutoPeftModelForCausalLM", None)
if auto_peft_model_cls is not None:
adapter_kwargs = {}
if (
"is_trainable"
in inspect.signature(auto_peft_model_cls.from_pretrained).parameters
):
adapter_kwargs["is_trainable"] = trainable_adapter
adapter_kwargs.update(_build_hf_auth_kwargs(auto_peft_model_cls.from_pretrained))
adapter_kwargs.setdefault("trust_remote_code", True)
return auto_peft_model_cls.from_pretrained(
compatible_model_path,
**_filter_supported_kwargs(
auto_peft_model_cls.from_pretrained,
adapter_kwargs,
),
)
return AutoModelForCausalLM.from_pretrained(
compatible_model_path,
**_filter_supported_kwargs(
AutoModelForCausalLM.from_pretrained,
{
**_build_model_load_kwargs(config),
**_build_hf_auth_kwargs(AutoModelForCausalLM.from_pretrained),
},
),
)
def _apply_peft_adapter(model: Any, config: TrainingConfig) -> Any:
if not _uses_peft(config):
return model
try:
from peft import ( # type: ignore
LoraConfig,
TaskType,
get_peft_model,
prepare_model_for_kbit_training,
)
except ImportError as exc:
raise ImportError("LoRA/QLoRA apmācībai vajag peft atkarību.") from exc
if _uses_qlora(config):
model = prepare_model_for_kbit_training(
model,
use_gradient_checkpointing=config.gradient_checkpointing,
)
target_modules: str | list[str] | None
if config.peft_target_modules:
target_modules = list(config.peft_target_modules)
else:
target_modules = "all-linear"
peft_config = LoraConfig(
r=config.lora_r,
lora_alpha=config.lora_alpha,
lora_dropout=config.lora_dropout,
bias=config.lora_bias,
task_type=TaskType.CAUSAL_LM,
target_modules=target_modules,
)
adapted_model = get_peft_model(model, peft_config)
if hasattr(adapted_model, "print_trainable_parameters"):
adapted_model.print_trainable_parameters()
return adapted_model
def _supports_gradient_checkpointing_kwargs(method: Any) -> bool:
with suppress(TypeError, ValueError):
signature = inspect.signature(method)
return "gradient_checkpointing_kwargs" in signature.parameters or any(
parameter.kind is inspect.Parameter.VAR_KEYWORD
for parameter in signature.parameters.values()
)
return False
def _prepare_training_model(model_name: str, tokenizer: Any, config: TrainingConfig) -> Any:
model = _load_model(model_name, config)
if (
getattr(model, "config", None) is not None
and getattr(model.config, "pad_token_id", None) is None
):
model.config.pad_token_id = getattr(tokenizer, "pad_token_id", None)
if getattr(model, "config", None) is not None and hasattr(model.config, "use_cache"):
model.config.use_cache = not config.gradient_checkpointing
if config.gradient_checkpointing and hasattr(model, "gradient_checkpointing_enable"):
gradient_checkpointing_enable = model.gradient_checkpointing_enable
if config.gradient_checkpointing_use_reentrant is None:
gradient_checkpointing_enable()
else:
if _supports_gradient_checkpointing_kwargs(gradient_checkpointing_enable):
gradient_checkpointing_enable(
gradient_checkpointing_kwargs={
"use_reentrant": config.gradient_checkpointing_use_reentrant
}
)
else:
logger.warning(
"Ignoring explicit gradient_checkpointing_use_reentrant=%s because "
"gradient_checkpointing_enable() does not accept keyword overrides in "
"the current runtime.",
config.gradient_checkpointing_use_reentrant,
)
gradient_checkpointing_enable()
return _apply_peft_adapter(model, config)
def _load_preference_training_dataset(config: TrainingConfig) -> Any:
examples = _filter_preference_examples_for_branch(
load_preference_dataset(config.preference_dataset_path),
branch_name=config.branch_name,
branch_filter_rules=config.branch_dataset_filter_rules,
)
rows = [
{
"prompt": example.prompt,
"chosen": example.chosen,
"rejected": example.rejected,
"source": example.source,
"annotator": example.annotator or "",
"edit_target": example.edit_target or "",
"context": example.context or "",
"branch": example.branch or "",
"task_type": example.task_type or "",
"language": example.language or "",
"repo_context": list(example.repo_context),
"execution_required": example.execution_required,
"tags": list(example.tags),
}
for example in examples
]
try:
from datasets import Dataset # type: ignore
except ImportError:
return rows
try:
return Dataset.from_list(rows)
except AttributeError as exc:
if "PreTrainedTokenizerBase" in str(exc):
logger.warning(
"Falling back to plain preference rows because datasets fingerprinting needs a fuller transformers install: %s",
exc,
)
return rows
raise
def _filter_preference_examples_for_branch(
examples: list[PreferenceExample],
*,
branch_name: str,
branch_filter_rules: dict[str, dict[str, Any]] | None = None,
) -> list[PreferenceExample]:
if branch_name not in TEXT_TRAINABLE_BRANCHES:
return examples
filtered = [
example
for example in examples
if _matches_branch_filter_rule(
_preference_branch_filter_signals(example),
branch_name=branch_name,
branch_filter_rules=branch_filter_rules,
)
]
if filtered:
return filtered
raise ValueError(
f"Preference dataset filter neatgrieza nevienu piemēru atzaram '{branch_name}'."
)
def _is_local_training_artifact_dir(path: Path) -> bool:
"""Return whether a directory contains a reusable saved model or adapter artifact."""
return path.is_dir() and any(
path.joinpath(name).is_file() for name in LOCAL_TRAINING_ARTIFACT_FILES
)
def _build_model_source_fingerprint(model_name: str) -> str:
normalized = model_name.strip().casefold().encode("utf-8")
return hashlib.sha256(normalized).hexdigest()
def _load_local_model_source_fingerprint(path: Path) -> str | None:
training_config = _load_json_if_exists(path / "training-config.json")
if not isinstance(training_config, dict):
return None
fingerprint = training_config.get(MODEL_SOURCE_FINGERPRINT_KEY)
if not isinstance(fingerprint, str):
return None
normalized = fingerprint.strip().casefold()
return normalized or None
def _resolve_training_model_source(config: TrainingConfig) -> str:
"""Resolve the effective source model, preferring local persistent artifacts when enabled."""
if not config.continue_from_latest_artifact:
return config.model_name
candidates: list[tuple[Path, bool]] = []
if config.continue_model_path:
candidates.append((Path(config.continue_model_path), True))
output_dir = Path(config.output_dir)
candidates.append((output_dir, False))
expected_fingerprint = _build_model_source_fingerprint(config.model_name)
seen: set[Path] = set()
for candidate, explicit_candidate in candidates:
try:
resolved = candidate.expanduser().resolve()
except OSError:
continue
if resolved in seen:
continue
seen.add(resolved)
if _is_local_training_artifact_dir(resolved):
if not explicit_candidate:
saved_fingerprint = _load_local_model_source_fingerprint(resolved)
if saved_fingerprint != expected_fingerprint:
if saved_fingerprint:
logger.info(
"Izlaižu auto-continue no %s, jo saglabātā modeļa bāze neatbilst izvēlētajam modelim.",
resolved,
)
else:
logger.info(
"Izlaižu auto-continue no %s, jo trūkst modeļa saderības metadata.",
resolved,
)
continue
logger.info("Turpinu treniņu no lokālā artefakta: %s", resolved)
return str(resolved)
return config.model_name
def _resolve_runtime_uid_suffix() -> str:
"""Best-effort UID suffix for runtime fallback paths and user labels."""
uid = "unknown"
getuid = getattr(os, "getuid", None)
if callable(getuid):
with suppress(OSError):
uid = str(getuid())
return uid
def _ensure_runtime_home_dir() -> str:
"""Ensure runtime HOME and user identity vars exist in containers without passwd entries."""
configured_home = os.environ.get("HOME", "").strip()
uid = _resolve_runtime_uid_suffix()
if not configured_home:
fallback_home = (Path(tempfile.gettempdir()) / f"maris-home-{uid}").resolve()
fallback_home.mkdir(parents=True, exist_ok=True)
configured_home = str(fallback_home)
os.environ["HOME"] = configured_home
logger.warning(
"HOME nav iestatīts; izmantojam fallback runtime home %s, lai treniņš strādātu konteineros bez passwd ieraksta.",
fallback_home,
)
runtime_user = ""
for name in ("USER", "LOGNAME", "USERNAME"):
candidate = os.environ.get(name, "").strip()
if candidate:
runtime_user = candidate
break
if not runtime_user:
runtime_user = f"maris-{uid}"
logger.warning(
"Lietotāja vides mainīgie nav iestatīti; izmantojam fallback runtime lietotāju %s, lai izvairītos no getpwuid kļūdām.",
runtime_user,
)
for name in ("USER", "LOGNAME", "USERNAME"):
if not os.environ.get(name, "").strip():
os.environ[name] = runtime_user
return configured_home
def _filter_supported_kwargs(callable_obj: Any, kwargs: dict[str, Any]) -> dict[str, Any]:
signature = inspect.signature(callable_obj)
parameters = signature.parameters
if any(parameter.kind is inspect.Parameter.VAR_KEYWORD for parameter in parameters.values()):
return kwargs
supported_names = {
name
for name, parameter in parameters.items()
if name != "self"
and parameter.kind
in {inspect.Parameter.POSITIONAL_OR_KEYWORD, inspect.Parameter.KEYWORD_ONLY}
}
return {name: value for name, value in kwargs.items() if name in supported_names}
def _build_preference_training_arguments(preference_config_cls: Any, config: TrainingConfig) -> Any:
return _build_training_arguments(
preference_config_cls,
output_dir=config.output_dir,
overwrite_output_dir=True,
num_train_epochs=config.num_epochs,
learning_rate=config.learning_rate,
per_device_train_batch_size=config.per_device_train_batch_size,
per_device_eval_batch_size=config.per_device_eval_batch_size,
gradient_accumulation_steps=config.gradient_accumulation_steps,
warmup_ratio=config.warmup_ratio,
weight_decay=config.weight_decay,
logging_steps=config.logging_steps,
save_steps=config.save_steps,
eval_steps=config.eval_steps,
save_total_limit=config.save_total_limit,
lr_scheduler_type=config.lr_scheduler_type,
seed=config.seed,
fp16=config.fp16,
bf16=config.bf16,
report_to=config.report_to,
save_safetensors=config.save_safetensors,
remove_unused_columns=False,
beta=config.preference_beta,
max_length=config.preference_max_length,
max_prompt_length=config.preference_max_prompt_length,
evaluation_strategy="no",
)
def _load_preference_reference_model(output_dir: Path, config: TrainingConfig) -> Any:
reference_path = config.preference_reference_model or str(output_dir)
return _load_model(reference_path, config, trainable_adapter=False)
def _run_preference_optimization(
model: Any,
tokenizer: Any,
config: TrainingConfig,
*,
output_dir: Path,
) -> tuple[Any, Any, dict[str, float]]:
if not _uses_preference_optimization(config):
return model, None, {}
try:
import trl # type: ignore
except ImportError as exc:
raise ImportError("Preference optimization (DPO/ORPO) vajag trl atkarību.") from exc
preference_dataset = _load_preference_training_dataset(config)
strategy = config.preference_optimization
trainer_name = f"{strategy.upper()}Trainer"
trainer_cls = getattr(trl, trainer_name, None)
if trainer_cls is None:
raise RuntimeError(f"TRL modulī nav pieejams {trainer_name}.")
trainer_config_cls = getattr(trl, f"{strategy.upper()}Config", None)
if trainer_config_cls is None:
from transformers import TrainingArguments # type: ignore
trainer_config_cls = TrainingArguments
preference_args = _build_preference_training_arguments(trainer_config_cls, config)
trainer_kwargs: dict[str, Any] = {
"model": model,
"args": preference_args,
"train_dataset": preference_dataset,
"tokenizer": tokenizer,
"processing_class": tokenizer,
}
if strategy == "dpo":
trainer_kwargs["ref_model"] = _load_preference_reference_model(output_dir, config)
preference_trainer = trainer_cls(
**_filter_supported_kwargs(trainer_cls.__init__, trainer_kwargs)
)
preference_result = preference_trainer.train()
preference_metrics = {
f"preference_{key}": float(value)
for key, value in dict(getattr(preference_result, "metrics", {})).items()
if isinstance(value, int | float)
}
preference_metrics["preference_examples"] = float(len(preference_dataset))
preference_metrics["preference_stage"] = 1.0
preference_metrics["preference_strategy"] = 1.0 if strategy == "dpo" else 2.0
return model, preference_trainer, preference_metrics
def _save_json(path: Path, payload: dict[str, Any]) -> None:
_save_json_payload(path, payload)
def _save_json_payload(path: Path, payload: Any) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(payload, indent=2, ensure_ascii=False), encoding="utf-8")
def _load_json_if_exists(path: Path) -> dict[str, Any] | None:
if not path.is_file():
return None
payload = json.loads(path.read_text(encoding="utf-8"))
return payload if isinstance(payload, dict) else None
def _write_text(path: Path, content: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content, encoding="utf-8")
def _build_training_artifact_config(config: TrainingConfig) -> dict[str, Any]:
payload = config.to_dict().copy()
payload.pop("model_name", None)
payload["model_preset"] = config.model_preset or "custom"
payload[MODEL_SOURCE_FINGERPRINT_KEY] = _build_model_source_fingerprint(config.model_name)
payload["maris_origin"] = MARIS_ORIGIN_NAME
payload["maris_framework"] = MARIS_FRAMEWORK_NAME
payload["maris_model_id"] = config.hub_model_id
payload["artifact_format"] = "maris-training-config-v1"
return payload
def _build_artifact_identity(config: TrainingConfig) -> dict[str, Any]:
return {
"maris_origin": MARIS_ORIGIN_NAME,
"maris_framework": MARIS_FRAMEWORK_NAME,
"dataset_repo": config.dataset_repo,
"dataset_repos": _resolve_training_dataset_repos(config),
"eval_dataset_repo": _resolve_primary_eval_dataset_repo(config),
"eval_dataset_repos": _resolve_eval_dataset_repos(config),
"branch_name": config.branch_name,
"branch_focus": config.branch_focus,
}
def _build_model_artifact_identity(config: TrainingConfig) -> dict[str, Any]:
return {
**_build_artifact_identity(config),
"maris_model_id": config.hub_model_id,
}
def _build_metrics_artifact(
config: TrainingConfig,
metrics: dict[str, float],
*,
artifact_type: str,
extra_payload: dict[str, Any] | None = None,
) -> dict[str, Any]:
payload = dict(metrics)
payload.update(_build_model_artifact_identity(config))
payload["artifact_type"] = artifact_type
if extra_payload:
payload.update(extra_payload)
return payload
def _build_branch_suite_artifact(
config: TrainingConfig, suite_results: dict[str, dict[str, Any]]
) -> dict[str, Any]:
return {
"branches": suite_results,
**_build_artifact_identity(config),
"artifact_type": "branch-suite",
}
def _write_origin_metadata(output_dir: Path, config: TrainingConfig) -> None:
_save_json(
output_dir / "maris-metadata.json",
{
"origin": MARIS_ORIGIN_NAME,
"framework": MARIS_FRAMEWORK_NAME,
**_build_model_artifact_identity(config),
},
)
def _build_model_card(
config: TrainingConfig,
*,
trained_at: str,
train_examples: int,
eval_examples: int,
metrics: dict[str, float],
) -> str:
metrics_lines = []
for key in ("train_loss", "eval_loss", "perplexity"):
if key in metrics:
metrics_lines.append(f"- `{key}`: {metrics[key]}")
if not metrics_lines:
metrics_lines.append("- Nav pieejamu metriku šim skrējienam.")
return "\n".join(
[
"---",
"language:",
" - lv",
" - en",
"license: mit",
"pipeline_tag: text-generation",
"tags:",
*(f" - {tag}" for tag in MODEL_CARD_TAGS),
"---",
"",
f"# {MARIS_ORIGIN_NAME} Model",
"",
"## Model Summary",
"",
f"Šis artefakts ir {MARIS_ORIGIN_NAME} treniņa izvads atzaram `{config.branch_name}`.",
f"Modelis tiek publicēts kā `{config.hub_model_id}` un saglabā Maris AI identitāti visos galvenajos artefaktos.",
"",
"## Training Data",
"",
f"- Dataset repo: `{config.dataset_repo}`",
f"- Dataset repos: `{', '.join(_resolve_training_dataset_repos(config))}`",
f"- Eval dataset repo: `{_resolve_primary_eval_dataset_repo(config)}`",
f"- Eval dataset repos: `{', '.join(_resolve_eval_dataset_repos(config) or [_resolve_primary_eval_dataset_repo(config)])}`",
f"- Train examples: `{train_examples}`",
f"- Eval examples: `{eval_examples}`",
f"- Branch focus: `{config.branch_focus}`",
"",
"## Training Configuration",
"",
"- Base lineage: `Maris AI`",
f"- Model preset: `{config.model_preset or 'custom'}`",
f"- Adapter strategy: `{config.adapter_type}`",
f"- Preference optimization: `{config.preference_optimization}`",
f"- Output model id: `{config.hub_model_id}`",
f"- Trained at: `{trained_at}`",
"",
"## Metrics",
"",
*metrics_lines,
"",
"## Maris Identity Guarantees",
"",
f"- `config.json`, `generation_config.json` un `tokenizer_config.json` tiek sanitizēti uz `{config.hub_model_id}`",
f"- `training-config.json`, `training-provenance.json` un `maris-metadata.json` satur tikai `{MARIS_ORIGIN_NAME}` izcelsmes metadatus",
"- Šis artefakts ir paredzēts publicēšanai un lietošanai kā Maris AI modelis",
"",
]
)
def _write_training_provenance(
output_dir: Path,
config: TrainingConfig,
*,
trained_at: str,
train_examples: int,
eval_examples: int,
metrics: dict[str, float],
quality_report: DatasetQualityReport | None = None,
scoring_report: DatasetScoringReport | None = None,
benchmark_feedback: DatasetBenchmarkFeedback | None = None,
) -> None:
artifact_files = [
"README.md",
"training-config.json",
"training-metrics.json",
"maris-metadata.json",
"training-provenance.json",
]
if quality_report is not None:
artifact_files.append("dataset-quality-report.json")
if scoring_report is not None:
artifact_files.append("dataset-scoring-report.json")
_save_json(
output_dir / "training-provenance.json",
{
"trained_at": trained_at,
"dataset_repo": config.dataset_repo,
"dataset_repos": _resolve_training_dataset_repos(config),
"eval_dataset_repo": _resolve_primary_eval_dataset_repo(config),
"eval_dataset_repos": _resolve_eval_dataset_repos(config),
"branch_name": config.branch_name,
"branch_focus": config.branch_focus,
"adapter_type": config.adapter_type,
"preference_optimization": config.preference_optimization,
"base_model_name": MARIS_ORIGIN_NAME,
"base_model_lineage": MARIS_ORIGIN_NAME,
"model_preset": config.model_preset or "custom",
"hub_model_id": config.hub_model_id,
"train_examples": train_examples,
"eval_examples": eval_examples,
"artifact_files": artifact_files,
"metrics": metrics,
"quality_report": quality_report.to_dict() if quality_report is not None else None,
"scoring_report": scoring_report.to_dict() if scoring_report is not None else None,
"benchmark_feedback": (
build_benchmark_feedback_artifact(benchmark_feedback)
if benchmark_feedback is not None
else None
),
"maris_origin": MARIS_ORIGIN_NAME,
"maris_framework": MARIS_FRAMEWORK_NAME,
},
)
def _sanitize_saved_artifact_json(path: Path, *, maris_model_id: str) -> None:
if not path.is_file():
return
payload = json.loads(path.read_text(encoding="utf-8"))
sanitized_payload = _sanitize_artifact_payload(
payload,
maris_model_id=maris_model_id,
)
if isinstance(sanitized_payload, dict):
sanitized_payload["maris_origin"] = MARIS_ORIGIN_NAME
sanitized_payload["maris_framework"] = MARIS_FRAMEWORK_NAME
sanitized_payload["maris_model_id"] = maris_model_id
_save_json_payload(path, sanitized_payload)
def _sanitize_identity_text(value: str, *, maris_model_id: str) -> str:
sanitized = FOREIGN_MODEL_REFERENCE_RE.sub(maris_model_id, value)
for pattern in FOREIGN_AI_BRAND_PATTERNS:
sanitized = pattern.sub(MARIS_ORIGIN_NAME, sanitized)
sanitized = MARIS_IDENTITY_VARIANT_RE.sub(MARIS_ORIGIN_NAME, sanitized)
return sanitized
def _is_sanitized_reference_key(field_name: str | None) -> bool:
return bool(field_name) and (
field_name in SANITIZED_ARTIFACT_KEYS or field_name.endswith("_name_or_path")
)
def _sanitize_artifact_payload(
payload: Any,
*,
maris_model_id: str,
field_name: str | None = None,
path: tuple[str, ...] = (),
) -> Any:
if isinstance(payload, dict):
return {
key: _sanitize_artifact_payload(
value,
maris_model_id=maris_model_id,
field_name=str(key),
path=(*path, str(key)),
)
for key, value in payload.items()
}
if isinstance(payload, list):
return [
_sanitize_artifact_payload(
item,
maris_model_id=maris_model_id,
field_name=field_name,
path=(*path, str(index)),
)
for index, item in enumerate(payload)
]
if not isinstance(payload, str):
return payload
if _is_sanitized_reference_key(field_name):
return maris_model_id
if field_name in {"base_model_name", "base_model_lineage"}:
return MARIS_ORIGIN_NAME
if field_name in IDENTITY_TEXT_KEYS or "chat_template" in path:
return _sanitize_identity_text(payload, maris_model_id=maris_model_id)
if FOREIGN_MODEL_REFERENCE_RE.search(payload) or any(
pattern.search(payload) for pattern in FOREIGN_AI_BRAND_PATTERNS
):
return _sanitize_identity_text(payload, maris_model_id=maris_model_id)
return payload
def _is_text_identity_artifact(path: Path) -> bool:
lowered = path.name.lower()
return (
path.suffix.lower() in TEXT_SANITIZED_ARTIFACT_EXTENSIONS
or "chat_template" in lowered
or "chat-template" in lowered
)
def _sanitize_text_identity_artifact(path: Path, *, maris_model_id: str) -> None:
if not path.is_file():
return
path.write_text(
_sanitize_identity_text(path.read_text(encoding="utf-8"), maris_model_id=maris_model_id),
encoding="utf-8",
)
def _sanitize_saved_artifacts(output_dir: Path, *, maris_model_id: str) -> None:
for path in sorted(output_dir.rglob("*")):
if not path.is_file():
continue
if path.suffix.lower() == ".json":
_sanitize_saved_artifact_json(path, maris_model_id=maris_model_id)
continue
if _is_text_identity_artifact(path):
_sanitize_text_identity_artifact(path, maris_model_id=maris_model_id)
write_maris_compatibility_artifact(output_dir, maris_model_id=maris_model_id)
apply_maris_compatibility_identity(output_dir)
def _collect_unsanitized_identity_strings(
payload: Any,
*,
path: tuple[str, ...] = (),
) -> list[str]:
if isinstance(payload, dict):
matches: list[str] = []
for key, value in payload.items():
matches.extend(
_collect_unsanitized_identity_strings(
value,
path=(*path, str(key)),
)
)
return matches
if isinstance(payload, list):
list_matches: list[str] = []
for index, item in enumerate(payload):
list_matches.extend(
_collect_unsanitized_identity_strings(item, path=(*path, str(index)))
)
return list_matches
if not isinstance(payload, str):
return []
if FOREIGN_MODEL_REFERENCE_RE.search(payload):
return ["/".join(path) or "<root>"]
for pattern in FOREIGN_AI_BRAND_PATTERNS:
if pattern.search(payload):
return ["/".join(path) or "<root>"]
return []
def _verify_saved_artifacts(output_dir: Path) -> None:
issues: list[str] = []
for path in sorted(output_dir.rglob("*")):
if not path.is_file():
continue
if path.name == MARIS_COMPATIBILITY_ARTIFACT_NAME:
continue
if path.suffix.lower() == ".json":
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except json.JSONDecodeError:
continue
for issue in _collect_unsanitized_identity_strings(payload):
issues.append(f"{path.relative_to(output_dir)}:{issue}")
continue
if _is_text_identity_artifact(path):
content = path.read_text(encoding="utf-8")
if FOREIGN_MODEL_REFERENCE_RE.search(content) or any(
pattern.search(content) for pattern in FOREIGN_AI_BRAND_PATTERNS
):
issues.append(str(path.relative_to(output_dir)))
if issues:
unique_issues = sorted(set(issues))
visible_issues = unique_issues[:20]
overflow_suffix = ""
if len(unique_issues) > len(visible_issues):
overflow_suffix = f" ... un vēl {len(unique_issues) - len(visible_issues)}"
raise ValueError(
"Atrasti nesanitizēti Hugging Face artefakti: "
+ ", ".join(visible_issues)
+ overflow_suffix
)
def _write_training_artifacts(
output_dir: Path,
config: TrainingConfig,
*,
trained_at: str,
train_examples: int,
eval_examples: int,
metrics: dict[str, float],
quality_report: DatasetQualityReport | None = None,
scoring_report: DatasetScoringReport | None = None,
benchmark_feedback: DatasetBenchmarkFeedback | None = None,
) -> None:
_sanitize_saved_artifacts(output_dir, maris_model_id=config.hub_model_id)
_save_json(output_dir / "training-config.json", _build_training_artifact_config(config))
_save_json(
output_dir / "training-metrics.json",
_build_metrics_artifact(
config,
metrics,
artifact_type="training-metrics",
extra_payload=_build_scoring_dashboard_payload(
scoring_report=scoring_report,
benchmark_feedback=benchmark_feedback,
),
),
)
if quality_report is not None:
_save_json(output_dir / "dataset-quality-report.json", quality_report.to_dict())
if scoring_report is not None:
_save_json(output_dir / "dataset-scoring-report.json", scoring_report.to_dict())
_write_origin_metadata(output_dir, config)
_write_training_provenance(
output_dir,
config,
trained_at=trained_at,
train_examples=train_examples,
eval_examples=eval_examples,
metrics=metrics,
quality_report=quality_report,
scoring_report=scoring_report,
benchmark_feedback=benchmark_feedback,
)
_write_text(
output_dir / "README.md",
_build_model_card(
config,
trained_at=trained_at,
train_examples=train_examples,
eval_examples=eval_examples,
metrics=metrics,
),
)
def _write_preference_artifact(output_dir: Path, config: TrainingConfig) -> dict[str, Any] | None:
if not config.preference_dataset_path:
return None
examples = _load_filtered_preference_examples(config)
payload = summarize_preference_dataset(examples)
payload.update(_build_model_artifact_identity(config))
human_eval_summary = build_human_eval_summary(examples)
human_eval_summary.update(_build_model_artifact_identity(config))
_save_json(output_dir / "preference-summary.json", payload)
_save_json(output_dir / "human-eval-summary.json", human_eval_summary)
_save_json(
output_dir / "blind-side-by-side-eval.json",
build_blind_side_by_side_artifact(examples),
)
return payload
def _load_filtered_preference_examples(config: TrainingConfig) -> list[PreferenceExample]:
return _filter_preference_examples_for_branch(
load_preference_dataset(config.preference_dataset_path),
branch_name=config.branch_name,
branch_filter_rules=config.branch_dataset_filter_rules,
)
def _sync_output_dir_to_hub(
output_dir: Path, config: TrainingConfig, *, commit_message: str
) -> None:
try:
from huggingface_hub import HfApi # type: ignore
except ImportError:
logger.warning("huggingface_hub nav pieejams — izlaižam Maris artefaktu sinhronizāciju.")
return
try:
token = get_env_any("MARIS_REPO_TOKEN", "MARIS_TOKEN", "HF_TOKEN")
api = HfApi(token=token) if token else HfApi()
api.create_repo(repo_id=config.hub_model_id, repo_type="model", exist_ok=True)
api.upload_folder(
folder_path=str(output_dir),
repo_id=config.hub_model_id,
repo_type="model",
commit_message=commit_message,
)
except Exception as exc: # noqa: BLE001
logger.warning("Neizdevās pārpublicēt Maris artefaktus uz Hub: %s", exc)
def _build_training_arguments(training_arguments_cls: Any, **kwargs: Any) -> Any:
signature = inspect.signature(training_arguments_cls.__init__)
parameters = signature.parameters
if any(parameter.kind is inspect.Parameter.VAR_KEYWORD for parameter in parameters.values()):
return training_arguments_cls(**kwargs)
supported_names = {
name
for name, parameter in parameters.items()
if name != "self"
and parameter.kind
in {inspect.Parameter.POSITIONAL_OR_KEYWORD, inspect.Parameter.KEYWORD_ONLY}
}
resolved_kwargs = dict(kwargs)
for source_name, target_name in TRAINING_ARGUMENT_ALIASES.items():
if (
source_name in resolved_kwargs
and source_name not in supported_names
and target_name in supported_names
):
resolved_kwargs[target_name] = resolved_kwargs.pop(source_name)
ignored_arguments = sorted(set(resolved_kwargs) - supported_names)
if ignored_arguments:
logger.info(
"Izlaiž neatbalstītos TrainingArguments parametrus: %s",
", ".join(ignored_arguments),
)
return training_arguments_cls(
**{name: value for name, value in resolved_kwargs.items() if name in supported_names}
)
def _materialize_split_records(split: Any) -> list[dict[str, Any]]:
raw_records = getattr(split, "items", split)
return [
dict(record) if isinstance(record, dict) else {"text": str(record)}
for record in raw_records
]
def _rebuild_split_like(split: Any, records: list[dict[str, Any]]) -> Any:
if type(split).__module__.startswith("datasets"):
from datasets import Dataset # type: ignore
return Dataset.from_list(records)
split_type = type(split)
try:
return split_type(records)
except Exception: # noqa: BLE001
return records
def _resolve_branch_benchmark_dataset_path(config: TrainingConfig, branch_name: str) -> str:
explicit = str(config.branch_benchmark_dataset_paths.get(branch_name, "") or "").strip()
if explicit:
return explicit
return config.benchmark_dataset_path
def _resolve_branch_benchmark_name(config: TrainingConfig, branch_name: str) -> str:
explicit = str(config.branch_benchmark_names.get(branch_name, "") or "").strip()
if explicit:
return explicit
return config.benchmark_name
def _resolve_branch_preference_dataset_path(config: TrainingConfig, branch_name: str) -> str:
explicit = str(config.branch_preference_dataset_paths.get(branch_name, "") or "").strip()
if explicit:
return explicit
return config.preference_dataset_path
def _apply_branch_runtime_defaults(config: TrainingConfig) -> TrainingConfig:
benchmark_dataset_path = config.benchmark_dataset_path
benchmark_name = config.benchmark_name
preference_dataset_path = config.preference_dataset_path
if benchmark_dataset_path and preference_dataset_path:
return config
if not benchmark_dataset_path and config.benchmark_gate_enabled:
benchmark_dataset_path = _resolve_branch_benchmark_dataset_path(config, config.branch_name)
if config.benchmark_gate_enabled and (
not benchmark_name or benchmark_name == DEFAULT_BENCHMARK_NAME
):
benchmark_name = _resolve_branch_benchmark_name(config, config.branch_name)
if not preference_dataset_path:
preference_dataset_path = _resolve_branch_preference_dataset_path(
config, config.branch_name
)
if (
benchmark_dataset_path == config.benchmark_dataset_path
and benchmark_name == config.benchmark_name
and preference_dataset_path == config.preference_dataset_path
):
return config
return replace(
config,
benchmark_dataset_path=benchmark_dataset_path,
benchmark_name=benchmark_name,
preference_dataset_path=preference_dataset_path,
)
def _normalize_branch_filter_token(value: Any) -> str:
return str(value or "").strip().lower().replace("-", "_").replace(" ", "_")
def _normalize_branch_filter_values(value: Any) -> set[str]:
if isinstance(value, list | tuple | set | frozenset):
return {
normalized for item in value if (normalized := _normalize_branch_filter_token(item))
}
normalized = _normalize_branch_filter_token(value)
return {normalized} if normalized else set()
def _record_branch_labels(record: dict[str, Any]) -> set[str]:
labels: set[str] = set()
def add_label(value: Any) -> None:
if isinstance(value, str):
normalized = _normalize_branch_filter_token(value)
if normalized:
labels.add(normalized)
elif isinstance(value, list):
for item in value:
add_label(item)
for key in ("branch", "profile", "type", "task_type", "category"):
add_label(record.get(key))
metadata = record.get("metadata")
if isinstance(metadata, dict):
for key in ("branch", "profile", "type", "task", "workflow", "project_area"):
add_label(metadata.get(key))
add_label(metadata.get("tags"))
add_label(record.get("tags"))
return labels
def _record_repo_context_terms(record: dict[str, Any]) -> set[str]:
values: list[str] = []
repo_context = record.get("repo_context")
if isinstance(repo_context, list):
values.extend(str(item).strip().lower() for item in repo_context if str(item).strip())
elif isinstance(repo_context, str) and repo_context.strip():
values.append(repo_context.strip().lower())
metadata = record.get("metadata")
if isinstance(metadata, dict):
for key in ("project_area", "audience"):
value = metadata.get(key)
if isinstance(value, str) and value.strip():
values.append(value.strip().lower())
return set(values)
def _record_branch_filter_signals(record: dict[str, Any]) -> BranchFilterSignals:
labels = _record_branch_labels(record)
metadata = record.get("metadata")
task_types = _normalize_branch_filter_values(record.get("task_type"))
if isinstance(metadata, dict):
task_types.update(_normalize_branch_filter_values(metadata.get("task")))
task_types.update(_normalize_branch_filter_values(metadata.get("workflow")))
languages = _normalize_branch_filter_values(record.get("language"))
if isinstance(metadata, dict):
languages.update(_normalize_branch_filter_values(metadata.get("language")))
record_types = _normalize_branch_filter_values(record.get("type"))
if isinstance(metadata, dict):
record_types.update(_normalize_branch_filter_values(metadata.get("type")))
presence_keys = {
_normalize_branch_filter_token(key)
for key, value in record.items()
if key != "metadata" and value not in (None, "", [], {}, ())
}
return BranchFilterSignals(
explicit_branches=frozenset(label for label in labels if label in TEXT_TRAINABLE_BRANCHES),
record_types=frozenset(record_types),
task_types=frozenset(task_types),
languages=frozenset(languages),
repo_context_terms=frozenset(_record_repo_context_terms(record)),
presence_keys=frozenset(presence_keys),
)
def _preference_branch_filter_signals(example: PreferenceExample) -> BranchFilterSignals:
explicit_branches = _normalize_branch_filter_values(example.branch)
return BranchFilterSignals(
explicit_branches=frozenset(
label for label in explicit_branches if label in TEXT_TRAINABLE_BRANCHES
),
task_types=frozenset(_normalize_branch_filter_values(example.task_type)),
languages=frozenset(_normalize_branch_filter_values(example.language)),
repo_context_terms=frozenset(
_normalize_branch_filter_token(item)
for item in example.repo_context
if str(item).strip()
),
presence_keys=frozenset(
key
for key, value in {
"edit_target": example.edit_target,
"context": example.context,
"execution_required": example.execution_required,
}.items()
if value not in (None, "", False)
),
)
def _matches_branch_filter_rule(
signals: BranchFilterSignals,
*,
branch_name: str,
branch_filter_rules: dict[str, dict[str, Any]] | None,
) -> bool:
normalized_branch = _normalize_branch_filter_token(branch_name)
if normalized_branch not in TEXT_TRAINABLE_BRANCHES:
return True
rules = (branch_filter_rules or DEFAULT_BRANCH_DATASET_FILTER_RULES).get(normalized_branch, {})
if not rules:
return True
include_explicit_branches = _normalize_branch_filter_values(
rules.get("include_explicit_branches")
)
exclude_explicit_branches = _normalize_branch_filter_values(
rules.get("exclude_explicit_branches")
)
if signals.explicit_branches.intersection(exclude_explicit_branches):
return False
if signals.explicit_branches.intersection(include_explicit_branches):
return True
positive_rule_matches = []
for rule_key, signal_values in (
("include_record_types", signals.record_types),
("include_task_types", signals.task_types),
("include_languages", signals.languages),
("include_repo_context_terms", signals.repo_context_terms),
("include_presence_keys", signals.presence_keys),
):
required_values = _normalize_branch_filter_values(rules.get(rule_key))
if required_values:
positive_rule_matches.append(bool(signal_values.intersection(required_values)))
allow_unlabeled = bool(rules.get("allow_unlabeled", False))
if any(positive_rule_matches):
return True
if positive_rule_matches or include_explicit_branches:
return allow_unlabeled and not signals.explicit_branches
if allow_unlabeled and not signals.explicit_branches:
return True
return not signals.explicit_branches
def _filter_records_for_branch(
records: list[dict[str, Any]],
*,
branch_name: str,
split_name: str,
branch_filter_rules: dict[str, dict[str, Any]] | None = None,
) -> tuple[list[dict[str, Any]], DatasetBranchFilterReport]:
if branch_name not in TEXT_TRAINABLE_BRANCHES:
report = DatasetBranchFilterReport(
split_name=split_name,
branch_name=branch_name,
input_records=len(records),
kept_records=len(records),
dropped_records=0,
)
return records, report
filtered_records = [
record
for record in records
if _matches_branch_filter_rule(
_record_branch_filter_signals(record),
branch_name=branch_name,
branch_filter_rules=branch_filter_rules,
)
]
report = DatasetBranchFilterReport(
split_name=split_name,
branch_name=branch_name,
input_records=len(records),
kept_records=len(filtered_records),
dropped_records=max(len(records) - len(filtered_records), 0),
)
return filtered_records, report
def _apply_branch_dataset_filter_to_split(
split: Any,
*,
split_name: str,
config: TrainingConfig,
allow_empty: bool = False,
) -> tuple[Any | None, DatasetBranchFilterReport]:
filtered_records, report = _filter_records_for_branch(
_materialize_split_records(split),
branch_name=config.branch_name,
split_name=split_name,
branch_filter_rules=config.branch_dataset_filter_rules,
)
if not filtered_records:
message = (
f"Branch dataset filter atmeta visus ierakstus splitam '{split_name}' "
f"atzaram '{config.branch_name}'. Ienākošie={report.input_records}."
)
if allow_empty:
logger.warning("%s Šo splitu izlaidīšu šajā training skrējienā.", message)
return None, report
raise ValueError(message)
return _rebuild_split_like(split, filtered_records), report
def _dataset_quality_config(config: TrainingConfig) -> DatasetQualityGateConfig:
return DatasetQualityGateConfig(
enabled=config.quality_gate_enabled,
dedupe_enabled=config.dedupe_enabled,
min_text_chars=config.quality_min_text_chars,
max_text_chars=max(config.max_seq_length * 8, config.quality_min_text_chars),
)
def _dataset_scoring_config(config: TrainingConfig) -> DatasetScoringConfig:
return DatasetScoringConfig(
enabled=config.scoring_enabled,
weighted_repetition_enabled=config.weighted_repetition_enabled,
max_text_chars=max(config.max_seq_length * 8, config.quality_min_text_chars),
medium_score_repeat_count=config.medium_score_repeat_count,
high_score_repeat_count=config.high_score_repeat_count,
source_weighting_enabled=config.source_weighting_enabled,
source_weight_map=config.source_weight_map.copy(),
category_weight_map=config.category_weight_map.copy(),
max_effective_repeat_count=config.max_effective_repeat_count,
benchmark_feedback_enabled=config.benchmark_feedback_enabled,
benchmark_feedback_path=config.benchmark_feedback_path,
benchmark_feedback_boost_scale=config.benchmark_feedback_boost_scale,
benchmark_feedback_max_multiplier=config.benchmark_feedback_max_multiplier,
)
def _benchmark_feedback_discovery_candidates(config: TrainingConfig) -> list[Path]:
output_dir = Path(config.output_dir)
candidates: list[Path] = []
direct_candidate = output_dir / "benchmark-feedback.json"
if direct_candidate.is_file():
candidates.append(direct_candidate)
search_roots = [output_dir.parent]
if output_dir.parent != output_dir.parent.parent:
search_roots.append(output_dir.parent.parent)
seen: set[Path] = {direct_candidate.resolve()} if direct_candidate.exists() else set()
for root in search_roots:
if not root.exists():
continue
for candidate in root.rglob("benchmark-feedback.json"):
try:
resolved = candidate.resolve()
except OSError:
continue
if resolved in seen or not candidate.is_file():
continue
seen.add(resolved)
candidates.append(candidate)
return candidates
def _discover_benchmark_feedback_path(config: TrainingConfig) -> str:
candidates = _benchmark_feedback_discovery_candidates(config)
if not candidates:
return ""
newest = max(candidates, key=lambda path: path.stat().st_mtime)
return str(newest)
def _load_dataset_benchmark_feedback(config: TrainingConfig) -> DatasetBenchmarkFeedback | None:
if not config.benchmark_feedback_enabled:
return None
feedback_path = config.benchmark_feedback_path
if not feedback_path and config.benchmark_feedback_auto_discover:
feedback_path = _discover_benchmark_feedback_path(config)
if not feedback_path:
return None
discovery_mode = "explicit" if config.benchmark_feedback_path else "auto_discovered"
feedback = load_benchmark_feedback(
feedback_path,
targets=_default_branch_benchmark_targets(config),
boost_scale=config.benchmark_feedback_boost_scale,
max_multiplier=config.benchmark_feedback_max_multiplier,
)
return DatasetBenchmarkFeedback(
artifact_path=feedback.artifact_path,
deficient_metrics=feedback.deficient_metrics,
overall_multiplier=feedback.overall_multiplier,
discovery_mode=discovery_mode,
)
def _build_scoring_dashboard_payload(
*,
scoring_report: DatasetScoringReport | None,
benchmark_feedback: DatasetBenchmarkFeedback | None,
) -> dict[str, Any]:
if scoring_report is None and benchmark_feedback is None:
return {}
payload: dict[str, Any] = {}
if scoring_report is not None:
payload["scoring_dashboard"] = {
split_name: {
"sources": split_report.source_dashboard,
"categories": split_report.category_dashboard,
}
for split_name, split_report in scoring_report.splits.items()
}
payload.update(_build_flattened_scoring_dashboard_metrics(scoring_report))
if benchmark_feedback is not None:
payload["benchmark_feedback"] = build_benchmark_feedback_artifact(benchmark_feedback)
return payload
def _build_flattened_scoring_dashboard_metrics(
scoring_report: DatasetScoringReport | None,
) -> dict[str, float]:
if scoring_report is None:
return {}
flattened: dict[str, float] = {}
for split_name, split_report in scoring_report.splits.items():
for dimension_name, dimension_payload in (
("sources", split_report.source_dashboard),
("categories", split_report.category_dashboard),
):
for label, metrics in dimension_payload.items():
for metric_name, metric_value in metrics.items():
flattened[
f"scoring_dashboard_{split_name}_{dimension_name}_{label}_{metric_name}"
] = float(metric_value)
return flattened
def _apply_quality_gate_to_split(
split: Any,
*,
split_name: str,
config: TrainingConfig,
allow_empty: bool = False,
) -> tuple[Any | None, DatasetQualitySplitReport]:
filtered_records, report = apply_quality_gate_to_records(
_materialize_split_records(split),
split_name=split_name,
config=_dataset_quality_config(config),
)
if not filtered_records:
message = (
f"Dataset quality gate atmeta visus ierakstus splitam '{split_name}'. "
f"Ienākošie={report.input_records}, paturētie={report.kept_records}, "
f"atmestie={report.dropped_records}, dublikāti={report.duplicates_removed}. "
"Pārbaudi īsos, zemas kvalitātes vai dublētos ierakstus."
)
if allow_empty:
logger.warning(
"%s Iemesli=%s; piemēri=%s. Šo splitu izlaidīšu šajā training skrējienā.",
message,
report.reasons,
report.sample_rejections,
)
return None, report
raise ValueError(message)
return _rebuild_split_like(split, filtered_records), report
def _apply_scoring_to_split(
split: Any,
*,
split_name: str,
config: TrainingConfig,
expand_weights: bool,
benchmark_feedback: DatasetBenchmarkFeedback | None,
) -> tuple[Any, DatasetScoringSplitReport]:
scored_records, report = apply_scoring_to_records(
_materialize_split_records(split),
split_name=split_name,
config=_dataset_scoring_config(config),
expand_weights=expand_weights,
benchmark_feedback=benchmark_feedback,
)
if not scored_records:
raise ValueError(
f"Dataset scoring pipeline neatgrieza nevienu ierakstu splitam '{split_name}'. "
f"Ienākošie={report.input_records}, expanded={report.expanded_records}, "
f"source_weighting={config.source_weighting_enabled}, "
f"benchmark_feedback={bool(benchmark_feedback)}."
)
return _rebuild_split_like(split, scored_records), report
def build_branch_training_configs(config: TrainingConfig) -> list[TrainingConfig]:
"""Izveido konfigurāciju katram arhitektūras atzaram."""
base_output_dir = Path(config.output_dir)
branch_specs = (
{
"branch_name": "master",
"branch_focus": "Maris AI realtime_text_chat",
"adapter_type": config.adapter_type,
"model_name": config.model_name,
"output_dir": base_output_dir / "master",
"hub_model_id": config.text_model_id,
"benchmark_name": _resolve_branch_benchmark_name(config, "master"),
"benchmark_dataset_path": _resolve_branch_benchmark_dataset_path(config, "master"),
"benchmark_gate_enabled": True,
"benchmark_min_overall": max(config.benchmark_min_overall, 0.76),
"quality_min_text_chars": max(config.quality_min_text_chars, 12),
"category_weight_map": {
**config.category_weight_map,
"reasoning": max(float(config.category_weight_map.get("reasoning", 1.0)), 1.2),
"helpfulness": max(float(config.category_weight_map.get("helpfulness", 1.0)), 1.15),
"grounding": max(float(config.category_weight_map.get("grounding", 1.0)), 1.15),
"latvian_quality": max(
float(config.category_weight_map.get("latvian_quality", 1.0)),
1.1,
),
},
},
{
"branch_name": "coder",
"branch_focus": "Maris AI code_generation_repo_grounded",
"adapter_type": config.adapter_type,
"model_name": config.model_name,
"output_dir": base_output_dir / "coder",
"hub_model_id": config.hub_model_id,
"benchmark_name": _resolve_branch_benchmark_name(config, "coder"),
"benchmark_dataset_path": _resolve_branch_benchmark_dataset_path(config, "coder"),
"preference_dataset_path": _resolve_branch_preference_dataset_path(config, "coder"),
"benchmark_gate_enabled": True,
"benchmark_min_overall": max(config.benchmark_min_overall, 0.76),
"quality_min_text_chars": max(config.quality_min_text_chars, 18),
"category_weight_map": {
**config.category_weight_map,
"coding": max(float(config.category_weight_map.get("coding", 1.0)), 1.35),
"debugging": max(float(config.category_weight_map.get("debugging", 1.0)), 1.35),
"grounding": max(float(config.category_weight_map.get("grounding", 1.0)), 1.25),
"refactor": max(float(config.category_weight_map.get("refactor", 1.0)), 1.2),
"tests": max(float(config.category_weight_map.get("tests", 1.0)), 1.15),
"unsafe": max(float(config.category_weight_map.get("unsafe", 1.0)), 1.15),
},
},
{
"branch_name": "planner",
"branch_focus": "Maris AI autonomous_tasks",
"adapter_type": config.adapter_type,
"model_name": config.model_name,
"output_dir": base_output_dir / "planner",
"hub_model_id": config.hub_model_id,
"benchmark_name": _resolve_branch_benchmark_name(config, "planner"),
"benchmark_dataset_path": _resolve_branch_benchmark_dataset_path(config, "planner"),
"benchmark_gate_enabled": True,
"benchmark_min_overall": max(config.benchmark_min_overall, 0.76),
"quality_min_text_chars": max(config.quality_min_text_chars, 18),
"category_weight_map": {
**config.category_weight_map,
"reasoning": max(float(config.category_weight_map.get("reasoning", 1.0)), 1.3),
"planning": max(float(config.category_weight_map.get("planning", 1.0)), 1.3),
"grounding": max(float(config.category_weight_map.get("grounding", 1.0)), 1.2),
"safety": max(float(config.category_weight_map.get("safety", 1.0)), 1.1),
},
},
{
"branch_name": "image",
"branch_focus": "Maris AI image_generation",
"adapter_type": "specialist_model",
"model_name": config.image_model_id,
"output_dir": base_output_dir / "image",
"hub_model_id": config.image_model_id,
},
{
"branch_name": "music",
"branch_focus": "Maris AI music_generation",
"adapter_type": "specialist_model",
"model_name": config.music_model_id,
"output_dir": base_output_dir / "music",
"hub_model_id": config.music_model_id,
},
{
"branch_name": "tts",
"branch_focus": "Maris AI realtime_tts",
"adapter_type": "specialist_model",
"model_name": config.tts_model_id,
"output_dir": base_output_dir / "tts",
"hub_model_id": config.tts_model_id,
},
{
"branch_name": "stt",
"branch_focus": "Maris AI realtime_stt",
"adapter_type": "specialist_model",
"model_name": config.stt_model_id,
"output_dir": base_output_dir / "stt",
"hub_model_id": config.stt_model_id,
},
{
"branch_name": "video",
"branch_focus": "Maris AI video_generation",
"adapter_type": "specialist_model",
"model_name": config.video_model_id,
"output_dir": base_output_dir / "video",
"hub_model_id": config.video_model_id,
},
)
return [
TrainingConfig(
model_name=str(branch["model_name"]),
branch_name=str(branch["branch_name"]),
branch_focus=str(branch["branch_focus"]),
adapter_type=str(branch["adapter_type"]),
lora_r=config.lora_r,
lora_alpha=config.lora_alpha,
lora_dropout=config.lora_dropout,
lora_bias=config.lora_bias,
peft_target_modules=list(config.peft_target_modules),
qlora_quant_type=config.qlora_quant_type,
qlora_use_double_quant=config.qlora_use_double_quant,
qlora_compute_dtype=config.qlora_compute_dtype,
dataset_repo=config.dataset_repo,
eval_dataset_repo=config.eval_dataset_repo,
output_dir=str(branch["output_dir"]),
hub_model_id=str(branch["hub_model_id"]),
text_model_id=config.text_model_id,
image_model_id=config.image_model_id,
music_model_id=config.music_model_id,
tts_model_id=config.tts_model_id,
stt_model_id=config.stt_model_id,
video_model_id=config.video_model_id,
num_epochs=config.num_epochs,
learning_rate=config.learning_rate,
per_device_train_batch_size=config.per_device_train_batch_size,
per_device_eval_batch_size=config.per_device_eval_batch_size,
gradient_accumulation_steps=config.gradient_accumulation_steps,
warmup_ratio=config.warmup_ratio,
weight_decay=config.weight_decay,
logging_steps=config.logging_steps,
save_steps=config.save_steps,
eval_steps=config.eval_steps,
save_total_limit=config.save_total_limit,
max_seq_length=config.max_seq_length,
validation_split_ratio=config.validation_split_ratio,
seed=config.seed,
fp16=config.fp16,
bf16=config.bf16,
gradient_checkpointing=config.gradient_checkpointing,
gradient_checkpointing_use_reentrant=config.gradient_checkpointing_use_reentrant,
report_to=list(config.report_to),
push_to_hub=config.push_to_hub,
save_safetensors=config.save_safetensors,
lr_scheduler_type=config.lr_scheduler_type,
benchmark_dataset_path=str(
branch.get("benchmark_dataset_path", config.benchmark_dataset_path) or ""
),
benchmark_name=str(branch.get("benchmark_name", config.benchmark_name)),
benchmark_levels=list(config.benchmark_levels),
benchmark_min_overall=float(
branch.get("benchmark_min_overall", config.benchmark_min_overall)
),
benchmark_gate_enabled=bool(
branch.get("benchmark_gate_enabled", config.benchmark_gate_enabled)
),
branch_benchmark_targets={
key: value.copy() for key, value in config.branch_benchmark_targets.items()
},
branch_benchmark_names=config.branch_benchmark_names.copy(),
branch_benchmark_dataset_paths=config.branch_benchmark_dataset_paths.copy(),
branch_preference_dataset_paths=config.branch_preference_dataset_paths.copy(),
branch_dataset_filter_rules={
key: value.copy() for key, value in config.branch_dataset_filter_rules.items()
},
preference_dataset_path=str(
branch.get("preference_dataset_path", config.preference_dataset_path) or ""
),
preference_optimization=config.preference_optimization,
preference_beta=config.preference_beta,
preference_max_prompt_length=config.preference_max_prompt_length,
preference_max_length=config.preference_max_length,
preference_reference_model=config.preference_reference_model,
quality_gate_enabled=config.quality_gate_enabled,
dedupe_enabled=config.dedupe_enabled,
quality_min_text_chars=int(
branch.get("quality_min_text_chars", config.quality_min_text_chars)
),
scoring_enabled=config.scoring_enabled,
weighted_repetition_enabled=config.weighted_repetition_enabled,
medium_score_repeat_count=config.medium_score_repeat_count,
high_score_repeat_count=config.high_score_repeat_count,
source_weighting_enabled=config.source_weighting_enabled,
source_weight_map=config.source_weight_map.copy(),
category_weight_map=dict(branch.get("category_weight_map", config.category_weight_map)),
max_effective_repeat_count=config.max_effective_repeat_count,
benchmark_feedback_enabled=config.benchmark_feedback_enabled,
benchmark_feedback_auto_discover=config.benchmark_feedback_auto_discover,
benchmark_feedback_path=config.benchmark_feedback_path,
benchmark_feedback_boost_scale=config.benchmark_feedback_boost_scale,
benchmark_feedback_max_multiplier=config.benchmark_feedback_max_multiplier,
continue_from_latest_artifact=config.continue_from_latest_artifact,
continue_model_path=config.continue_model_path,
)
for branch in branch_specs
]
def _augment_metrics(metrics: dict[str, float], *, eval_dataset: Any | None) -> dict[str, float]:
enriched = dict(metrics)
if eval_dataset is not None:
enriched["eval_examples"] = float(len(eval_dataset))
if "eval_loss" in enriched:
# Cap extremely large loss values before exponentiation to avoid overflow.
# math.exp(20) is already ~4.85e8, which is a generous upper bound for a
# diagnostic perplexity metric stored in training artifacts.
enriched["perplexity"] = float(
math.exp(min(enriched["eval_loss"], MAX_LOSS_FOR_PERPLEXITY))
)
return enriched
def _default_branch_benchmark_targets(config: TrainingConfig) -> dict[str, float]:
return config.branch_benchmark_targets.get(config.branch_name) or {
"overall": config.benchmark_min_overall
}
def _build_benchmark_gate_artifact(
config: TrainingConfig,
benchmark_manifest: dict[str, Any],
*,
regression_report: dict[str, Any] | None = None,
) -> dict[str, Any]:
targets = _augment_release_gate_targets(
_default_branch_benchmark_targets(config),
benchmark_manifest,
)
failed_metrics = {
metric: {
"required": target,
"actual": _benchmark_metric_value(benchmark_manifest, metric),
}
for metric, target in targets.items()
if metric == "overall" or _benchmark_metric_present(benchmark_manifest, metric)
if _benchmark_metric_value(benchmark_manifest, metric) < float(target)
}
regression_count = (
int(regression_report.get("regression_count", 0) or 0) if regression_report else 0
)
if regression_count > 0:
failed_metrics["regression_count"] = {"required": 0.0, "actual": float(regression_count)}
return {
"artifact_type": "benchmark-release-gate",
**_build_model_artifact_identity(config),
"benchmark_name": benchmark_manifest.get("benchmark_name", config.benchmark_name),
"passed": not failed_metrics,
"targets": targets,
"failed_metrics": failed_metrics,
"regression_policy": {
"allow_regressions": False,
"regression_count": regression_count,
},
"levels_checked": config.benchmark_levels,
}
def _augment_release_gate_targets(
targets: dict[str, float],
benchmark_manifest: dict[str, Any],
) -> dict[str, float]:
augmented = dict(targets)
score_manifest = benchmark_manifest.get("score_manifest", {})
critical_defaults = {
"success_rate": 0.85,
"grounding": 0.75,
"safety": 0.9,
"judge_overall": 0.72,
"judge_task_completion": 0.72,
"judge_instruction_following": 0.74,
"judge_safety": 0.9,
"judge_regression_risk": 0.72,
"pairwise_win_rate": 0.55,
}
if "long_context" in score_manifest:
critical_defaults["long_context"] = max(
float(augmented.get("long_context", 0.0) or 0.0), 0.72
)
if int(benchmark_manifest.get("execution_cases", 0) or 0) > 0:
critical_defaults["execution"] = 0.7
if int(benchmark_manifest.get("grounding_cases", 0) or 0) > 0:
critical_defaults["grounding"] = 0.75
if int(benchmark_manifest.get("production_like_cases", 0) or 0) > 0:
critical_defaults["production_like_pass_rate"] = 0.75
for metric, threshold in critical_defaults.items():
if _benchmark_metric_present(benchmark_manifest, metric):
augmented.setdefault(metric, threshold)
return augmented
def _benchmark_metric_present(benchmark_manifest: dict[str, Any], metric: str) -> bool:
score_manifest = benchmark_manifest.get("score_manifest", {})
return metric in score_manifest or metric in benchmark_manifest
def _benchmark_metric_value(benchmark_manifest: dict[str, Any], metric: str) -> float:
score_manifest = benchmark_manifest.get("score_manifest", {})
raw_value = (
score_manifest.get(metric) if metric in score_manifest else benchmark_manifest.get(metric)
)
return float(raw_value or 0.0)
def _same_benchmark_reference(left: dict[str, Any] | None, right: dict[str, Any]) -> bool:
if not isinstance(left, dict):
return False
return (
str(left.get("benchmark_name", "")).strip() == str(right.get("benchmark_name", "")).strip()
and str(left.get("branch", "")).strip() == str(right.get("branch", "")).strip()
and str(left.get("model", "")).strip() == str(right.get("model", "")).strip()
and str(left.get("generated_at", "")).strip() == str(right.get("generated_at", "")).strip()
)
def _select_previous_benchmark_baseline(
*,
current_manifest: dict[str, Any],
previous_history: dict[str, Any] | None,
previous_manifest: dict[str, Any] | None,
) -> dict[str, Any] | None:
runs = previous_history.get("runs") if isinstance(previous_history, dict) else None
if isinstance(runs, list):
run_objects = [dict(item) for item in runs if isinstance(item, dict)]
if run_objects:
last_run = run_objects[-1]
if _same_benchmark_reference(last_run, current_manifest):
return run_objects[-2] if len(run_objects) > 1 else None
return last_run
if _same_benchmark_reference(previous_manifest, current_manifest):
return None
return previous_manifest
def _write_benchmark_artifacts(
output_dir: Path,
config: TrainingConfig,
benchmark_manifest: dict[str, Any],
) -> tuple[dict[str, Any], dict[str, Any]]:
benchmark_manifest = _attach_human_eval_summary_to_manifest(config, benchmark_manifest)
previous_history = _load_json_if_exists(output_dir / "benchmark-history.json")
previous_manifest = _load_json_if_exists(output_dir / "benchmark-manifest.json")
regression_report = build_chat_benchmark_regression_report(
benchmark_manifest,
previous_run=_select_previous_benchmark_baseline(
current_manifest=benchmark_manifest,
previous_history=previous_history,
previous_manifest=previous_manifest,
),
)
benchmark_gate = _build_benchmark_gate_artifact(
config,
benchmark_manifest,
regression_report=regression_report,
)
benchmark_history = build_chat_benchmark_history_artifact(
benchmark_manifest,
previous_history=previous_history,
)
_save_json(output_dir / "benchmark-manifest.json", benchmark_manifest)
_save_json(output_dir / "benchmark-release-gate.json", benchmark_gate)
_save_json(output_dir / "benchmark-history.json", benchmark_history)
_save_json(output_dir / "benchmark-regression-report.json", regression_report)
_save_json(
output_dir / "benchmark-feedback.json",
build_benchmark_feedback_artifact(
load_benchmark_feedback(
output_dir / "benchmark-manifest.json",
targets=_default_branch_benchmark_targets(config),
boost_scale=config.benchmark_feedback_boost_scale,
max_multiplier=config.benchmark_feedback_max_multiplier,
)
),
)
return benchmark_gate, regression_report
def _attach_human_eval_summary_to_manifest(
config: TrainingConfig,
benchmark_manifest: dict[str, Any],
) -> dict[str, Any]:
if not config.preference_dataset_path:
return benchmark_manifest
examples = _load_filtered_preference_examples(config)
human_eval_summary = build_human_eval_summary(examples)
manifest = dict(benchmark_manifest)
manifest["human_eval_summary"] = human_eval_summary
score_manifest = dict(manifest.get("score_manifest", {}))
score_manifest["pairwise_win_rate"] = round(
float(human_eval_summary.get("pairwise_win_rate", 0.0) or 0.0),
3,
)
score_manifest["human_eval_confidence"] = round(
float(human_eval_summary.get("average_confidence", 0.0) or 0.0),
3,
)
manifest["score_manifest"] = score_manifest
return manifest
async def _run_post_training_benchmark(
config: TrainingConfig,
*,
model_path: str,
) -> dict[str, Any] | None:
if not config.benchmark_dataset_path:
return None
from transformers import pipeline # type: ignore
cases = load_chat_benchmark_dataset(config.benchmark_dataset_path)
selected_cases = select_chat_benchmark_cases(
cases,
levels=config.benchmark_levels,
branch=config.branch_name,
)
if not selected_cases:
raise ValueError("Benchmark datasetā nav neviena case izvēlētajiem benchmark leveliem.")
pipe = pipeline(
"text-generation",
model=model_path,
tokenizer=model_path,
trust_remote_code=True,
)
async def responder(case: Any) -> dict[str, Any]:
effective_profile = case.profile or {
"coder": "coder",
"planner": "planner",
}.get(config.branch_name, "general")
messages = [
{
"role": "system",
"content": build_system_prompt(
effective_profile,
persona_id=case.persona_id,
),
},
*list(case.history),
{"role": "user", "content": case.message},
]
raw_output = await asyncio.to_thread(
call_generation_pipeline,
pipe,
messages,
max_new_tokens=512,
temperature=0.0,
)
response_text = _extract_response_text(raw_output)
return {
"response": response_text,
"model": config.hub_model_id,
"tokens_used": _extract_usage_tokens(raw_output) or 0,
"persona_title": "Core Assistant",
}
results = await run_chat_benchmark_with_responder(
selected_cases,
responder=responder,
concurrency=1,
)
return build_chat_benchmark_manifest(
results,
benchmark_name=config.benchmark_name,
branch=config.branch_name,
model=config.hub_model_id,
)
def train_with_config(config: TrainingConfig) -> dict[str, float]:
"""Apmāca modeli pēc pilnas konfigurācijas."""
config = _apply_branch_runtime_defaults(_normalize_training_runtime_config(config))
_ensure_runtime_home_dir()
training_dataset_repos = _resolve_training_dataset_repos(config)
eval_dataset_repos = _resolve_eval_dataset_repos(config)
try:
_emit_training_progress_event(
"prepare_dataset",
stage="preparing",
label="Ielādē training datasetu",
total_epochs=config.num_epochs,
dataset_repo=config.dataset_repo,
dataset_repos=training_dataset_repos,
)
logger.info("Ielādē training datasetus: %s", ", ".join(training_dataset_repos))
dataset = _load_combined_hf_dataset(training_dataset_repos)
except HFDatasetError as exc:
logger.error("Apmācība apturēta: %s", exc)
raise SystemExit(str(exc)) from None
except Exception as exc: # noqa: BLE001
logger.error("Apmācības kļūda: %s", exc)
raise
try:
from transformers import ( # type: ignore
DataCollatorForLanguageModeling,
Trainer,
TrainingArguments,
)
try:
from transformers import TrainerCallback # type: ignore
except ImportError: # pragma: no cover - fallback for lightweight test doubles
class TrainerCallback: # type: ignore[no-redef]
pass
training_model_source = _resolve_training_model_source(config)
_emit_training_progress_event(
"prepare_model",
stage="preparing",
label="Ielādē tokenizeri un modeli",
total_epochs=config.num_epochs,
model_source=training_model_source,
)
logger.info("Ielādē modeli: %s", training_model_source)
tokenizer = _load_tokenizer(training_model_source, config)
_configure_tokenizer(tokenizer, config)
model = _prepare_training_model(training_model_source, tokenizer, config)
train_split, eval_split = _prepare_train_eval_splits(dataset, config)
if eval_dataset_repos:
_emit_training_progress_event(
"prepare_eval_dataset",
stage="preparing",
label="Ielādē eval datasetu",
total_epochs=config.num_epochs,
dataset_repo=config.eval_dataset_repo or eval_dataset_repos[0],
dataset_repos=eval_dataset_repos,
)
logger.info("Ielādē atsevišķus eval datasetus: %s", ", ".join(eval_dataset_repos))
eval_source_dataset = _load_combined_hf_dataset(eval_dataset_repos)
eval_split = _select_eval_split(
eval_source_dataset,
config,
allow_train_fallback=True,
)
train_split, train_branch_filter_report = _apply_branch_dataset_filter_to_split(
train_split,
split_name="train",
config=config,
)
eval_branch_filter_report: DatasetBranchFilterReport | None = None
if eval_split is not None:
eval_split, eval_branch_filter_report = _apply_branch_dataset_filter_to_split(
eval_split,
split_name="eval",
config=config,
allow_empty=True,
)
train_split, train_quality_report = _apply_quality_gate_to_split(
train_split,
split_name="train",
config=config,
)
benchmark_feedback = _load_dataset_benchmark_feedback(config)
train_split, train_scoring_report = _apply_scoring_to_split(
train_split,
split_name="train",
config=config,
expand_weights=True,
benchmark_feedback=benchmark_feedback,
)
eval_quality_report: DatasetQualitySplitReport | None = None
eval_scoring_report: DatasetScoringSplitReport | None = None
if eval_split is not None:
eval_split, eval_quality_report = _apply_quality_gate_to_split(
eval_split,
split_name="eval",
config=config,
allow_empty=True,
)
if eval_split is not None:
eval_split, eval_scoring_report = _apply_scoring_to_split(
eval_split,
split_name="eval",
config=config,
expand_weights=False,
benchmark_feedback=benchmark_feedback,
)
_emit_training_progress_event(
"prepare_tokenization",
stage="preparing",
label="Tokenizē treniņa un eval datus",
total_epochs=config.num_epochs,
)
train_dataset = _tokenize_dataset(train_split, tokenizer, config.max_seq_length)
eval_dataset = (
_tokenize_dataset(eval_split, tokenizer, config.max_seq_length)
if eval_split is not None
else None
)
_emit_training_progress_event(
"prepare_runtime",
stage="preparing",
label="Sagatavo treneri un runtime",
total_epochs=config.num_epochs,
)
training_args = _build_training_arguments(
TrainingArguments,
output_dir=config.output_dir,
overwrite_output_dir=True,
num_train_epochs=config.num_epochs,
learning_rate=config.learning_rate,
per_device_train_batch_size=config.per_device_train_batch_size,
per_device_eval_batch_size=config.per_device_eval_batch_size,
gradient_accumulation_steps=config.gradient_accumulation_steps,
warmup_ratio=config.warmup_ratio,
weight_decay=config.weight_decay,
logging_steps=config.logging_steps,
save_steps=config.save_steps,
eval_steps=config.eval_steps,
save_total_limit=config.save_total_limit,
lr_scheduler_type=config.lr_scheduler_type,
seed=config.seed,
fp16=config.fp16,
bf16=config.bf16,
report_to=config.report_to,
save_safetensors=config.save_safetensors,
remove_unused_columns=False,
evaluation_strategy="steps" if eval_dataset is not None else "no",
load_best_model_at_end=eval_dataset is not None,
metric_for_best_model="eval_loss" if eval_dataset is not None else None,
greater_is_better=False if eval_dataset is not None else None,
**_build_runtime_training_argument_overrides(),
**_build_distributed_training_argument_overrides(config),
)
class _SpaceTrainingProgressCallback(MarisTrainingProgressCallback, TrainerCallback):
pass
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
data_collator=DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False),
)
if hasattr(trainer, "add_callback"):
trainer.add_callback(_SpaceTrainingProgressCallback(total_epochs=config.num_epochs))
logger.info("Sāk apmācību...")
train_result = trainer.train()
metrics = dict(getattr(train_result, "metrics", {}))
if eval_dataset is not None:
metrics.update(trainer.evaluate())
metrics = _augment_metrics(metrics, eval_dataset=eval_dataset)
train_examples = len(train_dataset)
eval_examples = len(eval_dataset) if eval_dataset is not None else 0
trained_at = datetime.now(UTC).isoformat()
output_dir = Path(config.output_dir)
if _uses_preference_optimization(config):
trainer.save_model(str(output_dir))
tokenizer.save_pretrained(str(output_dir))
model, preference_trainer, preference_metrics = _run_preference_optimization(
model,
tokenizer,
config,
output_dir=output_dir,
)
metrics.update(preference_metrics)
if preference_trainer is not None:
trainer = preference_trainer
quality_report = build_dataset_quality_report(
config=_dataset_quality_config(config),
train_report=train_quality_report,
eval_report=eval_quality_report,
)
scoring_report = build_dataset_scoring_report(
config=_dataset_scoring_config(config),
train_report=train_scoring_report,
eval_report=eval_scoring_report,
)
metrics["branch_filter_train_kept"] = float(train_branch_filter_report.kept_records)
metrics["branch_filter_train_dropped"] = float(train_branch_filter_report.dropped_records)
metrics["quality_train_kept"] = float(train_quality_report.kept_records)
metrics["quality_train_dropped"] = float(train_quality_report.dropped_records)
metrics["quality_train_duplicates_removed"] = float(train_quality_report.duplicates_removed)
metrics["scoring_train_average_score"] = float(train_scoring_report.average_score)
metrics["scoring_train_expanded_records"] = float(train_scoring_report.expanded_records)
metrics["scoring_train_repeated_records"] = float(train_scoring_report.repeated_records)
metrics["scoring_train_average_repeat_multiplier"] = float(
train_scoring_report.average_repeat_multiplier
)
metrics["scoring_train_feedback_boosted_records"] = float(
train_scoring_report.feedback_boosted_records
)
if eval_quality_report is not None:
if eval_branch_filter_report is not None:
metrics["branch_filter_eval_kept"] = float(eval_branch_filter_report.kept_records)
metrics["branch_filter_eval_dropped"] = float(
eval_branch_filter_report.dropped_records
)
metrics["quality_eval_kept"] = float(eval_quality_report.kept_records)
metrics["quality_eval_dropped"] = float(eval_quality_report.dropped_records)
metrics["quality_eval_duplicates_removed"] = float(
eval_quality_report.duplicates_removed
)
metrics["quality_eval_skipped"] = float(eval_split is None)
elif eval_branch_filter_report is not None:
metrics["branch_filter_eval_kept"] = float(eval_branch_filter_report.kept_records)
metrics["branch_filter_eval_dropped"] = float(eval_branch_filter_report.dropped_records)
if eval_scoring_report is not None:
metrics["scoring_eval_average_score"] = float(eval_scoring_report.average_score)
metrics["scoring_eval_expanded_records"] = float(eval_scoring_report.expanded_records)
metrics["scoring_eval_feedback_boosted_records"] = float(
eval_scoring_report.feedback_boosted_records
)
trainer.save_model(str(output_dir))
tokenizer.save_pretrained(str(output_dir))
_write_training_artifacts(
output_dir,
config,
trained_at=trained_at,
train_examples=train_examples,
eval_examples=eval_examples,
metrics=metrics,
quality_report=quality_report,
scoring_report=scoring_report,
benchmark_feedback=benchmark_feedback,
)
_write_preference_artifact(output_dir, config)
benchmark_manifest = (
asyncio.run(_run_post_training_benchmark(config, model_path=str(output_dir)))
if config.benchmark_dataset_path
else None
)
if benchmark_manifest is not None:
_emit_training_progress_event(
"benchmark",
stage="benchmarking",
label=_build_training_progress_label(stage="benchmarking"),
output_dir=str(output_dir),
)
benchmark_gate, regression_report = _write_benchmark_artifacts(
output_dir,
config,
benchmark_manifest,
)
metrics["benchmark_overall"] = float(
benchmark_manifest.get("score_manifest", {}).get("overall", 0.0)
)
metrics["benchmark_gate_passed"] = 1.0 if benchmark_gate["passed"] else 0.0
metrics["benchmark_regressions"] = float(regression_report.get("regression_count", 0))
_save_json(
output_dir / "training-metrics.json",
_build_metrics_artifact(
config,
metrics,
artifact_type="training-metrics",
extra_payload=_build_scoring_dashboard_payload(
scoring_report=scoring_report,
benchmark_feedback=benchmark_feedback,
),
),
)
if config.benchmark_gate_enabled and not benchmark_gate["passed"]:
failure_details = ", ".join(
f"{metric}: {details['actual']:.3f} < {details['required']:.3f}"
for metric, details in sorted(benchmark_gate["failed_metrics"].items())
)
raise ValueError("Benchmark release gate neizgāja: " + failure_details)
_sanitize_saved_artifacts(output_dir, maris_model_id=config.hub_model_id)
_verify_saved_artifacts(output_dir)
if config.push_to_hub and hasattr(trainer, "push_to_hub"):
_emit_training_progress_event(
"publish",
stage="publishing",
label=_build_training_progress_label(stage="publishing"),
output_dir=str(output_dir),
)
trainer.push_to_hub(commit_message=f"Maris AI training sync ({config.branch_name})")
_write_training_artifacts(
output_dir,
config,
trained_at=trained_at,
train_examples=train_examples,
eval_examples=eval_examples,
metrics=metrics,
quality_report=quality_report,
scoring_report=scoring_report,
benchmark_feedback=benchmark_feedback,
)
if benchmark_manifest is not None:
_write_benchmark_artifacts(output_dir, config, benchmark_manifest)
_sanitize_saved_artifacts(output_dir, maris_model_id=config.hub_model_id)
_verify_saved_artifacts(output_dir)
_sync_output_dir_to_hub(
output_dir,
config,
commit_message=f"Maris AI artifact sync ({config.branch_name})",
)
logger.info("Apmācība pabeigta. Modelis saglabāts: %s", output_dir)
return metrics
except Exception as exc: # noqa: BLE001
logger.error("Apmācības kļūda: %s", exc)
raise
def train_branch_suite(config: TrainingConfig) -> dict[str, dict[str, Any]]:
"""Palaiž branch-oriented training pipeline visiem Maris atzariem."""
suite_results: dict[str, dict[str, Any]] = {}
base_output_dir = Path(config.output_dir)
for branch_config in build_branch_training_configs(config):
if branch_config.branch_name in TEXT_TRAINABLE_BRANCHES:
metrics = train_with_config(branch_config)
suite_results[branch_config.branch_name] = {
"status": "trained",
"maris_model_id": branch_config.hub_model_id,
"branch_focus": branch_config.branch_focus,
"output_dir": branch_config.output_dir,
"metrics": metrics,
"dataset_repo": branch_config.dataset_repo,
"maris_origin": MARIS_ORIGIN_NAME,
"maris_framework": MARIS_FRAMEWORK_NAME,
}
continue
branch_output_dir = Path(branch_config.output_dir)
branch_output_dir.mkdir(parents=True, exist_ok=True)
branch_manifest = {
"status": "external_specialist",
"maris_model_id": branch_config.hub_model_id,
"branch_focus": branch_config.branch_focus,
"output_dir": branch_config.output_dir,
"reason": "Šim atzaram nepieciešams specializēts multimodāls treniņa pipeline ārpus CausalLM trainer.",
"maris_origin": MARIS_ORIGIN_NAME,
"maris_framework": MARIS_FRAMEWORK_NAME,
}
_save_json(branch_output_dir / "branch-config.json", branch_manifest)
suite_results[branch_config.branch_name] = branch_manifest
_save_json(
base_output_dir / "branch-suite.json",
_build_branch_suite_artifact(config, suite_results),
)
return suite_results
def evaluate_with_config(
config: TrainingConfig,
*,
model_path: str | None = None,
) -> dict[str, float]:
"""Novērtē modeli ar to pašu datu pipeline, ko izmanto apmācībai."""
config = _apply_branch_runtime_defaults(config)
_ensure_runtime_home_dir()
eval_dataset_repos = _resolve_eval_dataset_repos(config)
dataset_repos = eval_dataset_repos or _resolve_training_dataset_repos(config)
try:
dataset = _load_combined_hf_dataset(dataset_repos)
except HFDatasetError as exc:
logger.error("Novērtēšana apturēta: %s", exc)
raise SystemExit(str(exc)) from None
from transformers import ( # type: ignore
DataCollatorForLanguageModeling,
Trainer,
TrainingArguments,
)
resolved_model = model_path or config.output_dir
tokenizer = _load_tokenizer(resolved_model, config)
_configure_tokenizer(tokenizer, config)
model = _load_model(resolved_model, config)
if eval_dataset_repos:
reference_split = _select_eval_split(dataset, config, allow_train_fallback=True)
else:
train_split, eval_split = _prepare_train_eval_splits(dataset, config)
reference_split = eval_split or train_split
reference_split, _ = _apply_branch_dataset_filter_to_split(
reference_split,
split_name="evaluation",
config=config,
)
reference_split, _ = _apply_quality_gate_to_split(
reference_split,
split_name="evaluation",
config=config,
)
reference_split, _ = _apply_scoring_to_split(
reference_split,
split_name="evaluation",
config=config,
expand_weights=False,
benchmark_feedback=_load_dataset_benchmark_feedback(config),
)
eval_dataset = _tokenize_dataset(reference_split, tokenizer, config.max_seq_length)
trainer = Trainer(
model=model,
args=_build_training_arguments(
TrainingArguments,
output_dir=config.output_dir,
per_device_eval_batch_size=config.per_device_eval_batch_size,
report_to=[],
remove_unused_columns=False,
**_build_runtime_training_argument_overrides(),
**_build_distributed_training_argument_overrides(config),
),
eval_dataset=eval_dataset,
data_collator=DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False),
)
metrics = _augment_metrics(dict(trainer.evaluate()), eval_dataset=eval_dataset)
output_dir = Path(config.output_dir)
_save_json(
output_dir / "evaluation-metrics.json",
_build_metrics_artifact(config, metrics, artifact_type="evaluation-metrics"),
)
if config.benchmark_dataset_path:
benchmark_manifest = asyncio.run(
_run_post_training_benchmark(config, model_path=str(resolved_model))
)
if benchmark_manifest is not None:
benchmark_gate, regression_report = _write_benchmark_artifacts(
output_dir,
config,
benchmark_manifest,
)
metrics["benchmark_overall"] = float(
benchmark_manifest.get("score_manifest", {}).get("overall", 0.0)
)
metrics["benchmark_gate_passed"] = 1.0 if benchmark_gate["passed"] else 0.0
metrics["benchmark_regressions"] = float(regression_report.get("regression_count", 0))
_save_json(
output_dir / "evaluation-metrics.json",
_build_metrics_artifact(config, metrics, artifact_type="evaluation-metrics"),
)
_sanitize_saved_artifacts(output_dir, maris_model_id=config.hub_model_id)
_verify_saved_artifacts(output_dir)
return metrics
def train(
config_path: str | None = None,
model_name: str | None = None,
dataset_repo: str | None = None,
dataset_repos: str | list[str] | None = None,
eval_dataset_repo: str | None = None,
eval_dataset_repos: str | list[str] | None = None,
benchmark_dataset_path: str | None = None,
benchmark_feedback_path: str | None = None,
preference_dataset_path: str | None = None,
preference_optimization: str | None = None,
preference_beta: float | None = None,
preference_max_prompt_length: int | None = None,
preference_max_length: int | None = None,
preference_reference_model: str | None = None,
output_dir: str | None = None,
num_epochs: int | None = None,
learning_rate: float | None = None,
max_seq_length: int | None = None,
validation_split_ratio: float | None = None,
adapter_type: str | None = None,
lora_r: int | None = None,
lora_alpha: int | None = None,
lora_dropout: float | None = None,
lora_bias: str | None = None,
peft_target_modules: str | list[str] | None = None,
qlora_quant_type: str | None = None,
qlora_use_double_quant: bool | None = None,
qlora_compute_dtype: str | None = None,
distributed_strategy: str | None = None,
distributed_config_path: str | None = None,
use_accelerate: bool | None = None,
accelerate_config_path: str | None = None,
num_processes: int | None = None,
num_machines: int | None = None,
machine_rank: int | None = None,
main_process_ip: str | None = None,
main_process_port: int | None = None,
fsdp_transformer_layer_cls_to_wrap: str | list[str] | None = None,
fsdp_min_num_params: int | None = None,
continue_from_latest_artifact: bool | None = None,
continue_model_path: str | None = None,
) -> dict[str, float]:
"""Apmāca modeli ar HuggingFace datasets datiem."""
config = load_training_config(
config_path,
overrides={
"model_name": model_name,
"dataset_repo": dataset_repo,
"dataset_repos": dataset_repos,
"eval_dataset_repo": eval_dataset_repo,
"eval_dataset_repos": eval_dataset_repos,
"benchmark_dataset_path": benchmark_dataset_path,
"benchmark_feedback_path": benchmark_feedback_path,
"preference_dataset_path": preference_dataset_path,
"preference_optimization": preference_optimization,
"preference_beta": preference_beta,
"preference_max_prompt_length": preference_max_prompt_length,
"preference_max_length": preference_max_length,
"preference_reference_model": preference_reference_model,
"output_dir": output_dir,
"num_epochs": num_epochs,
"learning_rate": learning_rate,
"max_seq_length": max_seq_length,
"validation_split_ratio": validation_split_ratio,
"adapter_type": adapter_type,
"lora_r": lora_r,
"lora_alpha": lora_alpha,
"lora_dropout": lora_dropout,
"lora_bias": lora_bias,
"peft_target_modules": peft_target_modules,
"qlora_quant_type": qlora_quant_type,
"qlora_use_double_quant": qlora_use_double_quant,
"qlora_compute_dtype": qlora_compute_dtype,
"distributed_strategy": distributed_strategy,
"distributed_config_path": distributed_config_path,
"use_accelerate": use_accelerate,
"accelerate_config_path": accelerate_config_path,
"num_processes": num_processes,
"num_machines": num_machines,
"machine_rank": machine_rank,
"main_process_ip": main_process_ip,
"main_process_port": main_process_port,
"fsdp_transformer_layer_cls_to_wrap": fsdp_transformer_layer_cls_to_wrap,
"fsdp_min_num_params": fsdp_min_num_params,
"continue_from_latest_artifact": continue_from_latest_artifact,
"continue_model_path": continue_model_path,
},
)
return train_with_config(config)
|