
    Vh                     2   U d dl Z d dlZd dlZddgZg Zee   ed<    ej                  dej                        Z
 ej                  dej                        Zded	ed
efdZd
efdZddZd Zd Zedk(  r! e       Z ej*                  ed k(  rd nd       yy)    N#check_code_for_cuda_kernel_launchescheck_cuda_kernel_launchesexclude_filesz^.*<<<[^>]+>>>\s*\()flagsz/\s*;(?![^;}]*C10_CUDA_KERNEL_LAUNCH_CHECK\(\);)sstartposreturnc                     d}t        | |d       D ]*  \  }}|dk(  r|dz  }|dk(  s|dz  }|dk(  s"||z   dz   c S  t        d      )zGiven a string "prefix (unknown number of characters) suffix"
    and the position of the first `(` returns the index of the character
    1 past the `)`, accounting for paren nesting
    r   N(   )zClosing parens not found!)	enumerate
IndexError)r   r   openingics        ]/home/dcms/DCMS/lib/python3.12/site-packages/torch/testing/_internal/check_kernel_launches.pyfind_matching_parenr   /   sk    
 G!HI,' (18qLG#XqLG!|!|a''( 0
11    c                 @    t         D ]  }| j                  |      s y y)NTF)r   endswith)filenameexclude_suffixs     r   should_exclude_filer   @   s'    ' ^, r   c                    |d}t        | j                  d            } | D cg c]  \  }}| d|  } }}dj                  |       } d}t        j	                  |       D ]r  }t        | |j                         dz
        }t        j                  | |      s7|dz  }| |j                         |dz    }t        d| d| t        j                         t |S c c}}w )	aE  Checks code for CUDA kernel launches without cuda error checks.

    Args:
        filename - Filename of file containing the code. Used only for display
                   purposes, so you can put anything here.
        code     - The code to check

    Returns:
        The number of unsafe kernel launches in the code
    z##Python Function Call##
z: r   r   z)Missing C10_CUDA_KERNEL_LAUNCH_CHECK in 'z'. Context:
file)r   splitjoinkernel_launch_startfinditerr   end	has_checkmatchstartprintsysstderr)coder   linenolinecodenum_launches_without_checksm	end_parencontexts           r   r   r   G   s     - TZZ%&D<@A(8vhb
#ADA99T?D"# ))$/ q'aeegk:	??4+'1,'1779Y]3G=hZ}U\T]^eheoeopq '& Bs   Cc                     | j                  d      syt        |       ryt        |       5 }|j                         }t	        ||       }ddd       |S # 1 sw Y   S xY w)zChecks a file for CUDA kernel launches without cuda error checks

    Args:
        filename - File to check

    Returns:
        The number of unsafe kernel launches in the file
    )z.cuz.cuhr   N)r   r   openreadr   )r   focontentsunsafeCounts       r   
check_filer7   f   sc     o.8$	h N27799(HMN N s   AAc                     t         j                  j                  t         j                  j                  t                    } t         j                  j                  |       } t         j                  j                  |       } d}g }t        j
                  |       D ]  \  }}}|t         j                  j                  | d      k(  s#|t         j                  j                  | d      k(  rg |dd S|D ]I  }t         j                  j                  ||      }t        |      }|dkD  s4||z  }|j                  |       K  |dkD  rd| dt        |       d}	t        |	t        j                         t        d	t        j                         |D ]   }t        d
| t        j                         " t        |	t        j                         |S )zChecks all pytorch code for CUDA kernel launches without cuda error checks

    Returns:
        The number of unsafe kernel launches in the codebase
    r   buildztorch/includeNzFound z instances in z0 files where kernel launches didn't have checks.r   zFiles without checks:	)ospathdirnamerealpath__file__walkr    r7   appendlenr'   r(   r)   )
	torch_dirkernels_without_checksfiles_without_checksrootdirnames	filenamesxr   file_result	count_strs
             r   r   r   y   s     0 0 :;I	*I	*I%'WWY%7 6!h	 277<<	733trww||IWf?g7g HQK 	6Aww||D!,H$X.KQ&+5&$++H5	66  !34N/01 233	 	icjj)%CJJ7% 	-ABqc(,	-icjj)!!r   __main__r   )N)r;   rer(   __all__r   liststr__annotations__compile	MULTILINEr!   r$   intr   boolr   r   r7   r   __name__unsafe_launchesexit r   r   <module>rZ      s    
 	 
 *  tCy ( !bjj",,  BJJ6bll	23 2# 2# 2"T '>&&"R z02OCHH/Q&QA. r   