o
    `۷iM                     @   s  d dl Z d dl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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mZmZ d d
lmZ e  eZ!eddG dd dZ"eddG dd dZ#de#defddZ$dede#fddZ%dS )    N)deepcopy)AnyCallableDictListOptionalTupleUnion)DeploymentConfigReplicaConfigRequestRouterConfighandle_num_replicas_auto)SERVE_LOGGER_NAME)ServeUsageTag)DEFAULTDefault)AutoscalingConfig)DeploymentSchemaLoggingConfigRayActorOptionsSchema)	PublicAPIstable)	stabilityc                   @   s   e Zd ZdZdddZdS )Applicationa  One or more deployments bound with arguments that can be deployed together.

    Can be passed into another `Deployment.bind()` to compose multiple deployments in a
    single application, passed to `serve.run`, or deployed via a Serve config file.

    For example, to define an Application and run it in Python:

        .. code-block:: python

            from ray import serve
            from ray.serve import Application

            @serve.deployment
            class MyDeployment:
                pass

            app: Application = MyDeployment.bind(OtherDeployment.bind())
            serve.run(app)

    To run the same app using the command line interface (CLI):

        .. code-block:: bash

            serve run python_file:app

    To deploy the same app via a config file:

        .. code-block:: yaml

            applications:
                my_app:
                    import_path: python_file:app

    bound_deployment
Deploymentc                 C   s
   || _ d S N)_bound_deployment)selfr    r   J/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/serve/deployment.py__init__;   s   
zApplication.__init__N)r   r   )__name__
__module____qualname____doc__r!   r   r   r   r    r      s    #r   c                3   @   s  e Zd ZdZ		dKdedededee ddf
d	d
ZdefddZ	e
defddZe
dee fddZe
deeef fddZe
defddZe
defddZe
defddZe
defddZe
dd Ze
dee fddZe
dee fdd Ze
dee fd!d"Ze
dee fd#d$Ze
defd%d&Zd'efd(d)Zd*d+ Zde fd,d-Z!de"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#de"j#fd.ee de$e de$e d/e$eeeef   d0e$eedf  d1e$ee  d2e$e%eee&f   d3e$e d4e$e%eeef   d5e$e d6e$ee  d7e$e d8e$e d9e$eee'df  d:e$e& d;e$e& d<e$e& d=e$e& d'e$eee(df  d>e$eee)df  d?e$ee  d@e$eeef  dAe*dBe$e dd f2dCdDZ+dEdF Z,dGdH Z-dIdJ Z.dS )Lr   a  Class (or function) decorated with the `@serve.deployment` decorator.

    This is run on a number of replica actors. Requests to those replicas call
    this class.

    One or more deployments can be composed together into an `Application` which is
    then run via `serve.run` or a config file.

    Example:

    .. code-block:: python

        @serve.deployment
        class MyDeployment:
            def __init__(self, name: str):
                self._name = name

            def __call__(self, request):
                return "Hello world!"

        app = MyDeployment.bind()
        # Run via `serve.run` or the `serve run` CLI command.
        serve.run(app)

    NFnamedeployment_configreplica_configversionreturnc                 C   sL   |st d| | |d u st|tstd|| _|| _|| _|| _d S )NzZThe Deployment constructor should not be called directly. Use `@serve.deployment` instead.zversion must be a string.)	RuntimeError_validate_name
isinstancestr	TypeError_name_version_deployment_config_replica_config)r   r&   r'   r(   r)   	_internalr   r   r    r!   \   s   

zDeployment.__init__c                 C   s4   t |ts	tdd|v rtd| d d S d S )Nzname must be a string.#zvDeployment names should not contain the '#' character, this will raise an error starting in Ray 2.46.0. Current name: .)r-   r.   r/   warningswarn)r   r&   r   r   r    r,   r   s   
zDeployment._validate_namec                 C      | j S )zUnique name of this deployment.r0   r   r   r   r    r&   }   s   zDeployment.namec                 C   r9   r   )r1   r;   r   r   r    r)      s   zDeployment.versionc                 C      | j jS )z8Underlying class or function that this deployment wraps.)r3   deployment_defr;   r   r   r    func_or_class      zDeployment.func_or_classc                 C   r<   )zTarget number of replicas.)r2   num_replicasr;   r   r   r    r@      r?   zDeployment.num_replicasc                 C   r<   )z%Dynamic user-provided config options.)r2   user_configr;   r   r   r    rA      r?   zDeployment.user_configc                 C   r<   )z4Max number of requests a replica can handle at once.)r2   max_ongoing_requestsr;   r   r   r    rB      r?   zDeployment.max_ongoing_requestsc                 C   r<   )zDMax number of requests that can be queued in each deployment handle.)r2   max_queued_requestsr;   r   r   r    rC      r?   zDeployment.max_queued_requestsc                 C      t d)N`route_prefix` can no longer be specified at the deployment level. Pass it to `serve.run` or in the application config instead.)
ValueErrorr;   r   r   r    route_prefix   s   zDeployment.route_prefixc                 C   r<   )z:Actor options such as resources required for each replica.)r3   ray_actor_optionsr;   r   r   r    rH      r?   zDeployment.ray_actor_optionsc                 C   r<   r   )r3   	init_argsr;   r   r   r    rI         zDeployment.init_argsc                 C   r<   r   )r3   init_kwargsr;   r   r   r    rK      rJ   zDeployment.init_kwargsc                 C   s   t d d S )NzUDeprecationWarning: `Deployment.url` is deprecated and will be removed in the future.)loggerwarningr;   r   r   r    url   s   zDeployment.urlc                 C   r<   r   r2   logging_configr;   r   r   r    rP      rJ   zDeployment.logging_configrP   c                 C   s   || j _d S r   rO   )r   rP   r   r   r    set_logging_config   s   zDeployment.set_logging_configc                 C   rD   )NzNDeployments cannot be constructed directly. Use `deployment.deploy() instead.`)r+   r;   r   r   r    __call__   s   zDeployment.__call__c                 O   s   t | j||dS )zBind the arguments to the deployment and return an Application.

        The returned Application can be deployed using `serve.run` (or via
        config file) or bound to another deployment for composition.
        )
_init_args_init_kwargs)r   options)r   argskwargsr   r   r    bind   s   zDeployment.bindr>   r@   rG   rH   placement_group_bundlesplacement_group_strategy%placement_group_bundle_label_selectormax_replicas_per_noderA   rB   rC   autoscaling_configgraceful_shutdown_wait_loop_sgraceful_shutdown_timeout_shealth_check_period_shealth_check_timeout_srequest_router_configrS   rT   r4   max_constructor_retry_countc                 C   s  |t jur	td|du rtd|dkr$d}t||\}}tjd dd t  D }t	| j
}|s;|j| |t jddfvrN|t jdfvrNtd|d	krVtd
|sc|t jurctd n
|t jdfvrm||_|t juru||_|t jur}||_|t jur||_|t jur||_|du r| jj}|t ju r| j}|t ju r| j}|t ju r| jj}|t ju r| jj}|t ju r| jj}|t ju r| jj}|t ju r| jj}|	t ju r| jj}	| jj}|
t ju r| jj}
|t jur||_ |t jur||_!|t jur||_"|t jur
||_#|t jur||_$|t jur||_%|t jur/t&|t'r,|( }||_)t*j+|||||||	||
d	}t,||||ddS )a  Return a copy of this deployment with updated options.

        Only those options passed in will be updated, all others will remain
        unchanged from the existing deployment.

        Refer to the `@serve.deployment` decorator docs for available arguments.
        rE   Nz2`max_ongoing_requests` must be non-null, got None.auto1c                 S   s&   g | ]\}}|d vr|t jur|qS )>   r   r4   r>   )r   VALUE).0optionvaluer   r   r    
<listcomp>  s    
z&Deployment.options.<locals>.<listcomp>zQManually setting num_replicas is not allowed when autoscaling_config is provided.r   z)num_replicas is expected to larger than 0zDeprecationWarning: `version` in `Deployment.options()` has been deprecated. Explicitly specifying version will raise an error in the future!)rI   rK   rH   rY   rZ   r[   !placement_group_fallback_strategyr\   T)r)   r4   )-r   rf   rF   r   r   AUTO_NUM_REPLICAS_USEDrecordlocalsitemsr   r2   user_configured_option_namesupdaterL   rM   r@   rA   rB   rC   rc   r3   r=   r0   r1   rI   rK   rH   rY   rZ   r[   rk   r\   r]   rb   r^   r_   r`   ra   r-   r   dictrP   r   creater   )r   r>   r&   r)   r@   rG   rH   rY   rZ   r[   r\   rA   rB   rC   r]   r^   r_   r`   ra   rP   rb   rS   rT   r4   rc   rp   new_deployment_configrk   new_replica_configr   r   r    rU      s   
(















zDeployment.optionsc                 C   sP   t | j|jk| j|jk| j|jk| jj|jjk| jj|jjk| jj|jjkgS r   )allr0   r1   r2   r3   rI   rK   rH   )r   otherr   r   r    __eq__  s   


zDeployment.__eq__c                 C   s   d| j  dS )NzDeployment(name=)r:   r;   r   r   r    __str__  s   zDeployment.__str__c                 C   s   t | S r   )r.   r;   r   r   r    __repr__  s   zDeployment.__repr__)NF)/r"   r#   r$   r%   r.   r
   r   r   r!   r,   propertyr&   r)   r	   r   r>   intr@   r   rA   rB   rC   rG   r   rH   r   rI   rK   rN   rP   rQ   rR   r   rX   r   rf   r   r   floatr   r   r   boolrU   rx   rz   r{   r   r   r   r    r   @   s   



	


 
 Ir   dr*   c                 C   s   | j durt| j }nd}i d| jd| jjrdn| jd| jd| jd| j	d| jjd| jj
d	| jjd
| jjd| jjd|d| jjd| jjd| jjd| jjd| jj}t| D ]}|dkrt|| jjvrt||= qetdi |S )zConverts a live deployment object to a corresponding structured schema.

    Args:
        d: Deployment object to convert
    Nr&   r@   rB   rC   rA   r]   r^   r_   r`   ra   rH   rZ   rY   r\   rP   rb   r   )rH   r   	parse_objr&   r2   r]   r@   rB   rC   rA   r^   r_   r`   ra   r3   rZ   rY   r\   rP   rb   listkeysrp   r   )r   ray_actor_options_schemadeployment_optionsrh   r   r   r    deployment_to_schema  sZ   
	
r   sc                 C   s   | j tju r	d}n| j jdd}| jtju rd}n| j}| jtju r%d}n| j}| jtju r1d}n| j}tj| j	| j
| j| j| j| j| j| j| j| j| jd}|  |_tjddi ||||d}t| j||ddS )	aM  Creates a deployment with parameters specified in schema.

    The returned deployment CANNOT be deployed immediately. It's func_or_class
    value is an empty string (""), which is not a valid import path. The
    func_or_class value must be overwritten with a valid function or class
    before the deployment can be deployed.
    NT)exclude_unset)r@   rA   rB   rC   r]   r^   r_   r`   ra   rP   rb    r   )r=   rI   rK   rH   rY   rZ   r\   )r&   r'   r(   r4   )rH   r   rf   rr   rY   rZ   r\   r
   from_defaultr@   rA   rB   rC   r]   r^   r_   r`   ra   rP   rb   !_get_user_configured_option_namesrp   r   rs   r   r&   )r   rH   rY   rZ   r\   r'   r(   r   r   r    schema_to_deployment  sT   	
r   )&loggingr7   copyr   typingr   r   r   r   r   r   r	   ray.serve._private.configr
   r   r   r   ray.serve._private.constantsr   ray.serve._private.usager   ray.serve._private.utilsr   r   ray.serve.configr   ray.serve.schemar   r   r   ray.util.annotationsr   	getLoggerrL   r   r   r   r   r   r   r   r    <module>   s(    $
)  m3