o
    h.                     @  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_functions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$   m/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torchgen/executorch/parse.pyparse_from_yaml   s@   




r7   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lenr7   r   )r8   indicesr   r.   funcsr1   opnameindexr$   r$   r6   parse_et_yaml_structL   s   
rG   "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.
    r:   r;   r<   r=   N)r   dictr(   r?   r-   r   r)   r   r@   r+   rA   	ET_FIELDS)r8   fieldsr   rD   r1   rE   fieldvaluer$   r$   r6   extract_kernel_fieldsd   s   
rN   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)rS   loaded_yaml)openyamlloadr   rN   strip_et_fieldsr	   r   )rO   rP   rQ   rS   fr8   	et_kernelnative_yamlr$   r$   r6   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)rJ   r'   )r8   r0   rL   r$   r$   r6   r[      s
   r[   )r   r   r   r   )r8   r9   r   r   )r8   r9   r   rH   )NF)
rO   r-   rP   r-   rQ   rR   rS   rT   r   rU   )r8   r9   r   r`   )
__future__r   collectionsr   r   typingr   rY   torchgen.executorch.modelr   r   torchgen.genr   r	   torchgen.modelr
   r   r   r   r   torchgen.utilsr   r   rJ   r7   rG   rN   r_   r[   r$   r$   r$   r6   <module>   s"    

1
