o
    Zhޏ                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
mZmZmZmZmZmZ d dlmZ d dlZd dlZd dlm  mZ ddlmZ dd	lmZmZmZmZ ed
Zeed  Z ed Z!d0ddZ"G dd dee Z#e$dj%Z&d1ddZ'e'e#Z(G dd de#e	 Z)e)*  G dd de)Z+G dd dZ,G d d! d!e,e)Z-G d"d# d#e)Z.G d$d% d%e)Z/G d&d' d'e/Z0G d(d) d)eZ1G d*d+ d+e)Z2G d,d- d-e+Z3G d.d/ d/e/Z4dS )2    )annotationsN)StringIO)AnyCallableGenericLiteral
NamedTupleOptionalTypeVarUnion)patch   )
OrderedSet   )IndentedBufferreduction_num_outputssympy_index_symbol	sympy_strTZ
atomic_add)
ZargmaxZargminZwelford_reduceZwelford_combineanymaxminprodsumZxor_sumaobjectreturnstrc                 C  s   t | tjr
t| S t| S N)
isinstancesympyExprr   r   r    r#   J/var/www/auris/lib/python3.10/site-packages/torch/_inductor/ops_handler.py_arg_str%   s   r%   c                   @  s@  e Zd ZdZdydd	ZdzddZd{ddZd{ddZd|ddZd}ddZ	d~d d!Z
dd$d%Z	&	'ddd-d.Zdd/d0Zdd1d2Zdd3d4Zdd5d6Zdd7d8Zdd9d:Z	'ddd=d>Zdd@dAZ	&dddEdFZddJdKZddLdMZddTdUZddXdYZ	&	&dddcddZddfdgZddhdiZddjdkZddldmZddndoZddpdqZ ddsdtZ!ddudvZ"ddwdxZ#ddydzZ$dd{d|Z%dd}d~Z&dddZ'dddZ(dddZ)dddZ*dddZ+dddZ,dddZ-dddZ.dddZ/dddZ0dddZ1dddZ2dddZ3dddZ4dddZ5dddZ6dddZ7dddZ8dddZ9dddZ:dddZ;dddZ<dddZ=dddZ>dddZ?dddZ@dddZAdddZBdddZCdddZDdddZEdddZFdddZGdddZHdddĄZIdddƄZJdddȄZKdddʄZLddd̄ZMddd΄ZNdddЄZOddd҄ZPdddԄZQdddքZRddd؄ZSdddڄZTddd܄ZUdddބZVdddZWdddZXdddZYdddZZdddZ[dddZ\dddZ]dddZ^dddZ_dddZ`dddZadddZbdddZcdddZddddZedddZfddd ZgdddZhdddZidddZjdd	d
ZkdddZldddZmdddZndddZodddZpdddZqdddZrdddZsdddZtdddZuddd Zvdd!d"Zwdd#d$Zxdd%d&Zydd'd(Zzdd)d*Z{dd+d,Z|dd-d.Z}dd/d0Z~dd1d2Zdd3d4Zdd5d6Zdd7d8Zdd9d:Zdd;d<Zdd=d>Zdd?d@ZddAdBZddCdDZddEdFZddGdHZddIdJZddKdLZddMdNZddOdPZddQdRZddSdTZddWdXZddYdZZdd[d\Zdd]d^Zdd_d`ZddadbZddcddZddedfZddgdhZd&ejd'didjddrdsZddudvZddwdxZd&S (  
OpsHandlera  
    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.
    valueUnion[bool, float, int]dtypetorch.dtyper   r   c                 C     t )z)Produces a scalar constant of type dtype.NotImplementedError)selfr'   r)   r#   r#   r$   constantI      zOpsHandler.constantnamer   offsetc                 C  r+   )z$Computes inductor_prims.lookup_seed.r,   )r.   r1   r2   r#   r#   r$   	load_seedM   r0   zOpsHandler.load_seedseedc                 C  r+   )zIComputes inductor_prims.random with mode="rand".  offset has dtype int32.r,   r.   r4   r2   r#   r#   r$   randQ   r0   zOpsHandler.randc                 C  r+   )zJComputes inductor_prims.random with mode="randn".  offset has dtype int32.r,   r5   r#   r#   r$   randnU   r0   zOpsHandler.randnlowhighc                 C  r+   )z9Computes inductor_prims.randint.  offset has dtype int32.r,   )r.   r4   r2   r8   r9   r#   r#   r$   	randint64Y   r0   zOpsHandler.randint64maskbodyCallable[[], T]otherc                 C  r+   )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.
        r,   )r.   r;   r<   r>   r#   r#   r$   masked]      zOpsHandler.masked	conditioninputc                 C  r+   )ze
        Computes torch.where: when condition is true, return input; otherwise return other.
        r,   )r.   rA   rB   r>   r#   r#   r$   wherek      zOpsHandler.whereexpr
sympy.Exprc                 C  r+   )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.
        r,   )r.   rE   r)   r#   r#   r$   
index_exprq   s   zOpsHandler.index_exprNTx	src_dtypeOptional[torch.dtype]use_compute_typesboolc                 C  r+   )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)).
        r,   )r.   rH   r)   rI   rK   r#   r#   r$   to_dtypey      zOpsHandler.to_dtypec                 C  r+   )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.
        r,   r.   rH   r)   r#   r#   r$   trunc_to_int   r@   zOpsHandler.trunc_to_intc                 C  r+   zT
        Convert x to dtype with ceiling semantics.  See also trunc_to_int.
        r,   rO   r#   r#   r$   ceil_to_int   rD   zOpsHandler.ceil_to_intc                 C  r+   rQ   r,   rO   r#   r#   r$   floor_to_int   rD   zOpsHandler.floor_to_intc                 C  r+   )zZ
        Convert x to dtype with round-to-even semantics.  See also trunc_to_int.
        r,   rO   r#   r#   r$   round_to_int   rD   zOpsHandler.round_to_intc                 C  r+   )z
        Reinterpret cast x to dtype (reinterpreting the bits in memory as another dtype.)
        src_dtype must be the original type of x.
        r,   )r.   rH   r)   rI   r#   r#   r$   to_dtype_bitcast      zOpsHandler.to_dtype_bitcastc                 C  r+   )z@
        Returns x as is.  This is used to trigger CSE.
        r,   r.   rH   r#   r#   r$   identity   rD   zOpsHandler.identitysizecheckc                 C  r+   )a  
        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).
        r,   )r.   rH   rY   rZ   wrap_negr#   r#   r$   indirect_indexing   rN   zOpsHandler.indirect_indexingindexc                 C  r+   )zc
        Load from the memory location 'name', offset by some indexing expression 'index'.
        r,   )r.   r1   r]   r#   r#   r$   load   rD   zOpsHandler.loadmode	StoreModeNonec                 C  r+   )z
        Store 'value' to the memory location 'name' offset by 'expr'.  If
        specified, 'mode' can require the store to be an atomic addition.
        r,   )r.   r1   r]   r'   r_   r#   r#   r$   store   rN   zOpsHandler.storereduction_typeReductionTypeUnion[T, tuple[T, ...]]c                 C  r+   )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.
        r,   )r.   r)   rI   rc   r'   r#   r#   r$   	reduction   s   zOpsHandler.reductionc                 C  r+   )z{
        Store the fully accumulated result of 'reduction' to the memory
        location 'name' offset by 'expr'.
        r,   )r.   r1   r]   r'   r#   r#   r$   store_reduction   rV   zOpsHandler.store_reductiondtypestuple[torch.dtype, ...]
combine_fn7Callable[[tuple[T, ...], tuple[T, ...]], tuple[T, ...]]valuestuple[T, ...]c                 C  r+   )z9
        Perform an associative scan on 'value'.
        r,   )r.   rh   rj   rl   r#   r#   r$   scan      
zOpsHandler.scanstable
descendingc                 C  r+   )z<
        Sort values along the reduction dimension.
        r,   )r.   rh   rl   rp   rq   r#   r#   r$   sort  ro   zOpsHandler.sort
boundaries.tuple[str, sympy.Expr, sympy.Expr, sympy.Expr]boundary_indicesindexing_dtyperightsorter Optional[tuple[str, sympy.Expr]]sorter_indicesOptional[T]c                 C  r+   r   r,   )r.   rl   rs   ru   rv   rw   rx   rz   r#   r#   r$   	bucketize  rN   zOpsHandler.bucketizex0c                 C  r+   r   r,   r.   r}   r#   r#   r$   abs%     zOpsHandler.absc                 C  r+   r   r,   r~   r#   r#   r$   exp(  r   zOpsHandler.expc                 C  r+   r   r,   r~   r#   r#   r$   exp2+  r   zOpsHandler.exp2c                 C  r+   r   r,   r~   r#   r#   r$   expm1.  r   zOpsHandler.expm1c                 C  r+   r   r,   r~   r#   r#   r$   sqrt1  r   zOpsHandler.sqrtc                 C  r+   r   r,   r~   r#   r#   r$   relu4  r   zOpsHandler.relux1c                 C  r+   r   r,   r.   r}   r   r#   r#   r$   minimum7  r   zOpsHandler.minimumc                 C  r+   r   r,   r   r#   r#   r$   maximum:  r   zOpsHandler.maximumc                 C  r+   r   r,   r~   r#   r#   r$   cos=  r   zOpsHandler.cosc                 C  r+   r   r,   r~   r#   r#   r$   sin@  r   zOpsHandler.sinc                 C  r+   r   r,   r~   r#   r#   r$   lgammaC  r   zOpsHandler.lgammac                 C  r+   r   r,   r~   r#   r#   r$   erfF  r   zOpsHandler.erfc                 C  r+   r   r,   r~   r#   r#   r$   coshI  r   zOpsHandler.coshc                 C  r+   r   r,   r~   r#   r#   r$   sinhL  r   zOpsHandler.sinhc                 C  r+   r   r,   r~   r#   r#   r$   acosO  r   zOpsHandler.acosc                 C  r+   r   r,   r~   r#   r#   r$   acoshR  r   zOpsHandler.acoshc                 C  r+   r   r,   r~   r#   r#   r$   asinU  r   zOpsHandler.asinc                 C  r+   r   r,   r~   r#   r#   r$   asinhX  r   zOpsHandler.asinhc                 C  r+   r   r,   r   r#   r#   r$   atan2[  r   zOpsHandler.atan2c                 C  r+   r   r,   r~   r#   r#   r$   atan^  r   zOpsHandler.atanc                 C  r+   r   r,   r~   r#   r#   r$   atanha  r   zOpsHandler.atanhc                 C  r+   r   r,   r   r#   r#   r$   copysignd  r   zOpsHandler.copysignc                 C  r+   r   r,   r~   r#   r#   r$   erfcg  r   zOpsHandler.erfcc                 C  r+   r   r,   r~   r#   r#   r$   erfinvj  r   zOpsHandler.erfinvc                 C  r+   r   r,   r~   r#   r#   r$   frexpm  r   zOpsHandler.frexpc                 C  r+   r   r,   r   r#   r#   r$   hypotp  r   zOpsHandler.hypotc                 C  r+   r   r,   r~   r#   r#   r$   log10s  r   zOpsHandler.log10c                 C  r+   r   r,   r~   r#   r#   r$   log2v  r   zOpsHandler.log2c                 C  r+   r   r,   r   r#   r#   r$   	nextaftery  r   zOpsHandler.nextafterc                 C  r+   r   r,   r   r#   r#   r$   logical_and|  r   zOpsHandler.logical_andc                 C  r+   r   r,   r~   r#   r#   r$   logical_not  r   zOpsHandler.logical_notc                 C  r+   r   r,   r   r#   r#   r$   
logical_or  r   zOpsHandler.logical_orc                 C  r+   r   r,   r   r#   r#   r$   logical_xor  r   zOpsHandler.logical_xorc                 C  r+   r   r,   r   r#   r#   r$   bitwise_and  r   zOpsHandler.bitwise_andc                 C  r+   r   r,   r~   r#   r#   r$   bitwise_not  r   zOpsHandler.bitwise_notc                 C  r+   r   r,   r   r#   r#   r$   
bitwise_or  r   zOpsHandler.bitwise_orc                 C  r+   r   r,   r   r#   r#   r$   bitwise_xor  r   zOpsHandler.bitwise_xorc                 C  r+   r   r,   r   r#   r#   r$   bitwise_left_shift  r   zOpsHandler.bitwise_left_shiftc                 C  r+   r   r,   r   r#   r#   r$   bitwise_right_shift  r   zOpsHandler.bitwise_right_shiftc                 C  r+   r   r,   r~   r#   r#   r$   rsqrt  r   zOpsHandler.rsqrtc                 C  r+   r   r,   r~   r#   r#   r$   log1p  r   zOpsHandler.log1pc                 C  r+   r   r,   r~   r#   r#   r$   tan  r   zOpsHandler.tanc                 C  r+   r   r,   r~   r#   r#   r$   tanh  r   zOpsHandler.tanhc                 C  r+   r   r,   r~   r#   r#   r$   sigmoid  r   zOpsHandler.sigmoidc                 C  r+   r   r,   r~   r#   r#   r$   signbit  r   zOpsHandler.signbitc                 C  r+   r   r,   r   r#   r#   r$   fmod  r   zOpsHandler.fmodc                 C  r+   r   r,   r~   r#   r#   r$   log  r   zOpsHandler.logc                 C  r+   r   r,   r~   r#   r#   r$   isinf  r   zOpsHandler.isinfc                 C  r+   r   r,   r~   r#   r#   r$   isnan  r   zOpsHandler.isnanc                 C  r+   r   r,   r~   r#   r#   r$   round  r   zOpsHandler.roundc                 C  r+   r   r,   r~   r#   r#   r$   floor  r   zOpsHandler.floorc                 C  r+   r   r,   r~   r#   r#   r$   sign  r   zOpsHandler.signc                 C  r+   r   r,   r~   r#   r#   r$   trunc  r   zOpsHandler.truncc                 C  r+   r   r,   r~   r#   r#   r$   ceil  r   zOpsHandler.ceilc                 C  r+   r   r,   r~   r#   r#   r$   neg  r   zOpsHandler.negc                 C  r+   r   r,   r~   r#   r#   r$   
reciprocal  r   zOpsHandler.reciprocalc                 C  r+   r   r,   r   r#   r#   r$   eq  r   zOpsHandler.eqc                 C  r+   r   r,   r   r#   r#   r$   ne  r   zOpsHandler.nec                 C  r+   r   r,   r   r#   r#   r$   lt  r   zOpsHandler.ltc                 C  r+   r   r,   r   r#   r#   r$   gt  r   zOpsHandler.gtc                 C  r+   r   r,   r   r#   r#   r$   le  r   zOpsHandler.lec                 C  r+   r   r,   r   r#   r#   r$   ge  r   zOpsHandler.gec                 C  r+   r   r,   r   r#   r#   r$   add  r   zOpsHandler.addc                 C  r+   r   r,   r   r#   r#   r$   sub  r   zOpsHandler.subc                 C  r+   r   r,   r   r#   r#   r$   mul  r   zOpsHandler.mulc                 C  r+   r   r,   r   r#   r#   r$   pow  r   zOpsHandler.powc                 C  r+   r   r,   r   r#   r#   r$   and_  r   zOpsHandler.and_c                 C  r+   r   r,   r   r#   r#   r$   or_  r   zOpsHandler.or_c                 C  r+   r   r,   r   r#   r#   r$   xor  r   zOpsHandler.xorc                 C  r+   r   r,   r   r#   r#   r$   lshift  r   zOpsHandler.lshiftc                 C  r+   r   r,   r   r#   r#   r$   rshift  r   zOpsHandler.rshiftc                 C  r+   r   r,   rW   r#   r#   r$   airy_ai  r   zOpsHandler.airy_aic                 C  r+   r   r,   rW   r#   r#   r$   	bessel_j0	  r   zOpsHandler.bessel_j0c                 C  r+   r   r,   rW   r#   r#   r$   	bessel_j1  r   zOpsHandler.bessel_j1c                 C  r+   r   r,   rW   r#   r#   r$   	bessel_y0  r   zOpsHandler.bessel_y0c                 C  r+   r   r,   rW   r#   r#   r$   	bessel_y1  r   zOpsHandler.bessel_y1c                 C  r+   r   r,   rW   r#   r#   r$   digamma  r   zOpsHandler.digammac                 C  r+   r   r,   rW   r#   r#   r$   erfcx  r   zOpsHandler.erfcxyzc                 C  r+   r   r,   )r.   rH   r   r   r#   r#   r$   fma  r   zOpsHandler.fmac                 C  r+   r   r,   r.   rH   r   r#   r#   r$   igamma  r   zOpsHandler.igammac                 C  r+   r   r,   r   r#   r#   r$   igammac!  r   zOpsHandler.igammacc                 C  r+   r   r,   r   r#   r#   r$   gammainc$  r   zOpsHandler.gammaincc                 C  r+   r   r,   r   r#   r#   r$   	gammaincc'  r   zOpsHandler.gammainccc                 C  r+   r   r,   rW   r#   r#   r$   i0*  r   zOpsHandler.i0c                 C  r+   r   r,   rW   r#   r#   r$   i0e-  r   zOpsHandler.i0ec                 C  r+   r   r,   rW   r#   r#   r$   i10  r   zOpsHandler.i1c                 C  r+   r   r,   rW   r#   r#   r$   i1e3  r   zOpsHandler.i1ec                 C  r+   r   r,   rW   r#   r#   r$   log_ndtr6  r   zOpsHandler.log_ndtrc                 C  r+   r   r,   rW   r#   r#   r$   modified_bessel_i09  r   zOpsHandler.modified_bessel_i0c                 C  r+   r   r,   rW   r#   r#   r$   modified_bessel_i1<  r   zOpsHandler.modified_bessel_i1c                 C  r+   r   r,   rW   r#   r#   r$   modified_bessel_k0?  r   zOpsHandler.modified_bessel_k0c                 C  r+   r   r,   rW   r#   r#   r$   modified_bessel_k1B  r   zOpsHandler.modified_bessel_k1c                 C  r+   r   r,   rW   r#   r#   r$   ndtrE  r   zOpsHandler.ndtrc                 C  r+   r   r,   rW   r#   r#   r$   ndtriH  r   zOpsHandler.ndtric                 C  r+   r   r,   r   r#   r#   r$   	polygammaK  r   zOpsHandler.polygammac                 C  r+   r   r,   rW   r#   r#   r$   scaled_modified_bessel_k0N  r   z$OpsHandler.scaled_modified_bessel_k0c                 C  r+   r   r,   rW   r#   r#   r$   scaled_modified_bessel_k1Q  r   z$OpsHandler.scaled_modified_bessel_k1c                 C  r+   r   r,   rW   r#   r#   r$   spherical_bessel_j0T  r   zOpsHandler.spherical_bessel_j0c                 C  r+   r   r,   r   r#   r#   r$   zetaW  r   zOpsHandler.zetac                 C  r+   r   r,   r   r#   r#   r$   chebyshev_polynomial_tZ  r   z!OpsHandler.chebyshev_polynomial_tc                 C  r+   r   r,   r   r#   r#   r$   chebyshev_polynomial_u]  r   z!OpsHandler.chebyshev_polynomial_uc                 C  r+   r   r,   r   r#   r#   r$   chebyshev_polynomial_v`  r   z!OpsHandler.chebyshev_polynomial_vc                 C  r+   r   r,   r   r#   r#   r$   chebyshev_polynomial_wc  r   z!OpsHandler.chebyshev_polynomial_wc                 C  r+   r   r,   r   r#   r#   r$   legendre_polynomial_pf  r   z OpsHandler.legendre_polynomial_pc                 C  r+   r   r,   r   r#   r#   r$   shifted_chebyshev_polynomial_ti  r   z)OpsHandler.shifted_chebyshev_polynomial_tc                 C  r+   r   r,   r   r#   r#   r$   shifted_chebyshev_polynomial_ul  r   z)OpsHandler.shifted_chebyshev_polynomial_uc                 C  r+   r   r,   r   r#   r#   r$   shifted_chebyshev_polynomial_vo  r   z)OpsHandler.shifted_chebyshev_polynomial_vc                 C  r+   r   r,   r   r#   r#   r$   shifted_chebyshev_polynomial_wr  r   z)OpsHandler.shifted_chebyshev_polynomial_wc                 C  r+   r   r,   r   r#   r#   r$   hermite_polynomial_hu  r   zOpsHandler.hermite_polynomial_hc                 C  r+   r   r,   r   r#   r#   r$   hermite_polynomial_hex  r   z OpsHandler.hermite_polynomial_hec                 C  r+   r   r,   r   r#   r#   r$   laguerre_polynomial_l{  r   z OpsHandler.laguerre_polynomial_lc                 C  r+   )zC-style trunc division between integers only.  Computes the true
        division of two numbers and rounds the result to zero.
        r,   r   r#   r#   r$   truncdiv  rD   zOpsHandler.truncdivc                 C  r+   )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.
        r,   r   r#   r#   r$   floordiv  rV   zOpsHandler.floordivc                 C  r+   )zTrue division between floats.  Integer inputs are NOT valid.  To
        do Python-style (int, int) -> float division, use int_truedivr,   r   r#   r#   r$   truediv     zOpsHandler.truedivc                 C  r+   )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.
        r,   r   r#   r#   r$   int_truediv  rV   zOpsHandler.int_truedivc                 C  r+   )z)C-style modulus, take sign from LHS (x0).r,   r   r#   r#   r$   mod  r0   zOpsHandler.modc                 C  r+   )z.Python-style modulus, take sign from RHS (x1).r,   r   r#   r#   r$   	remainder  r0   zOpsHandler.remainderc                 C  r+   r   r,   r~   r#   r#   r$   square  r   zOpsHandler.squarelowerupperc                 C  r+   r   r,   )r.   rE   rY   r   r   r#   r#   r$   check_bounds  r   zOpsHandler.check_boundsc                 C  r+   r   r,   r~   r#   r#   r$   libdevice_abs  r   zOpsHandler.libdevice_absc                 C  r+   r   r,   r~   r#   r#   r$   libdevice_exp  r   zOpsHandler.libdevice_expc                 C  r+   r   r,   r~   r#   r#   r$   libdevice_sqrt  r   zOpsHandler.libdevice_sqrtc                 C  r+   r   r,   r~   r#   r#   r$   libdevice_cos  r   zOpsHandler.libdevice_cosc                 C  r+   r   r,   r~   r#   r#   r$   libdevice_sin  r   zOpsHandler.libdevice_sinc                 C  r+   r   r,   r~   r#   r#   r$   libdevice_sigmoid  r   zOpsHandler.libdevice_sigmoidc                 C  r+   r   r,   r~   r#   r#   r$   libdevice_log  r   zOpsHandler.libdevice_logc                 C  r+   r   r,   )r.   r'   rY   rZ   r#   r#   r$   halide_clamp  r   zOpsHandler.halide_clampr   )constraintsr)   is_purepackinputsasmr  Optional[str]r  r	  intc                G  r+   r   r,   )r.   r  r  r)   r  r	  r
  r#   r#   r$   inline_asm_elementwise  s   	z!OpsHandler.inline_asm_elementwiseargsc                 G  r+   z2This is a fake op used in analysis but not codegenr,   )r.   r  r#   r#   r$   output  r0   zOpsHandler.outputc                 C  r+   r  r,   )r.   r]   r#   r#   r$   placeholder  r0   zOpsHandler.placeholder)r'   r(   r)   r*   r   r   )r1   r   r2   r   r   r   )r4   r   r2   r   r   r   )
r4   r   r2   r   r8   r   r9   r   r   r   )r;   r   r<   r=   r>   r   r   r   )rA   r   rB   r   r>   r   r   r   )rE   rF   r)   r*   r   r   )NT)
rH   r   r)   r*   rI   rJ   rK   rL   r   r   )rH   r   r)   r*   r   r   )rH   r   r)   r*   rI   r*   r   r   )rH   r   r   r   TT)rH   r   rY   rF   rZ   rL   r   rF   )r1   r   r]   rF   r   r   r   )
r1   r   r]   rF   r'   r   r_   r`   r   ra   )
r)   r*   rI   r*   rc   rd   r'   r   r   re   )r1   r   r]   rF   r'   r   r   ra   )rh   ri   rj   rk   rl   rm   r   rm   )
rh   ri   rl   rm   rp   rL   rq   rL   r   rm   NNrl   r   rs   rt   ru   r   rv   r*   rw   rL   rx   ry   rz   r{   r   r   )r}   r   r   r   )r}   r   r   r   r   r   )r}   r   )rH   r   r   r   r   r   r   r   )rH   r   r   r   r   r   )
rE   rF   rY   rF   r   rL   r   rL   r   ra   )r'   r   rY   rF   rZ   rL   r   r   )r
  r   r  r   r  r  r)   r*   r  rL   r	  r  r   r   )r  r   r   ra   )r]   r  r   r   )__name__
__module____qualname____doc__r/   r3   r6   r7   r:   r?   rC   rG   rM   rP   rR   rS   rT   rU   rX   r\   r^   rb   rf   rg   rn   rr   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   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   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  torchZfloat32r  r  r  r#   r#   r#   r$   r&   ,   sJ   r&   z	_.*|parencls	type[Any]c                 C  s   t dd t| D S )Nc                 S  s   g | ]}t |s|qS r#   )_ignore_op_re).0rH   r#   r#   r$   
<listcomp>  s    zlist_ops.<locals>.<listcomp>)r   dir)r  r#   r#   r$   list_ops     r!  c                   @  s:   e Zd Zdd	d
ZdddZedddZedd ZdS )DefaultHandlerr1   r   r  tuple[Any, ...]kwargsdict[str, Any]r   r   c                 C  r+   )aV  
        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

        r,   r.   r1   r  r%  r#   r#   r$   _default  s   zDefaultHandler._defaultc                   s&   d	 fdd}t d  d |S )
Nr  r   r%  r   c                    s     | |S r   r(  )r  r%  r1   r.   r#   r$   fallback     z,DefaultHandler.__getattr__.<locals>.fallbackzundefined OpHandler.z, please add missing op schema)r  r   r%  r   r   r   )warningswarn)r.   r1   r+  r#   r*  r$   __getattr__  s   zDefaultHandler.__getattr__targetc                   s    fdd} |_ |S )Nc                   s   |   ||S r   r)  r.   r  r%  r0  r#   r$   call_default  r,  z2DefaultHandler._call_default.<locals>.call_default)r  )r0  r3  r#   r2  r$   _call_default
  s   zDefaultHandler._call_defaultc                 C  s   t  }tD ]L}ttt|}tdd |j D rH|j	 ^}}|dks(J |
d| dd| d|dd| d		  |
d
 qt| || | qi }t| | | D ]\}}|tv rmt| || q_dS )a+  
        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                 s  s,    | ]}|j tjjko|jtjju V  qd S r   )kindinspect	ParameterPOSITIONAL_OR_KEYWORDdefaultempty)r  pr#   r#   r$   	<genexpr>   s    
z+DefaultHandler._init_cls.<locals>.<genexpr>r.   z
                    def z(self, , z0):
                        return self._default(z, (z, ), {})
                    z

N)r   OP_NAMESr6  	signaturegetattrr&   all
parametersrl   keyswritejoinstripsetattrr4  execgetvalueitems)r  coder0  sigZself_argr  ctximplr#   r#   r$   	_init_cls  s:   zDefaultHandler._init_clsNr1   r   r  r$  r%  r&  r   r   )r1   r   r   r   )r0  r   )	r  r  r  r(  r/  staticmethodr4  classmethodrO  r#   r#   r#   r$   r#    s    

r#  c                   @  sb   e Zd Zd Zdd	d
ZedddZedddZedddZedddZ	edd ddZ
dS )!NoopHandlerr1   r   r  r$  r%  r&  r   r   c                 C     d S r   r#   r'  r#   r#   r$   r(  ?  r   zNoopHandler._defaultra   c                 C  rT  r   r#   r;   r<   r>   r#   r#   r$   r?   B  r0   zNoopHandler.maskedtuple[None, None]c                 C  s   dS )Nr  r#   rH   r#   r#   r$   r   F  r0   zNoopHandler.frexptuple[None, ...]c                 C  s   dt | S Nr   lenrh   rj   rl   r#   r#   r$   rn   J     zNoopHandler.scanc                 C  s   dt | S rY  rZ  rh   rl   rp   rq   r#   r#   r$   rr   N  r]  zNoopHandler.sortTsympy.Symbolc                 C  s   t jjS r   )r    SZZeroZ	index_varrY   rZ   r[   r#   r#   r$   r\   R  s   zNoopHandler.indirect_indexingNrP  r   ra   )r   rV  )r   rX  r  r   r_  r  r  r  r1   r(  rQ  r?   r   rn   rr   r\   r#   r#   r#   r$   rS  <  s    
rS  c                   @  s   e Zd Zedd Zedd Zedd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd  Zed!d" Zed#d$ Zed%d& Zd'S )(BasicMathOpsMixinc                 C     |  d| S )Nz + r#   r   br#   r#   r$   r   X     zBasicMathOpsMixin.addc                 C  rf  )Nz - r#   rg  r#   r#   r$   r   \  ri  zBasicMathOpsMixin.subc                 C  rf  )Nz * r#   rg  r#   r#   r$   r   `  ri  zBasicMathOpsMixin.mulc                 C  rf  )Nz // r#   rg  r#   r#   r$   r   d  ri  zBasicMathOpsMixin.floordivc                 C  rf  )Nz / r#   rg  r#   r#   r$   r   h  ri  zBasicMathOpsMixin.truedivc                 C  rf  )Nz % r#   rg  r#   r#   r$   r   l  s   zBasicMathOpsMixin.modc                 C  rf  )Nz ** r#   rg  r#   r#   r$   r   q  ri  zBasicMathOpsMixin.powc                 C  rf  )Nz << r#   rg  r#   r#   r$   r   u  ri  zBasicMathOpsMixin.lshiftc                 C  rf  )Nz >> r#   rg  r#   r#   r$   r   y  ri  zBasicMathOpsMixin.rshiftc                 C  rf  )Nz & r#   rg  r#   r#   r$   r   }  ri  zBasicMathOpsMixin.and_c                 C  rf  )Nz | r#   rg  r#   r#   r$   r     ri  zBasicMathOpsMixin.or_c                 C  rf  )Nz ^ r#   rg  r#   r#   r$   r     ri  zBasicMathOpsMixin.xorc                 C  rf  )Nz == r#   rg  r#   r#   r$   r     ri  zBasicMathOpsMixin.eqc                 C  rf  )Nz != r#   rg  r#   r#   r$   r     ri  zBasicMathOpsMixin.nec                 C  rf  )Nz < r#   rg  r#   r#   r$   r     ri  zBasicMathOpsMixin.ltc                 C  rf  )Nz > r#   rg  r#   r#   r$   r     ri  zBasicMathOpsMixin.gtc                 C  rf  )Nz <= r#   rg  r#   r#   r$   r     ri  zBasicMathOpsMixin.lec                 C  rf  )Nz >= r#   rg  r#   r#   r$   r     ri  zBasicMathOpsMixin.gec                 C  s
   d|  S )N-r#   r"   r#   r#   r$   r     s   
zBasicMathOpsMixin.negN)r  r  r  rQ  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r#   r#   r#   r$   re  W  sN    

















re  c                   @  s\   e Zd Zd Zdd	d
ZedddZedd Zedd Zedd Z	eddddZ
dS )MockHandlerr1   r   r  r$  r%  r&  r   r   c                 C  sP   g t t|}| D ]\}}|| dt|  qd| dd| dS )N=zops.(r=  ))mapr%   rJ  appendrE  )r.   r1   r  r%  Zfargskvr#   r#   r$   r(    s   zMockHandler._defaultc                 C  s   d|  d|  d| dS )Nzops.masked(r=  rn  r#   rU  r#   r#   r$   r?     s   zMockHandler.maskedc                 C  s   d|  dd|  dfS )Nz
ops.frexp(z)[0]z)[1]r#   rW  r#   r#   r$   r     s   zMockHandler.frexpc                   s"   t  fddttD S )Nc              
   3  s.    | ]}d  d  d d| d	V  qdS )z	ops.scan(r=  )[]Nr#   r  irj   rh   rl   r#   r$   r<    s
    
z#MockHandler.scan.<locals>.<genexpr>tupleranger[  r\  r#   rw  r$   rn     s   
zMockHandler.scanc                   s$   t  fddttD S )Nc                 3  s4    | ]}d  d d d  d| dV  qdS )z	ops.sort(r=  z	, stable=z, descending=rs  rt  Nr#   ru  rq   rh   rp   rl   r#   r$   r<    s
    "
z#MockHandler.sort.<locals>.<genexpr>rx  r^  r#   r{  r$   rr     s   
zMockHandler.sortTr_  c                 C  s   t t| S r   )r   r   ra  r#   r#   r$   r\     r]  zMockHandler.indirect_indexingNrP  r   r   r  rc  rd  r#   r#   r#   r$   rk    s    



rk  c                   @  sT   e Zd Zd"ddZed#d$dd	Zd%ddZdd Zd&ddZd'ddZ	d d! Z
dS )(KernelFormatterHandlerparent_handlerOpsHandler[Any]c                 C  s   || _ td| _t | _d S )Nr   )r~  r   _output	itertoolscountvar_counter)r.   r~  r#   r#   r$   __init__  s   
zKernelFormatterHandler.__init__Nr   r   c              	   C  sT  ddl m} ddlm} |d ur||gn|g}|d urddgndg}tt }|jd |jdd	| d	 W d    n1 sEw   Y  t
||D ]\}}	|	rjd	d
d |	D }
|j|
 d|  qO||0 t|dd | | }||W  d    W  d    S 1 sw   Y  W d    d S 1 sw   Y  d S )Nr   )FlexibleLayout)Vr]   rindexzdef inner_fn(r=  z):c                 S  s(   g | ]}t t|ttjfrd n|qS )_)r   r   r  r    Integer)r  rr  r#   r#   r$   r    s    z7KernelFormatterHandler.ir_to_string.<locals>.<listcomp> = Zallow_indexingT)irr  Zvirtualizedr  r}  rk  r  indent	writelinerE  zipZset_ops_handlerr   r   rI  )Zir_fnr]   r  r  r  r  names	formatterr1   arglhsresultr#   r#   r$   ir_to_string  s0   
Rz#KernelFormatterHandler.ir_to_stringr_  c                 O  s   | j j|i |S r   )r~  r\   r1  r#   r#   r$   r\        z(KernelFormatterHandler.indirect_indexingc                 C  s*   dt | j }| j| d|  |S )Ntmpr  )nextr  r  r  )r.   linevarnamer#   r#   r$   _write  s   zKernelFormatterHandler._writer1   r  r$  r%  r&  r   c                 C  s    t | jt| j||i |S r   )pytreetree_mapr  r@  r~  r'  r#   r#   r$   r(    s   zKernelFormatterHandler._defaultr)   r*   rI   rc   rd   r'   Union[str, tuple[str, ...]]c                   sd    j ||||}t|} fddt|D } jd| d|  |dkr.t|S |d S )Nc                   s   g | ]
}d t  j qS )r  )r  r  )r  r  r.   r#   r$   r    s    z4KernelFormatterHandler.reduction.<locals>.<listcomp>,r  r   r   )r~  rf   r   rz  r  r  rE  ry  )r.   r)   rI   rc   r'   r  Z
num_valuesvarnamesr#   r  r$   rf     s
   z KernelFormatterHandler.reductionc                 C  s   | j d|  | j  S )Nzreturn )r  r  rI  )r.   r  r#   r#   r$   rI    s   
zKernelFormatterHandler.getvalue)r~  r  r   r|  rc  rP  )
r)   r*   rI   r*   rc   rd   r'   r  r   r  )r  r  r  r  rQ  r  r\   r  r(  rf   rI  r#   r#   r#   r$   r}    s    



r}  c                   @  s    e Zd ZdddZdddZdS )WrapperHandlerinnerr  c                 C  
   || _ d S r   )_innerr.   r  r#   r#   r$   r       
zWrapperHandler.__init__r1   r   r  r$  r%  r&  r   r   c                 C  s   t | j||i |S r   )r@  r  r'  r#   r#   r$   r(    r"  zWrapperHandler._defaultNr  r  rP  )r  r  r  r  r(  r#   r#   r#   r$   r        
r  c                   @  s   e Zd Zdd	d
ZdS )AddParenHandlerr1   r   r  r$  r%  r&  r   r   c                 C  s<   t | j||i |}|rt|tjttfr|S d| dS )Nrm  rn  )r@  r  r   r    r!   ry  list)r.   r1   r  r%  valr#   r#   r$   r(    s   zAddParenHandler._defaultNrP  )r  r  r  r(  r#   r#   r#   r$   r    s    r  c                   @  s.   e Zd ZU ded< ded< ded< ded< dS )	OpCountResultr  Znum_opszOrderedSet[str]Zused_opsz	list[str]Zread_buffersZnontrivial_read_countN)r  r  r  __annotations__r#   r#   r#   r$   r    s
   
 r  c                      sh   e Zd ZdZd- fddZd.ddZdd Zdd Zd/ddZd0ddZ			d1d2d)d*Z
d+d, Z  ZS )3OpCounterCSEz#Shim to count how many ops are usedr  r  c                   s4   t    || _d| _i | _t | _g | _d| _d S )Nr   )	superr  r~  op_count	var_namesr   	_used_ops_read_names_nontrivial_read_countr  	__class__r#   r$   r  &  s   

zOpCounterCSE.__init__r1   r   r  r$  r%  r&  r   r   c                 C  s,   | j | t| jt| j||i |S r   )r  r   r  r  _update_countr@  r~  r'  r#   r#   r$   r(  /  s   zOpCounterCSE._defaultc                 C  s8   | j |}|sd| j }|  jd7  _|| j |< |S )Nr  r   )r  getr  )r.   r  r  r#   r#   r$   r  5  s   
zOpCounterCSE._update_countc                 O  s   | j d | jj|i |S )Nr\   )r  r   r~  r\   r1  r#   r#   r$   r\   =  s   zOpCounterCSE.indirect_indexingr]   rF   c                 C  sX   | j ||}|| jvr'| jd | j| t|tj	t
fs'|  jd7  _| |S )Nr^   r   )r~  r^   r  r  r   r  rp  r   r    r  r  r  r  )r.   r1   r]   r  r#   r#   r$   r^   A  s   

zOpCounterCSE.loadr2   r   c                 C  s:   | j ||}|| jvr| jd | j| | |S )Nr3   )r~  r3   r  r  r   r  rp  r  )r.   r1   r2   r  r#   r#   r$   r3   J  s
   

zOpCounterCSE.load_seedNrl   rs   rt   ru   rv   r*   rw   rL   rx   ry   rz   r{   c           	   	   C  s`   | j |||||||}|| jvr+| jd | j|d  |dur+| j|d  | |S )z3
        See [Note: Inductor bucketize op]
        r|   r   N)r~  r|   r  r  r   r  rp  r  )	r.   rl   rs   ru   rv   rw   rx   rz   r  r#   r#   r$   r|   Q  s   
	
zOpCounterCSE.bucketizec                 C  s   t | j| j| j| jS r   )r  r  r  r  r  r  r#   r#   r$   rI  n  s   zOpCounterCSE.getvaluer  rP  )r1   r   r]   rF   r   r   )r1   r   r2   r   r  r  )r  r  r  r  r  r(  r  r\   r^   r3   r|   rI  __classcell__r#   r#   r  r$   r  #  s    
	

	r  c                   @  s    e Zd ZdddZdddZdS )ExtractConstantsHandlerdeviceOptional[torch.device]c                 C  r  r   )r  )r.   r  r#   r#   r$   r  u  r  z ExtractConstantsHandler.__init__r'   r   r)   r*   r   torch._inductor.ir.Constantc                 C  s&   ddl m} |j||| jpt dS )Nr   )r  )r'   r)   r  )Ztorch._inductorr  Constantr  r  Zget_default_device)r.   r'   r)   r  r#   r#   r$   r/   x  s   z ExtractConstantsHandler.constantN)r  r  )r'   r   r)   r*   r   r  )r  r  r  r  r/   r#   r#   r#   r$   r  t  r  r  c                      sN   e Zd ZdZd fddZd fdd	ZdddZdddZdddZ  Z	S )SimpleCSEHandlerzWraps 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.
    r  r   c                   s   t  | i | _t | _d S r   )r  r  	cse_cacherk  mockr  r  r#   r$   r    s   zSimpleCSEHandler.__init__r   rF   c                   s   t  j|i |S r   )r  r\   r1  r  r#   r$   r\     r  z"SimpleCSEHandler.indirect_indexingra   c                 O     t dNzstore not implementedr,   r1  r#   r#   r$   rb        zSimpleCSEHandler.storec                 O  r  r  r,   r1  r#   r#   r$   rg     r  z SimpleCSEHandler.store_reductionr1   r   r  r$  r%  r&  c                 C  sR   t | j||i |}| j|}|d ur|S t | j||i |}|| j|< |S r   )r@  r  r  r  r  )r.   r1   r  r%  keyr  r#   r#   r$   r(    s   
zSimpleCSEHandler._default)r  r   )r   rF   rb  rP  )
r  r  r  r  r  r\   rb   rg   r(  r  r#   r#   r  r$   r    s    

r  )r   r   r   r   )r  r  )5
__future__r   r6  r  rer-  ior   typingr   r   r   r   r   r	   r
   r   Zunittest.mockr   r    r  Ztorch.utils._pytreeutilsZ_pytreer  Zutils._ordered_setr   r   r   r   r   r   r`   rd   r%   r&   compile	fullmatchr  r!  r>  r#  rO  rS  re  rk  r}  r  r  r  r  r  r  r#   r#   r#   r$   <module>   sP   (
     @
HO$BQ