o
    ٷig                     @   s  U 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mZ d dlmZmZ e eZdedB fd	d
ZdedB fddZdedB fddZdedB fddZeeeedZdefddZdadaeed< erveed< defddZdefddZg dZdS )    N)chain)TYPE_CHECKING)resolve_obj_by_qualname)supports_xccl)OmniPlatformOmniPlatformEnum)OMNI_PLATFORM_PLUGINS_GROUPload_omni_plugins_by_groupreturnc               
   C   s   d} t d z.ddlm} | }|  z| dkr$d} t d nt d W |  n|  w W n tyO } zt dt| W Y d	}~nd	}~ww | rTd
S d	S )z/Check if CUDA OmniPlatform should be activated.Fz+Checking if CUDA OmniPlatform is available.r   )import_pynvmlTz)Confirmed CUDA OmniPlatform is available.z;CUDA OmniPlatform is not available because no GPU is found.z.CUDA OmniPlatform is not available because: %sNz2vllm_omni.platforms.cuda.platform.CudaOmniPlatform)	loggerdebugvllm.utils.import_utilsr   nvmlInitnvmlDeviceGetCountnvmlShutdown	Exceptionstr)is_cudar   pynvmle r   P/home/ubuntu/.local/lib/python3.10/site-packages/vllm_omni/platforms/__init__.pycuda_omni_platform_plugin   s$   

r   c               
   C   s   d} t d z+ddl}|  zt| dkr!d} t d nt d W |  n|  w W n tyL } zt dt| W Y d}~nd}~ww | rQd	S dS )
z/Check if ROCm OmniPlatform should be activated.Fz+Checking if ROCm OmniPlatform is available.r   NTz)Confirmed ROCm OmniPlatform is available.z;ROCm OmniPlatform is not available because no GPU is found.z.ROCm OmniPlatform is not available because: %sz2vllm_omni.platforms.rocm.platform.RocmOmniPlatform)	r   r   amdsmiamdsmi_initlenamdsmi_get_processor_handlesamdsmi_shut_downr   r   )is_rocmr   r   r   r   r   rocm_omni_platform_plugin,   s"   

r    c               
   C   s~   d} t d zddl}t|dr|j rd} t d W n ty8 } zt dt| W Y d}~nd}~ww | r=d	S dS )
z.Check if NPU OmniPlatform should be activated.Fz*Checking if NPU OmniPlatform is available.r   NnpuTz(Confirmed NPU OmniPlatform is available.z-NPU OmniPlatform is not available because: %sz0vllm_omni.platforms.npu.platform.NPUOmniPlatform)r   r   torchhasattrr!   is_availabler   r   )is_npur"   r   r   r   r   npu_omni_platform_pluginB   s   

r&   c               
   C   s   d} t d z3ddl}t rd}nd}ddl}t|dr9|j r9d} dd	lm	} ||_
t d
|j
 t d W n tyT } zt dt| W Y d}~nd}~ww | rYdS dS )z.Check if XPU OmniPlatform should be activated.Fz*Checking if XPU OmniPlatform is available.r   NxcclcclxpuT)XPUOmniPlatformz"Confirmed %s backend is available.z$Confirmed XPU platform is available.z.XPU omni platform is not available because: %sz0vllm_omni.platforms.xpu.platform.XPUOmniPlatform)r   r   r"   r   oneccl_bindings_for_pytorchr#   r)   r$   vllm_omni.platforms.xpur*   dist_backendr   r   )is_xpur"   r-   r+   r*   r   r   r   r   xpu_omni_platform_pluginR   s(   

r/   )cudarocmr!   r)   c               	   C   s4  t t} g }tt |  D ]!\}}zt|sJ | }|dur&|| W q ty0   Y qw tt	|t	t
 @ }tt	|t	| 
 @ }t|dkrVtd| t|dkrm| |d   }td|d  |S t|dkrztd| t|dkrt|d   }td|d  |S d}td	 |S )
z6Resolve the current OmniPlatform class qualified name.N   z8Only one OmniPlatform plugin can be activated, but got:    r   z#OmniPlatform plugin %s is activatedz'Automatically detected OmniPlatform %s.z5vllm_omni.platforms.interface.UnspecifiedOmniPlatformzENo platform detected, vLLM-Omni is running on UnspecifiedOmniPlatform)r	   r   r   builtin_omni_platform_pluginsitemscallableappendr   listsetkeysr   RuntimeErrorr   infor   )platform_pluginsactivated_pluginsnamefuncplatform_cls_qualnameactivated_builtin_pluginsactivated_oot_pluginsr   r   r   *resolve_current_omni_platform_cls_qualnamet   s:   


rD    _init_tracecurrent_omni_platformr?   c                 C   s\   | dkrt d u rt }t| a dt at S | t v r#t |  S td|  dt	 d)NrG   rE   No attribute named '' exists in .)
_current_omni_platformrD   r   join	tracebackformat_stackrF   globalsAttributeError__name__)r?   rA   r   r   r   __getattr__   s   


rR   c                 C   s>   | dkr|a d S | t v r|t | < d S td|  dt d)NrG   rH   rI   rJ   )rK   rO   rP   rQ   )r?   valuer   r   r   __setattr__   s
   
rT   )r   r   rG   rF   ) loggingrM   	itertoolsr   typingr   r   r   vllm.utils.torch_utilsr   vllm_omni.platforms.interfacer   r   vllm_omni.pluginsr   r	   	getLoggerrQ   r   r   r   r    r&   r/   r4   rD   rK   rF   __annotations__rR   rT   __all__r   r   r   r   <module>   s4   

#
