
    ɯwg(y                        d dl Z d dlmZmZmZmZmZmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlZd dlZd dlmc 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de fdZ! G d dee         Z" G d d      Z#de#de"d   fdZ$ G d d      Z%e%jM                          de%de"e    fdZ' G d d      Z(de(de"e    fdZ) G d dee         Z*de*e   de"e   fdZ+ G d de*e         Z,de,e   de"e   fdZ- G d  d!e      Z. G d" d#      Z/de/de"e    fd$Z0 G d% d&e#      Z1de1de"e   fd'Z2 G d( d)e*e         Z3de3e   de"e   fd*Z4y)+    N)AnyCallableDictGenericListLiteral
NamedTupleOptionalTupleTypeVarUnion)Protocol)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_sumreturnc                 b    t        | t        j                        rt        |       S t	        |       S N)
isinstancesympyExprr   str)as    `/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/torch/_inductor/ops_handler.py_arg_strr,   ,   s#    !UZZ |q6M    c                   v   e Zd ZdZdeeeef   dej                  de
fdZdede
fdZd	e
de
de
fd
Zd	e
de
de
fdZd	e
de
de
de
de
f
dZde
deg e
f   de
de
fdZde
de
de
de
fdZdej*                  dej                  de
fdZ	 	 dde
dej                  deej                     de
fdZde
dej                  de
fdZde
dej                  de
fdZde
dej                  de
fdZde
dej                  de
fdZde
dej                  dej                  de
fd Zde
de
fd!Z	 dde
d"ej*                  d#edej*                  fd$Zded%ej*                  de
fd&Z 	 dded%ej*                  de
d'e!ddf
d(Z"dej                  dej                  d)e#de
dee
e$e
d*f   f   f
d+Z%ded%ej*                  de
de
fd,Z&d-e$ej                  d*f   d.ee$e
d*f   e$e
d*f   ge$e
d*f   f   d/e$e
d*f   de$e
d*f   fd0Z'd-e$ej                  d*f   d/e$e
d*f   d1ed2ede$e
d*f   f
d3Z(d/e
d4ed5ej*                  d6ej                  d7ede
fd8Z)d9e
de
fd:Z*d9e
de
fd;Z+d9e
de
fd<Z,d9e
de
fd=Z-d9e
de
fd>Z.d9e
de
fd?Z/d9e
d@e
de
fdAZ0d9e
d@e
de
fdBZ1d9e
de
fdCZ2d9e
de
fdDZ3d9e
de
fdEZ4d9e
de
fdFZ5d9e
de
fdGZ6d9e
de
fdHZ7d9e
de
fdIZ8d9e
de
fdJZ9d9e
de
fdKZ:d9e
de
fdLZ;d9e
d@e
de
fdMZ<d9e
de
fdNZ=d9e
de
fdOZ>d9e
d@e
de
fdPZ?d9e
de
fdQZ@d9e
de
fdRZAd9e
fdSZBd9e
d@e
de
fdTZCd9e
de
fdUZDd9e
de
fdVZEd9e
d@e
de
fdWZFd9e
d@e
de
fdXZGd9e
de
fdYZHd9e
d@e
de
fdZZId9e
d@e
de
fd[ZJd9e
d@e
de
fd\ZKd9e
de
fd]ZLd9e
d@e
de
fd^ZMd9e
d@e
de
fd_ZNd9e
d@e
de
fd`ZOd9e
d@e
de
fdaZPd9e
de
fdbZQd9e
de
fdcZRd9e
de
fddZSd9e
de
fdeZTd9e
de
fdfZUd9e
de
fdgZVd9e
d@e
de
fdhZWd9e
de
fdiZXd9e
de
fdjZYd9e
de
fdkZZd9e
de
fdlZ[d9e
de
fdmZ\d9e
de
fdnZ]d9e
de
fdoZ^d9e
de
fdpZ_d9e
de
fdqZ`d9e
de
fdrZad9e
d@e
de
fdsZbd9e
d@e
de
fdtZcd9e
d@e
de
fduZdd9e
d@e
de
fdvZed9e
d@e
de
fdwZfd9e
d@e
de
fdxZgd9e
d@e
de
fdyZhd9e
d@e
de
fdzZid9e
d@e
de
fd{Zjd9e
d@e
de
fd|Zkd9e
d@e
de
fd}Zld9e
d@e
de
fd~Zmd9e
d@e
de
fdZnd9e
d@e
de
fdZod9e
d@e
de
fdZpd9e
d@e
de
fdZqd9e
d@e
de
fdZrd9e
de
fdZsde
de
fdZtde
de
fdZude
de
fdZvde
de
fdZwde
de
fdZxde
de
fdZyde
de
fdZzde
de
de
de
fdZ{de
de
de
fdZ|de
de
de
fdZ}de
de
de
fdZ~de
de
de
fdZde
de
fdZde
de
fdZde
de
fdZde
de
fdZde
de
fdZde
de
fdZde
de
fdZde
de
fdZde
de
fdZde
de
fdZde
de
fdZde
de
de
fdZde
de
fdZde
de
fdZde
de
fdZde
de
de
fdZde
de
de
fdZde
de
de
fdZde
de
de
fdZde
de
de
fdZde
de
de
fdZde
de
de
fdZde
de
de
fdZde
de
de
fdZde
de
de
fdZde
de
de
fdZde
de
de
fdZde
de
de
fdZd9e
d@e
de
fdZd9e
d@e
de
fdZd9e
d@e
de
fdZd9e
d@e
de
fdZd9e
d@e
de
fdZd9e
d@e
de
fdZd9e
d@e
de
fdZd9e
d@e
de
fdZd9e
de
fdZd9e
de
fdZd9e
de
fdZd9e
de
fdZd9e
de
fdZd9e
de
fdZd9e
de
fdZy)
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 of 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 ``__getattr__`` metaprogramming, which means
    that you cannot declare that a type implements a protocol by inheriting from
    it (as the type stubs count as attribute declarations and impede the getattr
    magic method from being called).  Instead, define a function that casts an
    argument of your type to the protocol, which is sufficient to induce mypy to
    test that the protocol is implemented correctly.  Search for ``_typecheck_``
    in this file to see some examples.  If you see an obscure error where a
    class doesn't implement a Protocol, but mypy doesn't say why, check to see
    that ``__getattr__`` is typed correctly (typically, it is not possible to
    type ``__getattr__`` without typing it as ``Callable[..., Any]``)
    valuedtyper#   c                      y)z)Produces a scalar constant of type dtype.N )selfr0   r1   s      r+   constantzOpsHandler.constant[       r-   nameoffsetc                      y)z$Computes inductor_prims.lookup_seed.Nr3   )r4   r7   r8   s      r+   	load_seedzOpsHandler.load_seed_   r6   r-   seedc                      y)zIComputes inductor_prims.random with mode="rand".  offset has dtype int32.Nr3   r4   r;   r8   s      r+   randzOpsHandler.randc   r6   r-   c                      y)zJComputes inductor_prims.random with mode="randn".  offset has dtype int32.Nr3   r=   s      r+   randnzOpsHandler.randng   r6   r-   lowhighc                      y)z9Computes inductor_prims.randint.  offset has dtype int32.Nr3   )r4   r;   r8   rA   rB   s        r+   	randint64zOpsHandler.randint64k   r6   r-   maskbodyotherc                      y)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.
        Nr3   )r4   rE   rF   rG   s       r+   maskedzOpsHandler.maskedo        	r-   	conditioninputc                      y)ze
        Computes torch.where: when condition is true, return input; otherwise return other.
        Nr3   )r4   rK   rL   rG   s       r+   wherezOpsHandler.where}        	r-   exprc                      y)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.
        Nr3   )r4   rP   r1   s      r+   
index_exprzOpsHandler.index_expr   s     	r-   Nx	src_dtypec                      y)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)).
        Nr3   )r4   rS   r1   rT   use_compute_typess        r+   to_dtypezOpsHandler.to_dtype        	r-   c                      y)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.
        Nr3   r4   rS   r1   s      r+   trunc_to_intzOpsHandler.trunc_to_int   rJ   r-   c                      yzT
        Convert x to dtype with ceiling semantics.  See also trunc_to_int.
        Nr3   rZ   s      r+   ceil_to_intzOpsHandler.ceil_to_int   rO   r-   c                      yr]   r3   rZ   s      r+   floor_to_intzOpsHandler.floor_to_int   rO   r-   c                      y)zZ
        Convert x to dtype with round-to-even semantics.  See also trunc_to_int.
        Nr3   rZ   s      r+   round_to_intzOpsHandler.round_to_int   rO   r-   c                      y)z
        Reinterpret cast x to dtype (reinterpreting the bits in memory as another dtype.)
        src_dtype must be the original type of x.
        Nr3   )r4   rS   r1   rT   s       r+   to_dtype_bitcastzOpsHandler.to_dtype_bitcast       
 	r-   c                      y)z@
        Returns x as is.  This is used to trigger CSE.
        Nr3   r4   rS   s     r+   identityzOpsHandler.identity   rO   r-   sizecheckc                      y)a  
        Convert an integral x into a sympy.Expr that can be subsequently used in
        indexing computation.  'size' represents an upper bound on the 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).
        Nr3   )r4   rS   ri   rj   wrap_negs        r+   indirect_indexingzOpsHandler.indirect_indexing   rX   r-   indexc                      y)zc
        Load from the memory location 'name', offset by some indexing expression 'index'.
        Nr3   )r4   r7   rn   s      r+   loadzOpsHandler.load   rO   r-   modec                      y)z
        Store 'value' to the memory location 'name' offset by 'expr'.  If
        specified, 'mode' can require the store to be an atomic addition.
        Nr3   )r4   r7   rn   r0   rq   s        r+   storezOpsHandler.store   rX   r-   reduction_type.c                      y)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.
        Nr3   )r4   r1   rT   rt   r0   s        r+   	reductionzOpsHandler.reduction   s    " 	r-   c                      y)z{
        Store the fully accumulated result of 'reduction' to the memory
        location 'name' offset by 'expr'.
        Nr3   )r4   r7   rn   r0   s       r+   store_reductionzOpsHandler.store_reduction  re   r-   dtypes
combine_fnvaluesc                      y)z9
        Perform an associative scan on 'value'.
        Nr3   )r4   ry   rz   r{   s       r+   scanzOpsHandler.scan       	r-   stable
descendingc                      y)z<
        Sort values along the reduction dimension.
        Nr3   )r4   ry   r{   r   r   s        r+   sortzOpsHandler.sort  r~   r-   offsets_nameoffsets_sizeindexing_dtyperightc                      y r%   r3   )r4   r{   r   r   r   r   s         r+   	bucketizezOpsHandler.bucketize&  s     	r-   x0c                      y r%   r3   r4   r   s     r+   abszOpsHandler.abs5      r-   c                      y r%   r3   r   s     r+   expzOpsHandler.exp8  r   r-   c                      y r%   r3   r   s     r+   exp2zOpsHandler.exp2;  r   r-   c                      y r%   r3   r   s     r+   expm1zOpsHandler.expm1>  r   r-   c                      y r%   r3   r   s     r+   sqrtzOpsHandler.sqrtA  r   r-   c                      y r%   r3   r   s     r+   reluzOpsHandler.reluD  r   r-   x1c                      y r%   r3   r4   r   r   s      r+   minimumzOpsHandler.minimumG  r   r-   c                      y r%   r3   r   s      r+   maximumzOpsHandler.maximumJ  r   r-   c                      y r%   r3   r   s     r+   coszOpsHandler.cosM  r   r-   c                      y r%   r3   r   s     r+   sinzOpsHandler.sinP  r   r-   c                      y r%   r3   r   s     r+   lgammazOpsHandler.lgammaS  r   r-   c                      y r%   r3   r   s     r+   erfzOpsHandler.erfV  r   r-   c                      y r%   r3   r   s     r+   coshzOpsHandler.coshY  r   r-   c                      y r%   r3   r   s     r+   sinhzOpsHandler.sinh\  r   r-   c                      y r%   r3   r   s     r+   acoszOpsHandler.acos_  r   r-   c                      y r%   r3   r   s     r+   acoshzOpsHandler.acoshb  r   r-   c                      y r%   r3   r   s     r+   asinzOpsHandler.asine  r   r-   c                      y r%   r3   r   s     r+   asinhzOpsHandler.asinhh  r   r-   c                      y r%   r3   r   s      r+   atan2zOpsHandler.atan2k  r   r-   c                      y r%   r3   r   s     r+   atanzOpsHandler.atann  r   r-   c                      y r%   r3   r   s     r+   atanhzOpsHandler.atanhq  r   r-   c                      y r%   r3   r   s      r+   copysignzOpsHandler.copysignt  r   r-   c                      y r%   r3   r   s     r+   erfczOpsHandler.erfcw  r   r-   c                      y r%   r3   r   s     r+   erfinvzOpsHandler.erfinvz  r   r-   c                      y r%   r3   r   s     r+   frexpzOpsHandler.frexp}  r   r-   c                      y r%   r3   r   s      r+   hypotzOpsHandler.hypot  r   r-   c                      y r%   r3   r   s     r+   log10zOpsHandler.log10  r   r-   c                      y r%   r3   r   s     r+   log2zOpsHandler.log2  r   r-   c                      y r%   r3   r   s      r+   	nextafterzOpsHandler.nextafter  r   r-   c                      y r%   r3   r   s      r+   logical_andzOpsHandler.logical_and  r   r-   c                      y r%   r3   r   s     r+   logical_notzOpsHandler.logical_not  r   r-   c                      y r%   r3   r   s      r+   
logical_orzOpsHandler.logical_or  r   r-   c                      y r%   r3   r   s      r+   logical_xorzOpsHandler.logical_xor  r   r-   c                      y r%   r3   r   s      r+   bitwise_andzOpsHandler.bitwise_and  r   r-   c                      y r%   r3   r   s     r+   bitwise_notzOpsHandler.bitwise_not  r   r-   c                      y r%   r3   r   s      r+   
bitwise_orzOpsHandler.bitwise_or  r   r-   c                      y r%   r3   r   s      r+   bitwise_xorzOpsHandler.bitwise_xor  r   r-   c                      y r%   r3   r   s      r+   bitwise_left_shiftzOpsHandler.bitwise_left_shift  r   r-   c                      y r%   r3   r   s      r+   bitwise_right_shiftzOpsHandler.bitwise_right_shift  r   r-   c                      y r%   r3   r   s     r+   rsqrtzOpsHandler.rsqrt  r   r-   c                      y r%   r3   r   s     r+   log1pzOpsHandler.log1p  r   r-   c                      y r%   r3   r   s     r+   tanzOpsHandler.tan  r   r-   c                      y r%   r3   r   s     r+   tanhzOpsHandler.tanh  r   r-   c                      y r%   r3   r   s     r+   sigmoidzOpsHandler.sigmoid  r   r-   c                      y r%   r3   r   s     r+   signbitzOpsHandler.signbit  r   r-   c                      y r%   r3   r   s      r+   fmodzOpsHandler.fmod  r   r-   c                      y r%   r3   r   s     r+   logzOpsHandler.log  r   r-   c                      y r%   r3   r   s     r+   isinfzOpsHandler.isinf  r   r-   c                      y r%   r3   r   s     r+   isnanzOpsHandler.isnan  r   r-   c                      y r%   r3   r   s     r+   roundzOpsHandler.round  r   r-   c                      y r%   r3   r   s     r+   floorzOpsHandler.floor  r   r-   c                      y r%   r3   r   s     r+   signzOpsHandler.sign  r   r-   c                      y r%   r3   r   s     r+   trunczOpsHandler.trunc  r   r-   c                      y r%   r3   r   s     r+   ceilzOpsHandler.ceil  r   r-   c                      y r%   r3   r   s     r+   negzOpsHandler.neg  r   r-   c                      y r%   r3   r   s     r+   
reciprocalzOpsHandler.reciprocal  r   r-   c                      y r%   r3   r   s      r+   eqzOpsHandler.eq  r   r-   c                      y r%   r3   r   s      r+   nezOpsHandler.ne  r   r-   c                      y r%   r3   r   s      r+   ltzOpsHandler.lt  r   r-   c                      y r%   r3   r   s      r+   gtzOpsHandler.gt  r   r-   c                      y r%   r3   r   s      r+   lezOpsHandler.le  r   r-   c                      y r%   r3   r   s      r+   gezOpsHandler.ge  r   r-   c                      y r%   r3   r   s      r+   addzOpsHandler.add  r   r-   c                      y r%   r3   r   s      r+   subzOpsHandler.sub  r   r-   c                      y r%   r3   r   s      r+   mulzOpsHandler.mul  r   r-   c                      y r%   r3   r   s      r+   powzOpsHandler.pow  r   r-   c                      y r%   r3   r   s      r+   and_zOpsHandler.and_  r   r-   c                      y r%   r3   r   s      r+   or_zOpsHandler.or_  r   r-   c                      y r%   r3   r   s      r+   xorzOpsHandler.xor  r   r-   c                      y r%   r3   r   s      r+   lshiftzOpsHandler.lshift  r   r-   c                      y r%   r3   r   s      r+   rshiftzOpsHandler.rshift  r   r-   c                      y r%   r3   r   s      r+   getitemzOpsHandler.getitem  r6   r-   c                      y r%   r3   r   s      r+   matmulzOpsHandler.matmul  r6   r-   c                      y r%   r3   r   s     r+   invertzOpsHandler.invert  r   r-   c                      y r%   r3   rg   s     r+   airy_aizOpsHandler.airy_ai!  r   r-   c                      y r%   r3   rg   s     r+   	bessel_j0zOpsHandler.bessel_j0$  r   r-   c                      y r%   r3   rg   s     r+   	bessel_j1zOpsHandler.bessel_j1'  r   r-   c                      y r%   r3   rg   s     r+   	bessel_y0zOpsHandler.bessel_y0*  r   r-   c                      y r%   r3   rg   s     r+   	bessel_y1zOpsHandler.bessel_y1-  r   r-   c                      y r%   r3   rg   s     r+   digammazOpsHandler.digamma0  r   r-   c                      y r%   r3   rg   s     r+   erfcxzOpsHandler.erfcx3  r   r-   yzc                      y r%   r3   )r4   rS   r0  r1  s       r+   fmazOpsHandler.fma6  r   r-   c                      y r%   r3   r4   rS   r0  s      r+   igammazOpsHandler.igamma9  r   r-   c                      y r%   r3   r5  s      r+   igammaczOpsHandler.igammac<  r   r-   c                      y r%   r3   r5  s      r+   gammainczOpsHandler.gammainc?  r   r-   c                      y r%   r3   r5  s      r+   	gammaincczOpsHandler.gammainccB  r   r-   c                      y r%   r3   rg   s     r+   i0zOpsHandler.i0E  r   r-   c                      y r%   r3   rg   s     r+   i0ezOpsHandler.i0eH  r   r-   c                      y r%   r3   rg   s     r+   i1zOpsHandler.i1K  r   r-   c                      y r%   r3   rg   s     r+   i1ezOpsHandler.i1eN  r   r-   c                      y r%   r3   rg   s     r+   log_ndtrzOpsHandler.log_ndtrQ  r   r-   c                      y r%   r3   rg   s     r+   modified_bessel_i0zOpsHandler.modified_bessel_i0T  r   r-   c                      y r%   r3   rg   s     r+   modified_bessel_i1zOpsHandler.modified_bessel_i1W  r   r-   c                      y r%   r3   rg   s     r+   modified_bessel_k0zOpsHandler.modified_bessel_k0Z  r   r-   c                      y r%   r3   rg   s     r+   modified_bessel_k1zOpsHandler.modified_bessel_k1]  r   r-   c                      y r%   r3   rg   s     r+   ndtrzOpsHandler.ndtr`  r   r-   c                      y r%   r3   rg   s     r+   ndtrizOpsHandler.ndtric  r   r-   c                      y r%   r3   r5  s      r+   	polygammazOpsHandler.polygammaf  r   r-   c                      y r%   r3   rg   s     r+   scaled_modified_bessel_k0z$OpsHandler.scaled_modified_bessel_k0i  r   r-   c                      y r%   r3   rg   s     r+   scaled_modified_bessel_k1z$OpsHandler.scaled_modified_bessel_k1l  r   r-   c                      y r%   r3   rg   s     r+   spherical_bessel_j0zOpsHandler.spherical_bessel_j0o  r   r-   c                      y r%   r3   r5  s      r+   zetazOpsHandler.zetar  r   r-   c                      y r%   r3   r5  s      r+   chebyshev_polynomial_tz!OpsHandler.chebyshev_polynomial_tu  r   r-   c                      y r%   r3   r5  s      r+   chebyshev_polynomial_uz!OpsHandler.chebyshev_polynomial_ux  r   r-   c                      y r%   r3   r5  s      r+   chebyshev_polynomial_vz!OpsHandler.chebyshev_polynomial_v{  r   r-   c                      y r%   r3   r5  s      r+   chebyshev_polynomial_wz!OpsHandler.chebyshev_polynomial_w~  r   r-   c                      y r%   r3   r5  s      r+   legendre_polynomial_pz OpsHandler.legendre_polynomial_p  r   r-   c                      y r%   r3   r5  s      r+   shifted_chebyshev_polynomial_tz)OpsHandler.shifted_chebyshev_polynomial_t  r   r-   c                      y r%   r3   r5  s      r+   shifted_chebyshev_polynomial_uz)OpsHandler.shifted_chebyshev_polynomial_u  r   r-   c                      y r%   r3   r5  s      r+   shifted_chebyshev_polynomial_vz)OpsHandler.shifted_chebyshev_polynomial_v  r   r-   c                      y r%   r3   r5  s      r+   shifted_chebyshev_polynomial_wz)OpsHandler.shifted_chebyshev_polynomial_w  r   r-   c                      y r%   r3   r5  s      r+   hermite_polynomial_hzOpsHandler.hermite_polynomial_h  r   r-   c                      y r%   r3   r5  s      r+   hermite_polynomial_hez OpsHandler.hermite_polynomial_he  r   r-   c                      y r%   r3   r5  s      r+   laguerre_polynomial_lz OpsHandler.laguerre_polynomial_l  r   r-   c                      y)zC-style trunc division between integers only.  Computes the true
        division of two numbers and rounds the result to zero.
        Nr3   r   s      r+   truncdivzOpsHandler.truncdiv  rO   r-   c                      y)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.
        Nr3   r   s      r+   floordivzOpsHandler.floordiv  re   r-   c                      y)zTrue division between floats.  Integer inputs are NOT valid.  To
        do Python-style (int, int) -> float division, use int_truedivNr3   r   s      r+   truedivzOpsHandler.truediv       	r-   c                      y)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.
        Nr3   r   s      r+   int_truedivzOpsHandler.int_truediv  re   r-   c                      y)zbTODO: to be removed.  This renders as / no matter what the backend is
        which is incoherent.Nr3   r   s      r+   divzOpsHandler.div  r{  r-   c                      y)z)C-style modulus, take sign from LHS (x0).Nr3   r   s      r+   modzOpsHandler.mod  r6   r-   c                      y)z.Python-style modulus, take sign from RHS (x1).Nr3   r   s      r+   	remainderzOpsHandler.remainder  r6   r-   c                      y)z(Python-style round with decimal argumentNr3   r   s      r+   round_decimalzOpsHandler.round_decimal  r6   r-   c                      y r%   r3   r   s     r+   libdevice_abszOpsHandler.libdevice_abs  r   r-   c                      y r%   r3   r   s     r+   libdevice_expzOpsHandler.libdevice_exp  r   r-   c                      y r%   r3   r   s     r+   libdevice_sqrtzOpsHandler.libdevice_sqrt  r   r-   c                      y r%   r3   r   s     r+   libdevice_coszOpsHandler.libdevice_cos  r   r-   c                      y r%   r3   r   s     r+   libdevice_sinzOpsHandler.libdevice_sin  r   r-   c                      y r%   r3   r   s     r+   libdevice_sigmoidzOpsHandler.libdevice_sigmoid  r   r-   c                      y r%   r3   r   s     r+   libdevice_logzOpsHandler.libdevice_log  r   r-   )NTTTr%   )__name__
__module____qualname____doc__r   boolfloatinttorchr1   r   r5   r)   r:   r>   r@   rD   r   rI   rN   r'   r(   rR   r
   rW   r[   r^   r`   rb   rd   rh   rm   rp   	StoreModers   ReductionTyper   rv   rx   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/  r3  r6  r8  r:  r<  r>  r@  rB  rD  rF  rH  rJ  rL  rN  rP  rR  rT  rV  rX  rZ  r\  r^  r`  rb  rd  rf  rh  rj  rl  rn  rp  rr  rt  rv  rx  rz  r}  r  r  r  r  r  r  r  r  r  r  r  r3   r-   r+   r/   r/   8   s@    DeD%$45 ekk a c 1  A ! ! Q 1 a   !  1 HRUO A ! q  1  uzz %++ !  ,0 {{ EKK(	 
a   Q u{{ q a   a   ! EKK EKK TU !    DH**-1	 UZZ A   zz 	
  
 {{ ;; &	
  
q%3-	 ,C 

 1  
ekk3&'
 eAsFmU1c6];U1c6]JK
 af	

 
q#v

ekk3&'
 af
 	

 
 
q#v
		 	 jj		
 	 	 
	a A a A q Q  a q Q q Q !  q !  q a A a A  q a A q Q q Q q Q  a q Q  a  q Q q Q  a 1 !  q Q  q   q Q  a q Q A 1  a Q 1 a A Q A ! a Q 1 a Q 1 a A Q A ! a Q 1 Q A ! a Q 1  a  a a A q Q !  !  q a A a A  a  a 
 a  a q Q  a q Q a A Q 1 Q A ! Q A ! Q A ! Q A ! Q A ! Q A ! a Q 1 a Q 1 a Q 1 a Q 1 q a A a Q 1 a Q 1   a   a !  q   a  q  q 1  1  1  1   q q Q Q 1  q  a A  q Q !  a 1  q A ! Q 1 A ! Q 1 !  A ! A ! A ! A ! a A q Q 1  q 1  1  Q 1 a A !  a A  a A  a A  a A q Q 1  a A  a A  a A  a A a A ! q Q 1 q Q 1 1 !  1 !  !  q 
a Q 1 a Q 1 
a Q 1 A 1   q Q " a  a  q  a  a A !  a r-   r/   c                       e Zd Zd Zedd       Zeded   fd       Zeded   fd       Zeded   fd	       Z	edde
j                  fd
       Zy)NoopHandlerc                     |dk(  ryd }|S )Nr7   r  c                       y r%   r3   )argskwargss     r+   innerz&NoopHandler.__getattr__.<locals>.inner  s    r-   r3   r4   r7   r  s      r+   __getattr__zNoopHandler.__getattr__  s    6> 	 r-   r#   Nc                      y r%   r3   rE   rF   rG   s      r+   rI   zNoopHandler.masked  s    r-   NNc                      y)Nr  r3   rS   s    r+   r   zNoopHandler.frexp  s    r-   )N.c                     dt        |      z  S Nr%   lenry   rz   r{   s      r+   r}   zNoopHandler.scan      V$$r-   c                     dt        |      z  S r  r  ry   r{   r   r   s       r+   r   zNoopHandler.sort  r  r-   c                 ,    t        j                  d      S Nr   )r'   Integer	index_varri   rj   rl   s       r+   rm   zNoopHandler.indirect_indexing  s    }}Qr-   )r#   Nr  )r  r  r  r  staticmethodrI   r   r   r}   r   r'   Symbolrm   r3   r-   r+   r  r    s       E*%   %E),< % % %E)4D % %      r-   r  hc                     | S r%   r3   r  s    r+   _typecheck_NoopHandlerr        Hr-   c                       e Zd Zd Zedefd       Zed        Zed        Zed        Z	ed
de
j                  fd       Zed        Zy	)MockHandlerc                     dk(  ryfd}|S )Nr7   r  c                      | D cg c]  }t        |       }}|j                  d |j                         D               d ddj                  |       dS c c}w )Nc              3   0   K   | ]  \  }}| d |   yw)=Nr3   ).0kvs      r+   	<genexpr>z9MockHandler.__getattr__.<locals>.inner.<locals>.<genexpr>  s     ?1A3as?s   zops.(, ))r,   extenditemsjoin)r  r  r*   fargsr7   s       r+   r  z&MockHandler.__getattr__.<locals>.inner  sV    *./QXa[/E/LL???$q5!1 2!44 0s   Ar3   r  s    ` r+   r  zMockHandler.__getattr__  s    6> 	5
 r-   r#   c                 $    d|  d |        d| dS )Nzops.masked(r  r  r3   r  s      r+   rI   zMockHandler.masked  s    TF"TVHBugQ77r-   c                     d|  dd|  dfS )Nz
ops.frexp(z)[0]z)[1]r3   r  s    r+   r   zMockHandler.frexp  s    QCt$
1#T&:;;r-   c                 X     t         fdt        t                    D              S )Nc           
   3   <   K   | ]  }d  d d d| d	  yw)z	ops.scan(r  )[]Nr3   )r  irz   ry   r{   s     r+   r  z#MockHandler.scan.<locals>.<genexpr>%  s4      
 xr*Rxr!A>
s   tupleranger  r  s   ```r+   r}   zMockHandler.scan#  s&     
3v;'
 
 	
r-   c                 \     t         fdt        t                    D              S )Nc              3   B   K   | ]  }d  d d d d| d  yw)z	ops.sort(r  z	, stable=z, descending=r  r  Nr3   )r  r  r   ry   r   r{   s     r+   r  z#MockHandler.sort.<locals>.<genexpr>,  s>      
 xr&6(-
|SUVWUXXYZ
s   r  r  s   ````r+   r   zMockHandler.sort*  s&     
3v;'
 
 	
r-   c                 *    t        t        |             S r%   )r   r)   r  s       r+   rm   zMockHandler.indirect_indexing1  s    !#i.11r-   c                     d }i dddddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&j                         D ]  \  }}t        | | ||              y )'Nc                 $     t          fd       }|S )Nc                  "     j                   |  S r%   )format)r  format_strings    r+   r  z:MockHandler._init_cls.<locals>.make_handler.<locals>.inner8  s    +}++T22r-   )r  )r  r  s   ` r+   make_handlerz+MockHandler._init_cls.<locals>.make_handler7  s    3 3 Lr-   r  z{} + {}r  z{} - {}r  z{} * {}rx  z{} // {}rz  z{} / {}r  z{} % {}r  z{} ** {}r  z{} << {}r  z{} >> {}r  z{} & {}r  z{} | {}r  z{} ^ {}r   z{} == {}r  z{} != {}r  z{} < {}r  z{} > {}r  z{} <= {}z{} >= {}z-{})r	  r   )r  setattr)clsr  r7   r  s       r+   	_init_clszMockHandler._init_cls5  s   	$
9$
9$
 9$
 
	$

 y$
 9$
 :$
 j$
 j$
 I$
 9$
 9$
 *$
 *$
 )$
  )!$
" *#$
$ '$
( %')	<D-* C|M:;+	<r-   Nr  )r  r  r  r  r  r)   rI   r   r}   r   r'   r  rm   classmethodr  r3   r-   r+   r  r    s    	 8S 8 8 < < 
 
 
 
 2 2 2 < <r-   r  c                     | S r%   r3   r  s    r+   _typecheck_MockHandlerr  Z  r  r-   c                       e Zd Zd Zeddefd       Zdedef   fdZ	de
j                  de
j                  d	ed
eeeedf   f   deeeedf   f   f
dZd Zy)KernelFormatterHandlerc                 d    || _         t        d      | _        t        j                         | _        y )Nr   )parent_handlerr   output	itertoolscountvar_counter)r4   r  s     r+   __init__zKernelFormatterHandler.__init___  s%    ,$Q'$??,r-   Nr#   c                    ddl m} ddlm} |||gn|g}|ddgndg}t	        t                     }|j                  j                  d      5  |j                  j                  ddj                  |       d	       d d d        t        ||      D ]r  \  }}	|	s	dj                  |	D 
cg c]/  }
t        t        |
t        t        j                  f      rd
n|
      1 c}
      }|j                  j                  | d|        t |j!                  |      5  t#        j$                  |dd      5   | | }|j'                  |      cd d d        cd d d        S # 1 sw Y   xY wc c}
w # 1 sw Y   nxY wd d d        y # 1 sw Y   y xY w)Nr   )FlexibleLayout)Vrn   rindexzdef inner_fn(r  z):_ = allow_indexingT)irr  virtualizedr  r  r  r  indent	writeliner  zipr)   r&   r  r'   r  set_ops_handlerr   objectgetvalue)ir_fnrn   r  r  r  r  names	formatterr7   argr  lhsresults                r+   ir_to_stringz#KernelFormatterHandler.ir_to_stringd  s~   &""("4v5''-'9(#y*;=9	$$R( 	M&&tyy7G6H'KL	MUD) 	>ID#ii "% :a#u}}1E#FCAN   **cU#dV+<=	> y) 	.5<<,d,
 	. D\F%%f-		. 	. 	.	M 	M
	. 	. 	. 	. 	.s6   /E(4E#
E=.E(	E=E (E1	-E==F.c                       fd}|S )Nc                       t        j                        | i |}dk(  r|S fd}t        j                  ||      S )Nrm   c                 x    dt        j                         }j                  j                  | d|         |S )Ntmpr  )nextr  r  r  )linevarnamer4   s     r+   writez@KernelFormatterHandler.__getattr__.<locals>.inner.<locals>.write  s=    T%5%5 678%%	TF&;<r-   )getattrr  pytreetree_map)r  r  r  r  r7   r4   s       r+   r  z1KernelFormatterHandler.__getattr__.<locals>.inner  sG    574..5tFvFD** ??5$//r-   r3   r  s   `` r+   r  z"KernelFormatterHandler.__getattr__  s    	0 r-   r1   rT   rt   r0   c                 B   | j                   j                  ||||      }t        |      }t        |      D cg c]  }dt	        | j
                          }}| j                  j                  dj                  |       d|        |dkD  rt        |      S |d   S c c}w )Nr  ,r  r   r   )
r  rv   r   r  r  r  r  r  r  r  )	r4   r1   rT   rt   r0   r  
num_valuesr  varnamess	            r+   rv   z KernelFormatterHandler.reduction  s     "",,UI~uU*>:
<A*<MNqc$t//012NN(!3 4Cv>?",q.uXAhqkA Os   Bc                 r    | j                   j                  d|        | j                   j                         S )Nzreturn )r  r  r  )r4   r	  s     r+   r  zKernelFormatterHandler.getvalue  s.    x01{{##%%r-   r%   )r  r  r  r  r  r)   r
  r   r   r  r  r1   r  r   r   rv   r  r3   r-   r+   r  r  ^  s    -
 .3 . .48CH#5  B{{B ;;B &	B
 S%S/)*B 
sE#s(O#	$B&r-   r  c                     | S r%   r3   r  s    r+   !_typecheck_KernelFormatterHandlerr    r  r-   c                   $    e Zd Zdee   fdZd Zy)WrapperHandlerr  c                     || _         y r%   )_inner)r4   r  s     r+   r  zWrapperHandler.__init__  s	    r-   c                 .    t        | j                  |      S r%   r  r   )r4   items     r+   r  zWrapperHandler.__getattr__  s    t{{D))r-   N)r  r  r  r/   r   r  r  r3   r-   r+   r  r    s    jm *r-   r  c                     | S r%   r3   r  s    r+   _typecheck_WrapperHandlerr%    r  r-   c                       e Zd Zd Zy)AddParenHandlerc                       fd}|S )Nc                  H     t        j                        | i |}d| dS )Nr  r  r"  )r  r  valr7   r4   s      r+   r  z*AddParenHandler.__getattr__.<locals>.inner  s-    ,'$++t,d=f=Cse1:r-   r3   r  s   `` r+   r  zAddParenHandler.__getattr__  s    	 r-   N)r  r  r  r  r3   r-   r+   r'  r'    s    r-   r'  c                     | S r%   r3   r  s    r+   _typecheck_AddParenHandlerr,    r  r-   c                   B    e Zd ZU eed<   ee   ed<   ee   ed<   eed<   y)OpCountResultnum_opsused_opsread_buffersnontrivial_read_countN)r  r  r  r  __annotations__r   r)   r   r3   r-   r+   r.  r.    s!    Los)r-   r.  c                        e Zd ZdZ fdZd Zd Zd Zdede	j                  defd	Zded
efdZdede	j                  dej                  defdZd Z xZS )OpCounterCSEz#Shim to count how many ops are usedc                     t         |           || _        d| _        i | _        t               | _        g | _        d| _        y r  )	superr  r  op_count	var_namesr   	_used_ops_read_names_nontrivial_read_countr4   r  	__class__s     r+   r  zOpCounterCSE.__init__  s=    #*4,&(&'#r-   c                 L      fd} j                   j                         |S )Nc                  x    t        j                  j                   t        j                        | i |      S r%   )r  r  _update_countr  r  )r  r  r7   r4   s     r+   r  z'OpCounterCSE.__getattr__.<locals>.inner  s:    ??""$FGD,?,?$F$WPV$W r-   )r:  r  r  s   `` r+   r  zOpCounterCSE.__getattr__  s!    	
 	4 r-   c                     | j                   j                  |      }|s3d| j                   }| xj                  dz  c_        || j                   |<   |S )Nr  r   )r9  getr8  )r4   r*  r  s      r+   rA  zOpCounterCSE._update_count  sJ    ..$$S)DMM?+GMMQM")DNN3r-   c                 p    | j                   j                  d        | j                  j                  |i |S )Nrm   )r:  r  r  rm   r4   r  r  s      r+   rm   zOpCounterCSE.indirect_indexing  s3    ./4t""44dEfEEr-   r7   rn   r#   c                 N   | j                   j                  ||      }|| j                  vrk| j                  j	                  d       | j
                  j                  |       t        |t        j                  t        f      s| xj                  dz  c_        | j                  |      S )Nrp   r   )r  rp   r9  r:  r  r;  appendr&   r'   r  r  r<  rA  )r4   r7   rn   r*  s       r+   rp   zOpCounterCSE.load  s    !!&&tU3dnn$NNv&##D)eemmS%9:++q0+!!#&&r-   r8   c                     | j                   j                  ||      }|| j                  vr6| j                  j	                  d       | j
                  j                  |       | j                  |      S )Nr:   )r  r:   r9  r:  r  r;  rG  rA  )r4   r7   r8   r*  s       r+   r:   zOpCounterCSE.load_seed  s]    !!++D&9dnn$NN{+##D)!!#&&r-   r   r   r   r   c                     | j                   j                  |||||      }|| j                  vr6| j                  j	                  d       | j
                  j                  |       | j                  |      S )Nr   )r  r   r9  r:  r  r;  rG  rA  )r4   r{   r   r   r   r   r*  s          r+   r   zOpCounterCSE.bucketize  sj     !!++L,
 dnn$NN{+##L1!!#&&r-   c                 n    t        | j                  | j                  | j                  | j                        S r%   )r.  r8  r:  r;  r<  )r4   s    r+   r  zOpCounterCSE.getvalue	  s,    MM4>>4+;+;T=X=X
 	
r-   )r  r  r  r  r  r  rA  rm   r)   r'   r(   rp   r   r:   r  r1   r  r   r  __classcell__r>  s   @r+   r5  r5    s    -(F' 'UZZ 'C ''c '1 '' ' jj	'
 ' ' 
r-   r5  c                     | S r%   r3   r  s    r+   _typecheck_OpCounterCSErN    r  r-   c                   :    e Zd Zd Zdedej                  ddfdZy)ExtractConstantsHandlerc                     || _         y r%   )device)r4   rR  s     r+   r  z ExtractConstantsHandler.__init__  s	    r-   r0   r1   r#   ztorch._inductor.ir.Constantc                 J    ddl m} |j                  ||| j                        S )Nr   )r  )r0   r1   rR  )torch._inductorr  ConstantrR  )r4   r0   r1   r  s       r+   r5   z ExtractConstantsHandler.constant  s    &{{eDKK{HHr-   N)r  r  r  r  r   r  r1   r5   r3   r-   r+   rP  rP    s*    Ic I%++ I:W Ir-   rP  c                     | S r%   r3   r  s    r+   "_typecheck_ExtractConstantsHandlerrW    r  r-   c                        e Zd ZdZdee   f fdZdej                  f fdZ	defdZ
defdZdedef   fd	Z x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  c                 P    t         |   |       i | _        t               | _        y r%   )r7  r  	cse_cacher  mockr=  s     r+   r  zSimpleCSEHandler.__init__(  s     =?M	r-   r#   c                 "    t        |   |i |S r%   )r7  rm   )r4   r  r  r>  s      r+   rm   z"SimpleCSEHandler.indirect_indexing-  s    w($9&99r-   c                     t        d      Nzstore not implementedNotImplementedErrorrE  s      r+   rs   zSimpleCSEHandler.store0      !"9::r-   c                     t        d      r_  r`  rE  s      r+   rx   z SimpleCSEHandler.store_reduction3  rb  r-   .c                       fd}|S )Nc                       t        j                        | i |}j                  j                  |      }||S  t        j                        | i |}|j                  |<   |S r%   )r  r\  r[  rC  r   )r  r  keyr*  r7   r4   s       r+   r  z+SimpleCSEHandler.__getattr__.<locals>.inner7  sl    *'$))T*D;F;C..$$S)C
,'$++t,d=f=C"%DNN3Jr-   r3   r  s   `` r+   r  zSimpleCSEHandler.__getattr__6  s    	 r-   )r  r  r  r  r/   r   r  r'   r(   rm   rs   rx   r   r   r  rK  rL  s   @r+   rY  rY  !  sU    "jm "
:EJJ :; ;;! ;8CH#5 r-   rY  c                     | S r%   r3   r  s    r+   _typecheck_SimpleCSEHandlerrh  D  r  r-   )5r  typingr   r   r   r   r   r   r	   r
   r   r   r   typing_extensionsr   unittest.mockr   r'   r  torch.utils._pytreeutils_pytreer  utils._ordered_setr   r   r   r   r   r   r  r  r)   r,   r/   r  r  r  r  r  r  r  r  r%  r'  r,  r.  r5  rN  rP  rW  rY  rh  r3   r-   r+   <module>rp     s       '    $ $ + W W CLW\*+		3 p
! p
f   @k j.> D< D<N    k jo @& @&H)? JsO *WQZ *!2 z!} nQ' /!"4 A J D
 D
N| 
3 Ik I*A jQTo  ~a(  F#3C#8 Z_ r-   