
    shޏ                    p   S SK Jr  S SKrS SKrS SKrS SKrS SKJr  S SKJ	r	J
r
JrJrJrJrJrJr  S SKJr  S SKrS SKrS SKJs  Jr  SSKJr  SS	KJrJrJrJr  \" S
5      r\\S      r \S   r!S(S jr" " S S\\   5      r#\RH                  " S5      RJ                  r&S)S jr'\'" \#5      r( " S S\#\	   5      r)\)RU                  5          " S S\)5      r+ " S S5      r, " S S\,\)5      r- " S S\)5      r. " S S\)5      r/ " S S\/5      r0 " S  S!\5      r1 " S" S#\)5      r2 " S$ S%\+5      r3 " S& S'\/5      r4g)*    )annotationsN)StringIO)AnyCallableGenericLiteral
NamedTupleOptionalTypeVarUnion)patch   )
OrderedSet   )IndentedBufferreduction_num_outputssympy_index_symbol	sympy_strT
atomic_add)
argmaxargminwelford_reducewelford_combineanymaxminprodsumxor_sumc                l    [        U [        R                  5      (       a  [        U 5      $ [	        U 5      $ N)
isinstancesympyExprr   stras    o/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/_inductor/ops_handler.py_arg_strr*   %   s&    !UZZ  |q6M    c                     \ rS rSrSrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jr  S         SS jjrSS jrSS jrSS jrSS jrSS jrSS jr S       SS jjrSS jr S         SS jjr          SS jrSS jr        SS jr          SS jr  S               SS jjrSS jrSS jrSS jrSS jrSS  jr SS! jr!SS" jr"SS# jr#SS$ jr$SS% jr%SS& jr&SS' jr'SS( jr(SS) jr)SS* jr*SS+ jr+SS, jr,SS- jr-SS. jr.SS/ jr/SS0 jr0SS1 jr1SS2 jr2SS3 jr3SS4 jr4SS5 jr5SS6 jr6SS7 jr7SS8 jr8SS9 jr9SS: jr:SS; jr;SS< jr<SS= jr=SS> jr>SS? jr?SS@ jr@SSA jrASSB jrBSSC jrCSSD jrDSSE jrESSF jrFSSG jrGSSH jrHSSI jrISSJ jrJSSK jrKSSL jrLSSM jrMSSN jrNSSO jrOSSP jrPSSQ jrQSSR jrRSSS jrSSST jrTSSU jrUSSV jrVSSW jrWSSX jrXSSY jrYSSZ jrZSS[ jr[SS\ jr\SS] jr]SS^ jr^SS_ jr_SS` jr`SSa jraSSb jrbSSc jrcSSd jrdSSe jreSSf jrfSSg jrgSSh jrhSSi jriSSj jrjSSk jrkSSl jrlSSm jrmSSn jrnSSo jroSSp jrpSSq jrqSSr jrrSSs jrsSSt jrtSSu jruSSv jrvSSw jrwSSx jrxSSy jrySSz jrzSS{ jr{SS| jr|SS} jr}SS~ jr~SS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jr          SS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrS\GR8                  SSS.             SS jjrSS jrSS jrSrg)
OpsHandler,   a  
Protocol describing the set of valid operations on ``torch._inductor.virtualized.ops``,
as well as the contract for op handlers.  The type T signifies the domain
of the abstract analysis AKA what all the functions return / take as arguments
anywhere compute occurs.

While these operators are typically dtype polymorphic (e.g., you can use mul
on both integers and floats), they do NOT do promotion and usually return the
same dtype as the input.  You are expected to have handled type promotion
during ATen decompositions.  Most operators correspond exactly to pointwise
operations as defined by torch, so when in doubt about semantics, check the
corresponding torch documentation.  These are all scalar operations (so they
are defined to operate on a single element at a time.)

For convenience, many operators take a src_dtype which indicates what the dtype
of the input argument is.  Although in principle this can be derived by an
analysis, providing this for ops where it is useful helps avoid having to repeatedly
recompute dtype in code generation.

Note that this often describes a class of static methods, for stateless
ops handlers.

Handlers are often defined using metaprogramming (e.g. _initialize_pointwise_overrides),
which means you will not get type errors for those methods.  We have tests in
test/inductor/test_op_completeness.py which check that all operators are implemented after
all the metaprogramming has run.
c                    [         e)z)Produces a scalar constant of type dtype.NotImplementedError)selfvaluedtypes      r)   constantOpsHandler.constantI       !!r+   c                    [         e)z$Computes inductor_prims.lookup_seed.r0   )r2   nameoffsets      r)   	load_seedOpsHandler.load_seedM   r7   r+   c                    [         e)zIComputes inductor_prims.random with mode="rand".  offset has dtype int32.r0   r2   seedr:   s      r)   randOpsHandler.randQ   r7   r+   c                    [         e)zJComputes inductor_prims.random with mode="randn".  offset has dtype int32.r0   r>   s      r)   randnOpsHandler.randnU   r7   r+   c                    [         e)z9Computes inductor_prims.randint.  offset has dtype int32.r0   )r2   r?   r:   lowhighs        r)   	randint64OpsHandler.randint64Y   r7   r+   c                    [         e)a  
Computes body, but only perform loads/stores if the boolean mask
evaluates to true.  For example, you would use this if you needed to
perform an indirect load that may not be valid on some elements;
without masking, invalid accesses can cause IMAs.  When mask is true,
the result is the result of body; otherwise it is other. Here, `other`
needs to be a constant.

Contrast this with ops.where, which can multiplex between two values
that have been unconditionally computed.
r0   )r2   maskbodyothers       r)   maskedOpsHandler.masked]   
     "!r+   c                    [         e)zU
Computes torch.where: when condition is true, return input; otherwise return other.
r0   )r2   	conditioninputrM   s       r)   whereOpsHandler.wherek   
     "!r+   c                    [         e)z
Converts a sympy expression into a scalar of type dtype.  expr is typically
an indexing expression, thus the name; however, it can also be used in
non-indexing situations.
r0   )r2   exprr4   s      r)   
index_exprOpsHandler.index_exprq   s
     "!r+   NTc                    [         e)z
Convert x to dtype.  src_dtype can be optionally set to specify what the original
dtype of x was, which can improve code generation (used by torch to(dtype=dtype)).
r0   )r2   xr4   	src_dtypeuse_compute_typess        r)   to_dtypeOpsHandler.to_dtypey   
     "!r+   c                    [         e)a  
Convert x to dtype with truncation semantics (similar to how the int
constructor works in Python).  In Inductor codegen, this just decays
to trunc and then to_dtype, but this composite operation helps
roundtrips for Sympy evaluation.

dtype is taken as an explicit parameter because the desired output
dtype is typically the index dtype, which may vary between int32 and
int64 depending on if we've shown that all the indexing operations can
be done in int32.
r0   r2   r\   r4   s      r)   trunc_to_intOpsHandler.trunc_to_int   rP   r+   c                    [         ezD
Convert x to dtype with ceiling semantics.  See also trunc_to_int.
r0   rc   s      r)   ceil_to_intOpsHandler.ceil_to_int   rV   r+   c                    [         erg   r0   rc   s      r)   floor_to_intOpsHandler.floor_to_int   rV   r+   c                    [         e)zJ
Convert x to dtype with round-to-even semantics.  See also trunc_to_int.
r0   rc   s      r)   round_to_intOpsHandler.round_to_int   rV   r+   c                    [         e)z}
Reinterpret cast x to dtype (reinterpreting the bits in memory as another dtype.)
src_dtype must be the original type of x.
r0   )r2   r\   r4   r]   s       r)   to_dtype_bitcastOpsHandler.to_dtype_bitcast   
    
 "!r+   c                    [         e)z0
Returns x as is.  This is used to trigger CSE.
r0   r2   r\   s     r)   identityOpsHandler.identity   rV   r+   c                    [         e)au  
Convert an integral x into a sympy.Expr that can be subsequently used in
indexing computation.  'size' represents an upper bound on what valid
indexes can be; when 'check' is True, we check that the x is in bounds.

NB: This is typically mandatory to implement for any analysis, because you
MUST return a valid sympy.Expr of some sort (even if it's a meaningless symbol).
r0   )r2   r\   sizecheckwrap_negs        r)   indirect_indexingOpsHandler.indirect_indexing   ra   r+   c                    [         e)zS
Load from the memory location 'name', offset by some indexing expression 'index'.
r0   )r2   r9   indexs      r)   loadOpsHandler.load   rV   r+   c                    [         e)z
Store 'value' to the memory location 'name' offset by 'expr'.  If
specified, 'mode' can require the store to be an atomic addition.
r0   )r2   r9   r   r3   modes        r)   storeOpsHandler.store   ra   r+   c                    [         e)a  
Perform a 'reduction_type' reduction on 'value' of dtype 'src_dtype',
using 'dtype' as the accumulation dtype for the reduction.  The result
is an intermediate computation which should be stored to the final
location using 'ops.store_reduction'.

Valid reduction types are .  For Welford reduction types, this
function returns multiple outputs; consult reduction_num_outputs to
determine the amount in metaprogramming applications.
r0   )r2   r4   r]   reduction_typer3   s        r)   	reductionOpsHandler.reduction   s
    " "!r+   c                    [         e)zc
Store the fully accumulated result of 'reduction' to the memory
location 'name' offset by 'expr'.
r0   )r2   r9   r   r3   s       r)   store_reductionOpsHandler.store_reduction   rs   r+   c                    [         e)z)
Perform an associative scan on 'value'.
r0   )r2   dtypes
combine_fnvaluess       r)   scanOpsHandler.scan   
     "!r+   c                    [         e)z,
Sort values along the reduction dimension.
r0   )r2   r   r   stable
descendings        r)   sortOpsHandler.sort  r   r+   c                    [         er"   r0   )r2   r   
boundariesboundary_indicesindexing_dtyperightsortersorter_indicess           r)   	bucketizeOpsHandler.bucketize  ra   r+   c                    [         er"   r0   r2   x0s     r)   absOpsHandler.abs%      !!r+   c                    [         er"   r0   r   s     r)   expOpsHandler.exp(  r   r+   c                    [         er"   r0   r   s     r)   exp2OpsHandler.exp2+  r   r+   c                    [         er"   r0   r   s     r)   expm1OpsHandler.expm1.  r   r+   c                    [         er"   r0   r   s     r)   sqrtOpsHandler.sqrt1  r   r+   c                    [         er"   r0   r   s     r)   reluOpsHandler.relu4  r   r+   c                    [         er"   r0   r2   r   x1s      r)   minimumOpsHandler.minimum7  r   r+   c                    [         er"   r0   r   s      r)   maximumOpsHandler.maximum:  r   r+   c                    [         er"   r0   r   s     r)   cosOpsHandler.cos=  r   r+   c                    [         er"   r0   r   s     r)   sinOpsHandler.sin@  r   r+   c                    [         er"   r0   r   s     r)   lgammaOpsHandler.lgammaC  r   r+   c                    [         er"   r0   r   s     r)   erfOpsHandler.erfF  r   r+   c                    [         er"   r0   r   s     r)   coshOpsHandler.coshI  r   r+   c                    [         er"   r0   r   s     r)   sinhOpsHandler.sinhL  r   r+   c                    [         er"   r0   r   s     r)   acosOpsHandler.acosO  r   r+   c                    [         er"   r0   r   s     r)   acoshOpsHandler.acoshR  r   r+   c                    [         er"   r0   r   s     r)   asinOpsHandler.asinU  r   r+   c                    [         er"   r0   r   s     r)   asinhOpsHandler.asinhX  r   r+   c                    [         er"   r0   r   s      r)   atan2OpsHandler.atan2[  r   r+   c                    [         er"   r0   r   s     r)   atanOpsHandler.atan^  r   r+   c                    [         er"   r0   r   s     r)   atanhOpsHandler.atanha  r   r+   c                    [         er"   r0   r   s      r)   copysignOpsHandler.copysignd  r   r+   c                    [         er"   r0   r   s     r)   erfcOpsHandler.erfcg  r   r+   c                    [         er"   r0   r   s     r)   erfinvOpsHandler.erfinvj  r   r+   c                    [         er"   r0   r   s     r)   frexpOpsHandler.frexpm  r   r+   c                    [         er"   r0   r   s      r)   hypotOpsHandler.hypotp  r   r+   c                    [         er"   r0   r   s     r)   log10OpsHandler.log10s  r   r+   c                    [         er"   r0   r   s     r)   log2OpsHandler.log2v  r   r+   c                    [         er"   r0   r   s      r)   	nextafterOpsHandler.nextaftery  r   r+   c                    [         er"   r0   r   s      r)   logical_andOpsHandler.logical_and|  r   r+   c                    [         er"   r0   r   s     r)   logical_notOpsHandler.logical_not  r   r+   c                    [         er"   r0   r   s      r)   
logical_orOpsHandler.logical_or  r   r+   c                    [         er"   r0   r   s      r)   logical_xorOpsHandler.logical_xor  r   r+   c                    [         er"   r0   r   s      r)   bitwise_andOpsHandler.bitwise_and  r   r+   c                    [         er"   r0   r   s     r)   bitwise_notOpsHandler.bitwise_not  r   r+   c                    [         er"   r0   r   s      r)   
bitwise_orOpsHandler.bitwise_or  r   r+   c                    [         er"   r0   r   s      r)   bitwise_xorOpsHandler.bitwise_xor  r   r+   c                    [         er"   r0   r   s      r)   bitwise_left_shiftOpsHandler.bitwise_left_shift  r   r+   c                    [         er"   r0   r   s      r)   bitwise_right_shiftOpsHandler.bitwise_right_shift  r   r+   c                    [         er"   r0   r   s     r)   rsqrtOpsHandler.rsqrt  r   r+   c                    [         er"   r0   r   s     r)   log1pOpsHandler.log1p  r   r+   c                    [         er"   r0   r   s     r)   tanOpsHandler.tan  r   r+   c                    [         er"   r0   r   s     r)   tanhOpsHandler.tanh  r   r+   c                    [         er"   r0   r   s     r)   sigmoidOpsHandler.sigmoid  r   r+   c                    [         er"   r0   r   s     r)   signbitOpsHandler.signbit  r   r+   c                    [         er"   r0   r   s      r)   fmodOpsHandler.fmod  r   r+   c                    [         er"   r0   r   s     r)   logOpsHandler.log  r   r+   c                    [         er"   r0   r   s     r)   isinfOpsHandler.isinf  r   r+   c                    [         er"   r0   r   s     r)   isnanOpsHandler.isnan  r   r+   c                    [         er"   r0   r   s     r)   roundOpsHandler.round  r   r+   c                    [         er"   r0   r   s     r)   floorOpsHandler.floor  r   r+   c                    [         er"   r0   r   s     r)   signOpsHandler.sign  r   r+   c                    [         er"   r0   r   s     r)   truncOpsHandler.trunc  r   r+   c                    [         er"   r0   r   s     r)   ceilOpsHandler.ceil  r   r+   c                    [         er"   r0   r   s     r)   negOpsHandler.neg  r   r+   c                    [         er"   r0   r   s     r)   
reciprocalOpsHandler.reciprocal  r   r+   c                    [         er"   r0   r   s      r)   eqOpsHandler.eq  r   r+   c                    [         er"   r0   r   s      r)   neOpsHandler.ne  r   r+   c                    [         er"   r0   r   s      r)   ltOpsHandler.lt  r   r+   c                    [         er"   r0   r   s      r)   gtOpsHandler.gt  r   r+   c                    [         er"   r0   r   s      r)   leOpsHandler.le  r   r+   c                    [         er"   r0   r   s      r)   geOpsHandler.ge  r   r+   c                    [         er"   r0   r   s      r)   addOpsHandler.add  r   r+   c                    [         er"   r0   r   s      r)   subOpsHandler.sub  r   r+   c                    [         er"   r0   r   s      r)   mulOpsHandler.mul  r   r+   c                    [         er"   r0   r   s      r)   powOpsHandler.pow  r   r+   c                    [         er"   r0   r   s      r)   and_OpsHandler.and_  r   r+   c                    [         er"   r0   r   s      r)   or_OpsHandler.or_  r   r+   c                    [         er"   r0   r   s      r)   xorOpsHandler.xor  r   r+   c                    [         er"   r0   r   s      r)   lshiftOpsHandler.lshift  r   r+   c                    [         er"   r0   r   s      r)   rshiftOpsHandler.rshift  r   r+   c                    [         er"   r0   ru   s     r)   airy_aiOpsHandler.airy_ai  r   r+   c                    [         er"   r0   ru   s     r)   	bessel_j0OpsHandler.bessel_j0	  r   r+   c                    [         er"   r0   ru   s     r)   	bessel_j1OpsHandler.bessel_j1  r   r+   c                    [         er"   r0   ru   s     r)   	bessel_y0OpsHandler.bessel_y0  r   r+   c                    [         er"   r0   ru   s     r)   	bessel_y1OpsHandler.bessel_y1  r   r+   c                    [         er"   r0   ru   s     r)   digammaOpsHandler.digamma  r   r+   c                    [         er"   r0   ru   s     r)   erfcxOpsHandler.erfcx  r   r+   c                    [         er"   r0   )r2   r\   yzs       r)   fmaOpsHandler.fma  r   r+   c                    [         er"   r0   r2   r\   r  s      r)   igammaOpsHandler.igamma  r   r+   c                    [         er"   r0   r  s      r)   igammacOpsHandler.igammac!  r   r+   c                    [         er"   r0   r  s      r)   gammaincOpsHandler.gammainc$  r   r+   c                    [         er"   r0   r  s      r)   	gammainccOpsHandler.gammaincc'  r   r+   c                    [         er"   r0   ru   s     r)   i0OpsHandler.i0*  r   r+   c                    [         er"   r0   ru   s     r)   i0eOpsHandler.i0e-  r   r+   c                    [         er"   r0   ru   s     r)   i1OpsHandler.i10  r   r+   c                    [         er"   r0   ru   s     r)   i1eOpsHandler.i1e3  r   r+   c                    [         er"   r0   ru   s     r)   log_ndtrOpsHandler.log_ndtr6  r   r+   c                    [         er"   r0   ru   s     r)   modified_bessel_i0OpsHandler.modified_bessel_i09  r   r+   c                    [         er"   r0   ru   s     r)   modified_bessel_i1OpsHandler.modified_bessel_i1<  r   r+   c                    [         er"   r0   ru   s     r)   modified_bessel_k0OpsHandler.modified_bessel_k0?  r   r+   c                    [         er"   r0   ru   s     r)   modified_bessel_k1OpsHandler.modified_bessel_k1B  r   r+   c                    [         er"   r0   ru   s     r)   ndtrOpsHandler.ndtrE  r   r+   c                    [         er"   r0   ru   s     r)   ndtriOpsHandler.ndtriH  r   r+   c                    [         er"   r0   r  s      r)   	polygammaOpsHandler.polygammaK  r   r+   c                    [         er"   r0   ru   s     r)   scaled_modified_bessel_k0$OpsHandler.scaled_modified_bessel_k0N  r   r+   c                    [         er"   r0   ru   s     r)   scaled_modified_bessel_k1$OpsHandler.scaled_modified_bessel_k1Q  r   r+   c                    [         er"   r0   ru   s     r)   spherical_bessel_j0OpsHandler.spherical_bessel_j0T  r   r+   c                    [         er"   r0   r  s      r)   zetaOpsHandler.zetaW  r   r+   c                    [         er"   r0   r  s      r)   chebyshev_polynomial_t!OpsHandler.chebyshev_polynomial_tZ  r   r+   c                    [         er"   r0   r  s      r)   chebyshev_polynomial_u!OpsHandler.chebyshev_polynomial_u]  r   r+   c                    [         er"   r0   r  s      r)   chebyshev_polynomial_v!OpsHandler.chebyshev_polynomial_v`  r   r+   c                    [         er"   r0   r  s      r)   chebyshev_polynomial_w!OpsHandler.chebyshev_polynomial_wc  r   r+   c                    [         er"   r0   r  s      r)   legendre_polynomial_p OpsHandler.legendre_polynomial_pf  r   r+   c                    [         er"   r0   r  s      r)   shifted_chebyshev_polynomial_t)OpsHandler.shifted_chebyshev_polynomial_ti  r   r+   c                    [         er"   r0   r  s      r)   shifted_chebyshev_polynomial_u)OpsHandler.shifted_chebyshev_polynomial_ul  r   r+   c                    [         er"   r0   r  s      r)   shifted_chebyshev_polynomial_v)OpsHandler.shifted_chebyshev_polynomial_vo  r   r+   c                    [         er"   r0   r  s      r)   shifted_chebyshev_polynomial_w)OpsHandler.shifted_chebyshev_polynomial_wr  r   r+   c                    [         er"   r0   r  s      r)   hermite_polynomial_hOpsHandler.hermite_polynomial_hu  r   r+   c                    [         er"   r0   r  s      r)   hermite_polynomial_he OpsHandler.hermite_polynomial_hex  r   r+   c                    [         er"   r0   r  s      r)   laguerre_polynomial_l OpsHandler.laguerre_polynomial_l{  r   r+   c                    [         e)zxC-style trunc division between integers only.  Computes the true
division of two numbers and rounds the result to zero.
r0   r   s      r)   truncdivOpsHandler.truncdiv  rV   r+   c                    [         e)zPython-style floor division between integers only.  Computes the
true division of two numbers and floors the result.  If you want
floor division for floats, do regular truediv and floor the result.
r0   r   s      r)   floordivOpsHandler.floordiv  rs   r+   c                    [         e)z~True division between floats.  Integer inputs are NOT valid.  To
do Python-style (int, int) -> float division, use int_truedivr0   r   s      r)   truedivOpsHandler.truediv  
     "!r+   c                    [         e)zTrue division between integers.  This is NOT the same as promoting
to float and doing integer division, there is a bespoke algorithm for
doing the division in higher precision than the above.
r0   r   s      r)   int_truedivOpsHandler.int_truediv  rs   r+   c                    [         e)z)C-style modulus, take sign from LHS (x0).r0   r   s      r)   modOpsHandler.mod  r7   r+   c                    [         e)z.Python-style modulus, take sign from RHS (x1).r0   r   s      r)   	remainderOpsHandler.remainder  r7   r+   c                    [         er"   r0   r   s     r)   squareOpsHandler.square  r   r+   c                    [         er"   r0   )r2   rX   ry   loweruppers        r)   check_boundsOpsHandler.check_bounds  r   r+   c                    [         er"   r0   r   s     r)   libdevice_absOpsHandler.libdevice_abs  r   r+   c                    [         er"   r0   r   s     r)   libdevice_expOpsHandler.libdevice_exp  r   r+   c                    [         er"   r0   r   s     r)   libdevice_sqrtOpsHandler.libdevice_sqrt  r   r+   c                    [         er"   r0   r   s     r)   libdevice_cosOpsHandler.libdevice_cos  r   r+   c                    [         er"   r0   r   s     r)   libdevice_sinOpsHandler.libdevice_sin  r   r+   c                    [         er"   r0   r   s     r)   libdevice_sigmoidOpsHandler.libdevice_sigmoid  r   r+   c                    [         er"   r0   r   s     r)   libdevice_logOpsHandler.libdevice_log  r   r+   c                    [         er"   r0   )r2   r3   ry   rz   s       r)   halide_clampOpsHandler.halide_clamp  r   r+   r   )constraintsr4   is_purepackc                   [         er"   r0   )r2   asmr*  r4   r+  r,  inputss          r)   inline_asm_elementwise!OpsHandler.inline_asm_elementwise  s
     "!r+   c                    [         ez2This is a fake op used in analysis but not codegenr0   )r2   argss     r)   outputOpsHandler.output  r7   r+   c                    [         er3  r0   )r2   r   s     r)   placeholderOpsHandler.placeholder  r7   r+    )r3   zUnion[bool, float, int]r4   torch.dtypereturnr   )r9   r&   r:   r   r<  r   )r?   r   r:   r   r<  r   )
r?   r   r:   r   rF   r   rG   r   r<  r   )rK   r   rL   zCallable[[], T]rM   r   r<  r   )rR   r   rS   r   rM   r   r<  r   )rX   
sympy.Exprr4   r;  r<  r   )NT)
r\   r   r4   r;  r]   zOptional[torch.dtype]r^   boolr<  r   )r\   r   r4   r;  r<  r   )r\   r   r4   r;  r]   r;  r<  r   )r\   r   r<  r   TT)r\   r   ry   r=  rz   r>  r<  r=  )r9   r&   r   r=  r<  r   r"   )
r9   r&   r   r=  r3   r   r   	StoreModer<  None)
r4   r;  r]   r;  r   ReductionTyper3   r   r<  zUnion[T, tuple[T, ...]])r9   r&   r   r=  r3   r   r<  rA  )r   tuple[torch.dtype, ...]r   z7Callable[[tuple[T, ...], tuple[T, ...]], tuple[T, ...]]r   tuple[T, ...]r<  rD  )
r   rC  r   rD  r   r>  r   r>  r<  rD  NNr   r   r   z.tuple[str, sympy.Expr, sympy.Expr, sympy.Expr]r   r   r   r;  r   r>  r   z Optional[tuple[str, sympy.Expr]]r   zOptional[T]r<  r   )r   r   r<  r   )r   r   r   r   r<  r   )r   r   )r\   r   r  r   r  r   r<  r   )r\   r   r  r   r<  r   )
rX   r=  ry   r=  r  r>  r  r>  r<  rA  )r3   r   ry   r=  rz   r>  r<  r   )r/  r   r.  r&   r*  zOptional[str]r4   r;  r+  r>  r,  intr<  r   )r4  r   r<  rA  )r   rG  r<  r   )__name__
__module____qualname____firstlineno____doc__r5   r;   r@   rC   rH   rN   rT   rY   r_   rd   rh   rk   rn   rq   rv   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r  r  r   r#  r&  r)  r,  r/  r2  r5  r8  r;  r>  rA  rD  rG  rJ  rM  rP  rS  rV  rY  r\  r_  rb  re  rh  rk  rn  rq  rt  rw  rz  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r"  r%  r(  torchfloat32r0  r5  r8  __static_attributes__r:  r+   r)   r-   r-   ,   s   8"""""""" ,0"&"" " )	"
  " 
"""""""  DH""$"-1"	"" "" " 	"
 " 
" "" " &	"
 " 
!","
"'
" L
" 	
"
 

"
"'
" 
" 	
"
 
" 

"& 48&*"" C" 	"
 $" " 1" $" 
"""""""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""&0"9="FJ"	"$"""""""" &*"]]	"	" 	" #		"
 	" 	" 	" 
	"""r+   r-   z	_.*|parenc                z    [        [        U 5       Vs/ s H  n[        U5      (       a  M  UPM     sn5      $ s  snf r"   )r   dir_ignore_op_re)clsr\   s     r)   list_opsrT    s*    #c(C(Q-2Bq(CDDCs   88c                  L    \ rS rSrSS jrS	S jr\S
S j5       r\S 5       r	Sr
g)DefaultHandleri  c                    [         e)a  
Default implementation for all ops.  Override in a subclass to
provide generic op behavior.

Args:
    name: name of the op, see OpHandler.{name}
    args: positional args passed to the op
    kwargs: keyword args passed to the op

Returns:
    return value of the op

r0   r2   r9   r4  kwargss       r)   _defaultDefaultHandler._default  s
     "!r+   c                P   ^ ^ SUU 4S jjn[         R                  " ST S35        U$ )Nc                 (   > TR                  TX5      $ r"   rZ  )r4  rY  r9   r2   s     r)   fallback,DefaultHandler.__getattr__.<locals>.fallback  s    ==t44r+   zundefined OpHandler.z, please add missing op schema)r4  r   rY  r   r<  r   )warningswarn)r2   r9   r_  s   `` r)   __getattr__DefaultHandler.__getattr__  s+    	5 	5 	,TF2PQRr+   c                "   ^  U 4S jnT Ul         U$ )Nc                (   > U R                  TX5      $ r"   r^  )r2   r4  rY  targets      r)   call_default2DefaultHandler._call_default.<locals>.call_default  s    ==66r+   )rH  )rg  rh  s   ` r)   _call_defaultDefaultHandler._call_default
  s    	7 !'r+   c                   [        5       n[         H  n[        R                  " [	        [
        U5      5      n[        S UR                  R                  5        5       5      (       a  UR                  R                  5       tpEUS:X  d   eUR                  SU SSR                  U5       SU< SSR                  U5       S3	R                  5       5        UR                  S	5        M  [        XU R                  U5      5        M     0 n[        UR!                  5       U5        UR#                  5        H  u  p'U[        ;   d  M  [        XU5        M     g
)z
Here we codegen many functions of the form:

    def add(self, a, b):
        return self._default('add', (a, b), {})

and install them in cls.  This is the same as _call_default above,
but is about 1.2x faster since CPython varargs parsing is slow.
c              3     #    U  HY  nUR                   [        R                  R                  :H  =(       a&    UR                  [        R                  R
                  L v   M[     g 7fr"   )kindinspect	ParameterPOSITIONAL_OR_KEYWORDdefaultempty).0ps     r)   	<genexpr>+DefaultHandler._init_cls.<locals>.<genexpr>   sP       1A '++AAA 9II!2!2!8!8890s   A!A#r2   z
                    def z(self, , z0):
                        return self._default(z, (z, ), {})
                    z

N)r   OP_NAMESro  	signaturegetattrr-   all
parametersr   keyswritejoinstripsetattrrj  execgetvalueitems)rS  coderg  sigself_argr4  ctximpls           r)   	_init_clsDefaultHandler._init_cls  s+    zF##GJ$?@C  ..0  
 #&.."5"5"76)))

		$'8 9..4Zs499T?:K L 	 

6" S%6%6v%>?% ( !T]]_c"IIKLF!T* (r+   r:  Nr9   r&   r4  ztuple[Any, ...]rY  zdict[str, Any]r<  r   )r9   r&   r<  r   )rg  r&   )rH  rI  rJ  rK  rZ  rc  staticmethodrj  classmethodr  rO  r:  r+   r)   rV  rV    s4    "    #+ #+r+   rV  c                      \ rS rSrS rS
S jr\SS j5       r\SS j5       r\SS j5       r	\SS j5       r
\SSS jj5       rSrg	)NoopHandleri<  c                    g r"   r:  rX  s       r)   rZ  NoopHandler._default?  s    r+   c                    g r"   r:  rK   rL   rM   s      r)   rN   NoopHandler.maskedB  s    r+   c                    g)NrE  r:  r\   s    r)   r   NoopHandler.frexpF  s    r+   c                    S[        U5      -  $ Nr"   lenr   r   r   s      r)   r   NoopHandler.scanJ      V$$r+   c                    S[        U5      -  $ r  r  r   r   r   r   s       r)   r   NoopHandler.sortN  r  r+   c                6    [         R                  R                  $ r"   )r$   SZero	index_varry   rz   r{   s       r)   r|   NoopHandler.indirect_indexingR  s    ww||r+   r:  Nr  r<  rA  )r<  ztuple[None, None])r<  ztuple[None, ...]r?  r<  zsympy.SymbolrH  rI  rJ  rK  r9   rZ  r  rN   r   r   r   r|   rO  r:  r+   r)   r  r  <  sp    D     % % % %  r+   r  c                  D   \ rS rSr\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       r\S	 5       r\S
 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       rSrg)BasicMathOpsMixiniW  c                    U  SU 3$ )Nz + r:  r(   bs     r)   rb  BasicMathOpsMixin.addX      Cs|r+   c                    U  SU 3$ )Nz - r:  r  s     r)   re  BasicMathOpsMixin.sub\  r  r+   c                    U  SU 3$ )Nz * r:  r  s     r)   rh  BasicMathOpsMixin.mul`  r  r+   c                    U  SU 3$ )Nz // r:  r  s     r)   r  BasicMathOpsMixin.floordivd      D}r+   c                    U  SU 3$ )Nz / r:  r  s     r)   r  BasicMathOpsMixin.truedivh  r  r+   c                    U  SU 3$ )Nz % r:  r  s     r)   r  BasicMathOpsMixin.modl  s     Cs|r+   c                    U  SU 3$ )Nz ** r:  r  s     r)   rk  BasicMathOpsMixin.powq  r  r+   c                    U  SU 3$ )Nz << r:  r  s     r)   rw  BasicMathOpsMixin.lshiftu  r  r+   c                    U  SU 3$ )Nz >> r:  r  s     r)   rz  BasicMathOpsMixin.rshifty  r  r+   c                    U  SU 3$ )Nz & r:  r  s     r)   rn  BasicMathOpsMixin.and_}  r  r+   c                    U  SU 3$ )Nz | r:  r  s     r)   rq  BasicMathOpsMixin.or_  r  r+   c                    U  SU 3$ )Nz ^ r:  r  s     r)   rt  BasicMathOpsMixin.xor  r  r+   c                    U  SU 3$ )Nz == r:  r  s     r)   rP  BasicMathOpsMixin.eq  r  r+   c                    U  SU 3$ )Nz != r:  r  s     r)   rS  BasicMathOpsMixin.ne  r  r+   c                    U  SU 3$ )Nz < r:  r  s     r)   rV  BasicMathOpsMixin.lt  r  r+   c                    U  SU 3$ )Nz > r:  r  s     r)   rY  BasicMathOpsMixin.gt  r  r+   c                    U  SU 3$ )Nz <= r:  r  s     r)   r\  BasicMathOpsMixin.le  r  r+   c                    U  SU 3$ )Nz >= r:  r  s     r)   r_  BasicMathOpsMixin.ge  r  r+   c                    SU  3$ )N-r:  r'   s    r)   rJ  BasicMathOpsMixin.neg  s    1#wr+   r:  N)rH  rI  rJ  rK  r  rb  re  rh  r  r  r  rk  rw  rz  rn  rq  rt  rP  rS  rV  rY  r\  r_  rJ  rO  r:  r+   r)   r  r  W  s|                                        r+   r  c                  ~    \ rS rSrS rS
S jr\SS j5       r\S 5       r\S 5       r	\S 5       r
\SSS jj5       rSrg	)MockHandleri  c                    / [        [        U5      QnUR                  5        H$  u  pVUR                  U S[        U5       35        M&     SU SSR	                  U5       S3$ )N=zops.(rx  ))mapr*   r  appendr  )r2   r9   r4  rY  fargskvs          r)   rZ  MockHandler._default  s_    &#h%&LLNDALLA3a}-. #dV1TYYu-.a00r+   c                $    SU  SU" 5        SU S3$ )Nzops.masked(rx  r  r:  r  s      r)   rN   MockHandler.masked  s    TF"TVHBugQ77r+   c                    SU  S3SU  S34$ )Nz
ops.frexp(z)[0]z)[1]r:  r  s    r)   r   MockHandler.frexp  s    QCt$
1#T&:;;r+   c                Z   ^ ^^ [        UU U4S j[        [        T5      5       5       5      $ )Nc           
   3  B   >#    U  H  nS T ST ST SU S3	v   M     g7f)z	ops.scan(rx  )[]Nr:  )rt  ir   r   r   s     r)   rv  #MockHandler.scan.<locals>.<genexpr>  s3      
' xr*Rxr!A>'s   tupleranger  r  s   ```r)   r   MockHandler.scan  s&     
3v;'
 
 	
r+   c                ^   ^ ^^^ [        UU UU4S j[        [        T5      5       5       5      $ )Nc              3  H   >#    U  H  nS T ST ST ST SU S3v   M     g7f)z	ops.sort(rx  z	, stable=z, descending=r  r  Nr:  )rt  r  r   r   r   r   s     r)   rv  #MockHandler.sort.<locals>.<genexpr>  s=      
' xr&6(-
|SUVWUXXYZ's   "r  r  s   ````r)   r   MockHandler.sort  s&     
3v;'
 
 	
r+   c                *    [        [        U 5      5      $ r"   )r   r&   r  s       r)   r|   MockHandler.indirect_indexing  s    !#i.11r+   r:  Nr  r<  r&   r?  r  r  r:  r+   r)   r  r    sp    D1 8 8 < < 
 
 
 
 2 2r+   r  c                  t    \ rS rSrSS jr\SSS jj5       rSS jrS rSS jr	          SS jr
S	 rS
rg)KernelFormatterHandleri  c                d    Xl         [        S5      U l        [        R                  " 5       U l        g )Nr   )parent_handlerr   _output	itertoolscountvar_counter)r2   r  s     r)   __init__KernelFormatterHandler.__init__  s#    ,%a($??,r+   Nc                R   SSK Jn  SSKJn  Ub  X/OU/nUb  SS/OS/n[	        [        5       5      nUR                  R                  S5         UR                  R                  SSR                  U5       S	35        S S S 5        [        Xe5       H  u  pU	(       d  M  SR                  U	 V
s/ s H5  n
[        [        U
[        [        R                  45      (       a  S
OU
5      PM7     sn
5      nUR                  R                  U SU 35        M     UR!                  U5         ["        R$                  " USS5         U " U6 nUR'                  U5      sS S S 5        sS S S 5        $ ! , (       d  f       N= fs  sn
f ! , (       d  f       O= f S S S 5        g ! , (       d  f       g = f)Nr   )FlexibleLayout)Vr   rindexzdef inner_fn(rx  z):_ = allow_indexingT)irr  virtualizedr  r  r  r  indent	writeliner  zipr&   r#   rG  r$   Integerset_ops_handlerr   objectr  )ir_fnr   r  r  r  r4  names	formatterr9   argr  lhsresults                r)   ir_to_string#KernelFormatterHandler.ir_to_string  sd   &""("45''-'9(#y*;=9	%%b)''-		%8H7I(LM *U)IDsii "%!$A :a#u}}1E#F#FCAN!$ !!++se3tf,=> * i(LL)94@D\F%%f- A@ )( *)
 A@@ )((s6   /E'-<E8
%F>E=	F'
E5=
F	F
F&c                :    U R                   R                  " U0 UD6$ r"   )r  r|   r2   r4  rY  s      r)   r|   (KernelFormatterHandler.indirect_indexing  s    ""44dEfEEr+   c                v    S[        U R                  5       3nU R                  R                  U SU 35        U$ )Ntmpr  )nextr  r  r  )r2   linevarnames      r)   _writeKernelFormatterHandler._write  s<    T--./0'#dV45r+   c                x    [         R                  " U R                  [        U R                  U5      " U0 UD65      $ r"   )pytreetree_mapr%  r{  r  rX  s       r)   rZ  KernelFormatterHandler._default  s3    KK!4!4d;TLVL
 	
r+   c                F   U R                   R                  XX45      n[        U5      n[        U5       Vs/ s H  nS[	        U R
                  5       3PM     nnU R                  R                  SR                  U5       SU 35        US:  a  [        U5      $ US   $ s  snf )Nr!  ,r  r   r   )
r  r   r   r  r"  r  r  r  r  r  )	r2   r4   r]   r   r3   r#  
num_valuesr
  varnamess	            r)   r    KernelFormatterHandler.reduction  s     "",,U~U*>:
<A*<MN<Mqc$t//012<MN#((8"4!5S?@",q.uXAhqkA Os   "Bc                r    U R                   R                  SU 35        U R                   R                  5       $ )Nzreturn )r  r  r  )r2   r  s     r)   r  KernelFormatterHandler.getvalue  s.    12||$$&&r+   )r  r  r  )r  OpsHandler[Any]r"   r  r  r  )
r4   r;  r]   r;  r   rB  r3   Union[str, tuple[str, ...]]r<  r3  )rH  rI  rJ  rK  r  r  r  r|   r%  rZ  r   r  rO  r:  r+   r)   r  r    sk    -
 . .6F

BB B &	B
 +B 
%B'r+   r  c                  (    \ rS rSrSS jrSS jrSrg)WrapperHandleri  c                    Xl         g r"   _inner)r2   inners     r)   r  WrapperHandler.__init__  s    r+   c                :    [        U R                  U5      " U0 UD6$ r"   )r{  r8  rX  s       r)   rZ  WrapperHandler._default  s    t{{D)4:6::r+   r7  Nr9  r2  r  )rH  rI  rJ  rK  r  rZ  rO  r:  r+   r)   r5  r5    s    ;r+   r5  c                      \ rS rSrSS jrSrg)AddParenHandleri  c                    [        U R                  U5      " U0 UD6nU(       a*  [        U[        R                  [
        [        45      (       a  U$ SU S3$ )Nr  r  )r{  r8  r#   r$   r%   r  list)r2   r9   r4  rY  vals        r)   rZ  AddParenHandler._default  sI    dkk4($9&9juzz5$&?@@J3%qzr+   r:  Nr  )rH  rI  rJ  rK  rZ  rO  r:  r+   r)   r?  r?    s    r+   r?  c                  >    \ rS rSr% S\S'   S\S'   S\S'   S\S'   S	rg
)OpCountResulti  rG  num_opszOrderedSet[str]used_opsz	list[str]read_buffersnontrivial_read_countr:  N)rH  rI  rJ  rK  __annotations__rO  r:  r+   r)   rE  rE    s    Lr+   rE  c                     ^  \ rS rSrSrSU 4S jjrSS jrS rS rSS jr	SS jr
  S               SS	 jjrS
 rSrU =r$ )OpCounterCSEi#  z#Shim to count how many ops are usedc                   > [         TU ]  5         Xl        SU l        0 U l        [        5       U l        / U l        SU l        g )Nr   )	superr  r  op_count	var_namesr   	_used_ops_read_names_nontrivial_read_countr2   r9  	__class__s     r)   r  OpCounterCSE.__init__&  s;    #)+*4,&(&'#r+   c                    U R                   R                  U5        [        R                  " U R                  [        U R                  U5      " U0 UD65      $ r"   )rQ  rb  r(  r)  _update_countr{  r  rX  s       r)   rZ  OpCounterCSE._default/  sG    4 (;(;T BD SF S
 	
r+   c                    U R                   R                  U5      nU(       d2  SU R                   3nU =R                  S-  sl        X R                   U'   U$ )Nr!  r   )rP  getrO  )r2   rB  r$  s      r)   rX  OpCounterCSE._update_count5  sH    ..$$S)DMM?+GMMQM")NN3r+   c                p    U R                   R                  S5        U R                  R                  " U0 UD6$ )Nr|   )rQ  rb  r  r|   r  s      r)   r|   OpCounterCSE.indirect_indexing=  s1    ./""44dEfEEr+   c                X   U R                   R                  X5      nX0R                  ;  ap  U R                  R	                  S5        U R
                  R                  U5        [        U[        R                  [        45      (       d  U =R                  S-  sl        U R                  U5      $ )Nr   r   )r  r   rP  rQ  rb  rR  r  r#   r$   r  rG  rS  rX  )r2   r9   r   rB  s       r)   r   OpCounterCSE.loadA  s    !!&&t3nn$NNv&##D)eemmS%9::++q0+!!#&&r+   c                    U R                   R                  X5      nX0R                  ;  a6  U R                  R	                  S5        U R
                  R                  U5        U R                  U5      $ )Nr;   )r  r;   rP  rQ  rb  rR  r  rX  )r2   r9   r:   rB  s       r)   r;   OpCounterCSE.load_seedJ  sY    !!++D9nn$NN{+##D)!!#&&r+   c           	     8   U R                   R                  UUUUUUU5      nXR                  ;  aZ  U R                  R	                  S5        U R
                  R                  US   5        Ub  U R
                  R                  US   5        U R                  U5      $ )z#
See [Note: Inductor bucketize op]
r   r   )r  r   rP  rQ  rb  rR  r  rX  )	r2   r   r   r   r   r   r   r   rB  s	            r)   r   OpCounterCSE.bucketizeQ  s     !!++
 nn$NN{+##JqM2!  ''q	2!!#&&r+   c                n    [        U R                  U R                  U R                  U R                  5      $ r"   )rE  rO  rQ  rR  rS  )r2   s    r)   r  OpCounterCSE.getvaluen  s,    MM4>>4+;+;T=X=X
 	
r+   )rS  rR  rQ  rO  r  rP  r=  r  )r9   r&   r   r=  r<  r&   )r9   r&   r:   r   rE  rF  )rH  rI  rJ  rK  rL  r  rZ  rX  r|   r   r;   r   r  rO  __classcell__rU  s   @r)   rL  rL  #  s    -(
F'' 48&*'' C' 	'
 $' ' 1' $' 
':
 
r+   rL  c                  (    \ rS rSrSS jrSS jrSrg)ExtractConstantsHandlerit  c                    Xl         g r"   device)r2   rm  s     r)   r   ExtractConstantsHandler.__init__u  s    r+   c                z    SSK Jn  UR                  XU R                  =(       d    [        R
                  " 5       S9$ )Nr   )r  )r3   r4   rm  )torch._inductorr  Constantrm  rM  get_default_device)r2   r3   r4   r  s       r)   r5    ExtractConstantsHandler.constantx  s2    &{{T[[-VE<T<T<V  
 	
r+   rl  N)rm  zOptional[torch.device])r3   r   r4   r;  r<  ztorch._inductor.ir.Constant)rH  rI  rJ  rK  r  r5   rO  r:  r+   r)   rj  rj  t  s    
r+   rj  c                  ^   ^  \ rS rSrSrS	U 4S jjrS
U 4S jjrSS jrSS jrSS jr	Sr
U =r$ )SimpleCSEHandleri  zWraps the underlying handler with a CSE pass

NOTE: Compared to codegen level CSE this is simplified as it
doesn't support stores which require load cache invalidation.
c                P   > [         TU ]  U5        0 U l        [        5       U l        g r"   )rN  r  	cse_cacher  mockrT  s     r)   r  SimpleCSEHandler.__init__  s     ACM	r+   c                $   > [         TU ]  " U0 UD6$ r"   )rN  r|   )r2   r4  rY  rU  s      r)   r|   "SimpleCSEHandler.indirect_indexing  s    w($9&99r+   c                    [        S5      eNzstore not implementedr0   r  s      r)   r   SimpleCSEHandler.store      !"9::r+   c                    [        S5      er}  r0   r  s      r)   r    SimpleCSEHandler.store_reduction  r  r+   c                    [        U R                  U5      " U0 UD6nU R                  R                  U5      nUb  U$ [        U R                  U5      " U0 UD6nXPR                  U'   U$ r"   )r{  rx  rw  r[  r8  )r2   r9   r4  rY  keyrB  s         r)   rZ  SimpleCSEHandler._default  se    dii&77nn  %?Jdkk4($9&9!s
r+   )rw  rx  )r9  r   )r<  r=  r  r  )rH  rI  rJ  rK  rL  r  r|   r   r   rZ  rO  rg  rh  s   @r)   ru  ru    s&    "
:;; r+   ru  )r(   r  r<  r&   )rS  z	type[Any])5
__future__r   ro  r   rera  ior   typingr   r   r   r   r	   r
   r   r   unittest.mockr   r$   rM  torch.utils._pytreeutils_pytreer(  utils._ordered_setr   r   r   r   r   r   r@  rB  r*   r-   compile	fullmatchrR  rT  ry  rV  r  r  r  r  r  r5  r?  rE  rL  rj  ru  r:  r+   r)   <module>r     sC   "   	   X X X    $ $ + W W CLW\*+		x
" x
"v 

<(22E JE+Z_ E+P    . 6L L^!2#^ !2H?'^ ?'D;^ ;n J N
> N
b	
k 	
~ r+   