
    S j)              !          % 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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
 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*S_S1 jr+S2 r,\RZ                  " S35      r.S4 r/S5 r0S6 r1S7 r2\RZ                  " S85      r3S9 r4\RZ                  " S:5      r5S; r6\RZ                  " S<5      r7S= r8S`S> jr9S\%4S? jr:S\%4S@ jr;SA r<S\%4SB jr=SC r> " SD SE5      r? " SF SG5      r@\@" 5       rA0 rB\\\C4   \SH'   \	 HI  rD\E" \D\5      (       d  \F" SI5      e\DR                  5        H  u  rHrI\AR                  \H5        \I\B\H'   M     MK     \RZ                  " SJ\AR                  5        SK35      rL\RZ                  " SL5      rM\RZ                  " SM5      rN\RZ                  " SN5      rO\RZ                  " SO5      rP S(\S)\S*\S+\S,\\\)4   S-\%S&\%S.\#S/\%S\4SP jrQS`S_SQ jjrRS_SR jrSSS rT\RZ                  " ST5      rUSU rVSV\S\%4SW jrW              SaSX\SY\%S\SZ\S(\S+\S#\S[\S%\%S$\S/\%S-\%S&\%S\\%S.\#S-  S\4 S] jjrXg)ba  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   )CUDA_TO_HIP_MAPPINGS)MATH_TRANSPILATIONS)CAFFE2_PATH_MAPPINGS)Iterator)MappingIterable)Enumc                 >    [         R                  " SU  3[        SS9  g )Nz-hipify version 2.0.0 no longer uses function    )
stacklevel)warningswarnFutureWarning)names    q/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/torch/utils/hipify/hipify_python.py_deprecatedr   -   s    MMA$H-def    c                       \ rS rSrSrSrSrg)CurrentState0   r   r    N)__name__
__module____qualname____firstlineno__INITIALIZEDDONE__static_attributes__r   r   r   r   r   0   s    KDr   r   c                   ,    \ rS rSrSS jrS\4S jrSrg)HipifyResult4   returnNc                 *    Xl         X l        SU l        g )N current_statehipified_pathstatus)selfr'   r(   s      r   __init__HipifyResult.__init__5   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__:   sF    01C1C0DDVW[WiWiVjjtuy  vA  vA  uB  C  	Dr   r&   r#   N)r   r   r   r   r+   strr/   r   r   r   r   r!   r!   4   s    
D 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_fileTriepreprocessorfile_specific_replacementfile_add_headerfix_static_global_kernelsextract_argumentsstr2boolr   r!   hipifyc                   :   ^  \ rS rSrSU 4S jjrS\4S jrSrU =r$ )r7   N   r#   c                 0   > [         TU ]  U5        Xl        g N)superr+   message)r*   rZ   	__class__s     r   r+   InputError.__init__Q   s    !r   c                      SU R                    3$ )NzInput error: rZ   r.   s    r   r/   InputError.__str__U   s    t||n--r   r^   r1   )	r   r   r   r   r+   r2   r/   r   __classcell__)r[   s   @r   r7   r7   N   s    . . .r   r7   c                     [        XSS9$ )Nignore)errors)open)filenamemodes     r   r8   r8   Y   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)r9   ^   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   r9   r9   ^   s*    FFGGDDDIr   r9   c                   F    \ rS rSrSrS
SS jjrS rS rS
SS jjrS r	S	r
g)r:   q   z+Context Manager to clean up generated filesNc                 <    Xl         [        5       U l        / U l        g rX   )keep_intermediatessetfiles_to_cleandirs_to_clean)r*   rt   s     r   r+   GeneratedFileCleaner.__init__s   s    "4!er   c                     U $ rX   r   r.   s    r   	__enter__GeneratedFileCleaner.__enter__x   s    r   c                     [         R                  R                  U5      (       d8  U R                  R	                  [         R                  R                  U5      5        [        U/UQ70 UD6$ rX   )ospathexistsrv   addabspathrd   )r*   fnargskwargss       r   rd   GeneratedFileCleaner.open{   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)
r}   r~   splitr   makedirsisdirmkdirrw   appendr   )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)rt   rv   r}   unlinkrw   rmdir)r*   typevalue	tracebackfds         r   __exit__GeneratedFileCleaner.__exit__   sO    &&((		! )''"- . 'r   )rw   rv   rt   Fr1   )r   r   r   r   __doc__r+   rz   rd   r   r   r   r   r   r   r:   r:   q   s    5 
);r   r:   r~   r#   c                 B    U R                  [        R                  S5      $ )N/)replacer}   sep)r~   s    r   _to_unix_pathr      s    <<$$r   re   
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rX   endswith).0ere   s     r   	<genexpr>#match_extensions.<locals>.<genexpr>   s     8Zx  ##Zs   !any)re   r   s   ` r   r;   r;      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rX   )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              #   6  #    [        U5      n[        R                  " U SS9 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	 Hj  n[        [        R                  R                  X{5      5      n[        X5      (       d  M=  [        X5      (       a  MO  [        X5      (       d  X;   d  Mf  Uv   Ml     M     g 7f)NT)topdown.z.gitbuildthird_partyzthird_party/nvfuser)ru   r}   walkr~   relpathremover   r   joinr   r;   )r   r   r   r   r   r   exact_matchesabs_dirpathdirs	filenamesrel_dirpathre   r   s                r   r<   r<      s      MM +-'')T*J&Iggook=#~F#$G$$M*12!H$RWW\\+%HIH ,,!(44%h;;x?X " +Ks   CD#D5DD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)r}   r~   r   r   r!   r   r   r3   rN   r)   printr(   )r   r   r   r   r   r   r   r   r   fin_pathhipify_resultresults               r   r=   r=      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   r>   r>      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   r?   r?      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   )findr7   r   )r  
kernel_endkernel_positionskernel_starts       r   find_kernel_bounds1processKernelLaunches.<locals>.find_kernel_boundsQ  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_commentsi  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  r?   r   rR   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   r@   r@     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   rA   rA     s     F
CNG
L!
!a(!U*%)"!%(*/AaKF{~%q L!
!  r   c                     [        XSS/S9$ )z%Finds the first balanced parentheses.{}r   rA   r;  r   s     r   rB   rB         l#sDDr   c                     [        XSS/S9$ )z!Finds the first balanced bracket.r   r   r   rC  rD  s     r   rC   rC     rE  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   )r;  r1  funcs      r   rD   rD     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   extr1  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_SYNCTHREADSrL  )r;  headershasDeviceLogicr1  s      @r   rE   rE     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.
See: https://github.com/ROCm/hip/blob/master/docs/markdown/hip_kernel_language.md#__shared__
Examples:
    "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.  )r;  r1  s     r   rF   rF     s(     !M$((^`moM r   c                    [         R                  R                  U 5      (       a  [        S5      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
$rel_filepath must be a relative path.cu.hipcudahipCUDAHIPTHCTHHzcaffe2/core_hip)	r}   r~   isabsAssertionErrorrH   r   splitextr   r   )rel_filepathr   dirpathre   rootrM  orig_filenameorig_dirpaths           r   rG   rG   "  s/    
ww}}\""CDD(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  [        S5      eU R	                  S5      (       a  gU R	                  S5      (       a  gU R	                  S5      (       a  gg)NrZ  torch/Fthird_party/nvfuser/tools/autograd/templates/T)r}   r~   rd  re  
startswithrg  s    r   rH   rH   k  sb    	ww}}\""CDDx((566:;;r   c                    [        S5        [        R                  R                  U 5      (       a  [	        S5      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U R                  S
5      (       a  gU R                  S5      (       a  gg)NrI   rZ  zaten/zaten/src/ATen/core/FTrm  rn  zthird_party/fbgemm/zthird_party/mslk/ro  )r   r}   r~   rd  re  rp  rq  s    r   rI   rI   x  s    !"	ww}}\""CDDw''""#899x((566455233:;;r   c                 `    [        S5        [        U 5      (       a  SU R                  5       ;   $ g)NrJ   sparseFr   rI   lowerrq  s    r   rJ   rJ     s-    "#|$$<--///r   c                     [        S5        [        U 5      (       a?  SU R                  5       ;   a  gSU R                  5       ;   a  SU R                  5       ;   a  ggg)NrK   rt  TlinalgbatchlinearalgebralibblasFru  rq  s    r   rK   rK     sU    !"|$$|))++++--*l.@.@.BBr   c                 ^   [        S5        [        R                  R                  U 5      (       a  [	        S5      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;  $ )NrL   rZ  zc10/cudaTgpur[  .cuhcudnn)r   r}   r~   rd  re  rp  basenamerf  )rg  re   r   rM  s       r   rL   rL     s    $%	ww}}\""CDDz**ww-HWWh'FAX7!7VgX>UVr   c                   "    \ rS rSrSrSS j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
Nc                     0 U l         g rX   childrenr.   s    r   r+   TrieNode.__init__  s	    r   r  r1   )r   r   r   r   r   r+   r   r   r   r   r  r    s    r   r  c                   n    \ rS rSrSrSS jrSS jrS rS rS r	\
R                  S	 5       rS
 rS rSrg)rM   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.Nc                     [        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)r  ri  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  ri  r  
setdefaultr  r*   wordnoder  s       r   r   Trie.add  su    

$++-(zz((*yyDMM$$T8:6==&D  !br   c                     U R                   $ )zReturn the root node of Trie. )ri  r.   s    r   dump	Trie.dump  s    yyr   c                 .    [         R                  " U5      $ )zEscape a char for regex. )rK  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%   )ri  r  r  s       r   rL  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
isinstancer  _patternr  r   r  	Exceptionr   )r*   ri  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  ri  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  ri  r1   )r   r   r   r   r   r+   r   r  r  rL  	functools	lru_cacher  r   r  r   r   r   r   rM   rM     sF    S+	!# ) )V66r   rM   PYTORCH_MAPz=Expected each mapping in CUDA_TO_HIP_MAPPINGS to be a Mappingz(?<=\W)(z)(?=\W)z#include "([^"]+)"z#include <([^>]+)>z"#define THC_GENERIC_FILE "([^"]+)"z\.cu\bc	                 
  ^ ^^^^^^^^ [         R                  R                  [         R                  R                  T U5      5      m[	        U5      n[
        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 n[*        R-                  X5      n[.        R0                  " 5        HW  u  nnSU;   a9  SU;  a3  S	[2        R4                  " U5      -   n[2        R,                  " UUU5      nME  UR7                  UU5      nMY     S#UUUUUUU UU4	S jjn[8        R-                  U" SS
5      U5      n[:        R-                  U" SS5      U5      n[<        R-                  U" S5      U5      nUR?                  S5      (       a:  UR7                  SS5      nUR7                  SS5      n[@        R-                  SU5      nT(       d  [C        UT5      nUR?                  S5      (       a  SU;  a  [E        U5      n[G        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  [I        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RK                  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! [L         a{  n[O        [P        RR                   SU SURT                   ST S[P        RV                   3	[X        RZ                  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_replV  s    1771:&&r   z/hip/z
(?<!/hip/)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
OUU5      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/ra  THCPc              3   D   >#    U  H  oR                  T5      v   M     g 7frX   r   )r   sre   s     r   r   >preprocessor.<locals>.mk_repl.<locals>.repl.<locals>.<genexpr>~  s     ,U9aZZ-A-A9s    r   )r   r}   r~   r  rp  formatrG   r   dirnamer   r   r   r3   r=   r'   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_filepathre   r   r   r   r   r   include_current_dirr   r   r   r   templs              @r   repl+preprocessor.<locals>.mk_repl.<locals>.replo  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+]+](||M"''//^v  _CBZGVXb3d %e f f 771:r   r   )r  r  r  r   r   r   r   r   r   r   r   r   s   `` r   mk_replpreprocessor.<locals>.mk_repln  s    9	 9	t r   z#include "{0}"z#include <{0}>Fz#define THC_GENERIC_FILE "{0}"zCMakeLists.txtr_  r`  ra  rb  r\  r|  	PowKernelz[skipped, no changes])r[  r}  .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).r}   r~   r   r   r   r3   r(   r)   r   r   r'   r   rd   readlineHIPIFY_C_BREADCRUMBseekreadrG   r   r  r   RE_PYTORCH_PREPROCESSORr.  r   itemsrK  r  r   RE_QUOTE_HEADERRE_ANGLE_HEADERRE_THC_GENERIC_FILEr   RE_CU_SUFFIXr@   rD   rE   r;   writeOSErrorr   r9   rl   strerrorrn   sysstderr)r   r   r   r   r   r   r   r   r   r   rg  finoutput_sourceorig_output_source	fout_pathr  	cuda_pathhip_pathr   r  do_writefout_oldfoutr   r   s   ` ```````               @r   rN   rN   /  ss    wwrww||,<hGHHX&H'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' ,//GM  499;	8 h7)#; $bii	&::GFF7HmDM *11)XFM  <; ;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#I 
*	)^ /. BA  	!W__%_YKwqzzlR]^f]ggrszss  sA  Bzz#*2M'#>M *6*;*;M'  	!sU   9=R< !R<%S	S0 S,+S0 <
S
S
S-)S0 0
U5:A0U0*U50U5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$ rX   r   )r&  replace_strings    r   r  +file_specific_replacement.<locals>.<lambda>  s    nr   r   )	r8   r  rK  r.  r  r   r  r  truncate)r   search_stringr  strictr   contentss     `   r   rO   rO     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 
)r8   r  r  r  r  )r   headerr   r  s       r   rP   rP     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__ staticrP  )r   )in_txts    r   rQ   rQ     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   rR   rR     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   vc                     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.)rv  argparseArgumentTypeError)r  s    r   rS   rS   7  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rX   )is_filer   r2   r;   r   )r   r~   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)rt   )r   r   z-Successfully preprocessed all matching files.r  )r}   getcwdr~   r   r   r  exitrstripr   shutilcopytreer/  mapr   r<   ru   rd  r   r   pathlibr  extendrglobr:   r=   r9   rk   rn   r  r>   r3   )r  r  r   r  r   r   r   r  r   r   r   r   r   r  r   includerb   r   all_files_setr   r  r  header_include_dir_pathr   r   s      `  `  `               r   rT   rT   B  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i b]s   
K+K)r   r   r   FFr1   r   )F)r[  r}  r  r  r  r  z.inr  )r}  r  r  r%   r   )r!  r   Fr   TFFFN)Yr   r  r   rK  r&  r  r}   r   cuda_to_hip_mappingsr   r   r   collections.abcr   r   r	   enumr
   r  r  r   r   r!   dictr2   HipifyFinalResultr  r3   __annotations__PYTORCH_TEMPLATE_MAP__all__r  r7   r8   r9   r:   r   boolr;   r   r<   r/  r=   r>   r?   compiler-  r@   rA   rB   rC   	RE_ASSERTrD   rR  rE   rX  rF   rG   rH   rI   rJ   rK   rL   r  rM   PYTORCH_TRIEr  objectmappingr  	TypeErrorr  srcdstr   r  r  r  r  r  r  rN   rO   rP   rQ   
RE_INCLUDErR   rS   rT   r   r   r   <module>rA     s  0   	  
 	  6 5 6 $ -   g4 D D l*+ T )+ & +!+*= s. .1
 & D% % %9s 9 9T 9
K  !"'%*### # 	#
  # ## 08}#L+++ + &	+
 CI+ + #+ (+ + !%+2
\ F ::>? Wt#LE
E
 JJ()	  =>: ::hi "F-R	T 	T *	T 		W \6 \6| v!#T#v+ ##Ggw''WXXMMOSC $ $ **1M1M1O0PPW%XY **23**23jj!FG zz)$zzz z &	z
 CIz z #z (z z !-zx	 ZZ()
)XD D D  T"8$&#"!&$)-1SSS S  	S
 S "S S S S S S S S "S $d*S  !Sr   