
    IЦi                        S SK r S SKrS SKJrJrJr  S SKrS SKJr  S SK	J
r
  S SKJr  S\R                  S\\R                     S\\R                  S	4   4S
 jrSS\
S\
S\4S jjrS\\
   4S jrS\R$                  R&                  4S jrg)    N)DictListTuple)symbolic_trace)Node)legalize_graphresultinputsreturn.c                     [        U [        R                  R                  5      (       a  S/[	        U5      -  nOU Vs/ s H  o3R
                  S   PM     nn[        R                  " X5      $ s  snf )ae  
A free function for use in the merge_matmul graph transformation below that
splits the output from a merged matmul into the individual results for each
input tensor.

Arguments:
    result: The merged matmul result tensor.
    inputs: The list of inputs that were merged into one for the matmul.

Returns:
    List of matmul results for each input tensor.
r   )
isinstancetorchfxProxylenshapesplit)r	   r
   splitsxs       a/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/fx/experimental/merge_matmul.pysplit_result_tensorsr      sW    " &%((..))s6{"&,-f''!*f-;;v&& .s   A.absearch_depthc                     X:X  a  g[        U R                  5      S:X  a  gUS:X  a  gU R                   H  n[        X1US-
  5      (       d  M    g   g)a.  
Determine if one node depends on another in a torch.fx.Graph.

Arguments:
    a: The node that may have a dependency on b.
    b: The node that a may have a dependency on.
    search_depth: In the case of an indirect dependency, this function
                    searches upto this many nodes away in search of a
                    data dependency. If none is found, the function
                    makes the conservative assumption that there is a
                    dependency.

Returns:
    True if a may depend on b, False if it definitely does not.
Tr   F   )r   all_input_nodesmay_depend_on)r   r   r   inps       r   r   r   %   s]    " 	v 1" q   !122 !     nodesc                     [         R                  " U S5       H'  u  p[        X5      (       d  [        X!5      (       d  M'    g   g)z
Check if all of the given nodes are pairwise-data independent.

Arguments:
    nodes: The nodes to check for data dependencies.

Returns:
    True if any pair in nodes has a data dependency.
   FT)	itertoolscombinationsr   )r!   ijs      r   are_nodes_independentr(   J   s:     &&ua0-"5"5 1 r    in_modc           	         [        U 5      n0 n0 nUR                  R                   H  nUR                  S:w  d  UR                  [
        R                  La  M2  UR                  u  pVUR                  S:X  a  UR                  OUnUR                  S:X  a  UR                  OUnUR                  U/ 5      R                  U5        UR                  U/ 5      R                  U5        M     UR                  5        GH  u  pg[        U5      S:  a  M  [        U5      (       d  M)  U Vs/ s H  oR                  S   PM     n	nU	 V
s/ s H5  n
[        U
[        5      (       a  UR                  R                  U
5      OU
PM7     nn
[        U[        5      (       a  UR                  R                  U5      OUnUR                  R!                  [
        R"                  U40 5      nUR                  R!                  [
        R                  UU40 5      nUR                  R!                  [$        X40 5      n['        [        U5      5       Vs/ s H/  nUR                  R!                  [(        R*                  X40 5      PM1     nn[-        X5       H2  u  nnUR/                  U5        UR                  R1                  U5        M4     [3        U5        GM     UR5                  5         UR                  R7                  5         U$ s  snf s  sn
f s  snf )a  
A graph transformation that merges matrix multiplication operations that share the same right-hand
side operand into one large matrix multiplication.
           ____      _________        _________
  ----    |    |    |         |     M|  A * C  |
M| A  |  T| B  | * K|    C    | =    |---------|
  ---- ,  |    |    |         |     T|  B * C  |
   K       ----      ---------        ---------
            K            R                R
call_functionget_attrr#   r   )r   graphr!   optargetr   matmulargs
setdefaultappenditemsr   r(   r   strr,   r+   catr   rangeoperatorgetitemzipreplace_all_uses_with
erase_noder   	recompilelint)r)   gm	rhs_users	lhs_usersnodelhsrhsmmsmmlhs_valslmerge_mm_catmerge_mmmerge_mm_splitoutmerge_mm_resoldnews                     r   merge_matmulrP   \   s    
	B(*I(*I 77o%ELL)H99
  FFj0cjjcFFj0cjjcS"%,,T2S"%,,T2  OO%s8a< %S))),-2GGAJ- KSS(Qz!S'9'9rxx  #q@(S(23(<(<bhh$# xx--eii#D
 88))LL 
 // 8/2

 SX
& HH""8#3#3n5JBO& 	 
 C.HC%%c*HH$ / 	rc &f LLNHHMMOIW . T.
s   %K9<K>6L)   )r$   r8   typingr   r   r   r   torch.fx._symbolic_tracer   torch.fx.noder   torch.fx.passes.tools_commonr   Tensorr   intr   r(   nnModulerP    r    r   <module>r[      s      $ $  3  7'LL'"&u||"4'
5<<'2"T "d "# "Jd $V Vr    