o
    پi!                     @   s   d Z ddlZddlmZ ddlmZmZmZmZ ddl	m
Z ddl	mZ ejZejZG dd dejZeG dd	 d	Zd
ee fddZG dd dZe Z	ddedee d
ee fddZ	ddee dee d
dfddZdS )a3  
Copyright (c) 2025 by FlashInfer team.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    N)	dataclass)AnyDictListOptional)ProcessGroupc                   @   s   e Zd Zdejd fgZdS )cudaIpcMemHandle_tinternal   N)__name__
__module____qualname__ctypesc_byte_fields_ r   r   L/home/ubuntu/.local/lib/python3.10/site-packages/flashinfer/comm/cuda_ipc.pyr       s    r   c                   @   s*   e Zd ZU eed< eed< ee ed< dS )FunctionnamerestypeargtypesN)r   r   r   str__annotations__r   r   r   r   r   r   r   $   s   
 r   returnc                 C   s   d}t d}|D ]
}| |v rd} nq	W d   n1 sw   Y  |s'dS |d}||d  }|dd }|dd | sOJ d	| d
|  |S )a)  
    According to according to https://man7.org/linux/man-pages/man5/proc_pid_maps.5.html,
    the file `/proc/self/maps` contains the memory maps of the process, which includes the
    shared libraries loaded by the process. We can use this file to find the path of the
    a loaded library.
    Fz/proc/self/mapsTN/z.sor   zUnexpected filename: z for library )openindexstripsplit
rpartition
startswith)lib_namefoundflinestartpathfilenamer   r   r   find_loaded_library+   s$   

r)   c                   @   s  e Zd ZU d Zedeejgedeg edeg edejegedee	ej
ejgedeej
gedeej
ejejgedeej
ej
ejeged	ee	eej
ged
ee	ej
eejgg
Zi Zeeef ed< i Zeeeeef f ed< d2dee fddZdeddfddZdedefddZdeddfddZd3ddZd3ddZdedej
fd d!Zd"ej
ddfd#d$Zd"ej
d%ed&eddfd'd(Z d)ej
d*ej
d&eddfd+d,Z!d"ej
defd-d.Z"d/edej
fd0d1Z#dS )4CudaRTLibrarycudaSetDevicecudaDeviceSynchronizecudaDeviceResetcudaGetErrorString
cudaMalloccudaFree
cudaMemset
cudaMemcpycudaIpcGetMemHandlecudaIpcOpenMemHandlepath_to_library_cachepath_to_dict_mappingNso_filec                 C   s   |d u rt d}|d usJ d|tjvrt|}|tj|< tj| | _|tjvrKi }tjD ]}t| j|j	}|j
|_
|j|_|||j	< q/|tj|< tj| | _d S )N	libcudartz.libcudart is not loaded in the current process)r)   r*   r5   r   CDLLlibr6   exported_functionsgetattrr   r   r   funcs)selfr7   r:   _funcsfuncr$   r   r   r   __init__z   s    





zCudaRTLibrary.__init__resultr   c                 C   s$   |dkr|  |}td| d S )Nr   zCUDART error: )r.   RuntimeError)r>   rB   	error_strr   r   r   CUDART_CHECK   s   
zCudaRTLibrary.CUDART_CHECKerrorc                 C   s   | j d |dS )Nr.   zutf-8)r=   decode)r>   rF   r   r   r   r.      s   z CudaRTLibrary.cudaGetErrorStringdevicec                 C      |  | jd | d S )Nr+   rE   r=   )r>   rH   r   r   r   r+         zCudaRTLibrary.cudaSetDevicec                 C      |  | jd   d S )Nr,   rJ   r>   r   r   r   r,         z#CudaRTLibrary.cudaDeviceSynchronizec                 C   rL   )Nr-   rJ   rM   r   r   r   r-      rN   zCudaRTLibrary.cudaDeviceResetsizec                 C   s(   t  }| | jd t || |S )Nr/   r   c_void_prE   r=   byref)r>   rO   devPtrr   r   r   r/      s   zCudaRTLibrary.cudaMallocrS   c                 C   rI   )Nr0   rJ   )r>   rS   r   r   r   r0      rK   zCudaRTLibrary.cudaFreevaluecountc                 C   s   |  | jd ||| d S )Nr1   rJ   )r>   rS   rT   rU   r   r   r   r1      s   zCudaRTLibrary.cudaMemsetdstsrcc                 C   s&   d}|}|  | jd |||| d S )N   r2   rJ   )r>   rV   rW   rU   cudaMemcpyDefaultkindr   r   r   r2      s   zCudaRTLibrary.cudaMemcpyc                 C   s&   t  }| | jd t|| |S )Nr3   )r   rE   r=   r   rR   )r>   rS   handler   r   r   r3      s
   z!CudaRTLibrary.cudaIpcGetMemHandler[   c                 C   s.   d}t  }| | jd t ||| |S )N   r4   rP   )r>   r[   cudaIpcMemLazyEnablePeerAccessrS   r   r   r   r4      s   z"CudaRTLibrary.cudaIpcOpenMemHandleN)r   N)$r   r   r   __doc__r   cudaError_tr   c_intc_char_pPOINTERrQ   c_size_tcudaMemcpyKindr   c_uintr;   r5   r   r   r   r   r6   r   rA   rE   r.   intr+   r,   r-   r/   r0   r1   r2   r3   r4   r   r   r   r   r*   F   sj   
 

+


r*   size_in_bytesgroupc           
      C   s   t | }t |}|du rtjj}tj|d}tj|d}dg| }tj|||d dg| }tj|||d g }t	|D ]\}}	||krM|
|j q>|
t |	j q>tj|d |S )z{
    Creates a shared buffer and returns a list of pointers
    representing the buffer on all processes in the group.
    Nri   )cudartr/   r3   distri   WORLDget_world_sizeget_rankall_gather_object	enumerateappendrT   r4   barrier)
rh   ri   pointerr[   
world_sizerankhandlespointersihr   r   r   create_shared_buffer   s"   



r{   rx   c                 C   s\   |du rt jj}t j|d}| r&t| |kr&| | dur&tt| |  t j	|d dS )z 
    Frees a shared buffer.
    Nrj   )
rl   ri   rm   ro   lenrk   r0   r   rQ   rs   )rx   ri   rv   r   r   r   free_shared_buffer   s   r}   r^   )r_   r   dataclassesr   typingr   r   r   r   torch.distributeddistributedrl   r   ra   r`   re   	Structurer   r   r   r)   r*   rk   rg   r{   r}   r   r   r   r   <module>   s<    |
