
    Vhls                        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 d dlm	Z	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 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$m%Z% d
dl&m'Z'm(Z(  ed      Z) ejT                  e+      Z, ejZ                  d      j\                  Z/ G d de j`                        Z1 ejd                  d       G d de1             Z3 ejd                  d       G d de1             Z4 ejd                  d       G d de1             Z5 ejd                  d       G d d             Z6ejd                   G d d             Z7 G d d e(jp                        Z9 G d! d"e(jt                        Z;d#e<d$e=e%e
ej|                  gej~                  f   f   fd%Z@d&eej|                     d#e<d$e=eAeAej~                        e%f   fd'ZBd(d)d&eej|                     d#e<d$e=eAeAej|                        e%f   fd*ZCd+d(d,d-d.e
d/e	f   d&eej|                     d0eDd#e<d1eeAej|                        d$e7fd2ZE	 d?d.e	d3eAeAej|                        d4e%d0eDd$e9f
d5ZFd6d7d$e=eeAej|                        eeAej|                        f   fd8ZGd$e<fd9ZH G d: d;e      ZI	 d@d.e
d/e	f   d<eej|                     d=eeej|                        d$eej~                     fd>ZJy)A    N)IterableSequence)AnyCallableOptionalTypeVarUnion)Self)patch)free_unbacked_symbols)
OrderedSet   )make_symbolSymT   )index_prevent_reordering)DefaultHandler)get_dtype_sizereduction_num_outputssympy_index_symbol	sympy_str
sympy_subs	VarRanges)ReductionTypeVTzindirect|tmpc                   <   e Zd ZU eed<   ej                  ed<   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dd
ede
fdZy)Depnameindexrenamesreturnc                      y N selfr!   s     L/home/dcms/DCMS/lib/python3.12/site-packages/torch/_inductor/dependencies.pyrenamez
Dep.rename)           c                      y r$   r%   r'   s    r(   	get_numelzDep.get_numel-   r*   r+   c                      y r$   r%   r-   s    r(   numbytes_hintzDep.numbytes_hint1   r*   r+   c                      y r$   r%   r-   s    r(   has_unbacked_symbolszDep.has_unbacked_symbols5   r*   r+   c                      y r$   r%   r-   s    r(   is_contiguouszDep.is_contiguous9   r*   r+   prefixc                     | S r$   r%   )r'   r5   s     r(   normalize_with_stride_orderzDep.normalize_with_stride_order=   s    r+   Nt)__name__
__module____qualname__str__annotations__sympyExprabcabstractmethoddictr
   r)   r.   intr0   boolr2   r4   r7   r%   r+   r(   r   r   %   s    
I::d38n    	5::   	s   	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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dedefdZdefdZdefdZy) 	MemoryDepr   r    .	var_namessizeNmoder"   c                     d}| j                   d| j                    }d| j                  d| j                   d| j                   | dS )N , z
MemoryDep())rK   r   r    ranges)r'   
maybe_modes     r(   __repr__zMemoryDep.__repr__I   sM    
99 dii[)JDII=4::,bj\QRSSr+   c                 ,    t        | j                        S r$   )lenrI   r-   s    r(   num_varszMemoryDep.num_varsO   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#        d| 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>h   s      PAqAvaPs   zaunable to decide loop order. self_dep=%s v.s. other_dep=%s, self_strides=%s v.s. other_strides=%sr   )rU   rT   r    free_symbolsany	itertoolschainrJ   r   graphsizevarsstride_hintsrI   r   logdebug	enumeraterange)r'   rV   self_stridesother_stridesirZ   stride_to_indexorders           r(   decide_loop_order_to_matchz$MemoryDep.decide_loop_order_to_matchS   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-:;#;;% JuQ/F$GGGG	 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    rC   fromkeysrI   r-   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.
        )rH   r   _RecordLoadStoreInner
_normalizer    rP   rK   r-   s    r(   	normalizezMemoryDep.normalize   sC     II
"--djj$++F
 II
 	
r+   r5   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._inductorru   r   r`   ra   rb   r    rI   sortedrf   rT   __getitem__same_reorderrJ   _simplify_loopsr   var_builderrC   zipr   r?   expandrH   r   tuplekeysvalues)r'   r5   ru   stridesrk   stride_reordersizesrI   new_reordered_sizesnew_reordered_var_namesnew_simplified_sizesreindex_prune
var_rangesadd_varxreplacement	new_indexouts                      r(   r7   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, ...})rC   r~   rI   rJ   r-   s    r(   rP   zMemoryDep.ranges   s     C		233r+   c                     t        | j                  t        j                  j                  j                  | j                  | j                        | j                  | j                  | j                        S )N)r   r    rI   rJ   rK   )rH   r   r   r`   ra   simplify_with_rangesr    rP   rI   rJ   rK   r-   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   r`   r.   r   r   r    r\   r?   SOner~   rI   rJ   )r'   numelvarsvarrJ   s        r(   r.   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)rI   rJ   rK   )r   rH   r    rI   rJ   rK   r&   s     r(   r)   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   r`   ra   	size_hintr.   r   	get_dtyper   NotImplementedErrorr-   s    r(   r0   zMemoryDep.numbytes_hint   b    	77##--dnn.>?.!!$)),C   # 		   A)A, ,	A87A8c                 L    t        t        | j                                     dkD  S r   rT   r   r.   r-   s    r(   r2   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    r?   IntegerSymbolrI   r-   s    r(   r4   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)
rT   rI   r   r    r?   AddargsMulrD   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    r?   r   rI   r   rD   r   r-   s    r(   	is_scalarzMemoryDep.is_scalar  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   )rY   vs     r(   r[   z(MemoryDep.is_indirect.<locals>.<genexpr>!  s     H1;qvv&Hs   !)r]   r    r\   r-   s    r(   r   zMemoryDep.is_indirect   s    H

0G0GHHHr+   )r"   rH   r8   T) r:   r;   r<   r=   r>   r?   r@   r   r   rK   r   rR   propertyrD   rU   listrl   ro   rs   r7   rC   rP   r   r.   r)   r0   rE   r2   r4   r   r   r   r%   r+   r(   rH   rH   A   sj   
I::U\\3&''


C
  D(3-T# T ## # #7 7c@S 7rHEJJ H

*# * *X 4U\\5::56 4 4
	5:: 		d38n 	 	s @d @Ut U
-$ -RV -:<4 <
IT Ir+   rH   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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   NrK   r"   c                     t        d      )NzStarDep does not have an indexr   r-   s    r(   r    zStarDep.index*      !"BCCr+   c                 T    t         j                  j                  | j                        S r$   )r   r`   r.   r   r-   s    r(   r.   zStarDep.get_numel.  s    ww  ++r+   r!   c                 h    | j                   |v r#t        || j                      | j                        S | S r$   )r   r   rK   r&   s     r(   r)   zStarDep.rename1  s-    997499-tyy99r+   c                     	 t         j                  j                  j                  | j	                               t        t         j                  j                  | j                              z  S # t        $ r Y yw xY wr   r   r-   s    r(   r0   zStarDep.numbytes_hint6  r   r   c                 L    t        t        | j                                     dkD  S r   r   r-   s    r(   r2   zStarDep.has_unbacked_symbols>  r   r+   c                      yNFr%   r-   s    r(   r4   zStarDep.is_contiguousA      r+   c                      yr   r%   r-   s    r(   r   zStarDep.is_scalarD  r   r+   c                      yr   r%   r-   s    r(   r   zStarDep.is_indirectG  r   r+   )r:   r;   r<   r=   r>   rK   r   r   r?   r@   r    r.   rC   r)   rD   r0   rE   r2   r4   r   r   r%   r+   r(   r   r   $  s    
ID(3- Duzz D D,5:: ,d38n  
s @d @t 4 T r+   r   c                       e Zd ZU eed<   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efdZdefd	Zdefd
Zy)WeakDepr   mutating_bufr"   c                     t        d      )NzWeakDep does not have an indexr   r-   s    r(   r    zWeakDep.indexZ  r   r+   c                 6    t         j                  j                  S r$   )r?   r   r   r-   s    r(   r.   zWeakDep.get_numel^  s    ww{{r+   r!   c                 h    | j                   |v r#t        || j                      | j                        S | S r$   )r   r   r   r&   s     r(   r)   zWeakDep.renamea  s/    997499-t/@/@AAr+   c                      yNr   r%   r-   s    r(   r0   zWeakDep.numbytes_hintf  s    r+   c                      yr   r%   r-   s    r(   r2   zWeakDep.has_unbacked_symbolsi  r   r+   c                      yr   r%   r-   s    r(   r4   zWeakDep.is_contiguousl  r   r+   N)r:   r;   r<   r=   r>   r   r?   r@   r    r.   rC   r)   rD   r0   rE   r2   r4   r%   r+   r(   r   r   S  s~     IDuzz D D5:: d38n  
s 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    .rI   rJ   N)r:   r;   r<   r?   r@   r>   r   r   r%   r+   r(   r   r   p  s3    ::U\\3&''


C
  r+   r   c                   "   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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$   r)   rY   depr!   s     r(   r[   z$ReadWrites.rename.<locals>.<genexpr>  s     Asszz'*A   c              3   @   K   | ]  }|j                          y wr$   r   r   s     r(   r[   z$ReadWrites.rename.<locals>.<genexpr>  s     Bsszz'*Br   )r   r   r   r   r   r   r   r&   s    `r(   r)   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'   rV   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$   )r^   r_   r   r   r-   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   rH   r    rD   r?   r   addr   )r'   r   namesr   s       r(   buffer_nameszReadWrites.buffer_names  se     ",((* 	$Cc9-'z		C/0 		#((#	$ r+   )rV   r   r"   r   r   )r:   r;   r<   r   r   r>   r   r   r   r   r?   r@   r   r   rC   r=   r)   r	   r   r   staticmethodr   r   r   r   rE   r   r%   r+   r(   r   r   w  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 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efdZdededefdZ	 ddede	j                  dedee   def
dZdede	j                  dedefdZde	j                  deej2                     de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 ) rq   r   rs   r"   Nc                     t         |           t               | _        t               | _        t               | _        || _        || _        y r$   )super__init__r   _reads_writes_index_exprs_var_ranges_should_normalize)r'   r   rs   	__class__s      r(   r   z_RecordLoadStoreInner.__init__  s:    '1|.8l6@l&0'0r+   r    rI   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   r?   r@   r\   pop)r    rI   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   r`   ra   r|   r   r}   canonicalization_prefixrC   r~   r   r?   r   r  )clsr    r   
index_varsr   	new_sizesr   r   new_varsr   r   r   s               r(   rr   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   r`   ra   simplifyr~   r   r  r   itemsrr   )r'   r    r   r   kr   rI   r   s           r(   canonicalizez"_RecordLoadStoreInner.canonicalize  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                     | j                   j                  t        |g| j                  |              d| dt	        |       dS )Nzload(rN   rO   )r   r   rH   r  r   r'   r   r    s      r(   loadz_RecordLoadStoreInner.load  sB    	$B):):5)ABCtfBy/022r+   c                 p    t        |t              sJ | j                  |t        j                  |            S r$   )r   rD   r  r?   r   r  s      r(   	load_seedz_RecordLoadStoreInner.load_seed  s,    %%%%yyu}}U344r+   valuerK   c           	          | j                   j                  t        |g| j                  |      d|i       d| dt	        |       d| d| d	S )NrK   zstore(rN   rO   )r   r   rH   r  r   )r'   r   r    r  rK   s        r(   storez_RecordLoadStoreInner.store  sW     	4N$*;*;E*BNNOvR	% 01E7"TF!DDr+   c                 0    | j                  ||d| d      S )Nzstore_reduction(rO   )r  )r'   r   r    r  s       r(   store_reductionz%_RecordLoadStoreInner.store_reduction  s    zz$)9%'BCCr+   dtypec                     | j                   j                  t        | j                  |              dt	        |       d| dS )Nzindex_expr(rN   rO   )r  r   r   r  r   )r'   r    r  s      r(   
index_exprz _RecordLoadStoreInner.index_expr  sA    lD,=,=e,DEFYu-.bq99r+   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   r   r!  r"  r#  r$  r%  s           r(   	bucketizez_RecordLoadStoreInner.bucketize  sA     	
1./KKOOGF1I./ r+   r$   NN)r:   r;   r<   r   rE   r   r   r	   rD   r?   r@   r   r  classmethodr   r   rr   r  r=   r  r  r   r  r  torchr  r  r   r'  __classcell__r  s   @r(   rq   rq     sm   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&3 3UZZ 3C 35c 5# 5# 5
 OSEE %

E36E>FsmE	EDC D

 D3 D3 D:

 :8EKK3H :S : 48&*00 #uzz5::uzzAB0 	0
 0 0 sEJJ/00 !0 
0r+   rq   c                   ,     e Zd Zdededdf fdZ xZS )RecordLoadStorer   rs   r"   Nc                 @    t        ||      }t        | 	  |       y )Nr   rs   )parent_handler)rq   r   r   )r'   r   rs   r1  r  s       r(   r   zRecordLoadStore.__init__*  s$    .!Y
 	7r+   )r:   r;   r<   r   rE   r   r+  r,  s   @r(   r.  r.  )  s"    89 8 8$ 8 8r+   r.  r5   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)r3  r   cntr5   r   s     r(   r   zvar_builder.<locals>.add_var6  s(    &$s)56
1r+   )r^   countr?   r@   r   )r5   r   r6  r   s   ` @@r(   r}   r}   2  s<    
//
CJ

 u|| 
 wr+   argsizesc           	      t    t        |       \  }}|D cg c]  }t        t        ||             }}||fS c c}w r$   )r}   r   map)r5   r8  r   r   rJ   r   s         r(   index_vars_no_squeezer;  >  sC     &f-JKS%T4d3w+=&>%TD%T &Us   5d)r5   c                     ddl m} t        |       \  }}g }g }|D ]Q  }|j                  |      \  }}	|j	                  |       |j	                   |	t        t        ||                         S ||fS )Nr   )SqueezeView)ru   r>  r}   squeezerappendr   r:  )
r5   r8  r>  r   r   r   r  rJ   new_sizer   s
             r(   index_vars_squeezerB  F  s|      %f-J#%D(*I ;'006'"GDWh!789:; r+   Fr%   )rs   r5   hidden_argsfn.rs   rC  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)Nr5   r   )LoopBody)rs   )rB  	loop_bodyrF  r   extract_loop_body_with_argsr.  r   set_ops_handlerr1  r^   r_   from_iterabler   r   r   r   r  )rD  rs   r5   rC  r8  r   r   rF  innerr   r   s              r(   extract_read_writesrL  U  s     *8CFCD*#"h+%$%%z9

 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   )MemoryUsageTyper0  )rG  rN  rq   indexing_from_argsindirect_varsre   r   r   TMPr  r   memory_usageLOADr  buffer_name
index_name	LOAD_SEEDr  rD   STOREr  rK   STORE_REDUCTIONr  
INDEX_EXPRr  	BUCKETIZEr'  )rD  r   r   rs   rN  rK  name_to_indexri   r   replr  entrys               r(   rH  rH  y  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)ru   r`  ra  rb  r   get_defining_opget_sizeget_reduction_sizerT   data	get_readsr   rH   r   r   r   r`   try_get_bufferextend)r^  r`  ra  rb  rJ   reduction_sizer   seen	new_readsreadbufferops               r(   #extract_input_node_reduction_rangesrp    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	    s    r+   c                      e Zd ZU eej
                     ed<   d"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)$FreeUnbackedSymbolsOpsHandlersymbolsr"   Nc                 "    t               | _        y r$   )r   ru  r-   s    r(   r   z&FreeUnbackedSymbolsOpsHandler.__init__  s    !|r+   r   r   .kwargsc                    t        j                  ||j                               D ]_  }t        |t        j
                  t        j                  j                  j                  f      sB| xj                  t        |      z  c_	        a y r$   )r^   r_   r   r   r?   r@   logicboolalgBooleanru  r   )r'   r   r   rw  as        r(   _defaultz&FreeUnbackedSymbolsOpsHandler._default  sY    v}}7 	9A!ejj%++*=*=*E*EFG 5a 88	9r+   	index_varrJ   checkwrap_negc                     t        |t        j                  t        j                  j                  j
                  f      rJ | xj                  t        |      z  c_        t        dt        |       d      S )N(rO   )
r   r?   r@   ry  rz  r{  ru  r   r   r=   )r'   r~  rJ   r  r  s        r(   indirect_indexingz/FreeUnbackedSymbolsOpsHandler.indirect_indexing  sZ     i%**ekk6I6I6Q6Q)RSSS-d33!Ac)n%5Q"788r+   r   )N.c                      y)Nr(  r%   )r'   r   s     r(   frexpz#FreeUnbackedSymbolsOpsHandler.frexp  s    r+   dtypes
combine_fnr   c                     dt        |      z  S Nr$   rT   )r'   r  r  r   s       r(   scanz"FreeUnbackedSymbolsOpsHandler.scan
       V$$r+   stable
descendingc                     dt        |      z  S r  r  )r'   r  r   r  r  s        r(   sortz"FreeUnbackedSymbolsOpsHandler.sort  r  r+   r  	src_dtypereduction_typer  c                 0    t        |      }|dkD  rd|z  S d S )Nr   r$   )r   )r'   r  r  r  r  
num_valuess         r(   	reductionz'FreeUnbackedSymbolsOpsHandler.reduction  s$     +>:
'1A~w#?4?r+   maskbodyrV   c                 6    t        |      sJ d        |        y )Nz$masked body must always be callable.)callable)r'   r  r  rV   s       r(   maskedz$FreeUnbackedSymbolsOpsHandler.masked  s    ~EEE~r+   )r"   N)TT)r:   r;   r<   r   r?   r   r>   r   r=   r   r   rC   r}  r	   rD   r@   rE   r  r  r   r  r  r*  r  r   r  r   r  r%   r+   r(   rt  rt    s   %%$9S 9c3h 9c3h 9TW 9 	9	9 CO$	9 		9
 	9 
	9s uY/ %%'*%4<SM%	y	%
%%#+C=%:=%KN%	y	%
@{{@ ;;@ &	@
 T5++,@ 
tU9%%	&@3 hsCx&8   r+   rt  r    rindexc                    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)ru   r  rt  r   rI  r   objectru  )rD  r    r  r  r   handlers         r(   extract_free_unbacked_symbolsr  $  s    
 #$0E6?ugD+-G 	
'"^%5t< 	D		 
 ??  
 ??s#   A5A)A5)A2	.A55B	)Fr$   )KrA   dataclassesr^   loggingrecollections.abcr   r   typingr   r   r   r   r	   typing_extensionsr
   unittest.mockr   r?   r*  %torch.fx.experimental.symbolic_shapesr   torch.utils._ordered_setr   utils._sympy.symbolr   r   codegen.commonr   ops_handlerr   utilsr   r   r   r   r   r   virtualizedr   r   r   	getLoggerr:   rc   compilesearchr   ABCr   	dataclassrH   r   r   r   r   MockHandlerrq   KernelFormatterHandlerr.  r=   r   r@   r   r}   r   r;  rB  rE   rL  rH  rp  r	  rt  r  r%   r+   r(   <module>r     s   
    	 . : : "    G / 3 4 '  * CLg!bjj)00#'' 8 d#_I _I $_ID d##c # $#\ d#c  $8 d#! ! $! A A AHj0AMM j0Z8a.. 8	 	i5::,:T1U&U V 	

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

#-0
4UZZ !9,-$ .0!c!

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