
    sg.$                        d dl Z d dlZd dlmZmZmZmZ d dlZd dlm	Z	m
Z
mZmZmZ d dlmZ d dlmZ d dlmZmZ 	 	 	 	 	 	 ddej*                  j,                  deed	f   d
ee   dee   dee   dee   dee   dee   dej.                  j*                  j0                  j,                  fdZ	 ddej.                  j*                  j0                  j,                  d
ee   dej.                  j*                  j4                  j,                  fdZy)    N)AnyCallableOptionalTuple)default_weight_fake_quantdefault_weight_observerFakeQuantizeBaseQConfigQConfigMapping)BackendConfig)_PartialWrapper)convert_to_reference_fx
prepare_fx
float_lstmexample_inputs.backend_configlinear_output_obs_ctrsigmoid_obs_ctrtanh_obs_ctrcell_state_obs_ctrhidden_state_obs_ctrreturnc                    dt         dt        fd}t        j                  j                  j
                  j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                        }	| j                  |	_        t        | j                        D ]n  }
t        j                  j                  j
                  j                   j"                  j$                  j'                  | |
| j                  d      |	j(                  |
<   p t+               j-                  | j                        }|H|j/                  d ||             |j/                  d ||             |j/                  d	 ||             ||j/                  d
 ||             |	j(                  D ]  }|j0                  j2                  }t5        ||||      }t        j6                  df|t        j8                  df|t        j8                  df|t        j6                  df|t        j8                  df|i}d}d}|j:                  j<                  D ]  }d}|j>                  t        j6                  k(  rt        j6                  |f}|dz  }n6|j>                  t        j8                  k(  rt        j8                  |f}|dz  }np||vrutA        |jB                        dk(  sJ tE        tG        |jB                  jI                                     jJ                  }||   }|tM        || |               ||j0                  _         |	S )aO  
    Return an observed `torch.ao.nn.quantizable.LSTM` created from a `torch.nn.LSTM`
    with specific observers or fake quantizes assigned to the inner ops or submodules.

    In both eager and FX graph mode quantization, `torch.ao.nn.quantizable.LSTM` is
    used as an observed custom module, which is responsible for inserting its own
    observers. By default, all inner ops inherit the parent custom module's QConfig.
    Users who wish to override this behavior may extend `torch.ao.nn.quantizable.LSTM`
    and use this helper function to customize the observer insertion logic.

    This is meant to be used to convert a float module to an observed module in the
    custom module flow.

    Args:
        `float_lstm`: The float LSTM module
        `example_inputs`: example inputs for the forward function of the LSTM module
        `backend_config`: BackendConfig to use to observe the LSTM module
        `linear_output_obs_ctr`: observer or fake quantize for linear outputs Wx + b,
            where W is the weight matrix, b is the bias, and x is either the inputs
            or the hidden state from the previous layer (if any)
        `sigmoid_obs_ctr`: observer or fake quantize for sigmoid activations
        `tanh_obs_ctr`: observer or fake quantize for tanh activations
        `cell_state_obs_ctr`: observer or fake quantize for the cell state
        `hidden_state_obs_ctr`: observer or fake quantize for the hidden state and
            the output

    Return:
        A `torch.ao.nn.quantizable.LSTM` with the specified observers or fake quantizes
        assigned to the inner ops.
    obs_ctrr   c                 `    t         |        t              rt        }nt        }t	        | |      S )zP
        Make a QConfig with fixed qparams observers or fake quantizes.
        )
activationweight)
isinstancer	   r   r   r
   )r   r   s     V/var/www/html/venv/lib/python3.12/site-packages/torch/ao/quantization/fx/lstm_utils.pymake_qconfigz@_get_lstm_with_individually_observed_parts.<locals>.make_qconfig<   s(     gi!12.F,F'&99    F)batch_firstN
input_gateforget_gateoutput_gate	cell_gater   r         )'r   r
   torchaonnquantizableLSTM
input_sizehidden_size
num_layersbiasr"   dropoutbidirectionalqconfigrangemodulesrnn
_LSTMLayer
from_floatlayersr   
set_globalset_module_namelayer_fwcellr   addmulgraphnodestargetlenusersnextiterkeysnamesetattr)r   r   r   r   r   r   r   r   r    quantizable_lstmidxcell_qmlayerr?   'op_index_to_activation_post_process_ctr	add_count	mul_countnodeop_indexactivation_post_process_nameactivation_post_process_ctrs                        r   *_get_lstm_with_individually_observed_partsrW      s   R:o :' : xx{{..33    *11Z**+ 
 HHKK##++//::EEZ//U F 
 		

 ))**<*<=G"l?.KL|O/LM|O/LM\,-GH "(( '#~~""$W YYN1YYN.YYN.YYN.YYN03
/ 		JJ$$ 	D7;H{{eii'!IIy1Q			)!IIy1Q	 FFtzz?a'''+/TZZ__5F0G+H+M+M(*Q+' +668S8U'	, #O'#P r!   observed_lstmc           	      t   t         j                  j                  j                  j	                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                        }t        |j                        D ]  \  }}t        j                  | j                  j!                  t#        |            j$                  j&                        }t)        ||      }t+        |t         j,                  j.                        sJ |j0                  j2                  D ].  }|j4                  t         j6                  k(  r|j8                  d   }|j4                  dk(  s9|j4                  t:        j<                  k(  rl|j8                  d   j4                  dk(  rP|j0                  j?                  |      5  |jA                  |       |j0                  jC                  |       ddd       |j4                  dk(  s|j8                  d   D ]E  }|j0                  j?                  |      5  |jE                  ||j8                  d          ddd       G 1 |j0                  jG                          |jI                          ||j$                  _        
 |S # 1 sw Y   xY w# 1 sw Y   xY w)aO  
    Return a `torch.ao.nn.quantized.LSTM` created from a `torch.ao.nn.quantizable.LSTM`
    with observers or fake quantizes inserted through `prepare_fx`, e.g. from
    `_get_lstm_with_individually_observed_parts`.

    This is meant to be used to convert an observed module to a quantized module in the
    custom module flow.

    Args:
        `observed_lstm`: a `torch.ao.nn.quantizable.LSTM` observed through `prepare_fx`
        `backend_config`: BackendConfig to use to produce the reference quantized model

    Return:
        A reference `torch.ao.nn.quantized.LSTM` module.
    r'   r   xhiddenNoutput)%r*   r+   r,   	quantizedr.   r/   r0   r1   r2   r"   r3   r4   	enumerater;   copydeepcopyget_submodulestrr>   r?   r   r   fxGraphModulerB   rC   rD   quantize_per_tensorargsoperatorgetiteminserting_beforereplace_all_uses_with
erase_nodereplace_input_witheliminate_dead_code	recompile)rX   r   quantized_lstmirO   r?   rS   args           r   $_get_reference_quantized_lstm_modulerr      s,   & XX[[**//  !!  !!##N n334 #5}}]11??AGPPUUV&tNK$ 4 4555 JJ$$ 	BD{{e777iil::$JJ("2"22sxx{7I7IX7U44T: 42237

--d34 {{h&99Q< BC44T: B//SXXa[AB BB	B 	

&&(";#< 4 4B Bs   -J"5 J.	"J+.J7)NNNNNN)N)r_   rg   typingr   r   r   r   r*   torch.ao.quantizationr   r   r	   r
   r   $torch.ao.quantization.backend_configr   torch.ao.quantization.observerr   !torch.ao.quantization.quantize_fxr   r   r,   r.   r+   r-   rW   r]   rr    r!   r   <module>ry      s6     1 1   ? : Q /37;15.2486:yy#s(Oy ]+y $O4	y
 o.y ?+y !1y #?3y XX[[!!y| /3;88;;**//;]+; XX[[;r!   