
      i	d                        d dl Z d dlmZmZ d dlmZmZ d dlmZ d dl	m
Z
mZmZmZ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 d dlmZ d dlmZ d dl m!Z! d dl"m#Z#m$Z$ d dl%m&Z& d dl'm(Z(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5m6Z6  ed      Z7 ed      Z8 e jr                  e:      Z; G d de      Z< G d d      Z=y)    N)ABCabstractmethod)	GeneratorMapping)contextmanager)AnyCallableOptionalTypeVarUnion)Tensor)Module)	Optimizer)CheckpointIO)_StrategyRegistry)move_data_to_device)ReduceOp)
_EmptyInit)_optimizer_to_device_optimizers_to_device)_PATH)LightningOptimizer"_init_optimizers_and_lr_schedulers)TorchCheckpointIO)_WrappingCheckpointIO)	Precision)	_Launcher)	TrainerFn)STEP_OUTPUTLRSchedulerConfig
TBroadcastTReducec                      e Zd ZdZ	 	 	 dYded   dee   dee   ddfdZedee	   fd	       Z
eded   fd
       Zej                  dod       Zedefd       Zej                  deddfd       Zedefd       Zej                  dee   ddfd       Zedee   fd       Zej                  dee   ddfd       ZdpdZdqdZdqdZdrdZdrdZdqdZdedeeef   fdZdedee   ded edef
d!Z	 dsded"eg ef   deede f      d edef
d#Z!de dee   de"e ee   f   fd$Z#de de fd%Z$dedefd&Z%dtd'ed(ee&jN                     d)e(defd*Z)ee*de&jN                  fd+              Z+e*dqd,       Z,ee*de-fd-              Z.e*	 	 dud.eeef   d/ee   d0eee/ef      deeef   fd1       Z0e*dsd2ee   ddfd3       Z1e*dvd4e2d5e(de2fd6       Z3e*dwd.ed/ee   d7e-defd8       Z4dxd9e-d:e-de-fd;Z5deddfd<Z6deddfd=Z7edee    fd>       Z8e8j                  d?ee    ddfd@       Z8eded   fdA       Z9dsdBe:dCee-   deeef   fdDZ;dxdEe<eef   dFe-ddfdGZ=dEe<eef   ddfdHZ>ded ede?fdIZ@dqdJZAded ede?fdKZBded ede?fdLZCded edefdMZDdNeEdeEfdOZFede-fdP       ZGede-fdQ       ZHede-fdR       ZIdeeef   fdSZJ	 dsdEeeef   dTe:dUee   ddfdVZKdTe:ddfdWZLeMdsdXee-   deNdY   fdZ       ZOeMdeNdY   fd[       ZPdqd\ZQeRd]eSddfd^       ZTdqd_ZUdqd`ZVdqdaZWdqdbZXdqdcZYdqddZZdqdeZ[dqdfZ\d'edge(ddfdhZ]die^ddfdjZ_dqdkZ`defdlZadmeddfdnZby)yStrategyzcBase class for all strategies that change the behaviour of the training, validation and test- loop.Nacceleratorpl.accelerators.Acceleratorcheckpoint_ioprecision_pluginreturnc                     || _         || _        d | _        || _        d | _        d | _        d | _        t               | _        g | _	        g | _
        g | _        y N)_accelerator_checkpoint_io_precision_pluginr(   _lightning_module_model	_launcher_ForwardRedirection_forward_redirection_optimizers_lightning_optimizerslr_scheduler_configs)selfr%   r'   r(   s       z/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning/pytorch/strategies/strategy.py__init__zStrategy.__init__2   s_     DO6C6: 0?C(,.29L9N!,.?A"=?!    c                     | j                   S r+   )r1   r7   s    r8   launcherzStrategy.launcherE   s    ~~r:   c                     | j                   S r+   r,   r<   s    r8   r%   zStrategy.acceleratorI   s       r:   c                     || _         y r+   r?   )r7   r%   s     r8   r%   zStrategy.acceleratorM   s
    'r:   c                     | j                   t               | _         | j                   S t        | j                   t              rt               | j                   _        | j                   S r+   )r-   r   
isinstancer   r'   r<   s    r8   r'   zStrategy.checkpoint_ioQ   sW    &"3"5D """ ++-BC0A0CD-"""r:   ioc                     || _         y r+   )r-   )r7   rC   s     r8   r'   zStrategy.checkpoint_ioZ   s
     r:   c                 F    | j                   | j                   S t               S r+   )r.   r   r<   s    r8   r(   zStrategy.precision_plugin^   s"    )-)?)?)Kt%%\QZQ\\r:   c                     || _         y r+   )r.   )r7   r(   s     r8   r(   zStrategy.precision_pluginb   s
    !1r:   c                     | j                   S r+   )r4   r<   s    r8   
optimizerszStrategy.optimizersf   s    r:   rH   c                 n    || _         |D cg c]  }t        j                  ||        c}| _        y c c}w r+   )r4   r   _to_lightning_optimizerr5   )r7   rH   opts      r8   rH   zStrategy.optimizersj   s7    %gq%rgq`c&8&P&PQTVZ&[gq%r"%rs   2modelpl.LightningModulec                      || _         || _        y)z=Called by the Trainer to connect the strategy with the model.N)r/   rL   r7   rL   s     r8   connectzStrategy.connecto   s     "'
r:   c                      y)z&Attach the launcher based on Strategy.N r<   s    r8   _configure_launcherzStrategy._configure_launcherv       r:   c                 j    | j                   J | j                   j                  | j                         y)zSetup any processes or distributed connections.

        This is called before the LightningModule/DataModule setup hook which allows the user to access the accelerator
        environment before setup is complete.

        N)r%   setup_deviceroot_devicer<   s    r8   setup_environmentzStrategy.setup_environmenty   s0     +++%%d&6&67r:   c                 d    | j                   J t        | j                         \  | _        | _        y)zCreates optimizers and schedulers.

        Args:
            trainer: the Trainer, these optimizers should be connected to

        N)lightning_moduler   rH   r6   r7   trainers     r8   setup_optimizerszStrategy.setup_optimizers   s1     $$0005WX\XmXm5n22r:   c                 F   | j                   J | j                   j                  |       | j                  J | j                  j	                  | j                        | _        | j                          | j                  | j                        | _        |j                  j                  t        j                  k(  r| j                  |       | j                          |j                  j                  t        j                  k(  r!t        | j                  | j                         yy)zSets up the accelerator, plugins and initializes the optimizers (if needed).

        Args:
            trainer: the trainer instance

        N)r%   setuprL   r(   convert_modulemodel_to_device_setup_modelstatefnr   FITTINGr]   setup_precision_pluginr   rH   rW   r[   s     r8   r_   zStrategy.setup   s     +++w'zz%%% **99$**E
&&tzz2
==y000!!'*##%==y000!$//43C3CD 1r:   c                     | j                   J | j                  j                  | j                   | j                  | j                        \  }}}|| _         || _        || _        y)z.Attaches the precision plugin to the strategy.N)rL   r(   rP   rH   r6   )r7   rL   rH   r6   s       r8   rf   zStrategy.setup_precision_plugin   s]    zz%%%262G2G2O2OJJ)B)B3
/z/ 
$$8!r:   	optimizerc                     t        |t              r|j                  }t        |d      r.|j	                          | j
                  r|j                         S i S |j                         S )zReturns state of an optimizer.

        Allows for syncing/collating optimizer state from processes in custom strategies.

        consolidate_state_dict)rB   r   
_optimizerhasattrrj   is_global_zero
state_dictr7   rh   s     r8   optimizer_statezStrategy.optimizer_state   s`     i!34!,,I967 ,,.-1-@-@9'')HbH ##%%r:   closure_lossargskwargsc                 V   | j                  |       | j                  J | j                  j                  || j                        } | j                  j                  || j                  |g|i | | j                  j	                  || j                        }| j	                  |       |S )a'  Forwards backward-calls to the precision plugin.

        Args:
            closure_loss: a tensor holding the loss value to backpropagate
            optimizer: An optional optimizer that gets passed down to the precision plugin's backward
            \*args: Positional arguments that get passed down to the precision plugin's backward, intended as arguments
                for the actual function that performs the backward, like :meth:`~torch.Tensor.backward`.
            \**kwargs: Keyword arguments for the same purpose as ``*args``.

        )pre_backwardrZ   r(   backwardpost_backward)r7   rq   rh   rr   rs   s        r8   rv   zStrategy.backward   s    " 	,'$$000,,99,H]H]^&&&|T5J5JIgX\g`fg,,::<I^I^_<(r:   closurec                     |xs | j                   }t        |t        j                        sJ  | j                  j
                  |f||d|S )aH  Performs the actual optimizer step.

        Args:
            optimizer: the optimizer performing the step
            closure: closure calculating the loss value
            model: reference to the model, optionally defining optimizer step related hooks
            \**kwargs: Keyword arguments to ``optimizer.step``

        )rL   rx   )rZ   rB   plLightningModuler(   optimizer_step)r7   rh   rx   rL   rs   s        r8   r|   zStrategy.optimizer_step   sQ      ...%!3!34443t$$33IfUT[f_effr:   c                 r    | j                  |      }|D cg c]  }| j                  |       }}||fS c c}w )zSetup a model and multiple optimizers together.

        The returned objects are expected to be in the same order they were passed in. The default implementation will
        call :meth:`_setup_model` and :meth:`_setup_optimizer` on the inputs.

        )rb   _setup_optimizer)r7   rL   rH   rh   s       r8   _setup_model_and_optimizersz$Strategy._setup_model_and_optimizers   sD     !!%(HRS
9d++I6

Sj   Ts   4c                     |S )zDPerforms setup for the model, e.g., by wrapping it by another class.rR   rO   s     r8   rb   zStrategy._setup_model   s	     r:   c                     |S )zHPerforms setup for the optimizer, e.g., by wrapping it by another class.rR   ro   s     r8   r~   zStrategy._setup_optimizer  s
     r:   batchdevicedataloader_idxc                 ~    | j                   }|xs | j                  }||j                  |||      S t        ||      S )az  Moves the batch to the correct device.

        The returned batch is of the same type as the input batch, just
        having all tensors on the correct device.

        Args:
            batch: The batch of samples to move to the correct device
            device: The target device
            dataloader_idx: The index of the dataloader to which the batch belongs.

        )r   r   )rZ   rW   _apply_batch_transfer_handlerr   )r7   r   r   r   rL   s        r8   batch_to_devicezStrategy.batch_to_device  sK     %%+4++66uV\j6kk"5&11r:   c                      y)zReturns the root device.NrR   r<   s    r8   rW   zStrategy.root_device  rT   r:   c                      y)z&Moves the model to the correct device.NrR   r<   s    r8   ra   zStrategy.model_to_device  rT   r:   c                      y)zcWhether the current process is the rank zero process not only on the local node, but for all nodes.NrR   r<   s    r8   rm   zStrategy.is_global_zero"  rT   r:   tensorgroup	reduce_opc                      y)a,  Reduces the given tensor (e.g. across GPUs/processes).

        Args:
            tensor: the tensor to sync and reduce
            group: the process group to reduce
            reduce_op: the reduction operation. Defaults to 'mean'.
                Can also be a string 'sum' or ReduceOp.

        NrR   )r7   r   r   r   s       r8   reducezStrategy.reduce'  rT   r:   namec                      y)zSynchronizes all processes which blocks processes until the whole group enters this function.

        Args:
            name: an optional name to pass into barrier.

        NrR   )r7   r   s     r8   barrierzStrategy.barrier8  rT   r:   objsrcc                      y)zBroadcasts an object to all processes.

        Args:
            obj: the object to broadcast
            src: source rank

        NrR   )r7   r   r   s      r8   	broadcastzStrategy.broadcastA  rT   r:   
sync_gradsc                      y)a  Perform an all_gather on all processes.

        Args:
            tensor: the tensor to all_gather
            group: the process group to gather results from
            sync_grads: flag that allows users to synchronize gradients for all_gather op

        NrR   )r7   r   r   r   s       r8   
all_gatherzStrategy.all_gatherK  rT   r:   decisionallc                     |S )z/Reduce a boolean decision across all processes.rR   )r7   r   r   s      r8   reduce_boolean_decisionz Strategy.reduce_boolean_decisionV  s    r:   c                      y)z.Run before precision plugin executes backward.NrR   r7   rq   s     r8   ru   zStrategy.pre_backwardZ  rT   r:   c                      y)z-Run after precision plugin executes backward.NrR   r   s     r8   rw   zStrategy.post_backward]  rT   r:   c                 J    | j                   | j                   S | j                  S )z0Returns the potentially wrapped LightningModule.)r0   r/   r<   s    r8   rL   zStrategy.model`  s"     #kk5t{{Q4;Q;QQr:   	new_modelc                     || _         y r+   )r0   )r7   r   s     r8   rL   zStrategy.modele  s	    r:   c                     | j                   S )z<Returns the pure LightningModule without potential wrappers.)r/   r<   s    r8   rZ   zStrategy.lightning_modulei  s     %%%r:   checkpoint_pathweights_onlyc                 x    t         j                  j                          | j                  j	                  ||      S )N)r   )torchcudaempty_cacher'   load_checkpoint)r7   r   r   s      r8   r   zStrategy.load_checkpointn  s/    

 !!11/P\1]]r:   
checkpointstrictc                 `    | j                   J | j                   j                  |d   |       y )Nrn   )r   )rZ   load_state_dict)r7   r   r   s      r8   load_model_state_dictzStrategy.load_model_state_dictr  s2    $$000--j.Fv-Vr:   c                     |d   }t        | j                  |      D ],  \  }}|j                  |       t        || j                         . y )Noptimizer_states)ziprH   r   r   rW   )r7   r   r   rh   	opt_states        r8   load_optimizer_state_dictz"Strategy.load_optimizer_state_dictv  sH    %&89$'9I$J Iy%%i0 D,<,<= %Kr:   c                 n   | j                   J | j                  J | j                  j                         5  | j                  | j                   k7  r5 | j                  | j                  | j                   dg|i |cddd       S  | j                   j
                  |i |cddd       S # 1 sw Y   yxY w)zThe actual training step.

        See :meth:`~lightning.pytorch.core.LightningModule.training_step` for more details

        Ntraining_step)rZ   rL   r(   train_step_contextr3   r   r7   rr   rs   s      r8   r   zStrategy.training_step|  s     $$000zz%%%""557zzT2220t00T=R=RTcufjuntu 87 74((66GG 877   AB+B++B4c                      y)zSThis hook is deprecated.

        Override :meth:`training_step` instead.

        NrR   r<   s    r8   post_training_stepzStrategy.post_training_step  s     	r:   c                 n   | j                   J | j                  J | j                  j                         5  | j                  | j                   k7  r5 | j                  | j                  | j                   dg|i |cddd       S  | j                   j
                  |i |cddd       S # 1 sw Y   yxY w)zThe actual validation step.

        See :meth:`~lightning.pytorch.core.LightningModule.validation_step` for more details

        Nvalidation_step)rZ   rL   r(   val_step_contextr3   r   r   s      r8   r   zStrategy.validation_step  s     $$000zz%%%""335zzT2220t00T=R=RTewhlwpvw 65 94((88$I&I 655r   c                 n   | j                   J | j                  J | j                  j                         5  | j                  | j                   k7  r5 | j                  | j                  | j                   dg|i |cddd       S  | j                   j
                  |i |cddd       S # 1 sw Y   yxY w)zwThe actual test step.

        See :meth:`~lightning.pytorch.core.LightningModule.test_step` for more details

        N	test_step)rZ   rL   r(   test_step_contextr3   r   r   s      r8   r   zStrategy.test_step  s     $$000zz%%%""446zzT2220t00T=R=RT_qbfqjpq 76 34((22DCFC 766r   c                 n   | j                   J | j                  J | j                  j                         5  | j                  | j                   k7  r5 | j                  | j                  | j                   dg|i |cddd       S  | j                   j
                  |i |cddd       S # 1 sw Y   yxY w)z}The actual predict step.

        See :meth:`~lightning.pytorch.core.LightningModule.predict_step` for more details

        Npredict_step)rZ   rL   r(   predict_step_contextr3   r   r   s      r8   r   zStrategy.predict_step  s     $$000zz%%%""779zzT2220t00T=R=RTbteitmst :9 64((55tFvF :99r   
dataloaderc                     |S )zWraps the dataloader if necessary.

        Args:
            dataloader: iterable. Ideally of type: :class:`torch.utils.data.DataLoader`

        rR   )r7   r   s     r8   process_dataloaderzStrategy.process_dataloader  s
     r:   c                      y)a  Override to delay restoring from checkpoint till after the setup phase has completed. This is useful when
        the strategy requires all the setup hooks to run before loading checkpoint.

        Returns:
            If ``True``, restore checkpoint after strategy setup.

        FrR   r<   s    r8   restore_checkpoint_after_setupz'Strategy.restore_checkpoint_after_setup  s     r:   c                      y)zOverride to disable Lightning restoring optimizers/schedulers.

        This is useful for strategies which manage restoring optimizers/schedulers.

        TrR   r<   s    r8   lightning_restore_optimizerz$Strategy.lightning_restore_optimizer  s     r:   c                      y)z>Whether the strategy handles gradient accumulation internally.FrR   r<   s    r8   handles_gradient_accumulationz&Strategy.handles_gradient_accumulation  s     r:   c                 R    | j                   J | j                   j                         S )zReturns model state.)rZ   rn   r<   s    r8   lightning_module_state_dictz$Strategy.lightning_module_state_dict  s)    $$000$$//11r:   filepathstorage_optionsc                 Z    | j                   r| j                  j                  |||       yy)a?  Save model/training states as a checkpoint file through state-dump and file-write.

        Args:
            checkpoint: dict containing model and trainer state
            filepath: write-target file's path
            storage_options: parameter for how to save to storage, passed to ``CheckpointIO`` plugin

        )r   N)rm   r'   save_checkpoint)r7   r   r   r   s       r8   r   zStrategy.save_checkpoint  s.     ..z8Ud.e r:   c                 T    | j                   r| j                  j                  |       yy)zqRemove checkpoint filepath from the filesystem.

        Args:
            filepath: Path to checkpoint

        N)rm   r'   remove_checkpoint)r7   r   s     r8   r   zStrategy.remove_checkpoint  s&     00: r:   
empty_init)NNNc              #     K   t        t        |            }|5  | j                  5  | j                  j	                         5  d ddd       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   yxY ww)a  Controls how tensors get created (device, dtype).

        Args:
            empty_init: Whether to initialize the model with empty weights (uninitialized memory).
                If ``None``, the strategy will decide. Some strategies may not support all options.

        )enabledN)r   boolrW   r(   tensor_init_context)r7   r   empty_init_contexts      r8   r   zStrategy.tensor_init_context  sZ      (Z0@A!1!143H3H3\3\3^ 4_!13^3^!1!1sK   BA8A,A A,A8	B A)%A,,A5	1A88B=Bc              #      K   d yw)a  Provide hook to create modules in a distributed aware context. This is useful for when we'd like to shard
        the model instantly, which is useful for extremely large models which can save memory and initialization time.

        Returns: Model parallel context.

        NrR   r<   s    r8   model_sharded_contextzStrategy.model_sharded_context  s      	s   c                    t        | j                  t        j                  d             | j                  Ft
        j                  | j                  j                   d       | j                  j                          | j                  j                          | j                  J | j                  j                          | j                  j                          y)zThis method is called to teardown the training process.

        It is the right place to release memory and free other resources.

        cpuNz: moving model to CPU)r   rH   r   r   rZ   logdebug	__class____name__r   r(   teardownr%   r'   r<   s    r8   r   zStrategy.teardown  s     	doou||E/BC  ,II0011FGH!!%%'&&(+++!!###%r:   strategy_registryc                      y r+   rR   )clsr   s     r8   register_strategieszStrategy.register_strategies      r:   c                      y)zCalled when train begins.NrR   r<   s    r8   on_train_startzStrategy.on_train_start"  r   r:   c                      y)zCalled when validation begins.NrR   r<   s    r8   on_validation_startzStrategy.on_validation_start&  r   r:   c                      y)zCalled when test begins.NrR   r<   s    r8   on_test_startzStrategy.on_test_start*  r   r:   c                      y)zCalled when predict begins.NrR   r<   s    r8   on_predict_startzStrategy.on_predict_start.  r   r:   c                      y)zCalled when train ends.NrR   r<   s    r8   on_train_endzStrategy.on_train_end2  r   r:   c                      y)zCalled when validation ends.NrR   r<   s    r8   on_validation_endzStrategy.on_validation_end6  r   r:   c                      y)zCalled when test end.NrR   r<   s    r8   on_test_endzStrategy.on_test_end:  r   r:   c                      y)zCalled when predict ends.NrR   r<   s    r8   on_predict_endzStrategy.on_predict_end>  r   r:   	batch_idxc                      y)zCCalled in the training loop before anything happens for that batch.NrR   )r7   r   r   s      r8   on_train_batch_startzStrategy.on_train_batch_startB  r   r:   	exceptionc                      y)zACalled when the trainer execution is interrupted by an exception.NrR   )r7   r   s     r8   on_exceptionzStrategy.on_exceptionF  r   r:   c                 .    g | _         g | _        g | _        y r+   )r4   r5   r6   r<   s    r8    _reset_optimizers_and_schedulersz)Strategy._reset_optimizers_and_schedulersJ  s    %'"$&!r:   c                 8    t        t        |             }g |d<   |S )Nr5   )dictvarsr7   rc   s     r8   __getstate__zStrategy.__getstate__O  s     T$Z )+%&r:   rc   c                 4    || _         | j                  | _        y r+   )__dict__rH   r  s     r8   __setstate__zStrategy.__setstate__U  s    //r:   )r%   r&   r)   N)rL   rM   r)   N)r)   N)r\   z
pl.Trainerr)   Nr+   )Nr   )Nmean)r   )NF)T)cr   
__module____qualname____doc__r
   r   r   r9   propertyr   r=   r%   setterr'   r(   listr   rH   rP   rS   rX   r]   r_   rf   r  strr   rp   r   rv   r	   r   r   r|   tupler   rb   r~   r   r   intr   r   rW   ra   r   rm   r   r   r   r!   r   r   r   ru   rw   rL   rZ   r   r   r   r   r   r   r   r   r   r   r   objectr   r   r   r   r   r   r   r   r   r   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   BaseExceptionr   r  r  r
  rR   r:   r8   r$   r$   /   s   m @D0404	@;<@  -@ #9-	@
 
@& (9-   !X&CD ! ! ( ( #| # # ! ! ! ! ]) ] ] 2)1D 2 2 2  DO     sT)_ s s s58oE.9& &tCK7H &$ I& 	
  
@ @D	gg "c'"g 2F:;<	g
 g 
g*
! 
!T)_ 
!Y^_egklugv_vYw 
!& V 
) 	 
2S 2(5<<2H 2ad 2mp 2$ 'U\\ '  ' 5 5 r r  r   $4:	fck" } E(C-01	
 
vs{	   HSM T   Z c *     RV ci   4 4 = =D =<& <T < Rx' R R \\ x/  D     &(+?"@ & &^u ^HTN ^^bcfhkck^l ^WS0A W4 W[_ W>GCH4E >$ >H3 H# H+ HJS JC JK JDs Dc Dk DG# G G GV      T   t  2T#s(^ 2 ]afsCx.f49fLTUXMf	f;% ;D ; 
htn 
	RbHc 
 
 y1A'B  &  4E $  # # $ m  '
d *$ *4 *r:   r$   c                   X    e Zd ZdZdedddedededefd	Zdedddd
fdZ	dedddd
fdZ
y
)r2   zImplements the `forward-redirection`.

    A method call to a wrapped module gets rerouted through the wrapper's `forward` method instead.

    wrapper_moduleoriginal_modulerM   method_namerr   rs   r)   c                      dk7  sJ j                   dt        dt        dt        f fd}|_          |i |} j                         |S )a  Reroutes a method call through the `wrapper_module`'s `forward` method.

        Args:
            wrapper_module: The module that has `original_module` wrapped.
            original_module: The module that was wrapped inside `wrapper_module`.
            method_name: The name of the method that should be called on the `original_module` after inputs get
                redirected through the `wrapper_module`'s `forward` method.
            *args: The positional arguments to the method `method_name`. They will get passed to a patched
                `forward` method instead.
            **kwargs: The keyword arguments to the method `method_name`. They will get passed to a patched
                `forward` method instead.

        forward_args_kwargsr)   c                  b    _         t              } || i |}j                         |S r+   )r  getattron_after_inner_forward)	r  r  methodoutr  original_forwardr  r7   r  s	       r8   wrapped_forwardz5_ForwardRedirection.__call__.<locals>.wrapped_forwardt  s=     '7O#_k:F%+7+C''HJr:   )r  r   on_after_outer_forward)	r7   r  r  r  rr   rs   r&  wrapper_outputr%  s	   ````    @r8   __call__z_ForwardRedirection.__call__a  sm      i'''*22	C 	C 	C 	 	 #2'88##NODr:   Nc                      y r+   rR   r7   r  r  s      r8   r"  z*_ForwardRedirection.on_after_inner_forward      r:   c                      y r+   rR   r+  s      r8   r'  z*_ForwardRedirection.on_after_outer_forward  r,  r:   )r   r  r  r  r   r  r   r   r)  r"  r'  rR   r:   r8   r2   r2   Z  su    "$"7K"Z]"fi"ux"	"HV Nb gk V Nb gk r:   r2   )>loggingabcr   r   collections.abcr   r   
contextlibr   typingr   r	   r
   r   r   r   r   torch.nnr   torch.optimr   lightning.pytorchpytorchrz   lightning.fabric.pluginsr   lightning.fabric.strategiesr   lightning.fabric.utilitiesr   &lightning.fabric.utilities.distributedr   lightning.fabric.utilities.initr   $lightning.fabric.utilities.optimizerr   r    lightning.fabric.utilities.typesr    lightning.pytorch.core.optimizerr   r   lightning.pytorch.pluginsr   $lightning.pytorch.plugins.io.wrapperr   #lightning.pytorch.plugins.precisionr   /lightning.pytorch.strategies.launchers.launcherr    lightning.pytorch.trainer.statesr   !lightning.pytorch.utilities.typesr   r    r!   r"   	getLoggerr   r   r$   r2   rR   r:   r8   <module>rF     s     # . % : :    !  1 9 : ; 6 \ 2 c 7 F 9 E 6 L\"

)
g!h*s h*V/ /r:   