
    `i                         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Zd dlmZm	Z	m
Z
mZmZmZmZm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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" 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Z)e*ej+        ej,        fZ-dej.        k    rd dlm/Z/ ee/d                  Z0ne1Z0 ej2        dg d          Z3 G d de4          Z5d Z6d Z7 G d d          Z8d Z9d Z:dIde
e1         d e1d!e
e1         fd"Z;d#e#j<        d!e*fd$Z= G d% d&          Z>d' Z?d( Z@d)e jA        d*eee&e%f                  d+e>d!ee&e%f         fd,ZBd-ejC        d*eee&e%f                  d.eejD                 d+e>d!e%f
d/ZEd0e
e jF                 d1e*d+e>d!efd2ZGe6d3e jF        d1e*d+e>d!efd4            ZHe6d5e jI        d+e>d!e#j<        fd6            ZJd5ee jI                 d+e>d!e#j<        fd7ZKd8ee&e%f         d9e%d+e>d!efd:ZL	 dJd;e jI        d+e>d<e%d1e*d=eMd!efd>ZNd?e#j<        d@e#j<        d+e>d!ee%e&f         fdAZO edBe&e%ee&e%f                   ZPdCePdDe!jQ        dEe0d+e>d!ePf
dFZRdCee&e%f         dGee&e%f         d+e>d!e%fdHZSdS )K    N)AnyDictListOptionalSequenceTupleTypeVarUnion)ComplexWarning)runtime)	CodeBlock	_CodeType)_kernel)_raise_if_invalid_cast)jit)_cuda_types)_cuda_typerules)_internal_typesDataConstant)_builtin_funcs)
_interfaceF)      )Literal)noequivsafe	same_kindunsafeResult)	func_namecodereturn_typeenable_cooperative_groupsbackendoptionsjitifyc                       e Zd Zd Zd ZdS )_JitCompileErrorc                 d    t          |          | _        t          |          | _        || _        d S N)type
error_typestrmesnode)selfer3   s      f/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/jit/_compile.py__init__z_JitCompileError.__init__7   s'    q''q66			    c                    | j         j        t          | j         d          d                    fdt	          |                    d                    D                       }|                     | j        dz   |z             )N
end_lineno
c                 t    g | ]4\  }}|d z   cxk    rk    rn nd| nd|                                  5S )   z>   )rstrip).0ilineendstarts      r6   
<listcomp>z,_JitCompileError.reraise.<locals>.<listcomp>?   sn     : : :4 "QU1111c11111[$[[[{D{{JJLL: : :r8   z

)r3   linenogetattrjoin	enumeratesplitr0   r2   )r4   pycoderC   rD   s     @@r6   reraisez_JitCompileError.reraise<   s    	 diu55 : : : : :$V\\$%7%788: : : ; ; oodh/&8999r8   N)__name__
__module____qualname__r7   rL    r8   r6   r,   r,   5   s2          
: : : : :r8   r,   c                 ,     dt           j        f fd}|S )Nr3   c                 v    	  | g|R i |S # t           $ r  t          $ r}t          ||           d }~ww xY wr.   )r,   	Exception)r3   argskwargsr5   funcs       r6   new_funcz,transpile_function_wrapper.<locals>.new_funcF   sl    	,4.t...v... 	 	 	 	, 	, 	,"1d+++	,s    838)astAST)rV   rW   s   ` r6   transpile_function_wrapperrZ   E   s1    ,sw , , , , , , Or8   c                   	
 t          |           st          d          	 t          j        |           }n# t          $ r d}Y nw xY w|dk    rt          d|  d          | j        dk    rt          j        |           \  }}t          |d                   t          |d         	                                          z
  
d
                    
fd	|D                       }t          j        |          }t          |t          j                  sJ t          |j                  d
k    sJ |j        d         |fS |t          d|  d          d
                    t!          j        |                    }t          j        |           \  }t          |          z   	d
                    |          }t          j        |          }	fdt          j        |          D             }t          |          d
k    rt          d|  d          |d         }t          j        d|j        t          j        |j                  gg dd          |fS )a/  Returns the tuple of ``ast.FunctionDef`` object and the source string
    for the given callable ``func``.

    ``func`` can be a ``def`` function or a ``lambda`` expression.

    The source is returned only for informational purposes (i.e., rendering
    an exception message in case of an error).
    z!`func` must be a callable object.Nz<stdin>z/JIT needs access to the Python source code for zf but it cannot be retrieved within the Python interactive interpreter. Consider using IPython instead.z<lambda>r    c                 D    g | ]}|                     d z  dd          S ) r\   r=   )replace)r@   rB   
num_indents     r6   rE   z*_parse_function_object.<locals>.<listcomp>p   sA     E E E6:DLLz)2q11E E Er8   r=   z+JIT needs access to Python source code for zX but could not be located.
(hint: it is likely you passed a built-in function or method)c                 r    g | ]3}t          |t          j                  r|j        cxk    rk     .n n|4S rP   )
isinstancerX   LambdarF   )r@   r3   end_line
start_lines     r6   rE   z*_parse_function_object.<locals>.<listcomp>   sZ     7 7 7d4,,7t{5555X55555 555r8   z4Multiple callables are found near the definition of z4, and JIT could not identify the source code for it._lambda_kernel)namerT   bodydecorator_listreturnstype_comment)callable
ValueErrorinspectgetsourcefile	TypeErrorRuntimeErrorrM   getsourcelineslenlstriprH   rX   parserb   Modulerh   	linecachegetlineswalkFunctionDefrT   Return)rV   filenamelines_sourcetreefull_sourcenodesr3   rd   r`   re   s            @@@r6   _parse_function_objectr   Q   s    D>> ><===
 (..    9<d < < <= = 	=
 }
"")$//qq]]Sq):):%;%;;
 E E E E>CE E E F Fy  $
+++++49~~""""y|V##L$ L L LM M 	M ''),X6677K /55FJCKK'HWWV__F9[!!D7 7 7 7 7chtnn 7 7 7E 5zzA~~ =+/= = = > > 	> 8D?DIj##$4d   	 s   8 AAc                   &    e Zd ZddZdeddfdZdS )	GeneratedreturnNc                     g | _         i | _        d| _        d| _        d| _        d| _        d| _        t          j        rdnd| _	        d| _
        d S )NF)z-DCUPY_JIT_MODEz--std=c++17z-DCUB_DISABLE_BF16_SUPPORTnvccnvrtc)codesdevice_function	enable_cg
include_cginclude_cg_memcpy_asyncinclude_cuda_barrierr)   r   is_hipr(   r*   )r4   s    r6   r7   zGenerated.__init__   s`     "
 68 	 ',$$)!7
 ")<vvW r8   r%   c                     || j         vrK| j                             |           t          | j                   t          j        k    rt          d          d S d S )Nz(Number of functions exceeds upper limit.)r   appendrs   r   _n_functions_upperlimitrm   )r4   r%   s     r6   add_codezGenerated.add_code   sX    tz!!Jd###4:!<<< !KLLL "!<<r8   )r   N)rM   rN   rO   r7   r1   r   rP   r8   r6   r   r      sR           6MS MT M M M M M Mr8   r   c           	      R   t                      }t          |          }t          | |||||          \  }}|j        | |f         \  }}	d                    |j                  }
|j        }|j        }|j        }|j	        }t          rt          |
           t          ||
|||||          S )as  Transpiles the target function.

    Args:
        func (function): Target function.
        attributes (list of str): Attributes of the generated CUDA function.
        mode ('numpy' or 'cuda'): The rule for typecast.
        in_types (list of _cuda_types.TypeBase): Types of the arguments.
        ret_type (_cuda_types.TypeBase or None): Type of the return value.
    r;   )r$   r%   r&   r*   r'   r(   r)   )r   tuple_transpile_func_objr   rH   r   r(   r)   r*   r   _is_debug_modeprintr#   )rV   
attributesmodein_typesret_type	generatedrg   r&   r$   r~   r%   r(   r)   r*   r   s                  r6   	transpiler      s     IXH+j$(I? ?D+,dH-=>LIq99Y_%%DGGF#I d$K"+WgO O O Or8   c                 L   | |f|j         v r"|j         | |f         }|t          d          |S t          |           \  }}t          j        |           }	t          di |	j        |	j        |	j        }
d	                    |          }|j
        }t          |j                   dk    r'|dt          t          |j                             z   z  }d |j         | |f<   t          |||||
||||	  	        \  }}||j        f|j         | |f<   |                    |           ||j        fS )Nz$Recursive function is not supported.r^   r   r~   )r   rP   )r   rm   r   rn   getclosurevarsdictglobals	nonlocalsbuiltinsrH   rg   rs   r1   _transpile_functionr   r   )rV   r   r   r   r   r   resultr   r   cvarsconstsrg   	cuda_codeenvs                 r6   r   r      sP   h9444*D(+;<>CDDD *$//LD&"4((EGGEMGU_GGGF*%%J9D
9$%%))c#i788999926ItX./(dJf(If6 6 6NIs 482FItX./y!!!r8   r>   r}   spacesr   c                      fd| D             S )Nc                     g | ]}|z   S rP   rP   )r@   rB   r   s     r6   rE   z_indent.<locals>.<listcomp>   s    ,,,dFTM,,,r8   rP   )r}   r   s    `r6   _indentr      s    ,,,,e,,,,r8   valuesc                  j    t          d | D                       sJ t          d | D                       S )Nc              3   J   K   | ]}t          |t          j                  V  d S r.   )rb   r   Exprr@   xs     r6   	<genexpr>zis_constants.<locals>.<genexpr>   s/      CCqz!_122CCCCCCr8   c              3   @   K   | ]}t          |t                    V  d S r.   rb   r   r   s     r6   r   zis_constants.<locals>.<genexpr>   s,      771z!X&&777777r8   )all)r   s    r6   is_constantsr      sB    CCFCCCCCCCC77777777r8   c            
           e Zd ZdZdedeeef         deeef         dej	        de
f
dZded	eeeef                  fd
Z	 ddeded	efdZdS )Environmenta:  Environment of the scope

    Attributes:
        mode ('numpy' or 'cuda'): The rule for typecast.
        consts (dict): The dictionary with keys as the variable names and
            the values as the data that is determined at compile-time.
        params (dict): The dictionary of function arguments with keys as
            the variable names and the values as the Data.
        locals (dict): The dictionary with keys as the variable names and the
            values as the Data stored at the local scope of the function.
        ret_type (_cuda_types.TypeBase):
            The type of return value of the function.
            If it is initialized to be ``None``, the return type must be
            inferred until the end of transpilation of the function.
        generated (Generated): Generated CUDA functions.
    r   r   paramsr   r   c                 v    || _         || _        || _        i | _        i | _        || _        || _        d| _        d S )Nr   )r   r   r   localsdeclsr   r   count)r4   r   r   r   r   r   s         r6   r7   zEnvironment.__init__  s@     	')&(
 "


r8   keyr   c                     || j         v r| j         |         S || j        v r| j        |         S || j        v r| j        |         S d S r.   )r   r   r   )r4   r   s     r6   __getitem__zEnvironment.__getitem__$  sU    $+;s##$+;s##$+;s##tr8   r\   prefixsuffixc                 |    | xj         dz  c_         | | j          | }| |         |S |                     ||          S )Nr=   )r   get_fresh_variable_name)r4   r   r   rg   s       r6   r   z#Environment.get_fresh_variable_name-  sM    

a

.$*.f..:K//???r8   N)r\   r\   )rM   rN   rO   __doc__r1   r   r   r   r   TypeBaser   r7   r   r
   r   r   rP   r8   r6   r   r     s         " h' dO	
 "* !   "s xhn0E'F     35@ @@,/@9<@ @ @ @ @ @r8   r   c          
          	 t          | |||||||          S # t          $ r(}	|	}
t          r|
                    |           Y d}	~	nd}	~	ww xY w|
                    |           J )a  Transpile the function
    Args:
        func (ast.FunctionDef): Target function.
        name (str): Function name.
        attributes (str): The attributes of target function.
        mode ('numpy' or 'cuda'): The rule for typecast.
        consts (dict): The dictionary with keys as variable names and
            values as concrete data object.
        in_types (list of _cuda_types.TypeBase): The types of arguments.
        ret_type (_cuda_types.TypeBase): The type of return value.

    Returns:
        code (str): The generated CUDA code.
        env (Environment): More details of analysis result of the function,
            which includes preambles, estimated return type and more.
    N)_transpile_function_internalr,   r   rL   )rV   rg   r   r   r   r   r   r   r   r5   excs              r6   r   r   7  s    & +$
D&h	+ + 	+        	 KK  KK5s    
A
AA
c           	         t          d |                                D                       }t          | t          j                  s/t          d                    t          |                               t          | j	                  dk    rgt          j        dk    rW| j	        D ]O}t          j        |          t          fddD                       st          j        d dt                      P| j        }	|	j        t          d	          t          |	j                  dk    rt          d
          |	j        t          d          t          |	j                  dk    rt          d          d |	j        D             }
t          |
          t          |          k    r2t-          | dt          |
           dt          |           d          t          d t/          |
|          D                       }t1          |||||          }t3          | j        d|          }d                    d t/          |
|          D                       }d |j                                        D             }|j        t<          j        |_        | d|j         d| d| d}tA          |||z             }tC          |          |fS )Nc                 6    g | ]\  }}|t          |          fS rP   r   r@   kvs      r6   rE   z0_transpile_function_internal.<locals>.<listcomp>Z  s'    @@@1Ax{{#@@@r8   zNot supported: {}r   )r   	   c              3       K   | ]}|v V  	d S r.   rP   )r@   word	deco_codes     r6   r   z/_transpile_function_internal.<locals>.<genexpr>d  sD       C C#  9, C C C C C Cr8   )	rawkernel	vectorizez
Decorator z may not supported in JIT.z#`*args` is not supported currently.z4keyword only arguments are not supported currently .z&`**kwargs` is not supported currently.z+Default values are not supported currently.c                     g | ]	}|j         
S rP   )arg)r@   r   s     r6   rE   z0_transpile_function_internal.<locals>.<listcomp>u  s    ...CG...r8   z	() takes z positional arguments but z were given.c                 8    g | ]\  }}|t          ||          fS rP   r   r@   r   ts      r6   rE   z0_transpile_function_internal.<locals>.<listcomp>z  s(    CCCtq!AtAqzz?CCCr8   T, c                 @    g | ]\  }}|                     |d           S r.   )declvarr   s      r6   rE   z0_transpile_function_internal.<locals>.<listcomp>}  s*    MMMA!))At,,MMMr8   c                 P    g | ]#\  }}|j                             |d           dz   $S )N;)ctyper   )r@   nr   s      r6   rE   z0_transpile_function_internal.<locals>.<listcomp>~  s1    OOOTQ!'//!T**S0OOOr8   r^   ())"r   itemsrb   rX   rz   NotImplementedErrorformatr/   rs   ri   sysversion_infounparseanywarningswarnRuntimeWarningrT   vararg
kwonlyargskwargdefaultsrp   zipr   _transpile_stmtsrh   rH   r   r   r   voidr   r1   )rV   rg   r   r   r   r   r   r   deco	argumentsrT   r   r   rh   params_s
local_varsheadr%   r   s                     @r6   r   r   X  s
   @@@@@AAFdCO,, J!"5"<"<T$ZZ"H"HIII
4!##v%%+ ( (K--	 C C C C'AC C C C C (MJYJJJ&( ( ( 	I#!"GHHH
9  1$$!BD D 	D"!"JKKK
9""!9; ; 	; /.y~...D
4yyCMM!! / /c$ii / /x==/ / /0 0 	0 CCs4/B/BCCCDDF
dFFHi
@
@CDItS11DyyMMT89L9LMMMNNHOOSY__=N=NOOOJ
|"'<<3<<<$<<<<<DT:,--Dt99c>r8   oprT   r   c                    t          | r=t          j        t          |                     }t	           |d |D                        S t          | t          j                  r|\  }}t          j	        ||          }t          j	        ||          }t          |j        d          r'|j                            |||          }|t          ur|S t          |j        d          r'|j                            |||          }|t          ur|S t          | t          j                  r|\  }}t          j	        ||          }t          j	        ||          }t          |j        d          r'|j                            |||          }|t          ur|S t          |j        d          r'|j                            |||          }|t          ur|S t          j        |j        t          |                     }t)          ||d |          S )Nc                     g | ]	}|j         
S rP   objr   s     r6   rE   z!_eval_operand.<locals>.<listcomp>  s     5 5 51 5 5 5r8   _add_radd_sub_rsub)r   r   
get_pyfuncr/   r   rb   rX   Addr   inithasattrr   r  NotImplementedr  Subr  r  	get_ufuncr   _call_ufunc)r   rT   r   pyfuncr   youtufuncs           r6   _eval_operandr    s   
 T 8 +DHH55 5 5 5 5 56777"cg 1IaIa17F## 	',,sAq))C.((
17G$$ 	'--Q**C.((
"cg 1IaIa17F## 	',,sAq))C.((
17G$$ 	'--Q**C.((
%chR99EudD#...r8   r  dtypec                    t          |          | j        k    rt          d          g }|D ]}t          |t                    r%t          j        |j        |j                  j	        }nDt          |j
        t          j                  r|j
        j	        }nt          d|j
         d          |                    |           t          j        | t!          |          ||j                  }|t          d| j         d|           |j        |                                 | j        dk    r|j                            d          rt          j        |j        d                   }|j                            dd	          }	g }
t1          ||j                  D ]S\  }}t5          |t          j        |          d
|          }t7          j        ||          }|
                    |           Td}t;          | j                  D ]^}t          t=          t?          j         d!                    |          |j                                      dk    rd}d| d|j        v rd}_|j"        #                    | j$                   |r]tK          |
          D ]#\  }}|	                    d| |j&                  }	$d|	                    dtO          |                    z   dz   }	nd(                    d t;          | j                  D                       }| j         dtO          tS          j	        |j        d                              }d(                    d t;          | j                  D                       }d| d| d| d| d| d|	 d}|j"        #                    |           d(                    d |
D                       }| d| d}	t7          |	|          S tU          d| j         d          ) Nzinvalid number of argumentszcupy.ufunc: z is unsupported"z(" does not support for the input types: r=   zout0 = r   r\   r!   Tzin{}Fin_typer   	out0_typer   r   c                     g | ]}d | d	S )ztypename inr  rP   r@   rA   s     r6   rE   z_call_ufunc.<locals>.<listcomp>  s7     ,B ,B ,B+,&a&&&,B ,B ,Br8   r~   c                     g | ]
}d | d| S )r  z_type inrP   r  s     r6   rE   z_call_ufunc.<locals>.<listcomp>  s)    NNN 3Q 3 3 3 3NNNr8   z
template <z>
__device__ r^   z) {
    typedef z out0_type;
    return z;
}
c                     g | ]	}|j         
S rP   r%   )r@   as     r6   rE   z_call_ufunc.<locals>.<listcomp>  s    'B'B'B1'B'B'Br8   zufunc `z` is not supported.)+rs   ninrm   rb   r   r   get_ctype_from_scalarr   r  r  r   r   Scalarrp   r   guess_routiner   rg   
error_funcnoutroutine
startswith	out_typesr_   r   r   _astype_scalarr   r  rangelistrefinditerr   r   r   	_preamblerI   r%   r1   rH   numpyr   )r  rT   r  r   r   r   r   r   out_typeexpr	in_paramscan_use_inline_expansionrA   template_typenames
ufunc_namer   
ufunc_codein_params_codes                     r6   r  r    s7    4yyEI6777H  a"" 	E5chFFLAA!344 	EAAC17CCCDDD		&ueHoouch	O	OB	zN
NNHNNP P 	P 
} 
zQ2:00;;%bl1o66z!!)R00	bk** 	  	 DAqq+"4Q"7"7cJJA	!S!!AQ#' uy!! 	1 	1A4GNN1$5$5rzBBCCDDqHH+0(A}}}
**+0(u///# 	5!),, 6 61||HHHaf55k3x==AAACGDD!% ,B ,B05ei0@0@,B ,B ,B "C "C!JLLU[a-I-I)J)JLLJYYNNU59=M=MNNNOOF(:   ! $*     J M"":...!YY'B'B	'B'B'BCCN 44>444DD(###
G
GGG
H
HHr8   stmtsis_toplevelc                 \    g }| D ]&}|                     t          |||                     '|S r.   )extend_transpile_stmt)r7  r8  r   
codeblocksstmts        r6   r   r     sC    
 J C C/$SAABBBBr8   r=  c                    t          | t          j                  rt          d          t          | t          j        t          j        f          rt          d          t          | t          j                  rt          | j        |          }t          |t                    r
|j
        dgS t          j        ||          }|j        }|j        ||_        n%|j        |k    rt          d|j         d|           d|j         dgS t          | t          j                  rt          d	          t          | t          j                  rt'          | j                  d
k    rt          d          t          | j        |          }| j        d         }t+          |          rt          |t          j                  rl|j        }t          |j
        t0                    sK|r:t          ||         t                    rt3          d| d          ||j        |<   g S t3          d          t          j        ||          }t7          ||||          S t          | t          j                  rt          | j        |          }t          | j        |          }t          |t                    st3          d|j                   t          j        ||          }t          |                    d          |j                  }t?          | j         ||f|          }	t          |t                    sJ t          |	t                    sJ t          |j        tB          j"                  sJ t          |	j        tB          j"                  sJ tG          |	j        j$        |j        j$        d           d|j        %                    d|j        z   |          z   dz   |j        &                    ||	          z   dz   gS t          | t          j'                  rt'          | j(                  dk    rt          d          t          | j        t          j                  sJ | j        j        }t          | j)        |          }
||         }|4t          | j        j        |
j                  }||j*        |<   ||j+        |<   nVt          |t                    rt3          d          |j        |
j        k    r"t3          d| d|j         d|
j                   t          |
tX          j-                  st          d          t]          | j/        d|          }|
j         d|
j0        j         d|
j1        j         d |
j2        j         }d!}|
j3        d"u rd#}n|
j3        du rd$}d%| d| d&}ti          || d'g|z             g}|
j5        }|d"u rd(g|z   }n|
d)| d*g|z   }|S t          | t          j6                  rt          d+          t          | t          j7                  rt'          | j(                  dk    rt          d          t          | j8        |          }ts          |tB          j:        d,|          }t          j        ||          }t]          | j/        d|          }d-|j         d*}ti          ||          gS t          | t          j;                  rt          | j8        |          }t+          |          r&|j
        r| j/        n| j(        }t]          |||          S d.|j         d*}t]          | j/        d|          }t]          | j(        d|          }ti          ||          ti          d/|          gS t          | t          j<        t          j=        f          rt          d0          t          | t          j>        t          j?        f          rt          d1          t          | t          j@                  r9t          | j8        |          }t+          |          r|j
        sJ dgS d2|z   d3z   gS t          | t          jA        t          jB        f          rt          d4          t          | t          jC        t          jD        f          rt          d5          t          | t          jE                  r2t          | j        |          }t+          |          rdgn
|j        dz   gS t          | t          jF                  rdgS t          | t          jG                  rt          d          t          | t          jH                  rt          d          J )6zaTranspile the statement.

    Returns (list of [CodeBlock or str]): The generated CUDA code.
    z!class is not supported currently.z-Nested functions are not supported currently.Nzreturn;z!Failed to infer the return type: z or zreturn r   z!`del` is not supported currently.r=   Not implemented.r   zType mismatch of variable: ``z.Cannot assign constant value not at top-level.zCannot augassign to _tmp_r!   z{ &z; z; }zwhile-else is not supported.z'loop counter must not be constant value!Data type mismatch of variable: ``:  != z.for-loop is supported only for range iterator.Fz __it = z, __stop = z, __step = z+__step >= 0 ? __it < __stop : __it > __stopTz__it < __stopz__it > __stopzfor (z; __it += __step)z = __it;z#pragma unrollz#pragma unroll(r   z`async for` is not allowed.r"   zwhile (zif (elsez#Switching contexts are not allowed.zthrow/catch are not allowed.zassert(z);z0Cannot import modules from the target functions.z3Cannot use global/nonlocal in the target functions.)Irb   rX   ClassDefr   rz   AsyncFunctionDefr{   _transpile_exprvaluer   r  r   r  r   r   rm   r%   DeleteAssignrs   targetsr   Nameid_typeclassesrp   r   _transpile_assign_stmt	AugAssigntargetr   r  r   r   r!  r   r  r   assignFororelseiterr   r   r   Ranger   rh   rD   stopstepstep_is_positiver   unrollAsyncForWhiletestr(  bool_IfWith	AsyncWithRaiseTryAssertImport
ImportFromGlobalNonlocalr   PassBreakContinue)r=  r8  r   rJ  r   varrg   rS  tmpr   itersloop_varrh   	init_codecondr   r%   r\  	conditionr7  	then_body	else_bodys                         r6   r;  r;    se	    $%% G!"EFFF$#*>?@@ =!;= = 	=$
## )
C00eX&& 	59+<;	%%%K<CLL\QICLIIaIIK K K'%*'''(($
## G!"EFFF$
## Dt|!!%&8999
C00l1o 
	J:c38#<#< 
	J6Dei66 J J!#d)T22 P'(Nt(N(N(NOOO',CJt$I#HJ J J 	%%%%c3{CCC$&& :
C00 c22&$'' 	B@6;@@AAA	%%%3..w77FFtwec::&$'''''&$'''''&,(:;;;;;&,(:;;;;;L 2K	A 	A 	A v|++C#(NFCCCdJ##C001389 : 	: $   +t{a%&DEEE$+sx00000{~	3//t9$+.%+66F%CJt$CIdOO(++ 	6EFFF^u{**5D 5 5>5 5',{5 56 6 6 %!677 	B%@B B B  	5#66 3 3${/3 3!&3 3 "'3 3	 =!T))"DD#u,,"D;y;;D;;;$Tt,=,=,=+>+EFFGT>>$%,DD/f///047D$%% 86777$	"" 't{a%&DEEE#DIs33	"9k.?3OO	Ii--		5#66****$%%&&$ J#DIs33		"" 	=!*?DIIDKE#E;<<<'in'''$TYs;;	$T[%==	$	**Ifi,H,HII$3=122 @>???$CG,-- 97888$
## .	3// 	.995L%,--$S^455 MKLLL$S\233 PNOOO$!! D
C00$U++Cuu%*s2B1CC$!! u$	"" 6!"4555$%% 6!"45555r8   r0  c                     t          | |          }t          |t                    r&t          |j        t          j                  r|j        S |S )z`Transpile the statement.

    Returns (Data): The CUDA code and its type of the expression.
    )_transpile_expr_internalrb   r   r  r   r   )r0  r   ress      r6   rI  rI    sG     #4
-
-C#x   Z9M%N%N w
r8   c           	         t          | t          j                  rBfd| j        D             }|d         }|dd          D ]}t	          | j        ||f          }|S t          | t          j                  rBt          | j                  }t          | j	                  }t	          | j        ||f          S t          | t          j
                  r,t          | j                  }t	          | j        |f          S t          | t          j                  rt          d          t          | t          j                  r\| j        g| j        z   }t!          |          dk    rt          d          fd|D             }t	          | j        d         |          S t          | t          j                  r2t          | j                  }t          | j                  }t          | j                  }	t          | t,                    r| j        r|n|	S |j        j        j        dk    rt7          d	          t9          ||	          t9          |	|          }	}|j        j        |	j        j        k    r)t7          d
|j        j         d|	j        j                   t;          |t<          j        d          }tA          d|j!         d|j!         d|	j!         d|j                  S t          | t          j"                  rrt          | j#                  }
fd| j$        D             }i }| j%        D ](}|j&        J t          |j'                  ||j&        <   )tP          j)        }tU          |
          r|
j        |v r||
j                 }
t          |
tV          j,                  r |
j-        g|R i |S t          |
t,                    st7          d|
 d          |
j        }
t          |
t\          j        j/                  r |
j0        g|R i |S t          |
t\          j1                  r|
j2        st7          d|
j3        j4         d          fd|D             }tk          d |D                       }tm          |
j3        dgj7        |d j8                  \  }}d9                    d |D                       }tA          | d| d|          S t          |
tt          j;                  r|<                    dt-          d                     j        }t!          |          dk    r6t{          t}          |                    }t7          d| d|
j?                   t          |
||          S tU          g ||                                R  rLd |D             }t          d |B                                D                       }t-           |
|i |          S t          jD        |
          rgt          |
t                    rRt!          |          dk    rt7          d|
 d           t=          jG        |
          }t;          |d         |d          S t7          d!|
j4         d"          | t-          d           S t          | t          j                  rt-          | j'                  S t          | t          jH                  r;t          | j'                  }t          | jI                  }t          ||          S t          | t          jK                  r(| jL                 }|t          d#| jL                   |S t          | t          jN                  rt          | j'                  }t          |t,                    r't-          t          |j        | jP                            S t          |t@                    rqt          |j        | jP                  rWt          |j        | jP        d           }t          |t          jS                  r ||          S t          |t@                    r|S t          d$| jP                   t          | t          jU                  rfd%| jV        D             }t          d& |D                       r&t-          tk          d' |D                                 S fd(|D             }d9                    d) |D                       }t!          |          dk    r0tA          d*| dt=          jU        d+ |D                                 S tA          d,| dt=          jU        d- |D                                 S t          | t          jX                  rt          | j'                  S t          d.Z                    t          |                               )/Nc                 0    g | ]}t          |          S rP   rI  r@   r5   r   s     r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>  s#    ???a/!S))???r8   r   r=   r?     z2Comparison of 3 or more values is not implemented.c                 0    g | ]}t          |          S rP   r|  r}  s     r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>  s#    :::a/!S)):::r8   cz/Complex type value cannot be boolean condition.z*Type mismatch in conditional expression.: rE  r"   r   z ? z : r   c                 0    g | ]}t          |          S rP   r|  r@   r   r   s     r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>  #    ;;;A3'';;;r8   'z' is not callable.zCalling __global__ function z) from __global__ function is not allowed.c                 :    g | ]}t          j        |          S rP   r   r  r  s     r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>  s%    444!DIa%%444r8   c                     g | ]	}|j         
S rP   r   r   s     r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>  s    444!ag444r8   
__device__r   c                     g | ]	}|j         
S rP   r  r   s     r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>  s    "8"8"8a16"8"8"8r8   r  z!' is an invalid keyword to ufunc c                     g | ]	}|j         
S rP   r   r   s     r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>  s    (((aAE(((r8   c                 &    g | ]\  }}||j         fS rP   r   r   s      r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>  s"    AAA$!QAqu:AAAr8   zfunction takes z invalid number of argumentzInvalid function call 'z'.zUnbound name: zUnknown attribute: c                 0    g | ]}t          |          S rP   r|  r  s     r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>8  r  r8   c                 8    g | ]}t          |t                    S rP   r   r   s     r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>:  s"    666A
1h''666r8   c                     g | ]	}|j         
S rP   r   r   s     r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>;  s    "7"7"7Q15"7"7"7r8   c                 :    g | ]}t          j        |          S rP   r  r  s     r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>=  s%    000a	!S!!000r8   c                     g | ]	}|j         
S rP   r  r   s     r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>>  s    444!qv444r8   zSTD::make_pair(c                     g | ]	}|j         
S rP   r  r   s     r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>C      "9"9"9q17"9"9"9r8   zSTD::make_tuple(c                     g | ]	}|j         
S rP   r  r   s     r6   rE   z,_transpile_expr_internal.<locals>.<listcomp>G  r  r8   zNot supported: type {})\rb   rX   BoolOpr   r  r   BinOprI  leftrightUnaryOpoperandrc   r   Comparecomparatorsrs   opsIfExpr_  rh   rV  r   r  r   r  kindrp   _infer_typer(  r   r`  r   r%   CallrV   rT   keywordsr   rJ  r   builtin_functions_dictr   r   BuiltinFunccallr   r   _instantiate_JitRawKernel_device_funcrM   r   r   r   r   rH   r   r  popnextrW  rg   r  r   r   rn   isclass
issubclassrP  r!  	Subscriptslice	_indexingrN  rO  	NameError	AttributerG   attrr	  types
MethodTypeAttributeErrorr   eltsr   Indexrm   r   r/   )r0  r   r   rJ  rhsr  r  rs  r   r  rV   rT   rU   kwbuiltin_funcsr   fnamer&   r1  r  rg   r   arrayindexr  r  	elts_codes    `                         r6   rx  rx    s	    $
## ????4;???q	!"": 	> 	>C!$'E3<==EE$	"" :ty#..
C00TWtUmS999$$$ 5c22TWuh444$
## 6!"4555$$$ 7)t//v;;!%DF F F::::6:::TXa[&#666$	"" Ety#..DIs++DK--dH%% 	('11a': C''MNNN1a%%{1a'='=17=AGM))67=6 6&'gm6 67 7 7 dK$5xEE:	::af:::::AGDDD$!! ;Ety#..;;;;;;;35- 	< 	<B6%%%,RXs;;F26NN&= 	+48}#<#< *DdO788 	349S242226222$)) 	:8888999xdJ2;<< 	;$4$S:4:::6:::dJ455 	>< @?4:3F ? ? ?@ @ @ 5444t444D44t44455H!4
\NCH$"/ "/E; 		"8"84"8"8"899I5//9///===dGM** 	7JJw77;E6{{QDLL))JJJtyJJL L LtT5#66600000 	3((4(((DAA&,,..AAABBFDD$1&11222?4   	AZl%C%C 	A4yyA~~GdGGGI I I&t,,E!$q'5(C@@@C$-CCCDDD|~~$%% $
###$&& ,
C00
C00s+++$!! DG=6TW66777$&& 
@
C00eX&& 	;GEIty99:::eT"" 	wu{DI'F'F 	5;	488D$ 011 #tE{{"$%% >49>>???$	"" <;;;;;;;6666677 	:E"7"7$"7"7"78899900004000II44t44455	t99>>.)...!"9"9D"9"9"9::< < < /9///!"9"9D"9"9"9::< < < $	"" 0tz3///
-44T$ZZ@@
A
AAr8   lvaluervaluec                    t          | t                    rt          d          t          | j        t          j                  r7t          |j        t          j                  rt          || j        d|          }n7| j        |j        k    r't          d| j         d| j         d|j                   | j                            | |          dz   gS )Nz/lvalue of assignment must not be constant valuer!   rC  rD  rE  r   )	rb   r   rp   r   r   r!  r(  r%   rT  )r  r  r   s      r6   _emit_assign_stmtr  P  s    
 &(## KIJJJ6<!344 16<);<<1k3GG		%	%0 0 0|0 0!'0 01 1 	1 L//#566r8   rS  rJ  depthc           	         t          | t          j                  rr| j        }||         }|Pt	          ||j                  }||j        |<   |r%|dk    r|j                            ||          dz   gS ||j        |<   t          |||          S t          | t          j
                  r4t	          j        t          | |          |          }t          |||          S t          | t          j                  rNt          |j        t          j                  st          |j         d          t!          | j                  }t!          |j        j                  |k    rt          d| d          t!          |j        j                  |k     rt          d| d          |j                            d| |          dz   g}t'          |          D ]a}	d|	 d	| d}
|j        j        |	         }t)          | j        |	         |t	          |
|          ||d
z             }|                    |           bt-          d|          gS J )Nr   r   z cannot be unpackz$too many values to unpack (expected r   z&not enough values to unpack (expected _temp	STD::get<z>(_tempr=   r\   )rb   rX   rN  rO  r   r   r   r   r   r  r  r  rI  r   r   rm   rs   r  r  r)  rQ  r:  r   )rS  r   rJ  r8  r  rg   r  sizer   rA   r%   r   r=  s                r6   rQ  rQ  c  sO    &#(## 	5yT>$,,F%CJt @uzz++D%883>??$CIdO 444&#-(( 5?6377== 444&#)$$ &%+{'899 	@>>>???6;u{ !!D((KDKKKLLLu{ !!D((MdMMMNNN$$_U__e<<sBCt 	 	A1q11111DK%a(E)AT$%6%6UQYP PDLL"e$$%%5r8   r  r  c                    t          | t                    rXt          |t                    rt          | j        |j                           S t          t	          | j                   d          t          j        | |          } t          | j        t          j	                  rYt          |t                    r5|j        }| j        j
        |         }t          d| d| j         d|          S t          d          t          | j        t          j                  rt          j        ||          }| j        j        }t          |j        t          j                  r|j        j        }|dk    rt          d          |dk    rbt          j        | j        j        | j        j        dz
  | j        j        | j        j                  }t          | j         d	|j         d|          S |j        d
vrt          d          t          | j         d|j         d| j        j                  S t          |j        t          j	                  r|t+          |j        j
                  k     rt-          d|           |j        j
        D ]H}t          |t          j                  st          d          |j        j        dvrt          d          I|t+          |j        j
                  k    rt          j        | j        j        | j        j        t+          |j        j
                  z
  | j        j        | j        j                  }|j         dt+          |j        j
                   d}t          | j         d	| d|          S |dk    r"t          | j         d| j        j                  S |dk    r*t          | j         d|j         d| j        j                  S t          | j         d|j         d| j        j                  S t          |j        t          j                  rt          d          J t          | j         d          )Nz) is not subscriptable with non-constants.r  z>(r   z.Tuple is not subscriptable with non-constants.r   z1Scalar indexing is not supported for 0-dim array.r=   z
._slicing(uizArray indices must be integers.[]z+The number of indices is beyond array dim: zArray indices must be scalar.iuzArray indices must be integer.z, Dim<z>()z[0]z[STD::get<0>(z)]z._indexing(z#Advanced indexing is not supported.Fz is not subscriptable.)rb   r   r  rp   r/   r   r  r   r   r   r  r%   	ArrayBase_ndimr!  r  CArray_c_contiguous_index_32_bitsr  
child_typers   
IndexError)	r  r  r   rA   r   ndimindex_dtype
new_carrayr   s	            r6   r  r    sd   
 %"" KeX&& 	2EIei0111EIIIIK K 	K IeS!!E%+{011 JeX&& 	;	A!!$A6A66666:::HIII%+{455 2	%%%{ ek;#566 	G++KqyyGI I Iaxx(/K%K%)K-K.	0 0

 z::UZ:::JH H Ht++ ABBB:--
---u{/EG G Gek;#455 	(c%++,,,, H$HHJ J J[& F F!![%788 E#$CDDD7<t++#$DEEE ,c%++,,,,(/K%K%EK,=(>(>>K-K.	0 0

 "JIIc%+2C.D.DIIIuz>>V>>>
KKKqyyz&&&(>@ @ @qyyz>>
>>>K*, , , :77%*777&( ( ( ek;#566 	CABBBu
uz999
:
::r8   _Tr   r   castingc           	         t          | t                    rCt          | t                    rJ t          |j                            | j                            S t          | j        t          j                  st          | j
         d          | j        j        }|j        }||k    r| S t          j        |                    d          ||          st          d| d| d| d          |j        dk    rL|j        dk    rA|j        dk    rt          j        d	t                      t          d
| d| j
         d|          S t          d
| d| j
         d|          S )Nz is not scalar type.r   zCannot cast from 'z' to z with casting rule .r  bz:Casting complex values to real discards the imaginary partr   z)(z.real())r   )rb   r   r   r  r/   r  r   r   r!  rp   r%   r.  can_castr  r   r   r   )r   r   r  r   from_tto_ts         r6   r(  r(    s    !X 1a&&&&&((//000 ag{122 916777888W]F;D~~ >&++a..$88 -, , ,d , ,!(, , ,- - 	- {cdi3..9ML      1111115999&E&&QV&&&...r8   hintc                 X   t          | t                    rt          | j        t          j                  rt          j        | |          S t          j        ||          }t          |j        t          j	                  sJ t          | |j        d|          }t          j        ||          S )Nr!   )rb   r   r  r.  genericr   r  r   r   r!  r(  )r   r  r   cast_xs       r6   r  r    s    
 a"" !j&F&F !yC   9T3Ddj+"455555Atz;<<F9VS!!!r8   )r>   )r   )TrX   collectionsrn   rw   numbersr+  r   typingr   r   r   r   r   r   r	   r
   r   r  r.  cupy.exceptionsr   cupy_backends.cuda.apir   cupy._core._codeblockr   r   
cupy._corer   cupy._core._dtyper   cupyxr   	cupyx.jitr   r   r   cupyx.jit._internal_typesr   r   r   r   r   boolr`  NumberrP  r   r   _CastingTyper1   
namedtupler#   rS   r,   rZ   r   r   r   r   r   r   r   r   r   r   rY   r  r  r  r  r=  r   r;  r0  rI  rx  r  intrQ  r  r  r!  r(  r  rP   r8   r6   <module>r     s   



           				 



 M M M M M M M M M M M M M M M M M M M M    * * * * * * * * * * * * 6 6 6 6 6 6 6 6       4 4 4 4 4 4       ! ! ! ! ! ! % % % % % % % % % % % % * * * * * * . . . . . . $ $ $ $ $ $             ek7>2	S<=?LL L		  

 

: : : : :y : : : 	 	 	H H HV!M !M !M !M !M !M !M !MHO O O:  6- -49 -c -T#Y - - - -8/. 84 8 8 8 8
3@ 3@ 3@ 3@ 3@ 3@ 3@ 3@l  B- - -`$/G$/uXt^,-$/ $/ 8T>	$/ $/ $/ $/NDI}DIuXt^,-DI $DI 	DI
 
DI DI DI DINCH~  	    bhbb b 	b b b bJ 
#( 
 
9M 
 
 
 
XBsx XBXB XB XB XB XBv7hn%77 7 	7 7 7 70 ( ((( ( 	(
 ( ( ( ( (VI;#I;#I; I; 4>	I; I; I; I;X WT8T54#899//!/ / 	/
 / / / /D
"4 
"HdN#
" 
" 
	
" 
" 
" 
" 
" 
"r8   