U
    1jg/                     @   s8  d dl mZmZ d dlmZmZmZmZmZm	Z	 d dl
Z
d dlmZmZ d dlmZmZ d dlmZmZmZmZmZ d dlmZ edd	d
gZdddgZeeef eeef dddZeedddZeeeeeef f dddZdeeeee  e e	ee eeeeef f f dddZ!eddddZ"dS )    )defaultdict
namedtuple)AnyDictListOptionalSetTupleN)ETKernelIndexETKernelKey)
LineLoaderparse_native_yaml)BackendMetadataDispatchKeyFunctionSchemaNativeFunctionOperatorName)NamespaceHelperETParsedYamlnative_functionsZet_kernel_indiceskernels
type_aliasdim_order_alias)eireturnc                 C   s  |   }|dd }dkr i S |di }|di }|dd i }|D ]}|d}|dk	rl|d |d}tj|dd	}	|	jd
d}
t|	jd|
d d}|dkrtdddgnt	|||}|D ]0}||kst
dt| d t| |||< qqL|S )a3  Given a loaded yaml representing kernel assignment information, extract the
    mapping from `kernel keys` to `BackendMetadata` (the latter representing the kernel instance)

    Args:
        ei: Dict keys {kernels, type_alias, dim_order_alias}
            See ETKernelKey for description of arguments
    r   Nr   r   Z__line__arg_metakernel_name   )	max_levelat)defaultFz::native)kernelZ
structuredZcpp_namespace TzDuplicate kernel key:  )copypopgetr   from_namespaced_entityZget_cpp_namespacer   entity_namer   Zgen_from_yamlAssertionErrorstr)r   er   r   r   Zkernel_mappingentryr   r   namespace_helperZkernel_namespaceZbackend_metadataZkernel_keysZ
kernel_keyr"   r"   =/tmp/pip-unpacked-wheel-ttp2cnii/torchgen/executorch/parse.pyparse_from_yaml   s@    


 
r/   )esr   c                 C   s   i }| D ]~}|  }|d}t|ts6td| tj|dd}t|j	j
}||ksjtd| dtt| }dkr|||< qt|S )zGiven a loaded yaml representing a list of operators, for each op extract the mapping
    of `kernel keys` to `BackendMetadata` (the latter representing the kernel instance
    that should be used by the kernel key).
    funcnot a str:    Znamespaced_entityr   zDuplicate func found in yaml: z alreadyr   )r$   r%   
isinstancer*   r)   r   r'   r   parser(   namelenr/   r
   )r0   indicesr   r+   funcsr-   opnameindexr"   r"   r.   parse_et_yaml_structJ   s    
 
r=   c                 C   s|   t t}| D ]j}|d}t|ts2td| tj|dd}t	|j
j}tD ]"}|| }dk	rR||| |< qRq|S )zGiven a loaded yaml representing a list of operators, extract the
    kernel key related fields indexed by the operator name.
    r1   r2   r3   r4   N)r   dictr&   r5   r*   r)   r   r'   r   r6   r(   r7   	ET_FIELDS)r0   fieldsr   r:   r-   r;   fieldvaluer"   r"   r.   extract_kernel_fieldsb   s    
 rC   F)pathtags_yaml_pathignore_keysskip_native_fns_genr   c              	   C   sN   t | }tj|td}W 5 Q R X t|}t| t| ||||d}|j|fS )zParse native_functions.yaml into NativeFunctions and an Operator Indexed Dict
    of fields to persist from native_functions.yaml to functions.yaml
    )Loader)rG   Zloaded_yaml)openyamlloadr   rC   strip_et_fieldsr   r   )rD   rE   rF   rG   fr0   Z	et_kernelZnative_yamlr"   r"   r.   parse_et_yamlv   s    	
rN   c                 C   s$   | D ]}t D ]}||d qqdS )zGiven a loaded yaml representing a list of operators,
    remove ET specific fields from every entries for BC compatibility
    N)r?   r%   )r0   r,   rA   r"   r"   r.   rL      s    rL   )NF)#collectionsr   r   typingr   r   r   r   r   r	   rJ   Ztorchgen.executorch.modelr
   r   Ztorchgen.genr   r   Ztorchgen.modelr   r   r   r   r   Ztorchgen.utilsr   r   r?   r*   objectr/   r=   rC   boolrN   rL   r"   r"   r"   r.   <module>   s*    
 1   
