11# SPDX-FileCopyrightText: Copyright (c) <2025> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22#
33# SPDX-License-Identifier: Apache-2.0
4+ import importlib .metadata
45import inspect
56import math
67import re
@@ -321,6 +322,7 @@ class _CompilerBinary:
321322 path : str
322323 bin_path : str
323324 ld_path : str
325+ pass_cuda_home_var : bool
324326
325327 def run (self ,
326328 args : list [str ],
@@ -335,6 +337,9 @@ def run(self,
335337 env = os .environ .copy ()
336338 env ['LD_LIBRARY_PATH' ] = self .ld_path
337339 env ['PATH' ] = self .bin_path
340+ if not self .pass_cuda_home_var :
341+ for key in {"CUDA_HOME" , "CUDA_PATH" }:
342+ env .pop (key , None )
338343 subprocess .run (command + flags , env = env , check = True , capture_output = True ,
339344 timeout = timeout_sec )
340345 except subprocess .CalledProcessError as e :
@@ -347,6 +352,54 @@ def run(self,
347352 _try_get_compiler_version (self .path ))
348353
349354
355+ _PIP_TILEIRAS_PACKAGES = (
356+ "nvidia-cuda-tileiras" ,
357+ "nvidia-cuda-nvcc" ,
358+ "nvidia-nvvm" ,
359+ )
360+
361+
362+ def _get_major_minor (version_str : str ) -> tuple [int , int ]:
363+ parts = version_str .split ("." )
364+ return int (parts [0 ]), int (parts [1 ])
365+
366+
367+ def _find_pip_tileiras () -> Optional [str ]:
368+ versions : dict [str , str ] = {}
369+ for pkg in _PIP_TILEIRAS_PACKAGES :
370+ try :
371+ versions [pkg ] = importlib .metadata .version (pkg )
372+ except importlib .metadata .PackageNotFoundError :
373+ return None
374+
375+ majors_minors = {pkg : _get_major_minor (v ) for pkg , v in versions .items ()}
376+ unique = set (majors_minors .values ())
377+ if len (unique ) != 1 :
378+ details = ", " .join (f"{ pkg } { versions [pkg ]} " for pkg in _PIP_TILEIRAS_PACKAGES )
379+ warnings .warn (
380+ f"Installed NVIDIA pip packages have mismatched versions ({ details } ). "
381+ "Falling back to system tileiras." ,
382+ stacklevel = 3 ,
383+ )
384+ return None
385+
386+ try :
387+ import nvidia .cu13 as cu13_pkg
388+ cu13_root = cu13_pkg .__path__ [0 ]
389+ except (ImportError , AttributeError , IndexError ):
390+ logger .debug ("Fail to get nvidia.cu13 package path." , exc_info = True )
391+ return None
392+
393+ pip_bin_dir = os .path .join (cu13_root , "bin" )
394+ res = shutil .which ("tileiras" , path = pip_bin_dir )
395+ if res is None :
396+ logger .debug ("Fail to find tileiras under nvidia.cu13 path." )
397+ return None
398+
399+ logger .debug (f"Found tileiras from pip package: { res } " )
400+ return res
401+
402+
350403@cache
351404def _find_compiler_bin () -> _CompilerBinary :
352405 # search under cuda/tile/_deps
@@ -360,30 +413,39 @@ def _find_compiler_bin() -> _CompilerBinary:
360413 if (res := shutil .which ("tileiras" , path = deps_bin_dir )):
361414 bin_path = deps_bin_dir + ":" + bin_path
362415 ld_path = deps_lib_dir + ":" + ld_path
363- return _CompilerBinary (res , bin_path , ld_path )
416+ return _CompilerBinary (res , bin_path , ld_path , pass_cuda_home_var = False )
417+
418+ # search from nvidia-cuda-tileiras pip package
419+ logger .debug ("Searching tileiras from nvidia pip package" )
420+ res = _find_pip_tileiras ()
421+ if res is not None :
422+ return _CompilerBinary (res , bin_path , ld_path , pass_cuda_home_var = False )
364423
365424 # search under PATH
366425 logger .debug (f"Searching tileiras: { bin_path } " )
367426 if (res := shutil .which ("tileiras" )):
368- return _CompilerBinary (res , bin_path , ld_path )
427+ return _CompilerBinary (res , bin_path , ld_path , pass_cuda_home_var = True )
369428
370429 # search under CUDA_HOME
371430 if (cuda_home := _get_cuda_home ()):
372431 cuda_bin_path = os .path .join (cuda_home , 'bin' )
373432 logger .debug (f"Searching tileiras: { cuda_bin_path } " )
374433 if (res := shutil .which ("tileiras" , path = cuda_bin_path )):
375434 bin_path = bin_path + ":" + cuda_bin_path
376- return _CompilerBinary (res , bin_path , ld_path )
435+ return _CompilerBinary (res , bin_path , ld_path , pass_cuda_home_var = True )
377436
378437 # Try default CUDA Toolkit installation paths as a fallback
379438 res = _find_compiler_in_default_cuda_toolkit_paths ()
380439 if res is not None :
381440 tileiras_path , bin_path = res
382- return _CompilerBinary (tileiras_path , bin_path , ld_path )
441+ return _CompilerBinary (tileiras_path , bin_path , ld_path , pass_cuda_home_var = False )
383442
384443 cuda_home_var = "CUDA_PATH" if is_windows () else "CUDA_HOME"
385- raise FileNotFoundError (f"'tileiras' compiler not found, "
386- f"make sure it is available in $PATH or ${ cuda_home_var } /bin" )
444+ raise FileNotFoundError ("'tileiras' compiler not found, "
445+ "make sure it is available as a python package via "
446+ "`pip install cuda-tile[tileiras]` or "
447+ f"available in $PATH or ${ cuda_home_var } /bin via system CTK (13.1+)"
448+ " installation." )
387449
388450
389451@cache
0 commit comments