
    9j                     <   d dl Z d dlmZ d dlmZmZ d dlZd dlmZ d dl	m
Z
 d dlmZ ddgZd	ej                  ee   z  eed
f   z  eeef   z  dej                  dz  fdZ	 	 ddee   dee   deeeef      dz  deeej*                  z  dz     dz  dee   f
dZy)    N)Sequence)Anycast)ExceptionWrapper)_get_device_index)Module	get_a_varparallel_applyobj.returnc                 p   t        | t        j                        r| S t        | t        t        f      r3t        t        |       D ]   }t        |t        j                        s|c S  t        | t              rAt        t        | j                               D ]   }t        |t        j                        s|c S  y )N)	
isinstancetorchTensorlisttuplemapr	   dictitems)r   results     `/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/nn/parallel/parallel_apply.pyr	   r	      s     #u||$
#e}%)S) 	F&%,,/	 #t)SYY[1 	F&%,,/	     modulesinputs
kwargs_tupdevicesc                 x   t        |       t        |      k7  r#t        dt        |        dt        |             |:t        |       t        |      k7  rMt        dt        |        dt        |             t        t        t        t
        f   i       ft        |       z  }|:t        |       t        |      k7  r2t        dt        |        dt        |             dgt        |       z  }|D cg c]  }t        |d       }}|D cg c]!  }t        j                  j                  |      # }}t        j                  j                         st        d      t        j                  j                         j                  t        j                         i t        j                         t        j                          c	 	 ddt"        d	t$        d
t
        dt        t        t
        f   dt"        t        j&                  z  dz  dt        j(                  dz  ddffd}t        |       dkD  rt+        t-        | ||||d            D 	
cg c](  \  }\  }}	}
}}t        j.                  ||||	|
||f      * }}}
}	}}}|D ]  }|j1                           |D ]  }|j3                           n |d| d   |d   |d   |d   |d          g }t5        t        |            D ]8  }|   }t7        |t8              r|j;                          |j=                  |       : |S c c}w c c}w c c}}}
}	}}w )a  Apply each `module` in :attr:`modules` in parallel on each of :attr:`devices`.

    Args:
        modules (Module): modules to be parallelized
        inputs (tensor): inputs to the modules
        devices (list of int or torch.device): CUDA devices

    :attr:`modules`, :attr:`inputs`, :attr:`kwargs_tup` (if given), and
    :attr:`devices` (if given) should all have same length. Moreover, each
    element of :attr:`inputs` can either be a single object as the only argument
    to a module, or a collection of positional arguments.
    zThe number of modules z& is not equal to the number of inputs Nz* is not equal to the number of kwargs_tup z' is not equal to the number of devices TzNo available accelerator found.imoduleinputkwargsdevicestreamr   c                 H   t        j                  
       |<t        |      }|5  t        d|  d      | <   d d d        y |j	                         }t        |t         j                        r|j                  }|t         j                  j                  |      }	 t         j                  j                  |      5  |5  t         j                  j                  	      5  t        |t        t        f      s|f} ||i |}d d d        d d d        d d d        5  | <   d d d        y # 1 sw Y   y xY w# 1 sw Y   6xY w# 1 sw Y   :xY w# 1 sw Y   >xY w# 1 sw Y   y xY w# t        $ r0 5  t        d|  d|       | <   d d d        Y y # 1 sw Y   Y y xY ww xY w)Nzin replica zQ, no device was provided and no tensor input was found; device cannot be resolved)where)enabledz on device )r   set_grad_enabledr	   r   
get_devicer   r"   indexacceleratorcurrent_streamdevice_indexampautocastr   r   	Exception)r   r   r    r!   r"   r#   toutputautocast_enableddevice_typegrad_enabledlockresultss           r   _workerzparallel_apply.<locals>._workerR   s    	|,>% Ay !1 +A3 /4 4"GAJ
 \\^Ffell+\\F>&&55f=F	!!..v622 		"";8H"I2 "%$7"HE1&12 2 2  $#
$ $+
 2 2 2 2 2 2$ $ 	 -'s+fX>
  	s   D,E( <E?"E!"D8EE
E( E#E( ,D58E=EE		EEE( E%!E( %E( (F!3F	F!F	F!F!   )strict)targetargsr   NN)lenAssertionErrorr   r   strr   r   r   r*   r+   is_availablecurrent_acceleratortype	threadingLockis_grad_enabledis_autocast_enabledintr   r"   Stream	enumeratezipThreadstartjoinranger   r   reraiseappend)r   r   r   r   xstreamsr7   r   r   r    r!   r"   r#   threadsthreadoutputsr1   r2   r3   r4   r5   r6   s                    @@@@@r   r
   r
      sF   $ 7|s6{"$S\N 3$$'K=2
 	
 w<3z?* (W 7,,/
O+<> 
 4S>2.03w<?
w<3w<' (W 7)),W8 
 &3w<'3:;a D);G;<CDqu  //2DGD))+>??##779>>K>>DG!!# #L" -1&*''' ' S#X	'
 ell"T)' t#' 
' 'R 7|a
 ?HGVZ'$O?	
 
 ;:FE666 a%O
 
  	FLLN	 	FKKM	 	71:vay*Q-WQZPG3v; f./NNv	
 NY <Dj
s   5L(&L--L2r<   )rC   collections.abcr   typingr   r   r   torch._utilsr   torch.cuda._utilsr   torch.nn.modulesr   __all__r   r   r   r   r	   r?   rG   r"   r
    r   r   <module>r]      s     $   ) / # (
)	S		!E#s(O	3d38n	D
\\D( 37:>	sfsSMs c3h(4/s cELL(4/047	s
 
#Ysr   