o
    iVL                  	   @   s   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	 ddl
mZmZmZ ddlmZ er4ddl
mZ ddlmZ eeeeefZejejejejejejejeej f	Z!h dZ"h d	Z#d
edefddZ$dd Z%dd Z&dd Z'dd Z(dd Z)dS )    N   )_is_numpy_array
_join_with_infer_numpy_type_is_npt_ndarray)
TensorDescTensorSymExprs)IS_AOT_ENABLED)KernelLaunchParams)AutoTuneCombination>    stdtrttensorrt>   streamtacticoutputsnsnamec                 C   sd   d| v rt d|  d|  d| dd|v r$t d| d|  d| d| tv r0t d|  dd S )N.zProvided namespace z- cannot have any '.' in trt.plugin.register("z::z", ...)zProvided name z is a reserved namespace)
ValueErrorRESERVED_NAMESPACES)r   r    r   V/home/ubuntu/.local/lib/python3.10/site-packages/tensorrt_bindings/plugin/_validate.py_validate_name_and_namespace9   s   
r   c                 C   s  g }t  }g }d}i }t| }t|j D ]\}\}	}
|
jtjjtjj	fvr0t
d|	 d|
jtjjkr?t
d|	 d|
jtjjurNt
d|	 dt|
jtrl|r^t
d| d||	 |d|	  q|d	krxt
d
| d|	tv rt
d|	 d|
jtvrt|
jr|s|
jtjkrt
dt|
jrt|
j}|tvrt
d|	 dt dd|f||	< nt
d|	 d|
j dt dt d	d|
jf||	< d}||
j d|	  |
j||	< q||dt| d|fS )NFz	Argument z3 is not a positional-or-keyword or keyword-only argzg does not have a type annotation. Please mark as TensorDesc or one of the serializable attribute types.z; has a default value. Default values are not supported yet.z]TensorDescs args and attribute args cannot be interspersed. Received function with signature r   zTensorDesc r   z]TensorDescs args should come first, followed by attributes. Received function with signature 'z,' is not allowed as a plugin attribute name.zIf using non-lazy registration, annotate numpy array attributes using 'numpy.typing.NDArray[dtype]', where 'dtype' is the expected numpy dtype of the array.Attribute 'zG' is not a supported numpy array type. Supported numpy arrays type are z
' of type z; is not a supported serializable type. Supported types are z or numpy arrays of type T ())dictinspect	signature	enumerate
parametersitemskind	ParameterPOSITIONAL_OR_KEYWORDKEYWORD_ONLYr   
annotationemptydefault
issubclassr   appendDISALLOWED_ATTR_NAMESSERIALIZABLE_BUILTIN_TYPESr   npndarrayr   r   SERIALIZABLE_NP_DTYPESr   )register_funclazy_registertensor_namesinput_attrsschema_chunkssaw_first_attrattrs_typessigidxr   paramnp_dtyper   r   r   _parse_register_inputsK   s   











r@   c                 C   s   t | }|j}|t jjkrtd| dt|tur3t 	|r't
|ts/td| dd}|S t|}|D ]}t
|tsItd| dq:t|}|S )NzENo return annotation found for register function. Received signature r   Return argument is of type z;. Return types can only be TensorDesc or Tuple[TensorDesc].r   )r"   r#   return_annotationr(   r,   r   typing
get_origintupleisclassr.   r   get_argslen)r5   r<   ret_annotationnum_outputsargsargr   r   r   _parse_register_return   s0   





rM   c              
      s2  g  d}t | }|j }|j D ]\}}|dkrd}|jt jjkr|dkrYt	
|jtur9td|j dt	|j}|D ]}t|tsWtd| d|j d	| d
qAq|dkrht|jtsgtdq|dkrwt|jtsvtdqt|jtr||jvrtd| d|j dq||jvrtd| dt| d|j|j| krtd| d|j|  d | q||jv rǈ | q fdd|D }	dt|j t|	d d }
|r|
d7 }
n|
d7 }
dd|j  d|
krtd| d|
 |jt jjkr|jd urtd |fS )NFr   Tr   z4'outputs' should be of type Tuple[Tensor]. Received r   z'Argument for receiving output Tensor, '' contains a . 'z' should be a Tuple[Tensor].r   z('stream' input argument should be an int('tactic' input argument should be an intUnexpected tensor '2' specified in autotune function. Expected one of Unexpected attribute 'z.' specified in impl function. Expected one of r   T' has a type annotation different from the one specified at registration. Expected ''.c                       g | ]}| v r|qS r   r   .0nimpl_attr_namesr   r   
<listcomp>       z"_validate_impl.<locals>.<listcomp>r   z, outputs, streamz	, tactic)r    , z Signature of the impl function '0' does not match the expected input arg schema: z!Return annotation should be None.)r"   r#   r8   keysr%   r&   r+   r(   r,   rC   rD   rE   r   rG   r.   r   intinput_tensor_nameslistr/   r   joinrB   )	impl_func
plugin_deffound_tacticr<   registered_attr_namesr   r>   rK   rL   expected_attr_schema_chunksexpected_schemar   rZ   r   _validate_impl   s   








rk   c              
      s  g  t | }|j }|j D ]\}}|jt jjkr|dkrQt	
|jtur1td|j dt	|jD ]}t|tsOtd| d|j d| dq9q|dkr`t|jts_td	qt|jtrx||jvrwtd
| d|j dq||jvrtd| dt| d|j|j| krtd| d|j|  d | q||jv r | q fdd|D }dt|j t|d d }dd|j  d|krtd| d| |j}	|	t jjkrtd| dd}
|	t jjkrVt	
|	turtd|	 d|
 dt	|	tdkr$td|	 d|
 dfdd }|d! |d" td# tsDtd$d#  d%td& tsVtd'd&  d( S ))Nr   8'outputs' should be of type Tuple[TensorDesc]. Received r   z+Argument for receiving output TensorDesc, 'rN   rO    ' should be a Tuple[TensorDesc].r   rP   rQ   rR   rS   z2' specified in aot_impl function. Expected one of r   rT   rU   c                    rV   r   r   rW   )aot_impl_attr_namesr   r   r\   G  r]   z&_validate_aot_impl.<locals>.<listcomp>r   Tz, outputs, tactic)r^   r    z$Signature of the aot_impl function 'r_   zENo return annotation found for aot_impl function. Received signature z`tuple[str | bytes, str | bytes, tensorrt.plugin.KernelLaunchParams, tensorrt.plugin.SymIntExprs]zReturn annotation is z. Expected    c                    s^    fdd}t   }|t ju s|tju r't   D ]}|| qd S |   d S )Nc                    s.   | t ur| turtd  d|  dd S d S )Nz"Return annotation for argument at z is 'z'. Expected 'str' or 'bytes'.)strbytesr   )arg_indexr   r   validate_str_or_byteso  s
   zV_validate_aot_impl.<locals>.validate_union_str_or_bytes.<locals>.validate_str_or_bytes)rC   rD   Uniontypes	UnionTyperG   )rt   ru   origa)rK   rs   r   validate_union_str_or_bytesn  s   
z7_validate_aot_impl.<locals>.validate_union_str_or_bytesr   r      z-Argument at index 2 of return annotation is 'z1'. Expected 'tensorrt.plugin.KernelLaunchParams'.   z-Argument at index 3 of return annotation is 'z5'. Expected a descendent of tensorrt.plugin.SymExprs.)r"   r#   r8   r`   r%   r&   r+   r(   r,   rC   rD   rE   r   rG   r.   r   ra   rb   rc   r/   r   rd   rB   rH   r   r	   )aot_impl_funcrf   r<   rh   r   r>   rL   ri   rj   rI   expected_return_schemar{   r   )rn   rK   r   _validate_aot_impl  s   









r   c              
      s"  t | }|j }g  |j D ]\}}|jt jjkr|dkrQt	
|jtur1td|j dt	|j}|D ]}t|tsOtd| d|j d| dq9qt|jtri||jvrhtd| d	|j dq||jvr{td
| d	t| d|j|j| krtd| d|j|  d | q||jv r | q fdd|D }dt|j t|d d }	dd|j  d|	krtd| d|	 d|j}
|
t jjkrt	
|
turt |
rt|
tstd|
 d S t	|
}|D ]}t|tstd|
 dq S )Nr   rl   r   z,Argument for receiving output TensorDescs, 'rN   rO   rm   rQ   rR   rS   r   rT   rU   c                    rV   r   r   rW   autotune_attr_namesr   r   r\     r]   z&_validate_autotune.<locals>.<listcomp>r   Tz
, outputs)r^   r    z&Specified autotune function signature z6 is not consistent with the expected input arg schema rA   zL. Return types can only be AutoTuneCombination or List[AutoTuneCombination].)r"   r#   r8   r`   r%   r&   r+   r(   r,   rC   rD   rE   r   rG   r.   r   rb   rc   r/   r   rd   rB   rF   r   )autotune_funcrf   r<   rh   r   r>   rK   rL   ri   rj   rI   r   r   r   _validate_autotune  s   










r   )*r"   numpyr2   rC   rw   _utilsr   r   r   r   _tensorr   r   r	   _exportr
   r   	_autotuner   ra   floatrq   boolrp   r1   int8int16int32int64float16float32float64bool_r4   r   r0   r   r@   rM   rk   r   r   r   r   r   r   <module>   s:   `!Ns