o
    $i9                     @   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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dVdededefddZdd ZdWd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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dd'd/ee!e"dfZ%d0ed1e"defd2d3Z&ed4d$dd'ed5ej'd'ed!d6d'ee$ee"dfd7d dd'ee$e"dfd8d eee#e(fd9d d$d'ee$e"dfd:d d;Z)ee#e!e"dfeee"dfdd'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'eee"dfdA
Z*i e)e*e%Z+eeef e,dB< i e%e)Z-eeef e,dC< i e%e*Z.eeef e,dD< dEe#e+/  dFZ0dGeeef fdHdIZ1dGeeef dJe$fdKdLZ2dGeeef dMefdNdOZ3dGeeef dMefdPdQZ4dReeef dSeeef deeef fdTdUZ5dS )XzKManage, parse and validate options for Ray tasks, actors and actor methods.    N)	dataclass)AnyCallableDictOptionalTupleUnion)ray_constants)validate_fallback_strategy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   Y/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/_common/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 )a  This 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.

    Returns:
        An Option object.
    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><      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-   D   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_quantityK   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-   j   s    z"_resource_option.<locals>.<lambda>r/   )r   r4   r0   r   )r%   r   r   r,   r   _resource_optionf   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_resourceso   s   
rH   label_selectorc                 C      t | S rB   )r   r*   r   r   r   r-          r-   fallback_strategyc                 C   rJ   rB   )r
   r*   r   r   r   r-      rK   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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^   rO   num_returnsrP   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enable_tensor_transportlifetimerp   rq   rr   rs   	namespaceget_if_existsallow_out_of_order_execution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.rQ   rR   rV   )rR   NNzrPlacement groups should be specified via the scheduling_strategy option. The placement_group option is deprecated.)getr   )r}   rQ   rV   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 )NrQ   rS   rT   rR   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   rQ   rS   rT   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   r{   r   rh   r   r   r}   r   kvr   r   r   validate_task_options?  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()'.rx   r%   z8The actor name must be specified to use `get_if_exists`.rP   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   r|   r   rh   r   r~   r   r   r   r   r   r   r   r   r   validate_actor_optionsS  s.   
r   original_optionsnew_optionsc                 C   s   i | |S )zUpdate original options with new options and return.
    The returned updated options contain shallow copy of original options.
    r   )r   r   r   r   r   update_optionsy  s   r   )TNrB   )6__doc__r   dataclassesr   typingr   r   r   r   r   r   r5   ray._privater	   ray._private.label_utilsr
   r   ray._private.utilsr   ray.util.placement_groupr   ray.util.scheduling_strategiesr   r   r   r   r$   rg   r1   rA   rC   r4   rH   dictr   rh   r0   _common_optionsr\   DEFAULT_TASK_MAX_RETRIESri   _task_only_options_actor_only_optionsrz   r#   r{   r|   keysremote_args_error_stringr   r   r   r   r   r   r   r   r   <module>   s     "		

#


	
8







#&


