Class F

java.lang.Object
ai.beestream.jitnn.F

public final class F extends Object
Functional operators that are not methods of Tensor.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    static enum 
    How pad(Tensor, int[], int[], F.PadMode, float) fills: a constant, the reflection without the edge, or the edge replicated.
    static enum 
    static enum 
    How a loss reduces its per-element values.
  • Method Summary

    Modifier and Type
    Method
    Description
    static float
    accuracy(Tensor logits, Tensor labels)
    Fraction of rows whose argmax equals the label; runs now and returns a host value.
    static Tensor
    adaptiveAvgPool(Tensor x, int... out)
    Adaptive average pooling of x [N, C, ...] (1 to 3 spatial axes) to the given output sizes (PyTorch's bins).
    static Tensor
    adaptiveMaxPool(Tensor x, int... out)
    Adaptive max pooling of x [N, C, ...] (1 to 3 spatial axes) to the given output sizes.
    static Tensor
    attentionSoftmax(Tensor scores, float scale, boolean causal)
    softmax(scores * scale) over the last axis of attention scores [..., T, S], in one kernel; causal: query t sees keys 0 .. t, as PyTorch's is_causal.
    static Tensor
    avgPool1d(Tensor x, int kernel, int stride, int padding)
    Average pooling of x [N, C, L], PyTorch's avg_pool1d; padding counts in the divisor, as PyTorch's default.
    static Tensor
    avgPool2d(Tensor x, int kernel, int stride, int padding)
    Average pooling of x [N, C, H, W] over square windows, PyTorch's avg_pool2d; padding counts in the divisor, as PyTorch's default.
    static Tensor
    avgPool3d(Tensor x, int kernel, int stride, int padding)
    Average pooling of x [N, C, D, H, W] over cubic windows, PyTorch's avg_pool3d; padding counts in the divisor.
    static Tensor
    batchNorm(Tensor x, Tensor runningMean, Tensor runningVar, Tensor weight, Tensor bias, boolean training, float momentum, float eps)
    Batch normalisation of an NCHW (or NC) tensor.
    static Tensor
    batchNormRelu(Tensor x, Tensor runningMean, Tensor runningVar, Tensor weight, Tensor bias, boolean training, float momentum, float eps)
    relu(batchNorm(...)), fused.
    static Tensor
    Binary cross-entropy of probabilities p in [0, 1] against target, PyTorch semantics: the logs are clamped at -100, so p = 0 or 1 gives finite values and gradients.
    static Tensor
    binaryCrossEntropy(Tensor p, Tensor target, F.Reduction reduction)
    binaryCrossEntropy(Tensor, Tensor) reduced as reduction says.
    static Tensor
    Binary cross-entropy on logits, sigmoid folded in without overflow (PyTorch's BCEWithLogits).
    static Tensor
    static Tensor
    centerCrop(Tensor x, int height, int width)
    The central height x width window of images [..., H, W] (torchvision's center_crop, top-left rounding down).
    static Tensor
    contrastiveLoss(Tensor a, Tensor b, Tensor similar, float margin, F.Reduction reduction)
    Contrastive loss (Hadsell et al., 2006) with similar 1 for similar pairs and 0 for dissimilar ones: similar d^2 + (1 - similar) max(margin - d, 0)^2.
    static Tensor
    conv(Tensor x, Tensor weight, Tensor bias, int[] stride, int[] pads, int[] dilation, int groups)
    Convolution over 1 to 3 spatial axes with a size per axis: pads holds the padding before every axis, then after every axis (ONNX's order), so it may be asymmetric.
    static Tensor
    conv1d(Tensor x, Tensor weight, Tensor bias, int stride, int padding, int dilation)
    1-D convolution of x [N, C, L] with weight [Cout, C, K]; bias may be null.
    static Tensor
    conv1d(Tensor x, Tensor weight, Tensor bias, int stride, int padding, int dilation, int groups)
    Grouped 1-D convolution: weight [Cout, C / groups, K]; groups = C is depthwise.
    static Tensor
    conv2d(Tensor x, Tensor weight, Tensor bias, int stride, int padding, int dilation, int groups)
    2-D convolution (NCHW), square kernel; bias may be null.
    static Tensor
    conv2dRelu(Tensor x, Tensor weight, Tensor bias, int stride, int padding, int dilation, int groups)
    relu(conv2d(...)) with the bias and ReLU fused into the convolution's output.
    static Tensor
    conv3d(Tensor x, Tensor weight, Tensor bias, int stride, int padding, int dilation)
    3-D convolution of x [N, C, D, H, W] with weight [Cout, C, KD, KH, KW]; bias may be null.
    static Tensor
    conv3d(Tensor x, Tensor weight, Tensor bias, int stride, int padding, int dilation, int groups)
    Grouped 3-D convolution: weight [Cout, C / groups, KD, KH, KW].
    static Tensor
    convTranspose2d(Tensor x, Tensor weight, Tensor bias, int stride, int padding, int outputPadding, int dilation, int groups)
    Transposed 2-D convolution (NCHW), PyTorch's conv_transpose2d: weight [Cin, Cout/groups, K, K], square kernel; bias may be null.
    static Tensor
    cosineEmbeddingLoss(Tensor a, Tensor b, Tensor target, float margin, F.Reduction reduction)
    Cosine embedding loss: 1 - cos(a, b) for target 1, max(0, cos(a, b) - margin) for -1.
    static Tensor
    cosineProximityLoss(Tensor input, Tensor target, F.Reduction reduction)
    Cosine proximity loss (DL4J, Keras): minus the cosine similarity along the last axis, reduced.
    static Tensor
    cosineSimilarity(Tensor a, Tensor b, int axis, float eps)
    Cosine similarity along axis: a . b / (max(|a|, eps) max(|b|, eps)), PyTorch's cosine_similarity.
    static Tensor
    crossEntropy(Tensor logits, Tensor labels)
    Mean cross-entropy between logits [N, C] and INT32 class labels [N] (fused kernels).
    static Tensor
    ctcLoss(Tensor logProbs, Tensor targets, int[] inputLengths, int[] targetLengths, int blank, F.Reduction reduction)
    CTC loss, PyTorch's ctc_loss: log-probabilities [T, B, C] (log_softmax over C), INT32 targets [B, L] padded, input and target lengths; blank index blank.
    static Tensor
    depthToSpace(Tensor x, int block, String mode)
    Moves blocks of channels into space: x [N, C, H, W] to [N, C / b^2, H b, W b].
    static Tensor
    A Q8_0 tensor back to FP32.
    static Tensor
    dropout(Tensor x, float p, Tensor seed, int stream)
    Inverted dropout with a fresh mask per step: seed is an INT32 [2] host tensor.
    static Tensor
    embedding(Tensor index, Tensor weight)
    Rows of weight [V, D] picked by the INT32 index: shape index.shape + [D].
    static Tensor
    embedding(Tensor index, Tensor weight, int paddingIdx)
    As embedding(Tensor, Tensor); row paddingIdx receives no gradient.
    static Tensor
    embeddingBag(Tensor indices, int[] offsets, Tensor weight, F.BagMode mode)
    EmbeddingBag (PyTorch's, 1-D input with offsets): the rows of weight picked by the INT32 indices, combined per bag by mode.
    static Tensor
    fakeQuantize(Tensor x, float scale, float lowest, int bits)
    Quantises x to bits (8 or 16) bits and back: clamp(round_half_even(x / scale), lowest, lowest + 2^bits - 1) * scale, lowest being the lowest code minus the zero point (ONNX QuantizeLinear followed by DequantizeLinear).
    static Tensor
    flip(Tensor x, int... axes)
    x reversed along each of axes (torch.flip).
    static Tensor
    fold(Tensor cols, int[] outputSize, int[] kernel, int[] stride, int[] pad, int[] dil)
    Folds columns back into an image, summing overlaps (torch fold, ONNX Col2Im); see Spatial.fold.
    static Tensor
    gatherND(Tensor data, Tensor indices)
    Slices of data picked by INT32 index rows indices [..., k] (ONNX GatherND, batch_dims 0); differentiable in data.
    static Tensor
    gaussianKl(Tensor mu, Tensor logvar)
    KL divergence of N(mu, exp(logvar)) from N(0, 1), summed over every element: -0.5 * sum(1 + logvar - mu^2 - exp(logvar)), the VAE regulariser.
    static Tensor
    Mean over the spatial dimensions: [N, C, H, W] to [N, C].
    static Tensor
    gloveLoss(Tensor wi, Tensor wj, Tensor bi, Tensor bj, Tensor logX, Tensor weight)
    GloVe's weighted least squares (Pennington et al., 2014) over a batch of co-occurrences: mean of weight (wi . wj + bi + bj - logX)^2, with wi, wj [B, D] and the rest [B].
    static Tensor
    groupNorm(Tensor x, int groups, Tensor weight, Tensor bias, float eps)
    GroupNorm (Wu and He) of x [N, C, ...] in groups groups of channels: each (sample, group) normalised over its channels and positions, then a per-channel affine (weight and bias may be null, as PyTorch with affine=False).
    static Tensor[]
    gru(Tensor x, Tensor wih, Tensor whh, Tensor bih, Tensor bhh, Tensor h0)
    One GRU layer over x [T, B, I] (time first), PyTorch's equations and gate order (r, z, n): wih [3H, I], whh [3H, H], biases [3H], h0 [B, H].
    static Tensor[]
    gru(Tensor x, Tensor wih, Tensor whh, Tensor bih, Tensor bhh, Tensor h0, int[] lengths)
    As gru(Tensor, Tensor, Tensor, Tensor, Tensor, Tensor) for padded batches of lengths.
    static Tensor
    hingeLoss(Tensor input, Tensor target, F.Reduction reduction)
    Hinge loss with targets in {-1, 1}: max(0, 1 - target * input), Keras's hinge (mean).
    static Tensor
    huberLoss(Tensor input, Tensor target, float delta, F.Reduction reduction)
    Huber loss with threshold delta: 0.5 d^2 where |d| < delta, else delta (|d| - delta / 2), d = input - target (PyTorch's huber_loss).
    static Tensor
    instanceNorm(Tensor x, Tensor weight, Tensor bias, float eps)
    InstanceNorm: GroupNorm with one channel per group (affine when weight and bias are given).
    static Tensor
    interpolate(Tensor x, int height, int width, boolean bilinear, boolean alignCorners)
    Resizes x [N, C, H, W] to [N, C, height, width], nearest or bilinear (PyTorch's interpolate with size).
    static Tensor
    l1Loss(Tensor input, Tensor target, F.Reduction reduction)
    Mean absolute error (L1), PyTorch's l1_loss.
    static Tensor
    layerNorm(Tensor x, Tensor weight, Tensor bias, float eps)
    LayerNorm over the last axis: (x - mean) / sqrt(var + eps) * weight + bias (bias may be null).
    static Tensor
    linear(Tensor x, Tensor weight, Tensor bias)
    y = x W^T + b, with W stored [out, in] and b of shape [out] (b may be null); one fused GEMM. x may have any leading dimensions: [..., in] gives [..., out].
    static Tensor
    linearQ8(Tensor x, Tensor weight, Tensor bias)
    y = x W^T + b with Q8_0 weights W [out, in]; the gradient reaches x, not W.
    static Tensor
    linearRelu(Tensor x, Tensor weight, Tensor bias)
    relu(x W^T + b), with the bias and ReLU fused into the GEMM epilogue.
    static Tensor
    localResponseNorm(Tensor x, int size, float alpha, float beta, float k)
    Local response normalisation across channels, PyTorch's local_response_norm (k 1, alpha 1e-4, beta 0.75 by default).
    static Tensor
    log sigmoid(x) = -softplus(-x), without overflow.
    static Tensor
    lpNormalize(Tensor x, int p, int axis, float eps)
    x / max(||x||_p, eps) along axis, p = 1 or 2 (torch.nn.functional.normalize; ONNX LpNormalization with eps 0).
    static Tensor[]
    lstm(Tensor x, Tensor wih, Tensor whh, Tensor bih, Tensor bhh, Tensor h0, Tensor c0)
    One LSTM layer over x [T, B, I] (time first), PyTorch's equations and gate order (i, f, g, o): wih [4H, I], whh [4H, H], biases [4H], h0 and c0 [B, H].
    static Tensor[]
    lstm(Tensor x, Tensor wih, Tensor whh, Tensor bih, Tensor bhh, Tensor h0, Tensor c0, int[] lengths)
    As lstm(Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor) for padded batches: INT32 lengths [B] (null: all T); outputs past a sequence's length are 0 and h_n, c_n are each sequence's last valid state.
    static Tensor
    mapeLoss(Tensor input, Tensor target, F.Reduction reduction)
    Mean absolute percentage error: 100 |target - input| / max(|target|, eps) (Keras's MAPE, eps 1e-7).
    static Tensor
    marginRankingLoss(Tensor x1, Tensor x2, Tensor target, float margin, F.Reduction reduction)
    Margin ranking loss: max(0, -target (x1 - x2) + margin), target 1 or -1.
    static Tensor
    maxPool1d(Tensor x, int kernel, int stride, int padding)
    Max pooling of x [N, C, L] over windows of kernel with the given stride and padding (padding never wins), PyTorch's max_pool1d.
    static Tensor
    maxPool2d(Tensor x, int kernel, int stride, int padding)
    Max pooling of x [N, C, H, W] over square windows of kernel with the given stride and padding, PyTorch's max_pool2d.
    static Tensor
    maxPool3d(Tensor x, int kernel, int stride, int padding)
    Max pooling of x [N, C, D, H, W] over cubic windows of kernel (padding never wins), PyTorch's max_pool3d.
    static Tensor
    mseLoss(Tensor input, Tensor target)
    Mean squared error between input and target (same shape).
    static Tensor
    mseLoss(Tensor input, Tensor target, F.Reduction reduction)
    Squared error between input and target (same shape), reduced as reduction says.
    static Tensor
    msleLoss(Tensor input, Tensor target, F.Reduction reduction)
    Mean squared logarithmic error: (log1p(input) - log1p(target))^2 (Keras's MSLE).
    static Tensor
    multiHeadAttention(Tensor x, Tensor wqkv, Tensor bqkv, Tensor wout, Tensor bout, int heads, Tensor mask, boolean causal)
    Multi-head self-attention on x [B, T, D], as PyTorch's nn.MultiheadAttention with batch_first: q, k, v = split(x Wqkv^T + bqkv), heads heads, then the output projection.
    static Tensor
    multinomial(Tensor weights, Tensor seed, int stream)
    One INT32 index per row of non-negative weights [..., V] (probabilities, or any unnormalised weights), drawn in proportion to the weight.
    static Tensor
    negativeSamplingLoss(Tensor hidden, Tensor target, Tensor negatives)
    Negative-sampling loss of word2vec (Mikolov et al., 2013): hidden [B, D] (the centre word, or the averaged context), target [B, D] (the output vector of the true word) and negatives [B, K, D] (output vectors of sampled words); mean over B of -log sigmoid(h . t) - sum_k log sigmoid(-h . n_k).
    static Tensor
    nll(Tensor logp, Tensor labels)
    Per-sample negative log-likelihood of INT32 labels under log-probabilities logp.
    static Tensor
    nllLoss(Tensor logp, Tensor labels, F.Reduction reduction)
    Negative log-likelihood of INT32 labels under log-probabilities [N, C], reduced.
    static Tensor
    nonzero(Tensor x, int size, int fill)
    torch.nonzero_static: INT32 coordinates [size, rank] of the first size non-zero elements, the rest of the rows fill.
    static Tensor
    normalizeImage(Tensor x, float[] mean, float[] std)
    Normalises images x [N, C, H, W] per channel: (x - mean[c]) / std[c], as torchvision's Normalize.
    static Tensor
    oneHot(Tensor labels, int classes)
    INT32 labels [...] to a one-hot [..., classes].
    static Tensor
    pad(Tensor x, int[] before, int[] after, F.PadMode mode, float value)
    Pads the spatial axes of x [N, C, ...]: before[k] and after[k] elements on spatial axis k, PyTorch's F.pad for constant, reflect and replicate modes.
    static Tensor
    Euclidean distance of each row pair (last axis), PyTorch's pairwise_distance with eps 1e-6.
    static Tensor
    poissonNllLoss(Tensor input, Tensor target, boolean logInput, float eps, F.Reduction reduction)
    Poisson negative log-likelihood, PyTorch's poisson_nll_loss without the Stirling term: exp(input) - target input with logInput, else input - target log(input + eps).
    static Tensor
    pool(Tensor x, F.PoolMode mode, int[] kernel, int[] stride, int[] pads, int[] dilation, boolean ceil, boolean countIncludePad, float p)
    Pooling over 1 to 3 spatial axes of x [N, C, ...] with a size per axis, as ONNX's MaxPool, AveragePool and LpPool and PyTorch's pooling layers: pads holds the padding before every axis, then after every axis; ceil rounds the output size up (a window must still start inside the input or its leading padding); average pooling counts the padding when countIncludePad (never the positions added by ceil); LP pooling computes (sum |x|^p)^(1/p).
    static Tensor
    prelu(Tensor x, Tensor weight)
    PReLU: x where x > 0, else a x, with a learnable slope weight of one element or one per channel (axis 1 of x, as PyTorch).
    static Tensor
    FP32 x (last axis a multiple of 32) quantised to Q8_0, GGUF's 8-bit block format.
    static Tensor
    reverseSequences(Tensor x, int[] lengths)
    x [T, B, ...] with each sequence reversed within its length (padding left in place), as a bidirectional layer reads it; lengths null reverses the whole time axis.
    static Tensor
    RGB images [N, 3, H, W] to luminance [N, 1, H, W] with torchvision's weights (0.2989, 0.587, 0.114).
    static Tensor
    rmsNorm(Tensor x, Tensor weight, float eps)
    RMSNorm over the last axis: x / sqrt(mean(x^2) + eps) * weight.
    static Tensor
    rope(Tensor x, float base, int offset, boolean interleaved)
    Rotary position embedding of x [..., T, D] at positions offset .. offset + T - 1, frequency base^(-2i / D) for pair i.
    static Tensor
    ropeHeads(Tensor x, float base, int offset, boolean interleaved)
    Rotary position embedding of x [..., T, H, D] (the heads of a token share its position); see rope(Tensor, float, int, boolean).
    static Tensor
    ropeHeads(Tensor x, float base, RopeScaling scaling, int offset, boolean interleaved)
    ropeHeads(Tensor, float, int, boolean) with the frequencies rescaled for longer contexts (Llama 3.1's rope_scaling; null for none).
    static Tensor
    rrelu(Tensor x, float lower, float upper, boolean training, Tensor seed)
    RReLU (Xu et al., 2015): in training, a slope drawn uniformly from [lower, upper) for every element, seeded by the INT32 [2] seed tensor (see Tensor.seed(long)); in evaluation the mean slope (lower + upper) / 2.
    static Tensor
    scaledDotProductAttention(Tensor q, Tensor k, Tensor v, Tensor mask, boolean causal, float scale)
    Scaled dot-product attention, PyTorch semantics: softmax(q k^T * scale + mask) v, for q [..., T, E], k [..., S, E], v [..., S, Ev].
    static Tensor
    scaledDotProductAttentionBTHD(Tensor q, Tensor k, Tensor v, Tensor mask, boolean causal, float scale)
    Scaled dot-product attention on q [B, T, H, E], k and v [B, S, H, E] (heads after tokens, as the projections produce them), returning [B, T, H, E]; otherwise as scaledDotProductAttention(Tensor, Tensor, Tensor, Tensor, boolean, float).
    static Tensor
    scatterND(Tensor data, Tensor indices, Tensor updates, String reduction)
    A copy of data with the update slices written ("none"), added ("add") or multiplied ("mul") at the INT32 index rows indices [..., k] (ONNX ScatterND); differentiable for none and add.
    static Tensor
    segmentReduce(Tensor x, int[] offsets, F.BagMode mode)
    Sum, mean or max of the row segments of x [rows, D]: segment b covers rows offsets[b] .. offsets[b + 1] - 1, the last to the end; an empty segment gives zeros.
    static Tensor
    smoothL1Loss(Tensor input, Tensor target, float beta, F.Reduction reduction)
    Smooth L1 loss, PyTorch's smooth_l1_loss: Huber with delta = beta, divided by beta.
    static Tensor
    spaceToDepth(Tensor x, int block)
    Moves blocks of space into channels: x [N, C, H, W] to [N, C b^2, H / b, W / b] (ONNX SpaceToDepth; inverse of DCR depthToSpace).
    static Tensor
    Sparsemax (Martins and Astudillo, 2016) along the last axis: the Euclidean projection onto the probability simplex, max(x - tau, 0) with tau set by the sorted support.
    static Tensor
    squaredHingeLoss(Tensor input, Tensor target, F.Reduction reduction)
    Squared hinge loss: max(0, 1 - target * input)^2.
    static Tensor
    swiglu(Tensor gate, Tensor up)
    SwiGLU gating, silu(gate) * up, as in the Llama feed-forward block.
    static Tensor
    tripletMarginLoss(Tensor anchor, Tensor positive, Tensor negative, float margin, F.Reduction reduction)
    Triplet margin loss: max(d(anchor, positive) - d(anchor, negative) + margin, 0).
    static Tensor[]
    unique(Tensor x, boolean returnInverse, boolean returnCounts)
    torch.unique (sorted): the distinct values of x, then (when asked for, else null) the INT32 index of each element's value among them, shaped like x, and the INT32 count of each value.
    static Tensor
    upsampleBilinear(Tensor x, float scale, boolean alignCorners)
    Bilinear upsampling of x [N, C, H, W] by scale.
    static Tensor
    upsampleNearest(Tensor x, float scale)
    Nearest-neighbour upsampling of x [N, C, H, W] by scale (PyTorch's mode='nearest').
    static Tensor
    Weight normalisation (Salimans and Kingma): w = g v / ||v|| with the norm over every axis but 0, g of shape [out] (PyTorch's weight_norm with dim 0).
    static Tensor
    where(Tensor cond, Tensor a, Tensor b)
    cond !

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • linear

      public static Tensor linear(Tensor x, Tensor weight, Tensor bias)
      y = x W^T + b, with W stored [out, in] and b of shape [out] (b may be null); one fused GEMM. x may have any leading dimensions: [..., in] gives [..., out].
    • linearRelu

      public static Tensor linearRelu(Tensor x, Tensor weight, Tensor bias)
      relu(x W^T + b), with the bias and ReLU fused into the GEMM epilogue.
    • nll

      public static Tensor nll(Tensor logp, Tensor labels)
      Per-sample negative log-likelihood of INT32 labels under log-probabilities logp.
    • crossEntropy

      public static Tensor crossEntropy(Tensor logits, Tensor labels)
      Mean cross-entropy between logits [N, C] and INT32 class labels [N] (fused kernels).
    • conv2d

      public static Tensor conv2d(Tensor x, Tensor weight, Tensor bias, int stride, int padding, int dilation, int groups)
      2-D convolution (NCHW), square kernel; bias may be null.
    • conv2dRelu

      public static Tensor conv2dRelu(Tensor x, Tensor weight, Tensor bias, int stride, int padding, int dilation, int groups)
      relu(conv2d(...)) with the bias and ReLU fused into the convolution's output.
    • batchNorm

      public static Tensor batchNorm(Tensor x, Tensor runningMean, Tensor runningVar, Tensor weight, Tensor bias, boolean training, float momentum, float eps)
      Batch normalisation of an NCHW (or NC) tensor. In training it uses the batch statistics and updates runningMean/runningVar (persistent tensors) in place.
    • batchNormRelu

      public static Tensor batchNormRelu(Tensor x, Tensor runningMean, Tensor runningVar, Tensor weight, Tensor bias, boolean training, float momentum, float eps)
      relu(batchNorm(...)), fused.
    • convTranspose2d

      public static Tensor convTranspose2d(Tensor x, Tensor weight, Tensor bias, int stride, int padding, int outputPadding, int dilation, int groups)
      Transposed 2-D convolution (NCHW), PyTorch's conv_transpose2d: weight [Cin, Cout/groups, K, K], square kernel; bias may be null.
    • upsampleNearest

      public static Tensor upsampleNearest(Tensor x, float scale)
      Nearest-neighbour upsampling of x [N, C, H, W] by scale (PyTorch's mode='nearest').
    • upsampleBilinear

      public static Tensor upsampleBilinear(Tensor x, float scale, boolean alignCorners)
      Bilinear upsampling of x [N, C, H, W] by scale.
    • interpolate

      public static Tensor interpolate(Tensor x, int height, int width, boolean bilinear, boolean alignCorners)
      Resizes x [N, C, H, W] to [N, C, height, width], nearest or bilinear (PyTorch's interpolate with size).
    • conv1d

      public static Tensor conv1d(Tensor x, Tensor weight, Tensor bias, int stride, int padding, int dilation)
      1-D convolution of x [N, C, L] with weight [Cout, C, K]; bias may be null.
    • conv1d

      public static Tensor conv1d(Tensor x, Tensor weight, Tensor bias, int stride, int padding, int dilation, int groups)
      Grouped 1-D convolution: weight [Cout, C / groups, K]; groups = C is depthwise.
    • conv3d

      public static Tensor conv3d(Tensor x, Tensor weight, Tensor bias, int stride, int padding, int dilation)
      3-D convolution of x [N, C, D, H, W] with weight [Cout, C, KD, KH, KW]; bias may be null.
    • conv3d

      public static Tensor conv3d(Tensor x, Tensor weight, Tensor bias, int stride, int padding, int dilation, int groups)
      Grouped 3-D convolution: weight [Cout, C / groups, KD, KH, KW].
    • conv

      public static Tensor conv(Tensor x, Tensor weight, Tensor bias, int[] stride, int[] pads, int[] dilation, int groups)
      Convolution over 1 to 3 spatial axes with a size per axis: pads holds the padding before every axis, then after every axis (ONNX's order), so it may be asymmetric. Square symmetric 2-D cases take conv2d(Tensor, Tensor, Tensor, int, int, int, int)'s kernels; the rest the unfold (im2col) path.
    • pool

      public static Tensor pool(Tensor x, F.PoolMode mode, int[] kernel, int[] stride, int[] pads, int[] dilation, boolean ceil, boolean countIncludePad, float p)
      Pooling over 1 to 3 spatial axes of x [N, C, ...] with a size per axis, as ONNX's MaxPool, AveragePool and LpPool and PyTorch's pooling layers: pads holds the padding before every axis, then after every axis; ceil rounds the output size up (a window must still start inside the input or its leading padding); average pooling counts the padding when countIncludePad (never the positions added by ceil); LP pooling computes (sum |x|^p)^(1/p).
    • maxPool1d

      public static Tensor maxPool1d(Tensor x, int kernel, int stride, int padding)
      Max pooling of x [N, C, L] over windows of kernel with the given stride and padding (padding never wins), PyTorch's max_pool1d.
    • avgPool1d

      public static Tensor avgPool1d(Tensor x, int kernel, int stride, int padding)
      Average pooling of x [N, C, L], PyTorch's avg_pool1d; padding counts in the divisor, as PyTorch's default.
    • maxPool3d

      public static Tensor maxPool3d(Tensor x, int kernel, int stride, int padding)
      Max pooling of x [N, C, D, H, W] over cubic windows of kernel (padding never wins), PyTorch's max_pool3d.
    • avgPool3d

      public static Tensor avgPool3d(Tensor x, int kernel, int stride, int padding)
      Average pooling of x [N, C, D, H, W] over cubic windows, PyTorch's avg_pool3d; padding counts in the divisor.
    • adaptiveAvgPool

      public static Tensor adaptiveAvgPool(Tensor x, int... out)
      Adaptive average pooling of x [N, C, ...] (1 to 3 spatial axes) to the given output sizes (PyTorch's bins).
    • adaptiveMaxPool

      public static Tensor adaptiveMaxPool(Tensor x, int... out)
      Adaptive max pooling of x [N, C, ...] (1 to 3 spatial axes) to the given output sizes.
    • pad

      public static Tensor pad(Tensor x, int[] before, int[] after, F.PadMode mode, float value)
      Pads the spatial axes of x [N, C, ...]: before[k] and after[k] elements on spatial axis k, PyTorch's F.pad for constant, reflect and replicate modes.
    • maxPool2d

      public static Tensor maxPool2d(Tensor x, int kernel, int stride, int padding)
      Max pooling of x [N, C, H, W] over square windows of kernel with the given stride and padding, PyTorch's max_pool2d.
    • avgPool2d

      public static Tensor avgPool2d(Tensor x, int kernel, int stride, int padding)
      Average pooling of x [N, C, H, W] over square windows, PyTorch's avg_pool2d; padding counts in the divisor, as PyTorch's default.
    • globalAvgPool2d

      public static Tensor globalAvgPool2d(Tensor x)
      Mean over the spatial dimensions: [N, C, H, W] to [N, C].
    • dropout

      public static Tensor dropout(Tensor x, float p, Tensor seed, int stream)
      Inverted dropout with a fresh mask per step: seed is an INT32 [2] host tensor.
    • layerNorm

      public static Tensor layerNorm(Tensor x, Tensor weight, Tensor bias, float eps)
      LayerNorm over the last axis: (x - mean) / sqrt(var + eps) * weight + bias (bias may be null).
    • groupNorm

      public static Tensor groupNorm(Tensor x, int groups, Tensor weight, Tensor bias, float eps)
      GroupNorm (Wu and He) of x [N, C, ...] in groups groups of channels: each (sample, group) normalised over its channels and positions, then a per-channel affine (weight and bias may be null, as PyTorch with affine=False).
    • instanceNorm

      public static Tensor instanceNorm(Tensor x, Tensor weight, Tensor bias, float eps)
      InstanceNorm: GroupNorm with one channel per group (affine when weight and bias are given).
    • localResponseNorm

      public static Tensor localResponseNorm(Tensor x, int size, float alpha, float beta, float k)
      Local response normalisation across channels, PyTorch's local_response_norm (k 1, alpha 1e-4, beta 0.75 by default).
    • weightNorm

      public static Tensor weightNorm(Tensor v, Tensor g)
      Weight normalisation (Salimans and Kingma): w = g v / ||v|| with the norm over every axis but 0, g of shape [out] (PyTorch's weight_norm with dim 0).
    • rmsNorm

      public static Tensor rmsNorm(Tensor x, Tensor weight, float eps)
      RMSNorm over the last axis: x / sqrt(mean(x^2) + eps) * weight.
    • embedding

      public static Tensor embedding(Tensor index, Tensor weight)
      Rows of weight [V, D] picked by the INT32 index: shape index.shape + [D].
    • embedding

      public static Tensor embedding(Tensor index, Tensor weight, int paddingIdx)
      As embedding(Tensor, Tensor); row paddingIdx receives no gradient.
    • attentionSoftmax

      public static Tensor attentionSoftmax(Tensor scores, float scale, boolean causal)
      softmax(scores * scale) over the last axis of attention scores [..., T, S], in one kernel; causal: query t sees keys 0 .. t, as PyTorch's is_causal.
    • fakeQuantize

      public static Tensor fakeQuantize(Tensor x, float scale, float lowest, int bits)
      Quantises x to bits (8 or 16) bits and back: clamp(round_half_even(x / scale), lowest, lowest + 2^bits - 1) * scale, lowest being the lowest code minus the zero point (ONNX QuantizeLinear followed by DequantizeLinear).
    • scaledDotProductAttention

      public static Tensor scaledDotProductAttention(Tensor q, Tensor k, Tensor v, Tensor mask, boolean causal, float scale)
      Scaled dot-product attention, PyTorch semantics: softmax(q k^T * scale + mask) v, for q [..., T, E], k [..., S, E], v [..., S, Ev]. mask is an additive mask broadcast to [..., T, S] (0 to keep, a large negative number to drop), or null; causal lets query t see keys 0 .. t (PyTorch's is_causal). A negative scale means 1 / sqrt(E).
    • multiHeadAttention

      public static Tensor multiHeadAttention(Tensor x, Tensor wqkv, Tensor bqkv, Tensor wout, Tensor bout, int heads, Tensor mask, boolean causal)
      Multi-head self-attention on x [B, T, D], as PyTorch's nn.MultiheadAttention with batch_first: q, k, v = split(x Wqkv^T + bqkv), heads heads, then the output projection. Wqkv is [3D, D] (rows q, then k, then v), Wout [D, D]; biases may be null.
    • scaledDotProductAttentionBTHD

      public static Tensor scaledDotProductAttentionBTHD(Tensor q, Tensor k, Tensor v, Tensor mask, boolean causal, float scale)
      Scaled dot-product attention on q [B, T, H, E], k and v [B, S, H, E] (heads after tokens, as the projections produce them), returning [B, T, H, E]; otherwise as scaledDotProductAttention(Tensor, Tensor, Tensor, Tensor, boolean, float). The fused kernels read this layout directly.
    • ropeHeads

      public static Tensor ropeHeads(Tensor x, float base, int offset, boolean interleaved)
      Rotary position embedding of x [..., T, H, D] (the heads of a token share its position); see rope(Tensor, float, int, boolean).
    • ropeHeads

      public static Tensor ropeHeads(Tensor x, float base, RopeScaling scaling, int offset, boolean interleaved)
      ropeHeads(Tensor, float, int, boolean) with the frequencies rescaled for longer contexts (Llama 3.1's rope_scaling; null for none).
    • rope

      public static Tensor rope(Tensor x, float base, int offset, boolean interleaved)
      Rotary position embedding of x [..., T, D] at positions offset .. offset + T - 1, frequency base^(-2i / D) for pair i. interleaved pairs (2i, 2i + 1) as GPT-J and Meta's Llama code; otherwise (i, i + D/2), the rotate-half form of Hugging Face's Llama.
    • multinomial

      public static Tensor multinomial(Tensor weights, Tensor seed, int stream)
      One INT32 index per row of non-negative weights [..., V] (probabilities, or any unnormalised weights), drawn in proportion to the weight. seed is an INT32 [2] host tensor; a new seed draws new samples without recompiling.
    • mseLoss

      public static Tensor mseLoss(Tensor input, Tensor target)
      Mean squared error between input and target (same shape).
    • mseLoss

      public static Tensor mseLoss(Tensor input, Tensor target, F.Reduction reduction)
      Squared error between input and target (same shape), reduced as reduction says.
    • binaryCrossEntropy

      public static Tensor binaryCrossEntropy(Tensor p, Tensor target)
      Binary cross-entropy of probabilities p in [0, 1] against target, PyTorch semantics: the logs are clamped at -100, so p = 0 or 1 gives finite values and gradients.
    • binaryCrossEntropy

      public static Tensor binaryCrossEntropy(Tensor p, Tensor target, F.Reduction reduction)
      binaryCrossEntropy(Tensor, Tensor) reduced as reduction says.
    • binaryCrossEntropyWithLogits

      public static Tensor binaryCrossEntropyWithLogits(Tensor logits, Tensor target)
      Binary cross-entropy on logits, sigmoid folded in without overflow (PyTorch's BCEWithLogits).
    • binaryCrossEntropyWithLogits

      public static Tensor binaryCrossEntropyWithLogits(Tensor logits, Tensor target, F.Reduction reduction)
    • l1Loss

      public static Tensor l1Loss(Tensor input, Tensor target, F.Reduction reduction)
      Mean absolute error (L1), PyTorch's l1_loss.
    • huberLoss

      public static Tensor huberLoss(Tensor input, Tensor target, float delta, F.Reduction reduction)
      Huber loss with threshold delta: 0.5 d^2 where |d| < delta, else delta (|d| - delta / 2), d = input - target (PyTorch's huber_loss).
    • smoothL1Loss

      public static Tensor smoothL1Loss(Tensor input, Tensor target, float beta, F.Reduction reduction)
      Smooth L1 loss, PyTorch's smooth_l1_loss: Huber with delta = beta, divided by beta.
    • nllLoss

      public static Tensor nllLoss(Tensor logp, Tensor labels, F.Reduction reduction)
      Negative log-likelihood of INT32 labels under log-probabilities [N, C], reduced.
    • hingeLoss

      public static Tensor hingeLoss(Tensor input, Tensor target, F.Reduction reduction)
      Hinge loss with targets in {-1, 1}: max(0, 1 - target * input), Keras's hinge (mean).
    • squaredHingeLoss

      public static Tensor squaredHingeLoss(Tensor input, Tensor target, F.Reduction reduction)
      Squared hinge loss: max(0, 1 - target * input)^2.
    • cosineSimilarity

      public static Tensor cosineSimilarity(Tensor a, Tensor b, int axis, float eps)
      Cosine similarity along axis: a . b / (max(|a|, eps) max(|b|, eps)), PyTorch's cosine_similarity.
    • cosineProximityLoss

      public static Tensor cosineProximityLoss(Tensor input, Tensor target, F.Reduction reduction)
      Cosine proximity loss (DL4J, Keras): minus the cosine similarity along the last axis, reduced.
    • poissonNllLoss

      public static Tensor poissonNllLoss(Tensor input, Tensor target, boolean logInput, float eps, F.Reduction reduction)
      Poisson negative log-likelihood, PyTorch's poisson_nll_loss without the Stirling term: exp(input) - target input with logInput, else input - target log(input + eps).
    • ctcLoss

      public static Tensor ctcLoss(Tensor logProbs, Tensor targets, int[] inputLengths, int[] targetLengths, int blank, F.Reduction reduction)
      CTC loss, PyTorch's ctc_loss: log-probabilities [T, B, C] (log_softmax over C), INT32 targets [B, L] padded, input and target lengths; blank index blank. MEAN divides each sample's loss by its target length, then averages (PyTorch's default).
    • nonzero

      public static Tensor nonzero(Tensor x, int size, int fill)
      torch.nonzero_static: INT32 coordinates [size, rank] of the first size non-zero elements, the rest of the rows fill. Unlike Tensor.nonzero() it needs no host read, so it stays inside a compiled plan.
    • unique

      public static Tensor[] unique(Tensor x, boolean returnInverse, boolean returnCounts)
      torch.unique (sorted): the distinct values of x, then (when asked for, else null) the INT32 index of each element's value among them, shaped like x, and the INT32 count of each value.
    • logSigmoid

      public static Tensor logSigmoid(Tensor x)
      log sigmoid(x) = -softplus(-x), without overflow.
    • negativeSamplingLoss

      public static Tensor negativeSamplingLoss(Tensor hidden, Tensor target, Tensor negatives)
      Negative-sampling loss of word2vec (Mikolov et al., 2013): hidden [B, D] (the centre word, or the averaged context), target [B, D] (the output vector of the true word) and negatives [B, K, D] (output vectors of sampled words); mean over B of -log sigmoid(h . t) - sum_k log sigmoid(-h . n_k).
    • gloveLoss

      public static Tensor gloveLoss(Tensor wi, Tensor wj, Tensor bi, Tensor bj, Tensor logX, Tensor weight)
      GloVe's weighted least squares (Pennington et al., 2014) over a batch of co-occurrences: mean of weight (wi . wj + bi + bj - logX)^2, with wi, wj [B, D] and the rest [B].
    • pairwiseDistance

      public static Tensor pairwiseDistance(Tensor a, Tensor b)
      Euclidean distance of each row pair (last axis), PyTorch's pairwise_distance with eps 1e-6.
    • tripletMarginLoss

      public static Tensor tripletMarginLoss(Tensor anchor, Tensor positive, Tensor negative, float margin, F.Reduction reduction)
      Triplet margin loss: max(d(anchor, positive) - d(anchor, negative) + margin, 0).
    • contrastiveLoss

      public static Tensor contrastiveLoss(Tensor a, Tensor b, Tensor similar, float margin, F.Reduction reduction)
      Contrastive loss (Hadsell et al., 2006) with similar 1 for similar pairs and 0 for dissimilar ones: similar d^2 + (1 - similar) max(margin - d, 0)^2.
    • cosineEmbeddingLoss

      public static Tensor cosineEmbeddingLoss(Tensor a, Tensor b, Tensor target, float margin, F.Reduction reduction)
      Cosine embedding loss: 1 - cos(a, b) for target 1, max(0, cos(a, b) - margin) for -1.
    • marginRankingLoss

      public static Tensor marginRankingLoss(Tensor x1, Tensor x2, Tensor target, float margin, F.Reduction reduction)
      Margin ranking loss: max(0, -target (x1 - x2) + margin), target 1 or -1.
    • sparsemax

      public static Tensor sparsemax(Tensor x)
      Sparsemax (Martins and Astudillo, 2016) along the last axis: the Euclidean projection onto the probability simplex, max(x - tau, 0) with tau set by the sorted support.
    • msleLoss

      public static Tensor msleLoss(Tensor input, Tensor target, F.Reduction reduction)
      Mean squared logarithmic error: (log1p(input) - log1p(target))^2 (Keras's MSLE).
    • mapeLoss

      public static Tensor mapeLoss(Tensor input, Tensor target, F.Reduction reduction)
      Mean absolute percentage error: 100 |target - input| / max(|target|, eps) (Keras's MAPE, eps 1e-7).
    • gaussianKl

      public static Tensor gaussianKl(Tensor mu, Tensor logvar)
      KL divergence of N(mu, exp(logvar)) from N(0, 1), summed over every element: -0.5 * sum(1 + logvar - mu^2 - exp(logvar)), the VAE regulariser.
    • lstm

      public static Tensor[] lstm(Tensor x, Tensor wih, Tensor whh, Tensor bih, Tensor bhh, Tensor h0, Tensor c0)
      One LSTM layer over x [T, B, I] (time first), PyTorch's equations and gate order (i, f, g, o): wih [4H, I], whh [4H, H], biases [4H], h0 and c0 [B, H]. Returns {y [T, B, H], h_n, c_n} ([B, H] each); c_n carries no gradient.
    • lstm

      public static Tensor[] lstm(Tensor x, Tensor wih, Tensor whh, Tensor bih, Tensor bhh, Tensor h0, Tensor c0, int[] lengths)
      As lstm(Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor) for padded batches: INT32 lengths [B] (null: all T); outputs past a sequence's length are 0 and h_n, c_n are each sequence's last valid state.
    • reverseSequences

      public static Tensor reverseSequences(Tensor x, int[] lengths)
      x [T, B, ...] with each sequence reversed within its length (padding left in place), as a bidirectional layer reads it; lengths null reverses the whole time axis.
    • gru

      public static Tensor[] gru(Tensor x, Tensor wih, Tensor whh, Tensor bih, Tensor bhh, Tensor h0)
      One GRU layer over x [T, B, I] (time first), PyTorch's equations and gate order (r, z, n): wih [3H, I], whh [3H, H], biases [3H], h0 [B, H]. Returns {y [T, B, H], h_n [B, H]}.
    • gru

      public static Tensor[] gru(Tensor x, Tensor wih, Tensor whh, Tensor bih, Tensor bhh, Tensor h0, int[] lengths)
      As gru(Tensor, Tensor, Tensor, Tensor, Tensor, Tensor) for padded batches of lengths.
    • prelu

      public static Tensor prelu(Tensor x, Tensor weight)
      PReLU: x where x > 0, else a x, with a learnable slope weight of one element or one per channel (axis 1 of x, as PyTorch).
    • rrelu

      public static Tensor rrelu(Tensor x, float lower, float upper, boolean training, Tensor seed)
      RReLU (Xu et al., 2015): in training, a slope drawn uniformly from [lower, upper) for every element, seeded by the INT32 [2] seed tensor (see Tensor.seed(long)); in evaluation the mean slope (lower + upper) / 2.
    • where

      public static Tensor where(Tensor cond, Tensor a, Tensor b)
      cond != 0 ? a : b element-wise, the three broadcast together.
    • oneHot

      public static Tensor oneHot(Tensor labels, int classes)
      INT32 labels [...] to a one-hot [..., classes].
    • segmentReduce

      public static Tensor segmentReduce(Tensor x, int[] offsets, F.BagMode mode)
      Sum, mean or max of the row segments of x [rows, D]: segment b covers rows offsets[b] .. offsets[b + 1] - 1, the last to the end; an empty segment gives zeros. [segments, D].
    • embeddingBag

      public static Tensor embeddingBag(Tensor indices, int[] offsets, Tensor weight, F.BagMode mode)
      EmbeddingBag (PyTorch's, 1-D input with offsets): the rows of weight picked by the INT32 indices, combined per bag by mode. [bags, D].
    • quantizeQ8

      public static Tensor quantizeQ8(Tensor x)
      FP32 x (last axis a multiple of 32) quantised to Q8_0, GGUF's 8-bit block format.
    • dequantize

      public static Tensor dequantize(Tensor q)
      A Q8_0 tensor back to FP32.
    • linearQ8

      public static Tensor linearQ8(Tensor x, Tensor weight, Tensor bias)
      y = x W^T + b with Q8_0 weights W [out, in]; the gradient reaches x, not W.
    • flip

      public static Tensor flip(Tensor x, int... axes)
      x reversed along each of axes (torch.flip).
    • centerCrop

      public static Tensor centerCrop(Tensor x, int height, int width)
      The central height x width window of images [..., H, W] (torchvision's center_crop, top-left rounding down).
    • lpNormalize

      public static Tensor lpNormalize(Tensor x, int p, int axis, float eps)
      x / max(||x||_p, eps) along axis, p = 1 or 2 (torch.nn.functional.normalize; ONNX LpNormalization with eps 0).
    • depthToSpace

      public static Tensor depthToSpace(Tensor x, int block, String mode)
      Moves blocks of channels into space: x [N, C, H, W] to [N, C / b^2, H b, W b]. Mode "DCR" (depth, column, row; ONNX's default) or "CRD" (column, row, depth: torch pixel_shuffle).
    • spaceToDepth

      public static Tensor spaceToDepth(Tensor x, int block)
      Moves blocks of space into channels: x [N, C, H, W] to [N, C b^2, H / b, W / b] (ONNX SpaceToDepth; inverse of DCR depthToSpace).
    • gatherND

      public static Tensor gatherND(Tensor data, Tensor indices)
      Slices of data picked by INT32 index rows indices [..., k] (ONNX GatherND, batch_dims 0); differentiable in data.
    • scatterND

      public static Tensor scatterND(Tensor data, Tensor indices, Tensor updates, String reduction)
      A copy of data with the update slices written ("none"), added ("add") or multiplied ("mul") at the INT32 index rows indices [..., k] (ONNX ScatterND); differentiable for none and add.
    • fold

      public static Tensor fold(Tensor cols, int[] outputSize, int[] kernel, int[] stride, int[] pad, int[] dil)
      Folds columns back into an image, summing overlaps (torch fold, ONNX Col2Im); see Spatial.fold.
    • normalizeImage

      public static Tensor normalizeImage(Tensor x, float[] mean, float[] std)
      Normalises images x [N, C, H, W] per channel: (x - mean[c]) / std[c], as torchvision's Normalize.
    • rgbToGrayscale

      public static Tensor rgbToGrayscale(Tensor x)
      RGB images [N, 3, H, W] to luminance [N, 1, H, W] with torchvision's weights (0.2989, 0.587, 0.114).
    • swiglu

      public static Tensor swiglu(Tensor gate, Tensor up)
      SwiGLU gating, silu(gate) * up, as in the Llama feed-forward block.
    • accuracy

      public static float accuracy(Tensor logits, Tensor labels)
      Fraction of rows whose argmax equals the label; runs now and returns a host value.