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

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

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

# Tensors

[common]\
object [Tensors](index.md)

The tensor factories and data entry.

```kotlin
ClikaRt.load()
val x = Tensors.of(floatArrayOf(1f, 2f, 3f, 4f), longArrayOf(2, 2))
val z = Tensors.zeros(longArrayOf(2, 2), where = Device.CPU)
println(x.sum().item())
```

Dtype carriage is honest end to end: payload bytes enter at the declared dtype, and dtypes the JVM lacks ride BIT CARRIERS: [ofFloat16Bits](ofFloat16Bits.md) / [ofBFloat16Bits](ofBFloat16Bits.md) take the 16-bit patterns (one `Short` per element); conversion runs in the runtime ([Tensor.to](../Tensor/to.md)), never host-side float math. A null [Placement](../Placement/index.md) is the runtime's default placement; a [Device](../Device/index.md) names the device; a [Stream](../Stream/index.md) names the lane exactly.

## Functions

| Name | Summary |
|---|---|
| [fromBlob](fromBlob.md) | [common]<br>fun [fromBlob](fromBlob.md)(buffer: &lt;Error class: unknown class&gt;, shape: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), dtype: [DType](../DType/index.md) = DType.FLOAT32, where: [Placement](../Placement/index.md)? = null, deleter: [VoidCallback](../VoidCallback/index.md)? = null): [Tensor](../Tensor/index.md)<br>Wrap memory the application owns, without copying: [buffer](fromBlob.md) must be a DIRECT `ByteBuffer` (`allocateDirect`, the one JVM buffer with a stable native address) holding [shape](fromBlob.md) elements of [dtype](fromBlob.md) in row-major order from its position 0. With no [deleter](fromBlob.md) the tensor BORROWS the buffer, which the returned tensor keeps referenced while it lives (a view derived from it does not: keep the buffer alive for as long as any view is in use); with a [deleter](fromBlob.md) the tensor ADOPTS it, and the deleter runs exactly once, when the last reference drops (on whichever thread that happens). Writes through the buffer are visible through the tensor: it is the same memory. A buffer smaller than the shape needs is refused. |
| [fromBlobStrided](fromBlobStrided.md) | [common]<br>fun [fromBlobStrided](fromBlobStrided.md)(buffer: &lt;Error class: unknown class&gt;, shape: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), strides: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), dtype: [DType](../DType/index.md) = DType.FLOAT32, where: [Placement](../Placement/index.md)? = null, deleter: [VoidCallback](../VoidCallback/index.md)? = null): [Tensor](../Tensor/index.md)<br>[fromBlob](fromBlob.md) over a strided layout: [strides](fromBlobStrided.md) are element strides per dimension (a region of a larger row-major image keeps the image's row pitch as its row stride). [buffer](fromBlobStrided.md)'s position 0 is the first element (`slice()` a buffer to start elsewhere). |
| [fromBytes](fromBytes.md) | [common]<br>fun [fromBytes](fromBytes.md)(bytes: [ByteArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html), shape: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), dtype: [DType](../DType/index.md), where: [Placement](../Placement/index.md)? = null): [Tensor](../Tensor/index.md)<br>Raw entry: [bytes](fromBytes.md) read at [dtype](fromBytes.md) in row-major order (the sub-byte dtypes pack). |
| [full](full.md) | [common]<br>fun [full](full.md)(shape: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), value: [Double](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-double/index.html), dtype: [DType](../DType/index.md) = DType.FLOAT32, where: [Placement](../Placement/index.md)? = null): [Tensor](../Tensor/index.md)<br>A [shape](full.md)-extent tensor filled with [value](full.md) (cast to [dtype](full.md)). |
| [of](of.md) | [common]<br>fun [of](of.md)(data: [DoubleArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-double-array/index.html), shape: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), where: [Placement](../Placement/index.md)? = null): [Tensor](../Tensor/index.md)<br>Float64 entry.<br>[common]<br>fun [of](of.md)(data: [FloatArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float-array/index.html), shape: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), where: [Placement](../Placement/index.md)? = null): [Tensor](../Tensor/index.md)<br>Float32 entry: [data](of.md) copied in row-major order over [shape](of.md).<br>[common]<br>fun [of](of.md)(data: [IntArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int-array/index.html), shape: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), where: [Placement](../Placement/index.md)? = null): [Tensor](../Tensor/index.md)<br>Int32 entry.<br>[common]<br>fun [of](of.md)(data: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), shape: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), where: [Placement](../Placement/index.md)? = null): [Tensor](../Tensor/index.md)<br>Int64 entry. |
| [ofBFloat16Bits](ofBFloat16Bits.md) | [common]<br>fun [ofBFloat16Bits](ofBFloat16Bits.md)(bits: [ShortArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-short-array/index.html), shape: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), where: [Placement](../Placement/index.md)? = null): [Tensor](../Tensor/index.md)<br>BFloat16 entry from the 16-bit patterns. |
| [ofFloat16Bits](ofFloat16Bits.md) | [common]<br>fun [ofFloat16Bits](ofFloat16Bits.md)(bits: [ShortArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-short-array/index.html), shape: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), where: [Placement](../Placement/index.md)? = null): [Tensor](../Tensor/index.md)<br>Float16 entry from the 16-bit patterns (one IEEE-754 binary16 pattern per `Short`). |
| [ones](ones.md) | [common]<br>fun [ones](ones.md)(shape: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), dtype: [DType](../DType/index.md) = DType.FLOAT32, where: [Placement](../Placement/index.md)? = null): [Tensor](../Tensor/index.md)<br>A [shape](ones.md)-extent tensor of ones. |
| [zeros](zeros.md) | [common]<br>fun [zeros](zeros.md)(shape: [LongArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html), dtype: [DType](../DType/index.md) = DType.FLOAT32, where: [Placement](../Placement/index.md)? = null): [Tensor](../Tensor/index.md)<br>A [shape](zeros.md)-extent tensor of zeros. |