o
    پi                     @   s   d Z ddlZddlmZmZmZ ddlZddlmZ e	e
Zg dZdedeeee ee f fdd	Zdeeee f fd
dZdS )z>Mooncake-specific utilities for custom memory pool management.    N)AnyOptionalTuple)envs)NVLINKBAREXINTRA_NVLINKdevicereturnc              
   C   s6  t  \}}d}|rzC|dkrddlm} || }n|dkr*ddlm} || }n|dkr1W dS td	| tj|	 }t
d
| d|   W nI typ } zt
d| d| d d}d}d}W Y d}~n*d}~w ty } zt
d| d| d d}d}d}W Y d}~nd}~ww dS |||fS )z
    Initialize custom memory pool based on environment variable.

    Args:
        device: The device to allocate memory on

    Returns:
        Tuple of (enable_custom_mem_pool, custom_mem_pool, custom_mem_pool_type)
    Nr   r   )NVLinkAllocatorr   )BarexAllocatorINTRA_NODE_NVLINK)FNNz"Unsupported custom mem pool type: z Initialized custom memory pool: z on device z(Failed to import mooncake allocator for z: z&. Falling back to default memory pool.Fz(Failed to initialize custom memory pool )&check_mooncake_custom_mem_pool_enabledmooncake.allocatorr   get_allocatorr   
ValueErrortorchcudaMemPool	allocatorloggerdebugImportErrorwarning	Exceptionerror)r	   enable_custom_mem_poolcustom_mem_pool_typecustom_mem_poolr   r   r   e r    \/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/disaggregation/mooncake/utils.pyinit_mooncake_custom_mem_pool   sN   	
r"   c                  C   sB   t j } | dur|  dkrd} | tv }|| fS d}d} || fS )z
    Check if custom memory pool is enabled without importing allocators.

    Returns:
        Tuple of (enable_custom_mem_pool, custom_mem_pool_type)
    Ntruer   F)r   SGLANG_MOONCAKE_CUSTOM_MEM_POOLgetlower(SUPPORTED_MOONCAKE_CUSTOM_MEM_POOL_TYPES)r   r   r    r    r!   r   \   s   
r   )__doc__loggingtypingr   r   r   r   sglang.srt.environr   	getLogger__name__r   r'   strboolr"   r   r    r    r    r!   <module>   s   

?