o
    $ij                     @   s  d dl Z d dl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 d dlmZ d dl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!m"Z"m#Z#m$Z$m%Z%m&Z& d dl'm(Z(m)Z) d dl*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 d dl1m,Z2m3Z4m5Z5m6Z7m8Z9m:Z;m0Z< d dl=m>Z> de5de?fddZ@dedefddZAG dd deZ3de
eBe(f dee
ee,e(f  fddZCG dd dZ:de
deDe/f d e
deEe.f de.fd!d"ZFdS )#    N)AnyCallableDictListOptionalSetTupleUnion)FieldDescriptor)Message)cloudpickle)ray_option_utils)	BaseModelFieldNonNegativeFloatNonNegativeIntPositiveFloatPositiveInt	validator)pickle_dumps)resources_from_ray_options)DEFAULT_CONSTRUCTOR_RETRY_COUNT#DEFAULT_GRACEFUL_SHUTDOWN_TIMEOUT_S%DEFAULT_GRACEFUL_SHUTDOWN_WAIT_LOOP_SDEFAULT_HEALTH_CHECK_PERIOD_SDEFAULT_HEALTH_CHECK_TIMEOUT_SDEFAULT_MAX_ONGOING_REQUESTSMAX_REPLICAS_PER_NODE_MAX_VALUE)DEFAULTDeploymentOptionUpdateType)AggregationFunctionAutoscalingConfigDeploymentModeHTTPOptionsProxyLocationRequestRouterConfig)r!   DeploymentConfigDeploymentLanguageEncodingTypeLoggingConfigReplicaConfigr%   )validate_placement_groupdeployment_languageis_cross_languagec                 C   s(   | t jkr	|s	dS | t jkr|rdS dS )zGFrom Serve client API's perspective, decide whether pickling is needed.TF)r'   PYTHONJAVA)r,   r-    r0   V/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/serve/_private/config.py_needs_pickle3   s
   r2   protoreturnc                 C   s   i }|   D ]6\}}|jtjkr)|jtjkr!dd |D ||j< qt|||j< q|jtjkr7t|||j< q|||j< q| j	j
D ]}|j|vrW|jtjkrW|jsW|j||j< qA|S )zRecursively convert a protobuf into a Python dictionary.

    This is an alternative to protobuf's `MessageToDict`. Unlike
    `MessageToDict`, this function doesn't add an extra base64
    encoding to bytes when constructing a json response.
    c                 S   s   g | ]}t |qS r0   )_proto_to_dict).0vr0   r0   r1   
<listcomp>P   s    z"_proto_to_dict.<locals>.<listcomp>)
ListFieldslabelr
   LABEL_REPEATEDtypeTYPE_MESSAGEnamelistr5   
DESCRIPTORfieldscontaining_oneofdefault_value)r3   datafieldvaluer0   r0   r1   r5   @   s&   
r5   c                   @   s  e Zd ZU dZedejdZee	 e
d< eeejdZee
d< edejdZee
d< edejdZee
d	< eeejdZee
d
< eeejdZee
d< eeejdZee
d< eeejdZee
d< edejdZee e
d< ee ejdZ!e e
d< dZ"e#e
d< e$j%Z&ee
d< edej'dZ(ee) e
d< edejdZ*ee+ e
d< ee,ejdZ-ee
d< e. Z/e0e) e
d< G dd dZ1e2d	dddd Z3e2ddddd Z4e2dddd d! Z5d"d# Z6d$d% Z7d&d' Z8e9d(e:fd)d*Z;e9d+e<fd,d-Z=e9d.d/ Z>dS )0r&   a  Internal datastructure wrapping config options for a deployment.

    Args:
        num_replicas: The number of processes to start up that
            handles requests to this deployment. Defaults to 1.
        max_ongoing_requests: The maximum number of queries
            that is sent to a replica of this deployment without receiving
            a response. Defaults to 5.
        max_queued_requests: Maximum number of requests to this deployment that will be
            queued at each *caller* (proxy or DeploymentHandle). Once this limit is
            reached, subsequent requests will raise a BackPressureError (for handles) or
            return an HTTP 503 status code (for HTTP requests). Defaults to -1 (no
            limit).
        user_config: Arguments to pass to the reconfigure
            method of the deployment. The reconfigure method is called if
            user_config is not None. Must be JSON-serializable.
        graceful_shutdown_wait_loop_s: Duration
            that deployment replicas wait until there is no more work to
            be done before shutting down.
        graceful_shutdown_timeout_s: Controller waits for this duration
            to forcefully kill the replica for shutdown.
        health_check_period_s: Frequency at which the controller health
            checks replicas.
        health_check_timeout_s: Timeout that the controller waits for a
            response from the replica's health check before marking it
            unhealthy.
        autoscaling_config: Autoscaling configuration.
        logging_config: Configuration for deployment logs.
        user_configured_option_names: The names of options manually
            configured by the user.
        request_router_config: Configuration for deployment request router.
        max_constructor_retry_count: Maximum number of times to retry the
            deployment constructor. Defaults to 20.
       )defaultupdate_typenum_replicasmax_ongoing_requestsmax_queued_requestsNuser_configgraceful_shutdown_timeout_sgraceful_shutdown_wait_loop_shealth_check_period_shealth_check_timeout_sautoscaling_config)default_factoryrI   request_router_configFr-   r,   versionlogging_configmax_constructor_retry_countuser_configured_option_namesc                   @   s   e Zd ZdZdZdS )zDeploymentConfig.ConfigTN)__name__
__module____qualname__validate_assignmentarbitrary_types_allowedr0   r0   r0   r1   Config   s    r_   T)alwaysc              
   C   sX   t |tr|S |d ur*zt| W |S  ty) } z
tdt| dd }~ww |S )Nz&user_config is not JSON-serializable: .)
isinstancebytesjsondumps	TypeError
ValueErrorstr)clsr7   er0   r0   r1   user_config_json_serializable   s   
z.DeploymentConfig.user_config_json_serializablec                 C   sL   |d u r|S t |tstdt| dddlm} |di | }|S )NzGot invalid type 'z,' for logging_config. Expected a dictionary.r   )r)   r0   )rb   dictrf   r<   ray.serve.schemar)   )ri   r7   r)   r0   r0   r1   logging_config_valid   s   
z%DeploymentConfig.logging_config_validc                 C   s.   t |ts	td|dk r|dkrtd|S )Nz'max_queued_requests must be an integer.rG   rL   z@max_queued_requests must be -1 (no limit) or a positive integer.)rb   intrf   rg   )ri   r7   r0   r0   r1   validate_max_queued_requests   s   
z-DeploymentConfig.validate_max_queued_requestsc                 C   s   t | j| jS N)r2   r,   r-   selfr0   r0   r1   needs_pickle   s   zDeploymentConfig.needs_picklec                 C   sL  |   }|dd ur|  rt|d |d< |dr3| jjj|d d d< tdi |d |d< |drt|d d}|d ura|sLd|d d< n|  rZt||d d< nt	d| | j
j|d d	< tdi |d |d< |d
rd|d
 v rt|d
 d |d
 d< tdi |d
 |d
< t|d |d< tdi |S )NrN   rS   policy_serialized_policy_defrU   request_router_kwargs    zRNon-empty request_router_kwargs not supportedfor cross-language deployments. Got: _serialized_request_router_clsrW   encodingrY   r0   )rl   getrt   r   re   rS   ru   rv   AutoscalingConfigProtorg   rU   ry   RequestRouterConfigProtoEncodingTypeProtoValueLoggingConfigProtor?   DeploymentConfigProto)rs   rD   router_kwargsr0   r0   r1   to_proto   sX   






zDeploymentConfig.to_protoc                 C      |    S rq   r   SerializeToStringrr   r0   r0   r1   to_proto_bytes,     zDeploymentConfig.to_proto_bytesr3   c                 C   s"  t |}d|v r|d ntj}d|v r|d nd}t||}d|v r=|d dkr9|r3t|j|d< n
|j|d< nd |d< d|v ryd|d v rn|d d }|dkrh|r_t|jj|d d< n|jj|d d< ni |d d< t	di |d |d< d|v r|d 
d	sd |d d	< |d 
d
sd |d d
< |d 
dsd |d d< |d 
dsd |d d< |d 
dsd |d d< |d 
dstj|d d< tdi |d |d< d|v r|d dkrd |d< d|v rt|d |d< d|v r
d|d v r
t|d d |d d< | di |S )Nr,   r-   FrN   rx   rU   rw   rS   upscale_smoothing_factordownscale_smoothing_factorupscaling_factordownscaling_factortarget_ongoing_requestsaggregation_functionrV    rY   rW   rz   r0   )r5   r'   r.   r2   r   loadsrN   rU   rw   r%   r{   r    MEANr!   setr~   Name)ri   r3   rD   r,   r-   rt   rw   r0   r0   r1   
from_proto/  s   



zDeploymentConfig.from_protoproto_bytesc                 C   s   t |}| |S rq   )r   
FromStringr   )ri   r   r3   r0   r0   r1   from_proto_bytesx  s   

z!DeploymentConfig.from_proto_bytesc              	   K   s   |  }t |  }| D ]\}}||vr'td| d| dt| dqdd | D }| D ]
\}}||| q5|S )zCreates a default DeploymentConfig and overrides it with kwargs.

        Ignores any kwargs set to DEFAULT.VALUE.

        Raises:
            TypeError: when a keyword that's not an argument to the class is
                passed in.
        z&Got invalid Deployment config option "z" (with value zO) as keyword argument. All Deployment config options must come from this list: ra   c                 S   s    i | ]\}}|t jkr||qS r0   )r   VALUE)r6   keyvalr0   r0   r1   
<dictcomp>  s     z1DeploymentConfig.from_default.<locals>.<dictcomp>)r   rl   keysitemsrf   r?   __setattr__)ri   kwargsconfigvalid_config_optionsr   r   r0   r0   r1   from_default}  s    zDeploymentConfig.from_default)?rZ   r[   r\   __doc__r   r   LightWeightrJ   r   r   __annotations__r   NeedsActorReconfigurerK   r   rM   ro   rN   r   r   NeedsReconfigurerO   r   r   rP   r   rQ   r   r   rR   rS   r!   r%   rU   r-   boolr'   r.   r,   HeavyWeightrV   rh   rW   rl   r   rX   r   rY   r   r_   r   rk   rn   rp   rt   r   r   classmethodr   r   rc   r   r   r0   r0   r0   r1   r&   f   s   
 #





0Hr&   rK   rS   c                 C   sh   |t jdfv rt }| |fS t jdd}t|tr|n|jdd}|| tdi |}| |fS )aF  Return modified `max_ongoing_requests` and `autoscaling_config`
    for when num_replicas="auto".

    If `autoscaling_config` is unspecified, returns the modified value
    AutoscalingConfig.default().
    If it is specified, the specified fields in `autoscaling_config`
    override that of AutoscalingConfig.default().
    NTexclude_unsetr0   )r   r   r!   rH   rl   rb   update)rK   rS   default_configr0   r0   r1   handle_num_replicas_auto  s   

r   c                   @   s  e Zd ZdZ				d/dededededed	eeeee	f   d
ee dee
 defddZdd Z			d0ded	eeeee	f   d
ee dee
 fddZe							d1deeef deee  deeeef  dee d	eeeee	f   d
ee dee
 dee fddZdd Zd2ddZd2ddZedeeef fdd Zedeeee ef  fd!d"Zedeee  fd#d$Zed3d%edefd&d'Zed3d(edefd)d*Zd+d, Z d-d. Z!dS )4r*   a>  Internal datastructure wrapping config options for a deployment's replicas.

    Provides five main properties (see property docstrings for more info):
        deployment_def: the code, or a reference to the code, that this
            replica should run.
        init_args: the deployment_def's init_args.
        init_kwargs: the deployment_def's init_kwargs.
        ray_actor_options: the Ray actor options to pass into the replica's
            actor.
        resource_dict: contains info on this replica's actor's resource needs.

    Offers a serialized equivalent (e.g. serialized_deployment_def) for
    deployment_def, init_args, and init_kwargs. Deserializes these properties
    when they're first accessed, if they were not passed in directly through
    create().

    Use the classmethod create() to make a ReplicaConfig with the deserialized
    properties.

    Note: overwriting or setting any property after the ReplicaConfig has been
    constructed is currently undefined behavior. The config's fields should not
    be modified externally after it is created.
    NTdeployment_def_nameserialized_deployment_defserialized_init_argsserialized_init_kwargsray_actor_optionsplacement_group_bundlesplacement_group_strategymax_replicas_per_nodert   c
           
      C   s`   || _ || _|| _|| _d| _d| _d| _|| _|| _|| _	|| _
|   t| j| _|	| _dS )zConstruct a ReplicaConfig with serialized properties.

        All parameters are required. See classmethod create() for defaults.
        N)r   r   r   r   _deployment_def
_init_args_init_kwargsr   r   r   r   	_validater   resource_dictrt   )
rs   r   r   r   r   r   r   r   r   rt   r0   r0   r1   __init__  s   
zReplicaConfig.__init__c                 C   s<   |    |   |   | jd ur| jd urtdd S d S )NzVSetting max_replicas_per_node is not allowed when placement_group_bundles is provided.)_validate_ray_actor_options!_validate_placement_group_options_validate_max_replicas_per_noder   r   rg   rr   r0   r0   r1   r     s   

zReplicaConfig._validatec                 C   s0   || _ || _|| _|| _|   t| j | _d S rq   )r   r   r   r   r   r   r   )rs   r   r   r   r   r0   r0   r1   r     s   zReplicaConfig.updatedeployment_def	init_argsinit_kwargsc	           
   	   C   s   t |st|tstd|du st|ttfstd|du s)t|ts)tdt|r:|r4t	d|r:t	dt|t
tfsKtdt| d|du rQd	}|du rWi }|du r]i }|du rlt|tri|}n|j}| |t|d
t| t|dt|d||||}	||	_||	_||	_|	S )z4Create a ReplicaConfig from deserialized parameters.z8@serve.deployment must be called on a class or function.Nzinit_args must be a tuple.zinit_kwargs must be a dict.z1init_args not supported for function deployments.z3init_kwargs not supported for function deployments.Got invalid type "zQ" for deployment_def. Expected deployment_def to be a class, function, or string.r0   z#Could not serialize the deployment z,Could not serialize the deployment init argsz.Could not serialize the deployment init kwargs)callablerb   rh   rf   tupler?   rl   inspect
isfunctionrg   r   r<   rZ   r   reprr   r   r   )
ri   r   r   r   r   r   r   r   r   r   r0   r0   r1   create#  sT   

zReplicaConfig.createc                 C   s   t | jtstdt| j dh d}| jD ]}||vr(td| d| qtj| jdd | jdd u r@d	| jd< d S d S )
Nr   z/" for ray_actor_options. Expected a dictionary.>   memorynum_cpusnum_gpus	resourcesruntime_envaccelerator_typezSpecifying 'z8' in ray_actor_options is not allowed. Allowed options: T)
in_optionsr   rG   )	rb   r   rl   rf   r<   rg   r   validate_actor_optionsr{   )rs   allowed_ray_actor_optionsoptionr0   r0   r1   r   g  s"   
z)ReplicaConfig._validate_ray_actor_optionsr4   c                 C   sf   | j d u rd S t| j tstdt| j  dt d| j dk s%| j tkr1td| j  dt dd S )NzGet invalid type 'zM' for max_replicas_per_node. Expected None or an integer in the range of [1, z].rG   zInvalid max_replicas_per_node z:. Valid values are None or an integer in the range of [1, )r   rb   ro   rf   r<   r   rg   rr   r0   r0   r1   r     s    



z-ReplicaConfig._validate_max_replicas_per_nodec                 C   s$  | j d ur| jd u rtd| jd urt| j| j pddd d}| jd }|dd}| jdd}||k rCt| d	| d
| d|dd}| jdd}||k rat| d| d
| d| jdi }| D ]#\}}	||d}
|
|	k rt| d| d|	 d
|
 d| d
qld S d S )Nz[If `placement_group_strategy` is provided, `placement_group_bundles` must also be provided.PACKdetached)bundlesstrategylifetimezWhen using `placement_group_bundles`, the replica actor will be placed in the first bundle, so the resource requirements for the actor must be a subset of the first bundle.r   CPUr   z `num_cpus` for the actor is z, but the bundle only has z `CPU` specified.GPUr   z `num_gpus` for the actor is z `GPU` specified.r   z `z` requirement for the actor is z` specified.)r   r   rg   r+   r{   r   r   )rs   resource_error_prefixfirst_bundle
bundle_cpureplica_actor_num_cpus
bundle_gpureplica_actor_num_gpusreplica_actor_resourcesactor_resourceactor_valuebundle_valuer0   r0   r1   r     sd   



$z/ReplicaConfig._validate_placement_group_optionsc                 C   s:   | j du r| jrt| j| _ | j S | jjdd| _ | j S )aD  The code, or a reference to the code, that this replica runs.

        For Python replicas, this can be one of the following:
            - Function (Callable)
            - Class (Callable)
            - Import path (str)

        For Java replicas, this can be one of the following:
            - Class path (str)
        Nzutf-8)rz   )r   rt   r   r   r   decoderr   r0   r0   r1   r     s   
zReplicaConfig.deployment_defc                 C   s2   | j du r| jrt| j| _ | j S | j| _ | j S )zThe init_args for a Python class.

        This property is only meaningful if deployment_def is a Python class.
        Otherwise, it is None.
        N)r   rt   r   r   r   rr   r0   r0   r1   r     s   
zReplicaConfig.init_argsc                 C   s   | j du rt| j| _ | j S )zThe init_kwargs for a Python class.

        This property is only meaningful if deployment_def is a Python class.
        Otherwise, it is None.
        N)r   r   r   r   rr   r0   r0   r1   r     s   
zReplicaConfig.init_kwargsr3   c              
   C   sv   t |j|j|jdkr|jnd |jdkr|jnd t|j|jr%t|jnd |j	dkr.|j	nd |j
r7|j
|	S d |	S )Nrx   r   )r*   r   r   r   r   rd   r   r   r   r   r   )ri   r3   rt   r0   r0   r1   r      s$   


zReplicaConfig.from_protor   c                 C   s   t |}| ||S rq   )ReplicaConfigProtor   r   )ri   r   rt   r3   r0   r0   r1   r     s   
zReplicaConfig.from_proto_bytesc              
   C   sT   t | j| j| j| jt| j| jd urt| jnd| j	| j
d ur&| j
dS ddS )Nr   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   rd   re   r   r   r   r   rr   r0   r0   r1   r     s   


zReplicaConfig.to_protoc                 C   r   rq   r   rr   r0   r0   r1   r   '  r   zReplicaConfig.to_proto_bytes)NNNT)NNN)NNNNNNN)r4   N)T)"rZ   r[   r\   r   rh   rc   r   r   r   floatro   r   r   r   rl   r   r   r	   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   r   r   r0   r0   r0   r1   r*     s    	

-


	C

5 r*   proxy_locationhttp_optionsc                 C   s   |du r
d}t  }n/t|trd|v }t di |}nt|t r.d}t di |jdd}ntdt|j d| du rE|sCtj|_|S t	
| |_|S )	a  Prepare `HTTPOptions` with a resolved `location` based on `proxy_location` and `http_options`.

    Precedence:
    - If `proxy_location` is provided, it overrides any `location` in `http_options`.
    - Else if `http_options` specifies a `location` explicitly (HTTPOptions(...) or dict with 'location'), keep it.
    - Else (no `proxy_location` and no explicit `location`) set `location` to `DeploymentMode.EveryNode`.
      A bare `HTTPOptions()` counts as an explicit default (`HeadOnly`).

    Args:
        proxy_location: Optional ProxyLocation (or its string representation).
        http_options: Optional HTTPOptions instance or dict. If None, a new HTTPOptions() is created.

    Returns:
        HTTPOptions: New instance with resolved location.

    Note:
        1. Default ProxyLocation (when unspecified) resolves to DeploymentMode.EveryNode.
        2. Default HTTPOptions() location is DeploymentMode.HeadOnly.
        3. `HTTPOptions` is used in `imperative` mode (Python API) cluster set-up.
            `Declarative` mode (CLI / REST) uses `HTTPOptionsSchema`.

    Raises:
        ValueError: If http_options is not None, dict, or HTTPOptions.
    NFlocationTr   z#Unexpected type for http_options: ``r0   )r#   rb   rl   rg   r<   rZ   r"   	EveryNoder   r$   _to_deployment_mode)r   r   location_set_explicitlyr0   r0   r1   prepare_imperative_http_options+  s$   

r   )Gr   rd   typingr   r   r   r   r   r   r   r	   google.protobuf.descriptorr
   google.protobuf.messager   rayr   ray._commonr   ray._common.pydantic_compatr   r   r   r   r   r   r   ray._common.serializationr   ray._common.utilsr   ray.serve._private.constantsr   r   r   r   r   r   r   ray.serve._private.utilsr   r   ray.serve.configr    r!   r"   r#   r$   r%   ray.serve.generated.serve_pb2r|   r&   r   r'   r(   r~   r)   r   r*   r   r}   ray.util.placement_groupr+   r   r2   r5   ro   r   rh   rl   r   r0   r0   r0   r1   <module>   sF    ($	$	 $	&  9

   p