o
    Ơi                     @   sn   d dl Zd dlZddlmZ ddlmZmZmZ dd Z	eej
jjjdd	 Zeej
jjjjd
d ZdS )    N   )tensorrt_converter   )get_dyn_rangemark_as_int8_layerto_numpyc           	      C   s   |  |}t|jd |_| d|_|rt||j t|s!|jn| }t|s,|j	n|	 }| j
|d|j||d}| d|_|rTt|j|jtj}t|| |  |d}t|jdd |jf |_| d|_|rwt|| |dS )	a  
    TensorRT fully connected layer implicitly flatten last three dimensions at
    the start and implicitly reshape the result to (K, 1, 1) at the end.

    e.g. If input is (N, C, H, W), first it gets flatten to (N, C*H*W). Then after
    going through fully connected operation, it becomes (N, K). Before sending it
    out, it gets reshaped into (N, K, 1, 1) and this is the final output.

    TODO: We can optimize this to get rid of unneccesary transformation.
    )r   r   _pre_shuffler   )inputnum_outputskernelbias_linearN_post_shuffle)add_shuffletupleshapereshape_dimsnamer   dynamic_ranger   weightr   add_fully_connected
get_outputout_featuresr   scale
zero_pointtorchquint8)	networkmod	input_val
layer_nameis_quantizedlayerr   r   	dyn_range r%   W/home/ubuntu/.local/lib/python3.10/site-packages/torch_tensorrt/fx/converters/linear.pycommon_linear
   s.   



r'   c                 C   sH   t |dksJ |d }t|tjjstd| dt| |||ddS )Nr   r	   zLinear received input ) that is not part of the TensorRT region!Fr"   )len
isinstancetrttensorrtITensorRuntimeErrorr'   r   submodargskwargsr!   r    r%   r%   r&   linear8   s   
r4   c                 C   s8   |d }t |tjjstd| dt| |||ddS )Nr   z Quantized Linear received input r(   Tr)   )r+   r,   r-   r.   r/   r'   r0   r%   r%   r&   quantized_linearG   s   
r5   )r-   r,   r   converter_registryr   converter_utilsr   r   r   r'   nnmodulesr4   Linear	quantizedr5   r%   r%   r%   r&   <module>   s   .
