o
    1 i$                     @   s  d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
 d dlZd dlmZ d dlmZmZmZmZ d dlmZ d dlmZ dZd	Zd
ZdZdZdZdZdZeeeeeeeegZ e!e"Z#dd Z$edd Z%ede&fddZ'de&de(fddZ)ede
e&ee	f de(fddZ*ed>de&de
ee	f de(fd d!Z+d"d# Z,ede&d	efd$d%Z-d&d' Z.ede&d(efd)d*Z/d+d, Z0ede&de(fd-d.Z1ede&defd/d0Z2d1d2 Z3d3d4 Z4d5e&d6e&d7e&fd8d9Z5G d:d; d;Z6e6 Z7ej8j9j:;e7j< G d<d= d=Z=dS )?    N)partial)FunctionType)CallableOptionalTypeUnion)_internal_kv_del_internal_kv_get_internal_kv_initialized_internal_kv_put	TuneError)DeveloperAPIZtrainable_classenv_creatorZrllib_modelZrllib_preprocessorZrllib_action_distZrllib_inputZrllib_connectorZ__test__c                 C      t t| S N)_global_registrycontainsTRAINABLE_CLASStrainable_name r   ]/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/tune/registry.py_has_trainable(   s   r   c                 C   s   t |  tt| S r   )validate_trainabler   getr   r   r   r   r   get_trainable_cls,   s   r   r   c                 C   s&   t | st| std|  d S d S )NzUnknown trainable: )r   _has_rllib_trainabler   r   r   r   r   r   2   s   r   returnc              	   C   s8   zddl m} W n ttfy   Y dS w |  t| S )Nr   )_register_allF)Z	ray.rllibr   ImportErrorModuleNotFoundErrorr   )r   r   r   r   r   r   8   s   r   	trainablec                 C   s^   ddl m} t| trt| } t| tot| |}|p.t| t o.t| tp.t| tp.t	| S )zCheck if a given trainable is a function trainable.
    Either the trainable has been wrapped as a FunctionTrainable class already,
    or it's still a FunctionType/partial/callable.r   )FunctionTrainable)
ray.tune.trainabler#   
isinstancestrr   type
issubclassr   r   callable)r"   r#   Zis_wrapped_funcr   r   r   is_function_trainableC   s   

r*   Tnamewarnc                 C   s   ddl m}m} t|trtd n!t|tst|tr'td ||}nt	|r4t
d ||}t||s>td|tt| | dS )a  Register a trainable function or class.

    This enables a class or function to be accessed on every Ray process
    in the cluster.

    Args:
        name: Name to register.
        trainable: Function or tune.Trainable class. Functions must
            take (config, status_reporter) as arguments and will be
            automatically converted into a class during registration.
    r   )	Trainablewrap_functionzDetected class for trainable.z Detected function for trainable.z=Detected unknown callable for trainable. Converting to class.z0Second argument must be convertable to TrainableN)r$   r-   r.   r%   r'   loggerdebugr   r   r)   infor(   	TypeErrorr   registerr   )r+   r"   r,   r-   r.   r   r   r   register_trainableZ   s   





r4   c                   C      t t d S r   )r   unregister_allr   r   r   r   r   _unregister_trainablesx      r7   c                 C   $   t |s	td|tt| | dS )zRegister a custom environment for use with RLlib.

    This enables the environment to be accessed on every Ray process
    in the cluster.

    Args:
        name: Name to register.
        env_creator: Callable that creates an env.
    !Second argument must be callable.N)r)   r2   r   r3   ENV_CREATOR)r+   r   r   r   r   register_env|   s   
r<   c                   C   r5   r   )r   r6   r;   r   r   r   r   _unregister_envs   r8   r=   input_creatorc                 C   r9   )zRegister a custom input api for RLlib.

    Args:
        name: Name to register.
        input_creator: Callable that creates an
            input reader.
    r:   N)r)   r2   r   r3   RLLIB_INPUT)r+   r>   r   r   r   register_input   s   	
r@   c                   C   r5   r   )r   r6   r?   r   r   r   r   _unregister_inputs   r8   rA   c                 C   r   r   )r   r   r?   r+   r   r   r   registry_contains_input      rC   c                 C   r   r   )r   r   r?   rB   r   r   r   registry_get_input   rD   rE   c                   C   s   t   t  t  d S r   )rA   r=   r7   r   r   r   r   _unregister_all   s   
rF   c                 C   s   t t| | d S r   )r   r3   TEST)keyvaluer   r   r   _check_serializability   s   rJ   prefixcategoryrH   c                 C   s*   d|  d d | d d | d S )zGenerate a binary key for the given category and key.

    Args:
        prefix: Prefix
        category: The category of the item
        key: The unique identifier for the item

    Returns:
        The key to use for storing a the value.
    s   TuneRegistry:ascii   :   /)encode)rK   rL   rH   r   r   r   	_make_key   s   rQ   c                   @   sp   e Zd Zddee fddZedd Zdd Zd	d
 Z	dd Z
ddee fddZdd Zdd Zdd ZdS )	_RegistryNrK   c                 C   s   i | _ || _t | _d| _dS )z2If no prefix is given, use runtime context job ID.FN)	_to_flush_prefixset_registered_atexit_handler_registered)selfrK   r   r   r   __init__   s   
z_Registry.__init__c                 C   s   | j s
t  | _ | j S r   )rT   rayZget_runtime_contextZ
get_job_idrX   r   r   r   rK      s   z_Registry.prefixc                 C   s4   | j rd S tjjjjtjkrd S tt	 d| _ d S )NT)
rW   rZ   _privateworkerZglobal_workermodeZSCRIPT_MODEatexitr3   rF   r[   r   r   r   _register_atexit   s   

z_Registry._register_atexitc                 C   sN   |t vrddlm} |d|t t|| j||f< t r%|   dS dS )a#  Registers the value with the global registry.

        Args:
            category: The category to register under.
            key: The key to register under.
            value: The value to register.

        Raises:
            PicklingError: If unable to pickle to provided file.
        r   r   z Unknown category {} not among {}N)	KNOWN_CATEGORIESZray.tuner   formatpickleZdumps_debugrS   r
   flush_values)rX   rL   rH   rI   r   r   r   r   r3      s   
z_Registry.registerc                 C   s2   t  rtt| j|| d S | j||fd  d S r   )r
   r   rQ   rK   rS   pop)rX   rL   rH   r   r   r   
unregister   s   z_Registry.unregisterrL   c                 C   sH   t  }| jD ]\}}|r||kr| || q|||f q|| _d S r   )rU   rV   rf   add)rX   rL   	remainingcatrH   r   r   r   r6     s   
z_Registry.unregister_allc                 C   s.   t  rtt| j||}|d uS ||f| jv S r   )r
   r	   rQ   rK   rS   rX   rL   rH   rI   r   r   r   r   
  s   z_Registry.containsc                 C   sN   t  rtt| j||}|d u rtd||t|S t| j||f S )Nz'Registry value for {}/{} doesn't exist.)	r
   r	   rQ   rK   
ValueErrorrb   rc   loadsrS   rj   r   r   r   r     s   

z_Registry.getc                 C   sV   |    | j D ]\\}}}tt| j|||dd | j||f q	| j  d S )NT)	overwrite)	r`   rS   itemsr   rQ   rK   rV   rg   clearrj   r   r   r   rd     s   z_Registry.flush_valuesr   )__name__
__module____qualname__r   r&   rY   propertyrK   r`   r3   rf   r6   r   r   rd   r   r   r   r   rR      s    
	rR   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
_ParameterRegistryc                 C   s   i | _ i | _d S r   )to_flush
referencesr[   r   r   r   rY   +  s   
z_ParameterRegistry.__init__c                 C   s"   || j |< t r|   d S d S r   )ru   rZ   is_initializedflushrX   kvr   r   r   put/  s   
z_ParameterRegistry.putc                 C   s"   t  s	| j| S t | j| S r   )rZ   rw   ru   r   rv   )rX   rz   r   r   r   r   4  s   
z_ParameterRegistry.getc                 C   sJ   | j  D ]\}}t|tjr|| j|< qt|| j|< q| j   d S r   )ru   rn   r%   rZ   Z	ObjectRefrv   r|   ro   ry   r   r   r   rx   9  s
   z_ParameterRegistry.flushN)rp   rq   rr   rY   r|   r   rx   r   r   r   r   rt   *  s
    rt   )T)>r_   logging	functoolsr   typesr   typingr   r   r   r   rZ   Zray.cloudpickleZcloudpicklerc   Zray.experimental.internal_kvr   r	   r
   r   Zray.tune.errorr   Zray.util.annotationsr   r   r;   ZRLLIB_MODELZRLLIB_PREPROCESSORZRLLIB_ACTION_DISTr?   ZRLLIB_CONNECTORrG   ra   	getLoggerrp   r/   r   r   r&   r   boolr   r*   r4   r7   r<   r=   r@   rA   rC   rE   rF   rJ   rQ   rR   r   r\   r]   Z_post_init_hooksappendrd   rt   r   r   r   r   <module>   sp    

"Z