U
    •Ê¹j  ã                   @   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/bwave/python/venv/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rìt
|Ž |	|< | |	| ¡ 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