Ë
    ëÓ^jŽ  ã                  óf   — d Z ddlmZ ddlmZmZmZ ddlZddl	m
Z
mZmZ ddlmZ  G d„ de
«      Zy)	zEDrop-path / dropout schedule callback for RF-DETR Lightning training.é    )Úannotations)ÚAnyÚLiteralÚOptionalN)ÚCallbackÚLightningModuleÚTrainer)Údrop_schedulerc                  óp   ‡ — 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.
    c                ó’   •— t         ‰| �  «        || _        || _        || _        || _        || _        || _        d | _        d | _	        y )N)
ÚsuperÚ__init__Ú
_drop_pathÚ_dropoutÚ_cutoff_epochÚ_modeÚ	_scheduleÚ_vit_encoder_num_layersÚ_dp_scheduleÚ_do_schedule)ÚselfÚ	drop_pathÚdropoutÚcutoff_epochÚmodeÚscheduleÚvit_encoder_num_layersÚ	__class__s          €úr/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/rfdetr/training/callbacks/drop_schedule.pyr   zDropPathCallback.__init__"   sL   ø€ ô 	‰ÑÔØ#ˆŒØˆŒØ)ˆÔØˆŒ
Ø!ˆŒØ'=ˆÔ$à26ˆÔØ26ˆÕó    c                óª  — |j                   j                  }t        |j                  «      }t	        d||z  «      }| j
                  dkD  r=t        | j
                  ||| j                  | j                  | j                  «      | _
        | j                  dkD  r>t        | j                  ||| j                  | j                  | j                  «      | _        yy)z¾Build per-step rate arrays from trainer metadata.

        Args:
            trainer: The Lightning Trainer instance.
            pl_module: The ``RFDETRModelModule`` being trained.
        é   r   N)Útrain_configÚepochsÚintÚestimated_stepping_batchesÚmaxr   r
   r   r   r   r   r   r   )r   ÚtrainerÚ	pl_moduler%   Útotal_stepsÚsteps_per_epochs         r    Úon_train_startzDropPathCallback.on_train_start6   s·   € ð  ×,Ñ,×3Ñ3ˆÜ˜'×<Ñ<Ó=ˆÜ˜a °Ñ!6Ó7ˆà�?‰?˜QÒÜ .Ø—‘ØØØ×"Ñ"Ø—
‘
Ø—‘ó!ˆDÔð �=‰=˜1ÒÜ .Ø—‘ØØØ×"Ñ"Ø—
‘
Ø—‘ó!ˆDÕð r!   c                óf  — |j                   }| j                  �K|t        | j                  «      k  r3|j                  j	                  | j                  |   | j
                  «       | j                  �B|t        | j                  «      k  r)|j                  j                  | j                  |   «       yyy)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).
        N)Úglobal_stepr   ÚlenÚmodelÚupdate_drop_pathr   r   Úupdate_dropout)r   r)   r*   ÚbatchÚ	batch_idxÚsteps         r    Úon_train_batch_startz%DropPathCallback.on_train_batch_startU   s–   € ð ×'Ñ'ˆà×ÑÐ(¨T´C¸×8IÑ8IÓ4JÒ-JØ�O‰O×,Ñ,¨T×->Ñ->¸tÑ-DÀd×FbÑFbÔcà×ÑÐ(¨T´C¸×8IÑ8IÓ4JÒ-JØ�O‰O×*Ñ*¨4×+<Ñ+<¸TÑ+BÕCð .KÐ(r!   )ç        r8   r   ÚstandardÚconstanté   )r   Úfloatr   r<   r   r&   r   z$Literal['standard', 'early', 'late']r   zLiteral['constant', 'linear']r   r&   ÚreturnÚNone)r)   r	   r*   r   r=   r>   )
r)   r	   r*   r   r4   r   r5   r&   r=   r>   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r-   r7   Ú__classcell__)r   s   @r    r   r      s¥   ø„ ñð" ØØØ5?Ø2<Ø&(ð7àð7ð ð7ð ð	7ð
 3ð7ð 0ð7ð !$ð7ð 
õ7ó(ð>DàðDð #ðDð ð	Dð
 ðDð 
÷Dr!   r   )rB   Ú
__future__r   Útypingr   r   r   ÚnumpyÚnpÚpytorch_lightningr   r   r	   Úrfdetr.training.drop_scheduler
   r   © r!   r    ú<module>rK      s0   ðñ Lå "ç )Ñ )ã ß @Ñ @å 8ôXD�xõ XDr!   