o
    Ih@=                     @  s  U 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mZ ddlmZ ddlm  mZ ddlmZmZ d	d
lmZ erRddlZddlmZ ddlmZ g Zded< ejrSddlZddlZddl m  m!Z" eruddl m#Z# eej$j%dddCddZ&dZ'dD ]Z'e(ee'Z)ee)dde)j* [)q['ee"j+dd	dDdEdd Z+ee"j,d!d	dDdFd#d$Z,ed$g7 Zee"j-dd	dDdGd&d'Z-ed'g7 ZG d(d) d)e.Z/e/ Z0[/edd*G d+d, d,Z1e1d-ddd-dZ2dHd0d1Z3ee"j4d!d	dDdId3d4Z4ed4g7 Zee"j5d!d	dDdJd5d6Z5ed6g7 Zee"j6d!ddKd9d:Z6ed:g7 Zee"j7dddd;dLd?d@Z7ed@g7 Zee"j8dddd;dLdAdBZ8edBg7 ZdS dS )Mz)
Python polyfills for torch.utils.pytree
    )annotations)deque)	dataclassfield)AnyCallableLiteralTYPE_CHECKING)TypeIsN)BUILTIN_TYPESSTANDARD_DICT_TYPES   )substitute_in_graph)Iterable)Selfz	list[str]__all__)PyTreeT)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   R/var/www/vscode/kcb/lib/python3.10/site-packages/torch/_dynamo/polyfills/pytree.py_$   s   r    )is_namedtupleis_namedtuple_classis_namedtuple_instanceis_structseqis_structseq_classis_structseq_instancenamedtuple_fieldsstructseq_fieldstreer   is_leafCallable[[PyTree], bool] | Nonec                 C  s<   | d u s|d ur|| rdS t jjt| ddd u rdS dS )NTtorch	namespaceF)optreeregister_pytree_nodegettyper%   r&   r   r   r   tree_is_leafB   s
   r0   FIterable[Any]c                 c  sX    | g}|r*|  }t||dr|V  qtj||ddd^}}|t| |sd S d S )Nr&   Tr(   r&   none_is_leafr*   )popr0   r+   tree_flatten_one_levelextendreversed)r%   r&   stacknodechildrenr   r   r   r   	tree_iterM   s   
r<   	list[Any]c                 C  s   t t| |dS )Nr2   )listr<   r/   r   r   r   tree_leavesc   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   rD   o   s   z_Asterisk.__new__strc                 C  s   dS rA   r   selfr   r   r   __repr__r   s   z_Asterisk.__repr__)r   r   r   rH   )__name__
__module____qualname__	__slots__rD   rK   __classcell__r   r   rF   r   r@   l   s    r@   )frozenc                   @  s  e Zd ZU dZ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< eddZ	ded< eddZ
ded< d9ddZd:ddZd;ddZed<dd Zd=d"d#Zd>d%d&Zd?d(d)Zd@d+d,ZdAd-d.ZdBd2d3ZdCd6d7Zd8S )D
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_funcF)initint	num_nodes
num_leavesnum_childrenzLiteral[True]r4   zLiteral['torch']r*   r   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 tdd | jD dd}tdd | jD }t| j}t	| d| t	| d	| t	| d
| t	| dd t	| dd d S )Nr   r      c                 s      | ]}|j V  qd S N)r\   .0specr   r   r   	<genexpr>       z+PyTreeSpec.__post_init__.<locals>.<genexpr>)startc                 s  ra   rb   r]   rc   r   r   r   rf      rg   r\   r]   r^   r4   Tr*   r(   )
rV   lenrT   rW   rX   rY   callablesumobject__setattr__)rJ   r\   r]   r^   r   r   r   __post_init__   s"   

zPyTreeSpec.__post_init__rH   c                   s&   d
 fdd d |  d| j d	S )NtreespecrS   r   rH   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 s6t| js6t	| jr=| | j
|S d| jj d| j
dd| dS )Nc                   s   g | ]} |qS r   r   )rd   subspechelperr   r   
<listcomp>   s    z7PyTreeSpec.__repr__.<locals>.helper.<locals>.<listcomp>zCustomTreeNode([z], [z, z]))r&   r.   	_asteriskrk   rY   rT   r   r+   r   r!   rW   rM   join)rp   children_representationsrr   r   r   rs      s*   



z#PyTreeSpec.__repr__.<locals>.helperzPyTreeSpec(z, NoneIsLeaf, namespace=))rp   rS   r   rH   r)   rI   r   rr   r   rK      s   zPyTreeSpec.__repr__c                 C     | j S rb   ri   rI   r   r   r   __len__   s   zPyTreeSpec.__len__c                 C  rz   rb   )rV   rI   r   r   r   r.      s   zPyTreeSpec.typer   c                 C  s   | j dko	| jdkS )Nr`   )r\   r]   rI   r   r   r   r&      s   zPyTreeSpec.is_leaflist[PyTreeSpec]c                 C  
   t | jS rb   )r>   rT   rI   r   r   r   r;         
zPyTreeSpec.childrenindexc                 C  
   | j | S rb   )rT   rJ   r   r   r   r   child   r~   zPyTreeSpec.childr=   c                 C  r}   rb   )r>   rX   rI   r   r   r   entries   r~   zPyTreeSpec.entriesc                 C  r   rb   )rX   r   r   r   r   entry   r~   zPyTreeSpec.entryr%   r   list[PyTree]c                   s"   d fd	d
 g } | || |S )Nrp   rS   r:   r   subtreesr   r   r_   c                   s  |   r|  d S t }| jtvrT|| jkr%td| jd|dtj ddd^}}}t|| jkrEtd| j dt| d|| j	krStd| jdn| jt
v o\|t
v }|sp|| jkrpt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 ddd^}}}|tur|| j	krtd| jd| j	d|dt|| jD ]
\}}||| qd S )NzType mismatch; expected z
, but got .Tr(   )r4   r*   zNode arity mismatch; expected z+Node context mismatch for custom node type zNode type mismatch; expected r   z; missing key(s): z; extra key(s): zNode keys mismatchc                   s   g | ]} | qS r   r   )rd   keyr:   r   r   rt     s    z<PyTreeSpec.flatten_up_to.<locals>.helper.<locals>.<listcomp>z%Node metadata mismatch for node type z; expected )r&   appendr.   r   r   r+   r6   rj   r^   rW   r   r   set
differencer   ziprT   )rp   r:   r   	node_typer;   metadatar   both_standard_dictexpected_keysgot_key_setexpected_key_setmissing_keys
extra_keysmessagesubtreerq   rr   r   r   rs      s   








z(PyTreeSpec.flatten_up_to.<locals>.helper)rp   rS   r:   r   r   r   r   r_   r   )rJ   r%   r   r   rr   r   flatten_up_to   s   TzPyTreeSpec.flatten_up_toleavesr1   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   )
isinstancer>   tuplerj   r]   r   r&   rT   r   	unflattenrk   rY   rW   )rJ   r   rh   endr   rq   r   r   r   r   )  s*   

zPyTreeSpec.unflattenN)r   r_   rL   )r   r[   )r   rU   )r   r   )r   r|   )r   r[   r   rS   )r   r=   )r   r[   r   r   )r%   r   r   r   )r   r1   r   r   )rM   rN   rO   __doc____annotations__r   r\   r]   r^   r4   r*   ro   rK   r{   propertyr.   r&   r;   r   r   r   r   r   r   r   r   r   rS   x   s0   
 








YrS   r   objTypeIs[PyTreeSpec]c                C  s
   t | tS rb   )r   rS   )r   r   r   r   _is_pytreespec_instanceC  r~   r   tuple[list[Any], PyTreeSpec]c                   s&   d	 fdd g } | |}||fS )
Nr:   r   r   r=   r   rS   c                   s`   t | dr |  tS tj| ddd\}}}}t fdd|D }t|t| |||S )Nr2   Tr(   r3   c                 3  s    | ]} |V  qd S rb   r   )rd   r   )rs   r   r   r   rf   b  s    z/tree_flatten.<locals>.helper.<locals>.<genexpr>)r0   r   
_LEAF_SPECr+   r6   r   rS   r.   )r:   r   r;   r   r   unflatten_funcsubspecsrs   r&   )r   r   rs   P  s    
	ztree_flatten.<locals>.helper)r:   r   r   r=   r   rS   r   )r%   r&   r   rp   r   r   r   tree_flattenF  s   

r   c                 C  s   t | |dd S )Nr2   r`   )r   r/   r   r   r   tree_structurek  s   
r   r   rp   c                 C  s&   t |stdt| d|| S )Nzhtree_unflatten(leaves, treespec): Expected `treespec` to be instance of PyTreeSpec but got item of type r   )r   	TypeErrorr.   r   )r   rp   r   r   r   tree_unflatteny  s   
r   r2   funcCallable[..., Any]restsc                  s>   t ||d\} |g fdd|D  } t| g|R  S )Nr2   c                      g | ]}  |qS r   r   rd   rrp   r   r   rt         ztree_map.<locals>.<listcomp>)r   r   mapr   r%   r&   r   r   	flat_argsr   r   r   tree_map  s   r   c                  sD   t ||d\} |g fdd|D  }tt| g|R  dd |S )Nr2   c                   r   r   r   r   r   r   r   rt     r   ztree_map_.<locals>.<listcomp>r   )maxlen)r   r   r   r   r   r   r   	tree_map_  s   r   )r   r   r   r   r   r   rb   )r%   r   r&   r'   r   r   )r%   r   r&   r'   r   r1   )r%   r   r&   r'   r   r=   )r   r   r   r   )r%   r   r&   r'   r   r   )r%   r   r&   r'   r   rS   )r   r1   rp   rS   r   r   )
r   r   r%   r   r   r   r&   r'   r   r   )9r   
__future__r   collectionsr   dataclassesr   r   typingr   r   r   r	   typing_extensionsr
   torch.utils._pytreeutils_pytreepython_pytreer   r   
decoratorsr   builtinscollections.abcr   r   r   r   _cxx_pytree_dynamo_traceabler+   	optree._Ctorch.utils._cxx_pytree_cxx_pytree
cxx_pytreer   _Cis_dict_insertion_orderedr   __namegetattr__func__python_implementation__r0   r<   r?   rH   r@   rv   rS   r   r   r   r   r   r   r   r   r   r   r   <module>   s    





	 I





   