
    `iη              !          U d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 ddl	mZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlZddlZ G d de          Z G d d          Zeeef         ZdZi Zeed<   	 dddZg dZ G d de           Z!d Z" G d d          Z# G d d          Z$dedefdZ%dedede&fd Z'd! Z(	 	 	 	 	 dcd$ed%ed&eded'e&d(e&dee         fd)Z)d*ed+ed,ed-ed.eee*f         d/e&d(e&d0e$d1e&ddfd2Z+d3 Z,d4 Z- ej.        d5          Z/d6 Z0d7 Z1d8 Z2d9 Z3 ej.        d:          Z4d; Z5 ej.        d<          Z6d= Z7 ej.        d>          Z8d? Z9ddd@Z:dA Z;dB Z<dC Z=dD Z>dE Z? G dF dG          Z@ G dH dI          ZA eA            ZBi ZC eA            ZDi ZEeeeFf         edJ<   i ZGe
D ]ZH eIeHe          sJ eHJ                                D ]\  ZKZLeLd         ZMeLdd         ZNejO        eNvr?eDP                    eK           ejQ        eNv reER                    eKdK          reMeGeK<   neMeEeK<   ejS        eNvr#ejQ        eNvreBP                    eK           eMeCeK<    ej.        eBT                                          ZU ej.        dLeDT                                 dM          ZV ej.        dN          ZW ej.        dO          ZX ej.        dP          ZY ej.        dQ          ZZ	 d*ed+ed,ed-ed.eee*f         d/e&d(e&d0e$d1e&defdRZ[dddSZ\dT Z]dU Z^ ej.        dV          Z_dW Z`dX Za	 	 	 	 	 	 	 	 	 	 	 	 	 	 ded]ed^e&ded_ed*ed-ed%ed`ed'e&d&ed1e&d/e&d(e&dae&d0ee$         def dbZbdS )fa   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                       e Zd ZdZdZdS )CurrentStater      N)__name__
__module____qualname__INITIALIZEDDONE     t/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/utils/hipify/hipify_python.pyr   r   -   s        KDDDr   r   c                       e Zd Zd Zd ZdS )HipifyResultc                 0    || _         || _        d| _        d S )N current_statehipified_pathstatus)selfr   r   s      r   __init__zHipifyResult.__init__2   s    **r   c                 6    d| j          d| j         d| j         S )NzHipifyResult:: current_state: z, hipified_path : z
, status: r   r   s    r   __str__zHipifyResult.__str__7   sG     C1C  C  CW[Wi  C  Cuy  vA  C  C  	Dr   N)r   r   r   r    r#   r   r   r   r   r   1   s7          
D D D 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_filer=   Triepreprocessorfile_specific_replacementfile_add_headerfix_static_global_kernelsextract_argumentsstr2boolr   r   hipifyc                   $     e Zd Z fdZd Z xZS )r(   c                 X    t                                          |           || _        d S N)superr    message)r   rJ   	__class__s     r   r    zInputError.__init__N   s&    !!!r   c                     d| j          S )NzInput error: )rJ   r"   s    r   r#   zInputError.__str__R   s    -t|---r   )r   r   r   r    r#   __classcell__)rK   s   @r   r(   r(   K   sG            . . . . . . .r   r(   c                 &    t          | |d          S )Nignore)errors)open)filenamemodes     r   r)   r)   V   s    $x0000r   c                   .    e Zd ZdZdZdZdZdZdZdZ	dZ
d	S )
r*   z[95mz[94mz[92mz[93mz[91mz[0mz[1mz[4mN)r   r   r   HEADEROKBLUEOKGREENWARNINGFAILENDCBOLD	UNDERLINEr   r   r   r*   r*   [   s6        FFGGDDDIIIr   r*   c                   4    e Zd ZdZd	dZd Zd Zd	dZd ZdS )
r+   z+Context Manager to clean up generated filesFc                 H    || _         t                      | _        g | _        d S rH   )keep_intermediatessetfiles_to_cleandirs_to_clean)r   r_   s     r   r    zGeneratedFileCleaner.__init__p   s$    "4!eer   c                     | S rH   r   r"   s    r   	__enter__zGeneratedFileCleaner.__enter__u   s    r   c                     t           j                            |          s7| j                            t           j                            |                     t          |g|R i |S rH   )ospathexistsra   addabspathrQ   )r   fnargskwargss       r   rQ   zGeneratedFileCleaner.openx   s[    w~~b!! 	9##BGOOB$7$7888B(((((((r   c                    t           j                            |          \  }}|s"t           j                            |          \  }}|r8|r6t           j                            |          s|                     |d           t           j                            |          r|sMt          j        |           | j                            t           j        	                    |                     d S d S )NT)exist_ok)
rf   rg   splitrh   makedirsisdirmkdirrb   appendrj   )r   dnro   parentns        r   rq   zGeneratedFileCleaner.makedirs}   s    GMM"%%	 	.f--IFA 	1a 	1v 6 6 	1MM&4M000w}}R   	; 	;HRLLL%%bgoob&9&9:::::	; 	;r   c                     | j         sE| j        D ]}t          j        |           | j        d d d         D ]}t          j        |           d S d S )N)r_   ra   rf   unlinkrb   rmdir)r   typevalue	tracebackfds         r   __exit__zGeneratedFileCleaner.__exit__   sn    & 	(  	!'"-  		 	 r   NF)	r   r   r   __doc__r    rd   rQ   rq   r   r   r   r   r+   r+   n   so        55       
  ) ) )
; ; ; ;    r   r+   rg   returnc                 B    |                      t          j        d          S )N/)replacerf   sep)rg   s    r   _to_unix_pathr      s    <<$$$r   rR   
extensionsc                 :     t           fd|D                       S )z<Helper method to see if filename ends with certain extensionc              3   B   K   | ]}                     |          V  d S rH   endswith).0erR   s     r   	<genexpr>z#match_extensions.<locals>.<genexpr>   s1      88x  ##888888r   any)rR   r   s   ` r   r,   r,      s&    8888Z888888r   c                 :     t           fd|D                       S )Nc              3   B   K   | ]}t          j         |          V  d S rH   )fnmatch)r   patternfilepaths     r   r   z_fnmatch.<locals>.<genexpr>   s/      JJgwx11JJJJJJr   r   )r   patternss   ` r   _fnmatchr      s&    JJJJJJJJJJr   r   F	root_pathincludesignoresout_of_place_onlyis_pytorch_extensionc              #     K   t          |          }t          j        | d          D ]W\  }}}	t          j                            ||           }
|
dk    r`d|v r|                    d           d|v r|                    d           d|v r*|                    d           |                    d           |	D ]}t          t          j                            ||                    }t          t          j                            |
|                    }t          ||          r[t          ||          sKt          ||          s||v r7|s1t          |          st          |          s|rt          |          s|V  ȐYd S )NT)topdown.z.gitbuildthird_partyzthird_party/nvfuser)r`   rf   walkrg   relpathremovert   r   joinr   r,   r:   r=   r9   )r   r   r   r   r   r   exact_matchesabs_dirpathdirs	filenamesrel_dirpathrR   r   rel_filepaths                 r   r-   r-      s      MMM +-')T*J*J*J  &dIgook9==#~~F###$G$$$$$M***1222! 	 	H$RW\\+x%H%HIIH(k8)L)LMML 8,,
!(G44
 &h
;;
 @H=?X?X+ !*<88 !AST`AaAa ! ( !1N1N ! 	 r   output_directoryr   	all_filesheader_include_dirsstatship_clang_launch	clean_ctxshow_progressc	                 ^   t           j                            t           j                            | |                    }	t	          t
          j        |	          }
|
t          |	<   t          | ||||||||	  	        }|r'd|j	        vrt          |	d|j        |j	        d           |t          |	<   d S )N)r   r   ignoredz->T)flush)rf   rg   rj   r   r   r   r   r$   r?   r   printr   )r   r   r   r   r   r   r   r   r   fin_pathhipify_resultresults               r   r.   r.      s     wrw||,<hGGHHH |/GW_```M$1!*HiATV[*,@)]\ \F  =&-77d &-t	= 	= 	= 	= %+!!!r   c                     d | d         D             }t          dt          |          d           t          d                    |                     t          dt          | d                   d           d S )Nc                     h | ]\  }}|S r   r   )r   	cuda_call	_filepaths      r   	<setcomp>z compute_stats.<locals>.<setcomp>   s    \\\'=	9\\\r   unsupported_callsz1Total number of unsupported CUDA function calls: r   , z+
Total number of replaced kernel launches: kernel_launches)r   lenr   )r   r   s     r   r/   r/      s    \\GZA[\\\ 

XcBS>T>T
X
X
XYYY 
$))%
&
&''' 

ZUCT=U9V9V
Z
Z
Z[[[[[r   c                    d}d}|                      dd                               dd          } d t          d          D             }d||         d<   t          |           D ]q\  }}|dk    r ne|d	k    r|dz  }n|d
k    r|dz  }|dk    s|t          |           dz
  k    r1|dk    r+||dk    z   ||         d<   |dz  }|dk     r|dz   ||         d<   r| |d         d         |d         d         dz            }| |d         d         |d         d                  }| |d         d         |d         d                                       dd                              d          }	| |d         d         |d         d                                       dd                              d          }
d|	 d
}d|
 d
}|                     |	|          }|                     |
|          }|                     ||z   ||z             }|S )zBadds dim3() to the second and third arguments in the kernel launchr   <<<r   >>>c                     g | ]}i S r   r   )r   _s     r   
<listcomp>zadd_dim3.<locals>.<listcomp>   s    %;%;%;Qb%;%;%;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   r0   r0      sL   EG!))%44<<UBGGM%;%;%((%;%;%;H HUOGM** 3 3Q199E88qLGG#XXqLGHHs=11A5557a<<%(AH%5HUOE"QJEqyy+.7(!(1+g"6x{57IA7M"MNM"8A;w#7E8J#JKN#HQK$8!U9K$KLTTUY[]^^ddehiiO$Xa[%9(1+e:L%LMUUVZ\^__eefijj/_///N1.111O&..OO(001A?SS%%mn&DFX[nFnooKr   z([ ]+)(detail?)::[ ]+\\\n[ ]+c           
          t                               d              fd}d }d }t           | |                               } }|D ]} ||          }                     d|d                   }	 |d         d         |	d	z            }
 |d         |d                  }|d	         d         d
k    rdnd	} |d         d         ||         d         d	z            }t	          ||
          }t          t          d|d                             dd                              dd                              }d|dd
                             ddd|z
  z  dz                                 dd                              dd                              |d|z   dz             z   }|                    |
|          }|d                             |           |S )zK Replace the CUDA style Kernel launches with the HIP style kernel launches.c                 \    |                      d           |                      d           dS )Nr   r   z::groupinps    r   <lambda>z'processKernelLaunches.<locals>.<lambda>  s(    1.Osyy||.O.O.O r   c                    | d         | d         dddddddd}ddi}d}d}d	}d
}|}t          |d         d         dz
  dd          D ]}
|         }	|||fv r^|	dk    r#||k    r|}||d         d<   |dxx         dz  cc<   |	dk    r/|dxx         dz  cc<   |d         dk    r||k    r||d         d<   |}||k    r
|                                         s

|         dv r>||k    r|}||d         d<   |dk    r$d|d         d<   |d         |d         |d         gc S ||k    r$||d         d<   |d         |d         |d         gc S d S )Nr   r   r   r   ry   )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_templatez7processKernelLaunches.<locals>.grab_method_and_template  s    (1'9)EBRSS%'33"$R00
 
 q	  s?+G4q8"bAA %	a %	aA!9D %---3;;!,12J.$KKK1$KKK3;;$KKK1$KKKT{a''V{-B-B34J0!/ $$!9$$&& a&)7P*P*P//!/45M*51 Avv67M*73 "%]!3s:#oJ^````	  //67M*73 "%]!3s:#oJ^````K%	a %	ar   c                 @   d}g }|                      d|          dk    r|                      d|          }|                      d|          dz   }|dk    rt          d          |                    ||| ||         d           |                      d|          dk    |S )zKFinds the starting and ending points for all kernel launches in the string.r   r   ry   r   r   zno kernel end found)r   r   r   )findr(   rt   )r   
kernel_endkernel_positionskernel_starts       r   find_kernel_boundsz1processKernelLaunches.<locals>.find_kernel_boundsS  s    
 kk%,,22!;;uj99L  UL99A=JQ !6777 ##l:.4\:5M.N%P %P Q Q Q kk%,,22  r   c                 0   d}d}d}| D ]}|dk    r3|dk    r	|dk    rd}nb|dk    r	|dk    rd}nS|dk    r|dk    r|dk    rd}n>|dk    r|d	k    s|d
k    rd}n)|dk    r|dk    r|dk    rd}n|dk    r|dk    r|dk    rd}|}|dk    r||z  }|dz  }|S )Nr   r   z//*z/*"\'r   xr   )r   
in_commentprev_c
new_stringr   s        r   mask_commentsz,processKernelLaunches.<locals>.mask_commentsk  s   

 	" 	"AR88#!%JJ#XX&C--!%JJ#XX&D..Vs]]!$Jt##99T		!#Jt##88#!#Js""88$!#JFRa

c!

r   r   r   r   r   r   ry   r   r   r   r   zhipLaunchKernelGGL(z, 0   r   r   )	RE_KERNEL_LAUNCHsublistr  r0   r   rC   r   rt   )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   r1   r1     sA    !!"O"OQWXXF9a 9a 9a 9a 9av     0  B   2 2==3H3H I IJJM ' 4 4))&11 kk#ve}55 VAYw/a?@vgve}<=%ay/255!!1$*6!9W+=f_>UV[>\_`>`+`$a!#M;??'6'?+B+B5#+N+N+V+VW\^a+b+bccdd*-=ad-C-K-K5AK(50.2 .229'%2F2Fww4H H !:CB[<[^a<abbc

 &--k:FF 	 ''
3333r   c                    d}d}|}d\  }}|t          |           k     r`| |         |d         k    r|du rd}d}|}n+|dz  }n%| |         |d         k    r|r|dz  }|dk    r|}||fS |dz  }|t          |           k     `dS )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   )ry   ry   Tr   )NN)r   )input_stringr   r   inside_parenthesisparensr   p_startp_ends           r   r2   r2     s     F
CNGU
L!!
!
!a((!U**%)"!#%(**/A*aKF{{~%q L!!
!
!  :r   c                 *    t          | |ddg          S )z%Finds the first balanced parentheses.{}r   r2   r!  r   s     r   r3   r3         lE#sDDDDr   c                 *    t          | |ddg          S )z!Finds the first balanced bracket.r   r   r   r)  r*  s     r   r4   r4     r+  r   z\bassert[ ]*\(c                 l    | }t           D ])}|                    | dt           |          d          }*|S )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!  r  funcs      r   r5   r5     sM     !M# \ \%--$kkk>QRV>W;Z;Z;Z[[r   z:?:?\b(__syncthreads)\b(\w*\()c                     | ddg}t          fd|D                       rS dv }|dv z  }|dv z  }|t                                        duz  }|rd| z   S )	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   P   K   | ] }t          j        d | d| d          V  !dS )z#include ("z"|<z>)N)research)r   extr  s     r   r   z#hip_header_magic.<locals>.<genexpr>  sC      
W
W293C33C333]CC
W
W
W
W
W
Wr   hipLaunchKernelGGL
__global__
__shared__Nz#include "hip/hip_runtime.h"
)r   RE_SYNCTHREADSr2  )r!  headershasDeviceLogicr  s      @r   r6   r6     s     !M #$;<G

W
W
W
Ww
W
W
WWW  *]:Nlm33Nlm33Nn++M::$FFN  H8<Gr   zGextern\s+([\w\(\)]+)?\s*__shared__\s+([\w:<>\s]+)\s+(\w+)\s*\[\s*\]\s*;c                 B    | }t                               d |          }|S )a  Match extern __shared__ type foo[]; syntax and use HIP_DYNAMIC_SHARED() MACRO instead.
       https://github.com/ROCm/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                     d|                      d          pd d|                      d           d|                      d           dS )	NzHIP_DYNAMIC_SHARED(r   r   r   r   r   r   r   r   r   s    r   r   z'replace_extern_shared.<locals>.<lambda>  sF    ^#))A,,*<"^^syy||^^syyYZ||^^^ r   )RE_EXTERN_SHAREDr  )r!  r  s     r   r7   r7     s1     !M$((^^`mo oM r   c                    t           j                            |           rJ |st          |           s| S t           j                            |           \  }}t           j                            |          \  }}|dk    rd}|}|}|                    dd          }|                    dd          }|                    dd          }|                    dd          }|                    dd          }|d	k    r|                    dd          }|s&||k    r t           j                            |d          }|r||k    r||z   |k    r|d
z   }t           j                            |||z             S )z3
    Returns the new name of the hipified file
    .cu.hipcudahipCUDAHIPTHCTHHzcaffe2/core_hip)rf   rg   isabsr9   rp   splitextr   r   )r   r   dirpathrR   rootr3  orig_filenameorig_dirpaths           r   r8   r8   !  sn    w}}\***** (E(E l33GX  **ID#H e||MLoofe,,Goofe,,GooeU++G<<&&D<<&&D-||E5)) /G|$;$;',,w.. < 7 7TCZM<Y<Yf}7<<,,,r   c                     t           j                            |           rJ |                     d          rdS |                     d          rdS |                     d          rdS dS )Ntorch/Fthird_party/nvfuser/tools/autograd/templates/Trf   rg   rG  
startswithr   s    r   r9   r9   i  st    w}}\*****x(( u566 u:;; u4r   c                 .   t           j                            |           rJ |                     d          r|                     d          rdS dS |                     d          rdS |                     d          rdS |                     d          rdS dS )Nzaten/zaten/src/ATen/core/FTrN  rO  rP  rQ  rS  s    r   r:   r:   u  s    w}}\*****w'' ""#899 	5tx(( t566 t:;; t5r   c                 P    t          |           rd|                                 v S dS )NsparseFr:   lowerrS  s    r   r;   r;     s.    |$$ 0<--////5r   c                     t          |           rHd|                                 v rdS d|                                 v rd|                                 v rdS dS dS )NrV  TlinalgbatchlinearalgebralibblasFrW  rS  s    r   r<   r<     sk    |$$ |))++++4++----*l.@.@.B.BBBu45r   c                    t           j                            |           rJ |                     d          rdS t           j                            |           }t           j                            |          \  }}d|v s|dv od|vS )Nzc10/cudaTgpur>  .cuhcudnn)rf   rg   rG  rR  basenamerH  )r   rR   r   r3  s       r   r=   r=     s    w}}\*****z** tw--HWh''FAsX7!7VgX>UVr   c                       e Zd ZdZd ZdS )TrieNodezA Trie node whose children are represented as a directory of char: TrieNode.
       A special char '' represents end of word
    c                     i | _         d S rH   )childrenr"   s    r   r    zTrieNode.__init__  s    r   N)r   r   r   r   r    r   r   r   rc  rc    s-             r   rc  c                   \    e Zd ZdZd Zd Zd Zd Zd Ze	j
        d             Zd Zd	 Zd
S )r>   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                     t                      | _        t          j        d          | _        | j                                        | _        dS )z,Initialize the trie with an empty root node.F)usedforsecurityN)rc  rJ  hashlibmd5_hashdigest_digestr"   s    r   r    zTrie.__init__  s:    JJ	[777
z((**r   c                 .   | j                             |                                           | j                                         | _        | j        }|D ]6}|j                            |t                                 |j        |         }7d|j        d<   dS )zAdd a word to the Trie. Tr   N)	rk  updateencoderl  rm  rJ  re  
setdefaultrc  r   wordnoder   s       r   ri   zTrie.add  s    
$++--(((z((**y 	' 	'DM$$T8::666=&DD br   c                     | j         S )zReturn the root node of Trie. )rJ  r"   s    r   dumpz	Trie.dump  s
    yr   c                 *    t          j        |          S )z Escape a char for regex. )r1  escape)r   r   s     r   quotez
Trie.quote  s    yr   c                 ^    | j         }|D ]}||j        v r|j        |         } dS d|j        v S )zZSearch whether word is present in the Trie.
        Returns True if yes, else return FalseFr   )rJ  re  rr  s       r   r2  zTrie.search  sK     y 	 	Dt}$$}T*uu T]""r   c                    |}d|j         v r,t          |j                                                   dk    rdS g }g }d}t          |j                                                   D ]}t	          |j         |         t
                    r	 |                     |j         |         | j                  }|                    | 	                    |          |z              u# t          $ r+ |                    | 	                    |                     Y w xY wd}t          |          dk     }	t          |          dk    r]t          |          dk    r|                    |d                    n.|                    dd                    |          z   dz              t          |          dk    r	|d         }
ndd                    |          z   d	z   }
|r|	r|
d
z  }
nd|
 d}
|
S )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)?)re  r   keyssorted
isinstancerc  _patternrm  rt   ry  	Exceptionr   )r   rJ  rl  rt  altccqr   recursecconlyr   s              r   r  zTrie._pattern  s    3t}'9'9';';#<#<#A#A44=--//00 	 	D$--x88 0"mmDM$,?NNGJJtzz$//'9::::  0 0 0IIdjj../////0 XX\!r77Q;;2ww!||

2a5!!!!

3,s2333s88q==VFFSXXc]]*S0F 	* *#)v)))s   AC2DDc                 B    |                      | j        | j                  S z#Export the Trie to a regex pattern.r  rJ  rm  r"   s    r   r   zTrie.pattern      }}TY555r   c                 B    |                      | j        | j                  S r  r  r"   s    r   export_to_regexzTrie.export_to_regex  r  r   N)r   r   r   r   r    ri   rv  ry  r2  	functools	lru_cacher  r   r  r   r   r   r>   r>     s        S S+ + +	! 	! 	!    # # # ) ) )V6 6 66 6 6 6 6r   r>   PYTORCH_MAPr   z(?<=\W)(z)(?=\W)z#include "([^"]+)"z#include <([^>]+)>z"#define THC_GENERIC_FILE "([^"]+)"z\.cu\bc	                 n    t           j                            t           j                             |                    t	          |          }t
                   }	|vr!d|	_        d|	_        t          j	        |	_
        |	S t	          t           j                            |                     }
t          d          5 }|                                t          k    r-d|	_        d|	_        t          j	        |	_
        |	cddd           S |                    d           |                                }ddd           n# 1 swxY w Y   |}t           j                            t           j                             t#          |
                              }t           j                            t           j                            |                    s2                    t           j                            |                     d fd}rt*                              |          }ntt/          |
          rt*                              ||          }nIt1          |
          rt*                              |          }nd	 }t2                              ||          }d# f	d	}t4                               |dd
          |          }t6                               |dd          |          }t8                               |d          |          }|                    d          rG|                    dd          }|                    dd          }t>                              d|          }stA          |          }|                    d          rd|vrtC          |          }tE          |          }rg||k    rat           j                                      t           j                            |          k    r!|	_        d|	_        t          j	        |	_
        |	S |k    rtG          d          r
t          |z   }d
}t           j                            |          rAt          |d          5 }|                                |k    }ddd           n# 1 swxY w Y   |r	                     |dd          5 }|$                    |           ddd           n# 1 swxY w Y   ||	_        d|	_        t          j	        |	_
        |	S # tJ          $ rm}tM          tN          j(         d| d|j)         d dtN          j*         	tV          j,                    |	_        d!|	_        t          j	        |	_
        |	cY d}~S d}~ww xY w||	_        d"|	_        t          j	        |	_
        |	S )$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                 B    t           |                     d                   S Nr   )r  r   ms    r   pt_replzpreprocessor.<locals>.pt_repl[  s    1771::&&r   c                 r    t                               |                     d           |                     S r  )PYTORCH_SPECIAL_MAPgetr   )r  r  s    r   pt_special_replz%preprocessor.<locals>.pt_special_repl^  s+    "&&qwwqzz771::>>>r   c                 B    t           |                     d                   S r  )
CAFFE2_MAPr   r  s    r   c2_replzpreprocessor.<locals>.c2_replk  s    !!''!**--r   Tc                 *   	  	
 fd}|S )Nc                    |                      d          }t          j                            |          |                    d          s*|                    d          rK|                    d          s6                    t          |                      d                              S rt          fdD                       rxd }d }rt          j                                      }t          j        	                    t          j        
                    ||                    }t          j                            |          r|}|}|D ]}t          j        
                    |          }t          j        	                    t          j        
                    ||                    }t          j                            |          r|}|}||                      d          S |t          vrt          |	  	         n|t          v rt          |         }|j        t          j        k    rt          j                            |          }t          j        	                    t          j        
                    t          |                              }	|	|_        |t          |<                       t          j                            |	|	n||                    S t          |         j        }
                    t'          t          j                            |
|
n||                              S |                      d          S )Nr   )z	ATen/cudazATen/native/cudazATen/native/nested/cudazATen/native/quantized/cudazATen/native/sparse/cudazATen/native/transformers/cudazTHC/rD  THCPc              3   B   K   | ]}|                               V  d S rH   r   )r   srR   s     r   r   z>preprocessor.<locals>.mk_repl.<locals>.repl.<locals>.<genexpr>  s/      ,U,UaQZZ-A-A,U,U,U,U,U,Ur   r   )r   rf   rg   ra  rR  formatr8   r   dirnamerj   r   rh   r$   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_filepathrR   r   r   r   r   r   include_current_dirr   r   r   r   templs              @r   replz+preprocessor.<locals>.mk_repl.<locals>.replq  sO   

Aw''**H & ' '
Y e$$
Y ./\\&-A-A
Y ||$5aggajjBV$W$WXXX$ (f,U,U,U,U9,U,U,U)U)U (f!
"&& ?*,'//(*C*C'+-7??27<<H[]^;_;_+`+`(w~~&:;; ?%8
*>"*.A C C*.0gll;KM_.`.`+/1wrw||L_ab?c?c/d/d,7>>*>?? C)<J.BO"*771::%"*===34D4C4=?RTY[k4H)Ubd d d d %(;;;$7$HM$2l6NNN*,'///K[*\*\+-7??27<<HXHYZik  IA  IA<B <B ,C ,C(6F3?L+O<$||BGOOP`Pl<L<LAPR\-^ -^  _  _ _+>+O+](||M"'//^v  _CBZBZGVXb3d 3d %e %e f f f 771::r   r   )r  r  r  r   r   r   r   r   r   r   r   r   s   `` r   mk_replzpreprocessor.<locals>.mk_replp  sX    9	 9	 9	 9	 9	 9	 9	 9	 9	 9	 9	 9	 9	 9	 9	t r   z#include "{0}"z#include <{0}>Fz#define THC_GENERIC_FILE "{0}"zCMakeLists.txtrB  rC  rD  rE  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)-rf   rg   rj   r   r   r$   r   r   r   r   r   r   rQ   readlineHIPIFY_C_BREADCRUMBseekreadr8   rh   r  rq   RE_PYTORCH_PREPROCESSORr  r<   r:   RE_CAFFE2_PREPROCESSORRE_QUOTE_HEADERRE_ANGLE_HEADERRE_THC_GENERIC_FILEr   r   RE_CU_SUFFIXr1   r5   r6   r,   writeOSErrorr   r*   rX   strerrorrZ   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   r?   r?   4  s    wrw||,<hGGHHHX&&H'1My  &*#>&2&7# ;K!L!LMML	h	)	)	) #S<<>>000*.M'#HM *6*;M' # # # # # # # # 	

# # # # # # # # # # # # # # # ' -=?PQ]_s?t?t u uvvI7>>"'//)4455 727??955666' ' '? ? ? ? ?
  
O/33G]KK<(( 	O377WWMM\** 	O377OOMM. . .266wNNM; ; ; ; ; ; ; ; ; ; ; ; ; ;x $''0@$(G(GWWM#''0@%(H(H-XXM'++GG4T,U,UWdeeM )** @%--fe<<%--eU;;$((??  D-mUCC 	/** >80K0K.}== %]33M 	-//GOOH%%)C)CCC&.#6&2&7# 9!1(<n!o!o+m;H	w~~i   8)g... 	8(}}-7H	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	!	3AA *T

=)))* * * * * * * * * * * * * * **3M'#)M *6*;M'   	! 	! 	!W_  B  BY  B  Bqz  B  B^f  B  Bszs  B  Bz# # # #*2M'#>M *6*;M'       	! '0#<&2&7#sm   >E)EEER((R,/R,6T S0$T 0S44T 7S48#T 
V&A"VVVc                    t          | d          5 }|                                }|r0t          j        dt          j        |           dfd|          }n|                    |          }|                    d           |                    |           |                                 d d d            d S # 1 swxY w Y   d S )Nr+z\b(z)\bc                     S rH   r   )r  replace_strings    r   r   z+file_specific_replacement.<locals>.<lambda>  s    n r   r   )	r)   r  r1  r  rx  r   r  r  truncate)r   search_stringr  strictr   contentss     `   r   r@   r@     s   	x		 !6688 	GvBRY}%=%=BBBD\D\D\D\^fggHH''~FFH	q					

                 s   BB::B>B>c                 J   t          | d          5 }|                                }|d         dk    r|d         dk    rd| d}d| d|z   }|                    d           |                    |           |                                 d d d            d S # 1 swxY w Y   d S )	Nr  r   r   ry   r   r	  z	#include z 
)r)   r  r  r  r  )r   headerr   r  s       r   rA   rA     s    	x		 !6688!9r
c 1 1"]]]F++++x7	q					

                 s   A:BBBc                 2    |                      dd          } | S )z<Static global kernels in HIP results in a compilation error.z __global__ staticr5  r   )in_txts    r   rB   rB     s    ^^0,??FMr   z#include .*\nc                 |   g }ddd}| }|dz   }|t          |          k     r||         dk    r|dxx         dz  cc<   nq||         dk    r|dxx         dz  cc<   nT||         dk    r|dxx         dz  cc<   n7||         dk    r+||dz
           dk    r|d         dk    r|dxx         dz  cc<   |d         dk    r%|d         dk    r|                    ||d	           nZ|d         dk    r5|d         dk    r)||         d
k    r|                    ||d	           |dz   }|dz  }|t          |          k     |S )ad   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   rt   )r   r   	argumentsclosurescurrent_positionargument_start_poss         r   rC   rC     s    I H )A- S[[
(
("#s**SMMMQMMMM$%,,SMMMQMMMM$%,,SMMMQMMMM$%,,8H18L1MQT1T1TYabeYfijYjYjSMMMQMMM C=A(3-1"4"4'9BRSSTTT C=A(3-1"4"4@P9QUX9X9X'9BRSSTTT!1A!5A+ S[[
(
(. r   c                     |                                  dv rdS |                                  dv rdS t          j        d          )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   rw   0FzBoolean value expected.)rX  argparseArgumentTypeError)vs    r   rD   rD   ;  sH     	wwyy222t	
4	4	4u()BCCCr   r>  r_  r  r  r  r  z.inr  r_  r  r  r  Tproject_directoryshow_detailedheader_extensionsextra_fileshipify_extra_files_onlyc                    	  dk    rt          j                     t           j                                       s#t	          d           t          j        d           s                     d            dz    k    r fdD              fd	D             	t           j                                      st          j	                    t          t          t                              t          t          t          	                    	t          t          	|||                    }t          |          }|D ]Z}t           j                            |          s t           j                            |          }||vr|                    |           [d	d
lm} |D ]}t           j                            |          r ||          }n) |t           j                            |                    }|                    	fd|                    d          D                        |t-          d          }g g d}|s|n|D ]}t/          ||||||||
	  	         t	          t0          j        dz   t0          j        z   t
          j                   |rt9          |           t:          S )Nr   z,The project folder specified does not exist.r   r   _amdc                 <    g | ]}|                               S r   r  )r   includer   r  s     r   r   zhipify.<locals>.<listcomp>e  s)    aaaWGOO$57GHHaaar   c                 <    g | ]}|                               S r   r  )r   rO   r   r  s     r   r   zhipify.<locals>.<listcomp>f  s)    ]]]66>>"35EFF]]]r   )r   r   r   r   r   r   )Pathc              3      K   | ]v}|                                 t          t          |                    3t          t          |                    Pt          |j                  et          |          V  wd S rH   )is_filer   strr,   name)r   rg   r  r   r   s     r   r   zhipify.<locals>.<genexpr>  s       
 

TH--
 c$ii11
 !,=>>	
II
 
 
 
 
 
r   r  T)r_   )r   r   z-Successfully preprocessed all matching files.r  )rf   getcwdrg   rh   r   r  exitrstripshutilcopytreer  mapr   r-   r`   rG  r   rt   pathlibr   extendrglobr+   r.   r*   rW   rZ   r  r/   r$   )r  r  r   r  r   r   r   r  r   r   r   r   r   r  r   r   all_files_setr   r   r  header_include_dir_pathr   r   s   `  `` `  `             r   rE   rE   F  sE   " BIKK 7>>+,, <===  6  %%%,v5,,,aaaaaX`aaa]]]]]U\]]] 7>>*++ =)+;<<<Cx0011H3}g..//G'(8807J:K=QS S S T TI 	NNM    w}}Q 	2-q11AM!!Q 1 

 

7==+,, 	_&*d+=&>&>##&*d27<<8HJ\+]+]&^&^# 
 
 
 
 
 
"9"?"?"D"D
 
 
 	
 	
 	
 	
 (DAAA	 46"MME&=NYY; q q'(8(IOb(-/?AUW`bo	q 	q 	q 	q 
'/K
Kgl
Zadakllll  er   )r   r   r   FFr   )Fr  r  r   r   r  r   Fr   TFFFN)cr   r  r   r1  r  r  rf   r   r   cuda_to_hip_mappingsr   r   typingr   collections.abcr   r	   r
   enumr   r  ri  r   r   dictr  HipifyFinalResultr  r$   __annotations__PYTORCH_TEMPLATE_MAP__all__r  r(   r)   r*   r+   r   boolr,   r   r-   r  r.   r/   r0   compiler  r1   r2   r3   r4   	RE_ASSERTr5   r7  r6   r<  r7   r8   r9   r:   r;   r<   r=   rc  r>   CAFFE2_TRIEr  PYTORCH_TRIEr  objectr  mappingr  itemssrcr}   dst	meta_data
API_CAFFE2ri   API_SPECIALr  API_PYTORCHr  r  r  r  r  r  r  r?   r@   rA   rB   
RE_INCLUDErC   rD   rE   r   r   r   <module>r(     s    0   				  



 				       6 6 6 6 6 6 5 5 5 5 5 5       $ $ $ $ $ $ - - - - - - - -               4   D D D D D D D D l*+ T )+ & + + +!+*== s s s. . . . . . . .1 1 1
       &       B% % % % % %9s 9 9T 9 9 9 9
K K K  !"'%*) ))) ) 	)
  ) #) 08}) ) ) )X+++ + &	+
 CI+ + #+ (+ + !%+ + + +2
\ 
\ 
\     F 2:>?? W W Wt# # #LE E E
E E E
 BJ())	    =>>  : 2:hii   E- E- E- E-P        W W W       \6 \6 \6 \6 \6 \6 \6 \6| dff
tvv!#T#v+ # # #  # " "G:gw'''''mmoo " "
UAh!""I	y00S!!! $	11kooc26N6N1+.#C((#&C  	11i6KS\6\6\OOC   !JsO" $K$?$?$A$ABB $"*%X1M1M1O1O%X%X%XYY "*233"*233 bj!FGG rz)$$www w &	w
 CIw w #w (w w !-w w w wr	 	 	 	     RZ())
+ + +\D D D  T"8$&#"!&$)04R RRR R  	R
 R "R R R R R R R R "R ,-R  !R R R R R Rr   