o
    i/                     @   sn   d dl Z d dlZd dlmZmZ d dlZddlmZ ddl	m
Z
 dd ZG dd	 d	Ze
 G d
d dZdS )    N)ListIterable   _str_to_data_type)
public_apic                 C   s  g }d}| D ]3}| d}t|dkr1d|v rtd| d|dkr1|t|kr1td|  dt|t|}qt|D ]}|d gt|   q>t| D ];\}}| d}t|dkryd|v rg| d}tt|D ]
}|d || |< qmqOtt|D ]
}|| || |< qqOt| D ]3\}}g }	d|v r| d}tdt|D ]}|D ]}
t	|
}|| ||< |	| qq|
|	 q|S )	Nr   |r   *zType/Format 'z' contains both '|' and '*'zType/Format combinations z" contain illegal dependent lengths)splitlen
ValueErrorbuiltinsmaxrangeappend	enumeratecopydeepcopyextend)io_variantscombinationsmx_poly
io_variantio_variant_list_jik	new_combscnew_c r!   V/home/ubuntu/.local/lib/python3.10/site-packages/tensorrt_bindings/plugin/_autotune.py_gen_onesided_combinations   sR   







r#   c                   @   sF   e Zd ZdddZdd ZdddZd	d
 Zdd ZdefddZ	dS )_TypeFormatCombinationr   c                 C   s"   d g| | _ d g| | _g | _d S N)typeslayoutstactics)selfnumr!   r!   r"   __init__V   s   
z_TypeFormatCombination.__init__c                 C   s
   || _ d S r%   )r&   )r)   r&   r!   r!   r"   	set_types[   s   
z _TypeFormatCombination.set_typesNc                 C   s*   t |tr
|| _d S |gt| j | _d S r%   )
isinstancer   r'   r   r&   )r)   r'   r!   r!   r"   set_layouts^   s   

z"_TypeFormatCombination.set_layoutsc                 C   s   t t| jt| jfS r%   )hashtupler&   r'   r)   r!   r!   r"   __hash__d   s   z_TypeFormatCombination.__hash__c                 C   s"   t |to| j|jko| j|jkS r%   )r-   r$   r&   r'   )r)   otherr!   r!   r"   __eq__g   s
   


z_TypeFormatCombination.__eq__returnc                 C   s    dt | j d t | j d S )N{z, })strr&   r'   r1   r!   r!   r"   __str__n   s    z_TypeFormatCombination.__str__)r   r%   )
__name__
__module____qualname__r+   r,   r.   r2   r4   r8   r9   r!   r!   r!   r"   r$   U   s    

r$   c                	   @   sx   e Zd Z	ddededee fddZddee deded	dfd
dZdee d	dfddZdd Z	dd Z
dd ZdS )AutoTuneCombinationNio_typesr'   r(   c                 C   s   |durSdd | dD | _|du rd}dd | dD | _t| jdkr3t| jt| jks3J t| jt| jkrRt| jdksFJ | jd gt| j | _ng | _g | _g | _|| _dS )	a  
        Construct a set of supported type/format combinations of a plugin's I/O.

        Any custom *tactic* s per each such type/format combination can also be advertised. A tactic is simply another way to
        calculate the output of a plugin for the same type/format combination of the I/O (e.g. if there are multiple kernels available).

        Args:
            io_types (str, optional): A string representation of a type combination.

                Valid format is "type0,type1,...,type#io" where 'type' is of the form "TYPE0[sep]TYPE1[sep]...".

                TYPE is a valid string representation of a `trt.DataType`. These include "FP32" for trt.float32, "FP16" for trt.float16. The string representation of other data types is the same as their name in the trt.DataType enum.


                [sep] is a valid separator, which is either '|' or '*'. Only one of these separators can appear in a given `io_types`.

                (1). '|' indicates a dependent combination: the dependence of the type of one I/O to another I/O. e.g. "FP32|FP16,FP32|FP16" indicates the IO can only be both FP32 or both FP16.

                (2). '*' indicates an independent combination. e.g. "FP32*FP16,FP32|FP16,FP32|FP16" indicates that the first input is independently either FP32 or FP16 regardless of the rest of the IO.

            layouts (str, optional): A string representation of a format combination.

                Valid format is "format0,format1,...,format#io" where 'format' is of the form "FORMAT0[sep]FORMAT1[sep]...".

                FORMAT is a valid string representation of a `trt.TensorFormat`. These are string versions for the enum values of `trt.TensorFormat`. e.g. "LINEAR" for `trt.TensorFormat.LINEAR`.

                [sep] is a valid separator, which is either '|' or '*'. The rules are the same as for `io_types`.

            tactics (Iterable[int], optional): Custom tactics for this type/format combination. Each custom tactic must be a positive integer. Defaults to default tactic (0).

        .. code-block:: python
            :linenos:
            :caption: For a plugin with 3 I/Os, I/O indices 0 and 1 are dependently either FP32/FP16 and index 2 is independently FP32/FP16.

            @trtp.autotune("my::plugin")
            def autotune(inp0: trtp.TensorDesc, inp1: trtp.TensorDesc, outputs: Tuple[trtp.TensorDesc]) -> List[trtp.AutoTuneCombination]:
                # The following would result in the following type combinations:
                # [FP32, FP32, FP32], [FP16, FP16, FP32], [FP32, FP32, FP16], [FP16, FP16, FP16]
                return [trtp.AutoTuneCombination("FP32|FP16, FP32|FP16, FP32|FP16", "LINEAR", [1, 2])]

        .. code-block:: python
            :linenos:
            :caption: For a plugin with 2 I/Os, the input/output supports either LINEAR or HWC format for FP32 and LINEAR format for FP16.

            @trtp.autotune("my::plugin")
            def autotune(inp0: trtp.TensorDesc, outputs: Tuple[trtp.TensorDesc]) -> List[trtp.AutoTuneCombination]:
                # Even though (FP16, HWC) is not a valid combination (see next example), TRT should intelligently reject those
                # and pass the following combinations to the impl function:
                # [{FP32, FP32}, {LINEAR, LINEAR}], [{FP32, FP32}, {HWC, LINEAR}], [{FP16, FP32}, {LINEAR, LINEAR}]
                return [trtp.AutoTuneCombination("FP32*FP16, FP32", "LINEAR*HWC, LINEAR", [1, 2])]

        .. code-block:: python
            :linenos:
            :caption: For a plugin with 2 I/Os, the input/output supports either LINEAR or HWC format for FP32 and LINEAR format for FP16 (second method).

            @trtp.autotune("my::plugin")
            def autotune(inp0: trtp.TensorDesc, outputs: Tuple[trtp.TensorDesc]) -> List[trtp.AutoTuneCombination]:
                # We can use two AutoTuneCombination objects to avoid communicating illegal combinations
                return [trtp.AutoTuneCombination("FP32*FP16, FP32", "LINEAR, LINEAR", [1, 2]), trtp.AutoTuneCombination("FP32, FP32", "HWC, LINEAR", [1, 2])]
        Nc                 S      g | ]}|  qS r!   strip.0sr!   r!   r"   
<listcomp>       z0AutoTuneCombination.__init__.<locals>.<listcomp>,LINEARc                 S   r?   r!   r@   rB   r!   r!   r"   rE      rF   r   r   )r
   r>   r'   r   r   _tactics)r)   r>   r'   r(   r!   r!   r"   r+   t   s   @
zAutoTuneCombination.__init__rH   posr5   c                 C   s   t |t| jkr9| jdgt |d t| j   | jdgt |d t| j   t| jt| jks9J |D ]*}| j| durLtd| d| j| dur[td| d|| j|< || j|< q;dS )a  
        Specify I/O types and formats for a specified set of I/O indices.

        Args:
            pos (Iterable[int]): I/O indices. Input indices are [0, 1, ..., #inputs - 1] and output indices are [#inputs, #inputs + 1, ..., #inputs + #outputs - 1].
            io_types (str): Data types for these I/O indices.
            layouts (str, optional): Tensor format(s) for these I/O indices. Defaults to "LINEAR".
        Raises:
            ValueError: If types or layouts for any of these I/O indices is already specified.

        .. code-block:: python
            :linenos:
            :caption: For a plugin with 3 I/Os, I/O indices 0 and 1 are dependently either FP32/FP16 and index 2 is independently FP32/FP16.

            @trtp.autotune("my::plugin")
            def autotune(inp0: trtp.TensorDesc, inp1: trtp.TensorDesc, outputs: Tuple[trtp.TensorDesc]) -> List[trtp.AutoTuneCombination]:
                c = trtp.AutoTuneCombination()
                c.pos([0, 1], "FP32|FP16", "LINEAR")
                c.pos(2, "FP32*FP16") # Omitting format is the same as declaring it to be LINEAR.
                c.tactics([1, 2])
                return [c]
        Nr   zType(s) for position z already specifiedzLayout(s) for position )r   r   r>   r   r'   r   )r)   rJ   r>   r'   pr!   r!   r"   rJ      s   $$
zAutoTuneCombination.posc                 C   s
   || _ dS )z
        Specify custom tactics for this type/format combination

        Args:
            tactics (Iterable[int]): Custom tactics. These must be positive integers.
        N)rI   )r)   r(   r!   r!   r"   r(      s   
zAutoTuneCombination.tacticsc                 C   st   g | _ t| j}t| j}|D ](}|D ]#}tt| j}dd |D |_dd |D |_| j|_| j 	| qqd S )Nc                 S   s   g | ]}t |qS r!   r   )rC   ttr!   r!   r"   rE      rF   z>AutoTuneCombination._generate_combinations.<locals>.<listcomp>c                 S   s   g | ]}t tj|qS r!   )getattrtrtTensorFormat)rC   ffr!   r!   r"   rE      s    )
r   r#   r>   r'   r$   r   r&   rI   r(   r   )r)   type_combinationslayout_combinationstlr   r!   r!   r"   _generate_combinations   s   

z*AutoTuneCombination._generate_combinationsc                 C   s   |    | jS r%   )rU   r   r1   r!   r!   r"   _get_combinations  s   z%AutoTuneCombination._get_combinationsc                 C   sL   t t| jD ]}| j| j| t|kr#| j| j| |jkr# dS qdS )NTF)r   r   r   r&   r   r'   name)r)   rJ   typelayoutr   r!   r!   r"   _check  s   zAutoTuneCombination._check)NNN)rH   )r:   r;   r<   r8   r   intr+   rJ   r(   rU   rV   rZ   r!   r!   r!   r"   r=   r   s    
 S$	r=   )r   tensorrtrN   typingr   r   r   _utilsr   _exportr   r#   r$   r=   r!   r!   r!   r"   <module>   s   7