
    $ShH*                       U d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZmZ ddlm Z m!Z! g dZ" ede          Z#de$d<    ede          Z%de$d<    G d d          Z& ej'        e(          5  ddl)m&Z& ddd           n# 1 swxY w Y   	 d9dddd:d!Z*e*Z+d;d(Z,d<d,Z-d=d-Z.d>d3Z/d?d8Z0dS )@zIntegration with JAX.    )annotationsN)
itemgetter)FunctionType)AnyCallable)	TypeAlias)Arraylax)dtypes)	ArrayLike)tree_flattentree_unflatten)
PyTreeSpecPyTreeTypeVar)safe_ziptotal_order_sorted)ArrayLikeTree	ArrayTree
tree_ravelr   r   r   c                  R    e Zd ZU dZded<   ded<   ded<   ddZddZddZddZdS )HashablePartialz1A hashable version of :class:`functools.partial`.r   funcztuple[Any, ...]argszdict[str, Any]kwargsFunctionType | HashablePartialr   returnNonec               H   t          |          st          d|d          t          |t                    r-|j        | _        |j        |z   | _        i |j        || _        dS t          |t                    r|| _        || _        || _        dS t          d|d          )z.Construct a :class:`HashablePartial` instance.zExpected a callable, got .zExpected a function, got N)callable	TypeError
isinstancer   r   r   r   r   )selfr   r   r   s       ]/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/optree/integrations/jax.py__init__zHashablePartial.__init__G   s    ~~ 	CAAAABBBdO,, 		C	DI	D(DI3T[3F3DKKKl++ 	CDIDI DKKKAAAABBB    otherobjectboolc                   t          |          t          u o7| j        j        |j        j        k    o| j        | j        f|j        |j        fk    S N)typer   r   __code__r   r   )r#   r'   s     r$   __eq__zHashablePartial.__eq__W   sM    KK?* G	"ej&99GDK(UZ,FF	
r&   intc                   t          | j        j        | j        t	          t          | j                                        t          d                              f          S )Nr   )key)	hashr   r-   r   tupler   r   itemsr   )r#   s    r$   __hash__zHashablePartial.__hash__^   sS    	"	():):)<)<*Q--PPPQQ
 
 	
r&   c               F    i | j         |} | j        g | j        |R i |S r+   )r   r   r   )r#   r   r   s      r$   __call__zHashablePartial.__call__g   s:    *DK*6*ty5$)5d555f555r&   N)r   r   r   r   r   r   r   r   )r'   r(   r   r)   )r   r/   )r   r   r   r   r   r   )	__name__
__module____qualname____doc____annotations__r%   r.   r5   r7    r&   r$   r   r   @   s         ;;C C C C 
 
 
 

 
 
 
6 6 6 6 6 6r&   r   )r   F )none_is_leaf	namespaceis_leafCallable[[Any], bool] | Nonetreer?   r)   r@   strr   *tuple[Array, Callable[[Array], ArrayTree]]c                  t          | |||          \  }}t          |          \  }}|t          t          ||          fS )a/  Ravel (flatten) a pytree of arrays down to a 1D array.

    >>> tree = {
    ...     'layer1': {
    ...         'weight': jnp.arange(0, 6, dtype=jnp.float32).reshape((2, 3)),
    ...         'bias': jnp.arange(6, 8, dtype=jnp.float32).reshape((2,)),
    ...     },
    ...     'layer2': {
    ...         'weight': jnp.arange(8, 10, dtype=jnp.float32).reshape((1, 2)),
    ...         'bias': jnp.arange(10, 11, dtype=jnp.float32).reshape((1,)),
    ...     },
    ... }
    >>> tree  # doctest: +IGNORE_WHITESPACE
    {
        'layer1': {
            'weight': Array([[0., 1., 2.],
                             [3., 4., 5.]], dtype=float32),
            'bias': Array([6., 7.], dtype=float32)
        },
        'layer2': {
            'weight': Array([[8., 9.]], dtype=float32),
            'bias': Array([10.], dtype=float32)
        }
    }
    >>> flat, unravel_func = tree_ravel(tree)
    >>> flat
    Array([ 6.,  7.,  0.,  1.,  2.,  3.,  4.,  5., 10.,  8.,  9.], dtype=float32)
    >>> unravel_func(flat)  # doctest: +IGNORE_WHITESPACE
    {
        'layer1': {
            'weight': Array([[0., 1., 2.],
                             [3., 4., 5.]], dtype=float32),
            'bias': Array([6., 7.], dtype=float32)
        },
        'layer2': {
            'weight': Array([[8., 9.]], dtype=float32),
            'bias': Array([10.], dtype=float32)
        }
    }

    Args:
        tree (pytree): a pytree of arrays and scalars to ravel.
        is_leaf (callable, optional): An optionally specified function that will be called at each
            flattening step. It should return a boolean, with :data:`True` stopping the traversal
            and the whole subtree being treated as a leaf, and :data:`False` indicating the
            flattening should traverse the current object.
        none_is_leaf (bool, optional): Whether to treat :data:`None` as a leaf. If :data:`False`,
            :data:`None` is a non-leaf node with arity 0. Thus :data:`None` is contained in the
            treespec rather than in the leaves list and :data:`None` will be remain in the result
            pytree. (default: :data:`False`)
        namespace (str, optional): The registry namespace used for custom pytree node types.
            (default: :const:`''`, i.e., the global namespace)

    Returns:
        A pair ``(array, unravel_func)`` where the first element is a 1D array representing the
        flattened and concatenated leaf values, with ``dtype`` determined by promoting the
        ``dtype``\s of leaf values, and the second element is a callable for unflattening a 1D array
        of the same length back to a pytree of the same structure as the input ``tree``. If the
        input pytree is empty (i.e. has no leaves) then as a convention a 1D empty array of the
        default dtype is returned in the first component of the output.
    )rA   r?   r@   )r   _ravel_leavesr   _tree_unravel)rC   rA   r?   r@   leavestreespecflatunravel_flats           r$   r   r   q   sU    J $!	  FH 'v..D,,GGGGr&   rJ   r   rL   "Callable[[Array], list[ArrayLike]]rK   r	   c               4    t          |  ||                    S r+   )r   )rJ   rL   rK   s      r$   rH   rH      s     (LL$6$6777r&   rI   list[ArrayLike]0tuple[Array, Callable[[Array], list[ArrayLike]]]c               H   | st          j        d          t          fS t          d | D                       }t	          j        | t          d | D                       }t          d | D                       }t          t          j        |                    }t          fd|D                       r6t          j	        d | D                       }|t          t          ||          fS t          j	        fd| D                       }|t          t          |||          fS )Nr   c              3  >   K   | ]}t          j        |          V  d S r+   )r   dtype.0leafs     r$   	<genexpr>z _ravel_leaves.<locals>.<genexpr>   s,      >>tT**>>>>>>r&   c              3  >   K   | ]}t          j        |          V  d S r+   )jnpsizerT   s     r$   rW   z _ravel_leaves.<locals>.<genexpr>   s*      44T#(4..444444r&   c              3  >   K   | ]}t          j        |          V  d S r+   )rY   shaperT   s     r$   rW   z _ravel_leaves.<locals>.<genexpr>   s*      66t39T??666666r&   c              3  $   K   | ]
}|k    V  d S r+   r=   )rU   dtto_dtypes     r$   rW   z _ravel_leaves.<locals>.<genexpr>   s'      
0
0b2>
0
0
0
0
0
0r&   c                6    g | ]}t          j        |          S r=   )rY   ravelrT   s     r$   
<listcomp>z!_ravel_leaves.<locals>.<listcomp>   s     "F"F"Ft39T??"F"F"Fr&   c                ^    g | ])}t          j        t          j        |                    *S r=   )rY   ra   r
   convert_element_type)rU   rV   r_   s     r$   rb   z!_ravel_leaves.<locals>.<listcomp>   s0    PPP3+D(;;	<	<PPPr&   )rY   zeros_unravel_emptyr3   r   result_type	itertools
accumulateallconcatenater   _unravel_leaves_single_dtype_unravel_leaves)rI   from_dtypessizesshapesindicesraveledr_   s         @r$   rG   rG      sG     .	!n-->>v>>>>>K!;/H44V44444E66v66666FI(//00G

0
0
0
0K
0
0
000 
 /"F"Fv"F"F"FGG8'6JJ
 	
 oPPPPPPP G 	&+xPP r&   c                   t          j        |           dk    r(t          dd dt          j        |            d          g S )N)r   0The unravel function expected an array of shape , got shape r   )rY   r\   
ValueError)rK   s    r$   rf   rf      sQ    
y$ctccQTQZ[_Q`Q`ccc
 
 	
 Ir&   rq   tuple[int, ...]rp   tuple[tuple[int, ...], ...]list[Array]c                  t          j        |          | d         fk    r/t          d| d         f dt          j        |           d          t          j        || d d                   }d t	          ||          D             S )Nrt   ru   r   c                >    g | ]\  }}|                     |          S r=   )reshape)rU   chunkr\   s      r$   rb   z0_unravel_leaves_single_dtype.<locals>.<listcomp>  s(    NNN\UEEMM%  NNNr&   )rY   r\   rv   splitr   )rq   rp   rK   chunkss       r$   rl   rl      s     y72;.((,~ , ,4, , ,
 
 	

 YtWSbS\**FNNXff5M5MNNNNr&   rn   tuple[jnp.dtype, ...]r_   	jnp.dtypec                  t          j        |          | d         fk    r/t          d| d         f dt          j        |           d          t          j        |          }||k    rt          d| d| d          t          j        || d d                   }t          j                    5  t          j        d           d t          |||          D             cd d d            S # 1 swxY w Y   d S )	Nr{   rt   ru   r   z0The unravel function expected an array of dtype z, got dtype ignorec                f    g | ].\  }}}t          j        |                    |          |          /S r=   )r
   rd   r}   )rU   r~   r\   rS   s       r$   rb   z#_unravel_leaves.<locals>.<listcomp>  sG     
 
 
#ue $U]]5%9%95AA
 
 
r&   )
rY   r\   rv   r   rS   r   warningscatch_warningssimplefilterr   )rq   rp   rn   r_   rK   array_dtyper   s          r$   rm   rm   	  sl    y72;.((,~ , ,4, , ,
 
 	
 ,t$$KhcxccU`ccc
 
 	
 YtWSbS\**F		 	"	" 
 
h'''
 
'/'L'L
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   //C++C/2C/r+   )
rA   rB   rC   r   r?   r)   r@   rD   r   rE   )rJ   r   rL   rM   rK   r	   r   r   )rI   rO   r   rP   )rK   r	   r   rO   )rq   rw   rp   rx   rK   r	   r   ry   )rq   rw   rp   rx   rn   r   r_   r   rK   r	   r   ry   )1r;   
__future__r   
contextlibrh   r   operatorr   typesr   typingr   r   typing_extensionsr   	jax.numpynumpyrY   jaxr	   r
   jax._srcr   
jax.typingr   
optree.opsr   r   optree.typingr   r   optree.utilsr   r   __all__r   r<   r   r   suppressImportErrorjax._src.utilr   ravel_pytreerH   rG   rf   rl   rm   r=   r&   r$   <module>r      s  @    # " " " " "                                      ' ' ' ' ' '                                 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 7
6
6 )=)DD D D D D$}[%88	 8 8 8 8)6 )6 )6 )6 )6 )6 )6 )6X Z%% . .------. . . . . . . . . . . . . . . -1LH
 LH LH LH LH LH LH^ 8 8 8 8! ! ! !H   O O O O 
 
 
 
 
 
s   %B88B<?B<