66from pathlib import Path
77import subprocess
88from typing import Any , Final
9+ import ctypes
10+ from triton import knobs
911
1012import torch
1113
1214from triton ._C .libtriton import llvm # pyright: ignore[reportMissingImports]
1315from triton ._C .libtriton .tle .llvm import parse_llvm_ir # pyright: ignore[reportMissingImports]
1416from triton .experimental .tle .raw .source_store import register_source
17+ from triton .experimental .tle .raw .nvshmem .utils import get_nvshmem_home
1518
1619# TODO: We use cli tools to compile CUDA code temporarily, and plan to replace it with LLVM components Python bindings in the future.
1720CLANG = os .getenv ("CLANG" , "clang" )
1821CLANG_FLAGS = shlex .split (os .getenv ("CLANG_FLAGS" , "" ))
1922
23+ _cumodule_hook_installed = False
24+ _nvshmemx_cumodule_init = None
25+
2026
2127def _sanitize_clang_ir (ir : str ) -> str :
2228 # Newer clang emits attributes that this Triton branch's LLVM parser does
@@ -46,10 +52,43 @@ def _get_cuda_gpu_arch() -> str:
4652 return f"--cuda-gpu-arch=sm_{ major } { minor } "
4753
4854
55+ def _get_nvshmemx_cumodule_init ():
56+ global _nvshmemx_cumodule_init
57+ if _nvshmemx_cumodule_init is not None :
58+ return _nvshmemx_cumodule_init
59+
60+ nvshmem_home = get_nvshmem_home ()
61+ library = ctypes .CDLL (str (Path (nvshmem_home ) / "lib" / "libnvshmem_host.so" ))
62+ fn = library .nvshmemx_cumodule_init
63+ fn .argtypes = [ctypes .c_void_p ]
64+ fn .restype = ctypes .c_int
65+ _nvshmemx_cumodule_init = fn
66+ return fn
67+
68+
69+ def _install_cumodule_hook ():
70+ global _cumodule_hook_installed
71+ if _cumodule_hook_installed :
72+ return
73+
74+ def hook (* args , ** kwargs ):
75+ key = kwargs ["key" ]
76+ function = kwargs ["fn" ].jit_function
77+ device = kwargs ["compile" ]["device" ]
78+ kernel = function .device_caches [device ][0 ].get (key )
79+ assert kernel is not None
80+ kernel ._init_handles ()
81+ result = _get_nvshmemx_cumodule_init ()(ctypes .c_void_p (kernel .module ))
82+ assert result == 0 , f"nvshmemx_cumodule_init failed: { result } "
83+
84+ knobs .runtime .jit_post_compile_hook = hook
85+ _cumodule_hook_installed = True
86+
87+
4988class CUDAJITFunction (object ):
5089
5190 def __init__ (self , fn : Any , file : Path , * args , ** kwargs ) -> None :
52- super ().__init__ (* args , ** { k : v for k , v in kwargs . items () if k not in ( "extern_func_name" , "deferred" )} )
91+ super ().__init__ ()
5392 self .fn : Final [Any ] = fn
5493 self .code : Final [str ] = file .read_text ()
5594 self .region_dialect : Final [str ] = "cuda"
@@ -60,6 +99,9 @@ def __init__(self, fn: Any, file: Path, *args, **kwargs) -> None:
6099 self .deferred : Final [bool ] = kwargs .get ("deferred" , False )
61100 self .__triton_builtin__ : Final [bool ] = True
62101
102+ if "nvshmem" in self .code :
103+ _install_cumodule_hook ()
104+
63105 def register_pending_source (self , * , hint : str = "" ) -> str :
64106 if not self .extern_func_name :
65107 raise RuntimeError ("deferred tle_raw CUDA source requires extern_func_name= "
0 commit comments