TensorOps

Package: sk.ainet.lang.tensor.ops

Modality: Core

add

Signature

fun add(a:Tensor, b:Tensor): Tensor

Parameters

  • a: Tensor

  • b: Tensor

Return Type

Tensor

subtract

Signature

fun subtract(a:Tensor, b:Tensor): Tensor

Parameters

  • a: Tensor

  • b: Tensor

Return Type

Tensor

multiply

Signature

fun multiply(a:Tensor, b:Tensor): Tensor

Parameters

  • a: Tensor

  • b: Tensor

Return Type

Tensor

divide

Signature

fun divide(a:Tensor, b:Tensor): Tensor

Parameters

  • a: Tensor

  • b: Tensor

Return Type

Tensor

addScalar

Signature

fun addScalar(a:Tensor, b:Number): Tensor

Parameters

  • a: Tensor

  • b: Number

Return Type

Tensor

subScalar

Signature

fun subScalar(a:Tensor, b:Number): Tensor

Parameters

  • a: Tensor

  • b: Number

Return Type

Tensor

mulScalar

Signature

fun mulScalar(a:Tensor, b:Number): Tensor

Parameters

  • a: Tensor

  • b: Number

Return Type

Tensor

divScalar

Signature

fun divScalar(a:Tensor, b:Number): Tensor

Parameters

  • a: Tensor

  • b: Number

Return Type

Tensor

rsubScalar

Signature

fun rsubScalar(a:Number, b:Tensor): Tensor

Parameters

  • a: Number

  • b: Tensor

Return Type

Tensor

rdivScalar

Signature

fun rdivScalar(a:Number, b:Tensor): Tensor

Parameters

  • a: Number

  • b: Tensor

Return Type

Tensor

matmul

Signature

fun matmul(a:Tensor, b:Tensor): Tensor

Parameters

  • a: Tensor left operand, shape […, m, k]. The last dimension k must match the second-to-last dimension of [b].

  • b: Tensor right operand, shape […, k, n]. Leading dimensions are broadcast against [a] using the usual broadcasting rules.

Return Type

Tensor

Definition

Given two matrices \(A \in \mathbb{R}^{m \times k}\) and \(B \in \mathbb{R}^{k \times n}\), the matrix product \(C = AB\) is defined as:

\[C_{ij} = \sum_{l=1}^{k} A_{il} \cdot B_{lj}\]

Where \(C \in \mathbb{R}^{m \times n}\), \(i\) ranges over rows \(1..m\), \(j\) over columns \(1..n\), and \(l\) is the summation index over the shared dimension \(k\).

Intuition

Matrix multiplication composes two linear transformations: each output element is the dot product of a row of \(A\) with a column of \(B\). It is the core primitive behind fully-connected layers, attention projections, and any linear map in a neural network’s forward pass.

Key properties:

  • Associativity: \((AB)C = A(BC)\)

  • Distributivity: \(A(B + C) = AB + AC\)

  • Non-commutativity: in general \(AB \neq BA\)

  • Identity: \(AI = IA = A\)

Complexity:

  • Standard algorithm: \(O(mnk)\)

  • Strassen’s algorithm: \(O(n^{2.807})\) for square matrices

  • Current theoretical best: \(O(n^{2.373})\)

Examples

val a: Tensor<FP32, FloatArray> = tensor(shape(2, 3)) { ... }
val b: Tensor<FP32, FloatArray> = tensor(shape(3, 4)) { ... }
val c = ops.matmul(a, b)   // shape(2, 4)

References

transpose

Signature

fun transpose(tensor:Tensor): Tensor

Parameters

  • tensor: Tensor

Return Type

Tensor

conv1d

Signature

fun conv1d(input:Tensor, weight:Tensor, bias:Tensor, stride:Int, padding:Int, dilation:Int, groups:Int): Tensor

Parameters

  • input: Tensor

  • weight: Tensor

  • bias: Tensor

  • stride: Int

  • padding: Int

  • dilation: Int

  • groups: Int

Return Type

Tensor

conv2d

Signature

fun conv2d(input:Tensor, weight:Tensor, bias:Tensor, stride:Pair, padding:Pair, dilation:Pair, groups:Int): Tensor

Parameters

  • input: Tensor

  • weight: Tensor

  • bias: Tensor

  • stride: Pair

  • padding: Pair

  • dilation: Pair

  • groups: Int

Return Type

Tensor

conv3d

Signature

fun conv3d(input:Tensor, weight:Tensor, bias:Tensor, stride:Triple, padding:Triple, dilation:Triple, groups:Int): Tensor

Parameters

  • input: Tensor

  • weight: Tensor

  • bias: Tensor

  • stride: Triple

  • padding: Triple

  • dilation: Triple

  • groups: Int

Return Type

Tensor

convTranspose1d

Signature

fun convTranspose1d(input:Tensor, weight:Tensor, bias:Tensor, stride:Int, padding:Int, outputPadding:Int, dilation:Int, groups:Int): Tensor

Parameters

  • input: Tensor

  • weight: Tensor

  • bias: Tensor

  • stride: Int

  • padding: Int

  • outputPadding: Int

  • dilation: Int

  • groups: Int

Return Type

Tensor

maxPool2d

Signature

fun maxPool2d(input:Tensor, kernelSize:Pair, stride:Pair, padding:Pair): Tensor

Parameters

  • input: Tensor

  • kernelSize: Pair

  • stride: Pair

  • padding: Pair

Return Type

Tensor

avgPool2d

Signature

fun avgPool2d(input:Tensor, kernelSize:Pair, stride:Pair, padding:Pair, countIncludePad:Boolean): Tensor

Parameters

  • input: Tensor

  • kernelSize: Pair

  • stride: Pair

  • padding: Pair

  • countIncludePad: Boolean

Return Type

Tensor

upsample2d

Signature

fun upsample2d(input:Tensor, scale:Pair, mode:UpsampleMode, alignCorners:Boolean): Tensor

Parameters

  • input: Tensor

  • scale: Pair

  • mode: UpsampleMode

  • alignCorners: Boolean

Return Type

Tensor

reshape

Signature

fun reshape(tensor:Tensor, newShape:Shape): Tensor

Parameters

  • tensor: Tensor

  • newShape: Shape

Return Type

Tensor

flatten

Signature

fun flatten(tensor:Tensor, startDim:Int, endDim:Int): Tensor

Parameters

  • tensor: Tensor

  • startDim: Int

  • endDim: Int

Return Type

Tensor

concat

Signature

fun concat(tensors:List, dim:Int): Tensor

Parameters

  • tensors: List

  • dim: Int

Return Type

Tensor

split

Signature

fun split(tensor:Tensor, splitSize:Int, dim:Int): List

Parameters

  • tensor: Tensor

  • splitSize: Int

  • dim: Int

Return Type

List

squeeze

Signature

fun squeeze(tensor:Tensor, dim:Int): Tensor

Parameters

  • tensor: Tensor

  • dim: Int

Return Type

Tensor

unsqueeze

Signature

fun unsqueeze(tensor:Tensor, dim:Int): Tensor

Parameters

  • tensor: Tensor

  • dim: Int

Return Type

Tensor

relu

Signature

fun relu(tensor:Tensor): Tensor

Parameters

  • tensor: Tensor

Return Type

Tensor

leakyRelu

Signature

fun leakyRelu(tensor:Tensor, negativeSlope:Float): Tensor

Parameters

  • tensor: Tensor

  • negativeSlope: Float

Return Type

Tensor

elu

Signature

fun elu(tensor:Tensor, alpha:Float): Tensor

Parameters

  • tensor: Tensor

  • alpha: Float

Return Type

Tensor

softmax

Signature

fun softmax(tensor:Tensor, dim:Int): Tensor

Parameters

  • tensor: Tensor

  • dim: Int

Return Type

Tensor

logSoftmax

Signature

fun logSoftmax(tensor:Tensor, dim:Int): Tensor

Parameters

  • tensor: Tensor

  • dim: Int

Return Type

Tensor

sigmoid

Signature

fun sigmoid(tensor:Tensor): Tensor

Parameters

  • tensor: Tensor

Return Type

Tensor

silu

Signature

fun silu(tensor:Tensor): Tensor

Parameters

  • tensor: Tensor

Return Type

Tensor

gelu

Signature

fun gelu(tensor:Tensor): Tensor

Parameters

  • tensor: Tensor

Return Type

Tensor

sum

Signature

fun sum(tensor:Tensor, dim:Int): Tensor

Parameters

  • tensor: Tensor

  • dim: Int

Return Type

Tensor

mean

Signature

fun mean(tensor:Tensor, dim:Int): Tensor

Parameters

  • tensor: Tensor

  • dim: Int

Return Type

Tensor

variance

Signature

fun variance(tensor:Tensor, dim:Int): Tensor

Parameters

  • tensor: Tensor

  • dim: Int

Return Type

Tensor

sqrt

Signature

fun sqrt(tensor:Tensor): Tensor

Parameters

  • tensor: Tensor

Return Type

Tensor

abs

Signature

fun abs(tensor:Tensor): Tensor

Parameters

  • tensor: Tensor

Return Type

Tensor

sign

Signature

fun sign(tensor:Tensor): Tensor

Parameters

  • tensor: Tensor

Return Type

Tensor

clamp

Signature

fun clamp(tensor:Tensor, minVal:Float, maxVal:Float): Tensor

Parameters

  • tensor: Tensor

  • minVal: Float

  • maxVal: Float

Return Type

Tensor

narrow

Signature

fun narrow(tensor:Tensor, dim:Int, start:Int, length:Int): Tensor

Parameters

  • tensor: Tensor

  • dim: Int

  • start: Int

  • length: Int

Return Type

Tensor

pad2d

Signature

fun pad2d(tensor:Tensor, padLeft:Int, padRight:Int, padTop:Int, padBottom:Int): Tensor

Parameters

  • tensor: Tensor

  • padLeft: Int

  • padRight: Int

  • padTop: Int

  • padBottom: Int

Return Type

Tensor

unfold

Signature

fun unfold(tensor:Tensor, dim:Int, size:Int, step:Int): Tensor

Parameters

  • tensor: Tensor

  • dim: Int

  • size: Int

  • step: Int

Return Type

Tensor

lt

Signature

fun lt(tensor:Tensor, value:Float): Tensor

Parameters

  • tensor: Tensor

  • value: Float

Return Type

Tensor

ge

Signature

fun ge(tensor:Tensor, value:Float): Tensor

Parameters

  • tensor: Tensor

  • value: Float

Return Type

Tensor

tril

Signature

fun tril(tensor:Tensor, k:Int): Tensor

Parameters

  • tensor: Tensor

  • k: Int

Return Type

Tensor

convert

Signature

fun convert(tensor:Tensor, targetType:TTo): Tensor

Parameters

  • tensor: Tensor

  • targetType: TTo

Return Type

Tensor

gather

Signature

fun gather(input:Tensor, indices:Tensor, dim:Int): Tensor

Parameters

  • input: Tensor

  • indices: Tensor

  • dim: Int

Return Type

Tensor

indexSelect

Signature

fun indexSelect(input:Tensor, indices:Tensor, dim:Int): Tensor

Parameters

  • input: Tensor

  • indices: Tensor

  • dim: Int

Return Type

Tensor

exp

Signature

fun exp(tensor:Tensor): Tensor

Parameters

  • tensor: Tensor

Return Type

Tensor

expm1

Signature

fun expm1(tensor:Tensor): Tensor

Parameters

  • tensor: Tensor

Return Type

Tensor

sin

Signature

fun sin(tensor:Tensor): Tensor

Parameters

  • tensor: Tensor

Return Type

Tensor

cos

Signature

fun cos(tensor:Tensor): Tensor

Parameters

  • tensor: Tensor

Return Type

Tensor

tanh

Signature

fun tanh(tensor:Tensor): Tensor

Parameters

  • tensor: Tensor

Return Type

Tensor

scaledDotProductAttention

Signature

fun scaledDotProductAttention(query:Tensor, key:Tensor, value:Tensor, mask:Tensor, scale:Float, causal:Boolean): Tensor

Parameters

  • query: Tensor [batch, nHeads, seqLen, headDim]

  • key: Tensor [batch, nKVHeads, kvLen, headDim]

  • value: Tensor [batch, nKVHeads, kvLen, headDim]

  • mask: Tensor optional additive mask [batch, 1, seqLen, kvLen] (e.g. causal)

  • scale: Float scaling factor, defaults to 1/sqrt(headDim)

  • causal: Boolean if true, apply causal masking (ignore [mask] parameter)

Return Type

Tensor