
    $Sh                        U d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
Z
ddlmZmZ ddlmZmZ ddlmZ d	d
gZ ed	e
j                  Zded	<   	 d/dddd0dZeZd1dZd2d#Zd3d$Zd4d)Zd5d.ZdS )6zIntegration with PyTorch.    )annotationsN)AnyCallable)	TypeAlias)tree_flattentree_unflatten)
PyTreeSpecPyTreeTypeVar)safe_zip
TensorTree
tree_ravelr   F )none_is_leaf	namespaceis_leafCallable[[Any], bool] | Nonetreer   boolr   strreturn9tuple[torch.Tensor, Callable[[torch.Tensor], TensorTree]]c                  t          | |||          \  }}t          |          \  }}|t          j        t          ||          fS )a{  Ravel (flatten) a pytree of tensors down to a 1D tensor.

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

    Args:
        tree (pytree): a pytree of tensors 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 ``(tensor, unravel_func)`` where the first element is a 1D tensor 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 tensor
        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 tensor of the
        default dtype is returned in the first component of the output.
    )r   r   r   )r   _ravel_leaves	functoolspartial_tree_unravel)r   r   r   r   leavestreespecflatunravel_flats           _/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/optree/integrations/torch.pyr   r   '   sX    J $!	  FH 'v..D,"=(LIIII    r   r	   r    ,Callable[[torch.Tensor], list[torch.Tensor]]r   torch.Tensorc               4    t          |  ||                    S N)r   )r   r    r   s      r!   r   r   y   s     (LL$6$6777r"   r   list[torch.Tensor]Atuple[torch.Tensor, Callable[[torch.Tensor], list[torch.Tensor]]]c                  | st          j        d          t          fS t          d | D                       st	          d          t          d | D                       }|d         |dd          D ]}t          j        |          t          d | D                       }t          d | D                       }t          fd|D                       r;t          j        d	 | D                       }|t          j	        t          ||          fS t          j        fd
| D                       }|t          j	        t          |||          fS )Nr   c              3  >   K   | ]}t          j        |          V  d S r&   )torch	is_tensor.0leafs     r!   	<genexpr>z _ravel_leaves.<locals>.<genexpr>   s,      88ut$$888888r"   zAll leaves must be tensors.c              3  $   K   | ]}|j         V  d S r&   )dtyper-   s     r!   r0   z _ravel_leaves.<locals>.<genexpr>   s$      66t
666666r"      c              3  >   K   | ]}|                                 V  d S r&   )numelr-   s     r!   r0   z _ravel_leaves.<locals>.<genexpr>   s*      224$**,,222222r"   c              3  $   K   | ]}|j         V  d S r&   )shaper-   s     r!   r0   z _ravel_leaves.<locals>.<genexpr>   s$      11$4:111111r"   c              3  $   K   | ]
}|k    V  d S r&    )r.   dtto_dtypes     r!   r0   z _ravel_leaves.<locals>.<genexpr>   s'      
0
0b2>
0
0
0
0
0
0r"   c                6    g | ]}t          j        |          S r9   )r+   ravelr-   s     r!   
<listcomp>z!_ravel_leaves.<locals>.<listcomp>   s"    BBB4U[..BBBr"   c                ^    g | ])}t          j        |                                        *S r9   )r+   r=   to)r.   r/   r;   s     r!   r>   z!_ravel_leaves.<locals>.<listcomp>   s1    KKKDT**--h77KKKr"   )r+   zeros_unravel_emptyall
ValueErrortuplepromote_typescatr   r   _unravel_leaves_single_dtype_unravel_leaves)r   from_dtypes
from_dtypesizesshapesraveledr;   s         @r!   r   r      s     0A//8888888 8677766v66666K1~H!!""o = =
&x<<22622222E11&11111F

0
0
0
0K
0
0
000 
)BB6BBBCC:E6JJ
 	
 iKKKKFKKKLLG/5&+xPP r"   c                   t          j        |           s t          dt          |           d          | j        dk    rt          dd d| j         d          g S )N"Expected a tensor to unravel, got .)r   0The unravel function expected a tensor of shape , got shape )r+   r,   rD   typer7   )r   s    r!   rB   rB      su    ?4   OMd4jjMMMNNNzT^t^^QUQ[^^^
 
 	
 Ir"   rL   tuple[int, ...]rM   tuple[tuple[int, ...], ...]c               f   t          j        |          s t          dt          |          d          |j        t          |           fk    r)t          dt          |           f d|j         d          t          j        |t          |                     }d t          ||          D             S )NrP   rQ   rR   rS   c                >    g | ]\  }}|                     |          S r9   )reshape)r.   chunkr7   s      r!   r>   z0_unravel_leaves_single_dtype.<locals>.<listcomp>   s(    NNN\UEEMM%  NNNr"   )	r+   r,   rD   rT   r7   sumsplitlistr   )rL   rM   r   chunkss       r!   rH   rH      s     ?4   OMd4jjMMMNNNzc%jj]""'E

} ' '' ' '
 
 	

 [tE{{++FNNXff5M5MNNNNr"   rJ   tuple[torch.dtype, ...]r;   torch.dtypec               6   t          j        |          s t          dt          |          d          |j        t          |           fk    r)t          dt          |           f d|j         d          |j        |k    rt          d| d|j         d          t          j        |t          |                     }t          j
                    5  t          j        d           d t          |||          D             cd d d            S # 1 swxY w Y   d S )	NrP   rQ   rR   rS   z0The unravel function expected a tensor of dtype z, got dtype ignorec                f    g | ].\  }}}|                     |                              |          /S r9   )rY   r@   )r.   rZ   r7   r2   s       r!   r>   z#_unravel_leaves.<locals>.<listcomp>   sH     
 
 
#ue MM%  ##E**
 
 
r"   )r+   r,   rD   rT   r7   r[   r2   r\   r]   warningscatch_warningssimplefilterr   )rL   rM   rJ   r;   r   r^   s         r!   rI   rI      s    ?4   OMd4jjMMMNNNzc%jj]""'E

} ' '' ' '
 
 	
 zXbxbbUYU_bbb
 
 	
 [tE{{++F		 	"	" 
 
h'''
 
'/'L'L
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   /DDDr&   )
r   r   r   r   r   r   r   r   r   r   )r   r	   r    r#   r   r$   r   r   )r   r'   r   r(   )r   r$   r   r'   )rL   rU   rM   rV   r   r$   r   r'   )rL   rU   rM   rV   rJ   r_   r;   r`   r   r$   r   r'   )__doc__
__future__r   r   rd   typingr   r   typing_extensionsr   r+   
optree.opsr   r   optree.typingr	   r
   optree.utilsr   __all__Tensorr   __annotations__r   ravel_pytreer   r   rB   rH   rI   r9   r"   r!   <module>rr      s       # " " " " "                      ' ' ' ' ' '  3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ! ! ! ! ! ! 
& &lELAA
 A A A A -1LJ
 LJ LJ LJ LJ LJ LJ^ 8 8 8 8! ! ! !H   O O O O$
 
 
 
 
 
r"   