
    ij                     z    d dl mZ d dlmZ d ZddZd Zd Zdd	Z	d
 Z
d ZdddZd Zd Zd Zd Zd Zd Zy)    )defaultdict)_pytreec                 h    t        j                  | d | j                  | j                   d        | S )Nc                 "    | j                         S N)torchtree_flattenxs    r/var/www/html/emotional.easysim.app/public_html/venv/lib/python3.12/site-packages/keras/src/tree/torchtree_impl.py<lambda>z*register_tree_node_class.<locals>.<lambda>	   s    Q002     c                 "    | j                         S r   )torchtree_flatten_with_keysr	   s    r   r   z*register_tree_node_class.<locals>.<lambda>   s    q'D'D'F r   )
flatten_fnunflatten_fnserialized_type_nameflatten_with_keys_fn)
torch_treeregister_pytree_nodetorchtree_unflatten__name__)clss    r   register_tree_node_classr      s2    ##2,, #~F Jr   Nc                 b    |	 ||       ryt        j                  |       t         j                  vS )NT)r   _get_node_typeSUPPORTED_NODES)treeis_leafs     r   _tree_is_leafr      s-    wt}$$T**2L2LLLr   c                 z     d } fd} |       }| |       S t        |t              r|j                  dk(  ry |S )Nc           	      0   t        |       t        u r+t        | j                               D ci c]  }|| |   
 c}S t        |       t        u r?t	        | j
                  t        | j                               D ci c]  }|| |   
 c}      S y c c}w c c}w r   )typedictsortedkeysr   default_factory)r
   ks     r   funcz#_dict_to_ordered_dict.<locals>.func   s    7d?%+AFFH%56AqtG66!W#!!"("23QAaD3   7 4s   B9B
c                      t        j                  fd      \  } }|j                  dk(  r|j                  dk(  rS t        j                  | D cg c]  }t        |       c}|      S c c}w )Nc                     | uS r    r
   	structures    r   r   zB_dict_to_ordered_dict.<locals>.traverse_children.<locals>.<lambda>'       ay0 r   r      )r   tree_flatten	num_nodes
num_leavestree_unflatten_dict_to_ordered_dict)childrentreedefcr-   s      r   traverse_childrenz0_dict_to_ordered_dict.<locals>.traverse_children$   sn    &330
' !g&8&8A&=,,3;<a&q)< <s   A,MAP_TO_NONE)
isinstancer"   r   )r-   r(   r9   rets   `   r   r5   r5      sC     y/C
{ ""#t!>Jr   c                     t        |        S r   )r   )r-   s    r   	is_nestedr>   9   s    Y'''r   Tc                       fd}t              r        }| |       S  |       }  |      }||S t        |t              r|j                  dk(  ry |S )Nc            
          t        j                  fd      \  } }|j                  dk(  r|j                  dk(  rS t        j                  | D cg c]  }t        |       c}|      S c c}w )Nc                     | uS r   r+   r,   s    r   r   z5traverse.<locals>.traverse_children.<locals>.<lambda>A   r.   r   r/   r0   top_down)r   r1   r2   r3   r4   traverse)r6   r7   r8   r(   r-   rC   s      r   r9   z#traverse.<locals>.traverse_children>   sq    &330
' !g&8&8A&=,,?GH!$H5H Hs   A/r:   )r5   r;   r"   r   )r(   r-   rC   r9   r<   traversed_structures   ```   r   rD   rD   =   sj     &i0I9o;$&&/1&';&&#t!>Jr   c                 L    t        |       } t        j                  |       \  }}|S r   )r5   r   r1   )r-   leaves_s      r   flattenrI   [   s'     &i0I''	2IFAMr   c                    t        |       } t        j                  |       \  }}g }g }|D ]Q  \  }}|D ]G  }t        |t        j                        s|j
                  |vs-|j                  |j
                         I S t        |      }t        |      D 	ci c]  \  }}	|	|
 }
}}	|D ]  \  }}g }|D ]  }t        |t        j                        r|j                  |j                         9t        |t        j                        r|j                  |j                         ot        |t        j                        s|j                  |
|j
                             |j                  t        |      |f        |S c c}	}w r   )r5   r   tree_flatten_with_pathr;   
GetAttrKeynameappendr$   	enumerateSequenceKeyidx
MappingKeykeytuple)r-   leaves_with_pathrH   resultsfieldsrS   leafr'   iffield_to_idxpaths               r   flatten_with_pathr]   c   sU    &i0I$;;IFaGF% &	T 	&A!Z223f8Laff%	&& F^F%.v%67TQAqD7L7% 
,	T 	2A!Z334AEE"Az445AEE"Az445L01	2 	dT*+
, N 8s   E=)none_is_leafc                `     |st        d       }|s fd}|}t        j                  |g| S )N#Must provide at least one structurec                  \    | d   "t        d | D              st        d|  d      y  |  S )Nr   c              3   $   K   | ]  }|d u  
 y wr   r+   ).0ss     r   	<genexpr>z<map_structure.<locals>.func_skipping_none.<locals>.<genexpr>   s     3193s   zQStructure mismatch: some arguments are None, others are not. Received arguments: .)all
ValueError)argsr(   s    r   func_skipping_nonez)map_structure.<locals>.func_skipping_none   sH    Aw3d33$88<vQ@  ;r   rh   r   tree_map)r(   r^   
structuresmap_funcrj   s   `    r   map_structurero   ~   s<    >??H		 &x5*55r   c                 V    |st        d      fd}t        j                  || g| S )Nr`   c                 :    t        |       st        d       | S Nz0Structures don't have the same nested structure.r   rh   )shallowri   r(   s     r   )func_with_check_without_shallow_structurezFmap_structure_up_to.<locals>.func_with_check_without_shallow_structure   s!    W%OPPT{r   rk   )shallow_structurer(   rm   ru   s    `  r   map_structure_up_torw      s<    >??
 1 
 r   c                 8    d }t        j                  || |       y )Nc                 F    t        |       rt        |      st        d      y rr   rs   )a_leafb_leafs     r   checkz$assert_same_structure.<locals>.check   s     V$M&,AOPPr   )r   rl   )abr|   s      r   assert_same_structurer      s    
 q!$r   c                 H   t        t        |       D cg c]  \  }}|	 c}}      }t        t        |      D cg c]  \  }}|	 c}}      }||k7  rCd}|j                  |      }|r|d| z  }|j                  |      }|r|d| z  }t        |      y c c}}w c c}}w )Nz&`a` and `b` don't have the same paths.z
Paths in `a` missing in `b`:
z
Paths in `b` missing in `a`:
)setr]   
differencerh   )	r}   r~   r\   rH   a_pathsb_pathsmsga_diffb_diffs	            r   assert_same_pathsr      s    '8';<GD!4<=G'8';<GD!4<=G'6##G,5fX>>C##G,5fX>>Co  =<s
   B
B
c                 t    t        |       } t        j                  |       \  }}t        j                  ||      S r   )r5   r   r1   r4   )r-   flat_sequencerH   treespecs       r   pack_sequence_asr      s5     &i0I)))4KAx$$]H==r   c                 $    d }t        || d      S )Nc                 <    t        | t              rt        |       S d S r   )r;   listrT   )instances    r   list_to_tuplez&lists_to_tuples.<locals>.list_to_tuple   s    ",Xt"<uXF$Fr   FrB   )rD   )r-   r   s     r   lists_to_tuplesr      s    G M9u==r   c                 N    d }t        |      }t        j                  | ||      S )Nc                 V    t        | t        t        f      xr t        d | D              S )Nc              3   R   K   | ]  }t        |t        t        d       f       ! y wr   )r;   intr"   )rc   es     r   re   z>map_shape_structure.<locals>.is_shape_tuple.<locals>.<genexpr>   s%      4
12Jq3T
+,4
s   %')r;   r   rT   rg   r	   s    r   is_shape_tuplez+map_shape_structure.<locals>.is_shape_tuple   s.    !dE]+ 
 4
674
 1
 	
r   r/   )r5   r   rl   )r(   r-   r   s      r   map_shape_structurer      s(    
 &i0ItYGGr   r   )T)collectionsr   torch.utilsr   r   r   r   r5   r>   rD   rI   r]   ro   rw   r   r   r   r   r   r+   r   r   <module>r      sY    # -MD(<6 37 6.$%>>	Hr   