
    &`i              
          d dl Z d dlZd dlZd dlZd dlZd dlZ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Zd dlmZ d dlZd dlmZ d dl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&m'Z' d d
l(m)Z) d dl*m+Z, d dl-m+Z. d dl/m0Z0 d dl1m2Z2 d dl3m4Z4  ej5        e6          Z7 ej8        d          Z9 ej8        d          Z: e4d           G d de j;                              Z<dej=        j>        de?de@fdZAdej=        jB        de@fdZCe#	 d&dee?e,e.f         ded         dee?ef         fd            ZDe#dee?e,e.f         de?de?fd             ZEe#d!ee?e,e.f         de?de?fd"            ZFe4d'd$e@fd%            ZGdS )(    N)MappingProxyType)Any
CollectionDictListOptionalTupleUnion)version)COMPONENT_LEARNERCOMPONENT_LEARNER_GROUPCOMPONENT_RL_MODULE)
force_list)FaultTolerantActorManager)OldAPIStack5OverrideToImplementCustomLogic_CallToSuperRecommended)NOT_SERIALIZABLEserialize_type)	StateDict)
Checkpoint)sync_dir_between_nodes)log_once)	PublicAPIz1.1z2.1alpha)	stabilityc                   ~   e Zd ZdZdZdZdZ	 ddddddeee	e
j        f                  dee         d	ed
         dede	f
dZddddee	e
j        f         dee	         d	ed
         ddfdZe	 ddee	e
j        f         d	ed
         dd fd            Zej        	 ddddeee	ee	         f                  deee	ee	         f                  defd            Zej        deddfd            Zej        deeee	ef         f         fd            Zedefd            Zdeee	d f                  fdZdefdZd Z 	 ddZ!dS )CheckpointableaM  Abstract base class for a component of RLlib that can be checkpointed to disk.

    Subclasses must implement the following APIs:
    - save_to_path()
    - restore_from_path()
    - from_checkpoint()
    - get_state()
    - set_state()
    - get_ctor_args_and_kwargs()
    - get_metadata()
    - get_checkpointable_components()
    statezclass_and_ctor_args.pklzmetadata.jsonNF)r   
filesystemuse_msgpackpathr   pyarrow.fs.FileSystemr    returnc          	      	   |Qddl }t          j        t          j                              }t          |                                          }|p||z  }t          |t
                    r|n|                                }|r)|s't          j
        j                            |          \  }}|                    |d           t          j        |          }|                                 }d|vrt          t                    |d<   |                    || j        z                                            5 }	|	                    t'          j        |                              d                     ddd           n# 1 swxY w Y   |                    || j        z                                            5 }	t/          j        t3          |           |                                 d|	           ddd           n# 1 swxY w Y   |du}
|p1|                     d |                                 D             	          }|                                 D ]\  }}|
r||vr||z  }t          |t:                    r~|                                d         }dd
}t?          tA          |!                    ||g                              }|j"        s|#                                |#                                } |            }d}|
r'tI          j%        |&                    |                    }||k    r|!                    |||fd|g           ||fd}t?          tA          |!                    ||g                              }|j"        s|#                                |#                                }tO          |||t          |                     |fd}|!                    ||g           |
r|&                    |          }n|                     |          |         }|(                    ||||           || j)        |rdndz   z  }|                    |                                          5 }	|r'tU          d          }|                    ||	           nt/          j        ||	           ddd           n# 1 swxY w Y   t          |          S )a  Saves the state of the implementing class (or `state`) to `path`.

        The state of the implementing class is always saved in the following format:

        .. testcode::
            :skipif: True

            path/
                [component1]/
                    [component1 subcomponentA]/
                        ...
                    [component1 subcomponentB]/
                        ...
                [component2]/
                        ...
                [cls.METADATA_FILE_NAME] (json)
                [cls.STATE_FILE_NAME] (pkl|msgpack)

        The main logic is to loop through all subcomponents of this Checkpointable
        and call their respective `save_to_path` methods. Then save the remaining
        (non subcomponent) state to this Checkpointable's STATE_FILE_NAME.
        In the exception that a component is a FaultTolerantActorManager instance,
        instead of calling `save_to_path` directly on that manager, the first healthy
        actor is interpreted as the component and its `save_to_path` method is called.
        Even if that actor is located on another node, the created file is automatically
        synced to the local node.

        Args:
            path: The path to the directory to save the state of the implementing class
                to. If `path` doesn't exist or is None, then a new directory will be
                created (and returned).
            state: An optional state dict to be used instead of getting a new state of
                the implementing class through `self.get_state()`.
            filesystem: PyArrow FileSystem to use to access data at the `path`.
                If not specified, this is inferred from the URI scheme of `path`.
            use_msgpack: Whether the state file should be written using msgpack and
                msgpack_numpy (file extension is `.msgpack`), rather than pickle (file
                extension is `.pkl`).

        Returns:
            The path (str) where the state has been saved.
        Nr   T	recursivecheckpoint_versionzutf-8)classctor_args_and_kwargsc                     g | ]
}|d          S r    .0cs     o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/rllib/utils/checkpoints.py
<listcomp>z/Checkpointable.save_to_path.<locals>.<listcomp>   s    OOOQAaDOOO    not_componentsc                 <    dd l }|j                                        S Nr   )rayutilget_node_ip_address)_r7   s     r0   _get_ipz,Checkpointable.save_to_path.<locals>._get_ip   s    JJJ877999r2   remote_actor_idsc                     |                      ||t          j        |          n|                                 |          S )Nr   r    )save_to_pathr7   get	get_state)w_path_state_use_msgpacks       r0   <lambda>z-Checkpointable.save_to_path.<locals>.<lambda>   sE    NN % (.'9 %(GFOOO)*,8 +   r2   c                     dd l } |j                    }|                     ||t          j        |          n|                                 |           |S )Nr   r?   )tempfilemkdtempr@   r7   rA   rB   )rC   rE   rF   rI   tmpdirs        r0   _savez*Checkpointable.save_to_path.<locals>._save  sa    ' "2!1!3!3"393E1;;==(4 '     &r2   c                 8    dd l }|                    |           d S r6   )shutilrmtree)r:   _dirrN   s      r0   _rmdirz+Checkpointable.save_to_path.<locals>._rmdir"  s#    %d+++++r2   )
components)r   r   r    .msgpack.pklerrorN)+uuidpathlibPathrI   
gettempdirstruuid4
isinstanceas_posixpyarrowfs
FileSystemfrom_uri
create_dirget_metadata)CHECKPOINT_VERSION_LEARNER_AND_ENV_RUNNERopen_output_streamMETADATA_FILE_NAMEwritejsondumpsencodeCLASS_AND_CTOR_ARGS_FILE_NAMEpickledumptypeget_ctor_args_and_kwargsrB   get_checkpointable_componentsr   healthy_actor_idsnextiterforeach_actorokrA   r7   putpopr   r@   STATE_FILE_NAMEtry_import_msgpack)selfr!   r   r   r    rX   tmp_dirrandom_dir_namemetadataf_state_provided	comp_namecomp	comp_pathactor_to_user;   _resultworker_ip_addrself_ip_addrcomp_state_refrL   worker_temp_dirrQ   
comp_statefilenamemsgpacks                             r0   r@   zCheckpointable.save_to_path^   sA   h <KKK l8#6#8#899G!$**,,//O 47_4D "$,,Att$--//  	D
 	D  'z4==dCCJ 	dd333 |D!! $$&&x//-09. .H)* **D++5577
 
 	:GGDJx((//88999	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: **D66@@BB
 
 		K!$ZZ,0,I,I,K,K    		 		 		 		 		 		 		 		 		 		 		 		 		 		 		  t+  
OO$*L*L*N*NOOO ( 
 

  $AACC i	 i	OIt  9E#9#9y(I
 $ 9:: _#5577:: : : :
 **#.:^ +     z (!++--'!(&wyy "&" C%(WUYYy-A-A%B%BN!\11&&(1.Wb 
 
 
 +7 '      )7[ & & & & #T///WWXX G #: ,%kkmm+&-kkmmO +&'$I	   (7 , , , ,
 &&v&OOOO # Q!&9!5!5JJ!%9!E!Ei!PJ !!)$ +	 "      +$IJJ6J
 **8+<+<+>+>?? 	&1 &,4888UA&&&&E1%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 4yys6   3;E::E>E>48G88G<?G<<?SSS)	componentr   r   c                h   t          |t                    r|n|                                }|r)|s't          j        j                            |          \  }}t          j        |          }t          ||                                          st          d| d          d |                                 D             } | j        ||fd|i| |^|| j        z  }|                    d                                          rzt!          d          }|                    |                    d                                                    5 }|                    |d	
          }	ddd           n# 1 swxY w Y   nf|                    |                    d                                                    5 }t'          j        |          }	ddd           n# 1 swxY w Y   |                     |	           d |                                 D             }
|
|z
  }|r | j        ||fd|i| dS dS dS )a.  Restores the state of the implementing class from the given path.

        If the `component` arg is provided, `path` refers to a checkpoint of a
        subcomponent of `self`, thus allowing the user to load only the subcomponent's
        state into `self` without affecting any of the other state information (for
        example, loading only the NN state into a Checkpointable, which contains such
        an NN, but also has other state information that should NOT be changed by
        calling this method).

        The given `path` should have the following structure and contain the following
        files:

        .. testcode::
            :skipif: True

            path/
                [component1]/
                    [component1 subcomponentA]/
                        ...
                    [component1 subcomponentB]/
                        ...
                [component2]/
                        ...
                [cls.METADATA_FILE_NAME] (json)
                [cls.STATE_FILE_NAME] (pkl|msgpack)

        Note that the self.METADATA_FILE_NAME file is not required to restore the state.

        Args:
            path: The path to load the implementing class' state from or to load the
                state of only one subcomponent's state of the implementing class (if
                `component` is provided).
            component: If provided, `path` is interpreted as the checkpoint path of only
                the subcomponent and thus, only that subcomponent's state is
                restored/loaded. All other state of `self` remains unchanged in this
                case.
            filesystem: PyArrow FileSystem to use to access data at the `path`. If not
                specified, this is inferred from the URI scheme of `path`.
            **kwargs: Forward compatibility kwargs.
        z`path` (z) not found!c                     h | ]
}|d          S r+   r,   r-   s     r0   	<setcomp>z3Checkpointable.restore_from_path.<locals>.<setcomp>  s    NNNA1Q4NNNr2   r   NrS   TrU   F)strict_map_keyrT   c                     h | ]
}|d          S r+   r,   r-   s     r0   r   z3Checkpointable.restore_from_path.<locals>.<setcomp>  s    QQQqadQQQr2   only_comp_names)r^   r\   r_   r`   ra   rb   rc   rY   rZ   _exists_at_fs_pathFileNotFoundErrorrr   $_restore_all_subcomponents_from_pathrz   with_suffixis_filer{   open_input_streamloadrn   	set_state)r|   r!   r   r   kwargsorig_comp_namesr   r   r   r   new_comp_namesdiff_comp_namess               r0   restore_from_pathz Checkpointable.restore_from_pathF  s   ` "$,,Att$--// 	D
 	D  'z4==dCCJ |D!!!*dmmoo>> 	C#$At$A$A$ABBB ON)K)K)M)MNNN11*	
 	
(1	
5;	
 	
 	

 d22H##J//7799 
+,488811((44==??  B#LL5LAAEB B B B B B B B B B B B B B B
  11((0099;;  +"KNNE+ + + + + + + + + + + + + + + NN5!!!QQD,N,N,P,PQQQN,>O 99* 6EIO    % " s$   E11E58E57GGGc           	         t          |t                    r|n|                                }|r)|s't          j        j                            |          \  }}t          j        |          }	 |	                    || j
        z                                            5 }t          j        |          }ddd           n# 1 swxY w Y   |d         }t          |d         d                   }|d         d         }t          t          j        |          j                                                  D ]Y\  }	\  }
}|
|v rM|                    |
          }|j        t          j        j        k    rt/          |          |	k    r|||	<   T|||
<   Zn# t0          $ r	 | }g }|}Y nw xY wt3          ||           st5          d| d| d|  d	          t3          |t6                    st5          d| d| d
           ||i |} |j        |fd|i| |S )a  Creates a new Checkpointable instance from the given location and returns it.

        Args:
            path: The checkpoint path to load (a) the information on how to construct
                a new instance of the implementing class and (b) the state to restore
                the created instance to.
            filesystem: PyArrow FileSystem to use to access data at the `path`. If not
                specified, this is inferred from the URI scheme of `path`.
            kwargs: Forward compatibility kwargs. Note that these kwargs are sent to
                each subcomponent's `from_checkpoint()` call.

        Returns:
             A new instance of the implementing class, already set to the state stored
             under `path`.
        Nr(   r)   r      zThe class (z) stored in checkpoint (z+) does not seem to be a subclass of `cls` (z)!zA) does not seem to be an implementer of the `Checkpointable` API!r   )r^   r\   r_   r`   ra   rb   rc   rY   rZ   r   rm   rn   r   r   	enumerateinspect	signature
parametersitemsry   kind_ParameterKindPOSITIONAL_OR_KEYWORDlen	Exception
issubclass
ValueErrorr   r   )clsr!   r   r   r   	ctor_infoctor	ctor_argsctor_kwargsi
param_nameparamvalobjs                 r0   from_checkpointzCheckpointable.from_checkpoint  s   . "$,,Att$--//  	D
 	D  'z4==dCCJ |D!!	!--99CCEE  +"KNN	+ + + + + + + + + + + + + + + W%D"9-C#DQ#GHHI#$:;A>K +4!$''288::+ + 6 6&&J '' **Z00C
g&<&RRR	NNQ..'*	!25J/6  	! 	! 	!DI KKK	! $$$ 		0d 0 0D 0 0(+0 0 0   D.11 	>d > >D > > >   dI---dDDzDVDDD
s7   ,/F B<0F <C  F C CF FFr3   rR   r4   c                    dS )a  Returns the implementing class's current state as a dict.

        The returned dict must only contain msgpack-serializable data if you want to
        use the `AlgorithmConfig._msgpack_checkpoints` option. Consider returning your
        non msgpack-serializable data from the `Checkpointable.get_ctor_args_and_kwargs`
        method, instead.

        Args:
            components: An optional collection of string keys to be included in the
                returned state. This might be useful, if getting certain components
                of the state is expensive (e.g. reading/compiling the weights of a large
                NN) and at the same time, these components are not required by the
                caller.
            not_components: An optional list of string keys to be excluded in the
                returned state, even if the same string is part of `components`.
                This is useful to get the complete state of the class, except
                one or a few components.
            kwargs: Forward-compatibility kwargs.

        Returns:
            The current state of the implementing class (or only the `components`
            specified, w/o those in `not_components`).
        Nr,   )r|   rR   r4   r   s       r0   rB   zCheckpointable.get_state        r2   c                     dS )ab  Sets the implementing class' state to the given state dict.

        If component keys are missing in `state`, these components of the implementing
        class will not be updated/set.

        Args:
            state: The state dict to restore the state from. Maps component keys
                to the corresponding subcomponent's own state.
        Nr,   )r|   r   s     r0   r   zCheckpointable.set_state  r   r2   c                     dS )zReturns the args/kwargs used to create `self` from its constructor.

        Returns:
            A tuple of the args (as a tuple) and kwargs (as a Dict[str, Any]) used to
            construct `self` from its class constructor.
        Nr,   r|   s    r0   rq   z'Checkpointable.get_ctor_args_and_kwargs$  r   r2   c                 L    | j         | j        t          j        t          j        dS )a  Returns JSON writable metadata further describing the implementing class.

        Note that this metadata is NOT part of any state and is thus NOT needed to
        restore the state of a Checkpointable instance from a directory. Rather, the
        metadata will be written into `self.METADATA_FILE_NAME` when calling
        `self.save_to_path()` for the user's convenience.

        Returns:
            A JSON-encodable dict of metadata information.
        )class_and_ctor_args_file
state_fileray_version
ray_commit)rm   rz   r7   __version__
__commit__r   s    r0   re   zCheckpointable.get_metadata-  s)     )-(J.?.	
 
 	
r2   c                     g S )a  Returns the implementing class's own Checkpointable subcomponents.

        Returns:
            A list of 2-tuples (name, subcomponent) describing the implementing class'
            subcomponents, all of which have to be `Checkpointable` themselves and
            whose state is therefore written into subdirectories (rather than the main
            state file (self.STATE_FILE_NAME) when calling `self.save_to_path()`).
        r,   r   s    r0   rr   z,Checkpointable.get_checkpointable_components@  s	     	r2   c                     t          |          }t          |          }|t          fd|D                       s|v r||vrdS dS )ao  Returns True if a component should be checkpointed.

        Args:
            name: The checkpoint name.
            components: A list of components that should be checkpointed.
            non_components: A list of components that should not be checkpointed.

        Returns:
            True, if the component should be checkpointed and otherwise False.
        Nc              3   H   K   | ]}|                     d z             V  dS )/N)
startswith)r.   r/   names     r0   	<genexpr>z2Checkpointable._check_component.<locals>.<genexpr>Z  s3      ??1<<s
++??????r2   TF)r   any)r|   r   rR   r4   	comp_listnot_comp_lists    `    r0   _check_componentzCheckpointable._check_componentK  so     z**	">22????Y????? y  %])B)B4ur2   c                     |d S t          |          }g }|D ]G}|                    |dz             r-|                    |t          |          dz   d                     H|sd n|S )Nr   r   )r   r   appendr   )r|   r   rR   subcomponentsr   s        r0   _get_subcomponentsz!Checkpointable._get_subcomponents`  s}    4
++
 	< 	<Dtcz** <$$T#d))a-//%:;;;(;ttm;r2   c                 $   |                                  D ]\  }}|||vrd }|)||z  }	t          ||	                                          s8n<|}	|                    |dz             r|t	          |          dz   d          }n||k    rut          |t                    r_t          j        	                                }
|
                                }t          |          |	|
|fd}|                    ||            |j        |	f||d| d S )Nr   r   c                    dd l }dd l}|j                                        }||k    r | j        |fd|i| d S  |j                    5 }t          ||||            | j        |fd|i| d d d            d S # 1 swxY w Y   d S )Nr   r   )rI   r7   r8   r9   r   TemporaryDirectoryr   )	rC   _kwargsrD   _head_ip	_comp_argrI   r7   worker_node_iptemp_dirs	            r0   _restorezECheckpointable._restore_all_subcomponents_from_path.<locals>._restore  s    $OOOJJJ%(X%A%A%C%CN &11++ERRYR'RRRRR8X8:: h2 (%   0A/ ( 4=AH  	                 s   	#A99A= A=r<   )r   r   )rr   r   r_   r   r   r^   r   r7   r8   r9   rs   r   rv   r   )r|   r!   r   r   r   r   r   r   comp_argcomp_dirhead_node_ipall_healthy_actorsr   s                r0   r   z3Checkpointable._restore_all_subcomponents_from_pathl  s     $AACC A	 A	OIt*y/O/O H )+ **h6G6G6I6IJJ    ''	C88 (Y!);)=)=>HH)++
 $ 9:: %"x;;==%)%;%;%=%=" -V44")&   4 ""8>P"QQQQ
 '&)3x KQ   A	 A	r2   rW   )NN)"__name__
__module____qualname____doc__rz   rm   rh   r   r
   r\   rY   rZ   r   boolr@   r   classmethodr   abcabstractmethodr   rB   r   r	   r   r   rq   r   re   r   rr   r   r   r   r,   r2   r0   r   r   ?   s        $ O
 %>! ) 48f &*8<!f f fuS',./0f 	"	f
 45f f 
f f f fX $(8<X X XC%&X C=	X
 45X 
X X X Xt  9=U UC%&U 45U
 
U U U [Un 	 =A AE	  U3
3#789 !sJsO';!<=	 
   > 		y 	T 	 	 	 	 	%tCH~0E*F     ;
d 
 
 
 ;:
$	tE#?O:O4P/Q 	 	 	 	D    *
< 
< 
< AED D D D D Dr2   r   ra   r!   r#   c                 j    |                      |          }|j        t          j        j        j        k    S )z:Returns `True` if the path can be found in the filesystem.)get_file_inforp   r`   ra   FileTypeNotFound)ra   r!   valids      r0   r   r     s*    T""E:,555r2   	file_infoc                 @    | j         t          j        j        j        k    S )z5Returns `True`, if the file info is from a directory.)rp   r`   ra   r   	Directory)r   s    r0   _is_dirr     s    >WZ0:::r2   
checkpointr   r"   c                    ddt           ddddd}t          | t          t          f          r|                                 } | r)|s't
          j        j                            |           \  }} t          j
        |           } t          ||                                           rt          |                    |                                                     r|                    dt!          |           i           |                    t
          j                            |                                 d                    }|D ]c}|j        rZt'          j        d|j                  r@|                    t-          j        d	          t!          |j                  d
           |c S dt          || dz                                            r|                    | dz                                            5 }t3          j        |          }ddd           n# 1 swxY w Y   d|v rt-          j        |d                   |d<   |                    |           n-t7          d          rt8                              d|  d           dD ]h}t          || d|z   z                                            r>|                    d|dk    rdndt           t!          | d| z            d           |c S id}dD ]d}| d| z  }	t          ||	                                          r8|                    |	                                          j        r|dk    rdnd} ne|t=          d          |                    |t!          |	          d           | dz  }
t          ||
                                          rt          |                    |
                                                    rt?                      }|                    t
          j                            |
                                d                    }|D ]}|                     |j                   |                    d|i           | tB          z  tD          z  tF          z  }t          ||                                           rt          |                    |                                                    rt?                      }|                    t
          j                            |                                d                    }|D ]Z}||j        z  }t          |                    |                                                    r|                     |j                   [|                    d|i           nt          ||                                           rw|                    |                                           j        rK|                    t-          j        d	          t!          | j$                  t!          |           d           n t=          dt!          |            d           |S )!a  Returns a dict with information about an Algorithm/Policy checkpoint.

    If the given checkpoint is a >=v1.0 checkpoint directory, try reading all
    information from the contained `rllib_checkpoint.json` file.

    Args:
        checkpoint: The checkpoint directory (str) or a Checkpoint object.
        filesystem: PyArrow FileSystem to use to access data at the `checkpoint`. If not
            specified, this is inferred from the URI scheme provided by `checkpoint`.

    Returns:
        A dict containing the keys:
        "type": One of "Policy" or "Algorithm".
        "checkpoint_version": A version tuple, e.g. v1.0, indicating the checkpoint
        version. This will help RLlib to remain backward compatible wrt. future
        Ray and checkpoint versions.
        "checkpoint_dir": The directory with all the checkpoint files in it. This might
        be the same as the incoming `checkpoint` arg.
        "state_file": The main file with the Algorithm/Policy's state information in it.
        This is usually a pickle-encoded file.
        "policy_ids": An optional set of PolicyIDs in case we are dealing with an
        Algorithm checkpoint. None if `checkpoint` is a Policy checkpoint.
    	AlgorithmcloudpickleN)rp   formatr'   checkpoint_dirr   
policy_ids
module_idsr   Fr%   zcheckpoint-\d+z0.1)r'   r   rllib_checkpoint.json)fpr'   no_rllib_checkpoint_json_filez>No `rllib_checkpoint.json` file found in checkpoint directory zG! Trying to extract checkpoint info from other files found in that dir.)pklmsgpckzpolicy_state.Policyr   r   )rp   r   r'   r   )r   r   r   zalgorithm_state.zGiven checkpoint does not seem to be valid! No file with the name `algorithm_state.[pkl|msgpack|msgpck]` (or `checkpoint-[0-9]+`) found.)r   r   policiesr   r   )r'   r   r   zGiven checkpoint (zV) not found! Must be a checkpoint directory (or a file for older checkpoint versions).)%CHECKPOINT_VERSIONr^   Checkpoint_trainCheckpoint_tuneto_directoryr`   ra   rb   rc   rY   rZ   r   r_   r   r   updater\   FileSelectorr   rematch	base_namer   Versionr   rj   r   r   loggerwarningr   setaddr   r   r   parent)r   r   infofile_info_listr   r   rllib_checkpoint_info	extensionr   r   policies_dirr   modules_dirr   
module_dirs                  r0   get_checkpoint_infor    sF   < 0 D */ABB /,,..
 L* L ")!6!?!?
!K!K
J j))J *j&9&9&;&;<< N
  !4!4!6!677B B N
 	%s:7888 $11J##J$7$7$9$9U#KK
 
 ( 		  		 I   8-y/BCC  KK29/%2H2H*-i.A*B*B     KKK &==GGII
 
 	 --55??AA  8(,	Q%	8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
 $'<<<>Eo)*>?? ?%&:; KK-.... 788 *!* * *   + 	 	I!Z?Y+FGQQSS    (3<3E3E--9.@&)*7Ry7R7R*R&S&S	     5 	 	I#&D&D&DDJ":z/B/B/D/DEE,,Z-@-@-B-BCCK +4u*<*<)>Y  
 	 !*oo 	
 	
 	
 "J.j,*?*?*A*ABB 		4w$$\%:%:%<%<==H
 H
 		4 J'55
''(=(=(?(?5'QQ N , 4 4	y23333KKz2333 %&  "" 	 j**=*=*?*?@@ 	4W$$[%9%9%;%;<<F
 F
 	4 J'55
''(<(<(>(>%'PP N , 8 8	 )9+>>
:33J4G4G4I4IJJKK 8NN9#6777KKz2333
 	:z':':'<'<==
$$Z%8%8%:%:;;C
 	&-oe&<&<"%j&7"8"8!*oo 	
 	
 	
 	
 NZ N N N
 
 	

 Ks   6HHHmsgpack_checkpoint_dirc                 r   ddl m} ddlm} ddlm} t          d          }|                    |           }|                                }t          |d                   |d<   t          |d         t                    s|d                                         |d<   n|                    |d                   |d<   i }d	|v r&d
|d	         v r|d	                             d
i           }t          |d	         d<   t          |d	         d<   t!          t"                    |d<   t$          j                            |d          }	t+          |	d          5 }
|                    ||
           ddd           n# 1 swxY w Y   t+          t$          j                            |d          d          5 }
t/          j        d|d         d|	t1          |                                          t4          j        t4          j        d|
           ddd           n# 1 swxY w Y   |                                D ]v\  }} ||d           t$          j                            |d|          }t%          j        |d           |                    |          }|                     ||d           w|!                                 |S )a
  Converts an Algorithm checkpoint (pickle based) to a msgpack based one.

    Msgpack has the advantage of being python version independent.

    Args:
        checkpoint: The directory, in which to find the Algorithm checkpoint (pickle
            based).
        msgpack_checkpoint_dir: The directory, in which to create the new msgpack
            based checkpoint.

    Returns:
        The directory in which the msgpack checkpoint has been created. Note that
        this is the same as `msgpack_checkpoint_dir`.
    r   )r   )AlgorithmConfig)validate_module_idTrU   algorithm_classconfigworkerpolicy_statespolicy_mapping_fnis_policy_to_trainr'   zalgorithm_state.msgpckwbNr   rC   r   r   )rp   r'   r   r   r   r   r   r  exist_okpolicy_statecheckpoint_format)"ray.rllib.algorithmsr   %ray.rllib.algorithms.algorithm_configr  ray.rllib.core.rl_moduler  r{   r   __getstate__r   r^   dict	serialize_serialize_dictry   r   r\   r  osr!   joinopenro   rj   listkeysr7   r   r   r   makedirs
get_policyexport_checkpointstop)r   r  r   r  r  r   algor   r!  r   r   pidr(  
policy_dirpolicys                  r0   convert_to_msgpack_checkpointr>    s[   & /.....EEEEEE;;;;;; !t,,,G $$Z00DE  .e4E.FGGE
eHot,, K/3355h)99%/JJh M5_h??h++OR@@ ,<E(O'(,<E(O() #&&8"9"9E
 46NOOJ	j$		 1UA               
bgll13JKKS	Q	Q 
UV	#&+,@&A#("=#5#5#7#788"!n  	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 +0022 

 

\3d++++W\\"8*cJJ

J....%%  %' 	! 	
 	
 	
 	
 	IIKKK!!s%   E11E58E5*AHHHpolicy_checkpointc                     ddl m} |                    |           }t          j        |d           |                    ||                                d           ~|S )a
  Converts a Policy checkpoint (pickle based) to a msgpack based one.

    Msgpack has the advantage of being python version independent.

    Args:
        policy_checkpoint: The directory, in which to find the Policy checkpoint (pickle
            based).
        msgpack_checkpoint_dir: The directory, in which to create the new msgpack
            based checkpoint.

    Returns:
        The directory in which the msgpack checkpoint has been created. Note that
        this is the same as `msgpack_checkpoint_dir`.
    r   )r  Tr%  r   r'  )ray.rllib.policy.policyr  r   r1  r6  r8  rB   )r?  r  r  r=  s       r0   $convert_to_msgpack_policy_checkpointrB    s}    & /.....##$566FK&6666
%%''#     	!!r2   FrV   c                     	 ddl }ddl}|                                 |S # t          $ r | rt	          d          Y dS w xY w)al  Tries importing msgpack and msgpack_numpy and returns the patched msgpack module.

    Returns None if error is False and msgpack or msgpack_numpy is not installed.
    Raises an error, if error is True and the modules could not be imported.

    Args:
        error: Whether to raise an error if msgpack/msgpack_numpy cannot be imported.

    Returns:
        The `msgpack` module, with the msgpack_numpy module already patched in. This
        means you can already encde and decode numpy arrays with the returned module.

    Raises:
        ImportError: If error=True and msgpack/msgpack_numpy is not installed.
    r   NzkCould not import or setup msgpack and msgpack_numpy! Try running `pip install msgpack msgpack_numpy` first.)r   msgpack_numpypatchr   ImportError)rV   r   rD  s      r0   r{   r{     s    " 	    	I  	 	 	s     ??rW   )F)Hr   r   rj   loggingr1  rY   r	  rI   typesr   typingr   r   r   r   r   r	   r
   
pyarrow.fsr`   	packagingr   r7   ray.cloudpickler   rn   ray.rllib.corer   r   r   ray.rllib.utilsr   ray.rllib.utils.actor_managerr   ray.rllib.utils.annotationsr   r   ray.rllib.utils.serializationr   r   ray.rllib.utils.typingr   	ray.trainr   r  ray.tuner  ray.tune.utils.file_transferr   ray.utilr   ray.util.annotationsr   	getLoggerr   r  r  r  rf   ABCr   ra   rb   r\   r   r   FileInfor   r  r>  rB  r{   r,   r2   r0   <module>r[     s   



    				  				  " " " " " " F F F F F F F F F F F F F F F F F F           



                     
 ' & & & & & C C C C C C        K J J J J J J J , , , , , , 4 4 4 4 4 4 2 2 2 2 2 2 ? ? ? ? ? ?       * * * * * *		8	$	$. %W_U++ ,;GOE,B,B ) Wp	 p	 p	 p	 p	SW p	 p	 p	f67:0 6 6 6 6 6 6;wz* ;t ; ; ; ;
  59C Cc+_<=C01C 
#s(^C C C CL X"c+_<=X"X" 	X" X" X" X"v  "S"2OCD " " 	 "  "  "  "F  d      r2   