
    ȅiE*                        S SK Jr  S SKJrJr  S SKJrJr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OptionalTYPE_CHECKINGUnionN)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/james-whalen/.local/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   r2   r.   torchr5   get_device_propertiesgcnArchName)r   r2   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       [
        R                  R                  R                  S 9v   M_     g7f)fallbackN)	r   graphsizevars
size_hintsr?   r6   	_inductorconfigunbacked_symint_fallbackr@   s     r   rC   -KernelInputs.shapes_hinted.<locals>.<genexpr>|   sQ      

 *	 GG''//HH (  *   A%A'rF   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   rC   0KernelInputs.strides_symbolic.<locals>.<genexpr>   s     E3D4__&&3DrE   rF   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       [
        R                  R                  R                  S 9v   M_     g7frL   )	r   rN   rO   rP   r[   r6   rQ   rR   rS   r@   s     r   rC   .KernelInputs.strides_hinted.<locals>.<genexpr>   sR      

 *	 GG''!//HH (  *rU   rF   r)   s    r   strides_hintedKernelInputs.strides_hinted   rX   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   rC   &KernelInputs.dtypes.<locals>.<genexpr>   s     D2C$^^%%2CrE   rF   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   rf   )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
Nrp   )r   flexibles     r   output_layoutKernelInputs.output_layout   rr   r!   )r   r   r   r   )NN)r   	list[Any]r   &Optional[dict[str, Union[float, int]]]r   Optional[torch.dtype]r>   )r$   zOptional[Sequence[int]]returnr{   )r~   int)r~   zOptional[str])r~   ztorch.device)r~   ztuple[tuple[Any, ...], ...])r~   ztuple[tuple[int, ...], ...])r~   z%tuple[tuple[sympy.Integer, ...], ...])r~   ztuple[torch.dtype, ...])r   )rk   r   r~   torch.dtyper~   r   )rt   strr~   zUnion[float, int]Trx   boolr~   r   )__name__
__module____qualname____firstlineno____doc__r   r&   propertyr*   r.   r2   r:   rH   rV   r]   rb   rh   rl   r   r   ru   ry   __static_attributes__rp   r!   r   r   r      s     ;?+/	HH 8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	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   rN   rO   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       $ )ro   r   )r   mat1mat2rf   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   rN   rO   check_equals_and_simplifyr   r2   r   )r   rx   r   r   r   b1r   k1b2k2r   absizes                 r   ry   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.mat1mat22  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#   )rV   r   r   )r   hinted_shapes
mat1_shape
mat2_shaper   r   r   k_checks           r   
mnk_hintedMMKernelInputs.mnk_hinted<  sw     **,">>2
">>2
rNrNrN R.|J9!D	JJ|ayr!   )r   r   )NNr   r   )
r   r{   r   r|   r   r}   r   r   r   r   )r~   z2tuple[sympy.Integer, sympy.Integer, sympy.Integer]r   r   r   )r~   ztuple[Any, Any])r~   ztuple[int, int, int])r   r   r   r   r   r   r   r   ry   r   r   r   __classcell__)r   s   @r   r   r      sw     ;?+/"" 8" )	"
 " " "B	;4	.?(< r!   r   )
__future__r   abcr   r   typingr   r   r   r	   r6   torch._inductor.configtorch._inductorr
   torch._inductor.virtualizedr   r   r   r   collections.abcr   sympyr   r   rp   r!   r   <module>r      sK    " # 6 6    ) 3 3 ({3 {|@\ @r!   