o
    nhO                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ ejejeZejedgZdd	 Ze  d
d Zdd ZG dd deZdd Zdd ZG dd deZG dd deZdS )    N)Path)_build)get_cache_manager)	GPUTarget)	GPUDriverincludec                    s   dd l }| dkrd S dd lddlm}m}m}mm m} G  fdddj	}
|||||||}zdj}W n
 tyL   Y d S w | g|_||_dd }	fd	d
}
|||
|	rut|	S d S )Nr   Linux)c_charc_intc_size_tc_void_pc_char_pPOINTERc                       s   e Zd Zdfd fgZdS )z8_find_already_mmapped_dylib_on_linux.<locals>.DlPhdrInfo	dlpi_addr	dlpi_nameN)__name__
__module____qualname___fields_ )r   r   r   n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/triton/backends/amd/driver.py
DlPhdrInfo   s    r   z	libc.so.6i      c                    s@   | j j}tt|}|jv r ||tt| dS dS )Nr   r   )	contentsr   r   osfsdecodenamememmoveminlen)infosizedatar   p)ctypeslib_namemax_path_lengthr   r   callback2   s   
z6_find_already_mmapped_dylib_on_linux.<locals>.callback)platformsystemr$   r	   r
   r   r   r   r   	Structure	CFUNCTYPECDLLdl_iterate_phdr	Exceptionargtypesrestypecreate_string_bufferr   r   	string_at)r%   r(   r	   r
   r   r   r   
callback_tr-   pathr'   r   )r   r   r$   r%   r&   r   $_find_already_mmapped_dylib_on_linux   s(    
	r5   c                     s  d t d} | r |  rt j| r| S td|  d  t }|r8t j|r.|S td| d  g }dd l}| }|	 }|j
rN|g| }|D ]}t j|dd	 }t j|re|  S || qPt d
}|r|dD ]}t j| }	t j|	r|	  S ||	 qwtddg }
 fdd|
 D }|D ]}t j|r|  S || qt jd }t j|r|S || td  d| )Nzlibamdhip64.soTRITON_LIBHIP_PATHzTRITON_LIBHIP_PATH 'z' does not point to a valid zmemory mapped 'z'' in process does not point to a valid r   torchlibLD_LIBRARY_PATH:z/sbin/ldconfigz-pc                    s&   g | ]}|   r| d  qS ))stripendswithsplit).0liner%   r   r   
<listcomp>p   s   & z2_get_path_to_hip_runtime_dylib.<locals>.<listcomp>z/opt/rocm/lib/zcannot locate z after attempted paths )r   getenvr=   r4   existsRuntimeErrorr5   sitegetsitepackagesgetusersitepackagesENABLE_USER_SITEjoinappendr>   
subprocesscheck_outputdecode
splitlines)env_libhip_pathmmapped_pathpathsrF   site_packages	user_siter4   env_ld_library_pathdflibslocsloccommon_install_pathr   rA   r   _get_path_to_hip_runtime_dylib@   sP   



r\   c              	   C   s&  t | d }t|}|| d}|d u rzt T}tj	
|d}t|d}||  W d    n1 s;w   Y  t|||g tg }t|d}|j| | ddd}W d    n1 sfw   Y  W d    n1 suw   Y  dd l}	|	j||}
|	j|
}|
j| |S )	Nzutf-8z.sozmain.cwrbT)binaryr   )hashlibsha256encode	hexdigestr   get_filetempfileTemporaryDirectoryr   r4   rJ   openwriter   include_dirputreadimportlib.utilutilspec_from_file_locationmodule_from_specloaderexec_module)srcr   keycache
cache_pathtmpdirsrc_pathrW   so	importlibspecmodr   r   r   compile_module_from_src   s(   
r|   c                       s$   e Zd Z fddZdd Z  ZS )HIPUtilsc                    s"   t | dstt| | | _| jS )Ninstance)hasattrsuperr}   __new__r~   )cls	__class__r   r   r      s   
zHIPUtils.__new__c                 C   sH   t  }ttjtd }|d|d}t|d}|j	| _	|j
| _
d S )Nzdriver.cz/*py_libhip_search_path*/r   	hip_utils)r\   r   r   r4   rJ   dirname	read_textreplacer|   load_binaryget_device_properties)selflibhip_pathrr   r{   r   r   r   __init__   s   
zHIPUtils.__init__)r   r   r   r   r   __classcell__r   r   r   r   r}      s    r}   c                 C   s8   | d dkrdS ddddddd	d
dddddddd|  S )Nr   *hipDeviceptr_tint32_tint8_tint16_tint64_tuint32_tuint8_tuint16_tuint64_tfloatdouble)i1i8i16i32i64u1u8u16u32u64fp16bf16fp32f32fp64r   )tyr   r   r   	ty_to_cpp   s(   r   c                    s  fdd fdd fddd fdd	| D }d
| }d t| }ttt|d}dd t|D }t|dkrUdd dd |	 D  nd}d dd |	 D }g }|	 D ] \}}	|	d dkr}|
d| d qi|	dkr|
d|  qit }
ttt|}dd	 |	 D }|
d d|
 dt|dkrd| nd dd | d| d| dd   fd!d	|	 D  d"| d#| d$d% d&d	 |	 D  d't|dkrdd | nd d(}|S ))Nc                    s   t | trdt | S | S )N,)
isinstancetuplerJ   map)sig)_serialize_signaturer   r   r      s   
z+make_launcher.<locals>._serialize_signaturec                    sJ   t | trdt | }d| dS | d dkrdS | dv r!dS t| S )Nr   []r   r   z	PyObject*	constexprr   r   rJ   r   r   r   val_extracted_typer   r   r      s   
z&make_launcher.<locals>._extracted_typec                    sf   t | trdt | }d| dS | d dkrdS | dv r!dS dd	d
dddddddddt|  S )N ()r   r   Or   rW   rV   lbhiLBHIK)r   r   longr   r   r   r   r   r   r   r   r   r   	format_ofr   r   r      s*   
z make_launcher.<locals>.format_ofr   c                    s   g | ]} |qS r   r   )r?   r   r   r   r   rB      s    z!make_launcher.<locals>.<listcomp>
piiiKKOOOOr   c                 S      i | ]\}}||qS r   r   )r?   r   sr   r   r   
<dictcomp>       z!make_launcher.<locals>.<dictcomp>r   , c                 s   s    | ]
\}}d | V  qdS )z&_argNr   r?   r   r   r   r   r   	<genexpr>   s    z make_launcher.<locals>.<genexpr>c                 s   s.    | ]\}}|d krt | d| V  qdS )r   z argN)r   r   r   r   r   r      s   , r   ptr_infoz.dev_ptrr   _argc                 S   s"   g | ]\}}|d krd| qS )r   z&argr   r   r   r   r   rB      s   " z&global_scratcha;  
#define __HIP_PLATFORM_AMD__
#include <hip/hip_runtime.h>
#include <Python.h>
#include <dlfcn.h>
#include <stdbool.h>
#include <dlfcn.h>

// The list of paths to search for the HIP runtime library. The caller Python
// code should substitute the search path placeholder.
static const char *hipLibSearchPaths[] = {"a  "};

// The list of HIP dynamic library symbols and their signature we are interested
// in this file.
#define HIP_SYMBOL_LIST(FOR_EACH_ERR_FN, FOR_EACH_STR_FN)                     \
  FOR_EACH_STR_FN(hipGetErrorString, hipError_t hipError)                     \
  FOR_EACH_ERR_FN(hipModuleLaunchKernel, hipFunction_t f,                     \
                  unsigned int gridDimX, unsigned int gridDimY,               \
                  unsigned int gridDimZ, unsigned int blockDimX,              \
                  unsigned int blockDimY, unsigned int blockDimZ,             \
                  unsigned int sharedMemBytes, hipStream_t stream,            \
                  void **kernelParams, void **extra)                          \
  FOR_EACH_ERR_FN(hipModuleLaunchCooperativeKernel, hipFunction_t f,          \
                  unsigned int gridDimX, unsigned int gridDimY,               \
                  unsigned int gridDimZ, unsigned int blockDimX,              \
                  unsigned int blockDimY, unsigned int blockDimZ,             \
                  unsigned int sharedMemBytes, hipStream_t stream,            \
                  void **kernelParams, void **extra)                          \
  FOR_EACH_ERR_FN(hipPointerGetAttribute, void *data,                         \
                  hipPointer_attribute attribute, hipDeviceptr_t ptr)

// The HIP symbol table for holding resolved dynamic library symbols.
struct HIPSymbolTable {
#define DEFINE_EACH_ERR_FIELD(hipSymbolName, ...)                             \
  hipError_t (*hipSymbolName)(__VA_ARGS__);
#define DEFINE_EACH_STR_FIELD(hipSymbolName, ...)                             \
  const char *(*hipSymbolName)(__VA_ARGS__);

  HIP_SYMBOL_LIST(DEFINE_EACH_ERR_FIELD, DEFINE_EACH_STR_FIELD)
};

static struct HIPSymbolTable hipSymbolTable;

bool initSymbolTable() {
  // Use the HIP runtime library loaded into the existing process if it exits.
  void *lib = dlopen("libamdhip64.so", RTLD_NOLOAD);
  if (lib) {
    // printf("[triton] chosen loaded libamdhip64.so in the process\n");
  }

  // Otherwise, go through the list of search paths to dlopen the first HIP
  // driver library.
  if (!lib) {
    int n = sizeof(hipLibSearchPaths) / sizeof(hipLibSearchPaths[0]);
    for (int i = 0; i < n; ++i) {
      void *handle = dlopen(hipLibSearchPaths[i], RTLD_LAZY | RTLD_LOCAL);
      if (handle) {
        lib = handle;
        // printf("[triton] chosen %s\n", hipLibSearchPaths[i]);
      }
    }
  }
  if (!lib) {
    PyErr_SetString(PyExc_RuntimeError, "cannot open libamdhip64.so");
    return false;
  }

  // Resolve all symbols we are interested in.
  dlerror(); // Clear existing errors
  const char *error = NULL;
#define QUERY_EACH_FN(hipSymbolName, ...)                                     \
  *(void **)&hipSymbolTable.hipSymbolName = dlsym(lib, #hipSymbolName);       \
  error = dlerror();                                                          \
  if (error) {                                                               \
    PyErr_SetString(PyExc_RuntimeError,                                       \
                    "cannot query " #hipSymbolName " from libamdhip64.so");   \
    dlclose(lib);                                                             \
    return false;                                                             \
  }

  HIP_SYMBOL_LIST(QUERY_EACH_FN, QUERY_EACH_FN)

  return true;
}

static inline void gpuAssert(hipError_t code, const char *file, int line)
{
   if (code != HIP_SUCCESS)
   {
      const char* prefix = "Triton Error [HIP]: ";
       const char* str = hipSymbolTable.hipGetErrorString(code);
      char err[1024] = {0};
      snprintf(err, 1024, "%s Code: %d, Messsage: %s", prefix, code, str );
      PyErr_SetString(PyExc_RuntimeError, err);
   }
}

#define HIP_CHECK(ans) { gpuAssert((ans), __FILE__, __LINE__); }

static void _launch(int gridX, int gridY, int gridZ, int num_warps, int num_ctas, int launch_cooperative_grid, int clusterDimX, int clusterDimY, int clusterDimZ, int shared_memory, hipStream_t stream, hipFunction_t functionzc) {
  // printf("_launch hip kernel\n");
  hipDeviceptr_t global_scratch = 0;
  void *params[] = { z };
  if (gridX*gridY*gridZ > 0 && launch_cooperative_grid) {
    HIP_CHECK(hipSymbolTable.hipModuleLaunchCooperativeKernel(function, gridX, gridY, gridZ, z*num_warps, 1, 1, shared_memory, stream, params, 0));
    return;
  }
  if (gridX*gridY*gridZ > 0) {
    HIP_CHECK(hipSymbolTable.hipModuleLaunchKernel(function, gridX, gridY, gridZ, a  *num_warps, 1, 1, shared_memory, stream, params, 0));
  }
}

typedef struct _DevicePtrInfo {
    hipDeviceptr_t dev_ptr;
    bool valid;
} DevicePtrInfo;

static inline DevicePtrInfo getPointer(PyObject *obj, int idx) {
  DevicePtrInfo ptr_info;
  ptr_info.dev_ptr = 0;
  ptr_info.valid = true;
  if (PyLong_Check(obj)) {
    ptr_info.dev_ptr = (hipDeviceptr_t)PyLong_AsUnsignedLongLong(obj);
    return ptr_info;
  }
  if (obj == Py_None) {
    // valid nullptr
    return ptr_info;
  }
  PyObject *ptr = PyObject_GetAttrString(obj, "data_ptr");
  if(ptr){
    PyObject *empty_tuple = PyTuple_New(0);
    PyObject *ret = PyObject_Call(ptr, empty_tuple, NULL);
    Py_DECREF(empty_tuple);
    Py_DECREF(ptr);
    if (!PyLong_Check(ret)) {
      PyErr_SetString(PyExc_TypeError, "data_ptr method of Pointer object must return 64-bit int");
      ptr_info.valid = false;
      return ptr_info;
    }
    ptr_info.dev_ptr = (hipDeviceptr_t)PyLong_AsUnsignedLongLong(ret);
    if(!ptr_info.dev_ptr)
      return ptr_info;
    uint64_t dev_ptr;
    hipError_t status = hipSymbolTable.hipPointerGetAttribute(&dev_ptr, HIP_POINTER_ATTRIBUTE_DEVICE_POINTER, ptr_info.dev_ptr);
    if (status == hipErrorInvalidValue) {
        PyErr_Format(PyExc_ValueError,
                     "Pointer argument (at %d) cannot be accessed from Triton (cpu tensor?)", idx);
        ptr_info.valid = false;
    }
    ptr_info.dev_ptr = (hipDeviceptr_t)dev_ptr;
    Py_DECREF(ret);
    return ptr_info;
  }
  PyErr_SetString(PyExc_TypeError, "Pointer argument must be either uint64 or have data_ptr method");
  return ptr_info;
}

static PyObject* launch(PyObject* self, PyObject* args) {
   // printf("launch\n");
  int gridX, gridY, gridZ;
  uint64_t _stream;
  uint64_t _function;
  int launch_cooperative_grid;
  PyObject *launch_enter_hook = NULL;
  PyObject *launch_exit_hook = NULL;
  PyObject *kernel_metadata = NULL;
  PyObject *launch_metadata = NULL;
   c                    s$   g | ]\}} | d | dqS )z _arg; r   r   r   r   r   rB     s   $ z
  if(!PyArg_ParseTuple(args, "a  ", &launch_cooperative_grid,
                                           &gridX, &gridY, &gridZ, &_stream, &_function,
                                           &kernel_metadata, &launch_metadata,
                                           &launch_enter_hook, &launch_exit_hook a=  )) {
    return NULL;
  }

  // extract kernel metadata
  int num_warps, num_ctas, shared_memory, clusterDimX, clusterDimY, clusterDimZ;
  if (!PyArg_ParseTuple(kernel_metadata, "iiiiii", &num_warps, &num_ctas, &shared_memory, &clusterDimX, &clusterDimY, &clusterDimZ)) {
    return NULL;
  }
  // extract launch metadata
  if (launch_enter_hook != Py_None){
    PyObject* args = Py_BuildValue("(O)", launch_metadata);
    PyObject* ret = PyObject_CallObject(launch_enter_hook, args);
    Py_DECREF(args);
    if (!ret)
      return NULL;
  }


  // raise exception asap
  r   c                 S   s>   g | ]\}}|d  dkrd| d| d| d| d	ndqS )r   r   zDevicePtrInfo ptr_infoz = getPointer(_argr   z); if (!ptr_infoz.valid) return NULL;r   r   r   r   r   r   rB     s   > z;
  _launch(gridX, gridY, gridZ, num_warps, num_ctas, launch_cooperative_grid, clusterDimX, clusterDimY, clusterDimZ, shared_memory, (hipStream_t)_stream, (hipFunction_t)_functionan  );

  if(launch_exit_hook != Py_None){
    PyObject* args = Py_BuildValue("(O)", launch_metadata);
    PyObject* ret = PyObject_CallObject(launch_exit_hook, args);
    Py_DECREF(args);
    if (!ret)
      return NULL;
  }

  if(PyErr_Occurred()) {
    return NULL;
  }
  // return None
  Py_INCREF(Py_None);
  return Py_None;
}

static PyMethodDef ModuleMethods[] = {
  {"launch", launch, METH_VARARGS, "Entry point for all kernels with this signature"},
  {NULL, NULL, 0, NULL} // sentinel
};

static struct PyModuleDef ModuleDef = {
  PyModuleDef_HEAD_INIT,
  "__triton_launcher",
  NULL, //documentation
  -1, //size
  ModuleMethods
};

PyMODINIT_FUNC PyInit___triton_launcher(void) {
  if (!initSymbolTable()) {
    return NULL;
  }
  PyObject *m = PyModule_Create(&ModuleDef);
  if(m == NULL) {
    return NULL;
  }
  PyModule_AddFunctions(m, ModuleMethods);
  return m;
}
)rJ   valuesr   listfilterboolr>   	enumerater   itemsrK   r\   range)	constants	signature	warp_sizeargs_formatformat	args_list	arg_declsinternal_args_listr   r   r   paramsrr   r   )r   r   r   r   make_launcher   sl   
,

cfhl )  *  -  A  B  mr   c                   @   s   e Zd Zdd Zdd ZdS )HIPLauncherc                    sx   t drjnt }fdd  fdd| D }dd j D }t|||jtd}|j| _|j	| _	d S )Nr   c                    s   t | tr jj| fS | S N)r   strfn	arg_namesindex)x)rr   r   r   <lambda>  s    z&HIPLauncher.__init__.<locals>.<lambda>c                    s   i | ]	\}} ||qS r   r   r?   idxvalue)arg_idxr   r   r     s    z(HIPLauncher.__init__.<locals>.<dictcomp>c                 S   r   r   r   r   r   r   r   r     r   __triton_launcher)
r   r   dictr   r   r   r   r|   launchlaunch_cooperative_grid)r   rr   metadatar   r   r{   r   )r   rr   r   r     s   
zHIPLauncher.__init__c                 G   s   | j | jg|R   d S r   )r   r   )r   argsr   r   r   __call__  s   zHIPLauncher.__call__N)r   r   r   r   r   r   r   r   r   r     s    
r   c                       sX   e Zd Z fddZdd Zedd Zdd Zd	d
 Zdd Z	dd Z
dd Z  ZS )	HIPDriverc                    s   t    t | _t| _d S r   )r   r   r}   utilsr   launcher_cls)r   r   r   r   r     s   

zHIPDriver.__init__c                 C   s   dd l }|jS )Nr   )r7   cudar   r7   r   r   r   get_device_interface  s   zHIPDriver.get_device_interfacec                  C   s,   zdd l } | jjd uW S  ty   Y dS w )Nr   F)r7   versionhipImportError)r7   r   r   r   	is_active  s   zHIPDriver.is_activec                 C   s:   |   }| j|}|d }|d }td|dd |S )NarchwarpSizer  r:   r   )get_current_devicer   r   r   r>   )r   devicedevice_propertiesr  r   r   r   r   get_current_target
  s
   zHIPDriver.get_current_targetc                 C   s   dd l }|d|  S )Nr   r   )r7   r  r  r   r   r   r   get_active_torch_device  s   z!HIPDriver.get_active_torch_devicec                 C   s   ddl m} |S )Nr   )do_bench)triton.testingr  )r   r  r   r   r   get_benchmarker  s   zHIPDriver.get_benchmarkerc                 C   s&   dd l }d}|jt|d |jddS )Nr   i      r   )dtyper  )r7   emptyint)r   r7   
cache_sizer   r   r   get_empty_cache_for_benchmark  s   z'HIPDriver.get_empty_cache_for_benchmarkc                 C   s   |   d S r   )zero_)r   rt   r   r   r   clear_cache!  s   zHIPDriver.clear_cache)r   r   r   r   r   staticmethodr  r	  r
  r  r  r  r   r   r   r   r   r     s    
r   )	functoolsr   r`   rL   re   pathlibr   triton.runtime.buildr   triton.runtime.cacher   triton.backends.compilerr   triton.backends.driverr   r4   r   realpath__file__rJ   ri   r5   	lru_cacher\   r|   objectr}   r   r   r   r   r   r   r   r   <module>   s.    0
>  -