o
    1 i|                     @   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- d dl.m,Z/m0Z1m2Z2m3Z4m5Z6m7Z8m-Z9 d dl:m;Z; de2de<fddZ=dedefddZ>G dd deZ0de
e?e(f dee
ee,e(f  fddZ@G dd dZ7d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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$   PYTHONZJAVA)r)   r*    r,   e/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/serve/_private/config.py_needs_pickle,   s
   r.   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 r,   )_proto_to_dict).0vr,   r,   r-   
<listcomp>I   s    z"_proto_to_dict.<locals>.<listcomp>)Z
ListFieldslabelr
   ZLABEL_REPEATEDtypeZTYPE_MESSAGEnamelistr1   Z
DESCRIPTORfieldsZcontaining_oneofdefault_value)r/   datafieldvaluer,   r,   r-   r1   9   s&   
r1   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 )/r#   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.
       )defaultZ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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__Zvalidate_assignmentZarbitrary_types_allowedr,   r,   r,   r-   Config   s    rR   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)clsr3   er,   r,   r-   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&   r,   )rU   dictrY   r6   Zray.serve.schemar&   )r\   r3   r&   r,   r,   r-   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.r>   rB   z@max_queued_requests must be -1 (no limit) or a positive integer.)rU   intrY   rZ   )r\   r3   r,   r,   r-   validate_max_queued_requests   s   
z-DeploymentConfig.validate_max_queued_requestsc                 C   s   t | j| jS N)r.   r)   r*   selfr,   r,   r-   needs_pickle   s   zDeploymentConfig.needs_picklec                 C   s&  |   }|dd ur|  rt|d |d< |dr(td
i |d |d< |dra|d d}|d urV|sAd|d d< n|  rOt||d d< ntd| td
i |d |d< |drd|d v ryt	|d d |d d< t
d
i |d |d< t|d	 |d	< td
i |S )NrD   rI   rJ   request_router_kwargs    zRNon-empty request_router_kwargs not supportedfor cross-language deployments. Got: rL   encodingrN   r,   )r_   getrf   r   rX   AutoscalingConfigProtorZ   RequestRouterConfigProtoEncodingTypeProtoValueLoggingConfigProtor8   DeploymentConfigProto)re   r;   Zrouter_kwargsr,   r,   r-   to_proto   sH   


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


zDeploymentConfig.to_protoc                 C      |    S rc   rq   ZSerializeToStringrd   r,   r,   r-   to_proto_bytes     zDeploymentConfig.to_proto_bytesr/   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*   FrD   rh   rJ   rg   rI   Zupscale_smoothing_factorZdownscale_smoothing_factorZupscaling_factorZdownscaling_factorZtarget_ongoing_requestsZaggregation_functionrK    rN   rL   ri   r,   )r1   r$   r+   r.   r   loadsrD   rJ   rg   r"   rj   r    ZMEANr!   setrm   Name)r\   r/   r;   r)   r*   rf   rg   r,   r,   r-   
from_proto  s   



zDeploymentConfig.from_protoproto_bytesc                 C   s   t |}| |S rc   )rp   
FromStringrz   )r\   r{   r/   r,   r,   r-   from_proto_bytesg  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: rT   c                 S   s    i | ]\}}|t jkr||qS r,   )r   VALUE)r2   keyvalr,   r,   r-   
<dictcomp>  s     z1DeploymentConfig.from_default.<locals>.<dictcomp>)rx   r_   keysitemsrY   r8   __setattr__)r\   kwargsconfigZvalid_config_optionsr   r   r,   r,   r-   from_defaultl  s    zDeploymentConfig.from_default)?rO   rP   rQ   __doc__r   r   ZLightWeightr@   r   r   __annotations__r   ZNeedsActorReconfigurerA   r   rC   ra   rD   r   r   ZNeedsReconfigurerE   r   r   rF   r   rG   r   r   rH   rI   r!   r"   rJ   r*   boolr$   r+   r)   ZHeavyWeightrK   r[   rL   r_   r   rM   rx   rN   r   rR   r   r^   r`   rb   rf   rq   rt   classmethodrp   rz   rV   r}   r   r,   r,   r,   r-   r#   _   s   
 #





&Hr#   rA   rI   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)Zexclude_unsetr,   )r   r~   r!   r?   r_   rU   update)rA   rI   Zdefault_configr,   r,   r-   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_noderf   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_dictrf   )
re   r   r   r   r   r   r   r   r   rf   r,   r,   r-   __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   rZ   rd   r,   r,   r-   r     s   

zReplicaConfig._validatec                 C   s0   || _ || _|| _|| _|   t| j | _d S rc   )r   r   r   r   r   r   r   )re   r   r   r   r   r,   r,   r-   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.r,   z#Could not serialize the deployment z,Could not serialize the deployment init argsz.Could not serialize the deployment init kwargs)callablerU   r[   rY   tupler8   r_   inspect
isfunctionrZ   r   r6   rO   r   reprr   r   r   )
r\   r   r   r   r   r   r   r   r   r   r,   r,   r-   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.>   	resourcesZmemoryZruntime_envnum_gpusnum_cpusZaccelerator_typezSpecifying 'z8' in ray_actor_options is not allowed. Allowed options: T)
in_optionsr   r>   )	rU   r   r_   rY   r6   rZ   r   Zvalidate_actor_optionsrj   )re   Zallowed_ray_actor_optionsoptionr,   r,   r-   r   V  s"   
z)ReplicaConfig._validate_ray_actor_optionsr0   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].r>   zInvalid max_replicas_per_node z:. Valid values are None or an integer in the range of [1, )r   rU   ra   rY   r6   r   rZ   rd   r,   r,   r-   r   u  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.ZPACKZdetached)ZbundlesZstrategyZ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   ZCPUr   z `num_cpus` for the actor is z, but the bundle only has z `CPU` specified.ZGPUr   z `num_gpus` for the actor is z `GPU` specified.r   z `z` requirement for the actor is z` specified.)r   r   rZ   r(   rj   r   r   )re   Zresource_error_prefixZfirst_bundleZ
bundle_cpuZreplica_actor_num_cpusZ
bundle_gpuZreplica_actor_num_gpusZreplica_actor_resourcesZactor_resourceZactor_valueZbundle_valuer,   r,   r-   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)ri   )r   rf   r   rw   r   decoderd   r,   r,   r-   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   rf   r   rw   r   rd   r,   r,   r-   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   rw   r   rd   r,   r,   r-   r     s   
zReplicaConfig.init_kwargsr/   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 )Nrh   rv   )r'   r   r   r   r   rW   rw   r   r   r   r   )r\   r/   rf   r,   r,   r-   rz     s$   


zReplicaConfig.from_protor{   c                 C   s   t |}| ||S rc   )ReplicaConfigProtor|   rz   )r\   r{   rf   r/   r,   r,   r-   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 )Nrv   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   rW   rX   r   r   r   r   rd   r,   r,   r-   rq     s   


zReplicaConfig.to_protoc                 C   rr   rc   rs   rd   r,   r,   r-   rt     ru   zReplicaConfig.to_proto_bytes)NNNT)NNN)NNNNNNN)r0   N)T)"rO   rP   rQ   r   r[   rV   r   r   r   floatra   r   r   r   r_   r   r   r	   r   r   r   r   r   r   r   propertyr   r   r   r   rz   r}   rq   rt   r,   r,   r,   r-   r'     s    	

-


	C

5 r'   )Ar   rW   typingr   r   r   r   r   r   r   r	   Zgoogle.protobuf.descriptorr
   Zgoogle.protobuf.messager   Zrayr   Zray._commonr   Zray._common.pydantic_compatr   r   r   r   r   r   r   Zray._common.serializationr   Zray._common.utilsr   Zray.serve._private.constantsr   r   r   r   r   r   r   Zray.serve._private.utilsr   r   Zray.serve.configr    r!   r"   Zray.serve.generated.serve_pb2rk   r#   rp   r$   r%   rm   r&   ro   r'   r   rl   Zray.util.placement_groupr(   r   r.   r1   ra   r   r,   r,   r,   r-   <module>   s4    ($	$	$	&  /

 