Skip to main content

//clika-runtime/io.clika.runtime/Tensors/fromBlob

fromBlob

[jvm]\

@JvmOverloads

fun fromBlob(buffer: ByteBuffer, shape: LongArray, dtype: Int = ClikaRtGen.DATA_TYPE_FLOAT32, device: Device? = null, deleter: VoidCallback? = null): Tensor

Wrap memory the application owns, without copying: buffer must be a DIRECT ByteBuffer (allocateDirect, the one JVM buffer with a stable native address), holding shape elements of dtype in row-major order. With no deleter the tensor BORROWS the buffer, which must outlive every view of it; with a deleter 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.

val buf = ByteBuffer.allocateDirect(8 * 4).order(ByteOrder.nativeOrder())
val view = Tensors.fromBlob(buf, longArrayOf(8)) // borrowed
val owned = Tensors.fromBlob(buf, longArrayOf(8), deleter = VoidCallback {
println("released") // adopted
})