
    j(                        d dl mZ d dlZd dlmZ d dlZd dlmZmZ d dl	m
Z
 ddgdgd	fd+dZ	 	 	 	 	 	 	 	 	 	 	 d,d-d*ZdS ).    )annotationsN)Path)	IS_JETSONLOGGER)	TORCH_2_4   imagesoutput0Ftorch_modeltorch.nn.Moduleimtorch.Tensor	onnx_filestropsetintinput_names	list[str]output_namesdynamicbool | dictreturnNonec           
     d    t           rddini }t          j        j        | ||fd|d|||pdd| dS )ac  Export a PyTorch model to ONNX format.

    Args:
        torch_model (torch.nn.Module): The PyTorch model to export.
        im (torch.Tensor): Example input tensor for the model.
        onnx_file (str): Path to save the exported ONNX file.
        opset (int): ONNX opset version to use for export.
        input_names (list[str]): List of input tensor names.
        output_names (list[str]): List of output tensor names.
        dynamic (bool | dict, optional): Whether to enable dynamic axes.

    Notes:
        Setting `do_constant_folding=True` may cause issues with DNN inference for torch>=1.12.
    dynamoFTN)verboseopset_versiondo_constant_foldingr   r   dynamic_axes)r   torchonnxexport)r   r   r   r   r   r   r   kwargss           d/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/utils/export/engine.py
torch2onnxr%      sn    . #,3hF	J
  !_                  r*    engine_file
str | None	workspace
int | Nonehalfboolint8shapetuple[int, int, int, int]dlametadatadict | Noner   prefixc           
        ddl |p!t          |                               d          }                    j        j                  }|
rj        j        j        |_                            |          }|	                                }t          pddz            }t          j                            dd          d                   dk    }|r'|dk    r!|                    j        j        |           n|dk    r||_        dt          j        j                  z  }|                    |          |j        o|}|j        o|}t,          st/          d          t1          j        | d	 d
           |s|st/          d          j        j        |_        t                    |_        |                    j        j                    !                    |          }|"                    |           stG          d|            fdtI          j%                  D             }fdtI          j&                  D             }|D ]0}t1          j        | d|j'         d|j(         d|j)                    1|D ]0}t1          j        | d|j'         d|j(         d|j)                    1|r|*                                }d|d         ddf}g |dd         fd|dd         D             R }|D ] }|+                    |j'        |||           !|,                    |           |r|s|-                    |           t1          j        | d|rdnd|rdndz    d|            |r|                    j        j.                   j/        j0        |_1         G fddj2                  } ||tg          t          |                               d                               |_4        n!|r|                    j        j5                   |r|6                    |          }|tG          d!          to          |d"          5 }|	stq          j9        |	          }|:                    tw          |          <                    d#d$d%&                     |:                    |=                                           |:                    |           ddd           dS # 1 swxY w Y   dS |>                    |          5 }to          |d"          5 }|	stq          j9        |	          }|:                    tw          |          <                    d#d$d%&                     |:                    |=                                           |:                    |?                                           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'  Export a YOLO model to TensorRT engine format.

    Args:
        onnx_file (str): Path to the ONNX file to be converted.
        engine_file (str | None): Path to save the generated TensorRT engine file.
        workspace (int | None): Workspace size in GB for TensorRT.
        half (bool, optional): Enable FP16 precision.
        int8 (bool, optional): Enable INT8 precision.
        dynamic (bool, optional): Enable dynamic input shapes.
        shape (tuple[int, int, int, int], optional): Input shape (batch, channels, height, width).
        dla (int | None): DLA core to use (Jetson devices only).
        dataset (ultralytics.data.build.InfiniteDataLoader, optional): Dataset for INT8 calibration.
        metadata (dict | None): Metadata to include in the engine file.
        verbose (bool, optional): Enable verbose logging.
        prefix (str, optional): Prefix for log messages.

    Raises:
        ValueError: If DLA is enabled on non-Jetson devices or required precision is not set.
        RuntimeError: If the ONNX file cannot be parsed.

    Notes:
        TensorRT version compatibility is handled for workspace size and engine building.
        INT8 calibration requires a dataset and generates a calibration cache.
        Metadata is serialized and written to the engine file if provided.
    r   Nz.enginei   @.r(   
   z.DLA is only available on NVIDIA Jetson devicesz enabling DLA on core z...ztDLA requires either 'half=True' (FP16) or 'int8=True' (INT8) to be enabled. Please enable one of them and try again.zfailed to load ONNX file: c                :    g | ]}                     |          S  )	get_input.0inetworks     r$   
<listcomp>zonnx2engine.<locals>.<listcomp>   s'    FFFqg""FFFr&   c                :    g | ]}                     |          S r=   )
get_outputr?   s     r$   rC   zonnx2engine.<locals>.<listcomp>   s'    IIIw!!!$$IIIr&   z input "z" with shape z	 output "       c              3  \   K   | ]&}t          t          d pd           |z            V  'dS )rH   N)r   max)r@   dr.   s     r$   	<genexpr>zonnx2engine.<locals>.<genexpr>   s>      "V"Vq3s1in1'='='A#B#B"V"V"V"V"V"Vr&   )minoptrJ   z
 building INT8FP1632z engine as c                  N    e Zd ZdZ	 dd fdZdd	ZddZddZddZddZ	dS )%onnx2engine.<locals>.EngineCalibratora  Custom INT8 calibrator for TensorRT engine optimization.

            This calibrator provides the necessary interface for TensorRT to perform INT8 quantization calibration using
            a dataset. It handles batch generation, caching, and calibration algorithm selection.

            Attributes:
                dataset: Dataset for calibration.
                data_iter: Iterator over the calibration dataset.
                algo (trt.CalibrationAlgoType): Calibration algorithm type.
                batch (int): Batch size for calibration.
                cache (Path): Path to save the calibration cache.

            Methods:
                get_algorithm: Get the calibration algorithm to use.
                get_batch_size: Get the batch size to use for calibration.
                get_batch: Get the next batch to use for calibration.
                read_calibration_cache: Use existing cache instead of calibrating again.
                write_calibration_cache: Write calibration cache to disk.
            r+   cacher   r   r   c                    j                             |            || _        t          |          | _        j        j        nj        j        | _        |j	        | _
        t          |          | _        dS )z;Initialize the INT8 calibrator with dataset and cache path.N)IInt8Calibrator__init__datasetiter	data_iterCalibrationAlgoTypeENTROPY_CALIBRATION_2MINMAX_CALIBRATIONalgo
batch_sizebatchr   rU   )selfrY   rU   r5   trts      r$   rX   z.onnx2engine.<locals>.EngineCalibrator.__init__   sr     #,,T222&!%g  +AA0C 	
 %/
!%[[


r&   trt.CalibrationAlgoTypec                    | j         S )z%Get the calibration algorithm to use.)r_   rb   s    r$   get_algorithmz3onnx2engine.<locals>.EngineCalibrator.get_algorithm   s
    y r&   r   c                    | j         pdS )z*Get the batch size to use for calibration.r(   )ra   rf   s    r$   get_batch_sizez4onnx2engine.<locals>.EngineCalibrator.get_batch_size   s    zQ&r&   list[int] | Nonec                    	 t          | j                  d         dz  }|j        j        dk    r|                    d          n|}t          |                                          gS # t          $ r Y dS w xY w)zOGet the next batch to use for calibration, as a list of device memory pointers.imgg     o@cpucudaN)nextr[   devicetypetor   data_ptrStopIteration)rb   namesim0ss      r$   	get_batchz/onnx2engine.<locals>.EngineCalibrator.get_batch   s}     //6>D.2k.>%.G.G4776???TD0011$      44 s   A%A( (
A65A6bytes | Nonec                    | j                                         r)| j         j        dk    r| j                                         S dS dS )zSUse existing cache instead of calibrating again, otherwise, implicitly return None..cacheN)rU   existssuffix
read_bytesrf   s    r$   read_calibration_cachez<onnx2engine.<locals>.EngineCalibrator.read_calibration_cache   sK    :$$&& 34:+<+H+H:002223 3+H+Hr&   bytesc                :    | j                             |          }dS )z Write calibration cache to disk.N)rU   write_bytes)rb   rU   _s      r$   write_calibration_cachez=onnx2engine.<locals>.EngineCalibrator.write_calibration_cache   s    J**511r&   N)r+   )rU   r   r   r   )r   rd   )r   r   )r   rj   )r   rx   )rU   r   r   r   )
__name__
__module____qualname____doc__rX   rg   ri   rw   r~   r   )r5   rc   s   r$   EngineCalibratorrT      s         .  ) ) ) ) ) ) ) )"! ! ! !' ' ' '       3 3 3 3
2 2 2 2 2 2r&   r   rz   )rY   rU   z3TensorRT engine build failed, check logs for errorswb   littleT)	byteordersigned)@tensorrtr   with_suffixLoggerINFOSeverityVERBOSEmin_severityBuildercreate_builder_configr   __version__splitset_memory_pool_limitMemoryPoolType	WORKSPACEmax_workspace_sizeNetworkDefinitionCreationFlagEXPLICIT_BATCHcreate_networkplatform_has_fast_fp16platform_has_fast_int8r   
ValueErrorr   info
DeviceTypeDLAdefault_device_typeDLA_coreset_flagBuilderFlagGPU_FALLBACK
OnnxParserparse_from_fileRuntimeErrorrange
num_inputsnum_outputsnamer3   dtypecreate_optimization_profile	set_shapeadd_optimization_profileset_calibration_profilerO   ProfilingVerbosityDETAILEDprofiling_verbosityrW   r   int8_calibratorFP16build_serialized_networkopenjsondumpswritelento_bytesencodebuild_engine	serialize) r   r,   r.   r0   r2   r   r3   r5   rY   r6   r   r8   loggerbuilderconfigworkspace_bytesis_trt10flagparserinputsoutputsinpoutprofile	min_shape	max_shaper   enginetmetarB   rc   s      `    `                      @@r$   onnx2enginer   4   sD   N Gi!<!<Y!G!GKZZ
((F :!j19 kk&!!G**,,F9>g677O3?((a00344:H 4Oa''$$S%7%A?SSSS	1		$3!C5DEEED$$T**G)2dD)2dD  	OMNNNv==S===>>> 	D 	 G   &)^%7"c((4555 ^^GV,,F!!),, EC	CCDDD GFFFE'2D,E,EFFFFIIIIeG4G.H.HIIIG V VvTTsxTTSYTTTTUUUU W WvUUUUciUU#)UUVVVV 45577a"b)	WeBQBiW"V"V"V"VERSRTRTI"V"V"VWW	 	Q 	QCchI5iPPPP''000 	4 	4**7333
K6rrt%XVVAWSW9Xrreprrsss L.,---%(%;%D"?	2 ?	2 ?	2 ?	2 ?	2 ?	2 ?	2 ?	2s2 ?	2 ?	2 ?	2D "2!1d9oo11(;;<<"
 "
 "

 
 .,---  (11'6BB>TUUU+t$$ 	#z(++D		**1*NNOOO&&&GGFOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 !!'622 	(fd;PT>U>U 	(YZ#z(++D		**1*NNOOO&&&GGF$$&&'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(sJ   BS//S36S3W$#BW W$W	W$W	W$$W(+W()r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )NNFFFr'   NNNFr+   )r   r   r,   r-   r.   r/   r0   r1   r2   r1   r   r1   r3   r4   r5   r/   r6   r7   r   r1   r8   r   r   r   )
__future__r   r   pathlibr   r    ultralytics.utilsr   r   ultralytics.utils.torch_utilsr   r%   r   r=   r&   r$   <module>r      s    # " " " " "         / / / / / / / / 3 3 3 3 3 3 &Z(k # # # # #P # '7 B( B( B( B( B( B( B(r&   