
    qiE*                        d dl mZ d dlmZ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 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OptionalTYPE_CHECKINGUnionN)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       c/home/ubuntu/crypto_trading_bot/.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	                         t
        j                  j                  j                          a yw)fallbackN)	r   graphsizevars
size_hintsr8   r0   	_inductorconfigunbacked_symint_fallbackr9   s     r   r<   z-KernelInputs.shapes_hinted.<locals>.<genexpr>|   sP      

 	 GG''//HH ( 
   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r7   )
get_strider9   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	                         t
        j                  j                  j                          a ywrC   )	r   rE   rF   rG   rP   r0   rH   rI   rJ   r9   s     r   r<   z.KernelInputs.strides_hinted.<locals>.<genexpr>   sQ      

 	 GG''!//HH ( 
rK   r>   r&   s    r   strides_hintedzKernelInputs.strides_hinted   rM   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r7   )	get_dtyper9   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   rW   )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
        Nr^   )r   flexibles     r   output_layoutzKernelInputs.output_layout   r_   r   )NN)r   	list[Any]r   &Optional[dict[str, Union[float, int]]]r   Optional[torch.dtype]r7   )r"   zOptional[Sequence[int]]returnrf   )ri   int)ri   zOptional[str])ri   ztorch.device)ri   ztuple[tuple[Any, ...], ...])ri   ztuple[tuple[int, ...], ...])ri   z%tuple[tuple[sympy.Integer, ...], ...])ri   ztuple[torch.dtype, ...])r   )rZ   rj   ri   torch.dtyperi   rk   )ra   strri   zUnion[float, int]Trd   boolri   r   )__name__
__module____qualname____doc__r   r$   propertyr'   r*   r-   r4   r@   rL   rQ   rT   rX   r[   r   r   rb   re   r^   r   r   r   r      s     ;?+/	HH 8H )	H&7$ & & 8 81!D
F
E
2  #  r   r   c                  n     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	 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$   r{   r|   r8   r   rE   rF   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 )r]   r   )r   mat1mat2rW   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   r8   zipr   rE   rF   check_equals_and_simplifyr   r-   r   )r   rd   r   r   r   b1r   k1b2k2r   absizes                 r   re   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$   r{   r|   )r   r$   s     r   r   zMMKernelInputs.mat1mat22  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!   )rL   r{   r|   )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9!D	JJ|1ayr   )NNr   r   )
r   rf   r   rg   r   rh   r}   rj   r~   rj   )ri   z2tuple[sympy.Integer, sympy.Integer, sympy.Integer]rl   rn   ro   )ri   ztuple[Any, Any])ri   ztuple[int, int, int])rq   rr   rs   rt   r   r   r   re   r   r   __classcell__)r   s   @r   rw   rw      sm     ;?+/"" 8" )	"
 " "B	;4	.?(<r   rw   )
__future__r   abcr   r   typingr   r   r   r	   r0   torch._inductor.configtorch._inductorr
   torch._inductor.virtualizedr   r   r   r   collections.abcr   sympyr   rw   r^   r   r   <module>r      sK    " # 6 6    ) 3 3 ({3 {|@\ @r   