
    ^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)zBuild 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     ''(TC8I8I4J-JOO,,T->->t-DdFbFbc(TC8I8I4J-JOO**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!   