o
    Ne                      @   sH   d dl mZ ddlmZ ddlmZ d dlZddgZdd Z	d	d Z
dS )
    )print_function   )core)DataToLoDTensorConverterNcreate_lod_tensorcreate_random_int_lodtensorc                 C   s   t | tjrtt| ||S t | tr\t|t|g tj	j
jd}g }| D ]}|t| || q%|g|ks=J dt|j}||jd }t }||| || |S t | tjr{t }|| | || | syJ d|S td)a  
    Create a LoDTensor from a numpy array, list or existing LoDTensor.

    The implementation is as follows:

    1. Check whether the length-based LoD, i.e., :code:`recursive_seq_lens`
       is valid.

    2. Convert :code:`recursive_seq_lens` to a offset-based LoD.

    3. Based on :code:`place` , copy the :code:`data` from a numpy array, list
       or existing LoDTensor to CPU or GPU device.

    4. Set offset-based LoD to the output LoDTensor.

    Suppose we want to create a LoDTensor to hold data for word sequences,
    where each word is represented by an integer. If we want to create
    a LoDTensor to represent two sentences, one of 2 words, and one of 3 words.

    Then :code:`data` would be a numpy array of integers with shape (5, 1).
    :code:`recursive_seq_lens` would be [[2, 3]], indicating the word number
    in each sentence. This length-based :code:`recursive_seq_lens` [[2, 3]]
    would be converted to offset-based LoD [[0, 2, 5]] inside the function
    call.

    Please reference :ref:`user_guide_lod_tensor` for more details regarding LoD.

    Args:
        data (numpy.ndarray|list|LoDTensor): a numpy array, a list or ad LoDTensor
                holding the data to be copied.
        recursive_seq_lens (list[list[int]]): a list of lists indicating the
                length-based LoD info.
        place (CPUPlace|CUDAPlace): CPU or GPU place indicating where the data
                in the created LoDTensor will be stored.

    Returns:
         A LoDTensor with tensor data and recursive_seq_lens info.

    Examples:

        .. code-block:: python

            import paddle.fluid as fluid
            import numpy as np

            t = fluid.create_lod_tensor(np.ndarray([5, 30]), [[2, 3]], fluid.CPUPlace())
    )placeZ	lod_levelshapeZdtypez(data and recursive_seq_lens do not match)r   z the provided lod info is invalidz:data should be either a LoDTensor, a Numpy array or a list)
isinstancer   Z	LoDTensorr   nparraylistr   lenZVarDescZVarTypeZFP32appendfeeddataZreshaper	   setZset_recursive_sequence_lengthsZndarrayZ$has_valid_recursive_sequence_lengths	TypeError)r   recursive_seq_lensr   	converterZnew_recursive_seq_lensseqZarrZtensor r   GD:\Projects\ConvertPro\env\Lib\site-packages\paddle/fluid/lod_tensor.pyr      sF   0



c                 C   sF   t |ts	J dt| d g| }tj|||d}t|| |S )a  
	:api_attr: Static Graph

    Create a LoDTensor containing random integers.

    The implementation is as follows:

    1. Obtain the shape of output LoDTensor based on :code:`recursive_seq_lens`
       and :code:`base_shape` . The first dimension of the shape is the total
       length of sequences, while the other dimensions are the same as
       :code:`base_shape` .

    2. Create a numpy array of random integers, and parse the created numpy
       array as parameter :code:`data` of :ref:`api_fluid_create_lod_tensor` to
       create the output LoDTensor.

    Suppose we want to create a LoDTensor to hold data for 2 sequences, where
    the dimension of the sequences are [2, 30] and [3, 30] respectively.
    The :code:`recursive_seq_lens` would be [[2, 3]], and :code:`base_shape`
    would be [30] (the other dimensions excluding the sequence length).
    Therefore, the shape of the output LoDTensor would be [5, 30], where
    the first dimension 5 is the total lengths of the sequences, and the
    other dimensions are :code:`base_shape`.

    Args:
        recursive_seq_lens (list[list[int]]): a list of lists indicating the
                length-based LoD info.
        base_shape (list[int]): the shape of the output LoDTensor excluding
                the first dimension.
        place (CPUPlace|CUDAPlace): CPU or GPU place indicating where
                the data in the created LoDTensor will be stored.
        low (int): the lower bound of the random integers.
        high (int): the upper bound of the random integers.

    Returns:
        A LoDTensor with tensor data and recursive_seq_lens info, whose data
        is inside [low, high].

    Examples:
        .. code-block:: python

          import paddle.fluid as fluid

          t = fluid.create_random_int_lodtensor(recursive_seq_lens=[[2, 3]],
                    base_shape=[30], place=fluid.CPUPlace(), low=0, high=10)
          print(t.shape()) # [5, 30]
    zbase_shape should be a listZint64)r
   r   sumr   randomZrandom_integersZastyper   )r   Z
base_shaper   lowhighZoverall_shaper   r   r   r   r   q   s   1)
__future__r    r   Zdata_feederr   numpyr   __all__r   r   r   r   r   r   <module>   s   Y