+
     °³j��  ã                   óú  € R t ^ RIt^ RIt^ RIHt ^ RIHtHt ^ RIH	t	H
t
 ^ RIHt ^ RIt^RIHtHt ^RIHt ^RIHtHt ^R	IHtHtHt ^R
IHt ]! 4       '       d
   ^ RIHu Ht ]! 4       '       d   ^ RI H!t! R t"R t#R t$R t%R t&R]"RR/R lt'RAR lt(R t)R t*R t+R t,R t-R t.R t/R t0 ! R R]14      t2R  t3R! t4]3R" 4       t5R# R$ lt6R% R& lt7R' R( lt8RBR) lt9RCR* lt:]Pv                  ^]Px                  ^]Pz                  ^]P|                  ^]P~                  ^]P€                  ^]P‚                  ^]P„                  ^]P†                  ^	]Pˆ                  ^
/
tE]EP�                  4        U Uu/ uF  w  rWbK	  	  upp tGR+ tHRDR, R- lltI]3RBR. R/ ll4       tJRBR0 R1 lltKRBR2 R3 lltLRER4 ltMRBR5 ltN ! R6 R7]O4      tP]4RFR8 l4       tQRBR9 ltR]3RGR: l4       tSR; tT ! R< R=4      tUR> tVR? tW]RHR@ l4       tXR# u upp i )IzB
A set of basic tensor ops compatible with tpu, gpu, and multigpu
N)ÚMapping)ÚcontextmanagerÚnullcontext)Úupdate_wrapperÚwraps©ÚAny)ÚAcceleratorStateÚPartialState)Ú!TORCH_DISTRIBUTED_OPERATION_TYPES)ÚDistributedTypeÚTensorInformation)Úis_npu_availableÚis_torch_distributed_availableÚis_torch_xla_available)Úis_torch_version)ÚReduceOpc                 ó6   € \        V \        P                  4      # ©N)Ú
isinstanceÚtorchÚTensor©Útensors   &Ú\/home/luca/screen-checking/.venv/lib/python3.14/site-packages/accelerate/utils/operations.pyÚis_torch_tensorr   -   s   € Ü�fœeŸl™lÓ+Ð+ó    c           
      óv  € \        V \        P                  P                  \        P                  P                  \        P                  P
                  \        P                  P                  \        P                  P                  \        P                  P                  \        P                  P                  4      # r   )
r   r   ÚxpuÚFloatTensorÚ
ByteTensorÚ	IntTensorÚ
LongTensorÚ
HalfTensorÚDoubleTensorÚBFloat16Tensorr   s   &r   Úis_torch_xpu_tensorr&   1   sm   € ÜØÜ�	‰	×ÑÜ�	‰	×ÑÜ�	‰	×ÑÜ�	‰	×ÑÜ�	‰	×ÑÜ�	‰	×ÑÜ�	‰	× Ñ ó	ð 	r   c                 ó"   € \        V \        4      # r   )r   r   ©Útensor_infos   &r   Úis_tensor_informationr*   >   s   € Ü�kÔ#4Ó5Ð5r   c                ór   € \        V \        4      ;'       d!    \        V R4      ;'       d    \        V R4      # )z†
Checks if `data` is a `namedtuple` or not. Can have false positives, but only if a user is trying to mimic a
`namedtuple` perfectly.
Ú_asdictÚ_fields)r   ÚtupleÚhasattr©Údatas   &r   Úis_namedtupler2   B   s0   € ô
 �dœEÓ"×\Ð\¤w¨t°YÓ'?×\Ð\ÄGÈDÐR[ÓD\Ð\r   c                óv   € \        V 4      '       d   \        V 4      ! \        V4      !  # \        V 4      ! V4      # )zG
Cast a generator to the same type as obj (list, tuple, or namedtuple)
)r2   ÚtypeÚlist)ÚobjÚ	generators   &&r   Ú
honor_typer8   J   s1   € ô
 �S×ÒÜ�CŒyœ$˜y›/Ñ*Ð*ä�CŒy˜Ó#Ð#r   Ú	test_typeÚerror_on_other_typeFc               óè  a aaaa€ \        V\        \        34      '       d   \        VVVV VV3R lV 4       4      # \        V\        4      '       dG   \        V4      ! VP                  4        UUu/ uF  w  rgV\        S V.SO5RSRS/SB bK  	  upp4      # S! V4      '       d   S ! V.SO5/ SB # S'       d3   \        R\        V4       RS P                   RSP                   R24      hV# u uppi )aX  
Recursively apply a function on a data structure that is a nested list/tuple/dictionary of a given base type.

Args:
    func (`callable`):
        The function to recursively apply.
    data (nested list/tuple/dictionary of objects accepted by `test_type`):
        The data on which to apply `func`
    *args:
        Positional arguments that will be passed to `func` when applied on the unpacked data.
    test_type (`Callable[[Any], bool]`, *optional*, defaults to `is_torch_tensor`):
        The predicate deciding whether `func` should be applied to a given leaf object.
    error_on_other_type (`bool`, *optional*, defaults to `False`):
        Whether to return an error or not if after unpacking `data`, we get on an object for which `test_type`
        returns `False`. If `False`, the function will leave such objects unchanged.
    **kwargs (additional keyword arguments, *optional*):
        Keyword arguments that will be passed to `func` when applied on the unpacked data.

Returns:
    The same data structure as `data` with `func` applied to every object for which `test_type` returns `True`.
c              3   óL   <"  € T F  p\        SV.SO5R SRS/SB x € K  	  R# 5i)r9   r:   N©Úrecursively_apply)Ú.0ÚoÚargsr:   ÚfuncÚkwargsr9   s   & €€€€€r   Ú	<genexpr>Ú$recursively_apply.<locals>.<genexpr>n   sF   øé € ð ñ �Aô "Ø˜!ðØ"òØ.7ðØM`ðØdjöó ùó   ƒ!$r9   r:   zUnsupported types (z) passed to `z?`. Only nested list/tuple/dicts of objects that are valid for `z` should be passed.)
r   r.   r5   r8   r   r4   Úitemsr>   Ú	TypeErrorÚ__name__)rB   r1   r9   r:   rA   rC   ÚkÚvs   f&ddjl  r   r>   r>   U   s!  ü€ ô, �$œ¤˜×&Ò&ÜØ÷ð ñ ó	ó
ð 	
ô 
�Dœ'×	"Ò	"Ü�DŒzð
 !ŸJ™JœLô	ñ )‘D�Að Ô$Ø˜!ðØ"òØ.7ðØM`ðØdjñò ñ )ò	ó
ð 	
ñ 
�4�ŠÙ�DÐ*˜4Ò* 6Ñ*Ð*ß	ÜØ!¤$ t£* ¨]¸4¿=¹=¸/ð J+Ø+4×+=Ñ+=Ð*>Ð>QðSó
ð 	
ð €Kùós   Á0 C.
c                ó.  aaa€ \        V 4      '       g   \        V R4      '       d   SR8X  d   Ro V P                  SSR7      # \        V \        \        34      '       d   \        V VVV3R lV  4       4      # \        V \        4      '       dj   \        S\        4      '       d   S.oMSf   . o\        V 4      ! V P                  4        UUu/ uF  w  rVYUS9   d   TM\        VSSSR7      bK  	  upp4      # V #   \         d    T P                  S4      u # \         d8   p\        4       '       d!   \        S\        4      '       d   RS 2o Rp?M
ThRp?ii ; i T P                  SSR7      #   \         d    T P                  S4      u # i ; iu uppi )	aO  
Recursively sends the elements in a nested list/tuple/dictionary of tensors to a given device.

Args:
    tensor (nested list/tuple/dictionary of `torch.Tensor`):
        The data to send to a given device.
    device (`torch.device`):
        The device to send the data to.
    non_blocking (`bool`, *optional*, defaults to `False`):
        If `True`, the transfer to the device is performed asynchronously, which can overlap
        data movement with computation. Only effective when the device supports it (e.g. CUDA).
    skip_keys (`str` or `List[str]`, *optional*):
        A key or list of keys in a dictionary `tensor` whose values should not be sent to
        the given `device`. Entries with these keys are left on their original device.

Returns:
    The same data structure as `tensor` with all tensors sent to the proper device.
ÚtoÚnpuznpu:0)Únon_blockingznpu:Nc              3   óB   <"  € T F  p\        VSSSR 7      x € K  	  R# 5i)©rO   Ú	skip_keysN)Úsend_to_device)r?   ÚtÚdevicerO   rR   s   & €€€r   rD   Ú!send_to_device.<locals>.<genexpr>±   s#   øé € ÐoÑhnÐcd”^ A v¸LÐT]×^Ñ^Óhnùs   ƒrQ   )r   r/   rM   rH   ÚAssertionErrorr   r   Úintr.   r5   r8   r   Ústrr4   rG   rS   )r   rU   rO   rR   ÚerrorrJ   rT   s   &fff   r   rS   rS   ˆ   s{  ú€ ô& �v×Ò¤'¨&°$×"7Ò"7à�UŒ?ØˆFð	Ø—9‘9˜V°,�9Ó?Ð?ô 
�FœU¤D˜M×	*Ò	*ÜØÖoÑhnÓoó
ð 	
ô 
�FœG×	$Ò	$Ü�i¤×%Ò%Ø"˜‰IØÒØˆIÜ�FŒ|ð #ŸL™LœNôá*‘D�Að ˜Yœ‘1¬N¸1¸fÐS_ÐktÔ,uÒuÙ*òó
ð 	
ð ˆøô= ô 	%Ø—9‘9˜VÓ$Ò$Üô 	ô  ×!Ò!Ü˜f¤c×*Ò*Ø# F 8˜_�Füà�ûð	úð	%Ø—9‘9˜V°,�9Ó?Ð?øÜô 	%Ø—9‘9˜VÓ$Ò$ð	%üósA   ±C9 Ã#F
Ã9EÄEÄ EÄ!+EÅEÅEÅE/ Å/FÆFc                ó   € R p\        W4      # )a/  
Recursively gathers the information needed to rebuild a nested list/tuple/dictionary of tensors.

Args:
    data (nested list/tuple/dictionary of `torch.Tensor`):
        The data to send to analyze.

Returns:
    The same data structure as `data` with [`~utils.TensorInformation`] instead of tensors.
c                 óD   € \        V P                  V P                  R 7      # ))ÚshapeÚdtype)r   r]   r^   r   s   &r   Ú_get_data_structureÚ/get_data_structure.<locals>._get_data_structureÎ   s   € Ü  v§|¡|¸6¿<¹<ÔHÐHr   r=   )r1   r_   s   & r   Úget_data_structurera   Â   s   € òIô Ð0Ó7Ð7r   c                ó   € R p\        W4      # )a  
Recursively gathers the shape of a nested list/tuple/dictionary of tensors as a list.

Args:
    data (nested list/tuple/dictionary of `torch.Tensor`):
        The data to send to analyze.

Returns:
    The same data structure as `data` with lists of tensor shapes instead of tensors.
c                 ó,   € \        V P                  4      # r   )r5   r]   r   s   &r   Ú
_get_shapeÚget_shape.<locals>._get_shapeà   s   € Ü�F—L‘LÓ!Ð!r   r=   )r1   rd   s   & r   Ú	get_shaperf   Ô   s   € ò"ô ˜ZÓ.Ð.r   c                ó*   € R p\        W\        R7      # )zÌ
Recursively initializes tensors from a nested list/tuple/dictionary of [`~utils.TensorInformation`].

Returns:
    The same data structure as `data` with tensors instead of [`~utils.TensorInformation`].
c                 óV   € \         P                  ! V P                  R V P                  / # ©r^   )r   Úemptyr]   r^   r(   s   &r   Ú_initialize_tensorÚ.initialize_tensors.<locals>._initialize_tensorî   s"   € Ü�{Š{˜K×-Ñ-ÐG°[×5FÑ5FÑGÐGr   ©r9   )r>   r*   )Údata_structurerk   s   & r   Úinitialize_tensorsro   æ   s   € òHô Ð/ÔK`ÔaÐar   c                ó  € \        V \        \        \        34      '       d)   \	        V 4      ^ 8X  d   \        R\        V 4       R24      h\        V \        \        34      '       d   \        V ^ ,          4      # \        V \        4      '       d*   V P                  4        F  p\        W,          4      u # 	  M8\        V \        P                  4      '       g   \        R\        V 4       R24      hV P                  ^ ,          # )zì
Recursively finds the batch size in a nested list/tuple/dictionary of lists of tensors.

Args:
    data (nested list/tuple/dictionary of `torch.Tensor`): The data from which to find the batch size.

Returns:
    `int`: The batch size.
z&Cannot find the batch size from empty Ú.z0Can only find the batch size of tensors but got )r   r.   r5   r   ÚlenÚ
ValueErrorr4   Úfind_batch_sizeÚkeysr   r   rH   r]   )r1   rJ   s   & r   rt   rt   ô   s¿   € ô �$œ¤¤gÐ.×/Ò/´S¸³YÀ!´^ÜÐAÄ$ÀtÃ*ÀÈQÐOÓPÐPä�$œ¤˜×&Ò&Ü˜t A�wÓ'Ð'Ü	�Dœ'×	"Ò	"Ø—‘–ˆAÜ" 4¥7Ó+Ò+ò ä˜œeŸl™l×+Ò+ÜÐJÌ4ÐPTË:È,ÐVWÐXÓYÐYØ�:‰:�a�=Ðr   c                óJ   €  \        V 4      #   \        \        3 d     R# i ; i)a   
Same as [`utils.operations.find_batch_size`] except will ignore if `ValueError` and `TypeErrors` are raised

Args:
    data (nested list/tuple/dictionary of `torch.Tensor`): The data from which to find the batch size.

Returns:
    `int`: The batch size.
N)rt   rs   rH   r0   s   &r   Úignorant_find_batch_sizerw     s,   € ðÜ˜tÓ$Ð$øÜœ	Ð"ô ØÙðús   ‚
 �"¡"c                ó   € R p\        W4      # )a;  
Recursively finds tensors in a nested list/tuple/dictionary and converts them to a list of numbers.

Args:
    data (nested list/tuple/dictionary of `torch.Tensor`): The data from which to convert to regular numbers.

Returns:
    The same data structure as `data` with lists of numbers instead of `torch.Tensor`.
c                 óÚ   € V P                  4       P                  4       p V P                  \        P                  8X  d    V P                  \        P                  4      p V P                  4       # r   )ÚdetachÚcpur^   r   Úbfloat16rM   Úfloat32Útolistr   s   &r   Ú_convert_to_listÚ!listify.<locals>._convert_to_list'  sF   € Ø—‘“×$Ñ$Ó&ˆØ�<‰<œ5Ÿ>™>Ô)ð —Y‘YœuŸ}™}Ó-ˆFØ�}‰}‹Ðr   r=   )r1   r   s   & r   Úlistifyr�     s   € òô Ð-Ó4Ð4r   c                 óP   € R  p\        WRR7      p\        P                  ! 4        V# )c                 óÊ   € V P                   ^ 8X  d   V P                  4       R,          p V P                  4       '       g   V P                  4       p \        P
                  ! V 4      # ©é    N)ÚndimÚcloneÚis_contiguousÚ
contiguousÚxmÚ
all_gatherr   s   &r   Ú_tpu_gather_oneÚ$_tpu_gather.<locals>._tpu_gather_one4  sL   € Ø�;‰;˜!ÔØ—\‘\“^ DÕ)ˆFð ×#Ñ#×%Ò%Ø×&Ñ&Ó(ˆFÜ�}Š}˜VÓ$Ð$r   T©r:   )r>   rŠ   Ú	mark_step)r   rŒ   Úress   &  r   Ú_tpu_gatherr‘   3  s#   € ò%ô ˜OÈÔ
N€CÜ‡L‚L„NØ€Jr   c                 ó  aa€ \        4       o\        P                  P                  oSP                  P
                  R 8X  d1   \        RR4      '       d   \        P                  P                  4        VV3R lp\        WRR7      # )r   z<=z2.8c                 óÀ  <€ V P                   ^ 8X  d   V P                  4       R,          p V P                  4       '       g   V P                  4       p SP                  e�   SP                  R8w  d~   \
        P                  ! SP                  V P                  4       ,          V P                  SP                  R7      pS! W4       VP                  ! R.V P                  4       R,          O5!  # \        SP                  4       Uu. uF  p\
        P                  ! V 4      NK  	  pp\
        P                  P!                  W4       \
        P"                  ! V^ R7      # u upi )r…   NÚgloo©r^   rU   :é   NN©Údiméÿÿÿÿ)r†   r‡   rˆ   r‰   Úbackendr   rj   Únum_processesÚnumelr^   rU   ÚviewÚsizeÚrangeÚ
empty_likeÚdistributedr‹   Úcat)r   Úoutput_tensorsÚ_Ú	gather_opÚstates   &  €€r   Ú_gpu_gather_oneÚ$_gpu_gather.<locals>._gpu_gather_oneJ  s  ø€ Ø�;‰;˜!ÔØ—\‘\“^ DÕ)ˆFð ×#Ñ#×%Ò%Ø×&Ñ&Ó(ˆFà�=‰=Ò$¨¯©¸&Ô)@ô
 #Ÿ[š[Ø×#Ñ# f§l¡l£nÕ4Ø—l‘lØ—|‘|ôˆNñ
 �nÔ-Ø!×&Ò& rÐ>¨F¯K©K«M¸"Õ,=Ó>Ð>ô
 AFÀe×FYÑFYÔ@ZÓ[Ñ@Z¸1œe×.Ò.¨vÖ6Ñ@ZˆNÐ[Ü×Ñ×(Ñ(¨Ô@Ü—9’9˜^°Ô3Ð3ùò \s   ÄETrŽ   )
r
   r   r¡   Úall_gather_into_tensorrU   r4   r   r   Úsynchronizer>   )r   r§   r¥   r¦   s   & @@r   Ú_gpu_gatherr«   B  s`   ù€ Ü‹N€EÜ×!Ñ!×8Ñ8€Ið ‡|�|×Ñ˜EÔ!Ô&6°t¸U×&CÒ&CÜ�	‰	×ÑÔö4ô8 ˜_È$ÔOÐOr   c                   ó   € ] tR tRtRtRtR# )ÚDistributedOperationExceptionii  z}
An exception class for distributed operations. Raised if the operation cannot be performed due to the shape of the
tensors.
© N)rI   Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__static_attributes__r®   r   r   r­   r­   i  s   † ñó
 	r   r­   c                ó0   a € \        S 4      V 3R l4       pV# )zn
Verifies that `tensor` is the same shape across all processes. Only ran if `PartialState().debug` is `True`.
c                  ó|  <€ \        4       P                  \        P                  8X  g   \        4       P                  '       g	   S
! V / VB # S
P
                   R S
P                   2pRV9   d   VR,          pM	V ^ ,          p\        4       P                  P                  \        V4      P                  8w  dh   \        RV RVP                  P                   R\        4       P                  P                   R\        4       P                  P                   RV R 24      h\        V4      p\        V.4      pV^ ,          er   VP                  V^ ,          4      \        V4      8H  pV'       gF   RP                  \!        V4       UUu. uF  w  rxRV R	V 2NK  	  upp4      p	\        R
V RV	 24      hS
! V / VB # u uppi )rq   r   z%One or more of the tensors passed to z were not on the z+ while the `Accelerator` is configured for z. Please move it to the z before calling z
  - zProcess z: znCannot apply desired operation due to shape mismatches. All shapes across devices must be valid.

Operation: `z`
Input shapes:
  - )r
   Údistributed_typer   ÚNOÚdebugr¯   rI   rU   r4   Úfind_devicer­   rf   Úgather_objectÚcountrr   ÚjoinÚ	enumerate)rA   rC   Ú	operationr   ÚshapesÚoutputÚare_sameÚir]   Úprocess_shape_strÚfunctions   *,        €r   ÚwrapperÚ!verify_operation.<locals>.wrapperw  sÑ  ø€ ä‹>×*Ñ*¬o×.@Ñ.@Ô@ÌË×H\×H\ÐH\Ù˜TÐ, VÑ,Ð,Ø×*Ñ*Ð+¨1¨X×->Ñ->Ð,?Ð@ˆ	Ø�vÔØ˜HÕ%‰Fà˜!•WˆFÜ‹>× Ñ ×%Ñ%¬°VÓ)<×)AÑ)AÔAÜ/Ø7¸	°{ÐBSÐTZ×TaÑTa×TfÑTfÐSgð  hSô  T`ó  Tb÷  Tiñ  Ti÷  Tnñ  Tnð  Soð o)Ü)5«×)>Ñ)>×)CÑ)CÐ(DÐDTÐU^ÐT_Ð_`ðbóð ô ˜6Ó"ˆÜ ˜xÓ(ˆØ�!�9Ò Ø—|‘| F¨1¥IÓ.´#°f³+Ñ=ˆHßØ$,§M¡MÔ[dÐekÔ[lÔ2mÑ[lÉxÈq°X¸a¸SÀÀ5À'Ó3JÑ[lÒ2mÓ$nÐ!Ü3ð'à'0 kÐ1GÐHYÐGZð\óð ñ
 ˜Ð( Ñ(Ð(ùó 3ns   ÆF8
©r   ©rÄ   rÅ   s   f r   Úverify_operationrÉ   r  s"   ø€ ô
 ˆ8ƒ_ô)ó ð)ð4 €Nr   c                ó0   a € \        S 4      V 3R l4       pV# )z…
Checks that `verify_operation` failed and if so reports a more helpful error chaining the existing
`DistributedOperationException`.
c                  ó’   <€  S! V / VB #   \          d1   pSP                   R SP                   2p\        RT R24      ThRp?ii ; i)rq   zError found while calling `z1`. Please see the earlier error for more details.N)r­   r¯   rI   )rA   rC   Úer¾   rÄ   s   *,  €r   rÅ   Ú"chained_operation.<locals>.wrapper›  sc   ø€ ð	Ù˜TÐ, VÑ,Ð,øÜ,ô 	Ø#×.Ñ.Ð/¨q°×1BÑ1BÐ0CÐDˆIÜ/Ø-¨i¨[Ð8iÐjóàðûð	ús   ƒ ‹A–+AÁArÇ   rÈ   s   f r   Úchained_operationrÎ   •  s"   ø€ ô ˆ8ƒ_ôó ðð €Nr   c                óº   € \        4       P                  \        P                  8X  d   \	        V 4      # \        4       P                  \
        9   d   \        V 4      # V # )a  
Recursively gather tensor in a nested list/tuple/dictionary of tensors from all devices.

Args:
    tensor (nested list/tuple/dictionary of `torch.Tensor`):
        The data to gather.

Returns:
    The same data structure as `tensor` with all tensors sent to the proper device.
)r
   r¶   r   ÚXLAr‘   r   r«   r   s   &r   ÚgatherrÑ   ¨  sF   € ô ƒ~×&Ñ&¬/×*=Ñ*=Ô=Ü˜6Ó"Ð"Ü	‹×	(Ñ	(Ô,MÔ	MÜ˜6Ó"Ð"àˆr   c                ó$   € V ^8„  d   QhR\         /# ©é   Úobjectr   )Úformats   "r   Ú__annotate__r×   ¼  s   € ÷ 3ñ 3¤#ñ 3r   c           	     ób  € ^ RI p\        4       pVP                  pVP                  pVP	                  4       p\
        P                  ! W4       VP                  4       p\        V4      p\        P                  ! V.\        P                  VR7      p\        V4       U	u. uF*  p	\        P                  ! ^\        P                  VR7      NK,  	  p
p	\        P                  P                  W¨4       \!        \#        R V
 4       4      4      pRpWË8  d   V^,          pK  \        P$                  P'                  V4      p\        P(                  ! V4      P+                  V4      pVP-                  V4       \        P.                  ! WÄ,          \        P0                  VR7      p\        V4       Uu. uF  pWüV,          VV^,           ,           NK  	  pp\        P                  P                  VV4       . p\3        V4       F¥  w  pp\!        V
V,          P5                  4       4      pVRV P7                  4       P9                  4       P;                  4       p\
        P<                  ! V4      p\?        V\@        4      '       d   VPC                  V4       K”  VPE                  V4       K§  	  V# u up	i u upi )a¾  Gather picklable objects from all ranks with padded allgather sizes.

On Neuron devices every unique tensor shape triggers a new NEFF compilation. The standard
``all_gather_object`` sizes its byte tensor to the exact max pickle size, which varies per call
and causes unbounded compilation cache growth.  This variant rounds the allgather size up to
the next power of 2 so the number of distinct compiled shapes stays bounded to O(log(max_size)).
Nr•   c              3   ó@   "  € T F  qP                  4       x € K  	  R # 5ir   )Úitem)r?   Úss   & r   rD   Ú(_neuron_gather_object.<locals>.<genexpr>Ô  s   é € Ð3© A—v‘v—x�x«ùs   ‚é   )#Úior
   rU   r›   ÚBytesIOÚpickleÚdumpÚgetvaluerr   r   r   ÚlongrŸ   Úzerosr¡   r‹   rX   ÚmaxÚByteStorageÚ_from_bufferr    rM   Úresize_rj   Úuint8r½   rÚ   r{   ÚnumpyÚtobytesÚloadsr   r5   ÚextendÚappend)rÕ   rÞ   r¦   rU   Ú
group_sizeÚbufÚ	raw_bytesÚ
local_sizeÚlocal_size_tensorr¤   Ú	size_listÚmax_sizeÚpadded_sizeÚbyte_storageÚinput_tensorÚ	coalescedrÂ   r£   Úresultr   Úobj_sizeÚ	obj_bytesr6   s   &                      r   Ú_neuron_gather_objectrý   ¼  s  € ó ä‹N€EØ�\‰\€FØ×$Ñ$€Jð �*‰*‹,€CÜ
‡K‚K�ÔØ—‘“€IÜ�Y“€Jô Ÿš j \¼¿¹ÈFÔSÐÜJOÐPZÔJ[Ó\ÑJ[ÀQ”—’˜Q¤e§j¡j¸×@ÑJ[€IÐ\Ü	×Ñ× Ñ  Ô>Ü”3Ñ3©Ó3Ó3Ó4€Hð €KØ
Ô
 Ø˜ÕŠô ×$Ñ$×1Ñ1°)Ó<€LÜ×#Ò# LÓ1×4Ñ4°VÓ<€LØ×Ñ˜Ô%ô —’˜KÕ4¼E¿K¹KÐPVÔW€IÜRWÐXbÔRcÓdÑRcÈQ�i¨a¥°+ÀÀQÅÕ2GÓHÑRc€NÐdÜ	×Ñ× Ñ  °Ô>ð €FÜ˜~Ö.‰	ˆˆ6Ü�y •|×(Ñ(Ó*Ó+ˆØ˜9˜HÐ%×)Ñ)Ó+×1Ñ1Ó3×;Ñ;Ó=ˆ	Ü�lŠl˜9Ó%ˆÜ�cœ4× Ò Ø�M‰M˜#Öà�M‰M˜#Öñ /ð €Mùò; ]ùò  es   Â0J'Æ+"J,c                ó$   € V ^8„  d   QhR\         /# rÓ   r   )rÖ   s   "r   r×   r×   ò  s   € ÷ 2ñ 2œsñ 2r   c                 óä   € \        \        4       P                  4       Uu. uF  pR NK  	  pp\        P                  P                  W 4       V UUu. uF  q3 F  qDNK  	  K  	  upp# u upi u uppi r   )rŸ   r
   r›   r   r¡   Úall_gather_object)rÕ   r¤   Úoutput_objectsÚyÚxs   &    r   Ú_gpu_gather_objectr  ò  sa   € Ü$)¬,«.×*FÑ*FÔ$GÓHÑ$G˜q“dÑ$G€NÐHÜ	×Ñ×'Ñ'¨Ô?á%Ô1‘~�!ªq¨!ŠA©q‰A‘~Ò1Ð1ùò Iùó 2s    A'ÁA,c                ó$   € V ^8„  d   QhR\         /# rÓ   r   )rÖ   s   "r   r×   r×   ù  s   € ÷ ñ œ#ñ r   c                ó  € \        4       P                  \        P                  8X  d   \	        R4      h\        4       P                  \
        9   d:   \        4       P                  P                  R8X  d   \        V 4      # \        V 4      # V # )a  
Recursively gather object in a nested list/tuple/dictionary of objects from all devices.

Args:
    object (nested list/tuple/dictionary of picklable object):
        The data to gather.

Returns:
    The same data structure as `object` with all the objects sent to every device.
z&gather objects in TPU is not supportedÚneuron)
r
   r¶   r   rÐ   ÚNotImplementedErrorr   rU   r4   rý   r  )rÕ   s   &r   rº   rº   ù  sh   € ô ƒ~×&Ñ&¬/×*=Ñ*=Ô=Ü!Ð"JÓKÐKÜ	‹×	(Ñ	(Ô,MÔ	MÜ‹>× Ñ ×%Ñ%¨Ô1Ü(¨Ó0Ð0Ü! &Ó)Ð)àˆr   c                 ó(   € RR lp\        W RVR7      # )r…   c                 óF   € \         P                  P                  WR 7       V # )©Úsrc)r   r¡   Ú	broadcast)r   r  s   &&r   Ú_gpu_broadcast_oneÚ*_gpu_broadcast.<locals>._gpu_broadcast_one  s   € Ü×Ñ×#Ñ# FÐ#Ô4Øˆr   T)r:   r  ©r…   r=   )r1   r  r  s   && r   Ú_gpu_broadcastr    s   € ôô Ð/È4ÐUXÔYÐYr   c                 ót  aa€ \        V \        \        34      '       d    \        V V3R  l\	        V 4       4       4      # \        V \
        4      '       dE   \        V 4      ! V P                  4        UUu/ uF  w  r4V\        VS RV 2R7      bK  	  upp4      # \        P                  ! SV V3R l4      # u uppi )c              3   óL   <"  € T F  w  r\        VS R V 2R7      x € K  	  R# 5i)r¤   ©ÚnameN)Ú_tpu_broadcast)r?   rÂ   rT   r  s   &  €r   rD   Ú!_tpu_broadcast.<locals>.<genexpr>  s)   øé € Ð"gÑUfÉTÈQ¤>°!¸T¸FÀ!ÀAÀ3¸-×#HÑ#HÓUfùrF   r¤   r  c                 ó   <€ V S,          # r   r®   )r  r  s   &€r   Ú<lambda>Ú _tpu_broadcast.<locals>.<lambda>  s	   ø€ °!°C¶&r   )r   r5   r.   r8   r½   r   r4   rG   r  rŠ   Úmesh_reduce)r   r  r  rJ   rK   s   &ff  r   r  r    s—   ù€ Ü�&œ4¤˜-×(Ò(Ü˜&Ô"gÔU^Ð_eÔUfÓ"gÓhÐhÜ	�FœG×	$Ò	$Ü�FŒ|ÐRX×R^ÑR^ÔR`ÔaÑR`É$È!˜Q¤¨q¸$¸¸qÀÀ°}Ô EÒEÑR`ÒaÓbÐbÜ�>Š>˜$ Ô(8Ó9Ð9ùó bs   Á2B4
c                óÊ  € Rp\        4       p\        P                  ! V\        P                  VP                  R7      pV ed   V P
                  p\        V P                  ,          p\        P                  ! \        V4      V.,           \        R7      VR\        V4      ^,           % \        VRR7      pW3P                  4       ,          p\        VRR ^ ,          4      pVRR pW63# )z]
Grabs the shape of `tensor` only available on one process and returns a tensor of its shape
r•   Nri   Úsum©Ú	reductionrÝ   r™   )r
   r   rj   rX   rU   r]   ÚTENSOR_TYPE_TO_INTr^   r   r5   rr   ÚreduceÚnonzero)r   Úmax_tensor_dimensionr¦   Úbase_tensorr]   Útensor_dtyper^   s   &      r   Úgather_tensor_shaper&  .  s¿   € ð
 !ÐÜ‹N€EÜ—+’+Ð2¼%¿)¹)ÈEÏLÉLÔY€Kð
 ÒØ—‘ˆÜ)¨&¯,©,Õ7ˆÜ(-¯ª´T¸%³[ÀLÀ>Õ5QÔY\Ô(]ˆÐ$”c˜%“j 1•nÐ%ä˜°Ô6€KØ×1Ñ1Ó3Õ4€Kä�˜B˜CÐ  Õ#Ó$€EØ˜c˜rÐ"€KØÐÐr   c                ó8   € V ^8„  d   QhR\         P                  /# )rÔ   Úreturn)r   r   )rÖ   s   "r   r×   r×   G  s   € ÷ +ñ +¬5¯<©<ñ +r   c                óÊ   € \        4       p\        V 4      w  r#V f=   \        P                  ! V\        V,          R7      P                  VP                  4      p \        V RR7      # )at  
Copies a tensor that only exists on a single device and broadcasts it to other devices. Differs from `broadcast` as
each worker doesn't need to know its shape when used (and tensor can be `None`)

Args:
    tensor (`torch.tensor`):
        The tensor that should be sent to all devices. Must only have it be defined on a single device, the rest
        should be `None`.
ri   r  r  )r
   r&  r   rä   ÚTENSOR_INT_TO_DTYPErM   rU   r!  )r   r¦   r]   r^   s   &   r   Úcopy_tensor_to_devicesr+  G  sN   € ô ‹N€EÜ& vÓ.�L€EØ‚~Ü—’˜UÔ*=¸eÕ*DÔE×HÑHÈÏÉÓVˆÜ�& EÔ*Ð*r   c                ó$   € V ^8„  d   QhR\         /# ©rÔ   Úfrom_process©rX   )rÖ   s   "r   r×   r×   Y  s   € ÷ ñ ¤Cñ r   c                óÀ   € \        4       P                  \        P                  8X  d   \	        WRR7      # \        4       P                  \
        9   d   \        WR7      # V # )a…  
Recursively broadcast tensor in a nested list/tuple/dictionary of tensors to all devices.

Args:
    tensor (nested list/tuple/dictionary of `torch.Tensor`):
        The data to gather.
    from_process (`int`, *optional*, defaults to 0):
        The process from which to send the data

Returns:
    The same data structure as `tensor` with all tensors broadcasted to the proper device.
zaccelerate.utils.broadcast)r  r  r  )r
   r¶   r   rÐ   r  r   r  )r   r.  s   &&r   r  r  X  sI   € ô ƒ~×&Ñ&¬/×*=Ñ*=Ô=Ü˜fÐ=YÔZÐZÜ	‹×	(Ñ	(Ô,MÔ	MÜ˜fÔ7Ð7àˆr   c                ó$   € V ^8„  d   QhR\         /# r-  r/  )rÖ   s   "r   r×   r×   n  s   € ÷ 2ñ 2¼Sñ 2r   c                óú  € ^ RI p\        4       pVP                  pVP                  V8X  dC   VP	                  4       p\
        P                  ! W4       VP                  4       p\        V4      pMRp^ p\        P                  ! V.\        P                  VR7      p\        P                  P                  V4       \        VP                  4       4      p	Rp
W©8  d   V
^,          p
K  VP                  V8X  dW   \        P                   P#                  V4      p\        P$                  ! V4      P'                  V4      pVP)                  V
4       M'\        P*                  ! V
\        P,                  VR7      p\        P                  P/                  WÁR7       \
        P0                  ! VRV	 P3                  4       P5                  4       P7                  4       4      p\9        \        V 4      4       F  pWÞ,          W&   K  	  V # )a  Broadcast a list of picklable objects with padded tensor sizes.

On Neuron devices ``ProcessGroupNeuron.broadcast()`` is implemented as an allreduce,
so every unique tensor shape triggers a new NEFF compilation.  The standard
``broadcast_object_list`` sizes its byte tensor to the exact serialized length, which
varies per call and causes unbounded compilation cache growth.  This variant rounds the
broadcast tensor size up to the next power of 2, bounding the number of distinct
compiled shapes to ~O(log(max_size)).
Nr   r•   r  rÝ   )rÞ   r
   rU   Úprocess_indexrß   rà   rá   râ   rr   r   r   rã   r¡   Ú
all_reducerX   rÚ   ræ   rç   r    rM   rè   rä   ré   r  rì   r{   rê   rë   rŸ   )Úobject_listr.  rÞ   r¦   rU   rð   rñ   rò   Úsize_tensorÚ	real_sizerö   r÷   Údata_tensorrú   rÂ   s   &&             r   Ú_neuron_broadcast_object_listr9  n  s…  € ó ä‹N€EØ�\‰\€Fð ×Ñ˜lÔ*Ø�j‰j‹lˆÜ�Š�KÔ%Ø—L‘L“Nˆ	Ü˜“^‰
àˆ	Øˆ
ô —,’, 
˜|´5·:±:ÀfÔM€KÜ	×Ñ× Ñ  Ô-Ü�K×$Ñ$Ó&Ó'€Ið €KØ
Ô
!Ø˜ÕŠð ×Ñ˜lÔ*Ü×(Ñ(×5Ñ5°iÓ@ˆÜ×&Ò& |Ó4×7Ñ7¸Ó?ˆØ×Ñ˜KÕ(ä—k’k +´U·[±[ÈÔPˆä	×Ñ×Ñ ÐÔ>ô �\Š\˜+ j yÐ1×5Ñ5Ó7×=Ñ=Ó?×GÑGÓIÓJ€FÜ”3�{Ó#Ö$ˆØ�ˆ‹ñ %àÐr   c                ó$   € V ^8„  d   QhR\         /# r-  r/  )rÖ   s   "r   r×   r×   £  s   € ÷ ñ ´Sñ r   c                óž  a€ \        4       P                  \        P                  8X  d5   \	        V 4       F#  w  r#\
        P                  ! RVV3R l4      W&   K%  	  V # \        4       P                  \        9   dT   \        4       P                  P                  R8X  d   \        V SR7       V # \        P                  P                  V SR7       V # )ar  
Broadcast a list of picklable objects from one process to the others.

Args:
    object_list (list of picklable objects):
        The list of objects to broadcast. This list will be modified inplace.
    from_process (`int`, *optional*, defaults to 0):
        The process from which to send the data.

Returns:
    The same list containing the objects from process 0.
z&accelerate.utils.broadcast_object_listc                 ó   <€ V S,          # r   r®   )r  r.  s   &€r   r  Ú'broadcast_object_list.<locals>.<lambda>²  s   ø€ ÐefÐgsÖetr   r  )r.  r  )r
   r¶   r   rÐ   r½   rŠ   r  r   rU   r4   r9  r   r¡   Úbroadcast_object_list)r5  r.  rÂ   r6   s   &f  r   r>  r>  £  s§   ø€ ô ƒ~×&Ñ&¬/×*=Ñ*=Ô=Ü Ö,‰FˆAÜŸ^š^Ð,TÐVYÔ[tÓuˆK‹Nñ -ð Ðô 
‹×	(Ñ	(Ô,MÔ	MÜ‹>× Ñ ×%Ñ%¨Ô1Ü)¨+ÀLÕQð Ðô ×Ñ×3Ñ3°KÀ\Ð3ÔRØÐr   c                ó    € R p\        W@V4      # )a*  
Recursively takes a slice in a nested list/tuple/dictionary of tensors.

Args:
    data (nested list/tuple/dictionary of `torch.Tensor`):
        The data to slice.
    tensor_slice (`slice`):
        The slice to take.

Returns:
    The same data structure as `data` with all the tensors slices.
c                 ó   € W,          # r   r®   )r   Útensor_slices   &&r   Ú_slice_tensorÚ$slice_tensors.<locals>._slice_tensorÉ  s   € ØÕ#Ð#r   r=   )r1   rA  r3  r›   rB  s   &&&& r   Úslice_tensorsrD  »  s   € ò$ô ˜]°,Ó?Ð?r   c                óî  a a€ \        S ^ ,          \        \        34      '       d8   \        S ^ ,          V V3R l\	        \        S ^ ,          4      4       4       4      # \        S ^ ,          \        4      '       da   \        S ^ ,          4      ! S ^ ,          P                  4        UUu/ uF&  q"\        S  Uu. uF  q3V,          NK  	  upSR7      bK(  	  upp4      # \        S ^ ,          \        P                  4      '       d   \        P                  ! S SR7      # \        S \        \        34      '       d   \        S 4      ^8X  d
   S ^ ,          # \        R\        S ^ ,          4       24      hu upi u uppi )aÑ  
Recursively concatenate the tensors in a nested list/tuple/dictionary of lists of tensors with the same shape.
If there is only a single batch of data, it is returned as-is.

Args:
    data (nested list/tuple/dictionary of lists of tensors `torch.Tensor`):
        The data to concatenate.
    dim (`int`, *optional*, defaults to 0):
        The dimension on which to concatenate.

Returns:
    The same data structure as `data` with all the tensors concatenated.
c              3   ót   <"  € T F(  p\        S Uu. uF  q"V,          NK  	  upSR 7      x € K*  	  R# u upi 5i)r—   N)Úconcatenate)r?   rÂ   Údr1   r˜   s   &  €€r   rD   Úconcatenate.<locals>.<genexpr>Þ  s4   øé € Ð#lÑXkÐST¤K¹tÓ0D¹t¸!°1·°¹tÑ0DÈ#×$NÑ$NÓXkùÒ0Dùs   ƒ8‘3
¢8r—   z%Can only concatenate tensors but got )r   r.   r5   r8   rŸ   rr   r   r4   ru   rG  r   r   r¢   rH   )r1   r˜   rJ   rH  s   ff  r   rG  rG  Ï  s  ù€ ô �$�q•'œE¤4˜=×)Ò)Ü˜$˜q�'Õ#lÔX]Ô^aÐbfÐghÕbiÓ^jÔXkÓ#lÓmÐmÜ	�D˜•GœW×	%Ò	%Ü�D˜•GŒ}ÐUYÐZ[ÕU\×UaÑUaÔUcÔdÑUcÐPQ¤¹DÓ-A¹D°q°¯d¨d¹DÑ-AÀsÔ!KÒKÑUcÒdÓeÐeÜ	�D˜•GœUŸ\™\×	*Ò	*Ü�yŠy˜ 3Ô'Ð'Ü	�Dœ5¤$˜-×	(Ò	(¬S°«Y¸!¬^Ø�A�wˆäÐ?ÄÀTÈ!ÅWÃ¸ÐOÓPÐPùò .BùÓds   Â&E1
Â4E,ÃE1
Å,E1
c                   ó   € ] tR tRtRtR# )ÚCannotPadNestedTensorWarningié  r®   N)rI   r¯   r°   r±   r³   r®   r   r   rK  rK  é  s   † Ûr   rK  c           	     ó*   € RR lp\        W@RWVR7      # )a  
Recursively pad the tensors in a nested list/tuple/dictionary of tensors from all devices to the same size so they
can safely be gathered.

Args:
    tensor (nested list/tuple/dictionary of `torch.Tensor`):
        The data to gather.
    dim (`int`, *optional*, defaults to 0):
        The dimension on which to pad.
    pad_index (`int`, *optional*, defaults to 0):
        The value with which to pad.
    pad_first (`bool`, *optional*, defaults to `False`):
        Whether to pad at the beginning or the end.
c                 ó  aa	a
€ \        V R R4      '       d   \        P                  ! R\        4       V # S\	        V P
                  4      8¼  g   S\	        V P
                  4      ) 8  d   V # S^ 8  d   S\	        V P
                  4      ,          o\        P                  ! V P
                  V P                  R7      R,          p\        V4      P                  4       p\        V3R lV 4       4      o	S	V P
                  S,          8X  d   V # V P
                  o
\        S
4      pS	VS&   V P                  \        V4      4      V,           pV'       d\   \        ;QJ d+    . VV	V
3R l\        \	        V4      4       4       F  NK  	  5M$! VV	V
3R l\        \	        V4      4       4       4      pMX\        ;QJ d*    . VV
3R l\        \	        V4      4       4       F  NK  	  5M#! VV
3R l\        \	        V4      4       4       4      pWV&   V# )Ú	is_nestedFzHCannot pad nested tensors without more information. Leaving unprocessed.)rU   Nc              3   ó4   <"  € T F  qS,          x € K  	  R # 5ir   r®   )r?   rÛ   r˜   s   & €r   rD   ÚFpad_across_processes.<locals>._pad_across_processes.<locals>.<genexpr>  s   øé € Ð-¡u !˜—v’v£uùs   ƒc              3   óz   <"  € T F0  qS8X  d   \        SSS,          ,
          S4      M
\        R 4      x € K2  	  R # 5ir   ©Úslice)r?   rÂ   r˜   rõ   Úold_sizes   & €€€r   rD   rP    s4   øé € ð Ù`tÐ[\À#¼X”�h ¨#¥Õ.°Ô9Ì5ÐQUË;ÔVÓ`tùs   ƒ8;c              3   ól   <"  € T F)  qS8X  d   \        ^ SS,          4      M
\        R4      x € K+  	  R# 5ir„   rR  ©r?   rÂ   r˜   rT  s   & €€r   rD   rP    s,   øé € ÐoÑZnÐUV¸C´xœE ! X¨c¥]Ô3ÄUÈ4Ã[ÔPÓZnùó   ƒ14)ÚgetattrÚwarningsÚwarnrK  rr   r]   r   r   rU   rÑ   r{   rå   r5   Ú	new_zerosr.   rŸ   )r   r˜   Ú	pad_indexÚ	pad_firstrž   ÚsizesÚnew_sizeÚ
new_tensorÚindicesrõ   rT  s   &f&&     @@r   Ú_pad_across_processesÚ3pad_across_processes.<locals>._pad_across_processesþ  s†  ú€ Ü�6˜;¨×.Ò.Ü�MŠMØZÜ,ôð ˆMØ”#�f—l‘lÓ#Ô# s¬c°&·,±,Ó.?Ð-?Ô'?ØˆMà�Œ7Ø”3�v—|‘|Ó$Õ$ˆCô �|Š|˜FŸL™L°·±Ô?ÀÕEˆÜ�t“× Ñ Ó"ˆäÔ-¡uÓ-Ó-ˆØ�v—|‘| CÕ(Ô(ØˆMà—<‘<ˆÜ˜“>ˆØ ˆ�‰Ø×%Ñ%¤e¨H£oÓ6¸ÕBˆ
ßß”eö Ü`eÔfiÐjrÓfsÔ`tó—e‘eö Ü`eÔfiÐjrÓfsÔ`tóó ‰G÷ ”eÕoÔZ_Ô`cÐdlÓ`mÔZnÓo—e‘eÕoÔZ_Ô`cÐdlÓ`mÔZnÓoÓoˆGØ$�7ÑØÐr   T)r:   r˜   r\  r]  ©r…   r…   Fr=   )r   r˜   r\  r]  rb  s   &&&& r   Úpad_across_processesre  í  s!   € ô" ôD Ø¸4ÀSÐirôð r   c           	     ó.   € RR lp\        VV RVVVR7      # )zê
Takes a `tensor` of arbitrary size and pads it so that it can work given `num_processes` needed dimensions.

New tensors are just the last input repeated.

E.g.:
  Tensor: ([3,4,4]) Num processes: 4 Expected result shape: ([4,4,4])

c                 óò  aa
€ W,          pWV,          ,
          pW,          ^ 8X  d
   W!,
          pMW!V,          ,
          pWVV,          u;8”  d   ^8  d   M MWV,
          pV P                   o
\        S
4      pW,           V^ &   V P                  \        V4      4      p\        ;QJ d*    . VV
3R l\	        \        V4      4       4       F  NK  	  5M#! VV
3R l\	        \        V4      4       4       4      p	WV	&   V# )r…   c              3   ól   <"  € T F)  qS8X  d   \        ^ SS,          4      M
\        R4      x € K+  	  R# 5ir„   rR  rV  s   & €€r   rD   Ú@pad_input_tensors.<locals>._pad_input_tensors.<locals>.<genexpr>?  s,   øé € ÐkÑVjÐQR¸´8œ˜a ¨#¥Ô/ÄÀtÃÔLÓVjùrW  )r]   r5   r[  r.   rŸ   rr   )r   Ú
batch_sizer›   r˜   Ú	remainderÚlast_inputsÚto_padr_  r`  ra  rT  s   &&&f      @r   Ú_pad_input_tensorsÚ-pad_input_tensors.<locals>._pad_input_tensors0  sÉ   ù€ ØÕ/ˆ	Ø °Õ$=Õ>ˆØÕ&¨!Ô+Ø"Õ/‰Fà"°MÕ&AÕBˆFð  &�Ö,¨1×,Ø Õ)ˆFØ—<‘<ˆÜ˜“>ˆØ Õ)ˆ�‰Ø×%Ñ%¤e¨H£oÓ6ˆ
ß”%ÕkÔV[Ô\_Ð`hÓ\iÔVjÓk—%‘%ÕkÔV[Ô\_Ð`hÓ\iÔVjÓkÓkˆØ$�7ÑØÐr   T)r:   rj  r›   r˜   r  r=   )r   rj  r›   r˜   rn  s   &&&& r   Úpad_input_tensorsrp  %  s(   € ôô& ØØØ ØØ#Øôð r   c                ó(   € RR lp\        W0RWR7      # )a.  
Recursively reduce the tensors in a nested list/tuple/dictionary of lists of tensors across all processes by the
mean of a given operation.

Args:
    tensor (nested list/tuple/dictionary of `torch.Tensor`):
        The data to reduce.
    reduction (`str`, *optional*, defaults to `"mean"`):
        A reduction method. Can be of "mean", "sum", "max", or "none"
    scale (`float`, *optional*):
        A default scaling value to be applied after the reduce, only valid on XLA.

Returns:
    The same data structure as `data` with all the tensors reduced.
c                 óŠ  € \        4       pV P                  4       pVP                  \        P                  8X  d   V# VP                  \        P
                  8X  dk   \        P                  ! 4        VR 8X  d   \        P                  M\        P                  p\        P                  ! WT.V4       \        P                  ! 4        MeVP                  P                  \        9   dG   VR 8X  d   \        P                  M\        P                  p\         P"                  P                  WF4       VR8X  d   WCP$                  ,          pV# )rå   Úmean)r
   r‡   r¶   r   r·   rÐ   rŠ   r�   Ú
REDUCE_MAXÚ
REDUCE_SUMr4  Úvaluer   r   ÚMAXÚSUMr   r¡   r›   )r   r  Úscaler¦   Úcloned_tensorÚxla_opÚtorch_ops   &&&    r   Ú_reduce_across_processesÚ(reduce.<locals>._reduce_across_processes_  s×   € Ü“ˆØŸ™›ˆØ×!Ñ!¤_×%7Ñ%7Ô7Ø Ð Ø×!Ñ!¤_×%8Ñ%8Ô8ô
 �LŠLŒNØ&/°5Ô&8”R—]’]¼b¿m¹mˆFÜ�MŠM˜& /°5Ô9Ü�LŠL�NØ×#Ñ#×)Ñ)Ô-NÔNØ'0°EÔ'9”x—|’|¼x¿|¹|ˆHÜ×Ñ×(Ñ(¨ÔAØ˜ÔØ×0Ñ0Õ0ˆMØÐr   T)r:   r  ry  ©rs  g      ð?r=   )r   r  ry  r}  s   &&& r   r!  r!  M  s   € ô$ô* Ø ¸dÈiôð r   c                ó(   € R pR p\        WVR7      # )aZ  
Recursively converts the elements nested list/tuple/dictionary of tensors in FP16/BF16 precision to FP32.

Args:
    tensor (nested list/tuple/dictionary of `torch.Tensor`):
        The data to convert from FP16/BF16 to FP32.

Returns:
    The same data structure as `tensor` with all tensors that were in FP16/BF16 precision converted to FP32.
c                 ó"   € V P                  4       # r   )Úfloatr   s   &r   Ú_convert_to_fp32Ú)convert_to_fp32.<locals>._convert_to_fp32…  s   € Ø�|‰|‹~Ðr   c                 óª   € \        V 4      ;'       g    \        V R 4      ;'       d.    V P                  \        P                  \        P
                  39   # ri   )r   r/   r^   r   Úfloat16r|   r   s   &r   Ú_is_fp16_bf16_tensorÚ-convert_to_fp32.<locals>._is_fp16_bf16_tensorˆ  sH   € Ü Ó'×CÐC¬7°6¸7Ó+C÷ 
ð 
ÈÏÉÜ�M‰MÜ�N‰NðZ
ñ J
ð 	
r   rm   r=   )r   rƒ  r‡  s   &  r   Úconvert_to_fp32r‰  y  s   € òò
ô Ð-ÐAUÔVÐVr   c                   ó6   a € ] tR tRt o RtR tR tR tRtV t	R# )ÚConvertOutputsToFp32i‘  aE  
Decorator to apply to a function outputting tensors (like a model forward pass) that ensures the outputs in FP16
precision will be convert back to FP32.

Args:
    model_forward (`Callable`):
        The function which outputs we want to treat.

Returns:
    The same function as `model_forward` but with converted outputs.
c                ó(   € Wn         \        W4       R # r   )Úmodel_forwardr   )Úselfr�  s   &&r   Ú__init__ÚConvertOutputsToFp32.__init__ž  s   € Ø*ÔÜ�tÖ+r   c                ó8   € \        V P                  ! V/ VB 4      # r   )r‰  r�  )rŽ  rA   rC   s   &*,r   Ú__call__ÚConvertOutputsToFp32.__call__¢  s   € Ü˜t×1Ò1°4ÐB¸6ÑBÓCÐCr   c                ó.   € \         P                  ! R 4      h)z‘Cannot pickle a prepared model with automatic mixed precision, please unwrap the model with `Accelerator.unwrap_model(model)` before pickling it.)rà   ÚPicklingError)rŽ  s   &r   Ú__getstate__Ú!ConvertOutputsToFp32.__getstate__¥  s   € Ü×"Ò"ð `ó
ð 	
r   )r�  N)
rI   r¯   r°   r±   r²   r�  r’  r–  r³   Ú__classdictcell__)Ú__classdict__s   @r   r‹  r‹  ‘  s   ø‡ € ñ
ò,òD÷
ð 
r   r‹  c                 ó8   a € \        S 4      o V 3R  lpS Vn        V# )c                  ó   <€ S! V / VB # r   r®   )rA   rC   r�  s   *,€r   ÚforwardÚ(convert_outputs_to_fp32.<locals>.forward®  s   ø€ Ù˜dÐ- fÑ-Ð-r   )r‹  Ú__wrapped__)r�  rœ  s   f r   Úconvert_outputs_to_fp32rŸ  «  s!   ø€ Ü(¨Ó7€Mõ.ð (€GÔà€Nr   c                óV  € \        V \        4      '       d-   V P                  4        F  p\        V4      pVf   K  Vu # 	  R# \        V \        \
        34      '       d   V  F  p\        V4      pVf   K  Vu # 	  R# \        V \        P                  4      '       d   V P                  # R# )zÕ
Finds the device on which a nested dict/list/tuple of tensors lies (assuming they are all on the same device).

Args:
    (nested list/tuple/dictionary of `torch.Tensor`): The data we want to know the device of.
N)	r   r   Úvaluesr¹   r.   r5   r   r   rU   )r1   r6   rU   s   &  r   r¹   r¹   ·  sŠ   € ô �$œ× Ò Ø—;‘;–=ˆCÜ  Ó%ˆFØÔ!Ø’ó !ô 
�Dœ5¤$˜-×	(Ò	(ÛˆCÜ  Ó%ˆFØÔ!Ø’ó ô 
�Dœ%Ÿ,™,×	'Ò	'Ø�{‰{Ðñ 
(r   c              #  ó†  "  € \        4       P                  \        P                  8w  g?   \        4       P                  e4   \        4       P                  P                  4       '       g   \        4       pM!^ RIpVP                  P                  WW#R7      pV;_uu_ 4        Rx € RRR4       R#   + '       g   i     R# ; i5i)z|
Wrapper around `deepspeed.runtime.zero.GatheredParameters`, but if Zero-3 is not enabled, will be a no-op context
manager.
N)Úmodifier_rankÚ
fwd_moduleÚenabled)
r	   r¶   r   Ú	DEEPSPEEDÚdeepspeed_pluginÚis_zero3_init_enabledr   Ú	deepspeedÚzeroÚGatheredParameters)Úparamsr£  r¤  r¥  Úgather_param_contextr©  s   &&&&  r   r«  r«  Ì  s‰   é € ô Ó×*Ñ*¬o×.GÑ.GÔGÜÓ×+Ñ+Ò7Ü Ó"×3Ñ3×IÑI×KÒKä*›}Ñãà(Ÿ~™~×@Ñ@Ø¸Jð  Aó  
Ð÷ 
Ò	Û÷ 
×	×	Ò	üs   ‚BCÂB-Â#
CÂ-B>	Â8	C)FNr  )r…   zbroadcast tensorr   )NNrd  r  )NNT)Yr²   rà   rY  Úcollections.abcr   Ú
contextlibr   r   Ú	functoolsr   r   Útypingr   r   r¦   r	   r
   Ú	constantsr   Údataclassesr   r   Úimportsr   r   r   Úversionsr   Útorch_xla.core.xla_modelÚcoreÚ	xla_modelrŠ   Útorch.distributedr   r   r&   r*   r2   r8   r>   rS   ra   rf   ro   rt   rw   r�   r‘   r«   Ú	Exceptionr­   rÉ   rÎ   rÑ   rý   r  rº   r  r  r‚  ÚdoubleÚhalfr|   ré   Úint8Úint16Úint32Úint64Úboolr   rG   r*  r&  r+  r  r9  r>  rD  rG  ÚUserWarningrK  re  rp  r!  r‰  r‹  rŸ  r¹   r«  )rJ   rK   s   00r   Ú<module>rÃ     s,  ðñó Û Ý #ß 2ß +Ý ã ç 2Ý 8ß ;÷ñ õ
 'ñ ×Òß)Ð)á!×#Ò#Ý*ò,ò
ò6ò]ò$ð0°?ð 0ÐX]ô 0ôf7òt8ò$/ò$bòò.ò"5ò.ò$PôN	 Iô 	ò òFð& ñó ðõ&3õl2õô*Zô:ð 
‡K�K�Ø	‡L�L�!Ø	‡J�J�Ø	‡N�N�AØ	‡K�K�Ø	‡J�J�Ø	‡K�K�Ø	‡K�K�Ø	‡K�K�Ø	‡J�J�ðÐ ð );×(@Ñ(@Ô(BÔCÑ(B¡ �q’tÑ(BÒCÐ ò÷2+ð" öó ð÷*2÷jô0@ô(Qô4	 ;ô 	ð ó4ó ð4ôn%ðP ó(ó ð(òVW÷0
ñ 
ò4	òð* óó òùóE Ds   Å1G7