o
    8} i                     @  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__ByteCharShortIntLongFloatDoubleBool r!   r!   ;/home/app/PyTorch/test/pytorch/torchgen/executorch/model.pyr      s    r   ETParsedYaml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__r4   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!   )r;   ).0aliasr!   r!   r"   
<listcomp>U       z8ETKernelKey.gen_from_yaml.<locals>.<dictcomp>.<listcomp>r!   )rE   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!   )rE   r*   rF   r!   r"   rG   h   rH   z8ETKernelKey.gen_from_yaml.<locals>.<listcomp>.<listcomp>r!   )rE   r?   rL   r"   rG   g   s    z-ETKernelKey.gen_from_yaml.<locals>.<listcomp>c                 S  rD   r!   )dict)rE   permutationr!   r!   r"   rG   k   s    r   r   )itemssetvaluesr(   addlist	itertoolsproducttupler'   appendr7   )r=   r?   rA   Zkernel_keysZdtype_alias_usedZ
type_aliasr+   Zalias_dtypesZalias_permutationsZop_arg_cacherO   arg_listr)   Zarg_specr*   Z	cache_keyr!   rM   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 )Nr:   rJ   /|c                 S  s   g | ]}|  qS r!   )r4   )rE   argr!   r!   r"   rG      rH   z0ETKernelKey.to_native_string.<locals>.<listcomp>)r:   r(   KERNEL_KEY_VERSIONjoinr8   )r3   r!   r!   r"   r4      s   zETKernelKey.to_native_stringN)r=   r>   r?   r@   rA   rB   r,   rC   r5   )r   r   r   r8   r6   r:   r^   r<   staticmethodrZ   r4   r!   r!   r!   r"   r7   5   s   
 @r7   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,   r9   c                 C  s   |  |}|d uS N)get_kernels)r3   rd   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 rf   )
isinstancer
   r   
functionalr   funcnamerc   )r3   rd   fr!   r!   r"   rg      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r:   )rP   r7   )r%   rp   dkrc   opbackend_metadatar!   r!   r"   grow_from_backend_indices   s   z'ETKernelIndex.grow_from_backend_indicesc                 C  s   t t}t||  t|S rf   )r   rN   ra   rw   )rp   r%   r!   r!   r"   from_backend_indices   s   z"ETKernelIndex.from_backend_indicesc                 C  s   t | j| | S rf   )ra   rw   rc   )r3   rp   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 Trs   r/   F)kernel
structuredcpp_namespace)dispatch_keyuse_out_as_primarydevice_guardexternalrc   )	rc   lenrR   getr7   r   r   r	   CPU)r3   rc   ru   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 rf   )r   rN   rc   copyrP   ra   )r   r   Zcombinedru   entrykeymetadatar!   r!   r"   merge_indices   s   zETKernelIndex.merge_indicesN)rd   re   r,   r9   )rd   re   r,   rj   )r%   rb   rp   rq   r,   rr   )rp   rq   r,   ra   )r,   r   )r   ra   r   ra   r,   ra   )r   r   r   r6   ri   rg   r`   rw   rx   ry   r   r   r!   r!   r!   r"   ra      s   
 


	
ra   )
__future__r   rU   collectionsr   r   dataclassesr   enumr   torchgen.modelr   r   r	   r
   r   r   torchgen.utilsr   r^   r   r#   r'   r7   ra   r!   r!   r!   r"   <module>   s     U