§
    5P„jß  ã                  ó„  — d Z ddlmZ ddlZddlZddlmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ dd	lmZmZ erdd
lmZ ddgZ ej        e¦  «        5  ddlmZ edgz  Zddd¦  «         n# 1 swxY w Y    G d„ d¦  «        Z ej        ej        ¬¦  «         G d„ dej        ee         ¦  «        ¦   «         ZdS )z)PyTree integration with :mod:`functools`.é    )ÚannotationsN)ÚTYPE_CHECKINGÚAnyÚCallableÚClassVar)ÚSelf)Úregistry)ÚGetAttrEntry)Útree_reduce)ÚCustomTreeNodeÚT)ÚPyTreeEntryÚpartialÚreduce)ÚPlaceholderr   c                  óh   — e Zd ZU dZdZded<   ded<   ded<   d	ed
<   dd„Zdd„Zdd„Zdd„Z	d d„Z
dS )!Ú_HashablePartialShimzzA shim object that delegates :meth:`__call__`, :meth:`__eq__`, and :meth:`__hash__` to a :func:`functools.partial` object.)ÚargsÚfuncÚkeywordsÚpartial_funczClassVar[tuple[str, ...]]Ú	__slots__úCallable[..., Any]r   ztuple[Any, ...]r   zdict[str, Any]r   r   úfunctools.partialÚreturnÚNonec               ó   — || _         d S ©N©r   )Úselfr   s     úU/root/disk_3tb/conda_envs/Huy-visual/lib/python3.11/site-packages/optree/functools.pyÚ__init__z_HashablePartialShim.__init__8   s   € Ø/;ˆÔÐÐó    r   Úkwargsc               ó   —  | j         |i |¤ŽS r   r   )r    r   r$   s      r!   Ú__call__z_HashablePartialShim.__call__;   s   € Ø ˆtÔ  $Ð1¨&Ð1Ð1Ð1r#   ÚotherÚobjectÚboolc               ób   — t          |t          ¦  «        r| j        |j        k    S | j        |k    S r   )Ú
isinstancer   r   )r    r'   s     r!   Ú__eq__z_HashablePartialShim.__eq__>   s4   € Ý�eÕ1Ñ2Ô2ð 	;ØÔ$¨Ô(:Ò:Ð:ØÔ  EÒ)Ð)r#   Úintc               ó*   — t          | j        ¦  «        S r   )Úhashr   ©r    s    r!   Ú__hash__z_HashablePartialShim.__hash__C   ó   € Ý�DÔ%Ñ&Ô&Ð&r#   Ústrc               ó*   — t          | j        ¦  «        S r   )Úreprr   r0   s    r!   Ú__repr__z_HashablePartialShim.__repr__F   r2   r#   N)r   r   r   r   )r   r   r$   r   r   r   )r'   r(   r   r)   )r   r-   ©r   r3   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r"   r&   r,   r1   r6   © r#   r!   r   r   /   s»   € € € € € € ð Eð  Eà+W€IÐWÐWÐWÑWàÐÐÑØÐÐÑØÐÐÑð<ð <ð <ð <ð2ð 2ð 2ð 2ð*ð *ð *ð *ð
'ð 'ð 'ð 'ð'ð 'ð 'ð 'ð 'ð 'r#   r   )Ú	namespacec                  óˆ   ‡ — e Zd ZU dZdZded<   ded<   ded<   d	ed
<   eZded<   dˆ fd„Zdd„Z	dd„Z
edd„¦   «         Zˆ xZS )r   aŒ  A version of :func:`functools.partial` that works in pytrees.

    Use it for partial function evaluation in a way that is compatible with transformations,
    e.g., ``partial(func, *args, **kwargs)``.

    (You need to explicitly opt-in to this behavior because we did not want to give
    :func:`functools.partial` different semantics than normal function closures.)

    For example, here is a basic usage of :class:`partial` in a manner similar to
    :func:`functools.partial`:

    >>> import operator
    >>> import torch
    >>> add_one = partial(operator.add, torch.ones(()))
    >>> add_one(torch.tensor([[1, 2], [3, 4]]))
    tensor([[2., 3.],
            [4., 5.]])

    Pytree compatibility means that the resulting partial function can be passed as an argument
    within tree-map functions, which is not possible with a standard :func:`functools.partial`
    function:

    >>> def call_func_on_cuda(f, *args, **kwargs):
    ...     f, args, kwargs = tree_map(lambda t: t.cuda(), (f, args, kwargs))
    ...     return f(*args, **kwargs)
    ...
    >>> # doctest: +SKIP
    >>> tree_map(lambda t: t.cuda(), add_one)
    optree.functools.partial(<built-in function add>, tensor(1., device='cuda:0'))
    >>> call_func_on_cuda(add_one, torch.tensor([[1, 2], [3, 4]]))
    tensor([[2., 3.],
            [4., 5.]], device='cuda:0')

    Passing zero arguments to :class:`partial` effectively wraps the original function, making it a
    valid argument in tree-map functions:

    >>> # doctest: +SKIP
    >>> call_func_on_cuda(partial(torch.add), torch.tensor(1), torch.tensor(2))
    tensor(3, device='cuda:0')

    Had we passed :func:`operator.add` to ``call_func_on_cuda`` directly, it would have resulted in
    a :class:`TypeError` or :class:`AttributeError`.

    On Python 3.14+, :data:`functools.Placeholder` can be used to reserve positional argument slots:

    >>> from functools import Placeholder  # doctest: +SKIP
    >>> square = partial(pow, Placeholder, 2)  # doctest: +SKIP
    >>> square(5)
    25

    :data:`~functools.Placeholder` objects are treated as leaves in the pytree and their identity is
    preserved through flatten/unflatten round-trips.
    r=   zClassVar[tuple[()]]r   r   r   ztuple[T, ...]r   zdict[str, T]r   zClassVar[type[PyTreeEntry]]ÚTREE_PATH_ENTRY_TYPEr   r   r   c               óR  •— t          |t          j        ¦  «        ro|}t          |¦  «        }t	          |d¦  «        r
J d¦   «         ‚ t          ¦   «         j        | |g|¢R i |¤Ž}|j        |_        |j        |_        |j	        |_	        |S  t          ¦   «         j        | |g|¢R i |¤ŽS )z'Create a new :class:`partial` instance.r   z3shimmed function should not have a `func` attribute)
r+   Ú	functoolsr   r   ÚhasattrÚsuperÚ__new__r   r   r   )Úclsr   r   r   Úoriginal_funcÚoutÚ	__class__s         €r!   rE   zpartial.__new__Ž   s½   ø€ õ �d�IÔ-Ñ.Ô.ð 	Ø ˆMÝ'¨Ñ6Ô6ˆDÝ˜t VÑ,Ô,ÐcÐcÐ.cÑcÔcÐ,Ø!•%‘'”'”/ # tÐ?¨dÐ?Ð?Ð?°hÐ?Ð?ˆCØ%Ô*ˆDŒIØ%Ô*ˆDŒIØ)Ô2ˆDŒMØˆJà�u‰wŒwŒ˜s DÐ<¨4Ð<Ð<Ð<°8Ð<Ð<Ð<r#   r3   c               óD  — t          | j        ¦  «        g}|                     d„ | j        D ¦   «         ¦  «         |                     d„ | j                             ¦   «         D ¦   «         ¦  «         | j        j        › d| j        j        › dd 	                    |¦  «        › d�S )z@Return a string representation of the :class:`partial` instance.c              3  ó4   K  — | ]}t          |¦  «        V — Œd S r   )r5   )Ú.0Úxs     r!   ú	<genexpr>z#partial.__repr__.<locals>.<genexpr>£   s(   è è € Ð/Ð/ •D˜‘G”GÐ/Ð/Ð/Ð/Ð/Ð/r#   c              3  ó*   K  — | ]\  }}|› d |›�V — ŒdS )ú=Nr=   )rL   ÚkÚvs      r!   rN   z#partial.__repr__.<locals>.<genexpr>¤   s0   è è € ÐEÐE¡V a¨�q�L�L˜1�L�LÐEÐEÐEÐEÐEÐEr#   ú.ú(z, ú))
r5   r   Úextendr   r   ÚitemsrI   r9   r:   Újoin)r    r   s     r!   r6   zpartial.__repr__    s˜   € å�T”Y‘”Ð ˆØ�ŠÐ/Ð/ T¤YÐ/Ñ/Ô/Ñ/Ô/Ð/Ø�ŠÐEÐE¨t¬}×/BÒ/BÑ/DÔ/DÐEÑEÔEÑEÔEÐEØ”.Ô+Ð^Ð^¨d¬nÔ.IÐ^Ð^ÈDÏIÊIÐVZÉOÌOÐ^Ð^Ð^Ð^r#   úNtuple[tuple[tuple[T, ...], dict[str, T]], Callable[..., Any], tuple[str, str]]c               ó.   — | j         | j        f| j        dfS )zAFlatten the :class:`partial` instance into children and metadata.)r   r   )r   r   r   r0   s    r!   Ú__tree_flatten__zpartial.__tree_flatten__§   s   € ð ”	˜4œ=Ð)¨4¬9Ð6JÐJÐJr#   ÚmetadataÚchildrenú"tuple[tuple[T, ...], dict[str, T]]c               ó$   — |\  }} | |g|¢R i |¤ŽS )zEUnflatten the children and metadata into a :class:`partial` instance.r=   )rF   r\   r]   r   r   s        r!   Ú__tree_unflatten__zpartial.__tree_unflatten__²   s-   € ð "‰ˆˆhØˆs�8Ð/˜dÐ/Ð/Ð/ hÐ/Ð/Ð/r#   )r   r   r   r   r   r   r   r   r7   )r   rY   )r\   r   r]   r^   r   r   )r8   r9   r:   r;   r   r<   r
   r@   rE   r6   r[   Úclassmethodr`   Ú__classcell__)rI   s   @r!   r   r   K   sã   ø€ € € € € € ð
4ð 4ðl &(€IÐ'Ð'Ð'Ñ'àÐÐÑØÐÐÑØÐÐÑà8DÐÐDÐDÐDÑDð=ð =ð =ð =ð =ð =ð$_ð _ð _ð _ð	Kð 	Kð 	Kð 	Kð ð0ð 0ð 0ñ „[ð0ð 0ð 0ð 0ð 0r#   )r;   Ú
__future__r   Ú
contextlibrB   Útypingr   r   r   r   Útyping_extensionsr   Úoptreer	   Úoptree.accessorsr
   Ú
optree.opsr   r   Úoptree.typingr   r   r   Ú__all__ÚsuppressÚImportErrorr   r   Úregister_pytree_node_classÚ__GLOBAL_NAMESPACEr   r=   r#   r!   ú<module>rp      s  ðð 0Ð /à "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø Ð Ð Ð Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð Ø )Ð )Ð )Ð )Ð )Ð )Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø +Ð +Ð +Ð +Ð +Ð +Ð +Ð +ð ð -Ø,Ð,Ð,Ð,Ð,Ð,ð Øð€ð €ZÔ˜Ñ%Ô%ð ð à%Ð%Ð%Ð%Ð%Ð%à�ˆÑ€Gð	ð ð ñ ô ð ð ð ð ð ð øøøð ð ð ð ð'ð 'ð 'ð 'ð 'ñ 'ô 'ð 'ð8 %€Ô$¨xÔ/JÐKÑKÔKðo0ð o0ð o0ð o0ð o0ØÔØ�1Ôño0ô o0ñ LÔKðo0ð o0ð o0s   ÁA2Á2A6Á9A6