+
    NV-jÒ,  ã                   óž   € ^ RI Ht ^ RIHt ^ RIHtHtHtHtH	t	H
t
Ht RR/R R lltRRRR/R	 R
 lltRR R lltR R ltRR ltRR ltR# )é    )Údefaultdict)Úzip_longest)ÚAnyÚCallableÚDictÚListÚOptionalÚTupleÚUnionÚis_leafNc          
      ój   € V ^8„  d   QhR\         R\        R\        R\        \         ,          R\        /# )é   ÚfnÚtreeÚrestr   Úreturn©r   r   r	   )Úformats   "ÚZ/Volumes/fast/ai/experiments/ui-tars-smoke/.venv/lib/python3.14/site-packages/mlx/utils.pyÚ__annotate__r      s9   € ÷ 3ñ 3Üð3Üð3Ü$'ð3Ü2:¼8Õ2Dð3äñ3ó    c          
     óÈ  a aaa€ Se   S! V4      '       d   S ! V.SO5!  # \        V\        \        34      '       dC   \        V4      pV VV3R l\	        V4       4       p\        VR4      '       d   V! V!  # V! V4      # \        V\        4      '       d@   VP                  4        UaUu/ uF!  w  opS\        S V.V3R lS 4       O5RS/ bK#  	  upp# S ! V.SO5!  # u uppi )a¡  Applies ``fn`` to the leaves of the Python tree ``tree`` and
returns a new collection with the results.

If ``rest`` is provided, every item is assumed to be a superset of ``tree``
and the corresponding leaves are provided as extra positional arguments to
``fn``. In that respect, :meth:`tree_map` is closer to :func:`itertools.starmap`
than to :func:`map`.

The keyword argument ``is_leaf`` decides what constitutes a leaf from
``tree`` similar to :func:`tree_flatten`.

.. code-block:: python

    import mlx.nn as nn
    from mlx.utils import tree_map

    model = nn.Linear(10, 10)
    print(model.parameters().keys())
    # dict_keys(['weight', 'bias'])

    # square the parameters
    model.update(tree_map(lambda x: x*x, model.parameters()))

Args:
    fn (callable): The function that processes the leaves of the tree.
    tree (Any): The main Python tree that will be iterated upon.
    rest (tuple[Any]): Extra trees to be iterated together with ``tree``.
    is_leaf (callable, optional): An optional callable that returns ``True``
       if the passed object is considered a leaf or ``False`` otherwise.

Returns:
    A Python tree with the new values returned by ``fn``.
c              3   ó`   <a"  € T F"  w  op\        SV.V3R  lS 4       O5RS/ x € K$  	  R# 5i)c              3   ó4   <"  € T F  qS,          x € K  	  R # 5i©N© ©Ú.0ÚrÚis   & €r   Ú	<genexpr>Ú%tree_map.<locals>.<genexpr>.<genexpr>1   s   øé € Ð!5±¨1 A§$¢$³ùó   ƒr   N)Útree_map)r   Úchildr    r   r   r   s   & @€€€r   r!   Útree_map.<locals>.<genexpr>0   s3   ùé € ð 
á+‘��5ô �R˜ÐGÔ!5±Ó!5ÒG¸wÖGÛ+ùs   „*.Ú_fieldsc              3   ó4   <"  € T F  qS,          x € K  	  R # 5ir   r   ©r   r   Úks   & €r   r!   r&   7   s   øé € Ð$8±4¨a q§T¢T³4ùr#   r   )	Ú
isinstanceÚlistÚtupleÚtypeÚ	enumerateÚhasattrÚdictÚitemsr$   )r   r   r   r   ÚTreeTypeÚsubtreesr*   r%   s   f&dj  ` r   r$   r$      sÜ   û€ ðH Ò™w tŸ}š}Ù�$ˆ˜‹ÐÜ	�Dœ4¤˜-×	(Ò	(Ü˜“:ˆö
ä% dœOó
ˆô '.¨d°I×&>Ò&>‰x˜Ñ"ÐVÁHÈXÓDVÐVÜ	�Dœ$×	Ò	ð !ŸJ™JœLõ
á(‘��5ð Œx˜˜EÐJÔ$8±4Ó$8ÒJÀ'ÑJÒJÙ(ò
ð 	
ñ
 �$ˆ˜‹Ðùó
s   Â)'CÚpathc                óŒ   € V ^8„  d   QhR\         R\        R\        R\        \         ,          R\        \        ,          R\        /# )r   r   r   r   r   r5   r   r   )r   s   "r   r   r   >   sM   € ÷ 5%ñ 5%Üð5%ä
ð5%ô ð5%ô ”hÕð	5%ô
 ”3�-ð5%ô 	ñ5%r   c               óà  a aaaa€ Se   S! V4      '       d   S ! W1.SO5!  # \        V\        \        34      '       d9   V'       d   V R2MRo\        V4      pV! V VVV3R l\	        V4       4       4      # \        V\
        4      '       dU   V'       d   V R2MRoVP                  4        UaUu/ uF'  w  opS\        S V.V3R lS 4       O5RSRS S 2/ bK)  	  upp# S ! W1.SO5!  # u uppi )aã  Applies ``fn`` to the path and leaves of the Python tree ``tree`` and
returns a new collection with the results.

This function is the same :func:`tree_map` but the ``fn`` takes the path as
the first argument followed by the remaining tree nodes.

Args:
    fn (callable): The function that processes the leaves of the tree.
    tree (Any): The main Python tree that will be iterated upon.
    rest (tuple[Any]): Extra trees to be iterated together with ``tree``.
    is_leaf (Optional[Callable]): An optional callable that returns ``True``
       if the passed object is considered a leaf or ``False`` otherwise.
    path (Optional[Any]): Prefix will be added to the result.

Returns:
    A Python tree with the new values returned by ``fn``.

Example:
    >>> from mlx.utils import tree_map_with_path
    >>> tree = {"model": [{"w": 0, "b": 1}, {"w": 0, "b": 1}]}
    >>> new_tree = tree_map_with_path(lambda path, _: print(path), tree)
    model.0.w
    model.0.b
    model.1.w
    model.1.b
Ú.Ú c           	   3   ól   <a"  € T F(  w  op\        SV.V3R  lS 4       O5RSRS S 2/ x € K*  	  R# 5i)c              3   ó4   <"  € T F  qS,          x € K  	  R # 5ir   r   r   s   & €r   r!   Ú/tree_map_with_path.<locals>.<genexpr>.<genexpr>f   ó   øé € Ð0©4 a˜qŸTšT«4ùr#   r   r5   N)Útree_map_with_path)r   r%   r    r   r   Úprefixr   s   & @€€€€r   r!   Ú%tree_map_with_path.<locals>.<genexpr>d   sQ   ùé € ð 
ñ ,‘��5ô Ø�EðÜ0©4Ó0òØ:AðØKQÈ(ÐSTÐRUÈöó ,ùs   „04c              3   ó4   <"  € T F  qS,          x € K  	  R # 5ir   r   r)   s   & €r   r!   r@   n   r=   r#   r   r5   )r+   r,   r-   r.   r/   r1   r2   r>   )	r   r   r   r5   r   r3   r*   r%   r?   s	   f&d$j ` @r   r>   r>   >   s  ü€ ðB Ò™w tŸ}š}Ù�$Ð$˜tÓ$Ð$Ü	�Dœ4¤˜-×	(Ò	(ß#�D�6˜‘¨ˆÜ˜“:ˆÙ÷ 
ô & dœOó	
ó 
ð 	
ô 
�Dœ$×	Ò	ß#�D�6˜‘¨ˆð
 !ŸJ™JœLõ	
ñ )‘��5ð Ô!Ø�EðÜ0©4Ó0òØ:AðØKQÈ(ÐSTÐRUÈñò ñ )ò	
ð 	
ñ �$Ð$˜tÓ$Ð$ùó
s   Â/-C*c                óx  € V ^8„  d   QhR\         R\        R\        \        ,          R\        \        \
        \        \        \         3,          ,          \        \        \         3,          3,          ,          R\        \
        \        \        \         3,          ,          \        \        \         3,          3,          /# )r   r   r?   r   Údestinationr   )r   Ústrr	   r   r   r   r
   r   )r   s   "r   r   r   v   s‰   € ÷ Iñ IÜ
ðIäðIô ”hÕðIô œ%¤¤U¬3´¨8¥_Õ 5´t¼CÄ¸Hµ~Ð EÕFÕGð	Iô
 Œ4””cœ3�h•Õ ¤$¤s¬C x¥.Ð0Õ1ñIr   c                ó.  € Vf   . p\        V\        4      '       d   VP                  pM.\        V\        4      '       d   VP                  pM\        R4      hVe#   V! V 4      '       d   V! VR,          V 3.4       V# \        V \        \        34      '       d(   \        V 4       F  w  rV\        Wa RV 2W#4       K  	  V# \        V \        4      '       d-   V P                  4        F  w  rx\        W� RV 2W#4       K  	  V# V! VR,          V 3.4       V# )a  Flattens a Python tree to a list of key, value tuples.

The keys are using the dot notation to define trees of arbitrary depth and
complexity.

.. code-block:: python

    from mlx.utils import tree_flatten

    print(tree_flatten([[[0]]]))
    # [("0.0.0", 0)]

    print(tree_flatten([[[0]]], prefix=".hello"))
    # [("hello.0.0.0", 0)]

    tree_flatten({"a": {"b": 1}}, destination={})
    {"a.b": 1}

.. note::
   Dictionaries should have keys that are valid Python identifiers.

Args:
    tree (Any): The Python tree to be flattened.
    prefix (str): A prefix to use for the keys. The first character is
        always discarded.
    is_leaf (callable): An optional callable that returns True if the
        passed object is considered a leaf or False otherwise.
    destination (list or dict, optional): A list or dictionary to store the
        flattened tree. If None an empty list will be used. Default: ``None``.

Returns:
    Union[List[Tuple[str, Any]], Dict[str, Any]]: The flat representation of
        the Python tree.
z;Destination should be either a list or a dictionary or None:é   NNr8   )
r+   r,   Úextendr1   ÚupdateÚ
ValueErrorr-   r/   Útree_flattenr2   )	r   r?   r   rC   Ú_add_to_destinationr    ÚitemÚkeyÚvalues	   &&&&     r   rJ   rJ   v   s  € ðP ÒØˆô
 �+œt×$Ò$Ø)×0Ñ0ÑÜ	�K¤×	&Ò	&Ø)×0Ñ0ÑäÐVÓWÐWð Ò™w tŸ}š}Ù˜f R�j¨$Ð/Ð0Ô1ØÐô �$œœu˜×&Ò&Ü  –‰GˆAÜ˜ ¨¨1¨#˜°ÖEñ 'àÐô �$œ×ÒØŸ*™*ž,‰JˆCÜ˜ (¨!¨C¨5Ð 1°7ÖHñ 'àÐñ ˜& �* dÐ+Ð,Ô-àÐr   c                ó¬   € V ^8„  d   QhR\         \        \        \        \        3,          ,          \
        \        \        3,          3,          R\        /# )r   r   r   )r   r   r
   rD   r   r   )r   s   "r   r   r   Â   s=   € ÷ /Cñ /Cœœt¤E¬#¬s¨(¥OÕ4´d¼3Ä¸8µnÐDÕEð /CÌ#ñ /Cr   c           	     ó  € \        V \        4      '       d   V P                  4       MT p\        V4      ^8X  d    \	        \        V4      4      w  r#VR8X  d   V# \        \        4      pV FD  w  r#VP                  R^R7      vrVV'       g   RMV^ ,          pWE,          P                  Wc34       KF  	   \        R VP                  4        4       4      p. pV F_  w  ršTP                  \        V	\        V4      ,
          4       Uu. uF  p/ NK  	  up4       VP                  \        WJ,          4      4       Ka  	  V# u upi   \         d9    TP                  4        U
Uu/ uF  w  r¬T
\        T4      bK  	  Mu upp
i upp
u # i ; i)aæ  Recreate a Python tree from its flat representation.

.. code-block:: python

    from mlx.utils import tree_unflatten

    d = tree_unflatten([("hello.world", 42)])
    print(d)
    # {"hello": {"world": 42}}

    d = tree_unflatten({"hello.world": 42})
    print(d)
    # {"hello": {"world": 42}}

Args:
    tree (list[tuple[str, Any]] or dict[str, Any]): The flat representation of a Python tree.
       For instance as returned by :meth:`tree_flatten`.

Returns:
    A Python tree.
r9   r8   )Úmaxsplitc              3   ó<   "  € T F  p\        V4      V3x € K  	  R # 5ir   )Úint)r   Úidxs   & r   r!   Ú!tree_unflatten.<locals>.<genexpr>é   s   é € ÐA±¨#”s˜3“x •o³ùs   ‚)r+   r1   r2   ÚlenÚnextÚiterr   r,   ÚsplitÚappendÚsortedÚkeysrG   ÚrangeÚtree_unflattenrI   )r   r2   rM   rN   ÚchildrenÚcurrent_idxÚnext_idxr\   Úlr    r*   Ú_Úvs   &            r   r^   r^   Â   sG  € ô, ' t¬T×2Ò2ˆD�J‰JŒL¸€Eô ˆ5ƒz�Q„Üœ$˜u›+Ó&‰
ˆØ�"Œ9ØˆLô œ4Ó €HÛ‰
ˆØ!$§¡¨3¸ Ó!;Ðˆß%‘2¨8°A­;ˆØÕ×$Ñ$ hÐ%6Ö7ñ ð	CÜÑA°·±´ÓAÓAˆØˆÛ‰DˆAà�H‰H¤%¨¬C°«F­
Ô"3Ó4Ñ"3˜Q“bÑ"3Ñ4Ô5Ø�H‰H”^ H¥KÓ0Ö1ñ ð ˆùò 5øô ô CØ19·±Ô1AÔBÑ1A©¨�”> !Ó$Ò$Ò1AùÖBÒBðCús1   Â2AE  ÄD;
Ä,E  Ä;E  Å FÅE6Å5FÆFc                ó>  € Ve   V! V4      '       d   Vf   V# V ! W!4      # Tp\        V\        \        34      '       d   V F  p\        WWC4      pK  	  V# \        V\        4      '       d&   VP                  4        F  p\        WWC4      pK  	  V# Vf   V# V ! WA4      # )až  Applies a reduction to the leaves of a Python tree.

This function reduces Python trees into an accumulated result by applying
the provided function ``fn`` to the leaves of the tree.

Example:
    >>> from mlx.utils import tree_reduce
    >>> tree = {"a": [1, 2, 3], "b": [4, 5]}
    >>> tree_reduce(lambda acc, x: acc + x, tree, 0)
    15

Args:
    fn (callable): The reducer function that takes two arguments (accumulator,
        current value) and returns the updated accumulator.
    tree (Any): The Python tree to reduce. It can be any nested combination of
        lists, tuples, or dictionaries.
    initializer (Any, optional): The initial value to start the reduction. If
        not provided, the first leaf value is used.
    is_leaf (callable, optional): A function to determine if an object is a
        leaf, returning ``True`` for leaf nodes and ``False`` otherwise.

Returns:
    Any: The accumulated value.
)r+   r,   r-   Útree_reducer1   Úvalues)r   r   Úinitializerr   ÚaccumulatorrL   s   &&&&  r   rf   rf   ô   s¥   € ð2 Ò™w tŸ}š}Ø"Ò*ˆtÐE±°;Ó0EÐEà€Kä�$œœu˜×&Ò&ÛˆDÜ% b°ÓEŠKñ ð Ðô 
�Dœ$×	Ò	Ø—K‘K–MˆDÜ% b°ÓEŠKñ "ð
 Ðð #Ò*ˆtÐE±°;Ó0EÐEr   c                ó*  a€ \        V \        \        \        34      '       d   \	        V 4      ^ 8X  d   Rp \        V\        \        \        34      '       d   \	        V4      ^ 8X  d   RpV f   Ve   V# V e   Vf   V # \        V \        \        34      '       dC   \        V\        \        34      '       d'   \        V 4      pV! V3R l\        W4       4       4      # \        V \        4      '       dŠ   \        V\        4      '       dt   \        V P                  4       4      \        VP                  4       4      ,           Uu/ uF1  pV\        V P                  VR4      VP                  VR4      S4      bK3  	  up# Sf   \        R4      hS! W4      # u upi )ad  Merge two Python trees in one containing the values of both. It can be
thought of as a deep dict.update method.

Args:
    tree_a (Any): The first Python tree.
    tree_b (Any): The second Python tree.
    merge_fn (callable, optional): A function to merge leaves.

Returns:
    The Python tree containing the values of both ``tree_a`` and
    ``tree_b``.
Nc              3   ó@   <"  € T F  w  r\        WS4      x € K  	  R # 5ir   )Ú
tree_merge)r   ÚaÚbÚmerge_fns   &  €r   r!   Útree_merge.<locals>.<genexpr>6  s!   øé € ð 
Ù3N©4¨1ŒJ�q˜X×&Ð&Ó3Nùs   ƒzOTrees contain elements at the same locations but no merge function was provided)r+   r1   r,   r-   rV   r.   r   Úsetr\   rl   ÚgetrI   )Útree_aÚtree_bro   r3   r*   s   &&f  r   rl   rl     s\  ø€ ô �&œ4¤¤uÐ-×.Ò.´3°v³;À!Ô3CØˆÜ�&œ4¤¤uÐ-×.Ò.´3°v³;À!Ô3CØˆØ‚~˜&Ò,ØˆØÒ˜fšnØˆä�&œ4¤˜-×(Ò(¬Z¸ÄÄuÀ×-NÒ-NÜ˜“<ˆÙô 
Ü3>¸vÔ3Nó
ó 
ð 	
ô 
�FœD×	!Ò	!¤j°¼×&>Ò&>ô ˜Ÿ™›Ó'¬#¨f¯k©k«mÓ*<Ö<ó
á<�ð Œz˜&Ÿ*™* Q¨Ó-¨v¯z©z¸!¸TÓ/BÀHÓMÒMÙ<ñ
ð 	
ð
 ÒÜð,óð ñ ˜Ó'Ð'ùò
s   Ä?7F)r9   NN)NNr   )Úcollectionsr   Ú	itertoolsr   Útypingr   r   r   r   r	   r
   r   r$   r>   rJ   r^   rf   rl   r   r   r   Ú<module>rx      sW   ðõ $Ý !ß D× DÑ Dð3ØGK÷3ðl5%ð #'ð	5%ð
 ÷5%÷pIõX/Côd'öT((r   