Skip to content

internal #

vtl.nn.internal

Internal numerical kernels and helpers shared by VTL's neural-network modules: initialization, activation math, losses, optimizer updates, convolution and pooling kernels, recurrent operations, and normalization. These functions are implementation details and may change without compatibility guarantees.

Use layers, loss, and optimizers from application code. Backend-specific files are selected by V compile-time conditions; preserve CPU behavior when changing accelerator code.

fn adaptive_avgpool1d_backward #

fn adaptive_avgpool1d_backward[T](grad_out &vtl.Tensor[T], input &vtl.Tensor[T], output_size int) !&vtl.Tensor[T]

adaptive_avgpool1d_backward distributes each output gradient across its adaptive 1D window.

fn adaptive_avgpool1d_forward #

fn adaptive_avgpool1d_forward[T](input &vtl.Tensor[T], output_size int) !&vtl.Tensor[T]

adaptive_avgpool1d_forward applies 1D adaptive average pooling to match the target output_size.

fn adaptive_avgpool2d_backward #

fn adaptive_avgpool2d_backward[T](grad_out &vtl.Tensor[T], input &vtl.Tensor[T], output_size []int) !&vtl.Tensor[T]

adaptive_avgpool2d_backward distributes each output gradient over its corresponding adaptive mean region.

fn adaptive_avgpool2d_forward #

fn adaptive_avgpool2d_forward[T](input &vtl.Tensor[T], output_size []int) !&vtl.Tensor[T]

adaptive_avgpool2d_forward computes non-overlapping or overlapping adaptive mean regions whose boundaries are derived from the requested output shape.

fn adaptive_avgpool3d_backward #

fn adaptive_avgpool3d_backward[T](grad_out &vtl.Tensor[T], input &vtl.Tensor[T], output_size []int) !&vtl.Tensor[T]

adaptive_avgpool3d_backward distributes each output gradient across its adaptive 3D volume.

fn adaptive_avgpool3d_forward #

fn adaptive_avgpool3d_forward[T](input &vtl.Tensor[T], output_size []int) !&vtl.Tensor[T]

adaptive_avgpool3d_forward applies 3D adaptive average pooling to match the target spatial output_size.

fn adaptive_maxpool1d_backward #

fn adaptive_maxpool1d_backward[T](grad_out &vtl.Tensor[T], input &vtl.Tensor[T], output_size int) !&vtl.Tensor[T]

adaptive_maxpool1d_backward routes each output gradient to the maximum element in its 1D adaptive window.

fn adaptive_maxpool1d_forward #

fn adaptive_maxpool1d_forward[T](input &vtl.Tensor[T], output_size int) !&vtl.Tensor[T]

adaptive_maxpool1d_forward applies 1D adaptive max pooling to match the target output_size.

fn adaptive_maxpool2d_backward #

fn adaptive_maxpool2d_backward[T](grad_out &vtl.Tensor[T], input &vtl.Tensor[T], output_size []int) !&vtl.Tensor[T]

adaptive_maxpool2d_backward routes each output gradient to the maximum element in its adaptive region.

fn adaptive_maxpool2d_forward #

fn adaptive_maxpool2d_forward[T](input &vtl.Tensor[T], output_size []int) !&vtl.Tensor[T]

adaptive_maxpool2d_forward applies 2D adaptive max pooling to match the target spatial output_size.

fn adaptive_maxpool3d_backward #

fn adaptive_maxpool3d_backward[T](grad_out &vtl.Tensor[T], input &vtl.Tensor[T], output_size []int) !&vtl.Tensor[T]

adaptive_maxpool3d_backward routes each output gradient to the maximum element in its adaptive 3D volume.

fn adaptive_maxpool3d_forward #

fn adaptive_maxpool3d_forward[T](input &vtl.Tensor[T], output_size []int) !&vtl.Tensor[T]

adaptive_maxpool3d_forward applies 3D adaptive max pooling to match the target spatial output_size.

fn attention_backward_cuda_f64 #

fn attention_backward_cuda_f64(grad &vtl.Tensor[f64], input &vtl.Tensor[f64], w_q &vtl.Tensor[f64], w_k &vtl.Tensor[f64], w_v &vtl.Tensor[f64], w_o &vtl.Tensor[f64], q &vtl.Tensor[f64], k &vtl.Tensor[f64], v &vtl.Tensor[f64], attn_weights &vtl.Tensor[f64], merged &vtl.Tensor[f64], num_heads int, head_dim int) ![]&vtl.Tensor[f64]

attention_backward_cuda_f64 exists only so the layer dispatch compiles without -d cuda.

fn attention_cuda_eligible #

fn attention_cuda_eligible() bool

attention_cuda_eligible is always false without -d cuda, so the CPU path runs.

fn avgpool1d_backward #

fn avgpool1d_backward[T](grad_out &vtl.Tensor[T], input &vtl.Tensor[T], kernel int, padding int, stride int) !&vtl.Tensor[T]

avgpool1d_backward distributes output gradients across the 1D average pooling kernel windows.

fn avgpool1d_forward #

fn avgpool1d_forward[T](input &vtl.Tensor[T], kernel int, padding int, stride int) !&vtl.Tensor[T]

avgpool1d_forward computes 1D average pooling over a 3D input tensor.

fn avgpool2d_backward #

fn avgpool2d_backward[T](grad_out &vtl.Tensor[T], input &vtl.Tensor[T], kernel []int, padding []int, stride []int) !&vtl.Tensor[T]

avgpool2d_backward exposes this operation as part of the public API.

fn avgpool2d_forward #

fn avgpool2d_forward[T](input &vtl.Tensor[T], kernel []int, padding []int, stride []int) !&vtl.Tensor[T]

avgpool2d_forward exposes this operation as part of the public API.

fn avgpool3d_backward #

fn avgpool3d_backward[T](grad_out &vtl.Tensor[T], input &vtl.Tensor[T], kernel []int, padding []int, stride []int) !&vtl.Tensor[T]

avgpool3d_backward distributes output gradients across the 3D average pooling kernel volumes.

fn avgpool3d_forward #

fn avgpool3d_forward[T](input &vtl.Tensor[T], kernel []int, padding []int, stride []int) !&vtl.Tensor[T]

avgpool3d_forward computes 3D average pooling over a 5D input tensor.

fn batchnorm1d_backward #

fn batchnorm1d_backward[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], gamma &vtl.Tensor[T], beta &vtl.Tensor[T], mean &vtl.Tensor[T], var_ &vtl.Tensor[T], eps f64) ![]&vtl.Tensor[T]

batchnorm1d_backward computes gradients w.r.t. input, gamma, beta.

fn batchnorm1d_backward_mode #

fn batchnorm1d_backward_mode[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], gamma &vtl.Tensor[T], _beta &vtl.Tensor[T], mean &vtl.Tensor[T], var_ &vtl.Tensor[T], eps f64, training bool) ![]&vtl.Tensor[T]

batchnorm1d_backward_mode computes gradients using batch or fixed running statistics, depending on whether the layer is training.

fn batchnorm1d_forward #

fn batchnorm1d_forward[T](input &vtl.Tensor[T], gamma &vtl.Tensor[T], beta &vtl.Tensor[T], running_mean &vtl.Tensor[T], running_var &vtl.Tensor[T], eps f64) !&vtl.Tensor[T]

batchnorm1d_forward computes batch norm using running mean/var (inference path).

fn batchnorm1d_training #

fn batchnorm1d_training[T](input &vtl.Tensor[T], gamma &vtl.Tensor[T], beta &vtl.Tensor[T], eps f64) !(&vtl.Tensor[T], &vtl.Tensor[T], &vtl.Tensor[T])

batchnorm1d_training computes batch norm using batch stats (training path).

fn batchnorm2d_backward_mode #

fn batchnorm2d_backward_mode[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], gamma &vtl.Tensor[T], beta &vtl.Tensor[T], mean &vtl.Tensor[T], var_ &vtl.Tensor[T], eps f64, training bool) ![]&vtl.Tensor[T]

batchnorm2d_backward_mode computes gradients w.r.t. input, gamma and beta for BatchNorm2D. Training-mode gradients use cuDNN when batchnorm_cudnn_eligible says so (-d cuda, VTL_USE_CUDA=1, VTL_CUDA_BACKWARD=1); otherwise the CPU path runs. Evaluation mode always uses the CPU path.

fn batchnorm2d_forward #

fn batchnorm2d_forward[T](input &vtl.Tensor[T], gamma &vtl.Tensor[T], beta &vtl.Tensor[T], running_mean &vtl.Tensor[T], running_var &vtl.Tensor[T], eps f64) !&vtl.Tensor[T]

batchnorm2d_forward computes BatchNorm2D using running mean/var (inference path). Input is [N, C, H, W]; gamma, beta and the running statistics are per-channel vectors of length C.

fn batchnorm2d_training #

fn batchnorm2d_training[T](input &vtl.Tensor[T], gamma &vtl.Tensor[T], beta &vtl.Tensor[T], eps f64) !(&vtl.Tensor[T], &vtl.Tensor[T], &vtl.Tensor[T])

batchnorm2d_training computes BatchNorm2D using per-channel batch statistics over NHW elements (training path). Returns the output plus the biased batch mean and variance as [C] tensors.

fn batchnorm3d_backward_mode #

fn batchnorm3d_backward_mode[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], gamma &vtl.Tensor[T], _beta &vtl.Tensor[T], mean &vtl.Tensor[T], var_ &vtl.Tensor[T], eps f64, training bool) ![]&vtl.Tensor[T]

batchnorm3d_backward_mode computes gradients w.r.t. input, gamma and beta for BatchNorm3D, using batch statistics in training mode and the fixed running statistics as a constant affine transform in evaluation mode. Training-mode gradients use cuDNN when batchnorm_cudnn_eligible says so.

fn batchnorm3d_forward #

fn batchnorm3d_forward[T](input &vtl.Tensor[T], gamma &vtl.Tensor[T], beta &vtl.Tensor[T], running_mean &vtl.Tensor[T], running_var &vtl.Tensor[T], eps f64) !&vtl.Tensor[T]

batchnorm3d_forward computes BatchNorm3D using running mean/var (inference path); gamma, beta and running statistics are [C] vectors.

fn batchnorm3d_training #

fn batchnorm3d_training[T](input &vtl.Tensor[T], gamma &vtl.Tensor[T], beta &vtl.Tensor[T], eps f64) !(&vtl.Tensor[T], &vtl.Tensor[T], &vtl.Tensor[T])

batchnorm3d_training computes BatchNorm3D using per-channel batch statistics over NDH*W elements (training path). Returns the output plus the biased batch mean and variance as [C] tensors.

fn bce #

@[direct_array_access; inline]
fn bce[T](input &vtl.Tensor[T], target &vtl.Tensor[T]) !&vtl.Tensor[T]

bce exposes this operation as part of the public API.

fn bce_backward #

fn bce_backward[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], target &vtl.Tensor[T], from_logits bool) !&vtl.Tensor[T]

bce_backward computes the gradient of mean BCE w.r.t. its input. Logit inputs use the simplified sigmoid(logit) - target derivative; probability inputs use the BCELoss derivative with PyTorch's denominator floor.

fn bce_with_logits #

@[direct_array_access; inline]
fn bce_with_logits[T](input &vtl.Tensor[T], target &vtl.Tensor[T]) !&vtl.Tensor[T]

bce_with_logits computes mean binary cross entropy directly from logits using the stable softplus form: max(x, 0) - x*y + log(1 + exp(-abs(x))).

fn compute_fans #

fn compute_fans(shape []int) (int, int)

compute_fans exposes this operation as part of the public API.

fn conv1d_backward #

fn conv1d_backward[T](grad_output &vtl.Tensor[T], input &vtl.Tensor[T], weight &vtl.Tensor[T],
	bias &vtl.Tensor[T], config Conv1DConfig) ![]&vtl.Tensor[T]

conv1d_backward computes derivatives for input, weight, and bias.

fn conv1d_forward #

fn conv1d_forward[T](input &vtl.Tensor[T], weight &vtl.Tensor[T], bias &vtl.Tensor[T],
	config Conv1DConfig) !&vtl.Tensor[T]

conv1d_forward computes grouped cross-correlation over input [batch, channels, length]. Weight layout is [out_channels, in_channels/groups, kernel_size].

fn conv2d_backward #

fn conv2d_backward[T](grad_out &vtl.Tensor[T],
	input &vtl.Tensor[T],
	weight &vtl.Tensor[T],
	bias &vtl.Tensor[T],
	kernel_size []int,
	config Conv2DConfig) ![]&vtl.Tensor[T]

conv2d_backward computes gradients for input, weight, bias. Returns [d_input, d_weight, d_bias].

fn conv2d_backward_cpu_f32 #

fn conv2d_backward_cpu_f32(grad_out &vtl.Tensor[f32], input &vtl.Tensor[f32], weight &vtl.Tensor[f32],
	bias &vtl.Tensor[f32], kernel_size []int, config Conv2DConfig) ![]&vtl.Tensor[f32]

conv2d_backward_cpu_f32 computes Conv2D gradients on CPU (reference).

fn conv2d_backward_cpu_f64 #

fn conv2d_backward_cpu_f64(grad_out &vtl.Tensor[f64], input &vtl.Tensor[f64], weight &vtl.Tensor[f64],
	bias &vtl.Tensor[f64], kernel_size []int, config Conv2DConfig) ![]&vtl.Tensor[f64]

conv2d_backward_cpu_f64 computes Conv2D gradients on CPU (reference).

fn conv2d_backward_f32 #

fn conv2d_backward_f32(grad_out &vtl.Tensor[f32], input &vtl.Tensor[f32], weight &vtl.Tensor[f32],
	bias &vtl.Tensor[f32], kernel_size []int, config Conv2DConfig) ![]&vtl.Tensor[f32]

conv2d_backward_f32 uses Vulkan GEMM for d_weight when eligible, else full CPU.

fn conv2d_backward_f64 #

fn conv2d_backward_f64(grad_out &vtl.Tensor[f64], input &vtl.Tensor[f64], weight &vtl.Tensor[f64],
	bias &vtl.Tensor[f64], kernel_size []int, config Conv2DConfig) ![]&vtl.Tensor[f64]

conv2d_backward_f64 exposes this operation as part of the public API.

fn conv2d_backward_vulkan_f32 #

fn conv2d_backward_vulkan_f32(grad_out &vtl.Tensor[f32], input &vtl.Tensor[f32], weight &vtl.Tensor[f32],
	bias &vtl.Tensor[f32], kernel_size []int, config Conv2DConfig) ![]&vtl.Tensor[f32]

conv2d_backward_vulkan_f32 exposes this operation as part of the public API.

fn conv2d_cuda_eligible #

fn conv2d_cuda_eligible(kernel_size []int, config Conv2DConfig) bool

conv2d_cuda_eligible is false without -d cuda.

fn conv2d_forward #

fn conv2d_forward[T](input &vtl.Tensor[T],
	weight &vtl.Tensor[T],
	bias &vtl.Tensor[T],
	kernel_size []int,
	config Conv2DConfig) !&vtl.Tensor[T]

conv2d_forward implements the forward pass of 2D convolution. input: [batch, in_ch, H, W] weight: [out_ch, in_ch/groups, k_h, k_w] bias: [1, out_ch] config: padding, stride, dilation, groups returns: [batch, out_ch, out_H, out_W]

fn conv2d_forward_cpu_f32 #

fn conv2d_forward_cpu_f32(input &vtl.Tensor[f32],
	weight &vtl.Tensor[f32],
	bias &vtl.Tensor[f32],
	kernel_size []int,
	config Conv2DConfig) !&vtl.Tensor[f32]

conv2d_forward_cpu_f32 uses direct indexing for compact row-major tensors and preserves the stride-aware reference path for views.

fn conv2d_forward_cpu_f64 #

fn conv2d_forward_cpu_f64(input &vtl.Tensor[f64],
	weight &vtl.Tensor[f64],
	bias &vtl.Tensor[f64],
	kernel_size []int,
	config Conv2DConfig) !&vtl.Tensor[f64]

conv2d_forward_cpu_f64 runs the contiguous fast path when tensors use row-major storage, and preserves the reference implementation for views.

fn conv2d_forward_cuda_f64 #

fn conv2d_forward_cuda_f64(input &vtl.Tensor[f64],
	weight &vtl.Tensor[f64],
	bias &vtl.Tensor[f64],
	kernel_size []int,
	config Conv2DConfig) !&vtl.Tensor[f64]

conv2d_forward_cuda_f64 stub when not built with -d cuda.

fn conv2d_forward_f32 #

fn conv2d_forward_f32(input &vtl.Tensor[f32],
	weight &vtl.Tensor[f32],
	bias &vtl.Tensor[f32],
	kernel_size []int,
	config Conv2DConfig) !&vtl.Tensor[f32]

conv2d_forward_f32 uses Vulkan im2col+GEMM when eligible (VTL_USE_VULKAN=1, -d vulkan), else CPU.

fn conv2d_forward_f64 #

fn conv2d_forward_f64(input &vtl.Tensor[f64],
	weight &vtl.Tensor[f64],
	bias &vtl.Tensor[f64],
	kernel_size []int,
	config Conv2DConfig) !&vtl.Tensor[f64]

conv2d_forward_f64 uses cuDNN when eligible (VTL_USE_CUDA=1, -d cuda), else CPU loops.

fn conv2d_forward_vulkan_f32 #

fn conv2d_forward_vulkan_f32(input &vtl.Tensor[f32],
	weight &vtl.Tensor[f32],
	bias &vtl.Tensor[f32],
	kernel_size []int,
	config Conv2DConfig) !&vtl.Tensor[f32]

conv2d_forward_vulkan_f32 stub when not built with -d vulkan.

fn conv2d_vulkan_backward_eligible #

fn conv2d_vulkan_backward_eligible(kernel_size []int, config Conv2DConfig) bool

conv2d_vulkan_backward_eligible exposes this operation as part of the public API.

fn conv2d_vulkan_eligible #

fn conv2d_vulkan_eligible(kernel_size []int, config Conv2DConfig) bool

conv2d_vulkan_eligible is false without -d vulkan.

fn conv_transpose1d_backward #

fn conv_transpose1d_backward[T](grad_output &vtl.Tensor[T], input &vtl.Tensor[T], weight &vtl.Tensor[T], bias &vtl.Tensor[T], config ConvTranspose1DConfig) ![]&vtl.Tensor[T]

conv_transpose1d_backward computes gradients with respect to input, weight, and bias for 1D transposed convolution.

fn conv_transpose1d_forward #

fn conv_transpose1d_forward[T](input &vtl.Tensor[T], weight &vtl.Tensor[T], bias &vtl.Tensor[T], config ConvTranspose1DConfig) !&vtl.Tensor[T]

conv_transpose1d_forward computes the forward pass of a 1D transposed convolution.

fn conv_transpose2d_backward #

fn conv_transpose2d_backward[T](grad_output &vtl.Tensor[T], input &vtl.Tensor[T], weight &vtl.Tensor[T], bias &vtl.Tensor[T], config ConvTranspose2DConfig) ![]&vtl.Tensor[T]

conv_transpose2d_backward computes gradients with respect to input, weight, and bias for 2D transposed convolution.

fn conv_transpose2d_forward #

fn conv_transpose2d_forward[T](input &vtl.Tensor[T], weight &vtl.Tensor[T], bias &vtl.Tensor[T], config ConvTranspose2DConfig) !&vtl.Tensor[T]

conv_transpose2d_forward computes the forward pass of a 2D transposed convolution.

fn cross_entropy #

fn cross_entropy[T](input &vtl.Tensor[T], target &vtl.Tensor[T]) !&vtl.Tensor[T]

cross_entropy computes mean CrossEntropyLoss from raw logits.

fn cross_entropy_backward #

fn cross_entropy_backward[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], target &vtl.Tensor[T]) !&vtl.Tensor[T]

cross_entropy_backward computes the mean CrossEntropyLoss gradient w.r.t. logits.

fn cross_entropy_backward_with_options #

fn cross_entropy_backward_with_options[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], target &vtl.Tensor[T], weight &vtl.Tensor[T], ignore_index int, reduction string, label_smoothing f64) !&vtl.Tensor[T]

cross_entropy_backward_with_options applies the NLL gradient to log-softmax and then its Jacobian to produce the gradient with respect to raw logits.

fn cross_entropy_with_options #

fn cross_entropy_with_options[T](input &vtl.Tensor[T], target &vtl.Tensor[T], weight &vtl.Tensor[T], ignore_index int, reduction string, label_smoothing f64) !&vtl.Tensor[T]

cross_entropy_with_options computes weighted CrossEntropyLoss with configurable ignored class indices and none, mean, or sum reduction.

fn deriv_elu #

@[inline]
fn deriv_elu[T](gradient &vtl.Tensor[T], cached &vtl.Tensor[T], alpha T) !&vtl.Tensor[T]

deriv_elu exposes this operation as part of the public API.

fn deriv_gelu #

@[direct_array_access]
fn deriv_gelu[T](gradient &vtl.Tensor[T], cached &vtl.Tensor[T]) !&vtl.Tensor[T]

deriv_gelu exposes this operation as part of the public API.

fn deriv_hardswish #

@[inline]
fn deriv_hardswish[T](gradient &vtl.Tensor[T], cached &vtl.Tensor[T]) !&vtl.Tensor[T]

deriv_hardswish multiplies by the piecewise derivative of HardSwish.

fn deriv_leaky_relu #

@[direct_array_access]
fn deriv_leaky_relu[T](gradient &vtl.Tensor[T], cached &vtl.Tensor[T], alpha T) !&vtl.Tensor[T]

deriv_leaky_relu exposes this operation as part of the public API.

fn deriv_mish #

@[inline]
fn deriv_mish[T](gradient &vtl.Tensor[T], cached &vtl.Tensor[T]) !&vtl.Tensor[T]

deriv_mish exposes this operation as part of the public API.

fn deriv_relu #

@[direct_array_access]
fn deriv_relu[T](gradient &vtl.Tensor[T], cached &vtl.Tensor[T]) !&vtl.Tensor[T]

deriv_relu exposes this operation as part of the public API.

fn deriv_selu #

@[inline]
fn deriv_selu[T](gradient &vtl.Tensor[T], cached &vtl.Tensor[T]) !&vtl.Tensor[T]

deriv_selu multiplies by the derivative of SELU at the cached input.

fn deriv_sigmoid #

@[direct_array_access]
fn deriv_sigmoid[T](gradient &vtl.Tensor[T], cached &vtl.Tensor[T]) !&vtl.Tensor[T]

deriv_sigmoid exposes this operation as part of the public API.

fn deriv_softmax #

@[inline]
fn deriv_softmax[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], dim int) !&vtl.Tensor[T]

deriv_softmax exposes this operation as part of the public API.

fn deriv_softplus #

@[direct_array_access]
fn deriv_softplus[T](gradient &vtl.Tensor[T], cached &vtl.Tensor[T]) !&vtl.Tensor[T]

deriv_softplus multiplies by sigmoid(x), the derivative of softplus.

fn deriv_swish #

@[inline]
fn deriv_swish[T](gradient &vtl.Tensor[T], cached &vtl.Tensor[T]) !&vtl.Tensor[T]

deriv_swish exposes this operation as part of the public API.

fn deriv_tanh #

@[direct_array_access]
fn deriv_tanh[T](gradient &vtl.Tensor[T], cached &vtl.Tensor[T]) !&vtl.Tensor[T]

deriv_tanh exposes this operation as part of the public API.

fn dropout #

fn dropout[T](input &vtl.Tensor[T], mask &vtl.Tensor[T], prob f64) !&vtl.Tensor[T]

dropout exposes this operation as part of the public API.

fn dropout_backwards #

fn dropout_backwards[T](gradient &vtl.Tensor[T], mask &vtl.Tensor[T], prob f64) !&vtl.Tensor[T]

dropout_backwards exposes this operation as part of the public API.

fn elu #

@[inline]
fn elu[T](x &vtl.Tensor[T], alpha T) &vtl.Tensor[T]

elu exposes this operation as part of the public API.

fn embedding_backward #

fn embedding_backward[T](grad_out &vtl.Tensor[T], input &vtl.Tensor[T], weight &vtl.Tensor[T]) ![]&vtl.Tensor[T]

embedding_backward computes gradient w.r.t. weight. Gradients are accumulated into the weight rows corresponding to the input indices.

fn embedding_forward #

fn embedding_forward[T](input &vtl.Tensor[T], weight &vtl.Tensor[T]) !&vtl.Tensor[T]

embedding_forward looks up each integer index in the weight matrix. input: [batch, seq_len] integer indices weight: [vocab_size, embedding_dim] returns: [batch, seq_len, embedding_dim]

fn focal #

fn focal[T](input &vtl.Tensor[T], target &vtl.Tensor[T], alpha f64, gamma f64, from_logits bool) !&vtl.Tensor[T]

focal computes mean binary focal loss. When from_logits is true input contains logits.

fn focal_backward #

fn focal_backward[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], target &vtl.Tensor[T], alpha f64, gamma f64, from_logits bool) !&vtl.Tensor[T]

focal_backward computes the gradient of mean binary focal loss.

fn gelu #

@[inline]
fn gelu[T](x &vtl.Tensor[T]) &vtl.Tensor[T]

gelu exposes this operation as part of the public API.

fn global_avgpool2d_backward #

fn global_avgpool2d_backward[T](grad_out &vtl.Tensor[T], input &vtl.Tensor[T]) !&vtl.Tensor[T]

global_avgpool2d_backward exposes this operation as part of the public API.

fn global_avgpool2d_forward #

fn global_avgpool2d_forward[T](input &vtl.Tensor[T]) !&vtl.Tensor[T]

global_avgpool2d_forward exposes this operation as part of the public API.

fn group_norm_backward #

fn group_norm_backward[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], num_groups int, gamma &vtl.Tensor[T], beta &vtl.Tensor[T], eps f64) ![]&vtl.Tensor[T]

group_norm_backward computes gradients for input and any present affine parameters.

fn group_norm_forward #

fn group_norm_forward[T](input &vtl.Tensor[T], num_groups int, gamma &vtl.Tensor[T], beta &vtl.Tensor[T], eps f64) !&vtl.Tensor[T]

group_norm_forward normalizes each sample over channel groups and all trailing spatial dimensions. Affine parameters, when present, are per-channel.

fn gru_backward_single #

fn gru_backward_single[T](input &vtl.Tensor[T], w_ih &vtl.Tensor[T], w_hh &vtl.Tensor[T],
	b_ih &vtl.Tensor[T], b_hh &vtl.Tensor[T], h0 &vtl.Tensor[T], grad_output &vtl.Tensor[T]) ![]&vtl.Tensor[T]

gru_backward_single differentiates the sum over timesteps supplied in grad_output. It returns gradients for input, w_ih, w_hh, b_ih, b_hh, and h0 respectively.

fn gru_backward_single_with_final_state #

fn gru_backward_single_with_final_state[T](input &vtl.Tensor[T], w_ih &vtl.Tensor[T],
	w_hh &vtl.Tensor[T], b_ih &vtl.Tensor[T], b_hh &vtl.Tensor[T], h0 &vtl.Tensor[T],
	grad_output &vtl.Tensor[T], grad_final_state &vtl.Tensor[T]) ![]&vtl.Tensor[T]

gru_backward_single_with_final_state also propagates a gradient supplied for the final hidden state returned by gru_forward_single.

fn gru_forward_single #

fn gru_forward_single[T](input &vtl.Tensor[T], w_ih &vtl.Tensor[T], w_hh &vtl.Tensor[T],
	b_ih &vtl.Tensor[T], b_hh &vtl.Tensor[T], h0 &vtl.Tensor[T]) !(&vtl.Tensor[T], &vtl.Tensor[T])

gru_forward_single computes a single-layer, unidirectional GRU. Input is [sequence, batch, input_size]. Weights follow PyTorch's gate order [reset, update, new] and shapes [3*hidden_size, input_size/hidden_size].

fn hardswish #

@[inline]
fn hardswish[T](x &vtl.Tensor[T]) &vtl.Tensor[T]

hardswish applies x * clamp(x + 3, 0, 6) / 6.

fn hinge #

fn hinge[T](input &vtl.Tensor[T], target &vtl.Tensor[T]) !&vtl.Tensor[T]

hinge computes mean max(0, 1 - target * score); labels must be -1 or +1.

fn hinge_backward #

fn hinge_backward[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], target &vtl.Tensor[T]) !&vtl.Tensor[T]

hinge_backward computes the gradient of mean hinge loss.

fn huber #

fn huber[T](input &vtl.Tensor[T], target &vtl.Tensor[T], delta T) !&vtl.Tensor[T]

huber computes the Huber loss (smooth L1)

fn huber_backward #

fn huber_backward[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], target &vtl.Tensor[T], delta T) !&vtl.Tensor[T]

huber_backward computes the gradient of Huber loss w.r.t. input

fn kaiming_normal #

fn kaiming_normal[T](shape []int) &vtl.Tensor[T]

kaiming_normal exposes this operation as part of the public API.

fn kaiming_uniform #

fn kaiming_uniform[T](shape []int) &vtl.Tensor[T]

kaiming_uniform exposes this operation as part of the public API.

fn kl_div #

fn kl_div[T](input &vtl.Tensor[T], target &vtl.Tensor[T]) !&vtl.Tensor[T]

kl_div computes KL Divergence loss D_KL(P || Q) = sum(P * log(P/Q)) input: log-probs Q, target: probabilities P

fn kl_div_backward #

fn kl_div_backward[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], target &vtl.Tensor[T]) !&vtl.Tensor[T]

kl_div_backward computes gradient of KL Divergence w.r.t. input (log-probs Q)

fn l1 #

fn l1[T](input &vtl.Tensor[T], target &vtl.Tensor[T]) !&vtl.Tensor[T]

l1 computes mean absolute error and returns a scalar tensor.

fn l1_backward #

fn l1_backward[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], target &vtl.Tensor[T]) !&vtl.Tensor[T]

l1_backward computes the mean absolute error gradient; the subgradient at zero is zero.

fn layer_norm_backward #

fn layer_norm_backward[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], gamma &vtl.Tensor[T], beta &vtl.Tensor[T], eps f64) ![]&vtl.Tensor[T]

layer_norm_backward computes gradients using the full input shape as the normalized shape, preserving the original low-level API behavior.

fn layer_norm_backward_shape #

fn layer_norm_backward_shape[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], gamma &vtl.Tensor[T], beta &vtl.Tensor[T], normalized_shape []int, eps f64) ![]&vtl.Tensor[T]

layer_norm_backward_shape computes input and affine gradients over trailing normalized_shape dimensions. Affine gradients reduce across leading groups.

fn layer_norm_forward #

fn layer_norm_forward[T](input &vtl.Tensor[T], gamma &vtl.Tensor[T], beta &vtl.Tensor[T], eps f64) !&vtl.Tensor[T]

layer_norm_forward computes layer normalization over all input elements. gamma, beta: optional affine parameters matching the normalized trailing shape

fn layer_norm_forward_shape #

fn layer_norm_forward_shape[T](input &vtl.Tensor[T], gamma &vtl.Tensor[T], beta &vtl.Tensor[T], normalized_shape []int, eps f64) !&vtl.Tensor[T]

layer_norm_forward_shape normalizes independently over each trailing block described by normalized_shape, matching the LayerNorm definition used by PyTorch. Affine parameters have exactly normalized_shape and are broadcast over all leading dimensions.

fn leaky_relu #

@[inline]
fn leaky_relu[T](x &vtl.Tensor[T], alpha T) &vtl.Tensor[T]

leaky_relu exposes this operation as part of the public API.

fn linear_backward_vulkan_f32 #

fn linear_backward_vulkan_f32(grad &vtl.Tensor[f32], input &vtl.Tensor[f32],
	weight &vtl.Tensor[f32], bias_needs_grad bool) ![]&vtl.Tensor[f32]

linear_backward_vulkan_f32 exposes this operation as part of the public API.

fn lstm_backward_single #

fn lstm_backward_single[T](input &vtl.Tensor[T], hidden0 &vtl.Tensor[T],
	cell0 &vtl.Tensor[T], w_ih &vtl.Tensor[T], w_hh &vtl.Tensor[T], b_ih &vtl.Tensor[T],
	b_hh &vtl.Tensor[T], grad_output &vtl.Tensor[T]) ![]&vtl.Tensor[T]

lstm_backward_single computes BPTT gradients for the full output sequence. Results are d_input, d_w_ih, d_w_hh, d_b_ih, d_b_hh, d_hidden0, d_cell0.

fn lstm_backward_single_with_final_state #

fn lstm_backward_single_with_final_state[T](input &vtl.Tensor[T], hidden0 &vtl.Tensor[T],
	cell0 &vtl.Tensor[T], w_ih &vtl.Tensor[T], w_hh &vtl.Tensor[T], b_ih &vtl.Tensor[T],
	b_hh &vtl.Tensor[T], grad_output &vtl.Tensor[T], grad_final_hidden &vtl.Tensor[T],
	grad_final_cell &vtl.Tensor[T]) ![]&vtl.Tensor[T]

lstm_backward_single_with_final_state also propagates gradients supplied for the final hidden and cell states returned by lstm_forward_single_with_cell.

fn lstm_cuda_eligible #

fn lstm_cuda_eligible() bool

lstm_cuda_eligible is always false without -d cuda, so the CPU path runs.

fn lstm_forward_multi #

fn lstm_forward_multi[T](input &vtl.Tensor[T], h0 &vtl.Tensor[T], w_ih &vtl.Tensor[T],
	w_hh &vtl.Tensor[T], b_ih &vtl.Tensor[T], b_hh &vtl.Tensor[T]) !(&vtl.Tensor[T], &vtl.Tensor[T])

lstm_forward_multi stacks single-layer LSTMs with an explicit initial hidden state for each layer and zero initial cell states.

fn lstm_forward_single #

fn lstm_forward_single[T](input &vtl.Tensor[T], hidden0 &vtl.Tensor[T], w_ih &vtl.Tensor[T],
	w_hh &vtl.Tensor[T], b_ih &vtl.Tensor[T], b_hh &vtl.Tensor[T]) !(&vtl.Tensor[T], &vtl.Tensor[T])

lstm_forward_single preserves the original zero-cell-state convenience API.

fn lstm_forward_single_with_cell #

fn lstm_forward_single_with_cell[T](input &vtl.Tensor[T], hidden0 &vtl.Tensor[T],
	cell0 &vtl.Tensor[T], w_ih &vtl.Tensor[T], w_hh &vtl.Tensor[T], b_ih &vtl.Tensor[T],
	b_hh &vtl.Tensor[T]) !(&vtl.Tensor[T], &vtl.Tensor[T], &vtl.Tensor[T])

lstm_forward_single_with_cell runs a single unidirectional LSTM layer. Input uses [sequence, batch, input_size], states use [batch, hidden_size], and weights use PyTorch gate order [input, forget, cell, output].

fn maxpool1d_backward #

fn maxpool1d_backward[T](grad_out &vtl.Tensor[T], input &vtl.Tensor[T], kernel int, padding int, stride int) !&vtl.Tensor[T]

maxpool1d_backward routes each output gradient to the maximum element in its 1D pooling window.

fn maxpool1d_forward #

fn maxpool1d_forward[T](input &vtl.Tensor[T], kernel int, padding int, stride int) !&vtl.Tensor[T]

maxpool1d_forward computes 1D max pooling over a 3D input tensor.

fn maxpool2d #

fn maxpool2d[T](input &vtl.Tensor[T], kernel []int, padding []int, stride []int) (&vtl.Tensor[int], &vtl.Tensor[T])

maxpool2d exposes this operation as part of the public API.

fn maxpool2d_backward #

fn maxpool2d_backward[T](shape []int, max_indices &vtl.Tensor[int], grad_output &vtl.Tensor[T]) !&vtl.Tensor[T]

maxpool2d_backward exposes this operation as part of the public API.

fn maxpool2d_backward_with_input #

fn maxpool2d_backward_with_input[T](shape []int, max_indices &vtl.Tensor[int], input &vtl.Tensor[T], grad_output &vtl.Tensor[T], kernel []int, stride []int, padding []int) !&vtl.Tensor[T]

maxpool2d_backward_with_input routes the gradient through cuDNN when maxpool2d_cudnn_eligible allows it (-d cuda, VTL_USE_CUDA=1, VTL_CUDA_BACKWARD=1). Otherwise it runs the CPU scatter in maxpool2d_backward.

fn maxpool3d #

fn maxpool3d[T](input &vtl.Tensor[T], kernel []int, padding []int, stride []int) !(&vtl.Tensor[int], &vtl.Tensor[T])

maxpool3d computes 3D max pooling over a 5D input tensor and returns both argmax indices and pooled values.

fn maxpool3d_backward #

fn maxpool3d_backward[T](shape []int, max_indices &vtl.Tensor[int], grad_output &vtl.Tensor[T]) !&vtl.Tensor[T]

maxpool3d_backward scatters output gradients back to the input positions recorded by maxpool3d.

fn mish #

@[inline]
fn mish[T](x &vtl.Tensor[T]) &vtl.Tensor[T]

mish exposes this operation as part of the public API.

fn mse #

@[inline]
fn mse[T](input &vtl.Tensor[T], target &vtl.Tensor[T]) !&vtl.Tensor[T]

mse exposes this operation as part of the public API.

fn mse_backward #

fn mse_backward[T](gradient &vtl.Tensor[T], cache &vtl.Tensor[T], target &vtl.Tensor[T]) ![]&vtl.Tensor[T]

mse_backward exposes this operation as part of the public API.

fn nll #

fn nll[T](input &vtl.Tensor[T], target &vtl.Tensor[T], weight &vtl.Tensor[T], ignore_index int, reduction string) !&vtl.Tensor[T]

nll computes Negative Log Likelihood from log-probabilities. Class-index targets omit the class axis; one-hot/soft targets match the input shape.

fn nll_backward #

fn nll_backward[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], target &vtl.Tensor[T], weight &vtl.Tensor[T], ignore_index int, reduction string) !&vtl.Tensor[T]

nll_backward computes the gradient of NLL w.r.t. input (log-probabilities).

fn poisson_nll #

fn poisson_nll[T](input &vtl.Tensor[T], target &vtl.Tensor[T], log_input bool, full bool, eps f64) !&vtl.Tensor[T]

poisson_nll computes the mean Poisson negative log-likelihood.

fn poisson_nll_backward #

fn poisson_nll_backward[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], target &vtl.Tensor[T], log_input bool, eps f64) !&vtl.Tensor[T]

poisson_nll_backward computes the gradient of Poisson negative log-likelihood loss with respect to input.

fn relu #

@[inline]
fn relu[T](x &vtl.Tensor[T]) &vtl.Tensor[T]

relu exposes this operation as part of the public API.

fn rms_norm_backward #

fn rms_norm_backward[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], weight &vtl.Tensor[T], normalized_shape []int, eps f64) ![]&vtl.Tensor[T]

rms_norm_backward computes input and optional per-element weight gradients.

fn rms_norm_forward #

fn rms_norm_forward[T](input &vtl.Tensor[T], weight &vtl.Tensor[T], normalized_shape []int, eps f64) !&vtl.Tensor[T]

rms_norm_forward normalizes each trailing normalized_shape block by its root mean square and optionally applies a per-element weight.

fn selu #

@[inline]
fn selu[T](x &vtl.Tensor[T]) &vtl.Tensor[T]

selu applies the scaled exponential linear unit.

fn sgd_optimize #

fn sgd_optimize[T](mut value vtl.Tensor[T], gradient &vtl.Tensor[T], learning_rate f64) !

sgd_optimize exposes this operation as part of the public API.

fn sigmoid #

@[inline]
fn sigmoid[T](x &vtl.Tensor[T]) &vtl.Tensor[T]

sigmoid exposes this operation as part of the public API.

fn sigmoid_cross_entropy #

@[inline]
fn sigmoid_cross_entropy[T](input &vtl.Tensor[T], target &vtl.Tensor[T]) !&vtl.Tensor[T]

sigmoid_cross_entropy exposes this operation as part of the public API.

fn sigmoid_cross_entropy_backward #

fn sigmoid_cross_entropy_backward[T](gradient &vtl.Tensor[T], cache &vtl.Tensor[T], target &vtl.Tensor[T]) ![]&vtl.Tensor[T]

sigmoid_cross_entropy_backward exposes this operation as part of the public API.

fn smooth_l1 #

fn smooth_l1[T](input &vtl.Tensor[T], target &vtl.Tensor[T], beta T, reduction string) !&vtl.Tensor[T]

smooth_l1 computes PyTorch-style Smooth L1 with the selected reduction.

fn smooth_l1_backward #

fn smooth_l1_backward[T](gradient &vtl.Tensor[T], input &vtl.Tensor[T], target &vtl.Tensor[T], beta T, reduction string) !&vtl.Tensor[T]

smooth_l1_backward computes the gradient of Smooth L1 for the selected reduction.

fn softmax_cross_entropy #

@[inline]
fn softmax_cross_entropy[T](input &vtl.Tensor[T], target &vtl.Tensor[T]) !&vtl.Tensor[T]

softmax_cross_entropy exposes this operation as part of the public API.

fn softmax_cross_entropy_backward #

fn softmax_cross_entropy_backward[T](gradient &vtl.Tensor[T], cache &vtl.Tensor[T], target &vtl.Tensor[T]) ![]&vtl.Tensor[T]

softmax_cross_entropy_backward computes the gradient of the SCE loss w.r.t. the logits. The gradient for each logit is: upstream * (sum(target) * softmax(logit_i) - target_i) / batch_size

This is the standard gradient derived from the log-softmax formulation (equivalent to Arraymancer's implementation).

fn softmax_forward #

fn softmax_forward[T](input &vtl.Tensor[T], dim int) !&vtl.Tensor[T]

softmax_forward computes softmax along a specified dimension.

fn softplus #

@[inline]
fn softplus[T](x &vtl.Tensor[T]) &vtl.Tensor[T]

softplus computes log(1 + exp(x)) using a stable formulation.

fn swish #

@[inline]
fn swish[T](x &vtl.Tensor[T]) &vtl.Tensor[T]

swish exposes this operation as part of the public API.

fn tanh #

@[inline]
fn tanh[T](x &vtl.Tensor[T]) &vtl.Tensor[T]

tanh exposes this operation as part of the public API.

fn variance_scaled #

fn variance_scaled[T](shape []int, scale T, fan_mode FanMode, distribution Distribution) &vtl.Tensor[T]

variance_scaled exposes this operation as part of the public API.

fn Distribution.from #

fn Distribution.from[W](input W) !Distribution

from returns the Distribution value of an integer or of a field name, or an error if there is none.

fn FanMode.from #

fn FanMode.from[W](input W) !FanMode

from returns the FanMode value of an integer or of a field name, or an error if there is none.

enum Distribution #

enum Distribution {
	uniform
	normal
}

Distribution lists the supported public values for this module.

enum FanMode #

enum FanMode {
	fan_avg
	fan_in
	fan_out
}

FanMode lists the supported public values for this module.

struct Conv1DConfig #

struct Conv1DConfig {
pub:
	stride   int = 1
	padding  int
	dilation int = 1
	groups   int = 1
}

Conv1DConfig configures a one-dimensional convolution.

struct Conv2DConfig #

struct Conv2DConfig {
pub:
	padding  []int = [0, 0]
	stride   []int = [1, 1]
	dilation []int = [1, 1]
	groups   int   = 1
}

Conv2DConfig mirrors vtl.nn.layers.Conv2DConfig to avoid import cycle.

struct ConvTranspose1DConfig #

@[params]
struct ConvTranspose1DConfig {
pub:
	stride         int = 1
	padding        int
	output_padding int
	dilation       int = 1
	groups         int = 1
}

ConvTranspose1DConfig configures 1D transposed convolution stride, padding, dilation, and groups.

struct ConvTranspose2DConfig #

@[params]
struct ConvTranspose2DConfig {
pub:
	stride         []int = [1, 1]
	padding        []int = [0, 0]
	output_padding []int = [0, 0]
	dilation       []int = [1, 1]
	groups         int   = 1
}

ConvTranspose2DConfig configures 2D transposed convolution stride, padding, dilation, and groups.