§
    ­¸j�  ã                  ó`   — d Z ddlmZ ddlZddlmZ ddlZ ej        e¦  «        Z	dgZ
dd„ZdS )a”  Checkpoint conversion utilities for the PTL training stack.

Provides :func:`convert_legacy_checkpoint` to convert RF-DETR ``*.pth`` checkpoints (produced by the pre-PTL
``engine.py`` training loop) into the ``*.ckpt`` format expected by ``pytorch_lightning.Trainer``.

Auto-detection of legacy format at load time is handled by
:meth:`rfdetr.training.module_model.RFDETRModelModule.on_load_checkpoint`.
é    )ÚannotationsN)ÚAnyÚconvert_legacy_checkpointÚold_pathÚstrÚnew_pathÚreturnÚNonec                ó"  — ddl m}  || d¬¦  «        }d|vrt          d| ›d�¦  «        ‚|                     d¦  «        }t	          |t
          ¦  «        r|}nU|€i }nP	 t          |¦  «        }n?# t          $ r2 t           	                    d
t          |¦  «        j        ¦  «         i }Y nw xY wd„ |d                              ¦   «         D ¦   «         |                     dd¦  «        d|ddœ}d|v r|d         |d<   t          j        ||¦  «         d	S )aé  Convert a legacy RF-DETR ``.pth`` checkpoint to PTL ``.ckpt`` format.

    Loads a checkpoint saved by the pre-PTL ``engine.py`` training loop and rewrites it in the structure expected by
    ``pytorch_lightning.Trainer``:

    * ``state_dict`` keys are prefixed with ``"model."`` to match the
      attribute path inside :class:`~rfdetr.training.module_model.RFDETRModelModule`.
    * ``args`` (``argparse.Namespace`` or ``dict``) is normalised to a plain
      ``dict`` and stored as ``hyper_parameters``.
    * ``legacy_checkpoint_format: True`` is written so
      :meth:`~rfdetr.training.module_model.RFDETRModelModule.on_load_checkpoint` can distinguish converted files from
      native PTL checkpoints.
    * If an ``ema_model`` key is present it is preserved verbatim under
      ``legacy_ema_state_dict`` for optional EMA weight restoration.

    Args:
        old_path: Path to the source legacy ``.pth`` checkpoint.
        new_path: Destination path for the converted ``.ckpt`` file.
    r   )Ú_safe_torch_loadT)ÚtrustÚmodelzThe checkpoint at zd does not contain a 'model' key. Only RF-DETR legacy .pth files produced by engine.py are supported.ÚargsNzICannot extract hyper_parameters from args of type %s; storing empty dict.c                ó    — i | ]\  }}d |z   |“ŒS )zmodel.© )Ú.0ÚkÚvs      új/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/training/checkpoint.pyú
<dictcomp>z-convert_legacy_checkpoint.<locals>.<dictcomp>K   s"   € ÐHÐHÐH©4¨1¨a�x !‘| QÐHÐHÐHó    Úepoch)Ú
state_dictr   Úglobal_stepÚhyper_parametersÚlegacy_checkpoint_formatÚ	ema_modelÚlegacy_ema_state_dict)Úrfdetr.utilities.ior   Ú
ValueErrorÚgetÚ
isinstanceÚdictÚvarsÚ	TypeErrorÚloggerÚwarningÚtypeÚ__name__ÚitemsÚtorchÚsave)r   r   r   ÚoldÚargs_objr   Únews          r   r   r      s€  € ð, 5Ð4Ð4Ð4Ð4Ð4à*Ð*¨8¸4Ð@Ñ@Ô@€Cà�cÐÐÝðS ð Sð Sð Sñ
ô 
ð 	
ð
 �wŠw�v‰Œ€HÝ�(�DÑ!Ô!ð "Ø+3ÐÐØ	Ð	ØÐÐð	"Ý# H™~œ~ÐÐøÝð 	"ð 	"ð 	"Ý�NŠNØ[Ý�X‘”Ô'ñô ð ð  "ÐÐÐð	"øøøð IÐH°3°w´<×3EÒ3EÑ3GÔ3GÐHÑHÔHØ—’˜ !Ñ$Ô$ØØ,Ø$(ðð €Cð �cÐÐð (+¨;Ô'7ˆÐ#Ñ$å	„Jˆs�HÑÔÐÐÐs   ÁA. Á.9B*Â)B*)r   r   r   r   r	   r
   )Ú__doc__Ú
__future__r   ÚloggingÚtypingr   r+   Ú	getLoggerr)   r&   Ú__all__r   r   r   r   ú<module>r6      s   ððð ð #Ð "Ð "Ð "Ð "Ð "à €€€Ø Ð Ð Ð Ð Ð à €€€à	ˆÔ	˜8Ñ	$Ô	$€à&Ð
'€ð<ð <ð <ð <ð <ð <r   