
    i                     L   d dl 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 d dlmZmZ d dl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lmZmZ d	dlmZ ddgZ	 	 ddede dee!e"ef   z  dz  dee!e"ef   z  dz  def
dZ#dedede!eef   de!e"e$eeeee!eef   f   f   fdZ%y)    N)Callable)Any)BackendConfigget_native_backend_config)get_fuser_method_mapping)get_fusion_pattern_to_extra_inputs_getter&get_fusion_pattern_to_root_node_getter)NodePatternPattern)GraphModulemap_argNode)Graph   )FuseCustomConfig)'_get_fusion_pattern_to_fuse_handler_clsFuseHandler)	_is_matchMatchAllNode)_sorted_patterns_dictfuser   modelis_qatfuse_custom_configbackend_configreturnc                 "   |
t               }t        |t              r1t        j                  dt
        d       t        j                  |      }t        |t              r1t        j                  dt
        d       t        j                  |      }t        | j                               }|
t               }t        t        |            }t        |      }t        |      }t        |      }t        | | j                   |      }	t#               }
i fd}d }| j                   j$                  D ]  }|	j'                  |j(                  d      \  }}}}}||j'                  |d       }nd }||u rl|t+        d      |j'                  ||      } ||      }|j'                  |d       }g }| ||      }|j-                  |||
||||||	      |j(                  <   |	|t.        u s|
j1                  ||      |j(                  <    t3        | |
      } | S )	NzPassing a fuse_custom_config_dict to fuse is deprecated and will not be supported in a future version. Please pass in a FuseCustomConfig instead.   )
stacklevelzPassing a backend_config_dict to prepare is deprecated and will not be supported in a future version. Please pass in a BackendConfig instead.c                 "    t        | fd      S )Nc                 "    | j                      S N)name)nodeenvs    W/var/www/html/engine/venv/lib/python3.12/site-packages/torch/ao/quantization/fx/fuse.py<lambda>z(fuse.<locals>.load_arg.<locals>.<lambda>T   s    s499~     )r   )ar%   s    r&   load_argzfuse.<locals>.load_argS   s    q566r(   c                 d    t        | d   t              s| d   } t        | d   t              s| d   S )N)
isinstancer   )node_patterns    r&   default_root_node_getterz&fuse.<locals>.default_root_node_getterV   s7    \"-t4'+L \"-t4Br(   )NNNNNz:fuse handler object must not be None for matched root node)r   r-   dictwarningswarnFutureWarning	from_dictr   named_modulesr   r   r   r   r	   r   _find_matchesgraphr   nodesgetr#   AssertionErrorr   r   	node_copyr   )r   r   r   r   r5   "fusion_pattern_to_fuse_handler_clsfuser_method_mapping"fusion_pattern_to_root_node_getter%fusion_pattern_to_extra_inputs_getterfusion_pairsfused_graphr*   r/   r$   maybe_last_nodepatternmatched_node_patternobjnode_to_subpatternnode_subpatternroot_node_getter	root_nodeextra_inputs_getterextra_inputsr%   s                           @r&   r   r   !   sV    !-/$d+N		
 .778JK.$'K		
 '00@,,./M24)>/?*& 4NC)O*& -V-)
 !5WXL 'KC7 
 !! *C TYY(FG	
  )044T4@O"Od"{$P   BEE1  ))=>I"G"K"K# L".23GH !XX$"$
C		N $<(G(224BC		NU*CZ {+ELr(   rootr7   pattern_to_fuse_handler_clsc                   	
 t        | j                               }i 
i }	
fd	t        |j                        D ]O  }|j                  
vs|j                         D ]+  \  }}g }t        |||      s 	|||| ||      f||        O Q 
S )Nc                 d   t        | t              rW| ^}}g } |||||       t        ||j                        D ]  \  }}	 ||	|||        |j	                  t        |             y |j
                  vr:|j	                  |       | t        ur | ||<   |\  }
} }|
| |||f|j
                  <   y y y r"   )r-   tuplezipargsappendr#   r   )rC   r$   matchrD   rF   srR   current_node_pattern
subpatternargrI   handlerapply_match	match_maps               r&   rZ   z"_find_matches.<locals>.apply_match   s    gu%HA/1 4(<>PQ#&tTYY#7 
CU,@BT !''.B(CD yy	)$++D1 ,./6&t,27/Iw!,*,Idii( /	 *r(   )r0   r5   reversedr8   r#   itemsr   )rL   r7   rM   modulesrF   r$   rC   fuse_handler_clsrD   rZ   r[   s            @@r&   r6   r6      s    
 4%%'(G 	  +-6 % 99I%-H-N-N-P 
))35$WdG4w(8(>?,* 
 r(   )NN)&r1   collections.abcr   typingr   $torch.ao.quantization.backend_configr   r   *torch.ao.quantization.backend_config.utilsr   r   r	   torch.ao.quantization.utilsr
   r   torch.fxr   r   r   torch.fx.graphr   custom_configr   fuse_handlerr   r   match_utilsr   r   pattern_utilsr   __all__boolr0   strr   rP   r6    r(   r&   <module>ro      s    $  
 = / /   + N 0 0  	 DH<@	hhh )4S>9D@h "DcN2T9	h
 hV7
77 "&gx&7!87 
#uT7Kd49oMN
NO	7r(   