Tensor Class Methods#
This page lists tensor methods intended for direct use in application code,
including inherited methods such as IsContiguous(). It covers dense,
dynamic, sparse, and distributed tensors. Overloads are grouped by method name
in the tables.
For an introduction to tensors, see Tensor Type. Prefer
Making Tensors to direct construction. Include <matx.h> to use the
regular tensor API. Dimensions and element indices are zero-based, and strides
are measured in elements.
Dense tensors#
tensor_t<T, RANK, Desc> owns or shares a storage container and inherits
shape, stride, pointer, and element-access methods from tensor_impl_t.
Copy construction shares storage; it does not copy elements. Methods that
create views generally share the same allocation, so writes through one view
are visible through the others.
Shape and layout#
Method |
Result and limitations |
|---|---|
|
Static, compile-time rank |
|
Extent of dimension |
|
Copy of the descriptor’s shape container. |
|
Element stride of dimension |
|
Copy of the descriptor’s stride container. |
|
Product of the extents (one for a rank-zero scalar). |
|
Size of the last dimension. Requires rank at least one. |
|
|
|
Whether the descriptor has contiguous row-major layout. For the default runtime descriptor, the last stride must be one and each preceding stride must equal the product of the subsequent extents. A rank-zero descriptor is contiguous. This query does not check pointer validity or memory space. |
|
Copy of the descriptor, including shape and strides. |
|
Whether another tensor view has the same rank, data pointer, shape, and strides. This compares metadata, not element values. |
For example, permuting a dense matrix changes its layout without moving data:
auto a = matx::make_tensor<float>({2, 3});
auto transposed = a.PermuteMatrix();
bool contiguous = a.IsContiguous(); // true
bool transposed_contiguous = transposed.IsContiguous(); // false
auto rows = a.Size(0); // 2
auto row_stride = a.Stride(0); // 3 elements
auto elements = a.TotalSize(); // 6
auto bytes = a.Bytes(); // 6 * sizeof(float)
These metadata queries do not synchronize outstanding GPU work. Shape and stride changes can make a view noncontiguous even when its original allocation was contiguous.
Data access and initialization#
Method |
Behavior |
|---|---|
|
Pointer to the start of this view, which may be offset from the allocation base. A pointer returned on the host may still refer to device memory. |
|
Whether |
|
Access an element using strides, with const and non-const overloads.
Supply exactly |
|
Equivalent element access for rank greater than zero, when the compiler
provides C++23 multidimensional subscripting
( |
|
Pointer at the stride-adjusted element offset for ordinary dense storage. |
|
Immediate host initialization for ranks zero through four. Real tensors take a scalar or lists nested to match the rank. CUDA complex tensors also support an extra list level containing real/imaginary pairs. The pointer must be host-accessible and the supplied values must fit. |
Direct element access requires memory accessible to the calling processor.
Synchronize relevant GPU work before reading or writing its results on the
host. operator() and SetVals perform immediate accesses; assignment
expressions below defer execution.
Assignment and ownership#
Method |
Behavior |
|---|---|
|
Creates a lazy element assignment from a tensor, operator, or scalar.
Execute the returned expression with |
|
Lazy arithmetic compound assignment, with tensor and generic RHS overloads. |
|
Lazy remainder and shift assignment for element types supporting the corresponding operations. |
|
Lazy bitwise compound assignment for compatible element types. |
|
Rebinds the tensor’s pointer, descriptor, and shared storage to |
|
On a non-const tensor, returns a storage copy sharing ownership. On a const tensor, returns a const storage reference that must not outlive the tensor. |
|
Reference count reported by the storage container. |
|
Replaces storage with a non-owning wrapper. The shape overload recomputes
the descriptor; the other overloads retain it. The two-pointer form uses
|
|
Friend function that swaps pointers, descriptors, and storage owners. |
|
Gets or sets the tensor’s diagnostic name. |
For example, auto b = a shares storage, b.Shallow(a) rebinds an existing
tensor, and (b = a).run(exec) assigns elements into b’s current storage.
Compound assignment requires the same explicit execution step.
Views#
Method |
Behavior and restrictions |
|---|---|
|
Reorders dimensions using a |
|
Swaps the final two dimensions, preserving leading batch dimensions. Requires rank at least two. |
|
Real or imaginary component view of an interleaved complex tensor, using doubled element strides. These views use non-owning storage: keep the original allocation alive while using them. |
Prefetch and interchange#
Method |
Behavior |
|---|---|
|
Asynchronously prefetches managed memory to the current CUDA device or
CPU, using the supplied stream. Requires memory supported by CUDA
prefetching. The requested range is |
|
Exports a legacy or versioned DLPack managed tensor, sharing the tensor’s
lifetime through its manager context. Prefer the versioned API. The
consumer must eventually call the returned object’s |
Dynamic tensors#
matx::detail::dynamic_tensor_t<T> has runtime rank and is created with
make_tensor<T>(), then initialized with make_tensor(tensor, shape, ...).
Execution requires CUDAJITExecutor and a build with MATX_EN_JIT=ON;
see Executor Compatibility for executor restrictions. It does not inherit
the dense tensor methods listed above.
Method |
Behavior |
|---|---|
|
Default construction; allocation-space/stream configuration; or a pointer
or |
|
Actual runtime rank. |
|
Static upper bound |
|
Runtime extent, element stride, and product of extents. |
|
View pointer and whether it is non-null. |
|
Gets or sets allocation preferences; setting them does not allocate or move existing data. |
|
Templated lazy assignment. Compound assignment is unsupported; use explicit
expressions such as |
|
Diagnostic name containing runtime rank and element type. |
Dynamic tensors currently have no public IsContiguous(), Shape(),
Strides(), or element-access operator() method.
Sparse tensors#
matx::experimental::sparse_tensor_t inherits tensor_impl_t with sparse
data storage. See Sparse Tensor Type for factories and supported formats.
Inherited Size, Shape, and TotalSize describe the logical tensor;
Data() points to stored values. Element access searches sparse storage,
returns a value (including implicit zero), and does not provide a writable
element reference. Dense layout/view helpers inherited from the base should
not be used to reshape sparse storage; its format defines the storage mapping.
In particular, inherited IsContiguous() describes the logical descriptor,
not the arrangement of sparse values and index buffers.
Method |
Behavior |
|---|---|
|
Constructs from value and per-level coordinate/position storage; destruction releases these storage containers. Prefer the sparse factories. |
|
Number of stored values, including any explicitly stored zeros. |
|
Number of coordinate or position entries for a storage level. |
|
Inherited raw coordinate and position pointers; available only with sparse data storage. Storage levels need not correspond one-to-one to dimensions. |
|
Lazy assignment from a supported sparse conversion transform. |
Distributed tensors#
matx::experimental::distributed_tensor_t<T, RANK, Distribution> describes a
global tensor using local dense fragments. Include
<matx/core/distributed_tensor.h> and see Experimental Distributed Tensors for
construction, executor setup, and current single-process limitations.
Method |
Behavior |
|---|---|
|
Constructs and validates a distributed tensor. Copy and move constructors are also available; fragments share their dense storage when copied. |
|
Global rank, extents, shape, and element count. |
|
Associated context identifier and this process’s rank. |
|
Const reference to the distribution descriptor. |
|
Number of fragments local to this process. |
|
Const or mutable reference to the local dense |
|
Const reference to a local fragment, including its view and endpoint. |
|
Const or mutable reference to the local fragment with the specified
distribution index. Raises |
|
Creates a lazy distributed assignment for a distributed executor. |
|
Returns the diagnostic name |