§
    ­¸j{  ã                  óŽ   — d Z ddlmZ ddlmZmZmZmZ ddlZ	ddl
mZ ddlmZmZmZ ddlmZ erddlmZ  G d	„ d
e¦  «        ZdS )zEDrop-path / dropout schedule callback for RF-DETR Lightning training.é    )Úannotations)ÚTYPE_CHECKINGÚAnyÚLiteralÚcastN)ÚNDArray)ÚCallbackÚLightningModuleÚTrainer)Údrop_scheduler)ÚRFDETRModelModulec                  óB   ‡ — e Zd ZdZ	 	 	 	 	 	 ddˆ fd„Zdd„Zd d„Zˆ xZS )!ÚDropPathCallbacka  Applies per-step drop-path and dropout rate schedules to the model.

    Computes the full schedule array in ``on_train_start`` using :func:`rfdetr.util.drop_scheduler.drop_scheduler`, then
    indexes into it on every training batch to update the model's stochastic-depth and dropout rates.

    Args:
        drop_path: Peak drop-path rate.  ``0.0`` disables the schedule.
        dropout: Peak dropout rate.  ``0.0`` disables the schedule.
        cutoff_epoch: Epoch boundary for *early* / *late* modes.
        mode: Schedule mode forwarded to ``drop_scheduler``.
        schedule: Schedule shape forwarded to ``drop_scheduler``.
        vit_encoder_num_layers: Passed to ``model.update_drop_path`` so the
            model can distribute rates across ViT encoder layers.
    ç        r   ÚstandardÚconstanté   Ú	drop_pathÚfloatÚdropoutÚcutoff_epochÚintÚmodeú$Literal['standard', 'early', 'late']ÚscheduleúLiteral['constant', 'linear']Úvit_encoder_num_layersÚreturnÚNonec                ó¸   •— t          ¦   «                              ¦   «          || _        || _        || _        || _        || _        || _        d | _        d | _	        d S )N)
ÚsuperÚ__init__Ú
_drop_pathÚ_dropoutÚ_cutoff_epochÚ_modeÚ	_scheduleÚ_vit_encoder_num_layersÚ_dp_scheduleÚ_do_schedule)Úselfr   r   r   r   r   r   Ú	__class__s          €úw/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/training/callbacks/drop_schedule.pyr"   zDropPathCallback.__init__&   s]   ø€ õ 	‰Œ×ÒÑÔÐØ#ˆŒØˆŒØ)ˆÔØˆŒ
Ø!ˆŒØ'=ˆÔ$à8<ˆÔØ8<ˆÔÐÐó    Útrainerr   Ú	pl_moduler
   c                óp  — t          d|¦  «        }|j        j        }t          |j        ¦  «        }t          d||z  ¦  «        }| j        dk    r-t          | j        ||| j        | j	        | j
        ¦  «        | _        | j        dk    r/t          | j        ||| j        | j	        | j
        ¦  «        | _        dS dS )z¾Build per-step rate arrays from trainer metadata.

        Args:
            trainer: The Lightning Trainer instance.
            pl_module: The ``RFDETRModelModule`` being trained.
        r   é   r   N)r   Útrain_configÚepochsr   Úestimated_stepping_batchesÚmaxr#   r   r%   r&   r'   r)   r$   r*   )r+   r/   r0   Úmoduler4   Útotal_stepsÚsteps_per_epochs          r-   Úon_train_startzDropPathCallback.on_train_start:   sÇ   € õ Ð)¨9Ñ5Ô5ˆØÔ)Ô0ˆÝ˜'Ô<Ñ=Ô=ˆÝ˜a °Ñ!6Ñ7Ô7ˆàŒ?˜QÒÐÝ .Ø”ØØØÔ"Ø”
Ø”ñ!ô !ˆDÔð Œ=˜1ÒÐÝ .Ø”ØØØÔ"Ø”
Ø”ñ!ô !ˆDÔÐÐð Ðr.   Úbatchr   Ú	batch_idxc                óX  — |j         }t          d|¦  «        }| j        �C|t          | j        ¦  «        k     r+|j                             | j        |         | j        ¦  «         | j        �?|t          | j        ¦  «        k     r)|j                             | j        |         ¦  «         dS dS dS )aH  Apply the scheduled rates for the current global step.

        Args:
            trainer: The Lightning Trainer instance.
            pl_module: The ``RFDETRModelModule`` being trained.
            batch: The current training batch (unused).
            batch_idx: Index of the current batch within the epoch (unused).
        r   N)	Úglobal_stepr   r)   ÚlenÚmodelÚupdate_drop_pathr(   r*   Úupdate_dropout)r+   r/   r0   r;   r<   Ústepr7   s          r-   Úon_train_batch_startz%DropPathCallback.on_train_batch_startZ   s­   € ð Ô'ˆÝÐ)¨9Ñ5Ô5ˆàÔÐ(¨TµC¸Ô8IÑ4JÔ4JÒ-JÐ-JØŒL×)Ò)¨$Ô*;¸DÔ*AÀ4ÔC_Ñ`Ô`Ð`àÔÐ(¨TµC¸Ô8IÑ4JÔ4JÒ-JÐ-JØŒL×'Ò'¨Ô(9¸$Ô(?Ñ@Ô@Ð@Ð@Ð@ð )Ð(Ð-JÐ-Jr.   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r/   r   r0   r
   r   r   )
r/   r   r0   r
   r;   r   r<   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r"   r:   rD   Ú__classcell__)r,   s   @r-   r   r      s‘   ø€ € € € € ðð ð" ØØØ5?Ø2<Ø&(ð=ð =ð =ð =ð =ð =ð =ð(ð ð ð ð@Að Að Að Að Að Að Að Ar.   r   )rH   Ú
__future__r   Útypingr   r   r   r   ÚnumpyÚnpÚnumpy.typingr   Úpytorch_lightningr	   r
   r   Úrfdetr.training.drop_scheduler   Úrfdetr.training.module_modelr   r   © r.   r-   ú<module>rS      sô   ðð LÐ Kà "Ð "Ð "Ð "Ð "Ð "à 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4à Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @à 8Ð 8Ð 8Ð 8Ð 8Ð 8àð ?Ø>Ð>Ð>Ð>Ð>Ð>ðZAð ZAð ZAð ZAð ZA�xñ ZAô ZAð ZAð ZAð ZAr.   