
    `iv,                     n   d dl mZmZ d dlZd dlmc mc mZ d dl	mc mc mc m
Z d dlmc mc mc mZ d dlmc mc m
Z d dlmc mc mZ d dlmc mc mc mZ d dlmZ d dlmc mZ d dlmZ d dlmZ ddlmZm Z  ddl!m"Z"m#Z#m$Z$ ej%        j        Z&dej'        d	ej(        fd
Z)dej'        d	ej(        fdZ*dej'        d	ej(        fdZ+dej'        d	e,ej(                 fdZ-dej'        d	e,ej(                 fdZ.dej'        d	ej(        fdZ/dej'        d	ej(        fdZ0dej'        d	e,ej(                 fdZ1deded	ej(        fdZ2deded	ej(        fdZ3deded	ej(        fdZ4deded	ej(        fdZ5d	e6e7e6eef         f         fdZ8	 ddededee6e7e6eef         f                  d	ee         fdZ9dS )    )CallableOptionalN)GraphModule)Node   )NSSingleResultTypeNSSingleResultValuesType)get_target_type_strgetattr_from_fqnreturn_first_non_observer_nodemodreturnc                 4    | j                                         S Nweightdetachr   s    o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/ao/ns/fx/weight_utils.pymod_weight_detachr      s    :    c                 @    | d         j                                         S Nr   r   r   s    r   mod_0_weight_detachr      s    q6=!!!r   c                 6    |                                  d         S r   )_weight_biasr   s    r   mod_weight_bias_0r      s    a  r   c                     g }t          | j                  D ]A\  }}d|v sd|v r4| j        |                                         }|                    |           B|S )Nweight_ih_lweight_hh_l)	enumerate_flat_weights_names_flat_weightsr   append)r   residx
param_nameparam_values        r   get_lstm_weightr)   "   sk    
C$S%<== $ $ZJ&&-:*E*E+C07799KJJ{###Jr   c                    g }| j         D ]}|                    |j                                        d         d         d                                         d         d                    |                    |j                                        d         d         d                                         d         d                    |S )Nr      r   )_all_weight_valuesr$   param__getstate__)r   r%   weight_values      r   get_qlstm_weightr0   +   s    
C. T T

<%2244Q7:1=JJLLQOPQRSSS

<%2244Q7:1=JJLLQOPQRSSSSJr   c                 j   t          | t          j        t          j        t          j        f          r| j                                        S t          | t          j        t          j	        t          j
        f          r| d         j                                        S |                                 d         S r   )
isinstancennConv1dConv2dConv3dr   r   nni
ConvReLU1d
ConvReLU2d
ConvReLU3dr   r   s    r   get_conv_mod_weightr;   3   s    #	29bi899 %z  """	C#.#.#.I	J	J %1v}##%%%!!!$$r   c                    t          | t          j                  r| j                                        S t          | t
          j                  r| d         j                                        S |                                 d         S r   )r2   r3   Linearr   r   r7   
LinearReLUr   r   s    r   get_linear_mod_weightr?   <   sm    #ry!! %z  """	C	(	( %1v}##%%%!!!$$r   c                    t          | t          j                  rZg }t          | j                  D ]A\  }}d|v sd|v r4| j        |                                         }|                    |           B|S t          | t          j                  sJ dt          |            d            g }| j
        D ]}|                    |j                                        d         d         d                                         d         d                    |                    |j                                        d         d         d                                         d         d                    |S )Nr   r    ztype z not handled yetr   r+   r   )r2   r3   LSTMr!   r"   r#   r   r$   nnqdtyper,   r-   r.   )r   r%   r&   r'   r(   r/   s         r   get_lstm_mod_weightsrD   E   sp   #rw ()@AA 	( 	(OC
**mz.I.I!/4;;==

;'''
#ty))NN+N499+N+N+NNN)2 	 	LJJ"//11!4Q7:GGII!LQO   JJ"//11!4Q7:GGII!LQO    
r   nodegmc                    | j         d         }t          |t                    sJ t          ||          }t          |t                    sJ |j        dk    sJ t          ||j                  }|                                S Nr   get_attr)argsr2   r   r   opr   targetr   )rE   rF   weight_arg_nodeweight_noder   s        r   get_conv_fun_weightrO   [   s{    ilOot,,,,,0"EEKk4(((((>Z''''b+"455F==??r   c                     | j         d         }t          |t                    sJ |j        dk    sJ t	          ||j                  }|                                S rH   )rJ   r2   r   rK   r   rL   r   )rE   rF   qconv_state_nodeqconv_state_objs       r   get_qconv_fun_weightrS   f   s^    y|&-----*,,,,&r+;+BCCO!!###r   c                 |   | j         d         }t          |t                    sJ |j        dk    r~| j         d         }t          |t                    sJ |j         d         }t          |t                    sJ |j        dk    sJ t	          ||j                  }|                                S |j        dk    r|j        dk    sJ | j         d         }t          |t                    sJ |j         d         }t          |t                    sJ |j         d         }|j         d         }t          |t                    sJ |j        dk    sJ t	          ||j                  }|                                                    |          S |j        dk    sJ t	          ||j                  }|                                S )Nr   call_moduler   rI   call_method)rJ   r2   r   rK   r   rL   r   to)	rE   rF   linear_second_argrM   rN   r   dequant_nodeto_fp16_nodetarget_dtypes	            r   get_linear_fun_weightr\   o   s   
 	!'.....},,)A,/400000%*1-+t,,,,,~++++!"k&899}}			.	. #}4444y|,-----#(+,-----#(+"'*+t,,,,,~++++!"k&899}}!!,/// #z1111!"&7&>??}}r   c                     | j         d         }t          |t                    sJ |j        dk    sJ t	          ||j                  }|                                \  \  }}}|S rH   )rJ   r2   r   rK   r   rL   r.   )rE   rF   packed_weight_nodepacked_weightr   _bias_names          r   get_qlinear_fun_weightrb      sk    1($///// J....$R);)BCCM*7799OVUUMr   c                     i t           j        t          t          j        t
          t          j        t          t          j        t          t          j
        t          t          j        t          t          j        t          t          j        t          t           j        t          t          j        t
          t          j        t          t          j        t          t          j        t          t          j        t          t          j        t          t          j        t          t           j        t          i t          j        t
          t          j        t          t          j        t          t          j        t          t          j        t          t          j        t          t          j        t          t           j        t          t          j        t          t          j        t
          t          j        t          t          j        t          t.          j        t          t          j        t          t          j        t          t           j        j        j        t          t           j        t:          t.          j        t<          it>          j         tB          t>          j"        tB          t>          j#        tB          tH          j         tJ          tH          j"        tJ          tH          j#        tJ          tH          j&        tJ          tH          j'        tJ          tH          j(        tJ          t>          j        tR          tH          j        tT          tH          j+        tT          id} | S )N)rU   call_function),r3   r4   r   r7   r8   r   nnqr   nnqatnniqatConvBn1dConvBnReLU1dnniqr5   r9   ConvBn2dConvBnReLU2dr6   r:   ConvBn3dConvBnReLU3dr=   r>   rB   
LinearBn1dmoduleslinearNonDynamicallyQuantizableLinearrA   r)   r0   Fconv1drO   conv2dconv3dtoqrS   conv1d_reluconv2d_reluconv3d_relur\   rb   linear_relu)"op_to_type_to_weight_extraction_fns    r   &get_op_to_type_to_weight_extraction_fnr}      s   )
I()
 N/)
 J)	)

 L+)
 O.)
 !2)
 0)
 O.)
 I()
 N/)
 J))
 L+)
 O.)
  !2!)
" 0#)
$ O.%)
( I())
 )
* N/+)
, J)-)
. L+/)
0 O.1)
2 !23)
4 05)
6 O.7)
: I(;)
< J)=)
> N/?)
@ O.A)
B L+C)
D K*E)
F 0G)
H 0I)
J J=?PK)
N G_O)
 )
P I'Q)
 )
X H)H)H)J,J,J,O1O1O1H+J.O3
W;O ;O&z .-r   r|   c                    t           j        j        }d }t          |d          r|j        | j                 d         }|t                      }t          | |          }|}| j        dk    rS|d         }|	                                D ]5\  }}	| j
        |k    r% |	| |          }
||
g| j        || j        |dd|d	c S 6n| j        dk    rt          | j
        t                    sJ t          || j
                  }|d         }|	                                D ]<\  }}	t          |          |k    r$ |	|          }
||
g| j        || j        |dd|d	c S =d S )N_node_name_to_scoper   rd   )	rC   valuesprev_node_nameprev_node_target_typeref_node_nameref_node_target_typeindex_within_argindex_of_argfqnrU   )r	   WEIGHTvaluehasattrr   namer}   r
   rK   itemsrL   r2   strr   rC   )rE   rF   r|   res_typer   ref_node_typeprev_node_typefunction_mappingtarget_fn_typeweight_extraction_fnr   r   module_mappingtarget_mod_types                 r   extract_weight_from_noder      s    (.4H Cr()) 3$TY/2)1-S-U-U*'b11M"Nw/!!=oN4D4J4J4L4L 	 	0N0{n,,--dB77$%h&*i-;%)Y,9()$%
 
 
 
 
 -	 
M	!	!$+s+++++r4;//;MJ5C5I5I5K5K 	 	1O1CyyO++--c22$%h&*i-;%)Y,9()$%
 
 
 
 
 , 4r   r   ):typingr   r   torchtorch.ao.nn.intrinsicaor3   	intrinsicr7   torch.ao.nn.intrinsic.qatqatrg   torch.ao.nn.intrinsic.quantized	quantizedrj   torch.ao.nn.qatrf   torch.ao.nn.quantizedre   torch.ao.nn.quantized.dynamicdynamicrB   torch.nntorch.nn.functional
functionalrs   torch.fxr   torch.fx.graphr   ns_typesr   r	   utilsr
   r   r   opsrw   ModuleTensorr   r   r   listr)   r0   r;   r?   rD   rO   rS   r\   rb   dictr   r}   r    r   r   <module>r      s6   % % % % % % % %  # # # # # # # # # # # # * * * * * * * * * * * * * * * . . . . . . . . . . . . . . .             # # # # # # # # # # # # , , , , , , , , , , , , , , ,                                  B B B B B B B B X X X X X X X X X X i29     "RY "5< " " " "!29 ! ! ! ! ! tEL'9    ") U\(:    %RY %5< % % % %%ry %U\ % % % %bi D,>    ,d      $t $ $ $ $ $ $# #+ #%, # # # #L ; 5<    >.S$x?Q:R5R0S >. >. >. >.L 	; ;
;; )1S$x)**+);  !; ; ; ; ; ;r   