
    pi"                        d dl mZ d dlmZmZmZmZ d dlZd dlZd dl	m
Z
 d dlmZ er
d dlmZ d dlZ G d d      Z G d	 d
e      Zy)    )annotations)AnyOptionalTYPE_CHECKINGUnionN)ir)V)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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                `    || _         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
        Nr   z Expected at least one input node)_input_nodes_device_name_scalarslen)selfinput_nodesscalarss      _/opt/services/ai/voice_agent/venv/lib/python3.12/site-packages/torch/_inductor/kernel_inputs.py__init__zKernelInputs.__init__   s;     (+/#*#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_typeD   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.deviceO   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_nameX   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>m   s     CT]]_C   tupler   r   s    r   shapes_symboliczKernelInputs.shapes_symbolicf   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	                         t
        j                  j                  j                          a yw)fallbackN)	r	   graphsizevars
size_hintsr0   r(   	_inductorconfigunbacked_symint_fallbackr1   s     r   r4   z-KernelInputs.shapes_hinted.<locals>.<genexpr>v   sP      

 	 GG''//HH ( 
   A%A'r6   r   s    r   shapes_hintedzKernelInputs.shapes_hintedo   &      

 ))
 
 	
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r/   )
get_strider1   s     r   r4   z0KernelInputs.strides_symbolic.<locals>.<genexpr>   s     E4T__&Er5   r6   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	                         t
        j                  j                  j                          a ywr;   )	r	   r=   r>   r?   rH   r(   r@   rA   rB   r1   s     r   r4   z.KernelInputs.strides_hinted.<locals>.<genexpr>   sQ      

 	 GG''!//HH ( 
rC   r6   r   s    r   strides_hintedzKernelInputs.strides_hinted   rE   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r/   )	get_dtyper1   s     r   r4   z&KernelInputs.dtypes.<locals>.<genexpr>   s     D$T^^%Dr5   r6   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   rO   )r   idxs     r   dtypezKernelInputs.dtype   s       %//11r   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   r/   )r   	list[Any]r   &Optional[dict[str, Union[float, int]]])r   zOptional[Sequence[int]]returnrW   )rY   int)rY   zOptional[str])rY   ztorch.device)rY   ztuple[tuple[Any, ...], ...])rY   ztuple[tuple[int, ...], ...])rY   z%tuple[tuple[sympy.Integer, ...], ...])rY   ztuple[torch.dtype, ...])r   )rR   rZ   rY   ztorch.dtype)rU   strrY   zUnion[float, int])__name__
__module____qualname____doc__r   r   propertyr   r"   r%   r,   r8   rD   rI   rL   rP   rS   rV    r   r   r   r      s     ;?HH 8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Z xZS )MMKernelInputsz
    Specialized KernelInputs for matrix multiplication operations.
    Provides additional methods to access M, N, K dimensions.
    c                T   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   mat1_idxmat2_idxm1_idxm2_idx	__class__s          r   r   zMMKernelInputs.__init__   s     	g. 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   rg   rh   r0   r	   r=   r>   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                ^    | j                         }|| j                     || j                     fS )zj
        Get the mat1 and mat2 nodes.

        Returns:
            A tuple of (mat1, mat2) nodes
        )r   rg   rh   )r   r   s     r   mat1mat2z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
        ro   rp   zK dimensions don't match: r   )rD   rg   rh   )r   hinted_shapes
mat1_shape
mat2_shapert   ru   rv   k_checks           r   
mnk_hintedzMMKernelInputs.mnk_hinted  s     **,"4>>2
"4>>2
rNrNrN R.G|J9!D	JJ|1ayr   )Nro   rp   )r   rW   r   rX   ri   rZ   rj   rZ   )rY   z2tuple[sympy.Integer, sympy.Integer, sympy.Integer])rY   ztuple[Any, Any])rY   ztuple[int, int, int])	r\   r]   r^   r_   r   rx   rz   r   __classcell__)rm   s   @r   rc   rc      sV     ;?"" 8" 	"
 "@	;4<r   rc   )
__future__r   typingr   r   r   r   r(   torch._inductor.configtorch._inductorr   torch._inductor.virtualizedr	   collections.abcr
   sympyr   rc   ra   r   r   <module>r      s@    " 6 6    ) (e# e#P`\ `r   