Ë
    ëÓ^jÁ  ã                  ób   — 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y)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_checkpointc                ó  — t        j                  | dd¬«      }d|vrt        d| ›d�«      ‚|j                  d«      }t	        |t
        «      r|}n|€i }n	 t        |«      }|d   j                  «       D ��ci c]  \  }}d
|z   |“Œ c}}|j                  dd«      d|ddœ}d|v r|d   |d<   t        j                  ||«       y# t        $ r. t        j                  d	t        |«      j                  «       i }Y Œ™w xY wc c}}w )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.
    ÚcpuF)Úmap_locationÚweights_onlyÚ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.zmodel.Úepochr   T)Ú
state_dictr   Úglobal_stepÚhyper_parametersÚlegacy_checkpoint_formatÚ	ema_modelÚlegacy_ema_state_dict)ÚtorchÚloadÚ
ValueErrorÚgetÚ
isinstanceÚdictÚvarsÚ	TypeErrorÚloggerÚwarningÚtypeÚ__name__ÚitemsÚsave)Úold_pathÚnew_pathÚoldÚargs_objr   ÚkÚvÚnews           úe/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/rfdetr/training/checkpoint.pyr   r      s$  € ô(  Ÿ*™* X¸EÐPUÔV€Cà�cÑÜØ   ð -Sð Só
ð 	
ð
 �w‰w�v‹€HÜ�(œDÔ!Ø+3ÑØ	Ð	ØÑð	"Ü# H›~Ðð 47°w±<×3EÑ3EÓ3G×H©4¨1¨a�x !‘| Q‘ÓHØ—‘˜ !Ó$ØØ,Ø$(ñ€Cð �cÑð (+¨;Ñ'7ˆÐ#Ñ$ä	‡J�Jˆs�HÕøô) ò 	"Ü�N‰NØ[Ü�X“×'Ñ'ôð  "Òð	"üó Is   ÁC Á7C>Ã4C;Ã:C;)r!   Ústrr"   r)   ÚreturnÚNone)Ú__doc__Ú
__future__r   ÚloggingÚtypingr   r   Ú	getLoggerr   r   Ú__all__r   © ó    r(   ú<module>r4      s7   ðñõ #ã Ý ã à	ˆ×	Ñ	˜8Ó	$€à&Ð
'€ô8r3   