U
    8‹Ñh  ã                   @   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IntZLongZFloatÚDoubleZBool© r   r   úG/tmp/user/2202/pip-unpacked-wheel-bf9o3bgk/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   r.   Ú__annotations__r   Úintr1   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   )r3   )Ú.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;   ©r9   rA   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ÚAssertionErrorr.   ÚaddÚlistÚ	itertoolsÚproductÚtupler#   Úappendr4   )r8   r9   r:   Zkernel_keysZdtype_alias_usedZ
type_aliasr&   Zalias_dtypesZalias_permutationsZop_arg_cacherD   Úarg_listr$   Zarg_specr%   Ú	cache_keyr   rB   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 )Nr6   r?   ú/ú|c                 S   s   g | ]}|  ¡ ‘qS r   )r1   )r;   Úargr   r   r   r=   …   s     z0ETKernelKey.to_native_string.<locals>.<listcomp>)r6   r.   ÚKERNEL_KEY_VERSIONÚjoinr5   )r0   r   r   r   r1   ~   s    ÿþýÿzETKernelKey.to_native_stringN)r   r   r   r5   r   r#   r2   r6   ÚboolrU   r7   r3   Ústaticmethodr   r.   r   rQ   r1   r   r   r   r   r4   3   s   
ü@r4   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)r0   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ÚnamerZ   )r0   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©r6   )rE   r4   )r!   rd   ZdkrZ   ÚopZbackend_metadatar   r   r   Úgrow_from_backend_indicesž   s    z'ETKernelIndex.grow_from_backend_indices)rd   r(   c                 C   s   t tƒ}t || ¡ t|ƒS r\   )r   rC   rY   rg   )rd   r!   r   r   r   Úfrom_backend_indices«   s    þz"ETKernelIndex.from_backend_indicesc                 C   s   t  | j|¡ | S r\   )rY   rg   rZ   )r0   rd   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 Tre   r+   F)ÚkernelZ
structuredZcpp_namespace)Zdispatch_keyZuse_out_as_primaryZdevice_guardZexternalrZ   )
rZ   ÚlenrG   rH   Úgetr4   r   r
   r   ZCPU)r0   rZ   rf   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   rC   rZ   ÚcopyrE   rY   )rn   ro   Zcombinedrf   ÚentryÚkeyÚmetadatar   r   r   Úmerge_indicesÒ   s
    zETKernelIndex.merge_indices)r   r   r   r   r   r4   r   r2   r	   r   r   rW   r_   r]   rX   r   rg   rh   ri   r
   rm   rt   r   r   r   r   rY   ‰   s.   


þýþ
þ þrY   )rK   Úcollectionsr   r   Zdataclassesr   Úenumr   Útypingr   r   r   r	   Ztorchgen.modelr
   r   r   r   r   r   Ztorchgen.utilsr   rU   r   r    r#   r4   rY   r   r   r   r   Ú<module>   s    U