
    ;i`7                         S SK r S SKrS SKrS SKrSSKJrJr  SSKJrJ	r	J
r
JrJrJrJrJr  SSKJr  S r              S	S jrS
S jrg)    N   )AcceleratorStatePartialState)PrecisionTypePrepareForLaunchare_libraries_initializedcheck_cuda_p2p_ib_supportget_gpu_infois_mps_availableis_torch_versionpatch_environment)0ELASTIC_LOG_LINE_PREFIX_TEMPLATE_PYTORCH_VERSIONc                      [        5       n g)z+Verify a `PartialState` can be initialized.N)r   )_s    d/home/dmtnaga/Documents/work/airagagent/rag_env/lib/python3.13/site-packages/accelerate/launchers.pytest_launchr   #   s	    A    c                 
   SnSn[        S [        R                  R                  5        5       5      (       a  SnOAS[        R
                  ;   a-  S[        [        R
                  S   R                  5       5      ;   n [        UR                  5       5      nU(       d  U(       a  [        R                  R                  S	S
5      c$  [        R                  R                  SS5      S:X  aX  SS
KJs  Jn  [#        [$        R&                  5      S:  a  [        S5      e[)        U SS9n[+        S5        UR-                  UUSS9  g
U(       aQ  [/        5       S   S:  a@  [0        R2                  R5                  5       (       a  [+        S5        O[+        S5        U " U6   g
Uc  [        S5      eXg:  a  [        S5      eUS:  Ga  SSKJnJn  SSKJn  SSK J!n  [#        [$        R&                  5      S:  a  [        S5      e[E        S5      n[#        U5      S:  a  S nU H  nUS!U S"3-  nM     [G        U5      e[I        UUXr-  UUUS#9n[K        5       (       d
  S$US%'   S$US&'   [M        S/0 UD6   [O        [0        S'5      (       a(  [0        RP                  RS                  5       RT                  OS(nUS):X  a  S*OS+n[        R                  R                  S,S-5      R                  5       S.:X  a  [)        [V        US9n U" US/USS09  [)        U US9n[+        S7U S8URY                  5        S935         U
c  0 n
US::X  a  XjS;'   U	(       d  U S<U 3n	[I        UUUUU	UU
UUSS=9
n[]        S>[^        5      (       a  UUS?'   U" U" S/0 UD6U S@9" U6    S
S
S
5        g
[a        5       (       a  S$[        R                  SC'   [+        SD5        Oi[0        R2                  R5                  5       (       a  [+        S5        O:[0        Rb                  R5                  5       (       a  [+        SE5        O[+        SF5        U " U6   g
! [         a>    [        SUR                  R                  5        S[        R                  " 5        S35      ef = f! U a]  nS1nS2URY                  5        S33URZ                  S   ;   a   [G        U S4URY                  5        S535      Ue[G        U S6U 35      UeS
nAff = f! U ag  nS2URY                  5        S33URZ                  S   ;   a.  [G        URY                  5        SAURY                  5        S535      Ue[G        SBU 35      UeS
nAff = f! , (       d  f       g
= f)Ga  
Launches a training function, using several processes or multiple nodes if it's possible in the current environment
(TPU with multiple cores for instance).

<Tip warning={true}>

To use this function absolutely zero calls to a device must be made in the notebook session before calling. If any
have been made, you will need to restart the notebook and make sure no cells use any device capability.

Setting `ACCELERATE_DEBUG_MODE="1"` in your environment will run a test before truly launching to ensure that none
of those calls have been made.

</Tip>

Args:
    function (`Callable`):
        The training function to execute. If it accepts arguments, the first argument should be the index of the
        process run.
    args (`Tuple`):
        Tuple of arguments to pass to the function (it will receive `*args`).
    num_processes (`int`, *optional*):
        The number of processes to use for training. Will default to 8 in Colab/Kaggle if a TPU is available, to
        the number of devices available otherwise.
    mixed_precision (`str`, *optional*, defaults to `"no"`):
        If `fp16` or `bf16`, will use mixed precision training on multi-device.
    use_port (`str`, *optional*, defaults to `"29500"`):
        The port to use to communicate between processes when launching a multi-device training.
    master_addr (`str`, *optional*, defaults to `"127.0.0.1"`):
        The address to use for communication between processes.
    node_rank (`int`, *optional*, defaults to 0):
        The rank of the current node.
    num_nodes (`int`, *optional*, defaults to 1):
        The number of nodes to use for training.
    rdzv_backend (`str`, *optional*, defaults to `"static"`):
        The rendezvous method to use, such as 'static' (the default) or 'c10d'
    rdzv_endpoint (`str`, *optional*, defaults to `""`):
        The endpoint of the rdzv sync. storage.
    rdzv_conf (`Dict`, *optional*, defaults to `None`):
        Additional rendezvous configuration.
    rdzv_id (`str`, *optional*, defaults to `"none"`):
        The unique run id of the job.
    max_restarts (`int`, *optional*, defaults to 0):
        The maximum amount of restarts that elastic agent will conduct on workers before failure.
    monitor_interval (`float`, *optional*, defaults to 0.1):
        The interval in seconds that is used by the elastic_agent as a period of monitoring workers.
    log_line_prefix_template (`str`, *optional*, defaults to `None`):
        The prefix template for elastic launch logging. Available from PyTorch 2.2.0.

Example:

```python
# Assume this is defined in a Jupyter Notebook on an instance with two devices
from accelerate import notebook_launcher


def train(*args):
    # Your training function here
    ...


notebook_launcher(train, args=(arg1, arg2), num_processes=2, mixed_precision="fp16")
```
Fc              3   B   #    U  H  oR                  S 5      v   M     g7f)KAGGLEN)
startswith).0keys     r   	<genexpr>$notebook_launcher.<locals>.<genexpr>{   s     
A/@>>(##/@s   TIPythonzgoogle.colabzUnknown mixed_precision mode: z. Choose between .TPU_NAMENPJRT_DEVICE TPUr   zTo train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside your training function. Restart your notebook and make sure no cells initializes an `Accelerator`.XLA)distributed_typez"Launching a training on TPU cores.fork)argsstart_methodr      zLaunching training on one GPU.zLaunching training on one CPU.zfYou have to specify the number of devices you would like to use, add `num_processes=...` to your call.z4The node_rank must be less than the number of nodes.)LaunchConfigelastic_launchstart_processes)ProcessRaisedExceptionzTo launch a multi-device training from your notebook, the `Accelerator` should only be initialized inside your training function. Restart your notebook and make sure no cells initializes an `Accelerator`.bitsandbyteszCould not start distributed process. Libraries known to initialize device upon import have been imported already. Please keep these imports inside your training function to try and help with this:z
	* ``)nproc	node_rank
world_sizemaster_addrmaster_portmixed_precision1nccl_p2p_disablenccl_ib_disableacceleratorcudaxpu	MULTI_XPU	MULTI_GPUACCELERATE_DEBUG_MODEfalsetrue r%   nprocsr&   zQAn issue was found when verifying a stable environment for the notebook launcher.zCannot re-initialize z in forked subprocesszThis likely stems from an outside import causing issues once the `notebook_launcher()` is called. Please review your imports and test them when running the `notebook_launcher()` to identify which one is problematic and causing z to be initialized.z! The following error was raised: zLaunching training on  zs.staticrank:)
	min_nodes	max_nodesnproc_per_noderun_idrdzv_endpointrdzv_backendrdzv_configsmax_restartsmonitor_intervalr&   z>=log_line_prefix_template)config
entrypointa:   has been initialized before the `notebook_launcher` could create a forked subprocess. This likely stems from an outside import causing issues once the `notebook_launcher()` is called. Please review your imports and test them when running the `notebook_launcher()` to identify which one is problematic and causing z0An issue was found when launching the training: PYTORCH_ENABLE_MPS_FALLBACKzLaunching training on MPS.zLaunching training on one XPU.zLaunching training on CPU.)2anyosenvironkeyssysmodulesstrget_ipythonr   lower
ValueErrorr4   listget)torch_xla.distributed.xla_multiprocessingdistributedxla_multiprocessinglenr   _shared_stater   printspawnr
   torchr9   is_availabletorch.distributed.launcher.apir(   r)   torch.multiprocessingr+   torch.multiprocessing.spawnr,   r   RuntimeErrordictr	   r   hasattrr8   current_acceleratortyper   upperr%   r   r   r   r:   )functionr%   num_processesr4   use_portr2   r0   	num_nodesrL   rK   	rdzv_confrdzv_idrN   rO   rP   in_colab	in_kagglexmplauncherr(   r)   r+   r,   problematic_importserrlib_namepatched_envdevice_typer#   elaunch_config_kwargss                                  r   notebook_launcherr   (   sz   b HI

Arzz/@
AAA		ckk	!!SY)?)K)K)M%NN
'(=(=(?@ 	I	
D	)	52::>>-Y[;\`e;e 	@?--.2!  $HuE23		(F	;	lnQ'!+::""$$2323$ x  !STT1S=J#112Q6 %  #<N"K&'!+{  !4HWXJa00C !4"3''##$4'$ /K -..25./14-. #1[1NUV[]jNkNke//CCEJJqw2=2F;K ::>>"97CIIKvU/N^_Hd'r-^de ,HGWX.}oQ{?P?P?R>SSUVWj ($&	#x/,5&),/:m1XJ,GM+/"+"+'4&&3%1%.%1)9%+,( (.^__Kc,-GH",*N9M*N[cdfjkW 21r  !!<?

8923((**67''))6723dOY  
,T-A-A-G-G-I,JJ[\i\n\n\p[qqrs
 	

d 2 
dq2;3D3D3F2GG\]abagaghiajj".#&%!HHSHYHYH[G\\o!q#
 $%% #/#6WXYWZ/["\bcc
dH . 	j.{/@/@/B.CCXY]^]c]cde]ff**0023 4D EPDUDUDWCXXkm
  !! +-]^_]`+abhii	jY 21s_   =P& 'B
U2	Q1;*U&AS&AQ.1S7ASSUUA"T??UU
Uc           
          SSK Jn  [        R                  " 5        n[	        USSSUR
                  SS9   [        U SS	9nU" XQUS
S9  SSS5        SSS5        g! , (       d  f       N= f! , (       d  f       g= f)a  
Launches a training function using several processes on CPU for debugging purposes.

<Tip warning={true}>

This function is provided for internal testing and debugging, but it's not intended for real trainings. It will
only use the CPU.

</Tip>

Args:
    function (`Callable`):
        The training function to execute.
    args (`Tuple`):
        Tuple of arguments to pass to the function (it will receive `*args`).
    num_processes (`int`, *optional*, defaults to 2):
        The number of processes to use for training.
r   r*   	127.0.0.129500noyes)r1   r2   r3   accelerate_mixed_precisionaccelerate_debug_rdv_fileaccelerate_use_cpuT)debugr$   rA   N)rj   r+   tempfileNamedTemporaryFiler   namer   )rr   r%   rs   r+   tmp_filer{   s         r   debug_launcherr     sq    & 6		$	$	&( $#'+&.mm$
 (=HHTZ[
 
'	&
 
 
'	&s"   A*AA*
A'	#A**
A8)r@   Nr   r   r   r   r   rD   r    Nnoner   g?N)r@   r'   )rU   rX   r   rg   stater   r   utilsr   r   r   r	   r
   r   r   r   utils.constantsr   r   r   r   r@   r   r   <module>r      si    
 
   1	 	 	 N 
!fR!\r   