§
    ­¸j}1  ã                  óV  — d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlZdd	lmZ dd
lmZmZ ddlmZmZ ddlmZ ddlmZ  e¦   «         ZdZdd„Z edd¬¦  «         G d„ de¦  «        ¦   «         Z G d„ dej        ¦  «        Z G d„ dee         ¦  «        Z dS )a%  Direct PyTorch -> LiteRT (``.tflite``) export via ``torch.export`` + ``litert-torch``.

LiteRT (formerly TensorFlow Lite) is Google's on-device runtime.  This route hands the export-mode RF-DETR module to
``litert_torch.convert``, which captures it with :func:`torch.export.export` and lowers the captured graph to a
``.tflite`` flatbuffer itself -- no ONNX and no TensorFlow step, unlike ``format="tflite"``
(:mod:`rfdetr.export._tflite`), which goes PyTorch -> ONNX -> ``onnx2tf`` -> TFLite.  Both routes produce a
``.tflite`` that the ``ai_edge_litert`` interpreter runs.

The exported graph is the full detector in one file, two-stage query selection (``topk``/``gather``) included, so it
runs on the CPU (XNNPACK) delegate.  On CPU the pretrained Nano / Seg-Nano exports track eager PyTorch to ~1e-7
(boxes) and ~3e-5 (class logits, mask probabilities) over the confident queries, and to ~2e-3 at worst over all 300 raw
mask logits; the ``e2e_litert`` test suite gates on looser regression bounds (see its docstrings for the measurements).
Keypoint models are not supported on litert-torch 0.9.4: its converter rejects the rank-4 ``batch_matmul`` that the
keypoint head's ``nn.Linear`` lowers to.

Note:
    The produced ``.tflite`` expects the same input normalization as the ONNX export: ImageNet mean/std
    (``mean=[0.485, 0.456, 0.406]``, ``std=[0.229, 0.224, 0.225]``), NCHW float32.  Output tensor names are
    litert-torch's own (``serving_default_output_<i>_output``); consumers match outputs by **position**, mirroring the
    CoreML and OpenVINO exports.
é    )ÚannotationsN)ÚMapping)Ú	dataclass)ÚPath)Ú
ModuleType)ÚAny)Únn)Úappend_backbone_markerÚresolve_export_stem)ÚExportConfigÚExporter)ÚExportGraph)Ú
get_loggerzTLiteRT export requires `litert-torch`. Install it with: pip install "rfdetr[litert]"ÚreturnÚNonec                 ó^   — 	 ddl } dS # t          $ r}t          t          ¦  «        |‚d}~ww xY w)a�  Verify that ``litert_torch`` is importable.

    Shared by :class:`LiteRTExporter` and the package-level ``_IS_LITERT_AVAILABLE`` flag so both surface the same
    actionable message, and so tests can monkeypatch a single choke point instead of relying on ``litert_torch``
    actually being absent from the environment.

    Raises:
        ImportError: If ``litert_torch`` cannot be imported.
    r   N)Úlitert_torchÚImportErrorÚ_INSTALL_HINT)r   Úerrors     ún/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/export/_litert/exporter.pyÚ_check_litert_availabler   2   sH   € ð4ØÐÐÐÐÐøÝð 4ð 4ð 4Ý�-Ñ(Ô(¨eÐ3øøøøð4øøøs   ‚ ˆ
,’'§,T)ÚfrozenÚslotsc                  ó   — e Zd ZdZdS )ÚLiteRTConfigav  Settings for ``format="litert"``.

    This route has no format-specific knob yet: it always writes one float32 ``.tflite`` with the shape baked in.
    ``RFDETR.export``'s ``quantization`` keyword is validated rather than stored -- see
    :meth:`LiteRTExporter._format_settings` -- so a value this route cannot honour is refused instead of being
    silently ignored.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    r   r   r   B   s   € € € € € ðð ð ð r"   r   c                  ó,   ‡ — e Zd ZdZdˆ fd„Zdd
„Zˆ xZS )ÚModelWrapperaÑ  Normalize an export-mode RF-DETR forward into a plain tensor tuple for ``litert_torch.convert``.

    The wrapped *model* must already be switched into export mode by the caller (:meth:`Exporter.__call__` does this
    before :meth:`LiteRTExporter._convert` runs) -- ``forward_export`` always returns a tuple of tensors for the full
    detector (``(dets, labels)`` / ``(dets, labels, masks)``), and the backbone-only export graph
    (:class:`rfdetr.export._backend._BackboneExport`) returns a plain list of tensors.  A dict output only reaches this
    wrapper when the caller forgot the mode-switch -- that is a caller bug, not a shape this wrapper can flatten, so it
    raises instead of silently dropping keys.
    Úmodelú	nn.Moduler   r   c                óV   •— t          ¦   «                              ¦   «          || _        d S )N)ÚsuperÚ__init__r%   )Úselfr%   Ú	__class__s     €r   r)   zModelWrapper.__init__X   s$   ø€ Ý‰Œ×ÒÑÔÐØˆŒ
ˆ
ˆ
r"   Úxútorch.Tensorútuple[torch.Tensor, ...]c                ó*  — |                       |¦  «        }t          |t          t          f¦  «        rt          |¦  «        S t          |t          ¦  «        r t          dt          |¦  «        › d�¦  «        ‚t          dt          |¦  «        ›�¦  «        ‚)Nz8LiteRT export received a dict-valued model output (keys=zœ); this means the model was not switched into export mode before wrapping (forward_export always returns a tuple/list). Call model.export() before wrapping.z1Unsupported model output type for LiteRT export: )	r%   Ú
isinstanceÚlistÚtupleÚdictÚNotImplementedErrorÚsortedÚ	TypeErrorÚtype)r*   r,   Úoutputs      r   ÚforwardzModelWrapper.forward\   s”   € Ø—’˜A‘”ˆÝ�f�t¥U˜mÑ,Ô,ð 	!Ý˜‘=”=Ð Ý�f�dÑ#Ô#ð 	Ý%ð7Í6ÐRXÉ>Ì>ð 7ð 7ð 7ñô ð õ
 Ð\ÍDÐQWÉLÌLÐ\Ð\Ñ]Ô]Ð]r"   )r%   r&   r   r   )r,   r-   r   r.   )r   r   r   r    r)   r9   Ú__classcell__)r+   s   @r   r$   r$   M   sc   ø€ € € € € ðð ðð ð ð ð ð ð
^ð 
^ð 
^ð 
^ð 
^ð 
^ð 
^ð 
^r"   r$   c                  óz   — e Zd ZdZeZdZdZdZdZ	dZ
dZdZed#d„¦   «         Zd$d„Zd%d„Zd&d„Zd'd„Zd(d „Zd)d!„Zd"S )*ÚLiteRTExporterañ  Convert a prepared graph straight to a LiteRT ``.tflite``, with no ONNX or TensorFlow step in between.

    The conversion runs as a short sequence of steps, each its own method: import ``litert_torch``, create the
    output directory, resolve the artifact name, wrap the graph for capture, then convert and write the ``.tflite``.
    Mirrors :class:`~rfdetr.export._openvino.exporter.OpenVINOExporter`, the other direct-from-PyTorch route.

    Examples:
        Requires the optional ``litert-torch`` dependency and a prepared graph, so this is documentation only
        (not a doctest):

        ```python
        LiteRTExporter(LiteRTConfig(variant_name="rfdetr-small"))(graph)
        # -> PosixPath('output/rfdetr-small.tflite')
        ```
    ÚlitertÚLiteRTzQ(the .tflite bakes a fixed input shape). Export one model per batch size instead.TzDUpstream dependency instabilities (litert-torch) may affect results.z4a .tflite flatbuffer has no ONNX-style metadata slotÚsettingsúMapping[str, Any]r   údict[str, Any]c                ó^   — |                      d¦  «        }|dvrt          d|›d�¦  «        ‚i S )aÌ  Refuse a ``quantization`` this route cannot apply; the configuration itself stores nothing.

        A ``.tflite`` caller may reasonably expect ``quantization`` to apply, so any value other than ``None`` /
        ``"fp32"`` is rejected here -- at :meth:`~rfdetr.export.base.Exporter.build_config` time, before the caller
        pays for a forward pass -- rather than silently writing a float32 file.

        Args:
            settings: The keyword arguments ``RFDETR.export`` was called with.

        Returns:
            An empty mapping: :class:`LiteRTConfig` declares no format-specific field.

        Raises:
            NotImplementedError: If *quantization* is anything but ``None`` / ``"fp32"``.
        Úquantization)NÚfp32z5LiteRT export writes a float32 .tflite; quantization=z‡ is not supported on this route yet. Use format='tflite' for its fp16/int8 modes, or quantize the exported file with ai-edge-quantizer.)Úgetr4   )Úclsr?   rC   s      r   Ú_format_settingszLiteRTExporter._format_settingsƒ   sO   € ð"  —|’| NÑ3Ô3ˆØ˜~Ð-Ð-Ý%ð%Èð %ð %ð %ñô ð ð
 ˆ	r"   r   c                óF   — t          ¦   «          t          j        d¦  «        S )a�  Verify ``litert_torch`` is installed and return the module the conversion calls into.

        Imported here rather than at module scope so ``format="litert"`` is the only export path that pays for the
        (optional, heavy) dependency.

        Returns:
            The imported ``litert_torch`` package.

        Raises:
            ImportError: If ``litert_torch`` is not installed.
        r   )r   Ú	importlibÚimport_module)r*   s    r   Ú_import_converterz LiteRTExporter._import_converter�   s!   € õ 	 Ñ!Ô!Ð!ÝÔ& ~Ñ6Ô6Ð6r"   r   c                óf   — t          | j        j        ¦  «        }|                     dd¬¦  «         |S )z–Create the configured output directory if it does not exist yet.

        Returns:
            The directory the ``.tflite`` is written into.
        T)ÚparentsÚexist_ok)r   ÚconfigÚ
output_dirÚmkdir)r*   rP   s     r   Ú_prepare_output_dirz"LiteRTExporter._prepare_output_dir¬   s5   € õ ˜$œ+Ô0Ñ1Ô1ˆ
Ø×Ò °ÐÑ5Ô5Ð5ØÐr"   Úbackbone_onlyÚboolÚstrc               óÆ   — t          | j        j        | j        j        |rdnd¬¦  «        \  }}t	          ||t          | j        j        p| j        j        ¦  «        ¬¦  «        S )zÊResolve the ``.tflite`` filename stem.

        Args:
            backbone_only: Whether the graph is a backbone-only export.

        Returns:
            The artifact name, without extension.
        Úbackbone_modelÚinference_model)Údefault)rS   Únamed)r   rO   Úvariant_nameÚoutput_namer
   rT   )r*   rS   ÚstemÚ_s       r   Ú_resolve_export_namez#LiteRTExporter._resolve_export_name¶   st   € õ &ØŒKÔ$ØŒKÔ#Ø(5ÐLÐ$Ð$Ð;Lð
ñ 
ô 
‰ˆˆaõ
 &ØØ'Ý�t”{Ô/ÐJ°4´;Ô3JÑKÔKð
ñ 
ô 
ð 	
r"   Úgraphr   ú!tuple[ModelWrapper, torch.Tensor]c                óˆ  — | j         j        rNt                               d¦  «         t                               dt	          |j        j        ¦  «        › �¦  «         |j                             ¦   «          	                    ¦   «         }t          |¦  «        }|                     ¦   «          ||j         	                    ¦   «         fS )aà  Announce the conversion, then move the graph onto CPU and wrap it for ``litert_torch.convert``.

        ``litert_torch.convert`` captures on CPU, and :class:`ModelWrapper` normalizes the export-mode forward's
        tuple/list output into the plain tensor tuple the converter accepts.

        Args:
            graph: The prepared model and its graph metadata.

        Returns:
            The wrapped model and the example input tensor, both on CPU and in eval mode.
        zAConverting PyTorch model to LiteRT (.tflite) with litert-torch...zInput shape: )rO   ÚverboseÚloggerÚinfor2   Úinput_tensorsÚshaper%   ÚevalÚcpur$   )r*   r`   r%   Úwrapped_models       r   Ú_prepare_module_for_tracingz*LiteRTExporter._prepare_module_for_tracingÊ   s¦   € ð Œ;Ôð 	LÝ�KŠKÐ[Ñ\Ô\Ð\Ý�KŠKÐJ­¨eÔ.AÔ.GÑ(HÔ(HÐJÐJÑKÔKÐKà”× Ò Ñ"Ô"×&Ò&Ñ(Ô(ˆÝ$ UÑ+Ô+ˆØ×ÒÑÔÐØ˜eÔ1×5Ò5Ñ7Ô7Ð7Ð7r"   r   rj   r$   rf   r-   Úoutput_filer   c                ó~  — 	 t          j        ¦   «         5  |                     ||f¦  «        }|                     t	          |¦  «        ¦  «         ddd¦  «         dS # 1 swxY w Y   dS # t
          t          t          f$ r ‚ t          $ r2}t           
                    d¦  «         t          d|› �¦  «        |‚d}~ww xY w)a„  Capture *wrapped_model* with ``litert_torch.convert`` and write the ``.tflite`` to *output_file*.

        Args:
            litert_torch: The imported ``litert_torch`` package from :meth:`_import_converter`.
            wrapped_model: The CPU, eval-mode module to capture.
            input_tensors: Example input the capture traces with; its shape is baked into the file.
            output_file: Destination path for the ``.tflite``.

        Raises:
            ImportError: If litert-torch's lazily imported converter/quantizer companions fail on a partial install.
            NotImplementedError: If the model was not switched into export mode first (see :class:`ModelWrapper`).
            TypeError: If the model's forward returns an output type :class:`ModelWrapper` cannot wrap.
            RuntimeError: If ``torch.export`` capture, lowering, or writing the file otherwise fails.
        NzLiteRT export failedz"Failed to export model to LiteRT: )ÚtorchÚno_gradÚconvertÚexportrU   r   r4   r6   Ú	Exceptionrd   Ú	exceptionÚRuntimeError)r*   r   rj   rf   rl   Ú
edge_modelÚes          r   Ú_convert_and_savez LiteRTExporter._convert_and_saveß   s  € ð*	PÝ”‘”ð 4ð 4Ø)×1Ò1°-À-ÐAQÑRÔR�
Ø×!Ò!¥# kÑ"2Ô"2Ñ3Ô3Ð3ð4ð 4ð 4ñ 4ô 4ð 4ð 4ð 4ð 4ð 4ð 4ð 4øøøð 4ð 4ð 4ð 4ð 4ð 4øõ Õ0µ)Ð<ð 	ð 	ð 	ð Ýð 	Pð 	Pð 	PÝ×ÒÐ3Ñ4Ô4Ð4ÝÐGÀAÐGÐGÑHÔHÈaÐOøøøøð	Pøøøs:   ‚A) •:AÁA) ÁA Á A) Á#A Á$A) Á)!B<Â
-B7Â7B<c                óJ  — |                       ¦   «         }|                      ¦   «         }||                      |j        ¬¦  «        › d�z  }|                      |¦  «        \  }}|                      ||||¦  «         | j        j        rt           	                    d|› �¦  «         |S )z*Write the ``.tflite`` and return its path.)rS   z.tfliteu   âœ“ LiteRT model saved to )
rK   rR   r_   rS   rk   rw   rO   rc   rd   re   )r*   r`   r   rP   rl   rj   rf   s          r   Ú_convertzLiteRTExporter._convert  s¬   € à×-Ò-Ñ/Ô/ˆØ×-Ò-Ñ/Ô/ˆ
Ø  d×&?Ò&?ÈeÔNaÐ&?Ñ&bÔ&bÐ#kÐ#kÐ#kÑkˆà'+×'GÒ'GÈÑ'NÔ'NÑ$ˆ�}Ø×Ò˜|¨]¸MÈ;ÑWÔWÐWàŒ;Ôð 	DÝ�KŠKÐB°[ÐBÐBÑCÔCÐCØÐr"   N)r?   r@   r   rA   )r   r   )r   r   )rS   rT   r   rU   )r`   r   r   ra   )
r   r   rj   r$   rf   r-   rl   r   r   r   )r`   r   r   r   )r   r   r   r    r   Úconfig_classÚformatÚdisplay_nameÚdynamic_batch_reasonÚexperimentalÚexperimental_noteÚ	pip_extraÚnotes_reasonÚclassmethodrG   rK   rR   r_   rk   rw   ry   r!   r"   r   r<   r<   i   så   € € € € € ðð ð   €LØ€FØ€LØnÐØ€LØ^ÐØ€IØI€Làðð ð ñ „[ðð27ð 7ð 7ð 7ðð ð ð ð
ð 
ð 
ð 
ð(8ð 8ð 8ð 8ð*"Pð "Pð "Pð "PðHð ð ð ð ð r"   r<   )r   r   )!r    Ú
__future__r   rI   Úcollections.abcr   Údataclassesr   Úpathlibr   Útypesr   Útypingr   rn   r	   Úrfdetr.export._namingr
   r   Úrfdetr.export.baser   r   Úrfdetr.export.preparer   Úrfdetr.utilities.loggerr   rd   r   r   r   ÚModuler$   r<   r!   r"   r   ú<module>rŽ      sÙ  ððð ð, #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø #Ð #Ð #Ð #Ð #Ð #Ø !Ð !Ð !Ð !Ð !Ð !Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð à MÐ MÐ MÐ MÐ MÐ MÐ MÐ MØ 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø -Ð -Ð -Ð -Ð -Ð -Ø .Ð .Ð .Ð .Ð .Ð .à	ˆ‰Œ€àf€ð4ð 4ð 4ð 4ð  €�$˜dÐ#Ñ#Ô#ðð ð ð ð �<ñ ô ñ $Ô#ðð^ð ^ð ^ð ^ð ^�2”9ñ ^ô ^ð ^ð8eð eð eð eð e�X˜lÔ+ñ eô eð eð eð er"   