
    `i"                        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dS )    )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d0d#ZdS )1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.
    Ninput_nodes	list[Any]scalars&Optional[dict[str, Union[float, int]]]c                r    || _         d| _        ||ni | _        t          |          dk    s
J d            dS )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)selfr   r   s      q/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/_inductor/kernel_inputs.py__init__zKernelInputs.__init__   sL     (+/#*#6B;!###%G#####    reorderOptional[Sequence[int]]returnc                     | j         S t           j                   t          |          k    s/J dt           j                    dt          |                        fd|D             S )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
        NzReorder length mismatch:  vs c                *    g | ]}j         |         S  )r   ).0ir   s     r   
<listcomp>z&KernelInputs.nodes.<locals>.<listcomp>8   s!    666!!$666r   )r   r   )r   r   s   ` r   nodeszKernelInputs.nodes(   s}     ?$$4$%%W555RD,=(>(>RRCLLRR 655 7666g6666r   intc                *    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   Optional[str]c                @    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     !$"3A"6777r   torch.devicec                @    | j         d                                         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00r   c                    | j         J|                                 }| j        dk    r+t          j                            |          }|j        | _         | j         S )zu
        Get the device name information.

        Returns:
            A tuple of (gpu_name, vendor, model)
        Ncuda)r   r0   r,   torchr2   get_device_propertiesgcnArchName)r   r0   device_propertiess      r   device_namezKernelInputs.device_nameX   sR     $[[]]F6))$)J$D$DV$L$L!$5$A!  r   tuple[tuple[Any, ...], ...]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   | ]}|                                 V  d S N)get_sizer!   nodes     r   	<genexpr>z/KernelInputs.shapes_symbolic.<locals>.<genexpr>m   s*      CCT]]__CCCCCCr   tupler   r'   s    r   shapes_symboliczKernelInputs.shapes_symbolicf   s$     CC1BCCCCCCr   tuple[tuple[int, ...], ...]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   | ]P}t           j        j                            |                                t
          j        j        j                   V  QdS )fallbackN)	r	   graphsizevars
size_hintsr<   r3   	_inductorconfigunbacked_symint_fallbackr=   s     r   r?   z-KernelInputs.shapes_hinted.<locals>.<genexpr>v   sh       
 

 	 G''/H (  
 
 
 
 
 
r   r@   r'   s    r   shapes_hintedzKernelInputs.shapes_hintedo   7      
 

 )
 
 
 
 
 	
r   %tuple[tuple[sympy.Integer, ...], ...]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   | ]}|                                 V  d S r;   )
get_strider=   s     r   r?   z0KernelInputs.strides_symbolic.<locals>.<genexpr>   s,      EE4T__&&EEEEEEr   r@   r'   s    r   strides_symboliczKernelInputs.strides_symbolic~   s$     EE43DEEE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   | ]P}t           j        j                            |                                t
          j        j        j                   V  QdS rF   )	r	   rH   rI   rJ   rS   r3   rK   rL   rM   r=   s     r   r?   z.KernelInputs.strides_hinted.<locals>.<genexpr>   sj       
 

 	 G''!!/H (  
 
 
 
 
 
r   r@   r'   s    r   strides_hintedzKernelInputs.strides_hinted   rO   r   tuple[torch.dtype, ...]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   | ]}|                                 V  d S r;   )	get_dtyper=   s     r   r?   z&KernelInputs.dtypes.<locals>.<genexpr>   s,      DD$T^^%%DDDDDDr   r@   r'   s    r   dtypeszKernelInputs.dtypes   s$     DD$2CDDDDDDr   r   idxtorch.dtypec                @    | 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   r[   )r   r]   s     r   dtypezKernelInputs.dtype   s      %//111r   namestrUnion[float, int]c                J    || 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   ra   s     r   
get_scalarzKernelInputs.get_scalar   s8     t}$$$&N&N&N&N$$$}T""r   r;   )r   r   r   r   )r   r   r   r   )r   r%   )r   r)   )r   r-   )r   r8   )r   rC   )r   rP   )r   rX   )r   )r]   r%   r   r^   )ra   rb   r   rc   )__name__
__module____qualname____doc__r   r$   propertyr(   r,   r0   r7   rB   rN   rT   rW   r\   r`   re   r    r   r   r   r      s_         ;?H H H H H 7 7 7 7 7$ & & & X& 8 8 8 X81 1 1 1! ! ! !D D D D
 
 
 
F F F F
 
 
 
E E E E
2 
2 
2 
2 
2# # # # # #r   r   c                  D     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.
    Nr   r   r   r   mat1_idxr%   mat2_idxc                   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|             || _        || _        dS )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   ro   rp   m1_idxm2_idx	__class__s          r   r   zMMKernelInputs.__init__   s    	g... 4$%%***,M*** "8a<<c+&&&Fa<<c+&&&FF----S-------/NH/N/N---F----S-------/NH/N/N---!!r   r   2tuple[sympy.Integer, sympy.Integer, sympy.Integer]c                   |                                  | j                 }|                                  | j                 }|                                d         }|                                d         }|                                d         }|                                d         }t          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
        rm   rn   )r$   rt   ru   r<   r	   rH   rI   check_equals)r   mat1mat2mknk0s          r   mnk_symboliczMMKernelInputs.mnk_symbolic   s     zz||DN+zz||DN+MMOOBMMOOBMMOOB ]]__R 	%%a,,,1ayr   tuple[Any, Any]c                ^    |                                  }|| j                 || j                 fS )zj
        Get the mat1 and mat2 nodes.

        Returns:
            A tuple of (mat1, mat2) nodes
        )r$   rt   ru   )r   r$   s     r   mat1mat2zMMKernelInputs.mat1mat2   s*     

T^$eDN&;;;r   tuple[int, int, int]c                    |                                  }|| 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
        rm   rn   zK dimensions don't match: r   )rN   rt   ru   )r   hinted_shapes
mat1_shape
mat2_shaper~   r   r   k_checks           r   
mnk_hintedzMMKernelInputs.mnk_hinted  s     **,,"4>2
"4>2
rNrNrN R.G|||J!JJJJ|||1ayr   )Nrm   rn   )r   r   r   r   ro   r%   rp   r%   )r   ry   )r   r   )r   r   )	rf   rg   rh   ri   r   r   r   r   __classcell__)rx   s   @r   rl   rl      s          ;?" " " " " " "@   4< < < <       r   rl   )
__future__r   typingr   r   r   r   r3   torch._inductor.configtorch._inductorr   torch._inductor.virtualizedr	   collections.abcr
   sympyr   rl   r    r   r   <module>r      s   " " " " " " 6 6 6 6 6 6 6 6 6 6 6 6            ) ) ) ) ) )  ((((((LLLe# e# e# e# e# e# e# e#P` ` ` ` `\ ` ` ` ` `r   