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
|
# Copyright (C) 2005-2011 Francois Meyer (dulle at free.fr)
# Copyright (C) 2012-2026 team free-astro (see more in AUTHORS file)
# Reference site is https://siril.org
# SPDX-License-Identifier: GPL-3.0-or-later
"""
GPU helper module for Siril Python interface providing helper functions for detection,
installation and testing of GPU-related modules.
Initial scope is ONNX, torch and jax
"""
import re
import os
import sys
import json
import time
import shutil
import platform
import tempfile
import importlib
import subprocess
from typing import Tuple, Optional, Dict, Any
from packaging import version as pkg_version
import requests
import numpy as np
from .version import __version__
from .utility import ensure_installed, _check_package_installed, _install_package, \
SuppressedStderr
def _detect_cuda_version(system) -> Optional[str]:
"""
Detects the CUDA version by parsing the output of 'nvcc -v'.
Returns:
Optional[str]: The CUDA version as a string (e.g., '11.7') if detected,
or "0.0" if nvcc is not installed or version cannot be determined.
"""
nvcc_command = 'nvcc.exe' if system == 'windows' else 'nvcc'
# Check if nvcc exists in PATH
if shutil.which(nvcc_command) is None:
print(f"{nvcc_command} not found in PATH, trying torch fallback")
return _try_torch_cuda_version()
try:
# Run nvcc -V and capture the output
result = subprocess.run([nvcc_command, '-V'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=False)
output = result.stderr if result.stderr else result.stdout
version_match = re.search(r'release (\d+\.\d+)', output)
if version_match:
return version_match.group(1)
alt_match = re.search(r'V(\d+\.\d+\.\d+)', output)
if alt_match:
# Return just the major.minor part (e.g., 11.7 from 11.7.0)
version_parts = alt_match.group(1).split('.')
return f"{version_parts[0]}.{version_parts[1]}"
print("Unable to confirm CUDA availability from nvcc output")
return "0.0"
except (subprocess.SubprocessError, FileNotFoundError):
# nvcc command failed
return _try_torch_cuda_version()
def _try_torch_cuda_version():
"""Helper method to get CUDA version from torch"""
try:
# possibly we have torch installed (this is a bit slow to import
# which is why we don't try it first...)
torch_helper = TorchHelper()
if torch_helper.is_torch_installed():
import torch
cuda_version = torch.version.cuda # e.g., '12.1'
return cuda_version
print("Torch unavailable, unable to confirm CUDA availability")
return "0.0"
except Exception:
print("Unable to confirm CUDA availability")
return "0.0"
def _get_windows_gpu_info(vendor_filter: str) -> Optional[str]:
"""
Helper function to get GPU name from Windows using PowerShell.
Args:
vendor_filter: Vendor name to filter for (e.g., 'NVIDIA', 'AMD', 'Intel')
Returns:
GPU name string if found, None otherwise
"""
try:
# More reliable PowerShell command that returns just the Name property
cmd = [
"powershell", "-Command",
f"(Get-WmiObject -Class Win32_VideoController | Where-Object {{$_.Name -like '*{vendor_filter}*'}}).Name"
]
output = subprocess.check_output(cmd, text=True, stderr=subprocess.DEVNULL)
if output.strip():
# Split by newlines and filter out empty lines
gpu_names = [line.strip() for line in output.strip().split('\n') if line.strip()]
if gpu_names:
# Return the first GPU found
return gpu_names[0]
except (subprocess.SubprocessError, FileNotFoundError, Exception):
pass
return None
def _detect_nvidia_gpu(system):
"""
Detect if NVIDIA GPU is available. (Required to check Windows and Linux, not required on MacOS.)
Returns:
bool: True if NVIDIA GPU is detected, False otherwise.
"""
try:
if system == "windows":
# Use the helper function for consistent detection
gpu_name = _get_windows_gpu_info('NVIDIA')
return gpu_name is not None
# Linux detection using nvidia-smi or lspci
if system == "linux":
try:
output = subprocess.check_output(
["nvidia-smi"],
stderr=subprocess.DEVNULL,
text=True
)
return True
except (subprocess.SubprocessError, FileNotFoundError):
pass
try:
output = subprocess.check_output(["lspci"], text=True)
return "NVIDIA" in output
except (subprocess.SubprocessError, FileNotFoundError):
return False
except Exception:
pass
return False
def _detect_amd_gpu():
"""
Detect if AMD GPU is available on Windows or Linux.
This is a legacy function - prefer using _get_amd_gpu_info() for more detailed info.
Returns:
bool: True if AMD GPU is detected, False otherwise.
"""
system = platform.system().lower()
# Windows detection
if system == 'windows':
for vendor in ['AMD', 'Radeon', 'ATI']:
if _get_windows_gpu_info(vendor):
return True
return False
# Linux detection
try:
# Try rocm-smi first
try:
output = subprocess.check_output(["rocm-smi"], stderr=subprocess.DEVNULL, text=True)
return True
except (subprocess.SubprocessError, FileNotFoundError):
pass
# Fallback to lspci
try:
output = subprocess.check_output(["lspci"], text=True)
return any(gpu in output for gpu in ["AMD", "ATI", "Radeon"])
except (subprocess.SubprocessError, FileNotFoundError):
return False
except Exception:
pass
return False
def _detect_intel_gpu_for_openvino():
"""
Detect if an Intel GPU compatible with OpenVINO is available.
Returns:
bool: True if compatible Intel GPU is detected
"""
# First try using OpenVINO's native detection if available
try:
import openvino as ov
core = ov.Core()
available_devices = core.available_devices
return any(device.startswith("GPU") for device in available_devices)
except ImportError:
print("OpenVINO not installed, falling back to hardware detection")
except Exception as e:
print(f"Error using OpenVINO device detection: {e}")
# Fall back to lspci detection if OpenVINO is not available
try:
output = subprocess.check_output(["lspci"], text=True).lower()
# Intel integrated and discrete GPU keywords
intel_keywords = ["intel", "graphics", "iris", "uhd", "hd graphics", "arc",
"a3", "a5", "a7", "a370m", "a730m", "a770", "a750",
"a580", "a380", "battlemage"]
# Check for any Intel GPU
return "intel" in output and any(keyword in output for keyword in intel_keywords)
except (subprocess.SubprocessError, FileNotFoundError):
return False
except Exception as e:
print(f"Error detecting Intel GPU: {e}")
return False
def _get_nvidia_gpu_info() -> Optional[Dict[str, Any]]:
"""
Detect NVIDIA GPU and determine the appropriate CUDA version for PyTorch.
Returns:
Dict with 'detected', 'gpu_name', 'compute_capability', 'recommended_cuda' keys,
or None if no NVIDIA GPU detected.
"""
system = platform.system().lower()
try:
# Try nvidia-smi first (works on both Windows and Linux)
result = subprocess.run(
['nvidia-smi', '--query-gpu=name,compute_cap', '--format=csv,noheader'],
capture_output=True,
text=True,
check=False
)
if result.returncode == 0 and result.stdout.strip():
lines = result.stdout.strip().split('\n')
parts = lines[0].split(',')
gpu_name = parts[0].strip()
compute_cap = parts[1].strip() if len(parts) > 1 else None
# Determine recommended CUDA version based on GPU generation
recommended_cuda = _determine_cuda_version_for_gpu(gpu_name, compute_cap)
return {
'detected': True,
'gpu_name': gpu_name,
'compute_capability': compute_cap,
'recommended_cuda': recommended_cuda
}
except (subprocess.SubprocessError, FileNotFoundError):
pass
# Windows fallback using PowerShell
if system == 'windows':
gpu_name = _get_windows_gpu_info('NVIDIA')
if gpu_name:
recommended_cuda = _determine_cuda_version_for_gpu(gpu_name, None)
return {
'detected': True,
'gpu_name': gpu_name,
'compute_capability': None,
'recommended_cuda': recommended_cuda
}
# Linux fallback to basic detection
if system == 'linux' and _detect_nvidia_gpu(system):
return {
'detected': True,
'gpu_name': 'Unknown NVIDIA GPU',
'compute_capability': None,
'recommended_cuda': 'cu126' # Safe default for most modern cards
}
return None
def _determine_cuda_version_for_gpu(gpu_name: str, compute_cap: Optional[str] = None) -> str:
"""
Determine the safest CUDA version for PyTorch based on GPU generation.
Args:
gpu_name: Name of the GPU (e.g., "GeForce RTX 3090")
compute_cap: Compute capability (e.g., "8.6"), if available
Returns:
CUDA version string for PyTorch (e.g., "cu118", "cu126", "cu128")
"""
# Use compute capability if available
if compute_cap:
try:
major, minor = map(int, compute_cap.split('.'))
cc_val = major * 10 + minor
# SM 9.0+ (Blackwell and future) - CUDA 12.8+
if cc_val >= 90:
return 'cu128'
# SM 7.0-8.9 (Volta, Turing, Ampere, Ada) - CUDA 12.6
elif cc_val >= 70:
return 'cu126'
# SM 5.0-6.x (Maxwell, Pascal) - CUDA 11.8
elif cc_val >= 50:
return 'cu118'
except (ValueError, AttributeError):
pass
# Use GPU name
gpu_lower = gpu_name.lower()
# RTX 50xx series (Blackwell) - requires CUDA 12.8+
if any(x in gpu_lower for x in ['rtx 50', 'rtx50']):
return 'cu128'
# RTX 40xx series (Ada Lovelace) - CUDA 12.6 recommended
# RTX 30xx series (Ampere) - CUDA 12.6 supported
# RTX 20xx series (Turing) - CUDA 12.6 supported
# GTX 16xx series (Turing) - CUDA 12.6 supported
# GTX 10xx series (Pascal) - CUDA 12.6 supported
if any(x in gpu_lower for x in ['rtx 40', 'rtx40', 'rtx 30', 'rtx30',
'rtx 20', 'rtx20', 'gtx 16', 'gtx16',
'gtx 10', 'gtx10', 'titan x', 'tesla p']):
return 'cu126'
# GTX 9xx series (Maxwell) - CUDA 11.8 is safest
# GTX 7xx/8xx series (Kepler) - CUDA 11.8
if any(x in gpu_lower for x in ['gtx 9', 'gtx 8', 'gtx 7',
'titan', 'tesla k', 'quadro k']):
return 'cu118'
# Default to cu126 for unknown modern GPUs
return 'cu126'
def _get_amd_gpu_info() -> Optional[Dict[str, Any]]:
"""
Detect AMD GPU and determine if it supports ROCm.
Returns:
Dict with 'detected', 'gpu_name', 'rocm_compatible', 'is_igpu' keys,
or None if no AMD GPU detected.
Note: rocm_compatible indicates hardware ROCm support regardless of OS.
Individual frameworks decide whether to use ROCm based on their own
platform support (e.g., ONNX doesn't support ROCm on Windows, but PyTorch does).
"""
system = platform.system().lower()
# Windows detection
if system == 'windows':
# Try multiple AMD/ATI vendor identifiers
gpu_name = None
for vendor in ['AMD', 'Radeon', 'ATI']:
gpu_name = _get_windows_gpu_info(vendor)
if gpu_name:
break
if gpu_name:
is_igpu = _is_amd_igpu(gpu_name)
# Check if GPU is ROCm-compatible (discrete GPUs with RDNA2/3 or newer)
rocm_compat = _is_rocm_compatible_gpu(gpu_name) and not is_igpu
return {
'detected': True,
'gpu_name': gpu_name,
'rocm_compatible': rocm_compat,
'is_igpu': is_igpu
}
return None
# Linux - check for ROCm compatibility
if system == 'linux':
try:
# Try rocm-smi first
result = subprocess.run(
['rocm-smi', '--showproductname'],
capture_output=True,
text=True,
check=False
)
if result.returncode == 0 and result.stdout.strip():
gpu_name = result.stdout.strip()
is_igpu = _is_amd_igpu(gpu_name)
rocm_compat = not is_igpu # iGPUs generally don't support ROCm well
return {
'detected': True,
'gpu_name': gpu_name,
'rocm_compatible': rocm_compat,
'is_igpu': is_igpu
}
except (subprocess.SubprocessError, FileNotFoundError):
pass
# Fallback to lspci
try:
output = subprocess.check_output(['lspci'], text=True)
amd_lines = [line for line in output.split('\n')
if any(x in line.lower() for x in ['amd', 'ati', 'radeon'])]
if amd_lines:
gpu_name = amd_lines[0].split(':')[-1].strip()
is_igpu = _is_amd_igpu(gpu_name)
# Check if this is a ROCm-compatible discrete GPU
rocm_compat = _is_rocm_compatible_gpu(gpu_name) and not is_igpu
return {
'detected': True,
'gpu_name': gpu_name,
'rocm_compatible': rocm_compat,
'is_igpu': is_igpu
}
except (subprocess.SubprocessError, FileNotFoundError):
pass
return None
def _is_amd_igpu(gpu_name: str) -> bool:
"""Check if AMD GPU is an integrated GPU."""
igpu_indicators = [
'vega', 'radeon graphics', 'ryzen', 'renoir', 'cezanne',
'barcelo', 'rembrandt', 'phoenix', 'raphael', 'dragon range',
'strix point', 'mendocino', 'picasso', 'raven'
]
gpu_lower = gpu_name.lower()
return any(indicator in gpu_lower for indicator in igpu_indicators)
def _is_rocm_compatible_gpu(gpu_name: str) -> bool:
"""
Check if AMD GPU is compatible with ROCm.
ROCm supports RDNA2/3 and some older GCN architectures.
"""
gpu_lower = gpu_name.lower()
# RDNA architectures (RX 5000, 6000, 7000 series)
rocm_compatible = [
'rx 7', 'rx 6', 'rx 5', # Consumer RDNA
'radeon pro w', 'radeon pro v', # Professional RDNA
'instinct', # Data center
'vii', # Radeon VII
]
return any(indicator in gpu_lower for indicator in rocm_compatible)
def _get_intel_gpu_info() -> Optional[Dict[str, Any]]:
"""
Detect Intel GPU and determine if it's Arc (discrete) or iGPU.
Returns:
Dict with 'detected', 'gpu_name', 'is_arc', 'is_igpu', 'torch_compatible' keys,
or None if no Intel GPU detected.
"""
system = platform.system().lower()
gpu_name = None
# Windows detection
if system == 'windows':
gpu_name = _get_windows_gpu_info('Intel')
# Linux detection
elif system == 'linux':
try:
output = subprocess.check_output(['lspci'], text=True)
intel_lines = [line for line in output.split('\n')
if 'intel' in line.lower() and
any(x in line.lower() for x in ['vga', 'display', '3d', 'graphics'])]
if intel_lines:
gpu_name = intel_lines[0].split(':')[-1].strip()
except (subprocess.SubprocessError, FileNotFoundError):
pass
# macOS detection
elif system == 'darwin':
try:
output = subprocess.check_output(
['system_profiler', 'SPDisplaysDataType'],
text=True
)
if 'intel' in output.lower():
for line in output.split('\n'):
if 'Chipset Model' in line or 'Graphics' in line:
if 'Intel' in line:
gpu_name = line.split(':')[-1].strip()
break
except (subprocess.SubprocessError, FileNotFoundError):
pass
if not gpu_name:
return None
gpu_lower = gpu_name.lower()
is_arc = any(x in gpu_lower for x in [
# Discrete Arc GPUs
'arc', 'a770', 'a750', 'a580', 'a380',
'a370m', 'a730m', 'a550m', 'a350m',
# Xe-based iGPUs (should be treated as Arc)
'iris xe',
'xe graphics',
'arc graphics', # Meteor Lake / Core Ultra iGPUs
])
# --- Legacy (non-Xe) Intel iGPUs ---
is_igpu = (
any(x in gpu_lower for x in [
'uhd graphics',
'hd graphics',
'uhd 6', 'uhd 7', # UHD 620 / 630 / etc.
])
and not is_arc
)
# Intel Arc GPUs support PyTorch via Intel Extension for PyTorch
# iGPUs generally have limited/no PyTorch support
torch_compatible = is_arc
return {
'detected': True,
'gpu_name': gpu_name,
'is_arc': is_arc,
'is_igpu': is_igpu,
'torch_compatible': torch_compatible
}
def _get_apple_silicon_info() -> Optional[Dict[str, Any]]:
"""
Detect Apple Silicon (M1/M2/M3/M4) chips.
Returns:
Dict with 'detected', 'chip_name', 'architecture' keys,
or None if not Apple Silicon.
"""
system = platform.system().lower()
if system != 'darwin':
return None
try:
# Check if it's Apple Silicon
machine = platform.machine().lower()
if machine != 'arm64':
return None
# Get chip information
output = subprocess.check_output(
['sysctl', '-n', 'machdep.cpu.brand_string'],
text=True
).strip()
chip_name = output
# Determine architecture
if 'M1' in output:
architecture = 'M1'
elif 'M2' in output:
architecture = 'M2'
elif 'M3' in output:
architecture = 'M3'
elif 'M4' in output:
architecture = 'M4'
else:
architecture = 'Apple Silicon'
return {
'detected': True,
'chip_name': chip_name,
'architecture': architecture
}
except (subprocess.SubprocessError, FileNotFoundError):
pass
return None
def detect_gpu_capabilities() -> Dict[str, Any]:
"""
Comprehensive GPU detection for all supported hardware.
Returns:
Dict containing detected hardware information and recommendations for
PyTorch, ONNX, and JAX backends.
"""
system = platform.system().lower()
capabilities = {
'system': system,
'nvidia': _get_nvidia_gpu_info(),
'amd': _get_amd_gpu_info(),
'intel': _get_intel_gpu_info(),
'apple_silicon': _get_apple_silicon_info()
}
return capabilities
def _get_gpu_priority() -> Dict[str, Any]:
"""
Determine GPU priority when multiple GPUs are available.
Priority order (highest to lowest):
1. NVIDIA discrete GPU (best PyTorch/ONNX/JAX support)
2. AMD ROCm-compatible discrete GPU
3. Intel Arc discrete GPU
4. Apple Silicon (macOS only)
5. AMD non-ROCm GPU
6. Intel iGPU (limited support)
7. AMD iGPU (very limited support)
Returns:
Dict with 'primary_gpu' (type), 'reason' (why chosen), and full capabilities
"""
caps = detect_gpu_capabilities()
# Priority 1: NVIDIA discrete GPU
if caps['nvidia'] and caps['nvidia']['detected']:
return {
'primary_gpu': 'nvidia',
'reason': 'NVIDIA GPU has best support across PyTorch, ONNX, and JAX',
'capabilities': caps
}
# Priority 2: AMD ROCm-compatible GPU
if (caps['amd'] and caps['amd']['detected'] and
caps['amd'].get('rocm_compatible') and
not caps['amd'].get('is_igpu')):
return {
'primary_gpu': 'amd_rocm',
'reason': 'AMD ROCm-compatible discrete GPU detected',
'capabilities': caps
}
# Priority 3: Intel Arc discrete GPU
if (caps['intel'] and caps['intel']['detected'] and
caps['intel'].get('is_arc')):
return {
'primary_gpu': 'intel_arc',
'reason': 'Intel Arc discrete GPU detected',
'capabilities': caps
}
# Priority 4: Apple Silicon
if caps['apple_silicon']:
return {
'primary_gpu': 'apple_silicon',
'reason': 'Apple Silicon with Metal/MPS support',
'capabilities': caps
}
# Priority 5: AMD GPU without ROCm support
if (caps['amd'] and caps['amd']['detected'] and
not caps['amd'].get('is_igpu')):
return {
'primary_gpu': 'amd_other',
'reason': 'AMD GPU detected (limited support)',
'capabilities': caps
}
# Priority 6: Intel iGPU (OpenVINO only for ONNX)
if (caps['intel'] and caps['intel']['detected'] and
caps['intel'].get('is_igpu')):
return {
'primary_gpu': 'intel_igpu',
'reason': 'Intel iGPU - limited ONNX OpenVINO support only',
'capabilities': caps
}
# Priority 7: AMD iGPU (very limited support)
if (caps['amd'] and caps['amd']['detected'] and
caps['amd'].get('is_igpu')):
return {
'primary_gpu': 'amd_igpu',
'reason': 'AMD iGPU - very limited support',
'capabilities': caps
}
# No GPU detected
return {
'primary_gpu': 'cpu',
'reason': 'No supported GPU detected',
'capabilities': caps
}
def get_gpu_detection_report() -> Dict[str, Any]:
"""
Generate a comprehensive GPU detection report suitable for display in a GUI.
Returns:
Dict containing all detected hardware and recommended configurations
for ONNX, PyTorch, and JAX, including GPU priority information.
"""
priority = _get_gpu_priority()
caps = priority['capabilities']
# Note: This assumes ONNXHelper, TorchHelper, and JaxHelper classes exist
# If not available, you'll need to import them or handle gracefully
try:
from . import ONNXHelper, TorchHelper, JaxHelper
onnx_helper = ONNXHelper()
onnx_backend = onnx_helper.get_recommended_backend()
onnx_package, from_url, index_url = onnx_helper._get_onnxruntime_package()
torch_backend = TorchHelper().get_recommended_backend()
jax_backend = JaxHelper().get_recommended_backend()
except (ImportError, AttributeError):
# Fallback if helper classes not available
onnx_backend = 'cpu'
onnx_package = 'onnxruntime'
from_url = None
index_url = None
torch_backend = 'cpu'
jax_backend = 'cpu'
report = {
'system': caps['system'],
'gpu_priority': {
'primary_gpu': priority['primary_gpu'],
'reason': priority['reason']
},
'hardware': {
'nvidia': caps['nvidia'],
'amd': caps['amd'],
'intel': caps['intel'],
'apple_silicon': caps['apple_silicon']
},
'recommendations': {
'onnx': {
'backend': onnx_backend,
'package': onnx_package,
'from_url': from_url,
'index_url': index_url
},
'torch': torch_backend,
'jax': jax_backend
}
}
return report
class ONNXHelper:
"""
A class to handle detection and installation of the appropriate ONNX Runtime
package based on the system hardware and configuration.
Example usage (this should be used instead of
``sirilpy.ensure_installed("onnxruntime")`` to install the correct package for
the user's system.)
.. code-block:: python
oh = sirilpy.ONNXHelper()
oh.ensure_onnxruntime()
"""
def __init__(self):
"""Initialize the ONNXHelper."""
ensure_installed("platformdirs")
ensure_installed("onnx")
from platformdirs import user_config_dir
self.system = platform.system().lower()
self.providers = None
self.config_file = os.path.join(user_config_dir(appname="siril"), "siril_onnx.conf")
def get_recommended_backend(self) -> str:
"""
Determine the recommended ONNX Runtime backend based on hardware.
When multiple GPUs are present, uses priority system to choose the best one.
Note: ONNX Runtime has platform-specific backend support:
- Windows: DirectML for all GPUs (NVIDIA, AMD, Intel) - more reliable, no driver dependencies
- Linux: CUDA for NVIDIA, CPU for AMD (no ROCm support yet), OpenVINO for Intel
Returns:
Backend name: 'gpu', 'directml', 'openvino', 'coreml', or 'cpu'
"""
priority = _get_gpu_priority()
primary_gpu = priority['primary_gpu']
system = priority['capabilities']['system']
# Windows: Use DirectML for all GPUs
# DirectML is more reliable as it doesn't depend on system CUDA/ROCm libraries
if system == 'windows':
if primary_gpu in ['nvidia', 'amd_rocm', 'amd_other', 'amd_igpu', 'intel_igpu']:
return 'directml'
elif primary_gpu == 'intel_arc':
return 'openvino'
else:
return 'cpu'
# Linux and other platforms: Use hardware-specific backends
gpu_backend_map = {
'nvidia': 'gpu', # CUDA
'amd_rocm': 'cpu', # ONNX Runtime doesn't support ROCm yet
'amd_other': 'cpu',
'intel_arc': 'openvino',
'apple_silicon': 'coreml',
'intel_igpu': 'cpu',
'amd_igpu': 'cpu',
'cpu': 'cpu'
}
return gpu_backend_map.get(primary_gpu, 'cpu')
def _get_onnxruntime_package(self):
"""
Determine which ONNX Runtime package to install.
Uses get_recommended_backend() to decide.
Returns:
tuple: (package_name, from_url, index_url) where from_url and index_url
are None except for special cases like ROCm
"""
from_url = None
index_url = None
backend = self.get_recommended_backend()
if backend == 'gpu':
onnxruntime_pkg = 'onnxruntime-gpu'
# Check CUDA version for index_url
cuda_version = _detect_cuda_version(self.system)
if cuda_version and pkg_version.Version(cuda_version).major == 11:
index_url = "https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/onnxruntime-cuda-11/pypi/simple/"
elif backend == 'directml':
onnxruntime_pkg = 'onnxruntime-directml'
elif backend == 'openvino':
onnxruntime_pkg = 'onnxruntime-openvino'
elif backend == 'coreml':
# Standard onnxruntime supports CoreML on macOS
onnxruntime_pkg = 'onnxruntime'
else: # CPU or unknown
onnxruntime_pkg = 'onnxruntime'
return onnxruntime_pkg, from_url, index_url
def status(self):
"""
Prints the current status of the ONNX Helper class in regard to its support for different
OSes, GPUs. The world of heterogenous computing is developing rapidly and while support
for some of the frameworks for which helpers are available is not yet universally
available, hopefully it will improve in the future.
"""
print(f"ONNXHelper status as of sirilpy version {__version__}")
if self.system == 'windows':
print("Windows: ONNXHelper will install the DirectML runtime wherever it is supported. "
"This includes NVidia CPUs which might see a slight performance improvement using "
"the onnxruntime-gpu module as this is difficult to configure correctly on some "
"systems as it relies on system libraries and paths, so onnxruntime-directml is "
"more robust.")
elif self.system == 'linux':
print("Linux: ONNXHelper will attempt to detect NVidia, AMD and Intel GPUs and install "
"either onnxruntime-gpu, onnxruntime-rocm or onnxruntime-intel as appropriate. "
"Note that we have had limited feedback so far from AMD or Intel GPU users and none of "
"the developers have these GPUs, so although it is believed to work on them "
"we would be particularly grateful for bug reports on these systems.")
elif self.system == 'darwin':
print("MacOS: ONNXHelper will install the standard onnxruntime module on MacOS. This "
"provides good support for Apple silicon and may provide reasonable support for "
"older Intel silicon Macs.")
print("Detection of working ExecutionProviders: ONNXHelper tests using a simple model to "
"confirm whether each supported ExecutionProvider in the installed runtime actually "
"works or not. This helps to ensure that scripts calling "
"ONNXHelper.get_execution_providers_ordered() get a set of known working "
"ExecutionProviders. The set is cached so the model does not need to be run on "
"subsequent calls.")
print("Model features: ONNX does not support all machine learning functions that are "
"supported by all targets, and different runtimes support different subsets of "
"machine learning operations. This means that some more demandng models may not "
"work on all providers. In such cases the user may need to fall back to the "
"CPU ExecutionProvider. As onnxruntime does not automatically handle runtime errors "
"the ONNXHelper.run() method is provided to manage this (see the method docstring "
"for details).")
def _create_simple_onnx_model(self):
"""Create a simple ONNX model with matrix multiplication and ReLU."""
import onnx
from onnx import helper, TensorProto
input_shape = [1, 128, 256]
weight_shape = [256, 512]
input_tensor = helper.make_tensor_value_info('input', TensorProto.FLOAT, input_shape)
output_tensor = helper.make_tensor_value_info('output', TensorProto.FLOAT, [1, 128, 512])
weight_data = np.random.randn(*weight_shape).astype(np.float32)
weight_tensor = helper.make_tensor('weight', TensorProto.FLOAT, weight_shape, weight_data.flatten())
matmul_node = helper.make_node('MatMul', inputs=['input', 'weight'], outputs=['matmul_output'])
relu_node = helper.make_node('Relu', inputs=['matmul_output'], outputs=['output'])
graph = helper.make_graph(
nodes=[matmul_node, relu_node],
name='SimpleGraph',
inputs=[input_tensor],
outputs=[output_tensor],
initializer=[weight_tensor]
)
model = helper.make_model(graph, producer_name='ep_test')
model.opset_import[0].version = 11
model.ir_version = 8
onnx.checker.check_model(model)
return model
def _try_provider(self, ort, model_path, input_data, provider, reference_output=None):
"""Try executing the model with a specific provider (no fallback)."""
try:
# Handle provider format and extract expected provider name
if isinstance(provider, tuple):
expected_provider_name, provider_options = provider
providers_for_session = [(expected_provider_name, provider_options)]
else:
expected_provider_name = provider
providers_for_session = [provider]
sess_options = ort.SessionOptions()
session = ort.InferenceSession(
model_path,
sess_options=sess_options,
providers=providers_for_session
)
actual_provider = session.get_providers()[0]
if actual_provider != expected_provider_name:
print(f"(x) {provider}: fallback occurred (used {actual_provider})")
return False
# Determine TF32 usage
provider_opts = session.get_provider_options().get(expected_provider_name, {})
use_tf32 = provider_opts.get("use_tf32", "0") # default to "0" if missing
# Set tolerances
if use_tf32 == "1":
rtol = 5e-2
atol = 1e-3
else:
rtol = 1e-3
atol = 1e-5
output = session.run(None, {'input': input_data})
print(f"OK: {expected_provider_name} ran successfully")
if reference_output is not None:
# 1.4.0-beta4: disable this test for use_tf32 is True: it is failing because of the lower
# precision, but with zero impact on the output, so a more useful test needs to be found.
if not use_tf32 and not np.allclose(reference_output, output[0], rtol=rtol, atol=atol):
print(f"(!) Output mismatch with CPU (rtol={rtol}, atol={atol})")
return False
return True
except Exception as e:
print(f"(x) {expected_provider_name} failed: {e}")
return False
def import_onnxruntime(self):
"""
Import onnxruntime, add it to the global dict, test if it's built against
CUDA and if so preload the CUDA and CUDNN libraries to improve the chances
of finding them if Torch[CUDA] happens to be installed.
"""
import onnxruntime
globals()['onnxruntime'] = onnxruntime # Add to the global dict
providers = onnxruntime.get_available_providers()
if 'CUDAExecutionProvider' in providers:
# Attempt to preload CUDA / CUDnn libraries
# This helps on some systems where the system-wide libraries are not found but
# torch is installed with nvidia library dependencies installed in the venv
onnxruntime.preload_dlls()
# Set logging to only report critical issues by default
onnxruntime.set_default_logger_severity(4)
def test_onnxruntime(self, ort=None):
"""
Test an imported onnxruntime.
Args:install_torch(cuda_version=cuda_version)
ort: The ONNX runtime module to test. If None, the method will
attempt to import onnxruntime for the test.
Returns:
list: a list of confirmed working ONNXRuntime ExecutionProviders in priority order
"""
import onnx
if ort is None:
try:
import onnxruntime as ort
except ImportError:
print("(x) Unable to import onnxruntime. Test failed.")
return []
print("=== ONNX Execution Provider Tester ===")
print("Creating ONNX model...")
model = self._create_simple_onnx_model()
# Create temporary file for the model - Windows compatible approach
temp_file = tempfile.NamedTemporaryFile(suffix='.onnx', delete=False)
model_path = temp_file.name
temp_file.close() # Close the file handle immediately
try:
onnx.save(model, model_path)
print(f"Model saved to temporary file: {model_path}")
input_data = np.random.randn(1, 128, 256).astype(np.float32)
print("\nRunning reference on CPU...")
cpu_output = None
try:
cpu_session = ort.InferenceSession(model_path, providers=['CPUExecutionProvider'])
cpu_output = cpu_session.run(None, {'input': input_data})[0]
print("OK: CPU output computed.")
except Exception as e:
print(f"(x) Failed to run on CPU: {e}")
return []
all_providers_raw = ort.get_available_providers()
all_providers = []
for p in all_providers_raw:
if p == "OpenVINOExecutionProvider":
all_providers.append((p, {
'device_type': 'GPU',
'precision': 'FP32'
},))
all_providers.append((p, {
'device_type': 'CPU',
'precision': 'FP32'
}))
else:
all_providers.append(p)
print("\nAvailable execution providers:")
for p in all_providers:
print(f" - {p}")
print("\nTesting each provider without fallback...")
working_providers = []
for provider in all_providers:
print(f"\nTesting {provider}...")
with SuppressedStderr():
if self._try_provider(ort, model_path, input_data, provider, reference_output=cpu_output):
working_providers.append(provider)
print("\n=== Summary ===")
if working_providers:
print("OK: Working providers (in priority order):")
for p in working_providers:
print(f" - {p}")
print(f"\n→ Best available provider: {working_providers[0]}")
# Save to cache
self._save_providers_to_cache(working_providers)
self.providers = working_providers
return working_providers
print("(x) No execution providers were able to run the model.")
return []
finally:
# Clean up the temporary file
try:
os.unlink(model_path)
except (OSError, FileNotFoundError):
# File might already be deleted or inaccessible, ignore
pass
def run(self, session, model_path, output_names, input_feed, run_options=None, return_first_output=False):
"""
Run inference with automatic CPU fallback if the session fails.
Args:
session: The ONNX runtime inference session
model_path (str): Path to the ONNX model file (needed for CPU fallback)
output_names: Names of the outputs to compute, or None for all outputs
input_feed: Dictionary mapping input names to input tensors
run_options: Optional run options for the inference session
return_first_output (bool): If True, return only the first output instead of the full list
Returns:
tuple: (result, session) where result is the inference output (or first output if return_first_output=True) and
session is the (potentially updated) inference session
"""
import onnxruntime
try:
# Try running with the provided session
result = session.run(output_names, input_feed, run_options)
if return_first_output:
result = result[0]
return result, session
except Exception:
print("Warning: falling back to CPU.")
# Create a new CPU-only session
providers = ['CPUExecutionProvider']
cpu_session = onnxruntime.InferenceSession(model_path, providers=providers)
# Run with the CPU session
result = cpu_session.run(output_names, input_feed, run_options)
if return_first_output:
result = result[0]
return result, cpu_session
def install_onnxruntime(self, force=False):
"""
Detect system configuration and install the appropriate ONNX Runtime package.
Args:
force: bool: If True, force reinstallation even if onnxruntime is already installed.
Returns:
bool: True if installation was successful or already installed, False otherwise.
Raises:
TimooutError: if a TimeoutError occurs in ensure_installed() - this avoids falling
back to the CPU-only package purely because of network issues
"""
# First check if any onnxruntime is already installed
try:
is_installed, package_name = self.is_onnxruntime_installed()
except:
# Error checking if it's installed
print("Error checking ONNX Runtime state. Removing and reinstalling...")
self.uninstall_onnxruntime()
is_installed = False
if is_installed:
if force:
print("ONNX Runtime is already installed. Removing and reinstalling...")
self.uninstall_onnxruntime()
if self.config_file is not None and os.path.exists(self.config_file):
os.unlink(self.config_file)
else:
print(f"ONNX Runtime is already installed: {package_name}")
return True
# If not installed, get recommended package
onnxruntime_pkg, from_url, index_url = self._get_onnxruntime_package()
# Check if the package exists
if not self._check_onnxruntime_availability(onnxruntime_pkg):
print(f"Package {onnxruntime_pkg} not found. Falling back to default onnxruntime.")
onnxruntime_pkg = "onnxruntime"
# Install the package
try:
if not _check_package_installed(onnxruntime_pkg):
_install_package(onnxruntime_pkg, None, from_url=from_url, index_url=index_url)
# For openvino we need to set the runtime environment:
# https://github.com/intel/onnxruntime/releases/tag/v5.6
if self.system == 'windows' and onnxruntime_pkg == 'onnxruntime-openvino':
import onnxruntime.tools.add_openvino_win_libs as utils
utils.add_openvino_libs_to_path()
try:
self.import_onnxruntime()
except ImportError as e:
print(f"Checked installed runtime {onnxruntime_pkg} cannot be imported: {e}. Falling back to the basic CPU runtime", file=sys.stderr)
self.uninstall_onnxruntime()
_install_package(onnxruntime, None)
except TimeoutError as e:
print(f"Failed to install {onnxruntime_pkg}: timeout error {str(e)}")
raise TimeoutError("Error: timeout in install_onnxruntime()") from e
except Exception as e:
print(f"Failed to install {onnxruntime_pkg}: {str(e)}")
print("Falling back to default onnxruntime package.")
try:
ensure_installed("onnxruntime")
except Exception as err:
print(f"Failed to install default onnxruntime: {str(err)}")
return False
return True
def ensure_onnxruntime(self) -> bool:
"""
Wrapper for install_onnxruntime() that only installs it if needed, with
negligible overhead if it is already installed.
"""
if not self.is_onnxruntime_installed():
return self.install_onnxruntime()
return True
def is_onnxruntime_installed(self):
"""
Check if any onnxruntime package is already installed and usable.
Returns:
tuple: (is_installed, package_name) where package_name could be
'onnxruntime', 'onnxruntime-gpu', 'onnxruntime-silicon', etc.
"""
try:
# Try importing onnxruntime - if this fails, the package is not usable
self.import_onnxruntime()
except ImportError as e:
# If import fails, package is not usable regardless of pip list
# One of the error messages is a clue that MSVC runtime need updating
if platform.system().lower() == "windows" and \
"DLL load failed" in str(e):
print("DLL load failed. This means you need to update system "
"libraries. Usually updating Microsoft Visual C++ Runtime "
"will solve the issue.", file=sys.stderr)
return False, None
except Exception as e:
print(f"Error checking for installed onnxruntime: {e}")
return False, None
# If we get here, some version of onnxruntime is installed and working
package_name = "onnxruntime" # Default assumption
# Check provider information to determine specific package variant
providers = onnxruntime.get_available_providers()
print(f"Detected ONNX Runtime with providers: {providers}")
# Check for specific provider patterns
if any(p for p in providers if "CUDA" in p or "GPU" in p):
package_name = "onnxruntime-gpu"
elif any(p for p in providers if "DirectML" in p):
package_name = "onnxruntime-directml"
elif any(p for p in providers if "ROCm" in p):
package_name = "onnxruntime-rocm"
elif any(p for p in providers if "OpenVINO" in p or "DML" in p):
package_name = "onnxruntime-intel"
return True, package_name
def _check_onnxruntime_availability(self, package_name):
"""
Check if the specified ONNX Runtime package is available on PyPI.
Args:
package_name (str): Package name to check
Returns:
bool: True if the package is available, False otherwise.
"""
try:
output = subprocess.check_output(
[sys.executable, "-m", "pip", "index", "versions", package_name],
stderr=subprocess.DEVNULL,
text=True
)
return "No matching distribution found" not in output
except subprocess.SubprocessError:
# If the command fails, check directly from PyPI
try:
url = f"https://pypi.org/pypi/{package_name}/json"
response = requests.get(url, timeout=10)
return response.status_code == 200
except requests.exceptions.RequestException:
print("Connection error {e}, please try again later")
raise
except Exception:
return False
def get_execution_providers_ordered(self, ai_gpu_acceleration=True, force_check=False):
"""
Get execution providers ordered by priority.
This function returns a list of available ONNX Runtime execution providers
in a reasonable order of priority, covering major GPU platforms:
The CPU provider is always included as the final fallback option.
Args:
ai_gpu_acceleration (bool): Whether to include GPU acceleration providers.
Defaults to True.
force_check (bool): Whether to force re-checking even if a cached config exists.
Defaults to False.
Returns:
list: Ordered list of available execution providers.
"""
if force_check is True: # Clear any previous config so it has to be re-checked
self.providers = None
if os.path.exists(self.config_file):
os.unlink(self.config_file)
if ai_gpu_acceleration is False:
return ["CPUExecutionProvider"]
if self.providers is not None:
return self.providers
self.import_onnxruntime()
# Try to load cached providers first
try:
cached_providers = self._load_cached_providers()
if cached_providers:
self.providers = cached_providers
return self.providers
except Exception:
# If an error occurs with _load_cached_providers() delete the config file and do
# the full test to re-cache them
if os.path.exists(self.config_file):
os.unlink(self.config_file)
# If no valid cache, run the test
return self.test_onnxruntime(onnxruntime)
def _load_cached_providers(self):
"""
Load cached execution providers from config file if they're still valid.
Returns:
list or None: List of cached providers if valid, None otherwise
"""
if not os.path.exists(self.config_file):
return None
try:
with open(self.config_file, 'r', encoding='utf-8') as f:
cached_data = json.load(f)
raw_providers = cached_data.get('execution_providers', [])
cached_providers = []
for p in raw_providers:
name = p["name"]
options = p["options"]
if options is not None:
cached_providers.append((name, options))
else:
cached_providers.append(name)
if not cached_providers:
return None
print(f"Using cached execution providers from {self.config_file}")
return valid_providers
except (json.JSONDecodeError, IOError, KeyError) as e:
print(f"Failed to load cached providers: {e}", file=sys.stderr)
os.unlink(self.config_file)
return None
def _save_providers_to_cache(self, providers):
"""
Save execution providers to config file.
Args:
providers (list): List of working execution providers
"""
try:
# Ensure directory exists
os.makedirs(os.path.dirname(self.config_file), exist_ok=True)
serialized_providers = []
for p in providers:
if isinstance(p, tuple):
provider_name, options = p
serialized_providers.append({"name": provider_name, "options": options})
else:
serialized_providers.append({"name": p, "options": None})
cache_data = {
'execution_providers': serialized_providers,
'system': self.system,
'cached_at': platform.platform()
}
with open(self.config_file, 'w', encoding='utf-8') as f:
json.dump(cache_data, f, indent=2)
print(f"Cached execution providers to {self.config_file}")
except IOError as e:
print(f"Failed to save providers cache: {e}", file=sys.stderr)
def uninstall_onnxruntime(self):
"""
Detects and uninstalls all variants of onnxruntime packages.
Checks for any package starting with 'onnxruntime'.
Returns:
list: A list of uninstalled packages
"""
# Remove the execution provders config file
if os.path.exists(self.config_file):
os.unlink(self.config_file)
self.providers = None
# Get all installed packages
try:
result = subprocess.run(
[sys.executable, "-m", "pip", "list"],
capture_output=True,
text=True,
check=True
)
installed_packages = result.stdout.splitlines()
except subprocess.CalledProcessError as e:
print(f"Error getting installed packages: {e}")
return []
# Find all packages that start with 'onnxruntime'
onnx_packages = []
for line in installed_packages:
parts = line.split()
if parts and parts[0].lower().startswith('onnxruntime'):
onnx_packages.append(parts[0])
# Uninstall found packages
if not onnx_packages:
print("No onnxruntime packages found.")
return []
print(f"Found onnxruntime packages: {', '.join(onnx_packages)}")
uninstalled = []
for package in onnx_packages:
print(f"Uninstalling {package}...")
try:
subprocess.run(
[sys.executable, "-m", "pip", "uninstall", "-y", package],
check=True
)
uninstalled.append(package)
print(f"Successfully uninstalled {package}")
except subprocess.CalledProcessError:
print(f"Failed to uninstall {package}")
return uninstalled
class TorchHelper:
"""Helper class for PyTorch detection, installation and testing."""
def __init__(self):
"""Initialize TorchHelper without importing torch at module level."""
self.device_info: Optional[dict] = None
self._torch_installed = False
self.system = platform.system().lower()
def is_torch_installed(self) -> bool:
"""Check if PyTorch is installed without importing it."""
if self._torch_installed:
return True
# Check if torch is available
torch_spec = importlib.util.find_spec("torch")
if torch_spec is not None:
self._torch_installed = True
return True
return False
def status(self):
"""
Prints the current status of the Torch Helper class in regard to its support for different
OSes, GPUs. The world of heterogenous computing is developing rapidly and while support
for some of the frameworks for which helpers are available is not yet universally
available, hopefully it will improve in the future.
"""
print(f"TorchHelper status as of sirilpy version {__version__}")
if self.system == 'windows':
print("Windows: TorchHelper will install Torch. A version may be specified but by default "
"autodetection will take place. The recommended CUDA version will be installed for NVidia GPUs "
"but other CUDA versions may be specified manually. For AMD and Intel GPUs the ROCm or XPU "
"version will be installed.")
elif self.system == 'linux':
print("Linux: TorchHelper will install Torch. A version may be specified but by default "
"autodetection will take place. The recommended CUDA version will be installed for NVidia GPUs "
"but other CUDA versions may be specified manually. For AMD and Intel GPUs the ROCm or XPU "
"version will be installed.")
elif self.system == 'darwin':
print("MacOS: TorchHelper will install the standard Torch module on MacOS. This is targeted "
"at all Apple Macs regardless of CPU architecture: any issues with Torch on MacOS "
"should be reported upstream to Torch.")
print("Dependencies: Torch is currently excessively strict about required versions of some "
"dependencies including CUDnn: it requires an exact version match rather than at least a "
"certain version. This cauess conflict with other GPU acceleration modules that have "
"differing dependency requirements, including jax. In order to accommodate scripts that "
"require both, Torch is installed twice - first normally, and second with the --nodeps "
"flag, as advised by the Torch project. However we note that this is a problematic approach "
"and have encouraged Torch to adopte a more pragmatic approach in future.")
def get_recommended_backend(self) -> Dict[str, Any]:
"""
Determine the recommended PyTorch backend and installation parameters.
When multiple GPUs are present, uses priority system to choose the best one.
PyTorch has good cross-platform support:
- NVIDIA: CUDA on all platforms
- AMD ROCm-compatible: ROCm on Linux AND Windows
- Intel Arc: XPU backend
- Apple Silicon: MPS backend
Returns:
Dict with 'backend', 'cuda_version', 'extra_index_url', 'packages' keys
"""
priority = _get_gpu_priority()
primary_gpu = priority['primary_gpu']
caps = priority['capabilities']
# NVIDIA GPUs - CUDA backend on all platforms
if primary_gpu == 'nvidia':
cuda_version = caps['nvidia']['recommended_cuda']
return {
'backend': 'cuda',
'cuda_version': cuda_version,
'extra_index_url': f'https://download.pytorch.org/whl/{cuda_version}',
'packages': ['torch', 'torchvision']
}
# AMD ROCm-compatible GPUs - ROCm backend on Linux and Windows
if primary_gpu == 'amd_rocm':
return {
'backend': 'rocm',
'cuda_version': None,
'extra_index_url': 'https://download.pytorch.org/whl/rocm7.1',
'packages': ['torch', 'torchvision']
}
# Intel Arc GPUs
if primary_gpu == 'intel_arc':
return {
'backend': 'intel',
'cuda_version': None,
'extra_index_url': 'https://download.pytorch.org/whl/xpu',
'packages': ['torch', 'torchvision']
}
# Apple Silicon
if primary_gpu == 'apple_silicon':
return {
'backend': 'mps',
'cuda_version': None,
'extra_index_url': None,
'packages': ['torch', 'torchvision']
}
# All other cases (AMD non-ROCm, iGPUs, CPU) - use CPU
return {
'backend': 'cpu',
'cuda_version': None,
'extra_index_url': None,
'packages': ['torch', 'torchvision']
}
def ensure_torch(self, cuda_version: Optional[str] = None) -> bool:
"""
Ensure PyTorch is installed with the appropriate backend.
Args:
cuda_version: Optional CUDA version to override auto-detection
(e.g., 'cu118', 'cu126', 'cu128')
Returns: True on success, False on failure
"""
if self.is_torch_installed():
print("Torch is already installed")
return True
backend_info = self.get_recommended_backend()
# Override CUDA version if specified
if cuda_version is not None and backend_info['backend'] == 'cuda':
backend_info['cuda_version'] = cuda_version
backend_info['extra_index_url'] = f'https://download.pytorch.org/whl/{cuda_version}'
print(f"Installing PyTorch with backend: {backend_info['backend']}")
if backend_info['cuda_version']:
print(f"Using CUDA version: {backend_info['cuda_version']}")
try:
self.install_torch(
cuda_version=backend_info['cuda_version'],
extra_index_url=backend_info['extra_index_url'],
packages=backend_info['packages']
)
return True
except Exception as e:
print(f"Error installing Torch: {e}")
return False
def install_torch(self, cuda_version: Optional[str] = None,
extra_index_url: Optional[str] = None,
packages: Optional[list] = None):
"""
Install PyTorch with specified configuration.
Args:
cuda_version: CUDA version (e.g., 'cu118', 'cu126', 'cu128')
extra_index_url: PyTorch wheel repository URL
packages: List of packages to install
"""
if packages is None:
packages = ['torch', 'torchvision', 'torchaudio']
install_cmd = [sys.executable, '-m', 'pip', 'install'] + packages
if extra_index_url:
install_cmd.extend(['--index-url', extra_index_url])
try:
print(f"Installing: {' '.join(packages)}")
subprocess.run(install_cmd, check=True)
self.torch_installed = self.is_torch_installed()
print("PyTorch installation completed successfully")
except subprocess.CalledProcessError as e:
print(f"Failed to install PyTorch: {e}")
raise
def _detect_compute_platform(self) -> str:
"""
Auto-detect the appropriate compute platform for PyTorch installation.
Returns:
str: The compute platform string ('cu128', 'rocm', 'cpu')
"""
if self.system == 'darwin':
# macOS: use standard package
return 'cpu' # This will trigger standard installation
if self.system == 'windows':
if _detect_nvidia_gpu(self.system):
return 'cu128'
return 'cpu'
if self.system == 'linux':
if _detect_nvidia_gpu(self.system):
return 'cu128'
if _detect_amd_gpu():
return 'rocm'
return 'cpu'
# Unknown system, default to CPU
print(f"Unknown system '{self.system}', defaulting to CPU installation")
return 'cpu'
def _import_torch(self) -> bool:
"""Import torch modules to verify availability."""
try:
import torch
self._torch_installed = True
# Get device info after successful import
self.device_info = self._get_device_info()
print(f"OK: PyTorch {torch.__version__} imported successfully!")
return True
except ImportError as e:
print(f"(x) Failed to import PyTorch: {e}")
return False
except Exception as e:
print(f"(x) Unexpected error importing PyTorch: {e}")
return False
def _get_device_info(self) -> dict:
"""Get information about available devices."""
try:
import torch
except ImportError:
return {}
info = {
'pytorch_version': torch.__version__,
'cuda_available': torch.cuda.is_available(),
'cuda_version': torch.version.cuda if torch.cuda.is_available() else None,
'gpu_count': torch.cuda.device_count() if torch.cuda.is_available() else 0,
'gpu_names': []
}
if torch.cuda.is_available():
for i in range(torch.cuda.device_count()):
info['gpu_names'].append(torch.cuda.get_device_name(i))
return info
def _create_simple_model(self, input_dim=256, hidden_dim=512, output_dim=128):
"""Create a simple neural network model."""
try:
import torch
from torch import nn
except ImportError:
raise RuntimeError("PyTorch not available. Call install_torch() first.")
class SimpleModel(nn.Module):
""" Simple model to test torch can utilise a host """
def __init__(self, input_dim, hidden_dim, output_dim):
super().__init__()
self.linear1 = nn.Linear(input_dim, hidden_dim)
self.linear2 = nn.Linear(hidden_dim, hidden_dim)
self.linear3 = nn.Linear(hidden_dim, output_dim)
self.dropout = nn.Dropout(0.1)
def forward(self, x):
""" Simple function within the test model """
import torch.nn.functional as F
x = F.relu(self.linear1(x))
x = self.dropout(x)
x = F.relu(self.linear2(x))
x = self.linear3(x)
return x
return SimpleModel(input_dim, hidden_dim, output_dim)
def _create_test_data(self, batch_size=32, input_dim=256):
"""Create random test input data."""
try:
import torch
except ImportError:
raise RuntimeError("PyTorch not available. Call install_torch() first.")
return torch.randn(batch_size, input_dim)
def _benchmark_model(self, model, input_data, device, num_runs=10):
"""Benchmark model execution on specified device."""
try:
import torch
except ImportError:
raise RuntimeError("PyTorch not available. Call install_torch() first.")
# Set model to evaluation mode to disable dropout
model.eval()
model = model.to(device)
input_data = input_data.to(device)
# Warm up
for _ in range(3):
with torch.no_grad():
_ = model(input_data)
# Benchmark
if device.type == 'cuda':
torch.cuda.synchronize()
start_time = time.time()
for _ in range(num_runs):
with torch.no_grad():
output = model(input_data)
if device.type == 'cuda':
torch.cuda.synchronize()
end_time = time.time()
avg_time = (end_time - start_time) / num_runs
return output, avg_time
def _print_device_info(self):
"""Print device information."""
print("=== Device Information ===")
print(f"PyTorch version: {self.device_info['pytorch_version']}")
print(f"CUDA available: {self.device_info['cuda_available']}")
if self.device_info['cuda_available']:
print(f"CUDA version: {self.device_info['cuda_version']}")
print(f"GPU count: {self.device_info['gpu_count']}")
for i, name in enumerate(self.device_info['gpu_names']):
print(f" GPU {i}: {name}")
def get_torch_device(self, use_gpu: Optional[bool] = True) -> "torch.device":
"""
Obtains a suitable torch device based on the capabilities of the installed
torch package. if use_gpu is False, torch.device("cpu") will be returned.
This function is available since sirilpy 1.0.17
"""
try:
import torch
except ImportError:
return False
if not use_gpu:
print("Using CPU")
return torch.device("cpu") # User has disabled GPU acceleration
if torch.cuda.is_available():
print("Using CUDA (NVidia / AMD)")
return torch.device("cuda") # Nvidia / AMD GPU support
if torch.backends.mps.is_available():
# Check if we're on Apple Silicon (ARM64), not Intel
import platform
if platform.machine() == "arm64":
print("Using MPS (Apple Silicon)")
return torch.device("mps") # Apple MPS support (M1/M2/M3)
else:
print("MPS detected but not applicable on Intel Mac - falling back to CPU")
if hasattr(torch, 'xpu') and torch.xpu.is_available():
print("Using XPU (Intel ARC / XPU)")
return torch.device("xpu") # Intel Arc / XPU Support
# DirectML support (Windows DirectX 12)
try:
import torch_directml
if torch_directml.is_available():
print("Using DirectML")
return torch_directml.device() # DirectML device
except ImportError:
pass
print("No GPU acceleration available, falling back to CPU use")
return torch.device("cpu")
def _test_torch_gpu(self):
"""Test PyTorch model execution on GPU."""
print("=== PyTorch GPU Test ===")
if not self._import_torch():
print("PyTorch not available. Please install it first using install_torch()")
return False
try:
import torch
except ImportError:
return False
# Print device info
self._print_device_info()
# Create model and test data with fixed seeds for reproducible results
print("\nCreating model and test data...")
torch.manual_seed(42) # Set seed for reproducible results
model = self._create_simple_model()
torch.manual_seed(42) # Reset seed for consistent input data
input_data = self._create_test_data(batch_size=64, input_dim=256)
print(f"Model parameters: {sum(p.numel() for p in model.parameters()):,}")
print(f"Input shape: {input_data.shape}")
# Test CPU execution
print("\nTesting CPU execution (for accuracy reference)...")
cpu_device = torch.device('cpu')
model_cpu = self._create_simple_model() # Create fresh model
torch.manual_seed(42) # Ensure same initialization
model_cpu.load_state_dict(model.state_dict()) # Copy weights
cpu_output, _ = self._benchmark_model(model_cpu, input_data, cpu_device)
# Test GPU execution
gpu_device = self.get_torch_device()
if gpu_device.type != "cpu":
print("\nTesting GPU execution...")
try:
model_gpu = self._create_simple_model() # Create fresh model for GPU
model_gpu.load_state_dict(model.state_dict()) # Copy same weights
gpu_output, _ = self._benchmark_model(model_gpu, input_data, gpu_device)
print("OK: GPU execution successful!")
# Compare outputs using appropriate tolerance for CPU vs GPU
cpu_output_np = cpu_output.numpy()
gpu_output_np = gpu_output.cpu().numpy()
# Check if outputs are close (accounting for floating point differences)
are_close = np.allclose(cpu_output_np, gpu_output_np, rtol=1e-3, atol=1e-4)
if are_close:
print("OK: CPU and GPU outputs match within tolerance!")
else:
print("(!) CPU and GPU outputs differ more than expected")
except Exception as e:
print(f"(x) GPU execution failed: {e}")
else:
print("\n(!) CUDA not available - cannot test GPU execution")
print("To enable GPU support:")
print(" 1. Install CUDA toolkit")
print(" 2. Use install_torch() with appropriate CUDA version")
print("\n" + "="*50)
return True
def _test_tensor_operations(self):
"""Test basic tensor operations on GPU."""
print("=== Tensor Operations Test ===")
retval = None
if not self._import_torch():
print("PyTorch not available. Please install it first using install_torch()")
return False
try:
import torch
except ImportError:
return False
if not torch.cuda.is_available():
print("(!) CUDA not available - skipping tensor operations test")
return False
# Create large tensors for meaningful GPU test with fixed seed
size = 2048
print(f"Creating {size}x{size} tensors...")
# Set seed for reproducible random tensors
torch.manual_seed(123)
a_cpu = torch.randn(size, size)
b_cpu = torch.randn(size, size)
# GPU tensors (copy the same data)
a_gpu = a_cpu.clone().cuda()
b_gpu = b_cpu.clone().cuda()
# CPU matrix multiplication
start_time = time.time()
c_cpu = torch.mm(a_cpu, b_cpu)
cpu_time = time.time() - start_time
# GPU matrix multiplication
torch.cuda.synchronize()
start_time = time.time()
c_gpu = torch.mm(a_gpu, b_gpu)
torch.cuda.synchronize()
gpu_time = time.time() - start_time
# Verify results using appropriate tolerance for large matrix operations
cpu_np = c_cpu.numpy()
gpu_np = c_gpu.cpu().numpy()
# Use more appropriate tolerances for large matrix multiplication
are_close = np.allclose(cpu_np, gpu_np, rtol=1e-3, atol=1e-3)
if are_close:
print("OK: Results match within tolerance!")
retval = True
else:
print("(!) Results differ more than expected")
retval = False
print(f"CPU time: {cpu_time:.4f}s")
print(f"GPU time: {gpu_time:.4f}s")
print(f"Speedup: {cpu_time/gpu_time:.2f}x")
print("\n" + "="*50)
return retval
def test_torch(self):
"""
Run tests to verify that torch is installed and runs correctly.
"""
if not self.ensure_torch():
print("Cannot run tests - PyTorch not available")
print("Use helper.install_torch() or helper.test_torch(auto_install=True)")
return False
self._test_torch_gpu()
return self._test_tensor_operations()
def uninstall_torch(self):
"""
Detects and uninstalls PyTorch and related packages.
Checks for torch ecosystem packages.
Returns:
list: A list of uninstalled packages
"""
self._torch_installed = False
self.device_info = None
# Define torch ecosystem packages to look for
torch_ecosystem_packages = [
'torch', 'torchvision', 'torchaudio', 'torchtext', 'torchdata',
'pytorch-lightning', 'lightning', 'pytorch-ignite', 'ignite',
'fastai', 'transformers', 'accelerate', 'timm'
]
# Get all installed packages
try:
result = subprocess.run(
[sys.executable, "-m", "pip", "list"],
capture_output=True,
text=True,
check=True
)
installed_packages = result.stdout.splitlines()
except subprocess.CalledProcessError as e:
print(f"Error getting installed packages: {e}")
return []
# Extract package names from pip list output
installed_package_names = set()
for line in installed_packages:
parts = line.split()
if parts:
installed_package_names.add(parts[0].lower())
# Find torch packages that are actually installed
torch_packages = []
for package in torch_ecosystem_packages:
if package.lower() in installed_package_names:
torch_packages.append(package)
# Uninstall found packages
if not torch_packages:
print("No torch packages found.")
return []
print(f"Found torch packages: {', '.join(torch_packages)}")
uninstalled = []
for package in torch_packages:
print(f"Uninstalling {package}...")
try:
subprocess.run(
[sys.executable, "-m", "pip", "uninstall", "-y", package],
check=True
)
uninstalled.append(package)
print(f"Successfully uninstalled {package}")
except subprocess.CalledProcessError:
print(f"Failed to uninstall {package}")
return uninstalled
class JaxHelper:
"""
A helper class for detecting, installing, and testing JAX with appropriate hardware acceleration.
This class automatically detects the system configuration and installs the correct JAX variant
(CPU, CUDA, ROCm, etc.) based on available hardware.
"""
def __init__(self):
self.system = platform.system().lower()
self.jax_installed = False
self.detected_config = None
def status(self):
"""
Prints the current status of the Jax Helper class in regard to its support for different
OSes, GPUs. The world of heterogenous computing is developing rapidly and while support
for some of the frameworks for which helpers are available is not yet universally
available, hopefully it will improve in the future.
"""
print(f"JaxHelper status as of sirilpy version {__version__}")
if self.system == 'windows':
print("Windows: JaxHelper will install the CPU version of jax. This does not provide "
"acceleration and jax.numpy may often be slower than numpy itself, so it is only "
"recommended for development and research purposes at the moment. A jax CUDA wheel is "
"in development for Windows however the current version does not support numpy v2.x "
"and therefore causes unacceptable conflict with other modules. As the wheel develops "
"the CUDA wheel will hopefully be supportable in the future. There is also an Intel "
"plugin under development, however as with the CUDA wheel this is not yet built so "
"as to be compatible with numpy 2.x so the helper will not install it. An issue has "
"been raised with the plugin authors and we hope progress on this plugin improves soon.")
elif self.system == 'linux':
print("Linux: JaxHelper will install jax variants for NVidia or AMD GPUs, with automatic "
"GPU hardware detection. Note that as none of the developers have an AMD GPU, feedback "
"on how well jax[rocm] works would be very much appreciated. There is also an Intel "
"plugin under development, however as with the CUDA wheel this is not yet built so "
"as to be compatible with numpy 2.x so the helper will not install it. An issue has "
"been raised with the plugin authors and we hope progress on this plugin improves soon.")
elif self.system == 'darwin':
print("MacOS: ONNXHelper will install the Metal jax backed on MacOS. Note that this is still "
"experimental and may provide only limited acceleration at present. We hope that support "
"for this platform improves soon.")
print("Dependencies: Jax has a sensible dependency on CUDnn however unfortunately it conflicts "
"with Torch, which is currently excessively strict about required versions of some "
"dependencies including CUDnn: it requires an exact version match rather than at least a "
"certain version, and the version Torch requires is older than the version that jax is "
"built against. This issue is best handled by using TorchHelper.install_torch() to install "
"Torch first (this auto-reinstalls with --no-deps), and then using JaxHelper.install_jax() to "
"install Jax. (You can do this the other way round and it will still work but will be less "
"efficient.)")
def is_jax_installed(self) -> bool:
"""Check if PyTorch is installed without importing it."""
if self.jax_installed:
return True
# Check if torch is available
jax_spec = importlib.util.find_spec("jax")
if jax_spec is not None:
self.jax_installed = True
return True
return False
def get_recommended_backend(self) -> Dict[str, Any]:
"""
Determine the recommended JAX backend and installation parameters.
When multiple GPUs are present, uses priority system to choose the best one.
JAX has limited platform support:
- NVIDIA: CUDA on Linux (Windows support experimental/limited)
- AMD ROCm: Linux only
- Apple Silicon: Metal backend
- Intel: Experimental plugin with dependency issues
Returns:
Dict with 'backend', 'packages', 'extra_index_url' keys
"""
priority = _get_gpu_priority()
primary_gpu = priority['primary_gpu']
system = priority['capabilities']['system']
# NVIDIA GPU - CUDA on Linux
if primary_gpu == 'nvidia' and system == 'linux':
return {
'backend': 'cuda',
'packages': ['jax[cuda12]'],
'extra_index_url': None
}
# AMD ROCm - Linux only
if primary_gpu == 'amd_rocm' and system == 'linux':
return {
'backend': 'rocm',
'packages': ['jax[rocm]'],
'extra_index_url': None
}
# Apple Silicon
if primary_gpu == 'apple_silicon':
return {
'backend': 'metal',
'packages': ['jax-metal'],
'extra_index_url': None
}
# Intel Arc (has dependency conflicts currently)
if primary_gpu == 'intel_arc':
return {
'backend': 'intel',
'packages': ['jax', 'intel-extension-for-openxla'],
'extra_index_url': None
}
# All other cases fall back to CPU
# This includes: NVIDIA/AMD on Windows, AMD non-ROCm, iGPUs
return {
'backend': 'cpu',
'packages': ['jax'],
'extra_index_url': None
}
def _detect_hardware_config(self) -> Dict[str, Any]:
"""
Detect the hardware configuration and determine the appropriate JAX variant.
Uses the GPU priority system to handle multi-GPU scenarios.
Returns:
Dict containing detected hardware info and recommended JAX installation.
"""
priority = _get_gpu_priority()
caps = priority['capabilities']
config = {
'system': self.system,
'primary_gpu': priority['primary_gpu'],
'has_nvidia_gpu': caps['nvidia'] is not None and caps['nvidia']['detected'],
'has_amd_gpu': caps['amd'] is not None and caps['amd']['detected'],
'has_intel_gpu': caps['intel'] is not None and caps['intel']['detected'],
'cuda_version': caps['nvidia']['recommended_cuda'] if caps['nvidia'] else None,
'amd_rocm_compatible': caps['amd']['rocm_compatible'] if caps['amd'] else False,
'intel_is_arc': caps['intel']['is_arc'] if caps['intel'] else False,
'apple_silicon': caps['apple_silicon'] is not None,
'recommended_jax_variant': 'jax[cpu]',
'install_url': None,
'index_url': None
}
# Determine JAX variant based on hardware
config = self._determine_jax_variant(config)
self.detected_config = config
return config
def _determine_jax_variant(self, config: Dict[str, Any], force_cpu=False) -> Dict[str, Any]:
"""
Determine the appropriate JAX variant based on detected hardware.
Uses primary_gpu from priority system when available.
Args:
config: Hardware configuration dictionary
force_cpu: forces CPU-only installation
Returns:
Updated configuration with JAX variant recommendation
"""
if config['system'] == 'windows':
print("(!) The Windows jax[cuda] wheel currently does not "
"support numpy 2.x and therefore causes unacceptable "
"conflicts with other packages. As jax Windows support "
"matures we hope to enable the cuda wheel however at "
"present only CPU support is available, which is mostly "
"only useful for development purposes.")
if force_cpu:
config['recommended_jax_variant'] = 'jax[cpu]'
print("(!) Warning: performance of the CPU-only jax variant "
"is slow and is intended for development use only. It "
"is generally recommended that you do NOT enable jax "
"optimisation in scripts that offer it!")
return config
# Use primary_gpu if available (from priority system)
primary_gpu = config.get('primary_gpu', 'cpu')
# NVIDIA GPU - Linux only
if primary_gpu == 'nvidia' and config['system'] == 'linux':
config['recommended_jax_variant'] = 'jax[cuda12]'
print(f"Detected NVIDIA GPU as primary - will install jax[cuda12]")
# AMD ROCm GPU - Linux only
elif primary_gpu == 'amd_rocm' and config['system'] == 'linux':
config['recommended_jax_variant'] = 'jax[rocm]'
print("Detected ROCm-compatible AMD GPU as primary - will install jax[rocm]")
print("(!) AMD ROCm support in JAX is experimental - feedback appreciated!")
# AMD ROCm GPU on Windows - not supported by JAX
elif primary_gpu == 'amd_rocm' and config['system'] == 'windows':
print("(!) AMD ROCm-compatible GPU detected on Windows: unfortunately "
"JAX does not yet support ROCm on Windows. Falling back to CPU-only.")
config['recommended_jax_variant'] = 'jax[cpu]'
# Intel Arc GPU (has dependency conflicts currently)
elif primary_gpu == 'intel_arc':
print("(!) Intel Arc GPU detected as primary: unfortunately the experimental "
"jax plugin for Intel GPUs has dependency clashes and "
"still requires numpy 1.x therefore only the CPU variant "
"can be installed. Hopefully this will change in the "
"future.")
config['recommended_jax_variant'] = 'jax[cpu]'
# Apple Silicon
elif primary_gpu == 'apple_silicon':
config['recommended_jax_variant'] = 'jax-metal'
print("Detected Apple Silicon - will install jax-metal")
print("(!) jax Metal support on macOS is still experimental")
# Fallback cases for when priority system isn't used
elif config['has_nvidia_gpu'] and config['system'] == 'linux':
config['recommended_jax_variant'] = 'jax[cuda12]'
print(f"Detected NVIDIA GPU - will install jax[cuda12]")
elif config['amd_rocm_compatible'] and config['system'] == 'linux':
config['recommended_jax_variant'] = 'jax[rocm]'
print("Detected ROCm-compatible AMD GPU - will install jax[rocm]")
print("(!) AMD ROCm support in JAX is experimental - feedback appreciated!")
elif config['intel_is_arc']:
print("(!) Intel Arc GPU detected: unfortunately the experimental "
"jax plugin for Intel GPUs has dependency clashes and "
"still requires numpy 1.x therefore only the CPU variant "
"can be installed. Hopefully this will change in the "
"future.")
config['recommended_jax_variant'] = 'jax[cpu]'
elif config['apple_silicon']:
config['recommended_jax_variant'] = 'jax-metal'
print("Detected Apple Silicon - will install jax-metal")
print("(!) jax Metal support on macOS is still experimental")
# AMD or Intel iGPU - fall back to CPU
elif config['has_amd_gpu'] or config['has_intel_gpu']:
print(f"(!) GPU detected but not suitable for JAX acceleration - falling back to CPU")
config['recommended_jax_variant'] = 'jax[cpu]'
# Default to CPU
else:
config['recommended_jax_variant'] = 'jax[cpu]'
# Warn about CPU-only performance
if config['recommended_jax_variant'] in ['jax[cpu]', 'jax']:
print("(!) Warning: performance of the CPU-only jax variant "
"is slow and is intended for development use only. It "
"is generally recommended that you do NOT enable jax "
"optimisation in scripts that offer it!")
return config
def install_jax(self, force_variant: Optional[str] = None,
version_constraint: Optional[str] = None,
force_reinstall: Optional[bool] = False) -> bool:
"""
Install JAX with the appropriate variant for the detected hardware. Use this instead of
ensure_installed() to ensure that jax is installed correctly for the given hardware / OS
Args:
force_variant: Override auto-detection with specific variant (e.g., 'jax[cpu]')
version_constraint: Version constraint string (e.g., '>=0.4.0')
force_reinstall: Forces a reinstallation
Returns:
bool: True if installation succeeded, False otherwise
"""
if self.is_jax_installed():
print("Jax is already installed.")
return
if not self.detected_config:
self._detect_hardware_config()
variant = force_variant or self.detected_config['recommended_jax_variant']
try:
print(f"Installing {variant}...")
# Use the provided install_package function
_install_package(
package_name=variant,
version_constraint=version_constraint,
from_url=self.detected_config.get('install_url'),
index_url=self.detected_config.get('index_url'),
reinstall = force_reinstall
)
self.jax_installed = self.is_jax_installed()
print(f"Successfully installed {variant}")
return True
except Exception as e:
print(f"Failed to install {variant}: {e}")
return False
def ensure_jax(self) -> bool:
"""
Wrapper for install_jax() that only installs it if needed, with
negligible overhead if it is already installed.
"""
if not self.is_jax_installed():
return self.install_jax()
return True
def test_jax(self) -> Tuple[bool, Optional[str]]:
"""
Test JAX functionality and return execution provider.
Returns:
Tuple[bool,str]: the bool returned is True if jax works or False if
it does not, and the str is "gpu" if JAX is using GPU, "cpu" if
using CPU or None if
Raises:
RuntimeError: If JAX computation fails or accuracy check fails
ImportError: If JAX is not installed
"""
try:
import jax
import jax.numpy as jnp
except ImportError as e:
raise ImportError(f"JAX is not installed or not importable: {e}") from e
try:
# Get the default device
default_device = jax.devices()[0]
# Create test data with fixed seed for reproducible results
np.random.seed(42)
test_data_np = np.random.randn(100, 100).astype(np.float32)
# Define a JIT-compiled function to test compilation
@jax.jit
def test_computation_jax(x):
# Test matrix operations that benefit from GPU acceleration
y = jnp.dot(x, x.T)
z = jnp.sin(y) + jnp.cos(y)
return jnp.sum(z)
# Define equivalent numpy computation for cross-check
def test_computation_numpy(x):
y = np.dot(x, x.T)
z = np.sin(y) + np.cos(y)
return np.sum(z)
# Convert to JAX array
test_data_jax = jnp.array(test_data_np)
# Execute JAX computation (JIT-compiled)
jax_result = test_computation_jax(test_data_jax)
jax_result.block_until_ready()
# Execute numpy computation for cross-check
numpy_result = test_computation_numpy(test_data_np)
# Cross-check accuracy with appropriate tolerance
# Using rtol=1e-5, atol=1e-6 for float32 precision
if not np.allclose(jax_result, numpy_result, rtol=1e-5, atol=1e-6):
raise RuntimeError(f"JAX result {jax_result} does not match numpy result {numpy_result} within tolerance")
print("(OK) Accuracy cross-check between jax.numpy and numpy succeeded")
# Check the platform of the device that was actually used
if default_device.platform.lower() == 'gpu':
return True, "gpu"
return True, "cpu"
except Exception as e:
# If main test fails, try fallback to basic CPU operations
if "does not match numpy" in str(e):
# Re-raise accuracy errors immediately
return False, None
try:
# Try basic CPU operations with accuracy check
test_array_np = np.array([1.0, 2.0, 3.0], dtype=np.float32)
test_array_jax = jnp.array(test_array_np)
jax_sum = jnp.sum(test_array_jax)
jax_sum.block_until_ready()
numpy_sum = np.sum(test_array_np)
if not np.allclose(jax_sum, numpy_sum, rtol=1e-7, atol=1e-8):
print(f"(x) Basic JAX operation failed accuracy check: {jax_sum} vs {numpy_sum}. Error: {e}")
return False, None
print("(OK) Jax available using CPU only. This is likely to perform less "
"well than numpy but support is hoped to improve in the future.")
return True, "cpu"
except Exception:
# If even CPU fails, something is seriously wrong
print("Jax test failed: {e}")
return False, None
def _get_jax_info(self) -> Dict[str, Any]:
"""
Get information about the current JAX installation.
Returns:
Dict containing JAX version, devices, and backend info
"""
try:
import jax
return {
'version': jax.__version__,
'devices': [str(device) for device in jax.devices()],
'default_backend': jax.default_backend(),
'available_backends': list(jax.lib.xla_bridge.get_backend_names())
}
except ImportError:
return {'error': 'JAX not installed'}
except Exception as e:
return {'error': f'Error getting JAX info: {e}'}
def uninstall_jax(self, dry_run: bool = False) -> Dict[str, Any]:
"""
Detect and uninstall any existing JAX-related packages.
This is useful when you need to clean up a problematic JAX installation
before installing a different variant (e.g., falling back from GPU to CPU).
Args:
dry_run: If True, only detect packages without uninstalling them
Returns:
Dict containing information about detected and uninstalled packages
"""
self.jax_installed = False
self.detected_config = None
results = {
'detected_packages': [],
'uninstalled_packages': [],
'errors': [],
'dry_run': dry_run
}
# Common JAX-related package patterns
jax_packages = [
'jax',
'jaxlib',
'jax-cuda',
'jax-cuda11-local',
'jax-cuda12-local',
'jax-rocm',
'jax-metal',
'intel-extension-for-openxla'
]
try:
# Get list of installed packages
result = subprocess.run(
[sys.executable, '-m', 'pip', 'list', '--format=freeze'],
capture_output=True,
text=True,
check=True
)
installed_packages = result.stdout.strip().split('\n')
installed_dict = {}
for package_line in installed_packages:
if '==' in package_line:
name, version = package_line.split('==', 1)
installed_dict[name.lower()] = version
# Find JAX-related packages
detected = []
for pkg_name in jax_packages:
if pkg_name.lower() in installed_dict:
detected.append({
'name': pkg_name,
'version': installed_dict[pkg_name.lower()],
'installed_name': pkg_name.lower()
})
# Also check for packages that start with 'jax'
installed_names = [p['installed_name'] for p in detected]
for installed_name, version in installed_dict.items():
if installed_name.startswith('jax') and installed_name not in installed_names:
detected.append({
'name': installed_name,
'version': version,
'installed_name': installed_name
})
results['detected_packages'] = detected
if detected:
print(f"Found {len(detected)} JAX-related packages:")
for pkg in detected:
print(f" - {pkg['name']}=={pkg['version']}")
else:
print("No JAX-related packages found.")
return results
# Uninstall packages if not dry run
if not dry_run and detected:
print("Uninstalling JAX packages...")
# Uninstall in reverse dependency order - start with main packages
uninstall_order = ['jax'] + [pkg['name'] for pkg in detected if pkg['name'] != 'jax']
for pkg_name in uninstall_order:
# Find the package info
pkg_info = next((p for p in detected if p['name'] == pkg_name), None)
if not pkg_info:
continue
try:
print(f"Uninstalling {pkg_name}...")
subprocess.run(
[sys.executable, '-m', 'pip', 'uninstall', pkg_name, '-y'],
check=True,
capture_output=True,
text=True
)
results['uninstalled_packages'].append(pkg_info)
print(f"Successfully uninstalled {pkg_name}")
except subprocess.CalledProcessError as e:
error_msg = f"Failed to uninstall {pkg_name}: {e}"
results['errors'].append(error_msg)
print(error_msg)
# Continue with other packages
# Reset installation status
self.jax_installed = False
self.detected_config = None
print(f"Uninstallation complete. Removed {len(results['uninstalled_packages'])} packages.")
elif detected:
print("Dry run - no packages were uninstalled.")
except subprocess.CalledProcessError as e:
error_msg = f"Error running pip list: {e}"
results['errors'].append(error_msg)
print(error_msg)
except Exception as e:
error_msg = f"Unexpected error during JAX detection/uninstallation: {e}"
results['errors'].append(error_msg)
print(error_msg)
return results
def __repr__(self) -> str:
return f"JaxHelper(system={self.system}, jax_installed={self.jax_installed})"
|