U
    9%ek                     @   s  d dl Z d dlmZmZmZ d dlZd dlmZmZ d dl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 )"    N)ListOptionalTuple)_VFTensor)PackedSequencec                       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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.ao.nn.quantized.dynamic.Linear instead.Zmemory_formatFZrequires_gradzQuantizedLinear requires a biaspacked_tensor_ptr)super__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__ R/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/jit/quantized.pyr      s6    
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'   _pack2   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'   forward8   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_reprE   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d |j| _|j| _|j| _t	|jj
tjd | _|jd k	sZt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.ao.nn.quantized.dynamic.Linear instead.r   z#QuantizedLinearFP16 requires a biasFr   packed_weight)r   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   O   s     
zQuantizedLinearFP16.__init__c                 C   s   | j t| j d S r(   )rG   r)   r   rI   rH   r*   r&   r&   r'   r+   b   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   h   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   n   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?   u   s    zQuantizedLinearFP16.extra_repr)r@   rA   rB   r   r   r/   rD   r+   r4   r9   r?   rE   r&   r&   r$   r'   rF   N   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                    s.  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.ao.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   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      sR    
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   s0   | d| jkr,td| d d| j d S )N   z'input has inconsistent input_size: got , expected )sizerK   RuntimeError)r"   r7   r&   r&   r'   check_forward_input   s    z(QuantizedRNNCellBase.check_forward_input N)r7   hxhidden_labelreturnc              	   C   sp   | d| dkr:td| d d| d| d | d| jkrltd| d| d d| j d S )	Nr   zInput batch size z doesn't match hiddenz batch size r]   hiddenz# has inconsistent hidden_size: got r^   )r_   r`   rL   )r"   r7   rc   rd   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)rb   )r@   rA   rB   rC   r   r?   r   r/   rD   ra   r   strrg   r+   r4   rE   r&   r&   r$   r'   rJ   }   s0   
0
   
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d |j| _d S )Nztorch.jit.QuantizedRNNCell is deprecated and will be removed in an upcoming PyTorch release. Please use the torch.ao.nn.quantized.dynamic.RNNCell instead.)r   r   r   r   rZ   r!   r$   r&   r'   r      s
    zQuantizedRNNCell.__init__Nr7   rc   re   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-   devicerb   r[   ZreluzUnknown nonlinearity: )ra   r   r.   r_   rL   r-   rl   rg   rZ   r   Zquantized_rnn_tanh_cellrQ   rS   rX   rY   rU   rV   rR   rT   rN   rM   rO   rP   Zquantized_rnn_relu_cellr`   )r"   r7   rc   retr&   r&   r'   r9      s\    
   

zQuantizedRNNCell.forward)N)r@   rA   rB   rC   r   r   r/   rD   r   r   r9   rE   r&   r&   r$   r'   ri      s   ri   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d d S )Nztorch.jit.QuantizedLSTMCell is deprecated and will be removed in an upcoming PyTorch release. Please use the torch.ao.nn.quantized.dynamic.LSTMCell instead.r   r   r   r   r!   r$   r&   r'   r   .  s    zQuantizedLSTMCell.__init__Nrj   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   rk   z[0]r]   z[1])ra   r   r.   r_   rL   r-   rl   rg   r   Zquantized_lstm_cellrQ   rS   rX   rY   rU   rV   rR   rT   rN   rM   rO   rP   )r"   r7   rc   r.   r&   r&   r'   r9   5  s6    
   zQuantizedLSTMCell.forward)N)r@   rA   rB   r   r   r/   rD   r   r   r   r9   rE   r&   r&   r$   r'   rn   -  s     
rn   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d d S )Nztorch.jit.QuantizedGRUCell is deprecated and will be removed in an upcoming PyTorch release. Please use the torch.ao.nn.quantized.dynamic.GRUCell instead.ro   r!   r$   r&   r'   r   T  s    zQuantizedGRUCell.__init__Nrj   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   rk   rb   )ra   r   r.   r_   rL   r-   rl   rg   r   Zquantized_gru_cellrQ   rS   rX   rY   rU   rV   rR   rT   rN   rM   rO   rP   r"   r7   rc   r&   r&   r'   r9   [  s2    
   zQuantizedGRUCell.forward)N)r@   rA   rB   r   r   r/   rD   r   r   r9   rE   r&   r&   r$   r'   rp   S  s   rp   r]   )tensorpermutationdimre   c                 C   s   |  ||S r(   )Zindex_select)rr   rs   rt   r&   r&   r'   apply_permutationu  s    ru   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d j| _j| _j| _j| _j| _j	| _	| jdkrX| j	rXt
j| _j| _| jrrd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rXtjj||	||
}n8tjj| |}tjj|	 |
}tjj||}t| d  d | | j| qqd S )Nztorch.jit.QuantizedRNNBase is deprecated and will be removed in an upcoming PyTorch release. Please use the torch.ao.nn.quantized.dynamic instead.GRU   r]   LSTMz.Only LSTM or GRU is supported for QuantizedRNNUnsupported dtype: r   Z_reverserb   c                    sD   d|  d   }d|  d   }t |}t |}||fS )NZweight_Z_lZbias_)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   r   r   r   rw   rK   rL   rx   r   ry   r   rz   r{   r-   r`   r   int8float16all_weightsrangeZ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     sf    



      zQuantizedRNNBase.__init__N)r7   batch_sizesre   c                 C   sd   |d k	rdnd}|  |kr4td| d|   | j|dkr`td| j d|d d S )Nr}      zinput must have z dimensions, got z5input.size(-1) must be equal to input_size. Expected z, got )rt   r`   rK   r_   )r"   r7   r   Zexpected_input_dimr&   r&   r'   check_input  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]   r}   )r1   ry   r_   r{   rx   rL   )r"   r7   r   Z
mini_batchr   expected_hidden_sizer&   r&   r'   get_expected_hidden_size  s    z)QuantizedRNNBase.get_expected_hidden_sizeExpected hidden size {}, got {})rc   r   msgre   c                 C   s(   |  |kr$t||t|  d S r(   )r_   r`   r;   list)r"   rc   r   r   r&   r&   r'   check_hidden_size  s    z"QuantizedRNNBase.check_hidden_sizer7   rf   r   re   c                 C   s,   |  || | ||}| j||dd d S )Nr   )r   r   r   r   r"   r7   rf   r   r   r&   r&   r'   check_forward_args  s      z#QuantizedRNNBase.check_forward_argsrc   rs   re   c                 C   s   |d kr|S t ||S r(   ru   r"   rc   rs   r&   r&   r'   permute_hidden  s    zQuantizedRNNBase.permute_hidden)r   )r@   rA   rB   rC   r   r   r   r/   rD   r   r   r   r   r1   r   rh   r   r   r   rE   r&   r&   r$   r'   rv   y  sD   C  	  	rv   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d d S )Nztorch.jit.QuantizedLSTM is deprecated and will be removed in an upcoming PyTorch release. Please use the torch.ao.nn.quantized.dynamic.LSTM instead.ro   )r"   r#   r-   r$   r&   r'   r     s    zQuantizedLSTM.__init__r7   rc   r   max_batch_sizesorted_indicesre   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 )Nr}   r]   rk   F)r-   Zuse_dynamicr   )r{   r   r.   rx   rL   r-   rl   r   r   r   Zquantized_lstmr   r   r   rz   trainingry   )r"   r7   rc   r   r   r   r   r.   resultoutputrf   r&   r&   r'   forward_impl
  s:    	
zQuantizedLSTM.forward_implNrj   c           	      C   sL   d }| j r|dn|d}d }d }| |||||\}}|| ||fS Nr   r]   ry   r_   r   r   	r"   r7   rc   r   r   r   unsorted_indicesr   rf   r&   r&   r'   r   6  s        zQuantizedLSTM.forward_tensorc           
      C   sL   |\}}}}t |d }| |||||\}}	t||||}|| |	|fS Nr   r1   r   r   r   
r"   r7   rc   Zinput_r   r   r   r   r   rf   r&   r&   r'   r   E  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  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   rq   r&   r&   r'   r9   n  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      sH   +    
 
	
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.ao.nn.quantized.dynamic.GRU instead.ro   )r"   argskwargsr$   r&   r'   r   x  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 )Nr}   r]   rk   r   )r{   r   r.   rx   rL   r-   rl   r   r   Zquantized_grur   r   r   rz   r   ry   )
r"   r7   rc   r   r   r   r   r   r   rf   r&   r&   r'   r     sJ    	
zQuantizedGRU.forward_implNrj   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   sL   |\}}}}t |d }| |||||\}}	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   rq   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   u  s2   
5  
  
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LSTMCellrn   ZGRUCellrp   ZRNNCellri   )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     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|   r   r   r&   r&   r'   r   	  s&    


r   )r]   )r   typingr   r   r   r   r   r   Ztorch.nn.utils.rnnr   r/   ZScriptModuler   rF   rJ   ri   rn   rp   r1   ru   rv   r   r   r   r   r   r   r&   r&   r&   r'   <module>   s$   D/lD&" uc