
    3j++                        S SK Jr  S SKJrJr  S SKJrJr  S SKrS SK	rS SK
Jr  S SKJr  SSKJrJrJr  \(       a
  S S	KJr  S SKr " S
 S\5      r " S S\5      rg)    )annotations)ABCabstractmethod)AnyTYPE_CHECKINGN)ir)V   )FixedLayoutFlexibleLayoutLayout)Sequencec                      \ rS rSrSr  S     SS jjrSSS jjr\SS j5       r\SS j5       r	SS jr
SS	 jrSS
 jrSS jrSS jrSS jrSS jrSS S jjr\S!S j5       rS"S jr\S#S$S jj5       rSrg)%KernelInputs   z
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    Xl         SU l        Ub  UO0 U l        X0l        [	        U5      S:  d   S5       eg)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       W/home/wildlama/miniconda3/lib/python3.13/site-packages/torch/_inductor/kernel_inputs.py__init__KernelInputs.__init__   s>     ((,#*#6B#;!#G%GG#    c                    Uc  U R                   $ [        U R                   5      [        U5      :X  d)   S[        U R                   5       S[        U5       35       eU Vs/ s H  o R                   U   PM     sn$ s  snf )z
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   nodesKernelInputs.nodes.   s|     ?$$$4$$%W5 	
'D,=,=(>'?tCL>R	
5 /66g!!!$g666s   A:c                ,    [        U R                  5      $ )zH
Get the number of input nodes.

Returns:
    The number of input nodes
)r   r   r   s    r   countKernelInputs.count@   s     4$$%%r   c                H    [         R                  " U R                  S   5      $ )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_typeKernelInputs.device_typeJ   s      !!$"3"3A"677r   c                <    U R                   S   R                  5       $ )zN
Get the device of the first node.

Returns:
    The device of the first node
r   )r   
get_devicer'   s    r   deviceKernelInputs.deviceU   s       #..00r   c                    U R                   cP  U R                  5       nU R                  S:X  a0  [        R                  R                  U5      nUR                  U l         U R                   $ )zU
Get the device name information.

Returns:
    A tuple of (gpu_name, vendor, model)
cuda)r   r0   r,   torchr3   get_device_propertiesgcnArchName)r   r0   device_propertiess      r   device_nameKernelInputs.device_name^   sX     $[[]F6)$)JJ$D$DV$L!$5$A$A!   r   c                :    [        S U R                   5       5      $ )zg
Get the symbolic shapes of all input nodes.

Returns:
    A tuple of shape tuples for each input node
c              3  @   #    U  H  oR                  5       v   M     g 7fN)get_size.0nodes     r   	<genexpr>/KernelInputs.shapes_symbolic.<locals>.<genexpr>s   s     C1B]]__1B   tupler   r'   s    r   shapes_symbolicKernelInputs.shapes_symbolicl   s     C1B1BCCCr   c                :    [        S U R                   5       5      $ )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     #    U  H<  n[         R                  R                  R                  UR	                  5       5      v   M>     g 7fr<   )r	   graphsizevarsoptimization_hintsr=   r>   s     r   rA   -KernelInputs.shapes_hinted.<locals>.<genexpr>|   s5      
) GG//@@)   AArD   r'   s    r   shapes_hintedKernelInputs.shapes_hintedu   $      
))
 
 	
r   c                :    [        S U R                   5       5      $ )zi
Get the symbolic strides of all input nodes.

Returns:
    A tuple of stride tuples for each input node
c              3  @   #    U  H  oR                  5       v   M     g 7fr<   )
get_strider>   s     r   rA   0KernelInputs.strides_symbolic.<locals>.<genexpr>   s     E3D4__&&3DrC   rD   r'   s    r   strides_symbolicKernelInputs.strides_symbolic   s     E43D3DEEEr   c                :    [        S U R                   5       5      $ )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     #    U  H<  n[         R                  R                  R                  UR	                  5       5      v   M>     g 7fr<   )r	   rJ   rK   rL   rT   r>   s     r   rA   .KernelInputs.strides_hinted.<locals>.<genexpr>   s6      
) GG//0ABB)rN   rD   r'   s    r   strides_hintedKernelInputs.strides_hinted   rQ   r   c                :    [        S U R                   5       5      $ )zX
Get the dtypes of all input nodes.

Returns:
    A tuple of dtypes for each input node
c              3  @   #    U  H  oR                  5       v   M     g 7fr<   )	get_dtyper>   s     r   rA   &KernelInputs.dtypes.<locals>.<genexpr>   s     D2C$^^%%2CrC   rD   r'   s    r   dtypesKernelInputs.dtypes   s     D$2C2CDDDr   c                <    U R                   U   R                  5       $ )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   idxs     r   dtypeKernelInputs.dtype   s       %//11r   c                    g)c
Get the output dtype, whether passed in or inferred from the nodes

Returns:
    The output dtype
N r'   s    r   r   KernelInputs.out_dtype       r   c                T    XR                   ;   d   SU S35       eU R                   U   $ )zr
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_scalarKernelInputs.get_scalar   s2     }}$Nv5M&NN$}}T""r   c                    g)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
Nri   )r   flexibles     r   output_layoutKernelInputs.output_layout   rk   r   )r   r   r   r   )NN)r   	list[Any]r   dict[str, float | int] | Noner   torch.dtype | Noner<   )r"   zSequence[int] | Nonereturnrt   rw   int)rw   z
str | None)rw   ztorch.device)rw   ztuple[tuple[Any, ...], ...])rw   ztuple[tuple[int, ...], ...])rw   z%tuple[tuple[sympy.Integer, ...], ...])rw   ztuple[torch.dtype, ...])r   )rd   ry   rw   torch.dtyperw   rz   )rm   strrw   zfloat | intTrq   boolrw   r   )__name__
__module____qualname____firstlineno____doc__r   r$   propertyr(   r,   r0   r8   rF   rO   rV   r[   ra   re   r   r   rn   rr   __static_attributes__ri   r   r   r   r      s     26(,	HH /H &	H&7$ & & 8 81!D

F

E
2  #  r   r   c                     ^  \ rS rSrSr    S         SU 4S jjjr  SS jrSS jrSSS jjrSS jr	SS jr
SS	 jrS
rU =r$ )MMKernelInputs   zz
Specialized KernelInputs for matrix multiplication operations.
Provides additional methods to access M, N, K dimensions.
c                Z  > [         TU ]  XU5        [        U R                  5      S:  d   S5       eXEpvUS:  a  U[        U5      -  nUS:  a  U[        U5      -  nSUs=::  a  [        U5      :  d  O   SU 35       eSUs=::  a  [        U5      :  d  O   SU 35       eX@l        XPl        g)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   MMKernelInputs.__init__   s     	y9 4$$%*M,MM* "a<c+&&Fa<c+&&FF-S--N1CH:/NN-F-S--N1CH:/NN-!!r   c                h   U R                  5       U R                     nU R                  5       U R                     nUR                  5       S   nUR                  5       S   nUR                  5       S   nUR                  5       S   n[        R
                  R                  R                  XF5        X5U4$ )aq  
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   r=   r	   rJ   rK   check_equals)r   mat1mat2mknk0s          r   mnk_symbolicMMKernelInputs.mnk_symbolic   s     zz|DNN+zz|DNN+MMOBMMOBMMOB ]]_R 	%%a,ayr   c                v    U R                   b  U R                   $ U R                  5       S   R                  5       $ )rh   r   )r   mat1mat2r_   r'   s    r   r   MMKernelInputs.out_dtype  s2     ??&??"}}q!++--r   c                   U R                  5       u  p#U R                  5       nUR                  5       Gt pVnUR                  5       Gt pn
[        XX5       VVs/ s H.  u  p[        R
                  R                  R                  X5      PM0     snnn/ UQUPU
PnU(       a  [        U R                  5       XM5      $ [        U R                  5       XM5      $ s  snnf )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   r=   zipr	   rJ   rK   check_equals_and_simplifyr   r0   r   )r   rq   r   r   r   b1r   k1b2k2r   absizes                 r   rr   MMKernelInputs.output_layout  s     ]]_
NN$	]]_
]]_
JMb+V+$!QWW77=+VzAzqz!$++-AAt{{}i>> Ws   5Cc                Z    U R                  5       nXR                     XR                     4$ )zJ
Get the mat1 and mat2 nodes.

Returns:
    A tuple of (mat1, mat2) nodes
)r$   r   r   )r   r$   s     r   r   MMKernelInputs.mat1mat2,  s(     

^^$eNN&;;;r   c                    U R                  5       nXR                     nXR                     nUS   nUS   nUS   nUS   nXW:X  d   SU SU 35       eXFU4$ )z
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!   )rO   r   r   )r   hinted_shapes
mat1_shape
mat2_shaper   r   r   k_checks           r   
mnk_hintedMMKernelInputs.mnk_hinted6  sw     **,">>2
">>2
rNrNrN R.|J9!D	JJ|ayr   c                h    U R                  5       nXR                     n[        U5      S:  a  US   $ g)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
   )rO   r   r   )r   r   r   s      r   batch_hintedMMKernelInputs.batch_hintedN  s7     **,">>2
z?ab>!r   )r   r   )NNr   r   )
r   rt   r   ru   r   rv   r   ry   r   ry   )rw   z2tuple[sympy.Integer, sympy.Integer, sympy.Integer]r{   r}   r~   )rw   ztuple[Any, Any])rw   ztuple[int, int, int]rx   )r   r   r   r   r   r   r   r   rr   r   r   r   r   __classcell__)r   s   @r   r   r      s|     26(,"" /" &	"
 " " "B	;4	.?(<0 r   r   )
__future__r   abcr   r   typingr   r   r4   torch._inductor.configtorch._inductorr   torch._inductor.virtualizedr	   r   r   r   collections.abcr   sympyr   r   ri   r   r   <module>r      sH    " # %    ) 3 3 (u3 upO\ Or   