
    9j++                        d dl mZ d dlmZmZ d dlmZmZ d dlZd dl	Zd dl
mZ d dlmZ ddlmZmZmZ er
d d	lmZ d dlZ G d
 de      Z G d de      Zy)    )annotations)ABCabstractmethod)AnyTYPE_CHECKINGN)ir)V   )FixedLayoutFlexibleLayoutLayout)Sequencec                      e Zd ZdZ	 	 d	 	 	 	 	 ddZdddZedd       Zedd       ZddZ	ddZ
dd	Zdd
ZddZddZddZdddZedd       Zd dZed!d"d       Zy)#KernelInputsz
    Class to store and provide access to input nodes for kernels.
    This class takes in a tuple of input nodes and provides methods to access
    information about these nodes, such as their device type and device.
    Nc                n    || _         d| _        ||ni | _        || _        t	        |      dkD  sJ d       y)z
        Initialize with a tuple of input nodes.

        Args:
            input_nodes: A tuple of input nodes to store
            out_dtype: Optional output dtype to store
        Nr   z Expected at least one input node)_input_nodes_device_name_scalars
_out_dtypelen)selfinput_nodesscalars	out_dtypes       ]/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/_inductor/kernel_inputs.py__init__zKernelInputs.__init__   sB     ((,#*#6B#;!#G%GG#    c                    || j                   S t        | j                         t        |      k(  s)J dt        | j                          dt        |              |D cg c]  }| j                   |    c}S c c}w )a7  
        Return the stored input nodes, optionally reordered.

        Args:
            reorder: Optional sequence of indices to reorder the nodes.
                    For example, (2, 0, 1) would return nodes in that order.

        Returns:
            The tuple of input nodes, optionally reordered
        zReorder length mismatch:  vs )r   r   )r   reorderis      r   nodeszKernelInputs.nodes.   sz     ?$$$4$$%W5 	
'D,=,=(>'?tCL>R	
5 /66!!!$666s   A6c                ,    t        | j                        S )zh
        Get the number of input nodes.

        Returns:
            The number of input nodes
        )r   r   r   s    r   countzKernelInputs.count@   s     4$$%%r   c                F    t        j                  | j                  d         S )z|
        Get the device type of the first node.

        Returns:
            The device type (e.g., 'cuda', 'cpu')
        r   )r   get_device_typer   r$   s    r   device_typezKernelInputs.device_typeJ   s      !!$"3"3A"677r   c                <    | j                   d   j                         S )zn
        Get the device of the first node.

        Returns:
            The device of the first node
        r   )r   
get_devicer$   s    r   devicezKernelInputs.deviceU   s       #..00r   c                    | j                   O| j                         }| j                  dk(  r0t        j                  j                  |      }|j                  | _         | j                   S )zu
        Get the device name information.

        Returns:
            A tuple of (gpu_name, vendor, model)
        cuda)r   r+   r(   torchr-   get_device_propertiesgcnArchName)r   r+   device_propertiess      r   device_namezKernelInputs.device_name^   sX     $[[]F6)$)JJ$D$DV$L!$5$A$A!   r   c                :    t        d | j                  D              S )z
        Get the symbolic shapes of all input nodes.

        Returns:
            A tuple of shape tuples for each input node
        c              3  <   K   | ]  }|j                           y wN)get_size.0nodes     r   	<genexpr>z/KernelInputs.shapes_symbolic.<locals>.<genexpr>s   s     CT]]_C   tupler   r$   s    r   shapes_symboliczKernelInputs.shapes_symbolicl   s     C1B1BCCCr   c                :    t        d | j                  D              S )z
        Get the size hints for shapes of all input nodes.

        Returns:
            A tuple of shape tuples with integer hints for each input node
        c              3     K   | ];  }t         j                  j                  j                  |j	                                = y wr5   )r	   graphsizevarsoptimization_hintsr6   r7   s     r   r:   z-KernelInputs.shapes_hinted.<locals>.<genexpr>|   s3      
 GG//@
   AAr<   r$   s    r   shapes_hintedzKernelInputs.shapes_hintedu   $      
))
 
 	
r   c                :    t        d | j                  D              S )z
        Get the symbolic strides of all input nodes.

        Returns:
            A tuple of stride tuples for each input node
        c              3  <   K   | ]  }|j                           y wr5   )
get_strider7   s     r   r:   z0KernelInputs.strides_symbolic.<locals>.<genexpr>   s     E4T__&Er;   r<   r$   s    r   strides_symboliczKernelInputs.strides_symbolic   s     E43D3DEEEr   c                :    t        d | j                  D              S )z
        Get the size hints for strides of all input nodes.

        Returns:
            A tuple of stride tuples with integer hints for each input node
        c              3     K   | ];  }t         j                  j                  j                  |j	                                = y wr5   )r	   rA   rB   rC   rI   r7   s     r   r:   z.KernelInputs.strides_hinted.<locals>.<genexpr>   s4      
 GG//0AB
rD   r<   r$   s    r   strides_hintedzKernelInputs.strides_hinted   rF   r   c                :    t        d | j                  D              S )zx
        Get the dtypes of all input nodes.

        Returns:
            A tuple of dtypes for each input node
        c              3  <   K   | ]  }|j                           y wr5   )	get_dtyper7   s     r   r:   z&KernelInputs.dtypes.<locals>.<genexpr>   s     D$T^^%Dr;   r<   r$   s    r   dtypeszKernelInputs.dtypes   s     D$2C2CDDDr   c                <    | j                   |   j                         S )z
        Get the dtype of a specific input node.

        Args:
            idx: Index of the node to get the dtype from (default: 0)

        Returns:
            The dtype of the specified input node
        )r   rP   )r   idxs     r   dtypezKernelInputs.dtype   s       %//11r   c                     y)
        Get the output dtype, whether passed in or inferred from the nodes

        Returns:
            The output dtype
        N r$   s    r   r   zKernelInputs.out_dtype       r   c                R    || j                   v sJ d| d       | j                   |   S )z
        Get the scalar value for a given name.

        Args:
            name: Name of the scalar to get

        Returns:
            The scalar value
        zScalar z not found, but required)r   )r   names     r   
get_scalarzKernelInputs.get_scalar   s4     t}}$Nv5M&NN$}}T""r   c                     y)z
        Abstract method to handle output layout generation.

        Args:
            out_dtype: Optional output dtype. If not provided, infer from inputs
            flexible: If True, return FlexibleLayout, otherwise FixedLayout
        NrW   )r   flexibles     r   output_layoutzKernelInputs.output_layout   rX   r   )NN)r   	list[Any]r   dict[str, float | int] | Noner   torch.dtype | Noner5   )r    zSequence[int] | Nonereturnr_   rb   int)rb   z
str | None)rb   ztorch.device)rb   ztuple[tuple[Any, ...], ...])rb   ztuple[tuple[int, ...], ...])rb   z%tuple[tuple[sympy.Integer, ...], ...])rb   ztuple[torch.dtype, ...])r   )rS   rd   rb   torch.dtyperb   re   )rZ   strrb   zfloat | intTr]   boolrb   r   )__name__
__module____qualname____doc__r   r"   propertyr%   r(   r+   r2   r>   rE   rJ   rM   rQ   rT   r   r   r[   r^   rW   r   r   r   r      s     26(,	HH /H &	H&7$ & & 8 81!D

F

E
2  #  r   r   c                  v     e Zd ZdZ	 	 	 	 d		 	 	 	 	 	 	 	 	 d
 fdZ	 	 ddZddZdddZddZddZ	ddZ
 xZS )MMKernelInputsz
    Specialized KernelInputs for matrix multiplication operations.
    Provides additional methods to access M, N, K dimensions.
    c                V   t         |   |||       t        | j                        dk\  sJ d       ||}}|dk  r|t        |      z  }|dk  r|t        |      z  }d|cxk  rt        |      k  sn J d|        d|cxk  rt        |      k  sn J d|        || _        || _        y)z
        Initialize with a tuple of input nodes.

        By default, we assume the last 2 input nodes are mat1 and mat2, but
        the caller can adjust when necessary
           zExpected at least 2 input nodesr   zInvalid mat1_idx: zInvalid mat2_idx: N)superr   r   r   	_mat1_idx	_mat2_idx)	r   r   r   r   mat1_idxmat2_idxm1_idxm2_idx	__class__s	           r   r   zMMKernelInputs.__init__   s     	gy9 4$$%*M,MM* "8a<c+&&Fa<c+&&FF-S--N1CH:/NN-F-S--N1CH:/NN-!!r   c                l   | j                         | j                     }| j                         | j                     }|j                         d   }|j                         d   }|j                         d   }|j                         d   }t        j
                  j                  j                  ||       |||fS )a  
        Get the symbolic M, N, K dimensions for matrix multiplication.
        Handles both 2D (MM) and 3D (BMM) tensors.

        M is extracted from the second-to-last dimension of the first operand (mat1).
        N is extracted from the last dimension of the second operand (mat2).
        K is extracted from the last dimension of the first operand (mat1).

        Returns:
            A tuple of (M, N, K) dimensions
        )r"   ru   rv   r6   r	   rA   rB   check_equals)r   mat1mat2mknk0s          r   mnk_symboliczMMKernelInputs.mnk_symbolic   s     zz|DNN+zz|DNN+MMOBMMOBMMOB ]]_R 	%%a,1ayr   c                t    | j                   | j                   S | j                         d   j                         S )rV   r   )r   mat1mat2rP   r$   s    r   r   zMMKernelInputs.out_dtype  s2     ??&??"}}q!++--r   c                   | j                         \  }}| j                         }|j                         ^ }}}|j                         ^ }}	}
t        ||      D cg c]/  \  }}t        j
                  j                  j                  ||      1 c}}}g |||
}|rt        | j                         ||      S t        | j                         ||      S c c}}w )z
        Handle output layout generation for matrix multiplication.

        Args:
            out_dtype: Optional output dtype. If not provided, infer from inputs
            flexible: If True, return FlexibleLayout, otherwise FixedLayout
        )r   r   r6   zipr	   rA   rB   check_equals_and_simplifyr   r+   r   )r   r]   r   r   r   b1r   k1b2k2r   absizes                 r   r^   zMMKernelInputs.output_layout  s     ]]_
dNN$	]]_
Q]]_
RJMbRT+V$!QQWW771=VzAzqz!$++-DAAt{{}i>> Ws   4Cc                ^    | j                         }|| j                     || j                     fS )zj
        Get the mat1 and mat2 nodes.

        Returns:
            A tuple of (mat1, mat2) nodes
        )r"   ru   rv   )r   r"   s     r   r   zMMKernelInputs.mat1mat2,  s,     

T^^$eDNN&;;;r   c                    | j                         }|| j                     }|| j                     }|d   }|d   }|d   }|d   }||k(  sJ d| d|        |||fS )a   
        Get the hinted M, N, K dimensions for matrix multiplication.
        Handles both 2D (MM) and 3D (BMM) tensors.

        Uses shapes_hinted from the base class to get integer hints for dimensions.

        Returns:
            A tuple of (M, N, K) dimensions as integers
        r}   r~   zK dimensions don't match: r   )rE   ru   rv   )r   hinted_shapes
mat1_shape
mat2_shaper   r   r   k_checks           r   
mnk_hintedzMMKernelInputs.mnk_hinted6  s     **,"4>>2
"4>>2
rNrNrN R.G|J9!D	JJ|1ayr   c                h    | j                         }|| j                     }t        |      dk\  r|d   S y)z
        Get the hinted batch size for batched matrix multiplication.
        Returns 1 for non-batched (2D) operations.

        Returns:
            The batch size as an integer
           r
   )rE   ru   r   )r   r   r   s      r   batch_hintedzMMKernelInputs.batch_hintedN  s9     **,"4>>2
z?ab>!r   )NNr}   r~   )
r   r_   r   r`   r   ra   rw   rd   rx   rd   )rb   z2tuple[sympy.Integer, sympy.Integer, sympy.Integer]rf   rh   ri   )rb   ztuple[Any, Any])rb   ztuple[int, int, int]rc   )rk   rl   rm   rn   r   r   r   r^   r   r   r   __classcell__)r{   s   @r   rq   rq      sr     26(,"" /" &	"
 " "B	;4	.?(<0r   rq   )
__future__r   abcr   r   typingr   r   r.   torch._inductor.configtorch._inductorr   torch._inductor.virtualizedr	   r   r   r   collections.abcr   sympyr   rq   rW   r   r   <module>r      sH    " # %    ) 3 3 (u3 upO\ Or   