§
    ­¸j|N  ã                  ó    — d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
  e
¦   «         ZdZd$d„Zd%d&d„Zd'd„Z	 d%d(d„Zd)d„Zd*d!„Zd+d#„ZdS ),z"Checkpoint and state-dict helpers.é    )ÚannotationsN)ÚOrderedDict)ÚAny)Ú
get_logger)Ú
state_dictÚglobal_stepzpytorch-lightning_versionÚloopsÚ	callbacksÚoptimizer_statesÚlr_schedulersÚckpt_patch_sizeÚintÚmodel_patch_sizeÚreturnÚNonec                ó.   — t          d| › d|› d�¦  «        ‚)uH  Raise a descriptive ValueError for a patch_size incompatibility.

    Args:
        ckpt_patch_size: patch_size recorded in (or inferred from) the checkpoint.
        model_patch_size: patch_size the current model is configured with.

    Raises:
        ValueError: Always â€” describes the mismatch and how to resolve it.
    z+The checkpoint was trained with patch_size=z(, but the current model uses patch_size=zë. The checkpoint is incompatible with this model architecture. To resolve this, either instantiate/configure the model with the checkpoint's patch_size or use a checkpoint that was trained with the same patch_size as the current model.)Ú
ValueError)r   r   s     úk/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/utilities/state_dict.pyÚ_raise_patch_size_mismatchr   &   s?   € õ ð	[°oð 	[ð 	[Ø&ð	[ð 	[ð 	[ñô ð ó    Úargsr   ÚfieldÚstrÚdefaultc                óz   — t          | t          ¦  «        r|                      ||¦  «        S t          | ||¦  «        S )ad  Get a field from checkpoint ``"args"``, handling both dict and attribute access.

    New checkpoints (PTL training stack) store ``"args"`` as a plain ``dict`` (via ``TrainConfig.model_dump()``).
    Legacy checkpoints (pre-PTL engine or the pre-release PTL code) stored it as a ``Namespace``-like object.  This
    helper abstracts both so callers do not need to branch on the type.

    Args:
        args: The ``checkpoint["args"]`` value.
        field: Field name to retrieve.
        default: Value returned when the field is absent.

    Returns:
        The field value, or ``default`` if not found.
    )Ú
isinstanceÚdictÚgetÚgetattr)r   r   r   s      r   Ú_ckpt_args_getr    8   s<   € õ �$�ÑÔð (Ø�xŠx˜˜wÑ'Ô'Ð'Ý�4˜ Ñ(Ô(Ð(r   Úepochúdict[str, Any]c                óÎ   — | dz   }dddddœ}ddddœ}dddœ}||||dœ}i ddii |¥i |¥ddœi |¥i |¥d	œi d
i |¥i |¥d	œi |¥i |¥d	œdœii i |¥i |¥d	œi i |¥i |¥ddœi |¥i |¥d	œdœS )u�  Build a minimal ``fit_loop`` state dict that restores the epoch counter.

    ``BestModelCallback`` stores ``trainer.current_epoch`` as ``"epoch"`` in the checkpoint.  That value is captured
    during ``on_validation_end``, which fires *before* the loop's epoch-end hooks increment the counter.  To resume
    training *after* that epoch, PTL's epoch-progress counter must be set to ``epoch + 1`` so that
    ``trainer.current_epoch == epoch + 1`` when the new ``trainer.fit()`` call begins.

    Optimizer and scheduler states are intentionally omitted â€” loading a ``.pth`` file starts a fresh optimizer for the
    new training phase.

    Args:
        epoch: The ``"epoch"`` value from the checkpoint (``trainer.current_epoch``
            at the time of ``on_validation_end``).

    Returns:
        A ``fit_loop`` state dict compatible with :meth:`pytorch_lightning.loops._FitLoop.load_state_dict`.
    é   r   )ÚreadyÚstartedÚ	processedÚ	completed)r%   r&   r(   )r%   r(   Ú_batches_that_steppedF)ÚtotalÚcurrentÚis_last_batch)r*   r+   Ú	optimizer)ÚstepÚ	zero_grad)r   zepoch_loop.state_dictzepoch_loop.batch_progresszepoch_loop.scheduler_progressz,epoch_loop.automatic_optimization.state_dictz0epoch_loop.automatic_optimization.optim_progressz)epoch_loop.manual_optimization.state_dictz2epoch_loop.manual_optimization.optim_step_progresszepoch_loop.val_loop.state_dictz"epoch_loop.val_loop.batch_progressÚepoch_progress© )r!   ÚnÚzero4Úzero3Úzero2Ún4s         r   Ú_make_fit_loop_stater7   L   s;  € ð$ 	�‰	€AØ A°AÀAÐFÐF€EØ A°AÐ6Ð6€EØ aÐ(Ð(€EØ °ÀÐ	CÐ	C€BàØ"9¸1Ð!=à˜�YØ ˜%�yØ"ð&
ð &
ð ˜�YØ ˜%�yð*
ð *
ð 9;àà& ˜YØ( %˜yðð ð
 ' ˜YØ( %˜yðð ð	ð 	ð=
ð 68à˜�YØ ˜%�yð?
ð ?
ð +-à˜�YØ ˜%�yØ"ð/
ð /
ð ˜�VØ˜"�vð
ð 
ðI(ð (ð (r   Ú
checkpointústr | os.PathLike[str]Úextra_metadataúdict[str, Any] | Nonec                ó‚  — ddl m} ddl}ddlm}  | || ¦  «        d¬¦  «        }|d         |d         d	œ}d
|v r|d
         |d
<   d|v r|d         |d<   t
          D ]}||v r||         ||<   Œd|v r|d         }dD ]}	|	|vrdi i||	<   Œ|r|                     |¦  «         t          j         	                    t          j         
                    t          j        | ¦  «        ¦  «        ¦  «        }
t          j        |
d¬¦  «        5 }|j        }ddd¦  «         n# 1 swxY w Y   	 |                     ||¦  «         t          j        || ¦  «         t          j                             |¦  «        rt          j        |¦  «         dS dS # t          j                             |¦  «        rt          j        |¦  «         w w xY w)a~  Strip a checkpoint file down to ``model``, ``args``, and PTL-compatible keys.

    Preserves ``model_name`` (when present) so that ``RFDETR.from_checkpoint()`` can still resolve the model class from
    the stripped file.  Also preserves ``rfdetr_version`` (when present) for provenance tracking.

    Also preserves ``state_dict``, ``global_step``, ``pytorch-lightning_version``, ``loops``, ``optimizer_states``, and
    ``lr_schedulers`` when present so the stripped checkpoint can still be used directly with
    ``trainer.fit(ckpt_path=...)``.

    Overwrites the file atomically so a partial write cannot corrupt it.

    Args:
        checkpoint: Path to the ``.pth`` checkpoint file to strip in place.
        extra_metadata: Optional key/value pairs merged into the stripped checkpoint. Used to record provenance that is
            not derivable from the surviving keys (e.g. ``{"best_total_source": "ema"}`` for
            ``checkpoint_best_total.pth``). Keys overwrite same-named preserved keys.

    Examples:
        >>> import tempfile
        >>> import torch
        >>> path = tempfile.NamedTemporaryFile(suffix=".pth", delete=False).name
        >>> torch.save({"model": {}, "args": {}, "optimizer_states": [1]}, path)
        >>> strip_checkpoint(path, extra_metadata={"best_total_source": "ema"})
        >>> torch.load(path, weights_only=False)["best_total_source"]
        'ema'
    r   )ÚPathN)Ú_safe_torch_loadT)ÚtrustÚmodelr   )r@   r   Ú
model_nameÚrfdetr_versionr	   )Úvalidate_loopÚ	test_loopr   F)ÚdirÚdelete)Úpathlibr=   ÚtorchÚrfdetr.utilities.ior>   Ú_PTL_COMPAT_KEYSÚupdateÚosÚpathÚdirnameÚabspathÚfspathÚtempfileÚNamedTemporaryFileÚnameÚsaveÚreplaceÚexistsÚremove)r8   r:   r=   rH   r>   r   Únew_state_dictÚkeyr	   Úloop_keyÚcheckpoint_dirÚtmp_fileÚtmp_paths                r   Ústrip_checkpointr^   Ž   sW  € ð< ÐÐÐÐÐà€L€L€Là4Ð4Ð4Ð4Ð4Ð4ð "Ð! $ $ zÑ"2Ô"2¸$Ð?Ñ?Ô?€Jà˜GÔ$Ø˜6Ô"ðð €Nð
 �zÐ!Ð!Ø'1°,Ô'?ˆ�|Ñ$à˜:Ð%Ð%Ø+5Ð6FÔ+GˆÐ'Ñ(åð 2ð 2ˆØ�*ÐÐØ",¨S¤/ˆN˜3Ñøð �.Ð Ð Ø˜wÔ'ˆØ6ð 	5ð 	5ˆHØ˜uÐ$Ð$Ø#/°Ð"4��h‘øàð .Ø×Ò˜nÑ-Ô-Ð-å”W—_’_¥R¤W§_¢_µR´Y¸zÑ5JÔ5JÑ%KÔ%KÑLÔL€NÝ	Ô	$¨ÀÐ	FÑ	FÔ	Fð !È(Ø”=ˆð!ð !ð !ñ !ô !ð !ð !ð !ð !ð !ð !øøøð !ð !ð !ð !ð Ø�
Š
�> 8Ñ,Ô,Ð,å
Œ
�8˜ZÑ(Ô(Ð(åŒ7�>Š>˜(Ñ#Ô#ð 	 ÝŒI�hÑÔÐÐÐð	 ð 	 ø�2Œ7�>Š>˜(Ñ#Ô#ð 	 ÝŒI�hÑÔÐÐð	 øøøs   ÄDÄDÄ!DÄ&+F Æ6F>r   úOrderedDict[str, Any]c                ó�   — t          ¦   «         }|                      ¦   «         D ]"\  }}|dd…         dk    r
|dd…         }|||<   Œ#|S )a
  Remove the ``module.`` prefix added by ``DataParallel`` / ``DistributedDataParallel``.

    Args:
        state_dict: State dict potentially containing ``module.``-prefixed keys.

    Returns:
        New ``OrderedDict`` with ``module.`` stripped from all keys.
    Né   zmodule.)r   Úitems)r   rX   ÚkÚvs       r   Úclean_state_dictre   Þ   s_   € õ !‘]”]€NØ× Ò Ñ"Ô"ð ð ‰ˆˆ1ØˆRˆaˆRŒ5�IÒÐØ�!�"�"”ˆAØˆ�qÑÐØÐr   r@   Úobjectc                ó  — t          |d¦  «        r|j        d         nd}|€| S t          |dd¦  «        du}t          t          |dd¦  «        ¦  «        p|}|s| S t	          d„ | D ¦   «         ¦  «        r| S d„ |                      ¦   «         D ¦   «         }|s| S t                               d	t          |¦  «        ¦  «         |                     ¦   «         D ]3\  }}| 	                    d
dd¦  «        }| 
                    ¦   «         | |<   Œ4| S )a7  Clone backbone projector weights into ``cross_attn_projector`` for dual-projector models.

    Older checkpoints can contain only ``backbone.0.projector.*`` keys. Dual-projector models expect an additional
    ``backbone.0.cross_attn_projector.*`` branch; this helper seeds it by cloning projector weights when missing.

    Args:
        state_dict: Checkpoint model-state dictionary (mutated in place).
        model: Instantiated model used to detect whether dual projector mode is active.

    Returns:
        The same ``state_dict`` object for convenience.
    Úbackboner   NÚcross_attn_projectorÚdual_projectorFc              3  ó@   K  — | ]}|                      d ¦  «        V — ŒdS )ú backbone.0.cross_attn_projector.N©Ú
startswith)Ú.0rY   s     r   ú	<genexpr>z0remap_projector_to_cross_attn.<locals>.<genexpr>  s/   è è € Ð
TÐ
TÀ#ˆ3�>Š>Ð<Ñ=Ô=Ð
TÐ
TÐ
TÐ
TÐ
TÐ
Tr   c                óD   — i | ]\  }}|                      d ¦  «        ¯||“ŒS )úbackbone.0.projector.rm   )ro   rY   Úvalues      r   ú
<dictcomp>z1remap_projector_to_cross_attn.<locals>.<dictcomp>	  s0   € ÐqÐqÐq¡Z S¨%ÈÏÊÐXoÑIpÔIpÐq�c˜5ÐqÐqÐqr   z`Cloning %d backbone projector key(s) into cross_attn_projector for dual-projector compatibility.rr   rl   r$   )Úhasattrrh   r   ÚboolÚanyrb   ÚloggerÚinfoÚlenrU   Úclone)	r   r@   rh   Úhas_cross_attn_projectorÚdual_projector_enabledÚprojector_keysrY   rs   Únew_keys	            r   Úremap_projector_to_cross_attnr€   ï   sD  € õ %,¨E°:Ñ$>Ô$>ÐHˆuŒ~˜aÔ Ð ÀD€HØÐØÐõ  ' xÐ1GÈÑNÔNÐVZÐZÐÝ!¥'¨(Ð4DÀeÑ"LÔ"LÑMÔMÐiÐQiÐØ!ð ØÐå
Ð
TÐ
TÈÐ
TÑ
TÔ
TÑTÔTð ØÐàqÐq°:×3CÒ3CÑ3EÔ3EÐqÑqÔq€NØð ØÐå
‡K‚KØjÝˆNÑÔñô ð ð %×*Ò*Ñ,Ô,ð ,ð ,‰
ˆˆUØ—+’+Ð5Ð7YÐ[\Ñ]Ô]ˆØ#Ÿkšk™mœmˆ
�7ÑÐàÐr   Ú
model_argsc                ó°  — |                       di ¦  «                              dd¦  «        }|�z|j        d         }t          |dd¦  «        }|�Z||dz   k    rQ|dz   |k     r!t                               d|dz
  ||¦  «         n't                               d|dz
  ||dz
  |dz
  ¦  «         |                       d	¦  «        }d}|�t          |d
¦  «        }|€¡d}|                       di ¦  «                              |¦  «        }t          |dd¦  «        }	|	�ct          |	¦  «        dk    rP|	d         |	d         k    r>t          |	d         ¦  «        }
t          |d
d¦  «        }|�|
|k    rt          |
|¦  «         d	| vrdS | d	         }t          |d¦  «        }t          |dd¦  «        }|�(|�&||k    r |rt          d¦  «        ‚t          d¦  «        ‚t          |d
¦  «        }t          |d
d¦  «        }|�|�||k    rt          ||¦  «         t          |d¦  «        }t          |dd¦  «        }|�(|�(||k    r$|rt          d¦  «        ‚t          d¦  «        ‚dS dS dS )uy  Validate that a checkpoint is compatible with the model configuration.

    Checks for mismatches in ``segmentation_head`` and ``patch_size`` between the checkpoint's saved training arguments
    and the current model configuration. Raises a descriptive :class:`ValueError` before ``load_state_dict`` fires so
    that users receive a clear, actionable message instead of a cryptic tensor size mismatch error.

    If either side is missing an attribute (e.g. a legacy checkpoint saved before
        ``segmentation_head`` or ``patch_size`` was added to ``args``), that specific
        check is skipped silently â€” this preserves backwards compatibility with pre-existing checkpoints.

    Args:
        checkpoint: Loaded checkpoint dictionary, expected to contain an optional
            ``"args"`` key with training namespace attributes or a plain dict.
        model_args: Namespace (e.g. ``types.SimpleNamespace``) with at least
            ``segmentation_head`` and ``patch_size`` attributes describing the current model.

    Raises:
        ValueError: If ``segmentation_head`` or ``patch_size`` in the checkpoint
            args do not match those of the model, or if the ``patch_size`` inferred from the DINOv2 projection weight
            shape differs from ``model_args.patch_size`` when no explicit ``args.patch_size`` is present.

    Note:
        This helper does not mutate ``model_args``. It emits ``logger.warning`` (not an exception) for class-count
        mismatches so that callers can still proceed with reinitialization or weight loading.

        When ``"args"`` is absent or ``args.patch_size`` is not set, a fallback infers ``patch_size`` from the DINOv2
        patch-embedding projection weight shape (key
        ``backbone.0.encoder.encoder.embeddings.patch_embeddings.projection.weight``). This fallback **can raise**
        :class:`ValueError` on a mismatch, providing a clear error before the cryptic :class:`RuntimeError` from
        :meth:`~torch.nn.Module.load_state_dict` would otherwise fire. For all other attributes (e.g.
        ``segmentation_head``), if either side is missing, that check is skipped silently â€” preserving backward
        compatibility.

        Two class-count scenarios are distinguished:

        * Backbone pretrain: the checkpoint head was trained with more classes
          than the current ``model_args.num_classes``. In this case the detection head is typically reinitialized or
          trimmed externally to match the configured number of classes.
        * Fine-tuned checkpoint: the checkpoint head was trained with fewer
          classes than the current ``model_args.num_classes``. If you intend to reuse the checkpoint's classification
          head as-is, set ``model_args.num_classes`` to ``ckpt_num_classes - 1`` (the value reported in the warning)
          before loading the state dict to align the configuration and silence the warning.
    r@   zclass_embed.biasNr   Únum_classesr$   zrCheckpoint has %d classes but model is configured for %d. The detection head will be re-initialized to %d classes.zŠCheckpoint has %d classes but model is configured for %d. Using checkpoint class count (%d). Pass num_classes=%d to suppress this warning.r   Ú
patch_sizezHbackbone.0.encoder.encoder.embeddings.patch_embeddings.projection.weightÚshapeé   é   é   éÿÿÿÿÚsegmentation_headz»The checkpoint was trained with a segmentation head, but the current model does not have one. Load the weights into a segmentation model (e.g. RFDETRSegNano) instead of a detection model.z±The current model has a segmentation head, but the checkpoint was trained without one. Load the weights into a detection model (e.g. RFDETRNano) instead of a segmentation model.Úuse_grouppose_keypointsz»The checkpoint was trained with a keypoint head, but the current model does not have one. Load the weights into a keypoint model (e.g. RFDETRKeypointPreview) instead of a detection model.z©The current model has a keypoint head, but the checkpoint was trained without one. Load the weights into a detection model (e.g. RFDETRNano) instead of a keypoint model.)
r   r…   r   rx   Úwarningr    rz   r   r   r   )r8   r�   Úckpt_class_biasÚckpt_num_classesÚmodel_num_classesÚ
_ckpt_argsÚ_ckpt_patch_size_from_argsÚ_patch_proj_keyÚ_ckpt_proj_wÚ_ckpt_proj_shapeÚ_inferred_psÚ	_model_psÚ	ckpt_argsÚckpt_segmentation_headÚmodel_segmentation_headr   r   Úckpt_keypoint_headÚmodel_keypoint_heads                      r   Ú!validate_checkpoint_compatibilityrœ     s-  € ðZ !—n’n W¨bÑ1Ô1×5Ò5Ð6HÈ$ÑOÔO€OØÐ"Ø*Ô0°Ô3ÐÝ(/°
¸MÈ4Ñ(PÔ(PÐØÐ(Ð-=ÐARÐUVÑAVÒ-VÐ-VØ  1Ñ$Ð'7Ò7Ð7å—’ðOà$ qÑ(Ø%Ø%ñô ð ð õ —’ðDð % qÑ(Ø%Ø$ qÑ(Ø$ qÑ(ñô ð ð$ —’ Ñ'Ô'€JØ-1ÐØÐÝ%3°JÀÑ%MÔ%MÐ"à!Ð)ØdˆØ!—~’~ g¨rÑ2Ô2×6Ò6°ÑGÔGˆÝ" <°¸$Ñ?Ô?ÐØÐ'­CÐ0@Ñ,AÔ,AÀQÒ,FÐ,FÐK[Ð\]ÔK^ÐbrÐstÔbuÒKuÐKuÝÐ/°Ô3Ñ4Ô4ˆLÝ$+¨J¸ÀdÑ$KÔ$KˆIØÐ$¨¸Ò)BÐ)BÝ*¨<¸ÑCÔCÐCØ�ZÐÐØˆà˜6Ô"€IÝ*8¸ÐDWÑ*XÔ*XÐÝ+2°:Ð?RÐTXÑ+YÔ+YÐð 	Ð*Ø#Ð/Ø"Ð&=Ò=Ð=à!ð 		Ýðpñô ð õ
 ðmñô ð õ
 #1°¸LÑ"IÔ"I€OÝ#*¨:°|ÀTÑ#JÔ#JÐØÐ"Ð'7Ð'CÈÐ[kÒHkÐHkÝ" ?Ð4DÑEÔEÐEå&4°YÐ@YÑ&ZÔ&ZÐÝ'.¨zÐ;TÐVZÑ'[Ô'[ÐàÐ%Ð*=Ð*IÐN`ÐdwÒNwÐNwØð 		Ýðtñô ð õ
 ðiñô ð ð &Ð%Ð*IÐ*IÐNwÐNwr   )r   r   r   r   r   r   )N)r   r   r   r   r   r   r   r   )r!   r   r   r"   )r8   r9   r:   r;   r   r   )r   r"   r   r_   )r   r"   r@   rf   r   r"   )r8   r"   r�   r   r   r   )Ú__doc__Ú
__future__r   rL   rQ   Úcollectionsr   Útypingr   Úrfdetr.utilities.loggerr   rx   rJ   r   r    r7   r^   re   r€   rœ   r1   r   r   ú<module>r¢      s-  ðð )Ð (à "Ð "Ð "Ð "Ð "Ð "à 	€	€	€	Ø €€€Ø #Ð #Ð #Ð #Ð #Ð #Ø Ð Ð Ð Ð Ð à .Ð .Ð .Ð .Ð .Ð .à	ˆ‰Œ€ðÐ ð"ð ð ð ð$)ð )ð )ð )ð )ð(?ð ?ð ?ð ?ðH -1ðM ð M ð M ð M ð M ð`ð ð ð ð"&ð &ð &ð &ðRFð Fð Fð Fð Fð Fr   