56
56
rocm_compile_command ,
57
57
rocm_compiler ,
58
58
)
59
- from torch ._inductor .cpu_vec_isa import (
59
+
60
+ """
61
+ codecache.py, cpp_builder.py and cpu_vec_isa.py import rule:
62
+ https://github.com/pytorch/pytorch/issues/124245#issuecomment-2197778902
63
+ """
64
+ from torch ._inductor .cpp_builder import (
65
+ _set_gpu_runtime_env ,
66
+ _transform_cuda_paths ,
67
+ CppBuilder ,
68
+ CppOptions ,
69
+ CppTorchCudaOptions ,
60
70
get_compiler_version_info ,
61
- invalid_vec_isa ,
62
- pick_vec_isa ,
63
- VecISA ,
64
71
)
72
+ from torch ._inductor .cpu_vec_isa import invalid_vec_isa , pick_vec_isa , VecISA
65
73
from torch ._inductor .runtime .compile_tasks import (
66
74
_module_to_triton_kernel ,
67
75
_reload_python_module ,
@@ -1178,99 +1186,23 @@ def __call__(self, inputs: List[Any]) -> Any:
1178
1186
return self .current_callable (inputs )
1179
1187
1180
1188
1181
- def cpp_compiler () -> str :
1182
- if config .is_fbcode ():
1183
- return build_paths .cc () if torch .version .hip is None else build_paths .clang ()
1184
- if isinstance (config .cpp .cxx , (list , tuple )):
1185
- search = tuple (config .cpp .cxx )
1186
- else :
1187
- search = (config .cpp .cxx ,)
1188
- return cpp_compiler_search (search )
1189
-
1190
-
1191
- @functools .lru_cache (1 )
1192
- def cpp_compiler_search (search : str ) -> str :
1193
- for cxx in search :
1194
- try :
1195
- if cxx is None :
1196
- # gxx package is only available for Linux
1197
- # according to https://anaconda.org/conda-forge/gxx/
1198
- if sys .platform != "linux" :
1199
- continue
1200
- # Do not install GXX by default
1201
- if not os .getenv ("TORCH_INDUCTOR_INSTALL_GXX" ):
1202
- continue
1203
- from filelock import FileLock
1204
-
1205
- lock_dir = get_lock_dir ()
1206
- lock = FileLock (
1207
- os .path .join (lock_dir , "g++.lock" ), timeout = LOCK_TIMEOUT
1208
- )
1209
- with lock :
1210
- cxx = install_gcc_via_conda ()
1211
- subprocess .check_output ([cxx , "--version" ])
1212
- return cxx
1213
- except (subprocess .SubprocessError , FileNotFoundError , ImportError ):
1214
- continue
1215
- raise exc .InvalidCxxCompiler
1216
-
1217
-
1218
- def install_gcc_via_conda () -> str :
1219
- """On older systems, this is a quick way to get a modern compiler"""
1220
- prefix = os .path .join (cache_dir (), "gcc" )
1221
- cxx_path = os .path .join (prefix , "bin" , "g++" )
1222
- if not os .path .exists (cxx_path ):
1223
- log .info ("Downloading GCC via conda" )
1224
- conda = os .environ .get ("CONDA_EXE" , "conda" )
1225
- if conda is None :
1226
- conda = shutil .which ("conda" )
1227
- if conda is not None :
1228
- subprocess .check_call (
1229
- [
1230
- conda ,
1231
- "create" ,
1232
- f"--prefix={ prefix } " ,
1233
- "--channel=conda-forge" ,
1234
- "--quiet" ,
1235
- "-y" ,
1236
- "python=3.8" ,
1237
- "gxx" ,
1238
- ],
1239
- stdout = subprocess .PIPE ,
1240
- )
1241
- return cxx_path
1242
-
1243
-
1244
- def is_gcc () -> bool :
1245
- if sys .platform == "darwin" and is_apple_clang ():
1246
- return False
1247
- return bool (re .search (r"(gcc|g\+\+)" , cpp_compiler ()))
1248
-
1249
-
1250
- @functools .lru_cache (None )
1251
- def is_apple_clang () -> bool :
1252
- cxx = cpp_compiler ()
1253
- version_string = subprocess .check_output ([cxx , "--version" ]).decode ("utf8" )
1254
- return "Apple" in version_string .splitlines ()[0 ]
1255
-
1256
-
1257
- def is_clang () -> bool :
1258
- # Mac OS apple clang maybe named as gcc, need check compiler info.
1259
- if sys .platform == "darwin" :
1260
- return is_apple_clang ()
1261
- return bool (re .search (r"(clang|clang\+\+)" , cpp_compiler ()))
<
F987
code> 1189
+ """
1190
+ TODO: will remove old cpp builder when we switch to the new one.
1191
+ """
1262
1192
1263
1193
1264
1194
def get_compile_only (compile_only : bool = True ) -> str :
1265
1195
return "-c" if compile_only else ""
1266
1196
1267
1197
1268
1198
def get_shared (shared : bool = True , compile_only : bool = False ) -> str :
1199
+ from .cpp_builder import get_cpp_compiler
1200
+
1269
1201
if not shared :
1270
1202
return ""
1271
1203
if compile_only :
1272
1204
return "-fPIC"
1273
- if platform .system () == "Darwin" and "clang" in cpp_compiler ():
1205
+ if platform .system () == "Darwin" and "clang" in get_cpp_compiler ():
1274
1206
# This causes undefined symbols to behave the same as linux
1275
1207
return "-shared -fPIC -undefined dynamic_lookup"
1276
1208
else :
@@ -1286,6 +1218,8 @@ def get_glibcxx_abi_build_flags() -> str:
1286
1218
1287
1219
1288
1220
def cpp_flags () -> str :
1221
+ from .cpp_builder import is_clang
1222
+
1289
1223
flags = ["-std=c++17" , "-Wno-unused-variable" , "-Wno-unknown-pragmas" ]
1290
1224
if is_clang ():
1291
1225
flags .append ("-Werror=ignored-optimization-argument" )
@@ -1356,82 +1290,19 @@ def use_standard_sys_dir_headers() -> str:
1356
1290
return ""
1357
1291
1358
1292
1359
- @functools .lru_cache (None )
1360
- def is_conda_llvm_openmp_installed () -> bool :
1361
- try :
1362
- command = "conda list llvm-openmp --json"
1363
- output = subprocess .check_output (command .split ()).decode ("utf8" )
1364
- return len (json .loads (output )) > 0
1365
- except subprocess .SubprocessError :
1366
- return False
1367
-
1368
-
1369
- @functools .lru_cache (None )
1370
- def homebrew_libomp () -> Tuple [bool , str ]:
1371
- try :
1372
- # check if `brew` is installed
1373
- subprocess .check_output (["which" , "brew" ])
1374
- # get the location of `libomp` if it is installed
1375
- # this is the location that `libomp` **would** be installed
1376
- # see https://github.com/Homebrew/brew/issues/10261#issuecomment-756563567 for details
1377
- libomp_path = (
1378
- subprocess .check_output (["brew" , "--prefix" , "libomp" ])
1379
- .decode ("utf8" )
1380
- .strip ()
1381
- )
1382
- # check if `libomp` is installed
1383
- omp_available = os .path .exists (libomp_path )
1384
- return omp_available , libomp_path
1385
- except subprocess .SubprocessError :
1386
- return False , ""
1387
-
1388
-
1389
- def _set_gpu_runtime_env () -> None :
1390
- if (
1391
- config .is_fbcode ()
1392
- and torch .version .hip is None
1393
- and "CUDA_HOME" not in os .environ
1394
- and "CUDA_PATH" not in os .environ
1395
- ):
1396
- os .environ ["CUDA_HOME" ] = build_paths .cuda ()
1397
-
1398
-
1399
- def _get_python_include_dirs ():
1400
- include_dir = Path (sysconfig .get_path ("include" ))
1401
- # On Darwin Python executable from a framework can return
1402
- # non-existing /Library/Python/... include path, in which case
1403
- # one should use Headers folder from the framework
1404
- if not include_dir .exists () and platform .system () == "Darwin" :
1405
- std_lib = Path (sysconfig .get_path ("stdlib" ))
1406
- include_dir = (std_lib .parent .parent / "Headers" ).absolute ()
1407
- if not (include_dir / "Python.h" ).exists ():
1408
- warnings .warn (f"Can't find Python.h in { str (include_dir )} " )
1409
- return [str (include_dir )]
1410
-
1411
-
1412
- def _transform_cuda_paths (lpaths ):
1413
- # This handles two cases:
1414
- # 1. Meta internal cuda-12 where libs are in lib/cuda-12 and lib/cuda-12/stubs
1415
- # 2. Linux machines may have CUDA installed under either lib64/ or lib/
1416
- for i , path in enumerate (lpaths ):
1417
- if (
1418
- "CUDA_HOME" in os .environ
1419
- and path .startswith (os .environ ["CUDA_HOME" ])
1420
- and not os .path .exists (f"{ path } /libcudart_static.a" )
1421
- ):
1422
- for root , dirs , files in os .walk (path ):
1423
- if "libcudart_static.a" in files :
1424
- lpaths [i ] = os .path .join (path , root )
1425
- lpaths .append (os .path .join (lpaths [i ], "stubs" ))
1426
- break
1427
-
1428
-
1429
1293
def get_include_and_linking_paths (
1430
1294
include_pytorch : bool = False ,
1431
1295
vec_isa : VecISA = invalid_vec_isa ,
1432
1296
cuda : bool = False ,
1433
1297
aot_mode : bool = False ,
1434
1298
) -> Tuple [List [str ], str , str , str , str ]:
1299
+ from .cpp_builder import (
1300
+ _get_python_include_dirs ,
1301
+ homebrew_libomp ,
1302
+ is_apple_clang ,
1303
+ is_conda_llvm_openmp_installed ,
1304
+ )
1305
+
1435
1306
_set_gpu_runtime_env ()
1436
1307
from torch .utils import cpp_extension
1437
1308
@@ -1616,6 +1487,8 @@ def cpp_compile_command(
1616
1487
use_mmap_weights : bool = False ,
1617
1488
extra_flags : Sequence [str ] = (),
1618
1489
) -> str :
1490
+ from .cpp_builder import get_cpp_compiler , is_clang
1491
+
1619
1492
ipaths , lpaths , libs , macros , build_arch_flags = get_include_and_linking_paths (
1620
1493
include_pytorch , vec_isa , cuda , aot_mode
1621
1494
)
@@ -1654,7 +1527,7 @@ def cpp_compile_command(
1654
1527
r"[ \n]+" ,
1655
1528
" " ,
1656
1529
f"""
1657
- { cpp_compiler ()} { inp_name_str } { get_shared (shared , compile_only )}
1530
+ { get_cpp_compiler ()} { inp_name_str } { get_shared (shared , compile_only )}
1658
1531
{ get_warning_all_flag (warning_all )} { cpp_flags ()}
1659
1532
{ get_glibcxx_abi_build_flags ()}
1660
1533
{ ipaths_str } { lpaths } { libs } { build_arch_flags }
@@ -1726,6 +1599,8 @@ def compile(
1726
1599
serialized_extern_kernel_nodes : Optional [str ],
1727
1600
cuda : bool ,
1728
1601
) -> str :
1602
+ from .cpp_builder import get_cpp_compiler
1603
+
1729
1604
picked_vec_isa = pick_vec_isa ()
1730
1605
cpp_command = repr (
1731
1606
cpp_compile_command (
@@ -1870,7 +1745,7 @@ def _compile_consts_darwin(consts: bytes) -> str:
1870
1745
specified_dir = specified_output_path ,
1871
1746
)
1872
1747
consts_o = os .path .splitext (consts_path )[0 ] + ".o"
1873
- cmd = f"{ cpp_compiler ()} -c -o { consts_o } { consts_path } "
1748
+ cmd = f"{ get_cpp_compiler ()} -c -o { consts_o } { consts_path } "
1874
1749
run_command_and_check (cmd )
1875
1750
if is_large_consts :
1876
1751
with open (consts_o , "r+b" ) as f :
@@ -2520,8 +2395,6 @@ def _do_validate_cpp_commands(
2520
2395
extra_flags = extra_flags ,
2521
2396
).split (" " )
2522
2397
2523
- from torch ._inductor .cpp_builder import CppBuilder , CppTorchCudaOptions
2524
-
2525
2398
dummy_build_option = CppTorchCudaOptions (
2526
2399
vec_isa = picked_isa ,
2527
2400
include_pytorch = include_pytorch ,
@@ -2718,8 +2591,6 @@ def _codegen_glue(cls, meta, headerfile):
2718
2591
@classmethod
2719
2592
@functools .lru_cache (None )
2720
2593
def config_hash (cls ):
2721
- from torch ._inductor .cpp_builder import CppBuilder , CppOptions
2722
-
2723
2594
command_gen = CppBuilder (
2724
2595
name = "O" ,
2725
2596
sources = "I" ,
0 commit comments