
    9jR                         U d dl 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	  G d de      Z
 G d d	e      Z G d
 de      Z G d de      Zdaeej                   dz     dz  ed<   dej$                  fdZy)    N)chain)_get_device_index)Function)commc                   ,    e Zd Zed        Zed        Zy)	Broadcastc                    t        d |D              st        d      |D cg c]  }t        |d       }}|| _        t	        |      dk(  ryt	        |      | _        |d   j                         | _        |D cg c]  }|j                          c}| _	        t        j                  || j                        }|D ]=  }t        | j                        D ]#  \  }}|s	t        j                  ||         ||<   % ? g }	t        | j                  dd        D ]"  \  }
|
r	|	j!                  fd|D               $  | j"                  |	  t%        t'        j(                  |            S c c}w c c}w )Nc              3   N   K   | ]  }|j                   j                  d k7    ywcpuNdevicetype.0is     \/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/nn/parallel/_functions.py	<genexpr>z$Broadcast.forward.<locals>.<genexpr>        :a188==E):   #%z2Broadcast function not implemented for CPU tensorsTr       c              3   (   K   | ]	  }|     y wNr   )r   outputidxs     r   r   z$Broadcast.forward.<locals>.<genexpr>"   s     *M66#;*Ms   )allAssertionErrorr   target_gpuslen
num_inputs
get_deviceinput_device
is_complexcomplex_maskr   broadcast_coalesced	enumeratetorchview_as_complexneeds_input_gradextendmark_non_differentiabletupler   from_iterable)ctxr   inputsxinpoutputsdevice_outputsr   r$   non_differentiablesinput_requires_gradr   s              @r   forwardzBroadcast.forward   si   :6:: !UVV;FGa(D1GG%v;!V!!9//18>?CNN,?**63??C% 	QN!*3+;+;!< Q:(-(=(=nQ>O(PN1%Q	Q
 !(1#2F2Fqr2J(K 	N$C$&#***MW*MM	N 	$##%89U((122+ H @s   E':E,c                 b    t        j                  | j                  | j                  g| }d|z   S )Nr   )ReduceAddCoalescedapplyr#   r!   )r/   grad_outputsgradss      r   backwardzBroadcast.backward&   s5    "((cnn
/;
     N__name__
__module____qualname__staticmethodr7   r=   r   r>   r   r   r   
   s(    3 34  r>   r   c                   ,    e Zd Zed        Zed        Zy)r9   c                    t        dt        |      |      D cg c]  }||   j                          c}| _        t        |      D cg c]  }||   j	                          }}|| _        t        d |D              }t        dt        |      |      D cg c]
  }||||z     }}t        j                  ||      }t        d t        ||      D              }|S c c}w c c}w c c}w )Nr   c              3   j   K   | ]+  }|j                         rt        j                  |      n| - y wr   )r$   r(   view_as_realr   gs     r   r   z-ReduceAddCoalesced.forward.<locals>.<genexpr>9   s-       
?@Q\\^Eq!: 
s   13c              3   T   K   | ]   \  }}|rt        j                  |      n| " y wr   r(   r)   )r   rr$   s      r   r   z-ReduceAddCoalesced.forward.<locals>.<genexpr>C   s.      
: )3E!!!$9
s   &()
ranger    r"   r   r$   r%   r-   r   reduce_add_coalescedzip)	r/   destinationr!   r<   r   r%   grads_convertedgrads_resultss	            r   r7   zReduceAddCoalesced.forward0   s     ,1CJ
+K
&'E!H!
 8=Z7HI!a++-II'  
DI 
 
 1c/2J?
 AJ/
 
 ++FK@ 
!$Wl!;
 

 -
 J
s   CCC$c                 H    dt        j                  | j                  g| z   S )NNN)r   r:   r   )r/   r;   s     r   r=   zReduceAddCoalesced.backwardJ   s(    
 OOCOO;l;< 	<r>   Nr?   r   r>   r   r9   r9   /   s(     2 < <r>   r9   c                   ,    e Zd Zed        Zed        Zy)Gatherc                 X    t        d |D              st        d      |dk(  rd _        nt        |d      }| _        | _        t        d |D               _        t        d |D              r6|dk(  r1t        d |D              }t        j                  d	d
       d _	        nd _	        t         fd|D               _
        t        |      dkD  xr |d   j                         }t        j                  | j                   j                        }|rt        j                   |      }|S )Nc              3   N   K   | ]  }|j                   j                  d k7    ywr   r   r   s     r   r   z!Gather.forward.<locals>.<genexpr>U   r   r   z/Gather function not implemented for CPU tensorsr   Tc              3   <   K   | ]  }|j                           y wr   )r"   r   s     r   r   z!Gather.forward.<locals>.<genexpr>]   s     >!q||~>s   c              3   B   K   | ]  }|j                         d k(    ywr   N)dimr   ts     r   r   z!Gather.forward.<locals>.<genexpr>^   s     ,quuw!|,s   r   c              3   >   K   | ]  }|j                  d         yw)r   N)viewr^   s     r   r   z!Gather.forward.<locals>.<genexpr>_   s     5166!95s   zvWas asked to gather along dimension 0, but all input tensors were scalars; will instead unsqueeze and return a vector.   )
stacklevelFc              3   T   K   | ]  }|j                  j                         ! y wr   )sizer]   )r   r   r/   s     r   r   z!Gather.forward.<locals>.<genexpr>i   s     @Asww@s   %()r   r   target_devicer   r]   r-   
input_gpuswarningswarnunsqueezed_scalarinput_sizesr    r$   r   gatherr(   r)   )r/   rf   r]   r0   r$   r   s   `     r   r7   zGather.forwardS   s   :6:: !RSSE! %C-mTBM -C>v>>,V,,5f55FMM' 	 %)C!$)C!@@@[1_?)=)=)?
VSWWc.?.?@**62Fr>   c                     t         j                  | j                  | j                  | j                  |      }| j
                  rt        d |D              }d|z   S )Nc              3   &   K   | ]	  }|d      ywr\   r   rH   s     r   r   z"Gather.backward.<locals>.<genexpr>z   s     #BQAaD#Bs   rU   )Scatterr:   rg   rk   r]   rj   r-   )r/   grad_outputscattered_gradss      r   r=   zGather.backwardt   sK    !--NNCOOSWWk
   ##B/#BBOo--r>   Nr?   r   r>   r   rW   rW   R   s)     @ . .r>   rW   c                   ,    e Zd Zed        Zed        Zy)ro   c                    |D cg c]  }t        |d       }}|| _        |j                  j                  dk7  r|j	                         nd| _        d }t        j                  j                         r:| j
                  dk(  r+|D cg c]   }t        t        j                  |            " }}|j                         }t        j                  |||| j                  |      }	|rt        d |	D              }	|t        |	      D ]s  \  }
}t        j                  j                  ||
         5  t        j                  j!                         }|j#                  ||
          |j%                  |       d d d        u |	S c c}w c c}w # 1 sw Y   xY w)NTr   c              3   F   K   | ]  }t        j                  |        y wr   rK   )r   os     r   r   z"Scatter.forward.<locals>.<genexpr>   s     FE11!4Fs   !)r   r]   r   r   r"   r#   r(   acceleratoris_available_get_streamr$   r   scatterr-   r'   device_indexcurrent_streamwait_streamrecord_stream)r/   r   chunk_sizesr]   inputr1   streamsr   r$   r3   r   r   main_streams                r   r7   zScatter.forward   sb   ;FGa(D1GG161B1Be1K5++-QS))+0@0@B0FGRSV{5<<#78SGS%%'
,,uk;QFgFFG &w/ 6	6&&33KNC 6"'"3"3"B"B"DK++GAJ7((56 66
 - H T6 6s   E7%E<'AFF
	c                 `    d d d t        j                  | j                  | j                  g| fS r   )rW   r:   r#   r]   )r/   rp   s     r   r=   zScatter.backward   s+    T4c.>.>!V+!VVVr>   Nr?   r   r>   r   ro   ro   ~   s*     2 W Wr>   ro   _streamsr   c                 F   | j                   dk(  st        j                  j                         syt        j                  j	                         j                   | j                   k7  rAt        dt        j                  j	                         j                    d| j                          t        "dgt        j                  j                         z  at        | j                     0t        j                  | j                        t        | j                  <   t        | j                     S )zBGet a background stream for copying between CPU and target device.r   Nz"Expected current accelerator type z to match device type )
r   r(   rw   rx   current_acceleratorr   r   device_countindexStream)r   s    r   ry   ry      s     {{e5#4#4#A#A#C,,.33v{{B01B1B1V1V1X1]1]0^ _$$*KK=2
 	
 6E--::<<%!&fll!;FLL!!r>   )rh   	itertoolsr   r(   torch._utilsr   torch.autogradr   torch.nn.parallelr   r   r9   rW   ro   r   listr   __annotations__r   ry   r   r>   r   <module>r      s|       * # "" "J <  <F).X ).XWh WB .2$u||d"
#d
* 1" "r>   