a
    ;=icd                     @   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 usJ 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register_bufferfbgemm_pack_quantized_matrixselfother	__class__ d/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/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 i | 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*   r3   r8   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 us^J 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   rE   r   r   r   r   r   r   original_weightr   fbgemm_pack_gemm_matrix_fp16r   r   r   r   r   r   r   r    r#   r%   r&   r   :   s    "zQuantizedLinearFP16.__init__c                 C   s   | j t| j d S r'   )rF   r(   r   rH   rG   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+   )
rF   r(   r   r-   r.   r/   r   r0   r1   r2   r)   r%   r%   r&   r3   N   s     zQuantizedLinearFP16._packc                 C   s   t | | j| j}|S r'   )r   Z)fbgemm_linear_fp16_weight_fp32_activationr   rF   r   r5   r%   r%   r&   r8   S   s    zQuantizedLinearFP16.forwardc                 C   s   dj f i | j}|S )Nz8in_features={in_features}, out_features={out_features}, r9   r<   r%   r%   r&   r>   Y   s    zQuantizedLinearFP16.extra_repr)r?   r@   rA   r   r   r.   rC   r*   r3   r8   r>   rD   r%   r%   r#   r&   rE   8   s   


rE   c                       sz   e Zd Zg d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   rI   r   r   r   rJ   rK   r   
ValueErrorr   r   rP   r   r   r   rM   rN   r   rR   rL   rO   r   r   r   bias_ihbias_hh)r!   r"   rP   rQ   rR   rS   rT   rU   r#   r%   r&   r   b   s.    "zQuantizedRNNCellBase.__init__c                 C   sN   d}d| j v r | jdur |d7 }d| j v r<| jdkr<|d7 }|jf i | j S )Nz{input_size}, {hidden_size}r   Tz, bias={bias}nonlinearitytanhz, nonlinearity={nonlinearity})r;   r   rY   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 {})sizerJ   RuntimeErrorr:   )r!   r6   r%   r%   r&   check_forward_input   s    z(QuantizedRNNCellBase.check_forward_input N)r6   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:   rK   )r!   r6   ra   rb   r%   r%   r&   check_forward_hidden   s    z)QuantizedRNNCellBase.check_forward_hiddenc                 C   s,   | j t| j | jt| j d S r'   )rT   r(   r   r   rP   rU   rR   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+   )rT   r(   r   r-   r.   r/   r   r0   r1   r2   rU   r)   r%   r%   r&   r3      s      zQuantizedRNNCellBase._pack)r`   )r?   r@   rA   rB   r   r>   r   r.   rC   r_   r   strrd   r*   r3   rD   r%   r%   r#   r&   rI   ^   s   

rI   c                       sD   e Zd Zg dZ fddZejjdee	e edddZ
  ZS )	QuantizedRNNCell)rJ   rK   r   rL   rM   rN   rO   rY   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   rf   r   r   r   rY   r    r#   r%   r&   r      s
    zQuantizedRNNCell.__init__Nr6   ra   rc   c                 C   s   |  | |d u 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,   devicer`   rZ   ZreluzUnknown nonlinearity: {})r_   r   r-   r]   rK   r,   ri   rd   rY   r   Zquantized_rnn_tanh_cellrP   rR   rW   rX   rT   rU   rQ   rS   rM   rL   rN   rO   Zquantized_rnn_relu_cellr^   r:   )r!   r6   ra   retr%   r%   r&   r8      s.    



zQuantizedRNNCell.forward)N)r?   r@   rA   rB   r   r   r.   rC   r   r   r8   rD   r%   r%   r#   r&   rf      s   rf   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   rk   r   r   r   r    r#   r%   r&   r      s    zQuantizedLSTMCell.__init__Nrg   c                 C   s   |  | |d u 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   rh   z[0]r\   z[1])r_   r   r-   r]   rK   r,   ri   rd   r   Zquantized_lstm_cellrP   rR   rW   rX   rT   rU   rQ   rS   rM   rL   rN   rO   )r!   r6   ra   r-   r%   r%   r&   r8      s    
zQuantizedLSTMCell.forward)N)r?   r@   rA   r   r   r.   rC   r   r   r   r8   rD   r%   r%   r#   r&   rk      s   rk   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   rl   r   r   r   r    r#   r%   r&   r      s    zQuantizedGRUCell.__init__Nrg   c                 C   sz   |  | |d u r0tj|d| j|j|jd}| ||d t	||| j
| j| j| j| j| j| j| j| j| j| j| jS )Nr   rh   r`   )r_   r   r-   r]   rK   r,   ri   rd   r   Zquantized_gru_cellrP   rR   rW   rX   rT   rU   rQ   rS   rM   rL   rN   rO   r!   r6   ra   r%   r%   r&   r8      s    
zQuantizedGRUCell.forward)N)r?   r@   rA   r   r   r.   rC   r   r   r8   rD   r%   r%   r#   r&   rl      s   rl   r\   )tensorpermutationdimrc   c                 C   s   |  ||S r'   )Zindex_select)rn   ro   rp   r%   r%   r&   apply_permutation   s    rq   c                       s   e Zd Zg d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)	moderJ   rK   
num_layersr   batch_firstdropoutbidirectionalr,   c                    s  t t|   td j| _j| _j| _j| _j	| _	j
| _
| jdkr\| j
r\J j| _j| _| jrvdnd}|| _| j	sJ | 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_reverser`   c                    s8   d |  }d |  }t|}t|}||fS )Nzweight_{}_l{}{}zbias_{}_l{}{})r:   getattr)ZihhhZweight_nameZ	bias_namer   r   layerr"   suffixr%   r&   get_weight_bias(  s
    

z2QuantizedRNNBase.__init__.<locals>.get_weight_biasZihhhzcell_params_{}_{})r   rr   r   r   r   rs   rJ   rK   rt   r   ru   rv   rw   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   rP   rW   rR   rX   Zcell_paramsrT   rU   r#   r}   r&   r     sX    


zQuantizedRNNBase.__init__N)r6   batch_sizesrc   c                 C   s\   |d urdnd}|  |kr0td||  | j|dkrXtd| j|dd S )Nry      z%input must have {} dimensions, got {}z?input.size(-1) must be equal to input_size. Expected {}, got {})rp   r^   r:   rJ   r]   )r!   r6   r   Zexpected_input_dimr%   r%   r&   check_inputB  s    zQuantizedRNNBase.check_inputc                 C   sT   |d urt |d }n| jr&|dn|d}| jr:dnd}| j| || jf}|S )Nr   r\   ry   )r0   ru   r]   rw   rt   rK   )r!   r6   r   Z
mini_batchr   expected_hidden_sizer%   r%   r&   get_expected_hidden_sizeN  s    z)QuantizedRNNBase.get_expected_hidden_sizeExpected hidden size {}, got {})ra   r   msgrc   c                 C   s(   |  |kr$t||t|  d S r'   )r]   r^   r:   list)r!   ra   r   r   r%   r%   r&   check_hidden_sizeY  s    z"QuantizedRNNBase.check_hidden_sizer6   hiddenr   rc   c                 C   s,   |  || | ||}| j||dd d S )Nr   )r   r   r   r   r!   r6   r   r   r   r%   r%   r&   check_forward_args_  s    z#QuantizedRNNBase.check_forward_argsra   ro   rc   c                 C   s   |d u r|S t ||S r'   rq   r!   ra   ro   r%   r%   r&   permute_hiddene  s    zQuantizedRNNBase.permute_hidden)r   )r?   r@   rA   rB   r   r   r   r.   rC   r   r   r   r   r0   r   re   r   r   r   rD   r%   r%   r#   r&   rr     s   <"
 rr   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 )QuantizedLSTMr8   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__r6   ra   r   max_batch_sizesorted_indicesrc   c                 C   s   |d u r@| j rdnd}tj| j| || j|j|jd}||f}n| ||}| ||| |d u sfJ tj	||| j
| j| jt| j| j| j | j| jdd}|d }	|dd  }
|	|
fS )Nry   r\   rh   F)r,   Zuse_dynamicr   )rw   r   r-   rt   rK   r,   ri   r   r   Zquantized_lstmr   r   r   rv   trainingru   )r!   r6   ra   r   r   r   r   r-   resultoutputr   r%   r%   r&   forward_implu  s"    

zQuantizedLSTM.forward_implNrg   c           	      C   sL   d }| j r|dn|d}d }d }| |||||\}}|| ||fS Nr   r\   ru   r]   r   r   	r!   r6   ra   r   r   r   unsorted_indicesr   r   r%   r%   r&   r     s    zQuantizedLSTM.forward_tensorc           	      C   sP   |\}}}}|d }t |}| |||||\}}t||||}|| ||fS Nr   r0   r   r   r   	r!   r6   ra   r   r   r   r   r   r   r%   r%   r&   r     s    zQuantizedLSTM.forward_packedr   c                 C   s(   |d u 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   rm   r%   r%   r&   r8     s    
zQuantizedLSTM.forward)N)N)N)r?   r@   rA   __overloads__r   r   r.   rC   r   r   r   r0   r   r   r   r   r   r   r8   rD   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 )QuantizedGRUr8   r   r   c                    s    t  j|i | 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 u r8| j rdnd}tj| j| || j|j|jd}n| ||}| ||| |d u rt	||| j
| j| jt| j| j| j | j	}n*t	|||| j
| j| jt| j| j| j 	}|d }|d }	||	fS )Nry   r\   rh   r   )rw   r   r-   rt   rK   r,   ri   r   r   Zquantized_grur   r   r   rv   r   ru   )
r!   r6   ra   r   r   r   r   r   r   r   r%   r%   r&   r     s&    zQuantizedGRU.forward_implNrg   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   rm   r%   r%   r&   r8     s    
zQuantizedGRU.forward)N)N)N)r?   r@   rA   r   r   r   r.   rC   r   r   r0   r   r   r   r   r   r8   rD   r%   r%   r#   r&   r     s   "	"
r   c                 C   s   t d i }|  D ]*\}}|| u r(qt|}||u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LSTMCellrk   ZGRUCellrl   ZRNNCellrf   )modulereassignnamemodnew_modr%   r%   r&   r     s"    

r   c                 C   s   t d i }|  D ],\}}|| u r(qt||}||u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   rE   r^   r:   r   r,   r   r   r   r   r%   r%   r&   r     s&    




r   c                 C   s   t d i }|  D ],\}}|| u r(qt||}||u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   rz   r   r   r^   r:   r   rx   r   r   r%   r%   r&   r   +  s"    



r   )r\   )r   r   r   Ztorch.nn.utils.rnnr   r   typingr   r   r   r.   ZScriptModuler   rE   rI   rf   rk   rl   r0   rq   rr   r   r   r   r   r   r   r%   r%   r%   r&   <module>   s"   .&N&jPA