
    קgx                         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dS )    N)Node)symbolic_trace)legalize_graph)DictListTupleresultinputsreturn.c                     t          | t          j        j                  rdgt	          |          z  }nd |D             }t          j        | |          S )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   c                 (    g | ]}|j         d          S r   )shape).0xs     ^/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/fx/experimental/merge_matmul.py
<listcomp>z(split_result_tensors.<locals>.<listcomp>!   s    ---!'!*---    )
isinstancetorchfxProxylensplit)r	   r
   splitss      r   split_result_tensorsr      sS    " &%(.)) .s6{{"--f---;vv&&&r      absearch_depthc                     | |k    rdS t          | j                  dk    rdS |dk    rdS | j        D ]}t          |||dz
            r dS dS )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    " 	Avvt 1""u qt    a!122 	44	 5r   nodesc                     t          j        | d          D ](\  }}t          ||          st          ||          r dS )dS )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   sZ     &ua00  1A 	-1"5"5 	55	 4r   in_modc                    t          |           i }i }j        j        D ]}|j        dk    s|j        t
          j        ur!|j        \  }}|j        dk    r|j        n|}|j        dk    r|j        n|}|                    |g           	                    |           |                    |g           	                    |           |
                                D ]d\  }}t          |          dk     rt          |          s*d |D             }fd|D             }t          |t                    rj                            |          n|}j                            t
          j        |fi           }j                            t
          j        ||fi           }	j                            t$          |	|fi           fdt'          t          |                    D             }
t)          ||
          D ]4\  }}|                    |           j                            |           5t/                     f                                 j                                         S )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(   c                 (    g | ]}|j         d          S r   )args)r   mms     r   r   z merge_matmul.<locals>.<listcomp>   s    ---2BGAJ---r   c                 r    g | ]3}t          |t                    rj                            |          n|4S  )r   strgraphr1   )r   lgms     r   r   z merge_matmul.<locals>.<listcomp>   s=    SSSQz!S'9'9@rx  ###qSSSr   c                 `    g | ]*}j                             t          j        |fi           +S r6   )r8   r0   operatorgetitem)r   outr:   merge_mm_splits     r   r   z merge_matmul.<locals>.<listcomp>   sE     
 
 
 H""8#3nc5JBOO
 
 
r   )r   r8   r&   optargetr   matmulr3   
setdefaultappenditemsr   r-   r   r7   r1   r0   catr   rangezipreplace_all_uses_with
erase_noder   	recompilelint)r.   	rhs_users	lhs_usersnodelhsrhsmmslhs_valsmerge_mm_catmerge_mmmerge_mm_resoldnewr:   r?   s                @@r   merge_matmulrY   ]   s    
		B(*I(*I  3 37o%%EL)H)H9S
  Fj00cjjcFj00cjjcS"%%,,T222S"%%,,T2222OO%% * *Ss88a<< %S)) 	----- TSSS(SSS(23(<(<Ebh$$$# x--ei#DD
 8))%,s8LbQQ // 8S/2
 

 
 
 
 
SXX
 
 
 C.. 	% 	%HC%%c***H$$$$ 	rLLNNNHMMOOOIr   )r   )r   torch.fx.noder   torch.fx._symbolic_tracer   torch.fx.passes.tools_commonr   r)   r<   typingr   r   r   Tensorr   intr$   r-   nnModulerY   r6   r   r   <module>rb      s6          3 3 3 3 3 3 7 7 7 7 7 7      $ $ $ $ $ $ $ $ $ $'L'"&u|"4'
5<' ' ' '2" "T "d "# " " " "Jd    $O O O O O O Or   