
    Ph|                     &
   U d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	 d dl
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mZ d dlmZmZmZmZ d dlmZmZ d d	lm Z  d d
l!m"Z" d dl#m$Z$ g dZ% edd       G d d             Z&ee   Z'de'_(        ee
jR                  jT                  e	e&   e	ee ge+f      ge	eee          f   Z,i Z-ee.e,f   e/d<   de.fdZ0 G d de      Z1dee    fdZ2dee    fdZ3de	e&   fdZ4de	e&   fdZ5de	e&   fdZ6de	e&   fdZ7 e0d      	 dDd e
jR                  jT                  de	e&   d!e	ee ge+f      d"e	eee          fd#       Z8 e0d$      	 dDd e
jR                  jT                  de	e&   d!e	ee ge+f      d"e	eee          fd%       Z9 e0d&      	 dDd e
jR                  jT                  de	e&   d!e	ee ge+f      d"e	eee          fd'       Z: e0d(      	 dDd e
jR                  jT                  de	e&   d!e	ee ge+f      d"e	eee          fd)       Z; e0d*      	 dDd e
jR                  jT                  de	e&   d!e	ee ge+f      d"e	eee          fd+       Z<d e
jR                  jT                  de	e&   d!e	ee ge+f      d,e+d"eee       f
d-Z= e0d.      	 dDd e
jR                  jT                  de	e&   d!e	ee ge+f      d"e	eee          fd/       Z> e0d0      	 dDd e
jR                  jT                  de	e&   d!e	ee ge+f      d"e	eee          fd1       Z? e0d2      	 dDd e
jR                  jT                  de	e&   d!e	ee ge+f      d"e	eee          fd3       Z@d4e d e
jR                  jT                  fd5ZA e0d6      	 dDd e
jR                  jT                  de	e&   d!e	ee ge+f      d"e	eee          fd7       ZB e0d8      	 dDd e
jR                  jT                  de	e&   d!e	ee ge+f      d"e	eee          fd9       ZC e0d:      	 dDd e
jR                  jT                  de	e&   d!e	ee ge+f      d"e	eee          fd;       ZD e0d<      	 dDd e
jR                  jT                  de	e&   d!e	ee ge+f      d"e	eee          fd=       ZE e0d>      	 dDd e
jR                  jT                  de	e&   d!e	ee ge+f      d"e	eee          fd?       ZFded"e+fd@ZGdAe
jR                  jT                  d"dfdBZHdAe
jR                  jT                  d"e
jR                  jT                  fdCZIy)E    N)	dataclass)CallableDictList
NamedTupleOptional)get_new_attr_name_with_prefix)find_sequential_partitions)_conv1d_bn_example_inputs_conv2d_bn_example_inputsget_aten_graph_module)QuantizationAnnotationQuantizationSpecQuantizationSpecBaseSharedQuantizationSpec)_annotate_input_qspec_map_annotate_output_qspec)Node)SubgraphMatcherWithNameNodeMap)get_source_partitions)	OperatorConfigOperatorPatternTypeQuantizationConfigget_input_act_qspecget_output_act_qspecget_weight_qspecget_bias_qspecOP_TO_ANNOTATORpropagate_annotationT)eqfrozenc                   \    e Zd ZU ee   ed<   ee   ed<   ee   ed<   ee   ed<   dZeed<   y)r   input_activationoutput_activationweightbiasFis_qatN)__name__
__module____qualname__r   r   __annotations__r'   bool     C:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/ao/quantization/quantizer/xnnpack_quantizer_utils.pyr   r   /   s<    /00 011%&&
#
$$FDr.   r   z7torch.ao.quantization.quantizer.xnnpack_quantizer_utilsr   opc                       dt         f fd}|S )N	annotatorc                     | t         <   y N)r   )r2   r0   s    r/   	decoratorz%register_annotator.<locals>.decoratorJ   s    'r.   )AnnotatorType)r0   r5   s   ` r/   register_annotatorr7   I   s    (] ( r.   c                   (    e Zd ZU eed<   ee   ed<   y)r   config	operatorsN)r(   r)   r*   r   r+   r   r   r-   r.   r/   r   r   P   s     '((r.   r   nodesc                 r    d}| D ]/  }|xs) d|j                   v xr |j                   d   j                  }1 |S )z
    Given a list of nodes (that represents an operator pattern),
    check if any of the node is annotated, return True if any of the node
    is annotated, otherwise return False
    Fquantization_annotation)meta
_annotated)r;   	annotatednodes      r/   _is_annotatedrB   ]   sM     I 
%2 @		34?? 	 
 r.   c                     | D ]>  }|d|j                   vrt               |j                   d<   d|j                   d   _        @ y )Nr=   T)r>   r   r?   )r;   rA   s     r/   _mark_nodes_as_annotatedrD   l   sB    (		97M7O		34>BDII/0;	 r.   quantization_configc                     | y | j                   y | j                   }|j                  t        j                  t        j                  fv sJ |S r4   )r#   qschemetorchper_tensor_affineper_tensor_symmetricrE   quantization_specs     r/   r   r   t   s]    "++3*=*N*N$$"")    r.   c                     | y | j                   y | j                   }|j                  t        j                  t        j                  fv sJ |S r4   )r$   rG   rH   rI   rJ   rK   s     r/   r   r      s]    ",,4*=*O*O$$"")    r.   c                     | y | J | j                   y | j                   }|j                  t        j                  t        j                  fvrt        d| d      |S )NzUnsupported quantization_spec z for weight)r%   rG   rH   rJ   per_channel_symmetric
ValueErrorrK   s     r/   r   r      s|    "***!!)*=*D*D  ""##)  ,->,?{K
 	
 r.   c                     | y | J | j                   y | j                   }|j                  t        j                  k(  sJ d       |S )Nz9Only float dtype for bias is supported for bias right now)r&   dtyperH   floatrK   s     r/   r   r      s\    "***'*=*B*B5;;.CBC.r.   lineargm	filter_fnreturnc                    g }t        |      }t        |      }t        |      }t        |      }| j                  j
                  D ]  }|j                  dk7  s;|j                  t        j                  j                  j                  j                  k7  rO|r	 ||      sZ|j                  d   }	|j                  d   }
d }t        |j                        dkD  r|j                  d   }t        |g      du st!        ||	|       t!        ||
|       ||
g}|rt!        |||       |j#                  |       t%        ||       t'        |       |j#                  |        |S )Ncall_functionr         F)r   r   r   r   graphr;   r0   targetrH   opsatenrT   defaultargslenrB   r   appendr   rD   )rU   rE   rV   annotated_partitionsinput_act_qspecoutput_act_qspecweight_qspec
bias_qspecrA   act_nodeweight_node	bias_nodenodes_to_mark_annotateds                r/   _annotate_linearrm      sP    )*=>O+,?@#$78L 34J77o%		8M8M8U8U)UYt_99Q<iil	tyy>A		!I$ E)%
 &
 (,[&9#)
 (..y9"4)9:$%<= ''(?@A D  r.   convc                    g }| j                   j                  D ]  }|j                  dk7  sh|j                  t        j
                  j                  j                  j                  t        j
                  j                  j                  j                  fvr||}i }|j                  d   }t        |t              sJ t        |      ||<   |j                  d   }t        |t              sJ t        |      ||<   ||j                  d   g}	t        |j                        dkD  r|j                  d   nd }
t        |
t              rt!        |      ||
<   |	j#                  |
       t%        |	      rTrt'        fd|	D              rlt)        |t+        |      d      |j,                  d<   t/        |	       |j#                  |	        |S )	NrY   r   rZ   r[   c              3   0   K   | ]  } |         y wr4   r-   .0nrV   s     r/   	<genexpr>z!_annotate_conv.<locals>.<genexpr>        Ay!1-y   Tinput_qspec_mapoutput_qspecr?   r=   )r\   r;   r0   r]   rH   r^   r_   conv1dr`   conv2dra   
isinstancer   r   r   rb   r   rc   rB   anyr   r   r>   rD   )rU   rE   rV   rd   rs   	conv_noderx   	input_actr%   	partitionr&   s     `        r/   _annotate_convr      s    XX^^44?"ahhIINN!!))IINN!!))7
 '
 	NN1%	)T***%89L%M	""&$'''"23F"G 	q 12	$'	$7!$;y~~a dD!$23F$GOD!T"#AyAA4J+-.AB5
	01
 	!+##I.K L  r.   	conv_reluc                    g }| j                   j                  D ]_  }|j                  dk7  sh|j                  t        j
                  j                  j                  j                  t        j
                  j                  j                  j                  fvr||}|j                  d   }t        |t              rw|j                  dk7  sh|j                  t        j
                  j                  j                  j                  t        j
                  j                  j                  j                  fvr|}i }|j                  d   }	t        |	t              sJ t        |      ||	<   |j                  d   }
t        |
t              sJ t!        |      ||
<   |||j                  d   g}t#        |j                        dkD  r|j                  d   nd }t        |t              rt%        |      ||<   |j'                  |       t)        |      rrt+        fd|D              rt-        |d      |j.                  d<   t-        t1        |      d	      |j.                  d<   t3        |       |j'                  |       b |S )
NrY   r   rZ   r[   c              3   0   K   | ]  } |         y wr4   r-   rq   s     r/   rt   z&_annotate_conv_relu.<locals>.<genexpr>:  ru   rv   Trx   r?   r=   ry   r?   )r\   r;   r0   r]   rH   r^   r_   relur`   relu_ra   r|   r   rz   r{   r   r   rb   r   rc   rB   r}   r   r>   r   rD   )rU   rE   rV   rd   rs   	relu_nodemaybe_conv_noder~   rx   r   r%   r   r&   s     `          r/   _annotate_conv_relur     s    XX^^44?"ahhIINN''IINN  ((7
 '
 	&&)?D1!!_4%%		%%--		%%-- #	NN1%	)T***%89L%M	""&$'''"23F"G 	9>>!+<=	$'	$7!$;y~~a dD!$23F$GOD!T"#AyAA4J+5
	01 5K-.AB5
	01 	!+##I.e f  r.   conv_bnc                      t        | ||d      S )z
    Find conv + batchnorm parititions
    Note: This is only used for QAT. In PTQ, batchnorm should already be fused into the conv.
    Fhas_relu_do_annotate_conv_bnrU   rE   rV   s      r/   _annotate_conv_bnr   I  s      $7USSr.   conv_bn_reluc                      t        | ||d      S )z
    Find conv + batchnorm + relu parititions
    Note: This is only used for QAT. In PTQ, batchnorm should already be fused into the conv.
    Tr   r   r   s      r/   _annotate_conv_bn_relur   V  s      $7TRRr.   r   c                 *   dt         dt        ffd}| j                  j                          | j	                          g }t
        j                  t        ft
        j                  t        fg}t        j                  |t        j                  j                         rddgndgrddgndg      }|D ]  \  \  }}}	}
 |||
      }t        |||	      }|j                  j                          |j	                          t!        |d      }|j#                  |j%                  | j                                g }|D ]X  }|j&                  }|d   }|d   }|d	   }|d
   }|d   }|j(                  d   |urt+        d|      |j(                  d   |urt+        d|      t-        |j(                        dkD  r|j(                  d   |urt+        d|      ||g}||j/                  |       t1        |      rrt3        fd|D              ri }t5        |      ||<   t7        |      ||<   |t9        |      ||<   t;        |d      |j<                  d<   t;        t?        |      d      |j<                  d<   tA        |       |j/                  |       [ |S )a%  
    Given a function that takes in a `conv_fn` and returns a conv-bn[-relu] pattern,
    return a list of annotated partitions.

    The output of the pattern must include a dictionary from string name to node
    for the following names: "input", "conv", "weight", "bias", and "output".
    conv_fnrelu_is_inplacec                       fd}|S )Nc                      
| ||      }t        j                  |||||d      }r-rt        j                  |      nt        j                  |      }	n|}	|	| ||||	dfS )NT)training)inputrn   r%   r&   output)F
batch_normr   r   )xconv_weight	conv_bias	bn_weightbn_biasbn_rmbn_rvrn   bnr   r   r   r   s             r/   _conv_bnz;_do_annotate_conv_bn.<locals>.get_pattern.<locals>._conv_bnr  sk    1k95DdE5)WtTB(7QVVBZ%!   r.   r-   )r   r   r   r   s   `` r/   get_patternz)_do_annotate_conv_bn.<locals>.get_patternq  s    	 r.   TF)ignore_literalsr   rn   r%   r&   r   r   z$Conv arg did not contain input node rZ   z%Conv arg did not contain weight node r[   z#Conv arg did not contain bias node c              3   0   K   | ]  } |         y wr4   r-   rq   s     r/   rt   z'_do_annotate_conv_bn.<locals>.<genexpr>  ru   rv   r   r=   r   )!r   r,   r\   eliminate_dead_code	recompiler   rz   r   r{   r   	itertoolsproductrH   cudais_availabler   r   extendmatchname_node_mapra   rP   rb   rc   rB   r}   r   r   r   r   r>   r   rD   )rU   rE   rV   r   r   matchescombinationsr   example_inputsis_cudar   patternmatcherrd   r   r   
input_noder~   rj   rk   output_noder   rx   s     ``                   r/   r   r   c  s   X  ( HH  "LLNG	
,-	
,-L $$002u!uwL @L;!.7Og7'I))+0$Ow}}RXX./ @L ++"7+
!&)	#H-!&)	#H- >>!J.CZPP>>!K/DkRRy~~"y~~a'8	'IBINN ,	 Y'#AyAA &9:M&N
#'78K'L$ )78K)LOI&4J+5
	01 7M-.AB7
23 	!+##I.] ^  r.   gru_io_onlyc                    t        | j                  t        j                  j                  g|      }t        t        j                  |j                                }g }|D ]  }|j                  |j                         |j                  }|j                  }t        ||z         rFi }|d   }	t        t        |	j                   j#                                     }
t%        |	t&              sJ t%        |
t&              sJ t)        |	t+        |      id      |
j,                  d<   |d   }t        t        |j                   j#                                     }t%        |t&              sJ t%        |t&              sJ t)        |t+        |      id      |j,                  d<   t/        |      dk(  sJ d       |D ]%  }t)        t1        |      d      |j,                  d<   ' t        |j                        }t3        |        |S )	Nr   Tr   r=   rZ   r[   z!expecting GRU to have two outputsr   )r   r\   rH   nnGRUlistr   chainvaluesrc   r;   output_nodesinput_nodesrB   nextiteruserskeysr|   r   r   r   r>   rb   r   rD   )rU   rE   rV   gru_partitionsrd   gru_partitionr   r   rx   r   input_act_userhidden_statehidden_state_userr   rl   s                  r/   _annotate_gru_io_onlyr     s    +288ehhll^YON)//>+@+@+BCDN'##M$7$78$11#//|34 =?N	d9??#7#7#9:;)T***.$///9O./BC 	:
56 #1~ l&8&8&=&=&?!@A,---+T222<R12EF 	=
89 < A%J'JJ%"F5K12EF6FKK12 #
 #'}':':"; !89O (P  r.   
max_pool2dc                    t        | j                  t        j                  j                  t        j                  j
                  j                  g|      }t        t        j                  |j                                }g }|D ]9  }|j                  |j                         |j                  d   }d }|j                  D ]@  }	|	j                  t        j                  j                   j                  j"                  k(  s?|	}B |J d       	 t%        ||g      r|j&                  d   }
t)        |
t*              sJ d|
j,                  vs2|
j,                  d   j.                  r|
j,                  d   j0                  t3        |
      }t5        |
|id      |j,                  d<   t5        |d      |j,                  d<   < |S )Nr   zDXNNPACKQuantizer only works with torch.ops.aten.max_pool2d.default, r=   Tr   r   )r   r\   rH   r   	MaxPool2d
functionalr   r   r   r   r   rc   r;   r   r]   r^   r_   r`   rB   ra   r|   r   r>   r?   ry   r   r   )rU   rE   rV   module_partitionsmaxpool_partitionsrd   maxpool_partitionr   maxpool_noders   r   	act_qspecs               r/   _annotate_max_pool2dr     s    .
588%%uxx':':'E'EF	 ioo/@/G/G/IJK/##$5$;$;<'44Q7"((Axx599>>44<<<  ) $	RQ	R$@+|45 %%a(	)T*** &Y^^;>>";<GG~~78EEM*95	7M9 	8
34 7M"7
23C 0J  r.   adaptive_avg_pool2dc                 N   t        | j                  t        j                  j                  t
        j                  g|      }t        t        j                  |j                                }g }|D ]4  }|j                  d   }|j                  dk7  s;|j                  t        j                  j                  j                  j                   k7  rt#        | d      t%        |g      rx|j'                  |j(                         |j*                  d   }t-        |t.              sJ d|j0                  vs2|j0                  d   j2                  r|j0                  d   j4                  t7        |      }	nt9        |      }	t9        ||f      }
t;        ||	i|
d      |j0                  d<   7 |S )z&Always annotate adaptive_avg_pool2d opr   rY   z, is not an aten adaptive_avg_pool2d operatorr=   Trw   )r   r\   rH   r   AdaptiveAvgPool2dr   r   r   r   r   r   r   r0   r]   r^   r_   r`   rP   rB   rc   r;   ra   r|   r   r>   r?   ry   r   r   r   )rU   rE   rV   r   
partitionsrd   r   	pool_noder   re   rf   s              r/   _annotate_adaptive_avg_pool2dr   9  sy    .
588--q/D/DEy ioo'8'?'?'ABCJ	**1-	LLO+599>>#E#E#M#MM	{*VWXX)%##IOO4NN1%	)T***
 &Y^^;>>";<GG~~78EEM12EFO4Y?O 29i2HI4J? *5
	019  F  r.   rA   c                     | j                   dk(  rIt        || j                        }d}|j                         dk(  xr t	        |j                               |kD  S y)zCheck if input is a large scalar value. So that we can skip quantization for the node
    since histc op (in HistogramObserver) only works for values up to certain upper bound
    get_attrg +-(CrZ   F)r0   getattrr]   numelabsitem)rA   rU   tensorHISTC_UPPER_BOUNDs       r/   _is_input_large_scalarr   k  sP     ww*T[[)(||~"Ms6;;='9<M'MMr.   add_reluc                 "   t        | t        j                  t        j                  j                  g|      }g }|D ]Q  }|\  }}|j                  |j                  |j                  z          t        |j                        dkD  rt        d      |j                  d   }t        |j                        dkD  rt        d      |j                  d   }	t        ||	g      rt        |      }
t        |      }i }|	j                  d   }t        |t              rt!        ||       r|
||<   |	j                  d   }t        |t              rt!        ||       r|
||<   t#        |d      |	j$                  d<   t#        |d      |j$                  d<   T |S )	NrZ   ,Relu partition has more than one output noder   z+add partition has more than one output nodeTr   r=   r   )r
   rH   addr   ReLUrc   r;   rb   r   rP   rB   r   r   ra   r|   r   r   r   r>   )rU   rE   rV   fused_partitionsrd   fused_partitionadd_partitionrelu_partitionr   add_nodere   rf   rx   
input_act0
input_act1s                  r/   _annotate_add_relur   w      2
UYY&	 +(7%~##M$7$7.:N:N$NO~**+a/KLL"//2	}))*Q.JKK --a0)X./-.AB/0CD]]1%
j$'%j"5*9OJ']]1%
j$'%j"5*9OJ'3I+4
/0 5K)5
	01C ,J  r.   r   c                    t        | j                  t        j                  t        j                  t        j
                  g|      }t        t        j                  |j                                }g }|D ]  }|j                  |j                         |j                  d   }t        |g      r:t        |      }t        |      }i }	|j                   d   }
t#        |
t$              rt'        |
|       r~||	|
<   |j                   d   }t#        |t$              rt'        ||       r||	|<   t)        |	|d      |j*                  d<    |S )Nr   rZ   Trw   r=   )r   r\   operatorr   rH   iaddr   r   r   r   rc   r;   r   rB   r   r   ra   r|   r   r   r   r>   )rU   rE   rV   add_partitionsrd   r   r   re   rf   rx   r   r   s               r/   _annotate_addr     s*    +
8<<HMM:IN )//>+@+@+BCDN'##M$7$78 --a0($-.AB/0CD]]1%
j$'%j"5*9OJ']]1%
j$'%j"5*9OJ'3I+)4
/0- (6  r.   mul_reluc                 "   t        | t        j                  t        j                  j                  g|      }g }|D ]Q  }|\  }}|j                  |j                  |j                  z          t        |j                        dkD  rt        d      |j                  d   }t        |j                        dkD  rt        d      |j                  d   }	t        ||	g      rt        |      }
t        |      }i }|	j                  d   }t        |t              rt!        ||       r|
||<   |	j                  d   }t        |t              rt!        ||       r|
||<   t#        |d      |	j$                  d<   t#        |d      |j$                  d<   T |S )	NrZ   r   r   z+mul partition has more than one output nodeTr   r=   r   )r
   rH   mulr   r   rc   r;   rb   r   rP   rB   r   r   ra   r|   r   r   r   r>   )rU   rE   rV   r   rd   r   mul_partitionr   r   mul_nodere   rf   rx   r   r   s                  r/   _annotate_mul_relur    r   r.   r   c                    t        | j                  ddt        j                  t        j                  t        j
                  g|      }t        t        j                  |j                                }g }|D ]  }|j                  |j                         |j                  d   }t        |g      r:t        |      }t        |      }i }	|j                   d   }
t#        |
t$              rt'        |
|       r~||	|
<   |j                   d   }t#        |t$              rt'        ||       r||	|<   t)        |	|d      |j*                  d<    |S )Nr   mul_r   rZ   Trw   r=   )r   r\   r   r   rH   imulr   r   r   r   rc   r;   r   rB   r   r   ra   r|   r   r   r   r>   )rU   rE   rV   mul_partitionsrd   r   r  re   rf   rx   r   r   s               r/   _annotate_mulr    s.    +
5&(,,		8==I9N )//>+@+@+BCDN'##M$7$78 --a0($-.AB/0CD]]1%
j$'%j"5*9OJ']]1%
j$'%j"5*9OJ'3I+)4
/0- (6  r.   catc                    t        | j                  t        j                  g|      }t	        t        j                  |j                                }g }|D ]  }|j                  d   }t        |g      r|j                  t        j                  j                  j                  j                  k7  rt        d|j                   d      |j                  |j                          t#        |      }|j$                  d   }i }	|d   }
t'        |
t(              r||	|
<   t+        |
|f      }|dd  D ]  }||	|<   	 |}t-        |	|d      |j.                  d<    |S )Nr   z9Expected cat node: torch.ops.aten.cat.default, but found z8 please check if you are calling the correct capture APIrZ   Trw   r=   )r   r\   rH   r  r   r   r   r   r   rB   r]   r^   r_   r`   	Exceptionrc   r;   r   ra   r|   r   r   r   r>   )rU   rE   rV   cat_partitionsrd   cat_partitioncat_nodere   inputsrx   r   shared_with_input0_qspecr   rf   s                 r/   _annotate_catr  .  sP    +288eii[)LN)//>+@+@+BCDN' --a0($??eiinn00888KHOOK\JK 
 	##M$7$78-.ABq!AY
j$'*9OJ'#9:x:P#Q I)AOI& $ 43I+)4
/09 (B  r.   c                    | t         j                  j                  j                  j                  t         j                  j                  j
                  j                  t         j                  j                  j                  j                  t         j                  j                  j                  j                  t         j                  j                  j                  j                  t         j                  j                  j                  j                  t         j                  j                  j                  j                  t         j                  j                  j                  j                  t         j                  j                  j                  j                  t         j                  j                  j                  j                  t         j                  j                  j                  j                  t         j                  j                  j                  j                   t         j                  j                  j"                  j$                  fv S r4   )rH   r^   r_   hardtanhr`   	hardtanh_meandimpermutepermute_copysqueezesqueeze_copyr   	view_copyview
slice_copyTensorflatten
using_ints)r0   s    r/   _is_share_obs_or_fq_opr   [  s?   		''		  ((		##				&&		##++		""		##''		**22		  ((		##		!!((		))  r.   modelc                    | j                   j                  D ]  }|j                  dk7  st        |j                        s(|j
                  d   }t        |t              sH|j                  j                  dd       }|sg|j                  }|svd|j                  v r|j                  d   j                  rt        |      }t        ||i|d      |j                  d<    y )NrY   r   r=   Trw   )r\   r;   r0   r   r]   ra   r|   r   r>   getry   r?   r   r   )r!  rs   	prev_noder=   ry   shared_qspecs         r/   r   r   n  s    [[44?"*@*JFF1I	)T*"+.."4"45NPT"U&.;; &/01<<-i8,B< &-
()3 r.   c                    | j                   j                  D ]  }|j                  dk7  sh|j                  t        j
                  j                  j                  j                  t        j
                  j                  j                  j                  fvr|t        |j                        }g }t        t        |            D ]  }t        ||   t        j                  j                         r|j#                  ||          ?d}t%        |      } ||       }| j'                  |t	        j(                  t+        ||                      | j                   j-                  |      5  | j                   j/                  d|di       }|j#                  |       d d d         t1        |      |_         | j3                          | S # 1 sw Y   	xY w)NrY   _tensor_constant_r   r-   )r\   r;   r0   r]   rH   r^   r_   r   r  r   r   ra   rangerb   r|   fxr   rc   r	   register_bufferr   rS   inserting_beforecreate_nodetupler   )	r!  rs   ra   new_argsiprefixget_new_attr_nametensor_constant_nameget_attr_nodes	            r/   _convert_scalars_to_attrsr4    s^   [[44?"ahhIINN%%IINN%%7
 '
 AFF|s4y!A$q'588==1Q((F =f E#4U#; !!"6U4PQ7^8TU--a0 % 7 7 4b"! .	 10 " x+ , 
OOL 10s   0F==Gr4   )Jr   r   dataclassesr   typingr   r   r   r   r   rH   torch.nn.functionalr   r   r   torch.ao.quantization.fx.utilsr	   &torch.ao.quantization.pt2e.graph_utilsr
    torch.ao.quantization.pt2e.utilsr   r   r   torch.ao.quantization.quantizerr   r   r   r   %torch.ao.quantization.quantizer.utilsr   r   torch.fxr   6torch.fx.passes.utils.matcher_with_name_node_map_utilsr   *torch.fx.passes.utils.source_matcher_utilsr   __all__r   r   r)   r)  GraphModuler,   r6   r   strr+   r7   r   rB   rD   r   r   r   r   rm   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r   r   r4  r-   r.   r/   <module>rC     s     ! = =    H M 
   M
 d4   ! 8n =   #$4&$,'(
 T$t*  -/c=() .3 
)Z 
)d CDJ C
X6H-I 

h7I.J 
(3E*F "
1C(D 
 H 37, , !"45,  $./,  d4:	,  , ^ F 37, , !"45,  $./,  d4:	,  , ^ K  379 9 !"459  $./9  d4:	9  !9 x I 37	T	T!"45	T $./	T d4:		T 	T N# 37	S	S!"45	S $./	S d4:		S $	Sl l !"45l  $./l  	l 
 
$t*l ^ M" 370 0 !"450  $./0  d4:	0  #0 f L! 37/ / !"45/  $.//  d4:	/  "/ d )* 37. . !"45.  $./.  d4:	.  +. b	 	588+?+? 	 J 37. . !"45.  $./.  d4:	.   . b E 37% % !"45%  $./%  d4:	%  % P J 37. . !"45.  $./.  d4:	.   . b E 37% % !"45%  $./%  d4:	%  % R E 37) ) !"45)  $./)  d4:	)  ) Xx D & 
 4 4  
  
HUXX%9%9 ehh>R>R r.   