
    $Sh                       d Z ddlm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 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reducec                  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 )!_HashablePartialShimz_Object that delegates :meth:`__call__`, :meth:`__eq__`, and :meth:`__hash__` to another 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     V/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/optree/functools.py__init__z_HashablePartialShim.__init__0   s    /;    r   kwargsc                    | j         |i |S r   r   )r   r   r#   s      r    __call__z_HashablePartialShim.__call__3   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__6   s4    e122 	;$(::: E))r"   intc               *    t          | j                  S r   )hashr   r   s    r    __hash__z_HashablePartialShim.__hash__;       D%&&&r"   strc               *    t          | j                  S r   )reprr   r/   s    r    __repr__z_HashablePartialShim.__repr__>   r1   r"   N)r   r   r   r   )r   r   r#   r   r   r   )r&   r'   r   r(   )r   r,   r   r2   )__name__
__module____qualname____doc__r   __annotations__r!   r%   r+   r0   r5    r"   r    r   r   '   s         ii+WI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`.
    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    rD   zpartial.__new__|   s     dI-.. 	 M'66DtV,,cc.ccc,!%''/#t?d???h??C%*DI%*DI)2DMJuwwsD<4<<<8<<<r"   r2   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   )r4   ).0xs     r    	<genexpr>z#partial.__repr__.<locals>.<genexpr>   s(      //DGG//////r"   c              3  *   K   | ]\  }}| d |V  dS )=Nr<   )rK   kvs      r    rM   z#partial.__repr__.<locals>.<genexpr>   s0      EEVaqLL1LLEEEEEEr"   .(z, ))
r4   r   extendr   r   itemsrH   r8   r9   join)r   r   s     r    r5   zpartial.__repr__   s    TY //TY//////EEt}/B/B/D/DEEEEEE.+^^dn.I^^DIIVZOO^^^^r"   Ntuple[tuple[tuple[T, ...], dict[str, T]], Callable[..., Any], tuple[str, str]]c               .    | j         | j        f| j        dfS )z?Flatten the :class:`partial` instance to children and metadata.)r   r   )r   r   r   r/   s    r    tree_flattenzpartial.tree_flatten   s     	4=)496J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<   )rE   r[   r\   r   r   s        r    tree_unflattenzpartial.tree_unflatten   s-     "hs8/d///h///r"   )r   r   r   r   r   r   r   r   r6   )r   rX   )r[   r   r\   r]   r   r   )r7   r8   r9   r:   r   r;   r
   r?   rD   r5   rZ   classmethodr_   __classcell__)rH   s   @r    r   r   C   s         
* *X &(I''''8DDDDD= = = = = =$_ _ _ _	K 	K 	K 	K 0 0 0 [0 0 0 0 0r"   )r:   
__future__r   rA   typingr   r   r   r   typing_extensionsr   optreer	   optree.accessorsr
   
optree.opsr   r   optree.typingr   r   r   __all__r   register_pytree_node_class__GLOBAL_NAMESPACEr   r<   r"   r    <module>rl      sm   0 / " " " " " "     9 9 9 9 9 9 9 9 9 9 9 9 " " " " " "       ) ) ) ) ) ) , , , , , , + + + + + + + +  -,,,,,, ' ' ' ' ' ' ' '8 %$x/JKKKe0 e0 e0 e0 e01e0 e0 LKe0 e0 e0r"   