
    )`i!                     `   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ej                  Ze G d d	                      Zd
ee         fdZ G d d          Z e            Z	 ddedee         d
ee         fdZ	 ddee         dee         d
df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                   (    e Zd Zdej        dz  fgZdS )cudaIpcMemHandle_tinternal   N)__name__
__module____qualname__ctypesc_byte_fields_     l/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/flashinfer/comm/cuda_ipc.pyr
   r
       s"        V]S012HHHr   r
   c                   :    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   $   s4         
IIILLL3ir   r   returnc                    d}t          d          5 }|D ]
}| |v rd} nddd           n# 1 swxY w Y   |sdS |                    d          }||d                                         }|                    d          d         }|                    d          d                             |           sJ 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filenames          r   find_loaded_libraryr.   +   s5    E		 	  A 	 	D4                
  t JJsOOE<Dzz#r"Hu%%a(33H==  AAAxAA = Ks   ,00c                   f   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j	        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j	        e          ej
        g           ed
e ej	        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<   d&dee         fdZdeddfdZdedefdZdeddfdZd'dZd'dZdedej
        fdZdej
        ddfdZdej
        dededdfdZ d ej
        d!ej
        deddfd"Z!dej
        defd#Z"d$edej
        fd%Z#dS )(CudaRTLibrarycudaSetDevicecudaDeviceSynchronizecudaDeviceResetcudaGetErrorString
cudaMalloccudaFree
cudaMemset
cudaMemcpycudaIpcGetMemHandlecudaIpcOpenMemHandlepath_to_library_cachepath_to_dict_mappingNso_filec                    |t          d          }|
J d            |t          j        vr#t          j        |          }|t          j        |<   t          j        |         | _        |t          j        vr\i }t          j        D ]>}t          | j        |j	                  }|j
        |_
        |j        |_        |||j	        <   ?|t          j        |<   t          j        |         | _        d S )N	libcudartz.libcudart is not loaded in the current process)r.   r0   r;   r   CDLLlibr<   exported_functionsgetattrr   r   r   funcs)selfr=   rA   _funcsfuncr)   s         r   __init__zCudaRTLibrary.__init__z   s    ?)+66G&&(X&&&-===+g&&C;>M/8 6w?-<<<F%8 & &DHdi00 L	!]
$%ty!!:@M.w7"7@


r   resultr   c                 `    |dk    r'|                      |          }t          d|           d S )Nr   zCUDART error: )r4   RuntimeError)rE   rI   	error_strs      r   CUDART_CHECKzCudaRTLibrary.CUDART_CHECK   s<    Q;;//77I;	;;<<< ;r   errorc                 T     | j         d         |                              d          S )Nr4   zutf-8)rD   decode)rE   rN   s     r   r4   z CudaRTLibrary.cudaGetErrorString   s(    /tz./66==gFFFr   devicec                 X    |                       | j        d         |                     d S )Nr1   rM   rD   )rE   rQ   s     r   r1   zCudaRTLibrary.cudaSetDevice   s.    5$*_5f==>>>>>r   c                 V    |                       | j        d                                d S )Nr2   rS   rE   s    r   r2   z#CudaRTLibrary.cudaDeviceSynchronize   s-    =$*%<=??@@@@@r   c                 V    |                       | j        d                                d S )Nr3   rS   rU   s    r   r3   zCudaRTLibrary.cudaDeviceReset   s-    7$*%6799:::::r   sizec                     t          j                    }|                      | j        d         t          j        |          |                     |S )Nr5   r   c_void_prM   rD   byref)rE   rW   devPtrs      r   r5   zCudaRTLibrary.cudaMalloc   sF    ""2$*\26<3G3GNNOOOr   r\   c                 X    |                       | j        d         |                     d S )Nr6   rS   )rE   r\   s     r   r6   zCudaRTLibrary.cudaFree   s.    0$*Z08899999r   valuecountc                 \    |                       | j        d         |||                     d S )Nr7   rS   )rE   r\   r^   r_   s       r   r7   zCudaRTLibrary.cudaMemset   s2    2$*\265%HHIIIIIr   dstsrcc                 f    d}|}|                       | j        d         ||||                     d S )N   r8   rS   )rE   ra   rb   r_   cudaMemcpyDefaultkinds         r   r8   zCudaRTLibrary.cudaMemcpy   sA      2$*\23UDIIJJJJJr   c                     t                      }|                      | j        d         t          j        |          |                     |S )Nr9   )r
   rM   rD   r   r[   )rE   r\   handles      r   r9   z!CudaRTLibrary.cudaIpcGetMemHandle   sM    #%%-DJ,-fl6.B.BFKK	
 	
 	
 r   rh   c                     d}t          j                    }|                      | j        d         t          j        |          ||                     |S )N   r:   rY   )rE   rh   cudaIpcMemLazyEnablePeerAccessr\   s       r   r:   z"CudaRTLibrary.cudaIpcOpenMemHandle   s]    )*&"".DJ-.V$$f.L 	
 	
 	

 r   N)r   N)$r   r   r   __doc__r   cudaError_tr   c_intc_char_pPOINTERrZ   c_size_tcudaMemcpyKindr
   c_uintrB   r;   r   r   r   r   r<   r   rH   rM   r4   intr1   r2   r3   r5   r6   r7   r8   r9   r:   r   r   r   r0   r0   F   sv         	+~>>(+r::"K44%vFFV^FO,,fo>	
 	
 	[6?*;<<+v'W	
 	
 	_fovO	
 	
 	!V^.//A	
 	
 	"V^FO,,.@&-P	
 	
E'V -/4S>... 79$sDcN23888A A A A A A&=; =4 = = = =
G G G G G G?C ?D ? ? ? ?A A A A; ; ; ;s v    
:v :4 : : : :J J JS JT J J J JK?K)/KADK	K K K K&/ >P    += &/      r   r0   size_in_bytesgroupc                 Z   t                               |           }t                               |          }|t          j        j        }t          j        |          }t          j        |          }dg|z  }t          j        |||           dg|z  }t          j        |||           g }t          |          D ]X\  }}	||k    r|
                    |j                   &|
                    t                               |	          j                   Yt          j        |           |S )z{
    Creates a shared buffer and returns a list of pointers
    representing the buffer on all processes in the group.
    Nrw   )cudartr5   r9   distrw   WORLDget_world_sizeget_rankall_gather_object	enumerateappendr^   r:   barrier)
rv   rw   pointerrh   
world_sizerankhandlespointersihs
             r   create_shared_bufferr      s&    ..G''00F}
 $5111J=u%%%Dfz!G7F%8888fz!G7F%8888H'"" B B199OOGM****OOF77::@AAAALuOr   r   c                    |t           j        j        }t          j        |          }| rMt	          |           |k    r:| |         2t
                              t          j        | |                              t          j	        |           dS )z 
    Frees a shared buffer.
    Nry   )
r{   rw   r|   r~   lenrz   r6   r   rZ   r   )r   rw   r   s      r   free_shared_bufferr      s     }
 =u%%%D 9CMMD((Xd^-G77888Lur   rl   )rm   r   dataclassesr   typingr   r   r   r   torch.distributeddistributedr{   r   ro   rn   rs   	Structurer
   r   r   r.   r0   rz   ru   r   r   r   r   r   <module>r      s      ! ! ! ! ! ! , , , , , , , , , , , ,             * * * * * * l3 3 3 3 3) 3 3 3        Xc]    6y y y y y y y yx 
 9= '5	#Y   < :> 3i ( 6	     r   