File size: 246,055 Bytes
aa783ee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 |
"""
Implementation of operations on Array objects and objects supporting
the buffer protocol.
"""
import functools
import math
import operator
from llvmlite import ir
from llvmlite.ir import Constant
import numpy as np
from numba import pndindex, literal_unroll
from numba.core import types, typing, errors, cgutils, extending
from numba.np.numpy_support import (as_dtype, from_dtype, carray, farray,
is_contiguous, is_fortran,
check_is_integer, type_is_scalar,
lt_complex, lt_floats)
from numba.np.numpy_support import type_can_asarray, is_nonelike, numpy_version
from numba.core.imputils import (lower_builtin, lower_getattr,
lower_getattr_generic,
lower_setattr_generic,
lower_cast, lower_constant,
iternext_impl, impl_ret_borrowed,
impl_ret_new_ref, impl_ret_untracked,
RefType)
from numba.core.typing import signature
from numba.core.types import StringLiteral
from numba.core.extending import (register_jitable, overload, overload_method,
intrinsic, overload_attribute)
from numba.misc import quicksort, mergesort
from numba.cpython import slicing
from numba.cpython.unsafe.tuple import tuple_setitem, build_full_slice_tuple
from numba.core.extending import overload_classmethod
from numba.core.typing.npydecl import (parse_dtype as ty_parse_dtype,
parse_shape as ty_parse_shape,
_parse_nested_sequence,
_sequence_of_arrays,
_choose_concatenation_layout)
def set_range_metadata(builder, load, lower_bound, upper_bound):
"""
Set the "range" metadata on a load instruction.
Note the interval is in the form [lower_bound, upper_bound).
"""
range_operands = [Constant(load.type, lower_bound),
Constant(load.type, upper_bound)]
md = builder.module.add_metadata(range_operands)
load.set_metadata("range", md)
def mark_positive(builder, load):
"""
Mark the result of a load instruction as positive (or zero).
"""
upper_bound = (1 << (load.type.width - 1)) - 1
set_range_metadata(builder, load, 0, upper_bound)
def make_array(array_type):
"""
Return the Structure representation of the given *array_type*
(an instance of types.ArrayCompatible).
Note this does not call __array_wrap__ in case a new array structure
is being created (rather than populated).
"""
real_array_type = array_type.as_array
base = cgutils.create_struct_proxy(real_array_type)
ndim = real_array_type.ndim
class ArrayStruct(base):
def _make_refs(self, ref):
sig = signature(real_array_type, array_type)
try:
array_impl = self._context.get_function('__array__', sig)
except NotImplementedError:
return super(ArrayStruct, self)._make_refs(ref)
# Return a wrapped structure and its unwrapped reference
datamodel = self._context.data_model_manager[array_type]
be_type = self._get_be_type(datamodel)
if ref is None:
outer_ref = cgutils.alloca_once(self._builder, be_type,
zfill=True)
else:
outer_ref = ref
# NOTE: __array__ is called with a pointer and expects a pointer
# in return!
ref = array_impl(self._builder, (outer_ref,))
return outer_ref, ref
@property
def shape(self):
"""
Override .shape to inform LLVM that its elements are all positive.
"""
builder = self._builder
if ndim == 0:
return base.__getattr__(self, "shape")
# Unfortunately, we can't use llvm.assume as its presence can
# seriously pessimize performance,
# *and* the range metadata currently isn't improving anything here,
# see https://llvm.org/bugs/show_bug.cgi?id=23848 !
ptr = self._get_ptr_by_name("shape")
dims = []
for i in range(ndim):
dimptr = cgutils.gep_inbounds(builder, ptr, 0, i)
load = builder.load(dimptr)
dims.append(load)
mark_positive(builder, load)
return cgutils.pack_array(builder, dims)
return ArrayStruct
def get_itemsize(context, array_type):
"""
Return the item size for the given array or buffer type.
"""
llty = context.get_data_type(array_type.dtype)
return context.get_abi_sizeof(llty)
def load_item(context, builder, arrayty, ptr):
"""
Load the item at the given array pointer.
"""
align = None if arrayty.aligned else 1
return context.unpack_value(builder, arrayty.dtype, ptr,
align=align)
def store_item(context, builder, arrayty, val, ptr):
"""
Store the item at the given array pointer.
"""
align = None if arrayty.aligned else 1
return context.pack_value(builder, arrayty.dtype, val, ptr, align=align)
def fix_integer_index(context, builder, idxty, idx, size):
"""
Fix the integer index' type and value for the given dimension size.
"""
if idxty.signed:
ind = context.cast(builder, idx, idxty, types.intp)
ind = slicing.fix_index(builder, ind, size)
else:
ind = context.cast(builder, idx, idxty, types.uintp)
return ind
def normalize_index(context, builder, idxty, idx):
"""
Normalize the index type and value. 0-d arrays are converted to scalars.
"""
if isinstance(idxty, types.Array) and idxty.ndim == 0:
assert isinstance(idxty.dtype, types.Integer)
idxary = make_array(idxty)(context, builder, idx)
idxval = load_item(context, builder, idxty, idxary.data)
return idxty.dtype, idxval
else:
return idxty, idx
def normalize_indices(context, builder, index_types, indices):
"""
Same as normalize_index(), but operating on sequences of
index types and values.
"""
if len(indices):
index_types, indices = zip(*[normalize_index(context, builder, idxty,
idx)
for idxty, idx in zip(index_types, indices)
])
return index_types, indices
def populate_array(array, data, shape, strides, itemsize, meminfo,
parent=None):
"""
Helper function for populating array structures.
This avoids forgetting to set fields.
*shape* and *strides* can be Python tuples or LLVM arrays.
"""
context = array._context
builder = array._builder
datamodel = array._datamodel
# doesn't matter what this array type instance is, it's just to get the
# fields for the datamodel of the standard array type in this context
standard_array = types.Array(types.float64, 1, 'C')
standard_array_type_datamodel = context.data_model_manager[standard_array]
required_fields = set(standard_array_type_datamodel._fields)
datamodel_fields = set(datamodel._fields)
# Make sure that the presented array object has a data model that is close
# enough to an array for this function to proceed.
if (required_fields & datamodel_fields) != required_fields:
missing = required_fields - datamodel_fields
msg = (f"The datamodel for type {array._fe_type} is missing "
f"field{'s' if len(missing) > 1 else ''} {missing}.")
raise ValueError(msg)
if meminfo is None:
meminfo = Constant(context.get_value_type(
datamodel.get_type('meminfo')), None)
intp_t = context.get_value_type(types.intp)
if isinstance(shape, (tuple, list)):
shape = cgutils.pack_array(builder, shape, intp_t)
if isinstance(strides, (tuple, list)):
strides = cgutils.pack_array(builder, strides, intp_t)
if isinstance(itemsize, int):
itemsize = intp_t(itemsize)
attrs = dict(shape=shape,
strides=strides,
data=data,
itemsize=itemsize,
meminfo=meminfo,)
# Set `parent` attribute
if parent is None:
attrs['parent'] = Constant(context.get_value_type(
datamodel.get_type('parent')), None)
else:
attrs['parent'] = parent
# Calc num of items from shape
nitems = context.get_constant(types.intp, 1)
unpacked_shape = cgutils.unpack_tuple(builder, shape, shape.type.count)
# (note empty shape => 0d array therefore nitems = 1)
for axlen in unpacked_shape:
nitems = builder.mul(nitems, axlen, flags=['nsw'])
attrs['nitems'] = nitems
# Make sure that we have all the fields
got_fields = set(attrs.keys())
if got_fields != required_fields:
raise ValueError("missing {0}".format(required_fields - got_fields))
# Set field value
for k, v in attrs.items():
setattr(array, k, v)
return array
def update_array_info(aryty, array):
"""
Update some auxiliary information in *array* after some of its fields
were changed. `itemsize` and `nitems` are updated.
"""
context = array._context
builder = array._builder
# Calc num of items from shape
nitems = context.get_constant(types.intp, 1)
unpacked_shape = cgutils.unpack_tuple(builder, array.shape, aryty.ndim)
for axlen in unpacked_shape:
nitems = builder.mul(nitems, axlen, flags=['nsw'])
array.nitems = nitems
array.itemsize = context.get_constant(types.intp,
get_itemsize(context, aryty))
def normalize_axis(func_name, arg_name, ndim, axis):
"""Constrain axis values to valid positive values."""
raise NotImplementedError()
@overload(normalize_axis)
def normalize_axis_overloads(func_name, arg_name, ndim, axis):
if not isinstance(func_name, StringLiteral):
raise errors.TypingError("func_name must be a str literal.")
if not isinstance(arg_name, StringLiteral):
raise errors.TypingError("arg_name must be a str literal.")
msg = (
f"{func_name.literal_value}: Argument {arg_name.literal_value} "
"out of bounds for dimensions of the array"
)
def impl(func_name, arg_name, ndim, axis):
if axis < 0:
axis += ndim
if axis < 0 or axis >= ndim:
raise ValueError(msg)
return axis
return impl
@lower_builtin('getiter', types.Buffer)
def getiter_array(context, builder, sig, args):
[arrayty] = sig.args
[array] = args
iterobj = context.make_helper(builder, sig.return_type)
zero = context.get_constant(types.intp, 0)
indexptr = cgutils.alloca_once_value(builder, zero)
iterobj.index = indexptr
iterobj.array = array
# Incref array
if context.enable_nrt:
context.nrt.incref(builder, arrayty, array)
res = iterobj._getvalue()
# Note: a decref on the iterator will dereference all internal MemInfo*
out = impl_ret_new_ref(context, builder, sig.return_type, res)
return out
def _getitem_array_single_int(context, builder, return_type, aryty, ary, idx):
""" Evaluate `ary[idx]`, where idx is a single int. """
# optimized form of _getitem_array_generic
shapes = cgutils.unpack_tuple(builder, ary.shape, count=aryty.ndim)
strides = cgutils.unpack_tuple(builder, ary.strides, count=aryty.ndim)
offset = builder.mul(strides[0], idx)
dataptr = cgutils.pointer_add(builder, ary.data, offset)
view_shapes = shapes[1:]
view_strides = strides[1:]
if isinstance(return_type, types.Buffer):
# Build array view
retary = make_view(context, builder, aryty, ary, return_type,
dataptr, view_shapes, view_strides)
return retary._getvalue()
else:
# Load scalar from 0-d result
assert not view_shapes
return load_item(context, builder, aryty, dataptr)
@lower_builtin('iternext', types.ArrayIterator)
@iternext_impl(RefType.BORROWED)
def iternext_array(context, builder, sig, args, result):
[iterty] = sig.args
[iter] = args
arrayty = iterty.array_type
iterobj = context.make_helper(builder, iterty, value=iter)
ary = make_array(arrayty)(context, builder, value=iterobj.array)
nitems, = cgutils.unpack_tuple(builder, ary.shape, count=1)
index = builder.load(iterobj.index)
is_valid = builder.icmp_signed('<', index, nitems)
result.set_valid(is_valid)
with builder.if_then(is_valid):
value = _getitem_array_single_int(
context, builder, iterty.yield_type, arrayty, ary, index
)
result.yield_(value)
nindex = cgutils.increment_index(builder, index)
builder.store(nindex, iterobj.index)
# ------------------------------------------------------------------------------
# Basic indexing (with integers and slices only)
def basic_indexing(context, builder, aryty, ary, index_types, indices,
boundscheck=None):
"""
Perform basic indexing on the given array.
A (data pointer, shapes, strides) tuple is returned describing
the corresponding view.
"""
zero = context.get_constant(types.intp, 0)
one = context.get_constant(types.intp, 1)
shapes = cgutils.unpack_tuple(builder, ary.shape, aryty.ndim)
strides = cgutils.unpack_tuple(builder, ary.strides, aryty.ndim)
output_indices = []
output_shapes = []
output_strides = []
num_newaxes = len([idx for idx in index_types if is_nonelike(idx)])
ax = 0
for indexval, idxty in zip(indices, index_types):
if idxty is types.ellipsis:
# Fill up missing dimensions at the middle
n_missing = aryty.ndim - len(indices) + 1 + num_newaxes
for i in range(n_missing):
output_indices.append(zero)
output_shapes.append(shapes[ax])
output_strides.append(strides[ax])
ax += 1
continue
# Regular index value
if isinstance(idxty, types.SliceType):
slice = context.make_helper(builder, idxty, value=indexval)
slicing.guard_invalid_slice(context, builder, idxty, slice)
slicing.fix_slice(builder, slice, shapes[ax])
output_indices.append(slice.start)
sh = slicing.get_slice_length(builder, slice)
st = slicing.fix_stride(builder, slice, strides[ax])
output_shapes.append(sh)
output_strides.append(st)
elif isinstance(idxty, types.Integer):
ind = fix_integer_index(context, builder, idxty, indexval,
shapes[ax])
if boundscheck:
cgutils.do_boundscheck(context, builder, ind, shapes[ax], ax)
output_indices.append(ind)
elif is_nonelike(idxty):
output_shapes.append(one)
output_strides.append(zero)
ax -= 1
else:
raise NotImplementedError("unexpected index type: %s" % (idxty,))
ax += 1
# Fill up missing dimensions at the end
assert ax <= aryty.ndim
while ax < aryty.ndim:
output_shapes.append(shapes[ax])
output_strides.append(strides[ax])
ax += 1
# No need to check wraparound, as negative indices were already
# fixed in the loop above.
dataptr = cgutils.get_item_pointer(context, builder, aryty, ary,
output_indices,
wraparound=False, boundscheck=False)
return (dataptr, output_shapes, output_strides)
def make_view(context, builder, aryty, ary, return_type,
data, shapes, strides):
"""
Build a view over the given array with the given parameters.
"""
retary = make_array(return_type)(context, builder)
populate_array(retary,
data=data,
shape=shapes,
strides=strides,
itemsize=ary.itemsize,
meminfo=ary.meminfo,
parent=ary.parent)
return retary
def _getitem_array_generic(context, builder, return_type, aryty, ary,
index_types, indices):
"""
Return the result of indexing *ary* with the given *indices*,
returning either a scalar or a view.
"""
dataptr, view_shapes, view_strides = \
basic_indexing(context, builder, aryty, ary, index_types, indices,
boundscheck=context.enable_boundscheck)
if isinstance(return_type, types.Buffer):
# Build array view
retary = make_view(context, builder, aryty, ary, return_type,
dataptr, view_shapes, view_strides)
return retary._getvalue()
else:
# Load scalar from 0-d result
assert not view_shapes
return load_item(context, builder, aryty, dataptr)
@lower_builtin(operator.getitem, types.Buffer, types.Integer)
@lower_builtin(operator.getitem, types.Buffer, types.SliceType)
def getitem_arraynd_intp(context, builder, sig, args):
"""
Basic indexing with an integer or a slice.
"""
aryty, idxty = sig.args
ary, idx = args
assert aryty.ndim >= 1
ary = make_array(aryty)(context, builder, ary)
res = _getitem_array_generic(context, builder, sig.return_type,
aryty, ary, (idxty,), (idx,))
return impl_ret_borrowed(context, builder, sig.return_type, res)
@lower_builtin(operator.getitem, types.Buffer, types.BaseTuple)
def getitem_array_tuple(context, builder, sig, args):
"""
Basic or advanced indexing with a tuple.
"""
aryty, tupty = sig.args
ary, tup = args
ary = make_array(aryty)(context, builder, ary)
index_types = tupty.types
indices = cgutils.unpack_tuple(builder, tup, count=len(tupty))
index_types, indices = normalize_indices(context, builder,
index_types, indices)
if any(isinstance(ty, types.Array) for ty in index_types):
# Advanced indexing
return fancy_getitem(context, builder, sig, args,
aryty, ary, index_types, indices)
res = _getitem_array_generic(context, builder, sig.return_type,
aryty, ary, index_types, indices)
return impl_ret_borrowed(context, builder, sig.return_type, res)
@lower_builtin(operator.setitem, types.Buffer, types.Any, types.Any)
def setitem_array(context, builder, sig, args):
"""
array[a] = scalar_or_array
array[a,..,b] = scalar_or_array
"""
aryty, idxty, valty = sig.args
ary, idx, val = args
if isinstance(idxty, types.BaseTuple):
index_types = idxty.types
indices = cgutils.unpack_tuple(builder, idx, count=len(idxty))
else:
index_types = (idxty,)
indices = (idx,)
ary = make_array(aryty)(context, builder, ary)
# First try basic indexing to see if a single array location is denoted.
index_types, indices = normalize_indices(context, builder,
index_types, indices)
try:
dataptr, shapes, strides = \
basic_indexing(context, builder, aryty, ary, index_types, indices,
boundscheck=context.enable_boundscheck)
except NotImplementedError:
use_fancy_indexing = True
else:
use_fancy_indexing = bool(shapes)
if use_fancy_indexing:
# Index describes a non-trivial view => use generic slice assignment
# (NOTE: this also handles scalar broadcasting)
return fancy_setslice(context, builder, sig, args,
index_types, indices)
# Store source value the given location
val = context.cast(builder, val, valty, aryty.dtype)
store_item(context, builder, aryty, val, dataptr)
@lower_builtin(len, types.Buffer)
def array_len(context, builder, sig, args):
(aryty,) = sig.args
(ary,) = args
arystty = make_array(aryty)
ary = arystty(context, builder, ary)
shapeary = ary.shape
res = builder.extract_value(shapeary, 0)
return impl_ret_untracked(context, builder, sig.return_type, res)
@lower_builtin("array.item", types.Array)
def array_item(context, builder, sig, args):
aryty, = sig.args
ary, = args
ary = make_array(aryty)(context, builder, ary)
nitems = ary.nitems
with builder.if_then(builder.icmp_signed('!=', nitems, nitems.type(1)),
likely=False):
msg = "item(): can only convert an array of size 1 to a Python scalar"
context.call_conv.return_user_exc(builder, ValueError, (msg,))
return load_item(context, builder, aryty, ary.data)
if numpy_version < (2, 0):
@lower_builtin("array.itemset", types.Array, types.Any)
def array_itemset(context, builder, sig, args):
aryty, valty = sig.args
ary, val = args
assert valty == aryty.dtype
ary = make_array(aryty)(context, builder, ary)
nitems = ary.nitems
with builder.if_then(builder.icmp_signed('!=', nitems, nitems.type(1)),
likely=False):
msg = "itemset(): can only write to an array of size 1"
context.call_conv.return_user_exc(builder, ValueError, (msg,))
store_item(context, builder, aryty, val, ary.data)
return context.get_dummy_value()
# ------------------------------------------------------------------------------
# Advanced / fancy indexing
class Indexer(object):
"""
Generic indexer interface, for generating indices over a fancy indexed
array on a single dimension.
"""
def prepare(self):
"""
Prepare the indexer by initializing any required variables, basic
blocks...
"""
raise NotImplementedError
def get_size(self):
"""
Return this dimension's size as an integer.
"""
raise NotImplementedError
def get_shape(self):
"""
Return this dimension's shape as a tuple.
"""
raise NotImplementedError
def get_index_bounds(self):
"""
Return a half-open [lower, upper) range of indices this dimension
is guaranteed not to step out of.
"""
raise NotImplementedError
def loop_head(self):
"""
Start indexation loop. Return a (index, count) tuple.
*index* is an integer LLVM value representing the index over this
dimension.
*count* is either an integer LLVM value representing the current
iteration count, or None if this dimension should be omitted from
the indexation result.
"""
raise NotImplementedError
def loop_tail(self):
"""
Finish indexation loop.
"""
raise NotImplementedError
class EntireIndexer(Indexer):
"""
Compute indices along an entire array dimension.
"""
def __init__(self, context, builder, aryty, ary, dim):
self.context = context
self.builder = builder
self.aryty = aryty
self.ary = ary
self.dim = dim
self.ll_intp = self.context.get_value_type(types.intp)
def prepare(self):
builder = self.builder
self.size = builder.extract_value(self.ary.shape, self.dim)
self.index = cgutils.alloca_once(builder, self.ll_intp)
self.bb_start = builder.append_basic_block()
self.bb_end = builder.append_basic_block()
def get_size(self):
return self.size
def get_shape(self):
return (self.size,)
def get_index_bounds(self):
# [0, size)
return (self.ll_intp(0), self.size)
def loop_head(self):
builder = self.builder
# Initialize loop variable
self.builder.store(Constant(self.ll_intp, 0), self.index)
builder.branch(self.bb_start)
builder.position_at_end(self.bb_start)
cur_index = builder.load(self.index)
with builder.if_then(builder.icmp_signed('>=', cur_index, self.size),
likely=False):
builder.branch(self.bb_end)
return cur_index, cur_index
def loop_tail(self):
builder = self.builder
next_index = cgutils.increment_index(builder, builder.load(self.index))
builder.store(next_index, self.index)
builder.branch(self.bb_start)
builder.position_at_end(self.bb_end)
class IntegerIndexer(Indexer):
"""
Compute indices from a single integer.
"""
def __init__(self, context, builder, idx):
self.context = context
self.builder = builder
self.idx = idx
self.ll_intp = self.context.get_value_type(types.intp)
def prepare(self):
pass
def get_size(self):
return Constant(self.ll_intp, 1)
def get_shape(self):
return ()
def get_index_bounds(self):
# [idx, idx+1)
return (self.idx, self.builder.add(self.idx, self.get_size()))
def loop_head(self):
return self.idx, None
def loop_tail(self):
pass
class IntegerArrayIndexer(Indexer):
"""
Compute indices from an array of integer indices.
"""
def __init__(self, context, builder, idxty, idxary, size):
self.context = context
self.builder = builder
self.idxty = idxty
self.idxary = idxary
self.size = size
assert idxty.ndim == 1
self.ll_intp = self.context.get_value_type(types.intp)
def prepare(self):
builder = self.builder
self.idx_size = cgutils.unpack_tuple(builder, self.idxary.shape)[0]
self.idx_index = cgutils.alloca_once(builder, self.ll_intp)
self.bb_start = builder.append_basic_block()
self.bb_end = builder.append_basic_block()
def get_size(self):
return self.idx_size
def get_shape(self):
return (self.idx_size,)
def get_index_bounds(self):
# Pessimal heuristic, as we don't want to scan for the min and max
return (self.ll_intp(0), self.size)
def loop_head(self):
builder = self.builder
# Initialize loop variable
self.builder.store(Constant(self.ll_intp, 0), self.idx_index)
builder.branch(self.bb_start)
builder.position_at_end(self.bb_start)
cur_index = builder.load(self.idx_index)
with builder.if_then(
builder.icmp_signed('>=', cur_index, self.idx_size),
likely=False
):
builder.branch(self.bb_end)
# Load the actual index from the array of indices
index = _getitem_array_single_int(
self.context, builder, self.idxty.dtype, self.idxty, self.idxary,
cur_index
)
index = fix_integer_index(self.context, builder,
self.idxty.dtype, index, self.size)
return index, cur_index
def loop_tail(self):
builder = self.builder
next_index = cgutils.increment_index(builder,
builder.load(self.idx_index))
builder.store(next_index, self.idx_index)
builder.branch(self.bb_start)
builder.position_at_end(self.bb_end)
class BooleanArrayIndexer(Indexer):
"""
Compute indices from an array of boolean predicates.
"""
def __init__(self, context, builder, idxty, idxary):
self.context = context
self.builder = builder
self.idxty = idxty
self.idxary = idxary
assert idxty.ndim == 1
self.ll_intp = self.context.get_value_type(types.intp)
self.zero = Constant(self.ll_intp, 0)
def prepare(self):
builder = self.builder
self.size = cgutils.unpack_tuple(builder, self.idxary.shape)[0]
self.idx_index = cgutils.alloca_once(builder, self.ll_intp)
self.count = cgutils.alloca_once(builder, self.ll_intp)
self.bb_start = builder.append_basic_block()
self.bb_tail = builder.append_basic_block()
self.bb_end = builder.append_basic_block()
def get_size(self):
builder = self.builder
count = cgutils.alloca_once_value(builder, self.zero)
# Sum all true values
with cgutils.for_range(builder, self.size) as loop:
c = builder.load(count)
pred = _getitem_array_single_int(
self.context, builder, self.idxty.dtype,
self.idxty, self.idxary, loop.index
)
c = builder.add(c, builder.zext(pred, c.type))
builder.store(c, count)
return builder.load(count)
def get_shape(self):
return (self.get_size(),)
def get_index_bounds(self):
# Pessimal heuristic, as we don't want to scan for the
# first and last true items
return (self.ll_intp(0), self.size)
def loop_head(self):
builder = self.builder
# Initialize loop variable
self.builder.store(self.zero, self.idx_index)
self.builder.store(self.zero, self.count)
builder.branch(self.bb_start)
builder.position_at_end(self.bb_start)
cur_index = builder.load(self.idx_index)
cur_count = builder.load(self.count)
with builder.if_then(builder.icmp_signed('>=', cur_index, self.size),
likely=False):
builder.branch(self.bb_end)
# Load the predicate and branch if false
pred = _getitem_array_single_int(
self.context, builder, self.idxty.dtype, self.idxty, self.idxary,
cur_index
)
with builder.if_then(builder.not_(pred)):
builder.branch(self.bb_tail)
# Increment the count for next iteration
next_count = cgutils.increment_index(builder, cur_count)
builder.store(next_count, self.count)
return cur_index, cur_count
def loop_tail(self):
builder = self.builder
builder.branch(self.bb_tail)
builder.position_at_end(self.bb_tail)
next_index = cgutils.increment_index(builder,
builder.load(self.idx_index))
builder.store(next_index, self.idx_index)
builder.branch(self.bb_start)
builder.position_at_end(self.bb_end)
class SliceIndexer(Indexer):
"""
Compute indices along a slice.
"""
def __init__(self, context, builder, aryty, ary, dim, idxty, slice):
self.context = context
self.builder = builder
self.aryty = aryty
self.ary = ary
self.dim = dim
self.idxty = idxty
self.slice = slice
self.ll_intp = self.context.get_value_type(types.intp)
self.zero = Constant(self.ll_intp, 0)
def prepare(self):
builder = self.builder
# Fix slice for the dimension's size
self.dim_size = builder.extract_value(self.ary.shape, self.dim)
slicing.guard_invalid_slice(self.context, builder, self.idxty,
self.slice)
slicing.fix_slice(builder, self.slice, self.dim_size)
self.is_step_negative = cgutils.is_neg_int(builder, self.slice.step)
# Create loop entities
self.index = cgutils.alloca_once(builder, self.ll_intp)
self.count = cgutils.alloca_once(builder, self.ll_intp)
self.bb_start = builder.append_basic_block()
self.bb_end = builder.append_basic_block()
def get_size(self):
return slicing.get_slice_length(self.builder, self.slice)
def get_shape(self):
return (self.get_size(),)
def get_index_bounds(self):
lower, upper = slicing.get_slice_bounds(self.builder, self.slice)
return lower, upper
def loop_head(self):
builder = self.builder
# Initialize loop variable
self.builder.store(self.slice.start, self.index)
self.builder.store(self.zero, self.count)
builder.branch(self.bb_start)
builder.position_at_end(self.bb_start)
cur_index = builder.load(self.index)
cur_count = builder.load(self.count)
is_finished = builder.select(self.is_step_negative,
builder.icmp_signed('<=', cur_index,
self.slice.stop),
builder.icmp_signed('>=', cur_index,
self.slice.stop))
with builder.if_then(is_finished, likely=False):
builder.branch(self.bb_end)
return cur_index, cur_count
def loop_tail(self):
builder = self.builder
next_index = builder.add(builder.load(self.index), self.slice.step,
flags=['nsw'])
builder.store(next_index, self.index)
next_count = cgutils.increment_index(builder, builder.load(self.count))
builder.store(next_count, self.count)
builder.branch(self.bb_start)
builder.position_at_end(self.bb_end)
class FancyIndexer(object):
"""
Perform fancy indexing on the given array.
"""
def __init__(self, context, builder, aryty, ary, index_types, indices):
self.context = context
self.builder = builder
self.aryty = aryty
self.shapes = cgutils.unpack_tuple(builder, ary.shape, aryty.ndim)
self.strides = cgutils.unpack_tuple(builder, ary.strides, aryty.ndim)
self.ll_intp = self.context.get_value_type(types.intp)
self.newaxes = []
indexers = []
num_newaxes = len([idx for idx in index_types if is_nonelike(idx)])
ax = 0 # keeps track of position of original axes
new_ax = 0 # keeps track of position for inserting new axes
for indexval, idxty in zip(indices, index_types):
if idxty is types.ellipsis:
# Fill up missing dimensions at the middle
n_missing = aryty.ndim - len(indices) + 1 + num_newaxes
for i in range(n_missing):
indexer = EntireIndexer(context, builder, aryty, ary, ax)
indexers.append(indexer)
ax += 1
new_ax += 1
continue
# Regular index value
if isinstance(idxty, types.SliceType):
slice = context.make_helper(builder, idxty, indexval)
indexer = SliceIndexer(context, builder, aryty, ary, ax,
idxty, slice)
indexers.append(indexer)
elif isinstance(idxty, types.Integer):
ind = fix_integer_index(context, builder, idxty, indexval,
self.shapes[ax])
indexer = IntegerIndexer(context, builder, ind)
indexers.append(indexer)
elif isinstance(idxty, types.Array):
idxary = make_array(idxty)(context, builder, indexval)
if isinstance(idxty.dtype, types.Integer):
indexer = IntegerArrayIndexer(context, builder,
idxty, idxary,
self.shapes[ax])
elif isinstance(idxty.dtype, types.Boolean):
indexer = BooleanArrayIndexer(context, builder,
idxty, idxary)
else:
assert 0
indexers.append(indexer)
elif is_nonelike(idxty):
self.newaxes.append(new_ax)
ax -= 1
else:
raise AssertionError("unexpected index type: %s" % (idxty,))
ax += 1
new_ax += 1
# Fill up missing dimensions at the end
assert ax <= aryty.ndim, (ax, aryty.ndim)
while ax < aryty.ndim:
indexer = EntireIndexer(context, builder, aryty, ary, ax)
indexers.append(indexer)
ax += 1
assert len(indexers) == aryty.ndim, (len(indexers), aryty.ndim)
self.indexers = indexers
def prepare(self):
for i in self.indexers:
i.prepare()
one = self.context.get_constant(types.intp, 1)
# Compute the resulting shape given by the indices
res_shape = [i.get_shape() for i in self.indexers]
# At every position where newaxis/None is present insert
# one as a constant shape in the resulting list of shapes.
for i in self.newaxes:
res_shape.insert(i, (one,))
# Store the shape as a tuple, we can't do a simple
# tuple(res_shape) here since res_shape is a list
# of tuples which may be differently sized.
self.indexers_shape = sum(res_shape, ())
def get_shape(self):
"""
Get the resulting data shape as Python tuple.
"""
return self.indexers_shape
def get_offset_bounds(self, strides, itemsize):
"""
Get a half-open [lower, upper) range of byte offsets spanned by
the indexer with the given strides and itemsize. The indexer is
guaranteed to not go past those bounds.
"""
assert len(strides) == self.aryty.ndim
builder = self.builder
is_empty = cgutils.false_bit
zero = self.ll_intp(0)
one = self.ll_intp(1)
lower = zero
upper = zero
for indexer, shape, stride in zip(self.indexers, self.indexers_shape,
strides):
is_empty = builder.or_(is_empty,
builder.icmp_unsigned('==', shape, zero))
# Compute [lower, upper) indices on this dimension
lower_index, upper_index = indexer.get_index_bounds()
lower_offset = builder.mul(stride, lower_index)
upper_offset = builder.mul(stride, builder.sub(upper_index, one))
# Adjust total interval
is_downwards = builder.icmp_signed('<', stride, zero)
lower = builder.add(lower,
builder.select(is_downwards,
upper_offset,
lower_offset))
upper = builder.add(upper,
builder.select(is_downwards,
lower_offset,
upper_offset))
# Make interval half-open
upper = builder.add(upper, itemsize)
# Adjust for empty shape
lower = builder.select(is_empty, zero, lower)
upper = builder.select(is_empty, zero, upper)
return lower, upper
def begin_loops(self):
indices, counts = zip(*(i.loop_head() for i in self.indexers))
return indices, counts
def end_loops(self):
for i in reversed(self.indexers):
i.loop_tail()
def fancy_getitem(context, builder, sig, args,
aryty, ary, index_types, indices):
shapes = cgutils.unpack_tuple(builder, ary.shape)
strides = cgutils.unpack_tuple(builder, ary.strides)
data = ary.data
indexer = FancyIndexer(context, builder, aryty, ary,
index_types, indices)
indexer.prepare()
# Construct output array
out_ty = sig.return_type
out_shapes = indexer.get_shape()
out = _empty_nd_impl(context, builder, out_ty, out_shapes)
out_data = out.data
out_idx = cgutils.alloca_once_value(builder,
context.get_constant(types.intp, 0))
# Loop on source and copy to destination
indices, _ = indexer.begin_loops()
# No need to check for wraparound, as the indexers all ensure
# a positive index is returned.
ptr = cgutils.get_item_pointer2(context, builder, data, shapes, strides,
aryty.layout, indices, wraparound=False,
boundscheck=context.enable_boundscheck)
val = load_item(context, builder, aryty, ptr)
# Since the destination is C-contiguous, no need for multi-dimensional
# indexing.
cur = builder.load(out_idx)
ptr = builder.gep(out_data, [cur])
store_item(context, builder, out_ty, val, ptr)
next_idx = cgutils.increment_index(builder, cur)
builder.store(next_idx, out_idx)
indexer.end_loops()
return impl_ret_new_ref(context, builder, out_ty, out._getvalue())
@lower_builtin(operator.getitem, types.Buffer, types.Array)
def fancy_getitem_array(context, builder, sig, args):
"""
Advanced or basic indexing with an array.
"""
aryty, idxty = sig.args
ary, idx = args
ary = make_array(aryty)(context, builder, ary)
if idxty.ndim == 0:
# 0-d array index acts as a basic integer index
idxty, idx = normalize_index(context, builder, idxty, idx)
res = _getitem_array_generic(context, builder, sig.return_type,
aryty, ary, (idxty,), (idx,))
return impl_ret_borrowed(context, builder, sig.return_type, res)
else:
# Advanced indexing
return fancy_getitem(context, builder, sig, args,
aryty, ary, (idxty,), (idx,))
def offset_bounds_from_strides(context, builder, arrty, arr, shapes, strides):
"""
Compute a half-open range [lower, upper) of byte offsets from the
array's data pointer, that bound the in-memory extent of the array.
This mimics offset_bounds_from_strides() from
numpy/core/src/private/mem_overlap.c
"""
itemsize = arr.itemsize
zero = itemsize.type(0)
one = zero.type(1)
if arrty.layout in 'CF':
# Array is contiguous: contents are laid out sequentially
# starting from arr.data and upwards
lower = zero
upper = builder.mul(itemsize, arr.nitems)
else:
# Non-contiguous array: need to examine strides
lower = zero
upper = zero
for i in range(arrty.ndim):
# Compute the largest byte offset on this dimension
# max_axis_offset = strides[i] * (shapes[i] - 1)
# (shapes[i] == 0 is catered for by the empty array case below)
max_axis_offset = builder.mul(strides[i],
builder.sub(shapes[i], one))
is_upwards = builder.icmp_signed('>=', max_axis_offset, zero)
# Expand either upwards or downwards depending on stride
upper = builder.select(is_upwards,
builder.add(upper, max_axis_offset), upper)
lower = builder.select(is_upwards,
lower, builder.add(lower, max_axis_offset))
# Return a half-open range
upper = builder.add(upper, itemsize)
# Adjust for empty arrays
is_empty = builder.icmp_signed('==', arr.nitems, zero)
upper = builder.select(is_empty, zero, upper)
lower = builder.select(is_empty, zero, lower)
return lower, upper
def compute_memory_extents(context, builder, lower, upper, data):
"""
Given [lower, upper) byte offsets and a base data pointer,
compute the memory pointer bounds as pointer-sized integers.
"""
data_ptr_as_int = builder.ptrtoint(data, lower.type)
start = builder.add(data_ptr_as_int, lower)
end = builder.add(data_ptr_as_int, upper)
return start, end
def get_array_memory_extents(context, builder, arrty, arr, shapes, strides,
data):
"""
Compute a half-open range [start, end) of pointer-sized integers
which fully contain the array data.
"""
lower, upper = offset_bounds_from_strides(context, builder, arrty, arr,
shapes, strides)
return compute_memory_extents(context, builder, lower, upper, data)
def extents_may_overlap(context, builder, a_start, a_end, b_start, b_end):
"""
Whether two memory extents [a_start, a_end) and [b_start, b_end)
may overlap.
"""
# Comparisons are unsigned, since we are really comparing pointers
may_overlap = builder.and_(
builder.icmp_unsigned('<', a_start, b_end),
builder.icmp_unsigned('<', b_start, a_end),
)
return may_overlap
def maybe_copy_source(context, builder, use_copy,
srcty, src, src_shapes, src_strides, src_data):
ptrty = src_data.type
copy_layout = 'C'
copy_data = cgutils.alloca_once_value(builder, src_data)
copy_shapes = src_shapes
copy_strides = None # unneeded for contiguous arrays
with builder.if_then(use_copy, likely=False):
# Allocate temporary scratchpad
# XXX: should we use a stack-allocated array for very small
# data sizes?
allocsize = builder.mul(src.itemsize, src.nitems)
data = context.nrt.allocate(builder, allocsize)
voidptrty = data.type
data = builder.bitcast(data, ptrty)
builder.store(data, copy_data)
# Copy source data into scratchpad
intp_t = context.get_value_type(types.intp)
with cgutils.loop_nest(builder, src_shapes, intp_t) as indices:
src_ptr = cgutils.get_item_pointer2(context, builder, src_data,
src_shapes, src_strides,
srcty.layout, indices)
dest_ptr = cgutils.get_item_pointer2(context, builder, data,
copy_shapes, copy_strides,
copy_layout, indices)
builder.store(builder.load(src_ptr), dest_ptr)
def src_getitem(source_indices):
src_ptr = cgutils.alloca_once(builder, ptrty)
with builder.if_else(use_copy, likely=False) as (if_copy, otherwise):
with if_copy:
builder.store(
cgutils.get_item_pointer2(context, builder,
builder.load(copy_data),
copy_shapes, copy_strides,
copy_layout, source_indices,
wraparound=False),
src_ptr)
with otherwise:
builder.store(
cgutils.get_item_pointer2(context, builder, src_data,
src_shapes, src_strides,
srcty.layout, source_indices,
wraparound=False),
src_ptr)
return load_item(context, builder, srcty, builder.load(src_ptr))
def src_cleanup():
# Deallocate memory
with builder.if_then(use_copy, likely=False):
data = builder.load(copy_data)
data = builder.bitcast(data, voidptrty)
context.nrt.free(builder, data)
return src_getitem, src_cleanup
def _bc_adjust_dimension(context, builder, shapes, strides, target_shape):
"""
Preprocess dimension for broadcasting.
Returns (shapes, strides) such that the ndim match *target_shape*.
When expanding to higher ndim, the returning shapes and strides are
prepended with ones and zeros, respectively.
When truncating to lower ndim, the shapes are checked (in runtime).
All extra dimension must have size of 1.
"""
zero = context.get_constant(types.uintp, 0)
one = context.get_constant(types.uintp, 1)
# Adjust for broadcasting to higher dimension
if len(target_shape) > len(shapes):
nd_diff = len(target_shape) - len(shapes)
# Fill missing shapes with one, strides with zeros
shapes = [one] * nd_diff + shapes
strides = [zero] * nd_diff + strides
# Adjust for broadcasting to lower dimension
elif len(target_shape) < len(shapes):
# Accepted if all extra dims has shape 1
nd_diff = len(shapes) - len(target_shape)
dim_is_one = [builder.icmp_unsigned('==', sh, one)
for sh in shapes[:nd_diff]]
accepted = functools.reduce(builder.and_, dim_is_one,
cgutils.true_bit)
# Check error
with builder.if_then(builder.not_(accepted), likely=False):
msg = "cannot broadcast source array for assignment"
context.call_conv.return_user_exc(builder, ValueError, (msg,))
# Truncate extra shapes, strides
shapes = shapes[nd_diff:]
strides = strides[nd_diff:]
return shapes, strides
def _bc_adjust_shape_strides(context, builder, shapes, strides, target_shape):
"""
Broadcast shapes and strides to target_shape given that their ndim already
matches. For each location where the shape is 1 and does not match the
dim for target, it is set to the value at the target and the stride is
set to zero.
"""
bc_shapes = []
bc_strides = []
zero = context.get_constant(types.uintp, 0)
one = context.get_constant(types.uintp, 1)
# Adjust all mismatching ones in shape
mismatch = [builder.icmp_signed('!=', tar, old)
for tar, old in zip(target_shape, shapes)]
src_is_one = [builder.icmp_signed('==', old, one) for old in shapes]
preds = [builder.and_(x, y) for x, y in zip(mismatch, src_is_one)]
bc_shapes = [builder.select(p, tar, old)
for p, tar, old in zip(preds, target_shape, shapes)]
bc_strides = [builder.select(p, zero, old)
for p, old in zip(preds, strides)]
return bc_shapes, bc_strides
def _broadcast_to_shape(context, builder, arrtype, arr, target_shape):
"""
Broadcast the given array to the target_shape.
Returns (array_type, array)
"""
# Compute broadcasted shape and strides
shapes = cgutils.unpack_tuple(builder, arr.shape)
strides = cgutils.unpack_tuple(builder, arr.strides)
shapes, strides = _bc_adjust_dimension(context, builder, shapes, strides,
target_shape)
shapes, strides = _bc_adjust_shape_strides(context, builder, shapes,
strides, target_shape)
new_arrtype = arrtype.copy(ndim=len(target_shape), layout='A')
# Create new view
new_arr = make_array(new_arrtype)(context, builder)
populate_array(new_arr,
data=arr.data,
shape=cgutils.pack_array(builder, shapes),
strides=cgutils.pack_array(builder, strides),
itemsize=arr.itemsize,
meminfo=arr.meminfo,
parent=arr.parent)
return new_arrtype, new_arr
@intrinsic
def _numpy_broadcast_to(typingctx, array, shape):
ret = array.copy(ndim=shape.count, layout='A', readonly=True)
sig = ret(array, shape)
def codegen(context, builder, sig, args):
src, shape_ = args
srcty = sig.args[0]
src = make_array(srcty)(context, builder, src)
shape_ = cgutils.unpack_tuple(builder, shape_)
_, dest = _broadcast_to_shape(context, builder, srcty, src, shape_,)
# Hack to get np.broadcast_to to return a read-only array
setattr(dest, 'parent', Constant(
context.get_value_type(dest._datamodel.get_type('parent')),
None))
res = dest._getvalue()
return impl_ret_borrowed(context, builder, sig.return_type, res)
return sig, codegen
@intrinsic
def get_readonly_array(typingctx, arr):
# returns a copy of arr which is readonly
ret = arr.copy(readonly=True)
sig = ret(arr)
def codegen(context, builder, sig, args):
[src] = args
srcty = sig.args[0]
dest = make_array(srcty)(context, builder, src)
# Hack to return a read-only array
dest.parent = cgutils.get_null_value(dest.parent.type)
res = dest._getvalue()
return impl_ret_borrowed(context, builder, sig.return_type, res)
return sig, codegen
@register_jitable
def _can_broadcast(array, dest_shape):
src_shape = array.shape
src_ndim = len(src_shape)
dest_ndim = len(dest_shape)
if src_ndim > dest_ndim:
raise ValueError('input operand has more dimensions than allowed '
'by the axis remapping')
for size in dest_shape:
if size < 0:
raise ValueError('all elements of broadcast shape must be '
'non-negative')
# based on _broadcast_onto function in numba/np/npyimpl.py
src_index = 0
dest_index = dest_ndim - src_ndim
while src_index < src_ndim:
src_dim = src_shape[src_index]
dest_dim = dest_shape[dest_index]
# possible cases for (src_dim, dest_dim):
# * (1, 1) -> Ok
# * (>1, 1) -> Error!
# * (>1, >1) -> src_dim == dest_dim else error!
# * (1, >1) -> Ok
if src_dim == dest_dim or src_dim == 1:
src_index += 1
dest_index += 1
else:
raise ValueError('operands could not be broadcast together '
'with remapped shapes')
def _default_broadcast_to_impl(array, shape):
array = np.asarray(array)
_can_broadcast(array, shape)
return _numpy_broadcast_to(array, shape)
@overload(np.broadcast_to)
def numpy_broadcast_to(array, shape):
if not type_can_asarray(array):
raise errors.TypingError('The first argument "array" must '
'be array-like')
if isinstance(shape, types.Integer):
def impl(array, shape):
return np.broadcast_to(array, (shape,))
return impl
elif isinstance(shape, types.UniTuple):
if not isinstance(shape.dtype, types.Integer):
msg = 'The second argument "shape" must be a tuple of integers'
raise errors.TypingError(msg)
return _default_broadcast_to_impl
elif isinstance(shape, types.Tuple) and shape.count > 0:
# check if all types are integers
if not all([isinstance(typ, types.IntegerLiteral) for typ in shape]):
msg = f'"{shape}" object cannot be interpreted as an integer'
raise errors.TypingError(msg)
return _default_broadcast_to_impl
elif isinstance(shape, types.Tuple) and shape.count == 0:
is_scalar_array = isinstance(array, types.Array) and array.ndim == 0
if type_is_scalar(array) or is_scalar_array:
def impl(array, shape): # broadcast_to(array, ())
# Array type must be supported by "type_can_asarray"
# Quick note that unicode types are not supported!
array = np.asarray(array)
return get_readonly_array(array)
return impl
else:
msg = 'Cannot broadcast a non-scalar to a scalar array'
raise errors.TypingError(msg)
else:
msg = ('The argument "shape" must be a tuple or an integer. '
'Got %s' % shape)
raise errors.TypingError(msg)
@register_jitable
def numpy_broadcast_shapes_list(r, m, shape):
for i in range(len(shape)):
k = m - len(shape) + i
tmp = shape[i]
if tmp < 0:
raise ValueError("negative dimensions are not allowed")
if tmp == 1:
continue
if r[k] == 1:
r[k] = tmp
elif r[k] != tmp:
raise ValueError("shape mismatch: objects"
" cannot be broadcast"
" to a single shape")
@overload(np.broadcast_shapes)
def ol_numpy_broadcast_shapes(*args):
# Based on https://github.com/numpy/numpy/blob/f702b26fff3271ba6a6ba29a021fc19051d1f007/numpy/core/src/multiarray/iterators.c#L1129-L1212 # noqa
for idx, arg in enumerate(args):
is_int = isinstance(arg, types.Integer)
is_int_tuple = isinstance(arg, types.UniTuple) and \
isinstance(arg.dtype, types.Integer)
is_empty_tuple = isinstance(arg, types.Tuple) and len(arg.types) == 0
if not (is_int or is_int_tuple or is_empty_tuple):
msg = (f'Argument {idx} must be either an int or tuple[int]. '
f'Got {arg}')
raise errors.TypingError(msg)
# discover the number of dimensions
m = 0
for arg in args:
if isinstance(arg, types.Integer):
m = max(m, 1)
elif isinstance(arg, types.BaseTuple):
m = max(m, len(arg))
if m == 0:
return lambda *args: ()
else:
tup_init = (1,) * m
def impl(*args):
# propagate args
r = [1] * m
tup = tup_init
for arg in literal_unroll(args):
if isinstance(arg, tuple) and len(arg) > 0:
numpy_broadcast_shapes_list(r, m, arg)
elif isinstance(arg, int):
numpy_broadcast_shapes_list(r, m, (arg,))
for idx, elem in enumerate(r):
tup = tuple_setitem(tup, idx, elem)
return tup
return impl
@overload(np.broadcast_arrays)
def numpy_broadcast_arrays(*args):
for idx, arg in enumerate(args):
if not type_can_asarray(arg):
raise errors.TypingError(f'Argument "{idx}" must '
'be array-like')
unified_dtype = None
dt = None
for arg in args:
if isinstance(arg, (types.Array, types.BaseTuple)):
dt = arg.dtype
else:
dt = arg
if unified_dtype is None:
unified_dtype = dt
elif unified_dtype != dt:
raise errors.TypingError('Mismatch of argument types. Numba cannot '
'broadcast arrays with different types. '
f'Got {args}')
# number of dimensions
m = 0
for idx, arg in enumerate(args):
if isinstance(arg, types.ArrayCompatible):
m = max(m, arg.ndim)
elif isinstance(arg, (types.Number, types.Boolean, types.BaseTuple)):
m = max(m, 1)
else:
raise errors.TypingError(f'Unhandled type {arg}')
tup_init = (0,) * m
def impl(*args):
# find out the output shape
# we can't call np.broadcast_shapes here since args may have arrays
# with different shapes and it is not possible to create a list
# with those shapes dynamically
shape = [1] * m
for array in literal_unroll(args):
numpy_broadcast_shapes_list(shape, m, np.asarray(array).shape)
tup = tup_init
for i in range(m):
tup = tuple_setitem(tup, i, shape[i])
# numpy checks if the input arrays have the same shape as `shape`
outs = []
for array in literal_unroll(args):
outs.append(np.broadcast_to(np.asarray(array), tup))
return outs
return impl
def fancy_setslice(context, builder, sig, args, index_types, indices):
"""
Implement slice assignment for arrays. This implementation works for
basic as well as fancy indexing, since there's no functional difference
between the two for indexed assignment.
"""
aryty, _, srcty = sig.args
ary, _, src = args
ary = make_array(aryty)(context, builder, ary)
dest_shapes = cgutils.unpack_tuple(builder, ary.shape)
dest_strides = cgutils.unpack_tuple(builder, ary.strides)
dest_data = ary.data
indexer = FancyIndexer(context, builder, aryty, ary,
index_types, indices)
indexer.prepare()
if isinstance(srcty, types.Buffer):
# Source is an array
src_dtype = srcty.dtype
index_shape = indexer.get_shape()
src = make_array(srcty)(context, builder, src)
# Broadcast source array to shape
srcty, src = _broadcast_to_shape(context, builder, srcty, src,
index_shape)
src_shapes = cgutils.unpack_tuple(builder, src.shape)
src_strides = cgutils.unpack_tuple(builder, src.strides)
src_data = src.data
# Check shapes are equal
shape_error = cgutils.false_bit
assert len(index_shape) == len(src_shapes)
for u, v in zip(src_shapes, index_shape):
shape_error = builder.or_(shape_error,
builder.icmp_signed('!=', u, v))
with builder.if_then(shape_error, likely=False):
msg = "cannot assign slice from input of different size"
context.call_conv.return_user_exc(builder, ValueError, (msg,))
# Check for array overlap
src_start, src_end = get_array_memory_extents(context, builder, srcty,
src, src_shapes,
src_strides, src_data)
dest_lower, dest_upper = indexer.get_offset_bounds(dest_strides,
ary.itemsize)
dest_start, dest_end = compute_memory_extents(context, builder,
dest_lower, dest_upper,
dest_data)
use_copy = extents_may_overlap(context, builder, src_start, src_end,
dest_start, dest_end)
src_getitem, src_cleanup = maybe_copy_source(context, builder, use_copy,
srcty, src, src_shapes,
src_strides, src_data)
elif isinstance(srcty, types.Sequence):
src_dtype = srcty.dtype
# Check shape is equal to sequence length
index_shape = indexer.get_shape()
assert len(index_shape) == 1
len_impl = context.get_function(len, signature(types.intp, srcty))
seq_len = len_impl(builder, (src,))
shape_error = builder.icmp_signed('!=', index_shape[0], seq_len)
with builder.if_then(shape_error, likely=False):
msg = "cannot assign slice from input of different size"
context.call_conv.return_user_exc(builder, ValueError, (msg,))
def src_getitem(source_indices):
idx, = source_indices
getitem_impl = context.get_function(
operator.getitem,
signature(src_dtype, srcty, types.intp),
)
return getitem_impl(builder, (src, idx))
def src_cleanup():
pass
else:
# Source is a scalar (broadcast or not, depending on destination
# shape).
src_dtype = srcty
def src_getitem(source_indices):
return src
def src_cleanup():
pass
zero = context.get_constant(types.uintp, 0)
# Loop on destination and copy from source to destination
dest_indices, counts = indexer.begin_loops()
# Source is iterated in natural order
# Counts represent a counter for the number of times a specified axis
# is being accessed, during setitem they are used as source
# indices
counts = list(counts)
# We need to artifically introduce the index zero wherever a
# newaxis is present within the indexer. These always remain
# zero.
for i in indexer.newaxes:
counts.insert(i, zero)
source_indices = [c for c in counts if c is not None]
val = src_getitem(source_indices)
# Cast to the destination dtype (cross-dtype slice assignment is allowed)
val = context.cast(builder, val, src_dtype, aryty.dtype)
# No need to check for wraparound, as the indexers all ensure
# a positive index is returned.
dest_ptr = cgutils.get_item_pointer2(context, builder, dest_data,
dest_shapes, dest_strides,
aryty.layout, dest_indices,
wraparound=False,
boundscheck=context.enable_boundscheck)
store_item(context, builder, aryty, val, dest_ptr)
indexer.end_loops()
src_cleanup()
return context.get_dummy_value()
# ------------------------------------------------------------------------------
# Shape / layout altering
def vararg_to_tuple(context, builder, sig, args):
aryty = sig.args[0]
dimtys = sig.args[1:]
# values
ary = args[0]
dims = args[1:]
# coerce all types to intp
dims = [context.cast(builder, val, ty, types.intp)
for ty, val in zip(dimtys, dims)]
# make a tuple
shape = cgutils.pack_array(builder, dims, dims[0].type)
shapety = types.UniTuple(dtype=types.intp, count=len(dims))
new_sig = typing.signature(sig.return_type, aryty, shapety)
new_args = ary, shape
return new_sig, new_args
@lower_builtin('array.transpose', types.Array)
def array_transpose(context, builder, sig, args):
return array_T(context, builder, sig.args[0], args[0])
def permute_arrays(axis, shape, strides):
if len(axis) != len(set(axis)):
raise ValueError("repeated axis in transpose")
dim = len(shape)
for x in axis:
if x >= dim or abs(x) > dim:
raise ValueError("axis is out of bounds for array of "
"given dimension")
shape[:] = shape[axis]
strides[:] = strides[axis]
# Transposing an array involves permuting the shape and strides of the array
# based on the given axes.
@lower_builtin('array.transpose', types.Array, types.BaseTuple)
def array_transpose_tuple(context, builder, sig, args):
aryty = sig.args[0]
ary = make_array(aryty)(context, builder, args[0])
axisty, axis = sig.args[1], args[1]
num_axis, dtype = axisty.count, axisty.dtype
ll_intp = context.get_value_type(types.intp)
ll_ary_size = ir.ArrayType(ll_intp, num_axis)
# Allocate memory for axes, shapes, and strides arrays.
arys = [axis, ary.shape, ary.strides]
ll_arys = [cgutils.alloca_once(builder, ll_ary_size) for _ in arys]
# Store axes, shapes, and strides arrays to the allocated memory.
for src, dst in zip(arys, ll_arys):
builder.store(src, dst)
np_ary_ty = types.Array(dtype=dtype, ndim=1, layout='C')
np_itemsize = context.get_constant(types.intp,
context.get_abi_sizeof(ll_intp))
# Form NumPy arrays for axes, shapes, and strides arrays.
np_arys = [make_array(np_ary_ty)(context, builder) for _ in arys]
# Roughly, `np_ary = np.array(ll_ary)` for each of axes, shapes, and strides
for np_ary, ll_ary in zip(np_arys, ll_arys):
populate_array(np_ary,
data=builder.bitcast(ll_ary, ll_intp.as_pointer()),
shape=[context.get_constant(types.intp, num_axis)],
strides=[np_itemsize],
itemsize=np_itemsize,
meminfo=None)
# Pass NumPy arrays formed above to permute_arrays function that permutes
# shapes and strides based on axis contents.
context.compile_internal(builder, permute_arrays,
typing.signature(types.void,
np_ary_ty, np_ary_ty, np_ary_ty),
[a._getvalue() for a in np_arys])
# Make a new array based on permuted shape and strides and return it.
ret = make_array(sig.return_type)(context, builder)
populate_array(ret,
data=ary.data,
shape=builder.load(ll_arys[1]),
strides=builder.load(ll_arys[2]),
itemsize=ary.itemsize,
meminfo=ary.meminfo,
parent=ary.parent)
res = ret._getvalue()
return impl_ret_borrowed(context, builder, sig.return_type, res)
@lower_builtin('array.transpose', types.Array, types.VarArg(types.Any))
def array_transpose_vararg(context, builder, sig, args):
new_sig, new_args = vararg_to_tuple(context, builder, sig, args)
return array_transpose_tuple(context, builder, new_sig, new_args)
@overload(np.transpose)
def numpy_transpose(a, axes=None):
if isinstance(a, types.BaseTuple):
raise errors.UnsupportedError("np.transpose does not accept tuples")
if axes is None:
def np_transpose_impl(a, axes=None):
return a.transpose()
else:
def np_transpose_impl(a, axes=None):
return a.transpose(axes)
return np_transpose_impl
@lower_getattr(types.Array, 'T')
def array_T(context, builder, typ, value):
if typ.ndim <= 1:
res = value
else:
ary = make_array(typ)(context, builder, value)
ret = make_array(typ)(context, builder)
shapes = cgutils.unpack_tuple(builder, ary.shape, typ.ndim)
strides = cgutils.unpack_tuple(builder, ary.strides, typ.ndim)
populate_array(ret,
data=ary.data,
shape=cgutils.pack_array(builder, shapes[::-1]),
strides=cgutils.pack_array(builder, strides[::-1]),
itemsize=ary.itemsize,
meminfo=ary.meminfo,
parent=ary.parent)
res = ret._getvalue()
return impl_ret_borrowed(context, builder, typ, res)
@overload(np.logspace)
def numpy_logspace(start, stop, num=50):
if not isinstance(start, types.Number):
raise errors.TypingError('The first argument "start" must be a number')
if not isinstance(stop, types.Number):
raise errors.TypingError('The second argument "stop" must be a number')
if not isinstance(num, (int, types.Integer)):
raise errors.TypingError('The third argument "num" must be an integer')
def impl(start, stop, num=50):
y = np.linspace(start, stop, num)
return np.power(10.0, y)
return impl
@overload(np.geomspace)
def numpy_geomspace(start, stop, num=50):
if not isinstance(start, types.Number):
msg = 'The argument "start" must be a number'
raise errors.TypingError(msg)
if not isinstance(stop, types.Number):
msg = 'The argument "stop" must be a number'
raise errors.TypingError(msg)
if not isinstance(num, (int, types.Integer)):
msg = 'The argument "num" must be an integer'
raise errors.TypingError(msg)
if any(isinstance(arg, types.Complex) for arg in [start, stop]):
result_dtype = from_dtype(np.result_type(as_dtype(start),
as_dtype(stop), None))
def impl(start, stop, num=50):
if start == 0 or stop == 0:
raise ValueError('Geometric sequence cannot include zero')
start = result_dtype(start)
stop = result_dtype(stop)
both_imaginary = (start.real == 0) & (stop.real == 0)
both_negative = (np.sign(start) == -1) & (np.sign(stop) == -1)
out_sign = 1
if both_imaginary:
start = start.imag
stop = stop.imag
out_sign = 1j
if both_negative:
start = -start
stop = -stop
out_sign = -out_sign
logstart = np.log10(start)
logstop = np.log10(stop)
result = np.logspace(logstart, logstop, num)
# Make sure the endpoints match the start and stop arguments.
# This is necessary because np.exp(np.log(x)) is not necessarily
# equal to x.
if num > 0:
result[0] = start
if num > 1:
result[-1] = stop
return out_sign * result
else:
def impl(start, stop, num=50):
if start == 0 or stop == 0:
raise ValueError('Geometric sequence cannot include zero')
both_negative = (np.sign(start) == -1) & (np.sign(stop) == -1)
out_sign = 1
if both_negative:
start = -start
stop = -stop
out_sign = -out_sign
logstart = np.log10(start)
logstop = np.log10(stop)
result = np.logspace(logstart, logstop, num)
# Make sure the endpoints match the start and stop arguments.
# This is necessary because np.exp(np.log(x)) is not necessarily
# equal to x.
if num > 0:
result[0] = start
if num > 1:
result[-1] = stop
return out_sign * result
return impl
@overload(np.rot90)
def numpy_rot90(m, k=1):
# supporting axes argument it needs to be included in np.flip
if not isinstance(k, (int, types.Integer)):
raise errors.TypingError('The second argument "k" must be an integer')
if not isinstance(m, types.Array):
raise errors.TypingError('The first argument "m" must be an array')
if m.ndim < 2:
raise errors.NumbaValueError('Input must be >= 2-d.')
def impl(m, k=1):
k = k % 4
if k == 0:
return m[:]
elif k == 1:
return np.swapaxes(np.fliplr(m), 0, 1)
elif k == 2:
return np.flipud(np.fliplr(m))
elif k == 3:
return np.fliplr(np.swapaxes(m, 0, 1))
else:
raise AssertionError # unreachable
return impl
def _attempt_nocopy_reshape(context, builder, aryty, ary,
newnd, newshape, newstrides):
"""
Call into Numba_attempt_nocopy_reshape() for the given array type
and instance, and the specified new shape.
Return value is non-zero if successful, and the array pointed to
by *newstrides* will be filled up with the computed results.
"""
ll_intp = context.get_value_type(types.intp)
ll_intp_star = ll_intp.as_pointer()
ll_intc = context.get_value_type(types.intc)
fnty = ir.FunctionType(ll_intc, [
# nd, *dims, *strides
ll_intp, ll_intp_star, ll_intp_star,
# newnd, *newdims, *newstrides
ll_intp, ll_intp_star, ll_intp_star,
# itemsize, is_f_order
ll_intp, ll_intc])
fn = cgutils.get_or_insert_function(builder.module, fnty,
"numba_attempt_nocopy_reshape")
nd = ll_intp(aryty.ndim)
shape = cgutils.gep_inbounds(builder, ary._get_ptr_by_name('shape'), 0, 0)
strides = cgutils.gep_inbounds(builder, ary._get_ptr_by_name('strides'),
0, 0)
newnd = ll_intp(newnd)
newshape = cgutils.gep_inbounds(builder, newshape, 0, 0)
newstrides = cgutils.gep_inbounds(builder, newstrides, 0, 0)
is_f_order = ll_intc(0)
res = builder.call(fn, [nd, shape, strides,
newnd, newshape, newstrides,
ary.itemsize, is_f_order])
return res
def normalize_reshape_value(origsize, shape):
num_neg_value = 0
known_size = 1
for ax, s in enumerate(shape):
if s < 0:
num_neg_value += 1
neg_ax = ax
else:
known_size *= s
if num_neg_value == 0:
if origsize != known_size:
raise ValueError("total size of new array must be unchanged")
elif num_neg_value == 1:
# Infer negative dimension
if known_size == 0:
inferred = 0
ok = origsize == 0
else:
inferred = origsize // known_size
ok = origsize % known_size == 0
if not ok:
raise ValueError("total size of new array must be unchanged")
shape[neg_ax] = inferred
else:
raise ValueError("multiple negative shape values")
@lower_builtin('array.reshape', types.Array, types.BaseTuple)
def array_reshape(context, builder, sig, args):
aryty = sig.args[0]
retty = sig.return_type
shapety = sig.args[1]
shape = args[1]
ll_intp = context.get_value_type(types.intp)
ll_shape = ir.ArrayType(ll_intp, shapety.count)
ary = make_array(aryty)(context, builder, args[0])
# We will change the target shape in this slot
# (see normalize_reshape_value() below)
newshape = cgutils.alloca_once(builder, ll_shape)
builder.store(shape, newshape)
# Create a shape array pointing to the value of newshape.
# (roughly, `shape_ary = np.array(ary.shape)`)
shape_ary_ty = types.Array(dtype=shapety.dtype, ndim=1, layout='C')
shape_ary = make_array(shape_ary_ty)(context, builder)
shape_itemsize = context.get_constant(types.intp,
context.get_abi_sizeof(ll_intp))
populate_array(shape_ary,
data=builder.bitcast(newshape, ll_intp.as_pointer()),
shape=[context.get_constant(types.intp, shapety.count)],
strides=[shape_itemsize],
itemsize=shape_itemsize,
meminfo=None)
# Compute the original array size
size = ary.nitems
# Call our normalizer which will fix the shape array in case of negative
# shape value
context.compile_internal(builder, normalize_reshape_value,
typing.signature(types.void,
types.uintp, shape_ary_ty),
[size, shape_ary._getvalue()])
# Perform reshape (nocopy)
newnd = shapety.count
newstrides = cgutils.alloca_once(builder, ll_shape)
ok = _attempt_nocopy_reshape(context, builder, aryty, ary, newnd,
newshape, newstrides)
fail = builder.icmp_unsigned('==', ok, ok.type(0))
with builder.if_then(fail):
msg = "incompatible shape for array"
context.call_conv.return_user_exc(builder, NotImplementedError, (msg,))
ret = make_array(retty)(context, builder)
populate_array(ret,
data=ary.data,
shape=builder.load(newshape),
strides=builder.load(newstrides),
itemsize=ary.itemsize,
meminfo=ary.meminfo,
parent=ary.parent)
res = ret._getvalue()
return impl_ret_borrowed(context, builder, sig.return_type, res)
@lower_builtin('array.reshape', types.Array, types.VarArg(types.Any))
def array_reshape_vararg(context, builder, sig, args):
new_sig, new_args = vararg_to_tuple(context, builder, sig, args)
return array_reshape(context, builder, new_sig, new_args)
@overload(np.reshape)
def np_reshape(a, newshape):
def np_reshape_impl(a, newshape):
return a.reshape(newshape)
return np_reshape_impl
@overload(np.resize)
def numpy_resize(a, new_shape):
if not type_can_asarray(a):
msg = 'The argument "a" must be array-like'
raise errors.TypingError(msg)
if not ((isinstance(new_shape, types.UniTuple)
and
isinstance(new_shape.dtype, types.Integer))
or
isinstance(new_shape, types.Integer)):
msg = ('The argument "new_shape" must be an integer or '
'a tuple of integers')
raise errors.TypingError(msg)
def impl(a, new_shape):
a = np.asarray(a)
a = np.ravel(a)
if isinstance(new_shape, tuple):
new_size = 1
for dim_length in np.asarray(new_shape):
new_size *= dim_length
if dim_length < 0:
msg = 'All elements of `new_shape` must be non-negative'
raise ValueError(msg)
else:
if new_shape < 0:
msg2 = 'All elements of `new_shape` must be non-negative'
raise ValueError(msg2)
new_size = new_shape
if a.size == 0:
return np.zeros(new_shape).astype(a.dtype)
repeats = -(-new_size // a.size) # ceil division
res = a
for i in range(repeats - 1):
res = np.concatenate((res, a))
res = res[:new_size]
return np.reshape(res, new_shape)
return impl
@overload(np.append)
def np_append(arr, values, axis=None):
if not type_can_asarray(arr):
raise errors.TypingError('The first argument "arr" must be array-like')
if not type_can_asarray(values):
raise errors.TypingError('The second argument "values" must be '
'array-like')
if is_nonelike(axis):
def impl(arr, values, axis=None):
arr = np.ravel(np.asarray(arr))
values = np.ravel(np.asarray(values))
return np.concatenate((arr, values))
else:
if not isinstance(axis, types.Integer):
raise errors.TypingError('The third argument "axis" must be an '
'integer')
def impl(arr, values, axis=None):
return np.concatenate((arr, values), axis=axis)
return impl
@lower_builtin('array.ravel', types.Array)
def array_ravel(context, builder, sig, args):
# Only support no argument version (default order='C')
def imp_nocopy(ary):
"""No copy version"""
return ary.reshape(ary.size)
def imp_copy(ary):
"""Copy version"""
return ary.flatten()
# If the input array is C layout already, use the nocopy version
if sig.args[0].layout == 'C':
imp = imp_nocopy
# otherwise, use flatten under-the-hood
else:
imp = imp_copy
res = context.compile_internal(builder, imp, sig, args)
res = impl_ret_new_ref(context, builder, sig.return_type, res)
return res
@lower_builtin(np.ravel, types.Array)
def np_ravel(context, builder, sig, args):
def np_ravel_impl(a):
return a.ravel()
return context.compile_internal(builder, np_ravel_impl, sig, args)
@lower_builtin('array.flatten', types.Array)
def array_flatten(context, builder, sig, args):
# Only support flattening to C layout currently.
def imp(ary):
return ary.copy().reshape(ary.size)
res = context.compile_internal(builder, imp, sig, args)
res = impl_ret_new_ref(context, builder, sig.return_type, res)
return res
@register_jitable
def _np_clip_impl(a, a_min, a_max, out):
# Both a_min and a_max are numpy arrays
ret = np.empty_like(a) if out is None else out
a_b, a_min_b, a_max_b = np.broadcast_arrays(a, a_min, a_max)
for index in np.ndindex(a_b.shape):
val_a = a_b[index]
val_a_min = a_min_b[index]
val_a_max = a_max_b[index]
ret[index] = min(max(val_a, val_a_min), val_a_max)
return ret
@register_jitable
def _np_clip_impl_none(a, b, use_min, out):
for index in np.ndindex(a.shape):
val_a = a[index]
val_b = b[index]
if use_min:
out[index] = min(val_a, val_b)
else:
out[index] = max(val_a, val_b)
return out
@overload(np.clip)
def np_clip(a, a_min, a_max, out=None):
if not type_can_asarray(a):
raise errors.TypingError('The argument "a" must be array-like')
if (not isinstance(a_min, types.NoneType) and
not type_can_asarray(a_min)):
raise errors.TypingError(('The argument "a_min" must be a number '
'or an array-like'))
if (not isinstance(a_max, types.NoneType) and
not type_can_asarray(a_max)):
raise errors.TypingError('The argument "a_max" must be a number '
'or an array-like')
if not (isinstance(out, types.Array) or is_nonelike(out)):
msg = 'The argument "out" must be an array if it is provided'
raise errors.TypingError(msg)
# TODO: support scalar a (issue #3469)
a_min_is_none = a_min is None or isinstance(a_min, types.NoneType)
a_max_is_none = a_max is None or isinstance(a_max, types.NoneType)
if a_min_is_none and a_max_is_none:
# Raises value error when both a_min and a_max are None
def np_clip_nn(a, a_min, a_max, out=None):
raise ValueError("array_clip: must set either max or min")
return np_clip_nn
a_min_is_scalar = isinstance(a_min, types.Number)
a_max_is_scalar = isinstance(a_max, types.Number)
if a_min_is_scalar and a_max_is_scalar:
def np_clip_ss(a, a_min, a_max, out=None):
# a_min and a_max are scalars
# since their shape will be empty
# so broadcasting is not needed at all
ret = np.empty_like(a) if out is None else out
for index in np.ndindex(a.shape):
val_a = a[index]
ret[index] = min(max(val_a, a_min), a_max)
return ret
return np_clip_ss
elif a_min_is_scalar and not a_max_is_scalar:
if a_max_is_none:
def np_clip_sn(a, a_min, a_max, out=None):
# a_min is a scalar
# since its shape will be empty
# so broadcasting is not needed at all
ret = np.empty_like(a) if out is None else out
for index in np.ndindex(a.shape):
val_a = a[index]
ret[index] = max(val_a, a_min)
return ret
return np_clip_sn
else:
def np_clip_sa(a, a_min, a_max, out=None):
# a_min is a scalar
# since its shape will be empty
# broadcast it to shape of a
# by using np.full_like
a_min_full = np.full_like(a, a_min)
return _np_clip_impl(a, a_min_full, a_max, out)
return np_clip_sa
elif not a_min_is_scalar and a_max_is_scalar:
if a_min_is_none:
def np_clip_ns(a, a_min, a_max, out=None):
# a_max is a scalar
# since its shape will be empty
# so broadcasting is not needed at all
ret = np.empty_like(a) if out is None else out
for index in np.ndindex(a.shape):
val_a = a[index]
ret[index] = min(val_a, a_max)
return ret
return np_clip_ns
else:
def np_clip_as(a, a_min, a_max, out=None):
# a_max is a scalar
# since its shape will be empty
# broadcast it to shape of a
# by using np.full_like
a_max_full = np.full_like(a, a_max)
return _np_clip_impl(a, a_min, a_max_full, out)
return np_clip_as
else:
# Case where exactly one of a_min or a_max is None
if a_min_is_none:
def np_clip_na(a, a_min, a_max, out=None):
# a_max is a numpy array but a_min is None
ret = np.empty_like(a) if out is None else out
a_b, a_max_b = np.broadcast_arrays(a, a_max)
return _np_clip_impl_none(a_b, a_max_b, True, ret)
return np_clip_na
elif a_max_is_none:
def np_clip_an(a, a_min, a_max, out=None):
# a_min is a numpy array but a_max is None
ret = np.empty_like(a) if out is None else out
a_b, a_min_b = np.broadcast_arrays(a, a_min)
return _np_clip_impl_none(a_b, a_min_b, False, ret)
return np_clip_an
else:
def np_clip_aa(a, a_min, a_max, out=None):
# Both a_min and a_max are clearly arrays
# because none of the above branches
# returned
return _np_clip_impl(a, a_min, a_max, out)
return np_clip_aa
@overload_method(types.Array, 'clip')
def array_clip(a, a_min=None, a_max=None, out=None):
def impl(a, a_min=None, a_max=None, out=None):
return np.clip(a, a_min, a_max, out)
return impl
def _change_dtype(context, builder, oldty, newty, ary):
"""
Attempt to fix up *ary* for switching from *oldty* to *newty*.
See Numpy's array_descr_set()
(np/core/src/multiarray/getset.c).
Attempt to fix the array's shape and strides for a new dtype.
False is returned on failure, True on success.
"""
assert oldty.ndim == newty.ndim
assert oldty.layout == newty.layout
new_layout = ord(newty.layout)
any_layout = ord('A')
c_layout = ord('C')
f_layout = ord('F')
int8 = types.int8
def imp(nd, dims, strides, old_itemsize, new_itemsize, layout):
# Attempt to update the layout due to limitation of the numba
# type system.
if layout == any_layout:
# Test rightmost stride to be contiguous
if strides[-1] == old_itemsize:
# Process this as if it is C contiguous
layout = int8(c_layout)
# Test leftmost stride to be F contiguous
elif strides[0] == old_itemsize:
# Process this as if it is F contiguous
layout = int8(f_layout)
if old_itemsize != new_itemsize and (layout == any_layout or nd == 0):
return False
if layout == c_layout:
i = nd - 1
else:
i = 0
if new_itemsize < old_itemsize:
# If it is compatible, increase the size of the dimension
# at the end (or at the front if F-contiguous)
if (old_itemsize % new_itemsize) != 0:
return False
newdim = old_itemsize // new_itemsize
dims[i] *= newdim
strides[i] = new_itemsize
elif new_itemsize > old_itemsize:
# Determine if last (or first if F-contiguous) dimension
# is compatible
bytelength = dims[i] * old_itemsize
if (bytelength % new_itemsize) != 0:
return False
dims[i] = bytelength // new_itemsize
strides[i] = new_itemsize
else:
# Same item size: nothing to do (this also works for
# non-contiguous arrays).
pass
return True
old_itemsize = context.get_constant(types.intp,
get_itemsize(context, oldty))
new_itemsize = context.get_constant(types.intp,
get_itemsize(context, newty))
nd = context.get_constant(types.intp, newty.ndim)
shape_data = cgutils.gep_inbounds(builder, ary._get_ptr_by_name('shape'),
0, 0)
strides_data = cgutils.gep_inbounds(builder,
ary._get_ptr_by_name('strides'), 0, 0)
shape_strides_array_type = types.Array(dtype=types.intp, ndim=1, layout='C')
arycls = context.make_array(shape_strides_array_type)
shape_constant = cgutils.pack_array(builder,
[context.get_constant(types.intp,
newty.ndim)])
sizeof_intp = context.get_abi_sizeof(context.get_data_type(types.intp))
sizeof_intp = context.get_constant(types.intp, sizeof_intp)
strides_constant = cgutils.pack_array(builder, [sizeof_intp])
shape_ary = arycls(context, builder)
populate_array(shape_ary,
data=shape_data,
shape=shape_constant,
strides=strides_constant,
itemsize=sizeof_intp,
meminfo=None)
strides_ary = arycls(context, builder)
populate_array(strides_ary,
data=strides_data,
shape=shape_constant,
strides=strides_constant,
itemsize=sizeof_intp,
meminfo=None)
shape = shape_ary._getvalue()
strides = strides_ary._getvalue()
args = [nd, shape, strides, old_itemsize, new_itemsize,
context.get_constant(types.int8, new_layout)]
sig = signature(types.boolean,
types.intp, # nd
shape_strides_array_type, # dims
shape_strides_array_type, # strides
types.intp, # old_itemsize
types.intp, # new_itemsize
types.int8, # layout
)
res = context.compile_internal(builder, imp, sig, args)
update_array_info(newty, ary)
res = impl_ret_borrowed(context, builder, sig.return_type, res)
return res
@overload(np.shape)
def np_shape(a):
if not type_can_asarray(a):
raise errors.TypingError("The argument to np.shape must be array-like")
def impl(a):
return np.asarray(a).shape
return impl
@overload(np.size)
def np_size(a):
if not type_can_asarray(a):
raise errors.TypingError("The argument to np.size must be array-like")
def impl(a):
return np.asarray(a).size
return impl
# ------------------------------------------------------------------------------
@overload(np.unique)
def np_unique(ar):
def np_unique_impl(ar):
b = np.sort(ar.ravel())
head = list(b[:1])
tail = [x for i, x in enumerate(b[1:]) if b[i] != x]
return np.array(head + tail)
return np_unique_impl
@overload(np.repeat)
def np_repeat(a, repeats):
# Implementation for repeats being a scalar is a module global function
# (see below) because it might be called from the implementation below.
def np_repeat_impl_repeats_array_like(a, repeats):
# implementation if repeats is an array like
repeats_array = np.asarray(repeats, dtype=np.int64)
# if it is a singleton array, invoke the scalar implementation
if repeats_array.shape[0] == 1:
return np_repeat_impl_repeats_scaler(a, repeats_array[0])
if np.any(repeats_array < 0):
raise ValueError("negative dimensions are not allowed")
asa = np.asarray(a)
aravel = asa.ravel()
n = aravel.shape[0]
if aravel.shape != repeats_array.shape:
raise ValueError(
"operands could not be broadcast together")
to_return = np.empty(np.sum(repeats_array), dtype=asa.dtype)
pos = 0
for i in range(n):
to_return[pos : pos + repeats_array[i]] = aravel[i]
pos += repeats_array[i]
return to_return
# type checking
if isinstance(a, (types.Array,
types.List,
types.BaseTuple,
types.Number,
types.Boolean,
)
):
if isinstance(repeats, types.Integer):
return np_repeat_impl_repeats_scaler
elif isinstance(repeats, (types.Array, types.List)):
if isinstance(repeats.dtype, types.Integer):
return np_repeat_impl_repeats_array_like
raise errors.TypingError(
"The repeats argument must be an integer "
"or an array-like of integer dtype")
@register_jitable
def np_repeat_impl_repeats_scaler(a, repeats):
if repeats < 0:
raise ValueError("negative dimensions are not allowed")
asa = np.asarray(a)
aravel = asa.ravel()
n = aravel.shape[0]
if repeats == 0:
return np.empty(0, dtype=asa.dtype)
elif repeats == 1:
return np.copy(aravel)
else:
to_return = np.empty(n * repeats, dtype=asa.dtype)
for i in range(n):
to_return[i * repeats : (i + 1) * repeats] = aravel[i]
return to_return
@extending.overload_method(types.Array, 'repeat')
def array_repeat(a, repeats):
def array_repeat_impl(a, repeats):
return np.repeat(a, repeats)
return array_repeat_impl
@intrinsic
def _intrin_get_itemsize(tyctx, dtype):
"""Computes the itemsize of the dtype"""
sig = types.intp(dtype)
def codegen(cgctx, builder, sig, llargs):
llty = cgctx.get_data_type(sig.args[0].dtype)
llintp = cgctx.get_data_type(sig.return_type)
return llintp(cgctx.get_abi_sizeof(llty))
return sig, codegen
def _compatible_view(a, dtype):
pass
@overload(_compatible_view, target='generic')
def ol_compatible_view(a, dtype):
"""Determines if the array and dtype are compatible for forming a view."""
# NOTE: NumPy 1.23+ uses this check.
# Code based on:
# https://github.com/numpy/numpy/blob/750ad21258cfc00663586d5a466e24f91b48edc7/numpy/core/src/multiarray/getset.c#L500-L555 # noqa: E501
def impl(a, dtype):
dtype_size = _intrin_get_itemsize(dtype)
if dtype_size != a.itemsize:
# catch forbidden cases
if a.ndim == 0:
msg1 = ("Changing the dtype of a 0d array is only supported "
"if the itemsize is unchanged")
raise ValueError(msg1)
else:
# NumPy has a check here for subarray type conversion which
# Numba doesn't support
pass
# Resize on last axis only
axis = a.ndim - 1
p1 = a.shape[axis] != 1
p2 = a.size != 0
p3 = a.strides[axis] != a.itemsize
if (p1 and p2 and p3):
msg2 = ("To change to a dtype of a different size, the last "
"axis must be contiguous")
raise ValueError(msg2)
if dtype_size < a.itemsize:
if dtype_size == 0 or a.itemsize % dtype_size != 0:
msg3 = ("When changing to a smaller dtype, its size must "
"be a divisor of the size of original dtype")
raise ValueError(msg3)
else:
newdim = a.shape[axis] * a.itemsize
if newdim % dtype_size != 0:
msg4 = ("When changing to a larger dtype, its size must be "
"a divisor of the total size in bytes of the last "
"axis of the array.")
raise ValueError(msg4)
return impl
@lower_builtin('array.view', types.Array, types.DTypeSpec)
def array_view(context, builder, sig, args):
aryty = sig.args[0]
retty = sig.return_type
ary = make_array(aryty)(context, builder, args[0])
ret = make_array(retty)(context, builder)
# Copy all fields, casting the "data" pointer appropriately
fields = set(ret._datamodel._fields)
for k in sorted(fields):
val = getattr(ary, k)
if k == 'data':
ptrty = ret.data.type
ret.data = builder.bitcast(val, ptrty)
else:
setattr(ret, k, val)
if numpy_version >= (1, 23):
# NumPy 1.23+ bans views using a dtype that is a different size to that
# of the array when the last axis is not contiguous. For example, this
# manifests at runtime when a dtype size altering view is requested
# on a Fortran ordered array.
tyctx = context.typing_context
fnty = tyctx.resolve_value_type(_compatible_view)
_compatible_view_sig = fnty.get_call_type(tyctx, (*sig.args,), {})
impl = context.get_function(fnty, _compatible_view_sig)
impl(builder, args)
ok = _change_dtype(context, builder, aryty, retty, ret)
fail = builder.icmp_unsigned('==', ok, Constant(ok.type, 0))
with builder.if_then(fail):
msg = "new type not compatible with array"
context.call_conv.return_user_exc(builder, ValueError, (msg,))
res = ret._getvalue()
return impl_ret_borrowed(context, builder, sig.return_type, res)
# ------------------------------------------------------------------------------
# Array attributes
@lower_getattr(types.Array, "dtype")
def array_dtype(context, builder, typ, value):
res = context.get_dummy_value()
return impl_ret_untracked(context, builder, typ, res)
@lower_getattr(types.Array, "shape")
@lower_getattr(types.MemoryView, "shape")
def array_shape(context, builder, typ, value):
arrayty = make_array(typ)
array = arrayty(context, builder, value)
res = array.shape
return impl_ret_untracked(context, builder, typ, res)
@lower_getattr(types.Array, "strides")
@lower_getattr(types.MemoryView, "strides")
def array_strides(context, builder, typ, value):
arrayty = make_array(typ)
array = arrayty(context, builder, value)
res = array.strides
return impl_ret_untracked(context, builder, typ, res)
@lower_getattr(types.Array, "ndim")
@lower_getattr(types.MemoryView, "ndim")
def array_ndim(context, builder, typ, value):
res = context.get_constant(types.intp, typ.ndim)
return impl_ret_untracked(context, builder, typ, res)
@lower_getattr(types.Array, "size")
def array_size(context, builder, typ, value):
arrayty = make_array(typ)
array = arrayty(context, builder, value)
res = array.nitems
return impl_ret_untracked(context, builder, typ, res)
@lower_getattr(types.Array, "itemsize")
@lower_getattr(types.MemoryView, "itemsize")
def array_itemsize(context, builder, typ, value):
arrayty = make_array(typ)
array = arrayty(context, builder, value)
res = array.itemsize
return impl_ret_untracked(context, builder, typ, res)
@lower_getattr(types.Array, "nbytes")
@lower_getattr(types.MemoryView, "nbytes")
def array_nbytes(context, builder, typ, value):
"""
nbytes = size * itemsize
"""
arrayty = make_array(typ)
array = arrayty(context, builder, value)
res = builder.mul(array.nitems, array.itemsize)
return impl_ret_untracked(context, builder, typ, res)
@lower_getattr(types.MemoryView, "contiguous")
def array_contiguous(context, builder, typ, value):
res = context.get_constant(types.boolean, typ.is_contig)
return impl_ret_untracked(context, builder, typ, res)
@lower_getattr(types.MemoryView, "c_contiguous")
def array_c_contiguous(context, builder, typ, value):
res = context.get_constant(types.boolean, typ.is_c_contig)
return impl_ret_untracked(context, builder, typ, res)
@lower_getattr(types.MemoryView, "f_contiguous")
def array_f_contiguous(context, builder, typ, value):
res = context.get_constant(types.boolean, typ.is_f_contig)
return impl_ret_untracked(context, builder, typ, res)
@lower_getattr(types.MemoryView, "readonly")
def array_readonly(context, builder, typ, value):
res = context.get_constant(types.boolean, not typ.mutable)
return impl_ret_untracked(context, builder, typ, res)
# array.ctypes
@lower_getattr(types.Array, "ctypes")
def array_ctypes(context, builder, typ, value):
arrayty = make_array(typ)
array = arrayty(context, builder, value)
# Create new ArrayCType structure
act = types.ArrayCTypes(typ)
ctinfo = context.make_helper(builder, act)
ctinfo.data = array.data
ctinfo.meminfo = array.meminfo
res = ctinfo._getvalue()
return impl_ret_borrowed(context, builder, act, res)
@lower_getattr(types.ArrayCTypes, "data")
def array_ctypes_data(context, builder, typ, value):
ctinfo = context.make_helper(builder, typ, value=value)
res = ctinfo.data
# Convert it to an integer
res = builder.ptrtoint(res, context.get_value_type(types.intp))
return impl_ret_untracked(context, builder, typ, res)
@lower_cast(types.ArrayCTypes, types.CPointer)
@lower_cast(types.ArrayCTypes, types.voidptr)
def array_ctypes_to_pointer(context, builder, fromty, toty, val):
ctinfo = context.make_helper(builder, fromty, value=val)
res = ctinfo.data
res = builder.bitcast(res, context.get_value_type(toty))
return impl_ret_untracked(context, builder, toty, res)
def _call_contiguous_check(checker, context, builder, aryty, ary):
"""Helper to invoke the contiguous checker function on an array
Args
----
checker :
``numba.numpy_supports.is_contiguous``, or
``numba.numpy_supports.is_fortran``.
context : target context
builder : llvm ir builder
aryty : numba type
ary : llvm value
"""
ary = make_array(aryty)(context, builder, value=ary)
tup_intp = types.UniTuple(types.intp, aryty.ndim)
itemsize = context.get_abi_sizeof(context.get_value_type(aryty.dtype))
check_sig = signature(types.bool_, tup_intp, tup_intp, types.intp)
check_args = [ary.shape, ary.strides,
context.get_constant(types.intp, itemsize)]
is_contig = context.compile_internal(builder, checker, check_sig,
check_args)
return is_contig
# array.flags
@lower_getattr(types.Array, "flags")
def array_flags(context, builder, typ, value):
flagsobj = context.make_helper(builder, types.ArrayFlags(typ))
flagsobj.parent = value
res = flagsobj._getvalue()
context.nrt.incref(builder, typ, value)
return impl_ret_new_ref(context, builder, typ, res)
@lower_getattr(types.ArrayFlags, "contiguous")
@lower_getattr(types.ArrayFlags, "c_contiguous")
def array_flags_c_contiguous(context, builder, typ, value):
if typ.array_type.layout != 'C':
# any layout can still be contiguous
flagsobj = context.make_helper(builder, typ, value=value)
res = _call_contiguous_check(is_contiguous, context, builder,
typ.array_type, flagsobj.parent)
else:
val = typ.array_type.layout == 'C'
res = context.get_constant(types.boolean, val)
return impl_ret_untracked(context, builder, typ, res)
@lower_getattr(types.ArrayFlags, "f_contiguous")
def array_flags_f_contiguous(context, builder, typ, value):
if typ.array_type.layout != 'F':
# any layout can still be contiguous
flagsobj = context.make_helper(builder, typ, value=value)
res = _call_contiguous_check(is_fortran, context, builder,
typ.array_type, flagsobj.parent)
else:
layout = typ.array_type.layout
val = layout == 'F' if typ.array_type.ndim > 1 else layout in 'CF'
res = context.get_constant(types.boolean, val)
return impl_ret_untracked(context, builder, typ, res)
# ------------------------------------------------------------------------------
# .real / .imag
@lower_getattr(types.Array, "real")
def array_real_part(context, builder, typ, value):
if typ.dtype in types.complex_domain:
return array_complex_attr(context, builder, typ, value, attr='real')
elif typ.dtype in types.number_domain:
# as an identity function
return impl_ret_borrowed(context, builder, typ, value)
else:
raise NotImplementedError('unsupported .real for {}'.format(type.dtype))
@lower_getattr(types.Array, "imag")
def array_imag_part(context, builder, typ, value):
if typ.dtype in types.complex_domain:
return array_complex_attr(context, builder, typ, value, attr='imag')
elif typ.dtype in types.number_domain:
# return a readonly zero array
sig = signature(typ.copy(readonly=True), typ)
arrtype, shapes = _parse_empty_like_args(context, builder, sig, [value])
ary = _empty_nd_impl(context, builder, arrtype, shapes)
cgutils.memset(builder, ary.data, builder.mul(ary.itemsize,
ary.nitems), 0)
return impl_ret_new_ref(context, builder, sig.return_type,
ary._getvalue())
else:
raise NotImplementedError('unsupported .imag for {}'.format(type.dtype))
def array_complex_attr(context, builder, typ, value, attr):
"""
Given a complex array, it's memory layout is:
R C R C R C
^ ^ ^
(`R` indicates a float for the real part;
`C` indicates a float for the imaginary part;
the `^` indicates the start of each element)
To get the real part, we can simply change the dtype and itemsize to that
of the underlying float type. The new layout is:
R x R x R x
^ ^ ^
(`x` indicates unused)
A load operation will use the dtype to determine the number of bytes to
load.
To get the imaginary part, we shift the pointer by 1 float offset and
change the dtype and itemsize. The new layout is:
x C x C x C
^ ^ ^
"""
if attr not in ['real', 'imag'] or typ.dtype not in types.complex_domain:
raise NotImplementedError("cannot get attribute `{}`".format(attr))
arrayty = make_array(typ)
array = arrayty(context, builder, value)
# sizeof underlying float type
flty = typ.dtype.underlying_float
sizeof_flty = context.get_abi_sizeof(context.get_data_type(flty))
itemsize = array.itemsize.type(sizeof_flty)
# cast data pointer to float type
llfltptrty = context.get_value_type(flty).as_pointer()
dataptr = builder.bitcast(array.data, llfltptrty)
# add offset
if attr == 'imag':
dataptr = builder.gep(dataptr, [ir.IntType(32)(1)])
# make result
resultty = typ.copy(dtype=flty, layout='A')
result = make_array(resultty)(context, builder)
repl = dict(data=dataptr, itemsize=itemsize)
cgutils.copy_struct(result, array, repl)
return impl_ret_borrowed(context, builder, resultty, result._getvalue())
@overload_method(types.Array, 'conj')
@overload_method(types.Array, 'conjugate')
def array_conj(arr):
def impl(arr):
return np.conj(arr)
return impl
# ------------------------------------------------------------------------------
# DType attribute
def dtype_type(context, builder, dtypety, dtypeval):
# Just return a dummy opaque value
return context.get_dummy_value()
lower_getattr(types.DType, 'type')(dtype_type)
lower_getattr(types.DType, 'kind')(dtype_type)
# ------------------------------------------------------------------------------
# static_getitem on Numba numerical types to create "array" types
@lower_builtin('static_getitem', types.NumberClass, types.Any)
def static_getitem_number_clazz(context, builder, sig, args):
"""This handles the "static_getitem" when a Numba type is subscripted e.g:
var = typed.List.empty_list(float64[::1, :])
It only allows this on simple numerical types. Compound types, like
records, are not supported.
"""
retty = sig.return_type
if isinstance(retty, types.Array):
# This isn't used or practically accessible, but has to exist, so just
# put in a NULL of the right type.
res = context.get_value_type(retty)(None)
return impl_ret_untracked(context, builder, retty, res)
else:
# This should be unreachable unless the implementation on the Type
# metaclass is changed.
msg = ("Unreachable; the definition of __getitem__ on the "
"numba.types.abstract.Type metaclass should prevent access.")
raise errors.LoweringError(msg)
# ------------------------------------------------------------------------------
# Structured / record lookup
@lower_getattr_generic(types.Array)
def array_record_getattr(context, builder, typ, value, attr):
"""
Generic getattr() implementation for record arrays: fetch the given
record member, i.e. a subarray.
"""
arrayty = make_array(typ)
array = arrayty(context, builder, value)
rectype = typ.dtype
if not isinstance(rectype, types.Record):
raise NotImplementedError("attribute %r of %s not defined"
% (attr, typ))
dtype = rectype.typeof(attr)
offset = rectype.offset(attr)
if isinstance(dtype, types.NestedArray):
resty = typ.copy(
dtype=dtype.dtype, ndim=typ.ndim + dtype.ndim, layout='A')
else:
resty = typ.copy(dtype=dtype, layout='A')
raryty = make_array(resty)
rary = raryty(context, builder)
constoffset = context.get_constant(types.intp, offset)
newdataptr = cgutils.pointer_add(
builder, array.data, constoffset, return_type=rary.data.type,
)
if isinstance(dtype, types.NestedArray):
# new shape = recarray shape + inner dimension from nestedarray
shape = cgutils.unpack_tuple(builder, array.shape, typ.ndim)
shape += [context.get_constant(types.intp, i) for i in dtype.shape]
# new strides = recarray strides + strides of the inner nestedarray
strides = cgutils.unpack_tuple(builder, array.strides, typ.ndim)
strides += [context.get_constant(types.intp, i) for i in dtype.strides]
# New datasize = size of elements of the nestedarray
datasize = context.get_abi_sizeof(context.get_data_type(dtype.dtype))
else:
# New shape, strides, and datasize match the underlying array
shape = array.shape
strides = array.strides
datasize = context.get_abi_sizeof(context.get_data_type(dtype))
populate_array(rary,
data=newdataptr,
shape=shape,
strides=strides,
itemsize=context.get_constant(types.intp, datasize),
meminfo=array.meminfo,
parent=array.parent)
res = rary._getvalue()
return impl_ret_borrowed(context, builder, resty, res)
@lower_builtin('static_getitem', types.Array, types.StringLiteral)
def array_record_getitem(context, builder, sig, args):
index = args[1]
if not isinstance(index, str):
# This will fallback to normal getitem
raise NotImplementedError
return array_record_getattr(context, builder, sig.args[0], args[0], index)
@lower_getattr_generic(types.Record)
def record_getattr(context, builder, typ, value, attr):
"""
Generic getattr() implementation for records: get the given record member.
"""
context.sentry_record_alignment(typ, attr)
offset = typ.offset(attr)
elemty = typ.typeof(attr)
if isinstance(elemty, types.NestedArray):
# Only a nested array's *data* is stored in a structured array,
# so we create an array structure to point to that data.
aryty = make_array(elemty)
ary = aryty(context, builder)
dtype = elemty.dtype
newshape = [context.get_constant(types.intp, s) for s in
elemty.shape]
newstrides = [context.get_constant(types.intp, s) for s in
elemty.strides]
newdata = cgutils.get_record_member(builder, value, offset,
context.get_data_type(dtype))
populate_array(
ary,
data=newdata,
shape=cgutils.pack_array(builder, newshape),
strides=cgutils.pack_array(builder, newstrides),
itemsize=context.get_constant(types.intp, elemty.size),
meminfo=None,
parent=None,
)
res = ary._getvalue()
return impl_ret_borrowed(context, builder, typ, res)
else:
dptr = cgutils.get_record_member(builder, value, offset,
context.get_data_type(elemty))
align = None if typ.aligned else 1
res = context.unpack_value(builder, elemty, dptr, align)
return impl_ret_borrowed(context, builder, typ, res)
@lower_setattr_generic(types.Record)
def record_setattr(context, builder, sig, args, attr):
"""
Generic setattr() implementation for records: set the given record member.
"""
typ, valty = sig.args
target, val = args
context.sentry_record_alignment(typ, attr)
offset = typ.offset(attr)
elemty = typ.typeof(attr)
if isinstance(elemty, types.NestedArray):
# Copy the data from the RHS into the nested array
val_struct = cgutils.create_struct_proxy(valty)(context, builder,
value=args[1])
src = val_struct.data
dest = cgutils.get_record_member(builder, target, offset,
src.type.pointee)
cgutils.memcpy(builder, dest, src,
context.get_constant(types.intp, elemty.nitems))
else:
# Set the given scalar record member
dptr = cgutils.get_record_member(builder, target, offset,
context.get_data_type(elemty))
val = context.cast(builder, val, valty, elemty)
align = None if typ.aligned else 1
context.pack_value(builder, elemty, val, dptr, align=align)
@lower_builtin('static_getitem', types.Record, types.StringLiteral)
def record_static_getitem_str(context, builder, sig, args):
"""
Record.__getitem__ redirects to getattr()
"""
impl = context.get_getattr(sig.args[0], args[1])
return impl(context, builder, sig.args[0], args[0], args[1])
@lower_builtin('static_getitem', types.Record, types.IntegerLiteral)
def record_static_getitem_int(context, builder, sig, args):
"""
Record.__getitem__ redirects to getattr()
"""
idx = sig.args[1].literal_value
fields = list(sig.args[0].fields)
ll_field = context.insert_const_string(builder.module, fields[idx])
impl = context.get_getattr(sig.args[0], ll_field)
return impl(context, builder, sig.args[0], args[0], fields[idx])
@lower_builtin('static_setitem', types.Record, types.StringLiteral, types.Any)
def record_static_setitem_str(context, builder, sig, args):
"""
Record.__setitem__ redirects to setattr()
"""
recty, _, valty = sig.args
rec, idx, val = args
getattr_sig = signature(sig.return_type, recty, valty)
impl = context.get_setattr(idx, getattr_sig)
assert impl is not None
return impl(builder, (rec, val))
@lower_builtin('static_setitem', types.Record, types.IntegerLiteral, types.Any)
def record_static_setitem_int(context, builder, sig, args):
"""
Record.__setitem__ redirects to setattr()
"""
recty, _, valty = sig.args
rec, idx, val = args
getattr_sig = signature(sig.return_type, recty, valty)
fields = list(sig.args[0].fields)
impl = context.get_setattr(fields[idx], getattr_sig)
assert impl is not None
return impl(builder, (rec, val))
# ------------------------------------------------------------------------------
# Constant arrays and records
@lower_constant(types.Array)
def constant_array(context, builder, ty, pyval):
"""
Create a constant array (mechanism is target-dependent).
"""
return context.make_constant_array(builder, ty, pyval)
@lower_constant(types.Record)
def constant_record(context, builder, ty, pyval):
"""
Create a record constant as a stack-allocated array of bytes.
"""
lty = ir.ArrayType(ir.IntType(8), pyval.nbytes)
val = lty(bytearray(pyval.tostring()))
return cgutils.alloca_once_value(builder, val)
@lower_constant(types.Bytes)
def constant_bytes(context, builder, ty, pyval):
"""
Create a constant array from bytes (mechanism is target-dependent).
"""
buf = np.array(bytearray(pyval), dtype=np.uint8)
return context.make_constant_array(builder, ty, buf)
# ------------------------------------------------------------------------------
# Comparisons
@lower_builtin(operator.is_, types.Array, types.Array)
def array_is(context, builder, sig, args):
aty, bty = sig.args
if aty != bty:
return cgutils.false_bit
def array_is_impl(a, b):
return (a.shape == b.shape and
a.strides == b.strides and
a.ctypes.data == b.ctypes.data)
return context.compile_internal(builder, array_is_impl, sig, args)
# ------------------------------------------------------------------------------
# Hash
@overload_attribute(types.Array, "__hash__")
def ol_array_hash(arr):
return lambda arr: None
# ------------------------------------------------------------------------------
# builtin `np.flat` implementation
def make_array_flat_cls(flatiterty):
"""
Return the Structure representation of the given *flatiterty* (an
instance of types.NumpyFlatType).
"""
return _make_flattening_iter_cls(flatiterty, 'flat')
def make_array_ndenumerate_cls(nditerty):
"""
Return the Structure representation of the given *nditerty* (an
instance of types.NumpyNdEnumerateType).
"""
return _make_flattening_iter_cls(nditerty, 'ndenumerate')
def _increment_indices(context, builder, ndim, shape, indices, end_flag=None,
loop_continue=None, loop_break=None):
zero = context.get_constant(types.intp, 0)
bbend = builder.append_basic_block('end_increment')
if end_flag is not None:
builder.store(cgutils.false_byte, end_flag)
for dim in reversed(range(ndim)):
idxptr = cgutils.gep_inbounds(builder, indices, dim)
idx = cgutils.increment_index(builder, builder.load(idxptr))
count = shape[dim]
in_bounds = builder.icmp_signed('<', idx, count)
with cgutils.if_likely(builder, in_bounds):
# New index is still in bounds
builder.store(idx, idxptr)
if loop_continue is not None:
loop_continue(dim)
builder.branch(bbend)
# Index out of bounds => reset it and proceed it to outer index
builder.store(zero, idxptr)
if loop_break is not None:
loop_break(dim)
if end_flag is not None:
builder.store(cgutils.true_byte, end_flag)
builder.branch(bbend)
builder.position_at_end(bbend)
def _increment_indices_array(context, builder, arrty, arr, indices,
end_flag=None):
shape = cgutils.unpack_tuple(builder, arr.shape, arrty.ndim)
_increment_indices(context, builder, arrty.ndim, shape, indices, end_flag)
def make_nditer_cls(nditerty):
"""
Return the Structure representation of the given *nditerty* (an
instance of types.NumpyNdIterType).
"""
ndim = nditerty.ndim
layout = nditerty.layout
narrays = len(nditerty.arrays)
nshapes = ndim if nditerty.need_shaped_indexing else 1
class BaseSubIter(object):
"""
Base class for sub-iterators of a nditer() instance.
"""
def __init__(self, nditer, member_name, start_dim, end_dim):
self.nditer = nditer
self.member_name = member_name
self.start_dim = start_dim
self.end_dim = end_dim
self.ndim = end_dim - start_dim
def set_member_ptr(self, ptr):
setattr(self.nditer, self.member_name, ptr)
@functools.cached_property
def member_ptr(self):
return getattr(self.nditer, self.member_name)
def init_specific(self, context, builder):
pass
def loop_continue(self, context, builder, logical_dim):
pass
def loop_break(self, context, builder, logical_dim):
pass
class FlatSubIter(BaseSubIter):
"""
Sub-iterator walking a contiguous array in physical order, with
support for broadcasting (the index is reset on the outer dimension).
"""
def init_specific(self, context, builder):
zero = context.get_constant(types.intp, 0)
self.set_member_ptr(cgutils.alloca_once_value(builder, zero))
def compute_pointer(self, context, builder, indices, arrty, arr):
index = builder.load(self.member_ptr)
return builder.gep(arr.data, [index])
def loop_continue(self, context, builder, logical_dim):
if logical_dim == self.ndim - 1:
# Only increment index inside innermost logical dimension
index = builder.load(self.member_ptr)
index = cgutils.increment_index(builder, index)
builder.store(index, self.member_ptr)
def loop_break(self, context, builder, logical_dim):
if logical_dim == 0:
# At the exit of outermost logical dimension, reset index
zero = context.get_constant(types.intp, 0)
builder.store(zero, self.member_ptr)
elif logical_dim == self.ndim - 1:
# Inside innermost logical dimension, increment index
index = builder.load(self.member_ptr)
index = cgutils.increment_index(builder, index)
builder.store(index, self.member_ptr)
class TrivialFlatSubIter(BaseSubIter):
"""
Sub-iterator walking a contiguous array in physical order,
*without* support for broadcasting.
"""
def init_specific(self, context, builder):
assert not nditerty.need_shaped_indexing
def compute_pointer(self, context, builder, indices, arrty, arr):
assert len(indices) <= 1, len(indices)
return builder.gep(arr.data, indices)
class IndexedSubIter(BaseSubIter):
"""
Sub-iterator walking an array in logical order.
"""
def compute_pointer(self, context, builder, indices, arrty, arr):
assert len(indices) == self.ndim
return cgutils.get_item_pointer(context, builder, arrty, arr,
indices, wraparound=False)
class ZeroDimSubIter(BaseSubIter):
"""
Sub-iterator "walking" a 0-d array.
"""
def compute_pointer(self, context, builder, indices, arrty, arr):
return arr.data
class ScalarSubIter(BaseSubIter):
"""
Sub-iterator "walking" a scalar value.
"""
def compute_pointer(self, context, builder, indices, arrty, arr):
return arr
class NdIter(cgutils.create_struct_proxy(nditerty)):
"""
.nditer() implementation.
Note: 'F' layout means the shape is iterated in reverse logical order,
so indices and shapes arrays have to be reversed as well.
"""
@functools.cached_property
def subiters(self):
l = []
factories = {'flat': FlatSubIter if nditerty.need_shaped_indexing
else TrivialFlatSubIter,
'indexed': IndexedSubIter,
'0d': ZeroDimSubIter,
'scalar': ScalarSubIter,
}
for i, sub in enumerate(nditerty.indexers):
kind, start_dim, end_dim, _ = sub
member_name = 'index%d' % i
factory = factories[kind]
l.append(factory(self, member_name, start_dim, end_dim))
return l
def init_specific(self, context, builder, arrtys, arrays):
"""
Initialize the nditer() instance for the specific array inputs.
"""
zero = context.get_constant(types.intp, 0)
# Store inputs
self.arrays = context.make_tuple(builder, types.Tuple(arrtys),
arrays)
# Create slots for scalars
for i, ty in enumerate(arrtys):
if not isinstance(ty, types.Array):
member_name = 'scalar%d' % i
# XXX as_data()?
slot = cgutils.alloca_once_value(builder, arrays[i])
setattr(self, member_name, slot)
arrays = self._arrays_or_scalars(context, builder, arrtys, arrays)
# Extract iterator shape (the shape of the most-dimensional input)
main_shape_ty = types.UniTuple(types.intp, ndim)
main_shape = None
main_nitems = None
for i, arrty in enumerate(arrtys):
if isinstance(arrty, types.Array) and arrty.ndim == ndim:
main_shape = arrays[i].shape
main_nitems = arrays[i].nitems
break
else:
# Only scalar inputs => synthesize a dummy shape
assert ndim == 0
main_shape = context.make_tuple(builder, main_shape_ty, ())
main_nitems = context.get_constant(types.intp, 1)
# Validate shapes of array inputs
def check_shape(shape, main_shape):
n = len(shape)
for i in range(n):
if shape[i] != main_shape[len(main_shape) - n + i]:
raise ValueError("nditer(): operands could not be "
"broadcast together")
for arrty, arr in zip(arrtys, arrays):
if isinstance(arrty, types.Array) and arrty.ndim > 0:
sig = signature(types.none,
types.UniTuple(types.intp, arrty.ndim),
main_shape_ty)
context.compile_internal(builder, check_shape,
sig, (arr.shape, main_shape))
# Compute shape and size
shapes = cgutils.unpack_tuple(builder, main_shape)
if layout == 'F':
shapes = shapes[::-1]
# If shape is empty, mark iterator exhausted
shape_is_empty = builder.icmp_signed('==', main_nitems, zero)
exhausted = builder.select(shape_is_empty, cgutils.true_byte,
cgutils.false_byte)
if not nditerty.need_shaped_indexing:
# Flatten shape to make iteration faster on small innermost
# dimensions (e.g. a (100000, 3) shape)
shapes = (main_nitems,)
assert len(shapes) == nshapes
indices = cgutils.alloca_once(builder, zero.type, size=nshapes)
for dim in range(nshapes):
idxptr = cgutils.gep_inbounds(builder, indices, dim)
builder.store(zero, idxptr)
self.indices = indices
self.shape = cgutils.pack_array(builder, shapes, zero.type)
self.exhausted = cgutils.alloca_once_value(builder, exhausted)
# Initialize subiterators
for subiter in self.subiters:
subiter.init_specific(context, builder)
def iternext_specific(self, context, builder, result):
"""
Compute next iteration of the nditer() instance.
"""
bbend = builder.append_basic_block('end')
# Branch early if exhausted
exhausted = cgutils.as_bool_bit(builder,
builder.load(self.exhausted))
with cgutils.if_unlikely(builder, exhausted):
result.set_valid(False)
builder.branch(bbend)
arrtys = nditerty.arrays
arrays = cgutils.unpack_tuple(builder, self.arrays)
arrays = self._arrays_or_scalars(context, builder, arrtys, arrays)
indices = self.indices
# Compute iterated results
result.set_valid(True)
views = self._make_views(context, builder, indices, arrtys, arrays)
views = [v._getvalue() for v in views]
if len(views) == 1:
result.yield_(views[0])
else:
result.yield_(context.make_tuple(builder, nditerty.yield_type,
views))
shape = cgutils.unpack_tuple(builder, self.shape)
_increment_indices(context, builder, len(shape), shape,
indices, self.exhausted,
functools.partial(self._loop_continue,
context,
builder),
functools.partial(self._loop_break,
context,
builder),
)
builder.branch(bbend)
builder.position_at_end(bbend)
def _loop_continue(self, context, builder, dim):
for sub in self.subiters:
if sub.start_dim <= dim < sub.end_dim:
sub.loop_continue(context, builder, dim - sub.start_dim)
def _loop_break(self, context, builder, dim):
for sub in self.subiters:
if sub.start_dim <= dim < sub.end_dim:
sub.loop_break(context, builder, dim - sub.start_dim)
def _make_views(self, context, builder, indices, arrtys, arrays):
"""
Compute the views to be yielded.
"""
views = [None] * narrays
indexers = nditerty.indexers
subiters = self.subiters
rettys = nditerty.yield_type
if isinstance(rettys, types.BaseTuple):
rettys = list(rettys)
else:
rettys = [rettys]
indices = [builder.load(cgutils.gep_inbounds(builder, indices, i))
for i in range(nshapes)]
for sub, subiter in zip(indexers, subiters):
_, _, _, array_indices = sub
sub_indices = indices[subiter.start_dim:subiter.end_dim]
if layout == 'F':
sub_indices = sub_indices[::-1]
for i in array_indices:
assert views[i] is None
views[i] = self._make_view(context, builder, sub_indices,
rettys[i],
arrtys[i], arrays[i], subiter)
assert all(v for v in views)
return views
def _make_view(self, context, builder, indices, retty, arrty, arr,
subiter):
"""
Compute a 0d view for a given input array.
"""
assert isinstance(retty, types.Array) and retty.ndim == 0
ptr = subiter.compute_pointer(context, builder, indices, arrty, arr)
view = context.make_array(retty)(context, builder)
itemsize = get_itemsize(context, retty)
shape = context.make_tuple(builder, types.UniTuple(types.intp, 0),
())
strides = context.make_tuple(builder, types.UniTuple(types.intp, 0),
())
# HACK: meminfo=None avoids expensive refcounting operations
# on ephemeral views
populate_array(view, ptr, shape, strides, itemsize, meminfo=None)
return view
def _arrays_or_scalars(self, context, builder, arrtys, arrays):
# Return a list of either array structures or pointers to
# scalar slots
l = []
for i, (arrty, arr) in enumerate(zip(arrtys, arrays)):
if isinstance(arrty, types.Array):
l.append(context.make_array(arrty)(context,
builder,
value=arr))
else:
l.append(getattr(self, "scalar%d" % i))
return l
return NdIter
def make_ndindex_cls(nditerty):
"""
Return the Structure representation of the given *nditerty* (an
instance of types.NumpyNdIndexType).
"""
ndim = nditerty.ndim
class NdIndexIter(cgutils.create_struct_proxy(nditerty)):
"""
.ndindex() implementation.
"""
def init_specific(self, context, builder, shapes):
zero = context.get_constant(types.intp, 0)
indices = cgutils.alloca_once(builder, zero.type,
size=context.get_constant(types.intp,
ndim))
exhausted = cgutils.alloca_once_value(builder, cgutils.false_byte)
for dim in range(ndim):
idxptr = cgutils.gep_inbounds(builder, indices, dim)
builder.store(zero, idxptr)
# 0-sized dimensions really indicate an empty array,
# but we have to catch that condition early to avoid
# a bug inside the iteration logic.
dim_size = shapes[dim]
dim_is_empty = builder.icmp_unsigned('==', dim_size, zero)
with cgutils.if_unlikely(builder, dim_is_empty):
builder.store(cgutils.true_byte, exhausted)
self.indices = indices
self.exhausted = exhausted
self.shape = cgutils.pack_array(builder, shapes, zero.type)
def iternext_specific(self, context, builder, result):
zero = context.get_constant(types.intp, 0)
bbend = builder.append_basic_block('end')
exhausted = cgutils.as_bool_bit(builder,
builder.load(self.exhausted))
with cgutils.if_unlikely(builder, exhausted):
result.set_valid(False)
builder.branch(bbend)
indices = [builder.load(cgutils.gep_inbounds(builder,
self.indices,
dim))
for dim in range(ndim)]
for load in indices:
mark_positive(builder, load)
result.yield_(cgutils.pack_array(builder, indices, zero.type))
result.set_valid(True)
shape = cgutils.unpack_tuple(builder, self.shape, ndim)
_increment_indices(context, builder, ndim, shape,
self.indices, self.exhausted)
builder.branch(bbend)
builder.position_at_end(bbend)
return NdIndexIter
def _make_flattening_iter_cls(flatiterty, kind):
assert kind in ('flat', 'ndenumerate')
array_type = flatiterty.array_type
if array_type.layout == 'C':
class CContiguousFlatIter(cgutils.create_struct_proxy(flatiterty)):
"""
.flat() / .ndenumerate() implementation for C-contiguous arrays.
"""
def init_specific(self, context, builder, arrty, arr):
zero = context.get_constant(types.intp, 0)
self.index = cgutils.alloca_once_value(builder, zero)
# We can't trust strides[-1] to always contain the right
# step value, see
# http://docs.scipy.org/doc/numpy-dev/release.html#npy-relaxed-strides-checking # noqa: E501
self.stride = arr.itemsize
if kind == 'ndenumerate':
# Zero-initialize the indices array.
indices = cgutils.alloca_once(
builder, zero.type,
size=context.get_constant(types.intp, arrty.ndim))
for dim in range(arrty.ndim):
idxptr = cgutils.gep_inbounds(builder, indices, dim)
builder.store(zero, idxptr)
self.indices = indices
# NOTE: Using gep() instead of explicit pointer addition helps
# LLVM vectorize the loop (since the stride is known and
# constant). This is not possible in the non-contiguous case,
# where the strides are unknown at compile-time.
def iternext_specific(self, context, builder, arrty, arr, result):
ndim = arrty.ndim
nitems = arr.nitems
index = builder.load(self.index)
is_valid = builder.icmp_signed('<', index, nitems)
result.set_valid(is_valid)
with cgutils.if_likely(builder, is_valid):
ptr = builder.gep(arr.data, [index])
value = load_item(context, builder, arrty, ptr)
if kind == 'flat':
result.yield_(value)
else:
# ndenumerate(): fetch and increment indices
indices = self.indices
idxvals = [builder.load(cgutils.gep_inbounds(builder,
indices,
dim))
for dim in range(ndim)]
idxtuple = cgutils.pack_array(builder, idxvals)
result.yield_(
cgutils.make_anonymous_struct(builder,
[idxtuple, value]))
_increment_indices_array(context, builder, arrty,
arr, indices)
index = cgutils.increment_index(builder, index)
builder.store(index, self.index)
def getitem(self, context, builder, arrty, arr, index):
ptr = builder.gep(arr.data, [index])
return load_item(context, builder, arrty, ptr)
def setitem(self, context, builder, arrty, arr, index, value):
ptr = builder.gep(arr.data, [index])
store_item(context, builder, arrty, value, ptr)
return CContiguousFlatIter
else:
class FlatIter(cgutils.create_struct_proxy(flatiterty)):
"""
Generic .flat() / .ndenumerate() implementation for
non-contiguous arrays.
It keeps track of pointers along each dimension in order to
minimize computations.
"""
def init_specific(self, context, builder, arrty, arr):
zero = context.get_constant(types.intp, 0)
data = arr.data
ndim = arrty.ndim
shapes = cgutils.unpack_tuple(builder, arr.shape, ndim)
indices = cgutils.alloca_once(
builder, zero.type, size=context.get_constant(types.intp,
arrty.ndim))
pointers = cgutils.alloca_once(
builder, data.type, size=context.get_constant(types.intp,
arrty.ndim))
exhausted = cgutils.alloca_once_value(builder,
cgutils.false_byte)
# Initialize indices and pointers with their start values.
for dim in range(ndim):
idxptr = cgutils.gep_inbounds(builder, indices, dim)
ptrptr = cgutils.gep_inbounds(builder, pointers, dim)
builder.store(data, ptrptr)
builder.store(zero, idxptr)
# 0-sized dimensions really indicate an empty array,
# but we have to catch that condition early to avoid
# a bug inside the iteration logic (see issue #846).
dim_size = shapes[dim]
dim_is_empty = builder.icmp_unsigned('==', dim_size, zero)
with cgutils.if_unlikely(builder, dim_is_empty):
builder.store(cgutils.true_byte, exhausted)
self.indices = indices
self.pointers = pointers
self.exhausted = exhausted
def iternext_specific(self, context, builder, arrty, arr, result):
ndim = arrty.ndim
shapes = cgutils.unpack_tuple(builder, arr.shape, ndim)
strides = cgutils.unpack_tuple(builder, arr.strides, ndim)
indices = self.indices
pointers = self.pointers
zero = context.get_constant(types.intp, 0)
bbend = builder.append_basic_block('end')
# Catch already computed iterator exhaustion
is_exhausted = cgutils.as_bool_bit(
builder, builder.load(self.exhausted))
with cgutils.if_unlikely(builder, is_exhausted):
result.set_valid(False)
builder.branch(bbend)
result.set_valid(True)
# Current pointer inside last dimension
last_ptr = cgutils.gep_inbounds(builder, pointers, ndim - 1)
ptr = builder.load(last_ptr)
value = load_item(context, builder, arrty, ptr)
if kind == 'flat':
result.yield_(value)
else:
# ndenumerate() => yield (indices, value)
idxvals = [builder.load(cgutils.gep_inbounds(builder,
indices,
dim))
for dim in range(ndim)]
idxtuple = cgutils.pack_array(builder, idxvals)
result.yield_(
cgutils.make_anonymous_struct(builder,
[idxtuple, value]))
# Update indices and pointers by walking from inner
# dimension to outer.
for dim in reversed(range(ndim)):
idxptr = cgutils.gep_inbounds(builder, indices, dim)
idx = cgutils.increment_index(builder,
builder.load(idxptr))
count = shapes[dim]
stride = strides[dim]
in_bounds = builder.icmp_signed('<', idx, count)
with cgutils.if_likely(builder, in_bounds):
# Index is valid => pointer can simply be incremented.
builder.store(idx, idxptr)
ptrptr = cgutils.gep_inbounds(builder, pointers, dim)
ptr = builder.load(ptrptr)
ptr = cgutils.pointer_add(builder, ptr, stride)
builder.store(ptr, ptrptr)
# Reset pointers in inner dimensions
for inner_dim in range(dim + 1, ndim):
ptrptr = cgutils.gep_inbounds(builder,
pointers,
inner_dim)
builder.store(ptr, ptrptr)
builder.branch(bbend)
# Reset index and continue with next dimension
builder.store(zero, idxptr)
# End of array
builder.store(cgutils.true_byte, self.exhausted)
builder.branch(bbend)
builder.position_at_end(bbend)
def _ptr_for_index(self, context, builder, arrty, arr, index):
ndim = arrty.ndim
shapes = cgutils.unpack_tuple(builder, arr.shape, count=ndim)
strides = cgutils.unpack_tuple(builder, arr.strides, count=ndim)
# First convert the flattened index into a regular n-dim index
indices = []
for dim in reversed(range(ndim)):
indices.append(builder.urem(index, shapes[dim]))
index = builder.udiv(index, shapes[dim])
indices.reverse()
ptr = cgutils.get_item_pointer2(context, builder, arr.data,
shapes, strides, arrty.layout,
indices)
return ptr
def getitem(self, context, builder, arrty, arr, index):
ptr = self._ptr_for_index(context, builder, arrty, arr, index)
return load_item(context, builder, arrty, ptr)
def setitem(self, context, builder, arrty, arr, index, value):
ptr = self._ptr_for_index(context, builder, arrty, arr, index)
store_item(context, builder, arrty, value, ptr)
return FlatIter
@lower_getattr(types.Array, "flat")
def make_array_flatiter(context, builder, arrty, arr):
flatitercls = make_array_flat_cls(types.NumpyFlatType(arrty))
flatiter = flatitercls(context, builder)
flatiter.array = arr
arrcls = context.make_array(arrty)
arr = arrcls(context, builder, ref=flatiter._get_ptr_by_name('array'))
flatiter.init_specific(context, builder, arrty, arr)
res = flatiter._getvalue()
return impl_ret_borrowed(context, builder, types.NumpyFlatType(arrty), res)
@lower_builtin('iternext', types.NumpyFlatType)
@iternext_impl(RefType.BORROWED)
def iternext_numpy_flatiter(context, builder, sig, args, result):
[flatiterty] = sig.args
[flatiter] = args
flatitercls = make_array_flat_cls(flatiterty)
flatiter = flatitercls(context, builder, value=flatiter)
arrty = flatiterty.array_type
arrcls = context.make_array(arrty)
arr = arrcls(context, builder, value=flatiter.array)
flatiter.iternext_specific(context, builder, arrty, arr, result)
@lower_builtin(operator.getitem, types.NumpyFlatType, types.Integer)
def iternext_numpy_getitem(context, builder, sig, args):
flatiterty = sig.args[0]
flatiter, index = args
flatitercls = make_array_flat_cls(flatiterty)
flatiter = flatitercls(context, builder, value=flatiter)
arrty = flatiterty.array_type
arrcls = context.make_array(arrty)
arr = arrcls(context, builder, value=flatiter.array)
res = flatiter.getitem(context, builder, arrty, arr, index)
return impl_ret_borrowed(context, builder, sig.return_type, res)
@lower_builtin(operator.setitem, types.NumpyFlatType, types.Integer,
types.Any)
def iternext_numpy_getitem_any(context, builder, sig, args):
flatiterty = sig.args[0]
flatiter, index, value = args
flatitercls = make_array_flat_cls(flatiterty)
flatiter = flatitercls(context, builder, value=flatiter)
arrty = flatiterty.array_type
arrcls = context.make_array(arrty)
arr = arrcls(context, builder, value=flatiter.array)
flatiter.setitem(context, builder, arrty, arr, index, value)
return context.get_dummy_value()
@lower_builtin(len, types.NumpyFlatType)
def iternext_numpy_getitem_flat(context, builder, sig, args):
flatiterty = sig.args[0]
flatitercls = make_array_flat_cls(flatiterty)
flatiter = flatitercls(context, builder, value=args[0])
arrcls = context.make_array(flatiterty.array_type)
arr = arrcls(context, builder, value=flatiter.array)
return arr.nitems
@lower_builtin(np.ndenumerate, types.Array)
def make_array_ndenumerate(context, builder, sig, args):
arrty, = sig.args
arr, = args
nditercls = make_array_ndenumerate_cls(types.NumpyNdEnumerateType(arrty))
nditer = nditercls(context, builder)
nditer.array = arr
arrcls = context.make_array(arrty)
arr = arrcls(context, builder, ref=nditer._get_ptr_by_name('array'))
nditer.init_specific(context, builder, arrty, arr)
res = nditer._getvalue()
return impl_ret_borrowed(context, builder, sig.return_type, res)
@lower_builtin('iternext', types.NumpyNdEnumerateType)
@iternext_impl(RefType.BORROWED)
def iternext_numpy_nditer(context, builder, sig, args, result):
[nditerty] = sig.args
[nditer] = args
nditercls = make_array_ndenumerate_cls(nditerty)
nditer = nditercls(context, builder, value=nditer)
arrty = nditerty.array_type
arrcls = context.make_array(arrty)
arr = arrcls(context, builder, value=nditer.array)
nditer.iternext_specific(context, builder, arrty, arr, result)
@lower_builtin(pndindex, types.VarArg(types.Integer))
@lower_builtin(np.ndindex, types.VarArg(types.Integer))
def make_array_ndindex(context, builder, sig, args):
"""ndindex(*shape)"""
shape = [context.cast(builder, arg, argty, types.intp)
for argty, arg in zip(sig.args, args)]
nditercls = make_ndindex_cls(types.NumpyNdIndexType(len(shape)))
nditer = nditercls(context, builder)
nditer.init_specific(context, builder, shape)
res = nditer._getvalue()
return impl_ret_borrowed(context, builder, sig.return_type, res)
@lower_builtin(pndindex, types.BaseTuple)
@lower_builtin(np.ndindex, types.BaseTuple)
def make_array_ndindex_tuple(context, builder, sig, args):
"""ndindex(shape)"""
ndim = sig.return_type.ndim
if ndim > 0:
idxty = sig.args[0].dtype
tup = args[0]
shape = cgutils.unpack_tuple(builder, tup, ndim)
shape = [context.cast(builder, idx, idxty, types.intp)
for idx in shape]
else:
shape = []
nditercls = make_ndindex_cls(types.NumpyNdIndexType(len(shape)))
nditer = nditercls(context, builder)
nditer.init_specific(context, builder, shape)
res = nditer._getvalue()
return impl_ret_borrowed(context, builder, sig.return_type, res)
@lower_builtin('iternext', types.NumpyNdIndexType)
@iternext_impl(RefType.BORROWED)
def iternext_numpy_ndindex(context, builder, sig, args, result):
[nditerty] = sig.args
[nditer] = args
nditercls = make_ndindex_cls(nditerty)
nditer = nditercls(context, builder, value=nditer)
nditer.iternext_specific(context, builder, result)
@lower_builtin(np.nditer, types.Any)
def make_array_nditer(context, builder, sig, args):
"""
nditer(...)
"""
nditerty = sig.return_type
arrtys = nditerty.arrays
if isinstance(sig.args[0], types.BaseTuple):
arrays = cgutils.unpack_tuple(builder, args[0])
else:
arrays = [args[0]]
nditer = make_nditer_cls(nditerty)(context, builder)
nditer.init_specific(context, builder, arrtys, arrays)
res = nditer._getvalue()
return impl_ret_borrowed(context, builder, nditerty, res)
@lower_builtin('iternext', types.NumpyNdIterType)
@iternext_impl(RefType.BORROWED)
def iternext_numpy_nditer2(context, builder, sig, args, result):
[nditerty] = sig.args
[nditer] = args
nditer = make_nditer_cls(nditerty)(context, builder, value=nditer)
nditer.iternext_specific(context, builder, result)
@lower_builtin(operator.eq, types.DType, types.DType)
def dtype_eq_impl(context, builder, sig, args):
arg1, arg2 = sig.args
res = ir.Constant(ir.IntType(1), int(arg1 == arg2))
return impl_ret_untracked(context, builder, sig.return_type, res)
# ------------------------------------------------------------------------------
# Numpy array constructors
def _empty_nd_impl(context, builder, arrtype, shapes):
"""Utility function used for allocating a new array during LLVM code
generation (lowering). Given a target context, builder, array
type, and a tuple or list of lowered dimension sizes, returns a
LLVM value pointing at a Numba runtime allocated array.
"""
arycls = make_array(arrtype)
ary = arycls(context, builder)
datatype = context.get_data_type(arrtype.dtype)
itemsize = context.get_constant(types.intp, get_itemsize(context, arrtype))
# compute array length
arrlen = context.get_constant(types.intp, 1)
overflow = Constant(ir.IntType(1), 0)
for s in shapes:
arrlen_mult = builder.smul_with_overflow(arrlen, s)
arrlen = builder.extract_value(arrlen_mult, 0)
overflow = builder.or_(
overflow, builder.extract_value(arrlen_mult, 1)
)
if arrtype.ndim == 0:
strides = ()
elif arrtype.layout == 'C':
strides = [itemsize]
for dimension_size in reversed(shapes[1:]):
strides.append(builder.mul(strides[-1], dimension_size))
strides = tuple(reversed(strides))
elif arrtype.layout == 'F':
strides = [itemsize]
for dimension_size in shapes[:-1]:
strides.append(builder.mul(strides[-1], dimension_size))
strides = tuple(strides)
else:
raise NotImplementedError(
"Don't know how to allocate array with layout '{0}'.".format(
arrtype.layout))
# Check overflow, numpy also does this after checking order
allocsize_mult = builder.smul_with_overflow(arrlen, itemsize)
allocsize = builder.extract_value(allocsize_mult, 0)
overflow = builder.or_(overflow, builder.extract_value(allocsize_mult, 1))
with builder.if_then(overflow, likely=False):
# Raise same error as numpy, see:
# https://github.com/numpy/numpy/blob/2a488fe76a0f732dc418d03b452caace161673da/numpy/core/src/multiarray/ctors.c#L1095-L1101 # noqa: E501
context.call_conv.return_user_exc(
builder, ValueError,
("array is too big; `arr.size * arr.dtype.itemsize` is larger than"
" the maximum possible size.",)
)
dtype = arrtype.dtype
align_val = context.get_preferred_array_alignment(dtype)
align = context.get_constant(types.uint32, align_val)
args = (context.get_dummy_value(), allocsize, align)
mip = types.MemInfoPointer(types.voidptr)
arytypeclass = types.TypeRef(type(arrtype))
argtypes = signature(mip, arytypeclass, types.intp, types.uint32)
meminfo = context.compile_internal(builder, _call_allocator, argtypes, args)
data = context.nrt.meminfo_data(builder, meminfo)
intp_t = context.get_value_type(types.intp)
shape_array = cgutils.pack_array(builder, shapes, ty=intp_t)
strides_array = cgutils.pack_array(builder, strides, ty=intp_t)
populate_array(ary,
data=builder.bitcast(data, datatype.as_pointer()),
shape=shape_array,
strides=strides_array,
itemsize=itemsize,
meminfo=meminfo)
return ary
@overload_classmethod(types.Array, "_allocate")
def _ol_array_allocate(cls, allocsize, align):
"""Implements a Numba-only default target (cpu) classmethod on the array
type.
"""
def impl(cls, allocsize, align):
return intrin_alloc(allocsize, align)
return impl
def _call_allocator(arrtype, size, align):
"""Trampoline to call the intrinsic used for allocation
"""
return arrtype._allocate(size, align)
@intrinsic
def intrin_alloc(typingctx, allocsize, align):
"""Intrinsic to call into the allocator for Array
"""
def codegen(context, builder, signature, args):
[allocsize, align] = args
meminfo = context.nrt.meminfo_alloc_aligned(builder, allocsize, align)
return meminfo
mip = types.MemInfoPointer(types.voidptr) # return untyped pointer
sig = signature(mip, allocsize, align)
return sig, codegen
def _parse_shape(context, builder, ty, val):
"""
Parse the shape argument to an array constructor.
"""
def safecast_intp(context, builder, src_t, src):
"""Cast src to intp only if value can be maintained"""
intp_t = context.get_value_type(types.intp)
intp_width = intp_t.width
intp_ir = ir.IntType(intp_width)
maxval = Constant(intp_ir, ((1 << intp_width - 1) - 1))
if src_t.width < intp_width:
res = builder.sext(src, intp_ir)
elif src_t.width >= intp_width:
is_larger = builder.icmp_signed(">", src, maxval)
with builder.if_then(is_larger, likely=False):
context.call_conv.return_user_exc(
builder, ValueError,
("Cannot safely convert value to intp",)
)
if src_t.width > intp_width:
res = builder.trunc(src, intp_ir)
else:
res = src
return res
if isinstance(ty, types.Integer):
ndim = 1
passed_shapes = [context.cast(builder, val, ty, types.intp)]
else:
assert isinstance(ty, types.BaseTuple)
ndim = ty.count
passed_shapes = cgutils.unpack_tuple(builder, val, count=ndim)
shapes = []
for s in passed_shapes:
shapes.append(safecast_intp(context, builder, s.type, s))
zero = context.get_constant_generic(builder, types.intp, 0)
for dim in range(ndim):
is_neg = builder.icmp_signed('<', shapes[dim], zero)
with cgutils.if_unlikely(builder, is_neg):
context.call_conv.return_user_exc(
builder, ValueError, ("negative dimensions not allowed",)
)
return shapes
def _parse_empty_args(context, builder, sig, args):
"""
Parse the arguments of a np.empty(), np.zeros() or np.ones() call.
"""
arrshapetype = sig.args[0]
arrshape = args[0]
arrtype = sig.return_type
return arrtype, _parse_shape(context, builder, arrshapetype, arrshape)
def _parse_empty_like_args(context, builder, sig, args):
"""
Parse the arguments of a np.empty_like(), np.zeros_like() or
np.ones_like() call.
"""
arytype = sig.args[0]
if isinstance(arytype, types.Array):
ary = make_array(arytype)(context, builder, value=args[0])
shapes = cgutils.unpack_tuple(builder, ary.shape, count=arytype.ndim)
return sig.return_type, shapes
else:
return sig.return_type, ()
def _check_const_str_dtype(fname, dtype):
if isinstance(dtype, types.UnicodeType):
msg = f"If np.{fname} dtype is a string it must be a string constant."
raise errors.TypingError(msg)
@intrinsic
def numpy_empty_nd(tyctx, ty_shape, ty_dtype, ty_retty_ref):
ty_retty = ty_retty_ref.instance_type
sig = ty_retty(ty_shape, ty_dtype, ty_retty_ref)
def codegen(cgctx, builder, sig, llargs):
arrtype, shapes = _parse_empty_args(cgctx, builder, sig, llargs)
ary = _empty_nd_impl(cgctx, builder, arrtype, shapes)
return ary._getvalue()
return sig, codegen
@overload(np.empty)
def ol_np_empty(shape, dtype=float):
_check_const_str_dtype("empty", dtype)
if (dtype is float or
(isinstance(dtype, types.Function) and dtype.typing_key is float) or
is_nonelike(dtype)): #default
nb_dtype = types.double
else:
nb_dtype = ty_parse_dtype(dtype)
ndim = ty_parse_shape(shape)
if nb_dtype is not None and ndim is not None:
retty = types.Array(dtype=nb_dtype, ndim=ndim, layout='C')
def impl(shape, dtype=float):
return numpy_empty_nd(shape, dtype, retty)
return impl
else:
msg = f"Cannot parse input types to function np.empty({shape}, {dtype})"
raise errors.TypingError(msg)
@intrinsic
def numpy_empty_like_nd(tyctx, ty_prototype, ty_dtype, ty_retty_ref):
ty_retty = ty_retty_ref.instance_type
sig = ty_retty(ty_prototype, ty_dtype, ty_retty_ref)
def codegen(cgctx, builder, sig, llargs):
arrtype, shapes = _parse_empty_like_args(cgctx, builder, sig, llargs)
ary = _empty_nd_impl(cgctx, builder, arrtype, shapes)
return ary._getvalue()
return sig, codegen
@overload(np.empty_like)
def ol_np_empty_like(arr, dtype=None):
_check_const_str_dtype("empty_like", dtype)
if not is_nonelike(dtype):
nb_dtype = ty_parse_dtype(dtype)
elif isinstance(arr, types.Array):
nb_dtype = arr.dtype
else:
nb_dtype = arr
if nb_dtype is not None:
if isinstance(arr, types.Array):
layout = arr.layout if arr.layout != 'A' else 'C'
retty = arr.copy(dtype=nb_dtype, layout=layout, readonly=False)
else:
retty = types.Array(nb_dtype, 0, 'C')
else:
msg = ("Cannot parse input types to function "
f"np.empty_like({arr}, {dtype})")
raise errors.TypingError(msg)
def impl(arr, dtype=None):
return numpy_empty_like_nd(arr, dtype, retty)
return impl
@intrinsic
def _zero_fill_array_method(tyctx, self):
sig = types.none(self)
def codegen(cgctx, builder, sig, llargs):
ary = make_array(sig.args[0])(cgctx, builder, llargs[0])
cgutils.memset(builder, ary.data, builder.mul(ary.itemsize, ary.nitems),
0)
return sig, codegen
@overload_method(types.Array, '_zero_fill')
def ol_array_zero_fill(self):
"""Adds a `._zero_fill` method to zero fill an array using memset."""
def impl(self):
_zero_fill_array_method(self)
return impl
@overload(np.zeros)
def ol_np_zeros(shape, dtype=float):
_check_const_str_dtype("zeros", dtype)
def impl(shape, dtype=float):
arr = np.empty(shape, dtype=dtype)
arr._zero_fill()
return arr
return impl
@overload(np.zeros_like)
def ol_np_zeros_like(a, dtype=None):
_check_const_str_dtype("zeros_like", dtype)
# NumPy uses 'a' as the arg name for the array-like
def impl(a, dtype=None):
arr = np.empty_like(a, dtype=dtype)
arr._zero_fill()
return arr
return impl
@overload(np.ones_like)
def ol_np_ones_like(a, dtype=None):
_check_const_str_dtype("ones_like", dtype)
# NumPy uses 'a' as the arg name for the array-like
def impl(a, dtype=None):
arr = np.empty_like(a, dtype=dtype)
arr_flat = arr.flat
for idx in range(len(arr_flat)):
arr_flat[idx] = 1
return arr
return impl
@overload(np.full)
def impl_np_full(shape, fill_value, dtype=None):
_check_const_str_dtype("full", dtype)
if not is_nonelike(dtype):
nb_dtype = ty_parse_dtype(dtype)
else:
nb_dtype = fill_value
def full(shape, fill_value, dtype=None):
arr = np.empty(shape, nb_dtype)
arr_flat = arr.flat
for idx in range(len(arr_flat)):
arr_flat[idx] = fill_value
return arr
return full
@overload(np.full_like)
def impl_np_full_like(a, fill_value, dtype=None):
_check_const_str_dtype("full_like", dtype)
def full_like(a, fill_value, dtype=None):
arr = np.empty_like(a, dtype)
arr_flat = arr.flat
for idx in range(len(arr_flat)):
arr_flat[idx] = fill_value
return arr
return full_like
@overload(np.ones)
def ol_np_ones(shape, dtype=None):
# for some reason the NumPy default for dtype is None in the source but
# ends up as np.float64 by definition.
_check_const_str_dtype("ones", dtype)
def impl(shape, dtype=None):
arr = np.empty(shape, dtype=dtype)
arr_flat = arr.flat
for idx in range(len(arr_flat)):
arr_flat[idx] = 1
return arr
return impl
@overload(np.identity)
def impl_np_identity(n, dtype=None):
_check_const_str_dtype("identity", dtype)
if not is_nonelike(dtype):
nb_dtype = ty_parse_dtype(dtype)
else:
nb_dtype = types.double
def identity(n, dtype=None):
arr = np.zeros((n, n), nb_dtype)
for i in range(n):
arr[i, i] = 1
return arr
return identity
def _eye_none_handler(N, M):
pass
@extending.overload(_eye_none_handler)
def _eye_none_handler_impl(N, M):
if isinstance(M, types.NoneType):
def impl(N, M):
return N
else:
def impl(N, M):
return M
return impl
@extending.overload(np.eye)
def numpy_eye(N, M=None, k=0, dtype=float):
if dtype is None or isinstance(dtype, types.NoneType):
dt = np.dtype(float)
elif isinstance(dtype, (types.DTypeSpec, types.Number)):
# dtype or instance of dtype
dt = as_dtype(getattr(dtype, 'dtype', dtype))
else:
dt = np.dtype(dtype)
def impl(N, M=None, k=0, dtype=float):
_M = _eye_none_handler(N, M)
arr = np.zeros((N, _M), dt)
if k >= 0:
d = min(N, _M - k)
for i in range(d):
arr[i, i + k] = 1
else:
d = min(N + k, _M)
for i in range(d):
arr[i - k, i] = 1
return arr
return impl
@overload(np.diag)
def impl_np_diag(v, k=0):
if not type_can_asarray(v):
raise errors.TypingError('The argument "v" must be array-like')
if isinstance(v, types.Array):
if v.ndim not in (1, 2):
raise errors.NumbaTypeError("Input must be 1- or 2-d.")
def diag_impl(v, k=0):
if v.ndim == 1:
s = v.shape
n = s[0] + abs(k)
ret = np.zeros((n, n), v.dtype)
if k >= 0:
for i in range(n - k):
ret[i, k + i] = v[i]
else:
for i in range(n + k):
ret[i - k, i] = v[i]
return ret
else: # 2-d
rows, cols = v.shape
if k < 0:
rows = rows + k
if k > 0:
cols = cols - k
n = max(min(rows, cols), 0)
ret = np.empty(n, v.dtype)
if k >= 0:
for i in range(n):
ret[i] = v[i, k + i]
else:
for i in range(n):
ret[i] = v[i - k, i]
return ret
return diag_impl
@overload(np.indices)
def numpy_indices(dimensions):
if not isinstance(dimensions, types.UniTuple):
msg = 'The argument "dimensions" must be a tuple of integers'
raise errors.TypingError(msg)
if not isinstance(dimensions.dtype, types.Integer):
msg = 'The argument "dimensions" must be a tuple of integers'
raise errors.TypingError(msg)
N = len(dimensions)
shape = (1,) * N
def impl(dimensions):
res = np.empty((N,) + dimensions, dtype=np.int64)
i = 0
for dim in dimensions:
idx = np.arange(dim, dtype=np.int64).reshape(
tuple_setitem(shape, i, dim)
)
res[i] = idx
i += 1
return res
return impl
@overload(np.diagflat)
def numpy_diagflat(v, k=0):
if not type_can_asarray(v):
msg = 'The argument "v" must be array-like'
raise errors.TypingError(msg)
if not isinstance(k, (int, types.Integer)):
msg = 'The argument "k" must be an integer'
raise errors.TypingError(msg)
def impl(v, k=0):
v = np.asarray(v)
v = v.ravel()
s = len(v)
abs_k = abs(k)
n = s + abs_k
res = np.zeros((n, n), v.dtype)
i = np.maximum(0, -k)
j = np.maximum(0, k)
for t in range(s):
res[i + t, j + t] = v[t]
return res
return impl
@overload(np.take)
@overload_method(types.Array, 'take')
def numpy_take(a, indices):
if isinstance(a, types.Array) and isinstance(indices, types.Integer):
def take_impl(a, indices):
if indices > (a.size - 1) or indices < -a.size:
raise IndexError("Index out of bounds")
return a.ravel()[indices]
return take_impl
if all(isinstance(arg, types.Array) for arg in [a, indices]):
F_order = indices.layout == 'F'
def take_impl(a, indices):
ret = np.empty(indices.size, dtype=a.dtype)
if F_order:
walker = indices.copy() # get C order
else:
walker = indices
it = np.nditer(walker)
i = 0
flat = a.ravel()
for x in it:
if x > (a.size - 1) or x < -a.size:
raise IndexError("Index out of bounds")
ret[i] = flat[x]
i = i + 1
return ret.reshape(indices.shape)
return take_impl
if isinstance(a, types.Array) and \
isinstance(indices, (types.List, types.BaseTuple)):
def take_impl(a, indices):
convert = np.array(indices)
ret = np.empty(convert.size, dtype=a.dtype)
it = np.nditer(convert)
i = 0
flat = a.ravel()
for x in it:
if x > (a.size - 1) or x < -a.size:
raise IndexError("Index out of bounds")
ret[i] = flat[x]
i = i + 1
return ret.reshape(convert.shape)
return take_impl
def _arange_dtype(*args):
bounds = [a for a in args if not isinstance(a, types.NoneType)]
if any(isinstance(a, types.Complex) for a in bounds):
dtype = types.complex128
elif any(isinstance(a, types.Float) for a in bounds):
dtype = types.float64
else:
# `np.arange(10).dtype` is always `np.dtype(int)`, aka `np.int_`, which
# in all released versions of numpy corresponds to the C `long` type.
# Windows 64 is broken by default here because Numba (as of 0.47) does
# not differentiate between Python and NumPy integers, so a `typeof(1)`
# on w64 is `int64`, i.e. `intp`. This means an arange(<some int>) will
# be typed as arange(int64) and the following will yield int64 opposed
# to int32. Example: without a load of analysis to work out of the args
# were wrapped in NumPy int*() calls it's not possible to detect the
# difference between `np.arange(10)` and `np.arange(np.int64(10)`.
NPY_TY = getattr(types, "int%s" % (8 * np.dtype(int).itemsize))
# unliteral these types such that `max` works.
unliteral_bounds = [types.unliteral(x) for x in bounds]
dtype = max(unliteral_bounds + [NPY_TY,])
return dtype
@overload(np.arange)
def np_arange(start, / ,stop=None, step=None, dtype=None):
if isinstance(stop, types.Optional):
stop = stop.type
if isinstance(step, types.Optional):
step = step.type
if isinstance(dtype, types.Optional):
dtype = dtype.type
if stop is None:
stop = types.none
if step is None:
step = types.none
if dtype is None:
dtype = types.none
if (not isinstance(start, types.Number) or
not isinstance(stop, (types.NoneType, types.Number)) or
not isinstance(step, (types.NoneType, types.Number)) or
not isinstance(dtype, (types.NoneType, types.DTypeSpec))):
return
if isinstance(dtype, types.NoneType):
true_dtype = _arange_dtype(start, stop, step)
else:
true_dtype = dtype.dtype
use_complex = any([isinstance(x, types.Complex)
for x in (start, stop, step)])
start_value = getattr(start, "literal_value", None)
stop_value = getattr(stop, "literal_value", None)
step_value = getattr(step, "literal_value", None)
def impl(start, /, stop=None, step=None, dtype=None):
# Allow for improved performance if given literal arguments.
lit_start = start_value if start_value is not None else start
lit_stop = stop_value if stop_value is not None else stop
lit_step = step_value if step_value is not None else step
_step = lit_step if lit_step is not None else 1
if lit_stop is None:
_start, _stop = 0, lit_start
else:
_start, _stop = lit_start, lit_stop
if _step == 0:
raise ValueError("Maximum allowed size exceeded")
nitems_c = (_stop - _start) / _step
nitems_r = int(math.ceil(nitems_c.real))
# Binary operator needed for compiler branch pruning.
if use_complex is True:
nitems_i = int(math.ceil(nitems_c.imag))
nitems = max(min(nitems_i, nitems_r), 0)
else:
nitems = max(nitems_r, 0)
arr = np.empty(nitems, true_dtype)
val = _start
for i in range(nitems):
arr[i] = val + (i * _step)
return arr
return impl
@overload(np.linspace)
def numpy_linspace(start, stop, num=50):
if not all(isinstance(arg, types.Number) for arg in [start, stop]):
return
if not isinstance(num, (int, types.Integer)):
msg = 'The argument "num" must be an integer'
raise errors.TypingError(msg)
if any(isinstance(arg, types.Complex) for arg in [start, stop]):
dtype = types.complex128
else:
dtype = types.float64
# Implementation based on https://github.com/numpy/numpy/blob/v1.20.0/numpy/core/function_base.py#L24 # noqa: E501
def linspace(start, stop, num=50):
arr = np.empty(num, dtype)
# The multiply by 1.0 mirrors
# https://github.com/numpy/numpy/blob/v1.20.0/numpy/core/function_base.py#L125-L128 # noqa: E501
# the side effect of this is important... start and stop become the same
# type as `dtype` i.e. 64/128 bits wide (float/complex). This is
# important later when used in the `np.divide`.
start = start * 1.0
stop = stop * 1.0
if num == 0:
return arr
div = num - 1
if div > 0:
delta = stop - start
step = np.divide(delta, div)
for i in range(0, num):
arr[i] = start + (i * step)
else:
arr[0] = start
if num > 1:
arr[-1] = stop
return arr
return linspace
def _array_copy(context, builder, sig, args):
"""
Array copy.
"""
arytype = sig.args[0]
ary = make_array(arytype)(context, builder, value=args[0])
shapes = cgutils.unpack_tuple(builder, ary.shape)
rettype = sig.return_type
ret = _empty_nd_impl(context, builder, rettype, shapes)
src_data = ary.data
dest_data = ret.data
assert rettype.layout in "CF"
if arytype.layout == rettype.layout:
# Fast path: memcpy
cgutils.raw_memcpy(builder, dest_data, src_data, ary.nitems,
ary.itemsize, align=1)
else:
src_strides = cgutils.unpack_tuple(builder, ary.strides)
dest_strides = cgutils.unpack_tuple(builder, ret.strides)
intp_t = context.get_value_type(types.intp)
with cgutils.loop_nest(builder, shapes, intp_t) as indices:
src_ptr = cgutils.get_item_pointer2(context, builder, src_data,
shapes, src_strides,
arytype.layout, indices)
dest_ptr = cgutils.get_item_pointer2(context, builder, dest_data,
shapes, dest_strides,
rettype.layout, indices)
builder.store(builder.load(src_ptr), dest_ptr)
return impl_ret_new_ref(context, builder, sig.return_type, ret._getvalue())
@intrinsic
def _array_copy_intrinsic(typingctx, a):
assert isinstance(a, types.Array)
layout = 'F' if a.layout == 'F' else 'C'
ret = a.copy(layout=layout, readonly=False)
sig = ret(a)
return sig, _array_copy
@lower_builtin("array.copy", types.Array)
def array_copy(context, builder, sig, args):
return _array_copy(context, builder, sig, args)
@overload(np.copy)
def impl_numpy_copy(a):
if isinstance(a, types.Array):
def numpy_copy(a):
return _array_copy_intrinsic(a)
return numpy_copy
def _as_layout_array(context, builder, sig, args, output_layout):
"""
Common logic for layout conversion function;
e.g. ascontiguousarray and asfortranarray
"""
retty = sig.return_type
aryty = sig.args[0]
assert retty.layout == output_layout, 'return-type has incorrect layout'
if aryty.ndim == 0:
# 0-dim input => asfortranarray() returns a 1-dim array
assert retty.ndim == 1
ary = make_array(aryty)(context, builder, value=args[0])
ret = make_array(retty)(context, builder)
shape = context.get_constant_generic(
builder, types.UniTuple(types.intp, 1), (1,),
)
strides = context.make_tuple(builder,
types.UniTuple(types.intp, 1),
(ary.itemsize,))
populate_array(ret, ary.data, shape, strides, ary.itemsize,
ary.meminfo, ary.parent)
return impl_ret_borrowed(context, builder, retty, ret._getvalue())
elif (retty.layout == aryty.layout
or (aryty.ndim == 1 and aryty.layout in 'CF')):
# 1-dim contiguous input => return the same array
return impl_ret_borrowed(context, builder, retty, args[0])
else:
if aryty.layout == 'A':
# There's still chance the array is in contiguous layout,
# just that we don't know at compile time.
# We can do a runtime check.
# Prepare and call is_contiguous or is_fortran
assert output_layout in 'CF'
check_func = is_contiguous if output_layout == 'C' else is_fortran
is_contig = _call_contiguous_check(check_func,
context,
builder,
aryty,
args[0])
with builder.if_else(is_contig) as (then, orelse):
# If the array is already contiguous, just return it
with then:
out_then = impl_ret_borrowed(context, builder, retty,
args[0])
then_blk = builder.block
# Otherwise, copy to a new contiguous region
with orelse:
out_orelse = _array_copy(context, builder, sig, args)
orelse_blk = builder.block
# Phi node for the return value
ret_phi = builder.phi(out_then.type)
ret_phi.add_incoming(out_then, then_blk)
ret_phi.add_incoming(out_orelse, orelse_blk)
return ret_phi
else:
# Return a copy with the right layout
return _array_copy(context, builder, sig, args)
@intrinsic
def _as_layout_array_intrinsic(typingctx, a, output_layout):
if not isinstance(output_layout, types.StringLiteral):
raise errors.RequireLiteralValue(output_layout)
ret = a.copy(layout=output_layout.literal_value, ndim=max(a.ndim, 1))
sig = ret(a, output_layout)
return sig, lambda c, b, s, a: _as_layout_array(
c, b, s, a, output_layout=output_layout.literal_value)
@overload(np.ascontiguousarray)
def array_ascontiguousarray(a):
if not type_can_asarray(a):
raise errors.TypingError('The argument "a" must be array-like')
if isinstance(a, (types.Number, types.Boolean,)):
def impl(a):
return np.ascontiguousarray(np.array(a))
elif isinstance(a, types.Array):
def impl(a):
return _as_layout_array_intrinsic(a, 'C')
return impl
@overload(np.asfortranarray)
def array_asfortranarray(a):
if not type_can_asarray(a):
raise errors.TypingError('The argument "a" must be array-like')
if isinstance(a, (types.Number, types.Boolean,)):
def impl(a):
return np.asfortranarray(np.array(a))
return impl
elif isinstance(a, types.Array):
def impl(a):
return _as_layout_array_intrinsic(a, 'F')
return impl
@lower_builtin("array.astype", types.Array, types.DTypeSpec)
@lower_builtin("array.astype", types.Array, types.StringLiteral)
def array_astype(context, builder, sig, args):
arytype = sig.args[0]
ary = make_array(arytype)(context, builder, value=args[0])
shapes = cgutils.unpack_tuple(builder, ary.shape)
rettype = sig.return_type
ret = _empty_nd_impl(context, builder, rettype, shapes)
src_data = ary.data
dest_data = ret.data
src_strides = cgutils.unpack_tuple(builder, ary.strides)
dest_strides = cgutils.unpack_tuple(builder, ret.strides)
intp_t = context.get_value_type(types.intp)
with cgutils.loop_nest(builder, shapes, intp_t) as indices:
src_ptr = cgutils.get_item_pointer2(context, builder, src_data,
shapes, src_strides,
arytype.layout, indices)
dest_ptr = cgutils.get_item_pointer2(context, builder, dest_data,
shapes, dest_strides,
rettype.layout, indices)
item = load_item(context, builder, arytype, src_ptr)
item = context.cast(builder, item, arytype.dtype, rettype.dtype)
store_item(context, builder, rettype, item, dest_ptr)
return impl_ret_new_ref(context, builder, sig.return_type, ret._getvalue())
@intrinsic
def np_frombuffer(typingctx, buffer, dtype, retty):
ty = retty.instance_type
sig = ty(buffer, dtype, retty)
def codegen(context, builder, sig, args):
bufty = sig.args[0]
aryty = sig.return_type
buf = make_array(bufty)(context, builder, value=args[0])
out_ary_ty = make_array(aryty)
out_ary = out_ary_ty(context, builder)
out_datamodel = out_ary._datamodel
itemsize = get_itemsize(context, aryty)
ll_itemsize = Constant(buf.itemsize.type, itemsize)
nbytes = builder.mul(buf.nitems, buf.itemsize)
# Check that the buffer size is compatible
rem = builder.srem(nbytes, ll_itemsize)
is_incompatible = cgutils.is_not_null(builder, rem)
with builder.if_then(is_incompatible, likely=False):
msg = "buffer size must be a multiple of element size"
context.call_conv.return_user_exc(builder, ValueError, (msg,))
shape = cgutils.pack_array(builder, [builder.sdiv(nbytes, ll_itemsize)])
strides = cgutils.pack_array(builder, [ll_itemsize])
data = builder.bitcast(
buf.data, context.get_value_type(out_datamodel.get_type('data'))
)
populate_array(out_ary,
data=data,
shape=shape,
strides=strides,
itemsize=ll_itemsize,
meminfo=buf.meminfo,
parent=buf.parent,)
res = out_ary._getvalue()
return impl_ret_borrowed(context, builder, sig.return_type, res)
return sig, codegen
@overload(np.frombuffer)
def impl_np_frombuffer(buffer, dtype=float):
_check_const_str_dtype("frombuffer", dtype)
if not isinstance(buffer, types.Buffer) or buffer.layout != 'C':
msg = f'Argument "buffer" must be buffer-like. Got {buffer}'
raise errors.TypingError(msg)
if (dtype is float or
(isinstance(dtype, types.Function) and dtype.typing_key is float) or
is_nonelike(dtype)): #default
nb_dtype = types.double
else:
nb_dtype = ty_parse_dtype(dtype)
if nb_dtype is not None:
retty = types.Array(dtype=nb_dtype, ndim=1, layout='C',
readonly=not buffer.mutable)
else:
msg = ("Cannot parse input types to function "
f"np.frombuffer({buffer}, {dtype})")
raise errors.TypingError(msg)
def impl(buffer, dtype=float):
return np_frombuffer(buffer, dtype, retty)
return impl
@overload(carray)
def impl_carray(ptr, shape, dtype=None):
if is_nonelike(dtype):
intrinsic_cfarray = get_cfarray_intrinsic('C', None)
def impl(ptr, shape, dtype=None):
return intrinsic_cfarray(ptr, shape)
return impl
elif isinstance(dtype, types.DTypeSpec):
intrinsic_cfarray = get_cfarray_intrinsic('C', dtype)
def impl(ptr, shape, dtype=None):
return intrinsic_cfarray(ptr, shape)
return impl
@overload(farray)
def impl_farray(ptr, shape, dtype=None):
if is_nonelike(dtype):
intrinsic_cfarray = get_cfarray_intrinsic('F', None)
def impl(ptr, shape, dtype=None):
return intrinsic_cfarray(ptr, shape)
return impl
elif isinstance(dtype, types.DTypeSpec):
intrinsic_cfarray = get_cfarray_intrinsic('F', dtype)
def impl(ptr, shape, dtype=None):
return intrinsic_cfarray(ptr, shape)
return impl
def get_cfarray_intrinsic(layout, dtype_):
@intrinsic
def intrinsic_cfarray(typingctx, ptr, shape):
if ptr is types.voidptr:
ptr_dtype = None
elif isinstance(ptr, types.CPointer):
ptr_dtype = ptr.dtype
else:
msg = f"pointer argument expected, got '{ptr}'"
raise errors.NumbaTypeError(msg)
if dtype_ is None:
if ptr_dtype is None:
msg = "explicit dtype required for void* argument"
raise errors.NumbaTypeError(msg)
dtype = ptr_dtype
elif isinstance(dtype_, types.DTypeSpec):
dtype = dtype_.dtype
if ptr_dtype is not None and dtype != ptr_dtype:
msg = f"mismatching dtype '{dtype}' for pointer type '{ptr}'"
raise errors.NumbaTypeError(msg)
else:
msg = f"invalid dtype spec '{dtype_}'"
raise errors.NumbaTypeError(msg)
ndim = ty_parse_shape(shape)
if ndim is None:
msg = f"invalid shape '{shape}'"
raise errors.NumbaTypeError(msg)
retty = types.Array(dtype, ndim, layout)
sig = signature(retty, ptr, shape)
return sig, np_cfarray
return intrinsic_cfarray
def np_cfarray(context, builder, sig, args):
"""
numba.numpy_support.carray(...) and
numba.numpy_support.farray(...).
"""
ptrty, shapety = sig.args[:2]
ptr, shape = args[:2]
aryty = sig.return_type
assert aryty.layout in 'CF'
out_ary = make_array(aryty)(context, builder)
itemsize = get_itemsize(context, aryty)
ll_itemsize = cgutils.intp_t(itemsize)
if isinstance(shapety, types.BaseTuple):
shapes = cgutils.unpack_tuple(builder, shape)
else:
shapety = (shapety,)
shapes = (shape,)
shapes = [context.cast(builder, value, fromty, types.intp)
for fromty, value in zip(shapety, shapes)]
off = ll_itemsize
strides = []
if aryty.layout == 'F':
for s in shapes:
strides.append(off)
off = builder.mul(off, s)
else:
for s in reversed(shapes):
strides.append(off)
off = builder.mul(off, s)
strides.reverse()
data = builder.bitcast(ptr,
context.get_data_type(aryty.dtype).as_pointer())
populate_array(out_ary,
data=data,
shape=shapes,
strides=strides,
itemsize=ll_itemsize,
# Array is not memory-managed
meminfo=None,
)
res = out_ary._getvalue()
return impl_ret_new_ref(context, builder, sig.return_type, res)
def _get_seq_size(context, builder, seqty, seq):
if isinstance(seqty, types.BaseTuple):
return context.get_constant(types.intp, len(seqty))
elif isinstance(seqty, types.Sequence):
len_impl = context.get_function(len, signature(types.intp, seqty,))
return len_impl(builder, (seq,))
else:
assert 0
def _get_borrowing_getitem(context, seqty):
"""
Return a getitem() implementation that doesn't incref its result.
"""
retty = seqty.dtype
getitem_impl = context.get_function(operator.getitem,
signature(retty, seqty, types.intp))
def wrap(builder, args):
ret = getitem_impl(builder, args)
if context.enable_nrt:
context.nrt.decref(builder, retty, ret)
return ret
return wrap
def compute_sequence_shape(context, builder, ndim, seqty, seq):
"""
Compute the likely shape of a nested sequence (possibly 0d).
"""
intp_t = context.get_value_type(types.intp)
zero = Constant(intp_t, 0)
def get_first_item(seqty, seq):
if isinstance(seqty, types.BaseTuple):
if len(seqty) == 0:
return None, None
else:
return seqty[0], builder.extract_value(seq, 0)
else:
getitem_impl = _get_borrowing_getitem(context, seqty)
return seqty.dtype, getitem_impl(builder, (seq, zero))
# Compute shape by traversing the first element of each nested
# sequence
shapes = []
innerty, inner = seqty, seq
for i in range(ndim):
if i > 0:
innerty, inner = get_first_item(innerty, inner)
shapes.append(_get_seq_size(context, builder, innerty, inner))
return tuple(shapes)
def check_sequence_shape(context, builder, seqty, seq, shapes):
"""
Check the nested sequence matches the given *shapes*.
"""
def _fail():
context.call_conv.return_user_exc(builder, ValueError,
("incompatible sequence shape",))
def check_seq_size(seqty, seq, shapes):
if len(shapes) == 0:
return
size = _get_seq_size(context, builder, seqty, seq)
expected = shapes[0]
mismatch = builder.icmp_signed('!=', size, expected)
with builder.if_then(mismatch, likely=False):
_fail()
if len(shapes) == 1:
return
if isinstance(seqty, types.Sequence):
getitem_impl = _get_borrowing_getitem(context, seqty)
with cgutils.for_range(builder, size) as loop:
innerty = seqty.dtype
inner = getitem_impl(builder, (seq, loop.index))
check_seq_size(innerty, inner, shapes[1:])
elif isinstance(seqty, types.BaseTuple):
for i in range(len(seqty)):
innerty = seqty[i]
inner = builder.extract_value(seq, i)
check_seq_size(innerty, inner, shapes[1:])
else:
assert 0, seqty
check_seq_size(seqty, seq, shapes)
def assign_sequence_to_array(context, builder, data, shapes, strides,
arrty, seqty, seq):
"""
Assign a nested sequence contents to an array. The shape must match
the sequence's structure.
"""
def assign_item(indices, valty, val):
ptr = cgutils.get_item_pointer2(context, builder, data, shapes, strides,
arrty.layout, indices, wraparound=False)
val = context.cast(builder, val, valty, arrty.dtype)
store_item(context, builder, arrty, val, ptr)
def assign(seqty, seq, shapes, indices):
if len(shapes) == 0:
assert not isinstance(seqty, (types.Sequence, types.BaseTuple))
assign_item(indices, seqty, seq)
return
size = shapes[0]
if isinstance(seqty, types.Sequence):
getitem_impl = _get_borrowing_getitem(context, seqty)
with cgutils.for_range(builder, size) as loop:
innerty = seqty.dtype
inner = getitem_impl(builder, (seq, loop.index))
assign(innerty, inner, shapes[1:], indices + (loop.index,))
elif isinstance(seqty, types.BaseTuple):
for i in range(len(seqty)):
innerty = seqty[i]
inner = builder.extract_value(seq, i)
index = context.get_constant(types.intp, i)
assign(innerty, inner, shapes[1:], indices + (index,))
else:
assert 0, seqty
assign(seqty, seq, shapes, ())
def np_array_typer(typingctx, object, dtype):
ndim, seq_dtype = _parse_nested_sequence(typingctx, object)
if is_nonelike(dtype):
dtype = seq_dtype
else:
dtype = ty_parse_dtype(dtype)
if dtype is None:
return
return types.Array(dtype, ndim, 'C')
@intrinsic
def np_array(typingctx, obj, dtype):
_check_const_str_dtype("array", dtype)
ret = np_array_typer(typingctx, obj, dtype)
sig = ret(obj, dtype)
def codegen(context, builder, sig, args):
arrty = sig.return_type
ndim = arrty.ndim
seqty = sig.args[0]
seq = args[0]
shapes = compute_sequence_shape(context, builder, ndim, seqty, seq)
assert len(shapes) == ndim
check_sequence_shape(context, builder, seqty, seq, shapes)
arr = _empty_nd_impl(context, builder, arrty, shapes)
assign_sequence_to_array(context, builder, arr.data, shapes,
arr.strides, arrty, seqty, seq)
return impl_ret_new_ref(context, builder, sig.return_type,
arr._getvalue())
return sig, codegen
@overload(np.array)
def impl_np_array(object, dtype=None):
_check_const_str_dtype("array", dtype)
if not type_can_asarray(object):
raise errors.TypingError('The argument "object" must '
'be array-like')
if not is_nonelike(dtype) and ty_parse_dtype(dtype) is None:
msg = 'The argument "dtype" must be a data-type if it is provided'
raise errors.TypingError(msg)
def impl(object, dtype=None):
return np_array(object, dtype)
return impl
def _normalize_axis(context, builder, func_name, ndim, axis):
zero = axis.type(0)
ll_ndim = axis.type(ndim)
# Normalize negative axis
is_neg_axis = builder.icmp_signed('<', axis, zero)
axis = builder.select(is_neg_axis, builder.add(axis, ll_ndim), axis)
# Check axis for bounds
axis_out_of_bounds = builder.or_(
builder.icmp_signed('<', axis, zero),
builder.icmp_signed('>=', axis, ll_ndim))
with builder.if_then(axis_out_of_bounds, likely=False):
msg = "%s(): axis out of bounds" % func_name
context.call_conv.return_user_exc(builder, IndexError, (msg,))
return axis
def _insert_axis_in_shape(context, builder, orig_shape, ndim, axis):
"""
Compute shape with the new axis inserted
e.g. given original shape (2, 3, 4) and axis=2,
the returned new shape is (2, 3, 1, 4).
"""
assert len(orig_shape) == ndim - 1
ll_shty = ir.ArrayType(cgutils.intp_t, ndim)
shapes = cgutils.alloca_once(builder, ll_shty)
one = cgutils.intp_t(1)
# 1. copy original sizes at appropriate places
for dim in range(ndim - 1):
ll_dim = cgutils.intp_t(dim)
after_axis = builder.icmp_signed('>=', ll_dim, axis)
sh = orig_shape[dim]
idx = builder.select(after_axis,
builder.add(ll_dim, one),
ll_dim)
builder.store(sh, cgutils.gep_inbounds(builder, shapes, 0, idx))
# 2. insert new size (1) at axis dimension
builder.store(one, cgutils.gep_inbounds(builder, shapes, 0, axis))
return cgutils.unpack_tuple(builder, builder.load(shapes))
def _insert_axis_in_strides(context, builder, orig_strides, ndim, axis):
"""
Same as _insert_axis_in_shape(), but with a strides array.
"""
assert len(orig_strides) == ndim - 1
ll_shty = ir.ArrayType(cgutils.intp_t, ndim)
strides = cgutils.alloca_once(builder, ll_shty)
one = cgutils.intp_t(1)
zero = cgutils.intp_t(0)
# 1. copy original strides at appropriate places
for dim in range(ndim - 1):
ll_dim = cgutils.intp_t(dim)
after_axis = builder.icmp_signed('>=', ll_dim, axis)
idx = builder.select(after_axis,
builder.add(ll_dim, one),
ll_dim)
builder.store(orig_strides[dim],
cgutils.gep_inbounds(builder, strides, 0, idx))
# 2. insert new stride at axis dimension
# (the value is indifferent for a 1-sized dimension, we use 0)
builder.store(zero, cgutils.gep_inbounds(builder, strides, 0, axis))
return cgutils.unpack_tuple(builder, builder.load(strides))
def expand_dims(context, builder, sig, args, axis):
"""
np.expand_dims() with the given axis.
"""
retty = sig.return_type
ndim = retty.ndim
arrty = sig.args[0]
arr = make_array(arrty)(context, builder, value=args[0])
ret = make_array(retty)(context, builder)
shapes = cgutils.unpack_tuple(builder, arr.shape)
strides = cgutils.unpack_tuple(builder, arr.strides)
new_shapes = _insert_axis_in_shape(context, builder, shapes, ndim, axis)
new_strides = _insert_axis_in_strides(context, builder, strides, ndim, axis)
populate_array(ret,
data=arr.data,
shape=new_shapes,
strides=new_strides,
itemsize=arr.itemsize,
meminfo=arr.meminfo,
parent=arr.parent)
return ret._getvalue()
@intrinsic
def np_expand_dims(typingctx, a, axis):
layout = a.layout if a.ndim <= 1 else 'A'
ret = a.copy(ndim=a.ndim + 1, layout=layout)
sig = ret(a, axis)
def codegen(context, builder, sig, args):
axis = context.cast(builder, args[1], sig.args[1], types.intp)
axis = _normalize_axis(context, builder, "np.expand_dims",
sig.return_type.ndim, axis)
ret = expand_dims(context, builder, sig, args, axis)
return impl_ret_borrowed(context, builder, sig.return_type, ret)
return sig, codegen
@overload(np.expand_dims)
def impl_np_expand_dims(a, axis):
if not isinstance(a, types.Array):
msg = f'First argument "a" must be an array. Got {a}'
raise errors.TypingError(msg)
if not isinstance(axis, types.Integer):
msg = f'Argument "axis" must be an integer. Got {axis}'
raise errors.TypingError(msg)
def impl(a, axis):
return np_expand_dims(a, axis)
return impl
def _atleast_nd(minimum, axes):
@intrinsic
def impl(typingcontext, *args):
arrtys = args
rettys = [arg.copy(ndim=max(arg.ndim, minimum)) for arg in args]
def codegen(context, builder, sig, args):
transform = _atleast_nd_transform(minimum, axes)
arrs = cgutils.unpack_tuple(builder, args[0])
rets = [transform(context, builder, arr, arrty, retty)
for arr, arrty, retty in zip(arrs, arrtys, rettys)]
if len(rets) > 1:
ret = context.make_tuple(builder, sig.return_type, rets)
else:
ret = rets[0]
return impl_ret_borrowed(context, builder, sig.return_type, ret)
return signature(types.Tuple(rettys) if len(rettys) > 1 else rettys[0],
types.StarArgTuple.from_types(args)), codegen
return lambda *args: impl(*args)
def _atleast_nd_transform(min_ndim, axes):
"""
Return a callback successively inserting 1-sized dimensions at the
following axes.
"""
assert min_ndim == len(axes)
def transform(context, builder, arr, arrty, retty):
for i in range(min_ndim):
ndim = i + 1
if arrty.ndim < ndim:
axis = cgutils.intp_t(axes[i])
newarrty = arrty.copy(ndim=arrty.ndim + 1)
arr = expand_dims(context, builder,
typing.signature(newarrty, arrty), (arr,),
axis)
arrty = newarrty
return arr
return transform
@overload(np.atleast_1d)
def np_atleast_1d(*args):
if all(isinstance(arg, types.Array) for arg in args):
return _atleast_nd(1, [0])
@overload(np.atleast_2d)
def np_atleast_2d(*args):
if all(isinstance(arg, types.Array) for arg in args):
return _atleast_nd(2, [0, 0])
@overload(np.atleast_3d)
def np_atleast_3d(*args):
if all(isinstance(arg, types.Array) for arg in args):
return _atleast_nd(3, [0, 0, 2])
def _do_concatenate(context, builder, axis,
arrtys, arrs, arr_shapes, arr_strides,
retty, ret_shapes):
"""
Concatenate arrays along the given axis.
"""
assert len(arrtys) == len(arrs) == len(arr_shapes) == len(arr_strides)
zero = cgutils.intp_t(0)
# Allocate return array
ret = _empty_nd_impl(context, builder, retty, ret_shapes)
ret_strides = cgutils.unpack_tuple(builder, ret.strides)
# Compute the offset by which to bump the destination pointer
# after copying each input array.
# Morally, we need to copy each input array at different start indices
# into the destination array; bumping the destination pointer
# is simply easier than offsetting all destination indices.
copy_offsets = []
for arr_sh in arr_shapes:
# offset = ret_strides[axis] * input_shape[axis]
offset = zero
for dim, (size, stride) in enumerate(zip(arr_sh, ret_strides)):
is_axis = builder.icmp_signed('==', axis.type(dim), axis)
addend = builder.mul(size, stride)
offset = builder.select(is_axis,
builder.add(offset, addend),
offset)
copy_offsets.append(offset)
# Copy input arrays into the return array
ret_data = ret.data
for arrty, arr, arr_sh, arr_st, offset in zip(arrtys, arrs, arr_shapes,
arr_strides, copy_offsets):
arr_data = arr.data
# Do the copy loop
# Note the loop nesting is optimized for the destination layout
loop_nest = cgutils.loop_nest(builder, arr_sh, cgutils.intp_t,
order=retty.layout)
with loop_nest as indices:
src_ptr = cgutils.get_item_pointer2(context, builder, arr_data,
arr_sh, arr_st,
arrty.layout, indices)
val = load_item(context, builder, arrty, src_ptr)
val = context.cast(builder, val, arrty.dtype, retty.dtype)
dest_ptr = cgutils.get_item_pointer2(context, builder, ret_data,
ret_shapes, ret_strides,
retty.layout, indices)
store_item(context, builder, retty, val, dest_ptr)
# Bump destination pointer
ret_data = cgutils.pointer_add(builder, ret_data, offset)
return ret
def _np_concatenate(context, builder, arrtys, arrs, retty, axis):
ndim = retty.ndim
arrs = [make_array(aty)(context, builder, value=a)
for aty, a in zip(arrtys, arrs)]
axis = _normalize_axis(context, builder, "np.concatenate", ndim, axis)
# Get input shapes
arr_shapes = [cgutils.unpack_tuple(builder, arr.shape) for arr in arrs]
arr_strides = [cgutils.unpack_tuple(builder, arr.strides) for arr in arrs]
# Compute return shape:
# - the dimension for the concatenation axis is summed over all inputs
# - other dimensions must match exactly for each input
ret_shapes = [cgutils.alloca_once_value(builder, sh)
for sh in arr_shapes[0]]
for dim in range(ndim):
is_axis = builder.icmp_signed('==', axis.type(dim), axis)
ret_shape_ptr = ret_shapes[dim]
ret_sh = builder.load(ret_shape_ptr)
other_shapes = [sh[dim] for sh in arr_shapes[1:]]
with builder.if_else(is_axis) as (on_axis, on_other_dim):
with on_axis:
sh = functools.reduce(
builder.add,
other_shapes + [ret_sh])
builder.store(sh, ret_shape_ptr)
with on_other_dim:
is_ok = cgutils.true_bit
for sh in other_shapes:
is_ok = builder.and_(is_ok,
builder.icmp_signed('==', sh, ret_sh))
with builder.if_then(builder.not_(is_ok), likely=False):
context.call_conv.return_user_exc(
builder, ValueError,
("np.concatenate(): input sizes over "
"dimension %d do not match" % dim,))
ret_shapes = [builder.load(sh) for sh in ret_shapes]
ret = _do_concatenate(context, builder, axis,
arrtys, arrs, arr_shapes, arr_strides,
retty, ret_shapes)
return impl_ret_new_ref(context, builder, retty, ret._getvalue())
def _np_stack(context, builder, arrtys, arrs, retty, axis):
ndim = retty.ndim
zero = cgutils.intp_t(0)
one = cgutils.intp_t(1)
ll_narrays = cgutils.intp_t(len(arrs))
arrs = [make_array(aty)(context, builder, value=a)
for aty, a in zip(arrtys, arrs)]
axis = _normalize_axis(context, builder, "np.stack", ndim, axis)
# Check input arrays have the same shape
orig_shape = cgutils.unpack_tuple(builder, arrs[0].shape)
for arr in arrs[1:]:
is_ok = cgutils.true_bit
for sh, orig_sh in zip(cgutils.unpack_tuple(builder, arr.shape),
orig_shape):
is_ok = builder.and_(is_ok, builder.icmp_signed('==', sh, orig_sh))
with builder.if_then(builder.not_(is_ok), likely=False):
context.call_conv.return_user_exc(
builder, ValueError,
("np.stack(): all input arrays must have the same shape",))
orig_strides = [cgutils.unpack_tuple(builder, arr.strides) for arr in arrs]
# Compute input shapes and return shape with the new axis inserted
# e.g. given 5 input arrays of shape (2, 3, 4) and axis=1,
# corrected input shape is (2, 1, 3, 4) and return shape is (2, 5, 3, 4).
ll_shty = ir.ArrayType(cgutils.intp_t, ndim)
input_shapes = cgutils.alloca_once(builder, ll_shty)
ret_shapes = cgutils.alloca_once(builder, ll_shty)
# 1. copy original sizes at appropriate places
for dim in range(ndim - 1):
ll_dim = cgutils.intp_t(dim)
after_axis = builder.icmp_signed('>=', ll_dim, axis)
sh = orig_shape[dim]
idx = builder.select(after_axis,
builder.add(ll_dim, one),
ll_dim)
builder.store(sh, cgutils.gep_inbounds(builder, input_shapes, 0, idx))
builder.store(sh, cgutils.gep_inbounds(builder, ret_shapes, 0, idx))
# 2. insert new size at axis dimension
builder.store(one, cgutils.gep_inbounds(builder, input_shapes, 0, axis))
builder.store(ll_narrays, cgutils.gep_inbounds(builder,
ret_shapes,
0,
axis))
input_shapes = cgutils.unpack_tuple(builder, builder.load(input_shapes))
input_shapes = [input_shapes] * len(arrs)
ret_shapes = cgutils.unpack_tuple(builder, builder.load(ret_shapes))
# Compute input strides for each array with the new axis inserted
input_strides = [cgutils.alloca_once(builder, ll_shty)
for i in range(len(arrs))]
# 1. copy original strides at appropriate places
for dim in range(ndim - 1):
ll_dim = cgutils.intp_t(dim)
after_axis = builder.icmp_signed('>=', ll_dim, axis)
idx = builder.select(after_axis,
builder.add(ll_dim, one),
ll_dim)
for i in range(len(arrs)):
builder.store(orig_strides[i][dim],
cgutils.gep_inbounds(builder, input_strides[i], 0,
idx))
# 2. insert new stride at axis dimension
# (the value is indifferent for a 1-sized dimension, we put 0)
for i in range(len(arrs)):
builder.store(zero, cgutils.gep_inbounds(builder, input_strides[i], 0,
axis))
input_strides = [cgutils.unpack_tuple(builder, builder.load(st))
for st in input_strides]
# Create concatenated array
ret = _do_concatenate(context, builder, axis,
arrtys, arrs, input_shapes, input_strides,
retty, ret_shapes)
return impl_ret_new_ref(context, builder, retty, ret._getvalue())
def np_concatenate_typer(typingctx, arrays, axis):
if axis is not None and not isinstance(axis, types.Integer):
# Note Numpy allows axis=None, but it isn't documented:
# https://github.com/numpy/numpy/issues/7968
return
# does type checking
dtype, ndim = _sequence_of_arrays(typingctx,
"np.concatenate", arrays)
if ndim == 0:
raise TypeError("zero-dimensional arrays cannot be concatenated")
layout = _choose_concatenation_layout(arrays)
return types.Array(dtype, ndim, layout)
@intrinsic
def np_concatenate(typingctx, arrays, axis):
ret = np_concatenate_typer(typingctx, arrays, axis)
assert isinstance(ret, types.Array)
sig = ret(arrays, axis)
def codegen(context, builder, sig, args):
axis = context.cast(builder, args[1], sig.args[1], types.intp)
return _np_concatenate(context, builder,
list(sig.args[0]),
cgutils.unpack_tuple(builder, args[0]),
sig.return_type,
axis)
return sig, codegen
@overload(np.concatenate)
def impl_np_concatenate(arrays, axis=0):
if isinstance(arrays, types.BaseTuple):
def impl(arrays, axis=0):
return np_concatenate(arrays, axis)
return impl
def _column_stack_dims(context, func_name, arrays):
# column_stack() allows stacking 1-d and 2-d arrays together
for a in arrays:
if a.ndim < 1 or a.ndim > 2:
raise TypeError("np.column_stack() is only defined on "
"1-d and 2-d arrays")
return 2
@intrinsic
def np_column_stack(typingctx, tup):
dtype, ndim = _sequence_of_arrays(typingctx,
"np.column_stack", tup,
dim_chooser=_column_stack_dims)
layout = _choose_concatenation_layout(tup)
ret = types.Array(dtype, ndim, layout)
sig = ret(tup)
def codegen(context, builder, sig, args):
orig_arrtys = list(sig.args[0])
orig_arrs = cgutils.unpack_tuple(builder, args[0])
arrtys = []
arrs = []
axis = context.get_constant(types.intp, 1)
for arrty, arr in zip(orig_arrtys, orig_arrs):
if arrty.ndim == 2:
arrtys.append(arrty)
arrs.append(arr)
else:
# Convert 1d array to 2d column array: np.expand_dims(a, 1)
assert arrty.ndim == 1
newty = arrty.copy(ndim=2)
expand_sig = typing.signature(newty, arrty)
newarr = expand_dims(context, builder, expand_sig, (arr,), axis)
arrtys.append(newty)
arrs.append(newarr)
return _np_concatenate(context, builder, arrtys, arrs,
sig.return_type, axis)
return sig, codegen
@overload(np.column_stack)
def impl_column_stack(tup):
if isinstance(tup, types.BaseTuple):
def impl(tup):
return np_column_stack(tup)
return impl
def _np_stack_common(context, builder, sig, args, axis):
"""
np.stack() with the given axis value.
"""
return _np_stack(context, builder,
list(sig.args[0]),
cgutils.unpack_tuple(builder, args[0]),
sig.return_type,
axis)
@intrinsic
def np_stack_common(typingctx, arrays, axis):
# does type checking
dtype, ndim = _sequence_of_arrays(typingctx,
"np.stack", arrays)
layout = 'F' if all(a.layout == 'F' for a in arrays) else 'C'
ret = types.Array(dtype, ndim + 1, layout)
sig = ret(arrays, axis)
def codegen(context, builder, sig, args):
axis = context.cast(builder, args[1], sig.args[1], types.intp)
return _np_stack_common(context, builder, sig, args, axis)
return sig, codegen
@overload(np.stack)
def impl_np_stack(arrays, axis=0):
if isinstance(arrays, types.BaseTuple):
def impl(arrays, axis=0):
return np_stack_common(arrays, axis)
return impl
def NdStack_typer(typingctx, func_name, arrays, ndim_min):
# does type checking
dtype, ndim = _sequence_of_arrays(typingctx, func_name, arrays)
ndim = max(ndim, ndim_min)
layout = _choose_concatenation_layout(arrays)
ret = types.Array(dtype, ndim, layout)
return ret
@intrinsic
def _np_hstack(typingctx, tup):
ret = NdStack_typer(typingctx, "np.hstack", tup, 1)
sig = ret(tup)
def codegen(context, builder, sig, args):
tupty = sig.args[0]
ndim = tupty[0].ndim
if ndim == 0:
# hstack() on 0-d arrays returns a 1-d array
axis = context.get_constant(types.intp, 0)
return _np_stack_common(context, builder, sig, args, axis)
else:
# As a special case, dimension 0 of 1-dimensional arrays
# is "horizontal"
axis = 0 if ndim == 1 else 1
def np_hstack_impl(arrays):
return np.concatenate(arrays, axis=axis)
return context.compile_internal(builder, np_hstack_impl, sig, args)
return sig, codegen
@overload(np.hstack)
def impl_np_hstack(tup):
if isinstance(tup, types.BaseTuple):
def impl(tup):
return _np_hstack(tup)
return impl
@intrinsic
def _np_vstack(typingctx, tup):
ret = NdStack_typer(typingctx, "np.vstack", tup, 2)
sig = ret(tup)
def codegen(context, builder, sig, args):
tupty = sig.args[0]
ndim = tupty[0].ndim
if ndim == 0:
def np_vstack_impl(arrays):
return np.expand_dims(np.hstack(arrays), 1)
elif ndim == 1:
# np.stack(arrays, axis=0)
axis = context.get_constant(types.intp, 0)
return _np_stack_common(context, builder, sig, args, axis)
else:
def np_vstack_impl(arrays):
return np.concatenate(arrays, axis=0)
return context.compile_internal(builder, np_vstack_impl, sig, args)
return sig, codegen
@overload(np.vstack)
def impl_np_vstack(tup):
if isinstance(tup, types.BaseTuple):
def impl(tup):
return _np_vstack(tup)
return impl
if numpy_version >= (2, 0):
overload(np.row_stack)(impl_np_vstack)
@intrinsic
def _np_dstack(typingctx, tup):
ret = NdStack_typer(typingctx, "np.dstack", tup, 3)
sig = ret(tup)
def codegen(context, builder, sig, args):
tupty = sig.args[0]
retty = sig.return_type
ndim = tupty[0].ndim
if ndim == 0:
def np_vstack_impl(arrays):
return np.hstack(arrays).reshape(1, 1, -1)
return context.compile_internal(builder, np_vstack_impl, sig, args)
elif ndim == 1:
# np.expand_dims(np.stack(arrays, axis=1), axis=0)
axis = context.get_constant(types.intp, 1)
stack_retty = retty.copy(ndim=retty.ndim - 1)
stack_sig = typing.signature(stack_retty, *sig.args)
stack_ret = _np_stack_common(context, builder, stack_sig, args,
axis)
axis = context.get_constant(types.intp, 0)
expand_sig = typing.signature(retty, stack_retty)
return expand_dims(context, builder, expand_sig, (stack_ret,), axis)
elif ndim == 2:
# np.stack(arrays, axis=2)
axis = context.get_constant(types.intp, 2)
return _np_stack_common(context, builder, sig, args, axis)
else:
def np_vstack_impl(arrays):
return np.concatenate(arrays, axis=2)
return context.compile_internal(builder, np_vstack_impl, sig, args)
return sig, codegen
@overload(np.dstack)
def impl_np_dstack(tup):
if isinstance(tup, types.BaseTuple):
def impl(tup):
return _np_dstack(tup)
return impl
@extending.overload_method(types.Array, 'fill')
def arr_fill(arr, val):
def fill_impl(arr, val):
arr[:] = val
return None
return fill_impl
@extending.overload_method(types.Array, 'dot')
def array_dot(arr, other):
def dot_impl(arr, other):
return np.dot(arr, other)
return dot_impl
@overload(np.fliplr)
def np_flip_lr(m):
if not type_can_asarray(m):
raise errors.TypingError("Cannot np.fliplr on %s type" % m)
def impl(m):
A = np.asarray(m)
# this handling is superfluous/dead as < 2d array cannot be indexed as
# present below and so typing fails. If the typing doesn't fail due to
# some future change, this will catch it.
if A.ndim < 2:
raise ValueError('Input must be >= 2-d.')
return A[::, ::-1, ...]
return impl
@overload(np.flipud)
def np_flip_ud(m):
if not type_can_asarray(m):
raise errors.TypingError("Cannot np.flipud on %s type" % m)
def impl(m):
A = np.asarray(m)
# this handling is superfluous/dead as a 0d array cannot be indexed as
# present below and so typing fails. If the typing doesn't fail due to
# some future change, this will catch it.
if A.ndim < 1:
raise ValueError('Input must be >= 1-d.')
return A[::-1, ...]
return impl
@intrinsic
def _build_flip_slice_tuple(tyctx, sz):
""" Creates a tuple of slices for np.flip indexing like
`(slice(None, None, -1),) * sz` """
if not isinstance(sz, types.IntegerLiteral):
raise errors.RequireLiteralValue(sz)
size = int(sz.literal_value)
tuple_type = types.UniTuple(dtype=types.slice3_type, count=size)
sig = tuple_type(sz)
def codegen(context, builder, signature, args):
def impl(length, empty_tuple):
out = empty_tuple
for i in range(length):
out = tuple_setitem(out, i, slice(None, None, -1))
return out
inner_argtypes = [types.intp, tuple_type]
inner_sig = typing.signature(tuple_type, *inner_argtypes)
ll_idx_type = context.get_value_type(types.intp)
# Allocate an empty tuple
empty_tuple = context.get_constant_undef(tuple_type)
inner_args = [ll_idx_type(size), empty_tuple]
res = context.compile_internal(builder, impl, inner_sig, inner_args)
return res
return sig, codegen
@overload(np.flip)
def np_flip(m):
# a constant value is needed for the tuple slice, types.Array.ndim can
# provide this and so at presnet only type.Array is support
if not isinstance(m, types.Array):
raise errors.TypingError("Cannot np.flip on %s type" % m)
def impl(m):
sl = _build_flip_slice_tuple(m.ndim)
return m[sl]
return impl
@overload(np.array_split)
def np_array_split(ary, indices_or_sections, axis=0):
if isinstance(ary, (types.UniTuple, types.ListType, types.List)):
def impl(ary, indices_or_sections, axis=0):
return np.array_split(
np.asarray(ary),
indices_or_sections,
axis=axis
)
return impl
if isinstance(indices_or_sections, types.Integer):
def impl(ary, indices_or_sections, axis=0):
l, rem = divmod(ary.shape[axis], indices_or_sections)
indices = np.cumsum(np.array(
[l + 1] * rem +
[l] * (indices_or_sections - rem - 1)
))
return np.array_split(ary, indices, axis=axis)
return impl
elif (
isinstance(indices_or_sections, types.IterableType)
and isinstance(
indices_or_sections.iterator_type.yield_type,
types.Integer
)
):
def impl(ary, indices_or_sections, axis=0):
slice_tup = build_full_slice_tuple(ary.ndim)
axis = normalize_axis("np.split", "axis", ary.ndim, axis)
out = []
prev = 0
for cur in indices_or_sections:
idx = tuple_setitem(slice_tup, axis, slice(prev, cur))
out.append(ary[idx])
prev = cur
out.append(ary[tuple_setitem(slice_tup, axis, slice(cur, None))])
return out
return impl
elif (
isinstance(indices_or_sections, types.Tuple)
and all(isinstance(t, types.Integer) for t in indices_or_sections.types)
):
def impl(ary, indices_or_sections, axis=0):
slice_tup = build_full_slice_tuple(ary.ndim)
axis = normalize_axis("np.split", "axis", ary.ndim, axis)
out = []
prev = 0
for cur in literal_unroll(indices_or_sections):
idx = tuple_setitem(slice_tup, axis, slice(prev, cur))
out.append(ary[idx])
prev = cur
out.append(ary[tuple_setitem(slice_tup, axis, slice(cur, None))])
return out
return impl
@overload(np.split)
def np_split(ary, indices_or_sections, axis=0):
# This is just a wrapper of array_split, but with an extra error if
# indices is an int.
if isinstance(ary, (types.UniTuple, types.ListType, types.List)):
def impl(ary, indices_or_sections, axis=0):
return np.split(np.asarray(ary), indices_or_sections, axis=axis)
return impl
if isinstance(indices_or_sections, types.Integer):
def impl(ary, indices_or_sections, axis=0):
_, rem = divmod(ary.shape[axis], indices_or_sections)
if rem != 0:
raise ValueError(
"array split does not result in an equal division"
)
return np.array_split(
ary, indices_or_sections, axis=axis
)
return impl
else:
return np_array_split(ary, indices_or_sections, axis=axis)
@overload(np.vsplit)
def numpy_vsplit(ary, indices_or_sections):
if not isinstance(ary, types.Array):
msg = 'The argument "ary" must be an array'
raise errors.TypingError(msg)
if not isinstance(indices_or_sections, (types.Integer, types.Array,
types.List, types.UniTuple)):
msg = ('The argument "indices_or_sections" must be int or 1d-array')
raise errors.TypingError(msg)
def impl(ary, indices_or_sections):
if ary.ndim < 2:
raise ValueError(('vsplit only works on '
'arrays of 2 or more dimensions'))
return np.split(ary, indices_or_sections, axis=0)
return impl
@overload(np.hsplit)
def numpy_hsplit(ary, indices_or_sections):
if not isinstance(ary, types.Array):
msg = 'The argument "ary" must be an array'
raise errors.TypingError(msg)
if not isinstance(indices_or_sections, (types.Integer, types.Array,
types.List, types.UniTuple)):
msg = ('The argument "indices_or_sections" must be int or 1d-array')
raise errors.TypingError(msg)
def impl(ary, indices_or_sections):
if ary.ndim == 0:
raise ValueError(('hsplit only works on '
'arrays of 1 or more dimensions'))
if ary.ndim > 1:
return np.split(ary, indices_or_sections, axis=1)
return np.split(ary, indices_or_sections, axis=0)
return impl
@overload(np.dsplit)
def numpy_dsplit(ary, indices_or_sections):
if not isinstance(ary, types.Array):
msg = 'The argument "ary" must be an array'
raise errors.TypingError(msg)
if not isinstance(indices_or_sections, (types.Integer, types.Array,
types.List, types.UniTuple)):
msg = ('The argument "indices_or_sections" must be int or 1d-array')
raise errors.TypingError(msg)
def impl(ary, indices_or_sections):
if ary.ndim < 3:
raise ValueError('dsplit only works on arrays of 3 or more '
'dimensions')
return np.split(ary, indices_or_sections, axis=2)
return impl
# -----------------------------------------------------------------------------
# Sorting
_sorts = {}
def default_lt(a, b):
"""
Trivial comparison function between two keys.
"""
return a < b
def get_sort_func(kind, lt_impl, is_argsort=False):
"""
Get a sort implementation of the given kind.
"""
key = kind, lt_impl.__name__, is_argsort
try:
return _sorts[key]
except KeyError:
if kind == 'quicksort':
sort = quicksort.make_jit_quicksort(
lt=lt_impl,
is_argsort=is_argsort,
is_np_array=True)
func = sort.run_quicksort
elif kind == 'mergesort':
sort = mergesort.make_jit_mergesort(
lt=lt_impl,
is_argsort=is_argsort)
func = sort.run_mergesort
_sorts[key] = func
return func
def lt_implementation(dtype):
if isinstance(dtype, types.Float):
return lt_floats
elif isinstance(dtype, types.Complex):
return lt_complex
else:
return default_lt
@lower_builtin("array.sort", types.Array)
def array_sort(context, builder, sig, args):
arytype = sig.args[0]
sort_func = get_sort_func(kind='quicksort',
lt_impl=lt_implementation(arytype.dtype))
def array_sort_impl(arr):
# Note we clobber the return value
sort_func(arr)
return context.compile_internal(builder, array_sort_impl, sig, args)
@overload(np.sort)
def impl_np_sort(a):
if not type_can_asarray(a):
raise errors.TypingError('Argument "a" must '
'be array-like')
def np_sort_impl(a):
res = a.copy()
res.sort()
return res
return np_sort_impl
@lower_builtin("array.argsort", types.Array, types.StringLiteral)
@lower_builtin(np.argsort, types.Array, types.StringLiteral)
def array_argsort(context, builder, sig, args):
arytype, kind = sig.args
sort_func = get_sort_func(kind=kind.literal_value,
lt_impl=lt_implementation(arytype.dtype),
is_argsort=True)
def array_argsort_impl(arr):
return sort_func(arr)
innersig = sig.replace(args=sig.args[:1])
innerargs = args[:1]
return context.compile_internal(builder, array_argsort_impl,
innersig, innerargs)
# ------------------------------------------------------------------------------
# Implicit cast
@lower_cast(types.Array, types.Array)
def array_to_array(context, builder, fromty, toty, val):
# Type inference should have prevented illegal array casting.
assert fromty.mutable != toty.mutable or toty.layout == 'A'
return val
@lower_cast(types.Array, types.UnicodeCharSeq)
@lower_cast(types.Array, types.Float)
@lower_cast(types.Array, types.Integer)
@lower_cast(types.Array, types.Complex)
@lower_cast(types.Array, types.Boolean)
@lower_cast(types.Array, types.NPTimedelta)
@lower_cast(types.Array, types.NPDatetime)
def array0d_to_scalar(context, builder, fromty, toty, val):
def impl(a):
# a is an array(T, 0d, O), T is type, O is order
return a.take(0)
sig = signature(toty, fromty)
res = context.compile_internal(builder, impl, sig, [val])
return impl_ret_untracked(context, builder, sig.return_type, res)
@lower_cast(types.Array, types.UnicodeCharSeq)
def array_to_unichrseq(context, builder, fromty, toty, val):
def impl(a):
return str(a[()])
sig = signature(toty, fromty)
res = context.compile_internal(builder, impl, sig, [val])
return impl_ret_borrowed(context, builder, sig.return_type, res)
# ------------------------------------------------------------------------------
# Stride tricks
def reshape_unchecked(a, shape, strides):
"""
An intrinsic returning a derived array with the given shape and strides.
"""
raise NotImplementedError
@extending.type_callable(reshape_unchecked)
def type_reshape_unchecked(context):
def check_shape(shape):
return (isinstance(shape, types.BaseTuple) and
all(isinstance(v, types.Integer) for v in shape))
def typer(a, shape, strides):
if not isinstance(a, types.Array):
return
if not check_shape(shape) or not check_shape(strides):
return
if len(shape) != len(strides):
return
return a.copy(ndim=len(shape), layout='A')
return typer
@lower_builtin(reshape_unchecked, types.Array, types.BaseTuple, types.BaseTuple)
def impl_shape_unchecked(context, builder, sig, args):
aryty = sig.args[0]
retty = sig.return_type
ary = make_array(aryty)(context, builder, args[0])
out = make_array(retty)(context, builder)
shape = cgutils.unpack_tuple(builder, args[1])
strides = cgutils.unpack_tuple(builder, args[2])
populate_array(out,
data=ary.data,
shape=shape,
strides=strides,
itemsize=ary.itemsize,
meminfo=ary.meminfo,
)
res = out._getvalue()
return impl_ret_borrowed(context, builder, retty, res)
@extending.overload(np.lib.stride_tricks.as_strided)
def as_strided(x, shape=None, strides=None):
if shape in (None, types.none):
@register_jitable
def get_shape(x, shape):
return x.shape
else:
@register_jitable
def get_shape(x, shape):
return shape
if strides in (None, types.none):
# When *strides* is not passed, as_strided() does a non-size-checking
# reshape(), possibly changing the original strides. This is too
# cumbersome to support right now, and a Web search shows all example
# use cases of as_strided() pass explicit *strides*.
raise NotImplementedError("as_strided() strides argument is mandatory")
else:
@register_jitable
def get_strides(x, strides):
return strides
def as_strided_impl(x, shape=None, strides=None):
x = reshape_unchecked(x, get_shape(x, shape), get_strides(x, strides))
return x
return as_strided_impl
@extending.overload(np.lib.stride_tricks.sliding_window_view)
def sliding_window_view(x, window_shape, axis=None):
# Window shape must be given as either an integer or tuple of integers.
# We also need to generate buffer tuples we can modify to contain the
# final shape and strides (reshape_unchecked does not accept lists).
if isinstance(window_shape, types.Integer):
shape_buffer = tuple(range(x.ndim + 1))
stride_buffer = tuple(range(x.ndim + 1))
@register_jitable
def get_window_shape(window_shape):
return (window_shape,)
elif (isinstance(window_shape, types.UniTuple) and
isinstance(window_shape.dtype, types.Integer)):
shape_buffer = tuple(range(x.ndim + len(window_shape)))
stride_buffer = tuple(range(x.ndim + len(window_shape)))
@register_jitable
def get_window_shape(window_shape):
return window_shape
else:
raise errors.TypingError(
"window_shape must be an integer or tuple of integers"
)
# Axis must be integer, tuple of integers, or None for all axes.
if is_nonelike(axis):
@register_jitable
def get_axis(window_shape, axis, ndim):
return list(range(ndim))
elif isinstance(axis, types.Integer):
@register_jitable
def get_axis(window_shape, axis, ndim):
return [
normalize_axis("sliding_window_view", "axis", ndim, axis)
]
elif (isinstance(axis, types.UniTuple) and
isinstance(axis.dtype, types.Integer)):
@register_jitable
def get_axis(window_shape, axis, ndim):
return [normalize_axis("sliding_window_view", "axis", ndim, a)
for a in axis]
else:
raise errors.TypingError(
"axis must be None, an integer or tuple of integers"
)
def sliding_window_view_impl(x, window_shape, axis=None):
window_shape = get_window_shape(window_shape)
axis = get_axis(window_shape, axis, x.ndim)
if len(window_shape) != len(axis):
raise ValueError(
"Must provide matching length window_shape and axis"
)
# Initialise view details with shape and strides of x.
out_shape = shape_buffer
out_strides = stride_buffer
for i in range(x.ndim):
out_shape = tuple_setitem(out_shape, i, x.shape[i])
out_strides = tuple_setitem(out_strides, i, x.strides[i])
# Trim the dimensions being windowed and set the window shape and
# strides. Note: the same axis can be windowed repeatedly.
i = x.ndim
for ax, dim in zip(axis, window_shape):
if dim < 0:
raise ValueError(
"`window_shape` cannot contain negative values"
)
if out_shape[ax] < dim:
raise ValueError(
"window_shape cannot be larger than input array shape"
)
trimmed = out_shape[ax] - dim + 1
out_shape = tuple_setitem(out_shape, ax, trimmed)
out_shape = tuple_setitem(out_shape, i, dim)
out_strides = tuple_setitem(out_strides, i, x.strides[ax])
i += 1
# The NumPy version calls as_strided, but our implementation of
# as_strided is effectively a wrapper for reshape_unchecked.
view = reshape_unchecked(x, out_shape, out_strides)
return view
return sliding_window_view_impl
@overload(bool)
def ol_bool(arr):
if isinstance(arr, types.Array):
def impl(arr):
if arr.size == 0:
return False # this is deprecated
elif arr.size == 1:
return bool(arr.take(0))
else:
msg = ("The truth value of an array with more than one element "
"is ambiguous. Use a.any() or a.all()")
raise ValueError(msg)
return impl
@overload(np.swapaxes)
def numpy_swapaxes(a, axis1, axis2):
if not isinstance(axis1, (int, types.Integer)):
raise errors.TypingError('The second argument "axis1" must be an '
'integer')
if not isinstance(axis2, (int, types.Integer)):
raise errors.TypingError('The third argument "axis2" must be an '
'integer')
if not isinstance(a, types.Array):
raise errors.TypingError('The first argument "a" must be an array')
# create tuple list for transpose
ndim = a.ndim
axes_list = tuple(range(ndim))
def impl(a, axis1, axis2):
axis1 = normalize_axis("np.swapaxes", "axis1", ndim, axis1)
axis2 = normalize_axis("np.swapaxes", "axis2", ndim, axis2)
# to ensure tuple_setitem support of negative values
if axis1 < 0:
axis1 += ndim
if axis2 < 0:
axis2 += ndim
axes_tuple = tuple_setitem(axes_list, axis1, axis2)
axes_tuple = tuple_setitem(axes_tuple, axis2, axis1)
return np.transpose(a, axes_tuple)
return impl
@register_jitable
def _take_along_axis_impl(
arr, indices, axis, Ni_orig, Nk_orig, indices_broadcast_shape
):
# Based on example code in
# https://github.com/numpy/numpy/blob/623bc1fae1d47df24e7f1e29321d0c0ba2771ce0/numpy/lib/shape_base.py#L90-L103
# With addition of pre-broadcasting:
# https://github.com/numpy/numpy/issues/19704
# Wrap axis, it's used in tuple_setitem so must be (axis >= 0) to ensure
# the GEP is in bounds.
axis = normalize_axis("np.take_along_axis", "axis", arr.ndim, axis)
# Broadcast the two arrays to matching shapes:
arr_shape = list(arr.shape)
arr_shape[axis] = 1
for i, (d1, d2) in enumerate(zip(arr_shape, indices.shape)):
if d1 == 1:
new_val = d2
elif d2 == 1:
new_val = d1
else:
if d1 != d2:
raise ValueError(
"`arr` and `indices` dimensions don't match"
)
new_val = d1
indices_broadcast_shape = tuple_setitem(
indices_broadcast_shape, i, new_val
)
arr_broadcast_shape = tuple_setitem(
indices_broadcast_shape, axis, arr.shape[axis]
)
arr = np.broadcast_to(arr, arr_broadcast_shape)
indices = np.broadcast_to(indices, indices_broadcast_shape)
Ni = Ni_orig
if len(Ni_orig) > 0:
for i in range(len(Ni)):
Ni = tuple_setitem(Ni, i, arr.shape[i])
Nk = Nk_orig
if len(Nk_orig) > 0:
for i in range(len(Nk)):
Nk = tuple_setitem(Nk, i, arr.shape[axis + 1 + i])
J = indices.shape[axis] # Need not equal M
out = np.empty(Ni + (J,) + Nk, arr.dtype)
np_s_ = (slice(None, None, None),)
for ii in np.ndindex(Ni):
for kk in np.ndindex(Nk):
a_1d = arr[ii + np_s_ + kk]
indices_1d = indices[ii + np_s_ + kk]
out_1d = out[ii + np_s_ + kk]
for j in range(J):
out_1d[j] = a_1d[indices_1d[j]]
return out
@overload(np.take_along_axis)
def arr_take_along_axis(arr, indices, axis):
if not isinstance(arr, types.Array):
raise errors.TypingError('The first argument "arr" must be an array')
if not isinstance(indices, types.Array):
raise errors.TypingError(
'The second argument "indices" must be an array')
if not isinstance(indices.dtype, types.Integer):
raise errors.TypingError('The indices array must contain integers')
if is_nonelike(axis):
arr_ndim = 1
else:
arr_ndim = arr.ndim
if arr_ndim != indices.ndim:
# Matches NumPy error:
raise errors.TypingError(
"`indices` and `arr` must have the same number of dimensions"
)
indices_broadcast_shape = tuple(range(indices.ndim))
if is_nonelike(axis):
def take_along_axis_impl(arr, indices, axis):
return _take_along_axis_impl(arr.flatten(), indices, 0, (), (),
indices_broadcast_shape)
else:
check_is_integer(axis, "axis")
if not isinstance(axis, types.IntegerLiteral):
raise errors.NumbaValueError("axis must be a literal value")
axis = axis.literal_value
if axis < 0:
axis = arr.ndim + axis
if axis < 0 or axis >= arr.ndim:
raise errors.NumbaValueError("axis is out of bounds")
Ni = tuple(range(axis))
Nk = tuple(range(axis + 1, arr.ndim))
def take_along_axis_impl(arr, indices, axis):
return _take_along_axis_impl(arr, indices, axis, Ni, Nk,
indices_broadcast_shape)
return take_along_axis_impl
@overload(np.nan_to_num)
def nan_to_num_impl(x, copy=True, nan=0.0):
if isinstance(x, types.Number):
if isinstance(x, types.Integer):
# Integers do not have nans or infs
def impl(x, copy=True, nan=0.0):
return x
elif isinstance(x, types.Float):
def impl(x, copy=True, nan=0.0):
if np.isnan(x):
return nan
elif np.isneginf(x):
return np.finfo(type(x)).min
elif np.isposinf(x):
return np.finfo(type(x)).max
return x
elif isinstance(x, types.Complex):
def impl(x, copy=True, nan=0.0):
r = np.nan_to_num(x.real, nan=nan)
c = np.nan_to_num(x.imag, nan=nan)
return complex(r, c)
else:
raise errors.TypingError(
"Only Integer, Float, and Complex values are accepted"
)
elif type_can_asarray(x):
if isinstance(x.dtype, types.Integer):
# Integers do not have nans or infs
def impl(x, copy=True, nan=0.0):
return x
elif isinstance(x.dtype, types.Float):
def impl(x, copy=True, nan=0.0):
min_inf = np.finfo(x.dtype).min
max_inf = np.finfo(x.dtype).max
x_ = np.asarray(x)
output = np.copy(x_) if copy else x_
output_flat = output.flat
for i in range(output.size):
if np.isnan(output_flat[i]):
output_flat[i] = nan
elif np.isneginf(output_flat[i]):
output_flat[i] = min_inf
elif np.isposinf(output_flat[i]):
output_flat[i] = max_inf
return output
elif isinstance(x.dtype, types.Complex):
def impl(x, copy=True, nan=0.0):
x_ = np.asarray(x)
output = np.copy(x_) if copy else x_
np.nan_to_num(output.real, copy=False, nan=nan)
np.nan_to_num(output.imag, copy=False, nan=nan)
return output
else:
raise errors.TypingError(
"Only Integer, Float, and Complex values are accepted"
)
else:
raise errors.TypingError("The first argument must be a scalar or an "
"array-like")
return impl
|