---
title: "Tensor"
sidebar_label: "Tensor"
description: "Kotlin binding reference: Tensor."
---

<!-- Generated by tools/api_reference/generate_api_docs.py. Do not edit. -->

//[clika-runtime](../../../index.md)/[io.clika.runtime](../index.md)/[Tensor](index.md)

# Tensor

[common]\
class [Tensor](index.md)

An N-dimensional array on a device, with a shape, a [dtype](dtype.md) and a placement. Build one through [Tensors](../Tensors/index.md), move it with [to](to.md), read it back with [item](item.md) and the typed array readers, and [release](release.md) 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 { }`:

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

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

## Properties

| Name | Summary |
|---|---|
| [device](device.md) | [common]<br>val [device](device.md): [Device](../Device/index.md)<br>The device this tensor is placed on. |
| [dtype](dtype.md) | [common]<br>val [dtype](dtype.md): [DType](../DType/index.md)<br>The element dtype. |
| [isComplete](isComplete.md) | [common]<br>val [isComplete](isComplete.md): [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)<br>True once the pending work has completed; never waits. |
| [isContiguous](isContiguous.md) | [common]<br>val [isContiguous](isContiguous.md): [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)<br>True when the elements sit in row-major order with no gaps. |
| [ndim](ndim.md) | [common]<br>val [ndim](ndim.md): [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)<br>The rank. |

## Functions

| Name | Summary |
|---|---|
| [close](close.md) | [common]<br>open fun [close](close.md)()<br>[release](release.md), under the AutoCloseable spelling. |
| [item](item.md) | [common]<br>fun [item](item.md)(): [Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)<br>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](../ClikaRtException/index.md)). |
| [itemDouble](itemDouble.md) | [common]<br>fun [itemDouble](itemDouble.md)(): [Double](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-double/index.html)<br>[item](item.md) for a Float64 tensor. |
| [itemInt](itemInt.md) | [common]<br>fun [itemInt](itemInt.md)(): [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)<br>[item](item.md) for an Int32 tensor. |
| [itemLong](itemLong.md) | [common]<br>fun [itemLong](itemLong.md)(): [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)<br>[item](item.md) for an Int64 tensor. |
| [numel](numel.md) | [common]<br>fun [numel](numel.md)(): [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)<br>The element count. |
| [onComplete](onComplete.md) | [common]<br>fun [onComplete](onComplete.md)(callback: [TensorCallback](../TensorCallback/index.md))<br>Register [callback](onComplete.md) to run once this tensor's producer has finished (a tensor that is already available fires it right away). The [Tensor](index.md) handed to the callback is its own for the call: whatever it does not [release](release.md) itself is released when the callback returns. |
| [release](release.md) | [common]<br>fun [release](release.md)()<br>Drop this handle's reference (the last release frees the storage); safe to call twice. |
| [shape](shape.md) | [common]<br>fun [shape](shape.md)(): [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html)<br>The per-dimension sizes. |
| [sum](sum.md) | [common]<br>fun [sum](sum.md)(): [Tensor](index.md)<br>The whole-tensor sum, a 0-D tensor.<br>[common]<br>fun [sum](sum.md)(dims: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), keepdim: [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) = false): [Tensor](index.md)<br>The sum along [dims](sum.md); [keepdim](sum.md) keeps the reduced dims as size 1. |
| [summary](summary.md) | [common]<br>fun [summary](summary.md)(): [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)<br>A human-readable summary: shape, dtype, device and the first values (a host read; large tensors are truncated). |
| [synchronize](synchronize.md) | [common]<br>fun [synchronize](synchronize.md)(): [Tensor](index.md)<br>Wait for this tensor's pending work; the settle point where an asynchronous failure surfaces. |
| [to](to.md) | [common]<br>fun [to](to.md)(dtype: [DType](../DType/index.md)): [Tensor](index.md)<br>Cast to [dtype](to.md) through the runtime (never host-side float math).<br>[common]<br>fun [to](to.md)(device: [Device](../Device/index.md)): [Tensor](index.md)<br>Move to [device](to.md); a tensor already there passes through.<br>[common]<br>fun [to](to.md)(device: [Device](../Device/index.md), dtype: [DType](../DType/index.md)): [Tensor](index.md)<br>Move to [device](to.md) and cast to [dtype](to.md). |
| [toDoubleArray](toDoubleArray.md) | [common]<br>fun [toDoubleArray](toDoubleArray.md)(): [DoubleArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-double-array/index.html)<br>[toFloatArray](toFloatArray.md) for a Float64 tensor: every element, flat, row-major. |
| [toFloatArray](toFloatArray.md) | [common]<br>fun [toFloatArray](toFloatArray.md)(): [FloatArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float-array/index.html)<br>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](shape.md) on your side when you need the grid back. |
| [toIntArray](toIntArray.md) | [common]<br>fun [toIntArray](toIntArray.md)(): [IntArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int-array/index.html)<br>[toFloatArray](toFloatArray.md) for an Int32 tensor: every element, flat, row-major. |
| [toLongArray](toLongArray.md) | [common]<br>fun [toLongArray](toLongArray.md)(): [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html)<br>[toFloatArray](toFloatArray.md) for an Int64 tensor: every element, flat, row-major. |
| [toString](toString.md) | [common]<br>open override fun [toString](toString.md)(): [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)<br>[summary](summary.md), or a marker for a released tensor. |