Class Tensor#
Defined in File tensor_struct.h
Class Documentation#
-
class Tensor#
An ABI stable wrapper around PyTorch tensors.
This class is modeled after TensorBase, as custom op kernels primarily need to interact with Tensor metadata (sizes, strides, device, dtype). Other tensor operations (like
empty_like) exist as standalone functions outside of this struct.Minimum compatible version: PyTorch 2.9.
Public Functions
-
inline Tensor()#
Constructs a Tensor with an uninitialized AtenTensorHandle.
Creates a new stable::Tensor by allocating an uninitialized tensor handle. The ownership of the handle is managed internally via shared_ptr.
Minimum compatible version: PyTorch 2.9.
-
inline explicit Tensor(AtenTensorHandle ath)#
Constructs a Tensor from an existing AtenTensorHandle.
Steals ownership of the provided AtenTensorHandle.
Minimum compatible version: PyTorch 2.9.
- Parameters:
ath – The AtenTensorHandle to wrap. Ownership is transferred to this Tensor.
-
inline AtenTensorHandle get() const#
Returns a borrowed reference to the underlying AtenTensorHandle.
Minimum compatible version: PyTorch 2.9.
- Returns:
The underlying AtenTensorHandle.
-
inline void *data_ptr() const#
Returns a pointer to the tensor’s data.
Minimum compatible version: PyTorch 2.9.
- Returns:
A void pointer to the tensor’s data storage.
-
inline int64_t dim() const#
Returns the number of dimensions of the tensor.
Minimum compatible version: PyTorch 2.9.
- Returns:
The number of dimensions (rank) of the tensor.
-
inline int64_t numel() const#
Returns the total number of elements in the tensor.
Minimum compatible version: PyTorch 2.9.
- Returns:
The total number of elements across all dimensions.
-
inline IntHeaderOnlyArrayRef sizes() const#
Returns the sizes (shape) of the tensor.
Returns a borrowed reference of the dimension sizes of the tensor.
Minimum compatible version: PyTorch 2.9.
- Returns:
An IntHeaderOnlyArrayRef containing the size of each dimension.
-
inline IntHeaderOnlyArrayRef strides() const#
Returns the strides of the tensor.
Returns a borrowed reference of the strides of the tensor.
Minimum compatible version: PyTorch 2.9.
- Returns:
An IntHeaderOnlyArrayRef containing the stride of each dimension.
-
inline bool is_contiguous() const#
Checks if the tensor is contiguous in memory.
Minimum compatible version: PyTorch 2.9.
Note
This is a subset of the original TensorBase API. It takes no arguments whereas the original API takes a memory format argument. Here, we assume the default contiguous memory format.
- Returns:
true if the tensor is contiguous, false otherwise.
-
inline int64_t stride(int64_t dim) const#
Returns the stride of a specific dimension.
Minimum compatible version: PyTorch 2.9.
- Parameters:
dim – The dimension index to query.
- Returns:
The stride of the specified dimension.
-
inline DeviceIndex get_device_index() const#
Returns the device index of the tensor.
Minimum compatible version: PyTorch 2.9.
- Returns:
The device index as DeviceIndex (int32_t).
-
inline bool is_cuda() const#
Checks if the tensor is on a CUDA device.
Minimum compatible version: PyTorch 2.9.
- Returns:
true if the tensor is on a CUDA device, false otherwise.
-
inline bool is_cpu() const#
Checks if the tensor is on the CPU.
Minimum compatible version: PyTorch 2.9.
- Returns:
true if the tensor is on the CPU, false otherwise.
-
inline int64_t size(int64_t dim) const#
Returns the size of a specific dimension.
Minimum compatible version: PyTorch 2.9.
- Parameters:
dim – The dimension index to query.
- Returns:
The size of the specified dimension.
-
inline bool defined() const#
Checks if the tensor is defined (not null).
Minimum compatible version: PyTorch 2.9.
- Returns:
true if the tensor is defined, false otherwise.
-
inline int64_t storage_offset() const#
Returns the storage offset of the tensor.
The storage offset is the number of elements from the beginning of the underlying storage to the first element of the tensor.
Minimum compatible version: PyTorch 2.9.
- Returns:
The storage offset in number of elements.
-
inline size_t element_size() const#
Returns the size in bytes of each element in the tensor.
Minimum compatible version: PyTorch 2.9.
- Returns:
The element size in bytes.
-
ScalarType scalar_type() const#
Returns the scalar type (dtype) of the tensor.
Minimum compatible version: PyTorch 2.9.
- Returns:
The ScalarType of the tensor.
-
inline Tensor()#