
    /hy                         S SK r S SKJr  S SKJr  S SKJr  S SKJrJ	r	J
r
JrJrJrJr  S rS rS rS	 rS
 rS rS rS rS rS rS rS r\S:X  a  \" 5         gg)    N)PartialState)assert_exception)DistributedType)DistributedOperationException	broadcastcopy_tensor_to_devicesgathergather_objectpad_across_processesreducec                     [         R                  " U R                  5      S-   U R                  U R                  -  -   R	                  U R
                  5      $ )Ng      ?)torcharangenum_processesprocess_indextodevicestates    `/home/james-whalen/.local/lib/python3.13/site-packages/accelerate/test_utils/scripts/test_ops.pycreate_tensorr   !   sD    LL,,-3u7J7JUM`M`7`aeefkfrfrss    c                     [        U 5      n[        U5      nUR                  5       [        [	        SU R
                  S-  S-   5      5      :X  d   eg )N      )r   r	   tolistlistranger   )r   tensorgathered_tensors      r   test_gatherr!   %   sH    5!FVnO!!#tE!U5H5H!5Ka5O,P'QQQQr   c                 |   U R                   [        R                  :X  a  g U R                  /n[	        U5      n[        U5      U R                  :X  d"   U S[        U5       SU R                   35       eU[        [        U R                  5      5      :X  d(   U S[        [        U R                  5      5       35       eg )Nz,  != )	distributed_typer   XLAr   r
   lenr   r   r   )r   objgathered_objs      r   test_gather_objectr)   +   s    !4!44
C %L| 3 33t~RLHYGZZ^_d_r_r^s5tt34e&9&9 :;;t~TRVW\]b]p]pWqRrQs=tt;r   c                     U R                   [        R                  :X  a  g [        R                  " SU R
                  S9R                  SS5      R                  5       nUR                  5       (       a   e[        U5      ng )N   )r         )
r$   r   r%   r   r   r   viewtis_contiguousr	   )r   r   _s      r   test_gather_non_contigousr2   5   sc    !4!44 \\"U\\2771=??AF##%%%%vAr   c                     [        U 5      n[        U5      nUR                  [        R                  " U R
                  /5      :X  d   eUR                  5       [        [        SU R
                  S-   5      5      :X  d   eg )Nr   )	r   r   shaper   Sizer   r   r   r   )r   r   broadcasted_tensors      r   test_broadcastr7   A   sk    5!F"6*##uzz53F3F2G'HHHH$$&$uQ8K8Ka8O/P*QQQQr   c                 "   U R                   (       a=  [        R                  " U R                  S-   5      R	                  U R
                  5      nO9[        R                  " U R                  5      R	                  U R
                  5      n[        U5      nUR                  [        R                  " U R                  S-   /5      :X  d   eU R                   (       d8  UR                  5       [        [        SU R                  5      5      S/-   :X  d   eg g )Nr   r   )is_main_processr   r   r   r   r   r   r4   r5   r   r   r   )r   r   padded_tensors      r   test_pad_across_processesr;   H   s     e11A5699%,,Ge11255ellC(0M%**e.A.AA.E-F"GGGG  ##%eAu7J7J.K)LPQs)RRRR !r   c                    U R                   S:w  a  g [        U 5      n[        US5      n[        R                  " SS/5      R                  U R                  5      n[        R                  " X#5      (       d   U SU 35       eg )Nr   sumg      @   r#   r   r   r   r   r   r   r   allcloser   r   reduced_tensortruth_tensors       r   test_reduce_sumrD   U   sq    a5!FFE*N<<a),,U\\:L>>.77^N;K4P\~9^^7r   c                    U R                   S:w  a  g [        U 5      n[        US5      n[        R                  " SS/5      R                  U R                  5      n[        R                  " X#5      (       d   U SU 35       eg )Nr   meang       @r-   r#   r?   rA   s       r   test_reduce_meanrG   _   sq    a5!FFF+N<<a),,U\\:L>>.77^N;K4P\~9^^7r   c                    U R                   [        R                  [        R                  4;   a  g SU l        U R
                  S:X  a5  S[        R                  " / SQ/5      R                  U R                  5      0nO5S[        R                  " / SQ//5      R                  U R                  5      0n[        [        5         [        USS9  S S S 5        U R
                  S:X  a5  S[        R                  " / SQ/5      R                  U R                  5      0nO8S[        R                  " / SQ/ SQ//5      R                  U R                  5      0n[        [        5         [        U5        S S S 5        U R
                  S:X  a5  S[        R                  " / SQ/5      R                  U R                  5      0nO8S[        R                  " / SQ/ SQ//5      R                  U R                  5      0n[        [        5         [        U5        S S S 5        SU l        g ! , (       d  f       GNW= f! , (       d  f       N= f! , (       d  f       N9= f)	NTr   r   )        r   r   r-   r,   )rI   r   r   r-   r,      )dim)rJ   r>         	   F)r$   r   NOr%   debugr   r   r   r   r   r   r   r   r   r   )r   datas     r   test_op_checkerrR   i   s   /"4"4o6I6I!JJEKa%,,(9':;>>u||LM%,,)=(>'?@CCELLQR	7	8Tq) 
9 a%,,(9':;>>u||LM%,,):O(L'MNQQRWR^R^_`	7	8t 
9 a%,,(9':;>>u||LM%,,):O(L'MNQQRWR^R^_`	7	8$ 
9 EK+ 
9	8 
9	8 
9	8s$    HH-?H>
H*-
H;>
Ic           	         U R                   [        R                  [        R                  4;  a  g U R                  (       a?  [
        R                  " / SQ[
        R                  S9R                  U R                  5      nOS n[        U5      n[
        R                  " U[
        R                  " / SQ[
        R                  U R                  S95      (       d   eg )N)r   r   r-   )dtype)rT   r   )r$   r   	MULTI_GPUr%   r9   r   r   intr   r   r   r@   )r   r   s     r   test_copy_tensor_to_devicesrW      s    o&?&?ATAT%UUiuyy9<<U\\J#F+F>>&%,,y		RWR^R^"_````r   c                     [        5         g )N)main)indexs    r   _mp_fnr[      s    Fr   c                  X   [        5       n U R                  SU  35        U R                  S5        [        U 5        U R                  S5        [        U 5        U R                  S5        [	        U 5        U R                  S5        [        U 5        U R                  S5        [        U 5        U R                  S5        [        U 5        U R                  S5        [        U 5        U R                  S	5        [        U 5        U R                  S
5        [        U 5        U R                  5         g )NzState: ztesting gatherztesting gather_objectztesting gather non-contigousztesting broadcastztesting pad_across_processesztesting reduce_sumztesting reduce_meanztesting op_checkerz&testing sending tensors across devices)r   printr!   r)   r2   r7   r;   rD   rG   rR   rW   destroy_process_groupr   s    r   rY   rY      s    NE	KK'%!"	KK !	KK'(u	KK./e$	KK#$5	KK./e$	KK$%E	KK%&U	KK$%E	KK89&	!r   __main__)r   
accelerater   accelerate.test_utils.testingr   accelerate.utils.dataclassesr   accelerate.utils.operationsr   r   r   r	   r
   r   r   r   r!   r)   r2   r7   r;   rD   rG   rR   rW   r[   rY   __name__ r   r   <module>rf      s{   "  # : 8  tRu	R
S__ Fa
"0 zF r   