o
    `۷i                      @   s  d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
mZmZmZ d dlmZ d dlmZ d dlmZmZ d dlmZ d d	lmZ e eZed
ZedZG dd dee	eef ZeddG dd dZdede defddZ!dddddddede dee  deeeef  deee ef  deeee gef  de"defd d!Z#dd"dede d#eee f d$eeef deee ef  deee gef de
e fd%d&Z$d'edeee ef  defd(d)Z%ded#eee f de fd*d+Z&dS ),    N)deepcopy)	dataclass)AnyCallableDictGenericListOptionalTypeVarUnion)_PyObjScanner)SERVE_LOGGER_NAME)Application
Deployment)DeploymentHandle)LoggingConfigKVc                       sd   e Zd ZdZdedef fddZdedef fddZdef fd	d
Zde	f fddZ
  ZS )IDDictzDictionary that uses id() for keys instead of hash().

    This is necessary because Application objects aren't hashable and we want each
    instance to map to a unique key.
    keyreturnc                       t |ts	t|}t |S N)
isinstanceintidsuper__getitem__selfr   	__class__ R/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/serve/_private/build_app.pyr         
zIDDict.__getitem__valuec                    s    t |ts	t|}t ||S r   )r   r   r   r   __setitem__)r   r   r%   r    r"   r#   r&      s   
zIDDict.__setitem__c                    r   r   )r   r   r   r   __delitem__r   r    r"   r#   r'   #   r$   zIDDict.__delitem__c                    r   r   )r   r   r   r   __contains__r   r    r"   r#   r(   (   r$   zIDDict.__contains__)__name__
__module____qualname____doc__r   r   r   r&   r'   objectr(   __classcell__r"   r"   r    r#   r      s    r   T)frozenc                   @   sZ   e Zd ZU eed< ee ed< ee ed< eed< ee ed< e	ee
f ed< eed< dS )	BuiltApplicationnameroute_prefixlogging_configingress_deployment_namedeploymentsdeployment_handlesexternal_scaler_enabledN)r)   r*   r+   str__annotations__r	   r   r   r   r   r   boolr"   r"   r"   r#   r0   .   s   
 r0   
deploymentapp_namer   c                 C   s   t | j|dS )N)r<   )r   r1   )r;   r<   r"   r"   r#   _make_deployment_handle_default?   s   r=   F)r2   r3   default_runtime_envmake_deployment_handler7   appr1   r2   r3   r>   r?   r7   c          	   	      sX   |du rt }t }t  t| || ||d}t||| |  | fdd| D |dS )a  Builds the application into a list of finalized deployments.

    The following transformations are made:
        - Application objects in constructor args/kwargs are converted to
          DeploymentHandles for injection at runtime.
        - Name conflicts from deployments that use the same class are handled
          by appending a monotonically increasing suffix (e.g., SomeClass_1).

    Returns: BuiltApplication
    N)r<   handlesdeployment_namesr>   r?   c                    s   i | ]	\}} | |qS r"   r"   ).0r@   handlerB   r"   r#   
<dictcomp>q   s    zbuild_app.<locals>.<dictcomp>)r1   r2   r3   r4   r5   r6   r7   )r=   r   _build_app_recursiver0   items)	r@   r1   r2   r3   r>   r?   r7   rA   r5   r"   rE   r#   	build_appH   s.   
rI   )r>   rB   rA   c                C   s   | |v rg S g }t td}zF|| jj| jjf}|D ]}	|t|	|||||d q||\}
}| jj	t
| ||
|d}t||}||||| < ||g W |  S |  w )aN  Recursively traverses the graph of Application objects.

    Each Application will have an associated DeploymentHandle created that will replace
    it in any occurrences in other Applications' args or kwargs.

    Also collects a list of the unique Applications encountered and returns them as
    deployable Deployment objects.
    )source_type)r<   rA   rB   r?   r>   )r1   
_init_args_init_kwargs)r   r   
find_nodes_bound_deployment	init_argsinit_kwargsextendrG   replace_nodesoptions$_get_unique_deployment_name_memoized_set_default_runtime_envclear)r@   r<   rB   rA   r>   r?   r5   scanner
child_apps	child_appnew_init_argsnew_init_kwargsfinal_deploymentr"   r"   r#   rG   x   sD   

rG   dc                 C   sX   |s| S t | jp	i }|dd}d|vr||d< n|dur&|d d| | j|dS )ah  Configures the deployment with the provided default runtime_env.

    If the deployment does not have a runtime_env configured, the default will be set.

    If it does have a runtime_env configured but that runtime_env does not have a
    working_dir, only the working_dir field will be set.

    Else the deployment's runtime_env will be left untouched.
    working_dirNruntime_env)ray_actor_options)r   r`   get
setdefaultrS   )r]   r>   r`   default_working_dirr"   r"   r#   rU      s   
rU   c                 C   s~   | |v r||  S d}| j j}|| v r'| j j d| }|d7 }|| v s|dkr9td| j j d| d ||| < |S )a  Generates a name for the deployment.

    This is used to handle collisions when the user does not specify a name
    explicitly, so typically we'd use the class name as the default.

    In that case, we append a monotonically increasing suffix to the name, e.g.,
    Deployment, then Deployment_1, then Deployment_2, ...

    Names are memoized in the `deployment_names` dict, which should be passed to
    subsequent calls to this function.
       _z3There are multiple deployments with the same name 'z'. Renaming one to 'z'.)rN   r1   valuesloggerwarning)r@   rB   idxr1   r"   r"   r#   rT      s$   rT   )'loggingcopyr   dataclassesr   typingr   r   r   r   r   r	   r
   r   ray.dag.py_obj_scannerr   ray.serve._private.constantsr   ray.serve.deploymentr   r   ray.serve.handler   ray.serve.schemar   	getLoggerrg   r   r   dictr   r0   r8   r=   r:   rI   rG   rU   rT   r"   r"   r"   r#   <module>   s    (



6


A

