o
    
۾i                     @   s   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
 d dlmZ zd dlm  mZ dZW n ey@   dZY nw eeZG d	d
 d
ZdS )    N)ProcessGroup)SYMM_MEM_ALL_REDUCE_MAX_SIZES)init_logger)vllm_is_batch_invariant)current_platformTFc                
   @   s   e Zd Zg dddgdZ		ddedeeB ejB de	dB d	edB fd
dZ
dejfddZdddejdejdB dejdB fddZdS )SymmMemCommunicator)         r	   r
   )z9.0z10.0Ngroupdeviceforce_multimemmax_size_overridec              
   C   s  d| _ tsd S t std d S t|tr t	d| }n
t|t
r*t	|}tj| tj| _|| _	|| _t| j| _t }|d u rPtd d S | | _| jtvrctd| j d S | jt| j vrttd| j d S |d ur|| _td| j n	t| j | j | _ztj| j| jj | j	| jd| _t| j| jj}W n ty } zt d	t
| W Y d }~d S d }~ww |j!d
krtd d S || _"d| _ t# rd| _ d S d S )NTz7SymmMemCommunicator: symmetric memory is not available.zcuda:zQSymmMemCommunicator: device capability is unknown, communicator is not available.zWSymmMemCommunicator: Device capability %s not supported, communicator is not available.zPSymmMemCommunicator: World size %d not supported, communicator is not available.z6SymmMemCommunicator: Using override max_size: %s bytes)r   dtypezSymmMemCommunicator: symmetric memory initialization failed: %s Communicator is not available. To suppress this warning set VLLM_ALLREDUCE_USE_SYMM_MEM=0r   zMSymmMemCommunicator: symmetric memory multicast operations are not supported.F)$disabledsymm_mem_availabler   is_cudaloggerwarning
isinstanceinttorchr   strcuda
set_devicebfloat16r   r   distget_world_size
world_sizeget_device_capabilityas_version_strdevice_capabilityr   max_sizeinfotorch_symm_mememptyitemsizebuffer
rendezvous
group_nameRuntimeErrorwarning_oncemulticast_ptrr   r   )selfr   r   r   r   
capabilityhandlee r1   b/home/ubuntu/.local/lib/python3.10/site-packages/vllm/distributed/device_communicators/symm_mem.py__init__!   s   







zSymmMemCommunicator.__init__inpc                 C   sD   | j rdS |j| jkrdS | |  }|d dkrdS || jk S )NFr   r   )r   r   numelelement_sizer"   )r-   r4   inp_sizer1   r1   r2   should_use_symm_memu   s   
z'SymmMemCommunicator.should_use_symm_mem)outr9   returnc                C   s   |  |sd S |d u rt|}| jd |  |d d}| jd ur*| j}n	| j| j	| j
 v }|rHtjj| jd |  d| jj ntjj| jd |  d| jj || jd |  |j |S )NFsum)r8   r   
empty_liker'   r5   copy_viewr   r   _WORLD_SIZES_MULTIMEMr!   opssymm_memmultimem_all_reduce_r   r)   two_shot_all_reduce_shape)r-   r4   r9   use_multimemr1   r1   r2   
all_reduce   s&   


 zSymmMemCommunicator.all_reduce)NN)__name__
__module____qualname__r@   r   r   r   r   r   boolr3   Tensorr8   rG   r1   r1   r1   r2   r      s0    

Tr   )r   torch.distributeddistributedr   r   6vllm.distributed.device_communicators.all_reduce_utilsr   vllm.loggerr   *vllm.model_executor.layers.batch_invariantr   vllm.platformsr   #torch.distributed._symmetric_memory_symmetric_memoryr$   r   ImportErrorrH   r   r   r1   r1   r1   r2   <module>   s   