
      i                         d dl Z d dlmZ d dlmZmZ d dlmZmZm	Z	 d dl
Z
d dlm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 d d	lmZ d d
lmZ d dlmZmZmZ d dlm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z( d dl)m*Z* d dl+m,Z, de	e   ddfdZ-de	e.   de.de	e.   de	e.   ddde/e.e.f   fdZ0ed0de&de1ded   fd        Z2d1d!e.d"e.de1fd#Z3d$eddfd%Z4ddd&e(defd'Z5d(edefd)Z6d*e/e7d+f   d,e1d&e(d-d.ddf
d/Z8y)2    N)	Generator)AbstractContextManagercontextmanager)AnyCallableOptional)Tensor)_distributed_is_initialized)PossibleUserWarning)XLAAccelerator)Timer)_Loop)_DataFetcher_DataLoaderIterDataFetcher_PrefetchDataFetcher)_BaseProgress)FSDPStrategy)ParallelStrategy)Strategy)RunningStage)rank_zero_warn)is_param_in_hook_signaturelossreturnc                 n    | 3t        j                  |       j                         st        d|  d      yy)zaChecks for finite loss value.

    Args:
        loss: the loss value to check to be finite

    Nz(The loss returned in `training_step` is .)torchisfiniteall
ValueError)r   s    v/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/pytorch_lightning/loops/utilities.pycheck_finite_lossr"   '   s;     t 4 8 8 :CD6KLL !;    	min_steps	max_steps
min_epochs
max_epochstrainerz
pl.Trainerc                     |7|dk(  r0t        d |j                  D              st        dt               d}nd}|| d}|d}||fS )a  This utility computes the default values for the minimum and maximum number of steps and epochs given the values
    the user has selected.

    Args:
        min_steps: Minimum number of steps.
        max_steps: Maximum number of steps.
        min_epochs: Minimum number of epochs.
        max_epochs: Maximum number of epochs.
        trainer: Trainer instance.

    Returns:
        The parsed limits, with default values being set for the ones that the user did not specify.

    c              3   <   K   | ]  }t        |t                y wN)
isinstancer   ).0cbs     r!   	<genexpr>z%_parse_loop_limits.<locals>.<genexpr>H   s     &YGXz"e'<GXs   zj`max_epochs` was not set. Setting it to 1000 epochs. To train without an epoch limit, set `max_epochs=-1`.)categoryi     r   )any	callbacksr   r   )r$   r%   r&   r'   r(   s        r!   _parse_loop_limitsr5   2   sf    * ?3&YwGXGX&Y#Y(,
 JJi3

z!!r#   strategyblock)NNNc              #      K   t        | t              r |r| j                         5  d ddd       yd y# 1 sw Y   yxY ww)a  Blocks synchronization in :class:`~pytorch_lightning.strategies.parallel.ParallelStrategy`. This is useful for
    example when accumulating gradients to reduce communication when it is not needed.

    Args:
        strategy: the strategy instance to use.
        block: whether the context manager is enabled or not

    Returns:
        context manager with sync behaviour off

    N)r-   r   block_backward_sync)r6   r7   s     r!   _block_parallel_sync_behaviorr:   ]   s=      (,-%))+J ,+ 
 ,+s   #A8AAAcurrentmaximumc                     |dk7  xr | |k\  S )zCheck if the limit has been reached (if enabled).

    Args:
        current: the current value
        maximum: the maximum value (or -1 to disable limit)

    Returns:
        bool: whether the limit has been reached

    r*    )r;   r<   s     r!   _is_max_limit_reachedr?   q   s     b=/W//r#   loopc                     t        |       j                         D ]?  }t        |t              r|j	                          $t        |t
              s5t        |       A y r,   )varsvaluesr-   r   resetr   _reset_progress)r@   vs     r!   rE   rE      s>    $Z a'GGI5!A	 !r#   stagec                    | j                   }|t        j                  k(  rd}ns|t        j                  k(  rd}n]|t        j                  t        j
                  fv rd}n8|t        j                  k(  rd}n"t        d| j                  j                         t        ||      }t        |dd      rt        d	| d
       t               S t               S )N	test_steptraining_stepvalidation_steppredict_stepzDataFetcher is unsupported for dataloader_iterT)explicitz)Found `dataloader_iter` argument in the `zb`. Note that the support for this signature is experimental and the behavior is subject to change.)lightning_moduler   TESTINGTRAINING
VALIDATINGSANITY_CHECKING
PREDICTINGRuntimeErrorstaterG   getattrr   r   r   r   )r(   rG   rO   step_fx_namestep_fxs        r!   _select_data_fetcherrZ      s    //$$$"	,''	'&	<**L,H,HI	I(	,))	)%<W]]=P=P<QRSS&5G!'+<tL7~ FT T	
 *++!!r#   loop_runc                 D     dt         dt        dt        dt        f fd}|S )Nselfargskwargsr   c                    t        | t              s"t        dt        |       j                   d      t        | d      s"t        dt        |       j                   d      t               r(t        j                         dk(  rt        j                  }nt        | j                  j                  t              rt        j                  }nbt        | j                  j                  t              rt        j                  }n-| j                   rt        j                   }nt        j                  } |       5   | g|i |cd d d        S # 1 sw Y   y xY w)N`z` needs to be a Loop.inference_modez$.inference_mode` needs to be definedgloo)r-   r   	TypeErrortype__name__hasattrr
   distget_backendr   no_gradr(   acceleratorr   r6   r   rb   )r]   r^   r_   context_managerr[   s       r!   
_decoratorz$_no_grad_context.<locals>._decorator   s    $&aT
 3 344IJKKt-.aT
 3 344XYZZ&(T-=-=-?6-I $mmO00.A#mmO--|<#mmO  #22O#mmOD24262 s   2EE)r   r   )r[   rm   s   ` r!   _no_grad_contextrn      s,    3 3s 3c 3c 3. r#   hooks.is_expected	pl_modulezpl.LightningModulec           
         | D ]  }t        ||      }t        |d      }|s|s t        j                  |      j                  }d|v sD|d   j
                  t        j                  j                  u snt        d|j                   dt        |      j                   d| d      |rt        d|j                   dt        |      j                   d| d       y )	Ndataloader_idxzYou provided only a single `z5_dataloader`, but have included `dataloader_idx` in `r   zS()`. Either remove the argument or give it a default value i.e. `dataloader_idx=0`.zYou provided multiple `z3_dataloader`, but no `dataloader_idx` argument in `z4()`. Try adding `dataloader_idx=0` to its signature.)rW   r   inspect	signature
parametersdefault	ParameteremptyrU   dataloader_prefixre   rf   )ro   rp   rG   rq   hookfxparam_presentparamss           r!   "_verify_dataloader_idx_requirementr      s     Y%227GH **2.99#v-&9I2J2R2RV]VgVgVmVm2m&6u7N7N6O P004Y0H0H/I4& QXX 
 )%*A*A)B C!!%i!9!9 :!D6 B  r#   )T)r*   )9rt   collections.abcr   
contextlibr   r   typingr   r   r   r   torch.distributeddistributedrh   r	   pytorch_lightningpl&lightning_fabric.utilities.distributedr
   #lightning_fabric.utilities.warningsr   "pytorch_lightning.accelerators.xlar   !pytorch_lightning.callbacks.timerr   pytorch_lightning.loopsr    pytorch_lightning.loops.fetchersr   r   r    pytorch_lightning.loops.progressr   pytorch_lightning.strategiesr   %pytorch_lightning.strategies.parallelr   %pytorch_lightning.strategies.strategyr    pytorch_lightning.trainer.statesr   %pytorch_lightning.utilities.rank_zeror   +pytorch_lightning.utilities.signature_utilsr   r"   inttupler5   boolr:   r?   rE   rZ   rn   strr   r>   r#   r!   <module>r      sw    % = * *      N C = 3 ) k k : 5 B : 9 @ RMHV, M M("}("(" (" 	("
 (" 38_("V H T YWgMh  &03 0 0d 0% D ", "| " ",x H 6c?)-6BOc	r#   