§
    “©ºjâ  ã                   óv   — d 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  G d	„ d
e¦  «        ZdS )z¸
Gradient Accumulator
====================

Change gradient accumulation factor according to scheduling.
Trainer also calls ``optimizer.step()`` for the last indivisible step number.

é    )ÚAny)ÚoverrideN)ÚCallback)ÚMisconfigurationException)Úis_overridden)Úrank_zero_warnc                   ó�   ‡ — e Zd ZdZdeeef         fˆ fd„Zdefd„Zdedefd„Z	e
dd„¦   «         Ze
dd	deddfd„¦   «         Zˆ xZS )ÚGradientAccumulationScheduleraÏ  Change gradient accumulation factor according to scheduling.

    Args:
        scheduling: scheduling in format {epoch: accumulation_factor}

    Note:
        The argument scheduling is a dictionary. Each key represent an epoch and
        its associated accumulation factor value.
        Warning: Epoch are zero-indexed c.f it means if you want to change
        the accumulation factor after 4 epochs, set ``Trainer(accumulate_grad_batches={4: factor})``
        or ``GradientAccumulationScheduler(scheduling={4: factor})``.
        For more info check the example below.

    Raises:
        TypeError:
            If ``scheduling`` is an empty ``dict``,
            or not all keys and values of ``scheduling`` are integers.
        IndexError:
            If ``minimal_epoch`` is less than 0.

    Example::

        >>> from pytorch_lightning import Trainer
        >>> from pytorch_lightning.callbacks import GradientAccumulationScheduler

        # from epoch 5, it starts accumulating every 2 batches. Here we have 4 instead of 5
        # because epoch (key) should be zero-indexed.
        >>> accumulator = GradientAccumulationScheduler(scheduling={4: 2})
        >>> trainer = Trainer(callbacks=[accumulator])

    Ú
schedulingc                 óÂ  •— t          ¦   «                              ¦   «          |st          d¦  «        ‚t          d„ |D ¦   «         ¦  «        r2t	          dt          |                     ¦   «         ¦  «        › d�¦  «        ‚t          d„ |                     ¦   «         D ¦   «         ¦  «        r2t	          dt          |                     ¦   «         ¦  «        › d�¦  «        ‚t          |                     ¦   «         ¦  «        }|dk     rt          d|› d	�¦  «        ‚|dk    r| 
                    dd
i¦  «         || _        t          |                     ¦   «         ¦  «        | _        d S )Nz(Empty dict cannot be interpreted correctc              3   óN   K  — | ] }t          |t          ¦  «         p|d k     V — Œ!dS )r   N©Ú
isinstanceÚint)Ú.0Úkeys     ú‹/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/pytorch_lightning/callbacks/gradient_accumulation_scheduler.pyú	<genexpr>z9GradientAccumulationScheduler.__init__.<locals>.<genexpr>I   s8   è è € ÐIÐI°s•:˜c¥3Ñ'Ô'Ð'Ð2¨3°ª7ÐIÐIÐIÐIÐIÐIó    z7Epoch should be an int greater than or equal to 0. Got ú.c              3   óN   K  — | ] }t          |t          ¦  «         p|d k     V — Œ!dS ©é   Nr   )r   Úvalues     r   r   z9GradientAccumulationScheduler.__init__.<locals>.<genexpr>N   s8   è è € ÐXÐX¸5•:˜e¥SÑ)Ô)Ð)Ð6¨U°QªYÐXÐXÐXÐXÐXÐXr   z9Accumulation factor should be an int greater than 0. Got r   zEpochs indexing from 1, epoch z cannot be interpreted correctr   )ÚsuperÚ__init__Ú	TypeErrorÚanyr   ÚlistÚkeysÚvaluesÚminÚ
IndexErrorÚupdater   ÚsortedÚepochs)Úselfr   Úminimal_epochÚ	__class__s      €r   r   z&GradientAccumulationScheduler.__init__C   si  ø€ Ý‰Œ×ÒÑÔÐàð 	HÝÐFÑGÔGÐGåÐIÐI¸jÐIÑIÔIÑIÔIð 	Ý+ØdÍ$ÈzÏÊÑO`ÔO`ÑJaÔJaÐdÐdÐdñô ð õ ÐXÐXÀJ×DUÒDUÑDWÔDWÐXÑXÔXÑXÔXð 	Ý+ØhÍDÐQ[×QbÒQbÑQdÔQdÑLeÔLeÐhÐhÐhñô ð õ ˜JŸOšOÑ-Ô-Ñ.Ô.ˆØ˜1ÒÐÝÐk¸mÐkÐkÐkÑlÔlÐlØ˜AÒÐØ×Ò˜q !˜fÑ%Ô%Ð%à$ˆŒÝ˜ZŸ_š_Ñ.Ô.Ñ/Ô/ˆŒˆˆr   Úreturnc                 ób   — t          d„ | j                             ¦   «         D ¦   «         ¦  «        S )Nc              3   ó"   K  — | ]
}|d k    V — ŒdS r   © )r   Úvs     r   r   zQGradientAccumulationScheduler.going_to_accumulate_grad_batches.<locals>.<genexpr>]   s&   è è € Ð;Ð;˜Q�1�q’5Ð;Ð;Ð;Ð;Ð;Ð;r   )r   r   r!   )r'   s    r   Ú going_to_accumulate_grad_batchesz>GradientAccumulationScheduler.going_to_accumulate_grad_batches\   s-   € ÝÐ;Ð; $¤/×"8Ò"8Ñ":Ô":Ð;Ñ;Ô;Ñ;Ô;Ð;r   Úepochc                 ób   — d}t          | j        ¦  «        D ]}||k    r| j        |         } nŒ|S )Nr   )Úreversedr&   r   )r'   r0   Úaccumulate_grad_batchesÚ
iter_epochs       r   Úget_accumulate_grad_batchesz9GradientAccumulationScheduler.get_accumulate_grad_batches_   sK   € Ø"#ÐÝ" 4¤;Ñ/Ô/ð 	ð 	ˆJØ˜
Ò"Ð"Ø*.¬/¸*Ô*EÐ'Ø�ð #ð 'Ð&r   Útrainerú
pl.TrainerÚ	pl_moduleúpl.LightningModuleNc                 ó†  — |j         st          d¦  «        ‚t          d|¦  «        }t          d|¦  «        }|                      ¦   «         }|p|}|r|rt	          d¦  «         ddlm} t          |j        |¦  «        r*t          dt          |j        ¦  «        j
        › d�¦  «        ‚|j        d	k    rt          d
¦  «        ‚dS )zhPerformns a configuration validation before training starts and raises errors for incompatible settings.zÁAutomatic gradient accumulation and the `GradientAccumulationScheduler` is not supported for
                manual optimization. Please remove the callback or switch to automatic optimization.Úoptimizer_stepÚoptimizer_zero_gradzÑWhen using `Trainer(accumulate_grad_batches != 1)` and overriding `LightningModule.optimizer_{step,zero_grad}`, the hooks will not be called on every batch (rather, they are called on every optimization step).r   )ÚDeepSpeedStrategyzThe `zE` does not support `accumulate_grad_batches` changing between epochs.r   z³You have set `accumulate_grad_batches` and are using the `GradientAccumulationScheduler` callback. Either remove `accumulate_grad_batches` from the Trainer or remove the callback.N)Úautomatic_optimizationÚRuntimeErrorr   r/   r   Úpytorch_lightning.strategiesr=   r   ÚstrategyÚtypeÚ__name__r3   Ú
ValueError)r'   r6   r8   Úoverridden_optimizer_stepÚoverridden_optimizer_zero_gradr/   Ú%has_overridden_optimization_functionsr=   s           r   Úon_train_startz,GradientAccumulationScheduler.on_train_startg   s"  € ð Ô/ð 	Ýðhñô ð õ
 %2Ð2BÀIÑ$NÔ$NÐ!Ý)6Ð7LÈiÑ)XÔ)XÐ&Ø+/×+PÒ+PÑ+RÔ+RÐ(Ø0IÐ0kÐMkÐ-Ø0ð 	Ð5Uð 	ÝðIñô ð ð 	CÐBÐBÐBÐBÐBå�gÔ&Ð(9Ñ:Ô:ð 	Ýð#�˜WÔ-Ñ.Ô.Ô7ð #ð #ð #ñô ð ð Ô*¨aÒ/Ð/Ýðnñô ð ð 0Ð/r   Ú_c                 óD   — |                       |j        ¦  «        |_        d S )N)r5   Úcurrent_epochr3   )r'   r6   rI   s      r   Úon_train_epoch_startz2GradientAccumulationScheduler.on_train_epoch_startŠ   s!   € à*.×*JÒ*JÈ7ÔK`Ñ*aÔ*aˆÔ'Ð'Ð'r   )r6   r7   r8   r9   r*   N)rC   Ú
__module__Ú__qualname__Ú__doc__Údictr   r   Úboolr/   r5   r   rH   r   rL   Ú__classcell__)r)   s   @r   r
   r
   "   sõ   ø€ € € € € ðð ð@0 4¨¨S¨¤>ð 0ð 0ð 0ð 0ð 0ð 0ð2<°$ð <ð <ð <ð <ð'°ð '¸ð 'ð 'ð 'ð 'ð ð ð  ð  ñ „Xð ðD ðb¨Lð b¸cð bÀdð bð bð bñ „Xðbð bð bð bð br   r
   )rO   Útypingr   Útyping_extensionsr   Úpytorch_lightningÚplÚ$pytorch_lightning.callbacks.callbackr   Ú&pytorch_lightning.utilities.exceptionsr   Ú)pytorch_lightning.utilities.model_helpersr   Ú%pytorch_lightning.utilities.rank_zeror   r
   r-   r   r   ú<module>r[      sÑ   ððð ð Ð Ð Ð Ð Ð à &Ð &Ð &Ð &Ð &Ð &à Ð Ð Ð Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø LÐ LÐ LÐ LÐ LÐ LØ CÐ CÐ CÐ CÐ CÐ CØ @Ð @Ð @Ð @Ð @Ð @ðjbð jbð jbð jbð jb Hñ jbô jbð jbð jbð jbr   