
    sgx                        d dl Z d dlmZ d dlmZ d dlmZ d dlZd dlZd dl	m
Z
mZmZ de j                  dee j                     dee j                  d	f   fd
ZddededefdZdee   fdZde j$                  j&                  fdZy)    N)Node)symbolic_trace)legalize_graph)DictListTupleresultinputsreturn.c                     t        | t        j                  j                        rdgt	        |      z  }n|D cg c]  }|j
                  d    }}t        j                  | |      S c c}w )a  
    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       U/var/www/html/venv/lib/python3.12/site-packages/torch/fx/experimental/merge_matmul.pysplit_result_tensorsr      sW    " &%((..)s6{"&,-!''!*--;;vv&& .s   A'absearch_depthc                     | |k(  ryt        | j                        dk(  ry|dk(  ry| j                  D ]  }t        |||dz
        s y y)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   &   sc    " 	Av 1" q    a!12     nodesc                 t    t        j                  | d      D ]  \  }}t        ||      st        ||      s y y)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(   K   s?     &&ua0 1A-1"5 r    in_modc           	         t        |       }i }i }|j                  j                  D ]  }|j                  dk7  s|j                  t
        j                  ur/|j                  \  }}|j                  dk(  r|j                  n|}|j                  dk(  r|j                  n|}|j                  |g       j                  |       |j                  |g       j                  |        |j                         D ]  \  }}t        |      dk  rt        |      s"|D cg c]  }|j                  d    }	}|	D 
cg c]/  }
t        |
t              r|j                  j                  |
      n|
1 }}
t        |t              r|j                  j                  |      n|}|j                  j!                  t
        j"                  |fi       }|j                  j!                  t
        j                  ||fi       }|j                  j!                  t$        ||fi       }t'        t        |            D cg c]/  }|j                  j!                  t(        j*                  ||fi       1 }}t-        ||      D ]1  \  }}|j/                  |       |j                  j1                  |       3 t3        |        |j5                          |j                  j7                          |S c c}w c c}
w c c}w )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  377o%ELL)H99S
  FFj0cjjcFFj0cjjcS"%,,T2S"%,,T23 OO% *Ss8a< %S)),-2BGGAJ-- KSSQz!S'9rxx  #q@SS(23(<bhh$# xx--eii#D
 88))%,,s8LbQ // 8S/2

 SX
 HH""8#3#3nc5JBO
 
 C. 	%HC%%c*HH$	% 	rU*X LLNHHMMOII . T 
s   K44K)4K#)   )r   torch.fx.noder   torch.fx._symbolic_tracer   torch.fx.passes.tools_commonr   r$   r8   typingr   r   r   Tensorr   intr   r(   nnModulerP    r    r   <module>r[      s      3 7   $ $'LL'"&u||"4'
5<<'2"T "d "# "Jd $O Or    