
    i                        S SK r S SKrS SKrS SKJr  S SKJrJrJrJ	r	  S SK
r
 S SK
Jr   " S S\
R                  5      rS\S	\	\   S
\4S jrS\\   SS4S jr\S:X  a  \" \R*                  SS 5        gg! \ a	    S SK
Jr   NZf = f)    N)defaultdict)AnyDictListOptional)CSafeLoader)
SafeLoaderc                   ,   ^  \ rS rSrSU 4S jjrSrU =r$ )BlankLineDumper   c                 t   > [         TU ]  U5        [        U R                  5      S:X  a  [         TU ]  5         g g )N   )superwrite_line_breaklenindents)selfdata	__class__s     b/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/codegen/tools/merge_yaml.pyr    BlankLineDumper.write_line_break   s1     &t||!G$& "     )N)__name__
__module____qualname____firstlineno__r   __static_attributes____classcell__)r   s   @r   r   r      s    ' 'r   r   functions_yaml_pathfallback_yaml_path
output_dirc           
         [         R                  R                  US5      nS[        S[        4S jn[        U 5       n[        R                  " U[        S9n[        [        5      nU H  nXU" U5      '   M     S S S 5        Ubn  [         R                  R                  U5      (       aJ  [        U5       n[        R                  " U[        S9n	U	 H  nU" U5      n
U
W;  d  M  XU
'   M     S S S 5        [        US5       n[        R                  " [        WR                  5       5      U[        SSSS	9  S S S 5        g ! , (       d  f       N= f! , (       d  f       Ni= f! , (       d  f       g = f)
Nzmerged.yamlfuncreturnc                 `    SU ;   a  U S   OU S   R                  S5      S   nSU;  a  SU-   nU$ )a  get the canonical name of an operator
"op" and "func" are two keywords we are supporting for yaml files.
To give an example:
- op: add.Tensor # mostly used for binding ATen ops to kernels
- func: add.Tensor(Tensor self, Tensor other, Scalar alpha) # mostly used for
    defining custom ops.

These two will be supported
Args:
    func (object): yaml object

Returns:
    str: canonical name of the operator
opr$   (r   z::zaten::)split)r$   opnames     r   get_canonical_opname#merge.<locals>.get_canonical_opname    s@      $t|df1C1CC1H1Kv&Fr   )LoaderwFi  )Dumperdefault_flow_style	sort_keyswidth)ospathjoinobjectstropenyamlloadr-   r   existsdumplistvaluesr   )r    r!   r"   output_filer+   ffunctions_objfunctions_dictr$   fallback_objr*   s              r   mergerD      s)   '',,z=9K6 c ( 
!	"a		!F3,7,?!D9=/56 " 
#
 %"''..9K*L*L$%99Qv6L$-d3/-16* % & 
k3	1		&&()"$	
 
 	 
#	" &% 
 	s)   <D<3+E"E>5E<
E

E
E,argvr%   c                    [         R                  " SS9nUR                  SSSSS9  UR                  SS	S
SS9  UR                  SSSS9  UR                  U 5      nUR                  b.  [
        R                  R                  UR                  5      (       d   e[        UR                  UR                  UR                  5        g)a  Merge functions.yaml and fallback yaml. The output yaml will be a union of all entries in functions.yaml and fallback yaml, with operator entries in functions.yaml overriding entries with the same op name in fallback yaml.
E.g.,
functions.yaml:
- op: add.Tensor
  - kernel: add_impl

fallback yaml:
- op: add.Tensor
  - kernel: add_fallback
- op: relu
  - kernel: relu_fallback

Merged:
- op: add.Tensor
  - kernel: add_impl
- op: relu
  - kernel: relu_fallback

zQMerge functions.yaml, custom_ops.yaml with fallback yaml, for codegen to consume.)descriptionz--functions-yaml-pathz--functions_yaml_pathz'path to the functions.yaml file to use.T)helprequiredz--fallback-yaml-pathz--fallback_yaml_pathzpath to fallback yaml file.Fz--output_dirz+The directory to store the output yaml fileN)argparseArgumentParseradd_argument
parse_argsr    r3   r4   r;   rD   r!   r"   )rE   parseroptionss      r   mainrP   L   s    ( $$gF 6	   *	   ;   %G&&2rww~~##8 8   
'
%
%w'A'A7CUCUVr   __main__r   )rJ   r3   syscollectionsr   typingr   r   r   r   r9   r   r-   ImportErrorr	   
SafeDumperr   r7   rD   rP   r   rE   r   r   r   <module>rW      s     	 
 # , , **
'doo ',
s ,
 ,
SV ,
^-WtCy -WT -W` z!" W  *)*s   A6 6BB