§
    ­¸jZg ã            
      óJ  — U d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	 ddl
mZmZmZ ddlZddlmc 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mZ ddlm Z  ddl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z( ddl)m*Z* ddl+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z:m;Z;m<Z< ddl=m>Z>  e>¦   «         Z?edej@        jA        f         ZBddddddddddœ	ZCd eDd!<   dLd&„ZEd'ZFdMd+„ZGdNd3„ZHdOd5„ZIdPd8„ZJedeez  f         ZKdQd:„ZLdRd?„ZMdSdE„ZNdTdI„ZO G dJ„ dKe¦  «        ZPdS )Uz4LightningModule for RF-DETR training and validation.é    )ÚannotationsN)Únullcontext)ÚAnyÚCallableÚcast)ÚLightningModuleÚseed_everything)ÚLightningOptimizer)ÚLRSchedulerConfigTypeÚOptimizerLRSchedulerConfig)ÚTensor)ÚLRSchedulerÚReduceLROnPlateau)Ú_namespace_from_configs)Ú_MANAGED_SCHEDULER_DEFAULTSÚModelConfigÚ
MultiScaleÚTrainConfigÚ_is_managed_optimizer_nameÚ_is_managed_scheduler_nameÚ_resolve_native_optimizer)Úcompute_multi_scale_scales)Úbuild_criterion_from_configÚbuild_model_from_config)ÚHungarianMatcher)Ú
apply_loraÚinterpolate_position_embeddingsÚload_pretrain_weights)Ú_get_ema_inner_module)ÚCudaGraphTrainingRunner)Ú_build_param_dictsÚget_param_dictÚ regroup_unmerged_optimizer_stateÚ!regroup_unmerged_scheduler_kwargs)Ú
get_logger.Úloss_clsÚloss_boxÚ	loss_giouÚmask_ceÚ	mask_diceÚkp_l1Úkp_findÚkp_visÚkp_nll)	Úloss_ceÚ	loss_bboxr(   Úloss_mask_ceÚloss_mask_diceÚloss_keypoints_l1Úloss_keypoints_findableÚloss_keypoints_visibleÚloss_keypoints_nllzdict[str, str]Ú_TRAIN_PROGRESS_LOSS_ALIASESÚ	optimizerÚobjectÚreturnÚboolc                óˆ   — t          | t          ¦  «        o-d| vo)|                      ¦   «                              ¦   «         dk    S )a‹  Return whether the config selects RF-DETR's built-in (managed, fused) AdamW path.

    Args:
        optimizer: The ``TrainConfig.optimizer`` value (string or callable).

    Returns:
        ``True`` only for the built-in ``"adamw"`` short name.

    Examples:
        >>> _is_builtin_fused_adamw("adamw")
        True
        >>> _is_builtin_fused_adamw("torch.optim.AdamW")
        False
    ú.Úadamw)Ú
isinstanceÚstrÚstripÚlower)r8   s    úl/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/training/module_model.pyÚ_is_builtin_fused_adamwrD   C   s?   € õ �i¥Ñ%Ô%Ðg¨#°YÐ*>ÐgÀ9Ç?Â?ÑCTÔCT×CZÒCZÑC\ÔC\Ð`gÒCgÐgó    z}fused_optimizer=True is ignored for optimizer=%r; the fused AdamW kernel only applies to the built-in optimizer='adamw' path.Údotted_pathr@   Ú_OptimizerFactoryc                ó.  — |                       d¦  «        \  }}}|st          d| ›d�¦  «        ‚	 t          j        |¦  «        }t	          t
          t          ||¦  «        ¦  «        S # t          t          f$ r}t          d| ›d|› d�¦  «        |‚d}~ww xY w)aN  Import an optimizer class or factory from a dotted path.

    Args:
        dotted_path: Fully-qualified path such as ``"torch.optim.AdamW"`` or
            ``"pytorch_optimizer.Lion"``.

    Returns:
        The imported optimizer class or factory.

    Raises:
        ValueError: If the module or attribute cannot be imported.
    r=   z
optimizer ú# is not a valid dotted import path.zCould not import optimizer ú: zX. Use a fully-qualified path to an importable optimizer class, e.g. 'torch.optim.AdamW'.N)	Ú
rpartitionÚ
ValueErrorÚ	importlibÚimport_moduler   rG   ÚgetattrÚImportErrorÚAttributeError©rF   Úmodule_pathÚ_Ú	attributeÚmoduleÚexcs         rC   Ú_import_optimizer_classrX   [   sÔ   € ð !,× 6Ò 6°sÑ ;Ô ;Ñ€K��IØð ZÝÐX kÐXÐXÐXÑYÔYÐYðÝÔ(¨Ñ5Ô5ˆÝÕ%¥w¨v°yÑ'AÔ'AÑBÔBÐBøÝ�Ð(ð ð ð Ýðe¨+ð eð e¸3ð eð eð eñ
ô 
ð ð	øøøøðøøøó   °6A' Á'BÁ8BÂBÚoptimizer_classÚoptimizer_nameÚparam_dictsúlist[dict[str, Any]]Úoptimizer_kwargsúdict[str, Any]útorch.optim.Optimizerc                óˆ   — 	  | |fi |¤ŽS # t           t          f$ r%} t          |¦  «        d|›d|› d�¦  «        |‚d}~ww xY w)u  Instantiate an explicitly-selected optimizer from param groups and kwargs only.

    Explicit optimizers (dotted import paths and callables) receive the RF-DETR
    parameter groups â€” which already carry per-group learning rates â€” plus the
    user's ``optimizer_kwargs`` verbatim. RF-DETR injects no ``lr`` or
    ``weight_decay`` of its own.

    Args:
        optimizer_class: Optimizer class or factory to instantiate.
        optimizer_name: Name used in error messages.
        param_dicts: RF-DETR parameter groups with layer-wise learning rates.
        optimizer_kwargs: Keyword arguments forwarded verbatim to the constructor.

    Returns:
        Instantiated optimizer.

    Raises:
        TypeError | ValueError: Re-raised with an RF-DETR-specific hint on failure.
    úFailed to initialize optimizer rJ   a  . Explicit optimizers (dotted paths and callables) are built from the RF-DETR parameter groups plus your `optimizer_kwargs` only, with no lr/weight_decay injected. Check that the class accepts these arguments, or pass a callable/functools.partial needing only `params`.N©Ú	TypeErrorrL   Útype)rZ   r[   r\   r^   rW   s        rC   Ú_instantiate_explicit_optimizerrf   u   s‹   € ð2Øˆ˜{Ð?Ð?Ð.>Ð?Ð?Ð?øÝ•zÐ"ð ð ð Ø�d�3‰iŒiði¨nð ið iÀ#ð ið ið iñ
ô 
ð
 ð	øøøøðøøøó   ‚ ‹Aœ <¼AÚnamec                óØ   — 	 t          j        | ¦  «        }n# t          t          f$ r Y dS w xY w|j                             ¦   «         }t          d„ |D ¦   «         ¦  «        rdS ||j        v S )u?  Return whether an optimizer constructor accepts a given keyword argument.

    Args:
        optimizer_class: Optimizer class or factory to inspect.
        name: Keyword-argument name to look for.

    Returns:
        ``True`` when the constructor declares ``name`` or accepts arbitrary
        keyword arguments, or when its signature cannot be introspected (e.g. a
        C-implemented constructor) â€” in which case the constructor validates the
        call itself.

    Examples:
        >>> _optimizer_accepts_kwarg(torch.optim.SGD, "weight_decay")
        True
    Tc              3  óF   K  — | ]}|j         t          j        j        u V — Œd S ©N)ÚkindÚinspectÚ	ParameterÚVAR_KEYWORD)Ú.0Ú	parameters     rC   ú	<genexpr>z+_optimizer_accepts_kwarg.<locals>.<genexpr>¯   s/   è è € Ð
WÐ
W¸yˆ9Œ>�WÔ.Ô:Ð:Ð
WÐ
WÐ
WÐ
WÐ
WÐ
WrE   )rm   Ú	signaturerd   rL   Ú
parametersÚvaluesÚany)rZ   rh   rs   rt   s       rC   Ú_optimizer_accepts_kwargrw   ™   s†   € ð"ÝÔ% oÑ6Ô6ˆ	ˆ	øÝ•zÐ"ð ð ð ØˆtˆtðøøøàÔ%×,Ò,Ñ.Ô.€JÝ
Ð
WÐ
WÈJÐ
WÑ
WÔ
WÑWÔWð ØˆtØ�9Ô'Ð'Ð'ó   ‚ —,«,Útrain_configr   c                ó  — d|j         i}t          | d¦  «        r
|j        |d<   |                     |j        ¦  «         	  | |fi |¤ŽS # t
          t          f$ r%} t          |¦  «        d|›d|› d�¦  «        |‚d}~ww xY w)aÐ  Instantiate an optimizer class with RF-DETR optimizer arguments.

    ``weight_decay`` is injected only when the optimizer constructor accepts it,
    so optimizers with a different regularization API are not forced to fail.

    Args:
        optimizer_class: Optimizer class or factory to instantiate.
        optimizer_name: Name used in error messages.
        param_dicts: RF-DETR parameter groups with layer-wise learning rates.
        train_config: Training config with base optimizer hyperparameters.

    Returns:
        Instantiated optimizer.

    Raises:
        TypeError | ValueError: Re-raised with an RF-DETR-specific hint when the
            optimizer constructor rejects the supplied arguments.
    ÚlrÚweight_decayrb   rJ   zÇ. For managed torch.optim short names, RF-DETR passes `params`, `lr`, and (when supported) `weight_decay`, then your `optimizer_kwargs`; this usually means an unsupported entry in `optimizer_kwargs`.N)r{   rw   r|   Úupdater^   rd   rL   re   )rZ   r[   r\   ry   Úinit_kwargsrW   s         rC   Ú_instantiate_optimizerr   ´   sÉ   € ð0 $(¨¬Ð"9€KÝ °Ñ@Ô@ð @Ø&2Ô&?ˆ�NÑ#Ø×Ò�|Ô4Ñ5Ô5Ð5ðØˆ˜{Ð:Ð:¨kÐ:Ð:Ð:øÝ•zÐ"ð ð ð Ø�d�3‰iŒið"¨nð "ð "À#ð "ð "ð "ñ
ô 
ð
 ð	øøøøðøøøs   ¿A ÁA>Á A9Á9A>Ú_SchedulerFactoryc                ó.  — |                       d¦  «        \  }}}|st          d| ›d�¦  «        ‚	 t          j        |¦  «        }t	          t
          t          ||¦  «        ¦  «        S # t          t          f$ r}t          d| ›d|› d�¦  «        |‚d}~ww xY w)ax  Import an LR-scheduler class or factory from a dotted path.

    Args:
        dotted_path: Fully-qualified path such as ``"torch.optim.lr_scheduler.StepLR"`` or
            ``"pytorch_optimizer.CosineAnnealingWarmupRestarts"``.

    Returns:
        The imported scheduler class or factory.

    Raises:
        ValueError: If the module or attribute cannot be imported.
    r=   zlr_scheduler rI   zCould not import lr_scheduler rJ   zf. Use a fully-qualified path to an importable scheduler class, e.g. 'torch.optim.lr_scheduler.StepLR'.N)	rK   rL   rM   rN   r   r€   rO   rP   rQ   rR   s         rC   Ú_import_scheduler_classr‚   Þ   sÔ   € ð !,× 6Ò 6°sÑ ;Ô ;Ñ€K��IØð ]ÝÐ[¨Ð[Ð[Ð[Ñ\Ô\Ð\ðÝÔ(¨Ñ5Ô5ˆÝÕ%¥w¨v°yÑ'AÔ'AÑBÔBÐBøÝ�Ð(ð ð ð Ýðs¨[ð sð s¸cð sð sð sñ
ô 
ð ð	øøøøðøøørY   Úscheduler_factoryÚscheduler_nameÚscheduler_kwargsúLRScheduler | ReduceLROnPlateauc                óˆ   — 	  | |fi |¤ŽS # t           t          f$ r%} t          |¦  «        d|›d|› d�¦  «        |‚d}~ww xY w)u  Instantiate an explicitly-selected LR scheduler from the optimizer and kwargs only.

    Explicit schedulers (dotted import paths and callables) receive the built optimizer plus the
    user's ``lr_scheduler_kwargs`` verbatim. RF-DETR injects no ``total_steps`` / ``T_max`` of its
    own â€” the managed ``"step"`` / ``"cosine"`` presets remain the runtime-aware option.

    Args:
        scheduler_factory: Scheduler class or factory to instantiate.
        scheduler_name: Name used in error messages.
        optimizer: The optimizer the scheduler drives.
        scheduler_kwargs: Keyword arguments forwarded verbatim to the constructor.

    Returns:
        Instantiated scheduler.

    Raises:
        TypeError | ValueError: Re-raised with an RF-DETR-specific hint on failure.
    z"Failed to initialize lr_scheduler rJ   a  . Explicit schedulers (dotted paths and callables) are built from the optimizer plus your `lr_scheduler_kwargs` only, with no total_steps/T_max injected. Check that the class accepts these arguments, or pass a callable/functools.partial needing only the optimizer.Nrc   )rƒ   r„   r8   r…   rW   s        rC   Ú_instantiate_explicit_schedulerrˆ   ø   sŒ   € ð0Ø Ð  Ð?Ð?Ð.>Ð?Ð?Ð?øÝ•zÐ"ð ð ð Ø�d�3‰iŒiðZ°ð Zð ZÀSð Zð Zð Zñ
ô 
ð
 ð	øøøøðøøørg   Útotal_stepsÚintÚsteps_per_epochÚwarmup_stepsú!torch.optim.lr_scheduler.LambdaLRc                ó²  ‡‡‡‡‡‡	— |j         }t          t          |j        ¦  «                             ¦   «                              ¦   «         Š	t          |                     dt          d         ¦  «        ¦  «        Št          |                     dt          d         ¦  «        ¦  «        Šd	ˆˆˆ	ˆˆˆfd„}t          j        j                             | |¬¦  «        S )
aX  Build the managed ``"step"`` / ``"cosine"`` scheduler as a warmup-aware ``LambdaLR``.

    Preserves RF-DETR's built-in schedule: a linear warmup ramp over ``warmup_steps`` followed by
    either cosine annealing down to ``min_factor`` or a 10x step decay after ``lr_drop`` epochs. The
    ``min_factor`` and ``lr_drop`` values are read from ``lr_scheduler_kwargs``, falling back to
    ``_MANAGED_SCHEDULER_DEFAULTS`` when a key is absent.

    Args:
        optimizer: The optimizer the scheduler drives.
        train_config: Training config carrying the preset name and schedule knobs.
        total_steps: Total optimizer steps over the whole run.
        steps_per_epoch: Optimizer steps per epoch.
        warmup_steps: Number of optimizer steps in the linear warmup ramp.

    Returns:
        A ``LambdaLR`` implementing the managed schedule.
    Ú
min_factorÚlr_dropÚcurrent_steprŠ   r:   Úfloatc                óV  •— | ‰k     r-t          | ¦  «        t          t          d‰¦  «        ¦  «        z  S ‰dk    rct          | ‰z
  ¦  «        t          t          d‰‰z
  ¦  «        ¦  «        z  }‰d‰z
  dz  dt          j        t          j        |z  ¦  «        z   z  z   S | ‰‰z  k     rdS dS )Né   Úcosineg      à?ç      ð?gš™™™™™¹?)r’   ÚmaxÚmathÚcosÚpi)r‘   Úprogressr�   r�   Úpresetr‹   r‰   rŒ   s     €€€€€€rC   Ú	lr_lambdaz+_build_managed_scheduler.<locals>.lr_lambda9  s·   ø€ Ø˜,Ò&Ð&Ý˜Ñ&Ô&­­s°1°lÑ/CÔ/CÑ)DÔ)DÑDÐDØ�XÒÐÝ˜\¨LÑ8Ñ9Ô9½EÅ#ÀaÈÐWcÑIcÑBdÔBdÑ<eÔ<eÑeˆHØ  Z¡°3Ñ 6¸!½d¼hÅtÄwÐQYÑGYÑ>ZÔ>ZÑ:ZÑ [Ñ[Ð[à˜' OÑ3Ò3Ð3Ø�3ØˆsrE   )r�   )r‘   rŠ   r:   r’   )Úlr_scheduler_kwargsr   r@   Úlr_schedulerrA   rB   r’   Úgetr   rŠ   ÚtorchÚoptimÚLambdaLR)
r8   ry   r‰   r‹   rŒ   Úkwargsr�   r�   r�   rœ   s
     ```  @@@rC   Ú_build_managed_schedulerr¥     sÕ   øøøøøø€ ð0 Ô-€Få•#�|Ô0Ñ1Ô1×7Ò7Ñ9Ô9×?Ò?ÑAÔA€FÝ�v—z’z ,Õ0KÈLÔ0YÑZÔZÑ[Ô[€JÝ�&—*’*˜YÕ(CÀIÔ(NÑOÔOÑPÔP€Gð	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	õ Œ;Ô#×,Ò,¨YÀ)Ð,ÑLÔLÐLrE   Ú	schedulerú$torch.optim.lr_scheduler.LRSchedulerú%torch.optim.lr_scheduler.SequentialLRc                óÈ   — t           j        j                             |dt	          d|¦  «        z  d|¬¦  «        }t           j        j                             ||| g|g¬¦  «        S )u„  Prepend a linear warmup ramp to an explicit scheduler via ``SequentialLR``.

    The warmup ramps the LR from ``1 / warmup_steps`` of its base value up to full over
    ``warmup_steps`` optimizer steps, then hands control to ``scheduler``. ``ReduceLROnPlateau`` is
    metric-driven and cannot be composed this way â€” callers must skip wrapping it.

    Args:
        scheduler: The explicit scheduler to run after warmup.
        optimizer: The optimizer both schedulers drive.
        warmup_steps: Number of optimizer steps in the linear warmup ramp.

    Returns:
        A ``SequentialLR`` chaining the warmup ramp and ``scheduler``.
    r–   r”   )Ústart_factorÚ
end_factorÚtotal_iters)Ú
schedulersÚ
milestones)r¡   r¢   rŸ   ÚLinearLRr—   ÚSequentialLR)r¦   r8   rŒ   Úwarmups       rC   Ú_wrap_with_warmupr²   G  sr   € õ& Œ[Ô%×.Ò.ØØ�3˜q ,Ñ/Ô/Ñ/ØØ ð	 /ñ ô €Fõ Œ;Ô#×0Ò0ØØ˜IÐ&Ø �>ð 1ñ ô ð rE   c                  óÐ  ‡ — e Zd ZdZdUˆ fd„ZedVd
„¦   «         ZdWd„ZdWd„ZdWd„Z	dXd„Z
dWd„ZdYd„ZdZd„Zd[d„Zd\d!„Zd]d%„Zd^d'„Zd_d)„Zd`d,„Zd`d-„Zdad/„ZdWd0„Zdbd2„ZdWd3„ZdWd4„ZdWd5„Zedcd7„¦   «         Zddd:„Zddd;„Zded=„Zdfd?„Ze dgd@„¦   «         Z!dgdA„Z"e dgdB„¦   «         Z#e dgdC„¦   «         Z$e dgdD„¦   «         Z%dhdF„Z&	 	 didjˆ fdK„Z'edkdL„¦   «         Z(dfdM„Z)dldmdP„Z*dndR„Z+dodT„Z,ˆ xZ-S )pÚRFDETRModelModulez¼LightningModule wrapping the RF-DETR model and training loop.

    Args:
        model_config: Architecture configuration.
        train_config: Training hyperparameter configuration.
    Úmodel_configr   ry   r   r:   ÚNonec                óÐ  •— t          ¦   «                              ¦   «          || _        || _        t	          t          |dd¦  «        ¦  «        | _        | j         | _        d| _        d | _	        d | _
        d| _        d | _        d | _        d | _        d | _        d| _        d| _        d| _        t'          ||¦  «        | _        |j        �jt-          | j        | j        ¦  «         |j        rIt          | j        dd ¦  «        }t1          |¦  «        r$ |¦   «          t2                               d¦  «         |j        rt9          | j        ¦  «         t;          | j        | j        ¦  «        \  | _        | _        ddl m!} tE          |j#        ¦  «         $                    ¦   «         }|dv }|j%        o|d	k    o|}|j%        r|st2                               d
||¦  «         |�rŠdtL          j'        j(        _)        dd l*m+c m(} i }	tY          |j-        d¦  «        rd|	d<   |j.        rZ|  /                    ||¦  «        }
|
€'d|	d<   d| _        t2                               d¦  «         nt2           0                    d|
¦  «         t          | j        dd ¦  «        }tc          |td          ¦  «        r/| 3                    |	¦  «         t2                               d¦  «         |j4        tj          j6        up0|j7         p(t	          |j8        ¦  «        p|j.        p|j9        p|j        }t          | j        dd ¦  «        }|�|s
 |¦   «          tM          j%        | j        ||	pd ¬¦  «        | _        d| _        d S d S )NÚuse_grouppose_keypointsFÚstepÚ"reset_keypoint_gaussian_parameterszVReset keypoint Gaussian precision outputs to unit values after pretrained weight load.r   )ÚDEVICE>   ÚgpuÚautoÚcudar¾   zçDisabling torch.compile: RF-DETR enables it only on a CUDA device with a CUDA-family accelerator (got DEVICE=%r, accelerator=%r). CPU and MPS are not expected to benefit; on XLA/TPU the graph is compiled by the XLA runtime instead.TÚcoalesce_tiling_analysisztriton.coalesce_tiling_analysisztriton.cudagraphsz¦CUDA graph replay of the compiled model enabled through Inductor cudagraph trees; each new input shape records its own graph (TORCH_LOGS=cudagraphs shows partitions).z˜Falling back to compile-only because %s; cuda_graphs together with compile is validated for single-GPU detection training without gradient accumulation.ÚmatcherzXMatcher L1 compilation enabled (dynamic shapes, no CUDA graphs; kernels compile on use).Ú enable_compiled_detection_losses)ÚdynamicÚoptions):ÚsuperÚ__init__rµ   ry   r;   rO   Ú_use_manual_optimizationÚautomatic_optimizationÚ_lr_scheduler_intervalÚ_lr_scheduler_monitorÚ_accumulated_box_normalizerÚ_logged_val_loss_skip_noticeÚ_resolved_compute_val_lossÚ_aux_aggregate_cacheÚ_aux_aggregate_cache_keysÚ_cuda_graph_runnerÚ_inductor_cudagraphsÚ_compile_activeÚstrict_loadingr   ÚmodelÚpretrain_weightsr   r¸   ÚcallableÚloggerÚinfoÚbackbone_lorar   r   Ú	criterionÚpostprocessÚrfdetr.configr»   r@   ÚacceleratorrB   Úcompiler¡   Ú_dynamoÚconfigÚcapture_scalar_outputsÚtorch._inductor.configÚ	_inductorÚhasattrÚtritonÚcuda_graphsÚ'_inductor_cudagraphs_unsupported_reasonÚwarningr?   r   Úenable_compiled_l1_costÚmulti_scaler   ÚOFFÚsquare_resize_div_64Ú
aug_configÚsegmentation_head)Úselfrµ   ry   rº   r»   rÜ   Úuses_cuda_acceleratorÚcompile_enabledÚinductor_configÚcompile_optionsÚunsupported_reasonrÀ   Údynamic_shapesÚenable_compiled_lossesÚ	__class__s                 €rC   rÅ   zRFDETRModelModule.__init__o  s  ø€ Ý‰Œ×ÒÑÔÐØ(ˆÔØ(ˆÔõ /3µ7¸<ÐIbÐdiÑ3jÔ3jÑ.kÔ.kˆÔ%Ø*.Ô*GÐ&GˆÔ#ð ,2ˆÔ#Ø15ˆÔ"Ø:>ˆÔ(ð 38ˆÔ)ð 8<ˆÔ'ð CGˆÔ!Ø@DˆÔ&ØBFˆÔð +0ˆÔ!ð &+ˆÔð $ˆÔõ -¨\¸<ÑHÔHˆŒ
ØÔ(Ð4õ " $¤*¨dÔ.?Ñ@Ô@Ð@ØÔ3ð å5<¸T¼ZÐImÐosÑ5tÔ5tÐ2ÝÐ>Ñ?Ô?ð Ø6Ð6Ñ8Ô8Ð8Ý—K’KØpñô ð ð Ô%ð 	#Ý�t”zÑ"Ô"Ð"õ ,GÀtÔGXÐZ^ÔZkÑ+lÔ+lÑ(ˆŒ˜Ô(ð 	)Ð(Ð(Ð(Ð(Ð(å˜,Ô2Ñ3Ô3×9Ò9Ñ;Ô;ˆØ +Ð/FÐ FÐð 'Ô.Ð]°6¸VÒ3CÐ]ÐH]ˆØÔð 	¨ð 	Ý�KŠKðdð Øñô ð ð ñ O	(ð ;?�EŒMÔ Ô7ð =Ð<Ð<Ð<Ð<Ð<Ð<Ð<Ð<à.0ˆOÝ�Ô-Ð/IÑJÔJð KØEJ�Ð AÑBØÔ'ð ð &*×%QÒ%QÐR^Ð`lÑ%mÔ%mÐ"Ø%Ð-Ø;?�OÐ$7Ñ8Ø04�DÔ-Ý—K’Kðoñô ð ð õ
 —N’Nðeà*ñô ð õ ˜dœn¨i¸Ñ>Ô>ˆGÝ˜'Õ#3Ñ4Ô4ð xð
 ×/Ò/°Ñ@Ô@Ð@Ý—’ÐvÑwÔwÐwð Ô(µ
´Ð>ð 8Ø#Ô8Ð8ð8å˜Ô/Ñ0Ô0ð8ð  Ô+ð8ð  Ô1ð	8ð
  Ô7ð õ &-¨T¬^Ð=_ÐaeÑ%fÔ%fÐ"Ø%Ð1¸.Ð1Ø&Ð&Ñ(Ô(Ð(ÝœØ”
 N¸OÐ<SÈtðñ ô ˆDŒJð $(ˆDÔ Ð Ð ð_O	(ð O	(rE   ú
str | Nonec                óž  — t          t          j        d¦  «        sdS |j        dk    rdS | j        rdS | j        rdS | j        rdS t          |j        ¦  «        dk    rd	|j        › d
�S |j	        }t          |j
        ¦  «        dk    pt          |t          ¦  «        o|dk    }t          |t          ¦  «        r|dvrd}|rd|›d|j
        › d
�S dS )a  Explain why Inductor CUDA graph replay must stay off for this run, or return ``None`` when it may run.

        Mirrors the single-GPU detection scope of :class:`CudaGraphTrainingRunner` from what is known at construction
        time, plus one limit specific to cudagraph trees: the compiled backward allocates its gradient outputs inside
        the graph pool, so a ``.grad`` adopted by the first microbatch is overwritten by the next replay and gradient
        accumulation raises on the second microbatch.

        Args:
            model_config: Model configuration being built.
            train_config: Training configuration for this fit run.

        Returns:
            A human-readable reason, or ``None`` when the combined path is within its validated scope.
        Úcudagraph_mark_step_beginzBthis torch version has no torch.compiler.cudagraph_mark_step_beginÚfp8zGFP8 graph replay requires Transformer Engine capture with compile=Falseú&segmentation training is not supportedú"keypoint training is not supportedú'gradient checkpointing is not supportedr”   z(gradient accumulation (grad_accum_steps=z) is not supported>   Ú1r½   Tzdistributed training (devices=z, num_nodes=N)rã   r¡   ÚcompilerÚ	amp_dtyperí   r¸   Úgradient_checkpointingrŠ   Úgrad_accum_stepsÚdevicesÚ	num_nodesr?   r@   )rµ   ry   r  Úmulti_devices       rC   ræ   z9RFDETRModelModule._inductor_cudagraphs_unsupported_reason  s  € õ  •u”~Ð'BÑCÔCð 	XØWÐWØÔ! UÒ*Ð*Ø\Ð\ØÔ)ð 	<Ø;Ð;ØÔ/ð 	8Ø7Ð7ØÔ.ð 	=Ø<Ð<Ýˆ|Ô,Ñ-Ô-°Ò1Ð1Øo¸lÔ>[ÐoÐoÐoÐoØÔ&ˆÝ˜<Ô1Ñ2Ô2°QÒ6Ðd½:ÀgÍsÑ;SÔ;SÐ;cÐX_ÐbcÒXcˆÝ�g�sÑ#Ô#ð 	 ¨°}Ð(DÐ(DØˆLØð 	vØu°GÐuÐuÈ<ÔKaÐuÐuÐuÐuØˆtrE   c                ó°   — | j         j        du r| j        rt          d¦  «        ‚| j         j        �%t          | j         j        | j        z   d¬¦  «         dS dS )aÄ  Validate loss consumers and seed RNGs at fit start.

        Rejects ``compute_val_loss=False`` when a configured callback or scheduler
        consumes ``val/loss``. Seeding here avoids hidden global side-effects in
        ``build_trainer`` while preserving deterministic training behaviour for fit runs.

        Raises:
            ValueError: If a callback or scheduler monitors ``val/loss`` while validation-loss computation is disabled.
        Fz¥compute_val_loss=False is incompatible with a callback or scheduler monitoring 'val/loss'. Set compute_val_loss=True or 'auto', or monitor a metric that is produced.NT)Úworkers)ry   Úcompute_val_lossÚ_validation_loss_is_monitoredrL   Úseedr	   Úglobal_rank©rî   s    rC   Úon_fit_startzRFDETRModelModule.on_fit_start:  sr   € ð ÔÔ-°Ð6Ð6¸4Ô;]Ð6Ýð]ñô ð ð ÔÔ!Ð-Ý˜DÔ-Ô2°TÔ5EÑEÈtÐTÑTÔTÐTÐTÐTð .Ð-rE   c                ó  — |                       ¦   «          | j        sdS 	 |                      d¬¦  «        }n# t          $ r Y dS w xY w|€dS t	          |t
          ¦  «        r|}n|g}d}|D ]i}t	          |t          j        j        ¦  «        st          |d|¦  «        }t	          |t          j        j        ¦  «        r||  
                    |¦  «        z  }Œj|r3t          | j        dd¦  «        rt                               d|¦  «         dS dS dS )	a  Configure the CUDA graph runner, then normalize restored fused-optimizer state.

        Device and world-size placement are only final once Lightning reaches this hook, so
        :meth:`_configure_cuda_graph_runner` is called first to decide, for this fit run, whether ``training_step``
        replays a captured graph or stays eager.

        Lightning restores optimizer state after ``on_fit_start``.  Fused AdamW is strict about the dtype, device, and
        layout of its moment tensors, so resuming from a checkpoint can fail if Lightning rehydrates those tensors in a
        layout that no longer matches the live parameters.  Recasting same-shaped floating-point tensors here keeps the
        resumed optimizer compatible without discarding the saved momentum state.
        NF)Úuse_pl_optimizerr   r8   Úis_global_zeroTzINormalized %d restored fused AdamW state tensors after checkpoint resume.)Ú_configure_cuda_graph_runnerÚ_use_fused_optimizerÚ
optimizersÚRuntimeErrorr?   Úlistr¡   r¢   Ú	OptimizerrO   Ú_normalize_optimizer_stateÚtrainerrÖ   r×   )rî   r  Úoptimizer_listÚnormalized_tensorsr8   s        rC   Úon_train_startz RFDETRModelModule.on_train_startL  sW  € ð 	×)Ò)Ñ+Ô+Ð+àÔ(ð 	ØˆFð	ØŸš¸%˜Ñ@Ô@ˆJˆJøÝð 	ð 	ð 	ØˆFˆFð	øøøàÐØˆFÝ�j¥$Ñ'Ô'ð 	*Ø'ˆNˆNà(˜\ˆNàÐØ'ð 	Qð 	QˆIÝ˜i­¬Ô)>Ñ?Ô?ð GÝ# I¨{¸IÑFÔF�	Ý˜)¥U¤[Ô%:Ñ;Ô;ð QØ" d×&EÒ&EÀiÑ&PÔ&PÑPÐ"øØð 	¥'¨$¬,Ð8HÈ$Ñ"OÔ"Oð 	Ý�KŠKØ[Ø"ñô ð ð ð ð	ð 	ð 	ð 	s   Ÿ6 ¶
AÁAc                ó’  — d| _         t          | j        dd¦  «        sdS | j        r#| j        st
                               d¦  «         dS | j        �r| j        j        dk    r.d| _        t
           	                    d| j        j        ¦  «         dS t          t          | j        dd¦  «        ¦  «        }t          t          | j        d	d¦  «        ¦  «        }|dk    s|dk    rt          d
|› d|› d�¦  «        ‚t          t          | j        dd¦  «        ¦  «                             d¦  «        rt          d¦  «        ‚t
                               d| j        ¦  «         dS d}| j        j        dk    rd| j        j        ›d�}nýt          t          | j        d	d¦  «        ¦  «        dk    rd}nÓ| j        j        rd}nÄ| j        j        rd}nµ| j        j        rd}n¦t          | j        j        ¦  «        dk    r^| j        j        sd}nz| j        j        t,          j        urd}n_t          t          | j        d| j        j        ¦  «        ¦  «        dk    rd}n+t          | j        j        ¦  «        dvrd| j        j        ›d�}|�t
           	                    d|¦  «         dS t          | j        j        ¦  «        dk    rHt          | j        j        dd¦  «        }|€t          d ¦  «        ‚t5          | j        |¬!¦  «        | _         nt5          | j        ¦  «        | _         t
                               d"| j        | j        j        ¦  «         dS )#aŸ  Enable CUDA graph replay only for its validated single-GPU detection scope.

        Raises:
            RuntimeError: If Inductor CUDA graph replay was compiled in but the trainer resolved gradient
                accumulation, more than one process, or Transformer Engine precision; or if the FP8 capture
                plugin has no recipe or a compatible Transformer Engine capture API is unavailable.
        Nrå   FziCUDA graphs requested with compilation outside Inductor's validated scope; training remains compile-only.r¾   zmDisabling Inductor CUDA graph replay because the model is on %r, not CUDA; training will remain compile-only.Úaccumulate_grad_batchesr”   Ú
world_sizez˜cuda_graphs=True with compile=True is validated for single-GPU training without gradient accumulation, but the trainer resolved accumulate_grad_batches=z and world_size=zA. Set cuda_graphs=False or drop the accumulation / extra devices.Ú	precisionÚ útransformer-enginez“FP8 CUDA graphs require compile=False and Transformer Engine capture; the trainer precision cannot override an already compiled Inductor graph run.zxCUDA graph replay is handled by Inductor cudagraph trees for the compiled model on %s; the eager graph runner stays off.zthe model is on z
, not CUDAz%distributed training is not supportedrû   rü   rý   z.FP8 capture requires square_resize_div_64=Truez&FP8 capture requires multi_scale=Falsez2FP8 capture does not support gradient accumulation>   ú	bf16-trueú
bf16-mixedzthe trainer precision is z1; capture requires BF16 or Transformer Engine FP8z<Disabling CUDA graphs because %s; training will run eagerly.ÚrecipezOFP8 CUDA graph capture requires the active Lightning Transformer Engine recipe.)Ú
fp8_recipez‚CUDA graph replay enabled for the training forward on %s (%s); the first batch of each input shape runs eager warm-up and capture.)rÏ   rO   rµ   rÑ   rÐ   rÖ   r×   Údevicere   rç   rŠ   r  r  r@   Ú
startswithrí   r¸   r  r  ry   rë   ré   r   rê   r  Úprecision_pluginr    rÓ   )rî   r  r  ró   r$  s        rC   r  z.RFDETRModelModule._configure_cuda_graph_runnert  s·  € ð #'ˆÔÝ�tÔ(¨-¸Ñ?Ô?ð 	ØˆFØÔð 	¨Ô(Að 	Ý�KŠKð1ñô ð ð ˆFØÔ$ñ 	ð Œ{Ô 6Ò)Ð)Ø,1�Ô)Ý—’ð9à”KÔ$ñô ð ð
 �Ý&)­'°$´,Ð@YÐ[\Ñ*]Ô*]Ñ&^Ô&^Ð#Ý�W T¤\°<ÀÑCÔCÑDÔDˆJØ&¨Ò*Ð*¨j¸Aªo¨oÝ"ðpØVmðpð pà",ðpð pð pñô ð õ
 •7˜4œ<¨°bÑ9Ô9Ñ:Ô:×EÒEÐFZÑ[Ô[ð Ý"ðdñô ð õ �KŠKð4à”ñô ð ð
 ˆFà)-ÐØŒ;Ô˜vÒ%Ð%Ø!R°D´KÔ4DÐ!RÐ!RÐ!RÐÐÝ•˜œ |°QÑ7Ô7Ñ8Ô8¸AÒ=Ð=Ø!HÐÐØÔÔ0ð 	Ø!IÐÐØÔÔ6ð 	Ø!EÐÐØÔÔ5ð 	Ø!JÐÐÝ�”Ô'Ñ(Ô(Ð,@Ò@Ð@ØÔ$Ô9ð ZØ%UÐ"Ð"ØÔ"Ô.µj´nÐDÐDØ%MÐ"Ð"Ý•W˜Tœ\Ð+DÀdÔFWÔFhÑiÔiÑjÔjÐnoÒoÐoØ%YÐ"øÝ�”Ô'Ñ(Ô(Ð0KÐKÐKàw¨D¬LÔ,BÐwÐwÐwð ð Ð)Ý�NŠNÐYÐ[mÑnÔnÐnØˆFÝˆtŒ|Ô%Ñ&Ô&Ð*>Ò>Ð>Ý˜Tœ\Ô:¸HÀdÑKÔKˆFØˆ~Ý"Ð#tÑuÔuÐuÝ&=¸d¼jÐU[Ð&\Ñ&\Ô&\ˆDÔ#Ð#å&=¸d¼jÑ&IÔ&IˆDÔ#Ý�Šð4àŒKØŒLÔ"ñ		
ô 	
ð 	
ð 	
ð 	
rE   Úbatchútuple[Any, Any]Ú	batch_idxrŠ   c                ó|  — | j         }| j        }|j        t          j        u �r|\  }}t          |j        |j        |j        |j	        ¦  «        }| j
        j        }t          j        |¦  «                             |¦  «        }	t          j        ¦   «         5  t#          j        |j        |	dd¬¦  «        |_        t#          j        |j                             d¦  «                             ¦   «         |	d¬¦  «                             d¦  «                             ¦   «         |_        ddd¦  «         dS # 1 swxY w Y   dS dS )an  Apply optional multi-scale resize to the incoming batch.

        Modifications to ``batch`` (in-place on ``NestedTensor``) are visible in ``training_step`` because they share
        the same object.

        Args:
            batch: Tuple of (NestedTensor samples, list of target dicts).
            batch_idx: Index of the current batch within the epoch.
        ÚbilinearF)ÚsizeÚmodeÚalign_cornersr”   Únearest)r.  r/  N)ry   rµ   ré   r   Ú	PER_BATCHr   Ú
resolutionÚexpanded_scalesÚ
patch_sizeÚnum_windowsr  Úglobal_stepÚrandomÚRandomÚchoicer¡   Úno_gradÚFÚinterpolateÚtensorsÚmaskÚ	unsqueezer’   Úsqueezer;   )
rî   r)  r+  ÚtcÚmcÚsamplesrT   Úscalesr¹   Úscales
             rC   Úon_train_batch_startz&RFDETRModelModule.on_train_batch_startÍ  s_  € ð ÔˆØÔˆàŒ>�ZÔ1Ð1Ñ1Ø‰JˆG�QÝ/°´¸rÔ?QÐSUÔS`ÐbdÔbpÑqÔqˆFØ”<Ô+ˆDõ ”M $Ñ'Ô'×.Ò.¨vÑ6Ô6ˆEÝ”‘”ð ð Ý"#¤-°´ÀeÐR\ÐlqÐ"rÑ"rÔ"r�”å”M '¤,×"8Ò"8¸Ñ";Ô";×"AÒ"AÑ"CÔ"CÈ%ÐV_Ð`Ñ`Ô`×hÒhÐijÑkÔk×pÒpÑrÔrð ”ðð ð ñ ô ð ð ð ð ð ð ð øøøð ð ð ð ð ð ð 2Ð1s   ÂBD/Ä/D3Ä6D3c                óÐ   — | j         �W	 |                      ¦   «         }t          |t          ¦  «        r|n|gD ]}|                     ¦   «          Œn# t
          $ r Y nw xY wd| _         dS )ay  Reset the accumulated box normalizer at the start of every training epoch.

        Lightning may reuse the module across epochs without calling ``_step_optimizer`` at the boundary (for example
        when an epoch ends mid-accumulation window with a non-divisible batch count). Clearing the accumulator here
        guarantees the manual-optimization path always starts each epoch from a known state, so the first microbatch's
        gradients are scaled by its own box count and not by a stale previous-epoch denominator.

        This is a no-op for non-keypoint models because they use Lightning's automatic optimization path and never
        populate ``self._accumulated_box_normalizer``.

        Note: on finite datasets the final-batch fallback in ``_should_step_optimizer`` always flushes a partial
        trailing window, so this reset is the only change needed.  On IterableDatasets (infinite
        ``num_training_batches``) a partial window may survive epoch end with un-stepped gradients; those are
        discarded here and the optimizer is zeroed so the first microbatch of the new epoch starts from a clean state.
        N)rÊ   r  r?   r  Ú	zero_gradr  )rî   ÚoptsÚopts      rC   Úon_train_epoch_startz&RFDETRModelModule.on_train_epoch_startç  s‡   € ð  Ô+Ð7ðØ—’Ñ(Ô(�Ý#-¨dµDÑ#9Ô#9ÐE˜4˜4À¸vð $ð $�CØ—M’M‘O”O�O�Oð$øåð ð ð Ø�ðøøøà+/ˆÔ(Ð(Ð(s   ‰AA Á
AÁAúTensor | dict[str, Any]c           	     óø  ‡‡‡— |\  }}t          |¦  «        }| j        rt          j                             ¦   «          | j        �|                      ||¦  «        n|                      ||¦  «        }| j        r1|                      ||¦  «        \  Š}}|  	                    ||¦  «        }	n|  
                    ||¦  «        Šd}	| j
        j        Št          j        ˆˆfd„‰D ¦   «         ¦  «                             ¦   «         }
t          | j        j        ¦  «        }t          | j        j        ¦  «        }| j        j        r|                      ‰‰¦  «        }d}n d„ ‰                     ¦   «         D ¦   «         }|}|                      ||d||¬¦  «         |                      d|
d|d||¬¦  «         |                      |
‰|¬	¦  «         | j        rÉ|                      ¦   «         }t3          |t4          ¦  «        r|d
         }|	€J ‚|                      |¦  «        }t3          |t8          ¦  «        r|                     |¬¦  «        nt=          ¦   «         }|5  |                      |	¦  «         ddd¦  «         n# 1 swxY w Y   |r|                       |¦  «         | j        j!        r¿t          j"        ¦   «         5  t          j        d„ |D ¦   «         ¦  «        }| j#        j$        Šˆfd„|                     ¦   «         D ¦   «         }|  %                    ||¦  «        }ddd¦  «         n# 1 swxY w Y   | j        r|
 &                    ¦   «         n|
|  '                    |¦  «        |dœS | j        r|
 &                    ¦   «         n|
S )a  Compute loss for one training step and log metrics.

        PTL handles AMP (``precision``) without a manual ``GradScaler``. Keypoint models perform manual optimization so
        box-count loss normalization is based on the full accumulated effective batch rather than each microbatch
        independently; detection and segmentation models keep Lightning's automatic optimization path.

        Args:
            batch: Tuple of (NestedTensor samples, list of target dicts).
            batch_idx: Batch index within the epoch.

        Returns:
            Scalar loss tensor by default. When ``compute_train_metrics=True``,
            returns a Lightning-compatible dict containing ``loss`` plus
            detached postprocessed predictions for train mAP logging.
        Nc                ó:   •— g | ]}|‰v ¯‰|         ‰|         z  ‘ŒS © rP  ©rp   ÚkÚ	loss_dictÚweight_dicts     €€rC   ú
<listcomp>z3RFDETRModelModule.training_step.<locals>.<listcomp>%  s2   ø€ Ð#iÐ#iÐ#iÀaÐXYÐ]hÐXhÐXh I¨a¤L°;¸q´>Ñ$AÐXhÐXhÐXhrE   Fc                ó    — i | ]\  }}d |› �|“ŒS )útrain/rP  )rp   Ú	loss_nameÚvalues      rC   ú
<dictcomp>z3RFDETRModelModule.training_step.<locals>.<dictcomp>0  s(   € Ð!hÐ!hÐ!hÑBRÀ)ÈUÐ"6¨9Ð"6Ð"6¸Ð!hÐ!hÐ!hrE   T©Úon_stepÚon_epochÚ	sync_distÚ
batch_sizez
train/loss)Úprog_barr\  r]  r^  r_  ©r_  r   )Ú	sync_gradc                ó   — g | ]
}|d          ‘ŒS ©Ú	orig_sizerP  ©rp   Úts     rC   rU  z3RFDETRModelModule.training_step.<locals>.<listcomp>Z  s   € Ð)JÐ)JÐ)J¸Q¨!¨K¬.Ð)JÐ)JÐ)JrE   c                ó€   •— i | ]:\  }}|d v ¯	t          |t          ¦  «        ¯||j        dk    r|dd…d‰…f         n|“Œ;S ))Úpred_logitsÚ
pred_boxesÚ
pred_masksÚpred_keypointsé   N)r?   r   Úndim)rp   rR  ÚvÚnqs      €rC   rZ  z3RFDETRModelModule.training_step.<locals>.<dictcomp>c  se   ø€ ð %ð %ð %á˜˜1ØÐYÐYÐYÕ^hÐijÕlrÑ^sÔ^sÐYð  A¤F¨a¢K K�q˜˜˜˜C˜R˜C˜”y�y°QàYÐYÐYrE   )ÚlossÚresultsÚtargets)(ÚlenrÐ   r¡   rÿ   rù   rÏ   rÓ   rÆ   Ú_compute_train_lossesÚ_scale_loss_for_accumulationrÙ   rT  ÚstackÚsumr;   ry   Útrain_log_sync_distÚtrain_log_on_stepÚcompact_train_metricsÚ_compact_train_loss_metricsÚitemsÚlog_dictÚlogÚ_log_train_progress_metricsr  r?   r  Ú_should_step_optimizerr
   Útoggle_modelr   Úmanual_backwardÚ_step_optimizerÚcompute_train_metricsr;  rµ   Únum_queriesrÚ   ÚdetachÚ_detach_results)rî   r)  r+  rD  rs  r_  ÚoutputsÚraw_lossÚ
normalizerÚloss_for_backwardrq  ry  rz  Útrain_loss_metricsÚcomponent_on_stepr8   Úshould_stepÚsync_contextÚ
orig_sizesÚinference_outputsrr  rS  rp  rT  s                        @@@rC   Útraining_stepzRFDETRModelModule.training_step  so  øøø€ ð  !Ñˆ�Ý˜‘\”\ˆ
ØÔ$ð 	7õ ŒN×4Ò4Ñ6Ô6Ð6ð Ô&Ð2ð ×#Ò# G¨WÑ5Ô5Ð5à—’˜G WÑ-Ô-ð 	ð
 Ô(ð 	%Ø.2×.HÒ.HÈÐRYÑ.ZÔ.ZÑ+ˆI�x Ø $× AÒ AÀ(ÈJÑ WÔ WÐÐàŸš w°Ñ8Ô8ˆIØ $ÐØ”nÔ0ˆÝ”{Ð#iÐ#iÐ#iÐ#iÐ#iÈ9Ð#iÑ#iÔ#iÑjÔj×nÒnÑpÔpˆõ
 # 4Ô#4Ô#HÑIÔIÐÝ  Ô!2Ô!DÑEÔEÐØÔÔ2ð 	2Ø!%×!AÒ!AÀ)È[Ñ!YÔ!YÐØ %ÐÐà!hÐ!hÐV_×VeÒVeÑVgÔVgÐ!hÑ!hÔ!hÐØ 1ÐØ�ŠØØ%ØØ)Ø!ð 	ñ 	
ô 	
ð 	
ð 	�ŠØØØØ%ØØ)Ø!ð 	ñ 	
ô 	
ð 	
ð 	×(Ò(¨¨yÀZÐ(ÑPÔPÐPØÔ(ð 	0ð ŸšÑ)Ô)ˆIÝ˜)¥TÑ*Ô*ð )Ø% aœL�	ð %Ð0Ð0Ð0Ø×5Ò5°iÑ@Ô@ˆKõ
 ˜iÕ);Ñ<Ô<ð#�	×&Ò&°Ð&Ñ=Ô=Ð=å ‘]”]ð ð
 ð 8ð 8Ø×$Ò$Ð%6Ñ7Ô7Ð7ð8ð 8ð 8ñ 8ô 8ð 8ð 8ð 8ð 8ð 8ð 8øøøð 8ð 8ð 8ð 8àð 0Ø×$Ò$ YÑ/Ô/Ð/ØÔÔ2ð 	Ý”‘”ð Jð JÝ"œ[Ð)JÐ)JÀ'Ð)JÑ)JÔ)JÑKÔK�
ð Ô&Ô2�ð%ð %ð %ð %à '§¢¡¤ð%ñ %ô %Ð!ð
 ×*Ò*Ð+<¸jÑIÔI�ðJð Jð Jñ Jô Jð Jð Jð Jð Jð Jð Jøøøð Jð Jð Jð Jð" *.Ô)FÐP˜Ÿš™œ˜ÈDØ×/Ò/°Ñ8Ô8Ø"ðð ð ð
 !%Ô =ÐGˆt�{Š{‰}Œ}ˆ}À4ÐGs%   ÉI2É2I6É9I6Ê3A!L Ì L$Ì'L$rS  údict[str, Tensor]rT  údict[str, float]údict[str, str | None]c                ó  — t          |¦  «        }| j        �| j        |k    r[i }|D ]H}|                     d¦  «        \  }}}|o|                     ¦   «         p|dk    }	|	r
||v rd|› d�nd||<   ŒI|| _        || _        | j        S )aÙ  Return the memoized ``loss_name -> aggregate train/ key`` map for the current loss_dict keys.

        The classification only depends on ``loss_dict``'s key set and ``weight_dict`` membership, both
        fixed by model architecture, so it is safe to compute once and cache across microbatches; it is
        recomputed only if the observed key set changes.

        Args:
            loss_dict: Per-term loss values for the current microbatch.
            weight_dict: Criterion-defined mapping of weighted loss-term names to their weights.

        Returns:
            Mapping from each ``loss_dict`` key to its ``train/<base>_aux`` aggregate key, or ``None``
            when the key should be logged individually.
        NrT   ÚencrW  Ú_aux)Ú	frozensetrÍ   rÎ   rK   Úisdigit)
rî   rS  rT  ÚkeysÚaggregate_maprX  Ú	base_nameÚ	separatorÚsuffixÚis_layer_suffixeds
             rC   Ú_aux_aggregate_mapz$RFDETRModelModule._aux_aggregate_mapp  sÅ   € õ ˜Ñ#Ô#ˆØÔ$Ð,°Ô0NÐRVÒ0VÐ0VØ35ˆMØ&ð ð �	Ø/8×/CÒ/CÀCÑ/HÔ/HÑ,�	˜9 fØ$-Ð$W°6·>²>Ñ3CÔ3CÐ3VÀvÐQVÂÐ!ð 1BÐhÀiÐS^ÐF^ÐF^Ð,˜YÐ,Ð,Ð,Ð,Ðdhð ˜iÑ(Ð(ð )6ˆDÔ%Ø-1ˆDÔ*ØÔ(Ð(rE   c                ól  — |                       ||¦  «        }i }i }|                     ¦   «         D ]A\  }}||         }|€	||d|› �<   Œ|                     |g ¦  «                             |¦  «         ŒB|                     ¦   «         D ].\  }}	t	          j        |	¦  «                             ¦   «         ||<   Œ/|S )a‡  Aggregate per-layer auxiliary loss terms into one ``train/<term>_aux`` tensor per base loss.

        Args:
            loss_dict: Per-term loss values for the current microbatch.
            weight_dict: Criterion-defined mapping of weighted loss-term names to their weights.

        Returns:
            Mapping of ``train/`` metric names to tensors, ready for ``self.log_dict``.
        NrW  )r¢  r}  Ú
setdefaultÚappendr¡   rw  rx  )
rî   rS  rT  r�  Úgroupedr�  rX  rY  Úaggregate_nameru   s
             rC   r|  z-RFDETRModelModule._compact_train_loss_metricsŽ  sÚ   € ð ×/Ò/°	¸;ÑGÔGˆØ+-ˆØ02ÐØ )§¢Ñ 1Ô 1ð 	Eð 	EÑˆI�uØ*¨9Ô5ˆNØÐ%Ø;@Ð"Ð#7¨IÐ#7Ð#7Ñ8Ð8à×"Ò" >°2Ñ6Ô6×=Ò=¸eÑDÔDÐDÐDØ&-§m¢m¡o¤oð 	Kð 	KÑ"ˆN˜FÝ16´¸VÑ1DÔ1D×1HÒ1HÑ1JÔ1JÐ˜~Ñ.Ð.Ø!Ð!rE   r‰  rs  úlist[dict[str, Tensor]]ú(tuple[dict[str, Tensor], Tensor, Tensor]c                ó¬  ‡‡‡— | j         j        Št          | j         dd¦  «        s)t          t	          | j         ¦  «        j        › d�¦  «        ‚| j                              ||¦  «        Š|                       ||t          j        ‰¦  «        ¬¦  «        Šˆˆfd„‰ 	                    ¦   «         D ¦   «         }t          ˆˆfd„‰D ¦   «         ¦  «        }||‰fS )aN  Compute normalized losses for logging and raw weighted loss for backward.

        Args:
            outputs: Model output dictionary.
            targets: Target dictionaries for the current batch.

        Returns:
            A tuple of normalized loss dictionary, unnormalized weighted loss numerator, and box normalizer.
        Ú!supports_loss_normalizer_overrideFa  .supports_loss_normalizer_override is False; manual optimization (keypoint models) requires a criterion that accepts a num_boxes keyword argument. Set supports_loss_normalizer_override = True on your criterion subclass and implement the num_boxes parameter in forward().)Ú	num_boxesc                ó.   •— i | ]\  }}||‰v r|‰z  n|“ŒS rP  rP  )rp   ÚkeyrY  r‹  rT  s      €€rC   rZ  z;RFDETRModelModule._compute_train_losses.<locals>.<dictcomp>Æ  s@   ø€ ð 
ð 
ð 
ÙISÈÈeˆC s¨kÐ'9Ð'9�˜Ñ#Ð#¸uð
ð 
ð 
rE   c              3  óB   •K  — | ]}|‰v ¯‰|         ‰|         z  V — Œd S rk   rP  )rp   rR  Únumerator_loss_dictrT  s     €€rC   rr   z:RFDETRModelModule._compute_train_losses.<locals>.<genexpr>É  s>   øè è € ÐpÐpÀ1Ð_`ÐdoÐ_oÐ_oÐ*¨1Ô-°¸A´Ñ>Ð_oÐ_oÐ_oÐ_oÐpÐprE   )rÙ   rT  rO   rL   re   Ú__name__Únum_boxes_for_targetsr¡   Ú	ones_liker}  rx  )rî   r‰  rs  rS  rŠ  r‹  r°  rT  s        @@@rC   ru  z'RFDETRModelModule._compute_train_losses§  s  øøø€ ð ”nÔ0ˆÝ�t”~Ð'JÈEÑRÔRð 	ÝÝ˜œÑ'Ô'Ô0ð ^ð ^ð ^ñô ð ð ”^×9Ò9¸'À7ÑKÔKˆ
Ø"Ÿnšn¨W°gÍÌÐYcÑIdÔId˜nÑeÔeÐð
ð 
ð 
ð 
ð 
ØWj×WpÒWpÑWrÔWrð
ñ 
ô 
ˆ	õ ÐpÐpÐpÐpÐpÐH[ÐpÑpÔpÑpÔpˆØ˜( JÐ.Ð.rE   rŠ  r   r‹  c                óº   — |                      ¦   «         }| j        }|€|n||z   }|�|                      ||z  ¦  «         |                      ¦   «         | _        ||z  S )a$  Scale the current numerator loss by the accumulated box denominator.

        Args:
            raw_loss: Current microbatch weighted loss numerator.
            normalizer: Current microbatch box denominator.

        Returns:
            Loss scalar to pass to ``manual_backward``.
        )r‡  rÊ   Ú_rescale_accumulated_gradients)rî   rŠ  r‹  Úprevious_normalizerÚaccumulated_normalizers        rC   rv  z.RFDETRModelModule._scale_loss_for_accumulationÌ  su   € ð  ×&Ò&Ñ(Ô(ˆ
Ø"Ô>ÐØ/BÐ/J  ÐPcÐfpÑPpÐØÐ*Ø×/Ò/Ð0CÐF\Ñ0\Ñ]Ô]Ð]Ø+A×+HÒ+HÑ+JÔ+JˆÔ(ØÐ0Ñ0Ð0rE   rF  c                óÈ   — |                       ¦   «         D ]L}|j        �C|j                             |                     |j        j        |j        j        ¬¦  «        ¦  «         ŒMdS )zÑRescale gradients already accumulated in the current optimizer window.

        Args:
            scale: Multiplicative factor that converts previous gradients from the old denominator to the new one.
        N©r&  Údtype)rt   ÚgradÚmul_Útor&  rº  )rî   rF  rq   s      rC   rµ  z0RFDETRModelModule._rescale_accumulated_gradientsâ  sf   € ð ŸšÑ*Ô*ð 	hð 	hˆIØŒ~Ð)Ø”×#Ò# E§H¢H°I´NÔ4IÐQZÔQ_ÔQe HÑ$fÔ$fÑgÔgÐgøð	hð 	hrE   r;   c                ó
  — t          dt          | j        j        ¦  «        ¦  «        }|dz   |z  dk    rdS t	          | j        dd¦  «        }t          |t          t          f¦  «        ot          j	        |¦  «        o|dz   |k    S )u�  Return whether the current batch closes an optimizer accumulation window.

        The optimizer steps when either:

        - The current batch closes a complete ``grad_accum_steps`` window
          (``(batch_idx + 1) % grad_accum_steps == 0``), or
        - This is the final batch of the epoch and a partial accumulation window
          is still open, so the trailing microbatches are not silently dropped.

        Lightning's ``Trainer.num_training_batches`` may be reported as ``float('inf')``
        for iterable / streaming datasets where the epoch length is unknown. In that case
        only the modulo path can ever close the window â€” the final-batch fallback is
        skipped because ``batch_idx + 1`` can never reach infinity.

        Args:
            batch_idx: Batch index within the epoch.

        Returns:
            ``True`` when the optimizer should step after this batch.
        r”   r   TÚnum_training_batchesN)
r—   rŠ   ry   r  rO   r  r?   r’   r˜   Úisfinite)rî   r+  Úaccum_stepsr¿  s       rC   r�  z(RFDETRModelModule._should_step_optimizerì  s‹   € õ* ˜!�S Ô!2Ô!CÑDÔDÑEÔEˆØ˜‰M˜[Ñ(¨AÒ-Ð-Ø�4Ý& t¤|Ð5KÈTÑRÔRÐåÐ+­cµ5¨\Ñ:Ô:ð 6Ý”Ð2Ñ3Ô3ð6à˜A‘Ð!5Ò5ð	
rE   r8   ú*torch.optim.Optimizer | LightningOptimizerc                ó  — d„ |j         D ¦   «         }|sdS |                      d|d         ddd¬¦  «         |                      dt          |¦  «        ddd¬¦  «         |                      d	t          |¦  «        ddd¬¦  «         dS )
z“Log the learning-rate range used by an optimizer update.

        Args:
            optimizer: Optimizer about to update model parameters.
        c                ó&   — g | ]}d |v ¯|d          ‘ŒS )r{   rP  )rp   Úparam_groups     rC   rU  z9RFDETRModelModule._log_learning_rates.<locals>.<listcomp>  s)   € ÐhÐhÐh¨;ÐTXÐ\gÐTgÐTg�[ Ô&ÐTgÐTgÐTgrE   Nztrain/lrr   TF)r`  r\  r]  ztrain/lr_minztrain/lr_max)Úparam_groupsr  Úminr—   )rî   r8   Ú	group_lrss      rC   Ú_log_learning_ratesz%RFDETRModelModule._log_learning_rates  s•   € ð iÐh¸)Ô:PÐhÑhÔhˆ	Øð 	ØˆFØ�Š�˜Y qœ\°DÀ$ÐQVˆÑWÔWÐWØ�Š�¥ Y¡¤¸%ÈÐX]ˆÑ^Ô^Ð^Ø�Š�¥ Y¡¤¸%ÈÐX]ˆÑ^Ô^Ð^Ð^Ð^rE   c                ó²  — t          | j        dd¦  «        }|€| j        j        }n!t	          |t
          t          f¦  «        r|}nd}t          | j        dd¦  «        }t	          |t          ¦  «        sd}|�|dk    r|                      |||¬¦  «         | 	                    ¦   «          | 
                    ¦   «          |                      ¦   «          d| _        dS )zœClip gradients, step optimizer and scheduler, then reset accumulation state.

        Args:
            optimizer: Optimizer returned by Lightning.
        Úgradient_clip_valNÚgradient_clip_algorithmr   ©rË  rÌ  )rO   r  ry   Úclip_max_normr?   rŠ   r’   r@   Úclip_gradientsr¹   rI  Ú_step_lr_schedulerrÊ   )rî   r8   Útrainer_gradient_clip_valrË  rÌ  s        rC   r„  z!RFDETRModelModule._step_optimizer  sþ   € õ %,¨D¬LÐ:MÈtÑ$TÔ$TÐ!Ø$Ð,Ø $Ô 1Ô ?ÐÐÝÐ1µC½°<Ñ@Ô@ð 	%Ø 9ÐÐà $ÐÝ")¨$¬,Ð8QÐSWÑ"XÔ"XÐÝÐ1µ3Ñ7Ô7ð 	+Ø&*Ð#ØÐ(Ð->ÀÒ-BÐ-BØ×ÒØØ"3Ø(?ð  ñ ô ð ð
 	�ŠÑÔÐØ×ÒÑÔÐØ×ÒÑ!Ô!Ð!Ø+/ˆÔ(Ð(Ð(rE   ú&LRScheduler | ReduceLROnPlateau | Nonec                ó¤   — 	 |                       ¦   «         }n# t          t          f$ r Y dS w xY wt          |t          ¦  «        r|r|d         ndS |S )a  Return the single configured LR scheduler, or ``None`` when none is available.

        Returns:
            The scheduler object (unwrapping Lightning's single-element list), or ``None`` when no
            scheduler is configured yet or Lightning is between fit stages.
        Nr   )Úlr_schedulersrQ   r  r?   r  ©rî   r¦   s     rC   Ú_current_lr_schedulerz'RFDETRModelModule._current_lr_scheduler3  sl   € ð	Ø×*Ò*Ñ,Ô,ˆIˆIøÝ¥Ð-ð 	ð 	ð 	Ø�4�4ð	øøøå�i¥Ñ&Ô&ð 	7Ø#,Ð6�9˜Q”<�<°$Ð6ØÐrx   c                ó¢   — | j         dk    rdS |                      ¦   «         }|�t          |t          ¦  «        rdS |                     ¦   «          dS )a(  Step step-interval schedulers once per optimizer step (manual-optimization path).

        Epoch-interval schedulers and metric-driven ``ReduceLROnPlateau`` are stepped at epoch boundaries by
        ``on_train_epoch_end`` / ``on_validation_epoch_end`` instead, so they are skipped here.
        r¹   N)rÈ   rÖ  r?   r   r¹   rÕ  s     rC   rÐ  z$RFDETRModelModule._step_lr_schedulerB  sY   € ð Ô&¨&Ò0Ð0ØˆFØ×.Ò.Ñ0Ô0ˆ	ØÐ¥
¨9Õ6GÑ HÔ HÐØˆFØ�ŠÑÔÐÐÐrE   r`   c                ó0   — |                       |¦  «         dS )ap  Log rates immediately before each Lightning-managed optimizer step.

        Lightning invokes this hook for both automatic-optimization and
        manual-optimization (keypoint) training paths, so it is the sole
        emission site for learning-rate logging on either path.

        Args:
            optimizer: Optimizer about to update model parameters.
        N)rÉ  )rî   r8   s     rC   Úon_before_optimizer_stepz*RFDETRModelModule.on_before_optimizer_stepO  s   € ð 	× Ò  Ñ+Ô+Ð+Ð+Ð+rE   c                ó°   — | j         s| j        dk    rdS |                      ¦   «         }|�t          |t          ¦  «        rdS |                     ¦   «          dS )zïStep epoch-interval (non-plateau) schedulers on the manual-optimization path.

        The automatic-optimization path leaves scheduler stepping entirely to Lightning; only the manual keypoint loop
        steps schedulers itself.
        ÚepochN)rÇ   rÈ   rÖ  r?   r   r¹   rÕ  s     rC   Úon_train_epoch_endz$RFDETRModelModule.on_train_epoch_end[  sc   € ð Ô&ð 	¨$Ô*EÈÒ*PÐ*PØˆFØ×.Ò.Ñ0Ô0ˆ	ØÐ¥
¨9Õ6GÑ HÔ HÐØˆFØ�ŠÑÔÐÐÐrE   c                ó  — |                       ¦   «         | _        | j        s| j        j        dk    rdS t          | j        dd¦  «        st          | j        dd¦  «        sdS | j        rdS d| _        t                               d¦  «         dS )u  Resolve the validation-loss policy for this epoch, announcing an ``"auto"`` skip the first time it happens.

        The resolution is cached here â€” unconditionally, before every early return below â€” because ``validation_step``
        reads it on every batch while it depends only on configuration that cannot change mid-epoch.

        The consumer scan behind ``compute_val_loss="auto"`` only sees programmatic consumers â€” a plateau scheduler or a
        monitoring callback. It cannot see a human reading a ``val/loss`` curve out of ``metrics.csv``, TensorBoard, or
        Weights & Biases, who would otherwise find the curve silently gone. Stating the resolution once, on the first
        real validation epoch of the global-zero rank, gives that reader the knob to turn.
        r½   NÚsanity_checkingFr  Ta.  Skipping validation-loss computation: compute_val_loss='auto' found no scheduler or callback monitoring 'val/loss', so no 'val/loss' metric is logged this run. Set compute_val_loss=True to log it every validation epoch (for example to read the curve from metrics.csv, TensorBoard, or Weights & Biases).)	Ú _resolve_should_compute_val_lossrÌ   rË   ry   r  rO   r  rÖ   r×   r  s    rC   Úon_validation_epoch_startz+RFDETRModelModule.on_validation_epoch_starth  sª   € ð +/×*OÒ*OÑ*QÔ*QˆÔ'ØÔ,ð 	°Ô0AÔ0RÐV\Ò0\Ð0\ØˆFå�4”<Ð!2°EÑ:Ô:ð 	Å'È$Ì,ÐXhÐjnÑBoÔBoð 	ØˆFØÔ*ð 	ØˆFØ,0ˆÔ)Ý�Šðsñ	
ô 	
ð 	
ð 	
ð 	
rE   c                ó*  — | j         s| j        j        rdS |                      ¦   «         }t	          |t
          ¦  «        sdS | j        pd}| j        j                             |¦  «        }|€t          d|›d�¦  «        ‚| 
                    |¦  «         dS )a­  Step ``ReduceLROnPlateau`` from the monitored metric on the manual-optimization path.

        The automatic-optimization path lets Lightning feed the monitored metric; the manual keypoint loop must read it
        from ``trainer.callback_metrics`` and step the scheduler itself. The pre-training sanity-check validation is
        skipped so plateau patience/cooldown bookkeeping is not seeded from the untrained model.
        Núval/losszReduceLROnPlateau monitor a   was not found in callback_metrics, so the learning rate would never be reduced. Ensure the monitored metric is logged every validation epoch (e.g. set compute_val_loss=True for the default 'val/loss' monitor), or set lr_scheduler_monitor to a metric that is produced.)rÇ   r  rÞ  rÖ  r?   r   rÉ   Úcallback_metricsr    r  r¹   )rî   r¦   ÚmonitorÚmetrics       rC   Úon_validation_epoch_endz)RFDETRModelModule.on_validation_epoch_end‚  s·   € ð Ô&ð 	¨$¬,Ô*Fð 	ØˆFØ×.Ò.Ñ0Ô0ˆ	Ý˜)Õ%6Ñ7Ô7ð 	ØˆFØÔ,Ð:°
ˆØ”Ô.×2Ò2°7Ñ;Ô;ˆØˆ>õ ð$¨Wð $ð $ð $ñô ð ð 	�Š�vÑÔÐÐÐrE   rr  c                ó   — d„ | D ¦   «         S )a  Detach postprocessed result tensors before handing them to callbacks.

        Args:
            results: Per-image postprocessed prediction dictionaries.

        Returns:
            Per-image dictionaries with tensor values detached from the graph.
        c                óJ   — g | ] }d „ |                      ¦   «         D ¦   «         ‘Œ!S )c                ój   — i | ]0\  }}|t          j        |¦  «        r|                     ¦   «         n|“Œ1S rP  )r¡   Ú	is_tensorr‡  )rp   r®  rY  s      rC   rZ  z@RFDETRModelModule._detach_results.<locals>.<listcomp>.<dictcomp>©  s9   € ÐgÐgÐgÉ*È#ÈuˆS¥E¤O°EÑ$:Ô$:ÐE�%—,’,‘.”.�.ÀÐgÐgÐgrE   )r}  )rp   Úresults     rC   rU  z5RFDETRModelModule._detach_results.<locals>.<listcomp>¨  sD   € ð 
ð 
ð 
àð hÐgÐX^×XdÒXdÑXfÔXfÐgÑgÔgð
ð 
ð 
rE   rP  )rr  s    rC   rˆ  z!RFDETRModelModule._detach_resultsž  s#   € ð
ð 
à!ð
ñ 
ô 
ð 	
rE   rq  r_  c          
     ó  — t          | j        j        ¦  «        s|                      d|dddd|¬¦  «         t                               ¦   «         D ]9\  }}|                     |¦  «        }|€Œ|                      ||dddd|¬¦  «         Œ:dS )a!  Log compact per-step convergence metrics for the progress bar only.

        Args:
            loss: Unscaled aggregate training loss.
            loss_dict: Raw criterion loss dictionary.
            batch_size: Current batch size used by Lightning for metric reduction metadata.
        rq  TF)r`  rÖ   r\  r]  r_  N)r;   ry   rz  r  r7   r}  r    )rî   rq  rS  r_  rX  Úprogress_namerY  s          rC   r€  z-RFDETRModelModule._log_train_progress_metrics­  sÅ   € õ( �DÔ%Ô7Ñ8Ô8ð 		Ø�HŠHØØØØØØØ%ð ñ ô ð õ )E×(JÒ(JÑ(LÔ(Lð 	ð 	Ñ$ˆI�}Ø—M’M )Ñ,Ô,ˆEØˆ}ØØ�HŠHØØØØØØØ%ð ñ ô ð ð ð		ð 	rE   c               ó¨   — |                       d„ |                     ¦   «         D ¦   «         ddd|¬¦  «         |                      d|ddd|¬¦  «         dS )a  Log aggregate and component validation losses.

        Args:
            loss: Aggregate weighted validation loss.
            loss_dict: Raw criterion loss dictionary.
            batch_size: Current batch size used by Lightning for metric reduction metadata.
        c                ó    — i | ]\  }}d |› �|“ŒS )zval/rP  ©rp   rR  ro  s      rC   rZ  z;RFDETRModelModule._log_val_loss_metrics.<locals>.<dictcomp>è  s$   € Ð9Ð9Ð9™t˜q !ˆZ�AˆZˆZ˜Ð9Ð9Ð9rE   FTr[  râ  )r`  r]  r^  r_  N)r~  r}  r  )rî   rq  rS  r_  s       rC   Ú_log_val_loss_metricsz'RFDETRModelModule._log_val_loss_metricsÙ  sm   € ð 	�ŠØ9Ð9 y§¢Ñ'8Ô'8Ð9Ñ9Ô9ØØØØ!ð 	ñ 	
ô 	
ð 	
ð 	�Š�˜T¨D¸4È4Ð\fˆÑgÔgÐgÐgÐgrE   r   c                óÒ   — | j         j        r| j        S 	 t          | j        dg ¦  «        }n# t
          $ r
 | j        cY S w xY w|D ]}t          |¦  «        }|�	|j        c S Œ| j        S )u-  Return the model to forward through for validation.

        Validation evaluates one model by default: the EMA-averaged weights when EMA is available,
        because those are the weights best-checkpoint selection ships. Forwarding through them here
        replaces the second, duplicate base+EMA forward pass ``COCOEvalCallback`` would otherwise run
        every validation batch (see issue 416). ``TrainConfig.eval_base_model`` opts back in to that
        comparison: the base model is forwarded here and ``COCOEvalCallback`` runs the EMA pass.

        Falls back to the base model when EMA is enabled but not yet warmed up (e.g. the very first
        validation epoch, before ``RFDETREMACallback.setup`` has built its averaged model), or when
        this module isn't attached to a ``Trainer`` at all (``LightningModule.trainer`` raises
        ``RuntimeError`` rather than returning ``None`` when unattached â€” e.g. ``validation_step``
        called directly outside ``Trainer.fit``/``Trainer.validate``). The same fallback covers
        ``use_ema=False``, where no EMA callback exists and the base model is the selected model.

        Uses the same ``_get_ema_inner_module`` helper as ``COCOEvalCallback`` (see
        ``coco_eval.py``) so both consumers resolve the EMA-averaged detection net through one
        shared code path instead of independently duck-typing ``RFDETREMACallback``.

        Returns:
            The EMA-averaged inner module's underlying detection net when it is the selected model and
            available, else the base model.
        Ú	callbacks)ry   Úeval_base_modelrÓ   rO   r  r  r   )rî   ró  ÚcallbackÚ	ema_inners       rC   Ú_resolve_eval_modelz%RFDETRModelModule._resolve_eval_modelð  sš   € ð0 ÔÔ,ð 	Ø”:Ðð	Ý ¤¨k¸2Ñ>Ô>ˆIˆIøÝð 	ð 	ð 	Ø”:ÐÐÐð	øøøà!ð 	'ð 	'ˆHÝ-¨hÑ7Ô7ˆIØÐ$Ø ”Ð&Ð&Ð&ð %àŒzÐs   •, ¬A ¿A r_   c                óÚ  ‡	‡
— |\  }}| j         rt          j                             ¦   «           |                      ¦   «         |¦  «        }| j        rc|                      ||¦  «        Š	| j        j        Š
t          ˆ	ˆ
fd„‰	D ¦   «         ¦  «        }|  	                    |‰	t          |¦  «        ¬¦  «         t          j        d„ |D ¦   «         ¦  «        }|                      ||¦  «        }||dœS )aË  Run forward pass and postprocess for one validation step.

        Returns raw results and targets so ``COCOEvalCallback`` can accumulate them across the epoch via
        ``on_validation_batch_end``.

        Args:
            batch: Tuple of (NestedTensor samples, list of target dicts).
            batch_idx: Batch index within the validation epoch.

        Returns:
            Dict with ``results`` (postprocessed predictions) and ``targets``.
        c              3  óB   •K  — | ]}|‰v ¯‰|         ‰|         z  V — Œd S rk   rP  rQ  s     €€rC   rr   z4RFDETRModelModule.validation_step.<locals>.<genexpr>*  ó;   øè è € Ð\Ð\¸È1ÐP[ÐK[ÐK[�y ”| k°!¤nÑ4ÐK[ÐK[ÐK[ÐK[Ð\Ð\rE   ra  c                ó   — g | ]
}|d          ‘ŒS rd  rP  rf  s     rC   rU  z5RFDETRModelModule.validation_step.<locals>.<listcomp>-  ó   € Ð!BÐ!BÐ!B°Q ! K¤.Ð!BÐ!BÐ!BrE   ©rr  rs  )rÐ   r¡   rÿ   rù   r÷  Ú_should_compute_val_lossrÙ   rT  rx  rñ  rt  rw  rÚ   ©rî   r)  r+  rD  rs  r‰  rq  r‘  rr  rS  rT  s            @@rC   Úvalidation_stepz!RFDETRModelModule.validation_step  sü   øø€ ð !Ñˆ�ØÔ$ð 	7õ ŒN×4Ò4Ñ6Ô6Ð6Ø,�$×*Ò*Ñ,Ô,¨WÑ5Ô5ˆØÔ(ð 	QØŸš w°Ñ8Ô8ˆIØœ.Ô4ˆKÝÐ\Ð\Ð\Ð\Ð\¸iÐ\Ñ\Ô\Ñ\Ô\ˆDØ×&Ò& t¨YÅ3ÀwÁ<Ä<Ð&ÑPÔPÐPå”[Ð!BÐ!B¸'Ð!BÑ!BÔ!BÑCÔCˆ
Ø×"Ò" 7¨JÑ7Ô7ˆØ"¨wÐ7Ð7Ð7rE   c                ó    — | j         dk    rdS 	 t          | j        dg ¦  «        }n# t          $ r Y dS w xY wt	          d„ |D ¦   «         ¦  «        S )aö  Return whether a configured scheduler or callback consumes ``val/loss``.

        ``_lr_scheduler_monitor`` is set only after a concrete ``ReduceLROnPlateau`` scheduler is instantiated. Callback
        inspection keeps the ``"auto"`` policy compatible with PTL-native checkpoint or early stopping callbacks
        supplied through ``build_trainer(..., callbacks=...)``.

        RF-DETR's own callbacks are not covered by the PTL-native ``monitor`` attribute alone: ``BestModelCallback`` and
        ``RFDETREarlyStopping`` track two metrics at once and keep their real targets in ``_monitor_regular`` /
        ``_monitor_ema`` (``RFDETREarlyStopping.monitor`` is a synthetic key it injects itself), so all three attributes
        are inspected.
        râ  Tró  Fc           	   3  ó~   K  — | ]8}d t          |dd¦  «        t          |dd¦  «        t          |dd¦  «        hv V — Œ9dS )râ  rä  NÚ_monitor_regularÚ_monitor_ema)rO   )rp   rõ  s     rC   rr   zBRFDETRModelModule._validation_loss_is_monitored.<locals>.<genexpr>D  sp   è è € ð 
ð 
ð ð å˜ )¨TÑ2Ô2Ý˜Ð"4°dÑ;Ô;Ý˜ .°$Ñ7Ô7ððð
ð 
ð 
ð 
ð 
ð 
rE   )rÉ   rO   r  r  rv   )rî   ró  s     rC   r	  z/RFDETRModelModule._validation_loss_is_monitored1  sƒ   € ð Ô%¨Ò3Ð3Ø�4ð	Ý ¤¨k¸2Ñ>Ô>ˆIˆIøÝð 	ð 	ð 	Ø�5�5ð	øøøåð 
ð 
ð &ð
ñ 
ô 
ñ 
ô 
ð 	
s   �& ¦
4³4c                óP   — | j         j        du rdS | j         j        dk    o| j        S )záResolve whether validation should calculate loss for the current configuration.

        Returns:
            ``True`` when the loss is requested outright, or when the ``"auto"`` policy finds a ``val/loss`` consumer.
        Tr½   )ry   r  r	  r  s    rC   rß  z2RFDETRModelModule._resolve_should_compute_val_lossN  s3   € ð ÔÔ-°Ð5Ð5Ø�4ØÔ Ô1°VÒ;ÐbÀÔ@bÐbrE   c                óF   — | j         �| j         S |                      ¦   «         S )a>  Return whether validation should calculate loss, reusing the resolution cached for this epoch.

        ``validation_step`` reads this once per batch while the ``"auto"`` policy resolution walks every configured
        callback, so ``on_validation_epoch_start`` resolves it once per validation epoch and caches the result here.
        The cache stays unset until that hook runs: a ``validation_step`` called directly, with no ``Trainer`` driving
        the loop, resolves the live configuration rather than reading a value frozen before the trainer was attached.
        )rÌ   rß  r  s    rC   rþ  z*RFDETRModelModule._should_compute_val_lossX  s(   € ð Ô*Ð6ØÔ2Ð2Ø×4Ò4Ñ6Ô6Ð6rE   c                óÈ   — | j         j        oVt          j                             ¦   «         o8t          j                             ¦   «         ot          | j        j        ¦  «        dv S )a…  Return whether the runtime would enable fused AdamW, ignoring optimizer choice.

        Captures only the hardware/precision preconditions (BF16 on CUDA), so the
        custom-optimizer path can tell whether a dropped ``fused_optimizer=True``
        would actually have mattered.

        Returns:
            ``True`` when fused AdamW is requested and the runtime supports it.
        >   Úbf16r"  r#  r!  )	rµ   Úfused_optimizerr¡   r¾   Úis_availableÚis_bf16_supportedr@   r  r  r  s    rC   Ú_fused_adamw_env_eligiblez+RFDETRModelModule._fused_adamw_env_eligiblee  s^   € ð ÔÔ-ð iÝ”
×'Ò'Ñ)Ô)ðiå”
×,Ò,Ñ.Ô.ðiõ �D”LÔ*Ñ+Ô+Ð/hÐhð		
rE   c                óF   — | j         sdS t          | j        j        ¦  «        S )aÜ  Return whether fused AdamW should be used for the current training configuration.

        Fused AdamW is only safe when the trainer's actual precision is a BF16 variant.  Checking GPU capability alone
        (``is_bf16_supported()``) is
        insufficient: on Ampere+ hardware that flag is always ``True`` even when
        the trainer is configured for ``32-true``, which causes a ``params, grads, exp_avgs, and exp_avg_sqs must have
        same dtype, device, and layout`` crash in DDP because gradient bucket views have non-matching strides in FP32.
        It additionally requires the built-in ``optimizer="adamw"`` selection: fused state normalization and gradient
        clipping are AdamW-specific and must not fire for custom optimizers.

        Returns:
            ``True`` when fused AdamW is requested, safe, and the built-in AdamW optimizer is selected.

        Examples:
            >>> from unittest.mock import patch
            >>> module = RFDETRModelModule.__new__(RFDETRModelModule)
            >>> module.model_config = type("Cfg", (), {"fused_optimizer": True})()
            >>> with patch("torch.cuda.is_available", return_value=False):
            ...     module._use_fused_optimizer
            False
        F)r  rD   ry   r8   r  s    rC   r  z&RFDETRModelModule._use_fused_optimizerw  s(   € ð. Ô-ð 	Ø�5Ý& tÔ'8Ô'BÑCÔCÐCrE   r   c                óÚ  — | j         }t          | j        |¦  «        }t          | j        d| j        ¦  «        }t          ||¦  «        }|j        }t          |¦  «        rW	 t          j	        j
        |f|j        |j        | j        dœ|j        ¤Ž}nì# t          $ r}t          d|› d�¦  «        |‚d}~ww xY w| j        r t"                               t&          |¦  «         t)          |t*          ¦  «        s3t          |dd¦  «        pt-          |¦  «        }t/          |||i ¦  «        }nWt1          |¦  «        r"t3          |¦  «        }	t5          |	|||¦  «        }n&t7          |¦  «        }
t/          |
|||j        ¦  «        }t9          dt;          |j        ¦  «        ¦  «        }t;          | j        j         ¦  «        }| j!        r%t9          dtE          j#        ||z  ¦  «        ¦  «        n|}t9          d||j$        z  ¦  «        }t;          ||j%        z  ¦  «        }|j&        }d}d}tO          |¦  «        rtQ          |||||¦  «        }�nŒt)          |t*          ¦  «        s3t          |dd¦  «        pt-          |¦  «        }tS          |||i ¦  «        }nntU          |¦  «        }|j+        }t)          | ,                    d	¦  «        tZ          ¦  «        rt]          |t_          ||¦  «        ¦  «        }tS          ||||¦  «        }|j0        }t)          |tb          ¦  «        rN|j2        }|d
k    r|j3        du rti          d¦  «        ‚d}|dk    r t"                               d|j%        ¦  «         nl|dk    r|}nLtE          j#        |j%        ¦  «        }|j%        dk    r(|dk     r"t"                               d|j%        ¦  «         d}|dk    rtk          |||¦  «        }|| _6        || _7        ||dœ}|�||d<   ||dœS )aï  Build the configured optimizer with layer-wise LR decay and scheduler.

        Uses ``trainer.estimated_stepping_batches`` for total step count so cosine annealing covers the full training
        run regardless of dataset size or accumulation settings.
        ``optimizer="adamw"`` keeps RF-DETR's fused torch AdamW path;
        other names can be loaded from ``pytorch-optimizer``.

        Returns:
            PTL optimizer config dict with optimizer and step-interval scheduler.
        Ú	_orig_mod)r{   r|   Úfusedz(Failed to initialize optimizer 'adamw': zF. Check optimizer_kwargs for arguments supported by torch.optim.AdamW.NÚ__qualname__r”   r¹   r�   râ  Fzžcompute_val_loss=False is incompatible with ReduceLROnPlateau monitoring 'val/loss'. Set compute_val_loss=True or 'auto', or select a metric that is produced.rÛ  r   zzwarmup_epochs=%s is ignored for ReduceLROnPlateau; a metric-driven scheduler cannot be composed with a linear warmup ramp.rm  zãwarmup_epochs=%s with lr_scheduler_interval='epoch' cannot form a gradual warmup ramp (epoch-granular warmup needs >= 2 epochs); skipping warmup. Use lr_scheduler_interval='step' for sub-epoch warmup, or set warmup_epochs >= 2.)r¦   Úintervalrä  )r8   rŸ   )8ry   r   rµ   rO   rÓ   r"   r8   rD   r¡   r¢   ÚAdamWr{   r|   r  r^   rd   r  rÖ   rç   Ú_FUSED_IGNORED_MSGr?   r@   Úreprrf   r   r   r   rX   r—   rŠ   r  r  Úestimated_stepping_batchesrÆ   r˜   ÚceilÚepochsÚwarmup_epochsrŸ   r   r¥   rˆ   r‚   rž   r    r  r$   r!   Úlr_scheduler_intervalr   Úlr_scheduler_monitorr  rL   r²   rÈ   rÉ   )rî   rB  ÚnsÚmodel_for_paramsr\   Úoptimizer_cfgr8   rW   Úcallable_nameÚnative_classrZ   r  Úmicrobatchesr‰   r‹   rŒ   Úscheduler_cfgr  rä  r¦   r„   Úscheduler_classr…   Úwarmup_unitsÚlr_scheduler_configs                            rC   Úconfigure_optimizersz&RFDETRModelModule.configure_optimizers’  sš  € ð ÔˆÝ$ TÔ%6¸Ñ;Ô;ˆõ
 # 4¤:¨{¸D¼JÑGÔGÐÝ$ RÐ)9Ñ:Ô:ˆàœˆå" =Ñ1Ô1ð 	ðÝ!œKÔ-Øðà”uØ!#¤ØÔ3ð	ð ð
 Ô)ðð �	�	øõ ð ð ð Ýð[¸sð [ð [ð [ñô ð ðøøøøðøøøð Ô-ð BÝ—’Õ1°=ÑAÔAÐAÝ˜m­SÑ1Ô1ð å '¨°~ÀtÑ LÔ LÐ cÕPTÐUbÑPcÔPc�Ý;¸MÈ=ÐZeÐgiÑjÔj�	�	Ý+¨MÑ:Ô:ð 	å2KÈMÑ2ZÔ2Z�Ý2°<ÀÐP[Ð]_Ñ`Ô`�	�	õ #:¸-Ñ"HÔ"H�Ý;Ø# ]°KÀÔATñô �	õ ˜q¥# bÔ&9Ñ":Ô":Ñ;Ô;ÐÝ˜4œ<ÔBÑCÔCˆð CGÔB_Ðq�C�•4”9˜\Ð,<Ñ<Ñ=Ô=Ñ>Ô>Ð>Ðeqð 	õ ˜a °´	Ñ!9Ñ:Ô:ˆÝ˜?¨RÔ-=Ñ=Ñ>Ô>ˆàœˆàˆØ"ˆÝ% mÑ4Ô4ð ;	Vå0°¸BÀÈ_Ð^jÑkÔkˆI‰Iå˜m­SÑ1Ô1ð yå!(¨¸ÈÑ!MÔ!MÐ!dÕQUÐVcÑQdÔQd�Ý;¸MÈ>Ð[dÐfhÑiÔi�	�	õ #:¸-Ñ"HÔ"H�Ø#%Ô#9Ð õ Ð.×2Ò2°;Ñ?Ô?ÅÑFÔFð Ý'HØ(Ý*¨2Ð/?Ñ@Ô@ñ(ô (Ð$õ <¸OÈ]Ð\eÐgwÑxÔx�	ØÔ/ˆHÝ˜)Õ%6Ñ7Ô7ð %VØÔ1�Ø˜jÒ(Ð(¨RÔ-@ÀEÐ-IÐ-IÝ$ðdñô ð ð #�Ø !Ò#Ð#Ý—N’NðAàÔ(ñô ð øð ˜vÒ%Ð%Ø#/�L�Lõ $(¤9¨RÔ-=Ñ#>Ô#>�LØÔ'¨!Ò+Ð+°¸qÒ0@Ð0@ÝŸšðlð Ô,ñ	ô ð ð ()˜Ø !Ò#Ð#Ý 1°)¸YÈÑ UÔ U�Ià&.ˆÔ#Ø%,ˆÔ"àCLÐZbÐ5cÐ5cÐØÐØ-4Ð 	Ñ*à"Ø/ð
ð 
ð 	
s   Á/B Â
B2ÂB-Â-B2NrË  úfloat | NonerÌ  c                óè   •— | j         rE|r?|dk    r;t          j        j                             |                      ¦   «         |¦  «         dS dS dS t          ¦   «                              |||¬¦  «         dS )a‹  Override PTL gradient clipping to support fused AdamW.

        PTL's AMP precision plugin refuses to clip gradients when the optimizer declares it handles unscaling internally
        (fused=True).  When fused is active we are on BF16 (no GradScaler) so ``clip_grad_norm_`` is correct.  For the
        non-fused path (FP16 + GradScaler or FP32) we delegate to ``super()`` to preserve scaler-aware unscaling.

        Args:
            optimizer: The current optimizer.
            gradient_clip_val: Maximum gradient norm.
            gradient_clip_algorithm: Clipping algorithm; forwarded to super()
                for the non-fused path.
        r   rÍ  N)r  r¡   ÚnnÚutilsÚclip_grad_norm_rt   rÄ   rÏ  )rî   r8   rË  rÌ  rö   s       €rC   rÏ  z RFDETRModelModule.clip_gradients*  s—   ø€ ð$ Ô$ð 
	Ø ð UÐ%6¸Ò%:Ð%:Ý””×.Ò.¨t¯ªÑ/@Ô/@ÐBSÑTÔTÐTÐTÐTðUð UÐ%:Ð%:õ
 ‰GŒG×"Ò"ØØ"3Ø(?ð #ñ ô ð ð ð rE   c           	     ó~  — d}| j         D �]1}|d         D �]$}| j                             |¦  «        }|sŒ t          |                     ¦   «         ¦  «        D ]â\  }}t          |t          j        ¦  «        r|                     ¦   «         sŒ4|j	        |j	        k    rŒE|j
        |j
        k    r;|j        |j        k    r+|                     ¦   «         |                     ¦   «         k    rŒ�t          j        |¦  «        }|                     |                     |j
        |j        ¬¦  «        ¦  «         |||<   |dz  }Œã�Œ&�Œ3|S )aH  Cast restored floating-point optimizer state tensors to match live parameters.

        Args:
            optimizer: AdamW optimizer whose state may have been rehydrated with a mismatched dtype or layout.

        Returns:
            Number of state tensors that were reallocated to match the current parameter layout.
        r   Úparamsr¹  r”   )rÆ  Ústater    r  r}  r?   r¡   r   Úis_floating_pointÚshaper&  rº  ÚstrideÚ
empty_likeÚcopy_r½  )r8   Ú
normalizedÚgroupÚparamr.  r®  rY  Úrestoreds           rC   r  z,RFDETRModelModule._normalize_optimizer_stateH  sM  € ð ˆ
ØÔ+ð 	$ñ 	$ˆEØ˜xœð $ñ $�Ø!œ×+Ò+¨EÑ2Ô2�Øð ØÝ"& u§{¢{¡}¤}Ñ"5Ô"5ð 
$ð 
$‘J�C˜Ý% e­U¬\Ñ:Ô:ð !À%×BYÒBYÑB[ÔB[ð !Ø Ø”{ e¤kÒ1Ð1Ø Ø”| u¤|Ò3Ð3¸¼ÀuÄ{Ò8RÐ8RÐW\×WcÒWcÑWeÔWeÐin×iuÒiuÑiwÔiwÒWwÐWwØ Ý$Ô/°Ñ6Ô6�HØ—N’N 5§8¢8°5´<ÀuÄ{ 8Ñ#SÔ#SÑTÔTÐTØ!)�E˜#‘JØ !‘O�J�Jñ
$ñ	$ð ÐrE   c                óŒ  ‡	‡
— |\  }}|                       |¦  «        }| j        j        rd|                      ||¦  «        Š	| j        j        Š
t          ˆ	ˆ
fd„‰	D ¦   «         ¦  «        }|                      d|dt          |¦  «        ¬¦  «         t          j	        d„ |D ¦   «         ¦  «        }|  
                    ||¦  «        }||dœS )a*  Run forward pass and postprocess for one test step.

        Mirrors :meth:`validation_step` so ``COCOEvalCallback`` can accumulate results via ``on_test_batch_end`` when
        ``trainer.test()`` is called (e.g. from :class:`~rfdetr.training.callbacks.BestModelCallback` at end of
        training).

        Args:
            batch: Tuple of (NestedTensor samples, list of target dicts).
            batch_idx: Batch index within the test epoch.

        Returns:
            Dict with ``results`` (postprocessed predictions) and ``targets``.
        c              3  óB   •K  — | ]}|‰v ¯‰|         ‰|         z  V — Œd S rk   rP  rQ  s     €€rC   rr   z.RFDETRModelModule.test_step.<locals>.<genexpr>x  rú  rE   z	test/lossT)r^  r_  c                ó   — g | ]
}|d          ‘ŒS rd  rP  rf  s     rC   rU  z/RFDETRModelModule.test_step.<locals>.<listcomp>{  rü  rE   rý  )rÓ   ry   Úcompute_test_lossrÙ   rT  rx  r  rt  r¡   rw  rÚ   rÿ  s            @@rC   Ú	test_stepzRFDETRModelModule.test_stepe  sÓ   øø€ ð !Ñˆ�Ø—*’*˜WÑ%Ô%ˆØÔÔ.ð 	QØŸš w°Ñ8Ô8ˆIØœ.Ô4ˆKÝÐ\Ð\Ð\Ð\Ð\¸iÐ\Ñ\Ô\Ñ\Ô\ˆDØ�HŠH�[ $°$Å3ÀwÁ<Ä<ˆHÑPÔPÐPå”[Ð!BÐ!B¸'Ð!BÑ!BÔ!BÑCÔCˆ
Ø×"Ò" 7¨JÑ7Ô7ˆØ"¨wÐ7Ð7Ð7rE   r   Údataloader_idxc                óô   — |\  }}t          j        ¦   «         5  |                      |¦  «        }ddd¦  «         n# 1 swxY w Y   t          j        d„ |D ¦   «         ¦  «        }|                      ||¦  «        S )a^  Run inference on a preprocessed batch and return postprocessed results.

        Args:
            batch: Tuple of (NestedTensor samples, list of target dicts).
            batch_idx: Batch index.
            dataloader_idx: Index of the predict dataloader.

        Returns:
            Postprocessed detection results from ``PostProcess``.
        Nc                ó   — g | ]
}|d          ‘ŒS rd  rP  rf  s     rC   rU  z2RFDETRModelModule.predict_step.<locals>.<listcomp>�  rü  rE   )r¡   r;  rÓ   rw  rÚ   )rî   r)  r+  r=  rD  rs  r‰  r‘  s           rC   Úpredict_stepzRFDETRModelModule.predict_step  s´   € ð !Ñˆ�ÝŒ]‰_Œ_ð 	*ð 	*Ø—j’j Ñ)Ô)ˆGð	*ð 	*ð 	*ñ 	*ô 	*ð 	*ð 	*ð 	*ð 	*ð 	*ð 	*øøøð 	*ð 	*ð 	*ð 	*å”[Ð!BÐ!B¸'Ð!BÑ!BÔ!BÑCÔCˆ
Ø×Ò ¨Ñ4Ô4Ð4s   ™;»?Á?Ú
checkpointc                óp  — d|v r+d|vr'd„ |d                               ¦   «         D ¦   «         |d<   d|v r t          |d         | j        j        ¦  «         d|v r d„ |d         D ¦   «         }|D ]}|d         |= Œt	          |¦  «         d|v r+|d         | _        t          j        dt          d¬¦  «         d	S d	S )
uÜ
  Auto-detect legacy formats and reconcile PE shapes at checkpoint load time.

        PTL calls this hook before applying ``checkpoint["state_dict"]`` to the module.  Four normalisation steps are
        applied in order:

        1. **Raw legacy format** â€” a ``*.pth`` file loaded directly by
           ``Trainer`` (e.g. via ``ckpt_path=``).  Recognised by the presence of ``"model"`` without ``"state_dict"``.
           The state dict is rewritten in-place with the ``"model."`` prefix so PTL can apply it normally.

        2. **Positional-embedding interpolation** â€” when the checkpoint was
           saved at a different image resolution than the current model, the DINOv2 ``position_embeddings`` tensor shape
           will mismatch. :func:`~rfdetr.models.weights.interpolate_position_embeddings` is called to bicubic-resize the
           PE to ``model_config.positional_encoding_size`` before PTL applies the state dict.  Regression fix for
           :issue:`998`.

        3. **FP8 extra-state removal** â€” under Transformer Engine (``amp_dtype="fp8"``), the live module's
           ``state_dict()`` carries ``_extra_state`` entries recording FP8 scaling history. Since ``strict_loading``
           is disabled on this module, ``load_state_dict()`` tolerates a missing or unexpected key afterward, but
           that does not stop it from calling ``set_extra_state()`` for any such key present in both the checkpoint
           and the module â€”
           and Transformer Engine intentionally rejects pickle-deserialised extra state. The entries are removed from
           ``checkpoint["state_dict"]`` here so the full-checkpoint resume path (``Trainer(ckpt_path=...)``) cannot
           abort on them, mirroring :meth:`~rfdetr.training.callbacks.ema.RFDETREMACallback._without_extra_state`.

        4. **Converted format** â€” a file produced by
           :func:`~rfdetr.training.checkpoint.convert_legacy_checkpoint` that already has ``"state_dict"`` but also
           carries ``"legacy_ema_state_dict"``.  The EMA weights are stashed on ``self._pending_legacy_ema_state`` for
           optional restoration by :class:`~rfdetr.training.callbacks.ema.RFDETREMACallback`.

        Note:
            This hook only fires on ``Trainer(ckpt_path=...)`` resume paths. Fresh-train bootstrap from a
            ``pretrain_weights`` checkpoint runs through :func:`~rfdetr.models.weights.load_pretrain_weights` during
            ``__init__`` instead â€” that helper performs its own PTL ``.ckpt`` normalisation (``state_dict`` â†’ ``model``
            key, ``_orig_mod`` strip) and PE interpolation, so the two code paths intentionally do not share state.

        Args:
            checkpoint: Checkpoint dict passed in by PTL (mutated in-place).
        rÓ   Ú
state_dictc                ó    — i | ]\  }}d |z   |“ŒS )zmodel.rP  rð  s      rC   rZ  z8RFDETRModelModule.on_load_checkpoint.<locals>.<dictcomp>¹  s"   € Ð'`Ð'`Ð'`¹D¸A¸q¨°1©°aÐ'`Ð'`Ð'`rE   c                óR   — g | ]$}|                      d d¦  «        d         dk    ¯"|‘Œ%S )r=   r”   éÿÿÿÿÚ_extra_state)Úrsplit)rp   r®  s     rC   rU  z8RFDETRModelModule.on_load_checkpoint.<locals>.<listcomp>Ë  s9   € ÐtÐtÐt¨È3Ï:Ê:ÐVYÐ[\ÑK]ÔK]Ð^`ÔKaÐesÒKsÐKs ÐKsÐKsÐKsrE   Úlegacy_ema_state_dictz²Checkpoint contains legacy EMA weights (`legacy_ema_state_dict`). Add RFDETREMACallback to your trainer callbacks to restore them; without it the stashed weights will be ignored.rm  )Ú
stacklevelN)	r}  r   rµ   Úpositional_encoding_sizer#   Ú_pending_legacy_ema_stateÚwarningsÚwarnÚUserWarning)rî   rA  Úextra_state_keysr®  s       rC   Úon_load_checkpointz$RFDETRModelModule.on_load_checkpoint�  s  € ðP �jÐ Ð  \¸Ð%CÐ%CØ'`Ð'`ÀJÈwÔDW×D]ÒD]ÑD_ÔD_Ð'`Ñ'`Ô'`ˆJ�|Ñ$ð ˜:Ð%Ð%Ý+Ø˜<Ô(ØÔ!Ô:ñô ð ð ˜:Ð%Ð%ØtÐt¨z¸,Ô/GÐtÑtÔtÐØ'ð 2ð 2�Ø˜|Ô,¨SÐ1Ð1õ
 	)¨Ñ4Ô4Ð4ð # jÐ0Ð0Ø-7Ð8OÔ-PˆDÔ*ÝŒMðBõ Øðñ ô ð ð ð ð 1Ð0rE   Únum_classesc                ó:   — | j                              |¦  «         dS )z•Reinitialize the detection head for a new class count.

        Args:
            num_classes: New number of classes (excluding background).
        N)rÓ   Úreinitialize_detection_head)rî   rR  s     rC   rT  z-RFDETRModelModule.reinitialize_detection_headà  s    € ð 	Œ
×.Ò.¨{Ñ;Ô;Ð;Ð;Ð;rE   )rµ   r   ry   r   r:   r¶   )rµ   r   ry   r   r:   r÷   )r:   r¶   )r)  r*  r+  rŠ   r:   r¶   )r)  r*  r+  rŠ   r:   rM  )rS  r”  rT  r•  r:   r–  )rS  r”  rT  r•  r:   r”  )r‰  r”  rs  r¨  r:   r©  )rŠ  r   r‹  r   r:   r   )rF  r   r:   r¶   )r+  rŠ   r:   r;   )r8   rÂ  r:   r¶   )r:   rÒ  )r8   r`   r:   r¶   )rr  r¨  r:   r¨  )rq  r   rS  r”  r_  rŠ   r:   r¶   )r:   r   )r)  r*  r+  rŠ   r:   r_   )r:   r;   )r:   r   )NN)r8   rÂ  rË  r'  rÌ  r÷   r:   r¶   )r8   r`   r:   rŠ   )r   )r)  r*  r+  rŠ   r=  rŠ   r:   r   )rA  r_   r:   r¶   )rR  rŠ   r:   r¶   ).r±  Ú
__module__r  Ú__doc__rÅ   Ústaticmethodræ   r  r  r  rG  rL  r“  r¢  r|  ru  rv  rµ  r�  rÉ  r„  rÖ  rÐ  rÙ  rÜ  rà  ræ  rˆ  r€  rñ  r÷  r   Úpropertyr	  rß  rþ  r  r  r&  rÏ  r  r<  r@  rQ  rT  Ú__classcell__)rö   s   @rC   r´   r´   g  së  ø€ € € € € ðð ða(ð a(ð a(ð a(ð a(ð a(ðF ð!ð !ð !ñ „\ð!ðNUð Uð Uð Uð$&ð &ð &ð &ðPW
ð W
ð W
ð W
ðrð ð ð ð40ð 0ð 0ð 0ð6lHð lHð lHð lHð\)ð )ð )ð )ð<"ð "ð "ð "ð2#/ð #/ð #/ð #/ðJ1ð 1ð 1ð 1ð,hð hð hð hð
ð 
ð 
ð 
ð>_ð _ð _ð _ð0ð 0ð 0ð 0ð6ð ð ð ðð ð ð ð
,ð 
,ð 
,ð 
,ðð ð ð ð
ð 
ð 
ð 
ð4ð ð ð ð8 ð
ð 
ð 
ñ „\ð
ð*ð *ð *ð *ðXhð hð hð hð."ð "ð "ð "ðH8ð 8ð 8ð 8ð: ð
ð 
ð 
ñ „Xð
ð8cð cð cð cð ð
7ð 
7ð 
7ñ „Xð
7ð ð
ð 
ð 
ñ „Xð
ð" ðDð Dð Dñ „XðDð4V
ð V
ð V
ð V
ðv +/Ø.2ð	ð ð ð ð ð ð ð< ðð ð ñ „\ðð88ð 8ð 8ð 8ð45ð 5ð 5ð 5ð 5ð"Nð Nð Nð Nð`<ð <ð <ð <ð <ð <ð <ð <rE   r´   )r8   r9   r:   r;   )rF   r@   r:   rG   )
rZ   rG   r[   r@   r\   r]   r^   r_   r:   r`   )rZ   rG   rh   r@   r:   r;   )
rZ   rG   r[   r@   r\   r]   ry   r   r:   r`   )rF   r@   r:   r€   )
rƒ   r€   r„   r@   r8   r`   r…   r_   r:   r†   )r8   r`   ry   r   r‰   rŠ   r‹   rŠ   rŒ   rŠ   r:   r�   )r¦   r§   r8   r`   rŒ   rŠ   r:   r¨   )QrV  Ú
__future__r   rM   rm   r˜   r8  rM  Ú
contextlibr   Útypingr   r   r   r¡   Útorch.nn.functionalr)  Ú
functionalr<  Úpytorch_lightningr   r	   Ú pytorch_lightning.core.optimizerr
   Ú!pytorch_lightning.utilities.typesr   r   r   Útorch.optim.lr_schedulerr   r   Úrfdetr._namespacer   rÛ   r   r   r   r   r   r   r   Úrfdetr.datasets.cocor   Úrfdetr.models.lwdetrr   r   Úrfdetr.models.matcherr   Úrfdetr.models.weightsr   r   r   Ú#rfdetr.training.callbacks.coco_evalr   Úrfdetr.training.cuda_graph_stepr    Úrfdetr.training.param_groupsr!   r"   r#   r$   Úrfdetr.utilities.loggerr%   rÖ   r¢   r  rG   r7   Ú__annotations__rD   r  rX   rf   rw   r   r€   r‚   rˆ   r¥   r²   r´   rP  rE   rC   ú<module>rm     sÍ  ðð ;Ð :Ð :à "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø €€€Ø €€€Ø €€€Ø €€€Ø "Ð "Ð "Ð "Ð "Ð "Ø &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &à €€€Ø Ð Ð Ð Ð Ð Ð Ð Ð Ø >Ð >Ð >Ð >Ð >Ð >Ð >Ð >Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø _Ð _Ð _Ð _Ð _Ð _Ð _Ð _Ø Ð Ð Ð Ð Ð Ø CÐ CÐ CÐ CÐ CÐ CÐ CÐ Cà 5Ð 5Ð 5Ð 5Ð 5Ð 5ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð <Ð ;Ð ;Ð ;Ð ;Ð ;Ø UÐ UÐ UÐ UÐ UÐ UÐ UÐ UØ 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø dÐ dÐ dÐ dÐ dÐ dÐ dÐ dÐ dÐ dØ EÐ EÐ EÐ EÐ EÐ EØ CÐ CÐ CÐ CÐ CÐ Cðð ð ð ð ð ð ð ð ð ð ð ð /Ð .Ð .Ð .Ð .Ð .à	ˆ‰Œ€à˜S %¤+Ô"7Ð7Ô8Ð ð ØØØØ!Ø Ø(Ø&Ø"ð
0ð 
0Ð ð 
ð 
ð 
ñ 
ðhð hð hð hð&'ð ðð ð ð ð4!ð !ð !ð !ðH(ð (ð (ð (ð6$ð $ð $ð $ðN ˜S +Ð0AÑ"AÐAÔBÐ ðð ð ð ð4 ð  ð  ð  ðF)Mð )Mð )Mð )MðXð ð ð ð@<ð <ð <ð <ð <˜ñ <ô <ð <ð <ð <rE   