
    ΅i                     B   S SK r S SKJr  S SKJrJr  S SKrS SKJr  S SK	J
r
  S SKJr  SS/rS	\R                  \\   -  \\S
4   -  \\\4   -  S\R                  S-  4S jr  SS\\   S\\   S\\\\4      S-  S\\\R*                  -  S-     S-  S\\   4
S jjrg)    N)Sequence)Anycast)ExceptionWrapper)_get_device_index)Module	get_a_varparallel_applyobj.returnc                    [        U [        R                  5      (       a  U $ [        U [        [        45      (       a:  [        [        U 5       H&  n[        U[        R                  5      (       d  M$  Us  $    [        U [        5      (       aH  [        [        U R                  5       5       H&  n[        U[        R                  5      (       d  M$  Us  $    g )N)	
isinstancetorchTensorlisttuplemapr	   dictitems)r   results     Z/home/james-whalen/.local/lib/python3.13/site-packages/torch/nn/parallel/parallel_apply.pyr	   r	      s     #u||$$
#e}%%)S)F&%,,// * #t)SYY[1F&%,,// 2     modulesinputs
kwargs_tupdevicesc                   ^^^^^ [        U 5      [        U5      :w  a#  [        S[        U 5       S[        U5       35      eUb<  [        U 5      [        U5      :w  a#  [        S[        U 5       S[        U5       35      eO*[        [        [        [
        4   0 5      4[        U 5      -  nUb<  [        U 5      [        U5      :w  a#  [        S[        U 5       S[        U5       35      eOS/[        U 5      -  nU Vs/ s H  n[        US5      PM     nnU Vs/ s H"  n[        R                  R                  U5      PM$     nn[        R                  R                  5       (       d  [        S5      e[        R                  R                  5       R                  m[        R                  " 5       m0 m[        R                  " 5       [        R                   " 5       smm  SS["        S	[$        S
[
        S[        [        [
        4   S["        [        R&                  -  S-  S[        R(                  S-  SS4UUUUU4S jjjn[        U 5      S:  a  [+        [-        XX#USS95       VVV	V
VVs/ s H"  u  nu  ppn[        R.                  " XgXXU4S9PM$     nnn
n	nnnU H  nUR1                  5         M     U H  nUR3                  5         M     OU" SU S   US   US   US   US   5        / n[5        [        U5      5       H>  nTU   n[7        U[8        5      (       a  UR;                  5         UR=                  U5        M@     U$ s  snf s  snf s  snnn
n	nnf )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                   > [         R                  " T
5        Uc:  [        U5      nUc  T   [        SU  S3S9TU '   S S S 5        g UR	                  5       n[        U[         R                  5      (       a  UR                  nUc  [         R                  R                  U5      n [         R                  R                  U5         U   [         R                  R                  T	TS9   [        U[        [        45      (       d  U4nU" U0 UD6nS S S 5        S S S 5        S S S 5        T   WTU '   S S S 5        g ! , (       d  f       g = f! , (       d  f       N@= f! , (       d  f       NI= f! , (       d  f       NR= f! , (       d  f       g = f! [         a2    T   [        SU  SU 3S9TU '   S S S 5         g ! , (       d  f        g = ff = f)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   _workerparallel_apply.<locals>._workerR   s    	|,>% Ay!1 +A3 /4 4"GAJ 
 \\^Ffell++\\F>&&55f=F	!!..v6		"";8H"I "%$77"HE1&1 J  7 #
 + T
  JI  76  	-'s+fX>
 	s   D4#F	 E'E$'EEE'
F	 %E8+F	 4
E
EE
E$	 E''
E51F	 8
FF	 F	 	GF2(G2
G	<GG   )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
      sA   $ 7|s6{"$S\N 3$$'K=2
 	
 w<3z?* (W 7,,/
O+<>  + 4S>2.03w<?
w<3w<' (W 7)),W8  ( &3w<'3:;7a D)7G;<CDGqu  //2GGD))++>??##779>>K>>DG!!# #L" -1&*''' ' S#X	'
 ell"T)' t#' 
' 'R 7|a
 ?HGZ'$O?	
 
?::F66 %O?	 	 
 FLLN FKKM  	71:vay*Q-WQZPG3v;f.//NNv	  
 NY <Dj
s   <M)M*)Mr=   )rD   collections.abcr   typingr   r   r   torch._utilsr   torch.cuda._utilsr   torch.nn.modulesr   __all__r   r   r   r   r	   r@   rH   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   