§
    `ƒiL(  ã                   ó¾  — d dl Z d dlZd dl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dej        j        j        j        fd„Z	 ddej        j        j        j        dee         dej        j        j        j        fd„ZdS )é    N)ÚAnyÚCallableÚOptional)Údefault_weight_fake_quantÚdefault_weight_observerÚFakeQuantizeBaseÚQConfigÚQConfigMapping)ÚBackendConfig)Ú_PartialWrapper)Úconvert_to_reference_fxÚ
prepare_fxFÚ
float_lstmÚexample_inputs.Úbackend_configÚlinear_output_obs_ctrÚsigmoid_obs_ctrÚtanh_obs_ctrÚcell_state_obs_ctrÚhidden_state_obs_ctrÚsplit_gatesÚreturnc	                 óö  — dt           dt          fd„}	t          j        j        j                             | j        | j        | j	        | j
        | j        | j        | j        |¬¦  «        }
| j        |
_        t          | j	        ¦  «        D ]L}t          j        j        j        j        j        j                             | || j        d|¬¦  «        |
j        |<   ŒMt+          ¦   «                              | j        ¦  «        }|]|                     d |	|¦  «        ¦  «         |                     d	 |	|¦  «        ¦  «         |                     d
 |	|¦  «        ¦  «         ||                     d |	|¦  «        ¦  «         |
j        D ]ö}|j        j        }t5          |t          j        j        ¦  «        s
J d¦   «         ‚t9          ||||¬¦  «        }|sIt          j        df|t          j        df|t          j        df|t          j        df|t          j        df|i}nrt          j        df|t          j        df|t          j        df|t          j        df|t          j        df|t          j        df|t          j        df|t          j        df|i}d}d}|j        j         D ]Ñ}d}|j!        t          j        k    rt          j        |f}|dz  }n*|j!        t          j        k    rt          j        |f}|dz  }nŒW||vrŒ\tE          |j#        ¦  «        dk    sJ ‚tI          tK          |j#         &                    ¦   «         ¦  «        ¦  «        j'        }||         }|tQ          || |¦   «         ¦  «         ŒÒ||j        _        Œø|
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     úw/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/ao/quantization/fx/lstm_utils.pyÚmake_qconfigz@_get_lstm_with_individually_observed_parts.<locals>.make_qconfig=   s<   € õ gg‘i”iÕ!1Ñ2Ô2ð 	-Ý.ˆFˆFå,ˆFÝ '°&Ð9Ñ9Ô9Ð9ó    )r   F)Úbatch_firstr   NÚ
input_gateÚforget_gateÚoutput_gateÚ	cell_gatezcell should be a nn.Module©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   ÚModuler   ÚaddÚmulÚgraphÚnodesÚtargetÚlenÚusersÚnextÚiterÚkeysÚnameÚsetattr)r   r   r   r   r   r   r   r   r   r    Úquantizable_lstmÚidxÚcell_qmÚlayerrA   Ú'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_partsrZ      s·  € ðT:oð :µ'ð :ð :ð :ð :õ ”x”{Ô.×3Ò3ØÔØÔØÔØŒØÔØÔØÔ Øð 4ñ 	ô 	Ðð  *Ô1ÐÔåZÔ*Ñ+Ô+ð 	
ð 	
ˆåŒHŒKÔ#Ô+Ô/Ô:×EÒEØØØÔ"Ø!Ø'ð Fñ ô ð 	Ô Ñ$Ð$õ ÑÔ×)Ò)¨*Ô*<Ñ=Ô=€GØÐ"Ø×Ò ¨l¨l¸?Ñ.KÔ.KÑLÔLÐLØ×Ò ¨|¨|¸OÑ/LÔ/LÑMÔMÐMØ×Ò ¨|¨|¸OÑ/LÔ/LÑMÔMÐMØÐØ×Ò ¨\¨\¸,Ñ-GÔ-GÑHÔHÐHð "Ô(ð 4#ñ 4#ˆØŒ~Ô"ˆÝ˜$¥¤¤Ñ0Ô0ÐNÐNÐ2NÑNÔNÐ0Ý˜$ ¨ÈÐWÑWÔWˆð ð 	å”˜AÐ 5Ý”˜AÐ 2Ý”˜AÐ 2Ý”˜AÐ 2Ý”˜AÐ 4ð7Ð3Ð3õ ”˜AÐ 5Ý”˜AÐ 5Ý”˜AÐ 5Ý”˜AÐ 5Ý”˜AÐ 2Ý”˜AÐ 2Ý”˜AÐ 2Ý”˜AÐ 4ð	7Ð3ð ˆ	Øˆ	Ø”JÔ$ð 	ð 	ˆDØ7;ˆHØŒ{eœiÒ'Ð'Ý!œI yÐ1Ø˜Q‘		Ø”¥¤	Ò)Ð)Ý!œI yÐ1Ø˜Q‘		ð ØÐFÐFÐFØÝt”z‘?”? aÒ'Ð'Ð'Ð'Ý+/µ°T´Z·_²_Ñ5FÔ5FÑ0GÔ0GÑ+HÔ+HÔ+MÐ(Ø*QØô+Ð'ð +Ð6ÝØÐ6Ð8SÐ8SÑ8UÔ8Uñô ð øð #ˆŒÔÑØÐr!   Úobserved_lstmc           	      ó®  — t           j        j        j                             | j        | j        | j        | j        | j	        | j
        | j        ¦  «        }t          |j        ¦  «        D ]ì\  }}t          j        | j                             t#          |¦  «        ¦  «        j        j        ¦  «        }t)          ||¬¦  «        }t+          |t           j        j        ¦  «        sJ ‚|j        j        D ]*}|j        t           j        k    r¤|j        d         }|j        dk    s+|j        t:          j        k    rw|j        d         j        dk    ra|j                             |¦  «        5  |                      |¦  «         |j         !                    |¦  «         ddd¦  «         n# 1 swxY w Y   |j        dk    rc|j        d         D ]U}|j                             |¦  «        5  | "                    ||j        d         ¦  «         ddd¦  «         n# 1 swxY w Y   ŒVŒ,|j         #                    ¦   «          | $                    ¦   «          ||j        _        Œî|S )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.   Ú	quantizedr0   r1   r2   r3   r4   r"   r5   r6   Ú	enumerater=   ÚcopyÚdeepcopyÚget_submoduleÚstrr@   rA   r   r   ÚfxÚGraphModulerE   rF   rG   Úquantize_per_tensorÚargsÚoperatorÚgetitemÚinserting_beforeÚreplace_all_uses_withÚ
erase_nodeÚreplace_input_withÚeliminate_dead_codeÚ	recompile)r[   r   Úquantized_lstmÚirR   rA   rV   Úargs           r   Ú$_get_reference_quantized_lstm_moduleru   ¢   s²  € õ& ”X”[Ô*×/Ò/ØÔ ØÔ!ØÔ ØÔØÔ!ØÔØÔ#ñô €Nõ ˜nÔ3Ñ4Ô4ð #ñ #‰ˆˆ5ÝŒ}˜]Ô1×?Ò?ÅÀAÁÄÑGÔGÔPÔUÑVÔVˆÝ& t¸NÐKÑKÔKˆÝ˜$¥¤Ô 4Ñ5Ô5Ð5Ð5Ð5ð ”JÔ$ð 	Bñ 	BˆDØŒ{eÔ7Ò7Ð7Ø”i ”là”: Ò$Ð$Ø”J¥(Ô"2Ò2Ð2°s´xÀ´{Ô7IÈXÒ7UÐ7Uàœ×4Ò4°TÑ:Ô:ð 4ð 4Ø×2Ò2°3Ñ7Ô7Ð7Øœ
×-Ò-¨dÑ3Ô3Ð3ð4ð 4ð 4ñ 4ô 4ð 4ð 4ð 4ð 4ð 4ð 4øøøð 4ð 4ð 4ð 4ð Œ{˜hÒ&Ð&àœ9 Qœ<ð Bð BCØœ×4Ò4°TÑ:Ô:ð Bð BØ×/Ò/°°S´X¸a´[ÑAÔAÐAðBð Bð Bñ Bô Bð Bð Bð Bð Bð Bð Bøøøð Bð Bð Bð BøùàŒ
×&Ò&Ñ(Ô(Ð(ØŠÑÔÐØ"ˆŒÔÑØÐs$   Å#0FÆF#Æ&F#Ç"H	ÈHÈH)NNNNNNF)N)rb   rj   Útypingr   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.   r0   ÚtupleÚboolr-   r/   rZ   r`   ru   © r!   r   ú<module>r~      sñ  ðØ €€€Ø €€€Ø *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *à €€€ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ?Ð >Ð >Ð >Ð >Ð >Ø :Ð :Ð :Ð :Ð :Ð :Ø QÐ QÐ QÐ QÐ QÐ QÐ QÐ Qð /3Ø7;Ø15Ø.2Ø48Ø6:ØðLð LØ””ðLà˜#˜s˜(”OðLð ˜]Ô+ðLð $ OÔ4ð	Lð
 ˜oÔ.ðLð ˜?Ô+ðLð ! Ô1ðLð # ?Ô3ðLð ðLð „X„[ÔÔ!ðLð Lð Lð Lðb /3ð;ð ;Ø”8”;Ô*Ô/ð;à˜]Ô+ð;ð „X„[ÔÔð;ð ;ð ;ð ;ð ;ð ;r!   