U
    dd                     @   s  d dl mZmZ d dlmZ d dl Z d dlZd dlmZmZm	Z	 G dd de j
jZG dd de j
jZG d	d
 d
e j
jZG dd deZG dd deZG dd deZd!eeeedddZG dd de j
jZG dd deZG dd deZdd Ze jfddZe jfdd ZdS )"    )Tensor_VF)PackedSequenceN)ListOptionalTuplec                       s\   e Zd ZddgZ fddZejjdd Zejjdd Z	ejjd	d
 Z
dd Z  ZS )QuantizedLinearscale
zero_pointc                    s   t t|   td |j| _|j| _t|j	j
tjd \| _	| _| _| _tjj| j	dd| _	tjj| jdd| _|jd k	stdtjj|jj
tjd dd| _| dt| j	j
tjd d S )Nztorch.jit.QuantizedLinear is deprecated and will be removed in an upcoming PyTorch release. Please use the torch.nn.quantized.dynamic.Linear instead.Zmemory_formatFZrequires_gradzQuantizedLinear requires a biaspacked_tensor_ptr)superr   __init__warningswarnin_featuresout_featurestorchfbgemm_linear_quantize_weightweightclonecontiguous_formatfloatcol_offsetsr	   r
   nn	ParameterbiasAssertionErrorregister_bufferfbgemm_pack_quantized_matrixselfother	__class__ 7/tmp/pip-unpacked-wheel-ua33x9lu/torch/jit/quantized.pyr      s"    "zQuantizedLinear.__init__c                 C   s   | j t| j d S N)r   set_r   r    r   r"   r&   r&   r'   _unpack!   s    
zQuantizedLinear._unpackc                 C   s.   | j tjtjtt g tjd	  d S N)dtype)
r   r)   r   zerosjitannotater   intuint8detachr*   r&   r&   r'   _pack&   s     zQuantizedLinear._packc              	   C   s2   t | | j| j| j| j| j| j}|	|j
S r(   )r   Z)fbgemm_linear_int8_weight_fp32_activationr   r   r   r   r	   r
   r   tor-   r"   inputoutr&   r&   r'   forward+   s         zQuantizedLinear.forwardc                 C   s   dj f | j}|S )Nz^in_features={in_features}, out_features={out_features}, scale={scale}, zero_point={zero_point}format__dict__r"   reprr&   r&   r'   
extra_repr2   s    zQuantizedLinear.extra_repr)__name__
__module____qualname____constants__r   r   r/   script_methodr+   r4   r9   r?   __classcell__r&   r&   r$   r'   r   
   s   


r   c                       sT   e Zd Z fddZejjdd Zejjdd Zejjdd Z	d	d
 Z
  ZS )QuantizedLinearFP16c                    s   t t|   td |j| _|j| _|j| _t	
|jjt	jd | _|jd k	s^tdt	jj|jjt	jd dd| _| d| j d S )Nztorch.jit.QuantizedLinearFP16 is deprecated and will be removed in an upcoming PyTorch release. Please use the torch.nn.quantized.dynamic.Linear instead.r   z#QuantizedLinearFP16 requires a biasFr   packed_weight)r   rF   r   r   r   r   r   r   original_weightr   fbgemm_pack_gemm_matrix_fp16r   r   r   r   r   r   r   r   r!   r$   r&   r'   r   :   s    "zQuantizedLinearFP16.__init__c                 C   s   | j t| j d S r(   )rG   r)   r   rI   rH   r*   r&   r&   r'   r+   H   s
    zQuantizedLinearFP16._unpackc                 C   s.   | j tjtjtt g tjd	  d S r,   )
rG   r)   r   r.   r/   r0   r   r1   r2   r3   r*   r&   r&   r'   r4   N   s     zQuantizedLinearFP16._packc                 C   s   t | | j| j}|S r(   )r   Z)fbgemm_linear_fp16_weight_fp32_activationr   rG   r   r6   r&   r&   r'   r9   S   s      zQuantizedLinearFP16.forwardc                 C   s   dj f | j}|S )Nz8in_features={in_features}, out_features={out_features}, r:   r=   r&   r&   r'   r?   Y   s    zQuantizedLinearFP16.extra_repr)r@   rA   rB   r   r   r/   rD   r+   r4   r9   r?   rE   r&   r&   r$   r'   rF   8   s   


rF   c                       s   e Zd ZdddddddgZ fdd	Zd
d Zejjdd Z	ejjde
e
eddddZejjdd Zejjdd Z  ZS )QuantizedRNNCellBase
input_sizehidden_sizer   scale_hhscale_ihzero_point_ihzero_point_hhc                    s2  t t|   td |j| _|j| _|j| _| js>tdt	
|jjt	jd \}}| _| _| d| | d| t	
|jjt	jd \}}| _| _| d| | d| t	| j}| d| t	| j}| d	| t	jj|jjt	jd d
d| _t	jj|jjt	jd d
d| _d S )Nztorch.jit.QuantizedRNNCellBase is deprecated and will be removed in an upcoming PyTorch release. Please use the torch.nn.quantized.dynamic.RNNCell instead.z&Quantized RNN cells require bias termsr   	weight_ihcol_offsets_ih	weight_hhcol_offsets_hh	packed_ih	packed_hhFr   )r   rJ   r   r   r   rK   rL   r   
ValueErrorr   r   rQ   r   r   r   rN   rO   r   rS   rM   rP   r    r   r   bias_ihbias_hh)r"   r#   rQ   rR   rS   rT   rU   rV   r$   r&   r'   r   b   s.    "zQuantizedRNNCellBase.__init__c                 C   sJ   d}d| j kr | jdk	r |d7 }d| j kr<| jdkr<|d7 }|jf | j S )Nz{input_size}, {hidden_size}r   Tz, bias={bias}nonlinearitytanhz, nonlinearity={nonlinearity})r<   r   rZ   r;   )r"   sr&   r&   r'   r?      s    zQuantizedRNNCellBase.extra_reprc                 C   s,   | d| jkr(td| d| jd S )N   z6input has inconsistent input_size: got {}, expected {})sizerK   RuntimeErrorr;   )r"   r7   r&   r&   r'   check_forward_input   s     z(QuantizedRNNCellBase.check_forward_input N)r7   hxhidden_labelreturnc                 C   s`   | d| dkr2td| d|| d| d| jkr\td|| d| jd S )Nr   z8Input batch size {} doesn't match hidden{} batch size {}r]   z:hidden{} has inconsistent hidden_size: got {}, expected {})r^   r_   r;   rL   )r"   r7   rb   rc   r&   r&   r'   check_forward_hidden   s         z)QuantizedRNNCellBase.check_forward_hiddenc                 C   s,   | j t| j | jt| j d S r(   )rU   r)   r   r    rQ   rV   rS   r*   r&   r&   r'   r+      s    zQuantizedRNNCellBase._unpackc                 C   sX   | j tjtjtt g tjd	  | j
tjtjtt g tjd	  d S r,   )rU   r)   r   r.   r/   r0   r   r1   r2   r3   rV   r*   r&   r&   r'   r4      s      zQuantizedRNNCellBase._pack)ra   )r@   rA   rB   rC   r   r?   r   r/   rD   r`   r   strre   r+   r4   rE   r&   r&   r$   r'   rJ   ^   s   
 

rJ   c                       sP   e Zd ZddddddddgZ fd	d
Zejjdee	e edddZ
  ZS )QuantizedRNNCellrK   rL   r   rM   rN   rO   rP   rZ   c                    s&   t t| | td |j| _d S )Nztorch.jit.QuantizedRNNCell is deprecated and will be removed in an upcoming PyTorch release. Please use the torch.nn.quantized.dynamic.RNNCell instead.)r   rg   r   r   r   rZ   r!   r$   r&   r'   r      s
    zQuantizedRNNCell.__init__Nr7   rb   rd   c                 C   s   |  | |d kr0tj|d| j|j|jd}| ||d | jdkrt	
||| j| j| j| j| j| j| j| j| j| j| j| j}n\| jdkrt	||| j| j| j| j| j| j| j| j| j| j| j| j}n|}td| j|S )Nr   r-   devicera   r[   ZreluzUnknown nonlinearity: {})r`   r   r.   r^   rL   r-   rj   re   rZ   r   Zquantized_rnn_tanh_cellrQ   rS   rX   rY   rU   rV   rR   rT   rN   rM   rO   rP   Zquantized_rnn_relu_cellr_   r;   )r"   r7   rb   retr&   r&   r'   r9      sV    

          
          
zQuantizedRNNCell.forward)N)r@   rA   rB   rC   r   r   r/   rD   r   r   r9   rE   r&   r&   r$   r'   rg      s   
  rg   c                       sL   e Zd Z fddZejjdeee	eef  e	eef dddZ
  ZS )QuantizedLSTMCellc                    s   t t| | td d S )Nztorch.jit.QuantizedLSTMCell is deprecated and will be removed in an upcoming PyTorch release. Please use the torch.nn.quantized.dynamic.LSTMCell instead.)r   rl   r   r   r   r!   r$   r&   r'   r      s    zQuantizedLSTMCell.__init__Nrh   c                 C   s   |  | |d kr8tj|d| j|j|jd}||f}| ||d d | ||d d t	||| j
| j| j| j| j| j| j| j| j| j| j| jS )Nr   ri   z[0]r]   z[1])r`   r   r.   r^   rL   r-   rj   re   r   Zquantized_lstm_cellrQ   rS   rX   rY   rU   rV   rR   rT   rN   rM   rO   rP   )r"   r7   rb   r.   r&   r&   r'   r9      s,    
          zQuantizedLSTMCell.forward)N)r@   rA   rB   r   r   r/   rD   r   r   r   r9   rE   r&   r&   r$   r'   rl      s   rl   c                       s<   e Zd Z fddZejjdeee edddZ	  Z
S )QuantizedGRUCellc                    s   t t| | td d S )Nztorch.jit.QuantizedGRUCell is deprecated and will be removed in an upcoming PyTorch release. Please use the torch.nn.quantized.dynamic.GRUCell instead.)r   rm   r   r   r   r!   r$   r&   r'   r      s    zQuantizedGRUCell.__init__Nrh   c                 C   sz   |  | |d kr0tj|d| j|j|jd}| ||d t	||| j
| j| j| j| j| j| j| j| j| j| j| jS )Nr   ri   ra   )r`   r   r.   r^   rL   r-   rj   re   r   Zquantized_gru_cellrQ   rS   rX   rY   rU   rV   rR   rT   rN   rM   rO   rP   r"   r7   rb   r&   r&   r'   r9      s(    
          zQuantizedGRUCell.forward)N)r@   rA   rB   r   r   r/   rD   r   r   r9   rE   r&   r&   r$   r'   rm      s   rm   r]   )tensorpermutationdimrd   c                 C   s   |  ||S r(   )Zindex_select)ro   rp   rq   r&   r&   r'   apply_permutation   s    rr   c                	       s   e Zd Zddddddddd	g	Zejf fd
d	Zejje	e
e	 ddddZejje	e
e	 eeeef dddZejjde	eeeef eddddZejje	e	e
e	 ddddZejje	e
e	 e	dddZ  ZS )QuantizedRNNBasemoderK   rL   
num_layersr   batch_firstdropoutbidirectionalr-   c                    s  t t|   td j| _j| _j| _j| _j	| _	j
| _
| jdkr\| j
r\tj| _j| _| jrvdnd}|| _| j	st| jdkr| jdkrtd|tjkr|tjkrtd|g | _t| jD ]މ t|D ]} dkr| jn| j| }|dkrd	nd
 fdd}|d\}}|d\}	}
|tjkr\tjj||	||
}n8tjj| |}tjj|	 |
}tjj||}t| d | | j| qqd S )Nztorch.jit.QuantizedRNNBase is deprecated and will be removed in an upcoming PyTorch release. Please use the torch.nn.quantized.dynamic instead.GRU   r]   LSTMz.Only LSTM or GRU is supported for QuantizedRNNUnsupported dtype: {}r   Z_reversera   c                    s8   d |  }d |  }t|}t|}||fS )Nzweight_{}_l{}{}zbias_{}_l{}{})r;   getattr)ZihhhZweight_nameZ	bias_namer   r   Zlayerr#   suffixr&   r'   get_weight_bias(  s
    

z2QuantizedRNNBase.__init__.<locals>.get_weight_biasZihhhzcell_params_{}_{})r   rs   r   r   r   rt   rK   rL   ru   r   rv   r   rw   rx   r-   r_   r   int8float16r;   all_weightsrangeopsZ	quantizedZmake_quantized_cell_paramsZlinear_prepack_fp16r   Zmake_quantized_cell_params_fp16setattrappend)r"   r#   r-   num_directions	directionZlayer_input_sizer   rQ   rX   rS   rY   Zcell_paramsrU   rV   r$   r~   r'   r     sd    


      zQuantizedRNNBase.__init__N)r7   batch_sizesrd   c                 C   s\   |d k	rdnd}|  |kr0td||  | j|dkrXtd| j|dd S )Nrz      z%input must have {} dimensions, got {}z?input.size(-1) must be equal to input_size. Expected {}, got {})rq   r_   r;   rK   r^   )r"   r7   r   Zexpected_input_dimr&   r&   r'   check_inputB  s      zQuantizedRNNBase.check_inputc                 C   sT   |d k	rt |d }n| jr&|dn|d}| jr:dnd}| j| || jf}|S )Nr   r]   rz   )r1   rv   r^   rx   ru   rL   )r"   r7   r   Z
mini_batchr   expected_hidden_sizer&   r&   r'   get_expected_hidden_sizeN  s     z)QuantizedRNNBase.get_expected_hidden_sizeExpected hidden size {}, got {})rb   r   msgrd   c                 C   s(   |  |kr$t||t|  d S r(   )r^   r_   r;   list)r"   rb   r   r   r&   r&   r'   check_hidden_sizeY  s    z"QuantizedRNNBase.check_hidden_sizer7   hiddenr   rd   c                 C   s,   |  || | ||}| j||dd d S )Nr   )r   r   r   r   r"   r7   r   r   r   r&   r&   r'   check_forward_args_  s    z#QuantizedRNNBase.check_forward_argsrb   rp   rd   c                 C   s   |d kr|S t ||S r(   rr   r"   rb   rp   r&   r&   r'   permute_hiddene  s    zQuantizedRNNBase.permute_hidden)r   )r@   rA   rB   rC   r   r   r   r/   rD   r   r   r   r   r1   r   rf   r   r   r   rE   r&   r&   r$   r'   rs     s*   
   <"
  rs   c                       s<  e Zd ZdddgiZ fddZejjee	e
eef  e	e ee	e e
ee
eef f dddZejjdee	e
eef  e
ee
eef f d
ddZejjdee	e
eef  e
ee
eef f d
ddZejje
eef e	e e
eef dddZejjee
eef e	e d	dddZdddZ  ZS )QuantizedLSTMr9   forward_packedforward_tensorc                    s    t t| || td d S )Nztorch.jit.QuantizedLSTM is deprecated and will be removed in an upcoming PyTorch release. Please use the torch.nn.quantized.dynamic.LSTM instead.)r   r   r   r   r   )r"   r#   r-   r$   r&   r'   r   o  s    zQuantizedLSTM.__init__r7   rb   r   max_batch_sizesorted_indicesrd   c                 C   s   |d kr@| j rdnd}tj| j| || j|j|jd}||f}n| ||}| ||| |d ksft	tj
||| j| j| jt| j| j| j | j| jdd}|d }	|dd  }
|	|
fS )Nrz   r]   ri   F)r-   Zuse_dynamicr   )rx   r   r.   ru   rL   r-   rj   r   r   r   Zquantized_lstmr   r   r   rw   trainingrv   )r"   r7   rb   r   r   r   r   r.   resultoutputr   r&   r&   r'   forward_implu  s.      
    zQuantizedLSTM.forward_implNrh   c           	      C   sL   d }| j r|dn|d}d }d }| |||||\}}|| ||fS Nr   r]   rv   r^   r   r   	r"   r7   rb   r   r   r   unsorted_indicesr   r   r&   r&   r'   r     s    zQuantizedLSTM.forward_tensorc           	      C   sP   |\}}}}|d }t |}| |||||\}}t||||}|| ||fS Nr   r1   r   r   r   	r"   r7   rb   r   r   r   r   r   r   r&   r&   r'   r     s    zQuantizedLSTM.forward_packedr   c                 C   s(   |d kr|S t |d |t |d |fS r   r   r   r&   r&   r'   r     s    zQuantizedLSTM.permute_hiddenr   c                 C   s@   |  || | ||}| |d |d | |d |d d S )Nr   z"Expected hidden[0] size {}, got {}r]   z"Expected hidden[1] size {}, got {}r   r   r&   r&   r'   r     s    z QuantizedLSTM.check_forward_argsc                 C   s&   t |tr| ||S | ||S d S r(   
isinstancer   r   r   rn   r&   r&   r'   r9     s    
zQuantizedLSTM.forward)N)N)N)r@   rA   rB   __overloads__r   r   r/   rD   r   r   r   r1   r   r   r   r   r   r   r9   rE   r&   r&   r$   r'   r   l  s&     2
("	r   c                	       s   e Zd ZdddgiZ fddZejjee	e e	e e
e	e eeef dddZejjdee	e eeef d
ddZejjdee	e eeef d
ddZdddZ  ZS )QuantizedGRUr9   r   r   c                    s   t  j|| td d S )Nztorch.jit.QuantizedGRU is deprecated and will be removed in an upcoming PyTorch release. Please use the torch.nn.quantized.dynamic.GRU instead.)r   r   r   r   )r"   argskwargsr$   r&   r'   r     s    zQuantizedGRU.__init__r   c           
      C   s   |d kr8| j rdnd}tj| j| || j|j|jd}n| ||}| ||| |d krt	||| j
| j| jt| j| j| j | j	}n*t	|||| j
| j| jt| j| j| j 	}|d }|d }	||	fS )Nrz   r]   ri   r   )rx   r   r.   ru   rL   r-   rj   r   r   Zquantized_grur   r   r   rw   r   rv   )
r"   r7   rb   r   r   r   r   r   r   r   r&   r&   r'   r     s2          zQuantizedGRU.forward_implNrh   c           	      C   sL   d }| j r|dn|d}d }d }| |||||\}}|| ||fS r   r   r   r&   r&   r'   r     s    zQuantizedGRU.forward_tensorc           	      C   sP   |\}}}}|d }t |}| |||||\}}t||||}|| ||fS r   r   r   r&   r&   r'   r     s    zQuantizedGRU.forward_packedc                 C   s&   t |tr| ||S | ||S d S r(   r   rn   r&   r&   r'   r9     s    
zQuantizedGRU.forward)N)N)N)r@   rA   rB   r   r   r   r/   rD   r   r   r1   r   r   r   r   r   r9   rE   r&   r&   r$   r'   r     s    
"	"
r   c                 C   s   t d i }|  D ]*\}}|| kr(qt|}||k	r|||< q| D ]\}}t| || qJt| tjj	rvt
| S t| tjjrt| S t| tjjrt| S | S )Nzvquantize_rnn_cell_modules function has been deprecated. Please use torch.ao.quantization.quantize_dynamic API instead.)r   r   named_modulesquantize_rnn_cell_modulesitemsr   r   r   r   ZLSTMCellrl   ZGRUCellrm   ZRNNCellrg   )modulereassignnamemodnew_modr&   r&   r'   r     s"    

r   c                 C   s   t d i }|  D ],\}}|| kr(qt||}||k	r|||< q| D ]\}}t| || qLt| tjj	r|tj
krt| S |tjkrt| S td|| S )Nztquantize_linear_modules function has been deprecated. Please use torch.ao.quantization.quantize_dynamic API instead.r|   )r   r   r   quantize_linear_modulesr   r   r   r   r   ZLinearr   r   r   rF   r_   r;   r   r-   r   r   r   r   r&   r&   r'   r     s&    




r   c                 C   s   t d i }|  D ],\}}|| kr(qt||}||k	r|||< q| D ]\}}t| || qLt| tjj	r|tj
kr|tjkrtd|t| |S t| tjjrt| S | S )Nzqquantize_rnn_modules function has been deprecated. Please use torch.ao.quantization.quantize_dynamic API instead.r|   )r   r   r   quantize_rnn_modulesr   r   r   r   r   r{   r   r   r_   r;   r   ry   r   r   r&   r&   r'   r   +  s"    



r   )r]   )r   r   r   Ztorch.nn.utils.rnnr   r   typingr   r   r   r/   ZScriptModuler   rF   rJ   rg   rl   rm   r1   rr   rs   r   r   r   r   r   r   r&   r&   r&   r'   <module>   s"   .&N&jPA