o
    1 iy                     @   s  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
 er$d dlZd dl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mZmZmZmZmZmZ d dl m!Z!m"Z" d dlm#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< d dl=m>Z> d dl?m@Z@ e AeBZCeDd	d
ZEG dd deFZGdd ZHdd ZIdd ZJG dd dZKdS )    N)TYPE_CHECKINGAnyDictListOptionalTupleUnion)ray_constants)	DynamicObjectRefGeneratorMessagePackSerializedObjectMessagePackSerializerPickle5SerializedObjectPickle5WriterRawSerializedObjectSerializedRayObjectsplit_bufferunpack_pickle5_buffers)	ErrorTypeRayErrorInfo)ActorDiedErrorActorPlacementGroupRemovedActorUnavailableErrorActorUnschedulableErrorLocalRayletDiedErrorNodeDiedErrorObjectFetchTimedOutErrorObjectFreedErrorObjectLostErrorObjectReconstructionFailedError-ObjectReconstructionFailedLineageEvictedError2ObjectReconstructionFailedMaxAttemptsExceededErrorObjectRefStreamEndOfStreamErrorOutOfDiskErrorOutOfMemoryErrorOwnerDiedErrorPlasmaObjectNotAvailableRayErrorRaySystemErrorRayTaskErrorReferenceCountingAssertionErrorRuntimeEnvSetupErrorTaskCancelledErrorTaskPlacementGroupRemovedTaskUnschedulableErrorWorkerCrashedError)CompiledDAGRef)serialization_addonsZ.RAY_allow_out_of_band_object_ref_serializationTc                   @   s   e Zd ZdS )DeserializationErrorN)__name__
__module____qualname__ r5   r5   f/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/_private/serialization.pyr1   @   s    r1   c           	      C   sd   t j| |||d}|r0t jjj}|  | }| }|d u r%t j }|j	
| ||| |S )N)tensor_transport_val)ray	ObjectRef_privateworkerglobal_workercheck_connectedget_serialization_contextget_outer_object_refnilcore_workerZ#deserialize_and_register_object_refbinary)	rB   	call_siteowner_addressobject_statusr7   obj_refr;   contextouter_idr5   r5   r6   _object_ref_deserializerD   s   

rI   c                 C   s,   t | ||||}tjjjj}||| |S )a  
    Deserialize a GPU object ref. When the GPU object ref is deserialized,
    it firstly deserialize the normal object ref, and then add metadata of
    the GPU object to the GPU object manager, which will be used to fetch
    the GPU object later.

    Args:
        binary: The binary data of the object ref.
        call_site: The call site of the object ref.
        owner_address: The owner address of the object ref.
        object_status: The object status of the object ref.
        tensor_transport_val: The tensor transport value of the GPU object ref.
        gpu_object_meta: The GPU object metadata. This is used to fetch the GPU object later.

    Returns:
        The deserialized GPU object ref.
    )rI   r8   r:   r;   r<   gpu_object_managerZadd_gpu_object_metadata)rB   rC   rD   rE   r7   gpu_object_metarF   rJ   r5   r5   r6   _gpu_object_ref_deserializere   s   
rL   c                 C   s(   t jjj }| }t jj| ||S N)	r8   r:   r;   r<   r>   r?   actorActorHandleZ_deserialization_helper)Zserialized_objweak_refrG   rH   r5   r5   r6   _actor_handle_deserializer   s
   rQ   c                   @   st  e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdddddedee fddZdedeed  defd d!Z	d=deed  fd"d#Zd$d% Zd&d' Zdeed  fd(d)Zd*ee d+eeed f fd,d-Zd.d/ Zd0d1 Zd2edeeed f fd3d4Zd5ed6ed fd7d8Z d2ede!e"ef fd9d:Z#d2edeeed f fd;d<Z$dS )>SerializationContextzInitialize the serialization library.

    This defines a custom serializer for object refs and also tells ray to
    serialize several exception classes that we define for error handling.
    c                    s   | _ t  _d _ fdd} tjj| dd } t	|  fdd} tj
| dd	 } t| t  d S )
NFc                    s:   t jjj  |  \}}}|s j|dd t||ffS )NT)allow_out_of_band_serialization)r8   r:   r;   r<   r=   _serialization_helperadd_contained_object_refrQ   )obj
serializedactor_handle_idrP   selfr5   r6   actor_handle_reducer   s   z;SerializationContext.__init__.<locals>.actor_handle_reducerc                 S   s   t d)Nz1Serialization of CompiledDAGRef is not supported.)	TypeErrorrV   r5   r5   r6   compiled_dag_ref_reducer      z?SerializationContext.__init__.<locals>.compiled_dag_ref_reducerc                    s   t jjj}|   j| t|  d |j	| \} }} 
 rC|j|  rCt jjjj}|| }t|  |  |||  |ffS t|  |  |||  ffS )N)rS   rC   )r8   r:   r;   r<   r=   rU   *ALLOW_OUT_OF_BAND_OBJECT_REF_SERIALIZATIONrC   rA   Zserialize_object_refis_in_band_serializationrJ   Zis_managed_objecthexZ_get_gpu_object_metadatarL   rB   Ztensor_transportrI   )rV   r;   rD   rE   rJ   rK   rY   r5   r6   object_ref_reducer   s@   



	z9SerializationContext.__init__.<locals>.object_ref_reducerc                 S   s   t | jffS rM   )r
   Z_refsr]   r5   r5   r6   object_ref_generator_reducer      zCSerializationContext.__init__.<locals>.object_ref_generator_reducer)r;   	threadinglocal_thread_local#_torch_custom_serializer_registered_register_cloudpickle_reducerr8   rN   rO   r/   r9   r
   r0   apply)rZ   r;   r[   r^   rc   rd   r5   rY   r6   __init__   s   
*zSerializationContext.__init__c                 C   s   |t jj|< d S rM   pickleCloudPicklerdispatch)rZ   clsZreducerr5   r5   r6   rj      s   z2SerializationContext._register_cloudpickle_reducerc                 C   s   t jj|d  d S rM   )rn   ro   rp   pop)rZ   rq   r5   r5   r6   _unregister_cloudpickle_reducer   s   z4SerializationContext._unregister_cloudpickle_reducerc                    s    fdd}|t jj|< d S )Nc                    s    | ffS rM   r5   r]   custom_deserializercustom_serializerr5   r6   _CloudPicklerReducer      zSSerializationContext._register_cloudpickle_serializer.<locals>._CloudPicklerReducerrm   )rZ   rq   rv   ru   rw   r5   rt   r6    _register_cloudpickle_serializer   s   z5SerializationContext._register_cloudpickle_serializerc                 C   s   t | jddS )Nin_bandFgetattrrh   rY   r5   r5   r6   ra      rx   z-SerializationContext.is_in_band_serializationc                 C      d| j _d S )NTrh   rz   rY   r5   r5   r6   set_in_band_serialization   re   z.SerializationContext.set_in_band_serializationc                 C   r}   )NFr~   rY   r5   r5   r6   set_out_of_band_serialization   re   z2SerializationContext.set_out_of_band_serializationc                 C   s   t | jdg }|r|d S d S )Nobject_ref_stackr{   )rZ   stackr5   r5   r6   r?     s   z)SerializationContext.get_outer_object_refc                 C   s2   t | jdst | j_t S | jj}t | j_|S )Nobject_refs)hasattrrh   setr   )rZ   r   r5   r5   r6   #get_and_clear_contained_object_refs  s   

z8SerializationContext.get_and_clear_contained_object_refsN)rC   
object_refzray.ObjectRefrS   rC   c                C   sj   |   rt| jdst | j_| jj| d S |s*tjd|	  d|p&d tj
jjj| d S )Nr   z-It is not allowed to serialize ray.ObjectRef a&  . If you want to allow serialization, set `RAY_allow_out_of_band_object_ref_serialization=1.` If you set the env var, the object is pinned forever in the lifetime of the worker process and can cause Ray object leaks. See the callsite and trace to find where the serialization occurs.
Callsite: z-Disabled. Set RAY_record_ref_creation_sites=1)ra   r   rh   r   r   addr8   
exceptionsZ(OufOfBandObjectRefSerializationExceptionrb   r:   r;   r<   rA   Zadd_object_ref_reference)rZ   r   rS   rC   r5   r5   r6   rU     s   
z-SerializationContext.add_contained_object_refdataout_of_band_tensorsztorch.Tensorreturnc           	   	   C   s   ddl m} | j}|du}|r|| z2zt|\}}t|dkr,tj||d}nt|}W n tjj	y>   t
 w W |rG|g  |S |rQ|g  w w )aY  

        Args:
            data: The data to deserialize.
            out_of_band_tensors: Tensors that were sent out-of-band. If this is
                not None, then the serialized data will contain placeholders
                that need to be replaced with these tensors.

        Returns:
            Any: The deserialized object.
        r   ChannelContextN)buffers)ray.experimental.channelr   get_currentserialization_contextreset_out_of_band_tensorsr   lenrn   loadsPicklingErrorr1   )	rZ   r   r   r   ctxZenable_gpu_objectsrz   r   rV   r5   r5   r6   _deserialize_pickle5_data3  s*   



z.SerializationContext._deserialize_pickle5_datac                    s`   t |\}}|d tjkr| || ng  z fdd}t||}W |S  ty/   t w )Nr   c                    s    |  S rM   r5   )indexZpython_objectsr5   r6   _python_deserializeri  r_   zLSerializationContext._deserialize_msgpack_data.<locals>._python_deserializer)r   r	   OBJECT_METADATA_TYPE_PYTHONr   r   r   	Exceptionr1   )rZ   r   metadata_fieldsr   msgpack_dataZpickle5_datar   rV   r5   r   r6   _deserialize_msgpack_dataX  s   z.SerializationContext._deserialize_msgpack_datac                 C   s0   |sJ |  ||}|sJ t }|| |S rM   )r   r   ZParseFromString)rZ   r   r   Zpb_bytesray_error_infor5   r5   r6   _deserialize_error_infoq  s   
z,SerializationContext._deserialize_error_infoc                 C   s\   |st  S | ||}|dsJ |jdrt|jjS |jds'J t |jjdS )Nactor_died_errorcreation_task_failure_contextactor_died_error_context)cause)r   r   HasFieldr   r&   Zfrom_ray_exceptionr   r   )rZ   r   r   r   r5   r5   r6   _deserialize_actor_died_errorz  s   z2SerializationContext._deserialize_actor_died_errorc                 C   sz  |r4| d}|d tjtjfv r| |||S |d tjkr*|d u r&dS | S |d tjkrL| |||}|d d |dd  dk}}t||S zt	|d }	W n t
ye   t
d| d| w |	tdkry| |||}t|S |	td	krt S |	td
kr| ||S |	tdkrt S |	tdkrzd}
|r| ||}|j}
t|
dW S  tjjjy   | |||}t| Y S w |	tdkrt| | | S |	tdkrt| | | S |	tdkrt| | | S |	tdkr| ||}t|jS |	tdkr-| ||}t |jS |	tdkrAt!| | | S |	tdkrUt"| | | S |	tdkrit#| | | S |	tdkr}t$| | | S |	tdkrt%| | | S |	tdkrt&| | | S |	tdkr| ||}d}|'dr|j(j}t)|dS |	tdkrt* S |	tdkrt+ S |	tdkr| ||}t,|jS |	tdkr | ||}t-|jS |	td krt. S |	td!kr,| ||}|'d"r$|j/j0}nd }t1|j|S t2d#t3|	 S |r;t4d$t5S )%N   ,r       r      1zCan't deserialize object: z, metadata: TASK_EXECUTION_EXCEPTIONZWORKER_DIEDZ
ACTOR_DIEDZLOCAL_RAYLET_DIEDTASK_CANCELLED )error_messageZOBJECT_LOSTZOBJECT_FETCH_TIMED_OUTZOUT_OF_DISK_ERRORZOUT_OF_MEMORYZ	NODE_DIEDZOBJECT_DELETEDZOBJECT_FREEDZ
OWNER_DIEDZOBJECT_UNRECONSTRUCTABLEZ.OBJECT_UNRECONSTRUCTABLE_MAX_ATTEMPTS_EXCEEDEDZ(OBJECT_UNRECONSTRUCTABLE_LINEAGE_EVICTEDZRUNTIME_ENV_SETUP_FAILEDruntime_env_setup_failed_errorZTASK_PLACEMENT_GROUP_REMOVEDZACTOR_PLACEMENT_GROUP_REMOVEDZTASK_UNSCHEDULABLE_ERRORZACTOR_UNSCHEDULABLE_ERRORZEND_OF_STREAMING_GENERATORZACTOR_UNAVAILABLEactor_unavailable_errorzUnrecognized error type z+non-null object should always have metadata)6splitr	   #OBJECT_METADATA_TYPE_CROSS_LANGUAGEr   r   ZOBJECT_METADATA_TYPE_RAWZ
to_pybytes!OBJECT_METADATA_TYPE_ACTOR_HANDLErQ   intr   r   Valuer&   
from_bytesr.   r   r   r   r   r+   googleprotobufmessageDecodeErrorr   rb   rD   rC   r   r"   r#   r   r)   r   r$   r   r    r   r   r   r*   r,   r   r-   r   r!   r   actor_idr   r'   str
ValueErrorr%   )rZ   r   metadatar   r   r   rV   rW   rP   Z
error_typer   
error_info	error_msgr   r5   r5   r6   _deserialize_object  s   








z(SerializationContext._deserialize_objectserialized_ray_objectsgpu_objectsc                 C   s
  t |t |ks
J t| jdsg | j_g }t||D ]g\}\}}}zNz#| jj| d }	|d ur=| }
|
|v r=||
 }	| ||||	}W n tyd } zt	
| t|t }W Y d }~nd }~ww W | jjrp| jj  n| jjr|| jj  w w || q|S )Nr   )r   r   rh   r   zipappendrb   r   r   logger	exceptionr'   	traceback
format_excrr   )rZ   r   r   r   resultsr   r   r   	transportZobject_tensorsZ	object_idrV   er5   r5   r6   deserialize_objects"  sD   
z(SerializationContext.deserialize_objectsc              
   C   st   t  }z(z|   tj|d|jd}W n ty% } z|   |d }~ww W |   n|   w t||||  S )N   )protocolbuffer_callback)	r   r   rn   dumpsr   r   r   r   r   )rZ   r   valuewriterZinbandr   r5   r5   r6   _serialize_to_pickle5I  s    
z*SerializationContext._serialize_to_pickle5c           
         s   g }t |tr,t|jjtrttd	d}|
 }n3ttd	d}|
 }n$t |tjjrM| \}}}|sA|| tj}||rIdnd }ntj}g   fdd}t||} rjtj}| | }	nd }	t||||	S )Nr   asciir   r      0c                    s   t  } |  |S rM   )r   r   )or   r   r5   r6   _python_serializer  s   
zFSerializationContext._serialize_to_msgpack.<locals>._python_serializer)
isinstancer(   
issubclassr   	__class__r+   r   r   r   encodeto_bytesr8   rN   rO   rT   r   r	   r   r   r   r   r   r   r   )
rZ   r   Zcontained_object_refsr   rW   rX   rP   r   r   Zpickle5_serialized_objectr5   r   r6   _serialize_to_msgpack[  s8   



z*SerializationContext._serialize_to_msgpackr   c                 C   s8   | j sddlm} |   d| _ | |\}}||fS )zRetrieve GPU data from `value` and store it in the GPU object store. Then, return the serialized value.

        Args:
            value: The value to serialize.

        Returns:
            Serialized value.
        r   )TorchTensorTypeT)ri   Z*ray.experimental.channel.torch_tensor_typer   Zregister_custom_serializer_serialize_and_retrieve_tensors)rZ   r   r   serialized_valtensorsr5   r5   r6   serialize_gpu_objects  s   
z*SerializationContext.serialize_gpu_objectsobj_idr   c                 C   s6   |dusJ dt jjj}|j}|jj||dd dS )aM  
        Store GPU objects in the GPU object store.

        Args:
            obj_id: The object ID of the value. `obj_id` is required, and the GPU data (e.g. tensors) in `value`
                will be stored in the GPU object store with the key `obj_id`.
            tensors: The tensors to store in the GPU object store.
        Nzd`obj_id` is required, and it is the key to retrieve corresponding tensors from the GPU object store.T)Z
is_primary)r8   r:   r;   r<   rJ   Zgpu_object_store
add_object)rZ   r   r   r;   rJ   r5   r5   r6   store_gpu_objects  s   


z&SerializationContext.store_gpu_objectsc                 C   s   t |tr	t|S | |S )zSerialize an object.

        Args:
            value: The value to serialize.

        Returns:
            Serialized value.
        )r   bytesr   r   )rZ   r   r5   r5   r6   	serialize  s   

zSerializationContext.serializec              	   C   sb   ddl m} | j}|j}|d z| |}W || n|| w |g \}}||fS )z
        Serialize `value` and return the serialized value and any tensors retrieved from `value`.
        This is only used for GPU objects.
        r   r   T)r   r   r   r   Zuse_external_transportZset_use_external_transportr   r   )rZ   r   r   r   Zprev_use_external_transportr   r   _r5   r5   r6   r     s   

z4SerializationContext._serialize_and_retrieve_tensorsrM   )%r2   r3   r4   __doc__rl   rj   rs   ry   ra   r   r   r?   r   boolr   r   rU   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r5   r5   r5   r6   rR      sv    S	
#

)

	

 
'7


rR   )Lloggingrf   r   typingr   r   r   r   r   r   r   ZtorchZgoogle.protobuf.messager   Zray._private.utilsr8   Zray.cloudpickleZcloudpicklern   Zray.exceptionsZray._privater	   Zray._rayletr
   r   r   r   r   r   r   r   r   Zray.core.generated.common_pb2r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   Z!ray.experimental.compiled_dag_refr/   Zray.utilr0   	getLoggerr2   r   Zenv_boolr`   r   r1   rI   rL   rQ   rR   r5   r5   r5   r6   <module>   s2    $,p
!"
