Ë
      ç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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l/m0Z0 d dl1m2Z2 d dl3m4Z4m5Z5  ed«      Z6 ed«      Z7 e jp                  e9«      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/pytorch_lightning/strategies/strategy.pyÚ__init__zStrategy.__init__2   s_   € ð DOˆÔØ6CˆÔØ6:ˆÔà 0ˆÔØ?CˆÔØ(,ˆŒØ.2ˆŒÜ9LÓ9NˆÔ!Ø,.ˆÔØ?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Ó"5ˆDÔð ×"Ñ"Ð"ô ˜×+Ñ+Ô-BÔCÜ0AÓ0CˆD×ÑÔ-à×"Ñ"Ð"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"   € à)-×)?Ñ)?Ð)Kˆt×%Ñ%Ð\ÔQZÓQ\Ð\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Ó%rÑgqÐ`cÔ&8×&PÑ&PÐQTÐVZÕ&[Ð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)zàSetup 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)z…Creates 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Ð0Ü5WÐX\×XmÑXmÓ5nÑ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)z�Sets 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Ô'à�z‰zÐ%Ð%Ð%ð ×*Ñ*×9Ñ9¸$¿*¹*ÓEˆŒ
Ø×ÑÔØ×&Ñ& t§z¡zÓ2ˆŒ
à�=‰=×Ñœy×0Ñ0Ò0Ø×!Ñ! 'Ô*Ø×#Ñ#Ô%Ø�=‰=×Ñœy×0Ñ0Ò0Ü! $§/¡/°4×3CÑ3CÕ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]   € à�z‰zÐ%Ð%Ð%Ø26×2GÑ2G×2OÑ2OØ�J‰J˜Ÿ™¨×)BÑ)Bó3
Ñ/ˆˆzÐ/ð ˆŒ
Ø$ˆŒØ$8ˆÕ!r:   Ú	optimizerc                 óÎ   — t        |t        «      r|j                  }t        |d«      r.|j	                  «        | j
                  r|j                  «       S i S |j                  «       S )zƒReturns 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×'Ñ'Ó)ÐHÀbÐ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]Ó^ˆà&ˆ×Ñ×&Ñ& |°T×5JÑ5JÈIÐgÐX\Ò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Ø3ˆt×$Ñ$×3Ñ3°IÐfÀUÐT[ÑfÐ_eÑfÐfr:   c                 ór   — | j                  |«      }|D �cg c]  }| j                  |«      ‘Œ }}||fS c c}w )zÿSetup 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É
¸9�d×+Ñ+¨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Ñ6°uÀVÐ\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)z¯Synchronizes 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)z…Broadcasts 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"   € ð #Ÿk™kÐ5ˆt�{‰{ÐQ¸4×;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¸Ñ.FÈvÐ-Õ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Ñ ˆI�yØ×%Ñ% 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:`~pytorch_lightning.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Ø�z‰zÐ%Ð%Ð%Ø×"Ñ"×5Ñ5Õ7Ø�z‰z˜T×2Ñ2Ò2Ø0�t×0Ñ0°·±¸T×=RÑ=RÐTcÐuÐfjÒuÐntÑu÷ 8Ñ7ð 7�4×(Ñ(×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)zƒThe actual validation step.

        See :meth:`~pytorch_lightning.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Ø�z‰zÐ%Ð%Ð%Ø×"Ñ"×3Ñ3Õ5Ø�z‰z˜T×2Ñ2Ò2Ø0�t×0Ñ0°·±¸T×=RÑ=RÐTeÐwÐhlÒwÐpvÑw÷ 6Ñ5ð 9�4×(Ñ(×8Ñ8¸$ÐIÀ&ÑI÷ 6×5Ò5ú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)zwThe actual test step.

        See :meth:`~pytorch_lightning.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Ø�z‰zÐ%Ð%Ð%Ø×"Ñ"×4Ñ4Õ6Ø�z‰z˜T×2Ñ2Ò2Ø0�t×0Ñ0°·±¸T×=RÑ=RÐT_ÐqÐbfÒqÐjpÑq÷ 7Ñ6ð 3�4×(Ñ(×2Ñ2°DÐC¸FÑC÷ 7×6Ò6ú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 predict step.

        See :meth:`~pytorch_lightning.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Ø�z‰zÐ%Ð%Ð%Ø×"Ñ"×7Ñ7Õ9Ø�z‰z˜T×2Ñ2Ò2Ø0�t×0Ñ0°·±¸T×=RÑ=RÐTbÐtÐeiÒtÐmsÑt÷ :Ñ9ð 6�4×(Ñ(×5Ñ5°tÐF¸vÑF÷ :×9Ò9úrº   Ú
dataloaderc                 ó   — |S )z“Wraps 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)z�Override 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.   € ð ×ÒØ×Ñ×.Ñ.¨z¸8ÐUdÐ.Õ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   è ø€ ô (´°ZÓ0@ÔAÐÚ ×!1Ó!1°4×3HÑ3H×3\Ñ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)zŒThis 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    € ô 	˜dŸo™o¬u¯|©|¸EÓ/BÔCà× Ñ Ð,Ü�I‰I˜Ÿ™×0Ñ0Ð1Ð1FÐ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Ø04Ø04ñ	@àÐ;Ñ<ð@ð   Ñ-ð@ð # 9Ñ-ð	@ð
 
ó@ð& ð˜( 9Ñ-ò ó ðð ð!˜XÐ&CÑDò !ó ð!ð ×Ñò(ó ð(ð ð#˜|ò #ó ð#ð ×Ñð! ð !°ò !ó ð!ð ð] )ò ]ó ð]ð ×Ñð2°¸)Ñ1Dð 2Èò 2ó ð2ð ð ˜D ™Oò  ó ð ð ×Ñðs T¨)¡_ð s¸ò só ðsóó5ó8óoóEó.9ð&¨ð &°t¸CÀ¸KÑ7Hó &ð$àðð ˜IÑ&ðð ð	ð
 ðð 
óð@ @Dñ	gàðgð ˜"˜c˜'Ñ"ðgð ˜Ð2°FÐ:Ñ;Ñ<ð	gð
 ðgð 
ógð*
!°ð 
!ÀTÈ)Á_ð 
!ÐY^Ð_eÐgkÐluÑgvÐ_vÑYwó 
!ð &ð ¨Vó ð
¨)ð ¸	ó ñ
2 Sð 2°(¸5¿<¹<Ñ2Hð 2Ðadð 2Ðmpó 2ð$ Øð'˜UŸ\™\ò 'ó ó ð'ð ò5ó ð5ð Øðr ò ró ó ðrð ð  $Ø4:ñ	à�f˜c�kÑ"ðð ˜‰}ðð ˜E (¨C -Ñ0Ñ1ð	ð
 
ˆv�sˆ{Ñ	òó ðð  ñ˜H S™Mð °Tò ó ðð ñ˜Zð ¨cð ¸*ò ó ðð ñ ð °¸±ð ÐRVð Ðciò ó ðñ°ð ¸4ð È4ó ð=¨ð =°Dó =ð<¨&ð <°Tó <ð ðR�x Ñ'ò Ró ðRð ‡\�\ð ˜x¨Ñ/ð  °Dò  ó ð ð ð& (Ð+?Ñ"@ò &ó ð&ñ^¨uð ^ÀHÈTÁNð ^Ð^bÐcfÐhkÐckÑ^ló ^ñW°¸¸S¸Ñ0Að WÈ4ð WÐ[_ó Wð>°G¸CÀ¸HÑ4Eð >È$ó >ð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ò ó ðð2¨T°#°s°(©^ó 2ð ]añfØ˜s C˜x™.ðfØ49ðfØLTÐUXÉMðfà	ófð;¨%ð ;°Dó ;ð ñ
¨h°t©nð 
È	ÐRbÑHcò 
ó ð
ð ð yÐ1AÑ'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   z�Implements 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=   ø€ ð '7ˆOÔ#ä˜_¨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ˆØ×#Ñ# N°OÔ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   Úpytorch_lightningrz   Ú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   Ú pytorch_lightning.core.optimizerr   r   Úpytorch_lightning.pluginsr   Ú$pytorch_lightning.plugins.io.wrapperr   Ú#pytorch_lightning.plugins.precisionr   Ú/pytorch_lightning.strategies.launchers.launcherr   Ú pytorch_lightning.trainer.statesr   Ú!pytorch_lightning.utilities.typesr   r    r!   r"   Ú	getLoggerrä   rá   r$   r2   rR   r:   r8   Ú<module>rE     s‘   ðó ß #ß .Ý %ß :Õ :ã Ý Ý Ý !ã Ý 1Ý 9Ý :Ý ;Ý 6ß \Ý 2ß cÝ 7Ý FÝ 9Ý EÝ 6ß Lá�\Ó"€
Ù
�)Ó
€à€g×Ñ˜Ó!€ôh*ˆsô h*÷V/ò /r:   