o
    پi                     @   sp   d dl mZ d dlmZ d dlZd dlZd dlmZmZm	Z	 d dl
mZ d dlmZ eeZG dd deZdS )	    )	lru_cache)AnyN)AttentionBackendEnumPlatformPlatformEnum)DeviceCapability)init_loggerc                   @   s  e Zd ZU ejZdZeed< dZ	eed< dZ
eed< dZeed< eedd	d
efddZed
ejfddZed1ded
edB fddZed1ded
efddZed1ded
efddZeedd	d1ded
efddZededB d
efddZe	d2dejjdB d
efddZe		 	!	d3ded"ed#ed$ed
ef
d%d&Zed'e dB d(ed)ej!d
efd*d+Z"ed
efd,d-Z#ed2d.edB d
dfd/d0Z$dS )4MpsPlatformmpsdevice_namedevice_typeMPSdispatch_keyMPS_VISIBLE_DEVICESdevice_control_env_var   )maxsizereturnc                 C      dS )NF clsr   r   _/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/runtime/platforms/mps.pyis_amp_supported      zMpsPlatform.is_amp_supportedc                 C   s
   t dS )Nr
   )torchdevicer   r   r   r   get_local_torch_device"   s   
z"MpsPlatform.get_local_torch_devicer   	device_idNc                 C      t NNotImplementedErrorr   r   r   r   r   get_device_capability&      z!MpsPlatform.get_device_capabilityc                 C   r   r    r!   r#   r   r   r   get_device_name*   r%   zMpsPlatform.get_device_namec                 C   r   r    r!   r#   r   r   r   get_device_uuid.   r%   zMpsPlatform.get_device_uuidc                 C   s
   t  jS r    )psutilvirtual_memorytotalr#   r   r   r   get_device_total_memory2   s   
z#MpsPlatform.get_device_total_memoryenforce_eagerc                 C   s   |r	t d dS dS )NzTo see benefits of async output processing, enable MPS graph. Since, enforce-eager is enabled, async output processor cannot be usedFT)loggerwarning)r   r,   r   r   r   is_async_output_supported8   s   z%MpsPlatform.is_async_output_supportedr   c                 C   r   )Ng        r   )r   r   r   r   r   get_current_memory_usageC   s   z$MpsPlatform.get_current_memory_usageFTdistributedempty_cache	cpu_groupc                 C   s`   |rt j  t j}|r,dd lm} t j|t j	d}|j
||jj|d t| }|d S )Nr   )dtype)opgroupi   @)r   r
   r2   r(   r)   	availabletorch.distributedr1   tensorfloat32
all_reduceReduceOpMINfloatitem)r   r   r1   r2   r3   free_memorydistr9   r   r   r   get_available_gpu_memoryI   s   	

z$MpsPlatform.get_available_gpu_memoryselected_backend	head_sizer4   c                 C   s   t d 	 dS )Nz!Using Torch SDPA backend for MPS.zHsglang.multimodal_gen.runtime.layers.attention.backends.sdpa.SDPABackend)r-   info)r   rC   rD   r4   r   r   r   get_attn_backend_cls_stra   s   
z$MpsPlatform.get_attn_backend_cls_strc                 C   r   )Nznsglang.multimodal_gen.runtime.distributed.device_communicators.base_device_communicator.DeviceCommunicatorBaser   r   r   r   r   get_device_communicator_clsn   r   z'MpsPlatform.get_device_communicator_clsseedc                 C   s@   |durddl }ddl}|| |j | t| dS dS )zSet the seed for MPS device.Nr   )randomnumpyrH   r   manual_seed)r   rH   rI   npr   r   r   seed_everythings   s   
zMpsPlatform.seed_everything)r   r    )r   FTN)%__name__
__module____qualname__r   r   _enumr   str__annotations__r   r   r   classmethodr   boolr   r   r   r   intr   r$   r&   r'   r+   r/   typesDevicer>   r0   r   rB   r   r4   rF   rG   rM   r   r   r   r   r	      sv   
 

r	   )	functoolsr   typingr   r(   r   'sglang.multimodal_gen.runtime.platformsr   r   r   1sglang.multimodal_gen.runtime.platforms.interfacer   1sglang.multimodal_gen.runtime.utils.logging_utilsr   rN   r-   r	   r   r   r   r   <module>   s   