oneflow.Tensor

OneFlow Tensor Class

class oneflow.Tensor
property T

Is this Tensor with its dimensions reversed.

If n is the number of dimensions in x, x.T is equivalent to x.permute(n-1, n-2, …, 0).

abs()

See oneflow.abs()

acos()

See oneflow.acos()

acosh()

See oneflow.acosh()

add(other)

See oneflow.add()

add_(other)

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

addmm(mat1, mat2, alpha=1, beta=1)
arccos()

See oneflow.arccos()

arccosh()

See oneflow.arccosh()

arcsin()

See oneflow.arcsin()

arcsinh()

See oneflow.arcsinh()

arctan()

See oneflow.arctan()

arctanh()

See oneflow.arctanh()

argmax(dim=None, keepdim=None)

See oneflow.argmax()

argmin(dim=None, keepdim=None)

See oneflow.argmin()

argsort(dim=None, descending=None)

This operator sorts the input Tensor at specified dim and return the indices of the sorted Tensor.

Parameters
  • input (oneflow.Tensor) – The input Tensor.

  • dim (int, optional) – dimension to be sorted. Defaults to the last dim (-1).

  • descending (bool, optional) – controls the sorting order (ascending or descending).

Returns

The indices of the sorted Tensor.

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow as flow
>>> x = np.array([[10, 2, 9, 3, 7],
...               [1, 9, 4, 3, 2]]).astype("float32")
>>> input = flow.Tensor(x)
>>> output = flow.argsort(input)
>>> output
tensor([[1, 3, 4, 2, 0],
        [0, 4, 3, 2, 1]], dtype=oneflow.int32)
>>> output = flow.argsort(input, descending=True)
>>> output
tensor([[0, 2, 4, 3, 1],
        [1, 2, 3, 4, 0]], dtype=oneflow.int32)
>>> output = flow.argsort(input, dim=0)
>>> output
tensor([[1, 0, 1, 0, 1],
        [0, 1, 0, 1, 0]], dtype=oneflow.int32)
argwhere()

See oneflow.argwhere()

asin()

See oneflow.asin()

asinh()

See oneflow.asinh()

atan()

See oneflow.atan()

atan2(other)

See oneflow.atan2()

atanh()

See oneflow.atanh()

backward(gradient=None, retain_graph=False, create_graph=False)

The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.Tensor.backward.html#torch.Tensor.backward.

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

The graph is differentiated using the chain rule. If the tensor is non-scalar (i.e. its data has more than one element) and requires gradient, the function additionally requires specifying gradient. It should be a tensor of matching type and location, that contains the gradient of the differentiated function w.r.t. self.

This function accumulates gradients in the leaves - you might need to zero .grad attributes or set them to None before calling it. See Default gradient layouts for details on the memory layout of accumulated gradients.

Note

If you run any forward ops, create gradient, and/or call backward in a user-specified CUDA stream context, see Stream semantics of backward passes.

Note

When inputs are provided and a given input is not a leaf, the current implementation will call its grad_fn (though it is not strictly needed to get this gradients). It is an implementation detail on which the user should not rely. See https://github.com/pytorch/pytorch/pull/60521#issuecomment-867061780 for more details.

Parameters
  • gradient (Tensor or None) – Gradient w.r.t. the tensor. If it is a tensor, it will be automatically converted to a Tensor that does not require grad unless create_graph is True. None values can be specified for scalar Tensors or ones that don’t require grad. If a None value would be acceptable then this argument is optional.

  • retain_graph (bool, optional) – If False, the graph used to compute the grads will be freed. Note that in nearly all cases setting this option to True is not needed and often can be worked around in a much more efficient way. Defaults to the value of create_graph.

  • create_graph (bool, optional) – If True, graph of the derivative will be constructed, allowing to compute higher order derivative products. Defaults to False.

bmm(other)

See oneflow.bmm()

cast(dtype)

See oneflow.cast()

ceil()

See oneflow.ceil()

chunk(chunks=None, dim=None)

See oneflow.chunk()

clamp(min=None, max=None)

See oneflow.clamp().

clamp_(min=None, max=None)

Inplace version of oneflow.Tensor.clamp().

clip(min=None, max=None)

Alias for oneflow.Tensor.clamp().

clip_(min=None, max=None)

Alias for oneflow.Tensor.clamp_().

clone(self: oneflow._oneflow_internal.Tensor)oneflow._oneflow_internal.Tensor
copy_(other: Union[oneflow._oneflow_internal.Tensor, numpy.ndarray])

The interface is consistent with PyTorch.

Tensor.copy_(src, non_blocking=False) → Tensor

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

The src tensor must be broadcastable with the self tensor. It may be of a different data type or reside on a different device.

Parameters
  • src (Tensor) – the source tensor to copy from

  • non_blocking (bool) – if True and this copy is between CPU and GPU, the copy may occur asynchronously with respect to the host. For other cases, this argument has no effect.

cos()

See oneflow.cos()

cosh()
cpu()

Returns a copy of this object in CPU memory. If this object is already in CPU memory and on the correct device, then no copy is performed and the original object is returned.

For example:

>>> import oneflow as flow

>>> input = flow.tensor([1, 2, 3, 4, 5], device=flow.device("cuda"))
>>> output = input.cpu()
>>> output.device
device(type='cpu', index=0)
cuda(device: Optional[Union[int, str, oneflow._oneflow_internal.device]] = None)

Returns a copy of this object in CUDA memory. If this object is already in CUDA memory and on the correct device, then no copy is performed and the original object is returned.

Parameters

device (flow.device) – The destination GPU device. Defaults to the current CUDA device.

For example:

>>> import oneflow as flow

>>> input = flow.Tensor([1, 2, 3, 4, 5])
>>> output = input.cuda()
>>> output.device
device(type='cuda', index=0)
property data
detach(self: oneflow._oneflow_internal.Tensor)oneflow._oneflow_internal.Tensor
property device
diag(diagonal=0)

See oneflow.diag()

diagonal(offset=0, dim1=0, dim2=1)
dim()

Tensor.dim() → int

Returns the number of dimensions of self tensor.

div(other)

See oneflow.div()

div_(value)Tensor

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

double()

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

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> input = flow.tensor(np.random.randn(1, 2, 3), dtype=flow.int)
>>> input = input.double()
>>> input.dtype
oneflow.float64
property dtype
element_size()

Tensor.element_size() → int

Returns the size in bytes of an individual element.

eq(other)

See oneflow.eq()

erf()Tensor

See oneflow.erf()

erfc()Tensor

See oneflow.erfc()

erfinv()

See oneflow.erfinv()

erfinv_()

Inplace version of oneflow.erfinv()

exp()

See oneflow.exp()

expand()Tensor

See oneflow.expand()

expand_as(other)Tensor

Expand this tensor to the same size as other. self.expand_as(other) is equivalent to self.expand(other.size()).

Please see expand() for more information about expand.

Parameters

other (oneflow.Tensor) – The result tensor has the same size as other.

expm1()

See oneflow.expm1()

fill_(value)

Tensor.fill_(value) → Tensor

Fills self tensor with the specified value.

flatten(start_dim: int = 0, end_dim: int = - 1)

See oneflow.flatten()

flip(dims)

See oneflow.flip()

float()

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

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> input = flow.tensor(np.random.randn(1, 2, 3), dtype=flow.int)
>>> input = input.float()
>>> input.dtype
oneflow.float32
floor()

See oneflow.floor()

floor_()

In-place version of oneflow.floor()

fmod(other)Tensor

See oneflow.fmod()

gather(dim, index)Tensor

See oneflow.gather()

ge(other)

See oneflow.ge()

gelu()

See oneflow.gelu()

get_device() -> Device ordinal (Integer)

For CUDA tensors, this function returns the device ordinal of the GPU on which the tensor resides. For CPU tensors, an error is thrown.

property grad
property grad_fn
gt(other)

See oneflow.gt()

in_top_k(targets, predictions, k)Tensor

See oneflow.in_top_k()

index_select(dim, index)Tensor

See oneflow.index_select()

int()

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

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> input = flow.tensor(np.random.randn(1, 2, 3), dtype=flow.float32)
>>> input = input.int()
>>> input.dtype
oneflow.int32
is_contiguous(self: oneflow._oneflow_internal.Tensor)bool
property is_cuda
is_floating_point()

See oneflow.is_floating_point()

property is_global
property is_lazy
property is_leaf
item()

Returns the value of this tensor as a standard Python number. This only works for tensors with one element. For other cases, see tolist().

This operation is not differentiable.

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> x = flow.tensor([1.0])
>>> x.item()
1.0
le(other)

See oneflow.le()

log()
log1p()

See oneflow.log1p()

logical_and()Tensor

See oneflow.logical_and()

logical_not()Tensor

See oneflow.logical_not()

logical_or()Tensor

See oneflow.logical_or()

logical_xor()Tensor

See oneflow.logical_xor()

long()

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

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> input = flow.tensor(np.random.randn(1, 2, 3), dtype=flow.float32)
>>> input = input.long()
>>> input.dtype
oneflow.int64
lt(other)

See oneflow.lt()

masked_fill(mask, fill_value)

See oneflow.masked_fill()

masked_select(mask)

See oneflow.masked_select()

matmul(other)

See oneflow.matmul()

max(dim, index)Tensor

See oneflow.max()

mean(dim, index)Tensor

See oneflow.mean()

min(dim, index)Tensor

See oneflow.min()

mish()

See oneflow.mish()

mul(value)Tensor

See oneflow.mul()

mul_(value)Tensor

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

narrow(dimension, start, length)

See oneflow.narrow()

property ndim
ndimension()

Tensor.dim() → int

Returns the number of dimensions of self tensor.

ne(other)

See oneflow.ne()

negative()

See oneflow.negative()

nelement()

Tensor.nelement() → int

Alias for numel()

new_ones()Tensor

See oneflow.new_ones()

nms(scores, iou_threshold: float)

See oneflow.nms()

nonzero(input, as_tuple=False)Tensor

See oneflow.nonzero()

norm(p=None, dim=None, keepdim=False, dtype=None)
normal_(mean=0, std=1, *, generator=None)Tensor

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

numel()

See oneflow.numel()

numpy()

Tensor.numpy() → numpy.ndarray

Returns self tensor as a NumPy ndarray. This tensor and the returned ndarray share the same underlying storage. Changes to

self tensor will be reflected in the ndarray and vice versa.

permute(*dims)

See oneflow.permute()

property placement
pow(b)

See oneflow.pow()

prod(dim, index)Tensor

See oneflow.prod()

reciprocal()

See oneflow.reciprocal()

register_hook(self: oneflow._oneflow_internal.Tensor, arg0: Callable[[oneflow._oneflow_internal.Tensor], oneflow._oneflow_internal.Tensor])None
relu(inplace=False)

See oneflow.relu()

repeat(*size)Tensor

See oneflow.repeat()

property requires_grad
requires_grad_(self: oneflow._oneflow_internal.Tensor, requires_grad: bool = True)oneflow._oneflow_internal.Tensor
reshape(*shape)

See oneflow.reshape()

retain_grad(self: oneflow._oneflow_internal.Tensor)None
roll(shifts, dims=None)

See oneflow.roll()

round()

See oneflow.round()

rsqrt()
selu()

See oneflow.selu()

property shape
sigmoid()

See oneflow.sigmoid()

sign()

See oneflow.sign()

silu()

See oneflow.silu()

sin()Tensor

See oneflow.sin()

sin_()

In-place version of oneflow.sin()

sinh()

See oneflow.sinh()

size(idx=None)

The interface is consistent with PyTorch.

Returns the size of the self tensor. If dim is not specified, the returned value is a oneflow.Size, a subclass of tuple. If dim is specified, returns an int holding the size of that dimension.

Parameters

idx (int, optional) – The dimension for which to retrieve the size.

softmax(dim=None)

See oneflow.softmax()

softplus()

See oneflow.softplus()

softsign()

See oneflow.softsign()

sort(dim: int = - 1, descending: bool = False)

See oneflow.sort()

split(split_size_or_sections=None, dim=0)

See oneflow.split()

sqrt()
square()
squeeze(dim=None)

See oneflow.squeeze()

std(dim=None, unbiased=True, keepdim=False)

See oneflow.std()

storage_offset(self: oneflow._oneflow_internal.Tensor)int
stride(self: oneflow._oneflow_internal.Tensor)tuple
sub(other)

See oneflow.sub()

sub_(value)Tensor

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

sum(dim, index)Tensor

See oneflow.sum()

swapaxes(dim0, dim1)

See oneflow.swapaxes()

t()

Tensor.t() → Tensor

See oneflow.t()

tan()

See oneflow.tan()

tanh()

See oneflow.tanh()

tile(*dims)Tensor

See oneflow.tile()

to(*args, **kwargs)
Performs Tensor dtype and/or device conversion.

A flow.dtype and flow.device are inferred from the arguments of input.to(*args, **kwargs).

Note

If the input Tensor already has the correct flow.dtype and flow.device, then input is returned. Otherwise, the returned tensor is a copy of input with the desired.

Parameters
  • input (oneflow.Tensor) – An input tensor.

  • *args (oneflow.Tensor or oneflow.device or oneflow.dtype) – Positional arguments

  • **kwargs (oneflow.device or oneflow.dtype) – Key-value arguments

Returns

A Tensor.

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> arr = np.random.randint(1, 9, size=(1, 2, 3, 4))
>>> input = flow.Tensor(arr)
>>> output = input.to(dtype=flow.float32)
>>> np.array_equal(arr.astype(np.float32), output.numpy())
True
to_global()Tensor

See oneflow.to_global()

to_local()

Returns the local tensor of a global tensor.

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> import numpy as np
>>> np_arr = np.array([0.5, 0.6, 0.7]).astype(np.float32)
>>> input = flow.tensor(np_arr, dtype=flow.float32)
>>> placement = flow.placement("cpu", ranks=[0])
>>> global_tensor = input.to_global(placement, [flow.sbp.split(0)])
>>> global_tensor.to_local()
tensor([0.5000, 0.6000, 0.7000], dtype=oneflow.float32)
tolist()

Returns the tensor as a (nested) list. For scalars, a standard Python number is returned, just like with item(). Tensors are automatically moved to the CPU first if necessary.

This operation is not differentiable.

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> input = flow.tensor([[1,2,3], [4,5,6]])
>>> input.tolist()
[[1, 2, 3], [4, 5, 6]]
topk(k, dim: Optional[int] = None, largest: bool = True, sorted: bool = True)

See oneflow.topk()

transpose(dim0, dim1)

See oneflow.transpose()

tril(diagonal=0)

See oneflow.tril()

triu(diagonal=0)

See oneflow.triu()

type_as(target)
Returns this tensor cast to the type of the given tensor.

This is a no-op if the tensor is already of the correct type.

Parameters
  • input (Tensor) – the input tensor.

  • target (Tensor) – the tensor which has the desired type.

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> input = flow.tensor(np.random.randn(1, 2, 3), dtype=flow.float32)
>>> target = flow.tensor(np.random.randn(4, 5, 6), dtype = flow.int32)
>>> input = input.type_as(target)
>>> input.dtype
oneflow.int32
unfold(dimension, size, step)

The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.Tensor.unfold.html#torch.Tensor.unfold.

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

Step between two slices is given by step.

If sizedim is the size of dimension dimension for self, the size of dimension dimension in the returned tensor will be (sizedim - size) / step + 1.

An additional dimension of size size is appended in the returned tensor.

Parameters
  • dimension (int) – dimension in which unfolding happens

  • size (int) – the size of each slice that is unfolded

  • step (int) – the step between each slice

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> x = flow.arange(1., 8)
>>> x
tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.])
>>> x.unfold(0, 2, 1)
tensor([[ 1.,  2.],
        [ 2.,  3.],
        [ 3.,  4.],
        [ 4.,  5.],
        [ 5.,  6.],
        [ 6.,  7.]])
>>> x.unfold(0, 2, 2)
tensor([[ 1.,  2.],
        [ 3.,  4.],
        [ 5.,  6.]])
uniform_(a=0, b=1)

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

Fills self tensor with numbers sampled from the continuous uniform distribution:

\[P(x)=1/(to-from)\]
unsqueeze(dim)

See oneflow.unsqueeze()

var(dim=None, unbiased=True, keepdim=False)

See oneflow.var()

view(*shape)

The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.Tensor.view.html

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

The returned tensor shares the same data and must have the same number of elements, but may have a different size. For a tensor to be viewed, the new view size must be compatible with its original size and stride, i.e., each new view dimension must either be a subspace of an original dimension, or only span across original dimensions \(d, d+1, \dots, d+k\) that satisfy the following contiguity-like condition that \(\forall i = d, \dots, d+k-1\),

\[\text{stride}[i] = \text{stride}[i+1] \times \text{size}[i+1]\]

Otherwise, it will not be possible to view self tensor as shape without copying it (e.g., via contiguous()). When it is unclear whether a view() can be performed, it is advisable to use reshape(), which returns a view if the shapes are compatible, and copies (equivalent to calling contiguous()) otherwise.

Parameters
  • input – A Tensor.

  • *shape – flow.Size or int…

Returns

A Tensor has the same type as input.

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> x = np.array(
...    [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
... ).astype(np.float32)
>>> input = flow.Tensor(x)

>>> y = input.view(2, 2, 2, -1).numpy().shape
>>> y
(2, 2, 2, 2)
where(x=None, y=None)

See oneflow.where()

zeros_(self: oneflow._oneflow_internal.Tensor)Maybe[void]