
    JTh              !          % S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJr  SSK	J
r
  SSK	Jr  SSKJr  SSKJr  SS	KJrJr  SS
KJr  SSKrSSKr " S S\5      r " S S5      r\\\4   rSr0 r\\S'    SSS.r/ SQr " S S\ 5      r!S r" " S S5      r# " S S5      r$S\S\4S jr%S\S\S\&4S  jr'S! r(     S]S"\S#\S$\S\S%\&S&\&S\\   4S' jjr)S(\S)\S*\S+\S,\\\*4   S-\&S&\&S.\$S/\&SS4S0 jr+S1 r,S2 r-\R\                  " S35      r/S4 r0S5 r1S6 r2S7 r3\R\                  " S85      r4S9 r5\R\                  " S:5      r6S; r7\R\                  " S<5      r8S= r9S^S> jr:S? r;S@ r<SA r=SB r>SC r? " SD SE5      r@ " SF SG5      rA\A" 5       rB0 rC\A" 5       rD0 rE\\\F4   \SH'   0 rG\
 H  rH\I" \H\5      (       d   e\HR                  5        H  u  rKrL\LS   rM\LSS rN\R                  \N;  aC  \DR                  \K5        \R                  \N;   a  \ER                  \KSI5      (       a  \M\G\K'   O\M\E\K'   \R                  \N;  d  Mu  \R                  \N;  d  M  \BR                  \K5        \M\C\K'   M     M     \R\                  " \BR                  5       5      rU\R\                  " SJ\DR                  5        SK35      rV\R\                  " SL5      rW\R\                  " SM5      rX\R\                  " SN5      rY\R\                  " SO5      rZ S(\S)\S*\S+\S,\\\*4   S-\&S&\&S.\$S/\&S\4SP jr[S^SQ jr\SR r]SS r^\R\                  " ST5      r_SU r`SV ra              S_SW\SX\&S\SY\S(\S+\S#\SZ\S%\&S$\S/\&S-\&S&\&S[\&S.\\$   S\4 S\ jjrbg)`a  The Python Hipify script.
##
# Copyright (c) 2015-2016 Advanced Micro Devices, Inc. All rights reserved.
#               2017-2018 Advanced Micro Devices, Inc. and
#                         Facebook Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
    N   )	constants)CUDA_TO_HIP_MAPPINGS)MATH_TRANSPILATIONS)Optional)Iterator)MappingIterable)Enumc                       \ rS rSrSrSrSrg)CurrentState-   r       N)__name__
__module____qualname____firstlineno__INITIALIZEDDONE__static_attributes__r       X/var/www/auris/envauris/lib/python3.13/site-packages/torch/utils/hipify/hipify_python.pyr   r   -   s    KDr   r   c                        \ rS rSrS rS rSrg)HipifyResult1   c                 *    Xl         X l        SU l        g )N current_statehipified_pathstatus)selfr    r!   s      r   __init__HipifyResult.__init__2   s    **r   c                 T    SU R                    SU R                   SU R                   3$ )NzHipifyResult:: current_state: z, hipified_path : z
, status: r   r#   s    r   __str__HipifyResult.__str__7   sF    01C1C0DDVW[WiWiVjjtuy  vA  vA  uB  C  	Dr   r   N)r   r   r   r   r$   r(   r   r   r   r   r   r   1   s    
Dr   r   z;// !!! This is a file automatically generated by hipify!!!
HIPIFY_FINAL_RESULTscalar_t)DtypeT)!
InputErroropenfbcolorsGeneratedFileCleanermatch_extensionsmatched_files_iterpreprocess_file_and_save_resultcompute_statsadd_dim3processKernelLaunchesfind_closure_groupfind_bracket_groupfind_parentheses_groupreplace_math_functionship_header_magicreplace_extern_sharedget_hip_file_pathis_out_of_placeis_pytorch_fileis_cusparse_fileis_special_fileis_caffe2_gpu_filerC   Triepreprocessorfile_specific_replacementfile_add_headerfix_static_global_kernelsextract_argumentsstr2boolr   r   hipifyc                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )r.   K   c                 0   > [         TU ]  U5        Xl        g N)superr$   message)r#   rQ   	__class__s     r   r$   InputError.__init__N   s    !r   c                      SU R                    3$ )NzInput error: rQ   r'   s    r   r(   InputError.__str__R   s    t||n--r   rU   )r   r   r   r   r$   r(   r   __classcell__)rR   s   @r   r.   r.   K   s    . .r   r.   c                     [        XSS9$ )Nignore)errors)open)filenamemodes     r   r/   r/   V   s    x00r   c                   4    \ rS rSrSrSrSrSrSrSr	Sr
S	rS
rg)r0   [   z[95mz[94mz[92mz[93mz[91mz[0mz[1mz[4mr   N)r   r   r   r   HEADEROKBLUEOKGREENWARNINGFAILENDCBOLD	UNDERLINEr   r   r   r   r0   r0   [   s*    FFGGDDDIr   r0   c                   >    \ rS rSrSrS
S jrS rS rS
S jrS r	Sr
g	)r1   n   z+Context Manager to clean up generated filesc                 <    Xl         [        5       U l        / U l        g rO   )keep_intermediatessetfiles_to_cleandirs_to_clean)r#   rk   s     r   r$   GeneratedFileCleaner.__init__p   s    "4!er   c                     U $ rO   r   r'   s    r   	__enter__GeneratedFileCleaner.__enter__u   s    r   c                     [         R                  R                  U5      (       d8  U R                  R	                  [         R                  R                  U5      5        [        U/UQ70 UD6$ rO   )ospathexistsrm   addabspathr[   )r#   fnargskwargss       r   r[   GeneratedFileCleaner.openx   sK    ww~~b!!##BGGOOB$78B((((r   c                    [         R                  R                  U5      u  p4U(       d!  [         R                  R                  U5      u  p4U(       a;  U(       a4  [         R                  R                  U5      (       d  U R	                  USS9  [         R                  R                  U5      (       a  U(       dO  [         R                  " U5        U R                  R                  [         R                  R                  U5      5        g g )NT)exist_ok)
rt   ru   splitrv   makedirsisdirmkdirrn   appendrx   )r#   dnr~   parentns        r   r   GeneratedFileCleaner.makedirs}   s    GGMM"%	f-IFav 6 6MM&4M0ww}}R  HHRL%%bggoob&9: )1r   c                     U R                   (       dY  U R                   H  n[        R                  " U5        M     U R                  S S S2    H  n[        R
                  " U5        M     g g )N)rk   rm   rt   unlinkrn   rmdir)r#   typevalue	tracebackfds         r   __exit__GeneratedFileCleaner.__exit__   sO    &&((		! )''"- . 'r   )rn   rm   rk   NF)r   r   r   r   __doc__r$   rq   r[   r   r   r   r   r   r   r1   r1   n   s    5 
)
;r   r1   ru   returnc                 B    U R                  [        R                  S5      $ )N/)replacert   sep)ru   s    r   _to_unix_pathr      s    <<$$r   r\   
extensionsc                 .   ^  [        U 4S jU 5       5      $ )z<Helper method to see if filename ends with certain extensionc              3   F   >#    U  H  nTR                  U5      v   M     g 7frO   endswith).0er\   s     r   	<genexpr>#match_extensions.<locals>.<genexpr>   s     8Zx  ##Zs   !any)r\   r   s   ` r   r2   r2      s    8Z888r   c                 .   ^  [        U 4S jU 5       5      $ )Nc              3   R   >#    U  H  n[         R                   " TU5      v   M     g 7frO   )fnmatch)r   patternfilepaths     r   r   _fnmatch.<locals>.<genexpr>   s     Jgwx11s   $'r   )r   patternss   ` r   _fnmatchr      s    JJJJr   	root_pathincludesignoresout_of_place_onlyis_pytorch_extensionc              #     #    [        U5      n[        R                  " U SS9 GH\  u  pxn	[        R                  R	                  Xp5      n
U
S:X  aV  SU;   a  UR                  S5        SU;   a  UR                  S5        SU;   a"  UR                  S5        UR                  S5        U	 H  n[        [        R                  R                  X{5      5      n[        [        R                  R                  X5      5      n[        X5      (       d  Me  [        X5      (       a  Mw  [        X5      (       d  X;   d  M  U(       d;  [        U5      (       d  [        U5      (       d  M  U(       a  [        U5      (       d  M  Uv   M     GM_     g 7f)NT)topdown.z.gitbuildthird_partyzthird_party/nvfuser)rl   rt   walkru   relpathremover   r   joinr   r2   r@   rC   r?   )r   r   r   r   r   r   exact_matchesabs_dirpathdirs	filenamesrel_dirpathr\   r   rel_filepaths                 r   r3   r3      s      MM +-'')T*J&Iggook=#~F#$G$$M*12!H$RWW\\+%HIH(k)LML ,,!(44%h;;x?X+*<88AST`AaAa (1N1N  " +Ks   DFFF5AFoutput_directoryr   	all_filesheader_include_dirsstatship_clang_launch	clean_ctxshow_progressc	                 ^   [         R                  R                  [         R                  R                  X5      5      n	[	        [
        R                  U	S9n
U
[        U	'   [        XX#UXVXx5	      nU(       a1  SUR                  ;  a!  [        U	SUR                  UR                  SS9  U[        U	'   g )N)r    r!   ignoredz->T)flush)rt   ru   rx   r   r   r   r   r*   rE   r"   printr!   )r   r   r   r   r   r   r   r   r   fin_pathhipify_resultresults               r   r4   r4      s     wwrww||,<GHH |/G/GW_`M$1!*iV[*)\F &--7d  &--t	= %+!r   c                     U S    VVs1 s H  u  pUiM	     nnn[        S[        U5      S 35        [        SR                  U5      5        [        S[        U S   5      S 35        g s  snnf )Nunsupported_callsz1Total number of unsupported CUDA function calls: r   , z+
Total number of replaced kernel launches: kernel_launches)r   lenr   )r   	cuda_call	_filepathr   s       r   r5   r5      sy    AFGZA[\A['=	A[\ 
=cBS>TUV=W
XY 
$))%
&' 
8UCT=U9VWX8Y
Z[ ]s   A(c                    SnSnU R                  SS5      R                  SS5      n [        S5       Vs/ s H  n0 PM     nnSXR   S'   [        U 5       Hi  u  pgUS:  a    O`US:X  a  US-  nOUS	:X  a  US-  nUS
:X  d  U[        U 5      S-
  :X  d  M>  US:X  d  MF  XgS
:g  -   XR   S'   US-  nUS:  d  M_  US-   XR   S'   Mk     XS   S   US   S   S-    nXS   S   US   S    n	XS   S   US   S    R                  SS5      R	                  S5      n
XS   S   US   S    R                  SS5      R	                  S5      nSU
 S	3nSU S	3nUR                  X5      nU	R                  X5      nUR                  X-   X-   5      nU$ s  snf )zBadds dim3() to the second and third arguments in the kernel launchr   <<<r   >>>r   startr   (),end
 zdim3()r   range	enumerater   strip)kernel_stringcuda_kernelcountclosure_arg_locsindcfirst_arg_rawsecond_arg_rawfirst_arg_cleansecond_arg_cleanfirst_arg_dim3second_arg_dim3first_arg_raw_dim3second_arg_raw_dim3s                   r   r6   r6      s   EG!))%4<<UBGM27(%;(Qb(H%; HOGM*198qLG#XqLGHs=1A557a<%(H%5HOE"QJEqy+.7( + "1+g"6x{57IA7MNM"A;w#7E8JKN#QK$8!U9KLTTUY[]^ddehiO$a[%9(1+e:LMUUVZ\^_eefij_-Q/N./q1O&..O(001AS%%m&DFXFnoK7 &<s   Fz([ ]+)(detail?)::[ ]+\\\n[ ]+c           
        ^  [         R                  S T 5      m U 4S jnS nS n[        U" U" T 5      5      5      nT nU GH  nU" U5      nT R                  SUS   5      n	T US   S   U	S	-    n
T US   US    nUS	   S   S
:X  a  SOS	nT US   S   X   S   S	-    n[	        X5      n[        [        SUS   R                  SS5      R                  SS5      5      5      nSUSS
 R                  SSSU-
  -  S-   5      R                  SS5      R                  SS5      R                  USU-   S-   5      -   nUR                  U
U5      nUS   R                  U5        GM      U$ )zJReplace the CUDA style Kernel launches with the HIP style kernel launches.c                 L    U R                  S5       U R                  S5       S3$ )Nr   r   z::groupinps    r   <lambda>'processKernelLaunches.<locals>.<lambda>  s    1syy|nB.Or   c                   > U S   U S   S.SSS.SSS.S.nSS0nSnSnS	nS
nUn[        US   S   S-
  SS5       H  nT
U   n	XsU4;   aK  U	S:X  a  Xs:X  a	  UnXS   S'   US==   S-  ss'   U	S:X  a$  US==   S-  ss'   US   S:X  a  Xt:X  a	  XS   S'   UnXt:w  d  Ma  T
U   R                  5       (       d	  T
U   S;   a.  Xv:w  a	  UnXS   S'   US:X  a  SUS   S'   US   US   US   /s  $ M  Xv:X  d  M  XS   S'   US   US   US   /s  $    g )Nr   r   r   r   r   )kernel_launchkernel_nametemplatez<>r   r   r      r   >r   <>   #r   r   :r   r   )r   isalnum)	in_kernelposr   STARTAT_TEMPLATEAFTER_TEMPLATEAT_KERNEL_NAMEr"   icharstrings             r   grab_method_and_template7processKernelLaunches.<locals>.grab_method_and_template  s    (1'9)EBRS%'3"$R0
 q	  s?+G4q8"bAA!9D --3;!,12J.$K1$K3;$K1$KT{a'V-B34J0!/ $!9$$&&&)7P*P/!/45M*51 Av67M*73 "%]!3s:#oJ^``	  /67M*73 "%]!3s:#oJ^``K Br   c                    Sn/ nU R                  SU5      S:w  af  U R                  SU5      nU R                  SU5      S-   nUS::  a  [        S5      eUR                  X1XU S.5        U R                  SU5      S:w  a  Mf  U$ )zKFinds the starting and ending points for all kernel launches in the string.r   r   r   r   r  zno kernel end found)r   r   r   )findr.   r   )r  
kernel_endkernel_positionskernel_starts       r   find_kernel_bounds1processKernelLaunches.<locals>.find_kernel_boundsT  s    
 kk%,2!;;uj9L  UL9A=JQ !677 ##l.4:.N%P Q kk%,2  r   c                 8   SnSnSnU  H  nUS:X  a3  US:X  a	  US:X  a  SnObUS:X  a	  US:X  a  SnOSUS:X  a  US:w  a  US:w  a  SnO>US:X  a  US	:X  d  US
:X  a  SnO)US:X  a  US:X  a  US:X  a  SnOUS:X  a  US:X  a  US:w  a  SnUnUS:X  a  X4-  nM  US-  nM     U$ )Nr   r   z//*z/*"\'r   xr   )r  
in_commentprev_c
new_stringr   s        r   mask_comments,processKernelLaunches.<locals>.mask_commentsl  s    

AR8#!%J#X&C-!%J#X&D.Vs]!$Jt#9T	!#Jt#8#!#Js"8$!#JFR
c!
3 4 r   r   r   r   r   r   r   r   r   r   r   zhipLaunchKernelGGL(z, 0   r   r   )	RE_KERNEL_LAUNCHsublistr  r6   r   rI   r   r   )r  r   r  r  r#  get_kernel_positionsoutput_stringkernelparamsparenthesisr   r   end_param_indexkernel_name_with_templatecuda_kernel_dim3num_klp
hip_kernels   `                r   r7   r7     s    !!"OQWXF9av 0B   2=3H IJM ')&1 kk#ve}5 VAYw/a@vgve}=%ay/25!1$*6!9W+=f>UV[>\_`>`$a!#M?'6'?+B+B5#+N+V+VW\^a+bcd*-=a-C-K-K5AK(50.229'%2Fww4H !:CB[<[^a<abc

 &--k:F 	 ''
31 '4 r   c                     SnSnUnSu  pgU[        U 5      :  aU  X   US   :X  a  USL a  SnSnUnO(US-  nO"X   US   :X  a  U(       a  US-  nUS:X  a  UnXg4$ US-  nU[        U 5      :  a  MU  g)a  Generalization for finding a balancing closure group

     if group = ["(", ")"], then finds the first balanced parentheses.
     if group = ["{", "}"], then finds the first balanced bracket.

Given an input string, a starting position in the input string, and the group type,
find_closure_group returns the positions of group[0] and group[1] as a tuple.

Example:
    >>> find_closure_group("(hi)", 0, ["(", ")"])
    (0, 3)
Fr   )r   r   Tr   )NN)r   )input_stringr   r   inside_parenthesisparensr  p_startp_ends           r   r8   r8     s     F
CNG
L!
!a(!U*%)"!%(*/AaKF{~%q L!
!  r   c                     [        XSS/S9$ )z%Finds the first balanced parantheses.{}r   r8   r4  r   s     r   r9   r9         l#sDDr   c                     [        XSS/S9$ )z!Finds the first balanced bracket.r   r   r   r<  r=  s     r   r:   r:     r>  r   z\bassert[ ]*\(c                 b    U n[          H"  nUR                  U S3[         U    S35      nM$     U$ )a_  FIXME: Temporarily replace std:: invocations of math functions
with non-std:: versions to prevent linker errors NOTE: This
can lead to correctness issues when running tests, since the
correct version of the math function (exp/expf) might not get
called.  Plan is to remove this function once HIP supports
std:: math function calls inside device code

r   )r   r   )r4  r*  funcs      r   r;   r;     sC     !M#%--$qk>QRV>W=XXY;Z[ $ r   z:?:?\b(__syncthreads)\b(\w*\()c                    ^ U mSS/n[        U4S jU 5       5      (       a  T$ ST;   nUST;   -  nUST;   -  nU[        R                  T5      SL-  nU(       a  SU -   mT$ )	a  If the file makes kernel builtin calls and does not include the cuda_runtime.h header,
then automatically add an #include to match the "magic" includes provided by NVCC.
TODO:
    Update logic to ignore cases where the cuda_runtime.h is included by another file.
zhip/hip_runtime.hzhip/hip_runtime_api.hc              3   `   >#    U  H#  n[         R                  " S U SU S3T5      v   M%     g7f)z#include ("z"|<z>)N)research)r   extr*  s     r   r   #hip_header_magic.<locals>.<genexpr>   s.     
Ww299C5C53]CCws   +.hipLaunchKernelGGL
__global__
__shared__Nz#include "hip/hip_runtime.h"
)r   RE_SYNCTHREADSrE  )r4  headershasDeviceLogicr*  s      @r   r<   r<     s     !M #$;<G

Ww
WWW *]:Nlm33Nlm33Nn++M:$FFN 8<Gr   zGextern\s+([\w\(\)]+)?\s*__shared__\s+([\w:<>\s]+)\s+(\w+)\s*\[\s*\]\s*;c                 8    U n[         R                  S U5      nU$ )a|  Match extern __shared__ type foo[]; syntax and use HIP_DYNAMIC_SHARED() MACRO instead.
   https://github.com/ROCm-Developer-Tools/HIP/blob/master/docs/markdown/hip_kernel_language.md#__shared__
Example:
    "extern __shared__ char smemChar[];" => "HIP_DYNAMIC_SHARED( char, smemChar)"
    "extern __shared__ unsigned char smem[];" => "HIP_DYNAMIC_SHARED( unsigned char, my_smem)"
c                     SU R                  S5      =(       d    S SU R                  S5       SU R                  S5       S3$ )	NzHIP_DYNAMIC_SHARED(r   r   r   r   r   r  r   r   r   s    r   r   'replace_extern_shared.<locals>.<lambda>  s:    )#))A,*<")=Qsyy|nBsyyYZ|n\]^r   )RE_EXTERN_SHAREDr'  )r4  r*  s     r   r=   r=     s(     !M$((^`moM r   c                    [         R                  R                  U 5      (       a   eU(       d  [        U 5      (       d  U $ [         R                  R	                  U 5      u  p#[         R                  R                  U5      u  pEUS:X  a  SnUnUnUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUS	:w  a  UR                  SS5      nU(       d%  X':X  a   [         R                  R                  US5      nU(       a  X':X  a  XE-   U:X  a  US
-   n[         R                  R                  X$U-   5      $ )z+
Returns the new name of the hipified file
.cu.hipcudahipCUDAHIPTHCTHHzcaffe2/core_hip)rt   ru   isabsr?   r   splitextr   r   )r   r   dirpathr\   rootrF  orig_filenameorig_dirpaths           r   r>   r>   "  s)    ww}}\****(E(El3G  *IDH e|MLoofe,Goofe,GooeU+G<<&D<<&D-||E5)G$;'',,w. 7TZM<Yf}77<<,,r   c                     [         R                  R                  U 5      (       a   eU R                  S5      (       a  gU R                  S5      (       a  gU R                  S5      (       a  gg)Ntorch/Fthird_party/nvfuser/tools/autograd/templates/Trt   ru   r\  
startswithr   s    r   r?   r?   j  s\    ww}}\****x((566:;;r   c                 6   [         R                  R                  U 5      (       a   eU R                  S5      (       a  U R                  S5      (       a  ggU R                  S5      (       a  gU R                  S5      (       a  gU R                  S5      (       a  gg)Nzaten/zaten/src/ATen/core/FTrc  rd  re  rf  rh  s    r   r@   r@   v  s    ww}}\****w''""#899x((566:;;r   c                 J    [        U 5      (       a  SU R                  5       ;   $ g)NsparseFr@   lowerrh  s    r   rA   rA     s$    |$$<--///r   c                     [        U 5      (       a?  SU R                  5       ;   a  gSU R                  5       ;   a  SU R                  5       ;   a  ggg)Nrk  TlinalgbatchlinearalgebralibblasFrl  rh  s    r   rB   rB     sL    |$$|))++++--*l.@.@.BBr   c                 6   [         R                  R                  U 5      (       a   eU R                  S5      (       a  g[         R                  R	                  U 5      n[         R                  R                  U5      u  p#SU;   =(       d    US;   =(       a    SU;  $ )Nzc10/cudaTgpurS  .cuhcudnn)rt   ru   r\  rg  basenamer]  )r   r\   r   rF  s       r   rC   rC     sx    ww}}\****z**ww-HWWh'FAX7!7VgX>UVr   c                       \ rS rSrSrS rSrg)TrieNodei  zvA Trie node whose children are represented as a directory of char: TrieNode.
A special char '' represents end of word
c                     0 U l         g rO   childrenr'   s    r   r$   TrieNode.__init__  s	    r   rz  N)r   r   r   r   r   r$   r   r   r   r   rx  rx    s    r   rx  c                   f    \ rS rSrSrS rS rS rS rS r	\
R                  S 5       rS	 rS
 rSrg)rD   i  zCreates a Trie out of a list of words. The trie can be exported to a Regex pattern.
The corresponding Regex should match much faster than a simple Regex union.c                     [        5       U l        [        R                  " SS9U l        U R                  R                  5       U l        g)z,Initialize the trie with an empty root node.F)usedforsecurityN)rx  r_  hashlibmd5_hashdigest_digestr'   s    r   r$   Trie.__init__  s/    J	[[7
zz((*r   c                 B   U R                   R                  UR                  5       5        U R                   R                  5       U l        U R
                  nU H6  nUR                  R                  U[        5       5        UR                  U   nM8     SUR                  S'   g)zAdd a word to the Trie. Tr   N)	r  updateencoder  r  r_  r{  
setdefaultrx  r#   wordnoder  s       r   rw   Trie.add  su    

$++-(zz((*yyDMM$$T8:6==&D  !br   c                     U R                   $ )zReturn the root node of Trie. )r_  r'   s    r   dump	Trie.dump  s    yyr   c                 .    [         R                  " U5      $ )zEscape a char for regex. )rD  escape)r#   r  s     r   quote
Trie.quote  s    yyr   c                     U R                   nU H#  nX2R                  ;   a  UR                  U   nM#    g   SUR                  ;   $ )zRSearch whether word is present in the Trie.
Returns True if yes, else return FalseFr   )r_  r{  r  s       r   rE  Trie.search  sC     yyD}}$}}T*	  T]]""r   c                    UnSUR                   ;   a(  [        UR                   R                  5       5      S:X  a  g/ n/ nSn[        UR                   R                  5       5       Hv  n[	        UR                   U   [
        5      (       aO   U R                  UR                   U   U R                  5      nUR                  U R                  U5      U-   5        Mt  SnMx     [        U5      S:  + n	[        U5      S:  aJ  [        U5      S:X  a  UR                  US   5        O&UR                  SSR                  U5      -   S-   5        [        U5      S:X  a  US   n
OSSR                  U5      -   S	-   n
U(       a  U	(       a  U
S
-  n
U
$ SU
 S3n
U
$ ! [         a%    UR                  U R                  U5      5         GMY  f = f)zConvert a Trie into a regular expression pattern

Memoized on the hash digest of the trie, which is built incrementally
during add().
r   r   Nr   []z(?:|r   ?z)?)r{  r   keyssorted
isinstancerx  _patternr  r   r  	Exceptionr   )r#   r_  r  r  altccqr  recursecconlyr   s              r   r  Trie._pattern  s    3t}}'9'9';#<#A4==--/0D$---x880"mmDMM$,?NGJJtzz$/'9:  1 X!r7Q;2w!|

2a5!

3,s23s8q=VFSXXc]*S0F#  vhb)- ! 0IIdjj.//0s   
AF*F>=F>c                 N    U R                  U R                  U R                  5      $ z#Export the Trie to a regex pattern.r  r_  r  r'   s    r   r   Trie.pattern      }}TYY55r   c                 N    U R                  U R                  U R                  5      $ r  r  r'   s    r   export_to_regexTrie.export_to_regex  r  r   )r  r  r_  N)r   r   r   r   r   r$   rw   r  r  rE  	functools	lru_cacher  r   r  r   r   r   r   rD   rD     sF    S+	!# ) )V66r   rD   PYTORCH_MAPr   z(?<=\W)(z)(?=\W)z#include "([^"]+)"z#include <([^>]+)>z"#define THC_GENERIC_FILE "([^"]+)"z\.cu\bc	                 
  ^ ^^^^^^^^^ [         R                  R                  [         R                  R                  T U5      5      m[        T   n	UT;  a%  SU	l        SU	l        [        R                  U	l	        U	$ [        [         R                  R                  UT 5      5      n
[        TSS9 nUR                  5       [        :X  a.  SU	l        SU	l        [        R                  U	l	        U	sSSS5        $ UR                  S5        UR!                  5       nSSS5        Wn[         R                  R                  [         R                  R                  T [#        U
T5      5      5      n[         R                  R%                  [         R                  R'                  U5      5      (       d.  TR)                  [         R                  R'                  U5      5        S mU4S jnT(       a  [*        R-                  TU5      nOf[/        U
5      (       a  [*        R-                  X5      nO@[1        U
5      (       a  [*        R-                  TU5      nOS	 n[2        R-                  UU5      nS#UUUUUUU UU4	S jjn[4        R-                  U" SS
5      U5      n[6        R-                  U" SS5      U5      n[8        R-                  U" S5      U5      nUR;                  S5      (       a:  UR=                  SS5      nUR=                  SS5      n[>        R-                  SU5      nT(       d  [A        UT5      nUR;                  S5      (       a  SU;  a  [C        U5      n[E        U5      nT(       aj  X:X  ae  [         R                  R'                  T5      [         R                  R'                  U5      :X  a%  TU	l        SU	l        [        R                  U	l	        U	$ TU:w  a  [G        TS5      (       a	  [        U-   nS
n[         R                  R%                  U5      (       a&  [        USS9 nUR!                  5       U:g  nSSS5        U(       aP   TR                  USSS9 nURI                  U5        SSS5        Xl        SU	l        [        R                  U	l	        U	$ Xl        S"U	l        [        R                  U	l	        U	$ ! , (       d  f       GN= f! , (       d  f       N= f! , (       d  f       Ny= f! [J         a{  n[M        [N        RP                   SU SURR                   ST S[N        RT                   3	[V        RX                  S 9  TU	l        S!U	l        [        R                  U	l	        U	s SnA$ SnAff = f)$z;Executes the CUDA -> HIP conversion on the specified file. Nz[ignored, not to be hipified]zutf-8)encodingz#[ignored, input is hipified output]r   c                 2    [         U R                  S5         $ Nr   )r  r   ms    r   pt_replpreprocessor.<locals>.pt_repl[  s    1771:&&r   c                 Z   > [         R                  U R                  S5      T" U 5      5      $ r  )PYTORCH_SPECIAL_MAPgetr   )r  r  s    r   pt_special_repl%preprocessor.<locals>.pt_special_repl^  s"    "&&qwwqz71:>>r   c                 2    [         U R                  S5         $ r  )
CAFFE2_MAPr   r  s    r   c2_replpreprocessor.<locals>.c2_replk  s    !!''!*--r   Tc                 ,   >	^ ^ UUUUUUUU	U
UU 4S jnU$ )Nc                   >^ U R                  S5      n[        R                  R                  U5      mUR	                  S5      (       d,  UR	                  S5      (       a@  UR	                  S5      (       d*  TR                  [        U R                  S5      T5      5      $ T(       Ga  [        U4S jT 5       5      (       Ga  S nS nT(       a  [        R                  R                  T5      n[        R                  R                  [        R                  R                  XA5      5      n[        R                  R                  U5      (       a  UnUnUc  T H  n[        R                  R                  TU5      n[        R                  R                  [        R                  R                  XA5      5      n[        R                  R                  U5      (       d  M  UnUnM     Uc  U R                  S5      $ U[        ;  a  [        TUTTTTTTT5	        OU[        ;   a  [        U   nUR                  [        R                   :X  a  [        R                  R#                  UT5      n[        R                  R                  [        R                  R                  T[        UT5      5      5      n	Xl        U[        U'   TR                  [        R                  R#                  U	b  U	U5      5      $ UU5      5      $ [        U   R$                  n
TR                  [        R                  R#                  U
b  U
U5      5      $ UU5      5      $ U R                  S5      $ )Nr   )z	ATen/cudazATen/native/cudazATen/native/nested/cudazATen/native/quantized/cudazATen/native/sparse/cudazATen/native/transformers/cudazTHC/rY  THCPc              3   D   >#    U  H  oR                  T5      v   M     g 7frO   r   )r   sr\   s     r   r   >preprocessor.<locals>.mk_repl.<locals>.repl.<locals>.<genexpr>  s     ,U9aZZ-A-A9s    r   )r   rt   ru   rv  rg  formatr>   r   dirnamerx   r   rv   r*   r4   r    r   r   r   r!   )r  r   
header_dirheader_filepathheader_dir_to_checkheader_path_to_checkheader_include_dirheader_resultheader_rel_pathheader_fout_pathhipified_header_filepathr\   r   r   r   r   r   include_current_dirr   r   r   r   templs              @r   repl+preprocessor.<locals>.mk_repl.<locals>.replq  s   
Aww''*H & ' ' e$$Q\\&-A-A||$5aggajBV$WXX$,U9,U)U)U!
"&&*,''//(*C'+-77??277<<H[;_+`(ww~~&:;;%8
*>"*.A*.0ggll;KM_.`+/1wwrww||L_?c/d,77>>*>??)<J.BO /B #*771:%"*==34D4C4=?RTY[k4H)Ubd %(;;$7$HM$22l6N6NN*,''///K[*\+-77??277<<HXHYZik  IA<B ,C(6F3?L+O<$||BGGOOP`Pl<LR\-^  _ _APR\-^  _ _+>+O+]+](||BGGOOPhPt4LJT%V W W9H*%V W W 771:r   r   )r  r  r  r   r   r   r   r   r   r   r   r   s   `` r   mk_replpreprocessor.<locals>.mk_replp  s    9	 9	t r   z#include "{0}"z#include <{0}>Fz#define THC_GENERIC_FILE "{0}"zCMakeLists.txtrW  rX  rY  rZ  rT  rs  	PowKernelz[skipped, no changes])rS  rt  .c.cc.cpp.h.hppwz[ok]zFailed to save z with "z", leaving z unchanged.filez[skipped, no permissions]z[skipped, already hipified])T)-rt   ru   rx   r   r*   r!   r"   r   r   r    r   r   r[   readlineHIPIFY_C_BREADCRUMBseekreadr>   rv   r  r   RE_PYTORCH_PREPROCESSORr'  rB   r@   RE_CAFFE2_PREPROCESSORRE_QUOTE_HEADERRE_ANGLE_HEADERRE_THC_GENERIC_FILEr   r   RE_CU_SUFFIXr7   r;   r<   r2   writePermissionErrorr   r0   rc   strerrorre   sysstderr)r   r   r   r   r   r   r   r   r   r   r   finoutput_sourceorig_output_source	fout_pathr  r  r  do_writefout_oldfoutr   r   r  s   ` ```````             @@r   rE   rE   5  s\    wwrww||,<hGHH'1My &*#>&2&7&7# ;K!LML	h	)S<<>00*.M'#HM *6*;*;M'  
*	) 	
 
* ' -=?PQ]_s?t uvI77>>"''//)455277??956'?
 /33G]K<((377WM\**377OM.266wNM; ;x $''0@$(GWM#''0@%(H-XM'++G4T,UWdeM )**%--fe<%--eU;$((? -mUC 	/**80K.}= %]3M 	/GGOOH%)CC&.#6&2&7&7# 9!1(<n!o!o+m;H	ww~~i  )g.(}}-7H /	!	3AT

=) B*3'#)M *6*;*;M'   '0#<&2&7&7#C 
*	)X /. BA  	!W__%_YKwqzzlR]^f]ggrszss  sA  Bzz#*2M'#>M *6*;*;M'  	!sU   /=R76!R7 S	S+ S'+S+ 7
S	
S
S($S+ +
U05A0U+%U0+U0c                 r  ^ [        U S5       nUR                  5       nU(       a5  [        R                  " S[        R                  " U5       S3U4S jU5      nOUR                  UT5      nUR                  S5        UR                  U5        UR                  5         S S S 5        g ! , (       d  f       g = f)Nr+z\b(z)\bc                    > T$ rO   r   )r  replace_strings    r   r   +file_specific_replacement.<locals>.<lambda>  s    nr   r   )	r/   r  rD  r'  r  r   r  r  truncate)r   search_stringr  strictr   contentss     `   r   rF   rF     s    	x	!668vvRYY}%=$>cBD\^fgH''~FH	q			

 
		s   BB((
B6c                    [        U S5       nUR                  5       nUS   S:w  a  US   S:w  a  SU S3nSU S3U-   nUR                  S5        UR                  U5        UR	                  5         S S S 5        g ! , (       d  f       g = f)	Nr  r   r  r   r  r  z	#include z 
)r/   r  r  r  r  )r   headerr   r  s       r   rG   rG     s|    	x	!668!9r
c 1]Fxs+x7	q			

 
		s   A$A::
Bc                 *    U R                  SS5      n U $ )z<Static global kernels in HIP results in a compilation error.z __global__ staticrI  )r   )in_txts    r   rH   rH     s    ^^0,?FMr   z#include .*\nc                    / nSSS.nU nUS-   nU[        U5      :  a  X   S:X  a  US==   S-  ss'   OUX   S:X  a  US==   S-  ss'   O?X   S:X  a  US==   S-  ss'   O)X   S:X  a!  XS-
     S:w  a  US   S:  a  US==   S-  ss'   US   S:X  a  US   S:X  a  UR                  XTS	.5         U$ US   S:X  a)  US   S:X  a   X   S
:X  a  UR                  XTS	.5        US-   nUS-  nU[        U5      :  a  M  U$ )a'  Return the list of arguments in the upcoming function parameter closure.
Example:
string (input): '(blocks, threads, 0, THCState_getCurrentStream(state))'
arguments (output):
    '[{'start': 1, 'end': 7},
    {'start': 8, 'end': 16},
    {'start': 17, 'end': 19},
    {'start': 20, 'end': 53}]'
r   )r  r   r   r   r   r  r  -r   r   )r   r   )r   r  	argumentsclosurescurrent_positionargument_start_poss         r   rI   rI     sR    IH )A- S[
(#s*SMQM%,SMQM%,SMQM%,18L1MQT1TYabeYfijYjSMQM C=A(3-1"4'9ST  C=A(3-1"49QUX9X'9ST!1A!5A+ S[
(. r   c                     U R                  5       S;   a  gU R                  5       S;   a  g[        R                  " S5      e)z{ArgumentParser doesn't support type=bool. Thus, this helper method will convert
from possible string types to True / False.)yestruety1T)nofalser   r   0FzBoolean value expected.)rm  argparseArgumentTypeError)vs    r   rJ   rJ   ;  s:     	wwy22	
4	4(()BCCr   project_directoryshow_detailedheader_extensionsextra_fileshipify_extra_files_onlyc                   ^^^	 U S:X  a  [         R                  " 5       n [         R                  R                  U 5      (       d!  [	        S5        [
        R                  " S5        U(       d  U R                  S5        U S-   nX:w  aC  T Vs/ s H  oR                  X5      PM     snmT	 Vs/ s H  nUR                  X5      PM     snm	[         R                  R                  U5      (       d  [        R                  " X5        [        [        [        T5      5      m[        [        [        T	5      5      m	[        [        UTT	UUUS95      n[        U5      nU H`  n[         R                  R!                  U5      (       d   [         R                  R#                  UU5      nUU;  d  MO  UR%                  U5        Mb     SSKJn  U H  n[         R                  R!                  U5      (       a	  U" U5      nO&U" [         R                  R#                  UU5      5      nUR+                  UU	U4S	 jUR-                  S
5       5       5        M     Uc	  [/        SS9n/ / S.nU(       d  UOU H  n[1        UUUUUXX5	        M     [	        [2        R4                  S-   [2        R6                  -   [
        R8                  S9  U(       a  [;        U5        [<        $ s  snf s  snf )Nr   z,The project folder specified does not exist.r   r   _amd)r   r   r   r   r   r   )Pathc              3     >#    U  H{  oR                  5       (       d  M  [        [        U5      T5      (       d  M5  [        [        U5      T5      (       a  MQ  [        UR                  T5      (       d  Mn  [        U5      v   M}     g 7frO   )is_filer   strr2   name)r   ru   r  r   r   s     r   r   hipify.<locals>.<genexpr>  s_      
"D$ITH- c$i1  !,=> CII"Ds   BBBB4Br  T)rk   )r   r   z-Successfully preprocessed all matching files.r  )rt   getcwdru   rv   r   r  exitrstripr   shutilcopytreer(  mapr   r3   rl   r\  r   r   pathlibr  extendrglobr1   r4   r0   rb   re   r  r5   r*   )r  r  r   r  r   r   r   r  r   r   r   r   r   r  r   includerY   r   all_files_setr   r  r  header_include_dir_pathr   r   s      `  `  `               r   rK   rK   F  sb   " BIIK 77>>+,,<=   %,v5,X`aX`WOO$5HX`aU\]U\66>>"3FU\] 77>>*++)<Cx01H3}g./G'(8807J:K=QS TI 	NMww}}Q-q1AM!Q	  177==+,,&*+=&>#&*277<<8HJ\+]&^# 
"9"?"?"D
 	
 2 (DA	 46"ME&=Y;N'(8(IOb(-/?W`	q O 
'//K
Kgll
Zadakakl eg b]s   
K+K)r   r   r   FFr   )F)rS  rt  r  r  r  r  z.inr  )rt  r  r  r   r   )r  r   Fr   TFFFN)cr   r  r   rD  r(  r  rt   r   r   cuda_to_hip_mappingsr   r   typingr   collections.abcr   r	   r
   enumr   r  r  r   r   dictr"  HipifyFinalResultr  r*   __annotations__PYTORCH_TEMPLATE_MAP__all__r  r.   r/   r0   r1   r   boolr2   r   r3   r(  r4   r5   r6   compiler&  r7   r8   r9   r:   	RE_ASSERTr;   rK  r<   rQ  r=   r>   r?   r@   rA   rB   rC   rx  rD   CAFFE2_TRIEr  PYTORCH_TRIEr  objectr  mappingr  itemssrcr   dst	meta_data
API_CAFFE2rw   API_SPECIALr  API_PYTORCHr  r  r  r  r  r  r  rE   rF   rG   rH   
RE_INCLUDErI   rJ   rK   r   r   r   <module>rI     s`  0   	  
 	  6 5  $ -   4 D D l*+ T )+ & +!+*= s. .1
 & D% % %9s 9 9T 9
K  !"'%*))) ) 	)
  ) #) 08})X+++ + &	+
 CI+ + #+ (+ + !%+2
\ F ::>? Wt#LE
E
 JJ()	  =>: ::hi E-PW \6 \6| f
v!#T#v+ #  #Ggw''''mmo
UAh!"I	y0S! $$	1kooc26N6N+.#C(#&C   	1i6K6KS\6\OOC !JsO & $  K$?$?$AB **1M1M1O0PPW%XY **23**23jj!FG zz)$vvv v &	v
 CIv v #v (v v !-vp	 ZZ()
+\D  T"8$&#"!&$)04RRR R  	R
 R "R R R R R R R R "R ,-R  !Rr   