oneflow.Tensor

A oneflow.Tensor is a multi-dimensional matrix containing elements of a single data type.

Data types

OneFlow defines 8 Tensor types with CPU and GPU variants which are as follows:

Data type

dtype

CPU tensor

GPU tensor

Boolean

oneflow.bool

oneflow.BoolTensor

oneflow.cuda.BoolTensor

8-bit integer (unsigned)

oneflow.uint8

oneflow.ByteTensor

oneflow.cuda.ByteTensor

8-bit integer (signed)

oneflow.int8

oneflow.CharTensor

oneflow.cuda.CharTensor

64-bit floating point

oneflow.float64 or oneflow.double

oneflow.DoubleTensor

oneflow.cuda.DoubleTensor

32-bit floating point

oneflow.float32 or oneflow.float

oneflow.FloatTensor

oneflow.cuda.FloatTensor

16-bit floating point

oneflow.float16 or oneflow.half

oneflow.HalfTensor

oneflow.cuda.HalfTensor

32-bit integer (signed)

oneflow.int32 or oneflow.int

oneflow.IntTensor

oneflow.cuda.IntTensor

64-bit integer (signed)

oneflow.int64 or oneflow.long

oneflow.LongTensor

oneflow.cuda.LongTensor

Initializing and basic operations

A tensor can be constructed from a Python list or sequence using the oneflow.tensor() constructor:

>>> import oneflow
>>> import numpy as np
>>> oneflow.tensor([[1., -1.], [1., -1.]])
tensor([[ 1., -1.],
        [ 1., -1.]], dtype=oneflow.float32)
>>> oneflow.tensor(np.array([[1, 2, 3], [4, 5, 6]]))
tensor([[ 1, 2, 3],
        [ 4, 5, 6]], dtype=oneflow.int64)

Warning

oneflow.tensor() always copies data. If you have a Tensor data and just want to change its requires_grad flag, use requires_grad_() or detach() to avoid a copy. If you have a numpy array and want to avoid a copy, use oneflow.as_tensor().

>>> import oneflow
>>> oneflow.zeros([2, 4], dtype=oneflow.int32)
tensor([[ 0, 0, 0, 0],
        [ 0, 0, 0, 0]], dtype=oneflow.int32)
>>> cuda0 = oneflow.device('cuda:0')
>>> oneflow.ones([2, 4], dtype=oneflow.float64, device=cuda0)
tensor([[ 1., 1., 1., 1.],
        [ 1., 1., 1., 1.]], device='cuda:0', dtype=oneflow.float64)

For more information about building tensors, see Creation Ops

The contents of a tensor can be accessed and modified using Python’s indexing and slicing notation:

>>> import oneflow
>>> x = oneflow.tensor([[1, 2, 3], [4, 5, 6]])
>>> print(x[1][2])
tensor(6, dtype=oneflow.int64)
>>> x[0][1] = 8
>>> print(x)
tensor([[1, 8, 3],
        [4, 5, 6]], dtype=oneflow.int64)

Use oneflow.Tensor.item() to get a Python number from a tensor containing a single value:

>>> import oneflow
>>> x = oneflow.tensor([[1]])
>>> x
tensor([[1]], dtype=oneflow.int64)
>>> x.item()
1
>>> x = oneflow.tensor(2.5)
>>> x
tensor(2.5000, dtype=oneflow.float32)
>>> x.item()
2.5

For more information about indexing, see Indexing, Slicing, Joining, Mutating Ops

A tensor can be created with requires_grad=True so that oneflow.autograd records operations on them for automatic differentiation.

>>> import oneflow
>>> x = oneflow.tensor([[1., -1.], [1., 1.]], requires_grad=True)
>>> out = x.pow(2).sum()
>>> out.backward()
>>> x.grad
tensor([[ 2., -2.],
        [ 2.,  2.]], dtype=oneflow.float32)

Note

For more information on the oneflow.dtype, oneflow.device, and oneflow.layout attributes of a oneflow.Tensor, see Tensor Attributes.

Note

Methods which mutate a tensor are marked with an underscore suffix. For example, oneflow.FloatTensor.add_() computes the absolute value in-place and returns the modified tensor, while oneflow.FloatTensor.add() computes the result in a new tensor.

Note

To change an existing tensor’s oneflow.device and/or oneflow.dtype, consider using to() method of Tensor object.

Warning

Current implementation of oneflow.Tensor introduces memory overhead, thus it might lead to unexpectedly high memory usage in the applications with many tiny tensors. If this is your case, consider using one large structure.

Tensor class reference

class oneflow.Tensor

There are a few main ways to create a tensor, depending on your use case.

  • To create a tensor with pre-existing data, use oneflow.tensor().

  • To create a tensor with specific size, use oneflow.* tensor creation ops (see Creation Ops).

  • To create a tensor with the same size (and similar types) as another tensor, use oneflow.*_like tensor creation ops (see Creation Ops).

Tensor.new_empty

Returns a Tensor of size size filled with uninitialized data.

Tensor.new_ones

See oneflow.new_ones()

Tensor.new_zeros

Returns a Tensor of size size filled with 0.

Tensor.new_full

Returns a Tensor of size size filled with fill_value.

Tensor.new_tensor

Tensor.is_cuda

Is True if the Tensor is stored on the GPU, False otherwise.

Tensor.is_global

Return whether this Tensor is a global tensor.

Tensor.device

Is the oneflow.device where this Tensor is, which is invalid for global tensor.

Tensor.grad

Return the gradient calculated by autograd functions.

Tensor.ndim

See oneflow.Tensor.dim()

Tensor.abs

See oneflow.abs()

Tensor.acos

See oneflow.acos()

Tensor.acosh

See oneflow.acosh()

Tensor.add

See oneflow.add()

Tensor.add_

In-place version of oneflow.Tensor.add().

Tensor.addcdiv

See oneflow.addcdiv()

Tensor.addcdiv_

In-place version of oneflow.Tensor.addcdiv()

Tensor.addcmul

See oneflow.addcmul()

Tensor.addcmul_

In-place version of oneflow.Tensor.addcmul().

Tensor.addmm

See oneflow.addmm()

Tensor.all

See oneflow.all()

Tensor.amin

See oneflow.amin()

Tensor.amax

See oneflow.amax()

Tensor.any

See oneflow.any()

Tensor.arccos

See oneflow.arccos()

Tensor.arccosh

See oneflow.arccosh()

Tensor.arcsin

See oneflow.arcsin()

Tensor.arcsinh

See oneflow.arcsinh()

Tensor.arctan

See oneflow.arctan()

Tensor.arctanh

See oneflow.arctanh()

Tensor.argmax

See oneflow.argmax()

Tensor.argmin

See oneflow.argmin()

Tensor.argsort

See oneflow.argsort()

Tensor.argwhere

See oneflow.argwhere()

Tensor.asin

See oneflow.asin()

Tensor.asinh

See oneflow.asinh()

Tensor.atan

See oneflow.atan()

Tensor.atan2

See oneflow.atan2()

Tensor.atanh

See oneflow.atanh()

Tensor.backward

Computes the gradient of current tensor w.r.t. graph leaves.

Tensor.bmm

See oneflow.bmm()

Tensor.bool

Tensor.bool() is equivalent to Tensor.to(oneflow.bool).

Tensor.byte

self.byte() is equivalent to self.to(oneflow.uint8).

Tensor.cast

See oneflow.cast()

Tensor.ceil

See oneflow.ceil()

Tensor.ceil_

See oneflow.ceil_()

Tensor.chunk

See oneflow.chunk()

Tensor.clamp

See oneflow.clamp().

Tensor.clamp_

Inplace version of oneflow.Tensor.clamp().

Tensor.clip

Alias for oneflow.Tensor.clamp().

Tensor.clip_

Alias for oneflow.Tensor.clamp_().

Tensor.clone

See oneflow.clone()

Tensor.contiguous

Tensor.copy_

Copies the elements from src into self tensor and returns self.

Tensor.cos

See oneflow.cos()

Tensor.cosh

See oneflow.cosh()

Tensor.cpu

Returns a copy of this object in CPU memory.

Tensor.cuda

Returns a copy of this object in CUDA memory.

Tensor.cumprod

See oneflow.cumprod()

Tensor.cumsum

See oneflow.cumsum()

Tensor.data

Tensor.dot

See oneflow.dot()

Tensor.detach

Tensor.placement

Is the oneflow.placement where this Tensor is, which is invalid for local tensor.

Tensor.sbp

Is the oneflow.sbp representing that how the data of the global tensor is distributed, which is invalid for local tensor.

Tensor.diag

See oneflow.diag()

Tensor.diagonal

See oneflow.diagonal()

Tensor.dim

Tensor.dim() → int

Tensor.div

See oneflow.div()

Tensor.div_

In-place version of oneflow.Tensor.div().

Tensor.double

Tensor.double() is equivalent to Tensor.to(flow.float64).

Tensor.dtype

Tensor.digamma

See oneflow.digamma()

Tensor.element_size

Tensor.element_size() → int

Tensor.eq

See oneflow.eq()

Tensor.equal

See oneflow.equal()

Tensor.erf

See oneflow.erf()

Tensor.erfc

See oneflow.erfc()

Tensor.erfinv

See oneflow.erfinv()

Tensor.erfinv_

Inplace version of oneflow.erfinv()

Tensor.exp

See oneflow.exp()

Tensor.exp2

See oneflow.exp2()

Tensor.expand

See oneflow.expand()

Tensor.expand_as

Expand this tensor to the same size as other.

Tensor.expm1

See oneflow.expm1()

Tensor.fill_

Tensor.fill_(value) → Tensor

Tensor.flatten

See oneflow.flatten()

Tensor.flip

See oneflow.flip()

Tensor.float

Tensor.float() is equivalent to Tensor.to(flow.float32).

Tensor.floor

See oneflow.floor()

Tensor.floor_

See oneflow.floor_()

Tensor.floor_divide

Tensor.fmod

See oneflow.fmod()

Tensor.gather

See oneflow.gather()

Tensor.ge

See oneflow.ge()

Tensor.get_device

For CUDA tensors, this function returns the device ordinal of the GPU on which the tensor resides.

Tensor.grad_fn

Return the function that created this tensor if it’s requires_grad is True.

Tensor.gt

See oneflow.gt()

Tensor.gt_

In-place version of oneflow.Tensor.gt().

Tensor.half

self.half() is equivalent to self.to(dtype=oneflow.float16).

Tensor.in_top_k

See oneflow.in_top_k()

Tensor.index_select

See oneflow.index_select()

Tensor.index_add

Tensor.index_add_

The interface is consistent with PyTorch.

Tensor.int

Tensor.int() is equivalent to Tensor.to(flow.int32).

Tensor.is_contiguous

Returns True if self tensor is contiguous in memory.

Tensor.is_floating_point

See oneflow.is_floating_point()

Tensor.is_lazy

Return whether this Tensor is a lazy tensor.

Tensor.is_leaf

All Tensors that have requires_grad which is False will be leaf Tensors by convention.

Tensor.isinf

See oneflow.isinf()

Tensor.isnan

See oneflow.isnan()

Tensor.item

Returns the value of this tensor as a standard Python number.

Tensor.le

See oneflow.le()

Tensor.lerp

See oneflow.lerp()

Tensor.lerp_

See oneflow.lerp_()

Tensor.log

See oneflow.log()

Tensor.log1p

See oneflow.log1p()

Tensor.log2

See oneflow.log2()

Tensor.log10

See oneflow.log10()

Tensor.logical_and

See oneflow.logical_and()

Tensor.logical_or

See oneflow.logical_or()

Tensor.logical_not

See oneflow.logical_not()

Tensor.logical_xor

See oneflow.logical_xor()

Tensor.long

Tensor.long() is equivalent to Tensor.to(flow.int64).

Tensor.lt

See oneflow.lt()

Tensor.masked_fill

See oneflow.masked_fill()

Tensor.masked_fill_

In-place version of oneflow.Tensor.masked_fill().

Tensor.masked_select

See oneflow.masked_select()

Tensor.matmul

See oneflow.matmul()

Tensor.mm

See oneflow.mm()

Tensor.mv

See oneflow.mv()

Tensor.max

See oneflow.max()

Tensor.maximum

See oneflow.maximum()

Tensor.median

See oneflow.median()

Tensor.mean

See oneflow.mean()

Tensor.min

See oneflow.min()

Tensor.minimum

See oneflow.minimum()

Tensor.mish

See oneflow.mish()

Tensor.mode

See oneflow.mode()

Tensor.mul

See oneflow.mul()

Tensor.mul_

In-place version of oneflow.Tensor.mul().

Tensor.frac

See oneflow.frac().

Tensor.frac_

In-place version of oneflow.Tensor.frac().

Tensor.nansum

See oneflow.nansum()

Tensor.narrow

See oneflow.narrow()

Tensor.ndimension

Tensor.ne

See oneflow.ne()

Tensor.neg

See oneflow.neg()

Tensor.negative

See oneflow.negative()

Tensor.nelement

Tensor.nelement() → int

Tensor.nonzero

See oneflow.nonzero()

Tensor.norm

See oneflow.norm()

Tensor.normal_

Fills self tensor with elements samples from the normal distribution parameterized by mean and std.

Tensor.numel

See oneflow.numel()

Tensor.numpy

Tensor.numpy() → numpy.ndarray

Tensor.offload

Transfer tensor data from GPU memory back to host (CPU) memory.

Tensor.load

Load tensor data stored on the host (CPU) back to GPU memory.

Tensor.is_offloaded

Determine whether the tensor has been moved to CPU memory and the CUDA device memory has been released.

Tensor.permute

See oneflow.permute()

Tensor.pow

See oneflow.pow()

Tensor.prod

See oneflow.prod()

Tensor.quantile

See oneflow.quantile()

Tensor.reciprocal

See oneflow.reciprocal()

Tensor.register_hook

Registers a backward hook.

Tensor.relu

See oneflow.relu()

Tensor.repeat

See oneflow.repeat()

Tensor.repeat_interleave

See oneflow.repeat_interleave()

Tensor.requires_grad

Is True if gradient need to be computed for this Tensor, False otherwise.

Tensor.requires_grad_

Sets this tensor’s requires_grad attribute in-place.

Tensor.reshape

See oneflow.reshape()

Tensor.reshape_as

Returns this tensor as the same shape as other.

Tensor.retain_grad

Enables this Tensor to have their grad populated during backward().

Tensor.roll

See oneflow.roll()

Tensor.round

See oneflow.round()

Tensor.round_

See oneflow.round_()

Tensor.rsqrt

See oneflow.rsqrt()

Tensor.selu

See oneflow.selu()

Tensor.shape

Tensor.sigmoid

See oneflow.sigmoid()

Tensor.sign

See oneflow.sign()

Tensor.silu

See oneflow.silu()

Tensor.sin

See oneflow.sin()

Tensor.sin_

See oneflow.sin_()

Tensor.sinh

See oneflow.sinh()

Tensor.size

Returns the size of the self tensor.

Tensor.softmax

See oneflow.softmax()

Tensor.softplus

See oneflow.softplus()

Tensor.softsign

See oneflow.softsign()

Tensor.sort

See oneflow.sort()

Tensor.split

See oneflow.split()

Tensor.sqrt

See oneflow.sqrt()

Tensor.square

See oneflow.square()

Tensor.squeeze

See oneflow.squeeze()

Tensor.squeeze_

In-place version of oneflow.Tensor.squeeze()

Tensor.std

See oneflow.std()

Tensor.storage_offset

Returns self tensor’s offset in the underlying storage in terms of number of storage elements (not bytes).

Tensor.stride

Tensor.logsumexp

See oneflow.logsumexp()

Tensor.sum

See oneflow.sum()

Tensor.swapaxes

See oneflow.swapaxes()

Tensor.swapdims

See oneflow.swapdims()

Tensor.sub

See oneflow.sub()

Tensor.sub_

In-place version of oneflow.Tensor.sub().

Tensor.tan

See oneflow.tan()

Tensor.tanh

See oneflow.tanh()

Tensor.tile

See oneflow.tile()

Tensor.to

Performs Tensor dtype and/or device conversion.

Tensor.local_to_global

Creates a global tensor from a local tensor.

Tensor.global_to_global

Performs Tensor placement and/or sbp conversion.

Tensor.to_global

Creates a global tensor if this tensor is a local tensor, otherwise performs Tensor placement and/or sbp conversion.

Tensor.to_local

Returns the local component of this global tensor in the current rank.

Tensor.to_consistent

This interface is no longer available, please use oneflow.Tensor.to_global() instead.

Tensor.tolist

Returns the tensor as a (nested) list.

Tensor.topk

See oneflow.topk()

Tensor.transpose

See oneflow.transpose()

Tensor.tril

See oneflow.tril()

Tensor.triu

See oneflow.triu()

Tensor.trunc

See oneflow.trunc()

Tensor.type_as

Returns this tensor cast to the type of the given tensor.

Tensor.type

Returns the type if dtype is not provided, else casts this object to the specified type.

Tensor.t

See oneflow.t()

Tensor.T

Is this Tensor with its dimensions reversed.

Tensor.unbind

See oneflow.unbind()

Tensor.unfold

Returns a view of the original tensor which contains all slices of size size from self tensor in the dimension dimension.

Tensor.uniform_

Tensor.uniform_(from=0, to=1) → Tensor

Tensor.unsqueeze

See oneflow.unsqueeze()

Tensor.unsqueeze_

In-place version of oneflow.Tensor.unsqueeze()

Tensor.as_strided

See oneflow.as_strided()

Tensor.as_strided_

In-place version of oneflow.Tensor.as_strided()

Tensor.var

See oneflow.var()

Tensor.view

Returns a new tensor with the same data as the self tensor but of a different shape.

Tensor.view_as

Expand this tensor to the same size as other.

Tensor.where

See oneflow.where()

Tensor.zero_

Fills self tensor with zeros.

Tensor.nms

See oneflow.nms()

Tensor.pin_memory

Copies the tensor to pinned memory, if it’s not already pinned.

Tensor.is_pinned

Returns true if this tensor resides in pinned memory.

Tensor.inverse

See oneflow.linalg.inv()

Tensor.cross

See oneflow.cross()

Tensor.scatter

See oneflow.scatter()

Tensor.scatter_

Inplace version of oneflow.Tensor.scatter()

Tensor.scatter_add

See oneflow.scatter_add()

Tensor.scatter_add_

Inplace version of oneflow.Tensor.scatter_add()

Tensor.bernoulli

See oneflow.bernoulli()

Tensor.bernoulli_

The inplace version of oneflow.Tensor.bernoulli_().

Tensor.bincount

See oneflow.bincount()

Tensor.isclose

Tensor.allclose

Tensor.broadcast_to

See oneflow.broadcast_to()

Tensor.unique

See oneflow.unique()

Tensor.bitwise_and

See oneflow.bitwise_and()

Tensor.bitwise_or

See oneflow.bitwise_or()

Tensor.bitwise_xor

See oneflow.bitwise_xor()

Tensor.baddbmm

See oneflow.baddbmm()