Example #1
0
    def test_tensor_type(test_case):
        x = flow.randn(2, 3)
        y = flow.as_tensor(x)
        y[0] = 2.0
        test_case.assertTrue(np.array_equal(x.numpy(), y.numpy()))
        test_case.assertTrue(np.array_equal(id(x), id(y)))

        x = flow.randn(2, 3)
        x = x.to("cuda")
        y = flow.as_tensor(x)
        y[0] = 2.0
        test_case.assertTrue(np.array_equal(x.numpy(), y.numpy()))
        test_case.assertTrue(np.array_equal(id(x), id(y)))

        x = flow.randn(2, 3)
        y = flow.as_tensor(x, device=flow.device("cuda:0"))
        test_case.assertTrue(id(x) != id(y))

        for dtype in [
            flow.float64,
            flow.float16,
            flow.int64,
            flow.int32,
            flow.int8,
            flow.uint8,
        ]:
            x = flow.randn(2, 3)
            y = flow.as_tensor(x, dtype=dtype)
            test_case.assertTrue(id(x) != id(y))
Example #2
0
 def test_numpy_dtype_bug(test_case):
     test_case.assertEqual(flow.as_tensor([1.0]).dtype, flow.float32)
     x = np.random.randn(10)
     y1 = flow.as_tensor(x, dtype=flow.int64)
     y2 = flow.as_tensor(x, dtype=flow.float64)
     test_case.assertEqual(y1.dtype, flow.int64)
     test_case.assertEqual(y2.dtype, flow.float64)
Example #3
0
 def test_numpy_type(test_case):
     for device in [flow.device("cpu"), flow.device("cuda:0"), None]:
         for np_dtype in [
             np.float64,
             np.float32,
             np.float16,
             np.int64,
             np.int32,
             np.int8,
             np.uint8,
         ]:
             for flow_dtype in [
                 flow.float64,
                 flow.float16,
                 flow.int64,
                 flow.int32,
                 flow.int8,
                 flow.uint8,
             ]:
                 np_arr = np.ones((2, 3), dtype=np_dtype)
                 try:
                     tensor = flow.as_tensor(np_arr, dtype=flow_dtype)
                     if numpy_dtype_to_oneflow_dtype_dict[
                         np_arr.dtype
                     ] == flow_dtype and device is not flow.device("cuda:0"):
                         tensor[0][0] += 1.0
                         test_case.assertTrue(np.array_equal(np_arr, tensor.numpy()))
                     else:
                         test_case.assertTrue(np.array_equal(np_arr, tensor.numpy()))
                 except Exception as e:
                     # Ignore cast or kernel mismatch error in test example
                     pass
Example #4
0
 def test_other_type(test_case):
     for device in [flow.device("cpu"), flow.device("cuda:0"), None]:
         for np_dtype in [
             np.float64,
             np.float32,
             np.float16,
             np.int64,
             np.int32,
             np.int8,
             np.uint8,
         ]:
             for flow_dtype in [
                 flow.float64,
                 flow.float16,
                 flow.int64,
                 flow.int32,
                 flow.int8,
                 flow.uint8,
             ]:
                 # tuple
                 np_arr = (1.0, 2.0, 3.0)
                 try:
                     tensor = flow.as_tensor(np_arr, dtype=flow_dtype)
                     test_case.assertTrue(np.array_equal(np_arr, tensor.numpy()))
                 except Exception as e:
                     # Ignore cast or kernel mismatch error in test example
                     pass
                 # tuple
                 np_arr = [1.0, 2.0, 3.0]
                 try:
                     tensor = flow.as_tensor(np_arr, dtype=flow_dtype)
                     test_case.assertTrue(np.array_equal(np_arr, tensor.numpy()))
                 except Exception as e:
                     # Ignore cast or kernel mismatch error in test example
                     pass
                 # scalar
                 np_arr = 4.0
                 try:
                     tensor = flow.as_tensor(np_arr, dtype=flow_dtype)
                     test_case.assertTrue(np.array_equal(np_arr, tensor.numpy()))
                 except Exception as e:
                     # Ignore cast or kernel mismatch error in test example
                     pass
Example #5
0
def pack_padded_sequence(
    input: Tensor,
    lengths: Tensor,
    batch_first: bool = False,
    enforce_sorted: bool = True,
) -> PackedSequence:
    """The interface is consistent with PyTorch.
    The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.nn.utils.rnn.pack_padded_sequence.html.
    
    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.

    Args:
        input (Tensor): padded batch of variable length sequences.
        lengths (Tensor or list(int)): list of sequence lengths of each batch
            element (must be on the CPU if provided as a tensor).
        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``, the input will get sorted unconditionally. Default: ``True``.

    Returns:
        a :class:`PackedSequence` object
    """
    lengths = flow.as_tensor(lengths, dtype=flow.int64)
    assert (
        enforce_sorted == True
    ), "Only support enforce_sorted == True for now. Plesase Sort the input by length in a decreasing order."
    if enforce_sorted:
        sorted_indices = None
    else:
        lengths, sorted_indices = flow.sort(lengths, descending=True)
        sorted_indices = sorted_indices.to(input.device)
        batch_dim = 0 if batch_first else 1
        input = input.index_select(batch_dim, sorted_indices)
    data, batch_sizes = flow._C.pack_padded_sequence(input, lengths,
                                                     batch_first)
    return PackedSequence(data, batch_sizes, sorted_indices, None)
Example #6
0
def pack_sequence(sequences: List[Tensor],
                  enforce_sorted: bool = True) -> PackedSequence:
    """Packs a list of variable length Tensors

    Consecutive call of the next functions: ``pad_sequence``, ``pack_padded_sequence``.

    ``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.

    Args:
        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

    For example:

    .. code-block:: python
    
        >>> from oneflow.nn.utils.rnn import pack_sequence
        >>> import oneflow as flow

        >>> a = flow.tensor([1,2,3])
        >>> b = flow.tensor([4,5])
        >>> c = flow.tensor([6])
        >>> packed = pack_sequence([a, b, c])
        >>> packed.data
        tensor([1, 4, 6, 2, 5, 3], dtype=oneflow.int64)
        >>> packed.batch_sizes
        tensor([3, 2, 1], dtype=oneflow.int64)

    """
    lengths = flow.as_tensor([v.size(0) for v in sequences])
    return pack_padded_sequence(pad_sequence(sequences),
                                lengths,
                                enforce_sorted=enforce_sorted)