U
    9%e                     @   s   d dl Z d dlmZmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZ d dlmZmZmZmZmZmZ d dlmZ dZG d	d
 d
eZedddgZeddG dd dZeddG dd dZeddG dd dZdS )    N)defaultdict
namedtuple)	dataclass)IntEnum)DictListTupleUnion)BackendIndexBackendMetadataDispatchKeyNativeFunctionNativeFunctionsGroupOperatorName)assert_never   c                   @   s,   e Zd ZdZdZdZdZdZdZdZ	dZ
d	S )

ScalarTyper   r                     N)__name__
__module____qualname__ZByteCharZShortZIntLongFloatDoubleZBool r    r    X/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torchgen/executorch/model.pyr      s   r   ETParsedYamlZnative_functionskernel_indexT)frozenc                   @   s<   e Zd ZU eed< eed< eedf ed< edddZdS )	ETKernelKeyOpArgMetaarg_namedtype.	dim_orderreturnc                 C   s4   t | j j}t| jdd dd}| d| S )Nr     ;)r   r'   valuestrr(   replace)selfZ	dtype_strZdim_strr    r    r!   to_native_string-   s    z%ETKernelKeyOpArgMeta.to_native_stringN)r   r   r   r0   __annotations__r   intr3   r    r    r    r!   r%   &   s   
r%   c                   @   s   e Zd ZU dZeedf ed< dZeed< e	Z
eed< eeeeeef f eeee f eeee f ed  ddd	Zed
ddZdS )ETKernelKeyr    .arg_metaFdefaultversion)argstype_alias_mapdim_order_alias_mapr*   c                    s  dd |  D }g }t }|  D ]B\}}| ksDtdt| ||ks\tdt| || q$ fdd|D }dd ttj| D }i }	|D ]t}
g }|   D ]P\}}|
|d  }||d	  }||t	|f }|	krt
| |	|< ||	|  q|tt	| q|S )
a  Generate ETKernelKeys from arg kernel specs
        Multiple ETKernelKeys are returned due to dtype permutations from utilizing
        type_alias_map (actualizing each potential type permutation as a KernelKey)

        Args:
            args: Mapping from argument name to kernel specs
                Kernel specs are a tuple of (dtype, dim_order).
                Currently tuple entries must be aliased via the alias map arguments
            type_alias_map: Mapping from type alias to potential type enums
                i.e { T0 : [Double, Int] } means T0 can be either Double or Int
                Used for lookup by args
            dim_order_alias_map: Mapping from alias to a list of dimension orders
                Used for lookup by args
        c                 S   s    i | ]\}}|d d |D qS )c                 S   s   g | ]}t |qS r    )r5   ).0aliasr    r    r!   
<listcomp>S   s     z8ETKernelKey.gen_from_yaml.<locals>.<dictcomp>.<listcomp>r    )r=   kvr    r    r!   
<dictcomp>R   s     z-ETKernelKey.gen_from_yaml.<locals>.<dictcomp>zUndefined type alias: zUndefined dim_order alias: c                    s"   g | ]  fd d  D qS )c                    s   g | ]} |fqS r    r    )r=   r'   r>   r    r!   r?   f   s     z8ETKernelKey.gen_from_yaml.<locals>.<listcomp>.<listcomp>r    )r=   r;   rC   r!   r?   e   s   z-ETKernelKey.gen_from_yaml.<locals>.<listcomp>c                 S   s   g | ]}t |qS r    )dict)r=   permutationr    r    r!   r?   i   s    r   r   )itemssetvaluesAssertionErrorr0   addlist	itertoolsproducttupler%   appendr6   )r:   r;   r<   Zkernel_keysZdtype_alias_usedZ
type_aliasr(   Zalias_dtypesZalias_permutationsZop_arg_cacherF   arg_listr&   Zarg_specr'   	cache_keyr    rD   r!   gen_from_yaml=   sB    

zETKernelKey.gen_from_yamlr)   c                 C   s0   | j r
dS dtt d ddd | jD  S )Nr8   rA   /|c                 S   s   g | ]}|  qS r    )r3   )r=   argr    r    r!   r?      s     z0ETKernelKey.to_native_string.<locals>.<listcomp>)r8   r0   KERNEL_KEY_VERSIONjoinr7   )r2   r    r    r!   r3   ~   s    zETKernelKey.to_native_stringN)r   r   r   r7   r   r%   r4   r8   boolrW   r9   r5   staticmethodr   r0   r   rS   r3   r    r    r    r!   r6   3   s   
@r6   c                   @   s   e Zd ZU eeeeef f ed< ee	e
f edddZee	e
f eeef dddZeeeeeef f eeeeef f ddd	d
Zeeeeeef f d dddZeeeeef f d dddZedddZed d d dddZdS )ETKernelIndexindex)gr*   c                 C   s   |  |}|d k	S N)get_kernels)r2   r]   mr    r    r!   has_kernels   s    
zETKernelIndex.has_kernelsc                 C   sJ   t |tr|}nt |tr"|j}nt| |jj| jkr<i S | j|jj S r^   )
isinstancer   r   Z
functionalr   funcnamer\   )r2   r]   fr    r    r!   r_      s    

zETKernelIndex.get_kernelsN)r#   backend_indicesr*   c                 C   sV   |D ]L}|| }|  D ]6\}}|| kr<|| | tdd< qtdd|i| |< qqd S )NTr8   )rG   r6   )r#   rf   Zdkr\   opZbackend_metadatar    r    r!   grow_from_backend_indices   s    z'ETKernelIndex.grow_from_backend_indices)rf   r*   c                 C   s   t t}t||  t|S r^   )r   rE   r[   ri   )rf   r#   r    r    r!   from_backend_indices   s    z"ETKernelIndex.from_backend_indicesc                 C   s   t | j| | S r^   )r[   ri   r\   )r2   rf   r    r    r!   grow   s    zETKernelIndex.growr)   c              	   C   sr   i }| j D ]R}| j | }t| dks<td| d| |tddtdddd||< q
ttj	ddd|d	S )
zp
        WARNING: this will be deprecated once all the codegen places know how to handle ETKernelIndex.
        r   z4Can't convert ETKernelIndex to BackendIndex because z  has more than one kernels. Got Trg   r-   F)ZkernelZ
structuredZcpp_namespace)Zdispatch_keyZuse_out_as_primaryZdevice_guardZexternalr\   )
r\   lenrI   rJ   getr6   r   r
   r   ZCPU)r2   r\   rh   Zkernel_dictr    r    r!   _to_backend_index   s$    


zETKernelIndex._to_backend_index)index_aindex_br*   c                 C   sJ   t t| j }|j D ]&\}}| D ]\}}||| |< q*qt|S r^   )r   rE   r\   copyrG   r[   )ro   rp   combinedrh   entrykeymetadatar    r    r!   merge_indices   s
    zETKernelIndex.merge_indices)r   r   r   r   r   r6   r   r4   r	   r   r   rY   ra   r_   rZ   r   ri   rj   rk   r
   rn   rv   r    r    r    r!   r[      s.   



 r[   )rM   collectionsr   r   dataclassesr   enumr   typingr   r   r   r	   Ztorchgen.modelr
   r   r   r   r   r   Ztorchgen.utilsr   rW   r   r"   r%   r6   r[   r    r    r    r!   <module>   s    U