o
    Whq"                     @   sf   d dl Z d dlmZ d dlZd dlmZmZ ddgdgdfdd	Z						
					dddZdS )    N)Path)	IS_JETSONLOGGER   imagesoutput0Fc                 C   s&   t jj| ||d|d|||pdd	 dS )at  
    Exports 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): List of input tensor names.
        output_names (list): List of output tensor names.
        dynamic (bool | dict, optional): Whether to enable dynamic axes. Defaults to False.

    Notes:
        - Setting `do_constant_folding=True` may cause issues with DNN inference for torch>=1.12.
    FTN)verboseopset_versiondo_constant_foldinginput_namesoutput_namesdynamic_axes)torchonnxexport)torch_modelim	onnx_fileopsetr   r   dynamic r   L/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/utils/export.pyexport_onnx   s   
r           r    c              
      s  ddl |pt| d}jj}|
rjjj|_|}|	 }t
p)dd t
jdd dk}|rHdkrH|jj ndkrO|_dt
jj> }|| |jo`|}|joe|}|durtsptdt| d	| d
 |s|stdjj|_t
||_|jj  ! |}|"| st#d|   fddt$ j%D } fddt$ j&D }|D ]}t| d|j' d|j( d|j)  q|D ]}t| d|j' d|j( d|j)  q|r6|d dkrt*| d |+ }d|d ddf}g |dd fdd|dd D R }|D ]}|j,|j'|||d q#|-| t| d|r@dnd|rFdnd  d|  |r}|jj. |/| j0j1|_2G fd d!d!j3}||t4t| d"d#|_5n
|r|jj6 |r|j7n|j8}| |Q}t9|d$2}|	durt:;|	}|<t=|j>d%d&d'd( |<|?  |<|r|n|@  W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS ))a  
    Exports a YOLO model to TensorRT engine format.

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

    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 | ]}  |qS r   )	get_input.0inetworkr   r   
<listcomp>       z!export_engine.<locals>.<listcomp>c                    r    r   )
get_outputr"   r%   r   r   r'      r(   z input "z" with shape z	 output "z> 'dynamic=True' model requires max batch size, i.e. 'batch=16'       c                 3   s&    | ]}t td  p
d | V  qdS )r   N)intmax)r#   d)	workspacer   r   	<genexpr>   s   $ z export_engine.<locals>.<genexpr>)minoptr.   z
 building INT8FP1632z engine as c                       sn   e Zd ZdZ	ddeddf fddZd jfdd	Zdefd
dZ	de
fddZdefddZdddZdS )z'export_engine.<locals>.EngineCalibratora   
            Custom INT8 calibrator for TensorRT.

            Args:
                dataset (object): Dataset for calibration.
                batch (int): Batch size for calibration.
                cache (str, optional): Path to save the calibration cache. Defaults to "".
            r   cachereturnNc                    s<    j |  || _t|| _ jj| _|j| _	t
|| _d S )N)IInt8Calibrator__init__datasetiter	data_iterCalibrationAlgoTypeENTROPY_CALIBRATION_2algo
batch_sizebatchr   r8   )selfr<   r8   trtr   r   r;      s   

z0export_engine.<locals>.EngineCalibrator.__init__c                 S   s   | j S )z%Get the calibration algorithm to use.)rA   rD   r   r   r   get_algorithm   s   z5export_engine.<locals>.EngineCalibrator.get_algorithmc                 S   s
   | j pdS )z*Get the batch size to use for calibration.r   )rC   rG   r   r   r   get_batch_size   s   
z6export_engine.<locals>.EngineCalibrator.get_batch_sizec                 S   sR   zt | jd d }|jjdkr|dn|}t| gW S  ty(   Y dS 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)rD   namesim0sr   r   r   	get_batch   s   z1export_engine.<locals>.EngineCalibrator.get_batchc                 S   s(   | 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)r8   existssuffix
read_bytesrG   r   r   r   read_calibration_cache   s   
z>export_engine.<locals>.EngineCalibrator.read_calibration_cachec                 S   s   | j |}dS )z Write calibration cache to disk.N)r8   write_bytes)rD   r8   _r   r   r   write_calibration_cache   s   z?export_engine.<locals>.EngineCalibrator.write_calibration_cache)r   )r9   N)__name__
__module____qualname____doc__strr;   r?   rH   r-   rI   listrU   bytesrZ   r]   r   rE   r   r   EngineCalibrator   s    
re   rV   )r<   r8   wb   littleT)	byteordersigned)A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nameshapedtypewarningcreate_optimization_profile	set_shapeadd_optimization_profiler4   set_calibration_profileProfilingVerbosityDETAILEDprofiling_verbosityr:   rb   int8_calibratorFP16build_serialized_networkbuild_engineopenjsondumpswritelento_bytesencode	serialize)r   engine_filer0   halfint8r   r   dlar<   metadatar   prefixloggerbuilderconfigis_trt10flagparserinputsoutputsinpoutprofile	min_shape	max_shapere   buildenginetmetar   )r&   rF   r0   r   export_engine0   s   (






((,
2

2


Tr   )NNFFFr   NNNFr   )	r   pathlibr   r   ultralytics.utilsr   r   r   r   r   r   r   r   <module>   s*   
'