
    i8                     .   S SK JrJrJrJr  S SKJr  S SKrS SKJ	r	J
r
Jr  S SKJr  S SKJr  S SKJrJrJrJrJrJr  S SKJrJr    S&S\R4                  R6                  S	\\R8                     S
\\   S\\   S\R<                  4
S jjr/ SQr / SQr!S\S\"SS4S jr#S\S\4S jr$S\RJ                  RL                  S\4S jr'S\RJ                  RL                  S\(S\RJ                  RL                  4S jr)S\RJ                  RL                  S\\RJ                  RL                     4S jr*S\RJ                  RL                  S\4S jr+S\S\RJ                  RL                  S\4S jr,S\S\RJ                  RL                  S\\R8                     4S jr-S\S\RJ                  RL                  S\"4S jr.S\RJ                  RL                  S\\RJ                  RL                     4S jr/S\RJ                  RL                  S\(4S  jr0 S'S!\\(S"4   S#\(S$\S\4S% jjr1g)(    )AnycastOptionalTupleN)get_transform_passesget_xnnpack_capture_configget_xnnpack_edge_compile_config)ExportedProgram)ops)
get_bufferget_lifted_tensor_constant	get_param	is_bufferis_lifted_tensor_constantis_param)_is_conv_node_is_conv_transpose_nodemoduleinputs
enable_aotunliftreturnc           	          [         R                  " U U[        X#S95      R                  [	        5       5      R
                  " [        5       6 $ )N)r   r   )exircapturer   to_edger	   	transformr   )r   r   r   r   s       f/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/backends/xnnpack/utils/utils.pycapture_graph_for_xnnpackr       sJ     	&*L	

 
02	3		 )*	,    )r            )r   r"   r#   r!   	conditionerrc                 (    U (       d  [        U5      eg)z
Raises runtime error if condition is false, with the given error message

Args:
    condition: boolean condition to check
    err: error message to raise if condition is not true
N)RuntimeError)r$   r%   s     r   check_or_raiser(   6   s     3 r    nodec                 J    [        U [        R                  R                  5      $ )z:
returns true if node is a torch.fx.Node, otherwise false
)
isinstancetorchfxNoder)   s    r   is_noder0   B   s     dEHHMM**r    c                 V    U R                   S:w  a  gU R                  R                  S:H  $ )Ncall_functionFgetitem)optarget__name__r/   s    r   
is_getitemr7   I   s&    ww/!;;9,,r    input_indexc                 d    [        [        R                  R                  U R                  U   5      $ N)r   r,   r-   r.   args)r)   r8   s     r   get_input_noder<   P   s     tyy566r    c                 F   [        U R                  5      S:X  a  [        U R                  R                  5       5      S   R                  [
        R                  R                  R                  R                  :X  a(  [        U R                  R                  5       5      S   nU$ g)z
Checks if the current node is only consumed by a relu node and can be fused,
if so, we return the relu node that can be fused, otherwise return None
r#   r   N)
lenuserslistkeysr5   exir_opsedgeatenreludefault)r)   	relu_nodes     r   get_relu_fused_noderH   T   sp     	DJJ1"#A&--1C1C1H1H1P1PP*+A.	r    c                 v    [        U [        R                  R                  5      =(       a    U R                  S:H  $ )zM
Returns true if the given node is a get attr node for a tensor of the model
get_attr)r+   r,   r-   r.   r4   r/   s    r   is_get_attr_noderK   c   s'     dEHHMM*Dtww*/DDr    exp_progc                     [        U5      =(       d/    [        X5      =(       d    [        X5      =(       d    [        X5      $ r:   )rK   r   r   r   rL   r)   s     r   is_param_noderO   j   s8     	5H#	5X$	5 %X4	r    c                    Uc  g [        X5      (       a  [        X5      $ [        X5      (       a  [        X5      $ [	        X5      (       a  [        X5      $ [        U5      (       a+   [        UR                  R                  UR                  5      $ [        SUR                   S35      e! [         a#    [        U R                  UR                  5      s $ f = f)Nzunsupported param type, .)r   r   r   r   r   r   rK   getattrgraphowning_moduler5   AttributeErrorgraph_moduler'   r4   rN   s     r   get_param_tensorrW   s   s     |	(	!	!((	8	"	"())	"8	2	2)(99	$			?4::33T[[AA 1$''!<
==  	?800$++>>	?s   ')B* **CCc                    Uc  g[        X5      (       a#  U R                  R                  UR                     $ [	        X5      (       a#  U R                  R
                  UR                     $ [        X5      (       a#  U R                  R                  UR                     $ [        UR                  [        5      (       d   eUR                  $ )N )r   graph_signatureinputs_to_parametersnamer   inputs_to_buffersr   !inputs_to_lifted_tensor_constantsr+   r5   strrN   s     r   get_tensor_namer`      s    |''<<TYYGG	8	"	"''99$))DD	"8	2	2''II$))TT$++s++++{{r    c                 v    U R                   S:w  d  U R                  R                  SS5      =nc  gUS   nUS   $ )zN
Returns the source fn of the given node, return None if something goes wrong
r2   source_fn_stackNr#   )r4   metaget)r)   source_fn_st	source_fns      r   get_source_fnrh      sB    
 	?" IIMM*;TBBLKR IQ<r    	conv_nodec                 v   [        U 5      (       a  [        [        R                  R                  U R
                  S   5      n[        [        R                  R                  U R
                  S   5      nUR                  S   R                  S   nUR                  S   R                  S   nX4-  $ [        U 5      (       am  [        [        R                  R                  U R
                  S   5      nUR                  S   R                  S   nU R                  S   R                  S   nXe-  $ [        SU  S35      e)Nr   r#   valz	expected z$ to be a conv or conv_transpose node)
r   r   r,   r-   r.   r;   rd   shaper   r'   )ri   in_nodeweight_nodein_channels	in_groups
out_groupsout_channelss          r   get_groups_from_convrs      s   Yuxx}}innQ&78588==)..*;<
 ll5)//2  $$U+11!4	''	 	+	+588==)..*;<
 !%%e,2215
 !~~e,2215))
9+-QR
SSr    kernel_shape.groupsis_transposec                     [        U 5      S:  d  US:  a  gU(       a  [        U S   U-  5      nU S   nOU S   n[        U S   U-  5      nUS:H  =(       a    XC-  S:H  $ )a  
A convolution is depthwise if:
    1) groups = input_channels (i.e. group_input_channels = 1)
    2) output_channels is a positive integer multiple of input channels

For standard convolutions:
    weight shape = (out_channels, in_channels_per_group, height, width)
For transposed convolutions:
    weight shape = (in_channels, out_channels_per_group, height, width)

Returns True if the convolution is depthwise
r!   r#   Fr   )r>   int)rt   ru   rv   group_input_channelsgroup_output_channelss        r   is_depthwise_convr{      sw     <1
"<?V#;< ,Q+A #LOf$< = 	!W&;&RVW&Wr    )NN)r#   F)2typingr   r   r   r   executorch.exirr   r,   )executorch.backends.xnnpack.utils.configsr   r   r	   r
   executorch.exir.dialects._opsr   rB   torch._export.utilsr   r   r   r   r   r   torchao.quantization.pt2e.utilsr   r   nnModuleTensorboolExirExportedProgramr   PERM_NCHW_TO_NHWCPERM_NHWC_TO_NCHWr_   r(   r0   r-   r.   r7   rx   r<   rH   rK   rO   rW   r`   rh   rs   r{    r    r   <module>r      sA   . -   
 , 9  S "&!	HHOO%,,  TN	
 
$ !   	 d 	  	  	 +# +$ +-UXX]] -t -7 7S 7UXX]] 7ehhmm 0G E588== ET EO 588== T >>%*XX]]>ell>(o UXX]] s  
 
(588==*A 
TEHHMM Tc T> JOS/+.BF	r    