U
    d                     @   sn   d Z ddlmZ ddlmZ ddlm  mZ ddl	Z
dd Zdd Zd	d
 ZdddZdd ZdddZdS )zK
TensorRT related transformation
Note that ONNX-TRT enforce an NCHW input!
    )
caffe2_pb2)	workspaceNc                 C   s   dd | D S )Nc                 S   s   g | ]
}|j qS  )Z	dim_value.0xr   r   ?/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/trt/transform.py
<listcomp>   s     z'_dim_values_to_list.<locals>.<listcomp>r   )Z
dim_valuesr   r   r   _dim_values_to_list   s    r
   c                 C   s*   dd | D }dd | D }t t||S )Nc                 S   s   g | ]
}|j qS r   )namer   r   r   r   r	      s     z&_get_output_shapes.<locals>.<listcomp>c                 S   s   g | ]}t |jjjjqS r   )r
   typeZtensor_typeshapeZdimr   r   r   r   r	      s     )dictzip)Zoutput_value_infosnamesZshapesr   r   r   _get_output_shapes   s    r   c               
   C   s<   zt   W n* tk
r6 }  ztdW 5 d } ~ X Y nX d S )Nz/TensorRT related functions require CUDA support)CZget_cuda_version	Exception)_r   r   r   
check_gpu_   s    r   @          Fc                 C   s<   t   t|  t| jj||||}t }|	| |S )z:
    Convert the whole ONNX model to a TensorRT C2 op
    )
r   r   Zonnx_to_trt_opSerializeToStringr   graphoutputr   ZOperatorDefParseFromString)Z
onnx_modelmax_batch_sizemax_workspace_size	verbositydebug_builderZtrt_stropr   r   r   convert_onnx_model_to_trt_op#   s    


r"   c                 C   s   t |  i }| jD ]h}|jD ]*}||krt |}t|dr|j||< q|jD ]*}||krPt |}t|drP|j||< qPq|S )Nr   )r   Z
RunNetOncer!   r   Z	FetchBlobhasattrr   input)pred_netinputshintsr!   oZblobir   r   r   _infer_shapes8   s    







r*   Tc              	   C   s   t   i }|rFi }	| D ] \}
}tjj| tj|	|
< qt| |	}| D ]\}
}|||
< qNt	| 
 ||||||}t }|| |S )zS
    Transform the caffe2_net by collapsing TRT-runnable nodes into trt c2 ops
    )r   itemsnprandomZrandnZastypeZfloat32r*   r   Ztransform_trtr   r   ZNetDefr   )r%   Zinput_shapesZpopulate_shapesr   r   r   r    Zbuild_serializable_opZshape_hintsZ
input_datakvZpred_net_strZpred_net_cutr   r   r   transform_caffe2_netJ   s(    



r0   )r   r   r   F)Fr   r   r   FT)__doc__Zcaffe2.protor   Zcaffe2.pythonr   Z!caffe2.python._import_c_extensionpythonZ_import_c_extensionr   Znumpyr,   r
   r   r   r"   r*   r0   r   r   r   r   <module>   s(   
    
      