
    i|                        d dl Z d dlZd dlZd dlZd dlZd dlmZmZmZ d dl	m
Z
mZmZ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 d dlmZmZ d dlmZ d	d
lmZmZ ddlmZ ddl m!Z! ddl"m#Z#m$Z$m%Z%m&Z&m'Z' ddl(m)Z)m*Z*  ed      Z+ ejX                  e-      Z. ej^                  d      j`                  Z1 G d de jd                        Z3 ejh                  d       G d de3             Z5 ejh                  d       G d de3             Z6 ejh                  d       G d de3             Z7 ejh                  d       G d d             Z8ejh                   G d d             Z9 G d  d!e*jt                        Z; G d" d#e*jx                        Z=d$e>d%e?e'eej                  gej                  f   f   fd&ZBd'eej                     d$e>d%e?eCeCej                        e'f   fd(ZDd)d*d'eej                     d$e>d%e?eCeej                        e'f   fd+ZEd,d)d-d.d/ed0e
f   d'eej                     d1eFd$e>d2eeCej                        d%e9fd3ZG	 dAd/e
d4eCeCej                        d5e'd1eFd%e;f
d6ZHd7d8d%e?eeCej                        eeCej                        f   fd9ZId%e>fd:ZJ G d; d<e!      ZK	 	 dBd/ed0e
f   d=eej                     d>eeej                        d?eFd%eej                     f
d@ZLy)C    N)CallableIterableSequence)AnyOptionalTypeVarUnion)Self)patch)get_free_symbols)free_symbolsfree_unbacked_symbols
OrderedSet   )make_symbolSymT   )index_prevent_reordering)DefaultHandler)get_dtype_sizereduction_num_outputssympy_index_symbol
sympy_subs	VarRanges)ReductionTypeVTzindirect|tmpc                      e Zd ZU eed<   ej                  ed<   ej                  	 dde	de
ej                     fd       Zej                  deeef   defd       Zej                  dej                  fd       Zej                  defd	       Zej                  defd
       Zej                  de	fd       Zej                  de	fd       ZddedefdZy)Depnameindexunbacked_onlyreturnc                      y N selfr#   s     V/var/www/html/engine/venv/lib/python3.12/site-packages/torch/_inductor/dependencies.pyget_free_symbol_useszDep.get_free_symbol_uses)   s     	    renamesc                      y r&   r'   r)   r-   s     r*   renamez
Dep.rename/       r,   c                      y r&   r'   r)   s    r*   	get_numelzDep.get_numel3   r1   r,   c                      y r&   r'   r3   s    r*   numbytes_hintzDep.numbytes_hint7   r1   r,   c                      y r&   r'   r3   s    r*   
numel_hintzDep.numel_hint;   r1   r,   c                      y r&   r'   r3   s    r*   has_unbacked_symbolszDep.has_unbacked_symbols?   r1   r,   c                      y r&   r'   r3   s    r*   is_contiguouszDep.is_contiguousC   r1   r,   prefixc                     | S r&   r'   )r)   r=   s     r*   normalize_with_stride_orderzDep.normalize_with_stride_orderG   s    r,   NFt)__name__
__module____qualname__str__annotations__sympyExprabcabstractmethodboolr   Symbolr+   dictr
   r0   r4   intr6   r8   r:   r<   r?   r'   r,   r*   r    r    %   s1   
I::$)!	ELL	! 
 	d38n    	5::   	s   	C   	d   	t  #  r,   r    T)frozenc                   $   e Zd ZU eed<   ej                  ed<   eej                  df   ed<   eej                  df   ed<   dZ	e
e   ed<   	 d ded	eej                     fd
Zd	efdZed	efd       Zdd d	e
ee      fdZd	ej                  fdZd!dZd"ded	d fdZed	eej                  ej                  f   fd       Zd!dZd	ej                  fdZdeeef   d	d fdZd	efdZd	efdZd	efdZd	efdZd#ded	efdZ d	efdZ!d	efdZ"y)$	MemoryDepr!   r"   .	var_namessizeNmoder#   r$   c                     t        | j                  |      t        | j                  |      z  t        | j                  |      z  S r&   )r   r"   rT   rS   r(   s     r*   r+   zMemoryDep.get_free_symbol_usesU   s=     TZZ7tyy-89t~~}=>	
r,   c                     d}| j                   d| j                    }d| j                  d| j                   d| j                   | dS )N z, z
MemoryDep())rU   r!   r"   ranges)r)   
maybe_modes     r*   __repr__zMemoryDep.__repr__^   sM    
99 dii[)JDII=4::,bj\QRSSr,   c                 ,    t        | j                        S r&   )lenrS   r3   s    r*   num_varszMemoryDep.num_varsd   s    4>>""r,   otherc                    | j                   |j                   k(  sJ | j                   t        | j                  j                        k7  ry|j                   t        |j                  j                        k7  ryt	        d t        j                  | j                  |j                        D              ryt        j                  j                  j                  | j                  | j                        }t        j                  j                  j                  |j                  |j                        }t        t        |            t        |      k7  s t        t        |            t        |      k7  rt        j                  d| |||       yt        |      t        |      k7  ryt!        |      D ci c]  \  }}||
 }}}|D cg c]  }||   	 }}t        |      t        t#        | j                               k(  sJ |S c c}}w c c}w )zD
        Can return None if not able to decide loop orders.
        Nc              3   4   K   | ]  }|d k(  xs |dk(    yw)r   r   Nr'   ).0ss     r*   	<genexpr>z7MemoryDep.decide_loop_order_to_match.<locals>.<genexpr>}   s      PAqAvaPs   zaunable to decide loop order. self_dep=%s v.s. other_dep=%s, self_strides=%s v.s. other_strides=%s)r_   r^   r"   r   any	itertoolschainrT   r   graphsizevarsstride_hintsrS   r   logdebug	enumeraterange)r)   r`   self_stridesother_stridesird   stride_to_indexorders           r*   decide_loop_order_to_matchz$MemoryDep.decide_loop_order_to_matchh   s    }}... ==C

 7 788>>S!9!9:: PEJJ)OPP ww''44TZZP((55ekk5??S z,'(C,==}%B
B  IIs 
 l#z-'@@,5l,CDDAq1a4DD-:;#;;% JuT]]/C$DDDD	 E;s   -G=Hc                 j    t        | j                  t        j                  | j                  d            S )zF
        Return the offset by setting every variable to be 0.
        r   )r   r"   rN   fromkeysrS   r3   s    r*   
get_offsetzMemoryDep.get_offset   s$     $**dmmDNNA&FGGr,   c                     t        | j                  gt        j                  | j                  | j
                        | j                   S )z
        Normalize by merging loops. The different to normalize_with_stride_order is,
        this method does not reorder loops while normalize_with_stride_order reorder
        loops based on stride order.
        )rR   r!   _RecordLoadStoreInner
_normalizer"   rZ   rU   r3   s    r*   	normalizezMemoryDep.normalize   sC     II
"--djj$++F
 II
 	
r,   r=   c                 @   ddl m} t        j                  j                  j                  | j                  | j                        }t        t        t        |            |j                  d      }|j                  |      }| j                  }| j                  } ||      } ||      }	t        j                  j                  j                  |	|t        | j                  g|	|            \  }
}}t!        |      \  }}t#        t%        |	 ||
D cg c]
  } ||       c}                  }t'        t)        j*                  | j                        |      }t-        | j.                  |t1        |j3                               t1        |j5                                     }|S c c}w )a'  
        Used to decide if two MemoryDep does not equal due to different loop orders.
        More specifically, when dep1 and dep2 are not equal, we can normalize
        both and check if they are equal after that. If yes, then the mismatch is
        caused by different loop orders.
        r   )irT)keyreverse)torch._inductorr~   r   ri   rj   rk   r"   rS   sortedro   r^   __getitem__same_reorderrT   _simplify_loopsr   var_builderrN   zipr   rH   expandrR   r!   tuplekeysvalues)r)   r=   r~   stridesrt   stride_reordersizesrS   new_reordered_sizesnew_reordered_var_namesnew_simplified_sizesreindex_prune
var_rangesadd_varxreplacement	new_indexouts                      r*   r?   z%MemoryDep.normalize_with_stride_order   sV    	'''""//

DNNK uS\*0C0CTR/		NN	,U3"0";010@0@0P0P#$57J1
-gv *&1
G'-ABBC
 u||DJJ7E	IIy%
(9":E*BSBSBU<V
 
 Cs   Fc                 T    t        t        | j                  | j                              S )z{c0: 128, c1: 512, ...})rN   r   rS   rT   r3   s    r*   rZ   zMemoryDep.ranges   s     C		233r,   c                     t        | j                  t        j                  j                  j                  | j                  | j                        | j                  | j                  | j                        S )N)r!   r"   rS   rT   rU   )rR   r!   r   ri   rj   simplify_with_rangesr"   rZ   rS   rT   rU   r3   s    r*   r   zMemoryDep.simplify_with_ranges   sM    ''""77

DKKPnn
 	
r,   c                 R   | j                         r+t        j                  j                  | j                        }|S t        | j                  j                        }t        j                  j                  }t        | j                  | j                        D ]  \  }}||v s||z  } |S r&   )is_indirectr   ri   r4   r!   r   r"   r   rH   SOner   rS   rT   )r)   numelvarsvarrT   s        r*   r4   zMemoryDep.get_numel   s    GG%%dii0E  -7tzz7N7N,ODGGKKE ; )	T$;!DLE) r,   r-   c                     | j                   |v rEt        || j                      | j                  | j                  | j                  | j
                        S | S )N)rS   rT   rU   )r!   rR   r"   rS   rT   rU   r/   s     r*   r0   zMemoryDep.rename   sJ    99		"

..YYYY  r,   c                     	 t         j                  j                  j                  | j	                               t        t         j                  j                  | j                              z  S # t        $ r Y yw xY wNr   	r   ri   rj   	size_hintr4   r   	get_dtyper!   NotImplementedErrorr3   s    r*   r6   zMemoryDep.numbytes_hint  b    	77##--dnn.>?.!!$)),C   # 		   A)A, ,	A87A8c                     	 t         j                  j                  j                  | j	                         d      S # t
        $ r Y yw xY wNr   )fallbackr   ri   rj   r   r4   r   r3   s    r*   r8   zMemoryDep.numel_hint  @    	77##--dnn.>-KK" 		   8; 	AAc                 L    t        t        | j                                     dkD  S r   r^   r   r4   r3   s    r*   r:   zMemoryDep.has_unbacked_symbols      ()9:;a??r,   c                     t        | j                  t        j                        ryt        | j                  t        j                        xr | j                  | j
                  v S NT)
isinstancer"   rH   IntegerrM   rS   r3   s    r*   r<   zMemoryDep.is_contiguous  s>    djj%--0$**ell3T

dnn8TTr,   result_for_complex_expressionc                    t        | j                        dk(  ryt        | j                  t        j
                        r| j                  j                  n| j                  g}| j                  d   }|D ]  }||k(  r yt        |t        j                        s%t        |j                        dk(  s>|j                  d   |k(  sQt        |j                  d   t        t        j                  f      s|j                  d   dkD  s y |S )zA
        Whether the stride for the last dimension is 1.
        r   Tr   r   F)
r^   rS   r   r"   rH   AddargsMulrO   r   )r)   r   termslast_symterms        r*   stride1_for_last_dimzMemoryDep.stride1_for_last_dim  s     t~~!##-djj%))#D

4::,>>"% 	Dx
 4+		Na'IIaLH,tyy|c5==-ABIIaL1$	 -,r,   c                     t        | j                  t        j                        r+| j                  | j                  vxr | j                          S t        | j                  t        t        j                  f      S r&   )r   r"   rH   rM   rS   r   rO   r   r3   s    r*   	is_scalarzMemoryDep.is_scalar6  sR    djj%,,/::T^^3ND<L<L<N8NN$**sEMM&:;;r,   c                 N    t        d | j                  j                  D              S )Nc              3   F   K   | ]  }t        |j                          y wr&   )r   r!   )rc   vs     r*   re   z(MemoryDep.is_indirect.<locals>.<genexpr><  s     H1;qvv&Hs   !)rf   r"   r   r3   s    r*   r   zMemoryDep.is_indirect;  s    H

0G0GHHHr,   r@   )r$   rR   rA   T)#rC   rD   rE   rF   rG   rH   rI   r   rM   rU   r   rL   r   r+   r\   propertyrO   r_   listru   rx   r|   r?   rN   rZ   r   r4   r0   r6   r8   r:   r<   r   r   r   r'   r,   r*   rR   rR   K   s    I::U\\3&''


C
  D(3- %*
!
	ELL	!
T# T ## # #7 7c@S 7rHEJJ H

*# * *X 4U\\5::56 4 4
	5:: 		d38n 	 	s C @d @Ut U
-$ -RV -:<4 <
IT Ir,   rR   c                      e Zd ZU eed<   dZee   ed<   edej                  fd       Z
dej                  fdZdeeef   dd fdZ	 dd	edeej                      fd
ZdefdZdefdZdefdZdefdZdefdZdefdZy)StarDepr!   NrU   r$   c                     t        d      )NzStarDep does not have an indexr   r3   s    r*   r"   zStarDep.indexF       ""BCCr,   c                 T    t         j                  j                  | j                        S r&   )r   ri   r4   r!   r3   s    r*   r4   zStarDep.get_numelK  s    ww  ++r,   r-   c                 h    | j                   |v r#t        || j                      | j                        S | S r&   )r!   r   rU   r/   s     r*   r0   zStarDep.renameN  s-    997499-tyy99r,   r#   c                     t               S r&   r   r(   s     r*   r+   zStarDep.get_free_symbol_usesS       |r,   c                     	 t         j                  j                  j                  | j	                               t        t         j                  j                  | j                              z  S # t        $ r Y yw xY wr   r   r3   s    r*   r6   zStarDep.numbytes_hintX  r   r   c                     	 t         j                  j                  j                  | j	                         d      S # t
        $ r Y yw xY wr   r   r3   s    r*   r8   zStarDep.numel_hint`  r   r   c                 L    t        t        | j                                     dkD  S r   r   r3   s    r*   r:   zStarDep.has_unbacked_symbolsf  r   r,   c                      yNFr'   r3   s    r*   r<   zStarDep.is_contiguousi      r,   c                      yr   r'   r3   s    r*   r   zStarDep.is_scalarl  r   r,   c                      yr   r'   r3   s    r*   r   zStarDep.is_indirecto  r   r,   r@   )rC   rD   rE   rF   rG   rU   r   r   rH   rI   r"   r4   rN   r0   rL   r   rM   r+   rO   r6   r8   r:   r<   r   r   r'   r,   r*   r   r   ?  s     ID(3- Duzz D D,5:: ,d38n   %*!	ELL	!
s C @d @t 4 T r,   r   c                       e Zd ZU eed<   eed<   dZeed<   	 ddedeej                     fdZ
edej                  fd       Zdej                  fd	Zd
eeef   dd fdZdefdZdefdZdefdZdefdZy)WeakDepr!   mutating_bufFis_faker#   r$   c                     t               S r&   r   r(   s     r*   r+   zWeakDep.get_free_symbol_uses  r   r,   c                     t        d      )NzWeakDep does not have an indexr   r3   s    r*   r"   zWeakDep.index  r   r,   c                 6    t         j                  j                  S r&   )rH   r   r   r3   s    r*   r4   zWeakDep.get_numel  s    ww{{r,   r-   c                 ~    | j                   |v r.t        || j                      | j                  | j                        S | S r&   )r!   r   r   r   r/   s     r*   r0   zWeakDep.rename  s5    997499-t/@/@$,,OOr,   c                      yNr   r'   r3   s    r*   r6   zWeakDep.numbytes_hint      r,   c                      yr   r'   r3   s    r*   r8   zWeakDep.numel_hint  r   r,   c                      yr   r'   r3   s    r*   r:   zWeakDep.has_unbacked_symbols  r   r,   c                      yr   r'   r3   s    r*   r<   zWeakDep.is_contiguous  r   r,   Nr@   )rC   rD   rE   rF   rG   r   rL   r   rH   rM   r+   r   rI   r"   r4   rN   r0   rO   r6   r8   r:   r<   r'   r,   r*   r   r   {  s     I GT %*!	ELL	!
 Duzz D D5:: d38n  
s C d t r,   r   c                   |    e Zd ZU ej                  ed<   eej                  df   ed<   eej                  df   ed<   y)IndexExprDepr"   .rS   rT   N)rC   rD   rE   rH   rI   rG   r   rM   r'   r,   r*   r   r     s3    ::U\\3&''


C
  r,   r   c                   P   e Zd ZU ee   ed<   ee   ed<   ee   ed<   dZee	e
j                        ed<   dZee   ed<   deeef   dd fd	Zd
eeee   f   dd fdZddZede	d    dd fd       Zdee   dd fdZdee   fdZddedee   fdZ	 ddedee
j6                     fdZy)
ReadWritesreadswritesindex_exprsN
range_varsr   r-   r$   c                     t        t        fd| j                  D              t        fd| j                  D              | j                  | j
                  | j                        S )Nc              3   @   K   | ]  }|j                          y wr&   r0   rc   depr-   s     r*   re   z$ReadWrites.rename.<locals>.<genexpr>  s     Asszz'*A   c              3   @   K   | ]  }|j                          y wr&   r   r   s     r*   re   z$ReadWrites.rename.<locals>.<genexpr>  s     Bsszz'*Br   )r   r   r   r   r   r   r   r/   s    `r*   r0   zReadWrites.rename  sK    AdjjAABdkkBBOOOO
 	
r,   r   c                    t        |t        t        t        f      sJ t        |t              st        |g      }t	        t        j
                  | j                  |      | j                  | j                  | j                  | j                        S r&   )r   r   r   r   r   unionr   r   r   r   r   )r)   r   s     r*   	with_readzReadWrites.with_read  sn    #*=>>>#z*cU#CTZZ-KKOOOO
 	
r,   c                    t        j                  | j                  |j                        }t        j                  | j                  |j                        }t        j                  | j                  |j                        }t        ||z
  ||      S r&   )r   r   r   r   r   r   )r)   r`   r   r   r   s        r*   mergezReadWrites.merge  sg      U[[9!!$++u||< &&t'7'79J9JK%&.&+>>r,   read_writesc                 <   t        j                  | D cg c]  }|j                   c} }t        j                  | D cg c]  }|j                   c} |z
  }t        j                  | D cg c]  }|j                   c} }t        |||      S c c}w c c}w c c}w r&   )r   r   r   r   r   r   )r   rw
all_writes	all_readsall_index_exprss        r*   
merge_listzReadWrites.merge_list  s    %%K'Hb		'HI
$$+&FBrxx&FG*T	$**k,RR^^,RS)ZAA (I&F,Rs   BB+B	rem_readsc                     t        | j                  |z
  | j                  | j                  | j                  | j
                        S r&   )r   r   r   r   r   r   )r)   r  s     r*   remove_readszReadWrites.remove_reads  s9    JJ"KKOOOO
 	
r,   c                 V    t        j                  | j                  | j                        S r&   )rg   rh   r   r   r3   s    r*   reads_and_writeszReadWrites.reads_and_writes  s    tzz4;;77r,   ignore_integer_indexc                     t               }| j                         D ][  }t        |t              s|r+t        |j                  t
        t        j                  f      rA|j                  |j                         ] |S )z6
        Integer index is used for load_seed.
        )
r   r
  r   rR   r"   rO   rH   r   addr!   )r)   r  namesr   s       r*   buffer_nameszReadWrites.buffer_names  se     ",((* 	$Cc9-'z		C/0 		#((#	$ r,   r#   c                 l    t               }| j                         D ]  }||j                  |      z  } |S r&   )r   r
  r+   )r)   r#   resultr   s       r*   r+   zReadWrites.get_free_symbol_uses  s=     ,6<((* 	>Cc..}==F	>r,   )r`   r   r$   r   r   r@   )rC   rD   rE   r   r    rG   r   r   r   r   rH   rI   r   r   rN   rF   r0   r	   r   r   staticmethodr  r  r   r
  rL   r  rM   r+   r'   r,   r*   r   r     s   c?sOL))-1Jejj)*1&*J#*
d38n 
 


U3
3#78 

\ 

? B\ 2 B| B B
jo 
, 
8(3- 8 C  %*!	ELL	!r,   r   c                   H    e Zd Zdededdf fdZedeee	j                  f   dee	j                     dee	j                     ddfd	       Zede	j                  dedee	j                  ee	j                  d
f   ee	j                  d
f   f   fd       Zde	j                  dee	j                  ee	j                  d
f   ee	j                  d
f   f   fdZdede	j                  ddfdZdededdfdZ	 ddede	j                  dedee   ddf
dZdede	j                  deddfdZde	j                  deej2                     ddfdZ	 	 ddedeee	j                  e	j                  e	j                  f   dedej2                  dedeeee	j                  f      dee   ddfdZ xZS ) rz   r   r|   r$   Nc                     t         |           t               | _        t               | _        t               | _        || _        || _        y r&   )super__init__r   _reads_writes_index_exprs_var_ranges_should_normalize)r)   r   r|   	__class__s      r*   r  z_RecordLoadStoreInner.__init__  s:    '1|.8l6@l&0'0r,   r"   rS   r   c                     t        | t        j                        sy| j                  }|r4|d   |vr,|j	                          |j	                          |r	|d   |vr*yyyy)zz
        Reduction has last (reduced) dim in its sizes, but
        downstream users won't.  Normalize this away.
        Nr   )r   rH   rI   r   pop)r"   rS   r   r   s       r*   drop_unused_symbolsz)_RecordLoadStoreInner.drop_unused_symbols  sW     %,))IbM=MMOIIK IbM=i=ir,   .c                     g |j                         }t        |j                               }t        j                  j
                  j                  ||t        |g||            \  }}}t        t                     \  }}	t        t        | ||D 
cg c]
  }
 |	|
       c}
                  }t        t        j                  |      |      }g |j                         }g |}| j                  |||       |t        |      t        |      fS c c}
w r&   )r   r   r   r   ri   rj   r   r   r   canonicalization_prefixrN   r   r   rH   r   r  )clsr"   r   
index_varsr   	new_sizesr   r   new_varsr   r   r   s               r*   r{   z _RecordLoadStoreInner._normalize  s     *z()
j'')*%&WW%5%5%E%E$eWj%@&
"	7F ((?(AB'3z7	3R1GAJ3R+STU5<<.<%X]]_% iL	x;eHouY'777 4Ss   Dc                    | j                   s| j                  j                         D cg c]+  }t        j                  j
                  j                  |      - }}t        | j                  j                         |      D cg c]  \  }}|dk7  s| }}}|D cg c]
  }|dk7  s	| }}| j                  |||       |t        |      t        |      fS | j                  j                         D ci c]/  \  }}|t        j                  j
                  j                  |      1 }}}| j                  ||      S c c}w c c}}w c c}w c c}}w r   )r  r  r   r   ri   rj   simplifyr   r   r  r   itemsr{   )r)   r"   r   r   kr   rS   r   s           r*   canonicalizez"_RecordLoadStoreInner.canonicalize2  s)    %%;?;K;K;R;R;TUaQWW%%..q1UEU'*4+;+;+@+@+BE'JUtq!aSTfUIU %01aQ0E0$$UIu=%	*E%L88 ((..0
1 qww((++

 
 uj11 VU0

s#   0D<EE
E&E34Er!   c                 l    | j                   j                  t        |g| j                  |              y r&   )r  r  rR   r*  r)   r!   r"   s      r*   loadz_RecordLoadStoreInner.loadE  s'    	$B):):5)ABCr,   c                 r    t        |t              sJ | j                  |t        j                  |             y r&   )r   rO   r-  rH   r   r,  s      r*   	load_seedz_RecordLoadStoreInner.load_seedH  s)    %%%%		$e,-r,   valuerU   c                 r    | j                   j                  t        |g| j                  |      d|i       y )NrU   )r  r  rR   r*  )r)   r!   r"   r0  rU   s        r*   storez_RecordLoadStoreInner.storeL  s0     	4N$*;*;E*BNNOr,   c                 2    | j                  ||d| d       y )Nzstore_reduction(rY   )r2  )r)   r!   r"   r0  s       r*   store_reductionz%_RecordLoadStoreInner.store_reductionQ  s    

4"25' ;<r,   dtypec                 d    | j                   j                  t        | j                  |              y r&   )r  r  r   r*  )r)   r"   r5  s      r*   
index_exprz _RecordLoadStoreInner.index_exprT  s&    lD,=,=e,DEFr,   r   
boundariesboundary_indicesindexing_dtyperightsortersorter_indicesc                     | j                   j                  t        |d                |(| j                   j                  t        |d                yy)z?Records the names of the buffers that bucketize will read from.r   N)r  r  r   )r)   r   r8  r9  r:  r;  r<  r=  s           r*   	bucketizez_RecordLoadStoreInner.bucketizeW  sA     	
1./KKOOGF1I./ r,   r&   NN)rC   rD   rE   r   rL   r  r  r	   rO   rH   rI   r   r  classmethodr   rM   r{   r*  rF   r-  r/  r   r2  r4  torchr5  r7  r   r?  __classcell__r  s   @r*   rz   rz     sp   19 1 1$ 1 S%**_%

# EJJ 
	 " 8JJ8,58	uzz5s!23U5::s?5KK	L8 822ZZ2	uzz5s!23U5::s?5KK	L2&D DUZZ DD D.c .# .$ .
 OSPP %

P36P>FsmP	P
=C =

 =3 =4 =G

 G8EKK3H GT G 48&*00 #uzz5::uzzAB0 	0
 0 0 sEJJ/00 !0 
0r,   rz   c                   ,     e Zd Zdededdf fdZ xZS )RecordLoadStorer   r|   r$   Nc                 @    t        ||      }t        | 	  |       y )Nr   r|   )parent_handler)rz   r  r  )r)   r   r|   rI  r  s       r*   r  zRecordLoadStore.__init__h  s$    .!Y
 	7r,   )rC   rD   rE   r   rL   r  rC  rD  s   @r*   rF  rF  g  s"    89 8 8$ 8 8r,   rF  r=   r$   c                      t        j                         i dt        j                  dt        j                  f fd}|fS )Nlengthr$   c                 B    t         t                     }| |<   |S r&   )r   next)rK  r   cntr=   r   s     r*   r   zvar_builder.<locals>.add_vart  s(    &$s)56
1r,   )rg   countrH   rI   rM   )r=   r   rN  r   s   ` @@r*   r   r   p  s<    
//
CJ

 u|| 
 wr,   argsizesc           	      t    t        |       \  }}|D cg c]  }t        t        ||             }}||fS c c}w r&   )r   r   map)r=   rP  r   r   rT   r   s         r*   index_vars_no_squeezerS  |  sC     &f-JKS%T4d3w+=&>%TD%T &Us   5d)r=   c                     ddl m} t        |       \  }}g }g }|D ]Q  }|j                  |      \  }}	|j	                  |       |j	                   |	t        t        ||                         S ||fS )Nr   )SqueezeView)r~   rV  r   squeezerappendr   rR  )
r=   rP  rV  r   r   r   r$  rT   new_sizer   s
             r*   index_vars_squeezerZ    s|      %f-J')D,.I ;'006'"GDWh!789:; r,   Fr'   )r|   r=   hidden_argsfn.r|   r[  c                   t        |d|i\  }}ddlm} t        | |      rt	        | g ||||      }nAt        ||      }	t        j                  |	      5   | g ||  d d d        |	j                  }|rg }
n!g t        j                  j                  |      }
t        t        |j                        t        |j                        |j                   |
|      S # 1 sw Y   zxY w)Nr=   r   )LoopBody)r|   )rZ  	loop_bodyr^  r   extract_loop_body_with_argsrF  r   set_ops_handlerrI  rg   rh   from_iterabler   r   r  r  r  )r\  r|   r=   r[  rP  r   r   r^  innerr  r   s              r*   extract_read_writesrd    s     *8CFCD*#"h+!d![!	
 Z9=r" 	$##{#	$!!
;y44T:;
5<< 5==!	 		$ 	$s   CC#r   r   c           	         ddl m} t        ||      }| j                  |      }| j                  rmt        | j                        D ci c]   \  }}|t        t        j                  |      " }	}}|j                         D 
ci c]  \  }
}|
t        ||	       }}
}| j                  |j                     D ]+  }|j                  |j                  ||j                            - | j                  |j                      D ]4  }|j#                  |j                  t%        ||j                                  6 | j                  |j&                     D ]7  }|j)                  |j                  ||j                     d |j*                         9 | j                  |j,                     D ],  }|j/                  |j                  ||j                     d        . | j                  |j0                     D ]!  }|j3                  ||j                     d        # | j                  |j4                     D ]%  }|j7                  d |j                  d d d fd d d        ' |S c c}}w c c}}
w )Nr   )MemoryUsageTyperH  )r_  rf  rz   indexing_from_argsindirect_varsrn   r   r   TMPr(  r   memory_usageLOADr-  buffer_name
index_name	LOAD_SEEDr/  rO   STOREr2  rU   STORE_REDUCTIONr4  
INDEX_EXPRr7  	BUCKETIZEr?  )r\  r   r   r|   rf  rc  name_to_indexrr   r   replr)  entrys               r*   r`  r`    s:    + "Z9ME))$/M	8A"BRBR8ST1;txx++TT<I<O<O<QRDAqJq$//RR!5!56 G

5$$mE4D4D&EFG!:!:; Q))3}U=M=M/N+OPQ!6!67 
%**+JJ		

 !@!@A 
%**+	

 !;!;< @u'7'78$?@!:!:; 	
 	dD1	
	
 LA URs   	%II
input_nodeztorch._inductor.ir.IRNodec                    ddl m}m}m} t	        | j                         |      r7g | j                         }g | j                         }t        |      dkD  r||fS yt	        | j                  j                  |      sy| j                         }d}d}|lt        |      dkD  r]t               }g }|D ]%  }	t	        |	t              s|	j                  |v r$|j                  |	j                         t        j                   j#                  |	j                        }
|
k|
j                         }|t	        ||      rt	        ||      rqt        |j                               dkD  rU|%g |j                         }g |j                         }|g |j                         k7  s|g |j                         k7  s y|j%                  |j                                ( ||k(  r||fS t        |      }|t        |      dkD  r]||fS )aX  
    Returns the size and reduction size of all inputs, if the sizes and reduction_sizes (if exist) are all the same.
    It's possible that a node has multiple inputs, some are Reduction nodes and others are Pointwise nodes.
    In this case, reduction_sizes of the Reduction nodes need to be the same.
    Otherwise returns (None, None).
    r   )ComputedBufferExternKernelLoopsr   r@  N)r~   rx  ry  rz  r   get_defining_opget_sizeget_reduction_sizer^   data	get_readsr   rR   r!   r  r   ri   try_get_bufferextend)rv  rx  ry  rz  rT   reduction_sizer   seen	new_readsreadbufferops               r*   #extract_input_node_reduction_rangesr    s    87
 *,,.?'$$&';:88:;~".))joo**E2
   "E15N'+D

 SZ!^ *!	 	1DdI.yyD HHTYYWW++DII6F~'')BzZL9"n-#b6K6K6M2NQR2R!)%?r'<'<'>%?N+R[[]+D#'A)>)>)@'AAT N[[]N F (  0-	1. I.))y)E; 
 SZ!^< .!!r,   c                       y)Ncr'   r'   r,   r*   r!  r!  0  s    r,   c                      e Zd ZU eej
                     ed<   d#deddfdZde	de
edf   d	ee	ef   defd
Z	 	 d$dedeeej                   f   dededej
                  f
dZdede
d   fdZdededee   de
d   fdZdedee   dedede
d   f
dZdej.                  dej.                  dedede
d   f   dede
d   f   f
dZded edef   d!eddfd"Zy)%FreeSymbolsOpsHandlersymbolsr#   r$   Nc                 T    t               | _        |rt        | _        y t        | _        y r&   )r   r  r   r   get_symbolsr(   s     r*   r  zFreeSymbolsOpsHandler.__init__8  s    !|4A0|r,   r!   r   .kwargsc                    t        j                  ||j                               D ]e  }t        |t        j
                  t        j                  j                  j                  f      sB| xj                  | j                  |      z  c_	        g y r&   )rg   rh   r   r   rH   rI   logicboolalgBooleanr  r  )r)   r!   r   r  as        r*   _defaultzFreeSymbolsOpsHandler._default<  s^    v}}7 	4A!ejj%++*=*=*E*EFG 0 0 33	4r,   	index_varrT   checkwrap_negc                     t        |t        j                  t        j                  j                  j
                  f      rJ | xj                  | j                  |      z  c_        t        dt        |       d      S )N(rY   )
r   rH   rI   r  r  r  r  r  r   rF   )r)   r  rT   r  r  s        r*   indirect_indexingz'FreeSymbolsOpsHandler.indirect_indexingA  s_     i%**ekk6I6I6Q6Q)RSSS((..!Ac)n%5Q"788r,   r   )N.c                      y)Nr@  r'   )r)   r   s     r*   frexpzFreeSymbolsOpsHandler.frexpL  s    r,   dtypes
combine_fnr   c                     dt        |      z  S Nr&   r^   )r)   r  r  r   s       r*   scanzFreeSymbolsOpsHandler.scanO       V$$r,   stable
descendingc                     dt        |      z  S r  r  )r)   r  r   r  r  s        r*   sortzFreeSymbolsOpsHandler.sortT  r  r,   r5  	src_dtypereduction_typer0  c                 0    t        |      }|dkD  rd|z  S d S )Nr   r&   )r   )r)   r5  r  r  r0  
num_valuess         r*   	reductionzFreeSymbolsOpsHandler.reductionY  s$     +>:
'1A~w#?4?r,   maskbodyr`   c                 6    t        |      sJ d        |        y )Nz$masked body must always be callable.)callable)r)   r  r  r`   s       r*   maskedzFreeSymbolsOpsHandler.maskedc  s    ~EEE~r,   r   )TT)rC   rD   rE   r   rH   rM   rG   rL   r  rF   r   r   rN   r  r	   rO   rI   r  r  r   r  r  rB  r5  r   r  r   r  r'   r,   r*   r  r  5  s   %%Td Td T4S 4c3h 4c3h 4TW 4 	9	9 CO$	9 		9
 	9 
	9s uY/ %%'*%4<SM%	y	%
%%#+C=%:=%KN%	y	%
@{{@ ;;@ &	@
 T5++,@ 
tU9%%	&@3 hsCx&8   r,   r  r"   rindexr#   c                    ddl m} |||gn|g}t        |      }t        j                  |      5  t        j                  |dd      5   | |  d d d        d d d        |j                  S # 1 sw Y   xY w# 1 sw Y   |j                  S xY w)Nr   )FlexibleLayoutallow_indexingT)r~   r  r  r   ra  r   objectr  )r\  r"   r  r#   r  r   handlers          r*   extract_free_symbolsr  i  s     #$0E6?ugD#M2G 	
'"^%5t< 	D		 
 ??  
 ??s#   A6A*A6*A3	/A66B
r@   r   )MrJ   dataclassesrg   loggingrecollections.abcr   r   r   typingr   r   r   r	   typing_extensionsr
   unittest.mockr   rH   rB  torch._inductor.utilsr   %torch.fx.experimental.symbolic_shapesr   r   torch.utils._ordered_setr   utils._sympy.symbolr   r   codegen.commonr   ops_handlerr   utilsr   r   r   r   r   virtualizedr   r   r   	getLoggerrC   rl   compilesearchr   ABCr    	dataclassrR   r   r   r   r   MockHandlerrz   KernelFormatterHandlerrF  rF   r   rI   rM   r   r   rS  rZ  rL   rd  r`  r  r!  r  r  r'   r,   r*   <module>r     s   
    	 8 8 0 0 "    2 U / 3 4 '  * CLg!bjj)00##'' #L d#pI pI $pIf d#0c 0 $0v d#)c ) $)X d#! ! $! J J JZg0AMM g0T8a.. 8	 	i5::,:T1U&U V 	

#-0
4U\\"#Y./ 47

#-0
4$%y01$ .0'c'

#' ' 	'
 $uzz*+' '\ 	--
tEJJ
 - - 	-
 -`@"+@"
8D$%xUZZ0@'AAB@"F 
1N 1n .2	cEJJ Xejj)* 	
 r,   