a
    8Sicw                     @   sf   d dl mZ ddlmZ ddlmZ dd Zdd Zd	d
 Zdd Z	dddZ
dddZdddZdS )   )comm    _get_device_index)OrderedDictc                 C   s   dd l }t| |jjS Nr   )	torch.jit
isinstancejitScriptModulemoduletorch r   W/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/nn/parallel/replicate.py_is_script_module   s    r   c                 C   s   dd l }t| |jjS r   )r   r	   _CScriptMethodr   r   r   r   _is_script_method   s    r   c                  C   s   dd l } | j S r   )r   r
   r   r   r   r   r   _init_script_module   s    r   c                  C   s   dd l } | jjjS r   )r   r
   _state_enabledr   r   r   r   _is_jit_enabled   s    r   Nc                 C   s   dd }t  sdS |d u r t }||  t| rV|||  tdd || D S |  D ]}||v rlq^t||s^ dS q^dS )Nc                 S   s   |   }t| |S N)modulesnext)r   genr   r   r   descendant_modules%   s    z0_replicatable_module.<locals>.descendant_modulesTc                 s   s   | ]}t |V  qd S r   )r   ).0Z
descendantr   r   r   	<genexpr>3   s   z'_replicatable_module.<locals>.<genexpr>F)r   setaddr   updateallchildren_replicatable_module)r   memor   childr   r   r   r&   "   s"    

r&   Fc                    sf   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 d S )Nr   )	Broadcastr   c                    s    g | ]} ||t   qS r   )len)r   iZtensor_copiestensorsr   r   
<listcomp>H   s   z0_broadcast_coalesced_reshape.<locals>.<listcomp>)
_functionsr)   r   Zbroadcast_coalescedr*   applyrange)r-   devicesdetachr)   r   r,   r   _broadcast_coalesced_reshape@   s    r4   c                    s  t | std|sg S dd |D }t|}t|  }dd t|D }t|||}t|  }g }g }	|D ]$}
|
jr|s|	|
 qp|		|
 qpdd t|D }dd t|	D }t|||d}t|	|d	d}t| 
 }d
d |D  i }t|D ]>\}}|||< t|D ]$}| }t |_ | 	| qqt|D ]\}}|j D ]t\}}|d u rt|D ]} | | }d |j|< qrn8|| }t|D ]&} | | }t|| | |  qqX|j D ]\}}|d u rt|D ]} | | }d |j|< qnF|| }t|D ]4} | | }|| | }t||| ||j|< q"q|j D ]\}}
|
d u rt|D ]} | | }d |j|< qnX|
jr|s|}||
 }n|}||
 }t|D ]&} | | }t|||| |  qАqfqD fddt|D S )NzKCannot replicate network where python modules are childrens of ScriptModulec                 S   s   g | ]}t |d qS )Tr   )r   xr   r   r   r.   V       zreplicate.<locals>.<listcomp>c                 S   s   i | ]\}}||qS r   r   )r   idxparamr   r   r   
<dictcomp>Z   r6   zreplicate.<locals>.<dictcomp>c                 S   s   i | ]\}}||qS r   r   r   r7   bufr   r   r   r9   f   r6   c                 S   s   i | ]\}}||qS r   r   r:   r   r   r   r9   g   r6   )r3   Tc                 S   s   g | ]}g qS r   r   )r   devicer   r   r   r.   m   r6   c                    s   g | ]} | d  qS )r   r   )r   jZmodule_copiesr   r   r.      r6   )r&   RuntimeErrorr*   list
parameters	enumerater4   buffersrequires_gradappendr   r1   _replicate_for_data_parallelr   Z_former_parameters_modulesitemssetattr_parameters_buffers)networkr2   r3   Znum_replicasparamsZparam_indicesZparam_copiesrC   Z
buffers_rgZbuffers_not_rgr;   Zbuffer_indices_rgZbuffer_indices_not_rgZbuffer_copies_rgZbuffer_copies_not_rgr   Zmodule_indicesr+   r   r=   replicakeyr(   Z
module_idxr8   Z	param_idxZbuffer_copiesZ
buffer_idxr   r>   r   	replicateN   s    




 rP   )N)F)F) r   Ztorch._utilsr   collectionsr   r   r   r   r   r&   r4   rP   r   r   r   r   <module>   s   

