3
I4W^æH  ã               @   s²   d dl mZ d dlZd dlZddlmZ ddlmZ eddd	d
dgƒZej	ej	eej	 eej	 dœe_
dd„ ZG dd„ deƒZdd„ Zddd„Zddd„Zddd„Zd dd„ZdS )!é    )Ú
namedtupleNé   )Ú_VFé   )ÚOptionalÚPackedSequenceÚdataÚbatch_sizesÚsorted_indicesÚunsorted_indices)r   r	   r
   r   c             C   s   | d krd S || ƒS )N© )ÚoptionalÚfnr   r   ú3/tmp/pip-build-e66j97b2/torch/torch/nn/utils/rnn.pyÚbind   s    r   c                   s–   e Zd ZdZd!‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdd „ Z‡  ZS )"r   aq  Holds the data and list of :attr:`batch_sizes` of a packed sequence.

    All RNN modules accept packed sequences as inputs.

    Note:
        Instances of this class should never be created manually. They are meant
        to be instantiated by functions like :func:`pack_padded_sequence`.

        Batch sizes represent the number elements at each sequence step in
        the batch, not the varying sequence lengths passed to
        :func:`pack_padded_sequence`.  For instance, given data ``abc`` and ``x``
        the :class:`PackedSequence` would contain data ``axbc`` with
        ``batch_sizes=[2,1,1]``.

    Attributes:
        data (Tensor): Tensor containing packed sequence
        batch_sizes (Tensor): Tensor of integers holding
            information about the batch size at each sequence step
        sorted_indices (Tensor, optional): Tensor of integers holding how this
            :class:`PackedSequence` is constructed from sequences.
        unsorted_indices (Tensor, optional): Tensor of integers holding how this
            to recover the original sequences with correct order.

    .. note::
        :attr:`data` can be on arbitrary device and of arbitrary dtype.
        :attr:`sorted_indices` and :attr:`unsorted_indices` must be ``torch.int64``
        tensors on the same device as :attr:`data`.

        However, :attr:`batch_sizes` should always be a CPU ``torch.int64`` tensor.

        This invariant is maintained throughout :class:`PackedSequence` class,
        and all functions that construct a `:class:PackedSequence` in PyTorch
        (i.e., they only pass in tensors conforming to this constraint).

    Nc                s„   |d krt |ƒ}|d k	rD|jjdkr,tdƒ‚tt| ƒj| ||||ƒS t|tt	fƒr^t
|ƒdksbt‚tt| ƒj| |d |d |ƒS d S )NÚcpua  batch_sizes should always be on CPU. Instances of PackedSequence should never be created manually. They should be instantiated by functions like pack_sequence and pack_padded_sequences in nn.utils.rnn. https://pytorch.org/docs/stable/nn.html#torch.nn.utils.rnn.pack_sequencer   r   é   )Úinvert_permutationÚdeviceÚtypeÚ
ValueErrorÚsuperr   Ú__new__Ú
isinstanceÚlistÚtupleÚlenÚAssertionError)Úclsr   r	   r
   r   )Ú	__class__r   r   r   A   s    	

zPackedSequence.__new__c             C   s2   t | ƒ| jjƒ | jt| jdd„ ƒt| jdd„ ƒƒS )Nc             S   s   | j ƒ S )N)Ú
pin_memory)Útr   r   r   Ú<lambda>c   s    z+PackedSequence.pin_memory.<locals>.<lambda>c             S   s   | j ƒ S )N)r    )r!   r   r   r   r"   d   s    )r   r   r    r	   r   r
   r   )Úselfr   r   r   r    _   s    zPackedSequence.pin_memoryc          	      sP   | j r
| S t| ƒ| jjˆ ˆŽ| jt| j‡ ‡fdd„ƒt| j‡ ‡fdd„ƒƒS dS )z8Returns a GPU copy if `self.data` not already on the GPUc                s   | j ˆ ˆŽS )N)Úcuda)r!   )ÚargsÚkwargsr   r   r"   n   s    z%PackedSequence.cuda.<locals>.<lambda>c                s   | j ˆ ˆŽS )N)r$   )r!   )r%   r&   r   r   r"   o   s    N)Úis_cudar   r   r$   r	   r   r
   r   )r#   r%   r&   r   )r%   r&   r   r$   f   s
    zPackedSequence.cudac             C   s@   | j r8t| ƒ| jjƒ | jt| jdd„ ƒt| jdd„ ƒƒS | S dS )z8Returns a CPU copy if `self.data` not already on the CPUc             S   s   | j ƒ S )N)r   )r!   r   r   r   r"   w   s    z$PackedSequence.cpu.<locals>.<lambda>c             S   s   | j ƒ S )N)r   )r!   r   r   r   r"   x   s    N)r'   r   r   r   r	   r   r
   r   )r#   r   r   r   r   q   s
    zPackedSequence.cpuc             C   s   t | ƒ| jjƒ | j| j| jƒS )z1Returns copy with `self.data` cast to double type)r   r   Údoubler	   r
   r   )r#   r   r   r   r(   |   s    zPackedSequence.doublec             C   s   t | ƒ| jjƒ | j| j| jƒS )z0Returns copy with `self.data` cast to float type)r   r   Úfloatr	   r
   r   )r#   r   r   r   r)   „   s    zPackedSequence.floatc             C   s   t | ƒ| jjƒ | j| j| jƒS )z/Returns copy with `self.data` cast to half type)r   r   Úhalfr	   r
   r   )r#   r   r   r   r*   Œ   s    zPackedSequence.halfc             C   s   t | ƒ| jjƒ | j| j| jƒS )z/Returns copy with `self.data` cast to long type)r   r   Úlongr	   r
   r   )r#   r   r   r   r+   ”   s    zPackedSequence.longc             C   s   t | ƒ| jjƒ | j| j| jƒS )z.Returns copy with `self.data` cast to int type)r   r   Úintr	   r
   r   )r#   r   r   r   r,   œ   s    zPackedSequence.intc             C   s   t | ƒ| jjƒ | j| j| jƒS )z0Returns copy with `self.data` cast to short type)r   r   Úshortr	   r
   r   )r#   r   r   r   r-   ¤   s    zPackedSequence.shortc             C   s   t | ƒ| jjƒ | j| j| jƒS )z/Returns copy with `self.data` cast to char type)r   r   Úcharr	   r
   r   )r#   r   r   r   r.   ¬   s    zPackedSequence.charc             C   s   t | ƒ| jjƒ | j| j| jƒS )z/Returns copy with `self.data` cast to byte type)r   r   Úbyter	   r
   r   )r#   r   r   r   r/   ´   s    zPackedSequence.bytec                st   | j j|ˆŽ}| j}| j}d‰ ˆ ˆkrNt|‡ ‡fdd„ƒ}t|‡ ‡fdd„ƒ}|| j kr\| S t| ƒ|| j||ƒS dS )ao  Performs dtype and/or device conversion on `self.data`.

        It has similar signature as :meth:`torch.Tensor.to`.

        .. note::

            If the ``self.data`` Tensor already has the correct :class:`torch.dtype`
            and :class:`torch.device`, then ``self`` is returned.
            Otherwise, returns a copy with the desired configuration.
        r   c                s   | j ˆˆ  ƒS )N)Úto)r!   )Ú	device_kwr&   r   r   r"   Ï   s    z#PackedSequence.to.<locals>.<lambda>c                s   | j ˆˆ  ƒS )N)r0   )r!   )r1   r&   r   r   r"   Ð   s    N)r   r0   r
   r   r   r   r	   )r#   r%   r&   r   r
   r   r   )r1   r&   r   r0   ¼   s    
zPackedSequence.toc             C   s   | j jS )z+Returns true if `self.data` stored on a gpu)r   r'   )r#   r   r   r   r'   ×   s    zPackedSequence.is_cudac             C   s
   | j jƒ S )z6Returns true if `self.data` stored on in pinned memory)r   Ú	is_pinned)r#   r   r   r   r2   Ü   s    zPackedSequence.is_pinned)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r$   r   r(   r)   r*   r+   r,   r-   r.   r/   r0   Úpropertyr'   r2   Ú__classcell__r   r   )r   r   r      s    #c             C   s:   | d krd S t j| ƒ}|jd| t jd| jƒ | jd�ƒ |S )Nr   )r   )ÚtorchZ
empty_likeZscatter_ZarangeZnumelr   )ZpermutationÚoutputr   r   r   r   á   s    
r   FTc             C   s–   t jjƒ r&t|t jƒ r&tjddd� t j|t jd�}|r@d}n6t j	|dd�\}}|j
| jƒ}|rfdnd	}| j||ƒ} tj| ||ƒ\}}t|||dƒS )
a  Packs a Tensor containing padded sequences of variable length.

    :attr:`input` can be of size ``T x B x *`` where `T` is the length of the
    longest sequence (equal to ``lengths[0]``), ``B`` is the batch size, and
    ``*`` is any number of dimensions (including 0). If ``batch_first`` is
    ``True``, ``B x T x *`` :attr:`input` is expected.

    For unsorted sequences, use `enforce_sorted = False`. If :attr:`enforce_sorted` is
    ``True``, the sequences should be sorted by length in a decreasing order, i.e.
    ``input[:,0]`` should be the longest sequence, and ``input[:,B-1]`` the shortest
    one. `enforce_sorted = True` is only necessary for ONNX export.

    Note:
        This function accepts any input that has at least two dimensions. You
        can apply it to pack the labels, and use the output of the RNN with
        them to compute the loss directly. A Tensor can be retrieved from
        a :class:`PackedSequence` object by accessing its ``.data`` attribute.

    Arguments:
        input (Tensor): padded batch of variable length sequences.
        lengths (Tensor): list of sequences lengths of each batch element.
        batch_first (bool, optional): if ``True``, the input is expected in ``B x T x *``
            format.
        enforce_sorted (bool, optional): if ``True``, the input is expected to
            contain sequences sorted by length in a decreasing order. If
            ``False``, this condition is not checked. Default: ``True``.

    Returns:
        a :class:`PackedSequence` object
    zôpack_padded_sequence has been called with a Python list of sequence lengths. The tracer cannot track the data flow of Python values, and it will treat them as constants, likely rendering the trace incorrect for any other combination of lengths.r   )Ú
stacklevel)ZdtypeNT)Z
descendingr   r   )r9   Z_CZ_get_tracing_stater   ÚTensorÚwarningsÚwarnZ	as_tensorZint64Úsortr0   r   Úindex_selectr   Z_pack_padded_sequencer   )ÚinputÚlengthsÚbatch_firstÚenforce_sortedr
   Ú	batch_dimr   r	   r   r   r   Úpack_padded_sequenceê   s     rF   ç        c       	      C   s€   | j jdƒ}|dk	r0||k r,tdj||ƒƒ‚|}tj| j| j |||ƒ\}}| j}|dk	rx|r`dnd}|j||ƒ|| fS ||fS )a„  Pads a packed batch of variable length sequences.

    It is an inverse operation to :func:`pack_padded_sequence`.

    The returned Tensor's data will be of size ``T x B x *``, where `T` is the length
    of the longest sequence and `B` is the batch size. If ``batch_first`` is True,
    the data will be transposed into ``B x T x *`` format.

    Batch elements will be ordered decreasingly by their length.

    .. note::
        :attr:`total_length` is useful to implement the
        ``pack sequence -> recurrent network -> unpack sequence`` pattern in a
        :class:`~torch.nn.Module` wrapped in :class:`~torch.nn.DataParallel`.
        See :ref:`this FAQ section <pack-rnn-unpack-with-data-parallelism>` for
        details.

    Arguments:
        sequence (PackedSequence): batch to pad
        batch_first (bool, optional): if ``True``, the output will be in ``B x T x *``
            format.
        padding_value (float, optional): values for padded elements.
        total_length (int, optional): if not ``None``, the output will be padded to
            have length :attr:`total_length`. This method will throw :class:`ValueError`
            if :attr:`total_length` is less than the max sequence length in
            :attr:`sequence`.

    Returns:
        Tuple of Tensor containing the padded sequence, and a Tensor
        containing the list of lengths of each sequence in the batch.

    r   NzŠExpected total_length to be at least the length of the longest sequence in input, but got total_length={} and max sequence length being {}r   )	r	   Úsizer   Úformatr   Z_pad_packed_sequencer   r   r@   )	ÚsequencerC   Úpadding_valueÚtotal_lengthZmax_seq_lengthZpadded_outputrB   r   rE   r   r   r   Úpad_packed_sequence  s    "
rM   c             C   s´   | d j ƒ }|dd… }tdd„ | D ƒƒ}|r@t| ƒ|f| }n|t| ƒf| }| d jj|Ž j|ƒ}xHt| ƒD ]<\}}	|	j dƒ}
|rš|	||d|
…df< qp|	|d|
…|df< qpW |S )a¥  Pad a list of variable length Tensors with ``padding_value``

    ``pad_sequence`` stacks a list of Tensors along a new dimension,
    and pads them to equal length. For example, if the input is list of
    sequences with size ``L x *`` and if batch_first is False, and ``T x B x *``
    otherwise.

    `B` is batch size. It is equal to the number of elements in ``sequences``.
    `T` is length of the longest sequence.
    `L` is length of the sequence.
    `*` is any number of trailing dimensions, including none.

    Example:
        >>> from torch.nn.utils.rnn import pad_sequence
        >>> a = torch.ones(25, 300)
        >>> b = torch.ones(22, 300)
        >>> c = torch.ones(15, 300)
        >>> pad_sequence([a, b, c]).size()
        torch.Size([25, 3, 300])

    Note:
        This function returns a Tensor of size ``T x B x *`` or ``B x T x *``
        where `T` is the length of the longest sequence. This function assumes
        trailing dimensions and type of all the Tensors in sequences are same.

    Arguments:
        sequences (list[Tensor]): list of variable length sequences.
        batch_first (bool, optional): output will be in ``B x T x *`` if True, or in
            ``T x B x *`` otherwise
        padding_value (float, optional): value for padded elements. Default: 0.

    Returns:
        Tensor of size ``T x B x *`` if :attr:`batch_first` is ``False``.
        Tensor of size ``B x T x *`` otherwise
    r   r   Nc             S   s   g | ]}|j d ƒ‘qS )r   )rH   )Ú.0Úsr   r   r   ú
<listcomp>z  s    z pad_sequence.<locals>.<listcomp>.)rH   Úmaxr   r   ÚnewZfill_Ú	enumerate)Ú	sequencesrC   rK   Úmax_sizeZtrailing_dimsÚmax_lenZout_dimsZ
out_tensorÚiZtensorÚlengthr   r   r   Úpad_sequenceQ  s    '
rY   c             C   s    dd„ | D ƒ}t t| ƒ||d�S )ak  Packs a list of variable length Tensors

    ``sequences`` should be a list of Tensors of size ``L x *``, where `L` is
    the length of a sequence and `*` is any number of trailing dimensions,
    including zero.

    For unsorted sequences, use `enforce_sorted = False`. If ``enforce_sorted``
    is ``True``, the sequences should be sorted in the order of decreasing length.
    ``enforce_sorted = True`` is only necessary for ONNX export.


    Example:
        >>> from torch.nn.utils.rnn import pack_sequence
        >>> a = torch.tensor([1,2,3])
        >>> b = torch.tensor([4,5])
        >>> c = torch.tensor([6])
        >>> pack_sequence([a, b, c])
        PackedSequence(data=tensor([ 1,  4,  6,  2,  5,  3]), batch_sizes=tensor([ 3,  2,  1]))


    Arguments:
        sequences (list[Tensor]): A list of sequences of decreasing length.
        enforce_sorted (bool, optional): if ``True``, checks that the input
            contains sequences sorted by length in a decreasing order. If
            ``False``, this condition is not checked. Default: ``True``.

    Returns:
        a :class:`PackedSequence` object
    c             S   s   g | ]}|j d ƒ‘qS )r   )rH   )rN   Úvr   r   r   rP   «  s    z!pack_sequence.<locals>.<listcomp>)rD   )rF   rY   )rT   rD   rB   r   r   r   Úpack_sequenceŒ  s    r[   )FT)FrG   N)Fr   )T)Úcollectionsr   r=   r9   Ú r   Z_jit_internalr   ZPackedSequence_r<   Ú__annotations__r   r   r   rF   rM   rY   r[   r   r   r   r   Ú<module>   s"    K	
4
3
;