
    rhs                        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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jV                  e,      Z- ej\                  d      j^                  Z0 G d de jb                        Z2 ejf                  d       G d de2             Z4 ejf                  d       G d de2             Z5 ejf                  d       G d de2             Z6 ejf                  d       G d d             Z7ejf                   G d d             Z8 G d d e)jr                        Z: G d! d"e)jv                        Z<d#e=d$e>e&e
ej~                  gej                  f   f   fd%ZAd&eej~                     d#e=d$e>eBeBej                        e&f   fd'ZCd(d)d&eej~                     d#e=d$e>eBeBej~                        e&f   fd*ZDd+d(d,d-d.e
d/e	f   d&eej~                     d0eEd#e=d1eeBej~                        d$e8fd2ZF	 d@d.e	d3eBeBej~                        d4e&d0eEd$e:f
d5ZGd6d7d$e>eeBej~                        eeBej~                        f   fd8ZHd$e=fd9ZI G d: d;e      ZJ	 	 dAd.e
d/e	f   d<eej~                     d=eeej~                        d>eEd$eej                     f
d?ZKy)B    N)IterableSequence)AnyCallableOptionalTypeVarUnion)Self)patch)free_symbols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     o/var/www/html/ai-insurance-compliance-backend/venv/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(   r6   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/   intr1   boolr3   r5   r8   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())rL   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%   )lenrJ   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   )rV   rU   r!   r   any	itertoolschainrK   r   graphsizevarsstride_hintsrJ   r   logdebug	enumeraterange)r(   rW   self_stridesother_stridesir[   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!   rD   fromkeysrJ   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.
        )rI   r    _RecordLoadStoreInner
_normalizer!   rQ   rL   r.   s    r)   	normalizezMemoryDep.normalize   sC     II
"--djj$++F
 II
 	
r,   r6   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!   rJ   sortedrf   rU   __getitem__same_reorderrK   _simplify_loopsr   var_builderrD   zipr   r@   expandrI   r    tuplekeysvalues)r(   r6   ru   stridesrk   stride_reordersizesrJ   new_reordered_sizesnew_reordered_var_namesnew_simplified_sizesreindex_prune
var_rangesadd_varxreplacement	new_indexouts                      r)   r8   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, ...})rD   r~   rJ   rK   r.   s    r)   rQ   zMemoryDep.ranges   s     C		233r,   c                     t        | j                  t        j                  j                  j                  | j                  | j                        | j                  | j                  | j                        S )N)r    r!   rJ   rK   rL   )rI   r    r   r`   ra   simplify_with_rangesr!   rQ   rJ   rK   rL   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~   rJ   rK   )r(   numelvarsvarrK   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)rJ   rK   rL   )r    rI   r!   rJ   rK   rL   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)   r1   zMemoryDep.numbytes_hint   b    	77##--dnn.>?.!!$)),C   # 		   A)A, ,	A87A8c                 L    t        t        | j                                     dkD  S r   rU   r   r/   r.   s    r)   r3   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SymbolrJ   r.   s    r)   r5   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)
rU   rJ   r   r!   r@   AddargsMulrE   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   rJ   r   rE   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    )rZ   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#   rI   r9   T) r;   r<   r=   r>   r?   r@   rA   r   r   rL   r   rS   propertyrE   rV   listrl   ro   rs   r8   rD   rQ   r   r/   r*   r1   rF   r3   r5   r   r   r   r&   r,   r)   rI   rI   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,   rI   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    NrL   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   rL   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)   r1   zStarDep.numbytes_hint6  r   r   c                 L    t        t        | j                                     dkD  S r   r   r.   s    r)   r3   zStarDep.has_unbacked_symbols>  r   r,   c                      yNFr&   r.   s    r)   r5   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?   rL   r   r   r@   rA   r!   r/   rD   r*   rE   r1   rF   r3   r5   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)   r1   zWeakDep.numbytes_hintf  s    r,   c                      yr   r&   r.   s    r)   r3   zWeakDep.has_unbacked_symbolsi  r   r,   c                      yr   r&   r.   s    r)   r5   zWeakDep.is_contiguousl  r   r,   N)r;   r<   r=   r>   r?   r   r@   rA   r!   r/   rD   r*   rE   r1   rF   r3   r5   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!   .rJ   rK   N)r;   r<   r=   r@   rA   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*   rZ   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(   rW   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   rI   r!   rE   r@   r   addr    )r(   r   namesr   s       r)   buffer_nameszReadWrites.buffer_names  se     ",((* 	$Cc9-'z		C/0 		#((#	$ r,   )rW   r   r#   r   r   )r;   r<   r=   r   r   r?   r   r   r   r   r@   rA   r   r   rD   r>   r*   r	   r   r   staticmethodr   r   r   r   rF   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!   rJ   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@   rA   r   pop)r!   rJ   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_prefixrD   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   rJ   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(rO   rP   )r   r   rI   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   rE   r  r@   r   r  s      r)   	load_seedz_RecordLoadStoreInner.load_seed  s,    %%%%yyu}}U344r,   valuerL   c           	          | j                   j                  t        |g| j                  |      d|i       d| dt	        |       d| d| d	S )NrL   zstore(rO   rP   )r  r   rI   r  r   )r(   r    r!   r  rL   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(rP   )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(rO   rP   )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   rF   r   r   r	   rE   r@   rA   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   r2  r  s       r)   r   zRecordLoadStore.__init__*  s$    .!Y
 	7r,   )r;   r<   r=   r   rF   r   r,  r-  s   @r)   r/  r/  )  s"    89 8 8$ 8 8r,   r/  r6   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)r4  r   cntr6   r   s     r)   r   zvar_builder.<locals>.add_var6  s(    &$s)56
1r,   )r^   countr@   rA   r   )r6   r   r7  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)r6   r9  r   r   rK   r   s         r)   index_vars_no_squeezer<  >  sC     &f-JKS%T4d3w+=&>%TD%T &Us   5d)r6   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;  )
r6   r9  r?  r   r   r   r  rK   new_sizer   s
             r)   index_vars_squeezerC  F  s|      %f-J#%D(*I ;'006'"GDWh!789:; r,   Fr&   )rs   r6   hidden_argsfn.rs   rD  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)Nr6   r   )LoopBody)rs   )rC  	loop_bodyrG  r   extract_loop_body_with_argsr/  r   set_ops_handlerr2  r^   r_   from_iterabler   r   r   r  r  )rE  rs   r6   rD  r9  r   r   rG  innerr   r   s              r)   extract_read_writesrM  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   )MemoryUsageTyper1  )rH  rO  rq   indexing_from_argsindirect_varsre   r   r   TMPr  r   memory_usageLOADr  buffer_name
index_name	LOAD_SEEDr  rE   STOREr  rL   STORE_REDUCTIONr  
INDEX_EXPRr   	BUCKETIZEr(  )rE  r   r   rs   rO  rL  name_to_indexri   r   replr  entrys               r)   rI  rI  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   ra  rb  rc  r   get_defining_opget_sizeget_reduction_sizerU   data	get_readsr   rI   r    r   r   r`   try_get_bufferextend)r_  ra  rb  rc  rK   reduction_sizer   seen	new_readsreadbufferops               r)   #extract_input_node_reduction_rangesrq    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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symbolsunbacked_onlyr#   Nc                 T    t               | _        |rt        | _        y t        | _        y r%   )r   rv  r   r   get_symbols)r(   rw  s     r)   r   zFreeSymbolsOpsHandler.__init__  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%   )r^   r_   r   r   r@   rA   logicboolalgBooleanrv  ry  )r(   r    r   rz  as        r)   _defaultzFreeSymbolsOpsHandler._default  s^    v}}7 	4A!ejj%++*=*=*E*EFG 0 0 33	4r,   	index_varrK   checkwrap_negc                     t        |t        j                  t        j                  j                  j
                  f      rJ | xj                  | j                  |      z  c_        t        dt        |       d      S )N(rP   )
r   r@   rA   r|  r}  r~  rv  ry  r   r>   )r(   r  rK   r  r  s        r)   indirect_indexingz'FreeSymbolsOpsHandler.indirect_indexing  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.frexp  s    r,   dtypes
combine_fnr   c                     dt        |      z  S Nr%   rU   )r(   r  r  r   s       r)   scanzFreeSymbolsOpsHandler.scan       V$$r,   stable
descendingc                     dt        |      z  S r  r  )r(   r  r   r  r  s        r)   sortzFreeSymbolsOpsHandler.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FreeSymbolsOpsHandler.reduction  s$     +>:
'1A~w#?4?r,   maskbodyrW   c                 6    t        |      sJ d        |        y )Nz$masked body must always be callable.)callable)r(   r  r  rW   s       r)   maskedzFreeSymbolsOpsHandler.masked  s    ~EEE~r,   r   )TT)r;   r<   r=   r   r@   r   r?   rF   r   r>   r   r   rD   r  r	   rE   rA   r  r  r   r  r  r+  r  r   r  r   r  r&   r,   r)   ru  ru    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,   ru  r!   rindexrw  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)ru   r  ru  r   rJ  r   objectrv  )rE  r!   r  rw  r  r   handlers          r)   extract_free_symbolsr  %  s     #$0E6?ugD#M2G 	
'"^%5t< 	D		 
 ??  
 ??s#   A6A*A6*A3	/A66B
)Fr   )LrB   dataclassesr^   loggingrecollections.abcr   r   typingr   r   r   r   r	   typing_extensionsr
   unittest.mockr   r@   r+  %torch.fx.experimental.symbolic_shapesr   r   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   	dataclassrI   r   r   r   r   MockHandlerrq   KernelFormatterHandlerr/  r>   r   rA   r   r}   r   r<  rC  rF   rM  rI  rq  r
  ru  r  r&   r,   r)   <module>r     s   
    	 . : : "    U / 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 
1N 1n .2	cEJJ Xejj)* 	
 r,   