o
    Z۷i՛                     @   s   d dl Z d dlZd dlmZ d dlmZmZm	Z	mZm
Z
mZmZ d dlmZmZ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Zd dlZd d	lmZ G d
d deZedd Z dd Z!G dd deZ"dddZ#dd Z$ee#dd Z%dS )    N)ir)typestypingutilsr   configir_utilsregistry)CallableTemplate	signatureinfer_globalAbstractTemplate)lower_builtin)register_jitableNumbaValueError)literal_unroll)numpy_supportc                   @   s    e Zd ZdZdd Zdd ZdS )StencilFuncLowererzMCallable class responsible for lowering calls to a specific StencilFunc.
    c                 C   s
   || _ d S N)stencilFunc)selfsf r   L/home/ubuntu/vllm_env/lib/python3.10/site-packages/numba/stencils/stencil.py__init__   s   
zStencilFuncLowerer.__init__c                 C   s:   | j |ji |jd }|||j||}||jg |S r   )r   compile_for_argtysargsreturn_typecall_internalfndescadd_linking_libslibrary)r   contextbuildersigr   cresresr   r   r   __call__   s   zStencilFuncLowerer.__call__N)__name__
__module____qualname____doc__r   r'   r   r   r   r   r      s    r   c                 G   s\   | j }t|D ]$}| j|jkrtd|j }tt|D ]}|| || kr*tdqqd S )Nz\Secondary stencil array does not have same number  of dimensions as the first stencil input.zaSecondary stencil array has some dimension smaller the same dimension in the first stencil input.)shaper   ndim
ValueErrorrangelen)ar   ashapeargargshapeir   r   r   !raise_if_incompatible_array_sizes$   s   
r6   c                 C   s   t | j| | j| S )z_ Called by stencil in Python mode to add the loop index to a
        user-specified slice.
    )slicestartstop)	the_sliceaddendr   r   r   slice_addition;   s   r<   c                   @   sd   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )StencilFuncz@
    A special type to hold stencil information for the IR.
    r   c                 C   sx   t | j| _t |  jd7  _|| _|| _|| _g | _tjj	| _
tjj| _| | j
 | jd| _i | _t| | _d S )N   neighborhood)type
id_counterid	kernel_irmodeoptionskwsr   
cpu_targettyping_context
_typingctxtarget_context
_targetctx_install_typegetr?   _type_cacher   	_lower_me)r   rC   rD   rE   r   r   r   r   H   s   

zStencilFunc.__init__c              
   C   s"  g }|  D ]\}}|j}|j}g }	|jD ]u}
t|
tjr|| t|dkrEt	|||}t	||d |}|	t
|||
j| qg }|D ]}t	|||}||g7 }qI|d|}tj||}|	t||| t	|||}t
|||
j|}|	| q|	|
 q|	|_q|S )z
        Find return statements in the IR and replace them with a SetItem
        call of the value "returned" by the kernel into the result array.
        Returns the block labels that contained return statements.
        r>   r   stencil_index)itemsscopelocbody
isinstancer   Returnappendr0   VarSetItemvalueredefineExprbuild_tupleAssign)r   blocks
index_varsout_name
ret_blockslabelblockrR   rS   new_bodystmtrvarivarvar_index_varsone_var	index_vars_index_var
tuple_callsir   r   r   replace_return_with_setitemX   s2   

z'StencilFunc.replace_return_with_setitemc           *      C   s  i }g }	t jdkrtd|| t|j |du rd}
nd}
t||kr.tdt||f t|j}t	 }|j
 D ]}|j}|j}g }|jD ]o}t|tjrqt|jtjrqt jdkritd|jj|jj |jj||jj< t|tjrt|jtjr|jjdv r|jjj|jv st|tjr|jj|jv rtd	t|tjrt|jtjr|jjd
v r|jjj|jv r|jjj|vr|jjdkr|jj}n|jj}||jjj |
rt|dsJ |j|v r|	||j g7 }	n|j|v r|	||j g7 }	ntd|dkrt||d |}|d|}||j }t|tj j!r|d|}t"#t$}tj%&|}|||j< t'd||}|(t||| tj)|||gd|}|*| j+|tj,gi ||< |(t||| |(ttj-|jj|||j| qJtj.t/j|||}|(t||| |(ttj-|jj|||j| qJg }g }|d|}g }g } ||j }t0|D ]}!|d|}|(tt|!||| ||g7 }t|||! |}||g7 }|d|}| |g7 } |d|}"tj-|||! |}#|(t|#|"| t|tj1r||! }$n|dd }$t|$tj j!rx|d|}t"#t$}tj%&|}|||j< t'd||}|(t||| tj)||"||! gd|}|*| j+|$tj,gi ||< |(t||| qtj.t/j|"||! |}|(t||| qtj2| |}%|(t|%|| |(ttj-|jj|||j| qJ|(| qJ||_q<|
radd t0|D }t|	dkrtd|	D ]}&t|&t3st|&t4r/t0t|&D ]=}'|&|' }(t|(tjr|(j|v r||(j }(t|(t5r&t6||' d |(||' d< t7||' d |(||' d< qtdt|&})n't|&t5rRt6|d d |&|d d< t7|d d |&|d d< d})ntd|)|kr_tdq||fS )z
        Transforms the stencil kernel as specified by the user into one
        that includes each dimension's index variable as part of the getitem
        calls.  So, in effect array[-1] becomes array[index0-1].
        r>   add_indices_to_kernelNTFD%d dimensional neighborhood specified for %d dimensional input arrayzremembering in const_dict)setitemstatic_setitemz?Assignments to arrays passed to stencil kernels is not allowed.)getitemstatic_getitemrt   namezDstencil kernel index is not constant, 'neighborhood' option requiredr   rP   r<   r   const_indexind_stencil_indexc                 S   s   g | ]}d d gqS )r   r   ).0_r   r   r   
<listcomp>*      z5StencilFunc.add_indices_to_kernel.<locals>.<listcomp>z=Stencil kernel with no accesses to relatively indexed arrays.zCstencil kernel index is not constant,'neighborhood' option requiredz/Non-tuple or non-integer used as stencil index.z2Stencil index does not match array dimensionality.)8r   DEBUG_ARRAY_OPTprintr   dump_blocksr_   r0   r   get_tuple_tablesetvaluesrR   rS   rT   rU   r   r^   rZ   Consttargetrv   r\   op	arg_namesrY   indexrk   addhasattrrX   r[   r   misc	SliceTypenumbanjitr<   	functions
DispatcherGlobalrW   callget_call_typerI   intprt   binopoperatorr/   
ConstSizedr]   tuplelistintminmax)*r   kernelindex_namesr-   r?   standard_indexedtypemap	calltypes
const_dictkernel_constsneed_to_calc_kerneltuple_tablerelatively_indexedrd   rR   rS   re   rf   stmt_index_varrk   tmpvarstmt_index_var_typsa_varsa_funcsa_func_typg_saslice_addition_callacc_callr`   sum_resultsrl   const_index_varsind_stencilsdim
getitemvargetitemcallone_index_typrm   r   r5   te	index_lenr   r   r   rp      s6  




















z!StencilFunc.add_indices_to_kernelc                 C   s   t jdkrtd| t| jj t|d tj	j
stdddlm} || j| j| j|d i \}}}}t|tj	j
r@tdtj	
||d j|d j}|||fS )Nr>   get_return_typer   zGThe first argument to a stencil kernel must be the primary input array.)typed_passesz:Stencil kernel must return a scalar and not a numpy array.)r   r}   r~   r   r   rC   r_   rU   r   npytypesArrayr   
numba.corer   type_inference_stagerI   rK   r-   layout)r   argtysr   r   r   r   rz   real_retr   r   r   r   K  s,   


zStencilFunc.get_return_typec                 C   s2   t dt| j tft| | jd}|| | dS )zmConstructs and installs a typing class for a StencilFunc object in
        the input typing context.
        StencilFuncTyping_)keygenericN)r@   strrB   r   dict_type_meinsert_user_function)r   	typingctx_ty_clsr   r   r   rL   d  s   zStencilFunc._install_typec           
      C   s0   | j | \}}}}| j|||||g|R  }	|	S r   )rN   _stencil_wrapper)
r   r   kwtysr   sigretrz   resultr   r   new_funcr   r   r   r   n  s   
zStencilFunc.compile_for_argtysc                 C   s4  | j durt| j |d jkrtdt| j |d jf |}d}d}d|v r6||d f7 }|d7 }|d }d|v rE||d f7 }|d7 }|| jv rU| j| \}}}}|S | |\}}	}
t|g|R  }d	d
| j	j
|}i }t|| |d }|jt|d}| j| j| |fg |||	|
f| j|< |S )z
        Implement AbstractTemplate.generic() for the typing class
        built by StencilFunc._install_type().
        Return the call-site signature.
        Nr   rq    outz
, out=Noner?   z, neighborhood=Nonez*def __numba_dummy_stencil({}{}):
    pass
,__numba_dummy_stencil)pysig)r?   r0   r-   r   rN   r   r
   formatjoinrC   r   execreplacer   pysignaturerK   insert_func_defnrO   )r   r   r   argtys_extra	sig_extrar   _sigrz   r   r   r   r$   
dummy_textdct
dummy_funcr   r   r   r   u  s<   


zStencilFunc._type_mec           
      C   s   i }|  }i |_|j D ]2\}}t |j| }g |_|j| jD ]}t |}	|j|	 ||v r:|| ||	< q#||j|< q||fS )a  
        Create a copy of a given IR along with its calltype information.
        We need a copy of the calltypes because copy propagation applied
        to the copied IR will change the calltypes and make subsequent
        uses of the original IR invalid.
        )copyr_   rQ   deepcopyrT   rW   )
r   r   r   copy_calltypeskernel_copyblock_labelrd   	new_blockrf   scopyr   r   r   copy_ir_with_calltypes  s   
z"StencilFunc.copy_ir_with_calltypesc           D   
      s  |  | j|\}}t|j |jd }	t|j|\}
}t|j}t|j|
||| d|v r5t	dt
d|}tjdkrFtd|| |d }tjdkratd||jt|j| t|j dtt|d	d
| jf }g }t|jD ]}t
dt| |}||g7 }qwt
d|}t
d|}d}|d ur|d|7 }dt| jv r|d|7 }| jdg }|	|v rt	dtt|t|j dkrt	d| |||j| j|||\}}| jd u r|| _tjdkrtd t|j |  |j||}tjdkrtd| t|j d|d!|j|}g }t|jD ]-}t"|| d t#r:|| d }|| d }nd||}d||}|$||f q!t|dkrq|d|	 7 }|D ]}||	krk|d| 7 }q^|d7 }t
d|}|d||	7 }dd } |d u rt%&|jjj'}!d|||!}"d| jv r| jd }#t(j))|#}$| j*+|$|jsd }%t	|%nd}#|d!|" 7 }t|jD ]G}&d"g|j }'d"g|j }(d#| j|& d |'|&< d$| j|& d |(|&< |d!d%|d!|'| |# 7 }|d!d%|d!|(| |# 7 }qn.d| jv r<| jd }#t(j))|#}$| j*+|$|js.d }%t	|%d&|| |#}"|d!|" 7 }d})t|jD ](}t|)D ]}*|d!7 }qI|d'|| || d |||| d 7 }|)d7 })qCt|)D ]}*|d!7 }qp|d(|7 }|d)|7 }tjdkrtd* t| i }+|+,t-  t.||+ |+| },|d urt/0|,}-|-|_1dd+l2m3}. |.4|,}/t5|/j t|/j}0i }1||||g|j | }2|06 D ]!\}3}4|3|2vrt"|4t7j8sJ |4j9:|4j;|4j<}5|5j;|1|3< qt=|/j|1 t>|/j? d  t@|j |_t>|j? d }6 fd,d-|D }tjdkrFtd.|  td/ t|/j td0 t|j |/j6 D ]{\}7}8tA|8jBD ]o\}}9t"|9t7jCr|9jDj;|kr|9j<}:|8j9};t7E|;|:}<|8jBd | |<_B|8jB|d d  |8_BtF|j? }=|<$t7G|=|: |j6 D ]
\}>}?|?|/j|>< q|8|/j|6< |<|/j|7< |D ]}@|/j|@ $t7G|6|: q nqTqK tH|/j|/_t5|/j t"|tIjJsJ |}AtK|A}Btjdkrtd1|B t|/j tL|/j |.M| j*| jN|/|Bd |.jOi }C|CS )2Nr   r   z6Cannot use the reserved word 'out' in stencil kernels.__sentinel__r>   name_var_tabler   z__numba_stencil_%s_%s-rz   r   r?   r   z	, {}=Nonestandard_indexingzYThe first argument to a stencil kernel must use relative indexing, not standard indexing.z[Standard indexing requested for an array name not present in the stencil kernel definition.zAfter add_indices_to_kernelz!After replace_return_with_setitemzdef {}({}{}):
r   z	{}[{}][0]z	{}[{}][1]z&    raise_if_incompatible_array_sizes(z)

full_shapez    {} = {}.shape
c                 S   s>   t | st | rdS t | r| dk rdS dS d S t| S )Nznp.nanr   z-np.infznp.inf)npisfiniteisnanisinfr   )cvalr   r   r   cval_as_strD  s   


z1StencilFunc._stencil_wrapper.<locals>.cval_as_strz{} = np.empty({}, dtype=np.{})
r   z-cval type does not match stencil return type.z    :z:-{}z-{}:z{}[{}] = {}
z{}[:] = {}
z.for {} in range(-min(0,{}),{}[{}]-max(0,{})):
z{} = 0
z    return {}
znew stencil func textcompilerc                    s   g | ]}|  qS r   r   ry   xstencil_stub_last_labelr   r   r{     r|   z0StencilFunc._stencil_wrapper.<locals>.<listcomp>zret_blocks w/ offsetsz"before replace sentinel stencil_irz#before replace sentinel kernel_copynew_stencil_param_types)Pr   rC   r   remove_argsr_   r   copy_propagateget_name_var_tableapply_copy_propagater   get_unused_var_namer   r}   r~   dtyper@   r   hexrB   r   r/   r-   r   r   r   rF   rE   rM   r0   r   rp   r?   ro   r   rU   r   rW   r   as_dtyper(   r   typeofrI   can_convertupdateglobalsr   r   r   r   r   r   run_frontendremove_delsrQ   r   rX   rR   r[   rv   rS   replace_var_namesr   keysadd_offset_to_labels	enumeraterT   r^   r   Blockr   Jumprename_labelsr   Typer   fixup_var_define_in_scope
compile_irrK   DEFAULT_FLAGS)Dr   r   r   r   r   r   r   r   r   	first_argin_cpsout_cpsr   sentinel_name	the_arraystencil_func_namer`   r5   index_var_namera   neighborhood_namer   r   kernel_sizer   rb   	func_textrangeslohiother_array
shape_namer   return_type_nameout_initr   cval_tymsgr   start_items	end_itemsoffsetjr   stencil_funcr   r   
stencil_ir	var_tablenew_var_dictreserved_namesrv   varnew_var	new_labelrc   rd   instrS   rR   
prev_blockbody_first_labellb	ret_blockarray_typesr   r   r   r   r   r     s  











 $














zStencilFunc._stencil_wrapperc                 O   s&  | j   | jd ur"t| j|d jkr"tdt| j|d jd|v rT|d }|j}t	|}t
j||jt|}tdd |D }tdd |D |g }nd }tdd |D }|}tjdkrmtd	||| | |\}	}
}| j|d |	|
|g|R  }|d u r|j| S |j||f  S )
Nr   zD{} dimensional neighborhood specified for {} dimensional input arrayr   c                 S      g | ]}t j|qS r   r   r  r   r   r   r   r{         z(StencilFunc.__call__.<locals>.<listcomp>c                 S   r:  r   r;  r   r   r   r   r{     r<  c                 S   r:  r   r;  r   r   r   r   r{     r<  r>   r'   )rI   refreshr?   r0   r-   r   r   r   r   
from_dtyper   r   r   
map_layoutr   r   r}   r~   r   r   entry_point)r   r   kwargsr   rdtyperttyperesult_typer9  array_types_fullr   r   r   r   r   r   r   r'     sB   




zStencilFunc.__call__N)r(   r)   r*   r+   rA   r   ro   rp   r   rL   r   r   r   r   r'   r   r   r   r   r=   A   s     * J
)  Kr=   constantc                 K   sX   t | ts
d}| }n| }d }|D ]}|dvrtd| qt||}|d ur*||S |S )NrF  )r   r   r?   zUnknown stencil option )rU   r   r   _stencil)func_or_moderE   rD   funcoptionwrapperr   r   r   stencil$  s   

rL  c                    s&    dkr
t d   fdd}|S )NrF  zUnsupported mode style c                    s"   ddl m} || }t| S )Nr   r   )r   r   r  r=   )rI  r   rC   rD   rE   r   r   	decorated:  s   
z_stencil.<locals>.decoratedr   )rD   rE   rN  r   rM  r   rG  6  s   rG  c                 C   s   t t tjjdS )z lowering for dummy stencil callsr   )lirConstantIntTyper   r   bitwidth)r"   r#   r$   r   r   r   r   stencil_dummy_lowerA  s   rS  )rF  )&r   numpyr   llvmliter   rO  r   r   r   r   r   r   r   numba.core.typing.templatesr	   r
   r   r   numba.core.imputilsr   numba.core.extendingr   numba.core.errorsr   numba.misc.specialr   r   r   numba.npr   objectr   r6   r<   r=   rL  rG  rS  r   r   r   r   <module>   s4   $
     
h