Back to Pytorch

Tensor Class

docs/cpp/source/api/aten/tensor.md

2.12.04.7 KB
Original Source

Tensor Class

The at::Tensor class is the primary tensor class in ATen, representing a multi-dimensional array with a specific data type and device.

Tensor

{cpp:class}

The primary tensor class in ATen. Represents a multi-dimensional array
with a specific data type and device.
{cpp:function}

Default constructor. Creates an undefined tensor.
{cpp:function}

Returns the number of dimensions of the tensor.
{cpp:function}

Returns the size of the tensor at the given dimension.
{cpp:function}

Returns the sizes of all dimensions.
{cpp:function}

Returns the strides of all dimensions.
{cpp:function}

Returns the data type of the tensor.
{cpp:function}

Returns the device where the tensor is stored.
{cpp:function}

Returns true if the tensor is on a CUDA device.
{cpp:function}

Returns true if the tensor is on CPU.
{cpp:function}

Returns true if gradients need to be computed for this tensor.
{cpp:function}

Sets whether gradients should be computed for this tensor.
{cpp:function}

Returns a tensor on the specified device.
{cpp:function}

Returns a tensor with the specified data type.
{cpp:function}

Returns a contiguous tensor with the same data.
{cpp:function}

Returns a pointer to the underlying data.

Example:

cpp
#include <ATen/ATen.h>

at::Tensor a = at::ones({2, 2}, at::kInt);
at::Tensor b = at::randn({2, 2});
auto c = a + b.to(at::kInt);

TensorOptions

{cpp:class}

A class to specify options for tensor creation, including dtype, device,
layout, and requires_grad.
{cpp:function}

Default constructor.
{cpp:function}

Returns options with the specified data type.
{cpp:function}

Returns options with the specified device.
{cpp:function}

Returns options with the specified layout.
{cpp:function}

Returns options with the specified requires_grad setting.

Example:

cpp
auto options = at::TensorOptions()
    .dtype(at::kFloat)
    .device(at::kCUDA, 0)
    .requires_grad(true);

at::Tensor tensor = at::zeros({3, 4}, options);

Scalar

{cpp:class}

Represents a scalar value that can be converted to various numeric types.
{cpp:function}

Construct from an integer.
{cpp:function}

Construct from a double.
{cpp:function}

Convert to the specified type.
{cpp:function}

Returns true if the scalar is an integral type.
{cpp:function}

Returns true if the scalar is a floating point type.

ScalarType

{cpp:enum-class}

Enumeration of data types supported by tensors.
{cpp:enumerator}

8-bit unsigned integer (uint8_t)
{cpp:enumerator}

8-bit signed integer (int8_t)
{cpp:enumerator}

16-bit signed integer (int16_t)
{cpp:enumerator}

32-bit signed integer (int32_t)
{cpp:enumerator}

64-bit signed integer (int64_t)
{cpp:enumerator}

16-bit floating point (float16)
{cpp:enumerator}

32-bit floating point (float)
{cpp:enumerator}

64-bit floating point (double)
{cpp:enumerator}

Boolean type
{cpp:enumerator}

Brain floating point (bfloat16)

Convenience constants:

  • at::kByte, at::kChar, at::kShort, at::kInt, at::kLong
  • at::kHalf, at::kFloat, at::kDouble, at::kBFloat16
  • at::kBool

DeviceGuard

{doxygenclass}
:members:
:undoc-members:

Example:

cpp
{
    c10::DeviceGuard guard(at::Device(at::kCUDA, 1));
    // Operations here run on CUDA device 1
    auto tensor = at::zeros({2, 2});
}
// Previous device is restored

Layout

{cpp:enum-class}

Enumeration of tensor memory layouts.
{cpp:enumerator}

Dense tensor with strides.
{cpp:enumerator}

Sparse tensor (COO format).
{cpp:enumerator}

Sparse tensor in CSR format.
{cpp:enumerator}

Sparse tensor in CSC format.