
    Vh                         d dl Z d dlZd dlZd dlmZ d dlZd dlmZmZ d dlm	Z	m
Z
 ddlmZmZ ddlmZ  G d d	      Zy)
    N)Optional)ExprSymbol)FloorDivModularIndexing   )	sympy_dot
sympy_subs)Vc                       e Zd ZdZedededefd       Zede	e   de	e   fd       Z
ededefd       Zed	ed
edededeee	e   e	e   e	e   f      f
d       Zed	ed
edee   fd       Zy)BlockPatternMatcherz-
    Matches block indexing expressions.
    exprsymbolreturnc                     | j                  |      }t        j                  j                  t	        fdt        j
                  j                  |      D              z   S )a  
        Given a sympy expression, return the subexpression comprised only of terms
        involving the specified symbol.

        For example, if `expr` is `x * 5 + x ** 2 + y * 2 + 5`, and `symbol` is `x`,
        this returns `x * 5 + x ** 2`.
        c              3   @   K   | ]  }|j                   v s|  y wN)free_symbols).0termr   s     V/home/dcms/DCMS/lib/python3.12/site-packages/torch/_inductor/codegen/block_analysis.py	<genexpr>zCBlockPatternMatcher.get_subexpr_involving_symbol.<locals>.<genexpr>   s#      "
&DDUDU:UD"
s   )_preprocesssympySZerosumAdd	make_args)clsr   r   s     `r   get_subexpr_involving_symbolz0BlockPatternMatcher.get_subexpr_involving_symbol   sK     t$ww||c "
"YY006"
 
 
 	
    dimsc                     t        j                  t        j                  j                  g      }| ddd   D ]  }||d   z  }|j                  |        g |S )z
        Compute the cumulative size of each dimension's slice.
        This proceeds from the last dim up to the second.
        Nr   )collectionsdequer   r   One
appendleft)r#   numelsdimnumels       r   get_slice_numelsz$BlockPatternMatcher.get_slice_numels"   s^     ""EGGKK=12; 	%C&)OEe$	% yr"   c                 &    | j                  d      S )NT)identity)expand)r   s    r   r   zBlockPatternMatcher._preprocess.   s     {{D{))r"   index	index_varr,   num_dimsc           
      <   | j                  |      }t        j                  t        j                  |g      }t        |      D cg c]  } |d|        }}t        |      D cg c]  } |d|        }}| j                  |d|       }	t        ||	d         gt        |dd |	dd       D 
cg c]  \  }
}t        |||
       c}}
z   }t        ||      }|dk\  rt        j                  d|	      \  }}}|t        |||      z  }|t        ||      z  }d
}d
}t        j                  j                  |      D ]1  }|j                  |      r	|rd} nd}|j                  |      r/d} n |ry|j                  |      y|dd D ]$  }
|
vst        j                  j                   |
<   & |dd D ]$  }|vst        j                  j"                  |<   & t$        j&                  j(                  dt*        dt*        ffd}|d   g|dd D 
cg c]
  }
 ||
       c}
z   }|D cg c]
  } ||       }}| j                  |      }	|D cg c]  }t-        |       }}|d   vsJ d       j/                  |	d         sy||	d   z  |d<   t        ||      }j1                  j3                  |      j3                  |            s!J t5        j6                  d| d| d             |||fS c c}w c c}w c c}}
w c c}
w c c}w c c}w )z
        Matches modular indexing expressions, converting them to implied block dimensions and strides.
        See triton.py for more information.
        excludedim_mod
stride_modNr         zstride denominator other)r    FTr   r   c                 ,    j                  |          S r   )lookup_precomputed_size)r   matchsizevarss    r   	get_matchz?BlockPatternMatcher.match_mod_div_block_expr.<locals>.get_match{   s    33E$K@@r"   z,Expected not to match the leading dimension!z/
            Invalid match!
            Index: z!
            Matched expression: z
            )r   	functoolspartialr   Wildranger-   r   zipr   r	   symbolsr   r   r=   r   r(   r   r   graphr>   r   r
   statically_known_multiple_ofstatically_known_equalsremove_precomputed_replacementstextwrapdedent)r    r1   r2   r,   r3   wildidxr#   stridesslice_numelsr+   block_index_exprs
match_exprstridedenomothermod_div_patternfloor_div_patternfirst_dim_floor_div_matchedmatch_failedargr?   r   matched_indexr=   r>   s                           @@r   match_mod_div_block_exprz,BlockPatternMatcher.match_mod_div_block_expr3   s    &   i[A=B8_McD73%1MMCH?SCtj$67SS ++D(O<%iaAB!$qr(L,<=F
U Iuc2F
 
 w(9:
 q=#(==1KQU#V FE5$y%'OOO &)U)C C*/' Lyy**51 99./ 3'+26/YY/#'L  J'= 8 	)C%"WW[[c
	) abk 	-FU" %f	- 77##	AD 	AT 	A Qyd12h?sIcN??3:;9V$;;++D1ARSZe4SS
 Awe#S%SS#44ULOL,q/)Q "'+<=//
 44]C44U;
 	
 ??7 !!. 0
	
 
 W///u NS
F
l @;Ss$   K?(L4L	L3LLc                     | j                  |      }t        j                  d|g      }|j                  ||z        }|y||   S )zf
        Matches simple expressions of the form stride * index, returning the
        stride.
        rR   r5   N)r   r   rB   r=   )r    r1   r2   rR   ms        r   match_affine_block_exprz+BlockPatternMatcher.match_affine_block_expr   sI     &Hyk:KK	F*+9yr"   N)__name__
__module____qualname____doc__classmethodr   r   r!   staticmethodlistr-   r   intr   tupler[   r^    r"   r   r   r      s    
 
f 
 
 
 	tDz 	d4j 	 	 *$ *4 * * i0i0 i0 	i0
 i0 
%T
DJT
:;	<i0 i0V   
$	 r"   r   )r&   r@   rJ   typingr   r   r   r   torch.utils._sympy.functionsr   r   utilsr	   r
   virtualizedr   r   rh   r"   r   <module>rm      s,          B ) ` `r"   