Skip to main content

//clika-runtime/io.clika.runtime/Tensor

Tensor

[common]
class Tensor

An N-dimensional array on a device, with a shape, a dtype and a placement. Build one through Tensors, move it with to, read it back with item and the typed array readers, and release it when done (the last release frees the storage).

Every member is NON-CONSUMING: a tensor stays valid after any call on it, and a call that produces a tensor returns a fresh one the caller owns. AutoCloseable, so a scope can ride use { }:

Tensors.full(longArrayOf(2, 3), 1.5).use { t -> println(t.sum().use { it.item() }) }

Reads settle: shape, numel, item, the array readers and summary wait for the tensor's producer; dtype, device, ndim and isContiguous never do.

Properties​

NameSummary
device[common]
val device: Device
The device this tensor is placed on.
dtype[common]
val dtype: DType
The element dtype.
isComplete[common]
val isComplete: Boolean
True once the pending work has completed; never waits.
isContiguous[common]
val isContiguous: Boolean
True when the elements sit in row-major order with no gaps.
ndim[common]
val ndim: Int
The rank.

Functions​

NameSummary
close[common]
open fun close()
release, under the AutoCloseable spelling.
item[common]
fun item(): Float
The one element of a single-element Float32 tensor (a reduction's result). Waits for the producer; refuses a numel other than 1 or another dtype (ClikaRtException).
itemDouble[common]
fun itemDouble(): Double
item for a Float64 tensor.
itemInt[common]
fun itemInt(): Int
item for an Int32 tensor.
itemLong[common]
fun itemLong(): Long
item for an Int64 tensor.
numel[common]
fun numel(): Long
The element count.
onComplete[common]
fun onComplete(callback: TensorCallback)
Register callback to run once this tensor's producer has finished (a tensor that is already available fires it right away). The Tensor handed to the callback is its own for the call: whatever it does not release itself is released when the callback returns.
release[common]
fun release()
Drop this handle's reference (the last release frees the storage); safe to call twice.
shape[common]
fun shape(): LongArray
The per-dimension sizes.
sum[common]
fun sum(): Tensor
The whole-tensor sum, a 0-D tensor.
[common]
fun sum(dims: LongArray, keepdim: Boolean = false): Tensor
The sum along dims; keepdim keeps the reduced dims as size 1.
summary[common]
fun summary(): String
A human-readable summary: shape, dtype, device and the first values (a host read; large tensors are truncated).
synchronize[common]
fun synchronize(): Tensor
Wait for this tensor's pending work; the settle point where an asynchronous failure surfaces.
to[common]
fun to(dtype: DType): Tensor
Cast to dtype through the runtime (never host-side float math).
[common]
fun to(device: Device): Tensor
Move to device; a tensor already there passes through.
[common]
fun to(device: Device, dtype: DType): Tensor
Move to device and cast to dtype.
toDoubleArray[common]
fun toDoubleArray(): DoubleArray
toFloatArray for a Float64 tensor: every element, flat, row-major.
toFloatArray[common]
fun toFloatArray(): FloatArray
Every element of a Float32 tensor of any rank, copied out flat in row-major order (the last dimension varies fastest: a [2, 3] tensor reads row 0's three values, then row 1's), after waiting for the producer. A tensor whose strides admit no flat view is packed into one for the read; the tensor itself is unchanged. Reshape the array by shape on your side when you need the grid back.
toIntArray[common]
fun toIntArray(): IntArray
toFloatArray for an Int32 tensor: every element, flat, row-major.
toLongArray[common]
fun toLongArray(): LongArray
toFloatArray for an Int64 tensor: every element, flat, row-major.
toString[common]
open override fun toString(): String
summary, or a marker for a released tensor.