o
    8} i.                     @  s   d dl mZ d dlmZmZ d dl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)ddZd*ddZd+ddZ		d,d-d$d%Zd.d'd(ZdS )/    )annotations)defaultdict
namedtuple)AnyN)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dict[str, object]return"dict[ETKernelKey, BackendMetadata]c                 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   __line__arg_metakernel_name   )	max_levelat)defaultFz::native)kernel
structuredcpp_namespace TzDuplicate kernel key:  )copypopgetr   from_namespaced_entityget_cpp_namespacer
   entity_namer   gen_from_yamlstr)r   er   r   r   kernel_mappingentryr   r   namespace_helperkernel_namespacebackend_metadatakernel_keys
kernel_keyr#   r#   ;/home/app/PyTorch/test/pytorch/torchgen/executorch/parse.pyparse_from_yaml   s@   




r6   esobjectr   c                 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:    namespaced_entityr   zDuplicate func found in yaml: z alreadyr   )r%   r&   
isinstancer,   r   r(   r   parser*   namelenr6   r   )r7   indicesr   r-   funcsr0   opnameindexr#   r#   r5   parse_et_yaml_structL   s   
rF   "dict[OperatorName, dict[str, Any]]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.
    r9   r:   r;   r<   N)r   dictr'   r>   r,   r   r(   r   r?   r*   r@   	ET_FIELDS)r7   fieldsr   rC   r0   rD   fieldvaluer#   r#   r5   extract_kernel_fieldsd   s   
rM   Fpathr,   tags_yaml_pathignore_keysset[DispatchKey] | Noneskip_native_fns_genbool?tuple[list[NativeFunction], dict[OperatorName, dict[str, Any]]]c                 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)rR   loaded_yaml)openyamlloadr   rM   strip_et_fieldsr	   r   )rN   rO   rP   rR   fr7   Z	et_kernelnative_yamlr#   r#   r5   parse_et_yamlx   s   
	
r]   Nonec                 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)rI   r&   )r7   r/   rK   r#   r#   r5   rZ      s
   rZ   )r   r   r   r   )r7   r8   r   r   )r7   r8   r   rG   )NF)
rN   r,   rO   r,   rP   rQ   rR   rS   r   rT   )r7   r8   r   r^   )
__future__r   collectionsr   r   typingr   rX   torchgen.executorch.modelr   r   torchgen.genr   r	   torchgen.modelr
   r   r   r   r   torchgen.utilsr   r   rI   r6   rF   rM   r]   rZ   r#   r#   r#   r5   <module>   s"    

1
