
    Wj|_              	      V   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/        j0        d          d]d            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          e3j4                   e5            e2<   [3-[2 e!ej6        d          	 d^d#dd$d_d,            Z6 e!ej7        d#          	 d^d#dd$d`d.            Z7 e!ej8        d          	 d^d#dd$dad0            Z8 G d1 d2e9          Z: e:            Z;[: edd3           G d4 d5                      Z<dbd8Z= e!ej>        d#          d#dd$dcd9            Z> e!ej?        d#          	 ddd#dd$ded=            Z? e!ej@        d#          	 ddd#dd$dfd@            Z@ e!ejA        d#          	 d^d#dd$dgdB            ZA e!ej/        jB        d#          	 	 	 dhdidD            ZC e!ejD        d#          	 d^d#dd$djdF            ZD e!ej/        jE        d#          	 	 	 dhdkdG            ZF e!ejG        d#          	 d^d#dd$dldH            ZG e!ejH        d#          dmdK            ZHejI        J                     eKd                    ZLeLJ  e!eLjM        ddL          dndQ            ZN ejO        d#dRS          5  ejI        J                    eP          ZQeQJ 	 ddd           n# 1 swxY w Y    e!eQjR        ddL          dodW            ZS e!eQjM        ddL          dpd\            ZTdS )qz)
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_throughargsr   kwargsreturnboolc                      t          d          )NzeShould not be called directly because the original function will be called in the constant fold path.)
ValueError)r*   r+   s     c/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/torch/_dynamo/polyfills/pytree.py_r1   C   s     	R       r
   r   r   r   r   r   r   r   Fnone_is_leaf	namespaceis_leafCallable[[PyTree], bool] | Nonetreer   r5   r6   strc                  | |s| ||           rdS t           j                            t          |           |          dS dS )NTr6   F)optreeregister_pytree_nodegettyper9   r7   r5   r6   s       r0   r   r   c   sP     	7+>774==+>t"&&tDzzY&GGOt5r2   Iterable[Any]c            #     K   | g}|rl|                                 }t          ||||          r|V  .t          j        ||||          ^}}|                    t          |                     |jd S d S Nr7   r5   r6   )popr   r=   tree_flatten_one_levelextendreversed)r9   r7   r5   r6   stacknodechildrenr1   s           r0   r    r    s   s       FE
 )yy{{%	
 
 
 	 JJJ4%	
 
 
1 	Xh''(((#  ) ) ) ) )r2   	list[Any]c              B    t          t          | |||                    S rD   )listr    rA   s       r0   r!   r!      s4     %		
 	
 	
  r2   c                  ,     e Zd ZdZd fdZddZ xZS )		_Asterisk r,   r   c                H    t                                          | d          S N*)super__new__)cls	__class__s    r0   rW   z_Asterisk.__new__   s    wwsC(((r2   r:   c                    dS rT   rR   selfs    r0   __repr__z_Asterisk.__repr__   s    sr2   )r,   r   r,   r:   )__name__
__module____qualname__	__slots__rW   r]   __classcell__)rY   s   @r0   rQ   rQ      sV        I) ) ) ) ) )       r2   rQ   )frozenslotsc                  8   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<   d0dZ	d1dZ
d2dZed3d            Zd4dZd5dZd6d Zd7d"Zd8d$Zd9d&Zd:d'Zd;d+Zd<d.Zd/S )=
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_funcr-   r5   r:   r6   F)initint	num_nodes
num_leavesnum_childrenr,   Nonec                  | j         @t          | j                  dk    sJ | j        J | j        dk    sJ | j        J d}d}d}nLt          | j                  sJ d}d}| j        D ]}||j        z  }||j        z  }t          | j                  }t          
                    | d|           t          
                    | d|           t          
                    | d|           d S )Nr   rR      rp   rq   rr   )rj   lenrh   rk   rl   rm   callablerp   rq   object__setattr__)r\   rp   rq   rr   childs        r0   __post_init__zPyTreeSpec.__post_init__   s   :t~&&!++++>)))=B&&&&'///IJLLD011111IJ / /U_,	e..

t~..L4i8884z:::4>>>>>r2   c                    d fdt                                g j        rdgng d j        }dd	                    |           d
S )Ntreespecrg   r,   r:   c                   |                                  r| j        J t          S | j        J t          | j                  sJ fd| j        D             }| j        t          v sO| j        t          d           u rj        r2t          j	        | j                  st          j
        | j                  r|                     | j        |          S d| j        j         d| j        dd                    |           dS )Nc                &    g | ]} |          S rR   rR   ).0subspechelpers     r0   
<listcomp>z7PyTreeSpec.__repr__.<locals>.helper.<locals>.<listcomp>   s.     ( ( ($+w( ( (r2   zCustomTreeNode([z], [, z]))r7   r@   	_asteriskrw   rm   rh   r   r5   r=   r   r   rk   r_   join)r}   children_representationsr   r\   s     r0   r   z#PyTreeSpec.__repr__.<locals>.helper   s<   !! !},,,  =,,,H455555( ( ( (/7/A( ( ($ ..MT$ZZ//8I/-hm<< 0,X];; 0
  //&,  
<(-"8 < <8;M < <II677< < <r2   
NoneIsLeafz
namespace=zPyTreeSpec(r   ))r}   rg   r,   r:   )r:   r5   r6   r   )r\   innerr   s   ` @r0   r]   zPyTreeSpec.__repr__   s    	 	 	 	 	 	 	6 t
 $ 19|nnr
 ,++

 1TYYu--0000r2   c                   | j         S N)rq   r[   s    r0   __len__zPyTreeSpec.__len__   s
    r2   c                   | j         S r   )rj   r[   s    r0   r@   zPyTreeSpec.type   s
    zr2   c               .    | j         dk    o
| j        dk    S )Nru   )rp   rq   r[   s    r0   r7   zPyTreeSpec.is_leaf  s    ~";t!';;r2   list[tuple[Any, ...]]c               H    d	fdg  | g            d D             S )
Nr}   rg   path_prefixrM   r,   rs   c                    |                                  r                    |           d S t          | j        | j        d          D ]\  }} |||gz              d S NTstrict)r7   appendziprl   rh   )r}   r   entryr   r   pathss       r0   r   z PyTreeSpec.paths.<locals>.helper  s    !! [)))"%!"# # # 7 7w
 wug 566667 7r2   c                ,    g | ]}t          |          S rR   )tupler   paths     r0   r   z$PyTreeSpec.paths.<locals>.<listcomp>  s    ...d...r2   )r}   rg   r   rM   r,   rs   rR   )r\   r   r   s    @@r0   r   zPyTreeSpec.paths  sT    
	7 
	7 
	7 
	7 
	7 
	7 
	7 "$tR......r2   list[optree.PyTreeAccessor]c               H    d	fdg  | g            d D             S )
Nr}   rg   entry_path_prefixlist[optree.PyTreeEntry]r,   rs   c           
     ^   |                                  r                    |           d S | j        }|J t          j                            || j                  }|J |j        }|j        }t          | j
        | j        d          D ] \  }} 	|| ||||          gz              !d S )Nr<   Tr   )r7   r   r@   r=   r>   r?   r6   kindpath_entry_typer   rl   rh   )
r}   r   	node_typehandlerr   r   r   r   entry_pathsr   s
           r0   r   z$PyTreeSpec.accessors.<locals>.helper  s     !! ""#4555 I(((155X%7 6  G &&&&-lD8?8OO"%!"# # #  w
 %	4)P)P(QQ    r2   c                6    g | ]}t          j        |          S rR   )r=   PyTreeAccessorr   s     r0   r   z(PyTreeSpec.accessors.<locals>.<listcomp>3  s#    DDD%d++DDDr2   )r}   rg   r   r   r,   rs   rR   )r\   r   r   s    @@r0   	accessorszPyTreeSpec.accessors  sT    	 	 	 	 	 	 	6 79tRDDDDDDr2   list[PyTreeSpec]c               *    t          | j                  S r   )rO   rh   r[   s    r0   rL   zPyTreeSpec.children5  s    DN###r2   indexc                   | j         |         S r   )rh   r\   r   s     r0   rz   zPyTreeSpec.child8  s    ~e$$r2   rM   c               *    t          | j                  S r   )rO   rl   r[   s    r0   entrieszPyTreeSpec.entries;  s    DM"""r2   c                   | j         |         S r   )rl   r   s     r0   r   zPyTreeSpec.entry>  s    }U##r2   r9   r   list[PyTree]c               6     d
 fd	g }  ||           |S )Nr}   rg   rK   r   subtreesr   r,   rs   c           	     8   |                                  r|                               d S t                    }| j        t          vr|| j        k    rt	          d| j        d|d          t          j        j        j                  ^}}}t          |          | j
        k    r(t	          d| j
         dt          |           d          || j        k    rt	          d| j        d          nv| j        t          v o|t          v }|s&|| j        k    rt	          d| j        d|d          t                    | j
        k    r(t	          d| j
         dt                     d          |r|                                 }t                    }	t          |          }
|	|
k    rS|
                    |	          }|	                    |
          }d}|r|d	| z  }|r|d
| z  }t	          d| d          fd|D             }n[t          j        j        j                  ^}}}|t           ur.|| j        k    r#t	          d| j        d| j        d|d          t#          || j        d          D ]\  }} |||           d S )NzType mismatch; expected z
, but got .r4   zNode arity mismatch; expected z+Node context mismatch for custom node type zNode type mismatch; expected r3   z; missing key(s): z; extra key(s): zNode keys mismatchc                     g | ]
}|         S rR   rR   )r   keyrK   s     r0   r   z<PyTreeSpec.flatten_up_to.<locals>.helper.<locals>.<listcomp>  s    CCCcS	CCCr2   z%Node metadata mismatch for node type z; expected Tr   )r7   r   r@   r   r/   r=   rG   r5   r6   rv   rr   rk   r   r   set
differencer   r   rh   )r}   rK   r   r   rL   metadatar1   both_standard_dictexpected_keysgot_key_setexpected_key_setmissing_keys
extra_keysmessagesubtreer   r   r\   s    `              r0   r   z(PyTreeSpec.flatten_up_to.<locals>.helperB  s   
 !! %%%T

I}M11--$N$,MN N?HN N N  
 *0)F!%!2"n* * *&(Q
 x==H$999$V$,$9V VEH]]V V V   x111$XhmXXX   2 M%88 9!%88 # * i8=.H.H$N$,MN N?HN N N   t99 555$R$,$9R REHYYR R R  
 & $,$4$4$6$6M"%d))K'*='9'9$"&666'7'B'B;'O'O%0%;%;<L%M%M
"$' K#'JL'J'JJG% G#'F*'F'FFG()Hg)H)H)HIIICCCC]CCCHH .4-J%)%6"&.. . .*Hh ".."h&888(VHM V V(0(:V VHPV V V  
 %((2DT$R$R$R 3 3 w22223 3r2   )r}   rg   rK   r   r   r   r,   rs   rR   )r\   r9   r   r   s   `  @r0   flatten_up_tozPyTreeSpec.flatten_up_toA  sO    Q	3 Q	3 Q	3 Q	3 Q	3 Q	3 Q	3f "$tT8$$$r2   leavesrB   c          	        t          |t          t          f          st          |          }t          |          | j        k    r+t          dt          |           d| j         d|  d          |                                 r|d         S d}d}g }| j        D ]>}||j        z  }|                    |	                    |||                              |}?t          | j                  sJ |                     | j        |          S )Nz0treespec.unflatten(leaves): `leaves` has length z, but the spec refers to a pytree that holds z items (z).r   )
isinstancerO   r   rv   rq   r/   r7   rh   r   	unflattenrw   rm   rk   )r\   r   startendr   r   s         r0   r   zPyTreeSpec.unflatten  s*   &4-00 	"&\\Fv;;$/))#3v;; # #>Bo# ## # #  
 <<>> 	!9 ~ 	 	G7%%COOG--fU3Y.?@@AAAEE,-----##DNH===r2   N)r,   rs   r^   )r,   ro   )r,   ri   )r,   r-   )r,   r   )r,   r   )r,   r   )r   ro   r,   rg   )r,   rM   )r   ro   r,   r   )r9   r   r,   r   )r   rB   r,   r   )r_   r`   ra   __doc____annotations__r   rp   rq   rr   r{   r]   r   propertyr@   r7   r   r   rL   rz   r   r   r   r   rR   r2   r0   rg   rg      s        ::%%%%NNNLLLLNNNU&&&I&&&&e'''J''''5)))L))))? ? ? ?, 1  1  1  1D       X< < < </ / / /"E E E E@$ $ $ $% % % %# # # #$ $ $ $V V V Vp> > > > > >r2   rg   obj+TypeIs[PyTreeSpec | python_pytree.TreeSpec]c               D    t          | t          t          j        f          S r   )r   rg   python_pytreeTreeSpec)r   s    r0   _is_pytreespec_instancer     s    cJ(>?@@@r2   c           	     .    t          dd d dd | d          S )NrR   r3   r4   )rg   r4   s     r0   r   r     s0     

!   r2   rR   iterableIterable[PyTreeSpec]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                            t           	          }|J t          t          |          t           d t          t          t          |                              |j	        
          S )Nc              3  6   K   | ]}t          |           V  d S r   r   r   rz   s     r0   	<genexpr>z!treespec_tuple.<locals>.<genexpr>  s.      
D
D%&u---
D
D
D
D
D
Dr2   z&Expected a tuple of PyTreeSpecs, got: r   c              3  .   K   | ]}|j         k    V  d S r   r5   r   rz   r5   s     r0   r   z!treespec_tuple.<locals>.<genexpr>  s+      
D
D%5-
D
D
D
D
D
Dr2   YAll children PyTreeSpecs must have the same `none_is_leaf` value as the parent; expected , got: c              3  .   K   | ]}|j         d fvV  dS r3   Nr<   r   rz   r6   s     r0   r   z!treespec_tuple.<locals>.<genexpr>  s.      
H
He5?9b/1
H
H
H
H
H
Hr2   VAll children PyTreeSpecs must have the same `namespace` value as the parent; expected r<   r4   )
r   anyr/   r=   r>   r?   rg   rangerv   unflatten_func)r   r5   r6   rL   r   s    ``  r0   r   r     s    XH

D
D8
D
D
DDD QO(OOOPPP

D
D
D
D8
D
D
DDD 
J'3J J<DJ J J
 
 	
 
H
H
H
Hx
H
H
HHH 
I'0I I;CI I I
 
 	
 )--ey-IIGheCMM""##!   r2   mapping;Mapping[Any, PyTreeSpec] | Iterable[tuple[Any, PyTreeSpec]]c         	     0   t          | fi |}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        |	          \  }}}}t          t          |          t           |||	          S )
Nc              3  6   K   | ]}t          |           V  d S r   r   r   s     r0   r   z treespec_dict.<locals>.<genexpr>  s.      
H
H%&u---
H
H
H
H
H
Hr2   z)Expected a dictionary of TreeSpecs, got: r   c              3  .   K   | ]}|j         k    V  d S r   r   r   s     r0   r   z treespec_dict.<locals>.<genexpr>  s+      
H
H%5-
H
H
H
H
H
Hr2   r   r   c              3  .   K   | ]}|j         d fvV  dS r   r<   r   s     r0   r   z treespec_dict.<locals>.<genexpr>	  s.      
L
Le5?9b/1
L
L
L
L
L
Lr2   r   r4   )dictr   valuesr/   r=   rG   rg   r   )	r   r5   r6   r+   dctrL   r   r   r   s	    ``      r0   r   r     s    w
!
!&
!
!C

H
H3::<<
H
H
HHH OMSMMMNNN

H
H
H
H3::<<
H
H
HHH 
E'3E E<?E E E
 
 	
 
L
L
L
Lszz||
L
L
LLL 
D'0D D;>D D D
 
 	
 	%!	 	 	 h!   r2   tuple[list[Any], PyTreeSpec]c              @    dfdg } | |          }||fS )	NrK   r   r   rM   r,   rg   c           	     :   t          | 
	          r+                    |            t          dd d dd 
	          S t          j        | 
	          \  }}}}t          fd|D                       }t          |t          |           |||
	          S )NrE   rR   r4   c              3  0   K   | ]} |          V  d S r   rR   )r   rz   r   r   s     r0   r   z/tree_flatten.<locals>.helper.<locals>.<genexpr>Q  s/      EE5v..EEEEEEr2   )r   r   rg   r=   rG   r   r@   )rK   r   rL   r   r   r   subspecsr   r7   r6   r5   s    `     r0   r   ztree_flatten.<locals>.helper2  s    %	
 
 
 	 MM$)#    )%	
 
 
	
 EEEEEHEEEEEJJ%
 
 
 	
r2   )rK   r   r   rM   r,   rg   rR   )r9   r7   r5   r6   r   r}   r   s    ```  @r0   r"   r"   $  sW    (
 (
 (
 (
 (
 (
 (
 (
 (
T FvdF##H8r2   leaf_predicatec               (    t          | |||          S rD   r"   r9   r   r5   r6   s       r0   
_C_flattenr   a  s'     !	   r2   3tuple[list[tuple[Any, ...]], list[Any], PyTreeSpec]c              \    t          | |||          \  }}|                                ||fS rD   )r"   r   )r9   r7   r5   r6   r   r}   s         r0   r#   r#   v  sB     $!	  FH >>VX--r2   c               (    t          | |||          S rD   )r#   r   s       r0   _C_flatten_with_pathr    s'     "!	   r2   c              4    t          | |||          d         S )NrE   ru   r   rA   s       r0   r$   r$     s1     !	  
 	 	r2   r}   r   c                    t          |           s t          dt          |            d          |                     |          S )NzIExpected `treespec` to be an instance of PyTreeSpec but got item of type r   )r   	TypeErrorr@   r   )r}   r   s     r0   r%   r%     s]     #8,, 
A/3H~~A A A
 
 	
 f%%%r2   )r)   skip_signature_checkr1   rs   rL   Iterable[_T]c               d    t          t          |                    dk    rt          d          d S )Nr   zExpected no children.)rv   rO   r/   )r1   rL   s     r0   none_unflattenr    s1     4>>a01114r2   torchr<   r   dict[_KT, _VT]>tuple[list[_VT], tuple[list[_KT], list[_KT]], tuple[_KT, ...]]c                    t           j                                       } fd|D             }t                     }|||ft	          |          fS )Nc                     g | ]
}|         S rR   rR   )r   r   r   s     r0   r   z dict_flatten.<locals>.<listcomp>  s    ...3c#h...r2   )r=   utilstotal_order_sortedrO   r   )r   sorted_keysr   original_keyss   `   r0   dict_flattenr    sX     ,11#66K....+...FIIMM;/{1C1CCCr2   r   tuple[list[_KT], list[_KT]]r   Iterable[_VT]c                   | \  }}t                               |          }|                    t          ||d                     |S r   )r   fromkeysupdater   )r   r   r  r  ds        r0   dict_unflattenr    sE     "*M;m$$AHHSfT222333Hr2   )r*   r   r+   r   r,   r-   r   )
r7   r8   r9   r   r5   r-   r6   r:   r,   r-   )
r7   r8   r9   r   r5   r-   r6   r:   r,   rB   )
r7   r8   r9   r   r5   r-   r6   r:   r,   rM   )r   r   r,   r   )r5   r-   r6   r:   r,   rg   )rR   )r   r   r5   r-   r6   r:   r,   rg   )
r   r   r5   r-   r6   r:   r+   rg   r,   rg   )
r7   r8   r9   r   r5   r-   r6   r:   r,   r   )NFr3   )
r   r8   r5   r-   r6   r:   r9   r   r,   r   )
r7   r8   r9   r   r5   r-   r6   r:   r,   r   )
r   r8   r5   r-   r6   r:   r9   r   r,   r   )
r7   r8   r9   r   r5   r-   r6   r:   r,   rg   )r}   rg   r   rB   r,   r   )r1   rs   rL   r  r,   rs   )r   r
  r,   r  )r   r  r   r  r,   r
  )Ur   
__future__r   collectionsr   dataclassesr   r   typingr   r   r	   r=   	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_orderedr1   __name__func__python_implementation__globalsr   r    r!   r:   rQ   r   rg   r   r   r   r   r"   flattenr   r#   flatten_with_pathr  r$   r%   r>   r?   r@   _none_registrationr   r  dict_insertion_orderedr   _dict_registrationflatten_funcr  r  rR   r2   r0   <module>r6     s    # " " " " "       ( ( ( ( ( ( ( ( . . . . . . . . . .         	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 - , , , , , , , , + + + + + + + + + B B B B B B B B , , , , , ,  /OOO;;;;;;;;;;..............  , WT]]genngenn 
I'"    	  
m$/056\"-.34+,)*	  NFF++"&   &( (GGIIf 	
 V(DIII 04
      JI V%GGG 04)
 ) ) ) ) ) HG): V'4HHH 04
      IH$        IKK	 $d###|> |> |> |> |> |> |> $#|>~A A A A 
 $	          
 $	   &(      B 
 $	   LN( ( ( ( ( ( (V 
 $	   044
 4 4 4 4 4 4n 
I $	   7;     
! $	   04.
 . . . . . ." 
I $	   7;     
 $	   04	
 	 	 	 	 	 	  
 $	  & & & & 044TT$ZZ@@ %%% %"  
   
 #V"5G<<< * *488>>))))* * * * * * * * * * * * * * *
 #"  
D D D 
D %"  
   
  s   8M$$M(+M(