
    PhD-                        d dl mZmZ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mZ g dZ	 	 	 ddee   dee   d	ee   d
efdZ ee	j"                  d      Zddddddd
efdZej(                  fdededeeef   deeef   d
ef
dZy)    )ListOptionalUnionN)SymIntTensor)_add_docstr_nested)_device_dtype)to_padded_tensoras_nested_tensornested_tensornarrowtensor_listdtypedevicereturnc                 H   t        | t              rt        d | D              rt        d      |t        j
                  }|t        j
                  k(  rt	        j                  | |d|d      S |t        j                  k(  rddlm	}  || d||      \  }}|S t        d|       )ab  
    Constructs a nested tensor preserving autograd history from :attr:`tensor_list` a list of tensors.

    .. note::
        Tensors within the list are always copied by this function due to current nested tensor semantics.

    Args:
        tensor_list (List[Tensor]): a list of tensors with the same ndim

    Keyword arguments:
        dtype (:class:`torch.dtype`, optional): the desired type of returned nested tensor.
            Default: if None, same :class:`torch.dtype` as leftmost tensor in the list.
        device (:class:`torch.device`, optional): the desired device of returned nested tensor.
            Default: if None, same :class:`torch.device` as leftmost tensor in the list
        layout (:class:`torch.layout`, optional): the desired layout of returned nested tensor.
            Only strided and jagged layouts are supported. Default: if None, the strided layout.

    Example::

        >>> a = torch.arange(3, dtype=torch.float, requires_grad=True)
        >>> b = torch.arange(5, dtype=torch.float, requires_grad=True)
        >>> nt = torch.nested.as_nested_tensor([a, b])
        >>> nt.is_leaf
        False
        >>> fake_grad = torch.nested.nested_tensor([torch.ones_like(a), torch.zeros_like(b)])
        >>> nt.backward(fake_grad)
        >>> a.grad
        tensor([1., 1., 1.])
        >>> b.grad
        tensor([0., 0., 0., 0., 0.])
    c              3   >   K   | ]  }t        |t                 y w)N)
isinstancer   ).0ts     `C:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/nested/__init__.py	<genexpr>z#as_nested_tensor.<locals>.<genexpr>8   s      0+6aJq&!!;s   zDas_nested_tensor(): Expected first argument to be a list of tensors Nr   jagged_from_listoffsetsr   r   4Specified layout is unsupported for nested tensors: )r   listany	TypeErrortorchstrided_nested_tensor_from_tensor_listjagged$torch.nested._internal.nested_tensorr   RuntimeError)r   r   r   layoutr   nt_s          r   r   r      s    J k4(C 0+60 - R
 	
 ~44[%vW[\\	5<<	I d6QVWA	QRXQYZ[[    a	  
to_padded_tensor(input, padding, output_size=None, out=None) -> Tensor

Returns a new (non-nested) Tensor by padding the :attr:`input` nested tensor.
The leading entries will be filled with the nested data,
while the trailing entries will be padded.

.. warning::

    :func:`to_padded_tensor` always copies the underlying data,
    since the nested and the non-nested tensors differ in memory layout.

Args:
    padding (float): The padding value for the trailing entries.

Keyword args:
    output_size (Tuple[int]): The size of the output tensor.
                              If given, it must be large enough to contain all nested data;
                              else, will infer by taking the max size of each nested sub-tensor along each dimension.
    out (Tensor, optional): the output tensor.

Example::

    >>> nt = torch.nested.nested_tensor([torch.randn((2, 5)), torch.randn((3, 4))])
    nested_tensor([
      tensor([[ 1.6862, -1.1282,  1.1031,  0.0464, -1.3276],
              [-1.9967, -1.0054,  1.8972,  0.9174, -1.4995]]),
      tensor([[-1.8546, -0.7194, -0.2918, -0.1846],
              [ 0.2773,  0.8793, -0.5183, -0.6447],
              [ 1.8009,  1.8468, -0.9832, -1.5272]])
    ])
    >>> pt_infer = torch.nested.to_padded_tensor(nt, 0.0)
    tensor([[[ 1.6862, -1.1282,  1.1031,  0.0464, -1.3276],
             [-1.9967, -1.0054,  1.8972,  0.9174, -1.4995],
             [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000]],
            [[-1.8546, -0.7194, -0.2918, -0.1846,  0.0000],
             [ 0.2773,  0.8793, -0.5183, -0.6447,  0.0000],
             [ 1.8009,  1.8468, -0.9832, -1.5272,  0.0000]]])
    >>> pt_large = torch.nested.to_padded_tensor(nt, 1.0, (2, 4, 6))
    tensor([[[ 1.6862, -1.1282,  1.1031,  0.0464, -1.3276,  1.0000],
             [-1.9967, -1.0054,  1.8972,  0.9174, -1.4995,  1.0000],
             [ 1.0000,  1.0000,  1.0000,  1.0000,  1.0000,  1.0000],
             [ 1.0000,  1.0000,  1.0000,  1.0000,  1.0000,  1.0000]],
            [[-1.8546, -0.7194, -0.2918, -0.1846,  1.0000,  1.0000],
             [ 0.2773,  0.8793, -0.5183, -0.6447,  1.0000,  1.0000],
             [ 1.8009,  1.8468, -0.9832, -1.5272,  1.0000,  1.0000],
             [ 1.0000,  1.0000,  1.0000,  1.0000,  1.0000,  1.0000]]])
    >>> pt_small = torch.nested.to_padded_tensor(nt, 2.0, (2, 2, 2))
    RuntimeError: Value in output_size is less than NestedTensor padded size. Truncation is not supported.

F)r   r)   r   requires_grad
pin_memoryc                   |t         j                  }|t         j                  k(  rt        j                  | ||||      S |t         j                  k(  r| D cg c]7  }t        |t              r|j                         nt        j                  |      9 }}ddl	m
} t        j                         5   ||d||      \  }	}
ddd       	j                  |       |r|	j                         }	|	S t        d|       c c}w # 1 sw Y   AxY w)u  
Constructs a nested tensor with no autograd history (also known as a “leaf tensor”, see
:ref:`Autograd mechanics <autograd-mechanics>`) from :attr:`tensor_list` a list of tensors.

Args:
    tensor_list (List[array_like]): a list of tensors, or anything that can be passed to torch.tensor,
    where each element of the list has the same dimensionality.

Keyword arguments:
    dtype (:class:`torch.dtype`, optional): the desired type of returned nested tensor.
        Default: if None, same :class:`torch.dtype` as leftmost tensor in the list.
    layout (:class:`torch.layout`, optional): the desired layout of returned nested tensor.
        Only strided and jagged layouts are supported. Default: if None, the strided layout.
    device (:class:`torch.device`, optional): the desired device of returned nested tensor.
        Default: if None, same :class:`torch.device` as leftmost tensor in the list
    requires_grad (bool, optional): If autograd should record operations on the
        returned nested tensor. Default: ``False``.
    pin_memory (bool, optional): If set, returned nested tensor would be allocated in
        the pinned memory. Works only for CPU tensors. Default: ``False``.

Example::

    >>> a = torch.arange(3, dtype=torch.float, requires_grad=True)
    >>> b = torch.arange(5, dtype=torch.float, requires_grad=True)
    >>> nt = torch.nested.nested_tensor([a, b], requires_grad=True)
    >>> nt.is_leaf
    True
    N)r   r   r-   r.   r   r   r   r   )r#   r$   r	   r   r&   r   r   detach	as_tensorr'   r   no_gradrequires_grad_r.   r(   )r   r   r)   r   r-   r.   r   list_of_tensorsr   r*   r+   s              r   r   r      s    : ~$$'!# 	# 
5<<	
 %01$/q *4Av)>188:EOOTUDVV$/ 	 1 	J]]_$_d6Y^_EB  	-(B	QRXQYZ[[1
 _s   <C:/C??Dtensordimstartlengthc                 B   t        |t        t        t        f      st	        d      t        |t        t        t        f      st	        d      |t
        j                  k(  rlt        |t              st        |t              rt	        d      t        t        j                  |       t
        j                        j                  |||      }|S |t
        j                  k(  r|dk7  rt	        d      ddlm} t        |t        t        f      r1t        j                  |g| j                  t
        j                  	      }t        |t        t        f      r1t        j                  |g| j                  t
        j                  	      } || ||      \  }}}|S t	        d
|       )a  
Constructs a nested tensor (which might be a view) from :attr:`tensor`, a strided tensor. This follows
similar semantics to torch.Tensor.narrow, where in the :attr:`dim`-th dimension the new nested tensor
shows only the elements in the interval `[start, start+length)`. As nested representations
allow for a different `start` and `length` at each 'row' of that dimension, :attr:`start` and :attr:`length`
can also be tensors of shape `tensor.shape[0]`.

There's some differences depending on the layout you use for the nested tensor. If using strided layout,
torch.narrow will do a copy of the narrowed data into a contiguous NT with strided layout, while
jagged layout narrow() will create a non-contiguous view of your original strided tensor. This particular
representation is really useful for representing kv-caches in Transformer models, as specialized
SDPA kernels can deal with format easily, resulting in performance improvements.


Args:
    tensor (:class:`torch.Tensor`): a strided tensor, which will be used as the underlying data
        for the nested tensor if using the jagged layout or will be copied for the strided layout.
    dim (int): the dimension where narrow will be applied. Only `dim=1` is supported for the
        jagged layout, while strided supports all dim
    start (Union[int, :class:`torch.Tensor`]): starting element for the narrow operation
    length (Union[int, :class:`torch.Tensor`]): number of elements taken during the narrow op

Keyword arguments:
    layout (:class:`torch.layout`, optional): the desired layout of returned nested tensor.
        Only strided and jagged layouts are supported. Default: if None, the strided layout.

Example::

    >>> starts = torch.tensor([0, 1, 2, 3, 4], dtype=torch.int64)
    >>> lengths = torch.tensor([3, 2, 2, 1, 5], dtype=torch.int64)
    >>> narrow_base = torch.randn(5, 10, 20)
    >>> nt_narrowed = torch.nested.narrow(narrow_base, 1, starts, lengths, layout=torch.jagged)
    >>> nt_narrowed.is_contiguous()
    False
    z$start must be an integer or a tensorz%length must be an integer or a tensorz@start and length must be integers for the strided layout NT impl)r)      z!jagged layout only supports dim=1r   )jagged_from_tensor_and_lengths)r   r   z3Specified layout is unsupported for nested narrow: )r   intr   r   r(   r#   r$   r   unbindr   r&   r'   r;   r5   r   int64)r5   r6   r7   r8   r)   r*   r;   r+   s           r   r   r      sF   H ec6623ABBfsFF34BCCeV$
66(Babbell625==IPPQTV[]cd" I! 
5<<	!8BCCWec6]+LL%ekkREfsFm,\\6(6==TF1&%HAq I PQWPXYZZr,   )NNN)typingr   r   r   r#   r   r   torch._Cr   r	   torch.typesr
   Devicer   DType__all__r   nested_to_padded_tensorr   r   r$   r<   r    r,   r   <module>rG      s    ( (    ) : "#	6\f6\E?6\ V6\
 6\x ##25 n )-T$V[hm 8\rx 8\v dicpcp ?6 ? ?E#v+,> ?cSYkHZ ?u{ ?r,   