o
    i<                     @   sj  d dl mZ d dlmZmZ d dlmZmZmZ d dl	m
Z
 d dlZd dlmZ d dlmZ d dlmZ erHd d	lmZ d d
lmZ d dlmZ dgZdede
d fddZdede
d fddZd'ddZd(ddZd)dedee dB defddZ 	d*deej! dee"ej#B  d ede$e$ej!  fd!d"Z%ed#ed$Z&	d*d%e&dee"ej#B  d ede$e& fd&dZ'dS )+    )OrderedDict)IteratorSequence)castTYPE_CHECKINGTypeVar)TypeIsN_get_device_index)Module)comm)ScriptMethod)ScriptModule)EnabledProxy	replicatemodulereturnr   c                 C      dd l }t| |jjS Nr   )	torch.jit
isinstancejitr   r   torch r   ]/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/torch/nn/parallel/replicate.py_is_script_module      r   r   c                 C   r   r   )r   r   _Cr   r   r   r   r   _is_script_method   r   r   c                  C   s   dd l } | j S r   )r   r   r   r   r   r   r   _init_script_module!      
r!   r   c                  C   s   dd l } | jjjS r   )torch.jit._stater   _state_enabledr    r   r   r   _is_jit_enabled'   r"   r&   memoc                 C   s   dt dtt  fdd}t sdS |d u rt }||  t| r2|||  tdd || D S |  D ]}||v r=q6t	||sE dS q6dS )	Nr   r   c                 S   s   |   }t| |S N)modulesnext)r   genr   r   r   descendant_modules6   s   z0_replicatable_module.<locals>.descendant_modulesTc                 s   s    | ]}t |V  qd S r(   )r   ).0
descendantr   r   r   	<genexpr>D   s    
z'_replicatable_module.<locals>.<genexpr>F)
r   r   r&   setaddr   updateallchildren_replicatable_module)r   r'   r,   childr   r   r   r5   4   s$   

r5   Ftensorsdevicesdetachc                    sb   ddl m} |rt|S tdkr/|j|gR    fddtdt tD S g S )Nr   )	Broadcastc                    s    g | ]} ||t   qS r   )len)r-   itensor_copiesr7   r   r   
<listcomp>`   s    z0_broadcast_coalesced_reshape.<locals>.<listcomp>)torch.nn.parallel._functionsr:   r   broadcast_coalescedr;   applyrange)r7   r8   r9   r:   r   r=   r   _broadcast_coalesced_reshapeS   s   rD   T)boundnetworkc                    s  t | std|sg S dd |D }t|}t|  }dd t|D }t|||}t|  }g }g }	|D ]}
|
jrE|sE|	|
 q8|		|
 q8dd t|D }dd t|	D }t|||d}t|	|d	d}t| 
 }d
d |D  i }t|D ]\}}|||< t|D ]}| }t |_ | 	| qq~t|D ]\}}|j D ]6\}}|d u rt|D ]} | | }d |j|< qq|| }t|D ]} | | }t|| | |  qq|j D ]?\}}|d u rt|D ]} | | }d |j|< qq|| }t|D ]} | | }|| | }t||| ||j|< q
q|j D ]K\}}
|
d u rIt|D ]} | | }d |j|< q8q+|
jrW|sW|}||
 }n|}||
 }t|D ]} | | }t|||| |  qaq+q fddt|D S )NzJCannot replicate network where python modules are children of ScriptModulec                 S   s   g | ]}t |d qS )Tr	   )r-   xr   r   r   r?   x       zreplicate.<locals>.<listcomp>c                 S      i | ]\}}||qS r   r   )r-   idxparamr   r   r   
<dictcomp>|   rI   zreplicate.<locals>.<dictcomp>c                 S   rJ   r   r   r-   rK   bufr   r   r   rM      rI   c                 S   rJ   r   r   rN   r   r   r   rM      rI   )r9   Tc                 S   s   g | ]}g qS r   r   )r-   _r   r   r   r?      s    c                    s   g | ]}t t | d  qS )r   )r   rE   )r-   jmodule_copiesr   r   r?      s    )r5   RuntimeErrorr;   list
parameters	enumeraterD   buffersrequires_gradappendr)   rC   _replicate_for_data_parallelr   _former_parameters_modulesitemssetattr_parameters_buffers)rG   r8   r9   num_replicasparamsparam_indicesparam_copiesrX   
buffers_rgbuffers_not_rgrO   buffer_indices_rgbuffer_indices_not_rgbuffer_copies_rgbuffer_copies_not_rgr)   module_indicesr<   r   rQ   replicakeyr6   
module_idxrL   	param_idx
param_copybuffer_copies
buffer_idxr   rR   r   r   k   s   



)r   r   )r   r   r(   )F)(collectionsr   collections.abcr   r   typingr   r   r   typing_extensionsr   r   torch._utilsr
   torch.nn.modulesr   torch.nn.parallelr   torch._Cr   r   r   r#   r   __all__r   objectr   r!   r&   r0   boolr5   TensorintdevicerU   rD   rE   r   r   r   r   r   <module>   sN    

 "
