o
    ߗi                  	   @   s  U d dl mZ d dlmZmZmZmZmZmZm	Z	m
Z
 d dlZd dlmZmZ eeegef Zeeegef Zi Zee
e ef ed< i Zee
e ee f ed< 	d#de
e ded	ee d
dfddZ	d$deded
ee fddZdeeef ded
ee fddZdee ded
ee fddZde	e ded
ee fddZdeded
ee fddZdeeef ded
efddZdee ded
efddZde	e ded
efdd Zdeded
efd!d"Zee ee ee!ee ee"ee ej#j$D ]Z%ee%ee qeeee dS )%    )
namedtuple)AnyCallableDictList
NamedTupleOptionalTupleTypeN)PyTreeTreeSpecSUPPORTED_NODESSUPPORTED_NODES_EXACT_MATCHclsflatten_fn_specflatten_fn_exact_match_specreturnc                 C   s   |t | < |t| < d S N)r   r   )r   r   r    r   N/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/torch/fx/_pytree.pyregister_pytree_flatten_spec   s   r   Fpytreespecc           
      C   s   |  r| gS |jtvrtt|  dt|j }|| |}|r7t|j }|r7|| |s7td|  d| g }t||jD ]\}}t|||}	||	7 }q?|S )Nz does not have a flatten_fn_spec associated with it. Please register one with torch.fx._pytree.register_pytree_flatten_spec.  If you have serialized your model, make sure that any custom pytrees have been registered before loading it.zCannot flatten pytree z, given spec: )is_leaftyper   RuntimeErrorr   zipchildren_specstree_flatten_spec)
r   r   exact_structural_matchr   child_pytreesr   resultchild
child_specflatr   r   r   r      s(   




r   dc                    s    fdd|j D S )Nc                       g | ]} | qS r   r   ).0kr%   r   r   
<listcomp>7       z&_dict_flatten_spec.<locals>.<listcomp>)contextr%   r   r   r)   r   _dict_flatten_spec6   s   r.   c                        fddt |jD S )Nc                    r&   r   r   r'   ir)   r   r   r*   ;   r+   z&_list_flatten_spec.<locals>.<listcomp>rangenum_childrenr-   r   r)   r   _list_flatten_spec:      r5   c                    r/   )Nc                    r&   r   r   r0   r)   r   r   r*   ?   r+   z'_tuple_flatten_spec.<locals>.<listcomp>r2   r-   r   r)   r   _tuple_flatten_spec>   r6   r7   c                    r/   )Nc                    r&   r   r   r0   r)   r   r   r*   C   r+   z,_namedtuple_flatten_spec.<locals>.<listcomp>r2   r-   r   r)   r   _namedtuple_flatten_specB   r6   r8   c                 C      t | |jkS r   lenr4   r-   r   r   r   _dict_flatten_spec_exact_matchF      r<   c                 C   r9   r   r:   r-   r   r   r   _list_flatten_spec_exact_matchJ   r=   r>   c                 C   r9   r   r:   r-   r   r   r   _tuple_flatten_spec_exact_matchN   r=   r?   c                 C   r9   r   r:   r-   r   r   r   $_namedtuple_flatten_spec_exact_matchR   r=   r@   r   )F)&collectionsr   typingr   r   r   r   r   r   r	   r
   torch.return_typestorchtorch.utils._pytreer   r   FlattenFuncSpecboolFlattenFuncExactMatchSpecr   __annotations__r   r   r   r.   r5   r7   r8   r<   r>   r?   r@   dictlisttuplereturn_typesall_return_typesreturn_typer   r   r   r   <module>   sh   (

"