o
    vZh                     @  s   d dl mZ 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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 )    )annotationsN)defaultdict
namedtuple)	dataclass)IntEnum)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   H/var/www/auris/lib/python3.10/site-packages/torchgen/executorch/model.pyr      s    r   ETParsedYamlZnative_functionskernel_indexT)frozenc                   @  s0   e Zd ZU ded< ded< ded< d
ddZd	S )ETKernelKeyOpArgMetastrarg_namedtypeztuple[int, ...]	dim_orderreturnc                 C  s4   t | j j}t| jdd dd}| d| S )Nr     ;)r   r%   valuer#   r&   replace)selfZ	dtype_strZdim_strr   r   r   to_native_string/   s   z%ETKernelKeyOpArgMeta.to_native_stringNr'   r#   )r   r   r   __annotations__r/   r   r   r   r   r"   (   s
   
 r"   c                   @  sJ   e Zd ZU dZded< dZded< eZded< edddZ	dddZ
dS )ETKernelKeyr   z tuple[ETKernelKeyOpArgMeta, ...]arg_metaFbooldefaultintversionargsdict[str, tuple[str, str]]type_alias_mapdict[str, list[str]]dim_order_alias_mapdict[str, list[int]]r'   list[ETKernelKey]c                   s  dd |  D }g }t }|  D ] \}}| v s"J dt| ||v s-J d| || q fdd|D }dd ttj| D }i }	|D ]:}
g }|   D ](\}}|
|d  }||d	  }||t|f }|	vrut	| |	|< |
|	|  qT|
tt| qL|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     g | ]}t |qS r   )r6   ).0aliasr   r   r   
<listcomp>U       z8ETKernelKey.gen_from_yaml.<locals>.<dictcomp>.<listcomp>r   )r@   kvr   r   r   
<dictcomp>T   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%   rA   r   r   rB   h   rC   z8ETKernelKey.gen_from_yaml.<locals>.<listcomp>.<listcomp>r   )r@   r:   rG   r   rB   g   s    z-ETKernelKey.gen_from_yaml.<locals>.<listcomp>c                 S  r?   r   )dict)r@   permutationr   r   r   rB   k   s    r   r   )itemssetvaluesr#   addlist	itertoolsproducttupler"   appendr2   )r8   r:   r<   Zkernel_keysZdtype_alias_usedZ
type_aliasr&   Zalias_dtypesZalias_permutationsZop_arg_cacherJ   arg_listr$   Zarg_specr%   	cache_keyr   rH   r   gen_from_yaml?   s@   

zETKernelKey.gen_from_yamlr#   c                 C  s0   | j rdS dtt d ddd | jD  S )Nr5   rE   /|c                 S  s   g | ]}|  qS r   )r/   )r@   argr   r   r   rB      rC   z0ETKernelKey.to_native_string.<locals>.<listcomp>)r5   r#   KERNEL_KEY_VERSIONjoinr3   )r.   r   r   r   r/      s   zETKernelKey.to_native_stringN)r8   r9   r:   r;   r<   r=   r'   r>   r0   )r   r   r   r3   r1   r5   rZ   r7   staticmethodrV   r/   r   r   r   r   r2   5   s   
 @r2   c                   @  sh   e Zd ZU ded< dddZdd
dZed ddZed!ddZd!ddZ	d"ddZ
ed#ddZdS )$ETKernelIndex6dict[OperatorName, dict[ETKernelKey, BackendMetadata]]indexg%NativeFunction | NativeFunctionsGroupr'   r4   c                 C  s   |  |}|d uS N)get_kernels)r.   r`   mr   r   r   has_kernels   s   
zETKernelIndex.has_kernels"dict[ETKernelKey, BackendMetadata]c                 C  sJ   t |tr|}nt |tr|j}nt| |jj| jvri S | j|jj S rb   )
isinstancer
   r   Z
functionalr   funcnamer_   )r.   r`   fr   r   r   rc      s   

zETKernelIndex.get_kernelsr    backend_indices6dict[DispatchKey, dict[OperatorName, BackendMetadata]]Nonec                 C  sV   |D ]&}|| }|  D ]\}}|| v r|| | tdd< qtdd|i| |< qqd S )NTr5   )rK   r2   )r    rk   Zdkr_   opZbackend_metadatar   r   r   grow_from_backend_indices   s   z'ETKernelIndex.grow_from_backend_indicesc                 C  s   t t}t||  t|S rb   )r   rI   r]   rp   )rk   r    r   r   r   from_backend_indices   s   z"ETKernelIndex.from_backend_indicesc                 C  s   t | j| | S rb   )r]   rp   r_   )r.   rk   r   r   r   grow   s   zETKernelIndex.growr   c              	   C  sr   i }| j D ])}| j | }t| dksJ 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 Trn   r*   F)ZkernelZ
structuredZcpp_namespace)Zdispatch_keyZuse_out_as_primaryZdevice_guardZexternalr_   )	r_   lenrM   getr2   r   r   r	   ZCPU)r.   r_   ro   Zkernel_dictr   r   r   _to_backend_index   s"   


zETKernelIndex._to_backend_indexindex_aindex_bc                 C  sJ   t t| j }|j D ]\}}| D ]
\}}||| |< qqt|S rb   )r   rI   r_   copyrK   r]   )rv   rw   combinedro   entrykeymetadatar   r   r   merge_indices   s   zETKernelIndex.merge_indicesN)r`   ra   r'   r4   )r`   ra   r'   rf   )r    r^   rk   rl   r'   rm   )rk   rl   r'   r]   )r'   r   )rv   r]   rw   r]   r'   r]   )r   r   r   r1   re   rc   r\   rp   rq   rr   ru   r}   r   r   r   r   r]      s   
 


	
r]   )
__future__r   rP   collectionsr   r   dataclassesr   enumr   Ztorchgen.modelr   r   r	   r
   r   r   Ztorchgen.utilsr   rZ   r   r   r"   r2   r]   r   r   r   r   <module>   s     U