o
    .iJ7                     @   s  d Z ddlZddlZ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ZddlmZ ddlmZ eeZdd	 Zed
d ZdedeejB fddZdedefddZedd ZG dd dZG dd deZG dd deZG dd de	ZededefddZ defd d!Z!defd"d#Z"defd$d%Z#defd&d'Z$defd(d)Z%defd*d+Z&defd,d-Z'defd.d/Z(defd0d1Z)dS )2zg
Contains helpers related to importing modules.

This is similar in concept to the `importlib` module.
    N)cache)
ModuleType)Any)Never)init_loggerc                  C   s   ddl m  m}  | S )a  
    Historical comments:

    libnvml.so is the library behind nvidia-smi, and
    pynvml is a Python wrapper around it. We use it to get GPU
    status without initializing CUDA context in the current process.
    Historically, there are two packages that provide pynvml:
    - `nvidia-ml-py` (https://pypi.org/project/nvidia-ml-py/): The official
        wrapper. It is a dependency of vLLM, and is installed when users
        install vLLM. It provides a Python module named `pynvml`.
    - `pynvml` (https://pypi.org/project/pynvml/): An unofficial wrapper.
        Prior to version 12.0, it also provides a Python module `pynvml`,
        and therefore conflicts with the official one. What's worse,
        the module is a Python package, and has higher priority than
        the official one which is a standalone Python file.
        This causes errors when both of them are installed.
        Starting from version 12.0, it migrates to a new module
        named `pynvml_utils` to avoid the conflict.
    It is so confusing that many packages in the community use the
    unofficial one by mistake, and we have to handle this case.
    For example, `nvcr.io/nvidia/pytorch:24.12-py3` uses the unofficial
    one, and it will cause errors, see the issue
    https://github.com/vllm-project/vllm/issues/12847 for example.
    After all the troubles, we decide to copy the official `pynvml`
    module to our codebase, and use it directly.
    r   N)vllm.third_party.pynvmlthird_partypynvml)r	    r
   T/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/vllm/utils/import_utils.pyimport_pynvml   s   r   c                  C   sz   t drddl} tjd| j ddd dS t dr6ddlm  m}  tjd| j ddd | tjd< dS t	d	 dS )
z
    For convenience, prioritize triton_kernels that is available in
    `site-packages`. Use `vllm.third_party.triton_kernels` as a fall-back.
    triton_kernelsr   Nz#Loading module triton_kernels from .local)scopevllm.third_party.triton_kernelsztriton_kernels unavailable in this build. Please consider installing triton_kernels from https://github.com/triton-lang/triton/tree/main/python/triton_kernels)
_has_moduler   logger
debug_once__file__vllm.third_party.triton_kernelsr   sysmodules	info_once)r   r
   r
   r   import_triton_kernels:   s    
r   module_name	file_pathc                 C   sX   t j| |}|du rtd| |jdusJ t j|}|tj| < |j| |S )z
    Import a Python file according to its file path.

    Based on the official recipe:
    https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
    NzNo module named )		importlibutilspec_from_file_locationModuleNotFoundErrorloadermodule_from_specr   r   exec_module)r   r   specmoduler
   r
   r   import_from_pathW   s   
r&   qualnamereturnc                 C   s$   |  dd\}}t|}t||S )z>
    Resolve an object by its fully-qualified class name.
    r      )rsplitr   import_modulegetattr)r'   r   obj_namer%   r
   r
   r   resolve_obj_by_qualnamej   s   

r.   c                     s6   t jd} | dg  | dg } fdd|D S )NvllmzRequires-DistzProvides-Extrac                    s    i | ]   fd dD qS )c                    s.   g | ]}| d   drtd|d qS )z
extra == ""z
;|>=|<=|==r   )endswithresplit).0reqextrar
   r   
<listcomp>z   s    z=get_vllm_optional_dependencies.<locals>.<dictcomp>.<listcomp>r
   )r4   requirementsr6   r   
<dictcomp>y   s    z2get_vllm_optional_dependencies.<locals>.<dictcomp>)r   metadataget_all)r<   extrasr
   r9   r   get_vllm_optional_dependenciess   s   
r?   c                   @   s  e Zd ZdZdedefddZdefddZdefd	d
Z	defddZ
defddZdefddZdefddZdd Zdd ZdedefddZdd ZdefddZdedefd d!Zdefd"d#Zdefd$d%Zdefd&d'Zdefd(d)Zdefd*d+Zdefd,d-Zdefd.d/Zdefd0d1Zdefd2d3ZdZded5efd6d7Zdefd8d9Zdefd:d;Zdefd<d=Z defd>d?Z!defd@dAZ"dBdC Z#dDdE Z$dFdG Z%dHdI Z&dJdK Z'dZdLefdMdNZ(dOdP Z)dQdR Z*dSdT Z+dUdV Z,dedefdWdXZ-dYS )[_PlaceholderBasea]  
    Disallows downstream usage of placeholder modules.

    We need to explicitly override each dunder method because
    [`__getattr__`][vllm.utils.import_utils._PlaceholderBase.__getattr__]
    is not called when they are accessed.

    Info:
        [Special method lookup](https://docs.python.org/3/reference/datamodel.html#special-lookup)
    keyr(   c                 C   s   t )z
        The main class should implement this to throw an error
        for attribute accesses representing downstream usage.
        )NotImplementedErrorselfrA   r
   r
   r   __getattr__   s   z_PlaceholderBase.__getattr__otherc                 C   
   |  dS )N__lt__rE   rD   rF   r
   r
   r   rH         
z_PlaceholderBase.__lt__c                 C   rG   )N__le__rI   rJ   r
   r
   r   rL      rK   z_PlaceholderBase.__le__c                 C   rG   )N__eq__rI   rJ   r
   r
   r   rM      rK   z_PlaceholderBase.__eq__c                 C   rG   )N__ne__rI   rJ   r
   r
   r   rN      rK   z_PlaceholderBase.__ne__c                 C   rG   )N__gt__rI   rJ   r
   r
   r   rO      rK   z_PlaceholderBase.__gt__c                 C   rG   )N__ge__rI   rJ   r
   r
   r   rP      rK   z_PlaceholderBase.__ge__c                 C   rG   )N__hash__rI   rD   r
   r
   r   rQ      rK   z_PlaceholderBase.__hash__c                 C   rG   )N__bool__rI   rR   r
   r
   r   rS      rK   z_PlaceholderBase.__bool__argskwargsc                 O   rG   )N__call__rI   rD   rT   rU   r
   r
   r   rV      rK   z_PlaceholderBase.__call__c                 C   rG   )N__len__rI   rR   r
   r
   r   rX      rK   z_PlaceholderBase.__len__c                 C   rG   )N__getitem__rI   rC   r
   r
   r   rY      rK   z_PlaceholderBase.__getitem__valuec                 C   rG   )N__setitem__rI   )rD   rA   rZ   r
   r
   r   r[      rK   z_PlaceholderBase.__setitem__c                 C   rG   )N__delitem__rI   rC   r
   r
   r   r\      rK   z_PlaceholderBase.__delitem__c                 C   rG   )N__add__rI   rJ   r
   r
   r   r]      rK   z_PlaceholderBase.__add__c                 C   rG   )N__sub__rI   rJ   r
   r
   r   r^      rK   z_PlaceholderBase.__sub__c                 C   rG   )N__mul__rI   rJ   r
   r
   r   r_      rK   z_PlaceholderBase.__mul__c                 C   rG   )N
__matmul__rI   rJ   r
   r
   r   r`      rK   z_PlaceholderBase.__matmul__c                 C   rG   )N__truediv__rI   rJ   r
   r
   r   ra      rK   z_PlaceholderBase.__truediv__c                 C   rG   )N__floordiv__rI   rJ   r
   r
   r   rb      rK   z_PlaceholderBase.__floordiv__c                 C   rG   )N__mod__rI   rJ   r
   r
   r   rc      rK   z_PlaceholderBase.__mod__c                 C   rG   )N
__divmod__rI   rJ   r
   r
   r   rd      rK   z_PlaceholderBase.__divmod__.moduloc                 C   rG   )N__pow__rI   )rD   rF   re   r
   r
   r   rf      rK   z_PlaceholderBase.__pow__c                 C   rG   )N
__lshift__rI   rJ   r
   r
   r   rg      rK   z_PlaceholderBase.__lshift__c                 C   rG   )N
__rshift__rI   rJ   r
   r
   r   rh      rK   z_PlaceholderBase.__rshift__c                 C   rG   )N__and__rI   rJ   r
   r
   r   ri      rK   z_PlaceholderBase.__and__c                 C   rG   )N__xor__rI   rJ   r
   r
   r   rj      rK   z_PlaceholderBase.__xor__c                 C   rG   )N__or__rI   rJ   r
   r
   r   rk      rK   z_PlaceholderBase.__or__c                 C   rG   )N__neg__rI   rR   r
   r
   r   rl      rK   z_PlaceholderBase.__neg__c                 C   rG   )N__pos__rI   rR   r
   r
   r   rm      rK   z_PlaceholderBase.__pos__c                 C   rG   )N__abs__rI   rR   r
   r
   r   rn      rK   z_PlaceholderBase.__abs__c                 C   rG   )N
__invert__rI   rR   r
   r
   r   ro     rK   z_PlaceholderBase.__invert__c                 C   rG   )N	__index__rI   rR   r
   r
   r   rp     rK   z_PlaceholderBase.__index__ndigitsc                 C   rG   )N	__round__rI   )rD   rq   r
   r
   r   rr   
  rK   z_PlaceholderBase.__round__c                 C   rG   )N	__trunc__rI   rR   r
   r
   r   rs     rK   z_PlaceholderBase.__trunc__c                 C   rG   )N	__floor__rI   rR   r
   r
   r   rt     rK   z_PlaceholderBase.__floor__c                 C   rG   )N__ceil__rI   rR   r
   r
   r   ru     rK   z_PlaceholderBase.__ceil__c                 C   rG   )N	__enter__rI   rR   r
   r
   r   rv     rK   z_PlaceholderBase.__enter__c                 O   rG   )N__exit__rI   rW   r
   r
   r   rw     rK   z_PlaceholderBase.__exit__N).).__name__
__module____qualname____doc__strr   rE   objectrH   rL   rM   rN   rO   rP   rQ   rS   rV   rX   rY   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rr   rs   rt   ru   rv   rw   r
   r
   r
   r   r@      sR    	r@   c                       sJ   e Zd ZdZdeddf fddZdefdd	Zd
edefddZ  Z	S )PlaceholderModulez
    A placeholder object to use when a module does not exist.

    This enables more informative errors when trying to access attributes
    of a module that does not exist.
    namer(   Nc                    s   t    || _d S N)super__init___PlaceholderModule__name)rD   r   	__class__r
   r   r   '  s   

zPlaceholderModule.__init__	attr_pathc                 C   s
   t | |S r   )_PlaceholderModuleAttrrD   r   r
   r
   r   placeholder_attr-  rK   z"PlaceholderModule.placeholder_attrrA   c              
   C   sp   | j }z
t| W td ty7 } zt  D ]\}}||v r0d| d| d}t||q|d }~ww )NzPlease install vllm[z] for z supportMPlaceholderModule should not be used when the original module can be imported)r   r   r+   ImportErrorr?   itemsAssertionError)rD   rA   r   excr7   namesmsgr
   r
   r   rE   0  s   	
zPlaceholderModule.__getattr__)
rx   ry   rz   r{   r|   r   r   r   rE   __classcell__r
   r
   r   r   r~     s
    r~   c                       sJ   e Zd Zdededdf fddZdefddZd	edefd
dZ  Z	S )r   r%   r   r(   Nc                    s   t    || _|| _d S r   )r   r   _PlaceholderModuleAttr__module!_PlaceholderModuleAttr__attr_path)rD   r%   r   r   r
   r   r   D  s   

z_PlaceholderModuleAttr.__init__c                 C   s   t | j| j d| S )Nr   )r   r   r   r   r
   r
   r   r   K  s   z'_PlaceholderModuleAttr.placeholder_attrrA   c                 C   s    t | j| j d|  td)Nr   r   )r,   r   r   r   rC   r
   r
   r   rE   N  s   z"_PlaceholderModuleAttr.__getattr__)
rx   ry   rz   r~   r|   r   r   r   rE   r   r
   r
   r   r   r   C  s    r   c                       sh   e Zd ZdZdedeeef def fddZdefdd	Z	d
edefddZ
dee fddZ  ZS )
LazyLoadera  
    `LazyLoader` module borrowed from [Tensorflow]
    (https://github.com/tensorflow/tensorflow/blob/main/tensorflow/python/util/lazy_loader.py)
    with an addition of "module caching".

    Lazily import a module, mainly to avoid pulling in large dependencies.
    Modules such as `xgrammar` might do additional side effects, so we
    only want to use this when it is needed, delaying all eager effects.
    
local_nameparent_module_globalsr   c                    s&   || _ || _d | _t t| d S r   )_local_name_parent_module_globals_moduler   r   r|   )rD   r   r   r   r   r
   r   r   b  s   zLazyLoader.__init__r(   c              
   C   sZ   zt | j}|| j| j< |tj| j< W n ty# } z|d d }~ww | j	|j |S r   )
r   r+   rx   r   r   r   r   r    __dict__update)rD   r%   errr
   r
   r   _loadn  s   zLazyLoader._loaditemc                 C   s    | j d u r
|  | _ t| j |S r   )r   r   r,   )rD   r   r
   r
   r   rE     s   

zLazyLoader.__getattr__c                 C   s   | j d u r
|  | _ t| j S r   )r   r   dirrR   r
   r
   r   __dir__  s   


zLazyLoader.__dir__)rx   ry   rz   r{   r|   dictr   r   r   r   rE   listr   r   r
   r
   r   r   r   W  s    

r   c                 C   s   t j| duS )zReturn True if *module_name* can be found in the current environment.

    The result is cached so that subsequent queries for the same module incur
    no additional overhead.
    N)r   r   	find_spec)r   r
   r
   r   r     s   r   c                   C      t dS )z9Whether the optional `pplx_kernels` package is available.pplx_kernelsr   r
   r
   r
   r   has_pplx     r   c                   C   r   )z4Whether the optional `deep_ep` package is available.deep_epr   r
   r
   r
   r   has_deep_ep  r   r   c                   C   r   )z6Whether the optional `deep_gemm` package is available.	deep_gemmr   r
   r
   r
   r   has_deep_gemm  r   r   c                  C   s   t dpt d} | rt  | S )z;Whether the optional `triton_kernels` package is available.r   r   )r   r   )is_availabler
   r
   r   has_triton_kernels  s   
r   c                   C   r   )z5Whether the optional `tilelang` package is available.tilelangr   r
   r
   r
   r   has_tilelang  r   r   c                   C   r   )z=Whether the optional `arctic_inference` package is available.arctic_inferencer   r
   r
   r
   r   has_arctic_inference  s   r   c                   C   r   )a$  Whether the optional `helion` package is available.

    Helion is a Python-embedded DSL for writing ML kernels.
    See: https://github.com/pytorch/helion

    Usage:
        if has_helion():
            import helion
            import helion.language as hl
            # use helion...
    helionr   r
   r
   r
   r   
has_helion  s   r   c                   C   r   )z2Whether the optional `aiter` package is available.aiterr   r
   r
   r
   r   	has_aiter  r   r   c                   C   r   )z1Whether the optional `mori` package is available.morir   r
   r
   r
   r   has_mori  r   r   )*r{   importlib.metadatar   importlib.utilosr   	functoolsr   typesr   typingr   regexr2   typing_extensionsr   vllm.loggerr   rx   r   r   r   r|   PathLiker&   r.   r?   r@   r~   r   r   boolr   r   r   r   r   r   r   r   r   r   r
   r
   r
   r   <module>   sF    
	
 $4	
