
    ip_              	         d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	m
Z
mZ ddlZddlZddlZddlmZmZmZmZmZmZmZmZ ddlmc mZ ddlmc mZ ddlmZmZ d	d
l m!Z! e
rddl"Z"ddl#m$Z$m%Z%m&Z& ddl'm(Z(m)Z) ddlm*Z* g dZ+ ed      Z, ed      Z- ed      Z. e!ej^                  j`                  d      d8d       Z1dZ2defdefdefdefdefdefdefdeffD ](  \  Z2Z3  e!e3d      e3jh                         e5       e2<   [3* [2 e!ejl                  d      	 d9ddd	 	 	 	 	 	 	 	 	 d:d        Z6 e!ejn                  d      	 d9ddd	 	 	 	 	 	 	 	 	 d;d!       Z7 e!ejp                  d      	 d9ddd	 	 	 	 	 	 	 	 	 d<d"       Z8 G d# d$e9      Z: e:       Z;[: ed%       G d& d'             Z<d=d(Z= e!ej|                  d      ddd	 	 	 	 	 d>d)       Z> e!ej~                  d      	 d?ddd	 	 	 	 	 	 	 d@d*       Z? e!ej                  d      	 d?ddd	 	 	 	 	 	 	 	 	 dAd+       Z@ e!ej                  d      	 d9ddd	 	 	 	 	 	 	 	 	 dBd,       ZA e!ej^                  j                  d      	 	 	 dC	 	 	 	 	 	 	 	 	 dDd-       ZC e!ej                  d      	 d9ddd	 	 	 	 	 	 	 	 	 dEd.       ZD e!ej^                  j                  d      	 	 	 dC	 	 	 	 	 	 	 	 	 dFd/       ZF e!ej                  d      	 d9ddd	 	 	 	 	 	 	 	 	 dGd0       ZG e!ej                  d      dHd1       ZHej                  j                   eKd            ZLeLJ  e!eLj                  dd2      dId3       ZN ej                  dd45      5  ej                  j                  eP      ZQeQJ 	 ddd        e!eQj                  dd2      	 	 	 	 dJd6       ZS e!eQj                  dd2      	 	 	 	 	 	 dKd7       ZTy# 1 sw Y   LxY w)Lz)
Python polyfills for torch.utils.pytree
    )annotations)deque)	dataclassfield)AnyTYPE_CHECKINGTypeVarN)is_namedtupleis_namedtuple_classis_namedtuple_instanceis_structseqis_structseq_classis_structseq_instancenamedtuple_fieldsstructseq_fields)BUILTIN_TYPESSTANDARD_DICT_TYPES   )substitute_in_graph)CallableIterableMapping)SelfTypeIs)PyTree)r
   r   r   r   r   r   r   r   treespec_leaftreespec_tupletreespec_dicttree_is_leaf	tree_itertree_leavestree_flattentree_flatten_with_pathtree_structuretree_unflatten_T_KT_VTT)can_constant_fold_throughc                     t        d      )NzeShould not be called directly because the original function will be called in the constant fold path.)
ValueError)argskwargss     X/var/www/html/engine/venv/lib/python3.12/site-packages/torch/_dynamo/polyfills/pytree.py_r/   C   s     	R      r
   r   r   r   r   r   r   r   Fnone_is_leaf	namespacec              x    | |s
|	 ||       ryt         j                  j                  t        |       |      yy)NTr4   F)optreeregister_pytree_nodegettypetreeis_leafr3   r4   s       r.   r   r   c   s?     	7+>74=""&&tDzY&GOr0   c            #     K   | g}|r]|j                         }t        ||||      r| &t        j                  ||||      ^}}|j	                  t        |             |r\y y wNr=   r3   r4   )popr   r7   tree_flatten_one_levelextendreversed)r<   r=   r3   r4   stacknodechildrenr/   s           r.   r    r    s   ss      FE
yy{%	
 J44%	
1 	Xh'(# s   A!A&$A&c              2    t        t        | |||            S r?   )listr    r;   s       r.   r!   r!      s%     %		
 r0   c                  ,     e Zd ZdZd fdZddZ xZS )	_Asterisk c                $    t         |   | d      S N*)super__new__)cls	__class__s    r.   rQ   z_Asterisk.__new__   s    wsC((r0   c                     yrN   rL   selfs    r.   __repr__z_Asterisk.__repr__   s    r0   )returnr   rX   str)__name__
__module____qualname__	__slots__rQ   rW   __classcell__)rS   s   @r.   rK   rK      s    I)r0   rK   )frozenc                     e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   ded<    ed      Zded<    ed      Zded<    ed      Zded<   d$dZ	d%dZ
d&dZed'd       Zd(dZd)dZd*dZd+dZd,dZd-dZd.d Zd/d!Zd0d"Zy#)1
PyTreeSpecz0Analog for :class:`optree.PyTreeSpec` in Python.ztuple[PyTreeSpec, ...]	_childrenbuiltins.type | None_typer   	_metadataztuple[Any, ...]_entriesz7Callable[[Any | None, Iterable[PyTree]], PyTree] | None_unflatten_funcboolr3   rZ   r4   F)initint	num_nodes
num_leavesnum_childrenc                   | j                   Nt        | j                        dk(  sJ | j                  J | j                  dk(  sJ | j
                  J d}d}d}n_t        | j
                        sJ d}d}| j                  D ]   }||j                  z  }||j                  z  }" t        | j                        }t        j                  | d|       t        j                  | d|       t        j                  | d|       y )Nr   rL      rl   rm   rn   )re   lenrc   rf   rg   rh   callablerl   rm   object__setattr__)rV   rl   rm   rn   childs        r.   __post_init__zPyTreeSpec.__post_init__   s    ::t~~&!+++>>)))==B&&&''///IJLD00111IJ /U__,	e...
/ t~~.L4i84z:4>r0   c                    d fdt                      g j                  rdgng d j                  }ddj                  |       dS )Nc                n   | j                         r| j                  J t        S | j                  J t        | j                        sJ | j
                  D cg c]
  } |       }}| j                  t        v sa| j                  t        d       u rj                  r>t        j                  | j                        st        j                  | j                        r| j	                  | j                  |      S d| j                  j                   d| j                  ddj                  |       dS c c}w )NzCustomTreeNode([z], [, z]))r=   r:   	_asteriskrr   rh   rc   r   r3   r7   r   r   rf   r[   join)treespecsubspecchildren_representationshelperrV   s      r.   r   z#PyTreeSpec.__repr__.<locals>.helper   s!   !}},,,  ==,,,H44555/7/A/A($+w($ ( .MMT$Z/8I8I--hmm<,,X]];  //&&, 
 "(--"8"8!98;M;M:P QII678<(s   D2
NoneIsLeafz
namespace=zPyTreeSpec(rz   ))r}   rb   rX   rZ   )rZ   r3   r4   r|   )rV   innerr   s   ` @r.   rW   zPyTreeSpec.__repr__   sb    	6 t
 $ 1 1|nr
 *+

 TYYu-.a00r0   c                   | j                   S N)rm   rU   s    r.   __len__zPyTreeSpec.__len__   s    r0   c                   | j                   S r   )re   rU   s    r.   r:   zPyTreeSpec.type   s    zzr0   c               B    | j                   dk(  xr | j                  dk(  S )Nrp   )rl   rm   rU   s    r.   r=   zPyTreeSpec.is_leaf  s    ~~";t!';;r0   c               f    dfdg  | g        D cg c]  }t        |       c}S c c}w )Nc                    | j                         rj                  |       y t        | j                  | j                  d      D ]  \  }} |||gz           y NTstrict)r=   appendziprg   rc   )r}   path_prefixentryr~   r   pathss       r.   r   z PyTreeSpec.paths.<locals>.helper  s^    ![)"%!!""# 7w
 wug 567r0   )r}   rb   r   	list[Any]rX   None)tuple)rV   pathr   r   s     @@r.   r   zPyTreeSpec.paths  s1    
	7 "$tR(-.d...s   .c                   	 	 	 	 	 	 dfdg  | g        D cg c]  }t        j                  |       c}S c c}w )Nc           
     v   | j                         rj                  |       y | j                  }|J t        j                  j                  || j                        }|J |j                  }|j                  }t        | j                  | j                  d      D ]  \  }} 	|| ||||      gz           y )Nr6   Tr   )r=   r   r:   r7   r8   r9   r4   kindpath_entry_typer   rg   rc   )
r}   entry_path_prefix	node_typehandlerr   r   r   r~   entry_pathsr   s
           r.   r   z$PyTreeSpec.accessors.<locals>.helper  s     !""#45 I(((1155X%7%7 6 G &&&&-llD8?8O8OO"%!!""# w
 %	4)P(QQr0   )r}   rb   r   zlist[optree.PyTreeEntry]rX   r   )r7   PyTreeAccessor)rV   r   r   r   s     @@r.   	accessorszPyTreeSpec.accessors  sR    	 	7	 	6 79tR8CD%%d+DDDs   >c               ,    t        | j                        S r   )rI   rc   rU   s    r.   rG   zPyTreeSpec.children5  s    DNN##r0   c                    | j                   |   S r   )rc   rV   indexs     r.   ru   zPyTreeSpec.child8  s    ~~e$$r0   c               ,    t        | j                        S r   )rI   rg   rU   s    r.   entrieszPyTreeSpec.entries;  s    DMM""r0   c                    | j                   |   S r   )rg   r   s     r.   r   zPyTreeSpec.entry>  s    }}U##r0   c               @     	 	 	 	 	 	 	 	 d fdg }  ||       |S )Nc           	     Z   | j                         r|j                  |       y t        |      }| j                  t        vr|| j                  k7  rt	        d| j                  d|d      t        j                  |j                  j                        ^}}}t        |      | j                  k7  r%t	        d| j                   dt        |       d      || j                  k7  rt	        d| j                  d      | j                  t        v xr |t        v }|s+|| j                  k7  rt	        d| j                  d|d      t        |      | j                  k7  r%t	        d| j                   dt        |       d      |r| j                         }t        |      }	t        |      }
|	|
k7  rG|
j                  |	      }|	j                  |
      }d}|r|d	| z  }|r|d
| z  }t	        d| d      |D cg c]  }||   	 }}not        j                  |j                  j                        ^}}}|t         ur8|| j                  k7  r)t	        d| j                  d| j                  d|d      t#        || j$                  d      D ]  \  }} |||        y c c}w )NzType mismatch; expected z
, but got .r2   zNode arity mismatch; expected z+Node context mismatch for custom node type zNode type mismatch; expected r1   z; missing key(s): z; extra key(s): zNode keys mismatchz%Node metadata mismatch for node type z; expected Tr   )r=   r   r:   r   r+   r7   rB   r3   r4   rq   rn   rf   r   r   set
differencer   r   rc   )r}   rF   subtreesr   rG   metadatar/   both_standard_dictexpected_keysgot_key_setexpected_key_setmissing_keys
extra_keysmessagekeysubtreer~   r   rV   s                    r.   r   z(PyTreeSpec.flatten_up_to.<locals>.helperB  s   
 !%T
I}}M1-$$$,MM#4Jym1N 
 *0)F)F!%!2!2"nn*&(Q
 x=H$9$99$$$,$9$9#:*S]OSTV  x111$EhmmEVVWX  MM%88 9!%88 # *i8==.H$$$,MM#4Jym1N  t9 5 55$$$,$9$9#:*SYKqR 
 &$,$4$4$6M"%d)K'*='9$"&66'7'B'B;'O%0%;%;<L%M
"$'#);L>'JJG%#)9*'FFG(+=gYa)HII5BCcS	CHC .4-J-J%)%6%6"&...*Hh "."h&8&88(CHMMCT U((0(:(:'=Z|STV 
 %((2D2DT$R 3 w23!  Ds   ?J()r}   rb   rF   r   r   list[PyTree]rX   r   rL   )rV   r<   r   r   s   `  @r.   flatten_up_tozPyTreeSpec.flatten_up_toA  sJ    Q	3 Q	3Q	3 #Q	3 	Q	3f "$tT8$r0   c          	        t        |t        t        f      st        |      }t        |      | j                  k7  r(t        dt        |       d| j                   d|  d      | j                         r|d   S d}d}g }| j                  D ]6  }||j                  z  }|j                  |j                  |||              |}8 t        | j                        sJ | j                  | j                  |      S )Nz0treespec.unflatten(leaves): `leaves` has length z, but the spec refers to a pytree that holds z items (z).r   )
isinstancerI   r   rq   rm   r+   r=   rc   r   	unflattenrr   rh   rf   )rV   leavesstartendr   r~   s         r.   r   zPyTreeSpec.unflatten  s    &4-0&\Fv;$//)B3v;- P>>Boo=N Or# 
 <<>!9 ~~ 	G7%%%COOG--fU3.?@AE	
 ,,---##DNNH==r0   N)rX   r   rY   )rX   rk   )rX   rd   )rX   ri   )rX   zlist[tuple[Any, ...]])rX   zlist[optree.PyTreeAccessor])rX   zlist[PyTreeSpec])r   rk   rX   rb   )rX   r   )r   rk   rX   r   )r<   r   rX   r   )r   Iterable[Any]rX   r   )r[   r\   r]   __doc____annotations__r   rl   rm   rn   rv   rW   r   propertyr:   r=   r   r   rG   ru   r   r   r   r   rL   r0   r.   rb   rb      s    :%%NLLN&Is&'J'5)L#)?, 1D  </"E@$%#$Vp>r0   rb   c               B    t        | t        t        j                  f      S r   )r   rb   python_pytreeTreeSpec)objs    r.   _is_pytreespec_instancer     s    cJ(>(>?@@r0   c           	     &    t        dd d dd | d      S )NrL   r1   r2   )rb   r2   s     r.   r   r     s&     

! r0   c                 t        |       }t        d |D              rt        d|d      t        fd|D              rt        d d|d      t        fd|D              rt        dd|d      t        j                  j                  t         	      }|J t        t        |      t         d t        t        t        |                  |j                  
      S )Nc              3  4   K   | ]  }t        |         y wr   r   .0ru   s     r.   	<genexpr>z!treespec_tuple.<locals>.<genexpr>  s     
D%&u--
D   z&Expected a tuple of PyTreeSpecs, got: r   c              3  <   K   | ]  }|j                   k7    y wr   r3   r   ru   r3   s     r.   r   z!treespec_tuple.<locals>.<genexpr>  s     
D%5-
D   YAll children PyTreeSpecs must have the same `none_is_leaf` value as the parent; expected , got: c              3  >   K   | ]  }|j                   d fv  ywr1   Nr6   r   ru   r4   s     r.   r   z!treespec_tuple.<locals>.<genexpr>  s     
He5??9b/1
H   VAll children PyTreeSpecs must have the same `namespace` value as the parent; expected r6   r2   )
r   anyr+   r7   r8   r9   rb   rangerq   unflatten_func)iterabler3   r4   rG   r   s    ``  r.   r   r     s     XH

D8
DDA(QOPP

D8
DD''3nGH<qJ
 	
 
Hx
HH''0m78,aI
 	
 ))--ey-IGheCM"#! r0   c         	        t        | fi |}t        d |j                         D              rt        d|d      t        fd|j                         D              rt        d d|d      t        fd|j                         D              rt        dd|d      t	        j
                  |	      \  }}}}t        t        |      t         |||	      S )
Nc              3  4   K   | ]  }t        |         y wr   r   r   s     r.   r   z treespec_dict.<locals>.<genexpr>  s     
H%&u--
Hr   z)Expected a dictionary of TreeSpecs, got: r   c              3  <   K   | ]  }|j                   k7    y wr   r   r   s     r.   r   z treespec_dict.<locals>.<genexpr>  s     
H%5-
Hr   r   r   c              3  >   K   | ]  }|j                   d fv  ywr   r6   r   s     r.   r   z treespec_dict.<locals>.<genexpr>	  s     
Le5??9b/1
Lr   r   r2   )dictr   valuesr+   r7   rB   rb   r   )	mappingr3   r4   r-   dctrG   r   r   r   s	    ``      r.   r   r     s    w
!&
!C

H3::<
HHDSG1MNN

H3::<
HH''3nGC7!E
 	
 
Lszz|
LL''0m73'D
 	
 	%%!	 h! r0   c              :    dfdg } | |      }||fS )Nc           	        t        | 
	      r#j                  |        t        dd d dd 
	      S t        j                  | 
	      \  }}}}t        fd|D              }t        |t        |       |||
	      S )Nr@   rL   r2   c              3  0   K   | ]  } |        y wr   rL   )r   ru   r   r   s     r.   r   z/tree_flatten.<locals>.helper.<locals>.<genexpr>Q  s     E5v.Es   )r   r   rb   r7   rB   r   r:   )rF   r   rG   r   r   r   subspecsr   r=   r4   r3   s    `     r.   r   ztree_flatten.<locals>.helper2  s    %	
 MM$)#  ))%	
	
 EHEEJ%
 	
r0   )rF   r   r   r   rX   rb   rL   )r<   r=   r3   r4   r   r}   r   s    ```  @r.   r"   r"   $  s,    (
 (
T FdF#H8r0   c                    t        | |||      S r?   r"   r<   leaf_predicater3   r4   s       r.   
_C_flattenr   a  s     !	 r0   c              L    t        | |||      \  }}|j                         ||fS r?   )r"   r   )r<   r=   r3   r4   r   r}   s         r.   r#   r#   v  s5     $!	FH >>VX--r0   c                    t        | |||      S r?   )r#   r   s       r.   _C_flatten_with_pathr     s     "!	 r0   c              &    t        | |||      d   S )Nr@   rp   r   r;   s       r.   r$   r$     s'     !	
 	 	r0   c                j    t        |       st        dt        |        d      | j                  |      S )NzIExpected `treespec` to be an instance of PyTreeSpec but got item of type r   )r   	TypeErrorr:   r   )r}   r   s     r.   r%   r%     sC     #8,//3H~.>aA
 	
 f%%r0   )r)   skip_signature_checkc               H    t        t        |            dk7  rt        d      y )Nr   zExpected no children.)rq   rI   r+   )r/   rG   s     r.   none_unflattenr     s$     4>a011r0   torchr6   c                   t         j                  j                  |       }|D cg c]  }| |   	 }}t        |       }|||ft	        |      fS c c}w r   )r7   utilstotal_order_sortedrI   r   )r   sorted_keysr   r   original_keyss        r.   dict_flattenr     sT     ,,11#6K"-.3c#h.F.IMM;/{1CCC /s   Ac               t    | \  }}t         j                  |      }|j                  t        ||d             |S r   )r   fromkeysupdater   )r   r   r   r   ds        r.   dict_unflattenr    s6     "*M;m$AHHSfT23Hr0   )r,   r   r-   r   rX   ri   r   )
r=   Callable[[PyTree], bool] | Noner<   r   r3   ri   r4   rZ   rX   ri   )
r=   r  r<   r   r3   ri   r4   rZ   rX   r   )
r=   r  r<   r   r3   ri   r4   rZ   rX   r   )r   r   rX   z+TypeIs[PyTreeSpec | python_pytree.TreeSpec])r3   ri   r4   rZ   rX   rb   )rL   )r   zIterable[PyTreeSpec]r3   ri   r4   rZ   rX   rb   )
r   z;Mapping[Any, PyTreeSpec] | Iterable[tuple[Any, PyTreeSpec]]r3   ri   r4   rZ   r-   rb   rX   rb   )
r=   r  r<   r   r3   ri   r4   rZ   rX   tuple[list[Any], PyTreeSpec])NFr1   )
r   r  r3   ri   r4   rZ   r<   r   rX   r  )
r=   r  r<   r   r3   ri   r4   rZ   rX   3tuple[list[tuple[Any, ...]], list[Any], PyTreeSpec])
r   r  r3   ri   r4   rZ   r<   r   rX   r  )
r=   r  r<   r   r3   ri   r4   rZ   rX   rb   )r}   rb   r   r   rX   r   )r/   r   rG   zIterable[_T]rX   r   )r   dict[_KT, _VT]rX   z>tuple[list[_VT], tuple[list[_KT], list[_KT]], tuple[_KT, ...]])r   ztuple[list[_KT], list[_KT]]r   zIterable[_VT]rX   r	  )Ur   
__future__r   collectionsr   dataclassesr   r   typingr   r   r	   r7   	optree._Coptree.utilsr
   r   r   r   r   r   r   r   torch.utils._cxx_pytreer   _cxx_pytree
cxx_pytreetorch.utils._pytree_pytreer   r   r   
decoratorsr   builtinscollections.abcr   r   r   typing_extensionsr   r   r   __all__r&   r'   r(   _Cis_dict_insertion_orderedr/   __name__func__python_implementation__globalsr   r    r!   rZ   rK   r{   rb   r   r   r   r   r"   flattenr   r#   flatten_with_pathr   r$   r%   r8   r9   r:   _none_registrationr   r   dict_insertion_orderedr   _dict_registrationflatten_funcr   r  rL   r0   r.   <module>r&     s   #  ( . .   	 	 	 - , + + B , ;;.., T]enen 
II''"	 
m$/056\"-.34+,)*	 NFF+"& &&(GIf 	  V((DI 04
  -

   
 J V%%G 04)
 ) -)
)
 ) ) ) H): V''4H 04
  -

    I$  K	 $|> |> |>~A 
 $	   	  
 $	 &( " 	
  B 
 $	 LN( (H( 	(
 ( ( ((V 
 $	 044
 4 -4
4
 4 4 "44n 
II $	 7; 4 	
 
 " 
!! $	 04.
 . -.
.
 . . 9.." 
II $	 7; 4 	
 
 9 
 $	 04	
 	 -	
	
 	 	 		  
 $	&& 0044T$Z@ % %% %%"

 #V""5G< *4488>)))*
 ##"
D	DCD
D %%"
) 	
1* *s   ; O''O0