§
    ­¸j®O  ã                  ó:  — d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZ ddlZddlZddlmZ 	 ddlZn# e$ r dZY nw xY w	 ddlmZ n# e$ r dZY nw xY wddlmZmZmZ dd	lmZ dd
lmZ  e¦   «         Z G d„ d¦  «        Z G d„ dej         ¦  «        Z!dS )u  Reference TensorRT runtime for an engine built by :mod:`rfdetr.export._tensorrt.exporter`.

Device-managed rather than session-tier: the caller hands over torch tensors already on the GPU and gets the engine's
output bindings back, with CUDA stream management and synchronization handled here. Nothing decodes detections â€” that
stays with the caller.

For production TensorRT inference prefer the ``inference-models`` library, which covers RF-DETR across PyTorch, ONNX and
TensorRT with automatic backend selection.
é    )ÚannotationsN)ÚOrderedDictÚ
namedtuple)ÚMapping)ÚAny)ÚTensor)ÚFp16StrategyÚfp16_source_graphÚresolve_fp16_strategy)Ú
BATCH_AXIS)Ú
get_loggerc                  ó²   — e Zd ZdZ	 	 	 	 d1d2d„Zd3d„Zd4d„Zd5d„Zd6d„Zd6d„Z	e
d7d„¦   «         Z	 d8d9d „Zd:d#„Zd;d$„Zd<d%„Zd<d&„Zd<d'„Zd3d(„Zd=d+„Zd>d?d0„ZdS )@ÚTRTInferencezTensorRT inference engine.údino.trtúcuda:0FÚengine_pathÚstrÚdeviceústr | torch.deviceÚ	sync_modeÚboolÚverboseÚreturnÚNonec                óP  — t           st          d¦  «        ‚|| _        || _        || _        |r#t          j        t           j        j        ¦  «        n"t          j        t           j        j        ¦  «        | _        |  	                    |¦  «        | _
        | j
                             ¦   «         | _        |                      | j
        | j        | j        ¦  «        | _        t          d„ | j                             ¦   «         D ¦   «         ¦  «        | _        |                      ¦   «         | _        |                      ¦   «         | _        |                      ¦   «          d | _        | j        s.t0          st          d¦  «        ‚t1          j        ¦   «         | _        t5          ¦   «         | _        d S )NzGTensorRT is not installed. Please install TensorRT to use TRTInference.c              3  ó.   K  — | ]\  }}||j         fV — Œd S ©N)Úptr)Ú.0ÚnÚvs      úq/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/export/_tensorrt/inference.pyú	<genexpr>z(TRTInference.__init__.<locals>.<genexpr>H   s,   è è € Ð(VÐ(V¹¸¸1¨!¨Q¬U¨Ð(VÐ(VÐ(VÐ(VÐ(VÐ(Vó    z‡pycuda is not installed. Install the `tensorrt-bench` extra (pip install 'rfdetr[tensorrt-bench]') to use TRTInference with async mode.)ÚtrtÚImportErrorr   r   r   ÚLoggerÚVERBOSEÚINFOÚloggerÚload_engineÚengineÚcreate_execution_contextÚcontextÚget_bindingsÚbindingsr   ÚitemsÚbindings_addrÚget_input_namesÚinput_namesÚget_output_namesÚoutput_namesÚ_prime_contextÚstreamÚcudaÚStreamÚTimeProfilerÚtime_profile)Úselfr   r   r   r   s        r"   Ú__init__zTRTInference.__init__3   sg  € õ ð 	iÝÐgÑhÔhÐhà&ˆÔØˆŒØ"ˆŒà8?Ð`•c”j¥¤Ô!3Ñ4Ô4Ð4ÅSÄZÕPSÔPZÔP_ÑE`ÔE`ˆŒà×&Ò& {Ñ3Ô3ˆŒà”{×;Ò;Ñ=Ô=ˆŒà×)Ò)¨$¬+°t´|ÀTÄ[ÑQÔQˆŒÝ(Ð(VÐ(VÀÄ×@SÒ@SÑ@UÔ@UÐ(VÑ(VÔ(VÑVÔVˆÔà×/Ò/Ñ1Ô1ˆÔØ ×1Ò1Ñ3Ô3ˆÔØ×ÒÑÔÐØˆŒàŒ~ð 	(Ýð Ý!ðbñô ð õ
 œ+™-œ-ˆDŒKõ )™NœNˆÔÐÐr$   c                óÒ   — t                                | j        ¦  «        | _        | j        D ]:}| j                             |t          | j        |         j	        ¦  «        ¦  «         Œ;dS )a  Register the context state that never changes again, so the per-call path only touches what does.

        The output buffers are allocated once and never move, so their addresses are registered here instead of on every
        call. The per-input shape memo starts empty rather than at the profile maximum :meth:`get_bindings` just
        declared: an unset entry can only cost one redundant ``set_input_shape`` on the first call, where a pre-filled
        one could skip a declaration the engine actually needs.
        N)
ÚdictÚfromkeysr4   Ú_declared_shapesr6   r.   Úset_tensor_addressÚintr0   r   )r=   Únames     r"   r7   zTRTInference._prime_context[   sk   € õ DHÇ=Â=ÐQUÔQaÑCbÔCbˆÔØÔ%ð 	Pð 	PˆDØŒL×+Ò+¨Dµ#°d´mÀDÔ6IÔ6MÑ2NÔ2NÑOÔOÐOÐOð	Pð 	Pr$   Ú
batch_sizerD   údict[str, Tensor]c                ó„  — i }| j                              ¦   «         D ]£\  }}| j                             |¦  «        t          j        j        k    rqt                               d|› d|j	        › �¦  «         t          j        |g|j	        dd …         ¢R Ž                      ¦   «                              | j        ¦  «        ||<   Œ¤|S )Nzmake dummy input z with shape é   )r0   r1   r,   Úget_tensor_moder%   ÚTensorIOModeÚINPUTr*   ÚinfoÚshapeÚtorchÚrandÚfloatÚtor   )r=   rF   ÚblobrE   Úbindings        r"   Úget_dummy_inputzTRTInference.get_dummy_inputg   sº   € Ø"$ˆØ!œ]×0Ò0Ñ2Ô2ð 	`ð 	`‰MˆD�'ØŒ{×*Ò*¨4Ñ0Ô0µCÔ4DÔ4JÒJÐJÝ—’ÐQ°ÐQÐQÀ'Ä-ÐQÐQÑRÔRÐRÝ"œZ¨
ÐG°W´]À1À2À2Ô5FÐGÐGÐG×MÒMÑOÔO×RÒRÐSWÔS^Ñ_Ô_��T‘
øØˆr$   Úpathr   c                óT  — t          j        | j        d¦  «         t          |d¦  «        5 }t          j        | j        ¦  «        5 }|                     |                     ¦   «         ¦  «        cddd¦  «         cddd¦  «         S # 1 swxY w Y   ddd¦  «         dS # 1 swxY w Y   dS )zLoad engine.Ú ÚrbN)r%   Úinit_libnvinfer_pluginsr*   ÚopenÚRuntimeÚdeserialize_cuda_engineÚread)r=   rV   ÚfÚruntimes       r"   r+   zTRTInference.load_engineo   s,  € åÔ# D¤K°Ñ4Ô4Ð4Ý�$˜ÑÔð 	= ¥C¤K°´Ñ$<Ô$<ð 	=ÀØ×2Ò2°1·6²6±8´8Ñ<Ô<ð	=ð 	=ð 	=ð 	=ñ 	=ô 	=ð 	=ð 	=ð 	=ð 	=ð 	=ñ 	=ô 	=ð 	=ð 	=ð 	=ð 	=ð 	=ð 	=øøøð 	=ð 	=ð 	=ð 	=ð 	=ð 	=ð 	=ñ 	=ô 	=ð 	=ð 	=ð 	=ð 	=ð 	=ð 	=ð 	=øøøð 	=ð 	=ð 	=ð 	=ð 	=ð 	=s5   «BÁ'BÁ,BÂB		Â	BÂB		ÂBÂB!Â$B!ú	list[str]c                óÂ   — g }t          | j        ¦  «        D ]G\  }}| j                             |¦  «        t          j        j        k    r|                     |¦  «         ŒH|S r   )Ú	enumerater,   rJ   r%   rK   rL   Úappend©r=   ÚnamesÚ_rE   s       r"   r3   zTRTInference.get_input_namesu   s]   € ØˆÝ  ¤Ñ-Ô-ð 	#ð 	#‰GˆAˆtØŒ{×*Ò*¨4Ñ0Ô0µCÔ4DÔ4JÒJÐJØ—’˜TÑ"Ô"Ð"øØˆr$   c                óÂ   — g }t          | j        ¦  «        D ]G\  }}| j                             |¦  «        t          j        j        k    r|                     |¦  «         ŒH|S r   )rc   r,   rJ   r%   rK   ÚOUTPUTrd   re   s       r"   r5   zTRTInference.get_output_names|   s]   € ØˆÝ  ¤Ñ-Ô-ð 	#ð 	#‰GˆAˆtØŒ{×*Ò*¨4Ñ0Ô0µCÔ4DÔ4KÒKÐKØ—’˜TÑ"Ô"Ð"øØˆr$   r,   r.   c                ój  — | D ]¯}|                       |¦  «        t          j        j        k    rŒ+|                      |¦  «        t
                   dk    rŒP|                      |d¦  «        \  }}}t          d„ |D ¦   «         ¦  «        }|                     ||¦  «        st          d|›d|› d�¦  «        ‚Œ°dS )aò  Declare every dynamic input at its own profile maximum, so the context can resolve the other tensors.

        TensorRT derives a tensor's concrete shape from the input shapes set on the execution context. Declaring the
        inputs first is therefore what makes :meth:`get_bindings` able to ask the context how large each output
        really is, instead of assuming an output's batch is some input's batch.

        Args:
            engine: A deserialized TensorRT engine.
            context: The execution context whose buffers are being allocated.

        Raises:
            ValueError: If TensorRT refuses a maximum it reported itself, which means *context* is not on the
                optimization profile the shape was read from.
        éÿÿÿÿr   c              3  ó4   K  — | ]}t          |¦  «        V — Œd S r   ©rD   ©r   Údims     r"   r#   z;TRTInference._declare_profile_max_inputs.<locals>.<genexpr>™   s(   è è € Ð>Ð>¨3�c #™hœhÐ>Ð>Ð>Ð>Ð>Ð>r$   zTensorRT refused input z at the profile maximum zL it reported itself; the execution context is not on optimization profile 0.N)
rJ   r%   rK   rL   Úget_tensor_shaper   Úget_tensor_profile_shapeÚtupleÚset_input_shapeÚ
ValueError)r,   r.   rE   rg   Úprofile_maxÚ	max_shapes         r"   Ú_declare_profile_max_inputsz(TRTInference._declare_profile_max_inputsƒ   sï   € ð  ð 	ð 	ˆDØ×%Ò% dÑ+Ô+­sÔ/?Ô/EÒEÐEØØ×&Ò& tÑ,Ô,­ZÔ8¸BÒ>Ð>ØØ &× ?Ò ?ÀÀaÑ HÔ HÑˆAˆq�+ÝÐ>Ð>°+Ð>Ñ>Ô>Ñ>Ô>ˆIØ×*Ò*¨4°Ñ;Ô;ð Ý ðJ¨dð Jð JÈið Jð Jð Jñô ð ðð	ð 	r$   Nústr | torch.device | NoneúOrderedDict[str, Any]c                ó&  — t          dd¦  «        }t          ¦   «         }|                      ||¦  «         |D �]X}|                     |¦  «        }t	          j        |                     |¦  «        ¦  «        }|t                   dk    }	|	r|                     |¦  «        n|}
t          d„ |
D ¦   «         ¦  «        }d|v r6| 	                    d¦  «        }t          d|›d|› d|› dt          › d	�	¦  «        ‚|                     |¦  «        t          j        j        k    r ||||d
d|	¦  «        ||<   Œøt          j        t!          j        ||¬¦  «        ¦  «                             |¦  «        } ||||||                     ¦   «         |	¦  «        ||<   �ŒZ|S )a·  Allocate one device buffer per engine output, and record the shape of every tensor.

        Inputs get no buffer. :meth:`_bind_inputs` runs before every execution and points each input binding at the
        caller's own tensor, so a buffer allocated here would never be read; the binding keeps its resolved shape
        (:meth:`get_dummy_input` builds from it) and a ``0`` address placeholder, because :meth:`run_sync` hands
        ``execute_v2`` the whole address list positionally and the entry has to be there.

        A tensor whose batch axis is dynamic (``-1``, from an engine built with ``dynamic_batch=True``) is allocated at
        the shape the execution context resolves once every dynamic input has been declared at its profile maximum, so
        any batch within the profile fits. Output sizes come from TensorRT itself rather than from an input's batch --
        an engine is free to emit an output whose batch axis does not track its input's. :meth:`run_sync` /
        :meth:`run_async` then set the real input shape per call and return the outputs trimmed to it.

        Args:
            engine: A deserialized TensorRT engine.
            context: The execution context whose dynamic inputs get declared at their profile maximum.
            device: The device output buffers are allocated on. Defaults to this instance's own device.

        Returns:
            One :class:`Binding` per engine tensor, keyed by tensor name, in engine iteration order.

        Raises:
            ValueError: If a tensor still carries an unresolved dimension after the inputs were declared, which
                ``np.empty`` would otherwise report as a bare "negative dimensions are not allowed".
        ÚBinding)rE   ÚdtyperN   Údatar   Údynamicrk   c              3  ó4   K  — | ]}t          |¦  «        V — Œd S r   rm   rn   s     r"   r#   z,TRTInference.get_bindings.<locals>.<genexpr>Æ   s(   è è € Ð7Ð7 s�#˜c™(œ(Ð7Ð7Ð7Ð7Ð7Ð7r$   zEngine tensor z% has an unresolved dimension at axis z (resolved shape z™) after every dynamic input was declared at its profile maximum: either no input carries an optimization profile, or the engine makes an axis other than z6 dynamic, which this runtime cannot size a buffer for.Nr   )r|   )r   r   rw   rp   r%   ÚnptypeÚget_tensor_dtyper   rr   Úindexrt   rJ   rK   rL   rO   Ú
from_numpyÚnpÚemptyrR   Údata_ptr)r=   r,   r.   r   r{   r0   rE   Úengine_shaper|   r~   ÚresolvedrN   Úaxisr}   s                 r"   r/   zTRTInference.get_bindings    s¾  € õ8 ˜YÐ(\Ñ]Ô]ˆÝ‘=”=ˆØ×(Ò(¨°Ñ9Ô9Ð9àð 	Yñ 	YˆDØ!×2Ò2°4Ñ8Ô8ˆLÝ”J˜v×6Ò6°tÑ<Ô<Ñ=Ô=ˆEØ"¥:Ô.°"Ò4ˆGà9@ÐR�w×/Ò/°Ñ5Ô5Ð5ÀlˆHÝÐ7Ð7¨hÐ7Ñ7Ô7Ñ7Ô7ˆEØ�Uˆ{ˆ{Ø—{’{ 2‘”�Ý ð8 Tð 8ð 8ÐRVð 8ð 8Ðinð 8ð 8åT^ð8ð 8ð 8ñô ð ð ×%Ò% dÑ+Ô+­sÔ/?Ô/EÒEÐEØ!( ¨¨u°e¸TÀ1ÀgÑ!NÔ!N�˜‘ØÝÔ#¥B¤H¨U¸%Ð$@Ñ$@Ô$@ÑAÔA×DÒDÀVÑLÔLˆDØ$˜W T¨5°%¸¸t¿}º}¹¼ÐPWÑXÔXˆH�T‰N‰Nàˆr$   rS   úMapping[str, Tensor]c                ó&  — | j         D �]}| j        |         }t          ||         j        ¦  «        }|j        r„|| j        |         k    rs| j                             ||¦  «        sNt          d|›d|› d|j        t                   › dt          |j        t          dz   d…         ¦  «        › d�	¦  «        ‚|| j        |<   |j        s)||j        k    rt          d|›d|› d|j        › d	�¦  «        ‚||          
                    ¦   «         | j        |<   �Œ	dS )
a0  Point the input bindings at *blob* and, for dynamic engines, declare this call's input shapes.

        Raises:
            ValueError: If a dynamic input's shape falls outside the engine's optimization profile -- TensorRT
                reports that by returning ``False`` from ``set_input_shape`` rather than raising -- or if a static
                engine is handed a shape it was not built for. Executing anyway would hand back whatever the output
                buffers held from the previous call, or read past the end of the caller's tensor.
        zInput z shape z; is outside the engine's optimization profile (batch up to z
, spatial rI   Nz'). Export with a larger max_batch_size.z  does not match the fixed shape zk this engine was built for. Export with dynamic_batch=True to serve a range of batch sizes from one engine.)r4   r0   rr   rN   r~   rB   r.   rs   rt   r   r†   r2   )r=   rS   rE   rT   rN   s        r"   Ú_bind_inputszTRTInference._bind_inputs×   s~  € ð Ô$ð 	=ñ 	=ˆDØ”m DÔ)ˆGÝ˜$˜tœ*Ô*Ñ+Ô+ˆEð Œð 4 5¨DÔ,AÀ$Ô,GÒ#GÐ#GØ”|×3Ò3°D¸%Ñ@Ô@ð Ý$ð? ð ?ð ?°ð ?ð ?Ø"œ=­Ô4ð?ð ?Ý@EÀgÄmÕT^ÐabÑTbÐTdÐTdÔFeÑ@fÔ@fð?ð ?ð ?ñô ð ð
 /4�Ô% dÑ+Ø”?ð  u°´Ò'=Ð'=õ !ðq˜Tð qð q¨Eð qð qÐSZÔS`ð qð qð qñô ð ð (,¨D¤z×':Ò':Ñ'<Ô'<ˆDÔ˜tÑ$Ñ$ð)	=ð 	=r$   c                óÊ   — i }| j         D ]X}| j        |         }|j        r%| j                             |¦  «        t
                   nd}|€|j        n|j        d|…         ||<   ŒY|S )zMReturn the output buffers, trimmed to the batch the engine actually produced.N)r6   r0   r~   r.   rp   r   r}   )r=   ÚoutputsrE   rT   Úproduceds        r"   Ú_collect_outputszTRTInference._collect_outputsö   su   € à%'ˆØÔ%ð 	Zð 	ZˆDØ”m DÔ)ˆGØJQÌ/Ðc�t”|×4Ò4°TÑ:Ô:½:ÔFÐFÐ_cˆHØ,4Ð,<˜GœL˜LÀ'Ä,ÈyÐPXÈyÔBYˆG�D‰MˆMØˆr$   c                óî   — |                       |¦  «         | j                             t          | j                             ¦   «         ¦  «        ¦  «        st          d¦  «        ‚|                      ¦   «         S )aØ  Run inference synchronously and return the outputs, trimmed to the produced batch.

        Args:
            blob: One tensor per engine input, already on this engine's device.

        Returns:
            One tensor per engine output. A dynamic output is a view into a buffer the next call
            overwrites -- copy it before the next call if it needs to outlive that call.

        Raises:
            RuntimeError: If TensorRT reports the launch failed.
        z.TensorRT execute_v2 reported a launch failure.)rŒ   r.   Ú
execute_v2Úlistr2   ÚvaluesÚRuntimeErrorr�   ©r=   rS   s     r"   Úrun_synczTRTInference.run_syncÿ   sk   € ð 	×Ò˜$ÑÔÐð Œ|×&Ò&¥t¨DÔ,>×,EÒ,EÑ,GÔ,GÑ'HÔ'HÑIÔIð 	QÝÐOÑPÔPÐPØ×$Ò$Ñ&Ô&Ð&r$   c                óº  — |                       |¦  «         | j        €t          d¦  «        ‚| j        D ]H}| j                             |t          | j        |         ¦  «        ¦  «        st          d|›d�¦  «        ‚ŒI| j                             | j        j	        ¬¦  «        st          d¦  «        ‚| j         
                    ¦   «          |                      ¦   «         S )a  Run inference on this engine's CUDA stream and return the outputs, trimmed to the produced batch.

        Args:
            blob: One tensor per engine input, already on this engine's device.

        Returns:
            One tensor per engine output. A dynamic output is a view into a buffer the next call
            overwrites -- copy it before the next call if it needs to outlive that call.

        Raises:
            RuntimeError: If no CUDA stream is available, or TensorRT reports the launch failed.
        Nz0Async TensorRT inference requires a CUDA stream.z.TensorRT refused the tensor address for input ú.)Ústream_handlez4TensorRT execute_async_v3 reported a launch failure.)rŒ   r8   r•   r4   r.   rC   rD   r2   Úexecute_async_v3ÚhandleÚsynchronizer�   )r=   rS   rE   s      r"   Ú	run_asynczTRTInference.run_async  sê   € ð 	×Ò˜$ÑÔÐØŒ;ÐÝÐQÑRÔRÐRð Ô$ð 	_ð 	_ˆDØ”<×2Ò2°4½¸TÔ=OÐPTÔ=UÑ9VÔ9VÑWÔWð _Ý"Ð#]ÐTXÐ#]Ð#]Ð#]Ñ^Ô^Ð^ð_àŒ|×,Ò,¸4¼;Ô;MÐ,ÑNÔNð 	WÝÐUÑVÔVÐVð 	Œ×ÒÑ!Ô!Ð!Ø×$Ò$Ñ&Ô&Ð&r$   c                ód   — | j         r|                      |¦  «        S |                      |¦  «        S r   )r   r—   rž   r–   s     r"   Ú__call__zTRTInference.__call__0  s0   € ØŒ>ð 	(Ø—=’= Ñ&Ô&Ð&à—>’> $Ñ'Ô'Ð'r$   c                óP  — | j         r>t          j                             ¦   «         rt          j                             ¦   «          d S | j        �| j                             ¦   «          d S t          j                             ¦   «         r t          j                             ¦   «          d S d S r   )r   rO   r9   Úis_availabler�   r8   ©r=   s    r"   r�   zTRTInference.synchronize6  s—   € ØŒ>ð 	ÝŒz×&Ò&Ñ(Ô(ð )Ý”
×&Ò&Ñ(Ô(Ð(ØˆFàŒ;Ð"ØŒK×#Ò#Ñ%Ô%Ð%Ð%Ð%ÝŒZ×$Ò$Ñ&Ô&ð 	%ÝŒJ×"Ò"Ñ$Ô$Ð$Ð$Ð$ð	%ð 	%r$   r    rQ   c                óÌ   — | j                              ¦   «          | j         5  t          |¦  «        D ]} | |¦  «        }Œ	 d d d ¦  «         n# 1 swxY w Y   | j         j        |z  S r   )r<   ÚresetÚrangeÚtotal)r=   rS   r    rg   s       r"   ÚspeedzTRTInference.speedA  s±   € ØÔ×ÒÑ!Ô!Ð!ØÔð 	ð 	Ý˜1‘X”Xð ð �Ø�D˜‘J”J��ðð	ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ð 	ð 	ð 	øøøð 	ð 	ð 	ð 	ð Ô Ô&¨Ñ*Ð*s   ¡AÁAÁAé    Úonnx_file_pathÚengine_file_pathÚmax_batch_sizec                ó  — t          t          ¦  «        \  }}|t          j        u }t	          j        ¦   «         5 }|}|t          j        u rA|                     t          |¦  «        ¦  «        }t           
                    d|› d�¦  «         n)|t          j        u rt                               d|¦  «         t          t          j        d¦  «        r!dt          t          j        j        ¦  «        z  nd}	t          j        | j	        ¦  «        5 }
|
                     |	¦  «        5 }t          j        || j	        ¦  «        5 }|
                     ¦   «         5 }|                     t          j        j        d¦  «         |r$|                     t          j        j        ¦  «         t7          |d¦  «        5 }|                     |                     ¦   «         ¦  «        s£t                               d	¦  «         t?          |j         ¦  «        D ]/}t                               | !                    |¦  «        ¦  «         Œ0	 d
d
d
¦  «         d
d
d
¦  «         d
d
d
¦  «         d
d
d
¦  «         d
d
d
¦  «         d
d
d
¦  «         d
S 	 d
d
d
¦  «         n# 1 swxY w Y   |
 "                    ||¦  «        }t7          |d¦  «        5 }| #                    |¦  «         d
d
d
¦  «         n# 1 swxY w Y   |cd
d
d
¦  «         cd
d
d
¦  «         cd
d
d
¦  «         cd
d
d
¦  «         cd
d
d
¦  «         S # 1 swxY w Y   d
d
d
¦  «         n# 1 swxY w Y   d
d
d
¦  «         n# 1 swxY w Y   d
d
d
¦  «         n# 1 swxY w Y   d
d
d
¦  «         d
S # 1 swxY w Y   d
S )a  Takes an ONNX file and creates a TensorRT engine to run inference with
        http://gitlab.baidu.com/paddle-inference/benchmark/blob/main/backend_trt.py#L57

        FP16 is always requested. Following
        :meth:`~rfdetr.export._tensorrt.exporter.TensorRTExporter.build_engine`, TensorRT
        11+ has no FP16 builder flag and takes precision from the graph, so the graph is cast first.

        Args:
            onnx_file_path: Path to the float32 ``.onnx`` model to build from.
            engine_file_path: Path the serialized engine is written to.
            max_batch_size: Unused; retained for call-site compatibility.

        Returns:
            The serialized engine, or ``None`` if the ONNX file failed to parse.

        Raises:
            Fp16CastUnsupportedError: If a strongly typed TensorRT needs the graph cast to fp16 and it
                cannot be (already fp16, or explicitly quantized).

        Examples:
            >>> TRTInference.build_engine(trt_inference, "model.onnx", "model.trt")  # doctest: +SKIP
        z	TensorRT z2 is strongly typed; benchmarking a cast FP16 graphzŽTensorRT %s does not expose the FP16 builder flag; benchmarking an FP32 engine instead, so these latencies are not comparable to FP16 numbers.ÚEXPLICIT_BATCHrI   r   i   @rY   z%ERROR: Failed to parse the ONNX file.NÚwb)$r   r%   r	   ÚBUILDER_FLAGÚ
contextlibÚ	ExitStackÚ
CAST_GRAPHÚenter_contextr
   r*   rM   ÚUNAVAILABLEÚwarningÚhasattrÚNetworkDefinitionCreationFlagrD   r®   ÚBuilderÚcreate_networkÚ
OnnxParserÚcreate_builder_configÚset_memory_pool_limitÚMemoryPoolTypeÚ	WORKSPACEÚset_flagÚBuilderFlagÚFP16r[   Úparser^   Úerrorr¦   Ú
num_errorsÚ	get_errorÚbuild_serialized_networkÚwrite)r=   rª   r«   r¬   ÚstrategyÚtrt_versionÚuse_fp16_flagÚcleanupÚbuild_sourceÚnetwork_flagsÚbuilderÚnetworkÚparserÚconfigÚmodelrÄ   Úserialized_enginer_   s                     r"   Úbuild_enginezTRTInference.build_engineH  sÜ  € õ0 !6µcÑ :Ô :Ñˆ�+Ø ¥LÔ$=Ð=ˆåÔ!Ñ#Ô#ð +	) wà)ˆLà�<Ô2Ð2Ð2Ø&×4Ò4Õ5FÀ~Ñ5VÔ5VÑWÔW�Ý—’Ðg¨ÐgÐgÐgÑhÔhÐhÐhØ�\Ô5Ð5Ð5Ý—’ðVàñô ð õ �3Ô<Ð>NÑOÔOð�•S�Ô:ÔIÑJÔJÑJÐJàð õ ”˜DœKÑ(Ô(ð)Ø,3Ø×&Ò& }Ñ5Ô5ð)à9@Ý”˜w¨¬Ñ4Ô4ð)ð 9?Ø×-Ò-Ñ/Ô/ð	)ð 4:à×,Ò,­SÔ-?Ô-IÈ7ÑSÔSÐSØ ð :Ø—O’O¥C¤OÔ$8Ñ9Ô9Ð9å˜,¨Ñ-Ô-ð $°Ø!Ÿ<š<¨¯
ª
©¬Ñ5Ô5ð $ÝŸšÐ%LÑMÔMÐMÝ%*¨6Ô+<Ñ%=Ô%=ð Bð B˜EÝ"ŸLšL¨×)9Ò)9¸%Ñ)@Ô)@ÑAÔAÐAÐAØ#ð$ð $ð $ñ $ô $ð $ð)ð )ð )ñ )ô )ð )ð )ð )ð )ñ )ô )ð )ð )ð )ð )ñ )ô )ð )ð )ð )ð )ñ )ô )ð )ð-+	)ð +	)ð +	)ñ +	)ô +	)ð +	)ð +	)ð +	)ðB$ð$ð $ð $ñ $ô $ð $ð $ð $ð $ð $ð $øøøð $ð $ð $ð $ð %,×$DÒ$DÀWÈfÑ$UÔ$UÐ!ÝÐ*¨DÑ1Ô1ð /°QØ—G’GÐ-Ñ.Ô.Ð.ð/ð /ð /ñ /ô /ð /ð /ð /ð /ð /ð /øøøð /ð /ð /ð /ð )ð+)ð )ð )ð )ñ )ô )ð )ð )ð )ð )ð )ñ )ô )ð )ð )ð )ð )ð )ñ )ô )ð )ð )ð )ð )ð )ñ )ô )ð )ð-+	)ð +	)ð +	)ð +	)ñ +	)ô +	)ð +	)ð +	)ð,)ð )ð )ð )øøøð )ð )ð )ð )ð )ð )ð )ñ )ô )ð )ð )ð )ð )ð )ð )øøøð )ð )ð )ð )ð )ð )ð )ñ )ô )ð )ð )ð )ð )ð )ð )øøøð )ð )ð )ð )ð )ð )ð )ñ )ô )ð )ð )ð )ð )ð )ð )øøøð )ð )ð )ð )ð-+	)ð +	)ð +	)ñ +	)ô +	)ð +	)ð +	)ð +	)ð +	)ð +	)ð +	)ð +	)øøøð +	)ð +	)ð +	)ð +	)ð +	)ð +	)s  ¹CM=Ä
M%Ä MÄ;L7	ÅAL Æ,BJÈ3L È>L7	É	MÉM%ÉM=É8L ÊJÊL ÊJÊ)L Ê5KËL ËKËL ËKËL Ë#L7	Ë/MË;M%ÌM=Ì L$Ì$L7	Ì'L$Ì(L7	Ì+MÌ7L;Ì;MÌ>L;Ì?MÍM%ÍMÍM%ÍMÍM%ÍM=Í%M)	Í)M=Í,M)	Í-M=Í=NÎN)r   r   FF)
r   r   r   r   r   r   r   r   r   r   ©r   r   )rF   rD   r   rG   )rV   r   r   r   )r   ra   )r,   r   r.   r   r   r   r   )r,   r   r.   r   r   rx   r   ry   )rS   rŠ   r   r   )r   rG   )rS   rŠ   r   rG   )rS   rŠ   r    rD   r   rQ   )r©   )rª   r   r«   r   r¬   rD   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r>   r7   rU   r+   r3   r5   Ústaticmethodrw   r/   rŒ   r�   r—   rž   r    r�   r¨   rÕ   © r$   r"   r   r   0   s—  € € € € € Ø$Ð$ð &Ø%-ØØð&+ð &+ð &+ð &+ð &+ðP
Pð 
Pð 
Pð 
Pðð ð ð ð=ð =ð =ð =ðð ð ð ðð ð ð ð ðð ð ñ „\ðð: NRð5ð 5ð 5ð 5ð 5ðn=ð =ð =ð =ð>ð ð ð ð'ð 'ð 'ð 'ð*'ð 'ð 'ð 'ð8(ð (ð (ð (ð	%ð 	%ð 	%ð 	%ð+ð +ð +ð +ðF)ð F)ð F)ð F)ð F)ð F)ð F)r$   r   c                  ó6   — e Zd Zdd„Zdd„Zdd
„Zdd„Zdd„ZdS )r;   r   r   c                ó"   — d| _         d| _        d S ©Ng        )r§   Ústartr£   s    r"   r>   zTimeProfiler.__init__’  s   € ØˆŒ
ØˆŒ
ˆ
ˆ
r$   ú'TimeProfiler'c                ó8   — |                       ¦   «         | _        | S r   )Útimerà   r£   s    r"   Ú	__enter__zTimeProfiler.__enter__–  s   € Ø—Y’Y‘[”[ˆŒ
Øˆr$   Útyper   ÚvalueÚ	tracebackc                óZ   — | xj         |                      ¦   «         | j        z
  z  c_         d S r   )r§   rã   rà   )r=   rå   ræ   rç   s       r"   Ú__exit__zTimeProfiler.__exit__š  s%   € Øˆ
Œ
�d—i’i‘k”k D¤JÑ.Ñ.ˆ
Œ
ˆ
ˆ
r$   c                ó   — d| _         d S rß   )r§   r£   s    r"   r¥   zTimeProfiler.reset�  s   € ØˆŒ
ˆ
ˆ
r$   rQ   c                ó    — t           j                             ¦   «         rt           j                             ¦   «          t	          j        ¦   «         S r   )rO   r9   r¢   r�   rã   Úperf_counterr£   s    r"   rã   zTimeProfiler.time   s;   € ÝŒ:×"Ò"Ñ$Ô$ð 	%ÝŒJ×"Ò"Ñ$Ô$Ð$ÝÔ Ñ"Ô"Ð"r$   NrÖ   )r   rá   )rå   r   ræ   r   rç   r   r   r   )r   rQ   )r×   rØ   rÙ   r>   rä   ré   r¥   rã   rÜ   r$   r"   r;   r;   ‘  sx   € € € € € ðð ð ð ðð ð ð ð/ð /ð /ð /ðð ð ð ð#ð #ð #ð #ð #ð #r$   r;   )"rÚ   Ú
__future__r   r±   rã   Úcollectionsr   r   Úcollections.abcr   Útypingr   Únumpyr„   rO   r   Útensorrtr%   r&   Úpycuda.driverÚdriverr9   Ú rfdetr.export._tensorrt.exporterr	   r
   r   Úrfdetr.export.preparer   Úrfdetr.utilities.loggerr   r*   r   ÚContextDecoratorr;   rÜ   r$   r"   ú<module>rù      sÉ  ððð ð #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø €€€Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ø #Ð #Ð #Ð #Ð #Ð #Ø Ð Ð Ð Ð Ð à Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð ðØÐÐÐÐøØð ð ð Ø
€C€C€CðøøøðØ Ð Ð Ð Ð Ð Ð øØð ð ð Ø€D€D€Dðøøøð dÐ cÐ cÐ cÐ cÐ cÐ cÐ cÐ cÐ cØ ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø .Ð .Ð .Ð .Ð .Ð .à	ˆ‰Œ€ð^)ð ^)ð ^)ð ^)ð ^)ñ ^)ô ^)ð ^)ðB#ð #ð #ð #ð #�:Ô.ñ #ô #ð #ð #ð #s!   ´9 ¹AÁAÁA ÁAÁA