
    &`iJ              
       r   d dl Z 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
 d dlZd dlmZmZ d dlmZ d dlmZ erd dlmZ  ej        e          Z ed          d	e
e	d
         ef         fd            Z ed          d	e
e	d
         ef         de
eeef         eeegef         f         fd            ZdS )    N)TYPE_CHECKINGAnyCallableDictOptionalTypeUnion)PlacementGroupFactoryresource_dict_to_pg_factory)_ParameterRegistry)	PublicAPI	Trainablebeta)	stability	trainabler   c                    	
 ddl m} t                     r$t          j                   r0t           |          s t          dt                      d          t                      
t          j
        j        j                            
j                   t                      d|                                D ]\  }}
                    |z   |           t%           dd          }t'          |                                          	t          j                   r G 	
fdd	           }nB	
 fd
}|}t+           d          r j        |_        t+           d          r j        |_        ||_        |S )a]  Wrapper for trainables to pass arbitrary large data objects.

    This wrapper function will store all passed parameters in the Ray
    object store and retrieve them when calling the function. It can thus
    be used to pass arbitrary data, even datasets, to Tune trainables.

    This can also be used as an alternative to ``functools.partial`` to pass
    default arguments to trainables.

    When used with the function API, the trainable function is called with
    the passed parameters as keyword arguments. When used with the class API,
    the ``Trainable.setup()`` method is called with the respective kwargs.

    If the data already exists in the object store (are instances of
    ObjectRef), using ``tune.with_parameters()`` is not necessary. You can
    instead pass the object refs to the training function via the ``config``
    or use Python partials.

    Args:
        trainable: Trainable to wrap.
        **kwargs: parameters to store in object store.

    Function API example:

    .. code-block:: python

        from ray import tune

        def train_fn(config, data=None):
            for sample in data:
                loss = update_model(sample)
                tune.report(dict(loss=loss))

        data = HugeDataset(download=True)

        tuner = Tuner(
            tune.with_parameters(train_fn, data=data),
            # ...
        )
        tuner.fit()

    Class API example:

    .. code-block:: python

        from ray import tune

        class MyTrainable(tune.Trainable):
            def setup(self, config, data=None):
                self.data = data
                self.iter = iter(self.data)
                self.next_sample = next(self.iter)

            def step(self):
                loss = update_model(self.next_sample)
                try:
                    self.next_sample = next(self.iter)
                except StopIteration:
                    return {"loss": loss, done: True}
                return {"loss": loss}

        data = HugeDataset(download=True)

        tuner = Tuner(
            tune.with_parameters(MyTrainable, data=data),
            # ...
        )
    r   r   zw`tune.with_parameters() only works with function trainables or classes that inherit from `tune.Trainable()`. Got type: .___name__tune_with_parametersc                   (     e Zd Z fdZ xZS )with_parameters.<locals>._Innerc                     i }D ]}                     |z             ||<    t          |           j        |fi | d S N)getsupersetup)	selfconfigsetup_kwargsk_Inner	__class__keysparameter_registryprefixs	       k/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/trainable/util.pyr   z%with_parameters.<locals>._Inner.setupt   sa    ! I IA&8&<&<VaZ&H&HLOO)fd##)&AALAAAAA    )r   
__module____qualname__r   __classcell__)r$   r#   r%   r&   r'   s   @r(   r#   r   s   sZ        B B B B B B B B B B B B Br)   r#   c                 Z    i }D ]}                     |z             ||<    | fi |S r   )r   )r    	fn_kwargsr"   r%   r&   r'   r   s      r(   innerzwith_parameters.<locals>.inner~   sN    I B B155fqjAA	!9V11y111r)   
__mixins__
_resources)ray.tune.trainabler   callableinspectisclass
issubclass
ValueErrortyper   ray_privateworker_post_init_hooksappendflushstritemsputgetattrsetr%   hasattrr0   r1   r   )r   kwargsr   r"   vtrainable_nametrainable_with_paramsr/   r#   r%   r&   r'   s   `       @@@@r(   with_parametersrI      s   L -,,,,,I 
	""
+5i+K+K
 "I" " "
 
 	
 ,--L(//0B0HIII I!!!F . .1vz1----Y
4JKKNv{{}}Dy!! D	B 	B 	B 	B 	B 	B 	B 	B 	B 	BY 	B 	B 	B !'	2 	2 	2 	2 	2 	2 	2 	2 !&9l++ 	D/8/C!, 9l++ 	D/8/C!,%3"  r)   	resourcesc                     ddl m} t                     r$t          j                   r0t           |          s t          dt                      d          t          |t                    r|nVt          |t                    rt          |          n1t          |          r|nt          dt          |                     t          j                   sSt           t          j                  r fd}|_        |S 	  _        n@# t          $ r}t!          d          |d}~ww xY w G fd	d
           } j        |_        |  S )a  Wrapper for trainables to specify resource requests.

    This wrapper allows specification of resource requirements for a specific
    trainable. It will override potential existing resource requests (use
    with caution!).

    The main use case is to request resources for function trainables when used
    with the Tuner() API.

    Class trainables should usually just implement the ``default_resource_request()``
    method.

    Args:
        trainable: Trainable to wrap.
        resources: Resource dict, placement group factory, or callable that takes
            in a config dict and returns a placement group factory.

    Example:

    .. code-block:: python

        from ray import tune
        from ray.tune.tuner import Tuner

        def train_fn(config):
            return len(ray.get_gpu_ids())  # Returns 2

        tuner = Tuner(
            tune.with_resources(train_fn, resources={"gpu": 2}),
            # ...
        )
        results = tuner.fit()

    r   r   zv`tune.with_resources() only works with function trainables or classes that inherit from `tune.Trainable()`. Got type: r   z.Invalid resource type for `with_resources()`: c                      |           S r    )r    r   s    r(   
_trainablez"with_resources.<locals>._trainable   s     y(((r)   zCould not use `tune.with_resources()` on the supplied trainable. Wrap your trainable in a regular function before passing it to Ray Tune.Nc                   P    e Zd Zedeeef         dee         f fd            Z	dS ))with_resources.<locals>.ResourceTrainabler    returnc                 f    t          t                    st                    r |          S S r   )
isinstancer
   r3   )clsr    pgfs     r(   default_resource_requestzBwith_resources.<locals>.ResourceTrainable.default_resource_request   s8     "#'<== '(3-- '3v;;&
r)   N)
r   r*   r+   classmethodr   r?   r   r   r
   rV   )rU   s   r(   ResourceTrainablerP      s[        !#s(^/0     [  r)   rX   )r2   r   r3   r4   r5   r6   r7   r8   rS   r
   dictr   types
MethodTyper1   AttributeErrorRuntimeErrorr   )r   rJ   r   rN   erX   rU   s   `     @r(   with_resourcesr_      s   V -,,,,,I 
	""
+5i+K+K
 "I" " "
 
 	
 )233 	
	It	$	$ 
))44	)		 
NT)__NN
 
 	
 ?9%% &i!122 	) ) ) ) ) %(J!	#&I   	 	 	  			 	 	 	 	 	 		 	 	 	 &/%7"%	s   D 
D.D))D.)r4   loggingrZ   typingr   r   r   r   r   r   r	   r9   #ray.tune.execution.placement_groupsr
   r   ray.tune.registryr   ray.util.annotationsr   r2   r   	getLoggerr   loggerrI   r?   floatrY   r_   rM   r)   r(   <module>rh      s      L L L L L L L L L L L L L L L L L L 



        1 0 0 0 0 0 * * * * * * -,,,,,,		8	$	$ Vz!uT+%6%@A z! z! z! z!z V`T+&01`S%Z$../	1` ` ` ` ` `r)   