Tensor

interface Tensor<T : DType, V>(source)

The core tensor abstraction in the SKaiNET framework, representing a fundamental architectural decision to compose tensors from data and operations.

Fundamental Architectural Decision: Tensor Composition

This interface embodies a key architectural principle in the SKaiNET framework: tensors are composed of two distinct, complementary components:

  1. Data Component (TensorData) - Responsible for:

    • Multi-dimensional data storage and indexing

    • Memory layout and access patterns

    • Shape and dimensional metadata

    • Type-safe element access

  2. Operations Component (TensorOps) - Responsible for:

    • Mathematical operations and transformations

    • Computational algorithms

    • Operation chaining and composition

    • Performance-optimized implementations

Benefits of This Compositional Architecture

Separation of Concerns: Data management is cleanly separated from computational logic, making the codebase more maintainable and easier to understand.

Flexibility: Different data storage strategies (dense, sparse, distributed) can be combined with different operation implementations (CPU, GPU, specialized hardware) without tight coupling.

Performance Optimization: Data layout can be optimized independently from computational algorithms, enabling targeted performance improvements.

Extensibility: New data formats or operation types can be added without modifying existing code, following the open-closed principle.

Testability: Data and operations can be tested independently, improving test coverage and reducing complexity.

Usage Pattern

The tensor acts as a unified interface that delegates data access to the data component and computational operations to the ops component, providing a seamless experience while maintaining the benefits of compositional design.

Parameters

T

the data type constraint extending DType, defining the numerical precision

V

the actual value type that will be stored and accessed

Inheritors

Properties

Link copied to clipboard
abstract val data: TensorData<T, V>

The data component responsible for storage, indexing, and memory management.

Link copied to clipboard
abstract val dtype: KClass<T>

The data type descriptor defining the numerical precision and value representation.

Link copied to clipboard
open val grad: Tensor<T, V>?

Accumulated gradient, if any.

Link copied to clipboard
abstract val gradState: GradState<T, V>

Gradient state tied to this tensor instance.

Link copied to clipboard
abstract val ops: TensorOps

The operations component responsible for computational algorithms and transformations.

Link copied to clipboard
open val rank: Int

The number of dimensions in the tensor.

Link copied to clipboard

Flag indicating if this tensor participates in gradient computation.

Link copied to clipboard
open val shape: Shape

The shape descriptor inherited from the data component.

Link copied to clipboard
open val volume: Int

The total number of elements in the tensor.

Functions

Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.abs(): Tensor<T, V>
Link copied to clipboard
open fun accumulateGrad(g: Tensor<T, V>)

Accumulate a gradient tensor onto this tensor.

Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.asIndices(strict: Boolean = true): IndexTensor<V>

Validates this tensor as an index tensor and wraps it into IndexTensor.

Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.avgPool2d(kernelSize: Pair<Int, Int>, stride: Pair<Int, Int> = kernelSize, padding: Pair<Int, Int> = 0 to 0, countIncludePad: Boolean = true): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.bind(ctx: ExecutionContext): Tensor<T, V>

Binds this tensor to the operations of the given execution context.

Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.clamp(minVal: Float, maxVal: Float): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.cosineDistance(other: Tensor<T, V>, dim: Int = -1, eps: Double = 1.0E-8): Tensor<T, V>

Calculates the cosine distance between two tensors along a given dimension. Cosine distance is defined as 1 - cosine similarity. Formula: 1 - (A dot B) / (||A|| * ||B||)

Link copied to clipboard
operator fun <T : DType, V> Tensor<T, V>.div(v: Number): Tensor<T, V>
operator fun <T : DType, V> Tensor<T, V>.div(other: Tensor<T, V>): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.elu(alpha: Float = 1.0f): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.flatten(startDim: Int = 0, endDim: Int = -1): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.ge(value: Float): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.gelu(): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.isMatrix(): Boolean
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.isScalar(): Boolean
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.isVector(): Boolean
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.leakyRelu(negativeSlope: Float = 0.01f): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.logSoftmax(dim: Int = -1): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.lt(value: Float): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.matmul(other: Tensor<T, V>): Tensor<T, V>
Link copied to clipboard

Extension function for Q4_K matmul.

Link copied to clipboard

Extension function for Q8_0 matmul.

Link copied to clipboard

Extension function for convenient ternary matmul. Use this when you know the weight is ternary-quantized.

Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.mean(dim: Int? = null): Tensor<T, V>
Link copied to clipboard
operator fun <T : DType, V> Tensor<T, V>.minus(v: Number): Tensor<T, V>
operator fun <T : DType, V> Tensor<T, V>.minus(other: Tensor<T, V>): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.narrow(dim: Int, start: Int, length: Int): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.pad2d(padLeft: Int, padRight: Int, padTop: Int, padBottom: Int): Tensor<T, V>
Link copied to clipboard
operator fun <T : DType, V> Tensor<T, V>.plus(v: Number): Tensor<T, V>
operator fun <T : DType, V> Tensor<T, V>.plus(other: Tensor<T, V>): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.pprint(): String
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.relu(): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.reshape(newShape: Shape): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.sigmoid(): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.sign(): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.silu(): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.slice(slices: List<Slice<T, V>>): TensorView<T, V>

Creates a tensor view using a direct list of slice descriptors.

Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.sliceAt(vararg indices: Int): TensorView<T, V>

Convenience method for extracting specific indices across dimensions.

Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.sliceCopy(builder: TensorSliceBuilder<T, V>.() -> Unit): Tensor<T, V>

Creates a new tensor by copying data from the sliced region of this tensor.

Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.sliceRanges(vararg ranges: Pair<Int, Int>): TensorView<T, V>

Convenience method for creating simple range slices across all dimensions.

Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.sliceView(builder: TensorSliceBuilder<T, V>.() -> Unit): TensorView<T, V>

Creates a zero-copy view of this tensor using the provided slicing DSL.

Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.softmax(dim: Int = -1): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.sqrt(): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.squeeze(dim: Int? = null): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.sum(dim: Int? = null): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.t(): Tensor<T, V>
Link copied to clipboard
operator fun <T : DType, V> Tensor<T, V>.times(v: Number): Tensor<T, V>
operator fun <T : DType, V> Tensor<T, V>.times(other: Tensor<T, V>): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.tril(k: Int = 0): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.unfold(dim: Int, size: Int, step: Int): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.unsqueeze(dim: Int): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.upsample2d(scale: Pair<Int, Int> = 2 to 2, mode: UpsampleMode = UpsampleMode.Nearest, alignCorners: Boolean = false): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.variance(dim: Int? = null): Tensor<T, V>
Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.withOps(ops: TensorOps): Tensor<T, V>

Returns a Tensor that uses the provided ops for subsequent operations.

Link copied to clipboard
fun <T : DType, V> Tensor<T, V>.withRequiresGrad(flag: Boolean = true): Tensor<T, V>

Mark a tensor as requiring gradients and return it for chaining.

Link copied to clipboard
open fun zeroGrad()

Reset accumulated gradient.