o
    ei|_                  	   @  s&  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  mZ ddlm  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/j0dddmd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 ]\Z2Z3e!e3dde3j4e5 e2< [3q[2e!ej6dd	dnd$dd%dod-d.Z6e!ej7d$d	dnd$dd%dpd0d1Z7e!ej8dd	dnd$dd%dqd3d4Z8G d5d6 d6e9Z:e: Z;[:eddd7G d8d9 d9Z<drd<d=Z=e!ej>d$dd$dd%dsd>d?Z>e!ej?d$d	@dtd$dd%dudCdDZ?e!ej@d$d	@dtd$dd%dvdGdHZ@e!ejAd$d	dnd$dd%dwdJdKZAe!ej/jBd$d		$	dxdydMdNZCe!ejDd$d	dnd$dd%dzdPdQZDe!ej/jEd$d		$	dxd{dRdSZFe!ejGd$d	dnd$dd%d|dTdUZGe!ejHd$dd}dXdYZHejIJeKdZLeLdusJ e!eLjMdddZd~d^d_ZNejOd$d`da ejIJePZQeQdusJ W d   n	1 sw   Y  e!eQjRdddZddedfZSe!eQjMdddZddkdlZTdS )z)
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                  O  s   t d)NzeShould not be called directly because the original function will be called in the constant fold path.)
ValueError)r)   r*    r.   h/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/_dynamo/polyfills/pytree.py_C   s   r0    r	   r
   r   r   r   r   r   r   Fnone_is_leaf	namespaceis_leafCallable[[PyTree], bool] | Nonetreer   r3   r4   strc               C  s@   | d u r|s|d ur|| rdS t jjt| |dd u rdS dS )NTr4   F)optreeregister_pytree_nodegettyper7   r5   r3   r4   r.   r.   r/   r   c   s
   	r   Iterable[Any]c               c  s\    | g}|r,|  }t||||dr|V  qtj||||d^}}|t| |sd S d S Nr5   r3   r4   )popr   r:   tree_flatten_one_levelextendreversed)r7   r5   r3   r4   stacknodechildrenr0   r.   r.   r/   r   s   s(   	
r   	list[Any]c               C  s   t t| |||dS r@   )listr   r>   r.   r.   r/   r       s   	r    c                      s,   e Zd ZdZd	 fddZd
ddZ  ZS )	_Asteriskr.   r+   r   c                   s   t  | dS N*)super__new__)cls	__class__r.   r/   rO      s   z_Asterisk.__new__r8   c                 C  s   dS rL   r.   selfr.   r.   r/   __repr__   s   z_Asterisk.__repr__)r+   r   r+   r8   )__name__
__module____qualname__	__slots__rO   rU   __classcell__r.   r.   rQ   r/   rK      s    rK   )frozenslotsc                   @  s  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dZded< eddZded< eddZded< d=ddZ	d>ddZ
d?ddZed@ddZdAd d!ZdBd#d$ZdCd&d'ZdDd)d*ZdEd,d-ZdFd/d0ZdGd1d2ZdHd6d7ZdId:d;Zd<S )J
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,   r3   r8   r4   F)initint	num_nodes
num_leavesnum_childrenr+   Nonec                C  s   | j d u r*t| jdksJ | jd u sJ | jdksJ | jd u s#J d}d}d}n t| js1J d}d}| jD ]}||j7 }||j7 }q8t| j}t	
| d| t	
| d| t	
| d| d S )Nr   r.      rg   rh   ri   )ra   lenr_   rb   rc   rd   callablerg   rh   object__setattr__)rT   rg   rh   ri   childr.   r.   r/   __post_init__   s$   



zPyTreeSpec.__post_init__c                  sL   d fdd t  gjrdgng dj}d	d
| dS )Ntreespecr^   r+   r8   c                   s   |   r| jd u sJ tS | jd usJ t| jsJ  fdd| jD }| jtv s@| jtd u r4jr@t	| js@t
| jrG| | j|S d| jj d| jdd| dS )Nc                   s   g | ]} |qS r.   r.   ).0subspec)helperr.   r/   
<listcomp>   s    z7PyTreeSpec.__repr__.<locals>.helper.<locals>.<listcomp>zCustomTreeNode([z], [, z]))r5   r=   	_asteriskrm   rd   r_   r   r3   r:   r
   r   rb   rW   join)rr   children_representationsru   rT   r.   r/   ru      s,   



z#PyTreeSpec.__repr__.<locals>.helper
NoneIsLeafz
namespace=zPyTreeSpec(rx   ))rr   r^   r+   r8   )r8   r3   r4   rz   )rT   innerr.   r|   r/   rU      s   

zPyTreeSpec.__repr__c                C     | j S N)rh   rS   r.   r.   r/   __len__   s   zPyTreeSpec.__len__c                C  r   r   )ra   rS   r.   r.   r/   r=      s   zPyTreeSpec.typec                C  s   | j dko	| jdkS )Nrk   )rg   rh   rS   r.   r.   r/   r5     s   zPyTreeSpec.is_leaflist[tuple[Any, ...]]c                  s,   d fdd g  | g  d	d
 D S )Nrr   r^   path_prefixrI   r+   rj   c                   sF   |   r| d S t| j| jddD ]\}} |||g  qd S NTstrict)r5   appendziprc   r_   )rr   r   entryrt   ru   pathsr.   r/   ru     s   
z PyTreeSpec.paths.<locals>.helperc                 S  s   g | ]}t |qS r.   )tuplers   pathr.   r.   r/   rv         z$PyTreeSpec.paths.<locals>.<listcomp>)rr   r^   r   rI   r+   rj   r.   rS   r.   r   r/   r     s   
zPyTreeSpec.pathslist[optree.PyTreeAccessor]c                  s,   d fddg  | g  d	d
  D S )Nrr   r^   entry_path_prefixlist[optree.PyTreeEntry]r+   rj   c                   s   |   r | d S | j}|d usJ tjj|| jd}|d us#J |j}|j}t	| j
| jddD ]\}}||||||g  q2d S )Nr9   Tr   )r5   r   r=   r:   r;   r<   r4   kindpath_entry_typer   rc   r_   )rr   r   	node_typehandlerr   r   r   rt   entry_pathsru   r.   r/   ru     s*   
z$PyTreeSpec.accessors.<locals>.helperc                 S  s   g | ]}t |qS r.   )r:   PyTreeAccessorr   r.   r.   r/   rv   3  s    z(PyTreeSpec.accessors.<locals>.<listcomp>)rr   r^   r   r   r+   rj   r.   rS   r.   r   r/   	accessors  s   
zPyTreeSpec.accessorslist[PyTreeSpec]c                C  
   t | jS r   )rJ   r_   rS   r.   r.   r/   rH   5     
zPyTreeSpec.childrenindexc                C  
   | j | S r   )r_   rT   r   r.   r.   r/   rp   8  r   zPyTreeSpec.childrI   c                C  r   r   )rJ   rc   rS   r.   r.   r/   entries;  r   zPyTreeSpec.entriesc                C  r   r   )rc   r   r.   r.   r/   r   >  r   zPyTreeSpec.entryr7   r   list[PyTree]c                  s$   d fd	d
 g } || |S )Nrr   r^   rG   r   subtreesr   r+   rj   c                   s  |   r|  d S t }| jtvrV|| jkr%td| jd|dtj jjd^}}}t	|| j
krGtd| j
 dt	| d|| jkrUtd| jdn| jtv o^|tv }|sr|| jkrrtd| jd|dt	 | j
krtd| j
 dt	  d|r|  }t }	t|}
|	|
kr|
|	}|	|
}d}|r|d	| 7 }|r|d
| 7 }td| d fdd|D }n&tj jjd^}}}|tur|| jkrtd| jd| jd|dt|| jddD ]
\}}||| qd S )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 mismatchc                      g | ]} | qS r.   r.   rs   keyrG   r.   r/   rv     r   z<PyTreeSpec.flatten_up_to.<locals>.helper.<locals>.<listcomp>z%Node metadata mismatch for node type z; expected Tr   )r5   r   r=   r   r-   r:   rC   r3   r4   rl   ri   rb   r   r   set
differencer   r   r_   )rr   rG   r   r   rH   metadatar0   both_standard_dictexpected_keysgot_key_setexpected_key_setmissing_keys
extra_keysmessagesubtreert   r|   r   r/   ru   B  s   








z(PyTreeSpec.flatten_up_to.<locals>.helper)rr   r^   rG   r   r   r   r+   rj   r.   )rT   r7   r   r.   r|   r/   flatten_up_toA  s   SzPyTreeSpec.flatten_up_toleavesr?   c                C  s   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7 }||	|||  |}q4t
| jsQJ | | j|S )Nz0treespec.unflatten(leaves): `leaves` has length z, but the spec refers to a pytree that holds z items (z).r   )
isinstancerJ   r   rl   rh   r-   r5   r_   r   	unflattenrm   rd   rb   )rT   r   startendr   rt   r.   r.   r/   r     s*   

zPyTreeSpec.unflattenN)r+   rj   rV   )r+   rf   )r+   r`   )r+   r,   )r+   r   )r+   r   )r+   r   )r   rf   r+   r^   )r+   rI   )r   rf   r+   r   )r7   r   r+   r   )r   r?   r+   r   )rW   rX   rY   __doc____annotations__r   rg   rh   ri   rq   rU   r   propertyr=   r5   r   r   rH   rp   r   r   r   r   r.   r.   r.   r/   r^      s4   
 


"



 



Xr^   obj+TypeIs[PyTreeSpec | python_pytree.TreeSpec]c                C  s   t | ttjfS r   )r   r^   python_pytreeTreeSpec)r   r.   r.   r/   _is_pytreespec_instance  s   r   c              	   C  s   t dd d dd | ddS )Nr.   r1   r2   )r^   r2   r.   r.   r/   r     s   r   r.   iterableIterable[PyTreeSpec]c            	     s   t | }tdd |D rtd|dtfdd|D r+td d|dt fdd|D rAtd	 d|dtjjt  d
}|d usOJ tt |t d t tt||j	 dS )Nc                 s      | ]}t | V  qd S r   r   rs   rp   r.   r.   r/   	<genexpr>      z!treespec_tuple.<locals>.<genexpr>z&Expected a tuple of PyTreeSpecs, got: r   c                 3      | ]}|j  kV  qd S r   r3   r   r   r.   r/   r     r   YAll children PyTreeSpecs must have the same `none_is_leaf` value as the parent; expected , got: c                 3      | ]
}|j  d fvV  qdS r1   Nr9   r   r9   r.   r/   r         VAll children PyTreeSpecs must have the same `namespace` value as the parent; expected r9   r2   )
r   anyr-   r:   r;   r<   r^   rangerl   unflatten_func)r   r3   r4   rH   r   r.   r4   r3   r/   r     s<   r   mapping;Mapping[Any, PyTreeSpec] | Iterable[tuple[Any, PyTreeSpec]]c         	   	     s   t | fi |}tdd | D rtd|dtfdd| D r3td d|dt fdd| D rKtd	 d|dtj| d
\}}}}tt|t ||| d
S )Nc                 s  r   r   r   r   r.   r.   r/   r     r   z treespec_dict.<locals>.<genexpr>z)Expected a dictionary of TreeSpecs, got: r   c                 3  r   r   r   r   r   r.   r/   r     r   r   r   c                 3  r   r   r9   r   r9   r.   r/   r   	  r   r   r2   )dictr   valuesr-   r:   rC   r^   r   )	r   r3   r4   r*   dctrH   r   r   r   r.   r   r/   r     sL   
r   tuple[list[Any], PyTreeSpec]c                 s*   d	 fdd g } | |}||fS )
NrG   r   r   rI   r+   r^   c              	     s|   t | dr |  tdd d dd dS tj| d\}}}}t fdd|D }t|t| |||dS )NrA   r.   r2   c                 3  s    | ]} |V  qd S r   r.   r   )ru   r   r.   r/   r   Q  r   z/tree_flatten.<locals>.helper.<locals>.<genexpr>)r   r   r^   r:   rC   r   r=   )rG   r   rH   r   r   r   subspecsru   r5   r4   r3   )r   r/   ru   2  sJ   
	ztree_flatten.<locals>.helper)rG   r   r   rI   r+   r^   r.   r7   r5   r3   r4   r   rr   r.   r   r/   r!   $  s   *
r!   leaf_predicatec                C     t | |||dS r@   r!   r7   r   r3   r4   r.   r.   r/   
_C_flattena     r   3tuple[list[tuple[Any, ...]], list[Any], PyTreeSpec]c               C  s"   t | |||d\}}| ||fS r@   )r!   r   r   r.   r.   r/   r"   v  s   
r"   c                C  r   r@   )r"   r   r.   r.   r/   _C_flatten_with_path  r   r   c               C  s   t | |||dd S )NrA   rk   r   r>   r.   r.   r/   r#     s   r#   rr   r   c                 C  s&   t | stdt|  d| |S )NzIExpected `treespec` to be an instance of PyTreeSpec but got item of type r   )r   	TypeErrorr=   r   )rr   r   r.   r.   r/   r$     s   
r$   )r(   skip_signature_checkrj   rH   Iterable[_T]c                C  s   t t|dkrtdd S )Nr   zExpected no children.)rl   rJ   r-   )r0   rH   r.   r.   r/   none_unflatten  s   r   torchr9   r   dict[_KT, _VT]>tuple[list[_VT], tuple[list[_KT], list[_KT]], tuple[_KT, ...]]c                  s8   t j } fdd|D }t }|||ft|fS )Nc                   r   r.   r.   r   r   r.   r/   rv     r   z dict_flatten.<locals>.<listcomp>)r:   utilstotal_order_sortedrJ   r   )r   sorted_keysr   original_keysr.   r   r/   dict_flatten  s   r   r   tuple[list[_KT], list[_KT]]r   Iterable[_VT]c                C  s*   | \}}t |}|t||dd |S r   )r   fromkeysupdater   )r   r   r   r   dr.   r.   r/   dict_unflatten  s   

r   )r)   r   r*   r   r+   r,   r   )
r5   r6   r7   r   r3   r,   r4   r8   r+   r,   )
r5   r6   r7   r   r3   r,   r4   r8   r+   r?   )
r5   r6   r7   r   r3   r,   r4   r8   r+   rI   )r   r   r+   r   )r3   r,   r4   r8   r+   r^   )r.   )r   r   r3   r,   r4   r8   r+   r^   )
r   r   r3   r,   r4   r8   r*   r^   r+   r^   )
r5   r6   r7   r   r3   r,   r4   r8   r+   r   )NFr1   )
r   r6   r3   r,   r4   r8   r7   r   r+   r   )
r5   r6   r7   r   r3   r,   r4   r8   r+   r   )
r   r6   r3   r,   r4   r8   r7   r   r+   r   )
r5   r6   r7   r   r3   r,   r4   r8   r+   r^   )rr   r^   r   r?   r+   r   )r0   rj   rH   r   r+   rj   )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_orderedr0   __name__func__python_implementation__globalsr   r   r    r8   rK   ry   r^   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   r.   r.   r.   r/   <module>   sP   (	



  
!+	7						