§
    ŠŠtjq+  ã                   óf  — 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	Z	d dl
mZ d dlmZ g d¢Z ej        ¦   «         aej        Zej        Z G d„ de¦  «        Z G d„ d¦  «        Z e¦   «         Zd	„ Zd
„ Zd„ Zd„ Zd„ Zd„ Z e j        dg d¢¦  «        Z  e j        dddg¦  «        Z!dS )é    N)ÚEnum)Ú_get_current_rpc_agent)ÚRPCExecModeÚ	serializeÚdeserializeÚ	PythonUDFÚRemoteExceptionc                   ó   — e Zd ZdZdZdZdZdS )r   ÚsyncÚasyncÚ	async_jitÚremoteN)Ú__name__Ú
__module__Ú__qualname__ÚSYNCÚASYNCÚ	ASYNC_JITÚREMOTE© ó    ú\/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/distributed/rpc/internal.pyr   r      s"   € € € € € Ø€DØ€EØ€IØ€F€F€Fr   r   c                   ó„   — e Zd ZdZd„ Zd„ Zed„ ¦   «         Zd„ Zed„ ¦   «         Z	d„ Z
d„ Zed	„ ¦   «         Zd
„ Zd„ Zd„ ZdS )Ú_InternalRPCPicklera	  
    This class provides serialize() and deserialize() interfaces to serialize
    data to be "binary string + tensor table" format
    So for RPC python UDF function and args, non tensor data will be serialized
    into regular binary string, tensor data will be put into thread local tensor
    tables, this serialization format is consistent with builtin operator and args
    using JIT pickler. This format will make tensor handling in C++ much easier,
    e.g. attach tensor to distributed autograd graph in C++
    c                 óŒ   — t           j                             ¦   «         | _        | j        | j        t
          j        <   i | _        d S ©N)ÚcopyregÚdispatch_tableÚcopyÚ_dispatch_tableÚ_tensor_reducerÚtorchÚTensorÚ_class_reducer_dict)Úselfs    r   Ú__init__z_InternalRPCPickler.__init__+   s9   € å&Ô5×:Ò:Ñ<Ô<ˆÔØ-1Ô-AˆÔ�Uœ\Ñ*à#%ˆÔ Ð Ð r   c                 ó0   — || j         vr|| j         |<   d S d S r   )r$   )r%   Ú	obj_classÚreducers      r   Ú_register_reducerz%_InternalRPCPickler._register_reducer2   s+   € à˜DÔ4Ð4Ð4Ø29ˆDÔ$ YÑ/Ð/Ð/ð 5Ð4r   c                 ó&   — t           j        |         S r   )Ú_thread_local_tensor_tablesÚrecv_tables)ÚclsÚtensor_indexs     r   Ú_tensor_receiverz$_InternalRPCPickler._tensor_receiver7   s   € õ +Ô6°|ÔDÐDr   c                 ó–   — t           j                             |¦  «         t          t           j        ¦  «        dz
  }t          j        |ffS )Né   )r,   Úsend_tablesÚappendÚlenr   r0   )r%   Útensorr/   s      r   r!   z#_InternalRPCPickler._tensor_reducer<   s?   € å#Ô/×6Ò6°vÑ>Ô>Ð>ÝÕ6ÔBÑCÔCÀaÑGˆÝ#Ô4°|°oÐFÐFr   c                 óJ   — t           j        j                             |¦  «        S r   )ÚdistÚrpcÚPyRRefÚ_deserialize)r.   Úrref_fork_datas     r   Ú_py_rref_receiverz%_InternalRPCPickler._py_rref_receiverB   s   € åŒxŒ×+Ò+¨NÑ;Ô;Ð;r   c                 óH   — |                      ¦   «         }t          j        |ffS r   )Ú
_serializer   r=   )r%   Úpy_rrefr<   s      r   Ú_py_rref_reducerz$_InternalRPCPickler._py_rref_reducerF   s$   € Ø ×+Ò+Ñ-Ô-ˆÝ#Ô5¸Ð7HÐIÐIr   c                 ó,   — |                       |¦  «        S r   )rA   )r%   Úrrefs     r   Ú_rref_reducerz!_InternalRPCPickler._rref_reducerJ   s   € Ø×$Ò$ TÑ*Ô*Ð*r   c                 ól   — t          j        |¦  «        }t          j                             |¦  «        }|S )zŽ
        Given a serialized representation of a ScriptModule created with torch.jit.save,
        loads and returns the ScriptModule.
        )ÚioÚBytesIOr"   ÚjitÚload)r.   Úscript_module_serializedÚfÚms       r   Ú_script_module_receiverz+_InternalRPCPickler._script_module_receiverM   s,   € õ ŒJÐ/Ñ0Ô0ˆÝŒI�NŠN˜1ÑÔˆØˆr   c                 óª   — t          j        ¦   «         }t          j                             ||¦  «         t
          j        |                     ¦   «         ffS )z,
        Serializes a ScriptModule.
        )rF   rG   r"   rH   Úsaver   rM   Úgetvalue)r%   Úscript_modulerK   s      r   Ú_script_module_reducerz*_InternalRPCPickler._script_module_reducerW   s=   € õ ŒJ‰LŒLˆÝŒ	�Š�} aÑ(Ô(Ð(Ý#Ô;¸a¿jºj¹l¼l¸_ÐMÐMr   c                 ót  — t          j        ¦   «         }t          |¦  «        }| j        |_        | j        |j        t          j        j        <   | j	        |j        t          j        j
        <   t          |t          j        j        ¦  «        r| j        |j        |j        <   | j        D ]}| j        |         |j        |<   Œt%          t&          d¦  «        rt&          j        }nd}g t&          _        |                     |¦  «         t&          j        }|�|t&          _        nt&          `|                     ¦   «         |fS )ze
        Serialize non tensor data into binary string, tensor data into
        tensor table
        r3   N)rF   rG   Ú_picklerr    r   rA   r8   r9   r:   rD   ÚRRefÚ
isinstancer"   rH   ÚScriptModulerR   Ú	__class__r$   Úhasattrr,   r3   ÚdumprP   )r%   ÚobjrK   ÚpÚ
class_nameÚold_send_tablesÚtensorss          r   r   z_InternalRPCPickler.serialize_   s  € õ
 ŒJ‰LŒLˆÝ�Q‰KŒKˆØÔ/ˆÔð -1Ô,AˆÔ�œœÑ)ð +/Ô*<ˆÔ�œœÑ'õ �c�5œ9Ô1Ñ2Ô2ð 	Jà.2Ô.IˆAÔ˜Sœ]Ñ+ð Ô2ð 	Pð 	PˆJØ+/Ô+CÀJÔ+OˆAÔ˜ZÑ(Ð(õ Õ.°Ñ>Ô>ð 	#Ý9ÔEˆOˆOà"ˆOØ24Õ#Ô/à	�Šˆs‰Œˆõ .Ô9ˆØÐ&Ø6EÕ'Ô3Ð3å+Ð7à—
’
‘”˜gÐ&Ð&r   c                 ó~  — t          t          d¦  «        rt          j        }nd}|t          _        	 t          t	          j        |¦  «        ¦  «        }|                     ¦   «         }n?# t          $ r2}t          |¦  «        dz   }t          |¦  «        }||_	        Y d}~nd}~ww xY w|�|t          _        nt          `|S )zJ
        Deserialize binary string + tensor table to original obj
        r-   NzŽ Default RPC pickler does not serialize
            function code. Ensure that UDFs are defined on both caller and
            callee modules.)
rY   r,   r-   Ú
_unpicklerrF   rG   rI   ÚAttributeErrorÚstrÚ	__cause__)r%   Úbinary_dataÚtensor_tableÚold_recv_tablesÚ	unpicklerÚretÚeÚ
except_strs           r   r   z_InternalRPCPickler.deserialize”   sÔ   € õ Õ.°Ñ>Ô>ð 	#Ý9ÔEˆOˆOà"ˆOØ2>Õ#Ô/ð	Ý"¥2¤:¨kÑ#:Ô#:Ñ;Ô;ˆIØ—.’.Ñ"Ô"ˆCˆCøÝð 	ð 	ð 	õ �A‘”ðñð õ ! Ñ,Ô,ˆCàˆCŒMˆMˆMˆMˆMˆMøøøøð	øøøð Ð&Ø6EÕ'Ô3Ð3å+Ð7àˆ
s   ²5A( Á(
B$Á2(BÂB$N)r   r   r   Ú__doc__r&   r*   Úclassmethodr0   r!   r=   rA   rD   rM   rR   r   r   r   r   r   r   r       sô   € € € € € ðð ð&ð &ð &ð:ð :ð :ð
 ðEð Eñ „[ðEðGð Gð Gð ð<ð <ñ „[ð<ðJð Jð Jð+ð +ð +ð ðð ñ „[ððNð Nð Nð3'ð 3'ð 3'ðj#ð #ð #ð #ð #r   r   c                 ó6   — t                                | ¦  «        S r   )Ú_internal_rpc_picklerr   )r[   s    r   r   r   ¾   s   € Ý ×*Ò*¨3Ñ/Ô/Ð/r   c                 ó8   — t                                | |¦  «        S r   )ro   r   )re   rf   s     r   r   r   Â   s   € Ý ×,Ò,¨[¸,ÑGÔGÐGr   c                 ó’  — 	 t          | t          ¦  «        r| ‚ | j        | j        i | j        ¤Ž}n–# t
          $ r‰}dt          ¦   «                              ¦   «         › dt          |¦  «        › dt          j
        ¦   «         › �}t          |t          j        ¬¦  «         t          |t          |¦  «        ¦  «        }Y d}~nd}~ww xY w|S )zò
    This function is exclusively called from C++.
    See ``torch/csrc/distributed/rpc/python_rpc_handler.cpp``.

    Runs a Python UDF and returns its return value.
    Wraps any exception in ``RemoteException`` if the function raises.
    zOn z:
ú
)ÚfileN)rV   rb   ÚfuncÚargsÚkwargsÚ	Exceptionr   Úget_worker_infoÚreprÚ	tracebackÚ
format_excÚprintÚsysÚstderrr	   Útype)Ú
python_udfÚresultrj   rk   s       r   Ú_run_functionr‚   Æ   sê   € ð6Ý�j¥.Ñ1Ô1ð 	ØÐØ �” *¤/ÐG°ZÔ5FÐGÐGˆˆøÝð 6ð 6ð 6ð3Õ(Ñ*Ô*×:Ò:Ñ<Ô<ð 3ð 3Ý�A‰wŒwð3ð 3Ý#Ô.Ñ0Ô0ð3ð 3ð 	õ 	ˆj�sœzÐ*Ñ*Ô*Ð*Ý  ­T°!©W¬WÑ5Ô5ˆˆˆˆˆˆøøøøð6øøøð €Ms   ‚.1 ±
C»A?B?Â?Cc                 ó2  — t          | t          ¦  «        r| j                             d¦  «                             d¦  «        }d }	 |                      |¦  «        }n5# t          $ r(}t          dt          |¦  «        › d|› �¦  «        |‚d }~ww xY w|�|‚d S d S )Nzutf-8Úunicode_escapez8Failed to create original exception type. Error msg was z' Original exception on remote side was )	rV   r	   ÚmsgÚencodeÚdecodeÚexception_typeÚBaseExceptionÚRuntimeErrorrc   )r�   Úexception_msgÚexcrj   s       r   Ú_handle_exceptionr�   Ý   sÕ   € Ý�&�/Ñ*Ô*ð Øœ
×)Ò)¨'Ñ2Ô2×9Ò9Ð:JÑKÔKˆð ˆð	Ø×'Ò'¨Ñ6Ô6ˆCˆCøÝð 	ð 	ð 	ÝðJÍ3ÈqÉ6Ì6ð Jð JØ:GðJð Jñô ð ðøøøøð	øøøð ˆ?ØˆIðð ð ˆ?s   ÁA Á
BÁ&#B	Â	Bc           	      ó.   — d| j         › d|› d|› d|› d�	}|S )aÓ  
    Builds the key that RPC calls are profiled with using the autograd profiler.
    This will be the name of the corresponding Event recorded in the profiler.

    Args:
        exec_type (RPCExecMode): Type of RPC/RRef call
        func_name (str): Name of function being profiled.
        current_worker_name (str): Name of current worker.
        dst_worker_name (str): Name of the destination worker.

    Returns:
        String representing profiling key
    Úrpc_ú#ú(ú -> ú))Úvalue)Ú	exec_typeÚ	func_nameÚcurrent_worker_nameÚdst_worker_nameÚprofile_keys        r   Ú_build_rpc_profiling_keyrš   ï   s;   € ð" 	YˆyŒÐXÐX ÐXÐXÐ-@ÐXÐXÀoÐXÐXÐXð ð Ðr   c           	      ó  — t           j                             ¦   «         st          d¦  «        ‚d| j        › dt          |¦  «        › d|› d|› d�	}t           j                             ¦   «         }t           j                             ||¦  «         |S )ar  
    This function should be called from RPC/RRef functions to create a
    RecordFunction object for profiling. This function also runs the before
    callbacks that start the profiling, though the user is responsible for
    running the appropriate callbacks when the function to be profiled finishes.

    Args:
        exec_type (RPCExecMode): Type of RPC/RRef call
        func_name (str): Name of function being profiled.
        current_worker_name (str): Name of current worker.
        dest_worker_name (str): Name of the destination worker.

    Returns:
        An instance of `torch.autograd._RecordFunction`.
    z$Autograd profiler should be enabled.r�   r�   r‘   r’   r“   )r"   ÚautogradÚ_profiler_enabledÚAssertionErrorr”   rc   Ú_RecordFunctionÚ_run_before_callbacks)r•   r–   r—   Údest_worker_namer™   Úrfs         r   Ú_start_record_functionr£     sŒ   € õ  Œ>×+Ò+Ñ-Ô-ð EÝÐCÑDÔDÐDØh˜œÐhÐh­3¨y©>¬>ÐhÐhÐ<OÐhÐhÐUeÐhÐhÐh€KÝ	Œ×	'Ò	'Ñ	)Ô	)€BÝ	„N×(Ò(¨¨[Ñ9Ô9Ð9Ø€Ir   r   )rt   ru   rv   r	   r…   rˆ   )"Úcollectionsr   rF   Úpickler}   Ú	threadingrz   Úenumr   r"   Útorch.distributedÚdistributedr8   Útorch._C._distributed_rpcr   Ú__all__Úlocalr,   ÚPicklerrT   Ú	Unpicklerra   r   r   ro   r   r   r‚   r�   rš   r£   Ú
namedtupler   r	   r   r   r   ú<module>r°      sÀ  ðà Ð Ð Ð Ø €€€Ø 	€	€	€	Ø €€€Ø 
€
€
€
Ø Ð Ð Ð Ø Ð Ð Ð Ø Ð Ð Ð Ð Ð à €€€Ø  Ð  Ð  Ð  Ð  Ð  Ø <Ð <Ð <Ð <Ð <Ð <ð VÐ
UÐ
U€ð .˜iœoÑ/Ô/Ð ØŒ>€ØÔ€
ðð ð ð ð �$ñ ô ð ðWð Wð Wð Wð Wñ Wô Wð Wðv ,Ð+Ñ-Ô-Ð ð0ð 0ð 0ðHð Hð Hðð ð ð.ð ð ð$ð ð ð,ð ð ð0 #ˆKÔ" ;Ð0JÐ0JÐ0JÑKÔK€	Ø(�+Ô(Ð):¸UÐDTÐ<UÑVÔV€€€r   