o
    i/                     @   sJ  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g dZdeeef deeef fddZdedefddZdedeeeeef f fddZ		ddededeee  de de	ee eeeeef f f f
ddZ!deddf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u ri S |di }|di }|dd i }|D ]Y}|d}|dur6|d |d}tj|dd	}	|	jd
d}
t|	jd|
d d}|du r]tdddgnt	|||}|D ]}||vszJ dt
| d t
| |||< qfq&|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)ZkernelZ
structuredZcpp_namespace TzDuplicate kernel key:  )copypopgetr   from_namespaced_entityZget_cpp_namespacer   entity_namer   Zgen_from_yamlstr)r   er   r   r   Zkernel_mappingentryr   r   namespace_helperZkernel_namespaceZbackend_metadataZkernel_keysZ
kernel_keyr!   r!   h/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/torchgen/executorch/parse.pyparse_from_yaml   s@   




r-   esc                 C   s   i }| D ]?}|  }|d}t|tsJ d| tj|dd}t|jj	}||vs5J d| dt
t| }dkrC|||< 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   parser'   namelenr-   r
   )r.   indicesr   r)   funcsr+   opnameindexr!   r!   r,   parse_et_yaml_structJ   s   
r;   c                 C   s|   t t}| D ]5}|d}t|tsJ d| tj|dd}t|j	j
}tD ]}|| }dur:||| |< q)q|S )zGiven a loaded yaml representing a list of operators, extract the
    kernel key related fields indexed by the operator name.
    r/   r0   r1   r2   N)r   dictr%   r3   r(   r   r&   r   r4   r'   r5   	ET_FIELDS)r.   fieldsr   r8   r+   r9   fieldvaluer!   r!   r,   extract_kernel_fieldsb   s   
rA   Fpathtags_yaml_pathignore_keysskip_native_fns_genc                 C   sb   t | }tj|td}W d   n1 sw   Y  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
    )LoaderN)rE   Zloaded_yaml)openyamlloadr   rA   strip_et_fieldsr   r   )rB   rC   rD   rE   fr.   Z	et_kernelZnative_yamlr!   r!   r,   parse_et_yamlv   s   
	
rL   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$   )r.   r*   r?   r!   r!   r,   rJ      s
   rJ   )NF)#collectionsr   r   typingr   r   r   r   r   r	   rH   Ztorchgen.executorch.modelr
   r   Ztorchgen.genr   r   Ztorchgen.modelr   r   r   r   r   Ztorchgen.utilsr   r   r=   r(   objectr-   r;   rA   boolrL   rJ   r!   r!   r!   r,   <module>   s4     "1"

