o
    i                     @   s  d dl Zd dlmZmZ d dlZddlmZm	Z	 d dl
mZmZ G dd deZG dd	 d	eeZG d
d deedZG dd dejZe G dd deZe G dd deZe G dd deZe G dd deZe G dd deZdd Ze G dd dZe G dd deZe G dd  d eZe G d!d" d"Ze G d#d$ d$Ze G d%d& d&eZe G d'd( d(Ze	re G d)d* d*Z dS dS )+    N)TupleUnion   )
public_apiIS_AOT_ENABLED)ABCabstractmethodc                   @   s   e Zd Zedd Zedd Zedd Zedd Zed	d
 Zedd Z	edd Z
edd ZeedefddZeedefddZeedd ZdS )SymExprc                 C      d S N selfopotherr   r   T/home/ubuntu/.local/lib/python3.10/site-packages/tensorrt_bindings/plugin/_tensor.py_op      zSymExpr._opc                 C   r
   r   r   r   r   r   r   r   __add__   r   zSymExpr.__add__c                 C   r
   r   r   r   r   r   r   __sub__"   r   zSymExpr.__sub__c                 C   r
   r   r   r   r   r   r   __mul__&   r   zSymExpr.__mul__c                 C   r
   r   r   r   r   r   r   __floordiv__*   r   zSymExpr.__floordiv__c                 C   r
   r   r   r   r   r   r   __eq__.   r   zSymExpr.__eq__c                 C   r
   r   r   r   r   r   r   __lt__2   r   zSymExpr.__lt__c                 C   r
   r   r   r   r   r   r   __repr__6   r   zSymExpr.__repr__returnc                 C   r
   r   r   r   r   r   r   is_constant:      zSymExpr.is_constantc                 C   r
   r   r   r   r   r   r   constant_value?   r   zSymExpr.constant_valuec                 C   r
   r   r   r   r   r   r   _exprE   r   zSymExpr._exprN)__name__
__module____qualname__r   r   r   r   r   r   r   r   r   propertyboolr   intr    r!   r   r   r   r   r	      s4    







r	   c                   @   s   e Zd ZdS )SymIntExprMetaN)r"   r#   r$   r   r   r   r   r(   J   s    r(   c                   @   s  e Zd ZdZdZd!deeejd f fddZ	dej
deed f fdd	Zdeed f fd
dZdeed f fddZdeed f fddZdeed f fddZdeed f fddZdeed f fddZdd ZedefddZdefddZedd Zdd  ZdS )"
SymIntExprz.
    Symbolic integer (scalar) expression
    Nvaluec                 C   sf   d| _ t|trtjdu rd| _ dS tj|| _ dS t|tjr&|| _ dS t|tr1|j | _ dS dS )z
        Args:
            value (Union[int, trt.IDimensionExpr, SymIntExpr], optional): Constant or another symbolic expression. Defaults to creating a fake shape expression.
        N)	_int_expr
isinstancer'   r)   _exprBuilderconstanttrtIDimensionExprr   r*   r   r   r   __init__S   s   




zSymIntExpr.__init__r   r   c                 C   s*   t |tr	t|}ttj|| j|jS r   )r,   r'   r)   r-   	operationr!   r   r   r   r   r   c   s   
zSymIntExpr._opc                 C      |  tjj|S r   )r   r/   DimensionOperationSUMr   r   r   r   r   j      zSymIntExpr.__add__c                 C   r4   r   )r   r/   r5   SUBr   r   r   r   r   m   r7   zSymIntExpr.__sub__c                 C   r4   r   )r   r/   r5   PRODr   r   r   r   r   p   r7   zSymIntExpr.__mul__c                 C   r4   r   )r   r/   r5   	FLOOR_DIVr   r   r   r   r   s   r7   zSymIntExpr.__floordiv__c                 C   r4   r   )r   r/   r5   EQUALr   r   r   r   r   v   r7   zSymIntExpr.__eq__c                 C   r4   r   )r   r/   r5   LESSr   r   r   r   r   y   r7   zSymIntExpr.__lt__c                 C   >   | j rdt|  dS | jsdt|  dS d| j  dS )NzFakeSymIntExpr[id=]zSymIntExpr[id=zSymIntExpr[	_is_dummyidr   r!   get_constant_valuer   r   r   r   r   |   
   zSymIntExpr.__repr__r   c                 C   s   | j rtd| j S )z
        `True` if this integer expression is a build-time constant, `False` otherwise.

        Raises:
            RuntimeError: For fake :class:`SymIntExpr`\s. Check :attr:`is_fake` to determine accessibility.
        zPNot accessible for fake 'SymIntExpr's. Check is_fake to determine accessibility.)r@   RuntimeErrorr!   r   r   r   r   r   r      s
   
zSymIntExpr.is_constantc                 C   s   | j std| j S )z
        Return value of the constant integer expression.

        Raises:
            RuntimeError: For non-constant integer expressions. Check :attr:`is_constant` to determine accessibility.
        zbNot accessible for non-constant integer expressions. Check is_constant to determine accessibility.)r   rD   r!   rB   r   r   r   r   r       
   
zSymIntExpr.constant_valuec                 C      | j S r   )r+   r   r   r   r   r!      s   zSymIntExpr._exprc                 C   s   t | d S Nr   )r)   r   r   r   r   _clone   s   zSymIntExpr._cloner   )r"   r#   r$   __doc__r-   r   r'   r/   r0   r2   r5   r   r   r   r   r   r   r   r   r%   r&   r   r    r!   rH   r   r   r   r   r)   M   s$    
r)   )	metaclassc                   @   s   e Zd ZdefddZdS )SymExprImplexprc                 C   sv   t j|  t jj| _t|trt jj	| _
nt|tr!t jj| _
nt|tr,t jj| _
n	tdt| |j| _d S )NzUnknown SymIntExpr type )r/   ISymExprr2   PluginArgTypeINTtyper,   SymInt32PluginArgDataTypeINT32dtypeSymInt16INT16SymInt8INT8
ValueErrorr!   rL   )r   rL   r   r   r   r2      s   



zSymExprImpl.__init__N)r"   r#   r$   r)   r2   r   r   r   r   rK      s    rK   c                       s<   e Zd ZdZddeeejef f fddZ	dd Z
  ZS )	rQ   z2
    Symbolic expression for a 32-bit integer
    Nr*   c                       t  | d S r   superr2   r1   	__class__r   r   r2      r7   zSymInt32.__init__c                 C   s   t | S r   )rK   r   r   r   r   __call__      zSymInt32.__call__r   )r"   r#   r$   rI   r   r'   r/   r0   r)   r2   r_   __classcell__r   r   r]   r   rQ      s     rQ   c                       4   e Zd ZdZddeeejdf f fddZ  Z	S )rW   z2
    Symbolic expression for an 8-bit integer
    Nr*   r)   c                    rZ   r   r[   r1   r]   r   r   r2      r7   zSymInt8.__init__r   
r"   r#   r$   rI   r   r'   r/   r0   r2   ra   r   r   r]   r   rW          (rW   c                       rb   )rU   z2
    Symbolic expression for a 16-bit integer
    Nr*   r)   c                    rZ   r   r[   r1   r]   r   r   r2      r7   zSymInt16.__init__r   rc   r   r   r]   r   rU      rd   rU   c                       s   e Zd ZdZddeeejd ef f fddZ	dej
deed f f fdd	Zd
d ZedefddZedefddZedef fddZdef fddZdd Z  ZS )	ShapeExprz>
    Symbolic expression for single dimension of a tensor
    Nr*   c                    s   t  | tj| _d| _d| _|du rd| _dS t|tr*| jdu r(d| _dS dS t|tr9|j| _|j| _dS t|trA	 dS dS )z
        Args:
            value (Union[int, trt.IDimensionExpr, ShapeExpr, SymIntExpr], optional): Constant or another symbolic expression. Defaults to creating a fake shape expression.
        FNT)	r\   r2   r)   r-   r@   _is_size_tensorr,   r'   re   r1   r]   r   r   r2      s    





zShapeExpr.__init__r   r   c                    s,   | j rtd| jrt S tt ||S NzKIt is not permitted to perform binary operations on size tensor expressions)rf   rY   r@   re   r\   r   r   r]   r   r   r      s
   zShapeExpr._opc                 C   r=   )NzFakeShapeExpr[id=r>   zShapeExpr[id=z
ShapeExpr[r?   r   r   r   r   r      rC   zShapeExpr.__repr__r   c                 C   rF   )z
        A ShapeExpr may be "fake" when it is accessed in a non-shape calculation context.
        Fake `ShapeExpr`s are externally indistinguishable unless `is_constant` or `constant_value` is required.
        )r@   r   r   r   r   is_fake   s   zShapeExpr.is_fakec                 C   rF   )zM
        `True` if this represents a size tensor, `False` otherwise.
        )rf   r   r   r   r   is_size_tensor      zShapeExpr.is_size_tensorc                    s   | j rtdt jS )z
        `True` if this shape expression is a build-time constant, `False` otherwise.

        Raises:
            RuntimeError: For fake :class:`ShapeExpr`\s. Check :attr:`is_fake` to determine accessibility.
        ONot accessible for fake 'ShapeExpr's. Check is_fake to determine accessibility.)r@   rD   r\   r   r   r]   r   r   r     s
   zShapeExpr.is_constantc                    s   | j stdt  S )z
        Return value of the constant shape expression.

        Raises:
            RuntimeError: For non-constant shape expressions. Check :attr:`is_constant` to determine accessibility.
        z`Not accessible for non-constant shape expressions. Check is_constant to determine accessibility.)r   rD   r\   r    r   r]   r   r   r      rE   zShapeExpr.constant_valuec                 C   s    t | d }| j|_| j|_|S rG   )re   r@   rf   r   retr   r   r   rH   "  s   zShapeExpr._cloner   )r"   r#   r$   rI   r   r'   r/   r0   r)   r2   r5   r   r   r%   r&   rh   ri   r   r    rH   ra   r   r   r]   r   re      s    " 	re   c                       sb   e Zd ZdZd fddZdejdeedf fd	d
Z	e
dd Ze
 fddZdd Z  ZS )SizeTensorShapeExprzZ
    Extends :class:`ShapeExpr`

    A shape expression that represent a size tensor

    size_tensor_descSizeTensorDescc                    s$   t    d| _|jj| _|| _dS )z
        .. note:: It is recommended to use :attr:`SizeTensorDesc.expr` to get a :class:`SizeTensorShapeExpr` representing a size tensor
        TN)r\   r2   rf   optrh   r@   _size_tensor_desc)r   ro   r]   r   r   r2   0  s   


zSizeTensorShapeExpr.__init__r   r   re   c                 C   s   t drg   )rY   r   r   r   r   r   9  r`   zSizeTensorShapeExpr._opc                 C   s   | j rtddS )Nrk   F)r@   rD   r   r   r   r   r   <  s
   zSizeTensorShapeExpr.is_constantc                    s:   | j d ur| j S t j| jj| jjj| jjj| _ | j S r   )	r+   r\   r-   declare_size_tensorrr   indexrq   r!   upper_boundr   r]   r   r   r!   D  s   
$zSizeTensorShapeExpr._exprc                 C   s   dt |  dS )Nz$ShapeExpr[is_size_tensor = True, id=r>   )rA   r   r   r   r   r   L  r7   zSizeTensorShapeExpr.__repr__)ro   rp   )r"   r#   r$   rI   r2   r/   r5   r   r'   r   r%   r   r!   r   ra   r   r   r]   r   rn   (  s    	
rn   c                 C   s8   t | tr	t| S t | trtdtdt|  d)Nz,Float symbolic expressions are not supportedzUnsupported type: '')r,   r'   rQ   floatrY   rP   )sr   r   r   _from_scalarO  s
   

ry   c                   @   sd   e Zd ZdefddZedeeeef  dd fddZ	dd	 Z
d
d Zdd Zdd Zdd ZdS )SymExprslengthc                 C   s   || _ dg| | _dS )z
        Iterable holding symbolic expressions

        Args:
            length (int): Number of dimensions of the tensor
        N)_length_exprsr   r{   r   r   r   r2   Z  s   zSymExprs.__init__shape_exprsr   c                 C   ,   t dd |D }| t|}t||_|S )zw
        Args:
            shape_exprs (Tuple[Union[SymExpr, int]]): Tuple to construct :class:`SymExprs` from
        c                 S   s"   g | ]}t |tr|nt|qS r   )r,   r	   ry   .0er   r   r   
<listcomp>k     " z'SymExprs.from_tuple.<locals>.<listcomp>tuplelenlistr}   clsr   shape_exprs_instr   r   r   
from_tupled     
zSymExprs.from_tuplec                 C   
   t | jS r   )iterr}   r   r   r   r   __iter__p     
zSymExprs.__iter__c                 C   
   | j | S r   )r}   r   rt   r   r   r   __getitem__s  r   zSymExprs.__getitem__c                 C   rF   r   r|   r   r   r   r   __len__v     zSymExprs.__len__c                 C   s2   || j kr	tdt|tst|}|| j|< d S NIndex out of range)r|   
IndexErrorr,   r	   ry   r}   )r   rt   rL   r   r   r   __setitem__y  s
   

zSymExprs.__setitem__c                 C      dd dd | jD  dS )Nz	SymExprs[, c                 S      g | ]}|  qS r   r   r   rx   r   r   r   r         z%SymExprs.__repr__.<locals>.<listcomp>r>   joinr}   r   r   r   r   r        zSymExprs.__repr__N)r"   r#   r$   r'   r2   classmethodr   r   r	   r   r   r   r   r   r   r   r   r   r   rz   X  s    
 	rz   c                       sf   e Zd Zd fdd	Zedeeeef  ddfddZ	defd	d
Z
dd Zdd Zdd Z  ZS )
ShapeExprsFc                    sR   |t jjkrtdt jj d| dt | || _|r't g| | _dS dS )z
        Iterable holding :class:`ShapeExpr`\s, representing a tensor shape

        Args:
            length (int): Number of dimensions of the tensor
        z6ShapeExprs can only support up to trt.Dims.MAX_DIMS = z dimensions. z given.N)	r/   DimsMAX_DIMSrY   r\   r2   r@   re   r}   )r   r{   r@   r]   r   r   r2     s   zShapeExprs.__init__r   r   re   c                 C   r   )z{
        Args:
            shape_exprs (Tuple[Union[ShapeExpr, int]]): Tuple to construct :class:`ShapeExprs` from
        c                 S   "   g | ]}t |tr|nt|qS r   )r,   re   r   r   r   r   r     r   z)ShapeExprs.from_tuple.<locals>.<listcomp>r   r   r   r   r   r     r   zShapeExprs.from_tuplec                 C   s    t d}| jD ]}||9 }q|S )zJ
        Returns a symbolic expression for the number of elements
        r   )re   r}   )r   rm   rx   r   r   r   numel  s   

zShapeExprs.numelc                 C   P   || j kr	tdt|ts!t|tstdt| dt|}|| j|< d S )Nr   z'Value should be int or ShapeExpr. Got 'rv   )r|   r   r,   re   r'   rY   rP   r}   r   rt   r*   r   r   r   r        


zShapeExprs.__setitem__c                 C   r   )NzShapeExprs[r   c                 S   r   r   r   r   r   r   r   r     r   z'ShapeExprs.__repr__.<locals>.<listcomp>r>   r   r   r   r   r   r     r   zShapeExprs.__repr__c                 C   s"   t dd | jD }| j|_|S )Nc                 s   s    | ]}|  V  qd S r   )rH   r   r   r   r   	<genexpr>  s    z$ShapeExprs._clone.<locals>.<genexpr>)r   r   r}   r@   rl   r   r   r   rH     s   zShapeExprs._clone)F)r"   r#   r$   r2   r   r   r   re   r'   r   r   r   r   rH   ra   r   r   r]   r   r     s     	r   c                       sN   e Zd Z fddZedeeeef  ddfddZ	dd	 Z
d
d Z  ZS )SymIntExprsc                    s   t  | dS )z
        Iterable holding :class:`SymIntExpr`\s

        Args:
            length (int): Number of symbolic expressions in the iterable
        Nr[   r~   r]   r   r   r2     s   zSymIntExprs.__init__r   r   r)   c                 C   r   )z}
        Args:
            shape_exprs (Tuple[Union[SymIntExpr, int]]): Tuple to construct :class:`SymIntExprs` from
        c                 S   r   r   )r,   r)   r   r   r   r   r     r   z*SymIntExprs.from_tuple.<locals>.<listcomp>r   r   r   r   r   r     r   zSymIntExprs.from_tuplec                 C   r   )Nr   z(Value should be int or SymIntExpr. Got 'rv   )r|   r   r,   r)   r'   rY   rP   r}   r   r   r   r   r     r   zSymIntExprs.__setitem__c                 C   r   )NzSymIntExprs[r   c                 S   r   r   r   r   r   r   r   r     r   z(SymIntExprs.__repr__.<locals>.<listcomp>r>   r   r   r   r   r   r     r   zSymIntExprs.__repr__)r"   r#   r$   r2   r   r   r   r)   r'   r   r   r   ra   r   r   r]   r   r     s    	 r   c                   @   s   e Zd ZdZ	ddeee ejej	f fddZ
defddZd	d
 Zdd Zdd Zdd ZedefddZedee fddZedee fddZedee fddZdd Zdd ZdS )Shapez4
    Numerical representation of a tensor shape
    Ntensor_descc                 C   s   d | _ t|tjrt|jj| _|jj| _|| _	d S t|tj
r,t|j| _|j| _d S t|tr?t|| _t| j| _d S |d u rNd| _td| _d S td)Nr   zUnsupported type used for constructing trt.plugin.Shape! tensor_desc must be a Tuple[int], trt.DynamicPluginTensorDesc, or trt.PluginTensorDesc)_is_dynamicr,   r/   DynamicPluginTensorDescr   descdimsr|   _shapes_descPluginTensorDescr   r   rY   )r   r   r   r   r   r2     s   


zShape.__init__r   c                 C      | j rtdtt| jS )zy
        Number of elements contained

        Raises:
            ValueError: When :attr:`is_dynamic` is `True`
        z)Shape has at least one dynamic dimension.)
is_dynamicrY   r'   npprodr   r   r   r   r   r     s   zShape.numelc                 c   s    | j E d H  d S r   r   r   r   r   r   r   	  s   zShape.__iter__c                 C   r   r   r   r   r   r   r   r     r   zShape.__getitem__c                 C   rF   r   r   r   r   r   r   r     r   zShape.__len__c                 C   s   dt t|  S )Nr   )strr   r   r   r   r   __str__  r7   zShape.__str__c                 C   s6   | j dur| j S d| _ | jD ]	}|dkrd| _ q| j S )^
        `True` if this tensor has at least one dynamic dimension, `False` otherwise.
        NFT)r   r   )r   dr   r   r   r     s   

zShape.is_dynamicc                 C   ,   | j stdt| dstdt| jjS )zH
        Optimum value of dimensions specified for auto-tuning.
        z5opt property is only accessible if is_dynamic is truer   zcShape object has at least one dynamic dimension, but no information is available on 'opt' property.)r   rY   hasattrAttributeErrorr   r   rq   r   r   r   r   rq   $     
z	Shape.optc                 C   r   )z6
        Lower bounds on tensor's dimensions.
        z5min property is only accessible if is_dynamic is truer   zcShape object has at least one dynamic dimension, but no information is available on 'min' property.)r   rY   r   r   r   r   minr   r   r   r   r   1  r   z	Shape.minc                 C   r   )z6
        Upper bounds on tensor's dimensions.
        z5max property is only accessible if is_dynamic is truer   zcShape object has at least one dynamic dimension, but no information is available on 'max' property.)r   rY   r   r   r   r   maxr   r   r   r   r   >  r   z	Shape.maxc                 C   s    || j kr	td|| j|< d S r   )r|   r   r   )r   rt   valr   r   r   r   K  s   
zShape.__setitem__c                 C   s   t  }|j| j |S r   )r   __dict__updaterl   r   r   r   rH   P  s   zShape._cloner   )r"   r#   r$   rI   r   r   r'   r/   r   r   r2   r   r   r   r   r   r%   r&   r   rq   r   r   r   rH   r   r   r   r   r     s*    
r   c                	       s  e Zd ZdZd5dedejdejdefddZ	d	e
fd
dZed	e
fddZedd Zd6ddZd6ddZd6ddZd6ddZd7ddZdd Zed	efddZed	ejfddZed	efd d!Zed	ejfd"d#Zed	efd$d%Zejd&d Zejd'd Zejd(d! Zejd)d# Zejd*d% Zed	efd+d,Zed	efd-d.Zed	efd/d0Zed	efd1d2Z  fd3d4Z!  Z"S )8
TensorDesczb
    Descriptor for a tensor
    A `TensorDesc` never contains nor refers to any tensor data.
    N
shape_exprrT   formatscalec                 C   s.   || _ || _d| _|| _|| _d| _d| _dS )a  
        Args:
            shape_expr (ShapeExprs): The data with which to initialize the tensor.
            dtype (trt.DataType): The data type of the tensor.
            format (trt.TensorFormat): Format (layout) of the tensor.
            scale (float): Scale for INT8 data type.

        .. code-block:: python
            :linenos:
            :caption: Creates a TensorDesc with constant shape expressions

            tensor = trt.TensorDesc((10, 2, 32, 32), dtype=trt.float32)

        .. code-block:: python
            :linenos:
            :caption: Creates a TensorDesc from shape expression of another TensorDesc

            tensor = trt.from_shape_expr(other.shape_expr, dtype=trt.float32)
        NF)_shape_expr_dtype_shape_format_scale_aliased_to
_immutable)r   r   rT   r   r   r   r   r   r2   ^  s   
zTensorDesc.__init__r   c                 C   s   | j stdtt| jS )a!  
        Returns:
            Returns an int with the number of elements of the tensor.

        .. warning::
            Should only be called when TensorDesc.has_shape is true. If a symbolic expression for the number of elements is required, query TensorDesc.shape_expr.numel().
        tTensorDesc has no shape information available at this stage. Inspect TensorDesc.has_shape to determine availability.)	has_shaperY   r'   r   r   shaper   r   r   r   r     s
   zTensorDesc.numelc                 C   r   z&
        Number of dimensions
        )r   r   r   r   r   r   ndim     
zTensorDesc.ndimc                 C      dS NFr   r   r   r   r   ri     r   zTensorDesc.is_size_tensorc                 C   s   |   }d|_|S )a  
        Returns:
            Returns a TensorDesc which has identical properties to this tensor, and is mutable.

        .. code-block:: python
            :linenos:
            :caption: Communicate that output tensor has identical properties to the input tensor

            @tensorrt.plugin.register("my::plugin")
            def _(inp: tensorrt.plugin.TensorDesc) -> tensorrt.plugin.TensorDesc:
                return inp.like()
        F)rH   r   r   clonedr   r   r   like  s   zTensorDesc.likec                 C   s   |   }d|_| |_d|_|S )a4  
        Returns:
            Returns a TensorDesc which has identical properties and is aliased to this tensor (would result in a `Tensor` during enqueue sharing the same data buffer).
            Returned TensorDesc is immutable.

        .. code-block:: python
            :linenos:
            :caption: Communicate that output tensor has identical properties to the input tensor

            @tensorrt.plugin.register("my::plugin")
            def _(inp: tensorrt.plugin.TensorDesc) -> tensorrt.plugin.TensorDesc:
                return inp.aliased()
        FT)rH   r   r   r   r   r   r   aliased  s
   zTensorDesc.aliasedc                 C   sF   t  }|j| j d|_| j |_| jd ur| j |_d|_|S )NFT)r   r   r   r   r   rH   r   r   r   r   r   rH     s   
zTensorDesc._clonec                 C   rF   )z
        Returns:
            Returns a TensorDesc for the tensor which this tensor is aliased to. Returns None is this tensor is not aliased to any other tensor.
        r   r   r   r   r   get_aliased  rj   zTensorDesc.get_aliasedc                 C   s   | j stdd S )Nr   )r   rY   r   r   r   r   _validate_has_shape  s
   zTensorDesc._validate_has_shapec                 C   s    t | dr| jrtdd S d S )Nr   z"Cannot modify immutable TensorDesc)r   r   rY   r   r   r   r   _validate_not_immutable  s   z"TensorDesc._validate_not_immutablec                 C   rF   )z<
        Symbolic expressions for the tensor shape.
        r   r   r   r   r   r     rj   zTensorDesc.shape_exprc                 C   rF   z*
        Data type of the tensor.
        r   r   r   r   r   rT     rj   zTensorDesc.dtypec                 C      |    | jS )z
        The (concrete) shape of the tensor.

        .. warning::
            Only accessible when TensorDesc.has_shape is true.
        )r   r   r   r   r   r   r        zTensorDesc.shapec                 C   r   )z
        The format of the tensor.

        .. warning::
            Only accessible when TensorDesc.has_shape is true.
        )r   r   r   r   r   r   r     r   zTensorDesc.formatc                 C   r   )z
        Scale for INT8 data type.

        .. warning::
            Only accessible when TensorDesc.has_shape is true.
        )r   r   r   r   r   r   r     r   zTensorDesc.scalec                 C   
   || _ d S r   r   r1   r   r   r   r   
     
c                 C   r   r   r   r1   r   r   r   rT     r   c                 C      |    || _d S r   )r   r   r1   r   r   r   r        
c                 C   r   r   )r   r   r1   r   r   r   r     r   c                 C   r   r   )r   r   r1   r   r   r   r     r   c                 C   
   | j duS zT
        True if this tensor is aliased to another tensor, False otherwise.
        Nr   r   r   r   r   
is_aliased!  r   zTensorDesc.is_aliasedc                 C   r   )zV
        True if this tensor has concrete shape information, False otherwise.
        Nr   r   r   r   r   r   (  r   zTensorDesc.has_shapec                 C   s   | j std| jjS )r   r   )r   rY   r   r   r   r   r   r   r   /  s
   zTensorDesc.is_dynamicc                 C   r   )zV
        True if this tensor has symbolic shape expressions, False otherwise.
        N)r   r   r   r   r   has_shape_expr:  r   zTensorDesc.has_shape_exprc                    2   t | dr| jr|dkrtdt || d S )Nr   z-Cannot modify immutable TensorDesc propertiesr   r   rY   r\   __setattr__r   namer*   r]   r   r   r   A     zTensorDesc.__setattr__)NNNN)r   r   )r   N)#r"   r#   r$   rI   r   r/   DataTypeTensorFormatrw   r2   r'   r   r%   r   ri   r   r   rH   r   r   r   r   rT   r   r   r   r   setterr&   r   r   r   r   r   ra   r   r   r]   r   r   X  sV     !














r   c                       s   e Zd ZdZdedef fddZedd Zedefd	d
ZedefddZ	ede
fddZde
fddZdefddZ  ZS )rp   z
    Extends :class:`TensorDesc`

    Descriptor for a size tensor: a scalar of either INT32 or INT64 data type used to express the extent of a data-dependent dimension.
    rq   ru   c                    s4   t  tdtj || _|| _d| _t| | _	dS )a  
        Args:
            opt (ShapeExpr): Symbolic expression for the extent of this size tensor to use in the autotune process of the engine build
            upper_bound (ShapeExpr): Symbolic expression for the upper-bound of this size tensor

        .. note:: It is recommended to construct a size tensor using :func:`size_tensor` instead of using this constructor directly
        r   N)
r\   r2   r   r/   int32_opt_upper_bound_indexrn   r!   )r   rq   ru   r]   r   r   r2   M  s
   zSizeTensorDesc.__init__c                 C   r   )NTr   r   r   r   r   ri   [  r   zSizeTensorDesc.is_size_tensorr   c                 C   rF   )z{
        Symbolic expression for the extent of this size tensor to use in the autotune process of the engine build
        )r  r   r   r   r   rq   _  rj   zSizeTensorDesc.optc                 C   rF   )zM
        Symbolic expression for the upper-bound of this size tensor
        )r  r   r   r   r   ru   f  rj   zSizeTensorDesc.upper_boundc                 C   rF   )z@
        Output index at which this size tensor resides
        r  r   r   r   r   rt   m  rj   zSizeTensorDesc.indexidxc                 C   r   r   r  )r   r  r   r   r   
_set_indext  r   zSizeTensorDesc._set_indexc                 C   rF   )z:
        Symbolic expression for this size tensor
        )r!   r   r   r   r   rL   w  s   zSizeTensorDesc.expr)r"   r#   r$   rI   re   r2   r%   ri   rq   ru   r'   rt   r  rn   rL   ra   r   r   r]   r   rp   F  s    
rp   c                       sb  e Zd ZdZdd ZedefddZedefddZede	j
fd	d
ZedefddZede	jfddZedefddZedee fddZejdd Zejdd
 Zejdd Zejdd Zejdd Zejdd ZdefddZedd Z fddZd)dd Zed!d" Zed#d$ Zd*d&eeee e	jf dd fd'd(Z  ZS )+Tensoraa  
    Representation of a tensor that carries data

    :class:`Tensor` objects are strictly *descriptors* of a tensor with an underlying data buffer. `tensorrt.plugin` does not provide any APIs that perform standard data-altering operations on :class:`Tensor`\s.

    Supports `__cuda_array_interface__` for interoperability with other frameworks.

    c                 C   s@   d | _ d | _d | _d | _d | _d | _d | _d | _d | _d| _	d S r   )
	_data_ptrr   r   r   r   _stridesr   _stream
_read_onlyr   r   r   r   r   r2     s   
zTensor.__init__r   c                 C   r   r   )r   r   r   r   r   r   r     r   zTensor.ndimc                 C   rF   )z;
        Pointer to the data buffer of this tensor
        r  r   r   r   r   data_ptr  rj   zTensor.data_ptrc                 C   rF   r   r   r   r   r   r   rT     rj   zTensor.dtypec                 C   rF   )z5
        The (concrete) shape of the tensor.
        r   r   r   r   r   r     rj   zTensor.shapec                 C   rF   )z+
        The format of the tensor.
        r   r   r   r   r   r     rj   zTensor.formatc                 C   rF   )z+
        Scale for INT8 data type.
        r   r   r   r   r   r     rj   zTensor.scalec                 C   rF   )z)
        Strides of this tensor.
        r	  r   r   r   r   strides  rj   zTensor.stridesc                 C   r   r   r  r1   r   r   r   r    r   c                 C   r   r   r   r1   r   r   r   rT     r   c                 C   r   r   r   r1   r   r   r   r     r   c                 C   r   r   r  r1   r   r   r   r     r   c                 C   r   r   r  r1   r   r   r   r     r   c                 C   r   r   r  r1   r   r   r   r    r   c                 C   r   )a.  
        Returns the number of elements of the tensor

        Raises:
            ValueError: If the tensor has a data-dependent dimension. Examine :attr:`is_data_dependent` to determine whether the tensor is data-dependent.

        Returns:
            int: Number of elements of the tensor
        z|Tensor has a data-dependent dimension. Examine Tensor.shape to determine wildcards (representing data-dependent dimensions).)is_data_dependentrY   r'   r   r   r   r   r   r   r   r     s
   
zTensor.numelc                    s    j tjjtjjtjjfv rtd j  dt jt	
t j jd} j|d< d|d<  jdf|d< t fd	d
 jD |d< |S )Nz	Handling z0 via '__cuda_array_interface__' is not supported)r   typestrstream   versionFdatac                    s$   g | ]}|t t jj qS r   )r   rT   r/   nptyper   itemsizer   r   r   r   r     s   $ z3Tensor.__cuda_array_interface__.<locals>.<listcomp>r  )r   r/   r   BF16FP8INT4rY   r   r   r   rT   r  r   r
  r  r	  )r   r   r   r   r   __cuda_array_interface__  s    
zTensor.__cuda_array_interface__c                    r   )Nr   z)Cannot modify immutable Tensor propertiesr   r   r]   r   r   r     r   zTensor.__setattr__c                 C   rF   )z
        Returns:
            Returns :class:`Tensor` of the tensor which this tensor is aliased to. Returns None is this tensor is not aliased to any other tensor.
        r   r   r   r   r   r     rj   zTensor.get_aliasedc                 C   s
   | j du S r   r   r   r   r   r   r     r   zTensor.is_aliasedc                 C   s   | j jS )zf
        True if this tensor contains at least one data-dependent dimension, False otherwise.
        )r   r   r   r   r   r   r    s   zTensor.is_data_dependentNr   c                 C   s   t  }|j| j d|_t|tjrt||_n0t|tr"||_n't|t	r@t }||_
| j|_| j|_| j|_t||_n	|du rEntd| js\|jr\|j |  kr\td| |_|S )a  
        Return a :class:`Tensor` which has the same :attr:`data_ptr` as this but has the provided `shape`.

        Args:
            shape (Union[Shape, Tuple[int], trt.PluginTensorDesc], optional): Required shape of the new tensor (must have the same volume). Defaults to same shape.

        Raises:
            ValueError: If `shape` is not a supported type or if it does not have the same volume
        FNzUnsupported type for 'shape'z8Volume of this tensor is less than the provided 'shape'.)r  r   r   r   r,   r/   r   r   r   r   r   r   rP   r   r   r   r   rY   r  r   r   )r   r   r   r   r   r   r   r   !  s,   


zTensor.aliased)r   r  r   )r"   r#   r$   rI   r2   r%   r'   r   r  r/   r   rT   r   r   r   r   rw   r   r   r  r   r   r  r   r   r   r  r   r   r   ra   r   r   r]   r   r    sN    









,r  c                   @   s   e Zd ZdZ							ddeeejef deeejef deeejef deeejef deeejef d	eeejef d
eeejef fddZ	dd Z
dS )KernelLaunchParamsac  
        Args:
            grid_x (Union[int, trt.IDimensionExpr, SymInt32], optional): The grid x dimension. Defaults to 1.
            grid_y (Union[int, trt.IDimensionExpr, SymInt32], optional): The grid y dimension. Defaults to 1.
            grid_z (Union[int, trt.IDimensionExpr, SymInt32], optional): The grid z dimension. Defaults to 1.
            block_x (Union[int, trt.IDimensionExpr, SymInt32], optional): The x dimension of each thread block. Defaults to 1.
            block_y (Union[int, trt.IDimensionExpr, SymInt32], optional): The y dimension of each thread block. Defaults to 1.
            block_z (Union[int, trt.IDimensionExpr, SymInt32], optional): The z dimension of each thread block. Defaults to 1.
            shared_mem (Union[int, trt.IDimensionExpr, SymInt32], optional): Shared-memory per thread block in bytes. Defaults to 0.
        r   r   grid_xgrid_ygrid_zblock_xblock_yblock_z
shared_memc                 C   sJ   t || _t || _t || _t || _t || _t || _t || _d S r   )rQ   r  r   r!  r"  r#  r$  r%  )r   r  r   r!  r"  r#  r$  r%  r   r   r   r2   S  s   





zKernelLaunchParams.__init__c                 C   s*   |dv rt || j|< d S td| d)N)r  r   r!  r"  r#  r$  r%  z,KernelLaunchParams object has no attribute 'rv   )rQ   r   r   r   r   r   r   r   d  s   zKernelLaunchParams.__setattr__N)r   r   r   r   r   r   r   )r"   r#   r$   rI   r   r'   r/   r0   rQ   r2   r   r   r   r   r   r  G  s2    
r  )!tensorrtr/   typingr   r   numpyr   _exportr   r   abcr   r   r	   rP   r(   r)   rM   rK   rQ   rW   rU   re   rn   ry   rz   r   r   r   r   rp   r  r  r   r   r   r   <module>   sP   2Y	X&	,9%r n8 G