o
    bi;                     @   s  U d 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
 ddl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 eG d
d dZdUdededefddZdd ZdVdedefddZdeeeef  dee fddZi dee e!dfdd deee!dfdeddeee!dfdedd ed d!ed!d"d#ee!deefd$d%d&ee"d'd%d(eee!dfdee e!dfd)d d*ee e!dfd+ee!deeeefd,ee e!dfd-eedd%d.ee e!dfZ#d/ed0e!defd1d2Z$ed3d"dd%ed4ej%d%edd5d%ee"ee!dfd6d dd%ee"e!dfd7d eee&e'fd8d d"d%ee"e!dfd9d d:Z(ee&e e!dfeee!dfd;d ed<d"ed=dd%ed>dd%ed?d'd%eee!dfeed"d%d@Z)i e(e)e#Z*eeef e+dA< i e#e(Z,eeef e+dB< i e#e)Z-eeef e+dC< dDe&e*.  dEZ/dFeeef fdGdHZ0dFeeef dIe"fdJdKZ1dFeeef dLefdMdNZ2dFeeef dLefdOdPZ3dQeeef dReeef deeef fdSdTZ4dS )WzKManage, parse and validate options for Ray tasks, actors and actor methods.    N)	dataclass)AnyCallableDictOptionalTupleUnion)ray_constantsvalidate_label_selector)get_ray_doc_version)PlacementGroup)NodeAffinitySchedulingStrategyNodeLabelSchedulingStrategy PlacementGroupSchedulingStrategyc                   @   sf   e Zd ZU dZeeeee f  ed< dZ	ee
egee f  ed< dZeed< dedefddZdS )	OptionNtype_constraintvalue_constraintdefault_valuekeywordvaluec                 C   s^   | j durt|| j std| d| j  dt| | jdur+| |}|r-t|dS dS )zValidate the option.NzThe type of keyword 'z
' must be z, but received type )r   
isinstance	TypeErrortyper   
ValueError)selfr   r   possible_error_message r   Q/home/ubuntu/.local/lib/python3.10/site-packages/ray/_private/ray_option_utils.pyvalidate   s   


zOption.validate)__name__
__module____qualname__r   r   r   r   r   __annotations__r   r   r   strr   r   r   r   r   r   r      s
   
 r   Tnameinfiniter   c                    s@   |rt ttdf fdd|dS t ttdf fdd|dS )zThis is used for positive and discrete options.

    Args:
        name: The name of the option keyword.
        infinite: If True, user could use -1 to represent infinity.
        default_value: The default value for this option.
    Nc                        | d u s| dkr
d S d  dS )NThe keyword 'zO' only accepts None, 0, -1 or a positive integer, where -1 represents infinity.r   xr%   r   r   <lambda>8      z"_counting_option.<locals>.<lambda>r   c                    r'   )Nr   r)   z-' only accepts None, 0 or a positive integer.r   r*   r,   r   r   r-   @   r.   )r   intr   )r%   r&   r   r   r,   r   _counting_option-   s   



r1   c                 C   s   |dk r
d|  dS t |tr#|dkr#t|tjj dkr#d|  dS | dkr)dn| }|tjj v rCtjj	|
|\}}|sC|S d S )	Nr   zThe quantity of resource z cannot be negativeg        z5The precision of the fractional quantity of resource z cannot go beyond 0.0001num_gpusGPU)r   floatr0   ray_rayletRESOURCE_UNIT_SCALING_privateaccelerators"get_all_accelerator_resource_names$get_accelerator_manager_for_resource"validate_resource_request_quantity)r%   quantityresource_namevaliderror_messager   r   r   _validate_resource_quantityG   s,   
rA   c                    s    t tttdf fdd|dS )z*This is used for resource related options.Nc                    s   | d u rd S t  | S N)rA   r*   r,   r   r   r-   f   s    z"_resource_option.<locals>.<lambda>r/   )r   r4   r0   r   )r%   r   r   r,   r   _resource_optionb   s
   
rC   	resourcesreturnc                 C   sN   | d u rd S d| v sd| v r	 dS |   D ]\}}t||}|r$|  S qd S )NCPUr3   z[Use the 'num_cpus' and 'num_gpus' keyword instead of 'CPU' and 'GPU' in 'resources' keyword)itemsrA   )rD   r%   r=   r   r   r   r   _validate_resourcesk   s   
rH   label_selectorc                 C      t | S rB   r
   r*   r   r   r   r-   ~       r-   accelerator_typememorynum_cpusr2   object_store_memoryFplacement_groupdefaultr/   placement_group_bundle_indexr(   #placement_group_capture_child_tasksc                 C   rJ   rB   )rH   r*   r   r   r   r-      rK   runtime_envscheduling_strategy	_metadataenable_task_events_labelsobjcls_c                 C   s"   zt | |W S  ty   Y dS w )NF)
issubclassr   )rY   rZ   r   r   r   issubclass_safe   s
   r\   	max_callsmax_retries   c                 C   s(   | d u s| dks| dks| dkrd S dS )Ndynamic	streamingr   ag  Default None. When None is passed, The default value is 1 for a task and actor task, and 'streaming' for generator tasks and generator actor tasks. The keyword 'num_returns' only accepts None, a non-negative integer, 'streaming' (for generators), or 'dynamic'. 'dynamic' flag will be deprecated in the future, and it is recommended to use 'streaming' instead.r   r*   r   r   r   r-      s    c                 C   s   | d u rd S dS )Nz:Setting 'object_store_memory' is not implemented for tasksr   r*   r   r   r   r-         c                 C   s2   t | tst | ttfrtdd | D rd S dS )Nc                 s   s    | ]}t |tV  qd S rB   )r\   	Exception).0x_r   r   r   	<genexpr>   s    z<lambda>.<locals>.<genexpr>zAretry_exceptions must be either a boolean or a list of exceptions)r   boollisttupleallr*   r   r   r   r-      s   c                 C   s   | dkrd S dS )Nr   z_generator_backpressure_num_objects=0 is not allowed. Use a value > 0. If the value is equal to 1, the behavior is identical to Python generator (generator 1 object whenever `next` is called). Use -1 to disable this feature. r   r*   r   r   r   r-      s   )r]   r^   rN   num_returnsrO   retry_exceptions#_generator_backpressure_num_objectsc                 C   s   | dv rd S dS )N)Ndetachednon_detachedzOactor `lifetime` argument must be one of 'detached', 'non_detached' and 'None'.r   r*   r   r   r   r-      rb   max_concurrencymax_restartsmax_task_retriesmax_pending_calls)concurrency_groupslifetimerp   rq   rr   rs   	namespaceget_if_existsvalid_optionstask_optionsactor_optionszThe @ray.remote decorator must be applied either with no arguments and no parentheses, for example '@ray.remote', or it must be applied using some of the arguments in the list zL, for example '@ray.remote(num_returns=2, resources={"CustomResource": 1})'.optionsc                 C   s6   |  dd}|  d}|dvr|durtddS dS )z2Check if deprecated placement group option exists.rP   rQ   rU   )rQ   NNzrPlacement groups should be specified via the scheduling_strategy option. The placement_group option is deprecated.)getr   )r{   rP   rU   r   r   r    _check_deprecate_placement_group  s   
r}   caller_stacklevelc                 C   s   | d }| d }| d }|dkrt jdt  dt|d d |d	kr2t jd
t  dt|d d |rEt jdt  dt|d d d S d S )NrP   rR   rS   rQ   zplacement_group parameter is deprecated. Use scheduling_strategy=PlacementGroupSchedulingStrategy(...) instead, see the usage at https://docs.ray.io/en/z&/ray-core/package-ref.html#ray-remote.r_   
stacklevelr(   zplacement_group_bundle_index parameter is deprecated. Use scheduling_strategy=PlacementGroupSchedulingStrategy(...) instead, see the usage at https://docs.ray.io/en/zplacement_group_capture_child_tasks parameter is deprecated. Use scheduling_strategy=PlacementGroupSchedulingStrategy(...) instead, see the usage at https://docs.ray.io/en/)warningswarnr   DeprecationWarning)r{   r~   rP   rR   rS   r   r   r   )_warn_if_using_deprecated_placement_group  s8   
r   
in_optionsc                 C   sd   |   D ]\}}|tvrtd| dtt dt| || q|r,d| v r,tdt|  dS )zOptions check for Ray tasks.

    Args:
        options: Options for Ray tasks.
        in_options: If True, we are checking the options under the context of
            ".options()".
    Invalid option keyword z& for remote functions. Valid ones are .r]   z5Setting 'max_calls' is not supported in '.options()'.N)rG   ry   r   rh   r   r}   r{   r   kvr   r   r   validate_task_options7  s   r   c                 C   s   |   D ]\}}|tvrtd| dtt dt| || q|r,d| v r,td| dr:| ds:tdd	| v rKtjd
t  dt	dd t
|  dS )zOptions check for Ray actors.

    Args:
        options: Options for Ray actors.
        in_options: If True, we are checking the options under the context of
            ".options()".
    r   z for actors. Valid ones are r   rt   z>Setting 'concurrency_groups' is not supported in '.options()'.rw   r%   z8The actor name must be specified to use `get_if_exists`.rO   zSetting 'object_store_memory' for actors is deprecated since it doesn't actually reserve the required object store memory. Use object spilling that's enabled by default (https://docs.ray.io/en/zb/ray-core/objects/object-spilling.html) instead to bypass the object store memory size limitation.r_   r   N)rG   rz   r   rh   r   r|   r   r   r   r   r}   r   r   r   r   validate_actor_optionsK  s.   
r   original_optionsnew_optionsc                 C   sn   i | |}|  ddur5| ddur5| d  }|d  D ]\}}i | |i |||< q ||d< |S )zUpdate original options with new options and return.
    The returned updated options contain shallow copy of original options.
    rV   N)r|   copyrG   )r   r   updated_optionsmetadatarv   configr   r   r   update_optionsq  s   r   )TNrB   )5__doc__r   dataclassesr   typingr   r   r   r   r   r   r5   ray._privater	   ray._private.label_utilsr   ray._private.utilsr   ray.util.placement_groupr   ray.util.scheduling_strategiesr   r   r   r   r$   rg   r1   rA   rC   r4   rH   dictr   r0   _common_optionsr\   DEFAULT_TASK_MAX_RETRIESrh   ri   _task_only_options_actor_only_optionsrx   r#   ry   rz   keysremote_args_error_stringr}   r   r   r   r   r   r   r   r   <module>   s     "	
!


	
8







#&


