3
I4W^½  ã               @   sº   d 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m	Z	m
Z
 ddlmZmZ dd	ljjjZdd	ljj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!
é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literals)Ú
caffe2_pb2)Úc2_native_run_netÚc2_native_run_op)ÚcoreÚ	workspaceNc             C   s   dd„ | D ƒS )Nc             S   s   g | ]
}|j ‘qS © )Z	dim_value)Ú.0Úxr   r   ú</tmp/pip-build-e66j97b2/torch/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   )Úname)r   r   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dim)r   r   r   r   r   r      s    )ÚdictÚzip)Zoutput_value_infosÚnamesZshapesr   r   r   Ú_get_output_shapes   s    r   c              C   s<   yt jƒ  W n* tk
r6 }  ztdƒ‚W Y d d } ~ X nX d S )Nz/TensorRT related functions require CUDA support)ÚCZget_cuda_versionÚ	Exception)Ú_r   r   r   Ú
check_gpu_   s    r   é@   é   i   é   Fc             C   s<   t ƒ  tj| jƒ t| jjƒ||||ƒ}tjƒ }|j	|ƒ |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 j| ƒ i }xz| jD ]p}x4|jD ]*}||kr"t j|ƒ}t|dƒr"|j||< q"W x4|jD ]*}||krXt j|ƒ}t|dƒrX|j||< qXW qW |S )Nr   )r
   Z
RunNetOncer'   r!   Z	FetchBlobÚhasattrr   Úinput)Úpred_netÚinputsZhintsr'   ÚoZblobÚir   r   r   Ú_infer_shapes:   s    




r/   Tc             C   s˜   t ƒ  i }|rJi }	x,|jƒ D ] \}
}tjj|Ž jtjƒ|	|
< qW t| |	ƒ}x|jƒ D ]\}
}|||
< qTW tj	| j
ƒ ||||||ƒ}tjƒ }|j|ƒ |S )zS
    Transfrom 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_netL   s&    


r5   i   é    )r   r6   r   Fi   é    )Fr   r7   r   FT)Ú__doc__Ú
__future__r   r   r   r   Zcaffe2.protor   Zcaffe2.python.onnx.helperr   r   Zcaffe2.pythonr	   r
   Zcaffe2.python.onnx.frontendÚpythonZonnxZfrontendZc2_frontZ!caffe2.python._import_c_extensionZ_import_c_extensionr   Znumpyr1   r   r   r   r(   r/   r5   r   r   r   r   Ú<module>   s0      
     