File size: 230,815 Bytes
b1d4de0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 |
# File: datatrove-main/src/datatrove/data.py """""" from dataclasses import dataclass, field from typing import Generator, NewType class MediaType: IMAGE = 0 VIDEO = 1 AUDIO = 2 @dataclass class Media: type: int url: str alt: str | None = None local_path: str | None = None @dataclass class Document: text: str id: str media: list[Media] = field(default_factory=list) metadata: dict[str, str | int | float | bool] = field(default_factory=dict) DocumentsPipeline = NewType('DocumentsPipeline', Generator[Document, None, None] | None) # File: datatrove-main/src/datatrove/executor/base.py import dataclasses import json import random import time from abc import ABC, abstractmethod from collections import deque from collections.abc import Sequence from typing import Callable from datatrove.io import DataFolderLike, get_datafolder from datatrove.pipeline.base import PipelineStep from datatrove.utils.logging import add_task_logger, close_task_logger, get_random_str, get_timestamp, log_pipeline, logger from datatrove.utils.stats import PipelineStats class PipelineExecutor(ABC): @abstractmethod def __init__(self, pipeline: list[PipelineStep | Callable], logging_dir: DataFolderLike=None, skip_completed: bool=True, randomize_start_duration: int=0): self.pipeline: list[PipelineStep | Callable] = pipeline self.logging_dir = get_datafolder(logging_dir if logging_dir else f'logs/{get_timestamp()}_{get_random_str()}') self.skip_completed = skip_completed self.randomize_start_duration = randomize_start_duration @abstractmethod def run(self): pass @property @abstractmethod def world_size(self) -> int: return 0 def _run_for_rank(self, rank: int, local_rank: int=0) -> PipelineStats: if self.is_rank_completed(rank): logger.info(f'Skipping rank={rank!r} as it has already been completed.') return PipelineStats() logfile = add_task_logger(self.logging_dir, rank, local_rank) log_pipeline(self.pipeline) if self.randomize_start_duration > 0: time.sleep(random.randint(0, self.randomize_start_duration)) try: pipelined_data = None for pipeline_step in self.pipeline: if callable(pipeline_step): pipelined_data = pipeline_step(pipelined_data, rank, self.world_size) elif isinstance(pipeline_step, Sequence) and (not isinstance(pipeline_step, str)): pipelined_data = pipeline_step else: raise ValueError if pipelined_data: deque(pipelined_data, maxlen=0) logger.success(f'Processing done for rank={rank!r}') stats = PipelineStats(self.pipeline) with self.logging_dir.open(f'stats/{rank:05d}.json', 'w') as f: stats.save_to_disk(f) logger.info(stats.get_repr(f'Task {rank}')) self.mark_rank_as_completed(rank) except Exception as e: logger.exception(e) raise e finally: close_task_logger(logfile) return stats def is_rank_completed(self, rank: int) -> bool: return self.skip_completed and self.logging_dir.isfile(f'completions/{rank:05d}') def mark_rank_as_completed(self, rank: int): self.logging_dir.open(f'completions/{rank:05d}', 'w').close() def get_incomplete_ranks(self, ranks=None) -> list[int]: completed = set(self.logging_dir.list_files('completions')) return list(filter(lambda rank: not self.skip_completed or f'completions/{rank:05d}' not in completed, ranks if ranks is not None else range(self.world_size))) def to_json(self, indent=4) -> str: data = self.__dict__ data['pipeline'] = [{a: b for (a, b) in x.__dict__.items() if a != 'stats'} for x in data['pipeline']] return json.dumps(data, indent=indent) def save_executor_as_json(self, indent: int=4): with self.logging_dir.open('executor.json', 'w') as f: json.dump(self, f, cls=ExecutorJSONEncoder, indent=indent) class ExecutorJSONEncoder(json.JSONEncoder): def default(self, o): if dataclasses.is_dataclass(o): return dataclasses.asdict(o) if isinstance(o, PipelineExecutor): return o.__dict__ | {'world_size': o.world_size} if isinstance(o, PipelineStep): return {a: b for (a, b) in o.__dict__.items() if a != 'stats'} return str(o) # File: datatrove-main/src/datatrove/executor/local.py import time from copy import deepcopy from functools import partial from typing import Callable import multiprocess from datatrove.executor.base import PipelineExecutor from datatrove.io import DataFolderLike from datatrove.pipeline.base import PipelineStep from datatrove.utils.logging import logger from datatrove.utils.stats import PipelineStats class LocalPipelineExecutor(PipelineExecutor): def __init__(self, pipeline: list[PipelineStep | Callable], tasks: int=1, workers: int=-1, logging_dir: DataFolderLike=None, depends: 'LocalPipelineExecutor'=None, skip_completed: bool=True, start_method: str='forkserver', local_tasks: int=-1, local_rank_offset: int=0, randomize_start_duration: int=0): super().__init__(pipeline, logging_dir, skip_completed, randomize_start_duration) self.tasks = tasks self.workers = workers if workers != -1 else tasks self.start_method = start_method self.local_tasks = local_tasks if local_tasks != -1 else tasks self.local_rank_offset = local_rank_offset self.depends = depends if self.local_rank_offset + self.local_tasks > self.tasks: raise ValueError(f'Local tasks go beyond the total tasks (local_rank_offset + local_tasks = {self.local_rank_offset + self.local_tasks} > {self.tasks} = tasks)') self._launched = False def _launch_run_for_rank(self, rank: int, ranks_q, completed=None, completed_lock=None) -> PipelineStats: local_rank = ranks_q.get() try: return self._run_for_rank(rank, local_rank) finally: if completed and completed_lock: with completed_lock: completed.value += 1 logger.info(f'{completed.value}/{self.world_size} tasks completed.') ranks_q.put(local_rank) def run(self): assert not self.depends or isinstance(self.depends, LocalPipelineExecutor), 'depends= must be a LocalPipelineExecutor' if self.depends: if not self.depends._launched: logger.info(f'Launching dependency job "{self.depends}"') self.depends.run() while (incomplete := len(self.depends.get_incomplete_ranks())) > 0: logger.info(f'Dependency job still has {incomplete}/{self.depends.world_size} tasks. Waiting...') time.sleep(2 * 60) self._launched = True if all(map(self.is_rank_completed, range(self.local_rank_offset, self.local_rank_offset + self.local_tasks))): logger.info(f'Not doing anything as all {self.local_tasks} tasks have already been completed.') return self.save_executor_as_json() mg = multiprocess.Manager() ranks_q = mg.Queue() for i in range(self.workers): ranks_q.put(i) ranks_to_run = self.get_incomplete_ranks(range(self.local_rank_offset, self.local_rank_offset + self.local_tasks)) if (skipped := (self.local_tasks - len(ranks_to_run))) > 0: logger.info(f'Skipping {skipped} already completed tasks') if self.workers == 1: pipeline = self.pipeline stats = [] for rank in ranks_to_run: self.pipeline = deepcopy(pipeline) stats.append(self._launch_run_for_rank(rank, ranks_q)) else: completed_counter = mg.Value('i', skipped) completed_lock = mg.Lock() ctx = multiprocess.get_context(self.start_method) with ctx.Pool(self.workers) as pool: stats = list(pool.imap_unordered(partial(self._launch_run_for_rank, ranks_q=ranks_q, completed=completed_counter, completed_lock=completed_lock), ranks_to_run)) stats = sum(stats, start=PipelineStats()) with self.logging_dir.open('stats.json', 'wt') as statsfile: stats.save_to_disk(statsfile) logger.success(stats.get_repr(f'All {self.local_tasks} tasks')) return stats @property def world_size(self) -> int: return self.tasks # File: datatrove-main/src/datatrove/executor/slurm.py from __future__ import annotations import json import math import os import signal import subprocess import sys import tempfile import textwrap import time from copy import deepcopy from typing import Callable import dill from dill import CONTENTS_FMODE from datatrove.executor.base import PipelineExecutor from datatrove.io import DataFolderLike from datatrove.pipeline.base import PipelineStep from datatrove.utils.logging import get_random_str, get_timestamp, logger def requeue_handler(signum, _frame): signame = signal.Signals(signum).name logger.warning(f'Received signal {signum} ({signame}). Requeueing and exiting...') subprocess.run(['scontrol', 'requeue', os.environ.get('SLURM_JOB_ID')]) sys.exit(15) class SlurmPipelineExecutor(PipelineExecutor): def __init__(self, pipeline: list[PipelineStep | Callable], tasks: int, time: str, partition: str, cpus_per_task: int=1, mem_per_cpu_gb: int=2, workers: int=-1, job_name: str='data_processing', qos: str='normal', env_command: str=None, condaenv: str=None, venv_path: str=None, sbatch_args: dict | None=None, max_array_size: int=1001, depends: SlurmPipelineExecutor | None=None, depends_job_id: str | None=None, logging_dir: DataFolderLike=None, skip_completed: bool=True, slurm_logs_folder: str=None, max_array_launch_parallel: bool=False, stagger_max_array_jobs: int=0, run_on_dependency_fail: bool=False, randomize_start_duration: int=0, requeue_signals: tuple[str] | None=('SIGUSR1',), mail_type: str='ALL', mail_user: str=None, requeue: bool=True, srun_args: dict=None, tasks_per_job: int=1): super().__init__(pipeline, logging_dir, skip_completed, randomize_start_duration) self.tasks = tasks self.workers = workers self.partition = partition self.cpus_per_task = cpus_per_task self.mem_per_cpu_gb = mem_per_cpu_gb self.tasks_per_job = tasks_per_job self.time = time self.job_name = job_name self.qos = qos self.env_command = env_command self.condaenv = condaenv self.venv_path = venv_path self.depends = depends self.depends_job_id = depends_job_id self._sbatch_args = sbatch_args if sbatch_args else {} self.max_array_size = max_array_size self.max_array_launch_parallel = max_array_launch_parallel self.stagger_max_array_jobs = stagger_max_array_jobs self.run_on_dependency_fail = run_on_dependency_fail self.randomize_start_duration = randomize_start_duration self.job_id = None self.requeue_signals = requeue_signals self.mail_type = mail_type self.mail_user = mail_user self.srun_args = srun_args self.slurm_logs_folder = slurm_logs_folder if slurm_logs_folder else f'slurm_logs/{self.job_name}/{get_timestamp()}_{get_random_str()}' if not self.logging_dir.is_local() else self.logging_dir.resolve_paths('slurm_logs') self.requeue = requeue def run(self): if 'SLURM_ARRAY_TASK_ID' in os.environ: slurm_rank = int(os.environ['SLURM_ARRAY_TASK_ID']) + self.max_array_size * int(os.environ.get('RUN_OFFSET', 0)) ranks_to_run_range = (slurm_rank * self.tasks_per_job, (slurm_rank + 1) * self.tasks_per_job) with self.logging_dir.open('ranks_to_run.json', 'r') as ranks_to_run_file: all_ranks = json.load(ranks_to_run_file) if ranks_to_run_range[0] >= len(all_ranks): return for ss in self.requeue_signals or []: signal.signal(signal.Signals[ss], requeue_handler) for rank_to_run in range(*ranks_to_run_range): if rank_to_run >= len(all_ranks): break rank = all_ranks[rank_to_run] self._run_for_rank(rank) else: self.launch_job() def launch_merge_stats(self): launch_slurm_job(self.get_launch_file_contents({**self.get_sbatch_args(), 'cpus-per-task': 1, 'mem-per-cpu': '1G', 'dependency': f'afterok:{self.job_id}'}, f"merge_stats {self.logging_dir.resolve_paths('stats')} -o {self.logging_dir.resolve_paths('stats.json')}")) @property def dependency(self) -> str: dependency = [] if self.depends_job_id: dependency.append(f"{('afterany' if self.run_on_dependency_fail else 'afterok')}:{self.depends_job_id}") if self.job_id and (not self.max_array_launch_parallel): dependency.append(f'afterany:{self.job_id}') return ','.join(dependency) def launch_job(self): assert not self.depends or isinstance(self.depends, SlurmPipelineExecutor), 'depends= must be a SlurmPipelineExecutor' if self.depends: if not self.depends.job_id: logger.info(f'Launching dependency job "{self.depends.job_name}"') self.depends.launch_job() if self.depends.job_id != -1: self.depends_job_id = self.depends.job_id self.depends = None ranks_to_run = self.get_incomplete_ranks() if len(ranks_to_run) == 0: logger.info(f'Skipping launch of {self.job_name} as all {self.tasks} tasks have already been completed.') self.job_id = -1 return executor = deepcopy(self) with self.logging_dir.open('executor.pik', 'wb') as executor_f: dill.dump(executor, executor_f, fmode=CONTENTS_FMODE) self.save_executor_as_json() with self.logging_dir.open('ranks_to_run.json', 'w') as ranks_to_run_file: json.dump(ranks_to_run, ranks_to_run_file) nb_jobs_to_launch = math.ceil(len(ranks_to_run) / self.tasks_per_job) max_array = min(nb_jobs_to_launch, self.max_array_size) if self.max_array_size != -1 else nb_jobs_to_launch srun_args_str = ' '.join([f'--{k}={v}' for (k, v) in self.srun_args.items()]) if self.srun_args else '' launch_file_contents = self.get_launch_file_contents(self.get_sbatch_args(max_array), f"srun {srun_args_str} -l launch_pickled_pipeline {self.logging_dir.resolve_paths('executor.pik')}") with self.logging_dir.open('launch_script.slurm', 'w') as launchscript_f: launchscript_f.write(launch_file_contents) logger.info(f'''Launching Slurm job {self.job_name} ({len(ranks_to_run)} tasks) with launch script "{self.logging_dir.resolve_paths('launch_script.slurm')}"''') launched_jobs = 0 while launched_jobs * max_array < nb_jobs_to_launch: if launched_jobs and self.max_array_launch_parallel and (self.stagger_max_array_jobs > 0): time.sleep(self.stagger_max_array_jobs) args = [f'--export=ALL,RUN_OFFSET={launched_jobs}'] if self.dependency: args.append(f'--dependency={self.dependency}') self.job_id = launch_slurm_job(launch_file_contents, *args) launched_jobs += 1 logger.info(f'Slurm job launched successfully with (last) id={self.job_id}.') self.launch_merge_stats() def get_sbatch_args(self, max_array: int=1) -> dict: os.makedirs(self.slurm_logs_folder, exist_ok=True) slurm_logfile = os.path.join(self.slurm_logs_folder, '%A_%a.out') sbatch_args = {'cpus-per-task': self.cpus_per_task, 'mem-per-cpu': f'{self.mem_per_cpu_gb}G', 'partition': self.partition, 'job-name': self.job_name, 'time': self.time, 'output': slurm_logfile, 'error': slurm_logfile, 'array': f"0-{max_array - 1}{(f'%{self.workers}' if self.workers != -1 else '')}", **({'mail-type': self.mail_type, 'mail-user': self.mail_user} if self.mail_user else {}), **self._sbatch_args} if self.requeue: sbatch_args['requeue'] = '' if self.qos: sbatch_args['qos'] = self.qos return sbatch_args def get_launch_file_contents(self, sbatch_args: dict, run_script: str) -> str: args = '\n'.join([f'#SBATCH --{k}={v}' if v else f'#SBATCH --{k}' for (k, v) in sbatch_args.items()]) env_command = self.env_command if self.env_command else f'conda init bash\n conda activate {self.condaenv}\n source ~/.bashrc' if self.condaenv else f'source {self.venv_path}' if self.venv_path else '' return '#!/bin/bash\n' + args + textwrap.dedent(f'\n echo "Starting data processing job {self.job_name}"\n {env_command}\n set -xe\n export PYTHONUNBUFFERED=TRUE\n {run_script}\n ') @property def world_size(self) -> int: return self.tasks def launch_slurm_job(launch_file_contents, *args): with tempfile.NamedTemporaryFile('w') as f: f.write(launch_file_contents) f.flush() return subprocess.check_output(['sbatch', *args, f.name]).decode('utf-8').split()[-1] # File: datatrove-main/src/datatrove/io.py import os.path from glob import has_magic from typing import IO, Callable, TypeAlias from fsspec import AbstractFileSystem from fsspec import open as fsspec_open from fsspec.callbacks import NoOpCallback, TqdmCallback from fsspec.core import get_fs_token_paths, strip_protocol, url_to_fs from fsspec.implementations.cached import CachingFileSystem from fsspec.implementations.dirfs import DirFileSystem from fsspec.implementations.local import LocalFileSystem from huggingface_hub import HfFileSystem, cached_assets_path from datatrove.utils._import_utils import check_required_dependencies from datatrove.utils.logging import logger class OutputFileManager: def __init__(self, fs, mode: str='wt', compression: str | None='infer'): self.fs = fs self.mode = mode self.compression = compression self._output_files = {} def get_file(self, filename): if filename not in self._output_files: self._output_files[filename] = self.fs.open(filename, mode=self.mode, compression=self.compression) return self._output_files[filename] def get_open_files(self): return self._output_files def pop(self, filename): file = self.get_file(filename) self._output_files.pop(filename) return file def write(self, filename, data): self.get_file(filename).write(data) def __enter__(self): return self def close(self): for file in self._output_files.values(): file.close() self._output_files.clear() def __exit__(self, exc_type, exc_val, exc_tb): self.close() class DataFolder(DirFileSystem): def __init__(self, path: str, fs: AbstractFileSystem | None=None, auto_mkdir: bool=True, **storage_options): super().__init__(path=path, fs=fs if fs else url_to_fs(path, **storage_options)[0]) self.auto_mkdir = auto_mkdir def list_files(self, subdirectory: str='', recursive: bool=True, glob_pattern: str | None=None, include_directories: bool=False) -> list[str]: if glob_pattern and (not has_magic(glob_pattern)): glob_pattern = f'*{glob_pattern}' extra_options = {} if isinstance(_get_true_fs(self.fs), HfFileSystem): extra_options['expand_info'] = False if include_directories and (not glob_pattern): extra_options['withdirs'] = True return sorted([f for (f, info) in (self.find(subdirectory, maxdepth=1 if not recursive else None, detail=True, **extra_options) if not glob_pattern else self.glob(self.fs.sep.join([subdirectory, glob_pattern]) if subdirectory else glob_pattern, maxdepth=1 if not recursive else None, detail=True, **extra_options)).items() if include_directories or info['type'] != 'directory']) def get_shard(self, rank: int, world_size: int, **kwargs) -> list[str]: return self.list_files(**kwargs)[rank::world_size] def resolve_paths(self, paths) -> list[str] | str: if isinstance(paths, str): if isinstance(self.fs, LocalFileSystem): return self.fs._strip_protocol(self._join(paths)) return self.fs.unstrip_protocol(self._join(paths)) return list(map(self.resolve_paths, paths)) def get_output_file_manager(self, **kwargs) -> OutputFileManager: return OutputFileManager(self, **kwargs) def open_files(self, paths, mode='rb', **kwargs): return [self.open(path, mode=mode, **kwargs) for path in paths] def open(self, path, mode='rb', *args, **kwargs): if self.auto_mkdir and ('w' in mode or 'a' in mode): self.fs.makedirs(self.fs._parent(self._join(path)), exist_ok=True) return super().open(path, *args, mode=mode, **kwargs) def is_local(self): return isinstance(self.fs, LocalFileSystem) def get_datafolder(data: DataFolder | str | tuple[str, dict] | tuple[str, AbstractFileSystem]) -> DataFolder: if isinstance(data, DataFolder): return data if isinstance(data, str): return DataFolder(data) if isinstance(data, tuple) and isinstance(data[0], str) and isinstance(data[1], dict): return DataFolder(data[0], **data[1]) if isinstance(data, tuple) and isinstance(data[0], str) and isinstance(data[1], AbstractFileSystem): return DataFolder(data[0], fs=data[1]) raise ValueError('You must pass a DataFolder instance, a str path, a (str path, fs_init_kwargs) or (str path, fs object)') def open_file(file: IO | str, mode='rt', **kwargs): if isinstance(file, str): return fsspec_open(file, mode, **kwargs) return file def file_exists(path: str): (fs, a, fpath) = get_fs_token_paths(path) return fs.exists(fpath[0]) def download_file(remote_path: str, local_path: str, progress: bool=True): (fs, _, paths) = get_fs_token_paths(remote_path) fs.get_file(paths[0], local_path, callback=TqdmCallback(tqdm_kwargs={'desc': f'↓ Downloading {os.path.basename(remote_path)}', 'unit': 'B', 'unit_scale': True, 'unit_divisor': 1024, 'miniters': 1}) if progress else NoOpCallback()) def safely_create_file(file_to_lock: str, do_processing: Callable): check_required_dependencies('io', ['fasteners']) from fasteners import InterProcessLock completed_file = f'{file_to_lock}.completed' if os.path.exists(completed_file): return with InterProcessLock(f'{file_to_lock}.lock'): if not os.path.exists(completed_file): do_processing() open(completed_file, 'a').close() def cached_asset_path_or_download(remote_path: str, progress: bool=True, namespace: str='default', subfolder: str='default', desc: str='file'): download_dir = cached_assets_path(library_name='datatrove', namespace=namespace, subfolder=subfolder) local_path = os.path.join(download_dir, strip_protocol(remote_path).replace('/', '_')) def do_download_file(): logger.info(f'⬇️ Downloading {desc} from "{remote_path}"...') download_file(remote_path, local_path, progress) logger.info(f'⬇️ Downloaded {desc} to "{local_path}".') safely_create_file(local_path, do_download_file) return local_path DataFolderLike: TypeAlias = str | tuple[str, dict] | DataFolder DataFileLike: TypeAlias = str | tuple[str, dict] def get_shard_from_paths_file(paths_file: DataFileLike, rank: int, world_size): kwargs = {} if isinstance(paths_file, tuple): (paths_file, kwargs) = paths_file with open_file(paths_file, mode='rt', **kwargs) as f: for (pathi, path) in enumerate(f): if (pathi - rank) % world_size == 0: yield path.strip() def _get_true_fs(fs: AbstractFileSystem): if isinstance(fs, CachingFileSystem): return fs.fs return fs # File: datatrove-main/src/datatrove/pipeline/base.py from abc import ABC, abstractmethod from itertools import chain from datatrove.data import Document, DocumentsPipeline from datatrove.utils._import_utils import check_required_dependencies from datatrove.utils.stats import Stats class PipelineStep(ABC): name: str = None type: str = None def __new__(cls, *args, **kwargs): required_dependencies = chain.from_iterable((getattr(t, '_requires_dependencies', []) for t in cls.mro())) if required_dependencies: check_required_dependencies(cls.__name__, required_dependencies) return super().__new__(cls) def __init__(self): super().__init__() self.stats = Stats(str(self)) def stat_update(self, *labels, value: int=1, unit: str=None): for label in labels: self.stats[label].update(value, unit) def update_doc_stats(self, document: Document): self.stat_update('doc_len', value=len(document.text), unit='doc') if (token_count := document.metadata.get('token_count', None)): self.stat_update('doc_len_tokens', value=token_count, unit='doc') def track_time(self, unit: str=None): if unit: self.stats.time_stats.unit = unit return self.stats.time_stats def __repr__(self): return f'{self.type}: {self.name}' @abstractmethod def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1) -> DocumentsPipeline: if data: yield from data def __call__(self, data: DocumentsPipeline=None, rank: int=0, world_size: int=1) -> DocumentsPipeline: return self.run(data, rank, world_size) # File: datatrove-main/src/datatrove/pipeline/decont/n_grams.py """""" import os from collections import defaultdict from concurrent.futures import ThreadPoolExecutor from dataclasses import dataclass, field from typing import Tuple import numpy as np from datatrove.data import Document, DocumentsPipeline from datatrove.io import DataFolderLike, file_exists, get_datafolder, open_file from datatrove.pipeline.base import PipelineStep from datatrove.pipeline.filters.base_filter import BaseFilter from datatrove.pipeline.writers.disk_base import DiskWriter from datatrove.utils.binaryio import read_np_from_file from datatrove.utils.hashing import HashConfig, create_hash_func from datatrove.utils.logging import logger from datatrove.utils.text import TextNormConfig, ngrams, simplify_text from datatrove.utils.typeshelper import Languages from datatrove.utils.word_tokenizers import load_word_tokenizer @dataclass class NGramsDecontConfig: n_grams: int = 12 find_query_ngrams: bool = False find_overlap_ngrams: bool = True norm_config: TextNormConfig = field(default_factory=TextNormConfig) hash_config: HashConfig = field(default_factory=HashConfig) class NGramsDecontIndexer(PipelineStep): type = '🦠 - DECONT' name = '💥 N-grams build index' _requires_dependencies = ['lighteval'] def __init__(self, output_folder: DataFolderLike, lighteval_tasks: str | list[str] | None=None, custom_lighteval_tasks: str | None=None, config: NGramsDecontConfig=None, language: str=Languages.english): super().__init__() self.output_folder = get_datafolder(output_folder) if isinstance(lighteval_tasks, str): if file_exists(lighteval_tasks): with open_file(lighteval_tasks, 'rt') as f: self.lighteval_tasks = f.read().strip().splitlines() else: self.lighteval_tasks = [lighteval_tasks] else: self.lighteval_tasks = lighteval_tasks self.custom_lighteval_tasks = custom_lighteval_tasks self.config = config or NGramsDecontConfig() self.tokenizer = load_word_tokenizer(language) self.hash_func = create_hash_func(self.config.hash_config) def compute_hashes(self, label: str, query: str | None=None) -> list[int]: label_tokens = self.tokenizer.word_tokenize(simplify_text(label, self.config.norm_config)) ngrams_to_compute = list(ngrams(label_tokens, self.config.n_grams)) if query is not None: query_tokens = self.tokenizer.word_tokenize(simplify_text(query, self.config.norm_config)) if self.config.find_query_ngrams: ngrams_to_compute.extend(ngrams(query_tokens, self.config.n_grams)) if self.config.find_overlap_ngrams: '' ngrams_to_compute.extend([query_tokens[-self.config.n_grams + 1 + i:] + label_tokens[:i + 1] for i in range(self.config.n_grams - 1) if len(query_tokens) >= self.config.n_grams - 1 - i and len(label_tokens) >= i + 1]) return list(map(self.hash_func, map(' '.join, ngrams_to_compute))) def run(self, data: DocumentsPipeline=None, rank: int=0, world_size: int=1): if world_size != 1: raise ValueError('Decontamination index building requires a single worker.') hashes = defaultdict(set) if data: for doc in data: if not self.config.find_query_ngrams and 'query' not in doc.metadata: raise ValueError("only_label_ngrams is False but could not find 'query' field in documents metadata") hashes[doc.metadata.get('task', 'input')].update(self.compute_hashes(doc.text, doc.metadata.get('query', None))) from lighteval.tasks.lighteval_task import LightevalTask from lighteval.tasks.registry import Registry task_dict = Registry(cache_dir=os.getenv('HF_HOME')).get_task_dict(self.lighteval_tasks, custom_tasks=self.custom_lighteval_tasks) LightevalTask.load_datasets(task_dict.values()) for (task_name, task) in task_dict.items(): for eval_doc in task.eval_docs(): try: golds = eval_doc.get_golds() query = eval_doc.query except Exception as e: logger.warning(f'Error while fetching doc data: {e}') continue for gold in golds: hashes[task_name].update(self.compute_hashes(gold, query)) for (task_name, task_hashes) in hashes.items(): hashes_array = np.array(list(task_hashes), dtype=self.config.hash_config.np_descr) logger.info(f'Saving {len(task_hashes)} hashes for {task_name}') with self.output_folder.open(f"{task_name.replace(' ', '_')}.index.hashes", mode='wb') as f: if self.output_folder.is_local(): hashes_array.tofile(f) else: f.write(hashes_array.tobytes()) class NGramsDecontFilter(BaseFilter): type = '🦠 - DECONT' name = '💥 N-grams decontaminate' def __init__(self, index_folder: DataFolderLike, config: NGramsDecontConfig=None, exclusion_writer: DiskWriter=None, language: str=Languages.english): super().__init__() self.index_folder = get_datafolder(index_folder) self.config = config or NGramsDecontConfig() self.exclusion_writer = exclusion_writer self.language = language self._index_hashes = None self.hash_func = create_hash_func(self.config.hash_config) self.tokenizer = load_word_tokenizer(language) def load_index_hashes(self): def load_index_from_file(file): with self.index_folder.open(file, mode='rb') as f: return (file, read_np_from_file(f, np.dtype(self.config.hash_config.np_descr), self.index_folder.is_local()).tolist()) with ThreadPoolExecutor() as pool: hashes = pool.map(load_index_from_file, self.index_folder.list_files()) self._index_hashes = {} for (filename, hashlist) in hashes: taskname = filename.removesuffix('.index.hashes') logger.info(f'Loading {len(hashlist)} hashes for {taskname}') for hash in hashlist: self._index_hashes[hash] = taskname def filter(self, doc: Document) -> bool | Tuple[bool, str]: if self._index_hashes is None: self.load_index_hashes() text_tokens = self.tokenizer.word_tokenize(simplify_text(doc.text, self.config.norm_config)) ngrams_to_compute = list(ngrams(text_tokens, self.config.n_grams)) for n_gram in map(' '.join, ngrams_to_compute): task = self._index_hashes.get(self.hash_func(n_gram), None) if task is not None: doc.metadata['contaminated_ngram'] = n_gram doc.metadata['contaminated_task'] = task self.stat_update(f'contaminated_{task}') if ':' in task: self.stat_update(f"contaminated_tg_{task[:task.index(':')]}") return (False, 'contaminated') return True # File: datatrove-main/src/datatrove/pipeline/dedup/__init__.py from .bloom_filter import SingleBloomFilter from .exact_substrings import ESDatasetToSequence, ESMergeSequences, ESRangeRemover from .minhash import MinhashBuildIndex, MinhashConfig, MinhashDedupBuckets, MinhashDedupCluster, MinhashDedupFilter, MinhashDedupSignature from .sentence_dedup import SentDedupConfig, SentenceDedupFilter, SentenceDedupSignature, SentenceFindDedups from .url_dedup import UrlDedupConfig, UrlDedupFilter, UrlDedupSignature, UrlFindDedups # File: datatrove-main/src/datatrove/pipeline/dedup/bloom_filter.py import contextlib import math from dataclasses import dataclass, field import numpy as np from datatrove.data import Document, DocumentsPipeline from datatrove.io import DataFolderLike, get_datafolder from datatrove.pipeline.base import PipelineStep from datatrove.pipeline.writers.disk_base import DiskWriter from datatrove.utils.hashing import HashConfig, create_hash_func from datatrove.utils.logging import logger from datatrove.utils.text import TextNormConfig, ngrams, simplify_text from datatrove.utils.typeshelper import Languages, StatHints from datatrove.utils.word_tokenizers import load_word_tokenizer _mersenne_prime = np.uint64((1 << 61) - 1) MAX_HASH = 1 << 32 - 1 @dataclass class BloomFilterConfig: m_bytes: int k: int = None expected_elements: int = None duplicate_threshold: float = 0.8 n_grams: int = 13 seed: int = 0 norm_config: TextNormConfig = field(default_factory=TextNormConfig) hash_config: HashConfig = field(default_factory=lambda : HashConfig(precision=32)) @property def m(self): return self.m_bytes * 8 def __post_init__(self): if self.k is None: self.k = get_optimal_k(self.m, expected_elements=self.expected_elements) def get_optimal_k(size_in_bytes: int, expected_elements: int) -> int: assert expected_elements, f'if expected_elements={expected_elements!r} then k must be given' m = size_in_bytes * 8 k = m / expected_elements * np.log(2) return math.ceil(k) def get_false_positive_prob(size_in_bytes: int, n: int, k: int) -> float: m = size_in_bytes * 8 return (1.0 - (1.0 - 1.0 / m) ** (k * n)) ** k class SingleBloomFilter(PipelineStep): type = '🫂 - DEDUPS' name = '\U0001fab7 Bloom-filter' def __init__(self, output_folder: DataFolderLike, config: BloomFilterConfig, save_bloom_filter: bool=False, exclusion_writer: DiskWriter=None, language: str=Languages.english): super().__init__() self.output_folder = get_datafolder(output_folder) self.tokenizer = load_word_tokenizer(language) self.config = config self.bit_vector = bytearray([0] * self.config.m_bytes) self.save_bloom_filter = save_bloom_filter self.exclusion_writer = exclusion_writer assert self.config.hash_config.precision == 32, 'Bloom filter only supports 32-bit hashes' self.hash_fc = create_hash_func(self.config.hash_config) assert self.config.m < MAX_HASH self.total_shingles = 0 self._parameters = None assert self.config.m_bytes < MAX_HASH, f'MAX_HASH={MAX_HASH!r} is smaller than self.config.m_bytes={self.config.m_bytes!r}' if self.config.expected_elements: fp = get_false_positive_prob(self.config.m_bytes, n=self.config.expected_elements, k=self.config.k) if fp > 0.05: logger.warning(f'False probability = {fp:.3}') else: logger.info(f'False probability = {fp:.3}') self.language = language @property def parameters(self): if self._parameters is None: gen = np.random.RandomState(self.config.seed) self._parameters = (gen.randint(1, _mersenne_prime, dtype=np.uint64, size=(1, self.config.k)), gen.randint(0, _mersenne_prime, dtype=np.uint64, size=(1, self.config.k))) return self._parameters def get_shingles(self, text: str) -> np.ndarray: return np.fromiter([self.hash_fc(' '.join(x)) for x in ngrams(self.tokenizer.word_tokenize(simplify_text(text, self.config.norm_config)), self.config.n_grams)], dtype=np.uint64).reshape((-1, 1)) def get_indexes(self, shingles: np.ndarray) -> list[list[int]]: (a, b) = self.parameters phv = np.bitwise_and((shingles * a + b) % _mersenne_prime, self.config.m_bytes) return phv.tolist() def update_bf(self, indexes: list[int]): for index in indexes: (byte_index, bit_index) = divmod(index, 8) mask = 1 << bit_index self.bit_vector[byte_index] |= mask def query(self, indexes: list[int]) -> bool: for idx in indexes: (byte_index, bit_index) = divmod(idx, 8) mask = 1 << bit_index if self.bit_vector[byte_index] & mask == 0: return False return True def step(self, doc: Document) -> bool: shingles = self.get_shingles(doc.text) self.total_shingles += shingles.size if shingles.size == 0: return True shingle_indexes = self.get_indexes(shingles) duplicate_shingles = 0 indexes_to_update = [] for indexes in shingle_indexes: if self.query(indexes): duplicate_shingles += 1 else: indexes_to_update.extend(indexes) self.update_bf(indexes_to_update) if duplicate_shingles / len(shingles) > self.config.duplicate_threshold: self.stat_update(StatHints.dropped) return False return True def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1): with self.exclusion_writer if self.exclusion_writer else contextlib.nullcontext() as writer: for (doc_idx, doc) in enumerate(data): with self.track_time(): self.stat_update(StatHints.total) if not self.step(doc): self.stat_update(StatHints.dropped) if self.exclusion_writer: writer.write(doc, rank) continue self.stat_update(StatHints.forwarded) yield doc if self.save_bloom_filter: with self.output_folder.open('bloom_filter.bloom', mode='wb') as f: f.write(self.bit_vector) logger.info(f'self.total_shingles={self.total_shingles!r}') logger.info(f'False probability = {get_false_positive_prob(self.config.m_bytes, n=self.total_shingles, k=self.config.k):.3}') logger.info(f'Optimal K given total shingles = {get_optimal_k(self.config.m_bytes, self.total_shingles)}') # File: datatrove-main/src/datatrove/pipeline/dedup/exact_substrings.py """""" import struct from typing import BinaryIO, Generator import numpy as np from datatrove.io import DataFolderLike, get_datafolder from datatrove.pipeline.base import DocumentsPipeline, PipelineStep from datatrove.utils.logging import logger from ...utils.tokenization import PipelineStepWithTokenizer from ...utils.typeshelper import ExtensionHelperES as EH from ...utils.typeshelper import Languages from ...utils.word_tokenizers import load_word_tokenizer SEPARATOR_BYTES = 12 def prepare_doc(tokenizer, doc: str, rank: int, doc_id: int): tokens = tokenizer.encode(doc).ids tokens = np.fromiter(tokens, dtype=np.uint16, count=len(tokens)) b_doc = b'\xff\xff' + struct.pack('<I', doc_id) + b'\xff\xff' + struct.pack('<I', rank) + tokens.tobytes() return b_doc class ESDatasetToSequence(PipelineStepWithTokenizer): type = '🫂 - DEDUP' name = '🪞 - exact-substrings stage 1' def __init__(self, output_folder: DataFolderLike, tokenizer_name_or_path: str='gpt2'): super().__init__() self.output_folder = get_datafolder(output_folder) self.tokenizer_name_or_path = tokenizer_name_or_path def save_sizes(self, doc_lens: list[int], rank: int): with self.output_folder.open(f'{rank:05d}{EH.stage_1_sequence_size}', mode='wb') as f_lens: f_lens.write(struct.pack('Q' * len(doc_lens), *doc_lens)) def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1): doc_lens = [] with self.output_folder.open(f'{rank:05d}{EH.stage_1_sequence}', mode='wb') as f_sequence: i = -1 for (i, doc) in enumerate(data): with self.stats.time_stats: b_doc = prepare_doc(tokenizer=self.tokenizer, doc=doc.text, rank=rank, doc_id=i) doc_lens.append(len(b_doc)) f_sequence.write(b_doc) assert i < 2 ** 32, 'doc ID overflow' assert i + 1 == len(doc_lens), f'i={i!r} but len(doc_lens)={len(doc_lens)!r}' self.save_sizes(doc_lens, rank) class ESMergeSequences(PipelineStep): type = '🫂 - DEDUP' name = '🪞 - exact-substrings stage 2' def __init__(self, data_folder: DataFolderLike, tasks_stage_1: int, bytes_per_batch: int=int(500000000.0)): super().__init__() self.data_folder = get_datafolder(data_folder) self.tasks_stage_1 = tasks_stage_1 self.bytes_per_batch = bytes_per_batch def run(self, data: DocumentsPipeline=None, rank: int=0, world_size: int=1): bytes_per_sequence = [0] with self.stats.time_stats: assert world_size == 1, f"world_size={world_size!r} can't be greater than 1!" all_files: list[str] = self.data_folder.list_files(glob_pattern=EH.stage_1_sequence) assert len(all_files) == self.tasks_stage_1 with self.data_folder.open(f'dataset{EH.stage_2_big_sequence}', mode='wb') as f_sequence: for file in all_files: len_sequence = 0 with self.data_folder.open(file, 'rb') as f: while True: sequence = f.read(self.bytes_per_batch) f_sequence.write(sequence) len_sequence += len(sequence) if len(sequence) != self.bytes_per_batch: break bytes_per_sequence.append(bytes_per_sequence[-1] + len_sequence) with self.data_folder.open(f'bytes_offsets{EH.stage_2_bytes_offset}', mode='wb') as f_bytes: f_bytes.write(np.array([bytes_per_sequence], np.uint32).tobytes()) def read_bytes(x): return np.frombuffer(x[SEPARATOR_BYTES:], dtype=np.uint16).tolist() def sequence_reader(file: BinaryIO, size_file: BinaryIO) -> Generator[list, None, None]: with size_file as f_size: with file as f: while True: n_bytes = f_size.read(struct.calcsize('<Q')) if len(n_bytes) == 0: break assert len(n_bytes) == 8 n_bytes = struct.unpack('<Q', n_bytes)[0] yield f.read(n_bytes) class ESRangeRemover(PipelineStepWithTokenizer): type = '🫂 - DEDUP' name = '🪞 - exact-substrings stage 3' def __init__(self, sequence_folder: DataFolderLike, tokenizer_name_or_path: str='gpt2', min_doc_words: int=50, language: str=Languages.english): super().__init__() self.sequence_folder = get_datafolder(sequence_folder) self.tokenizer_name_or_path = tokenizer_name_or_path self.min_doc_words = min_doc_words self.sequence_bytes_offset = None self.dup_ranges = None self.rank = None self.exhausted_ranges = False self.bytes_counter = 0 self.range_idx = 0 self.language = language self.word_tokenizer = load_word_tokenizer(language) def reset(self): self.bytes_counter = 0 self.range_idx = 0 self.exhausted_ranges = False self.sequence_bytes_offset = None self.dup_ranges = None self.rank = None def get_sequence_bytes_offset(self): offset_array_file: str = self.sequence_folder.list_files(glob_pattern=EH.stage_2_bytes_offset)[0] with self.sequence_folder.open(offset_array_file, 'rb') as f: offset_array = f.read() self.sequence_bytes_offset = np.frombuffer(offset_array, dtype=np.uint32) logger.info(f'self.rank={self.rank!r}, -> self.sequence_bytes_offset[self.rank]={self.sequence_bytes_offset[self.rank]!r}') def get_bytearange(self, bytes_range_file: BinaryIO): with bytes_range_file as f: dup_ranges = f.read() dup_ranges = dup_ranges.split('\n') i = 0 for (i, x) in enumerate(dup_ranges): if x == 'out': break dup_ranges = dup_ranges[i + 1:-1] rank_dup_ranges = [] for br in dup_ranges: (a, b) = br.split(' ') (a, b) = (int(a), int(b)) if b > self.sequence_bytes_offset[self.rank + 1] + SEPARATOR_BYTES: break if b > self.sequence_bytes_offset[self.rank] + SEPARATOR_BYTES: (a, b) = (a - self.sequence_bytes_offset[self.rank], b - self.sequence_bytes_offset[self.rank]) rank_dup_ranges.append((a, b)) self.dup_ranges = rank_dup_ranges def get_all_files(self, rank: int, world_size: int): self.get_sequence_bytes_offset() sequence_file = self.sequence_folder.get_shard(rank, world_size, glob_pattern=EH.stage_1_sequence) docs_sizes_file = self.sequence_folder.get_shard(rank, world_size, glob_pattern=EH.stage_1_sequence_size) byte_range_file = self.sequence_folder.list_files(glob_pattern=EH.stage_3_bytes_ranges) assert all([len(sequence_file) == 1, len(docs_sizes_file) == 1, len(byte_range_file) == 1]), f'Need to run with n_tasks = n_files. len(sequence_file)={len(sequence_file)!r}, len(sequence_file)={len(sequence_file)!r}, len(byte_range_file)={len(byte_range_file)!r}' (sequence_file, docs_sizes_file, byte_range_file) = (sequence_file[0], docs_sizes_file[0], byte_range_file[0]) self.get_bytearange(self.sequence_folder.open(byte_range_file, 'rt')) return (sequence_file, docs_sizes_file) def normalize_range(self, a, b, bytes_len): (a, b) = (a - self.bytes_counter, b - self.bytes_counter) a = max(SEPARATOR_BYTES, a) b = min(bytes_len, b) assert SEPARATOR_BYTES <= a < b <= bytes_len, f'SEPARATOR_BYTES={SEPARATOR_BYTES!r} < a={a!r} < b={b!r} < bytes_len={bytes_len!r} is NOT satisfied' if b % 2 == 1: b -= 1 if a % 2 == 1: a += 1 b = max(a, b) return (a, b) def get_duplicate_range(self, bytes_len: int): ranges = [] upper_limit = self.bytes_counter + bytes_len + SEPARATOR_BYTES if self.exhausted_ranges: return ranges while True: (a, b) = (self.dup_ranges[self.range_idx][0], self.dup_ranges[self.range_idx][1]) left = a < self.bytes_counter and self.bytes_counter + SEPARATOR_BYTES < b <= upper_limit centre = self.bytes_counter <= a < b <= upper_limit right = self.bytes_counter <= a < upper_limit - SEPARATOR_BYTES and upper_limit < b outside = a < self.bytes_counter < upper_limit < b if not any([left, centre, right, outside]): break assert sum([left, centre, right, outside]) == 1, f'left={left!r}, centre={centre!r}, right={right!r}, outside={outside!r}' if left: self.range_idx += 1 a = self.bytes_counter if centre: self.range_idx += 1 if right: ranges.append(self.normalize_range(a, upper_limit, bytes_len)) break if outside: ranges.append(self.normalize_range(self.bytes_counter, upper_limit, bytes_len)) break ranges.append(self.normalize_range(a, b, bytes_len)) if self.range_idx == len(self.dup_ranges): self.exhausted_ranges = True break return ranges def remove_duplicate(self, doc, bytes_content): n_bytes = len(bytes_content) duplicates_ranges = self.get_duplicate_range(n_bytes) duplicates = [] for (byte_a, byte_b) in duplicates_ranges: dup_sentence = self.tokenizer.decode(np.frombuffer(bytes_content[byte_a:byte_b], dtype=np.uint16).tolist()) duplicates.append(dup_sentence) if duplicates: text = doc.text for d in duplicates: text = text.replace(d, '') doc.text = text self.bytes_counter += len(bytes_content) if len(self.word_tokenizer.word_tokenize(doc.text)) < self.min_doc_words: return False return True def run(self, data: DocumentsPipeline=None, rank: int=0, world_size: int=1) -> DocumentsPipeline: self.reset() self.rank = rank (sequence_file, size_file) = self.get_all_files(rank=self.rank, world_size=world_size) if not self.dup_ranges: return for (doc, doc_content) in zip(data, sequence_reader(self.sequence_folder.open(sequence_file, 'rb'), self.sequence_folder.open(size_file, 'rb'))): with self.stats.time_stats: assert doc.text == self.tokenizer.decode(read_bytes(doc_content), skip_special_tokens=False), f'{doc.text}\n\n{self.tokenizer.decode(read_bytes(doc_content))}' to_yield = self.remove_duplicate(doc, doc_content) if to_yield: self.update_doc_stats(doc) yield doc assert self.bytes_counter == self.sequence_bytes_offset[rank + 1] - self.sequence_bytes_offset[rank], f'got self.bytes_counter={self.bytes_counter!r}, expected = {self.sequence_bytes_offset[rank + 1] - self.sequence_bytes_offset[rank]}' assert self.exhausted_ranges, 'One or more duplicate ranges have not been used' # File: datatrove-main/src/datatrove/pipeline/dedup/minhash.py import contextlib import heapq import os import re import struct from dataclasses import dataclass, field from pathlib import Path from typing import Generator import numpy as np from fsspec.spec import AbstractBufferedFile from datatrove.data import DocumentsPipeline from datatrove.io import DataFolderLike, get_datafolder from datatrove.pipeline.base import PipelineStep from datatrove.pipeline.writers.disk_base import DiskWriter from datatrove.utils.binaryio import read_tuples_from_file, seek_to_start from datatrove.utils.hashing import HashConfig, create_hash_func from datatrove.utils.logging import logger from datatrove.utils.text import TextNormConfig, ngrams, simplify_text from datatrove.utils.typeshelper import Languages, StatHints from datatrove.utils.word_tokenizers import load_word_tokenizer _mersenne_prime = np.uint64((1 << 61) - 1) '' SENTINEL = (1 << 32) - 1 @dataclass class MinhashConfig: n_grams: int = 5 num_buckets: int = 14 hashes_per_bucket: int = 8 seed: int = 1 norm_config: TextNormConfig = field(default_factory=TextNormConfig) hash_config: HashConfig = field(default_factory=HashConfig) def __str__(self): return f'{self.n_grams}ng_{self.num_buckets}bs_{self.hashes_per_bucket}hs_{self.hash_config}' @dataclass(order=True) class HashSig: sig: tuple[int] file_id: int file_stem: str doc_id: int reader_id: int def is_from_index(self): return self.reader_id != self.file_id def read_sigs(file: AbstractBufferedFile, reader_id: int, config: MinhashConfig, index_file: bool=False, min_hash: int=0, max_hash: int=_mersenne_prime, ensure_order: bool=True, lines_to_buffer: int=5) -> Generator: line_format = f"{config.hashes_per_bucket}{config.hash_config.struct_format}{('I' if not index_file else '')}" with file as f: if f.size == 0: return seek_to_start(f, min_hash, line_format, config.hash_config.struct_format) last = None file_stem = Path(file.path).name.removesuffix('.minhash.sig') for data in read_tuples_from_file(f, line_format, lines_to_buffer=lines_to_buffer): sigdata = data if index_file else data[:-1] assert sigdata[0] >= min_hash and (ensure_order is False or last is None or sigdata >= last), f'Hash order error. f.tell()={f.tell()!r}, min_hash={min_hash!r}, sigdata={sigdata!r}, last={last!r}' if sigdata[0] >= max_hash: break last = sigdata yield (HashSig(sig=sigdata, doc_id=-1, file_id=-1, reader_id=reader_id, file_stem=file_stem) if index_file else HashSig(sig=sigdata, doc_id=data[-1], file_id=reader_id, reader_id=reader_id, file_stem=file_stem)) class MinhashDedupSignature(PipelineStep): type = '🫂 - DEDUP' name = '🎯 MinHash stage 1' def __init__(self, output_folder: DataFolderLike, config: MinhashConfig=None, language: str=Languages.english): super().__init__() self.output_folder = get_datafolder(output_folder) self.config = config or MinhashConfig() self.num_hashes = self.config.num_buckets * self.config.hashes_per_bucket self._parameters = None self._hash_func = create_hash_func(self.config.hash_config) self.language = language self.word_tokenizer = load_word_tokenizer(language) @property def parameters(self): if self._parameters is None: gen = np.random.RandomState(self.config.seed) self._parameters = (gen.randint(1, _mersenne_prime, dtype=np.uint64, size=(1, self.num_hashes)), gen.randint(0, _mersenne_prime, dtype=np.uint64, size=(1, self.num_hashes))) return self._parameters def get_signature(self, shingles: np.ndarray) -> list[list[int]]: (a, b) = self.parameters phv = (shingles * a + b) % _mersenne_prime if self.config.hash_config.precision == 32: phv = np.bitwise_and(phv, self.config.hash_config.max) return [x.tolist() for x in np.split(np.min(phv, axis=0).astype(self.config.hash_config.np_dtype), self.config.num_buckets)] def get_shingles(self, text: str) -> np.ndarray: return np.fromiter([self._hash_func(' '.join(x)) for x in ngrams(self.word_tokenizer.word_tokenize(simplify_text(text, self.config.norm_config)), self.config.n_grams)], dtype=np.uint64).reshape((-1, 1)) def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1): buckets = [self.output_folder.open(f'bucket_{bi:03d}/{rank:05d}.minhash.sig', mode='wb') for bi in range(self.config.num_buckets)] with self.track_time(): for (doc_idx, doc) in enumerate(data): self.stat_update(StatHints.total) shingles = self.get_shingles(doc.text) if shingles.size != 0: sig = self.get_signature(shingles) for (bi, (bucket, bucket_sig)) in enumerate(zip(buckets, sig)): bucket.write(struct.pack(f'<{self.config.hashes_per_bucket}{self.config.hash_config.struct_format}I', *bucket_sig, doc_idx)) for file in buckets: file.close() logger.info('Sorting buckets...') for bi in range(len(buckets)): sigs = sorted(read_sigs(self.output_folder.open(f'bucket_{bi:03d}/{rank:05d}.minhash.sig', mode='rb'), -1, self.config, ensure_order=False, lines_to_buffer=-1)) with self.output_folder.open(f'bucket_{bi:03d}/{rank:05d}.minhash.sig', mode='wb') as fo: for sig in sigs: fo.write(struct.pack(f'<{self.config.hashes_per_bucket}{self.config.hash_config.struct_format}I', *sig.sig, sig.doc_id)) class MinhashDedupBuckets(PipelineStep): type = '🫂 - DEDUP' name = '🎯 MinHash stage 2' def __init__(self, input_folder: DataFolderLike, output_folder: DataFolderLike, index_folder: DataFolderLike=None, config: MinhashConfig=None, only_dedup_in_index: bool=True, create_index_name: str=None, lines_to_buffer: int=5): super().__init__() self.input_folder = get_datafolder(input_folder) self.output_folder = get_datafolder(output_folder) self.index_folder = get_datafolder(index_folder) if index_folder else None self.config = config or MinhashConfig() self.only_dedup_in_index = only_dedup_in_index self.create_index_name = create_index_name self.lines_to_buffer = lines_to_buffer def get_worker_hash_range(self, sig_files, rank, world_size): workers_per_bucket = world_size // self.config.num_buckets (bucket, bucket_worker) = divmod(rank, workers_per_bucket) (hash_min, hash_max) = (0, _mersenne_prime if self.config.hash_config.precision == 64 else self.config.hash_config.max) if workers_per_bucket > 1 and len(sig_files): with self.input_folder.open(sig_files[0], mode='rb') as f: line_size = struct.calcsize(f'{self.config.hashes_per_bucket}{self.config.hash_config.struct_format}I') (L, rem) = divmod(f.size, line_size) assert rem == 0, 'file size not divisible by line size' assert L >= workers_per_bucket, f'tried to use workers_per_bucket={workers_per_bucket!r} but there are only {L} lines' if bucket_worker > 0: f.seek(line_size * (L // workers_per_bucket) * bucket_worker, os.SEEK_SET) hash_min = struct.unpack(self.config.hash_config.struct_format, f.read(struct.calcsize(self.config.hash_config.struct_format)))[0] if bucket_worker + 1 < workers_per_bucket: f.seek(line_size * (L // workers_per_bucket) * (bucket_worker + 1), os.SEEK_SET) hash_max = struct.unpack(self.config.hash_config.struct_format, f.read(struct.calcsize(self.config.hash_config.struct_format)))[0] return (hash_min, hash_max) def run(self, data: DocumentsPipeline=None, rank: int=0, world_size: int=1): assert data is None, 'You should not use an input block before MinhashDedupBuckets' assert world_size % self.config.num_buckets == 0, 'Number of tasks must be divisible by num_buckets' workers_per_bucket = world_size // self.config.num_buckets (bucket, bucket_worker) = divmod(rank, workers_per_bucket) with self.track_time(): sig_files = self.input_folder.list_files(subdirectory=f'bucket_{bucket:03d}') (hash_min, hash_max) = self.get_worker_hash_range(sig_files, rank, world_size) logger.info(f'Running worker {bucket_worker + 1}/{workers_per_bucket} on bucket {bucket:03d}. Hash range: {[hash_min, hash_max]}') sig_readers = [read_sigs(file, file_i, self.config, min_hash=hash_min, max_hash=hash_max, lines_to_buffer=self.lines_to_buffer) for (file_i, file) in enumerate(self.input_folder.open_files(sig_files, mode='rb'))] own_index_regex = re.compile(f'bucket_{bucket:03d}/{self.create_index_name}_\\d{{2}}.minhash.index') index_files = [filename for filename in self.index_folder.list_files(subdirectory=f'bucket_{bucket:03d}') if not self.create_index_name or not own_index_regex.fullmatch(filename)] if self.index_folder else None if index_files: logger.info(f"Found {len(index_files)} index file(s): {', '.join(index_files)}") sig_readers.extend([read_sigs(file, len(sig_readers) + file_i, self.config, index_file=True, min_hash=hash_min, max_hash=hash_max, lines_to_buffer=self.lines_to_buffer) for (file_i, file) in enumerate(self.index_folder.open_files(index_files, mode='rb'))]) pq = [x for x in [next(sig_reader, None) for sig_reader in sig_readers] if x is not None] heapq.heapify(pq) logger.info('Finished initializing signatures priority queue.') out_index = None if self.index_folder and self.create_index_name: out_index = self.index_folder.open(f'bucket_{bucket:03d}/{self.create_index_name}_{bucket_worker:02d}.minhash.index', mode='wb') with self.output_folder.open(f'{bucket:05d}_{bucket_worker:02d}.dups', mode='wb') as out_f: last: HashSig | None = None while pq: v: HashSig = heapq.heappop(pq) assert last is None or v >= last, f'Sig queue sort error. v={v!r} < last={last!r}' if not v.is_from_index(): if last and last.sig == v.sig: if last.is_from_index(): out_f.write(struct.pack('<4I', SENTINEL, SENTINEL, int(v.file_stem), v.doc_id)) self.stat_update('index_match', 'total_matches') elif not index_files or not self.only_dedup_in_index: out_f.write(struct.pack('<4I', int(last.file_stem), last.doc_id, int(v.file_stem), v.doc_id)) self.stat_update('total_matches') elif out_index: out_index.write(struct.pack(f'<%d{self.config.hash_config.struct_format}' % self.config.hashes_per_bucket, *v.sig)) last = v next_sig = next(sig_readers[v.reader_id], None) if next_sig: assert next_sig >= v, f'Next sig sort error. next_sig={next_sig!r} < v={v!r}' heapq.heappush(pq, next_sig) if out_index: out_index.close() class MinhashDedupCluster(PipelineStep): type = '🫂 - DEDUP' name = '🎯 MinHash stage 3' def __init__(self, input_folder: DataFolderLike, output_folder: DataFolderLike, config: MinhashConfig=None, save_cluster_id: bool=False, ignore_index_matches: bool=False, lines_to_buffer: int=5): super().__init__() self.input_folder = get_datafolder(input_folder) self.output_folder = get_datafolder(output_folder) self.config = config or MinhashConfig() self.save_cluster_id = save_cluster_id self.ignore_index_matches = ignore_index_matches self.lines_to_buffer = lines_to_buffer def run(self, data: DocumentsPipeline=None, _: int=0, world_size: int=1): dup_files = self.input_folder.list_files(glob_pattern='*.dups') assert len(dup_files) % self.config.num_buckets == 0, 'Number of .dups files should be divisible by number of buckets' assert world_size == 1, 'World size must be 1 for clustering' union_set = {} def parent(x): if x not in union_set or union_set[x] == x: return x union_set[x] = parent(union_set[x]) return union_set[x] with self.track_time(): for dup_file in dup_files: with self.input_folder.open(dup_file, 'rb') as dupf: for (f1, d1, f2, d2) in read_tuples_from_file(dupf, '4I', lines_to_buffer=self.lines_to_buffer): (a, b) = ((f1, d1), (f2, d2)) if self.ignore_index_matches and a == (SENTINEL, SENTINEL): continue union_set[parent(b)] = parent(a) ci = 0 cluster_ids = {} with self.output_folder.get_output_file_manager(mode='wb') as output_mg: for node in sorted(union_set.keys()): self.stat_update('duplicates') (file, doc) = node p = parent(node) if node != p: output_mg.write(f'{file:06d}.remove', struct.pack('<I', doc)) self.stat_update('to_remove') if self.save_cluster_id: if p not in cluster_ids: cluster_ids[p] = ci ci += 1 self.stat_update('clusters') output_mg.write(f'{file:06d}.clusters', struct.pack('<I', doc)) output_mg.write(f'{file:06d}.clusters', struct.pack('<I', cluster_ids[p])) class MinhashDedupFilter(PipelineStep): type = '🫂 - DEDUP' name = '🎯 MinHash stage 4' def __init__(self, input_folder: DataFolderLike, exclusion_writer: DiskWriter=None, load_cluster_ids: bool=False, lines_to_buffer: int=5): super().__init__() self.data_folder = get_datafolder(input_folder) self.exclusion_writer = exclusion_writer self.load_cluster_ids = load_cluster_ids self.lines_to_buffer = lines_to_buffer def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1): clusters_data = self.data_folder.get_shard(rank, world_size, glob_pattern='*.clusters') assert not self.load_cluster_ids or len(clusters_data) <= 1, f'Must have exactly one .clusters file per task. Found {len(clusters_data)} files.' if not self.data_folder.isfile(f'{rank:06d}.remove'): logger.warning(f'No .remove file for rank={rank!r}.') for doc in data: self.stat_update(StatHints.total, StatHints.forwarded) yield doc return with self.data_folder.open(f'{rank:06d}.remove', 'rb') as f: with self.exclusion_writer if self.exclusion_writer else contextlib.nullcontext() as exc_writer: def get_next(): data = f.read(struct.calcsize('I')) if data: return struct.unpack('<I', data)[0] def load_clusters(): if clusters_data: with self.data_folder.open(clusters_data[0], 'rb') as clustersf: yield from read_tuples_from_file(clustersf, '2I', lines_to_buffer=self.lines_to_buffer) if self.load_cluster_ids: cluster_loader = load_clusters() next_cluster = next(cluster_loader, None) next_removal = get_next() for (idx, doc) in enumerate(data): with self.track_time(): if self.load_cluster_ids: if next_cluster and idx == next_cluster[0]: doc.metadata['minhash_cluster'] = next_cluster[1] next_cluster = next(cluster_loader, None) self.stat_update(StatHints.total) if next_removal == idx: self.stat_update(StatHints.dropped) if self.exclusion_writer: exc_writer.write(doc, rank) next_removal = get_next() continue self.stat_update(StatHints.forwarded) yield doc class MinhashBuildIndex(PipelineStep): type = '🫂 - DEDUP' name = '🎯 MinHash build index' def __init__(self, input_folder: DataFolderLike, output_folder: DataFolderLike, index_name: str, config: MinhashConfig=None, lines_to_buffer: int=5): super().__init__() self.input_folder = input_folder self.output_folder = output_folder self.config = config or MinhashConfig() self.index_name = index_name self.lines_to_buffer = lines_to_buffer def run(self, data: DocumentsPipeline=None, bucket: int=0, world_size: int=1): assert data is None, 'You should not use an input block before MinhashBuildIndex' assert world_size == self.config.num_buckets, 'You must run exactly one task per bucket' sig_files = self.input_folder.list_files(subdirectory=f'bucket_{bucket:03d}') sig_readers = [read_sigs(file, file_i, self.config, lines_to_buffer=self.lines_to_buffer) for (file_i, file) in enumerate(self.input_folder.open_files(sig_files, mode='rb'))] pq = [next(sig_reader) for sig_reader in sig_readers] heapq.heapify(pq) out_f = self.output_folder.open(f'bucket_{bucket:03d}/{self.index_name}.minhash.index', mode='wb') last: HashSig | None = None with self.track_time(): while pq: v: HashSig = heapq.heappop(pq) if not last or last.sig != v.sig: out_f.write(struct.pack(f'<%d{self.config.hash_config.struct_format}' % self.config.hashes_per_bucket, *v.sig)) last = v next_sig = next(sig_readers[v.file_id], None) if next_sig: heapq.heappush(pq, next_sig) out_f.close() # File: datatrove-main/src/datatrove/pipeline/dedup/sentence_dedup.py """""" import contextlib import dataclasses import heapq import struct from concurrent.futures import ThreadPoolExecutor from dataclasses import dataclass, field from pathlib import Path from typing import BinaryIO, Generator import numpy as np from fsspec.spec import AbstractBufferedFile from tqdm import tqdm from datatrove.data import Document, DocumentsPipeline from datatrove.io import DataFolderLike, get_datafolder from datatrove.pipeline.base import PipelineStep from datatrove.utils.binaryio import read_np_from_file, read_tuples_from_file from datatrove.utils.hashing import HashConfig, create_hash_func from datatrove.utils.logging import logger from datatrove.utils.text import SPLIT_TEXT_SENTENCES, TextNormConfig, ngrams, simplify_text, split_into_parts from datatrove.utils.typeshelper import ExtensionHelperSD, Languages, StatHints from ...utils.word_tokenizers import load_word_tokenizer from ..writers.disk_base import DiskWriter @dataclass class SentDedupConfig: n_sentences: int = 3 split_sentences: bool = True only_dedup_in_index: bool = True min_doc_words: int = 50 min_num_sentences: int = 3 min_words_to_remove_span: int = 0 norm_config: TextNormConfig = field(default_factory=TextNormConfig) hash_config: HashConfig = field(default_factory=HashConfig) @dataclass(order=True) class HashSig: hash_value: int doc_id: int file_id: int = None sent_id: int = None file_stem: str = None def is_from_index(self): return self.doc_id == self.sent_id == -1 class SentenceDedupSignature(PipelineStep): type = '🫂 - DEDUPS' name = '💥 sentence-deduplication stage 1' def __init__(self, output_folder: DataFolderLike, finder_workers: int=1, config: SentDedupConfig=None, language: str=Languages.english): super().__init__() self.output_folder = get_datafolder(output_folder) if finder_workers <= 0: raise ValueError('finder_workers must be >= 1') elif finder_workers > 1: logger.warning(f'Remember to also set the name of tasks of the finder block to finder_workers={finder_workers!r}!') self.finder_workers = finder_workers self.config = config or SentDedupConfig() self.hash_fc = create_hash_func(config.hash_config) self.language = language self.tokenizer = load_word_tokenizer(language) def save_hashes(self, rank: int, signatures): signatures = np.array(signatures, dtype=[('hash', self.config.hash_config.np_descr), ('doc', '<u4'), ('sent', '<u2')]) signatures.sort(axis=0) hashes_per_worker = self.config.hash_config.max // self.finder_workers left_idx = 0 for hash_i in range(self.finder_workers): with self.output_folder.open(f'{hash_i:04d}/{rank:05d}{ExtensionHelperSD.stage_1_signature}', mode='wb') as f: right_hash = (hash_i + 1) * hashes_per_worker if hash_i != self.finder_workers - 1 else self.config.hash_config.max right_idx = left_idx + signatures['hash'][left_idx:].searchsorted(right_hash, side='right') if right_idx > left_idx: if self.output_folder.is_local(): signatures[left_idx:right_idx].tofile(f) else: f.write(signatures[left_idx:right_idx].tobytes()) left_idx = right_idx if right_idx >= len(signatures): break def get_hashes(self, doc: Document, doc_idx: int) -> list[None] | list[tuple[int, int, int]]: sentences = self.tokenizer.sent_tokenize(doc.text) if self.config.split_sentences else doc.text.splitlines() if len(sentences) < self.config.n_sentences: return [] sentences_tokens = [simplify_text(sent, self.config.norm_config) for sent in sentences] n_sent_grams: list = [' '.join(x) for x in ngrams(sentences_tokens, self.config.n_sentences)] hashes = [(self.hash_fc(n_sent_gram), doc_idx, sentence_idx) for (sentence_idx, n_sent_gram) in enumerate(n_sent_grams) if n_sent_gram.strip() != ''] return hashes def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1): signatures = [] for (doc_idx, doc) in enumerate(data): with self.stats.time_stats: self.stat_update(StatHints.total) signatures.extend(self.get_hashes(doc, doc_idx)) self.save_hashes(rank, signatures) def read_sigs(file: AbstractBufferedFile, file_id: int, config: SentDedupConfig, index_file: bool=False, lines_to_buffer: int=5) -> Generator[HashSig, None, None]: line_format = f'{config.hash_config.struct_format}IH' if not index_file else config.hash_config.struct_format file_stem = Path(file.path).name.removesuffix(ExtensionHelperSD.stage_1_signature) last = None with file as f: for data in read_tuples_from_file(f, line_format, lines_to_buffer=lines_to_buffer): assert last is None or data[0] >= last, f'Hash order error. f.tell()={f.tell()!r}, data[0]={data[0]!r}, last={last!r}' last = data[0] yield (HashSig(hash_value=data[0], doc_id=-1, file_id=file_id, sent_id=-1, file_stem=file_stem) if index_file else HashSig(file_id=file_id, hash_value=data[0], doc_id=data[1], sent_id=data[2], file_stem=file_stem)) class SentenceFindDedups(PipelineStep): type = '🫂 - DEDUPS' name = '💥 sentence-deduplication stage 2' def __init__(self, data_folder: DataFolderLike, output_folder: DataFolderLike, index_folder: DataFolderLike=None, config: SentDedupConfig=None, lines_to_buffer: int=5): super().__init__() self.data_folder = get_datafolder(data_folder) self.output_folder = get_datafolder(output_folder) self.index_folder = get_datafolder(index_folder) if index_folder else None self.config = config or SentDedupConfig() self.lines_to_buffer = lines_to_buffer def run(self, data: DocumentsPipeline=None, rank: int=0, world_size: int=1): with self.stats.time_stats: if world_size == 1: sig_files = self.data_folder.list_files(glob_pattern='*/*' + ExtensionHelperSD.stage_1_signature) if any((not sig_file.startswith('0000/') for sig_file in sig_files)): raise ValueError(f'world_size={world_size!r} but found sig files for different hash buckets. Set tasks=finder_workers') else: sig_files = self.data_folder.list_files(subdirectory=f'{rank:04d}', glob_pattern=ExtensionHelperSD.stage_1_signature) sig_readers = [read_sigs(file, file_i, config=self.config, lines_to_buffer=self.lines_to_buffer) for (file_i, file) in enumerate(self.data_folder.open_files(sig_files))] index_files = self.index_folder.list_files() if self.index_folder else None if index_files: logger.info(f"Found index file(s): {', '.join(index_files)}") sig_readers.extend([read_sigs(file, len(sig_readers) + file_i, config=self.config, index_file=True, lines_to_buffer=self.lines_to_buffer) for (file_i, file) in enumerate(self.data_folder.open_files(index_files))]) logger.info(f'Initializing pq with {len(sig_readers)} files.') with ThreadPoolExecutor() as executor: pq = [x for x in tqdm(executor.map(lambda x: next(x, None), sig_readers), total=len(sig_readers), desc='Initializing pq...') if x] heapq.heapify(pq) logger.info('PQ initialized.') output_mg = self.output_folder.get_output_file_manager(mode='wb') packer = struct.Struct('<IH') last: HashSig | None = None while pq: v: HashSig = heapq.heappop(pq) if last and last.hash_value == v.hash_value and (not v.is_from_index()): out_filename = f'{rank:04d}/{v.file_stem}{ExtensionHelperSD.stage_2_duplicates}' if last.is_from_index() or not index_files or (not self.config.only_dedup_in_index): output_mg.write(out_filename, packer.pack(v.doc_id, v.sent_id)) last = v new_v = next(sig_readers[v.file_id], None) if new_v: heapq.heappush(pq, new_v) output_mg.close() class SentenceDedupFilter(PipelineStep): type = '🫂 - DEDUPS' name = '💥 sentence-deduplication stage 3' def __init__(self, data_folder: DataFolderLike, config: SentDedupConfig=None, exclusion_writer: DiskWriter=None, language: str=Languages.english): super().__init__() self.data_folder = get_datafolder(data_folder) self.config = config or SentDedupConfig() self.tokenizer = load_word_tokenizer(language) self.exclusion_writer = exclusion_writer self.language = language def read_duplicates(self, file: BinaryIO) -> np.ndarray: return read_np_from_file(file, dtype=np.dtype([('doc', '<u4'), ('sent', '<u2')]), is_local_file=self.data_folder.is_local()) def remove_dup_sentences(self, doc: Document, du_lines: np.ndarray) -> tuple[str, str]: sentence_spans = list(self.tokenizer.span_tokenize(doc.text)) if self.config.split_sentences else doc.text.splitlines() kept_sentences = [] original_formatted = [] last_s = 0 du_line_idx = 0 drop_until = 0 removed_span = [] for (idx, s) in enumerate(sentence_spans): line_text = doc.text[last_s:s[1]] if self.config.split_sentences else s if du_line_idx < len(du_lines): if du_lines[du_line_idx] < idx: raise ValueError('Error with duplicate line index') elif du_lines[du_line_idx] == idx: drop_until = idx + self.config.n_sentences du_line_idx += 1 if idx >= drop_until: if removed_span: original_formatted.append('<<<') if self.config.min_words_to_remove_span > 0 and len(self.tokenizer.word_tokenize('\n'.join(removed_span))) < self.config.min_words_to_remove_span: kept_sentences.extend(removed_span) removed_span.clear() kept_sentences.append(line_text) elif not removed_span: removed_span.append(line_text) original_formatted.append('>>>') original_formatted.append(line_text) if self.config.split_sentences: last_s = s[1] if removed_span: original_formatted.append('<<<') if self.config.min_words_to_remove_span > 0 and len(self.tokenizer.word_tokenize('\n'.join(removed_span))) < self.config.min_words_to_remove_span: kept_sentences.extend(removed_span) if len(kept_sentences) < len(sentence_spans): self.stat_update('removed_sentences', value=len(sentence_spans) - len(kept_sentences)) self.stat_update('original_sentences', value=len(sentence_spans)) merge_char = '' if self.config.split_sentences else '\n' return (merge_char.join(kept_sentences).lstrip(), merge_char.join(original_formatted)) def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1) -> DocumentsPipeline: folders = self.data_folder.list_files(include_directories=True, recursive=False) files = [f for f in [f'{folder}/{rank:05d}{ExtensionHelperSD.stage_2_duplicates}' for folder in folders] if self.data_folder.exists(f)] logger.info(f'Loading duplicate indexes from {len(files)} results files.') all_dups = np.array([], dtype=[('doc', '<u4'), ('sent', '<u2')]) if files: with ThreadPoolExecutor() as pool: all_dups = np.concatenate(list(tqdm(pool.map(self.read_duplicates, self.data_folder.open_files(files)), total=len(files))), axis=0) all_dups.sort() (_, doc_starts) = np.unique(all_dups['doc'], return_index=True) logger.info('Loaded duplicate indexes.') dups_doc_i = 0 with self.exclusion_writer if self.exclusion_writer else contextlib.nullcontext() as writer: for (doc_idx, doc) in enumerate(data): self.stat_update(StatHints.total) with self.stats.time_stats: if dups_doc_i >= len(doc_starts) or all_dups['doc'][doc_starts[dups_doc_i]] > doc_idx: (filtered_text, original_formatted) = (doc.text, None) else: (sents_span_l, sents_span_r) = (doc_starts[dups_doc_i], doc_starts[dups_doc_i + 1] if dups_doc_i + 1 < len(doc_starts) else None) (filtered_text, original_formatted) = self.remove_dup_sentences(doc, all_dups['sent'][sents_span_l:sents_span_r]) dups_doc_i += 1 if (filtered_text == doc.text or ((self.config.min_doc_words <= 0 or len(self.tokenizer.word_tokenize(filtered_text)) >= self.config.min_doc_words) and (self.config.min_num_sentences <= 0 or len(split_into_parts(filtered_text, SPLIT_TEXT_SENTENCES, self.language)) >= self.config.min_num_sentences))) and filtered_text: self.update_doc_stats(doc) if not filtered_text == doc.text and writer: writer.write(dataclasses.replace(doc, text=original_formatted), rank=rank) doc.text = filtered_text yield doc elif writer: doc.text = original_formatted writer.write(doc, rank=rank) class SentenceDedupBuildIndex(PipelineStep): type = '🫂 - DEDUP' name = '💥 sentence-deduplication build index' def __init__(self, data_folder: DataFolderLike, output_folder: DataFolderLike, index_name: str, config: SentDedupConfig=None, lines_to_buffer: int=5): super().__init__() self.data_folder = get_datafolder(data_folder) self.output_folder = get_datafolder(output_folder) self.index_name = index_name self.lines_to_buffer = lines_to_buffer self.config = config or SentDedupConfig() def run(self, data: DocumentsPipeline=None, rank: int=0, world_size: int=1): assert world_size == 1, 'SentenceDedupBuildIndex can only run on a single worker.' with self.stats.time_stats: sig_files = self.data_folder.list_files(glob_pattern=ExtensionHelperSD.stage_1_signature) sig_readers = [read_sigs(file, file_i, self.config, lines_to_buffer=self.lines_to_buffer) for (file_i, file) in enumerate(self.data_folder.open_files(sig_files))] pq = [next(sig_reader) for sig_reader in sig_readers] heapq.heapify(pq) with self.output_folder.open(f'{self.index_name}.{ExtensionHelperSD.index}', mode='wb') as out_f: last = None while pq: v: HashSig = heapq.heappop(pq) if last != v.hash_value: out_f.write(struct.pack(f'<{self.config.hash_config.struct_format}', v.hash_value)) last = v.hash_value new_v = next(sig_readers[v.file_id], None) if new_v: heapq.heappush(pq, new_v) # File: datatrove-main/src/datatrove/pipeline/dedup/url_dedup.py """""" import contextlib import heapq import struct from concurrent.futures import ThreadPoolExecutor from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import BinaryIO, Callable, Generator import numpy as np from fsspec.spec import AbstractBufferedFile from tqdm import tqdm from datatrove.data import Document, DocumentsPipeline from datatrove.io import DataFolderLike, get_datafolder from datatrove.pipeline.base import PipelineStep from datatrove.utils.binaryio import read_np_from_file, read_tuples_from_file from datatrove.utils.hashing import HashConfig, create_hash_func from datatrove.utils.logging import logger from datatrove.utils.typeshelper import ExtensionHelperSD, StatHints from ..writers.disk_base import DiskWriter @dataclass class UrlDedupConfig: url_normalizer: Callable[[str], str] | None = None document_priority: Callable[[Document], int] | None = None hash_config: HashConfig = field(default_factory=HashConfig) only_dedup_in_index: bool = True @dataclass(order=False) class HashSig: hash_value: int priority: int doc_id: int file_id: int file_stem: str def is_from_index(self): return self.doc_id == -1 and self.priority == 1 def __lt__(self, other: 'HashSig') -> bool: return (self.hash_value, -self.priority, self.doc_id) < (other.hash_value, -other.priority, other.doc_id) def get_sig_dtype(config: HashConfig) -> np.dtype: return np.dtype([('hash', config.np_dtype), ('priority', '<u2'), ('doc', '<u4')]) class UrlDedupSignature(PipelineStep): type = '🫂 - DEDUPS' name = '💥 url-deduplication stage 1' def __init__(self, output_folder: DataFolderLike, finder_workers: int=1, config: UrlDedupConfig | None=None): super().__init__() self.output_folder = get_datafolder(output_folder) if finder_workers <= 0: raise ValueError('finder_workers must be >= 1') elif finder_workers > 1: logger.warning(f'Remember to also set the number of tasks of the finder block to finder_workers={finder_workers!r}!') self.finder_workers = finder_workers self.config = config or UrlDedupConfig() self.hash_fc = create_hash_func(self.config.hash_config) def save_hashes(self, rank: int, signatures): sig_dtype = get_sig_dtype(self.config.hash_config) priority_max = np.iinfo(sig_dtype['priority']).max assert all((sig[1] >= 1 and sig[1] <= priority_max for sig in signatures)), f'priority must be between 1 and {priority_max}' signatures = np.array(signatures, dtype=sig_dtype) signatures['priority'] = -signatures['priority'] signatures.sort(axis=0) signatures['priority'] = -signatures['priority'] hashes_per_worker = self.config.hash_config.max // self.finder_workers left_idx = 0 for hash_i in range(self.finder_workers): with self.output_folder.open(f'{hash_i:04d}/{rank:05d}{ExtensionHelperSD.stage_1_signature}', mode='wb') as f: right_hash = (hash_i + 1) * hashes_per_worker if hash_i != self.finder_workers - 1 else np.iinfo(np.uint64).max right_idx = left_idx + signatures['hash'][left_idx:].searchsorted(right_hash, side='right') if right_idx > left_idx: bts = signatures[left_idx:right_idx].tobytes() f.write(bts) left_idx = right_idx if right_idx >= len(signatures): break def get_hashes(self, doc: Document, doc_idx: int) -> list[None] | list[tuple[int, int, int]]: normalized_url: str = self.config.url_normalizer(doc.metadata['url']) if self.config.url_normalizer else doc.metadata['url'] priority = self.config.document_priority(doc) if self.config.document_priority else 1 hashes = [(self.hash_fc(normalized_url), priority, doc_idx)] return hashes def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1): signatures = [] for (doc_idx, doc) in enumerate(data): with self.stats.time_stats: self.stat_update(StatHints.total) signatures.extend(self.get_hashes(doc, doc_idx)) self.save_hashes(rank, signatures) def read_sigs(file: AbstractBufferedFile, file_id: int, hash_config: HashConfig, index_file: bool=False, lines_to_buffer: int=5) -> Generator[HashSig, None, None]: last = None line_format = f'{hash_config.struct_format}HI' if not index_file else hash_config.struct_format with file as f: file_stem = Path(f.path).name.removesuffix(ExtensionHelperSD.stage_1_signature) for data in read_tuples_from_file(f, line_format, lines_to_buffer=lines_to_buffer): assert last is None or data[0] >= last, f'Hash order error. f.tell()={f.tell()!r}, data[0]={data[0]!r}, last={last!r}' last = data[0] yield (HashSig(hash_value=data[0], doc_id=-1, file_id=file_id, priority=-1, file_stem=file_stem) if index_file else HashSig(file_id=file_id, file_stem=file_stem, hash_value=data[0], priority=data[1], doc_id=data[2])) class UrlFindDedups(PipelineStep): type = '🫂 - DEDUPS' name = '💥 url-deduplication stage 2' def __init__(self, data_folder: DataFolderLike, output_folder: DataFolderLike, index_folder: DataFolderLike | None=None, config: UrlDedupConfig | None=None, lines_to_buffer: int=5): super().__init__() self.data_folder = get_datafolder(data_folder) self.output_folder = get_datafolder(output_folder) self.index_folder = get_datafolder(index_folder) if index_folder else None self.config = config or UrlDedupConfig() self.lines_to_buffer = lines_to_buffer def run(self, data: DocumentsPipeline=None, rank: int=0, world_size: int=1): with self.stats.time_stats: if world_size == 1: sig_files = self.data_folder.list_files(glob_pattern='*/*' + ExtensionHelperSD.stage_1_signature) if any((not sig_file.startswith('0000/') for sig_file in sig_files)): raise ValueError(f'world_size={world_size!r} but found sig files for different hash buckets. Set tasks=finder_workers') else: sig_files = self.data_folder.list_files(subdirectory=f'{rank:04d}', glob_pattern=ExtensionHelperSD.stage_1_signature) sig_readers = [read_sigs(file, file_i, self.config.hash_config, lines_to_buffer=self.lines_to_buffer) for (file_i, file) in enumerate(self.data_folder.open_files(sig_files))] index_files = self.index_folder.list_files() if self.index_folder else None if index_files: logger.info(f"Found index file(s): {', '.join(index_files)}") sig_readers.extend([read_sigs(file, len(sig_readers) + file_i, self.config.hash_config, index_file=True, lines_to_buffer=self.lines_to_buffer) for (file_i, file) in enumerate(self.data_folder.open_files(index_files))]) logger.info(f'Initializing pq with {len(sig_readers)} files.') with ThreadPoolExecutor() as executor: pq = [x for x in tqdm(executor.map(lambda x: next(x, None), sig_readers), total=len(sig_readers), desc='Initializing pq...') if x] heapq.heapify(pq) logger.info('PQ initialized.') output_mg = self.output_folder.get_output_file_manager(mode='wb') last: HashSig | None = None packer = struct.Struct('<I') while pq: v: HashSig = heapq.heappop(pq) if last and last.hash_value == v.hash_value and (not v.is_from_index()): out_filename = f'{rank:04d}/{v.file_stem}{ExtensionHelperSD.stage_2_duplicates}' if not index_files or last.is_from_index() or (not self.config.only_dedup_in_index): doc_id_bytes = packer.pack(v.doc_id) output_mg.write(out_filename, doc_id_bytes) last = v new_v = next(sig_readers[v.file_id], None) if new_v: heapq.heappush(pq, new_v) output_mg.close() class UrlDedupFilter(PipelineStep): type = '🫂 - DEDUPS' name = '💥 url-deduplication stage 3' def __init__(self, data_folder: DataFolderLike, config: UrlDedupConfig | None=None, exclusion_writer: DiskWriter | None=None): super().__init__() self.data_folder = get_datafolder(data_folder) self.config = config or UrlDedupConfig() self.exclusion_writer = exclusion_writer def read_duplicates(self, file: BinaryIO, dup_dtype: np.dtype) -> np.ndarray: with file as f: return read_np_from_file(f, dtype=dup_dtype, is_local_file=self.data_folder.is_local()) def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1): folders = self.data_folder.list_files(include_directories=True, recursive=False) files = [f for f in [f'{folder}/{rank:05d}{ExtensionHelperSD.stage_2_duplicates}' for folder in folders] if self.data_folder.exists(f)] logger.info(f'Loading duplicate indexes from {len(files)} results files.') dup_dtype = get_sig_dtype(self.config.hash_config)[2] all_dups = np.array([], dtype=dup_dtype) if files: with ThreadPoolExecutor() as pool: read_partial = partial(self.read_duplicates, dup_dtype=dup_dtype) all_dups = np.concatenate(list(tqdm(pool.map(read_partial, self.data_folder.open_files(files)), total=len(files))), axis=0) all_dups.sort() logger.info('Loaded duplicate indexes.') dups_doc_i = 0 with self.exclusion_writer if self.exclusion_writer else contextlib.nullcontext() as writer: with self.stats.time_stats: for (doc_idx, doc) in enumerate(data): self.stat_update(StatHints.total) with self.stats.time_stats: if dups_doc_i < all_dups.shape[0] and all_dups[dups_doc_i] == doc_idx: if writer: writer.write(doc, rank=rank) self.stat_update(StatHints.dropped) dups_doc_i += 1 else: self.stat_update(StatHints.forwarded) self.update_doc_stats(doc) yield doc class UrlDedupBuildIndex(PipelineStep): type = '🫂 - DEDUP' name = '💥 url-deduplication build index' def __init__(self, data_folder: DataFolderLike, output_folder: DataFolderLike, index_name: str, config: UrlDedupConfig | None=None, lines_to_buffer: int=5): super().__init__() self.data_folder = get_datafolder(data_folder) self.output_folder = get_datafolder(output_folder) self.index_name = index_name self.lines_to_buffer = lines_to_buffer self.config = config or UrlDedupConfig() def run(self, data: DocumentsPipeline=None, rank: int=0, world_size: int=1): assert world_size == 1, 'UrlDedupBuildIndex can only run on a single worker.' with self.stats.time_stats: sig_files = self.data_folder.list_files(glob_pattern=ExtensionHelperSD.stage_1_signature) sig_readers = [read_sigs(file, file_i, self.config.hash_config, lines_to_buffer=self.lines_to_buffer) for (file_i, file) in enumerate(self.data_folder.open_files(sig_files))] pq = [next(sig_reader) for sig_reader in sig_readers] heapq.heapify(pq) with self.output_folder.open(f'{self.index_name}.{ExtensionHelperSD.index}', mode='wb') as out_f: last = None while pq: v: HashSig = heapq.heappop(pq) if last != v.hash_value: out_f.write(struct.pack(f'<{self.config.hash_config.struct_format}', v.hash_value)) last = v.hash_value new_v = next(sig_readers[v.file_id], None) if new_v: heapq.heappush(pq, new_v) # File: datatrove-main/src/datatrove/pipeline/extractors/base.py from abc import abstractmethod from concurrent.futures import ThreadPoolExecutor from datatrove.data import DocumentsPipeline from datatrove.pipeline.base import PipelineStep from datatrove.utils.logging import logger from datatrove.utils.typeshelper import StatHints class BaseExtractor(PipelineStep): type = '🛢 - EXTRAC' @abstractmethod def __init__(self, timeout: float=0.1): super().__init__() self.timeout = timeout @abstractmethod def extract(self, text: str) -> str: pass def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1) -> DocumentsPipeline: with ThreadPoolExecutor() as executor: for doc in data: self.stat_update(StatHints.total) with self.track_time(): future = executor.submit(self.extract, doc.text) try: doc.text = future.result(timeout=self.timeout) except TimeoutError: logger.warning('⏰ Timeout while cleaning record text. Skipping record.') continue except Exception as e: logger.warning(f'❌ Error "{e}" while cleaning record text. Skipping record.') continue if doc.text: self.stat_update(StatHints.forwarded) self.update_doc_stats(doc) yield doc else: self.stat_update(StatHints.dropped) # File: datatrove-main/src/datatrove/pipeline/extractors/modular.py import re from .base import BaseExtractor class ReadabilityInscriptis(BaseExtractor): _requires_dependencies = ['inscriptis', ('readability', 'readability-lxml @ git+https://github.com/huggingface/python-readability.git@speedup')] def __init__(self, max_new_lines: int=2, min_text_length=25, min_text_score=20, timeout: float=0.1): from inscriptis.css_profiles import CSS_PROFILES from inscriptis.model.config import ParserConfig super().__init__(timeout) self.min_text_length = min_text_length self.min_text_score = min_text_score self.new_line_chars = '\n' * max_new_lines self.regex_excessive_lines = re.compile('(' + self.new_line_chars + '\n+)') self._parser_config = ParserConfig(css=CSS_PROFILES['strict']) def extract(self, text: str) -> str: from inscriptis import get_text from readability import Document as _Document parsed_doc = _Document(text, min_text_length=self.min_text_length, min_text_score=self.min_text_score) clean_html = parsed_doc.summary(html_partial=True) text = get_text(clean_html, self._parser_config).strip() return self.regex_excessive_lines.sub(self.new_line_chars, text) # File: datatrove-main/src/datatrove/pipeline/extractors/trafilatura.py from .base import BaseExtractor class Trafilatura(BaseExtractor): name = '⛏ Trafilatura' _requires_dependencies = ['trafilatura'] def __init__(self, favour_precision: bool=True, include_images: bool=False, timeout: float=0.1, deduplicate: bool=True, **kwargs): super().__init__(timeout) self.favour_precision = favour_precision self.include_images = include_images self.deduplicate = deduplicate self.kwargs = kwargs if self.include_images: raise NotImplementedError def extract(self, text: str) -> str: from trafilatura import extract return extract(text, favor_precision=self.favour_precision, include_comments=False, deduplicate=self.deduplicate, **self.kwargs) # File: datatrove-main/src/datatrove/pipeline/filters/__init__.py from .c4_filters import C4BadWordsFilter, C4ParagraphFilter, C4QualityFilter from .fasttext_filter import FastTextClassifierFilter from .fineweb_quality_filter import FineWebQualityFilter from .gopher_quality_filter import GopherQualityFilter from .gopher_repetition_filter import GopherRepetitionFilter from .lambda_filter import LambdaFilter from .language_filter import LanguageFilter from .regex_filter import RegexFilter from .sampler_filter import SamplerFilter from .unigram_log_probs import UnigramLogProbFilter from .url_filter import URLFilter # File: datatrove-main/src/datatrove/pipeline/filters/base_filter.py import contextlib from abc import ABC, abstractmethod from typing import List, Tuple from loguru import logger from datatrove.data import Document, DocumentsPipeline from datatrove.pipeline.base import PipelineStep from datatrove.pipeline.writers.disk_base import DiskWriter from datatrove.utils.batching import batched from datatrove.utils.typeshelper import StatHints def get_filter_result(res): (result, reason) = (res, None) if isinstance(result, tuple): (result, reason) = res return (result, reason) class BaseFilter(PipelineStep, ABC): type = '🔻 - FILTER' def __init__(self, exclusion_writer: DiskWriter=None, batch_size: int=1): super().__init__() self.exclusion_writer = exclusion_writer self.batch_size = batch_size if self.batch_size > 1 and type(self).filter_batch == BaseFilter.filter_batch: logger.warning(f'batch_size={batch_size!r} > 1 but {self} does not implement a custom filter_batch method.') @abstractmethod def filter(self, doc: Document) -> bool | Tuple[bool, str]: raise NotImplementedError def filter_batch(self, batch: List[Document]) -> List[bool | Tuple[bool, str]]: return list(map(self.filter, batch)) def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1) -> DocumentsPipeline: with self.exclusion_writer if self.exclusion_writer else contextlib.nullcontext() as writer: for batch in batched(data, self.batch_size): if self.batch_size > 1: self.stat_update('batches') with self.track_time('batch' if self.batch_size > 1 else None): batch_filter_result = self.filter_batch(batch) for (doc, doc_filter_result) in zip(batch, batch_filter_result): self.stat_update(StatHints.total) (filter_result, reason) = get_filter_result(doc_filter_result) if filter_result: self.stat_update(StatHints.forwarded) self.update_doc_stats(doc) yield doc else: self.stat_update(StatHints.dropped) if reason: self.stat_update(f'dropped_{reason}') if self.exclusion_writer: if reason: doc.metadata['filter_reason'] = reason writer.write(doc, rank) # File: datatrove-main/src/datatrove/pipeline/filters/c4_filters.py import heapq import re from numpy.random import default_rng from datatrove.data import Document from datatrove.io import cached_asset_path_or_download from datatrove.pipeline.filters.base_filter import BaseFilter from datatrove.pipeline.writers.disk_base import DiskWriter from datatrove.utils.typeshelper import Languages from datatrove.utils.word_tokenizers import load_word_tokenizer CITATION_REGEX = re.compile('\\[\\d*]|\\[edit]|\\[citation needed]') END_PUNCTUATION = ('.', '?', '!', '"', "'") ELLIPSIS = '...' POLICY_SUBSTRINGS = ['terms of use', 'privacy policy', 'cookie policy', 'uses cookies', 'use of cookies', 'use cookies'] class C4QualityFilter(BaseFilter): name = '⛰ C4 Quality' def __init__(self, exclusion_writer: DiskWriter=None, split_paragraph: bool=True, remove_citations: bool=True, filter_no_terminal_punct: bool=True, min_num_sentences: int=5, min_words_per_line: int=3, max_word_length: int=1000, filter_lorem_ipsum: bool=True, filter_javascript: bool=True, filter_curly_bracket: bool=True, filter_policy: bool=True, language: str=Languages.english): super().__init__(exclusion_writer) self.split_paragraph = split_paragraph self.remove_citations = remove_citations self.filter_no_terminal_punct = filter_no_terminal_punct self.min_num_sentences = min_num_sentences self.min_words_per_line = min_words_per_line self.max_word_length = max_word_length self.filter_lorem_ipsum = filter_lorem_ipsum self.filter_javascript = filter_javascript self.filter_curly_bracket = filter_curly_bracket self.filter_policy = filter_policy self.tokenizer = load_word_tokenizer(language) def filter(self, doc: Document) -> bool | tuple[bool, str]: lines = doc.text.splitlines() if self.split_paragraph else self.tokenizer.sent_tokenize(doc.text) num_sentences = 0 kept_lines = [] for line in lines: line = line.strip() words = line.split() self.stat_update('line-total') if self.max_word_length != -1 and any((len(word) > self.max_word_length for word in words)): self.stat_update('line-filter-too_long_word') continue if self.remove_citations: line = CITATION_REGEX.sub('', line) if self.filter_no_terminal_punct and (not line.endswith(END_PUNCTUATION) or line.endswith(ELLIPSIS)): self.stat_update('line-filter-no_terminal_punc') continue if len(words) < self.min_words_per_line: self.stat_update('line-filter-too_few_words') continue line_l = line.lower() if self.filter_lorem_ipsum and 'lorem ipsum' in line_l: return (False, 'lorem_ipsum') if self.filter_javascript and 'javascript' in line_l: self.stat_update('line-filter-javascript') continue if self.filter_curly_bracket and '{' in line: return (False, 'curly_bracket') if self.filter_policy and any((p in line_l for p in POLICY_SUBSTRINGS)): self.stat_update('line-filter-policy') continue if self.min_num_sentences != -1: num_sentences += len(self.tokenizer.sent_tokenize(line)) if self.split_paragraph else 1 kept_lines.append(line) self.stat_update('line-kept') if num_sentences < self.min_num_sentences: return (False, 'too_few_sentences') doc.text = ('\n' if self.split_paragraph else ' ').join(kept_lines).strip() return True class C4ParagraphFilter(BaseFilter): name = '⛰ C4 Paragraph' def __init__(self, exclusion_writer: DiskWriter=None): super().__init__(exclusion_writer) self.min_paragraphs = 3 self.min_paragraph_len = 200 self.line_delimiter = '\n' def paragraph_filter(self, page): lines = page.split(self.line_delimiter) if len(lines) < self.min_paragraphs or min(heapq.nlargest(3, [len(line) for line in lines])) < self.min_paragraph_len: return False return True def filter(self, doc: Document) -> bool | tuple[bool, str]: if not self.paragraph_filter(doc.text): return (False, f'< {self.min_paragraphs} paragraphs') return True _EN_BADWORDS_URL = 'https://raw.githubusercontent.com/LDNOOBW/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words/25e679f03d96baa721cde20db9944649e8d0a844/en' _BADWORDS_URL = 'https://raw.githubusercontent.com/LDNOOBW/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words/5faf2ba42d7b1c0977169ec3611df25a3c08eb13/' _BADWORDS_LANGS = ['ar', 'cs', 'da', 'de', 'en', 'eo', 'es', 'fa', 'fi', 'fil', 'fr', 'fr-CA-u-sd-caqc', 'hi', 'hu', 'it', 'ja', 'kab', 'ko', 'nl', 'no', 'pl', 'pt', 'ru', 'sv', 'th', 'tlh', 'tr', 'zh'] _BADWORDS_ALLOWLIST = {'ja': {'sm', 'グロ', '女の子'}, 'zh': {'性'}} class C4BadWordsFilter(BaseFilter): name = '⛰ C4 Badwords' def __init__(self, keep_fraction: float=0.0, fail_on_missing_language: bool=True, seed: int=None, default_language: str='en', exclusion_writer: DiskWriter=None): super().__init__(exclusion_writer) self.keep_fraction = keep_fraction self.fail_on_missing_language = fail_on_missing_language self._badwords_regex: dict[str, re.Pattern] = {} self.uniform = default_rng(seed).uniform self.default_language = default_language def _get_badwords(self, lang: str): if lang not in self._badwords_regex: if lang not in _BADWORDS_LANGS: if self.fail_on_missing_language: raise ValueError(f'There is not badwords list available for "{lang}". Set fail_on_missing_language=False to continue anyway.') else: return None local_path = cached_asset_path_or_download(_BADWORDS_URL + lang if lang != 'en' else _EN_BADWORDS_URL, namespace='filters', subfolder='c4_badwords') badwords: set[str] = set() with open(local_path, 'rt') as f: badwords.update((line.strip() for line in f)) for (lang, allowlist) in _BADWORDS_ALLOWLIST.items(): badwords -= allowlist words = [re.escape(w) for w in badwords] self._badwords_regex[lang] = re.compile('|'.join(words)) if lang in ('ja', 'th', 'zh') else re.compile('(?:\\W|^)({})(?:\\W|$)'.format('|'.join(words))) return self._badwords_regex[lang] def filter(self, doc: Document) -> bool | tuple[bool, str]: lang: str = doc.metadata.get('language', self.default_language) badwords_regex = self._get_badwords(lang) if badwords_regex is None: self.stat_update('missing_badwords_lang', f'missing_badwords_lang_{lang}') return True badwords_found = badwords_regex.search(doc.text.lower()) if badwords_found is not None: self.stat_update('documents_with_badwords', f'documents_with_badwords_{lang}') if self.keep_fraction > 0.0 and self.uniform() < self.keep_fraction: self.stat_update('document_kept_with_badwords', f'document_kept_with_badwords_{lang}') return True self.stat_update(f'document_removed_with_badwords_{lang}') return (False, 'document_removed_with_badwords') return True # File: datatrove-main/src/datatrove/pipeline/filters/fasttext_filter.py from collections import defaultdict from typing import Tuple import numpy as np from datatrove.data import Document from datatrove.io import cached_asset_path_or_download from datatrove.pipeline.filters.base_filter import BaseFilter from datatrove.pipeline.writers.disk_base import DiskWriter from datatrove.utils.text import SPLIT_TEXT_DOCUMENTS, split_into_parts class FastTextClassifierFilter(BaseFilter): name = '🤖 fastText' _requires_dependencies = [('fasttext', 'fasttext-wheel'), 'fasteners'] def __init__(self, model_url: str, keep_labels: Tuple[str, float] | list[Tuple[str, float]] | None=None, remove_labels: Tuple[str, float] | list[Tuple[str, float]] | None=None, save_labels_in_metadata: bool=True, exclusion_writer: DiskWriter | None=None, newline_replacement='', filter_mode: str=SPLIT_TEXT_DOCUMENTS): super().__init__(exclusion_writer) self.model_url = model_url self.keep_labels = keep_labels self.remove_labels = remove_labels self.filter_mode = filter_mode if keep_labels and remove_labels: raise ValueError('You can only supply one of `keep_labels` or `remove_labels`.') self.newline_replacement = newline_replacement if keep_labels and isinstance(keep_labels[0], str): self.keep_labels = [keep_labels] if remove_labels and isinstance(remove_labels[0], str): self.remove_labels = [remove_labels] self.save_labels_in_metadata = save_labels_in_metadata self._model = None @property def model(self): if self._model is None: from fasttext.FastText import _FastText model_file = cached_asset_path_or_download(self.model_url, namespace='filters', subfolder='fasttext', desc='fast-text model') self._model = _FastText(model_file) available_labels = [x.removeprefix('__label__') for x in self._model.labels] for (label, _) in self.keep_labels or [] + self.remove_labels or []: if label not in available_labels: raise ValueError(f"Label '{label}' passed as keep_labels or remove_labels is not available in this FastText model. Available labels: {available_labels}") return self._model def filter(self, doc: Document) -> bool: def check_label_scores(unit_scores): if self.keep_labels: return any((unit_scores.get(f'__label__{label}', -9000000000.0) >= min_score for (label, min_score) in self.keep_labels)) else: return not self.remove_labels or not any((unit_scores.get(f'__label__{label}', -9000000000.0) >= min_score for (label, min_score) in self.remove_labels)) units = split_into_parts(doc.text, mode=self.filter_mode) kept_spans = [] label_scores = defaultdict(list) for unit in units: (labels, scores) = self.model.predict(unit.strip().replace('\n', self.newline_replacement), k=-1) if self.save_labels_in_metadata: for (label, score) in zip(labels, scores): label_scores[label].append(score) if check_label_scores(dict(zip(labels, scores))): kept_spans.append(unit) self.stat_update('kept_span') else: self.stat_update('removed_span') doc.text = ''.join(kept_spans) if self.save_labels_in_metadata: doc.metadata.update({label: np.mean(scores).item() for (label, scores) in label_scores.items()}) return not not doc.text.strip() # File: datatrove-main/src/datatrove/pipeline/filters/fineweb_quality_filter.py from datatrove.pipeline.filters.base_filter import BaseFilter from datatrove.pipeline.filters.gopher_repetition_filter import find_duplicates from datatrove.pipeline.writers.disk_base import DiskWriter from datatrove.utils.typeshelper import Languages from datatrove.utils.word_tokenizers import load_word_tokenizer class FineWebQualityFilter(BaseFilter): name = '🍷 FineWeb Quality' def __init__(self, exclusion_writer: DiskWriter=None, line_punct_thr: float=0.12, line_punct_exclude_zero: bool=False, short_line_thr: float=0.67, short_line_length: int=30, char_duplicates_ratio: float=0.01, new_line_ratio: float=0.3, language: str=Languages.english): super().__init__(exclusion_writer) self.line_punct_thr = line_punct_thr self.line_punct_exclude_zero = line_punct_exclude_zero self.short_line_threshold = short_line_thr self.short_line_length = short_line_length self.char_duplicates_ratio = char_duplicates_ratio self.new_line_ratio = new_line_ratio self.tokenizer = load_word_tokenizer(language) def filter(self, doc) -> bool | tuple[bool, str]: stop_chars = ('.', "'", '"', '!', '?') lines = doc.text.split('\n') ratio = sum((1 for line in lines if line.endswith(stop_chars))) / len(lines) if ratio <= self.line_punct_thr and (not (ratio == 0 and self.line_punct_exclude_zero)): return (False, 'line_punct_ratio') ratio = sum((1 for line in lines if len(line) <= self.short_line_length)) / len(lines) if ratio >= self.short_line_threshold: return (False, 'short_line_ratio') non_empty_lines = [line for line in lines if line.strip() != ''] ratio = find_duplicates(non_empty_lines)[1] / len(doc.text.replace('\n', '')) if ratio >= self.char_duplicates_ratio: return (False, 'char_dup_ratio') words = self.tokenizer.word_tokenize(doc.text) new_line = doc.text.count('\n') if new_line / len(words) > self.new_line_ratio: return (False, 'list_ratio') return True # File: datatrove-main/src/datatrove/pipeline/filters/gopher_quality_filter.py import numpy as np from datatrove.data import Document from datatrove.pipeline.filters.base_filter import BaseFilter from datatrove.pipeline.writers.disk_base import DiskWriter from datatrove.utils.text import PUNCTUATION_SET from datatrove.utils.typeshelper import Languages from datatrove.utils.word_tokenizers import load_word_tokenizer STOP_WORDS = ['the', 'be', 'to', 'of', 'and', 'that', 'have', 'with'] class GopherQualityFilter(BaseFilter): name = '🥇 Gopher Quality' def __init__(self, min_doc_words: int | None=50, max_doc_words: int | None=100000, min_avg_word_length: int | None=3, max_avg_word_length: int | None=10, max_symbol_word_ratio: float | None=0.1, max_bullet_lines_ratio: float | None=0.9, max_ellipsis_lines_ratio: float | None=0.3, max_non_alpha_words_ratio: float | None=0.8, min_stop_words: int | None=2, stop_words: list[str] | None=None, exclusion_writer: DiskWriter=None, language: str=Languages.english): super().__init__(exclusion_writer) self.min_doc_words = min_doc_words self.max_doc_words = max_doc_words self.min_avg_word_length = min_avg_word_length self.max_avg_word_length = max_avg_word_length self.max_symbol_word_ratio = max_symbol_word_ratio self.max_bullet_lines_ratio = max_bullet_lines_ratio self.max_ellipsis_lines_ratio = max_ellipsis_lines_ratio self.max_non_alpha_words_ratio = max_non_alpha_words_ratio self.min_stop_words = min_stop_words self.stop_words = set(STOP_WORDS if stop_words is None else stop_words) self.tokenizer = load_word_tokenizer(language) def filter(self, doc: Document) -> bool | tuple[bool, str]: text = doc.text words = self.tokenizer.word_tokenize(text) n_words = len(words) non_symbol_words = [w for w in words if any((ch not in PUNCTUATION_SET for ch in w))] n_non_symbol_words_words = len(non_symbol_words) if self.min_doc_words and n_non_symbol_words_words < self.min_doc_words: return (False, 'gopher_short_doc') if self.max_doc_words and n_non_symbol_words_words > self.max_doc_words: return (False, 'gopher_long_doc') avg_n_words = np.mean([len(w) for w in non_symbol_words]) if self.min_avg_word_length and avg_n_words < self.min_avg_word_length: return (False, 'gopher_below_avg_threshold') if self.max_avg_word_length and avg_n_words > self.max_avg_word_length: return (False, 'gopher_above_avg_threshold') if self.max_symbol_word_ratio and text.count('#') / n_words > self.max_symbol_word_ratio: return (False, 'gopher_too_many_hashes') if self.max_symbol_word_ratio and (text.count('...') + text.count('…')) / n_words > self.max_symbol_word_ratio: return (False, 'gopher_too_many_ellipsis') lines = text.splitlines() if self.max_bullet_lines_ratio and sum((s.lstrip().startswith('•') or s.lstrip().startswith('-') for s in lines)) / len(lines) > self.max_bullet_lines_ratio: return (False, 'gopher_too_many_bullets') if self.max_ellipsis_lines_ratio and sum((s.rstrip().endswith('...') or s.rstrip().endswith('…') for s in lines)) / len(lines) > self.max_ellipsis_lines_ratio: return (False, 'gopher_too_many_end_ellipsis') if self.max_non_alpha_words_ratio and sum([any((c.isalpha() for c in w)) for w in words]) / n_words < self.max_non_alpha_words_ratio: return (False, 'gopher_below_alpha_threshold') if self.min_stop_words and sum((w in self.stop_words for w in words)) < self.min_stop_words: return (False, 'gopher_enough_stop_words') return True # File: datatrove-main/src/datatrove/pipeline/filters/gopher_repetition_filter.py import re from collections import Counter from datatrove.data import Document from datatrove.pipeline.filters.base_filter import BaseFilter from datatrove.pipeline.writers.disk_base import DiskWriter from datatrove.utils.typeshelper import Languages from datatrove.utils.word_tokenizers import load_word_tokenizer '' def get_n_grams(words: list[str], n: int) -> list[str]: return [' '.join(words[i:i + n]) for i in range(len(words) - n + 1)] def find_duplicates(x: list[str]) -> tuple[int, int]: unique_x = set() duplicate_chars = 0 duplicate_elements = 0 for element in x: if element in unique_x: duplicate_chars += len(element) duplicate_elements += 1 else: unique_x.add(element) return (duplicate_elements, duplicate_chars) def find_top_duplicate(x: list[str]) -> int: counter = Counter() for element in x: counter[element] += 1 top_n_gram = counter.most_common(1)[0] return len(top_n_gram[0]) * top_n_gram[1] def find_all_duplicate(words: list[str], n: int) -> int: n_words = len(words) unique = set() (repeated_chars, idx) = (0, 0) while idx < n_words - n + 1: n_gram = ''.join(words[idx:idx + n]) if n_gram in unique: repeated_chars += len(n_gram) idx += n else: unique.add(n_gram) idx += 1 assert repeated_chars <= len(''.join(words)) return repeated_chars class GopherRepetitionFilter(BaseFilter): name = '👯 Gopher Repetition' def __init__(self, dup_line_frac: float | None=0.3, dup_para_frac: float | None=0.3, dup_line_char_frac: float | None=0.2, dup_para_char_frac: float | None=0.2, top_n_grams: tuple[tuple[int, float]]=((2, 0.2), (3, 0.18), (4, 0.16)), dup_n_grams: tuple[tuple[int, float]]=((5, 0.15), (6, 0.14), (7, 0.13), (8, 0.12), (9, 0.11), (10, 0.1)), exclusion_writer: DiskWriter=None, language: str=Languages.english): super().__init__(exclusion_writer) self.dup_line_frac = dup_line_frac self.dup_para_frac = dup_para_frac self.dup_line_char_frac = dup_line_char_frac self.dup_para_char_frac = dup_para_char_frac self.top_n_grams = top_n_grams self.dup_n_grams = dup_n_grams self.paragraph_exp = re.compile('\\n{2,}') self._line_splitter = re.compile('\n+') self.tokenizer = load_word_tokenizer(language) def filter(self, doc: Document) -> bool | tuple[bool, str]: text = doc.text paragraphs = self.paragraph_exp.split(text.strip()) (paragraphs_duplicates, char_duplicates) = find_duplicates(paragraphs) if self.dup_para_frac and paragraphs_duplicates / len(paragraphs) > self.dup_para_frac: return (False, 'dup_para_frac') if self.dup_para_char_frac and char_duplicates / len(text) > self.dup_para_char_frac: return (False, 'dup_para_char_frac') lines = self._line_splitter.split(text) (line_duplicates, char_duplicates) = find_duplicates(lines) if self.dup_line_frac and line_duplicates / len(lines) > self.dup_line_frac: return (False, 'dup_line_frac') if self.dup_line_char_frac and char_duplicates / len(text) > self.dup_line_char_frac: return (False, 'dup_line_char_frac') words = self.tokenizer.word_tokenize(text) for (n, n_frac) in self.top_n_grams: n_grams = get_n_grams(words, n) if not n_grams: continue top_char_length = find_top_duplicate(n_grams) if top_char_length / len(text) > n_frac: return (False, f'top_{n}_gram') for (n, n_frac) in self.dup_n_grams: n_duplicates_char = find_all_duplicate(words, n) if n_duplicates_char / len(text) > n_frac: return (False, f'duplicated_{n}_n_grams') return True # File: datatrove-main/src/datatrove/pipeline/filters/lambda_filter.py from typing import Callable from datatrove.data import Document from datatrove.pipeline.filters.base_filter import BaseFilter from datatrove.pipeline.writers.disk_base import DiskWriter class LambdaFilter(BaseFilter): name = '👤 Lambda' def __init__(self, filter_function: Callable[[Document], bool], exclusion_writer: DiskWriter=None): super().__init__(exclusion_writer) self.filter_function = filter_function def filter(self, doc: Document) -> bool: return self.filter_function(doc) # File: datatrove-main/src/datatrove/pipeline/filters/language_filter.py from typing import Literal from datatrove.data import Document from datatrove.pipeline.filters.base_filter import BaseFilter from datatrove.pipeline.writers.disk_base import DiskWriter from datatrove.utils.lid import FT176LID, GlotLID class LanguageFilter(BaseFilter): name = '🌍 Language ID' _requires_dependencies = [('fasttext', 'fasttext-wheel'), 'fasteners'] def __init__(self, languages: list[str] | str | None=None, language_threshold: float=0.65, exclusion_writer: DiskWriter=None, backend: Literal['ft176', 'glotlid']='ft176', label_only: bool=False, keep_top_pairs_threshold: float=-1): super().__init__(exclusion_writer) self.language_threshold = language_threshold if isinstance(languages, str): languages = list(languages) self.languages = languages self.backend = backend self.model = FT176LID(languages) if backend == 'ft176' else GlotLID(languages) self.label_only = label_only self.keep_top_pairs_threshold = keep_top_pairs_threshold def filter(self, doc: Document) -> bool: (best_lang_pair, lang_pairs) = self.model.predict(doc) (lang, lang_score) = best_lang_pair if self.backend == 'glotlid': (lang, script) = lang.split('_') doc.metadata['language_script'] = script doc.metadata['language'] = lang doc.metadata['language_score'] = lang_score if self.keep_top_pairs_threshold != -1: for (key, value) in lang_pairs.items(): if value > self.keep_top_pairs_threshold: doc.metadata[f'top_language_{key}_score'] = value return self.label_only or (self.languages and any((score > self.language_threshold for score in lang_pairs.values()))) or (self.languages is None and lang_score > self.language_threshold) # File: datatrove-main/src/datatrove/pipeline/filters/regex_filter.py import re from datatrove.data import Document from datatrove.pipeline.filters.base_filter import BaseFilter from datatrove.pipeline.writers.disk_base import DiskWriter class RegexFilter(BaseFilter): name = '🕵 Regex' def __init__(self, regex_exp: str, exclusion_writer: DiskWriter=None): super().__init__(exclusion_writer) self.regex = re.compile(regex_exp) def filter(self, doc: Document) -> bool: return not self.regex.search(doc.text) # File: datatrove-main/src/datatrove/pipeline/filters/sampler_filter.py from numpy.random import default_rng from datatrove.data import Document from datatrove.pipeline.filters.base_filter import BaseFilter from datatrove.pipeline.writers.disk_base import DiskWriter class SamplerFilter(BaseFilter): name = '🎲 Sampler' def __init__(self, rate: float | None=0.5, seed: int=None, exclusion_writer: DiskWriter=None): """""" super().__init__(exclusion_writer) self.rate = rate self.uniform = default_rng(seed).uniform def filter(self, doc: Document) -> bool | tuple[bool, str]: return self.uniform() < self.rate # File: datatrove-main/src/datatrove/pipeline/filters/unigram_log_probs.py import csv import os import urllib.request import numpy as np from huggingface_hub import cached_assets_path from datatrove.data import Document from datatrove.pipeline.filters.base_filter import BaseFilter from datatrove.pipeline.writers.disk_base import DiskWriter from datatrove.utils.logging import logger from datatrove.utils.typeshelper import Languages from datatrove.utils.word_tokenizers import load_word_tokenizer UNIGRAM_DOWNLOAD = 'https://ai2-s2-research-public.s3-us-west-2.amazonaws.com/lucas/google-1T-unigram/unigram_freq.csv' class UnigramLogProbFilter(BaseFilter): name = '🧑\u200d🍳 Unigram log-prob filter' def __init__(self, logprobs_threshold: float=-10, exclusion_writer: DiskWriter=None, language: str=Languages.english): super().__init__(exclusion_writer) self.logprobs_threshold = logprobs_threshold self.unigram_frequencies = self.get_frequencies() self.tokenizer = load_word_tokenizer(language) def get_frequencies(self): download_dir = cached_assets_path(library_name='datatrove', namespace='filters', subfolder='unigram_logprob_filter') unigram_freq_file = os.path.join(download_dir, 'unigram_freq.csv') if not os.path.isfile(unigram_freq_file): logger.info('⬇️ Downloading unigram-frequencies ...') urllib.request.urlretrieve(UNIGRAM_DOWNLOAD, unigram_freq_file) words = [] counts = [] with open(unigram_freq_file, encoding='utf-8', newline='') as f: csv_reader = csv.DictReader(f) for row in csv_reader: words.append(row['word']) counts.append(int(row['count'])) total_count = sum(counts) return {word: count / total_count for (word, count) in zip(words, counts)} def get_logprob(self, doc): words = self.tokenizer.word_tokenize(doc.text) freqs = [self.unigram_frequencies.get(word.lower(), 1e-09) for word in words] if len(freqs) == 0: return 0 return sum([np.log(f) for f in freqs]) / len(freqs) def filter(self, doc: Document) -> bool: return self.get_logprob(doc) > self.logprobs_threshold # File: datatrove-main/src/datatrove/pipeline/filters/url_filter.py import os import re import tarfile from typing import Iterable from huggingface_hub import cached_assets_path from datatrove.data import Document from datatrove.io import safely_create_file from datatrove.utils._import_utils import ASSETS_PATH from datatrove.utils.logging import logger from ..writers.disk_base import DiskWriter from .base_filter import BaseFilter normalizer = re.compile('[^a-zA-Z0-9]+') def normalize(text, replace=''): return normalizer.sub(replace, text).lower() def parse_list(line, do_normalize=True): return {normalize(x) if do_normalize else x.strip() for x in line if x[0] != '#'} def get_list(abs_path: str, file_name: str, extra: set, do_normalize: bool=True): with open(os.path.join(abs_path, file_name)) as f: return parse_list(f, do_normalize).union(extra) class URLFilter(BaseFilter): name = '😈 Url-filter' _requires_dependencies = ['tldextract', 'fasteners', ('ahocorasick', 'pyahocorasick')] def __init__(self, soft_word_threshold: int=2, extra_domains: Iterable=None, extra_urls: Iterable=None, banned_words: Iterable=None, banned_subwords: Iterable=None, soft_banned_words: Iterable=None, use_integrated_lists: bool=True, exclusion_writer: DiskWriter=None): import ahocorasick from tldextract import TLDExtract super().__init__(exclusion_writer) self.soft_word_threshold = soft_word_threshold self.block_listed_domains = parse_list(extra_domains, do_normalize=False) if extra_domains else set() self.block_listed_url = parse_list(extra_urls, do_normalize=False) if extra_urls else set() self.banned_words = parse_list(banned_words) if banned_words else set() self.banned_subwords = parse_list(banned_subwords) if banned_subwords else set() self.soft_banned_words = parse_list(soft_banned_words) if soft_banned_words else set() self.use_integrated_lists = use_integrated_lists self._downloaded = False self.tldextractor = TLDExtract() self.banned_subwords_automaton = ahocorasick.Automaton(ahocorasick.STORE_INTS) for word in self.banned_subwords: self.banned_subwords_automaton.add_word(word, len(self.banned_subwords_automaton)) if not self.use_integrated_lists: self.banned_subwords_automaton.make_automaton() def download_data(self): if self._downloaded or not self.use_integrated_lists: return download_dir = cached_assets_path(library_name='datatrove', namespace='filters', subfolder='url_filter') file_to_lock = os.path.join(download_dir, 'url_filterblacklists.tar.gz') def do_extract(): logger.info('💥 Extracting url filter blacklists...') with tarfile.open(os.path.join(ASSETS_PATH, 'url_filterblacklists.tar.gz'), 'r:gz') as tar: tar.extractall(download_dir) logger.info('💥 Extracted url filter blacklists.') safely_create_file(file_to_lock, do_extract) self.block_listed_domains = get_list(download_dir, 'adult/domains', self.block_listed_domains, do_normalize=False) self.block_listed_url = get_list(download_dir, 'adult/urls', self.block_listed_url, do_normalize=False) self.banned_words = get_list(ASSETS_PATH, 'banned_words.txt', self.banned_words) self.banned_subwords = get_list(ASSETS_PATH, 'banned_subwords.txt', self.banned_subwords) self.soft_banned_words = get_list(ASSETS_PATH, 'soft_banned_words.txt', self.soft_banned_words) for word in self.banned_subwords: self.banned_subwords_automaton.add_word(word, len(self.banned_subwords_automaton)) self.banned_subwords_automaton.make_automaton() self._downloaded = True def filter(self, document: Document) -> bool | tuple[bool, str]: self.download_data() url = document.metadata.get('url') assert url, 'Document does not have url in its metadata' url_info = self.tldextractor(url) if url_info.registered_domain in self.block_listed_domains: return (False, 'domain') if url_info.fqdn in self.block_listed_domains: return (False, 'subdomain') if url in self.block_listed_url: return (False, 'url') url_words = set(normalizer.split(url)) if any((word in url_words for word in self.banned_words)): return (False, 'hard_blacklisted') nb_soft_words = sum([word in url_words for word in self.soft_banned_words]) if nb_soft_words >= self.soft_word_threshold: return (False, 'soft_blacklisted') normalized_space = normalize(url) if self.banned_subwords and next(self.banned_subwords_automaton.iter(normalized_space), False): return (False, 'blacklisted_subword') return True # File: datatrove-main/src/datatrove/pipeline/formatters/base.py from abc import ABC, abstractmethod from datatrove.data import DocumentsPipeline from datatrove.pipeline.base import PipelineStep from datatrove.utils.typeshelper import StatHints class BaseFormatter(PipelineStep, ABC): type = '✂️ - FORMAT' def __init__(self): super().__init__() @abstractmethod def format(self, text: str) -> str: return text def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1) -> DocumentsPipeline: for doc in data: self.stat_update(StatHints.total) with self.track_time(): doc.text = self.format(doc.text) yield doc # File: datatrove-main/src/datatrove/pipeline/formatters/pii.py import ipaddress import re from functools import partial from typing import Callable from datatrove.pipeline.formatters.base import BaseFormatter class PIIReplacer: def __init__(self, regex: str, replacements: tuple[str, ...] | str, validator: Callable[[str], bool] | None=None): self.regex: re.Pattern = re.compile(regex) self.replacements = replacements if type(replacements) is tuple else tuple(replacements) if not isinstance(replacements, str) else (replacements,) self.validator = validator self._replace_i = 0 def replace(self, text: str): def get_replacement(matchobj): if self.validator and (not self.validator(matchobj.group(0))): return matchobj.group(0) replacement = self.replacements[self._replace_i] self._replace_i = (self._replace_i + 1) % len(self.replacements) return replacement return self.regex.sub(get_replacement, text) def public_ip_validator(ip, public_only: bool=True) -> bool: try: ip = ipaddress.ip_address(ip) return not public_only or ip.is_global except ValueError: return False class PIIFormatter(BaseFormatter): name = '📞 PII' def __init__(self, remove_emails: bool=True, remove_ips: bool=True, only_remove_public_ips: bool=True, email_replacement: tuple[str, ...] | str=('[email protected]', '[email protected]'), ip_replacement: tuple[str, ...] | str=('22.214.171.124', '126.96.36.199', '188.8.131.52', '184.108.40.206', '220.127.116.11', '18.104.22.168')): super().__init__() self.remove_emails = remove_emails self.remove_ips = remove_ips self.emails_replacer = PIIReplacer("\\b[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[A-Za-z0-9-]*[A-Za-z0-9]:)])", email_replacement) self.ip_replacer = PIIReplacer('(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)', validator=partial(public_ip_validator, public_only=only_remove_public_ips), replacements=ip_replacement) def format(self, text: str) -> str: if self.remove_emails: text = self.emails_replacer.replace(text) if self.remove_ips: text = self.ip_replacer.replace(text) return text # File: datatrove-main/src/datatrove/pipeline/formatters/symbol_lines_remover.py from ...utils.text import PUNCTUATION_SET from .base import BaseFormatter class SymbolLinesFormatter(BaseFormatter): name = ' ⚞ Symbol Lines Remover' def __init__(self, replace_char: str=''): super().__init__() self.replace_char = replace_char def format(self, text: str) -> str: formatted = [] in_removed_span = False for line in text.splitlines(): chars_line = line.strip() != '' and all((c in PUNCTUATION_SET or c == ' ' for c in line)) if chars_line and (not in_removed_span): if self.replace_char: formatted.append(self.replace_char) in_removed_span = True elif not chars_line: formatted.append(line) in_removed_span = False return '\n'.join(formatted) # File: datatrove-main/src/datatrove/pipeline/readers/base.py import random from abc import abstractmethod from types import MethodType from typing import Callable from tqdm import tqdm from datatrove.data import Document, DocumentsPipeline from datatrove.io import DataFileLike, DataFolderLike, get_datafolder, get_shard_from_paths_file from datatrove.pipeline.base import PipelineStep from datatrove.utils.logging import logger class BaseReader(PipelineStep): type = '📖 - READER' def __init__(self, limit: int=-1, skip: int=0, adapter: Callable=None, text_key: str='text', id_key: str='id', default_metadata: dict=None): super().__init__() self.limit = limit self.skip = skip self.text_key = text_key self.id_key = id_key self.adapter = MethodType(adapter, self) if adapter else self._default_adapter self._empty_warning = False self.default_metadata = default_metadata def _default_adapter(self, data: dict, path: str, id_in_file: int | str): return {'text': data.pop(self.text_key, ''), 'id': data.pop(self.id_key, f'{path}/{id_in_file}'), 'media': data.pop('media', []), 'metadata': data.pop('metadata', {}) | data} def get_document_from_dict(self, data: dict, source_file: str, id_in_file: int | str): parsed_data = self.adapter(data, source_file, id_in_file) if not parsed_data.get('text', None): if not self._empty_warning: self._empty_warning = True logger.warning(f'Found document without text, skipping. Is your `text_key` ("{self.text_key}") correct? Available keys: {list(data.keys())}') return None document = Document(**parsed_data) if self.default_metadata: document.metadata = self.default_metadata | document.metadata return document @abstractmethod def run(self, data: DocumentsPipeline=None, rank: int=0, world_size: int=1) -> DocumentsPipeline: raise NotImplementedError class BaseDiskReader(BaseReader): type = '📖 - READER' def __init__(self, data_folder: DataFolderLike, paths_file: DataFileLike | None=None, limit: int=-1, skip: int=0, file_progress: bool=False, doc_progress: bool=False, adapter: Callable=None, text_key: str='text', id_key: str='id', default_metadata: dict=None, recursive: bool=True, glob_pattern: str | None=None, shuffle_files: bool=False): super().__init__(limit, skip, adapter, text_key, id_key, default_metadata) self.data_folder = get_datafolder(data_folder) self.paths_file = paths_file self.recursive = recursive self.glob_pattern = glob_pattern self.shuffle_files = shuffle_files self.file_progress = file_progress self.doc_progress = doc_progress def get_document_from_dict(self, data: dict, source_file: str, id_in_file: int): document = super().get_document_from_dict(data, source_file, id_in_file) if document: document.metadata.setdefault('file_path', self.data_folder.resolve_paths(source_file)) return document @abstractmethod def read_file(self, filepath: str) -> DocumentsPipeline: raise NotImplementedError def read_files_shard(self, shard: list[str]) -> DocumentsPipeline: li = 0 skipped = 0 with tqdm(total=self.limit if self.limit != -1 else None, desc='Document progress', unit='doc', disable=not self.doc_progress) as doc_pbar, tqdm(total=len(shard), desc='File progress', unit='file', disable=not self.file_progress) as file_pbar: for (i, filepath) in enumerate(shard): self.stat_update('input_files') logger.info(f'Reading input file {filepath}, {i + 1}/{len(shard)}') di = 0 ndocs = 0 for (di, document) in enumerate(self.read_file(filepath)): if skipped < self.skip: skipped += 1 continue if self.limit != -1 and li >= self.limit: break yield document doc_pbar.update() li += 1 ndocs += 1 file_pbar.update() self.stat_update('documents', value=ndocs, unit='input_file') if self.limit != -1 and li >= self.limit: break def run(self, data: DocumentsPipeline=None, rank: int=0, world_size: int=1) -> DocumentsPipeline: if data: yield from data files_shard = self.data_folder.get_shard(rank, world_size, recursive=self.recursive, glob_pattern=self.glob_pattern) if not self.paths_file else list(get_shard_from_paths_file(self.paths_file, rank, world_size)) if len(files_shard) == 0: if rank == 0: raise RuntimeError(f'No files found on {self.data_folder.path}!') logger.warning(f'No files found on {self.data_folder.path} for rank={rank!r}') if self.shuffle_files: random.shuffle(files_shard) for doc in self.read_files_shard(files_shard): self.update_doc_stats(doc) yield doc # File: datatrove-main/src/datatrove/pipeline/readers/csv.py import csv from typing import Callable, Literal from datatrove.io import DataFileLike, DataFolderLike from datatrove.pipeline.readers.base import BaseDiskReader class CsvReader(BaseDiskReader): name = '🔢 Csv' def __init__(self, data_folder: DataFolderLike, paths_file: DataFileLike | None=None, compression: Literal['infer', 'gzip', 'zstd'] | None='infer', limit: int=-1, skip: int=0, file_progress: bool=False, doc_progress: bool=False, adapter: Callable=None, text_key: str='text', id_key: str='id', default_metadata: dict=None, recursive: bool=True, glob_pattern: str | None=None, shuffle_files: bool=False): super().__init__(data_folder, paths_file, limit, skip, file_progress, doc_progress, adapter, text_key, id_key, default_metadata, recursive, glob_pattern, shuffle_files) self.compression = compression self.empty_warning = False def read_file(self, filepath: str): with self.data_folder.open(filepath, 'r', compression=self.compression) as f: csv_reader = csv.DictReader(f) for (di, d) in enumerate(csv_reader): with self.track_time(): document = self.get_document_from_dict(d, filepath, di) if not document: continue yield document CSVReader = CsvReader # File: datatrove-main/src/datatrove/pipeline/readers/huggingface.py import copy from typing import Callable from loguru import logger from tqdm import tqdm from datatrove.data import DocumentsPipeline from datatrove.pipeline.readers.base import BaseReader class HuggingFaceDatasetReader(BaseReader): name = '🤗 HuggingFace' _requires_dependencies = ['datasets'] def __init__(self, dataset: str, dataset_options: dict | None=None, streaming: bool=False, limit: int=-1, skip: int=0, batch_size: int=1000, doc_progress: bool=False, adapter: Callable=None, text_key: str='text', id_key: str='id', default_metadata: dict=None, shuffle_files: bool=False): super().__init__(limit, skip, adapter, text_key, id_key, default_metadata) self.dataset = dataset self.dataset_options = dataset_options or {} self.batch_size = batch_size self.doc_progress = doc_progress self.streaming = streaming self.shuffle_files = shuffle_files def get_document_from_dict(self, data: dict, source: str, id_in_file: int | str): document = super().get_document_from_dict(data, source, id_in_file) if document: document.metadata.setdefault('dataset', source) return document def _get_dataset_shard(self, dst, rank: int, world_size: int): from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node if isinstance(dst, Dataset): return dst.shard(world_size, rank, contiguous=True) elif isinstance(dst, IterableDataset) and dst.n_shards > 1: if rank >= dst.n_shards: logger.warning(f'Requested shard {rank} of a streaming dataset, but it only has {dst.n_shards} shards.') return None ex_iterable = dst._ex_iterable.shard_data_sources(rank, world_size) return IterableDataset(ex_iterable=ex_iterable, info=dst._info.copy(), split=dst._split, formatting=dst._formatting, shuffling=copy.deepcopy(dst._shuffling), distributed=copy.deepcopy(dst._distributed), token_per_repo_id=dst._token_per_repo_id) else: return split_dataset_by_node(dst, rank, world_size) def run(self, data: DocumentsPipeline=None, rank: int=0, world_size: int=1) -> DocumentsPipeline: from datasets import load_dataset if data: yield from data ds = load_dataset(self.dataset, **self.dataset_options, streaming=self.streaming) if self.shuffle_files: if not self.streaming: ds = ds.shuffle(seed=42) else: ds = ds.shuffle(seed=42, buffer_size=1000) if isinstance(ds, dict): raise ValueError(f"You forgot to specify the split of the dataset. Update your dataset_options to include 'split'. Available splits: {list(ds.keys())}") shard = self._get_dataset_shard(ds, rank, world_size) if not shard: return with tqdm(total=self.limit if self.limit != -1 else None, disable=not self.doc_progress) as pbar: li = 0 for batch in shard.iter(self.batch_size): if self.limit != -1 and li >= self.limit: break documents = [] with self.track_time('batch'): for line in (dict(zip(batch, t)) for t in zip(*batch.values())): if self.limit != -1 and li >= self.limit: break document = self.get_document_from_dict(line, self.dataset, f'{rank:05d}/{li}') if not document: continue documents.append(document) self.update_doc_stats(document) self.stat_update('documents') li += 1 pbar.update() yield from documents # File: datatrove-main/src/datatrove/pipeline/readers/ipc.py from typing import Callable from datatrove.io import DataFileLike, DataFolderLike from datatrove.pipeline.readers.base import BaseDiskReader class IpcReader(BaseDiskReader): name = '🪶 Ipc' _requires_dependencies = ['pyarrow'] def __init__(self, data_folder: DataFolderLike, paths_file: DataFileLike | None=None, limit: int=-1, skip: int=0, stream: bool=False, file_progress: bool=False, doc_progress: bool=False, adapter: Callable=None, text_key: str='text', id_key: str='id', default_metadata: dict=None, recursive: bool=True, glob_pattern: str | None=None, shuffle_files: bool=False): super().__init__(data_folder, paths_file, limit, skip, file_progress, doc_progress, adapter, text_key, id_key, default_metadata, recursive, glob_pattern, shuffle_files) self.stream = stream def _iter_file_batches(self, filepath: str): import pyarrow as pa with self.data_folder.open(filepath, 'rb') as f: with pa.ipc.open_file(f) as ipc_reader: for i in range(ipc_reader.num_record_batches): yield ipc_reader.get_batch(i) def _iter_stream_batches(self, filepath: str): import pyarrow as pa with self.data_folder.open(filepath, 'rb') as f: with pa.ipc.open_stream(f) as ipc_stream_reader: for batch in ipc_stream_reader: yield batch def read_file(self, filepath: str): batch_iter = self._iter_file_batches(filepath) if not self.stream else self._iter_stream_batches(filepath) li = 0 for batch in batch_iter: documents = [] with self.track_time('batch'): for line in batch.to_pylist(): document = self.get_document_from_dict(line, filepath, li) if not document: continue documents.append(document) li += 1 yield from documents # File: datatrove-main/src/datatrove/pipeline/readers/jsonl.py from typing import Callable, Literal from datatrove.io import DataFileLike, DataFolderLike from datatrove.pipeline.readers.base import BaseDiskReader from datatrove.utils.logging import logger class JsonlReader(BaseDiskReader): name = '🐿 Jsonl' _requires_dependencies = ['orjson'] def __init__(self, data_folder: DataFolderLike, paths_file: DataFileLike | None=None, compression: Literal['infer', 'gzip', 'zstd'] | None='infer', limit: int=-1, skip: int=0, file_progress: bool=False, doc_progress: bool=False, adapter: Callable=None, text_key: str='text', id_key: str='id', default_metadata: dict=None, recursive: bool=True, glob_pattern: str | None=None, shuffle_files: bool=False): super().__init__(data_folder, paths_file, limit, skip, file_progress, doc_progress, adapter, text_key, id_key, default_metadata, recursive, glob_pattern, shuffle_files) self.compression = compression def read_file(self, filepath: str): import orjson from orjson import JSONDecodeError with self.data_folder.open(filepath, 'r', compression=self.compression) as f: try: for (li, line) in enumerate(f): with self.track_time(): try: document = self.get_document_from_dict(orjson.loads(line), filepath, li) if not document: continue except (EOFError, JSONDecodeError) as e: logger.warning(f'Error when reading `{filepath}`: {e}') continue yield document except UnicodeDecodeError as e: logger.warning(f'File `{filepath}` may be corrupted: raised UnicodeDecodeError ({e})') # File: datatrove-main/src/datatrove/pipeline/readers/parquet.py from typing import Callable from datatrove.io import DataFileLike, DataFolderLike from datatrove.pipeline.readers.base import BaseDiskReader class ParquetReader(BaseDiskReader): name = '📒 Parquet' _requires_dependencies = ['pyarrow'] def __init__(self, data_folder: DataFolderLike, paths_file: DataFileLike | None=None, limit: int=-1, skip: int=0, batch_size: int=1000, read_metadata: bool=True, file_progress: bool=False, doc_progress: bool=False, adapter: Callable=None, text_key: str='text', id_key: str='id', default_metadata: dict=None, recursive: bool=True, glob_pattern: str | None=None, shuffle_files: bool=False): super().__init__(data_folder, paths_file, limit, skip, file_progress, doc_progress, adapter, text_key, id_key, default_metadata, recursive, glob_pattern, shuffle_files) self.batch_size = batch_size self.read_metadata = read_metadata def read_file(self, filepath: str): import pyarrow.parquet as pq with self.data_folder.open(filepath, 'rb') as f: with pq.ParquetFile(f) as pqf: li = 0 columns = [self.text_key, self.id_key] if not self.read_metadata else None for batch in pqf.iter_batches(batch_size=self.batch_size, columns=columns): documents = [] with self.track_time('batch'): for line in batch.to_pylist(): document = self.get_document_from_dict(line, filepath, li) if not document: continue documents.append(document) li += 1 yield from documents # File: datatrove-main/src/datatrove/pipeline/readers/warc.py from typing import TYPE_CHECKING, Callable, Literal from datatrove.io import DataFileLike, DataFolderLike from datatrove.pipeline.readers.base import BaseDiskReader if TYPE_CHECKING: from warcio.recordloader import ArcWarcRecord class WarcReader(BaseDiskReader): name = '🕷 Warc' _requires_dependencies = ['warcio', ('cchardet', 'faust-cchardet'), ('magic', 'python-magic')] def __init__(self, data_folder: DataFolderLike, paths_file: DataFileLike | None=None, compression: Literal['infer', 'gzip', 'zstd'] | None='infer', limit: int=-1, skip: int=0, file_progress: bool=False, doc_progress: bool=False, adapter: Callable=None, text_key: str='text', id_key: str='id', default_metadata: dict=None, recursive: bool=True, glob_pattern: str | None=None, shuffle_files: bool=False): self.compression = compression super().__init__(data_folder, paths_file, limit, skip, file_progress, doc_progress, adapter, text_key, id_key, default_metadata, recursive, glob_pattern, shuffle_files) def read_file(self, filepath: str): from warcio.archiveiterator import ArchiveIterator with self.data_folder.open(filepath, 'rb', compression=self.compression) as f: for (ri, record) in enumerate(ArchiveIterator(f)): with self.track_time(): extracted_data = process_record(record) if not extracted_data: continue document = self.get_document_from_dict(extracted_data, filepath, ri) if not document: continue yield document def process_record(record: 'ArcWarcRecord') -> dict | None: import cchardet import magic if record.rec_type != 'response' and record.rec_type != 'conversion': return mime_type = record.rec_headers.get('WARC-Identified-Payload-Type', None) if mime_type is not None and (mime_type != 'text/html' and (record.rec_type != 'conversion' or mime_type != 'text/plain')): return content_bytes = record.content_stream().read() if mime_type is None: mime_type = magic.from_buffer(content_bytes, mime=True) if mime_type != 'text/html' and (record.rec_type != 'conversion' or mime_type != 'text/plain'): return charset = 'UTF-8' try: html = content_bytes.decode(charset) except UnicodeDecodeError: encoding_det = cchardet.detect(content_bytes)['encoding'] if not encoding_det or encoding_det == charset: return charset = encoding_det try: html = content_bytes.decode(charset) except (UnicodeDecodeError, LookupError): return id_ = record.rec_headers['WARC-Record-ID'] url = record.rec_headers.get('WARC-Target-URI', None) date = record.rec_headers.get('WARC-Date', None) if not url: url = dict(record.rec_headers.headers)['uri'] if not date: date = dict(record.rec_headers.headers)['archive-date'] return {'text': html, 'id': id_, 'url': url, 'date': date} # File: datatrove-main/src/datatrove/pipeline/stats/__init__.py from datatrove.pipeline.stats.config import DEFAULT_TOP_K_CONFIG, GROUP, STAT_TYPE, TopKConfig from datatrove.pipeline.stats.contamination_stats import WordsContaminationStats from datatrove.pipeline.stats.doc_stats import DocStats from datatrove.pipeline.stats.lang_stats import LangStats from datatrove.pipeline.stats.line_stats import LineStats from datatrove.pipeline.stats.merger import STATS_MERGED_NAME, StatsMerger from datatrove.pipeline.stats.paragraph_stats import ParagraphStats from datatrove.pipeline.stats.perplexity_stats import CCNetPerplexityStats from datatrove.pipeline.stats.sentence_stats import SentenceStats from datatrove.pipeline.stats.token_stats import TokenStats from datatrove.pipeline.stats.word_stats import WordStats # File: datatrove-main/src/datatrove/pipeline/stats/base.py import heapq import json from abc import abstractmethod from collections import defaultdict from typing import get_args from loguru import logger from datatrove.data import Document, DocumentsPipeline from datatrove.io import DataFolderLike, get_datafolder from datatrove.pipeline.base import PipelineStep from datatrove.pipeline.stats.config import DEFAULT_TOP_K_CONFIG, GROUP, STAT_TYPE, TopKConfig from datatrove.utils.stats import MetricStatsDict class BaseStats(PipelineStep): type = '📊 - STATS' name = '👑 Summary stats' _requires_dependencies = ['tldextract'] def __init__(self, output_folder: DataFolderLike, groups_to_compute: list[GROUP] | None=None, histogram_round_digits: int=3, top_k_config: TopKConfig=DEFAULT_TOP_K_CONFIG) -> None: from tldextract import TLDExtract super().__init__() self.output_folder = get_datafolder(output_folder) self.groups = groups_to_compute or list(get_args(GROUP)) self.histogram_round_digits = histogram_round_digits self.top_k_cfg = top_k_config self.tld_extractor = TLDExtract() @abstractmethod def extract_stats(self, doc: Document) -> dict[str, int | float]: raise NotImplementedError() def get_kv(self, doc: Document, value: STAT_TYPE, group_name: GROUP) -> tuple[str, STAT_TYPE | dict[str, STAT_TYPE]]: if group_name == 'histogram': return (str(round(value, self.histogram_round_digits)), {'': 1, 'chars': len(doc.text), **({'tokens': doc.metadata['token_count']} if 'token_count' in doc.metadata else {})}) elif group_name == 'summary': return ('summary', value) elif group_name == 'fqdn': fqdn = doc.metadata.get('fqdn') if fqdn is None: fqdn = self.tld_extractor.extract_str(doc.metadata['url']).fqdn doc.metadata['fqdn'] = fqdn return (fqdn, value) elif group_name == 'suffix': suffix = doc.metadata.get('suffix') if suffix is None: suffix = self.tld_extractor.extract_str(doc.metadata['url']).suffix doc.metadata['suffix'] = suffix return (suffix, value) else: raise ValueError(f'Unknown group name: {group_name}') def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1) -> DocumentsPipeline: groups_dicts: dict[GROUP, dict[str, MetricStatsDict]] = {group: defaultdict(MetricStatsDict) for group in self.groups} for doc in data: with self.track_time(): try: doc_stats = self.extract_stats(doc) except Exception as e: logger.error(f'Error while extracting stats from document {doc.id}', exc_info=e) raise e for (group, counters) in groups_dicts.items(): for (stat, value) in doc_stats.items(): (key, value) = self.get_kv(doc, value, group) if not isinstance(value, dict): counters[stat][key] += value else: for (suffix, val) in value.items(): stat_name = stat if not suffix else f'{stat}__{suffix}' counters[stat_name][key] += val doc.metadata.update(doc_stats) yield doc for (group, stats_dict) in groups_dicts.items(): group_top_k_keys = None for (stat_name, stat_values) in stats_dict.items(): if group in self.top_k_cfg.top_k_groups: if group_top_k_keys is None: group_top_k_keys = heapq.nlargest(self.top_k_cfg.top_k, stat_values, key=lambda x: stat_values[x].n) stat_values = MetricStatsDict(init={s: stat_values[s] for s in group_top_k_keys}) with self.output_folder.open(f'{group}/{stat_name}/{rank:05d}.json', 'wt') as f: json.dump(stat_values.to_dict(), f) del groups_dicts # File: datatrove-main/src/datatrove/pipeline/stats/config.py from dataclasses import dataclass from typing import Literal GROUP = Literal['summary', 'histogram', 'fqdn', 'suffix'] @dataclass(frozen=True) class TopKConfig: top_k_groups: list[Literal['fqdn', 'suffix']] top_k: int DEFAULT_TOP_K_CONFIG = TopKConfig(top_k_groups=['fqdn', 'suffix'], top_k=100000) STAT_TYPE = int | float # File: datatrove-main/src/datatrove/pipeline/stats/contamination_stats.py from typing import get_args from datatrove.data import Document from datatrove.io import DataFolderLike from datatrove.pipeline.stats.base import BaseStats from datatrove.pipeline.stats.config import DEFAULT_TOP_K_CONFIG, GROUP, TopKConfig from datatrove.utils.text import TextNormConfig, simplify_text from datatrove.utils.typeshelper import Languages from datatrove.utils.word_tokenizers import load_word_tokenizer class WordsContaminationStats(BaseStats): name = '😷 Words contamination' def __init__(self, output_folder: DataFolderLike, words: list[str], norm_config: TextNormConfig=TextNormConfig(), language: str=Languages.english, groups_to_compute: list[GROUP]=list(get_args(GROUP)), histogram_round_digits: int=3, top_k_config: TopKConfig=DEFAULT_TOP_K_CONFIG) -> None: super().__init__(output_folder, groups_to_compute, histogram_round_digits, top_k_config=top_k_config) if len(words) == 0: raise ValueError('At least one word must be provided') self.norm_config = norm_config self.language = language self.words = words def extract_stats(self, doc: Document) -> dict[str, int | float]: word_tokenizer = load_word_tokenizer(self.language) doc_words = word_tokenizer.word_tokenize(simplify_text(doc.text, self.norm_config)) return {f'words_contamination_{self.words[0]}': sum([1 for word in doc_words if word in self.words]) / len(doc_words)} # File: datatrove-main/src/datatrove/pipeline/stats/doc_stats.py import re from typing import get_args from datatrove.data import Document from datatrove.io import DataFolderLike from datatrove.pipeline.stats.base import BaseStats from datatrove.pipeline.stats.config import DEFAULT_TOP_K_CONFIG, GROUP, TopKConfig from datatrove.utils.text import PUNCTUATION ELIPSIS = ['...', '…'] class DocStats(BaseStats): name = '📜 Doc stats' def __init__(self, output_folder: DataFolderLike, groups_to_compute: list[GROUP]=list(get_args(GROUP)), histogram_round_digits: int=3, top_k_config: TopKConfig=DEFAULT_TOP_K_CONFIG) -> None: super().__init__(output_folder, groups_to_compute, histogram_round_digits, top_k_config) self.elipsis_regex = re.compile('|'.join([f'(?:{re.escape(elipsis)})' for elipsis in ELIPSIS])) self.punc_regex = re.compile('|'.join([f'(?:{re.escape(punc)})' for punc in PUNCTUATION])) def extract_stats(self, doc: Document) -> dict[str, int | float]: return {'length': len(doc.text), 'white_space_ratio': sum([1 for c in doc.text if c.isspace()]) / len(doc.text), 'non_alpha_digit_ratio': sum([1 for c in doc.text if not c.isalpha() and (not c.isdigit())]) / len(doc.text), 'digit_ratio': sum([1 for c in doc.text if c.isdigit()]) / len(doc.text), 'uppercase_ratio': sum([1 for c in doc.text if c.isupper()]) / len(doc.text), 'elipsis_ratio': sum((len(elipsis) for elipsis in self.elipsis_regex.findall(doc.text))) / len(doc.text), 'punctuation_ratio': sum((len(punc) for punc in self.punc_regex.findall(doc.text))) / len(doc.text)} # File: datatrove-main/src/datatrove/pipeline/stats/lang_stats.py from typing import get_args from datatrove.data import Document from datatrove.io import DataFolderLike from datatrove.pipeline.stats.base import BaseStats from datatrove.pipeline.stats.config import DEFAULT_TOP_K_CONFIG, GROUP, TopKConfig from datatrove.utils.lid import FT176LID class LangStats(BaseStats): name = '🎤 Language stats' def __init__(self, output_folder: DataFolderLike, language: str, groups_to_compute: list[GROUP]=list(get_args(GROUP)), histogram_round_digits: int=3, top_k_config: TopKConfig=DEFAULT_TOP_K_CONFIG) -> None: super().__init__(output_folder, groups_to_compute, histogram_round_digits, top_k_config) self.fasttext = FT176LID([language]) self.language = language def extract_stats(self, doc: Document) -> dict[str, int | float]: language_score = 0 if doc.metadata.get('language') == self.language and 'language_score' in doc.metadata: language_score = doc.metadata['language_score'] else: language_score = self.fasttext.predict(doc)[1][self.language] return {f'fasttext_{self.language}': language_score} # File: datatrove-main/src/datatrove/pipeline/stats/line_stats.py from typing import get_args from datatrove.data import Document from datatrove.io import DataFolderLike from datatrove.pipeline.filters.c4_filters import END_PUNCTUATION from datatrove.pipeline.filters.gopher_repetition_filter import find_duplicates from datatrove.pipeline.stats.base import BaseStats from datatrove.pipeline.stats.config import DEFAULT_TOP_K_CONFIG, GROUP, TopKConfig def get_max_chars_per_line_ratio(lines, chars: int) -> float: return sum([1 for line in lines if len(line) <= chars]) / len(lines) def get_min_chars_per_line_ratio(lines, chars: int) -> float: return sum([1 for line in lines if len(line) >= chars]) / len(lines) def is_bullet_line(line: str): if len(line.strip()) == 0: return False return line.strip()[0] in '-*•' class LineStats(BaseStats): name = '🎼 Line stats' def __init__(self, output_folder: DataFolderLike, max_k_chars_per_line_tresholds: list[int] | None=None, min_k_chars_per_line_thresholds: list[int] | None=None, groups_to_compute: list[GROUP]=list(get_args(GROUP)), ignore_empty_lines: bool=False, histogram_round_digits: int=3, top_k_config: TopKConfig=DEFAULT_TOP_K_CONFIG) -> None: super().__init__(output_folder, groups_to_compute, histogram_round_digits, top_k_config) self.short_max_chars = max_k_chars_per_line_tresholds if max_k_chars_per_line_tresholds is not None else [10, 30] self.long_max_chars = min_k_chars_per_line_thresholds if min_k_chars_per_line_thresholds is not None else [2000, 10000] self.ignore_empty_lines = ignore_empty_lines def extract_stats(self, doc: Document): lines: list[str] = doc.metadata.get('lines') or doc.text.split('\n') n_lines = len(lines) lines = [line for line in lines if len(line.strip()) > 0] if self.ignore_empty_lines else lines (line_dups, char_dups) = find_duplicates(lines) return {'n_lines': n_lines, 'avg_line_length': sum([len(line) for line in lines]) / len(lines), **{f'short_line_ratio_chars_{chars}': get_max_chars_per_line_ratio(lines, chars) for chars in self.short_max_chars}, **{f'long_line_ratio_chars_{chars}': get_min_chars_per_line_ratio(lines, chars) for chars in self.long_max_chars}, 'lines_ending_with_terminal_mark_ratio': sum((1 for line in lines if line.endswith(END_PUNCTUATION))) / len(lines), 'bullet_point_lines_ratio': sum((1 for line in lines if is_bullet_line(line))) / len(lines), 'line_duplicates': line_dups / len(lines), 'line_char_duplicates': char_dups / sum((len(line) for line in lines))} # File: datatrove-main/src/datatrove/pipeline/stats/merger.py import heapq import json from pathlib import Path from loguru import logger from tqdm import tqdm from datatrove.data import DocumentsPipeline from datatrove.io import DataFolderLike, get_datafolder from datatrove.pipeline.base import PipelineStep from datatrove.pipeline.stats.config import DEFAULT_TOP_K_CONFIG, TopKConfig from datatrove.utils.stats import MetricStats, MetricStatsDict STATS_MERGED_NAME = 'metric.json' class StatsMerger(PipelineStep): type = '📊 - STATS' name = '🔗 Merging stats' def __init__(self, input_folder: DataFolderLike, output_folder: DataFolderLike, remove_input: bool=False, top_k_config: TopKConfig=DEFAULT_TOP_K_CONFIG) -> None: super().__init__() self.input_folder = get_datafolder(input_folder) self.output_folder = get_datafolder(output_folder) self.remove_input = remove_input self.top_k_config = top_k_config def get_leaf_non_empty_folders(self): return sorted([path for (path, folders, files) in self.input_folder.walk('') if not folders and files]) def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1) -> DocumentsPipeline: folders_shard = self.get_leaf_non_empty_folders()[rank::world_size] logger.info(f'Merging {len(folders_shard)} stat folders') with self.track_time(): for folder in tqdm(folders_shard): input_files = self.input_folder.glob(f'{folder}/[0-9][0-9][0-9][0-9][0-9].json') logger.info(f'Processing folder {folder} with {len(input_files)} files') stat = MetricStatsDict() for file in tqdm(input_files): with self.input_folder.open(file, 'rt') as f: for (key, item) in json.load(f).items(): stat[key] += MetricStats.from_dict(item) with self.output_folder.open(f'{folder}/{STATS_MERGED_NAME}', 'wt') as f: group_name = Path(folder).parent.name if group_name in self.top_k_config.top_k_groups: top_k_keys = heapq.nlargest(self.top_k_config.top_k, stat, key=lambda x: stat.get(x).n) stat = MetricStatsDict(init={s: stat.get(s) for s in top_k_keys}) json.dump(stat.to_dict(), f) if self.remove_input: for file in input_files: self.input_folder.rm(file) if data: yield from data # File: datatrove-main/src/datatrove/pipeline/stats/paragraph_stats.py from typing import get_args from datatrove.data import Document from datatrove.io import DataFolderLike from datatrove.pipeline.filters.gopher_repetition_filter import find_duplicates from datatrove.pipeline.stats.base import BaseStats from datatrove.pipeline.stats.config import DEFAULT_TOP_K_CONFIG, GROUP, TopKConfig def get_short_paragraph_ratio(paragraphs: list[str], threshold: int) -> float: return sum([1 for paragraph in paragraphs if len(paragraph) <= threshold]) / len(paragraphs) def get_long_paragraph_ratio(paragraphs: list[str], threshold: int) -> float: return sum([1 for paragraph in paragraphs if len(paragraph) >= threshold]) / len(paragraphs) class ParagraphStats(BaseStats): type = '📊 - STATS' name = '📄 Paragraph stats' def __init__(self, output_folder: DataFolderLike, short_paragraph_max_chars_threshold: list[int] | None=None, long_paragraph_max_chars_threshold: list[int] | None=None, ignore_empty_paragraphs: bool=False, histogram_round_digits: int=3, groups_to_compute: list[GROUP]=list(get_args(GROUP)), top_k_config: TopKConfig=DEFAULT_TOP_K_CONFIG) -> None: super().__init__(output_folder, groups_to_compute, histogram_round_digits, top_k_config) self.ignore_empty_paragraphs = ignore_empty_paragraphs self.short_paragraph_max_chars_threshold = short_paragraph_max_chars_threshold or [100] self.long_paragraph_max_chars_threshold = long_paragraph_max_chars_threshold or [1000] def extract_stats(self, doc: Document) -> dict[str, int | float]: paragraphs = [p for p in doc.text.split('\n\n') if p.strip()] n_paragraphs = len(paragraphs) paragraphs = [p for p in paragraphs if p.strip()] if self.ignore_empty_paragraphs else paragraphs (paragraph_dups, paragraph_char_dups) = find_duplicates(paragraphs) return {'n_paragraphs': n_paragraphs, 'avg_paragraph_length': sum([len(p) for p in paragraphs]) / n_paragraphs, **{f'short_paragraph_ratio_{chars}': get_short_paragraph_ratio(paragraphs, chars) for chars in self.short_paragraph_max_chars_threshold}, **{f'long_paragraph_ratio_{chars}': get_long_paragraph_ratio(paragraphs, chars) for chars in self.long_paragraph_max_chars_threshold}, 'paragraph_duplicates': paragraph_dups / n_paragraphs, 'paragraph_char_duplicates': paragraph_char_dups / sum((len(p) for p in paragraphs))} # File: datatrove-main/src/datatrove/pipeline/stats/perplexity_stats.py from typing import get_args from datatrove.data import Document from datatrove.io import DataFolderLike from datatrove.pipeline.stats.base import BaseStats from datatrove.pipeline.stats.config import DEFAULT_TOP_K_CONFIG, GROUP, TopKConfig from datatrove.utils.perplexity import KenlmModel from datatrove.utils.typeshelper import Languages class CCNetPerplexityStats(BaseStats): name = '🤯 CCNet perplexity stats' _requires_dependencies = BaseStats._requires_dependencies + ['kenlm'] def __init__(self, output_folder: DataFolderLike, model_dataset: str, language: str=Languages.english, histogram_round_digits: int=3, groups_to_compute: list[GROUP]=list(get_args(GROUP)), top_k_config: TopKConfig=DEFAULT_TOP_K_CONFIG) -> None: super().__init__(output_folder, groups_to_compute, histogram_round_digits, top_k_config) self.model = KenlmModel(model_dataset=model_dataset, language=language) def extract_stats(self, doc: Document) -> dict[str, int | float]: return {f'ccnet_perplexity_{self.model.model_dataset}_{self.model.language}': self.model.get_perplexity(doc.text)} # File: datatrove-main/src/datatrove/pipeline/stats/sentence_stats.py from typing import get_args from datatrove.data import Document from datatrove.io import DataFolderLike from datatrove.pipeline.stats.base import BaseStats from datatrove.pipeline.stats.config import DEFAULT_TOP_K_CONFIG, GROUP, TopKConfig from datatrove.utils.typeshelper import Languages from datatrove.utils.word_tokenizers import load_word_tokenizer def get_short_sentence_ratio(sentences: list[str], threshold: int) -> float: return sum([1 for sentence in sentences if len(sentence) <= threshold]) / len(sentences) def get_long_sentence_ratio(sentences: list[str], threshold: int) -> float: return sum([1 for sentence in sentences if len(sentence) >= threshold]) / len(sentences) class SentenceStats(BaseStats): name = '🈂️ Sentence stats' def __init__(self, output_folder: DataFolderLike, short_sentence_max_chars_threshold: list[int] | None=None, long_sentence_max_chars_threshold: list[int] | None=None, language: str=Languages.english, histogram_round_digits: int=3, groups_to_compute: list[GROUP]=list(get_args(GROUP)), top_k_config: TopKConfig=DEFAULT_TOP_K_CONFIG) -> None: super().__init__(output_folder, groups_to_compute, histogram_round_digits, top_k_config) self.short_sentence_max_chars_threshold = short_sentence_max_chars_threshold or [20] self.long_sentence_max_chars_threshold = long_sentence_max_chars_threshold or [75] self.language = language def extract_stats(self, doc: Document) -> dict[str, int | float]: word_tokenizer = load_word_tokenizer(self.language) sentences = [s for s in word_tokenizer.sent_tokenize(doc.text) if s.strip()] return {'n_sentences': len(sentences), 'avg_sentence_length': sum([len(s) for s in sentences]) / len(sentences), **{f'short_sentence_ratio_{chars}': get_short_sentence_ratio(sentences, chars) for chars in self.short_sentence_max_chars_threshold}, **{f'long_sentence_ratio_{chars}': get_long_sentence_ratio(sentences, chars) for chars in self.long_sentence_max_chars_threshold}} # File: datatrove-main/src/datatrove/pipeline/stats/token_stats.py from datatrove.data import Document from datatrove.io import DataFolderLike from datatrove.pipeline.stats.base import BaseStats from datatrove.pipeline.stats.config import DEFAULT_TOP_K_CONFIG, GROUP, TopKConfig from datatrove.utils.tokenization import PipelineStepWithTokenizer class TokenStats(BaseStats, PipelineStepWithTokenizer): name = '🔗 Token counter' _requires_dependencies = ['tokenizers'] + BaseStats._requires_dependencies def __init__(self, output_folder: DataFolderLike, tokenizer_name_or_path: str='gpt2', groups_to_compute: list[GROUP]=['fqdn', 'suffix', 'summary', 'histogram'], histogram_rounding: int=3, top_k_config: TopKConfig=DEFAULT_TOP_K_CONFIG) -> None: BaseStats.__init__(self, output_folder, groups_to_compute, histogram_rounding, top_k_config) PipelineStepWithTokenizer.__init__(self) self.tokenizer_name_or_path = tokenizer_name_or_path def extract_stats(self, doc: Document) -> dict[str, int | float]: tokens_count = doc.metadata.get('token_count', None) if tokens_count is None: tokens_count = len(self.tokenizer.encode(doc.text).tokens) return {'token_count': tokens_count} # File: datatrove-main/src/datatrove/pipeline/stats/word_stats.py from typing import get_args from datatrove.data import Document from datatrove.io import DataFolderLike from datatrove.pipeline.filters.gopher_quality_filter import STOP_WORDS from datatrove.pipeline.stats.base import BaseStats from datatrove.pipeline.stats.config import DEFAULT_TOP_K_CONFIG, GROUP, TopKConfig from datatrove.utils.typeshelper import Languages from datatrove.utils.word_tokenizers import load_word_tokenizer def get_short_word_ratio(words: list[str], threshold: int) -> float: return sum([1 for word in words if len(word) <= threshold]) / len(words) def get_long_word_ratio(words: list[str], threshold: int) -> float: return sum([1 for word in words if len(word) >= threshold]) / len(words) class WordStats(BaseStats): name = '🈂️ Word stats' def __init__(self, output_folder: DataFolderLike, stop_words: list[str]=STOP_WORDS, short_word_max_chars_threshold: list[int] | None=None, long_word_max_chars_threshold: list[int] | None=None, language: str=Languages.english, groups_to_compute: list[GROUP]=list(get_args(GROUP)), histogram_round_digits: int=3, top_k_config: TopKConfig=DEFAULT_TOP_K_CONFIG) -> None: super().__init__(output_folder, groups_to_compute, histogram_round_digits, top_k_config) self.short_word_max_chars_threshold = short_word_max_chars_threshold or [3] self.long_word_max_chars_threshold = long_word_max_chars_threshold or [7] self.language = language self.stop_words = stop_words def extract_stats(self, doc: Document) -> dict[str, int | float]: word_tokenizer = load_word_tokenizer(self.language) words = word_tokenizer.word_tokenize(doc.text) lines = doc.text.splitlines() return {'n_words': len(words), 'avg_word_length': sum([len(word) for word in words]) / len(words), 'avg_words_per_line': len(words) / len(lines), **{f'short_word_ratio_{chars}': get_short_word_ratio(words, chars) for chars in self.short_word_max_chars_threshold}, **{f'long_word_ratio_{chars}': get_long_word_ratio(words, chars) for chars in self.long_word_max_chars_threshold}, 'type_token_ratio': len(set(words)) / len(words), 'uppercase_word_ratio': sum([1 for word in words if word.isupper()]) / len(words), 'capitalized_word_ratio': sum([1 for word in words if word.istitle()]) / len(words), 'stop_word_ratio': sum([1 for word in words if word in self.stop_words]) / len(words)} # File: datatrove-main/src/datatrove/pipeline/tokens/context_shuffler.py import mmap import numpy as np from numpy.random import default_rng from datatrove.data import DocumentsPipeline from datatrove.io import DataFolderLike, get_datafolder from datatrove.pipeline.base import PipelineStep from datatrove.pipeline.tokens.merger import load_doc_ends from datatrove.utils.logging import logger class DocumentTokenizerContextShuffler(PipelineStep): name = '🗃 Context Shuffler' type = '🔢 - TOKENIZER' def __init__(self, input_folder: DataFolderLike, output_folder: DataFolderLike, window_size: int=2048 + 1, seed: int=None, token_size: int=2): super().__init__() self.input_folder = get_datafolder(input_folder) self.output_folder = get_datafolder(output_folder) self.window_size = window_size self.token_size = token_size self.rand = default_rng(seed) def get_ordering(self, all_doc_ends): doc_ids = np.concatenate([np.ones(len(doc_ends), dtype=int) * i for (i, doc_ends) in enumerate(all_doc_ends)]) return self.rand.permutation(doc_ids) def run(self, data: DocumentsPipeline=None, rank: int=0, world_size: int=1) -> DocumentsPipeline: datafiles = self.input_folder.get_shard(rank, world_size, glob_pattern='*.ds') datafiles_index = self.input_folder.get_shard(rank, world_size, glob_pattern='*.ds.index') for (datafile, index) in zip(datafiles, datafiles_index): logger.info(f'Context shuffling {datafile} with a {self.window_size} token window') total_len = load_doc_ends(self.input_folder.open(index, 'rb'))[-1] nr_windows = total_len // self.window_size ordering = self.rand.permutation(np.arange(0, nr_windows, dtype=int)) with self.output_folder.open(datafile, 'wb') as fout: with self.input_folder.open(datafile, 'rb') as f: with mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ) as unshuf: with self.track_time(): for windowi in ordering: (start, end) = (windowi * self.window_size * self.token_size, (windowi + 1) * self.window_size * self.token_size) fout.write(unshuf[start:end]) # File: datatrove-main/src/datatrove/pipeline/tokens/counter.py from datatrove.data import DocumentsPipeline from datatrove.pipeline.base import PipelineStep from datatrove.utils.batching import batched from datatrove.utils.tokenization import PipelineStepWithTokenizer class TokensCounter(PipelineStepWithTokenizer): name = '📊 Counter' type = '🔢 - TOKENIZER' def __init__(self, tokenizer_name_or_path: str='gpt2', count_eos_token: bool=False, batch_size: int=10000): super().__init__() self.tokenizer_name_or_path = tokenizer_name_or_path self.count_eos_token = count_eos_token self.batch_size = batch_size def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1) -> DocumentsPipeline: from tokenizers import Encoding for batch in batched(data, self.batch_size): with self.track_time(unit='batch'): encoded_batch: list[Encoding] = self.tokenizer.encode_batch([document.text for document in batch]) for (document, encoded) in zip(batch, encoded_batch): count = len(encoded.ids) if self.count_eos_token: count += 1 document.metadata['token_count'] = count self.stat_update('tokens', value=count) yield document class LengthCounter(PipelineStep): name = '📊 Document length counter' type = '🔢 - TOKENIZER' def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1) -> DocumentsPipeline: for document in data: count = document.metadata['token_count'] self.stats[count].update(1) yield document # File: datatrove-main/src/datatrove/pipeline/tokens/merger.py from functools import partial from typing import BinaryIO, Generator import numpy as np from numpy.random import default_rng from tqdm import tqdm from datatrove.data import DocumentsPipeline from datatrove.io import DataFolderLike, get_datafolder from datatrove.pipeline.base import PipelineStep from datatrove.pipeline.tokens.tokenizer import TokenizedFile class DocumentTokenizerMerger(PipelineStep): name = '🗃 Document Merger' type = '🔢 - TOKENIZER' def __init__(self, input_folder: DataFolderLike, output_folder: DataFolderLike, save_filename: str, max_tokens_per_file: int=100000000000.0, max_tokens: int=-1, shuffle: bool=True, upload_block_size: int=20 * 2 ** 20, seed: int=None, save_loss_metadata: bool=False, save_final_metadata: bool=True, progress: bool=True): super().__init__() self.input_folder = get_datafolder(input_folder) self.output_folder = get_datafolder(output_folder) self.save_filename = save_filename self.max_tokens_per_file = max_tokens_per_file self.max_tokens = max_tokens self.shuffle = shuffle self.save_loss_metadata = save_loss_metadata self.rand = default_rng(seed) self.save_final_metadata = save_final_metadata self.upload_block_size = upload_block_size self.progress = progress def get_ordering(self, all_doc_ends): doc_ids = np.concatenate([np.ones(len(doc_ends), dtype=int) * i for (i, doc_ends) in enumerate(all_doc_ends)]) return doc_ids if not self.shuffle else self.rand.permutation(doc_ids) def run(self, data: DocumentsPipeline=None, rank: int=0, world_size: int=1) -> DocumentsPipeline: assert world_size == 1, 'world_size must be 1 for DocumentTokenizerMerger' datafiles = self.input_folder.list_files(glob_pattern='*.ds') datafiles_index = self.input_folder.list_files(glob_pattern='*.ds.index') datafiles_loss = self.input_folder.list_files(glob_pattern='*.ds.loss') if self.save_loss_metadata else [None] * len(datafiles) assert len(datafiles) == len(datafiles_index) == len(datafiles_loss), f'Mismatch between number of .ds, .ds.index and/or .ds.loss files({len(datafiles)} vs {len(datafiles_index)} vs {len(datafiles_loss)})' (tokenizer_name_or_path, token_size) = (None, 2) if self.save_final_metadata: if self.input_folder.isfile(f'{datafiles[0]}.metadata'): with self.input_folder.open(f'{datafiles[0]}.metadata', 'rt') as f: tokenizer_name_or_path = f.read().splitlines()[0] if '|' in tokenizer_name_or_path: (tokenizer_name_or_path, token_size) = tokenizer_name_or_path.split('|') token_size = int(token_size) doc_ends = [load_doc_ends(self.input_folder.open(file, 'rb')) for file in datafiles_index] token_inputs = list(map(partial(get_data_reader, nb_bytes=token_size), self.input_folder.open_files(datafiles), doc_ends)) loss_inputs = list(map(partial(get_data_reader, nb_bytes=1), self.input_folder.open_files(datafiles_loss), doc_ends)) if self.save_loss_metadata else None ordering = self.get_ordering(doc_ends) file_ct = 0 output_file = TokenizedFile(output_folder=self.output_folder, filename=f'{file_ct:03d}_{self.save_filename}.ds', save_loss_metadata=self.save_loss_metadata, upload_block_size=self.upload_block_size, tokenizer_name_or_path=tokenizer_name_or_path, save_final_metadata=self.save_final_metadata, token_size=token_size) for input_file_id in tqdm(ordering, desc='Merging documents', unit='documents', total=len(ordering), disable=not self.progress): if 0 < self.max_tokens <= self.stats['tokens'].total: break if 0 < self.max_tokens_per_file <= len(output_file): output_file.close() file_ct += 1 output_file = TokenizedFile(output_folder=self.output_folder, filename=f'{file_ct:03d}_{self.save_filename}.ds', save_loss_metadata=self.save_loss_metadata, upload_block_size=self.upload_block_size, tokenizer_name_or_path=tokenizer_name_or_path, save_final_metadata=self.save_final_metadata, token_size=token_size) tokens = next(token_inputs[input_file_id]) output_file.write_bytes(tokens) if loss_inputs: output_file.write_loss_bytes(next(loss_inputs[input_file_id])) self.stat_update('tokens', value=len(tokens) // token_size) output_file.close() if self.save_final_metadata: output_file.write_final_metadata(self.stats['tokens'].total, filename=f'{self.save_filename}.ds') def load_doc_ends(file: BinaryIO) -> np.ndarray: with file as f: return np.frombuffer(f.read(), dtype=np.uint64).astype(int) def get_data_reader(file: BinaryIO, doc_ends: list, nb_bytes: int=1, start_e: int=0) -> Generator[bytes, None, None]: with file as f: if start_e != 0: f.seek(int(start_e) * nb_bytes) for r_e in doc_ends: yield f.read((r_e - start_e) * nb_bytes) start_e = r_e # File: datatrove-main/src/datatrove/pipeline/tokens/tokenizer.py import struct from typing import TYPE_CHECKING import humanize import numpy as np from numpy.random import default_rng from datatrove.data import Document, DocumentsPipeline from datatrove.io import DataFolder, DataFolderLike, get_datafolder from datatrove.utils.batching import batched from datatrove.utils.logging import logger from datatrove.utils.tokenization import PipelineStepWithTokenizer SHUFFLING_READ_BLOCK_SIZE = 50000 SHUFFLING_CACHE_TYPE = 'none' if TYPE_CHECKING: from tokenizers import Encoding class TokenizedFile: def __init__(self, output_folder: DataFolderLike, filename: str, save_index: bool=True, save_loss_metadata: bool=False, upload_block_size: int | None=None, tokenizer_name_or_path: str | None=None, save_final_metadata: bool=False, token_size: int=2): self.output_folder = get_datafolder(output_folder) self.filename = filename self.save_index = save_index self.save_loss_metadata = save_loss_metadata self.upload_block_size = upload_block_size self.write_idx = 0 self.token_size = token_size self.token_format = 'I' if self.token_size == 4 else 'H' self.doc_ends = [] self.tokenizer_name_or_path = tokenizer_name_or_path self.save_final_metadata = save_final_metadata self.tokens_file = self.output_folder.open(self.filename, mode='wb', block_size=upload_block_size) self.loss_file: DataFolderLike | None = None if self.save_loss_metadata: self.loss_file = self.output_folder.open(f'{self.filename}.loss', mode='wb', block_size=upload_block_size) def __len__(self): return self.doc_ends[-1] if self.doc_ends else 0 def close(self): if self.tokens_file: self.tokens_file.close() if self.loss_file: self.loss_file.close() if self.save_index: index_file = self.output_folder.open(f'{self.filename}.index', mode='wb') index_file.write(struct.pack('<%sQ' % len(self.doc_ends), *self.doc_ends)) index_file.close() if self.save_final_metadata: self.write_final_metadata() def cleanup(self): self.doc_ends = [] self.output_folder.rm_file(self.filename) if self.loss_file: self.output_folder.rm_file(f'{self.filename}.loss') if self.save_final_metadata and self.output_folder.exists(f'{self.filename}.metadata'): self.output_folder.rm_file(f'{self.filename}.metadata') def write_bytes(self, tk_bytes: bytes, doc_ends: list[int]=None): self.tokens_file.write(tk_bytes) if doc_ends is not None: self.doc_ends.extend([d + self.write_idx for d in doc_ends]) self.write_idx += len(tk_bytes) // self.token_size else: self.write_idx += len(tk_bytes) // self.token_size self.doc_ends.append(self.write_idx) def write_loss_bytes(self, l_bytes: bytes): if self.save_loss_metadata: self.loss_file.write(l_bytes) def write(self, tokens: list[int], loss_values: np.ndarray | None): self.write_bytes(struct.pack(f'<%s{self.token_format}' % len(tokens), *tokens)) if loss_values is not None: self.write_loss_bytes(struct.pack('<%s?' % len(loss_values), *loss_values)) def copy(self, save_filename: str, ordering: np.ndarray, new_output_folder: DataFolder=None, rank: int=0, max_tokens_per_file: int=None) -> 'TokenizedFile': with self.output_folder.open(self.filename, mode='rb', cache_type=SHUFFLING_CACHE_TYPE, block_size=SHUFFLING_READ_BLOCK_SIZE) as tokens_file: loss_file = None if not self.loss_file else self.output_folder.open(f'{self.filename}.loss', mode='rb', cache_type=SHUFFLING_CACHE_TYPE, block_size=SHUFFLING_READ_BLOCK_SIZE // 2) sub_rank = 0 destination = get_output_filename(save_filename, rank, 'shuffled', sub_rank) new_file = TokenizedFile(self.output_folder if not new_output_folder else new_output_folder, destination, save_loss_metadata=self.save_loss_metadata, upload_block_size=self.upload_block_size, tokenizer_name_or_path=self.tokenizer_name_or_path, save_final_metadata=self.save_final_metadata, token_size=self.token_size) logger.info(f'Shuffling in {destination}...') total_tokens_written = 0 for doc_id in ordering: (start, end) = (self.doc_ends[doc_id - 1] if doc_id > 0 else 0, self.doc_ends[doc_id]) tokens_file.seek(start * self.token_size) new_file.write_bytes(tokens_file.read((end - start) * self.token_size)) if loss_file: loss_file.seek(start) new_file.write_loss_bytes(loss_file.read(end - start)) total_tokens_written += end - start if max_tokens_per_file and total_tokens_written > max_tokens_per_file: new_file.close() sub_rank += 1 destination = get_output_filename(save_filename, rank, 'shuffled', sub_rank) new_file = TokenizedFile(self.output_folder if not new_output_folder else new_output_folder, destination, save_loss_metadata=self.save_loss_metadata, upload_block_size=self.upload_block_size, tokenizer_name_or_path=self.tokenizer_name_or_path, save_final_metadata=self.save_final_metadata, token_size=self.token_size) logger.info(f'Shuffling in {destination}...') total_tokens_written = 0 if loss_file: loss_file.close() new_file.close() return new_file def write_final_metadata(self, token_count: int=-1, filename: str=None): tokenizer_name = self.tokenizer_name_or_path if not tokenizer_name: tokenizer_name = 'Unknown Tokenizer' + '|' + str(self.token_size) if filename is None: filename = self.filename with self.output_folder.open(f'{filename}.metadata', 'wt') as f: if token_count == -1: token_count = self.write_idx f.write('\n'.join([tokenizer_name + '|' + str(self.token_size), str(token_count), humanize.metric(token_count, unit='T')])) def get_output_filename(save_filename, rank: int, name: str, sub_rank: int=None): if sub_rank is not None: return '_'.join([x for x in [save_filename, f'{rank:05d}', f'{sub_rank:05d}', f'{name}.ds'] if x]) return '_'.join([x for x in [save_filename, f'{rank:05d}', f'{name}.ds'] if x]) class DocumentTokenizer(PipelineStepWithTokenizer): name = '✍️ Writer' type = '🔢 - TOKENIZER' def __init__(self, output_folder: DataFolderLike, local_working_dir: DataFolderLike | None=None, save_filename: str=None, tokenizer_name_or_path: str='gpt2', eos_token: str='<|endoftext|>', save_loss_metadata: bool=False, shuffle: bool=True, batch_size: int=10000, max_tokens_per_file: int=None, seed: int=None, save_final_metadata: bool=True, upload_block_size: int | None=None): super().__init__() self.output_folder = get_datafolder(output_folder) self.local_working_dir = get_datafolder(local_working_dir) if local_working_dir else None if self.local_working_dir and (not self.local_working_dir.is_local()): raise ValueError('local_working_dir must be a local path') if self.local_working_dir is None and shuffle and (not self.output_folder.is_local()): logger.warning('local_working_dir is not set and output folder is not local. This may slow down the process.') self.save_filename = save_filename self.tokenizer_name_or_path = tokenizer_name_or_path self.eos_token = eos_token self.save_loss_metadata = save_loss_metadata self.shuffle = shuffle self.batch_size = batch_size self.rand = default_rng(seed) self.save_final_metadata = save_final_metadata self.upload_block_size = upload_block_size self.max_tokens_per_file = max_tokens_per_file def get_loss_values(self, document: Document, encoded: 'Encoding'): if self.save_loss_metadata: loss_values = np.ones(len(encoded.ids)) if (no_loss := document.metadata.get('no_loss_ranges', None)): for (start, end) in no_loss: (t_start, t_end) = (encoded.char_to_token(start), encoded.char_to_token(end)) loss_values[t_start:t_end] = 0 if t_end is None or t_end >= len(encoded.ids): loss_values = loss_values[:t_start] return loss_values def write_unshuffled(self, data: DocumentsPipeline, filename: str): from tokenizers import Encoding unshuff = TokenizedFile(self.output_folder if not self.shuffle or not self.local_working_dir else self.local_working_dir, filename, save_index=not self.shuffle, save_loss_metadata=self.save_loss_metadata, upload_block_size=self.upload_block_size, tokenizer_name_or_path=self.tokenizer_name_or_path, save_final_metadata=self.save_final_metadata, token_size=self.token_size) for batch in batched(data, self.batch_size): with self.track_time(unit='batch'): encoded_batch: list[Encoding] = self.tokenizer.encode_batch([document.text for document in batch]) for (document, encoded) in zip(batch, encoded_batch): tokens = encoded.ids loss_values = self.get_loss_values(document, encoded) if loss_values is not None and len(loss_values) < len(tokens): tokens = tokens[:len(loss_values)] unshuff.write(tokens, loss_values) self.stat_update('tokens', value=len(tokens)) unshuff.close() return unshuff def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1) -> DocumentsPipeline: unshuf_filename = get_output_filename(self.save_filename, rank, 'unshuffled') logger.info(f'Tokenizing in "{unshuf_filename}"...') outputfile: TokenizedFile = self.write_unshuffled(data, unshuf_filename) if len(outputfile) == 0: logger.warning('No data saved.') return if self.shuffle: logger.info('Shuffling...') outputfile.copy(self.save_filename, self.rand.permutation(len(outputfile.doc_ends)), self.output_folder, max_tokens_per_file=self.max_tokens_per_file, rank=rank) outputfile.cleanup() # File: datatrove-main/src/datatrove/pipeline/writers/disk_base.py import dataclasses import os.path from abc import ABC, abstractmethod from collections import Counter from string import Template from types import MethodType from typing import IO, Callable from datatrove.data import Document, DocumentsPipeline from datatrove.io import DataFolderLike, get_datafolder from datatrove.pipeline.base import PipelineStep from datatrove.utils.typeshelper import StatHints class DiskWriter(PipelineStep, ABC): default_output_filename: str = None type = '💽 - WRITER' def __init__(self, output_folder: DataFolderLike, output_filename: str=None, compression: str | None='infer', adapter: Callable=None, mode: str='wt', expand_metadata: bool=False, max_file_size: int=-1): super().__init__() self.compression = compression self.output_folder = get_datafolder(output_folder) output_filename = output_filename or self.default_output_filename if self.compression == 'gzip' and (not output_filename.endswith('.gz')): output_filename += '.gz' elif self.compression == 'zstd' and (not output_filename.endswith('.zst')): output_filename += '.zst' self.max_file_size = max_file_size self.file_id_counter = Counter() if self.max_file_size > 0 and mode != 'wb': raise ValueError('Can only specify `max_file_size` when writing in binary mode!') self.output_filename = Template(output_filename) self.output_mg = self.output_folder.get_output_file_manager(mode=mode, compression=compression) self.adapter = MethodType(adapter, self) if adapter else self._default_adapter self.expand_metadata = expand_metadata def _default_adapter(self, document: Document) -> dict: data = {key: val for (key, val) in dataclasses.asdict(document).items() if val} if self.expand_metadata and 'metadata' in data: data |= data.pop('metadata') return data def __enter__(self): return self def close(self): self.output_mg.close() def __exit__(self, exc_type, exc_val, exc_tb): self.close() def _get_output_filename(self, document: Document, rank: int | str=0, **kwargs) -> str: return self.output_filename.substitute({'rank': str(rank).zfill(5), 'id': document.id, **document.metadata, **kwargs}) @abstractmethod def _write(self, document: dict, file_handler: IO, filename: str): raise NotImplementedError def _on_file_switch(self, _original_name, old_filename, _new_filename): self.output_mg.pop(old_filename).close() def _get_filename_with_file_id(self, filename): if os.path.dirname(filename): return f'{os.path.dirname(filename)}/{self.file_id_counter[filename]:03d}_{os.path.basename(filename)}' return f'{self.file_id_counter[filename]:03d}_{os.path.basename(filename)}' def write(self, document: Document, rank: int=0, **kwargs): original_name = output_filename = self._get_output_filename(document, rank, **kwargs) if self.max_file_size > 0: output_filename = self._get_filename_with_file_id(original_name) if self.output_mg.get_file(output_filename).tell() >= self.max_file_size: self.file_id_counter[original_name] += 1 new_output_filename = self._get_filename_with_file_id(original_name) self._on_file_switch(original_name, output_filename, new_output_filename) output_filename = new_output_filename self._write(self.adapter(document), self.output_mg.get_file(output_filename), original_name) self.stat_update(self._get_output_filename(document, 'XXXXX', **kwargs)) self.stat_update(StatHints.total) self.update_doc_stats(document) def run(self, data: DocumentsPipeline, rank: int=0, world_size: int=1) -> DocumentsPipeline: with self: for document in data: with self.track_time(): self.write(document, rank) yield document # File: datatrove-main/src/datatrove/pipeline/writers/huggingface.py import os import random import tempfile import time from typing import Callable from huggingface_hub import CommitOperationAdd, create_commit, create_repo, preupload_lfs_files from huggingface_hub.utils import HfHubHTTPError from datatrove.io import DataFolderLike, get_datafolder from datatrove.pipeline.writers import ParquetWriter from datatrove.utils.logging import logger MAX_RETRIES = 12 BASE_DELAY = 0.1 class HuggingFaceDatasetWriter(ParquetWriter): default_output_filename: str = 'data/${rank}.parquet' name = '🤗 HuggingFace' def __init__(self, dataset: str, private: bool=True, local_working_dir: DataFolderLike | None=None, output_filename: str=None, compression: str | None=None, adapter: Callable=None, cleanup: bool=True, expand_metadata: bool=True, max_file_size: int=round(4.5 * 2 ** 30)): self.dataset = dataset self.private = private self.local_working_dir = get_datafolder(local_working_dir if local_working_dir else tempfile.TemporaryDirectory()) self.cleanup = cleanup if not self.local_working_dir.is_local(): raise ValueError('local_working_dir must be a local path') if os.environ.get('HF_HUB_ENABLE_HF_TRANSFER', '0') != '1': logger.warning('HF_HUB_ENABLE_HF_TRANSFER is not set to "1". Install hf_transfer and set the env variable for faster uploads:\npip install hf-transfer\nexport HF_HUB_ENABLE_HF_TRANSFER=1') super().__init__(output_folder=local_working_dir, output_filename=output_filename, compression=compression, adapter=adapter, expand_metadata=expand_metadata, max_file_size=max_file_size) self.operations = [] self._repo_init = False def upload_files(self, *filenames): if not self._repo_init: create_repo(self.dataset, private=self.private, repo_type='dataset', exist_ok=True) self._repo_init = True additions = [CommitOperationAdd(path_in_repo=filename, path_or_fileobj=self.local_working_dir.resolve_paths(filename)) for filename in filenames] logger.info(f"Uploading {','.join(filenames)} to the hub...") preupload_lfs_files(self.dataset, repo_type='dataset', additions=additions) logger.info(f"Upload of {','.join(filenames)} to the hub complete!") if self.cleanup: for filename in filenames: self.local_working_dir.rm(filename) self.operations.extend(additions) def close(self, rank: int=0): filelist = list(self.output_mg.get_open_files().keys()) super().close() if filelist: logger.info(f'Starting upload of {len(filelist)} files to {self.dataset}') self.upload_files(*filelist) retries = 0 while True: try: create_commit(self.dataset, repo_type='dataset', operations=self.operations, commit_message=f'DataTrove upload ({len(self.operations)} files)') break except HfHubHTTPError as e: if 'A commit has happened since' in e.server_message: if retries >= MAX_RETRIES: logger.error(f'Failed to create commit after MAX_RETRIES={MAX_RETRIES!r}. Giving up.') raise e logger.info('Commit creation race condition issue. Waiting...') time.sleep(BASE_DELAY * 2 ** retries + random.uniform(0, 2)) retries += 1 else: raise e def _on_file_switch(self, original_name, old_filename, new_filename): super()._on_file_switch(original_name, old_filename, new_filename) self.upload_files(old_filename) # File: datatrove-main/src/datatrove/pipeline/writers/jsonl.py from typing import IO, Callable from datatrove.io import DataFolderLike from datatrove.pipeline.writers.disk_base import DiskWriter class JsonlWriter(DiskWriter): default_output_filename: str = '${rank}.jsonl' name = '🐿 Jsonl' _requires_dependencies = ['orjson'] def __init__(self, output_folder: DataFolderLike, output_filename: str=None, compression: str | None='gzip', adapter: Callable=None, expand_metadata: bool=False, max_file_size: int=-1): super().__init__(output_folder, output_filename=output_filename, compression=compression, adapter=adapter, expand_metadata=expand_metadata, mode='wb', max_file_size=max_file_size) def _write(self, document: dict, file_handler: IO, _filename: str): import orjson file_handler.write(orjson.dumps(document, option=orjson.OPT_APPEND_NEWLINE)) # File: datatrove-main/src/datatrove/pipeline/writers/parquet.py from collections import Counter, defaultdict from typing import IO, Callable, Literal from datatrove.io import DataFolderLike from datatrove.pipeline.writers.disk_base import DiskWriter class ParquetWriter(DiskWriter): default_output_filename: str = '${rank}.parquet' name = '📒 Parquet' _requires_dependencies = ['pyarrow'] def __init__(self, output_folder: DataFolderLike, output_filename: str=None, compression: Literal['snappy', 'gzip', 'brotli', 'lz4', 'zstd'] | None=None, adapter: Callable=None, batch_size: int=1000, expand_metadata: bool=False, max_file_size: int=5 * 2 ** 30): if compression not in {'snappy', 'gzip', 'brotli', 'lz4', 'zstd', None}: raise ValueError("Invalid compression type. Allowed types are 'snappy', 'gzip', 'brotli', 'lz4', 'zstd', or None.") super().__init__(output_folder, output_filename, compression=None, adapter=adapter, mode='wb', expand_metadata=expand_metadata, max_file_size=max_file_size) self._writers = {} self._batches = defaultdict(list) self._file_counter = Counter() self.compression = compression self.batch_size = batch_size def _on_file_switch(self, original_name, old_filename, new_filename): self._writers.pop(original_name).close() super()._on_file_switch(original_name, old_filename, new_filename) def _write_batch(self, filename): if not self._batches[filename]: return import pyarrow as pa batch = pa.RecordBatch.from_pylist(self._batches.pop(filename)) self._writers[filename].write_batch(batch) def _write(self, document: dict, file_handler: IO, filename: str): import pyarrow as pa import pyarrow.parquet as pq if filename not in self._writers: self._writers[filename] = pq.ParquetWriter(file_handler, schema=pa.RecordBatch.from_pylist([document]).schema, compression=self.compression) self._batches[filename].append(document) if len(self._batches[filename]) == self.batch_size: self._write_batch(filename) def close(self): for filename in list(self._batches.keys()): self._write_batch(filename) for writer in self._writers.values(): writer.close() self._batches.clear() self._writers.clear() super().close() # File: datatrove-main/src/datatrove/tools/check_dataset.py import argparse import os import struct from typing import IO import numpy as np from tqdm import tqdm from datatrove.io import DataFolder, get_datafolder from datatrove.utils.tokenization import load_tokenizer parser = argparse.ArgumentParser() parser.add_argument('data', type=str, help='path to folder with dataset to check', nargs='?', default=os.getcwd()) parser.add_argument('-t', '--tokenizer', type=str, help='tokenizer to use', default='gpt2') parser.add_argument('--eos', type=str, help='eos token', default='<|endoftext|>') '' def load_doc_ends(file: IO): with file as f: return np.frombuffer(f.read(), dtype=np.uint64).tolist() def load_dataset_bytes(file, doc_ends, bytes_per_value: int=2): with file as f: for (start, end) in zip([0] + doc_ends[:-1], doc_ends): data = f.read((end - start) * bytes_per_value) assert len(data) == (end - start) * bytes_per_value, 'Could not read correct number of bytes' yield data assert f.read(1) == b'', 'Dataset should be exhausted but there is more data to read' def check_dataset(input_folder: DataFolder, tokenizer: str='gpt2', eos_token: str='<|endoftext|>'): tokenizer = load_tokenizer(tokenizer) eos_token = tokenizer.token_to_id(eos_token) def open_file(path): return input_folder.open(path, 'rb') datafiles = input_folder.list_files(glob_pattern='*.ds') datafiles_index = input_folder.list_files(glob_pattern='*.ds.index') datafiles_loss = input_folder.list_files(glob_pattern='*.ds.loss') check_loss = bool(datafiles_loss) assert len(datafiles) == len(datafiles_index) and (not check_loss or len(datafiles) == len(datafiles_loss)), 'Mismatch between number of .ds, .ds.index and/or .ds.loss files' doc_ends = [load_doc_ends(open_file(file)) for file in datafiles_index] token_inputs = [load_dataset_bytes(open_file(path), ends) for (path, ends) in zip(datafiles, doc_ends)] loss_inputs = [load_dataset_bytes(open_file(path), ends, bytes_per_value=1) for (path, ends) in zip(datafiles_loss, doc_ends)] if check_loss else [None] * len(token_inputs) for (filei, (file_doc_ends, file_token_inputs, file_loss_inputs)) in enumerate(zip(doc_ends, token_inputs, loss_inputs)): for (doci, tokens) in tqdm(enumerate(file_token_inputs), total=len(file_doc_ends)): last_token = struct.unpack('<H', tokens[-2:])[0] assert last_token == eos_token, f'no EOS at doc end of doc {doci}' if __name__ == '__main__': args = parser.parse_args() input_folder: DataFolder = get_datafolder(args.data) check_dataset(input_folder, args.tokenizer, args.eos) print('All checks ok') # File: datatrove-main/src/datatrove/tools/failed_logs.py import argparse import json import os.path import re from rich.console import Console from rich.prompt import Confirm from datatrove.io import get_datafolder from datatrove.utils._import_utils import is_rich_available from datatrove.utils.logging import logger if not is_rich_available(): raise ImportError('Please install `rich` to run this command (`pip install rich`).') parser = argparse.ArgumentParser('Fetch the log files of failed tasks.') parser.add_argument('path', type=str, nargs='?', help='Path to the logging folder. Defaults to current directory.', default=os.getcwd()) RANK_FROM_LOG_FILENAME_REGEX = re.compile('logs/task_(\\d{5})\\.log') def main(): args = parser.parse_args() console = Console() logger.remove() logging_dir = get_datafolder(args.path) if not logging_dir.isfile('executor.json'): console.log('Could not find "executor.json" in the given directory. Are you sure it is a logging folder?', style='red') return with logging_dir.open('executor.json', 'rt') as f: world_size = json.load(f).get('world_size', None) if not world_size: console.log('Could not get the total number of tasks, please try relaunching the run.', style='red') return console.log(f'Found executor config: {world_size} tasks') with console.status('Fetching list of incomplete tasks'): completed = set(logging_dir.list_files('completions')) incomplete = set(filter(lambda rank: f'completions/{rank:05d}' not in completed, range(world_size))) console.log(f'Found {len(incomplete)}/{world_size} incomplete tasks.') with console.status('Looking for log files'): incomplete_logs = list(filter(lambda file: int(RANK_FROM_LOG_FILENAME_REGEX.search(file).group(1)) in incomplete, logging_dir.list_files('logs'))) console.log(f'Found {len(incomplete_logs)} log files for incomplete tasks.') first = True for incomplete_log in incomplete_logs: if not first and (not Confirm.ask(f'Show next log ([i]{incomplete_log}[/i])?', default=True)): break with console.pager(): with logging_dir.open(incomplete_log, 'rt') as f: console.print(f.read()) first = False if __name__ == '__main__': main() # File: datatrove-main/src/datatrove/tools/inspect_data.py import argparse import os.path import sys from rich.console import Console from rich.panel import Panel from rich.prompt import Confirm, Prompt from datatrove.io import DataFolder, get_datafolder from datatrove.pipeline.filters import SamplerFilter from datatrove.pipeline.readers import CSVReader, JsonlReader, ParquetReader, WarcReader from datatrove.pipeline.writers import JsonlWriter from datatrove.utils._import_utils import is_rich_available '' if not is_rich_available(): raise ImportError('Please install `rich` to run this command (`pip install rich`).') parser = argparse.ArgumentParser("Manually inspect some RefinedWeb samples. Any unknown parameters will be passed to the reader (example: 'text_key=text').") parser.add_argument('path', type=str, nargs='?', help='Path to the data folder. Defaults to current directory.', default=os.getcwd()) parser.add_argument('-r', '--reader', type=str, help="The type of Reader to use to read the data. By default it will be guessed from the file extension. Can be ('jsonl', 'parquet', 'csv' or 'warc')") parser.add_argument('-s', '--sample', type=float, help='Randomly sample a given % of samples. 1.0 to see all samples', default=1.0) parser.add_argument('-l', '--label', type=str, help='Label the examples as good/bad and store at this location', default='') console = Console() def reader_class_from_name(reader_type): match reader_type: case 'jsonl': return JsonlReader case 'csv': return CSVReader case 'parquet': return ParquetReader case 'warc': return WarcReader case other: console.log(f'[red]Unknwon reader type {other}') sys.exit(-1) def reader_factory(data_folder: DataFolder, reader_type: str=None, **kwargs): data_files = data_folder.list_files() if not data_files: console.log(f'[red]Could not find any files in "{data_folder.path}"') sys.exit(-1) if not reader_type: match data_files[0][data_files[0].index('.'):]: case '.jsonl.gz' | '.jsonl' | '.json': reader_type = 'jsonl' case '.csv': reader_type = 'csv' case '.parquet': reader_type = 'parquet' case '.warc.gz' | 'arc.gz' | '.warc': reader_type = 'warc' case other: console.log(f'[red]Could not find a matching reader for file extension "{other}"') sys.exit(-1) return reader_class_from_name(reader_type)(data_folder, **kwargs) def get_filter_expr(text=None): return (lambda x: eval(text)) if text else lambda x: True def main(): """""" (args, extra_args) = parser.parse_known_args() kwargs = dict((extra_arg.split('=') for extra_arg in extra_args)) data_folder = get_datafolder(args.path) label_folder = get_datafolder(args.label) if args.label else None reader = reader_factory(data_folder, args.reader, **kwargs) sampler = SamplerFilter(args.sample) console.print(f'''Loading samples from "{data_folder.path}" with {reader} and sampling_rate={args.sample}.\nSamples are displayed full page one by one.\nIf you don't see any color you may run "export PAGER='less -r'".''') filter_expr_text = None if Confirm.ask("Would you like to add a filtering expression? (ex: x.metadata['token_count'] > 5000)", default=False): filter_expr_text = Confirm.get_input(console, 'Type your filtering expression: ', password=False) filter_expr = get_filter_expr(filter_expr_text) good_samples = [] bad_samples = [] iterator = sampler(reader()) try: for sample in iterator: if not filter_expr(sample): continue with console.pager(styles=True): console.print(Panel(f'[yellow]Data ID:[reset] {sample.id}\n[yellow]Metadata:[reset]\n' + '\n'.join((f'- [blue]{field}: [reset] {value}' for (field, value) in sample.metadata.items())))) console.print(sample.text) if label_folder: result = Prompt.ask("To label as good/bad example enter 'g'/'b'. Enter 'q' to skip labelling and move to the next sample. Enter 'e' (exit) to leave:", console=console, choices=['g', 'b', 'e', 'q']) if result == 'g': good_samples.append(sample) elif result == 'b': bad_samples.append(sample) elif result == 'e': break except Exception: console.print_exception() finally: if good_samples and label_folder: with JsonlWriter(label_folder, 'good_samples.jsonl', compression=None) as writer: for sample in good_samples: writer.write(sample) if bad_samples and label_folder: with JsonlWriter(label_folder, 'bad_samples.jsonl', compression=None) as writer: for sample in bad_samples: writer.write(sample) if __name__ == '__main__': main() # File: datatrove-main/src/datatrove/tools/jobs_status.py import argparse import json import os.path from rich.console import Console from datatrove.io import get_datafolder from datatrove.utils._import_utils import is_rich_available from datatrove.utils.logging import logger if not is_rich_available(): raise ImportError('Please install `rich` to run this command (`pip install rich`).') parser = argparse.ArgumentParser('Fetch all jobs that are running or complete.') parser.add_argument('path', type=str, nargs='?', help='Path to the logging folder. Defaults to current directory.', default=os.getcwd()) parser.add_argument('-p', '--log_prefix', type=str, nargs='?', help='Prefix of logging folders to be scanned.', default='') parser.add_argument('-hc', '--hide_complete', help='Hide all jobs that are already complete.', action='store_true') def main(): args = parser.parse_args() console = Console() main_folder = get_datafolder(args.path) logging_dirs = [f for (f, info) in main_folder.glob(f'{args.log_prefix}*', detail=True, maxdepth=1).items() if info['type'] == 'directory'] logger.remove() complete_jobs = 0 incomplete_jobs = 0 complete_tasks = 0 incomplete_tasks = 0 for path in logging_dirs: logging_dir = get_datafolder(main_folder.resolve_paths(path)) if not logging_dir.isfile('executor.json'): console.log(f'Could not find "executor.json" in the given directory ({path}). Are you sure it is a logging folder?', style='red') continue with logging_dir.open('executor.json', 'rt') as f: world_size = json.load(f).get('world_size', None) if not world_size: console.log(f'Could not get the total number of tasks in {path}, please try relaunching the run.', style='red') continue with console.status('Fetching list of incomplete tasks'): completed = set(logging_dir.list_files('completions')) incomplete = set(filter(lambda rank: f'completions/{rank:05d}' not in completed, range(world_size))) complete_tasks += len(completed) incomplete_tasks += len(incomplete) if len(incomplete) == 0: emoji = '✅' complete_jobs += 1 else: emoji = '❌' incomplete_jobs += 1 if len(incomplete) > 0 or not args.hide_complete: console.log(f"{emoji} {path + ':': <50}{len(completed)}/{world_size} ({len(completed) / world_size:.0%}) completed tasks.") if complete_jobs + incomplete_jobs > 0: console.log(f'Summary: {complete_jobs}/{complete_jobs + incomplete_jobs} ({complete_jobs / (complete_jobs + incomplete_jobs):.0%}) jobs completed, {complete_tasks}/{complete_tasks + incomplete_tasks} ({complete_tasks / (complete_tasks + incomplete_tasks):.0%}) tasks completed.') else: console.log('No jobs found.') if __name__ == '__main__': main() # File: datatrove-main/src/datatrove/tools/launch_pickled_pipeline.py import argparse import dill from datatrove.executor.base import PipelineExecutor from datatrove.io import open_file parser = argparse.ArgumentParser('Loads a pickled pipeline executor and launches it.') parser.add_argument('path', type=str, help='Path to the pickled file (usually a file called executor.pik)') def main(): args = parser.parse_args() with open_file(args.path, 'rb') as f: executor: PipelineExecutor = dill.load(f) executor.run() if __name__ == '__main__': main() # File: datatrove-main/src/datatrove/tools/merge_stats.py import argparse import json import os.path from tqdm import tqdm from datatrove.io import get_datafolder, open_file from datatrove.utils.logging import logger from datatrove.utils.stats import PipelineStats parser = argparse.ArgumentParser('Combine and average per task statistics into a single file.') parser.add_argument('path', type=str, nargs='?', help='Path to the stats folder. Defaults to current directory.', default=os.getcwd()) parser.add_argument('--output', '-o', type=str, help="Save file location. Defaults to 'merged_stats.json'.", default='merged_stats.json') def main(): args = parser.parse_args() stats_folder = get_datafolder(args.path) path = args.output stats = [] for file in tqdm(stats_folder.list_files()): with stats_folder.open(file, 'rt') as f: stats.append(PipelineStats.from_json(json.load(f))) merged = sum(tqdm(stats), start=PipelineStats()) with open_file(path, mode='wt') as f: merged.save_to_disk(f) logger.info(f'Processing complete. Results saved to {path}.') logger.info(merged) if __name__ == '__main__': main() |