Files
pytorch/docs/cpp/source/api/aten/tensor.md
T
Svetlana Karslioglu 19da3da3d3 Restructure C++ docs (#174096)
The fix-cpp-build branch is a major restructuring that:

- Drops Exhale (which auto-generated API docs but caused OOM in CI) in favor of hand-written RST pages using Breathe directives
- Reorganizes notes into a hierarchical API reference with proper categories
- Adds new content: XPU docs, stable ABI docs, DeviceGuard/Stream docs
- Always runs Doxygen instead of gating on an env var

Pull Request resolved: https://github.com/pytorch/pytorch/pull/174096
Approved by: https://github.com/janeyx99, https://github.com/seemethere, https://github.com/albanD
2026-04-07 17:24:38 +00:00

5.0 KiB

myst
myst
html_meta
description keywords
at::Tensor class reference — the primary tensor type in PyTorch C++ with creation, indexing, device, and dtype APIs. PyTorch, C++, Tensor, at::Tensor, TensorOptions, ScalarType, DeviceGuard

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


The primary tensor class in ATen. Represents a multi-dimensional array
with a specific data type and device.

Default constructor. Creates an undefined tensor.

Returns the number of dimensions of the tensor.

Returns the size of the tensor at the given dimension.

Returns the sizes of all dimensions.

Returns the strides of all dimensions.

Returns the data type of the tensor.

Returns the device where the tensor is stored.

Returns true if the tensor is on a CUDA device.

Returns true if the tensor is on CPU.

Returns true if gradients need to be computed for this tensor.

Sets whether gradients should be computed for this tensor.

Returns a tensor on the specified device.

Returns a tensor with the specified data type.

Returns a contiguous tensor with the same data.

Returns a pointer to the underlying data.

Example:

#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


A class to specify options for tensor creation, including dtype, device,
layout, and requires_grad.

Default constructor.

Returns options with the specified data type.

Returns options with the specified device.

Returns options with the specified layout.

Returns options with the specified requires_grad setting.

Example:

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

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

Scalar


Represents a scalar value that can be converted to various numeric types.

Construct from an integer.

Construct from a double.

Convert to the specified type.

Returns true if the scalar is an integral type.

Returns true if the scalar is a floating point type.

ScalarType


Enumeration of data types supported by tensors.

8-bit unsigned integer (uint8_t)

8-bit signed integer (int8_t)

16-bit signed integer (int16_t)

32-bit signed integer (int32_t)

64-bit signed integer (int64_t)

16-bit floating point (float16)

32-bit floating point (float)

64-bit floating point (double)

Boolean type

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

:members:
:undoc-members:

Example:

{
    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


Enumeration of tensor memory layouts.

Dense tensor with strides.

Sparse tensor (COO format).

Sparse tensor in CSR format.

Sparse tensor in CSC format.